Search in sources :

Example 1 with SESecurityManager

use of com.biglybt.pif.utils.security.SESecurityManager in project BiglyBT by BiglySoftware.

the class BuddyPluginBuddy method outgoingConnection.

protected GenericMessageConnection outgoingConnection() throws BuddyPluginException {
    GenericMessageRegistration msg_registration = plugin.getMessageRegistration();
    if (msg_registration == null) {
        throw (new BuddyPluginException("Messaging system unavailable"));
    }
    InetAddress ip = getIP();
    if (ip == null) {
        throw (new BuddyPluginException("Friend offline (no usable IP address)"));
    }
    InetSocketAddress tcp_target = null;
    InetSocketAddress udp_target = null;
    int tcp_port = getTCPPort();
    if (tcp_port > 0) {
        tcp_target = new InetSocketAddress(ip, tcp_port);
    }
    int udp_port = getUDPPort();
    if (udp_port > 0) {
        udp_target = new InetSocketAddress(ip, udp_port);
    }
    InetSocketAddress notional_target = tcp_target;
    if (notional_target == null) {
        notional_target = udp_target;
    }
    if (notional_target == null) {
        throw (new BuddyPluginException("Friend offline (no usable protocols)"));
    }
    GenericMessageEndpoint endpoint = msg_registration.createEndpoint(notional_target);
    if (tcp_target != null) {
        endpoint.addTCP(tcp_target);
    }
    if (udp_target != null) {
        endpoint.addUDP(udp_target);
    }
    GenericMessageConnection con = null;
    try {
        last_connect_attempt = SystemTime.getCurrentTime();
        con = msg_registration.createConnection(endpoint);
        plugin.addRateLimiters(con);
        String reason = "Friend: Outgoing connection establishment";
        SESecurityManager sec_man = plugin.getSecurityManager();
        con = sec_man.getSTSConnection(con, sec_man.getPublicKey(SEPublicKey.KEY_TYPE_ECC_192, reason), new SEPublicKeyLocator() {

            @Override
            public boolean accept(Object context, SEPublicKey other_key) {
                String other_key_str = Base32.encode(other_key.encodeRawPublicKey());
                if (other_key_str.equals(public_key)) {
                    consec_connect_fails = 0;
                    return (true);
                } else {
                    log(getString() + ": connection failed due to pk mismatch");
                    return (false);
                }
            }
        }, reason, BuddyPlugin.BLOCK_CRYPTO);
        con.connect();
        return (con);
    } catch (Throwable e) {
        if (con != null) {
            consec_connect_fails++;
            try {
                con.close();
            } catch (Throwable f) {
                log("Failed to close connection", f);
            }
        }
        throw (new BuddyPluginException("Failed to send message", e));
    }
}
Also used : SEPublicKey(com.biglybt.pif.utils.security.SEPublicKey) InetSocketAddress(java.net.InetSocketAddress) GenericMessageEndpoint(com.biglybt.pif.messaging.generic.GenericMessageEndpoint) SESecurityManager(com.biglybt.pif.utils.security.SESecurityManager) SEPublicKeyLocator(com.biglybt.pif.utils.security.SEPublicKeyLocator) GenericMessageEndpoint(com.biglybt.pif.messaging.generic.GenericMessageEndpoint) GenericMessageRegistration(com.biglybt.pif.messaging.generic.GenericMessageRegistration) InetAddress(java.net.InetAddress) GenericMessageConnection(com.biglybt.pif.messaging.generic.GenericMessageConnection)

Aggregations

GenericMessageConnection (com.biglybt.pif.messaging.generic.GenericMessageConnection)1 GenericMessageEndpoint (com.biglybt.pif.messaging.generic.GenericMessageEndpoint)1 GenericMessageRegistration (com.biglybt.pif.messaging.generic.GenericMessageRegistration)1 SEPublicKey (com.biglybt.pif.utils.security.SEPublicKey)1 SEPublicKeyLocator (com.biglybt.pif.utils.security.SEPublicKeyLocator)1 SESecurityManager (com.biglybt.pif.utils.security.SESecurityManager)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1