summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortb <>2024-05-29 16:14:38 +0000
committertb <>2024-05-29 16:14:38 +0000
commit2c2ad75207e4fbd435dd4424618c1ba29160e271 (patch)
tree7278b7924922b27a13993ec1e321b6690a17ec28
parentee6d4c1817b222ada4324d5184c408de5e9ed5ac (diff)
downloadopenbsd-2c2ad75207e4fbd435dd4424618c1ba29160e271.tar.gz
openbsd-2c2ad75207e4fbd435dd4424618c1ba29160e271.tar.bz2
openbsd-2c2ad75207e4fbd435dd4424618c1ba29160e271.zip
Fix i2d_ASN1_OBJECT()
When called with a pointer to NULL as an output buffer, one would expect an i2d API to allocate the buffer and return it. The implementation here is special and the allocation dance was forgotten, resulting in a SIGSEGV. Add said dance. ok jsing
-rw-r--r--src/lib/libcrypto/asn1/a_object.c15
1 files changed, 12 insertions, 3 deletions
diff --git a/src/lib/libcrypto/asn1/a_object.c b/src/lib/libcrypto/asn1/a_object.c
index aae1b8bbd7..ed9e9287c4 100644
--- a/src/lib/libcrypto/asn1/a_object.c
+++ b/src/lib/libcrypto/asn1/a_object.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: a_object.c,v 1.53 2024/05/29 16:10:41 tb Exp $ */ 1/* $OpenBSD: a_object.c,v 1.54 2024/05/29 16:14:38 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -615,7 +615,7 @@ c2i_ASN1_OBJECT(ASN1_OBJECT **out_aobj, const unsigned char **pp, long len)
615int 615int
616i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp) 616i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp)
617{ 617{
618 unsigned char *p; 618 unsigned char *buf, *p;
619 int objsize; 619 int objsize;
620 620
621 if (a == NULL || a->data == NULL) 621 if (a == NULL || a->data == NULL)
@@ -626,11 +626,20 @@ i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp)
626 if (pp == NULL) 626 if (pp == NULL)
627 return objsize; 627 return objsize;
628 628
629 p = *pp; 629 if ((buf = *pp) == NULL)
630 buf = calloc(1, objsize);
631 if (buf == NULL)
632 return -1;
633
634 p = buf;
630 ASN1_put_object(&p, 0, a->length, V_ASN1_OBJECT, V_ASN1_UNIVERSAL); 635 ASN1_put_object(&p, 0, a->length, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
631 memcpy(p, a->data, a->length); 636 memcpy(p, a->data, a->length);
632 p += a->length; 637 p += a->length;
633 638
639 /* If buf was allocated, return it, otherwise return the advanced p. */
640 if (*pp == NULL)
641 p = buf;
642
634 *pp = p; 643 *pp = p;
635 644
636 return objsize; 645 return objsize;