Skip to content

Commit

Permalink
Use macros
Browse files Browse the repository at this point in the history
(cherry picked from commit 74a1acd)
  • Loading branch information
justsmth authored and samuel40791765 committed May 31, 2024
1 parent acbfc6d commit 4a5be1e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 27 deletions.
27 changes: 3 additions & 24 deletions crypto/asn1/asn1_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2412,43 +2412,22 @@ 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_TYPE> asn1(
d2i_ASN1_TYPE(nullptr, &tag, sizeof(kTag128)));
ASSERT_TRUE(asn1);
EXPECT_EQ(128, asn1->type);
bssl::UniquePtr<ASN1_TYPE> 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);

bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
ASSERT_TRUE(key);
ASSERT_TRUE(EC_KEY_generate_key(key.get()));
bssl::UniquePtr<EC_KEY> 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())),
Expand All @@ -2468,7 +2447,7 @@ TEST(ASN1Test, ASN1Dup) {
ASSERT_TRUE(X509_PUBKEY_set(&tmp_key, evp_pkey.get()));
bssl::UniquePtr<X509_PUBKEY> x509_pubkey(tmp_key);
bssl::UniquePtr<X509_PUBKEY> 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()),
Expand Down
10 changes: 7 additions & 3 deletions include/openssl/asn1.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*|.
Expand Down

0 comments on commit 4a5be1e

Please sign in to comment.