DTS Application Library  0.2.3
Application library containing referenced objects and interfaces to common libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
SSL socket support

TLSv1 SSLv2 SSLv3 DTLSv1 support. More...

Files

file  sslutil.c
 TLSv1 SSLv2 SSLv3 DTLSv1 support.
 

Data Structures

struct  ssldata
 SSL data structure for enabling encryption on sockets. More...
 

Macros

#define COOKIE_SECRET_LENGTH   32
 length of cookie secret using SHA2-256 HMAC More...
 

Typedefs

typedef struct ssldata ssldata
 Forward decleration of structure. More...
 

Enumerations

enum  SSLFLAGS {
  SSL_TLSV1 = 1 << 0, SSL_SSLV2 = 1 << 1, SSL_SSLV3 = 1 << 2, SSL_DTLSV1 = 1 << 3,
  SSL_CLIENT = 1 << 4, SSL_SERVER = 1 << 5, SSL_DTLSCON = 1 << 6
}
 SSL configuration flags. More...
 

Functions

void ssl_shutdown (void *data, int sock)
 Shutdown the SSL connection. More...
 
void * tlsv1_init (const char *cacert, const char *cert, const char *key, int verify)
 Create a SSL structure for TLSv1. More...
 
void * sslv2_init (const char *cacert, const char *cert, const char *key, int verify)
 Create a SSL structure for SSLv2 (If available) More...
 
void * sslv3_init (const char *cacert, const char *cert, const char *key, int verify)
 Create a SSL structure for SSLv3. More...
 
void * dtlsv1_init (const char *cacert, const char *cert, const char *key, int verify)
 Create a SSL structure for DTLSv1. More...
 
void tlsaccept (struct fwsocket *sock, struct ssldata *orig)
 Create SSL session for new connection. More...
 
void sslstartup (void)
 Initialise SSL support this should be called at startup. More...
 
void dtsl_serveropts (struct fwsocket *sock)
 Start up the DTLSv1 Server. More...
 
struct fwsocketdtls_listenssl (struct fwsocket *sock)
 Implementation of "listen" for DTLSv1. More...
 
void startsslclient (struct fwsocket *sock)
 Start SSL on a client socket. More...
 
void dtlstimeout (struct fwsocket *sock, struct timeval *timeleft, int defusec)
 Get DTLSv1 timeout setting todefault timeout. More...
 
void dtlshandltimeout (struct fwsocket *sock)
 Handle DTLSv1 timeout. More...
 

Detailed Description

TLSv1 SSLv2 SSLv3 DTLSv1 support.

See Also
LIB-Sock This is part of the socket interface to support encrypted sockets a ssldata refernece will be created and passed on socket initialization.

This is part of the socket interface to upport encrypted sockets a ssldata refernece will be created and passed on socket initialization.

See Also
Network socket interface

Macro Definition Documentation

#define COOKIE_SECRET_LENGTH   32

length of cookie secret using SHA2-256 HMAC

Definition at line 83 of file sslutil.c.

Referenced by sslstartup().

Typedef Documentation

typedef struct ssldata ssldata

Forward decleration of structure.

Definition at line 97 of file dtsapp.h.

Enumeration Type Documentation

enum SSLFLAGS

SSL configuration flags.

Enumerator
SSL_TLSV1 

TLSv1.

SSL_SSLV2 

SSLv2 This may not be available due to security issues.

SSL_SSLV3 

SSLv3.

SSL_DTLSV1 

DTLSv1 (UDP Connections)

SSL_CLIENT 

This session is client mode.

SSL_SERVER 

This session is server mode.

SSL_DTLSCON 

UDP connection is listening.

Definition at line 48 of file sslutil.c.

48  {
50  SSL_TLSV1 = 1 << 0,
52  SSL_SSLV2 = 1 << 1,
54  SSL_SSLV3 = 1 << 2,
56  SSL_DTLSV1 = 1 << 3,
58  SSL_CLIENT = 1 << 4,
60  SSL_SERVER = 1 << 5,
62  SSL_DTLSCON = 1 << 6
63 };
DTLSv1 (UDP Connections)
Definition: sslutil.c:56
TLSv1.
Definition: sslutil.c:50
This session is server mode.
Definition: sslutil.c:60
UDP connection is listening.
Definition: sslutil.c:62
SSLv3.
Definition: sslutil.c:54
This session is client mode.
Definition: sslutil.c:58
SSLv2 This may not be available due to security issues.
Definition: sslutil.c:52

Function Documentation

struct fwsocket* dtls_listenssl ( struct fwsocket sock)

Implementation of "listen" for DTLSv1.

Warning
Do not call this directly.
Parameters
sockReference to server socket.
Returns
New socket reference for the new connection.

