1/*
2 * Copyright 1995-2018 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
21int 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 ASN1err(ASN1_F_I2D_ASN1_OBJECT, 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
53int 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 ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_FIRST_NUM_TOO_LARGE);
74 goto err;
75 }
76
77 if (num <= 0) {
78 ASN1err(ASN1_F_A2D_ASN1_OBJECT, 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 ASN1err(ASN1_F_A2D_ASN1_OBJECT, 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 ASN1err(ASN1_F_A2D_ASN1_OBJECT, 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 ASN1err(ASN1_F_A2D_ASN1_OBJECT,
120 ASN1_R_SECOND_NUMBER_TOO_LARGE);
121 goto err;
122 }
123 if (use_bn) {
124 if (!BN_add_word(bl, first * 40))
125 goto err;
126 } else
127 l += (long)first *40;
128 }
129 i = 0;
130 if (use_bn) {
131 int blsize;
132 blsize = BN_num_bits(bl);
133 blsize = (blsize + 6) / 7;
134 if (blsize > tmpsize) {
135 if (tmp != ftmp)
136 OPENSSL_free(tmp);
137 tmpsize = blsize + 32;
138 tmp = OPENSSL_malloc(tmpsize);
139 if (tmp == NULL)
140 goto err;
141 }
142 while (blsize--) {
143 BN_ULONG t = BN_div_word(bl, 0x80L);
144 if (t == (BN_ULONG)-1)
145 goto err;
146 tmp[i++] = (unsigned char)t;
147 }
148 } else {
149
150 for (;;) {
151 tmp[i++] = (unsigned char)l & 0x7f;
152 l >>= 7L;
153 if (l == 0L)
154 break;
155 }
156
157 }
158 if (out != NULL) {
159 if (len + i > olen) {
160 ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_BUFFER_TOO_SMALL);
161 goto err;
162 }
163 while (--i > 0)
164 out[len++] = tmp[i] | 0x80;
165 out[len++] = tmp[0];
166 } else
167 len += i;
168 }
169 if (tmp != ftmp)
170 OPENSSL_free(tmp);
171 BN_free(bl);
172 return len;
173 err:
174 if (tmp != ftmp)
175 OPENSSL_free(tmp);
176 BN_free(bl);
177 return 0;
178}
179
180int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a)
181{
182 return OBJ_obj2txt(buf, buf_len, a, 0);
183}
184
185int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a)
186{
187 char buf[80], *p = buf;
188 int i;
189
190 if ((a == NULL) || (a->data == NULL))
191 return BIO_write(bp, "NULL", 4);
192 i = i2t_ASN1_OBJECT(buf, sizeof(buf), a);
193 if (i > (int)(sizeof(buf) - 1)) {
194 if ((p = OPENSSL_malloc(i + 1)) == NULL) {
195 ASN1err(ASN1_F_I2A_ASN1_OBJECT, ERR_R_MALLOC_FAILURE);
196 return -1;
197 }
198 i2t_ASN1_OBJECT(p, i + 1, a);
199 }
200 if (i <= 0) {
201 i = BIO_write(bp, "<INVALID>", 9);
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
211ASN1_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 = c2i_ASN1_OBJECT(a, &p, len);
231 if (ret)
232 *pp = p;
233 return ret;
234 err:
235 ASN1err(ASN1_F_D2I_ASN1_OBJECT, i);
236 return NULL;
237}
238
239ASN1_OBJECT *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 ASN1err(ASN1_F_C2I_ASN1_OBJECT, 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 ASN1err(ASN1_F_C2I_ASN1_OBJECT, ASN1_R_INVALID_OBJECT_ENCODING);
285 return NULL;
286 }
287 }
288
289 /*
290 * only the ASN1_OBJECTs from the 'table' will have values for ->sn or
291 * ->ln
292 */
293 if ((a == NULL) || ((*a) == NULL) ||
294 !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
295 if ((ret = ASN1_OBJECT_new()) == NULL)
296 return NULL;
297 } else
298 ret = (*a);
299
300 p = *pp;
301 /* detach data from object */
302 data = (unsigned char *)ret->data;
303 ret->data = NULL;
304 /* once detached we can change it */
305 if ((data == NULL) || (ret->length < length)) {
306 ret->length = 0;
307 OPENSSL_free(data);
308 data = OPENSSL_malloc(length);
309 if (data == NULL) {
310 i = ERR_R_MALLOC_FAILURE;
311 goto err;
312 }
313 ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
314 }
315 memcpy(data, p, length);
316 /* reattach data to object, after which it remains const */
317 ret->data = data;
318 ret->length = length;
319 ret->sn = NULL;
320 ret->ln = NULL;
321 /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
322 p += length;
323
324 if (a != NULL)
325 (*a) = ret;
326 *pp = p;
327 return ret;
328 err:
329 ASN1err(ASN1_F_C2I_ASN1_OBJECT, i);
330 if ((a == NULL) || (*a != ret))
331 ASN1_OBJECT_free(ret);
332 return NULL;
333}
334
335ASN1_OBJECT *ASN1_OBJECT_new(void)
336{
337 ASN1_OBJECT *ret;
338
339 ret = OPENSSL_zalloc(sizeof(*ret));
340 if (ret == NULL) {
341 ASN1err(ASN1_F_ASN1_OBJECT_NEW, ERR_R_MALLOC_FAILURE);
342 return NULL;
343 }
344 ret->flags = ASN1_OBJECT_FLAG_DYNAMIC;
345 return ret;
346}
347
348void ASN1_OBJECT_free(ASN1_OBJECT *a)
349{
350 if (a == NULL)
351 return;
352 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
353#ifndef CONST_STRICT /* disable purely for compile-time strict
354 * const checking. Doing this on a "real"
355 * compile will cause memory leaks */
356 OPENSSL_free((void*)a->sn);
357 OPENSSL_free((void*)a->ln);
358#endif
359 a->sn = a->ln = NULL;
360 }
361 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
362 OPENSSL_free((void*)a->data);
363 a->data = NULL;
364 a->length = 0;
365 }
366 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
367 OPENSSL_free(a);
368}
369
370ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,
371 const char *sn, const char *ln)
372{
373 ASN1_OBJECT o;
374
375 o.sn = sn;
376 o.ln = ln;
377 o.data = data;
378 o.nid = nid;
379 o.length = len;
380 o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
381 ASN1_OBJECT_FLAG_DYNAMIC_DATA;
382 return OBJ_dup(&o);
383}
384