Search in sources :

Example 36 with SessionKey

use of net.i2p.data.SessionKey in project i2p.i2p by i2p.

the class PacketBuilder method buildSessionDestroyPacket.

/**
 *  Build a destroy packet, which contains a header but no body.
 *  If the keys and ip/port are not yet set, this will return null.
 *
 *  @return packet or null
 *  @since 0.9.2
 */
public UDPPacket buildSessionDestroyPacket(InboundEstablishState peer) {
    SessionKey cipherKey = peer.getCipherKey();
    SessionKey macKey = peer.getMACKey();
    byte[] ip = peer.getSentIP();
    int port = peer.getSentPort();
    if (cipherKey == null || macKey == null || ip == null || port <= 0) {
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("Cannot send destroy, incomplete " + peer);
        return null;
    }
    InetAddress addr;
    try {
        addr = InetAddress.getByAddress(ip);
    } catch (UnknownHostException uhe) {
        return null;
    }
    if (_log.shouldLog(Log.DEBUG))
        _log.debug("building session destroy packet to " + peer);
    return buildSessionDestroyPacket(cipherKey, macKey, addr, port);
}
Also used : UnknownHostException(java.net.UnknownHostException) SessionKey(net.i2p.data.SessionKey) InetAddress(java.net.InetAddress)

Example 37 with SessionKey

use of net.i2p.data.SessionKey in project i2p.i2p by i2p.

the class PacketBuilder method buildSessionDestroyPacket.

/**
 *  Build a destroy packet, which contains a header but no body.
 *  If the keys and ip/port are not yet set, this will return null.
 *
 *  @return packet or null
 *  @since 0.9.2
 */
public UDPPacket buildSessionDestroyPacket(OutboundEstablishState peer) {
    SessionKey cipherKey = peer.getCipherKey();
    SessionKey macKey = peer.getMACKey();
    byte[] ip = peer.getSentIP();
    int port = peer.getSentPort();
    if (cipherKey == null || macKey == null || ip == null || port <= 0) {
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("Cannot send destroy, incomplete " + peer);
        return null;
    }
    InetAddress addr;
    try {
        addr = InetAddress.getByAddress(ip);
    } catch (UnknownHostException uhe) {
        return null;
    }
    if (_log.shouldLog(Log.DEBUG))
        _log.debug("building session destroy packet to " + peer);
    return buildSessionDestroyPacket(cipherKey, macKey, addr, port);
}
Also used : UnknownHostException(java.net.UnknownHostException) SessionKey(net.i2p.data.SessionKey) InetAddress(java.net.InetAddress)

Example 38 with SessionKey

use of net.i2p.data.SessionKey in project i2p.i2p by i2p.

the class PeerTestManager method receiveTestReply.

/**
 * Receive a PeerTest message which contains the correct nonce for our current
 * test. We are Alice.
 */
