Upgrade libtomcrypt
This commit is contained in:
@@ -39,7 +39,7 @@ const int der_asn1_type_to_identifier_map[] =
|
||||
24, /* LTC_ASN1_GENERALIZEDTIME, */
|
||||
-1, /* LTC_ASN1_CUSTOM_TYPE, */
|
||||
};
|
||||
const unsigned long der_asn1_type_to_identifier_map_sz = sizeof(der_asn1_type_to_identifier_map)/sizeof(der_asn1_type_to_identifier_map[0]);
|
||||
const unsigned long der_asn1_type_to_identifier_map_sz = LTC_ARRAY_SIZE(der_asn1_type_to_identifier_map);
|
||||
|
||||
/**
|
||||
A Map from the ASN.1 Class to its string
|
||||
@@ -51,7 +51,7 @@ const char* der_asn1_class_to_string_map[] =
|
||||
"CONTEXT-SPECIFIC",
|
||||
"PRIVATE",
|
||||
};
|
||||
const unsigned long der_asn1_class_to_string_map_sz = sizeof(der_asn1_class_to_string_map)/sizeof(der_asn1_class_to_string_map[0]);
|
||||
const unsigned long der_asn1_class_to_string_map_sz = LTC_ARRAY_SIZE(der_asn1_class_to_string_map);
|
||||
|
||||
/**
|
||||
A Map from the ASN.1 P/C-bit to its string
|
||||
@@ -61,7 +61,7 @@ const char* der_asn1_pc_to_string_map[] =
|
||||
"PRIMITIVE",
|
||||
"CONSTRUCTED",
|
||||
};
|
||||
const unsigned long der_asn1_pc_to_string_map_sz = sizeof(der_asn1_pc_to_string_map)/sizeof(der_asn1_pc_to_string_map[0]);
|
||||
const unsigned long der_asn1_pc_to_string_map_sz = LTC_ARRAY_SIZE(der_asn1_pc_to_string_map);
|
||||
|
||||
/**
|
||||
A Map from the ASN.1 tag to its string
|
||||
@@ -106,7 +106,7 @@ const char* der_asn1_tag_to_string_map[] =
|
||||
"OID internationalized resource identifier type",
|
||||
"Relative OID internationalized resource identifier type",
|
||||
};
|
||||
const unsigned long der_asn1_tag_to_string_map_sz = sizeof(der_asn1_tag_to_string_map)/sizeof(der_asn1_tag_to_string_map[0]);
|
||||
const unsigned long der_asn1_tag_to_string_map_sz = LTC_ARRAY_SIZE(der_asn1_tag_to_string_map);
|
||||
|
||||
/**
|
||||
A Map from ASN.1 Tags to ltc_asn1_type
|
||||
@@ -152,6 +152,6 @@ const ltc_asn1_type der_asn1_tag_to_type_map[] =
|
||||
/* 30 */
|
||||
LTC_ASN1_CUSTOM_TYPE, /* BMPString types */
|
||||
};
|
||||
const unsigned long der_asn1_tag_to_type_map_sz = sizeof(der_asn1_tag_to_type_map)/sizeof(der_asn1_tag_to_type_map[0]);
|
||||
const unsigned long der_asn1_tag_to_type_map_sz = LTC_ARRAY_SIZE(der_asn1_tag_to_type_map);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -47,7 +47,7 @@ static const unsigned char tag_constructed_map[] =
|
||||
LTC_ASN1_PC_PRIMITIVE,
|
||||
LTC_ASN1_PC_PRIMITIVE,
|
||||
};
|
||||
static const unsigned long tag_constructed_map_sz = sizeof(tag_constructed_map)/sizeof(tag_constructed_map[0]);
|
||||
static const unsigned long tag_constructed_map_sz = LTC_ARRAY_SIZE(tag_constructed_map);
|
||||
|
||||
/**
|
||||
Decode the ASN.1 Identifier
|
||||
|
||||
@@ -119,7 +119,7 @@ static const struct {
|
||||
int der_ia5_char_encode(int c)
|
||||
{
|
||||
int x;
|
||||
for (x = 0; x < (int)(sizeof(ia5_table)/sizeof(ia5_table[0])); x++) {
|
||||
for (x = 0; x < (int)LTC_ARRAY_SIZE(ia5_table); x++) {
|
||||
if (ia5_table[x].code == c) {
|
||||
return ia5_table[x].value;
|
||||
}
|
||||
@@ -130,7 +130,7 @@ int der_ia5_char_encode(int c)
|
||||
int der_ia5_value_decode(int v)
|
||||
{
|
||||
int x;
|
||||
for (x = 0; x < (int)(sizeof(ia5_table)/sizeof(ia5_table[0])); x++) {
|
||||
for (x = 0; x < (int)LTC_ARRAY_SIZE(ia5_table); x++) {
|
||||
if (ia5_table[x].value == v) {
|
||||
return ia5_table[x].code;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ int der_encode_object_identifier(const unsigned long *words, unsigned long nwor
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* check length */
|
||||
if ((err = der_length_object_identifier(words, nwords, &x)) != CRYPT_OK) {
|
||||
if ((err = der_length_object_identifier_full(words, nwords, &x, &z)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
if (x > *outlen) {
|
||||
@@ -35,17 +35,6 @@ int der_encode_object_identifier(const unsigned long *words, unsigned long nwor
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
/* compute length to store OID data */
|
||||
z = 0;
|
||||
wordbuf = words[0] * 40 + words[1];
|
||||
for (y = 1; y < nwords; y++) {
|
||||
t = der_object_identifier_bits(wordbuf);
|
||||
z += t/7 + ((t%7) ? 1 : 0) + (wordbuf == 0 ? 1 : 0);
|
||||
if (y < nwords - 1) {
|
||||
wordbuf = words[y + 1];
|
||||
}
|
||||
}
|
||||
|
||||
/* store header + length */
|
||||
x = 0;
|
||||
out[x++] = 0x06;
|
||||
@@ -59,7 +48,7 @@ int der_encode_object_identifier(const unsigned long *words, unsigned long nwor
|
||||
wordbuf = words[0] * 40 + words[1];
|
||||
for (i = 1; i < nwords; i++) {
|
||||
/* store 7 bit words in little endian */
|
||||
t = wordbuf & 0xFFFFFFFF;
|
||||
t = wordbuf;
|
||||
if (t) {
|
||||
y = x;
|
||||
mask = 0;
|
||||
|
||||
@@ -9,27 +9,24 @@
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
unsigned long der_object_identifier_bits(unsigned long x)
|
||||
static LTC_INLINE unsigned long s_der_object_identifier_bits(unsigned long x)
|
||||
{
|
||||
#if defined(LTC_HAVE_CLZL_BUILTIN)
|
||||
if (x == 0)
|
||||
return 0;
|
||||
return sizeof(unsigned long) * CHAR_BIT - __builtin_clzl(x);
|
||||
#else
|
||||
unsigned long c;
|
||||
x &= 0xFFFFFFFF;
|
||||
c = 0;
|
||||
while (x) {
|
||||
++c;
|
||||
x >>= 1;
|
||||
}
|
||||
return c;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Gets length of DER encoding of Object Identifier
|
||||
@param nwords The number of OID words
|
||||
@param words The actual OID words to get the size of
|
||||
@param outlen [out] The length of the DER encoding for the given string
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_length_object_identifier(const unsigned long *words, unsigned long nwords, unsigned long *outlen)
|
||||
int der_length_object_identifier_full(const unsigned long *words, unsigned long nwords, unsigned long *outlen, unsigned long *datalen)
|
||||
{
|
||||
unsigned long y, z, t, wordbuf;
|
||||
|
||||
@@ -51,7 +48,7 @@ int der_length_object_identifier(const unsigned long *words, unsigned long nword
|
||||
z = 0;
|
||||
wordbuf = words[0] * 40 + words[1];
|
||||
for (y = 1; y < nwords; y++) {
|
||||
t = der_object_identifier_bits(wordbuf);
|
||||
t = s_der_object_identifier_bits(wordbuf);
|
||||
z += t/7 + ((t%7) ? 1 : 0) + (wordbuf == 0 ? 1 : 0);
|
||||
if (y < nwords - 1) {
|
||||
/* grab next word */
|
||||
@@ -59,6 +56,9 @@ int der_length_object_identifier(const unsigned long *words, unsigned long nword
|
||||
}
|
||||
}
|
||||
|
||||
if (datalen) {
|
||||
*datalen = z;
|
||||
}
|
||||
/* now depending on the length our length encoding changes */
|
||||
if (z < 128) {
|
||||
z += 2;
|
||||
@@ -74,4 +74,16 @@ int der_length_object_identifier(const unsigned long *words, unsigned long nword
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
Gets length of DER encoding of Object Identifier
|
||||
@param nwords The number of OID words
|
||||
@param words The actual OID words to get the size of
|
||||
@param outlen [out] The length of the DER encoding for the given string
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_length_object_identifier(const unsigned long *words, unsigned long nwords, unsigned long *outlen)
|
||||
{
|
||||
return der_length_object_identifier_full(words, nwords, outlen, NULL);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -91,7 +91,7 @@ static const struct {
|
||||
int der_printable_char_encode(int c)
|
||||
{
|
||||
int x;
|
||||
for (x = 0; x < (int)(sizeof(printable_table)/sizeof(printable_table[0])); x++) {
|
||||
for (x = 0; x < (int)LTC_ARRAY_SIZE(printable_table); x++) {
|
||||
if (printable_table[x].code == c) {
|
||||
return printable_table[x].value;
|
||||
}
|
||||
@@ -102,7 +102,7 @@ int der_printable_char_encode(int c)
|
||||
int der_printable_value_decode(int v)
|
||||
{
|
||||
int x;
|
||||
for (x = 0; x < (int)(sizeof(printable_table)/sizeof(printable_table[0])); x++) {
|
||||
for (x = 0; x < (int)LTC_ARRAY_SIZE(printable_table); x++) {
|
||||
if (printable_table[x].value == v) {
|
||||
return printable_table[x].code;
|
||||
}
|
||||
|
||||
@@ -31,6 +31,22 @@ static int s_new_element(ltc_asn1_list **l)
|
||||
}
|
||||
return CRYPT_OK;
|
||||
}
|
||||
#if defined(LTC_TEST_DBG)
|
||||
void s_print_err(const char *errstr, ltc_asn1_list *l, int err, unsigned long identifier, unsigned long data_offset, unsigned long len)
|
||||
{
|
||||
#if LTC_TEST_DBG <= 1
|
||||
if (err == CRYPT_OK)
|
||||
return;
|
||||
#endif
|
||||
if (l->type == LTC_ASN1_CUSTOM_TYPE) {
|
||||
fprintf(stderr, "%s %02lx: hl=%4lu l=%4lu - %s[%s %llu] (%s)\n", errstr, identifier, data_offset, len, der_asn1_class_to_string_map[l->klass], der_asn1_pc_to_string_map[l->pc], l->tag, error_to_string(err));
|
||||
} else {
|
||||
fprintf(stderr, "%s %02lx: hl=%4lu l=%4lu - %s (%s)\n", errstr, identifier, data_offset, len, der_asn1_tag_to_string_map[l->tag], error_to_string(err));
|
||||
}
|
||||
}
|
||||
#else
|
||||
#define s_print_err(errstr, l, err, identifier, data_offset, len) LTC_UNUSED_PARAM(data_offset)
|
||||
#endif
|
||||
|
||||
/**
|
||||
ASN.1 DER Flexi(ble) decoder will decode arbitrary DER packets and create a linked list of the decoded elements.
|
||||
@@ -43,7 +59,8 @@ static int s_new_element(ltc_asn1_list **l)
|
||||
static int s_der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out, unsigned long depth)
|
||||
{
|
||||
ltc_asn1_list *l;
|
||||
unsigned long err, identifier, len, totlen, data_offset, id_len, len_len;
|
||||
int err;
|
||||
unsigned long identifier, len, totlen, data_offset, id_len, len_len;
|
||||
void *realloc_tmp;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
@@ -77,30 +94,19 @@ static int s_der_decode_sequence_flexi(const unsigned char *in, unsigned long *i
|
||||
if (l->type != LTC_ASN1_EOL) {
|
||||
/* fetch length */
|
||||
len_len = *inlen - id_len;
|
||||
#if defined(LTC_TEST_DBG)
|
||||
/* init with dummy values for error cases */
|
||||
data_offset = 666;
|
||||
len = 0;
|
||||
#endif
|
||||
if ((err = der_decode_asn1_length(&in[id_len], &len_len, &len)) != CRYPT_OK) {
|
||||
#if defined(LTC_TEST_DBG)
|
||||
fprintf(stderr, "E1 %02lx: hl=%4lu l=%4lu - %s (%s)\n", identifier, data_offset, len, der_asn1_tag_to_string_map[l->tag], error_to_string(err));
|
||||
#endif
|
||||
s_print_err("E1", l, err, identifier, data_offset, len);
|
||||
goto error;
|
||||
} else if (len > (*inlen - id_len - len_len)) {
|
||||
err = CRYPT_INVALID_PACKET;
|
||||
#if defined(LTC_TEST_DBG)
|
||||
fprintf(stderr, "E2 %02lx: hl=%4lu l=%4lu - %s (%s)\n", identifier, data_offset, len, der_asn1_tag_to_string_map[l->tag], error_to_string(err));
|
||||
#endif
|
||||
s_print_err("E2", l, err, identifier, data_offset, len);
|
||||
goto error;
|
||||
}
|
||||
data_offset = id_len + len_len;
|
||||
#if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
|
||||
if (l->type == LTC_ASN1_CUSTOM_TYPE && l->klass == LTC_ASN1_CL_CONTEXT_SPECIFIC) {
|
||||
fprintf(stderr, "OK %02lx: hl=%4lu l=%4lu - Context Specific[%s %llu]\n", identifier, data_offset, len, der_asn1_pc_to_string_map[l->pc], l->tag);
|
||||
} else {
|
||||
fprintf(stderr, "OK %02lx: hl=%4lu l=%4lu - %s\n", identifier, data_offset, len, der_asn1_tag_to_string_map[l->tag]);
|
||||
}
|
||||
#endif
|
||||
s_print_err("OK", l, err, identifier, data_offset, len);
|
||||
len += data_offset;
|
||||
|
||||
if (l->type == LTC_ASN1_CUSTOM_TYPE) {
|
||||
|
||||
@@ -135,7 +135,7 @@ static const struct {
|
||||
int der_teletex_char_encode(int c)
|
||||
{
|
||||
int x;
|
||||
for (x = 0; x < (int)(sizeof(teletex_table)/sizeof(teletex_table[0])); x++) {
|
||||
for (x = 0; x < (int)LTC_ARRAY_SIZE(teletex_table); x++) {
|
||||
if (teletex_table[x].code == c) {
|
||||
return teletex_table[x].value;
|
||||
}
|
||||
@@ -146,7 +146,7 @@ int der_teletex_char_encode(int c)
|
||||
int der_teletex_value_decode(int v)
|
||||
{
|
||||
int x;
|
||||
for (x = 0; x < (int)(sizeof(teletex_table)/sizeof(teletex_table[0])); x++) {
|
||||
for (x = 0; x < (int)LTC_ARRAY_SIZE(teletex_table); x++) {
|
||||
if (teletex_table[x].value == v) {
|
||||
return teletex_table[x].code;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user