Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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