You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

s3_srvr.c 95 KiB

Fix TLS-PSK identity hint implementation issues. PSK identity hint can be stored in SSL_CTX and in SSL/SSL_SESSION, similar to other TLS parameters, with the value in SSL/SSL_SESSION taking precedence over the one in SSL_CTX. The value in SSL_CTX is shared (used as the default) between all SSL instances associated with that SSL_CTX, whereas the value in SSL/SSL_SESSION is confined to that particular TLS/SSL connection/session. The existing implementation of TLS-PSK does not correctly distinguish between PSK identity hint in SSL_CTX and in SSL/SSL_SESSION. This change fixes these issues: 1. SSL_use_psk_identity_hint does nothing and returns "success" when the SSL object does not have an associated SSL_SESSION. 2. On the client, the hint in SSL_CTX (which is shared between multiple SSL instances) is overwritten with the hint received from server or reset to NULL if no hint was received. 3. On the client, psk_client_callback is invoked with the hint from SSL_CTX rather than from current SSL/SSL_SESSION (i.e., the one received from the server). Issue #2 above masks this issue. 4. On the server, the hint in SSL/SSL_SESSION is ignored and the hint from SSL_CTX is sent to the client. 5. On the server, the hint in SSL/SSL_SESSION is reset to the one in SSL_CTX after the ClientKeyExchange message step. This change fixes the issues by: * Adding storage for the hint in the SSL object. The idea being that the hint in the associated SSL_SESSION takes precedence. * Reading the hint during the handshake only from the associated SSL_SESSION object. * Initializing the hint in SSL object with the one from the SSL_CTX object. * Initializing the hint in SSL_SESSION object with the one from the SSL object. * Making SSL_use_psk_identity_hint and SSL_get_psk_identity_hint set/get the hint to/from SSL_SESSION associated with the provided SSL object, or, if no SSL_SESSION is available, set/get the hint to/from the provided SSL object. * Removing code which resets the hint during handshake.
10 years ago
Fix TLS-PSK identity hint implementation issues. PSK identity hint can be stored in SSL_CTX and in SSL/SSL_SESSION, similar to other TLS parameters, with the value in SSL/SSL_SESSION taking precedence over the one in SSL_CTX. The value in SSL_CTX is shared (used as the default) between all SSL instances associated with that SSL_CTX, whereas the value in SSL/SSL_SESSION is confined to that particular TLS/SSL connection/session. The existing implementation of TLS-PSK does not correctly distinguish between PSK identity hint in SSL_CTX and in SSL/SSL_SESSION. This change fixes these issues: 1. SSL_use_psk_identity_hint does nothing and returns "success" when the SSL object does not have an associated SSL_SESSION. 2. On the client, the hint in SSL_CTX (which is shared between multiple SSL instances) is overwritten with the hint received from server or reset to NULL if no hint was received. 3. On the client, psk_client_callback is invoked with the hint from SSL_CTX rather than from current SSL/SSL_SESSION (i.e., the one received from the server). Issue #2 above masks this issue. 4. On the server, the hint in SSL/SSL_SESSION is ignored and the hint from SSL_CTX is sent to the client. 5. On the server, the hint in SSL/SSL_SESSION is reset to the one in SSL_CTX after the ClientKeyExchange message step. This change fixes the issues by: * Adding storage for the hint in the SSL object. The idea being that the hint in the associated SSL_SESSION takes precedence. * Reading the hint during the handshake only from the associated SSL_SESSION object. * Initializing the hint in SSL object with the one from the SSL_CTX object. * Initializing the hint in SSL_SESSION object with the one from the SSL object. * Making SSL_use_psk_identity_hint and SSL_get_psk_identity_hint set/get the hint to/from SSL_SESSION associated with the provided SSL object, or, if no SSL_SESSION is available, set/get the hint to/from the provided SSL object. * Removing code which resets the hint during handshake.
10 years ago
Fix TLS-PSK identity hint implementation issues. PSK identity hint can be stored in SSL_CTX and in SSL/SSL_SESSION, similar to other TLS parameters, with the value in SSL/SSL_SESSION taking precedence over the one in SSL_CTX. The value in SSL_CTX is shared (used as the default) between all SSL instances associated with that SSL_CTX, whereas the value in SSL/SSL_SESSION is confined to that particular TLS/SSL connection/session. The existing implementation of TLS-PSK does not correctly distinguish between PSK identity hint in SSL_CTX and in SSL/SSL_SESSION. This change fixes these issues: 1. SSL_use_psk_identity_hint does nothing and returns "success" when the SSL object does not have an associated SSL_SESSION. 2. On the client, the hint in SSL_CTX (which is shared between multiple SSL instances) is overwritten with the hint received from server or reset to NULL if no hint was received. 3. On the client, psk_client_callback is invoked with the hint from SSL_CTX rather than from current SSL/SSL_SESSION (i.e., the one received from the server). Issue #2 above masks this issue. 4. On the server, the hint in SSL/SSL_SESSION is ignored and the hint from SSL_CTX is sent to the client. 5. On the server, the hint in SSL/SSL_SESSION is reset to the one in SSL_CTX after the ClientKeyExchange message step. This change fixes the issues by: * Adding storage for the hint in the SSL object. The idea being that the hint in the associated SSL_SESSION takes precedence. * Reading the hint during the handshake only from the associated SSL_SESSION object. * Initializing the hint in SSL object with the one from the SSL_CTX object. * Initializing the hint in SSL_SESSION object with the one from the SSL object. * Making SSL_use_psk_identity_hint and SSL_get_psk_identity_hint set/get the hint to/from SSL_SESSION associated with the provided SSL object, or, if no SSL_SESSION is available, set/get the hint to/from the provided SSL object. * Removing code which resets the hint during handshake.
10 years ago
Fix TLS-PSK identity hint implementation issues. PSK identity hint can be stored in SSL_CTX and in SSL/SSL_SESSION, similar to other TLS parameters, with the value in SSL/SSL_SESSION taking precedence over the one in SSL_CTX. The value in SSL_CTX is shared (used as the default) between all SSL instances associated with that SSL_CTX, whereas the value in SSL/SSL_SESSION is confined to that particular TLS/SSL connection/session. The existing implementation of TLS-PSK does not correctly distinguish between PSK identity hint in SSL_CTX and in SSL/SSL_SESSION. This change fixes these issues: 1. SSL_use_psk_identity_hint does nothing and returns "success" when the SSL object does not have an associated SSL_SESSION. 2. On the client, the hint in SSL_CTX (which is shared between multiple SSL instances) is overwritten with the hint received from server or reset to NULL if no hint was received. 3. On the client, psk_client_callback is invoked with the hint from SSL_CTX rather than from current SSL/SSL_SESSION (i.e., the one received from the server). Issue #2 above masks this issue. 4. On the server, the hint in SSL/SSL_SESSION is ignored and the hint from SSL_CTX is sent to the client. 5. On the server, the hint in SSL/SSL_SESSION is reset to the one in SSL_CTX after the ClientKeyExchange message step. This change fixes the issues by: * Adding storage for the hint in the SSL object. The idea being that the hint in the associated SSL_SESSION takes precedence. * Reading the hint during the handshake only from the associated SSL_SESSION object. * Initializing the hint in SSL object with the one from the SSL_CTX object. * Initializing the hint in SSL_SESSION object with the one from the SSL object. * Making SSL_use_psk_identity_hint and SSL_get_psk_identity_hint set/get the hint to/from SSL_SESSION associated with the provided SSL object, or, if no SSL_SESSION is available, set/get the hint to/from the provided SSL object. * Removing code which resets the hint during handshake.
10 years ago
Fix TLS-PSK identity hint implementation issues. PSK identity hint can be stored in SSL_CTX and in SSL/SSL_SESSION, similar to other TLS parameters, with the value in SSL/SSL_SESSION taking precedence over the one in SSL_CTX. The value in SSL_CTX is shared (used as the default) between all SSL instances associated with that SSL_CTX, whereas the value in SSL/SSL_SESSION is confined to that particular TLS/SSL connection/session. The existing implementation of TLS-PSK does not correctly distinguish between PSK identity hint in SSL_CTX and in SSL/SSL_SESSION. This change fixes these issues: 1. SSL_use_psk_identity_hint does nothing and returns "success" when the SSL object does not have an associated SSL_SESSION. 2. On the client, the hint in SSL_CTX (which is shared between multiple SSL instances) is overwritten with the hint received from server or reset to NULL if no hint was received. 3. On the client, psk_client_callback is invoked with the hint from SSL_CTX rather than from current SSL/SSL_SESSION (i.e., the one received from the server). Issue #2 above masks this issue. 4. On the server, the hint in SSL/SSL_SESSION is ignored and the hint from SSL_CTX is sent to the client. 5. On the server, the hint in SSL/SSL_SESSION is reset to the one in SSL_CTX after the ClientKeyExchange message step. This change fixes the issues by: * Adding storage for the hint in the SSL object. The idea being that the hint in the associated SSL_SESSION takes precedence. * Reading the hint during the handshake only from the associated SSL_SESSION object. * Initializing the hint in SSL object with the one from the SSL_CTX object. * Initializing the hint in SSL_SESSION object with the one from the SSL object. * Making SSL_use_psk_identity_hint and SSL_get_psk_identity_hint set/get the hint to/from SSL_SESSION associated with the provided SSL object, or, if no SSL_SESSION is available, set/get the hint to/from the provided SSL object. * Removing code which resets the hint during handshake.
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  112. *
  113. * Portions of the attached software ("Contribution") are developed by
  114. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  115. *
  116. * The Contribution is licensed pursuant to the OpenSSL open source
  117. * license provided above.
  118. *
  119. * ECC cipher suite support in OpenSSL originally written by
  120. * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
  121. *
  122. */
  123. /* ====================================================================
  124. * Copyright 2005 Nokia. All rights reserved.
  125. *
  126. * The portions of the attached software ("Contribution") is developed by
  127. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  128. * license.
  129. *
  130. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  131. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  132. * support (see RFC 4279) to OpenSSL.
  133. *
  134. * No patent licenses or other rights except those expressly stated in
  135. * the OpenSSL open source license shall be deemed granted or received
  136. * expressly, by implication, estoppel, or otherwise.
  137. *
  138. * No assurances are provided by Nokia that the Contribution does not
  139. * infringe the patent or other intellectual property rights of any third
  140. * party or that the license provides you with all the necessary rights
  141. * to make use of the Contribution.
  142. *
  143. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  144. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  145. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  146. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  147. * OTHERWISE. */
  148. /* Undefined in Google code. We've never enabled this workaround
  149. * #define REUSE_CIPHER_BUG */
  150. #define NETSCAPE_HANG_BUG
  151. #include <stdio.h>
  152. #include <openssl/bn.h>
  153. #include <openssl/buf.h>
  154. #include <openssl/cipher.h>
  155. #include <openssl/dh.h>
  156. #include <openssl/ec.h>
  157. #include <openssl/ecdsa.h>
  158. #include <openssl/evp.h>
  159. #include <openssl/hmac.h>
  160. #include <openssl/md5.h>
  161. #include <openssl/mem.h>
  162. #include <openssl/obj.h>
  163. #include <openssl/rand.h>
  164. #include <openssl/sha.h>
  165. #include <openssl/x509.h>
  166. #include "ssl_locl.h"
  167. #include "../crypto/dh/internal.h"
  168. static const SSL_METHOD *ssl3_get_server_method(int ver);
  169. static const SSL_METHOD *ssl3_get_server_method(int ver)
  170. {
  171. if (ver == SSL3_VERSION)
  172. return(SSLv3_server_method());
  173. else
  174. return(NULL);
  175. }
  176. IMPLEMENT_ssl3_meth_func(SSLv3_server_method,
  177. ssl3_accept,
  178. ssl_undefined_function,
  179. ssl3_get_server_method)
  180. int ssl3_accept(SSL *s)
  181. {
  182. BUF_MEM *buf;
  183. unsigned long alg_k;
  184. unsigned long alg_a;
  185. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  186. int ret= -1;
  187. int new_state,state,skip=0;
  188. ERR_clear_error();
  189. ERR_clear_system_error();
  190. if (s->info_callback != NULL)
  191. cb=s->info_callback;
  192. else if (s->ctx->info_callback != NULL)
  193. cb=s->ctx->info_callback;
  194. /* init things to blank */
  195. s->in_handshake++;
  196. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  197. if (s->cert == NULL)
  198. {
  199. OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_NO_CERTIFICATE_SET);
  200. return(-1);
  201. }
  202. #ifndef OPENSSL_NO_HEARTBEATS
  203. /* If we're awaiting a HeartbeatResponse, pretend we
  204. * already got and don't await it anymore, because
  205. * Heartbeats don't make sense during handshakes anyway.
  206. */
  207. if (s->tlsext_hb_pending)
  208. {
  209. s->tlsext_hb_pending = 0;
  210. s->tlsext_hb_seq++;
  211. }
  212. #endif
  213. for (;;)
  214. {
  215. state=s->state;
  216. switch (s->state)
  217. {
  218. case SSL_ST_RENEGOTIATE:
  219. s->renegotiate=1;
  220. /* s->state=SSL_ST_ACCEPT; */
  221. case SSL_ST_BEFORE:
  222. case SSL_ST_ACCEPT:
  223. case SSL_ST_BEFORE|SSL_ST_ACCEPT:
  224. case SSL_ST_OK|SSL_ST_ACCEPT:
  225. s->server=1;
  226. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  227. if ((s->version>>8) != 3)
  228. {
  229. OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR);
  230. return -1;
  231. }
  232. s->type=SSL_ST_ACCEPT;
  233. if (s->init_buf == NULL)
  234. {
  235. if ((buf=BUF_MEM_new()) == NULL)
  236. {
  237. ret= -1;
  238. goto end;
  239. }
  240. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  241. {
  242. ret= -1;
  243. goto end;
  244. }
  245. s->init_buf=buf;
  246. }
  247. if (!ssl3_setup_buffers(s))
  248. {
  249. ret= -1;
  250. goto end;
  251. }
  252. s->init_num=0;
  253. s->s3->flags &= ~SSL3_FLAGS_SGC_RESTART_DONE;
  254. s->s3->flags &= ~TLS1_FLAGS_SKIP_CERT_VERIFY;
  255. if (s->state != SSL_ST_RENEGOTIATE)
  256. {
  257. /* Ok, we now need to push on a buffering BIO so that
  258. * the output is sent in a way that TCP likes :-)
  259. */
  260. if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
  261. ssl3_init_finished_mac(s);
  262. s->state=SSL3_ST_SR_CLNT_HELLO_A;
  263. s->ctx->stats.sess_accept++;
  264. }
  265. else if (!s->s3->send_connection_binding &&
  266. !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
  267. {
  268. /* Server attempting to renegotiate with
  269. * client that doesn't support secure
  270. * renegotiation.
  271. */
  272. OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
  273. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  274. ret = -1;
  275. goto end;
  276. }
  277. else
  278. {
  279. /* s->state == SSL_ST_RENEGOTIATE,
  280. * we will just send a HelloRequest */
  281. s->ctx->stats.sess_accept_renegotiate++;
  282. s->state=SSL3_ST_SW_HELLO_REQ_A;
  283. }
  284. break;
  285. case SSL3_ST_SW_HELLO_REQ_A:
  286. case SSL3_ST_SW_HELLO_REQ_B:
  287. s->shutdown=0;
  288. ret=ssl3_send_hello_request(s);
  289. if (ret <= 0) goto end;
  290. s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
  291. s->state=SSL3_ST_SW_FLUSH;
  292. s->init_num=0;
  293. ssl3_init_finished_mac(s);
  294. break;
  295. case SSL3_ST_SW_HELLO_REQ_C:
  296. s->state=SSL_ST_OK;
  297. break;
  298. case SSL3_ST_SR_CLNT_HELLO_A:
  299. case SSL3_ST_SR_CLNT_HELLO_B:
  300. case SSL3_ST_SR_CLNT_HELLO_C:
  301. case SSL3_ST_SR_CLNT_HELLO_D:
  302. s->shutdown=0;
  303. ret=ssl3_get_client_hello(s);
  304. if (ret == PENDING_SESSION) {
  305. s->rwstate = SSL_PENDING_SESSION;
  306. goto end;
  307. }
  308. if (ret == CERTIFICATE_SELECTION_PENDING)
  309. {
  310. s->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
  311. goto end;
  312. }
  313. if (ret <= 0) goto end;
  314. s->renegotiate = 2;
  315. s->state=SSL3_ST_SW_SRVR_HELLO_A;
  316. s->init_num=0;
  317. break;
  318. case SSL3_ST_SW_SRVR_HELLO_A:
  319. case SSL3_ST_SW_SRVR_HELLO_B:
  320. ret=ssl3_send_server_hello(s);
  321. if (ret <= 0) goto end;
  322. #ifndef OPENSSL_NO_TLSEXT
  323. if (s->hit)
  324. {
  325. if (s->tlsext_ticket_expected)
  326. s->state=SSL3_ST_SW_SESSION_TICKET_A;
  327. else
  328. s->state=SSL3_ST_SW_CHANGE_A;
  329. }
  330. #else
  331. if (s->hit)
  332. s->state=SSL3_ST_SW_CHANGE_A;
  333. #endif
  334. else
  335. #ifndef OPENSSL_NO_TLSEXT
  336. s->state = SSL3_ST_SW_SUPPLEMENTAL_DATA_A;
  337. #else
  338. s->state = SSL3_ST_SW_CERT_A;
  339. #endif
  340. s->init_num = 0;
  341. break;
  342. #ifndef OPENSSL_NO_TLSEXT
  343. case SSL3_ST_SW_SUPPLEMENTAL_DATA_A:
  344. case SSL3_ST_SW_SUPPLEMENTAL_DATA_B:
  345. /* We promised to send an audit proof in the hello. */
  346. if (s->s3->tlsext_authz_promised_to_client)
  347. {
  348. ret = tls1_send_server_supplemental_data(s);
  349. if (ret <= 0) goto end;
  350. }
  351. else
  352. skip = 1;
  353. s->state = SSL3_ST_SW_CERT_A;
  354. s->init_num = 0;
  355. break;
  356. #endif
  357. case SSL3_ST_SW_CERT_A:
  358. case SSL3_ST_SW_CERT_B:
  359. /* Check if it is anon DH or anon ECDH, */
  360. /* non-RSA PSK or KRB5 or SRP */
  361. if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
  362. /* Among PSK ciphersuites only RSA_PSK uses server certificate */
  363. && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK &&
  364. !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kRSA))
  365. && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
  366. {
  367. ret=ssl3_send_server_certificate(s);
  368. if (ret <= 0) goto end;
  369. #ifndef OPENSSL_NO_TLSEXT
  370. if (s->tlsext_status_expected)
  371. s->state=SSL3_ST_SW_CERT_STATUS_A;
  372. else
  373. s->state=SSL3_ST_SW_KEY_EXCH_A;
  374. }
  375. else
  376. {
  377. skip = 1;
  378. s->state=SSL3_ST_SW_KEY_EXCH_A;
  379. }
  380. #else
  381. }
  382. else
  383. skip=1;
  384. s->state=SSL3_ST_SW_KEY_EXCH_A;
  385. #endif
  386. s->init_num=0;
  387. break;
  388. case SSL3_ST_SW_KEY_EXCH_A:
  389. case SSL3_ST_SW_KEY_EXCH_B:
  390. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  391. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  392. /* clear this, it may get reset by
  393. * send_server_key_exchange */
  394. if ((s->options & SSL_OP_EPHEMERAL_RSA)
  395. )
  396. /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key
  397. * even when forbidden by protocol specs
  398. * (handshake may fail as clients are not required to
  399. * be able to handle this) */
  400. s->s3->tmp.use_rsa_tmp=1;
  401. else
  402. s->s3->tmp.use_rsa_tmp=0;
  403. /* only send if a DH key exchange, fortezza or
  404. * RSA but we have a sign only certificate
  405. *
  406. * PSK: may send PSK identity hints
  407. *
  408. * For ECC ciphersuites, we send a serverKeyExchange
  409. * message only if the cipher suite is either
  410. * ECDH-anon or ECDHE. In other cases, the
  411. * server certificate contains the server's
  412. * public key for key exchange.
  413. */
  414. if (s->s3->tmp.use_rsa_tmp
  415. /* PSK: send ServerKeyExchange if either:
  416. * - PSK identity hint is provided, or
  417. * - the key exchange is kEECDH. */
  418. #ifndef OPENSSL_NO_PSK
  419. || ((alg_a & SSL_aPSK) && ((alg_k & SSL_kEECDH) || s->session->psk_identity_hint))
  420. #endif
  421. || (alg_k & SSL_kEDH)
  422. || (alg_k & SSL_kEECDH)
  423. || ((alg_k & SSL_kRSA)
  424. && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
  425. || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
  426. && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
  427. )
  428. )
  429. )
  430. )
  431. {
  432. ret=ssl3_send_server_key_exchange(s);
  433. if (ret <= 0) goto end;
  434. }
  435. else
  436. skip=1;
  437. s->state=SSL3_ST_SW_CERT_REQ_A;
  438. s->init_num=0;
  439. break;
  440. case SSL3_ST_SW_CERT_REQ_A:
  441. case SSL3_ST_SW_CERT_REQ_B:
  442. if (/* don't request cert unless asked for it: */
  443. !(s->verify_mode & SSL_VERIFY_PEER) ||
  444. /* Don't request a certificate if an obc was presented */
  445. ((s->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
  446. s->s3->tlsext_channel_id_valid) ||
  447. /* if SSL_VERIFY_CLIENT_ONCE is set,
  448. * don't request cert during re-negotiation: */
  449. ((s->session->peer != NULL) &&
  450. (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
  451. /* never request cert in anonymous ciphersuites
  452. * (see section "Certificate request" in SSL 3 drafts
  453. * and in RFC 2246): */
  454. ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
  455. /* ... except when the application insists on verification
  456. * (against the specs, but s3_clnt.c accepts this for SSL 3) */
  457. !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
  458. /* never request cert in Kerberos ciphersuites */
  459. (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)
  460. /* With normal PSK Certificates and
  461. * Certificate Requests are omitted */
  462. || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
  463. {
  464. /* no cert request */
  465. skip=1;
  466. s->s3->tmp.cert_request=0;
  467. s->state=SSL3_ST_SW_SRVR_DONE_A;
  468. if (s->s3->handshake_buffer)
  469. if (!ssl3_digest_cached_records(s))
  470. return -1;
  471. }
  472. else
  473. {
  474. s->s3->tmp.cert_request=1;
  475. ret=ssl3_send_certificate_request(s);
  476. if (ret <= 0) goto end;
  477. #ifndef NETSCAPE_HANG_BUG
  478. s->state=SSL3_ST_SW_SRVR_DONE_A;
  479. #else
  480. s->state=SSL3_ST_SW_FLUSH;
  481. s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
  482. #endif
  483. s->init_num=0;
  484. }
  485. break;
  486. case SSL3_ST_SW_SRVR_DONE_A:
  487. case SSL3_ST_SW_SRVR_DONE_B:
  488. ret=ssl3_send_server_done(s);
  489. if (ret <= 0) goto end;
  490. s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
  491. s->state=SSL3_ST_SW_FLUSH;
  492. s->init_num=0;
  493. break;
  494. case SSL3_ST_SW_FLUSH:
  495. /* This code originally checked to see if
  496. * any data was pending using BIO_CTRL_INFO
  497. * and then flushed. This caused problems
  498. * as documented in PR#1939. The proposed
  499. * fix doesn't completely resolve this issue
  500. * as buggy implementations of BIO_CTRL_PENDING
  501. * still exist. So instead we just flush
  502. * unconditionally.
  503. */
  504. s->rwstate=SSL_WRITING;
  505. if (BIO_flush(s->wbio) <= 0)
  506. {
  507. ret= -1;
  508. goto end;
  509. }
  510. s->rwstate=SSL_NOTHING;
  511. s->state=s->s3->tmp.next_state;
  512. break;
  513. case SSL3_ST_SR_CERT_A:
  514. case SSL3_ST_SR_CERT_B:
  515. /* Check for second client hello (MS SGC) */
  516. ret = ssl3_check_client_hello(s);
  517. if (ret <= 0)
  518. goto end;
  519. if (ret == 2)
  520. s->state = SSL3_ST_SR_CLNT_HELLO_C;
  521. else {
  522. if (s->s3->tmp.cert_request)
  523. {
  524. ret=ssl3_get_client_certificate(s);
  525. if (ret <= 0) goto end;
  526. }
  527. s->init_num=0;
  528. s->state=SSL3_ST_SR_KEY_EXCH_A;
  529. }
  530. break;
  531. case SSL3_ST_SR_KEY_EXCH_A:
  532. case SSL3_ST_SR_KEY_EXCH_B:
  533. ret=ssl3_get_client_key_exchange(s);
  534. if (ret <= 0)
  535. goto end;
  536. if (ret == 2)
  537. {
  538. /* For the ECDH ciphersuites when
  539. * the client sends its ECDH pub key in
  540. * a certificate, the CertificateVerify
  541. * message is not sent.
  542. * Also for GOST ciphersuites when
  543. * the client uses its key from the certificate
  544. * for key exchange.
  545. */
  546. s->init_num = 0;
  547. s->state=SSL3_ST_SR_POST_CLIENT_CERT;
  548. }
  549. else if (SSL_USE_SIGALGS(s))
  550. {
  551. s->state=SSL3_ST_SR_CERT_VRFY_A;
  552. s->init_num=0;
  553. if (!s->session->peer)
  554. break;
  555. /* For sigalgs freeze the handshake buffer
  556. * at this point and digest cached records.
  557. */
  558. if (!s->s3->handshake_buffer)
  559. {
  560. OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR);
  561. return -1;
  562. }
  563. s->s3->flags |= TLS1_FLAGS_KEEP_HANDSHAKE;
  564. if (!ssl3_digest_cached_records(s))
  565. return -1;
  566. }
  567. else
  568. {
  569. int offset=0;
  570. int dgst_num;
  571. s->state=SSL3_ST_SR_CERT_VRFY_A;
  572. s->init_num=0;
  573. /* We need to get hashes here so if there is
  574. * a client cert, it can be verified
  575. * FIXME - digest processing for CertificateVerify
  576. * should be generalized. But it is next step
  577. */
  578. if (s->s3->handshake_buffer)
  579. if (!ssl3_digest_cached_records(s))
  580. return -1;
  581. for (dgst_num=0; dgst_num<SSL_MAX_DIGEST;dgst_num++)
  582. if (s->s3->handshake_dgst[dgst_num])
  583. {
  584. int dgst_size;
  585. s->method->ssl3_enc->cert_verify_mac(s,EVP_MD_CTX_type(s->s3->handshake_dgst[dgst_num]),&(s->s3->tmp.cert_verify_md[offset]));
  586. dgst_size=EVP_MD_CTX_size(s->s3->handshake_dgst[dgst_num]);
  587. if (dgst_size < 0)
  588. {
  589. ret = -1;
  590. goto end;
  591. }
  592. offset+=dgst_size;
  593. }
  594. }
  595. break;
  596. case SSL3_ST_SR_CERT_VRFY_A:
  597. case SSL3_ST_SR_CERT_VRFY_B:
  598. /* we should decide if we expected this one */
  599. ret=ssl3_get_cert_verify(s);
  600. if (ret <= 0) goto end;
  601. s->state=SSL3_ST_SR_POST_CLIENT_CERT;
  602. s->init_num=0;
  603. break;
  604. case SSL3_ST_SR_POST_CLIENT_CERT: {
  605. char next_proto_neg = 0;
  606. char channel_id = 0;
  607. #if !defined(OPENSSL_NO_TLSEXT)
  608. # if !defined(OPENSSL_NO_NEXTPROTONEG)
  609. next_proto_neg = s->s3->next_proto_neg_seen;
  610. # endif
  611. channel_id = s->s3->tlsext_channel_id_valid;
  612. #endif
  613. if (next_proto_neg)
  614. s->state=SSL3_ST_SR_NEXT_PROTO_A;
  615. else if (channel_id)
  616. s->state=SSL3_ST_SR_CHANNEL_ID_A;
  617. else
  618. s->state=SSL3_ST_SR_FINISHED_A;
  619. break;
  620. }
  621. #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
  622. case SSL3_ST_SR_NEXT_PROTO_A:
  623. case SSL3_ST_SR_NEXT_PROTO_B:
  624. ret=ssl3_get_next_proto(s);
  625. if (ret <= 0) goto end;
  626. s->init_num = 0;
  627. if (s->s3->tlsext_channel_id_valid)
  628. s->state=SSL3_ST_SR_CHANNEL_ID_A;
  629. else
  630. s->state=SSL3_ST_SR_FINISHED_A;
  631. break;
  632. #endif
  633. #if !defined(OPENSSL_NO_TLSEXT)
  634. case SSL3_ST_SR_CHANNEL_ID_A:
  635. case SSL3_ST_SR_CHANNEL_ID_B:
  636. ret=ssl3_get_channel_id(s);
  637. if (ret <= 0) goto end;
  638. s->init_num = 0;
  639. s->state=SSL3_ST_SR_FINISHED_A;
  640. break;
  641. #endif
  642. case SSL3_ST_SR_FINISHED_A:
  643. case SSL3_ST_SR_FINISHED_B:
  644. ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
  645. SSL3_ST_SR_FINISHED_B);
  646. if (ret <= 0) goto end;
  647. if (s->hit)
  648. s->state=SSL_ST_OK;
  649. #ifndef OPENSSL_NO_TLSEXT
  650. else if (s->tlsext_ticket_expected)
  651. s->state=SSL3_ST_SW_SESSION_TICKET_A;
  652. #endif
  653. else
  654. s->state=SSL3_ST_SW_CHANGE_A;
  655. /* If this is a full handshake with ChannelID then
  656. * record the hashshake hashes in |s->session| in case
  657. * we need them to verify a ChannelID signature on a
  658. * resumption of this session in the future. */
  659. if (!s->hit && s->s3->tlsext_channel_id_new)
  660. {
  661. ret = tls1_record_handshake_hashes_for_channel_id(s);
  662. if (ret <= 0) goto end;
  663. }
  664. s->init_num=0;
  665. break;
  666. #ifndef OPENSSL_NO_TLSEXT
  667. case SSL3_ST_SW_SESSION_TICKET_A:
  668. case SSL3_ST_SW_SESSION_TICKET_B:
  669. ret=ssl3_send_newsession_ticket(s);
  670. if (ret <= 0) goto end;
  671. s->state=SSL3_ST_SW_CHANGE_A;
  672. s->init_num=0;
  673. break;
  674. case SSL3_ST_SW_CERT_STATUS_A:
  675. case SSL3_ST_SW_CERT_STATUS_B:
  676. ret=ssl3_send_cert_status(s);
  677. if (ret <= 0) goto end;
  678. s->state=SSL3_ST_SW_KEY_EXCH_A;
  679. s->init_num=0;
  680. break;
  681. #endif
  682. case SSL3_ST_SW_CHANGE_A:
  683. case SSL3_ST_SW_CHANGE_B:
  684. s->session->cipher=s->s3->tmp.new_cipher;
  685. if (!s->method->ssl3_enc->setup_key_block(s))
  686. { ret= -1; goto end; }
  687. ret=ssl3_send_change_cipher_spec(s,
  688. SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
  689. if (ret <= 0) goto end;
  690. s->state=SSL3_ST_SW_FINISHED_A;
  691. s->init_num=0;
  692. if (!s->method->ssl3_enc->change_cipher_state(s,
  693. SSL3_CHANGE_CIPHER_SERVER_WRITE))
  694. {
  695. ret= -1;
  696. goto end;
  697. }
  698. break;
  699. case SSL3_ST_SW_FINISHED_A:
  700. case SSL3_ST_SW_FINISHED_B:
  701. ret=ssl3_send_finished(s,
  702. SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
  703. s->method->ssl3_enc->server_finished_label,
  704. s->method->ssl3_enc->server_finished_label_len);
  705. if (ret <= 0) goto end;
  706. s->state=SSL3_ST_SW_FLUSH;
  707. if (s->hit)
  708. s->s3->tmp.next_state=SSL3_ST_SR_POST_CLIENT_CERT;
  709. else
  710. s->s3->tmp.next_state=SSL_ST_OK;
  711. s->init_num=0;
  712. break;
  713. case SSL_ST_OK:
  714. /* clean a few things up */
  715. ssl3_cleanup_key_block(s);
  716. BUF_MEM_free(s->init_buf);
  717. s->init_buf=NULL;
  718. /* remove buffering on output */
  719. ssl_free_wbio_buffer(s);
  720. s->init_num=0;
  721. /* If we aren't retaining peer certificates then we can
  722. * discard it now. */
  723. if (s->session->peer && s->ctx->retain_only_sha256_of_client_certs)
  724. {
  725. X509_free(s->session->peer);
  726. s->session->peer = NULL;
  727. }
  728. if (s->renegotiate == 2) /* skipped if we just sent a HelloRequest */
  729. {
  730. s->renegotiate=0;
  731. s->new_session=0;
  732. ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
  733. s->ctx->stats.sess_accept_good++;
  734. /* s->server=1; */
  735. s->handshake_func=ssl3_accept;
  736. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  737. }
  738. ret = 1;
  739. goto end;
  740. /* break; */
  741. default:
  742. OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_UNKNOWN_STATE);
  743. ret= -1;
  744. goto end;
  745. /* break; */
  746. }
  747. if (!s->s3->tmp.reuse_message && !skip)
  748. {
  749. if (s->debug)
  750. {
  751. if ((ret=BIO_flush(s->wbio)) <= 0)
  752. goto end;
  753. }
  754. if ((cb != NULL) && (s->state != state))
  755. {
  756. new_state=s->state;
  757. s->state=state;
  758. cb(s,SSL_CB_ACCEPT_LOOP,1);
  759. s->state=new_state;
  760. }
  761. }
  762. skip=0;
  763. }
  764. end:
  765. /* BIO_flush(s->wbio); */
  766. s->in_handshake--;
  767. if (cb != NULL)
  768. cb(s,SSL_CB_ACCEPT_EXIT,ret);
  769. return(ret);
  770. }
  771. int ssl3_send_hello_request(SSL *s)
  772. {
  773. if (s->state == SSL3_ST_SW_HELLO_REQ_A)
  774. {
  775. ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0);
  776. s->state=SSL3_ST_SW_HELLO_REQ_B;
  777. }
  778. /* SSL3_ST_SW_HELLO_REQ_B */
  779. return ssl_do_write(s);
  780. }
  781. int ssl3_check_client_hello(SSL *s)
  782. {
  783. int ok;
  784. long n;
  785. /* this function is called when we really expect a Certificate message,
  786. * so permit appropriate message length */
  787. n=s->method->ssl_get_message(s,
  788. SSL3_ST_SR_CERT_A,
  789. SSL3_ST_SR_CERT_B,
  790. -1,
  791. s->max_cert_list,
  792. &ok);
  793. if (!ok) return((int)n);
  794. s->s3->tmp.reuse_message = 1;
  795. if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO)
  796. {
  797. /* We only allow the client to restart the handshake once per
  798. * negotiation. */
  799. if (s->s3->flags & SSL3_FLAGS_SGC_RESTART_DONE)
  800. {
  801. OPENSSL_PUT_ERROR(SSL, ssl3_check_client_hello, SSL_R_MULTIPLE_SGC_RESTARTS);
  802. return -1;
  803. }
  804. /* Throw away what we have done so far in the current handshake,
  805. * which will now be aborted. (A full SSL_clear would be too much.) */
  806. #ifndef OPENSSL_NO_DH
  807. if (s->s3->tmp.dh != NULL)
  808. {
  809. DH_free(s->s3->tmp.dh);
  810. s->s3->tmp.dh = NULL;
  811. }
  812. #endif
  813. #ifndef OPENSSL_NO_ECDH
  814. if (s->s3->tmp.ecdh != NULL)
  815. {
  816. EC_KEY_free(s->s3->tmp.ecdh);
  817. s->s3->tmp.ecdh = NULL;
  818. }
  819. #endif
  820. s->s3->flags |= SSL3_FLAGS_SGC_RESTART_DONE;
  821. return 2;
  822. }
  823. return 1;
  824. }
  825. int ssl3_get_client_hello(SSL *s)
  826. {
  827. int i,j,ok,al=SSL_AD_INTERNAL_ERROR,ret= -1;
  828. unsigned int cookie_len;
  829. long n;
  830. unsigned long id;
  831. unsigned char *p,*d;
  832. SSL_CIPHER *c;
  833. STACK_OF(SSL_CIPHER) *ciphers=NULL;
  834. struct ssl_early_callback_ctx early_ctx;
  835. /* We do this so that we will respond with our native type.
  836. * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
  837. * This down switching should be handled by a different method.
  838. * If we are SSLv3, we will respond with SSLv3, even if prompted with
  839. * TLSv1.
  840. */
  841. switch (s->state) {
  842. case SSL3_ST_SR_CLNT_HELLO_A:
  843. s->state=SSL3_ST_SR_CLNT_HELLO_B;
  844. /* fallthrough */
  845. case SSL3_ST_SR_CLNT_HELLO_B:
  846. s->first_packet=1;
  847. n=s->method->ssl_get_message(s,
  848. SSL3_ST_SR_CLNT_HELLO_B,
  849. SSL3_ST_SR_CLNT_HELLO_C,
  850. SSL3_MT_CLIENT_HELLO,
  851. SSL3_RT_MAX_PLAIN_LENGTH,
  852. &ok);
  853. if (!ok) return((int)n);
  854. s->first_packet=0;
  855. /* If we require cookies and this ClientHello doesn't
  856. * contain one, just return since we do not want to
  857. * allocate any memory yet. So check cookie length...
  858. */
  859. if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)
  860. {
  861. unsigned int session_length, cookie_length;
  862. p = (unsigned char *) s->init_msg;
  863. if (n < 2 + SSL3_RANDOM_SIZE)
  864. return 1;
  865. session_length = *(p + 2 + SSL3_RANDOM_SIZE);
  866. if (n < 2 + SSL3_RANDOM_SIZE + 1 + session_length)
  867. return 1;
  868. cookie_length =
  869. *(p + 2 + SSL3_RANDOM_SIZE + 1 + session_length);
  870. if (cookie_length == 0)
  871. return 1;
  872. }
  873. s->state = SSL3_ST_SR_CLNT_HELLO_C;
  874. /* fallthrough */
  875. case SSL3_ST_SR_CLNT_HELLO_C:
  876. case SSL3_ST_SR_CLNT_HELLO_D:
  877. /* We have previously parsed the ClientHello message,
  878. * and can't call ssl_get_message again without hashing
  879. * the message into the Finished digest again. */
  880. n = s->init_num;
  881. memset(&early_ctx, 0, sizeof(early_ctx));
  882. early_ctx.ssl = s;
  883. early_ctx.client_hello = s->init_msg;
  884. early_ctx.client_hello_len = n;
  885. if (!ssl_early_callback_init(&early_ctx))
  886. {
  887. al = SSL_AD_DECODE_ERROR;
  888. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CLIENTHELLO_PARSE_FAILED);
  889. goto f_err;
  890. }
  891. if (s->state == SSL3_ST_SR_CLNT_HELLO_C &&
  892. s->ctx->select_certificate_cb != NULL)
  893. {
  894. int ret;
  895. s->state = SSL3_ST_SR_CLNT_HELLO_D;
  896. ret = s->ctx->select_certificate_cb(&early_ctx);
  897. if (ret == 0)
  898. return CERTIFICATE_SELECTION_PENDING;
  899. else if (ret == -1)
  900. {
  901. /* Connection rejected. */
  902. al = SSL_AD_ACCESS_DENIED;
  903. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CONNECTION_REJECTED);
  904. goto f_err;
  905. }
  906. }
  907. s->state = SSL3_ST_SR_CLNT_HELLO_D;
  908. default:
  909. return -1;
  910. }
  911. d=p=(unsigned char *)s->init_msg;
  912. /* use version from inside client hello, not from record header
  913. * (may differ: see RFC 2246, Appendix E, second paragraph) */
  914. s->client_version=(((int)p[0])<<8)|(int)p[1];
  915. p+=2;
  916. if (SSL_IS_DTLS(s) ? (s->client_version > s->version &&
  917. s->method->version != DTLS_ANY_VERSION)
  918. : (s->client_version < s->version))
  919. {
  920. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_WRONG_VERSION_NUMBER);
  921. if ((s->client_version>>8) == SSL3_VERSION_MAJOR &&
  922. !s->enc_write_ctx && !s->write_hash)
  923. {
  924. /* similar to ssl3_get_record, send alert using remote version number */
  925. s->version = s->client_version;
  926. }
  927. al = SSL_AD_PROTOCOL_VERSION;
  928. goto f_err;
  929. }
  930. /* load the client random */
  931. memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE);
  932. p+=SSL3_RANDOM_SIZE;
  933. /* get the session-id */
  934. j= *(p++);
  935. s->hit=0;
  936. /* Versions before 0.9.7 always allow clients to resume sessions in renegotiation.
  937. * 0.9.7 and later allow this by default, but optionally ignore resumption requests
  938. * with flag SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
  939. * than a change to default behavior so that applications relying on this for security
  940. * won't even compile against older library versions).
  941. *
  942. * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to request
  943. * renegotiation but not a new session (s->new_session remains unset): for servers,
  944. * this essentially just means that the SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
  945. * setting will be ignored.
  946. */
  947. if ((s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)))
  948. {
  949. if (!ssl_get_new_session(s,1))
  950. goto err;
  951. }
  952. else
  953. {
  954. i=ssl_get_prev_session(s, &early_ctx);
  955. if (i == 1)
  956. { /* previous session */
  957. s->hit=1;
  958. }
  959. else if (i == -1)
  960. goto err;
  961. else if (i == PENDING_SESSION)
  962. {
  963. ret = PENDING_SESSION;
  964. goto err;
  965. }
  966. else /* i == 0 */
  967. {
  968. if (!ssl_get_new_session(s,1))
  969. goto err;
  970. }
  971. }
  972. p+=j;
  973. if (SSL_IS_DTLS(s))
  974. {
  975. /* cookie stuff */
  976. cookie_len = *(p++);
  977. /*
  978. * The ClientHello may contain a cookie even if the
  979. * HelloVerify message has not been sent--make sure that it
  980. * does not cause an overflow.
  981. */
  982. if ( cookie_len > sizeof(s->d1->rcvd_cookie))
  983. {
  984. /* too much data */
  985. al = SSL_AD_DECODE_ERROR;
  986. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_COOKIE_MISMATCH);
  987. goto f_err;
  988. }
  989. /* verify the cookie if appropriate option is set. */
  990. if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) &&
  991. cookie_len > 0)
  992. {
  993. memcpy(s->d1->rcvd_cookie, p, cookie_len);
  994. if ( s->ctx->app_verify_cookie_cb != NULL)
  995. {
  996. if ( s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie,
  997. cookie_len) == 0)
  998. {
  999. al=SSL_AD_HANDSHAKE_FAILURE;
  1000. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_COOKIE_MISMATCH);
  1001. goto f_err;
  1002. }
  1003. /* else cookie verification succeeded */
  1004. }
  1005. else if ( memcmp(s->d1->rcvd_cookie, s->d1->cookie,
  1006. s->d1->cookie_len) != 0) /* default verification */
  1007. {
  1008. al=SSL_AD_HANDSHAKE_FAILURE;
  1009. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_COOKIE_MISMATCH);
  1010. goto f_err;
  1011. }
  1012. /* Set to -2 so if successful we return 2 */
  1013. ret = -2;
  1014. }
  1015. p += cookie_len;
  1016. if (s->method->version == DTLS_ANY_VERSION)
  1017. {
  1018. /* Select version to use */
  1019. if (s->client_version <= DTLS1_2_VERSION &&
  1020. !(s->options & SSL_OP_NO_DTLSv1_2))
  1021. {
  1022. s->version = DTLS1_2_VERSION;
  1023. s->method = DTLSv1_2_server_method();
  1024. }
  1025. else if (tls1_suiteb(s))
  1026. {
  1027. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
  1028. s->version = s->client_version;
  1029. al = SSL_AD_PROTOCOL_VERSION;
  1030. goto f_err;
  1031. }
  1032. else if (s->client_version <= DTLS1_VERSION &&
  1033. !(s->options & SSL_OP_NO_DTLSv1))
  1034. {
  1035. s->version = DTLS1_VERSION;
  1036. s->method = DTLSv1_server_method();
  1037. }
  1038. else
  1039. {
  1040. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_WRONG_VERSION_NUMBER);
  1041. s->version = s->client_version;
  1042. al = SSL_AD_PROTOCOL_VERSION;
  1043. goto f_err;
  1044. }
  1045. s->session->ssl_version = s->version;
  1046. }
  1047. }
  1048. n2s(p,i);
  1049. if ((i == 0) && (j != 0))
  1050. {
  1051. /* we need a cipher if we are not resuming a session */
  1052. al=SSL_AD_ILLEGAL_PARAMETER;
  1053. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_CIPHERS_SPECIFIED);
  1054. goto f_err;
  1055. }
  1056. if ((p+i) >= (d+n))
  1057. {
  1058. /* not enough data */
  1059. al=SSL_AD_DECODE_ERROR;
  1060. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_LENGTH_MISMATCH);
  1061. goto f_err;
  1062. }
  1063. if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers))
  1064. == NULL))
  1065. {
  1066. goto err;
  1067. }
  1068. p+=i;
  1069. /* If it is a hit, check that the cipher is in the list */
  1070. if ((s->hit) && (i > 0))
  1071. {
  1072. j=0;
  1073. id=s->session->cipher->id;
  1074. #ifdef CIPHER_DEBUG
  1075. printf("client sent %d ciphers\n",sk_num(ciphers));
  1076. #endif
  1077. for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++)
  1078. {
  1079. c=sk_SSL_CIPHER_value(ciphers,i);
  1080. #ifdef CIPHER_DEBUG
  1081. printf("client [%2d of %2d]:%s\n",
  1082. i,sk_num(ciphers),SSL_CIPHER_get_name(c));
  1083. #endif
  1084. if (c->id == id)
  1085. {
  1086. j=1;
  1087. break;
  1088. }
  1089. }
  1090. /* Disabled because it can be used in a ciphersuite downgrade
  1091. * attack: CVE-2010-4180.
  1092. */
  1093. #if 0
  1094. if (j == 0 && (s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1))
  1095. {
  1096. /* Special case as client bug workaround: the previously used cipher may
  1097. * not be in the current list, the client instead might be trying to
  1098. * continue using a cipher that before wasn't chosen due to server
  1099. * preferences. We'll have to reject the connection if the cipher is not
  1100. * enabled, though. */
  1101. c = sk_SSL_CIPHER_value(ciphers, 0);
  1102. if (sk_SSL_CIPHER_find(SSL_get_ciphers(s), c) >= 0)
  1103. {
  1104. s->session->cipher = c;
  1105. j = 1;
  1106. }
  1107. }
  1108. #endif
  1109. if (j == 0)
  1110. {
  1111. /* we need to have the cipher in the cipher
  1112. * list if we are asked to reuse it */
  1113. al=SSL_AD_ILLEGAL_PARAMETER;
  1114. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_REQUIRED_CIPHER_MISSING);
  1115. goto f_err;
  1116. }
  1117. }
  1118. /* compression */
  1119. i= *(p++);
  1120. if ((p+i) > (d+n))
  1121. {
  1122. /* not enough data */
  1123. al=SSL_AD_DECODE_ERROR;
  1124. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_LENGTH_MISMATCH);
  1125. goto f_err;
  1126. }
  1127. for (j=0; j<i; j++)
  1128. {
  1129. if (p[j] == 0) break;
  1130. }
  1131. p+=i;
  1132. if (j >= i)
  1133. {
  1134. /* no compress */
  1135. al=SSL_AD_DECODE_ERROR;
  1136. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_COMPRESSION_SPECIFIED);
  1137. goto f_err;
  1138. }
  1139. #ifndef OPENSSL_NO_TLSEXT
  1140. /* TLS extensions*/
  1141. if (s->version >= SSL3_VERSION)
  1142. {
  1143. if (!ssl_parse_clienthello_tlsext(s,&p,d,n))
  1144. {
  1145. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_PARSE_TLSEXT);
  1146. goto err;
  1147. }
  1148. }
  1149. /* Check if we want to use external pre-shared secret for this
  1150. * handshake for not reused session only. We need to generate
  1151. * server_random before calling tls_session_secret_cb in order to allow
  1152. * SessionTicket processing to use it in key derivation. */
  1153. {
  1154. unsigned char *pos;
  1155. pos=s->s3->server_random;
  1156. if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0)
  1157. {
  1158. goto f_err;
  1159. }
  1160. }
  1161. if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
  1162. {
  1163. SSL_CIPHER *pref_cipher=NULL;
  1164. s->session->master_key_length=sizeof(s->session->master_key);
  1165. if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
  1166. ciphers, &pref_cipher, s->tls_session_secret_cb_arg))
  1167. {
  1168. s->hit=1;
  1169. s->session->ciphers=ciphers;
  1170. s->session->verify_result=X509_V_OK;
  1171. ciphers=NULL;
  1172. /* check if some cipher was preferred by call back */
  1173. pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, ssl_get_cipher_preferences(s));
  1174. if (pref_cipher == NULL)
  1175. {
  1176. al=SSL_AD_HANDSHAKE_FAILURE;
  1177. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_SHARED_CIPHER);
  1178. goto f_err;
  1179. }
  1180. s->session->cipher=pref_cipher;
  1181. if (s->cipher_list)
  1182. ssl_cipher_preference_list_free(s->cipher_list);
  1183. if (s->cipher_list_by_id)
  1184. sk_SSL_CIPHER_free(s->cipher_list_by_id);
  1185. s->cipher_list = ssl_cipher_preference_list_from_ciphers(s->session->ciphers);
  1186. s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
  1187. }
  1188. }
  1189. #endif
  1190. /* Worst case, we will use the NULL compression, but if we have other
  1191. * options, we will now look for them. We have i-1 compression
  1192. * algorithms from the client, starting at q. */
  1193. s->s3->tmp.new_compression=NULL;
  1194. /* If compression is disabled we'd better not try to resume a session
  1195. * using compression.
  1196. */
  1197. if (s->session->compress_meth != 0)
  1198. {
  1199. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_INCONSISTENT_COMPRESSION);
  1200. goto f_err;
  1201. }
  1202. /* Given s->session->ciphers and SSL_get_ciphers, we must
  1203. * pick a cipher */
  1204. if (!s->hit)
  1205. {
  1206. s->session->compress_meth=0;
  1207. if (s->session->ciphers != NULL)
  1208. sk_SSL_CIPHER_free(s->session->ciphers);
  1209. s->session->ciphers=ciphers;
  1210. if (ciphers == NULL)
  1211. {
  1212. al=SSL_AD_ILLEGAL_PARAMETER;
  1213. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_CIPHERS_PASSED);
  1214. goto f_err;
  1215. }
  1216. ciphers=NULL;
  1217. /* Let cert callback update server certificates if required */
  1218. if (s->cert->cert_cb)
  1219. {
  1220. int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
  1221. if (rv == 0)
  1222. {
  1223. al=SSL_AD_INTERNAL_ERROR;
  1224. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CERT_CB_ERROR);
  1225. goto f_err;
  1226. }
  1227. if (rv < 0)
  1228. {
  1229. s->rwstate=SSL_X509_LOOKUP;
  1230. return -1;
  1231. }
  1232. s->rwstate = SSL_NOTHING;
  1233. }
  1234. c=ssl3_choose_cipher(s,s->session->ciphers,
  1235. ssl_get_cipher_preferences(s));
  1236. if (c == NULL)
  1237. {
  1238. al=SSL_AD_HANDSHAKE_FAILURE;
  1239. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_SHARED_CIPHER);
  1240. goto f_err;
  1241. }
  1242. s->s3->tmp.new_cipher=c;
  1243. }
  1244. else
  1245. {
  1246. /* Session-id reuse */
  1247. #ifdef REUSE_CIPHER_BUG
  1248. STACK_OF(SSL_CIPHER) *sk;
  1249. SSL_CIPHER *nc=NULL;
  1250. SSL_CIPHER *ec=NULL;
  1251. if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG)
  1252. {
  1253. sk=s->session->ciphers;
  1254. for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
  1255. {
  1256. c=sk_SSL_CIPHER_value(sk,i);
  1257. if (c->algorithm_enc & SSL_eNULL)
  1258. nc=c;
  1259. if (SSL_C_IS_EXPORT(c))
  1260. ec=c;
  1261. }
  1262. if (nc != NULL)
  1263. s->s3->tmp.new_cipher=nc;
  1264. else if (ec != NULL)
  1265. s->s3->tmp.new_cipher=ec;
  1266. else
  1267. s->s3->tmp.new_cipher=s->session->cipher;
  1268. }
  1269. else
  1270. #endif
  1271. s->s3->tmp.new_cipher=s->session->cipher;
  1272. }
  1273. if (!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER))
  1274. {
  1275. if (!ssl3_digest_cached_records(s))
  1276. goto f_err;
  1277. }
  1278. /* we now have the following setup.
  1279. * client_random
  1280. * cipher_list - our prefered list of ciphers
  1281. * ciphers - the clients prefered list of ciphers
  1282. * compression - basically ignored right now
  1283. * ssl version is set - sslv3
  1284. * s->session - The ssl session has been setup.
  1285. * s->hit - session reuse flag
  1286. * s->tmp.new_cipher - the new cipher to use.
  1287. */
  1288. /* Handles TLS extensions that we couldn't check earlier */
  1289. if (s->version >= SSL3_VERSION)
  1290. {
  1291. if (ssl_check_clienthello_tlsext_late(s) <= 0)
  1292. {
  1293. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
  1294. goto err;
  1295. }
  1296. }
  1297. if (ret < 0) ret=-ret;
  1298. if (0)
  1299. {
  1300. f_err:
  1301. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1302. }
  1303. err:
  1304. if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers);
  1305. return ret;
  1306. }
  1307. int ssl3_send_server_hello(SSL *s)
  1308. {
  1309. unsigned char *buf;
  1310. unsigned char *p,*d;
  1311. int i,sl;
  1312. unsigned long l;
  1313. if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
  1314. {
  1315. /* We only accept ChannelIDs on connections with ECDHE in order
  1316. * to avoid a known attack while we fix ChannelID itself. */
  1317. if (s->s3 &&
  1318. s->s3->tlsext_channel_id_valid &&
  1319. (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kEECDH) == 0)
  1320. s->s3->tlsext_channel_id_valid = 0;
  1321. /* If this is a resumption and the original handshake didn't
  1322. * support ChannelID then we didn't record the original
  1323. * handshake hashes in the session and so cannot resume with
  1324. * ChannelIDs. */
  1325. if (s->hit &&
  1326. s->s3->tlsext_channel_id_new &&
  1327. s->session->original_handshake_hash_len == 0)
  1328. s->s3->tlsext_channel_id_valid = 0;
  1329. if (s->mode & SSL_MODE_RELEASE_BUFFERS)
  1330. {
  1331. /* Free s->session->ciphers in order to release memory. This
  1332. * breaks SSL_get_shared_ciphers(), but many servers will
  1333. * prefer the memory savings.
  1334. *
  1335. * It also breaks REUSE_CIPHER_BUG, which is disabled
  1336. * in our build. */
  1337. sk_SSL_CIPHER_free(s->session->ciphers);
  1338. s->session->ciphers = NULL;
  1339. }
  1340. buf=(unsigned char *)s->init_buf->data;
  1341. #ifdef OPENSSL_NO_TLSEXT
  1342. p=s->s3->server_random;
  1343. if (ssl_fill_hello_random(s, 1, p, SSL3_RANDOM_SIZE) <= 0)
  1344. return -1;
  1345. #endif
  1346. /* Do the message type and length last */
  1347. d=p= ssl_handshake_start(s);
  1348. *(p++)=s->version>>8;
  1349. *(p++)=s->version&0xff;
  1350. /* Random stuff */
  1351. memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
  1352. p+=SSL3_RANDOM_SIZE;
  1353. /* There are several cases for the session ID to send
  1354. * back in the server hello:
  1355. * - For session reuse from the session cache,
  1356. * we send back the old session ID.
  1357. * - If stateless session reuse (using a session ticket)
  1358. * is successful, we send back the client's "session ID"
  1359. * (which doesn't actually identify the session).
  1360. * - If it is a new session, we send back the new
  1361. * session ID.
  1362. * - However, if we want the new session to be single-use,
  1363. * we send back a 0-length session ID.
  1364. * s->hit is non-zero in either case of session reuse,
  1365. * so the following won't overwrite an ID that we're supposed
  1366. * to send back.
  1367. */
  1368. if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
  1369. && !s->hit)
  1370. s->session->session_id_length=0;
  1371. sl=s->session->session_id_length;
  1372. if (sl > (int)sizeof(s->session->session_id))
  1373. {
  1374. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR);
  1375. return -1;
  1376. }
  1377. *(p++)=sl;
  1378. memcpy(p,s->session->session_id,sl);
  1379. p+=sl;
  1380. /* put the cipher */
  1381. i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
  1382. p+=i;
  1383. /* put the compression method */
  1384. *(p++)=0;
  1385. #ifndef OPENSSL_NO_TLSEXT
  1386. if (ssl_prepare_serverhello_tlsext(s) <= 0)
  1387. {
  1388. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, SSL_R_SERVERHELLO_TLSEXT);
  1389. return -1;
  1390. }
  1391. if ((p = ssl_add_serverhello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
  1392. {
  1393. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR);
  1394. return -1;
  1395. }
  1396. #endif
  1397. /* do the header */
  1398. l=(p-d);
  1399. ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l);
  1400. s->state=SSL3_ST_SW_SRVR_HELLO_B;
  1401. }
  1402. /* SSL3_ST_SW_SRVR_HELLO_B */
  1403. return ssl_do_write(s);
  1404. }
  1405. int ssl3_send_server_done(SSL *s)
  1406. {
  1407. if (s->state == SSL3_ST_SW_SRVR_DONE_A)
  1408. {
  1409. ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0);
  1410. s->state = SSL3_ST_SW_SRVR_DONE_B;
  1411. }
  1412. /* SSL3_ST_SW_SRVR_DONE_B */
  1413. return ssl_do_write(s);
  1414. }
  1415. int ssl3_send_server_key_exchange(SSL *s)
  1416. {
  1417. #ifndef OPENSSL_NO_RSA
  1418. unsigned char *q;
  1419. int j,num;
  1420. RSA *rsa;
  1421. unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
  1422. unsigned int u;
  1423. #endif
  1424. #ifndef OPENSSL_NO_DH
  1425. DH *dh=NULL,*dhp;
  1426. #endif
  1427. #ifndef OPENSSL_NO_ECDH
  1428. EC_KEY *ecdh=NULL, *ecdhp;
  1429. unsigned char *encodedPoint = NULL;
  1430. int encodedlen = 0;
  1431. int curve_id = 0;
  1432. BN_CTX *bn_ctx = NULL;
  1433. #endif
  1434. #ifndef OPENSSL_NO_PSK
  1435. const char* psk_identity_hint;
  1436. size_t psk_identity_hint_len;
  1437. #endif
  1438. EVP_PKEY *pkey;
  1439. const EVP_MD *md = NULL;
  1440. unsigned char *p,*d;
  1441. int al,i;
  1442. unsigned long alg_k;
  1443. unsigned long alg_a;
  1444. int n;
  1445. CERT *cert;
  1446. BIGNUM *r[4];
  1447. int nr[4],kn;
  1448. BUF_MEM *buf;
  1449. EVP_MD_CTX md_ctx;
  1450. EVP_MD_CTX_init(&md_ctx);
  1451. if (s->state == SSL3_ST_SW_KEY_EXCH_A)
  1452. {
  1453. alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
  1454. alg_a=s->s3->tmp.new_cipher->algorithm_auth;
  1455. cert=s->cert;
  1456. buf=s->init_buf;
  1457. r[0]=r[1]=r[2]=r[3]=NULL;
  1458. n=0;
  1459. #ifndef OPENSSL_NO_PSK
  1460. if (alg_a & SSL_aPSK)
  1461. {
  1462. /* size for PSK identity hint */
  1463. psk_identity_hint = s->session->psk_identity_hint;
  1464. if (psk_identity_hint)
  1465. psk_identity_hint_len = strlen(psk_identity_hint);
  1466. else
  1467. psk_identity_hint_len = 0;
  1468. n+=2+psk_identity_hint_len;
  1469. }
  1470. #endif /* !OPENSSL_NO_PSK */
  1471. #ifndef OPENSSL_NO_RSA
  1472. if (alg_k & SSL_kRSA)
  1473. {
  1474. rsa=cert->rsa_tmp;
  1475. if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
  1476. {
  1477. rsa=s->cert->rsa_tmp_cb(s,
  1478. SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  1479. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  1480. if(rsa == NULL)
  1481. {
  1482. al=SSL_AD_HANDSHAKE_FAILURE;
  1483. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
  1484. goto f_err;
  1485. }
  1486. RSA_up_ref(rsa);
  1487. cert->rsa_tmp=rsa;
  1488. }
  1489. if (rsa == NULL)
  1490. {
  1491. al=SSL_AD_HANDSHAKE_FAILURE;
  1492. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_MISSING_TMP_RSA_KEY);
  1493. goto f_err;
  1494. }
  1495. r[0]=rsa->n;
  1496. r[1]=rsa->e;
  1497. s->s3->tmp.use_rsa_tmp=1;
  1498. }
  1499. #endif
  1500. #ifndef OPENSSL_NO_DH
  1501. else if (alg_k & SSL_kEDH)
  1502. {
  1503. dhp=cert->dh_tmp;
  1504. if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
  1505. dhp=s->cert->dh_tmp_cb(s,
  1506. SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  1507. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  1508. if (dhp == NULL)
  1509. {
  1510. al=SSL_AD_HANDSHAKE_FAILURE;
  1511. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_MISSING_TMP_DH_KEY);
  1512. goto f_err;
  1513. }
  1514. if (s->s3->tmp.dh != NULL)
  1515. {
  1516. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_INTERNAL_ERROR);
  1517. goto err;
  1518. }
  1519. if ((dh=DHparams_dup(dhp)) == NULL)
  1520. {
  1521. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
  1522. goto err;
  1523. }
  1524. s->s3->tmp.dh=dh;
  1525. if ((dhp->pub_key == NULL ||
  1526. dhp->priv_key == NULL ||
  1527. (s->options & SSL_OP_SINGLE_DH_USE)))
  1528. {
  1529. if(!DH_generate_key(dh))
  1530. {
  1531. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
  1532. goto err;
  1533. }
  1534. }
  1535. else
  1536. {
  1537. dh->pub_key=BN_dup(dhp->pub_key);
  1538. dh->priv_key=BN_dup(dhp->priv_key);
  1539. if ((dh->pub_key == NULL) ||
  1540. (dh->priv_key == NULL))
  1541. {
  1542. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
  1543. goto err;
  1544. }
  1545. }
  1546. r[0]=dh->p;
  1547. r[1]=dh->g;
  1548. r[2]=dh->pub_key;
  1549. }
  1550. #endif
  1551. #ifndef OPENSSL_NO_ECDH
  1552. else if (alg_k & SSL_kEECDH)
  1553. {
  1554. const EC_GROUP *group;
  1555. ecdhp=cert->ecdh_tmp;
  1556. if (s->cert->ecdh_tmp_auto)
  1557. {
  1558. /* Get NID of appropriate shared curve */
  1559. int nid = tls1_shared_curve(s, -2);
  1560. if (nid != NID_undef)
  1561. ecdhp = EC_KEY_new_by_curve_name(nid);
  1562. }
  1563. else if ((ecdhp == NULL) && s->cert->ecdh_tmp_cb)
  1564. {
  1565. ecdhp=s->cert->ecdh_tmp_cb(s,
  1566. SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  1567. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  1568. }
  1569. if (ecdhp == NULL)
  1570. {
  1571. al=SSL_AD_HANDSHAKE_FAILURE;
  1572. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_MISSING_TMP_ECDH_KEY);
  1573. goto f_err;
  1574. }
  1575. if (s->s3->tmp.ecdh != NULL)
  1576. {
  1577. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_INTERNAL_ERROR);
  1578. goto err;
  1579. }
  1580. /* Duplicate the ECDH structure. */
  1581. if (ecdhp == NULL)
  1582. {
  1583. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
  1584. goto err;
  1585. }
  1586. if (s->cert->ecdh_tmp_auto)
  1587. ecdh = ecdhp;
  1588. else if ((ecdh = EC_KEY_dup(ecdhp)) == NULL)
  1589. {
  1590. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
  1591. goto err;
  1592. }
  1593. s->s3->tmp.ecdh=ecdh;
  1594. if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
  1595. (EC_KEY_get0_private_key(ecdh) == NULL) ||
  1596. (s->options & SSL_OP_SINGLE_ECDH_USE))
  1597. {
  1598. if(!EC_KEY_generate_key(ecdh))
  1599. {
  1600. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
  1601. goto err;
  1602. }
  1603. }
  1604. if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
  1605. (EC_KEY_get0_public_key(ecdh) == NULL) ||
  1606. (EC_KEY_get0_private_key(ecdh) == NULL))
  1607. {
  1608. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
  1609. goto err;
  1610. }
  1611. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
  1612. (EC_GROUP_get_degree(group) > 163))
  1613. {
  1614. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
  1615. goto err;
  1616. }
  1617. /* XXX: For now, we only support ephemeral ECDH
  1618. * keys over named (not generic) curves. For
  1619. * supported named curves, curve_id is non-zero.
  1620. */
  1621. if ((curve_id =
  1622. tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
  1623. == 0)
  1624. {
  1625. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
  1626. goto err;
  1627. }
  1628. /* Encode the public key.
  1629. * First check the size of encoding and
  1630. * allocate memory accordingly.
  1631. */
  1632. encodedlen = EC_POINT_point2oct(group,
  1633. EC_KEY_get0_public_key(ecdh),
  1634. POINT_CONVERSION_UNCOMPRESSED,
  1635. NULL, 0, NULL);
  1636. encodedPoint = (unsigned char *)
  1637. OPENSSL_malloc(encodedlen*sizeof(unsigned char));
  1638. bn_ctx = BN_CTX_new();
  1639. if ((encodedPoint == NULL) || (bn_ctx == NULL))
  1640. {
  1641. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_MALLOC_FAILURE);
  1642. goto err;
  1643. }
  1644. encodedlen = EC_POINT_point2oct(group,
  1645. EC_KEY_get0_public_key(ecdh),
  1646. POINT_CONVERSION_UNCOMPRESSED,
  1647. encodedPoint, encodedlen, bn_ctx);
  1648. if (encodedlen == 0)
  1649. {
  1650. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
  1651. goto err;
  1652. }
  1653. BN_CTX_free(bn_ctx); bn_ctx=NULL;
  1654. /* XXX: For now, we only support named (not
  1655. * generic) curves in ECDH ephemeral key exchanges.
  1656. * In this situation, we need four additional bytes
  1657. * to encode the entire ServerECDHParams
  1658. * structure.
  1659. */
  1660. n += 4 + encodedlen;
  1661. /* We'll generate the serverKeyExchange message
  1662. * explicitly so we can set these to NULLs
  1663. */
  1664. r[0]=NULL;
  1665. r[1]=NULL;
  1666. r[2]=NULL;
  1667. r[3]=NULL;
  1668. }
  1669. #endif /* !OPENSSL_NO_ECDH */
  1670. else if (!(alg_k & SSL_kPSK))
  1671. {
  1672. al=SSL_AD_HANDSHAKE_FAILURE;
  1673. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  1674. goto f_err;
  1675. }
  1676. for (i=0; i < 4 && r[i] != NULL; i++)
  1677. {
  1678. nr[i]=BN_num_bytes(r[i]);
  1679. n+=2+nr[i];
  1680. }
  1681. if (!(alg_a & SSL_aNULL)
  1682. /* Among PSK ciphersuites only RSA uses a certificate */
  1683. && !((alg_a & SSL_aPSK) && !(alg_k & SSL_kRSA)))
  1684. {
  1685. if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher,&md))
  1686. == NULL)
  1687. {
  1688. al=SSL_AD_DECODE_ERROR;
  1689. goto f_err;
  1690. }
  1691. kn=EVP_PKEY_size(pkey);
  1692. }
  1693. else
  1694. {
  1695. pkey=NULL;
  1696. kn=0;
  1697. }
  1698. if (!BUF_MEM_grow_clean(buf,n+SSL_HM_HEADER_LENGTH(s)+kn))
  1699. {
  1700. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_BUF);
  1701. goto err;
  1702. }
  1703. d = p = ssl_handshake_start(s);
  1704. for (i=0; i < 4 && r[i] != NULL; i++)
  1705. {
  1706. s2n(nr[i],p);
  1707. BN_bn2bin(r[i],p);
  1708. p+=nr[i];
  1709. }
  1710. /* Note: ECDHE PSK ciphersuites use SSL_kEECDH and SSL_aPSK.
  1711. * When one of them is used, the server key exchange record needs to have both
  1712. * the psk_identity_hint and the ServerECDHParams. */
  1713. #ifndef OPENSSL_NO_PSK
  1714. if (alg_a & SSL_aPSK)
  1715. {
  1716. /* copy PSK identity hint (if provided) */
  1717. s2n(psk_identity_hint_len, p);
  1718. if (psk_identity_hint_len > 0)
  1719. {
  1720. memcpy(p, psk_identity_hint, psk_identity_hint_len);
  1721. p+=psk_identity_hint_len;
  1722. }
  1723. }
  1724. #endif /* OPENSSL_NO_PSK */
  1725. #ifndef OPENSSL_NO_ECDH
  1726. if (alg_k & SSL_kEECDH)
  1727. {
  1728. /* XXX: For now, we only support named (not generic) curves.
  1729. * In this situation, the serverKeyExchange message has:
  1730. * [1 byte CurveType], [2 byte CurveName]
  1731. * [1 byte length of encoded point], followed by
  1732. * the actual encoded point itself
  1733. */
  1734. *p = NAMED_CURVE_TYPE;
  1735. p += 1;
  1736. *p = 0;
  1737. p += 1;
  1738. *p = curve_id;
  1739. p += 1;
  1740. *p = encodedlen;
  1741. p += 1;
  1742. memcpy((unsigned char*)p,
  1743. (unsigned char *)encodedPoint,
  1744. encodedlen);
  1745. OPENSSL_free(encodedPoint);
  1746. encodedPoint = NULL;
  1747. p += encodedlen;
  1748. }
  1749. #endif /* OPENSSL_NO_ECDH */
  1750. /* not anonymous */
  1751. if (pkey != NULL)
  1752. {
  1753. /* n is the length of the params, they start at &(d[4])
  1754. * and p points to the space at the end. */
  1755. #ifndef OPENSSL_NO_RSA
  1756. if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s))
  1757. {
  1758. q=md_buf;
  1759. j=0;
  1760. for (num=2; num > 0; num--)
  1761. {
  1762. EVP_DigestInit_ex(&md_ctx,(num == 2)
  1763. ?s->ctx->md5:s->ctx->sha1, NULL);
  1764. EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1765. EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1766. EVP_DigestUpdate(&md_ctx,d,n);
  1767. EVP_DigestFinal_ex(&md_ctx,q,
  1768. (unsigned int *)&i);
  1769. q+=i;
  1770. j+=i;
  1771. }
  1772. if (RSA_sign(NID_md5_sha1, md_buf, j,
  1773. &(p[2]), &u, pkey->pkey.rsa) <= 0)
  1774. {
  1775. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_RSA);
  1776. goto err;
  1777. }
  1778. s2n(u,p);
  1779. n+=u+2;
  1780. }
  1781. else
  1782. #endif /* OPENSSL_NO_RSA */
  1783. if (md)
  1784. {
  1785. /* send signature algorithm */
  1786. if (SSL_USE_SIGALGS(s))
  1787. {
  1788. if (!tls12_get_sigandhash(p, pkey, md))
  1789. {
  1790. /* Should never happen */
  1791. al=SSL_AD_INTERNAL_ERROR;
  1792. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_INTERNAL_ERROR);
  1793. goto f_err;
  1794. }
  1795. p+=2;
  1796. }
  1797. #ifdef SSL_DEBUG
  1798. fprintf(stderr, "Using hash %s\n",
  1799. EVP_MD_name(md));
  1800. #endif
  1801. EVP_SignInit_ex(&md_ctx, md, NULL);
  1802. EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1803. EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1804. EVP_SignUpdate(&md_ctx,d,n);
  1805. if (!EVP_SignFinal(&md_ctx,&(p[2]),
  1806. (unsigned int *)&i,pkey))
  1807. {
  1808. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_EVP);
  1809. goto err;
  1810. }
  1811. s2n(i,p);
  1812. n+=i+2;
  1813. if (SSL_USE_SIGALGS(s))
  1814. n+= 2;
  1815. }
  1816. else
  1817. {
  1818. /* Is this error check actually needed? */
  1819. al=SSL_AD_HANDSHAKE_FAILURE;
  1820. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_UNKNOWN_PKEY_TYPE);
  1821. goto f_err;
  1822. }
  1823. }
  1824. ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n);
  1825. }
  1826. s->state = SSL3_ST_SW_KEY_EXCH_B;
  1827. EVP_MD_CTX_cleanup(&md_ctx);
  1828. return ssl_do_write(s);
  1829. f_err:
  1830. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1831. err:
  1832. #ifndef OPENSSL_NO_ECDH
  1833. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  1834. BN_CTX_free(bn_ctx);
  1835. #endif
  1836. EVP_MD_CTX_cleanup(&md_ctx);
  1837. return(-1);
  1838. }
  1839. int ssl3_send_certificate_request(SSL *s)
  1840. {
  1841. unsigned char *p,*d;
  1842. int i,j,nl,off,n;
  1843. STACK_OF(X509_NAME) *sk=NULL;
  1844. X509_NAME *name;
  1845. BUF_MEM *buf;
  1846. if (s->state == SSL3_ST_SW_CERT_REQ_A)
  1847. {
  1848. buf=s->init_buf;
  1849. d=p=ssl_handshake_start(s);
  1850. /* get the list of acceptable cert types */
  1851. p++;
  1852. n=ssl3_get_req_cert_type(s,p);
  1853. d[0]=n;
  1854. p+=n;
  1855. n++;
  1856. if (SSL_USE_SIGALGS(s))
  1857. {
  1858. const unsigned char *psigs;
  1859. nl = tls12_get_psigalgs(s, &psigs);
  1860. s2n(nl, p);
  1861. memcpy(p, psigs, nl);
  1862. p += nl;
  1863. n += nl + 2;
  1864. }
  1865. off=n;
  1866. p+=2;
  1867. n+=2;
  1868. sk=SSL_get_client_CA_list(s);
  1869. nl=0;
  1870. if (sk != NULL)
  1871. {
  1872. for (i=0; i<sk_X509_NAME_num(sk); i++)
  1873. {
  1874. name=sk_X509_NAME_value(sk,i);
  1875. j=i2d_X509_NAME(name,NULL);
  1876. if (!BUF_MEM_grow_clean(buf,SSL_HM_HEADER_LENGTH(s)+n+j+2))
  1877. {
  1878. OPENSSL_PUT_ERROR(SSL, ssl3_send_certificate_request, ERR_R_BUF_LIB);
  1879. goto err;
  1880. }
  1881. p = ssl_handshake_start(s) + n;
  1882. if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
  1883. {
  1884. s2n(j,p);
  1885. i2d_X509_NAME(name,&p);
  1886. n+=2+j;
  1887. nl+=2+j;
  1888. }
  1889. else
  1890. {
  1891. d=p;
  1892. i2d_X509_NAME(name,&p);
  1893. j-=2; s2n(j,d); j+=2;
  1894. n+=j;
  1895. nl+=j;
  1896. }
  1897. }
  1898. }
  1899. /* else no CA names */
  1900. p = ssl_handshake_start(s) + off;
  1901. s2n(nl,p);
  1902. ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n);
  1903. #ifdef NETSCAPE_HANG_BUG
  1904. if (!SSL_IS_DTLS(s))
  1905. {
  1906. p=(unsigned char *)s->init_buf->data + s->init_num;
  1907. /* do the header */
  1908. *(p++)=SSL3_MT_SERVER_DONE;
  1909. *(p++)=0;
  1910. *(p++)=0;
  1911. *(p++)=0;
  1912. s->init_num += 4;
  1913. }
  1914. #endif
  1915. s->state = SSL3_ST_SW_CERT_REQ_B;
  1916. }
  1917. /* SSL3_ST_SW_CERT_REQ_B */
  1918. return ssl_do_write(s);
  1919. err:
  1920. return(-1);
  1921. }
  1922. int ssl3_get_client_key_exchange(SSL *s)
  1923. {
  1924. int i,al,ok;
  1925. long n;
  1926. unsigned long alg_k;
  1927. unsigned long alg_a;
  1928. unsigned char *p;
  1929. #ifndef OPENSSL_NO_RSA
  1930. RSA *rsa=NULL;
  1931. EVP_PKEY *pkey=NULL;
  1932. #endif
  1933. #ifndef OPENSSL_NO_DH
  1934. BIGNUM *pub=NULL;
  1935. DH *dh_srvr, *dh_clnt = NULL;
  1936. #endif
  1937. #ifndef OPENSSL_NO_ECDH
  1938. EC_KEY *srvr_ecdh = NULL;
  1939. EVP_PKEY *clnt_pub_pkey = NULL;
  1940. EC_POINT *clnt_ecpoint = NULL;
  1941. BN_CTX *bn_ctx = NULL;
  1942. #ifndef OPENSSL_NO_PSK
  1943. unsigned int psk_len = 0;
  1944. unsigned char psk[PSK_MAX_PSK_LEN];
  1945. #endif /* OPENSSL_NO_PSK */
  1946. #endif
  1947. n=s->method->ssl_get_message(s,
  1948. SSL3_ST_SR_KEY_EXCH_A,
  1949. SSL3_ST_SR_KEY_EXCH_B,
  1950. SSL3_MT_CLIENT_KEY_EXCHANGE,
  1951. 2048, /* ??? */
  1952. &ok);
  1953. if (!ok) return((int)n);
  1954. p=(unsigned char *)s->init_msg;
  1955. alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
  1956. alg_a=s->s3->tmp.new_cipher->algorithm_auth;
  1957. #ifndef OPENSSL_NO_PSK
  1958. if (alg_a & SSL_aPSK)
  1959. {
  1960. unsigned char *t = NULL;
  1961. unsigned char pre_ms[PSK_MAX_PSK_LEN*2+4];
  1962. unsigned int pre_ms_len = 0;
  1963. int psk_err = 1;
  1964. char tmp_id[PSK_MAX_IDENTITY_LEN+1];
  1965. al=SSL_AD_HANDSHAKE_FAILURE;
  1966. n2s(p, i);
  1967. if (n != i+2 && !(alg_k & SSL_kEECDH))
  1968. {
  1969. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_LENGTH_MISMATCH);
  1970. goto psk_err;
  1971. }
  1972. if (i > PSK_MAX_IDENTITY_LEN)
  1973. {
  1974. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DATA_LENGTH_TOO_LONG);
  1975. goto psk_err;
  1976. }
  1977. if (s->psk_server_callback == NULL)
  1978. {
  1979. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_PSK_NO_SERVER_CB);
  1980. goto psk_err;
  1981. }
  1982. /* Create guaranteed NUL-terminated identity
  1983. * string for the callback */
  1984. memcpy(tmp_id, p, i);
  1985. memset(tmp_id+i, 0, PSK_MAX_IDENTITY_LEN+1-i);
  1986. psk_len = s->psk_server_callback(s, tmp_id, psk, sizeof(psk));
  1987. if (psk_len > PSK_MAX_PSK_LEN)
  1988. {
  1989. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_INTERNAL_ERROR);
  1990. goto psk_err;
  1991. }
  1992. else if (psk_len == 0)
  1993. {
  1994. /* PSK related to the given identity not found */
  1995. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_PSK_IDENTITY_NOT_FOUND);
  1996. al=SSL_AD_UNKNOWN_PSK_IDENTITY;
  1997. goto psk_err;
  1998. }
  1999. if (!(alg_k & SSL_kEECDH))
  2000. {
  2001. /* Create the shared secret now if we're not using ECDHE-PSK.*/
  2002. pre_ms_len=2+psk_len+2+psk_len;
  2003. t = pre_ms;
  2004. s2n(psk_len, t);
  2005. memset(t, 0, psk_len);
  2006. t+=psk_len;
  2007. s2n(psk_len, t);
  2008. memcpy(t, psk, psk_len);
  2009. s->session->master_key_length=
  2010. s->method->ssl3_enc->generate_master_secret(s,
  2011. s->session->master_key, pre_ms, pre_ms_len);
  2012. }
  2013. if (s->session->psk_identity != NULL)
  2014. OPENSSL_free(s->session->psk_identity);
  2015. s->session->psk_identity = BUF_strdup(tmp_id);
  2016. OPENSSL_cleanse(tmp_id, PSK_MAX_IDENTITY_LEN+1);
  2017. if (s->session->psk_identity == NULL)
  2018. {
  2019. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
  2020. goto psk_err;
  2021. }
  2022. p += i;
  2023. n -= (i + 2);
  2024. psk_err = 0;
  2025. psk_err:
  2026. OPENSSL_cleanse(pre_ms, sizeof(pre_ms));
  2027. if (psk_err != 0)
  2028. goto f_err;
  2029. }
  2030. #endif /* OPENSSL_NO_PSK */
  2031. if (0) {}
  2032. #ifndef OPENSSL_NO_RSA
  2033. else if (alg_k & SSL_kRSA)
  2034. {
  2035. unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
  2036. int decrypt_len, decrypt_good_mask;
  2037. unsigned char version_good;
  2038. size_t j;
  2039. /* FIX THIS UP EAY EAY EAY EAY */
  2040. if (s->s3->tmp.use_rsa_tmp)
  2041. {
  2042. if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
  2043. rsa=s->cert->rsa_tmp;
  2044. /* Don't do a callback because rsa_tmp should
  2045. * be sent already */
  2046. if (rsa == NULL)
  2047. {
  2048. al=SSL_AD_HANDSHAKE_FAILURE;
  2049. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_MISSING_TMP_RSA_PKEY);
  2050. goto f_err;
  2051. }
  2052. }
  2053. else
  2054. {
  2055. pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
  2056. if ( (pkey == NULL) ||
  2057. (pkey->type != EVP_PKEY_RSA) ||
  2058. (pkey->pkey.rsa == NULL))
  2059. {
  2060. al=SSL_AD_HANDSHAKE_FAILURE;
  2061. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_MISSING_RSA_CERTIFICATE);
  2062. goto f_err;
  2063. }
  2064. rsa=pkey->pkey.rsa;
  2065. }
  2066. /* TLS and [incidentally] DTLS{0xFEFF} */
  2067. if (s->version > SSL3_VERSION && s->version != DTLS1_BAD_VER)
  2068. {
  2069. n2s(p,i);
  2070. if (n != i+2)
  2071. {
  2072. if (!(s->options & SSL_OP_TLS_D5_BUG))
  2073. {
  2074. al = SSL_AD_DECODE_ERROR;
  2075. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
  2076. goto f_err;
  2077. }
  2078. else
  2079. p-=2;
  2080. }
  2081. else
  2082. n=i;
  2083. }
  2084. /* Reject overly short RSA ciphertext because we want to be
  2085. * sure that the buffer size makes it safe to iterate over the
  2086. * entire size of a premaster secret
  2087. * (SSL_MAX_MASTER_KEY_LENGTH). The actual expected size is
  2088. * larger due to RSA padding, but the bound is sufficient to be
  2089. * safe. */
  2090. if (n < SSL_MAX_MASTER_KEY_LENGTH)
  2091. {
  2092. al = SSL_AD_DECRYPT_ERROR;
  2093. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECRYPTION_FAILED);
  2094. goto f_err;
  2095. }
  2096. /* We must not leak whether a decryption failure occurs because
  2097. * of Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see
  2098. * RFC 2246, section 7.4.7.1). The code follows that advice of
  2099. * the TLS RFC and generates a random premaster secret for the
  2100. * case that the decrypt fails. See
  2101. * https://tools.ietf.org/html/rfc5246#section-7.4.7.1 */
  2102. if (RAND_pseudo_bytes(rand_premaster_secret,
  2103. sizeof(rand_premaster_secret)) <= 0)
  2104. goto err;
  2105. decrypt_len = RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
  2106. ERR_clear_error();
  2107. /* decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH.
  2108. * decrypt_good_mask will be zero if so and non-zero otherwise. */
  2109. decrypt_good_mask = decrypt_len ^ SSL_MAX_MASTER_KEY_LENGTH;
  2110. /* If the version in the decrypted pre-master secret is correct
  2111. * then version_good will be zero. The Klima-Pokorny-Rosa
  2112. * extension of Bleichenbacher's attack
  2113. * (http://eprint.iacr.org/2003/052/) exploits the version
  2114. * number check as a "bad version oracle". Thus version checks
  2115. * are done in constant time and are treated like any other
  2116. * decryption error. */
  2117. version_good = p[0] ^ (s->client_version>>8);
  2118. version_good |= p[1] ^ (s->client_version&0xff);
  2119. /* The premaster secret must contain the same version number as
  2120. * the ClientHello to detect version rollback attacks
  2121. * (strangely, the protocol does not offer such protection for
  2122. * DH ciphersuites). However, buggy clients exist that send the
  2123. * negotiated protocol version instead if the server does not
  2124. * support the requested protocol version. If
  2125. * SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
  2126. if (s->options & SSL_OP_TLS_ROLLBACK_BUG)
  2127. {
  2128. unsigned char workaround_mask = version_good;
  2129. unsigned char workaround;
  2130. /* workaround_mask will be 0xff if version_good is
  2131. * non-zero (i.e. the version match failed). Otherwise
  2132. * it'll be 0x00. */
  2133. workaround_mask |= workaround_mask >> 4;
  2134. workaround_mask |= workaround_mask >> 2;
  2135. workaround_mask |= workaround_mask >> 1;
  2136. workaround_mask = ~((workaround_mask & 1) - 1);
  2137. workaround = p[0] ^ (s->version>>8);
  2138. workaround |= p[1] ^ (s->version&0xff);
  2139. /* If workaround_mask is 0xff (i.e. there was a version
  2140. * mismatch) then we copy the value of workaround over
  2141. * version_good. */
  2142. version_good = (workaround & workaround_mask) |
  2143. (version_good & ~workaround_mask);
  2144. }
  2145. /* If any bits in version_good are set then they'll poision
  2146. * decrypt_good_mask and cause rand_premaster_secret to be
  2147. * used. */
  2148. decrypt_good_mask |= version_good;
  2149. /* decrypt_good_mask will be zero iff decrypt_len ==
  2150. * SSL_MAX_MASTER_KEY_LENGTH and the version check passed. We
  2151. * fold the bottom 32 bits of it with an OR so that the LSB
  2152. * will be zero iff everything is good. This assumes that we'll
  2153. * never decrypt a value > 2**31 bytes, which seems safe. */
  2154. decrypt_good_mask |= decrypt_good_mask >> 16;
  2155. decrypt_good_mask |= decrypt_good_mask >> 8;
  2156. decrypt_good_mask |= decrypt_good_mask >> 4;
  2157. decrypt_good_mask |= decrypt_good_mask >> 2;
  2158. decrypt_good_mask |= decrypt_good_mask >> 1;
  2159. /* Now select only the LSB and subtract one. If decrypt_len ==
  2160. * SSL_MAX_MASTER_KEY_LENGTH and the version check passed then
  2161. * decrypt_good_mask will be all ones. Otherwise it'll be all
  2162. * zeros. */
  2163. decrypt_good_mask &= 1;
  2164. decrypt_good_mask--;
  2165. /* Now copy rand_premaster_secret over p using
  2166. * decrypt_good_mask. */
  2167. for (j = 0; j < sizeof(rand_premaster_secret); j++)
  2168. {
  2169. p[j] = (p[j] & decrypt_good_mask) |
  2170. (rand_premaster_secret[j] & ~decrypt_good_mask);
  2171. }
  2172. s->session->master_key_length=
  2173. s->method->ssl3_enc->generate_master_secret(s,
  2174. s->session->master_key,
  2175. p,sizeof(rand_premaster_secret));
  2176. OPENSSL_cleanse(p,sizeof(rand_premaster_secret));
  2177. }
  2178. #endif
  2179. #ifndef OPENSSL_NO_DH
  2180. else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  2181. {
  2182. int idx = -1;
  2183. EVP_PKEY *skey = NULL;
  2184. if (n)
  2185. n2s(p,i);
  2186. else
  2187. i = 0;
  2188. if (n && n != i+2)
  2189. {
  2190. if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
  2191. {
  2192. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
  2193. goto err;
  2194. }
  2195. else
  2196. {
  2197. p-=2;
  2198. i=(int)n;
  2199. }
  2200. }
  2201. if (alg_k & SSL_kDHr)
  2202. idx = SSL_PKEY_DH_RSA;
  2203. else if (alg_k & SSL_kDHd)
  2204. idx = SSL_PKEY_DH_DSA;
  2205. if (idx >= 0)
  2206. {
  2207. skey = s->cert->pkeys[idx].privatekey;
  2208. if ((skey == NULL) ||
  2209. (skey->type != EVP_PKEY_DH) ||
  2210. (skey->pkey.dh == NULL))
  2211. {
  2212. al=SSL_AD_HANDSHAKE_FAILURE;
  2213. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_MISSING_RSA_CERTIFICATE);
  2214. goto f_err;
  2215. }
  2216. dh_srvr = skey->pkey.dh;
  2217. }
  2218. else if (s->s3->tmp.dh == NULL)
  2219. {
  2220. al=SSL_AD_HANDSHAKE_FAILURE;
  2221. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_MISSING_TMP_DH_KEY);
  2222. goto f_err;
  2223. }
  2224. else
  2225. dh_srvr=s->s3->tmp.dh;
  2226. if (n == 0L)
  2227. {
  2228. /* Get pubkey from cert */
  2229. EVP_PKEY *clkey=X509_get_pubkey(s->session->peer);
  2230. if (clkey)
  2231. {
  2232. if (EVP_PKEY_cmp_parameters(clkey, skey) == 1)
  2233. dh_clnt = EVP_PKEY_get1_DH(clkey);
  2234. }
  2235. if (dh_clnt == NULL)
  2236. {
  2237. al=SSL_AD_HANDSHAKE_FAILURE;
  2238. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_MISSING_TMP_DH_KEY);
  2239. goto f_err;
  2240. }
  2241. EVP_PKEY_free(clkey);
  2242. pub = dh_clnt->pub_key;
  2243. }
  2244. else
  2245. pub=BN_bin2bn(p,i,NULL);
  2246. if (pub == NULL)
  2247. {
  2248. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_BN_LIB);
  2249. goto err;
  2250. }
  2251. i=DH_compute_key(p,pub,dh_srvr);
  2252. if (i <= 0)
  2253. {
  2254. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_DH_LIB);
  2255. BN_clear_free(pub);
  2256. goto err;
  2257. }
  2258. DH_free(s->s3->tmp.dh);
  2259. s->s3->tmp.dh=NULL;
  2260. if (dh_clnt)
  2261. DH_free(dh_clnt);
  2262. else
  2263. BN_clear_free(pub);
  2264. pub=NULL;
  2265. s->session->master_key_length=
  2266. s->method->ssl3_enc->generate_master_secret(s,
  2267. s->session->master_key,p,i);
  2268. OPENSSL_cleanse(p,i);
  2269. if (dh_clnt)
  2270. return 2;
  2271. }
  2272. #endif
  2273. #ifndef OPENSSL_NO_ECDH
  2274. else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
  2275. {
  2276. int ret = 1;
  2277. int field_size = 0;
  2278. const EC_KEY *tkey;
  2279. const EC_GROUP *group;
  2280. const BIGNUM *priv_key;
  2281. #ifndef OPENSSL_NO_PSK
  2282. unsigned char *pre_ms;
  2283. unsigned int pre_ms_len;
  2284. unsigned char *t;
  2285. #endif /* OPENSSL_NO_PSK */
  2286. /* initialize structures for server's ECDH key pair */
  2287. if ((srvr_ecdh = EC_KEY_new()) == NULL)
  2288. {
  2289. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
  2290. goto err;
  2291. }
  2292. /* Let's get server private key and group information */
  2293. if (alg_k & (SSL_kECDHr|SSL_kECDHe))
  2294. {
  2295. /* use the certificate */
  2296. tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;
  2297. }
  2298. else
  2299. {
  2300. /* use the ephermeral values we saved when
  2301. * generating the ServerKeyExchange msg.
  2302. */
  2303. tkey = s->s3->tmp.ecdh;
  2304. }
  2305. group = EC_KEY_get0_group(tkey);
  2306. priv_key = EC_KEY_get0_private_key(tkey);
  2307. if (!EC_KEY_set_group(srvr_ecdh, group) ||
  2308. !EC_KEY_set_private_key(srvr_ecdh, priv_key))
  2309. {
  2310. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB);
  2311. goto err;
  2312. }
  2313. /* Let's get client's public key */
  2314. if ((clnt_ecpoint = EC_POINT_new(group)) == NULL)
  2315. {
  2316. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
  2317. goto err;
  2318. }
  2319. if (n == 0L)
  2320. {
  2321. /* Client Publickey was in Client Certificate */
  2322. if (alg_k & SSL_kEECDH)
  2323. {
  2324. al=SSL_AD_HANDSHAKE_FAILURE;
  2325. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_MISSING_TMP_ECDH_KEY);
  2326. goto f_err;
  2327. }
  2328. if (((clnt_pub_pkey=X509_get_pubkey(s->session->peer))
  2329. == NULL) ||
  2330. (clnt_pub_pkey->type != EVP_PKEY_EC))
  2331. {
  2332. /* XXX: For now, we do not support client
  2333. * authentication using ECDH certificates
  2334. * so this branch (n == 0L) of the code is
  2335. * never executed. When that support is
  2336. * added, we ought to ensure the key
  2337. * received in the certificate is
  2338. * authorized for key agreement.
  2339. * ECDH_compute_key implicitly checks that
  2340. * the two ECDH shares are for the same
  2341. * group.
  2342. */
  2343. al=SSL_AD_HANDSHAKE_FAILURE;
  2344. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
  2345. goto f_err;
  2346. }
  2347. if (EC_POINT_copy(clnt_ecpoint,
  2348. EC_KEY_get0_public_key(clnt_pub_pkey->pkey.ec)) == 0)
  2349. {
  2350. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB);
  2351. goto err;
  2352. }
  2353. ret = 2; /* Skip certificate verify processing */
  2354. }
  2355. else
  2356. {
  2357. /* Get client's public key from encoded point
  2358. * in the ClientKeyExchange message.
  2359. */
  2360. if ((bn_ctx = BN_CTX_new()) == NULL)
  2361. {
  2362. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
  2363. goto err;
  2364. }
  2365. /* Get encoded point length */
  2366. i = *p;
  2367. p += 1;
  2368. if (n != 1 + i)
  2369. {
  2370. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB);
  2371. goto err;
  2372. }
  2373. if (EC_POINT_oct2point(group,
  2374. clnt_ecpoint, p, i, bn_ctx) == 0)
  2375. {
  2376. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB);
  2377. goto err;
  2378. }
  2379. /* p is pointing to somewhere in the buffer
  2380. * currently, so set it to the start
  2381. */
  2382. p=(unsigned char *)s->init_buf->data;
  2383. }
  2384. /* Compute the shared pre-master secret */
  2385. field_size = EC_GROUP_get_degree(group);
  2386. if (field_size <= 0)
  2387. {
  2388. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB);
  2389. goto err;
  2390. }
  2391. i = ECDH_compute_key(p, (field_size+7)/8, clnt_ecpoint, srvr_ecdh, NULL);
  2392. if (i <= 0)
  2393. {
  2394. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB);
  2395. goto err;
  2396. }
  2397. EVP_PKEY_free(clnt_pub_pkey);
  2398. EC_POINT_free(clnt_ecpoint);
  2399. EC_KEY_free(srvr_ecdh);
  2400. BN_CTX_free(bn_ctx);
  2401. EC_KEY_free(s->s3->tmp.ecdh);
  2402. s->s3->tmp.ecdh = NULL;
  2403. #ifndef OPENSSL_NO_PSK
  2404. /* ECDHE PSK ciphersuites from RFC 5489 */
  2405. if ((alg_a & SSL_aPSK) && psk_len != 0)
  2406. {
  2407. pre_ms_len = 2+psk_len+2+i;
  2408. pre_ms = OPENSSL_malloc(pre_ms_len);
  2409. if (pre_ms == NULL)
  2410. {
  2411. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
  2412. goto err;
  2413. }
  2414. memset(pre_ms, 0, pre_ms_len);
  2415. t = pre_ms;
  2416. s2n(psk_len, t);
  2417. memcpy(t, psk, psk_len);
  2418. t += psk_len;
  2419. s2n(i, t);
  2420. memcpy(t, p, i);
  2421. s->session->master_key_length = s->method->ssl3_enc \
  2422. -> generate_master_secret(s,
  2423. s->session->master_key, pre_ms, pre_ms_len);
  2424. OPENSSL_cleanse(pre_ms, pre_ms_len);
  2425. OPENSSL_free(pre_ms);
  2426. }
  2427. #endif /* OPENSSL_NO_PSK */
  2428. if (!(alg_a & SSL_aPSK))
  2429. {
  2430. /* Compute the master secret */
  2431. s->session->master_key_length = s->method->ssl3_enc \
  2432. -> generate_master_secret(s,
  2433. s->session->master_key, p, i);
  2434. }
  2435. OPENSSL_cleanse(p, i);
  2436. return ret;
  2437. }
  2438. #endif
  2439. else if (alg_k & SSL_kGOST)
  2440. {
  2441. int ret = 0;
  2442. EVP_PKEY_CTX *pkey_ctx;
  2443. EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
  2444. unsigned char premaster_secret[32], *start;
  2445. size_t outlen=32, inlen;
  2446. unsigned long alg_a;
  2447. /* Get our certificate private key*/
  2448. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  2449. if (alg_a & SSL_aGOST94)
  2450. pk = s->cert->pkeys[SSL_PKEY_GOST94].privatekey;
  2451. else if (alg_a & SSL_aGOST01)
  2452. pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
  2453. pkey_ctx = EVP_PKEY_CTX_new(pk,NULL);
  2454. EVP_PKEY_decrypt_init(pkey_ctx);
  2455. /* If client certificate is present and is of the same type, maybe
  2456. * use it for key exchange. Don't mind errors from
  2457. * EVP_PKEY_derive_set_peer, because it is completely valid to use
  2458. * a client certificate for authorization only. */
  2459. client_pub_pkey = X509_get_pubkey(s->session->peer);
  2460. if (client_pub_pkey)
  2461. {
  2462. if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
  2463. ERR_clear_error();
  2464. }
  2465. /* Decrypt session key */
  2466. if ((*p!=( V_ASN1_SEQUENCE| V_ASN1_CONSTRUCTED)))
  2467. {
  2468. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECRYPTION_FAILED);
  2469. goto gerr;
  2470. }
  2471. if (p[1] == 0x81)
  2472. {
  2473. start = p+3;
  2474. inlen = p[2];
  2475. }
  2476. else if (p[1] < 0x80)
  2477. {
  2478. start = p+2;
  2479. inlen = p[1];
  2480. }
  2481. else
  2482. {
  2483. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECRYPTION_FAILED);
  2484. goto gerr;
  2485. }
  2486. if (EVP_PKEY_decrypt(pkey_ctx,premaster_secret,&outlen,start,inlen) <=0)
  2487. {
  2488. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECRYPTION_FAILED);
  2489. goto gerr;
  2490. }
  2491. /* Generate master secret */
  2492. s->session->master_key_length=
  2493. s->method->ssl3_enc->generate_master_secret(s,
  2494. s->session->master_key,premaster_secret,32);
  2495. /* Check if pubkey from client certificate was used */
  2496. if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
  2497. ret = 2;
  2498. else
  2499. ret = 1;
  2500. gerr:
  2501. EVP_PKEY_free(client_pub_pkey);
  2502. EVP_PKEY_CTX_free(pkey_ctx);
  2503. if (ret)
  2504. return ret;
  2505. else
  2506. goto err;
  2507. }
  2508. else if (!(alg_k & SSL_kPSK))
  2509. {
  2510. al=SSL_AD_HANDSHAKE_FAILURE;
  2511. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_UNKNOWN_CIPHER_TYPE);
  2512. goto f_err;
  2513. }
  2514. return(1);
  2515. f_err:
  2516. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  2517. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH)
  2518. err:
  2519. #endif
  2520. #ifndef OPENSSL_NO_ECDH
  2521. EVP_PKEY_free(clnt_pub_pkey);
  2522. EC_POINT_free(clnt_ecpoint);
  2523. if (srvr_ecdh != NULL)
  2524. EC_KEY_free(srvr_ecdh);
  2525. BN_CTX_free(bn_ctx);
  2526. #endif
  2527. return(-1);
  2528. }
  2529. int ssl3_get_cert_verify(SSL *s)
  2530. {
  2531. EVP_PKEY *pkey=NULL;
  2532. unsigned char *p;
  2533. int al,ok,ret=0;
  2534. long n;
  2535. int type=0,i,j;
  2536. X509 *peer;
  2537. const EVP_MD *md = NULL;
  2538. EVP_MD_CTX mctx;
  2539. EVP_MD_CTX_init(&mctx);
  2540. n=s->method->ssl_get_message(s,
  2541. SSL3_ST_SR_CERT_VRFY_A,
  2542. SSL3_ST_SR_CERT_VRFY_B,
  2543. -1,
  2544. 516, /* Enough for 4096 bit RSA key with TLS v1.2 */
  2545. &ok);
  2546. if (!ok) return((int)n);
  2547. if (s->session->peer != NULL)
  2548. {
  2549. peer=s->session->peer;
  2550. pkey=X509_get_pubkey(peer);
  2551. type=X509_certificate_type(peer,pkey);
  2552. }
  2553. else
  2554. {
  2555. peer=NULL;
  2556. pkey=NULL;
  2557. }
  2558. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY)
  2559. {
  2560. s->s3->tmp.reuse_message=1;
  2561. if ((peer != NULL) && (type & EVP_PKT_SIGN))
  2562. {
  2563. al=SSL_AD_UNEXPECTED_MESSAGE;
  2564. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_MISSING_VERIFY_MESSAGE);
  2565. goto f_err;
  2566. }
  2567. ret=1;
  2568. goto end;
  2569. }
  2570. if (peer == NULL)
  2571. {
  2572. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_NO_CLIENT_CERT_RECEIVED);
  2573. al=SSL_AD_UNEXPECTED_MESSAGE;
  2574. goto f_err;
  2575. }
  2576. if (!(type & EVP_PKT_SIGN))
  2577. {
  2578. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
  2579. al=SSL_AD_ILLEGAL_PARAMETER;
  2580. goto f_err;
  2581. }
  2582. if (s->s3->change_cipher_spec)
  2583. {
  2584. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_CCS_RECEIVED_EARLY);
  2585. al=SSL_AD_UNEXPECTED_MESSAGE;
  2586. goto f_err;
  2587. }
  2588. /* we now have a signature that we need to verify */
  2589. p=(unsigned char *)s->init_msg;
  2590. /* Check for broken implementations of GOST ciphersuites */
  2591. /* If key is GOST and n is exactly 64, it is bare
  2592. * signature without length field */
  2593. if (n==64 && (pkey->type==NID_id_GostR3410_94 ||
  2594. pkey->type == NID_id_GostR3410_2001) )
  2595. {
  2596. i=64;
  2597. }
  2598. else
  2599. {
  2600. if (SSL_USE_SIGALGS(s))
  2601. {
  2602. int rv = tls12_check_peer_sigalg(&md, s, p, pkey);
  2603. if (rv == -1)
  2604. {
  2605. al = SSL_AD_INTERNAL_ERROR;
  2606. goto f_err;
  2607. }
  2608. else if (rv == 0)
  2609. {
  2610. al = SSL_AD_DECODE_ERROR;
  2611. goto f_err;
  2612. }
  2613. #ifdef SSL_DEBUG
  2614. fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
  2615. #endif
  2616. p += 2;
  2617. n -= 2;
  2618. }
  2619. n2s(p,i);
  2620. n-=2;
  2621. if (i > n)
  2622. {
  2623. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_LENGTH_MISMATCH);
  2624. al=SSL_AD_DECODE_ERROR;
  2625. goto f_err;
  2626. }
  2627. }
  2628. j=EVP_PKEY_size(pkey);
  2629. if ((i > j) || (n > j) || (n <= 0))
  2630. {
  2631. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_WRONG_SIGNATURE_SIZE);
  2632. al=SSL_AD_DECODE_ERROR;
  2633. goto f_err;
  2634. }
  2635. if (SSL_USE_SIGALGS(s))
  2636. {
  2637. long hdatalen = 0;
  2638. char *hdata;
  2639. hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
  2640. if (hdatalen <= 0)
  2641. {
  2642. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, ERR_R_INTERNAL_ERROR);
  2643. al=SSL_AD_INTERNAL_ERROR;
  2644. goto f_err;
  2645. }
  2646. #ifdef SSL_DEBUG
  2647. fprintf(stderr, "Using TLS 1.2 with client verify alg %s\n",
  2648. EVP_MD_name(md));
  2649. #endif
  2650. if (!EVP_VerifyInit_ex(&mctx, md, NULL)
  2651. || !EVP_VerifyUpdate(&mctx, hdata, hdatalen))
  2652. {
  2653. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, ERR_R_EVP_LIB);
  2654. al=SSL_AD_INTERNAL_ERROR;
  2655. goto f_err;
  2656. }
  2657. if (EVP_VerifyFinal(&mctx, p , i, pkey) <= 0)
  2658. {
  2659. al=SSL_AD_DECRYPT_ERROR;
  2660. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_SIGNATURE);
  2661. goto f_err;
  2662. }
  2663. }
  2664. else
  2665. #ifndef OPENSSL_NO_RSA
  2666. if (pkey->type == EVP_PKEY_RSA)
  2667. {
  2668. i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
  2669. MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i,
  2670. pkey->pkey.rsa);
  2671. if (i < 0)
  2672. {
  2673. al=SSL_AD_DECRYPT_ERROR;
  2674. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_RSA_DECRYPT);
  2675. goto f_err;
  2676. }
  2677. if (i == 0)
  2678. {
  2679. al=SSL_AD_DECRYPT_ERROR;
  2680. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_RSA_SIGNATURE);
  2681. goto f_err;
  2682. }
  2683. }
  2684. else
  2685. #endif
  2686. #ifndef OPENSSL_NO_DSA
  2687. if (pkey->type == EVP_PKEY_DSA)
  2688. {
  2689. j=DSA_verify(pkey->save_type,
  2690. &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
  2691. SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa);
  2692. if (j <= 0)
  2693. {
  2694. /* bad signature */
  2695. al=SSL_AD_DECRYPT_ERROR;
  2696. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_DSA_SIGNATURE);
  2697. goto f_err;
  2698. }
  2699. }
  2700. else
  2701. #endif
  2702. #ifndef OPENSSL_NO_ECDSA
  2703. if (pkey->type == EVP_PKEY_EC)
  2704. {
  2705. j=ECDSA_verify(pkey->save_type,
  2706. &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
  2707. SHA_DIGEST_LENGTH,p,i,pkey->pkey.ec);
  2708. if (j <= 0)
  2709. {
  2710. /* bad signature */
  2711. al=SSL_AD_DECRYPT_ERROR;
  2712. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_ECDSA_SIGNATURE);
  2713. goto f_err;
  2714. }
  2715. }
  2716. else
  2717. #endif
  2718. if (pkey->type == NID_id_GostR3410_94 || pkey->type == NID_id_GostR3410_2001)
  2719. { unsigned char signature[64];
  2720. int idx;
  2721. EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey,NULL);
  2722. EVP_PKEY_verify_init(pctx);
  2723. if (i!=64) {
  2724. fprintf(stderr,"GOST signature length is %d",i);
  2725. }
  2726. for (idx=0;idx<64;idx++) {
  2727. signature[63-idx]=p[idx];
  2728. }
  2729. j=EVP_PKEY_verify(pctx,signature,64,s->s3->tmp.cert_verify_md,32);
  2730. EVP_PKEY_CTX_free(pctx);
  2731. if (j<=0)
  2732. {
  2733. al=SSL_AD_DECRYPT_ERROR;
  2734. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_ECDSA_SIGNATURE);
  2735. goto f_err;
  2736. }
  2737. }
  2738. else
  2739. {
  2740. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, ERR_R_INTERNAL_ERROR);
  2741. al=SSL_AD_UNSUPPORTED_CERTIFICATE;
  2742. goto f_err;
  2743. }
  2744. ret=1;
  2745. if (0)
  2746. {
  2747. f_err:
  2748. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  2749. }
  2750. end:
  2751. if (s->s3->handshake_buffer)
  2752. {
  2753. BIO_free(s->s3->handshake_buffer);
  2754. s->s3->handshake_buffer = NULL;
  2755. s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE;
  2756. }
  2757. EVP_MD_CTX_cleanup(&mctx);
  2758. EVP_PKEY_free(pkey);
  2759. return(ret);
  2760. }
  2761. int ssl3_get_client_certificate(SSL *s)
  2762. {
  2763. int i,ok,al,ret= -1;
  2764. X509 *x=NULL;
  2765. unsigned long l,nc,llen,n;
  2766. const unsigned char *p,*q;
  2767. unsigned char *d;
  2768. STACK_OF(X509) *sk=NULL;
  2769. SHA256_CTX sha256;
  2770. n=s->method->ssl_get_message(s,
  2771. SSL3_ST_SR_CERT_A,
  2772. SSL3_ST_SR_CERT_B,
  2773. -1,
  2774. s->max_cert_list,
  2775. &ok);
  2776. if (!ok) return((int)n);
  2777. if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
  2778. {
  2779. if ( (s->verify_mode & SSL_VERIFY_PEER) &&
  2780. (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
  2781. {
  2782. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  2783. al=SSL_AD_HANDSHAKE_FAILURE;
  2784. goto f_err;
  2785. }
  2786. /* If tls asked for a client cert, the client must return a 0 list */
  2787. if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
  2788. {
  2789. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
  2790. al=SSL_AD_UNEXPECTED_MESSAGE;
  2791. goto f_err;
  2792. }
  2793. s->s3->tmp.reuse_message=1;
  2794. return(1);
  2795. }
  2796. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
  2797. {
  2798. al=SSL_AD_UNEXPECTED_MESSAGE;
  2799. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_WRONG_MESSAGE_TYPE);
  2800. goto f_err;
  2801. }
  2802. p=d=(unsigned char *)s->init_msg;
  2803. if ((sk=sk_X509_new_null()) == NULL)
  2804. {
  2805. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
  2806. goto err;
  2807. }
  2808. n2l3(p,llen);
  2809. if (llen+3 != n)
  2810. {
  2811. al=SSL_AD_DECODE_ERROR;
  2812. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_LENGTH_MISMATCH);
  2813. goto f_err;
  2814. }
  2815. for (nc=0; nc<llen; )
  2816. {
  2817. n2l3(p,l);
  2818. if ((l+nc+3) > llen)
  2819. {
  2820. al=SSL_AD_DECODE_ERROR;
  2821. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_CERT_LENGTH_MISMATCH);
  2822. goto f_err;
  2823. }
  2824. if (nc == 0 && s->ctx->retain_only_sha256_of_client_certs)
  2825. {
  2826. /* If this is the first certificate, and we don't want
  2827. * to keep peer certificates in memory, then we hash it
  2828. * right away. */
  2829. SHA256_Init(&sha256);
  2830. SHA256_Update(&sha256, p, l);
  2831. SHA256_Final(s->session->peer_sha256, &sha256);
  2832. s->session->peer_sha256_valid = 1;
  2833. }
  2834. q=p;
  2835. x=d2i_X509(NULL,&p,l);
  2836. if (x == NULL)
  2837. {
  2838. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_ASN1_LIB);
  2839. goto err;
  2840. }
  2841. if (p != (q+l))
  2842. {
  2843. al=SSL_AD_DECODE_ERROR;
  2844. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_CERT_LENGTH_MISMATCH);
  2845. goto f_err;
  2846. }
  2847. if (!sk_X509_push(sk,x))
  2848. {
  2849. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
  2850. goto err;
  2851. }
  2852. x=NULL;
  2853. nc+=l+3;
  2854. }
  2855. if (sk_X509_num(sk) <= 0)
  2856. {
  2857. /* TLS does not mind 0 certs returned */
  2858. if (s->version == SSL3_VERSION)
  2859. {
  2860. al=SSL_AD_HANDSHAKE_FAILURE;
  2861. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_NO_CERTIFICATES_RETURNED);
  2862. goto f_err;
  2863. }
  2864. /* Fail for TLS only if we required a certificate */
  2865. else if ((s->verify_mode & SSL_VERIFY_PEER) &&
  2866. (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
  2867. {
  2868. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  2869. al=SSL_AD_HANDSHAKE_FAILURE;
  2870. goto f_err;
  2871. }
  2872. /* No client certificate so digest cached records */
  2873. if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s))
  2874. {
  2875. al=SSL_AD_INTERNAL_ERROR;
  2876. goto f_err;
  2877. }
  2878. }
  2879. else
  2880. {
  2881. i=ssl_verify_cert_chain(s,sk);
  2882. if (i <= 0)
  2883. {
  2884. al=ssl_verify_alarm_type(s->verify_result);
  2885. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_CERTIFICATE_VERIFY_FAILED);
  2886. goto f_err;
  2887. }
  2888. }
  2889. if (s->session->peer != NULL) /* This should not be needed */
  2890. X509_free(s->session->peer);
  2891. s->session->peer=sk_X509_shift(sk);
  2892. s->session->verify_result = s->verify_result;
  2893. /* With the current implementation, sess_cert will always be NULL
  2894. * when we arrive here. */
  2895. if (s->session->sess_cert == NULL)
  2896. {
  2897. s->session->sess_cert = ssl_sess_cert_new();
  2898. if (s->session->sess_cert == NULL)
  2899. {
  2900. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
  2901. goto err;
  2902. }
  2903. }
  2904. if (s->session->sess_cert->cert_chain != NULL)
  2905. sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
  2906. s->session->sess_cert->cert_chain=sk;
  2907. /* Inconsistency alert: cert_chain does *not* include the
  2908. * peer's own certificate, while we do include it in s3_clnt.c */
  2909. sk=NULL;
  2910. ret=1;
  2911. if (0)
  2912. {
  2913. f_err:
  2914. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  2915. }
  2916. err:
  2917. if (x != NULL) X509_free(x);
  2918. if (sk != NULL) sk_X509_pop_free(sk,X509_free);
  2919. return(ret);
  2920. }
  2921. int ssl3_send_server_certificate(SSL *s)
  2922. {
  2923. CERT_PKEY *cpk;
  2924. if (s->state == SSL3_ST_SW_CERT_A)
  2925. {
  2926. cpk=ssl_get_server_send_pkey(s);
  2927. if (cpk == NULL)
  2928. {
  2929. /* VRS: allow null cert if auth == KRB5 */
  2930. if ((s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5) ||
  2931. (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5))
  2932. {
  2933. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_certificate, ERR_R_INTERNAL_ERROR);
  2934. return(0);
  2935. }
  2936. }
  2937. ssl3_output_cert_chain(s,cpk);
  2938. s->state=SSL3_ST_SW_CERT_B;
  2939. }
  2940. /* SSL3_ST_SW_CERT_B */
  2941. return ssl_do_write(s);
  2942. }
  2943. #ifndef OPENSSL_NO_TLSEXT
  2944. /* send a new session ticket (not necessarily for a new session) */
  2945. int ssl3_send_newsession_ticket(SSL *s)
  2946. {
  2947. if (s->state == SSL3_ST_SW_SESSION_TICKET_A)
  2948. {
  2949. unsigned char *p, *senc, *macstart;
  2950. const unsigned char *const_p;
  2951. int len, slen_full, slen;
  2952. SSL_SESSION *sess;
  2953. unsigned int hlen;
  2954. EVP_CIPHER_CTX ctx;
  2955. HMAC_CTX hctx;
  2956. SSL_CTX *tctx = s->initial_ctx;
  2957. unsigned char iv[EVP_MAX_IV_LENGTH];
  2958. unsigned char key_name[16];
  2959. /* get session encoding length */
  2960. slen_full = i2d_SSL_SESSION(s->session, NULL);
  2961. /* Some length values are 16 bits, so forget it if session is
  2962. * too long
  2963. */
  2964. if (slen_full > 0xFF00)
  2965. return -1;
  2966. senc = OPENSSL_malloc(slen_full);
  2967. if (!senc)
  2968. return -1;
  2969. p = senc;
  2970. i2d_SSL_SESSION(s->session, &p);
  2971. /* create a fresh copy (not shared with other threads) to clean up */
  2972. const_p = senc;
  2973. sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
  2974. if (sess == NULL)
  2975. {
  2976. OPENSSL_free(senc);
  2977. return -1;
  2978. }
  2979. sess->session_id_length = 0; /* ID is irrelevant for the ticket */
  2980. slen = i2d_SSL_SESSION(sess, NULL);
  2981. if (slen > slen_full) /* shouldn't ever happen */
  2982. {
  2983. OPENSSL_free(senc);
  2984. return -1;
  2985. }
  2986. p = senc;
  2987. i2d_SSL_SESSION(sess, &p);
  2988. SSL_SESSION_free(sess);
  2989. /* Grow buffer if need be: the length calculation is as
  2990. * follows handshake_header_length +
  2991. * 4 (ticket lifetime hint) + 2 (ticket length) +
  2992. * 16 (key name) + max_iv_len (iv length) +
  2993. * session_length + max_enc_block_size (max encrypted session
  2994. * length) + max_md_size (HMAC).
  2995. */
  2996. if (!BUF_MEM_grow(s->init_buf,
  2997. SSL_HM_HEADER_LENGTH(s) + 22 + EVP_MAX_IV_LENGTH +
  2998. EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen))
  2999. return -1;
  3000. p = ssl_handshake_start(s);
  3001. EVP_CIPHER_CTX_init(&ctx);
  3002. HMAC_CTX_init(&hctx);
  3003. /* Initialize HMAC and cipher contexts. If callback present
  3004. * it does all the work otherwise use generated values
  3005. * from parent ctx.
  3006. */
  3007. if (tctx->tlsext_ticket_key_cb)
  3008. {
  3009. if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
  3010. &hctx, 1) < 0)
  3011. {
  3012. OPENSSL_free(senc);
  3013. return -1;
  3014. }
  3015. }
  3016. else
  3017. {
  3018. RAND_pseudo_bytes(iv, 16);
  3019. EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
  3020. tctx->tlsext_tick_aes_key, iv);
  3021. HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
  3022. tlsext_tick_md(), NULL);
  3023. memcpy(key_name, tctx->tlsext_tick_key_name, 16);
  3024. }
  3025. /* Ticket lifetime hint (advisory only):
  3026. * We leave this unspecified for resumed session (for simplicity),
  3027. * and guess that tickets for new sessions will live as long
  3028. * as their sessions. */
  3029. l2n(s->hit ? 0 : s->session->timeout, p);
  3030. /* Skip ticket length for now */
  3031. p += 2;
  3032. /* Output key name */
  3033. macstart = p;
  3034. memcpy(p, key_name, 16);
  3035. p += 16;
  3036. /* output IV */
  3037. memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
  3038. p += EVP_CIPHER_CTX_iv_length(&ctx);
  3039. /* Encrypt session data */
  3040. EVP_EncryptUpdate(&ctx, p, &len, senc, slen);
  3041. p += len;
  3042. EVP_EncryptFinal_ex(&ctx, p, &len);
  3043. p += len;
  3044. EVP_CIPHER_CTX_cleanup(&ctx);
  3045. HMAC_Update(&hctx, macstart, p - macstart);
  3046. HMAC_Final(&hctx, p, &hlen);
  3047. HMAC_CTX_cleanup(&hctx);
  3048. p += hlen;
  3049. /* Now write out lengths: p points to end of data written */
  3050. /* Total length */
  3051. len = p - ssl_handshake_start(s);
  3052. ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len);
  3053. /* Skip ticket lifetime hint */
  3054. p = ssl_handshake_start(s) + 4;
  3055. s2n(len - 6, p);
  3056. s->state=SSL3_ST_SW_SESSION_TICKET_B;
  3057. OPENSSL_free(senc);
  3058. }
  3059. /* SSL3_ST_SW_SESSION_TICKET_B */
  3060. return ssl_do_write(s);
  3061. }
  3062. int ssl3_send_cert_status(SSL *s)
  3063. {
  3064. if (s->state == SSL3_ST_SW_CERT_STATUS_A)
  3065. {
  3066. unsigned char *p;
  3067. /* Grow buffer if need be: the length calculation is as
  3068. * follows 1 (message type) + 3 (message length) +
  3069. * 1 (ocsp response type) + 3 (ocsp response length)
  3070. * + (ocsp response)
  3071. */
  3072. if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen))
  3073. return -1;
  3074. p=(unsigned char *)s->init_buf->data;
  3075. /* do the header */
  3076. *(p++)=SSL3_MT_CERTIFICATE_STATUS;
  3077. /* message length */
  3078. l2n3(s->tlsext_ocsp_resplen + 4, p);
  3079. /* status type */
  3080. *(p++)= s->tlsext_status_type;
  3081. /* length of OCSP response */
  3082. l2n3(s->tlsext_ocsp_resplen, p);
  3083. /* actual response */
  3084. memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
  3085. /* number of bytes to write */
  3086. s->init_num = 8 + s->tlsext_ocsp_resplen;
  3087. s->state=SSL3_ST_SW_CERT_STATUS_B;
  3088. s->init_off = 0;
  3089. }
  3090. /* SSL3_ST_SW_CERT_STATUS_B */
  3091. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  3092. }
  3093. # ifndef OPENSSL_NO_NEXTPROTONEG
  3094. /* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
  3095. * sets the next_proto member in s if found */
  3096. int ssl3_get_next_proto(SSL *s)
  3097. {
  3098. int ok;
  3099. int proto_len, padding_len;
  3100. long n;
  3101. const unsigned char *p;
  3102. /* Clients cannot send a NextProtocol message if we didn't see the
  3103. * extension in their ClientHello */
  3104. if (!s->s3->next_proto_neg_seen)
  3105. {
  3106. OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto, SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
  3107. return -1;
  3108. }
  3109. n=s->method->ssl_get_message(s,
  3110. SSL3_ST_SR_NEXT_PROTO_A,
  3111. SSL3_ST_SR_NEXT_PROTO_B,
  3112. SSL3_MT_NEXT_PROTO,
  3113. 514, /* See the payload format below */
  3114. &ok);
  3115. if (!ok)
  3116. return((int)n);
  3117. /* s->state doesn't reflect whether ChangeCipherSpec has been received
  3118. * in this handshake, but s->s3->change_cipher_spec does (will be reset
  3119. * by ssl3_get_finished). */
  3120. if (!s->s3->change_cipher_spec)
  3121. {
  3122. OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto, SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS);
  3123. return -1;
  3124. }
  3125. if (n < 2)
  3126. return 0; /* The body must be > 1 bytes long */
  3127. p=(unsigned char *)s->init_msg;
  3128. /* The payload looks like:
  3129. * uint8 proto_len;
  3130. * uint8 proto[proto_len];
  3131. * uint8 padding_len;
  3132. * uint8 padding[padding_len];
  3133. */
  3134. proto_len = p[0];
  3135. if (proto_len + 2 > s->init_num)
  3136. return 0;
  3137. padding_len = p[proto_len + 1];
  3138. if (proto_len + padding_len + 2 != s->init_num)
  3139. return 0;
  3140. s->next_proto_negotiated = OPENSSL_malloc(proto_len);
  3141. if (!s->next_proto_negotiated)
  3142. {
  3143. OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto, ERR_R_MALLOC_FAILURE);
  3144. return 0;
  3145. }
  3146. memcpy(s->next_proto_negotiated, p + 1, proto_len);
  3147. s->next_proto_negotiated_len = proto_len;
  3148. return 1;
  3149. }
  3150. # endif
  3151. /* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
  3152. int ssl3_get_channel_id(SSL *s)
  3153. {
  3154. int ret = -1, ok;
  3155. long n;
  3156. const unsigned char *p;
  3157. unsigned short extension_type, extension_len;
  3158. EC_GROUP* p256 = NULL;
  3159. EC_KEY* key = NULL;
  3160. EC_POINT* point = NULL;
  3161. ECDSA_SIG sig;
  3162. BIGNUM x, y;
  3163. unsigned short expected_extension_type;
  3164. if (s->state == SSL3_ST_SR_CHANNEL_ID_A && s->init_num == 0)
  3165. {
  3166. /* The first time that we're called we take the current
  3167. * handshake hash and store it. */
  3168. EVP_MD_CTX md_ctx;
  3169. unsigned int len;
  3170. EVP_MD_CTX_init(&md_ctx);
  3171. EVP_DigestInit_ex(&md_ctx, EVP_sha256(), NULL);
  3172. if (!tls1_channel_id_hash(&md_ctx, s))
  3173. return -1;
  3174. len = sizeof(s->s3->tlsext_channel_id);
  3175. EVP_DigestFinal(&md_ctx, s->s3->tlsext_channel_id, &len);
  3176. EVP_MD_CTX_cleanup(&md_ctx);
  3177. }
  3178. n = s->method->ssl_get_message(s,
  3179. SSL3_ST_SR_CHANNEL_ID_A,
  3180. SSL3_ST_SR_CHANNEL_ID_B,
  3181. SSL3_MT_ENCRYPTED_EXTENSIONS,
  3182. 2 + 2 + TLSEXT_CHANNEL_ID_SIZE,
  3183. &ok);
  3184. if (!ok)
  3185. return((int)n);
  3186. ssl3_finish_mac(s, (unsigned char*)s->init_buf->data, s->init_num + 4);
  3187. /* s->state doesn't reflect whether ChangeCipherSpec has been received
  3188. * in this handshake, but s->s3->change_cipher_spec does (will be reset
  3189. * by ssl3_get_finished). */
  3190. if (!s->s3->change_cipher_spec)
  3191. {
  3192. OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_GOT_CHANNEL_ID_BEFORE_A_CCS);
  3193. return -1;
  3194. }
  3195. if (n != 2 + 2 + TLSEXT_CHANNEL_ID_SIZE)
  3196. {
  3197. OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_INVALID_MESSAGE);
  3198. return -1;
  3199. }
  3200. p = (unsigned char *)s->init_msg;
  3201. /* The payload looks like:
  3202. * uint16 extension_type
  3203. * uint16 extension_len;
  3204. * uint8 x[32];
  3205. * uint8 y[32];
  3206. * uint8 r[32];
  3207. * uint8 s[32];
  3208. */
  3209. n2s(p, extension_type);
  3210. n2s(p, extension_len);
  3211. expected_extension_type = TLSEXT_TYPE_channel_id;
  3212. if (s->s3->tlsext_channel_id_new)
  3213. expected_extension_type = TLSEXT_TYPE_channel_id_new;
  3214. if (extension_type != expected_extension_type ||
  3215. extension_len != TLSEXT_CHANNEL_ID_SIZE)
  3216. {
  3217. OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_INVALID_MESSAGE);
  3218. return -1;
  3219. }
  3220. p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
  3221. if (!p256)
  3222. {
  3223. OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_NO_P256_SUPPORT);
  3224. return -1;
  3225. }
  3226. BN_init(&x);
  3227. BN_init(&y);
  3228. sig.r = BN_new();
  3229. sig.s = BN_new();
  3230. if (BN_bin2bn(p + 0, 32, &x) == NULL ||
  3231. BN_bin2bn(p + 32, 32, &y) == NULL ||
  3232. BN_bin2bn(p + 64, 32, sig.r) == NULL ||
  3233. BN_bin2bn(p + 96, 32, sig.s) == NULL)
  3234. goto err;
  3235. point = EC_POINT_new(p256);
  3236. if (!point ||
  3237. !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL))
  3238. goto err;
  3239. key = EC_KEY_new();
  3240. if (!key ||
  3241. !EC_KEY_set_group(key, p256) ||
  3242. !EC_KEY_set_public_key(key, point))
  3243. goto err;
  3244. /* We stored the handshake hash in |tlsext_channel_id| the first time
  3245. * that we were called. */
  3246. switch (ECDSA_do_verify(s->s3->tlsext_channel_id, SHA256_DIGEST_LENGTH, &sig, key)) {
  3247. case 1:
  3248. break;
  3249. case 0:
  3250. OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
  3251. s->s3->tlsext_channel_id_valid = 0;
  3252. goto err;
  3253. default:
  3254. s->s3->tlsext_channel_id_valid = 0;
  3255. goto err;
  3256. }
  3257. memcpy(s->s3->tlsext_channel_id, p, 64);
  3258. ret = 1;
  3259. err:
  3260. BN_free(&x);
  3261. BN_free(&y);
  3262. BN_free(sig.r);
  3263. BN_free(sig.s);
  3264. if (key)
  3265. EC_KEY_free(key);
  3266. if (point)
  3267. EC_POINT_free(point);
  3268. if (p256)
  3269. EC_GROUP_free(p256);
  3270. return ret;
  3271. }
  3272. int tls1_send_server_supplemental_data(SSL *s)
  3273. {
  3274. size_t length = 0;
  3275. const unsigned char *authz, *orig_authz;
  3276. unsigned char *p;
  3277. size_t authz_length, i;
  3278. if (s->state != SSL3_ST_SW_SUPPLEMENTAL_DATA_A)
  3279. return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
  3280. orig_authz = authz = ssl_get_authz_data(s, &authz_length);
  3281. if (authz == NULL)
  3282. {
  3283. /* This should never occur. */
  3284. return 0;
  3285. }
  3286. /* First we walk over the authz data to see how long the handshake
  3287. * message will be. */
  3288. for (i = 0; i < authz_length; i++)
  3289. {
  3290. unsigned short len;
  3291. unsigned char type;
  3292. type = *(authz++);
  3293. n2s(authz, len);
  3294. /* n2s increments authz by 2*/
  3295. i += 2;
  3296. if (memchr(s->s3->tlsext_authz_client_types,
  3297. type,
  3298. s->s3->tlsext_authz_client_types_len) != NULL)
  3299. length += 1 /* authz type */ + 2 /* length */ + len;
  3300. authz += len;
  3301. i += len;
  3302. }
  3303. length += 1 /* handshake type */ +
  3304. 3 /* handshake length */ +
  3305. 3 /* supplemental data length */ +
  3306. 2 /* supplemental entry type */ +
  3307. 2 /* supplemental entry length */;
  3308. if (!BUF_MEM_grow_clean(s->init_buf, length))
  3309. {
  3310. OPENSSL_PUT_ERROR(SSL, tls1_send_server_supplemental_data, ERR_R_BUF_LIB);
  3311. return 0;
  3312. }
  3313. p = (unsigned char *)s->init_buf->data;
  3314. *(p++) = SSL3_MT_SUPPLEMENTAL_DATA;
  3315. /* Handshake length */
  3316. l2n3(length - 4, p);
  3317. /* Length of supplemental data */
  3318. l2n3(length - 7, p);
  3319. /* Supplemental data type */
  3320. s2n(TLSEXT_SUPPLEMENTALDATATYPE_authz_data, p);
  3321. /* Its length */
  3322. s2n(length - 11, p);
  3323. authz = orig_authz;
  3324. /* Walk over the authz again and append the selected elements. */
  3325. for (i = 0; i < authz_length; i++)
  3326. {
  3327. unsigned short len;
  3328. unsigned char type;
  3329. type = *(authz++);
  3330. n2s(authz, len);
  3331. /* n2s increments authz by 2 */
  3332. i += 2;
  3333. if (memchr(s->s3->tlsext_authz_client_types,
  3334. type,
  3335. s->s3->tlsext_authz_client_types_len) != NULL)
  3336. {
  3337. *(p++) = type;
  3338. s2n(len, p);
  3339. memcpy(p, authz, len);
  3340. p += len;
  3341. }
  3342. authz += len;
  3343. i += len;
  3344. }
  3345. s->state = SSL3_ST_SW_SUPPLEMENTAL_DATA_B;
  3346. s->init_num = length;
  3347. s->init_off = 0;
  3348. return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
  3349. }
  3350. #endif