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.

if (!CryptAcquireContext(&hProvider, 0, MS_ENHANCED_PROV, PROV_RSA_FULL, 0)) {<br />

if (GetLastError( ) != NTE_BAD_KEYSET) return 0;<br />

if (!CryptAcquireContext(&hProvider, 0, MS_ENHANCED_PROV, PROV_RSA_FULL,<br />

CRYPT_NEWKEYSET)) return 0;<br />

}<br />

return hProvider;<br />

}<br />

SpcGetExportableContext( ) will obtain a handle to the Microsoft Enhanced Cryptographic<br />

Service Provider that allows for the use of private keys. Public key pairs are<br />

stored in containers by the underlying CSP. This function will use the default container,<br />

creating it if it doesn’t already exist.<br />

Every public key container can have a special public key pair known as an exchange<br />

key, which is the key that we’ll use to encrypt the exported key data. The function<br />

CryptGetUserKey( ) is used to obtain the exchange key. If it doesn’t exist,<br />

SpcImportKeyData( ), listed later in this section, will create a 1,024-bit exchange key,<br />

which will be stored as the exchange key in the public key container so future<br />

attempts to get the key will succeed. The special algorithm identifier AT_KEYEXCHANGE<br />

is used to reference the exchange key.<br />

Symmetric keys are always imported via CryptImportKey( ) in “simple blob” format,<br />

specified by the SIMPLEBLOB constant passed to CryptImportKey( ). A simple blob is<br />

composed of a BLOBHEADER structure, followed by an ALG_ID for the algorithm used to<br />

encrypt the key data. The raw key data follows the BLOBHEADER and ALG_ID header<br />

information. To import the raw key data into a CryptoAPI key, a simple blob structure<br />

must be constructed and passed to CryptImportKey( ).<br />

Finally, the raw key data must be encrypted using CryptEncrypt( ) and the exchange<br />

key. (The CryptEncrypt( ) function is described in more detail in Recipe 5.25.) The<br />

return from SpcImportKeyData( ) will be a handle to a CryptoAPI key object if the<br />

operation was performed successfully; otherwise, it will be 0. The CryptoAPI makes<br />

a copy of the key data internally in the key object it creates, so the key data passed<br />

into the function may be safely freed. The spc_memset( ) function from Recipe 13.2 is<br />

used here to destroy the unencrypted key data before returning.<br />

HCRYPTKEY SpcImportKeyData(HCRYPTPROV hProvider, ALG_ID Algid, BYTE *pbKeyData,<br />

DWORD cbKeyData) {<br />

BOOL bResult = FALSE;<br />

BYTE *pbData = 0;<br />

DWORD cbData, cbHeaderLen, cbKeyLen, dwDataLen;<br />

ALG_ID *pAlgid;<br />

HCRYPTKEY hImpKey = 0, hKey;<br />

BLOBHEADER *pBlob;<br />

if (!CryptGetUserKey(hProvider, AT_KEYEXCHANGE, &hImpKey)) {<br />

if (GetLastError( ) != NTE_NO_KEY) goto done;<br />

if (!CryptGenKey(hProvider, AT_KEYEXCHANGE, (1024

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

Saved successfully!

Ooh no, something went wrong!