t1_enc.c 23 KB

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