statem_lib.c 80 KB

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