From: Jouni Malinen Date: Sat, 13 Mar 2021 15:26:54 +0000 (+0200) Subject: ASN.1: Reject invalid definite long form length values in DER encoding X-Git-Tag: hostap_2_10~418 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ee76493bbd77c8f3a636beac5540dbbe0543f00d;p=thirdparty%2Fhostap.git ASN.1: Reject invalid definite long form length values in DER encoding The definite long form for the length is allowed only for cases where the definite short form cannot be used, i.e., if the length is 128 or greater. This was not previously enforced and as such, multiple different encoding options for the same length could have been accepted. Perform more strict checks to reject invalid cases for the definite long form for the length. This is needed for a compliant implementation and this is especially important for the case of verifying DER encoded signatures to prevent potential forging attacks. Signed-off-by: Jouni Malinen --- diff --git a/src/tls/asn1.c b/src/tls/asn1.c index 57e2d5387..04d532049 100644 --- a/src/tls/asn1.c +++ b/src/tls/asn1.c @@ -231,6 +231,11 @@ int asn1_get_next(const u8 *buf, size_t len, struct asn1_hdr *hdr) } tmp &= 0x7f; /* number of subsequent octets */ hdr->length = 0; + if (tmp == 0 || pos == end || *pos == 0) { + wpa_printf(MSG_DEBUG, + "ASN.1: Definite long form of the length does not start with a nonzero value"); + return -1; + } if (tmp > 4) { wpa_printf(MSG_DEBUG, "ASN.1: Too long length field"); return -1; @@ -243,6 +248,11 @@ int asn1_get_next(const u8 *buf, size_t len, struct asn1_hdr *hdr) } hdr->length = (hdr->length << 8) | *pos++; } + if (hdr->length < 128) { + wpa_printf(MSG_DEBUG, + "ASN.1: Definite long form of the length used with too short length"); + return -1; + } } else { /* Short form - length 0..127 in one octet */ hdr->length = tmp;