Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

366 rader
12 KiB

  1. /* Copyright (c) 2015, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/ssl.h>
  15. #include <assert.h>
  16. #include <string.h>
  17. #include <openssl/aead.h>
  18. #include <openssl/err.h>
  19. #include <openssl/rand.h>
  20. #include "../crypto/internal.h"
  21. #include "internal.h"
  22. SSL_AEAD_CTX *SSL_AEAD_CTX_new(enum evp_aead_direction_t direction,
  23. uint16_t version, int is_dtls,
  24. const SSL_CIPHER *cipher, const uint8_t *enc_key,
  25. size_t enc_key_len, const uint8_t *mac_key,
  26. size_t mac_key_len, const uint8_t *fixed_iv,
  27. size_t fixed_iv_len) {
  28. const EVP_AEAD *aead;
  29. size_t expected_mac_key_len, expected_fixed_iv_len;
  30. if (!ssl_cipher_get_evp_aead(&aead, &expected_mac_key_len,
  31. &expected_fixed_iv_len, cipher, version,
  32. is_dtls) ||
  33. /* Ensure the caller returned correct key sizes. */
  34. expected_fixed_iv_len != fixed_iv_len ||
  35. expected_mac_key_len != mac_key_len) {
  36. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  37. return 0;
  38. }
  39. uint8_t merged_key[EVP_AEAD_MAX_KEY_LENGTH];
  40. if (mac_key_len > 0) {
  41. /* This is a "stateful" AEAD (for compatibility with pre-AEAD cipher
  42. * suites). */
  43. if (mac_key_len + enc_key_len + fixed_iv_len > sizeof(merged_key)) {
  44. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  45. return 0;
  46. }
  47. OPENSSL_memcpy(merged_key, mac_key, mac_key_len);
  48. OPENSSL_memcpy(merged_key + mac_key_len, enc_key, enc_key_len);
  49. OPENSSL_memcpy(merged_key + mac_key_len + enc_key_len, fixed_iv,
  50. fixed_iv_len);
  51. enc_key = merged_key;
  52. enc_key_len += mac_key_len;
  53. enc_key_len += fixed_iv_len;
  54. }
  55. SSL_AEAD_CTX *aead_ctx = (SSL_AEAD_CTX *)OPENSSL_malloc(sizeof(SSL_AEAD_CTX));
  56. if (aead_ctx == NULL) {
  57. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  58. return NULL;
  59. }
  60. OPENSSL_memset(aead_ctx, 0, sizeof(SSL_AEAD_CTX));
  61. aead_ctx->cipher = cipher;
  62. aead_ctx->version = version;
  63. if (!EVP_AEAD_CTX_init_with_direction(
  64. &aead_ctx->ctx, aead, enc_key, enc_key_len,
  65. EVP_AEAD_DEFAULT_TAG_LENGTH, direction)) {
  66. OPENSSL_free(aead_ctx);
  67. return NULL;
  68. }
  69. assert(EVP_AEAD_nonce_length(aead) <= EVP_AEAD_MAX_NONCE_LENGTH);
  70. static_assert(EVP_AEAD_MAX_NONCE_LENGTH < 256,
  71. "variable_nonce_len doesn't fit in uint8_t");
  72. aead_ctx->variable_nonce_len = (uint8_t)EVP_AEAD_nonce_length(aead);
  73. if (mac_key_len == 0) {
  74. assert(fixed_iv_len <= sizeof(aead_ctx->fixed_nonce));
  75. OPENSSL_memcpy(aead_ctx->fixed_nonce, fixed_iv, fixed_iv_len);
  76. aead_ctx->fixed_nonce_len = fixed_iv_len;
  77. if (cipher->algorithm_enc & SSL_CHACHA20POLY1305) {
  78. /* The fixed nonce into the actual nonce (the sequence number). */
  79. aead_ctx->xor_fixed_nonce = 1;
  80. aead_ctx->variable_nonce_len = 8;
  81. } else {
  82. /* The fixed IV is prepended to the nonce. */
  83. assert(fixed_iv_len <= aead_ctx->variable_nonce_len);
  84. aead_ctx->variable_nonce_len -= fixed_iv_len;
  85. }
  86. /* AES-GCM uses an explicit nonce. */
  87. if (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) {
  88. aead_ctx->variable_nonce_included_in_record = 1;
  89. }
  90. /* The TLS 1.3 construction XORs the fixed nonce into the sequence number
  91. * and omits the additional data. */
  92. if (version >= TLS1_3_VERSION) {
  93. aead_ctx->xor_fixed_nonce = 1;
  94. aead_ctx->variable_nonce_len = 8;
  95. aead_ctx->variable_nonce_included_in_record = 0;
  96. aead_ctx->omit_ad = 1;
  97. assert(fixed_iv_len >= aead_ctx->variable_nonce_len);
  98. }
  99. } else {
  100. assert(version < TLS1_3_VERSION);
  101. aead_ctx->variable_nonce_included_in_record = 1;
  102. aead_ctx->random_variable_nonce = 1;
  103. aead_ctx->omit_length_in_ad = 1;
  104. aead_ctx->omit_version_in_ad = (version == SSL3_VERSION);
  105. }
  106. return aead_ctx;
  107. }
  108. void SSL_AEAD_CTX_free(SSL_AEAD_CTX *aead) {
  109. if (aead == NULL) {
  110. return;
  111. }
  112. EVP_AEAD_CTX_cleanup(&aead->ctx);
  113. OPENSSL_free(aead);
  114. }
  115. size_t SSL_AEAD_CTX_explicit_nonce_len(const SSL_AEAD_CTX *aead) {
  116. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  117. aead = NULL;
  118. #endif
  119. if (aead != NULL && aead->variable_nonce_included_in_record) {
  120. return aead->variable_nonce_len;
  121. }
  122. return 0;
  123. }
  124. size_t SSL_AEAD_CTX_max_suffix_len(const SSL_AEAD_CTX *aead,
  125. size_t extra_in_len) {
  126. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  127. aead = NULL;
  128. #endif
  129. return extra_in_len +
  130. (aead == NULL ? 0 : EVP_AEAD_max_overhead(aead->ctx.aead));
  131. }
  132. size_t SSL_AEAD_CTX_max_overhead(const SSL_AEAD_CTX *aead) {
  133. return SSL_AEAD_CTX_explicit_nonce_len(aead) +
  134. SSL_AEAD_CTX_max_suffix_len(aead, 0);
  135. }
  136. /* ssl_aead_ctx_get_ad writes the additional data for |aead| into |out| and
  137. * returns the number of bytes written. */
  138. static size_t ssl_aead_ctx_get_ad(SSL_AEAD_CTX *aead, uint8_t out[13],
  139. uint8_t type, uint16_t wire_version,
  140. const uint8_t seqnum[8],
  141. size_t plaintext_len) {
  142. if (aead->omit_ad) {
  143. return 0;
  144. }
  145. OPENSSL_memcpy(out, seqnum, 8);
  146. size_t len = 8;
  147. out[len++] = type;
  148. if (!aead->omit_version_in_ad) {
  149. out[len++] = (uint8_t)(wire_version >> 8);
  150. out[len++] = (uint8_t)wire_version;
  151. }
  152. if (!aead->omit_length_in_ad) {
  153. out[len++] = (uint8_t)(plaintext_len >> 8);
  154. out[len++] = (uint8_t)plaintext_len;
  155. }
  156. return len;
  157. }
  158. int SSL_AEAD_CTX_open(SSL_AEAD_CTX *aead, CBS *out, uint8_t type,
  159. uint16_t wire_version, const uint8_t seqnum[8],
  160. uint8_t *in, size_t in_len) {
  161. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  162. aead = NULL;
  163. #endif
  164. if (aead == NULL) {
  165. /* Handle the initial NULL cipher. */
  166. CBS_init(out, in, in_len);
  167. return 1;
  168. }
  169. /* TLS 1.2 AEADs include the length in the AD and are assumed to have fixed
  170. * overhead. Otherwise the parameter is unused. */
  171. size_t plaintext_len = 0;
  172. if (!aead->omit_length_in_ad) {
  173. size_t overhead = SSL_AEAD_CTX_max_overhead(aead);
  174. if (in_len < overhead) {
  175. /* Publicly invalid. */
  176. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
  177. return 0;
  178. }
  179. plaintext_len = in_len - overhead;
  180. }
  181. uint8_t ad[13];
  182. size_t ad_len = ssl_aead_ctx_get_ad(aead, ad, type, wire_version, seqnum,
  183. plaintext_len);
  184. /* Assemble the nonce. */
  185. uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
  186. size_t nonce_len = 0;
  187. /* Prepend the fixed nonce, or left-pad with zeros if XORing. */
  188. if (aead->xor_fixed_nonce) {
  189. nonce_len = aead->fixed_nonce_len - aead->variable_nonce_len;
  190. OPENSSL_memset(nonce, 0, nonce_len);
  191. } else {
  192. OPENSSL_memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
  193. nonce_len += aead->fixed_nonce_len;
  194. }
  195. /* Add the variable nonce. */
  196. if (aead->variable_nonce_included_in_record) {
  197. if (in_len < aead->variable_nonce_len) {
  198. /* Publicly invalid. */
  199. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
  200. return 0;
  201. }
  202. OPENSSL_memcpy(nonce + nonce_len, in, aead->variable_nonce_len);
  203. in += aead->variable_nonce_len;
  204. in_len -= aead->variable_nonce_len;
  205. } else {
  206. assert(aead->variable_nonce_len == 8);
  207. OPENSSL_memcpy(nonce + nonce_len, seqnum, aead->variable_nonce_len);
  208. }
  209. nonce_len += aead->variable_nonce_len;
  210. /* XOR the fixed nonce, if necessary. */
  211. if (aead->xor_fixed_nonce) {
  212. assert(nonce_len == aead->fixed_nonce_len);
  213. for (size_t i = 0; i < aead->fixed_nonce_len; i++) {
  214. nonce[i] ^= aead->fixed_nonce[i];
  215. }
  216. }
  217. /* Decrypt in-place. */
  218. size_t len;
  219. if (!EVP_AEAD_CTX_open(&aead->ctx, in, &len, in_len, nonce, nonce_len,
  220. in, in_len, ad, ad_len)) {
  221. return 0;
  222. }
  223. CBS_init(out, in, len);
  224. return 1;
  225. }
  226. int SSL_AEAD_CTX_seal_scatter(SSL_AEAD_CTX *aead, uint8_t *out_prefix,
  227. uint8_t *out, uint8_t *out_suffix,
  228. size_t *out_suffix_len, size_t max_out_suffix_len,
  229. uint8_t type, uint16_t wire_version,
  230. const uint8_t seqnum[8], const uint8_t *in,
  231. size_t in_len, const uint8_t *extra_in,
  232. size_t extra_in_len) {
  233. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  234. aead = NULL;
  235. #endif
  236. if ((in != out && buffers_alias(in, in_len, out, in_len)) ||
  237. buffers_alias(in, in_len, out_suffix, max_out_suffix_len)) {
  238. OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
  239. return 0;
  240. }
  241. if (extra_in_len > max_out_suffix_len) {
  242. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
  243. return 0;
  244. }
  245. if (aead == NULL) {
  246. /* Handle the initial NULL cipher. */
  247. OPENSSL_memmove(out, in, in_len);
  248. OPENSSL_memmove(out_suffix, extra_in, extra_in_len);
  249. *out_suffix_len = extra_in_len;
  250. return 1;
  251. }
  252. uint8_t ad[13];
  253. size_t ad_len = ssl_aead_ctx_get_ad(aead, ad, type, wire_version, seqnum,
  254. in_len);
  255. /* Assemble the nonce. */
  256. uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
  257. size_t nonce_len = 0;
  258. /* Prepend the fixed nonce, or left-pad with zeros if XORing. */
  259. if (aead->xor_fixed_nonce) {
  260. nonce_len = aead->fixed_nonce_len - aead->variable_nonce_len;
  261. OPENSSL_memset(nonce, 0, nonce_len);
  262. } else {
  263. OPENSSL_memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
  264. nonce_len += aead->fixed_nonce_len;
  265. }
  266. /* Select the variable nonce. */
  267. if (aead->random_variable_nonce) {
  268. assert(aead->variable_nonce_included_in_record);
  269. if (!RAND_bytes(nonce + nonce_len, aead->variable_nonce_len)) {
  270. return 0;
  271. }
  272. } else {
  273. /* When sending we use the sequence number as the variable part of the
  274. * nonce. */
  275. assert(aead->variable_nonce_len == 8);
  276. OPENSSL_memcpy(nonce + nonce_len, seqnum, aead->variable_nonce_len);
  277. }
  278. nonce_len += aead->variable_nonce_len;
  279. /* Emit the variable nonce if included in the record. */
  280. if (aead->variable_nonce_included_in_record) {
  281. assert(!aead->xor_fixed_nonce);
  282. if (buffers_alias(in, in_len, out_prefix, aead->variable_nonce_len)) {
  283. OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
  284. return 0;
  285. }
  286. OPENSSL_memcpy(out_prefix, nonce + aead->fixed_nonce_len,
  287. aead->variable_nonce_len);
  288. }
  289. /* XOR the fixed nonce, if necessary. */
  290. if (aead->xor_fixed_nonce) {
  291. assert(nonce_len == aead->fixed_nonce_len);
  292. for (size_t i = 0; i < aead->fixed_nonce_len; i++) {
  293. nonce[i] ^= aead->fixed_nonce[i];
  294. }
  295. }
  296. return EVP_AEAD_CTX_seal_scatter(&aead->ctx, out, out_suffix, out_suffix_len,
  297. max_out_suffix_len, nonce, nonce_len, in,
  298. in_len, extra_in, extra_in_len, ad, ad_len);
  299. }
  300. int SSL_AEAD_CTX_seal(SSL_AEAD_CTX *aead, uint8_t *out, size_t *out_len,
  301. size_t max_out_len, uint8_t type, uint16_t wire_version,
  302. const uint8_t seqnum[8], const uint8_t *in,
  303. size_t in_len) {
  304. size_t prefix_len = SSL_AEAD_CTX_explicit_nonce_len(aead);
  305. if (in_len + prefix_len < in_len) {
  306. OPENSSL_PUT_ERROR(CIPHER, SSL_R_RECORD_TOO_LARGE);
  307. return 0;
  308. }
  309. if (in_len + prefix_len > max_out_len) {
  310. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
  311. return 0;
  312. }
  313. size_t suffix_len;
  314. if (!SSL_AEAD_CTX_seal_scatter(aead, out, out + prefix_len,
  315. out + prefix_len + in_len, &suffix_len,
  316. max_out_len - prefix_len - in_len, type,
  317. wire_version, seqnum, in, in_len, 0, 0)) {
  318. return 0;
  319. }
  320. assert(suffix_len <= SSL_AEAD_CTX_max_suffix_len(aead, 0));
  321. *out_len = prefix_len + in_len + suffix_len;
  322. return 1;
  323. }