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.

The SpcNewStoreForCert( ) function creates a temporary in-memory certificate store<br />

that can be used with SpcVerifyCert( ). Only a single argument is required: the subject<br />

certificate that is, presumably, at the end of a certificate hierarchy. The subject<br />

certificate is added to the new certificate store, and for each issuing certificate in the<br />

hierarchy, the system stores are searched for a copy of the certificate. If one cannot<br />

be found, the new certificate store is destroyed and SpcNewStoreForCert( ) returns<br />

NULL; otherwise, the found certificate will be added to the new certificate store.<br />

Once the store has been created, it can now be passed directly into the<br />

SpcVerifyCert( ) function, along with the subject certificate to be verified. If there are<br />

CRLs for any of the certificates in the hierarchy, add them to the store before calling<br />

SpcVerifyCert( ) (see Recipe 10.11 for obtaining CRLs with CryptoAPI). You can<br />

enumerate the contents of the certificate store created by SpcNewStoreForCert( )<br />

using CertEnumCertificatesInStore( ):<br />

BOOL bResult;<br />

HCERTSTORE hCertStore;<br />

PCCRL_CONTEXT pCRLContext;<br />

PCCERT_CONTEXT pCertContext = 0;<br />

if (!(hCertStore = SpcNewStoreForCert(pSubjectContext))) {<br />

/* handle an error condition--could not create the store */<br />

abort( );<br />

}<br />

while ((pCertContext = CertEnumCertificatesInStore(hCertStore, pCertContext))) {<br />

/* do something with the certificate retrieved from the store.<br />

* if an error occurs, and enumeration must be terminated prematurely, the last<br />

* certificate retrieved must be freed manually.<br />

*<br />

* For example, attempt to retrieve the CRL for the certificate using the code<br />

* the can be found in Recipe 10.11. If no CRL can be retrieved, or the CRL<br />

* cannot be added to the certificate store, consider it a failure and break<br />

* out of the enumeration.<br />

*/<br />

if (!(pCRLContext = SpcRetrieveCRL(pCertContext, 0)) ||<br />

!CertAddCRLContextToStore(hCertStore, pCRLContext,<br />

CERT_ADD_USE_EXISTING, 0)) {<br />

if (pCRLContext) CertFreeCRLContext(pCRLContext);<br />

break;<br />

}<br />

CertFreeCRLContext(pCRLContext);<br />

}<br />

if (pCertContext) {<br />

CertFreeCertificateContext(pCertContext);<br />

CertCloseStore(hCertStore, 0);<br />

abort( );<br />

}<br />

bResult = SpcVerifyCert(hCertStore, pSubjectContext);<br />

CertCloseStore(hCertStore, 0);<br />

return bResult;<br />

534 | Chapter 10: Public Key Infrastructure<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!