Rewrite X509_PUBKEY_set0_param() to use asn1_abs_set_unused_bits()
authortb <tb@openbsd.org>
Tue, 24 May 2022 19:59:14 +0000 (19:59 +0000)
committertb <tb@openbsd.org>
Tue, 24 May 2022 19:59:14 +0000 (19:59 +0000)
This streamlines the logic and uses ASN1_STRING_set0() and
asn1_abs_set_unused_bits() instead of inlining them.

ok jsing

lib/libcrypto/asn1/x_pubkey.c

index 69be6ed..a2c3d88 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: x_pubkey.c,v 1.31 2021/12/25 13:17:48 jsing Exp $ */
+/* $OpenBSD: x_pubkey.c,v 1.32 2022/05/24 19:59:14 tb Exp $ */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
@@ -716,15 +716,13 @@ X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj, int ptype,
 {
        if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
                return 0;
-       if (penc) {
-               free(pub->public_key->data);
-               pub->public_key->data = penc;
-               pub->public_key->length = penclen;
-               /* Set number of unused bits to zero */
-               pub->public_key->flags&= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07);
-               pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
-       }
-       return 1;
+
+       if (penc == NULL)
+               return 1;
+
+       ASN1_STRING_set0(pub->public_key, penc, penclen);
+
+       return asn1_abs_set_unused_bits(pub->public_key, 0);
 }
 
 int