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