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