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