summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortb <>2023-03-06 12:00:27 +0000
committertb <>2023-03-06 12:00:27 +0000
commitde31bc5ff1a6f2187a998c7febfb7a19e7950c31 (patch)
tree58f9ac138ad5e7e4c2a1248f9b0276ba592de1fd
parentc32ff5d590d375133b0f6ea63f64836460523ada (diff)
downloadopenbsd-de31bc5ff1a6f2187a998c7febfb7a19e7950c31.tar.gz
openbsd-de31bc5ff1a6f2187a998c7febfb7a19e7950c31.tar.bz2
openbsd-de31bc5ff1a6f2187a998c7febfb7a19e7950c31.zip
Rework asn1_item_flags_i2d()
Flip the logic of NULL checks on out and *out to unindent, use calloc() instead of malloc() and check on assign. Also drop the newly added len2 again, it isn't needed. ok jsing
-rw-r--r--src/lib/libcrypto/asn1/tasn_enc.c39
1 files changed, 20 insertions, 19 deletions
diff --git a/src/lib/libcrypto/asn1/tasn_enc.c b/src/lib/libcrypto/asn1/tasn_enc.c
index 1b9cfa1a0e..6e0524c39f 100644
--- a/src/lib/libcrypto/asn1/tasn_enc.c
+++ b/src/lib/libcrypto/asn1/tasn_enc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: tasn_enc.c,v 1.28 2023/03/06 08:08:31 tb Exp $ */ 1/* $OpenBSD: tasn_enc.c,v 1.29 2023/03/06 12:00:27 tb Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
@@ -106,27 +106,28 @@ static int
106asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it, 106asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it,
107 int flags) 107 int flags)
108{ 108{
109 if (out && !*out) { 109 unsigned char *p, *buf;
110 unsigned char *p, *buf; 110 int len;
111 int len, len2; 111
112 len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags); 112 if (out == NULL || *out != NULL)
113 if (len <= 0) 113 return ASN1_item_ex_i2d(&val, out, it, -1, flags);
114 return len; 114
115 buf = malloc(len); 115 if ((len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags)) <= 0)
116 if (!buf)
117 return -1;
118 p = buf;
119 len2 = ASN1_item_ex_i2d(&val, &p, it, -1, flags);
120 if (len2 != len) {
121 freezero(buf, len);
122 ASN1error(ASN1_R_LENGTH_ERROR);
123 return -1;
124 }
125 *out = buf;
126 return len; 116 return len;
117
118 if ((buf = calloc(1, len)) == NULL)
119 return -1;
120
121 p = buf;
122 if (ASN1_item_ex_i2d(&val, &p, it, -1, flags) != len) {
123 freezero(buf, len);
124 ASN1error(ASN1_R_LENGTH_ERROR);
125 return -1;
127 } 126 }
128 127
129 return ASN1_item_ex_i2d(&val, out, it, -1, flags); 128 *out = buf;
129
130 return len;
130} 131}
131 132
132/* Encode an item, taking care of IMPLICIT tagging (if any). 133/* Encode an item, taking care of IMPLICIT tagging (if any).