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.

take precedence over a preexisting OpenSSLobject. If a preexisting key object is<br />

used, it is the caller’s responsibility to free it using EVP_PKEY_free( ) at any point after<br />

it is added into the spc_x509store_t object because it is reference counted, and spc_<br />

x509store_setusekey( ) increments its reference count.<br />

When specifying the certificates to be sent to a peer (whether the peer will be a server<br />

or a client), multiple certificates may be specified. The first certificate specified<br />

should always be the certificate belonging to your program. Any additional certificates<br />

should be certificates in the chain that may be needed to verify the validity of<br />

your own certificate. This is true whether the certificates are loaded from a file and<br />

specified via spc_x509store_setusecertfile( ), or are added to the spc_x509store_t<br />

one at a time using spc_x509store_addusecert( ). Note also that the certificates and<br />

the required private key may be contained within the same file. For both certificate<br />

and key files, PEM format should be used, because the alternative binary ASN.1 format<br />

(also known as DER) does not allow multiple objects to be present in the same<br />

file.<br />

At this point, spc_create_sslctx( ) has everything it needs. It takes a single argument—the<br />

spc_x509store_t object—to get its information from, and it returns a new<br />

SSL_CTX object that can be used to establish SSL-enabled connections.<br />

#include <br />

#define SPC_X509STORE_USE_CERTIFICATE 0x04<br />

#define SPC_X509STORE_SSL_VERIFY_NONE 0x10<br />

#define SPC_X509STORE_SSL_VERIFY_PEER 0x20<br />

#define SPC_X509STORE_SSL_VERIFY_FAIL_IF_NO_PEER_CERT 0x40<br />

#define SPC_X509STORE_SSL_VERIFY_CLIENT_ONCE 0x80<br />

#define SPC_X509STORE_SSL_VERIFY_MASK 0xF0<br />

SSL_CTX *spc_create_sslctx(spc_x509store_t *spc_store) {<br />

int i, verify_flags = 0;<br />

SSL_CTX *ctx = 0;<br />

X509_STORE *store = 0;<br />

spc_x509verifycallback_t verify_callback;<br />

if (!(ctx = SSL_CTX_new(SSLv23_method( )))) goto error_exit;<br />

if (!(store = spc_create_x509store(spc_store))) goto error_exit;<br />

SSL_CTX_set_cert_store(ctx, store); store = 0;<br />

SSL_CTX_set_options(ctx, SSL_OP_ALL | SSL_OP_NO_SSLv2);<br />

SSL_CTX_set_cipher_list(ctx, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");<br />

if (!(verify_callback = spc_store->callback))<br />

verify_callback = spc_verify_callback;<br />

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

verify_flags = SSL_VERIFY_NONE;<br />

else {<br />

if (spc_store->flags & SPC_X509STORE_SSL_VERIFY_NONE)<br />

verify_flags |= SSL_VERIFY_NONE;<br />

if (spc_store->flags & SPC_X509STORE_SSL_VERIFY_PEER)<br />

verify_flags |= SSL_VERIFY_PEER;<br />

if (spc_store->flags & SPC_X509STORE_SSL_VERIFY_FAIL_IF_NO_PEER_CERT)<br />

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