statem_lib.c 79 KB

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