2
0

t1_enc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. /*
  2. * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2005 Nokia. 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 <stdio.h>
  11. #include "ssl_local.h"
  12. #include <openssl/comp.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/kdf.h>
  15. #include <openssl/rand.h>
  16. /* seed1 through seed5 are concatenated */
  17. static int tls1_PRF(SSL *s,
  18. const void *seed1, size_t seed1_len,
  19. const void *seed2, size_t seed2_len,
  20. const void *seed3, size_t seed3_len,
  21. const void *seed4, size_t seed4_len,
  22. const void *seed5, size_t seed5_len,
  23. const unsigned char *sec, size_t slen,
  24. unsigned char *out, size_t olen, int fatal)
  25. {
  26. const EVP_MD *md = ssl_prf_md(s);
  27. EVP_PKEY_CTX *pctx = NULL;
  28. int ret = 0;
  29. if (md == NULL) {
  30. /* Should never happen */
  31. if (fatal)
  32. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_PRF,
  33. ERR_R_INTERNAL_ERROR);
  34. else
  35. SSLerr(SSL_F_TLS1_PRF, ERR_R_INTERNAL_ERROR);
  36. return 0;
  37. }
  38. pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_TLS1_PRF, NULL);
  39. if (pctx == NULL || EVP_PKEY_derive_init(pctx) <= 0
  40. || EVP_PKEY_CTX_set_tls1_prf_md(pctx, md) <= 0
  41. || EVP_PKEY_CTX_set1_tls1_prf_secret(pctx, sec, (int)slen) <= 0
  42. || EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed1, (int)seed1_len) <= 0
  43. || EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed2, (int)seed2_len) <= 0
  44. || EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed3, (int)seed3_len) <= 0
  45. || EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed4, (int)seed4_len) <= 0
  46. || EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed5, (int)seed5_len) <= 0
  47. || EVP_PKEY_derive(pctx, out, &olen) <= 0) {
  48. if (fatal)
  49. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_PRF,
  50. ERR_R_INTERNAL_ERROR);
  51. else
  52. SSLerr(SSL_F_TLS1_PRF, ERR_R_INTERNAL_ERROR);
  53. goto err;
  54. }
  55. ret = 1;
  56. err:
  57. EVP_PKEY_CTX_free(pctx);
  58. return ret;
  59. }
  60. static int tls1_generate_key_block(SSL *s, unsigned char *km, size_t num)
  61. {
  62. int ret;
  63. /* Calls SSLfatal() as required */
  64. ret = tls1_PRF(s,
  65. TLS_MD_KEY_EXPANSION_CONST,
  66. TLS_MD_KEY_EXPANSION_CONST_SIZE, s->s3->server_random,
  67. SSL3_RANDOM_SIZE, s->s3->client_random, SSL3_RANDOM_SIZE,
  68. NULL, 0, NULL, 0, s->session->master_key,
  69. s->session->master_key_length, km, num, 1);
  70. return ret;
  71. }
  72. int tls1_change_cipher_state(SSL *s, int which)
  73. {
  74. unsigned char *p, *mac_secret;
  75. unsigned char *ms, *key, *iv;
  76. EVP_CIPHER_CTX *dd;
  77. const EVP_CIPHER *c;
  78. #ifndef OPENSSL_NO_COMP
  79. const SSL_COMP *comp;
  80. #endif
  81. const EVP_MD *m;
  82. int mac_type;
  83. size_t *mac_secret_size;
  84. EVP_MD_CTX *mac_ctx;
  85. EVP_PKEY *mac_key;
  86. size_t n, i, j, k, cl;
  87. int reuse_dd = 0;
  88. c = s->s3->tmp.new_sym_enc;
  89. m = s->s3->tmp.new_hash;
  90. mac_type = s->s3->tmp.new_mac_pkey_type;
  91. #ifndef OPENSSL_NO_COMP
  92. comp = s->s3->tmp.new_compression;
  93. #endif
  94. if (which & SSL3_CC_READ) {
  95. if (s->ext.use_etm)
  96. s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC_READ;
  97. else
  98. s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_READ;
  99. if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
  100. s->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM;
  101. else
  102. s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM;
  103. if (s->enc_read_ctx != NULL) {
  104. reuse_dd = 1;
  105. } else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL) {
  106. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
  107. ERR_R_MALLOC_FAILURE);
  108. goto err;
  109. } else {
  110. /*
  111. * make sure it's initialised in case we exit later with an error
  112. */
  113. EVP_CIPHER_CTX_reset(s->enc_read_ctx);
  114. }
  115. dd = s->enc_read_ctx;
  116. mac_ctx = ssl_replace_hash(&s->read_hash, NULL);
  117. if (mac_ctx == NULL)
  118. goto err;
  119. #ifndef OPENSSL_NO_COMP
  120. COMP_CTX_free(s->expand);
  121. s->expand = NULL;
  122. if (comp != NULL) {
  123. s->expand = COMP_CTX_new(comp->method);
  124. if (s->expand == NULL) {
  125. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  126. SSL_F_TLS1_CHANGE_CIPHER_STATE,
  127. SSL_R_COMPRESSION_LIBRARY_ERROR);
  128. goto err;
  129. }
  130. }
  131. #endif
  132. /*
  133. * this is done by dtls1_reset_seq_numbers for DTLS
  134. */
  135. if (!SSL_IS_DTLS(s))
  136. RECORD_LAYER_reset_read_sequence(&s->rlayer);
  137. mac_secret = &(s->s3->read_mac_secret[0]);
  138. mac_secret_size = &(s->s3->read_mac_secret_size);
  139. } else {
  140. s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
  141. if (s->ext.use_etm)
  142. s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE;
  143. else
  144. s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE;
  145. if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
  146. s->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_STREAM;
  147. else
  148. s->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_STREAM;
  149. if (s->enc_write_ctx != NULL && !SSL_IS_DTLS(s)) {
  150. reuse_dd = 1;
  151. } else if ((s->enc_write_ctx = EVP_CIPHER_CTX_new()) == NULL) {
  152. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
  153. ERR_R_MALLOC_FAILURE);
  154. goto err;
  155. }
  156. dd = s->enc_write_ctx;
  157. if (SSL_IS_DTLS(s)) {
  158. mac_ctx = EVP_MD_CTX_new();
  159. if (mac_ctx == NULL) {
  160. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  161. SSL_F_TLS1_CHANGE_CIPHER_STATE,
  162. ERR_R_MALLOC_FAILURE);
  163. goto err;
  164. }
  165. s->write_hash = mac_ctx;
  166. } else {
  167. mac_ctx = ssl_replace_hash(&s->write_hash, NULL);
  168. if (mac_ctx == NULL) {
  169. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  170. SSL_F_TLS1_CHANGE_CIPHER_STATE,
  171. ERR_R_MALLOC_FAILURE);
  172. goto err;
  173. }
  174. }
  175. #ifndef OPENSSL_NO_COMP
  176. COMP_CTX_free(s->compress);
  177. s->compress = NULL;
  178. if (comp != NULL) {
  179. s->compress = COMP_CTX_new(comp->method);
  180. if (s->compress == NULL) {
  181. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  182. SSL_F_TLS1_CHANGE_CIPHER_STATE,
  183. SSL_R_COMPRESSION_LIBRARY_ERROR);
  184. goto err;
  185. }
  186. }
  187. #endif
  188. /*
  189. * this is done by dtls1_reset_seq_numbers for DTLS
  190. */
  191. if (!SSL_IS_DTLS(s))
  192. RECORD_LAYER_reset_write_sequence(&s->rlayer);
  193. mac_secret = &(s->s3->write_mac_secret[0]);
  194. mac_secret_size = &(s->s3->write_mac_secret_size);
  195. }
  196. if (reuse_dd)
  197. EVP_CIPHER_CTX_reset(dd);
  198. p = s->s3->tmp.key_block;
  199. i = *mac_secret_size = s->s3->tmp.new_mac_secret_size;
  200. /* TODO(size_t): convert me */
  201. cl = EVP_CIPHER_key_length(c);
  202. j = cl;
  203. /* Was j=(exp)?5:EVP_CIPHER_key_length(c); */
  204. /* If GCM/CCM mode only part of IV comes from PRF */
  205. if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE)
  206. k = EVP_GCM_TLS_FIXED_IV_LEN;
  207. else if (EVP_CIPHER_mode(c) == EVP_CIPH_CCM_MODE)
  208. k = EVP_CCM_TLS_FIXED_IV_LEN;
  209. else
  210. k = EVP_CIPHER_iv_length(c);
  211. if ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
  212. (which == SSL3_CHANGE_CIPHER_SERVER_READ)) {
  213. ms = &(p[0]);
  214. n = i + i;
  215. key = &(p[n]);
  216. n += j + j;
  217. iv = &(p[n]);
  218. n += k + k;
  219. } else {
  220. n = i;
  221. ms = &(p[n]);
  222. n += i + j;
  223. key = &(p[n]);
  224. n += j + k;
  225. iv = &(p[n]);
  226. n += k;
  227. }
  228. if (n > s->s3->tmp.key_block_length) {
  229. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
  230. ERR_R_INTERNAL_ERROR);
  231. goto err;
  232. }
  233. memcpy(mac_secret, ms, i);
  234. if (!(EVP_CIPHER_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
  235. /* TODO(size_t): Convert this function */
  236. mac_key = EVP_PKEY_new_mac_key(mac_type, NULL, mac_secret,
  237. (int)*mac_secret_size);
  238. if (mac_key == NULL
  239. || EVP_DigestSignInit(mac_ctx, NULL, m, NULL, mac_key) <= 0) {
  240. EVP_PKEY_free(mac_key);
  241. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
  242. ERR_R_INTERNAL_ERROR);
  243. goto err;
  244. }
  245. EVP_PKEY_free(mac_key);
  246. }
  247. #ifdef SSL_DEBUG
  248. printf("which = %04X\nmac key=", which);
  249. {
  250. size_t z;
  251. for (z = 0; z < i; z++)
  252. printf("%02X%c", ms[z], ((z + 1) % 16) ? ' ' : '\n');
  253. }
  254. #endif
  255. if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE) {
  256. if (!EVP_CipherInit_ex(dd, c, NULL, key, NULL, (which & SSL3_CC_WRITE))
  257. || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_GCM_SET_IV_FIXED, (int)k,
  258. iv)) {
  259. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
  260. ERR_R_INTERNAL_ERROR);
  261. goto err;
  262. }
  263. } else if (EVP_CIPHER_mode(c) == EVP_CIPH_CCM_MODE) {
  264. int taglen;
  265. if (s->s3->tmp.
  266. new_cipher->algorithm_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
  267. taglen = EVP_CCM8_TLS_TAG_LEN;
  268. else
  269. taglen = EVP_CCM_TLS_TAG_LEN;
  270. if (!EVP_CipherInit_ex(dd, c, NULL, NULL, NULL, (which & SSL3_CC_WRITE))
  271. || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_IVLEN, 12, NULL)
  272. || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_TAG, taglen, NULL)
  273. || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_CCM_SET_IV_FIXED, (int)k, iv)
  274. || !EVP_CipherInit_ex(dd, NULL, NULL, key, NULL, -1)) {
  275. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
  276. ERR_R_INTERNAL_ERROR);
  277. goto err;
  278. }
  279. } else {
  280. if (!EVP_CipherInit_ex(dd, c, NULL, key, iv, (which & SSL3_CC_WRITE))) {
  281. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
  282. ERR_R_INTERNAL_ERROR);
  283. goto err;
  284. }
  285. }
  286. /* Needed for "composite" AEADs, such as RC4-HMAC-MD5 */
  287. if ((EVP_CIPHER_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER) && *mac_secret_size
  288. && !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_MAC_KEY,
  289. (int)*mac_secret_size, mac_secret)) {
  290. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
  291. ERR_R_INTERNAL_ERROR);
  292. goto err;
  293. }
  294. s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
  295. #ifdef SSL_DEBUG
  296. printf("which = %04X\nkey=", which);
  297. {
  298. int z;
  299. for (z = 0; z < EVP_CIPHER_key_length(c); z++)
  300. printf("%02X%c", key[z], ((z + 1) % 16) ? ' ' : '\n');
  301. }
  302. printf("\niv=");
  303. {
  304. size_t z;
  305. for (z = 0; z < k; z++)
  306. printf("%02X%c", iv[z], ((z + 1) % 16) ? ' ' : '\n');
  307. }
  308. printf("\n");
  309. #endif
  310. return 1;
  311. err:
  312. return 0;
  313. }
  314. int tls1_setup_key_block(SSL *s)
  315. {
  316. unsigned char *p;
  317. const EVP_CIPHER *c;
  318. const EVP_MD *hash;
  319. SSL_COMP *comp;
  320. int mac_type = NID_undef;
  321. size_t num, mac_secret_size = 0;
  322. int ret = 0;
  323. if (s->s3->tmp.key_block_length != 0)
  324. return 1;
  325. if (!ssl_cipher_get_evp(s->session, &c, &hash, &mac_type, &mac_secret_size,
  326. &comp, s->ext.use_etm)) {
  327. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_SETUP_KEY_BLOCK,
  328. SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
  329. return 0;
  330. }
  331. s->s3->tmp.new_sym_enc = c;
  332. s->s3->tmp.new_hash = hash;
  333. s->s3->tmp.new_mac_pkey_type = mac_type;
  334. s->s3->tmp.new_mac_secret_size = mac_secret_size;
  335. num = EVP_CIPHER_key_length(c) + mac_secret_size + EVP_CIPHER_iv_length(c);
  336. num *= 2;
  337. ssl3_cleanup_key_block(s);
  338. if ((p = OPENSSL_malloc(num)) == NULL) {
  339. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_SETUP_KEY_BLOCK,
  340. ERR_R_MALLOC_FAILURE);
  341. goto err;
  342. }
  343. s->s3->tmp.key_block_length = num;
  344. s->s3->tmp.key_block = p;
  345. #ifdef SSL_DEBUG
  346. printf("client random\n");
  347. {
  348. int z;
  349. for (z = 0; z < SSL3_RANDOM_SIZE; z++)
  350. printf("%02X%c", s->s3->client_random[z],
  351. ((z + 1) % 16) ? ' ' : '\n');
  352. }
  353. printf("server random\n");
  354. {
  355. int z;
  356. for (z = 0; z < SSL3_RANDOM_SIZE; z++)
  357. printf("%02X%c", s->s3->server_random[z],
  358. ((z + 1) % 16) ? ' ' : '\n');
  359. }
  360. printf("master key\n");
  361. {
  362. size_t z;
  363. for (z = 0; z < s->session->master_key_length; z++)
  364. printf("%02X%c", s->session->master_key[z],
  365. ((z + 1) % 16) ? ' ' : '\n');
  366. }
  367. #endif
  368. if (!tls1_generate_key_block(s, p, num)) {
  369. /* SSLfatal() already called */
  370. goto err;
  371. }
  372. #ifdef SSL_DEBUG
  373. printf("\nkey block\n");
  374. {
  375. size_t z;
  376. for (z = 0; z < num; z++)
  377. printf("%02X%c", p[z], ((z + 1) % 16) ? ' ' : '\n');
  378. }
  379. #endif
  380. if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)
  381. && s->method->version <= TLS1_VERSION) {
  382. /*
  383. * enable vulnerability countermeasure for CBC ciphers with known-IV
  384. * problem (http://www.openssl.org/~bodo/tls-cbc.txt)
  385. */
  386. s->s3->need_empty_fragments = 1;
  387. if (s->session->cipher != NULL) {
  388. if (s->session->cipher->algorithm_enc == SSL_eNULL)
  389. s->s3->need_empty_fragments = 0;
  390. #ifndef OPENSSL_NO_RC4
  391. if (s->session->cipher->algorithm_enc == SSL_RC4)
  392. s->s3->need_empty_fragments = 0;
  393. #endif
  394. }
  395. }
  396. ret = 1;
  397. err:
  398. return ret;
  399. }
  400. size_t tls1_final_finish_mac(SSL *s, const char *str, size_t slen,
  401. unsigned char *out)
  402. {
  403. size_t hashlen;
  404. unsigned char hash[EVP_MAX_MD_SIZE];
  405. if (!ssl3_digest_cached_records(s, 0)) {
  406. /* SSLfatal() already called */
  407. return 0;
  408. }
  409. if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
  410. /* SSLfatal() already called */
  411. return 0;
  412. }
  413. if (!tls1_PRF(s, str, slen, hash, hashlen, NULL, 0, NULL, 0, NULL, 0,
  414. s->session->master_key, s->session->master_key_length,
  415. out, TLS1_FINISH_MAC_LENGTH, 1)) {
  416. /* SSLfatal() already called */
  417. return 0;
  418. }
  419. OPENSSL_cleanse(hash, hashlen);
  420. return TLS1_FINISH_MAC_LENGTH;
  421. }
  422. int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
  423. size_t len, size_t *secret_size)
  424. {
  425. if (s->session->flags & SSL_SESS_FLAG_EXTMS) {
  426. unsigned char hash[EVP_MAX_MD_SIZE * 2];
  427. size_t hashlen;
  428. /*
  429. * Digest cached records keeping record buffer (if present): this won't
  430. * affect client auth because we're freezing the buffer at the same
  431. * point (after client key exchange and before certificate verify)
  432. */
  433. if (!ssl3_digest_cached_records(s, 1)
  434. || !ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
  435. /* SSLfatal() already called */
  436. return 0;
  437. }
  438. #ifdef SSL_DEBUG
  439. fprintf(stderr, "Handshake hashes:\n");
  440. BIO_dump_fp(stderr, (char *)hash, hashlen);
  441. #endif
  442. if (!tls1_PRF(s,
  443. TLS_MD_EXTENDED_MASTER_SECRET_CONST,
  444. TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE,
  445. hash, hashlen,
  446. NULL, 0,
  447. NULL, 0,
  448. NULL, 0, p, len, out,
  449. SSL3_MASTER_SECRET_SIZE, 1)) {
  450. /* SSLfatal() already called */
  451. return 0;
  452. }
  453. OPENSSL_cleanse(hash, hashlen);
  454. } else {
  455. if (!tls1_PRF(s,
  456. TLS_MD_MASTER_SECRET_CONST,
  457. TLS_MD_MASTER_SECRET_CONST_SIZE,
  458. s->s3->client_random, SSL3_RANDOM_SIZE,
  459. NULL, 0,
  460. s->s3->server_random, SSL3_RANDOM_SIZE,
  461. NULL, 0, p, len, out,
  462. SSL3_MASTER_SECRET_SIZE, 1)) {
  463. /* SSLfatal() already called */
  464. return 0;
  465. }
  466. }
  467. #ifdef SSL_DEBUG
  468. fprintf(stderr, "Premaster Secret:\n");
  469. BIO_dump_fp(stderr, (char *)p, len);
  470. fprintf(stderr, "Client Random:\n");
  471. BIO_dump_fp(stderr, (char *)s->s3->client_random, SSL3_RANDOM_SIZE);
  472. fprintf(stderr, "Server Random:\n");
  473. BIO_dump_fp(stderr, (char *)s->s3->server_random, SSL3_RANDOM_SIZE);
  474. fprintf(stderr, "Master Secret:\n");
  475. BIO_dump_fp(stderr, (char *)s->session->master_key,
  476. SSL3_MASTER_SECRET_SIZE);
  477. #endif
  478. *secret_size = SSL3_MASTER_SECRET_SIZE;
  479. return 1;
  480. }
  481. int tls1_export_keying_material(SSL *s, unsigned char *out, size_t olen,
  482. const char *label, size_t llen,
  483. const unsigned char *context,
  484. size_t contextlen, int use_context)
  485. {
  486. unsigned char *val = NULL;
  487. size_t vallen = 0, currentvalpos;
  488. int rv;
  489. /*
  490. * construct PRF arguments we construct the PRF argument ourself rather
  491. * than passing separate values into the TLS PRF to ensure that the
  492. * concatenation of values does not create a prohibited label.
  493. */
  494. vallen = llen + SSL3_RANDOM_SIZE * 2;
  495. if (use_context) {
  496. vallen += 2 + contextlen;
  497. }
  498. val = OPENSSL_malloc(vallen);
  499. if (val == NULL)
  500. goto err2;
  501. currentvalpos = 0;
  502. memcpy(val + currentvalpos, (unsigned char *)label, llen);
  503. currentvalpos += llen;
  504. memcpy(val + currentvalpos, s->s3->client_random, SSL3_RANDOM_SIZE);
  505. currentvalpos += SSL3_RANDOM_SIZE;
  506. memcpy(val + currentvalpos, s->s3->server_random, SSL3_RANDOM_SIZE);
  507. currentvalpos += SSL3_RANDOM_SIZE;
  508. if (use_context) {
  509. val[currentvalpos] = (contextlen >> 8) & 0xff;
  510. currentvalpos++;
  511. val[currentvalpos] = contextlen & 0xff;
  512. currentvalpos++;
  513. if ((contextlen > 0) || (context != NULL)) {
  514. memcpy(val + currentvalpos, context, contextlen);
  515. }
  516. }
  517. /*
  518. * disallow prohibited labels note that SSL3_RANDOM_SIZE > max(prohibited
  519. * label len) = 15, so size of val > max(prohibited label len) = 15 and
  520. * the comparisons won't have buffer overflow
  521. */
  522. if (memcmp(val, TLS_MD_CLIENT_FINISH_CONST,
  523. TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0)
  524. goto err1;
  525. if (memcmp(val, TLS_MD_SERVER_FINISH_CONST,
  526. TLS_MD_SERVER_FINISH_CONST_SIZE) == 0)
  527. goto err1;
  528. if (memcmp(val, TLS_MD_MASTER_SECRET_CONST,
  529. TLS_MD_MASTER_SECRET_CONST_SIZE) == 0)
  530. goto err1;
  531. if (memcmp(val, TLS_MD_EXTENDED_MASTER_SECRET_CONST,
  532. TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE) == 0)
  533. goto err1;
  534. if (memcmp(val, TLS_MD_KEY_EXPANSION_CONST,
  535. TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0)
  536. goto err1;
  537. rv = tls1_PRF(s,
  538. val, vallen,
  539. NULL, 0,
  540. NULL, 0,
  541. NULL, 0,
  542. NULL, 0,
  543. s->session->master_key, s->session->master_key_length,
  544. out, olen, 0);
  545. goto ret;
  546. err1:
  547. SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
  548. rv = 0;
  549. goto ret;
  550. err2:
  551. SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, ERR_R_MALLOC_FAILURE);
  552. rv = 0;
  553. ret:
  554. OPENSSL_clear_free(val, vallen);
  555. return rv;
  556. }
  557. int tls1_alert_code(int code)
  558. {
  559. switch (code) {
  560. case SSL_AD_CLOSE_NOTIFY:
  561. return SSL3_AD_CLOSE_NOTIFY;
  562. case SSL_AD_UNEXPECTED_MESSAGE:
  563. return SSL3_AD_UNEXPECTED_MESSAGE;
  564. case SSL_AD_BAD_RECORD_MAC:
  565. return SSL3_AD_BAD_RECORD_MAC;
  566. case SSL_AD_DECRYPTION_FAILED:
  567. return TLS1_AD_DECRYPTION_FAILED;
  568. case SSL_AD_RECORD_OVERFLOW:
  569. return TLS1_AD_RECORD_OVERFLOW;
  570. case SSL_AD_DECOMPRESSION_FAILURE:
  571. return SSL3_AD_DECOMPRESSION_FAILURE;
  572. case SSL_AD_HANDSHAKE_FAILURE:
  573. return SSL3_AD_HANDSHAKE_FAILURE;
  574. case SSL_AD_NO_CERTIFICATE:
  575. return -1;
  576. case SSL_AD_BAD_CERTIFICATE:
  577. return SSL3_AD_BAD_CERTIFICATE;
  578. case SSL_AD_UNSUPPORTED_CERTIFICATE:
  579. return SSL3_AD_UNSUPPORTED_CERTIFICATE;
  580. case SSL_AD_CERTIFICATE_REVOKED:
  581. return SSL3_AD_CERTIFICATE_REVOKED;
  582. case SSL_AD_CERTIFICATE_EXPIRED:
  583. return SSL3_AD_CERTIFICATE_EXPIRED;
  584. case SSL_AD_CERTIFICATE_UNKNOWN:
  585. return SSL3_AD_CERTIFICATE_UNKNOWN;
  586. case SSL_AD_ILLEGAL_PARAMETER:
  587. return SSL3_AD_ILLEGAL_PARAMETER;
  588. case SSL_AD_UNKNOWN_CA:
  589. return TLS1_AD_UNKNOWN_CA;
  590. case SSL_AD_ACCESS_DENIED:
  591. return TLS1_AD_ACCESS_DENIED;
  592. case SSL_AD_DECODE_ERROR:
  593. return TLS1_AD_DECODE_ERROR;
  594. case SSL_AD_DECRYPT_ERROR:
  595. return TLS1_AD_DECRYPT_ERROR;
  596. case SSL_AD_EXPORT_RESTRICTION:
  597. return TLS1_AD_EXPORT_RESTRICTION;
  598. case SSL_AD_PROTOCOL_VERSION:
  599. return TLS1_AD_PROTOCOL_VERSION;
  600. case SSL_AD_INSUFFICIENT_SECURITY:
  601. return TLS1_AD_INSUFFICIENT_SECURITY;
  602. case SSL_AD_INTERNAL_ERROR:
  603. return TLS1_AD_INTERNAL_ERROR;
  604. case SSL_AD_USER_CANCELLED:
  605. return TLS1_AD_USER_CANCELLED;
  606. case SSL_AD_NO_RENEGOTIATION:
  607. return TLS1_AD_NO_RENEGOTIATION;
  608. case SSL_AD_UNSUPPORTED_EXTENSION:
  609. return TLS1_AD_UNSUPPORTED_EXTENSION;
  610. case SSL_AD_CERTIFICATE_UNOBTAINABLE:
  611. return TLS1_AD_CERTIFICATE_UNOBTAINABLE;
  612. case SSL_AD_UNRECOGNIZED_NAME:
  613. return TLS1_AD_UNRECOGNIZED_NAME;
  614. case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
  615. return TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
  616. case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
  617. return TLS1_AD_BAD_CERTIFICATE_HASH_VALUE;
  618. case SSL_AD_UNKNOWN_PSK_IDENTITY:
  619. return TLS1_AD_UNKNOWN_PSK_IDENTITY;
  620. case SSL_AD_INAPPROPRIATE_FALLBACK:
  621. return TLS1_AD_INAPPROPRIATE_FALLBACK;
  622. case SSL_AD_NO_APPLICATION_PROTOCOL:
  623. return TLS1_AD_NO_APPLICATION_PROTOCOL;
  624. case SSL_AD_CERTIFICATE_REQUIRED:
  625. return SSL_AD_HANDSHAKE_FAILURE;
  626. default:
  627. return -1;
  628. }
  629. }