2020-11-16 14:55:33 +00:00
|
|
|
public class Xmpp.StartTlsXmppStream : TlsXmppStream {
|
|
|
|
|
|
|
|
private const string TLS_NS_URI = "urn:ietf:params:xml:ns:xmpp-tls";
|
|
|
|
|
|
|
|
string host;
|
|
|
|
uint16 port;
|
2021-04-07 21:39:02 +00:00
|
|
|
TlsXmppStream.OnInvalidCertWrapper on_invalid_cert;
|
2020-11-16 14:55:33 +00:00
|
|
|
|
2021-04-07 21:39:02 +00:00
|
|
|
public StartTlsXmppStream(Jid remote, string host, uint16 port, TlsXmppStream.OnInvalidCertWrapper on_invalid_cert) {
|
2020-12-31 18:00:54 +00:00
|
|
|
base(remote);
|
2020-11-16 14:55:33 +00:00
|
|
|
this.host = host;
|
|
|
|
this.port = port;
|
2021-04-07 21:39:02 +00:00
|
|
|
this.on_invalid_cert = on_invalid_cert;
|
2020-11-16 14:55:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override async void connect() throws IOStreamError {
|
|
|
|
try {
|
|
|
|
SocketClient client = new SocketClient();
|
2021-03-01 05:16:47 +00:00
|
|
|
debug("Connecting to %s:%i (starttls)", host, port);
|
2020-11-16 14:55:33 +00:00
|
|
|
IOStream stream = yield client.connect_to_host_async(host, port);
|
|
|
|
reset_stream(stream);
|
|
|
|
|
|
|
|
yield setup();
|
|
|
|
|
|
|
|
StanzaNode node = yield read();
|
|
|
|
var starttls_node = node.get_subnode("starttls", TLS_NS_URI);
|
|
|
|
if (starttls_node == null) {
|
|
|
|
warning("%s does not offer starttls", remote_name.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
write(new StanzaNode.build("starttls", TLS_NS_URI).add_self_xmlns());
|
|
|
|
|
|
|
|
node = yield read();
|
|
|
|
|
|
|
|
if (node.ns_uri != TLS_NS_URI || node.name != "proceed") {
|
|
|
|
warning("Server did not 'proceed' starttls request");
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
var identity = new NetworkService("xmpp-client", "tcp", remote_name.to_string());
|
|
|
|
var conn = TlsClientConnection.new(get_stream(), identity);
|
|
|
|
reset_stream(conn);
|
|
|
|
|
|
|
|
conn.accept_certificate.connect(on_invalid_certificate);
|
2021-04-07 21:39:02 +00:00
|
|
|
conn.accept_certificate.connect((cert, flags) => on_invalid_cert.func(cert, flags));
|
2020-11-16 14:55:33 +00:00
|
|
|
} catch (Error e) {
|
|
|
|
stderr.printf("Failed to start TLS: %s\n", e.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
yield setup();
|
|
|
|
|
|
|
|
attach_negotation_modules();
|
|
|
|
} catch (Error e) {
|
|
|
|
throw new IOStreamError.CONNECT("Failed connecting to %s:%i (starttls): %s", host, port, e.message);
|
|
|
|
}
|
|
|
|
}
|
2021-03-01 05:16:47 +00:00
|
|
|
}
|