handshake_server.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  112. *
  113. * Portions of the attached software ("Contribution") are developed by
  114. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  115. *
  116. * The Contribution is licensed pursuant to the OpenSSL open source
  117. * license provided above.
  118. *
  119. * ECC cipher suite support in OpenSSL originally written by
  120. * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
  121. *
  122. */
  123. /* ====================================================================
  124. * Copyright 2005 Nokia. All rights reserved.
  125. *
  126. * The portions of the attached software ("Contribution") is developed by
  127. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  128. * license.
  129. *
  130. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  131. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  132. * support (see RFC 4279) to OpenSSL.
  133. *
  134. * No patent licenses or other rights except those expressly stated in
  135. * the OpenSSL open source license shall be deemed granted or received
  136. * expressly, by implication, estoppel, or otherwise.
  137. *
  138. * No assurances are provided by Nokia that the Contribution does not
  139. * infringe the patent or other intellectual property rights of any third
  140. * party or that the license provides you with all the necessary rights
  141. * to make use of the Contribution.
  142. *
  143. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  144. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  145. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  146. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  147. * OTHERWISE. */
  148. #include <openssl/ssl.h>
  149. #include <assert.h>
  150. #include <string.h>
  151. #include <openssl/bn.h>
  152. #include <openssl/buf.h>
  153. #include <openssl/bytestring.h>
  154. #include <openssl/cipher.h>
  155. #include <openssl/dh.h>
  156. #include <openssl/ec.h>
  157. #include <openssl/ecdsa.h>
  158. #include <openssl/err.h>
  159. #include <openssl/evp.h>
  160. #include <openssl/hmac.h>
  161. #include <openssl/md5.h>
  162. #include <openssl/mem.h>
  163. #include <openssl/nid.h>
  164. #include <openssl/rand.h>
  165. #include <openssl/x509.h>
  166. #include "internal.h"
  167. #include "../crypto/internal.h"
  168. static int ssl3_process_client_hello(SSL_HANDSHAKE *hs);
  169. static int ssl3_select_certificate(SSL_HANDSHAKE *hs);
  170. static int ssl3_select_parameters(SSL_HANDSHAKE *hs);
  171. static int ssl3_send_server_hello(SSL_HANDSHAKE *hs);
  172. static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs);
  173. static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs);
  174. static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs);
  175. static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs);
  176. static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs);
  177. static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs);
  178. static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs);
  179. static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs);
  180. static int ssl3_get_next_proto(SSL_HANDSHAKE *hs);
  181. static int ssl3_get_channel_id(SSL_HANDSHAKE *hs);
  182. static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs);
  183. static struct CRYPTO_STATIC_MUTEX g_v2clienthello_lock =
  184. CRYPTO_STATIC_MUTEX_INIT;
  185. static uint64_t g_v2clienthello_count = 0;
  186. uint64_t SSL_get_v2clienthello_count(void) {
  187. CRYPTO_STATIC_MUTEX_lock_read(&g_v2clienthello_lock);
  188. uint64_t ret = g_v2clienthello_count;
  189. CRYPTO_STATIC_MUTEX_unlock_read(&g_v2clienthello_lock);
  190. return ret;
  191. }
  192. int ssl3_accept(SSL_HANDSHAKE *hs) {
  193. SSL *const ssl = hs->ssl;
  194. uint32_t alg_a;
  195. int ret = -1;
  196. assert(ssl->handshake_func == ssl3_accept);
  197. assert(ssl->server);
  198. for (;;) {
  199. int state = hs->state;
  200. switch (hs->state) {
  201. case SSL_ST_INIT:
  202. ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
  203. hs->state = SSL3_ST_SR_CLNT_HELLO_A;
  204. break;
  205. case SSL3_ST_SR_CLNT_HELLO_A:
  206. ret = ssl->method->ssl_get_message(ssl);
  207. if (ret <= 0) {
  208. goto end;
  209. }
  210. hs->state = SSL3_ST_SR_CLNT_HELLO_B;
  211. break;
  212. case SSL3_ST_SR_CLNT_HELLO_B:
  213. ret = ssl3_process_client_hello(hs);
  214. if (ret <= 0) {
  215. goto end;
  216. }
  217. hs->state = SSL3_ST_SR_CLNT_HELLO_C;
  218. break;
  219. case SSL3_ST_SR_CLNT_HELLO_C:
  220. ret = ssl3_select_certificate(hs);
  221. if (ret <= 0) {
  222. goto end;
  223. }
  224. if (hs->state != SSL_ST_TLS13) {
  225. hs->state = SSL3_ST_SR_CLNT_HELLO_D;
  226. }
  227. break;
  228. case SSL3_ST_SR_CLNT_HELLO_D:
  229. ret = ssl3_select_parameters(hs);
  230. if (ret <= 0) {
  231. goto end;
  232. }
  233. ssl->method->received_flight(ssl);
  234. hs->state = SSL3_ST_SW_SRVR_HELLO_A;
  235. break;
  236. case SSL3_ST_SW_SRVR_HELLO_A:
  237. ret = ssl3_send_server_hello(hs);
  238. if (ret <= 0) {
  239. goto end;
  240. }
  241. if (ssl->session != NULL) {
  242. hs->state = SSL3_ST_SW_SESSION_TICKET_A;
  243. } else {
  244. hs->state = SSL3_ST_SW_CERT_A;
  245. }
  246. break;
  247. case SSL3_ST_SW_CERT_A:
  248. if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  249. ret = ssl3_send_server_certificate(hs);
  250. if (ret <= 0) {
  251. goto end;
  252. }
  253. }
  254. hs->state = SSL3_ST_SW_CERT_STATUS_A;
  255. break;
  256. case SSL3_ST_SW_CERT_STATUS_A:
  257. if (hs->certificate_status_expected) {
  258. ret = ssl3_send_certificate_status(hs);
  259. if (ret <= 0) {
  260. goto end;
  261. }
  262. }
  263. hs->state = SSL3_ST_SW_KEY_EXCH_A;
  264. break;
  265. case SSL3_ST_SW_KEY_EXCH_A:
  266. case SSL3_ST_SW_KEY_EXCH_B:
  267. alg_a = hs->new_cipher->algorithm_auth;
  268. /* PSK ciphers send ServerKeyExchange if there is an identity hint. */
  269. if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) ||
  270. ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
  271. ret = ssl3_send_server_key_exchange(hs);
  272. if (ret <= 0) {
  273. goto end;
  274. }
  275. }
  276. hs->state = SSL3_ST_SW_CERT_REQ_A;
  277. break;
  278. case SSL3_ST_SW_CERT_REQ_A:
  279. if (hs->cert_request) {
  280. ret = ssl3_send_certificate_request(hs);
  281. if (ret <= 0) {
  282. goto end;
  283. }
  284. }
  285. hs->state = SSL3_ST_SW_SRVR_DONE_A;
  286. break;
  287. case SSL3_ST_SW_SRVR_DONE_A:
  288. ret = ssl3_send_server_hello_done(hs);
  289. if (ret <= 0) {
  290. goto end;
  291. }
  292. hs->next_state = SSL3_ST_SR_CERT_A;
  293. hs->state = SSL3_ST_SW_FLUSH;
  294. break;
  295. case SSL3_ST_SR_CERT_A:
  296. if (hs->cert_request) {
  297. ret = ssl3_get_client_certificate(hs);
  298. if (ret <= 0) {
  299. goto end;
  300. }
  301. }
  302. hs->state = SSL3_ST_SR_KEY_EXCH_A;
  303. break;
  304. case SSL3_ST_SR_KEY_EXCH_A:
  305. case SSL3_ST_SR_KEY_EXCH_B:
  306. ret = ssl3_get_client_key_exchange(hs);
  307. if (ret <= 0) {
  308. goto end;
  309. }
  310. hs->state = SSL3_ST_SR_CERT_VRFY_A;
  311. break;
  312. case SSL3_ST_SR_CERT_VRFY_A:
  313. ret = ssl3_get_cert_verify(hs);
  314. if (ret <= 0) {
  315. goto end;
  316. }
  317. hs->state = SSL3_ST_SR_CHANGE;
  318. break;
  319. case SSL3_ST_SR_CHANGE:
  320. ret = ssl->method->read_change_cipher_spec(ssl);
  321. if (ret <= 0) {
  322. goto end;
  323. }
  324. if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_READ)) {
  325. ret = -1;
  326. goto end;
  327. }
  328. hs->state = SSL3_ST_SR_NEXT_PROTO_A;
  329. break;
  330. case SSL3_ST_SR_NEXT_PROTO_A:
  331. if (hs->next_proto_neg_seen) {
  332. ret = ssl3_get_next_proto(hs);
  333. if (ret <= 0) {
  334. goto end;
  335. }
  336. }
  337. hs->state = SSL3_ST_SR_CHANNEL_ID_A;
  338. break;
  339. case SSL3_ST_SR_CHANNEL_ID_A:
  340. if (ssl->s3->tlsext_channel_id_valid) {
  341. ret = ssl3_get_channel_id(hs);
  342. if (ret <= 0) {
  343. goto end;
  344. }
  345. }
  346. hs->state = SSL3_ST_SR_FINISHED_A;
  347. break;
  348. case SSL3_ST_SR_FINISHED_A:
  349. ret = ssl3_get_finished(hs);
  350. if (ret <= 0) {
  351. goto end;
  352. }
  353. ssl->method->received_flight(ssl);
  354. if (ssl->session != NULL) {
  355. hs->state = SSL_ST_OK;
  356. } else {
  357. hs->state = SSL3_ST_SW_SESSION_TICKET_A;
  358. }
  359. /* If this is a full handshake with ChannelID then record the handshake
  360. * hashes in |hs->new_session| in case we need them to verify a
  361. * ChannelID signature on a resumption of this session in the future. */
  362. if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid) {
  363. ret = tls1_record_handshake_hashes_for_channel_id(hs);
  364. if (ret <= 0) {
  365. goto end;
  366. }
  367. }
  368. break;
  369. case SSL3_ST_SW_SESSION_TICKET_A:
  370. if (hs->ticket_expected) {
  371. ret = ssl3_send_new_session_ticket(hs);
  372. if (ret <= 0) {
  373. goto end;
  374. }
  375. }
  376. hs->state = SSL3_ST_SW_CHANGE;
  377. break;
  378. case SSL3_ST_SW_CHANGE:
  379. if (!ssl->method->add_change_cipher_spec(ssl) ||
  380. !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
  381. ret = -1;
  382. goto end;
  383. }
  384. hs->state = SSL3_ST_SW_FINISHED_A;
  385. break;
  386. case SSL3_ST_SW_FINISHED_A:
  387. ret = ssl3_send_finished(hs);
  388. if (ret <= 0) {
  389. goto end;
  390. }
  391. hs->state = SSL3_ST_SW_FLUSH;
  392. if (ssl->session != NULL) {
  393. hs->next_state = SSL3_ST_SR_CHANGE;
  394. } else {
  395. hs->next_state = SSL_ST_OK;
  396. }
  397. break;
  398. case SSL3_ST_SW_FLUSH:
  399. ret = ssl->method->flush_flight(ssl);
  400. if (ret <= 0) {
  401. goto end;
  402. }
  403. hs->state = hs->next_state;
  404. if (hs->state != SSL_ST_OK) {
  405. ssl->method->expect_flight(ssl);
  406. }
  407. break;
  408. case SSL_ST_TLS13:
  409. ret = tls13_handshake(hs);
  410. if (ret <= 0) {
  411. goto end;
  412. }
  413. hs->state = SSL_ST_OK;
  414. break;
  415. case SSL_ST_OK:
  416. ssl->method->release_current_message(ssl, 1 /* free_buffer */);
  417. /* If we aren't retaining peer certificates then we can discard it
  418. * now. */
  419. if (hs->new_session != NULL &&
  420. ssl->retain_only_sha256_of_client_certs) {
  421. sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
  422. hs->new_session->certs = NULL;
  423. ssl->ctx->x509_method->session_clear(hs->new_session);
  424. }
  425. SSL_SESSION_free(ssl->s3->established_session);
  426. if (ssl->session != NULL) {
  427. SSL_SESSION_up_ref(ssl->session);
  428. ssl->s3->established_session = ssl->session;
  429. } else {
  430. ssl->s3->established_session = hs->new_session;
  431. ssl->s3->established_session->not_resumable = 0;
  432. hs->new_session = NULL;
  433. }
  434. if (hs->v2_clienthello) {
  435. CRYPTO_STATIC_MUTEX_lock_write(&g_v2clienthello_lock);
  436. g_v2clienthello_count++;
  437. CRYPTO_STATIC_MUTEX_unlock_write(&g_v2clienthello_lock);
  438. }
  439. ssl->s3->initial_handshake_complete = 1;
  440. ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
  441. ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
  442. ret = 1;
  443. goto end;
  444. default:
  445. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
  446. ret = -1;
  447. goto end;
  448. }
  449. if (hs->state != state) {
  450. ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1);
  451. }
  452. }
  453. end:
  454. ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret);
  455. return ret;
  456. }
  457. int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello,
  458. uint16_t id) {
  459. CBS cipher_suites;
  460. CBS_init(&cipher_suites, client_hello->cipher_suites,
  461. client_hello->cipher_suites_len);
  462. while (CBS_len(&cipher_suites) > 0) {
  463. uint16_t got_id;
  464. if (!CBS_get_u16(&cipher_suites, &got_id)) {
  465. return 0;
  466. }
  467. if (got_id == id) {
  468. return 1;
  469. }
  470. }
  471. return 0;
  472. }
  473. static int negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  474. const SSL_CLIENT_HELLO *client_hello) {
  475. SSL *const ssl = hs->ssl;
  476. assert(!ssl->s3->have_version);
  477. uint16_t min_version, max_version;
  478. if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
  479. *out_alert = SSL_AD_PROTOCOL_VERSION;
  480. return 0;
  481. }
  482. uint16_t version = 0;
  483. /* Check supported_versions extension if it is present. */
  484. CBS supported_versions;
  485. if (ssl_client_hello_get_extension(client_hello, &supported_versions,
  486. TLSEXT_TYPE_supported_versions)) {
  487. CBS versions;
  488. if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) ||
  489. CBS_len(&supported_versions) != 0 ||
  490. CBS_len(&versions) == 0) {
  491. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  492. *out_alert = SSL_AD_DECODE_ERROR;
  493. return 0;
  494. }
  495. /* Choose the newest commonly-supported version advertised by the client.
  496. * The client orders the versions according to its preferences, but we're
  497. * not required to honor the client's preferences. */
  498. int found_version = 0;
  499. while (CBS_len(&versions) != 0) {
  500. uint16_t ext_version;
  501. if (!CBS_get_u16(&versions, &ext_version)) {
  502. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  503. *out_alert = SSL_AD_DECODE_ERROR;
  504. return 0;
  505. }
  506. if (!ssl->method->version_from_wire(&ext_version, ext_version)) {
  507. continue;
  508. }
  509. if (min_version <= ext_version &&
  510. ext_version <= max_version &&
  511. (!found_version || version < ext_version)) {
  512. version = ext_version;
  513. found_version = 1;
  514. }
  515. }
  516. if (!found_version) {
  517. goto unsupported_protocol;
  518. }
  519. } else {
  520. /* Process ClientHello.version instead. Note that versions beyond (D)TLS 1.2
  521. * do not use this mechanism. */
  522. if (SSL_is_dtls(ssl)) {
  523. if (client_hello->version <= DTLS1_2_VERSION) {
  524. version = TLS1_2_VERSION;
  525. } else if (client_hello->version <= DTLS1_VERSION) {
  526. version = TLS1_1_VERSION;
  527. } else {
  528. goto unsupported_protocol;
  529. }
  530. } else {
  531. if (client_hello->version >= TLS1_2_VERSION) {
  532. version = TLS1_2_VERSION;
  533. } else if (client_hello->version >= TLS1_1_VERSION) {
  534. version = TLS1_1_VERSION;
  535. } else if (client_hello->version >= TLS1_VERSION) {
  536. version = TLS1_VERSION;
  537. } else if (client_hello->version >= SSL3_VERSION) {
  538. version = SSL3_VERSION;
  539. } else {
  540. goto unsupported_protocol;
  541. }
  542. }
  543. /* Apply our minimum and maximum version. */
  544. if (version > max_version) {
  545. version = max_version;
  546. }
  547. if (version < min_version) {
  548. goto unsupported_protocol;
  549. }
  550. }
  551. /* Handle FALLBACK_SCSV. */
  552. if (ssl_client_cipher_list_contains_cipher(client_hello,
  553. SSL3_CK_FALLBACK_SCSV & 0xffff) &&
  554. version < max_version) {
  555. OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
  556. *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
  557. return 0;
  558. }
  559. hs->client_version = client_hello->version;
  560. ssl->version = ssl->method->version_to_wire(version);
  561. /* At this point, the connection's version is known and |ssl->version| is
  562. * fixed. Begin enforcing the record-layer version. */
  563. ssl->s3->have_version = 1;
  564. return 1;
  565. unsupported_protocol:
  566. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
  567. *out_alert = SSL_AD_PROTOCOL_VERSION;
  568. return 0;
  569. }
  570. static STACK_OF(SSL_CIPHER) *
  571. ssl_parse_client_cipher_list(const SSL_CLIENT_HELLO *client_hello) {
  572. CBS cipher_suites;
  573. CBS_init(&cipher_suites, client_hello->cipher_suites,
  574. client_hello->cipher_suites_len);
  575. STACK_OF(SSL_CIPHER) *sk = sk_SSL_CIPHER_new_null();
  576. if (sk == NULL) {
  577. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  578. goto err;
  579. }
  580. while (CBS_len(&cipher_suites) > 0) {
  581. uint16_t cipher_suite;
  582. if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
  583. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  584. goto err;
  585. }
  586. const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
  587. if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
  588. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  589. goto err;
  590. }
  591. }
  592. return sk;
  593. err:
  594. sk_SSL_CIPHER_free(sk);
  595. return NULL;
  596. }
  597. /* ssl_get_compatible_server_ciphers determines the key exchange and
  598. * authentication cipher suite masks compatible with the server configuration
  599. * and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
  600. * exchange mask and |*out_mask_a| to the authentication mask. */
  601. static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
  602. uint32_t *out_mask_k,
  603. uint32_t *out_mask_a) {
  604. SSL *const ssl = hs->ssl;
  605. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  606. *out_mask_k = SSL_kGENERIC;
  607. *out_mask_a = SSL_aGENERIC;
  608. return;
  609. }
  610. uint32_t mask_k = 0;
  611. uint32_t mask_a = 0;
  612. if (ssl_has_certificate(ssl)) {
  613. int type = ssl_private_key_type(ssl);
  614. if (type == NID_rsaEncryption) {
  615. mask_k |= SSL_kRSA;
  616. mask_a |= SSL_aRSA;
  617. } else if (ssl_is_ecdsa_key_type(type)) {
  618. mask_a |= SSL_aECDSA;
  619. }
  620. }
  621. if (ssl->cert->dh_tmp != NULL || ssl->cert->dh_tmp_cb != NULL) {
  622. mask_k |= SSL_kDHE;
  623. }
  624. /* Check for a shared group to consider ECDHE ciphers. */
  625. uint16_t unused;
  626. if (tls1_get_shared_group(hs, &unused)) {
  627. mask_k |= SSL_kECDHE;
  628. }
  629. /* PSK requires a server callback. */
  630. if (ssl->psk_server_callback != NULL) {
  631. mask_k |= SSL_kPSK;
  632. mask_a |= SSL_aPSK;
  633. }
  634. *out_mask_k = mask_k;
  635. *out_mask_a = mask_a;
  636. }
  637. static const SSL_CIPHER *ssl3_choose_cipher(
  638. SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
  639. const struct ssl_cipher_preference_list_st *server_pref) {
  640. SSL *const ssl = hs->ssl;
  641. const SSL_CIPHER *c, *ret = NULL;
  642. STACK_OF(SSL_CIPHER) *srvr = server_pref->ciphers, *prio, *allow;
  643. int ok;
  644. size_t cipher_index;
  645. uint32_t alg_k, alg_a, mask_k, mask_a;
  646. /* in_group_flags will either be NULL, or will point to an array of bytes
  647. * which indicate equal-preference groups in the |prio| stack. See the
  648. * comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
  649. * struct. */
  650. const uint8_t *in_group_flags;
  651. /* group_min contains the minimal index so far found in a group, or -1 if no
  652. * such value exists yet. */
  653. int group_min = -1;
  654. STACK_OF(SSL_CIPHER) *clnt = ssl_parse_client_cipher_list(client_hello);
  655. if (clnt == NULL) {
  656. return NULL;
  657. }
  658. if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
  659. prio = srvr;
  660. in_group_flags = server_pref->in_group_flags;
  661. allow = clnt;
  662. } else {
  663. prio = clnt;
  664. in_group_flags = NULL;
  665. allow = srvr;
  666. }
  667. ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
  668. for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
  669. c = sk_SSL_CIPHER_value(prio, i);
  670. ok = 1;
  671. /* Check the TLS version. */
  672. if (SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
  673. SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl)) {
  674. ok = 0;
  675. }
  676. alg_k = c->algorithm_mkey;
  677. alg_a = c->algorithm_auth;
  678. ok = ok && (alg_k & mask_k) && (alg_a & mask_a);
  679. if (ok && sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
  680. if (in_group_flags != NULL && in_group_flags[i] == 1) {
  681. /* This element of |prio| is in a group. Update the minimum index found
  682. * so far and continue looking. */
  683. if (group_min == -1 || (size_t)group_min > cipher_index) {
  684. group_min = cipher_index;
  685. }
  686. } else {
  687. if (group_min != -1 && (size_t)group_min < cipher_index) {
  688. cipher_index = group_min;
  689. }
  690. ret = sk_SSL_CIPHER_value(allow, cipher_index);
  691. break;
  692. }
  693. }
  694. if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
  695. /* We are about to leave a group, but we found a match in it, so that's
  696. * our answer. */
  697. ret = sk_SSL_CIPHER_value(allow, group_min);
  698. break;
  699. }
  700. }
  701. sk_SSL_CIPHER_free(clnt);
  702. return ret;
  703. }
  704. static int ssl3_process_client_hello(SSL_HANDSHAKE *hs) {
  705. SSL *const ssl = hs->ssl;
  706. if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_HELLO)) {
  707. return -1;
  708. }
  709. SSL_CLIENT_HELLO client_hello;
  710. if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
  711. ssl->init_num)) {
  712. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  713. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  714. return -1;
  715. }
  716. /* Run the early callback. */
  717. if (ssl->ctx->select_certificate_cb != NULL) {
  718. switch (ssl->ctx->select_certificate_cb(&client_hello)) {
  719. case 0:
  720. ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
  721. return -1;
  722. case -1:
  723. /* Connection rejected. */
  724. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
  725. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  726. return -1;
  727. default:
  728. /* fallthrough */;
  729. }
  730. }
  731. uint8_t alert = SSL_AD_DECODE_ERROR;
  732. if (!negotiate_version(hs, &alert, &client_hello)) {
  733. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  734. return -1;
  735. }
  736. /* Load the client random. */
  737. if (client_hello.random_len != SSL3_RANDOM_SIZE) {
  738. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  739. return -1;
  740. }
  741. OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
  742. client_hello.random_len);
  743. /* Only null compression is supported. TLS 1.3 further requires the peer
  744. * advertise no other compression. */
  745. if (OPENSSL_memchr(client_hello.compression_methods, 0,
  746. client_hello.compression_methods_len) == NULL ||
  747. (ssl3_protocol_version(ssl) >= TLS1_3_VERSION &&
  748. client_hello.compression_methods_len != 1)) {
  749. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST);
  750. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  751. return -1;
  752. }
  753. /* TLS extensions. */
  754. if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
  755. OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
  756. return -1;
  757. }
  758. return 1;
  759. }
  760. static int ssl3_select_certificate(SSL_HANDSHAKE *hs) {
  761. SSL *const ssl = hs->ssl;
  762. /* Call |cert_cb| to update server certificates if required. */
  763. if (ssl->cert->cert_cb != NULL) {
  764. int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
  765. if (rv == 0) {
  766. OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
  767. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  768. return -1;
  769. }
  770. if (rv < 0) {
  771. ssl->rwstate = SSL_X509_LOOKUP;
  772. return -1;
  773. }
  774. }
  775. if (!ssl_auto_chain_if_needed(ssl)) {
  776. return -1;
  777. }
  778. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  779. /* Jump to the TLS 1.3 state machine. */
  780. hs->state = SSL_ST_TLS13;
  781. hs->do_tls13_handshake = tls13_server_handshake;
  782. return 1;
  783. }
  784. SSL_CLIENT_HELLO client_hello;
  785. if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
  786. ssl->init_num)) {
  787. return -1;
  788. }
  789. /* Negotiate the cipher suite. This must be done after |cert_cb| so the
  790. * certificate is finalized. */
  791. hs->new_cipher =
  792. ssl3_choose_cipher(hs, &client_hello, ssl_get_cipher_preferences(ssl));
  793. if (hs->new_cipher == NULL) {
  794. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
  795. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  796. return -1;
  797. }
  798. return 1;
  799. }
  800. static int ssl3_select_parameters(SSL_HANDSHAKE *hs) {
  801. SSL *const ssl = hs->ssl;
  802. uint8_t al = SSL_AD_INTERNAL_ERROR;
  803. int ret = -1;
  804. SSL_SESSION *session = NULL;
  805. SSL_CLIENT_HELLO client_hello;
  806. if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
  807. ssl->init_num)) {
  808. return -1;
  809. }
  810. /* Determine whether we are doing session resumption. */
  811. int tickets_supported = 0, renew_ticket = 0;
  812. switch (ssl_get_prev_session(ssl, &session, &tickets_supported, &renew_ticket,
  813. &client_hello)) {
  814. case ssl_session_success:
  815. break;
  816. case ssl_session_error:
  817. goto err;
  818. case ssl_session_retry:
  819. ssl->rwstate = SSL_PENDING_SESSION;
  820. goto err;
  821. }
  822. if (session != NULL) {
  823. if (session->extended_master_secret && !hs->extended_master_secret) {
  824. /* A ClientHello without EMS that attempts to resume a session with EMS
  825. * is fatal to the connection. */
  826. al = SSL_AD_HANDSHAKE_FAILURE;
  827. OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
  828. goto f_err;
  829. }
  830. if (!ssl_session_is_resumable(hs, session) ||
  831. /* If the client offers the EMS extension, but the previous session
  832. * didn't use it, then negotiate a new session. */
  833. hs->extended_master_secret != session->extended_master_secret) {
  834. SSL_SESSION_free(session);
  835. session = NULL;
  836. }
  837. }
  838. if (session != NULL) {
  839. /* Use the old session. */
  840. hs->ticket_expected = renew_ticket;
  841. ssl->session = session;
  842. session = NULL;
  843. ssl->s3->session_reused = 1;
  844. } else {
  845. hs->ticket_expected = tickets_supported;
  846. ssl_set_session(ssl, NULL);
  847. if (!ssl_get_new_session(hs, 1 /* server */)) {
  848. goto err;
  849. }
  850. /* Clear the session ID if we want the session to be single-use. */
  851. if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
  852. hs->new_session->session_id_length = 0;
  853. }
  854. }
  855. if (ssl->ctx->dos_protection_cb != NULL &&
  856. ssl->ctx->dos_protection_cb(&client_hello) == 0) {
  857. /* Connection rejected for DOS reasons. */
  858. al = SSL_AD_INTERNAL_ERROR;
  859. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
  860. goto f_err;
  861. }
  862. if (ssl->session == NULL) {
  863. hs->new_session->cipher = hs->new_cipher;
  864. /* On new sessions, stash the SNI value in the session. */
  865. if (hs->hostname != NULL) {
  866. OPENSSL_free(hs->new_session->tlsext_hostname);
  867. hs->new_session->tlsext_hostname = BUF_strdup(hs->hostname);
  868. if (hs->new_session->tlsext_hostname == NULL) {
  869. al = SSL_AD_INTERNAL_ERROR;
  870. goto f_err;
  871. }
  872. }
  873. /* Determine whether to request a client certificate. */
  874. hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
  875. /* Only request a certificate if Channel ID isn't negotiated. */
  876. if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
  877. ssl->s3->tlsext_channel_id_valid) {
  878. hs->cert_request = 0;
  879. }
  880. /* CertificateRequest may only be sent in certificate-based ciphers. */
  881. if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  882. hs->cert_request = 0;
  883. }
  884. if (!hs->cert_request) {
  885. /* OpenSSL returns X509_V_OK when no certificates are requested. This is
  886. * classed by them as a bug, but it's assumed by at least NGINX. */
  887. hs->new_session->verify_result = X509_V_OK;
  888. }
  889. }
  890. /* HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
  891. * deferred. Complete it now. */
  892. if (!ssl_negotiate_alpn(hs, &al, &client_hello)) {
  893. goto f_err;
  894. }
  895. /* Now that all parameters are known, initialize the handshake hash and hash
  896. * the ClientHello. */
  897. if (!SSL_TRANSCRIPT_init_hash(&hs->transcript, ssl3_protocol_version(ssl),
  898. hs->new_cipher->algorithm_prf) ||
  899. !ssl_hash_current_message(hs)) {
  900. goto f_err;
  901. }
  902. /* Release the handshake buffer if client authentication isn't required. */
  903. if (!hs->cert_request) {
  904. SSL_TRANSCRIPT_free_buffer(&hs->transcript);
  905. }
  906. ret = 1;
  907. if (0) {
  908. f_err:
  909. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  910. }
  911. err:
  912. SSL_SESSION_free(session);
  913. return ret;
  914. }
  915. static int ssl3_send_server_hello(SSL_HANDSHAKE *hs) {
  916. SSL *const ssl = hs->ssl;
  917. /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
  918. * known attack while we fix ChannelID itself. */
  919. if (ssl->s3->tlsext_channel_id_valid &&
  920. (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
  921. ssl->s3->tlsext_channel_id_valid = 0;
  922. }
  923. /* If this is a resumption and the original handshake didn't support
  924. * ChannelID then we didn't record the original handshake hashes in the
  925. * session and so cannot resume with ChannelIDs. */
  926. if (ssl->session != NULL &&
  927. ssl->session->original_handshake_hash_len == 0) {
  928. ssl->s3->tlsext_channel_id_valid = 0;
  929. }
  930. struct timeval now;
  931. ssl_get_current_time(ssl, &now);
  932. ssl->s3->server_random[0] = now.tv_sec >> 24;
  933. ssl->s3->server_random[1] = now.tv_sec >> 16;
  934. ssl->s3->server_random[2] = now.tv_sec >> 8;
  935. ssl->s3->server_random[3] = now.tv_sec;
  936. if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
  937. return -1;
  938. }
  939. /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
  940. * 1.3 is finalized and we are not implementing a draft version. */
  941. const SSL_SESSION *session = hs->new_session;
  942. if (ssl->session != NULL) {
  943. session = ssl->session;
  944. }
  945. CBB cbb, body, session_id;
  946. if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) ||
  947. !CBB_add_u16(&body, ssl->version) ||
  948. !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
  949. !CBB_add_u8_length_prefixed(&body, &session_id) ||
  950. !CBB_add_bytes(&session_id, session->session_id,
  951. session->session_id_length) ||
  952. !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
  953. !CBB_add_u8(&body, 0 /* no compression */) ||
  954. !ssl_add_serverhello_tlsext(hs, &body) ||
  955. !ssl_add_message_cbb(ssl, &cbb)) {
  956. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  957. CBB_cleanup(&cbb);
  958. return -1;
  959. }
  960. return 1;
  961. }
  962. static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs) {
  963. SSL *const ssl = hs->ssl;
  964. if (!ssl_has_certificate(ssl)) {
  965. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
  966. return -1;
  967. }
  968. if (!ssl3_output_cert_chain(ssl)) {
  969. return -1;
  970. }
  971. return 1;
  972. }
  973. static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs) {
  974. SSL *const ssl = hs->ssl;
  975. CBB cbb, body, ocsp_response;
  976. if (!ssl->method->init_message(ssl, &cbb, &body,
  977. SSL3_MT_CERTIFICATE_STATUS) ||
  978. !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
  979. !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
  980. !CBB_add_bytes(&ocsp_response,
  981. CRYPTO_BUFFER_data(ssl->cert->ocsp_response),
  982. CRYPTO_BUFFER_len(ssl->cert->ocsp_response)) ||
  983. !ssl_add_message_cbb(ssl, &cbb)) {
  984. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  985. CBB_cleanup(&cbb);
  986. return -1;
  987. }
  988. return 1;
  989. }
  990. static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs) {
  991. SSL *const ssl = hs->ssl;
  992. CBB cbb, child;
  993. CBB_zero(&cbb);
  994. /* Put together the parameters. */
  995. if (hs->state == SSL3_ST_SW_KEY_EXCH_A) {
  996. uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  997. uint32_t alg_a = hs->new_cipher->algorithm_auth;
  998. /* Pre-allocate enough room to comfortably fit an ECDHE public key. */
  999. if (!CBB_init(&cbb, 128)) {
  1000. goto err;
  1001. }
  1002. /* PSK ciphers begin with an identity hint. */
  1003. if (alg_a & SSL_aPSK) {
  1004. size_t len =
  1005. (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
  1006. if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
  1007. !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
  1008. len)) {
  1009. goto err;
  1010. }
  1011. }
  1012. if (alg_k & SSL_kDHE) {
  1013. /* Determine the group to use. */
  1014. DH *params = ssl->cert->dh_tmp;
  1015. if (params == NULL && ssl->cert->dh_tmp_cb != NULL) {
  1016. params = ssl->cert->dh_tmp_cb(ssl, 0, 1024);
  1017. }
  1018. if (params == NULL) {
  1019. OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY);
  1020. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1021. goto err;
  1022. }
  1023. /* Set up DH, generate a key, and emit the public half. */
  1024. DH *dh = DHparams_dup(params);
  1025. if (dh == NULL) {
  1026. goto err;
  1027. }
  1028. SSL_ECDH_CTX_init_for_dhe(&hs->ecdh_ctx, dh);
  1029. if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
  1030. !BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) ||
  1031. !CBB_add_u16_length_prefixed(&cbb, &child) ||
  1032. !BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) ||
  1033. !CBB_add_u16_length_prefixed(&cbb, &child) ||
  1034. !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
  1035. goto err;
  1036. }
  1037. } else if (alg_k & SSL_kECDHE) {
  1038. /* Determine the group to use. */
  1039. uint16_t group_id;
  1040. if (!tls1_get_shared_group(hs, &group_id)) {
  1041. OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY);
  1042. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1043. goto err;
  1044. }
  1045. hs->new_session->group_id = group_id;
  1046. /* Set up ECDH, generate a key, and emit the public half. */
  1047. if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
  1048. !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) ||
  1049. !CBB_add_u16(&cbb, group_id) ||
  1050. !CBB_add_u8_length_prefixed(&cbb, &child) ||
  1051. !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
  1052. goto err;
  1053. }
  1054. } else {
  1055. assert(alg_k & SSL_kPSK);
  1056. }
  1057. if (!CBB_finish(&cbb, &hs->server_params, &hs->server_params_len)) {
  1058. goto err;
  1059. }
  1060. }
  1061. /* Assemble the message. */
  1062. CBB body;
  1063. if (!ssl->method->init_message(ssl, &cbb, &body,
  1064. SSL3_MT_SERVER_KEY_EXCHANGE) ||
  1065. !CBB_add_bytes(&body, hs->server_params, hs->server_params_len)) {
  1066. goto err;
  1067. }
  1068. /* Add a signature. */
  1069. if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  1070. if (!ssl_has_private_key(ssl)) {
  1071. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1072. goto err;
  1073. }
  1074. /* Determine the signature algorithm. */
  1075. uint16_t signature_algorithm;
  1076. if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
  1077. goto err;
  1078. }
  1079. if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
  1080. if (!CBB_add_u16(&body, signature_algorithm)) {
  1081. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1082. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1083. goto err;
  1084. }
  1085. }
  1086. /* Add space for the signature. */
  1087. const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
  1088. uint8_t *ptr;
  1089. if (!CBB_add_u16_length_prefixed(&body, &child) ||
  1090. !CBB_reserve(&child, &ptr, max_sig_len)) {
  1091. goto err;
  1092. }
  1093. size_t sig_len;
  1094. enum ssl_private_key_result_t sign_result;
  1095. if (hs->state == SSL3_ST_SW_KEY_EXCH_A) {
  1096. CBB transcript;
  1097. uint8_t *transcript_data;
  1098. size_t transcript_len;
  1099. if (!CBB_init(&transcript,
  1100. 2 * SSL3_RANDOM_SIZE + hs->server_params_len) ||
  1101. !CBB_add_bytes(&transcript, ssl->s3->client_random,
  1102. SSL3_RANDOM_SIZE) ||
  1103. !CBB_add_bytes(&transcript, ssl->s3->server_random,
  1104. SSL3_RANDOM_SIZE) ||
  1105. !CBB_add_bytes(&transcript, hs->server_params,
  1106. hs->server_params_len) ||
  1107. !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
  1108. CBB_cleanup(&transcript);
  1109. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1110. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1111. goto err;
  1112. }
  1113. sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len,
  1114. signature_algorithm, transcript_data,
  1115. transcript_len);
  1116. OPENSSL_free(transcript_data);
  1117. } else {
  1118. assert(hs->state == SSL3_ST_SW_KEY_EXCH_B);
  1119. sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
  1120. }
  1121. switch (sign_result) {
  1122. case ssl_private_key_success:
  1123. if (!CBB_did_write(&child, sig_len)) {
  1124. goto err;
  1125. }
  1126. break;
  1127. case ssl_private_key_failure:
  1128. goto err;
  1129. case ssl_private_key_retry:
  1130. ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
  1131. hs->state = SSL3_ST_SW_KEY_EXCH_B;
  1132. goto err;
  1133. }
  1134. }
  1135. if (!ssl_add_message_cbb(ssl, &cbb)) {
  1136. goto err;
  1137. }
  1138. OPENSSL_free(hs->server_params);
  1139. hs->server_params = NULL;
  1140. hs->server_params_len = 0;
  1141. return 1;
  1142. err:
  1143. CBB_cleanup(&cbb);
  1144. return -1;
  1145. }
  1146. static int add_cert_types(SSL *ssl, CBB *cbb) {
  1147. /* Get configured signature algorithms. */
  1148. int have_rsa_sign = 0;
  1149. int have_ecdsa_sign = 0;
  1150. const uint16_t *sig_algs;
  1151. size_t num_sig_algs = tls12_get_verify_sigalgs(ssl, &sig_algs);
  1152. for (size_t i = 0; i < num_sig_algs; i++) {
  1153. switch (sig_algs[i]) {
  1154. case SSL_SIGN_RSA_PKCS1_SHA512:
  1155. case SSL_SIGN_RSA_PKCS1_SHA384:
  1156. case SSL_SIGN_RSA_PKCS1_SHA256:
  1157. case SSL_SIGN_RSA_PKCS1_SHA1:
  1158. have_rsa_sign = 1;
  1159. break;
  1160. case SSL_SIGN_ECDSA_SECP521R1_SHA512:
  1161. case SSL_SIGN_ECDSA_SECP384R1_SHA384:
  1162. case SSL_SIGN_ECDSA_SECP256R1_SHA256:
  1163. case SSL_SIGN_ECDSA_SHA1:
  1164. have_ecdsa_sign = 1;
  1165. break;
  1166. }
  1167. }
  1168. if (have_rsa_sign && !CBB_add_u8(cbb, SSL3_CT_RSA_SIGN)) {
  1169. return 0;
  1170. }
  1171. /* ECDSA certs can be used with RSA cipher suites as well so we don't need to
  1172. * check for SSL_kECDH or SSL_kECDHE. */
  1173. if (ssl->version >= TLS1_VERSION && have_ecdsa_sign &&
  1174. !CBB_add_u8(cbb, TLS_CT_ECDSA_SIGN)) {
  1175. return 0;
  1176. }
  1177. return 1;
  1178. }
  1179. static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs) {
  1180. SSL *const ssl = hs->ssl;
  1181. CBB cbb, body, cert_types, sigalgs_cbb;
  1182. if (!ssl->method->init_message(ssl, &cbb, &body,
  1183. SSL3_MT_CERTIFICATE_REQUEST) ||
  1184. !CBB_add_u8_length_prefixed(&body, &cert_types) ||
  1185. !add_cert_types(ssl, &cert_types)) {
  1186. goto err;
  1187. }
  1188. if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
  1189. const uint16_t *sigalgs;
  1190. size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
  1191. if (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb)) {
  1192. goto err;
  1193. }
  1194. for (size_t i = 0; i < num_sigalgs; i++) {
  1195. if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
  1196. goto err;
  1197. }
  1198. }
  1199. }
  1200. if (!ssl_add_client_CA_list(ssl, &body) ||
  1201. !ssl_add_message_cbb(ssl, &cbb)) {
  1202. goto err;
  1203. }
  1204. return 1;
  1205. err:
  1206. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1207. CBB_cleanup(&cbb);
  1208. return -1;
  1209. }
  1210. static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs) {
  1211. SSL *const ssl = hs->ssl;
  1212. CBB cbb, body;
  1213. if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO_DONE) ||
  1214. !ssl_add_message_cbb(ssl, &cbb)) {
  1215. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1216. CBB_cleanup(&cbb);
  1217. return -1;
  1218. }
  1219. return 1;
  1220. }
  1221. static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs) {
  1222. SSL *const ssl = hs->ssl;
  1223. assert(hs->cert_request);
  1224. int msg_ret = ssl->method->ssl_get_message(ssl);
  1225. if (msg_ret <= 0) {
  1226. return msg_ret;
  1227. }
  1228. if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
  1229. if (ssl->version == SSL3_VERSION &&
  1230. ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
  1231. /* In SSL 3.0, the Certificate message is omitted to signal no
  1232. * certificate. */
  1233. if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  1234. OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  1235. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1236. return -1;
  1237. }
  1238. /* OpenSSL returns X509_V_OK when no certificates are received. This is
  1239. * classed by them as a bug, but it's assumed by at least NGINX. */
  1240. hs->new_session->verify_result = X509_V_OK;
  1241. ssl->s3->tmp.reuse_message = 1;
  1242. return 1;
  1243. }
  1244. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  1245. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  1246. return -1;
  1247. }
  1248. if (!ssl_hash_current_message(hs)) {
  1249. return -1;
  1250. }
  1251. CBS certificate_msg;
  1252. CBS_init(&certificate_msg, ssl->init_msg, ssl->init_num);
  1253. sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
  1254. EVP_PKEY_free(hs->peer_pubkey);
  1255. hs->peer_pubkey = NULL;
  1256. uint8_t alert = SSL_AD_DECODE_ERROR;
  1257. hs->new_session->certs = ssl_parse_cert_chain(
  1258. &alert, &hs->peer_pubkey,
  1259. ssl->retain_only_sha256_of_client_certs ? hs->new_session->peer_sha256
  1260. : NULL,
  1261. &certificate_msg, ssl->ctx->pool);
  1262. if (hs->new_session->certs == NULL) {
  1263. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  1264. return -1;
  1265. }
  1266. if (CBS_len(&certificate_msg) != 0 ||
  1267. !ssl->ctx->x509_method->session_cache_objects(hs->new_session)) {
  1268. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1269. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1270. return -1;
  1271. }
  1272. if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) {
  1273. /* No client certificate so the handshake buffer may be discarded. */
  1274. SSL_TRANSCRIPT_free_buffer(&hs->transcript);
  1275. /* In SSL 3.0, sending no certificate is signaled by omitting the
  1276. * Certificate message. */
  1277. if (ssl->version == SSL3_VERSION) {
  1278. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
  1279. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1280. return -1;
  1281. }
  1282. if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  1283. /* Fail for TLS only if we required a certificate */
  1284. OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  1285. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1286. return -1;
  1287. }
  1288. /* OpenSSL returns X509_V_OK when no certificates are received. This is
  1289. * classed by them as a bug, but it's assumed by at least NGINX. */
  1290. hs->new_session->verify_result = X509_V_OK;
  1291. return 1;
  1292. }
  1293. /* The hash will have been filled in. */
  1294. if (ssl->retain_only_sha256_of_client_certs) {
  1295. hs->new_session->peer_sha256_valid = 1;
  1296. }
  1297. if (!ssl_verify_cert_chain(ssl, &hs->new_session->verify_result,
  1298. hs->new_session->x509_chain)) {
  1299. return -1;
  1300. }
  1301. return 1;
  1302. }
  1303. static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs) {
  1304. SSL *const ssl = hs->ssl;
  1305. int al;
  1306. CBS client_key_exchange;
  1307. uint32_t alg_k;
  1308. uint32_t alg_a;
  1309. uint8_t *premaster_secret = NULL;
  1310. size_t premaster_secret_len = 0;
  1311. uint8_t *decrypt_buf = NULL;
  1312. unsigned psk_len = 0;
  1313. uint8_t psk[PSK_MAX_PSK_LEN];
  1314. if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
  1315. int ret = ssl->method->ssl_get_message(ssl);
  1316. if (ret <= 0) {
  1317. return ret;
  1318. }
  1319. if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE) ||
  1320. !ssl_hash_current_message(hs)) {
  1321. return -1;
  1322. }
  1323. }
  1324. CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
  1325. alg_k = hs->new_cipher->algorithm_mkey;
  1326. alg_a = hs->new_cipher->algorithm_auth;
  1327. /* If using a PSK key exchange, prepare the pre-shared key. */
  1328. if (alg_a & SSL_aPSK) {
  1329. CBS psk_identity;
  1330. /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
  1331. * then this is the only field in the message. */
  1332. if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
  1333. ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
  1334. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1335. al = SSL_AD_DECODE_ERROR;
  1336. goto f_err;
  1337. }
  1338. if (ssl->psk_server_callback == NULL) {
  1339. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB);
  1340. al = SSL_AD_INTERNAL_ERROR;
  1341. goto f_err;
  1342. }
  1343. if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
  1344. CBS_contains_zero_byte(&psk_identity)) {
  1345. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  1346. al = SSL_AD_ILLEGAL_PARAMETER;
  1347. goto f_err;
  1348. }
  1349. if (!CBS_strdup(&psk_identity, &hs->new_session->psk_identity)) {
  1350. al = SSL_AD_INTERNAL_ERROR;
  1351. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1352. goto f_err;
  1353. }
  1354. /* Look up the key for the identity. */
  1355. psk_len = ssl->psk_server_callback(ssl, hs->new_session->psk_identity, psk,
  1356. sizeof(psk));
  1357. if (psk_len > PSK_MAX_PSK_LEN) {
  1358. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1359. al = SSL_AD_INTERNAL_ERROR;
  1360. goto f_err;
  1361. } else if (psk_len == 0) {
  1362. /* PSK related to the given identity not found */
  1363. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
  1364. al = SSL_AD_UNKNOWN_PSK_IDENTITY;
  1365. goto f_err;
  1366. }
  1367. }
  1368. /* Depending on the key exchange method, compute |premaster_secret| and
  1369. * |premaster_secret_len|. */
  1370. if (alg_k & SSL_kRSA) {
  1371. /* Allocate a buffer large enough for an RSA decryption. */
  1372. const size_t rsa_size = ssl_private_key_max_signature_len(ssl);
  1373. decrypt_buf = OPENSSL_malloc(rsa_size);
  1374. if (decrypt_buf == NULL) {
  1375. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1376. goto err;
  1377. }
  1378. enum ssl_private_key_result_t decrypt_result;
  1379. size_t decrypt_len;
  1380. if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
  1381. if (!ssl_has_private_key(ssl) ||
  1382. ssl_private_key_type(ssl) != NID_rsaEncryption) {
  1383. al = SSL_AD_HANDSHAKE_FAILURE;
  1384. OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE);
  1385. goto f_err;
  1386. }
  1387. CBS encrypted_premaster_secret;
  1388. if (ssl->version > SSL3_VERSION) {
  1389. if (!CBS_get_u16_length_prefixed(&client_key_exchange,
  1390. &encrypted_premaster_secret) ||
  1391. CBS_len(&client_key_exchange) != 0) {
  1392. al = SSL_AD_DECODE_ERROR;
  1393. OPENSSL_PUT_ERROR(SSL,
  1394. SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
  1395. goto f_err;
  1396. }
  1397. } else {
  1398. encrypted_premaster_secret = client_key_exchange;
  1399. }
  1400. /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
  1401. * timing-sensitive code below. */
  1402. decrypt_result = ssl_private_key_decrypt(
  1403. ssl, decrypt_buf, &decrypt_len, rsa_size,
  1404. CBS_data(&encrypted_premaster_secret),
  1405. CBS_len(&encrypted_premaster_secret));
  1406. } else {
  1407. assert(hs->state == SSL3_ST_SR_KEY_EXCH_B);
  1408. /* Complete async decrypt. */
  1409. decrypt_result =
  1410. ssl_private_key_complete(ssl, decrypt_buf, &decrypt_len, rsa_size);
  1411. }
  1412. switch (decrypt_result) {
  1413. case ssl_private_key_success:
  1414. break;
  1415. case ssl_private_key_failure:
  1416. goto err;
  1417. case ssl_private_key_retry:
  1418. ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
  1419. hs->state = SSL3_ST_SR_KEY_EXCH_B;
  1420. goto err;
  1421. }
  1422. if (decrypt_len != rsa_size) {
  1423. al = SSL_AD_DECRYPT_ERROR;
  1424. OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
  1425. goto f_err;
  1426. }
  1427. /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
  1428. * section 7.4.7.1. */
  1429. premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
  1430. premaster_secret = OPENSSL_malloc(premaster_secret_len);
  1431. if (premaster_secret == NULL) {
  1432. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1433. goto err;
  1434. }
  1435. if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
  1436. goto err;
  1437. }
  1438. /* The smallest padded premaster is 11 bytes of overhead. Small keys are
  1439. * publicly invalid. */
  1440. if (decrypt_len < 11 + premaster_secret_len) {
  1441. al = SSL_AD_DECRYPT_ERROR;
  1442. OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
  1443. goto f_err;
  1444. }
  1445. /* Check the padding. See RFC 3447, section 7.2.2. */
  1446. size_t padding_len = decrypt_len - premaster_secret_len;
  1447. uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
  1448. constant_time_eq_int_8(decrypt_buf[1], 2);
  1449. for (size_t i = 2; i < padding_len - 1; i++) {
  1450. good &= ~constant_time_is_zero_8(decrypt_buf[i]);
  1451. }
  1452. good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
  1453. /* The premaster secret must begin with |client_version|. This too must be
  1454. * checked in constant time (http://eprint.iacr.org/2003/052/). */
  1455. good &= constant_time_eq_8(decrypt_buf[padding_len],
  1456. (unsigned)(hs->client_version >> 8));
  1457. good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
  1458. (unsigned)(hs->client_version & 0xff));
  1459. /* Select, in constant time, either the decrypted premaster or the random
  1460. * premaster based on |good|. */
  1461. for (size_t i = 0; i < premaster_secret_len; i++) {
  1462. premaster_secret[i] = constant_time_select_8(
  1463. good, decrypt_buf[padding_len + i], premaster_secret[i]);
  1464. }
  1465. OPENSSL_free(decrypt_buf);
  1466. decrypt_buf = NULL;
  1467. } else if (alg_k & (SSL_kECDHE|SSL_kDHE)) {
  1468. /* Parse the ClientKeyExchange. */
  1469. CBS peer_key;
  1470. if (!SSL_ECDH_CTX_get_key(&hs->ecdh_ctx, &client_key_exchange, &peer_key) ||
  1471. CBS_len(&client_key_exchange) != 0) {
  1472. al = SSL_AD_DECODE_ERROR;
  1473. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1474. goto f_err;
  1475. }
  1476. /* Compute the premaster. */
  1477. uint8_t alert = SSL_AD_DECODE_ERROR;
  1478. if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, &premaster_secret,
  1479. &premaster_secret_len, &alert, CBS_data(&peer_key),
  1480. CBS_len(&peer_key))) {
  1481. al = alert;
  1482. goto f_err;
  1483. }
  1484. /* The key exchange state may now be discarded. */
  1485. SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
  1486. } else if (alg_k & SSL_kPSK) {
  1487. /* For plain PSK, other_secret is a block of 0s with the same length as the
  1488. * pre-shared key. */
  1489. premaster_secret_len = psk_len;
  1490. premaster_secret = OPENSSL_malloc(premaster_secret_len);
  1491. if (premaster_secret == NULL) {
  1492. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1493. goto err;
  1494. }
  1495. OPENSSL_memset(premaster_secret, 0, premaster_secret_len);
  1496. } else {
  1497. al = SSL_AD_HANDSHAKE_FAILURE;
  1498. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE);
  1499. goto f_err;
  1500. }
  1501. /* For a PSK cipher suite, the actual pre-master secret is combined with the
  1502. * pre-shared key. */
  1503. if (alg_a & SSL_aPSK) {
  1504. CBB new_premaster, child;
  1505. uint8_t *new_data;
  1506. size_t new_len;
  1507. CBB_zero(&new_premaster);
  1508. if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) ||
  1509. !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
  1510. !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
  1511. !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
  1512. !CBB_add_bytes(&child, psk, psk_len) ||
  1513. !CBB_finish(&new_premaster, &new_data, &new_len)) {
  1514. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1515. CBB_cleanup(&new_premaster);
  1516. goto err;
  1517. }
  1518. OPENSSL_cleanse(premaster_secret, premaster_secret_len);
  1519. OPENSSL_free(premaster_secret);
  1520. premaster_secret = new_data;
  1521. premaster_secret_len = new_len;
  1522. }
  1523. /* Compute the master secret */
  1524. hs->new_session->master_key_length = tls1_generate_master_secret(
  1525. hs, hs->new_session->master_key, premaster_secret, premaster_secret_len);
  1526. if (hs->new_session->master_key_length == 0) {
  1527. goto err;
  1528. }
  1529. hs->new_session->extended_master_secret = hs->extended_master_secret;
  1530. OPENSSL_cleanse(premaster_secret, premaster_secret_len);
  1531. OPENSSL_free(premaster_secret);
  1532. return 1;
  1533. f_err:
  1534. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  1535. err:
  1536. if (premaster_secret != NULL) {
  1537. OPENSSL_cleanse(premaster_secret, premaster_secret_len);
  1538. OPENSSL_free(premaster_secret);
  1539. }
  1540. OPENSSL_free(decrypt_buf);
  1541. return -1;
  1542. }
  1543. static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs) {
  1544. SSL *const ssl = hs->ssl;
  1545. int al;
  1546. CBS certificate_verify, signature;
  1547. /* Only RSA and ECDSA client certificates are supported, so a
  1548. * CertificateVerify is required if and only if there's a client certificate.
  1549. * */
  1550. if (hs->peer_pubkey == NULL) {
  1551. SSL_TRANSCRIPT_free_buffer(&hs->transcript);
  1552. return 1;
  1553. }
  1554. int msg_ret = ssl->method->ssl_get_message(ssl);
  1555. if (msg_ret <= 0) {
  1556. return msg_ret;
  1557. }
  1558. if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_VERIFY)) {
  1559. return -1;
  1560. }
  1561. CBS_init(&certificate_verify, ssl->init_msg, ssl->init_num);
  1562. /* Determine the digest type if needbe. */
  1563. uint16_t signature_algorithm = 0;
  1564. if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
  1565. if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
  1566. al = SSL_AD_DECODE_ERROR;
  1567. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1568. goto f_err;
  1569. }
  1570. if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
  1571. goto f_err;
  1572. }
  1573. hs->new_session->peer_signature_algorithm = signature_algorithm;
  1574. } else if (hs->peer_pubkey->type == EVP_PKEY_RSA) {
  1575. signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
  1576. } else if (hs->peer_pubkey->type == EVP_PKEY_EC) {
  1577. signature_algorithm = SSL_SIGN_ECDSA_SHA1;
  1578. } else {
  1579. al = SSL_AD_UNSUPPORTED_CERTIFICATE;
  1580. OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
  1581. goto f_err;
  1582. }
  1583. /* Parse and verify the signature. */
  1584. if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
  1585. CBS_len(&certificate_verify) != 0) {
  1586. al = SSL_AD_DECODE_ERROR;
  1587. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1588. goto f_err;
  1589. }
  1590. int sig_ok;
  1591. /* The SSL3 construction for CertificateVerify does not decompose into a
  1592. * single final digest and signature, and must be special-cased. */
  1593. if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
  1594. uint8_t digest[EVP_MAX_MD_SIZE];
  1595. size_t digest_len;
  1596. if (!SSL_TRANSCRIPT_ssl3_cert_verify_hash(&hs->transcript, digest,
  1597. &digest_len, hs->new_session,
  1598. signature_algorithm)) {
  1599. goto err;
  1600. }
  1601. EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(hs->peer_pubkey, NULL);
  1602. sig_ok = pctx != NULL &&
  1603. EVP_PKEY_verify_init(pctx) &&
  1604. EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature),
  1605. digest, digest_len);
  1606. EVP_PKEY_CTX_free(pctx);
  1607. } else {
  1608. sig_ok = ssl_public_key_verify(
  1609. ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
  1610. hs->peer_pubkey, (const uint8_t *)hs->transcript.buffer->data,
  1611. hs->transcript.buffer->length);
  1612. }
  1613. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  1614. sig_ok = 1;
  1615. ERR_clear_error();
  1616. #endif
  1617. if (!sig_ok) {
  1618. al = SSL_AD_DECRYPT_ERROR;
  1619. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
  1620. goto f_err;
  1621. }
  1622. /* The handshake buffer is no longer necessary, and we may hash the current
  1623. * message.*/
  1624. SSL_TRANSCRIPT_free_buffer(&hs->transcript);
  1625. if (!ssl_hash_current_message(hs)) {
  1626. goto err;
  1627. }
  1628. return 1;
  1629. f_err:
  1630. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  1631. err:
  1632. return 0;
  1633. }
  1634. /* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
  1635. * sets the next_proto member in s if found */
  1636. static int ssl3_get_next_proto(SSL_HANDSHAKE *hs) {
  1637. SSL *const ssl = hs->ssl;
  1638. int ret = ssl->method->ssl_get_message(ssl);
  1639. if (ret <= 0) {
  1640. return ret;
  1641. }
  1642. if (!ssl_check_message_type(ssl, SSL3_MT_NEXT_PROTO) ||
  1643. !ssl_hash_current_message(hs)) {
  1644. return -1;
  1645. }
  1646. CBS next_protocol, selected_protocol, padding;
  1647. CBS_init(&next_protocol, ssl->init_msg, ssl->init_num);
  1648. if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
  1649. !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
  1650. CBS_len(&next_protocol) != 0) {
  1651. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1652. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1653. return 0;
  1654. }
  1655. if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
  1656. &ssl->s3->next_proto_negotiated_len)) {
  1657. return 0;
  1658. }
  1659. return 1;
  1660. }
  1661. /* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
  1662. static int ssl3_get_channel_id(SSL_HANDSHAKE *hs) {
  1663. SSL *const ssl = hs->ssl;
  1664. int msg_ret = ssl->method->ssl_get_message(ssl);
  1665. if (msg_ret <= 0) {
  1666. return msg_ret;
  1667. }
  1668. if (!ssl_check_message_type(ssl, SSL3_MT_CHANNEL_ID) ||
  1669. !tls1_verify_channel_id(hs) ||
  1670. !ssl_hash_current_message(hs)) {
  1671. return -1;
  1672. }
  1673. return 1;
  1674. }
  1675. static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs) {
  1676. SSL *const ssl = hs->ssl;
  1677. const SSL_SESSION *session;
  1678. SSL_SESSION *session_copy = NULL;
  1679. if (ssl->session == NULL) {
  1680. /* Fix the timeout to measure from the ticket issuance time. */
  1681. ssl_session_rebase_time(ssl, hs->new_session);
  1682. session = hs->new_session;
  1683. } else {
  1684. /* We are renewing an existing session. Duplicate the session to adjust the
  1685. * timeout. */
  1686. session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
  1687. if (session_copy == NULL) {
  1688. return -1;
  1689. }
  1690. ssl_session_rebase_time(ssl, session_copy);
  1691. session = session_copy;
  1692. }
  1693. CBB cbb, body, ticket;
  1694. int ok =
  1695. ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEW_SESSION_TICKET) &&
  1696. CBB_add_u32(&body, session->timeout) &&
  1697. CBB_add_u16_length_prefixed(&body, &ticket) &&
  1698. ssl_encrypt_ticket(ssl, &ticket, session) &&
  1699. ssl_add_message_cbb(ssl, &cbb);
  1700. SSL_SESSION_free(session_copy);
  1701. CBB_cleanup(&cbb);
  1702. if (!ok) {
  1703. return -1;
  1704. }
  1705. return 1;
  1706. }