2 * Copyright 1995-2017 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/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 "internal/asn1_int.h"
19 #include "asn1_locl.h"
21 int i2d_ASN1_OBJECT(const ASN1_OBJECT
*a
, unsigned char **pp
)
26 if ((a
== NULL
) || (a
->data
== NULL
))
29 objsize
= ASN1_object_size(0, a
->length
, V_ASN1_OBJECT
);
30 if (pp
== NULL
|| objsize
== -1)
34 ASN1_put_object(&p
, 0, a
->length
, V_ASN1_OBJECT
, V_ASN1_UNIVERSAL
);
35 memcpy(p
, a
->data
, a
->length
);
42 int a2d_ASN1_OBJECT(unsigned char *out
, int olen
, const char *buf
, int num
)
44 int i
, first
, len
= 0, c
, use_bn
;
45 char ftmp
[24], *tmp
= ftmp
;
46 int tmpsize
= sizeof(ftmp
);
59 if ((c
>= '0') && (c
<= '2')) {
62 ASN1err(ASN1_F_A2D_ASN1_OBJECT
, ASN1_R_FIRST_NUM_TOO_LARGE
);
67 ASN1err(ASN1_F_A2D_ASN1_OBJECT
, ASN1_R_MISSING_SECOND_NUMBER
);
75 if ((c
!= '.') && (c
!= ' ')) {
76 ASN1err(ASN1_F_A2D_ASN1_OBJECT
, ASN1_R_INVALID_SEPARATOR
);
86 if ((c
== ' ') || (c
== '.'))
88 if (!ossl_isdigit(c
)) {
89 ASN1err(ASN1_F_A2D_ASN1_OBJECT
, ASN1_R_INVALID_DIGIT
);
92 if (!use_bn
&& l
>= ((ULONG_MAX
- 80) / 10L)) {
96 if (bl
== NULL
|| !BN_set_word(bl
, l
))
100 if (!BN_mul_word(bl
, 10L)
101 || !BN_add_word(bl
, c
- '0'))
104 l
= l
* 10L + (long)(c
- '0');
107 if ((first
< 2) && (l
>= 40)) {
108 ASN1err(ASN1_F_A2D_ASN1_OBJECT
,
109 ASN1_R_SECOND_NUMBER_TOO_LARGE
);
113 if (!BN_add_word(bl
, first
* 40))
116 l
+= (long)first
*40;
121 blsize
= BN_num_bits(bl
);
122 blsize
= (blsize
+ 6) / 7;
123 if (blsize
> tmpsize
) {
126 tmpsize
= blsize
+ 32;
127 tmp
= OPENSSL_malloc(tmpsize
);
132 BN_ULONG t
= BN_div_word(bl
, 0x80L
);
133 if (t
== (BN_ULONG
)-1)
135 tmp
[i
++] = (unsigned char)t
;
140 tmp
[i
++] = (unsigned char)l
& 0x7f;
148 if (len
+ i
> olen
) {
149 ASN1err(ASN1_F_A2D_ASN1_OBJECT
, ASN1_R_BUFFER_TOO_SMALL
);
153 out
[len
++] = tmp
[i
] | 0x80;
169 int i2t_ASN1_OBJECT(char *buf
, int buf_len
, const ASN1_OBJECT
*a
)
171 return OBJ_obj2txt(buf
, buf_len
, a
, 0);
174 int i2a_ASN1_OBJECT(BIO
*bp
, const ASN1_OBJECT
*a
)
176 char buf
[80], *p
= buf
;
179 if ((a
== NULL
) || (a
->data
== NULL
))
180 return BIO_write(bp
, "NULL", 4);
181 i
= i2t_ASN1_OBJECT(buf
, sizeof(buf
), a
);
182 if (i
> (int)(sizeof(buf
) - 1)) {
183 if ((p
= OPENSSL_malloc(i
+ 1)) == NULL
) {
184 ASN1err(ASN1_F_I2A_ASN1_OBJECT
, ERR_R_MALLOC_FAILURE
);
187 i2t_ASN1_OBJECT(p
, i
+ 1, a
);
190 i
= BIO_write(bp
, "<INVALID>", 9);
191 i
+= BIO_dump(bp
, (const char *)a
->data
, a
->length
);
200 ASN1_OBJECT
*d2i_ASN1_OBJECT(ASN1_OBJECT
**a
, const unsigned char **pp
,
203 const unsigned char *p
;
207 ASN1_OBJECT
*ret
= NULL
;
209 inf
= ASN1_get_object(&p
, &len
, &tag
, &xclass
, length
);
211 i
= ASN1_R_BAD_OBJECT_HEADER
;
215 if (tag
!= V_ASN1_OBJECT
) {
216 i
= ASN1_R_EXPECTING_AN_OBJECT
;
219 ret
= c2i_ASN1_OBJECT(a
, &p
, len
);
224 ASN1err(ASN1_F_D2I_ASN1_OBJECT
, i
);
228 ASN1_OBJECT
*c2i_ASN1_OBJECT(ASN1_OBJECT
**a
, const unsigned char **pp
,
231 ASN1_OBJECT
*ret
= NULL
, tobj
;
232 const unsigned char *p
;
237 * Sanity check OID encoding. Need at least one content octet. MSB must
238 * be clear in the last octet. can't have leading 0x80 in subidentifiers,
241 if (len
<= 0 || len
> INT_MAX
|| pp
== NULL
|| (p
= *pp
) == NULL
||
243 ASN1err(ASN1_F_C2I_ASN1_OBJECT
, ASN1_R_INVALID_OBJECT_ENCODING
);
246 /* Now 0 < len <= INT_MAX, so the cast is safe. */
249 * Try to lookup OID in table: these are all valid encodings so if we get
250 * a match we know the OID is valid.
252 tobj
.nid
= NID_undef
;
254 tobj
.length
= length
;
256 i
= OBJ_obj2nid(&tobj
);
257 if (i
!= NID_undef
) {
259 * Return shared registered OID object: this improves efficiency
260 * because we don't have to return a dynamically allocated OID
261 * and NID lookups can use the cached value.
263 ret
= OBJ_nid2obj(i
);
265 ASN1_OBJECT_free(*a
);
271 for (i
= 0; i
< length
; i
++, p
++) {
272 if (*p
== 0x80 && (!i
|| !(p
[-1] & 0x80))) {
273 ASN1err(ASN1_F_C2I_ASN1_OBJECT
, ASN1_R_INVALID_OBJECT_ENCODING
);
279 * only the ASN1_OBJECTs from the 'table' will have values for ->sn or
282 if ((a
== NULL
) || ((*a
) == NULL
) ||
283 !((*a
)->flags
& ASN1_OBJECT_FLAG_DYNAMIC
)) {
284 if ((ret
= ASN1_OBJECT_new()) == NULL
)
290 /* detach data from object */
291 data
= (unsigned char *)ret
->data
;
293 /* once detached we can change it */
294 if ((data
== NULL
) || (ret
->length
< length
)) {
297 data
= OPENSSL_malloc(length
);
299 i
= ERR_R_MALLOC_FAILURE
;
302 ret
->flags
|= ASN1_OBJECT_FLAG_DYNAMIC_DATA
;
304 memcpy(data
, p
, length
);
305 /* reattach data to object, after which it remains const */
307 ret
->length
= length
;
310 /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
318 ASN1err(ASN1_F_C2I_ASN1_OBJECT
, i
);
319 if ((a
== NULL
) || (*a
!= ret
))
320 ASN1_OBJECT_free(ret
);
324 ASN1_OBJECT
*ASN1_OBJECT_new(void)
328 ret
= OPENSSL_zalloc(sizeof(*ret
));
330 ASN1err(ASN1_F_ASN1_OBJECT_NEW
, ERR_R_MALLOC_FAILURE
);
333 ret
->flags
= ASN1_OBJECT_FLAG_DYNAMIC
;
337 void ASN1_OBJECT_free(ASN1_OBJECT
*a
)
341 if (a
->flags
& ASN1_OBJECT_FLAG_DYNAMIC_STRINGS
) {
342 #ifndef CONST_STRICT /* disable purely for compile-time strict
343 * const checking. Doing this on a "real"
344 * compile will cause memory leaks */
345 OPENSSL_free((void*)a
->sn
);
346 OPENSSL_free((void*)a
->ln
);
348 a
->sn
= a
->ln
= NULL
;
350 if (a
->flags
& ASN1_OBJECT_FLAG_DYNAMIC_DATA
) {
351 OPENSSL_free((void*)a
->data
);
355 if (a
->flags
& ASN1_OBJECT_FLAG_DYNAMIC
)
359 ASN1_OBJECT
*ASN1_OBJECT_create(int nid
, unsigned char *data
, int len
,
360 const char *sn
, const char *ln
)
369 o
.flags
= ASN1_OBJECT_FLAG_DYNAMIC
| ASN1_OBJECT_FLAG_DYNAMIC_STRINGS
|
370 ASN1_OBJECT_FLAG_DYNAMIC_DATA
;