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.

HCERTSTORE hCertStore;<br />

PCCERT_CONTEXT pIssuerContext<br />

if (!(hCertStore = CertOpenSystemStore(0, pszStoreName))) return 0;<br />

pIssuerContext = CertFindCertificateInStore(hCertStore, X509_ASN_ENCODING, 0,<br />

CERT_FIND_ISSUER_OF, pSubjectContext, 0);<br />

CertCloseStore(hCertStore, 0);<br />

return pIssuerContext;<br />

}<br />

static LPCTSTR SpcSystemStoreList[ ] = {<br />

TEXT("MY"), TEXT("CA"), TEXT("ROOT"), TEXT("SPC"), 0<br />

};<br />

HCERTSTORE SpcNewStoreForCert(PCCERT_CONTEXT pSubjectContext) {<br />

LPCTSTR pszStoreName;<br />

HCERTSTORE hCertStore;<br />

PCCERT_CONTEXT pIssuerContext;<br />

/* First create an in-memory store, and add the subject certificate to it */<br />

if (!(hCertStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0, 0, 0))) return 0;<br />

if (!CertAddCertificateContextToStore(hCertStore, pSubjectContext,<br />

CERT_STORE_ADD_REPLACE_EXISTING, 0)) {<br />

CertCloseStore(hCertStore, 0);<br />

return 0;<br />

}<br />

pSubjectContext = CertDuplicateCertificateContext(pSubjectContext);<br />

while (!CertCompareCertificateName(X509_ASN_ENCODING,<br />

pSubjectContext->pCertInfo->Issuer, pSubjectContext->pCertInfo->Subject)){<br />

for (pszStoreName = SpcSystemStoreList; pszStoreName; pszStoreName++) {<br />

pIssuerContext = FindIssuerInSystemStore(pszStoreName, pSubjectContext);<br />

if (pIssuerContext) {<br />

if (!CertAddCertificateContextToStore(hCertStore, pIssuerContext,<br />

CERT_STORE_ADD_REPLACE_EXISTING, 0)) {<br />

CertFreeCertificateContext(pSubjectContext);<br />

CertFreeCertificateContext(pIssuerContext);<br />

CertCloseStore(hCertStore, 0);<br />

return 0;<br />

}<br />

CertFreeCertificateContext(pSubjectContext);<br />

pSubjectContext = pIssuerContext;<br />

break;<br />

}<br />

}<br />

if (!pszStoreName) {<br />

CertFreeCertificateContext(pSubjectContext);<br />

CertCloseStore(hCertStore, 0);<br />

return 0;<br />

}<br />

}<br />

CertFreeCertificateContext(pSubjectContext);<br />

return hCertStore;<br />

}<br />

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