Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411
  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. return n;
  671. }
  672. CBS_init(&server_hello, s->init_msg, n);
  673. if (!CBS_get_u16(&server_hello, &server_version) ||
  674. !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
  675. !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
  676. CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
  677. !CBS_get_u16(&server_hello, &cipher_suite) ||
  678. !CBS_get_u8(&server_hello, &compression_method)) {
  679. al = SSL_AD_DECODE_ERROR;
  680. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_DECODE_ERROR);
  681. goto f_err;
  682. }
  683. if (!s->s3->have_version) {
  684. if (!ssl3_is_version_enabled(s, server_version)) {
  685. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_PROTOCOL);
  686. s->version = server_version;
  687. /* Mark the version as fixed so the record-layer version is not clamped
  688. * to TLS 1.0. */
  689. s->s3->have_version = 1;
  690. al = SSL_AD_PROTOCOL_VERSION;
  691. goto f_err;
  692. }
  693. s->version = server_version;
  694. s->enc_method = ssl3_get_enc_method(server_version);
  695. assert(s->enc_method != NULL);
  696. /* At this point, the connection's version is known and s->version is
  697. * fixed. Begin enforcing the record-layer version. */
  698. s->s3->have_version = 1;
  699. } else if (server_version != s->version) {
  700. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
  701. al = SSL_AD_PROTOCOL_VERSION;
  702. goto f_err;
  703. }
  704. /* Copy over the server random. */
  705. memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
  706. assert(s->session == NULL || s->session->session_id_length > 0);
  707. if (s->session != NULL && CBS_mem_equal(&session_id, s->session->session_id,
  708. s->session->session_id_length)) {
  709. if (s->sid_ctx_length != s->session->sid_ctx_length ||
  710. memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
  711. /* actually a client application bug */
  712. al = SSL_AD_ILLEGAL_PARAMETER;
  713. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
  714. SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  715. goto f_err;
  716. }
  717. s->hit = 1;
  718. } else {
  719. /* The session wasn't resumed. Create a fresh SSL_SESSION to
  720. * fill out. */
  721. s->hit = 0;
  722. if (!ssl_get_new_session(s, 0)) {
  723. goto f_err;
  724. }
  725. /* Note: session_id could be empty. */
  726. s->session->session_id_length = CBS_len(&session_id);
  727. memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id));
  728. }
  729. c = ssl3_get_cipher_by_value(cipher_suite);
  730. if (c == NULL) {
  731. /* unknown cipher */
  732. al = SSL_AD_ILLEGAL_PARAMETER;
  733. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
  734. SSL_R_UNKNOWN_CIPHER_RETURNED);
  735. goto f_err;
  736. }
  737. /* ct->mask_ssl was computed from client capabilities. Now
  738. * that the final version is known, compute a new mask_ssl. */
  739. if (!SSL_USE_TLS1_2_CIPHERS(s)) {
  740. mask_ssl = SSL_TLSV1_2;
  741. } else {
  742. mask_ssl = 0;
  743. }
  744. /* If the cipher is disabled then we didn't sent it in the ClientHello, so if
  745. * the server selected it, it's an error. */
  746. if ((c->algorithm_ssl & mask_ssl) ||
  747. (c->algorithm_mkey & ct->mask_k) ||
  748. (c->algorithm_auth & ct->mask_a)) {
  749. al = SSL_AD_ILLEGAL_PARAMETER;
  750. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
  751. goto f_err;
  752. }
  753. sk = ssl_get_ciphers_by_id(s);
  754. if (!sk_SSL_CIPHER_find(sk, NULL, c)) {
  755. /* we did not say we would use this cipher */
  756. al = SSL_AD_ILLEGAL_PARAMETER;
  757. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
  758. goto f_err;
  759. }
  760. /* Depending on the session caching (internal/external), the cipher
  761. and/or cipher_id values may not be set. Make sure that cipher_id is set
  762. and use it for comparison. */
  763. if (s->session->cipher) {
  764. s->session->cipher_id = s->session->cipher->id;
  765. }
  766. if (s->hit && s->session->cipher_id != c->id) {
  767. al = SSL_AD_ILLEGAL_PARAMETER;
  768. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
  769. SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  770. goto f_err;
  771. }
  772. s->s3->tmp.new_cipher = c;
  773. /* Most clients also require that the negotiated version match the session's
  774. * version if resuming. However OpenSSL has historically not had the
  775. * corresponding logic on the server, so this may not be compatible,
  776. * depending on other factors. (Whether the ClientHello version is clamped to
  777. * the session's version and whether the session cache is keyed on IP
  778. * address.)
  779. *
  780. * TODO(davidben): See if we can still enforce this? Perhaps for the future
  781. * TLS 1.3 and forward if this is fixed upstream. */
  782. /* Don't digest cached records if no sigalgs: we may need them for client
  783. * authentication. */
  784. if (!SSL_USE_SIGALGS(s) &&
  785. !ssl3_digest_cached_records(s, free_handshake_buffer)) {
  786. goto f_err;
  787. }
  788. /* Only the NULL compression algorithm is supported. */
  789. if (compression_method != 0) {
  790. al = SSL_AD_ILLEGAL_PARAMETER;
  791. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
  792. SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  793. goto f_err;
  794. }
  795. /* TLS extensions */
  796. if (!ssl_parse_serverhello_tlsext(s, &server_hello)) {
  797. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
  798. goto err;
  799. }
  800. /* There should be nothing left over in the record. */
  801. if (CBS_len(&server_hello) != 0) {
  802. /* wrong packet length */
  803. al = SSL_AD_DECODE_ERROR;
  804. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
  805. goto f_err;
  806. }
  807. return 1;
  808. f_err:
  809. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  810. err:
  811. return -1;
  812. }
  813. int ssl3_get_server_certificate(SSL *s) {
  814. int al, i, ok, ret = -1;
  815. unsigned long n;
  816. X509 *x = NULL;
  817. STACK_OF(X509) *sk = NULL;
  818. SESS_CERT *sc;
  819. EVP_PKEY *pkey = NULL;
  820. CBS cbs, certificate_list;
  821. const uint8_t *data;
  822. n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B,
  823. SSL3_MT_CERTIFICATE, s->max_cert_list,
  824. SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
  825. if (!ok) {
  826. return n;
  827. }
  828. CBS_init(&cbs, s->init_msg, n);
  829. sk = sk_X509_new_null();
  830. if (sk == NULL) {
  831. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
  832. goto err;
  833. }
  834. if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
  835. CBS_len(&cbs) != 0) {
  836. al = SSL_AD_DECODE_ERROR;
  837. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_LENGTH_MISMATCH);
  838. goto f_err;
  839. }
  840. while (CBS_len(&certificate_list) > 0) {
  841. CBS certificate;
  842. if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
  843. al = SSL_AD_DECODE_ERROR;
  844. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
  845. SSL_R_CERT_LENGTH_MISMATCH);
  846. goto f_err;
  847. }
  848. data = CBS_data(&certificate);
  849. x = d2i_X509(NULL, &data, CBS_len(&certificate));
  850. if (x == NULL) {
  851. al = SSL_AD_BAD_CERTIFICATE;
  852. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
  853. goto f_err;
  854. }
  855. if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
  856. al = SSL_AD_DECODE_ERROR;
  857. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
  858. SSL_R_CERT_LENGTH_MISMATCH);
  859. goto f_err;
  860. }
  861. if (!sk_X509_push(sk, x)) {
  862. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
  863. goto err;
  864. }
  865. x = NULL;
  866. }
  867. i = ssl_verify_cert_chain(s, sk);
  868. if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
  869. al = ssl_verify_alarm_type(s->verify_result);
  870. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
  871. SSL_R_CERTIFICATE_VERIFY_FAILED);
  872. goto f_err;
  873. }
  874. ERR_clear_error(); /* but we keep s->verify_result */
  875. sc = ssl_sess_cert_new();
  876. if (sc == NULL) {
  877. goto err;
  878. }
  879. if (s->session->sess_cert) {
  880. ssl_sess_cert_free(s->session->sess_cert);
  881. }
  882. s->session->sess_cert = sc;
  883. sc->cert_chain = sk;
  884. /* Inconsistency alert: cert_chain does include the peer's certificate, which
  885. * we don't include in s3_srvr.c */
  886. x = sk_X509_value(sk, 0);
  887. sk = NULL;
  888. /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
  889. pkey = X509_get_pubkey(x);
  890. if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
  891. x = NULL;
  892. al = SSL3_AL_FATAL;
  893. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
  894. SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
  895. goto f_err;
  896. }
  897. i = ssl_cert_type(pkey);
  898. if (i < 0) {
  899. x = NULL;
  900. al = SSL3_AL_FATAL;
  901. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
  902. SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  903. goto f_err;
  904. }
  905. int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
  906. if (exp_idx >= 0 && i != exp_idx) {
  907. x = NULL;
  908. al = SSL_AD_ILLEGAL_PARAMETER;
  909. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
  910. SSL_R_WRONG_CERTIFICATE_TYPE);
  911. goto f_err;
  912. }
  913. sc->peer_cert_type = i;
  914. /* Why would the following ever happen? We just created sc a couple of lines
  915. * ago. */
  916. if (sc->peer_pkeys[i].x509 != NULL) {
  917. X509_free(sc->peer_pkeys[i].x509);
  918. }
  919. sc->peer_pkeys[i].x509 = X509_up_ref(x);
  920. sc->peer_key = &(sc->peer_pkeys[i]);
  921. if (s->session->peer != NULL) {
  922. X509_free(s->session->peer);
  923. }
  924. s->session->peer = X509_up_ref(x);
  925. s->session->verify_result = s->verify_result;
  926. x = NULL;
  927. ret = 1;
  928. if (0) {
  929. f_err:
  930. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  931. }
  932. err:
  933. EVP_PKEY_free(pkey);
  934. X509_free(x);
  935. sk_X509_pop_free(sk, X509_free);
  936. return ret;
  937. }
  938. int ssl3_get_server_key_exchange(SSL *s) {
  939. EVP_MD_CTX md_ctx;
  940. int al, ok;
  941. long n, alg_k, alg_a;
  942. EVP_PKEY *pkey = NULL;
  943. const EVP_MD *md = NULL;
  944. RSA *rsa = NULL;
  945. DH *dh = NULL;
  946. EC_KEY *ecdh = NULL;
  947. BN_CTX *bn_ctx = NULL;
  948. EC_POINT *srvr_ecpoint = NULL;
  949. CBS server_key_exchange, server_key_exchange_orig, parameter;
  950. /* use same message size as in ssl3_get_certificate_request() as
  951. * ServerKeyExchange message may be skipped */
  952. n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A,
  953. SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list,
  954. SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
  955. if (!ok) {
  956. return n;
  957. }
  958. if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
  959. if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher)) {
  960. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  961. SSL_R_UNEXPECTED_MESSAGE);
  962. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  963. return -1;
  964. }
  965. /* In plain PSK ciphersuite, ServerKeyExchange can be
  966. omitted if no identity hint is sent. Set session->sess_cert anyway to
  967. avoid problems later.*/
  968. if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
  969. /* PSK ciphersuites that also send a Certificate would have already
  970. * initialized |sess_cert|. */
  971. if (s->session->sess_cert == NULL) {
  972. s->session->sess_cert = ssl_sess_cert_new();
  973. }
  974. /* TODO(davidben): This should be reset in one place with the rest of the
  975. * handshake state. */
  976. if (s->s3->tmp.peer_psk_identity_hint) {
  977. OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
  978. s->s3->tmp.peer_psk_identity_hint = NULL;
  979. }
  980. }
  981. s->s3->tmp.reuse_message = 1;
  982. return 1;
  983. }
  984. /* Retain a copy of the original CBS to compute the signature over. */
  985. CBS_init(&server_key_exchange, s->init_msg, n);
  986. server_key_exchange_orig = server_key_exchange;
  987. if (s->session->sess_cert != NULL) {
  988. if (s->session->sess_cert->peer_dh_tmp) {
  989. DH_free(s->session->sess_cert->peer_dh_tmp);
  990. s->session->sess_cert->peer_dh_tmp = NULL;
  991. }
  992. if (s->session->sess_cert->peer_ecdh_tmp) {
  993. EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
  994. s->session->sess_cert->peer_ecdh_tmp = NULL;
  995. }
  996. } else {
  997. s->session->sess_cert = ssl_sess_cert_new();
  998. }
  999. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1000. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1001. EVP_MD_CTX_init(&md_ctx);
  1002. if (alg_a & SSL_aPSK) {
  1003. CBS psk_identity_hint;
  1004. /* Each of the PSK key exchanges begins with a psk_identity_hint. */
  1005. if (!CBS_get_u16_length_prefixed(&server_key_exchange,
  1006. &psk_identity_hint)) {
  1007. al = SSL_AD_DECODE_ERROR;
  1008. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
  1009. goto f_err;
  1010. }
  1011. /* Store PSK identity hint for later use, hint is used in
  1012. * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
  1013. * identity hint can be as long as the maximum length of a PSK identity.
  1014. * Also do not allow NULL characters; identities are saved as C strings.
  1015. *
  1016. * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
  1017. * a specific identity. */
  1018. if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
  1019. CBS_contains_zero_byte(&psk_identity_hint)) {
  1020. al = SSL_AD_HANDSHAKE_FAILURE;
  1021. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1022. SSL_R_DATA_LENGTH_TOO_LONG);
  1023. goto f_err;
  1024. }
  1025. /* Save the identity hint as a C string. */
  1026. if (!CBS_strdup(&psk_identity_hint, &s->s3->tmp.peer_psk_identity_hint)) {
  1027. al = SSL_AD_INTERNAL_ERROR;
  1028. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1029. ERR_R_MALLOC_FAILURE);
  1030. goto f_err;
  1031. }
  1032. }
  1033. if (alg_k & SSL_kEDH) {
  1034. CBS dh_p, dh_g, dh_Ys;
  1035. if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
  1036. CBS_len(&dh_p) == 0 ||
  1037. !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
  1038. CBS_len(&dh_g) == 0 ||
  1039. !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
  1040. CBS_len(&dh_Ys) == 0) {
  1041. al = SSL_AD_DECODE_ERROR;
  1042. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
  1043. goto f_err;
  1044. }
  1045. dh = DH_new();
  1046. if (dh == NULL) {
  1047. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
  1048. goto err;
  1049. }
  1050. if ((dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)) == NULL ||
  1051. (dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)) == NULL ||
  1052. (dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)) ==
  1053. NULL) {
  1054. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
  1055. goto err;
  1056. }
  1057. if (DH_size(dh) < 512 / 8) {
  1058. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1059. SSL_R_BAD_DH_P_LENGTH);
  1060. goto err;
  1061. }
  1062. if (alg_a & SSL_aRSA) {
  1063. pkey = X509_get_pubkey(
  1064. s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1065. }
  1066. /* else anonymous DH, so no certificate or pkey. */
  1067. s->session->sess_cert->peer_dh_tmp = dh;
  1068. dh = NULL;
  1069. } else if (alg_k & SSL_kEECDH) {
  1070. uint16_t curve_id;
  1071. int curve_nid = 0;
  1072. EC_GROUP *ngroup;
  1073. const EC_GROUP *group;
  1074. CBS point;
  1075. /* Extract elliptic curve parameters and the server's ephemeral ECDH public
  1076. * key. Check curve is one of our preferences, if not server has sent an
  1077. * invalid curve. */
  1078. if (!tls1_check_curve(s, &server_key_exchange, &curve_id)) {
  1079. al = SSL_AD_DECODE_ERROR;
  1080. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_WRONG_CURVE);
  1081. goto f_err;
  1082. }
  1083. curve_nid = tls1_ec_curve_id2nid(curve_id);
  1084. if (curve_nid == 0) {
  1085. al = SSL_AD_INTERNAL_ERROR;
  1086. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1087. SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
  1088. goto f_err;
  1089. }
  1090. ecdh = EC_KEY_new();
  1091. if (ecdh == NULL) {
  1092. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1093. ERR_R_MALLOC_FAILURE);
  1094. goto err;
  1095. }
  1096. ngroup = EC_GROUP_new_by_curve_name(curve_nid);
  1097. if (ngroup == NULL ||
  1098. EC_KEY_set_group(ecdh, ngroup) == 0) {
  1099. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_EC_LIB);
  1100. goto err;
  1101. }
  1102. EC_GROUP_free(ngroup);
  1103. group = EC_KEY_get0_group(ecdh);
  1104. /* Next, get the encoded ECPoint */
  1105. if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
  1106. al = SSL_AD_DECODE_ERROR;
  1107. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
  1108. goto f_err;
  1109. }
  1110. if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
  1111. ((bn_ctx = BN_CTX_new()) == NULL)) {
  1112. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1113. ERR_R_MALLOC_FAILURE);
  1114. goto err;
  1115. }
  1116. if (!EC_POINT_oct2point(group, srvr_ecpoint, CBS_data(&point),
  1117. CBS_len(&point), bn_ctx)) {
  1118. al = SSL_AD_DECODE_ERROR;
  1119. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT);
  1120. goto f_err;
  1121. }
  1122. /* The ECC/TLS specification does not mention the use of DSA to sign
  1123. * ECParameters in the server key exchange message. We do support RSA and
  1124. * ECDSA. */
  1125. if (alg_a & SSL_aRSA) {
  1126. pkey = X509_get_pubkey(
  1127. s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1128. } else if (alg_a & SSL_aECDSA) {
  1129. pkey =
  1130. X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  1131. }
  1132. /* else anonymous ECDH, so no certificate or pkey. */
  1133. EC_KEY_set_public_key(ecdh, srvr_ecpoint);
  1134. s->session->sess_cert->peer_ecdh_tmp = ecdh;
  1135. ecdh = NULL;
  1136. BN_CTX_free(bn_ctx);
  1137. bn_ctx = NULL;
  1138. EC_POINT_free(srvr_ecpoint);
  1139. srvr_ecpoint = NULL;
  1140. } else if (!(alg_k & SSL_kPSK)) {
  1141. al = SSL_AD_UNEXPECTED_MESSAGE;
  1142. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1143. SSL_R_UNEXPECTED_MESSAGE);
  1144. goto f_err;
  1145. }
  1146. /* At this point, |server_key_exchange| contains the signature, if any, while
  1147. * |server_key_exchange_orig| contains the entire message. From that, derive
  1148. * a CBS containing just the parameter. */
  1149. CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
  1150. CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
  1151. /* if it was signed, check the signature */
  1152. if (pkey != NULL) {
  1153. CBS signature;
  1154. if (SSL_USE_SIGALGS(s)) {
  1155. if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey)) {
  1156. goto f_err;
  1157. }
  1158. } else if (pkey->type == EVP_PKEY_RSA) {
  1159. md = EVP_md5_sha1();
  1160. } else {
  1161. md = EVP_sha1();
  1162. }
  1163. /* The last field in |server_key_exchange| is the signature. */
  1164. if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
  1165. CBS_len(&server_key_exchange) != 0) {
  1166. al = SSL_AD_DECODE_ERROR;
  1167. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
  1168. goto f_err;
  1169. }
  1170. if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) ||
  1171. !EVP_DigestVerifyUpdate(&md_ctx, s->s3->client_random,
  1172. SSL3_RANDOM_SIZE) ||
  1173. !EVP_DigestVerifyUpdate(&md_ctx, s->s3->server_random,
  1174. SSL3_RANDOM_SIZE) ||
  1175. !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter),
  1176. CBS_len(&parameter)) ||
  1177. !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
  1178. CBS_len(&signature))) {
  1179. /* bad signature */
  1180. al = SSL_AD_DECRYPT_ERROR;
  1181. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
  1182. goto f_err;
  1183. }
  1184. } else {
  1185. if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
  1186. /* Might be wrong key type, check it */
  1187. if (ssl3_check_cert_and_algorithm(s)) {
  1188. /* Otherwise this shouldn't happen */
  1189. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1190. ERR_R_INTERNAL_ERROR);
  1191. }
  1192. goto err;
  1193. }
  1194. /* still data left over */
  1195. if (CBS_len(&server_key_exchange) > 0) {
  1196. al = SSL_AD_DECODE_ERROR;
  1197. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1198. SSL_R_EXTRA_DATA_IN_MESSAGE);
  1199. goto f_err;
  1200. }
  1201. }
  1202. EVP_PKEY_free(pkey);
  1203. EVP_MD_CTX_cleanup(&md_ctx);
  1204. return 1;
  1205. f_err:
  1206. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1207. err:
  1208. EVP_PKEY_free(pkey);
  1209. if (rsa != NULL) {
  1210. RSA_free(rsa);
  1211. }
  1212. if (dh != NULL) {
  1213. DH_free(dh);
  1214. }
  1215. BN_CTX_free(bn_ctx);
  1216. EC_POINT_free(srvr_ecpoint);
  1217. if (ecdh != NULL) {
  1218. EC_KEY_free(ecdh);
  1219. }
  1220. EVP_MD_CTX_cleanup(&md_ctx);
  1221. return -1;
  1222. }
  1223. static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
  1224. return X509_NAME_cmp(*a, *b);
  1225. }
  1226. int ssl3_get_certificate_request(SSL *s) {
  1227. int ok, ret = 0;
  1228. unsigned long n;
  1229. X509_NAME *xn = NULL;
  1230. STACK_OF(X509_NAME) *ca_sk = NULL;
  1231. CBS cbs;
  1232. CBS certificate_types;
  1233. CBS certificate_authorities;
  1234. const uint8_t *data;
  1235. n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A,
  1236. SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list,
  1237. SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
  1238. if (!ok) {
  1239. return n;
  1240. }
  1241. s->s3->tmp.cert_req = 0;
  1242. if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
  1243. s->s3->tmp.reuse_message = 1;
  1244. /* If we get here we don't need any cached handshake records as we wont be
  1245. * doing client auth. */
  1246. if (s->s3->handshake_buffer &&
  1247. !ssl3_digest_cached_records(s, free_handshake_buffer)) {
  1248. goto err;
  1249. }
  1250. return 1;
  1251. }
  1252. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
  1253. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  1254. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
  1255. SSL_R_WRONG_MESSAGE_TYPE);
  1256. goto err;
  1257. }
  1258. /* TLS does not like anon-DH with client cert */
  1259. if (s->version > SSL3_VERSION &&
  1260. (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)) {
  1261. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  1262. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
  1263. SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
  1264. goto err;
  1265. }
  1266. CBS_init(&cbs, s->init_msg, n);
  1267. ca_sk = sk_X509_NAME_new(ca_dn_cmp);
  1268. if (ca_sk == NULL) {
  1269. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
  1270. goto err;
  1271. }
  1272. /* get the certificate types */
  1273. if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
  1274. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1275. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
  1276. goto err;
  1277. }
  1278. if (!CBS_stow(&certificate_types, &s->s3->tmp.certificate_types,
  1279. &s->s3->tmp.num_certificate_types)) {
  1280. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1281. goto err;
  1282. }
  1283. if (SSL_USE_SIGALGS(s)) {
  1284. CBS supported_signature_algorithms;
  1285. if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms)) {
  1286. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1287. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
  1288. goto err;
  1289. }
  1290. if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
  1291. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1292. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
  1293. SSL_R_SIGNATURE_ALGORITHMS_ERROR);
  1294. goto err;
  1295. }
  1296. }
  1297. /* get the CA RDNs */
  1298. if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
  1299. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1300. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
  1301. goto err;
  1302. }
  1303. while (CBS_len(&certificate_authorities) > 0) {
  1304. CBS distinguished_name;
  1305. if (!CBS_get_u16_length_prefixed(&certificate_authorities,
  1306. &distinguished_name)) {
  1307. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1308. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
  1309. SSL_R_CA_DN_TOO_LONG);
  1310. goto err;
  1311. }
  1312. data = CBS_data(&distinguished_name);
  1313. xn = d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name));
  1314. if (xn == NULL) {
  1315. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1316. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
  1317. goto err;
  1318. }
  1319. if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) {
  1320. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1321. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_INTERNAL_ERROR);
  1322. goto err;
  1323. }
  1324. if (CBS_len(&distinguished_name) != 0) {
  1325. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1326. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
  1327. SSL_R_CA_DN_LENGTH_MISMATCH);
  1328. goto err;
  1329. }
  1330. if (!sk_X509_NAME_push(ca_sk, xn)) {
  1331. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
  1332. ERR_R_MALLOC_FAILURE);
  1333. goto err;
  1334. }
  1335. }
  1336. /* we should setup a certificate to return.... */
  1337. s->s3->tmp.cert_req = 1;
  1338. if (s->s3->tmp.ca_names != NULL) {
  1339. sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
  1340. }
  1341. s->s3->tmp.ca_names = ca_sk;
  1342. ca_sk = NULL;
  1343. ret = 1;
  1344. err:
  1345. if (ca_sk != NULL) {
  1346. sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
  1347. }
  1348. return ret;
  1349. }
  1350. int ssl3_get_new_session_ticket(SSL *s) {
  1351. int ok, al, ret = 0;
  1352. long n;
  1353. CBS new_session_ticket, ticket;
  1354. n = s->method->ssl_get_message(
  1355. s, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B,
  1356. SSL3_MT_NEWSESSION_TICKET, 16384, SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
  1357. if (!ok) {
  1358. return n;
  1359. }
  1360. CBS_init(&new_session_ticket, s->init_msg, n);
  1361. if (!CBS_get_u32(&new_session_ticket,
  1362. &s->session->tlsext_tick_lifetime_hint) ||
  1363. !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
  1364. CBS_len(&new_session_ticket) != 0) {
  1365. al = SSL_AD_DECODE_ERROR;
  1366. OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
  1367. goto f_err;
  1368. }
  1369. if (!CBS_stow(&ticket, &s->session->tlsext_tick,
  1370. &s->session->tlsext_ticklen)) {
  1371. OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
  1372. goto err;
  1373. }
  1374. /* There are two ways to detect a resumed ticket sesion. One is to set an
  1375. * appropriate session ID and then the server must return a match in
  1376. * ServerHello. This allows the normal client session ID matching to work and
  1377. * we know much earlier that the ticket has been accepted.
  1378. *
  1379. * The other way is to set zero length session ID when the ticket is
  1380. * presented and rely on the handshake to determine session resumption.
  1381. *
  1382. * We choose the former approach because this fits in with assumptions
  1383. * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
  1384. * SHA256 is disabled) hash of the ticket. */
  1385. EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), s->session->session_id,
  1386. &s->session->session_id_length, EVP_sha256(), NULL);
  1387. ret = 1;
  1388. return ret;
  1389. f_err:
  1390. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1391. err:
  1392. return -1;
  1393. }
  1394. int ssl3_get_cert_status(SSL *s) {
  1395. int ok, al;
  1396. long n;
  1397. CBS certificate_status, ocsp_response;
  1398. uint8_t status_type;
  1399. n = s->method->ssl_get_message(
  1400. s, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B,
  1401. SSL3_MT_CERTIFICATE_STATUS, 16384, SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
  1402. if (!ok) {
  1403. return n;
  1404. }
  1405. CBS_init(&certificate_status, s->init_msg, n);
  1406. if (!CBS_get_u8(&certificate_status, &status_type) ||
  1407. status_type != TLSEXT_STATUSTYPE_ocsp ||
  1408. !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
  1409. CBS_len(&ocsp_response) == 0 ||
  1410. CBS_len(&certificate_status) != 0) {
  1411. al = SSL_AD_DECODE_ERROR;
  1412. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
  1413. goto f_err;
  1414. }
  1415. if (!CBS_stow(&ocsp_response, &s->session->ocsp_response,
  1416. &s->session->ocsp_response_length)) {
  1417. al = SSL_AD_INTERNAL_ERROR;
  1418. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
  1419. goto f_err;
  1420. }
  1421. return 1;
  1422. f_err:
  1423. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1424. return -1;
  1425. }
  1426. int ssl3_get_server_done(SSL *s) {
  1427. int ok;
  1428. long n;
  1429. n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A,
  1430. SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
  1431. 30, /* should be very small, like 0 :-) */
  1432. SSL_GET_MESSAGE_HASH_MESSAGE, &ok);
  1433. if (!ok) {
  1434. return n;
  1435. }
  1436. if (n > 0) {
  1437. /* should contain no data */
  1438. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1439. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
  1440. return -1;
  1441. }
  1442. return 1;
  1443. }
  1444. int ssl3_send_client_key_exchange(SSL *s) {
  1445. uint8_t *p;
  1446. int n = 0;
  1447. unsigned long alg_k;
  1448. unsigned long alg_a;
  1449. uint8_t *q;
  1450. EVP_PKEY *pkey = NULL;
  1451. EC_KEY *clnt_ecdh = NULL;
  1452. const EC_POINT *srvr_ecpoint = NULL;
  1453. EVP_PKEY *srvr_pub_pkey = NULL;
  1454. uint8_t *encodedPoint = NULL;
  1455. int encoded_pt_len = 0;
  1456. BN_CTX *bn_ctx = NULL;
  1457. unsigned int psk_len = 0;
  1458. uint8_t psk[PSK_MAX_PSK_LEN];
  1459. uint8_t *pms = NULL;
  1460. size_t pms_len = 0;
  1461. if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
  1462. p = ssl_handshake_start(s);
  1463. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1464. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1465. /* If using a PSK key exchange, prepare the pre-shared key. */
  1466. if (alg_a & SSL_aPSK) {
  1467. char identity[PSK_MAX_IDENTITY_LEN + 1];
  1468. size_t identity_len;
  1469. if (s->psk_client_callback == NULL) {
  1470. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1471. SSL_R_PSK_NO_CLIENT_CB);
  1472. goto err;
  1473. }
  1474. memset(identity, 0, sizeof(identity));
  1475. psk_len =
  1476. s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint, identity,
  1477. sizeof(identity), psk, sizeof(psk));
  1478. if (psk_len > PSK_MAX_PSK_LEN) {
  1479. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1480. ERR_R_INTERNAL_ERROR);
  1481. goto err;
  1482. } else if (psk_len == 0) {
  1483. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1484. SSL_R_PSK_IDENTITY_NOT_FOUND);
  1485. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1486. goto err;
  1487. }
  1488. identity_len = OPENSSL_strnlen(identity, sizeof(identity));
  1489. if (identity_len > PSK_MAX_IDENTITY_LEN) {
  1490. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1491. ERR_R_INTERNAL_ERROR);
  1492. goto err;
  1493. }
  1494. if (s->session->psk_identity != NULL) {
  1495. OPENSSL_free(s->session->psk_identity);
  1496. }
  1497. s->session->psk_identity = BUF_strdup(identity);
  1498. if (s->session->psk_identity == NULL) {
  1499. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1500. ERR_R_MALLOC_FAILURE);
  1501. goto err;
  1502. }
  1503. /* Write out psk_identity. */
  1504. s2n(identity_len, p);
  1505. memcpy(p, identity, identity_len);
  1506. p += identity_len;
  1507. n = 2 + identity_len;
  1508. }
  1509. /* Depending on the key exchange method, compute |pms| and |pms_len|. */
  1510. if (alg_k & SSL_kRSA) {
  1511. RSA *rsa;
  1512. size_t enc_pms_len;
  1513. pms_len = SSL_MAX_MASTER_KEY_LENGTH;
  1514. pms = OPENSSL_malloc(pms_len);
  1515. if (pms == NULL) {
  1516. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1517. ERR_R_MALLOC_FAILURE);
  1518. goto err;
  1519. }
  1520. if (s->session->sess_cert == NULL) {
  1521. /* We should always have a server certificate with SSL_kRSA. */
  1522. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1523. ERR_R_INTERNAL_ERROR);
  1524. goto err;
  1525. }
  1526. pkey = X509_get_pubkey(
  1527. s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1528. if (pkey == NULL ||
  1529. pkey->type != EVP_PKEY_RSA ||
  1530. pkey->pkey.rsa == NULL) {
  1531. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1532. ERR_R_INTERNAL_ERROR);
  1533. if (pkey != NULL) {
  1534. EVP_PKEY_free(pkey);
  1535. }
  1536. goto err;
  1537. }
  1538. rsa = pkey->pkey.rsa;
  1539. EVP_PKEY_free(pkey);
  1540. pms[0] = s->client_version >> 8;
  1541. pms[1] = s->client_version & 0xff;
  1542. if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
  1543. goto err;
  1544. }
  1545. s->session->master_key_length = SSL_MAX_MASTER_KEY_LENGTH;
  1546. q = p;
  1547. /* In TLS and beyond, reserve space for the length prefix. */
  1548. if (s->version > SSL3_VERSION) {
  1549. p += 2;
  1550. n += 2;
  1551. }
  1552. if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa), pms, pms_len,
  1553. RSA_PKCS1_PADDING)) {
  1554. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1555. SSL_R_BAD_RSA_ENCRYPT);
  1556. goto err;
  1557. }
  1558. n += enc_pms_len;
  1559. /* Log the premaster secret, if logging is enabled. */
  1560. if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx, p, enc_pms_len, pms,
  1561. pms_len)) {
  1562. goto err;
  1563. }
  1564. /* Fill in the length prefix. */
  1565. if (s->version > SSL3_VERSION) {
  1566. s2n(enc_pms_len, q);
  1567. }
  1568. } else if (alg_k & SSL_kEDH) {
  1569. DH *dh_srvr, *dh_clnt;
  1570. SESS_CERT *scert = s->session->sess_cert;
  1571. int dh_len;
  1572. size_t pub_len;
  1573. if (scert == NULL) {
  1574. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  1575. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1576. SSL_R_UNEXPECTED_MESSAGE);
  1577. goto err;
  1578. }
  1579. if (scert->peer_dh_tmp == NULL) {
  1580. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1581. ERR_R_INTERNAL_ERROR);
  1582. goto err;
  1583. }
  1584. dh_srvr = scert->peer_dh_tmp;
  1585. /* generate a new random key */
  1586. dh_clnt = DHparams_dup(dh_srvr);
  1587. if (dh_clnt == NULL) {
  1588. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
  1589. goto err;
  1590. }
  1591. if (!DH_generate_key(dh_clnt)) {
  1592. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
  1593. DH_free(dh_clnt);
  1594. goto err;
  1595. }
  1596. pms_len = DH_size(dh_clnt);
  1597. pms = OPENSSL_malloc(pms_len);
  1598. if (pms == NULL) {
  1599. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1600. ERR_R_MALLOC_FAILURE);
  1601. DH_free(dh_clnt);
  1602. goto err;
  1603. }
  1604. dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
  1605. if (dh_len <= 0) {
  1606. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
  1607. DH_free(dh_clnt);
  1608. goto err;
  1609. }
  1610. pms_len = dh_len;
  1611. /* send off the data */
  1612. pub_len = BN_num_bytes(dh_clnt->pub_key);
  1613. s2n(pub_len, p);
  1614. BN_bn2bin(dh_clnt->pub_key, p);
  1615. n += 2 + pub_len;
  1616. DH_free(dh_clnt);
  1617. } else if (alg_k & SSL_kEECDH) {
  1618. const EC_GROUP *srvr_group = NULL;
  1619. EC_KEY *tkey;
  1620. int field_size = 0, ecdh_len;
  1621. if (s->session->sess_cert == NULL) {
  1622. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  1623. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1624. SSL_R_UNEXPECTED_MESSAGE);
  1625. goto err;
  1626. }
  1627. if (s->session->sess_cert->peer_ecdh_tmp == NULL) {
  1628. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1629. ERR_R_INTERNAL_ERROR);
  1630. goto err;
  1631. }
  1632. tkey = s->session->sess_cert->peer_ecdh_tmp;
  1633. srvr_group = EC_KEY_get0_group(tkey);
  1634. srvr_ecpoint = EC_KEY_get0_public_key(tkey);
  1635. if (srvr_group == NULL || srvr_ecpoint == NULL) {
  1636. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1637. ERR_R_INTERNAL_ERROR);
  1638. goto err;
  1639. }
  1640. clnt_ecdh = EC_KEY_new();
  1641. if (clnt_ecdh == NULL) {
  1642. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1643. ERR_R_MALLOC_FAILURE);
  1644. goto err;
  1645. }
  1646. if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
  1647. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
  1648. goto err;
  1649. }
  1650. /* Generate a new ECDH key pair */
  1651. if (!EC_KEY_generate_key(clnt_ecdh)) {
  1652. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
  1653. goto err;
  1654. }
  1655. field_size = EC_GROUP_get_degree(srvr_group);
  1656. if (field_size <= 0) {
  1657. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
  1658. goto err;
  1659. }
  1660. pms_len = (field_size + 7) / 8;
  1661. pms = OPENSSL_malloc(pms_len);
  1662. if (pms == NULL) {
  1663. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1664. ERR_R_MALLOC_FAILURE);
  1665. goto err;
  1666. }
  1667. ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL);
  1668. if (ecdh_len <= 0) {
  1669. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
  1670. goto err;
  1671. }
  1672. pms_len = ecdh_len;
  1673. /* First check the size of encoding and allocate memory accordingly. */
  1674. encoded_pt_len =
  1675. EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
  1676. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
  1677. encodedPoint =
  1678. (uint8_t *)OPENSSL_malloc(encoded_pt_len * sizeof(uint8_t));
  1679. bn_ctx = BN_CTX_new();
  1680. if (encodedPoint == NULL || bn_ctx == NULL) {
  1681. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1682. ERR_R_MALLOC_FAILURE);
  1683. goto err;
  1684. }
  1685. /* Encode the public key */
  1686. encoded_pt_len = EC_POINT_point2oct(
  1687. srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
  1688. POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encoded_pt_len, bn_ctx);
  1689. *p = encoded_pt_len; /* length of encoded point */
  1690. /* Encoded point will be copied here */
  1691. p += 1;
  1692. n += 1;
  1693. /* copy the point */
  1694. memcpy(p, encodedPoint, encoded_pt_len);
  1695. /* increment n to account for length field */
  1696. n += encoded_pt_len;
  1697. /* Free allocated memory */
  1698. BN_CTX_free(bn_ctx);
  1699. bn_ctx = NULL;
  1700. OPENSSL_free(encodedPoint);
  1701. encodedPoint = NULL;
  1702. EC_KEY_free(clnt_ecdh);
  1703. clnt_ecdh = NULL;
  1704. EVP_PKEY_free(srvr_pub_pkey);
  1705. srvr_pub_pkey = NULL;
  1706. } else if (alg_k & SSL_kPSK) {
  1707. /* For plain PSK, other_secret is a block of 0s with the same length as
  1708. * the pre-shared key. */
  1709. pms_len = psk_len;
  1710. pms = OPENSSL_malloc(pms_len);
  1711. if (pms == NULL) {
  1712. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1713. ERR_R_MALLOC_FAILURE);
  1714. goto err;
  1715. }
  1716. memset(pms, 0, pms_len);
  1717. } else {
  1718. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1719. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1720. ERR_R_INTERNAL_ERROR);
  1721. goto err;
  1722. }
  1723. /* For a PSK cipher suite, other_secret is combined with the pre-shared
  1724. * key. */
  1725. if (alg_a & SSL_aPSK) {
  1726. CBB cbb, child;
  1727. uint8_t *new_pms;
  1728. size_t new_pms_len;
  1729. if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len)) {
  1730. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1731. ERR_R_MALLOC_FAILURE);
  1732. goto err;
  1733. }
  1734. if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
  1735. !CBB_add_bytes(&child, pms, pms_len) ||
  1736. !CBB_add_u16_length_prefixed(&cbb, &child) ||
  1737. !CBB_add_bytes(&child, psk, psk_len) ||
  1738. !CBB_finish(&cbb, &new_pms, &new_pms_len)) {
  1739. CBB_cleanup(&cbb);
  1740. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1741. ERR_R_INTERNAL_ERROR);
  1742. goto err;
  1743. }
  1744. OPENSSL_cleanse(pms, pms_len);
  1745. OPENSSL_free(pms);
  1746. pms = new_pms;
  1747. pms_len = new_pms_len;
  1748. }
  1749. /* The message must be added to the finished hash before calculating the
  1750. * master secret. */
  1751. ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n);
  1752. s->state = SSL3_ST_CW_KEY_EXCH_B;
  1753. s->session->master_key_length = s->enc_method->generate_master_secret(
  1754. s, s->session->master_key, pms, pms_len);
  1755. if (s->session->master_key_length == 0) {
  1756. goto err;
  1757. }
  1758. s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
  1759. OPENSSL_cleanse(pms, pms_len);
  1760. OPENSSL_free(pms);
  1761. }
  1762. /* SSL3_ST_CW_KEY_EXCH_B */
  1763. return s->enc_method->do_write(s);
  1764. err:
  1765. BN_CTX_free(bn_ctx);
  1766. if (encodedPoint != NULL) {
  1767. OPENSSL_free(encodedPoint);
  1768. }
  1769. if (clnt_ecdh != NULL) {
  1770. EC_KEY_free(clnt_ecdh);
  1771. }
  1772. EVP_PKEY_free(srvr_pub_pkey);
  1773. if (pms) {
  1774. OPENSSL_cleanse(pms, pms_len);
  1775. OPENSSL_free(pms);
  1776. }
  1777. return -1;
  1778. }
  1779. int ssl3_send_cert_verify(SSL *s) {
  1780. uint8_t *buf, *p;
  1781. const EVP_MD *md = NULL;
  1782. uint8_t digest[EVP_MAX_MD_SIZE];
  1783. size_t digest_length;
  1784. EVP_PKEY *pkey;
  1785. EVP_PKEY_CTX *pctx = NULL;
  1786. size_t signature_length = 0;
  1787. unsigned long n = 0;
  1788. buf = (uint8_t *)s->init_buf->data;
  1789. if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
  1790. p = ssl_handshake_start(s);
  1791. pkey = s->cert->key->privatekey;
  1792. /* Write out the digest type if needbe. */
  1793. if (SSL_USE_SIGALGS(s)) {
  1794. md = tls1_choose_signing_digest(s, pkey);
  1795. if (!tls12_get_sigandhash(p, pkey, md)) {
  1796. OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR);
  1797. goto err;
  1798. }
  1799. p += 2;
  1800. n += 2;
  1801. }
  1802. /* Compute the digest. */
  1803. if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
  1804. goto err;
  1805. }
  1806. /* The handshake buffer is no longer necessary. */
  1807. if (s->s3->handshake_buffer &&
  1808. !ssl3_digest_cached_records(s, free_handshake_buffer)) {
  1809. goto err;
  1810. }
  1811. /* Sign the digest. */
  1812. pctx = EVP_PKEY_CTX_new(pkey, NULL);
  1813. if (pctx == NULL) {
  1814. goto err;
  1815. }
  1816. /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
  1817. if (!EVP_PKEY_sign_init(pctx) || !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
  1818. !EVP_PKEY_sign(pctx, NULL, &signature_length, digest, digest_length)) {
  1819. OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
  1820. goto err;
  1821. }
  1822. if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH) {
  1823. OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, SSL_R_DATA_LENGTH_TOO_LONG);
  1824. goto err;
  1825. }
  1826. if (!EVP_PKEY_sign(pctx, &p[2], &signature_length, digest, digest_length)) {
  1827. OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
  1828. goto err;
  1829. }
  1830. s2n(signature_length, p);
  1831. n += signature_length + 2;
  1832. ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n);
  1833. s->state = SSL3_ST_CW_CERT_VRFY_B;
  1834. }
  1835. EVP_PKEY_CTX_free(pctx);
  1836. return ssl_do_write(s);
  1837. err:
  1838. EVP_PKEY_CTX_free(pctx);
  1839. return -1;
  1840. }
  1841. /* ssl3_has_client_certificate returns true if a client certificate is
  1842. * configured. */
  1843. static int ssl3_has_client_certificate(SSL *s) {
  1844. return s->cert && s->cert->key->x509 && s->cert->key->privatekey;
  1845. }
  1846. int ssl3_send_client_certificate(SSL *s) {
  1847. X509 *x509 = NULL;
  1848. EVP_PKEY *pkey = NULL;
  1849. int i;
  1850. if (s->state == SSL3_ST_CW_CERT_A) {
  1851. /* Let cert callback update client certificates if required */
  1852. if (s->cert->cert_cb) {
  1853. i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
  1854. if (i < 0) {
  1855. s->rwstate = SSL_X509_LOOKUP;
  1856. return -1;
  1857. }
  1858. if (i == 0) {
  1859. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1860. return 0;
  1861. }
  1862. s->rwstate = SSL_NOTHING;
  1863. }
  1864. if (ssl3_has_client_certificate(s)) {
  1865. s->state = SSL3_ST_CW_CERT_C;
  1866. } else {
  1867. s->state = SSL3_ST_CW_CERT_B;
  1868. }
  1869. }
  1870. /* We need to get a client cert */
  1871. if (s->state == SSL3_ST_CW_CERT_B) {
  1872. /* If we get an error, we need to:
  1873. * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
  1874. * We then get retried later */
  1875. i = ssl_do_client_cert_cb(s, &x509, &pkey);
  1876. if (i < 0) {
  1877. s->rwstate = SSL_X509_LOOKUP;
  1878. return -1;
  1879. }
  1880. s->rwstate = SSL_NOTHING;
  1881. if (i == 1 && pkey != NULL && x509 != NULL) {
  1882. s->state = SSL3_ST_CW_CERT_B;
  1883. if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) {
  1884. i = 0;
  1885. }
  1886. } else if (i == 1) {
  1887. i = 0;
  1888. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate,
  1889. SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  1890. }
  1891. if (x509 != NULL) {
  1892. X509_free(x509);
  1893. }
  1894. if (pkey != NULL) {
  1895. EVP_PKEY_free(pkey);
  1896. }
  1897. if (i && !ssl3_has_client_certificate(s)) {
  1898. i = 0;
  1899. }
  1900. if (i == 0) {
  1901. if (s->version == SSL3_VERSION) {
  1902. s->s3->tmp.cert_req = 0;
  1903. ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
  1904. return 1;
  1905. } else {
  1906. s->s3->tmp.cert_req = 2;
  1907. }
  1908. }
  1909. /* Ok, we have a cert */
  1910. s->state = SSL3_ST_CW_CERT_C;
  1911. }
  1912. if (s->state == SSL3_ST_CW_CERT_C) {
  1913. s->state = SSL3_ST_CW_CERT_D;
  1914. ssl3_output_cert_chain(s, (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key);
  1915. }
  1916. /* SSL3_ST_CW_CERT_D */
  1917. return ssl_do_write(s);
  1918. }
  1919. #define has_bits(i, m) (((i) & (m)) == (m))
  1920. int ssl3_check_cert_and_algorithm(SSL *s) {
  1921. int i, idx;
  1922. long alg_k, alg_a;
  1923. EVP_PKEY *pkey = NULL;
  1924. SESS_CERT *sc;
  1925. DH *dh;
  1926. /* we don't have a certificate */
  1927. if (!ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
  1928. return 1;
  1929. }
  1930. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1931. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1932. sc = s->session->sess_cert;
  1933. if (sc == NULL) {
  1934. OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, ERR_R_INTERNAL_ERROR);
  1935. goto err;
  1936. }
  1937. dh = s->session->sess_cert->peer_dh_tmp;
  1938. /* This is the passed certificate */
  1939. idx = sc->peer_cert_type;
  1940. if (idx == SSL_PKEY_ECC) {
  1941. if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
  1942. /* check failed */
  1943. OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_BAD_ECC_CERT);
  1944. goto f_err;
  1945. } else {
  1946. return 1;
  1947. }
  1948. } else if (alg_a & SSL_aECDSA) {
  1949. OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
  1950. SSL_R_MISSING_ECDSA_SIGNING_CERT);
  1951. goto f_err;
  1952. }
  1953. pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
  1954. i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
  1955. EVP_PKEY_free(pkey);
  1956. /* Check that we have a certificate if we require one */
  1957. if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
  1958. OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
  1959. SSL_R_MISSING_RSA_SIGNING_CERT);
  1960. goto f_err;
  1961. }
  1962. if ((alg_k & SSL_kRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
  1963. OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
  1964. SSL_R_MISSING_RSA_ENCRYPTING_CERT);
  1965. goto f_err;
  1966. }
  1967. if ((alg_k & SSL_kEDH) &&
  1968. !(has_bits(i, EVP_PK_DH | EVP_PKT_EXCH) || dh != NULL)) {
  1969. OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_DH_KEY);
  1970. goto f_err;
  1971. }
  1972. return 1;
  1973. f_err:
  1974. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1975. err:
  1976. return 0;
  1977. }
  1978. int ssl3_send_next_proto(SSL *s) {
  1979. unsigned int len, padding_len;
  1980. uint8_t *d, *p;
  1981. if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
  1982. len = s->next_proto_negotiated_len;
  1983. padding_len = 32 - ((len + 2) % 32);
  1984. d = p = ssl_handshake_start(s);
  1985. *(p++) = len;
  1986. memcpy(p, s->next_proto_negotiated, len);
  1987. p += len;
  1988. *(p++) = padding_len;
  1989. memset(p, 0, padding_len);
  1990. p += padding_len;
  1991. ssl_set_handshake_header(s, SSL3_MT_NEXT_PROTO, p - d);
  1992. s->state = SSL3_ST_CW_NEXT_PROTO_B;
  1993. }
  1994. return ssl_do_write(s);
  1995. }
  1996. int ssl3_send_channel_id(SSL *s) {
  1997. uint8_t *d;
  1998. int ret = -1, public_key_len;
  1999. EVP_MD_CTX md_ctx;
  2000. size_t sig_len;
  2001. ECDSA_SIG *sig = NULL;
  2002. uint8_t *public_key = NULL, *derp, *der_sig = NULL;
  2003. if (s->state != SSL3_ST_CW_CHANNEL_ID_A) {
  2004. return ssl_do_write(s);
  2005. }
  2006. if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb) {
  2007. EVP_PKEY *key = NULL;
  2008. s->ctx->channel_id_cb(s, &key);
  2009. if (key != NULL) {
  2010. s->tlsext_channel_id_private = key;
  2011. }
  2012. }
  2013. if (!s->tlsext_channel_id_private) {
  2014. s->rwstate = SSL_CHANNEL_ID_LOOKUP;
  2015. return -1;
  2016. }
  2017. s->rwstate = SSL_NOTHING;
  2018. d = ssl_handshake_start(s);
  2019. if (s->s3->tlsext_channel_id_new) {
  2020. s2n(TLSEXT_TYPE_channel_id_new, d);
  2021. } else {
  2022. s2n(TLSEXT_TYPE_channel_id, d);
  2023. }
  2024. s2n(TLSEXT_CHANNEL_ID_SIZE, d);
  2025. EVP_MD_CTX_init(&md_ctx);
  2026. public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
  2027. if (public_key_len <= 0) {
  2028. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
  2029. SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
  2030. goto err;
  2031. }
  2032. /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
  2033. * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
  2034. * field elements as 32-byte, big-endian numbers. */
  2035. if (public_key_len != 65) {
  2036. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
  2037. goto err;
  2038. }
  2039. public_key = OPENSSL_malloc(public_key_len);
  2040. if (!public_key) {
  2041. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
  2042. goto err;
  2043. }
  2044. derp = public_key;
  2045. i2d_PublicKey(s->tlsext_channel_id_private, &derp);
  2046. if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
  2047. s->tlsext_channel_id_private) != 1) {
  2048. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
  2049. SSL_R_EVP_DIGESTSIGNINIT_FAILED);
  2050. goto err;
  2051. }
  2052. if (!tls1_channel_id_hash(&md_ctx, s)) {
  2053. goto err;
  2054. }
  2055. if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) {
  2056. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
  2057. SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
  2058. goto err;
  2059. }
  2060. der_sig = OPENSSL_malloc(sig_len);
  2061. if (!der_sig) {
  2062. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
  2063. goto err;
  2064. }
  2065. if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len)) {
  2066. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
  2067. SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
  2068. goto err;
  2069. }
  2070. derp = der_sig;
  2071. sig = d2i_ECDSA_SIG(NULL, (const uint8_t **)&derp, sig_len);
  2072. if (sig == NULL) {
  2073. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
  2074. goto err;
  2075. }
  2076. /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
  2077. memcpy(d, public_key + 1, 64);
  2078. d += 64;
  2079. if (!BN_bn2bin_padded(d, 32, sig->r) ||
  2080. !BN_bn2bin_padded(d + 32, 32, sig->s)) {
  2081. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_INTERNAL_ERROR);
  2082. goto err;
  2083. }
  2084. ssl_set_handshake_header(s, SSL3_MT_ENCRYPTED_EXTENSIONS,
  2085. 2 + 2 + TLSEXT_CHANNEL_ID_SIZE);
  2086. s->state = SSL3_ST_CW_CHANNEL_ID_B;
  2087. ret = ssl_do_write(s);
  2088. err:
  2089. EVP_MD_CTX_cleanup(&md_ctx);
  2090. if (public_key) {
  2091. OPENSSL_free(public_key);
  2092. }
  2093. if (der_sig) {
  2094. OPENSSL_free(der_sig);
  2095. }
  2096. if (sig) {
  2097. ECDSA_SIG_free(sig);
  2098. }
  2099. return ret;
  2100. }
  2101. int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) {
  2102. int i = 0;
  2103. if (s->ctx->client_cert_cb) {
  2104. i = s->ctx->client_cert_cb(s, px509, ppkey);
  2105. }
  2106. return i;
  2107. }