From 4a5be1e45173d1615c2d5ba19d0b228d16a22af3 Mon Sep 17 00:00:00 2001 From: Justin Smith Date: Thu, 23 May 2024 16:26:41 -0400 Subject: [PATCH] Use macros (cherry picked from commit 74a1acd3666c007e6ae15a6090e076bf05fe3fd0) --- crypto/asn1/asn1_test.cc | 27 +++------------------------ include/openssl/asn1.h | 10 +++++++--- 2 files changed, 10 insertions(+), 27 deletions(-) diff --git a/crypto/asn1/asn1_test.cc b/crypto/asn1/asn1_test.cc index c8cea4613c..6e56feca0d 100644 --- a/crypto/asn1/asn1_test.cc +++ b/crypto/asn1/asn1_test.cc @@ -2412,27 +2412,6 @@ TEST(ASN1Test, LargeString) { #endif } - -// Wrapper functions are needed to get around Control Flow Integrity Sanitizers. -static int i2d_ASN1_TYPE_void(const void *a, unsigned char **out) { - return i2d_ASN1_TYPE((ASN1_TYPE *)a, out); -} -static void *d2i_ASN1_TYPE_void(void **a, const unsigned char **in, long len) { - return d2i_ASN1_TYPE((ASN1_TYPE **)a, in, len); -} -static int i2d_ECPrivateKey_void(const void *a, unsigned char **out) { - return i2d_ECPrivateKey((EC_KEY *)a, out); -} -static void *d2i_ECPrivateKey_void(void **a, const unsigned char **in, long len) { - return d2i_ECPrivateKey((EC_KEY **)a, in, len); -} -static int i2d_X509_PUBKEY_void(const void *a, unsigned char **out) { - return i2d_X509_PUBKEY((X509_PUBKEY *)a, out); -} -static void *d2i_X509_PUBKEY_void(void **a, const unsigned char **in, long len) { - return d2i_X509_PUBKEY((X509_PUBKEY **)a, in, len); -} - TEST(ASN1Test, ASN1Dup) { const uint8_t *tag = kTag128; bssl::UniquePtr asn1( @@ -2440,7 +2419,7 @@ TEST(ASN1Test, ASN1Dup) { ASSERT_TRUE(asn1); EXPECT_EQ(128, asn1->type); bssl::UniquePtr asn1_copy((ASN1_TYPE *)ASN1_dup( - i2d_ASN1_TYPE_void, d2i_ASN1_TYPE_void, asn1.get())); + CHECKED_I2D_OF(ASN1_TYPE, i2d_ASN1_TYPE), CHECKED_D2I_OF(ASN1_TYPE, d2i_ASN1_TYPE), asn1.get())); ASSERT_TRUE(asn1_copy); EXPECT_EQ(ASN1_TYPE_cmp(asn1.get(), asn1_copy.get()), 0); @@ -2448,7 +2427,7 @@ TEST(ASN1Test, ASN1Dup) { ASSERT_TRUE(key); ASSERT_TRUE(EC_KEY_generate_key(key.get())); bssl::UniquePtr key_copy((EC_KEY *)ASN1_dup( - i2d_ECPrivateKey_void, d2i_ECPrivateKey_void, key.get())); + CHECKED_I2D_OF(EC_KEY, i2d_ECPrivateKey), CHECKED_D2I_OF(EC_KEY, d2i_ECPrivateKey), key.get())); ASSERT_TRUE(key_copy); EXPECT_EQ(BN_cmp(EC_KEY_get0_private_key(key.get()), EC_KEY_get0_private_key(key_copy.get())), @@ -2468,7 +2447,7 @@ TEST(ASN1Test, ASN1Dup) { ASSERT_TRUE(X509_PUBKEY_set(&tmp_key, evp_pkey.get())); bssl::UniquePtr x509_pubkey(tmp_key); bssl::UniquePtr x509_pubkey_copy((X509_PUBKEY *)ASN1_dup( - i2d_X509_PUBKEY_void, d2i_X509_PUBKEY_void, x509_pubkey.get())); + CHECKED_I2D_OF(X509_PUBKEY, i2d_X509_PUBKEY), CHECKED_D2I_OF(X509_PUBKEY, d2i_X509_PUBKEY), x509_pubkey.get())); ASSERT_TRUE(x509_pubkey_copy); EXPECT_EQ( ASN1_STRING_cmp(X509_PUBKEY_get0_public_key(x509_pubkey.get()), diff --git a/include/openssl/asn1.h b/include/openssl/asn1.h index 69bb666a0d..ba5b812870 100644 --- a/include/openssl/asn1.h +++ b/include/openssl/asn1.h @@ -269,13 +269,17 @@ int i2d_SAMPLE(const SAMPLE *in, uint8_t **outp); // The following macros are used to retrieve the function pointer of the // |d2i| or |i2d| ASN1 functions of |type|. -// -// NOTE: |D2I_OF| and |I2D_OF_const| are not implemented. #define I2D_OF(type) int (*)(type *, unsigned char **) +#define I2D_OF_const(type) int (*)(const type *, unsigned char **) +#define D2I_OF(type) type* (*)(type **, const unsigned char **, long) // CHECKED_I2D_OF casts a given pointer to i2d_of_void* and statically checks // that it was a pointer to |type|'s |i2d| function. -#define CHECKED_I2D_OF(type, i2d) ((i2d_of_void *)(1 ? i2d : ((I2D_OF(type))0))) +#define CHECKED_I2D_OF(type, i2d) ((i2d_of_void *)(1 ? i2d : ((I2D_OF_const(type))0))) + +// CHECKED_D2I_OF casts a given pointer to d2i_of_void* and statically checks +// that it was a pointer to |type|'s |d2i| function. +#define CHECKED_D2I_OF(type, d2i) ((d2i_of_void *)(1 ? d2i : ((D2I_OF(type))0))) // The following typedefs are sometimes used for pointers to functions like // |d2i_SAMPLE| and |i2d_SAMPLE|. Note, however, that these act on |void*|.