cleaup of the TCP/TLS socket API:
[asterisk/asterisk.git] / include / asterisk / tcptls.h
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*!
20  * \file tcptls.h
21  *
22  * \brief Generic support for tcp/tls servers in Asterisk.
23  * \note In order to have TLS/SSL support, we need the openssl libraries.
24  * Still we can decide whether or not to use them by commenting
25  * in or out the DO_SSL macro.
26  *
27  * TLS/SSL support is basically implemented by reading from a config file
28  * (currently http.conf and sip.conf) the names of the certificate and cipher to use,
29  * and then run ssl_setup() to create an appropriate SSL_CTX (ssl_ctx)
30  * If we support multiple domains, presumably we need to read multiple
31  * certificates.
32  *
33  * When we are requested to open a TLS socket, we run make_file_from_fd()
34  * on the socket, to do the necessary setup. At the moment the context's name
35  * is hardwired in the function, but we can certainly make it into an extra
36  * parameter to the function.
37  *
38  * We declare most of ssl support variables unconditionally,
39  * because their number is small and this simplifies the code.
40  *
41  * \note The ssl-support variables (ssl_ctx, do_ssl, certfile, cipher)
42  * and their setup should be moved to a more central place, e.g. asterisk.conf
43  * and the source files that processes it. Similarly, ssl_setup() should
44  * be run earlier in the startup process so modules have it available.
45  *
46  */
47
48 #ifndef _ASTERISK_TCPTLS_H
49 #define _ASTERISK_TCPTLS_H
50
51 #include "asterisk/utils.h"
52
53 #if defined(HAVE_OPENSSL) && (defined(HAVE_FUNOPEN) || defined(HAVE_FOPENCOOKIE))
54 #define DO_SSL  /* comment in/out if you want to support ssl */
55 #endif
56
57 #ifdef DO_SSL
58 #include <openssl/ssl.h>
59 #include <openssl/err.h>
60 #else
61 /* declare dummy types so we can define a pointer to them */
62 typedef struct {} SSL;
63 typedef struct {} SSL_CTX;
64 #endif /* DO_SSL */
65
66 /*! SSL support */
67 #define AST_CERTFILE "asterisk.pem"
68
69 enum ast_ssl_flags {
70         /*! Verify certificate when acting as server */
71         AST_SSL_VERIFY_CLIENT = (1 << 0),
72         /*! Don't verify certificate when connecting to a server */
73         AST_SSL_DONT_VERIFY_SERVER = (1 << 1),
74         /*! Don't compare "Common Name" against IP or hostname */
75         AST_SSL_IGNORE_COMMON_NAME = (1 << 2)
76 };
77
78 struct ast_tls_config {
79         int enabled;
80         char *certfile;
81         char *cipher;
82         char *cafile;
83         char *capath;
84         struct ast_flags flags;
85         SSL_CTX *ssl_ctx;
86 };
87
88 /*!
89  * The following code implements a generic mechanism for starting
90  * services on a TCP or TLS socket.
91  * The service is configured in the struct session_args, and
92  * then started by calling server_start(desc) on the descriptor.
93  * server_start() first verifies if an instance of the service is active,
94  * and in case shuts it down. Then, if the service must be started, creates
95  * a socket and a thread in charge of doing the accept().
96  *
97  * The body of the thread is desc->accept_fn(desc), which the user can define
98  * freely. We supply a sample implementation, server_root(), structured as an
99  * infinite loop. At the beginning of each iteration it runs periodic_fn()
100  * if defined (e.g. to perform some cleanup etc.) then issues a poll()
101  * or equivalent with a timeout of 'poll_timeout' milliseconds, and if the
102  * following accept() is successful it creates a thread in charge of
103  * running the session, whose body is desc->worker_fn(). The argument of
104  * worker_fn() is a struct ast_tcptls_session_instance, which contains the address
105  * of the other party, a pointer to desc, the file descriptors (fd) on which
106  * we can do a select/poll (but NOT I/O), and a FILE *on which we can do I/O.
107  * We have both because we want to support plain and SSL sockets, and
108  * going through a FILE * lets us provide the encryption/decryption
109  * on the stream without using an auxiliary thread.
110  */
111
112 /*! \brief
113  * arguments for the accepting thread
114  */
115 struct ast_tcptls_session_args {
116         struct sockaddr_in local_address;
117         struct sockaddr_in old_local_address;
118         struct sockaddr_in remote_address;
119         char hostname[MAXHOSTNAMELEN]; /*!< only necessary for SSL clients so we can compare to common name */
120         struct ast_tls_config *tls_cfg; /*!< points to the SSL configuration if any */
121         int accept_fd;
122         int poll_timeout;
123         pthread_t master;
124         void *(*accept_fn)(void *); /*!< the function in charge of doing the accept */
125         void (*periodic_fn)(void *);/*!< something we may want to run before after select on the accept socket */
126         void *(*worker_fn)(void *); /*!< the function in charge of doing the actual work */
127         const char *name;
128 };
129
130 /*
131  * describes a server instance
132  */
133 struct ast_tcptls_session_instance {
134         FILE *f;    /* fopen/funopen result */
135         int fd;     /* the socket returned by accept() */
136         SSL *ssl;   /* ssl state */
137 /*      iint (*ssl_setup)(SSL *); */
138         int client;
139         struct sockaddr_in remote_address;
140         struct ast_tcptls_session_args *parent;
141         ast_mutex_t lock;
142 };
143
144 #if defined(HAVE_FUNOPEN)
145 #define HOOK_T int
146 #define LEN_T int
147 #else
148 #define HOOK_T ssize_t
149 #define LEN_T size_t
150 #endif
151
152 struct ast_tcptls_session_instance *ast_tcptls_client_start(struct ast_tcptls_session_args *desc);
153
154 void *ast_tcptls_server_root(void *);
155 void ast_tcptls_server_start(struct ast_tcptls_session_args *desc);
156 void ast_tcptls_server_stop(struct ast_tcptls_session_args *desc);
157 int ast_ssl_setup(struct ast_tls_config *cfg);
158
159 HOOK_T ast_tcptls_server_read(struct ast_tcptls_session_instance *ser, void *buf, size_t count);
160 HOOK_T ast_tcptls_server_write(struct ast_tcptls_session_instance *ser, void *buf, size_t count);
161
162 #endif /* _ASTERISK_TCPTLS_H */