a045a629ee7e885b0c949b5b702f6032d506bba6
[openssl.git] / crypto / x509v3 / v3_info.c
1 /* v3_info.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/asn1_mac.h>
64 #include <openssl/x509v3.h>
65
66 static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_INFO_ACCESS(X509V3_EXT_METHOD *method,
67                                 STACK_OF(ACCESS_DESCRIPTION) *ainfo,
68                                                 STACK_OF(CONF_VALUE) *ret);
69 static STACK_OF(ACCESS_DESCRIPTION) *v2i_AUTHORITY_INFO_ACCESS(X509V3_EXT_METHOD *method,
70                                  X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval);
71
72 X509V3_EXT_METHOD v3_info =
73 { NID_info_access, X509V3_EXT_MULTILINE,
74 (X509V3_EXT_NEW)AUTHORITY_INFO_ACCESS_new,
75 (X509V3_EXT_FREE)AUTHORITY_INFO_ACCESS_free,
76 (X509V3_EXT_D2I)d2i_AUTHORITY_INFO_ACCESS,
77 (X509V3_EXT_I2D)i2d_AUTHORITY_INFO_ACCESS,
78 NULL, NULL,
79 (X509V3_EXT_I2V)i2v_AUTHORITY_INFO_ACCESS,
80 (X509V3_EXT_V2I)v2i_AUTHORITY_INFO_ACCESS,
81 NULL, NULL, NULL};
82
83 static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_INFO_ACCESS(X509V3_EXT_METHOD *method,
84                                 STACK_OF(ACCESS_DESCRIPTION) *ainfo,
85                                                 STACK_OF(CONF_VALUE) *ret)
86 {
87         ACCESS_DESCRIPTION *desc;
88         int i;
89         char objtmp[80], *ntmp;
90         CONF_VALUE *vtmp;
91         for(i = 0; i < sk_ACCESS_DESCRIPTION_num(ainfo); i++) {
92                 desc = sk_ACCESS_DESCRIPTION_value(ainfo, i);
93                 ret = i2v_GENERAL_NAME(method, desc->location, ret);
94                 if(!ret) break;
95                 vtmp = sk_CONF_VALUE_value(ret, i);
96                 i2t_ASN1_OBJECT(objtmp, 80, desc->method);
97                 ntmp = OPENSSL_malloc(strlen(objtmp) + strlen(vtmp->name) + 5);
98                 if(!ntmp) {
99                         X509V3err(X509V3_F_I2V_AUTHORITY_INFO_ACCESS,
100                                         ERR_R_MALLOC_FAILURE);
101                         return NULL;
102                 }
103                 strcpy(ntmp, objtmp);
104                 strcat(ntmp, " - ");
105                 strcat(ntmp, vtmp->name);
106                 OPENSSL_free(vtmp->name);
107                 vtmp->name = ntmp;
108                 
109         }
110         if(!ret) return sk_CONF_VALUE_new_null();
111         return ret;
112 }
113
114 static STACK_OF(ACCESS_DESCRIPTION) *v2i_AUTHORITY_INFO_ACCESS(X509V3_EXT_METHOD *method,
115                                  X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
116 {
117         STACK_OF(ACCESS_DESCRIPTION) *ainfo = NULL;
118         CONF_VALUE *cnf, ctmp;
119         ACCESS_DESCRIPTION *acc;
120         int i, objlen;
121         char *objtmp, *ptmp;
122         if(!(ainfo = sk_ACCESS_DESCRIPTION_new_null())) {
123                 X509V3err(X509V3_F_V2I_ACCESS_DESCRIPTION,ERR_R_MALLOC_FAILURE);
124                 return NULL;
125         }
126         for(i = 0; i < sk_CONF_VALUE_num(nval); i++) {
127                 cnf = sk_CONF_VALUE_value(nval, i);
128                 if(!(acc = ACCESS_DESCRIPTION_new())
129                         || !sk_ACCESS_DESCRIPTION_push(ainfo, acc)) {
130                         X509V3err(X509V3_F_V2I_ACCESS_DESCRIPTION,ERR_R_MALLOC_FAILURE);
131                         goto err;
132                 }
133                 ptmp = strchr(cnf->name, ';');
134                 if(!ptmp) {
135                         X509V3err(X509V3_F_V2I_ACCESS_DESCRIPTION,X509V3_R_INVALID_SYNTAX);
136                         goto err;
137                 }
138                 objlen = ptmp - cnf->name;
139                 ctmp.name = ptmp + 1;
140                 ctmp.value = cnf->value;
141                 if(!(acc->location = v2i_GENERAL_NAME(method, ctx, &ctmp)))
142                                                                  goto err; 
143                 if(!(objtmp = OPENSSL_malloc(objlen + 1))) {
144                         X509V3err(X509V3_F_V2I_ACCESS_DESCRIPTION,ERR_R_MALLOC_FAILURE);
145                         goto err;
146                 }
147                 strncpy(objtmp, cnf->name, objlen);
148                 objtmp[objlen] = 0;
149                 acc->method = OBJ_txt2obj(objtmp, 0);
150                 if(!acc->method) {
151                         X509V3err(X509V3_F_V2I_ACCESS_DESCRIPTION,X509V3_R_BAD_OBJECT);
152                         ERR_add_error_data(2, "value=", objtmp);
153                         OPENSSL_free(objtmp);
154                         goto err;
155                 }
156                 OPENSSL_free(objtmp);
157
158         }
159         return ainfo;
160         err:
161         sk_ACCESS_DESCRIPTION_pop_free(ainfo, ACCESS_DESCRIPTION_free);
162         return NULL;
163 }
164
165 int i2d_ACCESS_DESCRIPTION(ACCESS_DESCRIPTION *a, unsigned char **pp)
166 {
167         M_ASN1_I2D_vars(a);
168
169         M_ASN1_I2D_len(a->method, i2d_ASN1_OBJECT);
170         M_ASN1_I2D_len(a->location, i2d_GENERAL_NAME);
171
172         M_ASN1_I2D_seq_total();
173
174         M_ASN1_I2D_put(a->method, i2d_ASN1_OBJECT);
175         M_ASN1_I2D_put(a->location, i2d_GENERAL_NAME);
176
177         M_ASN1_I2D_finish();
178 }
179
180 ACCESS_DESCRIPTION *ACCESS_DESCRIPTION_new(void)
181 {
182         ACCESS_DESCRIPTION *ret=NULL;
183         ASN1_CTX c;
184         M_ASN1_New_Malloc(ret, ACCESS_DESCRIPTION);
185         ret->method = OBJ_nid2obj(NID_undef);
186         ret->location = NULL;
187         return (ret);
188         M_ASN1_New_Error(ASN1_F_ACCESS_DESCRIPTION_NEW);
189 }
190
191 ACCESS_DESCRIPTION *d2i_ACCESS_DESCRIPTION(ACCESS_DESCRIPTION **a, unsigned char **pp,
192              long length)
193 {
194         M_ASN1_D2I_vars(a,ACCESS_DESCRIPTION *,ACCESS_DESCRIPTION_new);
195         M_ASN1_D2I_Init();
196         M_ASN1_D2I_start_sequence();
197         M_ASN1_D2I_get(ret->method, d2i_ASN1_OBJECT);
198         M_ASN1_D2I_get(ret->location, d2i_GENERAL_NAME);
199         M_ASN1_D2I_Finish(a, ACCESS_DESCRIPTION_free, ASN1_F_D2I_ACCESS_DESCRIPTION);
200 }
201
202 void ACCESS_DESCRIPTION_free(ACCESS_DESCRIPTION *a)
203 {
204         if (a == NULL) return;
205         ASN1_OBJECT_free(a->method);
206         GENERAL_NAME_free(a->location);
207         OPENSSL_free (a);
208 }
209
210 STACK_OF(ACCESS_DESCRIPTION) *AUTHORITY_INFO_ACCESS_new(void)
211 {
212         return sk_ACCESS_DESCRIPTION_new_null();
213 }
214
215 void AUTHORITY_INFO_ACCESS_free(STACK_OF(ACCESS_DESCRIPTION) *a)
216 {
217         sk_ACCESS_DESCRIPTION_pop_free(a, ACCESS_DESCRIPTION_free);
218 }
219
220 STACK_OF(ACCESS_DESCRIPTION) *d2i_AUTHORITY_INFO_ACCESS(STACK_OF(ACCESS_DESCRIPTION) **a,
221                                          unsigned char **pp, long length)
222 {
223 return d2i_ASN1_SET_OF_ACCESS_DESCRIPTION(a, pp, length, d2i_ACCESS_DESCRIPTION,
224                          ACCESS_DESCRIPTION_free, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL);
225 }
226
227 int i2d_AUTHORITY_INFO_ACCESS(STACK_OF(ACCESS_DESCRIPTION) *a, unsigned char **pp)
228 {
229 return i2d_ASN1_SET_OF_ACCESS_DESCRIPTION(a, pp, i2d_ACCESS_DESCRIPTION, V_ASN1_SEQUENCE,
230                                                  V_ASN1_UNIVERSAL, IS_SEQUENCE);
231 }
232
233 IMPLEMENT_STACK_OF(ACCESS_DESCRIPTION)
234 IMPLEMENT_ASN1_SET_OF(ACCESS_DESCRIPTION)
235
236