Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Tighten up EMS resumption behaviour. The client and server both have to decide on behaviour when resuming a session where the EMS state of the session doesn't match the EMS state as exchanged in the handshake. Original handshake | No Yes ------+-------------------------------------------------------------- | R | Server: ok [1] Server: abort [3] e No | Client: ok [2] Client: abort [4] s | u | m | e | Yes | Server: don't resume No problem | Client: abort; server | shouldn't have resumed [1] Servers want to accept legacy clients. The draft[5] says that resumptions SHOULD be rejected so that Triple-Handshake can't be done, but we'll rather enforce that EMS was used when using tls-unique etc. [2] The draft[5] says that even the initial handshake should be aborted if the server doesn't support EMS, but we need to be able to talk to the world. [3] This is a very weird case where a client has regressed without flushing the session cache. Hopefully we can be strict and reject these. [4] This can happen when a server-farm shares a session cache but frontends are not all updated at once. If Chrome is strict here then hopefully we can prevent any servers from existing that will try to resume an EMS session that they don't understand. OpenSSL appears to be ok here: https://www.ietf.org/mail-archive/web/tls/current/msg16570.html [5] https://tools.ietf.org/html/draft-ietf-tls-session-hash-05#section-5.2 BUG=492200 Change-Id: Ie1225a3960d49117b05eefa5a36263d8e556e467 Reviewed-on: https://boringssl-review.googlesource.com/4981 Reviewed-by: Adam Langley <agl@google.com>
vor 9 Jahren
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344
  1. /* ssl/s3_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 <assert.h>
  151. #include <stdio.h>
  152. #include <string.h>
  153. #include <openssl/buf.h>
  154. #include <openssl/bytestring.h>
  155. #include <openssl/rand.h>
  156. #include <openssl/obj.h>
  157. #include <openssl/err.h>
  158. #include <openssl/evp.h>
  159. #include <openssl/mem.h>
  160. #include <openssl/md5.h>
  161. #include <openssl/dh.h>
  162. #include <openssl/bn.h>
  163. #include <openssl/x509.h>
  164. #include <openssl/x509v3.h>
  165. #include "internal.h"
  166. #include "../crypto/dh/internal.h"
  167. int ssl3_connect(SSL *s) {
  168. BUF_MEM *buf = NULL;
  169. void (*cb)(const SSL *ssl, int type, int val) = NULL;
  170. int ret = -1;
  171. int new_state, state, skip = 0;
  172. assert(s->handshake_func == ssl3_connect);
  173. assert(!s->server);
  174. assert(!SSL_IS_DTLS(s));
  175. ERR_clear_error();
  176. ERR_clear_system_error();
  177. if (s->info_callback != NULL) {
  178. cb = s->info_callback;
  179. } else if (s->ctx->info_callback != NULL) {
  180. cb = s->ctx->info_callback;
  181. }
  182. s->in_handshake++;
  183. for (;;) {
  184. state = s->state;
  185. switch (s->state) {
  186. case SSL_ST_CONNECT:
  187. if (cb != NULL) {
  188. cb(s, SSL_CB_HANDSHAKE_START, 1);
  189. }
  190. if (s->init_buf == NULL) {
  191. buf = BUF_MEM_new();
  192. if (buf == NULL ||
  193. !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
  194. ret = -1;
  195. goto end;
  196. }
  197. s->init_buf = buf;
  198. buf = NULL;
  199. }
  200. if (!ssl_init_wbio_buffer(s, 0)) {
  201. ret = -1;
  202. goto end;
  203. }
  204. /* don't push the buffering BIO quite yet */
  205. if (!ssl3_init_finished_mac(s)) {
  206. OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR);
  207. ret = -1;
  208. goto end;
  209. }
  210. s->state = SSL3_ST_CW_CLNT_HELLO_A;
  211. s->init_num = 0;
  212. break;
  213. case SSL3_ST_CW_CLNT_HELLO_A:
  214. case SSL3_ST_CW_CLNT_HELLO_B:
  215. s->shutdown = 0;
  216. ret = ssl3_send_client_hello(s);
  217. if (ret <= 0) {
  218. goto end;
  219. }
  220. s->state = SSL3_ST_CR_SRVR_HELLO_A;
  221. s->init_num = 0;
  222. /* turn on buffering for the next lot of output */
  223. if (s->bbio != s->wbio) {
  224. s->wbio = BIO_push(s->bbio, s->wbio);
  225. }
  226. break;
  227. case SSL3_ST_CR_SRVR_HELLO_A:
  228. case SSL3_ST_CR_SRVR_HELLO_B:
  229. ret = ssl3_get_server_hello(s);
  230. if (ret <= 0) {
  231. goto end;
  232. }
  233. if (s->hit) {
  234. s->state = SSL3_ST_CR_CHANGE;
  235. if (s->tlsext_ticket_expected) {
  236. /* receive renewed session ticket */
  237. s->state = SSL3_ST_CR_SESSION_TICKET_A;
  238. }
  239. } else {
  240. s->state = SSL3_ST_CR_CERT_A;
  241. }
  242. s->init_num = 0;
  243. break;
  244. case SSL3_ST_CR_CERT_A:
  245. case SSL3_ST_CR_CERT_B:
  246. if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
  247. ret = ssl3_get_server_certificate(s);
  248. if (ret <= 0) {
  249. goto end;
  250. }
  251. if (s->s3->tmp.certificate_status_expected) {
  252. s->state = SSL3_ST_CR_CERT_STATUS_A;
  253. } else {
  254. s->state = SSL3_ST_CR_KEY_EXCH_A;
  255. }
  256. } else {
  257. skip = 1;
  258. s->state = SSL3_ST_CR_KEY_EXCH_A;
  259. }
  260. s->init_num = 0;
  261. break;
  262. case SSL3_ST_CR_KEY_EXCH_A:
  263. case SSL3_ST_CR_KEY_EXCH_B:
  264. ret = ssl3_get_server_key_exchange(s);
  265. if (ret <= 0) {
  266. goto end;
  267. }
  268. s->state = SSL3_ST_CR_CERT_REQ_A;
  269. s->init_num = 0;
  270. break;
  271. case SSL3_ST_CR_CERT_REQ_A:
  272. case SSL3_ST_CR_CERT_REQ_B:
  273. ret = ssl3_get_certificate_request(s);
  274. if (ret <= 0) {
  275. goto end;
  276. }
  277. s->state = SSL3_ST_CR_SRVR_DONE_A;
  278. s->init_num = 0;
  279. break;
  280. case SSL3_ST_CR_SRVR_DONE_A:
  281. case SSL3_ST_CR_SRVR_DONE_B:
  282. ret = ssl3_get_server_done(s);
  283. if (ret <= 0) {
  284. goto end;
  285. }
  286. if (s->s3->tmp.cert_req) {
  287. s->state = SSL3_ST_CW_CERT_A;
  288. } else {
  289. s->state = SSL3_ST_CW_KEY_EXCH_A;
  290. }
  291. s->init_num = 0;
  292. break;
  293. case SSL3_ST_CW_CERT_A:
  294. case SSL3_ST_CW_CERT_B:
  295. case SSL3_ST_CW_CERT_C:
  296. case SSL3_ST_CW_CERT_D:
  297. ret = ssl3_send_client_certificate(s);
  298. if (ret <= 0) {
  299. goto end;
  300. }
  301. s->state = SSL3_ST_CW_KEY_EXCH_A;
  302. s->init_num = 0;
  303. break;
  304. case SSL3_ST_CW_KEY_EXCH_A:
  305. case SSL3_ST_CW_KEY_EXCH_B:
  306. ret = ssl3_send_client_key_exchange(s);
  307. if (ret <= 0) {
  308. goto end;
  309. }
  310. /* For TLS, cert_req is set to 2, so a cert chain
  311. * of nothing is sent, but no verify packet is sent */
  312. if (s->s3->tmp.cert_req == 1) {
  313. s->state = SSL3_ST_CW_CERT_VRFY_A;
  314. } else {
  315. s->state = SSL3_ST_CW_CHANGE_A;
  316. s->s3->change_cipher_spec = 0;
  317. }
  318. s->init_num = 0;
  319. break;
  320. case SSL3_ST_CW_CERT_VRFY_A:
  321. case SSL3_ST_CW_CERT_VRFY_B:
  322. case SSL3_ST_CW_CERT_VRFY_C:
  323. ret = ssl3_send_cert_verify(s);
  324. if (ret <= 0) {
  325. goto end;
  326. }
  327. s->state = SSL3_ST_CW_CHANGE_A;
  328. s->init_num = 0;
  329. s->s3->change_cipher_spec = 0;
  330. break;
  331. case SSL3_ST_CW_CHANGE_A:
  332. case SSL3_ST_CW_CHANGE_B:
  333. ret = ssl3_send_change_cipher_spec(s, SSL3_ST_CW_CHANGE_A,
  334. SSL3_ST_CW_CHANGE_B);
  335. if (ret <= 0) {
  336. goto end;
  337. }
  338. s->state = SSL3_ST_CW_FINISHED_A;
  339. if (s->s3->tlsext_channel_id_valid) {
  340. s->state = SSL3_ST_CW_CHANNEL_ID_A;
  341. }
  342. if (s->s3->next_proto_neg_seen) {
  343. s->state = SSL3_ST_CW_NEXT_PROTO_A;
  344. }
  345. s->init_num = 0;
  346. s->session->cipher = s->s3->tmp.new_cipher;
  347. if (!s->enc_method->setup_key_block(s) ||
  348. !s->enc_method->change_cipher_state(
  349. s, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
  350. ret = -1;
  351. goto end;
  352. }
  353. break;
  354. case SSL3_ST_CW_NEXT_PROTO_A:
  355. case SSL3_ST_CW_NEXT_PROTO_B:
  356. ret = ssl3_send_next_proto(s);
  357. if (ret <= 0) {
  358. goto end;
  359. }
  360. if (s->s3->tlsext_channel_id_valid) {
  361. s->state = SSL3_ST_CW_CHANNEL_ID_A;
  362. } else {
  363. s->state = SSL3_ST_CW_FINISHED_A;
  364. }
  365. break;
  366. case SSL3_ST_CW_CHANNEL_ID_A:
  367. case SSL3_ST_CW_CHANNEL_ID_B:
  368. ret = ssl3_send_channel_id(s);
  369. if (ret <= 0) {
  370. goto end;
  371. }
  372. s->state = SSL3_ST_CW_FINISHED_A;
  373. break;
  374. case SSL3_ST_CW_FINISHED_A:
  375. case SSL3_ST_CW_FINISHED_B:
  376. ret =
  377. ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B,
  378. s->enc_method->client_finished_label,
  379. s->enc_method->client_finished_label_len);
  380. if (ret <= 0) {
  381. goto end;
  382. }
  383. s->state = SSL3_ST_CW_FLUSH;
  384. if (s->hit) {
  385. s->s3->tmp.next_state = SSL_ST_OK;
  386. } else {
  387. /* This is a non-resumption handshake. If it involves ChannelID, then
  388. * record the handshake hashes at this point in the session so that
  389. * any resumption of this session with ChannelID can sign those
  390. * hashes. */
  391. if (s->s3->tlsext_channel_id_new) {
  392. ret = tls1_record_handshake_hashes_for_channel_id(s);
  393. if (ret <= 0) {
  394. goto end;
  395. }
  396. }
  397. if ((SSL_get_mode(s) & SSL_MODE_ENABLE_FALSE_START) &&
  398. ssl3_can_false_start(s) &&
  399. /* No False Start on renegotiation (would complicate the state
  400. * machine). */
  401. !s->s3->initial_handshake_complete) {
  402. s->s3->tmp.next_state = SSL3_ST_FALSE_START;
  403. } else {
  404. /* Allow NewSessionTicket if ticket expected */
  405. if (s->tlsext_ticket_expected) {
  406. s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
  407. } else {
  408. s->s3->tmp.next_state = SSL3_ST_CR_CHANGE;
  409. }
  410. }
  411. }
  412. s->init_num = 0;
  413. break;
  414. case SSL3_ST_CR_SESSION_TICKET_A:
  415. case SSL3_ST_CR_SESSION_TICKET_B:
  416. ret = ssl3_get_new_session_ticket(s);
  417. if (ret <= 0) {
  418. goto end;
  419. }
  420. s->state = SSL3_ST_CR_CHANGE;
  421. s->init_num = 0;
  422. break;
  423. case SSL3_ST_CR_CERT_STATUS_A:
  424. case SSL3_ST_CR_CERT_STATUS_B:
  425. ret = ssl3_get_cert_status(s);
  426. if (ret <= 0) {
  427. goto end;
  428. }
  429. s->state = SSL3_ST_CR_KEY_EXCH_A;
  430. s->init_num = 0;
  431. break;
  432. case SSL3_ST_CR_CHANGE:
  433. /* At this point, the next message must be entirely behind a
  434. * ChangeCipherSpec. */
  435. if (!ssl3_expect_change_cipher_spec(s)) {
  436. ret = -1;
  437. goto end;
  438. }
  439. s->state = SSL3_ST_CR_FINISHED_A;
  440. break;
  441. case SSL3_ST_CR_FINISHED_A:
  442. case SSL3_ST_CR_FINISHED_B:
  443. ret =
  444. ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B);
  445. if (ret <= 0) {
  446. goto end;
  447. }
  448. if (s->hit) {
  449. s->state = SSL3_ST_CW_CHANGE_A;
  450. } else {
  451. s->state = SSL_ST_OK;
  452. }
  453. s->init_num = 0;
  454. break;
  455. case SSL3_ST_CW_FLUSH:
  456. s->rwstate = SSL_WRITING;
  457. if (BIO_flush(s->wbio) <= 0) {
  458. ret = -1;
  459. goto end;
  460. }
  461. s->rwstate = SSL_NOTHING;
  462. s->state = s->s3->tmp.next_state;
  463. break;
  464. case SSL3_ST_FALSE_START:
  465. /* Allow NewSessionTicket if ticket expected */
  466. if (s->tlsext_ticket_expected) {
  467. s->state = SSL3_ST_CR_SESSION_TICKET_A;
  468. } else {
  469. s->state = SSL3_ST_CR_CHANGE;
  470. }
  471. s->s3->tmp.in_false_start = 1;
  472. ssl_free_wbio_buffer(s);
  473. ret = 1;
  474. goto end;
  475. case SSL_ST_OK:
  476. /* clean a few things up */
  477. ssl3_cleanup_key_block(s);
  478. BUF_MEM_free(s->init_buf);
  479. s->init_buf = NULL;
  480. /* Remove write buffering now. */
  481. ssl_free_wbio_buffer(s);
  482. const int is_initial_handshake = !s->s3->initial_handshake_complete;
  483. s->init_num = 0;
  484. s->s3->tmp.in_false_start = 0;
  485. s->s3->initial_handshake_complete = 1;
  486. if (is_initial_handshake) {
  487. /* Renegotiations do not participate in session resumption. */
  488. ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
  489. }
  490. ret = 1;
  491. /* s->server=0; */
  492. if (cb != NULL) {
  493. cb(s, SSL_CB_HANDSHAKE_DONE, 1);
  494. }
  495. goto end;
  496. default:
  497. OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE);
  498. ret = -1;
  499. goto end;
  500. }
  501. if (!s->s3->tmp.reuse_message && !skip) {
  502. if (cb != NULL && s->state != state) {
  503. new_state = s->state;
  504. s->state = state;
  505. cb(s, SSL_CB_CONNECT_LOOP, 1);
  506. s->state = new_state;
  507. }
  508. }
  509. skip = 0;
  510. }
  511. end:
  512. s->in_handshake--;
  513. BUF_MEM_free(buf);
  514. if (cb != NULL) {
  515. cb(s, SSL_CB_CONNECT_EXIT, ret);
  516. }
  517. return ret;
  518. }
  519. int ssl3_send_client_hello(SSL *s) {
  520. uint8_t *buf, *p, *d;
  521. int i;
  522. unsigned long l;
  523. buf = (uint8_t *)s->init_buf->data;
  524. if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
  525. if (!s->s3->have_version) {
  526. uint16_t max_version = ssl3_get_max_client_version(s);
  527. /* Disabling all versions is silly: return an error. */
  528. if (max_version == 0) {
  529. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_WRONG_SSL_VERSION);
  530. goto err;
  531. }
  532. s->version = max_version;
  533. s->client_version = max_version;
  534. }
  535. /* If the configured session was created at a version higher than our
  536. * maximum version, drop it. */
  537. if (s->session &&
  538. (s->session->session_id_length == 0 || s->session->not_resumable ||
  539. (!SSL_IS_DTLS(s) && s->session->ssl_version > s->version) ||
  540. (SSL_IS_DTLS(s) && s->session->ssl_version < s->version))) {
  541. SSL_set_session(s, NULL);
  542. }
  543. /* else use the pre-loaded session */
  544. p = s->s3->client_random;
  545. /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
  546. * renegerate the client_random. The random must be reused. */
  547. if ((!SSL_IS_DTLS(s) || !s->d1->send_cookie) &&
  548. !ssl_fill_hello_random(p, sizeof(s->s3->client_random),
  549. 0 /* client */)) {
  550. goto err;
  551. }
  552. /* Do the message type and length last. Note: the final argument to
  553. * ssl_add_clienthello_tlsext below depends on the size of this prefix. */
  554. d = p = ssl_handshake_start(s);
  555. /* version indicates the negotiated version: for example from an SSLv2/v3
  556. * compatible client hello). The client_version field is the maximum
  557. * version we permit and it is also used in RSA encrypted premaster
  558. * secrets. Some servers can choke if we initially report a higher version
  559. * then renegotiate to a lower one in the premaster secret. This didn't
  560. * happen with TLS 1.0 as most servers supported it but it can with TLS 1.1
  561. * or later if the server only supports 1.0.
  562. *
  563. * Possible scenario with previous logic:
  564. * 1. Client hello indicates TLS 1.2
  565. * 2. Server hello says TLS 1.0
  566. * 3. RSA encrypted premaster secret uses 1.2.
  567. * 4. Handhaked proceeds using TLS 1.0.
  568. * 5. Server sends hello request to renegotiate.
  569. * 6. Client hello indicates TLS v1.0 as we now
  570. * know that is maximum server supports.
  571. * 7. Server chokes on RSA encrypted premaster secret
  572. * containing version 1.0.
  573. *
  574. * For interoperability it should be OK to always use the maximum version
  575. * we support in client hello and then rely on the checking of version to
  576. * ensure the servers isn't being inconsistent: for example initially
  577. * negotiating with TLS 1.0 and renegotiating with TLS 1.2. We do this by
  578. * using client_version in client hello and not resetting it to the
  579. * negotiated version. */
  580. *(p++) = s->client_version >> 8;
  581. *(p++) = s->client_version & 0xff;
  582. /* Random stuff */
  583. memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
  584. p += SSL3_RANDOM_SIZE;
  585. /* Session ID */
  586. if (s->s3->initial_handshake_complete || s->session == NULL) {
  587. /* Renegotiations do not participate in session resumption. */
  588. i = 0;
  589. } else {
  590. i = s->session->session_id_length;
  591. }
  592. *(p++) = i;
  593. if (i != 0) {
  594. if (i > (int)sizeof(s->session->session_id)) {
  595. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
  596. goto err;
  597. }
  598. memcpy(p, s->session->session_id, i);
  599. p += i;
  600. }
  601. /* cookie stuff for DTLS */
  602. if (SSL_IS_DTLS(s)) {
  603. if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
  604. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
  605. goto err;
  606. }
  607. *(p++) = s->d1->cookie_len;
  608. memcpy(p, s->d1->cookie, s->d1->cookie_len);
  609. p += s->d1->cookie_len;
  610. }
  611. /* Ciphers supported */
  612. i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
  613. if (i == 0) {
  614. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello,
  615. SSL_R_NO_CIPHERS_AVAILABLE);
  616. goto err;
  617. }
  618. s2n(i, p);
  619. p += i;
  620. /* COMPRESSION */
  621. *(p++) = 1;
  622. *(p++) = 0; /* Add the NULL method */
  623. /* TLS extensions*/
  624. if (ssl_prepare_clienthello_tlsext(s) <= 0) {
  625. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
  626. goto err;
  627. }
  628. p = ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
  629. p - buf);
  630. if (p == NULL) {
  631. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
  632. goto err;
  633. }
  634. l = p - d;
  635. if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
  636. goto err;
  637. }
  638. s->state = SSL3_ST_CW_CLNT_HELLO_B;
  639. }
  640. /* SSL3_ST_CW_CLNT_HELLO_B */
  641. return ssl_do_write(s);
  642. err:
  643. return -1;
  644. }
  645. int ssl3_get_server_hello(SSL *s) {
  646. STACK_OF(SSL_CIPHER) *sk;
  647. const SSL_CIPHER *c;
  648. CERT *ct = s->cert;
  649. int al = SSL_AD_INTERNAL_ERROR, ok;
  650. long n;
  651. CBS server_hello, server_random, session_id;
  652. uint16_t server_version, cipher_suite;
  653. uint8_t compression_method;
  654. uint32_t mask_ssl;
  655. n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_HELLO_A,
  656. SSL3_ST_CR_SRVR_HELLO_B, SSL3_MT_SERVER_HELLO,
  657. 20000, /* ?? */
  658. ssl_hash_message, &ok);
  659. if (!ok) {
  660. uint32_t err = ERR_peek_error();
  661. if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
  662. ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
  663. /* Add a dedicated error code to the queue for a handshake_failure alert
  664. * in response to ClientHello. This matches NSS's client behavior and
  665. * gives a better error on a (probable) failure to negotiate initial
  666. * parameters. Note: this error code comes after the original one.
  667. *
  668. * See https://crbug.com/446505. */
  669. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
  670. SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
  671. }
  672. return n;
  673. }
  674. CBS_init(&server_hello, s->init_msg, n);
  675. if (!CBS_get_u16(&server_hello, &server_version) ||
  676. !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
  677. !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
  678. CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
  679. !CBS_get_u16(&server_hello, &cipher_suite) ||
  680. !CBS_get_u8(&server_hello, &compression_method)) {
  681. al = SSL_AD_DECODE_ERROR;
  682. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_DECODE_ERROR);
  683. goto f_err;
  684. }
  685. assert(s->s3->have_version == s->s3->initial_handshake_complete);
  686. if (!s->s3->have_version) {
  687. if (!ssl3_is_version_enabled(s, server_version)) {
  688. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_PROTOCOL);
  689. s->version = server_version;
  690. /* Mark the version as fixed so the record-layer version is not clamped
  691. * to TLS 1.0. */
  692. s->s3->have_version = 1;
  693. al = SSL_AD_PROTOCOL_VERSION;
  694. goto f_err;
  695. }
  696. s->version = server_version;
  697. s->enc_method = ssl3_get_enc_method(server_version);
  698. assert(s->enc_method != NULL);
  699. /* At this point, the connection's version is known and s->version is
  700. * fixed. Begin enforcing the record-layer version. */
  701. s->s3->have_version = 1;
  702. } else if (server_version != s->version) {
  703. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
  704. al = SSL_AD_PROTOCOL_VERSION;
  705. goto f_err;
  706. }
  707. /* Copy over the server random. */
  708. memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
  709. assert(s->session == NULL || s->session->session_id_length > 0);
  710. if (!s->s3->initial_handshake_complete && s->session != NULL &&
  711. CBS_mem_equal(&session_id, s->session->session_id,
  712. s->session->session_id_length)) {
  713. if (s->sid_ctx_length != s->session->sid_ctx_length ||
  714. memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
  715. /* actually a client application bug */
  716. al = SSL_AD_ILLEGAL_PARAMETER;
  717. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
  718. SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  719. goto f_err;
  720. }
  721. s->hit = 1;
  722. } else {
  723. /* The session wasn't resumed. Create a fresh SSL_SESSION to
  724. * fill out. */
  725. s->hit = 0;
  726. if (!ssl_get_new_session(s, 0)) {
  727. goto f_err;
  728. }
  729. /* Note: session_id could be empty. */
  730. s->session->session_id_length = CBS_len(&session_id);
  731. memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id));
  732. }
  733. c = SSL_get_cipher_by_value(cipher_suite);
  734. if (c == NULL) {
  735. /* unknown cipher */
  736. al = SSL_AD_ILLEGAL_PARAMETER;
  737. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
  738. SSL_R_UNKNOWN_CIPHER_RETURNED);
  739. goto f_err;
  740. }
  741. /* ct->mask_ssl was computed from client capabilities. Now
  742. * that the final version is known, compute a new mask_ssl. */
  743. if (!SSL_USE_TLS1_2_CIPHERS(s)) {
  744. mask_ssl = SSL_TLSV1_2;
  745. } else {
  746. mask_ssl = 0;
  747. }
  748. /* If the cipher is disabled then we didn't sent it in the ClientHello, so if
  749. * the server selected it, it's an error. */
  750. if ((c->algorithm_ssl & mask_ssl) ||
  751. (c->algorithm_mkey & ct->mask_k) ||
  752. (c->algorithm_auth & ct->mask_a)) {
  753. al = SSL_AD_ILLEGAL_PARAMETER;
  754. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
  755. goto f_err;
  756. }
  757. sk = ssl_get_ciphers_by_id(s);
  758. if (!sk_SSL_CIPHER_find(sk, NULL, c)) {
  759. /* we did not say we would use this cipher */
  760. al = SSL_AD_ILLEGAL_PARAMETER;
  761. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
  762. goto f_err;
  763. }
  764. if (s->hit) {
  765. if (s->session->cipher != c) {
  766. al = SSL_AD_ILLEGAL_PARAMETER;
  767. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
  768. SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  769. goto f_err;
  770. }
  771. if (s->session->ssl_version != s->version) {
  772. al = SSL_AD_ILLEGAL_PARAMETER;
  773. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
  774. SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
  775. goto f_err;
  776. }
  777. }
  778. s->s3->tmp.new_cipher = c;
  779. /* If doing a full handshake with TLS 1.2, the server may request a client
  780. * certificate which requires hashing the handshake transcript under a
  781. * different hash. Otherwise, release the handshake buffer. */
  782. if ((!SSL_USE_SIGALGS(s) || s->hit) &&
  783. !ssl3_digest_cached_records(s, free_handshake_buffer)) {
  784. goto f_err;
  785. }
  786. /* Only the NULL compression algorithm is supported. */
  787. if (compression_method != 0) {
  788. al = SSL_AD_ILLEGAL_PARAMETER;
  789. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
  790. SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  791. goto f_err;
  792. }
  793. /* TLS extensions */
  794. if (!ssl_parse_serverhello_tlsext(s, &server_hello)) {
  795. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
  796. goto err;
  797. }
  798. /* There should be nothing left over in the record. */
  799. if (CBS_len(&server_hello) != 0) {
  800. /* wrong packet length */
  801. al = SSL_AD_DECODE_ERROR;
  802. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
  803. goto f_err;
  804. }
  805. if (s->hit &&
  806. s->s3->tmp.extended_master_secret != s->session->extended_master_secret) {
  807. al = SSL_AD_HANDSHAKE_FAILURE;
  808. if (s->session->extended_master_secret) {
  809. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
  810. SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
  811. } else {
  812. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
  813. SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
  814. }
  815. goto f_err;
  816. }
  817. return 1;
  818. f_err:
  819. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  820. err:
  821. return -1;
  822. }
  823. /* ssl3_check_certificate_for_cipher returns one if |leaf| is a suitable server
  824. * certificate type for |cipher|. Otherwise, it returns zero and pushes an error
  825. * on the error queue. */
  826. static int ssl3_check_certificate_for_cipher(X509 *leaf,
  827. const SSL_CIPHER *cipher) {
  828. int ret = 0;
  829. EVP_PKEY *pkey = X509_get_pubkey(leaf);
  830. if (pkey == NULL) {
  831. goto err;
  832. }
  833. /* Check the certificate's type matches the cipher. */
  834. int cert_type = ssl_cert_type(pkey);
  835. if (cert_type < 0) {
  836. OPENSSL_PUT_ERROR(SSL, ssl3_check_certificate_for_cipher,
  837. SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  838. goto err;
  839. }
  840. int expected_type = ssl_cipher_get_cert_index(cipher);
  841. assert(expected_type >= 0);
  842. if (cert_type != expected_type) {
  843. OPENSSL_PUT_ERROR(SSL, ssl3_check_certificate_for_cipher,
  844. SSL_R_WRONG_CERTIFICATE_TYPE);
  845. goto err;
  846. }
  847. /* TODO(davidben): This behavior is preserved from upstream. Should key usages
  848. * be checked in other cases as well? */
  849. if (cipher->algorithm_auth & SSL_aECDSA) {
  850. /* This call populates the ex_flags field correctly */
  851. X509_check_purpose(leaf, -1, 0);
  852. if ((leaf->ex_flags & EXFLAG_KUSAGE) &&
  853. !(leaf->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)) {
  854. OPENSSL_PUT_ERROR(SSL, ssl3_check_certificate_for_cipher,
  855. SSL_R_ECC_CERT_NOT_FOR_SIGNING);
  856. goto err;
  857. }
  858. }
  859. ret = 1;
  860. err:
  861. EVP_PKEY_free(pkey);
  862. return ret;
  863. }
  864. int ssl3_get_server_certificate(SSL *s) {
  865. int al, i, ok, ret = -1;
  866. unsigned long n;
  867. X509 *x = NULL;
  868. STACK_OF(X509) *sk = NULL;
  869. SESS_CERT *sc;
  870. EVP_PKEY *pkey = NULL;
  871. CBS cbs, certificate_list;
  872. const uint8_t *data;
  873. n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B,
  874. SSL3_MT_CERTIFICATE, (long)s->max_cert_list,
  875. ssl_hash_message, &ok);
  876. if (!ok) {
  877. return n;
  878. }
  879. CBS_init(&cbs, s->init_msg, n);
  880. sk = sk_X509_new_null();
  881. if (sk == NULL) {
  882. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
  883. goto err;
  884. }
  885. if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
  886. CBS_len(&certificate_list) == 0 ||
  887. CBS_len(&cbs) != 0) {
  888. al = SSL_AD_DECODE_ERROR;
  889. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_DECODE_ERROR);
  890. goto f_err;
  891. }
  892. while (CBS_len(&certificate_list) > 0) {
  893. CBS certificate;
  894. if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
  895. al = SSL_AD_DECODE_ERROR;
  896. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
  897. SSL_R_CERT_LENGTH_MISMATCH);
  898. goto f_err;
  899. }
  900. data = CBS_data(&certificate);
  901. x = d2i_X509(NULL, &data, CBS_len(&certificate));
  902. if (x == NULL) {
  903. al = SSL_AD_BAD_CERTIFICATE;
  904. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
  905. goto f_err;
  906. }
  907. if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
  908. al = SSL_AD_DECODE_ERROR;
  909. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
  910. SSL_R_CERT_LENGTH_MISMATCH);
  911. goto f_err;
  912. }
  913. if (!sk_X509_push(sk, x)) {
  914. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
  915. goto err;
  916. }
  917. x = NULL;
  918. }
  919. i = ssl_verify_cert_chain(s, sk);
  920. if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
  921. al = ssl_verify_alarm_type(s->verify_result);
  922. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
  923. SSL_R_CERTIFICATE_VERIFY_FAILED);
  924. goto f_err;
  925. }
  926. ERR_clear_error(); /* but we keep s->verify_result */
  927. X509 *leaf = sk_X509_value(sk, 0);
  928. if (!ssl3_check_certificate_for_cipher(leaf, s->s3->tmp.new_cipher)) {
  929. al = SSL_AD_ILLEGAL_PARAMETER;
  930. goto f_err;
  931. }
  932. sc = ssl_sess_cert_new();
  933. if (sc == NULL) {
  934. goto err;
  935. }
  936. ssl_sess_cert_free(s->session->sess_cert);
  937. s->session->sess_cert = sc;
  938. /* NOTE: Unlike the server half, the client's copy of |cert_chain| includes
  939. * the leaf. */
  940. sc->cert_chain = sk;
  941. sk = NULL;
  942. X509_free(sc->peer_cert);
  943. sc->peer_cert = X509_up_ref(leaf);
  944. X509_free(s->session->peer);
  945. s->session->peer = X509_up_ref(leaf);
  946. s->session->verify_result = s->verify_result;
  947. ret = 1;
  948. if (0) {
  949. f_err:
  950. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  951. }
  952. err:
  953. EVP_PKEY_free(pkey);
  954. X509_free(x);
  955. sk_X509_pop_free(sk, X509_free);
  956. return ret;
  957. }
  958. int ssl3_get_server_key_exchange(SSL *s) {
  959. EVP_MD_CTX md_ctx;
  960. int al, ok;
  961. long n, alg_k, alg_a;
  962. EVP_PKEY *pkey = NULL;
  963. const EVP_MD *md = NULL;
  964. RSA *rsa = NULL;
  965. DH *dh = NULL;
  966. EC_KEY *ecdh = NULL;
  967. BN_CTX *bn_ctx = NULL;
  968. EC_POINT *srvr_ecpoint = NULL;
  969. CBS server_key_exchange, server_key_exchange_orig, parameter;
  970. /* use same message size as in ssl3_get_certificate_request() as
  971. * ServerKeyExchange message may be skipped */
  972. n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A,
  973. SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list,
  974. ssl_hash_message, &ok);
  975. if (!ok) {
  976. return n;
  977. }
  978. if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
  979. if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher)) {
  980. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  981. SSL_R_UNEXPECTED_MESSAGE);
  982. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  983. return -1;
  984. }
  985. /* In plain PSK ciphersuite, ServerKeyExchange can be
  986. omitted if no identity hint is sent. Set session->sess_cert anyway to
  987. avoid problems later.*/
  988. if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
  989. /* PSK ciphersuites that also send a Certificate would have already
  990. * initialized |sess_cert|. */
  991. if (s->session->sess_cert == NULL) {
  992. s->session->sess_cert = ssl_sess_cert_new();
  993. if (s->session->sess_cert == NULL) {
  994. return -1;
  995. }
  996. }
  997. /* TODO(davidben): This should be reset in one place with the rest of the
  998. * handshake state. */
  999. OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
  1000. s->s3->tmp.peer_psk_identity_hint = NULL;
  1001. }
  1002. s->s3->tmp.reuse_message = 1;
  1003. return 1;
  1004. }
  1005. /* Retain a copy of the original CBS to compute the signature over. */
  1006. CBS_init(&server_key_exchange, s->init_msg, n);
  1007. server_key_exchange_orig = server_key_exchange;
  1008. if (s->session->sess_cert != NULL) {
  1009. DH_free(s->session->sess_cert->peer_dh_tmp);
  1010. s->session->sess_cert->peer_dh_tmp = NULL;
  1011. EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
  1012. s->session->sess_cert->peer_ecdh_tmp = NULL;
  1013. } else {
  1014. s->session->sess_cert = ssl_sess_cert_new();
  1015. if (s->session->sess_cert == NULL) {
  1016. return -1;
  1017. }
  1018. }
  1019. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1020. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1021. EVP_MD_CTX_init(&md_ctx);
  1022. if (alg_a & SSL_aPSK) {
  1023. CBS psk_identity_hint;
  1024. /* Each of the PSK key exchanges begins with a psk_identity_hint. */
  1025. if (!CBS_get_u16_length_prefixed(&server_key_exchange,
  1026. &psk_identity_hint)) {
  1027. al = SSL_AD_DECODE_ERROR;
  1028. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
  1029. goto f_err;
  1030. }
  1031. /* Store PSK identity hint for later use, hint is used in
  1032. * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
  1033. * identity hint can be as long as the maximum length of a PSK identity.
  1034. * Also do not allow NULL characters; identities are saved as C strings.
  1035. *
  1036. * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
  1037. * a specific identity. */
  1038. if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
  1039. CBS_contains_zero_byte(&psk_identity_hint)) {
  1040. al = SSL_AD_HANDSHAKE_FAILURE;
  1041. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1042. SSL_R_DATA_LENGTH_TOO_LONG);
  1043. goto f_err;
  1044. }
  1045. /* Save the identity hint as a C string. */
  1046. if (!CBS_strdup(&psk_identity_hint, &s->s3->tmp.peer_psk_identity_hint)) {
  1047. al = SSL_AD_INTERNAL_ERROR;
  1048. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1049. ERR_R_MALLOC_FAILURE);
  1050. goto f_err;
  1051. }
  1052. }
  1053. if (alg_k & SSL_kDHE) {
  1054. CBS dh_p, dh_g, dh_Ys;
  1055. if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
  1056. CBS_len(&dh_p) == 0 ||
  1057. !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
  1058. CBS_len(&dh_g) == 0 ||
  1059. !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
  1060. CBS_len(&dh_Ys) == 0) {
  1061. al = SSL_AD_DECODE_ERROR;
  1062. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
  1063. goto f_err;
  1064. }
  1065. dh = DH_new();
  1066. if (dh == NULL) {
  1067. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
  1068. goto err;
  1069. }
  1070. if ((dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)) == NULL ||
  1071. (dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)) == NULL ||
  1072. (dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)) ==
  1073. NULL) {
  1074. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
  1075. goto err;
  1076. }
  1077. if (DH_num_bits(dh) < 1024) {
  1078. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1079. SSL_R_BAD_DH_P_LENGTH);
  1080. goto err;
  1081. }
  1082. s->session->sess_cert->peer_dh_tmp = dh;
  1083. dh = NULL;
  1084. } else if (alg_k & SSL_kECDHE) {
  1085. uint16_t curve_id;
  1086. int curve_nid = 0;
  1087. const EC_GROUP *group;
  1088. CBS point;
  1089. /* Extract elliptic curve parameters and the server's ephemeral ECDH public
  1090. * key. Check curve is one of our preferences, if not server has sent an
  1091. * invalid curve. */
  1092. if (!tls1_check_curve(s, &server_key_exchange, &curve_id)) {
  1093. al = SSL_AD_DECODE_ERROR;
  1094. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_WRONG_CURVE);
  1095. goto f_err;
  1096. }
  1097. curve_nid = tls1_ec_curve_id2nid(curve_id);
  1098. if (curve_nid == 0) {
  1099. al = SSL_AD_INTERNAL_ERROR;
  1100. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1101. SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
  1102. goto f_err;
  1103. }
  1104. ecdh = EC_KEY_new_by_curve_name(curve_nid);
  1105. if (ecdh == NULL) {
  1106. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1107. ERR_R_EC_LIB);
  1108. goto err;
  1109. }
  1110. group = EC_KEY_get0_group(ecdh);
  1111. /* Next, get the encoded ECPoint */
  1112. if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
  1113. al = SSL_AD_DECODE_ERROR;
  1114. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
  1115. goto f_err;
  1116. }
  1117. if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
  1118. ((bn_ctx = BN_CTX_new()) == NULL)) {
  1119. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1120. ERR_R_MALLOC_FAILURE);
  1121. goto err;
  1122. }
  1123. if (!EC_POINT_oct2point(group, srvr_ecpoint, CBS_data(&point),
  1124. CBS_len(&point), bn_ctx)) {
  1125. al = SSL_AD_DECODE_ERROR;
  1126. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT);
  1127. goto f_err;
  1128. }
  1129. EC_KEY_set_public_key(ecdh, srvr_ecpoint);
  1130. s->session->sess_cert->peer_ecdh_tmp = ecdh;
  1131. ecdh = NULL;
  1132. BN_CTX_free(bn_ctx);
  1133. bn_ctx = NULL;
  1134. EC_POINT_free(srvr_ecpoint);
  1135. srvr_ecpoint = NULL;
  1136. } else if (!(alg_k & SSL_kPSK)) {
  1137. al = SSL_AD_UNEXPECTED_MESSAGE;
  1138. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1139. SSL_R_UNEXPECTED_MESSAGE);
  1140. goto f_err;
  1141. }
  1142. /* At this point, |server_key_exchange| contains the signature, if any, while
  1143. * |server_key_exchange_orig| contains the entire message. From that, derive
  1144. * a CBS containing just the parameter. */
  1145. CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
  1146. CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
  1147. /* ServerKeyExchange should be signed by the server's public key. */
  1148. if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
  1149. pkey = X509_get_pubkey(s->session->sess_cert->peer_cert);
  1150. if (pkey == NULL) {
  1151. goto err;
  1152. }
  1153. if (SSL_USE_SIGALGS(s)) {
  1154. if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey)) {
  1155. goto f_err;
  1156. }
  1157. } else if (pkey->type == EVP_PKEY_RSA) {
  1158. md = EVP_md5_sha1();
  1159. } else {
  1160. md = EVP_sha1();
  1161. }
  1162. /* The last field in |server_key_exchange| is the signature. */
  1163. CBS signature;
  1164. if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
  1165. CBS_len(&server_key_exchange) != 0) {
  1166. al = SSL_AD_DECODE_ERROR;
  1167. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
  1168. goto f_err;
  1169. }
  1170. if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) ||
  1171. !EVP_DigestVerifyUpdate(&md_ctx, s->s3->client_random,
  1172. SSL3_RANDOM_SIZE) ||
  1173. !EVP_DigestVerifyUpdate(&md_ctx, s->s3->server_random,
  1174. SSL3_RANDOM_SIZE) ||
  1175. !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter),
  1176. CBS_len(&parameter)) ||
  1177. !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
  1178. CBS_len(&signature))) {
  1179. /* bad signature */
  1180. al = SSL_AD_DECRYPT_ERROR;
  1181. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
  1182. goto f_err;
  1183. }
  1184. } else {
  1185. /* PSK ciphers are the only supported certificate-less ciphers. */
  1186. assert(alg_a == SSL_aPSK);
  1187. if (CBS_len(&server_key_exchange) > 0) {
  1188. al = SSL_AD_DECODE_ERROR;
  1189. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1190. SSL_R_EXTRA_DATA_IN_MESSAGE);
  1191. goto f_err;
  1192. }
  1193. }
  1194. EVP_PKEY_free(pkey);
  1195. EVP_MD_CTX_cleanup(&md_ctx);
  1196. return 1;
  1197. f_err:
  1198. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1199. err:
  1200. EVP_PKEY_free(pkey);
  1201. RSA_free(rsa);
  1202. DH_free(dh);
  1203. BN_CTX_free(bn_ctx);
  1204. EC_POINT_free(srvr_ecpoint);
  1205. EC_KEY_free(ecdh);
  1206. EVP_MD_CTX_cleanup(&md_ctx);
  1207. return -1;
  1208. }
  1209. static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
  1210. return X509_NAME_cmp(*a, *b);
  1211. }
  1212. int ssl3_get_certificate_request(SSL *s) {
  1213. int ok, ret = 0;
  1214. unsigned long n;
  1215. X509_NAME *xn = NULL;
  1216. STACK_OF(X509_NAME) *ca_sk = NULL;
  1217. CBS cbs;
  1218. CBS certificate_types;
  1219. CBS certificate_authorities;
  1220. const uint8_t *data;
  1221. n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A,
  1222. SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list,
  1223. ssl_hash_message, &ok);
  1224. if (!ok) {
  1225. return n;
  1226. }
  1227. s->s3->tmp.cert_req = 0;
  1228. if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
  1229. s->s3->tmp.reuse_message = 1;
  1230. /* If we get here we don't need any cached handshake records as we wont be
  1231. * doing client auth. */
  1232. if (s->s3->handshake_buffer &&
  1233. !ssl3_digest_cached_records(s, free_handshake_buffer)) {
  1234. goto err;
  1235. }
  1236. return 1;
  1237. }
  1238. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
  1239. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  1240. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
  1241. SSL_R_WRONG_MESSAGE_TYPE);
  1242. goto err;
  1243. }
  1244. CBS_init(&cbs, s->init_msg, n);
  1245. ca_sk = sk_X509_NAME_new(ca_dn_cmp);
  1246. if (ca_sk == NULL) {
  1247. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
  1248. goto err;
  1249. }
  1250. /* get the certificate types */
  1251. if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
  1252. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1253. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
  1254. goto err;
  1255. }
  1256. if (!CBS_stow(&certificate_types, &s->s3->tmp.certificate_types,
  1257. &s->s3->tmp.num_certificate_types)) {
  1258. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1259. goto err;
  1260. }
  1261. if (SSL_USE_SIGALGS(s)) {
  1262. CBS supported_signature_algorithms;
  1263. if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms)) {
  1264. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1265. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
  1266. goto err;
  1267. }
  1268. if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
  1269. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1270. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
  1271. SSL_R_SIGNATURE_ALGORITHMS_ERROR);
  1272. goto err;
  1273. }
  1274. }
  1275. /* get the CA RDNs */
  1276. if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
  1277. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1278. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
  1279. goto err;
  1280. }
  1281. while (CBS_len(&certificate_authorities) > 0) {
  1282. CBS distinguished_name;
  1283. if (!CBS_get_u16_length_prefixed(&certificate_authorities,
  1284. &distinguished_name)) {
  1285. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1286. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
  1287. SSL_R_CA_DN_TOO_LONG);
  1288. goto err;
  1289. }
  1290. data = CBS_data(&distinguished_name);
  1291. xn = d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name));
  1292. if (xn == NULL) {
  1293. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1294. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
  1295. goto err;
  1296. }
  1297. if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) {
  1298. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1299. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_INTERNAL_ERROR);
  1300. goto err;
  1301. }
  1302. if (CBS_len(&distinguished_name) != 0) {
  1303. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1304. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
  1305. SSL_R_CA_DN_LENGTH_MISMATCH);
  1306. goto err;
  1307. }
  1308. if (!sk_X509_NAME_push(ca_sk, xn)) {
  1309. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
  1310. ERR_R_MALLOC_FAILURE);
  1311. goto err;
  1312. }
  1313. }
  1314. /* we should setup a certificate to return.... */
  1315. s->s3->tmp.cert_req = 1;
  1316. sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
  1317. s->s3->tmp.ca_names = ca_sk;
  1318. ca_sk = NULL;
  1319. ret = 1;
  1320. err:
  1321. sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
  1322. return ret;
  1323. }
  1324. int ssl3_get_new_session_ticket(SSL *s) {
  1325. int ok, al;
  1326. long n;
  1327. CBS new_session_ticket, ticket;
  1328. n = s->method->ssl_get_message(
  1329. s, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B,
  1330. SSL3_MT_NEWSESSION_TICKET, 16384, ssl_hash_message, &ok);
  1331. if (!ok) {
  1332. return n;
  1333. }
  1334. if (s->hit) {
  1335. /* The server is sending a new ticket for an existing session. Sessions are
  1336. * immutable once established, so duplicate all but the ticket of the
  1337. * existing session. */
  1338. uint8_t *bytes;
  1339. size_t bytes_len;
  1340. if (!SSL_SESSION_to_bytes_for_ticket(s->session, &bytes, &bytes_len)) {
  1341. goto err;
  1342. }
  1343. SSL_SESSION *new_session = SSL_SESSION_from_bytes(bytes, bytes_len);
  1344. OPENSSL_free(bytes);
  1345. if (new_session == NULL) {
  1346. /* This should never happen. */
  1347. OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_INTERNAL_ERROR);
  1348. goto err;
  1349. }
  1350. if (s->session->sess_cert != NULL) {
  1351. /* |sess_cert| is not serialized and must be duplicated explicitly. */
  1352. assert(new_session->sess_cert == NULL);
  1353. new_session->sess_cert = ssl_sess_cert_dup(s->session->sess_cert);
  1354. if (new_session->sess_cert == NULL) {
  1355. SSL_SESSION_free(new_session);
  1356. goto err;
  1357. }
  1358. }
  1359. SSL_SESSION_free(s->session);
  1360. s->session = new_session;
  1361. }
  1362. CBS_init(&new_session_ticket, s->init_msg, n);
  1363. if (!CBS_get_u32(&new_session_ticket,
  1364. &s->session->tlsext_tick_lifetime_hint) ||
  1365. !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
  1366. CBS_len(&new_session_ticket) != 0) {
  1367. al = SSL_AD_DECODE_ERROR;
  1368. OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
  1369. goto f_err;
  1370. }
  1371. if (!CBS_stow(&ticket, &s->session->tlsext_tick,
  1372. &s->session->tlsext_ticklen)) {
  1373. OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
  1374. goto err;
  1375. }
  1376. /* Generate a session ID for this session based on the session ticket. We use
  1377. * the session ID mechanism for detecting ticket resumption. This also fits in
  1378. * with assumptions elsewhere in OpenSSL.*/
  1379. if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), s->session->session_id,
  1380. &s->session->session_id_length, EVP_sha256(), NULL)) {
  1381. goto err;
  1382. }
  1383. return 1;
  1384. f_err:
  1385. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1386. err:
  1387. return -1;
  1388. }
  1389. int ssl3_get_cert_status(SSL *s) {
  1390. int ok, al;
  1391. long n;
  1392. CBS certificate_status, ocsp_response;
  1393. uint8_t status_type;
  1394. n = s->method->ssl_get_message(
  1395. s, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B,
  1396. -1, 16384, ssl_hash_message, &ok);
  1397. if (!ok) {
  1398. return n;
  1399. }
  1400. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
  1401. /* A server may send status_request in ServerHello and then change
  1402. * its mind about sending CertificateStatus. */
  1403. s->s3->tmp.reuse_message = 1;
  1404. return 1;
  1405. }
  1406. CBS_init(&certificate_status, s->init_msg, n);
  1407. if (!CBS_get_u8(&certificate_status, &status_type) ||
  1408. status_type != TLSEXT_STATUSTYPE_ocsp ||
  1409. !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
  1410. CBS_len(&ocsp_response) == 0 ||
  1411. CBS_len(&certificate_status) != 0) {
  1412. al = SSL_AD_DECODE_ERROR;
  1413. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
  1414. goto f_err;
  1415. }
  1416. if (!CBS_stow(&ocsp_response, &s->session->ocsp_response,
  1417. &s->session->ocsp_response_length)) {
  1418. al = SSL_AD_INTERNAL_ERROR;
  1419. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
  1420. goto f_err;
  1421. }
  1422. return 1;
  1423. f_err:
  1424. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1425. return -1;
  1426. }
  1427. int ssl3_get_server_done(SSL *s) {
  1428. int ok;
  1429. long n;
  1430. n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A,
  1431. SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
  1432. 30, /* should be very small, like 0 :-) */
  1433. ssl_hash_message, &ok);
  1434. if (!ok) {
  1435. return n;
  1436. }
  1437. if (n > 0) {
  1438. /* should contain no data */
  1439. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1440. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
  1441. return -1;
  1442. }
  1443. return 1;
  1444. }
  1445. int ssl3_send_client_key_exchange(SSL *s) {
  1446. uint8_t *p;
  1447. int n = 0;
  1448. uint32_t alg_k;
  1449. uint32_t alg_a;
  1450. uint8_t *q;
  1451. EVP_PKEY *pkey = NULL;
  1452. EC_KEY *clnt_ecdh = NULL;
  1453. const EC_POINT *srvr_ecpoint = NULL;
  1454. EVP_PKEY *srvr_pub_pkey = NULL;
  1455. uint8_t *encodedPoint = NULL;
  1456. int encoded_pt_len = 0;
  1457. BN_CTX *bn_ctx = NULL;
  1458. unsigned int psk_len = 0;
  1459. uint8_t psk[PSK_MAX_PSK_LEN];
  1460. uint8_t *pms = NULL;
  1461. size_t pms_len = 0;
  1462. if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
  1463. p = ssl_handshake_start(s);
  1464. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1465. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1466. /* If using a PSK key exchange, prepare the pre-shared key. */
  1467. if (alg_a & SSL_aPSK) {
  1468. char identity[PSK_MAX_IDENTITY_LEN + 1];
  1469. size_t identity_len;
  1470. if (s->psk_client_callback == NULL) {
  1471. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1472. SSL_R_PSK_NO_CLIENT_CB);
  1473. goto err;
  1474. }
  1475. memset(identity, 0, sizeof(identity));
  1476. psk_len =
  1477. s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint, identity,
  1478. sizeof(identity), psk, sizeof(psk));
  1479. if (psk_len > PSK_MAX_PSK_LEN) {
  1480. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1481. ERR_R_INTERNAL_ERROR);
  1482. goto err;
  1483. } else if (psk_len == 0) {
  1484. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1485. SSL_R_PSK_IDENTITY_NOT_FOUND);
  1486. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1487. goto err;
  1488. }
  1489. identity_len = OPENSSL_strnlen(identity, sizeof(identity));
  1490. if (identity_len > PSK_MAX_IDENTITY_LEN) {
  1491. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1492. ERR_R_INTERNAL_ERROR);
  1493. goto err;
  1494. }
  1495. OPENSSL_free(s->session->psk_identity);
  1496. s->session->psk_identity = BUF_strdup(identity);
  1497. if (s->session->psk_identity == NULL) {
  1498. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1499. ERR_R_MALLOC_FAILURE);
  1500. goto err;
  1501. }
  1502. /* Write out psk_identity. */
  1503. s2n(identity_len, p);
  1504. memcpy(p, identity, identity_len);
  1505. p += identity_len;
  1506. n = 2 + identity_len;
  1507. }
  1508. /* Depending on the key exchange method, compute |pms| and |pms_len|. */
  1509. if (alg_k & SSL_kRSA) {
  1510. RSA *rsa;
  1511. size_t enc_pms_len;
  1512. pms_len = SSL_MAX_MASTER_KEY_LENGTH;
  1513. pms = OPENSSL_malloc(pms_len);
  1514. if (pms == NULL) {
  1515. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1516. ERR_R_MALLOC_FAILURE);
  1517. goto err;
  1518. }
  1519. if (s->session->sess_cert == NULL) {
  1520. /* We should always have a server certificate with SSL_kRSA. */
  1521. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1522. ERR_R_INTERNAL_ERROR);
  1523. goto err;
  1524. }
  1525. pkey = X509_get_pubkey(s->session->sess_cert->peer_cert);
  1526. if (pkey == NULL ||
  1527. pkey->type != EVP_PKEY_RSA ||
  1528. pkey->pkey.rsa == NULL) {
  1529. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1530. ERR_R_INTERNAL_ERROR);
  1531. EVP_PKEY_free(pkey);
  1532. goto err;
  1533. }
  1534. rsa = pkey->pkey.rsa;
  1535. EVP_PKEY_free(pkey);
  1536. pms[0] = s->client_version >> 8;
  1537. pms[1] = s->client_version & 0xff;
  1538. if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
  1539. goto err;
  1540. }
  1541. s->session->master_key_length = SSL_MAX_MASTER_KEY_LENGTH;
  1542. q = p;
  1543. /* In TLS and beyond, reserve space for the length prefix. */
  1544. if (s->version > SSL3_VERSION) {
  1545. p += 2;
  1546. n += 2;
  1547. }
  1548. if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa), pms, pms_len,
  1549. RSA_PKCS1_PADDING)) {
  1550. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1551. SSL_R_BAD_RSA_ENCRYPT);
  1552. goto err;
  1553. }
  1554. n += enc_pms_len;
  1555. /* Log the premaster secret, if logging is enabled. */
  1556. if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx, p, enc_pms_len, pms,
  1557. pms_len)) {
  1558. goto err;
  1559. }
  1560. /* Fill in the length prefix. */
  1561. if (s->version > SSL3_VERSION) {
  1562. s2n(enc_pms_len, q);
  1563. }
  1564. } else if (alg_k & SSL_kDHE) {
  1565. DH *dh_srvr, *dh_clnt;
  1566. SESS_CERT *scert = s->session->sess_cert;
  1567. int dh_len;
  1568. size_t pub_len;
  1569. if (scert == NULL) {
  1570. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  1571. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1572. SSL_R_UNEXPECTED_MESSAGE);
  1573. goto err;
  1574. }
  1575. if (scert->peer_dh_tmp == NULL) {
  1576. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1577. ERR_R_INTERNAL_ERROR);
  1578. goto err;
  1579. }
  1580. dh_srvr = scert->peer_dh_tmp;
  1581. /* generate a new random key */
  1582. dh_clnt = DHparams_dup(dh_srvr);
  1583. if (dh_clnt == NULL) {
  1584. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
  1585. goto err;
  1586. }
  1587. if (!DH_generate_key(dh_clnt)) {
  1588. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
  1589. DH_free(dh_clnt);
  1590. goto err;
  1591. }
  1592. pms_len = DH_size(dh_clnt);
  1593. pms = OPENSSL_malloc(pms_len);
  1594. if (pms == NULL) {
  1595. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1596. ERR_R_MALLOC_FAILURE);
  1597. DH_free(dh_clnt);
  1598. goto err;
  1599. }
  1600. dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
  1601. if (dh_len <= 0) {
  1602. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
  1603. DH_free(dh_clnt);
  1604. goto err;
  1605. }
  1606. pms_len = dh_len;
  1607. /* send off the data */
  1608. pub_len = BN_num_bytes(dh_clnt->pub_key);
  1609. s2n(pub_len, p);
  1610. BN_bn2bin(dh_clnt->pub_key, p);
  1611. n += 2 + pub_len;
  1612. DH_free(dh_clnt);
  1613. } else if (alg_k & SSL_kECDHE) {
  1614. const EC_GROUP *srvr_group = NULL;
  1615. EC_KEY *tkey;
  1616. int field_size = 0, ecdh_len;
  1617. if (s->session->sess_cert == NULL) {
  1618. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  1619. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1620. SSL_R_UNEXPECTED_MESSAGE);
  1621. goto err;
  1622. }
  1623. if (s->session->sess_cert->peer_ecdh_tmp == NULL) {
  1624. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1625. ERR_R_INTERNAL_ERROR);
  1626. goto err;
  1627. }
  1628. tkey = s->session->sess_cert->peer_ecdh_tmp;
  1629. srvr_group = EC_KEY_get0_group(tkey);
  1630. srvr_ecpoint = EC_KEY_get0_public_key(tkey);
  1631. if (srvr_group == NULL || srvr_ecpoint == NULL) {
  1632. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1633. ERR_R_INTERNAL_ERROR);
  1634. goto err;
  1635. }
  1636. clnt_ecdh = EC_KEY_new();
  1637. if (clnt_ecdh == NULL) {
  1638. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1639. ERR_R_MALLOC_FAILURE);
  1640. goto err;
  1641. }
  1642. if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
  1643. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
  1644. goto err;
  1645. }
  1646. /* Generate a new ECDH key pair */
  1647. if (!EC_KEY_generate_key(clnt_ecdh)) {
  1648. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
  1649. goto err;
  1650. }
  1651. field_size = EC_GROUP_get_degree(srvr_group);
  1652. if (field_size <= 0) {
  1653. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
  1654. goto err;
  1655. }
  1656. pms_len = (field_size + 7) / 8;
  1657. pms = OPENSSL_malloc(pms_len);
  1658. if (pms == NULL) {
  1659. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1660. ERR_R_MALLOC_FAILURE);
  1661. goto err;
  1662. }
  1663. ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL);
  1664. if (ecdh_len <= 0) {
  1665. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
  1666. goto err;
  1667. }
  1668. pms_len = ecdh_len;
  1669. /* First check the size of encoding and allocate memory accordingly. */
  1670. encoded_pt_len =
  1671. EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
  1672. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
  1673. encodedPoint =
  1674. (uint8_t *)OPENSSL_malloc(encoded_pt_len * sizeof(uint8_t));
  1675. bn_ctx = BN_CTX_new();
  1676. if (encodedPoint == NULL || bn_ctx == NULL) {
  1677. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1678. ERR_R_MALLOC_FAILURE);
  1679. goto err;
  1680. }
  1681. /* Encode the public key */
  1682. encoded_pt_len = EC_POINT_point2oct(
  1683. srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
  1684. POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encoded_pt_len, bn_ctx);
  1685. *p = encoded_pt_len; /* length of encoded point */
  1686. /* Encoded point will be copied here */
  1687. p += 1;
  1688. n += 1;
  1689. /* copy the point */
  1690. memcpy(p, encodedPoint, encoded_pt_len);
  1691. /* increment n to account for length field */
  1692. n += encoded_pt_len;
  1693. /* Free allocated memory */
  1694. BN_CTX_free(bn_ctx);
  1695. bn_ctx = NULL;
  1696. OPENSSL_free(encodedPoint);
  1697. encodedPoint = NULL;
  1698. EC_KEY_free(clnt_ecdh);
  1699. clnt_ecdh = NULL;
  1700. EVP_PKEY_free(srvr_pub_pkey);
  1701. srvr_pub_pkey = NULL;
  1702. } else if (alg_k & SSL_kPSK) {
  1703. /* For plain PSK, other_secret is a block of 0s with the same length as
  1704. * the pre-shared key. */
  1705. pms_len = psk_len;
  1706. pms = OPENSSL_malloc(pms_len);
  1707. if (pms == NULL) {
  1708. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1709. ERR_R_MALLOC_FAILURE);
  1710. goto err;
  1711. }
  1712. memset(pms, 0, pms_len);
  1713. } else {
  1714. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1715. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1716. ERR_R_INTERNAL_ERROR);
  1717. goto err;
  1718. }
  1719. /* For a PSK cipher suite, other_secret is combined with the pre-shared
  1720. * key. */
  1721. if (alg_a & SSL_aPSK) {
  1722. CBB cbb, child;
  1723. uint8_t *new_pms;
  1724. size_t new_pms_len;
  1725. if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len)) {
  1726. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1727. ERR_R_MALLOC_FAILURE);
  1728. goto err;
  1729. }
  1730. if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
  1731. !CBB_add_bytes(&child, pms, pms_len) ||
  1732. !CBB_add_u16_length_prefixed(&cbb, &child) ||
  1733. !CBB_add_bytes(&child, psk, psk_len) ||
  1734. !CBB_finish(&cbb, &new_pms, &new_pms_len)) {
  1735. CBB_cleanup(&cbb);
  1736. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1737. ERR_R_INTERNAL_ERROR);
  1738. goto err;
  1739. }
  1740. OPENSSL_cleanse(pms, pms_len);
  1741. OPENSSL_free(pms);
  1742. pms = new_pms;
  1743. pms_len = new_pms_len;
  1744. }
  1745. /* The message must be added to the finished hash before calculating the
  1746. * master secret. */
  1747. if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
  1748. goto err;
  1749. }
  1750. s->state = SSL3_ST_CW_KEY_EXCH_B;
  1751. s->session->master_key_length = s->enc_method->generate_master_secret(
  1752. s, s->session->master_key, pms, pms_len);
  1753. if (s->session->master_key_length == 0) {
  1754. goto err;
  1755. }
  1756. s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
  1757. OPENSSL_cleanse(pms, pms_len);
  1758. OPENSSL_free(pms);
  1759. }
  1760. /* SSL3_ST_CW_KEY_EXCH_B */
  1761. return s->method->do_write(s);
  1762. err:
  1763. BN_CTX_free(bn_ctx);
  1764. OPENSSL_free(encodedPoint);
  1765. EC_KEY_free(clnt_ecdh);
  1766. EVP_PKEY_free(srvr_pub_pkey);
  1767. if (pms) {
  1768. OPENSSL_cleanse(pms, pms_len);
  1769. OPENSSL_free(pms);
  1770. }
  1771. return -1;
  1772. }
  1773. int ssl3_send_cert_verify(SSL *s) {
  1774. if (s->state == SSL3_ST_CW_CERT_VRFY_A ||
  1775. s->state == SSL3_ST_CW_CERT_VRFY_B) {
  1776. enum ssl_private_key_result_t sign_result;
  1777. uint8_t *p = ssl_handshake_start(s);
  1778. size_t signature_length = 0;
  1779. unsigned long n = 0;
  1780. EVP_PKEY *pkey = s->cert->key->privatekey;
  1781. assert(pkey != NULL || s->cert->key_method != NULL);
  1782. if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
  1783. uint8_t *buf = (uint8_t *)s->init_buf->data;
  1784. const EVP_MD *md = NULL;
  1785. uint8_t digest[EVP_MAX_MD_SIZE];
  1786. size_t digest_length;
  1787. /* Write out the digest type if need be. */
  1788. if (SSL_USE_SIGALGS(s)) {
  1789. md = tls1_choose_signing_digest(s, pkey);
  1790. if (!tls12_get_sigandhash(s, p, pkey, md)) {
  1791. OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR);
  1792. return -1;
  1793. }
  1794. p += 2;
  1795. n += 2;
  1796. }
  1797. /* Compute the digest. */
  1798. if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
  1799. return -1;
  1800. }
  1801. /* The handshake buffer is no longer necessary. */
  1802. if (s->s3->handshake_buffer &&
  1803. !ssl3_digest_cached_records(s, free_handshake_buffer)) {
  1804. return -1;
  1805. }
  1806. /* Sign the digest. */
  1807. signature_length = ssl_private_key_max_signature_len(s, pkey);
  1808. if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH) {
  1809. OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify,
  1810. SSL_R_DATA_LENGTH_TOO_LONG);
  1811. return -1;
  1812. }
  1813. s->rwstate = SSL_PRIVATE_KEY_OPERATION;
  1814. sign_result = ssl_private_key_sign(s, pkey, &p[2], &signature_length,
  1815. signature_length, md, digest,
  1816. digest_length);
  1817. } else {
  1818. if (SSL_USE_SIGALGS(s)) {
  1819. /* The digest has already been selected and written. */
  1820. p += 2;
  1821. n += 2;
  1822. }
  1823. signature_length = ssl_private_key_max_signature_len(s, pkey);
  1824. s->rwstate = SSL_PRIVATE_KEY_OPERATION;
  1825. sign_result = ssl_private_key_sign_complete(s, &p[2], &signature_length,
  1826. signature_length);
  1827. }
  1828. if (sign_result == ssl_private_key_retry) {
  1829. s->state = SSL3_ST_CW_CERT_VRFY_B;
  1830. return -1;
  1831. }
  1832. s->rwstate = SSL_NOTHING;
  1833. if (sign_result != ssl_private_key_success) {
  1834. return -1;
  1835. }
  1836. s2n(signature_length, p);
  1837. n += signature_length + 2;
  1838. if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
  1839. return -1;
  1840. }
  1841. s->state = SSL3_ST_CW_CERT_VRFY_C;
  1842. }
  1843. return ssl_do_write(s);
  1844. }
  1845. /* ssl3_has_client_certificate returns true if a client certificate is
  1846. * configured. */
  1847. static int ssl3_has_client_certificate(SSL *s) {
  1848. return s->cert && s->cert->key->x509 && (s->cert->key->privatekey ||
  1849. s->cert->key_method);
  1850. }
  1851. int ssl3_send_client_certificate(SSL *s) {
  1852. X509 *x509 = NULL;
  1853. EVP_PKEY *pkey = NULL;
  1854. int i;
  1855. if (s->state == SSL3_ST_CW_CERT_A) {
  1856. /* Let cert callback update client certificates if required */
  1857. if (s->cert->cert_cb) {
  1858. i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
  1859. if (i < 0) {
  1860. s->rwstate = SSL_X509_LOOKUP;
  1861. return -1;
  1862. }
  1863. if (i == 0) {
  1864. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1865. return 0;
  1866. }
  1867. s->rwstate = SSL_NOTHING;
  1868. }
  1869. if (ssl3_has_client_certificate(s)) {
  1870. s->state = SSL3_ST_CW_CERT_C;
  1871. } else {
  1872. s->state = SSL3_ST_CW_CERT_B;
  1873. }
  1874. }
  1875. /* We need to get a client cert */
  1876. if (s->state == SSL3_ST_CW_CERT_B) {
  1877. /* If we get an error, we need to:
  1878. * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
  1879. * We then get retried later */
  1880. i = ssl_do_client_cert_cb(s, &x509, &pkey);
  1881. if (i < 0) {
  1882. s->rwstate = SSL_X509_LOOKUP;
  1883. return -1;
  1884. }
  1885. s->rwstate = SSL_NOTHING;
  1886. if (i == 1 && pkey != NULL && x509 != NULL) {
  1887. s->state = SSL3_ST_CW_CERT_B;
  1888. if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) {
  1889. i = 0;
  1890. }
  1891. } else if (i == 1) {
  1892. i = 0;
  1893. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate,
  1894. SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  1895. }
  1896. X509_free(x509);
  1897. EVP_PKEY_free(pkey);
  1898. if (i && !ssl3_has_client_certificate(s)) {
  1899. i = 0;
  1900. }
  1901. if (i == 0) {
  1902. if (s->version == SSL3_VERSION) {
  1903. s->s3->tmp.cert_req = 0;
  1904. ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
  1905. return 1;
  1906. } else {
  1907. s->s3->tmp.cert_req = 2;
  1908. /* There is no client certificate, so the handshake buffer may be
  1909. * released. */
  1910. if (s->s3->handshake_buffer &&
  1911. !ssl3_digest_cached_records(s, free_handshake_buffer)) {
  1912. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1913. return -1;
  1914. }
  1915. }
  1916. }
  1917. /* Ok, we have a cert */
  1918. s->state = SSL3_ST_CW_CERT_C;
  1919. }
  1920. if (s->state == SSL3_ST_CW_CERT_C) {
  1921. CERT_PKEY *cert_pkey = (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key;
  1922. if (!ssl3_output_cert_chain(s, cert_pkey)) {
  1923. return -1;
  1924. }
  1925. }
  1926. /* SSL3_ST_CW_CERT_D */
  1927. return ssl_do_write(s);
  1928. }
  1929. int ssl3_send_next_proto(SSL *s) {
  1930. unsigned int len, padding_len;
  1931. uint8_t *d, *p;
  1932. if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
  1933. len = s->next_proto_negotiated_len;
  1934. padding_len = 32 - ((len + 2) % 32);
  1935. d = p = ssl_handshake_start(s);
  1936. *(p++) = len;
  1937. memcpy(p, s->next_proto_negotiated, len);
  1938. p += len;
  1939. *(p++) = padding_len;
  1940. memset(p, 0, padding_len);
  1941. p += padding_len;
  1942. if (!ssl_set_handshake_header(s, SSL3_MT_NEXT_PROTO, p - d)) {
  1943. return -1;
  1944. }
  1945. s->state = SSL3_ST_CW_NEXT_PROTO_B;
  1946. }
  1947. return ssl_do_write(s);
  1948. }
  1949. int ssl3_send_channel_id(SSL *s) {
  1950. uint8_t *d;
  1951. int ret = -1, public_key_len;
  1952. EVP_MD_CTX md_ctx;
  1953. size_t sig_len;
  1954. ECDSA_SIG *sig = NULL;
  1955. uint8_t *public_key = NULL, *derp, *der_sig = NULL;
  1956. if (s->state != SSL3_ST_CW_CHANNEL_ID_A) {
  1957. return ssl_do_write(s);
  1958. }
  1959. if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb) {
  1960. EVP_PKEY *key = NULL;
  1961. s->ctx->channel_id_cb(s, &key);
  1962. if (key != NULL) {
  1963. s->tlsext_channel_id_private = key;
  1964. }
  1965. }
  1966. if (!s->tlsext_channel_id_private) {
  1967. s->rwstate = SSL_CHANNEL_ID_LOOKUP;
  1968. return -1;
  1969. }
  1970. s->rwstate = SSL_NOTHING;
  1971. d = ssl_handshake_start(s);
  1972. if (s->s3->tlsext_channel_id_new) {
  1973. s2n(TLSEXT_TYPE_channel_id_new, d);
  1974. } else {
  1975. s2n(TLSEXT_TYPE_channel_id, d);
  1976. }
  1977. s2n(TLSEXT_CHANNEL_ID_SIZE, d);
  1978. EVP_MD_CTX_init(&md_ctx);
  1979. public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
  1980. if (public_key_len <= 0) {
  1981. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
  1982. SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
  1983. goto err;
  1984. }
  1985. /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
  1986. * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
  1987. * field elements as 32-byte, big-endian numbers. */
  1988. if (public_key_len != 65) {
  1989. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
  1990. goto err;
  1991. }
  1992. public_key = OPENSSL_malloc(public_key_len);
  1993. if (!public_key) {
  1994. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
  1995. goto err;
  1996. }
  1997. derp = public_key;
  1998. i2d_PublicKey(s->tlsext_channel_id_private, &derp);
  1999. if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
  2000. s->tlsext_channel_id_private) != 1) {
  2001. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
  2002. SSL_R_EVP_DIGESTSIGNINIT_FAILED);
  2003. goto err;
  2004. }
  2005. if (!tls1_channel_id_hash(&md_ctx, s)) {
  2006. goto err;
  2007. }
  2008. if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) {
  2009. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
  2010. SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
  2011. goto err;
  2012. }
  2013. der_sig = OPENSSL_malloc(sig_len);
  2014. if (!der_sig) {
  2015. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
  2016. goto err;
  2017. }
  2018. if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len)) {
  2019. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
  2020. SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
  2021. goto err;
  2022. }
  2023. derp = der_sig;
  2024. sig = d2i_ECDSA_SIG(NULL, (const uint8_t **)&derp, sig_len);
  2025. if (sig == NULL) {
  2026. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
  2027. goto err;
  2028. }
  2029. /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
  2030. memcpy(d, public_key + 1, 64);
  2031. d += 64;
  2032. if (!BN_bn2bin_padded(d, 32, sig->r) ||
  2033. !BN_bn2bin_padded(d + 32, 32, sig->s)) {
  2034. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_INTERNAL_ERROR);
  2035. goto err;
  2036. }
  2037. if (!ssl_set_handshake_header(s, SSL3_MT_ENCRYPTED_EXTENSIONS,
  2038. 2 + 2 + TLSEXT_CHANNEL_ID_SIZE)) {
  2039. goto err;
  2040. }
  2041. s->state = SSL3_ST_CW_CHANNEL_ID_B;
  2042. ret = ssl_do_write(s);
  2043. err:
  2044. EVP_MD_CTX_cleanup(&md_ctx);
  2045. OPENSSL_free(public_key);
  2046. OPENSSL_free(der_sig);
  2047. ECDSA_SIG_free(sig);
  2048. return ret;
  2049. }
  2050. int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) {
  2051. int i = 0;
  2052. if (s->ctx->client_cert_cb) {
  2053. i = s->ctx->client_cert_cb(s, px509, ppkey);
  2054. }
  2055. return i;
  2056. }