2 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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>
17 #include "pcy_local.h"
20 DEFINE_STACK_OF(CONF_VALUE)
21 DEFINE_STACK_OF(POLICYINFO)
22 DEFINE_STACK_OF(POLICYQUALINFO)
23 DEFINE_STACK_OF(ASN1_INTEGER)
25 /* Certificate policies extension support: this one is a bit complex... */
27 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
28 BIO *out, int indent);
29 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
30 X509V3_CTX *ctx, const char *value);
31 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
33 static void print_notice(BIO *out, USERNOTICE *notice, int indent);
34 static POLICYINFO *policy_section(X509V3_CTX *ctx,
35 STACK_OF(CONF_VALUE) *polstrs, int ia5org);
36 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
37 STACK_OF(CONF_VALUE) *unot, int ia5org);
38 static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos);
39 static int displaytext_str2tag(const char *tagstr, unsigned int *tag_len);
40 static int displaytext_get_tag_len(const char *tagstr);
42 const X509V3_EXT_METHOD v3_cpols = {
43 NID_certificate_policies, 0, ASN1_ITEM_ref(CERTIFICATEPOLICIES),
47 (X509V3_EXT_I2R)i2r_certpol,
48 (X509V3_EXT_R2I)r2i_certpol,
52 ASN1_ITEM_TEMPLATE(CERTIFICATEPOLICIES) =
53 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, CERTIFICATEPOLICIES, POLICYINFO)
54 ASN1_ITEM_TEMPLATE_END(CERTIFICATEPOLICIES)
56 IMPLEMENT_ASN1_FUNCTIONS(CERTIFICATEPOLICIES)
58 ASN1_SEQUENCE(POLICYINFO) = {
59 ASN1_SIMPLE(POLICYINFO, policyid, ASN1_OBJECT),
60 ASN1_SEQUENCE_OF_OPT(POLICYINFO, qualifiers, POLICYQUALINFO)
61 } ASN1_SEQUENCE_END(POLICYINFO)
63 IMPLEMENT_ASN1_FUNCTIONS(POLICYINFO)
65 ASN1_ADB_TEMPLATE(policydefault) = ASN1_SIMPLE(POLICYQUALINFO, d.other, ASN1_ANY);
67 ASN1_ADB(POLICYQUALINFO) = {
68 ADB_ENTRY(NID_id_qt_cps, ASN1_SIMPLE(POLICYQUALINFO, d.cpsuri, ASN1_IA5STRING)),
69 ADB_ENTRY(NID_id_qt_unotice, ASN1_SIMPLE(POLICYQUALINFO, d.usernotice, USERNOTICE))
70 } ASN1_ADB_END(POLICYQUALINFO, 0, pqualid, 0, &policydefault_tt, NULL);
72 ASN1_SEQUENCE(POLICYQUALINFO) = {
73 ASN1_SIMPLE(POLICYQUALINFO, pqualid, ASN1_OBJECT),
74 ASN1_ADB_OBJECT(POLICYQUALINFO)
75 } ASN1_SEQUENCE_END(POLICYQUALINFO)
77 IMPLEMENT_ASN1_FUNCTIONS(POLICYQUALINFO)
79 ASN1_SEQUENCE(USERNOTICE) = {
80 ASN1_OPT(USERNOTICE, noticeref, NOTICEREF),
81 ASN1_OPT(USERNOTICE, exptext, DISPLAYTEXT)
82 } ASN1_SEQUENCE_END(USERNOTICE)
84 IMPLEMENT_ASN1_FUNCTIONS(USERNOTICE)
86 ASN1_SEQUENCE(NOTICEREF) = {
87 ASN1_SIMPLE(NOTICEREF, organization, DISPLAYTEXT),
88 ASN1_SEQUENCE_OF(NOTICEREF, noticenos, ASN1_INTEGER)
89 } ASN1_SEQUENCE_END(NOTICEREF)
91 IMPLEMENT_ASN1_FUNCTIONS(NOTICEREF)
93 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
94 X509V3_CTX *ctx, const char *value)
96 STACK_OF(POLICYINFO) *pols;
100 STACK_OF(CONF_VALUE) *vals = X509V3_parse_list(value);
102 const int num = sk_CONF_VALUE_num(vals);
106 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_X509V3_LIB);
110 pols = sk_POLICYINFO_new_reserve(NULL, num);
112 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
117 for (i = 0; i < num; i++) {
118 cnf = sk_CONF_VALUE_value(vals, i);
120 if (cnf->value || !cnf->name) {
121 X509V3err(X509V3_F_R2I_CERTPOL,
122 X509V3_R_INVALID_POLICY_IDENTIFIER);
123 X509V3_conf_err(cnf);
127 if (strcmp(pstr, "ia5org") == 0) {
130 } else if (*pstr == '@') {
131 STACK_OF(CONF_VALUE) *polsect;
133 polsect = X509V3_get_section(ctx, pstr + 1);
134 if (polsect == NULL) {
135 X509V3err(X509V3_F_R2I_CERTPOL, X509V3_R_INVALID_SECTION);
137 X509V3_conf_err(cnf);
140 pol = policy_section(ctx, polsect, ia5org);
141 X509V3_section_free(ctx, polsect);
145 if ((pobj = OBJ_txt2obj(cnf->name, 0)) == NULL) {
146 X509V3err(X509V3_F_R2I_CERTPOL,
147 X509V3_R_INVALID_OBJECT_IDENTIFIER);
148 X509V3_conf_err(cnf);
151 pol = POLICYINFO_new();
153 ASN1_OBJECT_free(pobj);
154 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
157 pol->policyid = pobj;
159 if (!sk_POLICYINFO_push(pols, pol)) {
160 POLICYINFO_free(pol);
161 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
165 sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
168 sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
169 sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
173 static POLICYINFO *policy_section(X509V3_CTX *ctx,
174 STACK_OF(CONF_VALUE) *polstrs, int ia5org)
179 POLICYQUALINFO *qual;
181 if ((pol = POLICYINFO_new()) == NULL)
183 for (i = 0; i < sk_CONF_VALUE_num(polstrs); i++) {
184 cnf = sk_CONF_VALUE_value(polstrs, i);
185 if (strcmp(cnf->name, "policyIdentifier") == 0) {
187 if ((pobj = OBJ_txt2obj(cnf->value, 0)) == NULL) {
188 X509V3err(X509V3_F_POLICY_SECTION,
189 X509V3_R_INVALID_OBJECT_IDENTIFIER);
190 X509V3_conf_err(cnf);
193 pol->policyid = pobj;
195 } else if (!v3_name_cmp(cnf->name, "CPS")) {
196 if (pol->qualifiers == NULL)
197 pol->qualifiers = sk_POLICYQUALINFO_new_null();
198 if ((qual = POLICYQUALINFO_new()) == NULL)
200 if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
202 if ((qual->pqualid = OBJ_nid2obj(NID_id_qt_cps)) == NULL) {
203 X509V3err(X509V3_F_POLICY_SECTION, ERR_R_INTERNAL_ERROR);
206 if ((qual->d.cpsuri = ASN1_IA5STRING_new()) == NULL)
208 if (!ASN1_STRING_set(qual->d.cpsuri, cnf->value,
211 } else if (!v3_name_cmp(cnf->name, "userNotice")) {
212 STACK_OF(CONF_VALUE) *unot;
213 if (*cnf->value != '@') {
214 X509V3err(X509V3_F_POLICY_SECTION,
215 X509V3_R_EXPECTED_A_SECTION_NAME);
216 X509V3_conf_err(cnf);
219 unot = X509V3_get_section(ctx, cnf->value + 1);
221 X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_SECTION);
223 X509V3_conf_err(cnf);
226 qual = notice_section(ctx, unot, ia5org);
227 X509V3_section_free(ctx, unot);
230 if (pol->qualifiers == NULL)
231 pol->qualifiers = sk_POLICYQUALINFO_new_null();
232 if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
235 X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_OPTION);
237 X509V3_conf_err(cnf);
241 if (pol->policyid == NULL) {
242 X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_NO_POLICY_IDENTIFIER);
249 X509V3err(X509V3_F_POLICY_SECTION, ERR_R_MALLOC_FAILURE);
252 POLICYINFO_free(pol);
256 static int displaytext_get_tag_len(const char *tagstr)
258 char *colon = strchr(tagstr, ':');
260 return (colon == NULL) ? -1 : colon - tagstr;
263 static int displaytext_str2tag(const char *tagstr, unsigned int *tag_len)
268 len = displaytext_get_tag_len(tagstr);
271 return V_ASN1_VISIBLESTRING;
273 if (len == sizeof("UTF8") - 1 && strncmp(tagstr, "UTF8", len) == 0)
274 return V_ASN1_UTF8STRING;
275 if (len == sizeof("UTF8String") - 1 && strncmp(tagstr, "UTF8String", len) == 0)
276 return V_ASN1_UTF8STRING;
277 if (len == sizeof("BMP") - 1 && strncmp(tagstr, "BMP", len) == 0)
278 return V_ASN1_BMPSTRING;
279 if (len == sizeof("BMPSTRING") - 1 && strncmp(tagstr, "BMPSTRING", len) == 0)
280 return V_ASN1_BMPSTRING;
281 if (len == sizeof("VISIBLE") - 1 && strncmp(tagstr, "VISIBLE", len) == 0)
282 return V_ASN1_VISIBLESTRING;
283 if (len == sizeof("VISIBLESTRING") - 1 && strncmp(tagstr, "VISIBLESTRING", len) == 0)
284 return V_ASN1_VISIBLESTRING;
286 return V_ASN1_VISIBLESTRING;
289 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
290 STACK_OF(CONF_VALUE) *unot, int ia5org)
292 int i, ret, len, tag;
293 unsigned int tag_len;
296 POLICYQUALINFO *qual;
299 if ((qual = POLICYQUALINFO_new()) == NULL)
301 if ((qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice)) == NULL) {
302 X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_INTERNAL_ERROR);
305 if ((not = USERNOTICE_new()) == NULL)
307 qual->d.usernotice = not;
308 for (i = 0; i < sk_CONF_VALUE_num(unot); i++) {
309 cnf = sk_CONF_VALUE_value(unot, i);
311 if (strcmp(cnf->name, "explicitText") == 0) {
312 tag = displaytext_str2tag(value, &tag_len);
313 if ((not->exptext = ASN1_STRING_type_new(tag)) == NULL)
316 value += tag_len + 1;
318 if (!ASN1_STRING_set(not->exptext, value, len))
320 } else if (strcmp(cnf->name, "organization") == 0) {
322 if (!not->noticeref) {
323 if ((nref = NOTICEREF_new()) == NULL)
325 not->noticeref = nref;
327 nref = not->noticeref;
329 nref->organization->type = V_ASN1_IA5STRING;
331 nref->organization->type = V_ASN1_VISIBLESTRING;
332 if (!ASN1_STRING_set(nref->organization, cnf->value,
335 } else if (strcmp(cnf->name, "noticeNumbers") == 0) {
337 STACK_OF(CONF_VALUE) *nos;
338 if (!not->noticeref) {
339 if ((nref = NOTICEREF_new()) == NULL)
341 not->noticeref = nref;
343 nref = not->noticeref;
344 nos = X509V3_parse_list(cnf->value);
345 if (!nos || !sk_CONF_VALUE_num(nos)) {
346 X509V3err(X509V3_F_NOTICE_SECTION, X509V3_R_INVALID_NUMBERS);
347 X509V3_conf_err(cnf);
348 sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
351 ret = nref_nos(nref->noticenos, nos);
352 sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
356 X509V3err(X509V3_F_NOTICE_SECTION, X509V3_R_INVALID_OPTION);
357 X509V3_conf_err(cnf);
362 if (not->noticeref &&
363 (!not->noticeref->noticenos || !not->noticeref->organization)) {
364 X509V3err(X509V3_F_NOTICE_SECTION,
365 X509V3_R_NEED_ORGANIZATION_AND_NUMBERS);
372 X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_MALLOC_FAILURE);
375 POLICYQUALINFO_free(qual);
379 static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos)
386 for (i = 0; i < sk_CONF_VALUE_num(nos); i++) {
387 cnf = sk_CONF_VALUE_value(nos, i);
388 if ((aint = s2i_ASN1_INTEGER(NULL, cnf->name)) == NULL) {
389 X509V3err(X509V3_F_NREF_NOS, X509V3_R_INVALID_NUMBER);
392 if (!sk_ASN1_INTEGER_push(nnums, aint))
398 ASN1_INTEGER_free(aint);
399 X509V3err(X509V3_F_NREF_NOS, ERR_R_MALLOC_FAILURE);
405 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
406 BIO *out, int indent)
410 /* First print out the policy OIDs */
411 for (i = 0; i < sk_POLICYINFO_num(pol); i++) {
414 pinfo = sk_POLICYINFO_value(pol, i);
415 BIO_printf(out, "%*sPolicy: ", indent, "");
416 i2a_ASN1_OBJECT(out, pinfo->policyid);
417 if (pinfo->qualifiers) {
419 print_qualifiers(out, pinfo->qualifiers, indent + 2);
425 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
428 POLICYQUALINFO *qualinfo;
430 for (i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
433 qualinfo = sk_POLICYQUALINFO_value(quals, i);
434 switch (OBJ_obj2nid(qualinfo->pqualid)) {
436 BIO_printf(out, "%*sCPS: %s", indent, "",
437 qualinfo->d.cpsuri->data);
440 case NID_id_qt_unotice:
441 BIO_printf(out, "%*sUser Notice:\n", indent, "");
442 print_notice(out, qualinfo->d.usernotice, indent + 2);
446 BIO_printf(out, "%*sUnknown Qualifier: ", indent + 2, "");
448 i2a_ASN1_OBJECT(out, qualinfo->pqualid);
454 static void print_notice(BIO *out, USERNOTICE *notice, int indent)
457 if (notice->noticeref) {
459 ref = notice->noticeref;
460 BIO_printf(out, "%*sOrganization: %s\n", indent, "",
461 ref->organization->data);
462 BIO_printf(out, "%*sNumber%s: ", indent, "",
463 sk_ASN1_INTEGER_num(ref->noticenos) > 1 ? "s" : "");
464 for (i = 0; i < sk_ASN1_INTEGER_num(ref->noticenos); i++) {
467 num = sk_ASN1_INTEGER_value(ref->noticenos, i);
471 BIO_puts(out, "(null)");
473 tmp = i2s_ASN1_INTEGER(NULL, num);
484 BIO_printf(out, "%*sExplicit Text: %s", indent, "",
485 notice->exptext->data);
488 void X509_POLICY_NODE_print(BIO *out, X509_POLICY_NODE *node, int indent)
490 const X509_POLICY_DATA *dat = node->data;
492 BIO_printf(out, "%*sPolicy: ", indent, "");
494 i2a_ASN1_OBJECT(out, dat->valid_policy);
496 BIO_printf(out, "%*s%s\n", indent + 2, "",
497 node_data_critical(dat) ? "Critical" : "Non Critical");
498 if (dat->qualifier_set) {
499 print_qualifiers(out, dat->qualifier_set, indent + 2);
503 BIO_printf(out, "%*sNo Qualifiers\n", indent + 2, "");