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