21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

cbData = cbKeyData;<br />

cbHeaderLen = sizeof(BLOBHEADER) + sizeof(ALG_ID);<br />

if (!CryptEncrypt(hImpKey, 0, TRUE, 0, 0, &cbData, cbData)) goto done;<br />

if (!(pbData = (BYTE *)LocalAlloc(LMEM_FIXED, cbData + cbHeaderLen)))<br />

goto done;<br />

CopyMemory(pbData + cbHeaderLen, pbKeyData, cbKeyData);<br />

cbKeyLen = cbKeyData;<br />

if (!CryptEncrypt(hImpKey, 0, TRUE, 0, pbData + cbHeaderLen, &cbKeyLen, cbData))<br />

goto done;<br />

pBlob = (BLOBHEADER *)pbData;<br />

pAlgid = (ALG_ID *)(pbData + sizeof(BLOBHEADER));<br />

pBlob->bType = SIMPLEBLOB;<br />

pBlob->bVersion = 2;<br />

pBlob->reserved = 0;<br />

pBlob->aiKeyAlg = Algid;<br />

dwDataLen = sizeof(ALG_ID);<br />

if (!CryptGetKeyParam(hImpKey, KP_ALGID, (BYTE *)pAlgid, &dwDataLen, 0))<br />

goto done;<br />

bResult = CryptImportKey(hProvider, pbData, cbData + cbHeaderLen, hImpKey, 0,<br />

&hKey);<br />

if (bResult) spc_memset(pbKeyData, 0, cbKeyData);<br />

done:<br />

if (pbData) LocalFree(pbData);<br />

CryptDestroyKey(hImpKey);<br />

return (bResult ? hKey : 0);<br />

}<br />

See Also<br />

Recipes 5.25, 13.2<br />

5.27 Extracting Raw Key Data from a CryptoAPI<br />

Key Object<br />

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

You have a symmetric key stored in a CryptoAPI key object that you want to use<br />

with another API, such as OpenSSL.<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 />

246 | 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!