Cryptography Reference
In-Depth Information
unsigned char *pem_buffer = buffer;
buffer = (unsigned char * ) malloc( buffer_length );
buffer_length = pem_decode( pem_buffer, buffer );
free( pem_buffer );
}
parse_private_key( &privkey, buffer, buffer_length );
printf( “Modulus:” );
show_hex( privkey.modulus->rep, privkey.modulus->size );
printf( “Private Exponent:” );
show_hex( privkey.exponent->rep, privkey.exponent->size );
free( buffer );
return 0;
}
#endif
This relies on the simple utility function load_file_into_memory shown in
Listing 7-16.
Listing 7-16: “fi le.c” load_fi le_into_memory
/**
* Read a whole file into memory and return a pointer to that memory chunk,
* or NULL if something went wrong. Caller must free the allocated memory.
*/
char *load_file_into_memory( char *filename, int *buffer_length )
{
int file;
struct stat file_stat;
char *buffer, *bufptr;
int buffer_size;
int bytes_read;
if ( ( file = open( filename, O_RDONLY ) ) == -1 )
{
perror( “Unable to open file” );
return NULL;
}
// Slurp the whole thing into memory
if ( fstat( file, &file_stat ) )
{
perror( “Unable to stat certificate file” );
return NULL;
}
buffer_size = file_stat.st_size;
buffer = ( char * ) malloc( buffer_size );
 
Search WWH ::




Custom Search