Debian Codegraph @

main/tcptls.c

raw symbols SCIP
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 <[email protected]> (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 <[email protected]>
26 */
27
28 #include "asterisk.h"
29
30 #include "asterisk/tcptls.h" /* for ast_tls_config, ast_tcptls_se... */
31 #include "asterisk/iostream.h" /* for DO_SSL, ast_iostream_close, a... */
32
33 #ifdef HAVE_FCNTL_H
34 #include <fcntl.h> /* for O_NONBLOCK */
35 #endif /* HAVE_FCNTL_H */
36 #include <netinet/in.h> /* for IPPROTO_TCP */
37 #ifdef DO_SSL
38 #include <openssl/asn1.h> /* for ASN1_STRING_to_UTF8 */
39 #include <openssl/crypto.h> /* for OPENSSL_free */
40 #include <openssl/err.h> /* for ERR_print_errors_fp */
41 #include <openssl/opensslconf.h> /* for OPENSSL_NO_SSL3_METHOD, OPENS... */
42 #include <openssl/opensslv.h> /* for OPENSSL_VERSION_NUMBER */
43 #include <openssl/safestack.h> /* for STACK_OF */
44 #include <openssl/ssl.h> /* for SSL_CTX_free, SSL_get_error, ... */
45 #include <openssl/x509.h> /* for X509_free, X509_NAME_ENTRY_ge... */
46 #include <openssl/x509v3.h> /* for GENERAL_NAME, sk_GENERAL_NAME... */
47 #ifndef OPENSSL_NO_DH
48 #include <openssl/bio.h> /* for BIO_free, BIO_new_file */
49 #include <openssl/dh.h> /* for DH_free */
50 #include <openssl/pem.h> /* for PEM_read_bio_DHparams */
51 #endif /* OPENSSL_NO_DH */
52 #ifndef OPENSSL_NO_EC
53 #include <openssl/ec.h> /* for EC_KEY_free, EC_KEY_new_by_cu... */
54 #endif /* OPENSSL_NO_EC */
55 #endif /* DO_SSL */
56 #include <pthread.h> /* for pthread_cancel, pthread_join */
57 #include <signal.h> /* for pthread_kill, SIGURG */
58 #include <sys/socket.h> /* for setsockopt, shutdown, socket */
59 #include <sys/stat.h> /* for stat */
60
61 #include "asterisk/app.h" /* for ast_read_textfile */
62 #include "asterisk/astobj2.h" /* for ao2_ref, ao2_t_ref, ao2_alloc */
63 #include "asterisk/compat.h" /* for strcasecmp */
64 #include "asterisk/config.h" /* for ast_parse_arg, ast_parse_flag... */
65 #include "asterisk/io.h" /* for ast_sd_get_fd */
66 #include "asterisk/lock.h" /* for AST_PTHREADT_NULL */
67 #include "asterisk/logger.h" /* for ast_log, LOG_ERROR, ast_debug */
68 #include "asterisk/netsock2.h" /* for ast_sockaddr_copy, ast_sockad... */
69 #include "asterisk/pbx.h" /* for ast_thread_inhibit_escalations */
70 #include "asterisk/utils.h" /* for ast_true, ast_free, ast_wait_... */
71
72 static void session_instance_destructor(void *obj)
73 {
74 struct ast_tcptls_session_instance *i = obj;
75
76 if (i->stream) {
77 ast_iostream_close(i->stream);
78 i->stream = NULL;
79 }
80 ast_free(i->overflow_buf);
81 ao2_cleanup(i->private_data);
82 }
83
84 #ifdef DO_SSL
85 static int check_tcptls_cert_name(ASN1_STRING *cert_str, const char *hostname, const char *desc)
86 {
87 unsigned char *str;
88 int ret;
89
90 ret = ASN1_STRING_to_UTF8(&str, cert_str);
91 if (ret < 0 || !str) {
92 return -1;
93 }
94
95 if (strlen((char *) str) != ret) {
96 ast_log(LOG_WARNING, "Invalid certificate %s length (contains NULL bytes?)\n", desc);
97
98 ret = -1;
99 } else if (!strcasecmp(hostname, (char *) str)) {
100 ret = 0;
101 } else {
102 ret = -1;
103 }
104
105 ast_debug(3, "SSL %s compare s1='%s' s2='%s'\n", desc, hostname, str);
106 OPENSSL_free(str);
107
108 return ret;
109 }
110
111 static void write_openssl_error_to_log(void)
112 {
113 FILE *fp;
114 char *buffer;
115 size_t length;
116
117 fp = open_memstream(&buffer, &length);
118 if (!fp) {
119 return;
120 }
121
122 ERR_print_errors_fp(fp);
123 fclose(fp);
124
125 if (length) {
126 ast_log(LOG_ERROR, "%.*s\n", (int) length, buffer);
127 }
128
129 ast_std_free(buffer);
130 }
131 #endif
132
133 /*! \brief
134 * creates a FILE * from the fd passed by the accept thread.
135 * This operation is potentially expensive (certificate verification),
136 * so we do it in the child thread context.
137 *
138 * \note must decrement ref count before returning NULL on error
139 */
140 static void *handle_tcptls_connection(void *data)
141 {
142 struct ast_tcptls_session_instance *tcptls_session = data;
143 #ifdef DO_SSL
144 SSL *ssl;
145 #endif
146
147 /* TCP/TLS connections are associated with external protocols, and
148 * should not be allowed to execute 'dangerous' functions. This may
149 * need to be pushed down into the individual protocol handlers, but
150 * this seems like a good general policy.
151 */
152 if (ast_thread_inhibit_escalations()) {
153 ast_log(LOG_ERROR, "Failed to inhibit privilege escalations; killing connection from peer '%s'\n",
154 ast_sockaddr_stringify(&tcptls_session->remote_address));
155 ast_tcptls_close_session_file(tcptls_session);
156 ao2_ref(tcptls_session, -1);
157 return NULL;
158 }
159
160 /*
161 * TCP/TLS connections are associated with external protocols which can
162 * be considered to be user interfaces (even for SIP messages), and
163 * will not handle channel media. This may need to be pushed down into
164 * the individual protocol handlers, but this seems like a good start.
165 */
166 if (ast_thread_user_interface_set(1)) {
167 ast_log(LOG_ERROR, "Failed to set user interface status; killing connection from peer '%s'\n",
168 ast_sockaddr_stringify(&tcptls_session->remote_address));
169 ast_tcptls_close_session_file(tcptls_session);
170 ao2_ref(tcptls_session, -1);
171 return NULL;
172 }
173
174 if (tcptls_session->parent->tls_cfg) {
175 #ifdef DO_SSL
176 if (ast_iostream_start_tls(&tcptls_session->stream, tcptls_session->parent->tls_cfg->ssl_ctx, tcptls_session->client) < 0) {
177 SSL *ssl = ast_iostream_get_ssl(tcptls_session->stream);
178 if (ssl) {
179 ast_log(LOG_ERROR, "Unable to set up ssl connection with peer '%s'\n",
180 ast_sockaddr_stringify(&tcptls_session->remote_address));
181 }
182 ast_tcptls_close_session_file(tcptls_session);
183 ao2_ref(tcptls_session, -1);
184 return NULL;
185 }
186
187 ssl = ast_iostream_get_ssl(tcptls_session->stream);
188 if ((tcptls_session->client && !ast_test_flag(&tcptls_session->parent->tls_cfg->flags, AST_SSL_DONT_VERIFY_SERVER))
189 || (!tcptls_session->client && ast_test_flag(&tcptls_session->parent->tls_cfg->flags, AST_SSL_VERIFY_CLIENT))) {
190 X509 *peer;
191 long res;
192 peer = SSL_get_peer_certificate(ssl);
193 if (!peer) {
194 ast_log(LOG_ERROR, "No SSL certificate to verify from peer '%s'\n",
195 ast_sockaddr_stringify(&tcptls_session->remote_address));
196 ast_tcptls_close_session_file(tcptls_session);
197 ao2_ref(tcptls_session, -1);
198 return NULL;
199 }
200
201 res = SSL_get_verify_result(ssl);
202 if (res != X509_V_OK) {
203 ast_log(LOG_ERROR, "Certificate from peer '%s' did not verify: %s\n",
204 ast_sockaddr_stringify(&tcptls_session->remote_address),
205 X509_verify_cert_error_string(res));
206 X509_free(peer);
207 ast_tcptls_close_session_file(tcptls_session);
208 ao2_ref(tcptls_session, -1);
209 return NULL;
210 }
211 if (!ast_test_flag(&tcptls_session->parent->tls_cfg->flags, AST_SSL_IGNORE_COMMON_NAME)) {
212 ASN1_STRING *str;
213 X509_NAME *name = X509_get_subject_name(peer);
214 STACK_OF(GENERAL_NAME) *alt_names;
215 int pos = -1;
216 int found = 0;
217
218 for (;;) {
219 /* Walk the certificate to check all available "Common Name" */
220 /* XXX Probably should do a gethostbyname on the hostname and compare that as well */
221 pos = X509_NAME_get_index_by_NID(name, NID_commonName, pos);
222 if (pos < 0) {
223 break;
224 }
225 str = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name, pos));
226 if (!check_tcptls_cert_name(str, tcptls_session->parent->hostname, "common name")) {
227 found = 1;
228 break;
229 }
230 }
231
232 if (!found) {
233 alt_names = X509_get_ext_d2i(peer, NID_subject_alt_name, NULL, NULL);
234 if (alt_names != NULL) {
235 int alt_names_count = sk_GENERAL_NAME_num(alt_names);
236
237 for (pos = 0; pos < alt_names_count; pos++) {
238 const GENERAL_NAME *alt_name = sk_GENERAL_NAME_value(alt_names, pos);
239
240 if (alt_name->type != GEN_DNS) {
241 continue;
242 }
243
244 if (!check_tcptls_cert_name(alt_name->d.dNSName, tcptls_session->parent->hostname, "alt name")) {
245 found = 1;
246 break;
247 }
248 }
249
250 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
251 }
252 }
253
254 if (!found) {
255 ast_log(LOG_ERROR, "Certificate common name from peer '%s' did not match (%s)\n",
256 ast_sockaddr_stringify(&tcptls_session->remote_address), tcptls_session->parent->hostname);
257 X509_free(peer);
258 ast_tcptls_close_session_file(tcptls_session);
259 ao2_ref(tcptls_session, -1);
260 return NULL;
261 }
262 }
263 X509_free(peer);
264 }
265 #else
266 ast_log(LOG_ERROR, "TLS client failed: Asterisk is compiled without OpenSSL support. Install OpenSSL development headers and rebuild Asterisk after running ./configure\n");
267 ast_tcptls_close_session_file(tcptls_session);
268 ao2_ref(tcptls_session, -1);
269 return NULL;
270 #endif /* DO_SSL */
271 }
272
273 if (tcptls_session->parent->worker_fn) {
274 return tcptls_session->parent->worker_fn(tcptls_session);
275 } else {
276 return tcptls_session;
277 }
278 }
279
280 void *ast_tcptls_server_root(void *data)
281 {
282 struct ast_tcptls_session_args *desc = data;
283 int fd;
284 struct ast_sockaddr addr;
285 struct ast_tcptls_session_instance *tcptls_session;
286 pthread_t launched;
287
288 for (;;) {
289 int i;
290
291 if (desc->periodic_fn) {
292 desc->periodic_fn(desc);
293 }
294 i = ast_wait_for_input(desc->accept_fd, desc->poll_timeout);
295 if (i <= 0) {
296 /* Prevent tight loop from hogging CPU */
297 usleep(1);
298 continue;
299 }
300 fd = ast_accept(desc->accept_fd, &addr);
301 if (fd < 0) {
302 if (errno != EAGAIN
303 && errno != EWOULDBLOCK
304 && errno != EINTR
305 && errno != ECONNABORTED) {
306 ast_log(LOG_ERROR, "TCP/TLS accept failed: %s\n", strerror(errno));
307 if (errno != EMFILE) {
308 break;
309 }
310 }
311 /* Prevent tight loop from hogging CPU */
312 usleep(1);
313 continue;
314 }
315 tcptls_session = ao2_alloc(sizeof(*tcptls_session), session_instance_destructor);
316 if (!tcptls_session) {
317 close(fd);
318 continue;
319 }
320
321 tcptls_session->overflow_buf = ast_str_create(128);
322 if (!tcptls_session->overflow_buf) {
323 ao2_ref(tcptls_session, -1);
324 close(fd);
325 continue;
326 }
327 ast_fd_clear_flags(fd, O_NONBLOCK);
328
329 tcptls_session->stream = ast_iostream_from_fd(&fd);
330 if (!tcptls_session->stream) {
331 ao2_ref(tcptls_session, -1);
332 close(fd);
333 continue;
334 }
335
336 tcptls_session->parent = desc;
337 ast_sockaddr_copy(&tcptls_session->remote_address, &addr);
338
339 tcptls_session->client = 0;
340
341 /* This thread is now the only place that controls the single ref to tcptls_session */
342 if (ast_pthread_create_detached_background(&launched, NULL, handle_tcptls_connection, tcptls_session)) {
343 ast_log(LOG_ERROR, "TCP/TLS unable to launch helper thread for peer '%s': %s\n",
344 ast_sockaddr_stringify(&tcptls_session->remote_address),
345 strerror(errno));
346 ao2_ref(tcptls_session, -1);
347 }
348 }
349
350 ast_log(LOG_ERROR, "TCP/TLS listener thread ended abnormally\n");
351
352 /* Close the listener socket so Asterisk doesn't appear dead. */
353 fd = desc->accept_fd;
354 desc->accept_fd = -1;
355 if (0 <= fd) {
356 close(fd);
357 }
358 return NULL;
359 }
360
361 #ifdef DO_SSL
362 static void __ssl_setup_certs(struct ast_tls_config *cfg, const size_t cert_file_len, const char *key_type_extension, const char *key_type)
363 {
364 char *cert_file = ast_strdupa(cfg->certfile);
365
366 memcpy(cert_file + cert_file_len - 8, key_type_extension, 5);
367 if (access(cert_file, F_OK) == 0) {
368 if (SSL_CTX_use_certificate_chain_file(cfg->ssl_ctx, cert_file) == 0) {
369 ast_log(LOG_WARNING, "TLS/SSL error loading public %s key (certificate) from <%s>.\n", key_type, cert_file);
370 write_openssl_error_to_log();
371 } else if (SSL_CTX_use_PrivateKey_file(cfg->ssl_ctx, cert_file, SSL_FILETYPE_PEM) == 0) {
372 ast_log(LOG_WARNING, "TLS/SSL error loading private %s key from <%s>.\n", key_type, cert_file);
373 write_openssl_error_to_log();
374 } else if (SSL_CTX_check_private_key(cfg->ssl_ctx) == 0) {
375 ast_log(LOG_WARNING, "TLS/SSL error matching private %s key and certificate in <%s>.\n", key_type, cert_file);
376 write_openssl_error_to_log();
377 }
378 }
379 }
380 #endif
381
382 static int __ssl_setup(struct ast_tls_config *cfg, int client,
383 int suppress_progress_msgs)
384 {
385 #ifndef DO_SSL
386 if (cfg->enabled) {
387 ast_log(LOG_ERROR, "TLS server failed: Asterisk is compiled without OpenSSL support. Install OpenSSL development headers and rebuild Asterisk after running ./configure\n");
388 cfg->enabled = 0;
389 }
390 return 0;
391 #else
392 int disable_ssl = 0;
393 long ssl_opts = 0;
394
395 if (!cfg->enabled) {
396 return 0;
397 }
398
399 /* Get rid of an old SSL_CTX since we're about to
400 * allocate a new one
401 */
402 if (cfg->ssl_ctx) {
403 SSL_CTX_free(cfg->ssl_ctx);
404 cfg->ssl_ctx = NULL;
405 }
406
407 if (client) {
408 #if !defined(OPENSSL_NO_SSL2) && (OPENSSL_VERSION_NUMBER < 0x10100000L)
409 if (ast_test_flag(&cfg->flags, AST_SSL_SSLV2_CLIENT)) {
410 ast_log(LOG_WARNING, "Usage of SSLv2 is discouraged due to known vulnerabilities. Please use 'tlsv1' or leave the TLS method unspecified!\n");
411 cfg->ssl_ctx = SSL_CTX_new(SSLv2_client_method());
412 } else
413 #endif
414 #if !defined(OPENSSL_NO_SSL3_METHOD) && !(defined(OPENSSL_API_COMPAT) && (OPENSSL_API_COMPAT >= 0x10100000L)) && (OPENSSL_VERSION_NUMBER < 0x40000000L)
415 if (ast_test_flag(&cfg->flags, AST_SSL_SSLV3_CLIENT)) {
416 ast_log(LOG_WARNING, "Usage of SSLv3 is discouraged due to known vulnerabilities. Please use 'tlsv1' or leave the TLS method unspecified!\n");
417 cfg->ssl_ctx = SSL_CTX_new(SSLv3_client_method());
418 } else
419 #endif
420 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
421 cfg->ssl_ctx = SSL_CTX_new(TLS_client_method());
422 #else
423 if (ast_test_flag(&cfg->flags, AST_SSL_TLSV1_CLIENT)) {
424 cfg->ssl_ctx = SSL_CTX_new(TLSv1_client_method());
425 } else {
426 disable_ssl = 1;
427 cfg->ssl_ctx = SSL_CTX_new(SSLv23_client_method());
428 }
429 #endif
430 } else {
431 disable_ssl = 1;
432 cfg->ssl_ctx = SSL_CTX_new(SSLv23_server_method());
433 }
434
435 if (!cfg->ssl_ctx) {
436 ast_debug(1, "Sorry, SSL_CTX_new call returned null...\n");
437 cfg->enabled = 0;
438 return 0;
439 }
440
441 /* Due to the POODLE vulnerability, completely disable
442 * SSLv2 and SSLv3 if we are not explicitly told to use
443 * them. SSLv23_*_method supports TLSv1+.
444 */
445 if (disable_ssl) {
446 ssl_opts |= SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3;
447 }
448
449 if (ast_test_flag(&cfg->flags, AST_SSL_SERVER_CIPHER_ORDER)) {
450 ssl_opts |= SSL_OP_CIPHER_SERVER_PREFERENCE;
451 }
452
453 if (ast_test_flag(&cfg->flags, AST_SSL_DISABLE_TLSV1)) {
454 ssl_opts |= SSL_OP_NO_TLSv1;
455 }
456 #if defined(SSL_OP_NO_TLSv1_1) && defined(SSL_OP_NO_TLSv1_2)
457 if (ast_test_flag(&cfg->flags, AST_SSL_DISABLE_TLSV11)) {
458 ssl_opts |= SSL_OP_NO_TLSv1_1;
459 }
460 if (ast_test_flag(&cfg->flags, AST_SSL_DISABLE_TLSV12)) {
461 ssl_opts |= SSL_OP_NO_TLSv1_2;
462 }
463 #else
464 ast_log(LOG_WARNING, "Your version of OpenSSL leaves you potentially vulnerable "
465 "to the SSL BEAST attack. Please upgrade to OpenSSL 1.0.1 or later\n");
466 #endif
467
468 SSL_CTX_set_options(cfg->ssl_ctx, ssl_opts);
469
470 SSL_CTX_set_verify(cfg->ssl_ctx,
471 ast_test_flag(&cfg->flags, AST_SSL_VERIFY_CLIENT) ? SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT : SSL_VERIFY_NONE,
472 NULL);
473
474 if (!ast_strlen_zero(cfg->certfile)) {
475 char *tmpprivate = ast_strlen_zero(cfg->pvtfile) ? cfg->certfile : cfg->pvtfile;
476 if (SSL_CTX_use_certificate_chain_file(cfg->ssl_ctx, cfg->certfile) == 0) {
477 if (!client) {
478 /* Clients don't need a certificate, but if its setup we can use it */
479 ast_log(LOG_ERROR, "TLS/SSL error loading cert file. <%s>\n", cfg->certfile);
480 write_openssl_error_to_log();
481 cfg->enabled = 0;
482 SSL_CTX_free(cfg->ssl_ctx);
483 cfg->ssl_ctx = NULL;
484 return 0;
485 }
486 }
487 if ((SSL_CTX_use_PrivateKey_file(cfg->ssl_ctx, tmpprivate, SSL_FILETYPE_PEM) == 0) || (SSL_CTX_check_private_key(cfg->ssl_ctx) == 0 )) {
488 if (!client) {
489 /* Clients don't need a private key, but if its setup we can use it */
490 ast_log(LOG_ERROR, "TLS/SSL error loading private key file. <%s>\n", tmpprivate);
491 write_openssl_error_to_log();
492 cfg->enabled = 0;
493 SSL_CTX_free(cfg->ssl_ctx);
494 cfg->ssl_ctx = NULL;
495 return 0;
496 }
497 }
498 if (!client) {
499 size_t certfile_len = strlen(cfg->certfile);
500
501 /* expects a file name which contains _rsa. like asterisk_rsa.pem
502 * ignores any 3-character file-extension like .pem, .cer, .crt
503 */
504 if (certfile_len >= 8 && !strncmp(cfg->certfile + certfile_len - 8, "_rsa.", 5)) {
505 __ssl_setup_certs(cfg, certfile_len, "_ecc.", "ECC");
506 __ssl_setup_certs(cfg, certfile_len, "_dsa.", "DSA");
507 }
508 }
509 }
510 if (!ast_strlen_zero(cfg->cipher)) {
511 if (SSL_CTX_set_cipher_list(cfg->ssl_ctx, cfg->cipher) == 0 ) {
512 if (!client) {
513 ast_log(LOG_ERROR, "TLS/SSL cipher error <%s>\n", cfg->cipher);
514 write_openssl_error_to_log();
515 cfg->enabled = 0;
516 SSL_CTX_free(cfg->ssl_ctx);
517 cfg->ssl_ctx = NULL;
518 return 0;
519 }
520 }
521 }
522 if (!ast_strlen_zero(cfg->cafile) || !ast_strlen_zero(cfg->capath)) {
523 if (SSL_CTX_load_verify_locations(cfg->ssl_ctx, S_OR(cfg->cafile, NULL), S_OR(cfg->capath,NULL)) == 0) {
524 ast_log(LOG_ERROR, "TLS/SSL CA file(%s)/path(%s) error\n", cfg->cafile, cfg->capath);
525 write_openssl_error_to_log();
526 }
527 }
528
529 #ifndef OPENSSL_NO_DH
530 if (!ast_strlen_zero(cfg->pvtfile)) {
531 BIO *bio = BIO_new_file(cfg->pvtfile, "r");
532 if (bio != NULL) {
533 DH *dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
534 if (dh != NULL) {
535 if (SSL_CTX_set_tmp_dh(cfg->ssl_ctx, dh)) {
536 long options = SSL_OP_CIPHER_SERVER_PREFERENCE | SSL_OP_SINGLE_DH_USE | SSL_OP_SINGLE_ECDH_USE;
537 options = SSL_CTX_set_options(cfg->ssl_ctx, options);
538 if (!suppress_progress_msgs) {
539 ast_verb(2, "TLS/SSL DH initialized, PFS cipher-suites enabled\n");
540 }
541 }
542 DH_free(dh);
543 }
544 BIO_free(bio);
545 }
546 }
547 #endif
548
549 #ifndef SSL_CTRL_SET_ECDH_AUTO
550 #define SSL_CTRL_SET_ECDH_AUTO 94
551 #endif
552 /* SSL_CTX_set_ecdh_auto(cfg->ssl_ctx, on); requires OpenSSL 1.0.2 which wraps: */
553 if (SSL_CTX_ctrl(cfg->ssl_ctx, SSL_CTRL_SET_ECDH_AUTO, 1, NULL)) {
554 if (!suppress_progress_msgs) {
555 ast_verb(2, "TLS/SSL ECDH initialized (automatic), faster PFS ciphers enabled\n");
556 }
557 #if !defined(OPENSSL_NO_ECDH) && (OPENSSL_VERSION_NUMBER >= 0x10000000L) && (OPENSSL_VERSION_NUMBER < 0x10100000L)
558 } else {
559 /* enables AES-128 ciphers, to get AES-256 use NID_secp384r1 */
560 EC_KEY *ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
561 if (ecdh != NULL) {
562 if (SSL_CTX_set_tmp_ecdh(cfg->ssl_ctx, ecdh)) {
563 ast_verb(2, "TLS/SSL ECDH initialized (secp256r1), faster PFS cipher-suites enabled\n");
564 }
565 EC_KEY_free(ecdh);
566 }
567 #endif
568 }
569
570 if (!suppress_progress_msgs) {
571 ast_verb(2, "TLS/SSL certificate ok\n"); /* We should log which one that is ok. This message doesn't really make sense in production use */
572 }
573 return 1;
574 #endif
575 }
576
577 int ast_ssl_setup(struct ast_tls_config *cfg)
578 {
579 return __ssl_setup(cfg, 0, 0);
580 }
581
582 void ast_ssl_teardown(struct ast_tls_config *cfg)
583 {
584 #ifdef DO_SSL
585 if (cfg && cfg->ssl_ctx) {
586 SSL_CTX_free(cfg->ssl_ctx);
587 cfg->ssl_ctx = NULL;
588 }
589 #endif
590 }
591
592 /*!
593 * \internal
594 * \brief Connect a socket
595 *
596 * Attempt to connect to a given address for up to 'timeout' milliseconds. A negative
597 * timeout value equates to an infinite wait time.
598 *
599 * A -1 is returned on error, and an appropriate errno value is set based on the
600 * type of error.
601 *
602 * \param sockfd The socket file descriptor
603 * \param addr The address to connect to
604 * \param timeout How long, in milliseconds, to attempt to connect
605 *
606 * \return 0 if successfully connected, -1 otherwise
607 */
608 static int socket_connect(int sockfd, const struct ast_sockaddr *addr, int timeout)
609 {
610 int optval = 0;
611 socklen_t optlen = sizeof(int);
612
613 errno = 0;
614
615 if (ast_connect(sockfd, addr)) {
616 int res;
617
618 /*
619 * A connect failure could mean things are still in progress.
620 * If so wait for it to complete.
621 */
622
623 if (errno != EINPROGRESS) {
624 return -1;
625 }
626
627 while ((res = ast_wait_for_output(sockfd, timeout)) != 1) {
628 if (res == 0) {
629 errno = ETIMEDOUT;
630 return -1;
631 }
632
633 if (errno != EINTR) {
634 return -1;
635 }
636 }
637 }
638
639 /* Check the status to ensure it actually connected successfully */
640 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval, &optlen) < 0) {
641 return -1;
642 }
643
644 if (optval) {
645 errno = optval;
646 return -1;
647 }
648
649 return 0;
650 }
651
652 struct ast_tcptls_session_instance *ast_tcptls_client_start_timeout(
653 struct ast_tcptls_session_instance *tcptls_session, int timeout)
654 {
655 struct ast_tcptls_session_args *desc;
656
657 if (!(desc = tcptls_session->parent)) {
658 ao2_ref(tcptls_session, -1);
659 return NULL;
660 }
661
662 if (socket_connect(desc->accept_fd, &desc->remote_address, timeout)) {
663 if (!desc->suppress_connection_msgs) {
664 ast_log(LOG_WARNING, "Unable to connect %s to %s: %s\n", desc->name,
665 ast_sockaddr_stringify(&desc->remote_address), strerror(errno));
666 }
667
668 ao2_ref(tcptls_session, -1);
669 return NULL;
670 }
671
672 ast_fd_clear_flags(desc->accept_fd, O_NONBLOCK);
673
674 if (desc->tls_cfg) {
675 __ssl_setup(desc->tls_cfg, 1, desc->suppress_connection_msgs);
676 }
677
678 return handle_tcptls_connection(tcptls_session);
679 }
680
681 struct ast_tcptls_session_instance *ast_tcptls_client_start(struct ast_tcptls_session_instance *tcptls_session)
682 {
683 return ast_tcptls_client_start_timeout(tcptls_session, -1);
684 }
685
686 struct ast_tcptls_session_instance *ast_tcptls_client_create(struct ast_tcptls_session_args *desc)
687 {
688 int fd, x = 1;
689 struct ast_tcptls_session_instance *tcptls_session = NULL;
690
691 ast_assert(!desc->tls_cfg
692 || ast_test_flag(&desc->tls_cfg->flags, AST_SSL_DONT_VERIFY_SERVER)
693 || !ast_strlen_zero(desc->hostname));
694
695 /* Do nothing if nothing has changed */
696 if (!ast_sockaddr_cmp(&desc->old_address, &desc->remote_address)) {
697 ast_debug(1, "Nothing changed in %s\n", desc->name);
698 return NULL;
699 }
700
701 /* If we return early, there is no connection */
702 ast_sockaddr_setnull(&desc->old_address);
703
704 fd = desc->accept_fd = ast_socket_nonblock(ast_sockaddr_is_ipv6(&desc->remote_address) ?
705 AF_INET6 : AF_INET, SOCK_STREAM, IPPROTO_TCP);
706 if (desc->accept_fd < 0) {
707 ast_log(LOG_ERROR, "Unable to allocate socket for %s: %s\n",
708 desc->name, strerror(errno));
709 return NULL;
710 }
711
712 /* if a local address was specified, bind to it so the connection will
713 originate from the desired address */
714 if (!ast_sockaddr_isnull(&desc->local_address) &&
715 !ast_sockaddr_is_any(&desc->local_address)) {
716 setsockopt(desc->accept_fd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
717 if (ast_bind(desc->accept_fd, &desc->local_address)) {
718 ast_log(LOG_ERROR, "Unable to bind %s to %s: %s\n",
719 desc->name,
720 ast_sockaddr_stringify(&desc->local_address),
721 strerror(errno));
722 goto error;
723 }
724 }
725
726 tcptls_session = ao2_alloc(sizeof(*tcptls_session), session_instance_destructor);
727 if (!tcptls_session) {
728 goto error;
729 }
730
731 tcptls_session->overflow_buf = ast_str_create(128);
732 if (!tcptls_session->overflow_buf) {
733 goto error;
734 }
735 tcptls_session->client = 1;
736 tcptls_session->stream = ast_iostream_from_fd(&fd);
737 if (!tcptls_session->stream) {
738 goto error;
739 }
740
741 /* From here on out, the iostream owns the accept_fd and it will take
742 * care of closing it when the iostream is closed */
743
744 tcptls_session->parent = desc;
745 tcptls_session->parent->worker_fn = NULL;
746 ast_sockaddr_copy(&tcptls_session->remote_address,
747 &desc->remote_address);
748
749 /* Set current info */
750 ast_sockaddr_copy(&desc->old_address, &desc->remote_address);
751
752 if (!ast_strlen_zero(desc->hostname)) {
753 if (ast_iostream_set_sni_hostname(tcptls_session->stream, desc->hostname) != 0) {
754 ast_log(LOG_WARNING, "Unable to set SNI hostname '%s' on connection '%s'\n",
755 desc->hostname, desc->name);
756 }
757 }
758
759 return tcptls_session;
760
761 error:
762 close(desc->accept_fd);
763 desc->accept_fd = -1;
764 ao2_cleanup(tcptls_session);
765 return NULL;
766 }
767
768 void ast_tcptls_server_start(struct ast_tcptls_session_args *desc)
769 {
770 int x = 1;
771 int tls_changed = 0;
772 int sd_socket;
773
774 if (desc->tls_cfg) {
775 char hash[41];
776 char *str = NULL;
777 struct stat st;
778
779 /* Store the hashes of the TLS certificate etc. */
780 if (stat(desc->tls_cfg->certfile, &st) || NULL == (str = ast_read_textfile(desc->tls_cfg->certfile))) {
781 memset(hash, 0, 41);
782 } else {
783 ast_sha1_hash(hash, str);
784 }
785 ast_free(str);
786 str = NULL;
787 memcpy(desc->tls_cfg->certhash, hash, 41);
788 if (stat(desc->tls_cfg->pvtfile, &st) || NULL == (str = ast_read_textfile(desc->tls_cfg->pvtfile))) {
789 memset(hash, 0, 41);
790 } else {
791 ast_sha1_hash(hash, str);
792 }
793 ast_free(str);
794 str = NULL;
795 memcpy(desc->tls_cfg->pvthash, hash, 41);
796 if (stat(desc->tls_cfg->cafile, &st) || NULL == (str = ast_read_textfile(desc->tls_cfg->cafile))) {
797 memset(hash, 0, 41);
798 } else {
799 ast_sha1_hash(hash, str);
800 }
801 ast_free(str);
802 str = NULL;
803 memcpy(desc->tls_cfg->cahash, hash, 41);
804
805 /* Check whether TLS configuration has changed */
806 if (!desc->old_tls_cfg) { /* No previous configuration */
807 tls_changed = 1;
808 desc->old_tls_cfg = ast_calloc(1, sizeof(*desc->old_tls_cfg));
809 } else if (memcmp(desc->tls_cfg->certhash, desc->old_tls_cfg->certhash, 41)) {
810 tls_changed = 1;
811 } else if (memcmp(desc->tls_cfg->pvthash, desc->old_tls_cfg->pvthash, 41)) {
812 tls_changed = 1;
813 } else if (strcmp(desc->tls_cfg->cipher, desc->old_tls_cfg->cipher)) {
814 tls_changed = 1;
815 } else if (memcmp(desc->tls_cfg->cahash, desc->old_tls_cfg->cahash, 41)) {
816 tls_changed = 1;
817 } else if (strcmp(desc->tls_cfg->capath, desc->old_tls_cfg->capath)) {
818 tls_changed = 1;
819 } else if (memcmp(&desc->tls_cfg->flags, &desc->old_tls_cfg->flags, sizeof(desc->tls_cfg->flags))) {
820 tls_changed = 1;
821 }
822
823 if (tls_changed) {
824 ast_debug(1, "Changed parameters for %s found\n", desc->name);
825 }
826 }
827
828 /* Do nothing if nothing has changed */
829 if (!tls_changed && !ast_sockaddr_cmp(&desc->old_address, &desc->local_address)) {
830 ast_debug(1, "Nothing changed in %s\n", desc->name);
831 return;
832 }
833
834 /* If we return early, there is no one listening */
835 ast_sockaddr_setnull(&desc->old_address);
836
837 /* Shutdown a running server if there is one */
838 if (desc->master != AST_PTHREADT_NULL) {
839 pthread_cancel(desc->master);
840 pthread_kill(desc->master, SIGURG);
841 pthread_join(desc->master, NULL);
842 }
843
844 sd_socket = ast_sd_get_fd(SOCK_STREAM, &desc->local_address);
845
846 if (sd_socket != -1) {
847 if (desc->accept_fd != sd_socket) {
848 if (desc->accept_fd != -1) {
849 close(desc->accept_fd);
850 }
851 desc->accept_fd = sd_socket;
852 }
853
854 goto systemd_socket_activation;
855 }
856
857 if (desc->accept_fd != -1) {
858 close(desc->accept_fd);
859 desc->accept_fd = -1;
860 }
861
862 /* If there's no new server, stop here */
863 if (ast_sockaddr_isnull(&desc->local_address)) {
864 ast_debug(2, "Server disabled: %s\n", desc->name);
865 return;
866 }
867
868 desc->accept_fd = ast_socket_nonblock(ast_sockaddr_is_ipv6(&desc->local_address) ?
869 AF_INET6 : AF_INET, SOCK_STREAM, 0);
870 if (desc->accept_fd < 0) {
871 ast_log(LOG_ERROR, "Unable to allocate socket for %s: %s\n", desc->name, strerror(errno));
872 return;
873 }
874
875 setsockopt(desc->accept_fd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
876 if (ast_bind(desc->accept_fd, &desc->local_address)) {
877 ast_log(LOG_ERROR, "Unable to bind %s to %s: %s\n",
878 desc->name,
879 ast_sockaddr_stringify(&desc->local_address),
880 strerror(errno));
881 goto error;
882 }
883 if (listen(desc->accept_fd, 10)) {
884 ast_log(LOG_ERROR, "Unable to listen for %s!\n", desc->name);
885 goto error;
886 }
887
888 systemd_socket_activation:
889 if (ast_pthread_create_background(&desc->master, NULL, desc->accept_fn, desc)) {
890 ast_log(LOG_ERROR, "Unable to launch thread for %s on %s: %s\n",
891 desc->name,
892 ast_sockaddr_stringify(&desc->local_address),
893 strerror(errno));
894 goto error;
895 }
896
897 /* Set current info */
898 ast_sockaddr_copy(&desc->old_address, &desc->local_address);
899 if (desc->old_tls_cfg) {
900 ast_free(desc->old_tls_cfg->certfile);
901 ast_free(desc->old_tls_cfg->pvtfile);
902 ast_free(desc->old_tls_cfg->cipher);
903 ast_free(desc->old_tls_cfg->cafile);
904 ast_free(desc->old_tls_cfg->capath);
905 desc->old_tls_cfg->certfile = ast_strdup(desc->tls_cfg->certfile);
906 desc->old_tls_cfg->pvtfile = ast_strdup(desc->tls_cfg->pvtfile);
907 desc->old_tls_cfg->cipher = ast_strdup(desc->tls_cfg->cipher);
908 desc->old_tls_cfg->cafile = ast_strdup(desc->tls_cfg->cafile);
909 desc->old_tls_cfg->capath = ast_strdup(desc->tls_cfg->capath);
910 memcpy(desc->old_tls_cfg->certhash, desc->tls_cfg->certhash, 41);
911 memcpy(desc->old_tls_cfg->pvthash, desc->tls_cfg->pvthash, 41);
912 memcpy(desc->old_tls_cfg->cahash, desc->tls_cfg->cahash, 41);
913 memcpy(&desc->old_tls_cfg->flags, &desc->tls_cfg->flags, sizeof(desc->old_tls_cfg->flags));
914 }
915
916 return;
917
918 error:
919 close(desc->accept_fd);
920 desc->accept_fd = -1;
921 }
922
923 void ast_tcptls_close_session_file(struct ast_tcptls_session_instance *tcptls_session)
924 {
925 if (tcptls_session->stream) {
926 ast_iostream_close(tcptls_session->stream);
927 tcptls_session->stream = NULL;
928 } else {
929 ast_debug(1, "ast_tcptls_close_session_file invoked on session instance without file or file descriptor\n");
930 }
931 }
932
933 void ast_tcptls_server_stop(struct ast_tcptls_session_args *desc)
934 {
935 if (desc->master != AST_PTHREADT_NULL) {
936 pthread_cancel(desc->master);
937 pthread_kill(desc->master, SIGURG);
938 pthread_join(desc->master, NULL);
939 desc->master = AST_PTHREADT_NULL;
940 }
941 if (desc->accept_fd != -1) {
942 close(desc->accept_fd);
943 }
944 desc->accept_fd = -1;
945
946 if (desc->old_tls_cfg) {
947 ast_free(desc->old_tls_cfg->certfile);
948 ast_free(desc->old_tls_cfg->pvtfile);
949 ast_free(desc->old_tls_cfg->cipher);
950 ast_free(desc->old_tls_cfg->cafile);
951 ast_free(desc->old_tls_cfg->capath);
952 ast_free(desc->old_tls_cfg);
953 desc->old_tls_cfg = NULL;
954 }
955
956 ast_debug(2, "Stopped server :: %s\n", desc->name);
957 }
958
959 int ast_tls_read_conf(struct ast_tls_config *tls_cfg, struct ast_tcptls_session_args *tls_desc, const char *varname, const char *value)
960 {
961 if (!strcasecmp(varname, "tlsenable") || !strcasecmp(varname, "sslenable")) {
962 tls_cfg->enabled = ast_true(value) ? 1 : 0;
963 } else if (!strcasecmp(varname, "tlscertfile") || !strcasecmp(varname, "sslcert") || !strcasecmp(varname, "tlscert")) {
964 ast_free(tls_cfg->certfile);
965 tls_cfg->certfile = ast_strdup(value);
966 } else if (!strcasecmp(varname, "tlsprivatekey") || !strcasecmp(varname, "sslprivatekey")) {
967 ast_free(tls_cfg->pvtfile);
968 tls_cfg->pvtfile = ast_strdup(value);
969 } else if (!strcasecmp(varname, "tlscipher") || !strcasecmp(varname, "sslcipher")) {
970 ast_free(tls_cfg->cipher);
971 tls_cfg->cipher = ast_strdup(value);
972 } else if (!strcasecmp(varname, "tlscafile")) {
973 ast_free(tls_cfg->cafile);
974 tls_cfg->cafile = ast_strdup(value);
975 } else if (!strcasecmp(varname, "tlscapath") || !strcasecmp(varname, "tlscadir")) {
976 ast_free(tls_cfg->capath);
977 tls_cfg->capath = ast_strdup(value);
978 } else if (!strcasecmp(varname, "tlsverifyclient")) {
979 ast_set2_flag(&tls_cfg->flags, ast_true(value), AST_SSL_VERIFY_CLIENT);
980 } else if (!strcasecmp(varname, "tlsdontverifyserver")) {
981 ast_set2_flag(&tls_cfg->flags, ast_true(value), AST_SSL_DONT_VERIFY_SERVER);
982 } else if (!strcasecmp(varname, "tlsbindaddr") || !strcasecmp(varname, "sslbindaddr")) {
983 if (ast_parse_arg(value, PARSE_ADDR, &tls_desc->local_address))
984 ast_log(LOG_ERROR, "Invalid %s '%s'\n", varname, value);
985 } else if (!strcasecmp(varname, "tlsclientmethod") || !strcasecmp(varname, "sslclientmethod")) {
986 if (!strcasecmp(value, "tlsv1")) {
987 ast_set_flag(&tls_cfg->flags, AST_SSL_TLSV1_CLIENT);
988 ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV3_CLIENT);
989 ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV2_CLIENT);
990 } else if (!strcasecmp(value, "sslv3")) {
991 ast_set_flag(&tls_cfg->flags, AST_SSL_SSLV3_CLIENT);
992 ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV2_CLIENT);
993 ast_clear_flag(&tls_cfg->flags, AST_SSL_TLSV1_CLIENT);
994 } else if (!strcasecmp(value, "sslv2")) {
995 ast_set_flag(&tls_cfg->flags, AST_SSL_SSLV2_CLIENT);
996 ast_clear_flag(&tls_cfg->flags, AST_SSL_TLSV1_CLIENT);
997 ast_clear_flag(&tls_cfg->flags, AST_SSL_SSLV3_CLIENT);
998 }
999 } else if (!strcasecmp(varname, "tlsservercipherorder")) {
1000 ast_set2_flag(&tls_cfg->flags, ast_true(value), AST_SSL_SERVER_CIPHER_ORDER);
1001 } else if (!strcasecmp(varname, "tlsdisablev1")) {
1002 ast_set2_flag(&tls_cfg->flags, ast_true(value), AST_SSL_DISABLE_TLSV1);
1003 } else if (!strcasecmp(varname, "tlsdisablev11")) {
1004 ast_set2_flag(&tls_cfg->flags, ast_true(value), AST_SSL_DISABLE_TLSV11);
1005 } else if (!strcasecmp(varname, "tlsdisablev12")) {
1006 ast_set2_flag(&tls_cfg->flags, ast_true(value), AST_SSL_DISABLE_TLSV12);
1007 } else {
1008 return -1;
1009 }
1010
1011 return 0;
1012 }