From bca5875eb3c25348ec07758cde66ebec27031ce4 Mon Sep 17 00:00:00 2001 From: David Benjamin Date: Sun, 4 Feb 2024 10:24:18 -0500 Subject: [PATCH 1/4] Use a more fine-grained lock in by_dir.c Different BY_DIR_ENTRYs don't need to share a lock. Also switch some code to use OPENSSL_strndup. Change-Id: I3809e001afb9577bb96aab214e80e173900356fe Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/66012 Reviewed-by: Bob Beck Commit-Queue: David Benjamin --- crypto/x509/by_dir.c | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/crypto/x509/by_dir.c b/crypto/x509/by_dir.c index 5eca97bc7..d49220d88 100644 --- a/crypto/x509/by_dir.c +++ b/crypto/x509/by_dir.c @@ -72,6 +72,7 @@ typedef struct lookup_dir_hashes_st { } BY_DIR_HASH; typedef struct lookup_dir_entry_st { + CRYPTO_MUTEX lock; char *dir; int dir_type; STACK_OF(BY_DIR_HASH) *hashes; @@ -156,6 +157,7 @@ static int by_dir_hash_cmp(const BY_DIR_HASH *const *a, static void by_dir_entry_free(BY_DIR_ENTRY *ent) { if (ent != NULL) { + CRYPTO_MUTEX_cleanup(&ent->lock); OPENSSL_free(ent->dir); sk_BY_DIR_HASH_pop_free(ent->hashes, by_dir_hash_free); OPENSSL_free(ent); @@ -215,15 +217,12 @@ static int add_cert_dir(BY_DIR *ctx, const char *dir, int type) { if (!ent) { return 0; } + CRYPTO_MUTEX_init(&ent->lock); ent->dir_type = type; ent->hashes = sk_BY_DIR_HASH_new(by_dir_hash_cmp); - ent->dir = OPENSSL_malloc(len + 1); - if (!ent->dir || !ent->hashes) { - by_dir_entry_free(ent); - return 0; - } - OPENSSL_strlcpy(ent->dir, ss, len + 1); - if (!sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) { + ent->dir = OPENSSL_strndup(ss, len); + if (ent->dir == NULL || ent->hashes == NULL || + !sk_BY_DIR_ENTRY_push(ctx->dirs, ent)) { by_dir_entry_free(ent); return 0; } @@ -232,10 +231,6 @@ static int add_cert_dir(BY_DIR *ctx, const char *dir, int type) { return 1; } -// g_ent_hashes_lock protects the |hashes| member of all |BY_DIR_ENTRY| -// objects. -static CRYPTO_MUTEX g_ent_hashes_lock = CRYPTO_MUTEX_INIT; - static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name, X509_OBJECT *ret) { union { @@ -300,7 +295,7 @@ static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name, } if (type == X509_LU_CRL && ent->hashes) { htmp.hash = h; - CRYPTO_MUTEX_lock_read(&g_ent_hashes_lock); + CRYPTO_MUTEX_lock_read(&ent->lock); if (sk_BY_DIR_HASH_find(ent->hashes, &idx, &htmp)) { hent = sk_BY_DIR_HASH_value(ent->hashes, idx); k = hent->suffix; @@ -308,7 +303,7 @@ static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name, hent = NULL; k = 0; } - CRYPTO_MUTEX_unlock_read(&g_ent_hashes_lock); + CRYPTO_MUTEX_unlock_read(&ent->lock); } else { k = 0; hent = NULL; @@ -341,7 +336,7 @@ static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name, // If a CRL, update the last file suffix added for this if (type == X509_LU_CRL) { - CRYPTO_MUTEX_lock_write(&g_ent_hashes_lock); + CRYPTO_MUTEX_lock_write(&ent->lock); // Look for entry again in case another thread added an entry // first. if (!hent) { @@ -354,14 +349,14 @@ static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name, if (!hent) { hent = OPENSSL_malloc(sizeof(BY_DIR_HASH)); if (hent == NULL) { - CRYPTO_MUTEX_unlock_write(&g_ent_hashes_lock); + CRYPTO_MUTEX_unlock_write(&ent->lock); ok = 0; goto finish; } hent->hash = h; hent->suffix = k; if (!sk_BY_DIR_HASH_push(ent->hashes, hent)) { - CRYPTO_MUTEX_unlock_write(&g_ent_hashes_lock); + CRYPTO_MUTEX_unlock_write(&ent->lock); OPENSSL_free(hent); ok = 0; goto finish; @@ -371,7 +366,7 @@ static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name, hent->suffix = k; } - CRYPTO_MUTEX_unlock_write(&g_ent_hashes_lock); + CRYPTO_MUTEX_unlock_write(&ent->lock); } if (tmp != NULL) { From 5d880140674cd36d84331de7df95c2dc00dc7686 Mon Sep 17 00:00:00 2001 From: David Benjamin Date: Tue, 20 Feb 2024 17:34:14 -0500 Subject: [PATCH 2/4] Deprecate and simplify SSL_CTX_check_private_key It is not actually possible to configure an inconsistent certificate and private key pair (short of mutating the objects after you've configured them). The functions that configure certificates and private keys will refuse to get CERT into an inconsistent state. SSL_CTX_check_private_key is really just checking that you have a certificate and private key at all. Some callers (notably pyOpenSSL's tests) are written as if SSL_CTX_check_private_key does something more, but that's only because they also configure certificate and private key in the wrong order. If you configure the key first, configuring the certificate silently drops the mismatched private key because OpenSSL thinks you're overwriting an identity. SSL_CTX_check_private_key is really just detecting this case. Add tests for all this behavior, document that certificates should be configured first, and then deprecate SSL_CTX_check_private_key because, in the correct order, this function is superfluous. This will get shuffled around with SSL_CREDENTIAL, so add some tests first. Bug: 249 Change-Id: I3fcc0f51add1826d581583b43ff003c0dea979dd Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/66447 Commit-Queue: David Benjamin Reviewed-by: Bob Beck --- include/openssl/ssl.h | 38 ++++++++++++++++------ ssl/internal.h | 1 - ssl/ssl_cert.cc | 24 -------------- ssl/ssl_lib.cc | 27 ++++++++++++--- ssl/ssl_privkey.cc | 20 +++++++++--- ssl/ssl_test.cc | 76 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 142 insertions(+), 44 deletions(-) diff --git a/include/openssl/ssl.h b/include/openssl/ssl.h index 97fe96e20..04f187362 100644 --- a/include/openssl/ssl.h +++ b/include/openssl/ssl.h @@ -848,15 +848,22 @@ OPENSSL_EXPORT void SSL_CTX_set0_buffer_pool(SSL_CTX *ctx, // the wire) but does not include the leaf. Both client and server certificates // use these functions. // +// Prefer to configure the certificate before the private key. If configured in +// the other order, inconsistent private keys will be silently dropped, rather +// than return an error. Additionally, overwriting a previously-configured +// certificate and key pair only works if the certificate is configured first. +// // Certificates and keys may be configured before the handshake or dynamically // in the early callback and certificate callback. // SSL_CTX_use_certificate sets |ctx|'s leaf certificate to |x509|. It returns -// one on success and zero on failure. +// one on success and zero on failure. If |ctx| has a private key which is +// inconsistent with |x509|, the private key is silently dropped. OPENSSL_EXPORT int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x509); // SSL_use_certificate sets |ssl|'s leaf certificate to |x509|. It returns one -// on success and zero on failure. +// on success and zero on failure. If |ssl| has a private key which is +// inconsistent with |x509|, the private key is silently dropped. OPENSSL_EXPORT int SSL_use_certificate(SSL *ssl, X509 *x509); // SSL_CTX_use_PrivateKey sets |ctx|'s private key to |pkey|. It returns one on @@ -990,14 +997,6 @@ SSL_get0_peer_delegation_algorithms(const SSL *ssl, // chain of |ssl|. OPENSSL_EXPORT void SSL_certs_clear(SSL *ssl); -// SSL_CTX_check_private_key returns one if the certificate and private key -// configured in |ctx| are consistent and zero otherwise. -OPENSSL_EXPORT int SSL_CTX_check_private_key(const SSL_CTX *ctx); - -// SSL_check_private_key returns one if the certificate and private key -// configured in |ssl| are consistent and zero otherwise. -OPENSSL_EXPORT int SSL_check_private_key(const SSL *ssl); - // SSL_CTX_get0_certificate returns |ctx|'s leaf certificate. OPENSSL_EXPORT X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx); @@ -5317,6 +5316,25 @@ OPENSSL_EXPORT int SSL_set1_curves_list(SSL *ssl, const char *curves); // returns this value, but we define this constant for compatibility. #define TLSEXT_nid_unknown 0x1000000 +// SSL_CTX_check_private_key returns one if |ctx| has both a certificate and +// private key, and zero otherwise. +// +// This function does not check consistency because the library checks when the +// certificate and key are individually configured. However, if the private key +// is configured before the certificate, inconsistent private keys are silently +// dropped. Some callers are inadvertently relying on this function to detect +// when this happens. +// +// Instead, callers should configure the certificate first, then the private +// key, checking for errors in each. This function is then unnecessary. +OPENSSL_EXPORT int SSL_CTX_check_private_key(const SSL_CTX *ctx); + +// SSL_check_private_key returns one if |ssl| has both a certificate and private +// key, and zero otherwise. +// +// See discussion in |SSL_CTX_check_private_key|. +OPENSSL_EXPORT int SSL_check_private_key(const SSL *ssl); + // Compliance policy configurations // diff --git a/ssl/internal.h b/ssl/internal.h index 13b97daa4..9e7a05b1c 100644 --- a/ssl/internal.h +++ b/ssl/internal.h @@ -3202,7 +3202,6 @@ bool ssl_is_key_type_supported(int key_type); // message on the error queue. bool ssl_compare_public_and_private_key(const EVP_PKEY *pubkey, const EVP_PKEY *privkey); -bool ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey); bool ssl_get_new_session(SSL_HANDSHAKE *hs); bool ssl_encrypt_ticket(SSL_HANDSHAKE *hs, CBB *out, const SSL_SESSION *session); diff --git a/ssl/ssl_cert.cc b/ssl/ssl_cert.cc index d635fb35b..dbc4818eb 100644 --- a/ssl/ssl_cert.cc +++ b/ssl/ssl_cert.cc @@ -493,30 +493,6 @@ bool ssl_compare_public_and_private_key(const EVP_PKEY *pubkey, return false; } -bool ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey) { - if (privkey == nullptr) { - OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED); - return false; - } - - if (cert->chain == nullptr || - sk_CRYPTO_BUFFER_value(cert->chain.get(), 0) == nullptr) { - OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED); - return false; - } - - CBS cert_cbs; - CRYPTO_BUFFER_init_CBS(sk_CRYPTO_BUFFER_value(cert->chain.get(), 0), - &cert_cbs); - UniquePtr pubkey = ssl_cert_parse_pubkey(&cert_cbs); - if (!pubkey) { - OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE); - return false; - } - - return ssl_compare_public_and_private_key(pubkey.get(), privkey); -} - bool ssl_cert_check_key_usage(const CBS *in, enum ssl_key_usage_t bit) { CBS buf = *in; diff --git a/ssl/ssl_lib.cc b/ssl/ssl_lib.cc index 81a980713..f95cd5db2 100644 --- a/ssl/ssl_lib.cc +++ b/ssl/ssl_lib.cc @@ -1724,17 +1724,36 @@ int SSL_has_pending(const SSL *ssl) { return SSL_pending(ssl) != 0 || !ssl->s3->read_buffer.empty(); } +static bool has_cert_and_key(const CERT *cert) { + // TODO(davidben): If |cert->key_method| is set, that should be fine too. + if (cert->privatekey == nullptr) { + OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED); + return false; + } + + if (cert->chain == nullptr || + sk_CRYPTO_BUFFER_value(cert->chain.get(), 0) == nullptr) { + OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED); + return false; + } + + return true; +} + int SSL_CTX_check_private_key(const SSL_CTX *ctx) { - return ssl_cert_check_private_key(ctx->cert.get(), - ctx->cert->privatekey.get()); + // There is no need to actually check consistency because inconsistent values + // can never be configured. + return has_cert_and_key(ctx->cert.get()); } int SSL_check_private_key(const SSL *ssl) { if (!ssl->config) { return 0; } - return ssl_cert_check_private_key(ssl->config->cert.get(), - ssl->config->cert->privatekey.get()); + + // There is no need to actually check consistency because inconsistent values + // can never be configured. + return has_cert_and_key(ssl->config->cert.get()); } long SSL_get_default_timeout(const SSL *ssl) { diff --git a/ssl/ssl_privkey.cc b/ssl/ssl_privkey.cc index 57116cd6c..b3bb2c802 100644 --- a/ssl/ssl_privkey.cc +++ b/ssl/ssl_privkey.cc @@ -83,11 +83,21 @@ static bool ssl_set_pkey(CERT *cert, EVP_PKEY *pkey) { return false; } - if (cert->chain != nullptr && - sk_CRYPTO_BUFFER_value(cert->chain.get(), 0) != nullptr && - // Sanity-check that the private key and the certificate match. - !ssl_cert_check_private_key(cert, pkey)) { - return false; + // If the leaf certificate has been configured, check it matches. + const CRYPTO_BUFFER *leaf = cert->chain != nullptr + ? sk_CRYPTO_BUFFER_value(cert->chain.get(), 0) + : nullptr; + if (leaf != nullptr) { + CBS cert_cbs; + CRYPTO_BUFFER_init_CBS(leaf, &cert_cbs); + UniquePtr pubkey = ssl_cert_parse_pubkey(&cert_cbs); + if (!pubkey) { + OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE); + return false; + } + if (!ssl_compare_public_and_private_key(pubkey.get(), pkey)) { + return false; + } } cert->privatekey = UpRef(pkey); diff --git a/ssl/ssl_test.cc b/ssl/ssl_test.cc index 0b4ad3ccd..d12d49c78 100644 --- a/ssl/ssl_test.cc +++ b/ssl/ssl_test.cc @@ -4556,6 +4556,82 @@ TEST(SSLTest, SetChainAndKeyMismatch) { ERR_clear_error(); } +TEST(SSLTest, CertThenKeyMismatch) { + bssl::UniquePtr ctx(SSL_CTX_new(TLS_method())); + ASSERT_TRUE(ctx); + + bssl::UniquePtr key = GetTestKey(); + ASSERT_TRUE(key); + bssl::UniquePtr leaf = GetChainTestCertificate(); + ASSERT_TRUE(leaf); + + // There is no key or certificate, so |SSL_CTX_check_private_key| fails. + EXPECT_FALSE(SSL_CTX_check_private_key(ctx.get())); + + // With only a certificate, |SSL_CTX_check_private_key| still fails. + ASSERT_TRUE(SSL_CTX_use_certificate(ctx.get(), leaf.get())); + EXPECT_FALSE(SSL_CTX_check_private_key(ctx.get())); + + // The private key does not match the certificate, so it should fail. + EXPECT_FALSE(SSL_CTX_use_PrivateKey(ctx.get(), key.get())); + + // Checking the private key fails, but this is really because there is still + // no private key. + EXPECT_FALSE(SSL_CTX_check_private_key(ctx.get())); + EXPECT_EQ(nullptr, SSL_CTX_get0_privatekey(ctx.get())); +} + +TEST(SSLTest, KeyThenCertMismatch) { + bssl::UniquePtr ctx(SSL_CTX_new(TLS_method())); + ASSERT_TRUE(ctx); + + bssl::UniquePtr key = GetTestKey(); + ASSERT_TRUE(key); + bssl::UniquePtr leaf = GetChainTestCertificate(); + ASSERT_TRUE(leaf); + + // There is no key or certificate, so |SSL_CTX_check_private_key| fails. + EXPECT_FALSE(SSL_CTX_check_private_key(ctx.get())); + + // With only a key, |SSL_CTX_check_private_key| still fails. + ASSERT_TRUE(SSL_CTX_use_PrivateKey(ctx.get(), key.get())); + EXPECT_FALSE(SSL_CTX_check_private_key(ctx.get())); + + // If configuring a certificate that doesn't match the key, configuration + // actually succeeds. We just silently drop the private key. + ASSERT_TRUE(SSL_CTX_use_certificate(ctx.get(), leaf.get())); + EXPECT_EQ(nullptr, SSL_CTX_get0_privatekey(ctx.get())); + + // Some callers configure the private key, then the certificate, and then + // expect |SSL_CTX_check_private_key| to check consistency. It does, but only + // by way of noticing there is no private key. The actual consistency check + // happened in |SSL_CTX_use_certificate|. + EXPECT_FALSE(SSL_CTX_check_private_key(ctx.get())); +} + +TEST(SSLTest, OverrideCertAndKey) { + // It is possible to override an existing certificate by configuring + // certificate, then key, due to |SSL_CTX_use_certificate|'s above silent + // dropping behavior. + bssl::UniquePtr ctx(SSL_CTX_new(TLS_method())); + ASSERT_TRUE(ctx); + + bssl::UniquePtr key = GetTestKey(); + ASSERT_TRUE(key); + bssl::UniquePtr leaf = GetTestCertificate(); + ASSERT_TRUE(leaf); + bssl::UniquePtr key2 = GetChainTestKey(); + ASSERT_TRUE(key2); + bssl::UniquePtr leaf2 = GetChainTestCertificate(); + ASSERT_TRUE(leaf2); + + ASSERT_TRUE(SSL_CTX_use_certificate(ctx.get(), leaf.get())); + ASSERT_TRUE(SSL_CTX_use_PrivateKey(ctx.get(), key.get())); + + ASSERT_TRUE(SSL_CTX_use_certificate(ctx.get(), leaf2.get())); + ASSERT_TRUE(SSL_CTX_use_PrivateKey(ctx.get(), key2.get())); +} + TEST(SSLTest, SetChainAndKeyCtx) { bssl::UniquePtr client_ctx(SSL_CTX_new(TLS_with_buffers_method())); ASSERT_TRUE(client_ctx); From 85c5d9668fe3aef03dc3b5099adc549039a79ff9 Mon Sep 17 00:00:00 2001 From: Bob Beck Date: Wed, 21 Feb 2024 22:07:58 +0000 Subject: [PATCH 3/4] Make an include/openssl/experimental. Move kyber to it for now. Update-Note: has moved to Change-Id: I51d37aeb2b6cfbbaae494cc38f1b0a82669d2692 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/66467 Reviewed-by: David Benjamin Auto-Submit: Bob Beck Commit-Queue: Bob Beck --- API-CONVENTIONS.md | 3 +++ crypto/kyber/internal.h | 2 +- crypto/kyber/kyber.c | 2 +- crypto/kyber/kyber_test.cc | 2 +- include/openssl/{ => experimental}/kyber.h | 0 ssl/ssl_key_share.cc | 2 +- tool/speed.cc | 2 +- util/doc.config | 6 +++++- 8 files changed, 13 insertions(+), 6 deletions(-) rename include/openssl/{ => experimental}/kyber.h (100%) diff --git a/API-CONVENTIONS.md b/API-CONVENTIONS.md index ec08e57a2..e36037bb2 100644 --- a/API-CONVENTIONS.md +++ b/API-CONVENTIONS.md @@ -16,6 +16,9 @@ OpenSSL's legacy ASN.1, X.509, and PEM implementation. If possible, avoid using them. These are left largely unmodified from upstream and are retained only for compatibility with existing OpenSSL consumers. +Experimental public APIs are found in `include/openssl/experimental`. Use of +these will likely be incompatible with changes in the near future as they are +finalized. ## Forward declarations diff --git a/crypto/kyber/internal.h b/crypto/kyber/internal.h index 7f4a08c2e..007226904 100644 --- a/crypto/kyber/internal.h +++ b/crypto/kyber/internal.h @@ -16,7 +16,7 @@ #define OPENSSL_HEADER_CRYPTO_KYBER_INTERNAL_H #include -#include +#include #if defined(__cplusplus) extern "C" { diff --git a/crypto/kyber/kyber.c b/crypto/kyber/kyber.c index e9e3a9198..0e973590e 100644 --- a/crypto/kyber/kyber.c +++ b/crypto/kyber/kyber.c @@ -12,7 +12,7 @@ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include +#include #include #include diff --git a/crypto/kyber/kyber_test.cc b/crypto/kyber/kyber_test.cc index c42db787c..99564788b 100644 --- a/crypto/kyber/kyber_test.cc +++ b/crypto/kyber/kyber_test.cc @@ -20,7 +20,7 @@ #include #include -#include +#include #include "../test/file_test.h" #include "../test/test_util.h" diff --git a/include/openssl/kyber.h b/include/openssl/experimental/kyber.h similarity index 100% rename from include/openssl/kyber.h rename to include/openssl/experimental/kyber.h diff --git a/ssl/ssl_key_share.cc b/ssl/ssl_key_share.cc index 80317d898..f4fda3fda 100644 --- a/ssl/ssl_key_share.cc +++ b/ssl/ssl_key_share.cc @@ -24,7 +24,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/tool/speed.cc b/tool/speed.cc index 547390946..a8c058731 100644 --- a/tool/speed.cc +++ b/tool/speed.cc @@ -38,8 +38,8 @@ #include #include #include +#include #include -#include #include #include #include diff --git a/util/doc.config b/util/doc.config index e36a9601d..2e71c3c65 100644 --- a/util/doc.config +++ b/util/doc.config @@ -36,7 +36,6 @@ "include/openssl/engine.h", "include/openssl/hkdf.h", "include/openssl/hmac.h", - "include/openssl/kyber.h", "include/openssl/md5.h", "include/openssl/rc4.h", "include/openssl/rsa.h", @@ -60,6 +59,11 @@ "include/openssl/conf.h", "include/openssl/x509.h" ] + },{ + "Name": "Experimental primitives. Will be removed and replaced when standardized!", + "Headers": [ + "include/openssl/experimental/kyber.h" + ] },{ "Name": "SSL implementation", "Headers": [ From ec2a08dda8e7b156253abcf21f2c7ea80de39f82 Mon Sep 17 00:00:00 2001 From: David Benjamin Date: Wed, 21 Feb 2024 23:21:16 -0500 Subject: [PATCH 4/4] Introduce a test helper for asserting on the error This produces slightly nicer output, is less code, and helps us remember to check both the library and reason code. Change-Id: Ic49508accb0bc8a25cbb5b94cc7e4aeb1bd8cbd0 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/66507 Reviewed-by: Bob Beck Commit-Queue: David Benjamin --- crypto/bio/bio_test.cc | 5 +--- crypto/cipher_extra/aead_test.cc | 14 ++++----- crypto/dsa/dsa_test.cc | 5 ++-- crypto/evp/evp_extra_test.cc | 28 ++++++++--------- crypto/evp/scrypt_test.cc | 5 ++-- crypto/fipsmodule/bn/bn_test.cc | 4 +-- .../service_indicator_test.cc | 3 +- crypto/hpke/hpke_test.cc | 15 ++++------ crypto/pem/pem_test.cc | 7 +++-- crypto/rsa_extra/rsa_test.cc | 21 ++++--------- crypto/test/test_util.cc | 15 ++++++++++ crypto/test/test_util.h | 6 ++++ crypto/x509/x509_test.cc | 30 ++++++++----------- ssl/ssl_test.cc | 30 ++++++++----------- 14 files changed, 88 insertions(+), 100 deletions(-) diff --git a/crypto/bio/bio_test.cc b/crypto/bio/bio_test.cc index 65324d002..d44c9dddf 100644 --- a/crypto/bio/bio_test.cc +++ b/crypto/bio/bio_test.cc @@ -813,10 +813,7 @@ TEST_P(BIOPairTest, TestPair) { // A closed write end may not be written to. EXPECT_EQ(0u, BIO_ctrl_get_write_guarantee(bio1)); EXPECT_EQ(-1, BIO_write(bio1, "_____", 5)); - - uint32_t err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_BIO, ERR_GET_LIB(err)); - EXPECT_EQ(BIO_R_BROKEN_PIPE, ERR_GET_REASON(err)); + EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_BIO, BIO_R_BROKEN_PIPE)); // The other end is still functional. EXPECT_EQ(5, BIO_write(bio2, "12345", 5)); diff --git a/crypto/cipher_extra/aead_test.cc b/crypto/cipher_extra/aead_test.cc index 64c65e9eb..e16f5a24b 100644 --- a/crypto/cipher_extra/aead_test.cc +++ b/crypto/cipher_extra/aead_test.cc @@ -381,7 +381,7 @@ TEST_P(PerAEADTest, TestVectorScatterGather) { // Skip decryption for AEADs that don't implement open_gather(). if (!ret) { - int err = ERR_peek_error(); + uint32_t err = ERR_peek_error(); if (ERR_GET_LIB(err) == ERR_LIB_CIPHER && ERR_GET_REASON(err) == CIPHER_R_CTRL_NOT_IMPLEMENTED) { t->SkipCurrent(); @@ -709,10 +709,10 @@ TEST_P(PerAEADTest, InvalidNonceLength) { nonce.data(), nonce.size(), nullptr /* in */, 0, kZeros /* ad */, ad_len)); uint32_t err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_CIPHER, ERR_GET_LIB(err)); // TODO(davidben): Merge these errors. https://crbug.com/boringssl/129. - if (ERR_GET_REASON(err) != CIPHER_R_UNSUPPORTED_NONCE_SIZE) { - EXPECT_EQ(CIPHER_R_INVALID_NONCE_SIZE, ERR_GET_REASON(err)); + if (!ErrorEquals(err, ERR_LIB_CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE)) { + EXPECT_TRUE( + ErrorEquals(err, ERR_LIB_CIPHER, CIPHER_R_INVALID_NONCE_SIZE)); } ctx.Reset(); @@ -723,9 +723,9 @@ TEST_P(PerAEADTest, InvalidNonceLength) { nonce.data(), nonce.size(), kZeros /* in */, sizeof(kZeros), kZeros /* ad */, ad_len)); err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_CIPHER, ERR_GET_LIB(err)); - if (ERR_GET_REASON(err) != CIPHER_R_UNSUPPORTED_NONCE_SIZE) { - EXPECT_EQ(CIPHER_R_INVALID_NONCE_SIZE, ERR_GET_REASON(err)); + if (!ErrorEquals(err, ERR_LIB_CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE)) { + EXPECT_TRUE( + ErrorEquals(err, ERR_LIB_CIPHER, CIPHER_R_INVALID_NONCE_SIZE)); } } } diff --git a/crypto/dsa/dsa_test.cc b/crypto/dsa/dsa_test.cc index 22e9e13bb..3b83e1847 100644 --- a/crypto/dsa/dsa_test.cc +++ b/crypto/dsa/dsa_test.cc @@ -267,9 +267,8 @@ TEST(DSATest, InvalidGroup) { static const uint8_t kDigest[32] = {0}; EXPECT_FALSE( DSA_sign(0, kDigest, sizeof(kDigest), sig.data(), &sig_len, dsa.get())); - uint32_t err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_DSA, ERR_GET_LIB(err)); - EXPECT_EQ(DSA_R_INVALID_PARAMETERS, ERR_GET_REASON(err)); + EXPECT_TRUE( + ErrorEquals(ERR_get_error(), ERR_LIB_DSA, DSA_R_INVALID_PARAMETERS)); } // Signing and verifying should cleanly fail when the DSA object is empty. diff --git a/crypto/evp/evp_extra_test.cc b/crypto/evp/evp_extra_test.cc index 948bfb5ba..64683861a 100644 --- a/crypto/evp/evp_extra_test.cc +++ b/crypto/evp/evp_extra_test.cc @@ -812,7 +812,8 @@ TEST(EVPExtraTest, MarshalEmptyPublicKey) { bssl::ScopedCBB cbb; EXPECT_FALSE(EVP_marshal_public_key(cbb.get(), empty.get())) << "Marshalled empty public key."; - EXPECT_EQ(EVP_R_UNSUPPORTED_ALGORITHM, ERR_GET_REASON(ERR_peek_last_error())); + EXPECT_TRUE(ErrorEquals(ERR_peek_last_error(), ERR_LIB_EVP, + EVP_R_UNSUPPORTED_ALGORITHM)); } TEST(EVPExtraTest, d2i_PrivateKey) { @@ -890,16 +891,14 @@ TEST(EVPExtraTest, Ed25519) { // Passing too small of a buffer is noticed. len = 31; EXPECT_FALSE(EVP_PKEY_get_raw_public_key(pubkey.get(), buf, &len)); - uint32_t err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_EVP, ERR_GET_LIB(err)); - EXPECT_EQ(EVP_R_BUFFER_TOO_SMALL, ERR_GET_REASON(err)); + EXPECT_TRUE( + ErrorEquals(ERR_get_error(), ERR_LIB_EVP, EVP_R_BUFFER_TOO_SMALL)); ERR_clear_error(); // There is no private key. EXPECT_FALSE(EVP_PKEY_get_raw_private_key(pubkey.get(), nullptr, &len)); - err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_EVP, ERR_GET_LIB(err)); - EXPECT_EQ(EVP_R_NOT_A_PRIVATE_KEY, ERR_GET_REASON(err)); + EXPECT_TRUE( + ErrorEquals(ERR_get_error(), ERR_LIB_EVP, EVP_R_NOT_A_PRIVATE_KEY)); ERR_clear_error(); // The public key must encode properly. @@ -915,9 +914,8 @@ TEST(EVPExtraTest, Ed25519) { // The public key must gracefully fail to encode as a private key. ASSERT_TRUE(CBB_init(cbb.get(), 0)); EXPECT_FALSE(EVP_marshal_private_key(cbb.get(), pubkey.get())); - err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_EVP, ERR_GET_LIB(err)); - EXPECT_EQ(EVP_R_NOT_A_PRIVATE_KEY, ERR_GET_REASON(err)); + EXPECT_TRUE( + ErrorEquals(ERR_get_error(), ERR_LIB_EVP, EVP_R_NOT_A_PRIVATE_KEY)); ERR_clear_error(); cbb.Reset(); @@ -940,9 +938,8 @@ TEST(EVPExtraTest, Ed25519) { // Passing too small of a buffer is noticed. len = 31; EXPECT_FALSE(EVP_PKEY_get_raw_private_key(privkey.get(), buf, &len)); - err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_EVP, ERR_GET_LIB(err)); - EXPECT_EQ(EVP_R_BUFFER_TOO_SMALL, ERR_GET_REASON(err)); + EXPECT_TRUE( + ErrorEquals(ERR_get_error(), ERR_LIB_EVP, EVP_R_BUFFER_TOO_SMALL)); ERR_clear_error(); // The public key must be extractable. len = 32; @@ -995,9 +992,8 @@ TEST(EVPExtraTest, Ed25519) { EVP_DigestSignInit(ctx.get(), nullptr, nullptr, nullptr, privkey.get())); len = 31; EXPECT_FALSE(EVP_DigestSign(ctx.get(), buf, &len, nullptr /* msg */, 0)); - err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_EVP, ERR_GET_LIB(err)); - EXPECT_EQ(EVP_R_BUFFER_TOO_SMALL, ERR_GET_REASON(err)); + EXPECT_TRUE( + ErrorEquals(ERR_get_error(), ERR_LIB_EVP, EVP_R_BUFFER_TOO_SMALL)); ERR_clear_error(); } diff --git a/crypto/evp/scrypt_test.cc b/crypto/evp/scrypt_test.cc index e957fd0be..5a8838677 100644 --- a/crypto/evp/scrypt_test.cc +++ b/crypto/evp/scrypt_test.cc @@ -69,9 +69,8 @@ TEST(ScryptTest, MemoryLimit) { reinterpret_cast(kSalt), strlen(kSalt), 1048576 /* N */, 8 /* r */, 1 /* p */, 0 /* max_mem */, key, sizeof(key))); - uint32_t err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_EVP, ERR_GET_LIB(err)); - EXPECT_EQ(EVP_R_MEMORY_LIMIT_EXCEEDED, ERR_GET_REASON(err)); + EXPECT_TRUE( + ErrorEquals(ERR_get_error(), ERR_LIB_EVP, EVP_R_MEMORY_LIMIT_EXCEEDED)); } TEST(ScryptTest, InvalidParameters) { diff --git a/crypto/fipsmodule/bn/bn_test.cc b/crypto/fipsmodule/bn/bn_test.cc index d62f6e4dd..fcc59e00f 100644 --- a/crypto/fipsmodule/bn/bn_test.cc +++ b/crypto/fipsmodule/bn/bn_test.cc @@ -884,9 +884,7 @@ static void TestNotModSquare(BIGNUMFileTest *t, BN_CTX *ctx) { EXPECT_FALSE(BN_mod_sqrt(ret.get(), not_mod_square.get(), p.get(), ctx)) << "BN_mod_sqrt unexpectedly succeeded."; - uint32_t err = ERR_peek_error(); - EXPECT_EQ(ERR_LIB_BN, ERR_GET_LIB(err)); - EXPECT_EQ(BN_R_NOT_A_SQUARE, ERR_GET_REASON(err)); + EXPECT_TRUE(ErrorEquals(ERR_peek_error(), ERR_LIB_BN, BN_R_NOT_A_SQUARE)); ERR_clear_error(); } diff --git a/crypto/fipsmodule/service_indicator/service_indicator_test.cc b/crypto/fipsmodule/service_indicator/service_indicator_test.cc index ee66fc3f7..9eac62e4a 100644 --- a/crypto/fipsmodule/service_indicator/service_indicator_test.cc +++ b/crypto/fipsmodule/service_indicator/service_indicator_test.cc @@ -657,7 +657,8 @@ TEST_P(AEADServiceIndicatorTest, EVP_AEAD) { encrypt_output.size(), nonce.data(), nonce.size(), kPlaintext, sizeof(kPlaintext), nullptr, 0))); EXPECT_EQ(approved, FIPSStatus::NOT_APPROVED); - EXPECT_EQ(ERR_GET_REASON(ERR_get_error()), CIPHER_R_INVALID_NONCE); + EXPECT_TRUE( + ErrorEquals(ERR_get_error(), ERR_LIB_CIPHER, CIPHER_R_INVALID_NONCE)); } } diff --git a/crypto/hpke/hpke_test.cc b/crypto/hpke/hpke_test.cc index 30593f99a..ce68adc60 100644 --- a/crypto/hpke/hpke_test.cc +++ b/crypto/hpke/hpke_test.cc @@ -554,9 +554,8 @@ TEST(HPKETest, SetupSenderBufferTooSmall) { sender_ctx.get(), enc, &enc_len, sizeof(enc), EVP_hpke_x25519_hkdf_sha256(), EVP_hpke_hkdf_sha256(), EVP_hpke_aes_128_gcm(), public_key_r, sizeof(public_key_r), nullptr, 0)); - uint32_t err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_EVP, ERR_GET_LIB(err)); - EXPECT_EQ(EVP_R_INVALID_BUFFER_SIZE, ERR_GET_REASON(err)); + EXPECT_TRUE( + ErrorEquals(ERR_get_error(), ERR_LIB_EVP, EVP_R_INVALID_BUFFER_SIZE)); ERR_clear_error(); } @@ -587,9 +586,8 @@ TEST(HPKETest, SetupRecipientWrongLengthEnc) { ASSERT_FALSE(EVP_HPKE_CTX_setup_recipient( recipient_ctx.get(), key.get(), EVP_hpke_hkdf_sha256(), EVP_hpke_aes_128_gcm(), bogus_enc, sizeof(bogus_enc), nullptr, 0)); - uint32_t err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_EVP, ERR_GET_LIB(err)); - EXPECT_EQ(EVP_R_INVALID_PEER_KEY, ERR_GET_REASON(err)); + EXPECT_TRUE( + ErrorEquals(ERR_get_error(), ERR_LIB_EVP, EVP_R_INVALID_PEER_KEY)); ERR_clear_error(); } @@ -603,9 +601,8 @@ TEST(HPKETest, SetupSenderWrongLengthPeerPublicValue) { EVP_hpke_x25519_hkdf_sha256(), EVP_hpke_hkdf_sha256(), EVP_hpke_aes_128_gcm(), bogus_public_key_r, sizeof(bogus_public_key_r), nullptr, 0)); - uint32_t err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_EVP, ERR_GET_LIB(err)); - EXPECT_EQ(EVP_R_INVALID_PEER_KEY, ERR_GET_REASON(err)); + EXPECT_TRUE( + ErrorEquals(ERR_get_error(), ERR_LIB_EVP, EVP_R_INVALID_PEER_KEY)); ERR_clear_error(); } diff --git a/crypto/pem/pem_test.cc b/crypto/pem/pem_test.cc index aed523cd8..117e00062 100644 --- a/crypto/pem/pem_test.cc +++ b/crypto/pem/pem_test.cc @@ -20,6 +20,8 @@ #include #include +#include "../test/test_util.h" + // Test that implausible ciphers, notably an IV-less RC4, aren't allowed in PEM. // This is a regression test for https://github.com/openssl/openssl/issues/6347, @@ -39,7 +41,6 @@ TEST(PEMTest, NoRC4) { bssl::UniquePtr rsa(PEM_read_bio_RSAPublicKey( bio.get(), nullptr, nullptr, const_cast("password"))); EXPECT_FALSE(rsa); - uint32_t err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_PEM, ERR_GET_LIB(err)); - EXPECT_EQ(PEM_R_UNSUPPORTED_ENCRYPTION, ERR_GET_REASON(err)); + EXPECT_TRUE( + ErrorEquals(ERR_get_error(), ERR_LIB_PEM, PEM_R_UNSUPPORTED_ENCRYPTION)); } diff --git a/crypto/rsa_extra/rsa_test.cc b/crypto/rsa_extra/rsa_test.cc index 4d34fc947..82c5aa954 100644 --- a/crypto/rsa_extra/rsa_test.cc +++ b/crypto/rsa_extra/rsa_test.cc @@ -712,9 +712,8 @@ TEST(RSATest, GenerateSmallKey) { ASSERT_TRUE(BN_set_word(e.get(), RSA_F4)); EXPECT_FALSE(RSA_generate_key_ex(rsa.get(), 255, e.get(), nullptr)); - uint32_t err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_RSA, ERR_GET_LIB(err)); - EXPECT_EQ(RSA_R_KEY_SIZE_TOO_SMALL, ERR_GET_REASON(err)); + EXPECT_TRUE( + ErrorEquals(ERR_get_error(), ERR_LIB_RSA, RSA_R_KEY_SIZE_TOO_SMALL)); } // Attempting to generate an funny RSA key length should round down. @@ -1175,17 +1174,13 @@ TEST(RSATest, MissingParameters) { std::vector out(RSA_size(sample.get())); EXPECT_FALSE(RSA_sign(NID_sha256, kZeros, sizeof(kZeros), out.data(), &len_u, rsa.get())); - uint32_t err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_RSA, ERR_GET_LIB(err)); - EXPECT_EQ(RSA_R_VALUE_MISSING, ERR_GET_REASON(err)); + EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_RSA, RSA_R_VALUE_MISSING)); size_t len; EXPECT_FALSE(RSA_decrypt(rsa.get(), &len, out.data(), out.size(), kOAEPCiphertext1, sizeof(kOAEPCiphertext1), RSA_PKCS1_OAEP_PADDING)); - err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_RSA, ERR_GET_LIB(err)); - EXPECT_EQ(RSA_R_VALUE_MISSING, ERR_GET_REASON(err)); + EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_RSA, RSA_R_VALUE_MISSING)); // A private key without e cannot perform public key operations. rsa.reset(RSA_new_private_key_no_e(RSA_get0_n(sample.get()), @@ -1194,15 +1189,11 @@ TEST(RSATest, MissingParameters) { EXPECT_FALSE(RSA_verify(NID_sha256, kZeros, sizeof(kZeros), sig.data(), sig.size(), rsa.get())); - err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_RSA, ERR_GET_LIB(err)); - EXPECT_EQ(RSA_R_VALUE_MISSING, ERR_GET_REASON(err)); + EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_RSA, RSA_R_VALUE_MISSING)); EXPECT_FALSE(RSA_encrypt(rsa.get(), &len, out.data(), out.size(), kPlaintext, sizeof(kPlaintext), RSA_PKCS1_OAEP_PADDING)); - err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_RSA, ERR_GET_LIB(err)); - EXPECT_EQ(RSA_R_VALUE_MISSING, ERR_GET_REASON(err)); + EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_RSA, RSA_R_VALUE_MISSING)); } TEST(RSATest, Negative) { diff --git a/crypto/test/test_util.cc b/crypto/test/test_util.cc index 23e8909fe..6f5647681 100644 --- a/crypto/test/test_util.cc +++ b/crypto/test/test_util.cc @@ -16,6 +16,8 @@ #include +#include + #include "../internal.h" @@ -67,3 +69,16 @@ std::string EncodeHex(bssl::Span in) { return ret; } +testing::AssertionResult ErrorEquals(uint32_t err, int lib, int reason) { + if (ERR_GET_LIB(err) == lib && ERR_GET_REASON(err) == reason) { + return testing::AssertionSuccess(); + } + + char buf[128], expected[128]; + return testing::AssertionFailure() + << "Got \"" << ERR_error_string_n(err, buf, sizeof(buf)) + << "\", wanted \"" + << ERR_error_string_n(ERR_PACK(lib, reason), expected, + sizeof(expected)) + << "\""; +} diff --git a/crypto/test/test_util.h b/crypto/test/test_util.h index 796c93177..fff6cdb47 100644 --- a/crypto/test/test_util.h +++ b/crypto/test/test_util.h @@ -24,6 +24,8 @@ #include #include +#include + #include #include "../internal.h" @@ -67,5 +69,9 @@ bool DecodeHex(std::vector *out, const std::string &in); // EncodeHex returns |in| encoded in hexadecimal. std::string EncodeHex(bssl::Span in); +// ErrorEquals asserts that |err| is an error with library |lib| and reason +// |reason|. +testing::AssertionResult ErrorEquals(uint32_t err, int lib, int reason); + #endif // OPENSSL_HEADER_CRYPTO_TEST_TEST_UTIL_H diff --git a/crypto/x509/x509_test.cc b/crypto/x509/x509_test.cc index b25e8b395..8b20ade83 100644 --- a/crypto/x509/x509_test.cc +++ b/crypto/x509/x509_test.cc @@ -2062,9 +2062,8 @@ TEST(X509Test, TestEd25519BadParameters) { ASSERT_FALSE(X509_verify(cert.get(), pkey.get())); - uint32_t err = ERR_get_error(); - ASSERT_EQ(ERR_LIB_X509, ERR_GET_LIB(err)); - ASSERT_EQ(X509_R_INVALID_PARAMETER, ERR_GET_REASON(err)); + EXPECT_TRUE( + ErrorEquals(ERR_get_error(), ERR_LIB_X509, X509_R_INVALID_PARAMETER)); ERR_clear_error(); } @@ -2909,9 +2908,8 @@ TEST(X509Test, MismatchAlgorithms) { ASSERT_TRUE(pkey); EXPECT_FALSE(X509_verify(cert.get(), pkey.get())); - uint32_t err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_X509, ERR_GET_LIB(err)); - EXPECT_EQ(X509_R_SIGNATURE_ALGORITHM_MISMATCH, ERR_GET_REASON(err)); + EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_X509, + X509_R_SIGNATURE_ALGORITHM_MISMATCH)); } TEST(X509Test, PEMX509Info) { @@ -3044,9 +3042,8 @@ TEST(X509Test, ReadBIOEmpty) { // certificates. bssl::UniquePtr x509(d2i_X509_bio(bio.get(), nullptr)); EXPECT_FALSE(x509); - uint32_t err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_ASN1, ERR_GET_LIB(err)); - EXPECT_EQ(ASN1_R_HEADER_TOO_LONG, ERR_GET_REASON(err)); + EXPECT_TRUE( + ErrorEquals(ERR_get_error(), ERR_LIB_ASN1, ASN1_R_HEADER_TOO_LONG)); } TEST(X509Test, ReadBIOOneByte) { @@ -3058,9 +3055,8 @@ TEST(X509Test, ReadBIOOneByte) { // to signal EOF. bssl::UniquePtr x509(d2i_X509_bio(bio.get(), nullptr)); EXPECT_FALSE(x509); - uint32_t err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_ASN1, ERR_GET_LIB(err)); - EXPECT_EQ(ASN1_R_NOT_ENOUGH_DATA, ERR_GET_REASON(err)); + EXPECT_TRUE( + ErrorEquals(ERR_get_error(), ERR_LIB_ASN1, ASN1_R_NOT_ENOUGH_DATA)); } TEST(X509Test, PartialBIOReturn) { @@ -3773,9 +3769,8 @@ TEST(X509Test, AlgorithmParameters) { cert = CertFromPEM(kP256InvalidParam); ASSERT_TRUE(cert); EXPECT_FALSE(X509_verify(cert.get(), key.get())); - uint32_t err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_X509, ERR_GET_LIB(err)); - EXPECT_EQ(X509_R_INVALID_PARAMETER, ERR_GET_REASON(err)); + EXPECT_TRUE( + ErrorEquals(ERR_get_error(), ERR_LIB_X509, X509_R_INVALID_PARAMETER)); // RSA parameters should be NULL, but we accept omitted ones. key = PrivateKeyFromPEM(kRSAKey); @@ -3792,9 +3787,8 @@ TEST(X509Test, AlgorithmParameters) { cert = CertFromPEM(kRSAInvalidParam); ASSERT_TRUE(cert); EXPECT_FALSE(X509_verify(cert.get(), key.get())); - err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_X509, ERR_GET_LIB(err)); - EXPECT_EQ(X509_R_INVALID_PARAMETER, ERR_GET_REASON(err)); + EXPECT_TRUE( + ErrorEquals(ERR_get_error(), ERR_LIB_X509, X509_R_INVALID_PARAMETER)); } TEST(X509Test, GeneralName) { diff --git a/ssl/ssl_test.cc b/ssl/ssl_test.cc index d12d49c78..d7953e506 100644 --- a/ssl/ssl_test.cc +++ b/ssl/ssl_test.cc @@ -3021,9 +3021,8 @@ TEST(SSLTest, WriteAfterWrongVersionOnEarlyData) { // The client processes the ServerHello and fails. EXPECT_EQ(-1, SSL_do_handshake(client.get())); EXPECT_EQ(SSL_ERROR_SSL, SSL_get_error(client.get(), -1)); - uint32_t err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_SSL, ERR_GET_LIB(err)); - EXPECT_EQ(SSL_R_WRONG_VERSION_ON_EARLY_DATA, ERR_GET_REASON(err)); + EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_SSL, + SSL_R_WRONG_VERSION_ON_EARLY_DATA)); // The client should have written an alert to the transport. const uint8_t *unused; @@ -3035,9 +3034,8 @@ TEST(SSLTest, WriteAfterWrongVersionOnEarlyData) { // Writing should fail, with the same error as the handshake. EXPECT_EQ(-1, SSL_write(client.get(), "a", 1)); EXPECT_EQ(SSL_ERROR_SSL, SSL_get_error(client.get(), -1)); - err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_SSL, ERR_GET_LIB(err)); - EXPECT_EQ(SSL_R_WRONG_VERSION_ON_EARLY_DATA, ERR_GET_REASON(err)); + EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_SSL, + SSL_R_WRONG_VERSION_ON_EARLY_DATA)); // Nothing should be written to the transport. ASSERT_TRUE(BIO_mem_contents(mem.get(), &unused, &len)); @@ -5190,9 +5188,8 @@ TEST(SSLTest, NoCiphersAvailable) { int ret = SSL_do_handshake(ssl.get()); EXPECT_EQ(-1, ret); EXPECT_EQ(SSL_ERROR_SSL, SSL_get_error(ssl.get(), ret)); - uint32_t err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_SSL, ERR_GET_LIB(err)); - EXPECT_EQ(SSL_R_NO_CIPHERS_AVAILABLE, ERR_GET_REASON(err)); + EXPECT_TRUE( + ErrorEquals(ERR_get_error(), ERR_LIB_SSL, SSL_R_NO_CIPHERS_AVAILABLE)); } TEST_P(SSLVersionTest, SessionVersion) { @@ -7177,9 +7174,8 @@ TEST_F(QUICMethodTest, ExcessProvidedData) { // EncryptedExtensions on key change. ASSERT_EQ(SSL_do_handshake(client_.get()), -1); ASSERT_EQ(SSL_get_error(client_.get(), -1), SSL_ERROR_SSL); - uint32_t err = ERR_get_error(); - EXPECT_EQ(ERR_GET_LIB(err), ERR_LIB_SSL); - EXPECT_EQ(ERR_GET_REASON(err), SSL_R_EXCESS_HANDSHAKE_DATA); + EXPECT_TRUE( + ErrorEquals(ERR_get_error(), ERR_LIB_SSL, SSL_R_EXCESS_HANDSHAKE_DATA)); // The client sends an alert in response to this. The alert is sent at // handshake level because we install write secrets before read secrets and @@ -7628,9 +7624,8 @@ TEST_P(SSLVersionTest, DoubleSSLError) { // The client handshake should terminate on a certificate verification // error. EXPECT_EQ(SSL_ERROR_SSL, client_err); - uint32_t err = ERR_peek_error(); - EXPECT_EQ(ERR_LIB_SSL, ERR_GET_LIB(err)); - EXPECT_EQ(SSL_R_CERTIFICATE_VERIFY_FAILED, ERR_GET_REASON(err)); + EXPECT_TRUE(ErrorEquals(ERR_peek_error(), ERR_LIB_SSL, + SSL_R_CERTIFICATE_VERIFY_FAILED)); break; } @@ -7906,9 +7901,8 @@ TEST(SSLTest, WriteWhileExplicitRenegotiate) { // We never renegotiate as a server. ASSERT_EQ(-1, SSL_read(server.get(), buf, sizeof(buf))); ASSERT_EQ(SSL_ERROR_SSL, SSL_get_error(server.get(), -1)); - uint32_t err = ERR_get_error(); - EXPECT_EQ(ERR_LIB_SSL, ERR_GET_LIB(err)); - EXPECT_EQ(SSL_R_NO_RENEGOTIATION, ERR_GET_REASON(err)); + EXPECT_TRUE( + ErrorEquals(ERR_get_error(), ERR_LIB_SSL, SSL_R_NO_RENEGOTIATION)); } TEST(SSLTest, ConnectionPropertiesDuringRenegotiate) {