47e08c8fb8f19ccaa2451d5bd8ffcb67631473c2
[openssl.git] / crypto / x509v3 / v3_cpols.c
1 /* v3_cpols.c */
2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3  * project 1999.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999 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 "cryptlib.h"
61 #include <openssl/conf.h>
62 #include <openssl/asn1.h>
63 #include <openssl/asn1t.h>
64 #include <openssl/x509v3.h>
65
66 /* Certificate policies extension support: this one is a bit complex... */
67
68 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol, BIO *out, int indent);
69 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, char *value);
70 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals, int indent);
71 static void print_notice(BIO *out, USERNOTICE *notice, int indent);
72 static POLICYINFO *policy_section(X509V3_CTX *ctx,
73                                  STACK_OF(CONF_VALUE) *polstrs, int ia5org);
74 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
75                                         STACK_OF(CONF_VALUE) *unot, int ia5org);
76 static STACK_OF(ASN1_INTEGER) *nref_nos(STACK_OF(CONF_VALUE) *nos);
77
78 X509V3_EXT_METHOD v3_cpols = {
79 NID_certificate_policies, 0,
80 (X509V3_EXT_NEW)CERTIFICATEPOLICIES_new,
81 (X509V3_EXT_FREE)CERTIFICATEPOLICIES_free,
82 (X509V3_EXT_D2I)d2i_CERTIFICATEPOLICIES,
83 (X509V3_EXT_I2D)i2d_CERTIFICATEPOLICIES,
84 NULL, NULL,
85 NULL, NULL,
86 (X509V3_EXT_I2R)i2r_certpol,
87 (X509V3_EXT_R2I)r2i_certpol,
88 NULL
89 };
90
91 ASN1_ITEM_TEMPLATE(CERTIFICATEPOLICIES) = 
92         ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, CERTIFICATEPOLICIES, POLICYINFO)
93 ASN1_ITEM_TEMPLATE_END(CERTIFICATEPOLICIES);
94
95 IMPLEMENT_ASN1_FUNCTIONS(CERTIFICATEPOLICIES)
96
97 ASN1_SEQUENCE(POLICYINFO) = {
98         ASN1_SIMPLE(POLICYINFO, policyid, ASN1_OBJECT),
99         ASN1_SEQUENCE_OF_OPT(POLICYINFO, qualifiers, POLICYQUALINFO)
100 } ASN1_SEQUENCE_END(POLICYINFO);
101
102 IMPLEMENT_ASN1_FUNCTIONS(POLICYINFO)
103
104 ASN1_ADB_TEMPLATE(policydefault) = ASN1_SIMPLE(POLICYQUALINFO, d.other, ASN1_ANY);
105
106 ASN1_ADB(POLICYQUALINFO) = {
107         ADB_ENTRY(NID_id_qt_cps, ASN1_SIMPLE(POLICYQUALINFO, d.cpsuri, ASN1_IA5STRING)),
108         ADB_ENTRY(NID_id_qt_unotice, ASN1_SIMPLE(POLICYQUALINFO, d.usernotice, USERNOTICE))
109 } ASN1_ADB_END(POLICYQUALINFO, 0, pqualid, 0, &policydefault_tt, NULL);
110
111 ASN1_SEQUENCE(POLICYQUALINFO) = {
112         ASN1_SIMPLE(POLICYQUALINFO, pqualid, ASN1_OBJECT),
113         ASN1_ADB_OBJECT(POLICYQUALINFO)
114 } ASN1_SEQUENCE_END(POLICYQUALINFO);
115
116 IMPLEMENT_ASN1_FUNCTIONS(POLICYQUALINFO)
117
118 ASN1_SEQUENCE(USERNOTICE) = {
119         ASN1_OPT(USERNOTICE, noticeref, NOTICEREF),
120         ASN1_OPT(USERNOTICE, exptext, DISPLAYTEXT)
121 } ASN1_SEQUENCE_END(USERNOTICE);
122
123 IMPLEMENT_ASN1_FUNCTIONS(USERNOTICE)
124
125 ASN1_SEQUENCE(NOTICEREF) = {
126         ASN1_SIMPLE(NOTICEREF, organization, DISPLAYTEXT),
127         ASN1_SEQUENCE_OF(NOTICEREF, noticenos, ASN1_INTEGER)
128 } ASN1_SEQUENCE_END(NOTICEREF);
129
130 IMPLEMENT_ASN1_FUNCTIONS(NOTICEREF)
131
132 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
133                 X509V3_CTX *ctx, char *value)
134 {
135         STACK_OF(POLICYINFO) *pols = NULL;
136         char *pstr;
137         POLICYINFO *pol;
138         ASN1_OBJECT *pobj;
139         STACK_OF(CONF_VALUE) *vals;
140         CONF_VALUE *cnf;
141         int i, ia5org;
142         pols = sk_POLICYINFO_new_null();
143         vals =  X509V3_parse_list(value);
144         ia5org = 0;
145         for(i = 0; i < sk_CONF_VALUE_num(vals); i++) {
146                 cnf = sk_CONF_VALUE_value(vals, i);
147                 if(cnf->value || !cnf->name ) {
148                         X509V3err(X509V3_F_R2I_CERTPOL,X509V3_R_INVALID_POLICY_IDENTIFIER);
149                         X509V3_conf_err(cnf);
150                         goto err;
151                 }
152                 pstr = cnf->name;
153                 if(!strcmp(pstr,"ia5org")) {
154                         ia5org = 1;
155                         continue;
156                 } else if(*pstr == '@') {
157                         STACK_OF(CONF_VALUE) *polsect;
158                         polsect = X509V3_get_section(ctx, pstr + 1);
159                         if(!polsect) {
160                                 X509V3err(X509V3_F_R2I_CERTPOL,X509V3_R_INVALID_SECTION);
161
162                                 X509V3_conf_err(cnf);
163                                 goto err;
164                         }
165                         pol = policy_section(ctx, polsect, ia5org);
166                         X509V3_section_free(ctx, polsect);
167                         if(!pol) goto err;
168                 } else {
169                         if(!(pobj = OBJ_txt2obj(cnf->name, 0))) {
170                                 X509V3err(X509V3_F_R2I_CERTPOL,X509V3_R_INVALID_OBJECT_IDENTIFIER);
171                                 X509V3_conf_err(cnf);
172                                 goto err;
173                         }
174                         pol = POLICYINFO_new();
175                         pol->policyid = pobj;
176                 }
177                 sk_POLICYINFO_push(pols, pol);
178         }
179         sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
180         return pols;
181         err:
182         sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
183         return NULL;
184 }
185
186 static POLICYINFO *policy_section(X509V3_CTX *ctx,
187                                 STACK_OF(CONF_VALUE) *polstrs, int ia5org)
188 {
189         int i;
190         CONF_VALUE *cnf;
191         POLICYINFO *pol;
192         POLICYQUALINFO *qual;
193         if(!(pol = POLICYINFO_new())) goto merr;
194         for(i = 0; i < sk_CONF_VALUE_num(polstrs); i++) {
195                 cnf = sk_CONF_VALUE_value(polstrs, i);
196                 if(!strcmp(cnf->name, "policyIdentifier")) {
197                         ASN1_OBJECT *pobj;
198                         if(!(pobj = OBJ_txt2obj(cnf->value, 0))) {
199                                 X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_INVALID_OBJECT_IDENTIFIER);
200                                 X509V3_conf_err(cnf);
201                                 goto err;
202                         }
203                         pol->policyid = pobj;
204
205                 } else if(!name_cmp(cnf->name, "CPS")) {
206                         if(!pol->qualifiers) pol->qualifiers =
207                                                  sk_POLICYQUALINFO_new_null();
208                         if(!(qual = POLICYQUALINFO_new())) goto merr;
209                         if(!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
210                                                                  goto merr;
211                         qual->pqualid = OBJ_nid2obj(NID_id_qt_cps);
212                         qual->d.cpsuri = M_ASN1_IA5STRING_new();
213                         if(!ASN1_STRING_set(qual->d.cpsuri, cnf->value,
214                                                  strlen(cnf->value))) goto merr;
215                 } else if(!name_cmp(cnf->name, "userNotice")) {
216                         STACK_OF(CONF_VALUE) *unot;
217                         if(*cnf->value != '@') {
218                                 X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_EXPECTED_A_SECTION_NAME);
219                                 X509V3_conf_err(cnf);
220                                 goto err;
221                         }
222                         unot = X509V3_get_section(ctx, cnf->value + 1);
223                         if(!unot) {
224                                 X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_INVALID_SECTION);
225
226                                 X509V3_conf_err(cnf);
227                                 goto err;
228                         }
229                         qual = notice_section(ctx, unot, ia5org);
230                         X509V3_section_free(ctx, unot);
231                         if(!qual) goto err;
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) {
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 }
257
258 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx,
259                                         STACK_OF(CONF_VALUE) *unot, int ia5org)
260 {
261         int i;
262         CONF_VALUE *cnf;
263         USERNOTICE *not;
264         POLICYQUALINFO *qual;
265         if(!(qual = POLICYQUALINFO_new())) goto merr;
266         qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice);
267         if(!(not = USERNOTICE_new())) goto merr;
268         qual->d.usernotice = not;
269         for(i = 0; i < sk_CONF_VALUE_num(unot); i++) {
270                 cnf = sk_CONF_VALUE_value(unot, i);
271                 if(!strcmp(cnf->name, "explicitText")) {
272                         not->exptext = M_ASN1_VISIBLESTRING_new();
273                         if(!ASN1_STRING_set(not->exptext, cnf->value,
274                                                  strlen(cnf->value))) goto merr;
275                 } else if(!strcmp(cnf->name, "organization")) {
276                         NOTICEREF *nref;
277                         if(!not->noticeref) {
278                                 if(!(nref = NOTICEREF_new())) goto merr;
279                                 not->noticeref = nref;
280                         } else nref = not->noticeref;
281                         if(ia5org) nref->organization = M_ASN1_IA5STRING_new();
282                         else nref->organization = M_ASN1_VISIBLESTRING_new();
283                         if(!ASN1_STRING_set(nref->organization, cnf->value,
284                                                  strlen(cnf->value))) goto merr;
285                 } else if(!strcmp(cnf->name, "noticeNumbers")) {
286                         NOTICEREF *nref;
287                         STACK_OF(CONF_VALUE) *nos;
288                         if(!not->noticeref) {
289                                 if(!(nref = NOTICEREF_new())) goto merr;
290                                 not->noticeref = nref;
291                         } else nref = not->noticeref;
292                         nos = X509V3_parse_list(cnf->value);
293                         if(!nos || !sk_CONF_VALUE_num(nos)) {
294                                 X509V3err(X509V3_F_NOTICE_SECTION,X509V3_R_INVALID_NUMBERS);
295                                 X509V3_conf_err(cnf);
296                                 goto err;
297                         }
298                         nref->noticenos = nref_nos(nos);
299                         sk_CONF_VALUE_pop_free(nos, X509V3_conf_free);
300                         if(!nref->noticenos) goto err;
301                 } else {
302                         X509V3err(X509V3_F_NOTICE_SECTION,X509V3_R_INVALID_OPTION);
303
304                         X509V3_conf_err(cnf);
305                         goto err;
306                 }
307         }
308
309         if(not->noticeref && 
310               (!not->noticeref->noticenos || !not->noticeref->organization)) {
311                         X509V3err(X509V3_F_NOTICE_SECTION,X509V3_R_NEED_ORGANIZATION_AND_NUMBERS);
312                         goto err;
313         }
314
315         return qual;
316
317         merr:
318         X509V3err(X509V3_F_NOTICE_SECTION,ERR_R_MALLOC_FAILURE);
319
320         err:
321         POLICYQUALINFO_free(qual);
322         return NULL;
323 }
324
325 static STACK_OF(ASN1_INTEGER) *nref_nos(STACK_OF(CONF_VALUE) *nos)
326 {
327         STACK_OF(ASN1_INTEGER) *nnums;
328         CONF_VALUE *cnf;
329         ASN1_INTEGER *aint;
330
331         int i;
332
333         if(!(nnums = sk_ASN1_INTEGER_new_null())) goto merr;
334         for(i = 0; i < sk_CONF_VALUE_num(nos); i++) {
335                 cnf = sk_CONF_VALUE_value(nos, i);
336                 if(!(aint = s2i_ASN1_INTEGER(NULL, cnf->name))) {
337                         X509V3err(X509V3_F_NREF_NOS,X509V3_R_INVALID_NUMBER);
338                         goto err;
339                 }
340                 if(!sk_ASN1_INTEGER_push(nnums, aint)) goto merr;
341         }
342         return nnums;
343
344         merr:
345         X509V3err(X509V3_F_NOTICE_SECTION,ERR_R_MALLOC_FAILURE);
346
347         err:
348         sk_ASN1_INTEGER_pop_free(nnums, ASN1_STRING_free);
349         return NULL;
350 }
351
352
353 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
354                 BIO *out, int indent)
355 {
356         int i;
357         POLICYINFO *pinfo;
358         /* First print out the policy OIDs */
359         for(i = 0; i < sk_POLICYINFO_num(pol); i++) {
360                 pinfo = sk_POLICYINFO_value(pol, i);
361                 BIO_printf(out, "%*sPolicy: ", indent, "");
362                 i2a_ASN1_OBJECT(out, pinfo->policyid);
363                 BIO_puts(out, "\n");
364                 if(pinfo->qualifiers)
365                          print_qualifiers(out, pinfo->qualifiers, indent + 2);
366         }
367         return 1;
368 }
369
370 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
371                 int indent)
372 {
373         POLICYQUALINFO *qualinfo;
374         int i;
375         for(i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
376                 qualinfo = sk_POLICYQUALINFO_value(quals, i);
377                 switch(OBJ_obj2nid(qualinfo->pqualid))
378                 {
379                         case NID_id_qt_cps:
380                         BIO_printf(out, "%*sCPS: %s\n", indent, "",
381                                                 qualinfo->d.cpsuri->data);
382                         break;
383                 
384                         case NID_id_qt_unotice:
385                         BIO_printf(out, "%*sUser Notice:\n", indent, "");
386                         print_notice(out, qualinfo->d.usernotice, indent + 2);
387                         break;
388
389                         default:
390                         BIO_printf(out, "%*sUnknown Qualifier: ",
391                                                          indent + 2, "");
392                         
393                         i2a_ASN1_OBJECT(out, qualinfo->pqualid);
394                         BIO_puts(out, "\n");
395                         break;
396                 }
397         }
398 }
399
400 static void print_notice(BIO *out, USERNOTICE *notice, int indent)
401 {
402         int i;
403         if(notice->noticeref) {
404                 NOTICEREF *ref;
405                 ref = notice->noticeref;
406                 BIO_printf(out, "%*sOrganization: %s\n", indent, "",
407                                                  ref->organization->data);
408                 BIO_printf(out, "%*sNumber%s: ", indent, "",
409                            sk_ASN1_INTEGER_num(ref->noticenos) > 1 ? "s" : "");
410                 for(i = 0; i < sk_ASN1_INTEGER_num(ref->noticenos); i++) {
411                         ASN1_INTEGER *num;
412                         char *tmp;
413                         num = sk_ASN1_INTEGER_value(ref->noticenos, i);
414                         if(i) BIO_puts(out, ", ");
415                         tmp = i2s_ASN1_INTEGER(NULL, num);
416                         BIO_puts(out, tmp);
417                         OPENSSL_free(tmp);
418                 }
419                 BIO_puts(out, "\n");
420         }
421         if(notice->exptext)
422                 BIO_printf(out, "%*sExplicit Text: %s\n", indent, "",
423                                                          notice->exptext->data);
424 }
425