Skip to content
This repository was archived by the owner on Aug 24, 2025. It is now read-only.

Commit 6114041

Browse files
committed
i2d_ASN1_OBJECT(): allocate memory if the user didn't provide a buffer
Since 0.9.7, all i2d_ functions were documented to allocate an output buffer if the user didn't provide one, under these conditions (from the 1.0.2 documentation): For OpenSSL 0.9.7 and later if B<*out> is B<NULL> memory will be allocated for a buffer and the encoded data written to it. In this case B<*out> is not incremented and it points to the start of the data just written. i2d_ASN1_OBJECT was found not to do this, and would crash if a NULL output buffer was provided. Fixes openssl#6914 Reviewed-by: Matthias St. Pierre <Matthias.St.Pierre@ncp-e.com> (Merged from openssl#6918) (cherry picked from commit cba024d)
1 parent 9553d96 commit 6114041

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

crypto/asn1/a_object.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
int i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp)
2121
{
22-
unsigned char *p;
22+
unsigned char *p, *allocated = NULL;
2323
int objsize;
2424

2525
if ((a == NULL) || (a->data == NULL))
@@ -29,13 +29,24 @@ int i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp)
2929
if (pp == NULL || objsize == -1)
3030
return objsize;
3131

32-
p = *pp;
32+
if (*pp == NULL) {
33+
if ((p = allocated = OPENSSL_malloc(objsize)) == NULL) {
34+
ASN1err(ASN1_F_I2D_ASN1_OBJECT, ERR_R_MALLOC_FAILURE);
35+
return 0;
36+
}
37+
} else {
38+
p = *pp;
39+
}
40+
3341
ASN1_put_object(&p, 0, a->length, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
3442
memcpy(p, a->data, a->length);
35-
p += a->length;
3643

37-
*pp = p;
38-
return (objsize);
44+
/*
45+
* If a new buffer was allocated, just return it back.
46+
* If not, return the incremented buffer pointer.
47+
*/
48+
*pp = allocated != NULL ? allocated : p + a->length;
49+
return objsize;
3950
}
4051

4152
int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)

crypto/asn1/asn1_err.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ static ERR_STRING_DATA ASN1_str_functs[] = {
9595
{ERR_FUNC(ASN1_F_DO_BUF), "do_buf"},
9696
{ERR_FUNC(ASN1_F_DO_TCREATE), "do_tcreate"},
9797
{ERR_FUNC(ASN1_F_I2D_ASN1_BIO_STREAM), "i2d_ASN1_bio_stream"},
98+
{ERR_FUNC(ASN1_F_I2D_ASN1_OBJECT), "i2d_ASN1_OBJECT"},
9899
{ERR_FUNC(ASN1_F_I2D_DSA_PUBKEY), "i2d_DSA_PUBKEY"},
99100
{ERR_FUNC(ASN1_F_I2D_EC_PUBKEY), "i2d_EC_PUBKEY"},
100101
{ERR_FUNC(ASN1_F_I2D_PRIVATEKEY), "i2d_PrivateKey"},

include/openssl/asn1.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,7 @@ int ERR_load_ASN1_strings(void);
956956
# define ASN1_F_DO_BUF 142
957957
# define ASN1_F_DO_TCREATE 222
958958
# define ASN1_F_I2D_ASN1_BIO_STREAM 211
959+
# define ASN1_F_I2D_ASN1_OBJECT 143
959960
# define ASN1_F_I2D_DSA_PUBKEY 161
960961
# define ASN1_F_I2D_EC_PUBKEY 181
961962
# define ASN1_F_I2D_PRIVATEKEY 163

0 commit comments

Comments
 (0)