2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
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
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"
21 int i2d_ASN1_OBJECT(const ASN1_OBJECT
*a
, unsigned char **pp
)
23 unsigned char *p
, *allocated
= NULL
;
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 if ((p
= allocated
= OPENSSL_malloc(objsize
)) == NULL
) {
35 ASN1err(ASN1_F_I2D_ASN1_OBJECT
, ERR_R_MALLOC_FAILURE
);
42 ASN1_put_object(&p
, 0, a
->length
, V_ASN1_OBJECT
, V_ASN1_UNIVERSAL
);
43 memcpy(p
, a
->data
, a
->length
);
46 * If a new buffer was allocated, just return it back.
47 * If not, return the incremented buffer pointer.
49 *pp
= allocated
!= NULL
? allocated
: p
+ a
->length
;
53 int a2d_ASN1_OBJECT(unsigned char *out
, int olen
, const char *buf
, int num
)
55 int i
, first
, len
= 0, c
, use_bn
;
56 char ftmp
[24], *tmp
= ftmp
;
57 int tmpsize
= sizeof(ftmp
);
70 if ((c
>= '0') && (c
<= '2')) {
73 ASN1err(ASN1_F_A2D_ASN1_OBJECT
, ASN1_R_FIRST_NUM_TOO_LARGE
);
78 ASN1err(ASN1_F_A2D_ASN1_OBJECT
, ASN1_R_MISSING_SECOND_NUMBER
);
86 if ((c
!= '.') && (c
!= ' ')) {
87 ASN1err(ASN1_F_A2D_ASN1_OBJECT
, ASN1_R_INVALID_SEPARATOR
);
97 if ((c
== ' ') || (c
== '.'))
99 if (!ossl_isdigit(c
)) {
100 ASN1err(ASN1_F_A2D_ASN1_OBJECT
, ASN1_R_INVALID_DIGIT
);
103 if (!use_bn
&& l
>= ((ULONG_MAX
- 80) / 10L)) {
107 if (bl
== NULL
|| !BN_set_word(bl
, l
))
111 if (!BN_mul_word(bl
, 10L)
112 || !BN_add_word(bl
, c
- '0'))
115 l
= l
* 10L + (long)(c
- '0');
118 if ((first
< 2) && (l
>= 40)) {
119 ASN1err(ASN1_F_A2D_ASN1_OBJECT
,
120 ASN1_R_SECOND_NUMBER_TOO_LARGE
);
124 if (!BN_add_word(bl
, first
* 40))
127 l
+= (long)first
*40;
132 blsize
= BN_num_bits(bl
);
133 blsize
= (blsize
+ 6) / 7;
134 if (blsize
> tmpsize
) {
137 tmpsize
= blsize
+ 32;
138 tmp
= OPENSSL_malloc(tmpsize
);
143 BN_ULONG t
= BN_div_word(bl
, 0x80L
);
144 if (t
== (BN_ULONG
)-1)
146 tmp
[i
++] = (unsigned char)t
;
151 tmp
[i
++] = (unsigned char)l
& 0x7f;
159 if (len
+ i
> olen
) {
160 ASN1err(ASN1_F_A2D_ASN1_OBJECT
, ASN1_R_BUFFER_TOO_SMALL
);
164 out
[len
++] = tmp
[i
] | 0x80;
180 int i2t_ASN1_OBJECT(char *buf
, int buf_len
, const ASN1_OBJECT
*a
)
182 return OBJ_obj2txt(buf
, buf_len
, a
, 0);
185 int i2a_ASN1_OBJECT(BIO
*bp
, const ASN1_OBJECT
*a
)
187 char buf
[80], *p
= buf
;
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
);
198 i2t_ASN1_OBJECT(p
, i
+ 1, a
);
201 i
= BIO_write(bp
, "<INVALID>", 9);
202 i
+= BIO_dump(bp
, (const char *)a
->data
, a
->length
);
211 ASN1_OBJECT
*d2i_ASN1_OBJECT(ASN1_OBJECT
**a
, const unsigned char **pp
,
214 const unsigned char *p
;
218 ASN1_OBJECT
*ret
= NULL
;
220 inf
= ASN1_get_object(&p
, &len
, &tag
, &xclass
, length
);
222 i
= ASN1_R_BAD_OBJECT_HEADER
;
226 if (tag
!= V_ASN1_OBJECT
) {
227 i
= ASN1_R_EXPECTING_AN_OBJECT
;
230 ret
= c2i_ASN1_OBJECT(a
, &p
, len
);
235 ASN1err(ASN1_F_D2I_ASN1_OBJECT
, i
);
239 ASN1_OBJECT
*c2i_ASN1_OBJECT(ASN1_OBJECT
**a
, const unsigned char **pp
,
242 ASN1_OBJECT
*ret
= NULL
, tobj
;
243 const unsigned char *p
;
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,
252 if (len
<= 0 || len
> INT_MAX
|| pp
== NULL
|| (p
= *pp
) == NULL
||
254 ASN1err(ASN1_F_C2I_ASN1_OBJECT
, ASN1_R_INVALID_OBJECT_ENCODING
);
257 /* Now 0 < len <= INT_MAX, so the cast is safe. */
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.
263 tobj
.nid
= NID_undef
;
265 tobj
.length
= length
;
267 i
= OBJ_obj2nid(&tobj
);
268 if (i
!= NID_undef
) {
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.
274 ret
= OBJ_nid2obj(i
);
276 ASN1_OBJECT_free(*a
);
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
);
290 * only the ASN1_OBJECTs from the 'table' will have values for ->sn or
293 if ((a
== NULL
) || ((*a
) == NULL
) ||
294 !((*a
)->flags
& ASN1_OBJECT_FLAG_DYNAMIC
)) {
295 if ((ret
= ASN1_OBJECT_new()) == NULL
)
301 /* detach data from object */
302 data
= (unsigned char *)ret
->data
;
304 /* once detached we can change it */
305 if ((data
== NULL
) || (ret
->length
< length
)) {
308 data
= OPENSSL_malloc(length
);
310 i
= ERR_R_MALLOC_FAILURE
;
313 ret
->flags
|= ASN1_OBJECT_FLAG_DYNAMIC_DATA
;
315 memcpy(data
, p
, length
);
316 /* reattach data to object, after which it remains const */
318 ret
->length
= length
;
321 /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
329 ASN1err(ASN1_F_C2I_ASN1_OBJECT
, i
);
330 if ((a
== NULL
) || (*a
!= ret
))
331 ASN1_OBJECT_free(ret
);
335 ASN1_OBJECT
*ASN1_OBJECT_new(void)
339 ret
= OPENSSL_zalloc(sizeof(*ret
));
341 ASN1err(ASN1_F_ASN1_OBJECT_NEW
, ERR_R_MALLOC_FAILURE
);
344 ret
->flags
= ASN1_OBJECT_FLAG_DYNAMIC
;
348 void ASN1_OBJECT_free(ASN1_OBJECT
*a
)
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
);
359 a
->sn
= a
->ln
= NULL
;
361 if (a
->flags
& ASN1_OBJECT_FLAG_DYNAMIC_DATA
) {
362 OPENSSL_free((void*)a
->data
);
366 if (a
->flags
& ASN1_OBJECT_FLAG_DYNAMIC
)
370 ASN1_OBJECT
*ASN1_OBJECT_create(int nid
, unsigned char *data
, int len
,
371 const char *sn
, const char *ln
)
380 o
.flags
= ASN1_OBJECT_FLAG_DYNAMIC
| ASN1_OBJECT_FLAG_DYNAMIC_STRINGS
|
381 ASN1_OBJECT_FLAG_DYNAMIC_DATA
;