ssl_aead_ctx.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 "../crypto/internal.h"
  21. #include "internal.h"
  22. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  23. #define FUZZER_MODE true
  24. #else
  25. #define FUZZER_MODE false
  26. #endif
  27. BSSL_NAMESPACE_BEGIN
  28. SSLAEADContext::SSLAEADContext(uint16_t version_arg, bool is_dtls_arg,
  29. const SSL_CIPHER *cipher_arg)
  30. : cipher_(cipher_arg),
  31. version_(version_arg),
  32. is_dtls_(is_dtls_arg),
  33. variable_nonce_included_in_record_(false),
  34. random_variable_nonce_(false),
  35. xor_fixed_nonce_(false),
  36. omit_length_in_ad_(false),
  37. ad_is_header_(false) {
  38. OPENSSL_memset(fixed_nonce_, 0, sizeof(fixed_nonce_));
  39. }
  40. SSLAEADContext::~SSLAEADContext() {}
  41. UniquePtr<SSLAEADContext> SSLAEADContext::CreateNullCipher(bool is_dtls) {
  42. return MakeUnique<SSLAEADContext>(0 /* version */, is_dtls,
  43. nullptr /* cipher */);
  44. }
  45. UniquePtr<SSLAEADContext> SSLAEADContext::Create(
  46. enum evp_aead_direction_t direction, uint16_t version, bool is_dtls,
  47. const SSL_CIPHER *cipher, Span<const uint8_t> enc_key,
  48. Span<const uint8_t> mac_key, Span<const uint8_t> fixed_iv) {
  49. const EVP_AEAD *aead;
  50. uint16_t protocol_version;
  51. size_t expected_mac_key_len, expected_fixed_iv_len;
  52. if (!ssl_protocol_version_from_wire(&protocol_version, version) ||
  53. !ssl_cipher_get_evp_aead(&aead, &expected_mac_key_len,
  54. &expected_fixed_iv_len, cipher, protocol_version,
  55. is_dtls) ||
  56. // Ensure the caller returned correct key sizes.
  57. expected_fixed_iv_len != fixed_iv.size() ||
  58. expected_mac_key_len != mac_key.size()) {
  59. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  60. return nullptr;
  61. }
  62. uint8_t merged_key[EVP_AEAD_MAX_KEY_LENGTH];
  63. if (!mac_key.empty()) {
  64. // This is a "stateful" AEAD (for compatibility with pre-AEAD cipher
  65. // suites).
  66. if (mac_key.size() + enc_key.size() + fixed_iv.size() >
  67. sizeof(merged_key)) {
  68. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  69. return nullptr;
  70. }
  71. OPENSSL_memcpy(merged_key, mac_key.data(), mac_key.size());
  72. OPENSSL_memcpy(merged_key + mac_key.size(), enc_key.data(), enc_key.size());
  73. OPENSSL_memcpy(merged_key + mac_key.size() + enc_key.size(),
  74. fixed_iv.data(), fixed_iv.size());
  75. enc_key = MakeConstSpan(merged_key,
  76. enc_key.size() + mac_key.size() + fixed_iv.size());
  77. }
  78. UniquePtr<SSLAEADContext> aead_ctx =
  79. MakeUnique<SSLAEADContext>(version, is_dtls, cipher);
  80. if (!aead_ctx) {
  81. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  82. return nullptr;
  83. }
  84. assert(aead_ctx->ProtocolVersion() == protocol_version);
  85. if (!EVP_AEAD_CTX_init_with_direction(
  86. aead_ctx->ctx_.get(), aead, enc_key.data(), enc_key.size(),
  87. EVP_AEAD_DEFAULT_TAG_LENGTH, direction)) {
  88. return nullptr;
  89. }
  90. assert(EVP_AEAD_nonce_length(aead) <= EVP_AEAD_MAX_NONCE_LENGTH);
  91. static_assert(EVP_AEAD_MAX_NONCE_LENGTH < 256,
  92. "variable_nonce_len doesn't fit in uint8_t");
  93. aead_ctx->variable_nonce_len_ = (uint8_t)EVP_AEAD_nonce_length(aead);
  94. if (mac_key.empty()) {
  95. assert(fixed_iv.size() <= sizeof(aead_ctx->fixed_nonce_));
  96. OPENSSL_memcpy(aead_ctx->fixed_nonce_, fixed_iv.data(), fixed_iv.size());
  97. aead_ctx->fixed_nonce_len_ = fixed_iv.size();
  98. if (cipher->algorithm_enc & SSL_CHACHA20POLY1305) {
  99. // The fixed nonce into the actual nonce (the sequence number).
  100. aead_ctx->xor_fixed_nonce_ = true;
  101. aead_ctx->variable_nonce_len_ = 8;
  102. } else {
  103. // The fixed IV is prepended to the nonce.
  104. assert(fixed_iv.size() <= aead_ctx->variable_nonce_len_);
  105. aead_ctx->variable_nonce_len_ -= fixed_iv.size();
  106. }
  107. // AES-GCM uses an explicit nonce.
  108. if (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) {
  109. aead_ctx->variable_nonce_included_in_record_ = true;
  110. }
  111. // The TLS 1.3 construction XORs the fixed nonce into the sequence number
  112. // and omits the additional data.
  113. if (protocol_version >= TLS1_3_VERSION) {
  114. aead_ctx->xor_fixed_nonce_ = true;
  115. aead_ctx->variable_nonce_len_ = 8;
  116. aead_ctx->variable_nonce_included_in_record_ = false;
  117. aead_ctx->ad_is_header_ = true;
  118. assert(fixed_iv.size() >= aead_ctx->variable_nonce_len_);
  119. }
  120. } else {
  121. assert(protocol_version < TLS1_3_VERSION);
  122. aead_ctx->variable_nonce_included_in_record_ = true;
  123. aead_ctx->random_variable_nonce_ = true;
  124. aead_ctx->omit_length_in_ad_ = true;
  125. }
  126. return aead_ctx;
  127. }
  128. UniquePtr<SSLAEADContext> SSLAEADContext::CreatePlaceholderForQUIC(
  129. uint16_t version, const SSL_CIPHER *cipher) {
  130. return MakeUnique<SSLAEADContext>(version, false, cipher);
  131. }
  132. void SSLAEADContext::SetVersionIfNullCipher(uint16_t version) {
  133. if (is_null_cipher()) {
  134. version_ = version;
  135. }
  136. }
  137. uint16_t SSLAEADContext::ProtocolVersion() const {
  138. uint16_t protocol_version;
  139. if(!ssl_protocol_version_from_wire(&protocol_version, version_)) {
  140. assert(false);
  141. return 0;
  142. }
  143. return protocol_version;
  144. }
  145. uint16_t SSLAEADContext::RecordVersion() const {
  146. if (version_ == 0) {
  147. assert(is_null_cipher());
  148. return is_dtls_ ? DTLS1_VERSION : TLS1_VERSION;
  149. }
  150. if (ProtocolVersion() <= TLS1_2_VERSION) {
  151. return version_;
  152. }
  153. return TLS1_2_VERSION;
  154. }
  155. size_t SSLAEADContext::ExplicitNonceLen() const {
  156. if (!FUZZER_MODE && variable_nonce_included_in_record_) {
  157. return variable_nonce_len_;
  158. }
  159. return 0;
  160. }
  161. bool SSLAEADContext::SuffixLen(size_t *out_suffix_len, const size_t in_len,
  162. const size_t extra_in_len) const {
  163. if (is_null_cipher() || FUZZER_MODE) {
  164. *out_suffix_len = extra_in_len;
  165. return true;
  166. }
  167. return !!EVP_AEAD_CTX_tag_len(ctx_.get(), out_suffix_len, in_len,
  168. extra_in_len);
  169. }
  170. bool SSLAEADContext::CiphertextLen(size_t *out_len, const size_t in_len,
  171. const size_t extra_in_len) const {
  172. size_t len;
  173. if (!SuffixLen(&len, in_len, extra_in_len)) {
  174. return false;
  175. }
  176. len += ExplicitNonceLen();
  177. len += in_len;
  178. if (len < in_len || len >= 0xffff) {
  179. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  180. return false;
  181. }
  182. *out_len = len;
  183. return true;
  184. }
  185. size_t SSLAEADContext::MaxOverhead() const {
  186. return ExplicitNonceLen() +
  187. (is_null_cipher() || FUZZER_MODE
  188. ? 0
  189. : EVP_AEAD_max_overhead(EVP_AEAD_CTX_aead(ctx_.get())));
  190. }
  191. Span<const uint8_t> SSLAEADContext::GetAdditionalData(
  192. uint8_t storage[13], uint8_t type, uint16_t record_version,
  193. const uint8_t seqnum[8], size_t plaintext_len, Span<const uint8_t> header) {
  194. if (ad_is_header_) {
  195. return header;
  196. }
  197. OPENSSL_memcpy(storage, seqnum, 8);
  198. size_t len = 8;
  199. storage[len++] = type;
  200. storage[len++] = static_cast<uint8_t>((record_version >> 8));
  201. storage[len++] = static_cast<uint8_t>(record_version);
  202. if (!omit_length_in_ad_) {
  203. storage[len++] = static_cast<uint8_t>((plaintext_len >> 8));
  204. storage[len++] = static_cast<uint8_t>(plaintext_len);
  205. }
  206. return MakeConstSpan(storage, len);
  207. }
  208. bool SSLAEADContext::Open(Span<uint8_t> *out, uint8_t type,
  209. uint16_t record_version, const uint8_t seqnum[8],
  210. Span<const uint8_t> header, Span<uint8_t> in) {
  211. if (is_null_cipher() || FUZZER_MODE) {
  212. // Handle the initial NULL cipher.
  213. *out = in;
  214. return true;
  215. }
  216. // TLS 1.2 AEADs include the length in the AD and are assumed to have fixed
  217. // overhead. Otherwise the parameter is unused.
  218. size_t plaintext_len = 0;
  219. if (!omit_length_in_ad_) {
  220. size_t overhead = MaxOverhead();
  221. if (in.size() < overhead) {
  222. // Publicly invalid.
  223. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
  224. return false;
  225. }
  226. plaintext_len = in.size() - overhead;
  227. }
  228. uint8_t ad_storage[13];
  229. Span<const uint8_t> ad = GetAdditionalData(ad_storage, type, record_version,
  230. seqnum, plaintext_len, header);
  231. // Assemble the nonce.
  232. uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
  233. size_t nonce_len = 0;
  234. // Prepend the fixed nonce, or left-pad with zeros if XORing.
  235. if (xor_fixed_nonce_) {
  236. nonce_len = fixed_nonce_len_ - variable_nonce_len_;
  237. OPENSSL_memset(nonce, 0, nonce_len);
  238. } else {
  239. OPENSSL_memcpy(nonce, fixed_nonce_, fixed_nonce_len_);
  240. nonce_len += fixed_nonce_len_;
  241. }
  242. // Add the variable nonce.
  243. if (variable_nonce_included_in_record_) {
  244. if (in.size() < variable_nonce_len_) {
  245. // Publicly invalid.
  246. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
  247. return false;
  248. }
  249. OPENSSL_memcpy(nonce + nonce_len, in.data(), variable_nonce_len_);
  250. in = in.subspan(variable_nonce_len_);
  251. } else {
  252. assert(variable_nonce_len_ == 8);
  253. OPENSSL_memcpy(nonce + nonce_len, seqnum, variable_nonce_len_);
  254. }
  255. nonce_len += variable_nonce_len_;
  256. // XOR the fixed nonce, if necessary.
  257. if (xor_fixed_nonce_) {
  258. assert(nonce_len == fixed_nonce_len_);
  259. for (size_t i = 0; i < fixed_nonce_len_; i++) {
  260. nonce[i] ^= fixed_nonce_[i];
  261. }
  262. }
  263. // Decrypt in-place.
  264. size_t len;
  265. if (!EVP_AEAD_CTX_open(ctx_.get(), in.data(), &len, in.size(), nonce,
  266. nonce_len, in.data(), in.size(), ad.data(),
  267. ad.size())) {
  268. return false;
  269. }
  270. *out = in.subspan(0, len);
  271. return true;
  272. }
  273. bool SSLAEADContext::SealScatter(uint8_t *out_prefix, uint8_t *out,
  274. uint8_t *out_suffix, uint8_t type,
  275. uint16_t record_version,
  276. const uint8_t seqnum[8],
  277. Span<const uint8_t> header, const uint8_t *in,
  278. size_t in_len, const uint8_t *extra_in,
  279. size_t extra_in_len) {
  280. const size_t prefix_len = ExplicitNonceLen();
  281. size_t suffix_len;
  282. if (!SuffixLen(&suffix_len, in_len, extra_in_len)) {
  283. OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
  284. return false;
  285. }
  286. if ((in != out && buffers_alias(in, in_len, out, in_len)) ||
  287. buffers_alias(in, in_len, out_prefix, prefix_len) ||
  288. buffers_alias(in, in_len, out_suffix, suffix_len)) {
  289. OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
  290. return false;
  291. }
  292. if (is_null_cipher() || FUZZER_MODE) {
  293. // Handle the initial NULL cipher.
  294. OPENSSL_memmove(out, in, in_len);
  295. OPENSSL_memmove(out_suffix, extra_in, extra_in_len);
  296. return true;
  297. }
  298. uint8_t ad_storage[13];
  299. Span<const uint8_t> ad = GetAdditionalData(ad_storage, type, record_version,
  300. seqnum, in_len, header);
  301. // Assemble the nonce.
  302. uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
  303. size_t nonce_len = 0;
  304. // Prepend the fixed nonce, or left-pad with zeros if XORing.
  305. if (xor_fixed_nonce_) {
  306. nonce_len = fixed_nonce_len_ - variable_nonce_len_;
  307. OPENSSL_memset(nonce, 0, nonce_len);
  308. } else {
  309. OPENSSL_memcpy(nonce, fixed_nonce_, fixed_nonce_len_);
  310. nonce_len += fixed_nonce_len_;
  311. }
  312. // Select the variable nonce.
  313. if (random_variable_nonce_) {
  314. assert(variable_nonce_included_in_record_);
  315. if (!RAND_bytes(nonce + nonce_len, variable_nonce_len_)) {
  316. return false;
  317. }
  318. } else {
  319. // When sending we use the sequence number as the variable part of the
  320. // nonce.
  321. assert(variable_nonce_len_ == 8);
  322. OPENSSL_memcpy(nonce + nonce_len, seqnum, variable_nonce_len_);
  323. }
  324. nonce_len += variable_nonce_len_;
  325. // Emit the variable nonce if included in the record.
  326. if (variable_nonce_included_in_record_) {
  327. assert(!xor_fixed_nonce_);
  328. if (buffers_alias(in, in_len, out_prefix, variable_nonce_len_)) {
  329. OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
  330. return false;
  331. }
  332. OPENSSL_memcpy(out_prefix, nonce + fixed_nonce_len_,
  333. variable_nonce_len_);
  334. }
  335. // XOR the fixed nonce, if necessary.
  336. if (xor_fixed_nonce_) {
  337. assert(nonce_len == fixed_nonce_len_);
  338. for (size_t i = 0; i < fixed_nonce_len_; i++) {
  339. nonce[i] ^= fixed_nonce_[i];
  340. }
  341. }
  342. size_t written_suffix_len;
  343. bool result = !!EVP_AEAD_CTX_seal_scatter(
  344. ctx_.get(), out, out_suffix, &written_suffix_len, suffix_len, nonce,
  345. nonce_len, in, in_len, extra_in, extra_in_len, ad.data(), ad.size());
  346. assert(!result || written_suffix_len == suffix_len);
  347. return result;
  348. }
  349. bool SSLAEADContext::Seal(uint8_t *out, size_t *out_len, size_t max_out_len,
  350. uint8_t type, uint16_t record_version,
  351. const uint8_t seqnum[8], Span<const uint8_t> header,
  352. const uint8_t *in, size_t in_len) {
  353. const size_t prefix_len = ExplicitNonceLen();
  354. size_t suffix_len;
  355. if (!SuffixLen(&suffix_len, in_len, 0)) {
  356. OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
  357. return false;
  358. }
  359. if (in_len + prefix_len < in_len ||
  360. in_len + prefix_len + suffix_len < in_len + prefix_len) {
  361. OPENSSL_PUT_ERROR(CIPHER, SSL_R_RECORD_TOO_LARGE);
  362. return false;
  363. }
  364. if (in_len + prefix_len + suffix_len > max_out_len) {
  365. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
  366. return false;
  367. }
  368. if (!SealScatter(out, out + prefix_len, out + prefix_len + in_len, type,
  369. record_version, seqnum, header, in, in_len, 0, 0)) {
  370. return false;
  371. }
  372. *out_len = prefix_len + in_len + suffix_len;
  373. return true;
  374. }
  375. bool SSLAEADContext::GetIV(const uint8_t **out_iv, size_t *out_iv_len) const {
  376. return !is_null_cipher() &&
  377. EVP_AEAD_CTX_get_iv(ctx_.get(), out_iv, out_iv_len);
  378. }
  379. BSSL_NAMESPACE_END