t1_enc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /*
  2. * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2005 Nokia. All rights reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <stdio.h>
  11. #include "ssl_local.h"
  12. #include "record/record_local.h"
  13. #include "internal/ktls.h"
  14. #include "internal/cryptlib.h"
  15. #include <openssl/comp.h>
  16. #include <openssl/evp.h>
  17. #include <openssl/kdf.h>
  18. #include <openssl/rand.h>
  19. #include <openssl/obj_mac.h>
  20. #include <openssl/core_names.h>
  21. #include <openssl/trace.h>
  22. /* seed1 through seed5 are concatenated */
  23. static int tls1_PRF(SSL_CONNECTION *s,
  24. const void *seed1, size_t seed1_len,
  25. const void *seed2, size_t seed2_len,
  26. const void *seed3, size_t seed3_len,
  27. const void *seed4, size_t seed4_len,
  28. const void *seed5, size_t seed5_len,
  29. const unsigned char *sec, size_t slen,
  30. unsigned char *out, size_t olen, int fatal)
  31. {
  32. const EVP_MD *md = ssl_prf_md(s);
  33. EVP_KDF *kdf;
  34. EVP_KDF_CTX *kctx = NULL;
  35. OSSL_PARAM params[8], *p = params;
  36. const char *mdname;
  37. if (md == NULL) {
  38. /* Should never happen */
  39. if (fatal)
  40. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  41. else
  42. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  43. return 0;
  44. }
  45. kdf = EVP_KDF_fetch(SSL_CONNECTION_GET_CTX(s)->libctx,
  46. OSSL_KDF_NAME_TLS1_PRF,
  47. SSL_CONNECTION_GET_CTX(s)->propq);
  48. if (kdf == NULL)
  49. goto err;
  50. kctx = EVP_KDF_CTX_new(kdf);
  51. EVP_KDF_free(kdf);
  52. if (kctx == NULL)
  53. goto err;
  54. mdname = EVP_MD_get0_name(md);
  55. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  56. (char *)mdname, 0);
  57. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SECRET,
  58. (unsigned char *)sec,
  59. (size_t)slen);
  60. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
  61. (void *)seed1, (size_t)seed1_len);
  62. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
  63. (void *)seed2, (size_t)seed2_len);
  64. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
  65. (void *)seed3, (size_t)seed3_len);
  66. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
  67. (void *)seed4, (size_t)seed4_len);
  68. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
  69. (void *)seed5, (size_t)seed5_len);
  70. *p = OSSL_PARAM_construct_end();
  71. if (EVP_KDF_derive(kctx, out, olen, params)) {
  72. EVP_KDF_CTX_free(kctx);
  73. return 1;
  74. }
  75. err:
  76. if (fatal)
  77. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  78. else
  79. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  80. EVP_KDF_CTX_free(kctx);
  81. return 0;
  82. }
  83. static int tls1_generate_key_block(SSL_CONNECTION *s, unsigned char *km,
  84. size_t num)
  85. {
  86. int ret;
  87. /* Calls SSLfatal() as required */
  88. ret = tls1_PRF(s,
  89. TLS_MD_KEY_EXPANSION_CONST,
  90. TLS_MD_KEY_EXPANSION_CONST_SIZE, s->s3.server_random,
  91. SSL3_RANDOM_SIZE, s->s3.client_random, SSL3_RANDOM_SIZE,
  92. NULL, 0, NULL, 0, s->session->master_key,
  93. s->session->master_key_length, km, num, 1);
  94. return ret;
  95. }
  96. int tls_provider_set_tls_params(SSL_CONNECTION *s, EVP_CIPHER_CTX *ctx,
  97. const EVP_CIPHER *ciph,
  98. const EVP_MD *md)
  99. {
  100. /*
  101. * Provided cipher, the TLS padding/MAC removal is performed provider
  102. * side so we need to tell the ctx about our TLS version and mac size
  103. */
  104. OSSL_PARAM params[3], *pprm = params;
  105. size_t macsize = 0;
  106. int imacsize = -1;
  107. if ((EVP_CIPHER_get_flags(ciph) & EVP_CIPH_FLAG_AEAD_CIPHER) == 0
  108. /*
  109. * We look at s->ext.use_etm instead of SSL_READ_ETM() or
  110. * SSL_WRITE_ETM() because this test applies to both reading
  111. * and writing.
  112. */
  113. && !s->ext.use_etm)
  114. imacsize = EVP_MD_get_size(md);
  115. if (imacsize >= 0)
  116. macsize = (size_t)imacsize;
  117. *pprm++ = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS_VERSION,
  118. &s->version);
  119. *pprm++ = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_TLS_MAC_SIZE,
  120. &macsize);
  121. *pprm = OSSL_PARAM_construct_end();
  122. if (!EVP_CIPHER_CTX_set_params(ctx, params)) {
  123. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  124. return 0;
  125. }
  126. return 1;
  127. }
  128. static int tls_iv_length_within_key_block(const EVP_CIPHER *c)
  129. {
  130. /* If GCM/CCM mode only part of IV comes from PRF */
  131. if (EVP_CIPHER_get_mode(c) == EVP_CIPH_GCM_MODE)
  132. return EVP_GCM_TLS_FIXED_IV_LEN;
  133. else if (EVP_CIPHER_get_mode(c) == EVP_CIPH_CCM_MODE)
  134. return EVP_CCM_TLS_FIXED_IV_LEN;
  135. else
  136. return EVP_CIPHER_get_iv_length(c);
  137. }
  138. int tls1_change_cipher_state(SSL_CONNECTION *s, int which)
  139. {
  140. unsigned char *p, *mac_secret;
  141. unsigned char *key, *iv;
  142. const EVP_CIPHER *c;
  143. const SSL_COMP *comp = NULL;
  144. const EVP_MD *m;
  145. int mac_type;
  146. size_t mac_secret_size;
  147. size_t n, i, j, k, cl;
  148. int iivlen;
  149. /*
  150. * Taglen is only relevant for CCM ciphersuites. Other ciphersuites
  151. * ignore this value so we can default it to 0.
  152. */
  153. size_t taglen = 0;
  154. int direction;
  155. c = s->s3.tmp.new_sym_enc;
  156. m = s->s3.tmp.new_hash;
  157. mac_type = s->s3.tmp.new_mac_pkey_type;
  158. #ifndef OPENSSL_NO_COMP
  159. comp = s->s3.tmp.new_compression;
  160. #endif
  161. p = s->s3.tmp.key_block;
  162. i = mac_secret_size = s->s3.tmp.new_mac_secret_size;
  163. cl = EVP_CIPHER_get_key_length(c);
  164. j = cl;
  165. iivlen = tls_iv_length_within_key_block(c);
  166. if (iivlen < 0) {
  167. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  168. goto err;
  169. }
  170. k = iivlen;
  171. if ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
  172. (which == SSL3_CHANGE_CIPHER_SERVER_READ)) {
  173. mac_secret = &(p[0]);
  174. n = i + i;
  175. key = &(p[n]);
  176. n += j + j;
  177. iv = &(p[n]);
  178. n += k + k;
  179. } else {
  180. n = i;
  181. mac_secret = &(p[n]);
  182. n += i + j;
  183. key = &(p[n]);
  184. n += j + k;
  185. iv = &(p[n]);
  186. n += k;
  187. }
  188. if (n > s->s3.tmp.key_block_length) {
  189. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  190. goto err;
  191. }
  192. switch (EVP_CIPHER_get_mode(c)) {
  193. case EVP_CIPH_GCM_MODE:
  194. taglen = EVP_GCM_TLS_TAG_LEN;
  195. break;
  196. case EVP_CIPH_CCM_MODE:
  197. if ((s->s3.tmp.new_cipher->algorithm_enc
  198. & (SSL_AES128CCM8 | SSL_AES256CCM8)) != 0)
  199. taglen = EVP_CCM8_TLS_TAG_LEN;
  200. else
  201. taglen = EVP_CCM_TLS_TAG_LEN;
  202. break;
  203. default:
  204. if (EVP_CIPHER_is_a(c, "CHACHA20-POLY1305")) {
  205. taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;
  206. } else {
  207. /* MAC secret size corresponds to the MAC output size */
  208. taglen = s->s3.tmp.new_mac_secret_size;
  209. }
  210. break;
  211. }
  212. if (which & SSL3_CC_READ) {
  213. if (s->ext.use_etm)
  214. s->s3.flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC_READ;
  215. else
  216. s->s3.flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_READ;
  217. if (s->s3.tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
  218. s->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM;
  219. else
  220. s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM;
  221. if (s->s3.tmp.new_cipher->algorithm2 & TLS1_TLSTREE)
  222. s->mac_flags |= SSL_MAC_FLAG_READ_MAC_TLSTREE;
  223. else
  224. s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_TLSTREE;
  225. direction = OSSL_RECORD_DIRECTION_READ;
  226. } else {
  227. if (s->ext.use_etm)
  228. s->s3.flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE;
  229. else
  230. s->s3.flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE;
  231. if (s->s3.tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
  232. s->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_STREAM;
  233. else
  234. s->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_STREAM;
  235. if (s->s3.tmp.new_cipher->algorithm2 & TLS1_TLSTREE)
  236. s->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_TLSTREE;
  237. else
  238. s->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_TLSTREE;
  239. direction = OSSL_RECORD_DIRECTION_WRITE;
  240. }
  241. if (!ssl_set_new_record_layer(s, s->version, direction,
  242. OSSL_RECORD_PROTECTION_LEVEL_APPLICATION,
  243. key, cl, iv, (size_t)k, mac_secret,
  244. mac_secret_size, c, taglen, mac_type,
  245. m, comp)) {
  246. /* SSLfatal already called */
  247. goto err;
  248. }
  249. OSSL_TRACE_BEGIN(TLS) {
  250. BIO_printf(trc_out, "which = %04X, key:\n", which);
  251. BIO_dump_indent(trc_out, key, EVP_CIPHER_get_key_length(c), 4);
  252. BIO_printf(trc_out, "iv:\n");
  253. BIO_dump_indent(trc_out, iv, k, 4);
  254. } OSSL_TRACE_END(TLS);
  255. return 1;
  256. err:
  257. return 0;
  258. }
  259. int tls1_setup_key_block(SSL_CONNECTION *s)
  260. {
  261. unsigned char *p;
  262. const EVP_CIPHER *c;
  263. const EVP_MD *hash;
  264. SSL_COMP *comp;
  265. int mac_type = NID_undef;
  266. size_t num, mac_secret_size = 0;
  267. int ret = 0;
  268. int ivlen;
  269. if (s->s3.tmp.key_block_length != 0)
  270. return 1;
  271. if (!ssl_cipher_get_evp(SSL_CONNECTION_GET_CTX(s), s->session, &c, &hash,
  272. &mac_type, &mac_secret_size, &comp,
  273. s->ext.use_etm)) {
  274. /* Error is already recorded */
  275. SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
  276. return 0;
  277. }
  278. ssl_evp_cipher_free(s->s3.tmp.new_sym_enc);
  279. s->s3.tmp.new_sym_enc = c;
  280. ssl_evp_md_free(s->s3.tmp.new_hash);
  281. s->s3.tmp.new_hash = hash;
  282. s->s3.tmp.new_mac_pkey_type = mac_type;
  283. s->s3.tmp.new_mac_secret_size = mac_secret_size;
  284. ivlen = tls_iv_length_within_key_block(c);
  285. if (ivlen < 0) {
  286. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  287. return 0;
  288. }
  289. num = mac_secret_size + EVP_CIPHER_get_key_length(c) + ivlen;
  290. num *= 2;
  291. ssl3_cleanup_key_block(s);
  292. if ((p = OPENSSL_malloc(num)) == NULL) {
  293. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  294. goto err;
  295. }
  296. s->s3.tmp.key_block_length = num;
  297. s->s3.tmp.key_block = p;
  298. OSSL_TRACE_BEGIN(TLS) {
  299. BIO_printf(trc_out, "key block length: %zu\n", num);
  300. BIO_printf(trc_out, "client random\n");
  301. BIO_dump_indent(trc_out, s->s3.client_random, SSL3_RANDOM_SIZE, 4);
  302. BIO_printf(trc_out, "server random\n");
  303. BIO_dump_indent(trc_out, s->s3.server_random, SSL3_RANDOM_SIZE, 4);
  304. BIO_printf(trc_out, "master key\n");
  305. BIO_dump_indent(trc_out,
  306. s->session->master_key,
  307. s->session->master_key_length, 4);
  308. } OSSL_TRACE_END(TLS);
  309. if (!tls1_generate_key_block(s, p, num)) {
  310. /* SSLfatal() already called */
  311. goto err;
  312. }
  313. OSSL_TRACE_BEGIN(TLS) {
  314. BIO_printf(trc_out, "key block\n");
  315. BIO_dump_indent(trc_out, p, num, 4);
  316. } OSSL_TRACE_END(TLS);
  317. ret = 1;
  318. err:
  319. return ret;
  320. }
  321. size_t tls1_final_finish_mac(SSL_CONNECTION *s, const char *str,
  322. size_t slen, unsigned char *out)
  323. {
  324. size_t hashlen;
  325. unsigned char hash[EVP_MAX_MD_SIZE];
  326. size_t finished_size = TLS1_FINISH_MAC_LENGTH;
  327. if (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kGOST18)
  328. finished_size = 32;
  329. if (!ssl3_digest_cached_records(s, 0)) {
  330. /* SSLfatal() already called */
  331. return 0;
  332. }
  333. if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
  334. /* SSLfatal() already called */
  335. return 0;
  336. }
  337. if (!tls1_PRF(s, str, slen, hash, hashlen, NULL, 0, NULL, 0, NULL, 0,
  338. s->session->master_key, s->session->master_key_length,
  339. out, finished_size, 1)) {
  340. /* SSLfatal() already called */
  341. return 0;
  342. }
  343. OPENSSL_cleanse(hash, hashlen);
  344. return finished_size;
  345. }
  346. int tls1_generate_master_secret(SSL_CONNECTION *s, unsigned char *out,
  347. unsigned char *p, size_t len,
  348. size_t *secret_size)
  349. {
  350. if (s->session->flags & SSL_SESS_FLAG_EXTMS) {
  351. unsigned char hash[EVP_MAX_MD_SIZE * 2];
  352. size_t hashlen;
  353. /*
  354. * Digest cached records keeping record buffer (if present): this won't
  355. * affect client auth because we're freezing the buffer at the same
  356. * point (after client key exchange and before certificate verify)
  357. */
  358. if (!ssl3_digest_cached_records(s, 1)
  359. || !ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
  360. /* SSLfatal() already called */
  361. return 0;
  362. }
  363. OSSL_TRACE_BEGIN(TLS) {
  364. BIO_printf(trc_out, "Handshake hashes:\n");
  365. BIO_dump(trc_out, (char *)hash, hashlen);
  366. } OSSL_TRACE_END(TLS);
  367. if (!tls1_PRF(s,
  368. TLS_MD_EXTENDED_MASTER_SECRET_CONST,
  369. TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE,
  370. hash, hashlen,
  371. NULL, 0,
  372. NULL, 0,
  373. NULL, 0, p, len, out,
  374. SSL3_MASTER_SECRET_SIZE, 1)) {
  375. /* SSLfatal() already called */
  376. return 0;
  377. }
  378. OPENSSL_cleanse(hash, hashlen);
  379. } else {
  380. if (!tls1_PRF(s,
  381. TLS_MD_MASTER_SECRET_CONST,
  382. TLS_MD_MASTER_SECRET_CONST_SIZE,
  383. s->s3.client_random, SSL3_RANDOM_SIZE,
  384. NULL, 0,
  385. s->s3.server_random, SSL3_RANDOM_SIZE,
  386. NULL, 0, p, len, out,
  387. SSL3_MASTER_SECRET_SIZE, 1)) {
  388. /* SSLfatal() already called */
  389. return 0;
  390. }
  391. }
  392. OSSL_TRACE_BEGIN(TLS) {
  393. BIO_printf(trc_out, "Premaster Secret:\n");
  394. BIO_dump_indent(trc_out, p, len, 4);
  395. BIO_printf(trc_out, "Client Random:\n");
  396. BIO_dump_indent(trc_out, s->s3.client_random, SSL3_RANDOM_SIZE, 4);
  397. BIO_printf(trc_out, "Server Random:\n");
  398. BIO_dump_indent(trc_out, s->s3.server_random, SSL3_RANDOM_SIZE, 4);
  399. BIO_printf(trc_out, "Master Secret:\n");
  400. BIO_dump_indent(trc_out,
  401. s->session->master_key,
  402. SSL3_MASTER_SECRET_SIZE, 4);
  403. } OSSL_TRACE_END(TLS);
  404. *secret_size = SSL3_MASTER_SECRET_SIZE;
  405. return 1;
  406. }
  407. int tls1_export_keying_material(SSL_CONNECTION *s, unsigned char *out,
  408. size_t olen, const char *label, size_t llen,
  409. const unsigned char *context,
  410. size_t contextlen, int use_context)
  411. {
  412. unsigned char *val = NULL;
  413. size_t vallen = 0, currentvalpos;
  414. int rv = 0;
  415. /*
  416. * construct PRF arguments we construct the PRF argument ourself rather
  417. * than passing separate values into the TLS PRF to ensure that the
  418. * concatenation of values does not create a prohibited label.
  419. */
  420. vallen = llen + SSL3_RANDOM_SIZE * 2;
  421. if (use_context) {
  422. vallen += 2 + contextlen;
  423. }
  424. val = OPENSSL_malloc(vallen);
  425. if (val == NULL)
  426. goto ret;
  427. currentvalpos = 0;
  428. memcpy(val + currentvalpos, (unsigned char *)label, llen);
  429. currentvalpos += llen;
  430. memcpy(val + currentvalpos, s->s3.client_random, SSL3_RANDOM_SIZE);
  431. currentvalpos += SSL3_RANDOM_SIZE;
  432. memcpy(val + currentvalpos, s->s3.server_random, SSL3_RANDOM_SIZE);
  433. currentvalpos += SSL3_RANDOM_SIZE;
  434. if (use_context) {
  435. val[currentvalpos] = (contextlen >> 8) & 0xff;
  436. currentvalpos++;
  437. val[currentvalpos] = contextlen & 0xff;
  438. currentvalpos++;
  439. if ((contextlen > 0) || (context != NULL)) {
  440. memcpy(val + currentvalpos, context, contextlen);
  441. }
  442. }
  443. /*
  444. * disallow prohibited labels note that SSL3_RANDOM_SIZE > max(prohibited
  445. * label len) = 15, so size of val > max(prohibited label len) = 15 and
  446. * the comparisons won't have buffer overflow
  447. */
  448. if (memcmp(val, TLS_MD_CLIENT_FINISH_CONST,
  449. TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0)
  450. goto err1;
  451. if (memcmp(val, TLS_MD_SERVER_FINISH_CONST,
  452. TLS_MD_SERVER_FINISH_CONST_SIZE) == 0)
  453. goto err1;
  454. if (memcmp(val, TLS_MD_MASTER_SECRET_CONST,
  455. TLS_MD_MASTER_SECRET_CONST_SIZE) == 0)
  456. goto err1;
  457. if (memcmp(val, TLS_MD_EXTENDED_MASTER_SECRET_CONST,
  458. TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE) == 0)
  459. goto err1;
  460. if (memcmp(val, TLS_MD_KEY_EXPANSION_CONST,
  461. TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0)
  462. goto err1;
  463. rv = tls1_PRF(s,
  464. val, vallen,
  465. NULL, 0,
  466. NULL, 0,
  467. NULL, 0,
  468. NULL, 0,
  469. s->session->master_key, s->session->master_key_length,
  470. out, olen, 0);
  471. goto ret;
  472. err1:
  473. ERR_raise(ERR_LIB_SSL, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
  474. ret:
  475. OPENSSL_clear_free(val, vallen);
  476. return rv;
  477. }
  478. int tls1_alert_code(int code)
  479. {
  480. switch (code) {
  481. case SSL_AD_CLOSE_NOTIFY:
  482. return SSL3_AD_CLOSE_NOTIFY;
  483. case SSL_AD_UNEXPECTED_MESSAGE:
  484. return SSL3_AD_UNEXPECTED_MESSAGE;
  485. case SSL_AD_BAD_RECORD_MAC:
  486. return SSL3_AD_BAD_RECORD_MAC;
  487. case SSL_AD_DECRYPTION_FAILED:
  488. return TLS1_AD_DECRYPTION_FAILED;
  489. case SSL_AD_RECORD_OVERFLOW:
  490. return TLS1_AD_RECORD_OVERFLOW;
  491. case SSL_AD_DECOMPRESSION_FAILURE:
  492. return SSL3_AD_DECOMPRESSION_FAILURE;
  493. case SSL_AD_HANDSHAKE_FAILURE:
  494. return SSL3_AD_HANDSHAKE_FAILURE;
  495. case SSL_AD_NO_CERTIFICATE:
  496. return -1;
  497. case SSL_AD_BAD_CERTIFICATE:
  498. return SSL3_AD_BAD_CERTIFICATE;
  499. case SSL_AD_UNSUPPORTED_CERTIFICATE:
  500. return SSL3_AD_UNSUPPORTED_CERTIFICATE;
  501. case SSL_AD_CERTIFICATE_REVOKED:
  502. return SSL3_AD_CERTIFICATE_REVOKED;
  503. case SSL_AD_CERTIFICATE_EXPIRED:
  504. return SSL3_AD_CERTIFICATE_EXPIRED;
  505. case SSL_AD_CERTIFICATE_UNKNOWN:
  506. return SSL3_AD_CERTIFICATE_UNKNOWN;
  507. case SSL_AD_ILLEGAL_PARAMETER:
  508. return SSL3_AD_ILLEGAL_PARAMETER;
  509. case SSL_AD_UNKNOWN_CA:
  510. return TLS1_AD_UNKNOWN_CA;
  511. case SSL_AD_ACCESS_DENIED:
  512. return TLS1_AD_ACCESS_DENIED;
  513. case SSL_AD_DECODE_ERROR:
  514. return TLS1_AD_DECODE_ERROR;
  515. case SSL_AD_DECRYPT_ERROR:
  516. return TLS1_AD_DECRYPT_ERROR;
  517. case SSL_AD_EXPORT_RESTRICTION:
  518. return TLS1_AD_EXPORT_RESTRICTION;
  519. case SSL_AD_PROTOCOL_VERSION:
  520. return TLS1_AD_PROTOCOL_VERSION;
  521. case SSL_AD_INSUFFICIENT_SECURITY:
  522. return TLS1_AD_INSUFFICIENT_SECURITY;
  523. case SSL_AD_INTERNAL_ERROR:
  524. return TLS1_AD_INTERNAL_ERROR;
  525. case SSL_AD_USER_CANCELLED:
  526. return TLS1_AD_USER_CANCELLED;
  527. case SSL_AD_NO_RENEGOTIATION:
  528. return TLS1_AD_NO_RENEGOTIATION;
  529. case SSL_AD_UNSUPPORTED_EXTENSION:
  530. return TLS1_AD_UNSUPPORTED_EXTENSION;
  531. case SSL_AD_CERTIFICATE_UNOBTAINABLE:
  532. return TLS1_AD_CERTIFICATE_UNOBTAINABLE;
  533. case SSL_AD_UNRECOGNIZED_NAME:
  534. return TLS1_AD_UNRECOGNIZED_NAME;
  535. case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
  536. return TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
  537. case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
  538. return TLS1_AD_BAD_CERTIFICATE_HASH_VALUE;
  539. case SSL_AD_UNKNOWN_PSK_IDENTITY:
  540. return TLS1_AD_UNKNOWN_PSK_IDENTITY;
  541. case SSL_AD_INAPPROPRIATE_FALLBACK:
  542. return TLS1_AD_INAPPROPRIATE_FALLBACK;
  543. case SSL_AD_NO_APPLICATION_PROTOCOL:
  544. return TLS1_AD_NO_APPLICATION_PROTOCOL;
  545. case SSL_AD_CERTIFICATE_REQUIRED:
  546. return SSL_AD_HANDSHAKE_FAILURE;
  547. case TLS13_AD_MISSING_EXTENSION:
  548. return SSL_AD_HANDSHAKE_FAILURE;
  549. default:
  550. return -1;
  551. }
  552. }