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.

Solution<br />

The PEM format represents DER-encoded data in a printable format. Traditionally,<br />

PEM encoding simply base64-encodes DER-encoded data and adds a simple header<br />

and footer. OpenSSLprovides an API for such functionality that handles the DER<br />

encoding and header writing for you.<br />

OpenSSLhas introduced extensions for using encrypted DER representations, allowing<br />

you to use PEM to store encrypted private keys and other cryptographic data in<br />

ASCII format.<br />

Discussion<br />

Privacy Enhanced Mail (PEM) is the original encrypted email standard. Although the<br />

standard is long dead, a small subset of its encoding mechanism has managed to survive.<br />

In today’s day and age, PEM-encoded data is usually just DER-encoded data with a<br />

header and footer. The header is a single line consisting of five dashes followed by<br />

the word “BEGIN”, followed by anything. The data following the word “BEGIN” is<br />

not really standardized. In some cases, there might not be anything following this<br />

word. However, if you are using the OpenSSLPEM outputting routines, there is a<br />

textual description of the type of data object encoded. For example, OpenSSLproduces<br />

the following header line for an RSA private key:<br />

-----BEGIN RSA PRIVATE KEY-----<br />

This is a good convention, and one that is widely used.<br />

The footer has the same format, except that “BEGIN” is replaced with “END”. You<br />

should expect that anything could follow. Again, OpenSSLuses a textual description<br />

of the content.<br />

In between the two lines is a base64-encoded DER representation, which may contain<br />

line breaks (\r\n, often called CRLFs for “carriage return and line feed”), which get<br />

ignored. We cover base64 in Recipes 4.5 and 4.6, and DER encoding in Recipe 7.16.<br />

If you want to encrypt a DER object, the original PEM format supported that as well,<br />

but no one uses these extensions today. OpenSSLdoes implement something similar.<br />

First, we’ll describe what OpenSSLdoes, because this will offer compatibility<br />

with applications built with OpenSSLthat use this format—most notably Apache<br />

with mod_ssl. Next, we’ll demonstrate how to use OpenSSL’s PEM API directly.<br />

We’ll explain this format by walking through an example. Here’s a PEM-encoded,<br />

encrypted RSA private key:<br />

-----BEGIN RSA PRIVATE KEY-----<br />

Proc-Type: 4,ENCRYPTED<br />

DEK-Info: DES-EDE3-CBC,F2D4E6438DBD4EA8<br />

356 | Chapter 7: Public Key Cryptography<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!