asn1.c File Reference

ASN.1 encoding. More...

#include <stdint.h>
#include <stddef.h>
#include <errno.h>
#include <gpxe/asn1.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER)
static int asn1_start (struct asn1_cursor *cursor, unsigned int type)
 Start parsing ASN.1 object.
int asn1_enter (struct asn1_cursor *cursor, unsigned int type)
 Enter ASN.1 object.
int asn1_skip (struct asn1_cursor *cursor, unsigned int type)
 Skip ASN.1 object.


Detailed Description

ASN.1 encoding.

Definition in file asn1.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER   ) 

static int asn1_start ( struct asn1_cursor cursor,
unsigned int  type 
) [static]

Start parsing ASN.1 object.

Parameters:
cursor ASN.1 object cursor
type Expected type
Return values:
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.

If any error occurs (i.e. if the object is not of the expected type, or if we overflow beyond the end of the ASN.1 object), then the cursor will be invalidated and a negative value will be returned.

Definition at line 49 of file asn1.c.

References asn1_cursor::data, DBGC, EINVAL, ENXIO, asn1_cursor::len, and NULL.

Referenced by asn1_enter(), and asn1_skip().

00050                                                    {
00051         unsigned int len_len;
00052         unsigned int len;
00053         int rc;
00054 
00055         /* Sanity check */
00056         if ( cursor->len < 2 /* Tag byte and first length byte */ ) {
00057                 if ( cursor->len )
00058                         DBGC ( cursor, "ASN1 %p too short\n", cursor );
00059                 rc = -EINVAL;
00060                 goto notfound;
00061         }
00062 
00063         /* Check the tag byte */
00064         if ( *( ( uint8_t * ) cursor->data ) != type ) {
00065                 DBGC ( cursor, "ASN1 %p type mismatch (expected %d, got %d)\n",
00066                        cursor, type, *( ( uint8_t * ) cursor->data ) );
00067                 rc = -ENXIO;
00068                 goto notfound;
00069         }
00070         cursor->data++;
00071         cursor->len--;
00072 
00073         /* Extract length of the length field and sanity check */
00074         len_len = *( ( uint8_t * ) cursor->data );
00075         if ( len_len & 0x80 ) {
00076                 len_len = ( len_len & 0x7f );
00077                 cursor->data++;
00078                 cursor->len--;
00079         } else {
00080                 len_len = 1;
00081         }
00082         if ( cursor->len < len_len ) {
00083                 DBGC ( cursor, "ASN1 %p bad length field length %d (max "
00084                        "%zd)\n", cursor, len_len, cursor->len );
00085                 rc = -EINVAL;
00086                 goto notfound;
00087         }
00088 
00089         /* Extract the length and sanity check */
00090         for ( len = 0 ; len_len ; len_len-- ) {
00091                 len <<= 8;
00092                 len |= *( ( uint8_t * ) cursor->data );
00093                 cursor->data++;
00094                 cursor->len--;
00095         }
00096         if ( cursor->len < len ) {
00097                 DBGC ( cursor, "ASN1 %p bad length %d (max %zd)\n",
00098                        cursor, len, cursor->len );
00099                 rc = -EINVAL;
00100                 goto notfound;
00101         }
00102 
00103         return len;
00104 
00105  notfound:
00106         cursor->data = NULL;
00107         cursor->len = 0;
00108         return rc;
00109 }

int asn1_enter ( struct asn1_cursor cursor,
unsigned int  type 
)

Enter ASN.1 object.

Parameters:
cursor ASN.1 object cursor
type Expected type
Return values:
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.

Definition at line 122 of file asn1.c.

References asn1_start(), DBGC, and asn1_cursor::len.

Referenced by x509_public_key(), and x509_rsa_public_key().

00122                                                                  {
00123         int len;
00124 
00125         len = asn1_start ( cursor, type );
00126         if ( len < 0 )
00127                 return len;
00128 
00129         cursor->len = len;
00130         DBGC ( cursor, "ASN1 %p entered object type %02x (len %x)\n",
00131                cursor, type, len );
00132 
00133         return 0;
00134 }

int asn1_skip ( struct asn1_cursor cursor,
unsigned int  type 
)

Skip ASN.1 object.

Parameters:
cursor ASN.1 object cursor
type Expected type
Return values:
rc Return status code
The object cursor will be updated to point to the next ASN.1 object. If any error occurs, the object cursor will be invalidated.

Definition at line 147 of file asn1.c.

References asn1_start(), asn1_cursor::data, DBGC, ENOENT, asn1_cursor::len, and NULL.

Referenced by x509_public_key(), and x509_rsa_public_key().

00147                                                                 {
00148         int len;
00149 
00150         len = asn1_start ( cursor, type );
00151         if ( len < 0 )
00152                 return len;
00153 
00154         cursor->data += len;
00155         cursor->len -= len;
00156         DBGC ( cursor, "ASN1 %p skipped object type %02x (len %x)\n",
00157                cursor, type, len );
00158 
00159         if ( ! cursor->len ) {
00160                 DBGC ( cursor, "ASN1 %p reached end of object\n", cursor );
00161                 cursor->data = NULL;
00162                 return -ENOENT;
00163         }
00164 
00165         return 0;
00166 }


Generated on Tue Apr 6 20:01:15 2010 for gPXE by  doxygen 1.5.7.1