3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
61 #include "internal/cryptlib.h"
62 #include <openssl/conf.h>
63 #include <openssl/asn1.h>
64 #include <openssl/asn1t.h>
65 #include <openssl/x509v3.h>
70 /* Certificate policies extension support: this one is a bit complex... */
72 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
73 BIO *out, int indent);
74 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
75 X509V3_CTX *ctx, char *value);
76 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
78 static void print_notice(BIO *out, USERNOTICE *notice, int indent);
79 static POLICYINFO *policy_section(X509V3_CTX *ctx,
80 STACK_OF(CONF_VALUE) *polstrs, int ia5org);
81 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
82 STACK_OF(CONF_VALUE) *unot, int ia5org);
83 static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos);
85 const X509V3_EXT_METHOD v3_cpols = {
86 NID_certificate_policies, 0, ASN1_ITEM_ref(CERTIFICATEPOLICIES),
90 (X509V3_EXT_I2R)i2r_certpol,
91 (X509V3_EXT_R2I)r2i_certpol,
95 ASN1_ITEM_TEMPLATE(CERTIFICATEPOLICIES) =
96 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, CERTIFICATEPOLICIES, POLICYINFO)
97 ASN1_ITEM_TEMPLATE_END(CERTIFICATEPOLICIES)
99 IMPLEMENT_ASN1_FUNCTIONS(CERTIFICATEPOLICIES)
101 ASN1_SEQUENCE(POLICYINFO) = {
102 ASN1_SIMPLE(POLICYINFO, policyid, ASN1_OBJECT),
103 ASN1_SEQUENCE_OF_OPT(POLICYINFO, qualifiers, POLICYQUALINFO)
104 } ASN1_SEQUENCE_END(POLICYINFO)
106 IMPLEMENT_ASN1_FUNCTIONS(POLICYINFO)
108 ASN1_ADB_TEMPLATE(policydefault) = ASN1_SIMPLE(POLICYQUALINFO, d.other, ASN1_ANY);
110 ASN1_ADB(POLICYQUALINFO) = {
111 ADB_ENTRY(NID_id_qt_cps, ASN1_SIMPLE(POLICYQUALINFO, d.cpsuri, ASN1_IA5STRING)),
112 ADB_ENTRY(NID_id_qt_unotice, ASN1_SIMPLE(POLICYQUALINFO, d.usernotice, USERNOTICE))
113 } ASN1_ADB_END(POLICYQUALINFO, 0, pqualid, 0, &policydefault_tt, NULL);
115 ASN1_SEQUENCE(POLICYQUALINFO) = {
116 ASN1_SIMPLE(POLICYQUALINFO, pqualid, ASN1_OBJECT),
117 ASN1_ADB_OBJECT(POLICYQUALINFO)
118 } ASN1_SEQUENCE_END(POLICYQUALINFO)
120 IMPLEMENT_ASN1_FUNCTIONS(POLICYQUALINFO)
122 ASN1_SEQUENCE(USERNOTICE) = {
123 ASN1_OPT(USERNOTICE, noticeref, NOTICEREF),
124 ASN1_OPT(USERNOTICE, exptext, DISPLAYTEXT)
125 } ASN1_SEQUENCE_END(USERNOTICE)
127 IMPLEMENT_ASN1_FUNCTIONS(USERNOTICE)
129 ASN1_SEQUENCE(NOTICEREF) = {
130 ASN1_SIMPLE(NOTICEREF, organization, DISPLAYTEXT),
131 ASN1_SEQUENCE_OF(NOTICEREF, noticenos, ASN1_INTEGER)
132 } ASN1_SEQUENCE_END(NOTICEREF)
134 IMPLEMENT_ASN1_FUNCTIONS(NOTICEREF)
136 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
137 X509V3_CTX *ctx, char *value)
139 STACK_OF(POLICYINFO) *pols = NULL;
143 STACK_OF(CONF_VALUE) *vals;
146 pols = sk_POLICYINFO_new_null();
148 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
151 vals = X509V3_parse_list(value);
153 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_X509V3_LIB);
157 for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
158 cnf = sk_CONF_VALUE_value(vals, i);
159 if (cnf->value || !cnf->name) {
160 X509V3err(X509V3_F_R2I_CERTPOL,
161 X509V3_R_INVALID_POLICY_IDENTIFIER);
162 X509V3_conf_err(cnf);
166 if (strcmp(pstr, "ia5org") == 0) {
169 } else if (*pstr == '@') {
170 STACK_OF(CONF_VALUE) *polsect;
171 polsect = X509V3_get_section(ctx, pstr + 1);
173 X509V3err(X509V3_F_R2I_CERTPOL, X509V3_R_INVALID_SECTION);
175 X509V3_conf_err(cnf);
178 pol = policy_section(ctx, polsect, ia5org);
179 X509V3_section_free(ctx, polsect);
183 if ((pobj = OBJ_txt2obj(cnf->name, 0)) == NULL) {
184 X509V3err(X509V3_F_R2I_CERTPOL,
185 X509V3_R_INVALID_OBJECT_IDENTIFIER);
186 X509V3_conf_err(cnf);
189 pol = POLICYINFO_new();
191 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
194 pol->policyid = pobj;
196 if (!sk_POLICYINFO_push(pols, pol)) {
197 POLICYINFO_free(pol);
198 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
202 sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
205 sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
206 sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
210 static POLICYINFO *policy_section(X509V3_CTX *ctx,
211 STACK_OF(CONF_VALUE) *polstrs, int ia5org)
216 POLICYQUALINFO *qual;
218 if ((pol = POLICYINFO_new()) == NULL)
220 for (i = 0; i < sk_CONF_VALUE_num(polstrs); i++) {
221 cnf = sk_CONF_VALUE_value(polstrs, i);
222 if (strcmp(cnf->name, "policyIdentifier") == 0) {
224 if ((pobj = OBJ_txt2obj(cnf->value, 0)) == NULL) {
225 X509V3err(X509V3_F_POLICY_SECTION,
226 X509V3_R_INVALID_OBJECT_IDENTIFIER);
227 X509V3_conf_err(cnf);
230 pol->policyid = pobj;
232 } else if (!name_cmp(cnf->name, "CPS")) {
233 if (pol->qualifiers == NULL)
234 pol->qualifiers = sk_POLICYQUALINFO_new_null();
235 if ((qual = POLICYQUALINFO_new()) == NULL)
237 if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
239 if ((qual->pqualid = OBJ_nid2obj(NID_id_qt_cps)) == NULL) {
240 X509V3err(X509V3_F_POLICY_SECTION, ERR_R_INTERNAL_ERROR);
243 if ((qual->d.cpsuri = ASN1_IA5STRING_new()) == NULL)
245 if (!ASN1_STRING_set(qual->d.cpsuri, cnf->value,
248 } else if (!name_cmp(cnf->name, "userNotice")) {
249 STACK_OF(CONF_VALUE) *unot;
250 if (*cnf->value != '@') {
251 X509V3err(X509V3_F_POLICY_SECTION,
252 X509V3_R_EXPECTED_A_SECTION_NAME);
253 X509V3_conf_err(cnf);
256 unot = X509V3_get_section(ctx, cnf->value + 1);
258 X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_SECTION);
260 X509V3_conf_err(cnf);
263 qual = notice_section(ctx, unot, ia5org);
264 X509V3_section_free(ctx, unot);
267 if (!pol->qualifiers)
268 pol->qualifiers = sk_POLICYQUALINFO_new_null();
269 if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
272 X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_OPTION);
274 X509V3_conf_err(cnf);
278 if (!pol->policyid) {
279 X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_NO_POLICY_IDENTIFIER);
286 X509V3err(X509V3_F_POLICY_SECTION, ERR_R_MALLOC_FAILURE);
289 POLICYINFO_free(pol);
294 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
295 STACK_OF(CONF_VALUE) *unot, int ia5org)
300 POLICYQUALINFO *qual;
302 if ((qual = POLICYQUALINFO_new()) == NULL)
304 if ((qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice)) == NULL) {
305 X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_INTERNAL_ERROR);
308 if ((not = USERNOTICE_new()) == NULL)
310 qual->d.usernotice = not;
311 for (i = 0; i < sk_CONF_VALUE_num(unot); i++) {
312 cnf = sk_CONF_VALUE_value(unot, i);
313 if (strcmp(cnf->name, "explicitText") == 0) {
314 if ((not->exptext = ASN1_VISIBLESTRING_new()) == NULL)
316 if (!ASN1_STRING_set(not->exptext, cnf->value,
319 } else if (strcmp(cnf->name, "organization") == 0) {
321 if (!not->noticeref) {
322 if ((nref = NOTICEREF_new()) == NULL)
324 not->noticeref = nref;
326 nref = not->noticeref;
328 nref->organization->type = V_ASN1_IA5STRING;
330 nref->organization->type = V_ASN1_VISIBLESTRING;
331 if (!ASN1_STRING_set(nref->organization, cnf->value,
334 } else if (strcmp(cnf->name, "noticeNumbers") == 0) {
336 STACK_OF(CONF_VALUE) *nos;
337 if (!not->noticeref) {
338 if ((nref = NOTICEREF_new()) == NULL)
340 not->noticeref = nref;
342 nref = not->noticeref;
343 nos = X509V3_parse_list(cnf->value);
344 if (!nos || !sk_CONF_VALUE_num(nos)) {
345 X509V3err(X509V3_F_NOTICE_SECTION, X509V3_R_INVALID_NUMBERS);
346 X509V3_conf_err(cnf);
349 ret = nref_nos(nref->noticenos, nos);
350 sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
354 X509V3err(X509V3_F_NOTICE_SECTION, X509V3_R_INVALID_OPTION);
355 X509V3_conf_err(cnf);
360 if (not->noticeref &&
361 (!not->noticeref->noticenos || !not->noticeref->organization)) {
362 X509V3err(X509V3_F_NOTICE_SECTION,
363 X509V3_R_NEED_ORGANIZATION_AND_NUMBERS);
370 X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_MALLOC_FAILURE);
373 POLICYQUALINFO_free(qual);
377 static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos)
384 for (i = 0; i < sk_CONF_VALUE_num(nos); i++) {
385 cnf = sk_CONF_VALUE_value(nos, i);
386 if ((aint = s2i_ASN1_INTEGER(NULL, cnf->name)) == NULL) {
387 X509V3err(X509V3_F_NREF_NOS, X509V3_R_INVALID_NUMBER);
390 if (!sk_ASN1_INTEGER_push(nnums, aint))
396 X509V3err(X509V3_F_NREF_NOS, ERR_R_MALLOC_FAILURE);
399 sk_ASN1_INTEGER_pop_free(nnums, ASN1_STRING_free);
403 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
404 BIO *out, int indent)
408 /* First print out the policy OIDs */
409 for (i = 0; i < sk_POLICYINFO_num(pol); i++) {
410 pinfo = sk_POLICYINFO_value(pol, i);
411 BIO_printf(out, "%*sPolicy: ", indent, "");
412 i2a_ASN1_OBJECT(out, pinfo->policyid);
414 if (pinfo->qualifiers)
415 print_qualifiers(out, pinfo->qualifiers, indent + 2);
420 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
423 POLICYQUALINFO *qualinfo;
425 for (i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
426 qualinfo = sk_POLICYQUALINFO_value(quals, i);
427 switch (OBJ_obj2nid(qualinfo->pqualid)) {
429 BIO_printf(out, "%*sCPS: %s\n", indent, "",
430 qualinfo->d.cpsuri->data);
433 case NID_id_qt_unotice:
434 BIO_printf(out, "%*sUser Notice:\n", indent, "");
435 print_notice(out, qualinfo->d.usernotice, indent + 2);
439 BIO_printf(out, "%*sUnknown Qualifier: ", indent + 2, "");
441 i2a_ASN1_OBJECT(out, qualinfo->pqualid);
448 static void print_notice(BIO *out, USERNOTICE *notice, int indent)
451 if (notice->noticeref) {
453 ref = notice->noticeref;
454 BIO_printf(out, "%*sOrganization: %s\n", indent, "",
455 ref->organization->data);
456 BIO_printf(out, "%*sNumber%s: ", indent, "",
457 sk_ASN1_INTEGER_num(ref->noticenos) > 1 ? "s" : "");
458 for (i = 0; i < sk_ASN1_INTEGER_num(ref->noticenos); i++) {
461 num = sk_ASN1_INTEGER_value(ref->noticenos, i);
464 tmp = i2s_ASN1_INTEGER(NULL, num);
471 BIO_printf(out, "%*sExplicit Text: %s\n", indent, "",
472 notice->exptext->data);
475 void X509_POLICY_NODE_print(BIO *out, X509_POLICY_NODE *node, int indent)
477 const X509_POLICY_DATA *dat = node->data;
479 BIO_printf(out, "%*sPolicy: ", indent, "");
481 i2a_ASN1_OBJECT(out, dat->valid_policy);
483 BIO_printf(out, "%*s%s\n", indent + 2, "",
484 node_data_critical(dat) ? "Critical" : "Non Critical");
485 if (dat->qualifier_set)
486 print_qualifiers(out, dat->qualifier_set, indent + 2);
488 BIO_printf(out, "%*sNo Qualifiers\n", indent + 2, "");