Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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