From: Dr. Stephen Henson Date: Thu, 16 Apr 2015 15:43:09 +0000 (+0100) Subject: Fix encoding bug in i2c_ASN1_INTEGER X-Git-Tag: OpenSSL_0_9_8zg~19 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=303845a3b5ee7b999bf79e2e42c1489c2cc9f371;p=oweals%2Fopenssl.git 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 and Hanno Böck for reporting this issue. Reviewed-by: Rich Salz (cherry picked from commit a0eed48d37a4b7beea0c966caf09ad46f4a92a44) --- diff --git a/crypto/asn1/a_int.c b/crypto/asn1/a_int.c index b788617f00..4ff7826f45 100644 --- a/crypto/asn1/a_int.c +++ b/crypto/asn1/a_int.c @@ -124,6 +124,8 @@ int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp) else { ret = a->length; i = a->data[0]; + if (ret == 1 && i == 0) + neg = 0; if (!neg && (i > 127)) { pad = 1; pb = 0; @@ -162,7 +164,7 @@ int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp) p += a->length - 1; i = a->length; /* Copy zeros to destination as long as source is zero */ - while (!*n) { + while (!*n && i > 1) { *(p--) = 0; n--; i--; @@ -419,7 +421,7 @@ ASN1_INTEGER *BN_to_ASN1_INTEGER(BIGNUM *bn, ASN1_INTEGER *ai) ASN1err(ASN1_F_BN_TO_ASN1_INTEGER, ERR_R_NESTED_ASN1_ERROR); goto err; } - if (BN_is_negative(bn)) + if (BN_is_negative(bn) && !BN_is_zero(bn)) ret->type = V_ASN1_NEG_INTEGER; else ret->type = V_ASN1_INTEGER;