ssl_aead_ctx.cc 14 KB

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