2008-01-18 22:04:33 +00:00
|
|
|
/*
|
|
|
|
* Asterisk -- An open source telephony toolkit.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1999 - 2006, Digium, Inc.
|
|
|
|
*
|
|
|
|
* Mark Spencer <markster@digium.com>
|
|
|
|
*
|
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2008-01-22 08:58:46 +00:00
|
|
|
* \file tcptls.h
|
2008-01-18 22:04:33 +00:00
|
|
|
*
|
|
|
|
* \brief 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.
|
2008-01-22 08:58:46 +00:00
|
|
|
*
|
2008-01-18 22:04:33 +00:00
|
|
|
* TLS/SSL support is basically implemented by reading from a config file
|
2011-11-06 09:51:09 +00:00
|
|
|
* (currently manager.conf, http.conf and sip.conf) the names of the certificate
|
|
|
|
* files and cipher to use, and then run ssl_setup() to create an appropriate
|
|
|
|
* data structure named ssl_ctx.
|
|
|
|
*
|
2008-01-18 22:04:33 +00:00
|
|
|
* If we support multiple domains, presumably we need to read multiple
|
|
|
|
* certificates.
|
2008-01-22 08:58:46 +00:00
|
|
|
*
|
2008-01-18 22:04:33 +00:00
|
|
|
* 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.
|
2008-01-22 08:58:46 +00:00
|
|
|
*
|
2008-01-18 22:04:33 +00:00
|
|
|
* We declare most of ssl support variables unconditionally,
|
|
|
|
* because their number is small and this simplifies the code.
|
|
|
|
*
|
2008-01-22 08:58:46 +00:00
|
|
|
* \note The ssl-support variables (ssl_ctx, do_ssl, certfile, cipher)
|
2008-01-18 22:04:33 +00:00
|
|
|
* 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.
|
2011-11-06 09:51:09 +00:00
|
|
|
*
|
|
|
|
* \ref AstTlsOverview
|
|
|
|
*
|
|
|
|
* \todo For SIP, the SubjectAltNames should be checked on verification
|
|
|
|
* of the certificate. (Check RFC 5922)
|
2008-01-18 22:04:33 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-10-19 19:11:28 +00:00
|
|
|
#ifndef _ASTERISK_TCPTLS_H
|
|
|
|
#define _ASTERISK_TCPTLS_H
|
2008-01-18 22:04:33 +00:00
|
|
|
|
2010-07-08 22:08:07 +00:00
|
|
|
#include "asterisk/netsock2.h"
|
2008-01-18 22:04:33 +00:00
|
|
|
#include "asterisk/utils.h"
|
|
|
|
|
|
|
|
#if defined(HAVE_OPENSSL) && (defined(HAVE_FUNOPEN) || defined(HAVE_FOPENCOOKIE))
|
|
|
|
#define DO_SSL /* comment in/out if you want to support ssl */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef DO_SSL
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#else
|
|
|
|
/* declare dummy types so we can define a pointer to them */
|
|
|
|
typedef struct {} SSL;
|
|
|
|
typedef struct {} SSL_CTX;
|
|
|
|
#endif /* DO_SSL */
|
|
|
|
|
|
|
|
/*! SSL support */
|
|
|
|
#define AST_CERTFILE "asterisk.pem"
|
|
|
|
|
|
|
|
enum ast_ssl_flags {
|
|
|
|
/*! Verify certificate when acting as server */
|
|
|
|
AST_SSL_VERIFY_CLIENT = (1 << 0),
|
|
|
|
/*! Don't verify certificate when connecting to a server */
|
|
|
|
AST_SSL_DONT_VERIFY_SERVER = (1 << 1),
|
|
|
|
/*! Don't compare "Common Name" against IP or hostname */
|
2009-04-29 21:13:43 +00:00
|
|
|
AST_SSL_IGNORE_COMMON_NAME = (1 << 2),
|
|
|
|
/*! Use SSLv2 for outgoing client connections */
|
|
|
|
AST_SSL_SSLV2_CLIENT = (1 << 3),
|
|
|
|
/*! Use SSLv3 for outgoing client connections */
|
|
|
|
AST_SSL_SSLV3_CLIENT = (1 << 4),
|
|
|
|
/*! Use TLSv1 for outgoing client connections */
|
|
|
|
AST_SSL_TLSV1_CLIENT = (1 << 5)
|
2008-01-18 22:04:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ast_tls_config {
|
|
|
|
int enabled;
|
|
|
|
char *certfile;
|
2009-04-24 21:22:31 +00:00
|
|
|
char *pvtfile;
|
2008-01-18 22:04:33 +00:00
|
|
|
char *cipher;
|
|
|
|
char *cafile;
|
|
|
|
char *capath;
|
|
|
|
struct ast_flags flags;
|
|
|
|
SSL_CTX *ssl_ctx;
|
|
|
|
};
|
|
|
|
|
2011-11-06 09:51:09 +00:00
|
|
|
/*! \page AstTlsOverview TLS Implementation Overview
|
|
|
|
*
|
2008-01-18 22:04:33 +00:00
|
|
|
* The following code implements a generic mechanism for starting
|
|
|
|
* services on a TCP or TLS socket.
|
2008-10-19 19:11:28 +00:00
|
|
|
* The service is configured in the struct session_args, and
|
2008-01-18 22:04:33 +00:00
|
|
|
* then started by calling server_start(desc) on the descriptor.
|
|
|
|
* server_start() first verifies if an instance of the service is active,
|
|
|
|
* and in case shuts it down. Then, if the service must be started, creates
|
|
|
|
* a socket and a thread in charge of doing the accept().
|
|
|
|
*
|
|
|
|
* The body of the thread is desc->accept_fn(desc), which the user can define
|
|
|
|
* freely. We supply a sample implementation, server_root(), structured as an
|
|
|
|
* infinite loop. At the beginning of each iteration it runs periodic_fn()
|
|
|
|
* if defined (e.g. to perform some cleanup etc.) then issues a poll()
|
|
|
|
* or equivalent with a timeout of 'poll_timeout' milliseconds, and if the
|
|
|
|
* following accept() is successful it creates a thread in charge of
|
|
|
|
* running the session, whose body is desc->worker_fn(). The argument of
|
2008-03-12 22:13:18 +00:00
|
|
|
* worker_fn() is a struct ast_tcptls_session_instance, which contains the address
|
2008-01-18 22:04:33 +00:00
|
|
|
* of the other party, a pointer to desc, the file descriptors (fd) on which
|
2008-10-19 19:11:28 +00:00
|
|
|
* we can do a select/poll (but NOT I/O), and a FILE *on which we can do I/O.
|
2008-01-18 22:04:33 +00:00
|
|
|
* We have both because we want to support plain and SSL sockets, and
|
2008-10-19 19:11:28 +00:00
|
|
|
* going through a FILE * lets us provide the encryption/decryption
|
2008-01-18 22:04:33 +00:00
|
|
|
* on the stream without using an auxiliary thread.
|
|
|
|
*/
|
|
|
|
|
2008-01-22 08:58:46 +00:00
|
|
|
/*! \brief
|
2008-01-18 22:04:33 +00:00
|
|
|
* arguments for the accepting thread
|
|
|
|
*/
|
2008-10-19 19:11:28 +00:00
|
|
|
struct ast_tcptls_session_args {
|
2010-07-08 22:08:07 +00:00
|
|
|
struct ast_sockaddr local_address;
|
|
|
|
struct ast_sockaddr old_address; /*!< copy of the local or remote address depending on if its a client or server session */
|
|
|
|
struct ast_sockaddr remote_address;
|
2008-01-22 08:58:46 +00:00
|
|
|
char hostname[MAXHOSTNAMELEN]; /*!< only necessary for SSL clients so we can compare to common name */
|
|
|
|
struct ast_tls_config *tls_cfg; /*!< points to the SSL configuration if any */
|
2008-01-18 22:04:33 +00:00
|
|
|
int accept_fd;
|
|
|
|
int poll_timeout;
|
|
|
|
pthread_t master;
|
2008-01-22 08:58:46 +00:00
|
|
|
void *(*accept_fn)(void *); /*!< the function in charge of doing the accept */
|
|
|
|
void (*periodic_fn)(void *);/*!< something we may want to run before after select on the accept socket */
|
|
|
|
void *(*worker_fn)(void *); /*!< the function in charge of doing the actual work */
|
2008-01-18 22:04:33 +00:00
|
|
|
const char *name;
|
|
|
|
};
|
|
|
|
|
2011-11-06 09:51:09 +00:00
|
|
|
/*! \brief
|
2008-10-19 19:11:28 +00:00
|
|
|
* describes a server instance
|
|
|
|
*/
|
|
|
|
struct ast_tcptls_session_instance {
|
2011-11-06 09:51:09 +00:00
|
|
|
FILE *f; /*!< fopen/funopen result */
|
|
|
|
int fd; /*!< the socket returned by accept() */
|
|
|
|
SSL *ssl; /*!< ssl state */
|
2008-10-19 19:11:28 +00:00
|
|
|
/* iint (*ssl_setup)(SSL *); */
|
|
|
|
int client;
|
2010-07-08 22:08:07 +00:00
|
|
|
struct ast_sockaddr remote_address;
|
2008-10-19 19:11:28 +00:00
|
|
|
struct ast_tcptls_session_args *parent;
|
|
|
|
ast_mutex_t lock;
|
|
|
|
};
|
|
|
|
|
2008-01-18 22:04:33 +00:00
|
|
|
#if defined(HAVE_FUNOPEN)
|
|
|
|
#define HOOK_T int
|
|
|
|
#define LEN_T int
|
|
|
|
#else
|
|
|
|
#define HOOK_T ssize_t
|
|
|
|
#define LEN_T size_t
|
|
|
|
#endif
|
|
|
|
|
SIP TCP/TLS: move client connection setup/write into tcp helper thread, various related locking/memory fixes.
What this patch fixes
1.Moves sip TCP/TLS connection setup into the TCP helper thread:
Connection setup takes awhile and before this it was being
done while holding the monitor lock.
2.Moves TCP/TLS writing to the TCP helper thread: Through the
use of a packet queue and an alert pipe, the TCP helper thread
can now be woken up to write data as well as read data.
3.Locking error: sip_xmit returned an XMIT_ERROR without giving
up the tcptls_session lock. This lock has been completely removed
from sip_xmit and placed in the new sip_tcptls_write() function.
4.Memory leak: When creating a tcptls_client the tls_cfg was alloced
but never freed unless the tcptls_session failed to start. Now the
session_args for a sip client are an ao2 object which frees the
tls_cfg on destruction.
5.Pointer to stack variable: During sip_prepare_socket the creation
of a client's ast_tcptls_session_args was done on the stack and
stored as a pointer in the newly created tcptls_session. Depending
on the events that followed, there was a slight possibility that
pointer could have been accessed after the stack returned. Given
the new changes, it is always accessed after the stack returns
which is why I found it.
Notable code changes
1.I broke tcptls.c's ast_tcptls_client_start() function into two
functions. One for creating and allocating the new tcptls_session,
and a separate one for starting and handling the new connection.
This allowed me to create the tcptls_session, launch the helper
thread, and then establish the connection within the helper thread.
2.Writes to a tcptls_session are now done within the helper thread.
This is done by using an alert pipe to wake up the thread if new
data needs to be sent. The thread's sip_threadinfo object contains
the alert pipe as well as the packet queue.
3.Since the threadinfo object contains the alert pipe, it must now be
accessed outside of the helper thread for every write (queuing of a
packet). For easy lookup, I moved the threadinfo objects from a
linked list to an ao2_container.
(closes issue #13136)
Reported by: pabelanger
Tested by: dvossel, whys
(closes issue #15894)
Reported by: dvossel
Tested by: dvossel
Review: https://reviewboard.asterisk.org/r/380/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@225445 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2009-10-22 19:55:51 +00:00
|
|
|
/*!
|
|
|
|
* \brief 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.
|
|
|
|
*/
|
|
|
|
struct ast_tcptls_session_instance *ast_tcptls_client_start(struct ast_tcptls_session_instance *tcptls_session);
|
|
|
|
|
|
|
|
/* \brief Creates a client connection's ast_tcptls_session_instance. */
|
|
|
|
struct ast_tcptls_session_instance *ast_tcptls_client_create(struct ast_tcptls_session_args *desc);
|
2008-01-18 22:04:33 +00:00
|
|
|
|
2008-03-04 22:23:21 +00:00
|
|
|
void *ast_tcptls_server_root(void *);
|
2009-03-09 20:58:17 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief 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
|
|
|
|
*/
|
2008-10-19 19:11:28 +00:00
|
|
|
void ast_tcptls_server_start(struct ast_tcptls_session_args *desc);
|
2009-03-09 20:58:17 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Shutdown a running server if there is one
|
|
|
|
* \version 1.6.1 changed desc parameter to be of ast_tcptls_session_args type
|
|
|
|
*/
|
2008-10-19 19:11:28 +00:00
|
|
|
void ast_tcptls_server_stop(struct ast_tcptls_session_args *desc);
|
2008-03-04 22:23:21 +00:00
|
|
|
int ast_ssl_setup(struct ast_tls_config *cfg);
|
2008-01-18 22:04:33 +00:00
|
|
|
|
2009-04-29 14:39:48 +00:00
|
|
|
/*!
|
|
|
|
* \brief Used to parse conf files containing tls/ssl options.
|
|
|
|
*/
|
|
|
|
int ast_tls_read_conf(struct ast_tls_config *tls_cfg, struct ast_tcptls_session_args *tls_desc, const char *varname, const char *value);
|
|
|
|
|
2008-03-12 22:13:18 +00:00
|
|
|
HOOK_T ast_tcptls_server_read(struct ast_tcptls_session_instance *ser, void *buf, size_t count);
|
2008-12-13 08:36:35 +00:00
|
|
|
HOOK_T ast_tcptls_server_write(struct ast_tcptls_session_instance *ser, const void *buf, size_t count);
|
2008-01-18 22:04:33 +00:00
|
|
|
|
2008-10-19 19:11:28 +00:00
|
|
|
#endif /* _ASTERISK_TCPTLS_H */
|