21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

data ready to go into the certificate, you can use DER and the ASN.1 specification to<br />

encode the data into an interoperable binary representation.<br />

ASN.1 specifications of data objects can be quite complex. In particular, the specification<br />

for X.509v3 is vast because X.509v3 is a highly versatile certificate format. If<br />

you plan on reading and writing DER-encoded data on your own instead of using a<br />

cryptographic library, we recommend using an ASN.1 “compiler” that can take an<br />

ASN.1 specification as input and produce C data structures and routines that encode<br />

and parse data in a DER-encoded format. The Enhanced SNACC ASN.1 compiler is<br />

available under the GNU GPL from http://www.getronicsgov.com/hot/snacc_lib.htm.<br />

If you need to do sophisticated work with certificates, you may want to look at the<br />

freeware Certificate Management Library, available from http://www.getronicsgov.com/<br />

hot/cml_home.htm. It handles most operations you can perform on X.509 certificates,<br />

including retrieving certificates from LDAP databases.<br />

Here, we’ll show you the OpenSSLAPIs for DER-encoding data objects and for converting<br />

binary data into OpenSSLdata types. All of the functions in the OpenSSLAPI<br />

either convert OpenSSL’s internal representation to a DER representation (the i2d<br />

functions) or convert DER into the internal representation (the d2i functions).<br />

The basic i2d functions output to memory and take two arguments: the object to<br />

convert to DER and a buffer into which to write the result. The second argument is a<br />

pointer to a buffer of unsigned characters, represented as unsigned char **. That is,<br />

if you are outputting into an unsigned char *x, where x doesn’t actually hold the<br />

string, but holds the address in memory where that string starts, you need to pass in<br />

the address of x.<br />

OpenSSLrequires you to pass in a pointer to a pointer because it takes<br />

your actual pointer and “advances” it. We don’t like this feature and<br />

have never found it useful. In general, you should copy over the<br />

pointer to your buffer into a temporary variable, then send in the<br />

address of the temporary variable.<br />

Note that you need to know how big a buffer to pass in as the second parameter. To<br />

figure that out, call the function with a NULL value as the second argument. That<br />

causes the function to calculate and return the size.<br />

For example, here’s how to DER-encode an RSA public key:<br />

#include <br />

/* Returns the malloc'd buffer, and puts the size of the buffer into the integer<br />

* pointed to by the second argument.<br />

*/<br />

unsigned char *DER_encode_RSA_public(RSA *rsa, int *len) {<br />

unsigned char *buf, *next;<br />

*len = i2d_RSAPublicKey(rsa, 0);<br />

Representing Public Keys and Certificates in Binary (DER Encoding) | 353<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!