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