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.

Finally, when you’re finished using a key object, be sure to destroy the object by calling<br />

CryptDestroyKey( ) and passing the handle to the object to be destroyed. Likewise,<br />

when you’re done with a provider context, you must release it by calling<br />

CryptReleaseContext( ).<br />

See Also<br />

Recipes 5.4, 5.17, 5.22, 5.26, 5.27, 13.2<br />

5.26 Creating a CryptoAPI Key Object from Raw<br />

Key Data<br />

<strong>Problem</strong><br />

You have a symmetric key from another API, such as OpenSSL, that you would like<br />

to use with CryptoAPI. Therefore, you must create a CryptoAPI key object with the<br />

key data.<br />

Solution<br />

The Microsoft CryptoAPI is designed to prevent unintentional disclosure of sensitive<br />

key information. To do this, key information is stored in opaque data objects by the<br />

Cryptographic Service Provider (CSP) used to create the key object. Key data is<br />

exportable from key objects, but the data must be encrypted with another key to prevent<br />

accidental disclosure of the raw key data.<br />

Discussion<br />

In Recipe 5.25, we created a convenience function, SpcGetCryptContext( ), for<br />

obtaining a handle to a CSP context object. This function uses the CRYPT_<br />

VERIFYCONTEXT flag with the underlying CryptAcquireContext( ) function, which<br />

serves to prevent the use of private keys with the obtained context object. To be able<br />

to import and export symmetric encryption keys, you need to obtain a handle to a<br />

CSP context object without that flag, and use that CSP context object for creating the<br />

keys you wish to use. We’ll create a new function called SpcGetExportableContext( )<br />

that will return a CSP context object suitable for creating, importing, and exporting<br />

symmetric encryption keys.<br />

#include <br />

#include <br />

HCRYPTPROV SpcGetExportableContext(void) {<br />

HCRYPTPROV hProvider;<br />

244 | Chapter 5: Symmetric Encryption<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!