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