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.
 
 
 
 
 
 

258 rivejä
8.8 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 <assert.h>
  15. #include <string.h>
  16. #include <openssl/aead.h>
  17. #include <openssl/err.h>
  18. #include <openssl/rand.h>
  19. #include <openssl/type_check.h>
  20. #include "internal.h"
  21. OPENSSL_COMPILE_ASSERT(EVP_AEAD_MAX_NONCE_LENGTH < 256,
  22. variable_nonce_len_doesnt_fit_in_uint8_t);
  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 discard;
  30. if (!ssl_cipher_get_evp_aead(&aead, &discard, &discard, cipher, version)) {
  31. OPENSSL_PUT_ERROR(SSL, SSL_AEAD_CTX_new, ERR_R_INTERNAL_ERROR);
  32. return 0;
  33. }
  34. uint8_t merged_key[EVP_AEAD_MAX_KEY_LENGTH];
  35. if (mac_key_len > 0) {
  36. /* This is a "stateful" AEAD (for compatibility with pre-AEAD cipher
  37. * suites). */
  38. if (mac_key_len + enc_key_len + fixed_iv_len > sizeof(merged_key)) {
  39. OPENSSL_PUT_ERROR(SSL, SSL_AEAD_CTX_new, ERR_R_INTERNAL_ERROR);
  40. return 0;
  41. }
  42. memcpy(merged_key, mac_key, mac_key_len);
  43. memcpy(merged_key + mac_key_len, enc_key, enc_key_len);
  44. memcpy(merged_key + mac_key_len + enc_key_len, fixed_iv, fixed_iv_len);
  45. enc_key = merged_key;
  46. enc_key_len += mac_key_len;
  47. enc_key_len += fixed_iv_len;
  48. }
  49. SSL_AEAD_CTX *aead_ctx = (SSL_AEAD_CTX *)OPENSSL_malloc(sizeof(SSL_AEAD_CTX));
  50. if (aead_ctx == NULL) {
  51. OPENSSL_PUT_ERROR(SSL, SSL_AEAD_CTX_new, ERR_R_MALLOC_FAILURE);
  52. return NULL;
  53. }
  54. memset(aead_ctx, 0, sizeof(SSL_AEAD_CTX));
  55. aead_ctx->cipher = cipher;
  56. if (!EVP_AEAD_CTX_init_with_direction(
  57. &aead_ctx->ctx, aead, enc_key, enc_key_len,
  58. EVP_AEAD_DEFAULT_TAG_LENGTH, direction)) {
  59. OPENSSL_free(aead_ctx);
  60. return NULL;
  61. }
  62. assert(EVP_AEAD_nonce_length(aead) <= EVP_AEAD_MAX_NONCE_LENGTH);
  63. aead_ctx->variable_nonce_len = (uint8_t)EVP_AEAD_nonce_length(aead);
  64. if (mac_key_len == 0) {
  65. /* For a real AEAD, the IV is the fixed part of the nonce. */
  66. if (fixed_iv_len > sizeof(aead_ctx->fixed_nonce) ||
  67. fixed_iv_len > aead_ctx->variable_nonce_len) {
  68. SSL_AEAD_CTX_free(aead_ctx);
  69. OPENSSL_PUT_ERROR(SSL, SSL_AEAD_CTX_new, ERR_R_INTERNAL_ERROR);
  70. return 0;
  71. }
  72. aead_ctx->variable_nonce_len -= fixed_iv_len;
  73. memcpy(aead_ctx->fixed_nonce, fixed_iv, fixed_iv_len);
  74. aead_ctx->fixed_nonce_len = fixed_iv_len;
  75. aead_ctx->variable_nonce_included_in_record =
  76. (cipher->algorithm2 &
  77. SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD) != 0;
  78. } else {
  79. aead_ctx->variable_nonce_included_in_record = 1;
  80. aead_ctx->random_variable_nonce = 1;
  81. aead_ctx->omit_length_in_ad = 1;
  82. aead_ctx->omit_version_in_ad = (version == SSL3_VERSION);
  83. }
  84. return aead_ctx;
  85. }
  86. void SSL_AEAD_CTX_free(SSL_AEAD_CTX *aead) {
  87. if (aead == NULL) {
  88. return;
  89. }
  90. EVP_AEAD_CTX_cleanup(&aead->ctx);
  91. OPENSSL_free(aead);
  92. }
  93. size_t SSL_AEAD_CTX_explicit_nonce_len(SSL_AEAD_CTX *aead) {
  94. if (aead != NULL && aead->variable_nonce_included_in_record) {
  95. return aead->variable_nonce_len;
  96. }
  97. return 0;
  98. }
  99. size_t SSL_AEAD_CTX_max_overhead(SSL_AEAD_CTX *aead) {
  100. if (aead == NULL) {
  101. return 0;
  102. }
  103. return EVP_AEAD_max_overhead(aead->ctx.aead) +
  104. SSL_AEAD_CTX_explicit_nonce_len(aead);
  105. }
  106. /* ssl_aead_ctx_get_ad writes the additional data for |aead| into |out| and
  107. * returns the number of bytes written. */
  108. static size_t ssl_aead_ctx_get_ad(SSL_AEAD_CTX *aead, uint8_t out[13],
  109. uint8_t type, uint16_t wire_version,
  110. const uint8_t seqnum[8],
  111. size_t plaintext_len) {
  112. memcpy(out, seqnum, 8);
  113. size_t len = 8;
  114. out[len++] = type;
  115. if (!aead->omit_version_in_ad) {
  116. out[len++] = (uint8_t)(wire_version >> 8);
  117. out[len++] = (uint8_t)wire_version;
  118. }
  119. if (!aead->omit_length_in_ad) {
  120. out[len++] = (uint8_t)(plaintext_len >> 8);
  121. out[len++] = (uint8_t)plaintext_len;
  122. }
  123. return len;
  124. }
  125. int SSL_AEAD_CTX_open(SSL_AEAD_CTX *aead, uint8_t *out, size_t *out_len,
  126. size_t max_out, uint8_t type, uint16_t wire_version,
  127. const uint8_t seqnum[8], const uint8_t *in,
  128. size_t in_len) {
  129. if (aead == NULL) {
  130. /* Handle the initial NULL cipher. */
  131. if (in_len > max_out) {
  132. OPENSSL_PUT_ERROR(SSL, SSL_AEAD_CTX_open, SSL_R_BUFFER_TOO_SMALL);
  133. return 0;
  134. }
  135. memmove(out, in, in_len);
  136. *out_len = in_len;
  137. return 1;
  138. }
  139. /* TLS 1.2 AEADs include the length in the AD and are assumed to have fixed
  140. * overhead. Otherwise the parameter is unused. */
  141. size_t plaintext_len = 0;
  142. if (!aead->omit_length_in_ad) {
  143. size_t overhead = SSL_AEAD_CTX_max_overhead(aead);
  144. if (in_len < overhead) {
  145. /* Publicly invalid. */
  146. OPENSSL_PUT_ERROR(SSL, SSL_AEAD_CTX_open, SSL_R_BAD_PACKET_LENGTH);
  147. return 0;
  148. }
  149. plaintext_len = in_len - overhead;
  150. }
  151. uint8_t ad[13];
  152. size_t ad_len = ssl_aead_ctx_get_ad(aead, ad, type, wire_version, seqnum,
  153. plaintext_len);
  154. /* Assemble the nonce. */
  155. uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
  156. size_t nonce_len = 0;
  157. memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
  158. nonce_len += aead->fixed_nonce_len;
  159. if (aead->variable_nonce_included_in_record) {
  160. if (in_len < aead->variable_nonce_len) {
  161. /* Publicly invalid. */
  162. OPENSSL_PUT_ERROR(SSL, SSL_AEAD_CTX_open, SSL_R_BAD_PACKET_LENGTH);
  163. return 0;
  164. }
  165. memcpy(nonce + nonce_len, in, aead->variable_nonce_len);
  166. in += aead->variable_nonce_len;
  167. in_len -= aead->variable_nonce_len;
  168. } else {
  169. assert(aead->variable_nonce_len == 8);
  170. memcpy(nonce + nonce_len, seqnum, aead->variable_nonce_len);
  171. }
  172. nonce_len += aead->variable_nonce_len;
  173. return EVP_AEAD_CTX_open(&aead->ctx, out, out_len, max_out, nonce, nonce_len,
  174. in, in_len, ad, ad_len);
  175. }
  176. int SSL_AEAD_CTX_seal(SSL_AEAD_CTX *aead, uint8_t *out, size_t *out_len,
  177. size_t max_out, uint8_t type, uint16_t wire_version,
  178. const uint8_t seqnum[8], const uint8_t *in,
  179. size_t in_len) {
  180. if (aead == NULL) {
  181. /* Handle the initial NULL cipher. */
  182. if (in_len > max_out) {
  183. OPENSSL_PUT_ERROR(SSL, SSL_AEAD_CTX_seal, SSL_R_BUFFER_TOO_SMALL);
  184. return 0;
  185. }
  186. memmove(out, in, in_len);
  187. *out_len = in_len;
  188. return 1;
  189. }
  190. uint8_t ad[13];
  191. size_t ad_len = ssl_aead_ctx_get_ad(aead, ad, type, wire_version, seqnum,
  192. in_len);
  193. /* Assemble the nonce. */
  194. uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
  195. size_t nonce_len = 0;
  196. memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
  197. nonce_len += aead->fixed_nonce_len;
  198. if (aead->random_variable_nonce) {
  199. assert(aead->variable_nonce_included_in_record);
  200. if (!RAND_bytes(nonce + nonce_len, aead->variable_nonce_len)) {
  201. return 0;
  202. }
  203. } else {
  204. /* When sending we use the sequence number as the variable part of the
  205. * nonce. */
  206. assert(aead->variable_nonce_len == 8);
  207. memcpy(nonce + nonce_len, ad, aead->variable_nonce_len);
  208. }
  209. nonce_len += aead->variable_nonce_len;
  210. /* Emit the variable nonce if included in the record. */
  211. size_t extra_len = 0;
  212. if (aead->variable_nonce_included_in_record) {
  213. if (max_out < aead->variable_nonce_len) {
  214. OPENSSL_PUT_ERROR(SSL, SSL_AEAD_CTX_seal, SSL_R_BUFFER_TOO_SMALL);
  215. return 0;
  216. }
  217. if (out < in + in_len && in < out + aead->variable_nonce_len) {
  218. OPENSSL_PUT_ERROR(SSL, SSL_AEAD_CTX_seal, SSL_R_OUTPUT_ALIASES_INPUT);
  219. return 0;
  220. }
  221. memcpy(out, nonce + aead->fixed_nonce_len, aead->variable_nonce_len);
  222. extra_len = aead->variable_nonce_len;
  223. out += aead->variable_nonce_len;
  224. max_out -= aead->variable_nonce_len;
  225. }
  226. if (!EVP_AEAD_CTX_seal(&aead->ctx, out, out_len, max_out, nonce, nonce_len,
  227. in, in_len, ad, ad_len)) {
  228. return 0;
  229. }
  230. *out_len += extra_len;
  231. return 1;
  232. }