t1_enc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. * Copyright 1995-2024 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. static int tls_iv_length_within_key_block(const EVP_CIPHER *c)
  97. {
  98. /* If GCM/CCM mode only part of IV comes from PRF */
  99. if (EVP_CIPHER_get_mode(c) == EVP_CIPH_GCM_MODE)
  100. return EVP_GCM_TLS_FIXED_IV_LEN;
  101. else if (EVP_CIPHER_get_mode(c) == EVP_CIPH_CCM_MODE)
  102. return EVP_CCM_TLS_FIXED_IV_LEN;
  103. else
  104. return EVP_CIPHER_get_iv_length(c);
  105. }
  106. int tls1_change_cipher_state(SSL_CONNECTION *s, int which)
  107. {
  108. unsigned char *p, *mac_secret;
  109. unsigned char *key, *iv;
  110. const EVP_CIPHER *c;
  111. const SSL_COMP *comp = NULL;
  112. const EVP_MD *m;
  113. int mac_type;
  114. size_t mac_secret_size;
  115. size_t n, i, j, k, cl;
  116. int iivlen;
  117. /*
  118. * Taglen is only relevant for CCM ciphersuites. Other ciphersuites
  119. * ignore this value so we can default it to 0.
  120. */
  121. size_t taglen = 0;
  122. int direction;
  123. c = s->s3.tmp.new_sym_enc;
  124. m = s->s3.tmp.new_hash;
  125. mac_type = s->s3.tmp.new_mac_pkey_type;
  126. #ifndef OPENSSL_NO_COMP
  127. comp = s->s3.tmp.new_compression;
  128. #endif
  129. p = s->s3.tmp.key_block;
  130. i = mac_secret_size = s->s3.tmp.new_mac_secret_size;
  131. cl = EVP_CIPHER_get_key_length(c);
  132. j = cl;
  133. iivlen = tls_iv_length_within_key_block(c);
  134. if (iivlen < 0) {
  135. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  136. goto err;
  137. }
  138. k = iivlen;
  139. if ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
  140. (which == SSL3_CHANGE_CIPHER_SERVER_READ)) {
  141. mac_secret = &(p[0]);
  142. n = i + i;
  143. key = &(p[n]);
  144. n += j + j;
  145. iv = &(p[n]);
  146. n += k + k;
  147. } else {
  148. n = i;
  149. mac_secret = &(p[n]);
  150. n += i + j;
  151. key = &(p[n]);
  152. n += j + k;
  153. iv = &(p[n]);
  154. n += k;
  155. }
  156. if (n > s->s3.tmp.key_block_length) {
  157. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  158. goto err;
  159. }
  160. switch (EVP_CIPHER_get_mode(c)) {
  161. case EVP_CIPH_GCM_MODE:
  162. taglen = EVP_GCM_TLS_TAG_LEN;
  163. break;
  164. case EVP_CIPH_CCM_MODE:
  165. if ((s->s3.tmp.new_cipher->algorithm_enc
  166. & (SSL_AES128CCM8 | SSL_AES256CCM8)) != 0)
  167. taglen = EVP_CCM8_TLS_TAG_LEN;
  168. else
  169. taglen = EVP_CCM_TLS_TAG_LEN;
  170. break;
  171. default:
  172. if (EVP_CIPHER_is_a(c, "CHACHA20-POLY1305")) {
  173. taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;
  174. } else {
  175. /* MAC secret size corresponds to the MAC output size */
  176. taglen = s->s3.tmp.new_mac_secret_size;
  177. }
  178. break;
  179. }
  180. if (which & SSL3_CC_READ) {
  181. if (s->ext.use_etm)
  182. s->s3.flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC_READ;
  183. else
  184. s->s3.flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_READ;
  185. if (s->s3.tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
  186. s->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM;
  187. else
  188. s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM;
  189. if (s->s3.tmp.new_cipher->algorithm2 & TLS1_TLSTREE)
  190. s->mac_flags |= SSL_MAC_FLAG_READ_MAC_TLSTREE;
  191. else
  192. s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_TLSTREE;
  193. direction = OSSL_RECORD_DIRECTION_READ;
  194. } else {
  195. if (s->ext.use_etm)
  196. s->s3.flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE;
  197. else
  198. s->s3.flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE;
  199. if (s->s3.tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
  200. s->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_STREAM;
  201. else
  202. s->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_STREAM;
  203. if (s->s3.tmp.new_cipher->algorithm2 & TLS1_TLSTREE)
  204. s->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_TLSTREE;
  205. else
  206. s->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_TLSTREE;
  207. direction = OSSL_RECORD_DIRECTION_WRITE;
  208. }
  209. if (SSL_CONNECTION_IS_DTLS(s))
  210. dtls1_increment_epoch(s, which);
  211. if (!ssl_set_new_record_layer(s, s->version, direction,
  212. OSSL_RECORD_PROTECTION_LEVEL_APPLICATION,
  213. NULL, 0, key, cl, iv, (size_t)k, mac_secret,
  214. mac_secret_size, c, taglen, mac_type,
  215. m, comp, NULL)) {
  216. /* SSLfatal already called */
  217. goto err;
  218. }
  219. OSSL_TRACE_BEGIN(TLS) {
  220. BIO_printf(trc_out, "which = %04X, key:\n", which);
  221. BIO_dump_indent(trc_out, key, EVP_CIPHER_get_key_length(c), 4);
  222. BIO_printf(trc_out, "iv:\n");
  223. BIO_dump_indent(trc_out, iv, k, 4);
  224. } OSSL_TRACE_END(TLS);
  225. return 1;
  226. err:
  227. return 0;
  228. }
  229. int tls1_setup_key_block(SSL_CONNECTION *s)
  230. {
  231. unsigned char *p;
  232. const EVP_CIPHER *c;
  233. const EVP_MD *hash;
  234. SSL_COMP *comp;
  235. int mac_type = NID_undef;
  236. size_t num, mac_secret_size = 0;
  237. int ret = 0;
  238. int ivlen;
  239. if (s->s3.tmp.key_block_length != 0)
  240. return 1;
  241. if (!ssl_cipher_get_evp(SSL_CONNECTION_GET_CTX(s), s->session, &c, &hash,
  242. &mac_type, &mac_secret_size, &comp,
  243. s->ext.use_etm)) {
  244. /* Error is already recorded */
  245. SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
  246. return 0;
  247. }
  248. ssl_evp_cipher_free(s->s3.tmp.new_sym_enc);
  249. s->s3.tmp.new_sym_enc = c;
  250. ssl_evp_md_free(s->s3.tmp.new_hash);
  251. s->s3.tmp.new_hash = hash;
  252. s->s3.tmp.new_mac_pkey_type = mac_type;
  253. s->s3.tmp.new_mac_secret_size = mac_secret_size;
  254. ivlen = tls_iv_length_within_key_block(c);
  255. if (ivlen < 0) {
  256. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  257. return 0;
  258. }
  259. num = mac_secret_size + EVP_CIPHER_get_key_length(c) + ivlen;
  260. num *= 2;
  261. ssl3_cleanup_key_block(s);
  262. if ((p = OPENSSL_malloc(num)) == NULL) {
  263. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  264. goto err;
  265. }
  266. s->s3.tmp.key_block_length = num;
  267. s->s3.tmp.key_block = p;
  268. OSSL_TRACE_BEGIN(TLS) {
  269. BIO_printf(trc_out, "key block length: %zu\n", num);
  270. BIO_printf(trc_out, "client random\n");
  271. BIO_dump_indent(trc_out, s->s3.client_random, SSL3_RANDOM_SIZE, 4);
  272. BIO_printf(trc_out, "server random\n");
  273. BIO_dump_indent(trc_out, s->s3.server_random, SSL3_RANDOM_SIZE, 4);
  274. BIO_printf(trc_out, "master key\n");
  275. BIO_dump_indent(trc_out,
  276. s->session->master_key,
  277. s->session->master_key_length, 4);
  278. } OSSL_TRACE_END(TLS);
  279. if (!tls1_generate_key_block(s, p, num)) {
  280. /* SSLfatal() already called */
  281. goto err;
  282. }
  283. OSSL_TRACE_BEGIN(TLS) {
  284. BIO_printf(trc_out, "key block\n");
  285. BIO_dump_indent(trc_out, p, num, 4);
  286. } OSSL_TRACE_END(TLS);
  287. ret = 1;
  288. err:
  289. return ret;
  290. }
  291. size_t tls1_final_finish_mac(SSL_CONNECTION *s, const char *str,
  292. size_t slen, unsigned char *out)
  293. {
  294. size_t hashlen;
  295. unsigned char hash[EVP_MAX_MD_SIZE];
  296. size_t finished_size = TLS1_FINISH_MAC_LENGTH;
  297. if (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kGOST18)
  298. finished_size = 32;
  299. if (!ssl3_digest_cached_records(s, 0)) {
  300. /* SSLfatal() already called */
  301. return 0;
  302. }
  303. if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
  304. /* SSLfatal() already called */
  305. return 0;
  306. }
  307. if (!tls1_PRF(s, str, slen, hash, hashlen, NULL, 0, NULL, 0, NULL, 0,
  308. s->session->master_key, s->session->master_key_length,
  309. out, finished_size, 1)) {
  310. /* SSLfatal() already called */
  311. return 0;
  312. }
  313. OPENSSL_cleanse(hash, hashlen);
  314. return finished_size;
  315. }
  316. int tls1_generate_master_secret(SSL_CONNECTION *s, unsigned char *out,
  317. unsigned char *p, size_t len,
  318. size_t *secret_size)
  319. {
  320. if (s->session->flags & SSL_SESS_FLAG_EXTMS) {
  321. unsigned char hash[EVP_MAX_MD_SIZE * 2];
  322. size_t hashlen;
  323. /*
  324. * Digest cached records keeping record buffer (if present): this won't
  325. * affect client auth because we're freezing the buffer at the same
  326. * point (after client key exchange and before certificate verify)
  327. */
  328. if (!ssl3_digest_cached_records(s, 1)
  329. || !ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
  330. /* SSLfatal() already called */
  331. return 0;
  332. }
  333. OSSL_TRACE_BEGIN(TLS) {
  334. BIO_printf(trc_out, "Handshake hashes:\n");
  335. BIO_dump(trc_out, (char *)hash, hashlen);
  336. } OSSL_TRACE_END(TLS);
  337. if (!tls1_PRF(s,
  338. TLS_MD_EXTENDED_MASTER_SECRET_CONST,
  339. TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE,
  340. hash, hashlen,
  341. NULL, 0,
  342. NULL, 0,
  343. NULL, 0, p, len, out,
  344. SSL3_MASTER_SECRET_SIZE, 1)) {
  345. /* SSLfatal() already called */
  346. return 0;
  347. }
  348. OPENSSL_cleanse(hash, hashlen);
  349. } else {
  350. if (!tls1_PRF(s,
  351. TLS_MD_MASTER_SECRET_CONST,
  352. TLS_MD_MASTER_SECRET_CONST_SIZE,
  353. s->s3.client_random, SSL3_RANDOM_SIZE,
  354. NULL, 0,
  355. s->s3.server_random, SSL3_RANDOM_SIZE,
  356. NULL, 0, p, len, out,
  357. SSL3_MASTER_SECRET_SIZE, 1)) {
  358. /* SSLfatal() already called */
  359. return 0;
  360. }
  361. }
  362. OSSL_TRACE_BEGIN(TLS) {
  363. BIO_printf(trc_out, "Premaster Secret:\n");
  364. BIO_dump_indent(trc_out, p, len, 4);
  365. BIO_printf(trc_out, "Client Random:\n");
  366. BIO_dump_indent(trc_out, s->s3.client_random, SSL3_RANDOM_SIZE, 4);
  367. BIO_printf(trc_out, "Server Random:\n");
  368. BIO_dump_indent(trc_out, s->s3.server_random, SSL3_RANDOM_SIZE, 4);
  369. BIO_printf(trc_out, "Master Secret:\n");
  370. BIO_dump_indent(trc_out,
  371. s->session->master_key,
  372. SSL3_MASTER_SECRET_SIZE, 4);
  373. } OSSL_TRACE_END(TLS);
  374. *secret_size = SSL3_MASTER_SECRET_SIZE;
  375. return 1;
  376. }
  377. int tls1_export_keying_material(SSL_CONNECTION *s, unsigned char *out,
  378. size_t olen, const char *label, size_t llen,
  379. const unsigned char *context,
  380. size_t contextlen, int use_context)
  381. {
  382. unsigned char *val = NULL;
  383. size_t vallen = 0, currentvalpos;
  384. int rv = 0;
  385. /*
  386. * RFC 5705 embeds context length as uint16; reject longer context
  387. * before proceeding.
  388. */
  389. if (contextlen > 0xffff) {
  390. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT);
  391. return 0;
  392. }
  393. /*
  394. * construct PRF arguments we construct the PRF argument ourself rather
  395. * than passing separate values into the TLS PRF to ensure that the
  396. * concatenation of values does not create a prohibited label.
  397. */
  398. vallen = llen + SSL3_RANDOM_SIZE * 2;
  399. if (use_context) {
  400. vallen += 2 + contextlen;
  401. }
  402. val = OPENSSL_malloc(vallen);
  403. if (val == NULL)
  404. goto ret;
  405. currentvalpos = 0;
  406. memcpy(val + currentvalpos, (unsigned char *)label, llen);
  407. currentvalpos += llen;
  408. memcpy(val + currentvalpos, s->s3.client_random, SSL3_RANDOM_SIZE);
  409. currentvalpos += SSL3_RANDOM_SIZE;
  410. memcpy(val + currentvalpos, s->s3.server_random, SSL3_RANDOM_SIZE);
  411. currentvalpos += SSL3_RANDOM_SIZE;
  412. if (use_context) {
  413. val[currentvalpos] = (contextlen >> 8) & 0xff;
  414. currentvalpos++;
  415. val[currentvalpos] = contextlen & 0xff;
  416. currentvalpos++;
  417. if ((contextlen > 0) || (context != NULL)) {
  418. memcpy(val + currentvalpos, context, contextlen);
  419. }
  420. }
  421. /*
  422. * disallow prohibited labels note that SSL3_RANDOM_SIZE > max(prohibited
  423. * label len) = 15, so size of val > max(prohibited label len) = 15 and
  424. * the comparisons won't have buffer overflow
  425. */
  426. if (memcmp(val, TLS_MD_CLIENT_FINISH_CONST,
  427. TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0)
  428. goto err1;
  429. if (memcmp(val, TLS_MD_SERVER_FINISH_CONST,
  430. TLS_MD_SERVER_FINISH_CONST_SIZE) == 0)
  431. goto err1;
  432. if (memcmp(val, TLS_MD_MASTER_SECRET_CONST,
  433. TLS_MD_MASTER_SECRET_CONST_SIZE) == 0)
  434. goto err1;
  435. if (memcmp(val, TLS_MD_EXTENDED_MASTER_SECRET_CONST,
  436. TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE) == 0)
  437. goto err1;
  438. if (memcmp(val, TLS_MD_KEY_EXPANSION_CONST,
  439. TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0)
  440. goto err1;
  441. rv = tls1_PRF(s,
  442. val, vallen,
  443. NULL, 0,
  444. NULL, 0,
  445. NULL, 0,
  446. NULL, 0,
  447. s->session->master_key, s->session->master_key_length,
  448. out, olen, 0);
  449. goto ret;
  450. err1:
  451. ERR_raise(ERR_LIB_SSL, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
  452. ret:
  453. OPENSSL_clear_free(val, vallen);
  454. return rv;
  455. }
  456. int tls1_alert_code(int code)
  457. {
  458. switch (code) {
  459. case SSL_AD_CLOSE_NOTIFY:
  460. return SSL3_AD_CLOSE_NOTIFY;
  461. case SSL_AD_UNEXPECTED_MESSAGE:
  462. return SSL3_AD_UNEXPECTED_MESSAGE;
  463. case SSL_AD_BAD_RECORD_MAC:
  464. return SSL3_AD_BAD_RECORD_MAC;
  465. case SSL_AD_DECRYPTION_FAILED:
  466. return TLS1_AD_DECRYPTION_FAILED;
  467. case SSL_AD_RECORD_OVERFLOW:
  468. return TLS1_AD_RECORD_OVERFLOW;
  469. case SSL_AD_DECOMPRESSION_FAILURE:
  470. return SSL3_AD_DECOMPRESSION_FAILURE;
  471. case SSL_AD_HANDSHAKE_FAILURE:
  472. return SSL3_AD_HANDSHAKE_FAILURE;
  473. case SSL_AD_NO_CERTIFICATE:
  474. return -1;
  475. case SSL_AD_BAD_CERTIFICATE:
  476. return SSL3_AD_BAD_CERTIFICATE;
  477. case SSL_AD_UNSUPPORTED_CERTIFICATE:
  478. return SSL3_AD_UNSUPPORTED_CERTIFICATE;
  479. case SSL_AD_CERTIFICATE_REVOKED:
  480. return SSL3_AD_CERTIFICATE_REVOKED;
  481. case SSL_AD_CERTIFICATE_EXPIRED:
  482. return SSL3_AD_CERTIFICATE_EXPIRED;
  483. case SSL_AD_CERTIFICATE_UNKNOWN:
  484. return SSL3_AD_CERTIFICATE_UNKNOWN;
  485. case SSL_AD_ILLEGAL_PARAMETER:
  486. return SSL3_AD_ILLEGAL_PARAMETER;
  487. case SSL_AD_UNKNOWN_CA:
  488. return TLS1_AD_UNKNOWN_CA;
  489. case SSL_AD_ACCESS_DENIED:
  490. return TLS1_AD_ACCESS_DENIED;
  491. case SSL_AD_DECODE_ERROR:
  492. return TLS1_AD_DECODE_ERROR;
  493. case SSL_AD_DECRYPT_ERROR:
  494. return TLS1_AD_DECRYPT_ERROR;
  495. case SSL_AD_EXPORT_RESTRICTION:
  496. return TLS1_AD_EXPORT_RESTRICTION;
  497. case SSL_AD_PROTOCOL_VERSION:
  498. return TLS1_AD_PROTOCOL_VERSION;
  499. case SSL_AD_INSUFFICIENT_SECURITY:
  500. return TLS1_AD_INSUFFICIENT_SECURITY;
  501. case SSL_AD_INTERNAL_ERROR:
  502. return TLS1_AD_INTERNAL_ERROR;
  503. case SSL_AD_USER_CANCELLED:
  504. return TLS1_AD_USER_CANCELLED;
  505. case SSL_AD_NO_RENEGOTIATION:
  506. return TLS1_AD_NO_RENEGOTIATION;
  507. case SSL_AD_UNSUPPORTED_EXTENSION:
  508. return TLS1_AD_UNSUPPORTED_EXTENSION;
  509. case SSL_AD_CERTIFICATE_UNOBTAINABLE:
  510. return TLS1_AD_CERTIFICATE_UNOBTAINABLE;
  511. case SSL_AD_UNRECOGNIZED_NAME:
  512. return TLS1_AD_UNRECOGNIZED_NAME;
  513. case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
  514. return TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
  515. case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
  516. return TLS1_AD_BAD_CERTIFICATE_HASH_VALUE;
  517. case SSL_AD_UNKNOWN_PSK_IDENTITY:
  518. return TLS1_AD_UNKNOWN_PSK_IDENTITY;
  519. case SSL_AD_INAPPROPRIATE_FALLBACK:
  520. return TLS1_AD_INAPPROPRIATE_FALLBACK;
  521. case SSL_AD_NO_APPLICATION_PROTOCOL:
  522. return TLS1_AD_NO_APPLICATION_PROTOCOL;
  523. case SSL_AD_CERTIFICATE_REQUIRED:
  524. return SSL_AD_HANDSHAKE_FAILURE;
  525. case TLS13_AD_MISSING_EXTENSION:
  526. return SSL_AD_HANDSHAKE_FAILURE;
  527. default:
  528. return -1;
  529. }
  530. }