You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

413 lines
14 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 "../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. return TLS1_2_VERSION;
  152. }
  153. size_t SSLAEADContext::ExplicitNonceLen() const {
  154. if (!FUZZER_MODE && variable_nonce_included_in_record_) {
  155. return variable_nonce_len_;
  156. }
  157. return 0;
  158. }
  159. bool SSLAEADContext::SuffixLen(size_t *out_suffix_len, const size_t in_len,
  160. const size_t extra_in_len) const {
  161. if (is_null_cipher() || FUZZER_MODE) {
  162. *out_suffix_len = extra_in_len;
  163. return true;
  164. }
  165. return !!EVP_AEAD_CTX_tag_len(ctx_.get(), out_suffix_len, in_len,
  166. extra_in_len);
  167. }
  168. size_t SSLAEADContext::MaxOverhead() const {
  169. return ExplicitNonceLen() +
  170. (is_null_cipher() || FUZZER_MODE
  171. ? 0
  172. : EVP_AEAD_max_overhead(EVP_AEAD_CTX_aead(ctx_.get())));
  173. }
  174. size_t SSLAEADContext::GetAdditionalData(uint8_t out[13], uint8_t type,
  175. uint16_t record_version,
  176. const uint8_t seqnum[8],
  177. size_t plaintext_len) {
  178. if (omit_ad_) {
  179. return 0;
  180. }
  181. OPENSSL_memcpy(out, seqnum, 8);
  182. size_t len = 8;
  183. out[len++] = type;
  184. if (!omit_version_in_ad_) {
  185. out[len++] = static_cast<uint8_t>((record_version >> 8));
  186. out[len++] = static_cast<uint8_t>(record_version);
  187. }
  188. if (!omit_length_in_ad_) {
  189. out[len++] = static_cast<uint8_t>((plaintext_len >> 8));
  190. out[len++] = static_cast<uint8_t>(plaintext_len);
  191. }
  192. return len;
  193. }
  194. bool SSLAEADContext::Open(Span<uint8_t> *out, uint8_t type,
  195. uint16_t record_version, const uint8_t seqnum[8],
  196. Span<uint8_t> in) {
  197. if (is_null_cipher() || FUZZER_MODE) {
  198. // Handle the initial NULL cipher.
  199. *out = in;
  200. return true;
  201. }
  202. // TLS 1.2 AEADs include the length in the AD and are assumed to have fixed
  203. // overhead. Otherwise the parameter is unused.
  204. size_t plaintext_len = 0;
  205. if (!omit_length_in_ad_) {
  206. size_t overhead = MaxOverhead();
  207. if (in.size() < overhead) {
  208. // Publicly invalid.
  209. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
  210. return false;
  211. }
  212. plaintext_len = in.size() - overhead;
  213. }
  214. uint8_t ad[13];
  215. size_t ad_len =
  216. GetAdditionalData(ad, type, record_version, seqnum, plaintext_len);
  217. // Assemble the nonce.
  218. uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
  219. size_t nonce_len = 0;
  220. // Prepend the fixed nonce, or left-pad with zeros if XORing.
  221. if (xor_fixed_nonce_) {
  222. nonce_len = fixed_nonce_len_ - variable_nonce_len_;
  223. OPENSSL_memset(nonce, 0, nonce_len);
  224. } else {
  225. OPENSSL_memcpy(nonce, fixed_nonce_, fixed_nonce_len_);
  226. nonce_len += fixed_nonce_len_;
  227. }
  228. // Add the variable nonce.
  229. if (variable_nonce_included_in_record_) {
  230. if (in.size() < variable_nonce_len_) {
  231. // Publicly invalid.
  232. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
  233. return false;
  234. }
  235. OPENSSL_memcpy(nonce + nonce_len, in.data(), variable_nonce_len_);
  236. in = in.subspan(variable_nonce_len_);
  237. } else {
  238. assert(variable_nonce_len_ == 8);
  239. OPENSSL_memcpy(nonce + nonce_len, seqnum, variable_nonce_len_);
  240. }
  241. nonce_len += variable_nonce_len_;
  242. // XOR the fixed nonce, if necessary.
  243. if (xor_fixed_nonce_) {
  244. assert(nonce_len == fixed_nonce_len_);
  245. for (size_t i = 0; i < fixed_nonce_len_; i++) {
  246. nonce[i] ^= fixed_nonce_[i];
  247. }
  248. }
  249. // Decrypt in-place.
  250. size_t len;
  251. if (!EVP_AEAD_CTX_open(ctx_.get(), in.data(), &len, in.size(), nonce,
  252. nonce_len, in.data(), in.size(), ad, ad_len)) {
  253. return false;
  254. }
  255. *out = in.subspan(0, len);
  256. return true;
  257. }
  258. bool SSLAEADContext::SealScatter(uint8_t *out_prefix, uint8_t *out,
  259. uint8_t *out_suffix, uint8_t type,
  260. uint16_t record_version,
  261. const uint8_t seqnum[8], const uint8_t *in,
  262. size_t in_len, const uint8_t *extra_in,
  263. size_t extra_in_len) {
  264. const size_t prefix_len = ExplicitNonceLen();
  265. size_t suffix_len;
  266. if (!SuffixLen(&suffix_len, in_len, extra_in_len)) {
  267. OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
  268. return false;
  269. }
  270. if ((in != out && buffers_alias(in, in_len, out, in_len)) ||
  271. buffers_alias(in, in_len, out_prefix, prefix_len) ||
  272. buffers_alias(in, in_len, out_suffix, suffix_len)) {
  273. OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
  274. return false;
  275. }
  276. if (is_null_cipher() || FUZZER_MODE) {
  277. // Handle the initial NULL cipher.
  278. OPENSSL_memmove(out, in, in_len);
  279. OPENSSL_memmove(out_suffix, extra_in, extra_in_len);
  280. return true;
  281. }
  282. uint8_t ad[13];
  283. size_t ad_len = GetAdditionalData(ad, type, record_version, seqnum, in_len);
  284. // Assemble the nonce.
  285. uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
  286. size_t nonce_len = 0;
  287. // Prepend the fixed nonce, or left-pad with zeros if XORing.
  288. if (xor_fixed_nonce_) {
  289. nonce_len = fixed_nonce_len_ - variable_nonce_len_;
  290. OPENSSL_memset(nonce, 0, nonce_len);
  291. } else {
  292. OPENSSL_memcpy(nonce, fixed_nonce_, fixed_nonce_len_);
  293. nonce_len += fixed_nonce_len_;
  294. }
  295. // Select the variable nonce.
  296. if (random_variable_nonce_) {
  297. assert(variable_nonce_included_in_record_);
  298. if (!RAND_bytes(nonce + nonce_len, variable_nonce_len_)) {
  299. return false;
  300. }
  301. } else {
  302. // When sending we use the sequence number as the variable part of the
  303. // nonce.
  304. assert(variable_nonce_len_ == 8);
  305. OPENSSL_memcpy(nonce + nonce_len, seqnum, variable_nonce_len_);
  306. }
  307. nonce_len += variable_nonce_len_;
  308. // Emit the variable nonce if included in the record.
  309. if (variable_nonce_included_in_record_) {
  310. assert(!xor_fixed_nonce_);
  311. if (buffers_alias(in, in_len, out_prefix, variable_nonce_len_)) {
  312. OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
  313. return false;
  314. }
  315. OPENSSL_memcpy(out_prefix, nonce + fixed_nonce_len_,
  316. variable_nonce_len_);
  317. }
  318. // XOR the fixed nonce, if necessary.
  319. if (xor_fixed_nonce_) {
  320. assert(nonce_len == fixed_nonce_len_);
  321. for (size_t i = 0; i < fixed_nonce_len_; i++) {
  322. nonce[i] ^= fixed_nonce_[i];
  323. }
  324. }
  325. size_t written_suffix_len;
  326. bool result = !!EVP_AEAD_CTX_seal_scatter(
  327. ctx_.get(), out, out_suffix, &written_suffix_len, suffix_len, nonce,
  328. nonce_len, in, in_len, extra_in, extra_in_len, ad, ad_len);
  329. assert(!result || written_suffix_len == suffix_len);
  330. return result;
  331. }
  332. bool SSLAEADContext::Seal(uint8_t *out, size_t *out_len, size_t max_out_len,
  333. uint8_t type, uint16_t record_version,
  334. const uint8_t seqnum[8], const uint8_t *in,
  335. size_t in_len) {
  336. const size_t prefix_len = ExplicitNonceLen();
  337. size_t suffix_len;
  338. if (!SuffixLen(&suffix_len, in_len, 0)) {
  339. OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
  340. return false;
  341. }
  342. if (in_len + prefix_len < in_len ||
  343. in_len + prefix_len + suffix_len < in_len + prefix_len) {
  344. OPENSSL_PUT_ERROR(CIPHER, SSL_R_RECORD_TOO_LARGE);
  345. return false;
  346. }
  347. if (in_len + prefix_len + suffix_len > max_out_len) {
  348. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
  349. return false;
  350. }
  351. if (!SealScatter(out, out + prefix_len, out + prefix_len + in_len, type,
  352. record_version, seqnum, in, in_len, 0, 0)) {
  353. return false;
  354. }
  355. *out_len = prefix_len + in_len + suffix_len;
  356. return true;
  357. }
  358. bool SSLAEADContext::GetIV(const uint8_t **out_iv, size_t *out_iv_len) const {
  359. return !is_null_cipher() &&
  360. EVP_AEAD_CTX_get_iv(ctx_.get(), out_iv, out_iv_len);
  361. }
  362. } // namespace bssl