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