Add IPv6 to Asterisk.
[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 ast_sockaddr addr;
239         struct ast_tcptls_session_instance *tcptls_session;
240         pthread_t launched;
241
242         for (;;) {
243                 int i, flags;
244
245                 if (desc->periodic_fn)
246                         desc->periodic_fn(desc);
247                 i = ast_wait_for_input(desc->accept_fd, desc->poll_timeout);
248                 if (i <= 0)
249                         continue;
250                 fd = ast_accept(desc->accept_fd, &addr);
251                 if (fd < 0) {
252                         if ((errno != EAGAIN) && (errno != EINTR))
253                                 ast_log(LOG_WARNING, "Accept failed: %s\n", strerror(errno));
254                         continue;
255                 }
256                 tcptls_session = ao2_alloc(sizeof(*tcptls_session), session_instance_destructor);
257                 if (!tcptls_session) {
258                         ast_log(LOG_WARNING, "No memory for new session: %s\n", strerror(errno));
259                         close(fd);
260                         continue;
261                 }
262
263                 ast_mutex_init(&tcptls_session->lock);
264
265                 flags = fcntl(fd, F_GETFL);
266                 fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
267                 tcptls_session->fd = fd;
268                 tcptls_session->parent = desc;
269                 ast_sockaddr_copy(&tcptls_session->remote_address, &addr);
270
271                 tcptls_session->client = 0;
272
273                 /* This thread is now the only place that controls the single ref to tcptls_session */
274                 if (ast_pthread_create_detached_background(&launched, NULL, handle_tcptls_connection, tcptls_session)) {
275                         ast_log(LOG_WARNING, "Unable to launch helper thread: %s\n", strerror(errno));
276                         close(tcptls_session->fd);
277                         ao2_ref(tcptls_session, -1);
278                 }
279         }
280         return NULL;
281 }
282
283 static int __ssl_setup(struct ast_tls_config *cfg, int client)
284 {
285 #ifndef DO_SSL
286         cfg->enabled = 0;
287         return 0;
288 #else
289         if (!cfg->enabled)
290                 return 0;
291
292         SSL_load_error_strings();
293         SSLeay_add_ssl_algorithms();
294
295         if (client) {
296                 if (ast_test_flag(&cfg->flags, AST_SSL_SSLV2_CLIENT)) {
297                         cfg->ssl_ctx = SSL_CTX_new(SSLv2_client_method());
298                 } else if (ast_test_flag(&cfg->flags, AST_SSL_SSLV3_CLIENT)) {
299                         cfg->ssl_ctx = SSL_CTX_new(SSLv3_client_method());
300                 } else if (ast_test_flag(&cfg->flags, AST_SSL_TLSV1_CLIENT)) {
301                         cfg->ssl_ctx = SSL_CTX_new(TLSv1_client_method());
302                 } else {
303                         /* SSLv23_client_method() sends SSLv2, this was the original
304                          * default for ssl clients before the option was given to
305                          * pick what protocol a client should use.  In order not
306                          * to break expected behavior it remains the default. */
307                         cfg->ssl_ctx = SSL_CTX_new(SSLv23_client_method());
308                 }
309         } else {
310                 /* SSLv23_server_method() supports TLSv1, SSLv2, and SSLv3 inbound connections. */
311                 cfg->ssl_ctx = SSL_CTX_new(SSLv23_server_method());
312         }
313
314         if (!cfg->ssl_ctx) {
315                 ast_debug(1, "Sorry, SSL_CTX_new call returned null...\n");
316                 cfg->enabled = 0;
317                 return 0;
318         }
319         if (!ast_strlen_zero(cfg->certfile)) {
320                 char *tmpprivate = ast_strlen_zero(cfg->pvtfile) ? cfg->certfile : cfg->pvtfile;
321                 if (SSL_CTX_use_certificate_file(cfg->ssl_ctx, cfg->certfile, SSL_FILETYPE_PEM) == 0) {
322                         if (!client) {
323                                 /* Clients don't need a certificate, but if its setup we can use it */
324                                 ast_verb(0, "SSL error loading cert file. <%s>", cfg->certfile);
325                                 sleep(2);
326                                 cfg->enabled = 0;
327                                 return 0;
328                         }
329                 }
330                 if ((SSL_CTX_use_PrivateKey_file(cfg->ssl_ctx, tmpprivate, SSL_FILETYPE_PEM) == 0) || (SSL_CTX_check_private_key(cfg->ssl_ctx) == 0 )) {
331                         if (!client) {
332                                 /* Clients don't need a private key, but if its setup we can use it */
333                                 ast_verb(0, "SSL error loading private key file. <%s>", tmpprivate);
334                                 sleep(2);
335                                 cfg->enabled = 0;
336                                 return 0;
337                         }
338                 }
339         }
340         if (!ast_strlen_zero(cfg->cipher)) {
341                 if (SSL_CTX_set_cipher_list(cfg->ssl_ctx, cfg->cipher) == 0 ) {
342                         if (!client) {
343                                 ast_verb(0, "SSL cipher error <%s>", cfg->cipher);
344                                 sleep(2);
345                                 cfg->enabled = 0;
346                                 return 0;
347                         }
348                 }
349         }
350         if (!ast_strlen_zero(cfg->cafile) || !ast_strlen_zero(cfg->capath)) {
351                 if (SSL_CTX_load_verify_locations(cfg->ssl_ctx, S_OR(cfg->cafile, NULL), S_OR(cfg->capath,NULL)) == 0)
352                         ast_verb(0, "SSL CA file(%s)/path(%s) error\n", cfg->cafile, cfg->capath);
353         }
354
355         ast_verb(0, "SSL certificate ok\n");
356         return 1;
357 #endif
358 }
359
360 int ast_ssl_setup(struct ast_tls_config *cfg)
361 {
362         return __ssl_setup(cfg, 0);
363 }
364
365 struct ast_tcptls_session_instance *ast_tcptls_client_start(struct ast_tcptls_session_instance *tcptls_session)
366 {
367         struct ast_tcptls_session_args *desc;
368         int flags;
369
370         if (!(desc = tcptls_session->parent)) {
371                 goto client_start_error;
372         }
373
374         if (ast_connect(desc->accept_fd, &desc->remote_address)) {
375                 ast_log(LOG_ERROR, "Unable to connect %s to %s: %s\n",
376                         desc->name,
377                         ast_sockaddr_stringify(&desc->remote_address),
378                         strerror(errno));
379                 goto client_start_error;
380         }
381
382         flags = fcntl(desc->accept_fd, F_GETFL);
383         fcntl(desc->accept_fd, F_SETFL, flags & ~O_NONBLOCK);
384
385         if (desc->tls_cfg) {
386                 desc->tls_cfg->enabled = 1;
387                 __ssl_setup(desc->tls_cfg, 1);
388         }
389
390         return handle_tcptls_connection(tcptls_session);
391
392 client_start_error:
393         close(desc->accept_fd);
394         desc->accept_fd = -1;
395         if (tcptls_session) {
396                 ao2_ref(tcptls_session, -1);
397         }
398         return NULL;
399
400 }
401
402 struct ast_tcptls_session_instance *ast_tcptls_client_create(struct ast_tcptls_session_args *desc)
403 {
404         int x = 1;
405         struct ast_tcptls_session_instance *tcptls_session = NULL;
406
407         /* Do nothing if nothing has changed */
408         if (!ast_sockaddr_cmp(&desc->old_address, &desc->remote_address)) {
409                 ast_debug(1, "Nothing changed in %s\n", desc->name);
410                 return NULL;
411         }
412
413         ast_sockaddr_copy(&desc->old_address, &desc->remote_address);
414
415         if (desc->accept_fd != -1)
416                 close(desc->accept_fd);
417
418         desc->accept_fd = socket(ast_sockaddr_is_ipv6(&desc->remote_address) ?
419                                  AF_INET6 : AF_INET, SOCK_STREAM, IPPROTO_TCP);
420         if (desc->accept_fd < 0) {
421                 ast_log(LOG_WARNING, "Unable to allocate socket for %s: %s\n",
422                         desc->name, strerror(errno));
423                 return NULL;
424         }
425
426         /* if a local address was specified, bind to it so the connection will
427            originate from the desired address */
428         if (!ast_sockaddr_isnull(&desc->local_address)) {
429                 setsockopt(desc->accept_fd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
430                 if (ast_bind(desc->accept_fd, &desc->local_address)) {
431                         ast_log(LOG_ERROR, "Unable to bind %s to %s: %s\n",
432                                 desc->name,
433                                 ast_sockaddr_stringify(&desc->local_address),
434                                 strerror(errno));
435                         goto error;
436                 }
437         }
438
439         if (!(tcptls_session = ao2_alloc(sizeof(*tcptls_session), session_instance_destructor)))
440                 goto error;
441
442         ast_mutex_init(&tcptls_session->lock);
443         tcptls_session->client = 1;
444         tcptls_session->fd = desc->accept_fd;
445         tcptls_session->parent = desc;
446         tcptls_session->parent->worker_fn = NULL;
447         ast_sockaddr_copy(&tcptls_session->remote_address,
448                           &desc->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 (!ast_sockaddr_cmp(&desc->old_address, &desc->local_address)) {
467                 ast_debug(1, "Nothing changed in %s\n", desc->name);
468                 return;
469         }
470
471         ast_sockaddr_copy(&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 (ast_sockaddr_isnull(&desc->local_address)) {
485                 ast_debug(2, "Server disabled:  %s\n", desc->name);
486                 return;
487         }
488
489         desc->accept_fd = socket(ast_sockaddr_is_ipv6(&desc->local_address) ?
490                                  AF_INET6 : AF_INET, SOCK_STREAM, 0);
491         if (desc->accept_fd < 0) {
492                 ast_log(LOG_ERROR, "Unable to allocate socket for %s: %s\n", desc->name, strerror(errno));
493                 return;
494         }
495
496         setsockopt(desc->accept_fd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
497         if (ast_bind(desc->accept_fd, &desc->local_address)) {
498                 ast_log(LOG_ERROR, "Unable to bind %s to %s: %s\n",
499                         desc->name,
500                         ast_sockaddr_stringify(&desc->local_address),
501                         strerror(errno));
502                 goto error;
503         }
504         if (listen(desc->accept_fd, 10)) {
505                 ast_log(LOG_ERROR, "Unable to listen for %s!\n", desc->name);
506                 goto error;
507         }
508         flags = fcntl(desc->accept_fd, F_GETFL);
509         fcntl(desc->accept_fd, F_SETFL, flags | O_NONBLOCK);
510         if (ast_pthread_create_background(&desc->master, NULL, desc->accept_fn, desc)) {
511                 ast_log(LOG_ERROR, "Unable to launch thread for %s on %s: %s\n",
512                         desc->name,
513                         ast_sockaddr_stringify(&desc->local_address),
514                         strerror(errno));
515                 goto error;
516         }
517         return;
518
519 error:
520         close(desc->accept_fd);
521         desc->accept_fd = -1;
522 }
523
524 void ast_tcptls_server_stop(struct ast_tcptls_session_args *desc)
525 {
526         if (desc->master != AST_PTHREADT_NULL) {
527                 pthread_cancel(desc->master);
528                 pthread_kill(desc->master, SIGURG);
529                 pthread_join(desc->master, NULL);
530         }
531         if (desc->accept_fd != -1)
532                 close(desc->accept_fd);
533         desc->accept_fd = -1;
534         ast_debug(2, "Stopped server :: %s\n", desc->name);
535 }
536
537 int ast_tls_read_conf(struct ast_tls_config *tls_cfg, struct ast_tcptls_session_args *tls_desc, const char *varname, const char *value)
538 {
539         if (!strcasecmp(varname, "tlsenable") || !strcasecmp(varname, "sslenable")) {
540                 tls_cfg->enabled = ast_true(value) ? 1 : 0;
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_ADDR, &tls_desc->local_address))
562                         ast_log(LOG_WARNING, "Invalid %s '%s'\n", varname, value);
563         } else if (!strcasecmp(varname, "tlsclientmethod") || !strcasecmp(varname, "sslclientmethod")) {
564                 if (!strcasecmp(value, "tlsv1")) {
565                         ast_set_flag(&tls_cfg->flags, AST_SSL_TLSV1_CLIENT);
566                         ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV3_CLIENT);
567                         ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV2_CLIENT);
568                 } else if (!strcasecmp(value, "sslv3")) {
569                         ast_set_flag(&tls_cfg->flags, AST_SSL_SSLV3_CLIENT);
570                         ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV2_CLIENT);
571                         ast_clear_flag(&tls_cfg->flags, AST_SSL_TLSV1_CLIENT);
572                 } else if (!strcasecmp(value, "sslv2")) {
573                         ast_set_flag(&tls_cfg->flags, AST_SSL_SSLV2_CLIENT);
574                         ast_clear_flag(&tls_cfg->flags, AST_SSL_TLSV1_CLIENT);
575                         ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV3_CLIENT);
576                 }
577         } else {
578                 return -1;
579         }
580
581         return 0;
582 }