CTR, HASH and HMAC DRBGs in provider
[openssl.git] / crypto / x509 / v3_cpols.c
1 /*
2  * Copyright 1999-2020 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 "pcy_local.h"
18 #include "ext_dat.h"
19
20 DEFINE_STACK_OF(CONF_VALUE)
21 DEFINE_STACK_OF(POLICYINFO)
22 DEFINE_STACK_OF(POLICYQUALINFO)
23 DEFINE_STACK_OF(ASN1_INTEGER)
24
25 /* Certificate policies extension support: this one is a bit complex... */
26
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,
32                              int indent);
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);
41
42 const X509V3_EXT_METHOD v3_cpols = {
43     NID_certificate_policies, 0, ASN1_ITEM_ref(CERTIFICATEPOLICIES),
44     0, 0, 0, 0,
45     0, 0,
46     0, 0,
47     (X509V3_EXT_I2R)i2r_certpol,
48     (X509V3_EXT_R2I)r2i_certpol,
49     NULL
50 };
51
52 ASN1_ITEM_TEMPLATE(CERTIFICATEPOLICIES) =
53         ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, CERTIFICATEPOLICIES, POLICYINFO)
54 ASN1_ITEM_TEMPLATE_END(CERTIFICATEPOLICIES)
55
56 IMPLEMENT_ASN1_FUNCTIONS(CERTIFICATEPOLICIES)
57
58 ASN1_SEQUENCE(POLICYINFO) = {
59         ASN1_SIMPLE(POLICYINFO, policyid, ASN1_OBJECT),
60         ASN1_SEQUENCE_OF_OPT(POLICYINFO, qualifiers, POLICYQUALINFO)
61 } ASN1_SEQUENCE_END(POLICYINFO)
62
63 IMPLEMENT_ASN1_FUNCTIONS(POLICYINFO)
64
65 ASN1_ADB_TEMPLATE(policydefault) = ASN1_SIMPLE(POLICYQUALINFO, d.other, ASN1_ANY);
66
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);
71
72 ASN1_SEQUENCE(POLICYQUALINFO) = {
73         ASN1_SIMPLE(POLICYQUALINFO, pqualid, ASN1_OBJECT),
74         ASN1_ADB_OBJECT(POLICYQUALINFO)
75 } ASN1_SEQUENCE_END(POLICYQUALINFO)
76
77 IMPLEMENT_ASN1_FUNCTIONS(POLICYQUALINFO)
78
79 ASN1_SEQUENCE(USERNOTICE) = {
80         ASN1_OPT(USERNOTICE, noticeref, NOTICEREF),
81         ASN1_OPT(USERNOTICE, exptext, DISPLAYTEXT)
82 } ASN1_SEQUENCE_END(USERNOTICE)
83
84 IMPLEMENT_ASN1_FUNCTIONS(USERNOTICE)
85
86 ASN1_SEQUENCE(NOTICEREF) = {
87         ASN1_SIMPLE(NOTICEREF, organization, DISPLAYTEXT),
88         ASN1_SEQUENCE_OF(NOTICEREF, noticenos, ASN1_INTEGER)
89 } ASN1_SEQUENCE_END(NOTICEREF)
90
91 IMPLEMENT_ASN1_FUNCTIONS(NOTICEREF)
92
93 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
94                                          X509V3_CTX *ctx, const char *value)
95 {
96     STACK_OF(POLICYINFO) *pols;
97     char *pstr;
98     POLICYINFO *pol;
99     ASN1_OBJECT *pobj;
100     STACK_OF(CONF_VALUE) *vals = X509V3_parse_list(value);
101     CONF_VALUE *cnf;
102     const int num = sk_CONF_VALUE_num(vals);
103     int i, ia5org;
104
105     if (vals == NULL) {
106         X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_X509V3_LIB);
107         return NULL;
108     }
109
110     pols = sk_POLICYINFO_new_reserve(NULL, num);
111     if (pols == NULL) {
112         X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
113         goto err;
114     }
115
116     ia5org = 0;
117     for (i = 0; i < num; i++) {
118         cnf = sk_CONF_VALUE_value(vals, i);
119
120         if (cnf->value || !cnf->name) {
121             X509V3err(X509V3_F_R2I_CERTPOL,
122                       X509V3_R_INVALID_POLICY_IDENTIFIER);
123             X509V3_conf_err(cnf);
124             goto err;
125         }
126         pstr = cnf->name;
127         if (strcmp(pstr, "ia5org") == 0) {
128             ia5org = 1;
129             continue;
130         } else if (*pstr == '@') {
131             STACK_OF(CONF_VALUE) *polsect;
132
133             polsect = X509V3_get_section(ctx, pstr + 1);
134             if (polsect == NULL) {
135                 X509V3err(X509V3_F_R2I_CERTPOL, X509V3_R_INVALID_SECTION);
136
137                 X509V3_conf_err(cnf);
138                 goto err;
139             }
140             pol = policy_section(ctx, polsect, ia5org);
141             X509V3_section_free(ctx, polsect);
142             if (pol == NULL)
143                 goto err;
144         } else {
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);
149                 goto err;
150             }
151             pol = POLICYINFO_new();
152             if (pol == NULL) {
153                 ASN1_OBJECT_free(pobj);
154                 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
155                 goto err;
156             }
157             pol->policyid = pobj;
158         }
159         if (!sk_POLICYINFO_push(pols, pol)) {
160             POLICYINFO_free(pol);
161             X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
162             goto err;
163         }
164     }
165     sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
166     return pols;
167  err:
168     sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
169     sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
170     return NULL;
171 }
172
173 static POLICYINFO *policy_section(X509V3_CTX *ctx,
174                                   STACK_OF(CONF_VALUE) *polstrs, int ia5org)
175 {
176     int i;
177     CONF_VALUE *cnf;
178     POLICYINFO *pol;
179     POLICYQUALINFO *qual;
180
181     if ((pol = POLICYINFO_new()) == NULL)
182         goto merr;
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) {
186             ASN1_OBJECT *pobj;
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);
191                 goto err;
192             }
193             pol->policyid = pobj;
194
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)
199                 goto merr;
200             if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
201                 goto merr;
202             if ((qual->pqualid = OBJ_nid2obj(NID_id_qt_cps)) == NULL) {
203                 X509V3err(X509V3_F_POLICY_SECTION, ERR_R_INTERNAL_ERROR);
204                 goto err;
205             }
206             if ((qual->d.cpsuri = ASN1_IA5STRING_new()) == NULL)
207                 goto merr;
208             if (!ASN1_STRING_set(qual->d.cpsuri, cnf->value,
209                                  strlen(cnf->value)))
210                 goto merr;
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);
217                 goto err;
218             }
219             unot = X509V3_get_section(ctx, cnf->value + 1);
220             if (!unot) {
221                 X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_SECTION);
222
223                 X509V3_conf_err(cnf);
224                 goto err;
225             }
226             qual = notice_section(ctx, unot, ia5org);
227             X509V3_section_free(ctx, unot);
228             if (!qual)
229                 goto err;
230             if (pol->qualifiers == NULL)
231                 pol->qualifiers = sk_POLICYQUALINFO_new_null();
232             if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
233                 goto merr;
234         } else {
235             X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_OPTION);
236
237             X509V3_conf_err(cnf);
238             goto err;
239         }
240     }
241     if (pol->policyid == NULL) {
242         X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_NO_POLICY_IDENTIFIER);
243         goto err;
244     }
245
246     return pol;
247
248  merr:
249     X509V3err(X509V3_F_POLICY_SECTION, ERR_R_MALLOC_FAILURE);
250
251  err:
252     POLICYINFO_free(pol);
253     return NULL;
254 }
255
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
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 && 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;
285     *tag_len = 0;
286     return V_ASN1_VISIBLESTRING;
287 }
288
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         goto merr;
301     if ((qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice)) == NULL) {
302         X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_INTERNAL_ERROR);
303         goto err;
304     }
305     if ((not = USERNOTICE_new()) == NULL)
306         goto merr;
307     qual->d.usernotice = not;
308     for (i = 0; i < sk_CONF_VALUE_num(unot); i++) {
309         cnf = sk_CONF_VALUE_value(unot, i);
310         value = cnf->value;
311         if (strcmp(cnf->name, "explicitText") == 0) {
312             tag = displaytext_str2tag(value, &tag_len);
313             if ((not->exptext = ASN1_STRING_type_new(tag)) == NULL)
314                 goto merr;
315             if (tag_len != 0)
316                 value += tag_len + 1;
317             len = strlen(value);
318             if (!ASN1_STRING_set(not->exptext, value, len))
319                 goto merr;
320         } else if (strcmp(cnf->name, "organization") == 0) {
321             NOTICEREF *nref;
322             if (!not->noticeref) {
323                 if ((nref = NOTICEREF_new()) == NULL)
324                     goto merr;
325                 not->noticeref = nref;
326             } else
327                 nref = not->noticeref;
328             if (ia5org)
329                 nref->organization->type = V_ASN1_IA5STRING;
330             else
331                 nref->organization->type = V_ASN1_VISIBLESTRING;
332             if (!ASN1_STRING_set(nref->organization, cnf->value,
333                                  strlen(cnf->value)))
334                 goto merr;
335         } else if (strcmp(cnf->name, "noticeNumbers") == 0) {
336             NOTICEREF *nref;
337             STACK_OF(CONF_VALUE) *nos;
338             if (!not->noticeref) {
339                 if ((nref = NOTICEREF_new()) == NULL)
340                     goto merr;
341                 not->noticeref = nref;
342             } else
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);
349                 goto err;
350             }
351             ret = nref_nos(nref->noticenos, nos);
352             sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
353             if (!ret)
354                 goto err;
355         } else {
356             X509V3err(X509V3_F_NOTICE_SECTION, X509V3_R_INVALID_OPTION);
357             X509V3_conf_err(cnf);
358             goto err;
359         }
360     }
361
362     if (not->noticeref &&
363         (!not->noticeref->noticenos || !not->noticeref->organization)) {
364         X509V3err(X509V3_F_NOTICE_SECTION,
365                   X509V3_R_NEED_ORGANIZATION_AND_NUMBERS);
366         goto err;
367     }
368
369     return qual;
370
371  merr:
372     X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_MALLOC_FAILURE);
373
374  err:
375     POLICYQUALINFO_free(qual);
376     return NULL;
377 }
378
379 static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos)
380 {
381     CONF_VALUE *cnf;
382     ASN1_INTEGER *aint;
383
384     int i;
385
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);
390             goto err;
391         }
392         if (!sk_ASN1_INTEGER_push(nnums, aint))
393             goto merr;
394     }
395     return 1;
396
397  merr:
398     ASN1_INTEGER_free(aint);
399     X509V3err(X509V3_F_NREF_NOS, ERR_R_MALLOC_FAILURE);
400
401  err:
402     return 0;
403 }
404
405 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
406                        BIO *out, int indent)
407 {
408     int i;
409     POLICYINFO *pinfo;
410     /* First print out the policy OIDs */
411     for (i = 0; i < sk_POLICYINFO_num(pol); i++) {
412         if (i > 0)
413             BIO_puts(out, "\n");
414         pinfo = sk_POLICYINFO_value(pol, i);
415         BIO_printf(out, "%*sPolicy: ", indent, "");
416         i2a_ASN1_OBJECT(out, pinfo->policyid);
417         if (pinfo->qualifiers) {
418             BIO_puts(out, "\n");
419             print_qualifiers(out, pinfo->qualifiers, indent + 2);
420         }
421     }
422     return 1;
423 }
424
425 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
426                              int indent)
427 {
428     POLICYQUALINFO *qualinfo;
429     int i;
430     for (i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
431         if (i > 0)
432             BIO_puts(out, "\n");
433         qualinfo = sk_POLICYQUALINFO_value(quals, i);
434         switch (OBJ_obj2nid(qualinfo->pqualid)) {
435         case NID_id_qt_cps:
436             BIO_printf(out, "%*sCPS: %s", indent, "",
437                        qualinfo->d.cpsuri->data);
438             break;
439
440         case NID_id_qt_unotice:
441             BIO_printf(out, "%*sUser Notice:\n", indent, "");
442             print_notice(out, qualinfo->d.usernotice, indent + 2);
443             break;
444
445         default:
446             BIO_printf(out, "%*sUnknown Qualifier: ", indent + 2, "");
447
448             i2a_ASN1_OBJECT(out, qualinfo->pqualid);
449             break;
450         }
451     }
452 }
453
454 static void print_notice(BIO *out, USERNOTICE *notice, int indent)
455 {
456     int i;
457     if (notice->noticeref) {
458         NOTICEREF *ref;
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++) {
465             ASN1_INTEGER *num;
466             char *tmp;
467             num = sk_ASN1_INTEGER_value(ref->noticenos, i);
468             if (i)
469                 BIO_puts(out, ", ");
470             if (num == NULL)
471                 BIO_puts(out, "(null)");
472             else {
473                 tmp = i2s_ASN1_INTEGER(NULL, num);
474                 if (tmp == NULL)
475                     return;
476                 BIO_puts(out, tmp);
477                 OPENSSL_free(tmp);
478             }
479         }
480         if (notice->exptext)
481             BIO_puts(out, "\n");
482     }
483     if (notice->exptext)
484         BIO_printf(out, "%*sExplicit Text: %s", indent, "",
485                    notice->exptext->data);
486 }
487
488 void X509_POLICY_NODE_print(BIO *out, X509_POLICY_NODE *node, int indent)
489 {
490     const X509_POLICY_DATA *dat = node->data;
491
492     BIO_printf(out, "%*sPolicy: ", indent, "");
493
494     i2a_ASN1_OBJECT(out, dat->valid_policy);
495     BIO_puts(out, "\n");
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);
500         BIO_puts(out, "\n");
501     }
502     else
503         BIO_printf(out, "%*sNo Qualifiers\n", indent + 2, "");
504 }