Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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