check reviewer --reviewer=emilia
[openssl.git] / crypto / x509v3 / v3_cpols.c
1 /*
2  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
3  * 1999.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999-2004 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #include <stdio.h>
60 #include "internal/cryptlib.h"
61 #include <openssl/conf.h>
62 #include <openssl/asn1.h>
63 #include <openssl/asn1t.h>
64 #include <openssl/x509v3.h>
65
66 #include "pcy_int.h"
67 #include "ext_dat.h"
68
69 /* Certificate policies extension support: this one is a bit complex... */
70
71 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
72                        BIO *out, int indent);
73 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
74                                          X509V3_CTX *ctx, char *value);
75 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
76                              int indent);
77 static void print_notice(BIO *out, USERNOTICE *notice, int indent);
78 static POLICYINFO *policy_section(X509V3_CTX *ctx,
79                                   STACK_OF(CONF_VALUE) *polstrs, int ia5org);
80 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
81                                       STACK_OF(CONF_VALUE) *unot, int ia5org);
82 static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos);
83
84 const X509V3_EXT_METHOD v3_cpols = {
85     NID_certificate_policies, 0, ASN1_ITEM_ref(CERTIFICATEPOLICIES),
86     0, 0, 0, 0,
87     0, 0,
88     0, 0,
89     (X509V3_EXT_I2R)i2r_certpol,
90     (X509V3_EXT_R2I)r2i_certpol,
91     NULL
92 };
93
94 ASN1_ITEM_TEMPLATE(CERTIFICATEPOLICIES) =
95         ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, CERTIFICATEPOLICIES, POLICYINFO)
96 ASN1_ITEM_TEMPLATE_END(CERTIFICATEPOLICIES)
97
98 IMPLEMENT_ASN1_FUNCTIONS(CERTIFICATEPOLICIES)
99
100 ASN1_SEQUENCE(POLICYINFO) = {
101         ASN1_SIMPLE(POLICYINFO, policyid, ASN1_OBJECT),
102         ASN1_SEQUENCE_OF_OPT(POLICYINFO, qualifiers, POLICYQUALINFO)
103 } ASN1_SEQUENCE_END(POLICYINFO)
104
105 IMPLEMENT_ASN1_FUNCTIONS(POLICYINFO)
106
107 ASN1_ADB_TEMPLATE(policydefault) = ASN1_SIMPLE(POLICYQUALINFO, d.other, ASN1_ANY);
108
109 ASN1_ADB(POLICYQUALINFO) = {
110         ADB_ENTRY(NID_id_qt_cps, ASN1_SIMPLE(POLICYQUALINFO, d.cpsuri, ASN1_IA5STRING)),
111         ADB_ENTRY(NID_id_qt_unotice, ASN1_SIMPLE(POLICYQUALINFO, d.usernotice, USERNOTICE))
112 } ASN1_ADB_END(POLICYQUALINFO, 0, pqualid, 0, &policydefault_tt, NULL);
113
114 ASN1_SEQUENCE(POLICYQUALINFO) = {
115         ASN1_SIMPLE(POLICYQUALINFO, pqualid, ASN1_OBJECT),
116         ASN1_ADB_OBJECT(POLICYQUALINFO)
117 } ASN1_SEQUENCE_END(POLICYQUALINFO)
118
119 IMPLEMENT_ASN1_FUNCTIONS(POLICYQUALINFO)
120
121 ASN1_SEQUENCE(USERNOTICE) = {
122         ASN1_OPT(USERNOTICE, noticeref, NOTICEREF),
123         ASN1_OPT(USERNOTICE, exptext, DISPLAYTEXT)
124 } ASN1_SEQUENCE_END(USERNOTICE)
125
126 IMPLEMENT_ASN1_FUNCTIONS(USERNOTICE)
127
128 ASN1_SEQUENCE(NOTICEREF) = {
129         ASN1_SIMPLE(NOTICEREF, organization, DISPLAYTEXT),
130         ASN1_SEQUENCE_OF(NOTICEREF, noticenos, ASN1_INTEGER)
131 } ASN1_SEQUENCE_END(NOTICEREF)
132
133 IMPLEMENT_ASN1_FUNCTIONS(NOTICEREF)
134
135 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
136                                          X509V3_CTX *ctx, char *value)
137 {
138     STACK_OF(POLICYINFO) *pols = NULL;
139     char *pstr;
140     POLICYINFO *pol;
141     ASN1_OBJECT *pobj;
142     STACK_OF(CONF_VALUE) *vals;
143     CONF_VALUE *cnf;
144     int i, ia5org;
145     pols = sk_POLICYINFO_new_null();
146     if (pols == NULL) {
147         X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
148         return NULL;
149     }
150     vals = X509V3_parse_list(value);
151     if (vals == NULL) {
152         X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_X509V3_LIB);
153         goto err;
154     }
155     ia5org = 0;
156     for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
157         cnf = sk_CONF_VALUE_value(vals, i);
158         if (cnf->value || !cnf->name) {
159             X509V3err(X509V3_F_R2I_CERTPOL,
160                       X509V3_R_INVALID_POLICY_IDENTIFIER);
161             X509V3_conf_err(cnf);
162             goto err;
163         }
164         pstr = cnf->name;
165         if (strcmp(pstr, "ia5org") == 0) {
166             ia5org = 1;
167             continue;
168         } else if (*pstr == '@') {
169             STACK_OF(CONF_VALUE) *polsect;
170             polsect = X509V3_get_section(ctx, pstr + 1);
171             if (!polsect) {
172                 X509V3err(X509V3_F_R2I_CERTPOL, X509V3_R_INVALID_SECTION);
173
174                 X509V3_conf_err(cnf);
175                 goto err;
176             }
177             pol = policy_section(ctx, polsect, ia5org);
178             X509V3_section_free(ctx, polsect);
179             if (pol == NULL)
180                 goto err;
181         } else {
182             if ((pobj = OBJ_txt2obj(cnf->name, 0)) == NULL) {
183                 X509V3err(X509V3_F_R2I_CERTPOL,
184                           X509V3_R_INVALID_OBJECT_IDENTIFIER);
185                 X509V3_conf_err(cnf);
186                 goto err;
187             }
188             pol = POLICYINFO_new();
189             if (pol == NULL) {
190                 X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
191                 goto err;
192             }
193             pol->policyid = pobj;
194         }
195         if (!sk_POLICYINFO_push(pols, pol)) {
196             POLICYINFO_free(pol);
197             X509V3err(X509V3_F_R2I_CERTPOL, ERR_R_MALLOC_FAILURE);
198             goto err;
199         }
200     }
201     sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
202     return pols;
203  err:
204     sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
205     sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
206     return NULL;
207 }
208
209 static POLICYINFO *policy_section(X509V3_CTX *ctx,
210                                   STACK_OF(CONF_VALUE) *polstrs, int ia5org)
211 {
212     int i;
213     CONF_VALUE *cnf;
214     POLICYINFO *pol;
215     POLICYQUALINFO *qual;
216
217     if ((pol = POLICYINFO_new()) == NULL)
218         goto merr;
219     for (i = 0; i < sk_CONF_VALUE_num(polstrs); i++) {
220         cnf = sk_CONF_VALUE_value(polstrs, i);
221         if (strcmp(cnf->name, "policyIdentifier") == 0) {
222             ASN1_OBJECT *pobj;
223             if ((pobj = OBJ_txt2obj(cnf->value, 0)) == NULL) {
224                 X509V3err(X509V3_F_POLICY_SECTION,
225                           X509V3_R_INVALID_OBJECT_IDENTIFIER);
226                 X509V3_conf_err(cnf);
227                 goto err;
228             }
229             pol->policyid = pobj;
230
231         } else if (!name_cmp(cnf->name, "CPS")) {
232             if (pol->qualifiers == NULL)
233                 pol->qualifiers = sk_POLICYQUALINFO_new_null();
234             if ((qual = POLICYQUALINFO_new()) == NULL)
235                 goto merr;
236             if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
237                 goto merr;
238             if ((qual->pqualid = OBJ_nid2obj(NID_id_qt_cps)) == NULL) {
239                 X509V3err(X509V3_F_POLICY_SECTION, ERR_R_INTERNAL_ERROR);
240                 goto err;
241             }
242             if ((qual->d.cpsuri = ASN1_IA5STRING_new()) == NULL)
243                 goto merr;
244             if (!ASN1_STRING_set(qual->d.cpsuri, cnf->value,
245                                  strlen(cnf->value)))
246                 goto merr;
247         } else if (!name_cmp(cnf->name, "userNotice")) {
248             STACK_OF(CONF_VALUE) *unot;
249             if (*cnf->value != '@') {
250                 X509V3err(X509V3_F_POLICY_SECTION,
251                           X509V3_R_EXPECTED_A_SECTION_NAME);
252                 X509V3_conf_err(cnf);
253                 goto err;
254             }
255             unot = X509V3_get_section(ctx, cnf->value + 1);
256             if (!unot) {
257                 X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_SECTION);
258
259                 X509V3_conf_err(cnf);
260                 goto err;
261             }
262             qual = notice_section(ctx, unot, ia5org);
263             X509V3_section_free(ctx, unot);
264             if (!qual)
265                 goto err;
266             if (!pol->qualifiers)
267                 pol->qualifiers = sk_POLICYQUALINFO_new_null();
268             if (!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
269                 goto merr;
270         } else {
271             X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_INVALID_OPTION);
272
273             X509V3_conf_err(cnf);
274             goto err;
275         }
276     }
277     if (!pol->policyid) {
278         X509V3err(X509V3_F_POLICY_SECTION, X509V3_R_NO_POLICY_IDENTIFIER);
279         goto err;
280     }
281
282     return pol;
283
284  merr:
285     X509V3err(X509V3_F_POLICY_SECTION, ERR_R_MALLOC_FAILURE);
286
287  err:
288     POLICYINFO_free(pol);
289     return NULL;
290
291 }
292
293 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
294                                       STACK_OF(CONF_VALUE) *unot, int ia5org)
295 {
296     int i, ret;
297     CONF_VALUE *cnf;
298     USERNOTICE *not;
299     POLICYQUALINFO *qual;
300
301     if ((qual = POLICYQUALINFO_new()) == NULL)
302         goto merr;
303     if ((qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice)) == NULL) {
304         X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_INTERNAL_ERROR);
305         goto err;
306     }
307     if ((not = USERNOTICE_new()) == NULL)
308         goto merr;
309     qual->d.usernotice = not;
310     for (i = 0; i < sk_CONF_VALUE_num(unot); i++) {
311         cnf = sk_CONF_VALUE_value(unot, i);
312         if (strcmp(cnf->name, "explicitText") == 0) {
313             if ((not->exptext = ASN1_VISIBLESTRING_new()) == NULL)
314                 goto merr;
315             if (!ASN1_STRING_set(not->exptext, cnf->value,
316                                  strlen(cnf->value)))
317                 goto merr;
318         } else if (strcmp(cnf->name, "organization") == 0) {
319             NOTICEREF *nref;
320             if (!not->noticeref) {
321                 if ((nref = NOTICEREF_new()) == NULL)
322                     goto merr;
323                 not->noticeref = nref;
324             } else
325                 nref = not->noticeref;
326             if (ia5org)
327                 nref->organization->type = V_ASN1_IA5STRING;
328             else
329                 nref->organization->type = V_ASN1_VISIBLESTRING;
330             if (!ASN1_STRING_set(nref->organization, cnf->value,
331                                  strlen(cnf->value)))
332                 goto merr;
333         } else if (strcmp(cnf->name, "noticeNumbers") == 0) {
334             NOTICEREF *nref;
335             STACK_OF(CONF_VALUE) *nos;
336             if (!not->noticeref) {
337                 if ((nref = NOTICEREF_new()) == NULL)
338                     goto merr;
339                 not->noticeref = nref;
340             } else
341                 nref = not->noticeref;
342             nos = X509V3_parse_list(cnf->value);
343             if (!nos || !sk_CONF_VALUE_num(nos)) {
344                 X509V3err(X509V3_F_NOTICE_SECTION, X509V3_R_INVALID_NUMBERS);
345                 X509V3_conf_err(cnf);
346                 goto err;
347             }
348             ret = nref_nos(nref->noticenos, nos);
349             sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
350             if (!ret)
351                 goto err;
352         } else {
353             X509V3err(X509V3_F_NOTICE_SECTION, X509V3_R_INVALID_OPTION);
354             X509V3_conf_err(cnf);
355             goto err;
356         }
357     }
358
359     if (not->noticeref &&
360         (!not->noticeref->noticenos || !not->noticeref->organization)) {
361         X509V3err(X509V3_F_NOTICE_SECTION,
362                   X509V3_R_NEED_ORGANIZATION_AND_NUMBERS);
363         goto err;
364     }
365
366     return qual;
367
368  merr:
369     X509V3err(X509V3_F_NOTICE_SECTION, ERR_R_MALLOC_FAILURE);
370
371  err:
372     POLICYQUALINFO_free(qual);
373     return NULL;
374 }
375
376 static int nref_nos(STACK_OF(ASN1_INTEGER) *nnums, STACK_OF(CONF_VALUE) *nos)
377 {
378     CONF_VALUE *cnf;
379     ASN1_INTEGER *aint;
380
381     int i;
382
383     for (i = 0; i < sk_CONF_VALUE_num(nos); i++) {
384         cnf = sk_CONF_VALUE_value(nos, i);
385         if ((aint = s2i_ASN1_INTEGER(NULL, cnf->name)) == NULL) {
386             X509V3err(X509V3_F_NREF_NOS, X509V3_R_INVALID_NUMBER);
387             goto err;
388         }
389         if (!sk_ASN1_INTEGER_push(nnums, aint))
390             goto merr;
391     }
392     return 1;
393
394  merr:
395     X509V3err(X509V3_F_NREF_NOS, ERR_R_MALLOC_FAILURE);
396
397  err:
398     sk_ASN1_INTEGER_pop_free(nnums, ASN1_STRING_free);
399     return 0;
400 }
401
402 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
403                        BIO *out, int indent)
404 {
405     int i;
406     POLICYINFO *pinfo;
407     /* First print out the policy OIDs */
408     for (i = 0; i < sk_POLICYINFO_num(pol); i++) {
409         pinfo = sk_POLICYINFO_value(pol, i);
410         BIO_printf(out, "%*sPolicy: ", indent, "");
411         i2a_ASN1_OBJECT(out, pinfo->policyid);
412         BIO_puts(out, "\n");
413         if (pinfo->qualifiers)
414             print_qualifiers(out, pinfo->qualifiers, indent + 2);
415     }
416     return 1;
417 }
418
419 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
420                              int indent)
421 {
422     POLICYQUALINFO *qualinfo;
423     int i;
424     for (i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
425         qualinfo = sk_POLICYQUALINFO_value(quals, i);
426         switch (OBJ_obj2nid(qualinfo->pqualid)) {
427         case NID_id_qt_cps:
428             BIO_printf(out, "%*sCPS: %s\n", indent, "",
429                        qualinfo->d.cpsuri->data);
430             break;
431
432         case NID_id_qt_unotice:
433             BIO_printf(out, "%*sUser Notice:\n", indent, "");
434             print_notice(out, qualinfo->d.usernotice, indent + 2);
435             break;
436
437         default:
438             BIO_printf(out, "%*sUnknown Qualifier: ", indent + 2, "");
439
440             i2a_ASN1_OBJECT(out, qualinfo->pqualid);
441             BIO_puts(out, "\n");
442             break;
443         }
444     }
445 }
446
447 static void print_notice(BIO *out, USERNOTICE *notice, int indent)
448 {
449     int i;
450     if (notice->noticeref) {
451         NOTICEREF *ref;
452         ref = notice->noticeref;
453         BIO_printf(out, "%*sOrganization: %s\n", indent, "",
454                    ref->organization->data);
455         BIO_printf(out, "%*sNumber%s: ", indent, "",
456                    sk_ASN1_INTEGER_num(ref->noticenos) > 1 ? "s" : "");
457         for (i = 0; i < sk_ASN1_INTEGER_num(ref->noticenos); i++) {
458             ASN1_INTEGER *num;
459             char *tmp;
460             num = sk_ASN1_INTEGER_value(ref->noticenos, i);
461             if (i)
462                 BIO_puts(out, ", ");
463             tmp = i2s_ASN1_INTEGER(NULL, num);
464             BIO_puts(out, tmp);
465             OPENSSL_free(tmp);
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 }