/src/openssl/crypto/asn1/a_object.c
Line  | Count  | Source (jump to first uncovered line)  | 
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  |  |  | 
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  | 0  | { | 
53  | 0  |     int i, first, len = 0, c, use_bn;  | 
54  | 0  |     char ftmp[24], *tmp = ftmp;  | 
55  | 0  |     int tmpsize = sizeof(ftmp);  | 
56  | 0  |     const char *p;  | 
57  | 0  |     unsigned long l;  | 
58  | 0  |     BIGNUM *bl = NULL;  | 
59  |  | 
  | 
60  | 0  |     if (num == 0)  | 
61  | 0  |         return 0;  | 
62  | 0  |     else if (num == -1)  | 
63  | 0  |         num = strlen(buf);  | 
64  |  |  | 
65  | 0  |     p = buf;  | 
66  | 0  |     c = *(p++);  | 
67  | 0  |     num--;  | 
68  | 0  |     if ((c >= '0') && (c <= '2')) { | 
69  | 0  |         first = c - '0';  | 
70  | 0  |     } else { | 
71  | 0  |         ERR_raise(ERR_LIB_ASN1, ASN1_R_FIRST_NUM_TOO_LARGE);  | 
72  | 0  |         goto err;  | 
73  | 0  |     }  | 
74  |  |  | 
75  | 0  |     if (num <= 0) { | 
76  | 0  |         ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_SECOND_NUMBER);  | 
77  | 0  |         goto err;  | 
78  | 0  |     }  | 
79  | 0  |     c = *(p++);  | 
80  | 0  |     num--;  | 
81  | 0  |     for (;;) { | 
82  | 0  |         if (num <= 0)  | 
83  | 0  |             break;  | 
84  | 0  |         if ((c != '.') && (c != ' ')) { | 
85  | 0  |             ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_SEPARATOR);  | 
86  | 0  |             goto err;  | 
87  | 0  |         }  | 
88  | 0  |         l = 0;  | 
89  | 0  |         use_bn = 0;  | 
90  | 0  |         for (;;) { | 
91  | 0  |             if (num <= 0)  | 
92  | 0  |                 break;  | 
93  | 0  |             num--;  | 
94  | 0  |             c = *(p++);  | 
95  | 0  |             if ((c == ' ') || (c == '.'))  | 
96  | 0  |                 break;  | 
97  | 0  |             if (!ossl_isdigit(c)) { | 
98  | 0  |                 ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_DIGIT);  | 
99  | 0  |                 goto err;  | 
100  | 0  |             }  | 
101  | 0  |             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  | 0  |             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  | 0  |                 l = l * 10L + (long)(c - '0');  | 
114  | 0  |         }  | 
115  | 0  |         if (len == 0) { | 
116  | 0  |             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  | 0  |             if (use_bn) { | 
121  | 0  |                 if (!BN_add_word(bl, first * 40))  | 
122  | 0  |                     goto err;  | 
123  | 0  |             } else  | 
124  | 0  |                 l += (long)first *40;  | 
125  | 0  |         }  | 
126  | 0  |         i = 0;  | 
127  | 0  |         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  | 0  |         } else { | 
146  |  | 
  | 
147  | 0  |             for (;;) { | 
148  | 0  |                 tmp[i++] = (unsigned char)l & 0x7f;  | 
149  | 0  |                 l >>= 7L;  | 
150  | 0  |                 if (l == 0L)  | 
151  | 0  |                     break;  | 
152  | 0  |             }  | 
153  |  | 
  | 
154  | 0  |         }  | 
155  | 0  |         if (out != NULL) { | 
156  | 0  |             if (len + i > olen) { | 
157  | 0  |                 ERR_raise(ERR_LIB_ASN1, ASN1_R_BUFFER_TOO_SMALL);  | 
158  | 0  |                 goto err;  | 
159  | 0  |             }  | 
160  | 0  |             while (--i > 0)  | 
161  | 0  |                 out[len++] = tmp[i] | 0x80;  | 
162  | 0  |             out[len++] = tmp[0];  | 
163  | 0  |         } else  | 
164  | 0  |             len += i;  | 
165  | 0  |     }  | 
166  | 0  |     if (tmp != ftmp)  | 
167  | 0  |         OPENSSL_free(tmp);  | 
168  | 0  |     BN_free(bl);  | 
169  | 0  |     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  | 0  | }  | 
176  |  |  | 
177  |  | int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a)  | 
178  | 0  | { | 
179  | 0  |     return OBJ_obj2txt(buf, buf_len, a, 0);  | 
180  | 0  | }  | 
181  |  |  | 
182  |  | int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a)  | 
183  | 0  | { | 
184  | 0  |     char buf[80], *p = buf;  | 
185  | 0  |     int i;  | 
186  |  | 
  | 
187  | 0  |     if ((a == NULL) || (a->data == NULL))  | 
188  | 0  |         return BIO_write(bp, "NULL", 4);  | 
189  | 0  |     i = i2t_ASN1_OBJECT(buf, sizeof(buf), a);  | 
190  | 0  |     if (i > (int)(sizeof(buf) - 1)) { | 
191  | 0  |         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  | 0  |         if ((p = OPENSSL_malloc(i + 1)) == NULL)  | 
196  | 0  |             return -1;  | 
197  | 0  |         i2t_ASN1_OBJECT(p, i + 1, a);  | 
198  | 0  |     }  | 
199  | 0  |     if (i <= 0) { | 
200  | 0  |         i = BIO_write(bp, "<INVALID>", 9);  | 
201  | 0  |         if (i > 0)  | 
202  | 0  |             i += BIO_dump(bp, (const char *)a->data, a->length);  | 
203  | 0  |         return i;  | 
204  | 0  |     }  | 
205  | 0  |     BIO_write(bp, p, i);  | 
206  | 0  |     if (p != buf)  | 
207  | 0  |         OPENSSL_free(p);  | 
208  | 0  |     return i;  | 
209  | 0  | }  | 
210  |  |  | 
211  |  | ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,  | 
212  |  |                              long length)  | 
213  | 0  | { | 
214  | 0  |     const unsigned char *p;  | 
215  | 0  |     long len;  | 
216  | 0  |     int tag, xclass;  | 
217  | 0  |     int inf, i;  | 
218  | 0  |     ASN1_OBJECT *ret = NULL;  | 
219  | 0  |     p = *pp;  | 
220  | 0  |     inf = ASN1_get_object(&p, &len, &tag, &xclass, length);  | 
221  | 0  |     if (inf & 0x80) { | 
222  | 0  |         i = ASN1_R_BAD_OBJECT_HEADER;  | 
223  | 0  |         goto err;  | 
224  | 0  |     }  | 
225  |  |  | 
226  | 0  |     if (tag != V_ASN1_OBJECT) { | 
227  | 0  |         i = ASN1_R_EXPECTING_AN_OBJECT;  | 
228  | 0  |         goto err;  | 
229  | 0  |     }  | 
230  | 0  |     ret = ossl_c2i_ASN1_OBJECT(a, &p, len);  | 
231  | 0  |     if (ret)  | 
232  | 0  |         *pp = p;  | 
233  | 0  |     return ret;  | 
234  | 0  |  err:  | 
235  | 0  |     ERR_raise(ERR_LIB_ASN1, i);  | 
236  | 0  |     return NULL;  | 
237  | 0  | }  | 
238  |  |  | 
239  |  | ASN1_OBJECT *ossl_c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,  | 
240  |  |                                   long len)  | 
241  | 0  | { | 
242  | 0  |     ASN1_OBJECT *ret = NULL, tobj;  | 
243  | 0  |     const unsigned char *p;  | 
244  | 0  |     unsigned char *data;  | 
245  | 0  |     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  | 0  |     if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||  | 
253  | 0  |         p[len - 1] & 0x80) { | 
254  | 0  |         ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_OBJECT_ENCODING);  | 
255  | 0  |         return NULL;  | 
256  | 0  |     }  | 
257  |  |     /* Now 0 < len <= INT_MAX, so the cast is safe. */  | 
258  | 0  |     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  | 0  |     tobj.nid = NID_undef;  | 
264  | 0  |     tobj.data = p;  | 
265  | 0  |     tobj.length = length;  | 
266  | 0  |     tobj.flags = 0;  | 
267  | 0  |     i = OBJ_obj2nid(&tobj);  | 
268  | 0  |     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  | 0  |         ret = OBJ_nid2obj(i);  | 
275  | 0  |         if (a) { | 
276  | 0  |             ASN1_OBJECT_free(*a);  | 
277  | 0  |             *a = ret;  | 
278  | 0  |         }  | 
279  | 0  |         *pp += len;  | 
280  | 0  |         return ret;  | 
281  | 0  |     }  | 
282  | 0  |     for (i = 0; i < length; i++, p++) { | 
283  | 0  |         if (*p == 0x80 && (!i || !(p[-1] & 0x80))) { | 
284  | 0  |             ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_OBJECT_ENCODING);  | 
285  | 0  |             return NULL;  | 
286  | 0  |         }  | 
287  | 0  |     }  | 
288  |  |  | 
289  | 0  |     if ((a == NULL) || ((*a) == NULL) ||  | 
290  | 0  |         !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) { | 
291  | 0  |         if ((ret = ASN1_OBJECT_new()) == NULL)  | 
292  | 0  |             return NULL;  | 
293  | 0  |     } else { | 
294  | 0  |         ret = (*a);  | 
295  | 0  |     }  | 
296  |  |  | 
297  | 0  |     p = *pp;  | 
298  |  |     /* detach data from object */  | 
299  | 0  |     data = (unsigned char *)ret->data;  | 
300  | 0  |     ret->data = NULL;  | 
301  |  |     /* once detached we can change it */  | 
302  | 0  |     if ((data == NULL) || (ret->length < length)) { | 
303  | 0  |         ret->length = 0;  | 
304  | 0  |         OPENSSL_free(data);  | 
305  | 0  |         data = OPENSSL_malloc(length);  | 
306  | 0  |         if (data == NULL)  | 
307  | 0  |             goto err;  | 
308  | 0  |         ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;  | 
309  | 0  |     }  | 
310  | 0  |     memcpy(data, p, length);  | 
311  |  |     /* If there are dynamic strings, free them here, and clear the flag */  | 
312  | 0  |     if ((ret->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) != 0) { | 
313  | 0  |         OPENSSL_free((char *)ret->sn);  | 
314  | 0  |         OPENSSL_free((char *)ret->ln);  | 
315  | 0  |         ret->flags &= ~ASN1_OBJECT_FLAG_DYNAMIC_STRINGS;  | 
316  | 0  |     }  | 
317  |  |     /* reattach data to object, after which it remains const */  | 
318  | 0  |     ret->data = data;  | 
319  | 0  |     ret->length = length;  | 
320  | 0  |     ret->sn = NULL;  | 
321  | 0  |     ret->ln = NULL;  | 
322  |  |     /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */  | 
323  | 0  |     p += length;  | 
324  |  | 
  | 
