Debian Codegraph @

xmpp-vala/src/module/xep/0391_jingle_encrypted_transports.vala

raw
1 using Gee;
2 using Xmpp.Xep.Jingle;
3
4 namespace Xmpp.Xep.Jet {
5 public const string NS_URI = "urn:xmpp:jingle:jet:0";
6
7 public class Module : XmppStreamModule, SecurityPrecondition {
8 public static Xmpp.ModuleIdentity<Module> IDENTITY = new Xmpp.ModuleIdentity<Module>(NS_URI, "0391_jet");
9 private HashMap<string, EnvelopEncoding> envelop_encodings = new HashMap<string, EnvelopEncoding>();
10 private HashMap<string, Cipher> ciphers = new HashMap<string, Cipher>();
11
12 public override void attach(XmppStream stream) {
13 stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI);
14 stream.get_module(Jingle.Module.IDENTITY).register_security_precondition(this);
15 }
16
17 public override void detach(XmppStream stream) {
18 stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI);
19 }
20
21 public async bool is_available(XmppStream stream, Jid full_jid) {
22 return yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI);
23 }
24
25 public void register_envelop_encoding(EnvelopEncoding encoding) {
26 envelop_encodings[encoding.get_type_uri()] = encoding;
27 }
28
29 public void register_cipher(Cipher cipher) {
30 ciphers[cipher.get_cipher_uri()] = cipher;
31 }
32
33 public string security_ns_uri() {
34 return NS_URI;
35 }
36
37 public Jingle.SecurityParameters? create_security_parameters(XmppStream stream, Jid local_full_jid, Jid peer_full_jid, Object options) throws Jingle.Error requires (options is Options) {
38 Options jet_options = (Options) options;
39 string cipher = jet_options.cipher_uri;
40 string type = jet_options.type_uri;
41 if (!envelop_encodings.has_key(type) || !ciphers.has_key(cipher)) {
42 throw new Jingle.Error.UNSUPPORTED_SECURITY("JET cipher or type unknown");
43 }
44 EnvelopEncoding encoding = envelop_encodings[type];
45 return new SecurityParameters(ciphers[cipher], encoding, ciphers[cipher].generate_random_secret(), jet_options);
46 }
47
48 public Jingle.SecurityParameters? parse_security_parameters(XmppStream stream, Jid local_full_jid, Jid peer_full_jid, StanzaNode security) throws IqError {
49 string? cipher = security.get_attribute("cipher");
50 string? type = security.get_attribute("type");
51 if (cipher == null || type == null) {
52 throw new IqError.BAD_REQUEST("No cipher or type specified for JET");
53 }
54 if (!envelop_encodings.has_key(type) || !ciphers.has_key(cipher)) {
55 throw new IqError.NOT_IMPLEMENTED("JET cipher or type unknown");
56 }
57 EnvelopEncoding encoding = envelop_encodings[type];
58 TransportSecret secret = encoding.decode_envolop(stream, local_full_jid, peer_full_jid, security);
59 return new SecurityParameters(ciphers[cipher], encoding, secret);
60 }
61
62 public override string get_ns() { return NS_URI; }
63 public override string get_id() { return IDENTITY.id; }
64 }
65
66 public class Options : Object {
67 public string type_uri { get; private set; }
68 public string cipher_uri { get; private set; }
69
70 public Options(string type_uri, string cipher_uri) {
71 this.type_uri = type_uri;
72 this.cipher_uri = cipher_uri;
73 }
74 }
75
76 public class SecurityParameters : Jingle.SecurityParameters, Object {
77 public Cipher cipher { get; private set; }
78 public EnvelopEncoding encoding { get; private set; }
79 public TransportSecret secret { get; private set; }
80 public Options? options { get; private set; }
81
82 public SecurityParameters(Cipher cipher, EnvelopEncoding encoding, TransportSecret secret, Options? options = null) {
83 this.cipher = cipher;
84 this.encoding = encoding;
85 this.secret = secret;
86 this.options = options;
87 }
88
89 public string security_ns_uri() {
90 return NS_URI;
91 }
92 public IOStream wrap_stream(IOStream stream) {
93 debug("Wrapping stream into encrypted stream for %s/%s", encoding.get_type_uri(), cipher.get_cipher_uri());
94 return new EncryptedStream(cipher, secret, stream);
95 }
96 public StanzaNode to_security_stanza_node(XmppStream stream, Jid local_full_jid, Jid peer_full_jid) {
97 StanzaNode security = new StanzaNode.build("security", NS_URI)
98 .add_self_xmlns()
99 .put_attribute("cipher", cipher.get_cipher_uri())
100 .put_attribute("type", encoding.get_type_uri());
101 encoding.encode_envelop(stream, local_full_jid, peer_full_jid, this, security);
102 return security;
103 }
104 }
105
106 public interface Cipher : Object {
107 public abstract string get_cipher_uri();
108 public abstract TransportSecret generate_random_secret();
109 public abstract InputStream wrap_input_stream(InputStream input, TransportSecret secret);
110 public abstract OutputStream wrap_output_stream(OutputStream output, TransportSecret secret);
111 }
112
113 private class EncryptedStream : IOStream {
114 private IOStream stream;
115 private InputStream input;
116 private OutputStream output;
117 public override InputStream input_stream { get { return input; } }
118 public override OutputStream output_stream { get { return output; } }
119
120 public EncryptedStream(Cipher cipher, TransportSecret secret, IOStream stream) {
121 this.stream = stream;
122 input = cipher.wrap_input_stream(stream.input_stream, secret);
123 output = cipher.wrap_output_stream(stream.output_stream, secret);
124 }
125 }
126
127 public class TransportSecret {
128 public uint8[] transport_key { get; private set; }
129 public uint8[] initialization_vector { get; private set; }
130 public TransportSecret(uint8[] transport_key, uint8[] initialization_vector) {
131 this.transport_key = transport_key;
132 this.initialization_vector = initialization_vector;
133 }
134 }
135
136 public interface EnvelopEncoding : Object {
137 public abstract string get_type_uri();
138 public abstract TransportSecret decode_envolop(XmppStream stream, Jid local_full_jid, Jid peer_full_jid, StanzaNode security) throws IqError;
139 public abstract void encode_envelop(XmppStream stream, Jid local_full_jid, Jid peer_full_jid, SecurityParameters security_params, StanzaNode security);
140 }
141
142 }