Properly encode 0 in ASN.1.

According to X.690 an INTEGER object always has at least one content
octet.
This commit is contained in:
Tobias Brunner
2012-06-11 17:09:20 +02:00
parent e8120632ae
commit 82e526ce81
+7 -10
View File
@@ -28,7 +28,7 @@
/** /**
* Commonly used ASN1 values. * Commonly used ASN1 values.
*/ */
const chunk_t ASN1_INTEGER_0 = chunk_from_chars(0x02, 0x00); const chunk_t ASN1_INTEGER_0 = chunk_from_chars(0x02, 0x01, 0x00);
const chunk_t ASN1_INTEGER_1 = chunk_from_chars(0x02, 0x01, 0x01); const chunk_t ASN1_INTEGER_1 = chunk_from_chars(0x02, 0x01, 0x01);
const chunk_t ASN1_INTEGER_2 = chunk_from_chars(0x02, 0x01, 0x02); const chunk_t ASN1_INTEGER_2 = chunk_from_chars(0x02, 0x01, 0x02);
@@ -761,16 +761,13 @@ chunk_t asn1_integer(const char *mode, chunk_t content)
size_t len; size_t len;
u_char *pos; u_char *pos;
if (content.len == 0 || (content.len == 1 && *content.ptr == 0x00)) if (content.len == 0)
{ { /* make sure 0 is encoded properly */
/* a zero ASN.1 integer does not have a value field */ content = chunk_from_chars(0x00);
len = 0;
}
else
{
/* ASN.1 integers must be positive numbers in two's complement */
len = content.len + ((*content.ptr & 0x80) ? 1 : 0);
} }
/* ASN.1 integers must be positive numbers in two's complement */
len = content.len + ((*content.ptr & 0x80) ? 1 : 0);
pos = asn1_build_object(&object, ASN1_INTEGER, len); pos = asn1_build_object(&object, ASN1_INTEGER, len);
if (len > content.len) if (len > content.len)
{ {