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