Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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