1 /* Copyright (c) 2016, Google Inc.
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
15 #include <gtest/gtest.h>
17 #include <openssl/asn1.h>
18 #include <openssl/bytestring.h>
19 #include <openssl/crypto.h>
20 #include <openssl/obj.h>
22 #include "../internal.h"
25 TEST(ObjTest, TestBasic) {
26 static const int kNID = NID_sha256WithRSAEncryption;
27 static const char kShortName[] = "RSA-SHA256";
28 static const char kLongName[] = "sha256WithRSAEncryption";
29 static const char kText[] = "1.2.840.113549.1.1.11";
30 static const uint8_t kDER[] = {
31 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
35 CBS_init(&cbs, kDER, sizeof(kDER));
36 ASSERT_EQ(kNID, OBJ_cbs2nid(&cbs));
37 ASSERT_EQ(kNID, OBJ_sn2nid(kShortName));
38 ASSERT_EQ(kNID, OBJ_ln2nid(kLongName));
39 ASSERT_EQ(kNID, OBJ_txt2nid(kShortName));
40 ASSERT_EQ(kNID, OBJ_txt2nid(kLongName));
41 ASSERT_EQ(kNID, OBJ_txt2nid(kText));
43 ASSERT_STREQ(kShortName, OBJ_nid2sn(kNID));
44 ASSERT_STREQ(kLongName, OBJ_nid2ln(kNID));
46 ASSERT_EQ(NID_undef, OBJ_sn2nid("this is not an OID"));
47 ASSERT_EQ(NID_undef, OBJ_ln2nid("this is not an OID"));
48 ASSERT_EQ(NID_undef, OBJ_txt2nid("this is not an OID"));
50 CBS_init(&cbs, NULL, 0);
51 ASSERT_EQ(NID_undef, OBJ_cbs2nid(&cbs));
53 // 1.2.840.113554.4.1.72585.2 (https://davidben.net/oid).
54 static const uint8_t kUnknownDER[] = {
55 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x02,
57 CBS_init(&cbs, kUnknownDER, sizeof(kUnknownDER));
58 ASSERT_EQ(NID_undef, OBJ_cbs2nid(&cbs));
61 TEST(ObjTest, TestSignatureAlgorithms) {
62 int digest_nid, pkey_nid;
63 ASSERT_TRUE(OBJ_find_sigid_algs(NID_sha256WithRSAEncryption, &digest_nid,
65 ASSERT_EQ(digest_nid, NID_sha256);
66 ASSERT_EQ(pkey_nid, NID_rsaEncryption);
68 ASSERT_FALSE(OBJ_find_sigid_algs(NID_sha256, &digest_nid, &pkey_nid));
71 ASSERT_TRUE(OBJ_find_sigid_by_algs(&sign_nid, NID_sha256, NID_rsaEncryption));
72 ASSERT_EQ(sign_nid, NID_sha256WithRSAEncryption);
73 ASSERT_FALSE(OBJ_find_sigid_by_algs(&sign_nid, NID_dsa, NID_rsaEncryption));
76 static bool ExpectObj2Txt(const uint8_t *der, size_t der_len,
77 bool always_return_oid, const char *expected) {
79 OPENSSL_memset(&obj, 0, sizeof(obj));
81 obj.length = static_cast<int>(der_len);
83 int expected_len = static_cast<int>(strlen(expected));
85 int len = OBJ_obj2txt(nullptr, 0, &obj, always_return_oid);
86 if (len != expected_len) {
88 "OBJ_obj2txt of %s with out_len = 0 returned %d, wanted %d.\n",
89 expected, len, expected_len);
94 OPENSSL_memset(short_buf, 0xff, sizeof(short_buf));
95 len = OBJ_obj2txt(short_buf, sizeof(short_buf), &obj, always_return_oid);
96 if (len != expected_len) {
98 "OBJ_obj2txt of %s with out_len = 1 returned %d, wanted %d.\n",
99 expected, len, expected_len);
103 if (OPENSSL_memchr(short_buf, '\0', sizeof(short_buf)) == nullptr) {
105 "OBJ_obj2txt of %s with out_len = 1 did not NUL-terminate the "
112 len = OBJ_obj2txt(buf, sizeof(buf), &obj, always_return_oid);
113 if (len != expected_len) {
115 "OBJ_obj2txt of %s with out_len = 256 returned %d, wanted %d.\n",
116 expected, len, expected_len);
120 if (strcmp(buf, expected) != 0) {
121 fprintf(stderr, "OBJ_obj2txt returned \"%s\"; wanted \"%s\".\n", buf,
129 TEST(ObjTest, TestObj2Txt) {
130 // kSHA256WithRSAEncryption is the DER representation of
131 // 1.2.840.113549.1.1.11, id-sha256WithRSAEncryption.
132 static const uint8_t kSHA256WithRSAEncryption[] = {
133 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
136 // kBasicConstraints is the DER representation of 2.5.29.19,
137 // id-basicConstraints.
138 static const uint8_t kBasicConstraints[] = {
142 // kTestOID is the DER representation of 1.2.840.113554.4.1.72585.0,
143 // from https://davidben.net/oid.
144 static const uint8_t kTestOID[] = {
145 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x00,
149 ExpectObj2Txt(kSHA256WithRSAEncryption, sizeof(kSHA256WithRSAEncryption),
150 true /* don't return name */, "1.2.840.113549.1.1.11"));
152 ExpectObj2Txt(kSHA256WithRSAEncryption, sizeof(kSHA256WithRSAEncryption),
153 false /* return name */, "sha256WithRSAEncryption"));
154 ASSERT_TRUE(ExpectObj2Txt(kBasicConstraints, sizeof(kBasicConstraints),
155 true /* don't return name */, "2.5.29.19"));
156 ASSERT_TRUE(ExpectObj2Txt(kBasicConstraints, sizeof(kBasicConstraints),
157 false /* return name */,
158 "X509v3 Basic Constraints"));
159 ASSERT_TRUE(ExpectObj2Txt(kTestOID, sizeof(kTestOID),
160 true /* don't return name */,
161 "1.2.840.113554.4.1.72585.0"));
162 ASSERT_TRUE(ExpectObj2Txt(kTestOID, sizeof(kTestOID), false /* return name */,
163 "1.2.840.113554.4.1.72585.0"));
164 // Python depends on the empty OID successfully encoding as the empty
166 ASSERT_TRUE(ExpectObj2Txt(nullptr, 0, false /* return name */, ""));
167 ASSERT_TRUE(ExpectObj2Txt(nullptr, 0, true /* don't return name */, ""));
170 OPENSSL_memset(&obj, 0, sizeof(obj));
172 // kNonMinimalOID is kBasicConstraints with the final component non-minimally
174 static const uint8_t kNonMinimalOID[] = {
175 0x55, 0x1d, 0x80, 0x13,
177 obj.data = kNonMinimalOID;
178 obj.length = sizeof(kNonMinimalOID);
179 ASSERT_EQ(-1, OBJ_obj2txt(NULL, 0, &obj, 0));
181 // kOverflowOID is the DER representation of
182 // 1.2.840.113554.4.1.72585.18446744073709551616. (The final value is 2^64.)
183 static const uint8_t kOverflowOID[] = {
184 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09,
185 0x82, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00,
187 obj.data = kOverflowOID;
188 obj.length = sizeof(kOverflowOID);
189 ASSERT_EQ(-1, OBJ_obj2txt(NULL, 0, &obj, 0));
191 // kInvalidOID is a mis-encoded version of kBasicConstraints with the final
192 // octet having the high bit set.
193 static const uint8_t kInvalidOID[] = {
196 obj.data = kInvalidOID;
197 obj.length = sizeof(kInvalidOID);
198 ASSERT_EQ(-1, OBJ_obj2txt(NULL, 0, &obj, 0));