Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ssl_aead_ctx.c 8.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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, 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, 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, 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, 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. /* AES-GCM uses an explicit nonce. */
  76. if (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) {
  77. aead_ctx->variable_nonce_included_in_record = 1;
  78. }
  79. } else {
  80. aead_ctx->variable_nonce_included_in_record = 1;
  81. aead_ctx->random_variable_nonce = 1;
  82. aead_ctx->omit_length_in_ad = 1;
  83. aead_ctx->omit_version_in_ad = (version == SSL3_VERSION);
  84. }
  85. return aead_ctx;
  86. }
  87. void SSL_AEAD_CTX_free(SSL_AEAD_CTX *aead) {
  88. if (aead == NULL) {
  89. return;
  90. }
  91. EVP_AEAD_CTX_cleanup(&aead->ctx);
  92. OPENSSL_free(aead);
  93. }
  94. size_t SSL_AEAD_CTX_explicit_nonce_len(SSL_AEAD_CTX *aead) {
  95. if (aead != NULL && aead->variable_nonce_included_in_record) {
  96. return aead->variable_nonce_len;
  97. }
  98. return 0;
  99. }
  100. size_t SSL_AEAD_CTX_max_overhead(SSL_AEAD_CTX *aead) {
  101. if (aead == NULL) {
  102. return 0;
  103. }
  104. return EVP_AEAD_max_overhead(aead->ctx.aead) +
  105. SSL_AEAD_CTX_explicit_nonce_len(aead);
  106. }
  107. /* ssl_aead_ctx_get_ad writes the additional data for |aead| into |out| and
  108. * returns the number of bytes written. */
  109. static size_t ssl_aead_ctx_get_ad(SSL_AEAD_CTX *aead, uint8_t out[13],
  110. uint8_t type, uint16_t wire_version,
  111. const uint8_t seqnum[8],
  112. size_t plaintext_len) {
  113. memcpy(out, seqnum, 8);
  114. size_t len = 8;
  115. out[len++] = type;
  116. if (!aead->omit_version_in_ad) {
  117. out[len++] = (uint8_t)(wire_version >> 8);
  118. out[len++] = (uint8_t)wire_version;
  119. }
  120. if (!aead->omit_length_in_ad) {
  121. out[len++] = (uint8_t)(plaintext_len >> 8);
  122. out[len++] = (uint8_t)plaintext_len;
  123. }
  124. return len;
  125. }
  126. int SSL_AEAD_CTX_open(SSL_AEAD_CTX *aead, uint8_t *out, size_t *out_len,
  127. size_t max_out, uint8_t type, uint16_t wire_version,
  128. const uint8_t seqnum[8], const uint8_t *in,
  129. size_t in_len) {
  130. if (aead == NULL) {
  131. /* Handle the initial NULL cipher. */
  132. if (in_len > max_out) {
  133. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
  134. return 0;
  135. }
  136. memmove(out, in, in_len);
  137. *out_len = in_len;
  138. return 1;
  139. }
  140. /* TLS 1.2 AEADs include the length in the AD and are assumed to have fixed
  141. * overhead. Otherwise the parameter is unused. */
  142. size_t plaintext_len = 0;
  143. if (!aead->omit_length_in_ad) {
  144. size_t overhead = SSL_AEAD_CTX_max_overhead(aead);
  145. if (in_len < overhead) {
  146. /* Publicly invalid. */
  147. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
  148. return 0;
  149. }
  150. plaintext_len = in_len - overhead;
  151. }
  152. uint8_t ad[13];
  153. size_t ad_len = ssl_aead_ctx_get_ad(aead, ad, type, wire_version, seqnum,
  154. plaintext_len);
  155. /* Assemble the nonce. */
  156. uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
  157. size_t nonce_len = 0;
  158. memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
  159. nonce_len += aead->fixed_nonce_len;
  160. if (aead->variable_nonce_included_in_record) {
  161. if (in_len < aead->variable_nonce_len) {
  162. /* Publicly invalid. */
  163. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
  164. return 0;
  165. }
  166. memcpy(nonce + nonce_len, in, aead->variable_nonce_len);
  167. in += aead->variable_nonce_len;
  168. in_len -= aead->variable_nonce_len;
  169. } else {
  170. assert(aead->variable_nonce_len == 8);
  171. memcpy(nonce + nonce_len, seqnum, aead->variable_nonce_len);
  172. }
  173. nonce_len += aead->variable_nonce_len;
  174. return EVP_AEAD_CTX_open(&aead->ctx, out, out_len, max_out, nonce, nonce_len,
  175. in, in_len, ad, ad_len);
  176. }
  177. int SSL_AEAD_CTX_seal(SSL_AEAD_CTX *aead, uint8_t *out, size_t *out_len,
  178. size_t max_out, uint8_t type, uint16_t wire_version,
  179. const uint8_t seqnum[8], const uint8_t *in,
  180. size_t in_len) {
  181. if (aead == NULL) {
  182. /* Handle the initial NULL cipher. */
  183. if (in_len > max_out) {
  184. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
  185. return 0;
  186. }
  187. memmove(out, in, in_len);
  188. *out_len = in_len;
  189. return 1;
  190. }
  191. uint8_t ad[13];
  192. size_t ad_len = ssl_aead_ctx_get_ad(aead, ad, type, wire_version, seqnum,
  193. in_len);
  194. /* Assemble the nonce. */
  195. uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
  196. size_t nonce_len = 0;
  197. memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
  198. nonce_len += aead->fixed_nonce_len;
  199. if (aead->random_variable_nonce) {
  200. assert(aead->variable_nonce_included_in_record);
  201. if (!RAND_bytes(nonce + nonce_len, aead->variable_nonce_len)) {
  202. return 0;
  203. }
  204. } else {
  205. /* When sending we use the sequence number as the variable part of the
  206. * nonce. */
  207. assert(aead->variable_nonce_len == 8);
  208. memcpy(nonce + nonce_len, ad, aead->variable_nonce_len);
  209. }
  210. nonce_len += aead->variable_nonce_len;
  211. /* Emit the variable nonce if included in the record. */
  212. size_t extra_len = 0;
  213. if (aead->variable_nonce_included_in_record) {
  214. if (max_out < aead->variable_nonce_len) {
  215. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
  216. return 0;
  217. }
  218. if (out < in + in_len && in < out + aead->variable_nonce_len) {
  219. OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
  220. return 0;
  221. }
  222. memcpy(out, nonce + aead->fixed_nonce_len, aead->variable_nonce_len);
  223. extra_len = aead->variable_nonce_len;
  224. out += aead->variable_nonce_len;
  225. max_out -= aead->variable_nonce_len;
  226. }
  227. if (!EVP_AEAD_CTX_seal(&aead->ctx, out, out_len, max_out, nonce, nonce_len,
  228. in, in_len, ad, ad_len)) {
  229. return 0;
  230. }
  231. *out_len += extra_len;
  232. return 1;
  233. }