2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
12 #include "internal/cryptlib.h"
13 #include <openssl/buffer.h>
14 #include <openssl/asn1.h>
15 #include <openssl/objects.h>
16 #include <openssl/bn.h>
17 #include "internal/asn1_int.h"
18 #include "asn1_locl.h"
20 int i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp)
25 if ((a == NULL) || (a->data == NULL))
28 objsize = ASN1_object_size(0, a->length, V_ASN1_OBJECT);
29 if (pp == NULL || objsize == -1)
33 ASN1_put_object(&p, 0, a->length, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
34 memcpy(p, a->data, a->length);
41 int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)
43 int i, first, len = 0, c, use_bn;
44 char ftmp[24], *tmp = ftmp;
45 int tmpsize = sizeof ftmp;
58 if ((c >= '0') && (c <= '2')) {
61 ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_FIRST_NUM_TOO_LARGE);
66 ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_MISSING_SECOND_NUMBER);
74 if ((c != '.') && (c != ' ')) {
75 ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_INVALID_SEPARATOR);
85 if ((c == ' ') || (c == '.'))
87 if ((c < '0') || (c > '9')) {
88 ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_INVALID_DIGIT);
91 if (!use_bn && l >= ((ULONG_MAX - 80) / 10L)) {
95 if (bl == NULL || !BN_set_word(bl, l))
99 if (!BN_mul_word(bl, 10L)
100 || !BN_add_word(bl, c - '0'))
103 l = l * 10L + (long)(c - '0');
106 if ((first < 2) && (l >= 40)) {
107 ASN1err(ASN1_F_A2D_ASN1_OBJECT,
108 ASN1_R_SECOND_NUMBER_TOO_LARGE);
112 if (!BN_add_word(bl, first * 40))
115 l += (long)first *40;
120 blsize = BN_num_bits(bl);
121 blsize = (blsize + 6) / 7;
122 if (blsize > tmpsize) {
125 tmpsize = blsize + 32;
126 tmp = OPENSSL_malloc(tmpsize);
131 BN_ULONG t = BN_div_word(bl, 0x80L);
132 if (t == (BN_ULONG)-1)
134 tmp[i++] = (unsigned char)t;
139 tmp[i++] = (unsigned char)l & 0x7f;
147 if (len + i > olen) {
148 ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_BUFFER_TOO_SMALL);
152 out[len++] = tmp[i] | 0x80;
168 int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a)
170 return OBJ_obj2txt(buf, buf_len, a, 0);
173 int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a)
175 char buf[80], *p = buf;
178 if ((a == NULL) || (a->data == NULL))
179 return (BIO_write(bp, "NULL", 4));
180 i = i2t_ASN1_OBJECT(buf, sizeof buf, a);
181 if (i > (int)(sizeof(buf) - 1)) {
182 p = OPENSSL_malloc(i + 1);
185 i2t_ASN1_OBJECT(p, i + 1, a);
188 i = BIO_write(bp, "<INVALID>", 9);
189 i += BIO_dump(bp, (const char *)a->data, a->length);
198 ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
201 const unsigned char *p;
205 ASN1_OBJECT *ret = NULL;
207 inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
209 i = ASN1_R_BAD_OBJECT_HEADER;
213 if (tag != V_ASN1_OBJECT) {
214 i = ASN1_R_EXPECTING_AN_OBJECT;
217 ret = c2i_ASN1_OBJECT(a, &p, len);
222 ASN1err(ASN1_F_D2I_ASN1_OBJECT, i);
226 ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
229 ASN1_OBJECT *ret = NULL, tobj;
230 const unsigned char *p;
235 * Sanity check OID encoding. Need at least one content octet. MSB must
236 * be clear in the last octet. can't have leading 0x80 in subidentifiers,
239 if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||
241 ASN1err(ASN1_F_C2I_ASN1_OBJECT, ASN1_R_INVALID_OBJECT_ENCODING);
244 /* Now 0 < len <= INT_MAX, so the cast is safe. */
247 * Try to lookup OID in table: these are all valid encodings so if we get
248 * a match we know the OID is valid.
250 tobj.nid = NID_undef;
252 tobj.length = length;
254 i = OBJ_obj2nid(&tobj);
255 if (i != NID_undef) {
257 * Return shared registered OID object: this improves efficiency
258 * because we don't have to return a dynamically allocated OID
259 * and NID lookups can use the cached value.
261 ret = OBJ_nid2obj(i);
263 ASN1_OBJECT_free(*a);
269 for (i = 0; i < length; i++, p++) {
270 if (*p == 0x80 && (!i || !(p[-1] & 0x80))) {
271 ASN1err(ASN1_F_C2I_ASN1_OBJECT, ASN1_R_INVALID_OBJECT_ENCODING);
277 * only the ASN1_OBJECTs from the 'table' will have values for ->sn or
280 if ((a == NULL) || ((*a) == NULL) ||
281 !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
282 if ((ret = ASN1_OBJECT_new()) == NULL)
288 /* detach data from object */
289 data = (unsigned char *)ret->data;
291 /* once detached we can change it */
292 if ((data == NULL) || (ret->length < length)) {
295 data = OPENSSL_malloc(length);
297 i = ERR_R_MALLOC_FAILURE;
300 ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
302 memcpy(data, p, length);
303 /* reattach data to object, after which it remains const */
305 ret->length = length;
308 /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
316 ASN1err(ASN1_F_C2I_ASN1_OBJECT, i);
317 if ((a == NULL) || (*a != ret))
318 ASN1_OBJECT_free(ret);
322 ASN1_OBJECT *ASN1_OBJECT_new(void)
326 ret = OPENSSL_zalloc(sizeof(*ret));
328 ASN1err(ASN1_F_ASN1_OBJECT_NEW, ERR_R_MALLOC_FAILURE);
331 ret->flags = ASN1_OBJECT_FLAG_DYNAMIC;
335 void ASN1_OBJECT_free(ASN1_OBJECT *a)
339 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
340 #ifndef CONST_STRICT /* disable purely for compile-time strict
341 * const checking. Doing this on a "real"
342 * compile will cause memory leaks */
343 OPENSSL_free((void*)a->sn);
344 OPENSSL_free((void*)a->ln);
346 a->sn = a->ln = NULL;
348 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
349 OPENSSL_free((void*)a->data);
353 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
357 ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,
358 const char *sn, const char *ln)
367 o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
368 ASN1_OBJECT_FLAG_DYNAMIC_DATA;
369 return (OBJ_dup(&o));