c9340046d71f834d4f7cc01bd7c4529ebd17b57a
[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 "conf.h"
62 #include "asn1.h"
63 #include "asn1_mac.h"
64 #include "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, STACK *polstrs);
73
74 X509V3_EXT_METHOD v3_cpols = {
75 NID_certificate_policies, 0,
76 (X509V3_EXT_NEW)CERTIFICATEPOLICIES_new,
77 CERTIFICATEPOLICIES_free,
78 (X509V3_EXT_D2I)d2i_CERTIFICATEPOLICIES,
79 i2d_CERTIFICATEPOLICIES,
80 NULL, NULL,
81 NULL, NULL,
82 (X509V3_EXT_I2R)i2r_certpol,
83 (X509V3_EXT_R2I)r2i_certpol,
84 NULL
85 };
86
87
88 /*
89  * ASN1err(ASN1_F_POLICYINFO_NEW,ERR_R_MALLOC_FAILURE);
90  * ASN1err(ASN1_F_D2I_POLICYINFO,ERR_R_MALLOC_FAILURE);
91  * ASN1err(ASN1_F_POLICYQUALINFO_NEW,ERR_R_MALLOC_FAILURE);
92  * ASN1err(ASN1_F_D2I_POLICYQUALINFO,ERR_R_MALLOC_FAILURE);
93  * ASN1err(ASN1_F_USERNOTICE_NEW,ERR_R_MALLOC_FAILURE);
94  * ASN1err(ASN1_F_D2I_USERNOTICE,ERR_R_MALLOC_FAILURE);
95  * ASN1err(ASN1_F_NOTICEREF_NEW,ERR_R_MALLOC_FAILURE);
96  * ASN1err(ASN1_F_D2I_NOTICEREF,ERR_R_MALLOC_FAILURE);
97  */
98
99 static STACK_OF(POLICYINFO) *r2i_certpol(method, ctx, value)
100 X509V3_EXT_METHOD *method;
101 X509V3_CTX *ctx;
102 char *value;
103 {
104         STACK_OF(POLICYINFO) *pols = NULL;
105         char *pstr;
106         POLICYINFO *pol;
107         ASN1_OBJECT *pobj;
108         STACK *vals;
109         CONF_VALUE *cnf;
110         int i;
111         pols = sk_POLICYINFO_new_null();
112         vals =  X509V3_parse_list(value);
113         for(i = 0; i < sk_num(vals); i++) {
114                 cnf = (CONF_VALUE *)sk_value(vals, i);
115                 if(cnf->value || !cnf->name ) {
116                         X509V3err(X509V3_F_R2I_CERTPOL,X509V3_R_INVALID_POLICY_IDENTIFIER);
117                         X509V3_conf_err(cnf);
118                         goto err;
119                 }
120                 pstr = cnf->name;
121                 if(*pstr == '@') {
122                         STACK *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);
131                         X509V3_section_free(ctx, polsect);
132                         if(!pol) goto err;
133                 } else {
134                         if(!(pobj = OBJ_txt2obj(cnf->name, 0))) {
135                                 X509V3err(X509V3_F_R2I_CERTPOL,X509V3_R_INVALID_OBJECT_IDENTIFIER);
136                                 X509V3_conf_err(cnf);
137                                 goto err;
138                         }
139                         pol = POLICYINFO_new();
140                         pol->policyid = pobj;
141                 }
142                 sk_POLICYINFO_push(pols, pol);
143         }
144         sk_pop_free(vals, X509V3_conf_free);
145         return pols;
146         err:
147         sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
148         return NULL;
149 }
150
151 static POLICYINFO *policy_section(ctx, polstrs)
152 X509V3_CTX *ctx;
153 STACK *polstrs;
154 {
155         int i;
156         CONF_VALUE *cnf;
157         for(i = 0; i < sk_num(polstrs); i++) {
158                 cnf = (CONF_VALUE *)sk_value(polstrs, i);
159         }
160         return NULL;
161 }
162
163
164 static int i2r_certpol(method, pol, out, indent)
165 X509V3_EXT_METHOD *method;
166 STACK_OF(POLICYINFO) *pol;
167 BIO *out;
168 int indent;
169 {
170         int i;
171         POLICYINFO *pinfo;
172         /* First print out the policy OIDs */
173         for(i = 0; i < sk_POLICYINFO_num(pol); i++) {
174                 pinfo = sk_POLICYINFO_value(pol, i);
175                 BIO_printf(out, "%*sPolicy: ", indent, "");
176                 i2a_ASN1_OBJECT(out, pinfo->policyid);
177                 BIO_puts(out, "\n");
178                 if(pinfo->qualifiers)
179                          print_qualifiers(out, pinfo->qualifiers, indent + 2);
180         }
181         return 1;
182 }
183
184
185 int i2d_CERTIFICATEPOLICIES(a, pp)
186 STACK_OF(POLICYINFO) *a;
187 unsigned char **pp;
188 {
189
190 return i2d_ASN1_SET_OF_POLICYINFO(a, pp, i2d_POLICYINFO, V_ASN1_SEQUENCE,
191                                                  V_ASN1_UNIVERSAL, IS_SEQUENCE);}
192
193 STACK_OF(POLICYINFO) *CERTIFICATEPOLICIES_new()
194 {
195         return sk_POLICYINFO_new_null();
196 }
197
198 void CERTIFICATEPOLICIES_free(a)
199 STACK_OF(POLICYINFO) *a;
200 {
201         sk_POLICYINFO_pop_free(a, POLICYINFO_free);
202 }
203
204 STACK_OF(POLICYINFO) *d2i_CERTIFICATEPOLICIES(a,pp,length)
205 STACK_OF(POLICYINFO) **a;
206 unsigned char **pp;
207 long length;
208 {
209 return d2i_ASN1_SET_OF_POLICYINFO(a, pp, length, d2i_POLICYINFO,
210                          POLICYINFO_free, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL);
211
212 }
213
214 IMPLEMENT_STACK_OF(POLICYINFO)
215 IMPLEMENT_ASN1_SET_OF(POLICYINFO)
216
217 int i2d_POLICYINFO(a,pp)
218 POLICYINFO *a;
219 unsigned char **pp;
220 {
221         M_ASN1_I2D_vars(a);
222
223         M_ASN1_I2D_len (a->policyid, i2d_ASN1_OBJECT);
224         M_ASN1_I2D_len_SEQUENCE_type(POLICYQUALINFO, a->qualifiers,
225                                                          i2d_POLICYQUALINFO);
226
227         M_ASN1_I2D_seq_total();
228
229         M_ASN1_I2D_put (a->policyid, i2d_ASN1_OBJECT);
230         M_ASN1_I2D_put_SEQUENCE_type(POLICYQUALINFO, a->qualifiers,
231                                                          i2d_POLICYQUALINFO);
232
233         M_ASN1_I2D_finish();
234 }
235
236 POLICYINFO *POLICYINFO_new()
237 {
238         POLICYINFO *ret=NULL;
239         ASN1_CTX c;
240         M_ASN1_New_Malloc(ret, POLICYINFO);
241         ret->policyid = NULL;
242         ret->qualifiers = NULL;
243         return (ret);
244         M_ASN1_New_Error(ASN1_F_POLICYINFO_NEW);
245 }
246
247 POLICYINFO *d2i_POLICYINFO(a,pp,length)
248 POLICYINFO **a;
249 unsigned char **pp;
250 long length;
251 {
252         M_ASN1_D2I_vars(a,POLICYINFO *,POLICYINFO_new);
253         M_ASN1_D2I_Init();
254         M_ASN1_D2I_start_sequence();
255         M_ASN1_D2I_get(ret->policyid, d2i_ASN1_OBJECT);
256         if(!M_ASN1_D2I_end_sequence()) {
257                 M_ASN1_D2I_get_seq_type (POLICYQUALINFO, ret->qualifiers,
258                                  d2i_POLICYQUALINFO, POLICYQUALINFO_free);
259         }
260         M_ASN1_D2I_Finish(a, POLICYINFO_free, ASN1_F_D2I_POLICYINFO);
261 }
262
263 void POLICYINFO_free(a)
264 POLICYINFO *a;
265 {
266         if (a == NULL) return;
267         ASN1_OBJECT_free(a->policyid);
268         sk_POLICYQUALINFO_pop_free(a->qualifiers, POLICYQUALINFO_free);
269         Free (a);
270 }
271
272 static void print_qualifiers(out, quals, indent)
273 BIO *out;
274 STACK_OF(POLICYQUALINFO) *quals;
275 int indent;
276 {
277         POLICYQUALINFO *qualinfo;
278         int i;
279         for(i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
280                 qualinfo = sk_POLICYQUALINFO_value(quals, i);
281                 switch(OBJ_obj2nid(qualinfo->pqualid))
282                 {
283                         case NID_id_qt_cps:
284                         BIO_printf(out, "%*sCPS: %s\n", indent, "",
285                                                 qualinfo->d.cpsuri->data);
286                         break;
287                 
288                         case NID_id_qt_unotice:
289                         BIO_printf(out, "%*sUser Notice:\n", indent, "");
290                         print_notice(out, qualinfo->d.usernotice, indent + 2);
291                         break;
292
293                         default:
294                         BIO_printf(out, "%*sUnknown Qualifier: ",
295                                                          indent + 2, "");
296                         
297                         i2a_ASN1_OBJECT(out, qualinfo->pqualid);
298                         BIO_puts(out, "\n");
299                         break;
300                 }
301         }
302 }
303
304 static void print_notice(out, notice, indent)
305 BIO *out;
306 USERNOTICE *notice;
307 int indent;
308 {
309         int i;
310         if(notice->noticeref) {
311                 NOTICEREF *ref;
312                 ref = notice->noticeref;
313                 BIO_printf(out, "%*sOrganization: %s\n", indent, "",
314                                                  ref->organization->data);
315                 BIO_printf(out, "%*sNumber%s: ", indent, "",
316                                  (sk_num(ref->noticenos) > 1) ? "s" : "");
317                 for(i = 0; i < sk_num(ref->noticenos); i++) {
318                         ASN1_INTEGER *num;
319                         char *tmp;
320                         num = (ASN1_INTEGER *)sk_value(ref->noticenos, i);
321                         if(i) BIO_puts(out, ", ");
322                         tmp = i2s_ASN1_INTEGER(NULL, num);
323                         BIO_puts(out, tmp);
324                         Free(tmp);
325                 }
326                 BIO_puts(out, "\n");
327         }
328         if(notice->exptext)
329                 BIO_printf(out, "%*sNotice Reference: %s\n", indent, "",
330                                                          notice->exptext->data);
331 }
332                 
333         
334
335 int i2d_POLICYQUALINFO(a,pp)
336 POLICYQUALINFO *a;
337 unsigned char **pp;
338 {
339         M_ASN1_I2D_vars(a);
340
341         M_ASN1_I2D_len (a->pqualid, i2d_ASN1_OBJECT);
342         switch(OBJ_obj2nid(a->pqualid)) {
343                 case NID_id_qt_cps:
344                 M_ASN1_I2D_len(a->d.cpsuri, i2d_ASN1_IA5STRING);
345                 break;
346
347                 case NID_id_qt_unotice:
348                 M_ASN1_I2D_len(a->d.usernotice, i2d_USERNOTICE);
349                 break;
350
351                 default:
352                 M_ASN1_I2D_len(a->d.other, i2d_ASN1_TYPE);
353                 break;
354         }
355
356         M_ASN1_I2D_seq_total();
357
358         M_ASN1_I2D_put (a->pqualid, i2d_ASN1_OBJECT);
359         switch(OBJ_obj2nid(a->pqualid)) {
360                 case NID_id_qt_cps:
361                 M_ASN1_I2D_put(a->d.cpsuri, i2d_ASN1_IA5STRING);
362                 break;
363
364                 case NID_id_qt_unotice:
365                 M_ASN1_I2D_put(a->d.usernotice, i2d_USERNOTICE);
366                 break;
367
368                 default:
369                 M_ASN1_I2D_put(a->d.other, i2d_ASN1_TYPE);
370                 break;
371         }
372
373         M_ASN1_I2D_finish();
374 }
375
376 POLICYQUALINFO *POLICYQUALINFO_new()
377 {
378         POLICYQUALINFO *ret=NULL;
379         ASN1_CTX c;
380         M_ASN1_New_Malloc(ret, POLICYQUALINFO);
381         ret->pqualid = NULL;
382         ret->d.other = NULL;
383         return (ret);
384         M_ASN1_New_Error(ASN1_F_POLICYQUALINFO_NEW);
385 }
386
387 POLICYQUALINFO *d2i_POLICYQUALINFO(a,pp,length)
388 POLICYQUALINFO **a;
389 unsigned char **pp;
390 long length;
391 {
392         M_ASN1_D2I_vars(a,POLICYQUALINFO *,POLICYQUALINFO_new);
393         M_ASN1_D2I_Init();
394         M_ASN1_D2I_start_sequence();
395         M_ASN1_D2I_get (ret->pqualid, d2i_ASN1_OBJECT);
396         switch(OBJ_obj2nid(ret->pqualid)) {
397                 case NID_id_qt_cps:
398                 M_ASN1_D2I_get(ret->d.cpsuri, d2i_ASN1_IA5STRING);
399                 break;
400
401                 case NID_id_qt_unotice:
402                 M_ASN1_D2I_get(ret->d.usernotice, d2i_USERNOTICE);
403                 break;
404
405                 default:
406                 M_ASN1_D2I_get(ret->d.other, d2i_ASN1_TYPE);
407                 break;
408         }
409         M_ASN1_D2I_Finish(a, POLICYQUALINFO_free, ASN1_F_D2I_POLICYQUALINFO);
410 }
411
412 void POLICYQUALINFO_free(a)
413 POLICYQUALINFO *a;
414 {
415         if (a == NULL) return;
416         switch(OBJ_obj2nid(a->pqualid)) {
417                 case NID_id_qt_cps:
418                 ASN1_IA5STRING_free(a->d.cpsuri);
419                 break;
420
421                 case NID_id_qt_unotice:
422                 USERNOTICE_free(a->d.usernotice);
423                 break;
424
425                 default:
426                 ASN1_TYPE_free(a->d.other);
427                 break;
428         }
429         
430         ASN1_OBJECT_free(a->pqualid);
431         Free (a);
432 }
433
434 int i2d_USERNOTICE(a,pp)
435 USERNOTICE *a;
436 unsigned char **pp;
437 {
438         M_ASN1_I2D_vars(a);
439
440         M_ASN1_I2D_len (a->noticeref, i2d_NOTICEREF);
441         M_ASN1_I2D_len (a->exptext, i2d_DISPLAYTEXT);
442
443         M_ASN1_I2D_seq_total();
444
445         M_ASN1_I2D_put (a->noticeref, i2d_NOTICEREF);
446         M_ASN1_I2D_put (a->exptext, i2d_DISPLAYTEXT);
447
448         M_ASN1_I2D_finish();
449 }
450
451 USERNOTICE *USERNOTICE_new()
452 {
453         USERNOTICE *ret=NULL;
454         ASN1_CTX c;
455         M_ASN1_New_Malloc(ret, USERNOTICE);
456         ret->noticeref = NULL;
457         ret->exptext = NULL;
458         return (ret);
459         M_ASN1_New_Error(ASN1_F_USERNOTICE_NEW);
460 }
461
462 USERNOTICE *d2i_USERNOTICE(a,pp,length)
463 USERNOTICE **a;
464 unsigned char **pp;
465 long length;
466 {
467         M_ASN1_D2I_vars(a,USERNOTICE *,USERNOTICE_new);
468         M_ASN1_D2I_Init();
469         M_ASN1_D2I_start_sequence();
470         M_ASN1_D2I_get_opt(ret->noticeref, d2i_NOTICEREF, V_ASN1_SEQUENCE);
471         if (!M_ASN1_D2I_end_sequence()) {
472                 M_ASN1_D2I_get(ret->exptext, d2i_DISPLAYTEXT);
473         }
474         M_ASN1_D2I_Finish(a, USERNOTICE_free, ASN1_F_D2I_USERNOTICE);
475 }
476
477 void USERNOTICE_free(a)
478 USERNOTICE *a;
479 {
480         if (a == NULL) return;
481         NOTICEREF_free(a->noticeref);
482         DISPLAYTEXT_free(a->exptext);
483         Free (a);
484 }
485
486 int i2d_NOTICEREF(a,pp)
487 NOTICEREF *a;
488 unsigned char **pp;
489 {
490         M_ASN1_I2D_vars(a);
491
492         M_ASN1_I2D_len (a->organization, i2d_DISPLAYTEXT);
493         M_ASN1_I2D_len_SEQUENCE(a->noticenos, i2d_ASN1_INTEGER);
494
495         M_ASN1_I2D_seq_total();
496
497         M_ASN1_I2D_put (a->organization, i2d_DISPLAYTEXT);
498         M_ASN1_I2D_put_SEQUENCE(a->noticenos, i2d_ASN1_INTEGER);
499
500         M_ASN1_I2D_finish();
501 }
502
503 NOTICEREF *NOTICEREF_new()
504 {
505         NOTICEREF *ret=NULL;
506         ASN1_CTX c;
507         M_ASN1_New_Malloc(ret, NOTICEREF);
508         ret->organization = NULL;
509         ret->noticenos = NULL;
510         return (ret);
511         M_ASN1_New_Error(ASN1_F_NOTICEREF_NEW);
512 }
513
514 NOTICEREF *d2i_NOTICEREF(a,pp,length)
515 NOTICEREF **a;
516 unsigned char **pp;
517 long length;
518 {
519         M_ASN1_D2I_vars(a,NOTICEREF *,NOTICEREF_new);
520         M_ASN1_D2I_Init();
521         M_ASN1_D2I_start_sequence();
522         /* This is to cope with some broken encodings that use IA5STRING for
523          * the organization field
524          */
525         M_ASN1_D2I_get_opt(ret->organization, d2i_ASN1_IA5STRING,
526                                                          V_ASN1_IA5STRING);
527         if(!ret->organization) {
528                  M_ASN1_D2I_get(ret->organization, d2i_DISPLAYTEXT);
529         }
530         M_ASN1_D2I_get_seq(ret->noticenos, d2i_ASN1_INTEGER, ASN1_STRING_free);
531         M_ASN1_D2I_Finish(a, NOTICEREF_free, ASN1_F_D2I_NOTICEREF);
532 }
533
534 void NOTICEREF_free(a)
535 NOTICEREF *a;
536 {
537         if (a == NULL) return;
538         DISPLAYTEXT_free(a->organization);
539         sk_pop_free(a->noticenos, ASN1_STRING_free);
540         Free (a);
541 }
542
543 IMPLEMENT_STACK_OF(POLICYQUALINFO)
544 IMPLEMENT_ASN1_SET_OF(POLICYQUALINFO)