Bond's TCP Library  1.0
Bond's TCP Client/Server Library
tcp::SSLContext Class Reference

Encapsulates an openSSL SSL_CTX record. More...

#include <tcpssl.h>

Public Member Functions

 SSLContext (SSLMode mode)
 Constructor. More...
 
virtual ~SSLContext ()
 Destructor.
 
void setOptions (bool verifypeer=false, bool compression=true, bool tlsonly=false)
 Sets default options for all SSL objects created from this context. More...
 
bool useDefaultVerifyPaths ()
 Use the default operating system certificate store for validation. More...
 
bool setVerifyPaths (const char *cafile=NULL, const char *capath=NULL)
 Use the specified CA certificate or certificate directory. More...
 
bool setVerifyPaths (string &cafile, string &capath)
 
bool setCertificateAndKey (const char *certfile, const char *keyfile)
 Sets the certificate filename and key filename. More...
 
void setPrivateKeyPassword (string value)
 Sets the value of the private key password.
 
virtual int passwordCallback (char *buf, int size, int rwflag)
 Called when a password to decrypt a private key is required. More...
 

Protected Attributes

SSL_CTX * ctx_
 The openSSL context object.
 
SSLMode mode_
 The mode of the context object is passed to SSL objects created from this context.
 

Friends

class SSL
 

Detailed Description

Encapsulates an openSSL SSL_CTX record.

Definition at line 41 of file tcpssl.h.

Constructor & Destructor Documentation

◆ SSLContext()

tcp::SSLContext::SSLContext ( SSLMode  mode)

Constructor.

Parameters
mode[in] Is this context record for client connections or a server

Definition at line 242 of file tcpssl.cpp.

242  : mode_(mode)
243 {
244  initSSLLibrary();
245  const SSL_METHOD *meth = (mode == SSLMode::SERVER) ? TLS_server_method() : TLS_client_method();
246  unsigned long ssl_err = ERR_get_error();
247  if (meth == NULL) {
248  print_error_string(ssl_err, "TLS_method");
249  return;
250  }
251  ctx_ = SSL_CTX_new(meth);
252  ssl_err = ERR_get_error();
253  if (ctx_ == NULL) {
254  print_error_string(ssl_err, "SSL_CTX_new");
255  return;
256  }
257 }
Here is the call graph for this function:

Member Function Documentation

◆ passwordCallback()

int tcp::SSLContext::passwordCallback ( char *  buf,
int  size,
int  rwflag 
)
virtual

Called when a password to decrypt a private key is required.

Descendant classes may want to override this to provide a more secure means of storing the private key password. The default behavior is to return the value of keypass_.

Remarks
See the openSSL function SSL_CTX_set_default_passwd_cb for more details
This must be public because it is called from the passwordCallback() function
This password callback function only applies to private keys assigned to this SSL_CTX object, it is not 'inherited' from any SSL objects created from it.

Definition at line 382 of file tcpssl.cpp.

383 {
384  (void)rwflag;
385  if (!keypass_.empty()) {
386  int lsize = max<int>(size,keypass_.length());
387  strncpy(buf,keypass_.c_str(),lsize);
388  return lsize;
389  } else {
390  return 0;
391  }
392 }

◆ setCertificateAndKey()

bool tcp::SSLContext::setCertificateAndKey ( const char *  certfile,
const char *  keyfile 
)

Sets the certificate filename and key filename.

Sets the certificate and key file for all SSL connections created from this context. This is primarily intended for servers but clients may want to connect to more than one server using the same certificate and key. This default certificate and key set can be overriden for a specific connection using the equivelant method from the SSL class.

Parameters
certfile[in] The filename of a certificate chain in PEM (or CRT) format
keyfile[in] The filename of a private key in PEM (or CRT) format
Returns
True of the certificate and key were successfully set, false otherwise. Check cerr log for details.
Remarks
If the keyfile requires a password to decrypt, passwordCallback will be called to provide that password.

Definition at line 340 of file tcpssl.cpp.

341 {
342  long res = 1;
343  unsigned long ssl_err = 0;
344 
345  if ((certfile && !keyfile) || (!certfile && keyfile)) {
346  cerr << "Error: Both a certificate and a private key file are required" << endl;
347  return false;
348  }
349 
350  if (certfile && keyfile) {
351  res = SSL_CTX_use_certificate_file(ctx_, certfile, SSL_FILETYPE_PEM);
352  ssl_err = ERR_get_error();
353  if ( res != 1) {
354  print_error_string(ssl_err,"SSL_CTX_use_certificate_file");
355  return false;
356  }
357 
358  res = SSL_CTX_use_PrivateKey_file(ctx_, keyfile, SSL_FILETYPE_PEM);
359  ssl_err = ERR_get_error();
360  if (res != 1) {
361  print_error_string(ssl_err,"SSL_CTX_use_PrivateKey_file");
362  return false;
363  }
364 
365  /* Make sure the key and certificate file match. */
366  res = SSL_CTX_check_private_key(ctx_);
367  ssl_err = ERR_get_error();
368  if ( res != 1) {
369  print_error_string(ssl_err,"SSL_CTX_check_private_key");
370  return false;
371  }
372 
373  SSL_CTX_set_default_passwd_cb(ctx_,&ctx_password_callback);
374  SSL_CTX_set_default_passwd_cb_userdata(ctx_,this);
375 
376  return true;
377  } else {
378  return false;
379  }
380 }

