From 76a91fca2fc7dd5975e8ff0e6695f27c98539dba Mon Sep 17 00:00:00 2001 From: hrxi Date: Fri, 24 Nov 2023 20:53:50 +0100 Subject: Add support for OpenSSL Uses work by @hrxi in https://github.com/dino/dino/pull/1515 Co-Authored-By: hrxi --- plugins/ice/src/module.vala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins/ice/src/module.vala') diff --git a/plugins/ice/src/module.vala b/plugins/ice/src/module.vala index 2645d7dc..ace62108 100644 --- a/plugins/ice/src/module.vala +++ b/plugins/ice/src/module.vala @@ -42,7 +42,7 @@ public class Dino.Plugins.Ice.Module : JingleIceUdp.Module { private DtlsSrtp.CredentialsCapsule? get_create_credentials(Jid local_full_jid, Jid peer_full_jid) { string from_to_id = local_full_jid.to_string() + peer_full_jid.to_string(); try { - if (!cerds.has_key(from_to_id)) cerds[from_to_id] = DtlsSrtp.Handler.generate_credentials(); + if (!cerds.has_key(from_to_id)) cerds[from_to_id] = DtlsSrtp.generate_credentials(); } catch (Error e) { warning("Error creating dtls credentials: %s", e.message); } -- cgit v1.3 From 49f4ae03b742e73b6efdf9edf50dee92ebae1448 Mon Sep 17 00:00:00 2001 From: Marvin W Date: Mon, 24 Aug 2026 11:48:22 +0200 Subject: Move DLTS-SRTP into libdino --- libdino/meson.build | 6 +- libdino/src/crypto/dtls_srtp.vala | 146 +++++++++++ libdino/src/crypto/gnu/dtls_srtp.vala | 245 +++++++++++++++++ libdino/src/crypto/openssl/dtls_srtp.vala | 202 ++++++++++++++ libdino/src/crypto/openssl/utils.c | 49 ++++ libdino/src/crypto/openssl/utils.h | 8 + libdino/vapi/gnutls.vapi | 420 ++++++++++++++++++++++++++++++ libdino/vapi/openssl.vapi | 2 +- plugins/ice/meson.build | 13 - plugins/ice/src/dtls_srtp.vala | 146 ----------- plugins/ice/src/dtls_srtp_gnutls.vala | 245 ----------------- plugins/ice/src/dtls_srtp_openssl.vala | 202 -------------- plugins/ice/src/module.vala | 4 +- plugins/ice/src/openssl_utils.c | 49 ---- plugins/ice/src/openssl_utils.h | 8 - plugins/ice/src/transport_parameters.vala | 2 + plugins/ice/vapi/gnutls.vapi | 420 ------------------------------ plugins/ice/vapi/openssl.vapi | 218 ---------------- 18 files changed, 1081 insertions(+), 1304 deletions(-) create mode 100644 libdino/src/crypto/dtls_srtp.vala create mode 100644 libdino/src/crypto/gnu/dtls_srtp.vala create mode 100644 libdino/src/crypto/openssl/dtls_srtp.vala create mode 100644 libdino/src/crypto/openssl/utils.c create mode 100644 libdino/src/crypto/openssl/utils.h create mode 100644 libdino/vapi/gnutls.vapi delete mode 100644 plugins/ice/src/dtls_srtp.vala delete mode 100644 plugins/ice/src/dtls_srtp_gnutls.vala delete mode 100644 plugins/ice/src/dtls_srtp_openssl.vala delete mode 100644 plugins/ice/src/openssl_utils.c delete mode 100644 plugins/ice/src/openssl_utils.h delete mode 100644 plugins/ice/vapi/gnutls.vapi delete mode 100644 plugins/ice/vapi/openssl.vapi (limited to 'plugins/ice/src/module.vala') diff --git a/libdino/meson.build b/libdino/meson.build index 5c7c20c4..67ac6a83 100644 --- a/libdino/meson.build +++ b/libdino/meson.build @@ -19,6 +19,7 @@ sources = files( 'src/dbus/upower.vala', 'src/crypto/cipher.vala', 'src/crypto/cipher_converter.vala', + 'src/crypto/dtls_srtp.vala', 'src/crypto/error.vala', 'src/crypto/srtp.vala', 'src/entity/account.vala', @@ -92,15 +93,18 @@ sources = files( sources += [version_c] if crypto_backend == 'gnu' - dependencies += [ dep_libgcrypt ] + dependencies += [ dep_libgcrypt, dep_gnutls ] sources += files( 'src/crypto/gnu/cipher.vala', + 'src/crypto/gnu/dtls_srtp.vala', 'src/crypto/gnu/utils.vala', ) elif crypto_backend == 'openssl' dependencies += [ dep_openssl ] sources += files( 'src/crypto/openssl/cipher.vala', + 'src/crypto/openssl/dtls_srtp.vala', + 'src/crypto/openssl/utils.c', 'src/crypto/openssl/utils.vala', ) endif diff --git a/libdino/src/crypto/dtls_srtp.vala b/libdino/src/crypto/dtls_srtp.vala new file mode 100644 index 00000000..884f1a43 --- /dev/null +++ b/libdino/src/crypto/dtls_srtp.vala @@ -0,0 +1,146 @@ +namespace Dino.Crypto.DtlsSrtp { + +public abstract class CredentialsCapsule : Object { + public uint8[] own_fingerprint; +} + +public abstract class AbstractHandler { + + public signal void send_data(uint8[] data); + + public bool ready { get { + return srtp_session.has_encrypt && srtp_session.has_decrypt; + }} + + public Mode mode { get; set; default = Mode.CLIENT; } + public uint8[] own_fingerprint { get; private set; } + public uint8[] peer_fingerprint { get; set; } + public string peer_fp_algo { get; set; } + + protected CredentialsCapsule credentials; + protected Cond buffer_cond = Cond(); + protected Mutex buffer_mutex = Mutex(); + protected Gee.LinkedList buffer_queue = new Gee.LinkedList(); + + protected bool running = false; + protected bool stop = false; + protected bool restart = false; + + protected Crypto.Srtp.Session srtp_session = new Crypto.Srtp.Session(); + + protected AbstractHandler.with_cert(CredentialsCapsule creds) { + this.credentials = creds; + this.own_fingerprint = creds.own_fingerprint; + } + + public uint8[]? process_incoming_data(uint component_id, uint8[] data) throws Crypto.Error { + if (data[0] >= 128) { + if (!srtp_session.has_decrypt) { + debug("Received data before SRTP session is ready, dropping."); + return null; + } + if (component_id == 1) { + if (data.length >= 2 && data[1] >= 192 && data[1] < 224) { + return srtp_session.decrypt_rtcp(data); + } + return srtp_session.decrypt_rtp(data); + } + if (component_id == 2) return srtp_session.decrypt_rtcp(data); + } + if (component_id == 1 && data.length >= 1 && (data[0] >= 20 && data[0] < 64)) { + on_data_rec(data); + return null; + } + debug("Dropping unknown data from component %u", component_id); + return null; + } + + public uint8[]? process_outgoing_data(uint component_id, uint8[] data) throws Crypto.Error { + if (srtp_session.has_encrypt) { + if (component_id == 1) { + if (data.length >= 2 && data[1] >= 192 && data[1] < 224) { + return srtp_session.encrypt_rtcp(data); + } + return srtp_session.encrypt_rtp(data); + } + if (component_id == 2) return srtp_session.encrypt_rtcp(data); + } + return null; + } + + public void on_data_rec(owned uint8[] data) { + buffer_mutex.lock(); + buffer_queue.add(new Bytes.take(data)); + buffer_cond.signal(); + buffer_mutex.unlock(); + } + + public void stop_dtls_connection() { + buffer_mutex.lock(); + stop = true; + buffer_cond.signal(); + buffer_mutex.unlock(); + } + + protected Bytes get_buffer_if_present () { + buffer_mutex.lock(); + if (buffer_queue.size == 0) { + buffer_mutex.unlock(); + return null; + } + Bytes res = buffer_queue.remove_at(0); + buffer_mutex.unlock(); + return res; + } + + public async Xmpp.Xep.Jingle.ContentEncryption? setup_dtls_connection() { + MainContext context = MainContext.current_source().get_context(); + var thread = new Thread("dtls-connection", () => { + // Check we can start working on the thread + buffer_mutex.lock(); + if (stop) { + restart = true; + buffer_mutex.unlock(); + return null; + } + if (running || ready) { + buffer_mutex.unlock(); + return null; + } + running = true; + restart = false; + buffer_mutex.unlock(); + + // Actual work + var res = setup_dtls_connection_thread(); + + // Callback + Source source = new IdleSource(); + source.set_callback(setup_dtls_connection.callback); + source.attach(context); + return res; + }); + yield; + return thread.join(); + } + + protected abstract Xmpp.Xep.Jingle.ContentEncryption? setup_dtls_connection_thread(); +} + +private string format_fingerprint(uint8[] fingerprint) { + var sb = new StringBuilder(); + for (int i = 0; i < fingerprint.length; i++) { + sb.append("%02X".printf(fingerprint[i])); + if (i < fingerprint.length - 1) { + sb.append(":"); + } + } + return sb.str; +} + + +public enum Mode { + CLIENT, SERVER +} + +} diff --git a/libdino/src/crypto/gnu/dtls_srtp.vala b/libdino/src/crypto/gnu/dtls_srtp.vala new file mode 100644 index 00000000..f6f0a24e --- /dev/null +++ b/libdino/src/crypto/gnu/dtls_srtp.vala @@ -0,0 +1,245 @@ +using GnuTLS; + +namespace Dino.Crypto.DtlsSrtp { + +public class CredentialsCapsuleGnuTLS : CredentialsCapsule { + internal X509.Certificate[] own_cert; + internal X509.PrivateKey private_key; +} + +private uint8[] get_fingerprint(X509.Certificate certificate, DigestAlgorithm digest_algo) { + uint8[] buf = new uint8[512]; + size_t buf_out_size = 512; + certificate.get_fingerprint(digest_algo, buf, ref buf_out_size); + + uint8[] ret = new uint8[buf_out_size]; + for (int i = 0; i < buf_out_size; i++) { + ret[i] = buf[i]; + } + return ret; +} + +public CredentialsCapsule generate_credentials () throws GLib.Error { + int err = 0; + + X509.PrivateKey private_key = X509.PrivateKey.create (); + err = private_key.generate (PKAlgorithm.ECDSA, 256); + throw_if_error (err); + + var start_time = new DateTime.now_local().add_days(-1); + var end_time = start_time.add_days (2); + + X509.Certificate cert = X509.Certificate.create (); + cert.set_key (private_key); + cert.set_version (1); + cert.set_activation_time ((time_t) start_time.to_unix ()); + cert.set_expiration_time ((time_t) end_time.to_unix ()); + + uint32 serial = 1; + cert.set_serial (&serial, sizeof(uint32)); + + cert.sign (cert, private_key); + + uint8[] own_fingerprint = get_fingerprint (cert, DigestAlgorithm.SHA256); + X509.Certificate[] own_cert = new X509.Certificate[] { (owned)cert }; + + var creds = new CredentialsCapsuleGnuTLS(); + creds.own_fingerprint = own_fingerprint; + creds.own_cert = (owned) own_cert; + creds.private_key = (owned) private_key; + + return creds; +} + +public class Handler : AbstractHandler { + + public Handler.with_cert(CredentialsCapsule creds) { + base.with_cert(creds); + } + + protected override Xmpp.Xep.Jingle.ContentEncryption? setup_dtls_connection_thread() { + InitFlags server_or_client = mode == Mode.SERVER ? InitFlags.SERVER : InitFlags.CLIENT; + debug("Setting up DTLS connection using GnuTLS. We're %s", mode.to_string()); + + CertificateCredentials cert_cred = CertificateCredentials.create(); + int err = cert_cred.set_x509_key(((CredentialsCapsuleGnuTLS)credentials).own_cert, ((CredentialsCapsuleGnuTLS)credentials).private_key); + throw_if_error(err); + + Session? session = Session.create(server_or_client | InitFlags.DATAGRAM); + session.enable_heartbeat(1); + session.set_srtp_profile_direct("SRTP_AES128_CM_HMAC_SHA1_80"); + session.set_credentials(GnuTLS.CredentialsType.CERTIFICATE, cert_cred); + session.server_set_request(CertificateRequest.REQUEST); + session.set_priority_from_string("NORMAL:!VERS-TLS-ALL:+VERS-DTLS-ALL:+CTYPE-CLI-X509"); + + session.set_transport_pointer(this); + session.set_pull_function(pull_function); + session.set_pull_timeout_function(pull_timeout_function); + session.set_push_function(push_function); + session.set_verify_function(verify_function); + + DateTime maximum_time = new DateTime.now_utc().add_seconds(20); + do { + err = session.handshake(); + + DateTime current_time = new DateTime.now_utc(); + if (maximum_time.compare(current_time) < 0) { + warning("DTLS handshake timeouted"); + err = ErrorCode.APPLICATION_ERROR_MIN + 1; + break; + } + if (stop) { + debug("DTLS handshake stopped"); + err = ErrorCode.APPLICATION_ERROR_MIN + 2; + break; + } + } while (err < 0 && !((ErrorCode)err).is_fatal()); + + buffer_mutex.lock(); + if (stop) { + stop = false; + running = false; + bool restart = restart; + buffer_mutex.unlock(); + if (restart) { + debug("Restarting DTLS handshake"); + return setup_dtls_connection_thread(); + } + return null; + } + buffer_mutex.unlock(); + if (err != ErrorCode.SUCCESS) { + warning("DTLS handshake failed: %s", ((ErrorCode)err).to_string()); + return null; + } + + uint8[] km = new uint8[150]; + Datum? client_key, client_salt, server_key, server_salt; + session.get_srtp_keys(km, km.length, out client_key, out client_salt, out server_key, out server_salt); + if (client_key == null || client_salt == null || server_key == null || server_salt == null) { + warning("SRTP client/server key/salt null"); + } + + debug("Finished DTLS connection. We're %s", mode.to_string()); + if (mode == Mode.SERVER) { + srtp_session.set_encryption_key(Crypto.Srtp.AES_CM_128_HMAC_SHA1_80, server_key.extract(), server_salt.extract()); + srtp_session.set_decryption_key(Crypto.Srtp.AES_CM_128_HMAC_SHA1_80, client_key.extract(), client_salt.extract()); + } else { + srtp_session.set_encryption_key(Crypto.Srtp.AES_CM_128_HMAC_SHA1_80, client_key.extract(), client_salt.extract()); + srtp_session.set_decryption_key(Crypto.Srtp.AES_CM_128_HMAC_SHA1_80, server_key.extract(), server_salt.extract()); + } + return new Xmpp.Xep.Jingle.ContentEncryption(Xmpp.Xep.JingleIceUdp.DTLS_NS_URI, "DTLS-SRTP", credentials.own_fingerprint, peer_fingerprint); + } + + private static ssize_t pull_function(void* transport_ptr, uint8[] buffer) { + Handler self = transport_ptr as Handler; + + self.buffer_mutex.lock(); + while (self.buffer_queue.size == 0) { + self.buffer_cond.wait(self.buffer_mutex); + if (self.stop) { + self.buffer_mutex.unlock(); + debug("DTLS handshake pull_function stopped"); + return -1; + } + } + Bytes data = self.buffer_queue.remove_at(0); + self.buffer_mutex.unlock(); + + uint8[] data_uint8 = Bytes.unref_to_data((owned) data); + Memory.copy(buffer, data_uint8, data_uint8.length); + + // The callback should return 0 on connection termination, a positive number indicating the number of bytes received, and -1 on error. + return (ssize_t)data_uint8.length; + } + + private static int pull_timeout_function(void* transport_ptr, uint ms) { + Handler self = transport_ptr as Handler; + + int64 end_time = get_monotonic_time() + ms * 1000; + + self.buffer_mutex.lock(); + while (self.buffer_queue.size == 0) { + self.buffer_cond.wait_until(self.buffer_mutex, end_time); + if (self.stop) { + self.buffer_mutex.unlock(); + debug("DTLS handshake pull_timeout_function stopped"); + return -1; + } + + if (get_monotonic_time() > end_time) { + self.buffer_mutex.unlock(); + return 0; + } + } + self.buffer_mutex.unlock(); + + // The callback should return 0 on timeout, a positive number if data can be received, and -1 on error. + return 1; + } + + private static ssize_t push_function(void* transport_ptr, uint8[] buffer) { + Handler self = transport_ptr as Handler; + self.send_data(buffer); + + // The callback should return a positive number indicating the bytes sent, and -1 on error. + return (ssize_t)buffer.length; + } + + private static int verify_function(Session session) { + Handler self = session.get_transport_pointer() as Handler; + try { + bool valid = self.verify_peer_cert(session); + if (!valid) { + warning("DTLS certificate invalid. Aborting handshake."); + return 1; + } + } catch (Error e) { + warning("Error during DTLS certificate validation: %s. Aborting handshake.", e.message); + return 1; + } + + // The callback function should return 0 for the handshake to continue or non-zero to terminate. + return 0; + } + + private bool verify_peer_cert(Session session) throws GLib.Error { + unowned Datum[] cert_datums = session.get_peer_certificates(); + if (cert_datums.length == 0) { + warning("No peer certs"); + return false; + } + if (cert_datums.length > 1) warning("More than one peer cert"); + + X509.Certificate peer_cert = X509.Certificate.create(); + peer_cert.import(ref cert_datums[0], CertificateFormat.DER); + + DigestAlgorithm algo; + switch (peer_fp_algo) { + case "sha-256": + algo = DigestAlgorithm.SHA256; + break; + default: + warning("Unkown peer fingerprint algorithm: %s", peer_fp_algo); + return false; + } + + uint8[] real_peer_fp = get_fingerprint(peer_cert, algo); + + if (real_peer_fp.length != this.peer_fingerprint.length) { + warning("Fingerprint lengths not equal %i vs %i", real_peer_fp.length, peer_fingerprint.length); + return false; + } + + for (int i = 0; i < real_peer_fp.length; i++) { + if (real_peer_fp[i] != this.peer_fingerprint[i]) { + warning("First cert in peer cert list doesn't equal advertised one: %s vs %s", format_fingerprint(real_peer_fp), format_fingerprint(peer_fingerprint)); + return false; + } + } + + return true; + } +} + +} \ No newline at end of file diff --git a/libdino/src/crypto/openssl/dtls_srtp.vala b/libdino/src/crypto/openssl/dtls_srtp.vala new file mode 100644 index 00000000..192f0da2 --- /dev/null +++ b/libdino/src/crypto/openssl/dtls_srtp.vala @@ -0,0 +1,202 @@ +using OpenSSL; + +namespace Dino.Crypto.DtlsSrtp { + +public class CredentialsCapsuleOpenSSL : CredentialsCapsule { + internal X509.Certificate own_cert; + internal EVP.Key private_key; +} + +public CredentialsCapsule generate_credentials () throws GLib.Error { + EVP.Key key = new EVP.Key.generate_ecdsa ("prime256v1"); + + var start_time = new DateTime.now_local().add_days(-1); + var end_time = start_time.add_days (2); + + X509.Certificate cert = new X509.Certificate (); + cert.set_public_key (key); + cert.set_version (1); + cert.not_before.set ((time_t) start_time.to_unix ()); + cert.not_after.set ((time_t) end_time.to_unix ()); + cert.serial_number.set (1); + + cert.sign (key, EVP.sha256 ()); + + CredentialsCapsuleOpenSSL credentials = new CredentialsCapsuleOpenSSL (); + credentials.own_fingerprint = cert.digest (EVP.sha256 ()); + credentials.own_cert = (owned) cert; + credentials.private_key = (owned) key; + return credentials; +} + +public class Handler : AbstractHandler { + + public Handler.with_cert (CredentialsCapsule creds) { + base.with_cert (creds); + } + + protected override Xmpp.Xep.Jingle.ContentEncryption? setup_dtls_connection_thread () { + debug("Setting up DTLS connection using OpenSSL. We're %s", mode.to_string()); + SSL.Context context = new SSL.Context (mode == Mode.SERVER ? SSL.Method.DTLS_SERVER : SSL.Method.DTLS_CLIENT); + context.set_tlsext_use_srtp ("SRTP_AES128_CM_SHA1_80"); + context.use_certificate (((CredentialsCapsuleOpenSSL) credentials).own_cert); + context.use_private_key (((CredentialsCapsuleOpenSSL) credentials).private_key); + SSL session = new SSL (context); + BIO bio = new BIO(get_handler_based_bio_method()); + bio.data = this; + session.rbio = bio; + session.wbio = bio; + + DateTime maximum_time = new DateTime.now_utc().add_seconds(20); + int err = 0; + if (mode == Mode.SERVER) { + session.set_accept_state (); + } else { + session.set_connect_state (); + } + do { + try { + SSL.Error ret = session.do_handshake(); + switch (ret) { + case SSL.Error.NONE: + err = 1; + break; + case SSL.Error.WANT_READ: + debug ("DTLS wants to read, but nothing in queue, wait for buffer"); + buffer_mutex.lock(); + if (buffer_queue.size == 0) { + buffer_cond.wait_until(buffer_mutex, get_monotonic_time() + maximum_time.difference(new DateTime.now_utc())); + } + buffer_mutex.unlock(); + err = -1; + break; + default: + warning ("DTLS Error: %s", @"$ret"); + err = 4; + break; + } + } catch (Error e) { + warning ("DTLS Error: %s", e.message); + err = e.code; + } + + DateTime current_time = new DateTime.now_utc(); + if (maximum_time.compare(current_time) < 0) { + debug ("DTLS handshake timeouted"); + err = 2; + break; + } + if (stop) { + debug ("DTLS handshake stopped"); + err = 3; + break; + } + } while (err < 0); + + buffer_mutex.lock(); + if (stop) { + stop = false; + running = false; + bool restart = restart; + buffer_mutex.unlock(); + if (restart) { + debug ("Restarting DTLS handshake"); + return setup_dtls_connection_thread(); + } + return null; + } + buffer_mutex.unlock(); + if (err != 1) { + debug ("DTLS handshake failed"); + return null; + } + + // TODO: Check peer certificate + + uint8[] km = new uint8[60]; + session.export_keying_material (km, "EXTRACTOR-dtls_srtp"); + uint8[] client_key = km[0:16]; + uint8[] server_key = km[16:32]; + uint8[] client_salt = km[32:46]; + uint8[] server_salt = km[46:60]; + + debug ("Finished DTLS connection. We're %s", mode.to_string()); + if (mode == Mode.SERVER) { + srtp_session.set_encryption_key (Crypto.Srtp.AES_CM_128_HMAC_SHA1_80, server_key, server_salt); + srtp_session.set_decryption_key (Crypto.Srtp.AES_CM_128_HMAC_SHA1_80, client_key, client_salt); + } else { + srtp_session.set_encryption_key (Crypto.Srtp.AES_CM_128_HMAC_SHA1_80, client_key, client_salt); + srtp_session.set_decryption_key (Crypto.Srtp.AES_CM_128_HMAC_SHA1_80, server_key, server_salt); + } + return new Xmpp.Xep.Jingle.ContentEncryption (Xmpp.Xep.JingleIceUdp.DTLS_NS_URI, "DTLS-SRTP", credentials.own_fingerprint, peer_fingerprint); + } + + private int read_impl (BIO bio, uint8[] out_buf) { + Bytes data = get_buffer_if_present (); + if (data == null) { + if (!stop) { + debug ("No pending buffer in queue, but end not reached yet, set_retry_read"); + bio.set_retry_read (); + } + return -1; + } + if (data.length > out_buf.length) { + warning ("Buffer in queue does not fit in out_buf"); + return -1; + } + debug ("Read %d bytes from queue", data.length); + + uint8[] data_uint8 = Bytes.unref_to_data((owned) data); + Memory.copy (out_buf, data_uint8, data_uint8.length); + bio.clear_retry_flags (); + return data_uint8.length; + } + + private int write_impl (BIO bio, uint8[] buf) { + debug ("Write %d bytes to network", buf.length); + send_data (buf); + return buf.length; + } + + private long ctrl_impl (BIO bio, BIO.ControlCommand cmd, long num, void* ptr) { + switch (cmd) { + case BIO.ControlCommand.FLUSH: + // Datagrams are sent out immediately + return 1; + case BIO.ControlCommand.DGRAM_SET_NEXT_TIMEOUT: + // Datagrams are sent out immediately + return 1; + case BIO.ControlCommand.DGRAM_GET_MTU_OVERHEAD: + // Just some safe estimate + return 70; + case BIO.ControlCommand.DGRAM_QUERY_MTU: + // Just some safe estimate + return 1200; + case BIO.ControlCommand.WPENDING: + // Datagrams are sent out immediately + return 0; + case BIO.ControlCommand.POP: + case BIO.ControlCommand.PUSH: + // Ignored + return 0; + default: + warning (@"Unimplemented ctrl cmd:$cmd, num:$num"); + return 0; + } + } + + private static BIO.Method _handler_based_bio_method_instance = null; + + private static unowned BIO.Method get_handler_based_bio_method () { + if (_handler_based_bio_method_instance != null) return _handler_based_bio_method_instance; + _handler_based_bio_method_instance = new BIO.Method (BIO.Type.SOURCE_SINK | BIO.Type.DGRAM, "Dino DTLS Handler BIO"); + _handler_based_bio_method_instance.read = (bio, out_buf) => bio.data.read_impl (bio, out_buf); + _handler_based_bio_method_instance.write = (bio, buf) => bio.data.write_impl (bio, buf); + _handler_based_bio_method_instance.ctrl = (bio, cmd, num, ptr) => bio.data.ctrl_impl (bio, cmd, num, ptr); + return _handler_based_bio_method_instance; + } +} + + + +} \ No newline at end of file diff --git a/libdino/src/crypto/openssl/utils.c b/libdino/src/crypto/openssl/utils.c new file mode 100644 index 00000000..1a84f642 --- /dev/null +++ b/libdino/src/crypto/openssl/utils.c @@ -0,0 +1,49 @@ +#include "utils.h" +#include +#include + +#if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x30000000L +// This is for OpenSSL 3.0.0 and newer + +EVP_PKEY *EVP_PKEY_new_generate_ecdsa (const char* curve) { + // Generate EVP_PKEY directly + EVP_PKEY *pkey = EVP_EC_gen (curve); + return pkey; +} + +#else +// This is for OpenSSL before 3.0.0 and forks of earlier OpenSSL versions like LibreSSL + +EVP_PKEY *EVP_PKEY_new_generate_ecdsa (const char* curve) { + // Get NID from curve name + int nid = OBJ_sn2nid (curve); + if (nid == NID_undef) return NULL; + + // Create EC_KEY + EC_KEY *ec_key = EC_KEY_new_by_curve_name (nid); + if (!ec_key) return NULL; + + // Generate key + if (!EC_KEY_generate_key (ec_key)) { + EC_KEY_free (ec_key); + return NULL; + } + + // Create EVP_PKEY + EVP_PKEY *pkey = EVP_PKEY_new (); + if (!pkey) { + EC_KEY_free (ec_key); + return NULL; + } + + // Assign EC_KEY to EVP_PKEY + if (!EVP_PKEY_assign_EC_KEY (pkey, ec_key)) { + EC_KEY_free (ec_key); + return NULL; + } + + // EVP_PKEY_assign_EC_KEY takes ownership of ec_key, no need to free it explicitly + return pkey; +} + +#endif diff --git a/libdino/src/crypto/openssl/utils.h b/libdino/src/crypto/openssl/utils.h new file mode 100644 index 00000000..bcc6f53a --- /dev/null +++ b/libdino/src/crypto/openssl/utils.h @@ -0,0 +1,8 @@ +#ifndef DINO_OPENSSL_UTILS_H +#define DINO_OPENSSL_UTILS_H + +#include + +EVP_PKEY *EVP_PKEY_new_generate_ecdsa (const char* curve); + +#endif //DINO_OPENSSL_UTILS_H \ No newline at end of file diff --git a/libdino/vapi/gnutls.vapi b/libdino/vapi/gnutls.vapi new file mode 100644 index 00000000..86a8c401 --- /dev/null +++ b/libdino/vapi/gnutls.vapi @@ -0,0 +1,420 @@ +[CCode (cprefix = "gnutls_", lower_case_cprefix = "gnutls_", cheader_filename = "gnutls/gnutls.h")] +namespace GnuTLS { + + public int global_init(); + + [CCode (cname = "gnutls_pull_func", has_target = false)] + public delegate ssize_t PullFunc(void* transport_ptr, [CCode (ctype = "void*", array_length_type="size_t")] uint8[] array); + + [CCode (cname = "gnutls_pull_timeout_func", has_target = false)] + public delegate int PullTimeoutFunc(void* transport_ptr, uint ms); + + [CCode (cname = "gnutls_push_func", has_target = false)] + public delegate ssize_t PushFunc(void* transport_ptr, [CCode (ctype = "void*", array_length_type="size_t")] uint8[] array); + + [CCode (cname = "gnutls_certificate_verify_function", has_target = false)] + public delegate int VerifyFunc(Session session); + + [Compact] + [CCode (cname = "struct gnutls_session_int", free_function = "gnutls_deinit")] + public class Session { + + public static Session? create(int con_end) throws GLib.Error { + Session result; + var ret = init(out result, con_end); + throw_if_error(ret); + return result; + } + + [CCode (cname = "gnutls_init")] + private static int init(out Session session, int con_end); + + [CCode (cname = "gnutls_transport_set_push_function")] + public void set_push_function(PushFunc func); + + [CCode (cname = "gnutls_transport_set_pull_function")] + public void set_pull_function(PullFunc func); + + [CCode (cname = "gnutls_transport_set_pull_timeout_function")] + public void set_pull_timeout_function(PullTimeoutFunc func); + + [CCode (cname = "gnutls_transport_set_ptr")] + public void set_transport_pointer(void* ptr); + + [CCode (cname = "gnutls_transport_get_ptr")] + public void* get_transport_pointer(); + + [CCode (cname = "gnutls_heartbeat_enable")] + public int enable_heartbeat(uint type); + + [CCode (cname = "gnutls_certificate_server_set_request")] + public void server_set_request(CertificateRequest req); + + [CCode (cname = "gnutls_credentials_set")] + public int set_credentials_(CredentialsType type, void* cred); + [CCode (cname = "gnutls_credentials_set_")] + public void set_credentials(CredentialsType type, void* cred) throws GLib.Error { + int err = set_credentials_(type, cred); + throw_if_error(err); + } + + [CCode (cname = "gnutls_priority_set_direct")] + public int set_priority_from_string_(string priority, out unowned string err_pos = null); + [CCode (cname = "gnutls_priority_set_direct_")] + public void set_priority_from_string(string priority, out unowned string err_pos = null) throws GLib.Error { + int err = set_priority_from_string_(priority, out err_pos); + throw_if_error(err); + } + + [CCode (cname = "gnutls_srtp_set_profile_direct")] + public int set_srtp_profile_direct_(string profiles, out unowned string err_pos = null); + [CCode (cname = "gnutls_srtp_set_profile_direct_")] + public void set_srtp_profile_direct(string profiles, out unowned string err_pos = null) throws GLib.Error { + int err = set_srtp_profile_direct_(profiles, out err_pos); + throw_if_error(err); + } + + [CCode (cname = "gnutls_transport_set_int")] + public void transport_set_int(int fd); + + [CCode (cname = "gnutls_handshake")] + public int handshake(); + + [CCode (cname = "gnutls_srtp_get_keys")] + public int get_srtp_keys_(void *key_material, uint32 key_material_size, out Datum client_key, out Datum client_salt, out Datum server_key, out Datum server_salt); + [CCode (cname = "gnutls_srtp_get_keys_")] + public void get_srtp_keys(void *key_material, uint32 key_material_size, out Datum client_key, out Datum client_salt, out Datum server_key, out Datum server_salt) throws GLib.Error { + get_srtp_keys_(key_material, key_material_size, out client_key, out client_salt, out server_key, out server_salt); + } + + [CCode (cname = "gnutls_certificate_get_peers", array_length_type = "unsigned int")] + public unowned Datum[]? get_peer_certificates(); + + [CCode (cname = "gnutls_session_set_verify_function")] + public void set_verify_function(VerifyFunc func); + } + + [Compact] + [CCode (cname = "struct gnutls_certificate_credentials_st", free_function = "gnutls_certificate_free_credentials", cprefix = "gnutls_certificate_")] + public class CertificateCredentials { + + [CCode (cname = "gnutls_certificate_allocate_credentials")] + private static int allocate(out CertificateCredentials credentials); + + public static CertificateCredentials create() throws GLib.Error { + CertificateCredentials result; + var ret = allocate (out result); + throw_if_error(ret); + return result; + } + + public void get_x509_crt(uint index, [CCode (array_length_type = "unsigned int")] out unowned X509.Certificate[] x509_ca_list); + + public int set_x509_key(X509.Certificate[] cert_list, X509.PrivateKey key); + } + + [CCode (cheader_filename = "gnutls/x509.h", cprefix = "GNUTLS_")] + namespace X509 { + + [Compact] + [CCode (cname = "struct gnutls_x509_crt_int", cprefix = "gnutls_x509_crt_", free_function = "gnutls_x509_crt_deinit")] + public class Certificate { + + [CCode (cname = "gnutls_x509_crt_init")] + private static int init (out Certificate cert); + public static Certificate create() throws GLib.Error { + Certificate result; + var ret = init (out result); + throw_if_error(ret); + return result; + } + + [CCode (cname = "gnutls_x509_crt_import")] + public int import_(ref Datum data, CertificateFormat format); + [CCode (cname = "gnutls_x509_crt_import_")] + public void import(ref Datum data, CertificateFormat format) throws GLib.Error { + int err = import_(ref data, format); + throw_if_error(err); + } + + [CCode (cname = "gnutls_x509_crt_set_version")] + public int set_version_(uint version); + [CCode (cname = "gnutls_x509_crt_set_version_")] + public void set_version(uint version) throws GLib.Error { + int err = set_version_(version); + throw_if_error(err); + } + + [CCode (cname = "gnutls_x509_crt_set_key")] + public int set_key_(PrivateKey key); + [CCode (cname = "gnutls_x509_crt_set_key_")] + public void set_key(PrivateKey key) throws GLib.Error { + int err = set_key_(key); + throw_if_error(err); + } + + [CCode (cname = "gnutls_x509_crt_set_activation_time")] + public int set_activation_time_(time_t act_time); + [CCode (cname = "gnutls_x509_crt_set_activation_time_")] + public void set_activation_time(time_t act_time) throws GLib.Error { + int err = set_activation_time_(act_time); + throw_if_error(err); + } + + [CCode (cname = "gnutls_x509_crt_set_expiration_time")] + public int set_expiration_time_(time_t exp_time); + [CCode (cname = "gnutls_x509_crt_set_expiration_time_")] + public void set_expiration_time(time_t exp_time) throws GLib.Error { + int err = set_expiration_time_(exp_time); + throw_if_error(err); + } + + [CCode (cname = "gnutls_x509_crt_set_serial")] + public int set_serial_(void* serial, size_t serial_size); + [CCode (cname = "gnutls_x509_crt_set_serial_")] + public void set_serial(void* serial, size_t serial_size) throws GLib.Error { + int err = set_serial_(serial, serial_size); + throw_if_error(err); + } + + [CCode (cname = "gnutls_x509_crt_sign")] + public int sign_(Certificate issuer, PrivateKey issuer_key); + [CCode (cname = "gnutls_x509_crt_sign_")] + public void sign(Certificate issuer, PrivateKey issuer_key) throws GLib.Error { + int err = sign_(issuer, issuer_key); + throw_if_error(err); + } + + [CCode (cname = "gnutls_x509_crt_get_fingerprint")] + public int get_fingerprint_(DigestAlgorithm algo, void* buf, ref size_t buf_size); + [CCode (cname = "gnutls_x509_crt_get_fingerprint_")] + public void get_fingerprint(DigestAlgorithm algo, void* buf, ref size_t buf_size) throws GLib.Error { + int err = get_fingerprint_(algo, buf, ref buf_size); + throw_if_error(err); + } + } + + [Compact] + [CCode (cname = "struct gnutls_x509_privkey_int", cprefix = "gnutls_x509_privkey_", free_function = "gnutls_x509_privkey_deinit")] + public class PrivateKey { + private static int init (out PrivateKey key); + public static PrivateKey create () throws GLib.Error { + PrivateKey result; + var ret = init (out result); + throw_if_error(ret); + return result; + } + + public int generate(PKAlgorithm algo, uint bits, uint flags = 0); + } + + } + + [CCode (cname = "gnutls_certificate_request_t", cprefix = "GNUTLS_CERT_", has_type_id = false)] + public enum CertificateRequest { + IGNORE, + REQUEST, + REQUIRE + } + + [CCode (cname = "gnutls_pk_algorithm_t", cprefix = "GNUTLS_PK_", has_type_id = false)] + public enum PKAlgorithm { + UNKNOWN, + RSA, + DSA, + ECDSA; + } + + [CCode (cname = "gnutls_digest_algorithm_t", cprefix = "GNUTLS_DIG_", has_type_id = false)] + public enum DigestAlgorithm { + NULL, + MD5, + SHA1, + RMD160, + MD2, + SHA224, + SHA256, + SHA384, + SHA512; + } + + [Flags] + [CCode (cname = "gnutls_init_flags_t", cprefix = "GNUTLS_", has_type_id = false)] + public enum InitFlags { + SERVER, + CLIENT, + DATAGRAM + } + + [CCode (cname = "gnutls_credentials_type_t", cprefix = "GNUTLS_CRD_", has_type_id = false)] + public enum CredentialsType { + CERTIFICATE, + ANON, + SRP, + PSK, + IA + } + + [CCode (cname = "gnutls_x509_crt_fmt_t", cprefix = "GNUTLS_X509_FMT_", has_type_id = false)] + public enum CertificateFormat { + DER, + PEM + } + + [Flags] + [CCode (cname = "gnutls_certificate_status_t", cprefix = "GNUTLS_CERT_", has_type_id = false)] + public enum CertificateStatus { + INVALID, // will be set if the certificate was not verified. + REVOKED, // in X.509 this will be set only if CRLs are checked + SIGNER_NOT_FOUND, + SIGNER_NOT_CA, + INSECURE_ALGORITHM + } + + [SimpleType] + [CCode (cname = "gnutls_datum_t", has_type_id = false)] + public struct Datum { + public uint8* data; + public uint size; + + public uint8[] extract() { + uint8[] ret = new uint8[size]; + for (int i = 0; i < size; i++) { + ret[i] = data[i]; + } + return ret; + } + } + + // Gnutls error codes. The mapping to a TLS alert is also shown in comments. + [CCode (cname = "int", cprefix = "GNUTLS_E_", lower_case_cprefix = "gnutls_error_", has_type_id = false)] + public enum ErrorCode { + SUCCESS, + UNKNOWN_COMPRESSION_ALGORITHM, + UNKNOWN_CIPHER_TYPE, + LARGE_PACKET, + UNSUPPORTED_VERSION_PACKET, // GNUTLS_A_PROTOCOL_VERSION + UNEXPECTED_PACKET_LENGTH, // GNUTLS_A_RECORD_OVERFLOW + INVALID_SESSION, + FATAL_ALERT_RECEIVED, + UNEXPECTED_PACKET, // GNUTLS_A_UNEXPECTED_MESSAGE + WARNING_ALERT_RECEIVED, + ERROR_IN_FINISHED_PACKET, + UNEXPECTED_HANDSHAKE_PACKET, + UNKNOWN_CIPHER_SUITE, // GNUTLS_A_HANDSHAKE_FAILURE + UNWANTED_ALGORITHM, + MPI_SCAN_FAILED, + DECRYPTION_FAILED, // GNUTLS_A_DECRYPTION_FAILED, GNUTLS_A_BAD_RECORD_MAC + MEMORY_ERROR, + DECOMPRESSION_FAILED, // GNUTLS_A_DECOMPRESSION_FAILURE + COMPRESSION_FAILED, + AGAIN, + EXPIRED, + DB_ERROR, + SRP_PWD_ERROR, + INSUFFICIENT_CREDENTIALS, + HASH_FAILED, + BASE64_DECODING_ERROR, + MPI_PRINT_FAILED, + REHANDSHAKE, // GNUTLS_A_NO_RENEGOTIATION + GOT_APPLICATION_DATA, + RECORD_LIMIT_REACHED, + ENCRYPTION_FAILED, + PK_ENCRYPTION_FAILED, + PK_DECRYPTION_FAILED, + PK_SIGN_FAILED, + X509_UNSUPPORTED_CRITICAL_EXTENSION, + KEY_USAGE_VIOLATION, + NO_CERTIFICATE_FOUND, // GNUTLS_A_BAD_CERTIFICATE + INVALID_REQUEST, + SHORT_MEMORY_BUFFER, + INTERRUPTED, + PUSH_ERROR, + PULL_ERROR, + RECEIVED_ILLEGAL_PARAMETER, // GNUTLS_A_ILLEGAL_PARAMETER + REQUESTED_DATA_NOT_AVAILABLE, + PKCS1_WRONG_PAD, + RECEIVED_ILLEGAL_EXTENSION, + INTERNAL_ERROR, + DH_PRIME_UNACCEPTABLE, + FILE_ERROR, + TOO_MANY_EMPTY_PACKETS, + UNKNOWN_PK_ALGORITHM, + // returned if libextra functionality was requested but + // gnutls_global_init_extra() was not called. + + INIT_LIBEXTRA, + LIBRARY_VERSION_MISMATCH, + // returned if you need to generate temporary RSA + // parameters. These are needed for export cipher suites. + + NO_TEMPORARY_RSA_PARAMS, + LZO_INIT_FAILED, + NO_COMPRESSION_ALGORITHMS, + NO_CIPHER_SUITES, + OPENPGP_GETKEY_FAILED, + PK_SIG_VERIFY_FAILED, + ILLEGAL_SRP_USERNAME, + SRP_PWD_PARSING_ERROR, + NO_TEMPORARY_DH_PARAMS, + // For certificate and key stuff + + ASN1_ELEMENT_NOT_FOUND, + ASN1_IDENTIFIER_NOT_FOUND, + ASN1_DER_ERROR, + ASN1_VALUE_NOT_FOUND, + ASN1_GENERIC_ERROR, + ASN1_VALUE_NOT_VALID, + ASN1_TAG_ERROR, + ASN1_TAG_IMPLICIT, + ASN1_TYPE_ANY_ERROR, + ASN1_SYNTAX_ERROR, + ASN1_DER_OVERFLOW, + OPENPGP_UID_REVOKED, + CERTIFICATE_ERROR, + CERTIFICATE_KEY_MISMATCH, + UNSUPPORTED_CERTIFICATE_TYPE, // GNUTLS_A_UNSUPPORTED_CERTIFICATE + X509_UNKNOWN_SAN, + OPENPGP_FINGERPRINT_UNSUPPORTED, + X509_UNSUPPORTED_ATTRIBUTE, + UNKNOWN_HASH_ALGORITHM, + UNKNOWN_PKCS_CONTENT_TYPE, + UNKNOWN_PKCS_BAG_TYPE, + INVALID_PASSWORD, + MAC_VERIFY_FAILED, // for PKCS #12 MAC + CONSTRAINT_ERROR, + WARNING_IA_IPHF_RECEIVED, + WARNING_IA_FPHF_RECEIVED, + IA_VERIFY_FAILED, + UNKNOWN_ALGORITHM, + BASE64_ENCODING_ERROR, + INCOMPATIBLE_CRYPTO_LIBRARY, + INCOMPATIBLE_LIBTASN1_LIBRARY, + OPENPGP_KEYRING_ERROR, + X509_UNSUPPORTED_OID, + RANDOM_FAILED, + BASE64_UNEXPECTED_HEADER_ERROR, + OPENPGP_SUBKEY_ERROR, + CRYPTO_ALREADY_REGISTERED, + HANDSHAKE_TOO_LARGE, + UNIMPLEMENTED_FEATURE, + APPLICATION_ERROR_MAX, // -65000 + APPLICATION_ERROR_MIN; // -65500 + + [CCode (cname = "gnutls_error_is_fatal")] + public bool is_fatal(); + + [CCode (cname = "gnutls_perror")] + public void print(); + + [CCode (cname = "gnutls_strerror")] + public unowned string to_string(); + } + + public void throw_if_error(int err_int) throws GLib.Error { + ErrorCode error = (ErrorCode)err_int; + if (error != ErrorCode.SUCCESS) { + throw new GLib.Error(-1, error, "%s%s", error.to_string(), error.is_fatal() ? " fatal" : ""); + } + } +} \ No newline at end of file diff --git a/libdino/vapi/openssl.vapi b/libdino/vapi/openssl.vapi index 87693d37..57e4022c 100644 --- a/libdino/vapi/openssl.vapi +++ b/libdino/vapi/openssl.vapi @@ -86,7 +86,7 @@ namespace OpenSSL { [CCode (cname = "EVP_PKEY", lower_case_cprefix = "EVP_PKEY_", cprefix = "EVP_PKEY_", ref_function = "EVP_PKEY_up_ref", ref_function_void = true, unref_function = "EVP_PKEY_free")] public class Key { public Key (); - [CCode (cheader_filename = "openssl_utils.h")] + [CCode (cheader_filename = "crypto/openssl/utils.h")] public Key.generate_ecdsa (string curve); } [Compact] diff --git a/plugins/ice/meson.build b/plugins/ice/meson.build index 99f8e6a1..19030259 100644 --- a/plugins/ice/meson.build +++ b/plugins/ice/meson.build @@ -8,7 +8,6 @@ dependencies = [ dep_xmpp_vala, ] sources = files( - 'src/dtls_srtp.vala', 'src/module.vala', 'src/plugin.vala', 'src/transport_parameters.vala', @@ -21,18 +20,6 @@ c_args = vala_c_args + [ vala_args = [ '--vapidir', meson.current_source_dir() / 'vapi', ] -if crypto_backend == 'gnu' - dependencies += [ dep_gnutls ] - sources += files( - 'src/dtls_srtp_gnutls.vala' - ) -elif crypto_backend == 'openssl' - dependencies += [ dep_openssl ] - sources += files( - 'src/dtls_srtp_openssl.vala', - 'src/openssl_utils.c', - ) -endif lib_ice = shared_library('ice', sources, name_prefix: '', c_args: c_args, vala_args: vala_args, include_directories: include_directories('src'), dependencies: dependencies, install: true, install_dir: get_option('libdir') / get_option('plugindir'), install_rpath: default_install_rpath) dep_ice = declare_dependency(link_with: lib_ice, include_directories: include_directories('.')) summary('Peer-to-peer communication (ice)', dep_ice.found(), bool_yn: true, section: 'Plugins') diff --git a/plugins/ice/src/dtls_srtp.vala b/plugins/ice/src/dtls_srtp.vala deleted file mode 100644 index 41918359..00000000 --- a/plugins/ice/src/dtls_srtp.vala +++ /dev/null @@ -1,146 +0,0 @@ -namespace Dino.Plugins.Ice.DtlsSrtp { - -public abstract class CredentialsCapsule : Object { - public uint8[] own_fingerprint; -} - -public abstract class AbstractHandler { - - public signal void send_data(uint8[] data); - - public bool ready { get { - return srtp_session.has_encrypt && srtp_session.has_decrypt; - }} - - public Mode mode { get; set; default = Mode.CLIENT; } - public uint8[] own_fingerprint { get; private set; } - public uint8[] peer_fingerprint { get; set; } - public string peer_fp_algo { get; set; } - - protected CredentialsCapsule credentials; - protected Cond buffer_cond = Cond(); - protected Mutex buffer_mutex = Mutex(); - protected Gee.LinkedList buffer_queue = new Gee.LinkedList(); - - protected bool running = false; - protected bool stop = false; - protected bool restart = false; - - protected Crypto.Srtp.Session srtp_session = new Crypto.Srtp.Session(); - - protected AbstractHandler.with_cert(CredentialsCapsule creds) { - this.credentials = creds; - this.own_fingerprint = creds.own_fingerprint; - } - - public uint8[]? process_incoming_data(uint component_id, uint8[] data) throws Crypto.Error { - if (data[0] >= 128) { - if (!srtp_session.has_decrypt) { - debug("Received data before SRTP session is ready, dropping."); - return null; - } - if (component_id == 1) { - if (data.length >= 2 && data[1] >= 192 && data[1] < 224) { - return srtp_session.decrypt_rtcp(data); - } - return srtp_session.decrypt_rtp(data); - } - if (component_id == 2) return srtp_session.decrypt_rtcp(data); - } - if (component_id == 1 && data.length >= 1 && (data[0] >= 20 && data[0] < 64)) { - on_data_rec(data); - return null; - } - debug("Dropping unknown data from component %u", component_id); - return null; - } - - public uint8[]? process_outgoing_data(uint component_id, uint8[] data) throws Crypto.Error { - if (srtp_session.has_encrypt) { - if (component_id == 1) { - if (data.length >= 2 && data[1] >= 192 && data[1] < 224) { - return srtp_session.encrypt_rtcp(data); - } - return srtp_session.encrypt_rtp(data); - } - if (component_id == 2) return srtp_session.encrypt_rtcp(data); - } - return null; - } - - public void on_data_rec(owned uint8[] data) { - buffer_mutex.lock(); - buffer_queue.add(new Bytes.take(data)); - buffer_cond.signal(); - buffer_mutex.unlock(); - } - - public void stop_dtls_connection() { - buffer_mutex.lock(); - stop = true; - buffer_cond.signal(); - buffer_mutex.unlock(); - } - - protected Bytes get_buffer_if_present () { - buffer_mutex.lock(); - if (buffer_queue.size == 0) { - buffer_mutex.unlock(); - return null; - } - Bytes res = buffer_queue.remove_at(0); - buffer_mutex.unlock(); - return res; - } - - public async Xmpp.Xep.Jingle.ContentEncryption? setup_dtls_connection() { - MainContext context = MainContext.current_source().get_context(); - var thread = new Thread("dtls-connection", () => { - // Check we can start working on the thread - buffer_mutex.lock(); - if (stop) { - restart = true; - buffer_mutex.unlock(); - return null; - } - if (running || ready) { - buffer_mutex.unlock(); - return null; - } - running = true; - restart = false; - buffer_mutex.unlock(); - - // Actual work - var res = setup_dtls_connection_thread(); - - // Callback - Source source = new IdleSource(); - source.set_callback(setup_dtls_connection.callback); - source.attach(context); - return res; - }); - yield; - return thread.join(); - } - - protected abstract Xmpp.Xep.Jingle.ContentEncryption? setup_dtls_connection_thread(); -} - -private string format_fingerprint(uint8[] fingerprint) { - var sb = new StringBuilder(); - for (int i = 0; i < fingerprint.length; i++) { - sb.append("%02X".printf(fingerprint[i])); - if (i < fingerprint.length - 1) { - sb.append(":"); - } - } - return sb.str; -} - - -public enum Mode { - CLIENT, SERVER -} - -} diff --git a/plugins/ice/src/dtls_srtp_gnutls.vala b/plugins/ice/src/dtls_srtp_gnutls.vala deleted file mode 100644 index 91a7cfb2..00000000 --- a/plugins/ice/src/dtls_srtp_gnutls.vala +++ /dev/null @@ -1,245 +0,0 @@ -using GnuTLS; - -namespace Dino.Plugins.Ice.DtlsSrtp { - -public class CredentialsCapsuleGnuTLS : CredentialsCapsule { - public X509.Certificate[] own_cert; - public X509.PrivateKey private_key; -} - -private uint8[] get_fingerprint(X509.Certificate certificate, DigestAlgorithm digest_algo) { - uint8[] buf = new uint8[512]; - size_t buf_out_size = 512; - certificate.get_fingerprint(digest_algo, buf, ref buf_out_size); - - uint8[] ret = new uint8[buf_out_size]; - for (int i = 0; i < buf_out_size; i++) { - ret[i] = buf[i]; - } - return ret; -} - -internal CredentialsCapsule generate_credentials () throws GLib.Error { - int err = 0; - - X509.PrivateKey private_key = X509.PrivateKey.create (); - err = private_key.generate (PKAlgorithm.ECDSA, 256); - throw_if_error (err); - - var start_time = new DateTime.now_local().add_days(-1); - var end_time = start_time.add_days (2); - - X509.Certificate cert = X509.Certificate.create (); - cert.set_key (private_key); - cert.set_version (1); - cert.set_activation_time ((time_t) start_time.to_unix ()); - cert.set_expiration_time ((time_t) end_time.to_unix ()); - - uint32 serial = 1; - cert.set_serial (&serial, sizeof(uint32)); - - cert.sign (cert, private_key); - - uint8[] own_fingerprint = get_fingerprint (cert, DigestAlgorithm.SHA256); - X509.Certificate[] own_cert = new X509.Certificate[] { (owned)cert }; - - var creds = new CredentialsCapsuleGnuTLS(); - creds.own_fingerprint = own_fingerprint; - creds.own_cert = (owned) own_cert; - creds.private_key = (owned) private_key; - - return creds; -} - -public class Handler : AbstractHandler { - - public Handler.with_cert(CredentialsCapsule creds) { - base.with_cert(creds); - } - - protected override Xmpp.Xep.Jingle.ContentEncryption? setup_dtls_connection_thread() { - InitFlags server_or_client = mode == Mode.SERVER ? InitFlags.SERVER : InitFlags.CLIENT; - debug("Setting up DTLS connection using GnuTLS. We're %s", mode.to_string()); - - CertificateCredentials cert_cred = CertificateCredentials.create(); - int err = cert_cred.set_x509_key(((CredentialsCapsuleGnuTLS)credentials).own_cert, ((CredentialsCapsuleGnuTLS)credentials).private_key); - throw_if_error(err); - - Session? session = Session.create(server_or_client | InitFlags.DATAGRAM); - session.enable_heartbeat(1); - session.set_srtp_profile_direct("SRTP_AES128_CM_HMAC_SHA1_80"); - session.set_credentials(GnuTLS.CredentialsType.CERTIFICATE, cert_cred); - session.server_set_request(CertificateRequest.REQUEST); - session.set_priority_from_string("NORMAL:!VERS-TLS-ALL:+VERS-DTLS-ALL:+CTYPE-CLI-X509"); - - session.set_transport_pointer(this); - session.set_pull_function(pull_function); - session.set_pull_timeout_function(pull_timeout_function); - session.set_push_function(push_function); - session.set_verify_function(verify_function); - - DateTime maximum_time = new DateTime.now_utc().add_seconds(20); - do { - err = session.handshake(); - - DateTime current_time = new DateTime.now_utc(); - if (maximum_time.compare(current_time) < 0) { - warning("DTLS handshake timeouted"); - err = ErrorCode.APPLICATION_ERROR_MIN + 1; - break; - } - if (stop) { - debug("DTLS handshake stopped"); - err = ErrorCode.APPLICATION_ERROR_MIN + 2; - break; - } - } while (err < 0 && !((ErrorCode)err).is_fatal()); - - buffer_mutex.lock(); - if (stop) { - stop = false; - running = false; - bool restart = restart; - buffer_mutex.unlock(); - if (restart) { - debug("Restarting DTLS handshake"); - return setup_dtls_connection_thread(); - } - return null; - } - buffer_mutex.unlock(); - if (err != ErrorCode.SUCCESS) { - warning("DTLS handshake failed: %s", ((ErrorCode)err).to_string()); - return null; - } - - uint8[] km = new uint8[150]; - Datum? client_key, client_salt, server_key, server_salt; - session.get_srtp_keys(km, km.length, out client_key, out client_salt, out server_key, out server_salt); - if (client_key == null || client_salt == null || server_key == null || server_salt == null) { - warning("SRTP client/server key/salt null"); - } - - debug("Finished DTLS connection. We're %s", mode.to_string()); - if (mode == Mode.SERVER) { - srtp_session.set_encryption_key(Crypto.Srtp.AES_CM_128_HMAC_SHA1_80, server_key.extract(), server_salt.extract()); - srtp_session.set_decryption_key(Crypto.Srtp.AES_CM_128_HMAC_SHA1_80, client_key.extract(), client_salt.extract()); - } else { - srtp_session.set_encryption_key(Crypto.Srtp.AES_CM_128_HMAC_SHA1_80, client_key.extract(), client_salt.extract()); - srtp_session.set_decryption_key(Crypto.Srtp.AES_CM_128_HMAC_SHA1_80, server_key.extract(), server_salt.extract()); - } - return new Xmpp.Xep.Jingle.ContentEncryption(Xmpp.Xep.JingleIceUdp.DTLS_NS_URI, "DTLS-SRTP", credentials.own_fingerprint, peer_fingerprint); - } - - private static ssize_t pull_function(void* transport_ptr, uint8[] buffer) { - Handler self = transport_ptr as Handler; - - self.buffer_mutex.lock(); - while (self.buffer_queue.size == 0) { - self.buffer_cond.wait(self.buffer_mutex); - if (self.stop) { - self.buffer_mutex.unlock(); - debug("DTLS handshake pull_function stopped"); - return -1; - } - } - Bytes data = self.buffer_queue.remove_at(0); - self.buffer_mutex.unlock(); - - uint8[] data_uint8 = Bytes.unref_to_data((owned) data); - Memory.copy(buffer, data_uint8, data_uint8.length); - - // The callback should return 0 on connection termination, a positive number indicating the number of bytes received, and -1 on error. - return (ssize_t)data_uint8.length; - } - - private static int pull_timeout_function(void* transport_ptr, uint ms) { - Handler self = transport_ptr as Handler; - - int64 end_time = get_monotonic_time() + ms * 1000; - - self.buffer_mutex.lock(); - while (self.buffer_queue.size == 0) { - self.buffer_cond.wait_until(self.buffer_mutex, end_time); - if (self.stop) { - self.buffer_mutex.unlock(); - debug("DTLS handshake pull_timeout_function stopped"); - return -1; - } - - if (get_monotonic_time() > end_time) { - self.buffer_mutex.unlock(); - return 0; - } - } - self.buffer_mutex.unlock(); - - // The callback should return 0 on timeout, a positive number if data can be received, and -1 on error. - return 1; - } - - private static ssize_t push_function(void* transport_ptr, uint8[] buffer) { - Handler self = transport_ptr as Handler; - self.send_data(buffer); - - // The callback should return a positive number indicating the bytes sent, and -1 on error. - return (ssize_t)buffer.length; - } - - private static int verify_function(Session session) { - Handler self = session.get_transport_pointer() as Handler; - try { - bool valid = self.verify_peer_cert(session); - if (!valid) { - warning("DTLS certificate invalid. Aborting handshake."); - return 1; - } - } catch (Error e) { - warning("Error during DTLS certificate validation: %s. Aborting handshake.", e.message); - return 1; - } - - // The callback function should return 0 for the handshake to continue or non-zero to terminate. - return 0; - } - - private bool verify_peer_cert(Session session) throws GLib.Error { - unowned Datum[] cert_datums = session.get_peer_certificates(); - if (cert_datums.length == 0) { - warning("No peer certs"); - return false; - } - if (cert_datums.length > 1) warning("More than one peer cert"); - - X509.Certificate peer_cert = X509.Certificate.create(); - peer_cert.import(ref cert_datums[0], CertificateFormat.DER); - - DigestAlgorithm algo; - switch (peer_fp_algo) { - case "sha-256": - algo = DigestAlgorithm.SHA256; - break; - default: - warning("Unkown peer fingerprint algorithm: %s", peer_fp_algo); - return false; - } - - uint8[] real_peer_fp = get_fingerprint(peer_cert, algo); - - if (real_peer_fp.length != this.peer_fingerprint.length) { - warning("Fingerprint lengths not equal %i vs %i", real_peer_fp.length, peer_fingerprint.length); - return false; - } - - for (int i = 0; i < real_peer_fp.length; i++) { - if (real_peer_fp[i] != this.peer_fingerprint[i]) { - warning("First cert in peer cert list doesn't equal advertised one: %s vs %s", format_fingerprint(real_peer_fp), format_fingerprint(peer_fingerprint)); - return false; - } - } - - return true; - } -} - -} \ No newline at end of file diff --git a/plugins/ice/src/dtls_srtp_openssl.vala b/plugins/ice/src/dtls_srtp_openssl.vala deleted file mode 100644 index 86f080e5..00000000 --- a/plugins/ice/src/dtls_srtp_openssl.vala +++ /dev/null @@ -1,202 +0,0 @@ -using OpenSSL; - -namespace Dino.Plugins.Ice.DtlsSrtp { - -public class CredentialsCapsuleOpenSSL : CredentialsCapsule { - public X509.Certificate own_cert; - public EVP.Key private_key; -} - -internal CredentialsCapsule generate_credentials () throws GLib.Error { - EVP.Key key = new EVP.Key.generate_ecdsa ("prime256v1"); - - var start_time = new DateTime.now_local().add_days(-1); - var end_time = start_time.add_days (2); - - X509.Certificate cert = new X509.Certificate (); - cert.set_public_key (key); - cert.set_version (1); - cert.not_before.set ((time_t) start_time.to_unix ()); - cert.not_after.set ((time_t) end_time.to_unix ()); - cert.serial_number.set (1); - - cert.sign (key, EVP.sha256 ()); - - CredentialsCapsuleOpenSSL credentials = new CredentialsCapsuleOpenSSL (); - credentials.own_fingerprint = cert.digest (EVP.sha256 ()); - credentials.own_cert = (owned) cert; - credentials.private_key = (owned) key; - return credentials; -} - -public class Handler : AbstractHandler { - - public Handler.with_cert (CredentialsCapsule creds) { - base.with_cert (creds); - } - - protected override Xmpp.Xep.Jingle.ContentEncryption? setup_dtls_connection_thread () { - debug("Setting up DTLS connection using OpenSSL. We're %s", mode.to_string()); - SSL.Context context = new SSL.Context (mode == Mode.SERVER ? SSL.Method.DTLS_SERVER : SSL.Method.DTLS_CLIENT); - context.set_tlsext_use_srtp ("SRTP_AES128_CM_SHA1_80"); - context.use_certificate (((CredentialsCapsuleOpenSSL) credentials).own_cert); - context.use_private_key (((CredentialsCapsuleOpenSSL) credentials).private_key); - SSL session = new SSL (context); - BIO bio = new BIO(get_handler_based_bio_method()); - bio.data = this; - session.rbio = bio; - session.wbio = bio; - - DateTime maximum_time = new DateTime.now_utc().add_seconds(20); - int err = 0; - if (mode == Mode.SERVER) { - session.set_accept_state (); - } else { - session.set_connect_state (); - } - do { - try { - SSL.Error ret = session.do_handshake(); - switch (ret) { - case SSL.Error.NONE: - err = 1; - break; - case SSL.Error.WANT_READ: - debug ("DTLS wants to read, but nothing in queue, wait for buffer"); - buffer_mutex.lock(); - if (buffer_queue.size == 0) { - buffer_cond.wait_until(buffer_mutex, get_monotonic_time() + maximum_time.difference(new DateTime.now_utc())); - } - buffer_mutex.unlock(); - err = -1; - break; - default: - warning ("DTLS Error: %s", @"$ret"); - err = 4; - break; - } - } catch (Error e) { - warning ("DTLS Error: %s", e.message); - err = e.code; - } - - DateTime current_time = new DateTime.now_utc(); - if (maximum_time.compare(current_time) < 0) { - debug ("DTLS handshake timeouted"); - err = 2; - break; - } - if (stop) { - debug ("DTLS handshake stopped"); - err = 3; - break; - } - } while (err < 0); - - buffer_mutex.lock(); - if (stop) { - stop = false; - running = false; - bool restart = restart; - buffer_mutex.unlock(); - if (restart) { - debug ("Restarting DTLS handshake"); - return setup_dtls_connection_thread(); - } - return null; - } - buffer_mutex.unlock(); - if (err != 1) { - debug ("DTLS handshake failed"); - return null; - } - - // TODO: Check peer certificate - - uint8[] km = new uint8[60]; - session.export_keying_material (km, "EXTRACTOR-dtls_srtp"); - uint8[] client_key = km[0:16]; - uint8[] server_key = km[16:32]; - uint8[] client_salt = km[32:46]; - uint8[] server_salt = km[46:60]; - - debug ("Finished DTLS connection. We're %s", mode.to_string()); - if (mode == Mode.SERVER) { - srtp_session.set_encryption_key (Crypto.Srtp.AES_CM_128_HMAC_SHA1_80, server_key, server_salt); - srtp_session.set_decryption_key (Crypto.Srtp.AES_CM_128_HMAC_SHA1_80, client_key, client_salt); - } else { - srtp_session.set_encryption_key (Crypto.Srtp.AES_CM_128_HMAC_SHA1_80, client_key, client_salt); - srtp_session.set_decryption_key (Crypto.Srtp.AES_CM_128_HMAC_SHA1_80, server_key, server_salt); - } - return new Xmpp.Xep.Jingle.ContentEncryption (Xmpp.Xep.JingleIceUdp.DTLS_NS_URI, "DTLS-SRTP", credentials.own_fingerprint, peer_fingerprint); - } - - private int read_impl (BIO bio, uint8[] out_buf) { - Bytes data = get_buffer_if_present (); - if (data == null) { - if (!stop) { - debug ("No pending buffer in queue, but end not reached yet, set_retry_read"); - bio.set_retry_read (); - } - return -1; - } - if (data.length > out_buf.length) { - warning ("Buffer in queue does not fit in out_buf"); - return -1; - } - debug ("Read %d bytes from queue", data.length); - - uint8[] data_uint8 = Bytes.unref_to_data((owned) data); - Memory.copy (out_buf, data_uint8, data_uint8.length); - bio.clear_retry_flags (); - return data_uint8.length; - } - - private int write_impl (BIO bio, uint8[] buf) { - debug ("Write %d bytes to network", buf.length); - send_data (buf); - return buf.length; - } - - private long ctrl_impl (BIO bio, BIO.ControlCommand cmd, long num, void* ptr) { - switch (cmd) { - case BIO.ControlCommand.FLUSH: - // Datagrams are sent out immediately - return 1; - case BIO.ControlCommand.DGRAM_SET_NEXT_TIMEOUT: - // Datagrams are sent out immediately - return 1; - case BIO.ControlCommand.DGRAM_GET_MTU_OVERHEAD: - // Just some safe estimate - return 70; - case BIO.ControlCommand.DGRAM_QUERY_MTU: - // Just some safe estimate - return 1200; - case BIO.ControlCommand.WPENDING: - // Datagrams are sent out immediately - return 0; - case BIO.ControlCommand.POP: - case BIO.ControlCommand.PUSH: - // Ignored - return 0; - default: - warning (@"Unimplemented ctrl cmd:$cmd, num:$num"); - return 0; - } - } - - private static BIO.Method _handler_based_bio_method_instance = null; - - private static unowned BIO.Method get_handler_based_bio_method () { - if (_handler_based_bio_method_instance != null) return _handler_based_bio_method_instance; - _handler_based_bio_method_instance = new BIO.Method (BIO.Type.SOURCE_SINK | BIO.Type.DGRAM, "Dino DTLS Handler BIO"); - _handler_based_bio_method_instance.read = (bio, out_buf) => bio.data.read_impl (bio, out_buf); - _handler_based_bio_method_instance.write = (bio, buf) => bio.data.write_impl (bio, buf); - _handler_based_bio_method_instance.ctrl = (bio, cmd, num, ptr) => bio.data.ctrl_impl (bio, cmd, num, ptr); - return _handler_based_bio_method_instance; - } -} - - - -} \ No newline at end of file diff --git a/plugins/ice/src/module.vala b/plugins/ice/src/module.vala index ace62108..c3c92124 100644 --- a/plugins/ice/src/module.vala +++ b/plugins/ice/src/module.vala @@ -1,4 +1,6 @@ using Gee; + +using Dino.Crypto; using Xmpp; using Xmpp.Xep; @@ -43,7 +45,7 @@ public class Dino.Plugins.Ice.Module : JingleIceUdp.Module { string from_to_id = local_full_jid.to_string() + peer_full_jid.to_string(); try { if (!cerds.has_key(from_to_id)) cerds[from_to_id] = DtlsSrtp.generate_credentials(); - } catch (Error e) { + } catch (GLib.Error e) { warning("Error creating dtls credentials: %s", e.message); } return cerds[from_to_id]; diff --git a/plugins/ice/src/openssl_utils.c b/plugins/ice/src/openssl_utils.c deleted file mode 100644 index 7f4bf331..00000000 --- a/plugins/ice/src/openssl_utils.c +++ /dev/null @@ -1,49 +0,0 @@ -#include "openssl_utils.h" -#include -#include - -#if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x30000000L -// This is for OpenSSL 3.0.0 and newer - -EVP_PKEY *EVP_PKEY_new_generate_ecdsa (const char* curve) { - // Generate EVP_PKEY directly - EVP_PKEY *pkey = EVP_EC_gen (curve); - return pkey; -} - -#else -// This is for OpenSSL before 3.0.0 and forks of earlier OpenSSL versions like LibreSSL - -EVP_PKEY *EVP_PKEY_new_generate_ecdsa (const char* curve) { - // Get NID from curve name - int nid = OBJ_sn2nid (curve); - if (nid == NID_undef) return NULL; - - // Create EC_KEY - EC_KEY *ec_key = EC_KEY_new_by_curve_name (nid); - if (!ec_key) return NULL; - - // Generate key - if (!EC_KEY_generate_key (ec_key)) { - EC_KEY_free (ec_key); - return NULL; - } - - // Create EVP_PKEY - EVP_PKEY *pkey = EVP_PKEY_new (); - if (!pkey) { - EC_KEY_free (ec_key); - return NULL; - } - - // Assign EC_KEY to EVP_PKEY - if (!EVP_PKEY_assign_EC_KEY (pkey, ec_key)) { - EC_KEY_free (ec_key); - return NULL; - } - - // EVP_PKEY_assign_EC_KEY takes ownership of ec_key, no need to free it explicitly - return pkey; -} - -#endif diff --git a/plugins/ice/src/openssl_utils.h b/plugins/ice/src/openssl_utils.h deleted file mode 100644 index bcc6f53a..00000000 --- a/plugins/ice/src/openssl_utils.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef DINO_OPENSSL_UTILS_H -#define DINO_OPENSSL_UTILS_H - -#include - -EVP_PKEY *EVP_PKEY_new_generate_ecdsa (const char* curve); - -#endif //DINO_OPENSSL_UTILS_H \ No newline at end of file diff --git a/plugins/ice/src/transport_parameters.vala b/plugins/ice/src/transport_parameters.vala index 66dde8b1..122207c5 100644 --- a/plugins/ice/src/transport_parameters.vala +++ b/plugins/ice/src/transport_parameters.vala @@ -1,4 +1,6 @@ using Gee; + +using Dino.Crypto; using Xmpp; using Xmpp.Xep; diff --git a/plugins/ice/vapi/gnutls.vapi b/plugins/ice/vapi/gnutls.vapi deleted file mode 100644 index 86a8c401..00000000 --- a/plugins/ice/vapi/gnutls.vapi +++ /dev/null @@ -1,420 +0,0 @@ -[CCode (cprefix = "gnutls_", lower_case_cprefix = "gnutls_", cheader_filename = "gnutls/gnutls.h")] -namespace GnuTLS { - - public int global_init(); - - [CCode (cname = "gnutls_pull_func", has_target = false)] - public delegate ssize_t PullFunc(void* transport_ptr, [CCode (ctype = "void*", array_length_type="size_t")] uint8[] array); - - [CCode (cname = "gnutls_pull_timeout_func", has_target = false)] - public delegate int PullTimeoutFunc(void* transport_ptr, uint ms); - - [CCode (cname = "gnutls_push_func", has_target = false)] - public delegate ssize_t PushFunc(void* transport_ptr, [CCode (ctype = "void*", array_length_type="size_t")] uint8[] array); - - [CCode (cname = "gnutls_certificate_verify_function", has_target = false)] - public delegate int VerifyFunc(Session session); - - [Compact] - [CCode (cname = "struct gnutls_session_int", free_function = "gnutls_deinit")] - public class Session { - - public static Session? create(int con_end) throws GLib.Error { - Session result; - var ret = init(out result, con_end); - throw_if_error(ret); - return result; - } - - [CCode (cname = "gnutls_init")] - private static int init(out Session session, int con_end); - - [CCode (cname = "gnutls_transport_set_push_function")] - public void set_push_function(PushFunc func); - - [CCode (cname = "gnutls_transport_set_pull_function")] - public void set_pull_function(PullFunc func); - - [CCode (cname = "gnutls_transport_set_pull_timeout_function")] - public void set_pull_timeout_function(PullTimeoutFunc func); - - [CCode (cname = "gnutls_transport_set_ptr")] - public void set_transport_pointer(void* ptr); - - [CCode (cname = "gnutls_transport_get_ptr")] - public void* get_transport_pointer(); - - [CCode (cname = "gnutls_heartbeat_enable")] - public int enable_heartbeat(uint type); - - [CCode (cname = "gnutls_certificate_server_set_request")] - public void server_set_request(CertificateRequest req); - - [CCode (cname = "gnutls_credentials_set")] - public int set_credentials_(CredentialsType type, void* cred); - [CCode (cname = "gnutls_credentials_set_")] - public void set_credentials(CredentialsType type, void* cred) throws GLib.Error { - int err = set_credentials_(type, cred); - throw_if_error(err); - } - - [CCode (cname = "gnutls_priority_set_direct")] - public int set_priority_from_string_(string priority, out unowned string err_pos = null); - [CCode (cname = "gnutls_priority_set_direct_")] - public void set_priority_from_string(string priority, out unowned string err_pos = null) throws GLib.Error { - int err = set_priority_from_string_(priority, out err_pos); - throw_if_error(err); - } - - [CCode (cname = "gnutls_srtp_set_profile_direct")] - public int set_srtp_profile_direct_(string profiles, out unowned string err_pos = null); - [CCode (cname = "gnutls_srtp_set_profile_direct_")] - public void set_srtp_profile_direct(string profiles, out unowned string err_pos = null) throws GLib.Error { - int err = set_srtp_profile_direct_(profiles, out err_pos); - throw_if_error(err); - } - - [CCode (cname = "gnutls_transport_set_int")] - public void transport_set_int(int fd); - - [CCode (cname = "gnutls_handshake")] - public int handshake(); - - [CCode (cname = "gnutls_srtp_get_keys")] - public int get_srtp_keys_(void *key_material, uint32 key_material_size, out Datum client_key, out Datum client_salt, out Datum server_key, out Datum server_salt); - [CCode (cname = "gnutls_srtp_get_keys_")] - public void get_srtp_keys(void *key_material, uint32 key_material_size, out Datum client_key, out Datum client_salt, out Datum server_key, out Datum server_salt) throws GLib.Error { - get_srtp_keys_(key_material, key_material_size, out client_key, out client_salt, out server_key, out server_salt); - } - - [CCode (cname = "gnutls_certificate_get_peers", array_length_type = "unsigned int")] - public unowned Datum[]? get_peer_certificates(); - - [CCode (cname = "gnutls_session_set_verify_function")] - public void set_verify_function(VerifyFunc func); - } - - [Compact] - [CCode (cname = "struct gnutls_certificate_credentials_st", free_function = "gnutls_certificate_free_credentials", cprefix = "gnutls_certificate_")] - public class CertificateCredentials { - - [CCode (cname = "gnutls_certificate_allocate_credentials")] - private static int allocate(out CertificateCredentials credentials); - - public static CertificateCredentials create() throws GLib.Error { - CertificateCredentials result; - var ret = allocate (out result); - throw_if_error(ret); - return result; - } - - public void get_x509_crt(uint index, [CCode (array_length_type = "unsigned int")] out unowned X509.Certificate[] x509_ca_list); - - public int set_x509_key(X509.Certificate[] cert_list, X509.PrivateKey key); - } - - [CCode (cheader_filename = "gnutls/x509.h", cprefix = "GNUTLS_")] - namespace X509 { - - [Compact] - [CCode (cname = "struct gnutls_x509_crt_int", cprefix = "gnutls_x509_crt_", free_function = "gnutls_x509_crt_deinit")] - public class Certificate { - - [CCode (cname = "gnutls_x509_crt_init")] - private static int init (out Certificate cert); - public static Certificate create() throws GLib.Error { - Certificate result; - var ret = init (out result); - throw_if_error(ret); - return result; - } - - [CCode (cname = "gnutls_x509_crt_import")] - public int import_(ref Datum data, CertificateFormat format); - [CCode (cname = "gnutls_x509_crt_import_")] - public void import(ref Datum data, CertificateFormat format) throws GLib.Error { - int err = import_(ref data, format); - throw_if_error(err); - } - - [CCode (cname = "gnutls_x509_crt_set_version")] - public int set_version_(uint version); - [CCode (cname = "gnutls_x509_crt_set_version_")] - public void set_version(uint version) throws GLib.Error { - int err = set_version_(version); - throw_if_error(err); - } - - [CCode (cname = "gnutls_x509_crt_set_key")] - public int set_key_(PrivateKey key); - [CCode (cname = "gnutls_x509_crt_set_key_")] - public void set_key(PrivateKey key) throws GLib.Error { - int err = set_key_(key); - throw_if_error(err); - } - - [CCode (cname = "gnutls_x509_crt_set_activation_time")] - public int set_activation_time_(time_t act_time); - [CCode (cname = "gnutls_x509_crt_set_activation_time_")] - public void set_activation_time(time_t act_time) throws GLib.Error { - int err = set_activation_time_(act_time); - throw_if_error(err); - } - - [CCode (cname = "gnutls_x509_crt_set_expiration_time")] - public int set_expiration_time_(time_t exp_time); - [CCode (cname = "gnutls_x509_crt_set_expiration_time_")] - public void set_expiration_time(time_t exp_time) throws GLib.Error { - int err = set_expiration_time_(exp_time); - throw_if_error(err); - } - - [CCode (cname = "gnutls_x509_crt_set_serial")] - public int set_serial_(void* serial, size_t serial_size); - [CCode (cname = "gnutls_x509_crt_set_serial_")] - public void set_serial(void* serial, size_t serial_size) throws GLib.Error { - int err = set_serial_(serial, serial_size); - throw_if_error(err); - } - - [CCode (cname = "gnutls_x509_crt_sign")] - public int sign_(Certificate issuer, PrivateKey issuer_key); - [CCode (cname = "gnutls_x509_crt_sign_")] - public void sign(Certificate issuer, PrivateKey issuer_key) throws GLib.Error { - int err = sign_(issuer, issuer_key); - throw_if_error(err); - } - - [CCode (cname = "gnutls_x509_crt_get_fingerprint")] - public int get_fingerprint_(DigestAlgorithm algo, void* buf, ref size_t buf_size); - [CCode (cname = "gnutls_x509_crt_get_fingerprint_")] - public void get_fingerprint(DigestAlgorithm algo, void* buf, ref size_t buf_size) throws GLib.Error { - int err = get_fingerprint_(algo, buf, ref buf_size); - throw_if_error(err); - } - } - - [Compact] - [CCode (cname = "struct gnutls_x509_privkey_int", cprefix = "gnutls_x509_privkey_", free_function = "gnutls_x509_privkey_deinit")] - public class PrivateKey { - private static int init (out PrivateKey key); - public static PrivateKey create () throws GLib.Error { - PrivateKey result; - var ret = init (out result); - throw_if_error(ret); - return result; - } - - public int generate(PKAlgorithm algo, uint bits, uint flags = 0); - } - - } - - [CCode (cname = "gnutls_certificate_request_t", cprefix = "GNUTLS_CERT_", has_type_id = false)] - public enum CertificateRequest { - IGNORE, - REQUEST, - REQUIRE - } - - [CCode (cname = "gnutls_pk_algorithm_t", cprefix = "GNUTLS_PK_", has_type_id = false)] - public enum PKAlgorithm { - UNKNOWN, - RSA, - DSA, - ECDSA; - } - - [CCode (cname = "gnutls_digest_algorithm_t", cprefix = "GNUTLS_DIG_", has_type_id = false)] - public enum DigestAlgorithm { - NULL, - MD5, - SHA1, - RMD160, - MD2, - SHA224, - SHA256, - SHA384, - SHA512; - } - - [Flags] - [CCode (cname = "gnutls_init_flags_t", cprefix = "GNUTLS_", has_type_id = false)] - public enum InitFlags { - SERVER, - CLIENT, - DATAGRAM - } - - [CCode (cname = "gnutls_credentials_type_t", cprefix = "GNUTLS_CRD_", has_type_id = false)] - public enum CredentialsType { - CERTIFICATE, - ANON, - SRP, - PSK, - IA - } - - [CCode (cname = "gnutls_x509_crt_fmt_t", cprefix = "GNUTLS_X509_FMT_", has_type_id = false)] - public enum CertificateFormat { - DER, - PEM - } - - [Flags] - [CCode (cname = "gnutls_certificate_status_t", cprefix = "GNUTLS_CERT_", has_type_id = false)] - public enum CertificateStatus { - INVALID, // will be set if the certificate was not verified. - REVOKED, // in X.509 this will be set only if CRLs are checked - SIGNER_NOT_FOUND, - SIGNER_NOT_CA, - INSECURE_ALGORITHM - } - - [SimpleType] - [CCode (cname = "gnutls_datum_t", has_type_id = false)] - public struct Datum { - public uint8* data; - public uint size; - - public uint8[] extract() { - uint8[] ret = new uint8[size]; - for (int i = 0; i < size; i++) { - ret[i] = data[i]; - } - return ret; - } - } - - // Gnutls error codes. The mapping to a TLS alert is also shown in comments. - [CCode (cname = "int", cprefix = "GNUTLS_E_", lower_case_cprefix = "gnutls_error_", has_type_id = false)] - public enum ErrorCode { - SUCCESS, - UNKNOWN_COMPRESSION_ALGORITHM, - UNKNOWN_CIPHER_TYPE, - LARGE_PACKET, - UNSUPPORTED_VERSION_PACKET, // GNUTLS_A_PROTOCOL_VERSION - UNEXPECTED_PACKET_LENGTH, // GNUTLS_A_RECORD_OVERFLOW - INVALID_SESSION, - FATAL_ALERT_RECEIVED, - UNEXPECTED_PACKET, // GNUTLS_A_UNEXPECTED_MESSAGE - WARNING_ALERT_RECEIVED, - ERROR_IN_FINISHED_PACKET, - UNEXPECTED_HANDSHAKE_PACKET, - UNKNOWN_CIPHER_SUITE, // GNUTLS_A_HANDSHAKE_FAILURE - UNWANTED_ALGORITHM, - MPI_SCAN_FAILED, - DECRYPTION_FAILED, // GNUTLS_A_DECRYPTION_FAILED, GNUTLS_A_BAD_RECORD_MAC - MEMORY_ERROR, - DECOMPRESSION_FAILED, // GNUTLS_A_DECOMPRESSION_FAILURE - COMPRESSION_FAILED, - AGAIN, - EXPIRED, - DB_ERROR, - SRP_PWD_ERROR, - INSUFFICIENT_CREDENTIALS, - HASH_FAILED, - BASE64_DECODING_ERROR, - MPI_PRINT_FAILED, - REHANDSHAKE, // GNUTLS_A_NO_RENEGOTIATION - GOT_APPLICATION_DATA, - RECORD_LIMIT_REACHED, - ENCRYPTION_FAILED, - PK_ENCRYPTION_FAILED, - PK_DECRYPTION_FAILED, - PK_SIGN_FAILED, - X509_UNSUPPORTED_CRITICAL_EXTENSION, - KEY_USAGE_VIOLATION, - NO_CERTIFICATE_FOUND, // GNUTLS_A_BAD_CERTIFICATE - INVALID_REQUEST, - SHORT_MEMORY_BUFFER, - INTERRUPTED, - PUSH_ERROR, - PULL_ERROR, - RECEIVED_ILLEGAL_PARAMETER, // GNUTLS_A_ILLEGAL_PARAMETER - REQUESTED_DATA_NOT_AVAILABLE, - PKCS1_WRONG_PAD, - RECEIVED_ILLEGAL_EXTENSION, - INTERNAL_ERROR, - DH_PRIME_UNACCEPTABLE, - FILE_ERROR, - TOO_MANY_EMPTY_PACKETS, - UNKNOWN_PK_ALGORITHM, - // returned if libextra functionality was requested but - // gnutls_global_init_extra() was not called. - - INIT_LIBEXTRA, - LIBRARY_VERSION_MISMATCH, - // returned if you need to generate temporary RSA - // parameters. These are needed for export cipher suites. - - NO_TEMPORARY_RSA_PARAMS, - LZO_INIT_FAILED, - NO_COMPRESSION_ALGORITHMS, - NO_CIPHER_SUITES, - OPENPGP_GETKEY_FAILED, - PK_SIG_VERIFY_FAILED, - ILLEGAL_SRP_USERNAME, - SRP_PWD_PARSING_ERROR, - NO_TEMPORARY_DH_PARAMS, - // For certificate and key stuff - - ASN1_ELEMENT_NOT_FOUND, - ASN1_IDENTIFIER_NOT_FOUND, - ASN1_DER_ERROR, - ASN1_VALUE_NOT_FOUND, - ASN1_GENERIC_ERROR, - ASN1_VALUE_NOT_VALID, - ASN1_TAG_ERROR, - ASN1_TAG_IMPLICIT, - ASN1_TYPE_ANY_ERROR, - ASN1_SYNTAX_ERROR, - ASN1_DER_OVERFLOW, - OPENPGP_UID_REVOKED, - CERTIFICATE_ERROR, - CERTIFICATE_KEY_MISMATCH, - UNSUPPORTED_CERTIFICATE_TYPE, // GNUTLS_A_UNSUPPORTED_CERTIFICATE - X509_UNKNOWN_SAN, - OPENPGP_FINGERPRINT_UNSUPPORTED, - X509_UNSUPPORTED_ATTRIBUTE, - UNKNOWN_HASH_ALGORITHM, - UNKNOWN_PKCS_CONTENT_TYPE, - UNKNOWN_PKCS_BAG_TYPE, - INVALID_PASSWORD, - MAC_VERIFY_FAILED, // for PKCS #12 MAC - CONSTRAINT_ERROR, - WARNING_IA_IPHF_RECEIVED, - WARNING_IA_FPHF_RECEIVED, - IA_VERIFY_FAILED, - UNKNOWN_ALGORITHM, - BASE64_ENCODING_ERROR, - INCOMPATIBLE_CRYPTO_LIBRARY, - INCOMPATIBLE_LIBTASN1_LIBRARY, - OPENPGP_KEYRING_ERROR, - X509_UNSUPPORTED_OID, - RANDOM_FAILED, - BASE64_UNEXPECTED_HEADER_ERROR, - OPENPGP_SUBKEY_ERROR, - CRYPTO_ALREADY_REGISTERED, - HANDSHAKE_TOO_LARGE, - UNIMPLEMENTED_FEATURE, - APPLICATION_ERROR_MAX, // -65000 - APPLICATION_ERROR_MIN; // -65500 - - [CCode (cname = "gnutls_error_is_fatal")] - public bool is_fatal(); - - [CCode (cname = "gnutls_perror")] - public void print(); - - [CCode (cname = "gnutls_strerror")] - public unowned string to_string(); - } - - public void throw_if_error(int err_int) throws GLib.Error { - ErrorCode error = (ErrorCode)err_int; - if (error != ErrorCode.SUCCESS) { - throw new GLib.Error(-1, error, "%s%s", error.to_string(), error.is_fatal() ? " fatal" : ""); - } - } -} \ No newline at end of file diff --git a/plugins/ice/vapi/openssl.vapi b/plugins/ice/vapi/openssl.vapi deleted file mode 100644 index e0654869..00000000 --- a/plugins/ice/vapi/openssl.vapi +++ /dev/null @@ -1,218 +0,0 @@ -[CCode (cprefix = "")] -namespace OpenSSL { - [CCode (cprefix = "ASN1_", lower_case_cprefix = "ASN1_", cheader_filename = "openssl/asn1.h")] - namespace ASN1 { - [Compact] - [CCode (cname = "ASN1_INTEGER", lower_case_cprefix = "ASN1_INTEGER_", cprefix = "ASN1_INTEGER_", free_function = "ASN1_INTEGER_free")] - public class Integer { - public bool @set (long v); - } - - [Compact] - [CCode (cname = "ASN1_TIME", lower_case_cprefix = "ASN1_TIME_", cprefix = "ASN1_TIME_", copy_function = "ASN1_TIME_dup", free_function = "ASN1_STRING_free")] - public class Time { - public bool @set (time_t t); - } - } - [Compact] - [CCode (cname = "BIO", lower_case_cprefix = "BIO_", cprefix = "BIO_", ref_function = "BIO_up_ref", ref_function_void = true, unref_function = "BIO_free", cheader_filename = "openssl/bio.h", simple_generics = true)] - public class BIO { - [Compact] - [CCode (cname = "BIO_METHOD", lower_case_cprefix = "BIO_meth_", cprefix = "BIO_meth_", unref_function = "BIO_meth_free", simple_generics = true)] - public class Method { - public Method (Type type, string name); - [CCode (has_target = false)] - public delegate int WriteFunc (BIO bio, [CCode (type = "const char *", array_length_type = "int")] uint8[] buf); - [CCode (has_target = false)] - public delegate int ReadFunc (BIO bio, [CCode (type = "char *", array_length_type = "int")] uint8[] out_buf); - [CCode (has_target = false, simple_generics = true)] - public delegate long CtrlFunc (BIO bio, ControlCommand cmd, long num, void* ptr); - public WriteFunc write { get; set; } - public ReadFunc read { get; set; } - public CtrlFunc ctrl { get; set; } - } - - [Flags] - [CCode (cname = "int")] - public enum Type { - SOURCE_SINK, - DGRAM, - } - - [CCode (cname = "int", cprefix = "BIO_CTRL_", has_type_id = false)] - public enum ControlCommand { - RESET, - EOF, - SET, - GET, - PUSH, - POP, - GET_CLOSE, - SET_CLOSE, - PENDING, - FLUSH, - DUP, - WPENDING, - - DGRAM_QUERY_MTU, - DGRAM_GET_MTU, - DGRAM_SET_MTU, - DGRAM_SET_PEER, - DGRAM_SET_NEXT_TIMEOUT, - DGRAM_GET_PEER, - DGRAM_GET_FALLBACK_MTU, - DGRAM_SET_DONT_FRAG, - DGRAM_GET_MTU_OVERHEAD, - } - - public BIO (Method type); - public T data { get; set; } - - public void clear_retry_flags (); - public void set_retry_read (); - public bool should_read (); - } - - [CCode (cprefix = "ERR_", lower_case_cprefix = "ERR_", cheader_filename = "openssl/err.h")] - namespace ERR { - public ulong get_error (); - public unowned string lib_error_string (ulong error); - public unowned string reason_error_string (ulong error); - } - - [CCode (cprefix = "EVP_", lower_case_cprefix = "EVP_", cheader_filename = "openssl/evp.h")] - namespace EVP { - [Compact] - [CCode (cname = "EVP_PKEY", lower_case_cprefix = "EVP_PKEY_", cprefix = "EVP_PKEY_", ref_function = "EVP_PKEY_up_ref", ref_function_void = true, unref_function = "EVP_PKEY_free")] - public class Key { - public Key (); - [CCode (cheader_filename = "openssl_utils.h")] - public Key.generate_ecdsa (string curve); - } - [Compact] - [CCode (cname = "EVP_MD", lower_case_cprefix = "EVP_MD_", cprefix = "EVP_MD_", ref_function = "EVP_MD_up_ref", ref_function_void = true, unref_function = "EVP_MD_free")] - public class MessageDigest { - } - - public static unowned MessageDigest sha256 (); - } - - [Compact] - [CCode (cname = "SSL", lower_case_cprefix = "SSL_", cprefix = "SSL_", ref_function = "SSL_up_ref", ref_function_void = true, unref_function = "SSL_free", cheader_filename = "openssl/ssl.h")] - class SSL { - [Compact] - [CCode (cname = "SSL_CTX", lower_case_cprefix = "SSL_CTX_", cprefix = "SSL_CTX_", ref_function = "SSL_CTX_up_ref", ref_function_void = true, unref_function = "SSL_CTX_free")] - public class Context { - public Context (Method method); - public int set_tlsext_use_srtp (string profiles); - public int use_certificate (X509.Certificate certificate); - [CCode (cname = "SSL_CTX_use_PrivateKey")] - public int use_private_key (EVP.Key private_key); - } - - [CCode (cname = "const SSL_METHOD*")] - public enum Method { - [CCode (cname = "DTLS_client_method()")] - DTLS_CLIENT, - [CCode (cname = "DTLS_server_method()")] - DTLS_SERVER, - } - - [CCode (cname = "int", has_type_id = false)] - public enum Error { - NONE, - ZERO_RETURN, - WANT_READ, - WANT_WRITE, - WANT_CONNECT, - WANT_ACCEPT, - WANT_X509_LOOKUP, - WANT_ASYNC, - WANT_ASYNC_JOB, - WANT_CLIENT_HELLO_CB, - SYSCALL, - SSL - } - - public SSL (Context context); - public BIO rbio { get; [CCode (cname="SSL_set0_rbio")] owned set; } - public BIO wbio { get; [CCode (cname="SSL_set0_wbio")] owned set; } - - [CCode (cname = "SSL_do_handshake")] - private int do_handshake_ (); - [CCode (cname = "SSL_do_handshake_")] - public Error do_handshake () throws GLib.Error { - int ret = throw_if_error (do_handshake_ ()); - return get_error (ret); - } - [CCode (cname = "SSL_export_keying_material")] - private int export_keying_material_ (uint8[] out, string label, size_t llen, [CCode (type = "const unsigned char *")] string context, size_t contextlen, bool use_context); - [CCode (cname = "SSL_export_keying_material_")] - public void export_keying_material (uint8[] out, string label, string? context = null) throws GLib.Error { - throw_if_error (export_keying_material_ (@out, label, label.length, context, context == null ? 0 : context.length, context != null)); - } - public Error get_error (int ret); - public void set_accept_state (); - public void set_connect_state (); - public int want (); - } - - [CCode (cprefix = "X509_", lower_case_cprefix = "X509_", cheader_filename = "openssl/x509.h")] - namespace X509 { - [Compact] - [CCode (cname = "X509", lower_case_cprefix = "X509_", cprefix = "X509_", ref_function = "X509_up_ref", ref_function_void = true, unref_function = "X509_free")] - public class Certificate { - public Certificate (); - public ASN1.Time not_before { - [CCode (cname = "X509_get_notBefore")] - get; - [CCode (cname = "X509_set_notBefore")] - set; - } - public ASN1.Time not_after { - [CCode (cname = "X509_get_notAfter")] - get; - [CCode (cname = "X509_set_notAfter")] - set; - } - public ASN1.Integer serial_number { - [CCode (cname = "X509_get_serialNumber")] - get; - [CCode (cname = "X509_set_serialNumber")] - set; - } - [CCode (cname = "X509_digest")] - private int digest_ (EVP.MessageDigest type, uchar* md, ref uint len); - [CCode (cname = "X509_digest_")] - public uint8[] digest (EVP.MessageDigest type, size_t buf_len = 512) throws GLib.Error { - uint8[] buf = new uint8[buf_len]; - uint buf_out_size = (uint) buf_len; - throw_if_error (digest_ (type, (uchar*) buf, ref buf_out_size)); - - uint8[] ret = new uint8[buf_out_size]; - for (int i = 0; i < buf_out_size; i++) { - ret[i] = buf[i]; - } - return ret; - } - [CCode (cname = "X509_set_pubkey")] - public int set_public_key (EVP.Key public_key); - public int set_version (long version); - [CCode (cname = "X509_sign")] - private int sign_ (EVP.Key private_key, EVP.MessageDigest md); - [CCode (cname = "X509_sign_")] - public void sign (EVP.Key private_key, EVP.MessageDigest md) throws GLib.Error { - throw_if_error (sign_ (private_key, md)); - } - } - } - - public int throw_if_error(int res) throws GLib.Error { - if (res <= 0) { - ulong error = ERR.get_error (); - if (error == 0) return res; - throw new GLib.Error (-1, (int) error, "%s:%s", ERR.lib_error_string (error), ERR.reason_error_string (error)); - } - return res; - } -} \ No newline at end of file -- cgit v1.3 From cd0d992b9b5d3ea29c7d4a3aab76ad53319ea162 Mon Sep 17 00:00:00 2001 From: Marvin W Date: Sat, 3 Oct 2026 13:22:02 +0200 Subject: Move ICE into libdino --- libdino/meson.build | 2 + libdino/src/service/ice.vala | 538 +++++++++++++++++++++ libdino/src/service/module_manager.vala | 5 +- libdino/src/service/util.vala | 12 +- libdino/vapi/metadata/Nice-0.1.metadata | 11 + libdino/vapi/nice.vapi | 386 +++++++++++++++ plugins/ice/meson.build | 25 - plugins/ice/src/module.vala | 57 --- plugins/ice/src/plugin.vala | 104 ---- plugins/ice/src/register_plugin.vala | 3 - plugins/ice/src/transport_parameters.vala | 391 --------------- plugins/ice/src/util.vala | 18 - plugins/ice/vapi/metadata/Nice-0.1.metadata | 11 - plugins/ice/vapi/nice.vapi | 386 --------------- plugins/meson.build | 1 - .../src/module/xep/0065_socks5_bytestreams.vala | 4 + xmpp-vala/src/module/xep/0177_jingle_raw_udp.vala | 4 + 17 files changed, 956 insertions(+), 1002 deletions(-) create mode 100644 libdino/src/service/ice.vala create mode 100644 libdino/vapi/metadata/Nice-0.1.metadata create mode 100644 libdino/vapi/nice.vapi delete mode 100644 plugins/ice/meson.build delete mode 100644 plugins/ice/src/module.vala delete mode 100644 plugins/ice/src/plugin.vala delete mode 100644 plugins/ice/src/register_plugin.vala delete mode 100644 plugins/ice/src/transport_parameters.vala delete mode 100644 plugins/ice/src/util.vala delete mode 100644 plugins/ice/vapi/metadata/Nice-0.1.metadata delete mode 100644 plugins/ice/vapi/nice.vapi (limited to 'plugins/ice/src/module.vala') diff --git a/libdino/meson.build b/libdino/meson.build index 67ac6a83..e680b662 100644 --- a/libdino/meson.build +++ b/libdino/meson.build @@ -9,6 +9,7 @@ dependencies = [ dep_gmodule, dep_libsoup, dep_libsrtp2, + dep_nice, dep_sqlite3, dep_xmpp_vala ] @@ -63,6 +64,7 @@ sources = files( 'src/service/file_manager.vala', 'src/service/file_transfer_storage.vala', 'src/service/history_sync.vala', + 'src/service/ice.vala', 'src/service/jingle_file_transfers.vala', 'src/service/message_correction.vala', 'src/service/message_deletion.vala', diff --git a/libdino/src/service/ice.vala b/libdino/src/service/ice.vala new file mode 100644 index 00000000..0e168be9 --- /dev/null +++ b/libdino/src/service/ice.vala @@ -0,0 +1,538 @@ +using Gee; + +using Dino.Crypto; +using Xmpp; +using Xmpp.Xep; + +private extern const size_t NICE_ADDRESS_STRING_LEN; + +public class Dino.IceUdpModule : Xmpp.Xep.JingleIceUdp.Module { + + public string? stun_ip = null; + public uint stun_port = 0; + public string? turn_ip = null; + public Xep.ExternalServiceDiscovery.Service? turn_service = null; + + private static weak Nice.Agent? agent; + private HashMap cerds = new HashMap(); + + private uint timeout = 0; + + public override void attach(XmppStream stream) { + base.attach(stream); + external_discovery_refresh_services.begin(stream); + } + public override void detach(XmppStream stream) { + base.detach(stream); + stop_external_discovery_refresh_services(); + } + + private Nice.Agent get_agent() { + Nice.Agent? agent = this.agent; + if (agent == null) { + Nice.debug_enable(true); + agent = new Nice.Agent(MainContext.@default(), Nice.Compatibility.RFC5245); + if (stun_ip != null) { + agent.stun_server = stun_ip; + agent.stun_server_port = stun_port; + } + agent.ice_tcp = false; + agent.set_software("Dino"); + agent.weak_ref(agent_unweak); + this.agent = agent; + debug("STUN server for libnice %s %u", agent.stun_server, agent.stun_server_port); + } + return agent; + } + + private void agent_unweak() { + this.agent = null; + } + + public override Jingle.TransportParameters create_transport_parameters(XmppStream stream, uint8 components, Jid local_full_jid, Jid peer_full_jid) { + DtlsSrtp.CredentialsCapsule? cred = get_create_credentials(local_full_jid, peer_full_jid); + return new IceUdpTransportParameters(get_agent(), cred, turn_service, turn_ip, components, local_full_jid, peer_full_jid); + } + + public override Jingle.TransportParameters parse_transport_parameters(XmppStream stream, uint8 components, Jid local_full_jid, Jid peer_full_jid, StanzaNode transport) throws Jingle.IqError { + DtlsSrtp.CredentialsCapsule? cred = get_create_credentials(local_full_jid, peer_full_jid); + return new IceUdpTransportParameters(get_agent(), cred, turn_service, turn_ip, components, local_full_jid, peer_full_jid, transport); + } + + private DtlsSrtp.CredentialsCapsule? get_create_credentials(Jid local_full_jid, Jid peer_full_jid) { + string from_to_id = local_full_jid.to_string() + peer_full_jid.to_string(); + try { + if (!cerds.has_key(from_to_id)) cerds[from_to_id] = DtlsSrtp.generate_credentials(); + } catch (GLib.Error e) { + warning("Error creating dtls credentials: %s", e.message); + } + return cerds[from_to_id]; + } + + private void stop_external_discovery_refresh_services() { + if (timeout != 0) { + Source.remove(timeout); + timeout = 0; + } + } + + private async void external_discovery_refresh_services(XmppStream stream) { + stop_external_discovery_refresh_services(); + Gee.List services = yield ExternalServiceDiscovery.request_services(stream); + foreach (Xep.ExternalServiceDiscovery.Service service in services) { + if (service.transport == "udp" && (service.ty == "stun" || service.ty == "turn")) { + InetAddress? ip = yield lookup_ipv4_addess(service.host); + if (ip == null) continue; + + if (ip.is_any || ip.is_link_local || ip.is_loopback || ip.is_multicast || ip.is_site_local) { + warning("Ignoring STUN/TURN server at %s", service.host); + continue; + } + + if (service.ty == "stun") { + debug("Server offers STUN server: %s:%u, resolved to %s", service.host, service.port, ip.to_string()); + stun_ip = ip.to_string(); + stun_port = service.port; + } else if (service.ty == "turn") { + debug("Server offers TURN server: %s:%u, resolved to %s", service.host, service.port, ip.to_string()); + turn_ip = ip.to_string(); + turn_service = service; + } + } + } + if (stun_ip == null) { + InetAddress ip = yield lookup_ipv4_addess("stun.dino.im"); + if (ip == null) return; + + debug("Using fallback STUN server: stun.dino.im:7886, resolved to %s", ip.to_string()); + + stun_ip = ip.to_string(); + stun_port = 7886; + } + + // Refresh TURN credentials before they expire + if (turn_service != null) { + DateTime? expires = turn_service.expires; + if (expires != null) { + uint refresh_delay = (uint) (expires.to_unix() - new DateTime.now_utc().to_unix()) / 2; + + if (refresh_delay >= 300 && refresh_delay <= (int64) uint.MAX) { // 5 min < refetch < max + debug("Re-fetching TURN credentials in %u sec", refresh_delay); + + stop_external_discovery_refresh_services(); + timeout = Timeout.add_seconds((uint) refresh_delay, () => { + timeout = 0; + external_discovery_refresh_services.begin(stream); + return false; + }); + } else { + warning("TURN credentials' expiry time = %u, *not* re-fetching", refresh_delay); + } + } + } + } + + private async InetAddress? lookup_ipv4_addess(string host) { + try { + Resolver resolver = Resolver.get_default(); + GLib.List? ips = yield resolver.lookup_by_name_async(host); + foreach (GLib.InetAddress ina in ips) { + if (ina.get_family() != SocketFamily.IPV4) continue; + return ina; + } + } catch (GLib.Error e) { + warning("Failed looking up IP address of %s", host); + } + return null; + } +} + +public class Dino.IceUdpTransportParameters : JingleIceUdp.IceUdpTransportParameters { + private Nice.Agent agent; + private uint stream_id; + private bool we_want_connection; + private bool remote_credentials_set; + private Map connections = new HashMap(); + private DtlsSrtp.Handler? dtls_srtp_handler; + private MainContext thread_context; + private MainLoop thread_loop; + + private static bool is_component_ready(Nice.Agent agent, uint stream_id, uint component_id) { + var state = agent.get_component_state(stream_id, component_id); + return state == Nice.ComponentState.CONNECTED || state == Nice.ComponentState.READY; + } + + private class DatagramConnection : Jingle.DatagramConnection { + private Nice.Agent agent; + private DtlsSrtp.Handler? dtls_srtp_handler; + private uint stream_id; + private string? error; + private ulong datagram_received_id; + + internal DatagramConnection(Nice.Agent agent, DtlsSrtp.Handler? dtls_srtp_handler, uint stream_id, uint8 component_id) { + this.agent = agent; + this.dtls_srtp_handler = dtls_srtp_handler; + this.stream_id = stream_id; + this.component_id = component_id; + this.datagram_received_id = this.datagram_received.connect((datagram) => { + bytes_received += datagram.length; + }); + } + + public override async void terminate(bool we_terminated, string? reason_string = null, string? reason_text = null) { + yield base.terminate(we_terminated, reason_string, reason_text); + this.disconnect(datagram_received_id); + agent = null; + dtls_srtp_handler = null; + } + + public override void send_datagram(Bytes datagram) { + if (this.agent != null && is_component_ready(agent, stream_id, component_id)) { + try { + if (dtls_srtp_handler != null) { + uint8[] encrypted_data = dtls_srtp_handler.process_outgoing_data(component_id, datagram.get_data()); + if (encrypted_data == null) return; + GLib.OutputVector vector = { encrypted_data, encrypted_data.length }; + GLib.OutputVector[] vectors = { vector }; + Nice.OutputMessage message = { vectors }; + Nice.OutputMessage[] messages = { message }; + agent.send_messages_nonblocking(stream_id, component_id, messages); + } else { + GLib.OutputVector vector = { datagram.get_data(), datagram.get_size() }; + GLib.OutputVector[] vectors = { vector }; + Nice.OutputMessage message = { vectors }; + Nice.OutputMessage[] messages = { message }; + agent.send_messages_nonblocking(stream_id, component_id, messages); + } + bytes_sent += datagram.length; + } catch (GLib.Error e) { + warning("%s while send_datagram stream %u component %u", e.message, stream_id, component_id); + } + } + } + } + + internal IceUdpTransportParameters(Nice.Agent agent, DtlsSrtp.CredentialsCapsule? credentials, Xep.ExternalServiceDiscovery.Service? turn_service, string? turn_ip, uint8 components, Jid local_full_jid, Jid peer_full_jid, StanzaNode? node = null) { + base(components, local_full_jid, peer_full_jid, node); + this.we_want_connection = (node == null); + this.agent = agent; + + if (this.peer_fingerprint != null || !incoming) { + dtls_srtp_handler = setup_dtls(this, credentials); + own_fingerprint = dtls_srtp_handler.own_fingerprint; + if (incoming) { + own_setup = "active"; + dtls_srtp_handler.mode = DtlsSrtp.Mode.CLIENT; + dtls_srtp_handler.peer_fingerprint = peer_fingerprint; + dtls_srtp_handler.peer_fp_algo = peer_fp_algo; + } else { + own_setup = "actpass"; + dtls_srtp_handler.mode = DtlsSrtp.Mode.SERVER; + dtls_srtp_handler.setup_dtls_connection.begin((_, res) => { + var content_encryption = dtls_srtp_handler.setup_dtls_connection.end(res); + if (content_encryption != null) { + this.content.encryptions[content_encryption.encryption_ns] = content_encryption; + } + }); + } + } + + agent.candidate_gathering_done.connect(on_candidate_gathering_done); + agent.initial_binding_request_received.connect(on_initial_binding_request_received); + agent.component_state_changed.connect(on_component_state_changed); + agent.new_selected_pair_full.connect(on_new_selected_pair_full); + agent.new_candidate_full.connect(on_new_candidate); + + agent.controlling_mode = !incoming; + stream_id = agent.add_stream(components); + thread_context = new MainContext(); + new Thread(@"ice-thread-$stream_id", () => { + thread_context.push_thread_default(); + thread_loop = new MainLoop(thread_context, false); + thread_loop.run(); + thread_context.pop_thread_default(); + return null; + }); + + if (turn_ip != null) { + for (uint8 component_id = 1; component_id <= components; component_id++) { + agent.set_relay_info(stream_id, component_id, turn_ip, turn_service.port, turn_service.username, turn_service.password, Nice.RelayType.UDP); + debug("TURN info (component %i) %s:%u", component_id, turn_ip, turn_service.port); + } + } + string ufrag; + string pwd; + agent.get_local_credentials(stream_id, out ufrag, out pwd); + init(ufrag, pwd); + + for (uint8 component_id = 1; component_id <= components; component_id++) { + // We don't properly get local candidates before this call + agent.attach_recv(stream_id, component_id, thread_context, on_recv); + } + + agent.gather_candidates(stream_id); + } + + private static DtlsSrtp.Handler setup_dtls(IceUdpTransportParameters tp, DtlsSrtp.CredentialsCapsule credentials) { + var weak_self = WeakRef(tp); + DtlsSrtp.Handler dtls_srtp = new DtlsSrtp.Handler.with_cert(credentials); + dtls_srtp.send_data.connect((data) => { + IceUdpTransportParameters self = (IceUdpTransportParameters) weak_self.get(); + if (self != null) self.agent.send(self.stream_id, 1, data); + }); + return dtls_srtp; + } + + private void on_candidate_gathering_done(uint stream_id) { + if (stream_id != this.stream_id) return; + debug("on_candidate_gathering_done in %u", stream_id); + + for (uint8 i = 1; i <= components; i++) { + foreach (unowned Nice.Candidate nc in agent.get_local_candidates(stream_id, i)) { + if (nc.transport == Nice.CandidateTransport.UDP) { + JingleIceUdp.Candidate? candidate = candidate_to_jingle(nc); + if (candidate == null) continue; + debug("Local candidate summary: %s", agent.generate_local_candidate_sdp(nc)); + } + } + } + } + + private void on_new_candidate(Nice.Candidate nc) { + if (nc.stream_id != stream_id) return; + JingleIceUdp.Candidate? candidate = candidate_to_jingle(nc); + if (candidate == null) return; + + if (nc.transport == Nice.CandidateTransport.UDP) { + // Execution was in the agent thread before + add_local_candidate_threadsafe(candidate); + } + } + + private bool bytes_equal(uint8[] a1, uint8[] a2) { + return a1.length == a2.length && Memory.cmp(a1, a2, a1.length) == 0; + } + + public override void handle_transport_accept(StanzaNode transport) throws Jingle.IqError { + debug("on_transport_accept from %s", peer_full_jid.to_string()); + base.handle_transport_accept(transport); + + if (dtls_srtp_handler != null && peer_fingerprint != null) { + if (dtls_srtp_handler.peer_fingerprint != null) { + if (!bytes_equal(dtls_srtp_handler.peer_fingerprint, peer_fingerprint)) { + warning("Tried to replace certificate fingerprint mid use. We don't allow that."); + peer_fingerprint = dtls_srtp_handler.peer_fingerprint; + peer_fp_algo = dtls_srtp_handler.peer_fp_algo; + } + } else { + dtls_srtp_handler.peer_fingerprint = peer_fingerprint; + dtls_srtp_handler.peer_fp_algo = peer_fp_algo; + } + if (peer_setup == "passive") { + dtls_srtp_handler.mode = DtlsSrtp.Mode.CLIENT; + dtls_srtp_handler.stop_dtls_connection(); + dtls_srtp_handler.setup_dtls_connection.begin((_, res) => { + var content_encryption = dtls_srtp_handler.setup_dtls_connection.end(res); + if (content_encryption != null) { + this.content.encryptions[content_encryption.encryption_ns] = content_encryption; + } + }); + } + } else { + dtls_srtp_handler = null; + } + } + + public override void handle_transport_info(StanzaNode transport) throws Jingle.IqError { + debug("on_transport_info from %s", peer_full_jid.to_string()); + base.handle_transport_info(transport); + + if (dtls_srtp_handler != null && peer_fingerprint != null) { + if (dtls_srtp_handler.peer_fingerprint != null) { + if (!bytes_equal(dtls_srtp_handler.peer_fingerprint, peer_fingerprint)) { + warning("Tried to replace certificate fingerprint mid use. We don't allow that."); + peer_fingerprint = dtls_srtp_handler.peer_fingerprint; + peer_fp_algo = dtls_srtp_handler.peer_fp_algo; + } + } else { + dtls_srtp_handler.peer_fingerprint = peer_fingerprint; + dtls_srtp_handler.peer_fp_algo = peer_fp_algo; + } + } + + if (!we_want_connection) return; + + if (remote_ufrag != null && remote_pwd != null && !remote_credentials_set) { + agent.set_remote_credentials(stream_id, remote_ufrag, remote_pwd); + remote_credentials_set = true; + } + for (uint8 i = 1; i <= components; i++) { + SList candidates = new SList(); + foreach (JingleIceUdp.Candidate candidate in remote_candidates) { + if (candidate.component == i) { + candidates.append(candidate_to_nice(candidate)); + } + } + int new_candidates = agent.set_remote_candidates(stream_id, i, candidates); + debug("Updated to %i remote candidates for candidate %u via transport info", new_candidates, i); + } + } + + public override void create_transport_connection(XmppStream stream, Jingle.Content content) { + debug("create_transport_connection: %s", content.session.sid); + debug("local_credentials: %s %s", local_ufrag, local_pwd); + debug("remote_credentials: %s %s", remote_ufrag, remote_pwd); + debug("expected incoming credentials: %s %s", local_ufrag + ":" + remote_ufrag, local_pwd); + debug("expected outgoing credentials: %s %s", remote_ufrag + ":" + local_ufrag, remote_pwd); + + we_want_connection = true; + + if (remote_ufrag != null && remote_pwd != null && !remote_credentials_set) { + agent.set_remote_credentials(stream_id, remote_ufrag, remote_pwd); + remote_credentials_set = true; + } + for (uint8 i = 1; i <= components; i++) { + SList candidates = new SList(); + foreach (JingleIceUdp.Candidate candidate in remote_candidates) { + if (candidate.ip.has_prefix("fe80::")) continue; + if (candidate.component == i) { + candidates.append(candidate_to_nice(candidate)); + debug("remote candidate: %s", agent.generate_local_candidate_sdp(candidate_to_nice(candidate))); + } + } + int new_candidates = agent.set_remote_candidates(stream_id, i, candidates); + debug("Initiated component %u with %i remote candidates", i, new_candidates); + + connections[i] = new DatagramConnection(agent, dtls_srtp_handler, stream_id, i); + content.set_transport_connection(connections[i], i); + } + + base.create_transport_connection(stream, content); + } + + private void on_component_state_changed(uint stream_id, uint component_id, uint state) { + if (stream_id != this.stream_id) return; + debug("stream %u component %u state changed to %s", stream_id, component_id, agent.get_component_state(stream_id, component_id).to_string()); + may_consider_ready(stream_id, component_id); + if (incoming && dtls_srtp_handler != null && !dtls_srtp_handler.ready && is_component_ready(agent, stream_id, component_id) && dtls_srtp_handler.mode == DtlsSrtp.Mode.CLIENT) { + dtls_srtp_handler.setup_dtls_connection.begin((_, res) => { + Jingle.ContentEncryption? encryption = dtls_srtp_handler.setup_dtls_connection.end(res); + if (encryption != null) { + this.content.encryptions[encryption.encryption_ns] = encryption; + } + }); + } + } + + private void may_consider_ready(uint stream_id, uint component_id) { + if (stream_id != this.stream_id) return; + if (connections.has_key((uint8) component_id) && !connections[(uint8)component_id].ready && is_component_ready(agent, stream_id, component_id) && (dtls_srtp_handler == null || dtls_srtp_handler.ready)) { + connections[(uint8)component_id].ready = true; + } + } + + private void on_initial_binding_request_received(uint stream_id) { + if (stream_id != this.stream_id) return; + debug("initial_binding_request_received"); + } + + private void on_new_selected_pair_full(uint stream_id, uint component_id, Nice.Candidate p1, Nice.Candidate p2) { + if (stream_id != this.stream_id) return; + debug("new_selected_pair_full %u [%s, %s]", component_id, agent.generate_local_candidate_sdp(p1), agent.generate_local_candidate_sdp(p2)); + } + + private void on_recv(Nice.Agent agent, uint stream_id, uint component_id, uint8[] data) { + if (stream_id != this.stream_id) return; + uint8[] decrypt_data = null; + if (dtls_srtp_handler != null) { + try { + decrypt_data = dtls_srtp_handler.process_incoming_data(component_id, data); + if (decrypt_data == null) return; + } catch (Crypto.Error e) { + warning("%s while on_recv stream %u component %u", e.message, stream_id, component_id); + return; + } + } + may_consider_ready(stream_id, component_id); + if (connections.has_key((uint8) component_id)) { + if (!connections[(uint8) component_id].ready) { + debug("on_recv stream %u component %u when state %s", stream_id, component_id, agent.get_component_state(stream_id, component_id).to_string()); + } + connections[(uint8) component_id].datagram_received(new Bytes(decrypt_data ?? data)); + } else { + debug("on_recv stream %u component %u length %u", stream_id, component_id, data.length); + } + } + + private static Nice.Candidate candidate_to_nice(JingleIceUdp.Candidate c) { + Nice.CandidateType type; + switch (c.type_) { + case JingleIceUdp.Candidate.Type.HOST: type = Nice.CandidateType.HOST; break; + case JingleIceUdp.Candidate.Type.PRFLX: type = Nice.CandidateType.PEER_REFLEXIVE; break; + case JingleIceUdp.Candidate.Type.RELAY: type = Nice.CandidateType.RELAYED; break; + case JingleIceUdp.Candidate.Type.SRFLX: type = Nice.CandidateType.SERVER_REFLEXIVE; break; + default: assert_not_reached(); + } + + Nice.Candidate candidate = new Nice.Candidate(type); + candidate.component_id = c.component; + char[] foundation = new char[Nice.CANDIDATE_MAX_FOUNDATION]; + Memory.copy(foundation, c.foundation.data, size_t.min(c.foundation.length, Nice.CANDIDATE_MAX_FOUNDATION - 1)); + candidate.foundation = foundation; + candidate.addr = Nice.Address(); + candidate.addr.init(); + candidate.addr.set_from_string(c.ip); + candidate.addr.set_port(c.port); + candidate.priority = c.priority; + if (c.rel_addr != null) { + candidate.base_addr = Nice.Address(); + candidate.base_addr.init(); + candidate.base_addr.set_from_string(c.rel_addr); + candidate.base_addr.set_port(c.rel_port); + } + candidate.transport = Nice.CandidateTransport.UDP; + return candidate; + } + + private static JingleIceUdp.Candidate? candidate_to_jingle(Nice.Candidate nc) { + JingleIceUdp.Candidate candidate = new JingleIceUdp.Candidate(); + switch (nc.type) { + case Nice.CandidateType.HOST: candidate.type_ = JingleIceUdp.Candidate.Type.HOST; break; + case Nice.CandidateType.PEER_REFLEXIVE: candidate.type_ = JingleIceUdp.Candidate.Type.PRFLX; break; + case Nice.CandidateType.RELAYED: candidate.type_ = JingleIceUdp.Candidate.Type.RELAY; break; + case Nice.CandidateType.SERVER_REFLEXIVE: candidate.type_ = JingleIceUdp.Candidate.Type.SRFLX; break; + default: assert_not_reached(); + } + candidate.component = (uint8) nc.component_id; + candidate.foundation = ((string)nc.foundation).dup(); + candidate.generation = 0; + candidate.id = Random.next_int().to_string("%08x"); // TODO + + char[] res = new char[NICE_ADDRESS_STRING_LEN]; + nc.addr.to_string(res); + candidate.ip = (string) res; + candidate.network = 0; // TODO + candidate.port = (uint16) nc.addr.get_port(); + candidate.priority = nc.priority; + candidate.protocol = "udp"; + if (nc.base_addr.is_valid() && !nc.base_addr.equal(nc.addr)) { + res = new char[NICE_ADDRESS_STRING_LEN]; + nc.base_addr.to_string(res); + candidate.rel_addr = (string) res; + candidate.rel_port = (uint16) nc.base_addr.get_port(); + } + if (candidate.ip.has_prefix("fe80::")) return null; + + return candidate; + } + + public override void dispose() { + base.dispose(); + agent = null; + dtls_srtp_handler = null; + connections.clear(); + if (thread_loop != null) { + thread_loop.quit(); + } + } +} diff --git a/libdino/src/service/module_manager.vala b/libdino/src/service/module_manager.vala index f7cad0fe..a6efe629 100644 --- a/libdino/src/service/module_manager.vala +++ b/libdino/src/service/module_manager.vala @@ -85,7 +85,7 @@ public class ModuleManager { module_map[account].add(new Xep.InBandRegistration.Module()); module_map[account].add(new Xep.HttpFileUpload.Module()); module_map[account].add(new Xep.Reactions.Module()); - module_map[account].add(new Xep.Socks5Bytestreams.Module()); + module_map[account].add(new Xep.Socks5Bytestreams.Module.with_local_ip_address_handler(Util.get_local_ip_addresses)); module_map[account].add(new Xep.InBandBytestreams.Module()); module_map[account].add(new Xep.Jingle.Module()); module_map[account].add(new Xep.JingleSocks5Bytestreams.Module()); @@ -97,11 +97,12 @@ public class ModuleManager { module_map[account].add(new Xep.JingleMessageInitiation.Module()); module_map[account].add(new Xep.OccupantIds.Module()); module_map[account].add(new Xep.MessageRetraction.Module()); - module_map[account].add(new Xep.JingleRawUdp.Module()); + module_map[account].add(new Xep.JingleRawUdp.Module.with_local_ip_address_handler(Util.get_local_ip_addresses)); module_map[account].add(new Xep.Muji.Module()); module_map[account].add(new Xep.CallInvites.Module()); module_map[account].add(new Xep.Coin.Module()); module_map[account].add(new Xep.MessageDisplayedSynchronization.Module()); + module_map[account].add(new IceUdpModule()); initialize_account_modules(account, module_map[account]); } } diff --git a/libdino/src/service/util.vala b/libdino/src/service/util.vala index ac596cad..577bc6cc 100644 --- a/libdino/src/service/util.vala +++ b/libdino/src/service/util.vala @@ -1,8 +1,6 @@ using Dino.Entities; -namespace Dino { - -public class Util { +namespace Dino.Util { public static Message.Type get_message_type_for_conversation(Conversation conversation) { switch (conversation.type_) { case Conversation.Type.CHAT: @@ -41,6 +39,12 @@ public class Util { } return false; } -} + public static Gee.List get_local_ip_addresses() { + Gee.List result = new Gee.ArrayList(); + foreach (string ip_address in Nice.interfaces_get_local_ips(false)) { + result.add(ip_address); + } + return result; + } } diff --git a/libdino/vapi/metadata/Nice-0.1.metadata b/libdino/vapi/metadata/Nice-0.1.metadata new file mode 100644 index 00000000..7fcf046a --- /dev/null +++ b/libdino/vapi/metadata/Nice-0.1.metadata @@ -0,0 +1,11 @@ +Nice cheader_filename="nice.h" +Address.to_string.dst type="char[]" +Agent.new_reliable#constructor name="create_reliable" +Agent.attach_recv skip=false +Agent.send.buf type="uint8[]" array_length_idx=2 +AgentRecvFunc.buf type="uint8[]" array_length_idx=3 +PseudoTcpCallbacks#record skip +PseudoTcpSocket#class skip + +# Not yet supported by vapigen +# Candidate copy_function="nice_candidate_copy" free_function="nice_candidate_free" type_id="" diff --git a/libdino/vapi/nice.vapi b/libdino/vapi/nice.vapi new file mode 100644 index 00000000..540e2b4e --- /dev/null +++ b/libdino/vapi/nice.vapi @@ -0,0 +1,386 @@ +/* nice.vapi generated by vapigen, do not modify. */ + +[CCode (cprefix = "Nice", gir_namespace = "Nice", gir_version = "0.1", lower_case_cprefix = "nice_")] +namespace Nice { + [CCode (cheader_filename = "nice.h", type_id = "nice_agent_get_type ()")] + public class Agent : GLib.Object { + [CCode (has_construct_function = false)] + public Agent (GLib.MainContext ctx, Nice.Compatibility compat); + public bool add_local_address (Nice.Address addr); + public uint add_stream (uint n_components); + public bool attach_recv (uint stream_id, uint component_id, GLib.MainContext ctx, Nice.AgentRecvFunc func); + [Version (since = "0.1.16")] + public async void close_async (); + [CCode (cname = "nice_agent_new_reliable", has_construct_function = false)] + [Version (since = "0.0.11")] + public Agent.create_reliable (GLib.MainContext ctx, Nice.Compatibility compat); + [Version (since = "0.1.6")] + public bool forget_relays (uint stream_id, uint component_id); + [CCode (has_construct_function = false)] + [Version (since = "0.1.15")] + public Agent.full (GLib.MainContext ctx, Nice.Compatibility compat, Nice.AgentOption flags); + public bool gather_candidates (uint stream_id); + [Version (since = "0.1.4")] + public string generate_local_candidate_sdp (Nice.Candidate candidate); + [Version (since = "0.1.4")] + public string generate_local_sdp (); + [Version (since = "0.1.4")] + public string generate_local_stream_sdp (uint stream_id, bool include_non_ice); + [Version (since = "0.1.8")] + public Nice.ComponentState get_component_state (uint stream_id, uint component_id); + public Nice.Candidate get_default_local_candidate (uint stream_id, uint component_id); + [Version (since = "0.1.5")] + public GLib.IOStream get_io_stream (uint stream_id, uint component_id); + public GLib.SList get_local_candidates (uint stream_id, uint component_id); + public bool get_local_credentials (uint stream_id, out string ufrag, out string pwd); + public GLib.SList get_remote_candidates (uint stream_id, uint component_id); + public bool get_selected_pair (uint stream_id, uint component_id, Nice.Candidate local, Nice.Candidate remote); + [Version (since = "0.1.5")] + public GLib.Socket? get_selected_socket (uint stream_id, uint component_id); + [Version (since = "0.1.4")] + public unowned string get_stream_name (uint stream_id); + [Version (since = "0.1.4")] + public Nice.Candidate parse_remote_candidate_sdp (uint stream_id, string sdp); + [Version (since = "0.1.4")] + public int parse_remote_sdp (string sdp); + [Version (since = "0.1.4")] + public GLib.SList parse_remote_stream_sdp (uint stream_id, string sdp, string ufrag, string pwd); + [Version (since = "0.1.16")] + public bool peer_candidate_gathering_done (uint stream_id); + [Version (since = "0.1.5")] + public ssize_t recv (uint stream_id, uint component_id, [CCode (array_length_cname = "buf_len", array_length_pos = 3.5, array_length_type = "gsize")] out unowned uint8[] buf, GLib.Cancellable? cancellable = null) throws GLib.Error; + [Version (since = "0.1.5")] + public int recv_messages (uint stream_id, uint component_id, [CCode (array_length_cname = "n_messages", array_length_pos = 3.5, array_length_type = "guint")] out unowned Nice.InputMessage[] messages, GLib.Cancellable? cancellable = null) throws GLib.Error; + [Version (since = "0.1.5")] + public int recv_messages_nonblocking (uint stream_id, uint component_id, [CCode (array_length_cname = "n_messages", array_length_pos = 3.5, array_length_type = "guint")] out unowned Nice.InputMessage[] messages, GLib.Cancellable? cancellable = null) throws GLib.Error; + [Version (since = "0.1.5")] + public ssize_t recv_nonblocking (uint stream_id, uint component_id, [CCode (array_length_cname = "buf_len", array_length_pos = 3.5, array_length_type = "gsize")] out unowned uint8[] buf, GLib.Cancellable? cancellable = null) throws GLib.Error; + public void remove_stream (uint stream_id); + public bool restart (); + [Version (since = "0.1.6")] + public bool restart_stream (uint stream_id); + public int send (uint stream_id, uint component_id, [CCode (array_length_cname = "len", array_length_pos = 2.5, array_length_type = "guint", type = "const gchar*")] uint8[] buf); + [Version (since = "0.1.5")] + public int send_messages_nonblocking (uint stream_id, uint component_id, [CCode (array_length_cname = "n_messages", array_length_pos = 3.5, array_length_type = "guint")] Nice.OutputMessage[] messages, GLib.Cancellable? cancellable = null) throws GLib.Error; + public bool set_local_credentials (uint stream_id, string ufrag, string pwd); + public void set_port_range (uint stream_id, uint component_id, uint min_port, uint max_port); + public bool set_relay_info (uint stream_id, uint component_id, string server_ip, uint server_port, string username, string password, Nice.RelayType type); + public int set_remote_candidates (uint stream_id, uint component_id, GLib.SList candidates); + public bool set_remote_credentials (uint stream_id, string ufrag, string pwd); + public bool set_selected_pair (uint stream_id, uint component_id, string lfoundation, string rfoundation); + public bool set_selected_remote_candidate (uint stream_id, uint component_id, Nice.Candidate candidate); + [Version (since = "0.0.10")] + public void set_software (string software); + [Version (since = "0.1.4")] + public bool set_stream_name (uint stream_id, string name); + [Version (since = "0.0.9")] + public void set_stream_tos (uint stream_id, int tos); + [NoAccessorMethod] + [Version (since = "0.1.8")] + public bool bytestream_tcp { get; } + [NoAccessorMethod] + public uint compatibility { get; construct; } + [NoAccessorMethod] + public bool controlling_mode { get; set; } + [NoAccessorMethod] + [Version (since = "0.1.14")] + public bool force_relay { get; set; } + [NoAccessorMethod] + public bool full_mode { get; construct; } + [NoAccessorMethod] + [Version (since = "0.1.8")] + public bool ice_tcp { get; set; } + [NoAccessorMethod] + [Version (since = "0.1.16")] + public bool ice_trickle { get; set; } + [NoAccessorMethod] + [Version (since = "0.1.8")] + public bool ice_udp { get; set; } + [NoAccessorMethod] + [Version (since = "0.1.8")] + public bool keepalive_conncheck { get; set; } + [NoAccessorMethod] + public void* main_context { get; construct; } + [NoAccessorMethod] + public uint max_connectivity_checks { get; set; } + [NoAccessorMethod] + [Version (since = "0.0.4")] + public string proxy_ip { owned get; set; } + [NoAccessorMethod] + [Version (since = "0.0.4")] + public string proxy_password { owned get; set; } + [NoAccessorMethod] + [Version (since = "0.0.4")] + public uint proxy_port { get; set; } + [NoAccessorMethod] + [Version (since = "0.0.4")] + public uint proxy_type { get; set; } + [NoAccessorMethod] + [Version (since = "0.0.4")] + public string proxy_username { owned get; set; } + [NoAccessorMethod] + [Version (since = "0.0.11")] + public bool reliable { get; construct; } + [NoAccessorMethod] + [Version (since = "0.1.15")] + public uint stun_initial_timeout { get; set construct; } + [NoAccessorMethod] + [Version (since = "0.1.15")] + public uint stun_max_retransmissions { get; set construct; } + [NoAccessorMethod] + public uint stun_pacing_timer { get; set construct; } + [NoAccessorMethod] + [Version (since = "0.1.15")] + public uint stun_reliable_timeout { get; set construct; } + [NoAccessorMethod] + public string stun_server { owned get; set; } + [NoAccessorMethod] + public uint stun_server_port { get; set; } + [NoAccessorMethod] + public bool support_renomination { get; set; } + [NoAccessorMethod] + [Version (since = "0.0.7")] + public bool upnp { get; set construct; } + [NoAccessorMethod] + [Version (since = "0.0.7")] + public uint upnp_timeout { get; set construct; } + public signal void candidate_gathering_done (uint stream_id); + public signal void component_state_changed (uint stream_id, uint component_id, uint state); + public signal void initial_binding_request_received (uint stream_id); + [Version (deprecated = true, deprecated_since = "0.1.8")] + public signal void new_candidate (uint stream_id, uint component_id, string foundation); + [Version (since = "0.1.8")] + public signal void new_candidate_full (Nice.Candidate candidate); + [Version (deprecated = true, deprecated_since = "0.1.8")] + public signal void new_remote_candidate (uint stream_id, uint component_id, string foundation); + [Version (since = "0.1.8")] + public signal void new_remote_candidate_full (Nice.Candidate candidate); + [Version (deprecated = true, deprecated_since = "0.1.8")] + public signal void new_selected_pair (uint stream_id, uint component_id, string lfoundation, string rfoundation); + [Version (since = "0.1.8")] + public signal void new_selected_pair_full (uint stream_id, uint component_id, Nice.Candidate lcandidate, Nice.Candidate rcandidate); + [Version (since = "0.0.11")] + public signal void reliable_transport_writable (uint stream_id, uint component_id); + [Version (since = "0.1.5")] + public signal void streams_removed ([CCode (array_length = false, array_null_terminated = true)] uint[] stream_ids); + } + [CCode (cheader_filename = "nice.h", copy_function = "nice_candidate_copy", free_function = "nice_candidate_free")] + [Compact] + public class Candidate { + public Nice.Address addr; + public Nice.Address base_addr; + public uint component_id; + [CCode (array_length = false)] + public weak char foundation[33]; + public weak string password; + public uint32 priority; + public void* sockptr; + public uint stream_id; + public Nice.CandidateTransport transport; + public Nice.TurnServer turn; + public Nice.CandidateType type; + public weak string username; + [CCode (has_construct_function = false)] + public Candidate (Nice.CandidateType type); + public Nice.Candidate copy (); + [Version (since = "0.1.15")] + public bool equal_target (Nice.Candidate candidate2); + public void free (); + } + [CCode (cheader_filename = "nice.h", has_type_id = false)] + public struct Address { + [CCode (cname = "s.addr")] + public void* s_addr; + [CCode (cname = "s.ip4")] + public void* s_ip4; + [CCode (cname = "s.ip6")] + public void* s_ip6; + public void copy_to_sockaddr (void* sin); + public bool equal (Nice.Address b); + [Version (since = "0.1.8")] + public bool equal_no_port (Nice.Address b); + public void free (); + public uint get_port (); + public void init (); + public int ip_version (); + public bool is_private (); + public bool is_valid (); + public void set_from_sockaddr (void* sin); + public bool set_from_string (string str); + public void set_ipv4 (uint32 addr_ipv4); + public void set_ipv6 (uint8 addr_ipv6); + public void set_port (uint port); + public void to_string ([CCode (array_length = false, type = "gchar*")] char[] dst); + } + [CCode (cheader_filename = "nice.h", has_type_id = false)] + [Version (since = "0.1.5")] + public struct InputMessage { + [CCode (array_length_cname = "n_buffers")] + public weak GLib.InputVector[] buffers; + public int n_buffers; + public Nice.Address from; + public size_t length; + } + [CCode (cheader_filename = "nice.h", has_type_id = false)] + [Version (since = "0.1.5")] + public struct OutputMessage { + [CCode (array_length_cname = "n_buffers")] + public weak GLib.OutputVector[] buffers; + public int n_buffers; + } + [CCode (cheader_filename = "nice.h", cname = "TurnServer", has_type_id = false)] + public struct TurnServer { + public int ref_count; + public Nice.Address server; + public weak string username; + public weak string password; + public Nice.RelayType type; + } + [CCode (cheader_filename = "nice.h", cprefix = "NICE_AGENT_OPTION_", has_type_id = false)] + [Flags] + [Version (since = "0.1.15")] + public enum AgentOption { + REGULAR_NOMINATION, + RELIABLE, + LITE_MODE, + ICE_TRICKLE, + SUPPORT_RENOMINATION + } + [CCode (cheader_filename = "nice.h", cprefix = "NICE_CANDIDATE_TRANSPORT_", has_type_id = false)] + public enum CandidateTransport { + UDP, + TCP_ACTIVE, + TCP_PASSIVE, + TCP_SO + } + [CCode (cheader_filename = "nice.h", cprefix = "NICE_CANDIDATE_TYPE_", has_type_id = false)] + public enum CandidateType { + HOST, + SERVER_REFLEXIVE, + PEER_REFLEXIVE, + RELAYED + } + [CCode (cheader_filename = "nice.h", cprefix = "NICE_COMPATIBILITY_", has_type_id = false)] + public enum Compatibility { + RFC5245, + DRAFT19, + GOOGLE, + MSN, + WLM2009, + OC2007, + OC2007R2, + LAST + } + [CCode (cheader_filename = "nice.h", cprefix = "NICE_COMPONENT_STATE_", has_type_id = false)] + public enum ComponentState { + DISCONNECTED, + GATHERING, + CONNECTING, + CONNECTED, + READY, + FAILED, + LAST; + [Version (since = "0.1.6")] + public unowned string to_string (); + } + [CCode (cheader_filename = "nice.h", cprefix = "NICE_COMPONENT_TYPE_", has_type_id = false)] + public enum ComponentType { + RTP, + RTCP + } + [CCode (cheader_filename = "nice.h", cprefix = "NICE_NOMINATION_MODE_", has_type_id = false)] + [Version (since = "0.1.15")] + public enum NominationMode { + REGULAR, + AGGRESSIVE + } + [CCode (cheader_filename = "nice.h", cprefix = "NICE_PROXY_TYPE_", has_type_id = false)] + [Version (since = "0.0.4")] + public enum ProxyType { + NONE, + SOCKS5, + HTTP, + LAST + } + [CCode (cheader_filename = "nice.h", cname = "PseudoTcpDebugLevel", cprefix = "PSEUDO_TCP_DEBUG_", has_type_id = false)] + [Version (since = "0.0.11")] + public enum PseudoTcpDebugLevel { + NONE, + NORMAL, + VERBOSE + } + [CCode (cheader_filename = "nice.h", cname = "PseudoTcpShutdown", cprefix = "PSEUDO_TCP_SHUTDOWN_", has_type_id = false)] + [Version (since = "0.1.8")] + public enum PseudoTcpShutdown { + RD, + WR, + RDWR + } + [CCode (cheader_filename = "nice.h", cname = "PseudoTcpState", cprefix = "PSEUDO_TCP_", has_type_id = false)] + [Version (since = "0.0.11")] + public enum PseudoTcpState { + LISTEN, + SYN_SENT, + SYN_RECEIVED, + ESTABLISHED, + CLOSED, + FIN_WAIT_1, + FIN_WAIT_2, + CLOSING, + TIME_WAIT, + CLOSE_WAIT, + LAST_ACK + } + [CCode (cheader_filename = "nice.h", cname = "PseudoTcpWriteResult", cprefix = "WR_", has_type_id = false)] + [Version (since = "0.0.11")] + public enum PseudoTcpWriteResult { + SUCCESS, + TOO_LARGE, + FAIL + } + [CCode (cheader_filename = "nice.h", cprefix = "NICE_RELAY_TYPE_TURN_", has_type_id = false)] + public enum RelayType { + UDP, + TCP, + TLS + } + [CCode (cheader_filename = "nice.h", instance_pos = 4.9)] + public delegate void AgentRecvFunc (Nice.Agent agent, uint stream_id, uint component_id, [CCode (array_length_cname = "len", array_length_pos = 3.5, array_length_type = "guint", type = "gchar*")] uint8[] buf); + [CCode (cheader_filename = "nice.h", cname = "NICE_AGENT_MAX_REMOTE_CANDIDATES")] + public const int AGENT_MAX_REMOTE_CANDIDATES; + [CCode (cheader_filename = "nice.h", cname = "NICE_CANDIDATE_DIRECTION_MS_PREF_ACTIVE")] + public const int CANDIDATE_DIRECTION_MS_PREF_ACTIVE; + [CCode (cheader_filename = "nice.h", cname = "NICE_CANDIDATE_DIRECTION_MS_PREF_PASSIVE")] + public const int CANDIDATE_DIRECTION_MS_PREF_PASSIVE; + [CCode (cheader_filename = "nice.h", cname = "NICE_CANDIDATE_MAX_FOUNDATION")] + public const int CANDIDATE_MAX_FOUNDATION; + [CCode (cheader_filename = "nice.h", cname = "NICE_CANDIDATE_TRANSPORT_MS_PREF_TCP")] + public const int CANDIDATE_TRANSPORT_MS_PREF_TCP; + [CCode (cheader_filename = "nice.h", cname = "NICE_CANDIDATE_TRANSPORT_MS_PREF_UDP")] + public const int CANDIDATE_TRANSPORT_MS_PREF_UDP; + [CCode (cheader_filename = "nice.h", cname = "NICE_CANDIDATE_TYPE_PREF_HOST")] + public const int CANDIDATE_TYPE_PREF_HOST; + [CCode (cheader_filename = "nice.h", cname = "NICE_CANDIDATE_TYPE_PREF_NAT_ASSISTED")] + public const int CANDIDATE_TYPE_PREF_NAT_ASSISTED; + [CCode (cheader_filename = "nice.h", cname = "NICE_CANDIDATE_TYPE_PREF_PEER_REFLEXIVE")] + public const int CANDIDATE_TYPE_PREF_PEER_REFLEXIVE; + [CCode (cheader_filename = "nice.h", cname = "NICE_CANDIDATE_TYPE_PREF_RELAYED")] + public const int CANDIDATE_TYPE_PREF_RELAYED; + [CCode (cheader_filename = "nice.h", cname = "NICE_CANDIDATE_TYPE_PREF_RELAYED_UDP")] + public const int CANDIDATE_TYPE_PREF_RELAYED_UDP; + [CCode (cheader_filename = "nice.h", cname = "NICE_CANDIDATE_TYPE_PREF_SERVER_REFLEXIVE")] + public const int CANDIDATE_TYPE_PREF_SERVER_REFLEXIVE; + [CCode (cheader_filename = "nice.h")] + public static void debug_disable (bool with_stun); + [CCode (cheader_filename = "nice.h")] + public static void debug_enable (bool with_stun); + [CCode (cheader_filename = "nice.h")] + public static string? interfaces_get_ip_for_interface (string interface_name); + [CCode (cheader_filename = "nice.h")] + public static GLib.List interfaces_get_local_interfaces (); + [CCode (cheader_filename = "nice.h")] + public static GLib.List interfaces_get_local_ips (bool include_loopback); + [CCode (cheader_filename = "nice.h", cname = "pseudo_tcp_set_debug_level")] + [Version (since = "0.0.11")] + public static void pseudo_tcp_set_debug_level (Nice.PseudoTcpDebugLevel level); +} diff --git a/plugins/ice/meson.build b/plugins/ice/meson.build deleted file mode 100644 index 19030259..00000000 --- a/plugins/ice/meson.build +++ /dev/null @@ -1,25 +0,0 @@ -dependencies = [ - dep_dino, - dep_gee, - dep_gio, - dep_glib, - dep_gmodule, - dep_nice, - dep_xmpp_vala, -] -sources = files( - 'src/module.vala', - 'src/plugin.vala', - 'src/transport_parameters.vala', - 'src/util.vala', - 'src/register_plugin.vala', -) -c_args = vala_c_args + [ - '-DG_LOG_DOMAIN="ice"', -] -vala_args = [ - '--vapidir', meson.current_source_dir() / 'vapi', -] -lib_ice = shared_library('ice', sources, name_prefix: '', c_args: c_args, vala_args: vala_args, include_directories: include_directories('src'), dependencies: dependencies, install: true, install_dir: get_option('libdir') / get_option('plugindir'), install_rpath: default_install_rpath) -dep_ice = declare_dependency(link_with: lib_ice, include_directories: include_directories('.')) -summary('Peer-to-peer communication (ice)', dep_ice.found(), bool_yn: true, section: 'Plugins') diff --git a/plugins/ice/src/module.vala b/plugins/ice/src/module.vala deleted file mode 100644 index c3c92124..00000000 --- a/plugins/ice/src/module.vala +++ /dev/null @@ -1,57 +0,0 @@ -using Gee; - -using Dino.Crypto; -using Xmpp; -using Xmpp.Xep; - -public class Dino.Plugins.Ice.Module : JingleIceUdp.Module { - - public string? stun_ip = null; - public uint stun_port = 0; - public string? turn_ip = null; - public Xep.ExternalServiceDiscovery.Service? turn_service = null; - - private weak Nice.Agent? agent; - private HashMap cerds = new HashMap(); - - private Nice.Agent get_agent() { - Nice.Agent? agent = this.agent; - if (agent == null) { - agent = new Nice.Agent(MainContext.@default(), Nice.Compatibility.RFC5245); - if (stun_ip != null) { - agent.stun_server = stun_ip; - agent.stun_server_port = stun_port; - } - agent.ice_tcp = false; - agent.set_software("Dino"); - agent.weak_ref(agent_unweak); - this.agent = agent; - debug("STUN server for libnice %s %u", agent.stun_server, agent.stun_server_port); - } - return agent; - } - - public override Jingle.TransportParameters create_transport_parameters(XmppStream stream, uint8 components, Jid local_full_jid, Jid peer_full_jid) { - DtlsSrtp.CredentialsCapsule? cred = get_create_credentials(local_full_jid, peer_full_jid); - return new TransportParameters(get_agent(), cred, turn_service, turn_ip, components, local_full_jid, peer_full_jid); - } - - public override Jingle.TransportParameters parse_transport_parameters(XmppStream stream, uint8 components, Jid local_full_jid, Jid peer_full_jid, StanzaNode transport) throws Jingle.IqError { - DtlsSrtp.CredentialsCapsule? cred = get_create_credentials(local_full_jid, peer_full_jid); - return new TransportParameters(get_agent(), cred, turn_service, turn_ip, components, local_full_jid, peer_full_jid, transport); - } - - private DtlsSrtp.CredentialsCapsule? get_create_credentials(Jid local_full_jid, Jid peer_full_jid) { - string from_to_id = local_full_jid.to_string() + peer_full_jid.to_string(); - try { - if (!cerds.has_key(from_to_id)) cerds[from_to_id] = DtlsSrtp.generate_credentials(); - } catch (GLib.Error e) { - warning("Error creating dtls credentials: %s", e.message); - } - return cerds[from_to_id]; - } - - private void agent_unweak() { - this.agent = null; - } -} \ No newline at end of file diff --git a/plugins/ice/src/plugin.vala b/plugins/ice/src/plugin.vala deleted file mode 100644 index d1655a74..00000000 --- a/plugins/ice/src/plugin.vala +++ /dev/null @@ -1,104 +0,0 @@ -using Gee; -using Dino.Entities; -using Xmpp; -using Xmpp.Xep; - -private extern const size_t NICE_ADDRESS_STRING_LEN; - -public class Dino.Plugins.Ice.Plugin : RootInterface, Object { - private HashMap timeouts = new HashMap(Account.hash_func, Account.equals_func); - - public Dino.Application app; - - public void registered(Dino.Application app) { - Nice.debug_enable(true); - this.app = app; - app.stream_interactor.module_manager.initialize_account_modules.connect((account, list) => { - list.add(new Module()); - }); - app.stream_interactor.stream_attached_modules.connect((account, stream) => { - if (stream.get_module(Socks5Bytestreams.Module.IDENTITY) != null) { - stream.get_module(Socks5Bytestreams.Module.IDENTITY).set_local_ip_address_handler(get_local_ip_addresses); - } - if (stream.get_module(JingleRawUdp.Module.IDENTITY) != null) { - stream.get_module(JingleRawUdp.Module.IDENTITY).set_local_ip_address_handler(get_local_ip_addresses); - } - }); - app.stream_interactor.stream_negotiated.connect(external_discovery_refresh_services); - } - - private async void external_discovery_refresh_services(Account account, XmppStream stream) { - Module? ice_udp_module = stream.get_module(JingleIceUdp.Module.IDENTITY) as Module; - if (ice_udp_module == null) return; - - Gee.List services = yield ExternalServiceDiscovery.request_services(stream); - foreach (Xep.ExternalServiceDiscovery.Service service in services) { - if (service.transport == "udp" && (service.ty == "stun" || service.ty == "turn")) { - InetAddress? ip = yield lookup_ipv4_addess(service.host); - if (ip == null) continue; - - if (ip.is_any || ip.is_link_local || ip.is_loopback || ip.is_multicast || ip.is_site_local) { - warning("Ignoring STUN/TURN server at %s", service.host); - continue; - } - - if (service.ty == "stun") { - debug("Server offers STUN server: %s:%u, resolved to %s", service.host, service.port, ip.to_string()); - ice_udp_module.stun_ip = ip.to_string(); - ice_udp_module.stun_port = service.port; - } else if (service.ty == "turn") { - debug("Server offers TURN server: %s:%u, resolved to %s", service.host, service.port, ip.to_string()); - ice_udp_module.turn_ip = ip.to_string(); - ice_udp_module.turn_service = service; - } - } - } - if (ice_udp_module.stun_ip == null) { - InetAddress ip = yield lookup_ipv4_addess("stun.dino.im"); - if (ip == null) return; - - debug("Using fallback STUN server: stun.dino.im:7886, resolved to %s", ip.to_string()); - - ice_udp_module.stun_ip = ip.to_string(); - ice_udp_module.stun_port = 7886; - } - - // Refresh TURN credentials before they expire - if (ice_udp_module.turn_service != null) { - DateTime? expires = ice_udp_module.turn_service.expires; - if (expires != null) { - uint refresh_delay = (uint) (expires.to_unix() - new DateTime.now_utc().to_unix()) / 2; - - if (refresh_delay >= 300 && refresh_delay <= (int64) uint.MAX) { // 5 min < refetch < max - debug("Re-fetching TURN credentials in %u sec", refresh_delay); - - timeouts.unset(account); - timeouts[account] = Timeout.add_seconds((uint) refresh_delay, () => { - external_discovery_refresh_services.begin(account, stream); - return false; - }); - } else { - warning("TURN credentials' expiry time = %u, *not* re-fetching", refresh_delay); - } - } - } - } - - public void shutdown() { - // Nothing to do - } - - private async InetAddress? lookup_ipv4_addess(string host) { - try { - Resolver resolver = Resolver.get_default(); - GLib.List? ips = yield resolver.lookup_by_name_async(host); - foreach (GLib.InetAddress ina in ips) { - if (ina.get_family() != SocketFamily.IPV4) continue; - return ina; - } - } catch (Error e) { - warning("Failed looking up IP address of %s", host); - } - return null; - } -} \ No newline at end of file diff --git a/plugins/ice/src/register_plugin.vala b/plugins/ice/src/register_plugin.vala deleted file mode 100644 index b2ed56c1..00000000 --- a/plugins/ice/src/register_plugin.vala +++ /dev/null @@ -1,3 +0,0 @@ -public Type register_plugin(Module module) { - return typeof (Dino.Plugins.Ice.Plugin); -} diff --git a/plugins/ice/src/transport_parameters.vala b/plugins/ice/src/transport_parameters.vala deleted file mode 100644 index 122207c5..00000000 --- a/plugins/ice/src/transport_parameters.vala +++ /dev/null @@ -1,391 +0,0 @@ -using Gee; - -using Dino.Crypto; -using Xmpp; -using Xmpp.Xep; - - -public class Dino.Plugins.Ice.TransportParameters : JingleIceUdp.IceUdpTransportParameters { - private Nice.Agent agent; - private uint stream_id; - private bool we_want_connection; - private bool remote_credentials_set; - private Map connections = new HashMap(); - private DtlsSrtp.Handler? dtls_srtp_handler; - private MainContext thread_context; - private MainLoop thread_loop; - - private class DatagramConnection : Jingle.DatagramConnection { - private Nice.Agent agent; - private DtlsSrtp.Handler? dtls_srtp_handler; - private uint stream_id; - private string? error; - private ulong datagram_received_id; - - public DatagramConnection(Nice.Agent agent, DtlsSrtp.Handler? dtls_srtp_handler, uint stream_id, uint8 component_id) { - this.agent = agent; - this.dtls_srtp_handler = dtls_srtp_handler; - this.stream_id = stream_id; - this.component_id = component_id; - this.datagram_received_id = this.datagram_received.connect((datagram) => { - bytes_received += datagram.length; - }); - } - - public override async void terminate(bool we_terminated, string? reason_string = null, string? reason_text = null) { - yield base.terminate(we_terminated, reason_string, reason_text); - this.disconnect(datagram_received_id); - agent = null; - dtls_srtp_handler = null; - } - - public override void send_datagram(Bytes datagram) { - if (this.agent != null && is_component_ready(agent, stream_id, component_id)) { - try { - if (dtls_srtp_handler != null) { - uint8[] encrypted_data = dtls_srtp_handler.process_outgoing_data(component_id, datagram.get_data()); - if (encrypted_data == null) return; - GLib.OutputVector vector = { encrypted_data, encrypted_data.length }; - GLib.OutputVector[] vectors = { vector }; - Nice.OutputMessage message = { vectors }; - Nice.OutputMessage[] messages = { message }; - agent.send_messages_nonblocking(stream_id, component_id, messages); - } else { - GLib.OutputVector vector = { datagram.get_data(), datagram.get_size() }; - GLib.OutputVector[] vectors = { vector }; - Nice.OutputMessage message = { vectors }; - Nice.OutputMessage[] messages = { message }; - agent.send_messages_nonblocking(stream_id, component_id, messages); - } - bytes_sent += datagram.length; - } catch (GLib.Error e) { - warning("%s while send_datagram stream %u component %u", e.message, stream_id, component_id); - } - } - } - } - - public TransportParameters(Nice.Agent agent, DtlsSrtp.CredentialsCapsule? credentials, Xep.ExternalServiceDiscovery.Service? turn_service, string? turn_ip, uint8 components, Jid local_full_jid, Jid peer_full_jid, StanzaNode? node = null) { - base(components, local_full_jid, peer_full_jid, node); - this.we_want_connection = (node == null); - this.agent = agent; - - if (this.peer_fingerprint != null || !incoming) { - dtls_srtp_handler = setup_dtls(this, credentials); - own_fingerprint = dtls_srtp_handler.own_fingerprint; - if (incoming) { - own_setup = "active"; - dtls_srtp_handler.mode = DtlsSrtp.Mode.CLIENT; - dtls_srtp_handler.peer_fingerprint = peer_fingerprint; - dtls_srtp_handler.peer_fp_algo = peer_fp_algo; - } else { - own_setup = "actpass"; - dtls_srtp_handler.mode = DtlsSrtp.Mode.SERVER; - dtls_srtp_handler.setup_dtls_connection.begin((_, res) => { - var content_encryption = dtls_srtp_handler.setup_dtls_connection.end(res); - if (content_encryption != null) { - this.content.encryptions[content_encryption.encryption_ns] = content_encryption; - } - }); - } - } - - agent.candidate_gathering_done.connect(on_candidate_gathering_done); - agent.initial_binding_request_received.connect(on_initial_binding_request_received); - agent.component_state_changed.connect(on_component_state_changed); - agent.new_selected_pair_full.connect(on_new_selected_pair_full); - agent.new_candidate_full.connect(on_new_candidate); - - agent.controlling_mode = !incoming; - stream_id = agent.add_stream(components); - thread_context = new MainContext(); - new Thread(@"ice-thread-$stream_id", () => { - thread_context.push_thread_default(); - thread_loop = new MainLoop(thread_context, false); - thread_loop.run(); - thread_context.pop_thread_default(); - return null; - }); - - if (turn_ip != null) { - for (uint8 component_id = 1; component_id <= components; component_id++) { - agent.set_relay_info(stream_id, component_id, turn_ip, turn_service.port, turn_service.username, turn_service.password, Nice.RelayType.UDP); - debug("TURN info (component %i) %s:%u", component_id, turn_ip, turn_service.port); - } - } - string ufrag; - string pwd; - agent.get_local_credentials(stream_id, out ufrag, out pwd); - init(ufrag, pwd); - - for (uint8 component_id = 1; component_id <= components; component_id++) { - // We don't properly get local candidates before this call - agent.attach_recv(stream_id, component_id, thread_context, on_recv); - } - - agent.gather_candidates(stream_id); - } - - private static DtlsSrtp.Handler setup_dtls(TransportParameters tp, DtlsSrtp.CredentialsCapsule credentials) { - var weak_self = WeakRef(tp); - DtlsSrtp.Handler dtls_srtp = new DtlsSrtp.Handler.with_cert(credentials); - dtls_srtp.send_data.connect((data) => { - TransportParameters self = (TransportParameters) weak_self.get(); - if (self != null) self.agent.send(self.stream_id, 1, data); - }); - return dtls_srtp; - } - - private void on_candidate_gathering_done(uint stream_id) { - if (stream_id != this.stream_id) return; - debug("on_candidate_gathering_done in %u", stream_id); - - for (uint8 i = 1; i <= components; i++) { - foreach (unowned Nice.Candidate nc in agent.get_local_candidates(stream_id, i)) { - if (nc.transport == Nice.CandidateTransport.UDP) { - JingleIceUdp.Candidate? candidate = candidate_to_jingle(nc); - if (candidate == null) continue; - debug("Local candidate summary: %s", agent.generate_local_candidate_sdp(nc)); - } - } - } - } - - private void on_new_candidate(Nice.Candidate nc) { - if (nc.stream_id != stream_id) return; - JingleIceUdp.Candidate? candidate = candidate_to_jingle(nc); - if (candidate == null) return; - - if (nc.transport == Nice.CandidateTransport.UDP) { - // Execution was in the agent thread before - add_local_candidate_threadsafe(candidate); - } - } - - private bool bytes_equal(uint8[] a1, uint8[] a2) { - return a1.length == a2.length && Memory.cmp(a1, a2, a1.length) == 0; - } - - public override void handle_transport_accept(StanzaNode transport) throws Jingle.IqError { - debug("on_transport_accept from %s", peer_full_jid.to_string()); - base.handle_transport_accept(transport); - - if (dtls_srtp_handler != null && peer_fingerprint != null) { - if (dtls_srtp_handler.peer_fingerprint != null) { - if (!bytes_equal(dtls_srtp_handler.peer_fingerprint, peer_fingerprint)) { - warning("Tried to replace certificate fingerprint mid use. We don't allow that."); - peer_fingerprint = dtls_srtp_handler.peer_fingerprint; - peer_fp_algo = dtls_srtp_handler.peer_fp_algo; - } - } else { - dtls_srtp_handler.peer_fingerprint = peer_fingerprint; - dtls_srtp_handler.peer_fp_algo = peer_fp_algo; - } - if (peer_setup == "passive") { - dtls_srtp_handler.mode = DtlsSrtp.Mode.CLIENT; - dtls_srtp_handler.stop_dtls_connection(); - dtls_srtp_handler.setup_dtls_connection.begin((_, res) => { - var content_encryption = dtls_srtp_handler.setup_dtls_connection.end(res); - if (content_encryption != null) { - this.content.encryptions[content_encryption.encryption_ns] = content_encryption; - } - }); - } - } else { - dtls_srtp_handler = null; - } - } - - public override void handle_transport_info(StanzaNode transport) throws Jingle.IqError { - debug("on_transport_info from %s", peer_full_jid.to_string()); - base.handle_transport_info(transport); - - if (dtls_srtp_handler != null && peer_fingerprint != null) { - if (dtls_srtp_handler.peer_fingerprint != null) { - if (!bytes_equal(dtls_srtp_handler.peer_fingerprint, peer_fingerprint)) { - warning("Tried to replace certificate fingerprint mid use. We don't allow that."); - peer_fingerprint = dtls_srtp_handler.peer_fingerprint; - peer_fp_algo = dtls_srtp_handler.peer_fp_algo; - } - } else { - dtls_srtp_handler.peer_fingerprint = peer_fingerprint; - dtls_srtp_handler.peer_fp_algo = peer_fp_algo; - } - } - - if (!we_want_connection) return; - - if (remote_ufrag != null && remote_pwd != null && !remote_credentials_set) { - agent.set_remote_credentials(stream_id, remote_ufrag, remote_pwd); - remote_credentials_set = true; - } - for (uint8 i = 1; i <= components; i++) { - SList candidates = new SList(); - foreach (JingleIceUdp.Candidate candidate in remote_candidates) { - if (candidate.component == i) { - candidates.append(candidate_to_nice(candidate)); - } - } - int new_candidates = agent.set_remote_candidates(stream_id, i, candidates); - debug("Updated to %i remote candidates for candidate %u via transport info", new_candidates, i); - } - } - - public override void create_transport_connection(XmppStream stream, Jingle.Content content) { - debug("create_transport_connection: %s", content.session.sid); - debug("local_credentials: %s %s", local_ufrag, local_pwd); - debug("remote_credentials: %s %s", remote_ufrag, remote_pwd); - debug("expected incoming credentials: %s %s", local_ufrag + ":" + remote_ufrag, local_pwd); - debug("expected outgoing credentials: %s %s", remote_ufrag + ":" + local_ufrag, remote_pwd); - - we_want_connection = true; - - if (remote_ufrag != null && remote_pwd != null && !remote_credentials_set) { - agent.set_remote_credentials(stream_id, remote_ufrag, remote_pwd); - remote_credentials_set = true; - } - for (uint8 i = 1; i <= components; i++) { - SList candidates = new SList(); - foreach (JingleIceUdp.Candidate candidate in remote_candidates) { - if (candidate.ip.has_prefix("fe80::")) continue; - if (candidate.component == i) { - candidates.append(candidate_to_nice(candidate)); - debug("remote candidate: %s", agent.generate_local_candidate_sdp(candidate_to_nice(candidate))); - } - } - int new_candidates = agent.set_remote_candidates(stream_id, i, candidates); - debug("Initiated component %u with %i remote candidates", i, new_candidates); - - connections[i] = new DatagramConnection(agent, dtls_srtp_handler, stream_id, i); - content.set_transport_connection(connections[i], i); - } - - base.create_transport_connection(stream, content); - } - - private void on_component_state_changed(uint stream_id, uint component_id, uint state) { - if (stream_id != this.stream_id) return; - debug("stream %u component %u state changed to %s", stream_id, component_id, agent.get_component_state(stream_id, component_id).to_string()); - may_consider_ready(stream_id, component_id); - if (incoming && dtls_srtp_handler != null && !dtls_srtp_handler.ready && is_component_ready(agent, stream_id, component_id) && dtls_srtp_handler.mode == DtlsSrtp.Mode.CLIENT) { - dtls_srtp_handler.setup_dtls_connection.begin((_, res) => { - Jingle.ContentEncryption? encryption = dtls_srtp_handler.setup_dtls_connection.end(res); - if (encryption != null) { - this.content.encryptions[encryption.encryption_ns] = encryption; - } - }); - } - } - - private void may_consider_ready(uint stream_id, uint component_id) { - if (stream_id != this.stream_id) return; - if (connections.has_key((uint8) component_id) && !connections[(uint8)component_id].ready && is_component_ready(agent, stream_id, component_id) && (dtls_srtp_handler == null || dtls_srtp_handler.ready)) { - connections[(uint8)component_id].ready = true; - } - } - - private void on_initial_binding_request_received(uint stream_id) { - if (stream_id != this.stream_id) return; - debug("initial_binding_request_received"); - } - - private void on_new_selected_pair_full(uint stream_id, uint component_id, Nice.Candidate p1, Nice.Candidate p2) { - if (stream_id != this.stream_id) return; - debug("new_selected_pair_full %u [%s, %s]", component_id, agent.generate_local_candidate_sdp(p1), agent.generate_local_candidate_sdp(p2)); - } - - private void on_recv(Nice.Agent agent, uint stream_id, uint component_id, uint8[] data) { - if (stream_id != this.stream_id) return; - uint8[] decrypt_data = null; - if (dtls_srtp_handler != null) { - try { - decrypt_data = dtls_srtp_handler.process_incoming_data(component_id, data); - if (decrypt_data == null) return; - } catch (Crypto.Error e) { - warning("%s while on_recv stream %u component %u", e.message, stream_id, component_id); - return; - } - } - may_consider_ready(stream_id, component_id); - if (connections.has_key((uint8) component_id)) { - if (!connections[(uint8) component_id].ready) { - debug("on_recv stream %u component %u when state %s", stream_id, component_id, agent.get_component_state(stream_id, component_id).to_string()); - } - connections[(uint8) component_id].datagram_received(new Bytes(decrypt_data ?? data)); - } else { - debug("on_recv stream %u component %u length %u", stream_id, component_id, data.length); - } - } - - private static Nice.Candidate candidate_to_nice(JingleIceUdp.Candidate c) { - Nice.CandidateType type; - switch (c.type_) { - case JingleIceUdp.Candidate.Type.HOST: type = Nice.CandidateType.HOST; break; - case JingleIceUdp.Candidate.Type.PRFLX: type = Nice.CandidateType.PEER_REFLEXIVE; break; - case JingleIceUdp.Candidate.Type.RELAY: type = Nice.CandidateType.RELAYED; break; - case JingleIceUdp.Candidate.Type.SRFLX: type = Nice.CandidateType.SERVER_REFLEXIVE; break; - default: assert_not_reached(); - } - - Nice.Candidate candidate = new Nice.Candidate(type); - candidate.component_id = c.component; - char[] foundation = new char[Nice.CANDIDATE_MAX_FOUNDATION]; - Memory.copy(foundation, c.foundation.data, size_t.min(c.foundation.length, Nice.CANDIDATE_MAX_FOUNDATION - 1)); - candidate.foundation = foundation; - candidate.addr = Nice.Address(); - candidate.addr.init(); - candidate.addr.set_from_string(c.ip); - candidate.addr.set_port(c.port); - candidate.priority = c.priority; - if (c.rel_addr != null) { - candidate.base_addr = Nice.Address(); - candidate.base_addr.init(); - candidate.base_addr.set_from_string(c.rel_addr); - candidate.base_addr.set_port(c.rel_port); - } - candidate.transport = Nice.CandidateTransport.UDP; - return candidate; - } - - private static JingleIceUdp.Candidate? candidate_to_jingle(Nice.Candidate nc) { - JingleIceUdp.Candidate candidate = new JingleIceUdp.Candidate(); - switch (nc.type) { - case Nice.CandidateType.HOST: candidate.type_ = JingleIceUdp.Candidate.Type.HOST; break; - case Nice.CandidateType.PEER_REFLEXIVE: candidate.type_ = JingleIceUdp.Candidate.Type.PRFLX; break; - case Nice.CandidateType.RELAYED: candidate.type_ = JingleIceUdp.Candidate.Type.RELAY; break; - case Nice.CandidateType.SERVER_REFLEXIVE: candidate.type_ = JingleIceUdp.Candidate.Type.SRFLX; break; - default: assert_not_reached(); - } - candidate.component = (uint8) nc.component_id; - candidate.foundation = ((string)nc.foundation).dup(); - candidate.generation = 0; - candidate.id = Random.next_int().to_string("%08x"); // TODO - - char[] res = new char[NICE_ADDRESS_STRING_LEN]; - nc.addr.to_string(res); - candidate.ip = (string) res; - candidate.network = 0; // TODO - candidate.port = (uint16) nc.addr.get_port(); - candidate.priority = nc.priority; - candidate.protocol = "udp"; - if (nc.base_addr.is_valid() && !nc.base_addr.equal(nc.addr)) { - res = new char[NICE_ADDRESS_STRING_LEN]; - nc.base_addr.to_string(res); - candidate.rel_addr = (string) res; - candidate.rel_port = (uint16) nc.base_addr.get_port(); - } - if (candidate.ip.has_prefix("fe80::")) return null; - - return candidate; - } - - public override void dispose() { - base.dispose(); - agent = null; - dtls_srtp_handler = null; - connections.clear(); - if (thread_loop != null) { - thread_loop.quit(); - } - } -} diff --git a/plugins/ice/src/util.vala b/plugins/ice/src/util.vala deleted file mode 100644 index dd89d2f4..00000000 --- a/plugins/ice/src/util.vala +++ /dev/null @@ -1,18 +0,0 @@ -using Gee; - -namespace Dino.Plugins.Ice { - -internal static bool is_component_ready(Nice.Agent agent, uint stream_id, uint component_id) { - var state = agent.get_component_state(stream_id, component_id); - return state == Nice.ComponentState.CONNECTED || state == Nice.ComponentState.READY; -} - -internal Gee.List get_local_ip_addresses() { - Gee.List result = new ArrayList(); - foreach (string ip_address in Nice.interfaces_get_local_ips(false)) { - result.add(ip_address); - } - return result; -} - -} \ No newline at end of file diff --git a/plugins/ice/vapi/metadata/Nice-0.1.metadata b/plugins/ice/vapi/metadata/Nice-0.1.metadata deleted file mode 100644 index 7fcf046a..00000000 --- a/plugins/ice/vapi/metadata/Nice-0.1.metadata +++ /dev/null @@ -1,11 +0,0 @@ -Nice cheader_filename="nice.h" -Address.to_string.dst type="char[]" -Agent.new_reliable#constructor name="create_reliable" -Agent.attach_recv skip=false -Agent.send.buf type="uint8[]" array_length_idx=2 -AgentRecvFunc.buf type="uint8[]" array_length_idx=3 -PseudoTcpCallbacks#record skip -PseudoTcpSocket#class skip - -# Not yet supported by vapigen -# Candidate copy_function="nice_candidate_copy" free_function="nice_candidate_free" type_id="" diff --git a/plugins/ice/vapi/nice.vapi b/plugins/ice/vapi/nice.vapi deleted file mode 100644 index 540e2b4e..00000000 --- a/plugins/ice/vapi/nice.vapi +++ /dev/null @@ -1,386 +0,0 @@ -/* nice.vapi generated by vapigen, do not modify. */ - -[CCode (cprefix = "Nice", gir_namespace = "Nice", gir_version = "0.1", lower_case_cprefix = "nice_")] -namespace Nice { - [CCode (cheader_filename = "nice.h", type_id = "nice_agent_get_type ()")] - public class Agent : GLib.Object { - [CCode (has_construct_function = false)] - public Agent (GLib.MainContext ctx, Nice.Compatibility compat); - public bool add_local_address (Nice.Address addr); - public uint add_stream (uint n_components); - public bool attach_recv (uint stream_id, uint component_id, GLib.MainContext ctx, Nice.AgentRecvFunc func); - [Version (since = "0.1.16")] - public async void close_async (); - [CCode (cname = "nice_agent_new_reliable", has_construct_function = false)] - [Version (since = "0.0.11")] - public Agent.create_reliable (GLib.MainContext ctx, Nice.Compatibility compat); - [Version (since = "0.1.6")] - public bool forget_relays (uint stream_id, uint component_id); - [CCode (has_construct_function = false)] - [Version (since = "0.1.15")] - public Agent.full (GLib.MainContext ctx, Nice.Compatibility compat, Nice.AgentOption flags); - public bool gather_candidates (uint stream_id); - [Version (since = "0.1.4")] - public string generate_local_candidate_sdp (Nice.Candidate candidate); - [Version (since = "0.1.4")] - public string generate_local_sdp (); - [Version (since = "0.1.4")] - public string generate_local_stream_sdp (uint stream_id, bool include_non_ice); - [Version (since = "0.1.8")] - public Nice.ComponentState get_component_state (uint stream_id, uint component_id); - public Nice.Candidate get_default_local_candidate (uint stream_id, uint component_id); - [Version (since = "0.1.5")] - public GLib.IOStream get_io_stream (uint stream_id, uint component_id); - public GLib.SList get_local_candidates (uint stream_id, uint component_id); - public bool get_local_credentials (uint stream_id, out string ufrag, out string pwd); - public GLib.SList get_remote_candidates (uint stream_id, uint component_id); - public bool get_selected_pair (uint stream_id, uint component_id, Nice.Candidate local, Nice.Candidate remote); - [Version (since = "0.1.5")] - public GLib.Socket? get_selected_socket (uint stream_id, uint component_id); - [Version (since = "0.1.4")] - public unowned string get_stream_name (uint stream_id); - [Version (since = "0.1.4")] - public Nice.Candidate parse_remote_candidate_sdp (uint stream_id, string sdp); - [Version (since = "0.1.4")] - public int parse_remote_sdp (string sdp); - [Version (since = "0.1.4")] - public GLib.SList parse_remote_stream_sdp (uint stream_id, string sdp, string ufrag, string pwd); - [Version (since = "0.1.16")] - public bool peer_candidate_gathering_done (uint stream_id); - [Version (since = "0.1.5")] - public ssize_t recv (uint stream_id, uint component_id, [CCode (array_length_cname = "buf_len", array_length_pos = 3.5, array_length_type = "gsize")] out unowned uint8[] buf, GLib.Cancellable? cancellable = null) throws GLib.Error; - [Version (since = "0.1.5")] - public int recv_messages (uint stream_id, uint component_id, [CCode (array_length_cname = "n_messages", array_length_pos = 3.5, array_length_type = "guint")] out unowned Nice.InputMessage[] messages, GLib.Cancellable? cancellable = null) throws GLib.Error; - [Version (since = "0.1.5")] - public int recv_messages_nonblocking (uint stream_id, uint component_id, [CCode (array_length_cname = "n_messages", array_length_pos = 3.5, array_length_type = "guint")] out unowned Nice.InputMessage[] messages, GLib.Cancellable? cancellable = null) throws GLib.Error; - [Version (since = "0.1.5")] - public ssize_t recv_nonblocking (uint stream_id, uint component_id, [CCode (array_length_cname = "buf_len", array_length_pos = 3.5, array_length_type = "gsize")] out unowned uint8[] buf, GLib.Cancellable? cancellable = null) throws GLib.Error; - public void remove_stream (uint stream_id); - public bool restart (); - [Version (since = "0.1.6")] - public bool restart_stream (uint stream_id); - public int send (uint stream_id, uint component_id, [CCode (array_length_cname = "len", array_length_pos = 2.5, array_length_type = "guint", type = "const gchar*")] uint8[] buf); - [Version (since = "0.1.5")] - public int send_messages_nonblocking (uint stream_id, uint component_id, [CCode (array_length_cname = "n_messages", array_length_pos = 3.5, array_length_type = "guint")] Nice.OutputMessage[] messages, GLib.Cancellable? cancellable = null) throws GLib.Error; - public bool set_local_credentials (uint stream_id, string ufrag, string pwd); - public void set_port_range (uint stream_id, uint component_id, uint min_port, uint max_port); - public bool set_relay_info (uint stream_id, uint component_id, string server_ip, uint server_port, string username, string password, Nice.RelayType type); - public int set_remote_candidates (uint stream_id, uint component_id, GLib.SList candidates); - public bool set_remote_credentials (uint stream_id, string ufrag, string pwd); - public bool set_selected_pair (uint stream_id, uint component_id, string lfoundation, string rfoundation); - public bool set_selected_remote_candidate (uint stream_id, uint component_id, Nice.Candidate candidate); - [Version (since = "0.0.10")] - public void set_software (string software); - [Version (since = "0.1.4")] - public bool set_stream_name (uint stream_id, string name); - [Version (since = "0.0.9")] - public void set_stream_tos (uint stream_id, int tos); - [NoAccessorMethod] - [Version (since = "0.1.8")] - public bool bytestream_tcp { get; } - [NoAccessorMethod] - public uint compatibility { get; construct; } - [NoAccessorMethod] - public bool controlling_mode { get; set; } - [NoAccessorMethod] - [Version (since = "0.1.14")] - public bool force_relay { get; set; } - [NoAccessorMethod] - public bool full_mode { get; construct; } - [NoAccessorMethod] - [Version (since = "0.1.8")] - public bool ice_tcp { get; set; } - [NoAccessorMethod] - [Version (since = "0.1.16")] - public bool ice_trickle { get; set; } - [NoAccessorMethod] - [Version (since = "0.1.8")] - public bool ice_udp { get; set; } - [NoAccessorMethod] - [Version (since = "0.1.8")] - public bool keepalive_conncheck { get; set; } - [NoAccessorMethod] - public void* main_context { get; construct; } - [NoAccessorMethod] - public uint max_connectivity_checks { get; set; } - [NoAccessorMethod] - [Version (since = "0.0.4")] - public string proxy_ip { owned get; set; } - [NoAccessorMethod] - [Version (since = "0.0.4")] - public string proxy_password { owned get; set; } - [NoAccessorMethod] - [Version (since = "0.0.4")] - public uint proxy_port { get; set; } - [NoAccessorMethod] - [Version (since = "0.0.4")] - public uint proxy_type { get; set; } - [NoAccessorMethod] - [Version (since = "0.0.4")] - public string proxy_username { owned get; set; } - [NoAccessorMethod] - [Version (since = "0.0.11")] - public bool reliable { get; construct; } - [NoAccessorMethod] - [Version (since = "0.1.15")] - public uint stun_initial_timeout { get; set construct; } - [NoAccessorMethod] - [Version (since = "0.1.15")] - public uint stun_max_retransmissions { get; set construct; } - [NoAccessorMethod] - public uint stun_pacing_timer { get; set construct; } - [NoAccessorMethod] - [Version (since = "0.1.15")] - public uint stun_reliable_timeout { get; set construct; } - [NoAccessorMethod] - public string stun_server { owned get; set; } - [NoAccessorMethod] - public uint stun_server_port { get; set; } - [NoAccessorMethod] - public bool support_renomination { get; set; } - [NoAccessorMethod] - [Version (since = "0.0.7")] - public bool upnp { get; set construct; } - [NoAccessorMethod] - [Version (since = "0.0.7")] - public uint upnp_timeout { get; set construct; } - public signal void candidate_gathering_done (uint stream_id); - public signal void component_state_changed (uint stream_id, uint component_id, uint state); - public signal void initial_binding_request_received (uint stream_id); - [Version (deprecated = true, deprecated_since = "0.1.8")] - public signal void new_candidate (uint stream_id, uint component_id, string foundation); - [Version (since = "0.1.8")] - public signal void new_candidate_full (Nice.Candidate candidate); - [Version (deprecated = true, deprecated_since = "0.1.8")] - public signal void new_remote_candidate (uint stream_id, uint component_id, string foundation); - [Version (since = "0.1.8")] - public signal void new_remote_candidate_full (Nice.Candidate candidate); - [Version (deprecated = true, deprecated_since = "0.1.8")] - public signal void new_selected_pair (uint stream_id, uint component_id, string lfoundation, string rfoundation); - [Version (since = "0.1.8")] - public signal void new_selected_pair_full (uint stream_id, uint component_id, Nice.Candidate lcandidate, Nice.Candidate rcandidate); - [Version (since = "0.0.11")] - public signal void reliable_transport_writable (uint stream_id, uint component_id); - [Version (since = "0.1.5")] - public signal void streams_removed ([CCode (array_length = false, array_null_terminated = true)] uint[] stream_ids); - } - [CCode (cheader_filename = "nice.h", copy_function = "nice_candidate_copy", free_function = "nice_candidate_free")] - [Compact] - public class Candidate { - public Nice.Address addr; - public Nice.Address base_addr; - public uint component_id; - [CCode (array_length = false)] - public weak char foundation[33]; - public weak string password; - public uint32 priority; - public void* sockptr; - public uint stream_id; - public Nice.CandidateTransport transport; - public Nice.TurnServer turn; - public Nice.CandidateType type; - public weak string username; - [CCode (has_construct_function = false)] - public Candidate (Nice.CandidateType type); - public Nice.Candidate copy (); - [Version (since = "0.1.15")] - public bool equal_target (Nice.Candidate candidate2); - public void free (); - } - [CCode (cheader_filename = "nice.h", has_type_id = false)] - public struct Address { - [CCode (cname = "s.addr")] - public void* s_addr; - [CCode (cname = "s.ip4")] - public void* s_ip4; - [CCode (cname = "s.ip6")] - public void* s_ip6; - public void copy_to_sockaddr (void* sin); - public bool equal (Nice.Address b); - [Version (since = "0.1.8")] - public bool equal_no_port (Nice.Address b); - public void free (); - public uint get_port (); - public void init (); - public int ip_version (); - public bool is_private (); - public bool is_valid (); - public void set_from_sockaddr (void* sin); - public bool set_from_string (string str); - public void set_ipv4 (uint32 addr_ipv4); - public void set_ipv6 (uint8 addr_ipv6); - public void set_port (uint port); - public void to_string ([CCode (array_length = false, type = "gchar*")] char[] dst); - } - [CCode (cheader_filename = "nice.h", has_type_id = false)] - [Version (since = "0.1.5")] - public struct InputMessage { - [CCode (array_length_cname = "n_buffers")] - public weak GLib.InputVector[] buffers; - public int n_buffers; - public Nice.Address from; - public size_t length; - } - [CCode (cheader_filename = "nice.h", has_type_id = false)] - [Version (since = "0.1.5")] - public struct OutputMessage { - [CCode (array_length_cname = "n_buffers")] - public weak GLib.OutputVector[] buffers; - public int n_buffers; - } - [CCode (cheader_filename = "nice.h", cname = "TurnServer", has_type_id = false)] - public struct TurnServer { - public int ref_count; - public Nice.Address server; - public weak string username; - public weak string password; - public Nice.RelayType type; - } - [CCode (cheader_filename = "nice.h", cprefix = "NICE_AGENT_OPTION_", has_type_id = false)] - [Flags] - [Version (since = "0.1.15")] - public enum AgentOption { - REGULAR_NOMINATION, - RELIABLE, - LITE_MODE, - ICE_TRICKLE, - SUPPORT_RENOMINATION - } - [CCode (cheader_filename = "nice.h", cprefix = "NICE_CANDIDATE_TRANSPORT_", has_type_id = false)] - public enum CandidateTransport { - UDP, - TCP_ACTIVE, - TCP_PASSIVE, - TCP_SO - } - [CCode (cheader_filename = "nice.h", cprefix = "NICE_CANDIDATE_TYPE_", has_type_id = false)] - public enum CandidateType { - HOST, - SERVER_REFLEXIVE, - PEER_REFLEXIVE, - RELAYED - } - [CCode (cheader_filename = "nice.h", cprefix = "NICE_COMPATIBILITY_", has_type_id = false)] - public enum Compatibility { - RFC5245, - DRAFT19, - GOOGLE, - MSN, - WLM2009, - OC2007, - OC2007R2, - LAST - } - [CCode (cheader_filename = "nice.h", cprefix = "NICE_COMPONENT_STATE_", has_type_id = false)] - public enum ComponentState { - DISCONNECTED, - GATHERING, - CONNECTING, - CONNECTED, - READY, - FAILED, - LAST; - [Version (since = "0.1.6")] - public unowned string to_string (); - } - [CCode (cheader_filename = "nice.h", cprefix = "NICE_COMPONENT_TYPE_", has_type_id = false)] - public enum ComponentType { - RTP, - RTCP - } - [CCode (cheader_filename = "nice.h", cprefix = "NICE_NOMINATION_MODE_", has_type_id = false)] - [Version (since = "0.1.15")] - public enum NominationMode { - REGULAR, - AGGRESSIVE - } - [CCode (cheader_filename = "nice.h", cprefix = "NICE_PROXY_TYPE_", has_type_id = false)] - [Version (since = "0.0.4")] - public enum ProxyType { - NONE, - SOCKS5, - HTTP, - LAST - } - [CCode (cheader_filename = "nice.h", cname = "PseudoTcpDebugLevel", cprefix = "PSEUDO_TCP_DEBUG_", has_type_id = false)] - [Version (since = "0.0.11")] - public enum PseudoTcpDebugLevel { - NONE, - NORMAL, - VERBOSE - } - [CCode (cheader_filename = "nice.h", cname = "PseudoTcpShutdown", cprefix = "PSEUDO_TCP_SHUTDOWN_", has_type_id = false)] - [Version (since = "0.1.8")] - public enum PseudoTcpShutdown { - RD, - WR, - RDWR - } - [CCode (cheader_filename = "nice.h", cname = "PseudoTcpState", cprefix = "PSEUDO_TCP_", has_type_id = false)] - [Version (since = "0.0.11")] - public enum PseudoTcpState { - LISTEN, - SYN_SENT, - SYN_RECEIVED, - ESTABLISHED, - CLOSED, - FIN_WAIT_1, - FIN_WAIT_2, - CLOSING, - TIME_WAIT, - CLOSE_WAIT, - LAST_ACK - } - [CCode (cheader_filename = "nice.h", cname = "PseudoTcpWriteResult", cprefix = "WR_", has_type_id = false)] - [Version (since = "0.0.11")] - public enum PseudoTcpWriteResult { - SUCCESS, - TOO_LARGE, - FAIL - } - [CCode (cheader_filename = "nice.h", cprefix = "NICE_RELAY_TYPE_TURN_", has_type_id = false)] - public enum RelayType { - UDP, - TCP, - TLS - } - [CCode (cheader_filename = "nice.h", instance_pos = 4.9)] - public delegate void AgentRecvFunc (Nice.Agent agent, uint stream_id, uint component_id, [CCode (array_length_cname = "len", array_length_pos = 3.5, array_length_type = "guint", type = "gchar*")] uint8[] buf); - [CCode (cheader_filename = "nice.h", cname = "NICE_AGENT_MAX_REMOTE_CANDIDATES")] - public const int AGENT_MAX_REMOTE_CANDIDATES; - [CCode (cheader_filename = "nice.h", cname = "NICE_CANDIDATE_DIRECTION_MS_PREF_ACTIVE")] - public const int CANDIDATE_DIRECTION_MS_PREF_ACTIVE; - [CCode (cheader_filename = "nice.h", cname = "NICE_CANDIDATE_DIRECTION_MS_PREF_PASSIVE")] - public const int CANDIDATE_DIRECTION_MS_PREF_PASSIVE; - [CCode (cheader_filename = "nice.h", cname = "NICE_CANDIDATE_MAX_FOUNDATION")] - public const int CANDIDATE_MAX_FOUNDATION; - [CCode (cheader_filename = "nice.h", cname = "NICE_CANDIDATE_TRANSPORT_MS_PREF_TCP")] - public const int CANDIDATE_TRANSPORT_MS_PREF_TCP; - [CCode (cheader_filename = "nice.h", cname = "NICE_CANDIDATE_TRANSPORT_MS_PREF_UDP")] - public const int CANDIDATE_TRANSPORT_MS_PREF_UDP; - [CCode (cheader_filename = "nice.h", cname = "NICE_CANDIDATE_TYPE_PREF_HOST")] - public const int CANDIDATE_TYPE_PREF_HOST; - [CCode (cheader_filename = "nice.h", cname = "NICE_CANDIDATE_TYPE_PREF_NAT_ASSISTED")] - public const int CANDIDATE_TYPE_PREF_NAT_ASSISTED; - [CCode (cheader_filename = "nice.h", cname = "NICE_CANDIDATE_TYPE_PREF_PEER_REFLEXIVE")] - public const int CANDIDATE_TYPE_PREF_PEER_REFLEXIVE; - [CCode (cheader_filename = "nice.h", cname = "NICE_CANDIDATE_TYPE_PREF_RELAYED")] - public const int CANDIDATE_TYPE_PREF_RELAYED; - [CCode (cheader_filename = "nice.h", cname = "NICE_CANDIDATE_TYPE_PREF_RELAYED_UDP")] - public const int CANDIDATE_TYPE_PREF_RELAYED_UDP; - [CCode (cheader_filename = "nice.h", cname = "NICE_CANDIDATE_TYPE_PREF_SERVER_REFLEXIVE")] - public const int CANDIDATE_TYPE_PREF_SERVER_REFLEXIVE; - [CCode (cheader_filename = "nice.h")] - public static void debug_disable (bool with_stun); - [CCode (cheader_filename = "nice.h")] - public static void debug_enable (bool with_stun); - [CCode (cheader_filename = "nice.h")] - public static string? interfaces_get_ip_for_interface (string interface_name); - [CCode (cheader_filename = "nice.h")] - public static GLib.List interfaces_get_local_interfaces (); - [CCode (cheader_filename = "nice.h")] - public static GLib.List interfaces_get_local_ips (bool include_loopback); - [CCode (cheader_filename = "nice.h", cname = "pseudo_tcp_set_debug_level")] - [Version (since = "0.0.11")] - public static void pseudo_tcp_set_debug_level (Nice.PseudoTcpDebugLevel level); -} diff --git a/plugins/meson.build b/plugins/meson.build index 8aa05215..3f804fcb 100644 --- a/plugins/meson.build +++ b/plugins/meson.build @@ -1,4 +1,3 @@ -subdir('ice') subdir('notification-sound') subdir('omemo') subdir('openpgp') diff --git a/xmpp-vala/src/module/xep/0065_socks5_bytestreams.vala b/xmpp-vala/src/module/xep/0065_socks5_bytestreams.vala index 97674f3f..9cfb4afc 100644 --- a/xmpp-vala/src/module/xep/0065_socks5_bytestreams.vala +++ b/xmpp-vala/src/module/xep/0065_socks5_bytestreams.vala @@ -25,6 +25,10 @@ public class Module : XmppStreamModule, Iq.Handler { private GetLocalIpAddresses? get_local_ip_addresses_impl = null; + public Module.with_local_ip_address_handler(owned GetLocalIpAddresses get_local_ip_addresses) { + set_local_ip_address_handler((owned) get_local_ip_addresses); + } + public override void attach(XmppStream stream) { stream.add_flag(new Flag()); query_availability.begin(stream); diff --git a/xmpp-vala/src/module/xep/0177_jingle_raw_udp.vala b/xmpp-vala/src/module/xep/0177_jingle_raw_udp.vala index 200cdfa9..b59c1b06 100644 --- a/xmpp-vala/src/module/xep/0177_jingle_raw_udp.vala +++ b/xmpp-vala/src/module/xep/0177_jingle_raw_udp.vala @@ -13,6 +13,10 @@ namespace Xmpp.Xep.JingleRawUdp { private GetLocalIpAddresses? get_local_ip_addresses_impl = null; + public Module.with_local_ip_address_handler(owned GetLocalIpAddresses get_local_ip_addresses) { + set_local_ip_address_handler((owned) get_local_ip_addresses); + } + public override void attach(XmppStream stream) { stream.get_module(Jingle.Module.IDENTITY).register_transport(this); stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI); -- cgit v1.3