Skip to content

Commit 86fbed9

Browse files
snhensonandi34
authored andcommitted
Fix encoding bug in i2c_ASN1_INTEGER
Fix bug where i2c_ASN1_INTEGER mishandles zero if it is marked as negative. Thanks to Huzaifa Sidhpurwala <huzaifas@redhat.com> and Hanno Böck <hanno@hboeck.de> for reporting this issue. Reviewed-by: Rich Salz <rsalz@openssl.org> (cherry-picked from a0eed48d37a4b7beea0c966caf09ad46f4a92a44) Bug: 28175332 Change-Id: I4366a15dee46a02e67746fd1642f2a7bd17f8f84
1 parent 7b976ba commit 86fbed9

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

crypto/asn1/a_int.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
124124
{
125125
ret=a->length;
126126
i=a->data[0];
127+
if (ret == 1 && i == 0)
128+
neg = 0;
127129
if (!neg && (i > 127)) {
128130
pad=1;
129131
pb=0;
@@ -157,7 +159,7 @@ int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
157159
p += a->length - 1;
158160
i = a->length;
159161
/* Copy zeros to destination as long as source is zero */
160-
while(!*n) {
162+
while(!*n && i > 1) {
161163
*(p--) = 0;
162164
n--;
163165
i--;
@@ -415,7 +417,7 @@ ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
415417
ASN1err(ASN1_F_BN_TO_ASN1_INTEGER,ERR_R_NESTED_ASN1_ERROR);
416418
goto err;
417419
}
418-
if (BN_is_negative(bn))
420+
if (BN_is_negative(bn) && !BN_is_zero(bn))
419421
ret->type = V_ASN1_NEG_INTEGER;
420422
else ret->type=V_ASN1_INTEGER;
421423
j=BN_num_bits(bn);

openssl.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,7 @@ alpn.patch \
996996
early_ccs.patch \
997997
0018-tls_fallback_scsv.patch \
998998
0019-dsa_double_free.patch \
999+
0020-encoding_neg_int.patch \
9991000
"
10001001

10011002
OPENSSL_PATCHES_progs_SOURCES="\
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
From: "Dr. Stephen Henson" <steve@openssl.org>
2+
Date: Thu, 16 Apr 2015 16:43:09 +0100
3+
Subject: [PATCH] Fix encoding bug in i2c_ASN1_INTEGER
4+
5+
Fix bug where i2c_ASN1_INTEGER mishandles zero if it is marked as
6+
negative.
7+
8+
Thanks to Huzaifa Sidhpurwala <huzaifas@redhat.com> and
9+
Hanno Böck <hanno@hboeck.de> for reporting this issue.
10+
11+
Reviewed-by: Rich Salz <rsalz@openssl.org>
12+
13+
---
14+
crypto/asn1/a_int.c | 6 ++++--
15+
1 file changed, 4 insertions(+), 2 deletions(-)
16+
17+
diff --git a/crypto/asn1/a_int.c b/crypto/asn1/a_int.c
18+
index ad0d250..6574260 100644
19+
--- a/crypto/asn1/a_int.c
20+
+++ b/crypto/asn1/a_int.c
21+
@@ -124,6 +124,8 @@ int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
22+
{
23+
ret=a->length;
24+
i=a->data[0];
25+
+ if (ret == 1 && i == 0)
26+
+ neg = 0;
27+
if (!neg && (i > 127)) {
28+
pad=1;
29+
pb=0;
30+
@@ -157,7 +159,7 @@ int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
31+
p += a->length - 1;
32+
i = a->length;
33+
/* Copy zeros to destination as long as source is zero */
34+
- while(!*n) {
35+
+ while(!*n && i > 1) {
36+
*(p--) = 0;
37+
n--;
38+
i--;
39+
@@ -415,7 +417,7 @@ ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
40+
ASN1err(ASN1_F_BN_TO_ASN1_INTEGER,ERR_R_NESTED_ASN1_ERROR);
41+
goto err;
42+
}
43+
- if (BN_is_negative(bn))
44+
+ if (BN_is_negative(bn) && !BN_is_zero(bn))
45+
ret->type = V_ASN1_NEG_INTEGER;
46+
else ret->type=V_ASN1_INTEGER;
47+
j=BN_num_bits(bn);
48+
--
49+
2.8.0.rc3.226.g39d4020
50+

0 commit comments

Comments
 (0)