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