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