explicitText encoding
[openssl.git] / crypto / x509v3 / v3_cpols.c
1 /*
2  * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
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_int.h"
18 #include "ext_dat.h"
19
20 /* Certificate policies extension support: this one is a bit complex... */
21
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,
27                              int indent);
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);
36
37 const X509V3_EXT_METHOD v3_cpols = {
38     NID_certificate_policies, 0, ASN1_ITEM_ref(CERTIFICATEPOLICIES),
39     0, 0, 0, 0,
40     0, 0,
41     0, 0,
42     (X509V3_EXT_I2R)i2r_certpol,
43     (X509V3_EXT_R2I)r2i_certpol,
44     NULL
45 };
46
47 ASN1_ITEM_TEMPLATE(CERTIFICATEPOLICIES) =
48         ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, CERTIFICATEPOLICIES, POLICYINFO)
49 ASN1_ITEM_TEMPLATE_END(CERTIFICATEPOLICIES)
50
51 IMPLEMENT_ASN1_FUNCTIONS(CERTIFICATEPOLICIES)
52
53 ASN1_SEQUENCE(POLICYINFO) = {
54         ASN1_SIMPLE(POLICYINFO, policyid, ASN1_OBJECT),
55         ASN1_SEQUENCE_OF_OPT(POLICYINFO, qualifiers, POLICYQUALINFO)
56 } ASN1_SEQUENCE_END(POLICYINFO)
57
58 IMPLEMENT_ASN1_FUNCTIONS(POLICYINFO)
59
60 ASN1_ADB_TEMPLATE(policydefault) = ASN1_SIMPLE(POLICYQUALINFO, d.other, ASN1_ANY);
61
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);
66
67 ASN1_SEQUENCE(POLICYQUALINFO) = {
68         ASN1_SIMPLE(POLICYQUALINFO, pqualid, ASN1_OBJECT),
69         ASN1_ADB_OBJECT(POLICYQUALINFO)
70 } ASN1_SEQUENCE_END(POLICYQUALINFO)
71
72 IMPLEMENT_ASN1_FUNCTIONS(POLICYQUALINFO)
73
74 ASN1_SEQUENCE(USERNOTICE) = {
75         ASN1_OPT(USERNOTICE, noticeref, NOTICEREF),
76         ASN1_OPT(USERNOTICE, exptext, DISPLAYTEXT)
77 } ASN1_SEQUENCE_END(USERNOTICE)
78
79 IMPLEMENT_ASN1_FUNCTIONS(USERNOTICE)
80
81 ASN1_SEQUENCE(NOTICEREF) = {
82         ASN1_SIMPLE(NOTICEREF, organization, DISPLAYTEXT),
83         ASN1_SEQUENCE_OF(NOTICEREF, noticenos, ASN1_INTEGER)
84 } ASN1_SEQUENCE_END(NOTICEREF)
85
86 IMPLEMENT_ASN1_FUNCTIONS(NOTICEREF)
87
88 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
89                                          X509V3_CTX *ctx, const char *value)
90 {
91     STACK_OF(POLICYINFO) *pols = NULL;
92     char *pstr;
93     POLICYINFO *pol;
94     ASN1_OBJECT *pobj;
95     STACK_OF(CONF_VALUE) *vals;
96     CONF_VALUE *cnf;
97     int i, ia5org;
98     pols = sk_POLICYINFO_new_null();
99     if (pols == NULL) {
100         X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
101         return NULL;
102     }
103     vals = X509V3_parse_list(value);
104     if (vals == NULL) {
105         X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_X509V3_LIB);
106         goto err;
107     }
108     ia5org = 0;
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);
115             goto err;
116         }
117         pstr = cnf->name;
118         if (strcmp(pstr, "ia5org") == 0) {
119             ia5org = 1;
120             continue;
121         } else if (*pstr == '@') {
122             STACK_OF(CONF_VALUE) *polsect;
123             polsect = X509V3_get_section(ctx, pstr + 1);
124             if (!polsect) {
125                 X509V3err(X509V3_F_R2I_CERTPOL, X509V3_R_INVALID_SECTION);
126
127                 X509V3_conf_err(cnf);
128                 goto err;
129             }
130             pol = policy_section(ctx, polsect, ia5org);
131             X509V3_section_free(ctx, polsect);
132             if (pol == NULL)
133                 goto err;
134         } else {
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);
139                 goto err;
140             }
141             pol = POLICYINFO_new();
142             if (pol == NULL) {
143                 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
144                 ASN1_OBJECT_free(pobj);
145                 goto err;
146             }
147             pol->policyid = pobj;
148         }
149         if (!sk_POLICYINFO_push(pols, pol)) {
150             POLICYINFO_free(pol);
151             X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
152             goto err;
153         }
154     }
155     sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
156     return pols;
157  err:
158     sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
159     sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
160     return NULL;
161 }
162
163 static POLICYINFO *policy_section(X509V3_CTX *ctx,
164                                   STACK_OF(CONF_VALUE) *polstrs, int ia5org)
165 {
166     int i;
167     CONF_VALUE *cnf;
168     POLICYINFO *pol;
169     POLICYQUALINFO *qual;
170
171     if ((pol = POLICYINFO_new()) == NULL)
172         goto merr;
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) {
176             ASN1_OBJECT *pobj;
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);
181                 goto err;
182             }
183             pol->policyid = pobj;
184
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)
189                 goto merr;
190             if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
191                 goto merr;
192             if ((qual->pqualid = OBJ_nid2obj(NID_id_qt_cps)) == NULL) {
193                 X509V3err(X509V3_F_POLICY_SECTION, ERR_R_INTERNAL_ERROR);
194                 goto err;
195             }
196             if ((qual->d.cpsuri = ASN1_IA5STRING_new()) == NULL)
197                 goto merr;
198             if (!ASN1_STRING_set(qual->d.cpsuri, cnf->value,
199                                  strlen(cnf->value)))
200                 goto merr;
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);
207                 goto err;
208             }
209             unot = X509V3_get_section(ctx, cnf->value + 1);
210             if (!unot) {
211                 X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_SECTION);
212
213                 X509V3_conf_err(cnf);
214                 goto err;
215             }
216             qual = notice_section(ctx, unot, ia5org);
217             X509V3_section_free(ctx, unot);
218             if (!qual)
219                 goto err;
220             if (!pol->qualifiers)
221                 pol->qualifiers = sk_POLICYQUALINFO_new_null();
222             if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
223                 goto merr;
224         } else {
225             X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_OPTION);
226
227             X509V3_conf_err(cnf);
228             goto err;
229         }
230     }
231     if (!pol->policyid) {
232         X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_NO_POLICY_IDENTIFIER);
233         goto err;
234     }
235
236     return pol;
237
238  merr:
239     X509V3err(X509V3_F_POLICY_SECTION, ERR_R_MALLOC_FAILURE);
240
241  err:
242     POLICYINFO_free(pol);
243     return NULL;
244
245 }
246
247 static int displaytext_get_tag_len(const char *tagstr)
248 {
249     char *colon = strchr(tagstr, ':');
250
251     return (colon == NULL) ? -1 : colon - tagstr;
252 }
253
254 static int displaytext_str2tag(const char *tagstr, unsigned int *tag_len)
255 {
256     int len;
257
258     *tag_len = 0;
259     len = displaytext_get_tag_len(tagstr);
260
261     if (len == -1)
262         return V_ASN1_VISIBLESTRING;
263     *tag_len = len;
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;
276     *tag_len = 0;
277     return V_ASN1_VISIBLESTRING;
278 }
279
280 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
281                                       STACK_OF(CONF_VALUE) *unot, int ia5org)
282 {
283     int i, ret, len, tag;
284     unsigned int tag_len;
285     CONF_VALUE *cnf;
286     USERNOTICE *not;
287     POLICYQUALINFO *qual;
288     char *value = NULL;
289
290     if ((qual = POLICYQUALINFO_new()) == NULL)
291         goto merr;
292     if ((qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice)) == NULL) {
293         X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_INTERNAL_ERROR);
294         goto err;
295     }
296     if ((not = USERNOTICE_new()) == NULL)
297         goto merr;
298     qual->d.usernotice = not;
299     for (i = 0; i < sk_CONF_VALUE_num(unot); i++) {
300         cnf = sk_CONF_VALUE_value(unot, i);
301         value = cnf->value;
302         if (strcmp(cnf->name, "explicitText") == 0) {
303             tag = displaytext_str2tag(value, &tag_len);
304             if ((not->exptext = ASN1_STRING_type_new(tag)) == NULL)
305                 goto merr;
306             if (tag_len != 0)
307                 value += tag_len + 1;
308             len = strlen(value);
309             if (!ASN1_STRING_set(not->exptext, value, len))
310                 goto merr;
311         } else if (strcmp(cnf->name, "organization") == 0) {
312             NOTICEREF *nref;
313             if (!not->noticeref) {
314                 if ((nref = NOTICEREF_new()) == NULL)
315                     goto merr;
316                 not->noticeref = nref;
317             } else
318                 nref = not->noticeref;
319             if (ia5org)
320                 nref->organization->type = V_ASN1_IA5STRING;
321             else
322                 nref->organization->type = V_ASN1_VISIBLESTRING;
323             if (!ASN1_STRING_set(nref->organization, cnf->value,
324                                  strlen(cnf->value)))
325                 goto merr;
326         } else if (strcmp(cnf->name, "noticeNumbers") == 0) {
327             NOTICEREF *nref;
328             STACK_OF(CONF_VALUE) *nos;
329             if (!not->noticeref) {
330                 if ((nref = NOTICEREF_new()) == NULL)
331                     goto merr;
332                 not->noticeref = nref;
333             } else
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);
340                 goto err;
341             }
342             ret = nref_nos(nref->noticenos, nos);
343             sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
344             if (!ret)
345                 goto err;
346         } else {
347             X509V3err(X509V3_F_NOTICE_SECTION, X509V3_R_INVALID_OPTION);
348             X509V3_conf_err(cnf);
349             goto err;
350         }
351     }
352
353     if (not->noticeref &&
354         (!not->noticeref->noticenos || !not->noticeref->organization)) {
355         X509V3err(X509V3_F_NOTICE_SECTION,
356                   X509V3_R_NEED_ORGANIZATION_AND_NUMBERS);
357         goto err;
358     }
359
360     return qual;
361
362  merr:
363     X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_MALLOC_FAILURE);
364
365  err:
366     POLICYQUALINFO_free(qual);
367     return NULL;
368 }
369
370 static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos)
371 {
372     CONF_VALUE *cnf;
373     ASN1_INTEGER *aint;
374
375     int i;
376
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);
381             goto err;
382         }
383         if (!sk_ASN1_INTEGER_push(nnums, aint))
384             goto merr;
385     }
386     return 1;
387
388  merr:
389     ASN1_INTEGER_free(aint);
390     X509V3err(X509V3_F_NREF_NOS, ERR_R_MALLOC_FAILURE);
391
392  err:
393     return 0;
394 }
395
396 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
397                        BIO *out, int indent)
398 {
399     int i;
400     POLICYINFO *pinfo;
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);
406         BIO_puts(out, "\n");
407         if (pinfo->qualifiers)
408             print_qualifiers(out, pinfo->qualifiers, indent + 2);
409     }
410     return 1;
411 }
412
413 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
414                              int indent)
415 {
416     POLICYQUALINFO *qualinfo;
417     int i;
418     for (i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
419         qualinfo = sk_POLICYQUALINFO_value(quals, i);
420         switch (OBJ_obj2nid(qualinfo->pqualid)) {
421         case NID_id_qt_cps:
422             BIO_printf(out, "%*sCPS: %s\n", indent, "",
423                        qualinfo->d.cpsuri->data);
424             break;
425
426         case NID_id_qt_unotice:
427             BIO_printf(out, "%*sUser Notice:\n", indent, "");
428             print_notice(out, qualinfo->d.usernotice, indent + 2);
429             break;
430
431         default:
432             BIO_printf(out, "%*sUnknown Qualifier: ", indent + 2, "");
433
434             i2a_ASN1_OBJECT(out, qualinfo->pqualid);
435             BIO_puts(out, "\n");
436             break;
437         }
438     }
439 }
440
441 static void print_notice(BIO *out, USERNOTICE *notice, int indent)
442 {
443     int i;
444     if (notice->noticeref) {
445         NOTICEREF *ref;
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++) {
452             ASN1_INTEGER *num;
453             char *tmp;
454             num = sk_ASN1_INTEGER_value(ref->noticenos, i);
455             if (i)
456                 BIO_puts(out, ", ");
457             if (num == NULL)
458                 BIO_puts(out, "(null)");
459             else {
460                 tmp = i2s_ASN1_INTEGER(NULL, num);
461                 if (tmp == NULL)
462                     return;
463                 BIO_puts(out, tmp);
464                 OPENSSL_free(tmp);
465             }
466         }
467         BIO_puts(out, "\n");
468     }
469     if (notice->exptext)
470         BIO_printf(out, "%*sExplicit Text: %s\n", indent, "",
471                    notice->exptext->data);
472 }
473
474 void X509_POLICY_NODE_print(BIO *out, X509_POLICY_NODE *node, int indent)
475 {
476     const X509_POLICY_DATA *dat = node->data;
477
478     BIO_printf(out, "%*sPolicy: ", indent, "");
479
480     i2a_ASN1_OBJECT(out, dat->valid_policy);
481     BIO_puts(out, "\n");
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);
486     else
487         BIO_printf(out, "%*sNo Qualifiers\n", indent + 2, "");
488 }