s3_enc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 <openssl/evp.h>
  13. #include <openssl/md5.h>
  14. #include <openssl/core_names.h>
  15. #include "internal/cryptlib.h"
  16. #include "internal/ssl_unwrap.h"
  17. static int ssl3_generate_key_block(SSL_CONNECTION *s, unsigned char *km, int num)
  18. {
  19. const EVP_MD *md5 = NULL, *sha1 = NULL;
  20. EVP_MD_CTX *m5;
  21. EVP_MD_CTX *s1;
  22. unsigned char buf[16], smd[SHA_DIGEST_LENGTH];
  23. unsigned char c = 'A';
  24. unsigned int i, k;
  25. int ret = 0;
  26. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  27. #ifdef CHARSET_EBCDIC
  28. c = os_toascii[c]; /* 'A' in ASCII */
  29. #endif
  30. k = 0;
  31. md5 = ssl_evp_md_fetch(sctx->libctx, NID_md5, sctx->propq);
  32. sha1 = ssl_evp_md_fetch(sctx->libctx, NID_sha1, sctx->propq);
  33. m5 = EVP_MD_CTX_new();
  34. s1 = EVP_MD_CTX_new();
  35. if (md5 == NULL || sha1 == NULL || m5 == NULL || s1 == NULL) {
  36. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  37. goto err;
  38. }
  39. for (i = 0; (int)i < num; i += MD5_DIGEST_LENGTH) {
  40. k++;
  41. if (k > sizeof(buf)) {
  42. /* bug: 'buf' is too small for this ciphersuite */
  43. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  44. goto err;
  45. }
  46. memset(buf, c, k);
  47. c++;
  48. if (!EVP_DigestInit_ex(s1, sha1, NULL)
  49. || !EVP_DigestUpdate(s1, buf, k)
  50. || !EVP_DigestUpdate(s1, s->session->master_key,
  51. s->session->master_key_length)
  52. || !EVP_DigestUpdate(s1, s->s3.server_random, SSL3_RANDOM_SIZE)
  53. || !EVP_DigestUpdate(s1, s->s3.client_random, SSL3_RANDOM_SIZE)
  54. || !EVP_DigestFinal_ex(s1, smd, NULL)
  55. || !EVP_DigestInit_ex(m5, md5, NULL)
  56. || !EVP_DigestUpdate(m5, s->session->master_key,
  57. s->session->master_key_length)
  58. || !EVP_DigestUpdate(m5, smd, SHA_DIGEST_LENGTH)) {
  59. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  60. goto err;
  61. }
  62. if ((int)(i + MD5_DIGEST_LENGTH) > num) {
  63. if (!EVP_DigestFinal_ex(m5, smd, NULL)) {
  64. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  65. goto err;
  66. }
  67. memcpy(km, smd, (num - i));
  68. } else {
  69. if (!EVP_DigestFinal_ex(m5, km, NULL)) {
  70. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  71. goto err;
  72. }
  73. }
  74. km += MD5_DIGEST_LENGTH;
  75. }
  76. OPENSSL_cleanse(smd, sizeof(smd));
  77. ret = 1;
  78. err:
  79. EVP_MD_CTX_free(m5);
  80. EVP_MD_CTX_free(s1);
  81. ssl_evp_md_free(md5);
  82. ssl_evp_md_free(sha1);
  83. return ret;
  84. }
  85. int ssl3_change_cipher_state(SSL_CONNECTION *s, int which)
  86. {
  87. unsigned char *p, *mac_secret;
  88. size_t md_len;
  89. unsigned char *key, *iv;
  90. const EVP_CIPHER *ciph;
  91. const SSL_COMP *comp = NULL;
  92. const EVP_MD *md;
  93. int mdi;
  94. size_t n, iv_len, key_len;
  95. int direction = (which & SSL3_CC_READ) != 0 ? OSSL_RECORD_DIRECTION_READ
  96. : OSSL_RECORD_DIRECTION_WRITE;
  97. ciph = s->s3.tmp.new_sym_enc;
  98. md = s->s3.tmp.new_hash;
  99. /* m == NULL will lead to a crash later */
  100. if (!ossl_assert(md != NULL)) {
  101. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  102. goto err;
  103. }
  104. #ifndef OPENSSL_NO_COMP
  105. comp = s->s3.tmp.new_compression;
  106. #endif
  107. p = s->s3.tmp.key_block;
  108. mdi = EVP_MD_get_size(md);
  109. if (mdi < 0) {
  110. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  111. goto err;
  112. }
  113. md_len = (size_t)mdi;
  114. key_len = EVP_CIPHER_get_key_length(ciph);
  115. iv_len = EVP_CIPHER_get_iv_length(ciph);
  116. if ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
  117. (which == SSL3_CHANGE_CIPHER_SERVER_READ)) {
  118. mac_secret = &(p[0]);
  119. n = md_len + md_len;
  120. key = &(p[n]);
  121. n += key_len + key_len;
  122. iv = &(p[n]);
  123. n += iv_len + iv_len;
  124. } else {
  125. n = md_len;
  126. mac_secret = &(p[n]);
  127. n += md_len + key_len;
  128. key = &(p[n]);
  129. n += key_len + iv_len;
  130. iv = &(p[n]);
  131. n += iv_len;
  132. }
  133. if (n > s->s3.tmp.key_block_length) {
  134. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  135. goto err;
  136. }
  137. if (!ssl_set_new_record_layer(s, SSL3_VERSION,
  138. direction,
  139. OSSL_RECORD_PROTECTION_LEVEL_APPLICATION,
  140. NULL, 0, key, key_len, iv, iv_len, mac_secret,
  141. md_len, ciph, 0, NID_undef, md, comp, NULL)) {
  142. /* SSLfatal already called */
  143. goto err;
  144. }
  145. return 1;
  146. err:
  147. return 0;
  148. }
  149. int ssl3_setup_key_block(SSL_CONNECTION *s)
  150. {
  151. unsigned char *p;
  152. const EVP_CIPHER *c;
  153. const EVP_MD *hash;
  154. int num;
  155. int ret = 0;
  156. SSL_COMP *comp;
  157. if (s->s3.tmp.key_block_length != 0)
  158. return 1;
  159. if (!ssl_cipher_get_evp(SSL_CONNECTION_GET_CTX(s), s->session, &c, &hash,
  160. NULL, NULL, &comp, 0)) {
  161. /* Error is already recorded */
  162. SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
  163. return 0;
  164. }
  165. ssl_evp_cipher_free(s->s3.tmp.new_sym_enc);
  166. s->s3.tmp.new_sym_enc = c;
  167. ssl_evp_md_free(s->s3.tmp.new_hash);
  168. s->s3.tmp.new_hash = hash;
  169. #ifdef OPENSSL_NO_COMP
  170. s->s3.tmp.new_compression = NULL;
  171. #else
  172. s->s3.tmp.new_compression = comp;
  173. #endif
  174. num = EVP_MD_get_size(hash);
  175. if (num < 0)
  176. return 0;
  177. num = EVP_CIPHER_get_key_length(c) + num + EVP_CIPHER_get_iv_length(c);
  178. num *= 2;
  179. ssl3_cleanup_key_block(s);
  180. if ((p = OPENSSL_malloc(num)) == NULL) {
  181. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
  182. return 0;
  183. }
  184. s->s3.tmp.key_block_length = num;
  185. s->s3.tmp.key_block = p;
  186. /* Calls SSLfatal() as required */
  187. ret = ssl3_generate_key_block(s, p, num);
  188. return ret;
  189. }
  190. void ssl3_cleanup_key_block(SSL_CONNECTION *s)
  191. {
  192. OPENSSL_clear_free(s->s3.tmp.key_block, s->s3.tmp.key_block_length);
  193. s->s3.tmp.key_block = NULL;
  194. s->s3.tmp.key_block_length = 0;
  195. }
  196. int ssl3_init_finished_mac(SSL_CONNECTION *s)
  197. {
  198. BIO *buf = BIO_new(BIO_s_mem());
  199. if (buf == NULL) {
  200. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BIO_LIB);
  201. return 0;
  202. }
  203. ssl3_free_digest_list(s);
  204. s->s3.handshake_buffer = buf;
  205. (void)BIO_set_close(s->s3.handshake_buffer, BIO_CLOSE);
  206. return 1;
  207. }
  208. /*
  209. * Free digest list. Also frees handshake buffer since they are always freed
  210. * together.
  211. */
  212. void ssl3_free_digest_list(SSL_CONNECTION *s)
  213. {
  214. BIO_free(s->s3.handshake_buffer);
  215. s->s3.handshake_buffer = NULL;
  216. EVP_MD_CTX_free(s->s3.handshake_dgst);
  217. s->s3.handshake_dgst = NULL;
  218. }
  219. int ssl3_finish_mac(SSL_CONNECTION *s, const unsigned char *buf, size_t len)
  220. {
  221. int ret;
  222. if (s->s3.handshake_dgst == NULL) {
  223. /* Note: this writes to a memory BIO so a failure is a fatal error */
  224. if (len > INT_MAX) {
  225. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_OVERFLOW_ERROR);
  226. return 0;
  227. }
  228. ret = BIO_write(s->s3.handshake_buffer, (void *)buf, (int)len);
  229. if (ret <= 0 || ret != (int)len) {
  230. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  231. return 0;
  232. }
  233. } else {
  234. ret = EVP_DigestUpdate(s->s3.handshake_dgst, buf, len);
  235. if (!ret) {
  236. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  237. return 0;
  238. }
  239. }
  240. return 1;
  241. }
  242. int ssl3_digest_cached_records(SSL_CONNECTION *s, int keep)
  243. {
  244. const EVP_MD *md;
  245. long hdatalen;
  246. void *hdata;
  247. if (s->s3.handshake_dgst == NULL) {
  248. hdatalen = BIO_get_mem_data(s->s3.handshake_buffer, &hdata);
  249. if (hdatalen <= 0) {
  250. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_LENGTH);
  251. return 0;
  252. }
  253. s->s3.handshake_dgst = EVP_MD_CTX_new();
  254. if (s->s3.handshake_dgst == NULL) {
  255. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  256. return 0;
  257. }
  258. md = ssl_handshake_md(s);
  259. if (md == NULL) {
  260. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  261. SSL_R_NO_SUITABLE_DIGEST_ALGORITHM);
  262. return 0;
  263. }
  264. if (!EVP_DigestInit_ex(s->s3.handshake_dgst, md, NULL)
  265. || !EVP_DigestUpdate(s->s3.handshake_dgst, hdata, hdatalen)) {
  266. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  267. return 0;
  268. }
  269. }
  270. if (keep == 0) {
  271. BIO_free(s->s3.handshake_buffer);
  272. s->s3.handshake_buffer = NULL;
  273. }
  274. return 1;
  275. }
  276. void ssl3_digest_master_key_set_params(const SSL_SESSION *session,
  277. OSSL_PARAM params[])
  278. {
  279. int n = 0;
  280. params[n++] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
  281. (void *)session->master_key,
  282. session->master_key_length);
  283. params[n++] = OSSL_PARAM_construct_end();
  284. }
  285. size_t ssl3_final_finish_mac(SSL_CONNECTION *s, const char *sender, size_t len,
  286. unsigned char *p)
  287. {
  288. int ret;
  289. EVP_MD_CTX *ctx = NULL;
  290. if (!ssl3_digest_cached_records(s, 0)) {
  291. /* SSLfatal() already called */
  292. return 0;
  293. }
  294. if (EVP_MD_CTX_get_type(s->s3.handshake_dgst) != NID_md5_sha1) {
  295. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_REQUIRED_DIGEST);
  296. return 0;
  297. }
  298. ctx = EVP_MD_CTX_new();
  299. if (ctx == NULL) {
  300. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  301. return 0;
  302. }
  303. if (!EVP_MD_CTX_copy_ex(ctx, s->s3.handshake_dgst)) {
  304. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  305. ret = 0;
  306. goto err;
  307. }
  308. ret = EVP_MD_CTX_get_size(ctx);
  309. if (ret < 0) {
  310. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  311. ret = 0;
  312. goto err;
  313. }
  314. if (sender != NULL) {
  315. OSSL_PARAM digest_cmd_params[3];
  316. ssl3_digest_master_key_set_params(s->session, digest_cmd_params);
  317. if (EVP_DigestUpdate(ctx, sender, len) <= 0
  318. || EVP_MD_CTX_set_params(ctx, digest_cmd_params) <= 0
  319. || EVP_DigestFinal_ex(ctx, p, NULL) <= 0) {
  320. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  321. ret = 0;
  322. }
  323. }
  324. err:
  325. EVP_MD_CTX_free(ctx);
  326. return ret;
  327. }
  328. int ssl3_generate_master_secret(SSL_CONNECTION *s, unsigned char *out,
  329. unsigned char *p,
  330. size_t len, size_t *secret_size)
  331. {
  332. static const unsigned char *const salt[3] = {
  333. #ifndef CHARSET_EBCDIC
  334. (const unsigned char *)"A",
  335. (const unsigned char *)"BB",
  336. (const unsigned char *)"CCC",
  337. #else
  338. (const unsigned char *)"\x41",
  339. (const unsigned char *)"\x42\x42",
  340. (const unsigned char *)"\x43\x43\x43",
  341. #endif
  342. };
  343. unsigned char buf[EVP_MAX_MD_SIZE];
  344. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  345. int i, ret = 1;
  346. unsigned int n;
  347. size_t ret_secret_size = 0;
  348. if (ctx == NULL) {
  349. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  350. return 0;
  351. }
  352. for (i = 0; i < 3; i++) {
  353. if (EVP_DigestInit_ex(ctx, SSL_CONNECTION_GET_CTX(s)->sha1, NULL) <= 0
  354. || EVP_DigestUpdate(ctx, salt[i],
  355. strlen((const char *)salt[i])) <= 0
  356. || EVP_DigestUpdate(ctx, p, len) <= 0
  357. || EVP_DigestUpdate(ctx, &(s->s3.client_random[0]),
  358. SSL3_RANDOM_SIZE) <= 0
  359. || EVP_DigestUpdate(ctx, &(s->s3.server_random[0]),
  360. SSL3_RANDOM_SIZE) <= 0
  361. || EVP_DigestFinal_ex(ctx, buf, &n) <= 0
  362. || EVP_DigestInit_ex(ctx, SSL_CONNECTION_GET_CTX(s)->md5, NULL) <= 0
  363. || EVP_DigestUpdate(ctx, p, len) <= 0
  364. || EVP_DigestUpdate(ctx, buf, n) <= 0
  365. || EVP_DigestFinal_ex(ctx, out, &n) <= 0) {
  366. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  367. ret = 0;
  368. break;
  369. }
  370. out += n;
  371. ret_secret_size += n;
  372. }
  373. EVP_MD_CTX_free(ctx);
  374. OPENSSL_cleanse(buf, sizeof(buf));
  375. if (ret)
  376. *secret_size = ret_secret_size;
  377. return ret;
  378. }
  379. int ssl3_alert_code(int code)
  380. {
  381. switch (code) {
  382. case SSL_AD_CLOSE_NOTIFY:
  383. return SSL3_AD_CLOSE_NOTIFY;
  384. case SSL_AD_UNEXPECTED_MESSAGE:
  385. return SSL3_AD_UNEXPECTED_MESSAGE;
  386. case SSL_AD_BAD_RECORD_MAC:
  387. return SSL3_AD_BAD_RECORD_MAC;
  388. case SSL_AD_DECRYPTION_FAILED:
  389. return SSL3_AD_BAD_RECORD_MAC;
  390. case SSL_AD_RECORD_OVERFLOW:
  391. return SSL3_AD_BAD_RECORD_MAC;
  392. case SSL_AD_DECOMPRESSION_FAILURE:
  393. return SSL3_AD_DECOMPRESSION_FAILURE;
  394. case SSL_AD_HANDSHAKE_FAILURE:
  395. return SSL3_AD_HANDSHAKE_FAILURE;
  396. case SSL_AD_NO_CERTIFICATE:
  397. return SSL3_AD_NO_CERTIFICATE;
  398. case SSL_AD_BAD_CERTIFICATE:
  399. return SSL3_AD_BAD_CERTIFICATE;
  400. case SSL_AD_UNSUPPORTED_CERTIFICATE:
  401. return SSL3_AD_UNSUPPORTED_CERTIFICATE;
  402. case SSL_AD_CERTIFICATE_REVOKED:
  403. return SSL3_AD_CERTIFICATE_REVOKED;
  404. case SSL_AD_CERTIFICATE_EXPIRED:
  405. return SSL3_AD_CERTIFICATE_EXPIRED;
  406. case SSL_AD_CERTIFICATE_UNKNOWN:
  407. return SSL3_AD_CERTIFICATE_UNKNOWN;
  408. case SSL_AD_ILLEGAL_PARAMETER:
  409. return SSL3_AD_ILLEGAL_PARAMETER;
  410. case SSL_AD_UNKNOWN_CA:
  411. return SSL3_AD_BAD_CERTIFICATE;
  412. case SSL_AD_ACCESS_DENIED:
  413. return SSL3_AD_HANDSHAKE_FAILURE;
  414. case SSL_AD_DECODE_ERROR:
  415. return SSL3_AD_HANDSHAKE_FAILURE;
  416. case SSL_AD_DECRYPT_ERROR:
  417. return SSL3_AD_HANDSHAKE_FAILURE;
  418. case SSL_AD_EXPORT_RESTRICTION:
  419. return SSL3_AD_HANDSHAKE_FAILURE;
  420. case SSL_AD_PROTOCOL_VERSION:
  421. return SSL3_AD_HANDSHAKE_FAILURE;
  422. case SSL_AD_INSUFFICIENT_SECURITY:
  423. return SSL3_AD_HANDSHAKE_FAILURE;
  424. case SSL_AD_INTERNAL_ERROR:
  425. return SSL3_AD_HANDSHAKE_FAILURE;
  426. case SSL_AD_USER_CANCELLED:
  427. return SSL3_AD_HANDSHAKE_FAILURE;
  428. case SSL_AD_NO_RENEGOTIATION:
  429. return -1; /* Don't send it :-) */
  430. case SSL_AD_UNSUPPORTED_EXTENSION:
  431. return SSL3_AD_HANDSHAKE_FAILURE;
  432. case SSL_AD_CERTIFICATE_UNOBTAINABLE:
  433. return SSL3_AD_HANDSHAKE_FAILURE;
  434. case SSL_AD_UNRECOGNIZED_NAME:
  435. return SSL3_AD_HANDSHAKE_FAILURE;
  436. case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
  437. return SSL3_AD_HANDSHAKE_FAILURE;
  438. case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
  439. return SSL3_AD_HANDSHAKE_FAILURE;
  440. case SSL_AD_UNKNOWN_PSK_IDENTITY:
  441. return TLS1_AD_UNKNOWN_PSK_IDENTITY;
  442. case SSL_AD_INAPPROPRIATE_FALLBACK:
  443. return TLS1_AD_INAPPROPRIATE_FALLBACK;
  444. case SSL_AD_NO_APPLICATION_PROTOCOL:
  445. return TLS1_AD_NO_APPLICATION_PROTOCOL;
  446. case SSL_AD_CERTIFICATE_REQUIRED:
  447. return SSL_AD_HANDSHAKE_FAILURE;
  448. case TLS13_AD_MISSING_EXTENSION:
  449. return SSL_AD_HANDSHAKE_FAILURE;
  450. default:
  451. return -1;
  452. }
  453. }