Suppport for CRL distribution points extension. Also document some of
[openssl.git] / crypto / x509v3 / v3_crld.c
1 /* v3_crld.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 static STACK *i2v_crld(X509V3_EXT_METHOD *method, STACK_OF(DIST_POINT) *crld,
67                                                          STACK *extlist);
68 static STACK_OF(DIST_POINT) *v2i_crld(X509V3_EXT_METHOD *method,
69                                                  X509V3_CTX *ctx, STACK *nval);
70
71 X509V3_EXT_METHOD v3_crld = {
72 NID_crl_distribution_points, X509V3_EXT_MULTILINE,
73 (X509V3_EXT_NEW)CRL_DIST_POINTS_new,
74 CRL_DIST_POINTS_free,
75 (X509V3_EXT_D2I)d2i_CRL_DIST_POINTS,
76 i2d_CRL_DIST_POINTS,
77 NULL, NULL,
78 (X509V3_EXT_I2V)i2v_crld,
79 (X509V3_EXT_V2I)v2i_crld,
80 NULL, NULL, NULL
81 };
82
83 /*
84  * ASN1err(ASN1_F_DIST_POINT_NEW,ERR_R_MALLOC_FAILURE);
85  * ASN1err(ASN1_F_D2I_DIST_POINT,ERR_R_MALLOC_FAILURE);
86  * ASN1err(ASN1_F_DIST_POINT_NAME_NEW,ERR_R_MALLOC_FAILURE);
87  * ASN1err(ASN1_F_D2I_DIST_POINT_NAME,ERR_R_MALLOC_FAILURE);
88  */
89
90 static STACK *i2v_crld(X509V3_EXT_METHOD *method, STACK_OF(DIST_POINT) *crld,
91                                                                  STACK *exts)
92 {
93         DIST_POINT *point;
94         int i;
95         for(i = 0; i < sk_DIST_POINT_num(crld); i++) {
96                 point = sk_DIST_POINT_value(crld, i);
97                 if(point->distpoint->fullname) {
98                         exts = i2v_GENERAL_NAMES(NULL,
99                                          point->distpoint->fullname, exts);
100                 }
101                 if(point->reasons) 
102                         X509V3_add_value("reasons","<UNSUPPORTED>", &exts);
103                 if(point->CRLissuer)
104                         X509V3_add_value("CRLissuer","<UNSUPPORTED>", &exts);
105                 if(point->distpoint->relativename)
106                         X509V3_add_value("RelativeName","<UNSUPPORTED>", &exts);
107         }
108         return exts;
109 }
110
111 static STACK_OF(DIST_POINT) *v2i_crld(X509V3_EXT_METHOD *method,
112                                                  X509V3_CTX *ctx, STACK *nval)
113 {
114         STACK_OF(DIST_POINT) *crld = NULL;
115         STACK_OF(GENERAL_NAME) *gens = NULL;
116         GENERAL_NAME *gen = NULL;
117         CONF_VALUE *cnf;
118         int i;
119         if(!(crld = sk_DIST_POINT_new(NULL))) goto merr;
120         for(i = 0; i < sk_num(nval); i++) {
121                 DIST_POINT *point;
122                 cnf = (CONF_VALUE *)sk_value(nval, i);
123                 if(!(gen = v2i_GENERAL_NAME(method, ctx, cnf))) goto err; 
124                 if(!(gens = GENERAL_NAMES_new())) goto merr;
125                 if(!sk_GENERAL_NAME_push(gens, gen)) goto merr;
126                 gen = NULL;
127                 if(!(point = DIST_POINT_new())) goto merr;
128                 if(!sk_DIST_POINT_push(crld, point)) {
129                         DIST_POINT_free(point);
130                         goto merr;
131                 }
132                 if(!(point->distpoint = DIST_POINT_NAME_new())) goto merr;
133                 point->distpoint->fullname = gens;
134                 gens = NULL;
135         }
136         return crld;
137
138         merr:
139         X509V3err(X509V3_F_V2I_CRLD,ERR_R_MALLOC_FAILURE);
140         err:
141         GENERAL_NAME_free(gen);
142         GENERAL_NAMES_free(gens);
143         sk_DIST_POINT_pop_free(crld, DIST_POINT_free);
144         return NULL;
145 }
146
147 int i2d_CRL_DIST_POINTS(STACK_OF(DIST_POINT) *a, unsigned char **pp)
148 {
149
150 return i2d_ASN1_SET_OF_DIST_POINT(a, pp, i2d_DIST_POINT, V_ASN1_SEQUENCE,
151                                                  V_ASN1_UNIVERSAL, IS_SEQUENCE);}
152
153 STACK_OF(DIST_POINT) *CRL_DIST_POINTS_new(void)
154 {
155         return sk_DIST_POINT_new_null();
156 }
157
158 void CRL_DIST_POINTS_free(STACK_OF(DIST_POINT) *a)
159 {
160         sk_DIST_POINT_pop_free(a, DIST_POINT_free);
161 }
162
163 STACK_OF(DIST_POINT) *d2i_CRL_DIST_POINTS(STACK_OF(DIST_POINT) **a,
164                 unsigned char **pp,long length)
165 {
166 return d2i_ASN1_SET_OF_DIST_POINT(a, pp, length, d2i_DIST_POINT,
167                          DIST_POINT_free, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL);
168
169 }
170
171 IMPLEMENT_STACK_OF(DIST_POINT)
172 IMPLEMENT_ASN1_SET_OF(DIST_POINT)
173
174 int i2d_DIST_POINT(DIST_POINT *a, unsigned char **pp)
175 {
176         int v = 0;
177         M_ASN1_I2D_vars(a);
178         /* NB: underlying type is a CHOICE so need EXPLICIT tagging */
179         M_ASN1_I2D_len_EXP_opt (a->distpoint, i2d_DIST_POINT_NAME, 0, v);
180         M_ASN1_I2D_len_IMP_opt (a->reasons, i2d_ASN1_BIT_STRING);
181         M_ASN1_I2D_len_IMP_opt (a->CRLissuer, i2d_GENERAL_NAMES);
182
183         M_ASN1_I2D_seq_total();
184
185         M_ASN1_I2D_put_EXP_opt (a->distpoint, i2d_DIST_POINT_NAME, 0, v);
186         M_ASN1_I2D_put_IMP_opt (a->reasons, i2d_ASN1_BIT_STRING, 1);
187         M_ASN1_I2D_put_IMP_opt (a->CRLissuer, i2d_GENERAL_NAMES, 2);
188
189         M_ASN1_I2D_finish();
190 }
191
192 DIST_POINT *DIST_POINT_new(void)
193 {
194         DIST_POINT *ret=NULL;
195         ASN1_CTX c;
196         M_ASN1_New_Malloc(ret, DIST_POINT);
197         ret->distpoint = NULL;
198         ret->reasons = NULL;
199         ret->CRLissuer = NULL;
200         return (ret);
201         M_ASN1_New_Error(ASN1_F_DIST_POINT_NEW);
202 }
203
204 DIST_POINT *d2i_DIST_POINT(DIST_POINT **a, unsigned char **pp, long length)
205 {
206         M_ASN1_D2I_vars(a,DIST_POINT *,DIST_POINT_new);
207         M_ASN1_D2I_Init();
208         M_ASN1_D2I_start_sequence();
209         M_ASN1_D2I_get_EXP_opt (ret->distpoint, d2i_DIST_POINT_NAME, 0);
210         M_ASN1_D2I_get_IMP_opt (ret->reasons, d2i_ASN1_BIT_STRING, 1,
211                                                         V_ASN1_BIT_STRING);
212         M_ASN1_D2I_get_IMP_opt (ret->CRLissuer, d2i_GENERAL_NAMES, 2,
213                                                         V_ASN1_SEQUENCE);
214         M_ASN1_D2I_Finish(a, DIST_POINT_free, ASN1_F_D2I_DIST_POINT);
215 }
216
217 void DIST_POINT_free(DIST_POINT *a)
218 {
219         if (a == NULL) return;
220         DIST_POINT_NAME_free(a->distpoint);
221         ASN1_BIT_STRING_free(a->reasons);
222         sk_GENERAL_NAME_pop_free(a->CRLissuer, GENERAL_NAME_free);
223         Free ((char *)a);
224 }
225
226 int i2d_DIST_POINT_NAME(DIST_POINT_NAME *a, unsigned char **pp)
227 {
228         int v = 0;
229         M_ASN1_I2D_vars(a);
230
231         if(a->fullname) {
232                 M_ASN1_I2D_len_IMP_opt (a->fullname, i2d_GENERAL_NAMES);
233         } else {
234                 M_ASN1_I2D_len_EXP_opt (a->relativename, i2d_X509_NAME, 1, v);
235         }
236
237         /* Don't want a SEQUENCE so... */
238         if(pp == NULL) return ret;
239         p = *pp;
240
241         if(a->fullname) {
242                 M_ASN1_I2D_put_IMP_opt (a->fullname, i2d_GENERAL_NAMES, 0);
243         } else {
244                 M_ASN1_I2D_put_EXP_opt (a->relativename, i2d_X509_NAME, 1, v);
245         }
246         M_ASN1_I2D_finish();
247 }
248
249 DIST_POINT_NAME *DIST_POINT_NAME_new(void)
250 {
251         DIST_POINT_NAME *ret=NULL;
252         ASN1_CTX c;
253         M_ASN1_New_Malloc(ret, DIST_POINT_NAME);
254         ret->fullname = NULL;
255         ret->relativename = NULL;
256         return (ret);
257         M_ASN1_New_Error(ASN1_F_DIST_POINT_NAME_NEW);
258 }
259
260 void DIST_POINT_NAME_free(DIST_POINT_NAME *a)
261 {
262         if (a == NULL) return;
263         X509_NAME_free(a->relativename);
264         sk_GENERAL_NAME_pop_free(a->fullname, GENERAL_NAME_free);
265         Free ((char *)a);
266 }
267
268 DIST_POINT_NAME *d2i_DIST_POINT_NAME(DIST_POINT_NAME **a, unsigned char **pp,
269              long length)
270 {
271         unsigned char _tmp, tag;
272         M_ASN1_D2I_vars(a,DIST_POINT_NAME *,DIST_POINT_NAME_new);
273         M_ASN1_D2I_Init();
274         c.slen = length;
275
276         _tmp = M_ASN1_next;
277         tag = _tmp & ~V_ASN1_CONSTRUCTED;
278         
279         if(tag == (0|V_ASN1_CONTEXT_SPECIFIC)) {
280                 M_ASN1_D2I_get_imp(ret->fullname, d2i_GENERAL_NAMES,
281                                                         V_ASN1_SEQUENCE);
282         } else if (tag == (1|V_ASN1_CONTEXT_SPECIFIC)) {
283                 M_ASN1_D2I_get_EXP_opt (ret->relativename, d2i_X509_NAME, 1);
284         } else {
285                 c.error = ASN1_R_BAD_TAG;
286                 goto err;
287         }
288
289         M_ASN1_D2I_Finish(a, DIST_POINT_NAME_free, ASN1_F_D2I_DIST_POINT_NAME);
290 }