1 /*
2 * Copyright 1999-2025 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 #include <stdio.h>
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>
16
17 #include "x509_local.h"
18 #include "pcy_local.h"
19 #include "ext_dat.h"
20
21 /* Certificate policies extension support: this one is a bit complex... */
22
23 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
24 BIO *out, int indent);
25 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
26 X509V3_CTX *ctx, const char *value);
27 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
28 int indent);
29 static void print_notice(BIO *out, USERNOTICE *notice, int indent);
30 static POLICYINFO *policy_section(X509V3_CTX *ctx,
31 STACK_OF(CONF_VALUE) *polstrs, int ia5org);
32 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
33 STACK_OF(CONF_VALUE) *unot, int ia5org);
34 static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos);
35 static int displaytext_str2tag(const char *tagstr, unsigned int *tag_len);
36 static int displaytext_get_tag_len(const char *tagstr);
37
38 const X509V3_EXT_METHOD ossl_v3_cpols = {
39 NID_certificate_policies, 0, ASN1_ITEM_ref(CERTIFICATEPOLICIES),
40 0, 0, 0, 0,
41 0, 0,
42 0, 0,
43 (X509V3_EXT_I2R)i2r_certpol,
44 (X509V3_EXT_R2I)r2i_certpol,
45 NULL
46 };
47
48 ASN1_ITEM_TEMPLATE(CERTIFICATEPOLICIES) =
49 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, CERTIFICATEPOLICIES, POLICYINFO)
50 ASN1_ITEM_TEMPLATE_END(CERTIFICATEPOLICIES)
51
52 IMPLEMENT_ASN1_FUNCTIONS(CERTIFICATEPOLICIES)
53
54 ASN1_SEQUENCE(POLICYINFO) = {
55 ASN1_SIMPLE(POLICYINFO, policyid, ASN1_OBJECT),
56 ASN1_SEQUENCE_OF_OPT(POLICYINFO, qualifiers, POLICYQUALINFO)
57 } ASN1_SEQUENCE_END(POLICYINFO)
58
59 IMPLEMENT_ASN1_FUNCTIONS(POLICYINFO)
60
61 ASN1_ADB_TEMPLATE(policydefault) = ASN1_SIMPLE(POLICYQUALINFO, d.other, ASN1_ANY);
62
63 ASN1_ADB(POLICYQUALINFO) = {
64 ADB_ENTRY(NID_id_qt_cps, ASN1_SIMPLE(POLICYQUALINFO, d.cpsuri, ASN1_IA5STRING)),
65 ADB_ENTRY(NID_id_qt_unotice, ASN1_SIMPLE(POLICYQUALINFO, d.usernotice, USERNOTICE))
66 } ASN1_ADB_END(POLICYQUALINFO, 0, pqualid, 0, &policydefault_tt, NULL);
67
68 ASN1_SEQUENCE(POLICYQUALINFO) = {
69 ASN1_SIMPLE(POLICYQUALINFO, pqualid, ASN1_OBJECT),
70 ASN1_ADB_OBJECT(POLICYQUALINFO)
71 } ASN1_SEQUENCE_END(POLICYQUALINFO)
72
73 IMPLEMENT_ASN1_FUNCTIONS(POLICYQUALINFO)
74
75 ASN1_SEQUENCE(USERNOTICE) = {
76 ASN1_OPT(USERNOTICE, noticeref, NOTICEREF),
77 ASN1_OPT(USERNOTICE, exptext, DISPLAYTEXT)
78 } ASN1_SEQUENCE_END(USERNOTICE)
79
80 IMPLEMENT_ASN1_FUNCTIONS(USERNOTICE)
81
82 ASN1_SEQUENCE(NOTICEREF) = {
83 ASN1_SIMPLE(NOTICEREF, organization, DISPLAYTEXT),
84 ASN1_SEQUENCE_OF(NOTICEREF, noticenos, ASN1_INTEGER)
85 } ASN1_SEQUENCE_END(NOTICEREF)
86
87 IMPLEMENT_ASN1_FUNCTIONS(NOTICEREF)
88
89 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
90 X509V3_CTX *ctx, const char *value)
91 {
92 STACK_OF(POLICYINFO) *pols;
93 char *pstr;
94 POLICYINFO *pol;
95 ASN1_OBJECT *pobj;
96 STACK_OF(CONF_VALUE) *vals = X509V3_parse_list(value);
97 CONF_VALUE *cnf;
98 const int num = sk_CONF_VALUE_num(vals);
99 int i, ia5org;
100
101 if (vals == NULL) {
102 ERR_raise(ERR_LIB_X509V3, ERR_R_X509V3_LIB);
103 return NULL;
104 }
105
106 pols = sk_POLICYINFO_new_reserve(NULL, num);
107 if (pols == NULL) {
108 ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
109 goto err;
110 }
111
112 ia5org = 0;
113 for (i = 0; i < num; i++) {
114 cnf = sk_CONF_VALUE_value(vals, i);
115 if (cnf->value != NULL || cnf->name == NULL) {
116 ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_POLICY_IDENTIFIER);
117 X509V3_conf_add_error_name_value(cnf);
118 goto err;
119 }
120 pstr = cnf->name;
121 if (strcmp(pstr, "ia5org") == 0) {
122 ia5org = 1;
123 continue;
124 } else if (*pstr == '@') {
125 STACK_OF(CONF_VALUE) *polsect;
126
127 polsect = X509V3_get_section(ctx, pstr + 1);
128 if (polsect == NULL) {
129 ERR_raise_data(ERR_LIB_X509V3, X509V3_R_INVALID_SECTION,
130 "%s", cnf->name);
131 goto err;
132 }
133 pol = policy_section(ctx, polsect, ia5org);
134 X509V3_section_free(ctx, polsect);
135 if (pol == NULL)
136 goto err;
137 } else {
138 if ((pobj = OBJ_txt2obj(cnf->name, 0)) == NULL) {
139 ERR_raise_data(ERR_LIB_X509V3,
140 X509V3_R_INVALID_OBJECT_IDENTIFIER,
141 "%s", cnf->name);
142 goto err;
143 }
144 pol = POLICYINFO_new();
145 if (pol == NULL) {
146 ASN1_OBJECT_free(pobj);
147 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
148 goto err;
149 }
150 pol->policyid = pobj;
151 }
152 if (!sk_POLICYINFO_push(pols, pol)) {
153 POLICYINFO_free(pol);
154 ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
155 goto err;
156 }
157 }
158 sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
159 return pols;
160 err:
161 sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
162 sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
163 return NULL;
164 }
165
policy_section(X509V3_CTX * ctx,STACK_OF (CONF_VALUE)* polstrs,int ia5org)166 static POLICYINFO *policy_section(X509V3_CTX *ctx,
167 STACK_OF(CONF_VALUE) *polstrs, int ia5org)
168 {
169 int i;
170 CONF_VALUE *cnf;
171 POLICYINFO *pol;
172 POLICYQUALINFO *qual;
173
174 if ((pol = POLICYINFO_new()) == NULL) {
175 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
176 goto err;
177 }
178 for (i = 0; i < sk_CONF_VALUE_num(polstrs); i++) {
179 cnf = sk_CONF_VALUE_value(polstrs, i);
180 if (strcmp(cnf->name, "policyIdentifier") == 0) {
181 ASN1_OBJECT *pobj;
182
183 if ((pobj = OBJ_txt2obj(cnf->value, 0)) == NULL) {
184 ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_OBJECT_IDENTIFIER);
185 X509V3_conf_err(cnf);
186 goto err;
187 }
188 pol->policyid = pobj;
189
190 } else if (!ossl_v3_name_cmp(cnf->name, "CPS")) {
191 if (pol->qualifiers == NULL)
192 pol->qualifiers = sk_POLICYQUALINFO_new_null();
193 if ((qual = POLICYQUALINFO_new()) == NULL) {
194 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
195 goto err;
196 }
197 if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual)) {
198 POLICYQUALINFO_free(qual);
199 ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
200 goto err;
201 }
202 if ((qual->pqualid = OBJ_nid2obj(NID_id_qt_cps)) == NULL) {
203 ERR_raise(ERR_LIB_X509V3, ERR_R_INTERNAL_ERROR);
204 goto err;
205 }
206 if ((qual->d.cpsuri = ASN1_IA5STRING_new()) == NULL) {
207 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
208 goto err;
209 }
210 if (!ASN1_STRING_set(qual->d.cpsuri, cnf->value,
211 strlen(cnf->value))) {
212 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
213 goto err;
214 }
215 } else if (!ossl_v3_name_cmp(cnf->name, "userNotice")) {
216 STACK_OF(CONF_VALUE) *unot;
217 if (*cnf->value != '@') {
218 ERR_raise(ERR_LIB_X509V3, X509V3_R_EXPECTED_A_SECTION_NAME);
219 X509V3_conf_err(cnf);
220 goto err;
221 }
222 unot = X509V3_get_section(ctx, cnf->value + 1);
223 if (!unot) {
224 ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_SECTION);
225
226 X509V3_conf_err(cnf);
227 goto err;
228 }
229 qual = notice_section(ctx, unot, ia5org);
230 X509V3_section_free(ctx, unot);
231 if (!qual)
232 goto err;
233 if (pol->qualifiers == NULL)
234 pol->qualifiers = sk_POLICYQUALINFO_new_null();
235 if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual)) {
236 POLICYQUALINFO_free(qual);
237 ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
238 goto err;
239 }
240 } else {
241 ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_OPTION);
242 X509V3_conf_err(cnf);
243 goto err;
244 }
245 }
246 if (pol->policyid == NULL) {
247 ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_POLICY_IDENTIFIER);
248 goto err;
249 }
250
251 return pol;
252
253 err:
254 POLICYINFO_free(pol);
255 return NULL;
256 }
257
displaytext_get_tag_len(const char * tagstr)258 static int displaytext_get_tag_len(const char *tagstr)
259 {
260 char *colon = strchr(tagstr, ':');
261
262 return (colon == NULL) ? -1 : colon - tagstr;
263 }
264
displaytext_str2tag(const char * tagstr,unsigned int * tag_len)265 static int displaytext_str2tag(const char *tagstr, unsigned int *tag_len)
266 {
267 int len;
268
269 *tag_len = 0;
270 len = displaytext_get_tag_len(tagstr);
271
272 if (len == -1)
273 return V_ASN1_VISIBLESTRING;
274 *tag_len = len;
275 if (len == sizeof("UTF8") - 1 && HAS_PREFIX(tagstr, "UTF8"))
276 return V_ASN1_UTF8STRING;
277 if (len == sizeof("UTF8String") - 1 && HAS_PREFIX(tagstr, "UTF8String"))
278 return V_ASN1_UTF8STRING;
279 if (len == sizeof("BMP") - 1 && HAS_PREFIX(tagstr, "BMP"))
280 return V_ASN1_BMPSTRING;
281 if (len == sizeof("BMPSTRING") - 1 && HAS_PREFIX(tagstr, "BMPSTRING"))
282 return V_ASN1_BMPSTRING;
283 if (len == sizeof("VISIBLE") - 1 && HAS_PREFIX(tagstr, "VISIBLE"))
284 return V_ASN1_VISIBLESTRING;
285 if (len == sizeof("VISIBLESTRING") - 1 && HAS_PREFIX(tagstr, "VISIBLESTRING"))
286 return V_ASN1_VISIBLESTRING;
287 *tag_len = 0;
288 return V_ASN1_VISIBLESTRING;
289 }
290
notice_section(X509V3_CTX * ctx,STACK_OF (CONF_VALUE)* unot,int ia5org)291 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
292 STACK_OF(CONF_VALUE) *unot, int ia5org)
293 {
294 int i, ret, len, tag;
295 unsigned int tag_len;
296 CONF_VALUE *cnf;
297 USERNOTICE *not;
298 POLICYQUALINFO *qual;
299 char *value = NULL;
300
301 if ((qual = POLICYQUALINFO_new()) == NULL) {
302 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
303 goto err;
304 }
305 if ((qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice)) == NULL) {
306 ERR_raise(ERR_LIB_X509V3, ERR_R_INTERNAL_ERROR);
307 goto err;
308 }
309 if ((not = USERNOTICE_new()) == NULL) {
310 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
311 goto err;
312 }
313 qual->d.usernotice = not;
314 for (i = 0; i < sk_CONF_VALUE_num(unot); i++) {
315 cnf = sk_CONF_VALUE_value(unot, i);
316
317 value = cnf->value;
318 if (strcmp(cnf->name, "explicitText") == 0) {
319 tag = displaytext_str2tag(value, &tag_len);
320 if ((not->exptext = ASN1_STRING_type_new(tag)) == NULL) {
321 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
322 goto err;
323 }
324 if (tag_len != 0)
325 value += tag_len + 1;
326 len = strlen(value);
327 if (!ASN1_STRING_set(not->exptext, value, len)) {
328 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
329 goto err;
330 }
331 } else if (strcmp(cnf->name, "organization") == 0) {
332 NOTICEREF *nref;
333
334 if (!not->noticeref) {
335 if ((nref = NOTICEREF_new()) == NULL) {
336 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
337 goto err;
338 }
339 not->noticeref = nref;
340 } else
341 nref = not->noticeref;
342 if (ia5org)
343 nref->organization->type = V_ASN1_IA5STRING;
344 else
345 nref->organization->type = V_ASN1_VISIBLESTRING;
346 if (!ASN1_STRING_set(nref->organization, cnf->value,
347 strlen(cnf->value))) {
348 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
349 goto err;
350 }
351 } else if (strcmp(cnf->name, "noticeNumbers") == 0) {
352 NOTICEREF *nref;
353
354 STACK_OF(CONF_VALUE) *nos;
355 if (!not->noticeref) {
356 if ((nref = NOTICEREF_new()) == NULL) {
357 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
358 goto err;
359 }
360 not->noticeref = nref;
361 } else
362 nref = not->noticeref;
363 nos = X509V3_parse_list(cnf->value);
364 if (!nos || !sk_CONF_VALUE_num(nos)) {
365 ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_NUMBERS);
366 X509V3_conf_add_error_name_value(cnf);
367 sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
368 goto err;
369 }
370 ret = nref_nos(nref->noticenos, nos);
371 sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
372 if (!ret)
373 goto err;
374 } else {
375 ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_OPTION);
376 X509V3_conf_add_error_name_value(cnf);
377 goto err;
378 }
379 }
380
381 if (not->noticeref &&
382 (!not->noticeref->noticenos || !not->noticeref->organization)) {
383 ERR_raise(ERR_LIB_X509V3, X509V3_R_NEED_ORGANIZATION_AND_NUMBERS);
384 goto err;
385 }
386
387 return qual;
388
389 err:
390 POLICYQUALINFO_free(qual);
391 return NULL;
392 }
393
nref_nos(STACK_OF (ASN1_INTEGER)* nnums,STACK_OF (CONF_VALUE)* nos)394 static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos)
395 {
396 CONF_VALUE *cnf;
397 ASN1_INTEGER *aint;
398
399 int i;
400
401 for (i = 0; i < sk_CONF_VALUE_num(nos); i++) {
402 cnf = sk_CONF_VALUE_value(nos, i);
403 if ((aint = s2i_ASN1_INTEGER(NULL, cnf->name)) == NULL) {
404 ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_NUMBER);
405 return 0;
406 }
407 if (!sk_ASN1_INTEGER_push(nnums, aint)) {
408 ASN1_INTEGER_free(aint);
409 ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
410 return 0;
411 }
412 }
413 return 1;
414 }
415
i2r_certpol(X509V3_EXT_METHOD * method,STACK_OF (POLICYINFO)* pol,BIO * out,int indent)416 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
417 BIO *out, int indent)
418 {
419 int i;
420 POLICYINFO *pinfo;
421 /* First print out the policy OIDs */
422 for (i = 0; i < sk_POLICYINFO_num(pol); i++) {
423 if (i > 0)
424 BIO_puts(out, "\n");
425 pinfo = sk_POLICYINFO_value(pol, i);
426 BIO_printf(out, "%*sPolicy: ", indent, "");
427 i2a_ASN1_OBJECT(out, pinfo->policyid);
428 if (pinfo->qualifiers) {
429 BIO_puts(out, "\n");
430 print_qualifiers(out, pinfo->qualifiers, indent + 2);
431 }
432 }
433 return 1;
434 }
435
print_qualifiers(BIO * out,STACK_OF (POLICYQUALINFO)* quals,int indent)436 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
437 int indent)
438 {
439 POLICYQUALINFO *qualinfo;
440 int i;
441 for (i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
442 if (i > 0)
443 BIO_puts(out, "\n");
444 qualinfo = sk_POLICYQUALINFO_value(quals, i);
445 switch (OBJ_obj2nid(qualinfo->pqualid)) {
446 case NID_id_qt_cps:
447 BIO_printf(out, "%*sCPS: %.*s", indent, "",
448 qualinfo->d.cpsuri->length,
449 qualinfo->d.cpsuri->data);
450 break;
451
452 case NID_id_qt_unotice:
453 BIO_printf(out, "%*sUser Notice:\n", indent, "");
454 print_notice(out, qualinfo->d.usernotice, indent + 2);
455 break;
456
457 default:
458 BIO_printf(out, "%*sUnknown Qualifier: ", indent + 2, "");
459
460 i2a_ASN1_OBJECT(out, qualinfo->pqualid);
461 break;
462 }
463 }
464 }
465
print_notice(BIO * out,USERNOTICE * notice,int indent)466 static void print_notice(BIO *out, USERNOTICE *notice, int indent)
467 {
468 int i;
469 if (notice->noticeref) {
470 NOTICEREF *ref;
471 ref = notice->noticeref;
472 BIO_printf(out, "%*sOrganization: %.*s\n", indent, "",
473 ref->organization->length,
474 ref->organization->data);
475 BIO_printf(out, "%*sNumber%s: ", indent, "",
476 sk_ASN1_INTEGER_num(ref->noticenos) > 1 ? "s" : "");
477 for (i = 0; i < sk_ASN1_INTEGER_num(ref->noticenos); i++) {
478 ASN1_INTEGER *num;
479 char *tmp;
480 num = sk_ASN1_INTEGER_value(ref->noticenos, i);
481 if (i)
482 BIO_puts(out, ", ");
483 if (num == NULL)
484 BIO_puts(out, "(null)");
485 else {
486 tmp = i2s_ASN1_INTEGER(NULL, num);
487 if (tmp == NULL)
488 return;
489 BIO_puts(out, tmp);
490 OPENSSL_free(tmp);
491 }
492 }
493 if (notice->exptext)
494 BIO_puts(out, "\n");
495 }
496 if (notice->exptext)
497 BIO_printf(out, "%*sExplicit Text: %.*s", indent, "",
498 notice->exptext->length,
499 notice->exptext->data);
500 }
501
X509_POLICY_NODE_print(BIO * out,X509_POLICY_NODE * node,int indent)502 void X509_POLICY_NODE_print(BIO *out, X509_POLICY_NODE *node, int indent)
503 {
504 const X509_POLICY_DATA *dat = node->data;
505
506 BIO_printf(out, "%*sPolicy: ", indent, "");
507
508 i2a_ASN1_OBJECT(out, dat->valid_policy);
509 BIO_puts(out, "\n");
510 BIO_printf(out, "%*s%s\n", indent + 2, "",
511 node_data_critical(dat) ? "Critical" : "Non Critical");
512 if (dat->qualifier_set) {
513 print_qualifiers(out, dat->qualifier_set, indent + 2);
514 BIO_puts(out, "\n");
515 }
516 else
517 BIO_printf(out, "%*sNo Qualifiers\n", indent + 2, "");
518 }
519