Sat Mar 26 11:29:30 2011

Asterisk developer's documentation


tcptls.h File Reference

Generic support for tcp/tls servers in Asterisk. More...

#include "asterisk/netsock2.h"
#include "asterisk/utils.h"
#include <openssl/ssl.h>
#include <openssl/err.h>

Include dependency graph for tcptls.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ast_tcptls_session_args
 arguments for the accepting thread More...
struct  ast_tcptls_session_instance
struct  ast_tls_config

Defines

#define AST_CERTFILE   "asterisk.pem"
#define DO_SSL
#define HOOK_T   ssize_t
#define LEN_T   size_t

Enumerations

enum  ast_ssl_flags {
  AST_SSL_VERIFY_CLIENT = (1 << 0), AST_SSL_DONT_VERIFY_SERVER = (1 << 1), AST_SSL_IGNORE_COMMON_NAME = (1 << 2), AST_SSL_SSLV2_CLIENT = (1 << 3),
  AST_SSL_SSLV3_CLIENT = (1 << 4), AST_SSL_TLSV1_CLIENT = (1 << 5)
}

Functions

int ast_ssl_setup (struct ast_tls_config *cfg)
struct
ast_tcptls_session_instance * 
ast_tcptls_client_create (struct ast_tcptls_session_args *desc)
struct
ast_tcptls_session_instance * 
ast_tcptls_client_start (struct ast_tcptls_session_instance *tcptls_session)
 attempts to connect and start tcptls session, on error the tcptls_session's ref count is decremented, fd and file are closed, and NULL is returned.
HOOK_T ast_tcptls_server_read (struct ast_tcptls_session_instance *ser, void *buf, size_t count)
void * ast_tcptls_server_root (void *)
void ast_tcptls_server_start (struct ast_tcptls_session_args *desc)
 This is a generic (re)start routine for a TCP server, which does the socket/bind/listen and starts a thread for handling accept().
void ast_tcptls_server_stop (struct ast_tcptls_session_args *desc)
 Shutdown a running server if there is one.
HOOK_T ast_tcptls_server_write (struct ast_tcptls_session_instance *ser, const void *buf, size_t count)
int ast_tls_read_conf (struct ast_tls_config *tls_cfg, struct ast_tcptls_session_args *tls_desc, const char *varname, const char *value)
 Used to parse conf files containing tls/ssl options.


Detailed Description

Generic support for tcp/tls servers in Asterisk.

Note:
In order to have TLS/SSL support, we need the openssl libraries. Still we can decide whether or not to use them by commenting in or out the DO_SSL macro.
TLS/SSL support is basically implemented by reading from a config file (currently http.conf and sip.conf) the names of the certificate and cipher to use, and then run ssl_setup() to create an appropriate SSL_CTX (ssl_ctx) If we support multiple domains, presumably we need to read multiple certificates.

When we are requested to open a TLS socket, we run make_file_from_fd() on the socket, to do the necessary setup. At the moment the context's name is hardwired in the function, but we can certainly make it into an extra parameter to the function.

We declare most of ssl support variables unconditionally, because their number is small and this simplifies the code.

Note:
The ssl-support variables (ssl_ctx, do_ssl, certfile, cipher) and their setup should be moved to a more central place, e.g. asterisk.conf and the source files that processes it. Similarly, ssl_setup() should be run earlier in the startup process so modules have it available.

Definition in file tcptls.h.


Define Documentation

#define AST_CERTFILE   "asterisk.pem"

SSL support

Definition at line 68 of file tcptls.h.

Referenced by __ast_http_load(), __init_manager(), and reload_config().

#define DO_SSL

Definition at line 55 of file tcptls.h.

#define HOOK_T   ssize_t

Definition at line 156 of file tcptls.h.

#define LEN_T   size_t

Definition at line 157 of file tcptls.h.


Enumeration Type Documentation

