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