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.

CertGetIssuerCertificateFromStore( ) is called, CryptoAPI will attempt to locate the<br />

issuer of the subject certificate passed into it. If the issuer certificate is found, the signature<br />

of the subject certificate will be verified with the public key of the issuer certificate.<br />

In addition, time validity checks will be performed on the subject certificate,<br />

and the subject certificate will be compared against the issuer’s CRLif it is present in<br />

the store.<br />

#include <br />

#include <br />

BOOL SpcVerifyCert(HCERTSTORE hCertStore, PCCERT_CONTEXT pSubjectContext) {<br />

DWORD dwFlags;<br />

PCCERT_CONTEXT pIssuerContext;<br />

if (!(pSubjectContext = CertDuplicateCertificateContext(pSubjectContext)))<br />

return FALSE;<br />

do {<br />

dwFlags = CERT_STORE_REVOCATION_FLAG | CERT_STORE_SIGNATURE_FLAG |<br />

CERT_STORE_TIME_VALIDITY_FLAG;<br />

pIssuerContext = CertGetIssuerCertificateFromStore(hCertStore,<br />

pSubjectContext, 0, &dwFlags);<br />

CertFreeCertificateContext(pSubjectContext);<br />

if (pIssuerContext) {<br />

pSubjectContext = pIssuerContext;<br />

if (dwFlags & CERT_STORE_NO_CRL_FLAG)<br />

dwFlags &= ~(CERT_STORE_NO_CRL_FLAG | CERT_STORE_REVOCATION_FLAG);<br />

if (dwFlags) break;<br />

} else if (GetLastError( ) = = CRYPT_E_SELF_SIGNED) return TRUE;<br />

} while (pIssuerContext);<br />

return FALSE;<br />

}<br />

Every certificate returned by CertGetIssuerCertificateFromStore( ) must be freed<br />

with a call to CertFreeCertificateContext( ). To make things a bit simpler, a copy of<br />

the original subject certificate is made so that the subject certificate can always be<br />

freed after the call to CertGetIssuerCertificateFromStore( ). If an issuer certificate is<br />

returned, the subject becomes the issuer for the next iteration through the loop.<br />

When CertGetIssuerCertificateFromStore( ) cannot find the issuing certificate for<br />

the subject certificate in the store, it returns NULL. This could mean that the end of<br />

the certificate hierarchy has been reached, in which case GetLastError( ) will return<br />

CRYPT_E_SELF_SIGNED because the root certificate in any hierarchy must always be<br />

self-signed. A NULL return from CertGetIssuerCertificateFromStore( ) might also<br />

indicate that there may be an issuer certificate for the subject certificate, but that one<br />

wasn’t present in the certificate store; this is an error condition that results in the verification<br />

failure of the subject certificate.<br />

The call to CertGetIssuerCertificateFromStore( ) requires a set of flags to be passed<br />

into it that determines what verification checks are to be performed on the subject<br />

certificate. Upon return from the call, this set of flags is modified, leaving the bits set<br />

Performing X.509 Certificate Verification with CryptoAPI | 531<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!