Debian Codegraph @

crypto-vala/src/srtp.vala

raw
1 using Srtp;
2
3 namespace Crypto.Srtp {
4 public const string AES_CM_128_HMAC_SHA1_80 = "AES_CM_128_HMAC_SHA1_80";
5 public const string AES_CM_128_HMAC_SHA1_32 = "AES_CM_128_HMAC_SHA1_32";
6 public const string F8_128_HMAC_SHA1_80 = "F8_128_HMAC_SHA1_80";
7
8 public class Session {
9 public bool has_encrypt { get; private set; default = false; }
10 public bool has_decrypt { get; private set; default = false; }
11
12 private Context encrypt_context;
13 private Context decrypt_context;
14
15 static construct {
16 init();
17 install_log_handler(log);
18 }
19
20 private static void log(LogLevel level, string msg) {
21 print(@"SRTP[$level]: $msg\n");
22 }
23
24 public Session() {
25 Context.create(out encrypt_context, null);
26 Context.create(out decrypt_context, null);
27 }
28
29 public uint8[] encrypt_rtp(uint8[] data) throws Error {
30 uint8[] buf = new uint8[data.length + MAX_TRAILER_LEN];
31 Memory.copy(buf, data, data.length);
32 int buf_use = data.length;
33 ErrorStatus res = encrypt_context.protect(buf, ref buf_use);
34 if (res != ErrorStatus.ok) {
35 throw new Error.UNKNOWN(@"SRTP encrypt failed: $res");
36 }
37 buf.length = buf_use;
38 return buf;
39 }
40
41 public uint8[] decrypt_rtp(uint8[] data) throws Error {
42 uint8[] buf = new uint8[data.length];
43 Memory.copy(buf, data, data.length);
44 int buf_use = data.length;
45 ErrorStatus res = decrypt_context.unprotect(buf, ref buf_use);
46 switch (res) {
47 case ErrorStatus.auth_fail:
48 throw new Error.AUTHENTICATION_FAILED("SRTP packet failed the message authentication check");
49 case ErrorStatus.ok:
50 break;
51 default:
52 throw new Error.UNKNOWN(@"SRTP decrypt failed: $res");
53 }
54 uint8[] ret = new uint8[buf_use];
55 GLib.Memory.copy(ret, buf, buf_use);
56 return ret;
57 }
58
59 public uint8[] encrypt_rtcp(uint8[] data) throws Error {
60 uint8[] buf = new uint8[data.length + MAX_TRAILER_LEN + 4];
61 Memory.copy(buf, data, data.length);
62 int buf_use = data.length;
63 ErrorStatus res = encrypt_context.protect_rtcp(buf, ref buf_use);
64 if (res != ErrorStatus.ok) {
65 throw new Error.UNKNOWN(@"SRTCP encrypt failed: $res");
66 }
67 buf.length = buf_use;
68 return buf;
69 }
70
71 public uint8[] decrypt_rtcp(uint8[] data) throws Error {
72 uint8[] buf = new uint8[data.length];
73 Memory.copy(buf, data, data.length);
74 int buf_use = data.length;
75 ErrorStatus res = decrypt_context.unprotect_rtcp(buf, ref buf_use);
76 switch (res) {
77 case ErrorStatus.auth_fail:
78 throw new Error.AUTHENTICATION_FAILED("SRTCP packet failed the message authentication check");
79 case ErrorStatus.ok:
80 break;
81 default:
82 throw new Error.UNKNOWN(@"SRTCP decrypt failed: $res");
83 }
84 uint8[] ret = new uint8[buf_use];
85 GLib.Memory.copy(ret, buf, buf_use);
86 return ret;
87 }
88
89 private Policy create_policy(string profile) {
90 Policy policy = Policy();
91 switch (profile) {
92 case AES_CM_128_HMAC_SHA1_80:
93 policy.rtp.set_aes_cm_128_hmac_sha1_80();
94 policy.rtcp.set_aes_cm_128_hmac_sha1_80();
95 break;
96 }
97 return policy;
98 }
99
100 public void set_encryption_key(string profile, uint8[] key, uint8[] salt) {
101 Policy policy = create_policy(profile);
102 policy.ssrc.type = SsrcType.any_outbound;
103 policy.key = new uint8[key.length + salt.length];
104 Memory.copy(policy.key, key, key.length);
105 Memory.copy(((uint8*)policy.key) + key.length, salt, salt.length);
106 policy.next = null;
107 encrypt_context.add_stream(ref policy);
108 has_encrypt = true;
109 }
110
111 public void set_decryption_key(string profile, uint8[] key, uint8[] salt) {
112 Policy policy = create_policy(profile);
113 policy.ssrc.type = SsrcType.any_inbound;
114 policy.key = new uint8[key.length + salt.length];
115 Memory.copy(policy.key, key, key.length);
116 Memory.copy(((uint8*)policy.key) + key.length, salt, salt.length);
117 policy.next = null;
118 decrypt_context.add_stream(ref policy);
119 has_decrypt = true;
120 }
121 }
122 }