xref: /openssl/crypto/asn1/a_object.c (revision 7ed6de99)
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         }
155         if (out != NULL) {
156             if (len + i > olen) {
157                 ERR_raise(ERR_LIB_ASN1, ASN1_R_BUFFER_TOO_SMALL);
158                 goto err;
159             }
160             while (--i > 0)
161                 out[len++] = tmp[i] | 0x80;
162             out[len++] = tmp[0];
163         } else
164             len += i;
165     }
166     if (tmp != ftmp)
167         OPENSSL_free(tmp);
168     BN_free(bl);
169     return len;
170  err:
171     if (tmp != ftmp)
172         OPENSSL_free(tmp);
173     BN_free(bl);
174     return 0;
175 }
176 
i2t_ASN1_OBJECT(char * buf,int buf_len,const ASN1_OBJECT * a)177 int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a)
178 {
179     return OBJ_obj2txt(buf, buf_len, a, 0);
180 }
181 
i2a_ASN1_OBJECT(BIO * bp,const ASN1_OBJECT * a)182 int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a)
183 {
184     char buf[80], *p = buf;
185     int i;
186 
187     if ((a == NULL) || (a->data == NULL))
188         return BIO_write(bp, "NULL", 4);
189     i = i2t_ASN1_OBJECT(buf, sizeof(buf), a);
190     if (i > (int)(sizeof(buf) - 1)) {
191         if (i > INT_MAX - 1) {  /* catch an integer overflow */
192             ERR_raise(ERR_LIB_ASN1, ASN1_R_LENGTH_TOO_LONG);
193             return -1;
194         }
195         if ((p = OPENSSL_malloc(i + 1)) == NULL)
196             return -1;
197         i2t_ASN1_OBJECT(p, i + 1, a);
198     }
199     if (i <= 0) {
200         i = BIO_write(bp, "<INVALID>", 9);
201         if (i > 0)
202             i += BIO_dump(bp, (const char *)a->data, a->length);
203         return i;
204     }
205     BIO_write(bp, p, i);
206     if (p != buf)
207         OPENSSL_free(p);
208     return i;
209 }
210 
d2i_ASN1_OBJECT(ASN1_OBJECT ** a,const unsigned char ** pp,long length)211 ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
212                              long length)
213 {
214     const unsigned char *p;
215     long len;
216     int tag, xclass;
217     int inf, i;
218     ASN1_OBJECT *ret = NULL;
219     p = *pp;
220     inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
221     if (inf & 0x80) {
222         i = ASN1_R_BAD_OBJECT_HEADER;
223         goto err;
224     }
225 
226     if (tag != V_ASN1_OBJECT) {
227         i = ASN1_R_EXPECTING_AN_OBJECT;
228         goto err;
229     }
230     ret = ossl_c2i_ASN1_OBJECT(a, &p, len);
231     if (ret)
232         *pp = p;
233     return ret;
234  err:
235     ERR_raise(ERR_LIB_ASN1, i);
236     return NULL;
237 }
238 
ossl_c2i_ASN1_OBJECT(ASN1_OBJECT ** a,const unsigned char ** pp,long len)239 ASN1_OBJECT *ossl_c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
240                                   long len)
241 {
242     ASN1_OBJECT *ret = NULL, tobj;
243     const unsigned char *p;
244     unsigned char *data;
245     int i, length;
246 
247     /*
248      * Sanity check OID encoding. Need at least one content octet. MSB must
249      * be clear in the last octet. can't have leading 0x80 in subidentifiers,
250      * see: X.690 8.19.2
251      */
252     if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||
253         p[len - 1] & 0x80) {
254         ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
255         return NULL;
256     }
257     /* Now 0 < len <= INT_MAX, so the cast is safe. */
258     length = (int)len;
259     /*
260      * Try to lookup OID in table: these are all valid encodings so if we get
261      * a match we know the OID is valid.
262      */
263     tobj.nid = NID_undef;
264     tobj.data = p;
265     tobj.length = length;
266     tobj.flags = 0;
267     i = OBJ_obj2nid(&tobj);
268     if (i != NID_undef) {
269         /*
270          * Return shared registered OID object: this improves efficiency
271          * because we don't have to return a dynamically allocated OID
272          * and NID lookups can use the cached value.
273          */
274         ret = OBJ_nid2obj(i);
275         if (a) {
276             ASN1_OBJECT_free(*a);
277             *a = ret;
278         }
279         *pp += len;
280         return ret;
281     }
282     for (i = 0; i < length; i++, p++) {
283         if (*p == 0x80 && (!i || !(p[-1] & 0x80))) {
284             ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
285             return NULL;
286         }
287     }
288 
289     if ((a == NULL) || ((*a) == NULL) ||
290         !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
291         if ((ret = ASN1_OBJECT_new()) == NULL)
292             return NULL;
293     } else {
294         ret = (*a);
295     }
296 
297     p = *pp;
298     /* detach data from object */
299     data = (unsigned char *)ret->data;
300     ret->data = NULL;
301     /* once detached we can change it */
302     if ((data == NULL) || (ret->length < length)) {
303         ret->length = 0;
304         OPENSSL_free(data);
305         data = OPENSSL_malloc(length);
306         if (data == NULL)
307             goto err;
308         ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
309     }
310     memcpy(data, p, length);
311     /* If there are dynamic strings, free them here, and clear the flag */
312     if ((ret->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) != 0) {
313         OPENSSL_free((char *)ret->sn);
314         OPENSSL_free((char *)ret->ln);
315         ret->flags &= ~ASN1_OBJECT_FLAG_DYNAMIC_STRINGS;
316     }
317     /* reattach data to object, after which it remains const */
318     ret->data = data;
319     ret->length = length;
320     ret->sn = NULL;
321     ret->ln = NULL;
322     /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
323     p += length;
324 
325     if (a != NULL)
326         (*a) = ret;
327     *pp = p;
328     return ret;
329  err:
330     ERR_raise(ERR_LIB_ASN1, i);
331     if ((a == NULL) || (*a != ret))
332         ASN1_OBJECT_free(ret);
333     return NULL;
334 }
335 
ASN1_OBJECT_new(void)336 ASN1_OBJECT *ASN1_OBJECT_new(void)
337 {
338     ASN1_OBJECT *ret;
339 
340     ret = OPENSSL_zalloc(sizeof(*ret));
341     if (ret == NULL)
342         return NULL;
343     ret->flags = ASN1_OBJECT_FLAG_DYNAMIC;
344     return ret;
345 }
346 
ASN1_OBJECT_free(ASN1_OBJECT * a)347 void ASN1_OBJECT_free(ASN1_OBJECT *a)
348 {
349     if (a == NULL)
350         return;
351     if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
352 #ifndef CONST_STRICT
353         /*
354          * Disable purely for compile-time strict const checking.  Doing this
355          * on a "real" compile will cause memory leaks
356          */
357         OPENSSL_free((void*)a->sn);
358         OPENSSL_free((void*)a->ln);
359 #endif
360         a->sn = a->ln = NULL;
361     }
362     if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
363         OPENSSL_free((void*)a->data);
364         a->data = NULL;
365         a->length = 0;
366     }
367     if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
368         OPENSSL_free(a);
369 }
370 
ASN1_OBJECT_create(int nid,unsigned char * data,int len,const char * sn,const char * ln)371 ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,
372                                 const char *sn, const char *ln)
373 {
374     ASN1_OBJECT o;
375 
376     o.sn = sn;
377     o.ln = ln;
378     o.data = data;
379     o.nid = nid;
380     o.length = len;
381     o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
382         ASN1_OBJECT_FLAG_DYNAMIC_DATA;
383     return OBJ_dup(&o);
384 }
385