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