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.

Additionally, over time, SSPI-specific recipes may end up on the book’s companion<br />

web site, particularly if submitted by readers such as you.<br />

9.1 Creating an SSL Client<br />

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

You want to establish a connection from a client to a remote server using SSL.<br />

Solution<br />

Establishing a connection to a remote server using SSLis not entirely different from<br />

establishing a connection without using SSL—at least it doesn’t have to be. Establishing<br />

an SSLconnection requires a little more setup work, consisting primarily of<br />

building an spc_x509store_t object (see Recipe 10.5) that contains the information<br />

necessary to verify the server. Once this is done, you need to create an SSL_CTX object<br />

and attach it to the connection. OpenSSL will handle the rest.<br />

Discussion<br />

Before reading this recipe, make sure you understand the basics of<br />

public key infrastructure (see Recipe 10.1).<br />

Once you’ve created an spc_x509store_t object by loading it with the appropriate<br />

certificates and CRLs (see Recipes 10.10 and 10.11 for information on obtaining<br />

CRLs), connecting to a remote server over SSL can be as simple as making a call to<br />

the following function, spc_connect_ssl( ). You can optionally create an SSL_CTX<br />

object yourself using spc_create_sslctx( ) or the OpenSSLAPI. Alternatively, you<br />

can share one that has already been created for other connections, or you can let spc_<br />

connect_ssl( ) do it for you. In the latter case, the connection will be established and<br />

the SSL_CTX object that was created will be returned by way of a pointer to the SSL_<br />

CTX object pointer in the function’s argument list.<br />

#include <br />

#include <br />

BIO *spc_connect_ssl(char *host, int port, spc_x509store_t *spc_store,<br />

SSL_CTX **ctx) {<br />

BIO *conn = 0;<br />

int our_ctx = 0;<br />

if (*ctx) {<br />

CRYPTO_add(&((*ctx)->references), 1, CRYPTO_LOCK_SSL_CTX);<br />

if (spc_store && spc_store != SSL_CTX_get_app_data(*ctx)) {<br />

SSL_CTX_set_cert_store(*ctx, spc_create_x509store(spc_store));<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.<br />

Creating an SSL Client | 455

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!