]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/a_object.c
Update copyright year
[thirdparty/openssl.git] / crypto / asn1 / a_object.c
CommitLineData
2039c421 1/*
3c2bdd7d 2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
365a2d99 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
2039c421
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
452ae49d 11#include <limits.h>
25f2138b 12#include "crypto/ctype.h"
b39fc560 13#include "internal/cryptlib.h"
ec577822
BM
14#include <openssl/buffer.h>
15#include <openssl/asn1.h>
16#include <openssl/objects.h>
1e26a8ba 17#include <openssl/bn.h>
25f2138b 18#include "crypto/asn1.h"
706457b7 19#include "asn1_local.h"
d02b48c6 20
e83f154f 21int i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp)
0f113f3e 22{
cba024dc 23 unsigned char *p, *allocated = NULL;
0f113f3e 24 int objsize;
d02b48c6 25
0f113f3e 26 if ((a == NULL) || (a->data == NULL))
26a7d938 27 return 0;
d02b48c6 28
0f113f3e 29 objsize = ASN1_object_size(0, a->length, V_ASN1_OBJECT);
56f9953c 30 if (pp == NULL || objsize == -1)
0f113f3e 31 return objsize;
d02b48c6 32
cba024dc
RL
33 if (*pp == NULL) {
34 if ((p = allocated = OPENSSL_malloc(objsize)) == NULL) {
9311d0c4 35 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
cba024dc
RL
36 return 0;
37 }
38 } else {
39 p = *pp;
40 }
41
0f113f3e
MC
42 ASN1_put_object(&p, 0, a->length, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
43 memcpy(p, a->data, a->length);
d02b48c6 44
cba024dc
RL
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;
26a7d938 50 return objsize;
0f113f3e 51}
d02b48c6 52
6343829a 53int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)
0f113f3e
MC
54{
55 int i, first, len = 0, c, use_bn;
56 char ftmp[24], *tmp = ftmp;
cbe29648 57 int tmpsize = sizeof(ftmp);
0f113f3e
MC
58 const char *p;
59 unsigned long l;
60 BIGNUM *bl = NULL;
d02b48c6 61
0f113f3e 62 if (num == 0)
26a7d938 63 return 0;
0f113f3e
MC
64 else if (num == -1)
65 num = strlen(buf);
d02b48c6 66
0f113f3e
MC
67 p = buf;
68 c = *(p++);
69 num--;
70 if ((c >= '0') && (c <= '2')) {
71 first = c - '0';
72 } else {
9311d0c4 73 ERR_raise(ERR_LIB_ASN1, ASN1_R_FIRST_NUM_TOO_LARGE);
0f113f3e
MC
74 goto err;
75 }
d02b48c6 76
0f113f3e 77 if (num <= 0) {
9311d0c4 78 ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_SECOND_NUMBER);
0f113f3e
MC
79 goto err;
80 }
81 c = *(p++);
82 num--;
83 for (;;) {
84 if (num <= 0)
85 break;
86 if ((c != '.') && (c != ' ')) {
9311d0c4 87 ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_SEPARATOR);
0f113f3e
MC
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;
a1df06b3 99 if (!ossl_isdigit(c)) {
9311d0c4 100 ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_DIGIT);
0f113f3e
MC
101 goto err;
102 }
103 if (!use_bn && l >= ((ULONG_MAX - 80) / 10L)) {
104 use_bn = 1;
90945fa3 105 if (bl == NULL)
0f113f3e 106 bl = BN_new();
90945fa3 107 if (bl == NULL || !BN_set_word(bl, l))
0f113f3e
MC
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)) {
9311d0c4 119 ERR_raise(ERR_LIB_ASN1, ASN1_R_SECOND_NUMBER_TOO_LARGE);
0f113f3e
MC
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);
90945fa3 138 if (tmp == NULL)
0f113f3e
MC
139 goto err;
140 }
8b9afbc0
DSH
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 }
0f113f3e 147 } else {
452ae49d 148
0f113f3e
MC
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) {
9311d0c4 159 ERR_raise(ERR_LIB_ASN1, ASN1_R_BUFFER_TOO_SMALL);
0f113f3e
MC
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);
23a1d5e9 170 BN_free(bl);
26a7d938 171 return len;
0f113f3e
MC
172 err:
173 if (tmp != ftmp)
174 OPENSSL_free(tmp);
23a1d5e9 175 BN_free(bl);
26a7d938 176 return 0;
0f113f3e 177}
d02b48c6 178
e83f154f 179int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a)
3e3d2ea2 180{
0f113f3e 181 return OBJ_obj2txt(buf, buf_len, a, 0);
3e3d2ea2 182}
58964a49 183
e83f154f 184int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a)
0f113f3e
MC
185{
186 char buf[80], *p = buf;
187 int i;
58964a49 188
0f113f3e 189 if ((a == NULL) || (a->data == NULL))
26a7d938 190 return BIO_write(bp, "NULL", 4);
cbe29648 191 i = i2t_ASN1_OBJECT(buf, sizeof(buf), a);
0f113f3e 192 if (i > (int)(sizeof(buf) - 1)) {
08066980
P
193 if (i > INT_MAX - 1) { /* catch an integer overflow */
194 ERR_raise(ERR_LIB_ASN1, ASN1_R_LENGTH_TOO_LONG);
195 return -1;
196 }
cdb10bae 197 if ((p = OPENSSL_malloc(i + 1)) == NULL) {
9311d0c4 198 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
0f113f3e 199 return -1;
cdb10bae 200 }
0f113f3e
MC
201 i2t_ASN1_OBJECT(p, i + 1, a);
202 }
203 if (i <= 0) {
204 i = BIO_write(bp, "<INVALID>", 9);
205 i += BIO_dump(bp, (const char *)a->data, a->length);
206 return i;
207 }
208 BIO_write(bp, p, i);
209 if (p != buf)
210 OPENSSL_free(p);
26a7d938 211 return i;
0f113f3e 212}
d02b48c6 213
875a644a 214ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
0f113f3e 215 long length)
284ef5f3 216{
0f113f3e
MC
217 const unsigned char *p;
218 long len;
219 int tag, xclass;
220 int inf, i;
221 ASN1_OBJECT *ret = NULL;
222 p = *pp;
223 inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
224 if (inf & 0x80) {
225 i = ASN1_R_BAD_OBJECT_HEADER;
226 goto err;
227 }
d02b48c6 228
0f113f3e
MC
229 if (tag != V_ASN1_OBJECT) {
230 i = ASN1_R_EXPECTING_AN_OBJECT;
231 goto err;
232 }
adf7e6d1 233 ret = ossl_c2i_ASN1_OBJECT(a, &p, len);
0f113f3e
MC
234 if (ret)
235 *pp = p;
236 return ret;
237 err:
9311d0c4 238 ERR_raise(ERR_LIB_ASN1, i);
26a7d938 239 return NULL;
284ef5f3 240}
0042fb5f 241
adf7e6d1
SL
242ASN1_OBJECT *ossl_c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
243 long len)
0f113f3e 244{
331bf00b 245 ASN1_OBJECT *ret = NULL, tobj;
0f113f3e
MC
246 const unsigned char *p;
247 unsigned char *data;
248 int i, length;
0042fb5f 249
0f113f3e
MC
250 /*
251 * Sanity check OID encoding. Need at least one content octet. MSB must
252 * be clear in the last octet. can't have leading 0x80 in subidentifiers,
253 * see: X.690 8.19.2
254 */
255 if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||
256 p[len - 1] & 0x80) {
9311d0c4 257 ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
0f113f3e
MC
258 return NULL;
259 }
260 /* Now 0 < len <= INT_MAX, so the cast is safe. */
261 length = (int)len;
331bf00b
DSH
262 /*
263 * Try to lookup OID in table: these are all valid encodings so if we get
264 * a match we know the OID is valid.
265 */
266 tobj.nid = NID_undef;
267 tobj.data = p;
268 tobj.length = length;
269 tobj.flags = 0;
270 i = OBJ_obj2nid(&tobj);
271 if (i != NID_undef) {
272 /*
273 * Return shared registered OID object: this improves efficiency
274 * because we don't have to return a dynamically allocated OID
275 * and NID lookups can use the cached value.
276 */
277 ret = OBJ_nid2obj(i);
278 if (a) {
279 ASN1_OBJECT_free(*a);
280 *a = ret;
281 }
282 *pp += len;
283 return ret;
284 }
0f113f3e
MC
285 for (i = 0; i < length; i++, p++) {
286 if (*p == 0x80 && (!i || !(p[-1] & 0x80))) {
9311d0c4 287 ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
0f113f3e
MC
288 return NULL;
289 }
290 }
284ef5f3 291
0f113f3e
MC
292 /*
293 * only the ASN1_OBJECTs from the 'table' will have values for ->sn or
294 * ->ln
295 */
296 if ((a == NULL) || ((*a) == NULL) ||
297 !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
298 if ((ret = ASN1_OBJECT_new()) == NULL)
26a7d938 299 return NULL;
0f113f3e
MC
300 } else
301 ret = (*a);
284ef5f3 302
0f113f3e
MC
303 p = *pp;
304 /* detach data from object */
305 data = (unsigned char *)ret->data;
306 ret->data = NULL;
307 /* once detached we can change it */
308 if ((data == NULL) || (ret->length < length)) {
309 ret->length = 0;
b548a1f1 310 OPENSSL_free(data);
b196e7d9 311 data = OPENSSL_malloc(length);
0f113f3e
MC
312 if (data == NULL) {
313 i = ERR_R_MALLOC_FAILURE;
314 goto err;
315 }
316 ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
317 }
318 memcpy(data, p, length);
319 /* reattach data to object, after which it remains const */
320 ret->data = data;
321 ret->length = length;
322 ret->sn = NULL;
323 ret->ln = NULL;
324 /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
325 p += length;
d02b48c6 326
0f113f3e
MC
327 if (a != NULL)
328 (*a) = ret;
329 *pp = p;
26a7d938 330 return ret;
0f113f3e 331 err:
9311d0c4 332 ERR_raise(ERR_LIB_ASN1, i);
2ace7450 333 if ((a == NULL) || (*a != ret))
0f113f3e 334 ASN1_OBJECT_free(ret);
26a7d938 335 return NULL;
0f113f3e 336}
d02b48c6 337
6b691a5c 338ASN1_OBJECT *ASN1_OBJECT_new(void)
0f113f3e
MC
339{
340 ASN1_OBJECT *ret;
d02b48c6 341
64b25758 342 ret = OPENSSL_zalloc(sizeof(*ret));
0f113f3e 343 if (ret == NULL) {
9311d0c4 344 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
26a7d938 345 return NULL;
0f113f3e 346 }
0f113f3e 347 ret->flags = ASN1_OBJECT_FLAG_DYNAMIC;
26a7d938 348 return ret;
0f113f3e 349}
d02b48c6 350
6b691a5c 351void ASN1_OBJECT_free(ASN1_OBJECT *a)
0f113f3e
MC
352{
353 if (a == NULL)
354 return;
355 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
08066980
P
356#ifndef CONST_STRICT
357 /*
358 * Disable purely for compile-time strict const checking. Doing this
359 * on a "real" compile will cause memory leaks
360 */
b548a1f1
RS
361 OPENSSL_free((void*)a->sn);
362 OPENSSL_free((void*)a->ln);
e778802f 363#endif
0f113f3e
MC
364 a->sn = a->ln = NULL;
365 }
366 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
b548a1f1 367 OPENSSL_free((void*)a->data);
0f113f3e
MC
368 a->data = NULL;
369 a->length = 0;
370 }
371 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
372 OPENSSL_free(a);
373}
d02b48c6 374
6343829a 375ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,
0f113f3e
MC
376 const char *sn, const char *ln)
377{
378 ASN1_OBJECT o;
d02b48c6 379
0f113f3e
MC
380 o.sn = sn;
381 o.ln = ln;
382 o.data = data;
383 o.nid = nid;
384 o.length = len;
385 o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
386 ASN1_OBJECT_FLAG_DYNAMIC_DATA;
26a7d938 387 return OBJ_dup(&o);
0f113f3e 388}