21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

X509_LOOKUP *lookup;<br />

store = X509_STORE_new( );<br />

if (spc_store->callback)<br />

X509_STORE_set_verify_cb_func(store, spc_store->callback);<br />

else<br />

X509_STORE_set_verify_cb_func(store, spc_verify_callback);<br />

if (!(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file( ))))<br />

goto error_exit;<br />

if (!spc_store->cafile) {<br />

if (!(spc_store->flags & SPC_X509STORE_NO_DEFAULT_CAFILE))<br />

X509_LOOKUP_load_file(lookup, 0, X509_FILETYPE_DEFAULT);<br />

} else if (!X509_LOOKUP_load_file(lookup, spc_store->cafile, X509_FILETYPE_PEM))<br />

goto error_exit;<br />

if (spc_store->crlfile) {<br />

if (!X509_load_crl_file(lookup, spc_store->crlfile, X509_FILETYPE_PEM))<br />

goto error_exit;<br />

X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK |<br />

X509_V_FLAG_CRL_CHECK_ALL);<br />

}<br />

if (!(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir( ))))<br />

goto error_exit;<br />

if (!spc_store->capath) {<br />

if (!(spc_store->flags & SPC_X509STORE_NO_DEFAULT_CAPATH))<br />

X509_LOOKUP_add_dir(lookup, 0, X509_FILETYPE_DEFAULT);<br />

} else if (!X509_LOOKUP_add_dir(lookup, spc_store->capath, X509_FILETYPE_PEM))<br />

goto error_exit;<br />

for (i = 0; i < sk_X509_num(spc_store->certs); i++)<br />

if (!X509_STORE_add_cert(store, sk_X509_value(spc_store->certs, i)))<br />

goto error_exit;<br />

for (i = 0; i < sk_X509_CRL_num(spc_store->crls); i++)<br />

if (!X509_STORE_add_crl(store, sk_X509_CRL_value(spc_store->crls, i)))<br />

goto error_exit;<br />

return store;<br />

error_exit:<br />

if (store) X509_STORE_free(store);<br />

return 0;<br />

}<br />

We can now use the functions to manipulate spc_x509store_t objects in verifying an<br />

X.509 certificate’s validity. The function spc_verify_cert( ) requires an X509 object<br />

and spc_x509store_t object. It creates an X509_STORE object from the information in<br />

the spc_x509store_t object, and combines it with the X509 object to create an X509_<br />

STORE_CTX object as required by X509_verify_cert( ). The return value from spc_<br />

verify_cert( ) will be –1 if some kind of error occurred that was not related to the<br />

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