/src/openssl32/crypto/asn1/a_object.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <stdio.h> |
11 | | #include <limits.h> |
12 | | #include "crypto/ctype.h" |
13 | | #include "internal/cryptlib.h" |
14 | | #include <openssl/buffer.h> |
15 | | #include <openssl/asn1.h> |
16 | | #include <openssl/objects.h> |
17 | | #include <openssl/bn.h> |
18 | | #include "crypto/asn1.h" |
19 | | #include "asn1_local.h" |
20 | | |
21 | | int i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp) |
22 | 0 | { |
23 | 0 | unsigned char *p, *allocated = NULL; |
24 | 0 | int objsize; |
25 | |
|
26 | 0 | if ((a == NULL) || (a->data == NULL)) |
27 | 0 | return 0; |
28 | | |
29 | 0 | objsize = ASN1_object_size(0, a->length, V_ASN1_OBJECT); |
30 | 0 | if (pp == NULL || objsize == -1) |
31 | 0 | return objsize; |
32 | | |
33 | 0 | if (*pp == NULL) { |
34 | 0 | if ((p = allocated = OPENSSL_malloc(objsize)) == NULL) |
35 | 0 | return 0; |
36 | 0 | } else { |
37 | 0 | p = *pp; |
38 | 0 | } |
39 | | |
40 | 0 | ASN1_put_object(&p, 0, a->length, V_ASN1_OBJECT, V_ASN1_UNIVERSAL); |
41 | 0 | memcpy(p, a->data, a->length); |
42 | | |
43 | | /* |
44 | | * If a new buffer was allocated, just return it back. |
45 | | * If not, return the incremented buffer pointer. |
46 | | */ |
47 | 0 | *pp = allocated != NULL ? allocated : p + a->length; |
48 | 0 | return objsize; |
49 | 0 | } |
50 | | |
51 | | int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num) |
52 | 186k | { |
53 | 186k | int i, first, len = 0, c, use_bn; |
54 | 186k | char ftmp[24], *tmp = ftmp; |
55 | 186k | int tmpsize = sizeof(ftmp); |
56 | 186k | const char *p; |
57 | 186k | unsigned long l; |
58 | 186k | BIGNUM *bl = NULL; |
59 | | |
60 | 186k | if (num == 0) |
61 | 0 | return 0; |
62 | 186k | else if (num == -1) |
63 | 186k | num = strlen(buf); |
64 | | |
65 | 186k | p = buf; |
66 | 186k | c = *(p++); |
67 | 186k | num--; |
68 | 186k | if ((c >= '0') && (c <= '2')) { |
69 | 186k | first = c - '0'; |
70 | 186k | } else { |
71 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_FIRST_NUM_TOO_LARGE); |
72 | 0 | goto err; |
73 | 0 | } |
74 | | |
75 | 186k | if (num <= 0) { |
76 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_SECOND_NUMBER); |
77 | 0 | goto err; |
78 | 0 | } |
79 | 186k | c = *(p++); |
80 | 186k | num--; |
81 | 1.67M | for (;;) { |
82 | 1.67M | if (num <= 0) |
83 | 186k | break; |
84 | 1.49M | if ((c != '.') && (c != ' ')) { |
85 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_SEPARATOR); |
86 | 0 | goto err; |
87 | 0 | } |
88 | 1.49M | l = 0; |
89 | 1.49M | use_bn = 0; |
90 | 4.10M | for (;;) { |
91 | 4.10M | if (num <= 0) |
92 | 186k | break; |
93 | 3.91M | num--; |
94 | 3.91M | c = *(p++); |
95 | 3.91M | if ((c == ' ') || (c == '.')) |
96 | 1.30M | break; |
97 | 2.60M | if (!ossl_isdigit(c)) { |
98 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_DIGIT); |
99 | 0 | goto err; |
100 | 0 | } |
101 | 2.60M | if (!use_bn && l >= ((ULONG_MAX - 80) / 10L)) { |
102 | 0 | use_bn = 1; |
103 | 0 | if (bl == NULL) |
104 | 0 | bl = BN_new(); |
105 | 0 | if (bl == NULL || !BN_set_word(bl, l)) |
106 | 0 | goto err; |
107 | 0 | } |
108 | 2.60M | if (use_bn) { |
109 | 0 | if (!BN_mul_word(bl, 10L) |
110 | 0 | || !BN_add_word(bl, c - '0')) |
111 | 0 | goto err; |
112 | 0 | } else |
113 | 2.60M | l = l * 10L + (long)(c - '0'); |
114 | 2.60M | } |
115 | 1.49M | if (len == 0) { |
116 | 186k | if ((first < 2) && (l >= 40)) { |
117 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_SECOND_NUMBER_TOO_LARGE); |
118 | 0 | goto err; |
119 | 0 | } |
120 | 186k | if (use_bn) { |
121 | 0 | if (!BN_add_word(bl, first * 40)) |
122 | 0 | goto err; |
123 | 0 | } else |
124 | 186k | l += (long)first *40; |
125 | 186k | } |
126 | 1.49M | i = 0; |
127 | 1.49M | if (use_bn) { |
128 | 0 | int blsize; |
129 | 0 | blsize = BN_num_bits(bl); |
130 | 0 | blsize = (blsize + 6) / 7; |
131 | 0 | if (blsize > tmpsize) { |
132 | 0 | if (tmp != ftmp) |
133 | 0 | OPENSSL_free(tmp); |
134 | 0 | tmpsize = blsize + 32; |
135 | 0 | tmp = OPENSSL_malloc(tmpsize); |
136 | 0 | if (tmp == NULL) |
137 | 0 | goto err; |
138 | 0 | } |
139 | 0 | while (blsize--) { |
140 | 0 | BN_ULONG t = BN_div_word(bl, 0x80L); |
141 | 0 | if (t == (BN_ULONG)-1) |
142 | 0 | goto err; |
143 | 0 | tmp[i++] = (unsigned char)t; |
144 | 0 | } |
145 | 1.49M | } else { |
146 | | |
147 | 1.67M | for (;;) { |
148 | 1.67M | tmp[i++] = (unsigned char)l & 0x7f; |
149 | 1.67M | l >>= 7L; |
150 | 1.67M | if (l == 0L) |
151 | 1.49M | break; |
152 | 1.67M | } |
153 | | |
154 | 1.49M | } |
155 | 1.49M | if (out != NULL) { |
156 | 745k | if (len + i > olen) { |
157 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_BUFFER_TOO_SMALL); |
158 | 0 | goto err; |
159 | 0 | } |
160 | 838k | while (--i > 0) |
161 | 93.1k | out[len++] = tmp[i] | 0x80; |
162 | 745k | out[len++] = tmp[0]; |
163 | 745k | } else |
164 | 745k | len += i; |
165 | 1.49M | } |
166 | 186k | if (tmp != ftmp) |
167 | 0 | OPENSSL_free(tmp); |
168 | 186k | BN_free(bl); |
169 | 186k | return len; |
170 | 0 | err: |
171 | 0 | if (tmp != ftmp) |
172 | 0 | OPENSSL_free(tmp); |
173 | 0 | BN_free(bl); |
174 | 0 | return 0; |
175 | 186k | } |
176 | | |
177 | | int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a) |
178 | 6.68M | { |
179 | 6.68M | return OBJ_obj2txt(buf, buf_len, a, 0); |
180 | 6.68M | } |
181 | | |
182 | | int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a) |
183 | 1.81M | { |
184 | 1.81M | char buf[80], *p = buf; |
185 | 1.81M | int i; |
186 | | |
187 | 1.81M | if ((a == NULL) || (a->data == NULL)) |
188 | 0 | return BIO_write(bp, "NULL", 4); |
189 | 1.81M | i = i2t_ASN1_OBJECT(buf, sizeof(buf), a); |
190 | 1.81M | if (i > (int)(sizeof(buf) - 1)) { |
191 | 14.8k | if (i > INT_MAX - 1) { /* catch an integer overflow */ |
192 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_LENGTH_TOO_LONG); |
193 | 0 | return -1; |
194 | 0 | } |
195 | 14.8k | if ((p = OPENSSL_malloc(i + 1)) == NULL) |
196 | 0 | return -1; |
197 | 14.8k | i2t_ASN1_OBJECT(p, i + 1, a); |
198 | 14.8k | } |
199 | 1.81M | if (i <= 0) { |
200 | 3.79k | i = BIO_write(bp, "<INVALID>", 9); |
201 | 3.79k | i += BIO_dump(bp, (const char *)a->data, a->length); |
202 | 3.79k | return i; |
203 | 3.79k | } |
204 | 1.81M | BIO_write(bp, p, i); |
205 | 1.81M | if (p != buf) |
206 | 14.8k | OPENSSL_free(p); |
207 | 1.81M | return i; |
208 | 1.81M | } |
209 | | |
210 | | ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp, |
211 | | long length) |
212 | 1.35M | { |
213 | 1.35M | const unsigned char *p; |
214 | 1.35M | long len; |
215 | 1.35M | int tag, xclass; |
216 | 1.35M | int inf, i; |
217 | 1.35M | ASN1_OBJECT *ret = NULL; |
218 | 1.35M | p = *pp; |
219 | 1.35M | inf = ASN1_get_object(&p, &len, &tag, &xclass, length); |
220 | 1.35M | if (inf & 0x80) { |
221 | 10.8k | i = ASN1_R_BAD_OBJECT_HEADER; |
222 | 10.8k | goto err; |
223 | 10.8k | } |
224 | | |
225 | 1.34M | if (tag != V_ASN1_OBJECT) { |
226 | 0 | i = ASN1_R_EXPECTING_AN_OBJECT; |
227 | 0 | goto err; |
228 | 0 | } |
229 | 1.34M | ret = ossl_c2i_ASN1_OBJECT(a, &p, len); |
230 | 1.34M | if (ret) |
231 | 1.29M | *pp = p; |
232 | 1.34M | return ret; |
233 | 10.8k | err: |
234 | 10.8k | ERR_raise(ERR_LIB_ASN1, i); |
235 | 10.8k | return NULL; |
236 | 1.34M | } |
237 | | |
238 | | ASN1_OBJECT *ossl_c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp, |
239 | | long len) |
240 | 26.5M | { |
241 | 26.5M | ASN1_OBJECT *ret = NULL, tobj; |
242 | 26.5M | const unsigned char *p; |
243 | 26.5M | unsigned char *data; |
244 | 26.5M | int i, length; |
245 | | |
246 | | /* |
247 | | * Sanity check OID encoding. Need at least one content octet. MSB must |
248 | | * be clear in the last octet. can't have leading 0x80 in subidentifiers, |
249 | | * see: X.690 8.19.2 |
250 | | */ |
251 | 26.5M | if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL || |
252 | 26.5M | p[len - 1] & 0x80) { |
253 | 62.3k | ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_OBJECT_ENCODING); |
254 | 62.3k | return NULL; |
255 | 62.3k | } |
256 | | /* Now 0 < len <= INT_MAX, so the cast is safe. */ |
257 | 26.4M | length = (int)len; |
258 | | /* |
259 | | * Try to lookup OID in table: these are all valid encodings so if we get |
260 | | * a match we know the OID is valid. |
261 | | */ |
262 | 26.4M | tobj.nid = NID_undef; |
263 | 26.4M | tobj.data = p; |
264 | 26.4M | tobj.length = length; |
265 | 26.4M | tobj.flags = 0; |
266 | 26.4M | i = OBJ_obj2nid(&tobj); |
267 | 26.4M | if (i != NID_undef) { |
268 | | /* |
269 | | * Return shared registered OID object: this improves efficiency |
270 | | * because we don't have to return a dynamically allocated OID |
271 | | * and NID lookups can use the cached value. |
272 | | */ |
273 | 8.36M | ret = OBJ_nid2obj(i); |
274 | 8.36M | if (a) { |
275 | 8.26M | ASN1_OBJECT_free(*a); |
276 | 8.26M | *a = ret; |
277 | 8.26M | } |
278 | 8.36M | *pp += len; |
279 | 8.36M | return ret; |
280 | 8.36M | } |
281 | 227M | for (i = 0; i < length; i++, p++) { |
282 | 209M | if (*p == 0x80 && (!i || !(p[-1] & 0x80))) { |
283 | 17.5k | ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_OBJECT_ENCODING); |
284 | 17.5k | return NULL; |
285 | 17.5k | } |
286 | 209M | } |
287 | | |
288 | 18.0M | if ((a == NULL) || ((*a) == NULL) || |
289 | 18.0M | !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) { |
290 | 17.9M | if ((ret = ASN1_OBJECT_new()) == NULL) |
291 | 0 | return NULL; |
292 | 17.9M | } else { |
293 | 89.3k | ret = (*a); |
294 | 89.3k | } |
295 | | |
296 | 18.0M | p = *pp; |
297 | | /* detach data from object */ |
298 | 18.0M | data = (unsigned char *)ret->data; |
299 | 18.0M | ret->data = NULL; |
300 | | /* once detached we can change it */ |
301 | 18.0M | if ((data == NULL) || (ret->length < length)) { |
302 | 18.0M | ret->length = 0; |
303 | 18.0M | OPENSSL_free(data); |
304 | 18.0M | data = OPENSSL_malloc(length); |
305 | 18.0M | if (data == NULL) |
306 | 0 | goto err; |
307 | 18.0M | ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA; |
308 | 18.0M | } |
309 | 18.0M | memcpy(data, p, length); |
310 | | /* If there are dynamic strings, free them here, and clear the flag */ |
311 | 18.0M | if ((ret->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) != 0) { |
312 | 0 | OPENSSL_free((char *)ret->sn); |
313 | 0 | OPENSSL_free((char *)ret->ln); |
314 | 0 | ret->flags &= ~ASN1_OBJECT_FLAG_DYNAMIC_STRINGS; |
315 | 0 | } |
316 | | /* reattach data to object, after which it remains const */ |
317 | 18.0M | ret->data = data; |
318 | 18.0M | ret->length = length; |
319 | 18.0M | ret->sn = NULL; |
320 | 18.0M | ret->ln = NULL; |
321 | | /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */ |
322 | 18.0M | p += length; |
323 | | |
324 | 18.0M | if (a != NULL) |
325 | 18.0M | (*a) = ret; |
326 | 18.0M | *pp = p; |
327 | 18.0M | return ret; |
328 | 0 | err: |
329 | 0 | ERR_raise(ERR_LIB_ASN1, i); |
330 | 0 | if ((a == NULL) || (*a != ret)) |
331 | 0 | ASN1_OBJECT_free(ret); |
332 | 0 | return NULL; |
333 | 18.0M | } |
334 | | |
335 | | ASN1_OBJECT *ASN1_OBJECT_new(void) |
336 | 36.9M | { |
337 | 36.9M | ASN1_OBJECT *ret; |
338 | | |
339 | 36.9M | ret = OPENSSL_zalloc(sizeof(*ret)); |
340 | 36.9M | if (ret == NULL) |
341 | 0 | return NULL; |
342 | 36.9M | ret->flags = ASN1_OBJECT_FLAG_DYNAMIC; |
343 | 36.9M | return ret; |
344 | 36.9M | } |
345 | | |
346 | | void ASN1_OBJECT_free(ASN1_OBJECT *a) |
347 | 61.1M | { |
348 | 61.1M | if (a == NULL) |
349 | 3.81M | return; |
350 | 57.3M | if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) { |
351 | 18.9M | #ifndef CONST_STRICT |
352 | | /* |
353 | | * Disable purely for compile-time strict const checking. Doing this |
354 | | * on a "real" compile will cause memory leaks |
355 | | */ |
356 | 18.9M | OPENSSL_free((void*)a->sn); |
357 | 18.9M | OPENSSL_free((void*)a->ln); |
358 | 18.9M | #endif |
359 | 18.9M | a->sn = a->ln = NULL; |
360 | 18.9M | } |
361 | 57.3M | if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) { |
362 | 36.9M | OPENSSL_free((void*)a->data); |
363 | 36.9M | a->data = NULL; |
364 | 36.9M | a->length = 0; |
365 | 36.9M | } |
366 | 57.3M | if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC) |
367 | 36.9M | OPENSSL_free(a); |
368 | 57.3M | } |
369 | | |
370 | | ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len, |
371 | | const char *sn, const char *ln) |
372 | 0 | { |
373 | 0 | ASN1_OBJECT o; |
374 | |
|
375 | 0 | o.sn = sn; |
376 | 0 | o.ln = ln; |
377 | 0 | o.data = data; |
378 | 0 | o.nid = nid; |
379 | 0 | o.length = len; |
380 | 0 | o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | |
381 | 0 | ASN1_OBJECT_FLAG_DYNAMIC_DATA; |
382 | 0 | return OBJ_dup(&o); |
383 | 0 | } |