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