1 /*
2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
3 * 1999.
4 */
5 /* ====================================================================
6 * Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59 #include <stdio.h>
60 #include <string.h>
61
62 #include <openssl/asn1.h>
63 #include <openssl/asn1t.h>
64 #include <openssl/conf.h>
65 #include <openssl/err.h>
66 #include <openssl/mem.h>
67 #include <openssl/obj.h>
68 #include <openssl/stack.h>
69 #include <openssl/x509v3.h>
70
71 #include "internal.h"
72
73 // Certificate policies extension support: this one is a bit complex...
74
75 static int i2r_certpol(const X509V3_EXT_METHOD *method, void *ext, BIO *out,
76 int indent);
77 static void *r2i_certpol(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx,
78 const char *value);
79 static void print_qualifiers(BIO *out, const STACK_OF(POLICYQUALINFO) *quals,
80 int indent);
81 static void print_notice(BIO *out, const USERNOTICE *notice, int indent);
82 static POLICYINFO *policy_section(const X509V3_CTX *ctx,
83 const STACK_OF(CONF_VALUE) *polstrs,
84 int ia5org);
85 static POLICYQUALINFO *notice_section(const X509V3_CTX *ctx,
86 const STACK_OF(CONF_VALUE) *unot,
87 int ia5org);
88 static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums,
89 const STACK_OF(CONF_VALUE) *nos);
90
91 const X509V3_EXT_METHOD v3_cpols = {
92 NID_certificate_policies,
93 0,
94 ASN1_ITEM_ref(CERTIFICATEPOLICIES),
95 0,
96 0,
97 0,
98 0,
99 0,
100 0,
101 0,
102 0,
103 i2r_certpol,
104 r2i_certpol,
105 NULL,
106 };
107
108 ASN1_ITEM_TEMPLATE(CERTIFICATEPOLICIES) = ASN1_EX_TEMPLATE_TYPE(
109 ASN1_TFLG_SEQUENCE_OF, 0, CERTIFICATEPOLICIES, POLICYINFO)
110 ASN1_ITEM_TEMPLATE_END(CERTIFICATEPOLICIES)
111
112 IMPLEMENT_ASN1_FUNCTIONS_const(CERTIFICATEPOLICIES)
113
114 ASN1_SEQUENCE(POLICYINFO) = {
115 ASN1_SIMPLE(POLICYINFO, policyid, ASN1_OBJECT),
116 ASN1_SEQUENCE_OF_OPT(POLICYINFO, qualifiers, POLICYQUALINFO),
117 } ASN1_SEQUENCE_END(POLICYINFO)
118
119 IMPLEMENT_ASN1_FUNCTIONS_const(POLICYINFO)
120
121 ASN1_ADB_TEMPLATE(policydefault) = ASN1_SIMPLE(POLICYQUALINFO, d.other,
122 ASN1_ANY);
123
124 ASN1_ADB(POLICYQUALINFO) = {
125 ADB_ENTRY(NID_id_qt_cps,
126 ASN1_SIMPLE(POLICYQUALINFO, d.cpsuri, ASN1_IA5STRING)),
127 ADB_ENTRY(NID_id_qt_unotice,
128 ASN1_SIMPLE(POLICYQUALINFO, d.usernotice, USERNOTICE)),
129 } ASN1_ADB_END(POLICYQUALINFO, 0, pqualid, 0, &policydefault_tt, NULL);
130
131 ASN1_SEQUENCE(POLICYQUALINFO) = {
132 ASN1_SIMPLE(POLICYQUALINFO, pqualid, ASN1_OBJECT),
133 ASN1_ADB_OBJECT(POLICYQUALINFO),
134 } ASN1_SEQUENCE_END(POLICYQUALINFO)
135
136 IMPLEMENT_ASN1_FUNCTIONS_const(POLICYQUALINFO)
137
138 ASN1_SEQUENCE(USERNOTICE) = {
139 ASN1_OPT(USERNOTICE, noticeref, NOTICEREF),
140 ASN1_OPT(USERNOTICE, exptext, DISPLAYTEXT),
141 } ASN1_SEQUENCE_END(USERNOTICE)
142
143 IMPLEMENT_ASN1_FUNCTIONS_const(USERNOTICE)
144
145 ASN1_SEQUENCE(NOTICEREF) = {
146 ASN1_SIMPLE(NOTICEREF, organization, DISPLAYTEXT),
147 ASN1_SEQUENCE_OF(NOTICEREF, noticenos, ASN1_INTEGER),
148 } ASN1_SEQUENCE_END(NOTICEREF)
149
150 IMPLEMENT_ASN1_FUNCTIONS_const(NOTICEREF)
151
152 static void *r2i_certpol(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx,
153 const char *value) {
154 STACK_OF(POLICYINFO) *pols = sk_POLICYINFO_new_null();
155 if (pols == NULL) {
156 return NULL;
157 }
158 STACK_OF(CONF_VALUE) *vals = X509V3_parse_list(value);
159 if (vals == NULL) {
160 OPENSSL_PUT_ERROR(X509V3, ERR_R_X509V3_LIB);
161 goto err;
162 }
163 int ia5org = 0;
164 for (size_t i = 0; i < sk_CONF_VALUE_num(vals); i++) {
165 const CONF_VALUE *cnf = sk_CONF_VALUE_value(vals, i);
166 if (cnf->value || !cnf->name) {
167 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_POLICY_IDENTIFIER);
168 X509V3_conf_err(cnf);
169 goto err;
170 }
171 POLICYINFO *pol;
172 const char *pstr = cnf->name;
173 if (!strcmp(pstr, "ia5org")) {
174 ia5org = 1;
175 continue;
176 } else if (*pstr == '@') {
177 const STACK_OF(CONF_VALUE) *polsect = X509V3_get_section(ctx, pstr + 1);
178 if (!polsect) {
179 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_SECTION);
180
181 X509V3_conf_err(cnf);
182 goto err;
183 }
184 pol = policy_section(ctx, polsect, ia5org);
185 if (!pol) {
186 goto err;
187 }
188 } else {
189 ASN1_OBJECT *pobj = OBJ_txt2obj(cnf->name, 0);
190 if (pobj == NULL) {
191 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_OBJECT_IDENTIFIER);
192 X509V3_conf_err(cnf);
193 goto err;
194 }
195 pol = POLICYINFO_new();
196 if (pol == NULL) {
197 ASN1_OBJECT_free(pobj);
198 goto err;
199 }
200 pol->policyid = pobj;
201 }
202 if (!sk_POLICYINFO_push(pols, pol)) {
203 POLICYINFO_free(pol);
204 goto err;
205 }
206 }
207 sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
208 return pols;
209 err:
210 sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
211 sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
212 return NULL;
213 }
214
policy_section(const X509V3_CTX * ctx,const STACK_OF (CONF_VALUE)* polstrs,int ia5org)215 static POLICYINFO *policy_section(const X509V3_CTX *ctx,
216 const STACK_OF(CONF_VALUE) *polstrs,
217 int ia5org) {
218 POLICYINFO *pol;
219 POLICYQUALINFO *qual;
220 if (!(pol = POLICYINFO_new())) {
221 goto err;
222 }
223 for (size_t i = 0; i < sk_CONF_VALUE_num(polstrs); i++) {
224 const CONF_VALUE *cnf = sk_CONF_VALUE_value(polstrs, i);
225 if (!strcmp(cnf->name, "policyIdentifier")) {
226 ASN1_OBJECT *pobj;
227 if (!(pobj = OBJ_txt2obj(cnf->value, 0))) {
228 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_OBJECT_IDENTIFIER);
229 X509V3_conf_err(cnf);
230 goto err;
231 }
232 pol->policyid = pobj;
233
234 } else if (x509v3_conf_name_matches(cnf->name, "CPS")) {
235 if (!pol->qualifiers) {
236 pol->qualifiers = sk_POLICYQUALINFO_new_null();
237 }
238 if (!(qual = POLICYQUALINFO_new())) {
239 goto err;
240 }
241 if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual)) {
242 goto err;
243 }
244 qual->pqualid = OBJ_nid2obj(NID_id_qt_cps);
245 if (qual->pqualid == NULL) {
246 OPENSSL_PUT_ERROR(X509V3, ERR_R_INTERNAL_ERROR);
247 goto err;
248 }
249 qual->d.cpsuri = ASN1_IA5STRING_new();
250 if (qual->d.cpsuri == NULL) {
251 goto err;
252 }
253 if (!ASN1_STRING_set(qual->d.cpsuri, cnf->value, strlen(cnf->value))) {
254 goto err;
255 }
256 } else if (x509v3_conf_name_matches(cnf->name, "userNotice")) {
257 if (*cnf->value != '@') {
258 OPENSSL_PUT_ERROR(X509V3, X509V3_R_EXPECTED_A_SECTION_NAME);
259 X509V3_conf_err(cnf);
260 goto err;
261 }
262 const STACK_OF(CONF_VALUE) *unot =
263 X509V3_get_section(ctx, cnf->value + 1);
264 if (!unot) {
265 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_SECTION);
266 X509V3_conf_err(cnf);
267 goto err;
268 }
269 qual = notice_section(ctx, unot, ia5org);
270 if (!qual) {
271 goto err;
272 }
273 if (!pol->qualifiers) {
274 pol->qualifiers = sk_POLICYQUALINFO_new_null();
275 }
276 if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual)) {
277 goto err;
278 }
279 } else {
280 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_OPTION);
281
282 X509V3_conf_err(cnf);
283 goto err;
284 }
285 }
286 if (!pol->policyid) {
287 OPENSSL_PUT_ERROR(X509V3, X509V3_R_NO_POLICY_IDENTIFIER);
288 goto err;
289 }
290
291 return pol;
292
293 err:
294 POLICYINFO_free(pol);
295 return NULL;
296 }
297
notice_section(const X509V3_CTX * ctx,const STACK_OF (CONF_VALUE)* unot,int ia5org)298 static POLICYQUALINFO *notice_section(const X509V3_CTX *ctx,
299 const STACK_OF(CONF_VALUE) *unot,
300 int ia5org) {
301 USERNOTICE *notice;
302 POLICYQUALINFO *qual;
303 if (!(qual = POLICYQUALINFO_new())) {
304 goto err;
305 }
306 qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice);
307 if (qual->pqualid == NULL) {
308 OPENSSL_PUT_ERROR(X509V3, ERR_R_INTERNAL_ERROR);
309 goto err;
310 }
311 if (!(notice = USERNOTICE_new())) {
312 goto err;
313 }
314 qual->d.usernotice = notice;
315 for (size_t i = 0; i < sk_CONF_VALUE_num(unot); i++) {
316 const CONF_VALUE *cnf = sk_CONF_VALUE_value(unot, i);
317 if (!strcmp(cnf->name, "explicitText")) {
318 notice->exptext = ASN1_VISIBLESTRING_new();
319 if (notice->exptext == NULL) {
320 goto err;
321 }
322 if (!ASN1_STRING_set(notice->exptext, cnf->value, strlen(cnf->value))) {
323 goto err;
324 }
325 } else if (!strcmp(cnf->name, "organization")) {
326 NOTICEREF *nref;
327 if (!notice->noticeref) {
328 if (!(nref = NOTICEREF_new())) {
329 goto err;
330 }
331 notice->noticeref = nref;
332 } else {
333 nref = notice->noticeref;
334 }
335 if (ia5org) {
336 nref->organization->type = V_ASN1_IA5STRING;
337 } else {
338 nref->organization->type = V_ASN1_VISIBLESTRING;
339 }
340 if (!ASN1_STRING_set(nref->organization, cnf->value,
341 strlen(cnf->value))) {
342 goto err;
343 }
344 } else if (!strcmp(cnf->name, "noticeNumbers")) {
345 NOTICEREF *nref;
346 STACK_OF(CONF_VALUE) *nos;
347 if (!notice->noticeref) {
348 if (!(nref = NOTICEREF_new())) {
349 goto err;
350 }
351 notice->noticeref = nref;
352 } else {
353 nref = notice->noticeref;
354 }
355 nos = X509V3_parse_list(cnf->value);
356 if (!nos || !sk_CONF_VALUE_num(nos)) {
357 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NUMBERS);
358 X509V3_conf_err(cnf);
359 sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
360 goto err;
361 }
362 int ret = nref_nos(nref->noticenos, nos);
363 sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
364 if (!ret) {
365 goto err;
366 }
367 } else {
368 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_OPTION);
369 X509V3_conf_err(cnf);
370 goto err;
371 }
372 }
373
374 if (notice->noticeref &&
375 (!notice->noticeref->noticenos || !notice->noticeref->organization)) {
376 OPENSSL_PUT_ERROR(X509V3, X509V3_R_NEED_ORGANIZATION_AND_NUMBERS);
377 goto err;
378 }
379
380 return qual;
381
382 err:
383 POLICYQUALINFO_free(qual);
384 return NULL;
385 }
386
nref_nos(STACK_OF (ASN1_INTEGER)* nnums,const STACK_OF (CONF_VALUE)* nos)387 static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums,
388 const STACK_OF(CONF_VALUE) *nos) {
389 for (size_t i = 0; i < sk_CONF_VALUE_num(nos); i++) {
390 const CONF_VALUE *cnf = sk_CONF_VALUE_value(nos, i);
391 ASN1_INTEGER *aint = s2i_ASN1_INTEGER(NULL, cnf->name);
392 if (aint == NULL) {
393 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NUMBER);
394 return 0;
395 }
396 if (!sk_ASN1_INTEGER_push(nnums, aint)) {
397 ASN1_INTEGER_free(aint);
398 return 0;
399 }
400 }
401 return 1;
402 }
403
i2r_certpol(const X509V3_EXT_METHOD * method,void * ext,BIO * out,int indent)404 static int i2r_certpol(const X509V3_EXT_METHOD *method, void *ext, BIO *out,
405 int indent) {
406 const STACK_OF(POLICYINFO) *pol = ext;
407 // First print out the policy OIDs
408 for (size_t i = 0; i < sk_POLICYINFO_num(pol); i++) {
409 const POLICYINFO *pinfo = sk_POLICYINFO_value(pol, i);
410 BIO_printf(out, "%*sPolicy: ", indent, "");
411 i2a_ASN1_OBJECT(out, pinfo->policyid);
412 BIO_puts(out, "\n");
413 if (pinfo->qualifiers) {
414 print_qualifiers(out, pinfo->qualifiers, indent + 2);
415 }
416 }
417 return 1;
418 }
419
print_qualifiers(BIO * out,const STACK_OF (POLICYQUALINFO)* quals,int indent)420 static void print_qualifiers(BIO *out, const STACK_OF(POLICYQUALINFO) *quals,
421 int indent) {
422 for (size_t i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
423 const POLICYQUALINFO *qualinfo = sk_POLICYQUALINFO_value(quals, i);
424 switch (OBJ_obj2nid(qualinfo->pqualid)) {
425 case NID_id_qt_cps:
426 BIO_printf(out, "%*sCPS: %.*s\n", indent, "",
427 qualinfo->d.cpsuri->length, qualinfo->d.cpsuri->data);
428 break;
429
430 case NID_id_qt_unotice:
431 BIO_printf(out, "%*sUser Notice:\n", indent, "");
432 print_notice(out, qualinfo->d.usernotice, indent + 2);
433 break;
434
435 default:
436 BIO_printf(out, "%*sUnknown Qualifier: ", indent + 2, "");
437
438 i2a_ASN1_OBJECT(out, qualinfo->pqualid);
439 BIO_puts(out, "\n");
440 break;
441 }
442 }
443 }
444
print_notice(BIO * out,const USERNOTICE * notice,int indent)445 static void print_notice(BIO *out, const USERNOTICE *notice, int indent) {
446 if (notice->noticeref) {
447 NOTICEREF *ref;
448 ref = notice->noticeref;
449 BIO_printf(out, "%*sOrganization: %.*s\n", indent, "",
450 ref->organization->length, ref->organization->data);
451 BIO_printf(out, "%*sNumber%s: ", indent, "",
452 sk_ASN1_INTEGER_num(ref->noticenos) > 1 ? "s" : "");
453 for (size_t i = 0; i < sk_ASN1_INTEGER_num(ref->noticenos); i++) {
454 ASN1_INTEGER *num;
455 char *tmp;
456 num = sk_ASN1_INTEGER_value(ref->noticenos, i);
457 if (i) {
458 BIO_puts(out, ", ");
459 }
460 if (num == NULL) {
461 BIO_puts(out, "(null)");
462 } else {
463 tmp = i2s_ASN1_INTEGER(NULL, num);
464 if (tmp == NULL) {
465 return;
466 }
467 BIO_puts(out, tmp);
468 OPENSSL_free(tmp);
469 }
470 }
471 BIO_puts(out, "\n");
472 }
473 if (notice->exptext) {
474 BIO_printf(out, "%*sExplicit Text: %.*s\n", indent, "",
475 notice->exptext->length, notice->exptext->data);
476 }
477 }
478