Definition at line 731 of file sslutil.c.

References fwsocket::addr, ssldata::flags, make_socket(), objalloc(), objlock(), objunlock(), objunref(), fwsocket::proto, sockstruct::sa, setflag, fwsocket::sock, SOCK_FLAG_SSL, ssldata::ssl, fwsocket::ssl, SSL_DTLSCON, and fwsocket::type.

731  {
732  struct ssldata *ssl = sock->ssl;
733  struct ssldata *newssl;
734  struct fwsocket *newsock;
735  union sockstruct client;
736 #ifndef __WIN32__
737  int on = 1;
738 #else
739 /* unsigned long on = 1;*/
740 #endif
741 
742  if (!(newssl = objalloc(sizeof(*newssl), free_ssldata))) {
743  return NULL;
744  }
745 
746  newssl->flags |= SSL_DTLSCON;
747 
748  dtlssetopts(newssl, ssl, sock);
749  memset(&client, 0, sizeof(client));
750  if (DTLSv1_listen(newssl->ssl, &client) <= 0) {
751  objunref(newssl);
752  return NULL;
753  }
754 
755  objlock(sock);
756  if (!(newsock = make_socket(sock->addr.sa.sa_family, sock->type, sock->proto, newssl))) {
757  objunlock(sock);
758  objunref(newssl);
759  return NULL;
760  }
761  objunlock(sock);
762  memcpy(&newsock->addr, &client, sizeof(newsock->addr));
763 #ifndef __WIN32__
764  setsockopt(newsock->sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
765 #ifdef SO_REUSEPORT
766  setsockopt(newsock->sock, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on));
767 #endif
768 #else
769 /* ioctlsocket(newsock->sock, FIONBIO, (unsigned long*)&on);*/
770 #endif
771  objlock(sock);
772  bind(newsock->sock, &sock->addr.sa, sizeof(sock->addr));
773  objunlock(sock);
774  connect(newsock->sock, &newsock->addr.sa, sizeof(newsock->addr));
775 
776  dtlsaccept(newsock);
777  setflag(newsock, SOCK_FLAG_SSL);
778 
779  return (newsock);
780 }
SSL data structure for enabling encryption on sockets.
Definition: sslutil.c:66
union sockstruct addr
system socket data structure.
Definition: dtsapp.h:143
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
Socket data structure.
Definition: dtsapp.h:131
int flags
SSL flags.
Definition: sslutil.c:75
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
#define setflag(obj, flag)
Atomically set a flag in the flags field of a referenced object.
Definition: dtsapp.h:925
int sock
Socket FD.
Definition: dtsapp.h:133
SSL has been requested on this socket dont allow clear read/send.
Definition: dtsapp.h:108
UDP connection is listening.
Definition: sslutil.c:62
int proto
Socket protocol.
Definition: dtsapp.h:135
struct ssldata * ssl
SSL structure for encryption.
Definition: dtsapp.h:146
struct fwsocket * make_socket(int family, int type, int proto, void *ssl)
Allocate a socket structure and return reference.
Definition: socket.c:120
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
struct sockaddr sa
Base socket addr structure.
Definition: dtsapp.h:82
int type
Socket type.
Definition: dtsapp.h:137
SSL * ssl
OpenSSL ssl.
Definition: sslutil.c:70
Socket union describing all address types.
Definition: dtsapp.h:80
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
void dtlshandltimeout ( struct fwsocket sock)

Handle DTLSv1 timeout.

Parameters
sockReference to socket.

Definition at line 846 of file sslutil.c.

References objlock(), objunlock(), ssldata::ssl, and fwsocket::ssl.

846  {
847  if (!sock->ssl) {
848  return;
849  }
850 
851  objlock(sock->ssl);
852  DTLSv1_handle_timeout(sock->ssl->ssl);
853  objunlock(sock->ssl);
854 }
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
struct ssldata * ssl
SSL structure for encryption.
Definition: dtsapp.h:146
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
SSL * ssl
OpenSSL ssl.
Definition: sslutil.c:70
void dtlstimeout ( struct fwsocket sock,
struct timeval *  timeleft,
int  defusec 
)

Get DTLSv1 timeout setting todefault timeout.

Warning
Do not call this directly.
Parameters
sockReference to socket.
timelefttimeval to store timeleft or set to default.
defusecDefault timeout to set.

Definition at line 831 of file sslutil.c.

References objlock(), objunlock(), ssldata::ssl, and fwsocket::ssl.

