No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

337 líneas
11 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 <openssl/type_check.h>
  21. #include "../crypto/internal.h"
  22. #include "internal.h"
  23. SSL_AEAD_CTX *SSL_AEAD_CTX_new(enum evp_aead_direction_t direction,
  24. uint16_t version, const SSL_CIPHER *cipher,
  25. const uint8_t *enc_key, size_t enc_key_len,
  26. const uint8_t *mac_key, size_t mac_key_len,
  27. const uint8_t *fixed_iv, 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. /* Ensure the caller returned correct key sizes. */
  33. expected_fixed_iv_len != fixed_iv_len ||
  34. expected_mac_key_len != mac_key_len) {
  35. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  36. return 0;
  37. }
  38. uint8_t merged_key[EVP_AEAD_MAX_KEY_LENGTH];
  39. if (mac_key_len > 0) {
  40. /* This is a "stateful" AEAD (for compatibility with pre-AEAD cipher
  41. * suites). */
  42. if (mac_key_len + enc_key_len + fixed_iv_len > sizeof(merged_key)) {
  43. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  44. return 0;
  45. }
  46. OPENSSL_memcpy(merged_key, mac_key, mac_key_len);
  47. OPENSSL_memcpy(merged_key + mac_key_len, enc_key, enc_key_len);
  48. OPENSSL_memcpy(merged_key + mac_key_len + enc_key_len, fixed_iv,
  49. fixed_iv_len);
  50. enc_key = merged_key;
  51. enc_key_len += mac_key_len;
  52. enc_key_len += fixed_iv_len;
  53. }
  54. SSL_AEAD_CTX *aead_ctx = OPENSSL_malloc(sizeof(SSL_AEAD_CTX));
  55. if (aead_ctx == NULL) {
  56. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  57. return NULL;
  58. }
  59. OPENSSL_memset(aead_ctx, 0, sizeof(SSL_AEAD_CTX));
  60. aead_ctx->cipher = cipher;
  61. aead_ctx->version = version;
  62. if (!EVP_AEAD_CTX_init_with_direction(
  63. &aead_ctx->ctx, aead, enc_key, enc_key_len,
  64. EVP_AEAD_DEFAULT_TAG_LENGTH, direction)) {
  65. OPENSSL_free(aead_ctx);
  66. return NULL;
  67. }
  68. assert(EVP_AEAD_nonce_length(aead) <= EVP_AEAD_MAX_NONCE_LENGTH);
  69. OPENSSL_COMPILE_ASSERT(EVP_AEAD_MAX_NONCE_LENGTH < 256,
  70. variable_nonce_len_doesnt_fit_in_uint8_t);
  71. aead_ctx->variable_nonce_len = (uint8_t)EVP_AEAD_nonce_length(aead);
  72. if (mac_key_len == 0) {
  73. assert(fixed_iv_len <= sizeof(aead_ctx->fixed_nonce));
  74. OPENSSL_memcpy(aead_ctx->fixed_nonce, fixed_iv, fixed_iv_len);
  75. aead_ctx->fixed_nonce_len = fixed_iv_len;
  76. if (cipher->algorithm_enc & SSL_CHACHA20POLY1305) {
  77. /* The fixed nonce into the actual nonce (the sequence number). */
  78. aead_ctx->xor_fixed_nonce = 1;
  79. aead_ctx->variable_nonce_len = 8;
  80. } else {
  81. /* The fixed IV is prepended to the nonce. */
  82. assert(fixed_iv_len <= aead_ctx->variable_nonce_len);
  83. aead_ctx->variable_nonce_len -= fixed_iv_len;
  84. }
  85. /* AES-GCM uses an explicit nonce. */
  86. if (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) {
  87. aead_ctx->variable_nonce_included_in_record = 1;
  88. }
  89. /* The TLS 1.3 construction XORs the fixed nonce into the sequence number
  90. * and omits the additional data. */
  91. if (version >= TLS1_3_VERSION) {
  92. aead_ctx->xor_fixed_nonce = 1;
  93. aead_ctx->variable_nonce_len = 8;
  94. aead_ctx->variable_nonce_included_in_record = 0;
  95. aead_ctx->omit_ad = 1;
  96. assert(fixed_iv_len >= aead_ctx->variable_nonce_len);
  97. }
  98. } else {
  99. assert(version < TLS1_3_VERSION);
  100. aead_ctx->variable_nonce_included_in_record = 1;
  101. aead_ctx->random_variable_nonce = 1;
  102. aead_ctx->omit_length_in_ad = 1;
  103. aead_ctx->omit_version_in_ad = (version == SSL3_VERSION);
  104. }
  105. return aead_ctx;
  106. }
  107. void SSL_AEAD_CTX_free(SSL_AEAD_CTX *aead) {
  108. if (aead == NULL) {
  109. return;
  110. }
  111. EVP_AEAD_CTX_cleanup(&aead->ctx);
  112. OPENSSL_free(aead);
  113. }
  114. size_t SSL_AEAD_CTX_explicit_nonce_len(const SSL_AEAD_CTX *aead) {
  115. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  116. aead = NULL;
  117. #endif
  118. if (aead != NULL && aead->variable_nonce_included_in_record) {
  119. return aead->variable_nonce_len;
  120. }
  121. return 0;
  122. }
  123. size_t SSL_AEAD_CTX_max_overhead(const SSL_AEAD_CTX *aead) {
  124. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  125. aead = NULL;
  126. #endif
  127. if (aead == NULL) {
  128. return 0;
  129. }
  130. return EVP_AEAD_max_overhead(aead->ctx.aead) +
  131. SSL_AEAD_CTX_explicit_nonce_len(aead);
  132. }
  133. /* ssl_aead_ctx_get_ad writes the additional data for |aead| into |out| and
  134. * returns the number of bytes written. */
  135. static size_t ssl_aead_ctx_get_ad(SSL_AEAD_CTX *aead, uint8_t out[13],
  136. uint8_t type, uint16_t wire_version,
  137. const uint8_t seqnum[8],
  138. size_t plaintext_len) {
  139. if (aead->omit_ad) {
  140. return 0;
  141. }
  142. OPENSSL_memcpy(out, seqnum, 8);
  143. size_t len = 8;
  144. out[len++] = type;
  145. if (!aead->omit_version_in_ad) {
  146. out[len++] = (uint8_t)(wire_version >> 8);
  147. out[len++] = (uint8_t)wire_version;
  148. }
  149. if (!aead->omit_length_in_ad) {
  150. out[len++] = (uint8_t)(plaintext_len >> 8);
  151. out[len++] = (uint8_t)plaintext_len;
  152. }
  153. return len;
  154. }
  155. int SSL_AEAD_CTX_open(SSL_AEAD_CTX *aead, CBS *out, uint8_t type,
  156. uint16_t wire_version, const uint8_t seqnum[8],
  157. uint8_t *in, size_t in_len) {
  158. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  159. aead = NULL;
  160. #endif
  161. if (aead == NULL) {
  162. /* Handle the initial NULL cipher. */
  163. CBS_init(out, in, in_len);
  164. return 1;
  165. }
  166. /* TLS 1.2 AEADs include the length in the AD and are assumed to have fixed
  167. * overhead. Otherwise the parameter is unused. */
  168. size_t plaintext_len = 0;
  169. if (!aead->omit_length_in_ad) {
  170. size_t overhead = SSL_AEAD_CTX_max_overhead(aead);
  171. if (in_len < overhead) {
  172. /* Publicly invalid. */
  173. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
  174. return 0;
  175. }
  176. plaintext_len = in_len - overhead;
  177. }
  178. uint8_t ad[13];
  179. size_t ad_len = ssl_aead_ctx_get_ad(aead, ad, type, wire_version, seqnum,
  180. plaintext_len);
  181. /* Assemble the nonce. */
  182. uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
  183. size_t nonce_len = 0;
  184. /* Prepend the fixed nonce, or left-pad with zeros if XORing. */
  185. if (aead->xor_fixed_nonce) {
  186. nonce_len = aead->fixed_nonce_len - aead->variable_nonce_len;
  187. OPENSSL_memset(nonce, 0, nonce_len);
  188. } else {
  189. OPENSSL_memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
  190. nonce_len += aead->fixed_nonce_len;
  191. }
  192. /* Add the variable nonce. */
  193. if (aead->variable_nonce_included_in_record) {
  194. if (in_len < aead->variable_nonce_len) {
  195. /* Publicly invalid. */
  196. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
  197. return 0;
  198. }
  199. OPENSSL_memcpy(nonce + nonce_len, in, aead->variable_nonce_len);
  200. in += aead->variable_nonce_len;
  201. in_len -= aead->variable_nonce_len;
  202. } else {
  203. assert(aead->variable_nonce_len == 8);
  204. OPENSSL_memcpy(nonce + nonce_len, seqnum, aead->variable_nonce_len);
  205. }
  206. nonce_len += aead->variable_nonce_len;
  207. /* XOR the fixed nonce, if necessary. */
  208. if (aead->xor_fixed_nonce) {
  209. assert(nonce_len == aead->fixed_nonce_len);
  210. for (size_t i = 0; i < aead->fixed_nonce_len; i++) {
  211. nonce[i] ^= aead->fixed_nonce[i];
  212. }
  213. }
  214. /* Decrypt in-place. */
  215. size_t len;
  216. if (!EVP_AEAD_CTX_open(&aead->ctx, in, &len, in_len, nonce, nonce_len,
  217. in, in_len, ad, ad_len)) {
  218. return 0;
  219. }
  220. CBS_init(out, in, len);
  221. return 1;
  222. }
  223. int SSL_AEAD_CTX_seal(SSL_AEAD_CTX *aead, uint8_t *out, size_t *out_len,
  224. size_t max_out, uint8_t type, uint16_t wire_version,
  225. const uint8_t seqnum[8], const uint8_t *in,
  226. size_t in_len) {
  227. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  228. aead = NULL;
  229. #endif
  230. if (aead == NULL) {
  231. /* Handle the initial NULL cipher. */
  232. if (in_len > max_out) {
  233. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
  234. return 0;
  235. }
  236. OPENSSL_memmove(out, in, in_len);
  237. *out_len = in_len;
  238. return 1;
  239. }
  240. uint8_t ad[13];
  241. size_t ad_len = ssl_aead_ctx_get_ad(aead, ad, type, wire_version, seqnum,
  242. in_len);
  243. /* Assemble the nonce. */
  244. uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
  245. size_t nonce_len = 0;
  246. /* Prepend the fixed nonce, or left-pad with zeros if XORing. */
  247. if (aead->xor_fixed_nonce) {
  248. nonce_len = aead->fixed_nonce_len - aead->variable_nonce_len;
  249. OPENSSL_memset(nonce, 0, nonce_len);
  250. } else {
  251. OPENSSL_memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
  252. nonce_len += aead->fixed_nonce_len;
  253. }
  254. /* Select the variable nonce. */
  255. if (aead->random_variable_nonce) {
  256. assert(aead->variable_nonce_included_in_record);
  257. if (!RAND_bytes(nonce + nonce_len, aead->variable_nonce_len)) {
  258. return 0;
  259. }
  260. } else {
  261. /* When sending we use the sequence number as the variable part of the
  262. * nonce. */
  263. assert(aead->variable_nonce_len == 8);
  264. OPENSSL_memcpy(nonce + nonce_len, seqnum, aead->variable_nonce_len);
  265. }
  266. nonce_len += aead->variable_nonce_len;
  267. /* Emit the variable nonce if included in the record. */
  268. size_t extra_len = 0;
  269. if (aead->variable_nonce_included_in_record) {
  270. assert(!aead->xor_fixed_nonce);
  271. if (max_out < aead->variable_nonce_len) {
  272. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
  273. return 0;
  274. }
  275. if (out < in + in_len && in < out + aead->variable_nonce_len) {
  276. OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
  277. return 0;
  278. }
  279. OPENSSL_memcpy(out, nonce + aead->fixed_nonce_len,
  280. aead->variable_nonce_len);
  281. extra_len = aead->variable_nonce_len;
  282. out += aead->variable_nonce_len;
  283. max_out -= aead->variable_nonce_len;
  284. }
  285. /* XOR the fixed nonce, if necessary. */
  286. if (aead->xor_fixed_nonce) {
  287. assert(nonce_len == aead->fixed_nonce_len);
  288. for (size_t i = 0; i < aead->fixed_nonce_len; i++) {
  289. nonce[i] ^= aead->fixed_nonce[i];
  290. }
  291. }
  292. if (!EVP_AEAD_CTX_seal(&aead->ctx, out, out_len, max_out, nonce, nonce_len,
  293. in, in_len, ad, ad_len)) {
  294. return 0;
  295. }
  296. *out_len += extra_len;
  297. return 1;
  298. }