diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/build-data/policy/bsi.txt | 3 | ||||
-rw-r--r-- | src/lib/pubkey/ecdh/ecdh.h | 11 | ||||
-rw-r--r-- | src/lib/tls/info.txt | 1 | ||||
-rw-r--r-- | src/lib/tls/msg_client_hello.cpp | 16 | ||||
-rw-r--r-- | src/lib/tls/msg_client_kex.cpp | 5 | ||||
-rw-r--r-- | src/lib/tls/msg_server_hello.cpp | 8 | ||||
-rw-r--r-- | src/lib/tls/msg_server_kex.cpp | 5 | ||||
-rw-r--r-- | src/lib/tls/tls_cbc.cpp | 403 | ||||
-rw-r--r-- | src/lib/tls/tls_cbc.h | 168 | ||||
-rw-r--r-- | src/lib/tls/tls_channel.cpp | 13 | ||||
-rw-r--r-- | src/lib/tls/tls_extensions.cpp | 40 | ||||
-rw-r--r-- | src/lib/tls/tls_extensions.h | 32 | ||||
-rw-r--r-- | src/lib/tls/tls_messages.h | 18 | ||||
-rw-r--r-- | src/lib/tls/tls_policy.cpp | 14 | ||||
-rw-r--r-- | src/lib/tls/tls_policy.h | 8 | ||||
-rw-r--r-- | src/lib/tls/tls_record.cpp | 393 | ||||
-rw-r--r-- | src/lib/tls/tls_record.h | 34 | ||||
-rw-r--r-- | src/tests/data/tls/client_hello.vec | 4 | ||||
-rw-r--r-- | src/tests/data/tls/server_hello.vec | 4 | ||||
-rw-r--r-- | src/tests/unit_tls.cpp | 273 |
20 files changed, 1001 insertions, 452 deletions
diff --git a/src/build-data/policy/bsi.txt b/src/build-data/policy/bsi.txt index 16671fd13..c2b31e4c6 100644 --- a/src/build-data/policy/bsi.txt +++ b/src/build-data/policy/bsi.txt @@ -78,6 +78,7 @@ blowfish camellia cascade cast +des gost_28147 idea idea_sse2 @@ -110,6 +111,7 @@ rc4 salsa20 # kdf +hkdf kdf1 kdf2 prf_x942 @@ -149,6 +151,7 @@ siphash x919_mac # rng +hmac_rng x931_rng # entropy sources diff --git a/src/lib/pubkey/ecdh/ecdh.h b/src/lib/pubkey/ecdh/ecdh.h index 2f892436c..bdd9ea047 100644 --- a/src/lib/pubkey/ecdh/ecdh.h +++ b/src/lib/pubkey/ecdh/ecdh.h @@ -55,6 +55,12 @@ class BOTAN_DLL ECDH_PublicKey : public virtual EC_PublicKey std::vector<byte> public_value() const { return unlock(EC2OSP(public_point(), PointGFp::UNCOMPRESSED)); } + /** + * @return public point value + */ + std::vector<byte> public_value(PointGFp::Compression_Type type) const + { return unlock(EC2OSP(public_point(), type)); } + protected: ECDH_PublicKey(); }; @@ -84,7 +90,10 @@ class BOTAN_DLL ECDH_PrivateKey : public ECDH_PublicKey, EC_PrivateKey(rng, domain, x) {} std::vector<byte> public_value() const override - { return ECDH_PublicKey::public_value(); } + { return ECDH_PublicKey::public_value(PointGFp::UNCOMPRESSED); } + + std::vector<byte> public_value(PointGFp::Compression_Type type) const + { return ECDH_PublicKey::public_value(type); } }; } diff --git a/src/lib/tls/info.txt b/src/lib/tls/info.txt index 667726318..ad0d266fa 100644 --- a/src/lib/tls/info.txt +++ b/src/lib/tls/info.txt @@ -22,6 +22,7 @@ tls_version.h </header:public> <header:internal> +tls_cbc.h tls_extensions.h tls_handshake_hash.h tls_handshake_io.h diff --git a/src/lib/tls/msg_client_hello.cpp b/src/lib/tls/msg_client_hello.cpp index b493fd3ee..36335e7ce 100644 --- a/src/lib/tls/msg_client_hello.cpp +++ b/src/lib/tls/msg_client_hello.cpp @@ -98,8 +98,12 @@ Client_Hello::Client_Hello(Handshake_IO& io, if(reneg_info.empty() && !next_protocols.empty()) m_extensions.add(new Application_Layer_Protocol_Notification(next_protocols)); + if(m_version.supports_negotiable_signature_algorithms()) + m_extensions.add(new Signature_Algorithms(policy.allowed_signature_hashes(), + policy.allowed_signature_methods())); + if(m_version.is_datagram_protocol()) - m_extensions.add(new SRTP_Protection_Profiles(policy.srtp_profiles())); + m_extensions.add(new SRTP_Protection_Profiles(policy.srtp_profiles())); #if defined(BOTAN_HAS_SRP6) m_extensions.add(new SRP_Identifier(client_settings.srp_identifier())); @@ -112,6 +116,11 @@ Client_Hello::Client_Hello(Handshake_IO& io, m_extensions.add(new Supported_Elliptic_Curves(policy.allowed_ecc_curves())); + if(!policy.allowed_ecc_curves().empty() && policy.use_ecc_point_compression()) + { + m_extensions.add(new Supported_Point_Formats()); + } + if(m_version.supports_negotiable_signature_algorithms()) m_extensions.add(new Signature_Algorithms(policy.allowed_signature_hashes(), policy.allowed_signature_methods())); @@ -156,6 +165,11 @@ Client_Hello::Client_Hello(Handshake_IO& io, m_extensions.add(new Session_Ticket(session.session_ticket())); m_extensions.add(new Supported_Elliptic_Curves(policy.allowed_ecc_curves())); + if(!policy.allowed_ecc_curves().empty() && policy.use_ecc_point_compression()) + { + m_extensions.add(new Supported_Point_Formats()); + } + if(m_version.supports_negotiable_signature_algorithms()) m_extensions.add(new Signature_Algorithms(policy.allowed_signature_hashes(), policy.allowed_signature_methods())); diff --git a/src/lib/tls/msg_client_kex.cpp b/src/lib/tls/msg_client_kex.cpp index bc4d33d52..0eceadb3b 100644 --- a/src/lib/tls/msg_client_kex.cpp +++ b/src/lib/tls/msg_client_kex.cpp @@ -172,7 +172,10 @@ Client_Key_Exchange::Client_Key_Exchange(Handshake_IO& io, append_tls_length_value(m_pre_master, psk.bits_of(), 2); } - append_tls_length_value(m_key_material, priv_key.public_value(), 1); + // follow server's preference for point compression + append_tls_length_value(m_key_material, + priv_key.public_value(state.server_hello()->prefers_compressed_ec_points() ? + PointGFp::COMPRESSED : PointGFp::UNCOMPRESSED ), 1); } #if defined(BOTAN_HAS_SRP6) else if(kex_algo == "SRP_SHA") diff --git a/src/lib/tls/msg_server_hello.cpp b/src/lib/tls/msg_server_hello.cpp index ebe8fb085..4f95a5c9d 100644 --- a/src/lib/tls/msg_server_hello.cpp +++ b/src/lib/tls/msg_server_hello.cpp @@ -35,12 +35,18 @@ Server_Hello::Server_Hello(Handshake_IO& io, if(client_hello.supports_extended_master_secret()) m_extensions.add(new Extended_Master_Secret); + Ciphersuite c = Ciphersuite::by_id(m_ciphersuite); + if(client_hello.supports_encrypt_then_mac() && policy.negotiate_encrypt_then_mac()) { - Ciphersuite c = Ciphersuite::by_id(m_ciphersuite); if(c.cbc_ciphersuite()) m_extensions.add(new Encrypt_then_MAC); } + + if(c.ecc_ciphersuite() && policy.use_ecc_point_compression()) + { + m_extensions.add(new Supported_Point_Formats()); + } if(client_hello.secure_renegotiation()) m_extensions.add(new Renegotiation_Extension(reneg_info)); diff --git a/src/lib/tls/msg_server_kex.cpp b/src/lib/tls/msg_server_kex.cpp index 10581fe45..33b980ba9 100644 --- a/src/lib/tls/msg_server_kex.cpp +++ b/src/lib/tls/msg_server_kex.cpp @@ -85,7 +85,10 @@ Server_Key_Exchange::Server_Key_Exchange(Handshake_IO& io, m_params.push_back(get_byte(0, named_curve_id)); m_params.push_back(get_byte(1, named_curve_id)); - append_tls_length_value(m_params, ecdh->public_value(), 1); + // follow client's preference for point compression + append_tls_length_value(m_params, + ecdh->public_value(state.client_hello()->prefers_compressed_ec_points() ? + PointGFp::COMPRESSED : PointGFp::UNCOMPRESSED), 1); m_kex_key.reset(ecdh.release()); } diff --git a/src/lib/tls/tls_cbc.cpp b/src/lib/tls/tls_cbc.cpp new file mode 100644 index 000000000..c7203003b --- /dev/null +++ b/src/lib/tls/tls_cbc.cpp @@ -0,0 +1,403 @@ +/* +* TLS CBC Record Handling +* (C) 2012,2013,2014,2015,2016 Jack Lloyd +* 2016 Juraj Somorovsky +* 2016 Matthias Gierlings +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include <botan/internal/tls_cbc.h> +#include <botan/internal/rounding.h> +#include <botan/internal/ct_utils.h> +#include <botan/tls_alert.h> +#include <botan/tls_magic.h> +#include <botan/tls_exceptn.h> + +namespace Botan { + +namespace TLS { + +/* +* TLS_CBC_HMAC_AEAD_Mode Constructor +*/ +TLS_CBC_HMAC_AEAD_Mode::TLS_CBC_HMAC_AEAD_Mode(const std::string& cipher_name, + size_t cipher_keylen, + const std::string& mac_name, + size_t mac_keylen, + bool use_explicit_iv, + bool use_encrypt_then_mac) : + m_cipher_name(cipher_name), + m_mac_name(mac_name), + m_cipher_keylen(cipher_keylen), + m_mac_keylen(mac_keylen), + m_use_encrypt_then_mac(use_encrypt_then_mac) + { + m_cipher = BlockCipher::create(m_cipher_name); + if(!m_cipher) + throw Algorithm_Not_Found(m_cipher_name); + + m_mac = MessageAuthenticationCode::create("HMAC(" + m_mac_name + ")"); + if(!m_mac) + throw Algorithm_Not_Found("HMAC(" + m_mac_name + ")"); + + m_tag_size = m_mac->output_length(); + m_block_size = m_cipher->block_size(); + + m_iv_size = use_explicit_iv ? m_block_size : 0; + } + +void TLS_CBC_HMAC_AEAD_Mode::clear() + { + cipher().clear(); + mac().clear(); + cbc_state().clear(); + } + +std::string TLS_CBC_HMAC_AEAD_Mode::name() const + { + return "TLS_CBC(" + m_cipher_name + "," + m_mac_name + ")"; + } + +size_t TLS_CBC_HMAC_AEAD_Mode::update_granularity() const + { + return 1; // just buffers anyway + } + +bool TLS_CBC_HMAC_AEAD_Mode::valid_nonce_length(size_t nl) const + { + if(m_cbc_state.empty()) + return nl == block_size(); + return nl == iv_size(); + } + +Key_Length_Specification TLS_CBC_HMAC_AEAD_Mode::key_spec() const + { + return Key_Length_Specification(m_cipher_keylen + m_mac_keylen); + } + +void TLS_CBC_HMAC_AEAD_Mode::key_schedule(const byte key[], size_t keylen) + { + // Both keys are of fixed length specified by the ciphersuite + + if(keylen != m_cipher_keylen + m_mac_keylen) + throw Invalid_Key_Length(name(), keylen); + + cipher().set_key(&key[0], m_cipher_keylen); + mac().set_key(&key[m_cipher_keylen], m_mac_keylen); + } + +void TLS_CBC_HMAC_AEAD_Mode::start_msg(const byte nonce[], size_t nonce_len) + { + if(!valid_nonce_length(nonce_len)) + { + throw Invalid_IV_Length(name(), nonce_len); + } + + m_msg.clear(); + + if(nonce_len > 0) + { + m_cbc_state.assign(nonce, nonce + nonce_len); + } + } + +size_t TLS_CBC_HMAC_AEAD_Mode::process(byte buf[], size_t sz) + { + m_msg.insert(m_msg.end(), buf, buf + sz); + return 0; + } + +std::vector<byte> TLS_CBC_HMAC_AEAD_Mode::assoc_data_with_len(uint16_t len) + { + std::vector<byte> ad = m_ad; + BOTAN_ASSERT(ad.size() == 13, "Expected AAD size"); + ad[11] = get_byte(0, len); + ad[12] = get_byte(1, len); + return ad; + } + +void TLS_CBC_HMAC_AEAD_Mode::set_associated_data(const byte ad[], size_t ad_len) + { + if(ad_len != 13) + throw Exception("Invalid TLS AEAD associated data length"); + m_ad.assign(ad, ad + ad_len); + } + +void TLS_CBC_HMAC_AEAD_Encryption::set_associated_data(const byte ad[], size_t ad_len) + { + TLS_CBC_HMAC_AEAD_Mode::set_associated_data(ad, ad_len); + + if(use_encrypt_then_mac()) + { + std::vector<byte>& ad = assoc_data(); + // AAD hack for EtM + size_t pt_size = make_u16bit(ad[11], ad[12]); + size_t enc_size = round_up(iv_size() + pt_size + 1, block_size()); + ad[11] = get_byte<uint16_t>(0, enc_size); + ad[12] = get_byte<uint16_t>(1, enc_size); + } + } + +void TLS_CBC_HMAC_AEAD_Encryption::cbc_encrypt_record(byte buf[], size_t buf_size) + { + const size_t blocks = buf_size / block_size(); + BOTAN_ASSERT(buf_size % block_size() == 0, "Valid CBC input"); + + xor_buf(buf, cbc_state().data(), block_size()); + cipher().encrypt(buf); + + for(size_t i = 1; i < blocks; ++i) + { + xor_buf(&buf[block_size()*i], &buf[block_size()*(i-1)], block_size()); + cipher().encrypt(&buf[block_size()*i]); + } + + cbc_state().assign(&buf[block_size()*(blocks-1)], + &buf[block_size()*blocks]); + } + +size_t TLS_CBC_HMAC_AEAD_Encryption::output_length(size_t input_length) const + { + return round_up(input_length + 1 + (use_encrypt_then_mac() ? 0 : tag_size()), block_size()) + + (use_encrypt_then_mac() ? tag_size() : 0); + } + +void TLS_CBC_HMAC_AEAD_Encryption::finish(secure_vector<byte>& buffer, size_t offset) + { + update(buffer, offset); + buffer.resize(offset); // truncate, leaving just header + const size_t header_size = offset; + + buffer.insert(buffer.end(), msg().begin(), msg().end()); + + const size_t input_size = msg().size() + 1 + (use_encrypt_then_mac() ? 0 : tag_size()); + const size_t enc_size = round_up(input_size, block_size()); + const size_t pad_val = enc_size - input_size; + const size_t buf_size = enc_size + (use_encrypt_then_mac() ? tag_size() : 0); + + BOTAN_ASSERT(enc_size % block_size() == 0, + "Buffer is an even multiple of block size"); + + mac().update(assoc_data()); + + if(use_encrypt_then_mac()) + { + if(iv_size() > 0) + { + mac().update(cbc_state()); + } + + for(size_t i = 0; i != pad_val + 1; ++i) + buffer.push_back(static_cast<byte>(pad_val)); + cbc_encrypt_record(&buffer[header_size], enc_size); + } + + // EtM also uses ciphertext size instead of plaintext size for AEAD input + const byte* mac_input = (use_encrypt_then_mac() ? &buffer[header_size] : msg().data()); + const size_t mac_input_len = (use_encrypt_then_mac() ? enc_size : msg().size()); + + mac().update(mac_input, mac_input_len); + + buffer.resize(buffer.size() + tag_size()); + mac().final(&buffer[buffer.size() - tag_size()]); + + if(use_encrypt_then_mac() == false) + { + for(size_t i = 0; i != pad_val + 1; ++i) + buffer.push_back(static_cast<byte>(pad_val)); + cbc_encrypt_record(&buffer[header_size], buf_size); + } + } + +namespace { + + +/* +* Checks the TLS padding. Returns 0 if the padding is invalid (we +* count the padding_length field as part of the padding size so a +* valid padding will always be at least one byte long), or the length +* of the padding otherwise. This is actually padding_length + 1 +* because both the padding and padding_length fields are padding from +* our perspective. +* +* Returning 0 in the error case should ensure the MAC check will fail. +* This approach is suggested in section 6.2.3.2 of RFC 5246. +*/ +u16bit check_tls_padding(const byte record[], size_t record_len) + { + /* + * TLS v1.0 and up require all the padding bytes be the same value + * and allows up to 255 bytes. + */ + + const byte pad_byte = record[(record_len-1)]; + + byte pad_invalid = 0; + for(size_t i = 0; i != record_len; ++i) + { + const size_t left = record_len - i - 2; + const byte delim_mask = CT::is_less<u16bit>(static_cast<u16bit>(left), pad_byte) & 0xFF; + pad_invalid |= (delim_mask & (record[i] ^ pad_byte)); + } + + u16bit pad_invalid_mask = CT::expand_mask<u16bit>(pad_invalid); + return CT::select<u16bit>(pad_invalid_mask, 0, pad_byte + 1); + } + +} + +void TLS_CBC_HMAC_AEAD_Decryption::cbc_decrypt_record(byte record_contents[], size_t record_len) + { + BOTAN_ASSERT(record_len % block_size() == 0, + "Buffer is an even multiple of block size"); + + const size_t blocks = record_len / block_size(); + + BOTAN_ASSERT(blocks >= 1, "At least one ciphertext block"); + + byte* buf = record_contents; + + secure_vector<byte> last_ciphertext(block_size()); + copy_mem(last_ciphertext.data(), buf, block_size()); + + cipher().decrypt(buf); + xor_buf(buf, cbc_state().data(), block_size()); + + secure_vector<byte> last_ciphertext2; + + for(size_t i = 1; i < blocks; ++i) + { + last_ciphertext2.assign(&buf[block_size()*i], &buf[block_size()*(i+1)]); + cipher().decrypt(&buf[block_size()*i]); + xor_buf(&buf[block_size()*i], last_ciphertext.data(), block_size()); + std::swap(last_ciphertext, last_ciphertext2); + } + + cbc_state().assign(last_ciphertext.begin(), last_ciphertext.end()); + } + +size_t TLS_CBC_HMAC_AEAD_Decryption::output_length(size_t) const + { + /* + * We don't know this because the padding is arbitrary + */ + return 0; + } + +void TLS_CBC_HMAC_AEAD_Decryption::finish(secure_vector<byte>& buffer, size_t offset) + { + update(buffer, offset); + buffer.resize(offset); + + const size_t record_len = msg().size(); + byte* record_contents = msg().data(); + + // This early exit does not leak info because all the values compared are public + if(record_len < tag_size() || + (record_len - (use_encrypt_then_mac() ? tag_size() : 0)) % block_size() != 0) + { + throw TLS_Exception(Alert::BAD_RECORD_MAC, "Message authentication failure"); + } + + if(use_encrypt_then_mac()) + { + const size_t enc_size = record_len - tag_size(); + + mac().update(assoc_data_with_len(iv_size() + enc_size)); + if(iv_size() > 0) + { + mac().update(cbc_state()); + } + mac().update(record_contents, enc_size); + + std::vector<byte> mac_buf(tag_size()); + mac().final(mac_buf.data()); + + const size_t mac_offset = enc_size; + + const bool mac_ok = same_mem(&record_contents[mac_offset], mac_buf.data(), tag_size()); + + if(!mac_ok) + { + throw TLS_Exception(Alert::BAD_RECORD_MAC, "Message authentication failure"); + } + + cbc_decrypt_record(record_contents, enc_size); + + // 0 if padding was invalid, otherwise 1 + padding_bytes + u16bit pad_size = check_tls_padding(record_contents, enc_size); + + // No oracle here, whoever sent us this had the key since MAC check passed + if(pad_size == 0) + { + throw TLS_Exception(Alert::BAD_RECORD_MAC, "Message authentication failure"); + } + + const byte* plaintext_block = &record_contents[0]; + const u16bit plaintext_length = enc_size - pad_size; + + buffer.insert(buffer.end(), plaintext_block, plaintext_block + plaintext_length); + } + else + { + uint8_t* record_contents = msg().data(); + const size_t record_len = msg().size(); + + CT::poison(record_contents, record_len); + + cbc_decrypt_record(record_contents, record_len); + + // 0 if padding was invalid, otherwise 1 + padding_bytes + u16bit pad_size = check_tls_padding(record_contents, record_len); + + /* + This mask is zero if there is not enough room in the packet to get a valid MAC. + + We have to accept empty packets, since otherwise we are not compatible + with how OpenSSL's countermeasure for fixing BEAST in TLS 1.0 CBC works + (sending empty records, instead of 1/(n-1) splitting) + */ + + const u16bit size_ok_mask = CT::is_lte<u16bit>(static_cast<u16bit>(tag_size() + pad_size), static_cast<u16bit>(record_len + 1)); + pad_size &= size_ok_mask; + + CT::unpoison(record_contents, record_len); + + /* + This is unpoisoned sooner than it should. The pad_size leaks to plaintext_length and + then to the timing channel in the MAC computation described in the Lucky 13 paper. + */ + CT::unpoison(pad_size); + + const byte* plaintext_block = &record_contents[0]; + const u16bit plaintext_length = static_cast<u16bit>(record_len - tag_size() - pad_size); + + mac().update(assoc_data_with_len(plaintext_length)); + mac().update(plaintext_block, plaintext_length); + + std::vector<byte> mac_buf(tag_size()); + mac().final(mac_buf.data()); + + const size_t mac_offset = record_len - (tag_size() + pad_size); + + const bool mac_ok = same_mem(&record_contents[mac_offset], mac_buf.data(), tag_size()); + + const u16bit ok_mask = size_ok_mask & CT::expand_mask<u16bit>(mac_ok) & CT::expand_mask<u16bit>(pad_size); + + CT::unpoison(ok_mask); + + if(ok_mask) + { + buffer.insert(buffer.end(), plaintext_block, plaintext_block + plaintext_length); + } + else + { + throw TLS_Exception(Alert::BAD_RECORD_MAC, "Message authentication failure"); + } + } + } + +} + +} diff --git a/src/lib/tls/tls_cbc.h b/src/lib/tls/tls_cbc.h new file mode 100644 index 000000000..90b54bb5a --- /dev/null +++ b/src/lib/tls/tls_cbc.h @@ -0,0 +1,168 @@ +/* +* TLS CBC+HMAC AEAD +* (C) 2016 Jack Lloyd +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#ifndef BOTAN_TLS_CBC_HMAC_AEAD_H__ +#define BOTAN_TLS_CBC_HMAC_AEAD_H__ + +#include <botan/aead.h> +#include <botan/block_cipher.h> +#include <botan/mac.h> + +namespace Botan { + +namespace TLS { + +/** +* TLS CBC+HMAC AEAD base class (GenericBlockCipher in TLS spec) +* This is the weird TLS-specific mode, not for general consumption. +*/ +class TLS_CBC_HMAC_AEAD_Mode : public AEAD_Mode + { + public: + size_t process(uint8_t buf[], size_t sz) override final; + + std::string name() const override final; + + void set_associated_data(const byte ad[], size_t ad_len) override; + + size_t update_granularity() const override final; + + Key_Length_Specification key_spec() const override final; + + bool valid_nonce_length(size_t nl) const override final; + + size_t tag_size() const override final { return m_tag_size; } + + size_t default_nonce_length() const override final { return m_iv_size; } + + void clear() override final; + + protected: + TLS_CBC_HMAC_AEAD_Mode(const std::string& cipher_name, + size_t cipher_keylen, + const std::string& mac_name, + size_t mac_keylen, + bool use_explicit_iv, + bool use_encrypt_then_mac); + + size_t cipher_keylen() const { return m_cipher_keylen; } + size_t mac_keylen() const { return m_mac_keylen; } + size_t iv_size() const { return m_iv_size; } + size_t block_size() const { return m_block_size; } + + bool use_encrypt_then_mac() const { return m_use_encrypt_then_mac; } + + BlockCipher& cipher() const + { + BOTAN_ASSERT_NONNULL(m_cipher); + return *m_cipher; + } + + MessageAuthenticationCode& mac() const + { + BOTAN_ASSERT_NONNULL(m_mac); + return *m_mac; + } + + secure_vector<byte>& cbc_state() { return m_cbc_state; } + std::vector<byte>& assoc_data() { return m_ad; } + secure_vector<byte>& msg() { return m_msg; } + + std::vector<byte> assoc_data_with_len(uint16_t len); + + private: + void start_msg(const byte nonce[], size_t nonce_len) override final; + + void key_schedule(const byte key[], size_t length) override final; + + const std::string m_cipher_name; + const std::string m_mac_name; + size_t m_cipher_keylen; + size_t m_mac_keylen; + size_t m_iv_size; + size_t m_tag_size; + size_t m_block_size; + bool m_use_encrypt_then_mac; + + std::unique_ptr<BlockCipher> m_cipher; + std::unique_ptr<MessageAuthenticationCode> m_mac; + + secure_vector<byte> m_cbc_state; + std::vector<byte> m_ad; + secure_vector<byte> m_msg; + }; + +/** +* TLS_CBC_HMAC_AEAD Encryption +*/ +class BOTAN_DLL TLS_CBC_HMAC_AEAD_Encryption final : public TLS_CBC_HMAC_AEAD_Mode + { + public: + /** + */ + TLS_CBC_HMAC_AEAD_Encryption(const std::string& cipher_algo, + const size_t cipher_keylen, + const std::string& mac_algo, + const size_t mac_keylen, + bool use_explicit_iv, + bool use_encrypt_then_mac) : + TLS_CBC_HMAC_AEAD_Mode(cipher_algo, + cipher_keylen, + mac_algo, + mac_keylen, + use_explicit_iv, + use_encrypt_then_mac) + {} + + void set_associated_data(const byte ad[], size_t ad_len) override; + + size_t output_length(size_t input_length) const override; + + size_t minimum_final_size() const override { return 0; } + + void finish(secure_vector<byte>& final_block, size_t offset = 0) override; + private: + void cbc_encrypt_record(byte record_contents[], size_t record_len); + }; + +/** +* TLS_CBC_HMAC_AEAD Decryption +*/ +class BOTAN_DLL TLS_CBC_HMAC_AEAD_Decryption final : public TLS_CBC_HMAC_AEAD_Mode + { + public: + /** + */ + TLS_CBC_HMAC_AEAD_Decryption(const std::string& cipher_algo, + const size_t cipher_keylen, + const std::string& mac_algo, + const size_t mac_keylen, + bool use_explicit_iv, + bool use_encrypt_then_mac) : + TLS_CBC_HMAC_AEAD_Mode(cipher_algo, + cipher_keylen, + mac_algo, + mac_keylen, + use_explicit_iv, + use_encrypt_then_mac) + {} + + size_t output_length(size_t input_length) const override; + + size_t minimum_final_size() const override { return tag_size(); } + + void finish(secure_vector<byte>& final_block, size_t offset = 0) override; + + private: + void cbc_decrypt_record(byte record_contents[], size_t record_len); + }; + +} + +} + +#endif diff --git a/src/lib/tls/tls_channel.cpp b/src/lib/tls/tls_channel.cpp index 5e9207da7..574be8a46 100644 --- a/src/lib/tls/tls_channel.cpp +++ b/src/lib/tls/tls_channel.cpp @@ -276,7 +276,7 @@ void Channel::activate_session() if(!m_active_state->version().is_datagram_protocol()) { // TLS is easy just remove all but the current state - auto current_epoch = sequence_numbers().current_write_epoch(); + const u16bit current_epoch = sequence_numbers().current_write_epoch(); const auto not_current_epoch = [current_epoch](u16bit epoch) { return (epoch != current_epoch); }; @@ -502,10 +502,11 @@ void Channel::send_record_array(u16bit epoch, byte type, const byte input[], siz return; /* - * If using CBC mode without an explicit IV (SSL v3 or TLS v1.0), - * send a single byte of plaintext to randomize the (implicit) IV of - * the following main block. If using a stream cipher, or TLS v1.1 - * or higher, this isn't necessary. + * In versions without an explicit IV field (only TLS v1.0 now that + * SSLv3 has been removed) send a single byte record first to randomize + * the following (implicit) IV of the following record. + * + * This isn't needed in TLS v1.1 or higher. * * An empty record also works but apparently some implementations do * not like this (https://bugzilla.mozilla.org/show_bug.cgi?id=665814) @@ -515,7 +516,7 @@ void Channel::send_record_array(u16bit epoch, byte type, const byte input[], siz auto cipher_state = write_cipher_state_epoch(epoch); - if(type == APPLICATION_DATA && cipher_state->cbc_without_explicit_iv()) + if(type == APPLICATION_DATA && m_active_state->version().supports_explicit_cbc_ivs() == false) { write_record(cipher_state.get(), epoch, type, input, 1); input += 1; diff --git a/src/lib/tls/tls_extensions.cpp b/src/lib/tls/tls_extensions.cpp index e38e4ccdc..df265d915 100644 --- a/src/lib/tls/tls_extensions.cpp +++ b/src/lib/tls/tls_extensions.cpp @@ -33,6 +33,9 @@ Extension* make_extension(TLS_Data_Reader& reader, case TLSEXT_USABLE_ELLIPTIC_CURVES: return new Supported_Elliptic_Curves(reader, size); + case TLSEXT_EC_POINT_FORMATS: + return new Supported_Point_Formats(reader, size); + case TLSEXT_SAFE_RENEGOTIATION: return new Renegotiation_Extension(reader, size); @@ -353,6 +356,43 @@ Supported_Elliptic_Curves::Supported_Elliptic_Curves(TLS_Data_Reader& reader, } } +std::vector<byte> Supported_Point_Formats::serialize() const + { + // if we send this extension, we prefer compressed points, + // otherwise we don't send it (which is equal to supporting only uncompressed) + // if this extension is sent, it MUST include uncompressed (RFC 4492, section 5.1) + return std::vector<byte>{2, ANSIX962_COMPRESSED_PRIME, UNCOMPRESSED}; + } + +Supported_Point_Formats::Supported_Point_Formats(TLS_Data_Reader& reader, + u16bit extension_size) + { + byte len = reader.get_byte(); + + if(len + 1 != extension_size) + throw Decoding_Error("Inconsistent length field in supported point formats list"); + + for(size_t i = 0; i != len; ++i) + { + byte format = reader.get_byte(); + + if(format == UNCOMPRESSED) + { + m_prefers_compressed = false; + reader.discard_next(len-i-1); + return; + } + else if(format == ANSIX962_COMPRESSED_PRIME) + { + m_prefers_compressed = true; + reader.discard_next(len-i-1); + return; + } + + // ignore ANSIX962_COMPRESSED_CHAR2, we don't support these curves + } + } + std::string Signature_Algorithms::hash_algo_name(byte code) { switch(code) diff --git a/src/lib/tls/tls_extensions.h b/src/lib/tls/tls_extensions.h index c270bf23a..d69e40a60 100644 --- a/src/lib/tls/tls_extensions.h +++ b/src/lib/tls/tls_extensions.h @@ -258,6 +258,38 @@ class Supported_Elliptic_Curves final : public Extension }; /** +* Supported Point Formats Extension (RFC 4492) +*/ +class Supported_Point_Formats final : public Extension + { + public: + enum ECPointFormat : byte { + UNCOMPRESSED = 0, + ANSIX962_COMPRESSED_PRIME = 1, + ANSIX962_COMPRESSED_CHAR2 = 2, // don't support these curves + }; + + static Handshake_Extension_Type static_type() + { return TLSEXT_EC_POINT_FORMATS; } + + Handshake_Extension_Type type() const override { return static_type(); } + + std::vector<byte> serialize() const override; + + explicit Supported_Point_Formats() : m_prefers_compressed(true) {} + + Supported_Point_Formats(TLS_Data_Reader& reader, + u16bit extension_size); + + bool empty() const override { return false; } + + bool prefers_compressed() { return m_prefers_compressed; } + + private: + bool m_prefers_compressed = false; + }; + +/** * Signature Algorithms Extension for TLS 1.2 (RFC 5246) */ class Signature_Algorithms final : public Extension diff --git a/src/lib/tls/tls_messages.h b/src/lib/tls/tls_messages.h index 698dbc99f..25228c865 100644 --- a/src/lib/tls/tls_messages.h +++ b/src/lib/tls/tls_messages.h @@ -121,6 +121,15 @@ class BOTAN_DLL Client_Hello final : public Handshake_Message return std::vector<std::string>(); } + bool prefers_compressed_ec_points() const + { + if(Supported_Point_Formats* ecc_formats = m_extensions.get<Supported_Point_Formats>()) + { + return ecc_formats->prefers_compressed(); + } + return false; + } + std::string sni_hostname() const { if(Server_Name_Indicator* sni = m_extensions.get<Server_Name_Indicator>()) @@ -328,6 +337,15 @@ class BOTAN_DLL Server_Hello final : public Handshake_Message std::set<Handshake_Extension_Type> extension_types() const { return m_extensions.extension_types(); } + bool prefers_compressed_ec_points() const + { + if(auto ecc_formats = m_extensions.get<Supported_Point_Formats>()) + { + return ecc_formats->prefers_compressed(); + } + return false; + } + Server_Hello(Handshake_IO& io, Handshake_Hash& hash, const Policy& policy, diff --git a/src/lib/tls/tls_policy.cpp b/src/lib/tls/tls_policy.cpp index 592d4f572..53ef8e46d 100644 --- a/src/lib/tls/tls_policy.cpp +++ b/src/lib/tls/tls_policy.cpp @@ -104,6 +104,11 @@ bool Policy::allowed_ecc_curve(const std::string& curve) const return value_exists(allowed_ecc_curves(), curve); } +bool Policy::use_ecc_point_compression() const + { + return false; + } + /* * Choose an ECC curve to use */ @@ -347,12 +352,19 @@ std::vector<u16bit> Policy::ciphersuite_list(Protocol_Version version, for(auto&& suite : Ciphersuite::all_known_ciphersuites()) { - if(!acceptable_ciphersuite(suite)) + // Can we use it? + if(suite.valid() == false) + continue; + + // Is it acceptable to the policy? + if(!this->acceptable_ciphersuite(suite)) continue; + // Are we doing SRP? if(!have_srp && suite.kex_algo() == "SRP_SHA") continue; + // Are we doing AEAD in a non-AEAD version if(!version.supports_aead_modes() && suite.mac_algo() == "AEAD") continue; diff --git a/src/lib/tls/tls_policy.h b/src/lib/tls/tls_policy.h index 76e80ddde..47ac51685 100644 --- a/src/lib/tls/tls_policy.h +++ b/src/lib/tls/tls_policy.h @@ -67,6 +67,11 @@ class BOTAN_DLL Policy bool allowed_ecc_curve(const std::string& curve) const; /** + * Request that ECC curve points are sent compressed + */ + virtual bool use_ecc_point_compression() const; + + /** * Returns a list of compression algorithms we are willing to use, * in order of preference. Allowed values any value of * Compression_Method. @@ -348,6 +353,9 @@ class BOTAN_DLL Text_Policy : public Policy std::vector<std::string> allowed_ecc_curves() const override { return get_list("ecc_curves", Policy::allowed_ecc_curves()); } + bool use_ecc_point_compression() const override + { return get_bool("use_ecc_point_compression", Policy::use_ecc_point_compression()); } + bool allow_tls10() const override { return get_bool("allow_tls10", Policy::allow_tls10()); } diff --git a/src/lib/tls/tls_record.cpp b/src/lib/tls/tls_record.cpp index 88609ca26..0bee24e34 100644 --- a/src/lib/tls/tls_record.cpp +++ b/src/lib/tls/tls_record.cpp @@ -13,6 +13,7 @@ #include <botan/loadstor.h> #include <botan/internal/tls_seq_numbers.h> #include <botan/internal/tls_session_key.h> +#include <botan/internal/tls_cbc.h> #include <botan/internal/rounding.h> #include <botan/internal/ct_utils.h> #include <botan/rng.h> @@ -29,8 +30,7 @@ Connection_Cipher_State::Connection_Cipher_State(Protocol_Version version, bool uses_encrypt_then_mac) : m_start_time(std::chrono::system_clock::now()), m_nonce_bytes_from_handshake(suite.nonce_bytes_from_handshake()), - m_nonce_bytes_from_record(suite.nonce_bytes_from_record()), - m_uses_encrypt_then_mac(uses_encrypt_then_mac) + m_nonce_bytes_from_record(suite.nonce_bytes_from_record()) { SymmetricKey mac_key, cipher_key; InitializationVector iv; @@ -48,46 +48,75 @@ Connection_Cipher_State::Connection_Cipher_State(Protocol_Version version, mac_key = keys.server_mac_key(); } - const std::string cipher_algo = suite.cipher_algo(); - const std::string mac_algo = suite.mac_algo(); + BOTAN_ASSERT_EQUAL(iv.length(), nonce_bytes_from_handshake(), "Matching nonce sizes"); - if(AEAD_Mode* aead = get_aead(cipher_algo, our_side ? ENCRYPTION : DECRYPTION)) + m_nonce = unlock(iv.bits_of()); + + if(suite.mac_algo() == "AEAD") { - m_aead.reset(aead); - m_aead->set_key(cipher_key + mac_key); + m_aead.reset(get_aead(suite.cipher_algo(), our_side ? ENCRYPTION : DECRYPTION)); + BOTAN_ASSERT(m_aead, "Have AEAD"); - BOTAN_ASSERT_EQUAL(iv.length(), nonce_bytes_from_handshake(), "Matching nonce sizes"); - m_nonce = unlock(iv.bits_of()); + m_aead->set_key(cipher_key + mac_key); BOTAN_ASSERT(nonce_bytes_from_record() == 0 || nonce_bytes_from_record() == 8, "Ciphersuite uses implemented IV length"); + m_cbc_nonce = false; if(m_nonce.size() != 12) { m_nonce.resize(m_nonce.size() + 8); } - - return; } + else + { + // legacy CBC+HMAC mode + if(our_side) + { + m_aead.reset(new TLS_CBC_HMAC_AEAD_Encryption( + suite.cipher_algo(), + suite.cipher_keylen(), + suite.mac_algo(), + suite.mac_keylen(), + version.supports_explicit_cbc_ivs(), + uses_encrypt_then_mac)); + } + else + { + m_aead.reset(new TLS_CBC_HMAC_AEAD_Decryption( + suite.cipher_algo(), + suite.cipher_keylen(), + suite.mac_algo(), + suite.mac_keylen(), + version.supports_explicit_cbc_ivs(), + uses_encrypt_then_mac)); + } - m_block_cipher = BlockCipher::create(cipher_algo); - m_mac = MessageAuthenticationCode::create("HMAC(" + mac_algo + ")"); - if(!m_block_cipher) - throw Invalid_Argument("Unknown TLS cipher " + cipher_algo); - - m_block_cipher->set_key(cipher_key); - m_block_cipher_cbc_state = iv.bits_of(); - m_block_size = m_block_cipher->block_size(); - - if(version.supports_explicit_cbc_ivs()) - m_iv_size = m_block_size; + m_aead->set_key(cipher_key + mac_key); - m_mac->set_key(mac_key); + m_cbc_nonce = true; + if(version.supports_explicit_cbc_ivs()) + m_nonce_bytes_from_record = m_nonce_bytes_from_handshake; + else if(our_side == false) + m_aead->start(iv.bits_of()); + } } -std::vector<byte> Connection_Cipher_State::aead_nonce(u64bit seq) +std::vector<byte> Connection_Cipher_State::aead_nonce(u64bit seq, RandomNumberGenerator& rng) { - if(nonce_bytes_from_handshake() == 12) + if(m_cbc_nonce) + { + if(m_nonce.size()) + { + std::vector<byte> nonce; + nonce.swap(m_nonce); + return nonce; + } + std::vector<byte> nonce(nonce_bytes_from_record()); + rng.randomize(nonce.data(), nonce.size()); + return nonce; + } + else if(nonce_bytes_from_handshake() == 12) { std::vector<byte> nonce(12); store_be(seq, nonce.data() + 4); @@ -105,7 +134,14 @@ std::vector<byte> Connection_Cipher_State::aead_nonce(u64bit seq) std::vector<byte> Connection_Cipher_State::aead_nonce(const byte record[], size_t record_len, u64bit seq) { - if(nonce_bytes_from_handshake() == 12) + if(m_cbc_nonce) + { + if(record_len < nonce_bytes_from_record()) + throw Decoding_Error("Invalid CBC packet too short to be valid"); + std::vector<byte> nonce(record, record + nonce_bytes_from_record()); + return nonce; + } + else if(nonce_bytes_from_handshake() == 12) { /* Assumes if the suite specifies 12 bytes come from the handshake then @@ -157,29 +193,6 @@ Connection_Cipher_State::format_ad(u64bit msg_sequence, namespace { -void cbc_encrypt_record(const BlockCipher& bc, - secure_vector<byte>& cbc_state, - byte buf[], - size_t buf_size) - { - const size_t block_size = bc.block_size(); - const size_t blocks = buf_size / block_size; - BOTAN_ASSERT(buf_size % block_size == 0, "CBC input"); - BOTAN_ASSERT(blocks > 0, "Expected at least 1 block"); - - xor_buf(buf, cbc_state.data(), block_size); - bc.encrypt(buf); - - for(size_t i = 1; i < blocks; ++i) - { - xor_buf(&buf[block_size*i], &buf[block_size*(i-1)], block_size); - bc.encrypt(&buf[block_size*i]); - } - - cbc_state.assign(&buf[block_size*(blocks-1)], - &buf[block_size*blocks]); - } - inline void append_u16_len(secure_vector<byte>& output, size_t len_field) { const uint16_t len16 = len_field; @@ -213,99 +226,38 @@ void write_record(secure_vector<byte>& output, { append_u16_len(output, msg.get_size()); output.insert(output.end(), msg.get_data(), msg.get_data() + msg.get_size()); - return; } + AEAD_Mode* aead = cs->aead(); std::vector<byte> aad = cs->format_ad(seq, msg.get_type(), version, static_cast<u16bit>(msg.get_size())); - if(AEAD_Mode* aead = cs->aead()) - { - const size_t ctext_size = aead->output_length(msg.get_size()); - - const std::vector<byte> nonce = cs->aead_nonce(seq); + const size_t ctext_size = aead->output_length(msg.get_size()); - const size_t rec_size = ctext_size + cs->nonce_bytes_from_record(); + const size_t rec_size = ctext_size + cs->nonce_bytes_from_record(); - BOTAN_ASSERT(rec_size <= 0xFFFF, "Ciphertext length fits in field"); - append_u16_len(output, rec_size); + aead->set_ad(aad); - aead->set_ad(aad); + const std::vector<byte> nonce = cs->aead_nonce(seq, rng); - if(cs->nonce_bytes_from_record() > 0) - { - output += std::make_pair(&nonce[cs->nonce_bytes_from_handshake()], cs->nonce_bytes_from_record()); - } - const size_t header_size = output.size(); - output += std::make_pair(msg.get_data(), msg.get_size()); + append_u16_len(output, rec_size); - aead->start(nonce); - aead->finish(output, header_size); - - BOTAN_ASSERT(output.size() < MAX_CIPHERTEXT_SIZE, - "Produced ciphertext larger than protocol allows"); - return; - } - - const size_t block_size = cs->block_size(); - const size_t iv_size = cs->iv_size(); - const size_t mac_size = cs->mac_size(); - - const size_t input_size = - iv_size + msg.get_size() + 1 + (cs->uses_encrypt_then_mac() ? 0 : mac_size); - const size_t enc_size = round_up(input_size, block_size); - const size_t pad_val = enc_size - input_size; - const size_t buf_size = enc_size + (cs->uses_encrypt_then_mac() ? mac_size : 0); - - if(cs->uses_encrypt_then_mac()) + if(cs->nonce_bytes_from_record() > 0) { - aad[11] = get_byte<uint16_t>(0, enc_size); - aad[12] = get_byte<uint16_t>(1, enc_size); + if(cs->cbc_nonce()) + output += nonce; + else + output += std::make_pair(&nonce[cs->nonce_bytes_from_handshake()], cs->nonce_bytes_from_record()); } - BOTAN_ASSERT(enc_size % block_size == 0, - "Buffer is an even multiple of block size"); - - append_u16_len(output, buf_size); - const size_t header_size = output.size(); + output += std::make_pair(msg.get_data(), msg.get_size()); - if(iv_size) - { - output.resize(output.size() + iv_size); - rng.randomize(&output[output.size() - iv_size], iv_size); - } - - output.insert(output.end(), msg.get_data(), msg.get_data() + msg.get_size()); - - // EtM also uses ciphertext size instead of plaintext size for AEAD input - const byte* mac_input = (cs->uses_encrypt_then_mac() ? &output[header_size] : msg.get_data()); - const size_t mac_input_len = (cs->uses_encrypt_then_mac() ? enc_size : msg.get_size()); - - if(cs->uses_encrypt_then_mac()) - { - for(size_t i = 0; i != pad_val + 1; ++i) - output.push_back(static_cast<byte>(pad_val)); - cbc_encrypt_record(*cs->block_cipher(), cs->cbc_state(), &output[header_size], enc_size); - } - - output.resize(output.size() + mac_size); - cs->mac()->update(aad); - cs->mac()->update(mac_input, mac_input_len); - cs->mac()->final(&output[output.size() - mac_size]); - - if(cs->uses_encrypt_then_mac() == false) - { - for(size_t i = 0; i != pad_val + 1; ++i) - output.push_back(static_cast<byte>(pad_val)); - cbc_encrypt_record(*cs->block_cipher(), cs->cbc_state(), &output[header_size], buf_size); - } - - if(buf_size > MAX_CIPHERTEXT_SIZE) - throw Internal_Error("Output record is larger than allowed by protocol"); + aead->start(nonce); + aead->finish(output, header_size); - BOTAN_ASSERT_EQUAL(buf_size + header_size, output.size(), - "Output buffer is sized properly"); + BOTAN_ASSERT(output.size() < MAX_CIPHERTEXT_SIZE, + "Produced ciphertext larger than protocol allows"); } namespace { @@ -329,72 +281,6 @@ size_t fill_buffer_to(secure_vector<byte>& readbuf, return (desired - readbuf.size()); // how many bytes do we still need? } -/* -* Checks the TLS padding. Returns 0 if the padding is invalid (we -* count the padding_length field as part of the padding size so a -* valid padding will always be at least one byte long), or the length -* of the padding otherwise. This is actually padding_length + 1 -* because both the padding and padding_length fields are padding from -* our perspective. -* -* Returning 0 in the error case should ensure the MAC check will fail. -* This approach is suggested in section 6.2.3.2 of RFC 5246. -*/ -u16bit tls_padding_check(const byte record[], size_t record_len) - { - /* - * TLS v1.0 and up require all the padding bytes be the same value - * and allows up to 255 bytes. - */ - - const byte pad_byte = record[(record_len-1)]; - - byte pad_invalid = 0; - for(size_t i = 0; i != record_len; ++i) - { - const size_t left = record_len - i - 2; - const byte delim_mask = CT::is_less<u16bit>(static_cast<u16bit>(left), pad_byte) & 0xFF; - pad_invalid |= (delim_mask & (record[i] ^ pad_byte)); - } - - u16bit pad_invalid_mask = CT::expand_mask<u16bit>(pad_invalid); - return CT::select<u16bit>(pad_invalid_mask, 0, pad_byte + 1); - } - -void cbc_decrypt_record(byte record_contents[], size_t record_len, - Connection_Cipher_State& cs, - const BlockCipher& bc) - { - const size_t block_size = cs.block_size(); - - BOTAN_ASSERT(record_len % block_size == 0, - "Buffer is an even multiple of block size"); - - const size_t blocks = record_len / block_size; - - BOTAN_ASSERT(blocks >= 1, "At least one ciphertext block"); - - byte* buf = record_contents; - - secure_vector<byte> last_ciphertext(block_size); - copy_mem(last_ciphertext.data(), buf, block_size); - - bc.decrypt(buf); - xor_buf(buf, &cs.cbc_state()[0], block_size); - - secure_vector<byte> last_ciphertext2; - - for(size_t i = 1; i < blocks; ++i) - { - last_ciphertext2.assign(&buf[block_size*i], &buf[block_size*(i+1)]); - bc.decrypt(&buf[block_size*i]); - xor_buf(&buf[block_size*i], last_ciphertext.data(), block_size); - std::swap(last_ciphertext, last_ciphertext2); - } - - cs.cbc_state() = last_ciphertext; - } - void decrypt_record(secure_vector<byte>& output, byte record_contents[], size_t record_len, u64bit record_sequence, @@ -402,121 +288,24 @@ void decrypt_record(secure_vector<byte>& output, Record_Type record_type, Connection_Cipher_State& cs) { - if(AEAD_Mode* aead = cs.aead()) - { - const std::vector<byte> nonce = cs.aead_nonce(record_contents, record_len, record_sequence); - const byte* msg = &record_contents[cs.nonce_bytes_from_record()]; - const size_t msg_length = record_len - cs.nonce_bytes_from_record(); - - const size_t ptext_size = aead->output_length(msg_length); - - aead->set_associated_data_vec( - cs.format_ad(record_sequence, record_type, record_version, static_cast<u16bit>(ptext_size)) - ); - - aead->start(nonce); - - const size_t offset = output.size(); - output += std::make_pair(msg, msg_length); - aead->finish(output, offset); + AEAD_Mode* aead = cs.aead(); + BOTAN_ASSERT(aead, "Cannot decrypt without cipher"); - BOTAN_ASSERT(output.size() == ptext_size + offset, "Produced expected size"); - } - else - { - // GenericBlockCipher case - BlockCipher* bc = cs.block_cipher(); - BOTAN_ASSERT(bc != nullptr, "No cipher state set but needed to decrypt"); - - const size_t mac_size = cs.mac_size(); - const size_t iv_size = cs.iv_size(); - - if(!cs.uses_encrypt_then_mac()) - { - // This early exit does not leak info because all the values are public - if((record_len < mac_size + iv_size) || (record_len % cs.block_size() != 0)) - throw TLS_Exception(Alert::BAD_RECORD_MAC, "Message authentication failure"); - - CT::poison(record_contents, record_len); - - cbc_decrypt_record(record_contents, record_len, cs, *bc); - - // 0 if padding was invalid, otherwise 1 + padding_bytes - u16bit pad_size = tls_padding_check(record_contents, record_len); - - // This mask is zero if there is not enough room in the packet to get - // a valid MAC. We have to accept empty packets, since otherwise we - // are not compatible with the BEAST countermeasure (thus record_len+1). - const u16bit size_ok_mask = CT::is_lte<u16bit>(static_cast<u16bit>(mac_size + pad_size + iv_size), static_cast<u16bit>(record_len + 1)); - pad_size &= size_ok_mask; - - CT::unpoison(record_contents, record_len); - - /* - This is unpoisoned sooner than it should. The pad_size leaks to plaintext_length and - then to the timing channel in the MAC computation described in the Lucky 13 paper. - */ - CT::unpoison(pad_size); - - const byte* plaintext_block = &record_contents[iv_size]; - const u16bit plaintext_length = static_cast<u16bit>(record_len - mac_size - iv_size - pad_size); - - cs.mac()->update(cs.format_ad(record_sequence, record_type, record_version, plaintext_length)); - cs.mac()->update(plaintext_block, plaintext_length); - - std::vector<byte> mac_buf(mac_size); - cs.mac()->final(mac_buf.data()); + const std::vector<byte> nonce = cs.aead_nonce(record_contents, record_len, record_sequence); + const byte* msg = &record_contents[cs.nonce_bytes_from_record()]; + const size_t msg_length = record_len - cs.nonce_bytes_from_record(); - const size_t mac_offset = record_len - (mac_size + pad_size); + const size_t ptext_size = aead->output_length(msg_length); - const bool mac_ok = same_mem(&record_contents[mac_offset], mac_buf.data(), mac_size); + aead->set_associated_data_vec( + cs.format_ad(record_sequence, record_type, record_version, static_cast<u16bit>(ptext_size)) + ); - const u16bit ok_mask = size_ok_mask & CT::expand_mask<u16bit>(mac_ok) & CT::expand_mask<u16bit>(pad_size); + aead->start(nonce); - CT::unpoison(ok_mask); - - if(ok_mask) - { - output.assign(plaintext_block, plaintext_block + plaintext_length); - } - else - { - throw TLS_Exception(Alert::BAD_RECORD_MAC, "Message authentication failure"); - } - } - else - { - const size_t enc_size = record_len - mac_size; - // This early exit does not leak info because all the values are public - if((record_len < mac_size + iv_size) || ( enc_size % cs.block_size() != 0)) - throw TLS_Exception(Alert::BAD_RECORD_MAC, "Message authentication failure"); - - cs.mac()->update(cs.format_ad(record_sequence, record_type, record_version, enc_size)); - cs.mac()->update(record_contents, enc_size); - - std::vector<byte> mac_buf(mac_size); - cs.mac()->final(mac_buf.data()); - - const size_t mac_offset = enc_size; - - const bool mac_ok = same_mem(&record_contents[mac_offset], mac_buf.data(), mac_size); - - if(!mac_ok) - { - throw TLS_Exception(Alert::BAD_RECORD_MAC, "Message authentication failure"); - } - - cbc_decrypt_record(record_contents, enc_size, cs, *bc); - - // 0 if padding was invalid, otherwise 1 + padding_bytes - u16bit pad_size = tls_padding_check(record_contents, enc_size); - - const byte* plaintext_block = &record_contents[iv_size]; - const u16bit plaintext_length = enc_size - iv_size - pad_size; - - output.assign(plaintext_block, plaintext_block + plaintext_length); - } - } + const size_t offset = output.size(); + output += std::make_pair(msg, msg_length); + aead->finish(output, offset); } size_t read_tls_record(secure_vector<byte>& readbuf, diff --git a/src/lib/tls/tls_record.h b/src/lib/tls/tls_record.h index 4420a9c66..b17d0a7b6 100644 --- a/src/lib/tls/tls_record.h +++ b/src/lib/tls/tls_record.h @@ -44,7 +44,7 @@ class Connection_Cipher_State AEAD_Mode* aead() { return m_aead.get(); } - std::vector<byte> aead_nonce(u64bit seq); + std::vector<byte> aead_nonce(u64bit seq, RandomNumberGenerator& rng); std::vector<byte> aead_nonce(const byte record[], size_t record_len, u64bit seq); @@ -52,26 +52,9 @@ class Connection_Cipher_State Protocol_Version version, u16bit ptext_length); - BlockCipher* block_cipher() { return m_block_cipher.get(); } - - MessageAuthenticationCode* mac() { return m_mac.get(); } - - secure_vector<byte>& cbc_state() { return m_block_cipher_cbc_state; } - - size_t block_size() const { return m_block_size; } - - size_t mac_size() const { return m_mac->output_length(); } - - size_t iv_size() const { return m_iv_size; } - - size_t nonce_bytes_from_record() const { return m_nonce_bytes_from_record; } - size_t nonce_bytes_from_handshake() const { return m_nonce_bytes_from_handshake; } - - bool uses_encrypt_then_mac() const { return m_uses_encrypt_then_mac; } - - bool cbc_without_explicit_iv() const - { return (m_block_size > 0) && (m_iv_size == 0); } + size_t nonce_bytes_from_record() const { return m_nonce_bytes_from_record; } + bool cbc_nonce() const { return m_cbc_nonce; } std::chrono::seconds age() const { @@ -81,19 +64,12 @@ class Connection_Cipher_State private: std::chrono::system_clock::time_point m_start_time; - std::unique_ptr<BlockCipher> m_block_cipher; - secure_vector<byte> m_block_cipher_cbc_state; - std::unique_ptr<MessageAuthenticationCode> m_mac; - std::unique_ptr<AEAD_Mode> m_aead; - std::vector<byte> m_nonce; - size_t m_block_size = 0; + std::vector<byte> m_nonce; size_t m_nonce_bytes_from_handshake; size_t m_nonce_bytes_from_record; - size_t m_iv_size = 0; - - bool m_uses_encrypt_then_mac; + bool m_cbc_nonce; }; class Record diff --git a/src/tests/data/tls/client_hello.vec b/src/tests/data/tls/client_hello.vec index d629e3f6e..aa8c03258 100644 --- a/src/tests/data/tls/client_hello.vec +++ b/src/tests/data/tls/client_hello.vec @@ -13,13 +13,13 @@ Exception = # with extensions: point formats, ec curves, session ticket, signature algorithms, heartbeat (point formats and heartbeat not supported, empty renegotiation generated) Buffer = 0303871e18983024eaee1be8ae6607d5ecad941d33fd7fc1d8554a9e1fbfda8d30880000aac030c02cc028c024c014c00a00a500a300a1009f006b006a0069006800390038003700360088008700860085c032c02ec02ac026c00fc005009d003d00350084c02fc02bc027c023c013c00900a400a200a0009e00670040003f003e0033003200310030009a0099009800970045004400430042c031c02dc029c025c00ec004009c003c002f00960041c011c007c00cc00200050004c012c008001600130010000dc00dc003000a00ff01000055000b000403000102000a001c001a00170019001c001b0018001a0016000e000d000b000c0009000a00230000000d0020001e060106020603050105020503040104020403030103020303020102020203000f000101 Protocol = 0303 -AdditionalData = 000A000D0023FF01 +AdditionalData = 000A000B000D0023FF01 Exception = # with extensions: point formats, ec curves, session ticket, signature algorithms, heartbeat, Encrypt-then-MAC, Extended Master Secret (point formats and heartbeat not supported, empty renegotiation generated) Buffer = 0303e00da23523058b5dc9c445d97b2bb6315b019e97838ac4f16c23b2cb031b6a490000e2c0afc0adc030c02cc028c024c014c00ac0a3c09f00a500a300a1009f006b006a006900680039003800370036cca9cca8c077c073ccaa00c400c300c200c10088008700860085c032c02ec02ac026c00fc005c079c075c0a1c09d009d003d003500c00084c0aec0acc02fc02bc027c023c013c009c0a2c09e00a400a200a0009e00670040003f003e0033003200310030c076c07200be00bd00bc00bb009a0099009800970045004400430042c031c02dc029c025c00ec004c078c074c0a0c09c009c003c002f00ba009600410007c012c008001600130010000dc00dc003000a00ff0100005f000b000403000102000a001c001a00170019001c001b0018001a0016000e000d000b000c0009000a00230000000d00220020060106020603050105020503040104020403030103020303020102020203eded000f0001010016000000170000 Protocol = 0303 -AdditionalData = 000A000D001600170023FF01 +AdditionalData = 000A000B000D001600170023FF01 Exception = # empty diff --git a/src/tests/data/tls/server_hello.vec b/src/tests/data/tls/server_hello.vec index 64ec40b80..f3bf889cb 100644 --- a/src/tests/data/tls/server_hello.vec +++ b/src/tests/data/tls/server_hello.vec @@ -9,14 +9,14 @@ Buffer = 0303ffea0bcfba564a4ce177c6a444b0ebdff5629b277293c618c1125f231e8628dd00c030000016ff01000100000b00040300010200230000000f000101 Protocol = 0303 Ciphersuite = C030 -AdditionalData = 0023FF01 +AdditionalData = 000B0023FF01 Exception = # correct, with session ticket, extended master secret, and renegotiation info Buffer = 03019f9cafa88664d9095f85dd64a39e5dd5c09f5a4a5362938af3718ee4e818af6a00c03000001aff01000100000b00040300010200230000000f00010100170000 Protocol = 0301 Ciphersuite = C030 -AdditionalData = 00170023FF01 +AdditionalData = 000B00170023FF01 Exception = # incorrect, corrupted diff --git a/src/tests/unit_tls.cpp b/src/tests/unit_tls.cpp index de54c9747..f869f426b 100644 --- a/src/tests/unit_tls.cpp +++ b/src/tests/unit_tls.cpp @@ -118,9 +118,10 @@ class Credentials_Manager_Test : public Botan::Credentials_Manager bool m_provides_client_certs; }; -Botan::Credentials_Manager* create_creds(bool client_type) +Botan::Credentials_Manager* create_creds(Botan::RandomNumberGenerator& rng, + bool with_client_certs = false) { - std::unique_ptr<Botan::Private_Key> ca_key(new Botan::RSA_PrivateKey(Test::rng(), 1024)); + std::unique_ptr<Botan::Private_Key> ca_key(new Botan::RSA_PrivateKey(rng, 1024)); Botan::X509_Cert_Options ca_opts; ca_opts.common_name = "Test CA"; @@ -131,9 +132,9 @@ Botan::Credentials_Manager* create_creds(bool client_type) Botan::X509::create_self_signed_cert(ca_opts, *ca_key, "SHA-256", - Test::rng()); + rng); - Botan::Private_Key* server_key = new Botan::RSA_PrivateKey(Test::rng(), 1024); + Botan::Private_Key* server_key = new Botan::RSA_PrivateKey(rng, 1024); Botan::X509_Cert_Options server_opts; server_opts.common_name = "server.example.com"; @@ -142,7 +143,7 @@ Botan::Credentials_Manager* create_creds(bool client_type) Botan::PKCS10_Request req = Botan::X509::create_cert_req(server_opts, *server_key, "SHA-256", - Test::rng()); + rng); Botan::X509_CA ca(ca_cert, *ca_key, "SHA-256"); @@ -152,12 +153,12 @@ Botan::Credentials_Manager* create_creds(bool client_type) Botan::X509_Time end_time(now + years(1)); Botan::X509_Certificate server_cert = ca.sign_request(req, - Test::rng(), + rng, start_time, end_time); Credentials_Manager_Test* cmt (new Credentials_Manager_Test(server_cert, ca_cert, server_key)); - cmt->m_provides_client_certs = client_type; + cmt->m_provides_client_certs = with_client_certs; return cmt; } @@ -176,11 +177,10 @@ void alert_cb_with_data(Botan::TLS::Alert, const byte[], size_t) Test::Result test_tls_handshake(Botan::TLS::Protocol_Version offer_version, Botan::Credentials_Manager& creds, - Botan::TLS::Policy& client_policy, - Botan::TLS::Policy& server_policy ) + const Botan::TLS::Policy& client_policy, + const Botan::TLS::Policy& server_policy, + Botan::RandomNumberGenerator& rng) { - Botan::RandomNumberGenerator& rng = Test::rng(); - Botan::TLS::Session_Manager_In_Memory server_sessions(rng); Botan::TLS::Session_Manager_In_Memory client_sessions(rng); @@ -197,9 +197,12 @@ Test::Result test_tls_handshake(Botan::TLS::Protocol_Version offer_version, auto handshake_complete = [&](const Botan::TLS::Session& session) -> bool { handshake_done = true; - result.test_note("Session established " + session.version().to_string() + " " + - session.ciphersuite().to_string() + " " + - Botan::hex_encode(session.session_id())); + const std::string session_report = + "Session established " + session.version().to_string() + " " + + session.ciphersuite().to_string() + " " + + Botan::hex_encode(session.session_id()); + + result.test_note(session_report); if(session.version() != offer_version) { @@ -311,7 +314,9 @@ Test::Result test_tls_handshake(Botan::TLS::Protocol_Version offer_version, if(rounds > 25) { if(r <= 2) + { result.test_failure("Still here after many rounds, deadlock?"); + } break; } @@ -320,21 +325,39 @@ Test::Result test_tls_handshake(Botan::TLS::Protocol_Version offer_version, if(client->is_active() && client_sent.empty()) { - // Choose a len between 1 and 511 - const size_t c_len = 1 + rng.next_byte() + rng.next_byte(); + // Choose random application data to send + const size_t c_len = 1 + (static_cast<size_t>(rng.next_byte()) << 4) ^ rng.next_byte(); client_sent = unlock(rng.random_vec(c_len)); - // TODO send in several records - client->send(client_sent); + size_t sent_so_far = 0; + while(sent_so_far != client_sent.size()) + { + const size_t left = client_sent.size() - sent_so_far; + const size_t rnd12 = (rng.next_byte() << 4) ^ rng.next_byte(); + const size_t sending = std::min(left, rnd12); + + client->send(&client_sent[sent_so_far], sending); + sent_so_far += sending; + } } if(server->is_active() && server_sent.empty()) { result.test_eq("server->protocol", server->next_protocol(), "test/3"); - const size_t s_len = 1 + rng.next_byte() + rng.next_byte(); + const size_t s_len = 1 + (static_cast<size_t>(rng.next_byte()) << 4) ^ rng.next_byte(); server_sent = unlock(rng.random_vec(s_len)); - server->send(server_sent); + + size_t sent_so_far = 0; + while(sent_so_far != server_sent.size()) + { + const size_t left = server_sent.size() - sent_so_far; + const size_t rnd12 = (rng.next_byte() << 4) ^ rng.next_byte(); + const size_t sending = std::min(left, rnd12); + + server->send(&server_sent[sent_so_far], sending); + sent_so_far += sending; + } } const bool corrupt_client_data = (r == 3); @@ -362,7 +385,7 @@ Test::Result test_tls_handshake(Botan::TLS::Protocol_Version offer_version, result.test_lt("Total requested is readonable", total_consumed, 128*1024)) { input.resize(needed); - Test::rng().randomize(input.data(), input.size()); + rng.randomize(input.data(), input.size()); needed = server->received_data(input.data(), input.size()); total_consumed += needed; } @@ -391,7 +414,7 @@ Test::Result test_tls_handshake(Botan::TLS::Protocol_Version offer_version, while(needed > 0 && result.test_lt("Never requesting more than max protocol len", needed, 18*1024)) { input.resize(needed); - Test::rng().randomize(input.data(), input.size()); + rng.randomize(input.data(), input.size()); needed = client->received_data(input.data(), input.size()); total_consumed += needed; } @@ -461,20 +484,20 @@ Test::Result test_tls_handshake(Botan::TLS::Protocol_Version offer_version, Test::Result test_tls_handshake(Botan::TLS::Protocol_Version offer_version, Botan::Credentials_Manager& creds, - Botan::TLS::Policy& policy ) + const Botan::TLS::Policy& policy, + Botan::RandomNumberGenerator& rng) { - return test_tls_handshake(offer_version, creds, policy, policy); + return test_tls_handshake(offer_version, creds, policy, policy, rng); } Test::Result test_dtls_handshake(Botan::TLS::Protocol_Version offer_version, Botan::Credentials_Manager& creds, - Botan::TLS::Policy& client_policy, - Botan::TLS::Policy& server_policy ) + const Botan::TLS::Policy& client_policy, + const Botan::TLS::Policy& server_policy, + Botan::RandomNumberGenerator& rng) { BOTAN_ASSERT(offer_version.is_datagram_protocol(), "Test is for datagram version"); - Botan::RandomNumberGenerator& rng = Test::rng(); - Botan::TLS::Session_Manager_In_Memory server_sessions(rng); Botan::TLS::Session_Manager_In_Memory client_sessions(rng); @@ -592,7 +615,7 @@ Test::Result test_dtls_handshake(Botan::TLS::Protocol_Version offer_version, while(true) { // TODO: client and server should be in different threads - std::this_thread::sleep_for(std::chrono::milliseconds(rng.next_byte() % 2)); + std::this_thread::sleep_for(std::chrono::microseconds(rng.next_byte() % 128)); ++rounds; if(rounds > 100) @@ -644,7 +667,7 @@ Test::Result test_dtls_handshake(Botan::TLS::Protocol_Version offer_version, if(needed > 0 && result.test_lt("Never requesting more than max protocol len", needed, 18*1024)) { input.resize(needed); - Test::rng().randomize(input.data(), input.size()); + rng.randomize(input.data(), input.size()); client->received_data(input.data(), input.size()); } } @@ -684,7 +707,7 @@ Test::Result test_dtls_handshake(Botan::TLS::Protocol_Version offer_version, if(needed > 0 && result.test_lt("Never requesting more than max protocol len", needed, 18*1024)) { input.resize(needed); - Test::rng().randomize(input.data(), input.size()); + rng.randomize(input.data(), input.size()); client->received_data(input.data(), input.size()); } } @@ -761,10 +784,11 @@ Test::Result test_dtls_handshake(Botan::TLS::Protocol_Version offer_version, } Test::Result test_dtls_handshake(Botan::TLS::Protocol_Version offer_version, - Botan::Credentials_Manager& creds, - Botan::TLS::Policy& policy) + Botan::Credentials_Manager& creds, + const Botan::TLS::Policy& policy, + Botan::RandomNumberGenerator& rng) { - return test_dtls_handshake(offer_version, creds, policy, policy); + return test_dtls_handshake(offer_version, creds, policy, policy, rng); } class Test_Policy : public Botan::TLS::Text_Policy @@ -781,93 +805,132 @@ class Test_Policy : public Botan::TLS::Text_Policy }; + class TLS_Unit_Tests : public Test { + private: + void test_with_policy(std::vector<Test::Result>& results, + const std::vector<Botan::TLS::Protocol_Version>& versions, + Botan::Credentials_Manager& creds, + const Botan::TLS::Policy& policy) + { + Botan::RandomNumberGenerator& rng = Test::rng(); + + for(auto&& version : versions) + { + if(version.is_datagram_protocol()) + results.push_back(test_dtls_handshake(version, creds, policy, rng)); + else + results.push_back(test_tls_handshake(version, creds, policy, rng)); + } + } + + void test_all_versions(std::vector<Test::Result>& results, + Botan::Credentials_Manager& creds, + const std::string& kex_policy, + const std::string& cipher_policy, + const std::string& mac_policy, + const std::string& etm_policy) + { + Test_Policy policy; + policy.set("ciphers", cipher_policy); + policy.set("macs", mac_policy); + policy.set("key_exchange_methods", kex_policy); + policy.set("negotiate_encrypt_then_mac", etm_policy); + + std::vector<Botan::TLS::Protocol_Version> versions = { + Botan::TLS::Protocol_Version::TLS_V10, + Botan::TLS::Protocol_Version::TLS_V11, + Botan::TLS::Protocol_Version::TLS_V12, + Botan::TLS::Protocol_Version::DTLS_V10, + Botan::TLS::Protocol_Version::DTLS_V12 + }; + + return test_with_policy(results, versions, creds, policy); + } + + void test_modern_versions(std::vector<Test::Result>& results, + Botan::Credentials_Manager& creds, + const std::string& kex_policy, + const std::string& cipher_policy, + const std::string& mac_policy = "AEAD", + const std::map<std::string, std::string>& extra_policies = {}) + { + Test_Policy policy; + policy.set("ciphers", cipher_policy); + policy.set("macs", mac_policy); + policy.set("key_exchange_methods", kex_policy); + + for(auto&& kv : extra_policies) + policy.set(kv.first, kv.second); + + std::vector<Botan::TLS::Protocol_Version> versions = { + Botan::TLS::Protocol_Version::TLS_V12, + Botan::TLS::Protocol_Version::DTLS_V12 + }; + + return test_with_policy(results, versions, creds, policy); + } + public: std::vector<Test::Result> run() override { - std::unique_ptr<Botan::Credentials_Manager> basic_creds(create_creds(false)); - std::unique_ptr<Botan::Credentials_Manager> basic_creds_with_client_cert(create_creds(true)); + Botan::RandomNumberGenerator& rng = Test::rng(); + + std::unique_ptr<Botan::Credentials_Manager> creds(create_creds(rng)); std::vector<Test::Result> results; - Test_Policy policy; - results.push_back(test_tls_handshake(Botan::TLS::Protocol_Version::TLS_V10, *basic_creds, policy)); - - results.push_back(test_tls_handshake(Botan::TLS::Protocol_Version::TLS_V11, *basic_creds, policy)); - results.push_back(test_tls_handshake(Botan::TLS::Protocol_Version::TLS_V12, *basic_creds, policy)); - results.push_back(test_dtls_handshake(Botan::TLS::Protocol_Version::DTLS_V10, *basic_creds, policy)); - results.push_back(test_dtls_handshake(Botan::TLS::Protocol_Version::DTLS_V12, *basic_creds, policy)); - - policy.set("key_exchange_methods", "RSA"); - results.push_back(test_tls_handshake(Botan::TLS::Protocol_Version::TLS_V10, *basic_creds, policy)); - results.push_back(test_tls_handshake(Botan::TLS::Protocol_Version::TLS_V11, *basic_creds, policy)); - results.push_back(test_tls_handshake(Botan::TLS::Protocol_Version::TLS_V12, *basic_creds, policy)); - results.push_back(test_dtls_handshake(Botan::TLS::Protocol_Version::DTLS_V10, *basic_creds, policy)); - results.push_back(test_dtls_handshake(Botan::TLS::Protocol_Version::DTLS_V12, *basic_creds, policy)); - - policy.set("key_exchange_methods", "DH"); - results.push_back(test_tls_handshake(Botan::TLS::Protocol_Version::TLS_V10, *basic_creds, policy)); - results.push_back(test_tls_handshake(Botan::TLS::Protocol_Version::TLS_V11, *basic_creds, policy)); - - policy.set("key_exchange_methods", "ECDH"); - results.push_back(test_tls_handshake(Botan::TLS::Protocol_Version::TLS_V12, *basic_creds, policy)); - results.push_back(test_dtls_handshake(Botan::TLS::Protocol_Version::DTLS_V10, *basic_creds, policy)); - results.push_back(test_dtls_handshake(Botan::TLS::Protocol_Version::DTLS_V12, *basic_creds, policy)); - - policy.set("ciphers", "AES-128"); - results.push_back(test_tls_handshake(Botan::TLS::Protocol_Version::TLS_V10, *basic_creds, policy)); - results.push_back(test_tls_handshake(Botan::TLS::Protocol_Version::TLS_V11, *basic_creds, policy)); - results.push_back(test_tls_handshake(Botan::TLS::Protocol_Version::TLS_V12, *basic_creds, policy)); - results.push_back(test_dtls_handshake(Botan::TLS::Protocol_Version::DTLS_V10, *basic_creds, policy)); - results.push_back(test_dtls_handshake(Botan::TLS::Protocol_Version::DTLS_V12, *basic_creds, policy)); - results.push_back(test_tls_handshake(Botan::TLS::Protocol_Version::TLS_V12, *basic_creds_with_client_cert, policy)); + for(std::string etm_setting : { "true", "false" }) + { + test_all_versions(results, *creds, "RSA", "AES-128", "SHA-256 SHA-1", etm_setting); + test_all_versions(results, *creds, "ECDH", "AES-128", "SHA-256 SHA-1", etm_setting); + + test_all_versions(results, *creds, "RSA", "AES-256", "SHA-1", etm_setting); + test_all_versions(results, *creds, "ECDH", "AES-256", "SHA-1", etm_setting); + +#if defined(BOTAN_HAS_CAMELLIA) + test_all_versions(results, *creds, "RSA", "Camellia-128", "SHA-256", etm_setting); + test_all_versions(results, *creds, "ECDH", "Camellia-256", "SHA-256 SHA-384", etm_setting); +#endif + +#if defined(BOTAN_HAS_DES) + test_all_versions(results, *creds, "RSA", "3DES", "SHA-1", etm_setting); + test_all_versions(results, *creds, "ECDH", "3DES", "SHA-1", etm_setting); +#endif + +#if defined(BOTAN_HAS_SEED) + test_all_versions(results, *creds, "RSA", "SEED", "SHA-1", etm_setting); +#endif + } + + test_modern_versions(results, *creds, "DH", "AES-128", "SHA-256"); + test_modern_versions(results, *creds, "RSA", "AES-128/GCM"); + test_modern_versions(results, *creds, "ECDH", "AES-128/GCM"); + test_modern_versions(results, *creds, "ECDH", "AES-128/GCM", "AEAD", + { { "use_ecc_point_compression", "true" } }); + + std::unique_ptr<Botan::Credentials_Manager> creds_with_client_cert(create_creds(rng, true)); + test_modern_versions(results, *creds_with_client_cert, "ECDH", "AES-256/GCM"); #if defined(BOTAN_HAS_AEAD_OCB) - policy.set("ciphers", "AES-128/OCB(12)"); - results.push_back(test_tls_handshake(Botan::TLS::Protocol_Version::TLS_V12, *basic_creds, policy)); - results.push_back(test_dtls_handshake(Botan::TLS::Protocol_Version::DTLS_V12, *basic_creds, policy)); + test_modern_versions(results, *creds, "ECDH", "AES-128/OCB(12)"); #endif #if defined(BOTAN_HAS_AEAD_CHACHA20_POLY1305) - policy.set("ciphers", "ChaCha20Poly1305"); - results.push_back(test_tls_handshake(Botan::TLS::Protocol_Version::TLS_V12, *basic_creds, policy)); - results.push_back(test_dtls_handshake(Botan::TLS::Protocol_Version::DTLS_V12, *basic_creds, policy)); + test_modern_versions(results, *creds, "ECDH", "ChaCha20Poly1305"); #endif - policy.set("ciphers", "AES-128/GCM"); - policy.set("key_exchange_methods", "PSK"); - results.push_back(test_tls_handshake(Botan::TLS::Protocol_Version::TLS_V12, *basic_creds, policy)); - results.push_back(test_dtls_handshake(Botan::TLS::Protocol_Version::DTLS_V12, *basic_creds, policy)); + test_modern_versions(results, *creds, "PSK", "AES-128/GCM"); + +#if defined(BOTAN_HAS_CCM) + test_modern_versions(results, *creds, "PSK", "AES-128/CCM"); + test_modern_versions(results, *creds, "PSK", "AES-128/CCM(8)"); +#endif // For whatever reason no (EC)DHE_PSK GCM ciphersuites are defined - policy.set("ciphers", "AES-128"); - policy.set("key_exchange_methods", "ECDHE_PSK"); - results.push_back(test_tls_handshake(Botan::TLS::Protocol_Version::TLS_V12, *basic_creds, policy)); - results.push_back(test_dtls_handshake(Botan::TLS::Protocol_Version::DTLS_V12, *basic_creds, policy)); - - policy.set("key_exchange_methods", "DHE_PSK"); - results.push_back(test_tls_handshake(Botan::TLS::Protocol_Version::TLS_V12, *basic_creds, policy)); - results.push_back(test_dtls_handshake(Botan::TLS::Protocol_Version::DTLS_V12, *basic_creds, policy)); - - policy.set("negotiate_encrypt_then_mac", "false"); - policy.set("key_exchange_methods", "ECDH"); - policy.set("ciphers", "AES-128"); - Test_Policy server_policy; - server_policy.set("key_exchange_methods", "ECDH"); - server_policy.set("ciphers", "AES-128"); - server_policy.set("negotiate_encrypt_then_mac", "true"); - results.push_back(test_tls_handshake(Botan::TLS::Protocol_Version::TLS_V10, *basic_creds, policy, server_policy)); - results.push_back(test_tls_handshake(Botan::TLS::Protocol_Version::TLS_V11, *basic_creds, policy, server_policy)); - results.push_back(test_tls_handshake(Botan::TLS::Protocol_Version::TLS_V12, *basic_creds, policy, server_policy)); - results.push_back(test_dtls_handshake(Botan::TLS::Protocol_Version::DTLS_V10, *basic_creds, policy, server_policy)); - results.push_back(test_dtls_handshake(Botan::TLS::Protocol_Version::DTLS_V12, *basic_creds, policy, server_policy)); - - policy.set("negotiate_encrypt_then_mac", "true"); - policy.set("ciphers", "AES-128/GCM"); - server_policy.set("ciphers", "AES-128/GCM"); - results.push_back(test_tls_handshake(Botan::TLS::Protocol_Version::TLS_V12, *basic_creds, policy, server_policy)); - results.push_back(test_dtls_handshake(Botan::TLS::Protocol_Version::DTLS_V12, *basic_creds, policy, server_policy)); - + test_modern_versions(results, *creds, "ECDHE_PSK", "AES-128", "SHA-256"); + test_modern_versions(results, *creds, "DHE_PSK", "AES-128", "SHA-1"); + return results; } |