| 1 |
// Copyright (c) 2016, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include <gtest/gtest.h> |
| 5 |
|
| 6 |
#include <openssl/asn1.h> |
| 7 |
#include <openssl/bytestring.h> |
| 8 |
#include <openssl/crypto.h> |
| 9 |
#include <openssl/obj.h> |
| 10 |
|
| 11 |
#include "../internal.h" |
| 12 |
|
| 13 |
|
| 14 |
TEST(ObjTest, TestBasic) { |
| 15 |
static const int kNID = NID_sha256WithRSAEncryption; |
| 16 |
static const char kShortName[] = "RSA-SHA256"; |
| 17 |
static const char kLongName[] = "sha256WithRSAEncryption"; |
| 18 |
static const char kText[] = "1.2.840.113549.1.1.11"; |
| 19 |
static const uint8_t kDER[] = { |
| 20 |
0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, |
| 21 |
}; |
| 22 |
|
| 23 |
CBS cbs; |
| 24 |
CBS_init(&cbs, kDER, sizeof(kDER)); |
| 25 |
ASSERT_EQ(kNID, OBJ_cbs2nid(&cbs)); |
| 26 |
ASSERT_EQ(kNID, OBJ_sn2nid(kShortName)); |
| 27 |
ASSERT_EQ(kNID, OBJ_ln2nid(kLongName)); |
| 28 |
ASSERT_EQ(kNID, OBJ_txt2nid(kShortName)); |
| 29 |
ASSERT_EQ(kNID, OBJ_txt2nid(kLongName)); |
| 30 |
ASSERT_EQ(kNID, OBJ_txt2nid(kText)); |
| 31 |
|
| 32 |
ASSERT_STREQ(kShortName, OBJ_nid2sn(kNID)); |
| 33 |
ASSERT_STREQ(kLongName, OBJ_nid2ln(kNID)); |
| 34 |
|
| 35 |
ASSERT_EQ(NID_undef, OBJ_sn2nid("this is not an OID")); |
| 36 |
ASSERT_EQ(NID_undef, OBJ_ln2nid("this is not an OID")); |
| 37 |
ASSERT_EQ(NID_undef, OBJ_txt2nid("this is not an OID")); |
| 38 |
|
| 39 |
CBS_init(&cbs, NULL, 0); |
| 40 |
ASSERT_EQ(NID_undef, OBJ_cbs2nid(&cbs)); |
| 41 |
|
| 42 |
// 1.2.840.113554.4.1.72585.2 (https://davidben.net/oid). |
| 43 |
static const uint8_t kUnknownDER[] = { |
| 44 |
0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x02, |
| 45 |
}; |
| 46 |
CBS_init(&cbs, kUnknownDER, sizeof(kUnknownDER)); |
| 47 |
ASSERT_EQ(NID_undef, OBJ_cbs2nid(&cbs)); |
| 48 |
|
| 49 |
EXPECT_EQ(NID_undef, OBJ_sn2nid("UNDEF")); |
| 50 |
EXPECT_EQ(NID_undef, OBJ_ln2nid("undefined")); |
| 51 |
EXPECT_EQ(OBJ_get_undef(), OBJ_nid2obj(NID_undef)); |
| 52 |
} |
| 53 |
|
| 54 |
TEST(ObjTest, TestSignatureAlgorithms) { |
| 55 |
int digest_nid, pkey_nid; |
| 56 |
ASSERT_TRUE(OBJ_find_sigid_algs(NID_sha256WithRSAEncryption, &digest_nid, |
| 57 |
&pkey_nid)); |
| 58 |
ASSERT_EQ(digest_nid, NID_sha256); |
| 59 |
ASSERT_EQ(pkey_nid, NID_rsaEncryption); |
| 60 |
|
| 61 |
ASSERT_FALSE(OBJ_find_sigid_algs(NID_sha256, &digest_nid, &pkey_nid)); |
| 62 |
|
| 63 |
int sign_nid; |
| 64 |
ASSERT_TRUE(OBJ_find_sigid_by_algs(&sign_nid, NID_sha256, NID_rsaEncryption)); |
| 65 |
ASSERT_EQ(sign_nid, NID_sha256WithRSAEncryption); |
| 66 |
ASSERT_FALSE(OBJ_find_sigid_by_algs(&sign_nid, NID_dsa, NID_rsaEncryption)); |
| 67 |
} |
| 68 |
|
| 69 |
static bool ExpectObj2Txt(const uint8_t *der, size_t der_len, |
| 70 |
bool always_return_oid, const char *expected) { |
| 71 |
bssl::UniquePtr<ASN1_OBJECT> obj( |
| 72 |
ASN1_OBJECT_create(NID_undef, der, static_cast<int>(der_len), |
| 73 |
/*sn=*/nullptr, /*ln=*/nullptr)); |
| 74 |
if (!obj) { |
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
int expected_len = static_cast<int>(strlen(expected)); |
| 79 |
|
| 80 |
int len = OBJ_obj2txt(nullptr, 0, obj.get(), always_return_oid); |
| 81 |
if (len != expected_len) { |
| 82 |
fprintf(stderr, |
| 83 |
"OBJ_obj2txt of %s with out_len = 0 returned %d, wanted %d.\n", |
| 84 |
expected, len, expected_len); |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
char short_buf[1]; |
| 89 |
OPENSSL_memset(short_buf, 0xff, sizeof(short_buf)); |
| 90 |
len = OBJ_obj2txt(short_buf, sizeof(short_buf), obj.get(), always_return_oid); |
| 91 |
if (len != expected_len) { |
| 92 |
fprintf(stderr, |
| 93 |
"OBJ_obj2txt of %s with out_len = 1 returned %d, wanted %d.\n", |
| 94 |
expected, len, expected_len); |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
if (OPENSSL_memchr(short_buf, '\0', sizeof(short_buf)) == nullptr) { |
| 99 |
fprintf(stderr, |
| 100 |
"OBJ_obj2txt of %s with out_len = 1 did not NUL-terminate the " |
| 101 |
"output.\n", |
| 102 |
expected); |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
char buf[256]; |
| 107 |
len = OBJ_obj2txt(buf, sizeof(buf), obj.get(), always_return_oid); |
| 108 |
if (len != expected_len) { |
| 109 |
fprintf(stderr, |
| 110 |
"OBJ_obj2txt of %s with out_len = 256 returned %d, wanted %d.\n", |
| 111 |
expected, len, expected_len); |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
if (strcmp(buf, expected) != 0) { |
| 116 |
fprintf(stderr, "OBJ_obj2txt returned \"%s\"; wanted \"%s\".\n", buf, |
| 117 |
expected); |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
return true; |
| 122 |
} |
| 123 |
|
| 124 |
TEST(ObjTest, TestObj2Txt) { |
| 125 |
// kSHA256WithRSAEncryption is the DER representation of |
| 126 |
// 1.2.840.113549.1.1.11, id-sha256WithRSAEncryption. |
| 127 |
static const uint8_t kSHA256WithRSAEncryption[] = { |
| 128 |
0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, |
| 129 |
}; |
| 130 |
|
| 131 |
// kBasicConstraints is the DER representation of 2.5.29.19, |
| 132 |
// id-basicConstraints. |
| 133 |
static const uint8_t kBasicConstraints[] = { |
| 134 |
0x55, 0x1d, 0x13, |
| 135 |
}; |
| 136 |
|
| 137 |
// kTestOID is the DER representation of 1.2.840.113554.4.1.72585.0, |
| 138 |
// from https://davidben.net/oid. |
| 139 |
static const uint8_t kTestOID[] = { |
| 140 |
0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x00, |
| 141 |
}; |
| 142 |
|
| 143 |
ASSERT_TRUE( |
| 144 |
ExpectObj2Txt(kSHA256WithRSAEncryption, sizeof(kSHA256WithRSAEncryption), |
| 145 |
true /* don't return name */, "1.2.840.113549.1.1.11")); |
| 146 |
ASSERT_TRUE( |
| 147 |
ExpectObj2Txt(kSHA256WithRSAEncryption, sizeof(kSHA256WithRSAEncryption), |
| 148 |
false /* return name */, "sha256WithRSAEncryption")); |
| 149 |
ASSERT_TRUE(ExpectObj2Txt(kBasicConstraints, sizeof(kBasicConstraints), |
| 150 |
true /* don't return name */, "2.5.29.19")); |
| 151 |
ASSERT_TRUE(ExpectObj2Txt(kBasicConstraints, sizeof(kBasicConstraints), |
| 152 |
false /* return name */, |
| 153 |
"X509v3 Basic Constraints")); |
| 154 |
ASSERT_TRUE(ExpectObj2Txt(kTestOID, sizeof(kTestOID), |
| 155 |
true /* don't return name */, |
| 156 |
"1.2.840.113554.4.1.72585.0")); |
| 157 |
ASSERT_TRUE(ExpectObj2Txt(kTestOID, sizeof(kTestOID), false /* return name */, |
| 158 |
"1.2.840.113554.4.1.72585.0")); |
| 159 |
// Python depends on the empty OID successfully encoding as the empty |
| 160 |
// string. |
| 161 |
ASSERT_TRUE(ExpectObj2Txt(nullptr, 0, false /* return name */, "")); |
| 162 |
ASSERT_TRUE(ExpectObj2Txt(nullptr, 0, true /* don't return name */, "")); |
| 163 |
|
| 164 |
// kNonMinimalOID is kBasicConstraints with the final component non-minimally |
| 165 |
// encoded. |
| 166 |
static const uint8_t kNonMinimalOID[] = {0x55, 0x1d, 0x80, 0x13}; |
| 167 |
bssl::UniquePtr<ASN1_OBJECT> obj( |
| 168 |
ASN1_OBJECT_create(NID_undef, kNonMinimalOID, sizeof(kNonMinimalOID), |
| 169 |
/*sn=*/nullptr, /*ln=*/nullptr)); |
| 170 |
ASSERT_TRUE(obj); |
| 171 |
ASSERT_EQ(-1, OBJ_obj2txt(NULL, 0, obj.get(), 0)); |
| 172 |
|
| 173 |
// kOverflowOID is the DER representation of |
| 174 |
// 1.2.840.113554.4.1.72585.18446744073709551616. (The final value is 2^64.) |
| 175 |
static const uint8_t kOverflowOID[] = { |
| 176 |
0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, |
| 177 |
0x82, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, |
| 178 |
}; |
| 179 |
obj.reset(ASN1_OBJECT_create(NID_undef, kOverflowOID, sizeof(kOverflowOID), |
| 180 |
/*sn=*/nullptr, /*ln=*/nullptr)); |
| 181 |
ASSERT_TRUE(obj); |
| 182 |
ASSERT_EQ(-1, OBJ_obj2txt(NULL, 0, obj.get(), 0)); |
| 183 |
|
| 184 |
// kInvalidOID is a mis-encoded version of kBasicConstraints with the final |
| 185 |
// octet having the high bit set. |
| 186 |
static const uint8_t kInvalidOID[] = {0x55, 0x1d, 0x93}; |
| 187 |
obj.reset(ASN1_OBJECT_create(NID_undef, kInvalidOID, sizeof(kInvalidOID), |
| 188 |
/*sn=*/nullptr, /*ln=*/nullptr)); |
| 189 |
ASSERT_TRUE(obj); |
| 190 |
ASSERT_EQ(-1, OBJ_obj2txt(NULL, 0, obj.get(), 0)); |
| 191 |
} |