2
0

statem_lib.c 67 KB

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