You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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