From 4884808b6929d6c789c4631661c9c8ca6b5746ed Mon Sep 17 00:00:00 2001 From: Justin W Smith <103147162+justsmth@users.noreply.github.com> Date: Tue, 5 Dec 2023 09:45:14 -0500 Subject: [PATCH] Per PR comments --- crypto/pem/pem_test.cc | 28 ++++++++++++++-------------- crypto/x509/x509_test.cc | 5 ++--- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/crypto/pem/pem_test.cc b/crypto/pem/pem_test.cc index 2373d8ca882..df135022a06 100644 --- a/crypto/pem/pem_test.cc +++ b/crypto/pem/pem_test.cc @@ -27,13 +27,15 @@ const char* SECRET = "test"; static int pem_password_callback(char *buf, int size, int rwflag, void *userdata) { char* data = (char *)userdata; - int data_len = (int)strlen(data); - if(size + 1 < data_len) { - data_len = size-1; - buf[size-1] = 0; + int data_len = (int)strlen(data) + 1; // Add 1 for null terminator + if(size < data_len) { + data_len = size; } - strncpy(buf, data, data_len); - return data_len; + if(data_len <= 0) { + return 0; + } + BUF_strlcpy(buf, data, data_len); + return data_len-1; } // Test that implausible ciphers, notably an IV-less RC4, aren't allowed in PEM. @@ -80,17 +82,15 @@ TEST(PEMTest, WriteReadASN1IntegerPem) { GTEST_SKIP(); #endif // Numbers for testing - long nums[8] = { + std::vector nums = { 0x00000001L, 0x00000100L, 0x00010000L, 0x01000000L, -2L}; - for(size_t i = 0; i < 8; i++) { - long original_value = nums[i]; - - // Create an ASN1_INTEGER with value 42 + for(long original_value: nums) { + // Create an ASN1_INTEGER with value bssl::UniquePtr asn1_int(ASN1_INTEGER_new()); ASSERT_TRUE(asn1_int.get()); ASSERT_TRUE(ASN1_INTEGER_set(asn1_int.get(), original_value)); @@ -130,7 +130,7 @@ TEST(PEMTest, ReadPrivateKeyPem) { bssl::UniquePtr read_bio(BIO_new_mem_buf(kPemRsaPrivateKey, strlen(kPemRsaPrivateKey)) ); ASSERT_TRUE(read_bio.get()); bssl::UniquePtr ec_key(PEM_read_bio_ECPrivateKey(read_bio.get(), nullptr, pem_password_callback, (void*)SECRET)); - ASSERT_TRUE(ec_key); + ASSERT_TRUE(ec_key.get()); const EC_GROUP* p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1); ASSERT_EQ(p256, EC_KEY_get0_group(ec_key.get())); } @@ -142,7 +142,7 @@ TEST(PEMTest, WriteReadRSAPem) { bssl::UniquePtr bn(BN_new()); ASSERT_TRUE(bn); BN_set_u64(bn.get(), RSA_F4); -#ifdef BORINGSSL_FIPS +#if defined(BORINGSSL_FIPS) ASSERT_TRUE(RSA_generate_key_fips(rsa.get(), 2048, nullptr)); #else ASSERT_TRUE(RSA_generate_key_ex(rsa.get(), 2048, bn.get(), nullptr)); @@ -172,7 +172,7 @@ TEST(PEMTest, WriteReadECPem) { ASSERT_TRUE(ec_group.get()); ASSERT_TRUE(EC_KEY_set_group(ec_key.get(), ec_group.get())); -#ifdef BORINGSSL_FIPS +#if defined(BORINGSSL_FIPS) ASSERT_TRUE(EC_KEY_generate_key_fips(ec_key.get())); #else ASSERT_TRUE(EC_KEY_generate_key(ec_key.get())); diff --git a/crypto/x509/x509_test.cc b/crypto/x509/x509_test.cc index 9dadf4430fb..910fc630222 100644 --- a/crypto/x509/x509_test.cc +++ b/crypto/x509/x509_test.cc @@ -5716,7 +5716,7 @@ TEST(X509Test, SetSerialNumberCheckEndian) { ASSERT_TRUE(root); // Numbers for testing - int64_t nums[8] = { + std::vector nums = { 0x0000000000000001LL, 0x0000000000000100LL, 0x0000000000010000LL, @@ -5726,8 +5726,7 @@ TEST(X509Test, SetSerialNumberCheckEndian) { 0x0001000000000000LL, -2LL}; - for(size_t i = 0; i < 8; i++) { - int64_t num = nums[i]; + for(int64_t num: nums) { bssl::UniquePtr serial(ASN1_INTEGER_new()); ASSERT_TRUE(serial); // Set serial number for cert