1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57 #include <openssl/asn1.h>
58
59 #include <string.h>
60
61 #include <openssl/buf.h>
62 #include <openssl/err.h>
63 #include <openssl/mem.h>
64
65
66 static int asn1_collate_primitive(ASN1_STRING *a, ASN1_const_CTX *c);
67 /* type is a 'bitmap' of acceptable string types.
68 */
d2i_ASN1_type_bytes(ASN1_STRING ** a,const unsigned char ** pp,long length,int type)69 ASN1_STRING *d2i_ASN1_type_bytes(ASN1_STRING **a, const unsigned char **pp,
70 long length, int type)
71 {
72 ASN1_STRING *ret=NULL;
73 const unsigned char *p;
74 unsigned char *s;
75 long len;
76 int inf,tag,xclass;
77 int i=0;
78
79 p= *pp;
80 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
81 if (inf & 0x80) goto err;
82
83 if (tag >= 32)
84 {
85 i=ASN1_R_TAG_VALUE_TOO_HIGH;
86 goto err;
87 }
88 if (!(ASN1_tag2bit(tag) & type))
89 {
90 i=ASN1_R_WRONG_TYPE;
91 goto err;
92 }
93
94 /* If a bit-string, exit early */
95 if (tag == V_ASN1_BIT_STRING)
96 return(d2i_ASN1_BIT_STRING(a,pp,length));
97
98 if ((a == NULL) || ((*a) == NULL))
99 {
100 if ((ret=ASN1_STRING_new()) == NULL) return(NULL);
101 }
102 else
103 ret=(*a);
104
105 if (len != 0)
106 {
107 s=(unsigned char *)OPENSSL_malloc((int)len+1);
108 if (s == NULL)
109 {
110 i=ERR_R_MALLOC_FAILURE;
111 goto err;
112 }
113 memcpy(s,p,(int)len);
114 s[len]='\0';
115 p+=len;
116 }
117 else
118 s=NULL;
119
120 if (ret->data != NULL) OPENSSL_free(ret->data);
121 ret->length=(int)len;
122 ret->data=s;
123 ret->type=tag;
124 if (a != NULL) (*a)=ret;
125 *pp=p;
126 return(ret);
127 err:
128 OPENSSL_PUT_ERROR(ASN1, d2i_ASN1_type_bytes, i);
129 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
130 ASN1_STRING_free(ret);
131 return(NULL);
132 }
133
i2d_ASN1_bytes(ASN1_STRING * a,unsigned char ** pp,int tag,int xclass)134 int i2d_ASN1_bytes(ASN1_STRING *a, unsigned char **pp, int tag, int xclass)
135 {
136 int ret,r,constructed;
137 unsigned char *p;
138
139 if (a == NULL) return(0);
140
141 if (tag == V_ASN1_BIT_STRING)
142 return(i2d_ASN1_BIT_STRING(a,pp));
143
144 ret=a->length;
145 r=ASN1_object_size(0,ret,tag);
146 if (pp == NULL) return(r);
147 p= *pp;
148
149 if ((tag == V_ASN1_SEQUENCE) || (tag == V_ASN1_SET))
150 constructed=1;
151 else
152 constructed=0;
153 ASN1_put_object(&p,constructed,ret,tag,xclass);
154 memcpy(p,a->data,a->length);
155 p+=a->length;
156 *pp= p;
157 return(r);
158 }
159
d2i_ASN1_bytes(ASN1_STRING ** a,const unsigned char ** pp,long length,int Ptag,int Pclass)160 ASN1_STRING *d2i_ASN1_bytes(ASN1_STRING **a, const unsigned char **pp,
161 long length, int Ptag, int Pclass)
162 {
163 ASN1_STRING *ret=NULL;
164 const unsigned char *p;
165 unsigned char *s;
166 long len;
167 int inf,tag,xclass;
168 int i=0;
169
170 if ((a == NULL) || ((*a) == NULL))
171 {
172 if ((ret=ASN1_STRING_new()) == NULL) return(NULL);
173 }
174 else
175 ret=(*a);
176
177 p= *pp;
178 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
179 if (inf & 0x80)
180 {
181 i=ASN1_R_BAD_OBJECT_HEADER;
182 goto err;
183 }
184
185 if (tag != Ptag)
186 {
187 i=ASN1_R_WRONG_TAG;
188 goto err;
189 }
190
191 if (inf & V_ASN1_CONSTRUCTED)
192 {
193 ASN1_const_CTX c;
194
195 c.pp=pp;
196 c.p=p;
197 c.inf=inf;
198 c.slen=len;
199 c.tag=Ptag;
200 c.xclass=Pclass;
201 c.max=(length == 0)?0:(p+length);
202 if (!asn1_collate_primitive(ret,&c))
203 goto err;
204 else
205 {
206 p=c.p;
207 }
208 }
209 else
210 {
211 if (len != 0)
212 {
213 if ((ret->length < len) || (ret->data == NULL))
214 {
215 if (ret->data != NULL) OPENSSL_free(ret->data);
216 s=(unsigned char *)OPENSSL_malloc((int)len + 1);
217 if (s == NULL)
218 {
219 i=ERR_R_MALLOC_FAILURE;
220 goto err;
221 }
222 }
223 else
224 s=ret->data;
225 memcpy(s,p,(int)len);
226 s[len] = '\0';
227 p+=len;
228 }
229 else
230 {
231 s=NULL;
232 if (ret->data != NULL) OPENSSL_free(ret->data);
233 }
234
235 ret->length=(int)len;
236 ret->data=s;
237 ret->type=Ptag;
238 }
239
240 if (a != NULL) (*a)=ret;
241 *pp=p;
242 return(ret);
243 err:
244 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
245 ASN1_STRING_free(ret);
246 OPENSSL_PUT_ERROR(ASN1, d2i_ASN1_bytes, i);
247 return(NULL);
248 }
249
250
251 /* We are about to parse 0..n d2i_ASN1_bytes objects, we are to collapse
252 * them into the one structure that is then returned */
253 /* There have been a few bug fixes for this function from
254 * Paul Keogh <paul.keogh@sse.ie>, many thanks to him */
asn1_collate_primitive(ASN1_STRING * a,ASN1_const_CTX * c)255 static int asn1_collate_primitive(ASN1_STRING *a, ASN1_const_CTX *c)
256 {
257 ASN1_STRING *os=NULL;
258 BUF_MEM b;
259 int num;
260
261 b.length=0;
262 b.max=0;
263 b.data=NULL;
264
265 if (a == NULL)
266 {
267 c->error=ERR_R_PASSED_NULL_PARAMETER;
268 goto err;
269 }
270
271 num=0;
272 for (;;)
273 {
274 if (c->inf & 1)
275 {
276 c->eos=ASN1_const_check_infinite_end(&c->p,
277 (long)(c->max-c->p));
278 if (c->eos) break;
279 }
280 else
281 {
282 if (c->slen <= 0) break;
283 }
284
285 c->q=c->p;
286 if (d2i_ASN1_bytes(&os,&c->p,c->max-c->p,c->tag,c->xclass)
287 == NULL)
288 {
289 c->error=ERR_R_ASN1_LIB;
290 goto err;
291 }
292
293 if (!BUF_MEM_grow_clean(&b,num+os->length))
294 {
295 c->error=ERR_R_BUF_LIB;
296 goto err;
297 }
298 memcpy(&(b.data[num]),os->data,os->length);
299 if (!(c->inf & 1))
300 c->slen-=(c->p-c->q);
301 num+=os->length;
302 }
303
304 if (!asn1_const_Finish(c)) goto err;
305
306 a->length=num;
307 if (a->data != NULL) OPENSSL_free(a->data);
308 a->data=(unsigned char *)b.data;
309 if (os != NULL) ASN1_STRING_free(os);
310 return(1);
311 err:
312 OPENSSL_PUT_ERROR(ASN1, asn1_collate_primitive, c->error);
313 if (os != NULL) ASN1_STRING_free(os);
314 if (b.data != NULL) OPENSSL_free(b.data);
315 return(0);
316 }
317
318