private synchronized void receiveTestReply(RemoteHostId from, UDPPacketReader.PeerTestReader testInfo) {
    _context.statManager().addRateData("udp.receiveTestReply", 1);
    PeerTestState test = _currentTest;
    if (expired())
        return;
    if (_currentTestComplete)
        return;
    if ((DataHelper.eq(from.getIP(), test.getBobIP().getAddress())) && (from.getPort() == test.getBobPort())) {
        // The reply is from Bob
        int ipSize = testInfo.readIPSize();
        boolean expectV6 = test.isIPv6();
        if ((!expectV6 && ipSize != 4) || (expectV6 && ipSize != 16)) {
            // To do: fix the bug.
            if (_log.shouldLog(Log.WARN))
                _log.warn("Bad IP length " + ipSize + " from bob's reply: " + from + ", " + testInfo);
            return;
        }
        byte[] ip = new byte[ipSize];
        testInfo.readIP(ip, 0);
        try {
            InetAddress addr = InetAddress.getByAddress(ip);
            test.setAliceIP(addr);
            test.setReceiveBobTime(_context.clock().now());
            int testPort = testInfo.readPort();
            if (testPort == 0)
                throw new UnknownHostException("port 0");
            test.setAlicePort(testPort);
            if (_log.shouldLog(Log.DEBUG))
                _log.debug("Receive test reply from Bob: " + test);
            if (test.getAlicePortFromCharlie() > 0)
                testComplete(true);
        } catch (UnknownHostException uhe) {
            if (_log.shouldLog(Log.WARN))
                _log.warn("Unable to get our IP (length " + ipSize + ") from bob's reply: " + from + ", " + testInfo, uhe);
            _context.statManager().addRateData("udp.testBadIP", 1);
        }
    } else {
        // The reply is from Charlie
        PeerState charlieSession = _transport.getPeerState(from);
        long recentBegin = _context.clock().now() - CHARLIE_RECENT_PERIOD;
        if ((charlieSession != null) && ((charlieSession.getLastACKSend() > recentBegin) || (charlieSession.getLastSendTime() > recentBegin))) {
            if (_log.shouldLog(Log.WARN))
                _log.warn("Bob chose a charlie we already have a session to, cancelling the test and rerunning (bob: " + _currentTest + ", charlie: " + from + ")");
            // why are we doing this instead of calling testComplete() ?
            _currentTestComplete = true;
            _context.statManager().addRateData("udp.statusKnownCharlie", 1);
            honorStatus(Status.UNKNOWN, test.isIPv6());
            _currentTest = null;
            return;
        }
        if (test.getReceiveCharlieTime() > 0) {
            // this is our second charlie, yay!
            try {
                int testPort = testInfo.readPort();
                if (testPort == 0)
                    throw new UnknownHostException("port 0");
                test.setAlicePortFromCharlie(testPort);
                byte[] ip = new byte[testInfo.readIPSize()];
                int ipSize = ip.length;
                boolean expectV6 = test.isIPv6();
                if ((!expectV6 && ipSize != 4) || (expectV6 && ipSize != 16))
                    throw new UnknownHostException("bad sz - expect v6? " + expectV6 + " act sz: " + ipSize);
                testInfo.readIP(ip, 0);
                InetAddress addr = InetAddress.getByAddress(ip);
                test.setAliceIPFromCharlie(addr);
                if (_log.shouldLog(Log.DEBUG))
                    _log.debug("Receive test reply from Charlie: " + test);
                if (test.getReceiveBobTime() > 0)
                    testComplete(true);
            } catch (UnknownHostException uhe) {
                if (_log.shouldLog(Log.ERROR))
                    _log.error("Charlie @ " + from + " said we were an invalid IP address: " + uhe.getMessage(), uhe);
                _context.statManager().addRateData("udp.testBadIP", 1);
            }
        } else {
            if (test.incrementPacketsRelayed() > MAX_RELAYED_PER_TEST_ALICE) {
                testComplete(false);
                if (_log.shouldLog(Log.WARN))
                    _log.warn("Sent too many packets on the test: " + test);
                return;
            }
            if (_log.shouldLog(Log.INFO) && charlieSession != null)
                _log.info("Bob chose a charlie we last acked " + DataHelper.formatDuration(_context.clock().now() - charlieSession.getLastACKSend()) + " last sent " + DataHelper.formatDuration(_context.clock().now() - charlieSession.getLastSendTime()) + " (bob: " + _currentTest + ", charlie: " + from + ")");
            // ok, first charlie.  send 'em a packet
            test.setReceiveCharlieTime(_context.clock().now());
            SessionKey charlieIntroKey = new SessionKey(new byte[SessionKey.KEYSIZE_BYTES]);
            testInfo.readIntroKey(charlieIntroKey.getData(), 0);
            test.setCharlieIntroKey(charlieIntroKey);
            try {
                test.setCharlieIP(InetAddress.getByAddress(from.getIP()));
                test.setCharliePort(from.getPort());
                if (_log.shouldLog(Log.DEBUG))
                    _log.debug("Receive test from Charlie: " + test);
                sendTestToCharlie();
            } catch (UnknownHostException uhe) {
                if (_log.shouldLog(Log.WARN))
                    _log.warn("Charlie's IP is b0rked: " + from + ": " + testInfo);
                _context.statManager().addRateData("udp.testBadIP", 1);
            }
        }
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) SessionKey(net.i2p.data.SessionKey) InetAddress(java.net.InetAddress)

Example 39 with SessionKey

use of net.i2p.data.SessionKey in project i2p.i2p by i2p.

the class PeerTestManager method receiveFromAliceAsCharlie.

/**
 * We are charlie, so send Alice her PeerTest message
 *
 * testInfo IP/port ignored
 * @param state non-null
 */
private void receiveFromAliceAsCharlie(RemoteHostId from, UDPPacketReader.PeerTestReader testInfo, long nonce, PeerTestState state) {
    long now = _context.clock().now();
    if (state.getReceiveAliceTime() > now - (RESEND_TIMEOUT / 2)) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("Too soon, not retransmitting: " + state);
        return;
    }
    if (state.incrementPacketsRelayed() > MAX_RELAYED_PER_TEST_CHARLIE) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("Too many, not retransmitting: " + state);
        return;
    }
    state.setReceiveAliceTime(now);
    try {
        InetAddress aliceIP = InetAddress.getByAddress(from.getIP());
        SessionKey aliceIntroKey = new SessionKey(new byte[SessionKey.KEYSIZE_BYTES]);
        testInfo.readIntroKey(aliceIntroKey.getData(), 0);
        UDPPacket packet = _packetBuilder.buildPeerTestToAlice(aliceIP, from.getPort(), aliceIntroKey, _transport.getIntroKey(), nonce);
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("Receive from Alice: " + state);
        _transport.send(packet);
    } catch (UnknownHostException uhe) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("Unable to build the aliceIP from " + from, uhe);
        _context.statManager().addRateData("udp.testBadIP", 1);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) SessionKey(net.i2p.data.SessionKey) InetAddress(java.net.InetAddress)

