From 3f8937d2f3a82371022303c2e70369ce7a05f89e Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Mon, 21 Apr 2025 22:40:59 +0100 Subject: [crypto] Remove userptr_t from ASN.1 parsers Simplify the ASN.1 code by assuming that all objects are fully accessible via pointer dereferences. This allows the concept of "additional data beyond the end of the cursor" to be removed, and simplifies parsing of all ASN.1 image formats. Signed-off-by: Michael Brown --- src/crypto/asn1.c | 46 +++++++++++----------------------------------- 1 file changed, 11 insertions(+), 35 deletions(-) (limited to 'src/crypto') diff --git a/src/crypto/asn1.c b/src/crypto/asn1.c index aa57c6a8b..7b84f6fc3 100644 --- a/src/crypto/asn1.c +++ b/src/crypto/asn1.c @@ -88,7 +88,6 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); * * @v cursor ASN.1 object cursor * @v type Expected type, or ASN1_ANY - * @v extra Additional length not present within partial cursor * @ret len Length of object body, or negative error * * The object cursor will be updated to point to the start of the @@ -100,8 +99,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); * modified. If any other error occurs, the object cursor will be * invalidated. */ -static int asn1_start ( struct asn1_cursor *cursor, unsigned int type, - size_t extra ) { +static int asn1_start ( struct asn1_cursor *cursor, unsigned int type ) { unsigned int len_len; unsigned int len; @@ -145,9 +143,9 @@ static int asn1_start ( struct asn1_cursor *cursor, unsigned int type, cursor->data++; cursor->len--; } - if ( ( cursor->len + extra ) < len ) { + if ( cursor->len < len ) { DBGC ( cursor, "ASN1 %p bad length %d (max %zd)\n", - cursor, len, ( cursor->len + extra ) ); + cursor, len, cursor->len ); asn1_invalidate_cursor ( cursor ); return -EINVAL_ASN1_LEN; } @@ -156,58 +154,36 @@ static int asn1_start ( struct asn1_cursor *cursor, unsigned int type, } /** - * Enter ASN.1 partial object + * Enter ASN.1 object * * @v cursor ASN.1 object cursor * @v type Expected type, or ASN1_ANY - * @v extra Additional length beyond partial object * @ret rc Return status code * - * The object cursor and additional length will be updated to point to - * the body of the current ASN.1 object. + * The object cursor will be updated to point to the body of the + * current ASN.1 object. * * If any error occurs, the object cursor will be invalidated. */ -int asn1_enter_partial ( struct asn1_cursor *cursor, unsigned int type, - size_t *extra ) { +int asn1_enter ( struct asn1_cursor *cursor, unsigned int type ) { int len; /* Parse current object */ - len = asn1_start ( cursor, type, *extra ); + len = asn1_start ( cursor, type ); if ( len < 0 ) { asn1_invalidate_cursor ( cursor ); return len; } - /* Update cursor and additional length */ + /* Update cursor */ if ( ( ( size_t ) len ) <= cursor->len ) cursor->len = len; - assert ( ( len - cursor->len ) <= *extra ); - *extra = ( len - cursor->len ); DBGC ( cursor, "ASN1 %p entered object type %02x (len %x)\n", cursor, type, len ); return 0; } -/** - * Enter ASN.1 object - * - * @v cursor ASN.1 object cursor - * @v type Expected type, or ASN1_ANY - * @ret rc Return status code - * - * The object cursor will be updated to point to the body of the - * current ASN.1 object. - * - * If any error occurs, the object cursor will be invalidated. - */ -int asn1_enter ( struct asn1_cursor *cursor, unsigned int type ) { - static size_t no_extra = 0; - - return asn1_enter_partial ( cursor, type, &no_extra ); -} - /** * Skip ASN.1 object if present * @@ -226,7 +202,7 @@ int asn1_skip_if_exists ( struct asn1_cursor *cursor, unsigned int type ) { int len; /* Parse current object */ - len = asn1_start ( cursor, type, 0 ); + len = asn1_start ( cursor, type ); if ( len < 0 ) return len; @@ -281,7 +257,7 @@ int asn1_shrink ( struct asn1_cursor *cursor, unsigned int type ) { /* Find end of object */ memcpy ( &temp, cursor, sizeof ( temp ) ); - len = asn1_start ( &temp, type, 0 ); + len = asn1_start ( &temp, type ); if ( len < 0 ) { asn1_invalidate_cursor ( cursor ); return len; -- cgit v1.2.3-55-g7522