fixes tcptls_session memory leak caused by ref count error
[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_tls_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                                                 fclose(tcptls_session->f);
201                                                 ao2_ref(tcptls_session, -1);
202                                                 return NULL;
203                                         }
204                                 }
205                                 if (peer)
206                                         X509_free(peer);
207                         }
208                 }
209                 if (!tcptls_session->f) /* no success opening descriptor stacking */
210                         SSL_free(tcptls_session->ssl);
211    }
212 #endif /* DO_SSL */
213
214         if (!tcptls_session->f) {
215                 close(tcptls_session->fd);
216                 ast_log(LOG_WARNING, "FILE * open failed!\n");
217                 ao2_ref(tcptls_session, -1);
218                 return NULL;
219         }
220
221         if (tcptls_session && tcptls_session->parent->worker_fn)
222                 return tcptls_session->parent->worker_fn(tcptls_session);
223         else
224                 return tcptls_session;
225 }
226
227 void *ast_tcptls_server_root(void *data)
228 {
229         struct ast_tcptls_session_args *desc = data;
230         int fd;
231         struct sockaddr_in sin;
232         socklen_t sinlen;
233         struct ast_tcptls_session_instance *tcptls_session;
234         pthread_t launched;
235
236         for (;;) {
237                 int i, flags;
238
239                 if (desc->periodic_fn)
240                         desc->periodic_fn(desc);
241                 i = ast_wait_for_input(desc->accept_fd, desc->poll_timeout);
242                 if (i <= 0)
243                         continue;
244                 sinlen = sizeof(sin);
245                 fd = accept(desc->accept_fd, (struct sockaddr *) &sin, &sinlen);
246                 if (fd < 0) {
247                         if ((errno != EAGAIN) && (errno != EINTR))
248                                 ast_log(LOG_WARNING, "Accept failed: %s\n", strerror(errno));
249                         continue;
250                 }
251                 tcptls_session = ao2_alloc(sizeof(*tcptls_session), session_instance_destructor);
252                 if (!tcptls_session) {
253                         ast_log(LOG_WARNING, "No memory for new session: %s\n", strerror(errno));
254                         close(fd);
255                         continue;
256                 }
257
258                 ast_mutex_init(&tcptls_session->lock);
259
260                 flags = fcntl(fd, F_GETFL);
261                 fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
262                 tcptls_session->fd = fd;
263                 tcptls_session->parent = desc;
264                 memcpy(&tcptls_session->remote_address, &sin, sizeof(tcptls_session->remote_address));
265
266                 tcptls_session->client = 0;
267
268                 /* This thread is now the only place that controls the single ref to tcptls_session */
269                 if (ast_pthread_create_detached_background(&launched, NULL, handle_tls_connection, tcptls_session)) {
270                         ast_log(LOG_WARNING, "Unable to launch helper thread: %s\n", strerror(errno));
271                         close(tcptls_session->fd);
272                         ao2_ref(tcptls_session, -1);
273                 }
274         }
275         return NULL;
276 }
277
278 static int __ssl_setup(struct ast_tls_config *cfg, int client)
279 {
280 #ifndef DO_SSL
281         cfg->enabled = 0;
282         return 0;
283 #else
284         if (!cfg->enabled)
285                 return 0;
286
287         SSL_load_error_strings();
288         SSLeay_add_ssl_algorithms();
289
290         if (client) {
291                 if (ast_test_flag(&cfg->flags, AST_SSL_SSLV2_CLIENT)) {
292                         cfg->ssl_ctx = SSL_CTX_new(SSLv2_client_method());
293                 } else if (ast_test_flag(&cfg->flags, AST_SSL_SSLV3_CLIENT)) {
294                         cfg->ssl_ctx = SSL_CTX_new(SSLv3_client_method());
295                 } else if (ast_test_flag(&cfg->flags, AST_SSL_TLSV1_CLIENT)) {
296                         cfg->ssl_ctx = SSL_CTX_new(TLSv1_client_method());
297                 } else {
298                         /* SSLv23_client_method() sends SSLv2, this was the original
299                          * default for ssl clients before the option was given to
300                          * pick what protocol a client should use.  In order not
301                          * to break expected behavior it remains the default. */
302                         cfg->ssl_ctx = SSL_CTX_new(SSLv23_client_method());
303                 }
304         } else {
305                 /* SSLv23_server_method() supports TLSv1, SSLv2, and SSLv3 inbound connections. */
306                 cfg->ssl_ctx = SSL_CTX_new(SSLv23_server_method());
307         }
308
309         if (!cfg->ssl_ctx) {
310                 ast_debug(1, "Sorry, SSL_CTX_new call returned null...\n");
311                 cfg->enabled = 0;
312                 return 0;
313         }
314         if (!ast_strlen_zero(cfg->certfile)) {
315                 char *tmpprivate = ast_strlen_zero(cfg->pvtfile) ? cfg->certfile : cfg->pvtfile;
316                 if (SSL_CTX_use_certificate_file(cfg->ssl_ctx, cfg->certfile, SSL_FILETYPE_PEM) == 0) {
317                         if (!client) {
318                                 /* Clients don't need a certificate, but if its setup we can use it */
319                                 ast_verb(0, "SSL error loading cert file. <%s>", cfg->certfile);
320                                 sleep(2);
321                                 cfg->enabled = 0;
322                                 return 0;
323                         }
324                 }
325                 if ((SSL_CTX_use_PrivateKey_file(cfg->ssl_ctx, tmpprivate, SSL_FILETYPE_PEM) == 0) || (SSL_CTX_check_private_key(cfg->ssl_ctx) == 0 )) {
326                         if (!client) {
327                                 /* Clients don't need a private key, but if its setup we can use it */
328                                 ast_verb(0, "SSL error loading private key file. <%s>", tmpprivate);
329                                 sleep(2);
330                                 cfg->enabled = 0;
331                                 return 0;
332                         }
333                 }
334         }
335         if (!ast_strlen_zero(cfg->cipher)) {
336                 if (SSL_CTX_set_cipher_list(cfg->ssl_ctx, cfg->cipher) == 0 ) {
337                         if (!client) {
338                                 ast_verb(0, "SSL cipher error <%s>", cfg->cipher);
339                                 sleep(2);
340                                 cfg->enabled = 0;
341                                 return 0;
342                         }
343                 }
344         }
345         if (!ast_strlen_zero(cfg->cafile) || !ast_strlen_zero(cfg->capath)) {
346                 if (SSL_CTX_load_verify_locations(cfg->ssl_ctx, S_OR(cfg->cafile, NULL), S_OR(cfg->capath,NULL)) == 0)
347                         ast_verb(0, "SSL CA file(%s)/path(%s) error\n", cfg->cafile, cfg->capath);
348         }
349
350         ast_verb(0, "SSL certificate ok\n");
351         return 1;
352 #endif
353 }
354
355 int ast_ssl_setup(struct ast_tls_config *cfg)
356 {
357         return __ssl_setup(cfg, 0);
358 }
359
360 struct ast_tcptls_session_instance *ast_tcptls_client_start(struct ast_tcptls_session_args *desc)
361 {
362         int flags;
363         int x = 1;
364         struct ast_tcptls_session_instance *tcptls_session = NULL;
365
366         /* Do nothing if nothing has changed */
367         if (!memcmp(&desc->old_address, &desc->remote_address, sizeof(desc->old_address))) {
368                 ast_debug(1, "Nothing changed in %s\n", desc->name);
369                 return NULL;
370         }
371
372         desc->old_address = desc->remote_address;
373
374         if (desc->accept_fd != -1)
375                 close(desc->accept_fd);
376
377         desc->accept_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
378         if (desc->accept_fd < 0) {
379                 ast_log(LOG_WARNING, "Unable to allocate socket for %s: %s\n",
380                         desc->name, strerror(errno));
381                 return NULL;
382         }
383
384         /* if a local address was specified, bind to it so the connection will
385            originate from the desired address */
386         if (desc->local_address.sin_family != 0) {
387                 setsockopt(desc->accept_fd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
388                 if (bind(desc->accept_fd, (struct sockaddr *) &desc->local_address, sizeof(desc->local_address))) {
389                         ast_log(LOG_ERROR, "Unable to bind %s to %s:%d: %s\n",
390                         desc->name,
391                                 ast_inet_ntoa(desc->local_address.sin_addr), ntohs(desc->local_address.sin_port),
392                                 strerror(errno));
393                         goto error;
394                 }
395         }
396
397         if (connect(desc->accept_fd, (const struct sockaddr *) &desc->remote_address, sizeof(desc->remote_address))) {
398                 ast_log(LOG_ERROR, "Unable to connect %s to %s:%d: %s\n",
399                         desc->name,
400                         ast_inet_ntoa(desc->remote_address.sin_addr), ntohs(desc->remote_address.sin_port),
401                         strerror(errno));
402                 goto error;
403         }
404
405         if (!(tcptls_session = ao2_alloc(sizeof(*tcptls_session), session_instance_destructor)))
406                 goto error;
407
408         ast_mutex_init(&tcptls_session->lock);
409
410         flags = fcntl(desc->accept_fd, F_GETFL);
411         fcntl(desc->accept_fd, F_SETFL, flags & ~O_NONBLOCK);
412
413         tcptls_session->fd = desc->accept_fd;
414         tcptls_session->parent = desc;
415         tcptls_session->parent->worker_fn = NULL;
416         memcpy(&tcptls_session->remote_address, &desc->remote_address, sizeof(tcptls_session->remote_address));
417
418         tcptls_session->client = 1;
419
420         if (desc->tls_cfg) {
421                 desc->tls_cfg->enabled = 1;
422                 __ssl_setup(desc->tls_cfg, 1);
423         }
424
425         /* handle_tls_connection controls the single ref to tcptls_session. If
426          * tcptls_session returns NULL then the session has been destroyed */
427         if (!(tcptls_session = handle_tls_connection(tcptls_session)))
428                 goto error;
429
430         return tcptls_session;
431
432 error:
433         close(desc->accept_fd);
434         desc->accept_fd = -1;
435         if (tcptls_session)
436                 ao2_ref(tcptls_session, -1);
437         return NULL;
438 }
439
440 void ast_tcptls_server_start(struct ast_tcptls_session_args *desc)
441 {
442         int flags;
443         int x = 1;
444
445         /* Do nothing if nothing has changed */
446         if (!memcmp(&desc->old_address, &desc->local_address, sizeof(desc->old_address))) {
447                 ast_debug(1, "Nothing changed in %s\n", desc->name);
448                 return;
449         }
450
451         desc->old_address = desc->local_address;
452
453         /* Shutdown a running server if there is one */
454         if (desc->master != AST_PTHREADT_NULL) {
455                 pthread_cancel(desc->master);
456                 pthread_kill(desc->master, SIGURG);
457                 pthread_join(desc->master, NULL);
458         }
459
460         if (desc->accept_fd != -1)
461                 close(desc->accept_fd);
462
463         /* If there's no new server, stop here */
464         if (desc->local_address.sin_family == 0) {
465                 ast_debug(2, "Server disabled:  %s\n", desc->name);
466                 return;
467         }
468
469         desc->accept_fd = socket(AF_INET, SOCK_STREAM, 0);
470         if (desc->accept_fd < 0) {
471                 ast_log(LOG_ERROR, "Unable to allocate socket for %s: %s\n", desc->name, strerror(errno));
472                 return;
473         }
474
475         setsockopt(desc->accept_fd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
476         if (bind(desc->accept_fd, (struct sockaddr *) &desc->local_address, sizeof(desc->local_address))) {
477                 ast_log(LOG_ERROR, "Unable to bind %s to %s:%d: %s\n",
478                         desc->name,
479                         ast_inet_ntoa(desc->local_address.sin_addr), ntohs(desc->local_address.sin_port),
480                         strerror(errno));
481                 goto error;
482         }
483         if (listen(desc->accept_fd, 10)) {
484                 ast_log(LOG_ERROR, "Unable to listen for %s!\n", desc->name);
485                 goto error;
486         }
487         flags = fcntl(desc->accept_fd, F_GETFL);
488         fcntl(desc->accept_fd, F_SETFL, flags | O_NONBLOCK);
489         if (ast_pthread_create_background(&desc->master, NULL, desc->accept_fn, desc)) {
490                 ast_log(LOG_ERROR, "Unable to launch thread for %s on %s:%d: %s\n",
491                         desc->name,
492                         ast_inet_ntoa(desc->local_address.sin_addr), ntohs(desc->local_address.sin_port),
493                         strerror(errno));
494                 goto error;
495         }
496         return;
497
498 error:
499         close(desc->accept_fd);
500         desc->accept_fd = -1;
501 }
502
503 void ast_tcptls_server_stop(struct ast_tcptls_session_args *desc)
504 {
505         if (desc->master != AST_PTHREADT_NULL) {
506                 pthread_cancel(desc->master);
507                 pthread_kill(desc->master, SIGURG);
508                 pthread_join(desc->master, NULL);
509         }
510         if (desc->accept_fd != -1)
511                 close(desc->accept_fd);
512         desc->accept_fd = -1;
513         ast_debug(2, "Stopped server :: %s\n", desc->name);
514 }
515
516 int ast_tls_read_conf(struct ast_tls_config *tls_cfg, struct ast_tcptls_session_args *tls_desc, const char *varname, const char *value)
517 {
518         if (!strcasecmp(varname, "tlsenable") || !strcasecmp(varname, "sslenable")) {
519                 tls_cfg->enabled = ast_true(value) ? 1 : 0;
520                 tls_desc->local_address.sin_family = AF_INET;
521         } else if (!strcasecmp(varname, "tlscertfile") || !strcasecmp(varname, "sslcert") || !strcasecmp(varname, "tlscert")) {
522                 ast_free(tls_cfg->certfile);
523                 tls_cfg->certfile = ast_strdup(value);
524         } else if (!strcasecmp(varname, "tlsprivatekey") || !strcasecmp(varname, "sslprivatekey")) {
525                 ast_free(tls_cfg->pvtfile);
526                 tls_cfg->pvtfile = ast_strdup(value);
527         } else if (!strcasecmp(varname, "tlscipher") || !strcasecmp(varname, "sslcipher")) {
528                 ast_free(tls_cfg->cipher);
529                 tls_cfg->cipher = ast_strdup(value);
530         } else if (!strcasecmp(varname, "tlscafile")) {
531                 ast_free(tls_cfg->cafile);
532                 tls_cfg->cafile = ast_strdup(value);
533         } else if (!strcasecmp(varname, "tlscapath")) {
534                 ast_free(tls_cfg->capath);
535                 tls_cfg->capath = ast_strdup(value);
536         } else if (!strcasecmp(varname, "tlsverifyclient")) {
537                 ast_set2_flag(&tls_cfg->flags, ast_true(value), AST_SSL_VERIFY_CLIENT);
538         } else if (!strcasecmp(varname, "tlsdontverifyserver")) {
539                 ast_set2_flag(&tls_cfg->flags, ast_true(value), AST_SSL_DONT_VERIFY_SERVER);
540         } else if (!strcasecmp(varname, "tlsbindaddr") || !strcasecmp(varname, "sslbindaddr")) {
541                 if (ast_parse_arg(value, PARSE_INADDR, &tls_desc->local_address))
542                         ast_log(LOG_WARNING, "Invalid %s '%s'\n", varname, value);
543         } else if (!strcasecmp(varname, "tlsbindport") || !strcasecmp(varname, "sslbindport")) {
544                 tls_desc->local_address.sin_port = htons(atoi(value));
545         } else if (!strcasecmp(varname, "tlsclientmethod") || !strcasecmp(varname, "sslclientmethod")) {
546                 if (!strcasecmp(value, "tlsv1")) {
547                         ast_set_flag(&tls_cfg->flags, AST_SSL_TLSV1_CLIENT);
548                         ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV3_CLIENT);
549                         ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV2_CLIENT);
550                 } else if (!strcasecmp(value, "sslv3")) {
551                         ast_set_flag(&tls_cfg->flags, AST_SSL_SSLV3_CLIENT);
552                         ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV2_CLIENT);
553                         ast_clear_flag(&tls_cfg->flags, AST_SSL_TLSV1_CLIENT);
554                 } else if (!strcasecmp(value, "sslv2")) {
555                         ast_set_flag(&tls_cfg->flags, AST_SSL_SSLV2_CLIENT);
556                         ast_clear_flag(&tls_cfg->flags, AST_SSL_TLSV1_CLIENT);
557                         ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV3_CLIENT);
558                 }
559         } else {
560                 return -1;
561         }
562
563         return 0;
564 }