xref: /openssl/crypto/x509/v3_cpols.c (revision e077455e)
1 /*
2  * Copyright 1999-2021 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                 ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
199                 goto err;
200             }
201             if ((qual->pqualid = OBJ_nid2obj(NID_id_qt_cps)) == NULL) {
202                 ERR_raise(ERR_LIB_X509V3, ERR_R_INTERNAL_ERROR);
203                 goto err;
204             }
205             if ((qual->d.cpsuri = ASN1_IA5STRING_new()) == NULL) {
206                 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
207                 goto err;
208             }
209             if (!ASN1_STRING_set(qual->d.cpsuri, cnf->value,
210                                  strlen(cnf->value))) {
211                 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
212                 goto err;
213             }
214         } else if (!ossl_v3_name_cmp(cnf->name, "userNotice")) {
215             STACK_OF(CONF_VALUE) *unot;
216             if (*cnf->value != '@') {
217                 ERR_raise(ERR_LIB_X509V3, X509V3_R_EXPECTED_A_SECTION_NAME);
218                 X509V3_conf_err(cnf);
219                 goto err;
220             }
221             unot = X509V3_get_section(ctx, cnf->value + 1);
222             if (!unot) {
223                 ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_SECTION);
224 
225                 X509V3_conf_err(cnf);
226                 goto err;
227             }
228             qual = notice_section(ctx, unot, ia5org);
229             X509V3_section_free(ctx, unot);
230             if (!qual)
231                 goto err;
232             if (pol->qualifiers == NULL)
233                 pol->qualifiers = sk_POLICYQUALINFO_new_null();
234             if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual)) {
235                 ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
236                 goto err;
237             }
238         } else {
239             ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_OPTION);
240             X509V3_conf_err(cnf);
241             goto err;
242         }
243     }
244     if (pol->policyid == NULL) {
245         ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_POLICY_IDENTIFIER);
246         goto err;
247     }
248 
249     return pol;
250 
251  err:
252     POLICYINFO_free(pol);
253     return NULL;
254 }
255 
displaytext_get_tag_len(const char * tagstr)256 static int displaytext_get_tag_len(const char *tagstr)
257 {
258     char *colon = strchr(tagstr, ':');
259 
260     return (colon == NULL) ? -1 : colon - tagstr;
261 }
262 
displaytext_str2tag(const char * tagstr,unsigned int * tag_len)263 static int displaytext_str2tag(const char *tagstr, unsigned int *tag_len)
264 {
265     int len;
266 
267     *tag_len = 0;
268     len = displaytext_get_tag_len(tagstr);
269 
270     if (len == -1)
271         return V_ASN1_VISIBLESTRING;
272     *tag_len = len;
273     if (len == sizeof("UTF8") - 1 && HAS_PREFIX(tagstr, "UTF8"))
274         return V_ASN1_UTF8STRING;
275     if (len == sizeof("UTF8String") - 1 && HAS_PREFIX(tagstr, "UTF8String"))
276         return V_ASN1_UTF8STRING;
277     if (len == sizeof("BMP") - 1 && HAS_PREFIX(tagstr, "BMP"))
278         return V_ASN1_BMPSTRING;
279     if (len == sizeof("BMPSTRING") - 1 && HAS_PREFIX(tagstr, "BMPSTRING"))
280         return V_ASN1_BMPSTRING;
281     if (len == sizeof("VISIBLE") - 1 && HAS_PREFIX(tagstr, "VISIBLE"))
282         return V_ASN1_VISIBLESTRING;
283     if (len == sizeof("VISIBLESTRING") - 1 && HAS_PREFIX(tagstr, "VISIBLESTRING"))
284         return V_ASN1_VISIBLESTRING;
285     *tag_len = 0;
286     return V_ASN1_VISIBLESTRING;
287 }
288 
notice_section(X509V3_CTX * ctx,STACK_OF (CONF_VALUE)* unot,int ia5org)289 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
290                                       STACK_OF(CONF_VALUE) *unot, int ia5org)
291 {
292     int i, ret, len, tag;
293     unsigned int tag_len;
294     CONF_VALUE *cnf;
295     USERNOTICE *not;
296     POLICYQUALINFO *qual;
297     char *value = NULL;
298 
299     if ((qual = POLICYQUALINFO_new()) == NULL) {
300         ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
301         goto err;
302     }
303     if ((qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice)) == NULL) {
304         ERR_raise(ERR_LIB_X509V3, ERR_R_INTERNAL_ERROR);
305         goto err;
306     }
307     if ((not = USERNOTICE_new()) == NULL) {
308         ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
309         goto err;
310     }
311     qual->d.usernotice = not;
312     for (i = 0; i < sk_CONF_VALUE_num(unot); i++) {
313         cnf = sk_CONF_VALUE_value(unot, i);
314 
315         value = cnf->value;
316         if (strcmp(cnf->name, "explicitText") == 0) {
317             tag = displaytext_str2tag(value, &tag_len);
318             if ((not->exptext = ASN1_STRING_type_new(tag)) == NULL) {
319                 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
320                 goto err;
321             }
322             if (tag_len != 0)
323                 value += tag_len + 1;
324             len = strlen(value);
325             if (!ASN1_STRING_set(not->exptext, value, len)) {
326                 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
327                 goto err;
328             }
329         } else if (strcmp(cnf->name, "organization") == 0) {
330             NOTICEREF *nref;
331 
332             if (!not->noticeref) {
333                 if ((nref = NOTICEREF_new()) == NULL) {
334                     ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
335                     goto err;
336                 }
337                 not->noticeref = nref;
338             } else
339                 nref = not->noticeref;
340             if (ia5org)
341                 nref->organization->type = V_ASN1_IA5STRING;
342             else
343                 nref->organization->type = V_ASN1_VISIBLESTRING;
344             if (!ASN1_STRING_set(nref->organization, cnf->value,
345                                  strlen(cnf->value))) {
346                 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
347                 goto err;
348             }
349         } else if (strcmp(cnf->name, "noticeNumbers") == 0) {
350             NOTICEREF *nref;
351 
352             STACK_OF(CONF_VALUE) *nos;
353             if (!not->noticeref) {
354                 if ((nref = NOTICEREF_new()) == NULL) {
355                     ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
356                     goto err;
357                 }
358                 not->noticeref = nref;
359             } else
360                 nref = not->noticeref;
361             nos = X509V3_parse_list(cnf->value);
362             if (!nos || !sk_CONF_VALUE_num(nos)) {
363                 ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_NUMBERS);
364                 X509V3_conf_add_error_name_value(cnf);
365                 sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
366                 goto err;
367             }
368             ret = nref_nos(nref->noticenos, nos);
369             sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
370             if (!ret)
371                 goto err;
372         } else {
373             ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_OPTION);
374             X509V3_conf_add_error_name_value(cnf);
375             goto err;
376         }
377     }
378 
379     if (not->noticeref &&
380         (!not->noticeref->noticenos || !not->noticeref->organization)) {
381         ERR_raise(ERR_LIB_X509V3, X509V3_R_NEED_ORGANIZATION_AND_NUMBERS);
382         goto err;
383     }
384 
385     return qual;
386 
387  err:
388     POLICYQUALINFO_free(qual);
389     return NULL;
390 }
391 
nref_nos(STACK_OF (ASN1_INTEGER)* nnums,STACK_OF (CONF_VALUE)* nos)392 static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos)
393 {
394     CONF_VALUE *cnf;
395     ASN1_INTEGER *aint;
396 
397     int i;
398 
399     for (i = 0; i < sk_CONF_VALUE_num(nos); i++) {
400         cnf = sk_CONF_VALUE_value(nos, i);
401         if ((aint = s2i_ASN1_INTEGER(NULL, cnf->name)) == NULL) {
402             ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_NUMBER);
403             return 0;
404         }
405         if (!sk_ASN1_INTEGER_push(nnums, aint)) {
406             ASN1_INTEGER_free(aint);
407             ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
408             return 0;
409         }
410     }
411     return 1;
412 }
413 
i2r_certpol(X509V3_EXT_METHOD * method,STACK_OF (POLICYINFO)* pol,BIO * out,int indent)414 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
415                        BIO *out, int indent)
416 {
417     int i;
418     POLICYINFO *pinfo;
419     /* First print out the policy OIDs */
420     for (i = 0; i < sk_POLICYINFO_num(pol); i++) {
421         if (i > 0)
422             BIO_puts(out, "\n");
423         pinfo = sk_POLICYINFO_value(pol, i);
424         BIO_printf(out, "%*sPolicy: ", indent, "");
425         i2a_ASN1_OBJECT(out, pinfo->policyid);
426         if (pinfo->qualifiers) {
427             BIO_puts(out, "\n");
428             print_qualifiers(out, pinfo->qualifiers, indent + 2);
429         }
430     }
431     return 1;
432 }
433 
print_qualifiers(BIO * out,STACK_OF (POLICYQUALINFO)* quals,int indent)434 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
435                              int indent)
436 {
437     POLICYQUALINFO *qualinfo;
438     int i;
439     for (i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
440         if (i > 0)
441             BIO_puts(out, "\n");
442         qualinfo = sk_POLICYQUALINFO_value(quals, i);
443         switch (OBJ_obj2nid(qualinfo->pqualid)) {
444         case NID_id_qt_cps:
445             BIO_printf(out, "%*sCPS: %.*s", indent, "",
446                        qualinfo->d.cpsuri->length,
447                        qualinfo->d.cpsuri->data);
448             break;
449 
450         case NID_id_qt_unotice:
451             BIO_printf(out, "%*sUser Notice:\n", indent, "");
452             print_notice(out, qualinfo->d.usernotice, indent + 2);
453             break;
454 
455         default:
456             BIO_printf(out, "%*sUnknown Qualifier: ", indent + 2, "");
457 
458             i2a_ASN1_OBJECT(out, qualinfo->pqualid);
459             break;
460         }
461     }
462 }
463 
print_notice(BIO * out,USERNOTICE * notice,int indent)464 static void print_notice(BIO *out, USERNOTICE *notice, int indent)
465 {
466     int i;
467     if (notice->noticeref) {
468         NOTICEREF *ref;
469         ref = notice->noticeref;
470         BIO_printf(out, "%*sOrganization: %.*s\n", indent, "",
471                    ref->organization->length,
472                    ref->organization->data);
473         BIO_printf(out, "%*sNumber%s: ", indent, "",
474                    sk_ASN1_INTEGER_num(ref->noticenos) > 1 ? "s" : "");
475         for (i = 0; i < sk_ASN1_INTEGER_num(ref->noticenos); i++) {
476             ASN1_INTEGER *num;
477             char *tmp;
478             num = sk_ASN1_INTEGER_value(ref->noticenos, i);
479             if (i)
480                 BIO_puts(out, ", ");
481             if (num == NULL)
482                 BIO_puts(out, "(null)");
483             else {
484                 tmp = i2s_ASN1_INTEGER(NULL, num);
485                 if (tmp == NULL)
486                     return;
487                 BIO_puts(out, tmp);
488                 OPENSSL_free(tmp);
489             }
490         }
491         if (notice->exptext)
492             BIO_puts(out, "\n");
493     }
494     if (notice->exptext)
495         BIO_printf(out, "%*sExplicit Text: %.*s", indent, "",
496                    notice->exptext->length,
497                    notice->exptext->data);
498 }
499 
X509_POLICY_NODE_print(BIO * out,X509_POLICY_NODE * node,int indent)500 void X509_POLICY_NODE_print(BIO *out, X509_POLICY_NODE *node, int indent)
501 {
502     const X509_POLICY_DATA *dat = node->data;
503 
504     BIO_printf(out, "%*sPolicy: ", indent, "");
505 
506     i2a_ASN1_OBJECT(out, dat->valid_policy);
507     BIO_puts(out, "\n");
508     BIO_printf(out, "%*s%s\n", indent + 2, "",
509                node_data_critical(dat) ? "Critical" : "Non Critical");
510     if (dat->qualifier_set) {
511         print_qualifiers(out, dat->qualifier_set, indent + 2);
512         BIO_puts(out, "\n");
513     }
514     else
515         BIO_printf(out, "%*sNo Qualifiers\n", indent + 2, "");
516 }
517