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