statem_clnt.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919
  1. /* ssl/statem/statem_clnt.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. /* ====================================================================
  112. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  113. *
  114. * Portions of the attached software ("Contribution") are developed by
  115. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  116. *
  117. * The Contribution is licensed pursuant to the OpenSSL open source
  118. * license provided above.
  119. *
  120. * ECC cipher suite support in OpenSSL originally written by
  121. * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
  122. *
  123. */
  124. /* ====================================================================
  125. * Copyright 2005 Nokia. All rights reserved.
  126. *
  127. * The portions of the attached software ("Contribution") is developed by
  128. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  129. * license.
  130. *
  131. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  132. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  133. * support (see RFC 4279) to OpenSSL.
  134. *
  135. * No patent licenses or other rights except those expressly stated in
  136. * the OpenSSL open source license shall be deemed granted or received
  137. * expressly, by implication, estoppel, or otherwise.
  138. *
  139. * No assurances are provided by Nokia that the Contribution does not
  140. * infringe the patent or other intellectual property rights of any third
  141. * party or that the license provides you with all the necessary rights
  142. * to make use of the Contribution.
  143. *
  144. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  145. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  146. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  147. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  148. * OTHERWISE.
  149. */
  150. #include <stdio.h>
  151. #include "../ssl_locl.h"
  152. #include "statem_locl.h"
  153. #include <openssl/buffer.h>
  154. #include <openssl/rand.h>
  155. #include <openssl/objects.h>
  156. #include <openssl/evp.h>
  157. #include <openssl/md5.h>
  158. #ifndef OPENSSL_NO_DH
  159. # include <openssl/dh.h>
  160. #endif
  161. #include <openssl/bn.h>
  162. #ifndef OPENSSL_NO_ENGINE
  163. # include <openssl/engine.h>
  164. #endif
  165. static ossl_inline int cert_req_allowed(SSL *s);
  166. static int key_exchange_expected(SSL *s);
  167. static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b);
  168. static int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
  169. unsigned char *p);
  170. /*
  171. * Is a CertificateRequest message allowed at the moment or not?
  172. *
  173. * Return values are:
  174. * 1: Yes
  175. * 0: No
  176. */
  177. static ossl_inline int cert_req_allowed(SSL *s)
  178. {
  179. /* TLS does not like anon-DH with client cert */
  180. if ((s->version > SSL3_VERSION
  181. && (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL))
  182. || (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aSRP | SSL_aPSK)))
  183. return 0;
  184. return 1;
  185. }
  186. /*
  187. * Should we expect the ServerKeyExchange message or not?
  188. *
  189. * Return values are:
  190. * 1: Yes
  191. * 0: No
  192. * -1: Error
  193. */
  194. static int key_exchange_expected(SSL *s)
  195. {
  196. long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  197. /*
  198. * Can't skip server key exchange if this is an ephemeral
  199. * ciphersuite or for SRP
  200. */
  201. if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK
  202. | SSL_kSRP)) {
  203. return 1;
  204. }
  205. return 0;
  206. }
  207. /*
  208. * ossl_statem_client_read_transition() encapsulates the logic for the allowed
  209. * handshake state transitions when the client is reading messages from the
  210. * server. The message type that the server has sent is provided in |mt|. The
  211. * current state is in |s->statem.hand_state|.
  212. *
  213. * Return values are:
  214. * 1: Success (transition allowed)
  215. * 0: Error (transition not allowed)
  216. */
  217. int ossl_statem_client_read_transition(SSL *s, int mt)
  218. {
  219. OSSL_STATEM *st = &s->statem;
  220. int ske_expected;
  221. switch(st->hand_state) {
  222. case TLS_ST_CW_CLNT_HELLO:
  223. if (mt == SSL3_MT_SERVER_HELLO) {
  224. st->hand_state = TLS_ST_CR_SRVR_HELLO;
  225. return 1;
  226. }
  227. if (SSL_IS_DTLS(s)) {
  228. if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
  229. st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
  230. return 1;
  231. }
  232. }
  233. break;
  234. case TLS_ST_CR_SRVR_HELLO:
  235. if (s->hit) {
  236. if (s->tlsext_ticket_expected) {
  237. if (mt == SSL3_MT_NEWSESSION_TICKET) {
  238. st->hand_state = TLS_ST_CR_SESSION_TICKET;
  239. return 1;
  240. }
  241. } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  242. st->hand_state = TLS_ST_CR_CHANGE;
  243. return 1;
  244. }
  245. } else {
  246. if (SSL_IS_DTLS(s) && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
  247. st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
  248. return 1;
  249. } else if (s->version >= TLS1_VERSION
  250. && s->tls_session_secret_cb != NULL
  251. && s->session->tlsext_tick != NULL
  252. && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  253. /*
  254. * Normally, we can tell if the server is resuming the session
  255. * from the session ID. EAP-FAST (RFC 4851), however, relies on
  256. * the next server message after the ServerHello to determine if
  257. * the server is resuming.
  258. */
  259. s->hit = 1;
  260. st->hand_state = TLS_ST_CR_CHANGE;
  261. return 1;
  262. } else if (!(s->s3->tmp.new_cipher->algorithm_auth
  263. & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
  264. if (mt == SSL3_MT_CERTIFICATE) {
  265. st->hand_state = TLS_ST_CR_CERT;
  266. return 1;
  267. }
  268. } else {
  269. ske_expected = key_exchange_expected(s);
  270. if (ske_expected < 0)
  271. return 0;
  272. /* SKE is optional for some PSK ciphersuites */
  273. if (ske_expected
  274. || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
  275. && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
  276. if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
  277. st->hand_state = TLS_ST_CR_KEY_EXCH;
  278. return 1;
  279. }
  280. } else if (mt == SSL3_MT_CERTIFICATE_REQUEST
  281. && cert_req_allowed(s)) {
  282. st->hand_state = TLS_ST_CR_CERT_REQ;
  283. return 1;
  284. } else if (mt == SSL3_MT_SERVER_DONE) {
  285. st->hand_state = TLS_ST_CR_SRVR_DONE;
  286. return 1;
  287. }
  288. }
  289. }
  290. break;
  291. case TLS_ST_CR_CERT:
  292. /*
  293. * The CertificateStatus message is optional even if
  294. * |tlsext_status_expected| is set
  295. */
  296. if (s->tlsext_status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) {
  297. st->hand_state = TLS_ST_CR_CERT_STATUS;
  298. return 1;
  299. }
  300. /* Fall through */
  301. case TLS_ST_CR_CERT_STATUS:
  302. ske_expected = key_exchange_expected(s);
  303. if (ske_expected < 0)
  304. return 0;
  305. /* SKE is optional for some PSK ciphersuites */
  306. if (ske_expected
  307. || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
  308. && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
  309. if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
  310. st->hand_state = TLS_ST_CR_KEY_EXCH;
  311. return 1;
  312. }
  313. return 0;
  314. }
  315. /* Fall through */
  316. case TLS_ST_CR_KEY_EXCH:
  317. if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
  318. if (cert_req_allowed(s)) {
  319. st->hand_state = TLS_ST_CR_CERT_REQ;
  320. return 1;
  321. }
  322. return 0;
  323. }
  324. /* Fall through */
  325. case TLS_ST_CR_CERT_REQ:
  326. if (mt == SSL3_MT_SERVER_DONE) {
  327. st->hand_state = TLS_ST_CR_SRVR_DONE;
  328. return 1;
  329. }
  330. break;
  331. case TLS_ST_CW_FINISHED:
  332. if (mt == SSL3_MT_NEWSESSION_TICKET && s->tlsext_ticket_expected) {
  333. st->hand_state = TLS_ST_CR_SESSION_TICKET;
  334. return 1;
  335. } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  336. st->hand_state = TLS_ST_CR_CHANGE;
  337. return 1;
  338. }
  339. break;
  340. case TLS_ST_CR_SESSION_TICKET:
  341. if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  342. st->hand_state = TLS_ST_CR_CHANGE;
  343. return 1;
  344. }
  345. break;
  346. case TLS_ST_CR_CHANGE:
  347. if (mt == SSL3_MT_FINISHED) {
  348. st->hand_state = TLS_ST_CR_FINISHED;
  349. return 1;
  350. }
  351. break;
  352. default:
  353. break;
  354. }
  355. /* No valid transition found */
  356. return 0;
  357. }
  358. /*
  359. * client_write_transition() works out what handshake state to move to next
  360. * when the client is writing messages to be sent to the server.
  361. */
  362. WRITE_TRAN ossl_statem_client_write_transition(SSL *s)
  363. {
  364. OSSL_STATEM *st = &s->statem;
  365. switch(st->hand_state) {
  366. case TLS_ST_OK:
  367. /* Renegotiation - fall through */
  368. case TLS_ST_BEFORE:
  369. st->hand_state = TLS_ST_CW_CLNT_HELLO;
  370. return WRITE_TRAN_CONTINUE;
  371. case TLS_ST_CW_CLNT_HELLO:
  372. /*
  373. * No transition at the end of writing because we don't know what
  374. * we will be sent
  375. */
  376. return WRITE_TRAN_FINISHED;
  377. case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
  378. st->hand_state = TLS_ST_CW_CLNT_HELLO;
  379. return WRITE_TRAN_CONTINUE;
  380. case TLS_ST_CR_SRVR_DONE:
  381. if (s->s3->tmp.cert_req)
  382. st->hand_state = TLS_ST_CW_CERT;
  383. else
  384. st->hand_state = TLS_ST_CW_KEY_EXCH;
  385. return WRITE_TRAN_CONTINUE;
  386. case TLS_ST_CW_CERT:
  387. st->hand_state = TLS_ST_CW_KEY_EXCH;
  388. return WRITE_TRAN_CONTINUE;
  389. case TLS_ST_CW_KEY_EXCH:
  390. /*
  391. * For TLS, cert_req is set to 2, so a cert chain of nothing is
  392. * sent, but no verify packet is sent
  393. */
  394. /*
  395. * XXX: For now, we do not support client authentication in ECDH
  396. * cipher suites with ECDH (rather than ECDSA) certificates. We
  397. * need to skip the certificate verify message when client's
  398. * ECDH public key is sent inside the client certificate.
  399. */
  400. if (s->s3->tmp.cert_req == 1) {
  401. st->hand_state = TLS_ST_CW_CERT_VRFY;
  402. } else {
  403. st->hand_state = TLS_ST_CW_CHANGE;
  404. }
  405. if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
  406. st->hand_state = TLS_ST_CW_CHANGE;
  407. }
  408. return WRITE_TRAN_CONTINUE;
  409. case TLS_ST_CW_CERT_VRFY:
  410. st->hand_state = TLS_ST_CW_CHANGE;
  411. return WRITE_TRAN_CONTINUE;
  412. case TLS_ST_CW_CHANGE:
  413. #if defined(OPENSSL_NO_NEXTPROTONEG)
  414. st->hand_state = TLS_ST_CW_FINISHED;
  415. #else
  416. if (!SSL_IS_DTLS(s) && s->s3->next_proto_neg_seen)
  417. st->hand_state = TLS_ST_CW_NEXT_PROTO;
  418. else
  419. st->hand_state = TLS_ST_CW_FINISHED;
  420. #endif
  421. return WRITE_TRAN_CONTINUE;
  422. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  423. case TLS_ST_CW_NEXT_PROTO:
  424. st->hand_state = TLS_ST_CW_FINISHED;
  425. return WRITE_TRAN_CONTINUE;
  426. #endif
  427. case TLS_ST_CW_FINISHED:
  428. if (s->hit) {
  429. st->hand_state = TLS_ST_OK;
  430. ossl_statem_set_in_init(s, 0);
  431. return WRITE_TRAN_CONTINUE;
  432. } else {
  433. return WRITE_TRAN_FINISHED;
  434. }
  435. case TLS_ST_CR_FINISHED:
  436. if (s->hit) {
  437. st->hand_state = TLS_ST_CW_CHANGE;
  438. return WRITE_TRAN_CONTINUE;
  439. } else {
  440. st->hand_state = TLS_ST_OK;
  441. ossl_statem_set_in_init(s, 0);
  442. return WRITE_TRAN_CONTINUE;
  443. }
  444. default:
  445. /* Shouldn't happen */
  446. return WRITE_TRAN_ERROR;
  447. }
  448. }
  449. /*
  450. * Perform any pre work that needs to be done prior to sending a message from
  451. * the client to the server.
  452. */
  453. WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst)
  454. {
  455. OSSL_STATEM *st = &s->statem;
  456. switch(st->hand_state) {
  457. case TLS_ST_CW_CLNT_HELLO:
  458. s->shutdown = 0;
  459. if (SSL_IS_DTLS(s)) {
  460. /* every DTLS ClientHello resets Finished MAC */
  461. ssl3_init_finished_mac(s);
  462. }
  463. break;
  464. case TLS_ST_CW_CERT:
  465. return tls_prepare_client_certificate(s, wst);
  466. case TLS_ST_CW_CHANGE:
  467. if (SSL_IS_DTLS(s)) {
  468. if (s->hit) {
  469. /*
  470. * We're into the last flight so we don't retransmit these
  471. * messages unless we need to.
  472. */
  473. st->use_timer = 0;
  474. }
  475. #ifndef OPENSSL_NO_SCTP
  476. if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
  477. return dtls_wait_for_dry(s);
  478. #endif
  479. }
  480. return WORK_FINISHED_CONTINUE;
  481. case TLS_ST_OK:
  482. return tls_finish_handshake(s, wst);
  483. default:
  484. /* No pre work to be done */
  485. break;
  486. }
  487. return WORK_FINISHED_CONTINUE;
  488. }
  489. /*
  490. * Perform any work that needs to be done after sending a message from the
  491. * client to the server.
  492. */
  493. WORK_STATE ossl_statem_client_post_work(SSL *s, WORK_STATE wst)
  494. {
  495. OSSL_STATEM *st = &s->statem;
  496. s->init_num = 0;
  497. switch(st->hand_state) {
  498. case TLS_ST_CW_CLNT_HELLO:
  499. if (SSL_IS_DTLS(s) && s->d1->cookie_len > 0 && statem_flush(s) != 1)
  500. return WORK_MORE_A;
  501. #ifndef OPENSSL_NO_SCTP
  502. /* Disable buffering for SCTP */
  503. if (!SSL_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(s))) {
  504. #endif
  505. /*
  506. * turn on buffering for the next lot of output
  507. */
  508. if (s->bbio != s->wbio)
  509. s->wbio = BIO_push(s->bbio, s->wbio);
  510. #ifndef OPENSSL_NO_SCTP
  511. }
  512. #endif
  513. if (SSL_IS_DTLS(s)) {
  514. /* Treat the next message as the first packet */
  515. s->first_packet = 1;
  516. }
  517. break;
  518. case TLS_ST_CW_KEY_EXCH:
  519. if (tls_client_key_exchange_post_work(s) == 0)
  520. return WORK_ERROR;
  521. break;
  522. case TLS_ST_CW_CHANGE:
  523. s->session->cipher = s->s3->tmp.new_cipher;
  524. #ifdef OPENSSL_NO_COMP
  525. s->session->compress_meth = 0;
  526. #else
  527. if (s->s3->tmp.new_compression == NULL)
  528. s->session->compress_meth = 0;
  529. else
  530. s->session->compress_meth = s->s3->tmp.new_compression->id;
  531. #endif
  532. if (!s->method->ssl3_enc->setup_key_block(s))
  533. return WORK_ERROR;
  534. if (!s->method->ssl3_enc->change_cipher_state(s,
  535. SSL3_CHANGE_CIPHER_CLIENT_WRITE))
  536. return WORK_ERROR;
  537. if (SSL_IS_DTLS(s)) {
  538. #ifndef OPENSSL_NO_SCTP
  539. if (s->hit) {
  540. /*
  541. * Change to new shared key of SCTP-Auth, will be ignored if
  542. * no SCTP used.
  543. */
  544. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
  545. 0, NULL);
  546. }
  547. #endif
  548. dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
  549. }
  550. break;
  551. case TLS_ST_CW_FINISHED:
  552. #ifndef OPENSSL_NO_SCTP
  553. if (wst == WORK_MORE_A && SSL_IS_DTLS(s) && s->hit == 0) {
  554. /*
  555. * Change to new shared key of SCTP-Auth, will be ignored if
  556. * no SCTP used.
  557. */
  558. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
  559. 0, NULL);
  560. }
  561. #endif
  562. if (statem_flush(s) != 1)
  563. return WORK_MORE_B;
  564. break;
  565. default:
  566. /* No post work to be done */
  567. break;
  568. }
  569. return WORK_FINISHED_CONTINUE;
  570. }
  571. /*
  572. * Construct a message to be sent from the client to the server.
  573. *
  574. * Valid return values are:
  575. * 1: Success
  576. * 0: Error
  577. */
  578. int ossl_statem_client_construct_message(SSL *s)
  579. {
  580. OSSL_STATEM *st = &s->statem;
  581. switch(st->hand_state) {
  582. case TLS_ST_CW_CLNT_HELLO:
  583. return tls_construct_client_hello(s);
  584. case TLS_ST_CW_CERT:
  585. return tls_construct_client_certificate(s);
  586. case TLS_ST_CW_KEY_EXCH:
  587. return tls_construct_client_key_exchange(s);
  588. case TLS_ST_CW_CERT_VRFY:
  589. return tls_construct_client_verify(s);
  590. case TLS_ST_CW_CHANGE:
  591. if (SSL_IS_DTLS(s))
  592. return dtls_construct_change_cipher_spec(s);
  593. else
  594. return tls_construct_change_cipher_spec(s);
  595. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  596. case TLS_ST_CW_NEXT_PROTO:
  597. return tls_construct_next_proto(s);
  598. #endif
  599. case TLS_ST_CW_FINISHED:
  600. return tls_construct_finished(s,
  601. s->method->
  602. ssl3_enc->client_finished_label,
  603. s->method->
  604. ssl3_enc->client_finished_label_len);
  605. default:
  606. /* Shouldn't happen */
  607. break;
  608. }
  609. return 0;
  610. }
  611. /*
  612. * Returns the maximum allowed length for the current message that we are
  613. * reading. Excludes the message header.
  614. */
  615. unsigned long ossl_statem_client_max_message_size(SSL *s)
  616. {
  617. OSSL_STATEM *st = &s->statem;
  618. switch(st->hand_state) {
  619. case TLS_ST_CR_SRVR_HELLO:
  620. return SERVER_HELLO_MAX_LENGTH;
  621. case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
  622. return HELLO_VERIFY_REQUEST_MAX_LENGTH;
  623. case TLS_ST_CR_CERT:
  624. return s->max_cert_list;
  625. case TLS_ST_CR_CERT_STATUS:
  626. return SSL3_RT_MAX_PLAIN_LENGTH;
  627. case TLS_ST_CR_KEY_EXCH:
  628. return SERVER_KEY_EXCH_MAX_LENGTH;
  629. case TLS_ST_CR_CERT_REQ:
  630. /* Set to s->max_cert_list for compatibility with previous releases.
  631. * In practice these messages can get quite long if servers are
  632. * configured to provide a long list of acceptable CAs
  633. */
  634. return s->max_cert_list;
  635. case TLS_ST_CR_SRVR_DONE:
  636. return SERVER_HELLO_DONE_MAX_LENGTH;
  637. case TLS_ST_CR_CHANGE:
  638. return CCS_MAX_LENGTH;
  639. case TLS_ST_CR_SESSION_TICKET:
  640. return SSL3_RT_MAX_PLAIN_LENGTH;
  641. case TLS_ST_CR_FINISHED:
  642. return FINISHED_MAX_LENGTH;
  643. default:
  644. /* Shouldn't happen */
  645. break;
  646. }
  647. return 0;
  648. }
  649. /*
  650. * Process a message that the client has been received from the server.
  651. */
  652. MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt)
  653. {
  654. OSSL_STATEM *st = &s->statem;
  655. switch(st->hand_state) {
  656. case TLS_ST_CR_SRVR_HELLO:
  657. return tls_process_server_hello(s, pkt);
  658. case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
  659. return dtls_process_hello_verify(s, pkt);
  660. case TLS_ST_CR_CERT:
  661. return tls_process_server_certificate(s, pkt);
  662. case TLS_ST_CR_CERT_STATUS:
  663. return tls_process_cert_status(s, pkt);
  664. case TLS_ST_CR_KEY_EXCH:
  665. return tls_process_key_exchange(s, pkt);
  666. case TLS_ST_CR_CERT_REQ:
  667. return tls_process_certificate_request(s, pkt);
  668. case TLS_ST_CR_SRVR_DONE:
  669. return tls_process_server_done(s, pkt);
  670. case TLS_ST_CR_CHANGE:
  671. return tls_process_change_cipher_spec(s, pkt);
  672. case TLS_ST_CR_SESSION_TICKET:
  673. return tls_process_new_session_ticket(s, pkt);
  674. case TLS_ST_CR_FINISHED:
  675. return tls_process_finished(s, pkt);
  676. default:
  677. /* Shouldn't happen */
  678. break;
  679. }
  680. return MSG_PROCESS_ERROR;
  681. }
  682. /*
  683. * Perform any further processing required following the receipt of a message
  684. * from the server
  685. */
  686. WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst)
  687. {
  688. OSSL_STATEM *st = &s->statem;
  689. switch(st->hand_state) {
  690. #ifndef OPENSSL_NO_SCTP
  691. case TLS_ST_CR_SRVR_DONE:
  692. /* We only get here if we are using SCTP and we are renegotiating */
  693. if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
  694. s->s3->in_read_app_data = 2;
  695. s->rwstate = SSL_READING;
  696. BIO_clear_retry_flags(SSL_get_rbio(s));
  697. BIO_set_retry_read(SSL_get_rbio(s));
  698. ossl_statem_set_sctp_read_sock(s, 1);
  699. return WORK_MORE_A;
  700. }
  701. ossl_statem_set_sctp_read_sock(s, 0);
  702. return WORK_FINISHED_STOP;
  703. #endif
  704. default:
  705. break;
  706. }
  707. /* Shouldn't happen */
  708. return WORK_ERROR;
  709. }
  710. int tls_construct_client_hello(SSL *s)
  711. {
  712. unsigned char *buf;
  713. unsigned char *p, *d;
  714. int i;
  715. int protverr;
  716. unsigned long l;
  717. int al = 0;
  718. #ifndef OPENSSL_NO_COMP
  719. int j;
  720. SSL_COMP *comp;
  721. #endif
  722. SSL_SESSION *sess = s->session;
  723. buf = (unsigned char *)s->init_buf->data;
  724. /* Work out what SSL/TLS/DTLS version to use */
  725. protverr = ssl_set_client_hello_version(s);
  726. if (protverr != 0) {
  727. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, protverr);
  728. goto err;
  729. }
  730. if ((sess == NULL) || (sess->ssl_version != s->version) ||
  731. /*
  732. * In the case of EAP-FAST, we can have a pre-shared
  733. * "ticket" without a session ID.
  734. */
  735. (!sess->session_id_length && !sess->tlsext_tick) ||
  736. (sess->not_resumable)) {
  737. if (!ssl_get_new_session(s, 0))
  738. goto err;
  739. }
  740. /* else use the pre-loaded session */
  741. p = s->s3->client_random;
  742. /*
  743. * for DTLS if client_random is initialized, reuse it, we are
  744. * required to use same upon reply to HelloVerify
  745. */
  746. if (SSL_IS_DTLS(s)) {
  747. size_t idx;
  748. i = 1;
  749. for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
  750. if (p[idx]) {
  751. i = 0;
  752. break;
  753. }
  754. }
  755. } else
  756. i = 1;
  757. if (i && ssl_fill_hello_random(s, 0, p,
  758. sizeof(s->s3->client_random)) <= 0)
  759. goto err;
  760. /* Do the message type and length last */
  761. d = p = ssl_handshake_start(s);
  762. /*-
  763. * version indicates the negotiated version: for example from
  764. * an SSLv2/v3 compatible client hello). The client_version
  765. * field is the maximum version we permit and it is also
  766. * used in RSA encrypted premaster secrets. Some servers can
  767. * choke if we initially report a higher version then
  768. * renegotiate to a lower one in the premaster secret. This
  769. * didn't happen with TLS 1.0 as most servers supported it
  770. * but it can with TLS 1.1 or later if the server only supports
  771. * 1.0.
  772. *
  773. * Possible scenario with previous logic:
  774. * 1. Client hello indicates TLS 1.2
  775. * 2. Server hello says TLS 1.0
  776. * 3. RSA encrypted premaster secret uses 1.2.
  777. * 4. Handhaked proceeds using TLS 1.0.
  778. * 5. Server sends hello request to renegotiate.
  779. * 6. Client hello indicates TLS v1.0 as we now
  780. * know that is maximum server supports.
  781. * 7. Server chokes on RSA encrypted premaster secret
  782. * containing version 1.0.
  783. *
  784. * For interoperability it should be OK to always use the
  785. * maximum version we support in client hello and then rely
  786. * on the checking of version to ensure the servers isn't
  787. * being inconsistent: for example initially negotiating with
  788. * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
  789. * client_version in client hello and not resetting it to
  790. * the negotiated version.
  791. */
  792. *(p++) = s->client_version >> 8;
  793. *(p++) = s->client_version & 0xff;
  794. /* Random stuff */
  795. memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
  796. p += SSL3_RANDOM_SIZE;
  797. /* Session ID */
  798. if (s->new_session)
  799. i = 0;
  800. else
  801. i = s->session->session_id_length;
  802. *(p++) = i;
  803. if (i != 0) {
  804. if (i > (int)sizeof(s->session->session_id)) {
  805. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  806. goto err;
  807. }
  808. memcpy(p, s->session->session_id, i);
  809. p += i;
  810. }
  811. /* cookie stuff for DTLS */
  812. if (SSL_IS_DTLS(s)) {
  813. if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
  814. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  815. goto err;
  816. }
  817. *(p++) = s->d1->cookie_len;
  818. memcpy(p, s->d1->cookie, s->d1->cookie_len);
  819. p += s->d1->cookie_len;
  820. }
  821. /* Ciphers supported */
  822. i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]));
  823. if (i == 0) {
  824. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
  825. goto err;
  826. }
  827. #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
  828. /*
  829. * Some servers hang if client hello > 256 bytes as hack workaround
  830. * chop number of supported ciphers to keep it well below this if we
  831. * use TLS v1.2
  832. */
  833. if (TLS1_get_version(s) >= TLS1_2_VERSION
  834. && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
  835. i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
  836. #endif
  837. s2n(i, p);
  838. p += i;
  839. /* COMPRESSION */
  840. #ifdef OPENSSL_NO_COMP
  841. *(p++) = 1;
  842. #else
  843. if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
  844. j = 0;
  845. else
  846. j = sk_SSL_COMP_num(s->ctx->comp_methods);
  847. *(p++) = 1 + j;
  848. for (i = 0; i < j; i++) {
  849. comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
  850. *(p++) = comp->id;
  851. }
  852. #endif
  853. *(p++) = 0; /* Add the NULL method */
  854. /* TLS extensions */
  855. if (ssl_prepare_clienthello_tlsext(s) <= 0) {
  856. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
  857. goto err;
  858. }
  859. if ((p =
  860. ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
  861. &al)) == NULL) {
  862. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  863. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  864. goto err;
  865. }
  866. l = p - d;
  867. if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
  868. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  869. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  870. goto err;
  871. }
  872. return 1;
  873. err:
  874. ossl_statem_set_error(s);
  875. return 0;
  876. }
  877. MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt)
  878. {
  879. int al;
  880. unsigned int cookie_len;
  881. PACKET cookiepkt;
  882. if (!PACKET_forward(pkt, 2)
  883. || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) {
  884. al = SSL_AD_DECODE_ERROR;
  885. SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
  886. goto f_err;
  887. }
  888. cookie_len = PACKET_remaining(&cookiepkt);
  889. if (cookie_len > sizeof(s->d1->cookie)) {
  890. al = SSL_AD_ILLEGAL_PARAMETER;
  891. SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_TOO_LONG);
  892. goto f_err;
  893. }
  894. if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) {
  895. al = SSL_AD_DECODE_ERROR;
  896. SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
  897. goto f_err;
  898. }
  899. s->d1->cookie_len = cookie_len;
  900. return MSG_PROCESS_FINISHED_READING;
  901. f_err:
  902. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  903. ossl_statem_set_error(s);
  904. return MSG_PROCESS_ERROR;
  905. }
  906. MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt)
  907. {
  908. STACK_OF(SSL_CIPHER) *sk;
  909. const SSL_CIPHER *c;
  910. PACKET session_id;
  911. size_t session_id_len;
  912. unsigned char *cipherchars;
  913. int i, al = SSL_AD_INTERNAL_ERROR;
  914. unsigned int compression;
  915. unsigned int sversion;
  916. int protverr;
  917. #ifndef OPENSSL_NO_COMP
  918. SSL_COMP *comp;
  919. #endif
  920. if (!PACKET_get_net_2(pkt, &sversion)) {
  921. al = SSL_AD_DECODE_ERROR;
  922. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
  923. goto f_err;
  924. }
  925. protverr = ssl_choose_client_version(s, sversion);
  926. if (protverr != 0) {
  927. al = SSL_AD_PROTOCOL_VERSION;
  928. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, protverr);
  929. goto f_err;
  930. }
  931. /* load the server hello data */
  932. /* load the server random */
  933. if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
  934. al = SSL_AD_DECODE_ERROR;
  935. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
  936. goto f_err;
  937. }
  938. s->hit = 0;
  939. /* Get the session-id. */
  940. if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
  941. al = SSL_AD_DECODE_ERROR;
  942. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
  943. goto f_err;
  944. }
  945. session_id_len = PACKET_remaining(&session_id);
  946. if (session_id_len > sizeof s->session->session_id
  947. || session_id_len > SSL3_SESSION_ID_SIZE) {
  948. al = SSL_AD_ILLEGAL_PARAMETER;
  949. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
  950. goto f_err;
  951. }
  952. if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
  953. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
  954. al = SSL_AD_DECODE_ERROR;
  955. goto f_err;
  956. }
  957. /*
  958. * Check if we can resume the session based on external pre-shared secret.
  959. * EAP-FAST (RFC 4851) supports two types of session resumption.
  960. * Resumption based on server-side state works with session IDs.
  961. * Resumption based on pre-shared Protected Access Credentials (PACs)
  962. * works by overriding the SessionTicket extension at the application
  963. * layer, and does not send a session ID. (We do not know whether EAP-FAST
  964. * servers would honour the session ID.) Therefore, the session ID alone
  965. * is not a reliable indicator of session resumption, so we first check if
  966. * we can resume, and later peek at the next handshake message to see if the
  967. * server wants to resume.
  968. */
  969. if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
  970. s->session->tlsext_tick) {
  971. const SSL_CIPHER *pref_cipher = NULL;
  972. s->session->master_key_length = sizeof(s->session->master_key);
  973. if (s->tls_session_secret_cb(s, s->session->master_key,
  974. &s->session->master_key_length,
  975. NULL, &pref_cipher,
  976. s->tls_session_secret_cb_arg)) {
  977. s->session->cipher = pref_cipher ?
  978. pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
  979. } else {
  980. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
  981. al = SSL_AD_INTERNAL_ERROR;
  982. goto f_err;
  983. }
  984. }
  985. if (session_id_len != 0 && session_id_len == s->session->session_id_length
  986. && memcmp(PACKET_data(&session_id), s->session->session_id,
  987. session_id_len) == 0) {
  988. if (s->sid_ctx_length != s->session->sid_ctx_length
  989. || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
  990. /* actually a client application bug */
  991. al = SSL_AD_ILLEGAL_PARAMETER;
  992. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
  993. SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  994. goto f_err;
  995. }
  996. s->hit = 1;
  997. } else {
  998. /*
  999. * If we were trying for session-id reuse but the server
  1000. * didn't echo the ID, make a new SSL_SESSION.
  1001. * In the case of EAP-FAST and PAC, we do not send a session ID,
  1002. * so the PAC-based session secret is always preserved. It'll be
  1003. * overwritten if the server refuses resumption.
  1004. */
  1005. if (s->session->session_id_length > 0) {
  1006. if (!ssl_get_new_session(s, 0)) {
  1007. goto f_err;
  1008. }
  1009. }
  1010. s->session->session_id_length = session_id_len;
  1011. /* session_id_len could be 0 */
  1012. memcpy(s->session->session_id, PACKET_data(&session_id),
  1013. session_id_len);
  1014. }
  1015. c = ssl_get_cipher_by_char(s, cipherchars);
  1016. if (c == NULL) {
  1017. /* unknown cipher */
  1018. al = SSL_AD_ILLEGAL_PARAMETER;
  1019. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
  1020. goto f_err;
  1021. }
  1022. /* Set version disabled mask now we know version */
  1023. if (!SSL_USE_TLS1_2_CIPHERS(s))
  1024. s->s3->tmp.mask_ssl = SSL_TLSV1_2;
  1025. else
  1026. s->s3->tmp.mask_ssl = 0;
  1027. /* Skip TLS v1.0 ciphersuites if SSLv3 */
  1028. if ((c->algorithm_ssl & SSL_TLSV1) && s->version == SSL3_VERSION)
  1029. s->s3->tmp.mask_ssl |= SSL_TLSV1;
  1030. /*
  1031. * If it is a disabled cipher we didn't send it in client hello, so
  1032. * return an error.
  1033. */
  1034. if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
  1035. al = SSL_AD_ILLEGAL_PARAMETER;
  1036. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
  1037. goto f_err;
  1038. }
  1039. sk = ssl_get_ciphers_by_id(s);
  1040. i = sk_SSL_CIPHER_find(sk, c);
  1041. if (i < 0) {
  1042. /* we did not say we would use this cipher */
  1043. al = SSL_AD_ILLEGAL_PARAMETER;
  1044. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
  1045. goto f_err;
  1046. }
  1047. /*
  1048. * Depending on the session caching (internal/external), the cipher
  1049. * and/or cipher_id values may not be set. Make sure that cipher_id is
  1050. * set and use it for comparison.
  1051. */
  1052. if (s->session->cipher)
  1053. s->session->cipher_id = s->session->cipher->id;
  1054. if (s->hit && (s->session->cipher_id != c->id)) {
  1055. al = SSL_AD_ILLEGAL_PARAMETER;
  1056. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
  1057. SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  1058. goto f_err;
  1059. }
  1060. s->s3->tmp.new_cipher = c;
  1061. /* lets get the compression algorithm */
  1062. /* COMPRESSION */
  1063. if (!PACKET_get_1(pkt, &compression)) {
  1064. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
  1065. al = SSL_AD_DECODE_ERROR;
  1066. goto f_err;
  1067. }
  1068. #ifdef OPENSSL_NO_COMP
  1069. if (compression != 0) {
  1070. al = SSL_AD_ILLEGAL_PARAMETER;
  1071. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
  1072. SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  1073. goto f_err;
  1074. }
  1075. /*
  1076. * If compression is disabled we'd better not try to resume a session
  1077. * using compression.
  1078. */
  1079. if (s->session->compress_meth != 0) {
  1080. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
  1081. goto f_err;
  1082. }
  1083. #else
  1084. if (s->hit && compression != s->session->compress_meth) {
  1085. al = SSL_AD_ILLEGAL_PARAMETER;
  1086. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
  1087. SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
  1088. goto f_err;
  1089. }
  1090. if (compression == 0)
  1091. comp = NULL;
  1092. else if (!ssl_allow_compression(s)) {
  1093. al = SSL_AD_ILLEGAL_PARAMETER;
  1094. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
  1095. goto f_err;
  1096. } else {
  1097. comp = ssl3_comp_find(s->ctx->comp_methods, compression);
  1098. }
  1099. if (compression != 0 && comp == NULL) {
  1100. al = SSL_AD_ILLEGAL_PARAMETER;
  1101. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
  1102. SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  1103. goto f_err;
  1104. } else {
  1105. s->s3->tmp.new_compression = comp;
  1106. }
  1107. #endif
  1108. /* TLS extensions */
  1109. if (!ssl_parse_serverhello_tlsext(s, pkt)) {
  1110. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
  1111. goto err;
  1112. }
  1113. if (PACKET_remaining(pkt) != 0) {
  1114. /* wrong packet length */
  1115. al = SSL_AD_DECODE_ERROR;
  1116. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
  1117. goto f_err;
  1118. }
  1119. #ifndef OPENSSL_NO_SCTP
  1120. if (SSL_IS_DTLS(s) && s->hit) {
  1121. unsigned char sctpauthkey[64];
  1122. char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
  1123. /*
  1124. * Add new shared key for SCTP-Auth, will be ignored if
  1125. * no SCTP used.
  1126. */
  1127. memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
  1128. sizeof(DTLS1_SCTP_AUTH_LABEL));
  1129. if (SSL_export_keying_material(s, sctpauthkey,
  1130. sizeof(sctpauthkey),
  1131. labelbuffer,
  1132. sizeof(labelbuffer), NULL, 0,
  1133. 0) <= 0)
  1134. goto err;
  1135. BIO_ctrl(SSL_get_wbio(s),
  1136. BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  1137. sizeof(sctpauthkey), sctpauthkey);
  1138. }
  1139. #endif
  1140. return MSG_PROCESS_CONTINUE_READING;
  1141. f_err:
  1142. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1143. err:
  1144. ossl_statem_set_error(s);
  1145. return MSG_PROCESS_ERROR;
  1146. }
  1147. MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt)
  1148. {
  1149. int al, i, ret = MSG_PROCESS_ERROR, exp_idx;
  1150. unsigned long cert_list_len, cert_len;
  1151. X509 *x = NULL;
  1152. unsigned char *certstart, *certbytes;
  1153. STACK_OF(X509) *sk = NULL;
  1154. EVP_PKEY *pkey = NULL;
  1155. if ((sk = sk_X509_new_null()) == NULL) {
  1156. SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  1157. goto err;
  1158. }
  1159. if (!PACKET_get_net_3(pkt, &cert_list_len)
  1160. || PACKET_remaining(pkt) != cert_list_len) {
  1161. al = SSL_AD_DECODE_ERROR;
  1162. SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
  1163. goto f_err;
  1164. }
  1165. while (PACKET_remaining(pkt)) {
  1166. if (!PACKET_get_net_3(pkt, &cert_len)
  1167. || !PACKET_get_bytes(pkt, &certbytes, cert_len)) {
  1168. al = SSL_AD_DECODE_ERROR;
  1169. SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
  1170. SSL_R_CERT_LENGTH_MISMATCH);
  1171. goto f_err;
  1172. }
  1173. certstart = certbytes;
  1174. x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len);
  1175. if (x == NULL) {
  1176. al = SSL_AD_BAD_CERTIFICATE;
  1177. SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
  1178. goto f_err;
  1179. }
  1180. if (certbytes != (certstart + cert_len)) {
  1181. al = SSL_AD_DECODE_ERROR;
  1182. SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
  1183. SSL_R_CERT_LENGTH_MISMATCH);
  1184. goto f_err;
  1185. }
  1186. if (!sk_X509_push(sk, x)) {
  1187. SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  1188. goto err;
  1189. }
  1190. x = NULL;
  1191. }
  1192. i = ssl_verify_cert_chain(s, sk);
  1193. if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
  1194. al = ssl_verify_alarm_type(s->verify_result);
  1195. SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
  1196. SSL_R_CERTIFICATE_VERIFY_FAILED);
  1197. goto f_err;
  1198. }
  1199. ERR_clear_error(); /* but we keep s->verify_result */
  1200. if (i > 1) {
  1201. SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i);
  1202. al = SSL_AD_HANDSHAKE_FAILURE;
  1203. goto f_err;
  1204. }
  1205. s->session->peer_chain = sk;
  1206. /*
  1207. * Inconsistency alert: cert_chain does include the peer's certificate,
  1208. * which we don't include in s3_srvr.c
  1209. */
  1210. x = sk_X509_value(sk, 0);
  1211. sk = NULL;
  1212. /*
  1213. * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
  1214. */
  1215. pkey = X509_get0_pubkey(x);
  1216. if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
  1217. x = NULL;
  1218. al = SSL3_AL_FATAL;
  1219. SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
  1220. SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
  1221. goto f_err;
  1222. }
  1223. i = ssl_cert_type(x, pkey);
  1224. if (i < 0) {
  1225. x = NULL;
  1226. al = SSL3_AL_FATAL;
  1227. SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
  1228. SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  1229. goto f_err;
  1230. }
  1231. exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
  1232. if (exp_idx >= 0 && i != exp_idx
  1233. && (exp_idx != SSL_PKEY_GOST_EC ||
  1234. (i != SSL_PKEY_GOST12_512 && i != SSL_PKEY_GOST12_256
  1235. && i != SSL_PKEY_GOST01))) {
  1236. x = NULL;
  1237. al = SSL_AD_ILLEGAL_PARAMETER;
  1238. SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
  1239. SSL_R_WRONG_CERTIFICATE_TYPE);
  1240. goto f_err;
  1241. }
  1242. s->session->peer_type = i;
  1243. X509_free(s->session->peer);
  1244. X509_up_ref(x);
  1245. s->session->peer = x;
  1246. s->session->verify_result = s->verify_result;
  1247. x = NULL;
  1248. ret = MSG_PROCESS_CONTINUE_READING;
  1249. goto done;
  1250. f_err:
  1251. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1252. err:
  1253. ossl_statem_set_error(s);
  1254. done:
  1255. X509_free(x);
  1256. sk_X509_pop_free(sk, X509_free);
  1257. return ret;
  1258. }
  1259. MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt)
  1260. {
  1261. EVP_MD_CTX *md_ctx;
  1262. int al, j;
  1263. long alg_k, alg_a;
  1264. EVP_PKEY *pkey = NULL;
  1265. const EVP_MD *md = NULL;
  1266. #ifndef OPENSSL_NO_RSA
  1267. RSA *rsa = NULL;
  1268. #endif
  1269. #ifndef OPENSSL_NO_EC
  1270. EVP_PKEY_CTX *pctx = NULL;
  1271. #endif
  1272. PACKET save_param_start, signature;
  1273. md_ctx = EVP_MD_CTX_new();
  1274. if (md_ctx == NULL) {
  1275. al = SSL_AD_INTERNAL_ERROR;
  1276. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  1277. goto f_err;
  1278. }
  1279. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1280. save_param_start = *pkt;
  1281. #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
  1282. EVP_PKEY_free(s->s3->peer_tmp);
  1283. s->s3->peer_tmp = NULL;
  1284. #endif
  1285. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1286. al = SSL_AD_DECODE_ERROR;
  1287. #ifndef OPENSSL_NO_PSK
  1288. /* PSK ciphersuites are preceded by an identity hint */
  1289. if (alg_k & SSL_PSK) {
  1290. PACKET psk_identity_hint;
  1291. if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
  1292. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
  1293. goto f_err;
  1294. }
  1295. /*
  1296. * Store PSK identity hint for later use, hint is used in
  1297. * ssl3_send_client_key_exchange. Assume that the maximum length of
  1298. * a PSK identity hint can be as long as the maximum length of a PSK
  1299. * identity.
  1300. */
  1301. if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
  1302. al = SSL_AD_HANDSHAKE_FAILURE;
  1303. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
  1304. goto f_err;
  1305. }
  1306. if (PACKET_remaining(&psk_identity_hint) == 0) {
  1307. OPENSSL_free(s->session->psk_identity_hint);
  1308. s->session->psk_identity_hint = NULL;
  1309. } else if (!PACKET_strndup(&psk_identity_hint,
  1310. &s->session->psk_identity_hint)) {
  1311. al = SSL_AD_INTERNAL_ERROR;
  1312. goto f_err;
  1313. }
  1314. }
  1315. /* Nothing else to do for plain PSK or RSAPSK */
  1316. if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
  1317. } else
  1318. #endif /* !OPENSSL_NO_PSK */
  1319. /*
  1320. * Dummy "if" to ensure sane C code in the event of various OPENSSL_NO_*
  1321. * options
  1322. */
  1323. if (0) {
  1324. }
  1325. #ifndef OPENSSL_NO_SRP
  1326. else if (alg_k & SSL_kSRP) {
  1327. PACKET prime, generator, salt, server_pub;
  1328. if (!PACKET_get_length_prefixed_2(pkt, &prime)
  1329. || !PACKET_get_length_prefixed_2(pkt, &generator)
  1330. || !PACKET_get_length_prefixed_1(pkt, &salt)
  1331. || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
  1332. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
  1333. goto f_err;
  1334. }
  1335. if ((s->srp_ctx.N =
  1336. BN_bin2bn(PACKET_data(&prime),
  1337. PACKET_remaining(&prime), NULL)) == NULL
  1338. || (s->srp_ctx.g =
  1339. BN_bin2bn(PACKET_data(&generator),
  1340. PACKET_remaining(&generator), NULL)) == NULL
  1341. || (s->srp_ctx.s =
  1342. BN_bin2bn(PACKET_data(&salt),
  1343. PACKET_remaining(&salt), NULL)) == NULL
  1344. || (s->srp_ctx.B =
  1345. BN_bin2bn(PACKET_data(&server_pub),
  1346. PACKET_remaining(&server_pub), NULL)) == NULL) {
  1347. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
  1348. goto err;
  1349. }
  1350. if (!srp_verify_server_param(s, &al)) {
  1351. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
  1352. goto f_err;
  1353. }
  1354. /* We must check if there is a certificate */
  1355. if (alg_a & (SSL_aRSA|SSL_aDSS))
  1356. pkey = X509_get0_pubkey(s->session->peer);
  1357. }
  1358. #endif /* !OPENSSL_NO_SRP */
  1359. #ifndef OPENSSL_NO_DH
  1360. else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
  1361. PACKET prime, generator, pub_key;
  1362. DH *dh;
  1363. if (!PACKET_get_length_prefixed_2(pkt, &prime)
  1364. || !PACKET_get_length_prefixed_2(pkt, &generator)
  1365. || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
  1366. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
  1367. goto f_err;
  1368. }
  1369. s->s3->peer_tmp = EVP_PKEY_new();
  1370. dh = DH_new();
  1371. if (s->s3->peer_tmp == NULL || dh == NULL) {
  1372. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  1373. DH_free(dh);
  1374. goto err;
  1375. }
  1376. if (EVP_PKEY_assign_DH(s->s3->peer_tmp, dh) == 0) {
  1377. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
  1378. DH_free(dh);
  1379. goto err;
  1380. }
  1381. if ((dh->p = BN_bin2bn(PACKET_data(&prime),
  1382. PACKET_remaining(&prime), NULL)) == NULL
  1383. || (dh->g = BN_bin2bn(PACKET_data(&generator),
  1384. PACKET_remaining(&generator), NULL)) == NULL
  1385. || (dh->pub_key =
  1386. BN_bin2bn(PACKET_data(&pub_key),
  1387. PACKET_remaining(&pub_key), NULL)) == NULL) {
  1388. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
  1389. goto err;
  1390. }
  1391. if (BN_is_zero(dh->p) || BN_is_zero(dh->g) || BN_is_zero(dh->pub_key)) {
  1392. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_DH_VALUE);
  1393. goto f_err;
  1394. }
  1395. if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
  1396. al = SSL_AD_HANDSHAKE_FAILURE;
  1397. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
  1398. goto f_err;
  1399. }
  1400. if (alg_a & (SSL_aRSA|SSL_aDSS))
  1401. pkey = X509_get0_pubkey(s->session->peer);
  1402. /* else anonymous DH, so no certificate or pkey. */
  1403. }
  1404. #endif /* !OPENSSL_NO_DH */
  1405. #ifndef OPENSSL_NO_EC
  1406. else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
  1407. PACKET encoded_pt;
  1408. unsigned char *ecparams;
  1409. int curve_nid;
  1410. /*
  1411. * Extract elliptic curve parameters and the server's ephemeral ECDH
  1412. * public key. For now we only support named (not generic) curves and
  1413. * ECParameters in this case is just three bytes.
  1414. */
  1415. if (!PACKET_get_bytes(pkt, &ecparams, 3)) {
  1416. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1417. goto f_err;
  1418. }
  1419. /*
  1420. * Check curve is one of our preferences, if not server has sent an
  1421. * invalid curve. ECParameters is 3 bytes.
  1422. */
  1423. if (!tls1_check_curve(s, ecparams, 3)) {
  1424. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
  1425. goto f_err;
  1426. }
  1427. curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2));
  1428. if (curve_nid == 0) {
  1429. al = SSL_AD_INTERNAL_ERROR;
  1430. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
  1431. SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
  1432. goto f_err;
  1433. }
  1434. /* Set up EVP_PKEY with named curve as parameters */
  1435. pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
  1436. if (pctx == NULL
  1437. || EVP_PKEY_paramgen_init(pctx) <= 0
  1438. || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, curve_nid) <= 0
  1439. || EVP_PKEY_paramgen(pctx, &s->s3->peer_tmp) <= 0) {
  1440. al = SSL_AD_INTERNAL_ERROR;
  1441. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
  1442. goto f_err;
  1443. }
  1444. EVP_PKEY_CTX_free(pctx);
  1445. pctx = NULL;
  1446. if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
  1447. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
  1448. goto f_err;
  1449. }
  1450. if (EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(s->s3->peer_tmp),
  1451. PACKET_data(&encoded_pt),
  1452. PACKET_remaining(&encoded_pt), NULL) == 0) {
  1453. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
  1454. goto f_err;
  1455. }
  1456. /*
  1457. * The ECC/TLS specification does not mention the use of DSA to sign
  1458. * ECParameters in the server key exchange message. We do support RSA
  1459. * and ECDSA.
  1460. */
  1461. if (0) ;
  1462. # ifndef OPENSSL_NO_RSA
  1463. else if (alg_a & SSL_aRSA)
  1464. pkey = X509_get0_pubkey(s->session->peer);
  1465. # endif
  1466. # ifndef OPENSSL_NO_EC
  1467. else if (alg_a & SSL_aECDSA)
  1468. pkey = X509_get0_pubkey(s->session->peer);
  1469. # endif
  1470. /* else anonymous ECDH, so no certificate or pkey. */
  1471. } else if (alg_k) {
  1472. al = SSL_AD_UNEXPECTED_MESSAGE;
  1473. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
  1474. goto f_err;
  1475. }
  1476. #endif /* !OPENSSL_NO_EC */
  1477. /* if it was signed, check the signature */
  1478. if (pkey != NULL) {
  1479. PACKET params;
  1480. /*
  1481. * |pkt| now points to the beginning of the signature, so the difference
  1482. * equals the length of the parameters.
  1483. */
  1484. if (!PACKET_get_sub_packet(&save_param_start, &params,
  1485. PACKET_remaining(&save_param_start) -
  1486. PACKET_remaining(pkt))) {
  1487. al = SSL_AD_INTERNAL_ERROR;
  1488. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1489. goto f_err;
  1490. }
  1491. if (SSL_USE_SIGALGS(s)) {
  1492. unsigned char *sigalgs;
  1493. int rv;
  1494. if (!PACKET_get_bytes(pkt, &sigalgs, 2)) {
  1495. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1496. goto f_err;
  1497. }
  1498. rv = tls12_check_peer_sigalg(&md, s, sigalgs, pkey);
  1499. if (rv == -1)
  1500. goto err;
  1501. else if (rv == 0) {
  1502. goto f_err;
  1503. }
  1504. #ifdef SSL_DEBUG
  1505. fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
  1506. #endif
  1507. } else if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) {
  1508. md = EVP_md5_sha1();
  1509. } else {
  1510. md = EVP_sha1();
  1511. }
  1512. if (!PACKET_get_length_prefixed_2(pkt, &signature)
  1513. || PACKET_remaining(pkt) != 0) {
  1514. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
  1515. goto f_err;
  1516. }
  1517. j = EVP_PKEY_size(pkey);
  1518. if (j < 0) {
  1519. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1520. goto f_err;
  1521. }
  1522. /*
  1523. * Check signature length
  1524. */
  1525. if (PACKET_remaining(&signature) > (size_t)j) {
  1526. /* wrong packet length */
  1527. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
  1528. goto f_err;
  1529. }
  1530. if (EVP_VerifyInit_ex(md_ctx, md, NULL) <= 0
  1531. || EVP_VerifyUpdate(md_ctx, &(s->s3->client_random[0]),
  1532. SSL3_RANDOM_SIZE) <= 0
  1533. || EVP_VerifyUpdate(md_ctx, &(s->s3->server_random[0]),
  1534. SSL3_RANDOM_SIZE) <= 0
  1535. || EVP_VerifyUpdate(md_ctx, PACKET_data(&params),
  1536. PACKET_remaining(&params)) <= 0) {
  1537. al = SSL_AD_INTERNAL_ERROR;
  1538. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
  1539. goto f_err;
  1540. }
  1541. if (EVP_VerifyFinal(md_ctx, PACKET_data(&signature),
  1542. PACKET_remaining(&signature), pkey) <= 0) {
  1543. /* bad signature */
  1544. al = SSL_AD_DECRYPT_ERROR;
  1545. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
  1546. goto f_err;
  1547. }
  1548. } else {
  1549. /* aNULL, aSRP or PSK do not need public keys */
  1550. if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_PSK)) {
  1551. /* Might be wrong key type, check it */
  1552. if (ssl3_check_cert_and_algorithm(s))
  1553. /* Otherwise this shouldn't happen */
  1554. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1555. goto err;
  1556. }
  1557. /* still data left over */
  1558. if (PACKET_remaining(pkt) != 0) {
  1559. SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
  1560. goto f_err;
  1561. }
  1562. }
  1563. EVP_MD_CTX_free(md_ctx);
  1564. return MSG_PROCESS_CONTINUE_READING;
  1565. f_err:
  1566. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1567. err:
  1568. #ifndef OPENSSL_NO_RSA
  1569. RSA_free(rsa);
  1570. #endif
  1571. #ifndef OPENSSL_NO_EC
  1572. EVP_PKEY_CTX_free(pctx);
  1573. #endif
  1574. EVP_MD_CTX_free(md_ctx);
  1575. ossl_statem_set_error(s);
  1576. return MSG_PROCESS_ERROR;
  1577. }
  1578. MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt)
  1579. {
  1580. int ret = MSG_PROCESS_ERROR;
  1581. unsigned int list_len, ctype_num, i, name_len;
  1582. X509_NAME *xn = NULL;
  1583. unsigned char *data;
  1584. unsigned char *namestart, *namebytes;
  1585. STACK_OF(X509_NAME) *ca_sk = NULL;
  1586. if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
  1587. SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
  1588. goto err;
  1589. }
  1590. /* get the certificate types */
  1591. if (!PACKET_get_1(pkt, &ctype_num)
  1592. || !PACKET_get_bytes(pkt, &data, ctype_num)) {
  1593. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1594. SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
  1595. goto err;
  1596. }
  1597. OPENSSL_free(s->cert->ctypes);
  1598. s->cert->ctypes = NULL;
  1599. if (ctype_num > SSL3_CT_NUMBER) {
  1600. /* If we exceed static buffer copy all to cert structure */
  1601. s->cert->ctypes = OPENSSL_malloc(ctype_num);
  1602. if (s->cert->ctypes == NULL) {
  1603. SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
  1604. goto err;
  1605. }
  1606. memcpy(s->cert->ctypes, data, ctype_num);
  1607. s->cert->ctype_num = (size_t)ctype_num;
  1608. ctype_num = SSL3_CT_NUMBER;
  1609. }
  1610. for (i = 0; i < ctype_num; i++)
  1611. s->s3->tmp.ctype[i] = data[i];
  1612. if (SSL_USE_SIGALGS(s)) {
  1613. if (!PACKET_get_net_2(pkt, &list_len)
  1614. || !PACKET_get_bytes(pkt, &data, list_len)) {
  1615. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1616. SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  1617. SSL_R_LENGTH_MISMATCH);
  1618. goto err;
  1619. }
  1620. /* Clear certificate digests and validity flags */
  1621. for (i = 0; i < SSL_PKEY_NUM; i++) {
  1622. s->s3->tmp.md[i] = NULL;
  1623. s->s3->tmp.valid_flags[i] = 0;
  1624. }
  1625. if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) {
  1626. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1627. SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  1628. SSL_R_SIGNATURE_ALGORITHMS_ERROR);
  1629. goto err;
  1630. }
  1631. if (!tls1_process_sigalgs(s)) {
  1632. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1633. SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
  1634. goto err;
  1635. }
  1636. } else {
  1637. ssl_set_default_md(s);
  1638. }
  1639. /* get the CA RDNs */
  1640. if (!PACKET_get_net_2(pkt, &list_len)
  1641. || PACKET_remaining(pkt) != list_len) {
  1642. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1643. SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
  1644. goto err;
  1645. }
  1646. while (PACKET_remaining(pkt)) {
  1647. if (!PACKET_get_net_2(pkt, &name_len)
  1648. || !PACKET_get_bytes(pkt, &namebytes, name_len)) {
  1649. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1650. SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  1651. SSL_R_LENGTH_MISMATCH);
  1652. goto err;
  1653. }
  1654. namestart = namebytes;
  1655. if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes,
  1656. name_len)) == NULL) {
  1657. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1658. SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
  1659. goto err;
  1660. }
  1661. if (namebytes != (namestart + name_len)) {
  1662. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1663. SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  1664. SSL_R_CA_DN_LENGTH_MISMATCH);
  1665. goto err;
  1666. }
  1667. if (!sk_X509_NAME_push(ca_sk, xn)) {
  1668. SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
  1669. goto err;
  1670. }
  1671. }
  1672. /* we should setup a certificate to return.... */
  1673. s->s3->tmp.cert_req = 1;
  1674. s->s3->tmp.ctype_num = ctype_num;
  1675. sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
  1676. s->s3->tmp.ca_names = ca_sk;
  1677. ca_sk = NULL;
  1678. ret = MSG_PROCESS_CONTINUE_READING;
  1679. goto done;
  1680. err:
  1681. ossl_statem_set_error(s);
  1682. done:
  1683. sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
  1684. return ret;
  1685. }
  1686. static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
  1687. {
  1688. return (X509_NAME_cmp(*a, *b));
  1689. }
  1690. MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt)
  1691. {
  1692. int al;
  1693. unsigned int ticklen;
  1694. unsigned long ticket_lifetime_hint;
  1695. if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
  1696. || !PACKET_get_net_2(pkt, &ticklen)
  1697. || PACKET_remaining(pkt) != ticklen) {
  1698. al = SSL_AD_DECODE_ERROR;
  1699. SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
  1700. goto f_err;
  1701. }
  1702. /* Server is allowed to change its mind and send an empty ticket. */
  1703. if (ticklen == 0)
  1704. return MSG_PROCESS_CONTINUE_READING;
  1705. if (s->session->session_id_length > 0) {
  1706. int i = s->session_ctx->session_cache_mode;
  1707. SSL_SESSION *new_sess;
  1708. /*
  1709. * We reused an existing session, so we need to replace it with a new
  1710. * one
  1711. */
  1712. if (i & SSL_SESS_CACHE_CLIENT) {
  1713. /*
  1714. * Remove the old session from the cache
  1715. */
  1716. if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
  1717. if (s->session_ctx->remove_session_cb != NULL)
  1718. s->session_ctx->remove_session_cb(s->session_ctx,
  1719. s->session);
  1720. } else {
  1721. /* We carry on if this fails */
  1722. SSL_CTX_remove_session(s->session_ctx, s->session);
  1723. }
  1724. }
  1725. if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
  1726. al = SSL_AD_INTERNAL_ERROR;
  1727. SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
  1728. goto f_err;
  1729. }
  1730. SSL_SESSION_free(s->session);
  1731. s->session = new_sess;
  1732. }
  1733. OPENSSL_free(s->session->tlsext_tick);
  1734. s->session->tlsext_ticklen = 0;
  1735. s->session->tlsext_tick = OPENSSL_malloc(ticklen);
  1736. if (s->session->tlsext_tick == NULL) {
  1737. SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
  1738. goto err;
  1739. }
  1740. if (!PACKET_copy_bytes(pkt, s->session->tlsext_tick, ticklen)) {
  1741. al = SSL_AD_DECODE_ERROR;
  1742. SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
  1743. goto f_err;
  1744. }
  1745. s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
  1746. s->session->tlsext_ticklen = ticklen;
  1747. /*
  1748. * There are two ways to detect a resumed ticket session. One is to set
  1749. * an appropriate session ID and then the server must return a match in
  1750. * ServerHello. This allows the normal client session ID matching to work
  1751. * and we know much earlier that the ticket has been accepted. The
  1752. * other way is to set zero length session ID when the ticket is
  1753. * presented and rely on the handshake to determine session resumption.
  1754. * We choose the former approach because this fits in with assumptions
  1755. * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
  1756. * SHA256 is disabled) hash of the ticket.
  1757. */
  1758. EVP_Digest(s->session->tlsext_tick, ticklen,
  1759. s->session->session_id, &s->session->session_id_length,
  1760. EVP_sha256(), NULL);
  1761. return MSG_PROCESS_CONTINUE_READING;
  1762. f_err:
  1763. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1764. err:
  1765. ossl_statem_set_error(s);
  1766. return MSG_PROCESS_ERROR;
  1767. }
  1768. MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt)
  1769. {
  1770. int al;
  1771. unsigned long resplen;
  1772. unsigned int type;
  1773. if (!PACKET_get_1(pkt, &type)
  1774. || type != TLSEXT_STATUSTYPE_ocsp) {
  1775. al = SSL_AD_DECODE_ERROR;
  1776. SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
  1777. goto f_err;
  1778. }
  1779. if (!PACKET_get_net_3(pkt, &resplen)
  1780. || PACKET_remaining(pkt) != resplen) {
  1781. al = SSL_AD_DECODE_ERROR;
  1782. SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
  1783. goto f_err;
  1784. }
  1785. s->tlsext_ocsp_resp = OPENSSL_malloc(resplen);
  1786. if (s->tlsext_ocsp_resp == NULL) {
  1787. al = SSL_AD_INTERNAL_ERROR;
  1788. SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
  1789. goto f_err;
  1790. }
  1791. if (!PACKET_copy_bytes(pkt, s->tlsext_ocsp_resp, resplen)) {
  1792. al = SSL_AD_DECODE_ERROR;
  1793. SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
  1794. goto f_err;
  1795. }
  1796. s->tlsext_ocsp_resplen = resplen;
  1797. return MSG_PROCESS_CONTINUE_READING;
  1798. f_err:
  1799. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1800. ossl_statem_set_error(s);
  1801. return MSG_PROCESS_ERROR;
  1802. }
  1803. MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt)
  1804. {
  1805. if (PACKET_remaining(pkt) > 0) {
  1806. /* should contain no data */
  1807. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1808. SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
  1809. ossl_statem_set_error(s);
  1810. return MSG_PROCESS_ERROR;
  1811. }
  1812. #ifndef OPENSSL_NO_SRP
  1813. if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
  1814. if (SRP_Calc_A_param(s) <= 0) {
  1815. SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC);
  1816. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1817. ossl_statem_set_error(s);
  1818. return MSG_PROCESS_ERROR;
  1819. }
  1820. }
  1821. #endif
  1822. /*
  1823. * at this point we check that we have the required stuff from
  1824. * the server
  1825. */
  1826. if (!ssl3_check_cert_and_algorithm(s)) {
  1827. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1828. ossl_statem_set_error(s);
  1829. return MSG_PROCESS_ERROR;
  1830. }
  1831. /*
  1832. * Call the ocsp status callback if needed. The |tlsext_ocsp_resp| and
  1833. * |tlsext_ocsp_resplen| values will be set if we actually received a status
  1834. * message, or NULL and -1 otherwise
  1835. */
  1836. if (s->tlsext_status_type != -1 && s->ctx->tlsext_status_cb != NULL) {
  1837. int ret;
  1838. ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
  1839. if (ret == 0) {
  1840. ssl3_send_alert(s, SSL3_AL_FATAL,
  1841. SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE);
  1842. SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE,
  1843. SSL_R_INVALID_STATUS_RESPONSE);
  1844. return MSG_PROCESS_ERROR;
  1845. }
  1846. if (ret < 0) {
  1847. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1848. SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, ERR_R_MALLOC_FAILURE);
  1849. return MSG_PROCESS_ERROR;
  1850. }
  1851. }
  1852. #ifndef OPENSSL_NO_SCTP
  1853. /* Only applies to renegotiation */
  1854. if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))
  1855. && s->renegotiate != 0)
  1856. return MSG_PROCESS_CONTINUE_PROCESSING;
  1857. else
  1858. #endif
  1859. return MSG_PROCESS_FINISHED_READING;
  1860. }
  1861. int tls_construct_client_key_exchange(SSL *s)
  1862. {
  1863. unsigned char *p;
  1864. int n;
  1865. #ifndef OPENSSL_NO_PSK
  1866. size_t pskhdrlen = 0;
  1867. #endif
  1868. unsigned long alg_k;
  1869. #ifndef OPENSSL_NO_RSA
  1870. unsigned char *q;
  1871. EVP_PKEY *pkey = NULL;
  1872. EVP_PKEY_CTX *pctx = NULL;
  1873. #endif
  1874. #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
  1875. EVP_PKEY *ckey = NULL, *skey = NULL;
  1876. #endif
  1877. #ifndef OPENSSL_NO_EC
  1878. unsigned char *encodedPoint = NULL;
  1879. int encoded_pt_len = 0;
  1880. #endif
  1881. unsigned char *pms = NULL;
  1882. size_t pmslen = 0;
  1883. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1884. p = ssl_handshake_start(s);
  1885. #ifndef OPENSSL_NO_PSK
  1886. if (alg_k & SSL_PSK) {
  1887. int psk_err = 1;
  1888. /*
  1889. * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
  1890. * \0-terminated identity. The last byte is for us for simulating
  1891. * strnlen.
  1892. */
  1893. char identity[PSK_MAX_IDENTITY_LEN + 1];
  1894. size_t identitylen;
  1895. unsigned char psk[PSK_MAX_PSK_LEN];
  1896. size_t psklen;
  1897. if (s->psk_client_callback == NULL) {
  1898. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
  1899. SSL_R_PSK_NO_CLIENT_CB);
  1900. goto err;
  1901. }
  1902. memset(identity, 0, sizeof(identity));
  1903. psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
  1904. identity, sizeof(identity) - 1,
  1905. psk, sizeof(psk));
  1906. if (psklen > PSK_MAX_PSK_LEN) {
  1907. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
  1908. ERR_R_INTERNAL_ERROR);
  1909. goto psk_err;
  1910. } else if (psklen == 0) {
  1911. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
  1912. SSL_R_PSK_IDENTITY_NOT_FOUND);
  1913. goto psk_err;
  1914. }
  1915. OPENSSL_free(s->s3->tmp.psk);
  1916. s->s3->tmp.psk = OPENSSL_memdup(psk, psklen);
  1917. OPENSSL_cleanse(psk, psklen);
  1918. if (s->s3->tmp.psk == NULL) {
  1919. OPENSSL_cleanse(identity, sizeof(identity));
  1920. goto memerr;
  1921. }
  1922. s->s3->tmp.psklen = psklen;
  1923. identitylen = strlen(identity);
  1924. if (identitylen > PSK_MAX_IDENTITY_LEN) {
  1925. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
  1926. ERR_R_INTERNAL_ERROR);
  1927. goto psk_err;
  1928. }
  1929. OPENSSL_free(s->session->psk_identity);
  1930. s->session->psk_identity = OPENSSL_strdup(identity);
  1931. if (s->session->psk_identity == NULL) {
  1932. OPENSSL_cleanse(identity, sizeof(identity));
  1933. goto memerr;
  1934. }
  1935. s2n(identitylen, p);
  1936. memcpy(p, identity, identitylen);
  1937. pskhdrlen = 2 + identitylen;
  1938. p += identitylen;
  1939. psk_err = 0;
  1940. psk_err:
  1941. OPENSSL_cleanse(identity, sizeof(identity));
  1942. if (psk_err != 0) {
  1943. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1944. goto err;
  1945. }
  1946. }
  1947. if (alg_k & SSL_kPSK) {
  1948. n = 0;
  1949. } else
  1950. #endif
  1951. /* Fool emacs indentation */
  1952. if (0) {
  1953. }
  1954. #ifndef OPENSSL_NO_RSA
  1955. else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
  1956. size_t enclen;
  1957. pmslen = SSL_MAX_MASTER_KEY_LENGTH;
  1958. pms = OPENSSL_malloc(pmslen);
  1959. if (pms == NULL)
  1960. goto memerr;
  1961. if (s->session->peer == NULL) {
  1962. /*
  1963. * We should always have a server certificate with SSL_kRSA.
  1964. */
  1965. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
  1966. ERR_R_INTERNAL_ERROR);
  1967. goto err;
  1968. }
  1969. pkey = X509_get0_pubkey(s->session->peer);
  1970. if (EVP_PKEY_get0_RSA(pkey) == NULL) {
  1971. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
  1972. ERR_R_INTERNAL_ERROR);
  1973. goto err;
  1974. }
  1975. pms[0] = s->client_version >> 8;
  1976. pms[1] = s->client_version & 0xff;
  1977. if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
  1978. goto err;
  1979. q = p;
  1980. /* Fix buf for TLS and beyond */
  1981. if (s->version > SSL3_VERSION)
  1982. p += 2;
  1983. pctx = EVP_PKEY_CTX_new(pkey, NULL);
  1984. if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0
  1985. || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {
  1986. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
  1987. ERR_R_EVP_LIB);
  1988. goto err;
  1989. }
  1990. if (EVP_PKEY_encrypt(pctx, p, &enclen, pms, pmslen) <= 0) {
  1991. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
  1992. SSL_R_BAD_RSA_ENCRYPT);
  1993. goto err;
  1994. }
  1995. n = enclen;
  1996. EVP_PKEY_CTX_free(pctx);
  1997. pctx = NULL;
  1998. # ifdef PKCS1_CHECK
  1999. if (s->options & SSL_OP_PKCS1_CHECK_1)
  2000. p[1]++;
  2001. if (s->options & SSL_OP_PKCS1_CHECK_2)
  2002. tmp_buf[0] = 0x70;
  2003. # endif
  2004. /* Fix buf for TLS and beyond */
  2005. if (s->version > SSL3_VERSION) {
  2006. s2n(n, q);
  2007. n += 2;
  2008. }
  2009. }
  2010. #endif
  2011. #ifndef OPENSSL_NO_DH
  2012. else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
  2013. DH *dh_clnt = NULL;
  2014. skey = s->s3->peer_tmp;
  2015. if (skey == NULL) {
  2016. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
  2017. ERR_R_INTERNAL_ERROR);
  2018. goto err;
  2019. }
  2020. ckey = ssl_generate_pkey(skey, NID_undef);
  2021. dh_clnt = EVP_PKEY_get0_DH(ckey);
  2022. if (dh_clnt == NULL || ssl_derive(s, ckey, skey) == 0) {
  2023. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
  2024. ERR_R_INTERNAL_ERROR);
  2025. goto err;
  2026. }
  2027. /* send off the data */
  2028. n = BN_num_bytes(dh_clnt->pub_key);
  2029. s2n(n, p);
  2030. BN_bn2bin(dh_clnt->pub_key, p);
  2031. n += 2;
  2032. EVP_PKEY_free(ckey);
  2033. ckey = NULL;
  2034. }
  2035. #endif
  2036. #ifndef OPENSSL_NO_EC
  2037. else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe | SSL_kECDHEPSK)) {
  2038. if (s->s3->peer_tmp != NULL) {
  2039. skey = s->s3->peer_tmp;
  2040. } else {
  2041. /* Get the Server Public Key from Cert */
  2042. skey = X509_get0_pubkey(s->session->peer);
  2043. if ((skey == NULL) || EVP_PKEY_get0_EC_KEY(skey) == NULL) {
  2044. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
  2045. ERR_R_INTERNAL_ERROR);
  2046. goto err;
  2047. }
  2048. }
  2049. ckey = ssl_generate_pkey(skey, NID_undef);
  2050. if (ssl_derive(s, ckey, skey) == 0) {
  2051. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EVP_LIB);
  2052. goto err;
  2053. }
  2054. /* Generate encoding of client key */
  2055. encoded_pt_len = EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(ckey),
  2056. POINT_CONVERSION_UNCOMPRESSED,
  2057. &encodedPoint, NULL);
  2058. if (encoded_pt_len == 0) {
  2059. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
  2060. goto err;
  2061. }
  2062. EVP_PKEY_free(ckey);
  2063. ckey = NULL;
  2064. n = encoded_pt_len;
  2065. *p = n; /* length of encoded point */
  2066. /* Encoded point will be copied here */
  2067. p += 1;
  2068. /* copy the point */
  2069. memcpy(p, encodedPoint, n);
  2070. /* increment n to account for length field */
  2071. n += 1;
  2072. /* Free allocated memory */
  2073. OPENSSL_free(encodedPoint);
  2074. }
  2075. #endif /* !OPENSSL_NO_EC */
  2076. #ifndef OPENSSL_NO_GOST
  2077. else if (alg_k & SSL_kGOST) {
  2078. /* GOST key exchange message creation */
  2079. EVP_PKEY_CTX *pkey_ctx;
  2080. X509 *peer_cert;
  2081. size_t msglen;
  2082. unsigned int md_len;
  2083. unsigned char shared_ukm[32], tmp[256];
  2084. EVP_MD_CTX *ukm_hash;
  2085. int dgst_nid = NID_id_GostR3411_94;
  2086. if ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0)
  2087. dgst_nid = NID_id_GostR3411_2012_256;
  2088. pmslen = 32;
  2089. pms = OPENSSL_malloc(pmslen);
  2090. if (pms == NULL)
  2091. goto memerr;
  2092. /*
  2093. * Get server sertificate PKEY and create ctx from it
  2094. */
  2095. peer_cert = s->session->peer;
  2096. if (!peer_cert) {
  2097. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
  2098. SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
  2099. goto err;
  2100. }
  2101. pkey_ctx = EVP_PKEY_CTX_new(X509_get0_pubkey(peer_cert), NULL);
  2102. if (pkey_ctx == NULL) {
  2103. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
  2104. ERR_R_MALLOC_FAILURE);
  2105. goto err;
  2106. }
  2107. /*
  2108. * If we have send a certificate, and certificate key
  2109. * parameters match those of server certificate, use
  2110. * certificate key for key exchange
  2111. */
  2112. /* Otherwise, generate ephemeral key pair */
  2113. if (pkey_ctx == NULL
  2114. || EVP_PKEY_encrypt_init(pkey_ctx) <= 0
  2115. /* Generate session key */
  2116. || RAND_bytes(pms, pmslen) <= 0) {
  2117. EVP_PKEY_CTX_free(pkey_ctx);
  2118. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
  2119. ERR_R_INTERNAL_ERROR);
  2120. goto err;
  2121. };
  2122. /*
  2123. * If we have client certificate, use its secret as peer key
  2124. */
  2125. if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
  2126. if (EVP_PKEY_derive_set_peer
  2127. (pkey_ctx, s->cert->key->privatekey) <= 0) {
  2128. /*
  2129. * If there was an error - just ignore it. Ephemeral key
  2130. * * would be used
  2131. */
  2132. ERR_clear_error();
  2133. }
  2134. }
  2135. /*
  2136. * Compute shared IV and store it in algorithm-specific context
  2137. * data
  2138. */
  2139. ukm_hash = EVP_MD_CTX_new();
  2140. if (EVP_DigestInit(ukm_hash,
  2141. EVP_get_digestbynid(dgst_nid)) <= 0
  2142. || EVP_DigestUpdate(ukm_hash, s->s3->client_random,
  2143. SSL3_RANDOM_SIZE) <= 0
  2144. || EVP_DigestUpdate(ukm_hash, s->s3->server_random,
  2145. SSL3_RANDOM_SIZE) <= 0
  2146. || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
  2147. EVP_MD_CTX_free(ukm_hash);
  2148. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
  2149. ERR_R_INTERNAL_ERROR);
  2150. goto err;
  2151. }
  2152. EVP_MD_CTX_free(ukm_hash);
  2153. if (EVP_PKEY_CTX_ctrl
  2154. (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
  2155. shared_ukm) < 0) {
  2156. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
  2157. SSL_R_LIBRARY_BUG);
  2158. goto err;
  2159. }
  2160. /* Make GOST keytransport blob message */
  2161. /*
  2162. * Encapsulate it into sequence
  2163. */
  2164. *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
  2165. msglen = 255;
  2166. if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {
  2167. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
  2168. SSL_R_LIBRARY_BUG);
  2169. goto err;
  2170. }
  2171. if (msglen >= 0x80) {
  2172. *(p++) = 0x81;
  2173. *(p++) = msglen & 0xff;
  2174. n = msglen + 3;
  2175. } else {
  2176. *(p++) = msglen & 0xff;
  2177. n = msglen + 2;
  2178. }
  2179. memcpy(p, tmp, msglen);
  2180. /* Check if pubkey from client certificate was used */
  2181. if (EVP_PKEY_CTX_ctrl
  2182. (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
  2183. /* Set flag "skip certificate verify" */
  2184. s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
  2185. }
  2186. EVP_PKEY_CTX_free(pkey_ctx);
  2187. }
  2188. #endif
  2189. #ifndef OPENSSL_NO_SRP
  2190. else if (alg_k & SSL_kSRP) {
  2191. if (s->srp_ctx.A != NULL) {
  2192. /* send off the data */
  2193. n = BN_num_bytes(s->srp_ctx.A);
  2194. s2n(n, p);
  2195. BN_bn2bin(s->srp_ctx.A, p);
  2196. n += 2;
  2197. } else {
  2198. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
  2199. ERR_R_INTERNAL_ERROR);
  2200. goto err;
  2201. }
  2202. OPENSSL_free(s->session->srp_username);
  2203. s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
  2204. if (s->session->srp_username == NULL) {
  2205. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
  2206. ERR_R_MALLOC_FAILURE);
  2207. goto err;
  2208. }
  2209. }
  2210. #endif
  2211. else {
  2212. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  2213. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  2214. goto err;
  2215. }
  2216. #ifndef OPENSSL_NO_PSK
  2217. n += pskhdrlen;
  2218. #endif
  2219. if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
  2220. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  2221. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  2222. goto err;
  2223. }
  2224. if (pms != NULL) {
  2225. s->s3->tmp.pms = pms;
  2226. s->s3->tmp.pmslen = pmslen;
  2227. }
  2228. return 1;
  2229. memerr:
  2230. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  2231. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  2232. err:
  2233. OPENSSL_clear_free(pms, pmslen);
  2234. s->s3->tmp.pms = NULL;
  2235. #ifndef OPENSSL_NO_RSA
  2236. EVP_PKEY_CTX_free(pctx);
  2237. #endif
  2238. #ifndef OPENSSL_NO_EC
  2239. OPENSSL_free(encodedPoint);
  2240. #endif
  2241. #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
  2242. EVP_PKEY_free(ckey);
  2243. #endif
  2244. #ifndef OPENSSL_NO_PSK
  2245. OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
  2246. s->s3->tmp.psk = NULL;
  2247. #endif
  2248. ossl_statem_set_error(s);
  2249. return 0;
  2250. }
  2251. int tls_client_key_exchange_post_work(SSL *s)
  2252. {
  2253. unsigned char *pms = NULL;
  2254. size_t pmslen = 0;
  2255. #ifndef OPENSSL_NO_SRP
  2256. /* Check for SRP */
  2257. if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
  2258. if (!srp_generate_client_master_secret(s)) {
  2259. SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK,
  2260. ERR_R_INTERNAL_ERROR);
  2261. goto err;
  2262. }
  2263. return 1;
  2264. }
  2265. #endif
  2266. pms = s->s3->tmp.pms;
  2267. pmslen = s->s3->tmp.pmslen;
  2268. if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
  2269. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  2270. SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE);
  2271. goto err;
  2272. }
  2273. if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
  2274. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  2275. SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR);
  2276. goto err;
  2277. }
  2278. #ifndef OPENSSL_NO_SCTP
  2279. if (SSL_IS_DTLS(s)) {
  2280. unsigned char sctpauthkey[64];
  2281. char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
  2282. /*
  2283. * Add new shared key for SCTP-Auth, will be ignored if no SCTP
  2284. * used.
  2285. */
  2286. memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
  2287. sizeof(DTLS1_SCTP_AUTH_LABEL));
  2288. if (SSL_export_keying_material(s, sctpauthkey,
  2289. sizeof(sctpauthkey), labelbuffer,
  2290. sizeof(labelbuffer), NULL, 0, 0) <= 0)
  2291. goto err;
  2292. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  2293. sizeof(sctpauthkey), sctpauthkey);
  2294. }
  2295. #endif
  2296. return 1;
  2297. err:
  2298. OPENSSL_clear_free(pms, pmslen);
  2299. s->s3->tmp.pms = NULL;
  2300. return 0;
  2301. }
  2302. int tls_construct_client_verify(SSL *s)
  2303. {
  2304. unsigned char *p;
  2305. EVP_PKEY *pkey;
  2306. const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
  2307. EVP_MD_CTX *mctx;
  2308. unsigned u = 0;
  2309. unsigned long n = 0;
  2310. long hdatalen = 0;
  2311. void *hdata;
  2312. mctx = EVP_MD_CTX_new();
  2313. if (mctx == NULL) {
  2314. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_MALLOC_FAILURE);
  2315. goto err;
  2316. }
  2317. p = ssl_handshake_start(s);
  2318. pkey = s->cert->key->privatekey;
  2319. hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
  2320. if (hdatalen <= 0) {
  2321. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
  2322. goto err;
  2323. }
  2324. if (SSL_USE_SIGALGS(s)) {
  2325. if (!tls12_get_sigandhash(p, pkey, md)) {
  2326. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
  2327. goto err;
  2328. }
  2329. p += 2;
  2330. n = 2;
  2331. }
  2332. #ifdef SSL_DEBUG
  2333. fprintf(stderr, "Using client alg %s\n", EVP_MD_name(md));
  2334. #endif
  2335. if (!EVP_SignInit_ex(mctx, md, NULL)
  2336. || !EVP_SignUpdate(mctx, hdata, hdatalen)
  2337. || (s->version == SSL3_VERSION
  2338. && !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
  2339. s->session->master_key_length,
  2340. s->session->master_key))
  2341. || !EVP_SignFinal(mctx, p + 2, &u, pkey)) {
  2342. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
  2343. goto err;
  2344. }
  2345. #ifndef OPENSSL_NO_GOST
  2346. {
  2347. int pktype = EVP_PKEY_id(pkey);
  2348. if (pktype == NID_id_GostR3410_2001
  2349. || pktype == NID_id_GostR3410_2012_256
  2350. || pktype == NID_id_GostR3410_2012_512)
  2351. BUF_reverse(p + 2, NULL, u);
  2352. }
  2353. #endif
  2354. s2n(u, p);
  2355. n += u + 2;
  2356. /* Digest cached records and discard handshake buffer */
  2357. if (!ssl3_digest_cached_records(s, 0))
  2358. goto err;
  2359. if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
  2360. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
  2361. goto err;
  2362. }
  2363. EVP_MD_CTX_free(mctx);
  2364. return 1;
  2365. err:
  2366. EVP_MD_CTX_free(mctx);
  2367. return 0;
  2368. }
  2369. /*
  2370. * Check a certificate can be used for client authentication. Currently check
  2371. * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
  2372. * certificates can be used and optionally checks suitability for Suite B.
  2373. */
  2374. static int ssl3_check_client_certificate(SSL *s)
  2375. {
  2376. if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
  2377. return 0;
  2378. /* If no suitable signature algorithm can't use certificate */
  2379. if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
  2380. return 0;
  2381. /*
  2382. * If strict mode check suitability of chain before using it. This also
  2383. * adjusts suite B digest if necessary.
  2384. */
  2385. if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
  2386. !tls1_check_chain(s, NULL, NULL, NULL, -2))
  2387. return 0;
  2388. return 1;
  2389. }
  2390. WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst)
  2391. {
  2392. X509 *x509 = NULL;
  2393. EVP_PKEY *pkey = NULL;
  2394. int i;
  2395. if (wst == WORK_MORE_A) {
  2396. /* Let cert callback update client certificates if required */
  2397. if (s->cert->cert_cb) {
  2398. i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
  2399. if (i < 0) {
  2400. s->rwstate = SSL_X509_LOOKUP;
  2401. return WORK_MORE_A;
  2402. }
  2403. if (i == 0) {
  2404. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  2405. ossl_statem_set_error(s);
  2406. return 0;
  2407. }
  2408. s->rwstate = SSL_NOTHING;
  2409. }
  2410. if (ssl3_check_client_certificate(s))
  2411. return WORK_FINISHED_CONTINUE;
  2412. /* Fall through to WORK_MORE_B */
  2413. wst = WORK_MORE_B;
  2414. }
  2415. /* We need to get a client cert */
  2416. if (wst == WORK_MORE_B) {
  2417. /*
  2418. * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
  2419. * return(-1); We then get retied later
  2420. */
  2421. i = ssl_do_client_cert_cb(s, &x509, &pkey);
  2422. if (i < 0) {
  2423. s->rwstate = SSL_X509_LOOKUP;
  2424. return WORK_MORE_B;
  2425. }
  2426. s->rwstate = SSL_NOTHING;
  2427. if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
  2428. if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
  2429. i = 0;
  2430. } else if (i == 1) {
  2431. i = 0;
  2432. SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
  2433. SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  2434. }
  2435. X509_free(x509);
  2436. EVP_PKEY_free(pkey);
  2437. if (i && !ssl3_check_client_certificate(s))
  2438. i = 0;
  2439. if (i == 0) {
  2440. if (s->version == SSL3_VERSION) {
  2441. s->s3->tmp.cert_req = 0;
  2442. ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
  2443. return WORK_FINISHED_CONTINUE;
  2444. } else {
  2445. s->s3->tmp.cert_req = 2;
  2446. if (!ssl3_digest_cached_records(s, 0)) {
  2447. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  2448. ossl_statem_set_error(s);
  2449. return 0;
  2450. }
  2451. }
  2452. }
  2453. return WORK_FINISHED_CONTINUE;
  2454. }
  2455. /* Shouldn't ever get here */
  2456. return WORK_ERROR;
  2457. }
  2458. int tls_construct_client_certificate(SSL *s)
  2459. {
  2460. if (!ssl3_output_cert_chain(s,
  2461. (s->s3->tmp.cert_req ==
  2462. 2) ? NULL : s->cert->key)) {
  2463. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
  2464. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  2465. ossl_statem_set_error(s);
  2466. return 0;
  2467. }
  2468. return 1;
  2469. }
  2470. #define has_bits(i,m) (((i)&(m)) == (m))
  2471. int ssl3_check_cert_and_algorithm(SSL *s)
  2472. {
  2473. int i;
  2474. #ifndef OPENSSL_NO_EC
  2475. int idx;
  2476. #endif
  2477. long alg_k, alg_a;
  2478. EVP_PKEY *pkey = NULL;
  2479. int al = SSL_AD_HANDSHAKE_FAILURE;
  2480. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  2481. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  2482. /* we don't have a certificate */
  2483. if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
  2484. return (1);
  2485. /* This is the passed certificate */
  2486. #ifndef OPENSSL_NO_EC
  2487. idx = s->session->peer_type;
  2488. if (idx == SSL_PKEY_ECC) {
  2489. if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
  2490. /* check failed */
  2491. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
  2492. goto f_err;
  2493. } else {
  2494. return 1;
  2495. }
  2496. } else if (alg_a & SSL_aECDSA) {
  2497. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  2498. SSL_R_MISSING_ECDSA_SIGNING_CERT);
  2499. goto f_err;
  2500. } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
  2501. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
  2502. goto f_err;
  2503. }
  2504. #endif
  2505. pkey = X509_get0_pubkey(s->session->peer);
  2506. i = X509_certificate_type(s->session->peer, pkey);
  2507. /* Check that we have a certificate if we require one */
  2508. if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
  2509. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  2510. SSL_R_MISSING_RSA_SIGNING_CERT);
  2511. goto f_err;
  2512. }
  2513. #ifndef OPENSSL_NO_DSA
  2514. else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
  2515. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  2516. SSL_R_MISSING_DSA_SIGNING_CERT);
  2517. goto f_err;
  2518. }
  2519. #endif
  2520. #ifndef OPENSSL_NO_RSA
  2521. if (alg_k & (SSL_kRSA | SSL_kRSAPSK) &&
  2522. !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
  2523. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  2524. SSL_R_MISSING_RSA_ENCRYPTING_CERT);
  2525. goto f_err;
  2526. }
  2527. #endif
  2528. #ifndef OPENSSL_NO_DH
  2529. if ((alg_k & SSL_kDHE) && (s->s3->peer_tmp == NULL)) {
  2530. al = SSL_AD_INTERNAL_ERROR;
  2531. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
  2532. goto f_err;
  2533. }
  2534. #endif
  2535. return (1);
  2536. f_err:
  2537. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  2538. return (0);
  2539. }
  2540. #ifndef OPENSSL_NO_NEXTPROTONEG
  2541. int tls_construct_next_proto(SSL *s)
  2542. {
  2543. unsigned int len, padding_len;
  2544. unsigned char *d;
  2545. len = s->next_proto_negotiated_len;
  2546. padding_len = 32 - ((len + 2) % 32);
  2547. d = (unsigned char *)s->init_buf->data;
  2548. d[4] = len;
  2549. memcpy(d + 5, s->next_proto_negotiated, len);
  2550. d[5 + len] = padding_len;
  2551. memset(d + 6 + len, 0, padding_len);
  2552. *(d++) = SSL3_MT_NEXT_PROTO;
  2553. l2n3(2 + len + padding_len, d);
  2554. s->init_num = 4 + 2 + len + padding_len;
  2555. s->init_off = 0;
  2556. return 1;
  2557. }
  2558. #endif
  2559. int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
  2560. {
  2561. int i = 0;
  2562. #ifndef OPENSSL_NO_ENGINE
  2563. if (s->ctx->client_cert_engine) {
  2564. i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
  2565. SSL_get_client_CA_list(s),
  2566. px509, ppkey, NULL, NULL, NULL);
  2567. if (i != 0)
  2568. return i;
  2569. }
  2570. #endif
  2571. if (s->ctx->client_cert_cb)
  2572. i = s->ctx->client_cert_cb(s, px509, ppkey);
  2573. return i;
  2574. }
  2575. int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
  2576. unsigned char *p)
  2577. {
  2578. int i, j = 0;
  2579. const SSL_CIPHER *c;
  2580. unsigned char *q;
  2581. int empty_reneg_info_scsv = !s->renegotiate;
  2582. /* Set disabled masks for this session */
  2583. ssl_set_client_disabled(s);
  2584. if (sk == NULL)
  2585. return (0);
  2586. q = p;
  2587. for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
  2588. c = sk_SSL_CIPHER_value(sk, i);
  2589. /* Skip disabled ciphers */
  2590. if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
  2591. continue;
  2592. #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
  2593. if (c->id == SSL3_CK_SCSV) {
  2594. if (!empty_reneg_info_scsv)
  2595. continue;
  2596. else
  2597. empty_reneg_info_scsv = 0;
  2598. }
  2599. #endif
  2600. j = s->method->put_cipher_by_char(c, p);
  2601. p += j;
  2602. }
  2603. /*
  2604. * If p == q, no ciphers; caller indicates an error. Otherwise, add
  2605. * applicable SCSVs.
  2606. */
  2607. if (p != q) {
  2608. if (empty_reneg_info_scsv) {
  2609. static SSL_CIPHER scsv = {
  2610. 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
  2611. };
  2612. j = s->method->put_cipher_by_char(&scsv, p);
  2613. p += j;
  2614. #ifdef OPENSSL_RI_DEBUG
  2615. fprintf(stderr,
  2616. "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
  2617. #endif
  2618. }
  2619. if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
  2620. static SSL_CIPHER scsv = {
  2621. 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
  2622. };
  2623. j = s->method->put_cipher_by_char(&scsv, p);
  2624. p += j;
  2625. }
  2626. }
  2627. return (p - q);
  2628. }