2
0

t1_enc.c 23 KB

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