Example 40 with SessionKey

use of net.i2p.data.SessionKey in project i2p.i2p by i2p.

the class IndexBean method generateNewEncryptionKey.

/**
 * New key
 */
private String generateNewEncryptionKey() {
    TunnelController tun = getController(_tunnel);
    Properties config = getConfig();
    if (tun == null) {
        // creating new
        tun = new TunnelController(config, "", true);
        _group.addController(tun);
        saveChanges();
    } else if (tun.getIsRunning() || tun.getIsStarting()) {
        return "Tunnel must be stopped before modifying leaseset encryption key";
    }
    byte[] data = new byte[SessionKey.KEYSIZE_BYTES];
    _context.random().nextBytes(data);
    SessionKey sk = new SessionKey(data);
    setEncryptKey(sk.toBase64());
    setEncrypt("");
    saveChanges();
    return "New Leaseset Encryption Key: " + sk.toBase64();
}
Also used : SessionKey(net.i2p.data.SessionKey) TunnelController(net.i2p.i2ptunnel.TunnelController) Properties(java.util.Properties)

Aggregations

SessionKey (net.i2p.data.SessionKey)69 SessionTag (net.i2p.data.SessionTag)15 PublicKey (net.i2p.data.PublicKey)14 I2PAppContext (net.i2p.I2PAppContext)13 HashSet (java.util.HashSet)11 Hash (net.i2p.data.Hash)11 SessionKeyManager (net.i2p.crypto.SessionKeyManager)10 PrivateKey (net.i2p.data.PrivateKey)10 InetAddress (java.net.InetAddress)9 DataFormatException (net.i2p.data.DataFormatException)9 UnknownHostException (java.net.UnknownHostException)7 TagSetHandle (net.i2p.crypto.TagSetHandle)5 Map (java.util.Map)4 GarlicMessage (net.i2p.data.i2np.GarlicMessage)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Set (java.util.Set)3 EncryptedBuildRecord (net.i2p.data.i2np.EncryptedBuildRecord)3 BigInteger (java.math.BigInteger)2