|
1 /* crypto/asn1/a_object.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 © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. |
|
60 */ |
|
61 #include <stdio.h> |
|
62 #include <limits.h> |
|
63 #include "cryptlib.h" |
|
64 #include <openssl/buffer.h> |
|
65 #include <openssl/asn1.h> |
|
66 #include <openssl/objects.h> |
|
67 #include <openssl/bn.h> |
|
68 |
|
69 |
|
70 EXPORT_C int i2d_ASN1_OBJECT(ASN1_OBJECT *a, unsigned char **pp) |
|
71 { |
|
72 unsigned char *p; |
|
73 int objsize; |
|
74 |
|
75 if ((a == NULL) || (a->data == NULL)) return(0); |
|
76 |
|
77 objsize = ASN1_object_size(0,a->length,V_ASN1_OBJECT); |
|
78 if (pp == NULL) return objsize; |
|
79 |
|
80 p= *pp; |
|
81 ASN1_put_object(&p,0,a->length,V_ASN1_OBJECT,V_ASN1_UNIVERSAL); |
|
82 memcpy(p,a->data,a->length); |
|
83 p+=a->length; |
|
84 |
|
85 *pp=p; |
|
86 return(objsize); |
|
87 } |
|
88 |
|
89 EXPORT_C int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num) |
|
90 { |
|
91 int i,first,len=0,c, use_bn; |
|
92 char ftmp[24], *tmp = ftmp; |
|
93 int tmpsize = sizeof ftmp; |
|
94 const char *p; |
|
95 unsigned long l; |
|
96 BIGNUM *bl = NULL; |
|
97 |
|
98 if (num == 0) |
|
99 return(0); |
|
100 else if (num == -1) |
|
101 num=strlen(buf); |
|
102 |
|
103 p=buf; |
|
104 c= *(p++); |
|
105 num--; |
|
106 if ((c >= '0') && (c <= '2')) |
|
107 { |
|
108 first= c-'0'; |
|
109 } |
|
110 else |
|
111 { |
|
112 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_FIRST_NUM_TOO_LARGE); |
|
113 goto err; |
|
114 } |
|
115 |
|
116 if (num <= 0) |
|
117 { |
|
118 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_MISSING_SECOND_NUMBER); |
|
119 goto err; |
|
120 } |
|
121 c= *(p++); |
|
122 num--; |
|
123 for (;;) |
|
124 { |
|
125 if (num <= 0) break; |
|
126 if ((c != '.') && (c != ' ')) |
|
127 { |
|
128 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_SEPARATOR); |
|
129 goto err; |
|
130 } |
|
131 l=0; |
|
132 use_bn = 0; |
|
133 for (;;) |
|
134 { |
|
135 if (num <= 0) break; |
|
136 num--; |
|
137 c= *(p++); |
|
138 if ((c == ' ') || (c == '.')) |
|
139 break; |
|
140 if ((c < '0') || (c > '9')) |
|
141 { |
|
142 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_DIGIT); |
|
143 goto err; |
|
144 } |
|
145 if (!use_bn && l > (ULONG_MAX / 10L)) |
|
146 { |
|
147 use_bn = 1; |
|
148 if (!bl) |
|
149 bl = BN_new(); |
|
150 if (!bl || !BN_set_word(bl, l)) |
|
151 goto err; |
|
152 } |
|
153 if (use_bn) |
|
154 { |
|
155 if (!BN_mul_word(bl, 10L) |
|
156 || !BN_add_word(bl, c-'0')) |
|
157 goto err; |
|
158 } |
|
159 else |
|
160 l=l*10L+(long)(c-'0'); |
|
161 } |
|
162 if (len == 0) |
|
163 { |
|
164 if ((first < 2) && (l >= 40)) |
|
165 { |
|
166 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_SECOND_NUMBER_TOO_LARGE); |
|
167 goto err; |
|
168 } |
|
169 if (use_bn) |
|
170 { |
|
171 if (!BN_add_word(bl, first * 40)) |
|
172 goto err; |
|
173 } |
|
174 else |
|
175 l+=(long)first*40; |
|
176 } |
|
177 i=0; |
|
178 if (use_bn) |
|
179 { |
|
180 int blsize; |
|
181 blsize = BN_num_bits(bl); |
|
182 blsize = (blsize + 6)/7; |
|
183 if (blsize > tmpsize) |
|
184 { |
|
185 if (tmp != ftmp) |
|
186 OPENSSL_free(tmp); |
|
187 tmpsize = blsize + 32; |
|
188 tmp = OPENSSL_malloc(tmpsize); |
|
189 if (!tmp) |
|
190 goto err; |
|
191 } |
|
192 while(blsize--) |
|
193 tmp[i++] = (unsigned char)BN_div_word(bl, 0x80L); |
|
194 } |
|
195 else |
|
196 { |
|
197 |
|
198 for (;;) |
|
199 { |
|
200 tmp[i++]=(unsigned char)l&0x7f; |
|
201 l>>=7L; |
|
202 if (l == 0L) break; |
|
203 } |
|
204 |
|
205 } |
|
206 if (out != NULL) |
|
207 { |
|
208 if (len+i > olen) |
|
209 { |
|
210 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_BUFFER_TOO_SMALL); |
|
211 goto err; |
|
212 } |
|
213 while (--i > 0) |
|
214 out[len++]=tmp[i]|0x80; |
|
215 out[len++]=tmp[0]; |
|
216 } |
|
217 else |
|
218 len+=i; |
|
219 } |
|
220 if (tmp != ftmp) |
|
221 OPENSSL_free(tmp); |
|
222 if (bl) |
|
223 BN_free(bl); |
|
224 return(len); |
|
225 err: |
|
226 if (tmp != ftmp) |
|
227 OPENSSL_free(tmp); |
|
228 if (bl) |
|
229 BN_free(bl); |
|
230 return(0); |
|
231 |
|
232 } |
|
233 |
|
234 EXPORT_C int i2t_ASN1_OBJECT(char *buf, int buf_len, ASN1_OBJECT *a) |
|
235 { |
|
236 return OBJ_obj2txt(buf, buf_len, a, 0); |
|
237 } |
|
238 |
|
239 EXPORT_C int i2a_ASN1_OBJECT(BIO *bp, ASN1_OBJECT *a) |
|
240 { |
|
241 #ifndef SYMBIAN |
|
242 char buf[80]; |
|
243 #else |
|
244 char buf[40]; |
|
245 #endif |
|
246 char *p = buf; |
|
247 int i; |
|
248 |
|
249 if ((a == NULL) || (a->data == NULL)) |
|
250 return(BIO_write(bp,"NULL",4)); |
|
251 i=i2t_ASN1_OBJECT(buf,sizeof buf,a); |
|
252 if (i > (int)(sizeof(buf) - 1)) |
|
253 { |
|
254 p = OPENSSL_malloc(i + 1); |
|
255 if (!p) |
|
256 return -1; |
|
257 i2t_ASN1_OBJECT(p,i + 1,a); |
|
258 } |
|
259 if (i <= 0) |
|
260 return BIO_write(bp, "<INVALID>", 9); |
|
261 BIO_write(bp,p,i); |
|
262 if (p != buf) |
|
263 OPENSSL_free(p); |
|
264 return(i); |
|
265 } |
|
266 |
|
267 EXPORT_C ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp, |
|
268 long length) |
|
269 { |
|
270 const unsigned char *p; |
|
271 long len; |
|
272 int tag,xclass; |
|
273 int inf,i; |
|
274 ASN1_OBJECT *ret = NULL; |
|
275 p= *pp; |
|
276 inf=ASN1_get_object(&p,&len,&tag,&xclass,length); |
|
277 if (inf & 0x80) |
|
278 { |
|
279 i=ASN1_R_BAD_OBJECT_HEADER; |
|
280 goto err; |
|
281 } |
|
282 |
|
283 if (tag != V_ASN1_OBJECT) |
|
284 { |
|
285 i=ASN1_R_EXPECTING_AN_OBJECT; |
|
286 goto err; |
|
287 } |
|
288 ret = c2i_ASN1_OBJECT(a, &p, len); |
|
289 if(ret) *pp = p; |
|
290 return ret; |
|
291 err: |
|
292 ASN1err(ASN1_F_D2I_ASN1_OBJECT,i); |
|
293 if ((ret != NULL) && ((a == NULL) || (*a != ret))) |
|
294 ASN1_OBJECT_free(ret); |
|
295 return(NULL); |
|
296 } |
|
297 EXPORT_C ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp, |
|
298 long len) |
|
299 { |
|
300 ASN1_OBJECT *ret=NULL; |
|
301 const unsigned char *p; |
|
302 int i; |
|
303 |
|
304 /* only the ASN1_OBJECTs from the 'table' will have values |
|
305 * for ->sn or ->ln */ |
|
306 if ((a == NULL) || ((*a) == NULL) || |
|
307 !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) |
|
308 { |
|
309 if ((ret=ASN1_OBJECT_new()) == NULL) return(NULL); |
|
310 } |
|
311 else ret=(*a); |
|
312 |
|
313 p= *pp; |
|
314 if ((ret->data == NULL) || (ret->length < len)) |
|
315 { |
|
316 if (ret->data != NULL) OPENSSL_free(ret->data); |
|
317 ret->data=(unsigned char *)OPENSSL_malloc(len ? (int)len : 1); |
|
318 ret->flags|=ASN1_OBJECT_FLAG_DYNAMIC_DATA; |
|
319 if (ret->data == NULL) |
|
320 { i=ERR_R_MALLOC_FAILURE; goto err; } |
|
321 } |
|
322 memcpy(ret->data,p,(int)len); |
|
323 ret->length=(int)len; |
|
324 ret->sn=NULL; |
|
325 ret->ln=NULL; |
|
326 /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */ |
|
327 p+=len; |
|
328 |
|
329 if (a != NULL) (*a)=ret; |
|
330 *pp=p; |
|
331 return(ret); |
|
332 err: |
|
333 ASN1err(ASN1_F_C2I_ASN1_OBJECT,i); |
|
334 if ((ret != NULL) && ((a == NULL) || (*a != ret))) |
|
335 ASN1_OBJECT_free(ret); |
|
336 return(NULL); |
|
337 } |
|
338 |
|
339 EXPORT_C ASN1_OBJECT *ASN1_OBJECT_new(void) |
|
340 { |
|
341 ASN1_OBJECT *ret; |
|
342 |
|
343 ret=(ASN1_OBJECT *)OPENSSL_malloc(sizeof(ASN1_OBJECT)); |
|
344 if (ret == NULL) |
|
345 { |
|
346 ASN1err(ASN1_F_ASN1_OBJECT_NEW,ERR_R_MALLOC_FAILURE); |
|
347 return(NULL); |
|
348 } |
|
349 ret->length=0; |
|
350 ret->data=NULL; |
|
351 ret->nid=0; |
|
352 ret->sn=NULL; |
|
353 ret->ln=NULL; |
|
354 ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; |
|
355 return(ret); |
|
356 } |
|
357 |
|
358 EXPORT_C void ASN1_OBJECT_free(ASN1_OBJECT *a) |
|
359 { |
|
360 if (a == NULL) return; |
|
361 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) |
|
362 { |
|
363 #ifndef CONST_STRICT /* disable purely for compile-time strict const checking. Doing this on a "real" compile will cause memory leaks */ |
|
364 if (a->sn != NULL) OPENSSL_free((void *)a->sn); |
|
365 if (a->ln != NULL) OPENSSL_free((void *)a->ln); |
|
366 #endif |
|
367 a->sn=a->ln=NULL; |
|
368 } |
|
369 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) |
|
370 { |
|
371 if (a->data != NULL) OPENSSL_free(a->data); |
|
372 a->data=NULL; |
|
373 a->length=0; |
|
374 } |
|
375 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC) |
|
376 OPENSSL_free(a); |
|
377 } |
|
378 |
|
379 EXPORT_C ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len, |
|
380 const char *sn, const char *ln) |
|
381 { |
|
382 ASN1_OBJECT o; |
|
383 |
|
384 o.sn=sn; |
|
385 o.ln=ln; |
|
386 o.data=data; |
|
387 o.nid=nid; |
|
388 o.length=len; |
|
389 o.flags=ASN1_OBJECT_FLAG_DYNAMIC|ASN1_OBJECT_FLAG_DYNAMIC_STRINGS| |
|
390 ASN1_OBJECT_FLAG_DYNAMIC_DATA; |
|
391 return(OBJ_dup(&o)); |
|
392 } |
|
393 |
|
394 IMPLEMENT_STACK_OF(ASN1_OBJECT) |
|
395 IMPLEMENT_ASN1_SET_OF(ASN1_OBJECT) |