2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include <openssl/conf.h>
13 #include <openssl/asn1.h>
14 #include <openssl/asn1t.h>
15 #include <openssl/x509v3.h>
20 /* Certificate policies extension support: this one is a bit complex... */
22 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
23 BIO *out, int indent);
24 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
25 X509V3_CTX *ctx, const char *value);
26 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
28 static void print_notice(BIO *out, USERNOTICE *notice, int indent);
29 static POLICYINFO *policy_section(X509V3_CTX *ctx,
30 STACK_OF(CONF_VALUE) *polstrs, int ia5org);
31 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
32 STACK_OF(CONF_VALUE) *unot, int ia5org);
33 static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos);
34 static int displaytext_str2tag(const char *tagstr, unsigned int *tag_len);
35 static int displaytext_get_tag_len(const char *tagstr);
37 const X509V3_EXT_METHOD v3_cpols = {
38 NID_certificate_policies, 0, ASN1_ITEM_ref(CERTIFICATEPOLICIES),
42 (X509V3_EXT_I2R)i2r_certpol,
43 (X509V3_EXT_R2I)r2i_certpol,
47 ASN1_ITEM_TEMPLATE(CERTIFICATEPOLICIES) =
48 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, CERTIFICATEPOLICIES, POLICYINFO)
49 ASN1_ITEM_TEMPLATE_END(CERTIFICATEPOLICIES)
51 IMPLEMENT_ASN1_FUNCTIONS(CERTIFICATEPOLICIES)
53 ASN1_SEQUENCE(POLICYINFO) = {
54 ASN1_SIMPLE(POLICYINFO, policyid, ASN1_OBJECT),
55 ASN1_SEQUENCE_OF_OPT(POLICYINFO, qualifiers, POLICYQUALINFO)
56 } ASN1_SEQUENCE_END(POLICYINFO)
58 IMPLEMENT_ASN1_FUNCTIONS(POLICYINFO)
60 ASN1_ADB_TEMPLATE(policydefault) = ASN1_SIMPLE(POLICYQUALINFO, d.other, ASN1_ANY);
62 ASN1_ADB(POLICYQUALINFO) = {
63 ADB_ENTRY(NID_id_qt_cps, ASN1_SIMPLE(POLICYQUALINFO, d.cpsuri, ASN1_IA5STRING)),
64 ADB_ENTRY(NID_id_qt_unotice, ASN1_SIMPLE(POLICYQUALINFO, d.usernotice, USERNOTICE))
65 } ASN1_ADB_END(POLICYQUALINFO, 0, pqualid, 0, &policydefault_tt, NULL);
67 ASN1_SEQUENCE(POLICYQUALINFO) = {
68 ASN1_SIMPLE(POLICYQUALINFO, pqualid, ASN1_OBJECT),
69 ASN1_ADB_OBJECT(POLICYQUALINFO)
70 } ASN1_SEQUENCE_END(POLICYQUALINFO)
72 IMPLEMENT_ASN1_FUNCTIONS(POLICYQUALINFO)
74 ASN1_SEQUENCE(USERNOTICE) = {
75 ASN1_OPT(USERNOTICE, noticeref, NOTICEREF),
76 ASN1_OPT(USERNOTICE, exptext, DISPLAYTEXT)
77 } ASN1_SEQUENCE_END(USERNOTICE)
79 IMPLEMENT_ASN1_FUNCTIONS(USERNOTICE)
81 ASN1_SEQUENCE(NOTICEREF) = {
82 ASN1_SIMPLE(NOTICEREF, organization, DISPLAYTEXT),
83 ASN1_SEQUENCE_OF(NOTICEREF, noticenos, ASN1_INTEGER)
84 } ASN1_SEQUENCE_END(NOTICEREF)
86 IMPLEMENT_ASN1_FUNCTIONS(NOTICEREF)
88 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
89 X509V3_CTX *ctx, const char *value)
91 STACK_OF(POLICYINFO) *pols = NULL;
95 STACK_OF(CONF_VALUE) *vals;
98 pols = sk_POLICYINFO_new_null();
100 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
103 vals = X509V3_parse_list(value);
105 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_X509V3_LIB);
109 for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
110 cnf = sk_CONF_VALUE_value(vals, i);
111 if (cnf->value || !cnf->name) {
112 X509V3err(X509V3_F_R2I_CERTPOL,
113 X509V3_R_INVALID_POLICY_IDENTIFIER);
114 X509V3_conf_err(cnf);
118 if (strcmp(pstr, "ia5org") == 0) {
121 } else if (*pstr == '@') {
122 STACK_OF(CONF_VALUE) *polsect;
123 polsect = X509V3_get_section(ctx, pstr + 1);
125 X509V3err(X509V3_F_R2I_CERTPOL, X509V3_R_INVALID_SECTION);
127 X509V3_conf_err(cnf);
130 pol = policy_section(ctx, polsect, ia5org);
131 X509V3_section_free(ctx, polsect);
135 if ((pobj = OBJ_txt2obj(cnf->name, 0)) == NULL) {
136 X509V3err(X509V3_F_R2I_CERTPOL,
137 X509V3_R_INVALID_OBJECT_IDENTIFIER);
138 X509V3_conf_err(cnf);
141 pol = POLICYINFO_new();
143 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
144 ASN1_OBJECT_free(pobj);
147 pol->policyid = pobj;
149 if (!sk_POLICYINFO_push(pols, pol)) {
150 POLICYINFO_free(pol);
151 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
155 sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
158 sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
159 sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
163 static POLICYINFO *policy_section(X509V3_CTX *ctx,
164 STACK_OF(CONF_VALUE) *polstrs, int ia5org)
169 POLICYQUALINFO *qual;
171 if ((pol = POLICYINFO_new()) == NULL)
173 for (i = 0; i < sk_CONF_VALUE_num(polstrs); i++) {
174 cnf = sk_CONF_VALUE_value(polstrs, i);
175 if (strcmp(cnf->name, "policyIdentifier") == 0) {
177 if ((pobj = OBJ_txt2obj(cnf->value, 0)) == NULL) {
178 X509V3err(X509V3_F_POLICY_SECTION,
179 X509V3_R_INVALID_OBJECT_IDENTIFIER);
180 X509V3_conf_err(cnf);
183 pol->policyid = pobj;
185 } else if (!name_cmp(cnf->name, "CPS")) {
186 if (pol->qualifiers == NULL)
187 pol->qualifiers = sk_POLICYQUALINFO_new_null();
188 if ((qual = POLICYQUALINFO_new()) == NULL)
190 if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
192 if ((qual->pqualid = OBJ_nid2obj(NID_id_qt_cps)) == NULL) {
193 X509V3err(X509V3_F_POLICY_SECTION, ERR_R_INTERNAL_ERROR);
196 if ((qual->d.cpsuri = ASN1_IA5STRING_new()) == NULL)
198 if (!ASN1_STRING_set(qual->d.cpsuri, cnf->value,
201 } else if (!name_cmp(cnf->name, "userNotice")) {
202 STACK_OF(CONF_VALUE) *unot;
203 if (*cnf->value != '@') {
204 X509V3err(X509V3_F_POLICY_SECTION,
205 X509V3_R_EXPECTED_A_SECTION_NAME);
206 X509V3_conf_err(cnf);
209 unot = X509V3_get_section(ctx, cnf->value + 1);
211 X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_SECTION);
213 X509V3_conf_err(cnf);
216 qual = notice_section(ctx, unot, ia5org);
217 X509V3_section_free(ctx, unot);
220 if (!pol->qualifiers)
221 pol->qualifiers = sk_POLICYQUALINFO_new_null();
222 if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
225 X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_OPTION);
227 X509V3_conf_err(cnf);
231 if (!pol->policyid) {
232 X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_NO_POLICY_IDENTIFIER);
239 X509V3err(X509V3_F_POLICY_SECTION, ERR_R_MALLOC_FAILURE);
242 POLICYINFO_free(pol);
247 static int displaytext_get_tag_len(const char *tagstr)
249 char *colon = strchr(tagstr, ':');
251 return (colon == NULL) ? -1 : colon - tagstr;
254 static int displaytext_str2tag(const char *tagstr, unsigned int *tag_len)
259 len = displaytext_get_tag_len(tagstr);
262 return V_ASN1_VISIBLESTRING;
264 if (len == sizeof("UTF8") - 1 && strncmp(tagstr, "UTF8", len) == 0)
265 return V_ASN1_UTF8STRING;
266 if (len == sizeof("UTF8String") - 1 && strncmp(tagstr, "UTF8String", len) == 0)
267 return V_ASN1_UTF8STRING;
268 if (len == sizeof("BMP") - 1 && strncmp(tagstr, "BMP", len) == 0)
269 return V_ASN1_BMPSTRING;
270 if (len == sizeof("BMPSTRING") - 1 && strncmp(tagstr, "BMPSTRING", len) == 0)
271 return V_ASN1_BMPSTRING;
272 if (len == sizeof("VISIBLE") - 1 && strncmp(tagstr, "VISIBLE", len) == 0)
273 return V_ASN1_VISIBLESTRING;
274 if (len == sizeof("VISIBLESTRING") - 1 && strncmp(tagstr, "VISIBLESTRING", len) == 0)
275 return V_ASN1_VISIBLESTRING;
277 return V_ASN1_VISIBLESTRING;
280 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
281 STACK_OF(CONF_VALUE) *unot, int ia5org)
283 int i, ret, len, tag;
284 unsigned int tag_len;
287 POLICYQUALINFO *qual;
290 if ((qual = POLICYQUALINFO_new()) == NULL)
292 if ((qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice)) == NULL) {
293 X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_INTERNAL_ERROR);
296 if ((not = USERNOTICE_new()) == NULL)
298 qual->d.usernotice = not;
299 for (i = 0; i < sk_CONF_VALUE_num(unot); i++) {
300 cnf = sk_CONF_VALUE_value(unot, i);
302 if (strcmp(cnf->name, "explicitText") == 0) {
303 tag = displaytext_str2tag(value, &tag_len);
304 if ((not->exptext = ASN1_STRING_type_new(tag)) == NULL)
307 value += tag_len + 1;
309 if (!ASN1_STRING_set(not->exptext, value, len))
311 } else if (strcmp(cnf->name, "organization") == 0) {
313 if (!not->noticeref) {
314 if ((nref = NOTICEREF_new()) == NULL)
316 not->noticeref = nref;
318 nref = not->noticeref;
320 nref->organization->type = V_ASN1_IA5STRING;
322 nref->organization->type = V_ASN1_VISIBLESTRING;
323 if (!ASN1_STRING_set(nref->organization, cnf->value,
326 } else if (strcmp(cnf->name, "noticeNumbers") == 0) {
328 STACK_OF(CONF_VALUE) *nos;
329 if (!not->noticeref) {
330 if ((nref = NOTICEREF_new()) == NULL)
332 not->noticeref = nref;
334 nref = not->noticeref;
335 nos = X509V3_parse_list(cnf->value);
336 if (!nos || !sk_CONF_VALUE_num(nos)) {
337 X509V3err(X509V3_F_NOTICE_SECTION, X509V3_R_INVALID_NUMBERS);
338 X509V3_conf_err(cnf);
339 sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
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)) == NULL) {
380 X509V3err(X509V3_F_NREF_NOS, X509V3_R_INVALID_NUMBER);
383 if (!sk_ASN1_INTEGER_push(nnums, aint))
389 ASN1_INTEGER_free(aint);
390 X509V3err(X509V3_F_NREF_NOS, ERR_R_MALLOC_FAILURE);
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);
458 BIO_puts(out, "(null)");
460 tmp = i2s_ASN1_INTEGER(NULL, num);
470 BIO_printf(out, "%*sExplicit Text: %s\n", indent, "",
471 notice->exptext->data);
474 void X509_POLICY_NODE_print(BIO *out, X509_POLICY_NODE *node, int indent)
476 const X509_POLICY_DATA *dat = node->data;
478 BIO_printf(out, "%*sPolicy: ", indent, "");
480 i2a_ASN1_OBJECT(out, dat->valid_policy);
482 BIO_printf(out, "%*s%s\n", indent + 2, "",
483 node_data_critical(dat) ? "Critical" : "Non Critical");
484 if (dat->qualifier_set)
485 print_qualifiers(out, dat->qualifier_set, indent + 2);
487 BIO_printf(out, "%*sNo Qualifiers\n", indent + 2, "");