831  {
832  if (!sock || !sock->ssl || !sock->ssl->ssl) {
833  return;
834  }
835 
836  objlock(sock->ssl);
837  if (!DTLSv1_get_timeout(sock->ssl->ssl, timeleft)) {
838  timeleft->tv_sec = 0;
839  timeleft->tv_usec = defusec;
840  }
841  objunlock(sock->ssl);
842 }
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
struct ssldata * ssl
SSL structure for encryption.
Definition: dtsapp.h:146
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
SSL * ssl
OpenSSL ssl.
Definition: sslutil.c:70
void* dtlsv1_init ( const char *  cacert,
const char *  cert,
const char *  key,
int  verify 
)

Create a SSL structure for DTLSv1.

Parameters
cacertPath to the CA certificate[s].
certPublic certificate to use.
keyPrivate key file.
verifyOpenSSL flags.

Definition at line 325 of file sslutil.c.

References ssldata::ctx, ssldata::ssl, and SSL_DTLSV1.

Referenced by socktest().

325  {
326  const SSL_METHOD *meth = DTLSv1_method();
327  struct ssldata *ssl;
328 
329  ssl = sslinit(cacert, cert, key, verify, meth, SSL_DTLSV1);
330  /* XXX BIO_CTRL_DGRAM_MTU_DISCOVER*/
331  SSL_CTX_set_read_ahead(ssl->ctx, 1);
332 
333  return (ssl);
334 }
SSL data structure for enabling encryption on sockets.
Definition: sslutil.c:66
DTLSv1 (UDP Connections)
Definition: sslutil.c:56
SSL_CTX * ctx
OpenSSL context.
Definition: sslutil.c:68
SSL * ssl
OpenSSL ssl.
Definition: sslutil.c:70
void dtsl_serveropts ( struct fwsocket sock)

Start up the DTLSv1 Server.

Warning
This should not be called directly
See Also
socketserver
Parameters
sockReference to socket structure of DTLSv1 Server

Definition at line 685 of file sslutil.c.

References ssldata::ctx, ssldata::flags, objlock(), objunlock(), ssldata::ssl, fwsocket::ssl, and SSL_SERVER.

Referenced by socketserver().

685  {
686  struct ssldata *ssl = sock->ssl;
687 
688  if (!ssl) {
689  return;
690  }
691 
692  dtlssetopts(ssl, NULL, sock);
693 
694  objlock(ssl);
695  SSL_CTX_set_cookie_generate_cb(ssl->ctx, generate_cookie);
696  SSL_CTX_set_cookie_verify_cb(ssl->ctx, verify_cookie);
697  SSL_CTX_set_session_cache_mode(ssl->ctx, SSL_SESS_CACHE_OFF);
698 
699  SSL_set_options(ssl->ssl, SSL_OP_COOKIE_EXCHANGE);
700  ssl->flags |= SSL_SERVER;
701  objunlock(ssl);
702 }
SSL data structure for enabling encryption on sockets.
Definition: sslutil.c:66
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
int flags
SSL flags.
Definition: sslutil.c:75
This session is server mode.
Definition: sslutil.c:60
struct ssldata * ssl
SSL structure for encryption.
Definition: dtsapp.h:146
SSL_CTX * ctx
OpenSSL context.
Definition: sslutil.c:68
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
SSL * ssl
OpenSSL ssl.
Definition: sslutil.c:70
void ssl_shutdown ( void *  data,
int  sock 
)

Shutdown the SSL connection.

Extra read/write may be required if so use select on failure the port has probably gone only try 3 times.

Todo:
Make sure this is only called when the thread has stoped selecting here may be wrong.
Parameters
dataRefernece to the SSL data of socket.
sockSocket FD to wait for data on.

Definition at line 179 of file sslutil.c.

References objlock(), objunlock(), and ssldata::ssl.

179  {
180  struct ssldata *ssl = data;
181  int ret, selfd, cnt = 0;
182 
183  if (!ssl) {
184  return;
185  }
186 
187  objlock(ssl);
188 
189  while (ssl->ssl && (ret = _ssl_shutdown(ssl) && (cnt < 3))) {
190  selfd = socket_select(sock, ret);
191  if (selfd <= 0) {
192  break;
193  }
194  cnt++;
195  }
196 
197  if (ssl->ssl) {
198  SSL_free(ssl->ssl);
199  ssl->ssl = NULL;
200  }
201  objunlock(ssl);
202 }
SSL data structure for enabling encryption on sockets.
Definition: sslutil.c:66
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
SSL * ssl
OpenSSL ssl.
Definition: sslutil.c:70
void sslstartup ( void  )

Initialise SSL support this should be called at startup.

See Also
FRAMEWORK_MAIN

Definition at line 639 of file sslutil.c.

References COOKIE_SECRET_LENGTH, and genrand().

Referenced by framework_init().

