Search in sources :

Example 1 with LeaseSetKeys

use of net.i2p.router.LeaseSetKeys in project i2p.i2p by i2p.

the class GarlicMessageReceiver method receive.

public void receive(GarlicMessage message) {
    PrivateKey decryptionKey;
    SessionKeyManager skm;
    if (_clientDestination != null) {
        LeaseSetKeys keys = _context.keyManager().getKeys(_clientDestination);
        skm = _context.clientManager().getClientSessionKeyManager(_clientDestination);
        if (keys != null && skm != null) {
            decryptionKey = keys.getDecryptionKey();
        } else {
            if (_log.shouldLog(Log.WARN))
                _log.warn("Not trying to decrypt a garlic routed message to a disconnected client");
            return;
        }
    } else {
        decryptionKey = _context.keyManager().getPrivateKey();
        skm = _context.sessionKeyManager();
    }
    CloveSet set = _context.garlicMessageParser().getGarlicCloves(message, decryptionKey, skm);
    if (set != null) {
        for (int i = 0; i < set.getCloveCount(); i++) {
            GarlicClove clove = set.getClove(i);
            handleClove(clove);
        }
    } else {
        if (_log.shouldLog(Log.WARN))
            _log.warn("CloveMessageParser failed to decrypt the message [" + message.getUniqueId() + "]", new Exception("Decrypt garlic failed"));
        _context.statManager().addRateData("crypto.garlic.decryptFail", 1);
        _context.messageHistory().messageProcessingError(message.getUniqueId(), message.getClass().getName(), "Garlic could not be decrypted");
    }
}
Also used : PrivateKey(net.i2p.data.PrivateKey) LeaseSetKeys(net.i2p.router.LeaseSetKeys) SessionKeyManager(net.i2p.crypto.SessionKeyManager) GarlicClove(net.i2p.data.i2np.GarlicClove)

Example 2 with LeaseSetKeys

use of net.i2p.router.LeaseSetKeys in project i2p.i2p by i2p.

the class ClientMessageEventListener method handleCreateLeaseSet.

/**
 * override for testing
 */
protected void handleCreateLeaseSet(CreateLeaseSetMessage message) {
    if ((message.getLeaseSet() == null) || (message.getPrivateKey() == null) || (message.getSigningPrivateKey() == null)) {
        if (_log.shouldLog(Log.ERROR))
            _log.error("Null lease set granted: " + message);
        _runner.disconnectClient("Invalid CreateLeaseSetMessage");
        return;
    }
    SessionId id = message.getSessionId();
    SessionConfig cfg = _runner.getConfig(id);
    if (cfg == null) {
        List<SessionId> current = _runner.getSessionIds();
        String msg = "CreateLeaseSet invalid session: " + id + " current: " + current;
        if (_log.shouldLog(Log.ERROR))
            _log.error(msg);
        _runner.disconnectClient(msg);
        return;
    }
    Destination dest = cfg.getDestination();
    Destination ndest = message.getLeaseSet().getDestination();
    if (!dest.equals(ndest)) {
        if (_log.shouldLog(Log.ERROR))
            _log.error("Different destination in LS");
        _runner.disconnectClient("Different destination in LS");
        return;
    }
    LeaseSetKeys keys = _context.keyManager().getKeys(dest);
    if (keys == null || !message.getPrivateKey().equals(keys.getDecryptionKey())) {
        // Verify and register crypto keys if new or if changed
        // Private crypto key should never change, and if it does,
        // one of the checks below will fail
        PublicKey pk;
        try {
            pk = message.getPrivateKey().toPublic();
        } catch (IllegalArgumentException iae) {
            if (_log.shouldLog(Log.ERROR))
                _log.error("Bad private key in LS");
            _runner.disconnectClient("Bad private key in LS");
            return;
        }
        if (!pk.equals(message.getLeaseSet().getEncryptionKey())) {
            if (_log.shouldLog(Log.ERROR))
                _log.error("Private/public crypto key mismatch in LS");
            _runner.disconnectClient("Private/public crypto key mismatch in LS");
            return;
        }
        // just register new SPK, don't verify, unused
        _context.keyManager().registerKeys(dest, message.getSigningPrivateKey(), message.getPrivateKey());
    } else if (!message.getSigningPrivateKey().equals(keys.getRevocationKey())) {
        // just register new SPK, don't verify, unused
        _context.keyManager().registerKeys(dest, message.getSigningPrivateKey(), message.getPrivateKey());
    }
    try {
        _context.netDb().publish(message.getLeaseSet());
    } catch (IllegalArgumentException iae) {
        if (_log.shouldLog(Log.ERROR))
            _log.error("Invalid leaseset from client", iae);
        _runner.disconnectClient("Invalid leaseset: " + iae);
        return;
    }
    if (_log.shouldLog(Log.INFO))
        _log.info("New lease set granted for destination " + dest);
    // leaseSetCreated takes care of all the LeaseRequestState stuff (including firing any jobs)
    _runner.leaseSetCreated(message.getLeaseSet());
}
Also used : Destination(net.i2p.data.Destination) PublicKey(net.i2p.data.PublicKey) SessionConfig(net.i2p.data.i2cp.SessionConfig) LeaseSetKeys(net.i2p.router.LeaseSetKeys) SessionId(net.i2p.data.i2cp.SessionId)

Aggregations

LeaseSetKeys (net.i2p.router.LeaseSetKeys)2 SessionKeyManager (net.i2p.crypto.SessionKeyManager)1 Destination (net.i2p.data.Destination)1 PrivateKey (net.i2p.data.PrivateKey)1 PublicKey (net.i2p.data.PublicKey)1 SessionConfig (net.i2p.data.i2cp.SessionConfig)1 SessionId (net.i2p.data.i2cp.SessionId)1 GarlicClove (net.i2p.data.i2np.GarlicClove)1