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).
62 #include <openssl/conf.h>
63 #include <openssl/asn1.h>
64 #include <openssl/asn1t.h>
65 #include <openssl/x509v3.h>
69 /* Certificate policies extension support: this one is a bit complex... */
71 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
72 BIO *out, int indent);
73 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
74 X509V3_CTX *ctx, char *value);
75 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
77 static void print_notice(BIO *out, USERNOTICE *notice, int indent);
78 static POLICYINFO *policy_section(X509V3_CTX *ctx,
79 STACK_OF(CONF_VALUE) *polstrs, int ia5org);
80 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
81 STACK_OF(CONF_VALUE) *unot, int ia5org);
82 static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos);
84 const X509V3_EXT_METHOD v3_cpols = {
85 NID_certificate_policies, 0, ASN1_ITEM_ref(CERTIFICATEPOLICIES),
89 (X509V3_EXT_I2R)i2r_certpol,
90 (X509V3_EXT_R2I)r2i_certpol,
94 ASN1_ITEM_TEMPLATE(CERTIFICATEPOLICIES) =
95 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, CERTIFICATEPOLICIES, POLICYINFO)
96 ASN1_ITEM_TEMPLATE_END(CERTIFICATEPOLICIES)
98 IMPLEMENT_ASN1_FUNCTIONS(CERTIFICATEPOLICIES)
100 ASN1_SEQUENCE(POLICYINFO) = {
101 ASN1_SIMPLE(POLICYINFO, policyid, ASN1_OBJECT),
102 ASN1_SEQUENCE_OF_OPT(POLICYINFO, qualifiers, POLICYQUALINFO)
103 } ASN1_SEQUENCE_END(POLICYINFO)
105 IMPLEMENT_ASN1_FUNCTIONS(POLICYINFO)
107 ASN1_ADB_TEMPLATE(policydefault) = ASN1_SIMPLE(POLICYQUALINFO, d.other, ASN1_ANY);
109 ASN1_ADB(POLICYQUALINFO) = {
110 ADB_ENTRY(NID_id_qt_cps, ASN1_SIMPLE(POLICYQUALINFO, d.cpsuri, ASN1_IA5STRING)),
111 ADB_ENTRY(NID_id_qt_unotice, ASN1_SIMPLE(POLICYQUALINFO, d.usernotice, USERNOTICE))
112 } ASN1_ADB_END(POLICYQUALINFO, 0, pqualid, 0, &policydefault_tt, NULL);
114 ASN1_SEQUENCE(POLICYQUALINFO) = {
115 ASN1_SIMPLE(POLICYQUALINFO, pqualid, ASN1_OBJECT),
116 ASN1_ADB_OBJECT(POLICYQUALINFO)
117 } ASN1_SEQUENCE_END(POLICYQUALINFO)
119 IMPLEMENT_ASN1_FUNCTIONS(POLICYQUALINFO)
121 ASN1_SEQUENCE(USERNOTICE) = {
122 ASN1_OPT(USERNOTICE, noticeref, NOTICEREF),
123 ASN1_OPT(USERNOTICE, exptext, DISPLAYTEXT)
124 } ASN1_SEQUENCE_END(USERNOTICE)
126 IMPLEMENT_ASN1_FUNCTIONS(USERNOTICE)
128 ASN1_SEQUENCE(NOTICEREF) = {
129 ASN1_SIMPLE(NOTICEREF, organization, DISPLAYTEXT),
130 ASN1_SEQUENCE_OF(NOTICEREF, noticenos, ASN1_INTEGER)
131 } ASN1_SEQUENCE_END(NOTICEREF)
133 IMPLEMENT_ASN1_FUNCTIONS(NOTICEREF)
135 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
136 X509V3_CTX *ctx, char *value)
138 STACK_OF(POLICYINFO) *pols = NULL;
142 STACK_OF(CONF_VALUE) *vals;
145 pols = sk_POLICYINFO_new_null();
147 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
150 vals = X509V3_parse_list(value);
152 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_X509V3_LIB);
156 for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
157 cnf = sk_CONF_VALUE_value(vals, i);
158 if (cnf->value || !cnf->name) {
159 X509V3err(X509V3_F_R2I_CERTPOL,
160 X509V3_R_INVALID_POLICY_IDENTIFIER);
161 X509V3_conf_err(cnf);
165 if (!strcmp(pstr, "ia5org")) {
168 } else if (*pstr == '@') {
169 STACK_OF(CONF_VALUE) *polsect;
170 polsect = X509V3_get_section(ctx, pstr + 1);
172 X509V3err(X509V3_F_R2I_CERTPOL, X509V3_R_INVALID_SECTION);
174 X509V3_conf_err(cnf);
177 pol = policy_section(ctx, polsect, ia5org);
178 X509V3_section_free(ctx, polsect);
182 if (!(pobj = OBJ_txt2obj(cnf->name, 0))) {
183 X509V3err(X509V3_F_R2I_CERTPOL,
184 X509V3_R_INVALID_OBJECT_IDENTIFIER);
185 X509V3_conf_err(cnf);
188 pol = POLICYINFO_new();
189 pol->policyid = pobj;
191 if (!sk_POLICYINFO_push(pols, pol)) {
192 POLICYINFO_free(pol);
193 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
197 sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
200 sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
201 sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
205 static POLICYINFO *policy_section(X509V3_CTX *ctx,
206 STACK_OF(CONF_VALUE) *polstrs, int ia5org)
211 POLICYQUALINFO *qual;
212 if (!(pol = POLICYINFO_new()))
214 for (i = 0; i < sk_CONF_VALUE_num(polstrs); i++) {
215 cnf = sk_CONF_VALUE_value(polstrs, i);
216 if (!strcmp(cnf->name, "policyIdentifier")) {
218 if (!(pobj = OBJ_txt2obj(cnf->value, 0))) {
219 X509V3err(X509V3_F_POLICY_SECTION,
220 X509V3_R_INVALID_OBJECT_IDENTIFIER);
221 X509V3_conf_err(cnf);
224 pol->policyid = pobj;
226 } else if (!name_cmp(cnf->name, "CPS")) {
227 if (!pol->qualifiers)
228 pol->qualifiers = sk_POLICYQUALINFO_new_null();
229 if (!(qual = POLICYQUALINFO_new()))
231 if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
233 if(!(qual->pqualid = OBJ_nid2obj(NID_id_qt_cps))) {
234 X509V3err(X509V3_F_POLICY_SECTION, ERR_R_INTERNAL_ERROR);
237 if(!(qual->d.cpsuri = M_ASN1_IA5STRING_new()))
239 if (!ASN1_STRING_set(qual->d.cpsuri, cnf->value,
242 } else if (!name_cmp(cnf->name, "userNotice")) {
243 STACK_OF(CONF_VALUE) *unot;
244 if (*cnf->value != '@') {
245 X509V3err(X509V3_F_POLICY_SECTION,
246 X509V3_R_EXPECTED_A_SECTION_NAME);
247 X509V3_conf_err(cnf);
250 unot = X509V3_get_section(ctx, cnf->value + 1);
252 X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_SECTION);
254 X509V3_conf_err(cnf);
257 qual = notice_section(ctx, unot, ia5org);
258 X509V3_section_free(ctx, unot);
261 if (!pol->qualifiers)
262 pol->qualifiers = sk_POLICYQUALINFO_new_null();
263 if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
266 X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_OPTION);
268 X509V3_conf_err(cnf);
272 if (!pol->policyid) {
273 X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_NO_POLICY_IDENTIFIER);
280 X509V3err(X509V3_F_POLICY_SECTION, ERR_R_MALLOC_FAILURE);
283 POLICYINFO_free(pol);
288 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
289 STACK_OF(CONF_VALUE) *unot, int ia5org)
294 POLICYQUALINFO *qual;
295 if (!(qual = POLICYQUALINFO_new()))
297 if(!(qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice))) {
298 X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_INTERNAL_ERROR);
301 if (!(not = USERNOTICE_new()))
303 qual->d.usernotice = not;
304 for (i = 0; i < sk_CONF_VALUE_num(unot); i++) {
305 cnf = sk_CONF_VALUE_value(unot, i);
306 if (!strcmp(cnf->name, "explicitText")) {
307 if(!(not->exptext = M_ASN1_VISIBLESTRING_new()))
309 if (!ASN1_STRING_set(not->exptext, cnf->value,
312 } else if (!strcmp(cnf->name, "organization")) {
314 if (!not->noticeref) {
315 if (!(nref = NOTICEREF_new()))
317 not->noticeref = nref;
319 nref = not->noticeref;
321 nref->organization->type = V_ASN1_IA5STRING;
323 nref->organization->type = V_ASN1_VISIBLESTRING;
324 if (!ASN1_STRING_set(nref->organization, cnf->value,
327 } else if (!strcmp(cnf->name, "noticeNumbers")) {
329 STACK_OF(CONF_VALUE) *nos;
330 if (!not->noticeref) {
331 if (!(nref = NOTICEREF_new()))
333 not->noticeref = nref;
335 nref = not->noticeref;
336 nos = X509V3_parse_list(cnf->value);
337 if (!nos || !sk_CONF_VALUE_num(nos)) {
338 X509V3err(X509V3_F_NOTICE_SECTION, X509V3_R_INVALID_NUMBERS);
339 X509V3_conf_err(cnf);
342 ret = nref_nos(nref->noticenos, nos);
343 sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
347 X509V3err(X509V3_F_NOTICE_SECTION, X509V3_R_INVALID_OPTION);
348 X509V3_conf_err(cnf);
353 if (not->noticeref &&
354 (!not->noticeref->noticenos || !not->noticeref->organization)) {
355 X509V3err(X509V3_F_NOTICE_SECTION,
356 X509V3_R_NEED_ORGANIZATION_AND_NUMBERS);
363 X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_MALLOC_FAILURE);
366 POLICYQUALINFO_free(qual);
370 static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos)
377 for (i = 0; i < sk_CONF_VALUE_num(nos); i++) {
378 cnf = sk_CONF_VALUE_value(nos, i);
379 if (!(aint = s2i_ASN1_INTEGER(NULL, cnf->name))) {
380 X509V3err(X509V3_F_NREF_NOS, X509V3_R_INVALID_NUMBER);
383 if (!sk_ASN1_INTEGER_push(nnums, aint))
389 X509V3err(X509V3_F_NREF_NOS, ERR_R_MALLOC_FAILURE);
392 sk_ASN1_INTEGER_pop_free(nnums, ASN1_STRING_free);
396 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
397 BIO *out, int indent)
401 /* First print out the policy OIDs */
402 for (i = 0; i < sk_POLICYINFO_num(pol); i++) {
403 pinfo = sk_POLICYINFO_value(pol, i);
404 BIO_printf(out, "%*sPolicy: ", indent, "");
405 i2a_ASN1_OBJECT(out, pinfo->policyid);
407 if (pinfo->qualifiers)
408 print_qualifiers(out, pinfo->qualifiers, indent + 2);
413 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
416 POLICYQUALINFO *qualinfo;
418 for (i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
419 qualinfo = sk_POLICYQUALINFO_value(quals, i);
420 switch (OBJ_obj2nid(qualinfo->pqualid)) {
422 BIO_printf(out, "%*sCPS: %s\n", indent, "",
423 qualinfo->d.cpsuri->data);
426 case NID_id_qt_unotice:
427 BIO_printf(out, "%*sUser Notice:\n", indent, "");
428 print_notice(out, qualinfo->d.usernotice, indent + 2);
432 BIO_printf(out, "%*sUnknown Qualifier: ", indent + 2, "");
434 i2a_ASN1_OBJECT(out, qualinfo->pqualid);
441 static void print_notice(BIO *out, USERNOTICE *notice, int indent)
444 if (notice->noticeref) {
446 ref = notice->noticeref;
447 BIO_printf(out, "%*sOrganization: %s\n", indent, "",
448 ref->organization->data);
449 BIO_printf(out, "%*sNumber%s: ", indent, "",
450 sk_ASN1_INTEGER_num(ref->noticenos) > 1 ? "s" : "");
451 for (i = 0; i < sk_ASN1_INTEGER_num(ref->noticenos); i++) {
454 num = sk_ASN1_INTEGER_value(ref->noticenos, i);
457 tmp = i2s_ASN1_INTEGER(NULL, num);
464 BIO_printf(out, "%*sExplicit Text: %s\n", indent, "",
465 notice->exptext->data);
468 void X509_POLICY_NODE_print(BIO *out, X509_POLICY_NODE *node, int indent)
470 const X509_POLICY_DATA *dat = node->data;
472 BIO_printf(out, "%*sPolicy: ", indent, "");
474 i2a_ASN1_OBJECT(out, dat->valid_policy);
476 BIO_printf(out, "%*s%s\n", indent + 2, "",
477 node_data_critical(dat) ? "Critical" : "Non Critical");
478 if (dat->qualifier_set)
479 print_qualifiers(out, dat->qualifier_set, indent + 2);
481 BIO_printf(out, "%*sNo Qualifiers\n", indent + 2, "");