639  {
640  SSL_library_init();
641  SSL_load_error_strings();
642  OpenSSL_add_ssl_algorithms();
643 
644  if ((cookie_secret = malloc(COOKIE_SECRET_LENGTH))) {
645  genrand(cookie_secret, COOKIE_SECRET_LENGTH);
646  }
647 }
#define COOKIE_SECRET_LENGTH
length of cookie secret using SHA2-256 HMAC
Definition: sslutil.c:83
int genrand(void *buf, int len)
Generate random sequence.
Definition: util.c:82
void* sslv2_init ( const char *  cacert,
const char *  cert,
const char *  key,
int  verify 
)

Create a SSL structure for SSLv2 (If available)

Parameters
cacertPath to the CA certificate[s].
certPublic certificate to use.
keyPrivate key file.
verifyOpenSSL flags.

Definition at line 299 of file sslutil.c.

References SSL_SSLV2.

299  {
300  const SSL_METHOD *meth = SSLv2_method();
301 
302  return (sslinit(cacert, cert, key, verify, meth, SSL_SSLV2));
303 }
const SSL_METHOD * meth
SSL method.
Definition: sslutil.c:77
SSLv2 This may not be available due to security issues.
Definition: sslutil.c:52
void* sslv3_init ( const char *  cacert,
const char *  cert,
const char *  key,
int  verify 
)

Create a SSL structure for SSLv3.

Parameters
cacertPath to the CA certificate[s].
certPublic certificate to use.
keyPrivate key file.
verifyOpenSSL flags.

Definition at line 311 of file sslutil.c.

References ssldata::ssl, and SSL_SSLV3.

Referenced by socktest().

311  {
312  const SSL_METHOD *meth = SSLv3_method();
313  struct ssldata *ssl;
314 
315  ssl = sslinit(cacert, cert, key, verify, meth, SSL_SSLV3);
316 
317  return (ssl);
318 }
const SSL_METHOD * meth
SSL method.
Definition: sslutil.c:77
SSL data structure for enabling encryption on sockets.
Definition: sslutil.c:66
SSLv3.
Definition: sslutil.c:54
SSL * ssl
OpenSSL ssl.
Definition: sslutil.c:70
void startsslclient ( struct fwsocket sock)

Start SSL on a client socket.

Warning
This should not be called directly
See Also
clientsocket()
Parameters
sockReference to client socket.

Definition at line 811 of file sslutil.c.

References ssldata::flags, fwsocket::ssl, SSL_SERVER, and fwsocket::type.

Referenced by socketclient().

811  {
812  if (!sock || !sock->ssl || (sock->ssl->flags & SSL_SERVER)) {
813  return;
814  }
815 
816  switch(sock->type) {
817  case SOCK_DGRAM:
818  dtlsconnect(sock);
819  break;
820  case SOCK_STREAM:
821  sslsockstart(sock, NULL, 0);
822  break;
823  }
824 }
int flags
SSL flags.
Definition: sslutil.c:75
This session is server mode.
Definition: sslutil.c:60
struct ssldata * ssl
SSL structure for encryption.
Definition: dtsapp.h:146
int type
Socket type.
Definition: dtsapp.h:137
void tlsaccept ( struct fwsocket sock,
struct ssldata orig 
)

Create SSL session for new connection.

Warning
This should never be called.
Parameters
sockReference too new incoming socket.
origServers SSL session to clone.

Definition at line 382 of file sslutil.c.

References objalloc(), setflag, SOCK_FLAG_SSL, and fwsocket::ssl.

Referenced by accept_socket().

382  {
383  setflag(sock, SOCK_FLAG_SSL);
384  if ((sock->ssl = objalloc(sizeof(*sock->ssl), free_ssldata))) {
385  sslsockstart(sock, orig, 1);
386  }
387 }
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
#define setflag(obj, flag)
Atomically set a flag in the flags field of a referenced object.
Definition: dtsapp.h:925
SSL has been requested on this socket dont allow clear read/send.
Definition: dtsapp.h:108
struct ssldata * ssl
SSL structure for encryption.
Definition: dtsapp.h:146
void* tlsv1_init ( const char *  cacert,
const char *  cert,
const char *  key,
int  verify 
)

Create a SSL structure for TLSv1.

Parameters
cacertPath to the CA certificate[s].
certPublic certificate to use.
keyPrivate key file.
verifyOpenSSL flags.

Definition at line 287 of file sslutil.c.

References SSL_TLSV1.

287  {
288  const SSL_METHOD *meth = TLSv1_method();
289 
290  return (sslinit(cacert, cert, key, verify, meth, SSL_TLSV1));
291 }
const SSL_METHOD * meth
SSL method.
Definition: sslutil.c:77
TLSv1.
Definition: sslutil.c:50