Debian Codegraph @

external/boringssl/crypto/asn1/a_object.c

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