2
0

statem_lib.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the OpenSSL license (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <limits.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include "../ssl_locl.h"
  14. #include "statem_locl.h"
  15. #include "internal/cryptlib.h"
  16. #include <openssl/buffer.h>
  17. #include <openssl/objects.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/x509.h>
  20. /*
  21. * Map error codes to TLS/SSL alart types.
  22. */
  23. typedef struct x509err2alert_st {
  24. int x509err;
  25. int alert;
  26. } X509ERR2ALERT;
  27. /* Fixed value used in the ServerHello random field to identify an HRR */
  28. const unsigned char hrrrandom[] = {
  29. 0xcf, 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02,
  30. 0x1e, 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e,
  31. 0x07, 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c
  32. };
  33. /*
  34. * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
  35. * SSL3_RT_CHANGE_CIPHER_SPEC)
  36. */
  37. int ssl3_do_write(SSL *s, int type)
  38. {
  39. int ret;
  40. size_t written = 0;
  41. ret = ssl3_write_bytes(s, type, &s->init_buf->data[s->init_off],
  42. s->init_num, &written);
  43. if (ret < 0)
  44. return -1;
  45. if (type == SSL3_RT_HANDSHAKE)
  46. /*
  47. * should not be done for 'Hello Request's, but in that case we'll
  48. * ignore the result anyway
  49. * TLS1.3 KeyUpdate and NewSessionTicket do not need to be added
  50. */
  51. if (!SSL_IS_TLS13(s) || (s->statem.hand_state != TLS_ST_SW_SESSION_TICKET
  52. && s->statem.hand_state != TLS_ST_CW_KEY_UPDATE
  53. && s->statem.hand_state != TLS_ST_SW_KEY_UPDATE))
  54. if (!ssl3_finish_mac(s,
  55. (unsigned char *)&s->init_buf->data[s->init_off],
  56. written))
  57. return -1;
  58. if (written == s->init_num) {
  59. if (s->msg_callback)
  60. s->msg_callback(1, s->version, type, s->init_buf->data,
  61. (size_t)(s->init_off + s->init_num), s,
  62. s->msg_callback_arg);
  63. return 1;
  64. }
  65. s->init_off += written;
  66. s->init_num -= written;
  67. return 0;
  68. }
  69. int tls_close_construct_packet(SSL *s, WPACKET *pkt, int htype)
  70. {
  71. size_t msglen;
  72. if ((htype != SSL3_MT_CHANGE_CIPHER_SPEC && !WPACKET_close(pkt))
  73. || !WPACKET_get_length(pkt, &msglen)
  74. || msglen > INT_MAX)
  75. return 0;
  76. s->init_num = (int)msglen;
  77. s->init_off = 0;
  78. return 1;
  79. }
  80. int tls_setup_handshake(SSL *s)
  81. {
  82. if (!ssl3_init_finished_mac(s)) {
  83. /* SSLfatal() already called */
  84. return 0;
  85. }
  86. /* Reset any extension flags */
  87. memset(s->ext.extflags, 0, sizeof(s->ext.extflags));
  88. if (s->server) {
  89. STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(s);
  90. int i, ver_min, ver_max, ok = 0;
  91. /*
  92. * Sanity check that the maximum version we accept has ciphers
  93. * enabled. For clients we do this check during construction of the
  94. * ClientHello.
  95. */
  96. if (ssl_get_min_max_version(s, &ver_min, &ver_max) != 0) {
  97. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_SETUP_HANDSHAKE,
  98. ERR_R_INTERNAL_ERROR);
  99. return 0;
  100. }
  101. for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
  102. const SSL_CIPHER *c = sk_SSL_CIPHER_value(ciphers, i);
  103. if (SSL_IS_DTLS(s)) {
  104. if (DTLS_VERSION_GE(ver_max, c->min_dtls) &&
  105. DTLS_VERSION_LE(ver_max, c->max_dtls))
  106. ok = 1;
  107. } else if (ver_max >= c->min_tls && ver_max <= c->max_tls) {
  108. ok = 1;
  109. }
  110. if (ok)
  111. break;
  112. }
  113. if (!ok) {
  114. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_SETUP_HANDSHAKE,
  115. SSL_R_NO_CIPHERS_AVAILABLE);
  116. ERR_add_error_data(1, "No ciphers enabled for max supported "
  117. "SSL/TLS version");
  118. return 0;
  119. }
  120. if (SSL_IS_FIRST_HANDSHAKE(s)) {
  121. /* N.B. s->session_ctx == s->ctx here */
  122. CRYPTO_atomic_add(&s->session_ctx->stats.sess_accept, 1, &i,
  123. s->session_ctx->lock);
  124. } else {
  125. /* N.B. s->ctx may not equal s->session_ctx */
  126. CRYPTO_atomic_add(&s->ctx->stats.sess_accept_renegotiate, 1, &i,
  127. s->ctx->lock);
  128. s->s3->tmp.cert_request = 0;
  129. }
  130. } else {
  131. int discard;
  132. if (SSL_IS_FIRST_HANDSHAKE(s))
  133. CRYPTO_atomic_add(&s->session_ctx->stats.sess_connect, 1, &discard,
  134. s->session_ctx->lock);
  135. else
  136. CRYPTO_atomic_add(&s->session_ctx->stats.sess_connect_renegotiate,
  137. 1, &discard, s->session_ctx->lock);
  138. /* mark client_random uninitialized */
  139. memset(s->s3->client_random, 0, sizeof(s->s3->client_random));
  140. s->hit = 0;
  141. s->s3->tmp.cert_req = 0;
  142. if (SSL_IS_DTLS(s))
  143. s->statem.use_timer = 1;
  144. }
  145. return 1;
  146. }
  147. /*
  148. * Size of the to-be-signed TLS13 data, without the hash size itself:
  149. * 64 bytes of value 32, 33 context bytes, 1 byte separator
  150. */
  151. #define TLS13_TBS_START_SIZE 64
  152. #define TLS13_TBS_PREAMBLE_SIZE (TLS13_TBS_START_SIZE + 33 + 1)
  153. static int get_cert_verify_tbs_data(SSL *s, unsigned char *tls13tbs,
  154. void **hdata, size_t *hdatalen)
  155. {
  156. static const char *servercontext = "TLS 1.3, server CertificateVerify";
  157. static const char *clientcontext = "TLS 1.3, client CertificateVerify";
  158. if (SSL_IS_TLS13(s)) {
  159. size_t hashlen;
  160. /* Set the first 64 bytes of to-be-signed data to octet 32 */
  161. memset(tls13tbs, 32, TLS13_TBS_START_SIZE);
  162. /* This copies the 33 bytes of context plus the 0 separator byte */
  163. if (s->statem.hand_state == TLS_ST_CR_CERT_VRFY
  164. || s->statem.hand_state == TLS_ST_SW_CERT_VRFY)
  165. strcpy((char *)tls13tbs + TLS13_TBS_START_SIZE, servercontext);
  166. else
  167. strcpy((char *)tls13tbs + TLS13_TBS_START_SIZE, clientcontext);
  168. /*
  169. * If we're currently reading then we need to use the saved handshake
  170. * hash value. We can't use the current handshake hash state because
  171. * that includes the CertVerify itself.
  172. */
  173. if (s->statem.hand_state == TLS_ST_CR_CERT_VRFY
  174. || s->statem.hand_state == TLS_ST_SR_CERT_VRFY) {
  175. memcpy(tls13tbs + TLS13_TBS_PREAMBLE_SIZE, s->cert_verify_hash,
  176. s->cert_verify_hash_len);
  177. hashlen = s->cert_verify_hash_len;
  178. } else if (!ssl_handshake_hash(s, tls13tbs + TLS13_TBS_PREAMBLE_SIZE,
  179. EVP_MAX_MD_SIZE, &hashlen)) {
  180. /* SSLfatal() already called */
  181. return 0;
  182. }
  183. *hdata = tls13tbs;
  184. *hdatalen = TLS13_TBS_PREAMBLE_SIZE + hashlen;
  185. } else {
  186. size_t retlen;
  187. retlen = BIO_get_mem_data(s->s3->handshake_buffer, hdata);
  188. if (retlen <= 0) {
  189. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_GET_CERT_VERIFY_TBS_DATA,
  190. ERR_R_INTERNAL_ERROR);
  191. return 0;
  192. }
  193. *hdatalen = retlen;
  194. }
  195. return 1;
  196. }
  197. int tls_construct_cert_verify(SSL *s, WPACKET *pkt)
  198. {
  199. EVP_PKEY *pkey = NULL;
  200. const EVP_MD *md = NULL;
  201. EVP_MD_CTX *mctx = NULL;
  202. EVP_PKEY_CTX *pctx = NULL;
  203. size_t hdatalen = 0, siglen = 0;
  204. void *hdata;
  205. unsigned char *sig = NULL;
  206. unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE];
  207. const SIGALG_LOOKUP *lu = s->s3->tmp.sigalg;
  208. if (lu == NULL || s->s3->tmp.cert == NULL) {
  209. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
  210. ERR_R_INTERNAL_ERROR);
  211. goto err;
  212. }
  213. pkey = s->s3->tmp.cert->privatekey;
  214. if (pkey == NULL || !tls1_lookup_md(lu, &md)) {
  215. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
  216. ERR_R_INTERNAL_ERROR);
  217. goto err;
  218. }
  219. mctx = EVP_MD_CTX_new();
  220. if (mctx == NULL) {
  221. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
  222. ERR_R_MALLOC_FAILURE);
  223. goto err;
  224. }
  225. /* Get the data to be signed */
  226. if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) {
  227. /* SSLfatal() already called */
  228. goto err;
  229. }
  230. if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) {
  231. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
  232. ERR_R_INTERNAL_ERROR);
  233. goto err;
  234. }
  235. siglen = EVP_PKEY_size(pkey);
  236. sig = OPENSSL_malloc(siglen);
  237. if (sig == NULL) {
  238. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
  239. ERR_R_MALLOC_FAILURE);
  240. goto err;
  241. }
  242. if (EVP_DigestSignInit(mctx, &pctx, md, NULL, pkey) <= 0) {
  243. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
  244. ERR_R_EVP_LIB);
  245. goto err;
  246. }
  247. if (lu->sig == EVP_PKEY_RSA_PSS) {
  248. if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
  249. || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
  250. RSA_PSS_SALTLEN_DIGEST) <= 0) {
  251. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
  252. ERR_R_EVP_LIB);
  253. goto err;
  254. }
  255. }
  256. if (s->version == SSL3_VERSION) {
  257. if (EVP_DigestSignUpdate(mctx, hdata, hdatalen) <= 0
  258. || !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
  259. (int)s->session->master_key_length,
  260. s->session->master_key)
  261. || EVP_DigestSignFinal(mctx, sig, &siglen) <= 0) {
  262. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
  263. ERR_R_EVP_LIB);
  264. goto err;
  265. }
  266. } else if (EVP_DigestSign(mctx, sig, &siglen, hdata, hdatalen) <= 0) {
  267. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
  268. ERR_R_EVP_LIB);
  269. goto err;
  270. }
  271. #ifndef OPENSSL_NO_GOST
  272. {
  273. int pktype = lu->sig;
  274. if (pktype == NID_id_GostR3410_2001
  275. || pktype == NID_id_GostR3410_2012_256
  276. || pktype == NID_id_GostR3410_2012_512)
  277. BUF_reverse(sig, NULL, siglen);
  278. }
  279. #endif
  280. if (!WPACKET_sub_memcpy_u16(pkt, sig, siglen)) {
  281. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
  282. ERR_R_INTERNAL_ERROR);
  283. goto err;
  284. }
  285. /* Digest cached records and discard handshake buffer */
  286. if (!ssl3_digest_cached_records(s, 0)) {
  287. /* SSLfatal() already called */
  288. goto err;
  289. }
  290. OPENSSL_free(sig);
  291. EVP_MD_CTX_free(mctx);
  292. return 1;
  293. err:
  294. OPENSSL_free(sig);
  295. EVP_MD_CTX_free(mctx);
  296. return 0;
  297. }
  298. MSG_PROCESS_RETURN tls_process_cert_verify(SSL *s, PACKET *pkt)
  299. {
  300. EVP_PKEY *pkey = NULL;
  301. const unsigned char *data;
  302. #ifndef OPENSSL_NO_GOST
  303. unsigned char *gost_data = NULL;
  304. #endif
  305. MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
  306. int j;
  307. unsigned int len;
  308. X509 *peer;
  309. const EVP_MD *md = NULL;
  310. size_t hdatalen = 0;
  311. void *hdata;
  312. unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE];
  313. EVP_MD_CTX *mctx = EVP_MD_CTX_new();
  314. EVP_PKEY_CTX *pctx = NULL;
  315. if (mctx == NULL) {
  316. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
  317. ERR_R_MALLOC_FAILURE);
  318. goto err;
  319. }
  320. peer = s->session->peer;
  321. pkey = X509_get0_pubkey(peer);
  322. if (pkey == NULL) {
  323. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
  324. ERR_R_INTERNAL_ERROR);
  325. goto err;
  326. }
  327. if (ssl_cert_lookup_by_pkey(pkey, NULL) == NULL) {
  328. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_CERT_VERIFY,
  329. SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
  330. goto err;
  331. }
  332. if (SSL_USE_SIGALGS(s)) {
  333. unsigned int sigalg;
  334. if (!PACKET_get_net_2(pkt, &sigalg)) {
  335. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
  336. SSL_R_BAD_PACKET);
  337. goto err;
  338. }
  339. if (tls12_check_peer_sigalg(s, sigalg, pkey) <= 0) {
  340. /* SSLfatal() already called */
  341. goto err;
  342. }
  343. #ifdef SSL_DEBUG
  344. fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
  345. #endif
  346. } else if (!tls1_set_peer_legacy_sigalg(s, pkey)) {
  347. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
  348. ERR_R_INTERNAL_ERROR);
  349. goto err;
  350. }
  351. if (!tls1_lookup_md(s->s3->tmp.peer_sigalg, &md)) {
  352. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
  353. ERR_R_INTERNAL_ERROR);
  354. goto err;
  355. }
  356. /* Check for broken implementations of GOST ciphersuites */
  357. /*
  358. * If key is GOST and len is exactly 64 or 128, it is signature without
  359. * length field (CryptoPro implementations at least till TLS 1.2)
  360. */
  361. #ifndef OPENSSL_NO_GOST
  362. if (!SSL_USE_SIGALGS(s)
  363. && ((PACKET_remaining(pkt) == 64
  364. && (EVP_PKEY_id(pkey) == NID_id_GostR3410_2001
  365. || EVP_PKEY_id(pkey) == NID_id_GostR3410_2012_256))
  366. || (PACKET_remaining(pkt) == 128
  367. && EVP_PKEY_id(pkey) == NID_id_GostR3410_2012_512))) {
  368. len = PACKET_remaining(pkt);
  369. } else
  370. #endif
  371. if (!PACKET_get_net_2(pkt, &len)) {
  372. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
  373. SSL_R_LENGTH_MISMATCH);
  374. goto err;
  375. }
  376. j = EVP_PKEY_size(pkey);
  377. if (((int)len > j) || ((int)PACKET_remaining(pkt) > j)
  378. || (PACKET_remaining(pkt) == 0)) {
  379. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
  380. SSL_R_WRONG_SIGNATURE_SIZE);
  381. goto err;
  382. }
  383. if (!PACKET_get_bytes(pkt, &data, len)) {
  384. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
  385. SSL_R_LENGTH_MISMATCH);
  386. goto err;
  387. }
  388. if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) {
  389. /* SSLfatal() already called */
  390. goto err;
  391. }
  392. #ifdef SSL_DEBUG
  393. fprintf(stderr, "Using client verify alg %s\n", EVP_MD_name(md));
  394. #endif
  395. if (EVP_DigestVerifyInit(mctx, &pctx, md, NULL, pkey) <= 0) {
  396. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
  397. ERR_R_EVP_LIB);
  398. goto err;
  399. }
  400. #ifndef OPENSSL_NO_GOST
  401. {
  402. int pktype = EVP_PKEY_id(pkey);
  403. if (pktype == NID_id_GostR3410_2001
  404. || pktype == NID_id_GostR3410_2012_256
  405. || pktype == NID_id_GostR3410_2012_512) {
  406. if ((gost_data = OPENSSL_malloc(len)) == NULL) {
  407. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  408. SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_MALLOC_FAILURE);
  409. goto err;
  410. }
  411. BUF_reverse(gost_data, data, len);
  412. data = gost_data;
  413. }
  414. }
  415. #endif
  416. if (SSL_USE_PSS(s)) {
  417. if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
  418. || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
  419. RSA_PSS_SALTLEN_DIGEST) <= 0) {
  420. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
  421. ERR_R_EVP_LIB);
  422. goto err;
  423. }
  424. }
  425. if (s->version == SSL3_VERSION) {
  426. if (EVP_DigestVerifyUpdate(mctx, hdata, hdatalen) <= 0
  427. || !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
  428. (int)s->session->master_key_length,
  429. s->session->master_key)) {
  430. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
  431. ERR_R_EVP_LIB);
  432. goto err;
  433. }
  434. if (EVP_DigestVerifyFinal(mctx, data, len) <= 0) {
  435. SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
  436. SSL_R_BAD_SIGNATURE);
  437. goto err;
  438. }
  439. } else {
  440. j = EVP_DigestVerify(mctx, data, len, hdata, hdatalen);
  441. if (j <= 0) {
  442. SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
  443. SSL_R_BAD_SIGNATURE);
  444. goto err;
  445. }
  446. }
  447. ret = MSG_PROCESS_CONTINUE_READING;
  448. err:
  449. BIO_free(s->s3->handshake_buffer);
  450. s->s3->handshake_buffer = NULL;
  451. EVP_MD_CTX_free(mctx);
  452. #ifndef OPENSSL_NO_GOST
  453. OPENSSL_free(gost_data);
  454. #endif
  455. return ret;
  456. }
  457. int tls_construct_finished(SSL *s, WPACKET *pkt)
  458. {
  459. size_t finish_md_len;
  460. const char *sender;
  461. size_t slen;
  462. /* This is a real handshake so make sure we clean it up at the end */
  463. if (!s->server && s->post_handshake_auth != SSL_PHA_REQUESTED)
  464. s->statem.cleanuphand = 1;
  465. /*
  466. * We only change the keys if we didn't already do this when we sent the
  467. * client certificate
  468. */
  469. if (SSL_IS_TLS13(s)
  470. && !s->server
  471. && s->s3->tmp.cert_req == 0
  472. && (!s->method->ssl3_enc->change_cipher_state(s,
  473. SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) {;
  474. /* SSLfatal() already called */
  475. return 0;
  476. }
  477. if (s->server) {
  478. sender = s->method->ssl3_enc->server_finished_label;
  479. slen = s->method->ssl3_enc->server_finished_label_len;
  480. } else {
  481. sender = s->method->ssl3_enc->client_finished_label;
  482. slen = s->method->ssl3_enc->client_finished_label_len;
  483. }
  484. finish_md_len = s->method->ssl3_enc->final_finish_mac(s,
  485. sender, slen,
  486. s->s3->tmp.finish_md);
  487. if (finish_md_len == 0) {
  488. /* SSLfatal() already called */
  489. return 0;
  490. }
  491. s->s3->tmp.finish_md_len = finish_md_len;
  492. if (!WPACKET_memcpy(pkt, s->s3->tmp.finish_md, finish_md_len)) {
  493. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_FINISHED,
  494. ERR_R_INTERNAL_ERROR);
  495. return 0;
  496. }
  497. /*
  498. * Log the master secret, if logging is enabled. We don't log it for
  499. * TLSv1.3: there's a different key schedule for that.
  500. */
  501. if (!SSL_IS_TLS13(s) && !ssl_log_secret(s, MASTER_SECRET_LABEL,
  502. s->session->master_key,
  503. s->session->master_key_length)) {
  504. /* SSLfatal() already called */
  505. return 0;
  506. }
  507. /*
  508. * Copy the finished so we can use it for renegotiation checks
  509. */
  510. if (!ossl_assert(finish_md_len <= EVP_MAX_MD_SIZE)) {
  511. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_FINISHED,
  512. ERR_R_INTERNAL_ERROR);
  513. return 0;
  514. }
  515. if (!s->server) {
  516. memcpy(s->s3->previous_client_finished, s->s3->tmp.finish_md,
  517. finish_md_len);
  518. s->s3->previous_client_finished_len = finish_md_len;
  519. } else {
  520. memcpy(s->s3->previous_server_finished, s->s3->tmp.finish_md,
  521. finish_md_len);
  522. s->s3->previous_server_finished_len = finish_md_len;
  523. }
  524. return 1;
  525. }
  526. int tls_construct_key_update(SSL *s, WPACKET *pkt)
  527. {
  528. if (!WPACKET_put_bytes_u8(pkt, s->key_update)) {
  529. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_KEY_UPDATE,
  530. ERR_R_INTERNAL_ERROR);
  531. return 0;
  532. }
  533. s->key_update = SSL_KEY_UPDATE_NONE;
  534. return 1;
  535. }
  536. MSG_PROCESS_RETURN tls_process_key_update(SSL *s, PACKET *pkt)
  537. {
  538. unsigned int updatetype;
  539. s->key_update_count++;
  540. if (s->key_update_count > MAX_KEY_UPDATE_MESSAGES) {
  541. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_KEY_UPDATE,
  542. SSL_R_TOO_MANY_KEY_UPDATES);
  543. return MSG_PROCESS_ERROR;
  544. }
  545. /*
  546. * A KeyUpdate message signals a key change so the end of the message must
  547. * be on a record boundary.
  548. */
  549. if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
  550. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_TLS_PROCESS_KEY_UPDATE,
  551. SSL_R_NOT_ON_RECORD_BOUNDARY);
  552. return MSG_PROCESS_ERROR;
  553. }
  554. if (!PACKET_get_1(pkt, &updatetype)
  555. || PACKET_remaining(pkt) != 0) {
  556. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_KEY_UPDATE,
  557. SSL_R_BAD_KEY_UPDATE);
  558. return MSG_PROCESS_ERROR;
  559. }
  560. /*
  561. * There are only two defined key update types. Fail if we get a value we
  562. * didn't recognise.
  563. */
  564. if (updatetype != SSL_KEY_UPDATE_NOT_REQUESTED
  565. && updatetype != SSL_KEY_UPDATE_REQUESTED) {
  566. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_KEY_UPDATE,
  567. SSL_R_BAD_KEY_UPDATE);
  568. return MSG_PROCESS_ERROR;
  569. }
  570. /*
  571. * If we get a request for us to update our sending keys too then, we need
  572. * to additionally send a KeyUpdate message. However that message should
  573. * not also request an update (otherwise we get into an infinite loop).
  574. */
  575. if (updatetype == SSL_KEY_UPDATE_REQUESTED)
  576. s->key_update = SSL_KEY_UPDATE_NOT_REQUESTED;
  577. if (!tls13_update_key(s, 0)) {
  578. /* SSLfatal() already called */
  579. return MSG_PROCESS_ERROR;
  580. }
  581. return MSG_PROCESS_FINISHED_READING;
  582. }
  583. /*
  584. * ssl3_take_mac calculates the Finished MAC for the handshakes messages seen
  585. * to far.
  586. */
  587. int ssl3_take_mac(SSL *s)
  588. {
  589. const char *sender;
  590. size_t slen;
  591. if (!s->server) {
  592. sender = s->method->ssl3_enc->server_finished_label;
  593. slen = s->method->ssl3_enc->server_finished_label_len;
  594. } else {
  595. sender = s->method->ssl3_enc->client_finished_label;
  596. slen = s->method->ssl3_enc->client_finished_label_len;
  597. }
  598. s->s3->tmp.peer_finish_md_len =
  599. s->method->ssl3_enc->final_finish_mac(s, sender, slen,
  600. s->s3->tmp.peer_finish_md);
  601. if (s->s3->tmp.peer_finish_md_len == 0) {
  602. /* SSLfatal() already called */
  603. return 0;
  604. }
  605. return 1;
  606. }
  607. MSG_PROCESS_RETURN tls_process_change_cipher_spec(SSL *s, PACKET *pkt)
  608. {
  609. size_t remain;
  610. remain = PACKET_remaining(pkt);
  611. /*
  612. * 'Change Cipher Spec' is just a single byte, which should already have
  613. * been consumed by ssl_get_message() so there should be no bytes left,
  614. * unless we're using DTLS1_BAD_VER, which has an extra 2 bytes
  615. */
  616. if (SSL_IS_DTLS(s)) {
  617. if ((s->version == DTLS1_BAD_VER
  618. && remain != DTLS1_CCS_HEADER_LENGTH + 1)
  619. || (s->version != DTLS1_BAD_VER
  620. && remain != DTLS1_CCS_HEADER_LENGTH - 1)) {
  621. SSLfatal(s, SSL_AD_DECODE_ERROR,
  622. SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC,
  623. SSL_R_BAD_CHANGE_CIPHER_SPEC);
  624. return MSG_PROCESS_ERROR;
  625. }
  626. } else {
  627. if (remain != 0) {
  628. SSLfatal(s, SSL_AD_DECODE_ERROR,
  629. SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC,
  630. SSL_R_BAD_CHANGE_CIPHER_SPEC);
  631. return MSG_PROCESS_ERROR;
  632. }
  633. }
  634. /* Check we have a cipher to change to */
  635. if (s->s3->tmp.new_cipher == NULL) {
  636. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
  637. SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, SSL_R_CCS_RECEIVED_EARLY);
  638. return MSG_PROCESS_ERROR;
  639. }
  640. s->s3->change_cipher_spec = 1;
  641. if (!ssl3_do_change_cipher_spec(s)) {
  642. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC,
  643. ERR_R_INTERNAL_ERROR);
  644. return MSG_PROCESS_ERROR;
  645. }
  646. if (SSL_IS_DTLS(s)) {
  647. dtls1_reset_seq_numbers(s, SSL3_CC_READ);
  648. if (s->version == DTLS1_BAD_VER)
  649. s->d1->handshake_read_seq++;
  650. #ifndef OPENSSL_NO_SCTP
  651. /*
  652. * Remember that a CCS has been received, so that an old key of
  653. * SCTP-Auth can be deleted when a CCS is sent. Will be ignored if no
  654. * SCTP is used
  655. */
  656. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD, 1, NULL);
  657. #endif
  658. }
  659. return MSG_PROCESS_CONTINUE_READING;
  660. }
  661. MSG_PROCESS_RETURN tls_process_finished(SSL *s, PACKET *pkt)
  662. {
  663. size_t md_len;
  664. /* This is a real handshake so make sure we clean it up at the end */
  665. if (s->server) {
  666. if (s->post_handshake_auth != SSL_PHA_REQUESTED)
  667. s->statem.cleanuphand = 1;
  668. if (SSL_IS_TLS13(s) && !tls13_save_handshake_digest_for_pha(s)) {
  669. /* SSLfatal() already called */
  670. return MSG_PROCESS_ERROR;
  671. }
  672. }
  673. /*
  674. * In TLSv1.3 a Finished message signals a key change so the end of the
  675. * message must be on a record boundary.
  676. */
  677. if (SSL_IS_TLS13(s) && RECORD_LAYER_processed_read_pending(&s->rlayer)) {
  678. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_TLS_PROCESS_FINISHED,
  679. SSL_R_NOT_ON_RECORD_BOUNDARY);
  680. return MSG_PROCESS_ERROR;
  681. }
  682. /* If this occurs, we have missed a message */
  683. if (!SSL_IS_TLS13(s) && !s->s3->change_cipher_spec) {
  684. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_TLS_PROCESS_FINISHED,
  685. SSL_R_GOT_A_FIN_BEFORE_A_CCS);
  686. return MSG_PROCESS_ERROR;
  687. }
  688. s->s3->change_cipher_spec = 0;
  689. md_len = s->s3->tmp.peer_finish_md_len;
  690. if (md_len != PACKET_remaining(pkt)) {
  691. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_FINISHED,
  692. SSL_R_BAD_DIGEST_LENGTH);
  693. return MSG_PROCESS_ERROR;
  694. }
  695. if (CRYPTO_memcmp(PACKET_data(pkt), s->s3->tmp.peer_finish_md,
  696. md_len) != 0) {
  697. SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_FINISHED,
  698. SSL_R_DIGEST_CHECK_FAILED);
  699. return MSG_PROCESS_ERROR;
  700. }
  701. /*
  702. * Copy the finished so we can use it for renegotiation checks
  703. */
  704. if (!ossl_assert(md_len <= EVP_MAX_MD_SIZE)) {
  705. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_FINISHED,
  706. ERR_R_INTERNAL_ERROR);
  707. return MSG_PROCESS_ERROR;
  708. }
  709. if (s->server) {
  710. memcpy(s->s3->previous_client_finished, s->s3->tmp.peer_finish_md,
  711. md_len);
  712. s->s3->previous_client_finished_len = md_len;
  713. } else {
  714. memcpy(s->s3->previous_server_finished, s->s3->tmp.peer_finish_md,
  715. md_len);
  716. s->s3->previous_server_finished_len = md_len;
  717. }
  718. /*
  719. * In TLS1.3 we also have to change cipher state and do any final processing
  720. * of the initial server flight (if we are a client)
  721. */
  722. if (SSL_IS_TLS13(s)) {
  723. if (s->server) {
  724. if (s->post_handshake_auth != SSL_PHA_REQUESTED &&
  725. !s->method->ssl3_enc->change_cipher_state(s,
  726. SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_READ)) {
  727. /* SSLfatal() already called */
  728. return MSG_PROCESS_ERROR;
  729. }
  730. } else {
  731. if (!s->method->ssl3_enc->generate_master_secret(s,
  732. s->master_secret, s->handshake_secret, 0,
  733. &s->session->master_key_length)) {
  734. /* SSLfatal() already called */
  735. return MSG_PROCESS_ERROR;
  736. }
  737. if (!s->method->ssl3_enc->change_cipher_state(s,
  738. SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_READ)) {
  739. /* SSLfatal() already called */
  740. return MSG_PROCESS_ERROR;
  741. }
  742. if (!tls_process_initial_server_flight(s)) {
  743. /* SSLfatal() already called */
  744. return MSG_PROCESS_ERROR;
  745. }
  746. }
  747. }
  748. return MSG_PROCESS_FINISHED_READING;
  749. }
  750. int tls_construct_change_cipher_spec(SSL *s, WPACKET *pkt)
  751. {
  752. if (!WPACKET_put_bytes_u8(pkt, SSL3_MT_CCS)) {
  753. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  754. SSL_F_TLS_CONSTRUCT_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR);
  755. return 0;
  756. }
  757. return 1;
  758. }
  759. /* Add a certificate to the WPACKET */
  760. static int ssl_add_cert_to_wpacket(SSL *s, WPACKET *pkt, X509 *x, int chain)
  761. {
  762. int len;
  763. unsigned char *outbytes;
  764. len = i2d_X509(x, NULL);
  765. if (len < 0) {
  766. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_TO_WPACKET,
  767. ERR_R_BUF_LIB);
  768. return 0;
  769. }
  770. if (!WPACKET_sub_allocate_bytes_u24(pkt, len, &outbytes)
  771. || i2d_X509(x, &outbytes) != len) {
  772. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_TO_WPACKET,
  773. ERR_R_INTERNAL_ERROR);
  774. return 0;
  775. }
  776. if (SSL_IS_TLS13(s)
  777. && !tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_CERTIFICATE, x,
  778. chain)) {
  779. /* SSLfatal() already called */
  780. return 0;
  781. }
  782. return 1;
  783. }
  784. /* Add certificate chain to provided WPACKET */
  785. static int ssl_add_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk)
  786. {
  787. int i, chain_count;
  788. X509 *x;
  789. STACK_OF(X509) *extra_certs;
  790. STACK_OF(X509) *chain = NULL;
  791. X509_STORE *chain_store;
  792. if (cpk == NULL || cpk->x509 == NULL)
  793. return 1;
  794. x = cpk->x509;
  795. /*
  796. * If we have a certificate specific chain use it, else use parent ctx.
  797. */
  798. if (cpk->chain != NULL)
  799. extra_certs = cpk->chain;
  800. else
  801. extra_certs = s->ctx->extra_certs;
  802. if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)
  803. chain_store = NULL;
  804. else if (s->cert->chain_store)
  805. chain_store = s->cert->chain_store;
  806. else
  807. chain_store = s->ctx->cert_store;
  808. if (chain_store != NULL) {
  809. X509_STORE_CTX *xs_ctx = X509_STORE_CTX_new();
  810. if (xs_ctx == NULL) {
  811. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_CHAIN,
  812. ERR_R_MALLOC_FAILURE);
  813. return 0;
  814. }
  815. if (!X509_STORE_CTX_init(xs_ctx, chain_store, x, NULL)) {
  816. X509_STORE_CTX_free(xs_ctx);
  817. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_CHAIN,
  818. ERR_R_X509_LIB);
  819. return 0;
  820. }
  821. /*
  822. * It is valid for the chain not to be complete (because normally we
  823. * don't include the root cert in the chain). Therefore we deliberately
  824. * ignore the error return from this call. We're not actually verifying
  825. * the cert - we're just building as much of the chain as we can
  826. */
  827. (void)X509_verify_cert(xs_ctx);
  828. /* Don't leave errors in the queue */
  829. ERR_clear_error();
  830. chain = X509_STORE_CTX_get0_chain(xs_ctx);
  831. i = ssl_security_cert_chain(s, chain, NULL, 0);
  832. if (i != 1) {
  833. #if 0
  834. /* Dummy error calls so mkerr generates them */
  835. SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_EE_KEY_TOO_SMALL);
  836. SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_KEY_TOO_SMALL);
  837. SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_MD_TOO_WEAK);
  838. #endif
  839. X509_STORE_CTX_free(xs_ctx);
  840. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_CHAIN, i);
  841. return 0;
  842. }
  843. chain_count = sk_X509_num(chain);
  844. for (i = 0; i < chain_count; i++) {
  845. x = sk_X509_value(chain, i);
  846. if (!ssl_add_cert_to_wpacket(s, pkt, x, i)) {
  847. /* SSLfatal() already called */
  848. X509_STORE_CTX_free(xs_ctx);
  849. return 0;
  850. }
  851. }
  852. X509_STORE_CTX_free(xs_ctx);
  853. } else {
  854. i = ssl_security_cert_chain(s, extra_certs, x, 0);
  855. if (i != 1) {
  856. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_CHAIN, i);
  857. return 0;
  858. }
  859. if (!ssl_add_cert_to_wpacket(s, pkt, x, 0)) {
  860. /* SSLfatal() already called */
  861. return 0;
  862. }
  863. for (i = 0; i < sk_X509_num(extra_certs); i++) {
  864. x = sk_X509_value(extra_certs, i);
  865. if (!ssl_add_cert_to_wpacket(s, pkt, x, i + 1)) {
  866. /* SSLfatal() already called */
  867. return 0;
  868. }
  869. }
  870. }
  871. return 1;
  872. }
  873. unsigned long ssl3_output_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk)
  874. {
  875. if (!WPACKET_start_sub_packet_u24(pkt)) {
  876. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_OUTPUT_CERT_CHAIN,
  877. ERR_R_INTERNAL_ERROR);
  878. return 0;
  879. }
  880. if (!ssl_add_cert_chain(s, pkt, cpk))
  881. return 0;
  882. if (!WPACKET_close(pkt)) {
  883. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_OUTPUT_CERT_CHAIN,
  884. ERR_R_INTERNAL_ERROR);
  885. return 0;
  886. }
  887. return 1;
  888. }
  889. /*
  890. * Tidy up after the end of a handshake. In the case of SCTP this may result
  891. * in NBIO events. If |clearbufs| is set then init_buf and the wbio buffer is
  892. * freed up as well.
  893. */
  894. WORK_STATE tls_finish_handshake(SSL *s, WORK_STATE wst, int clearbufs, int stop)
  895. {
  896. int discard;
  897. void (*cb) (const SSL *ssl, int type, int val) = NULL;
  898. if (clearbufs) {
  899. if (!SSL_IS_DTLS(s)) {
  900. /*
  901. * We don't do this in DTLS because we may still need the init_buf
  902. * in case there are any unexpected retransmits
  903. */
  904. BUF_MEM_free(s->init_buf);
  905. s->init_buf = NULL;
  906. }
  907. if (!ssl_free_wbio_buffer(s)) {
  908. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_FINISH_HANDSHAKE,
  909. ERR_R_INTERNAL_ERROR);
  910. return WORK_ERROR;
  911. }
  912. s->init_num = 0;
  913. }
  914. if (SSL_IS_TLS13(s) && !s->server
  915. && s->post_handshake_auth == SSL_PHA_REQUESTED)
  916. s->post_handshake_auth = SSL_PHA_EXT_SENT;
  917. /*
  918. * Only set if there was a Finished message and this isn't after a TLSv1.3
  919. * post handshake exchange
  920. */
  921. if (s->statem.cleanuphand) {
  922. /* skipped if we just sent a HelloRequest */
  923. s->renegotiate = 0;
  924. s->new_session = 0;
  925. s->statem.cleanuphand = 0;
  926. s->ext.ticket_expected = 0;
  927. ssl3_cleanup_key_block(s);
  928. if (s->server) {
  929. /*
  930. * In TLSv1.3 we update the cache as part of constructing the
  931. * NewSessionTicket
  932. */
  933. if (!SSL_IS_TLS13(s))
  934. ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
  935. /* N.B. s->ctx may not equal s->session_ctx */
  936. CRYPTO_atomic_add(&s->ctx->stats.sess_accept_good, 1, &discard,
  937. s->ctx->lock);
  938. s->handshake_func = ossl_statem_accept;
  939. if (SSL_IS_DTLS(s) && !s->hit) {
  940. /*
  941. * We are finishing after the client. We start the timer going
  942. * in case there are any retransmits of our final flight
  943. * required.
  944. */
  945. dtls1_start_timer(s);
  946. }
  947. } else {
  948. /*
  949. * In TLSv1.3 we update the cache as part of processing the
  950. * NewSessionTicket
  951. */
  952. if (!SSL_IS_TLS13(s))
  953. ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
  954. if (s->hit)
  955. CRYPTO_atomic_add(&s->session_ctx->stats.sess_hit, 1, &discard,
  956. s->session_ctx->lock);
  957. s->handshake_func = ossl_statem_connect;
  958. CRYPTO_atomic_add(&s->session_ctx->stats.sess_connect_good, 1,
  959. &discard, s->session_ctx->lock);
  960. if (SSL_IS_DTLS(s) && s->hit) {
  961. /*
  962. * We are finishing after the server. We start the timer going
  963. * in case there are any retransmits of our final flight
  964. * required.
  965. */
  966. dtls1_start_timer(s);
  967. }
  968. }
  969. if (SSL_IS_DTLS(s)) {
  970. /* done with handshaking */
  971. s->d1->handshake_read_seq = 0;
  972. s->d1->handshake_write_seq = 0;
  973. s->d1->next_handshake_write_seq = 0;
  974. dtls1_clear_received_buffer(s);
  975. }
  976. }
  977. if (s->info_callback != NULL)
  978. cb = s->info_callback;
  979. else if (s->ctx->info_callback != NULL)
  980. cb = s->ctx->info_callback;
  981. /* The callback may expect us to not be in init at handshake done */
  982. ossl_statem_set_in_init(s, 0);
  983. if (cb != NULL)
  984. cb(s, SSL_CB_HANDSHAKE_DONE, 1);
  985. if (!stop) {
  986. /* If we've got more work to do we go back into init */
  987. ossl_statem_set_in_init(s, 1);
  988. return WORK_FINISHED_CONTINUE;
  989. }
  990. return WORK_FINISHED_STOP;
  991. }
  992. int tls_get_message_header(SSL *s, int *mt)
  993. {
  994. /* s->init_num < SSL3_HM_HEADER_LENGTH */
  995. int skip_message, i, recvd_type;
  996. unsigned char *p;
  997. size_t l, readbytes;
  998. p = (unsigned char *)s->init_buf->data;
  999. do {
  1000. while (s->init_num < SSL3_HM_HEADER_LENGTH) {
  1001. i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type,
  1002. &p[s->init_num],
  1003. SSL3_HM_HEADER_LENGTH - s->init_num,
  1004. 0, &readbytes);
  1005. if (i <= 0) {
  1006. s->rwstate = SSL_READING;
  1007. return 0;
  1008. }
  1009. if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
  1010. /*
  1011. * A ChangeCipherSpec must be a single byte and may not occur
  1012. * in the middle of a handshake message.
  1013. */
  1014. if (s->init_num != 0 || readbytes != 1 || p[0] != SSL3_MT_CCS) {
  1015. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
  1016. SSL_F_TLS_GET_MESSAGE_HEADER,
  1017. SSL_R_BAD_CHANGE_CIPHER_SPEC);
  1018. return 0;
  1019. }
  1020. if (s->statem.hand_state == TLS_ST_BEFORE
  1021. && (s->s3->flags & TLS1_FLAGS_STATELESS) != 0) {
  1022. /*
  1023. * We are stateless and we received a CCS. Probably this is
  1024. * from a client between the first and second ClientHellos.
  1025. * We should ignore this, but return an error because we do
  1026. * not return success until we see the second ClientHello
  1027. * with a valid cookie.
  1028. */
  1029. return 0;
  1030. }
  1031. s->s3->tmp.message_type = *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
  1032. s->init_num = readbytes - 1;
  1033. s->init_msg = s->init_buf->data;
  1034. s->s3->tmp.message_size = readbytes;
  1035. return 1;
  1036. } else if (recvd_type != SSL3_RT_HANDSHAKE) {
  1037. SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
  1038. SSL_F_TLS_GET_MESSAGE_HEADER,
  1039. SSL_R_CCS_RECEIVED_EARLY);
  1040. return 0;
  1041. }
  1042. s->init_num += readbytes;
  1043. }
  1044. skip_message = 0;
  1045. if (!s->server)
  1046. if (s->statem.hand_state != TLS_ST_OK
  1047. && p[0] == SSL3_MT_HELLO_REQUEST)
  1048. /*
  1049. * The server may always send 'Hello Request' messages --
  1050. * we are doing a handshake anyway now, so ignore them if
  1051. * their format is correct. Does not count for 'Finished'
  1052. * MAC.
  1053. */
  1054. if (p[1] == 0 && p[2] == 0 && p[3] == 0) {
  1055. s->init_num = 0;
  1056. skip_message = 1;
  1057. if (s->msg_callback)
  1058. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
  1059. p, SSL3_HM_HEADER_LENGTH, s,
  1060. s->msg_callback_arg);
  1061. }
  1062. } while (skip_message);
  1063. /* s->init_num == SSL3_HM_HEADER_LENGTH */
  1064. *mt = *p;
  1065. s->s3->tmp.message_type = *(p++);
  1066. if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
  1067. /*
  1068. * Only happens with SSLv3+ in an SSLv2 backward compatible
  1069. * ClientHello
  1070. *
  1071. * Total message size is the remaining record bytes to read
  1072. * plus the SSL3_HM_HEADER_LENGTH bytes that we already read
  1073. */
  1074. l = RECORD_LAYER_get_rrec_length(&s->rlayer)
  1075. + SSL3_HM_HEADER_LENGTH;
  1076. s->s3->tmp.message_size = l;
  1077. s->init_msg = s->init_buf->data;
  1078. s->init_num = SSL3_HM_HEADER_LENGTH;
  1079. } else {
  1080. n2l3(p, l);
  1081. /* BUF_MEM_grow takes an 'int' parameter */
  1082. if (l > (INT_MAX - SSL3_HM_HEADER_LENGTH)) {
  1083. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_GET_MESSAGE_HEADER,
  1084. SSL_R_EXCESSIVE_MESSAGE_SIZE);
  1085. return 0;
  1086. }
  1087. s->s3->tmp.message_size = l;
  1088. s->init_msg = s->init_buf->data + SSL3_HM_HEADER_LENGTH;
  1089. s->init_num = 0;
  1090. }
  1091. return 1;
  1092. }
  1093. int tls_get_message_body(SSL *s, size_t *len)
  1094. {
  1095. size_t n, readbytes;
  1096. unsigned char *p;
  1097. int i;
  1098. if (s->s3->tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC) {
  1099. /* We've already read everything in */
  1100. *len = (unsigned long)s->init_num;
  1101. return 1;
  1102. }
  1103. p = s->init_msg;
  1104. n = s->s3->tmp.message_size - s->init_num;
  1105. while (n > 0) {
  1106. i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
  1107. &p[s->init_num], n, 0, &readbytes);
  1108. if (i <= 0) {
  1109. s->rwstate = SSL_READING;
  1110. *len = 0;
  1111. return 0;
  1112. }
  1113. s->init_num += readbytes;
  1114. n -= readbytes;
  1115. }
  1116. /*
  1117. * If receiving Finished, record MAC of prior handshake messages for
  1118. * Finished verification.
  1119. */
  1120. if (*(s->init_buf->data) == SSL3_MT_FINISHED && !ssl3_take_mac(s)) {
  1121. /* SSLfatal() already called */
  1122. *len = 0;
  1123. return 0;
  1124. }
  1125. /* Feed this message into MAC computation. */
  1126. if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
  1127. if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
  1128. s->init_num)) {
  1129. /* SSLfatal() already called */
  1130. *len = 0;
  1131. return 0;
  1132. }
  1133. if (s->msg_callback)
  1134. s->msg_callback(0, SSL2_VERSION, 0, s->init_buf->data,
  1135. (size_t)s->init_num, s, s->msg_callback_arg);
  1136. } else {
  1137. /*
  1138. * We defer feeding in the HRR until later. We'll do it as part of
  1139. * processing the message
  1140. * The TLsv1.3 handshake transcript stops at the ClientFinished
  1141. * message.
  1142. */
  1143. #define SERVER_HELLO_RANDOM_OFFSET (SSL3_HM_HEADER_LENGTH + 2)
  1144. /* KeyUpdate and NewSessionTicket do not need to be added */
  1145. if (!SSL_IS_TLS13(s) || (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET
  1146. && s->s3->tmp.message_type != SSL3_MT_KEY_UPDATE)) {
  1147. if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO
  1148. || s->init_num < SERVER_HELLO_RANDOM_OFFSET + SSL3_RANDOM_SIZE
  1149. || memcmp(hrrrandom,
  1150. s->init_buf->data + SERVER_HELLO_RANDOM_OFFSET,
  1151. SSL3_RANDOM_SIZE) != 0) {
  1152. if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
  1153. s->init_num + SSL3_HM_HEADER_LENGTH)) {
  1154. /* SSLfatal() already called */
  1155. *len = 0;
  1156. return 0;
  1157. }
  1158. }
  1159. }
  1160. if (s->msg_callback)
  1161. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data,
  1162. (size_t)s->init_num + SSL3_HM_HEADER_LENGTH, s,
  1163. s->msg_callback_arg);
  1164. }
  1165. *len = s->init_num;
  1166. return 1;
  1167. }
  1168. static const X509ERR2ALERT x509table[] = {
  1169. {X509_V_ERR_APPLICATION_VERIFICATION, SSL_AD_HANDSHAKE_FAILURE},
  1170. {X509_V_ERR_CA_KEY_TOO_SMALL, SSL_AD_BAD_CERTIFICATE},
  1171. {X509_V_ERR_CA_MD_TOO_WEAK, SSL_AD_BAD_CERTIFICATE},
  1172. {X509_V_ERR_CERT_CHAIN_TOO_LONG, SSL_AD_UNKNOWN_CA},
  1173. {X509_V_ERR_CERT_HAS_EXPIRED, SSL_AD_CERTIFICATE_EXPIRED},
  1174. {X509_V_ERR_CERT_NOT_YET_VALID, SSL_AD_BAD_CERTIFICATE},
  1175. {X509_V_ERR_CERT_REJECTED, SSL_AD_BAD_CERTIFICATE},
  1176. {X509_V_ERR_CERT_REVOKED, SSL_AD_CERTIFICATE_REVOKED},
  1177. {X509_V_ERR_CERT_SIGNATURE_FAILURE, SSL_AD_DECRYPT_ERROR},
  1178. {X509_V_ERR_CERT_UNTRUSTED, SSL_AD_BAD_CERTIFICATE},
  1179. {X509_V_ERR_CRL_HAS_EXPIRED, SSL_AD_CERTIFICATE_EXPIRED},
  1180. {X509_V_ERR_CRL_NOT_YET_VALID, SSL_AD_BAD_CERTIFICATE},
  1181. {X509_V_ERR_CRL_SIGNATURE_FAILURE, SSL_AD_DECRYPT_ERROR},
  1182. {X509_V_ERR_DANE_NO_MATCH, SSL_AD_BAD_CERTIFICATE},
  1183. {X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT, SSL_AD_UNKNOWN_CA},
  1184. {X509_V_ERR_EE_KEY_TOO_SMALL, SSL_AD_BAD_CERTIFICATE},
  1185. {X509_V_ERR_EMAIL_MISMATCH, SSL_AD_BAD_CERTIFICATE},
  1186. {X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD, SSL_AD_BAD_CERTIFICATE},
  1187. {X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD, SSL_AD_BAD_CERTIFICATE},
  1188. {X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD, SSL_AD_BAD_CERTIFICATE},
  1189. {X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD, SSL_AD_BAD_CERTIFICATE},
  1190. {X509_V_ERR_HOSTNAME_MISMATCH, SSL_AD_BAD_CERTIFICATE},
  1191. {X509_V_ERR_INVALID_CA, SSL_AD_UNKNOWN_CA},
  1192. {X509_V_ERR_INVALID_CALL, SSL_AD_INTERNAL_ERROR},
  1193. {X509_V_ERR_INVALID_PURPOSE, SSL_AD_UNSUPPORTED_CERTIFICATE},
  1194. {X509_V_ERR_IP_ADDRESS_MISMATCH, SSL_AD_BAD_CERTIFICATE},
  1195. {X509_V_ERR_OUT_OF_MEM, SSL_AD_INTERNAL_ERROR},
  1196. {X509_V_ERR_PATH_LENGTH_EXCEEDED, SSL_AD_UNKNOWN_CA},
  1197. {X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN, SSL_AD_UNKNOWN_CA},
  1198. {X509_V_ERR_STORE_LOOKUP, SSL_AD_INTERNAL_ERROR},
  1199. {X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY, SSL_AD_BAD_CERTIFICATE},
  1200. {X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE, SSL_AD_BAD_CERTIFICATE},
  1201. {X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE, SSL_AD_BAD_CERTIFICATE},
  1202. {X509_V_ERR_UNABLE_TO_GET_CRL, SSL_AD_UNKNOWN_CA},
  1203. {X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER, SSL_AD_UNKNOWN_CA},
  1204. {X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT, SSL_AD_UNKNOWN_CA},
  1205. {X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, SSL_AD_UNKNOWN_CA},
  1206. {X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE, SSL_AD_UNKNOWN_CA},
  1207. {X509_V_ERR_UNSPECIFIED, SSL_AD_INTERNAL_ERROR},
  1208. /* Last entry; return this if we don't find the value above. */
  1209. {X509_V_OK, SSL_AD_CERTIFICATE_UNKNOWN}
  1210. };
  1211. int ssl_x509err2alert(int x509err)
  1212. {
  1213. const X509ERR2ALERT *tp;
  1214. for (tp = x509table; tp->x509err != X509_V_OK; ++tp)
  1215. if (tp->x509err == x509err)
  1216. break;
  1217. return tp->alert;
  1218. }
  1219. int ssl_allow_compression(SSL *s)
  1220. {
  1221. if (s->options & SSL_OP_NO_COMPRESSION)
  1222. return 0;
  1223. return ssl_security(s, SSL_SECOP_COMPRESSION, 0, 0, NULL);
  1224. }
  1225. static int version_cmp(const SSL *s, int a, int b)
  1226. {
  1227. int dtls = SSL_IS_DTLS(s);
  1228. if (a == b)
  1229. return 0;
  1230. if (!dtls)
  1231. return a < b ? -1 : 1;
  1232. return DTLS_VERSION_LT(a, b) ? -1 : 1;
  1233. }
  1234. typedef struct {
  1235. int version;
  1236. const SSL_METHOD *(*cmeth) (void);
  1237. const SSL_METHOD *(*smeth) (void);
  1238. } version_info;
  1239. #if TLS_MAX_VERSION != TLS1_3_VERSION
  1240. # error Code needs update for TLS_method() support beyond TLS1_3_VERSION.
  1241. #endif
  1242. /* Must be in order high to low */
  1243. static const version_info tls_version_table[] = {
  1244. #ifndef OPENSSL_NO_TLS1_3
  1245. {TLS1_3_VERSION, tlsv1_3_client_method, tlsv1_3_server_method},
  1246. #else
  1247. {TLS1_3_VERSION, NULL, NULL},
  1248. #endif
  1249. #ifndef OPENSSL_NO_TLS1_2
  1250. {TLS1_2_VERSION, tlsv1_2_client_method, tlsv1_2_server_method},
  1251. #else
  1252. {TLS1_2_VERSION, NULL, NULL},
  1253. #endif
  1254. #ifndef OPENSSL_NO_TLS1_1
  1255. {TLS1_1_VERSION, tlsv1_1_client_method, tlsv1_1_server_method},
  1256. #else
  1257. {TLS1_1_VERSION, NULL, NULL},
  1258. #endif
  1259. #ifndef OPENSSL_NO_TLS1
  1260. {TLS1_VERSION, tlsv1_client_method, tlsv1_server_method},
  1261. #else
  1262. {TLS1_VERSION, NULL, NULL},
  1263. #endif
  1264. #ifndef OPENSSL_NO_SSL3
  1265. {SSL3_VERSION, sslv3_client_method, sslv3_server_method},
  1266. #else
  1267. {SSL3_VERSION, NULL, NULL},
  1268. #endif
  1269. {0, NULL, NULL},
  1270. };
  1271. #if DTLS_MAX_VERSION != DTLS1_2_VERSION
  1272. # error Code needs update for DTLS_method() support beyond DTLS1_2_VERSION.
  1273. #endif
  1274. /* Must be in order high to low */
  1275. static const version_info dtls_version_table[] = {
  1276. #ifndef OPENSSL_NO_DTLS1_2
  1277. {DTLS1_2_VERSION, dtlsv1_2_client_method, dtlsv1_2_server_method},
  1278. #else
  1279. {DTLS1_2_VERSION, NULL, NULL},
  1280. #endif
  1281. #ifndef OPENSSL_NO_DTLS1
  1282. {DTLS1_VERSION, dtlsv1_client_method, dtlsv1_server_method},
  1283. {DTLS1_BAD_VER, dtls_bad_ver_client_method, NULL},
  1284. #else
  1285. {DTLS1_VERSION, NULL, NULL},
  1286. {DTLS1_BAD_VER, NULL, NULL},
  1287. #endif
  1288. {0, NULL, NULL},
  1289. };
  1290. /*
  1291. * ssl_method_error - Check whether an SSL_METHOD is enabled.
  1292. *
  1293. * @s: The SSL handle for the candidate method
  1294. * @method: the intended method.
  1295. *
  1296. * Returns 0 on success, or an SSL error reason on failure.
  1297. */
  1298. static int ssl_method_error(const SSL *s, const SSL_METHOD *method)
  1299. {
  1300. int version = method->version;
  1301. if ((s->min_proto_version != 0 &&
  1302. version_cmp(s, version, s->min_proto_version) < 0) ||
  1303. ssl_security(s, SSL_SECOP_VERSION, 0, version, NULL) == 0)
  1304. return SSL_R_VERSION_TOO_LOW;
  1305. if (s->max_proto_version != 0 &&
  1306. version_cmp(s, version, s->max_proto_version) > 0)
  1307. return SSL_R_VERSION_TOO_HIGH;
  1308. if ((s->options & method->mask) != 0)
  1309. return SSL_R_UNSUPPORTED_PROTOCOL;
  1310. if ((method->flags & SSL_METHOD_NO_SUITEB) != 0 && tls1_suiteb(s))
  1311. return SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE;
  1312. return 0;
  1313. }
  1314. /*
  1315. * ssl_version_supported - Check that the specified `version` is supported by
  1316. * `SSL *` instance
  1317. *
  1318. * @s: The SSL handle for the candidate method
  1319. * @version: Protocol version to test against
  1320. *
  1321. * Returns 1 when supported, otherwise 0
  1322. */
  1323. int ssl_version_supported(const SSL *s, int version)
  1324. {
  1325. const version_info *vent;
  1326. const version_info *table;
  1327. switch (s->method->version) {
  1328. default:
  1329. /* Version should match method version for non-ANY method */
  1330. return version_cmp(s, version, s->version) == 0;
  1331. case TLS_ANY_VERSION:
  1332. table = tls_version_table;
  1333. break;
  1334. case DTLS_ANY_VERSION:
  1335. table = dtls_version_table;
  1336. break;
  1337. }
  1338. for (vent = table;
  1339. vent->version != 0 && version_cmp(s, version, vent->version) <= 0;
  1340. ++vent) {
  1341. if (vent->cmeth != NULL &&
  1342. version_cmp(s, version, vent->version) == 0 &&
  1343. ssl_method_error(s, vent->cmeth()) == 0) {
  1344. return 1;
  1345. }
  1346. }
  1347. return 0;
  1348. }
  1349. /*
  1350. * ssl_check_version_downgrade - In response to RFC7507 SCSV version
  1351. * fallback indication from a client check whether we're using the highest
  1352. * supported protocol version.
  1353. *
  1354. * @s server SSL handle.
  1355. *
  1356. * Returns 1 when using the highest enabled version, 0 otherwise.
  1357. */
  1358. int ssl_check_version_downgrade(SSL *s)
  1359. {
  1360. const version_info *vent;
  1361. const version_info *table;
  1362. /*
  1363. * Check that the current protocol is the highest enabled version
  1364. * (according to s->ctx->method, as version negotiation may have changed
  1365. * s->method).
  1366. */
  1367. if (s->version == s->ctx->method->version)
  1368. return 1;
  1369. /*
  1370. * Apparently we're using a version-flexible SSL_METHOD (not at its
  1371. * highest protocol version).
  1372. */
  1373. if (s->ctx->method->version == TLS_method()->version)
  1374. table = tls_version_table;
  1375. else if (s->ctx->method->version == DTLS_method()->version)
  1376. table = dtls_version_table;
  1377. else {
  1378. /* Unexpected state; fail closed. */
  1379. return 0;
  1380. }
  1381. for (vent = table; vent->version != 0; ++vent) {
  1382. if (vent->smeth != NULL && ssl_method_error(s, vent->smeth()) == 0)
  1383. return s->version == vent->version;
  1384. }
  1385. return 0;
  1386. }
  1387. /*
  1388. * ssl_set_version_bound - set an upper or lower bound on the supported (D)TLS
  1389. * protocols, provided the initial (D)TLS method is version-flexible. This
  1390. * function sanity-checks the proposed value and makes sure the method is
  1391. * version-flexible, then sets the limit if all is well.
  1392. *
  1393. * @method_version: The version of the current SSL_METHOD.
  1394. * @version: the intended limit.
  1395. * @bound: pointer to limit to be updated.
  1396. *
  1397. * Returns 1 on success, 0 on failure.
  1398. */
  1399. int ssl_set_version_bound(int method_version, int version, int *bound)
  1400. {
  1401. if (version == 0) {
  1402. *bound = version;
  1403. return 1;
  1404. }
  1405. /*-
  1406. * Restrict TLS methods to TLS protocol versions.
  1407. * Restrict DTLS methods to DTLS protocol versions.
  1408. * Note, DTLS version numbers are decreasing, use comparison macros.
  1409. *
  1410. * Note that for both lower-bounds we use explicit versions, not
  1411. * (D)TLS_MIN_VERSION. This is because we don't want to break user
  1412. * configurations. If the MIN (supported) version ever rises, the user's
  1413. * "floor" remains valid even if no longer available. We don't expect the
  1414. * MAX ceiling to ever get lower, so making that variable makes sense.
  1415. */
  1416. switch (method_version) {
  1417. default:
  1418. /*
  1419. * XXX For fixed version methods, should we always fail and not set any
  1420. * bounds, always succeed and not set any bounds, or set the bounds and
  1421. * arrange to fail later if they are not met? At present fixed-version
  1422. * methods are not subject to controls that disable individual protocol
  1423. * versions.
  1424. */
  1425. return 0;
  1426. case TLS_ANY_VERSION:
  1427. if (version < SSL3_VERSION || version > TLS_MAX_VERSION)
  1428. return 0;
  1429. break;
  1430. case DTLS_ANY_VERSION:
  1431. if (DTLS_VERSION_GT(version, DTLS_MAX_VERSION) ||
  1432. DTLS_VERSION_LT(version, DTLS1_BAD_VER))
  1433. return 0;
  1434. break;
  1435. }
  1436. *bound = version;
  1437. return 1;
  1438. }
  1439. static void check_for_downgrade(SSL *s, int vers, DOWNGRADE *dgrd)
  1440. {
  1441. if (vers == TLS1_2_VERSION
  1442. && ssl_version_supported(s, TLS1_3_VERSION)) {
  1443. *dgrd = DOWNGRADE_TO_1_2;
  1444. } else if (!SSL_IS_DTLS(s) && vers < TLS1_2_VERSION
  1445. && (ssl_version_supported(s, TLS1_2_VERSION)
  1446. || ssl_version_supported(s, TLS1_3_VERSION))) {
  1447. *dgrd = DOWNGRADE_TO_1_1;
  1448. } else {
  1449. *dgrd = DOWNGRADE_NONE;
  1450. }
  1451. }
  1452. /*
  1453. * ssl_choose_server_version - Choose server (D)TLS version. Called when the
  1454. * client HELLO is received to select the final server protocol version and
  1455. * the version specific method.
  1456. *
  1457. * @s: server SSL handle.
  1458. *
  1459. * Returns 0 on success or an SSL error reason number on failure.
  1460. */
  1461. int ssl_choose_server_version(SSL *s, CLIENTHELLO_MSG *hello, DOWNGRADE *dgrd)
  1462. {
  1463. /*-
  1464. * With version-flexible methods we have an initial state with:
  1465. *
  1466. * s->method->version == (D)TLS_ANY_VERSION,
  1467. * s->version == (D)TLS_MAX_VERSION.
  1468. *
  1469. * So we detect version-flexible methods via the method version, not the
  1470. * handle version.
  1471. */
  1472. int server_version = s->method->version;
  1473. int client_version = hello->legacy_version;
  1474. const version_info *vent;
  1475. const version_info *table;
  1476. int disabled = 0;
  1477. RAW_EXTENSION *suppversions;
  1478. s->client_version = client_version;
  1479. switch (server_version) {
  1480. default:
  1481. if (!SSL_IS_TLS13(s)) {
  1482. if (version_cmp(s, client_version, s->version) < 0)
  1483. return SSL_R_WRONG_SSL_VERSION;
  1484. *dgrd = DOWNGRADE_NONE;
  1485. /*
  1486. * If this SSL handle is not from a version flexible method we don't
  1487. * (and never did) check min/max FIPS or Suite B constraints. Hope
  1488. * that's OK. It is up to the caller to not choose fixed protocol
  1489. * versions they don't want. If not, then easy to fix, just return
  1490. * ssl_method_error(s, s->method)
  1491. */
  1492. return 0;
  1493. }
  1494. /*
  1495. * Fall through if we are TLSv1.3 already (this means we must be after
  1496. * a HelloRetryRequest
  1497. */
  1498. /* fall thru */
  1499. case TLS_ANY_VERSION:
  1500. table = tls_version_table;
  1501. break;
  1502. case DTLS_ANY_VERSION:
  1503. table = dtls_version_table;
  1504. break;
  1505. }
  1506. suppversions = &hello->pre_proc_exts[TLSEXT_IDX_supported_versions];
  1507. /* If we did an HRR then supported versions is mandatory */
  1508. if (!suppversions->present && s->hello_retry_request != SSL_HRR_NONE)
  1509. return SSL_R_UNSUPPORTED_PROTOCOL;
  1510. if (suppversions->present && !SSL_IS_DTLS(s)) {
  1511. unsigned int candidate_vers = 0;
  1512. unsigned int best_vers = 0;
  1513. const SSL_METHOD *best_method = NULL;
  1514. PACKET versionslist;
  1515. /* TODO(TLS1.3): Remove this before release */
  1516. unsigned int orig_candidate = 0;
  1517. suppversions->parsed = 1;
  1518. if (!PACKET_as_length_prefixed_1(&suppversions->data, &versionslist)) {
  1519. /* Trailing or invalid data? */
  1520. return SSL_R_LENGTH_MISMATCH;
  1521. }
  1522. while (PACKET_get_net_2(&versionslist, &candidate_vers)) {
  1523. /* TODO(TLS1.3): Remove this before release */
  1524. if (candidate_vers == TLS1_3_VERSION_DRAFT
  1525. || candidate_vers == TLS1_3_VERSION_DRAFT_27
  1526. || candidate_vers == TLS1_3_VERSION_DRAFT_26) {
  1527. if (best_vers == TLS1_3_VERSION
  1528. && orig_candidate > candidate_vers)
  1529. continue;
  1530. orig_candidate = candidate_vers;
  1531. candidate_vers = TLS1_3_VERSION;
  1532. } else if (candidate_vers == TLS1_3_VERSION) {
  1533. /* Don't actually accept real TLSv1.3 */
  1534. continue;
  1535. }
  1536. /*
  1537. * TODO(TLS1.3): There is some discussion on the TLS list about
  1538. * whether to ignore versions <TLS1.2 in supported_versions. At the
  1539. * moment we honour them if present. To be reviewed later
  1540. */
  1541. if (version_cmp(s, candidate_vers, best_vers) <= 0)
  1542. continue;
  1543. for (vent = table;
  1544. vent->version != 0 && vent->version != (int)candidate_vers;
  1545. ++vent)
  1546. continue;
  1547. if (vent->version != 0 && vent->smeth != NULL) {
  1548. const SSL_METHOD *method;
  1549. method = vent->smeth();
  1550. if (ssl_method_error(s, method) == 0) {
  1551. best_vers = candidate_vers;
  1552. best_method = method;
  1553. }
  1554. }
  1555. }
  1556. if (PACKET_remaining(&versionslist) != 0) {
  1557. /* Trailing data? */
  1558. return SSL_R_LENGTH_MISMATCH;
  1559. }
  1560. if (best_vers > 0) {
  1561. if (s->hello_retry_request != SSL_HRR_NONE) {
  1562. /*
  1563. * This is after a HelloRetryRequest so we better check that we
  1564. * negotiated TLSv1.3
  1565. */
  1566. if (best_vers != TLS1_3_VERSION)
  1567. return SSL_R_UNSUPPORTED_PROTOCOL;
  1568. return 0;
  1569. }
  1570. check_for_downgrade(s, best_vers, dgrd);
  1571. s->version = best_vers;
  1572. /* TODO(TLS1.3): Remove this before release */
  1573. if (best_vers == TLS1_3_VERSION)
  1574. s->version_draft = orig_candidate;
  1575. s->method = best_method;
  1576. return 0;
  1577. }
  1578. return SSL_R_UNSUPPORTED_PROTOCOL;
  1579. }
  1580. /*
  1581. * If the supported versions extension isn't present, then the highest
  1582. * version we can negotiate is TLSv1.2
  1583. */
  1584. if (version_cmp(s, client_version, TLS1_3_VERSION) >= 0)
  1585. client_version = TLS1_2_VERSION;
  1586. /*
  1587. * No supported versions extension, so we just use the version supplied in
  1588. * the ClientHello.
  1589. */
  1590. for (vent = table; vent->version != 0; ++vent) {
  1591. const SSL_METHOD *method;
  1592. if (vent->smeth == NULL ||
  1593. version_cmp(s, client_version, vent->version) < 0)
  1594. continue;
  1595. method = vent->smeth();
  1596. if (ssl_method_error(s, method) == 0) {
  1597. check_for_downgrade(s, vent->version, dgrd);
  1598. s->version = vent->version;
  1599. s->method = method;
  1600. return 0;
  1601. }
  1602. disabled = 1;
  1603. }
  1604. return disabled ? SSL_R_UNSUPPORTED_PROTOCOL : SSL_R_VERSION_TOO_LOW;
  1605. }
  1606. /*
  1607. * ssl_choose_client_version - Choose client (D)TLS version. Called when the
  1608. * server HELLO is received to select the final client protocol version and
  1609. * the version specific method.
  1610. *
  1611. * @s: client SSL handle.
  1612. * @version: The proposed version from the server's HELLO.
  1613. * @extensions: The extensions received
  1614. *
  1615. * Returns 1 on success or 0 on error.
  1616. */
  1617. int ssl_choose_client_version(SSL *s, int version, RAW_EXTENSION *extensions)
  1618. {
  1619. const version_info *vent;
  1620. const version_info *table;
  1621. int highver = 0;
  1622. int origv;
  1623. origv = s->version;
  1624. s->version = version;
  1625. /* This will overwrite s->version if the extension is present */
  1626. if (!tls_parse_extension(s, TLSEXT_IDX_supported_versions,
  1627. SSL_EXT_TLS1_2_SERVER_HELLO
  1628. | SSL_EXT_TLS1_3_SERVER_HELLO, extensions,
  1629. NULL, 0)) {
  1630. s->version = origv;
  1631. return 0;
  1632. }
  1633. if (s->hello_retry_request != SSL_HRR_NONE
  1634. && s->version != TLS1_3_VERSION) {
  1635. s->version = origv;
  1636. SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_F_SSL_CHOOSE_CLIENT_VERSION,
  1637. SSL_R_WRONG_SSL_VERSION);
  1638. return 0;
  1639. }
  1640. switch (s->method->version) {
  1641. default:
  1642. if (s->version != s->method->version) {
  1643. s->version = origv;
  1644. SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
  1645. SSL_F_SSL_CHOOSE_CLIENT_VERSION,
  1646. SSL_R_WRONG_SSL_VERSION);
  1647. return 0;
  1648. }
  1649. /*
  1650. * If this SSL handle is not from a version flexible method we don't
  1651. * (and never did) check min/max, FIPS or Suite B constraints. Hope
  1652. * that's OK. It is up to the caller to not choose fixed protocol
  1653. * versions they don't want. If not, then easy to fix, just return
  1654. * ssl_method_error(s, s->method)
  1655. */
  1656. return 1;
  1657. case TLS_ANY_VERSION:
  1658. table = tls_version_table;
  1659. break;
  1660. case DTLS_ANY_VERSION:
  1661. table = dtls_version_table;
  1662. break;
  1663. }
  1664. for (vent = table; vent->version != 0; ++vent) {
  1665. const SSL_METHOD *method;
  1666. int err;
  1667. if (vent->cmeth == NULL)
  1668. continue;
  1669. if (highver != 0 && s->version != vent->version)
  1670. continue;
  1671. method = vent->cmeth();
  1672. err = ssl_method_error(s, method);
  1673. if (err != 0) {
  1674. if (s->version == vent->version) {
  1675. s->version = origv;
  1676. SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
  1677. SSL_F_SSL_CHOOSE_CLIENT_VERSION, err);
  1678. return 0;
  1679. }
  1680. continue;
  1681. }
  1682. if (highver == 0)
  1683. highver = vent->version;
  1684. if (s->version != vent->version)
  1685. continue;
  1686. #ifndef OPENSSL_NO_TLS13DOWNGRADE
  1687. /* Check for downgrades */
  1688. if (s->version == TLS1_2_VERSION && highver > s->version) {
  1689. if (memcmp(tls12downgrade,
  1690. s->s3->server_random + SSL3_RANDOM_SIZE
  1691. - sizeof(tls12downgrade),
  1692. sizeof(tls12downgrade)) == 0) {
  1693. s->version = origv;
  1694. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1695. SSL_F_SSL_CHOOSE_CLIENT_VERSION,
  1696. SSL_R_INAPPROPRIATE_FALLBACK);
  1697. return 0;
  1698. }
  1699. } else if (!SSL_IS_DTLS(s)
  1700. && s->version < TLS1_2_VERSION
  1701. && highver > s->version) {
  1702. if (memcmp(tls11downgrade,
  1703. s->s3->server_random + SSL3_RANDOM_SIZE
  1704. - sizeof(tls11downgrade),
  1705. sizeof(tls11downgrade)) == 0) {
  1706. s->version = origv;
  1707. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
  1708. SSL_F_SSL_CHOOSE_CLIENT_VERSION,
  1709. SSL_R_INAPPROPRIATE_FALLBACK);
  1710. return 0;
  1711. }
  1712. }
  1713. #endif
  1714. s->method = method;
  1715. return 1;
  1716. }
  1717. s->version = origv;
  1718. SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_F_SSL_CHOOSE_CLIENT_VERSION,
  1719. SSL_R_UNSUPPORTED_PROTOCOL);
  1720. return 0;
  1721. }
  1722. /*
  1723. * ssl_get_min_max_version - get minimum and maximum protocol version
  1724. * @s: The SSL connection
  1725. * @min_version: The minimum supported version
  1726. * @max_version: The maximum supported version
  1727. *
  1728. * Work out what version we should be using for the initial ClientHello if the
  1729. * version is initially (D)TLS_ANY_VERSION. We apply any explicit SSL_OP_NO_xxx
  1730. * options, the MinProtocol and MaxProtocol configuration commands, any Suite B
  1731. * constraints and any floor imposed by the security level here,
  1732. * so we don't advertise the wrong protocol version to only reject the outcome later.
  1733. *
  1734. * Computing the right floor matters. If, e.g., TLS 1.0 and 1.2 are enabled,
  1735. * TLS 1.1 is disabled, but the security level, Suite-B and/or MinProtocol
  1736. * only allow TLS 1.2, we want to advertise TLS1.2, *not* TLS1.
  1737. *
  1738. * Returns 0 on success or an SSL error reason number on failure. On failure
  1739. * min_version and max_version will also be set to 0.
  1740. */
  1741. int ssl_get_min_max_version(const SSL *s, int *min_version, int *max_version)
  1742. {
  1743. int version;
  1744. int hole;
  1745. const SSL_METHOD *single = NULL;
  1746. const SSL_METHOD *method;
  1747. const version_info *table;
  1748. const version_info *vent;
  1749. switch (s->method->version) {
  1750. default:
  1751. /*
  1752. * If this SSL handle is not from a version flexible method we don't
  1753. * (and never did) check min/max FIPS or Suite B constraints. Hope
  1754. * that's OK. It is up to the caller to not choose fixed protocol
  1755. * versions they don't want. If not, then easy to fix, just return
  1756. * ssl_method_error(s, s->method)
  1757. */
  1758. *min_version = *max_version = s->version;
  1759. return 0;
  1760. case TLS_ANY_VERSION:
  1761. table = tls_version_table;
  1762. break;
  1763. case DTLS_ANY_VERSION:
  1764. table = dtls_version_table;
  1765. break;
  1766. }
  1767. /*
  1768. * SSL_OP_NO_X disables all protocols above X *if* there are some protocols
  1769. * below X enabled. This is required in order to maintain the "version
  1770. * capability" vector contiguous. Any versions with a NULL client method
  1771. * (protocol version client is disabled at compile-time) is also a "hole".
  1772. *
  1773. * Our initial state is hole == 1, version == 0. That is, versions above
  1774. * the first version in the method table are disabled (a "hole" above
  1775. * the valid protocol entries) and we don't have a selected version yet.
  1776. *
  1777. * Whenever "hole == 1", and we hit an enabled method, its version becomes
  1778. * the selected version, and the method becomes a candidate "single"
  1779. * method. We're no longer in a hole, so "hole" becomes 0.
  1780. *
  1781. * If "hole == 0" and we hit an enabled method, then "single" is cleared,
  1782. * as we support a contiguous range of at least two methods. If we hit
  1783. * a disabled method, then hole becomes true again, but nothing else
  1784. * changes yet, because all the remaining methods may be disabled too.
  1785. * If we again hit an enabled method after the new hole, it becomes
  1786. * selected, as we start from scratch.
  1787. */
  1788. *min_version = version = 0;
  1789. hole = 1;
  1790. for (vent = table; vent->version != 0; ++vent) {
  1791. /*
  1792. * A table entry with a NULL client method is still a hole in the
  1793. * "version capability" vector.
  1794. */
  1795. if (vent->cmeth == NULL) {
  1796. hole = 1;
  1797. continue;
  1798. }
  1799. method = vent->cmeth();
  1800. if (ssl_method_error(s, method) != 0) {
  1801. hole = 1;
  1802. } else if (!hole) {
  1803. single = NULL;
  1804. *min_version = method->version;
  1805. } else {
  1806. version = (single = method)->version;
  1807. *min_version = version;
  1808. hole = 0;
  1809. }
  1810. }
  1811. *max_version = version;
  1812. /* Fail if everything is disabled */
  1813. if (version == 0)
  1814. return SSL_R_NO_PROTOCOLS_AVAILABLE;
  1815. return 0;
  1816. }
  1817. /*
  1818. * ssl_set_client_hello_version - Work out what version we should be using for
  1819. * the initial ClientHello.legacy_version field.
  1820. *
  1821. * @s: client SSL handle.
  1822. *
  1823. * Returns 0 on success or an SSL error reason number on failure.
  1824. */
  1825. int ssl_set_client_hello_version(SSL *s)
  1826. {
  1827. int ver_min, ver_max, ret;
  1828. /*
  1829. * In a renegotiation we always send the same client_version that we sent
  1830. * last time, regardless of which version we eventually negotiated.
  1831. */
  1832. if (!SSL_IS_FIRST_HANDSHAKE(s))
  1833. return 0;
  1834. ret = ssl_get_min_max_version(s, &ver_min, &ver_max);
  1835. if (ret != 0)
  1836. return ret;
  1837. s->version = ver_max;
  1838. /* TLS1.3 always uses TLS1.2 in the legacy_version field */
  1839. if (!SSL_IS_DTLS(s) && ver_max > TLS1_2_VERSION)
  1840. ver_max = TLS1_2_VERSION;
  1841. s->client_version = ver_max;
  1842. return 0;
  1843. }
  1844. /*
  1845. * Checks a list of |groups| to determine if the |group_id| is in it. If it is
  1846. * and |checkallow| is 1 then additionally check if the group is allowed to be
  1847. * used. Returns 1 if the group is in the list (and allowed if |checkallow| is
  1848. * 1) or 0 otherwise.
  1849. */
  1850. #ifndef OPENSSL_NO_EC
  1851. int check_in_list(SSL *s, uint16_t group_id, const uint16_t *groups,
  1852. size_t num_groups, int checkallow)
  1853. {
  1854. size_t i;
  1855. if (groups == NULL || num_groups == 0)
  1856. return 0;
  1857. for (i = 0; i < num_groups; i++) {
  1858. uint16_t group = groups[i];
  1859. if (group_id == group
  1860. && (!checkallow
  1861. || tls_curve_allowed(s, group, SSL_SECOP_CURVE_CHECK))) {
  1862. return 1;
  1863. }
  1864. }
  1865. return 0;
  1866. }
  1867. #endif
  1868. /* Replace ClientHello1 in the transcript hash with a synthetic message */
  1869. int create_synthetic_message_hash(SSL *s, const unsigned char *hashval,
  1870. size_t hashlen, const unsigned char *hrr,
  1871. size_t hrrlen)
  1872. {
  1873. unsigned char hashvaltmp[EVP_MAX_MD_SIZE];
  1874. unsigned char msghdr[SSL3_HM_HEADER_LENGTH];
  1875. memset(msghdr, 0, sizeof(msghdr));
  1876. if (hashval == NULL) {
  1877. hashval = hashvaltmp;
  1878. hashlen = 0;
  1879. /* Get the hash of the initial ClientHello */
  1880. if (!ssl3_digest_cached_records(s, 0)
  1881. || !ssl_handshake_hash(s, hashvaltmp, sizeof(hashvaltmp),
  1882. &hashlen)) {
  1883. /* SSLfatal() already called */
  1884. return 0;
  1885. }
  1886. }
  1887. /* Reinitialise the transcript hash */
  1888. if (!ssl3_init_finished_mac(s)) {
  1889. /* SSLfatal() already called */
  1890. return 0;
  1891. }
  1892. /* Inject the synthetic message_hash message */
  1893. msghdr[0] = SSL3_MT_MESSAGE_HASH;
  1894. msghdr[SSL3_HM_HEADER_LENGTH - 1] = (unsigned char)hashlen;
  1895. if (!ssl3_finish_mac(s, msghdr, SSL3_HM_HEADER_LENGTH)
  1896. || !ssl3_finish_mac(s, hashval, hashlen)) {
  1897. /* SSLfatal() already called */
  1898. return 0;
  1899. }
  1900. /*
  1901. * Now re-inject the HRR and current message if appropriate (we just deleted
  1902. * it when we reinitialised the transcript hash above). Only necessary after
  1903. * receiving a ClientHello2 with a cookie.
  1904. */
  1905. if (hrr != NULL
  1906. && (!ssl3_finish_mac(s, hrr, hrrlen)
  1907. || !ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
  1908. s->s3->tmp.message_size
  1909. + SSL3_HM_HEADER_LENGTH))) {
  1910. /* SSLfatal() already called */
  1911. return 0;
  1912. }
  1913. return 1;
  1914. }
  1915. static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
  1916. {
  1917. return X509_NAME_cmp(*a, *b);
  1918. }
  1919. int parse_ca_names(SSL *s, PACKET *pkt)
  1920. {
  1921. STACK_OF(X509_NAME) *ca_sk = sk_X509_NAME_new(ca_dn_cmp);
  1922. X509_NAME *xn = NULL;
  1923. PACKET cadns;
  1924. if (ca_sk == NULL) {
  1925. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_PARSE_CA_NAMES,
  1926. ERR_R_MALLOC_FAILURE);
  1927. goto err;
  1928. }
  1929. /* get the CA RDNs */
  1930. if (!PACKET_get_length_prefixed_2(pkt, &cadns)) {
  1931. SSLfatal(s, SSL_AD_DECODE_ERROR,SSL_F_PARSE_CA_NAMES,
  1932. SSL_R_LENGTH_MISMATCH);
  1933. goto err;
  1934. }
  1935. while (PACKET_remaining(&cadns)) {
  1936. const unsigned char *namestart, *namebytes;
  1937. unsigned int name_len;
  1938. if (!PACKET_get_net_2(&cadns, &name_len)
  1939. || !PACKET_get_bytes(&cadns, &namebytes, name_len)) {
  1940. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_PARSE_CA_NAMES,
  1941. SSL_R_LENGTH_MISMATCH);
  1942. goto err;
  1943. }
  1944. namestart = namebytes;
  1945. if ((xn = d2i_X509_NAME(NULL, &namebytes, name_len)) == NULL) {
  1946. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_PARSE_CA_NAMES,
  1947. ERR_R_ASN1_LIB);
  1948. goto err;
  1949. }
  1950. if (namebytes != (namestart + name_len)) {
  1951. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_PARSE_CA_NAMES,
  1952. SSL_R_CA_DN_LENGTH_MISMATCH);
  1953. goto err;
  1954. }
  1955. if (!sk_X509_NAME_push(ca_sk, xn)) {
  1956. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_PARSE_CA_NAMES,
  1957. ERR_R_MALLOC_FAILURE);
  1958. goto err;
  1959. }
  1960. xn = NULL;
  1961. }
  1962. sk_X509_NAME_pop_free(s->s3->tmp.peer_ca_names, X509_NAME_free);
  1963. s->s3->tmp.peer_ca_names = ca_sk;
  1964. return 1;
  1965. err:
  1966. sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
  1967. X509_NAME_free(xn);
  1968. return 0;
  1969. }
  1970. int construct_ca_names(SSL *s, WPACKET *pkt)
  1971. {
  1972. const STACK_OF(X509_NAME) *ca_sk = SSL_get0_CA_list(s);
  1973. /* Start sub-packet for client CA list */
  1974. if (!WPACKET_start_sub_packet_u16(pkt)) {
  1975. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_CA_NAMES,
  1976. ERR_R_INTERNAL_ERROR);
  1977. return 0;
  1978. }
  1979. if (ca_sk != NULL) {
  1980. int i;
  1981. for (i = 0; i < sk_X509_NAME_num(ca_sk); i++) {
  1982. unsigned char *namebytes;
  1983. X509_NAME *name = sk_X509_NAME_value(ca_sk, i);
  1984. int namelen;
  1985. if (name == NULL
  1986. || (namelen = i2d_X509_NAME(name, NULL)) < 0
  1987. || !WPACKET_sub_allocate_bytes_u16(pkt, namelen,
  1988. &namebytes)
  1989. || i2d_X509_NAME(name, &namebytes) != namelen) {
  1990. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_CA_NAMES,
  1991. ERR_R_INTERNAL_ERROR);
  1992. return 0;
  1993. }
  1994. }
  1995. }
  1996. if (!WPACKET_close(pkt)) {
  1997. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_CA_NAMES,
  1998. ERR_R_INTERNAL_ERROR);
  1999. return 0;
  2000. }
  2001. return 1;
  2002. }
  2003. /* Create a buffer containing data to be signed for server key exchange */
  2004. size_t construct_key_exchange_tbs(SSL *s, unsigned char **ptbs,
  2005. const void *param, size_t paramlen)
  2006. {
  2007. size_t tbslen = 2 * SSL3_RANDOM_SIZE + paramlen;
  2008. unsigned char *tbs = OPENSSL_malloc(tbslen);
  2009. if (tbs == NULL) {
  2010. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_KEY_EXCHANGE_TBS,
  2011. ERR_R_MALLOC_FAILURE);
  2012. return 0;
  2013. }
  2014. memcpy(tbs, s->s3->client_random, SSL3_RANDOM_SIZE);
  2015. memcpy(tbs + SSL3_RANDOM_SIZE, s->s3->server_random, SSL3_RANDOM_SIZE);
  2016. memcpy(tbs + SSL3_RANDOM_SIZE * 2, param, paramlen);
  2017. *ptbs = tbs;
  2018. return tbslen;
  2019. }
  2020. /*
  2021. * Saves the current handshake digest for Post-Handshake Auth,
  2022. * Done after ClientFinished is processed, done exactly once
  2023. */
  2024. int tls13_save_handshake_digest_for_pha(SSL *s)
  2025. {
  2026. if (s->pha_dgst == NULL) {
  2027. if (!ssl3_digest_cached_records(s, 1))
  2028. /* SSLfatal() already called */
  2029. return 0;
  2030. s->pha_dgst = EVP_MD_CTX_new();
  2031. if (s->pha_dgst == NULL) {
  2032. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2033. SSL_F_TLS13_SAVE_HANDSHAKE_DIGEST_FOR_PHA,
  2034. ERR_R_INTERNAL_ERROR);
  2035. return 0;
  2036. }
  2037. if (!EVP_MD_CTX_copy_ex(s->pha_dgst,
  2038. s->s3->handshake_dgst)) {
  2039. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2040. SSL_F_TLS13_SAVE_HANDSHAKE_DIGEST_FOR_PHA,
  2041. ERR_R_INTERNAL_ERROR);
  2042. return 0;
  2043. }
  2044. }
  2045. return 1;
  2046. }
  2047. /*
  2048. * Restores the Post-Handshake Auth handshake digest
  2049. * Done just before sending/processing the Cert Request
  2050. */
  2051. int tls13_restore_handshake_digest_for_pha(SSL *s)
  2052. {
  2053. if (s->pha_dgst == NULL) {
  2054. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2055. SSL_F_TLS13_RESTORE_HANDSHAKE_DIGEST_FOR_PHA,
  2056. ERR_R_INTERNAL_ERROR);
  2057. return 0;
  2058. }
  2059. if (!EVP_MD_CTX_copy_ex(s->s3->handshake_dgst,
  2060. s->pha_dgst)) {
  2061. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  2062. SSL_F_TLS13_RESTORE_HANDSHAKE_DIGEST_FOR_PHA,
  2063. ERR_R_INTERNAL_ERROR);
  2064. return 0;
  2065. }
  2066. return 1;
  2067. }