325  | 0  |     if (a != NULL)  | 
326  | 0  |         (*a) = ret;  | 
327  | 0  |     *pp = p;  | 
328  | 0  |     return ret;  | 
329  | 0  |  err:  | 
330  | 0  |     ERR_raise(ERR_LIB_ASN1, i);  | 
331  | 0  |     if ((a == NULL) || (*a != ret))  | 
332  | 0  |         ASN1_OBJECT_free(ret);  | 
333  | 0  |     return NULL;  | 
334  | 0  | }  | 
335  |  |  | 
336  |  | ASN1_OBJECT *ASN1_OBJECT_new(void)  | 
337  | 0  | { | 
338  | 0  |     ASN1_OBJECT *ret;  | 
339  |  | 
  | 
340  | 0  |     ret = OPENSSL_zalloc(sizeof(*ret));  | 
341  | 0  |     if (ret == NULL)  | 
342  | 0  |         return NULL;  | 
343  | 0  |     ret->flags = ASN1_OBJECT_FLAG_DYNAMIC;  | 
344  | 0  |     return ret;  | 
345  | 0  | }  | 
346  |  |  | 
347  |  | void ASN1_OBJECT_free(ASN1_OBJECT *a)  | 
348  | 600  | { | 
349  | 600  |     if (a == NULL)  | 
350  | 0  |         return;  | 
351  | 600  |     if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) { | 
352  | 0  | #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  | 0  |         OPENSSL_free((void*)a->sn);  | 
358  | 0  |         OPENSSL_free((void*)a->ln);  | 
359  | 0  | #endif  | 
360  | 0  |         a->sn = a->ln = NULL;  | 
361  | 0  |     }  | 
362  | 600  |     if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) { | 
363  | 0  |         OPENSSL_free((void*)a->data);  | 
364  | 0  |         a->data = NULL;  | 
365  | 0  |         a->length = 0;  | 
366  | 0  |     }  | 
367  | 600  |     if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)  | 
368  | 0  |         OPENSSL_free(a);  | 
369  | 600  | }  | 
370  |  |  | 
371  |  | ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,  | 
372  |  |                                 const char *sn, const char *ln)  | 
373  | 0  | { | 
374  | 0  |     ASN1_OBJECT o;  | 
375  |  | 
  | 
376  | 0  |     o.sn = sn;  | 
377  | 0  |     o.ln = ln;  | 
378  | 0  |     o.data = data;  | 
379  | 0  |     o.nid = nid;  | 
380  | 0  |     o.length = len;  | 
381  | 0  |     o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |  | 
382  | 0  |         ASN1_OBJECT_FLAG_DYNAMIC_DATA;  | 
383  | 0  |     return OBJ_dup(&o);  | 
384  | 0  | }  |