s3_clnt.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090
  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. */
  149. #include <openssl/ssl.h>
  150. #include <assert.h>
  151. #include <stdio.h>
  152. #include <string.h>
  153. #include <openssl/bn.h>
  154. #include <openssl/buf.h>
  155. #include <openssl/bytestring.h>
  156. #include <openssl/dh.h>
  157. #include <openssl/ec_key.h>
  158. #include <openssl/ecdsa.h>
  159. #include <openssl/err.h>
  160. #include <openssl/evp.h>
  161. #include <openssl/md5.h>
  162. #include <openssl/mem.h>
  163. #include <openssl/obj.h>
  164. #include <openssl/rand.h>
  165. #include <openssl/x509.h>
  166. #include <openssl/x509v3.h>
  167. #include "internal.h"
  168. #include "../crypto/dh/internal.h"
  169. int ssl3_connect(SSL *ssl) {
  170. BUF_MEM *buf = NULL;
  171. void (*cb)(const SSL *ssl, int type, int value) = NULL;
  172. int ret = -1;
  173. int new_state, state, skip = 0;
  174. assert(ssl->handshake_func == ssl3_connect);
  175. assert(!ssl->server);
  176. assert(!SSL_IS_DTLS(ssl));
  177. ERR_clear_error();
  178. ERR_clear_system_error();
  179. if (ssl->info_callback != NULL) {
  180. cb = ssl->info_callback;
  181. } else if (ssl->ctx->info_callback != NULL) {
  182. cb = ssl->ctx->info_callback;
  183. }
  184. ssl->in_handshake++;
  185. for (;;) {
  186. state = ssl->state;
  187. switch (ssl->state) {
  188. case SSL_ST_CONNECT:
  189. if (cb != NULL) {
  190. cb(ssl, SSL_CB_HANDSHAKE_START, 1);
  191. }
  192. if (ssl->init_buf == NULL) {
  193. buf = BUF_MEM_new();
  194. if (buf == NULL ||
  195. !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
  196. ret = -1;
  197. goto end;
  198. }
  199. ssl->init_buf = buf;
  200. buf = NULL;
  201. }
  202. if (!ssl_init_wbio_buffer(ssl, 0)) {
  203. ret = -1;
  204. goto end;
  205. }
  206. /* don't push the buffering BIO quite yet */
  207. if (!ssl3_init_handshake_buffer(ssl)) {
  208. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  209. ret = -1;
  210. goto end;
  211. }
  212. ssl->state = SSL3_ST_CW_CLNT_HELLO_A;
  213. ssl->init_num = 0;
  214. break;
  215. case SSL3_ST_CW_CLNT_HELLO_A:
  216. case SSL3_ST_CW_CLNT_HELLO_B:
  217. ssl->shutdown = 0;
  218. ret = ssl3_send_client_hello(ssl);
  219. if (ret <= 0) {
  220. goto end;
  221. }
  222. ssl->state = SSL3_ST_CR_SRVR_HELLO_A;
  223. ssl->init_num = 0;
  224. /* turn on buffering for the next lot of output */
  225. if (ssl->bbio != ssl->wbio) {
  226. ssl->wbio = BIO_push(ssl->bbio, ssl->wbio);
  227. }
  228. break;
  229. case SSL3_ST_CR_SRVR_HELLO_A:
  230. case SSL3_ST_CR_SRVR_HELLO_B:
  231. ret = ssl3_get_server_hello(ssl);
  232. if (ret <= 0) {
  233. goto end;
  234. }
  235. if (ssl->hit) {
  236. ssl->state = SSL3_ST_CR_CHANGE;
  237. if (ssl->tlsext_ticket_expected) {
  238. /* receive renewed session ticket */
  239. ssl->state = SSL3_ST_CR_SESSION_TICKET_A;
  240. }
  241. } else {
  242. ssl->state = SSL3_ST_CR_CERT_A;
  243. }
  244. ssl->init_num = 0;
  245. break;
  246. case SSL3_ST_CR_CERT_A:
  247. case SSL3_ST_CR_CERT_B:
  248. if (ssl_cipher_has_server_public_key(ssl->s3->tmp.new_cipher)) {
  249. ret = ssl3_get_server_certificate(ssl);
  250. if (ret <= 0) {
  251. goto end;
  252. }
  253. if (ssl->s3->tmp.certificate_status_expected) {
  254. ssl->state = SSL3_ST_CR_CERT_STATUS_A;
  255. } else {
  256. ssl->state = SSL3_ST_VERIFY_SERVER_CERT;
  257. }
  258. } else {
  259. skip = 1;
  260. ssl->state = SSL3_ST_CR_KEY_EXCH_A;
  261. }
  262. ssl->init_num = 0;
  263. break;
  264. case SSL3_ST_VERIFY_SERVER_CERT:
  265. ret = ssl3_verify_server_cert(ssl);
  266. if (ret <= 0) {
  267. goto end;
  268. }
  269. ssl->state = SSL3_ST_CR_KEY_EXCH_A;
  270. ssl->init_num = 0;
  271. break;
  272. case SSL3_ST_CR_KEY_EXCH_A:
  273. case SSL3_ST_CR_KEY_EXCH_B:
  274. ret = ssl3_get_server_key_exchange(ssl);
  275. if (ret <= 0) {
  276. goto end;
  277. }
  278. ssl->state = SSL3_ST_CR_CERT_REQ_A;
  279. ssl->init_num = 0;
  280. break;
  281. case SSL3_ST_CR_CERT_REQ_A:
  282. case SSL3_ST_CR_CERT_REQ_B:
  283. ret = ssl3_get_certificate_request(ssl);
  284. if (ret <= 0) {
  285. goto end;
  286. }
  287. ssl->state = SSL3_ST_CR_SRVR_DONE_A;
  288. ssl->init_num = 0;
  289. break;
  290. case SSL3_ST_CR_SRVR_DONE_A:
  291. case SSL3_ST_CR_SRVR_DONE_B:
  292. ret = ssl3_get_server_done(ssl);
  293. if (ret <= 0) {
  294. goto end;
  295. }
  296. if (ssl->s3->tmp.cert_req) {
  297. ssl->state = SSL3_ST_CW_CERT_A;
  298. } else {
  299. ssl->state = SSL3_ST_CW_KEY_EXCH_A;
  300. }
  301. ssl->init_num = 0;
  302. break;
  303. case SSL3_ST_CW_CERT_A:
  304. case SSL3_ST_CW_CERT_B:
  305. case SSL3_ST_CW_CERT_C:
  306. case SSL3_ST_CW_CERT_D:
  307. ret = ssl3_send_client_certificate(ssl);
  308. if (ret <= 0) {
  309. goto end;
  310. }
  311. ssl->state = SSL3_ST_CW_KEY_EXCH_A;
  312. ssl->init_num = 0;
  313. break;
  314. case SSL3_ST_CW_KEY_EXCH_A:
  315. case SSL3_ST_CW_KEY_EXCH_B:
  316. ret = ssl3_send_client_key_exchange(ssl);
  317. if (ret <= 0) {
  318. goto end;
  319. }
  320. /* For TLS, cert_req is set to 2, so a cert chain
  321. * of nothing is sent, but no verify packet is sent */
  322. if (ssl->s3->tmp.cert_req == 1) {
  323. ssl->state = SSL3_ST_CW_CERT_VRFY_A;
  324. } else {
  325. ssl->state = SSL3_ST_CW_CHANGE_A;
  326. }
  327. ssl->init_num = 0;
  328. break;
  329. case SSL3_ST_CW_CERT_VRFY_A:
  330. case SSL3_ST_CW_CERT_VRFY_B:
  331. case SSL3_ST_CW_CERT_VRFY_C:
  332. ret = ssl3_send_cert_verify(ssl);
  333. if (ret <= 0) {
  334. goto end;
  335. }
  336. ssl->state = SSL3_ST_CW_CHANGE_A;
  337. ssl->init_num = 0;
  338. break;
  339. case SSL3_ST_CW_CHANGE_A:
  340. case SSL3_ST_CW_CHANGE_B:
  341. ret = ssl3_send_change_cipher_spec(ssl, SSL3_ST_CW_CHANGE_A,
  342. SSL3_ST_CW_CHANGE_B);
  343. if (ret <= 0) {
  344. goto end;
  345. }
  346. ssl->state = SSL3_ST_CW_FINISHED_A;
  347. if (ssl->s3->tlsext_channel_id_valid) {
  348. ssl->state = SSL3_ST_CW_CHANNEL_ID_A;
  349. }
  350. if (ssl->s3->next_proto_neg_seen) {
  351. ssl->state = SSL3_ST_CW_NEXT_PROTO_A;
  352. }
  353. ssl->init_num = 0;
  354. if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
  355. ret = -1;
  356. goto end;
  357. }
  358. break;
  359. case SSL3_ST_CW_NEXT_PROTO_A:
  360. case SSL3_ST_CW_NEXT_PROTO_B:
  361. ret = ssl3_send_next_proto(ssl);
  362. if (ret <= 0) {
  363. goto end;
  364. }
  365. if (ssl->s3->tlsext_channel_id_valid) {
  366. ssl->state = SSL3_ST_CW_CHANNEL_ID_A;
  367. } else {
  368. ssl->state = SSL3_ST_CW_FINISHED_A;
  369. }
  370. break;
  371. case SSL3_ST_CW_CHANNEL_ID_A:
  372. case SSL3_ST_CW_CHANNEL_ID_B:
  373. ret = ssl3_send_channel_id(ssl);
  374. if (ret <= 0) {
  375. goto end;
  376. }
  377. ssl->state = SSL3_ST_CW_FINISHED_A;
  378. break;
  379. case SSL3_ST_CW_FINISHED_A:
  380. case SSL3_ST_CW_FINISHED_B:
  381. ret = ssl3_send_finished(ssl, SSL3_ST_CW_FINISHED_A,
  382. SSL3_ST_CW_FINISHED_B);
  383. if (ret <= 0) {
  384. goto end;
  385. }
  386. ssl->state = SSL3_ST_CW_FLUSH;
  387. if (ssl->hit) {
  388. ssl->s3->tmp.next_state = SSL_ST_OK;
  389. } else {
  390. /* This is a non-resumption handshake. If it involves ChannelID, then
  391. * record the handshake hashes at this point in the session so that
  392. * any resumption of this session with ChannelID can sign those
  393. * hashes. */
  394. ret = tls1_record_handshake_hashes_for_channel_id(ssl);
  395. if (ret <= 0) {
  396. goto end;
  397. }
  398. if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
  399. ssl3_can_false_start(ssl) &&
  400. /* No False Start on renegotiation (would complicate the state
  401. * machine). */
  402. !ssl->s3->initial_handshake_complete) {
  403. ssl->s3->tmp.next_state = SSL3_ST_FALSE_START;
  404. } else {
  405. /* Allow NewSessionTicket if ticket expected */
  406. if (ssl->tlsext_ticket_expected) {
  407. ssl->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
  408. } else {
  409. ssl->s3->tmp.next_state = SSL3_ST_CR_CHANGE;
  410. }
  411. }
  412. }
  413. ssl->init_num = 0;
  414. break;
  415. case SSL3_ST_CR_SESSION_TICKET_A:
  416. case SSL3_ST_CR_SESSION_TICKET_B:
  417. ret = ssl3_get_new_session_ticket(ssl);
  418. if (ret <= 0) {
  419. goto end;
  420. }
  421. ssl->state = SSL3_ST_CR_CHANGE;
  422. ssl->init_num = 0;
  423. break;
  424. case SSL3_ST_CR_CERT_STATUS_A:
  425. case SSL3_ST_CR_CERT_STATUS_B:
  426. ret = ssl3_get_cert_status(ssl);
  427. if (ret <= 0) {
  428. goto end;
  429. }
  430. ssl->state = SSL3_ST_VERIFY_SERVER_CERT;
  431. ssl->init_num = 0;
  432. break;
  433. case SSL3_ST_CR_CHANGE:
  434. ret = ssl->method->ssl_read_change_cipher_spec(ssl);
  435. if (ret <= 0) {
  436. goto end;
  437. }
  438. if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_READ)) {
  439. ret = -1;
  440. goto end;
  441. }
  442. ssl->state = SSL3_ST_CR_FINISHED_A;
  443. break;
  444. case SSL3_ST_CR_FINISHED_A:
  445. case SSL3_ST_CR_FINISHED_B:
  446. ret = ssl3_get_finished(ssl, SSL3_ST_CR_FINISHED_A,
  447. SSL3_ST_CR_FINISHED_B);
  448. if (ret <= 0) {
  449. goto end;
  450. }
  451. if (ssl->hit) {
  452. ssl->state = SSL3_ST_CW_CHANGE_A;
  453. } else {
  454. ssl->state = SSL_ST_OK;
  455. }
  456. ssl->init_num = 0;
  457. break;
  458. case SSL3_ST_CW_FLUSH:
  459. ssl->rwstate = SSL_WRITING;
  460. if (BIO_flush(ssl->wbio) <= 0) {
  461. ret = -1;
  462. goto end;
  463. }
  464. ssl->rwstate = SSL_NOTHING;
  465. ssl->state = ssl->s3->tmp.next_state;
  466. break;
  467. case SSL3_ST_FALSE_START:
  468. /* Allow NewSessionTicket if ticket expected */
  469. if (ssl->tlsext_ticket_expected) {
  470. ssl->state = SSL3_ST_CR_SESSION_TICKET_A;
  471. } else {
  472. ssl->state = SSL3_ST_CR_CHANGE;
  473. }
  474. ssl->s3->tmp.in_false_start = 1;
  475. ssl_free_wbio_buffer(ssl);
  476. ret = 1;
  477. goto end;
  478. case SSL_ST_OK:
  479. /* clean a few things up */
  480. ssl3_cleanup_key_block(ssl);
  481. BUF_MEM_free(ssl->init_buf);
  482. ssl->init_buf = NULL;
  483. /* Remove write buffering now. */
  484. ssl_free_wbio_buffer(ssl);
  485. const int is_initial_handshake = !ssl->s3->initial_handshake_complete;
  486. ssl->init_num = 0;
  487. ssl->s3->tmp.in_false_start = 0;
  488. ssl->s3->initial_handshake_complete = 1;
  489. if (is_initial_handshake) {
  490. /* Renegotiations do not participate in session resumption. */
  491. ssl_update_cache(ssl, SSL_SESS_CACHE_CLIENT);
  492. }
  493. ret = 1;
  494. /* ssl->server=0; */
  495. if (cb != NULL) {
  496. cb(ssl, SSL_CB_HANDSHAKE_DONE, 1);
  497. }
  498. goto end;
  499. default:
  500. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
  501. ret = -1;
  502. goto end;
  503. }
  504. if (!ssl->s3->tmp.reuse_message && !skip) {
  505. if (cb != NULL && ssl->state != state) {
  506. new_state = ssl->state;
  507. ssl->state = state;
  508. cb(ssl, SSL_CB_CONNECT_LOOP, 1);
  509. ssl->state = new_state;
  510. }
  511. }
  512. skip = 0;
  513. }
  514. end:
  515. ssl->in_handshake--;
  516. BUF_MEM_free(buf);
  517. if (cb != NULL) {
  518. cb(ssl, SSL_CB_CONNECT_EXIT, ret);
  519. }
  520. return ret;
  521. }
  522. static int ssl3_write_client_cipher_list(SSL *ssl, CBB *out) {
  523. /* Prepare disabled cipher masks. */
  524. ssl_set_client_disabled(ssl);
  525. CBB child;
  526. if (!CBB_add_u16_length_prefixed(out, &child)) {
  527. return 0;
  528. }
  529. STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl);
  530. int any_enabled = 0;
  531. size_t i;
  532. for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
  533. const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
  534. /* Skip disabled ciphers */
  535. if ((cipher->algorithm_mkey & ssl->cert->mask_k) ||
  536. (cipher->algorithm_auth & ssl->cert->mask_a)) {
  537. continue;
  538. }
  539. if (SSL_CIPHER_get_min_version(cipher) >
  540. ssl3_version_from_wire(ssl, ssl->client_version)) {
  541. continue;
  542. }
  543. any_enabled = 1;
  544. if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
  545. return 0;
  546. }
  547. }
  548. /* If all ciphers were disabled, return the error to the caller. */
  549. if (!any_enabled) {
  550. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
  551. return 0;
  552. }
  553. /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
  554. * added. */
  555. if (ssl->client_version == SSL3_VERSION &&
  556. !ssl->s3->initial_handshake_complete) {
  557. if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
  558. return 0;
  559. }
  560. /* The renegotiation extension is required to be at index zero. */
  561. ssl->s3->tmp.extensions.sent |= (1u << 0);
  562. }
  563. if ((ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) &&
  564. !CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
  565. return 0;
  566. }
  567. return CBB_flush(out);
  568. }
  569. int ssl3_send_client_hello(SSL *ssl) {
  570. if (ssl->state == SSL3_ST_CW_CLNT_HELLO_B) {
  571. return ssl_do_write(ssl);
  572. }
  573. /* In DTLS, reset the handshake buffer each time a new ClientHello is
  574. * assembled. We may send multiple if we receive HelloVerifyRequest. */
  575. if (SSL_IS_DTLS(ssl) && !ssl3_init_handshake_buffer(ssl)) {
  576. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  577. return -1;
  578. }
  579. CBB cbb;
  580. CBB_zero(&cbb);
  581. assert(ssl->state == SSL3_ST_CW_CLNT_HELLO_A);
  582. if (!ssl->s3->have_version) {
  583. uint16_t max_version = ssl3_get_max_client_version(ssl);
  584. /* Disabling all versions is silly: return an error. */
  585. if (max_version == 0) {
  586. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
  587. goto err;
  588. }
  589. ssl->version = max_version;
  590. /* Only set |ssl->client_version| on the initial handshake. Renegotiations,
  591. * although locked to a version, reuse the value. When using the plain RSA
  592. * key exchange, the ClientHello version is checked in the premaster secret.
  593. * Some servers fail when this value changes. */
  594. ssl->client_version = max_version;
  595. }
  596. /* If the configured session has expired or was created at a version higher
  597. * than our maximum version, drop it. */
  598. if (ssl->session != NULL &&
  599. (ssl->session->session_id_length == 0 || ssl->session->not_resumable ||
  600. ssl->session->timeout < (long)(time(NULL) - ssl->session->time) ||
  601. (!SSL_IS_DTLS(ssl) && ssl->session->ssl_version > ssl->version) ||
  602. (SSL_IS_DTLS(ssl) && ssl->session->ssl_version < ssl->version))) {
  603. SSL_set_session(ssl, NULL);
  604. }
  605. /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
  606. * renegerate the client_random. The random must be reused. */
  607. if ((!SSL_IS_DTLS(ssl) || !ssl->d1->send_cookie) &&
  608. !ssl_fill_hello_random(ssl->s3->client_random,
  609. sizeof(ssl->s3->client_random), 0 /* client */)) {
  610. goto err;
  611. }
  612. /* Renegotiations do not participate in session resumption. */
  613. int has_session = ssl->session != NULL &&
  614. !ssl->s3->initial_handshake_complete;
  615. CBB child;
  616. if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
  617. ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
  618. !CBB_add_u16(&cbb, ssl->client_version) ||
  619. !CBB_add_bytes(&cbb, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
  620. !CBB_add_u8_length_prefixed(&cbb, &child) ||
  621. (has_session &&
  622. !CBB_add_bytes(&child, ssl->session->session_id,
  623. ssl->session->session_id_length))) {
  624. goto err;
  625. }
  626. if (SSL_IS_DTLS(ssl)) {
  627. if (!CBB_add_u8_length_prefixed(&cbb, &child) ||
  628. !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
  629. goto err;
  630. }
  631. }
  632. size_t length;
  633. if (!ssl3_write_client_cipher_list(ssl, &cbb) ||
  634. !CBB_add_u8(&cbb, 1 /* one compression method */) ||
  635. !CBB_add_u8(&cbb, 0 /* null compression */) ||
  636. !ssl_add_clienthello_tlsext(ssl, &cbb,
  637. CBB_len(&cbb) + SSL_HM_HEADER_LENGTH(ssl)) ||
  638. !CBB_finish(&cbb, NULL, &length) ||
  639. !ssl_set_handshake_header(ssl, SSL3_MT_CLIENT_HELLO, length)) {
  640. goto err;
  641. }
  642. ssl->state = SSL3_ST_CW_CLNT_HELLO_B;
  643. return ssl_do_write(ssl);
  644. err:
  645. CBB_cleanup(&cbb);
  646. return -1;
  647. }
  648. int ssl3_get_server_hello(SSL *ssl) {
  649. STACK_OF(SSL_CIPHER) *sk;
  650. const SSL_CIPHER *c;
  651. CERT *ct = ssl->cert;
  652. int al = SSL_AD_INTERNAL_ERROR, ok;
  653. long n;
  654. CBS server_hello, server_random, session_id;
  655. uint16_t server_version, cipher_suite;
  656. uint8_t compression_method;
  657. n = ssl->method->ssl_get_message(ssl, SSL3_ST_CR_SRVR_HELLO_A,
  658. SSL3_ST_CR_SRVR_HELLO_B, SSL3_MT_SERVER_HELLO,
  659. 20000, /* ?? */
  660. ssl_hash_message, &ok);
  661. if (!ok) {
  662. uint32_t err = ERR_peek_error();
  663. if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
  664. ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
  665. /* Add a dedicated error code to the queue for a handshake_failure alert
  666. * in response to ClientHello. This matches NSS's client behavior and
  667. * gives a better error on a (probable) failure to negotiate initial
  668. * parameters. Note: this error code comes after the original one.
  669. *
  670. * See https://crbug.com/446505. */
  671. OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
  672. }
  673. return n;
  674. }
  675. CBS_init(&server_hello, ssl->init_msg, n);
  676. if (!CBS_get_u16(&server_hello, &server_version) ||
  677. !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
  678. !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
  679. CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
  680. !CBS_get_u16(&server_hello, &cipher_suite) ||
  681. !CBS_get_u8(&server_hello, &compression_method)) {
  682. al = SSL_AD_DECODE_ERROR;
  683. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  684. goto f_err;
  685. }
  686. assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
  687. if (!ssl->s3->have_version) {
  688. if (!ssl3_is_version_enabled(ssl, server_version)) {
  689. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
  690. ssl->version = server_version;
  691. /* Mark the version as fixed so the record-layer version is not clamped
  692. * to TLS 1.0. */
  693. ssl->s3->have_version = 1;
  694. al = SSL_AD_PROTOCOL_VERSION;
  695. goto f_err;
  696. }
  697. ssl->version = server_version;
  698. ssl->s3->enc_method = ssl3_get_enc_method(server_version);
  699. assert(ssl->s3->enc_method != NULL);
  700. /* At this point, the connection's version is known and ssl->version is
  701. * fixed. Begin enforcing the record-layer version. */
  702. ssl->s3->have_version = 1;
  703. } else if (server_version != ssl->version) {
  704. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
  705. al = SSL_AD_PROTOCOL_VERSION;
  706. goto f_err;
  707. }
  708. /* Copy over the server random. */
  709. memcpy(ssl->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
  710. assert(ssl->session == NULL || ssl->session->session_id_length > 0);
  711. if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
  712. CBS_mem_equal(&session_id, ssl->session->session_id,
  713. ssl->session->session_id_length)) {
  714. if (ssl->sid_ctx_length != ssl->session->sid_ctx_length ||
  715. memcmp(ssl->session->sid_ctx, ssl->sid_ctx, ssl->sid_ctx_length)) {
  716. /* actually a client application bug */
  717. al = SSL_AD_ILLEGAL_PARAMETER;
  718. OPENSSL_PUT_ERROR(SSL,
  719. SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  720. goto f_err;
  721. }
  722. ssl->hit = 1;
  723. } else {
  724. /* The session wasn't resumed. Create a fresh SSL_SESSION to
  725. * fill out. */
  726. ssl->hit = 0;
  727. if (!ssl_get_new_session(ssl, 0 /* client */)) {
  728. goto f_err;
  729. }
  730. /* Note: session_id could be empty. */
  731. ssl->session->session_id_length = CBS_len(&session_id);
  732. memcpy(ssl->session->session_id, CBS_data(&session_id),
  733. CBS_len(&session_id));
  734. }
  735. c = SSL_get_cipher_by_value(cipher_suite);
  736. if (c == NULL) {
  737. /* unknown cipher */
  738. al = SSL_AD_ILLEGAL_PARAMETER;
  739. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
  740. goto f_err;
  741. }
  742. /* If the cipher is disabled then we didn't sent it in the ClientHello, so if
  743. * the server selected it, it's an error. */
  744. if ((c->algorithm_mkey & ct->mask_k) || (c->algorithm_auth & ct->mask_a) ||
  745. SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl)) {
  746. al = SSL_AD_ILLEGAL_PARAMETER;
  747. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
  748. goto f_err;
  749. }
  750. sk = ssl_get_ciphers_by_id(ssl);
  751. if (!sk_SSL_CIPHER_find(sk, NULL, c)) {
  752. /* we did not say we would use this cipher */
  753. al = SSL_AD_ILLEGAL_PARAMETER;
  754. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
  755. goto f_err;
  756. }
  757. if (ssl->hit) {
  758. if (ssl->session->cipher != c) {
  759. al = SSL_AD_ILLEGAL_PARAMETER;
  760. OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  761. goto f_err;
  762. }
  763. if (ssl->session->ssl_version != ssl->version) {
  764. al = SSL_AD_ILLEGAL_PARAMETER;
  765. OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
  766. goto f_err;
  767. }
  768. } else {
  769. ssl->session->cipher = c;
  770. }
  771. ssl->s3->tmp.new_cipher = c;
  772. /* Now that the cipher is known, initialize the handshake hash. */
  773. if (!ssl3_init_handshake_hash(ssl)) {
  774. goto f_err;
  775. }
  776. /* If doing a full handshake with TLS 1.2, the server may request a client
  777. * certificate which requires hashing the handshake transcript under a
  778. * different hash. Otherwise, the handshake buffer may be released. */
  779. if (ssl->hit || ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
  780. ssl3_free_handshake_buffer(ssl);
  781. }
  782. /* Only the NULL compression algorithm is supported. */
  783. if (compression_method != 0) {
  784. al = SSL_AD_ILLEGAL_PARAMETER;
  785. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  786. goto f_err;
  787. }
  788. /* TLS extensions */
  789. if (!ssl_parse_serverhello_tlsext(ssl, &server_hello)) {
  790. OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
  791. goto err;
  792. }
  793. /* There should be nothing left over in the record. */
  794. if (CBS_len(&server_hello) != 0) {
  795. /* wrong packet length */
  796. al = SSL_AD_DECODE_ERROR;
  797. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
  798. goto f_err;
  799. }
  800. if (ssl->hit &&
  801. ssl->s3->tmp.extended_master_secret !=
  802. ssl->session->extended_master_secret) {
  803. al = SSL_AD_HANDSHAKE_FAILURE;
  804. if (ssl->session->extended_master_secret) {
  805. OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
  806. } else {
  807. OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
  808. }
  809. goto f_err;
  810. }
  811. return 1;
  812. f_err:
  813. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  814. err:
  815. return -1;
  816. }
  817. /* ssl3_check_leaf_certificate returns one if |leaf| is a suitable leaf server
  818. * certificate for |ssl|. Otherwise, it returns zero and pushes an error on the
  819. * error queue. */
  820. static int ssl3_check_leaf_certificate(SSL *ssl, X509 *leaf) {
  821. int ret = 0;
  822. EVP_PKEY *pkey = X509_get_pubkey(leaf);
  823. if (pkey == NULL) {
  824. goto err;
  825. }
  826. /* Check the certificate's type matches the cipher. */
  827. const SSL_CIPHER *cipher = ssl->s3->tmp.new_cipher;
  828. int expected_type = ssl_cipher_get_key_type(cipher);
  829. assert(expected_type != EVP_PKEY_NONE);
  830. if (pkey->type != expected_type) {
  831. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
  832. goto err;
  833. }
  834. if (cipher->algorithm_auth & SSL_aECDSA) {
  835. /* TODO(davidben): This behavior is preserved from upstream. Should key
  836. * usages be checked in other cases as well? */
  837. /* This call populates the ex_flags field correctly */
  838. X509_check_purpose(leaf, -1, 0);
  839. if ((leaf->ex_flags & EXFLAG_KUSAGE) &&
  840. !(leaf->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)) {
  841. OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
  842. goto err;
  843. }
  844. if (!tls1_check_ec_cert(ssl, leaf)) {
  845. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
  846. goto err;
  847. }
  848. }
  849. ret = 1;
  850. err:
  851. EVP_PKEY_free(pkey);
  852. return ret;
  853. }
  854. int ssl3_get_server_certificate(SSL *ssl) {
  855. int al, ok, ret = -1;
  856. unsigned long n;
  857. X509 *x = NULL;
  858. STACK_OF(X509) *sk = NULL;
  859. EVP_PKEY *pkey = NULL;
  860. CBS cbs, certificate_list;
  861. const uint8_t *data;
  862. n = ssl->method->ssl_get_message(ssl, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B,
  863. SSL3_MT_CERTIFICATE, (long)ssl->max_cert_list,
  864. ssl_hash_message, &ok);
  865. if (!ok) {
  866. return n;
  867. }
  868. CBS_init(&cbs, ssl->init_msg, n);
  869. sk = sk_X509_new_null();
  870. if (sk == NULL) {
  871. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  872. goto err;
  873. }
  874. if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
  875. CBS_len(&certificate_list) == 0 ||
  876. CBS_len(&cbs) != 0) {
  877. al = SSL_AD_DECODE_ERROR;
  878. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  879. goto f_err;
  880. }
  881. while (CBS_len(&certificate_list) > 0) {
  882. CBS certificate;
  883. if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
  884. al = SSL_AD_DECODE_ERROR;
  885. OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
  886. goto f_err;
  887. }
  888. /* A u24 length cannot overflow a long. */
  889. data = CBS_data(&certificate);
  890. x = d2i_X509(NULL, &data, (long)CBS_len(&certificate));
  891. if (x == NULL) {
  892. al = SSL_AD_BAD_CERTIFICATE;
  893. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  894. goto f_err;
  895. }
  896. if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
  897. al = SSL_AD_DECODE_ERROR;
  898. OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
  899. goto f_err;
  900. }
  901. if (!sk_X509_push(sk, x)) {
  902. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  903. goto err;
  904. }
  905. x = NULL;
  906. }
  907. X509 *leaf = sk_X509_value(sk, 0);
  908. if (!ssl3_check_leaf_certificate(ssl, leaf)) {
  909. al = SSL_AD_ILLEGAL_PARAMETER;
  910. goto f_err;
  911. }
  912. /* NOTE: Unlike the server half, the client's copy of |cert_chain| includes
  913. * the leaf. */
  914. sk_X509_pop_free(ssl->session->cert_chain, X509_free);
  915. ssl->session->cert_chain = sk;
  916. sk = NULL;
  917. X509_free(ssl->session->peer);
  918. ssl->session->peer = X509_up_ref(leaf);
  919. ssl->session->verify_result = ssl->verify_result;
  920. ret = 1;
  921. if (0) {
  922. f_err:
  923. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  924. }
  925. err:
  926. EVP_PKEY_free(pkey);
  927. X509_free(x);
  928. sk_X509_pop_free(sk, X509_free);
  929. return ret;
  930. }
  931. int ssl3_get_server_key_exchange(SSL *ssl) {
  932. EVP_MD_CTX md_ctx;
  933. int al, ok;
  934. EVP_PKEY *pkey = NULL;
  935. DH *dh = NULL;
  936. EC_KEY *ecdh = NULL;
  937. EC_POINT *srvr_ecpoint = NULL;
  938. /* use same message size as in ssl3_get_certificate_request() as
  939. * ServerKeyExchange message may be skipped */
  940. long n = ssl->method->ssl_get_message(
  941. ssl, SSL3_ST_CR_KEY_EXCH_A, SSL3_ST_CR_KEY_EXCH_B, -1, ssl->max_cert_list,
  942. ssl_hash_message, &ok);
  943. if (!ok) {
  944. return n;
  945. }
  946. if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
  947. if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher)) {
  948. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  949. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  950. return -1;
  951. }
  952. /* In plain PSK ciphersuite, ServerKeyExchange may be omitted to send no
  953. * identity hint. */
  954. if (ssl->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
  955. /* TODO(davidben): This should be reset in one place with the rest of the
  956. * handshake state. */
  957. OPENSSL_free(ssl->s3->tmp.peer_psk_identity_hint);
  958. ssl->s3->tmp.peer_psk_identity_hint = NULL;
  959. }
  960. ssl->s3->tmp.reuse_message = 1;
  961. return 1;
  962. }
  963. /* Retain a copy of the original CBS to compute the signature over. */
  964. CBS server_key_exchange;
  965. CBS_init(&server_key_exchange, ssl->init_msg, n);
  966. CBS server_key_exchange_orig = server_key_exchange;
  967. uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
  968. uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
  969. EVP_MD_CTX_init(&md_ctx);
  970. if (alg_a & SSL_aPSK) {
  971. CBS psk_identity_hint;
  972. /* Each of the PSK key exchanges begins with a psk_identity_hint. */
  973. if (!CBS_get_u16_length_prefixed(&server_key_exchange,
  974. &psk_identity_hint)) {
  975. al = SSL_AD_DECODE_ERROR;
  976. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  977. goto f_err;
  978. }
  979. /* Store PSK identity hint for later use, hint is used in
  980. * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
  981. * identity hint can be as long as the maximum length of a PSK identity.
  982. * Also do not allow NULL characters; identities are saved as C strings.
  983. *
  984. * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
  985. * a specific identity. */
  986. if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
  987. CBS_contains_zero_byte(&psk_identity_hint)) {
  988. al = SSL_AD_HANDSHAKE_FAILURE;
  989. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  990. goto f_err;
  991. }
  992. /* Save the identity hint as a C string. */
  993. if (!CBS_strdup(&psk_identity_hint, &ssl->s3->tmp.peer_psk_identity_hint)) {
  994. al = SSL_AD_INTERNAL_ERROR;
  995. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  996. goto f_err;
  997. }
  998. }
  999. if (alg_k & SSL_kDHE) {
  1000. CBS dh_p, dh_g, dh_Ys;
  1001. if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
  1002. CBS_len(&dh_p) == 0 ||
  1003. !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
  1004. CBS_len(&dh_g) == 0 ||
  1005. !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
  1006. CBS_len(&dh_Ys) == 0) {
  1007. al = SSL_AD_DECODE_ERROR;
  1008. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1009. goto f_err;
  1010. }
  1011. dh = DH_new();
  1012. if (dh == NULL) {
  1013. goto err;
  1014. }
  1015. dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL);
  1016. dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL);
  1017. if (dh->p == NULL || dh->g == NULL) {
  1018. goto err;
  1019. }
  1020. ssl->session->key_exchange_info = DH_num_bits(dh);
  1021. if (ssl->session->key_exchange_info < 1024) {
  1022. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DH_P_LENGTH);
  1023. goto err;
  1024. } else if (ssl->session->key_exchange_info > 4096) {
  1025. /* Overly large DHE groups are prohibitively expensive, so enforce a limit
  1026. * to prevent a server from causing us to perform too expensive of a
  1027. * computation. */
  1028. OPENSSL_PUT_ERROR(SSL, SSL_R_DH_P_TOO_LONG);
  1029. goto err;
  1030. }
  1031. SSL_ECDH_CTX_init_for_dhe(&ssl->s3->tmp.ecdh_ctx, dh);
  1032. dh = NULL;
  1033. /* Save the peer public key for later. */
  1034. size_t peer_key_len;
  1035. if (!CBS_stow(&dh_Ys, &ssl->s3->tmp.peer_key, &peer_key_len)) {
  1036. goto err;
  1037. }
  1038. /* |dh_Ys| has a u16 length prefix, so this fits in a |uint16_t|. */
  1039. assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff);
  1040. ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len;
  1041. } else if (alg_k & SSL_kECDHE) {
  1042. /* Parse the server parameters. */
  1043. uint8_t curve_type;
  1044. uint16_t curve_id;
  1045. CBS point;
  1046. if (!CBS_get_u8(&server_key_exchange, &curve_type) ||
  1047. curve_type != NAMED_CURVE_TYPE ||
  1048. !CBS_get_u16(&server_key_exchange, &curve_id) ||
  1049. !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
  1050. al = SSL_AD_DECODE_ERROR;
  1051. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1052. goto f_err;
  1053. }
  1054. ssl->session->key_exchange_info = curve_id;
  1055. /* Ensure the curve is consistent with preferences. */
  1056. if (!tls1_check_curve_id(ssl, curve_id)) {
  1057. al = SSL_AD_ILLEGAL_PARAMETER;
  1058. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
  1059. goto f_err;
  1060. }
  1061. /* Initialize ECDH and save the peer public key for later. */
  1062. size_t peer_key_len;
  1063. if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, curve_id) ||
  1064. !CBS_stow(&point, &ssl->s3->tmp.peer_key, &peer_key_len)) {
  1065. goto err;
  1066. }
  1067. /* |point| has a u8 length prefix, so this fits in a |uint16_t|. */
  1068. assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff);
  1069. ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len;
  1070. } else if (!(alg_k & SSL_kPSK)) {
  1071. al = SSL_AD_UNEXPECTED_MESSAGE;
  1072. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  1073. goto f_err;
  1074. }
  1075. /* At this point, |server_key_exchange| contains the signature, if any, while
  1076. * |server_key_exchange_orig| contains the entire message. From that, derive
  1077. * a CBS containing just the parameter. */
  1078. CBS parameter;
  1079. CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
  1080. CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
  1081. /* ServerKeyExchange should be signed by the server's public key. */
  1082. if (ssl_cipher_has_server_public_key(ssl->s3->tmp.new_cipher)) {
  1083. pkey = X509_get_pubkey(ssl->session->peer);
  1084. if (pkey == NULL) {
  1085. goto err;
  1086. }
  1087. const EVP_MD *md = NULL;
  1088. if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
  1089. uint8_t hash, signature;
  1090. if (!CBS_get_u8(&server_key_exchange, &hash) ||
  1091. !CBS_get_u8(&server_key_exchange, &signature)) {
  1092. al = SSL_AD_DECODE_ERROR;
  1093. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1094. goto f_err;
  1095. }
  1096. if (!tls12_check_peer_sigalg(ssl, &md, &al, hash, signature, pkey)) {
  1097. goto f_err;
  1098. }
  1099. ssl->s3->tmp.server_key_exchange_hash = hash;
  1100. } else if (pkey->type == EVP_PKEY_RSA) {
  1101. md = EVP_md5_sha1();
  1102. } else {
  1103. md = EVP_sha1();
  1104. }
  1105. /* The last field in |server_key_exchange| is the signature. */
  1106. CBS signature;
  1107. if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
  1108. CBS_len(&server_key_exchange) != 0) {
  1109. al = SSL_AD_DECODE_ERROR;
  1110. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1111. goto f_err;
  1112. }
  1113. if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) ||
  1114. !EVP_DigestVerifyUpdate(&md_ctx, ssl->s3->client_random,
  1115. SSL3_RANDOM_SIZE) ||
  1116. !EVP_DigestVerifyUpdate(&md_ctx, ssl->s3->server_random,
  1117. SSL3_RANDOM_SIZE) ||
  1118. !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter),
  1119. CBS_len(&parameter)) ||
  1120. !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
  1121. CBS_len(&signature))) {
  1122. /* bad signature */
  1123. al = SSL_AD_DECRYPT_ERROR;
  1124. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
  1125. goto f_err;
  1126. }
  1127. } else {
  1128. /* PSK ciphers are the only supported certificate-less ciphers. */
  1129. assert(alg_a == SSL_aPSK);
  1130. if (CBS_len(&server_key_exchange) > 0) {
  1131. al = SSL_AD_DECODE_ERROR;
  1132. OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
  1133. goto f_err;
  1134. }
  1135. }
  1136. EVP_PKEY_free(pkey);
  1137. EVP_MD_CTX_cleanup(&md_ctx);
  1138. return 1;
  1139. f_err:
  1140. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  1141. err:
  1142. EVP_PKEY_free(pkey);
  1143. DH_free(dh);
  1144. EC_POINT_free(srvr_ecpoint);
  1145. EC_KEY_free(ecdh);
  1146. EVP_MD_CTX_cleanup(&md_ctx);
  1147. return -1;
  1148. }
  1149. static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
  1150. return X509_NAME_cmp(*a, *b);
  1151. }
  1152. int ssl3_get_certificate_request(SSL *ssl) {
  1153. int ok, ret = 0;
  1154. unsigned long n;
  1155. X509_NAME *xn = NULL;
  1156. STACK_OF(X509_NAME) *ca_sk = NULL;
  1157. CBS cbs;
  1158. CBS certificate_types;
  1159. CBS certificate_authorities;
  1160. const uint8_t *data;
  1161. n = ssl->method->ssl_get_message(ssl, SSL3_ST_CR_CERT_REQ_A,
  1162. SSL3_ST_CR_CERT_REQ_B, -1, ssl->max_cert_list,
  1163. ssl_hash_message, &ok);
  1164. if (!ok) {
  1165. return n;
  1166. }
  1167. ssl->s3->tmp.cert_req = 0;
  1168. if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
  1169. ssl->s3->tmp.reuse_message = 1;
  1170. /* If we get here we don't need the handshake buffer as we won't be doing
  1171. * client auth. */
  1172. ssl3_free_handshake_buffer(ssl);
  1173. return 1;
  1174. }
  1175. if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
  1176. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  1177. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_MESSAGE_TYPE);
  1178. goto err;
  1179. }
  1180. CBS_init(&cbs, ssl->init_msg, n);
  1181. ca_sk = sk_X509_NAME_new(ca_dn_cmp);
  1182. if (ca_sk == NULL) {
  1183. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1184. goto err;
  1185. }
  1186. /* get the certificate types */
  1187. if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
  1188. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1189. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1190. goto err;
  1191. }
  1192. if (!CBS_stow(&certificate_types, &ssl->s3->tmp.certificate_types,
  1193. &ssl->s3->tmp.num_certificate_types)) {
  1194. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1195. goto err;
  1196. }
  1197. if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
  1198. CBS supported_signature_algorithms;
  1199. if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) ||
  1200. !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
  1201. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1202. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1203. goto err;
  1204. }
  1205. }
  1206. /* get the CA RDNs */
  1207. if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
  1208. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1209. OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
  1210. goto err;
  1211. }
  1212. while (CBS_len(&certificate_authorities) > 0) {
  1213. CBS distinguished_name;
  1214. if (!CBS_get_u16_length_prefixed(&certificate_authorities,
  1215. &distinguished_name)) {
  1216. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1217. OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
  1218. goto err;
  1219. }
  1220. data = CBS_data(&distinguished_name);
  1221. /* A u16 length cannot overflow a long. */
  1222. xn = d2i_X509_NAME(NULL, &data, (long)CBS_len(&distinguished_name));
  1223. if (xn == NULL) {
  1224. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1225. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  1226. goto err;
  1227. }
  1228. if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) {
  1229. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1230. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1231. goto err;
  1232. }
  1233. if (CBS_len(&distinguished_name) != 0) {
  1234. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1235. OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_LENGTH_MISMATCH);
  1236. goto err;
  1237. }
  1238. if (!sk_X509_NAME_push(ca_sk, xn)) {
  1239. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1240. goto err;
  1241. }
  1242. }
  1243. /* we should setup a certificate to return.... */
  1244. ssl->s3->tmp.cert_req = 1;
  1245. sk_X509_NAME_pop_free(ssl->s3->tmp.ca_names, X509_NAME_free);
  1246. ssl->s3->tmp.ca_names = ca_sk;
  1247. ca_sk = NULL;
  1248. ret = 1;
  1249. err:
  1250. sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
  1251. return ret;
  1252. }
  1253. int ssl3_get_new_session_ticket(SSL *ssl) {
  1254. int ok, al;
  1255. long n = ssl->method->ssl_get_message(
  1256. ssl, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B,
  1257. SSL3_MT_NEWSESSION_TICKET, 16384, ssl_hash_message, &ok);
  1258. if (!ok) {
  1259. return n;
  1260. }
  1261. CBS new_session_ticket, ticket;
  1262. uint32_t ticket_lifetime_hint;
  1263. CBS_init(&new_session_ticket, ssl->init_msg, n);
  1264. if (!CBS_get_u32(&new_session_ticket, &ticket_lifetime_hint) ||
  1265. !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
  1266. CBS_len(&new_session_ticket) != 0) {
  1267. al = SSL_AD_DECODE_ERROR;
  1268. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1269. goto f_err;
  1270. }
  1271. if (CBS_len(&ticket) == 0) {
  1272. /* RFC 5077 allows a server to change its mind and send no ticket after
  1273. * negotiating the extension. The value of |tlsext_ticket_expected| is
  1274. * checked in |ssl_update_cache| so is cleared here to avoid an unnecessary
  1275. * update. */
  1276. ssl->tlsext_ticket_expected = 0;
  1277. return 1;
  1278. }
  1279. if (ssl->hit) {
  1280. /* The server is sending a new ticket for an existing session. Sessions are
  1281. * immutable once established, so duplicate all but the ticket of the
  1282. * existing session. */
  1283. uint8_t *bytes;
  1284. size_t bytes_len;
  1285. if (!SSL_SESSION_to_bytes_for_ticket(ssl->session, &bytes, &bytes_len)) {
  1286. goto err;
  1287. }
  1288. SSL_SESSION *new_session = SSL_SESSION_from_bytes(bytes, bytes_len);
  1289. OPENSSL_free(bytes);
  1290. if (new_session == NULL) {
  1291. /* This should never happen. */
  1292. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1293. goto err;
  1294. }
  1295. SSL_SESSION_free(ssl->session);
  1296. ssl->session = new_session;
  1297. }
  1298. if (!CBS_stow(&ticket, &ssl->session->tlsext_tick,
  1299. &ssl->session->tlsext_ticklen)) {
  1300. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1301. goto err;
  1302. }
  1303. ssl->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
  1304. /* Generate a session ID for this session based on the session ticket. We use
  1305. * the session ID mechanism for detecting ticket resumption. This also fits in
  1306. * with assumptions elsewhere in OpenSSL.*/
  1307. if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), ssl->session->session_id,
  1308. &ssl->session->session_id_length, EVP_sha256(), NULL)) {
  1309. goto err;
  1310. }
  1311. return 1;
  1312. f_err:
  1313. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  1314. err:
  1315. return -1;
  1316. }
  1317. int ssl3_get_cert_status(SSL *ssl) {
  1318. int ok, al;
  1319. long n;
  1320. CBS certificate_status, ocsp_response;
  1321. uint8_t status_type;
  1322. n = ssl->method->ssl_get_message(
  1323. ssl, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B,
  1324. -1, 16384, ssl_hash_message, &ok);
  1325. if (!ok) {
  1326. return n;
  1327. }
  1328. if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
  1329. /* A server may send status_request in ServerHello and then change
  1330. * its mind about sending CertificateStatus. */
  1331. ssl->s3->tmp.reuse_message = 1;
  1332. return 1;
  1333. }
  1334. CBS_init(&certificate_status, ssl->init_msg, n);
  1335. if (!CBS_get_u8(&certificate_status, &status_type) ||
  1336. status_type != TLSEXT_STATUSTYPE_ocsp ||
  1337. !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
  1338. CBS_len(&ocsp_response) == 0 ||
  1339. CBS_len(&certificate_status) != 0) {
  1340. al = SSL_AD_DECODE_ERROR;
  1341. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1342. goto f_err;
  1343. }
  1344. if (!CBS_stow(&ocsp_response, &ssl->session->ocsp_response,
  1345. &ssl->session->ocsp_response_length)) {
  1346. al = SSL_AD_INTERNAL_ERROR;
  1347. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1348. goto f_err;
  1349. }
  1350. return 1;
  1351. f_err:
  1352. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  1353. return -1;
  1354. }
  1355. int ssl3_get_server_done(SSL *ssl) {
  1356. int ok;
  1357. long n;
  1358. n = ssl->method->ssl_get_message(ssl, SSL3_ST_CR_SRVR_DONE_A,
  1359. SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
  1360. 30, /* should be very small, like 0 :-) */
  1361. ssl_hash_message, &ok);
  1362. if (!ok) {
  1363. return n;
  1364. }
  1365. if (n > 0) {
  1366. /* should contain no data */
  1367. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1368. OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
  1369. return -1;
  1370. }
  1371. return 1;
  1372. }
  1373. OPENSSL_COMPILE_ASSERT(sizeof(size_t) >= sizeof(unsigned),
  1374. SIZE_T_IS_SMALLER_THAN_UNSIGNED);
  1375. int ssl3_send_client_key_exchange(SSL *ssl) {
  1376. if (ssl->state == SSL3_ST_CW_KEY_EXCH_B) {
  1377. return ssl_do_write(ssl);
  1378. }
  1379. assert(ssl->state == SSL3_ST_CW_KEY_EXCH_A);
  1380. uint8_t *pms = NULL;
  1381. size_t pms_len = 0;
  1382. CBB cbb;
  1383. if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
  1384. ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl))) {
  1385. goto err;
  1386. }
  1387. uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
  1388. uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
  1389. /* If using a PSK key exchange, prepare the pre-shared key. */
  1390. unsigned psk_len = 0;
  1391. uint8_t psk[PSK_MAX_PSK_LEN];
  1392. if (alg_a & SSL_aPSK) {
  1393. if (ssl->psk_client_callback == NULL) {
  1394. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
  1395. goto err;
  1396. }
  1397. char identity[PSK_MAX_IDENTITY_LEN + 1];
  1398. memset(identity, 0, sizeof(identity));
  1399. psk_len = ssl->psk_client_callback(
  1400. ssl, ssl->s3->tmp.peer_psk_identity_hint, identity, sizeof(identity),
  1401. psk, sizeof(psk));
  1402. if (psk_len == 0) {
  1403. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
  1404. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1405. goto err;
  1406. }
  1407. assert(psk_len <= PSK_MAX_PSK_LEN);
  1408. OPENSSL_free(ssl->session->psk_identity);
  1409. ssl->session->psk_identity = BUF_strdup(identity);
  1410. if (ssl->session->psk_identity == NULL) {
  1411. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1412. goto err;
  1413. }
  1414. /* Write out psk_identity. */
  1415. CBB child;
  1416. if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
  1417. !CBB_add_bytes(&child, (const uint8_t *)identity,
  1418. OPENSSL_strnlen(identity, sizeof(identity))) ||
  1419. !CBB_flush(&cbb)) {
  1420. goto err;
  1421. }
  1422. }
  1423. /* Depending on the key exchange method, compute |pms| and |pms_len|. */
  1424. if (alg_k & SSL_kRSA) {
  1425. pms_len = SSL_MAX_MASTER_KEY_LENGTH;
  1426. pms = OPENSSL_malloc(pms_len);
  1427. if (pms == NULL) {
  1428. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1429. goto err;
  1430. }
  1431. EVP_PKEY *pkey = X509_get_pubkey(ssl->session->peer);
  1432. if (pkey == NULL) {
  1433. goto err;
  1434. }
  1435. RSA *rsa = EVP_PKEY_get0_RSA(pkey);
  1436. if (rsa == NULL) {
  1437. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1438. EVP_PKEY_free(pkey);
  1439. goto err;
  1440. }
  1441. ssl->session->key_exchange_info = EVP_PKEY_bits(pkey);
  1442. EVP_PKEY_free(pkey);
  1443. pms[0] = ssl->client_version >> 8;
  1444. pms[1] = ssl->client_version & 0xff;
  1445. if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
  1446. goto err;
  1447. }
  1448. CBB child, *enc_pms = &cbb;
  1449. size_t enc_pms_len;
  1450. /* In TLS, there is a length prefix. */
  1451. if (ssl->version > SSL3_VERSION) {
  1452. if (!CBB_add_u16_length_prefixed(&cbb, &child)) {
  1453. goto err;
  1454. }
  1455. enc_pms = &child;
  1456. }
  1457. uint8_t *ptr;
  1458. if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
  1459. !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len,
  1460. RSA_PKCS1_PADDING) ||
  1461. /* Log the premaster secret, if logging is enabled. */
  1462. !ssl_log_rsa_client_key_exchange(ssl, ptr, enc_pms_len, pms, pms_len) ||
  1463. !CBB_did_write(enc_pms, enc_pms_len) ||
  1464. !CBB_flush(&cbb)) {
  1465. goto err;
  1466. }
  1467. } else if (alg_k & (SSL_kECDHE|SSL_kDHE)) {
  1468. /* Generate a keypair and serialize the public half. ECDHE uses a u8 length
  1469. * prefix while DHE uses u16. */
  1470. CBB child;
  1471. int child_ok;
  1472. if (alg_k & SSL_kECDHE) {
  1473. child_ok = CBB_add_u8_length_prefixed(&cbb, &child);
  1474. } else {
  1475. child_ok = CBB_add_u16_length_prefixed(&cbb, &child);
  1476. }
  1477. if (!child_ok ||
  1478. !SSL_ECDH_CTX_generate_keypair(&ssl->s3->tmp.ecdh_ctx, &child) ||
  1479. !CBB_flush(&cbb)) {
  1480. goto err;
  1481. }
  1482. /* Compute the premaster. */
  1483. uint8_t alert;
  1484. if (!SSL_ECDH_CTX_compute_secret(&ssl->s3->tmp.ecdh_ctx, &pms, &pms_len,
  1485. &alert, ssl->s3->tmp.peer_key,
  1486. ssl->s3->tmp.peer_key_len)) {
  1487. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  1488. goto err;
  1489. }
  1490. /* The key exchange state may now be discarded. */
  1491. SSL_ECDH_CTX_cleanup(&ssl->s3->tmp.ecdh_ctx);
  1492. OPENSSL_free(ssl->s3->tmp.peer_key);
  1493. ssl->s3->tmp.peer_key = NULL;
  1494. } else if (alg_k & SSL_kPSK) {
  1495. /* For plain PSK, other_secret is a block of 0s with the same length as
  1496. * the pre-shared key. */
  1497. pms_len = psk_len;
  1498. pms = OPENSSL_malloc(pms_len);
  1499. if (pms == NULL) {
  1500. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1501. goto err;
  1502. }
  1503. memset(pms, 0, pms_len);
  1504. } else {
  1505. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1506. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1507. goto err;
  1508. }
  1509. /* For a PSK cipher suite, other_secret is combined with the pre-shared
  1510. * key. */
  1511. if (alg_a & SSL_aPSK) {
  1512. CBB pms_cbb, child;
  1513. uint8_t *new_pms;
  1514. size_t new_pms_len;
  1515. CBB_zero(&pms_cbb);
  1516. if (!CBB_init(&pms_cbb, 2 + psk_len + 2 + pms_len) ||
  1517. !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
  1518. !CBB_add_bytes(&child, pms, pms_len) ||
  1519. !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
  1520. !CBB_add_bytes(&child, psk, psk_len) ||
  1521. !CBB_finish(&pms_cbb, &new_pms, &new_pms_len)) {
  1522. CBB_cleanup(&pms_cbb);
  1523. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1524. goto err;
  1525. }
  1526. OPENSSL_cleanse(pms, pms_len);
  1527. OPENSSL_free(pms);
  1528. pms = new_pms;
  1529. pms_len = new_pms_len;
  1530. }
  1531. /* The message must be added to the finished hash before calculating the
  1532. * master secret. */
  1533. size_t length;
  1534. if (!CBB_finish(&cbb, NULL, &length) ||
  1535. !ssl_set_handshake_header(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE, length)) {
  1536. goto err;
  1537. }
  1538. ssl->state = SSL3_ST_CW_KEY_EXCH_B;
  1539. ssl->session->master_key_length =
  1540. tls1_generate_master_secret(ssl, ssl->session->master_key, pms, pms_len);
  1541. if (ssl->session->master_key_length == 0) {
  1542. goto err;
  1543. }
  1544. ssl->session->extended_master_secret = ssl->s3->tmp.extended_master_secret;
  1545. OPENSSL_cleanse(pms, pms_len);
  1546. OPENSSL_free(pms);
  1547. /* SSL3_ST_CW_KEY_EXCH_B */
  1548. return ssl_do_write(ssl);
  1549. err:
  1550. if (pms != NULL) {
  1551. OPENSSL_cleanse(pms, pms_len);
  1552. OPENSSL_free(pms);
  1553. }
  1554. return -1;
  1555. }
  1556. int ssl3_send_cert_verify(SSL *ssl) {
  1557. if (ssl->state == SSL3_ST_CW_CERT_VRFY_C) {
  1558. return ssl_do_write(ssl);
  1559. }
  1560. CBB cbb, child;
  1561. if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
  1562. ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl))) {
  1563. goto err;
  1564. }
  1565. assert(ssl_has_private_key(ssl));
  1566. const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
  1567. size_t sig_len;
  1568. enum ssl_private_key_result_t sign_result;
  1569. if (ssl->state == SSL3_ST_CW_CERT_VRFY_A) {
  1570. /* Select and write out the digest type in TLS 1.2. */
  1571. const EVP_MD *md = NULL;
  1572. if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
  1573. md = tls1_choose_signing_digest(ssl);
  1574. if (!tls12_add_sigandhash(ssl, &cbb, md)) {
  1575. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1576. goto err;
  1577. }
  1578. }
  1579. /* Compute the digest. In TLS 1.1 and below, the digest type is also
  1580. * selected here. */
  1581. uint8_t digest[EVP_MAX_MD_SIZE];
  1582. size_t digest_len;
  1583. if (!ssl3_cert_verify_hash(ssl, digest, &digest_len, &md,
  1584. ssl_private_key_type(ssl))) {
  1585. goto err;
  1586. }
  1587. /* The handshake buffer is no longer necessary. */
  1588. ssl3_free_handshake_buffer(ssl);
  1589. /* Sign the digest. */
  1590. uint8_t *ptr;
  1591. if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
  1592. !CBB_reserve(&child, &ptr, max_sig_len)) {
  1593. goto err;
  1594. }
  1595. sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len, md,
  1596. digest, digest_len);
  1597. } else {
  1598. assert(ssl->state == SSL3_ST_CW_CERT_VRFY_B);
  1599. /* Skip over the already written signature algorithm and retry the
  1600. * signature. */
  1601. uint8_t *ptr;
  1602. if ((ssl3_protocol_version(ssl) >= TLS1_2_VERSION &&
  1603. !CBB_did_write(&cbb, 2)) ||
  1604. !CBB_add_u16_length_prefixed(&cbb, &child) ||
  1605. !CBB_reserve(&child, &ptr, max_sig_len)) {
  1606. goto err;
  1607. }
  1608. sign_result =
  1609. ssl_private_key_sign_complete(ssl, ptr, &sig_len, max_sig_len);
  1610. }
  1611. switch (sign_result) {
  1612. case ssl_private_key_success:
  1613. ssl->rwstate = SSL_NOTHING;
  1614. break;
  1615. case ssl_private_key_failure:
  1616. ssl->rwstate = SSL_NOTHING;
  1617. goto err;
  1618. case ssl_private_key_retry:
  1619. ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
  1620. ssl->state = SSL3_ST_CW_CERT_VRFY_B;
  1621. goto err;
  1622. }
  1623. size_t length;
  1624. if (!CBB_did_write(&child, sig_len) ||
  1625. !CBB_finish(&cbb, NULL, &length) ||
  1626. !ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE_VERIFY, length)) {
  1627. goto err;
  1628. }
  1629. ssl->state = SSL3_ST_CW_CERT_VRFY_C;
  1630. return ssl_do_write(ssl);
  1631. err:
  1632. CBB_cleanup(&cbb);
  1633. return -1;
  1634. }
  1635. /* ssl3_has_client_certificate returns true if a client certificate is
  1636. * configured. */
  1637. static int ssl3_has_client_certificate(SSL *ssl) {
  1638. return ssl->cert && ssl->cert->x509 && ssl_has_private_key(ssl);
  1639. }
  1640. int ssl3_send_client_certificate(SSL *ssl) {
  1641. X509 *x509 = NULL;
  1642. EVP_PKEY *pkey = NULL;
  1643. int i;
  1644. if (ssl->state == SSL3_ST_CW_CERT_A) {
  1645. /* Let cert callback update client certificates if required */
  1646. if (ssl->cert->cert_cb) {
  1647. i = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
  1648. if (i < 0) {
  1649. ssl->rwstate = SSL_X509_LOOKUP;
  1650. return -1;
  1651. }
  1652. if (i == 0) {
  1653. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1654. return 0;
  1655. }
  1656. ssl->rwstate = SSL_NOTHING;
  1657. }
  1658. if (ssl3_has_client_certificate(ssl)) {
  1659. ssl->state = SSL3_ST_CW_CERT_C;
  1660. } else {
  1661. ssl->state = SSL3_ST_CW_CERT_B;
  1662. }
  1663. }
  1664. /* We need to get a client cert */
  1665. if (ssl->state == SSL3_ST_CW_CERT_B) {
  1666. /* If we get an error, we need to:
  1667. * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
  1668. * We then get retried later */
  1669. i = ssl_do_client_cert_cb(ssl, &x509, &pkey);
  1670. if (i < 0) {
  1671. ssl->rwstate = SSL_X509_LOOKUP;
  1672. return -1;
  1673. }
  1674. ssl->rwstate = SSL_NOTHING;
  1675. if (i == 1 && pkey != NULL && x509 != NULL) {
  1676. ssl->state = SSL3_ST_CW_CERT_B;
  1677. if (!SSL_use_certificate(ssl, x509) || !SSL_use_PrivateKey(ssl, pkey)) {
  1678. i = 0;
  1679. }
  1680. } else if (i == 1) {
  1681. i = 0;
  1682. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  1683. }
  1684. X509_free(x509);
  1685. EVP_PKEY_free(pkey);
  1686. if (i && !ssl3_has_client_certificate(ssl)) {
  1687. i = 0;
  1688. }
  1689. if (i == 0) {
  1690. if (ssl->version == SSL3_VERSION) {
  1691. ssl->s3->tmp.cert_req = 0;
  1692. ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
  1693. return 1;
  1694. } else {
  1695. ssl->s3->tmp.cert_req = 2;
  1696. /* There is no client certificate, so the handshake buffer may be
  1697. * released. */
  1698. ssl3_free_handshake_buffer(ssl);
  1699. }
  1700. }
  1701. /* Ok, we have a cert */
  1702. ssl->state = SSL3_ST_CW_CERT_C;
  1703. }
  1704. if (ssl->state == SSL3_ST_CW_CERT_C) {
  1705. if (ssl->s3->tmp.cert_req == 2) {
  1706. /* Send an empty Certificate message. */
  1707. uint8_t *p = ssl_handshake_start(ssl);
  1708. l2n3(0, p);
  1709. if (!ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE, 3)) {
  1710. return -1;
  1711. }
  1712. } else if (!ssl3_output_cert_chain(ssl)) {
  1713. return -1;
  1714. }
  1715. ssl->state = SSL3_ST_CW_CERT_D;
  1716. }
  1717. /* SSL3_ST_CW_CERT_D */
  1718. return ssl_do_write(ssl);
  1719. }
  1720. int ssl3_send_next_proto(SSL *ssl) {
  1721. if (ssl->state == SSL3_ST_CW_NEXT_PROTO_B) {
  1722. return ssl_do_write(ssl);
  1723. }
  1724. assert(ssl->state == SSL3_ST_CW_NEXT_PROTO_A);
  1725. static const uint8_t kZero[32] = {0};
  1726. size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32);
  1727. CBB cbb, child;
  1728. size_t length;
  1729. CBB_zero(&cbb);
  1730. if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
  1731. ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
  1732. !CBB_add_u8_length_prefixed(&cbb, &child) ||
  1733. !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated,
  1734. ssl->s3->next_proto_negotiated_len) ||
  1735. !CBB_add_u8_length_prefixed(&cbb, &child) ||
  1736. !CBB_add_bytes(&child, kZero, padding_len) ||
  1737. !CBB_finish(&cbb, NULL, &length) ||
  1738. !ssl_set_handshake_header(ssl, SSL3_MT_NEXT_PROTO, length)) {
  1739. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1740. CBB_cleanup(&cbb);
  1741. return -1;
  1742. }
  1743. ssl->state = SSL3_ST_CW_NEXT_PROTO_B;
  1744. return ssl_do_write(ssl);
  1745. }
  1746. int ssl3_send_channel_id(SSL *ssl) {
  1747. if (ssl->state == SSL3_ST_CW_CHANNEL_ID_B) {
  1748. return ssl_do_write(ssl);
  1749. }
  1750. assert(ssl->state == SSL3_ST_CW_CHANNEL_ID_A);
  1751. if (ssl->tlsext_channel_id_private == NULL &&
  1752. ssl->ctx->channel_id_cb != NULL) {
  1753. EVP_PKEY *key = NULL;
  1754. ssl->ctx->channel_id_cb(ssl, &key);
  1755. if (key != NULL &&
  1756. !SSL_set1_tls_channel_id(ssl, key)) {
  1757. EVP_PKEY_free(key);
  1758. return -1;
  1759. }
  1760. EVP_PKEY_free(key);
  1761. }
  1762. if (ssl->tlsext_channel_id_private == NULL) {
  1763. ssl->rwstate = SSL_CHANNEL_ID_LOOKUP;
  1764. return -1;
  1765. }
  1766. ssl->rwstate = SSL_NOTHING;
  1767. EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
  1768. if (ec_key == NULL) {
  1769. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1770. return -1;
  1771. }
  1772. int ret = -1;
  1773. BIGNUM *x = BN_new();
  1774. BIGNUM *y = BN_new();
  1775. ECDSA_SIG *sig = NULL;
  1776. if (x == NULL || y == NULL ||
  1777. !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
  1778. EC_KEY_get0_public_key(ec_key),
  1779. x, y, NULL)) {
  1780. goto err;
  1781. }
  1782. uint8_t digest[EVP_MAX_MD_SIZE];
  1783. size_t digest_len;
  1784. if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
  1785. goto err;
  1786. }
  1787. sig = ECDSA_do_sign(digest, digest_len, ec_key);
  1788. if (sig == NULL) {
  1789. goto err;
  1790. }
  1791. CBB cbb, child;
  1792. size_t length;
  1793. CBB_zero(&cbb);
  1794. if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
  1795. ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
  1796. !CBB_add_u16(&cbb, TLSEXT_TYPE_channel_id) ||
  1797. !CBB_add_u16_length_prefixed(&cbb, &child) ||
  1798. !BN_bn2cbb_padded(&child, 32, x) ||
  1799. !BN_bn2cbb_padded(&child, 32, y) ||
  1800. !BN_bn2cbb_padded(&child, 32, sig->r) ||
  1801. !BN_bn2cbb_padded(&child, 32, sig->s) ||
  1802. !CBB_finish(&cbb, NULL, &length) ||
  1803. !ssl_set_handshake_header(ssl, SSL3_MT_ENCRYPTED_EXTENSIONS, length)) {
  1804. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1805. CBB_cleanup(&cbb);
  1806. goto err;
  1807. }
  1808. ssl->state = SSL3_ST_CW_CHANNEL_ID_B;
  1809. ret = ssl_do_write(ssl);
  1810. err:
  1811. BN_free(x);
  1812. BN_free(y);
  1813. ECDSA_SIG_free(sig);
  1814. return ret;
  1815. }
  1816. int ssl_do_client_cert_cb(SSL *ssl, X509 **out_x509, EVP_PKEY **out_pkey) {
  1817. if (ssl->ctx->client_cert_cb == NULL) {
  1818. return 0;
  1819. }
  1820. return ssl->ctx->client_cert_cb(ssl, out_x509, out_pkey);
  1821. }
  1822. int ssl3_verify_server_cert(SSL *ssl) {
  1823. int ret = ssl_verify_cert_chain(ssl, ssl->session->cert_chain);
  1824. if (ssl->verify_mode != SSL_VERIFY_NONE && ret <= 0) {
  1825. int al = ssl_verify_alarm_type(ssl->verify_result);
  1826. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  1827. OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
  1828. } else {
  1829. ret = 1;
  1830. ERR_clear_error(); /* but we keep ssl->verify_result */
  1831. }
  1832. return ret;
  1833. }