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