blob: 8bec2b48b679718503415d93caec9439e51308b9 [file] [log] [blame]
David Benjamindc9194f2016-04-24 12:17:46 -04001/* 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 <stdio.h>
16#include <string.h>
17
David Benjaminea655fa2016-08-04 16:45:15 -040018#include <openssl/asn1.h>
David Benjamindc9194f2016-04-24 12:17:46 -040019#include <openssl/bytestring.h>
20#include <openssl/crypto.h>
21#include <openssl/obj.h>
22
23
24static bool TestBasic() {
25 static const int kNID = NID_sha256WithRSAEncryption;
26 static const char kShortName[] = "RSA-SHA256";
27 static const char kLongName[] = "sha256WithRSAEncryption";
28 static const char kText[] = "1.2.840.113549.1.1.11";
29 static const uint8_t kDER[] = {
30 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
31 };
32
33 CBS cbs;
34 CBS_init(&cbs, kDER, sizeof(kDER));
35 if (OBJ_cbs2nid(&cbs) != kNID ||
36 OBJ_sn2nid(kShortName) != kNID ||
37 OBJ_ln2nid(kLongName) != kNID ||
38 OBJ_txt2nid(kShortName) != kNID ||
39 OBJ_txt2nid(kLongName) != kNID ||
40 OBJ_txt2nid(kText) != kNID) {
41 return false;
42 }
43
44 if (strcmp(kShortName, OBJ_nid2sn(kNID)) != 0 ||
45 strcmp(kLongName, OBJ_nid2ln(kNID)) != 0) {
46 return false;
47 }
48
49 if (OBJ_sn2nid("this is not an OID") != NID_undef ||
50 OBJ_ln2nid("this is not an OID") != NID_undef ||
51 OBJ_txt2nid("this is not an OID") != NID_undef) {
52 return false;
53 }
54
55 CBS_init(&cbs, NULL, 0);
56 if (OBJ_cbs2nid(&cbs) != NID_undef) {
57 return false;
58 }
59
60 // 1.2.840.113554.4.1.72585.2 (https://davidben.net/oid).
61 static const uint8_t kUnknownDER[] = {
62 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x02,
63 };
64 CBS_init(&cbs, kUnknownDER, sizeof(kUnknownDER));
65 if (OBJ_cbs2nid(&cbs) != NID_undef) {
66 return false;
67 }
68
69 return true;
70}
71
David Benjaminf040d3c2016-04-26 11:52:29 -040072static bool TestSignatureAlgorithms() {
David Benjamindc9194f2016-04-24 12:17:46 -040073 int digest_nid, pkey_nid;
74 if (!OBJ_find_sigid_algs(NID_sha256WithRSAEncryption, &digest_nid,
75 &pkey_nid) ||
76 digest_nid != NID_sha256 || pkey_nid != NID_rsaEncryption) {
77 return false;
78 }
79
80 if (OBJ_find_sigid_algs(NID_sha256, &digest_nid, &pkey_nid)) {
81 return false;
82 }
83
84 int sign_nid;
85 if (!OBJ_find_sigid_by_algs(&sign_nid, NID_sha256, NID_rsaEncryption) ||
86 sign_nid != NID_sha256WithRSAEncryption) {
87 return false;
88 }
89
90 if (OBJ_find_sigid_by_algs(&sign_nid, NID_dsa, NID_rsaEncryption)) {
91 return false;
92 }
93
94 return true;
95}
96
David Benjaminea655fa2016-08-04 16:45:15 -040097static bool ExpectObj2Txt(const uint8_t *der, size_t der_len,
98 bool dont_return_name,
99 const char *expected) {
100 ASN1_OBJECT obj;
101 memset(&obj, 0, sizeof(obj));
102 obj.data = der;
103 obj.length = static_cast<int>(der_len);
104
105 int expected_len = static_cast<int>(strlen(expected));
106
107 int len = OBJ_obj2txt(nullptr, 0, &obj, dont_return_name);
108 if (len != expected_len) {
109 fprintf(stderr,
110 "OBJ_obj2txt of %s with out_len = 0 returned %d, wanted %d.\n",
111 expected, len, expected_len);
112 return false;
113 }
114
115 char short_buf[1];
116 memset(short_buf, 0xff, sizeof(short_buf));
117 len = OBJ_obj2txt(short_buf, sizeof(short_buf), &obj, dont_return_name);
118 if (len != expected_len) {
119 fprintf(stderr,
120 "OBJ_obj2txt of %s with out_len = 1 returned %d, wanted %d.\n",
121 expected, len, expected_len);
122 return false;
123 }
124
125 if (memchr(short_buf, '\0', sizeof(short_buf)) == nullptr) {
126 fprintf(stderr,
127 "OBJ_obj2txt of %s with out_len = 1 did not NUL-terminate the "
128 "output.\n",
129 expected);
130 return false;
131 }
132
133 char buf[256];
134 len = OBJ_obj2txt(buf, sizeof(buf), &obj, dont_return_name);
135 if (len != expected_len) {
136 fprintf(stderr,
137 "OBJ_obj2txt of %s with out_len = 256 returned %d, wanted %d.\n",
138 expected, len, expected_len);
139 return false;
140 }
141
142 if (strcmp(buf, expected) != 0) {
143 fprintf(stderr, "OBJ_obj2txt returned \"%s\"; wanted \"%s\".\n", buf,
144 expected);
145 return false;
146 }
147
148 return true;
149}
150
151static bool TestObj2Txt() {
152 // kSHA256WithRSAEncryption is the DER representation of
153 // 1.2.840.113549.1.1.11, id-sha256WithRSAEncryption.
154 static const uint8_t kSHA256WithRSAEncryption[] = {
155 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
156 };
157
158 // kBasicConstraints is the DER representation of 2.5.29.19,
159 // id-basicConstraints.
160 static const uint8_t kBasicConstraints[] = {
161 0x55, 0x1d, 0x13,
162 };
163
164 // kTestOID is the DER representation of 1.2.840.113554.4.1.72585.0,
165 // from https://davidben.net/oid.
166 static const uint8_t kTestOID[] = {
167 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x00,
168 };
169
170 if (!ExpectObj2Txt(kSHA256WithRSAEncryption, sizeof(kSHA256WithRSAEncryption),
171 true /* don't return name */, "1.2.840.113549.1.1.11") ||
172 !ExpectObj2Txt(kSHA256WithRSAEncryption, sizeof(kSHA256WithRSAEncryption),
173 false /* return name */, "sha256WithRSAEncryption") ||
174 !ExpectObj2Txt(kBasicConstraints, sizeof(kBasicConstraints),
175 true /* don't return name */, "2.5.29.19") ||
176 !ExpectObj2Txt(kBasicConstraints, sizeof(kBasicConstraints),
177 false /* return name */, "X509v3 Basic Constraints") ||
178 !ExpectObj2Txt(kTestOID, sizeof(kTestOID), true /* don't return name */,
179 "1.2.840.113554.4.1.72585.0") ||
180 !ExpectObj2Txt(kTestOID, sizeof(kTestOID), false /* return name */,
181 "1.2.840.113554.4.1.72585.0")) {
182 return false;
183 }
184
David Benjamin2f8ea542016-08-04 17:21:36 -0400185 ASN1_OBJECT obj;
186 memset(&obj, 0, sizeof(obj));
187
188 if (OBJ_obj2txt(NULL, 0, &obj, 0) != -1) {
189 fprintf(stderr, "OBJ_obj2txt accepted the empty OID.\n");
190 return false;
191 }
192
193 // kNonMinimalOID is kBasicConstraints with the final component non-minimally
194 // encoded.
195 static const uint8_t kNonMinimalOID[] = {
196 0x55, 0x1d, 0x80, 0x13,
197 };
198 obj.data = kNonMinimalOID;
199 obj.length = sizeof(kNonMinimalOID);
200 if (OBJ_obj2txt(NULL, 0, &obj, 0) != -1) {
201 fprintf(stderr, "OBJ_obj2txt accepted non-minimal OIDs.\n");
202 return false;
203 }
204
205 // kOverflowOID is the DER representation of
206 // 1.2.840.113554.4.1.72585.18446744073709551616. (The final value is 2^64.)
207 static const uint8_t kOverflowOID[] = {
208 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09,
209 0x82, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00,
210 };
211 obj.data = kOverflowOID;
212 obj.length = sizeof(kOverflowOID);
213 if (OBJ_obj2txt(NULL, 0, &obj, 0) != -1) {
214 fprintf(stderr, "OBJ_obj2txt accepted an OID with a large component.\n");
215 return false;
216 }
217
218 // kInvalidOID is a mis-encoded version of kBasicConstraints with the final
219 // octet having the high bit set.
220 static const uint8_t kInvalidOID[] = {
221 0x55, 0x1d, 0x93,
222 };
223 obj.data = kInvalidOID;
224 obj.length = sizeof(kInvalidOID);
225 if (OBJ_obj2txt(NULL, 0, &obj, 0) != -1) {
226 fprintf(stderr, "OBJ_obj2txt accepted a mis-encoded OID.\n");
227 return false;
228 }
229
David Benjaminea655fa2016-08-04 16:45:15 -0400230 return true;
231}
232
David Benjamindc9194f2016-04-24 12:17:46 -0400233int main() {
234 CRYPTO_library_init();
235
236 if (!TestBasic() ||
David Benjaminea655fa2016-08-04 16:45:15 -0400237 !TestSignatureAlgorithms() ||
238 !TestObj2Txt()) {
David Benjamindc9194f2016-04-24 12:17:46 -0400239 return 1;
240 }
241
242 printf("PASS\n");
243 return 0;
244}