t1_enc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* ====================================================================
  10. * Copyright 2005 Nokia. All rights reserved.
  11. *
  12. * The portions of the attached software ("Contribution") is developed by
  13. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  14. * license.
  15. *
  16. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  17. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  18. * support (see RFC 4279) to OpenSSL.
  19. *
  20. * No patent licenses or other rights except those expressly stated in
  21. * the OpenSSL open source license shall be deemed granted or received
  22. * expressly, by implication, estoppel, or otherwise.
  23. *
  24. * No assurances are provided by Nokia that the Contribution does not
  25. * infringe the patent or other intellectual property rights of any third
  26. * party or that the license provides you with all the necessary rights
  27. * to make use of the Contribution.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  30. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  31. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  32. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  33. * OTHERWISE.
  34. */
  35. #include <stdio.h>
  36. #include "ssl_locl.h"
  37. #include <openssl/comp.h>
  38. #include <openssl/evp.h>
  39. #include <openssl/kdf.h>
  40. #include <openssl/rand.h>
  41. /* seed1 through seed5 are concatenated */
  42. static int tls1_PRF(SSL *s,
  43. const void *seed1, size_t seed1_len,
  44. const void *seed2, size_t seed2_len,
  45. const void *seed3, size_t seed3_len,
  46. const void *seed4, size_t seed4_len,
  47. const void *seed5, size_t seed5_len,
  48. const unsigned char *sec, size_t slen,
  49. unsigned char *out, size_t olen)
  50. {
  51. const EVP_MD *md = ssl_prf_md(s);
  52. EVP_PKEY_CTX *pctx = NULL;
  53. int ret = 0;
  54. if (md == NULL) {
  55. /* Should never happen */
  56. SSLerr(SSL_F_TLS1_PRF, ERR_R_INTERNAL_ERROR);
  57. return 0;
  58. }
  59. pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_TLS1_PRF, NULL);
  60. if (pctx == NULL || EVP_PKEY_derive_init(pctx) <= 0
  61. || EVP_PKEY_CTX_set_tls1_prf_md(pctx, md) <= 0
  62. || EVP_PKEY_CTX_set1_tls1_prf_secret(pctx, sec, (int)slen) <= 0)
  63. goto err;
  64. if (EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed1, (int)seed1_len) <= 0)
  65. goto err;
  66. if (EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed2, (int)seed2_len) <= 0)
  67. goto err;
  68. if (EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed3, (int)seed3_len) <= 0)
  69. goto err;
  70. if (EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed4, (int)seed4_len) <= 0)
  71. goto err;
  72. if (EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed5, (int)seed5_len) <= 0)
  73. goto err;
  74. if (EVP_PKEY_derive(pctx, out, &olen) <= 0)
  75. goto err;
  76. ret = 1;
  77. err:
  78. EVP_PKEY_CTX_free(pctx);
  79. return ret;
  80. }
  81. static int tls1_generate_key_block(SSL *s, unsigned char *km, size_t num)
  82. {
  83. int ret;
  84. ret = tls1_PRF(s,
  85. TLS_MD_KEY_EXPANSION_CONST,
  86. TLS_MD_KEY_EXPANSION_CONST_SIZE, s->s3->server_random,
  87. SSL3_RANDOM_SIZE, s->s3->client_random, SSL3_RANDOM_SIZE,
  88. NULL, 0, NULL, 0, s->session->master_key,
  89. s->session->master_key_length, km, num);
  90. return ret;
  91. }
  92. int tls1_change_cipher_state(SSL *s, int which)
  93. {
  94. unsigned char *p, *mac_secret;
  95. unsigned char tmp1[EVP_MAX_KEY_LENGTH];
  96. unsigned char tmp2[EVP_MAX_KEY_LENGTH];
  97. unsigned char iv1[EVP_MAX_IV_LENGTH * 2];
  98. unsigned char iv2[EVP_MAX_IV_LENGTH * 2];
  99. unsigned char *ms, *key, *iv;
  100. EVP_CIPHER_CTX *dd;
  101. const EVP_CIPHER *c;
  102. #ifndef OPENSSL_NO_COMP
  103. const SSL_COMP *comp;
  104. #endif
  105. const EVP_MD *m;
  106. int mac_type;
  107. size_t *mac_secret_size;
  108. EVP_MD_CTX *mac_ctx;
  109. EVP_PKEY *mac_key;
  110. size_t n, i, j, k, cl;
  111. int reuse_dd = 0;
  112. c = s->s3->tmp.new_sym_enc;
  113. m = s->s3->tmp.new_hash;
  114. mac_type = s->s3->tmp.new_mac_pkey_type;
  115. #ifndef OPENSSL_NO_COMP
  116. comp = s->s3->tmp.new_compression;
  117. #endif
  118. if (which & SSL3_CC_READ) {
  119. if (s->ext.use_etm)
  120. s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC_READ;
  121. else
  122. s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_READ;
  123. if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
  124. s->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM;
  125. else
  126. s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM;
  127. if (s->enc_read_ctx != NULL)
  128. reuse_dd = 1;
  129. else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL)
  130. goto err;
  131. else
  132. /*
  133. * make sure it's initialised in case we exit later with an error
  134. */
  135. EVP_CIPHER_CTX_reset(s->enc_read_ctx);
  136. dd = s->enc_read_ctx;
  137. mac_ctx = ssl_replace_hash(&s->read_hash, NULL);
  138. if (mac_ctx == NULL)
  139. goto err;
  140. #ifndef OPENSSL_NO_COMP
  141. COMP_CTX_free(s->expand);
  142. s->expand = NULL;
  143. if (comp != NULL) {
  144. s->expand = COMP_CTX_new(comp->method);
  145. if (s->expand == NULL) {
  146. SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,
  147. SSL_R_COMPRESSION_LIBRARY_ERROR);
  148. goto err2;
  149. }
  150. }
  151. #endif
  152. /*
  153. * this is done by dtls1_reset_seq_numbers for DTLS
  154. */
  155. if (!SSL_IS_DTLS(s))
  156. RECORD_LAYER_reset_read_sequence(&s->rlayer);
  157. mac_secret = &(s->s3->read_mac_secret[0]);
  158. mac_secret_size = &(s->s3->read_mac_secret_size);
  159. } else {
  160. if (s->ext.use_etm)
  161. s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE;
  162. else
  163. s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE;
  164. if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
  165. s->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_STREAM;
  166. else
  167. s->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_STREAM;
  168. if (s->enc_write_ctx != NULL && !SSL_IS_DTLS(s))
  169. reuse_dd = 1;
  170. else if ((s->enc_write_ctx = EVP_CIPHER_CTX_new()) == NULL)
  171. goto err;
  172. dd = s->enc_write_ctx;
  173. if (SSL_IS_DTLS(s)) {
  174. mac_ctx = EVP_MD_CTX_new();
  175. if (mac_ctx == NULL)
  176. goto err;
  177. s->write_hash = mac_ctx;
  178. } else {
  179. mac_ctx = ssl_replace_hash(&s->write_hash, NULL);
  180. if (mac_ctx == NULL)
  181. goto err;
  182. }
  183. #ifndef OPENSSL_NO_COMP
  184. COMP_CTX_free(s->compress);
  185. s->compress = NULL;
  186. if (comp != NULL) {
  187. s->compress = COMP_CTX_new(comp->method);
  188. if (s->compress == NULL) {
  189. SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,
  190. SSL_R_COMPRESSION_LIBRARY_ERROR);
  191. goto err2;
  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. SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
  237. goto err2;
  238. }
  239. memcpy(mac_secret, ms, i);
  240. if (!(EVP_CIPHER_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
  241. /* TODO(size_t): Convert this function */
  242. mac_key = EVP_PKEY_new_mac_key(mac_type, NULL,
  243. mac_secret, (int)*mac_secret_size);
  244. if (mac_key == NULL
  245. || EVP_DigestSignInit(mac_ctx, NULL, m, NULL, mac_key) <= 0) {
  246. EVP_PKEY_free(mac_key);
  247. SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
  248. goto err2;
  249. }
  250. EVP_PKEY_free(mac_key);
  251. }
  252. #ifdef SSL_DEBUG
  253. printf("which = %04X\nmac key=", which);
  254. {
  255. size_t z;
  256. for (z = 0; z < i; z++)
  257. printf("%02X%c", ms[z], ((z + 1) % 16) ? ' ' : '\n');
  258. }
  259. #endif
  260. if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE) {
  261. if (!EVP_CipherInit_ex(dd, c, NULL, key, NULL, (which & SSL3_CC_WRITE))
  262. || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_GCM_SET_IV_FIXED, (int)k,
  263. iv)) {
  264. SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
  265. goto err2;
  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. SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
  280. goto err2;
  281. }
  282. } else {
  283. if (!EVP_CipherInit_ex(dd, c, NULL, key, iv, (which & SSL3_CC_WRITE))) {
  284. SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
  285. goto err2;
  286. }
  287. }
  288. /* Needed for "composite" AEADs, such as RC4-HMAC-MD5 */
  289. if ((EVP_CIPHER_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER) && *mac_secret_size
  290. && !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_MAC_KEY,
  291. (int)*mac_secret_size, mac_secret)) {
  292. SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
  293. goto err2;
  294. }
  295. #ifdef OPENSSL_SSL_TRACE_CRYPTO
  296. if (s->msg_callback) {
  297. int wh = which & SSL3_CC_WRITE ? TLS1_RT_CRYPTO_WRITE : 0;
  298. if (*mac_secret_size)
  299. s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_MAC,
  300. mac_secret, *mac_secret_size,
  301. s, s->msg_callback_arg);
  302. if (c->key_len)
  303. s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_KEY,
  304. key, c->key_len, s, s->msg_callback_arg);
  305. if (k) {
  306. if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE)
  307. wh |= TLS1_RT_CRYPTO_FIXED_IV;
  308. else
  309. wh |= TLS1_RT_CRYPTO_IV;
  310. s->msg_callback(2, s->version, wh, iv, k, s, s->msg_callback_arg);
  311. }
  312. }
  313. #endif
  314. #ifdef SSL_DEBUG
  315. printf("which = %04X\nkey=", which);
  316. {
  317. int z;
  318. for (z = 0; z < EVP_CIPHER_key_length(c); z++)
  319. printf("%02X%c", key[z], ((z + 1) % 16) ? ' ' : '\n');
  320. }
  321. printf("\niv=");
  322. {
  323. size_t z;
  324. for (z = 0; z < k; z++)
  325. printf("%02X%c", iv[z], ((z + 1) % 16) ? ' ' : '\n');
  326. }
  327. printf("\n");
  328. #endif
  329. OPENSSL_cleanse(tmp1, sizeof(tmp1));
  330. OPENSSL_cleanse(tmp2, sizeof(tmp1));
  331. OPENSSL_cleanse(iv1, sizeof(iv1));
  332. OPENSSL_cleanse(iv2, sizeof(iv2));
  333. return (1);
  334. err:
  335. SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
  336. err2:
  337. OPENSSL_cleanse(tmp1, sizeof(tmp1));
  338. OPENSSL_cleanse(tmp2, sizeof(tmp1));
  339. OPENSSL_cleanse(iv1, sizeof(iv1));
  340. OPENSSL_cleanse(iv2, sizeof(iv2));
  341. return (0);
  342. }
  343. int tls1_setup_key_block(SSL *s)
  344. {
  345. unsigned char *p;
  346. const EVP_CIPHER *c;
  347. const EVP_MD *hash;
  348. SSL_COMP *comp;
  349. int mac_type = NID_undef;
  350. size_t num, mac_secret_size = 0;
  351. int ret = 0;
  352. if (s->s3->tmp.key_block_length != 0)
  353. return (1);
  354. if (!ssl_cipher_get_evp(s->session, &c, &hash, &mac_type, &mac_secret_size,
  355. &comp, s->ext.use_etm)) {
  356. SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
  357. return (0);
  358. }
  359. s->s3->tmp.new_sym_enc = c;
  360. s->s3->tmp.new_hash = hash;
  361. s->s3->tmp.new_mac_pkey_type = mac_type;
  362. s->s3->tmp.new_mac_secret_size = mac_secret_size;
  363. num = EVP_CIPHER_key_length(c) + mac_secret_size + EVP_CIPHER_iv_length(c);
  364. num *= 2;
  365. ssl3_cleanup_key_block(s);
  366. if ((p = OPENSSL_malloc(num)) == NULL) {
  367. SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK, ERR_R_MALLOC_FAILURE);
  368. goto err;
  369. }
  370. s->s3->tmp.key_block_length = num;
  371. s->s3->tmp.key_block = p;
  372. #ifdef SSL_DEBUG
  373. printf("client random\n");
  374. {
  375. int z;
  376. for (z = 0; z < SSL3_RANDOM_SIZE; z++)
  377. printf("%02X%c", s->s3->client_random[z],
  378. ((z + 1) % 16) ? ' ' : '\n');
  379. }
  380. printf("server random\n");
  381. {
  382. int z;
  383. for (z = 0; z < SSL3_RANDOM_SIZE; z++)
  384. printf("%02X%c", s->s3->server_random[z],
  385. ((z + 1) % 16) ? ' ' : '\n');
  386. }
  387. printf("master key\n");
  388. {
  389. size_t z;
  390. for (z = 0; z < s->session->master_key_length; z++)
  391. printf("%02X%c", s->session->master_key[z],
  392. ((z + 1) % 16) ? ' ' : '\n');
  393. }
  394. #endif
  395. if (!tls1_generate_key_block(s, p, num))
  396. goto err;
  397. #ifdef SSL_DEBUG
  398. printf("\nkey block\n");
  399. {
  400. size_t z;
  401. for (z = 0; z < num; z++)
  402. printf("%02X%c", p[z], ((z + 1) % 16) ? ' ' : '\n');
  403. }
  404. #endif
  405. if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)
  406. && s->method->version <= TLS1_VERSION) {
  407. /*
  408. * enable vulnerability countermeasure for CBC ciphers with known-IV
  409. * problem (http://www.openssl.org/~bodo/tls-cbc.txt)
  410. */
  411. s->s3->need_empty_fragments = 1;
  412. if (s->session->cipher != NULL) {
  413. if (s->session->cipher->algorithm_enc == SSL_eNULL)
  414. s->s3->need_empty_fragments = 0;
  415. #ifndef OPENSSL_NO_RC4
  416. if (s->session->cipher->algorithm_enc == SSL_RC4)
  417. s->s3->need_empty_fragments = 0;
  418. #endif
  419. }
  420. }
  421. ret = 1;
  422. err:
  423. return (ret);
  424. }
  425. size_t tls1_final_finish_mac(SSL *s, const char *str, size_t slen,
  426. unsigned char *out)
  427. {
  428. size_t hashlen;
  429. unsigned char hash[EVP_MAX_MD_SIZE];
  430. if (!ssl3_digest_cached_records(s, 0))
  431. return 0;
  432. if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen))
  433. return 0;
  434. if (!tls1_PRF(s, str, slen, hash, hashlen, NULL, 0, NULL, 0, NULL, 0,
  435. s->session->master_key, s->session->master_key_length,
  436. out, TLS1_FINISH_MAC_LENGTH))
  437. return 0;
  438. OPENSSL_cleanse(hash, hashlen);
  439. return TLS1_FINISH_MAC_LENGTH;
  440. }
  441. int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
  442. size_t len, size_t *secret_size)
  443. {
  444. if (s->session->flags & SSL_SESS_FLAG_EXTMS) {
  445. unsigned char hash[EVP_MAX_MD_SIZE * 2];
  446. size_t hashlen;
  447. /*
  448. * Digest cached records keeping record buffer (if present): this wont
  449. * affect client auth because we're freezing the buffer at the same
  450. * point (after client key exchange and before certificate verify)
  451. */
  452. if (!ssl3_digest_cached_records(s, 1))
  453. return 0;
  454. if(!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen))
  455. return 0;
  456. #ifdef SSL_DEBUG
  457. fprintf(stderr, "Handshake hashes:\n");
  458. BIO_dump_fp(stderr, (char *)hash, hashlen);
  459. #endif
  460. tls1_PRF(s,
  461. TLS_MD_EXTENDED_MASTER_SECRET_CONST,
  462. TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE,
  463. hash, hashlen,
  464. NULL, 0,
  465. NULL, 0,
  466. NULL, 0, p, len, s->session->master_key,
  467. SSL3_MASTER_SECRET_SIZE);
  468. OPENSSL_cleanse(hash, hashlen);
  469. } else {
  470. 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, s->session->master_key,
  477. SSL3_MASTER_SECRET_SIZE);
  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. #ifdef OPENSSL_SSL_TRACE_CRYPTO
  491. if (s->msg_callback) {
  492. s->msg_callback(2, s->version, TLS1_RT_CRYPTO_PREMASTER,
  493. p, len, s, s->msg_callback_arg);
  494. s->msg_callback(2, s->version, TLS1_RT_CRYPTO_CLIENT_RANDOM,
  495. s->s3->client_random, SSL3_RANDOM_SIZE,
  496. s, s->msg_callback_arg);
  497. s->msg_callback(2, s->version, TLS1_RT_CRYPTO_SERVER_RANDOM,
  498. s->s3->server_random, SSL3_RANDOM_SIZE,
  499. s, s->msg_callback_arg);
  500. s->msg_callback(2, s->version, TLS1_RT_CRYPTO_MASTER,
  501. s->session->master_key,
  502. SSL3_MASTER_SECRET_SIZE, s, s->msg_callback_arg);
  503. }
  504. #endif
  505. *secret_size = SSL3_MASTER_SECRET_SIZE;
  506. return 1;
  507. }
  508. int tls1_export_keying_material(SSL *s, unsigned char *out, size_t olen,
  509. const char *label, size_t llen,
  510. const unsigned char *context,
  511. size_t contextlen, int use_context)
  512. {
  513. unsigned char *val = NULL;
  514. size_t vallen = 0, currentvalpos;
  515. int rv;
  516. /*
  517. * construct PRF arguments we construct the PRF argument ourself rather
  518. * than passing separate values into the TLS PRF to ensure that the
  519. * concatenation of values does not create a prohibited label.
  520. */
  521. vallen = llen + SSL3_RANDOM_SIZE * 2;
  522. if (use_context) {
  523. vallen += 2 + contextlen;
  524. }
  525. val = OPENSSL_malloc(vallen);
  526. if (val == NULL)
  527. goto err2;
  528. currentvalpos = 0;
  529. memcpy(val + currentvalpos, (unsigned char *)label, llen);
  530. currentvalpos += llen;
  531. memcpy(val + currentvalpos, s->s3->client_random, SSL3_RANDOM_SIZE);
  532. currentvalpos += SSL3_RANDOM_SIZE;
  533. memcpy(val + currentvalpos, s->s3->server_random, SSL3_RANDOM_SIZE);
  534. currentvalpos += SSL3_RANDOM_SIZE;
  535. if (use_context) {
  536. val[currentvalpos] = (contextlen >> 8) & 0xff;
  537. currentvalpos++;
  538. val[currentvalpos] = contextlen & 0xff;
  539. currentvalpos++;
  540. if ((contextlen > 0) || (context != NULL)) {
  541. memcpy(val + currentvalpos, context, contextlen);
  542. }
  543. }
  544. /*
  545. * disallow prohibited labels note that SSL3_RANDOM_SIZE > max(prohibited
  546. * label len) = 15, so size of val > max(prohibited label len) = 15 and
  547. * the comparisons won't have buffer overflow
  548. */
  549. if (memcmp(val, TLS_MD_CLIENT_FINISH_CONST,
  550. TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0)
  551. goto err1;
  552. if (memcmp(val, TLS_MD_SERVER_FINISH_CONST,
  553. TLS_MD_SERVER_FINISH_CONST_SIZE) == 0)
  554. goto err1;
  555. if (memcmp(val, TLS_MD_MASTER_SECRET_CONST,
  556. TLS_MD_MASTER_SECRET_CONST_SIZE) == 0)
  557. goto err1;
  558. if (memcmp(val, TLS_MD_EXTENDED_MASTER_SECRET_CONST,
  559. TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE) == 0)
  560. goto err1;
  561. if (memcmp(val, TLS_MD_KEY_EXPANSION_CONST,
  562. TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0)
  563. goto err1;
  564. rv = tls1_PRF(s,
  565. val, vallen,
  566. NULL, 0,
  567. NULL, 0,
  568. NULL, 0,
  569. NULL, 0,
  570. s->session->master_key, s->session->master_key_length,
  571. out, olen);
  572. goto ret;
  573. err1:
  574. SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
  575. rv = 0;
  576. goto ret;
  577. err2:
  578. SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, ERR_R_MALLOC_FAILURE);
  579. rv = 0;
  580. ret:
  581. OPENSSL_clear_free(val, vallen);
  582. return (rv);
  583. }
  584. int tls1_alert_code(int code)
  585. {
  586. switch (code) {
  587. case SSL_AD_CLOSE_NOTIFY:
  588. return (SSL3_AD_CLOSE_NOTIFY);
  589. case SSL_AD_UNEXPECTED_MESSAGE:
  590. return (SSL3_AD_UNEXPECTED_MESSAGE);
  591. case SSL_AD_BAD_RECORD_MAC:
  592. return (SSL3_AD_BAD_RECORD_MAC);
  593. case SSL_AD_DECRYPTION_FAILED:
  594. return (TLS1_AD_DECRYPTION_FAILED);
  595. case SSL_AD_RECORD_OVERFLOW:
  596. return (TLS1_AD_RECORD_OVERFLOW);
  597. case SSL_AD_DECOMPRESSION_FAILURE:
  598. return (SSL3_AD_DECOMPRESSION_FAILURE);
  599. case SSL_AD_HANDSHAKE_FAILURE:
  600. return (SSL3_AD_HANDSHAKE_FAILURE);
  601. case SSL_AD_NO_CERTIFICATE:
  602. return (-1);
  603. case SSL_AD_BAD_CERTIFICATE:
  604. return (SSL3_AD_BAD_CERTIFICATE);
  605. case SSL_AD_UNSUPPORTED_CERTIFICATE:
  606. return (SSL3_AD_UNSUPPORTED_CERTIFICATE);
  607. case SSL_AD_CERTIFICATE_REVOKED:
  608. return (SSL3_AD_CERTIFICATE_REVOKED);
  609. case SSL_AD_CERTIFICATE_EXPIRED:
  610. return (SSL3_AD_CERTIFICATE_EXPIRED);
  611. case SSL_AD_CERTIFICATE_UNKNOWN:
  612. return (SSL3_AD_CERTIFICATE_UNKNOWN);
  613. case SSL_AD_ILLEGAL_PARAMETER:
  614. return (SSL3_AD_ILLEGAL_PARAMETER);
  615. case SSL_AD_UNKNOWN_CA:
  616. return (TLS1_AD_UNKNOWN_CA);
  617. case SSL_AD_ACCESS_DENIED:
  618. return (TLS1_AD_ACCESS_DENIED);
  619. case SSL_AD_DECODE_ERROR:
  620. return (TLS1_AD_DECODE_ERROR);
  621. case SSL_AD_DECRYPT_ERROR:
  622. return (TLS1_AD_DECRYPT_ERROR);
  623. case SSL_AD_EXPORT_RESTRICTION:
  624. return (TLS1_AD_EXPORT_RESTRICTION);
  625. case SSL_AD_PROTOCOL_VERSION:
  626. return (TLS1_AD_PROTOCOL_VERSION);
  627. case SSL_AD_INSUFFICIENT_SECURITY:
  628. return (TLS1_AD_INSUFFICIENT_SECURITY);
  629. case SSL_AD_INTERNAL_ERROR:
  630. return (TLS1_AD_INTERNAL_ERROR);
  631. case SSL_AD_USER_CANCELLED:
  632. return (TLS1_AD_USER_CANCELLED);
  633. case SSL_AD_NO_RENEGOTIATION:
  634. return (TLS1_AD_NO_RENEGOTIATION);
  635. case SSL_AD_UNSUPPORTED_EXTENSION:
  636. return (TLS1_AD_UNSUPPORTED_EXTENSION);
  637. case SSL_AD_CERTIFICATE_UNOBTAINABLE:
  638. return (TLS1_AD_CERTIFICATE_UNOBTAINABLE);
  639. case SSL_AD_UNRECOGNIZED_NAME:
  640. return (TLS1_AD_UNRECOGNIZED_NAME);
  641. case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
  642. return (TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE);
  643. case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
  644. return (TLS1_AD_BAD_CERTIFICATE_HASH_VALUE);
  645. case SSL_AD_UNKNOWN_PSK_IDENTITY:
  646. return (TLS1_AD_UNKNOWN_PSK_IDENTITY);
  647. case SSL_AD_INAPPROPRIATE_FALLBACK:
  648. return (TLS1_AD_INAPPROPRIATE_FALLBACK);
  649. case SSL_AD_NO_APPLICATION_PROTOCOL:
  650. return (TLS1_AD_NO_APPLICATION_PROTOCOL);
  651. case SSL_AD_CERTIFICATE_REQUIRED:
  652. return SSL_AD_HANDSHAKE_FAILURE;
  653. default:
  654. return (-1);
  655. }
  656. }