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.

validity of the certificate. If the certificate is valid, the return value will be 1; otherwise,<br />

the return value will be 0.<br />

#include <br />

int spc_verify_cert(X509 *cert, spc_x509store_t *spc_store) {<br />

int result = -1;<br />

X509_STORE *store = 0;<br />

X509_STORE_CTX *ctx = 0;<br />

if (!(store = spc_create_x509store(spc_store))) return -1;<br />

if ((ctx = X509_STORE_CTX_new( )) != 0) {<br />

if (X509_STORE_CTX_init(ctx, store, cert, 0) = = 1)<br />

result = (X509_verify_cert(ctx) = = 1);<br />

X509_STORE_CTX_free(ctx);<br />

}<br />

X509_STORE_free(store);<br />

return result;<br />

}<br />

See Also<br />

Recipes 7.17, 10.7<br />

10.6 Performing X.509 Certificate Verification<br />

with CryptoAPI<br />

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

You have an X.509 certificate, and you want to verify its validity using Microsoft’s<br />

CryptoAPI on Windows.<br />

Solution<br />

CryptoAPI represents an X.509 certificate using a CERT_CONTEXT object. Another<br />

object, referenced by a HCERTSTORE handle, must be created to hold the certificates<br />

that will be required for verification, as well as any certificate revocation lists (CRLs)<br />

that may be necessary. The actual certificate verification is performed by calling the<br />

CertGetIssuerCertificateFromStore( ) function for each certificate in the hierarchy.<br />

This function will verify the signature, certificate validity times, and revocation status<br />

of each certificate as it obtains the issuer for each call. The last certificate in the<br />

hierarchy will have no issuing certificate and should be self-signed.<br />

Discussion<br />

Call the CertGetIssuerCertificateFromStore( ) function for each certificate in the<br />

hierarchy, beginning with the subject certificate at the end of the chain. Each time<br />

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