summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortb <>2019-04-13 18:42:23 +0000
committertb <>2019-04-13 18:42:23 +0000
commit72fa0e445948a698471c35be00d67bf6c770dd65 (patch)
tree68e67bf426c34e0c27c2e29dd3cdb3c9e1f11494
parent3d8cce08c8bebf3054e601a8c3dc21b4bd8fd790 (diff)
downloadopenbsd-72fa0e445948a698471c35be00d67bf6c770dd65.tar.gz
openbsd-72fa0e445948a698471c35be00d67bf6c770dd65.tar.bz2
openbsd-72fa0e445948a698471c35be00d67bf6c770dd65.zip
Avoid quadratic behavior of decimal BIGNUM conversion
The complexity of BN_bn2dec(bn) is quadratic in the length of bn. This function is used for printing numbers in CRLs which are typically small. If a BN is larger than 127 bits, dump it as hex because that's cheap and for numbers this size not significantly harder for humans to parse. OpenSSL commit 10a3195fcf7d04ba519651cf12e945a8fe470a3c by David Benjamin (still under the old licence), but significantly simplified. Ideally, we would catch excessively large numbers on deserialization, but that is made trickier by the templated ASN1. Erroring out is also not an option since the relevant part of the x509v3/ directory doesn't like to do proper error checking (looking at you v2i and i2v). Timeout found by oss-fuzz, should fix issues #13823 and #14130. input & ok jsing
-rw-r--r--src/lib/libcrypto/x509v3/v3_utl.c45
1 files changed, 36 insertions, 9 deletions
diff --git a/src/lib/libcrypto/x509v3/v3_utl.c b/src/lib/libcrypto/x509v3/v3_utl.c
index 4f8d16fd00..75f7662e7e 100644
--- a/src/lib/libcrypto/x509v3/v3_utl.c
+++ b/src/lib/libcrypto/x509v3/v3_utl.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: v3_utl.c,v 1.31 2018/05/19 10:50:08 tb Exp $ */ 1/* $OpenBSD: v3_utl.c,v 1.32 2019/04/13 18:42:23 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. 3 * project.
4 */ 4 */
@@ -66,6 +66,7 @@
66#include <openssl/err.h> 66#include <openssl/err.h>
67#include <openssl/x509v3.h> 67#include <openssl/x509v3.h>
68 68
69char *bnstr(const BIGNUM *bn);
69static char *strip_spaces(char *name); 70static char *strip_spaces(char *name);
70static int sk_strcmp(const char * const *a, const char * const *b); 71static int sk_strcmp(const char * const *a, const char * const *b);
71static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name, 72static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name,
@@ -148,17 +149,43 @@ X509V3_add_value_bool_nf(const char *name, int asn1_bool,
148 return 1; 149 return 1;
149} 150}
150 151
152char *
153bn_to_string(const BIGNUM *bn)
154{
155 const char *sign = "";
156 char *bnstr, *hex;
157 char *ret = NULL;
158
159 /* Only display small numbers in decimal, as conversion is quadratic. */
160 if (BN_num_bits(bn) < 128)
161 return BN_bn2dec(bn);
162
163 if ((hex = bnstr = BN_bn2hex(bn)) == NULL)
164 goto err;
165
166 if (BN_is_negative(bn)) {
167 sign = "-";
168 hex++;
169 }
170
171 if (asprintf(&ret, "%s0x%s", sign, hex) == -1)
172 ret = NULL;
173
174 err:
175 free(bnstr);
176 return ret;
177}
151 178
152char * 179char *
153i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, const ASN1_ENUMERATED *a) 180i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, const ASN1_ENUMERATED *a)
154{ 181{
155 BIGNUM *bntmp = NULL; 182 BIGNUM *bntmp;
156 char *strtmp = NULL; 183 char *strtmp = NULL;
157 184
158 if (!a) 185 if (a == NULL)
159 return NULL; 186 return NULL;
160 if (!(bntmp = ASN1_ENUMERATED_to_BN(a, NULL)) || 187 if ((bntmp = ASN1_ENUMERATED_to_BN(a, NULL)) == NULL ||
161 !(strtmp = BN_bn2dec(bntmp))) 188 (strtmp = bn_to_string(bntmp)) == NULL)
162 X509V3error(ERR_R_MALLOC_FAILURE); 189 X509V3error(ERR_R_MALLOC_FAILURE);
163 BN_free(bntmp); 190 BN_free(bntmp);
164 return strtmp; 191 return strtmp;
@@ -167,13 +194,13 @@ i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, const ASN1_ENUMERATED *a)
167char * 194char *
168i2s_ASN1_INTEGER(X509V3_EXT_METHOD *method, const ASN1_INTEGER *a) 195i2s_ASN1_INTEGER(X509V3_EXT_METHOD *method, const ASN1_INTEGER *a)
169{ 196{
170 BIGNUM *bntmp = NULL; 197 BIGNUM *bntmp;
171 char *strtmp = NULL; 198 char *strtmp = NULL;
172 199
173 if (!a) 200 if (a == NULL)
174 return NULL; 201 return NULL;
175 if (!(bntmp = ASN1_INTEGER_to_BN(a, NULL)) || 202 if ((bntmp = ASN1_INTEGER_to_BN(a, NULL)) == NULL ||
176 !(strtmp = BN_bn2dec(bntmp))) 203 (strtmp = bn_to_string(bntmp)) == NULL)
177 X509V3error(ERR_R_MALLOC_FAILURE); 204 X509V3error(ERR_R_MALLOC_FAILURE);
178 BN_free(bntmp); 205 BN_free(bntmp);
179 return strtmp; 206 return strtmp;