aboutsummaryrefslogtreecommitdiff
path: root/libdino/src
diff options
context:
space:
mode:
authorMarvin W <git@larma.de>2026-08-22 19:03:26 +0200
committerMarvin W <git@larma.de>2026-10-23 16:58:30 +0200
commitb0456e13ed35e6ebf684b3a7f4d1cb4b6ec352cc (patch)
tree5ecd70a986a08c1cf9984bdda97a9cc8c05255af /libdino/src
parent76a91fca2fc7dd5975e8ff0e6695f27c98539dba (diff)
Move crypto-vala inside libdino
Diffstat (limited to 'libdino/src')
-rw-r--r--libdino/src/crypto/cipher.vala19
-rw-r--r--libdino/src/crypto/cipher_converter.vala110
-rw-r--r--libdino/src/crypto/error.vala11
-rw-r--r--libdino/src/crypto/gnu/cipher.vala137
-rw-r--r--libdino/src/crypto/gnu/utils.vala21
-rw-r--r--libdino/src/crypto/openssl/cipher.vala156
-rw-r--r--libdino/src/crypto/openssl/utils.vala19
-rw-r--r--libdino/src/crypto/srtp.vala122
8 files changed, 595 insertions, 0 deletions
diff --git a/libdino/src/crypto/cipher.vala b/libdino/src/crypto/cipher.vala
new file mode 100644
index 00000000..89282144
--- /dev/null
+++ b/libdino/src/crypto/cipher.vala
@@ -0,0 +1,19 @@
+namespace Dino.Crypto {
+
+public interface SymmetricCipher {
+ public abstract void set_key(uint8[] key) throws Error;
+ public abstract void set_iv(uint8[] iv) throws Error;
+ public abstract void reset() throws Error;
+}
+
+public interface SymmetricCipherEncryption : SymmetricCipher, Object {
+ public abstract int encrypt(uint8[] output, uint8[] input) throws Error;
+ public abstract uint8[] get_tag(size_t taglen) throws Error;
+}
+
+public interface SymmetricCipherDecryption : SymmetricCipher, Object {
+ public abstract int decrypt(uint8[] output, uint8[] input) throws Error;
+ public abstract void check_tag(uint8[] tag) throws Error;
+}
+
+}
diff --git a/libdino/src/crypto/cipher_converter.vala b/libdino/src/crypto/cipher_converter.vala
new file mode 100644
index 00000000..c2404c6d
--- /dev/null
+++ b/libdino/src/crypto/cipher_converter.vala
@@ -0,0 +1,110 @@
+using GLib;
+
+namespace Dino.Crypto {
+
+public class SymmetricCipherEncrypter : Converter, Object {
+ private SymmetricCipherEncryption cipher;
+ private size_t attached_taglen;
+
+ public SymmetricCipherEncrypter(owned SymmetricCipherEncryption cipher, size_t attached_taglen = 0) {
+ this.cipher = (owned) cipher;
+ this.attached_taglen = attached_taglen;
+ }
+
+ public void reset() {
+ try {
+ cipher.reset();
+ } catch (Crypto.Error e) {
+ warning(@"$(e.domain) error while resetting cipher: $(e.message)");
+ }
+ }
+
+ public uint8[] get_tag(size_t taglen) throws Error {
+ return ((SymmetricCipherEncryption)cipher).get_tag(taglen);
+ }
+
+ public ConverterResult convert(uint8[] inbuf, uint8[] outbuf, ConverterFlags flags, out size_t bytes_read, out size_t bytes_written) throws IOError {
+ if (inbuf.length > outbuf.length) {
+ throw new IOError.NO_SPACE("CipherConverter needs at least the size of input as output space");
+ }
+ if ((flags & ConverterFlags.INPUT_AT_END) != 0 && inbuf.length + attached_taglen > outbuf.length) {
+ throw new IOError.NO_SPACE("CipherConverter needs additional output space to attach tag");
+ }
+ try {
+ if (inbuf.length > 0) {
+ ((SymmetricCipherEncryption)cipher).encrypt(outbuf, inbuf);
+ }
+ bytes_read = inbuf.length;
+ bytes_written = inbuf.length;
+ if ((flags & ConverterFlags.INPUT_AT_END) != 0) {
+ if (attached_taglen > 0) {
+ Memory.copy((uint8*)outbuf + inbuf.length, get_tag(attached_taglen), attached_taglen);
+ bytes_written = inbuf.length + attached_taglen;
+ }
+ return ConverterResult.FINISHED;
+ }
+ if ((flags & ConverterFlags.FLUSH) != 0) {
+ return ConverterResult.FLUSHED;
+ }
+ return ConverterResult.CONVERTED;
+ } catch (Crypto.Error e) {
+ throw new IOError.FAILED(@"$(e.domain) error while encrypting: $(e.message)");
+ }
+ }
+}
+
+public class SymmetricCipherDecrypter : Converter, Object {
+ private SymmetricCipherDecryption cipher;
+ private size_t attached_taglen;
+
+ public SymmetricCipherDecrypter(owned SymmetricCipherDecryption cipher, size_t attached_taglen = 0) {
+ this.cipher = (owned) cipher;
+ this.attached_taglen = attached_taglen;
+ }
+
+ public void reset() {
+ try {
+ cipher.reset();
+ } catch (Crypto.Error e) {
+ warning(@"$(e.domain) error while resetting cipher: $(e.message)");
+ }
+ }
+
+ public void check_tag(uint8[] tag) throws Error {
+ ((SymmetricCipherDecryption)cipher).check_tag(tag);
+ }
+
+ public ConverterResult convert(uint8[] inbuf, uint8[] outbuf, ConverterFlags flags, out size_t bytes_read, out size_t bytes_written) throws IOError {
+ if (inbuf.length > outbuf.length + attached_taglen) {
+ throw new IOError.NO_SPACE("CipherConverter needs at least the size of input as output space");
+ }
+ if ((flags & ConverterFlags.INPUT_AT_END) != 0 && inbuf.length < attached_taglen) {
+ throw new IOError.PARTIAL_INPUT("CipherConverter needs additional input to read tag");
+ } else if ((flags & ConverterFlags.INPUT_AT_END) == 0 && inbuf.length < attached_taglen + 1) {
+ throw new IOError.PARTIAL_INPUT("CipherConverter needs additional input to make sure to not accidentally read tag");
+ }
+ try {
+ inbuf.length -= (int) attached_taglen;
+ if (inbuf.length > 0) {
+ ((SymmetricCipherDecryption)cipher).decrypt(outbuf, inbuf);
+ }
+ bytes_read = inbuf.length;
+ bytes_written = inbuf.length;
+ inbuf.length += (int) attached_taglen;
+ if ((flags & ConverterFlags.INPUT_AT_END) != 0) {
+ if (attached_taglen > 0) {
+ check_tag(inbuf[(inbuf.length - attached_taglen):inbuf.length]);
+ bytes_read = inbuf.length;
+ }
+ return ConverterResult.FINISHED;
+ }
+ if ((flags & ConverterFlags.FLUSH) != 0) {
+ return ConverterResult.FLUSHED;
+ }
+ return ConverterResult.CONVERTED;
+ } catch (Crypto.Error e) {
+ throw new IOError.FAILED(@"$(e.domain) error while decrypting: $(e.message)");
+ }
+ }
+}
+}
diff --git a/libdino/src/crypto/error.vala b/libdino/src/crypto/error.vala
new file mode 100644
index 00000000..2458ae4a
--- /dev/null
+++ b/libdino/src/crypto/error.vala
@@ -0,0 +1,11 @@
+namespace Dino.Crypto {
+
+public errordomain Error {
+ ILLEGAL_ARGUMENTS,
+ GCRYPT,
+ OPENSSL,
+ AUTHENTICATION_FAILED,
+ UNKNOWN
+}
+
+}
diff --git a/libdino/src/crypto/gnu/cipher.vala b/libdino/src/crypto/gnu/cipher.vala
new file mode 100644
index 00000000..41f15837
--- /dev/null
+++ b/libdino/src/crypto/gnu/cipher.vala
@@ -0,0 +1,137 @@
+namespace Dino.Crypto {
+
+internal class SymmetricCipherGcrypt : SymmetricCipher, SymmetricCipherEncryption, SymmetricCipherDecryption, Object {
+ private GCrypt.Cipher.Cipher cipher;
+
+ private static unowned string mode_to_string(GCrypt.Cipher.Mode mode) {
+ switch (mode) {
+ case GCrypt.Cipher.Mode.ECB: return "ECB";
+ case GCrypt.Cipher.Mode.CFB: return "CFB";
+ case GCrypt.Cipher.Mode.CBC: return "CBC";
+ case GCrypt.Cipher.Mode.STREAM: return "STREAM";
+ case GCrypt.Cipher.Mode.OFB: return "OFB";
+ case GCrypt.Cipher.Mode.CTR: return "CTR";
+ case GCrypt.Cipher.Mode.AESWRAP: return "AESWRAP";
+ case GCrypt.Cipher.Mode.GCM: return "GCM";
+ case GCrypt.Cipher.Mode.POLY1305: return "POLY1305";
+ case GCrypt.Cipher.Mode.OCB: return "OCB";
+ case GCrypt.Cipher.Mode.CFB8: return "CFB8";
+ // case GCrypt.Cipher.Mode.XTS: return "XTS"; // Not supported in gcrypt < 1.8
+ }
+ return "NONE";
+ }
+
+ private static GCrypt.Cipher.Mode mode_from_string(string name) {
+ switch (name) {
+ case "ECB": return GCrypt.Cipher.Mode.ECB;
+ case "CFB": return GCrypt.Cipher.Mode.CFB;
+ case "CBC": return GCrypt.Cipher.Mode.CBC;
+ case "STREAM": return GCrypt.Cipher.Mode.STREAM;
+ case "OFB": return GCrypt.Cipher.Mode.OFB;
+ case "CTR": return GCrypt.Cipher.Mode.CTR;
+ case "AESWRAP": return GCrypt.Cipher.Mode.AESWRAP;
+ case "GCM": return GCrypt.Cipher.Mode.GCM;
+ case "POLY1305": return GCrypt.Cipher.Mode.POLY1305;
+ case "OCB": return GCrypt.Cipher.Mode.OCB;
+ case "CFB8": return GCrypt.Cipher.Mode.CFB8;
+ // case "XTS": return GCrypt.Cipher.Mode.XTS; // Not supported in gcrypt < 1.8
+ }
+ return GCrypt.Cipher.Mode.NONE;
+ }
+
+ private static string flags_to_string(GCrypt.Cipher.Flag flags) {
+ string? s = null;
+ if ((GCrypt.Cipher.Flag.CBC_MAC & flags) != 0) s = (s == null ? "" : @"$s-") + "MAC";
+ if ((GCrypt.Cipher.Flag.CBC_CTS & flags) != 0) s = (s == null ? "" : @"$s-") + "CTS";
+ if ((GCrypt.Cipher.Flag.ENABLE_SYNC & flags) != 0) s = (s == null ? "" : @"$s-") + "SYNC";
+ if ((GCrypt.Cipher.Flag.SECURE & flags) != 0) s = (s == null ? "" : @"$s-") + "SECURE";
+ return s ?? "NONE";
+ }
+
+ private static GCrypt.Cipher.Flag flag_from_string(string flag_name) {
+ if (flag_name == "SECURE") return GCrypt.Cipher.Flag.SECURE;
+ if (flag_name == "SYNC") return GCrypt.Cipher.Flag.ENABLE_SYNC;
+ if (flag_name == "CTS") return GCrypt.Cipher.Flag.CBC_CTS;
+ if (flag_name == "MAC") return GCrypt.Cipher.Flag.CBC_MAC;
+ return 0;
+ }
+
+ private static GCrypt.Cipher.Flag flags_from_string(string flag_names) {
+ GCrypt.Cipher.Flag flags = 0;
+ foreach(string flag in flag_names.split("-")) {
+ flags |= flag_from_string(flag);
+ }
+ return flags;
+ }
+
+ private static bool parse(string algo_name, out GCrypt.Cipher.Algorithm algo, out GCrypt.Cipher.Mode mode, out GCrypt.Cipher.Flag flags) {
+ algo = GCrypt.Cipher.Algorithm.NONE;
+ mode = GCrypt.Cipher.Mode.NONE;
+ flags = 0;
+ string[] algo_parts = algo_name.split("-", 3);
+
+ algo = GCrypt.Cipher.Algorithm.from_string(algo_parts[0]);
+ if (algo_parts.length >= 2) {
+ mode = mode_from_string(algo_parts[1]);
+ }
+ if (algo_parts.length == 3) {
+ flags |= flags_from_string(algo_parts[2]);
+ }
+ return to_algo_name(algo, mode, flags) == algo_name;
+ }
+
+ private static string to_algo_name(GCrypt.Cipher.Algorithm algo = GCrypt.Cipher.Algorithm.NONE, GCrypt.Cipher.Mode mode = GCrypt.Cipher.Mode.NONE, GCrypt.Cipher.Flag flags = 0) {
+ if (flags != 0) {
+ return @"$algo-$(mode_to_string(mode))-$(flags_to_string(flags))";
+ } else if (mode != GCrypt.Cipher.Mode.NONE) {
+ return @"$algo-$(mode_to_string(mode))";
+ } else {
+ return algo.to_string();
+ }
+ }
+
+ public SymmetricCipherGcrypt(string algo_name) throws Error {
+ GCrypt.Cipher.Algorithm algo;
+ GCrypt.Cipher.Mode mode;
+ GCrypt.Cipher.Flag flags;
+ if (parse(algo_name, out algo, out mode, out flags)) {
+ may_throw_gcrypt_error(GCrypt.Cipher.Cipher.open(out this.cipher, algo, mode, flags));
+ } else {
+ throw new Error.ILLEGAL_ARGUMENTS(@"The algorithm $algo_name is not supported");
+ }
+ }
+
+ public void set_key(uint8[] key) throws Error {
+ may_throw_gcrypt_error(cipher.set_key(key));
+ }
+
+ public void set_iv(uint8[] iv) throws Error {
+ may_throw_gcrypt_error(cipher.set_iv(iv));
+ }
+
+ public void reset() throws Error {
+ may_throw_gcrypt_error(cipher.reset());
+ }
+
+ public uint8[] get_tag(size_t taglen) throws Error {
+ uint8[] tag = new uint8[taglen];
+ may_throw_gcrypt_error(cipher.get_tag(tag));
+ return tag;
+ }
+
+ public void check_tag(uint8[] tag) throws Error {
+ may_throw_gcrypt_error(cipher.check_tag(tag));
+ }
+
+ public int encrypt(uint8[] output, uint8[] input) throws Error {
+ may_throw_gcrypt_error(cipher.encrypt(output, input));
+ return input.length;
+ }
+
+ public int decrypt(uint8[] output, uint8[] input) throws Error {
+ may_throw_gcrypt_error(cipher.decrypt(output, input));
+ return input.length;
+ }
+}
+
+}
diff --git a/libdino/src/crypto/gnu/utils.vala b/libdino/src/crypto/gnu/utils.vala
new file mode 100644
index 00000000..8a25ab27
--- /dev/null
+++ b/libdino/src/crypto/gnu/utils.vala
@@ -0,0 +1,21 @@
+namespace Dino.Crypto {
+
+public static void randomize(uint8[] buffer) {
+ GCrypt.Random.randomize(buffer);
+}
+
+internal void may_throw_gcrypt_error(GCrypt.Error e) throws Error {
+ if (((int)e) != 0) {
+ throw new Crypto.Error.GCRYPT(e.to_string());
+ }
+}
+
+public SymmetricCipherEncryption create_symmetric_cipher_encryption(string algo_name) throws Error {
+ return new SymmetricCipherGcrypt(algo_name);
+}
+
+public SymmetricCipherDecryption create_symmetric_cipher_decryption(string algo_name) throws Error {
+ return new SymmetricCipherGcrypt(algo_name);
+}
+
+}
diff --git a/libdino/src/crypto/openssl/cipher.vala b/libdino/src/crypto/openssl/cipher.vala
new file mode 100644
index 00000000..02064770
--- /dev/null
+++ b/libdino/src/crypto/openssl/cipher.vala
@@ -0,0 +1,156 @@
+namespace Dino.Crypto {
+
+internal class SymmetricCipherEncryptionOpenssl : SymmetricCipher, SymmetricCipherEncryption, Object {
+ private OpenSSL.EVP.CipherContext? cipher;
+ private bool is_finalized = false;
+
+ public SymmetricCipherEncryptionOpenssl(string algo_name) throws Error {
+ if (algo_name != "AES-GCM") throw new Error.ILLEGAL_ARGUMENTS(@"The algorithm $algo_name is not supported");
+ cipher = new OpenSSL.EVP.CipherContext();
+ }
+
+ public void set_key(uint8[] key) throws Error {
+ unowned OpenSSL.EVP.Cipher algo;
+ if (key.length == 16) {
+ algo = OpenSSL.EVP.aes_128_gcm();
+ } else if (key.length == 32) {
+ algo = OpenSSL.EVP.aes_256_gcm();
+ } else {
+ throw new Crypto.Error.ILLEGAL_ARGUMENTS("key length must be 16 or 32 for AES-GCM");
+ }
+ try {
+ cipher.encrypt_init(algo, null, null);
+ cipher.encrypt_init(null, key, null);
+ } catch (GLib.Error e) {
+ throw new Crypto.Error.OPENSSL(e.message);
+ }
+ }
+
+ public void set_iv(uint8[] iv) throws Error {
+ if (iv.length != 12) {
+ cipher.ctrl(OpenSSL.EVP.Control.GCM_SET_IVLEN, iv.length, null);
+ }
+ try {
+ cipher.encrypt_init(null, null, iv);
+ } catch (GLib.Error e) {
+ throw new Crypto.Error.OPENSSL(e.message);
+ }
+ }
+
+ public void reset() throws Error {
+ throw new Crypto.Error.ILLEGAL_ARGUMENTS("can't reset OpenSSL cipher context");
+ }
+
+ public uint8[] get_tag(size_t taglen) throws Error {
+ uint8[] tag = new uint8[taglen];
+ if (!is_finalized) {
+ uint8[] empty = new uint8[0];
+ int empty_len = 0;
+ try {
+ cipher.encrypt_final(empty, out empty_len);
+ } catch (GLib.Error e) {
+ throw new Crypto.Error.OPENSSL(e.message);
+ }
+ if (empty_len != 0) {
+ throw new Crypto.Error.ILLEGAL_ARGUMENTS("get_tag called on a stream with remaining data");
+ }
+ }
+ if (cipher.ctrl(OpenSSL.EVP.Control.GCM_GET_TAG, (int)taglen, tag) != 1) {
+ openssl_error();
+ }
+ return tag;
+ }
+
+ public int encrypt(uint8[] output, uint8[] input) throws Error {
+ int output_length = output.length;
+ if (is_finalized) {
+ throw new Crypto.Error.ILLEGAL_ARGUMENTS("encrypt called after get_tag");
+ }
+ try {
+ cipher.encrypt_update(output, out output_length, input);
+ } catch (GLib.Error e) {
+ throw new Crypto.Error.OPENSSL(e.message);
+ }
+ return output_length;
+ }
+}
+
+internal class SymmetricCipherDecryptionOpenssl : SymmetricCipher, SymmetricCipherDecryption, Object {
+ private OpenSSL.EVP.CipherContext? cipher;
+ private bool is_finalized = false;
+
+ public SymmetricCipherDecryptionOpenssl(string algo_name) throws Error {
+ if (algo_name != "AES-GCM") throw new Error.ILLEGAL_ARGUMENTS(@"The algorithm $algo_name is not supported");
+ cipher = new OpenSSL.EVP.CipherContext();
+ try {
+ cipher.decrypt_init(OpenSSL.EVP.aes_128_gcm(), null, null);
+ } catch (GLib.Error e) {
+ throw new Crypto.Error.OPENSSL(e.message);
+ }
+ }
+
+ public void set_key(uint8[] key) throws Error {
+ unowned OpenSSL.EVP.Cipher algo;
+ if (key.length == 16) {
+ algo = OpenSSL.EVP.aes_128_gcm();
+ } else if (key.length == 32) {
+ algo = OpenSSL.EVP.aes_256_gcm();
+ } else {
+ throw new Crypto.Error.ILLEGAL_ARGUMENTS("key length must be 16 or 32 for AES-GCM");
+ }
+ try {
+ cipher.decrypt_init(algo, null, null);
+ cipher.decrypt_init(null, key, null);
+ } catch (GLib.Error e) {
+ throw new Crypto.Error.OPENSSL(e.message);
+ }
+ }
+
+ public void set_iv(uint8[] iv) throws Error {
+ if (iv.length != 12) {
+ cipher.ctrl(OpenSSL.EVP.Control.GCM_SET_IVLEN, iv.length, null);
+ }
+ try {
+ cipher.decrypt_init(null, null, iv);
+ } catch (GLib.Error e) {
+ throw new Crypto.Error.OPENSSL(e.message);
+ }
+ }
+
+ public void reset() throws Error {
+ throw new Crypto.Error.ILLEGAL_ARGUMENTS("can't reset OpenSSL cipher context");
+ }
+
+ public void check_tag(uint8[] tag) throws Error {
+ if (cipher.ctrl(OpenSSL.EVP.Control.GCM_SET_TAG, tag.length, tag) != 1) {
+ openssl_error();
+ }
+ if (!is_finalized) {
+ uint8[] empty = new uint8[0];
+ int empty_len = 0;
+ try {
+ cipher.decrypt_final(empty, out empty_len);
+ } catch (GLib.Error e) {
+ throw new Crypto.Error.OPENSSL(e.message);
+ }
+ if (empty_len != 0) {
+ throw new Crypto.Error.ILLEGAL_ARGUMENTS("check_tag called on a stream with remaining data");
+ }
+ }
+ }
+
+ public int decrypt(uint8[] output, uint8[] input) throws Error {
+ if (is_finalized) {
+ throw new Crypto.Error.ILLEGAL_ARGUMENTS("decrypt called after check_tag");
+ }
+ int output_length = output.length;
+ try {
+ cipher.decrypt_update(output, out output_length, input);
+ } catch (GLib.Error e) {
+ throw new Crypto.Error.OPENSSL(e.message);
+ }
+ return output_length;
+ }
+}
+
+}
diff --git a/libdino/src/crypto/openssl/utils.vala b/libdino/src/crypto/openssl/utils.vala
new file mode 100644
index 00000000..7a9ed9b6
--- /dev/null
+++ b/libdino/src/crypto/openssl/utils.vala
@@ -0,0 +1,19 @@
+namespace Dino.Crypto {
+
+public static void randomize(uint8[] buffer) {
+ OpenSSL.RAND.bytes(buffer);
+}
+
+internal void openssl_error() throws Error {
+ throw new Crypto.Error.OPENSSL(OpenSSL.ERR.reason_error_string(OpenSSL.ERR.get_error()));
+}
+
+public SymmetricCipherEncryption create_symmetric_cipher_encryption(string algo_name) throws Error {
+ return new SymmetricCipherEncryptionOpenssl(algo_name);
+}
+
+public SymmetricCipherDecryption create_symmetric_cipher_decryption(string algo_name) throws Error {
+ return new SymmetricCipherDecryptionOpenssl(algo_name);
+}
+
+}
diff --git a/libdino/src/crypto/srtp.vala b/libdino/src/crypto/srtp.vala
new file mode 100644
index 00000000..852dccdc
--- /dev/null
+++ b/libdino/src/crypto/srtp.vala
@@ -0,0 +1,122 @@
+using Srtp;
+
+namespace Dino.Crypto.Srtp {
+public const string AES_CM_128_HMAC_SHA1_80 = "AES_CM_128_HMAC_SHA1_80";
+public const string AES_CM_128_HMAC_SHA1_32 = "AES_CM_128_HMAC_SHA1_32";
+public const string F8_128_HMAC_SHA1_80 = "F8_128_HMAC_SHA1_80";
+
+public class Session {
+ public bool has_encrypt { get; private set; default = false; }
+ public bool has_decrypt { get; private set; default = false; }
+
+ private Context encrypt_context;
+ private Context decrypt_context;
+
+ static construct {
+ init();
+ install_log_handler(log);
+ }
+
+ private static void log(LogLevel level, string msg) {
+ print(@"SRTP[$level]: $msg\n");
+ }
+
+ public Session() {
+ Context.create(out encrypt_context, null);
+ Context.create(out decrypt_context, null);
+ }
+
+ public uint8[] encrypt_rtp(uint8[] data) throws Error {
+ uint8[] buf = new uint8[data.length + MAX_TRAILER_LEN];
+ Memory.copy(buf, data, data.length);
+ int buf_use = data.length;
+ ErrorStatus res = encrypt_context.protect(buf, ref buf_use);
+ if (res != ErrorStatus.ok) {
+ throw new Error.UNKNOWN(@"SRTP encrypt failed: $res");
+ }
+ buf.length = buf_use;
+ return buf;
+ }
+
+ public uint8[] decrypt_rtp(uint8[] data) throws Error {
+ uint8[] buf = new uint8[data.length];
+ Memory.copy(buf, data, data.length);
+ int buf_use = data.length;
+ ErrorStatus res = decrypt_context.unprotect(buf, ref buf_use);
+ switch (res) {
+ case ErrorStatus.auth_fail:
+ throw new Error.AUTHENTICATION_FAILED("SRTP packet failed the message authentication check");
+ case ErrorStatus.ok:
+ break;
+ default:
+ throw new Error.UNKNOWN(@"SRTP decrypt failed: $res");
+ }
+ uint8[] ret = new uint8[buf_use];
+ GLib.Memory.copy(ret, buf, buf_use);
+ return ret;
+ }
+
+ public uint8[] encrypt_rtcp(uint8[] data) throws Error {
+ uint8[] buf = new uint8[data.length + MAX_TRAILER_LEN + 4];
+ Memory.copy(buf, data, data.length);
+ int buf_use = data.length;
+ ErrorStatus res = encrypt_context.protect_rtcp(buf, ref buf_use);
+ if (res != ErrorStatus.ok) {
+ throw new Error.UNKNOWN(@"SRTCP encrypt failed: $res");
+ }
+ buf.length = buf_use;
+ return buf;
+ }
+
+ public uint8[] decrypt_rtcp(uint8[] data) throws Error {
+ uint8[] buf = new uint8[data.length];
+ Memory.copy(buf, data, data.length);
+ int buf_use = data.length;
+ ErrorStatus res = decrypt_context.unprotect_rtcp(buf, ref buf_use);
+ switch (res) {
+ case ErrorStatus.auth_fail:
+ throw new Error.AUTHENTICATION_FAILED("SRTCP packet failed the message authentication check");
+ case ErrorStatus.ok:
+ break;
+ default:
+ throw new Error.UNKNOWN(@"SRTCP decrypt failed: $res");
+ }
+ uint8[] ret = new uint8[buf_use];
+ GLib.Memory.copy(ret, buf, buf_use);
+ return ret;
+ }
+
+ private Policy create_policy(string profile) {
+ Policy policy = Policy();
+ switch (profile) {
+ case AES_CM_128_HMAC_SHA1_80:
+ policy.rtp.set_aes_cm_128_hmac_sha1_80();
+ policy.rtcp.set_aes_cm_128_hmac_sha1_80();
+ break;
+ }
+ return policy;
+ }
+
+ public void set_encryption_key(string profile, uint8[] key, uint8[] salt) {
+ Policy policy = create_policy(profile);
+ policy.ssrc.type = SsrcType.any_outbound;
+ policy.key = new uint8[key.length + salt.length];
+ Memory.copy(policy.key, key, key.length);
+ Memory.copy(((uint8*)policy.key) + key.length, salt, salt.length);
+ policy.next = null;
+ encrypt_context.add_stream(ref policy);
+ has_encrypt = true;
+ }
+
+ public void set_decryption_key(string profile, uint8[] key, uint8[] salt) {
+ Policy policy = create_policy(profile);
+ policy.ssrc.type = SsrcType.any_inbound;
+ policy.key = new uint8[key.length + salt.length];
+ Memory.copy(policy.key, key, key.length);
+ Memory.copy(((uint8*)policy.key) + key.length, salt, salt.length);
+ policy.next = null;
+ decrypt_context.add_stream(ref policy);
+ has_decrypt = true;
+ }
+}
+} \ No newline at end of file