Cryptography Reference
In-Depth Information
As you can see, the recursive defi nition of the asn1struct structure makes
cleanup and traversal very straightforward.
The asn1parse Code in Action
To see this code in action, put together a sample main routine as in Listing 5-7
that takes as input a certifi cate fi le (or any other ASN.1 DER-encoded fi le) and
output the ASN.1 structure elements.
Listing 5-7: “asn1.c” test routine
#ifdef TEST_ASN1
int main( int argc, char *argv[ ] )
{
int certificate_file;
struct stat certificate_file_stat;
unsigned char *buffer, *bufptr;
int buffer_size;
int bytes_read;
struct asn1struct certificate;
if ( argc < 2 )
{
fprintf( stderr, “Usage: %s <certificate file>\n”, argv[ 0 ] );
exit( 0 );
}
if ( ( certificate_file = open( argv[ 1 ], O_RDONLY ) ) == -1 )
{
perror( “Unable to open certificate file” );
return 1;
}
// Slurp the whole thing into memory
if ( fstat( certificate_file, &certificate_file_stat ) )
{
perror( “Unable to stat certificate file” );
return 2;
}
buffer_size = certificate_file_stat.st_size;
buffer = ( char * ) malloc( buffer_size );
(Continued)
 
Search WWH ::




Custom Search