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(ASN1_OBJECT *a, unsigned char **pp)
25 if ((a == NULL) || (a->data == NULL))
28 objsize = ASN1_object_size(0, a->length, V_ASN1_OBJECT);
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 tmp[i++] = (unsigned char)BN_div_word(bl, 0x80L);
135 tmp[i++] = (unsigned char)l & 0x7f;
143 if (len + i > olen) {
144 ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_BUFFER_TOO_SMALL);
148 out[len++] = tmp[i] | 0x80;
164 int i2t_ASN1_OBJECT(char *buf, int buf_len, ASN1_OBJECT *a)
166 return OBJ_obj2txt(buf, buf_len, a, 0);
169 int i2a_ASN1_OBJECT(BIO *bp, ASN1_OBJECT *a)
171 char buf[80], *p = buf;
174 if ((a == NULL) || (a->data == NULL))
175 return (BIO_write(bp, "NULL", 4));
176 i = i2t_ASN1_OBJECT(buf, sizeof buf, a);
177 if (i > (int)(sizeof(buf) - 1)) {
178 p = OPENSSL_malloc(i + 1);
181 i2t_ASN1_OBJECT(p, i + 1, a);
184 i = BIO_write(bp, "<INVALID>", 9);
185 i += BIO_dump(bp, (const char *)a->data, a->length);
194 ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
197 const unsigned char *p;
201 ASN1_OBJECT *ret = NULL;
203 inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
205 i = ASN1_R_BAD_OBJECT_HEADER;
209 if (tag != V_ASN1_OBJECT) {
210 i = ASN1_R_EXPECTING_AN_OBJECT;
213 ret = c2i_ASN1_OBJECT(a, &p, len);
218 ASN1err(ASN1_F_D2I_ASN1_OBJECT, i);
222 ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
225 ASN1_OBJECT *ret = NULL, tobj;
226 const unsigned char *p;
231 * Sanity check OID encoding. Need at least one content octet. MSB must
232 * be clear in the last octet. can't have leading 0x80 in subidentifiers,
235 if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||
237 ASN1err(ASN1_F_C2I_ASN1_OBJECT, ASN1_R_INVALID_OBJECT_ENCODING);
240 /* Now 0 < len <= INT_MAX, so the cast is safe. */
243 * Try to lookup OID in table: these are all valid encodings so if we get
244 * a match we know the OID is valid.
246 tobj.nid = NID_undef;
248 tobj.length = length;
250 i = OBJ_obj2nid(&tobj);
251 if (i != NID_undef) {
253 * Return shared registered OID object: this improves efficiency
254 * because we don't have to return a dynamically allocated OID
255 * and NID lookups can use the cached value.
257 ret = OBJ_nid2obj(i);
259 ASN1_OBJECT_free(*a);
265 for (i = 0; i < length; i++, p++) {
266 if (*p == 0x80 && (!i || !(p[-1] & 0x80))) {
267 ASN1err(ASN1_F_C2I_ASN1_OBJECT, ASN1_R_INVALID_OBJECT_ENCODING);
273 * only the ASN1_OBJECTs from the 'table' will have values for ->sn or
276 if ((a == NULL) || ((*a) == NULL) ||
277 !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
278 if ((ret = ASN1_OBJECT_new()) == NULL)
284 /* detach data from object */
285 data = (unsigned char *)ret->data;
287 /* once detached we can change it */
288 if ((data == NULL) || (ret->length < length)) {
291 data = OPENSSL_malloc(length);
293 i = ERR_R_MALLOC_FAILURE;
296 ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
298 memcpy(data, p, length);
299 /* reattach data to object, after which it remains const */
301 ret->length = length;
304 /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
312 ASN1err(ASN1_F_C2I_ASN1_OBJECT, i);
313 if ((a == NULL) || (*a != ret))
314 ASN1_OBJECT_free(ret);
318 ASN1_OBJECT *ASN1_OBJECT_new(void)
322 ret = OPENSSL_zalloc(sizeof(*ret));
324 ASN1err(ASN1_F_ASN1_OBJECT_NEW, ERR_R_MALLOC_FAILURE);
327 ret->flags = ASN1_OBJECT_FLAG_DYNAMIC;
331 void ASN1_OBJECT_free(ASN1_OBJECT *a)
335 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
336 #ifndef CONST_STRICT /* disable purely for compile-time strict
337 * const checking. Doing this on a "real"
338 * compile will cause memory leaks */
339 OPENSSL_free((void*)a->sn);
340 OPENSSL_free((void*)a->ln);
342 a->sn = a->ln = NULL;
344 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
345 OPENSSL_free((void*)a->data);
349 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
353 ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,
354 const char *sn, const char *ln)
363 o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
364 ASN1_OBJECT_FLAG_DYNAMIC_DATA;
365 return (OBJ_dup(&o));