diff options
author | Richard Levitte <levitte@openssl.org> | 2018-08-11 09:59:20 +0200 |
---|---|---|
committer | Richard Levitte <levitte@openssl.org> | 2018-08-11 12:27:02 +0200 |
commit | cba024dc685d13dbcbd0577bed028ee6b295b56a (patch) | |
tree | 45aa5e834a56f1f863ab03788c644c62cf85a06b /crypto/asn1/a_object.c | |
parent | d0d0e8a71918816f7039f1f5443ebb3e28c06393 (diff) |
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 #6914
Reviewed-by: Matthias St. Pierre <Matthias.St.Pierre@ncp-e.com>
(Merged from https://github.com/openssl/openssl/pull/6918)
Diffstat (limited to 'crypto/asn1/a_object.c')
-rw-r--r-- | crypto/asn1/a_object.c | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/crypto/asn1/a_object.c b/crypto/asn1/a_object.c index 42c138c2cd..5e1424a806 100644 --- a/crypto/asn1/a_object.c +++ b/crypto/asn1/a_object.c @@ -20,7 +20,7 @@ int i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp) { - unsigned char *p; + unsigned char *p, *allocated = NULL; int objsize; if ((a == NULL) || (a->data == NULL)) @@ -30,12 +30,23 @@ int i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp) if (pp == NULL || objsize == -1) return objsize; - p = *pp; + if (*pp == NULL) { + if ((p = allocated = OPENSSL_malloc(objsize)) == NULL) { + ASN1err(ASN1_F_I2D_ASN1_OBJECT, ERR_R_MALLOC_FAILURE); + return 0; + } + } else { + p = *pp; + } + ASN1_put_object(&p, 0, a->length, V_ASN1_OBJECT, V_ASN1_UNIVERSAL); memcpy(p, a->data, a->length); - p += a->length; - *pp = p; + /* + * If a new buffer was allocated, just return it back. + * If not, return the incremented buffer pointer. + */ + *pp = allocated != NULL ? allocated : p + a->length; return objsize; } |