statem_lib.c 80 KB

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