Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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