36 #include <netinet/in.h> 38 #include <openssl/asn1.h> 39 #include <openssl/crypto.h> 40 #include <openssl/err.h> 41 #include <openssl/opensslconf.h> 42 #include <openssl/opensslv.h> 43 #include <openssl/safestack.h> 44 #include <openssl/ssl.h> 45 #include <openssl/x509.h> 46 #include <openssl/x509v3.h> 48 #include <openssl/bio.h> 49 #include <openssl/dh.h> 50 #include <openssl/pem.h> 53 #include <openssl/ec.h> 58 #include <sys/socket.h> 90 ret = ASN1_STRING_to_UTF8(&str, cert_str);
91 if (ret < 0 || !str) {
95 if (strlen((
char *) str) != ret) {
99 }
else if (!strcasecmp(hostname, (
char *) str)) {
105 ast_debug(3,
"SSL %s compare s1='%s' s2='%s'\n", desc, hostname, str);
117 fp = open_memstream(&buffer, &length);
122 ERR_print_errors_fp(fp);
153 ast_log(
LOG_ERROR,
"Failed to inhibit privilege escalations; killing connection from peer '%s'\n",
167 ast_log(
LOG_ERROR,
"Failed to set user interface status; killing connection from peer '%s'\n",
192 peer = SSL_get_peer_certificate(ssl);
201 res = SSL_get_verify_result(ssl);
202 if (res != X509_V_OK) {
205 X509_verify_cert_error_string(res));
213 X509_NAME *
name = X509_get_subject_name(peer);
214 STACK_OF(GENERAL_NAME) *alt_names;
221 pos = X509_NAME_get_index_by_NID(name, NID_commonName, pos);
225 str = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name, pos));
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);
237 for (pos = 0; pos < alt_names_count; pos++) {
238 const GENERAL_NAME *alt_name = sk_GENERAL_NAME_value(alt_names, pos);
240 if (alt_name->type != GEN_DNS) {
250 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
255 ast_log(
LOG_ERROR,
"Certificate common name from peer '%s' did not match (%s)\n",
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");
276 return tcptls_session;
303 &&
errno != EWOULDBLOCK
305 &&
errno != ECONNABORTED) {
307 if (
errno != EMFILE) {
316 if (!tcptls_session) {
330 if (!tcptls_session->
stream) {
339 tcptls_session->
client = 0;
343 ast_log(
LOG_ERROR,
"TCP/TLS unable to launch helper thread for peer '%s': %s\n",
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);
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);
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);
386 ast_log(
LOG_ERROR,
"TLS server failed: Asterisk is compiled without OpenSSL support. Install OpenSSL development headers and rebuild Asterisk after running ./configure\n");
407 #if !defined(OPENSSL_NO_SSL2) && (OPENSSL_VERSION_NUMBER < 0x10100000L) 409 ast_log(
LOG_WARNING,
"Usage of SSLv2 is discouraged due to known vulnerabilities. Please use 'tlsv1' or leave the TLS method unspecified!\n");
410 cfg->
ssl_ctx = SSL_CTX_new(SSLv2_client_method());
413 #if !defined(OPENSSL_NO_SSL3_METHOD) && !(defined(OPENSSL_API_COMPAT) && (OPENSSL_API_COMPAT >= 0x10100000L)) 415 ast_log(
LOG_WARNING,
"Usage of SSLv3 is discouraged due to known vulnerabilities. Please use 'tlsv1' or leave the TLS method unspecified!\n");
416 cfg->
ssl_ctx = SSL_CTX_new(SSLv3_client_method());
419 #if OPENSSL_VERSION_NUMBER >= 0x10100000L 420 cfg->
ssl_ctx = SSL_CTX_new(TLS_client_method());
423 cfg->
ssl_ctx = SSL_CTX_new(TLSv1_client_method());
426 cfg->
ssl_ctx = SSL_CTX_new(SSLv23_client_method());
431 cfg->
ssl_ctx = SSL_CTX_new(SSLv23_server_method());
435 ast_debug(1,
"Sorry, SSL_CTX_new call returned null...\n");
445 ssl_opts |= SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3;
449 ssl_opts |= SSL_OP_CIPHER_SERVER_PREFERENCE;
453 ssl_opts |= SSL_OP_NO_TLSv1;
455 #if defined(SSL_OP_NO_TLSv1_1) && defined(SSL_OP_NO_TLSv1_2) 457 ssl_opts |= SSL_OP_NO_TLSv1_1;
460 ssl_opts |= SSL_OP_NO_TLSv1_2;
464 "to the SSL BEAST attack. Please upgrade to OpenSSL 1.0.1 or later\n");
467 SSL_CTX_set_options(cfg->
ssl_ctx, ssl_opts);
469 SSL_CTX_set_verify(cfg->
ssl_ctx,
475 if (SSL_CTX_use_certificate_chain_file(cfg->
ssl_ctx, cfg->
certfile) == 0) {
486 if ((SSL_CTX_use_PrivateKey_file(cfg->
ssl_ctx, tmpprivate, SSL_FILETYPE_PEM) == 0) || (SSL_CTX_check_private_key(cfg->
ssl_ctx) == 0 )) {
489 ast_log(
LOG_ERROR,
"TLS/SSL error loading private key file. <%s>\n", tmpprivate);
498 size_t certfile_len = strlen(cfg->
certfile);
503 if (certfile_len >= 8 && !strncmp(cfg->
certfile + certfile_len - 8,
"_rsa.", 5)) {
510 if (SSL_CTX_set_cipher_list(cfg->
ssl_ctx, cfg->
cipher) == 0 ) {
528 #ifndef OPENSSL_NO_DH 530 BIO *bio = BIO_new_file(cfg->
pvtfile,
"r");
534 if (SSL_CTX_set_tmp_dh(cfg->
ssl_ctx, dh)) {
535 long options = SSL_OP_CIPHER_SERVER_PREFERENCE | SSL_OP_SINGLE_DH_USE | SSL_OP_SINGLE_ECDH_USE;
536 options = SSL_CTX_set_options(cfg->
ssl_ctx, options);
537 ast_verb(2,
"TLS/SSL DH initialized, PFS cipher-suites enabled\n");
546 #ifndef SSL_CTRL_SET_ECDH_AUTO 547 #define SSL_CTRL_SET_ECDH_AUTO 94 551 ast_verb(2,
"TLS/SSL ECDH initialized (automatic), faster PFS ciphers enabled\n");
552 #if !defined(OPENSSL_NO_ECDH) && (OPENSSL_VERSION_NUMBER >= 0x10000000L) && (OPENSSL_VERSION_NUMBER < 0x10100000L) 555 EC_KEY *ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
557 if (SSL_CTX_set_tmp_ecdh(cfg->
ssl_ctx, ecdh)) {
558 ast_verb(2,
"TLS/SSL ECDH initialized (secp256r1), faster PFS cipher-suites enabled\n");
565 ast_verb(2,
"TLS/SSL certificate ok\n");
589 if (!(desc = tcptls_session->
parent)) {
590 goto client_start_error;
598 goto client_start_error;
630 AF_INET6 : AF_INET, SOCK_STREAM, IPPROTO_TCP);
641 setsockopt(desc->
accept_fd, SOL_SOCKET, SO_REUSEADDR, &x,
sizeof(x));
652 if (!tcptls_session) {
660 tcptls_session->
client = 1;
662 if (!tcptls_session->
stream) {
676 return tcptls_session;
741 ast_debug(1,
"Changed parameters for %s found\n", desc->
name);
756 pthread_cancel(desc->
master);
757 pthread_kill(desc->
master, SIGURG);
763 if (sd_socket != -1) {
771 goto systemd_socket_activation;
785 AF_INET6 : AF_INET, SOCK_STREAM, 0);
791 setsockopt(desc->
accept_fd, SOL_SOCKET, SO_REUSEADDR, &x,
sizeof(x));
804 systemd_socket_activation:
841 if (tcptls_session->
stream) {
845 ast_debug(1,
"ast_tcptls_close_session_file invoked on session instance without file or file descriptor\n");
852 pthread_cancel(desc->
master);
853 pthread_kill(desc->
master, SIGURG);
877 if (!strcasecmp(varname,
"tlsenable") || !strcasecmp(varname,
"sslenable")) {
879 }
else if (!strcasecmp(varname,
"tlscertfile") || !strcasecmp(varname,
"sslcert") || !strcasecmp(varname,
"tlscert")) {
882 }
else if (!strcasecmp(varname,
"tlsprivatekey") || !strcasecmp(varname,
"sslprivatekey")) {
885 }
else if (!strcasecmp(varname,
"tlscipher") || !strcasecmp(varname,
"sslcipher")) {
888 }
else if (!strcasecmp(varname,
"tlscafile")) {
891 }
else if (!strcasecmp(varname,
"tlscapath") || !strcasecmp(varname,
"tlscadir")) {
894 }
else if (!strcasecmp(varname,
"tlsverifyclient")) {
896 }
else if (!strcasecmp(varname,
"tlsdontverifyserver")) {
898 }
else if (!strcasecmp(varname,
"tlsbindaddr") || !strcasecmp(varname,
"sslbindaddr")) {
901 }
else if (!strcasecmp(varname,
"tlsclientmethod") || !strcasecmp(varname,
"sslclientmethod")) {
902 if (!strcasecmp(value,
"tlsv1")) {
906 }
else if (!strcasecmp(value,
"sslv3")) {
910 }
else if (!strcasecmp(value,
"sslv2")) {
915 }
else if (!strcasecmp(varname,
"tlsservercipherorder")) {
917 }
else if (!strcasecmp(varname,
"tlsdisablev1")) {
919 }
else if (!strcasecmp(varname,
"tlsdisablev11")) {
921 }
else if (!strcasecmp(varname,
"tlsdisablev12")) {
void(* periodic_fn)(void *)
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
int ast_iostream_start_tls(struct ast_iostream **stream, SSL_CTX *ctx, int client)
Begin TLS on an iostream.
#define ast_set2_flag(p, value, flag)
#define ast_test_flag(p, flag)
static void ast_sockaddr_copy(struct ast_sockaddr *dst, const struct ast_sockaddr *src)
Copies the data from one ast_sockaddr to another.
struct ast_str * overflow_buf
#define ast_set_flag(p, flag)
#define ast_socket_nonblock(domain, type, protocol)
Create a non-blocking socket.
struct ast_tcptls_session_args * parent
#define SSL_CTRL_SET_ECDH_AUTO
static void * handle_tcptls_connection(void *data)
creates a FILE * from the fd passed by the accept thread. This operation is potentially expensive (ce...
struct ast_iostream * ast_iostream_from_fd(int *fd)
Create an iostream from a file descriptor.
arguments for the accepting thread
#define ast_strdup(str)
A wrapper for strdup()
int ast_sockaddr_cmp(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
Compares two ast_sockaddr structures.
I/O Management (derived from Cheops-NG)
Generic support for tcp/tls servers in Asterisk.
Socket address structure.
int ast_bind(int sockfd, const struct ast_sockaddr *addr)
Wrapper around bind(2) that uses struct ast_sockaddr.
#define ast_pthread_create_detached_background(a, b, c, d)
#define ast_verb(level,...)
char * ast_read_textfile(const char *file)
Read a file into asterisk.
int ast_sd_get_fd(int type, const struct ast_sockaddr *addr)
Find a listening file descriptor provided by socket activation.
static void ast_sockaddr_setnull(struct ast_sockaddr *addr)
Sets address addr to null.
#define ast_strlen_zero(foo)
#define ast_pthread_create_background(a, b, c, d)
SSL * ast_iostream_get_ssl(struct ast_iostream *stream)
Get a pointer to an iostream's OpenSSL SSL structure.
static int ast_sockaddr_isnull(const struct ast_sockaddr *addr)
Checks if the ast_sockaddr is null. "null" in this sense essentially means uninitialized, or having a 0 length.
Configuration File Parser.
void ast_tcptls_server_start(struct ast_tcptls_session_args *desc)
This is a generic (re)start routine for a TCP server, which does the socket/bind/listen and starts a ...
#define ast_debug(level,...)
Log a DEBUG message.
int ast_sockaddr_is_any(const struct ast_sockaddr *addr)
Determine if the address type is unspecified, or "any" address.
int ast_accept(int sockfd, struct ast_sockaddr *addr)
Wrapper around accept(2) that uses struct ast_sockaddr.
int ast_iostream_close(struct ast_iostream *stream)
Close an iostream.
static void write_openssl_error_to_log(void)
#define AST_PTHREADT_NULL
struct ast_sockaddr remote_address
#define ao2_ref(o, delta)
#define ast_strdupa(s)
duplicate a string in memory from the stack
static int __ssl_setup(struct ast_tls_config *cfg, int client)
Core PBX routines and definitions.
describes a server instance
void * ast_tcptls_server_root(void *data)
void ast_sha1_hash(char *output, const char *input)
Produces SHA1 hash based on input string.
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true". This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
static char * ast_sockaddr_stringify(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() with default format.
#define ao2_alloc(data_size, destructor_fn)
static void session_instance_destructor(void *obj)
General Definitions for Asterisk top level program Included by asterisk.h to handle platform-specific...
Generic abstraction for input/output streams.
#define ast_calloc(num, len)
A wrapper for calloc()
#define ast_fd_clear_flags(fd, flags)
Clear flags on the given file descriptor.
void *(* worker_fn)(void *)
int ast_parse_arg(const char *arg, enum ast_parse_flags flags, void *result,...)
The argument parsing routine.
struct ast_sockaddr old_address
struct ast_tls_config * old_tls_cfg
#define ast_clear_flag(p, flag)
Support for logging to various files, console and syslog Configuration in file logger.conf.
struct ast_iostream * stream
void ast_tcptls_close_session_file(struct ast_tcptls_session_instance *tcptls_session)
Closes a tcptls session instance's file and/or file descriptor. The tcptls_session will be set to NUL...
struct ast_tcptls_session_instance * ast_tcptls_client_create(struct ast_tcptls_session_args *desc)
int ast_ssl_setup(struct ast_tls_config *cfg)
Set up an SSL server.
void ast_ssl_teardown(struct ast_tls_config *cfg)
free resources used by an SSL server
int ast_tls_read_conf(struct ast_tls_config *tls_cfg, struct ast_tcptls_session_args *tls_desc, const char *varname, const char *value)
Used to parse conf files containing tls/ssl options.
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
struct ast_tcptls_session_instance * ast_tcptls_client_start(struct ast_tcptls_session_instance *tcptls_session)
attempts to connect and start tcptls session, on error the tcptls_session's ref count is decremented...
int ast_thread_user_interface_set(int is_user_interface)
Set the current thread's user interface status.
int ast_thread_inhibit_escalations(void)
Inhibit (in the current thread) the execution of dialplan functions which cause privilege escalations...
static struct ast_str * hostname
static int check_tcptls_cert_name(ASN1_STRING *cert_str, const char *hostname, const char *desc)
int error(const char *format,...)
int ast_wait_for_input(int fd, int ms)
struct ast_tls_config * tls_cfg
struct ast_sockaddr local_address
void ast_tcptls_server_stop(struct ast_tcptls_session_args *desc)
Shutdown a running server if there is one.
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
int ast_sockaddr_is_ipv6(const struct ast_sockaddr *addr)
Determine if this is an IPv6 address.
int ast_connect(int sockfd, const struct ast_sockaddr *addr)
Wrapper around connect(2) that uses struct ast_sockaddr.
char hostname[MAXHOSTNAMELEN]
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
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)
struct ast_sockaddr remote_address
void *(* accept_fn)(void *)