diff options
author | Matt Caswell <matt@openssl.org> | 2016-10-12 10:56:29 +0100 |
---|---|---|
committer | Matt Caswell <matt@openssl.org> | 2016-11-04 10:38:54 +0000 |
commit | 7b73b7beda88501b9d6e062a5dbf9a72f791dfda (patch) | |
tree | b543407170dba31204f5afc0015fec5dff1eb163 /test/ossl_shim | |
parent | 8c6c5077b211ea73223c950edff98f0891853dc8 (diff) |
Rebase shim against latest boringssl code
Numerous conflicts resolved. rebase was against commit 490469f850.
Reviewed-by: Richard Levitte <levitte@openssl.org>
Diffstat (limited to 'test/ossl_shim')
-rw-r--r-- | test/ossl_shim/Makefile | 2 | ||||
-rw-r--r-- | test/ossl_shim/async_bio.cc | 9 | ||||
-rw-r--r-- | test/ossl_shim/async_bio.h | 7 | ||||
-rw-r--r-- | test/ossl_shim/crypto/scoped_types.h | 102 | ||||
-rw-r--r-- | test/ossl_shim/include/openssl/base.h | 158 | ||||
-rw-r--r-- | test/ossl_shim/ossl_shim.cc | 395 | ||||
-rw-r--r-- | test/ossl_shim/packeted_bio.cc | 203 | ||||
-rw-r--r-- | test/ossl_shim/packeted_bio.h | 33 | ||||
-rw-r--r-- | test/ossl_shim/test_config.cc | 37 | ||||
-rw-r--r-- | test/ossl_shim/test_config.h | 18 |
10 files changed, 653 insertions, 311 deletions
diff --git a/test/ossl_shim/Makefile b/test/ossl_shim/Makefile index 688317cc79..bab8441646 100644 --- a/test/ossl_shim/Makefile +++ b/test/ossl_shim/Makefile @@ -1,7 +1,7 @@ all: ossl_shim ossl_shim: ../../libssl.a ../../libcrypto.a *.cc - g++ -g -std=c++11 -I. -I../../include *.cc \ + g++ -g -std=c++11 -I. -Iinclude -I../../include *.cc \ ../../libssl.a ../../libcrypto.a -ldl -lpthread \ -o ossl_shim diff --git a/test/ossl_shim/async_bio.cc b/test/ossl_shim/async_bio.cc index 825b22372f..b905a0e697 100644 --- a/test/ossl_shim/async_bio.cc +++ b/test/ossl_shim/async_bio.cc @@ -17,6 +17,7 @@ #include <errno.h> #include <string.h> +#include <openssl/bio.h> #include <openssl/crypto.h> @@ -158,12 +159,12 @@ static const BIO_METHOD *AsyncMethod(void) } // namespace -ScopedBIO AsyncBioCreate() { - return ScopedBIO(BIO_new(AsyncMethod())); +bssl::UniquePtr<BIO> AsyncBioCreate() { + return bssl::UniquePtr<BIO>(BIO_new(AsyncMethod())); } -ScopedBIO AsyncBioCreateDatagram() { - ScopedBIO ret(BIO_new(AsyncMethod())); +bssl::UniquePtr<BIO> AsyncBioCreateDatagram() { + bssl::UniquePtr<BIO> ret(BIO_new(AsyncMethod())); if (!ret) { return nullptr; } diff --git a/test/ossl_shim/async_bio.h b/test/ossl_shim/async_bio.h index 9466a8ca3d..8a8ee18fba 100644 --- a/test/ossl_shim/async_bio.h +++ b/test/ossl_shim/async_bio.h @@ -15,22 +15,21 @@ #ifndef HEADER_ASYNC_BIO #define HEADER_ASYNC_BIO +#include <openssl/base.h> #include <openssl/bio.h> -#include "crypto/scoped_types.h" - // AsyncBioCreate creates a filter BIO for testing asynchronous state // machines which consume a stream socket. Reads and writes will fail // and return EAGAIN unless explicitly allowed. Each async BIO has a // read quota and a write quota. Initially both are zero. As each is // incremented, bytes are allowed to flow through the BIO. -ScopedBIO AsyncBioCreate(); +bssl::UniquePtr<BIO> AsyncBioCreate(); // AsyncBioCreateDatagram creates a filter BIO for testing for // asynchronous state machines which consume datagram sockets. The read // and write quota count in packets rather than bytes. -ScopedBIO AsyncBioCreateDatagram(); +bssl::UniquePtr<BIO> AsyncBioCreateDatagram(); // AsyncBioAllowRead increments |bio|'s read quota by |count|. void AsyncBioAllowRead(BIO *bio, size_t count); diff --git a/test/ossl_shim/crypto/scoped_types.h b/test/ossl_shim/crypto/scoped_types.h deleted file mode 100644 index a1f6851228..0000000000 --- a/test/ossl_shim/crypto/scoped_types.h +++ /dev/null @@ -1,102 +0,0 @@ -/* 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. */ - -#ifndef OPENSSL_HEADER_CRYPTO_TEST_SCOPED_TYPES_H -#define OPENSSL_HEADER_CRYPTO_TEST_SCOPED_TYPES_H - -#include <stdint.h> -#include <stdio.h> - -#include <memory> - -#include <openssl/asn1.h> -#include <openssl/bio.h> -#include <openssl/bn.h> -#include <openssl/crypto.h> -#include <openssl/cmac.h> -#include <openssl/dh.h> -#include <openssl/ecdsa.h> -#include <openssl/ec.h> -#include <openssl/evp.h> -#include <openssl/pkcs12.h> -#include <openssl/rsa.h> -#include <openssl/stack.h> -#include <openssl/x509.h> - - -template<typename T, void (*func)(T*)> -struct OpenSSLDeleter { - void operator()(T *obj) { - func(obj); - } -}; - -template<typename StackType, typename T, void (*func)(T*)> -struct OpenSSLStackDeleter { - void operator()(StackType *obj) { - sk_pop_free(reinterpret_cast<_STACK*>(obj), - reinterpret_cast<void (*)(void *)>(func)); - } -}; - -template<typename T> -struct OpenSSLFree { - void operator()(T *buf) { - OPENSSL_free(buf); - } -}; - -struct FileCloser { - void operator()(FILE *file) { - fclose(file); - } -}; - -template<typename T, void (*func)(T*)> -using ScopedOpenSSLType = std::unique_ptr<T, OpenSSLDeleter<T, func>>; - -template<typename StackType, typename T, void (*func)(T*)> -using ScopedOpenSSLStack = - std::unique_ptr<StackType, OpenSSLStackDeleter<StackType, T, func>>; - -using ScopedASN1_TYPE = ScopedOpenSSLType<ASN1_TYPE, ASN1_TYPE_free>; -using ScopedBIO = ScopedOpenSSLType<BIO, BIO_vfree>; -using ScopedBIGNUM = ScopedOpenSSLType<BIGNUM, BN_free>; -using ScopedBN_CTX = ScopedOpenSSLType<BN_CTX, BN_CTX_free>; -using ScopedBN_MONT_CTX = ScopedOpenSSLType<BN_MONT_CTX, BN_MONT_CTX_free>; -using ScopedCMAC_CTX = ScopedOpenSSLType<CMAC_CTX, CMAC_CTX_free>; -using ScopedDH = ScopedOpenSSLType<DH, DH_free>; -using ScopedECDSA_SIG = ScopedOpenSSLType<ECDSA_SIG, ECDSA_SIG_free>; -using ScopedEC_GROUP = ScopedOpenSSLType<EC_GROUP, EC_GROUP_free>; -using ScopedEC_KEY = ScopedOpenSSLType<EC_KEY, EC_KEY_free>; -using ScopedEC_POINT = ScopedOpenSSLType<EC_POINT, EC_POINT_free>; -using ScopedEVP_PKEY = ScopedOpenSSLType<EVP_PKEY, EVP_PKEY_free>; -using ScopedEVP_PKEY_CTX = ScopedOpenSSLType<EVP_PKEY_CTX, EVP_PKEY_CTX_free>; -using ScopedPKCS8_PRIV_KEY_INFO = ScopedOpenSSLType<PKCS8_PRIV_KEY_INFO, - PKCS8_PRIV_KEY_INFO_free>; -using ScopedPKCS12 = ScopedOpenSSLType<PKCS12, PKCS12_free>; -using ScopedRSA = ScopedOpenSSLType<RSA, RSA_free>; -using ScopedX509 = ScopedOpenSSLType<X509, X509_free>; -using ScopedX509_ALGOR = ScopedOpenSSLType<X509_ALGOR, X509_ALGOR_free>; -using ScopedX509_SIG = ScopedOpenSSLType<X509_SIG, X509_SIG_free>; -using ScopedX509_STORE_CTX = ScopedOpenSSLType<X509_STORE_CTX, X509_STORE_CTX_free>; - -using ScopedX509Stack = ScopedOpenSSLStack<STACK_OF(X509), X509, X509_free>; - -using ScopedOpenSSLBytes = std::unique_ptr<uint8_t, OpenSSLFree<uint8_t>>; -using ScopedOpenSSLString = std::unique_ptr<char, OpenSSLFree<char>>; - -using ScopedFILE = std::unique_ptr<FILE, FileCloser>; - -#endif // OPENSSL_HEADER_CRYPTO_TEST_SCOPED_TYPES_H diff --git a/test/ossl_shim/include/openssl/base.h b/test/ossl_shim/include/openssl/base.h new file mode 100644 index 0000000000..755d520032 --- /dev/null +++ b/test/ossl_shim/include/openssl/base.h @@ -0,0 +1,158 @@ +/* ==================================================================== + * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. All advertising materials mentioning features or use of this + * software must display the following acknowledgment: + * "This product includes software developed by the OpenSSL Project + * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" + * + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please contact + * openssl-core@openssl.org. + * + * 5. Products derived from this software may not be called "OpenSSL" + * nor may "OpenSSL" appear in their names without prior written + * permission of the OpenSSL Project. + * + * 6. Redistributions of any form whatsoever must retain the following + * acknowledgment: + * "This product includes software developed by the OpenSSL Project + * for use in the OpenSSL Toolkit (http://www.openssl.org/)" + * + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + * + * This product includes cryptographic software written by Eric Young + * (eay@cryptsoft.com). This product includes software written by Tim + * Hudson (tjh@cryptsoft.com). */ + +#ifndef OPENSSL_HEADER_BASE_H +#define OPENSSL_HEADER_BASE_H + +/* Needed for BORINGSSL_MAKE_DELETER */ +# include <openssl/bio.h> +# include <openssl/evp.h> +# include <openssl/dh.h> +# include <openssl/x509.h> +# include <openssl/ssl.h> + +# define OPENSSL_ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0])) + +/* Temporary TLS1.3 defines until OpenSSL supports these */ +# define TLS1_3_VERSION 0x0304 +# define SSL_OP_NO_TLSv1_3 0 + +extern "C++" { + +#include <memory> + +namespace bssl { + +namespace internal { + +template <typename T> +struct DeleterImpl {}; + +template <typename T> +struct Deleter { + void operator()(T *ptr) { + // Rather than specialize Deleter for each type, we specialize + // DeleterImpl. This allows bssl::UniquePtr<T> to be used while only + // including base.h as long as the destructor is not emitted. This matches + // std::unique_ptr's behavior on forward-declared types. + // + // DeleterImpl itself is specialized in the corresponding module's header + // and must be included to release an object. If not included, the compiler + // will error that DeleterImpl<T> does not have a method Free. + DeleterImpl<T>::Free(ptr); + } +}; + +template <typename T, typename CleanupRet, void (*init)(T *), + CleanupRet (*cleanup)(T *)> +class StackAllocated { + public: + StackAllocated() { init(&ctx_); } + ~StackAllocated() { cleanup(&ctx_); } + + StackAllocated(const StackAllocated<T, CleanupRet, init, cleanup> &) = delete; + T& operator=(const StackAllocated<T, CleanupRet, init, cleanup> &) = delete; + + T *get() { return &ctx_; } + const T *get() const { return &ctx_; } + + void Reset() { + cleanup(&ctx_); + init(&ctx_); + } + + private: + T ctx_; +}; + +} // namespace internal + +#define BORINGSSL_MAKE_DELETER(type, deleter) \ + namespace internal { \ + template <> \ + struct DeleterImpl<type> { \ + static void Free(type *ptr) { deleter(ptr); } \ + }; \ + } + +// This makes a unique_ptr to STACK_OF(type) that owns all elements on the +// stack, i.e. it uses sk_pop_free() to clean up. +#define BORINGSSL_MAKE_STACK_DELETER(type, deleter) \ + namespace internal { \ + template <> \ + struct DeleterImpl<STACK_OF(type)> { \ + static void Free(STACK_OF(type) *ptr) { \ + sk_##type##_pop_free(ptr, deleter); \ + } \ + }; \ + } + +// Holds ownership of heap-allocated BoringSSL structures. Sample usage: +// bssl::UniquePtr<BIO> rsa(RSA_new()); +// bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem())); +template <typename T> +using UniquePtr = std::unique_ptr<T, internal::Deleter<T>>; + +BORINGSSL_MAKE_DELETER(BIO, BIO_free) +BORINGSSL_MAKE_DELETER(EVP_PKEY, EVP_PKEY_free) +BORINGSSL_MAKE_DELETER(DH, DH_free) +BORINGSSL_MAKE_DELETER(X509, X509_free) +BORINGSSL_MAKE_DELETER(SSL, SSL_free) +BORINGSSL_MAKE_DELETER(SSL_CTX, SSL_CTX_free) +BORINGSSL_MAKE_DELETER(SSL_SESSION, SSL_SESSION_free) + +} // namespace bssl + +} /* extern C++ */ + + +#endif /* OPENSSL_HEADER_BASE_H */ diff --git a/test/ossl_shim/ossl_shim.cc b/test/ossl_shim/ossl_shim.cc index b1abcf6d6c..526d614d26 100644 --- a/test/ossl_shim/ossl_shim.cc +++ b/test/ossl_shim/ossl_shim.cc @@ -28,14 +28,15 @@ #include <unistd.h> #else #include <io.h> -#pragma warning(push, 3) +OPENSSL_MSVC_PRAGMA(warning(push, 3)) #include <winsock2.h> #include <ws2tcpip.h> -#pragma warning(pop) +OPENSSL_MSVC_PRAGMA(warning(pop)) -#pragma comment(lib, "Ws2_32.lib") +OPENSSL_MSVC_PRAGMA(comment(lib, "Ws2_32.lib")) #endif +#include <assert.h> #include <inttypes.h> #include <string.h> @@ -49,17 +50,17 @@ #include <openssl/objects.h> #include <openssl/rand.h> #include <openssl/ssl.h> +#include <openssl/x509.h> #include <memory> #include <string> #include <vector> -#include "crypto/scoped_types.h" #include "async_bio.h" #include "packeted_bio.h" -#include "scoped_types.h" #include "test_config.h" +namespace bssl { #if !defined(OPENSSL_SYS_WINDOWS) static int closesocket(int sock) { @@ -81,31 +82,26 @@ static int Usage(const char *program) { } struct TestState { - TestState() { - // MSVC cannot initialize these inline. - memset(&clock, 0, sizeof(clock)); - memset(&clock_delta, 0, sizeof(clock_delta)); - } - // async_bio is async BIO which pauses reads and writes. BIO *async_bio = nullptr; - // clock is the current time for the SSL connection. - timeval clock; - // clock_delta is how far the clock advanced in the most recent failed - // |BIO_read|. - timeval clock_delta; + // packeted_bio is the packeted BIO which simulates read timeouts. + BIO *packeted_bio = nullptr; + bssl::UniquePtr<EVP_PKEY> channel_id; bool cert_ready = false; - ScopedSSL_SESSION session; - ScopedSSL_SESSION pending_session; + bssl::UniquePtr<SSL_SESSION> session; + bssl::UniquePtr<SSL_SESSION> pending_session; bool early_callback_called = false; bool handshake_done = false; // private_key is the underlying private key used when testing custom keys. - ScopedEVP_PKEY private_key; + bssl::UniquePtr<EVP_PKEY> private_key; std::vector<uint8_t> private_key_result; // private_key_retries is the number of times an asynchronous private key // operation has been retried. unsigned private_key_retries = 0; bool got_new_session = false; + bssl::UniquePtr<SSL_SESSION> new_session; + bool ticket_decrypt_done = false; + bool alpn_select_done = false; }; static void TestStateExFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad, @@ -116,11 +112,11 @@ static void TestStateExFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad, static int g_config_index = 0; static int g_state_index = 0; -static bool SetConfigPtr(SSL *ssl, const TestConfig *config) { +static bool SetTestConfig(SSL *ssl, const TestConfig *config) { return SSL_set_ex_data(ssl, g_config_index, (void *)config) == 1; } -static const TestConfig *GetConfigPtr(const SSL *ssl) { +static const TestConfig *GetTestConfig(const SSL *ssl) { return (const TestConfig *)SSL_get_ex_data(ssl, g_config_index); } @@ -137,20 +133,21 @@ static TestState *GetTestState(const SSL *ssl) { return (TestState *)SSL_get_ex_data(ssl, g_state_index); } -static ScopedX509 LoadCertificate(const std::string &file) { - ScopedBIO bio(BIO_new(BIO_s_file())); +static bssl::UniquePtr<X509> LoadCertificate(const std::string &file) { + bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_file())); if (!bio || !BIO_read_filename(bio.get(), file.c_str())) { return nullptr; } - return ScopedX509(PEM_read_bio_X509(bio.get(), NULL, NULL, NULL)); + return bssl::UniquePtr<X509>(PEM_read_bio_X509(bio.get(), NULL, NULL, NULL)); } -static ScopedEVP_PKEY LoadPrivateKey(const std::string &file) { - ScopedBIO bio(BIO_new(BIO_s_file())); +static bssl::UniquePtr<EVP_PKEY> LoadPrivateKey(const std::string &file) { + bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_file())); if (!bio || !BIO_read_filename(bio.get(), file.c_str())) { return nullptr; } - return ScopedEVP_PKEY(PEM_read_bio_PrivateKey(bio.get(), NULL, NULL, NULL)); + return bssl::UniquePtr<EVP_PKEY>( + PEM_read_bio_PrivateKey(bio.get(), NULL, NULL, NULL)); } template<typename T> @@ -160,15 +157,20 @@ struct Free { } }; -static bool GetCertificate(SSL *ssl, ScopedX509 *out_x509, - ScopedEVP_PKEY *out_pkey) { - const TestConfig *config = GetConfigPtr(ssl); +static bool GetCertificate(SSL *ssl, bssl::UniquePtr<X509> *out_x509, + bssl::UniquePtr<EVP_PKEY> *out_pkey) { + const TestConfig *config = GetTestConfig(ssl); if (!config->digest_prefs.empty()) { fprintf(stderr, "Digest prefs not supported.\n"); return false; } + if (!config->signing_prefs.empty()) { + fprintf(stderr, "Set signing algorithm prefs not supported\n"); + return false; + } + if (!config->key_file.empty()) { *out_pkey = LoadPrivateKey(config->key_file.c_str()); if (!*out_pkey) { @@ -189,15 +191,15 @@ static bool GetCertificate(SSL *ssl, ScopedX509 *out_x509, } static bool InstallCertificate(SSL *ssl) { - ScopedX509 x509; - ScopedEVP_PKEY pkey; + bssl::UniquePtr<X509> x509; + bssl::UniquePtr<EVP_PKEY> pkey; if (!GetCertificate(ssl, &x509, &pkey)) { return false; } if (pkey) { TestState *test_state = GetTestState(ssl); - const TestConfig *config = GetConfigPtr(ssl); + const TestConfig *config = GetTestConfig(ssl); if (!SSL_use_PrivateKey(ssl, pkey.get())) { return false; } @@ -211,12 +213,12 @@ static bool InstallCertificate(SSL *ssl) { } static int ClientCertCallback(SSL *ssl, X509 **out_x509, EVP_PKEY **out_pkey) { - if (GetConfigPtr(ssl)->async && !GetTestState(ssl)->cert_ready) { + if (GetTestConfig(ssl)->async && !GetTestState(ssl)->cert_ready) { return -1; } - ScopedX509 x509; - ScopedEVP_PKEY pkey; + bssl::UniquePtr<X509> x509; + bssl::UniquePtr<EVP_PKEY> pkey; if (!GetCertificate(ssl, &x509, &pkey)) { return -1; } @@ -243,7 +245,7 @@ static int VerifyFail(X509_STORE_CTX *store_ctx, void *arg) { static int NextProtosAdvertisedCallback(SSL *ssl, const uint8_t **out, unsigned int *out_len, void *arg) { - const TestConfig *config = GetConfigPtr(ssl); + const TestConfig *config = GetTestConfig(ssl); if (config->advertise_npn.empty()) { return SSL_TLSEXT_ERR_NOACK; } @@ -255,7 +257,7 @@ static int NextProtosAdvertisedCallback(SSL *ssl, const uint8_t **out, static int NextProtoSelectCallback(SSL* ssl, uint8_t** out, uint8_t* outlen, const uint8_t* in, unsigned inlen, void* arg) { - const TestConfig *config = GetConfigPtr(ssl); + const TestConfig *config = GetTestConfig(ssl); if (config->select_next_proto.empty()) { return SSL_TLSEXT_ERR_NOACK; } @@ -267,8 +269,15 @@ static int NextProtoSelectCallback(SSL* ssl, uint8_t** out, uint8_t* outlen, static int AlpnSelectCallback(SSL* ssl, const uint8_t** out, uint8_t* outlen, const uint8_t* in, unsigned inlen, void* arg) { - const TestConfig *config = GetConfigPtr(ssl); - if (config->select_alpn.empty()) { + if (GetTestState(ssl)->alpn_select_done) { + fprintf(stderr, "AlpnSelectCallback called after completion.\n"); + exit(1); + } + + GetTestState(ssl)->alpn_select_done = true; + + const TestConfig *config = GetTestConfig(ssl); + if (config->decline_alpn) { return SSL_TLSEXT_ERR_NOACK; } @@ -289,9 +298,15 @@ static unsigned PskClientCallback(SSL *ssl, const char *hint, char *out_identity, unsigned max_identity_len, uint8_t *out_psk, unsigned max_psk_len) { - const TestConfig *config = GetConfigPtr(ssl); + const TestConfig *config = GetTestConfig(ssl); - if (strcmp(hint ? hint : "", config->psk_identity.c_str()) != 0) { + if (config->psk_identity.empty()) { + if (hint != nullptr) { + fprintf(stderr, "Server PSK hint was non-null.\n"); + return 0; + } + } else if (hint == nullptr || + strcmp(hint, config->psk_identity.c_str()) != 0) { fprintf(stderr, "Server PSK hint did not match.\n"); return 0; } @@ -311,7 +326,7 @@ static unsigned PskClientCallback(SSL *ssl, const char *hint, static unsigned PskServerCallback(SSL *ssl, const char *identity, uint8_t *out_psk, unsigned max_psk_len) { - const TestConfig *config = GetConfigPtr(ssl); + const TestConfig *config = GetTestConfig(ssl); if (strcmp(identity, config->psk_identity.c_str()) != 0) { fprintf(stderr, "Client PSK identity did not match.\n"); @@ -328,6 +343,31 @@ static unsigned PskServerCallback(SSL *ssl, const char *identity, } static int CertCallback(SSL *ssl, void *arg) { + const TestConfig *config = GetTestConfig(ssl); + + // Check the CertificateRequest metadata is as expected. + // + // TODO(davidben): Test |SSL_get_client_CA_list|. + if (!SSL_is_server(ssl) && + !config->expected_certificate_types.empty()) { + const uint8_t *certificate_types; + size_t certificate_types_len = + SSL_get0_certificate_types(ssl, &certificate_types); + if (certificate_types_len != config->expected_certificate_types.size() || + memcmp(certificate_types, + config->expected_certificate_types.data(), + certificate_types_len) != 0) { + fprintf(stderr, "certificate types mismatch\n"); + return 0; + } + } + + // The certificate will be installed via other means. + if (!config->async || config->use_early_callback || + config->use_old_client_cert_callback) { + return 1; + } + if (!GetTestState(ssl)->cert_ready) { return -1; } @@ -339,26 +379,38 @@ static int CertCallback(SSL *ssl, void *arg) { static void InfoCallback(const SSL *ssl, int type, int val) { if (type == SSL_CB_HANDSHAKE_DONE) { - if (GetConfigPtr(ssl)->handshake_never_done) { - fprintf(stderr, "handshake completed\n"); + if (GetTestConfig(ssl)->handshake_never_done) { + fprintf(stderr, "Handshake unexpectedly completed.\n"); // Abort before any expected error code is printed, to ensure the overall // test fails. abort(); } GetTestState(ssl)->handshake_done = true; + + // Callbacks may be called again on a new handshake. + GetTestState(ssl)->ticket_decrypt_done = false; + GetTestState(ssl)->alpn_select_done = false; } } static int NewSessionCallback(SSL *ssl, SSL_SESSION *session) { GetTestState(ssl)->got_new_session = true; - // BoringSSL passes a reference to |session|. - SSL_SESSION_free(session); + GetTestState(ssl)->new_session.reset(session); return 1; } static int TicketKeyCallback(SSL *ssl, uint8_t *key_name, uint8_t *iv, EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx, int encrypt) { + if (!encrypt) { + if (GetTestState(ssl)->ticket_decrypt_done) { + fprintf(stderr, "TicketKeyCallback called after completion.\n"); + return -1; + } + + GetTestState(ssl)->ticket_decrypt_done = true; + } + // This is just test code, so use the all-zeros key. static const uint8_t kZeros[16] = {0}; @@ -375,7 +427,7 @@ static int TicketKeyCallback(SSL *ssl, uint8_t *key_name, uint8_t *iv, } if (!encrypt) { - return GetConfigPtr(ssl)->renew_ticket ? 2 : 1; + return GetTestConfig(ssl)->renew_ticket ? 2 : 1; } return 1; } @@ -397,10 +449,10 @@ static int CustomExtensionAddCallback(SSL *ssl, unsigned extension_value, abort(); } - if (GetConfigPtr(ssl)->custom_extension_skip) { + if (GetTestConfig(ssl)->custom_extension_skip) { return 0; } - if (GetConfigPtr(ssl)->custom_extension_fail_add) { + if (GetTestConfig(ssl)->custom_extension_fail_add) { return -1; } @@ -495,14 +547,22 @@ class SocketCloser { const int sock_; }; -static ScopedSSL_CTX SetupCtx(const TestConfig *config) { - ScopedSSL_CTX ssl_ctx(SSL_CTX_new( +static bssl::UniquePtr<SSL_CTX> SetupCtx(const TestConfig *config) { + bssl::UniquePtr<SSL_CTX> ssl_ctx(SSL_CTX_new( config->is_dtls ? DTLS_method() : TLS_method())); if (!ssl_ctx) { return nullptr; } SSL_CTX_set_security_level(ssl_ctx.get(), 0); +#if 0 + /* Disabled for now until we have some TLS1.3 support */ + // Enable TLS 1.3 for tests. + if (!config->is_dtls && + !SSL_CTX_set_max_proto_version(ssl_ctx.get(), TLS1_3_VERSION)) { + return nullptr; + } +#endif std::string cipher_list = "ALL"; if (!config->cipher.empty()) { @@ -552,7 +612,7 @@ static ScopedSSL_CTX SetupCtx(const TestConfig *config) { tmpdh = DH_get_2048_256(); } - ScopedDH dh(tmpdh); + bssl::UniquePtr<DH> dh(tmpdh); if (!dh || !SSL_CTX_set_tmp_dh(ssl_ctx.get(), dh.get())) { return nullptr; @@ -571,7 +631,7 @@ static ScopedSSL_CTX SetupCtx(const TestConfig *config) { NULL); } - if (!config->select_alpn.empty()) { + if (!config->select_alpn.empty() || config->decline_alpn) { SSL_CTX_set_alpn_select_cb(ssl_ctx.get(), AlpnSelectCallback, NULL); } @@ -609,6 +669,10 @@ static ScopedSSL_CTX SetupCtx(const TestConfig *config) { return nullptr; } + if (config->use_null_client_ca_list) { + SSL_CTX_set_client_CA_list(ssl_ctx.get(), nullptr); + } + return ssl_ctx; } @@ -621,26 +685,16 @@ static bool RetryAsync(SSL *ssl, int ret) { return false; } - const TestConfig *config = GetConfigPtr(ssl); TestState *test_state = GetTestState(ssl); - if (test_state->clock_delta.tv_usec != 0 || - test_state->clock_delta.tv_sec != 0) { - // Process the timeout and retry. - test_state->clock.tv_usec += test_state->clock_delta.tv_usec; - test_state->clock.tv_sec += test_state->clock.tv_usec / 1000000; - test_state->clock.tv_usec %= 1000000; - test_state->clock.tv_sec += test_state->clock_delta.tv_sec; - memset(&test_state->clock_delta, 0, sizeof(test_state->clock_delta)); + assert(GetTestConfig(ssl)->async); + if (test_state->packeted_bio != nullptr && + PacketedBioAdvanceClock(test_state->packeted_bio)) { // The DTLS retransmit logic silently ignores write failures. So the test // may progress, allow writes through synchronously. - if (config->async) { - AsyncBioEnforceWriteQuota(test_state->async_bio, false); - } + AsyncBioEnforceWriteQuota(test_state->async_bio, false); int timeout_ret = DTLSv1_handle_timeout(ssl); - if (config->async) { - AsyncBioEnforceWriteQuota(test_state->async_bio, true); - } + AsyncBioEnforceWriteQuota(test_state->async_bio, true); if (timeout_ret < 0) { fprintf(stderr, "Error retransmitting.\n"); @@ -669,7 +723,7 @@ static bool RetryAsync(SSL *ssl, int ret) { // DoRead reads from |ssl|, resolving any asynchronous operations. It returns // the result value of the final |SSL_read| call. static int DoRead(SSL *ssl, uint8_t *out, size_t max_out) { - const TestConfig *config = GetConfigPtr(ssl); + const TestConfig *config = GetTestConfig(ssl); TestState *test_state = GetTestState(ssl); int ret; do { @@ -679,18 +733,40 @@ static int DoRead(SSL *ssl, uint8_t *out, size_t max_out) { // trigger a retransmit, so disconnect the write quota. AsyncBioEnforceWriteQuota(test_state->async_bio, false); } - ret = SSL_read(ssl, out, max_out); + ret = config->peek_then_read ? SSL_peek(ssl, out, max_out) + : SSL_read(ssl, out, max_out); if (config->async) { AsyncBioEnforceWriteQuota(test_state->async_bio, true); } } while (config->async && RetryAsync(ssl, ret)); + + if (config->peek_then_read && ret > 0) { + std::unique_ptr<uint8_t[]> buf(new uint8_t[static_cast<size_t>(ret)]); + + // SSL_peek should synchronously return the same data. + int ret2 = SSL_peek(ssl, buf.get(), ret); + if (ret2 != ret || + memcmp(buf.get(), out, ret) != 0) { + fprintf(stderr, "First and second SSL_peek did not match.\n"); + return -1; + } + + // SSL_read should synchronously return the same data and consume it. + ret2 = SSL_read(ssl, buf.get(), ret); + if (ret2 != ret || + memcmp(buf.get(), out, ret) != 0) { + fprintf(stderr, "SSL_peek and SSL_read did not match.\n"); + return -1; + } + } + return ret; } // WriteAll writes |in_len| bytes from |in| to |ssl|, resolving any asynchronous // operations. It returns the result of the final |SSL_write| call. static int WriteAll(SSL *ssl, const uint8_t *in, size_t in_len) { - const TestConfig *config = GetConfigPtr(ssl); + const TestConfig *config = GetTestConfig(ssl); int ret; do { ret = SSL_write(ssl, in, in_len); @@ -705,7 +781,7 @@ static int WriteAll(SSL *ssl, const uint8_t *in, size_t in_len) { // DoShutdown calls |SSL_shutdown|, resolving any asynchronous operations. It // returns the result of the final |SSL_shutdown| call. static int DoShutdown(SSL *ssl) { - const TestConfig *config = GetConfigPtr(ssl); + const TestConfig *config = GetTestConfig(ssl); int ret; do { ret = SSL_shutdown(ssl); @@ -713,11 +789,19 @@ static int DoShutdown(SSL *ssl) { return ret; } +static uint16_t GetProtocolVersion(const SSL *ssl) { + uint16_t version = SSL_version(ssl); + if (!SSL_is_dtls(ssl)) { + return version; + } + return 0x0201 + ~version; +} + // CheckHandshakeProperties checks, immediately after |ssl| completes its // initial handshake (or False Starts), whether all the properties are // consistent with the test configuration and invariants. static bool CheckHandshakeProperties(SSL *ssl, bool is_resume) { - const TestConfig *config = GetConfigPtr(ssl); + const TestConfig *config = GetTestConfig(ssl); if (SSL_get_current_cipher(ssl) == nullptr) { fprintf(stderr, "null cipher after handshake\n"); @@ -741,7 +825,9 @@ static bool CheckHandshakeProperties(SSL *ssl, bool is_resume) { if (expect_handshake_done && !config->is_server) { bool expect_new_session = !config->expect_no_session && - (!SSL_session_reused(ssl) || config->expect_ticket_renewal); + (!SSL_session_reused(ssl) || config->expect_ticket_renewal) && + // Session tickets are sent post-handshake in TLS 1.3. + GetProtocolVersion(ssl) < TLS1_3_VERSION; if (expect_new_session != GetTestState(ssl)->got_new_session) { fprintf(stderr, "new session was%s cached, but we expected the opposite\n", @@ -760,19 +846,6 @@ static bool CheckHandshakeProperties(SSL *ssl, bool is_resume) { } } - if (!config->expected_certificate_types.empty()) { - const uint8_t *certificate_types; - size_t certificate_types_len = - SSL_get0_certificate_types(ssl, &certificate_types); - if (certificate_types_len != config->expected_certificate_types.size() || - memcmp(certificate_types, - config->expected_certificate_types.data(), - certificate_types_len) != 0) { - fprintf(stderr, "certificate types mismatch\n"); - return false; - } - } - if (!config->expected_next_proto.empty()) { const uint8_t *next_proto; unsigned next_proto_len; @@ -797,6 +870,13 @@ static bool CheckHandshakePro |