[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 <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2025-04-21 22:40:59 +01:00
parent 04d0b2fdf9
commit 3f8937d2f3
10 changed files with 70 additions and 109 deletions

View File

@@ -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;
}
@@ -155,41 +153,6 @@ static int asn1_start ( struct asn1_cursor *cursor, unsigned int type,
return len;
}
/**
* Enter ASN.1 partial 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.
*
* 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 len;
/* Parse current object */
len = asn1_start ( cursor, type, *extra );
if ( len < 0 ) {
asn1_invalidate_cursor ( cursor );
return len;
}
/* Update cursor and additional length */
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
*
@@ -203,9 +166,22 @@ int asn1_enter_partial ( struct asn1_cursor *cursor, unsigned int type,
* 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;
int len;
return asn1_enter_partial ( cursor, type, &no_extra );
/* Parse current object */
len = asn1_start ( cursor, type );
if ( len < 0 ) {
asn1_invalidate_cursor ( cursor );
return len;
}
/* Update cursor */
if ( ( ( size_t ) len ) <= cursor->len )
cursor->len = len;
DBGC ( cursor, "ASN1 %p entered object type %02x (len %x)\n",
cursor, type, len );
return 0;
}
/**
@@ -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;