00001 /* 00002 * Asterisk -- An open source telephony toolkit. 00003 * 00004 * Copyright (C) 1999 - 2006, Digium, Inc. 00005 * 00006 * Mark Spencer <markster@digium.com> 00007 * 00008 * See http://www.asterisk.org for more information about 00009 * the Asterisk project. Please do not directly contact 00010 * any of the maintainers of this project for assistance; 00011 * the project provides a web site, mailing lists and IRC 00012 * channels for your use. 00013 * 00014 * This program is free software, distributed under the terms of 00015 * the GNU General Public License Version 2. See the LICENSE file 00016 * at the top of the source tree. 00017 */ 00018 00019 /*! 00020 * \file tcptls.h 00021 * 00022 * \brief Generic support for tcp/tls servers in Asterisk. 00023 * \note In order to have TLS/SSL support, we need the openssl libraries. 00024 * Still we can decide whether or not to use them by commenting 00025 * in or out the DO_SSL macro. 00026 * 00027 * TLS/SSL support is basically implemented by reading from a config file 00028 * (currently manager.conf, http.conf and sip.conf) the names of the certificate 00029 * files and cipher to use, and then run ssl_setup() to create an appropriate 00030 * data structure named ssl_ctx. 00031 * 00032 * If we support multiple domains, presumably we need to read multiple 00033 * certificates. 00034 * 00035 * When we are requested to open a TLS socket, we run make_file_from_fd() 00036 * on the socket, to do the necessary setup. At the moment the context's name 00037 * is hardwired in the function, but we can certainly make it into an extra 00038 * parameter to the function. 00039 * 00040 * We declare most of ssl support variables unconditionally, 00041 * because their number is small and this simplifies the code. 00042 * 00043 * \note The ssl-support variables (ssl_ctx, do_ssl, certfile, cipher) 00044 * and their setup should be moved to a more central place, e.g. asterisk.conf 00045 * and the source files that processes it. Similarly, ssl_setup() should 00046 * be run earlier in the startup process so modules have it available. 00047 * 00048 * \ref AstTlsOverview 00049 * 00050 * \todo For SIP, the SubjectAltNames should be checked on verification 00051 * of the certificate. (Check RFC 5922) 00052 * 00053 */ 00054 00055 #ifndef _ASTERISK_TCPTLS_H 00056 #define _ASTERISK_TCPTLS_H 00057 00058 #include "asterisk/netsock2.h" 00059 #include "asterisk/utils.h" 00060 00061 #if defined(HAVE_OPENSSL) && (defined(HAVE_FUNOPEN) || defined(HAVE_FOPENCOOKIE)) 00062 #define DO_SSL /* comment in/out if you want to support ssl */ 00063 #endif 00064 00065 #ifdef DO_SSL 00066 #include <openssl/ssl.h> 00067 #include <openssl/err.h> 00068 #else 00069 /* declare dummy types so we can define a pointer to them */ 00070 typedef struct {} SSL; 00071 typedef struct {} SSL_CTX; 00072 #endif /* DO_SSL */ 00073 00074 /*! SSL support */ 00075 #define AST_CERTFILE "asterisk.pem" 00076 00077 enum ast_ssl_flags { 00078 /*! Verify certificate when acting as server */ 00079 AST_SSL_VERIFY_CLIENT = (1 << 0), 00080 /*! Don't verify certificate when connecting to a server */ 00081 AST_SSL_DONT_VERIFY_SERVER = (1 << 1), 00082 /*! Don't compare "Common Name" against IP or hostname */ 00083 AST_SSL_IGNORE_COMMON_NAME = (1 << 2), 00084 /*! Use SSLv2 for outgoing client connections */ 00085 AST_SSL_SSLV2_CLIENT = (1 << 3), 00086 /*! Use SSLv3 for outgoing client connections */ 00087 AST_SSL_SSLV3_CLIENT = (1 << 4), 00088 /*! Use TLSv1 for outgoing client connections */ 00089 AST_SSL_TLSV1_CLIENT = (1 << 5) 00090 }; 00091 00092 struct ast_tls_config { 00093 int enabled; 00094 char *certfile; 00095 char *pvtfile; 00096 char *cipher; 00097 char *cafile; 00098 char *capath; 00099 struct ast_flags flags; 00100 SSL_CTX *ssl_ctx; 00101 }; 00102 00103 /*! \page AstTlsOverview TLS Implementation Overview 00104 * 00105 * The following code implements a generic mechanism for starting 00106 * services on a TCP or TLS socket. 00107 * The service is configured in the struct session_args, and 00108 * then started by calling server_start(desc) on the descriptor. 00109 * server_start() first verifies if an instance of the service is active, 00110 * and in case shuts it down. Then, if the service must be started, creates 00111 * a socket and a thread in charge of doing the accept(). 00112 * 00113 * The body of the thread is desc->accept_fn(desc), which the user can define 00114 * freely. We supply a sample implementation, server_root(), structured as an 00115 * infinite loop. At the beginning of each iteration it runs periodic_fn() 00116 * if defined (e.g. to perform some cleanup etc.) then issues a poll() 00117 * or equivalent with a timeout of 'poll_timeout' milliseconds, and if the 00118 * following accept() is successful it creates a thread in charge of 00119 * running the session, whose body is desc->worker_fn(). The argument of 00120 * worker_fn() is a struct ast_tcptls_session_instance, which contains the address 00121 * of the other party, a pointer to desc, the file descriptors (fd) on which 00122 * we can do a select/poll (but NOT I/O), and a FILE *on which we can do I/O. 00123 * We have both because we want to support plain and SSL sockets, and 00124 * going through a FILE * lets us provide the encryption/decryption 00125 * on the stream without using an auxiliary thread. 00126 */ 00127 00128 /*! \brief 00129 * arguments for the accepting thread 00130 */ 00131 struct ast_tcptls_session_args { 00132 struct ast_sockaddr local_address; 00133 struct ast_sockaddr old_address; /*!< copy of the local or remote address depending on if its a client or server session */ 00134 struct ast_sockaddr remote_address; 00135 char hostname[MAXHOSTNAMELEN]; /*!< only necessary for SSL clients so we can compare to common name */ 00136 struct ast_tls_config *tls_cfg; /*!< points to the SSL configuration if any */ 00137 int accept_fd; 00138 int poll_timeout; 00139 /*! Server accept_fn thread ID used for external shutdown requests. */ 00140 pthread_t master; 00141 void *(*accept_fn)(void *); /*!< the function in charge of doing the accept */ 00142 void (*periodic_fn)(void *);/*!< something we may want to run before after select on the accept socket */ 00143 void *(*worker_fn)(void *); /*!< the function in charge of doing the actual work */ 00144 const char *name; 00145 }; 00146 00147 /*! \brief 00148 * describes a server instance 00149 */ 00150 struct ast_tcptls_session_instance { 00151 FILE *f; /*!< fopen/funopen result */ 00152 int fd; /*!< the socket returned by accept() */ 00153 SSL *ssl; /*!< ssl state */ 00154 /* iint (*ssl_setup)(SSL *); */ 00155 int client; 00156 struct ast_sockaddr remote_address; 00157 struct ast_tcptls_session_args *parent; 00158 /* Sometimes, when an entity reads TCP data, multiple 00159 * logical messages might be read at the same time. In such 00160 * a circumstance, there needs to be a place to stash the 00161 * extra data. 00162 */ 00163 struct ast_str *overflow_buf; 00164 }; 00165 00166 #if defined(HAVE_FUNOPEN) 00167 #define HOOK_T int 00168 #define LEN_T int 00169 #else 00170 #define HOOK_T ssize_t 00171 #define LEN_T size_t 00172 #endif 00173 00174 /*! 00175 * \brief attempts to connect and start tcptls session, on error the tcptls_session's 00176 * ref count is decremented, fd and file are closed, and NULL is returned. 00177 */ 00178 struct ast_tcptls_session_instance *ast_tcptls_client_start(struct ast_tcptls_session_instance *tcptls_session); 00179 00180 /* \brief Creates a client connection's ast_tcptls_session_instance. */ 00181 struct ast_tcptls_session_instance *ast_tcptls_client_create(struct ast_tcptls_session_args *desc); 00182 00183 void *ast_tcptls_server_root(void *); 00184 00185 /*! 00186 * \brief Closes a tcptls session instance's file and/or file descriptor. 00187 * The tcptls_session will be set to NULL and it's file descriptor will be set to -1 00188 * by this function. 00189 */ 00190 void ast_tcptls_close_session_file(struct ast_tcptls_session_instance *tcptls_session); 00191 00192 /*! 00193 * \brief This is a generic (re)start routine for a TCP server, 00194 * which does the socket/bind/listen and starts a thread for handling 00195 * accept(). 00196 * \version 1.6.1 changed desc parameter to be of ast_tcptls_session_args type 00197 */ 00198 void ast_tcptls_server_start(struct ast_tcptls_session_args *desc); 00199 00200 /*! 00201 * \brief Shutdown a running server if there is one 00202 * \version 1.6.1 changed desc parameter to be of ast_tcptls_session_args type 00203 */ 00204 void ast_tcptls_server_stop(struct ast_tcptls_session_args *desc); 00205 00206 /*! 00207 * \brief Set up an SSL server 00208 * 00209 * \param cfg Configuration for the SSL server 00210 * \retval 1 Success 00211 * \retval 0 Failure 00212 */ 00213 int ast_ssl_setup(struct ast_tls_config *cfg); 00214 00215 /*! 00216 * \brief free resources used by an SSL server 00217 * 00218 * \note This only needs to be called if ast_ssl_setup() was 00219 * directly called first. 00220 * \param cfg Configuration for the SSL server 00221 */ 00222 void ast_ssl_teardown(struct ast_tls_config *cfg); 00223 00224 /*! 00225 * \brief Used to parse conf files containing tls/ssl options. 00226 */ 00227 int ast_tls_read_conf(struct ast_tls_config *tls_cfg, struct ast_tcptls_session_args *tls_desc, const char *varname, const char *value); 00228 00229 HOOK_T ast_tcptls_server_read(struct ast_tcptls_session_instance *ser, void *buf, size_t count); 00230 HOOK_T ast_tcptls_server_write(struct ast_tcptls_session_instance *ser, const void *buf, size_t count); 00231 00232 #endif /* _ASTERISK_TCPTLS_H */