Clean up and ensure proper usage of alloca()
[asterisk/asterisk.git] / main / tcptls.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007 - 2008, Digium, Inc.
5  *
6  * Luigi Rizzo (TCP and TLS server code)
7  * Brett Bryant <brettbryant@gmail.com> (updated for client support)
8  *
9  * See http://www.asterisk.org for more information about
10  * the Asterisk project. Please do not directly contact
11  * any of the maintainers of this project for assistance;
12  * the project provides a web site, mailing lists and IRC
13  * channels for your use.
14  *
15  * This program is free software, distributed under the terms of
16  * the GNU General Public License Version 2. See the LICENSE file
17  * at the top of the source tree.
18  */
19
20 /*!
21  * \file
22  * \brief Code to support TCP and TLS server/client
23  *
24  * \author Luigi Rizzo
25  * \author Brett Bryant <brettbryant@gmail.com>
26  */
27
28 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #ifdef HAVE_FCNTL_H
37 #include <fcntl.h>
38 #endif
39
40 #include <signal.h>
41 #include <sys/signal.h>
42
43 #include "asterisk/compat.h"
44 #include "asterisk/tcptls.h"
45 #include "asterisk/http.h"
46 #include "asterisk/utils.h"
47 #include "asterisk/strings.h"
48 #include "asterisk/options.h"
49 #include "asterisk/manager.h"
50 #include "asterisk/astobj2.h"
51
52 /*! \brief
53  * replacement read/write functions for SSL support.
54  * We use wrappers rather than SSL_read/SSL_write directly so
55  * we can put in some debugging.
56  */
57
58 #ifdef DO_SSL
59 static HOOK_T ssl_read(void *cookie, char *buf, LEN_T len)
60 {
61         int i = SSL_read(cookie, buf, len-1);
62 #if 0
63         if (i >= 0) {
64                 buf[i] = '\0';
65         }
66         ast_verb(0, "ssl read size %d returns %d <%s>\n", (int)len, i, buf);
67 #endif
68         return i;
69 }
70
71 static HOOK_T ssl_write(void *cookie, const char *buf, LEN_T len)
72 {
73 #if 0
74         char *s = ast_alloca(len+1);
75
76         strncpy(s, buf, len);
77         s[len] = '\0';
78         ast_verb(0, "ssl write size %d <%s>\n", (int)len, s);
79 #endif
80         return SSL_write(cookie, buf, len);
81 }
82
83 static int ssl_close(void *cookie)
84 {
85         int cookie_fd = SSL_get_fd(cookie);
86         int ret;
87         if (cookie_fd > -1) {
88                 /*
89                  * According to the TLS standard, it is acceptable for an application to only send its shutdown
90                  * alert and then close the underlying connection without waiting for the peer's response (this
91                  * way resources can be saved, as the process can already terminate or serve another connection).
92                  */
93                 if ((ret = SSL_shutdown(cookie)) < 0) {
94                         ast_log(LOG_ERROR, "SSL_shutdown() failed: %d\n", SSL_get_error(cookie, ret));
95                 }
96                 SSL_free(cookie);
97                 /* adding shutdown(2) here has no added benefit */
98                 if (close(cookie_fd)) {
99                         ast_log(LOG_ERROR, "close() failed: %s\n", strerror(errno));
100                 }
101         }
102         return 0;
103 }
104 #endif  /* DO_SSL */
105
106 HOOK_T ast_tcptls_server_read(struct ast_tcptls_session_instance *tcptls_session, void *buf, size_t count)
107 {
108         if (tcptls_session->fd == -1) {
109                 ast_log(LOG_ERROR, "server_read called with an fd of -1\n");
110                 errno = EIO;
111                 return -1;
112         }
113
114 #ifdef DO_SSL
115         if (tcptls_session->ssl) {
116                 return ssl_read(tcptls_session->ssl, buf, count);
117         }
118 #endif
119         return read(tcptls_session->fd, buf, count);
120 }
121
122 HOOK_T ast_tcptls_server_write(struct ast_tcptls_session_instance *tcptls_session, const void *buf, size_t count)
123 {
124         if (tcptls_session->fd == -1) {
125                 ast_log(LOG_ERROR, "server_write called with an fd of -1\n");
126                 errno = EIO;
127                 return -1;
128         }
129
130 #ifdef DO_SSL
131         if (tcptls_session->ssl) {
132                 return ssl_write(tcptls_session->ssl, buf, count);
133         }
134 #endif
135         return write(tcptls_session->fd, buf, count);
136 }
137
138 /*! \brief
139 * creates a FILE * from the fd passed by the accept thread.
140 * This operation is potentially expensive (certificate verification),
141 * so we do it in the child thread context.
142 *
143 * \note must decrement ref count before returning NULL on error
144 */
145 static void *handle_tcptls_connection(void *data)
146 {
147         struct ast_tcptls_session_instance *tcptls_session = data;
148 #ifdef DO_SSL
149         int (*ssl_setup)(SSL *) = (tcptls_session->client) ? SSL_connect : SSL_accept;
150         int ret;
151         char err[256];
152 #endif
153
154         /*
155         * open a FILE * as appropriate.
156         */
157         if (!tcptls_session->parent->tls_cfg) {
158                 if ((tcptls_session->f = fdopen(tcptls_session->fd, "w+"))) {
159                         if(setvbuf(tcptls_session->f, NULL, _IONBF, 0)) {
160                                 ast_tcptls_close_session_file(tcptls_session);
161                         }
162                 }
163         }
164 #ifdef DO_SSL
165         else if ( (tcptls_session->ssl = SSL_new(tcptls_session->parent->tls_cfg->ssl_ctx)) ) {
166                 SSL_set_fd(tcptls_session->ssl, tcptls_session->fd);
167                 if ((ret = ssl_setup(tcptls_session->ssl)) <= 0) {
168                         ast_verb(2, "Problem setting up ssl connection: %s\n", ERR_error_string(ERR_get_error(), err));
169                 } else {
170 #if defined(HAVE_FUNOPEN)       /* the BSD interface */
171                         tcptls_session->f = funopen(tcptls_session->ssl, ssl_read, ssl_write, NULL, ssl_close);
172
173 #elif defined(HAVE_FOPENCOOKIE) /* the glibc/linux interface */
174                         static const cookie_io_functions_t cookie_funcs = {
175                                 ssl_read, ssl_write, NULL, ssl_close
176                         };
177                         tcptls_session->f = fopencookie(tcptls_session->ssl, "w+", cookie_funcs);
178 #else
179                         /* could add other methods here */
180                         ast_debug(2, "no tcptls_session->f methods attempted!\n");
181 #endif
182                         if ((tcptls_session->client && !ast_test_flag(&tcptls_session->parent->tls_cfg->flags, AST_SSL_DONT_VERIFY_SERVER))
183                                 || (!tcptls_session->client && ast_test_flag(&tcptls_session->parent->tls_cfg->flags, AST_SSL_VERIFY_CLIENT))) {
184                                 X509 *peer;
185                                 long res;
186                                 peer = SSL_get_peer_certificate(tcptls_session->ssl);
187                                 if (!peer) {
188                                         ast_log(LOG_WARNING, "No peer SSL certificate\n");
189                                 }
190                                 res = SSL_get_verify_result(tcptls_session->ssl);
191                                 if (res != X509_V_OK) {
192                                         ast_log(LOG_ERROR, "Certificate did not verify: %s\n", X509_verify_cert_error_string(res));
193                                 }
194                                 if (!ast_test_flag(&tcptls_session->parent->tls_cfg->flags, AST_SSL_IGNORE_COMMON_NAME)) {
195                                         ASN1_STRING *str;
196                                         unsigned char *str2;
197                                         X509_NAME *name = X509_get_subject_name(peer);
198                                         int pos = -1;
199                                         int found = 0;
200
201                                         for (;;) {
202                                                 /* Walk the certificate to check all available "Common Name" */
203                                                 /* XXX Probably should do a gethostbyname on the hostname and compare that as well */
204                                                 pos = X509_NAME_get_index_by_NID(name, NID_commonName, pos);
205                                                 if (pos < 0) {
206                                                         break;
207                                                 }
208                                                 str = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name, pos));
209                                                 ASN1_STRING_to_UTF8(&str2, str);
210                                                 if (str2) {
211                                                         if (!strcasecmp(tcptls_session->parent->hostname, (char *) str2)) {
212                                                                 found = 1;
213                                                         }
214                                                         ast_debug(3, "SSL Common Name compare s1='%s' s2='%s'\n", tcptls_session->parent->hostname, str2);
215                                                         OPENSSL_free(str2);
216                                                 }
217                                                 if (found) {
218                                                         break;
219                                                 }
220                                         }
221                                         if (!found) {
222                                                 ast_log(LOG_ERROR, "Certificate common name did not match (%s)\n", tcptls_session->parent->hostname);
223                                                 if (peer) {
224                                                         X509_free(peer);
225                                                 }
226                                                 ast_tcptls_close_session_file(tcptls_session);
227                                                 ao2_ref(tcptls_session, -1);
228                                                 return NULL;
229                                         }
230                                 }
231                                 if (peer) {
232                                         X509_free(peer);
233                                 }
234                         }
235                 }
236                 if (!tcptls_session->f) {       /* no success opening descriptor stacking */
237                         SSL_free(tcptls_session->ssl);
238                 }
239         }
240 #endif /* DO_SSL */
241
242         if (!tcptls_session->f) {
243                 ast_tcptls_close_session_file(tcptls_session);
244                 ast_log(LOG_WARNING, "FILE * open failed!\n");
245 #ifndef DO_SSL
246                 if (tcptls_session->parent->tls_cfg) {
247                         ast_log(LOG_WARNING, "Attempted a TLS connection without OpenSSL support. This will not work!\n");
248                 }
249 #endif
250                 ao2_ref(tcptls_session, -1);
251                 return NULL;
252         }
253
254         if (tcptls_session->parent->worker_fn) {
255                 return tcptls_session->parent->worker_fn(tcptls_session);
256         } else {
257                 return tcptls_session;
258         }
259 }
260
261 void *ast_tcptls_server_root(void *data)
262 {
263         struct ast_tcptls_session_args *desc = data;
264         int fd;
265         struct ast_sockaddr addr;
266         struct ast_tcptls_session_instance *tcptls_session;
267         pthread_t launched;
268
269         for (;;) {
270                 int i, flags;
271
272                 if (desc->periodic_fn) {
273                         desc->periodic_fn(desc);
274                 }
275                 i = ast_wait_for_input(desc->accept_fd, desc->poll_timeout);
276                 if (i <= 0) {
277                         continue;
278                 }
279                 fd = ast_accept(desc->accept_fd, &addr);
280                 if (fd < 0) {
281                         if ((errno != EAGAIN) && (errno != EINTR)) {
282                                 ast_log(LOG_WARNING, "Accept failed: %s\n", strerror(errno));
283                         }
284                         continue;
285                 }
286                 tcptls_session = ao2_alloc(sizeof(*tcptls_session), NULL);
287                 if (!tcptls_session) {
288                         ast_log(LOG_WARNING, "No memory for new session: %s\n", strerror(errno));
289                         if (close(fd)) {
290                                 ast_log(LOG_ERROR, "close() failed: %s\n", strerror(errno));
291                         }
292                         continue;
293                 }
294
295                 flags = fcntl(fd, F_GETFL);
296                 fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
297                 tcptls_session->fd = fd;
298                 tcptls_session->parent = desc;
299                 ast_sockaddr_copy(&tcptls_session->remote_address, &addr);
300
301                 tcptls_session->client = 0;
302
303                 /* This thread is now the only place that controls the single ref to tcptls_session */
304                 if (ast_pthread_create_detached_background(&launched, NULL, handle_tcptls_connection, tcptls_session)) {
305                         ast_log(LOG_WARNING, "Unable to launch helper thread: %s\n", strerror(errno));
306                         ast_tcptls_close_session_file(tcptls_session);
307                         ao2_ref(tcptls_session, -1);
308                 }
309         }
310         return NULL;
311 }
312
313 static int __ssl_setup(struct ast_tls_config *cfg, int client)
314 {
315 #ifndef DO_SSL
316         cfg->enabled = 0;
317         return 0;
318 #else
319         if (!cfg->enabled) {
320                 return 0;
321         }
322
323         SSL_load_error_strings();
324         SSLeay_add_ssl_algorithms();
325
326         /* Get rid of an old SSL_CTX since we're about to
327          * allocate a new one
328          */
329         if (cfg->ssl_ctx) {
330                 SSL_CTX_free(cfg->ssl_ctx);
331                 cfg->ssl_ctx = NULL;
332         }
333
334         if (client) {
335 #ifndef OPENSSL_NO_SSL2
336                 if (ast_test_flag(&cfg->flags, AST_SSL_SSLV2_CLIENT)) {
337                         cfg->ssl_ctx = SSL_CTX_new(SSLv2_client_method());
338                 } else
339 #endif
340                 if (ast_test_flag(&cfg->flags, AST_SSL_SSLV3_CLIENT)) {
341                         cfg->ssl_ctx = SSL_CTX_new(SSLv3_client_method());
342                 } else if (ast_test_flag(&cfg->flags, AST_SSL_TLSV1_CLIENT)) {
343                         cfg->ssl_ctx = SSL_CTX_new(TLSv1_client_method());
344                 } else {
345                         /* SSLv23_client_method() sends SSLv2, this was the original
346                          * default for ssl clients before the option was given to
347                          * pick what protocol a client should use.  In order not
348                          * to break expected behavior it remains the default. */
349                         cfg->ssl_ctx = SSL_CTX_new(SSLv23_client_method());
350                 }
351         } else {
352                 /* SSLv23_server_method() supports TLSv1, SSLv2, and SSLv3 inbound connections. */
353                 cfg->ssl_ctx = SSL_CTX_new(SSLv23_server_method());
354         }
355
356         if (!cfg->ssl_ctx) {
357                 ast_debug(1, "Sorry, SSL_CTX_new call returned null...\n");
358                 cfg->enabled = 0;
359                 return 0;
360         }
361         if (!ast_strlen_zero(cfg->certfile)) {
362                 char *tmpprivate = ast_strlen_zero(cfg->pvtfile) ? cfg->certfile : cfg->pvtfile;
363                 if (SSL_CTX_use_certificate_file(cfg->ssl_ctx, cfg->certfile, SSL_FILETYPE_PEM) == 0) {
364                         if (!client) {
365                                 /* Clients don't need a certificate, but if its setup we can use it */
366                                 ast_verb(0, "SSL error loading cert file. <%s>\n", cfg->certfile);
367                                 sleep(2);
368                                 cfg->enabled = 0;
369                                 SSL_CTX_free(cfg->ssl_ctx);
370                                 cfg->ssl_ctx = NULL;
371                                 return 0;
372                         }
373                 }
374                 if ((SSL_CTX_use_PrivateKey_file(cfg->ssl_ctx, tmpprivate, SSL_FILETYPE_PEM) == 0) || (SSL_CTX_check_private_key(cfg->ssl_ctx) == 0 )) {
375                         if (!client) {
376                                 /* Clients don't need a private key, but if its setup we can use it */
377                                 ast_verb(0, "SSL error loading private key file. <%s>\n", tmpprivate);
378                                 sleep(2);
379                                 cfg->enabled = 0;
380                                 SSL_CTX_free(cfg->ssl_ctx);
381                                 cfg->ssl_ctx = NULL;
382                                 return 0;
383                         }
384                 }
385         }
386         if (!ast_strlen_zero(cfg->cipher)) {
387                 if (SSL_CTX_set_cipher_list(cfg->ssl_ctx, cfg->cipher) == 0 ) {
388                         if (!client) {
389                                 ast_verb(0, "SSL cipher error <%s>\n", cfg->cipher);
390                                 sleep(2);
391                                 cfg->enabled = 0;
392                                 SSL_CTX_free(cfg->ssl_ctx);
393                                 cfg->ssl_ctx = NULL;
394                                 return 0;
395                         }
396                 }
397         }
398         if (!ast_strlen_zero(cfg->cafile) || !ast_strlen_zero(cfg->capath)) {
399                 if (SSL_CTX_load_verify_locations(cfg->ssl_ctx, S_OR(cfg->cafile, NULL), S_OR(cfg->capath,NULL)) == 0) {
400                         ast_verb(0, "SSL CA file(%s)/path(%s) error\n", cfg->cafile, cfg->capath);
401                 }
402         }
403
404         ast_verb(0, "SSL certificate ok\n");
405         return 1;
406 #endif
407 }
408
409 int ast_ssl_setup(struct ast_tls_config *cfg)
410 {
411         return __ssl_setup(cfg, 0);
412 }
413
414 void ast_ssl_teardown(struct ast_tls_config *cfg)
415 {
416 #ifdef DO_SSL
417         if (cfg->ssl_ctx) {
418                 SSL_CTX_free(cfg->ssl_ctx);
419                 cfg->ssl_ctx = NULL;
420         }
421 #endif
422 }
423
424 struct ast_tcptls_session_instance *ast_tcptls_client_start(struct ast_tcptls_session_instance *tcptls_session)
425 {
426         struct ast_tcptls_session_args *desc;
427         int flags;
428
429         if (!(desc = tcptls_session->parent)) {
430                 goto client_start_error;
431         }
432
433         if (ast_connect(desc->accept_fd, &desc->remote_address)) {
434                 ast_log(LOG_ERROR, "Unable to connect %s to %s: %s\n",
435                         desc->name,
436                         ast_sockaddr_stringify(&desc->remote_address),
437                         strerror(errno));
438                 goto client_start_error;
439         }
440
441         flags = fcntl(desc->accept_fd, F_GETFL);
442         fcntl(desc->accept_fd, F_SETFL, flags & ~O_NONBLOCK);
443
444         if (desc->tls_cfg) {
445                 desc->tls_cfg->enabled = 1;
446                 __ssl_setup(desc->tls_cfg, 1);
447         }
448
449         return handle_tcptls_connection(tcptls_session);
450
451 client_start_error:
452         if (desc) {
453                 close(desc->accept_fd);
454                 desc->accept_fd = -1;
455         }
456         ao2_ref(tcptls_session, -1);
457         return NULL;
458
459 }
460
461 struct ast_tcptls_session_instance *ast_tcptls_client_create(struct ast_tcptls_session_args *desc)
462 {
463         int x = 1;
464         struct ast_tcptls_session_instance *tcptls_session = NULL;
465
466         /* Do nothing if nothing has changed */
467         if (!ast_sockaddr_cmp(&desc->old_address, &desc->remote_address)) {
468                 ast_debug(1, "Nothing changed in %s\n", desc->name);
469                 return NULL;
470         }
471
472         /* If we return early, there is no connection */
473         ast_sockaddr_setnull(&desc->old_address);
474
475         if (desc->accept_fd != -1) {
476                 close(desc->accept_fd);
477         }
478
479         desc->accept_fd = socket(ast_sockaddr_is_ipv6(&desc->remote_address) ?
480                                  AF_INET6 : AF_INET, SOCK_STREAM, IPPROTO_TCP);
481         if (desc->accept_fd < 0) {
482                 ast_log(LOG_WARNING, "Unable to allocate socket for %s: %s\n",
483                         desc->name, strerror(errno));
484                 return NULL;
485         }
486
487         /* if a local address was specified, bind to it so the connection will
488            originate from the desired address */
489         if (!ast_sockaddr_isnull(&desc->local_address)) {
490                 setsockopt(desc->accept_fd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
491                 if (ast_bind(desc->accept_fd, &desc->local_address)) {
492                         ast_log(LOG_ERROR, "Unable to bind %s to %s: %s\n",
493                                 desc->name,
494                                 ast_sockaddr_stringify(&desc->local_address),
495                                 strerror(errno));
496                         goto error;
497                 }
498         }
499
500         if (!(tcptls_session = ao2_alloc(sizeof(*tcptls_session), NULL))) {
501                 goto error;
502         }
503
504         tcptls_session->client = 1;
505         tcptls_session->fd = desc->accept_fd;
506         tcptls_session->parent = desc;
507         tcptls_session->parent->worker_fn = NULL;
508         ast_sockaddr_copy(&tcptls_session->remote_address,
509                           &desc->remote_address);
510
511         /* Set current info */
512         ast_sockaddr_copy(&desc->old_address, &desc->remote_address);
513         return tcptls_session;
514
515 error:
516         close(desc->accept_fd);
517         desc->accept_fd = -1;
518         if (tcptls_session) {
519                 ao2_ref(tcptls_session, -1);
520         }
521         return NULL;
522 }
523
524 void ast_tcptls_server_start(struct ast_tcptls_session_args *desc)
525 {
526         int flags;
527         int x = 1;
528
529         /* Do nothing if nothing has changed */
530         if (!ast_sockaddr_cmp(&desc->old_address, &desc->local_address)) {
531                 ast_debug(1, "Nothing changed in %s\n", desc->name);
532                 return;
533         }
534
535         /* If we return early, there is no one listening */
536         ast_sockaddr_setnull(&desc->old_address);
537
538         /* Shutdown a running server if there is one */
539         if (desc->master != AST_PTHREADT_NULL) {
540                 pthread_cancel(desc->master);
541                 pthread_kill(desc->master, SIGURG);
542                 pthread_join(desc->master, NULL);
543         }
544
545         if (desc->accept_fd != -1) {
546                 close(desc->accept_fd);
547         }
548
549         /* If there's no new server, stop here */
550         if (ast_sockaddr_isnull(&desc->local_address)) {
551                 ast_debug(2, "Server disabled:  %s\n", desc->name);
552                 return;
553         }
554
555         desc->accept_fd = socket(ast_sockaddr_is_ipv6(&desc->local_address) ?
556                                  AF_INET6 : AF_INET, SOCK_STREAM, 0);
557         if (desc->accept_fd < 0) {
558                 ast_log(LOG_ERROR, "Unable to allocate socket for %s: %s\n", desc->name, strerror(errno));
559                 return;
560         }
561
562         setsockopt(desc->accept_fd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
563         if (ast_bind(desc->accept_fd, &desc->local_address)) {
564                 ast_log(LOG_ERROR, "Unable to bind %s to %s: %s\n",
565                         desc->name,
566                         ast_sockaddr_stringify(&desc->local_address),
567                         strerror(errno));
568                 goto error;
569         }
570         if (listen(desc->accept_fd, 10)) {
571                 ast_log(LOG_ERROR, "Unable to listen for %s!\n", desc->name);
572                 goto error;
573         }
574         flags = fcntl(desc->accept_fd, F_GETFL);
575         fcntl(desc->accept_fd, F_SETFL, flags | O_NONBLOCK);
576         if (ast_pthread_create_background(&desc->master, NULL, desc->accept_fn, desc)) {
577                 ast_log(LOG_ERROR, "Unable to launch thread for %s on %s: %s\n",
578                         desc->name,
579                         ast_sockaddr_stringify(&desc->local_address),
580                         strerror(errno));
581                 goto error;
582         }
583
584         /* Set current info */
585         ast_sockaddr_copy(&desc->old_address, &desc->local_address);
586
587         return;
588
589 error:
590         close(desc->accept_fd);
591         desc->accept_fd = -1;
592 }
593
594 void ast_tcptls_close_session_file(struct ast_tcptls_session_instance *tcptls_session)
595 {
596         if (tcptls_session->f) {
597                 if (fclose(tcptls_session->f)) {
598                         ast_log(LOG_ERROR, "fclose() failed: %s\n", strerror(errno));
599                 }
600                 tcptls_session->f = NULL;
601                 tcptls_session->fd = -1;
602         } else if (tcptls_session->fd != -1) {
603                 if (close(tcptls_session->fd)) {
604                         ast_log(LOG_ERROR, "close() failed: %s\n", strerror(errno));
605                 }
606                 tcptls_session->fd = -1;
607         } else {
608                 ast_log(LOG_ERROR, "ast_tcptls_close_session_file invoked on session instance without file or file descriptor\n");
609         }
610 }
611
612 void ast_tcptls_server_stop(struct ast_tcptls_session_args *desc)
613 {
614         if (desc->master != AST_PTHREADT_NULL) {
615                 pthread_cancel(desc->master);
616                 pthread_kill(desc->master, SIGURG);
617                 pthread_join(desc->master, NULL);
618                 desc->master = AST_PTHREADT_NULL;
619         }
620         if (desc->accept_fd != -1) {
621                 close(desc->accept_fd);
622         }
623         desc->accept_fd = -1;
624         ast_debug(2, "Stopped server :: %s\n", desc->name);
625 }
626
627 int ast_tls_read_conf(struct ast_tls_config *tls_cfg, struct ast_tcptls_session_args *tls_desc, const char *varname, const char *value)
628 {
629         if (!strcasecmp(varname, "tlsenable") || !strcasecmp(varname, "sslenable")) {
630                 tls_cfg->enabled = ast_true(value) ? 1 : 0;
631         } else if (!strcasecmp(varname, "tlscertfile") || !strcasecmp(varname, "sslcert") || !strcasecmp(varname, "tlscert")) {
632                 ast_free(tls_cfg->certfile);
633                 tls_cfg->certfile = ast_strdup(value);
634         } else if (!strcasecmp(varname, "tlsprivatekey") || !strcasecmp(varname, "sslprivatekey")) {
635                 ast_free(tls_cfg->pvtfile);
636                 tls_cfg->pvtfile = ast_strdup(value);
637         } else if (!strcasecmp(varname, "tlscipher") || !strcasecmp(varname, "sslcipher")) {
638                 ast_free(tls_cfg->cipher);
639                 tls_cfg->cipher = ast_strdup(value);
640         } else if (!strcasecmp(varname, "tlscafile")) {
641                 ast_free(tls_cfg->cafile);
642                 tls_cfg->cafile = ast_strdup(value);
643         } else if (!strcasecmp(varname, "tlscapath") || !strcasecmp(varname, "tlscadir")) {
644                 ast_free(tls_cfg->capath);
645                 tls_cfg->capath = ast_strdup(value);
646         } else if (!strcasecmp(varname, "tlsverifyclient")) {
647                 ast_set2_flag(&tls_cfg->flags, ast_true(value), AST_SSL_VERIFY_CLIENT);
648         } else if (!strcasecmp(varname, "tlsdontverifyserver")) {
649                 ast_set2_flag(&tls_cfg->flags, ast_true(value), AST_SSL_DONT_VERIFY_SERVER);
650         } else if (!strcasecmp(varname, "tlsbindaddr") || !strcasecmp(varname, "sslbindaddr")) {
651                 if (ast_parse_arg(value, PARSE_ADDR, &tls_desc->local_address))
652                         ast_log(LOG_WARNING, "Invalid %s '%s'\n", varname, value);
653         } else if (!strcasecmp(varname, "tlsclientmethod") || !strcasecmp(varname, "sslclientmethod")) {
654                 if (!strcasecmp(value, "tlsv1")) {
655                         ast_set_flag(&tls_cfg->flags, AST_SSL_TLSV1_CLIENT);
656                         ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV3_CLIENT);
657                         ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV2_CLIENT);
658                 } else if (!strcasecmp(value, "sslv3")) {
659                         ast_set_flag(&tls_cfg->flags, AST_SSL_SSLV3_CLIENT);
660                         ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV2_CLIENT);
661                         ast_clear_flag(&tls_cfg->flags, AST_SSL_TLSV1_CLIENT);
662                 } else if (!strcasecmp(value, "sslv2")) {
663                         ast_set_flag(&tls_cfg->flags, AST_SSL_SSLV2_CLIENT);
664                         ast_clear_flag(&tls_cfg->flags, AST_SSL_TLSV1_CLIENT);
665                         ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV3_CLIENT);
666                 }
667         } else {
668                 return -1;
669         }
670
671         return 0;
672 }