Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[crypto] Allow for parsing of partial ASN.1 cursors
Allow code to create a partial ASN.1 cursor containing only the type
and length bytes, so that asn1_start() may be used to determine the
length of a large ASN.1 blob without first allocating memory to hold
the entire blob.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
  • Loading branch information
mcb30 committed Jul 28, 2016
1 parent 5846ce2 commit 296670a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/crypto/asn1.c
Expand Up @@ -86,14 +86,15 @@ 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
* object body (i.e. the first byte following the length byte(s)), and
* the length of the object body (i.e. the number of bytes until the
* following object tag, if any) is returned.
*/
static int asn1_start ( struct asn1_cursor *cursor, unsigned int type ) {
int asn1_start ( struct asn1_cursor *cursor, unsigned int type, size_t extra ) {
unsigned int len_len;
unsigned int len;

Expand Down Expand Up @@ -135,9 +136,9 @@ static int asn1_start ( struct asn1_cursor *cursor, unsigned int type ) {
cursor->data++;
cursor->len--;
}
if ( cursor->len < len ) {
if ( ( cursor->len + extra ) < len ) {
DBGC ( cursor, "ASN1 %p bad length %d (max %zd)\n",
cursor, len, cursor->len );
cursor, len, ( cursor->len + extra ) );
return -EINVAL_ASN1_LEN;
}

Expand All @@ -158,7 +159,7 @@ static int asn1_start ( struct asn1_cursor *cursor, unsigned int type ) {
int asn1_enter ( struct asn1_cursor *cursor, unsigned int type ) {
int len;

len = asn1_start ( cursor, type );
len = asn1_start ( cursor, type, 0 );
if ( len < 0 ) {
asn1_invalidate_cursor ( cursor );
return len;
Expand All @@ -185,7 +186,7 @@ int asn1_enter ( struct asn1_cursor *cursor, unsigned int type ) {
int asn1_skip_if_exists ( struct asn1_cursor *cursor, unsigned int type ) {
int len;

len = asn1_start ( cursor, type );
len = asn1_start ( cursor, type, 0 );
if ( len < 0 )
return len;

Expand Down Expand Up @@ -242,7 +243,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 );
len = asn1_start ( &temp, type, 0 );
if ( len < 0 ) {
asn1_invalidate_cursor ( cursor );
return len;
Expand Down
2 changes: 2 additions & 0 deletions src/include/ipxe/asn1.h
Expand Up @@ -337,6 +337,8 @@ asn1_type ( const struct asn1_cursor *cursor ) {
return ( ( cursor->len >= sizeof ( *type ) ) ? *type : ASN1_END );
}

extern int asn1_start ( struct asn1_cursor *cursor, unsigned int type,
size_t extra );
extern int asn1_enter ( struct asn1_cursor *cursor, unsigned int type );
extern int asn1_skip_if_exists ( struct asn1_cursor *cursor,
unsigned int type );
Expand Down

0 comments on commit 296670a

Please sign in to comment.