diff options
| author | doug <> | 2016-03-02 06:16:11 +0000 |
|---|---|---|
| committer | doug <> | 2016-03-02 06:16:11 +0000 |
| commit | f4e95f97ea9c7fe75b429a3364b1449e83bb1b81 (patch) | |
| tree | 281c8cc8af8ee09d4488676b45944435b8f68b91 /src/lib/libcrypto/bn/bn_print.c | |
| parent | 4b2ecbbafcc28f5338cb1c6af81eedcaccedc4f4 (diff) | |
| download | openbsd-f4e95f97ea9c7fe75b429a3364b1449e83bb1b81.tar.gz openbsd-f4e95f97ea9c7fe75b429a3364b1449e83bb1b81.tar.bz2 openbsd-f4e95f97ea9c7fe75b429a3364b1449e83bb1b81.zip | |
Add bounds checking for BN_hex2bn/BN_dec2bn.
Need to make sure i * 4 won't overflow. Based on OpenSSL:
commit 99ba9fd02fd481eb971023a3a0a251a37eb87e4c
input + ok bcook@
ok beck@
Diffstat (limited to 'src/lib/libcrypto/bn/bn_print.c')
| -rw-r--r-- | src/lib/libcrypto/bn/bn_print.c | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/src/lib/libcrypto/bn/bn_print.c b/src/lib/libcrypto/bn/bn_print.c index 1614a11449..2c1681a2c0 100644 --- a/src/lib/libcrypto/bn/bn_print.c +++ b/src/lib/libcrypto/bn/bn_print.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: bn_print.c,v 1.28 2015/09/28 18:58:33 deraadt Exp $ */ | 1 | /* $OpenBSD: bn_print.c,v 1.29 2016/03/02 06:16:11 doug 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 | * |
| @@ -57,6 +57,7 @@ | |||
| 57 | */ | 57 | */ |
| 58 | 58 | ||
| 59 | #include <ctype.h> | 59 | #include <ctype.h> |
| 60 | #include <limits.h> | ||
| 60 | #include <stdio.h> | 61 | #include <stdio.h> |
| 61 | 62 | ||
| 62 | #include <openssl/opensslconf.h> | 63 | #include <openssl/opensslconf.h> |
| @@ -119,7 +120,7 @@ BN_bn2dec(const BIGNUM *a) | |||
| 119 | if (buf == NULL) { | 120 | if (buf == NULL) { |
| 120 | BNerr(BN_F_BN_BN2DEC, ERR_R_MALLOC_FAILURE); | 121 | BNerr(BN_F_BN_BN2DEC, ERR_R_MALLOC_FAILURE); |
| 121 | goto err; | 122 | goto err; |
| 122 | } | 123 | } |
| 123 | p = buf; | 124 | p = buf; |
| 124 | if (BN_is_negative(a)) | 125 | if (BN_is_negative(a)) |
| 125 | *p++ = '-'; | 126 | *p++ = '-'; |
| @@ -127,7 +128,7 @@ BN_bn2dec(const BIGNUM *a) | |||
| 127 | *p++ = '\0'; | 128 | *p++ = '\0'; |
| 128 | return (buf); | 129 | return (buf); |
| 129 | } | 130 | } |
| 130 | 131 | ||
| 131 | /* get an upper bound for the length of the decimal integer | 132 | /* get an upper bound for the length of the decimal integer |
| 132 | * num <= (BN_num_bits(a) + 1) * log(2) | 133 | * num <= (BN_num_bits(a) + 1) * log(2) |
| 133 | * <= 3 * BN_num_bits(a) * 0.1001 + log(2) + 1 (rounding error) | 134 | * <= 3 * BN_num_bits(a) * 0.1001 + log(2) + 1 (rounding error) |
| @@ -197,8 +198,10 @@ BN_hex2bn(BIGNUM **bn, const char *a) | |||
| 197 | a++; | 198 | a++; |
| 198 | } | 199 | } |
| 199 | 200 | ||
| 200 | for (i = 0; isxdigit((unsigned char)a[i]); i++) | 201 | for (i = 0; i <= (INT_MAX / 4) && isxdigit((unsigned char)a[i]); i++) |
| 201 | ; | 202 | ; |
| 203 | if (i > INT_MAX / 4) | ||
| 204 | goto err; | ||
| 202 | 205 | ||
| 203 | num = i + neg; | 206 | num = i + neg; |
| 204 | if (bn == NULL) | 207 | if (bn == NULL) |
| @@ -213,7 +216,7 @@ BN_hex2bn(BIGNUM **bn, const char *a) | |||
| 213 | BN_zero(ret); | 216 | BN_zero(ret); |
| 214 | } | 217 | } |
| 215 | 218 | ||
| 216 | /* i is the number of hex digests; */ | 219 | /* i is the number of hex digits */ |
| 217 | if (bn_expand(ret, i * 4) == NULL) | 220 | if (bn_expand(ret, i * 4) == NULL) |
| 218 | goto err; | 221 | goto err; |
| 219 | 222 | ||
| @@ -271,8 +274,10 @@ BN_dec2bn(BIGNUM **bn, const char *a) | |||
| 271 | a++; | 274 | a++; |
| 272 | } | 275 | } |
| 273 | 276 | ||
| 274 | for (i = 0; isdigit((unsigned char)a[i]); i++) | 277 | for (i = 0; i <= (INT_MAX / 4) && isdigit((unsigned char)a[i]); i++) |
| 275 | ; | 278 | ; |
| 279 | if (i > INT_MAX / 4) | ||
| 280 | goto err; | ||
| 276 | 281 | ||
| 277 | num = i + neg; | 282 | num = i + neg; |
| 278 | if (bn == NULL) | 283 | if (bn == NULL) |
| @@ -288,7 +293,7 @@ BN_dec2bn(BIGNUM **bn, const char *a) | |||
| 288 | BN_zero(ret); | 293 | BN_zero(ret); |
| 289 | } | 294 | } |
| 290 | 295 | ||
| 291 | /* i is the number of digests, a bit of an over expand; */ | 296 | /* i is the number of digits, a bit of an over expand */ |
| 292 | if (bn_expand(ret, i * 4) == NULL) | 297 | if (bn_expand(ret, i * 4) == NULL) |
| 293 | goto err; | 298 | goto err; |
| 294 | 299 | ||
