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.

that demonstrates the use of this flag in the book, but you can find one on<br />

the book’s companion web site.<br />

You can use the following convenience function to enable session caching on the<br />

server side. If you want to use it with the SSLserver functions presented in Recipe 9.2,<br />

you should create an SSL_CTX object using spc_create_sslctx( ) yourself. Then call<br />

spc_enable_sessions( ) using that SSL_CTX object, and pass the SSL_CTX object to spc_<br />

accept( ) so that a new one will not be created automatically for you. Whether you<br />

enable session caching or not, it’s a good idea to create your own SSL_CTX object<br />

before calling spc_accept( ) anyway, so that a fresh SSL_CTX object isn’t created for<br />

each and every client connection.<br />

#include <br />

#include <br />

void spc_enable_sessions(SSL_CTX *ctx, unsigned char *id, unsigned int id_len,<br />

long timeout, int mode) {<br />

SSL_CTX_set_session_id_context(ctx, id, id_len);<br />

SSL_CTX_set_timeout(ctx, timeout);<br />

SSL_CTX_set_session_cache_mode(ctx, mode);<br />

}<br />

Enabling session caching on the client side is even easier than it is on the server side.<br />

All that’s required is setting the SSL_SESSION object in the SSL_CTX object before actually<br />

establishing the connection. The following function, spc_reconnect( ), is a reimplementation<br />

of spc_connect_ssl( ) with the necessary changes to enable clientside<br />

session caching.<br />

BIO *spc_reconnect(char *host, int port, SSL_SESSION *session,<br />

spc_x509store_t *spc_store, SSL_CTX **ctx) {<br />

BIO *conn = 0;<br />

int our_ctx = 0;<br />

SSL *ssl_ptr;<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 />

SSL_CTX_set_app_data(*ctx, spc_store);<br />

}<br />

} else {<br />

*ctx = spc_create_sslctx(spc_store);<br />

our_ctx = 1;<br />

}<br />

if (!(conn = BIO_new_ssl_connect(*ctx))) goto error_exit;<br />

BIO_set_conn_hostname(conn, host);<br />

BIO_set_conn_int_port(conn, &port);<br />

if (session) {<br />

BIO_get_ssl(conn, &ssl_ptr);<br />

SSL_set_session(ssl_ptr, session);<br />

}<br />

462 | Chapter 9: Networking<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!