Enumerator:
AST_SSL_VERIFY_CLIENT  Verify certificate when acting as server
AST_SSL_DONT_VERIFY_SERVER  Don't verify certificate when connecting to a server
AST_SSL_IGNORE_COMMON_NAME  Don't compare "Common Name" against IP or hostname
AST_SSL_SSLV2_CLIENT  Use SSLv2 for outgoing client connections
AST_SSL_SSLV3_CLIENT  Use SSLv3 for outgoing client connections
AST_SSL_TLSV1_CLIENT  Use TLSv1 for outgoing client connections

Definition at line 70 of file tcptls.h.

00070                    {
00071    /*! Verify certificate when acting as server */
00072    AST_SSL_VERIFY_CLIENT = (1 << 0),
00073    /*! Don't verify certificate when connecting to a server */
00074    AST_SSL_DONT_VERIFY_SERVER = (1 << 1),
00075    /*! Don't compare "Common Name" against IP or hostname */
00076    AST_SSL_IGNORE_COMMON_NAME = (1 << 2),
00077    /*! Use SSLv2 for outgoing client connections */
00078    AST_SSL_SSLV2_CLIENT = (1 << 3),
00079    /*! Use SSLv3 for outgoing client connections */
00080    AST_SSL_SSLV3_CLIENT = (1 << 4),
00081    /*! Use TLSv1 for outgoing client connections */
00082    AST_SSL_TLSV1_CLIENT = (1 << 5)
00083 };


Function Documentation

int ast_ssl_setup ( struct ast_tls_config *  cfg  ) 

Definition at line 360 of file tcptls.c.

References __ssl_setup().

Referenced by __ast_http_load(), __init_manager(), and reload_config().

00361 {
00362    return __ssl_setup(cfg, 0);
00363 }

struct ast_tcptls_session_instance* ast_tcptls_client_create ( struct ast_tcptls_session_args *  desc  )  [read]

Definition at line 402 of file tcptls.c.

References ast_tcptls_session_args::accept_fd, ao2_alloc, ao2_ref, ast_bind(), ast_debug, ast_log(), ast_mutex_init, ast_sockaddr_cmp(), ast_sockaddr_copy(), ast_sockaddr_is_ipv6(), ast_sockaddr_isnull(), ast_sockaddr_stringify(), ast_tcptls_session_instance::client, errno, ast_tcptls_session_instance::fd, ast_tcptls_session_args::local_address, ast_tcptls_session_instance::lock, LOG_ERROR, LOG_WARNING, ast_tcptls_session_args::name, ast_tcptls_session_args::old_address, ast_tcptls_session_instance::parent, ast_tcptls_session_instance::remote_address, ast_tcptls_session_args::remote_address, session_instance_destructor(), and ast_tcptls_session_args::worker_fn.

Referenced by app_exec(), and sip_prepare_socket().