◆ setOptions()

void tcp::SSLContext::setOptions ( bool  verifypeer = false,
bool  compression = true,
bool  tlsonly = false 
)

Sets default options for all SSL objects created from this context.

Parameters
verifypeer[in] If true, the server/client will validate the peer certificate
compression[in] Set to false to disable SSL compression
tlsonly[in] if true, disable the SSLv2 and SSLv3 protocols. This is recommended.
Remarks
Logs error information to cerr but does not return error information.

Definition at line 264 of file tcpssl.cpp.

265 {
266  long flags = 0;
267 
268  if (verifypeer) {
269  if (mode_ == SSLMode::SERVER) {
270  SSL_CTX_set_verify(ctx_, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT | SSL_VERIFY_CLIENT_ONCE, verify_callback);
271  } else {
272  SSL_CTX_set_verify(ctx_, SSL_VERIFY_PEER, verify_callback);
273  }
274  } else {
275  SSL_CTX_set_verify(ctx_, SSL_VERIFY_NONE, NULL);
276  }
277  SSL_CTX_set_verify_depth(ctx_, 4);
278 
279  if (tlsonly) {
280  flags = SSL_OP_ALL | SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3;
281  SSL_CTX_set_min_proto_version(ctx_,TLS1_VERSION);
282  } else {
283  flags = SSL_OP_ALL;
284  }
285 
286  if (!compression) {
287  flags |= SSL_OP_NO_COMPRESSION;
288  }
289 
290  SSL_CTX_set_options(ctx_, flags);
291 
292  printSSLErrors();
293 }

◆ setVerifyPaths()

bool tcp::SSLContext::setVerifyPaths ( const char *  cafile = NULL,
const char *  capath = NULL 
)

Use the specified CA certificate or certificate directory.

Provide one or the other or set both to NULL to use the operating system certificate store

Parameters
cafile[in] The filename of a CA certificate chain in PEM (or CRT) format
capath[in] The path of a directory containing CA certificates that are considered valid
Returns
True if the CA filename or CA path were set successfully, false otherwise. Check cerr log for details.

Definition at line 315 of file tcpssl.cpp.

316 {
317  long res = 1;
318  unsigned long ssl_err = 0;
319 
320  if (cafile || capath) {
321  res = SSL_CTX_load_verify_locations(ctx_, cafile, capath);
322  ssl_err = ERR_get_error();
323  if (res == 0) {
324  print_error_string(ssl_err,"SSL_CTX_load_verify_locations");
325  return false;
326  }
327  } else {
328  res = SSL_CTX_set_default_verify_paths(ctx_);
329  ssl_err = ERR_get_error();
330  if (res == 0) {
331  print_error_string(ssl_err,"SSL_CTX_set_default_verify_paths");
332  return false;
333  }
334  }
335 
336  printSSLErrors();
337  return true;
338 }
Here is the call graph for this function:

◆ useDefaultVerifyPaths()

bool tcp::SSLContext::useDefaultVerifyPaths ( )
inline

Use the default operating system certificate store for validation.

Returns
True if the CA filename or CA path were set successfully, false otherwise. Check cerr log for details.

Definition at line 62 of file tcpssl.h.

62 { return setVerifyPaths(NULL,NULL); }

The documentation for this class was generated from the following files:
tcp::printSSLErrors
void printSSLErrors()
This method logs openSSL errors to cerr.
Definition: tcpssl.cpp:56
tcp::SSLContext::mode_
SSLMode mode_
The mode of the context object is passed to SSL objects created from this context.
Definition: tcpssl.h:102
tcp::SSLContext::setVerifyPaths
bool setVerifyPaths(const char *cafile=NULL, const char *capath=NULL)
Use the specified CA certificate or certificate directory.
Definition: tcpssl.cpp:315
tcp::verify_callback
int verify_callback(int preverify, X509_STORE_CTX *x509_ctx)
Prints the certificate details to clog.
Definition: tcpssl.cpp:195
tcp::initSSLLibrary
void initSSLLibrary()
Initialize the openSSL library.
Definition: tcpssl.cpp:19
tcp::SSLContext::ctx_
SSL_CTX * ctx_
The openSSL context object.
Definition: tcpssl.h:101