2015-03-03 19:20:26 +00:00
|
|
|
/* Copyright (c) 2015, Google Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
2015-09-15 06:48:04 +01:00
|
|
|
#include <openssl/ssl.h>
|
|
|
|
|
2015-03-03 19:20:26 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <openssl/aead.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
#include "../crypto/internal.h"
|
2015-03-03 19:20:26 +00:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
|
2017-07-19 03:45:18 +01:00
|
|
|
#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
|
|
|
|
#define FUZZER_MODE true
|
|
|
|
#else
|
|
|
|
#define FUZZER_MODE false
|
|
|
|
#endif
|
|
|
|
|
Support symbol prefixes
- In base.h, if BORINGSSL_PREFIX is defined, include
boringssl_prefix_symbols.h
- In all .S files, if BORINGSSL_PREFIX is defined, include
boringssl_prefix_symbols_asm.h
- In base.h, BSSL_NAMESPACE_BEGIN and BSSL_NAMESPACE_END are
defined with appropriate values depending on whether
BORINGSSL_PREFIX is defined; these macros are used in place
of 'namespace bssl {' and '}'
- Add util/make_prefix_headers.go, which takes a list of symbols
and auto-generates the header files mentioned above
- In CMakeLists.txt, if BORINGSSL_PREFIX and BORINGSSL_PREFIX_SYMBOLS
are defined, run util/make_prefix_headers.go to generate header
files
- In various CMakeLists.txt files, add "global_target" that all
targets depend on to give us a place to hook logic that must run
before all other targets (in particular, the header file generation
logic)
- Document this in BUILDING.md, including the fact that it is
the caller's responsibility to provide the symbol list and keep it
up to date
- Note that this scheme has not been tested on Windows, and likely
does not work on it; Windows support will need to be added in a
future commit
Change-Id: If66a7157f46b5b66230ef91e15826b910cf979a2
Reviewed-on: https://boringssl-review.googlesource.com/31364
Commit-Queue: David Benjamin <davidben@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
Reviewed-by: David Benjamin <davidben@google.com>
2018-08-27 02:53:36 +01:00
|
|
|
BSSL_NAMESPACE_BEGIN
|
Move libssl's internals into the bssl namespace.
This is horrible, but everything else I tried was worse. The goal with
this CL is to take the extern "C" out of ssl/internal.h and move most
symbols to namespace bssl, so we can start using C++ helpers and
destructors without worry.
Complications:
- Public API functions must be extern "C" and match their declaration in
ssl.h, which is unnamespaced. C++ really does not want you to
interleave namespaced and unnamespaced things. One can actually write
a namespaced extern "C" function, but this means, from C++'s
perspective, the function is namespaced. Trying to namespace the
public header would worked but ended up too deep a rabbithole.
- Our STACK_OF macros do not work right in namespaces.
- The typedefs for our exposed but opaque types are visible in the
header files and copied into consuming projects as forward
declarations. We ultimately want to give SSL a destructor, but
clobbering an unnamespaced ssl_st::~ssl_st seems bad manners.
- MSVC complains about ambiguous names if one typedefs SSL to bssl::SSL.
This CL opts for:
- ssl/*.cc must begin with #define BORINGSSL_INTERNAL_CXX_TYPES. This
informs the public headers to create forward declarations which are
compatible with our namespaces.
- For now, C++-defined type FOO ends up at bssl::FOO with a typedef
outside. Later I imagine we'll rename many of them.
- Internal functions get namespace bssl, so we stop worrying about
stomping the tls1_prf symbol. Exported C functions are stuck as they
are. Rather than try anything weird, bite the bullet and reorder files
which have a mix of public and private functions. I expect that over
time, the public functions will become fairly small as we move logic
to more idiomatic C++.
Files without any public C functions can just be written normally.
- To avoid MSVC troubles, some bssl types are renamed to CPlusPlusStyle
in advance of them being made idiomatic C++.
Bug: 132
Change-Id: Ic931895e117c38b14ff8d6e5a273e868796c7581
Reviewed-on: https://boringssl-review.googlesource.com/18124
Reviewed-by: David Benjamin <davidben@google.com>
2017-07-18 21:34:25 +01:00
|
|
|
|
2017-09-11 18:53:08 +01:00
|
|
|
SSLAEADContext::SSLAEADContext(uint16_t version_arg, bool is_dtls_arg,
|
2017-07-19 03:45:18 +01:00
|
|
|
const SSL_CIPHER *cipher_arg)
|
|
|
|
: cipher_(cipher_arg),
|
|
|
|
version_(version_arg),
|
2017-09-11 18:53:08 +01:00
|
|
|
is_dtls_(is_dtls_arg),
|
2017-07-19 03:45:18 +01:00
|
|
|
variable_nonce_included_in_record_(false),
|
|
|
|
random_variable_nonce_(false),
|
2018-04-05 04:55:06 +01:00
|
|
|
xor_fixed_nonce_(false),
|
2017-07-19 03:45:18 +01:00
|
|
|
omit_length_in_ad_(false),
|
|
|
|
omit_ad_(false),
|
2018-04-05 04:55:06 +01:00
|
|
|
ad_is_header_(false) {
|
2017-07-19 03:45:18 +01:00
|
|
|
OPENSSL_memset(fixed_nonce_, 0, sizeof(fixed_nonce_));
|
|
|
|
}
|
|
|
|
|
|
|
|
SSLAEADContext::~SSLAEADContext() {}
|
|
|
|
|
2017-09-11 18:53:08 +01:00
|
|
|
UniquePtr<SSLAEADContext> SSLAEADContext::CreateNullCipher(bool is_dtls) {
|
|
|
|
return MakeUnique<SSLAEADContext>(0 /* version */, is_dtls,
|
|
|
|
nullptr /* cipher */);
|
2017-07-19 03:45:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
UniquePtr<SSLAEADContext> SSLAEADContext::Create(
|
2018-09-06 00:44:15 +01:00
|
|
|
enum evp_aead_direction_t direction, uint16_t version, bool is_dtls,
|
2017-09-28 00:02:51 +01:00
|
|
|
const SSL_CIPHER *cipher, Span<const uint8_t> enc_key,
|
|
|
|
Span<const uint8_t> mac_key, Span<const uint8_t> fixed_iv) {
|
2015-03-03 19:20:26 +00:00
|
|
|
const EVP_AEAD *aead;
|
2017-09-11 18:53:08 +01:00
|
|
|
uint16_t protocol_version;
|
2016-10-28 22:17:14 +01:00
|
|
|
size_t expected_mac_key_len, expected_fixed_iv_len;
|
2017-09-11 18:53:08 +01:00
|
|
|
if (!ssl_protocol_version_from_wire(&protocol_version, version) ||
|
|
|
|
!ssl_cipher_get_evp_aead(&aead, &expected_mac_key_len,
|
|
|
|
&expected_fixed_iv_len, cipher, protocol_version,
|
2017-05-24 21:54:35 +01:00
|
|
|
is_dtls) ||
|
2017-08-29 21:33:21 +01:00
|
|
|
// Ensure the caller returned correct key sizes.
|
2017-09-28 00:02:51 +01:00
|
|
|
expected_fixed_iv_len != fixed_iv.size() ||
|
|
|
|
expected_mac_key_len != mac_key.size()) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
2017-07-19 03:45:18 +01:00
|
|
|
return nullptr;
|
2015-03-03 19:20:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t merged_key[EVP_AEAD_MAX_KEY_LENGTH];
|
2017-09-28 00:02:51 +01:00
|
|
|
if (!mac_key.empty()) {
|
2017-08-29 21:33:21 +01:00
|
|
|
// This is a "stateful" AEAD (for compatibility with pre-AEAD cipher
|
|
|
|
// suites).
|
2017-09-28 00:02:51 +01:00
|
|
|
if (mac_key.size() + enc_key.size() + fixed_iv.size() >
|
|
|
|
sizeof(merged_key)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
2017-07-19 03:45:18 +01:00
|
|
|
return nullptr;
|
2015-03-03 19:20:26 +00:00
|
|
|
}
|
2017-09-28 00:02:51 +01:00
|
|
|
OPENSSL_memcpy(merged_key, mac_key.data(), mac_key.size());
|
|
|
|
OPENSSL_memcpy(merged_key + mac_key.size(), enc_key.data(), enc_key.size());
|
|
|
|
OPENSSL_memcpy(merged_key + mac_key.size() + enc_key.size(),
|
|
|
|
fixed_iv.data(), fixed_iv.size());
|
|
|
|
enc_key = MakeConstSpan(merged_key,
|
|
|
|
enc_key.size() + mac_key.size() + fixed_iv.size());
|
2015-03-03 19:20:26 +00:00
|
|
|
}
|
|
|
|
|
2017-07-19 03:45:18 +01:00
|
|
|
UniquePtr<SSLAEADContext> aead_ctx =
|
2017-09-11 18:53:08 +01:00
|
|
|
MakeUnique<SSLAEADContext>(version, is_dtls, cipher);
|
2017-07-19 03:45:18 +01:00
|
|
|
if (!aead_ctx) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
2017-07-19 03:45:18 +01:00
|
|
|
return nullptr;
|
2015-03-03 19:20:26 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 18:53:08 +01:00
|
|
|
assert(aead_ctx->ProtocolVersion() == protocol_version);
|
|
|
|
|
2015-03-03 19:20:26 +00:00
|
|
|
if (!EVP_AEAD_CTX_init_with_direction(
|
2017-09-28 00:02:51 +01:00
|
|
|
aead_ctx->ctx_.get(), aead, enc_key.data(), enc_key.size(),
|
2015-03-03 19:20:26 +00:00
|
|
|
EVP_AEAD_DEFAULT_TAG_LENGTH, direction)) {
|
2017-07-19 03:45:18 +01:00
|
|
|
return nullptr;
|
2015-03-03 19:20:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(EVP_AEAD_nonce_length(aead) <= EVP_AEAD_MAX_NONCE_LENGTH);
|
2017-07-15 00:36:07 +01:00
|
|
|
static_assert(EVP_AEAD_MAX_NONCE_LENGTH < 256,
|
|
|
|
"variable_nonce_len doesn't fit in uint8_t");
|
2017-07-19 03:45:18 +01:00
|
|
|
aead_ctx->variable_nonce_len_ = (uint8_t)EVP_AEAD_nonce_length(aead);
|
2017-09-28 00:02:51 +01:00
|
|
|
if (mac_key.empty()) {
|
|
|
|
assert(fixed_iv.size() <= sizeof(aead_ctx->fixed_nonce_));
|
|
|
|
OPENSSL_memcpy(aead_ctx->fixed_nonce_, fixed_iv.data(), fixed_iv.size());
|
|
|
|
aead_ctx->fixed_nonce_len_ = fixed_iv.size();
|
2015-12-10 04:02:39 +00:00
|
|
|
|
|
|
|
if (cipher->algorithm_enc & SSL_CHACHA20POLY1305) {
|
2017-08-29 21:33:21 +01:00
|
|
|
// The fixed nonce into the actual nonce (the sequence number).
|
2017-07-19 03:45:18 +01:00
|
|
|
aead_ctx->xor_fixed_nonce_ = true;
|
|
|
|
aead_ctx->variable_nonce_len_ = 8;
|
2015-12-10 04:02:39 +00:00
|
|
|
} else {
|
2017-08-29 21:33:21 +01:00
|
|
|
// The fixed IV is prepended to the nonce.
|
2017-09-28 00:02:51 +01:00
|
|
|
assert(fixed_iv.size() <= aead_ctx->variable_nonce_len_);
|
|
|
|
aead_ctx->variable_nonce_len_ -= fixed_iv.size();
|
2015-12-10 04:02:39 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// AES-GCM uses an explicit nonce.
|
2015-06-21 20:13:57 +01:00
|
|
|
if (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) {
|
2017-07-19 03:45:18 +01:00
|
|
|
aead_ctx->variable_nonce_included_in_record_ = true;
|
2015-06-21 20:13:57 +01:00
|
|
|
}
|
2016-05-24 17:43:04 +01:00
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// The TLS 1.3 construction XORs the fixed nonce into the sequence number
|
|
|
|
// and omits the additional data.
|
2017-09-11 18:53:08 +01:00
|
|
|
if (protocol_version >= TLS1_3_VERSION) {
|
2017-07-19 03:45:18 +01:00
|
|
|
aead_ctx->xor_fixed_nonce_ = true;
|
|
|
|
aead_ctx->variable_nonce_len_ = 8;
|
|
|
|
aead_ctx->variable_nonce_included_in_record_ = false;
|
2018-03-27 18:15:26 +01:00
|
|
|
if (ssl_is_draft28(version)) {
|
2018-04-05 04:55:06 +01:00
|
|
|
aead_ctx->ad_is_header_ = true;
|
2018-03-27 18:15:26 +01:00
|
|
|
} else {
|
|
|
|
aead_ctx->omit_ad_ = true;
|
|
|
|
}
|
2017-09-28 00:02:51 +01:00
|
|
|
assert(fixed_iv.size() >= aead_ctx->variable_nonce_len_);
|
2016-05-24 17:43:04 +01:00
|
|
|
}
|
2015-03-03 19:20:26 +00:00
|
|
|
} else {
|
2017-09-11 18:53:08 +01:00
|
|
|
assert(protocol_version < TLS1_3_VERSION);
|
2017-07-19 03:45:18 +01:00
|
|
|
aead_ctx->variable_nonce_included_in_record_ = true;
|
|
|
|
aead_ctx->random_variable_nonce_ = true;
|
|
|
|
aead_ctx->omit_length_in_ad_ = true;
|
2015-03-03 19:20:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return aead_ctx;
|
|
|
|
}
|
|
|
|
|
2018-07-14 16:23:01 +01:00
|
|
|
UniquePtr<SSLAEADContext> SSLAEADContext::CreatePlaceholderForQUIC(
|
|
|
|
uint16_t version, const SSL_CIPHER *cipher) {
|
|
|
|
return MakeUnique<SSLAEADContext>(version, false, cipher);
|
|
|
|
}
|
|
|
|
|
2017-09-11 18:53:08 +01:00
|
|
|
void SSLAEADContext::SetVersionIfNullCipher(uint16_t version) {
|
|
|
|
if (is_null_cipher()) {
|
|
|
|
version_ = version;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t SSLAEADContext::ProtocolVersion() const {
|
|
|
|
uint16_t protocol_version;
|
|
|
|
if(!ssl_protocol_version_from_wire(&protocol_version, version_)) {
|
|
|
|
assert(false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return protocol_version;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t SSLAEADContext::RecordVersion() const {
|
|
|
|
if (version_ == 0) {
|
|
|
|
assert(is_null_cipher());
|
|
|
|
return is_dtls_ ? DTLS1_VERSION : TLS1_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ProtocolVersion() <= TLS1_2_VERSION) {
|
|
|
|
return version_;
|
|
|
|
}
|
|
|
|
|
2017-12-04 16:15:37 +00:00
|
|
|
return TLS1_2_VERSION;
|
2017-09-11 18:53:08 +01:00
|
|
|
}
|
|
|
|
|
2017-07-19 03:45:18 +01:00
|
|
|
size_t SSLAEADContext::ExplicitNonceLen() const {
|
|
|
|
if (!FUZZER_MODE && variable_nonce_included_in_record_) {
|
|
|
|
return variable_nonce_len_;
|
2015-03-03 19:20:26 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:27:54 +01:00
|
|
|
bool SSLAEADContext::SuffixLen(size_t *out_suffix_len, const size_t in_len,
|
|
|
|
const size_t extra_in_len) const {
|
|
|
|
if (is_null_cipher() || FUZZER_MODE) {
|
|
|
|
*out_suffix_len = extra_in_len;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return !!EVP_AEAD_CTX_tag_len(ctx_.get(), out_suffix_len, in_len,
|
|
|
|
extra_in_len);
|
2017-06-30 13:29:50 +01:00
|
|
|
}
|
|
|
|
|
2018-04-05 04:55:06 +01:00
|
|
|
bool SSLAEADContext::CiphertextLen(size_t *out_len, const size_t in_len,
|
|
|
|
const size_t extra_in_len) const {
|
|
|
|
size_t len;
|
|
|
|
if (!SuffixLen(&len, in_len, extra_in_len)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
len += ExplicitNonceLen();
|
|
|
|
len += in_len;
|
|
|
|
if (len < in_len || len >= 0xffff) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*out_len = len;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-19 03:45:18 +01:00
|
|
|
size_t SSLAEADContext::MaxOverhead() const {
|
2017-07-22 00:27:54 +01:00
|
|
|
return ExplicitNonceLen() +
|
|
|
|
(is_null_cipher() || FUZZER_MODE
|
|
|
|
? 0
|
|
|
|
: EVP_AEAD_max_overhead(EVP_AEAD_CTX_aead(ctx_.get())));
|
2015-03-03 19:20:26 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 04:55:06 +01:00
|
|
|
Span<const uint8_t> SSLAEADContext::GetAdditionalData(
|
|
|
|
uint8_t storage[13], uint8_t type, uint16_t record_version,
|
|
|
|
const uint8_t seqnum[8], size_t plaintext_len, Span<const uint8_t> header) {
|
|
|
|
if (ad_is_header_) {
|
|
|
|
return header;
|
2016-05-24 17:43:04 +01:00
|
|
|
}
|
|
|
|
|
2018-04-05 04:55:06 +01:00
|
|
|
if (omit_ad_) {
|
|
|
|
return {};
|
2018-03-27 18:15:26 +01:00
|
|
|
}
|
2018-04-05 04:55:06 +01:00
|
|
|
|
|
|
|
OPENSSL_memcpy(storage, seqnum, 8);
|
|
|
|
size_t len = 8;
|
|
|
|
storage[len++] = type;
|
2018-06-26 05:07:40 +01:00
|
|
|
storage[len++] = static_cast<uint8_t>((record_version >> 8));
|
|
|
|
storage[len++] = static_cast<uint8_t>(record_version);
|
2018-04-05 04:55:06 +01:00
|
|
|
if (!omit_length_in_ad_) {
|
|
|
|
storage[len++] = static_cast<uint8_t>((plaintext_len >> 8));
|
|
|
|
storage[len++] = static_cast<uint8_t>(plaintext_len);
|
2015-03-03 19:20:26 +00:00
|
|
|
}
|
2018-04-05 04:55:06 +01:00
|
|
|
return MakeConstSpan(storage, len);
|
2015-03-03 19:20:26 +00:00
|
|
|
}
|
|
|
|
|
2017-10-04 23:14:28 +01:00
|
|
|
bool SSLAEADContext::Open(Span<uint8_t> *out, uint8_t type,
|
|
|
|
uint16_t record_version, const uint8_t seqnum[8],
|
2018-04-05 04:55:06 +01:00
|
|
|
Span<const uint8_t> header, Span<uint8_t> in) {
|
2017-07-19 03:45:18 +01:00
|
|
|
if (is_null_cipher() || FUZZER_MODE) {
|
2017-08-29 21:33:21 +01:00
|
|
|
// Handle the initial NULL cipher.
|
2017-10-04 23:14:28 +01:00
|
|
|
*out = in;
|
2017-07-19 03:45:18 +01:00
|
|
|
return true;
|
2015-03-03 19:20:26 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// TLS 1.2 AEADs include the length in the AD and are assumed to have fixed
|
|
|
|
// overhead. Otherwise the parameter is unused.
|
2015-03-03 19:20:26 +00:00
|
|
|
size_t plaintext_len = 0;
|
2017-07-19 03:45:18 +01:00
|
|
|
if (!omit_length_in_ad_) {
|
|
|
|
size_t overhead = MaxOverhead();
|
2017-10-04 23:14:28 +01:00
|
|
|
if (in.size() < overhead) {
|
2017-08-29 21:33:21 +01:00
|
|
|
// Publicly invalid.
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
|
2017-07-19 03:45:18 +01:00
|
|
|
return false;
|
2015-03-03 19:20:26 +00:00
|
|
|
}
|
2017-10-04 23:14:28 +01:00
|
|
|
plaintext_len = in.size() - overhead;
|
2015-03-03 19:20:26 +00:00
|
|
|
}
|
2018-04-05 04:55:06 +01:00
|
|
|
|
|
|
|
uint8_t ad_storage[13];
|
|
|
|
Span<const uint8_t> ad = GetAdditionalData(ad_storage, type, record_version,
|
|
|
|
seqnum, plaintext_len, header);
|
2015-03-03 19:20:26 +00:00
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Assemble the nonce.
|
2015-03-03 19:20:26 +00:00
|
|
|
uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
|
|
|
|
size_t nonce_len = 0;
|
2015-12-10 04:02:39 +00:00
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Prepend the fixed nonce, or left-pad with zeros if XORing.
|
2017-07-19 03:45:18 +01:00
|
|
|
if (xor_fixed_nonce_) {
|
|
|
|
nonce_len = fixed_nonce_len_ - variable_nonce_len_;
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(nonce, 0, nonce_len);
|
2015-12-10 04:02:39 +00:00
|
|
|
} else {
|
2017-07-19 03:45:18 +01:00
|
|
|
OPENSSL_memcpy(nonce, fixed_nonce_, fixed_nonce_len_);
|
|
|
|
nonce_len += fixed_nonce_len_;
|
2015-12-10 04:02:39 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Add the variable nonce.
|
2017-07-19 03:45:18 +01:00
|
|
|
if (variable_nonce_included_in_record_) {
|
2017-10-04 23:14:28 +01:00
|
|
|
if (in.size() < variable_nonce_len_) {
|
2017-08-29 21:33:21 +01:00
|
|
|
// Publicly invalid.
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
|
2017-07-19 03:45:18 +01:00
|
|
|
return false;
|
2015-03-03 19:20:26 +00:00
|
|
|
}
|
2017-10-04 23:14:28 +01:00
|
|
|
OPENSSL_memcpy(nonce + nonce_len, in.data(), variable_nonce_len_);
|
|
|
|
in = in.subspan(variable_nonce_len_);
|
2015-03-03 19:20:26 +00:00
|
|
|
} else {
|
2017-07-19 03:45:18 +01:00
|
|
|
assert(variable_nonce_len_ == 8);
|
|
|
|
OPENSSL_memcpy(nonce + nonce_len, seqnum, variable_nonce_len_);
|
2015-03-03 19:20:26 +00:00
|
|
|
}
|
2017-07-19 03:45:18 +01:00
|
|
|
nonce_len += variable_nonce_len_;
|
2015-03-03 19:20:26 +00:00
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// XOR the fixed nonce, if necessary.
|
2017-07-19 03:45:18 +01:00
|
|
|
if (xor_fixed_nonce_) {
|
|
|
|
assert(nonce_len == fixed_nonce_len_);
|
|
|
|
for (size_t i = 0; i < fixed_nonce_len_; i++) {
|
|
|
|
nonce[i] ^= fixed_nonce_[i];
|
2015-12-10 04:02:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Decrypt in-place.
|
2016-06-06 23:54:51 +01:00
|
|
|
size_t len;
|
2017-10-04 23:14:28 +01:00
|
|
|
if (!EVP_AEAD_CTX_open(ctx_.get(), in.data(), &len, in.size(), nonce,
|
2018-04-05 04:55:06 +01:00
|
|
|
nonce_len, in.data(), in.size(), ad.data(),
|
|
|
|
ad.size())) {
|
2017-07-19 03:45:18 +01:00
|
|
|
return false;
|
2016-06-06 23:54:51 +01:00
|
|
|
}
|
2017-10-04 23:14:28 +01:00
|
|
|
*out = in.subspan(0, len);
|
2017-07-19 03:45:18 +01:00
|
|
|
return true;
|
2015-03-03 19:20:26 +00:00
|
|
|
}
|
|
|
|
|
2017-07-19 03:45:18 +01:00
|
|
|
bool SSLAEADContext::SealScatter(uint8_t *out_prefix, uint8_t *out,
|
2017-07-22 00:27:54 +01:00
|
|
|
uint8_t *out_suffix, uint8_t type,
|
2017-09-11 18:53:08 +01:00
|
|
|
uint16_t record_version,
|
2018-04-05 04:55:06 +01:00
|
|
|
const uint8_t seqnum[8],
|
|
|
|
Span<const uint8_t> header, const uint8_t *in,
|
2017-09-11 18:53:08 +01:00
|
|
|
size_t in_len, const uint8_t *extra_in,
|
|
|
|
size_t extra_in_len) {
|
2017-07-22 00:27:54 +01:00
|
|
|
const size_t prefix_len = ExplicitNonceLen();
|
|
|
|
size_t suffix_len;
|
|
|
|
if (!SuffixLen(&suffix_len, in_len, extra_in_len)) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
|
2017-07-19 03:45:18 +01:00
|
|
|
return false;
|
2017-06-30 13:29:50 +01:00
|
|
|
}
|
2017-07-22 00:27:54 +01:00
|
|
|
if ((in != out && buffers_alias(in, in_len, out, in_len)) ||
|
|
|
|
buffers_alias(in, in_len, out_prefix, prefix_len) ||
|
|
|
|
buffers_alias(in, in_len, out_suffix, suffix_len)) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
|
2017-07-19 03:45:18 +01:00
|
|
|
return false;
|
2017-06-30 13:29:50 +01:00
|
|
|
}
|
|
|
|
|
2017-07-19 03:45:18 +01:00
|
|
|
if (is_null_cipher() || FUZZER_MODE) {
|
2017-08-29 21:33:21 +01:00
|
|
|
// Handle the initial NULL cipher.
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memmove(out, in, in_len);
|
2017-06-30 13:29:50 +01:00
|
|
|
OPENSSL_memmove(out_suffix, extra_in, extra_in_len);
|
2017-07-19 03:45:18 +01:00
|
|
|
return true;
|
2015-03-03 19:20:26 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 04:55:06 +01:00
|
|
|
uint8_t ad_storage[13];
|
|
|
|
Span<const uint8_t> ad = GetAdditionalData(ad_storage, type, record_version,
|
|
|
|
seqnum, in_len, header);
|
2015-03-03 19:20:26 +00:00
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Assemble the nonce.
|
2015-03-03 19:20:26 +00:00
|
|
|
uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
|
|
|
|
size_t nonce_len = 0;
|
2015-12-10 04:02:39 +00:00
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Prepend the fixed nonce, or left-pad with zeros if XORing.
|
2017-07-19 03:45:18 +01:00
|
|
|
if (xor_fixed_nonce_) {
|
|
|
|
nonce_len = fixed_nonce_len_ - variable_nonce_len_;
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(nonce, 0, nonce_len);
|
2015-12-10 04:02:39 +00:00
|
|
|
} else {
|
2017-07-19 03:45:18 +01:00
|
|
|
OPENSSL_memcpy(nonce, fixed_nonce_, fixed_nonce_len_);
|
|
|
|
nonce_len += fixed_nonce_len_;
|
2015-12-10 04:02:39 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Select the variable nonce.
|
2017-07-19 03:45:18 +01:00
|
|
|
if (random_variable_nonce_) {
|
|
|
|
assert(variable_nonce_included_in_record_);
|
|
|
|
if (!RAND_bytes(nonce + nonce_len, variable_nonce_len_)) {
|
|
|
|
return false;
|
2015-03-03 19:20:26 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-08-29 21:33:21 +01:00
|
|
|
// When sending we use the sequence number as the variable part of the
|
|
|
|
// nonce.
|
2017-07-19 03:45:18 +01:00
|
|
|
assert(variable_nonce_len_ == 8);
|
|
|
|
OPENSSL_memcpy(nonce + nonce_len, seqnum, variable_nonce_len_);
|
2015-03-03 19:20:26 +00:00
|
|
|
}
|
2017-07-19 03:45:18 +01:00
|
|
|
nonce_len += variable_nonce_len_;
|
2015-03-03 19:20:26 +00:00
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Emit the variable nonce if included in the record.
|
2017-07-19 03:45:18 +01:00
|
|
|
if (variable_nonce_included_in_record_) {
|
|
|
|
assert(!xor_fixed_nonce_);
|
|
|
|
if (buffers_alias(in, in_len, out_prefix, variable_nonce_len_)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
|
2017-07-19 03:45:18 +01:00
|
|
|
return false;
|
2015-03-03 19:20:26 +00:00
|
|
|
}
|
2017-07-19 03:45:18 +01:00
|
|
|
OPENSSL_memcpy(out_prefix, nonce + fixed_nonce_len_,
|
|
|
|
variable_nonce_len_);
|
2015-03-03 19:20:26 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// XOR the fixed nonce, if necessary.
|
2017-07-19 03:45:18 +01:00
|
|
|
if (xor_fixed_nonce_) {
|
|
|
|
assert(nonce_len == fixed_nonce_len_);
|
|
|
|
for (size_t i = 0; i < fixed_nonce_len_; i++) {
|
|
|
|
nonce[i] ^= fixed_nonce_[i];
|
2015-12-10 04:02:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:27:54 +01:00
|
|
|
size_t written_suffix_len;
|
|
|
|
bool result = !!EVP_AEAD_CTX_seal_scatter(
|
|
|
|
ctx_.get(), out, out_suffix, &written_suffix_len, suffix_len, nonce,
|
2018-04-05 04:55:06 +01:00
|
|
|
nonce_len, in, in_len, extra_in, extra_in_len, ad.data(), ad.size());
|
2017-07-22 00:27:54 +01:00
|
|
|
assert(!result || written_suffix_len == suffix_len);
|
|
|
|
return result;
|
2017-06-30 13:29:50 +01:00
|
|
|
}
|
|
|
|
|
2017-07-19 03:45:18 +01:00
|
|
|
bool SSLAEADContext::Seal(uint8_t *out, size_t *out_len, size_t max_out_len,
|
2017-09-11 18:53:08 +01:00
|
|
|
uint8_t type, uint16_t record_version,
|
2018-04-05 04:55:06 +01:00
|
|
|
const uint8_t seqnum[8], Span<const uint8_t> header,
|
|
|
|
const uint8_t *in, size_t in_len) {
|
2017-07-22 00:27:54 +01:00
|
|
|
const size_t prefix_len = ExplicitNonceLen();
|
|
|
|
size_t suffix_len;
|
|
|
|
if (!SuffixLen(&suffix_len, in_len, 0)) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (in_len + prefix_len < in_len ||
|
|
|
|
in_len + prefix_len + suffix_len < in_len + prefix_len) {
|
2017-06-30 13:29:50 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, SSL_R_RECORD_TOO_LARGE);
|
2017-07-19 03:45:18 +01:00
|
|
|
return false;
|
2017-06-30 13:29:50 +01:00
|
|
|
}
|
2017-07-22 00:27:54 +01:00
|
|
|
if (in_len + prefix_len + suffix_len > max_out_len) {
|
2017-06-30 13:29:50 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
|
2017-07-19 03:45:18 +01:00
|
|
|
return false;
|
2017-06-30 13:29:50 +01:00
|
|
|
}
|
|
|
|
|
2017-07-22 00:27:54 +01:00
|
|
|
if (!SealScatter(out, out + prefix_len, out + prefix_len + in_len, type,
|
2018-04-05 04:55:06 +01:00
|
|
|
record_version, seqnum, header, in, in_len, 0, 0)) {
|
2017-07-19 03:45:18 +01:00
|
|
|
return false;
|
2015-03-03 19:20:26 +00:00
|
|
|
}
|
2017-06-30 13:29:50 +01:00
|
|
|
*out_len = prefix_len + in_len + suffix_len;
|
2017-07-19 03:45:18 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SSLAEADContext::GetIV(const uint8_t **out_iv, size_t *out_iv_len) const {
|
|
|
|
return !is_null_cipher() &&
|
|
|
|
EVP_AEAD_CTX_get_iv(ctx_.get(), out_iv, out_iv_len);
|
2015-03-03 19:20:26 +00:00
|
|
|
}
|
Move libssl's internals into the bssl namespace.
This is horrible, but everything else I tried was worse. The goal with
this CL is to take the extern "C" out of ssl/internal.h and move most
symbols to namespace bssl, so we can start using C++ helpers and
destructors without worry.
Complications:
- Public API functions must be extern "C" and match their declaration in
ssl.h, which is unnamespaced. C++ really does not want you to
interleave namespaced and unnamespaced things. One can actually write
a namespaced extern "C" function, but this means, from C++'s
perspective, the function is namespaced. Trying to namespace the
public header would worked but ended up too deep a rabbithole.
- Our STACK_OF macros do not work right in namespaces.
- The typedefs for our exposed but opaque types are visible in the
header files and copied into consuming projects as forward
declarations. We ultimately want to give SSL a destructor, but
clobbering an unnamespaced ssl_st::~ssl_st seems bad manners.
- MSVC complains about ambiguous names if one typedefs SSL to bssl::SSL.
This CL opts for:
- ssl/*.cc must begin with #define BORINGSSL_INTERNAL_CXX_TYPES. This
informs the public headers to create forward declarations which are
compatible with our namespaces.
- For now, C++-defined type FOO ends up at bssl::FOO with a typedef
outside. Later I imagine we'll rename many of them.
- Internal functions get namespace bssl, so we stop worrying about
stomping the tls1_prf symbol. Exported C functions are stuck as they
are. Rather than try anything weird, bite the bullet and reorder files
which have a mix of public and private functions. I expect that over
time, the public functions will become fairly small as we move logic
to more idiomatic C++.
Files without any public C functions can just be written normally.
- To avoid MSVC troubles, some bssl types are renamed to CPlusPlusStyle
in advance of them being made idiomatic C++.
Bug: 132
Change-Id: Ic931895e117c38b14ff8d6e5a273e868796c7581
Reviewed-on: https://boringssl-review.googlesource.com/18124
Reviewed-by: David Benjamin <davidben@google.com>
2017-07-18 21:34:25 +01:00
|
|
|
|
Support symbol prefixes
- In base.h, if BORINGSSL_PREFIX is defined, include
boringssl_prefix_symbols.h
- In all .S files, if BORINGSSL_PREFIX is defined, include
boringssl_prefix_symbols_asm.h
- In base.h, BSSL_NAMESPACE_BEGIN and BSSL_NAMESPACE_END are
defined with appropriate values depending on whether
BORINGSSL_PREFIX is defined; these macros are used in place
of 'namespace bssl {' and '}'
- Add util/make_prefix_headers.go, which takes a list of symbols
and auto-generates the header files mentioned above
- In CMakeLists.txt, if BORINGSSL_PREFIX and BORINGSSL_PREFIX_SYMBOLS
are defined, run util/make_prefix_headers.go to generate header
files
- In various CMakeLists.txt files, add "global_target" that all
targets depend on to give us a place to hook logic that must run
before all other targets (in particular, the header file generation
logic)
- Document this in BUILDING.md, including the fact that it is
the caller's responsibility to provide the symbol list and keep it
up to date
- Note that this scheme has not been tested on Windows, and likely
does not work on it; Windows support will need to be added in a
future commit
Change-Id: If66a7157f46b5b66230ef91e15826b910cf979a2
Reviewed-on: https://boringssl-review.googlesource.com/31364
Commit-Queue: David Benjamin <davidben@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
Reviewed-by: David Benjamin <davidben@google.com>
2018-08-27 02:53:36 +01:00
|
|
|
BSSL_NAMESPACE_END
|