Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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