Quellcode durchsuchen

improve style of length-checks

As `der_decode_asn1_length()` will now also decode a uint64 with all 0xff
the old style would overflow in that check which "wouldn't be good"^TM.

The old way the length-checks were written were kind of fine when building
on 64bit architectures, but have the same problem on 32bit.
Karel Miko vor 7 Jahren
Ursprung
Commit
3044b227f8

+ 1 - 1
src/pk/asn1/der/bit/der_decode_bit_string.c

@@ -54,7 +54,7 @@ int der_decode_bit_string(const unsigned char *in,  unsigned long inlen,
    }
    x += y;
    /* is the data len too long or too short? */
-   if ((dlen == 0) || (dlen + x > inlen)) {
+   if ((dlen == 0) || (dlen > (inlen - x))) {
        return CRYPT_INVALID_PACKET;
    }
 

+ 1 - 1
src/pk/asn1/der/bit/der_decode_raw_bit_string.c

@@ -57,7 +57,7 @@ int der_decode_raw_bit_string(const unsigned char *in,  unsigned long inlen,
    }
    x += y;
    /* is the data len too long or too short? */
-   if ((dlen == 0) || (dlen + x > inlen)) {
+   if ((dlen == 0) || (dlen > (inlen - x))) {
        return CRYPT_INVALID_PACKET;
    }
 

+ 1 - 1
src/pk/asn1/der/custom_type/der_decode_custom_type.c

@@ -95,7 +95,7 @@ int der_decode_custom_type(const unsigned char *in, unsigned long  inlen,
    }
 
    /* would this blksize overflow? */
-   if (x + blksize > inlen) {
+   if (blksize > (inlen - x)) {
       err = CRYPT_INVALID_PACKET;
       goto LBL_ERR;
    }

+ 1 - 1
src/pk/asn1/der/ia5/der_decode_ia5_string.c

@@ -58,7 +58,7 @@ int der_decode_ia5_string(const unsigned char *in, unsigned long inlen,
       return CRYPT_BUFFER_OVERFLOW;
    }
 
-   if (len + x > inlen) {
+   if (len > (inlen - x)) {
       return CRYPT_INVALID_PACKET;
    }
 

+ 1 - 1
src/pk/asn1/der/object_identifier/der_decode_object_identifier.c

@@ -56,7 +56,7 @@ int der_decode_object_identifier(const unsigned char *in,    unsigned long  inle
    }
    x += y;
 
-   if (len < 1 || (len + x) > inlen) {
+   if ((len == 0) || (len > (inlen - x))) {
       return CRYPT_INVALID_PACKET;
    }
 

+ 1 - 1
src/pk/asn1/der/octet/der_decode_octet_string.c

@@ -58,7 +58,7 @@ int der_decode_octet_string(const unsigned char *in, unsigned long inlen,
       return CRYPT_BUFFER_OVERFLOW;
    }
 
-   if (len + x > inlen) {
+   if (len > (inlen - x)) {
       return CRYPT_INVALID_PACKET;
    }
 

+ 1 - 1
src/pk/asn1/der/printable_string/der_decode_printable_string.c

@@ -58,7 +58,7 @@ int der_decode_printable_string(const unsigned char *in, unsigned long inlen,
       return CRYPT_BUFFER_OVERFLOW;
    }
 
-   if (len + x > inlen) {
+   if (len > (inlen - x)) {
       return CRYPT_INVALID_PACKET;
    }
 

+ 1 - 1
src/pk/asn1/der/sequence/der_decode_sequence_ex.c

@@ -58,7 +58,7 @@ int der_decode_sequence_ex(const unsigned char *in, unsigned long  inlen,
    x += y;
 
    /* would this blksize overflow? */
-   if (x + blksize > inlen) {
+   if (blksize > (inlen - x)) {
       return CRYPT_INVALID_PACKET;
    }
 

+ 1 - 1
src/pk/asn1/der/sequence/der_decode_sequence_flexi.c

@@ -87,7 +87,7 @@ int der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc
             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
             goto error;
-         } else if ((len + id_len + len_len) > *inlen) {
+         } 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));

+ 1 - 1
src/pk/asn1/der/teletex_string/der_decode_teletex_string.c

@@ -57,7 +57,7 @@ int der_decode_teletex_string(const unsigned char *in, unsigned long inlen,
       return CRYPT_BUFFER_OVERFLOW;
    }
 
-   if (len + x > inlen) {
+   if (len > (inlen - x)) {
       return CRYPT_INVALID_PACKET;
    }
 

+ 1 - 1
src/pk/asn1/der/utf8/der_decode_utf8_string.c

@@ -53,7 +53,7 @@ int der_decode_utf8_string(const unsigned char *in,  unsigned long inlen,
    }
    x += y;
 
-   if (len + x > inlen) {
+   if (len > (inlen - x)) {
       return CRYPT_INVALID_PACKET;
    }