t1_enc.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2005 Nokia. All rights reserved.
  112. *
  113. * The portions of the attached software ("Contribution") is developed by
  114. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  115. * license.
  116. *
  117. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  118. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  119. * support (see RFC 4279) to OpenSSL.
  120. *
  121. * No patent licenses or other rights except those expressly stated in
  122. * the OpenSSL open source license shall be deemed granted or received
  123. * expressly, by implication, estoppel, or otherwise.
  124. *
  125. * No assurances are provided by Nokia that the Contribution does not
  126. * infringe the patent or other intellectual property rights of any third
  127. * party or that the license provides you with all the necessary rights
  128. * to make use of the Contribution.
  129. *
  130. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  131. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  132. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  133. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  134. * OTHERWISE. */
  135. #include <openssl/ssl.h>
  136. #include <assert.h>
  137. #include <string.h>
  138. #include <utility>
  139. #include <openssl/err.h>
  140. #include <openssl/evp.h>
  141. #include <openssl/hmac.h>
  142. #include <openssl/md5.h>
  143. #include <openssl/mem.h>
  144. #include <openssl/nid.h>
  145. #include <openssl/rand.h>
  146. #include "../crypto/fipsmodule/tls/internal.h"
  147. #include "../crypto/internal.h"
  148. #include "internal.h"
  149. BSSL_NAMESPACE_BEGIN
  150. bool tls1_prf(const EVP_MD *digest, Span<uint8_t> out,
  151. Span<const uint8_t> secret, Span<const char> label,
  152. Span<const uint8_t> seed1, Span<const uint8_t> seed2) {
  153. return 1 == CRYPTO_tls1_prf(digest, out.data(), out.size(), secret.data(),
  154. secret.size(), label.data(), label.size(),
  155. seed1.data(), seed1.size(), seed2.data(),
  156. seed2.size());
  157. }
  158. static bool get_key_block_lengths(const SSL *ssl, size_t *out_mac_secret_len,
  159. size_t *out_key_len, size_t *out_iv_len,
  160. const SSL_CIPHER *cipher) {
  161. const EVP_AEAD *aead = NULL;
  162. if (!ssl_cipher_get_evp_aead(&aead, out_mac_secret_len, out_iv_len, cipher,
  163. ssl_protocol_version(ssl), SSL_is_dtls(ssl))) {
  164. OPENSSL_PUT_ERROR(SSL, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
  165. return false;
  166. }
  167. *out_key_len = EVP_AEAD_key_length(aead);
  168. if (*out_mac_secret_len > 0) {
  169. // For "stateful" AEADs (i.e. compatibility with pre-AEAD cipher suites) the
  170. // key length reported by |EVP_AEAD_key_length| will include the MAC key
  171. // bytes and initial implicit IV.
  172. if (*out_key_len < *out_mac_secret_len + *out_iv_len) {
  173. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  174. return false;
  175. }
  176. *out_key_len -= *out_mac_secret_len + *out_iv_len;
  177. }
  178. return true;
  179. }
  180. int tls1_configure_aead(SSL *ssl, evp_aead_direction_t direction,
  181. Array<uint8_t> *key_block_cache,
  182. const SSL_CIPHER *cipher,
  183. Span<const uint8_t> iv_override) {
  184. size_t mac_secret_len, key_len, iv_len;
  185. if (!get_key_block_lengths(ssl, &mac_secret_len, &key_len, &iv_len, cipher)) {
  186. return 0;
  187. }
  188. // Ensure that |key_block_cache| is set up.
  189. const size_t key_block_size = 2 * (mac_secret_len + key_len + iv_len);
  190. if (key_block_cache->empty()) {
  191. if (!key_block_cache->Init(key_block_size) ||
  192. !SSL_generate_key_block(ssl, key_block_cache->data(), key_block_size)) {
  193. return 0;
  194. }
  195. }
  196. assert(key_block_cache->size() == key_block_size);
  197. Span<const uint8_t> key_block = *key_block_cache;
  198. Span<const uint8_t> mac_secret, key, iv;
  199. if (direction == (ssl->server ? evp_aead_open : evp_aead_seal)) {
  200. // Use the client write (server read) keys.
  201. mac_secret = key_block.subspan(0, mac_secret_len);
  202. key = key_block.subspan(2 * mac_secret_len, key_len);
  203. iv = key_block.subspan(2 * mac_secret_len + 2 * key_len, iv_len);
  204. } else {
  205. // Use the server write (client read) keys.
  206. mac_secret = key_block.subspan(mac_secret_len, mac_secret_len);
  207. key = key_block.subspan(2 * mac_secret_len + key_len, key_len);
  208. iv = key_block.subspan(2 * mac_secret_len + 2 * key_len + iv_len, iv_len);
  209. }
  210. if (!iv_override.empty()) {
  211. if (iv_override.size() != iv_len) {
  212. return 0;
  213. }
  214. iv = iv_override;
  215. }
  216. UniquePtr<SSLAEADContext> aead_ctx = SSLAEADContext::Create(
  217. direction, ssl->version, SSL_is_dtls(ssl), cipher, key, mac_secret, iv);
  218. if (!aead_ctx) {
  219. return 0;
  220. }
  221. if (direction == evp_aead_open) {
  222. return ssl->method->set_read_state(ssl, std::move(aead_ctx));
  223. }
  224. return ssl->method->set_write_state(ssl, std::move(aead_ctx));
  225. }
  226. int tls1_change_cipher_state(SSL_HANDSHAKE *hs,
  227. evp_aead_direction_t direction) {
  228. return tls1_configure_aead(hs->ssl, direction, &hs->key_block,
  229. hs->new_cipher, {});
  230. }
  231. int tls1_generate_master_secret(SSL_HANDSHAKE *hs, uint8_t *out,
  232. Span<const uint8_t> premaster) {
  233. static const char kMasterSecretLabel[] = "master secret";
  234. static const char kExtendedMasterSecretLabel[] = "extended master secret";
  235. const SSL *ssl = hs->ssl;
  236. auto out_span = MakeSpan(out, SSL3_MASTER_SECRET_SIZE);
  237. if (hs->extended_master_secret) {
  238. auto label = MakeConstSpan(kExtendedMasterSecretLabel,
  239. sizeof(kExtendedMasterSecretLabel) - 1);
  240. uint8_t digests[EVP_MAX_MD_SIZE];
  241. size_t digests_len;
  242. if (!hs->transcript.GetHash(digests, &digests_len) ||
  243. !tls1_prf(hs->transcript.Digest(), out_span, premaster, label,
  244. MakeConstSpan(digests, digests_len), {})) {
  245. return 0;
  246. }
  247. } else {
  248. auto label =
  249. MakeConstSpan(kMasterSecretLabel, sizeof(kMasterSecretLabel) - 1);
  250. if (!tls1_prf(hs->transcript.Digest(), out_span, premaster, label,
  251. ssl->s3->client_random, ssl->s3->server_random)) {
  252. return 0;
  253. }
  254. }
  255. return SSL3_MASTER_SECRET_SIZE;
  256. }
  257. BSSL_NAMESPACE_END
  258. using namespace bssl;
  259. size_t SSL_get_key_block_len(const SSL *ssl) {
  260. size_t mac_secret_len, key_len, fixed_iv_len;
  261. if (!get_key_block_lengths(ssl, &mac_secret_len, &key_len, &fixed_iv_len,
  262. SSL_get_current_cipher(ssl))) {
  263. ERR_clear_error();
  264. return 0;
  265. }
  266. return 2 * (mac_secret_len + key_len + fixed_iv_len);
  267. }
  268. int SSL_generate_key_block(const SSL *ssl, uint8_t *out, size_t out_len) {
  269. const SSL_SESSION *session = SSL_get_session(ssl);
  270. auto out_span = MakeSpan(out, out_len);
  271. auto master_key =
  272. MakeConstSpan(session->master_key, session->master_key_length);
  273. static const char kLabel[] = "key expansion";
  274. auto label = MakeConstSpan(kLabel, sizeof(kLabel) - 1);
  275. const EVP_MD *digest = ssl_session_get_digest(session);
  276. return tls1_prf(digest, out_span, master_key, label, ssl->s3->server_random,
  277. ssl->s3->client_random);
  278. }
  279. int SSL_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len,
  280. const char *label, size_t label_len,
  281. const uint8_t *context, size_t context_len,
  282. int use_context) {
  283. // Exporters may be used in False Start and server 0-RTT, where the handshake
  284. // has progressed enough. Otherwise, they may not be used during a handshake.
  285. if (SSL_in_init(ssl) &&
  286. !SSL_in_false_start(ssl) &&
  287. !(SSL_is_server(ssl) && SSL_in_early_data(ssl))) {
  288. OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_NOT_COMPLETE);
  289. return 0;
  290. }
  291. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  292. if (!use_context) {
  293. context = nullptr;
  294. context_len = 0;
  295. }
  296. return tls13_export_keying_material(
  297. ssl, MakeSpan(out, out_len),
  298. MakeConstSpan(ssl->s3->exporter_secret, ssl->s3->exporter_secret_len),
  299. MakeConstSpan(label, label_len), MakeConstSpan(context, context_len));
  300. }
  301. size_t seed_len = 2 * SSL3_RANDOM_SIZE;
  302. if (use_context) {
  303. if (context_len >= 1u << 16) {
  304. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  305. return 0;
  306. }
  307. seed_len += 2 + context_len;
  308. }
  309. Array<uint8_t> seed;
  310. if (!seed.Init(seed_len)) {
  311. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  312. return 0;
  313. }
  314. OPENSSL_memcpy(seed.data(), ssl->s3->client_random, SSL3_RANDOM_SIZE);
  315. OPENSSL_memcpy(seed.data() + SSL3_RANDOM_SIZE, ssl->s3->server_random,
  316. SSL3_RANDOM_SIZE);
  317. if (use_context) {
  318. seed[2 * SSL3_RANDOM_SIZE] = static_cast<uint8_t>(context_len >> 8);
  319. seed[2 * SSL3_RANDOM_SIZE + 1] = static_cast<uint8_t>(context_len);
  320. OPENSSL_memcpy(seed.data() + 2 * SSL3_RANDOM_SIZE + 2, context, context_len);
  321. }
  322. const SSL_SESSION *session = SSL_get_session(ssl);
  323. const EVP_MD *digest = ssl_session_get_digest(session);
  324. return tls1_prf(
  325. digest, MakeSpan(out, out_len),
  326. MakeConstSpan(session->master_key, session->master_key_length),
  327. MakeConstSpan(label, label_len), seed, {});
  328. }