• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the License); you may
5  *  not use this file except in compliance with the License.
6  *
7  *  http://www.apache.org/licenses/LICENSE-2.0
8  */
9 
10 
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <gmssl/oid.h>
15 #include <gmssl/x509_oid.h>
16 #include <gmssl/x509.h>
17 #include <gmssl/rand.h>
18 #include <gmssl/error.h>
19 
20 
test_x509_name_type()21 static int test_x509_name_type()
22 {
23 	char *names[] = {
24 		"name",
25 		"surname",
26 		"givenName",
27 		"initials",
28 		"generationQualifier",
29 		"commonName",
30 		"localityName",
31 		"stateOrProvinceName",
32 		"organizationName",
33 		"organizationalUnitName",
34 		"title",
35 		"dnQualifier",
36 		"countryName",
37 		"serialNumber",
38 		"pseudonym",
39 		"domainComponent",
40 	};
41 	int oid;
42 	uint8_t buf[256];
43 	uint8_t *p = buf;
44 	const uint8_t *cp = buf;
45 	size_t len = 0;
46 	int i;
47 
48 	format_print(stderr, 0, 0, "DER\n");
49 	for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
50 		oid = x509_name_type_from_name(names[i]);
51 		if (asn1_check(oid != OID_undef) != 1
52 			|| x509_name_type_to_der(oid, &p, &len) != 1) {
53 			error_print();
54 			return -1;
55 		}
56 		format_bytes(stderr, 0, 4, "", buf, len);
57 	}
58 
59 	format_print(stderr, 0, 0, "OID\n");
60 	for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
61 		if (x509_name_type_from_der(&oid, &cp, &len) != 1) {
62 			error_print();
63 			return -1;
64 		}
65 		if (oid != x509_name_type_from_name(names[i])) {
66 			error_print();
67 			return -1;
68 		}
69 		format_print(stderr, 0, 4, "%s\n", x509_name_type_name(oid));
70 	}
71 	if (len != 0) {
72 		error_print();
73 		return -1;
74 	}
75 	printf("%s() ok\n", __FUNCTION__);
76 	return 1;
77 }
78 
test_x509_ext_id()79 static int test_x509_ext_id()
80 {
81 	char *names[] = {
82 		"AuthorityKeyIdentifier",
83 		"SubjectKeyIdentifier",
84 		"KeyUsage",
85 		"CertificatePolicies",
86 		"PolicyMappings",
87 		"SubjectAltName",
88 		"IssuerAltName",
89 		"SubjectDirectoryAttributes",
90 		"BasicConstraints",
91 		"NameConstraints",
92 		"PolicyConstraints",
93 		"ExtKeyUsage",
94 		"CRLDistributionPoints",
95 		"InhibitAnyPolicy",
96 		"FreshestCRL",
97 	};
98 	int oid;
99 	uint32_t nodes[32];
100 	size_t nodes_cnt;
101 	uint8_t buf[256];
102 	uint8_t *p = buf;
103 	const uint8_t *cp = buf;
104 	size_t len = 0;
105 	int i;
106 
107 	format_print(stderr, 0, 0, "DER\n");
108 	for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
109 		oid = x509_ext_id_from_name(names[i]);
110 		if (asn1_check(oid != OID_undef) != 1
111 			|| x509_ext_id_to_der(oid, &p, &len) != 1) {
112 			error_print();
113 			return -1;
114 		}
115 		format_bytes(stderr, 0, 4, "", buf, len);
116 	}
117 
118 	format_print(stderr, 0, 0, "ExtnID\n");
119 	for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
120 		if (x509_ext_id_from_der(&oid, nodes, &nodes_cnt, &cp, &len) != 1) {
121 			error_print();
122 			return -1;
123 		}
124 		if (oid != x509_ext_id_from_name(names[i])) {
125 			error_print();
126 			return -1;
127 		}
128 		format_print(stderr, 0, 4, "%s\n", x509_ext_id_name(oid));
129 	}
130 	if (len != 0) {
131 		error_print();
132 		return -1;
133 	}
134 	printf("%s() ok\n", __FUNCTION__);
135 	return 1;
136 }
137 
test_x509_qualifier_id(void)138 static int test_x509_qualifier_id(void)
139 {
140 	char *names[] = {
141 		"CPS",
142 		"userNotice",
143 	};
144 	int oid;
145 	uint8_t buf[256];
146 	uint8_t *p = buf;
147 	const uint8_t *cp = buf;
148 	size_t len = 0;
149 	int i;
150 
151 	format_print(stderr, 0, 0, "DER\n");
152 	for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
153 		oid = x509_qualifier_id_from_name(names[i]);
154 		if (asn1_check(oid != OID_undef) != 1
155 			|| x509_qualifier_id_to_der(oid, &p, &len) != 1) {
156 			error_print();
157 			return -1;
158 		}
159 		format_bytes(stderr, 0, 4, "", buf, len);
160 	}
161 
162 	format_print(stderr, 0, 0, "OID\n");
163 	for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
164 		if (x509_qualifier_id_from_der(&oid, &cp, &len) != 1) {
165 			error_print();
166 			return -1;
167 		}
168 		if (asn1_check(oid == x509_qualifier_id_from_name(names[i])) != 1) {
169 			error_print();
170 			return -1;
171 		}
172 		format_print(stderr, 0, 4, "%s\n", x509_qualifier_id_name(oid));
173 	}
174 	if (len != 0) {
175 		error_print();
176 		return -1;
177 	}
178 	printf("%s() ok\n", __FUNCTION__);
179 	return 1;
180 }
181 
test_x509_cert_policy_id(void)182 static int test_x509_cert_policy_id(void)
183 {
184 	char *names[] = {
185 		"anyPolicy",
186 	};
187 	int oid;
188 	uint32_t nodes[32];
189 	size_t nodes_cnt;
190 	uint8_t buf[256];
191 	uint8_t *p = buf;
192 	const uint8_t *cp = buf;
193 	size_t len = 0;
194 	int i;
195 
196 	format_print(stderr, 0, 0, "DER\n");
197 	for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
198 		oid = x509_cert_policy_id_from_name(names[i]);
199 		if (asn1_check(oid != OID_undef) != 1
200 			|| x509_cert_policy_id_to_der(oid, NULL, 0, &p, &len) != 1) {
201 			error_print();
202 			return -1;
203 		}
204 		format_bytes(stderr, 0, 4, "", buf, len);
205 	}
206 
207 	format_print(stderr, 0, 0, "OID\n");
208 	for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
209 		if (x509_cert_policy_id_from_der(&oid, nodes, &nodes_cnt, &cp, &len) != 1) {
210 			error_print();
211 			return -1;
212 		}
213 		if (oid != x509_cert_policy_id_from_name(names[i])) {
214 			error_print();
215 			return -1;
216 		}
217 		format_print(stderr, 0, 4, "%s\n", x509_cert_policy_id_name(oid));
218 	}
219 	if (len != 0) {
220 		error_print();
221 		return -1;
222 	}
223 	printf("%s() ok\n", __FUNCTION__);
224 	return 1;
225 }
226 
test_x509_key_purpose(void)227 static int test_x509_key_purpose(void)
228 {
229 	char *names[] = {
230 		"serverAuth",
231 		"clientAuth",
232 		"codeSigning",
233 		"emailProtection",
234 		"timeStamping",
235 		"OCSPSigning",
236 	};
237 	int oid;
238 	uint8_t buf[256];
239 	uint8_t *p = buf;
240 	const uint8_t *cp = buf;
241 	size_t len = 0;
242 	int i;
243 
244 	format_print(stderr, 0, 0, "DER\n");
245 	for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
246 		oid = x509_key_purpose_from_name(names[i]);
247 		if (asn1_check(oid != OID_undef) != 1
248 			|| x509_key_purpose_to_der(oid, &p, &len) != 1) {
249 			error_print();
250 			return -1;
251 		}
252 		format_bytes(stderr, 0, 4, "", buf, len);
253 	}
254 
255 	format_print(stderr, 0, 0, "OID\n");
256 	for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
257 		if (x509_key_purpose_from_der(&oid, &cp, &len) != 1) {
258 			error_print();
259 			return -1;
260 		}
261 		if (oid != x509_key_purpose_from_name(names[i])) {
262 			error_print();
263 			return -1;
264 		}
265 		format_print(stderr, 0, 4, "%s\n", x509_key_purpose_name(oid));
266 	}
267 	if (len != 0) {
268 		error_print();
269 		return -1;
270 	}
271 	printf("%s() ok\n", __FUNCTION__);
272 	return 1;
273 }
274 
main(void)275 int main(void)
276 {
277 	if (test_x509_name_type() != 1) goto err;
278 	if (test_x509_ext_id() != 1) goto err;
279 	if (test_x509_qualifier_id() != 1) goto err;
280 	if (test_x509_cert_policy_id() != 1) goto err;
281 	if (test_x509_key_purpose() != 1) goto err;
282 	printf("%s all tests passed\n", __FILE__);
283 	return 0;
284 err:
285 	error_print();
286 	return 1;
287 }
288