Debian Codegraph @

xmpp-vala/src/core/starttls_xmpp_stream.vala

raw
1 public class Xmpp.StartTlsXmppStream : TlsXmppStream {
2
3 private const string TLS_NS_URI = "urn:ietf:params:xml:ns:xmpp-tls";
4
5 string host;
6 uint16 port;
7 TlsXmppStream.OnInvalidCertWrapper on_invalid_cert;
8
9 public StartTlsXmppStream(Jid remote, string host, uint16 port, TlsXmppStream.OnInvalidCertWrapper on_invalid_cert) {
10 base(remote);
11 this.host = host;
12 this.port = port;
13 this.on_invalid_cert = on_invalid_cert;
14 }
15
16 public override async void connect() throws IOError {
17 try {
18 SocketClient client = new SocketClient();
19 debug("Connecting to %s:%i (starttls)", host, port);
20 IOStream stream = yield client.connect_to_host_async(host, port, cancellable);
21 reset_stream(stream);
22
23 yield setup();
24
25 StanzaNode node = yield read();
26 var starttls_node = node.get_subnode("starttls", TLS_NS_URI);
27 if (starttls_node == null) {
28 warning("%s does not offer starttls", remote_name.to_string());
29 }
30
31 write(new StanzaNode.build("starttls", TLS_NS_URI).add_self_xmlns());
32
33 node = yield read();
34
35 if (node.ns_uri != TLS_NS_URI || node.name != "proceed") {
36 warning("Server did not 'proceed' starttls request");
37 }
38
39 try {
40 var identity = new NetworkService("xmpp-client", "tcp", remote_name.to_string());
41 var conn = TlsClientConnection.new(get_stream(), identity);
42 reset_stream(conn);
43
44 conn.accept_certificate.connect(on_invalid_certificate);
45 conn.accept_certificate.connect((cert, flags) => on_invalid_cert.func(cert, flags));
46 } catch (Error e) {
47 stderr.printf("Failed to start TLS: %s\n", e.message);
48 }
49
50 yield setup();
51
52 attach_negotation_modules();
53 } catch (IOError e) {
54 throw e;
55 } catch (Error e) {
56 throw new IOError.CONNECTION_REFUSED("Failed connecting to %s:%i (starttls): %s", host, port, e.message);
57 }
58 }
59 }