00403 {
00404    int x = 1;
00405    struct ast_tcptls_session_instance *tcptls_session = NULL;
00406 
00407    /* Do nothing if nothing has changed */
00408    if (!ast_sockaddr_cmp(&desc->old_address, &desc->remote_address)) {
00409       ast_debug(1, "Nothing changed in %s\n", desc->name);
00410       return NULL;
00411    }
00412 
00413    ast_sockaddr_copy(&desc->old_address, &desc->remote_address);
00414 
00415    if (desc->accept_fd != -1)
00416       close(desc->accept_fd);
00417 
00418    desc->accept_fd = socket(ast_sockaddr_is_ipv6(&desc->remote_address) ?
00419              AF_INET6 : AF_INET, SOCK_STREAM, IPPROTO_TCP);
00420    if (desc->accept_fd < 0) {
00421       ast_log(LOG_WARNING, "Unable to allocate socket for %s: %s\n",
00422          desc->name, strerror(errno));
00423       return NULL;
00424    }
00425 
00426    /* if a local address was specified, bind to it so the connection will
00427       originate from the desired address */
00428    if (!ast_sockaddr_isnull(&desc->local_address)) {
00429       setsockopt(desc->accept_fd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
00430       if (ast_bind(desc->accept_fd, &desc->local_address)) {
00431          ast_log(LOG_ERROR, "Unable to bind %s to %s: %s\n",
00432             desc->name,
00433             ast_sockaddr_stringify(&desc->local_address),
00434             strerror(errno));
00435          goto error;
00436       }
00437    }
00438 
00439    if (!(tcptls_session = ao2_alloc(sizeof(*tcptls_session), session_instance_destructor)))
00440       goto error;
00441 
00442    ast_mutex_init(&tcptls_session->lock);
00443    tcptls_session->client = 1;
00444    tcptls_session->fd = desc->accept_fd;
00445    tcptls_session->parent = desc;
00446    tcptls_session->parent->worker_fn = NULL;
00447    ast_sockaddr_copy(&tcptls_session->remote_address,
00448            &desc->remote_address);
00449 
00450    return tcptls_session;
00451 
00452 error:
00453    close(desc->accept_fd);
00454    desc->accept_fd = -1;
00455    if (tcptls_session)
00456       ao2_ref(tcptls_session, -1);
00457    return NULL;
00458 }

struct ast_tcptls_session_instance* ast_tcptls_client_start ( struct ast_tcptls_session_instance *  tcptls_session  )  [read]

attempts to connect and start tcptls session, on error the tcptls_session's ref count is decremented, fd and file are closed, and NULL is returned.

Definition at line 365 of file tcptls.c.

References __ssl_setup(), ast_tcptls_session_args::accept_fd, ao2_ref, ast_connect(), ast_log(), ast_sockaddr_stringify(), desc, ast_tls_config::enabled, errno, handle_tcptls_connection(), LOG_ERROR, ast_tcptls_session_args::name, ast_tcptls_session_instance::parent, ast_tcptls_session_args::remote_address, and ast_tcptls_session_args::tls_cfg.

Referenced by _sip_tcp_helper_thread(), and app_exec().

00366 {
00367    struct ast_tcptls_session_args *desc;
00368    int flags;
00369 
00370    if (!(desc = tcptls_session->parent)) {
00371       goto client_start_error;
00372    }
00373 
00374    if (ast_connect(desc->accept_fd, &desc->remote_address)) {
00375       ast_log(LOG_ERROR, "Unable to connect %s to %s: %s\n",
00376          desc->name,
00377          ast_sockaddr_stringify(&desc->remote_address),
00378          strerror(errno));
00379       goto client_start_error;
00380    }
00381 
00382    flags = fcntl(desc->accept_fd, F_GETFL);
00383    fcntl(desc->accept_fd, F_SETFL, flags & ~O_NONBLOCK);
00384 
00385    if (desc->tls_cfg) {
00386       desc->tls_cfg->enabled = 1;
00387       __ssl_setup(desc->tls_cfg, 1);
00388    }
00389 
00390    return handle_tcptls_connection(tcptls_session);
00391 
00392 client_start_error:
00393    close(desc->accept_fd);
00394    desc->accept_fd = -1;
00395    if (tcptls_session) {
00396       ao2_ref(tcptls_session, -1);
00397    }
00398    return NULL;
00399 
00400 }

HOOK_T ast_tcptls_server_read ( struct ast_tcptls_session_instance *  ser,
void *  buf,
size_t  count 
)

Definition at line 86 of file tcptls.c.

References ast_log(), errno, ast_tcptls_session_instance::fd, LOG_ERROR, ast_tcptls_session_instance::ssl, and ssl_read().

00087 {
00088    if (tcptls_session->fd == -1) {
00089       ast_log(LOG_ERROR, "server_read called with an fd of -1\n");
00090       errno = EIO;
00091       return -1;
00092    }
00093 
00094 #ifdef DO_SSL
00095    if (tcptls_session->ssl)
00096       return ssl_read(tcptls_session->ssl, buf, count);
00097 #endif
00098    return read(tcptls_session->fd, buf, count);
00099 }

void* ast_tcptls_server_root ( void *   ) 

Definition at line 234 of file tcptls.c.

References ast_tcptls_session_args::accept_fd, ao2_alloc, ao2_ref, ast_accept(), ast_log(), ast_mutex_init, ast_pthread_create_detached_background, ast_sockaddr_copy(), ast_wait_for_input(), ast_tcptls_session_instance::client, desc, errno, ast_tcptls_session_instance::fd, handle_tcptls_connection(), ast_tcptls_session_instance::lock, LOG_WARNING, ast_tcptls_session_instance::parent, ast_tcptls_session_args::periodic_fn, ast_tcptls_session_args::poll_timeout, ast_tcptls_session_instance::remote_address, and session_instance_destructor().

00235 {
00236    struct ast_tcptls_session_args *desc = data;
00237    int fd;
00238    struct ast_sockaddr addr;
00239    struct ast_tcptls_session_instance *tcptls_session;
00240    pthread_t launched;
00241 
00242    for (;;) {
00243       int i, flags;
00244 
00245       if (desc->periodic_fn)
00246          desc->periodic_fn(desc);
00247       i = ast_wait_for_input(desc->accept_fd, desc->poll_timeout);
00248       if (i <= 0)
00249          continue;
00250       fd = ast_accept(desc->accept_fd, &addr);
00251       if (fd < 0) {
00252          if ((errno != EAGAIN) && (errno != EINTR))
00253             ast_log(LOG_WARNING, "Accept failed: %s\n", strerror(errno));
00254          continue;
00255       }
00256       tcptls_session = ao2_alloc(sizeof(*tcptls_session), session_instance_destructor);
00257       if (!tcptls_session) {
00258          ast_log(LOG_WARNING, "No memory for new session: %s\n", strerror(errno));
00259          close(fd);
00260          continue;
00261       }
00262 
00263       ast_mutex_init(&tcptls_session->lock);
00264 
00265       flags = fcntl(fd, F_GETFL);
00266       fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
00267       tcptls_session->fd = fd;
00268       tcptls_session->parent = desc;
00269       ast_sockaddr_copy(&tcptls_session->remote_address, &addr);
00270 
00271       tcptls_session->client = 0;
00272 
00273       /* This thread is now the only place that controls the single ref to tcptls_session */
00274       if (ast_pthread_create_detached_background(&launched, NULL, handle_tcptls_connection, tcptls_session)) {
00275          ast_log(LOG_WARNING, "Unable to launch helper thread: %s\n", strerror(errno));
00276          close(tcptls_session->fd);
00277          ao2_ref(tcptls_session, -1);
00278       }
00279    }
00280    return NULL;
00281 }

void ast_tcptls_server_start ( struct ast_tcptls_session_args *  desc  ) 

This is a generic (re)start routine for a TCP server, which does the socket/bind/listen and starts a thread for handling accept().

Version:
1.6.1 changed desc parameter to be of ast_tcptls_session_args type

Definition at line 460 of file tcptls.c.

References ast_tcptls_session_args::accept_fd, ast_tcptls_session_args::accept_fn, ast_bind(), ast_debug, ast_log(), ast_pthread_create_background, AST_PTHREADT_NULL, ast_sockaddr_cmp(), ast_sockaddr_copy(), ast_sockaddr_is_ipv6(), ast_sockaddr_isnull(), ast_sockaddr_stringify(), errno, ast_tcptls_session_args::local_address, LOG_ERROR, ast_tcptls_session_args::master, ast_tcptls_session_args::name, and ast_tcptls_session_args::old_address.

Referenced by __ast_http_load(), __init_manager(), and reload_config().

00461 {
00462    int flags;
00463    int x = 1;
00464 
00465    /* Do nothing if nothing has changed */
00466    if (!ast_sockaddr_cmp(&desc->old_address, &desc->local_address)) {
00467       ast_debug(1, "Nothing changed in %s\n", desc->name);
00468       return;
00469    }
00470 
00471    ast_sockaddr_copy(&desc->old_address, &desc->local_address);
00472 
00473    /* Shutdown a running server if there is one */
00474    if (desc->master != AST_PTHREADT_NULL) {
00475       pthread_cancel(desc->master);
00476       pthread_kill(desc->master, SIGURG);
00477       pthread_join(desc->master, NULL);
00478    }
00479 
00480    if (desc->accept_fd != -1)
00481       close(desc->accept_fd);
00482 
00483    /* If there's no new server, stop here */
00484    if (ast_sockaddr_isnull(&desc->local_address)) {
00485       ast_debug(2, "Server disabled:  %s\n", desc->name);
00486       return;
00487    }
00488 
00489    desc->accept_fd = socket(ast_sockaddr_is_ipv6(&desc->local_address) ?
00490              AF_INET6 : AF_INET, SOCK_STREAM, 0);
00491    if (desc->accept_fd < 0) {
00492       ast_log(LOG_ERROR, "Unable to allocate socket for %s: %s\n", desc->name, strerror(errno));
00493       return;
00494    }
00495 
00496    setsockopt(desc->accept_fd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
00497    if (ast_bind(desc->accept_fd, &desc->local_address)) {
00498       ast_log(LOG_ERROR, "Unable to bind %s to %s: %s\n",
00499          desc->name,
00500          ast_sockaddr_stringify(&desc->local_address),
00501          strerror(errno));
00502       goto error;
00503    }
00504    if (listen(desc->accept_fd, 10)) {
00505       ast_log(LOG_ERROR, "Unable to listen for %s!\n", desc->name);
00506       goto error;
00507    }
00508    flags = fcntl(desc->accept_fd, F_GETFL);
00509    fcntl(desc->accept_fd, F_SETFL, flags | O_NONBLOCK);
00510    if (ast_pthread_create_background(&desc->master, NULL, desc->accept_fn, desc)) {
00511       ast_log(LOG_ERROR, "Unable to launch thread for %s on %s: %s\n",
00512          desc->name,
00513          ast_sockaddr_stringify(&desc->local_address),
00514          strerror(errno));
00515       goto error;
00516    }
00517    return;
00518 
00519 error:
00520    close(desc->accept_fd);
00521    desc->accept_fd = -1;
00522 }

void ast_tcptls_server_stop ( struct ast_tcptls_session_args *  desc  ) 

Shutdown a running server if there is one.

Version:
1.6.1 changed desc parameter to be of ast_tcptls_session_args type

Definition at line 524 of file tcptls.c.

References ast_tcptls_session_args::accept_fd, ast_debug, AST_PTHREADT_NULL, ast_tcptls_session_args::master, and ast_tcptls_session_args::name.

Referenced by unload_module().

00525 {
00526    if (desc->master != AST_PTHREADT_NULL) {
00527       pthread_cancel(desc->master);
00528       pthread_kill(desc->master, SIGURG);
00529       pthread_join(desc->master, NULL);
00530    }
00531    if (desc->accept_fd != -1)
00532       close(desc->accept_fd);
00533    desc->accept_fd = -1;
00534    ast_debug(2, "Stopped server :: %s\n", desc->name);
00535 }

HOOK_T ast_tcptls_server_write ( struct ast_tcptls_session_instance *  ser,
const void *  buf,
size_t  count 
)

Definition at line 101 of file tcptls.c.

References ast_log(), errno, ast_tcptls_session_instance::fd, LOG_ERROR, ast_tcptls_session_instance::ssl, and ssl_write().

Referenced by _sip_tcp_helper_thread().

00102 {
00103    if (tcptls_session->fd == -1) {
00104       ast_log(LOG_ERROR, "server_write called with an fd of -1\n");
00105       errno = EIO;
00106       return -1;
00107    }
00108 
00109 #ifdef DO_SSL
00110    if (tcptls_session->ssl)
00111       return ssl_write(tcptls_session->ssl, buf, count);
00112 #endif
00113    return write(tcptls_session->fd, buf, count);
00114 }

int ast_tls_read_conf ( struct ast_tls_config *  tls_cfg,
struct ast_tcptls_session_args *  tls_desc,
const char *  varname,
const char *  value 
)

Used to parse conf files containing tls/ssl options.

Definition at line 537 of file tcptls.c.

References ast_clear_flag, ast_free, ast_log(), ast_parse_arg(), ast_set2_flag, ast_set_flag, AST_SSL_DONT_VERIFY_SERVER, AST_SSL_SSLV2_CLIENT, AST_SSL_SSLV3_CLIENT, AST_SSL_TLSV1_CLIENT, AST_SSL_VERIFY_CLIENT, ast_strdup, ast_true(), ast_tls_config::cafile, ast_tls_config::capath, ast_tls_config::certfile, ast_tls_config::cipher, ast_tls_config::enabled, ast_tls_config::flags, ast_tcptls_session_args::local_address, LOG_WARNING, PARSE_ADDR, and ast_tls_config::pvtfile.

Referenced by __ast_http_load(), __init_manager(), and reload_config().

00538 {
00539    if (!strcasecmp(varname, "tlsenable") || !strcasecmp(varname, "sslenable")) {
00540       tls_cfg->enabled = ast_true(value) ? 1 : 0;
00541    } else if (!strcasecmp(varname, "tlscertfile") || !strcasecmp(varname, "sslcert") || !strcasecmp(varname, "tlscert")) {
00542       ast_free(tls_cfg->certfile);
00543       tls_cfg->certfile = ast_strdup(value);
00544    } else if (!strcasecmp(varname, "tlsprivatekey") || !strcasecmp(varname, "sslprivatekey")) {
00545       ast_free(tls_cfg->pvtfile);
00546       tls_cfg->pvtfile = ast_strdup(value);
00547    } else if (!strcasecmp(varname, "tlscipher") || !strcasecmp(varname, "sslcipher")) {
00548       ast_free(tls_cfg->cipher);
00549       tls_cfg->cipher = ast_strdup(value);
00550    } else if (!strcasecmp(varname, "tlscafile")) {
00551       ast_free(tls_cfg->cafile);
00552       tls_cfg->cafile = ast_strdup(value);
00553    } else if (!strcasecmp(varname, "tlscapath")) {
00554       ast_free(tls_cfg->capath);
00555       tls_cfg->capath = ast_strdup(value);
00556    } else if (!strcasecmp(varname, "tlsverifyclient")) {
00557       ast_set2_flag(&tls_cfg->flags, ast_true(value), AST_SSL_VERIFY_CLIENT);
00558    } else if (!strcasecmp(varname, "tlsdontverifyserver")) {
00559       ast_set2_flag(&tls_cfg->flags, ast_true(value), AST_SSL_DONT_VERIFY_SERVER);
00560    } else if (!strcasecmp(varname, "tlsbindaddr") || !strcasecmp(varname, "sslbindaddr")) {
00561       if (ast_parse_arg(value, PARSE_ADDR, &tls_desc->local_address))
00562          ast_log(LOG_WARNING, "Invalid %s '%s'\n", varname, value);
00563    } else if (!strcasecmp(varname, "tlsclientmethod") || !strcasecmp(varname, "sslclientmethod")) {
00564       if (!strcasecmp(value, "tlsv1")) {
00565          ast_set_flag(&tls_cfg->flags, AST_SSL_TLSV1_CLIENT);
00566          ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV3_CLIENT);
00567          ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV2_CLIENT);
00568       } else if (!strcasecmp(value, "sslv3")) {
00569          ast_set_flag(&tls_cfg->flags, AST_SSL_SSLV3_CLIENT);
00570          ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV2_CLIENT);
00571          ast_clear_flag(&tls_cfg->flags, AST_SSL_TLSV1_CLIENT);
00572       } else if (!strcasecmp(value, "sslv2")) {
00573          ast_set_flag(&tls_cfg->flags, AST_SSL_SSLV2_CLIENT);
00574          ast_clear_flag(&tls_cfg->flags, AST_SSL_TLSV1_CLIENT);
00575          ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV3_CLIENT);
00576       }
00577    } else {
00578       return -1;
00579    }
00580 
00581    return 0;
00582 }


Generated on Sat Mar 26 11:29:32 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.9