Search in sources :

Example 56 with Destination

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

the class ClientMessageEventListener method handleCreateSession.

/**
 * Handle a CreateSessionMessage.
 * On errors, we could perhaps send a SessionStatusMessage with STATUS_INVALID before
 * sending the DisconnectMessage... but right now the client will send _us_ a
 * DisconnectMessage in return, and not wait around for our DisconnectMessage.
 * So keep it simple.
 *
 * Defaults in SessionConfig options are, in general, NOT honored.
 * In-JVM client side must promote defaults to the primary map.
 */
private void handleCreateSession(CreateSessionMessage message) {
    SessionConfig in = message.getSessionConfig();
    Destination dest = in.getDestination();
    if (in.verifySignature()) {
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("Signature verified correctly on create session message");
    } else {
        // For now, we do NOT send a SessionStatusMessage - see javadoc above
        int itype = dest.getCertificate().getCertificateType();
        SigType stype = SigType.getByCode(itype);
        if (stype == null || !stype.isAvailable()) {
            _log.error("Client requested unsupported signature type " + itype);
            _runner.disconnectClient("Unsupported signature type " + itype);
        } else if (in.tooOld()) {
            long skew = _context.clock().now() - in.getCreationDate().getTime();
            String msg = "Create session message client clock skew? ";
            if (skew >= 0)
                msg += DataHelper.formatDuration(skew) + " in the past";
            else
                msg += DataHelper.formatDuration(0 - skew) + " in the future";
            _log.error(msg);
            _runner.disconnectClient(msg);
        } else {
            _log.error("Signature verification failed on a create session message");
            _runner.disconnectClient("Invalid signature on CreateSessionMessage");
        }
        return;
    }
    // Auth, since 0.8.2
    Properties inProps = in.getOptions();
    if (!checkAuth(inProps))
        return;
    SessionId id = _runner.getSessionId(dest.calculateHash());
    if (id != null) {
        _runner.disconnectClient("Already have session " + id);
        return;
    }
    // Copy over the whole config structure so we don't later corrupt it on
    // the client side if we change settings or later get a
    // ReconfigureSessionMessage
    SessionConfig cfg = new SessionConfig(dest);
    cfg.setSignature(in.getSignature());
    Properties props = new Properties();
    boolean isPrimary = _runner.getSessionIds().isEmpty();
    if (!isPrimary) {
        // all the primary options, then the overrides from the alias
        SessionConfig pcfg = _runner.getPrimaryConfig();
        if (pcfg != null) {
            props.putAll(pcfg.getOptions());
        } else {
            _log.error("no primary config?");
        }
    }
    props.putAll(inProps);
    cfg.setOptions(props);
    // this sets the session id
    int status = _runner.sessionEstablished(cfg);
    if (status != SessionStatusMessage.STATUS_CREATED) {
        // For now, we do NOT send a SessionStatusMessage - see javadoc above
        if (_log.shouldLog(Log.ERROR))
            _log.error("Session establish failed: code = " + status);
        String msg;
        if (status == SessionStatusMessage.STATUS_INVALID)
            msg = "duplicate destination";
        else if (status == SessionStatusMessage.STATUS_REFUSED)
            msg = "session limit exceeded";
        else
            msg = "unknown error";
        _runner.disconnectClient(msg);
        return;
    }
    // get the new session ID
    id = _runner.getSessionId(dest.calculateHash());
    if (_log.shouldLog(Log.INFO))
        _log.info("Session " + id + " established for " + dest.calculateHash());
    if (isPrimary) {
        sendStatusMessage(id, status);
        startCreateSessionJob(cfg);
    } else {
        SessionConfig pcfg = _runner.getPrimaryConfig();
        if (pcfg != null) {
            ClientTunnelSettings settings = new ClientTunnelSettings(dest.calculateHash());
            settings.readFromProperties(props);
            // addAlias() sends the create lease set msg, so we have to send the SMS first
            sendStatusMessage(id, status);
            boolean ok = _context.tunnelManager().addAlias(dest, settings, pcfg.getDestination());
            if (!ok) {
                _log.error("Add alias failed");
            // FIXME cleanup
            }
        } else {
            _log.error("no primary config?");
            status = SessionStatusMessage.STATUS_INVALID;
            sendStatusMessage(id, status);
        // FIXME cleanup
        }
    }
}
Also used : Destination(net.i2p.data.Destination) ClientTunnelSettings(net.i2p.router.ClientTunnelSettings) SessionConfig(net.i2p.data.i2cp.SessionConfig) Properties(java.util.Properties) SessionId(net.i2p.data.i2cp.SessionId) SigType(net.i2p.crypto.SigType)

Example 57 with Destination

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

the class LookupDestJob method runJob.

public void runJob() {
    if (_name != null) {
        // inline, ignore timeout
        Destination d = getContext().namingService().lookup(_name);
        if (d != null)
            returnDest(d);
        else
            returnFail();
    } else {
        DoneJob done = new DoneJob(getContext());
        getContext().netDb().lookupDestination(_hash, done, _timeout, _fromLocalDest);
    }
}
Also used : Destination(net.i2p.data.Destination)

Example 58 with Destination

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

the class LocalClientMessageEventListener method handleDestLookup.

/**
 *  Look only in current local dests
 */
@Override
protected void handleDestLookup(DestLookupMessage message) {
    Hash h = message.getHash();
    DestReplyMessage msg;
    Destination d = ((LocalClientConnectionRunner) _runner).localLookup(h);
    if (d != null)
        msg = new DestReplyMessage(d);
    else
        msg = new DestReplyMessage(h);
    try {
        _runner.doSend(msg);
    } catch (I2CPMessageException ime) {
        ime.printStackTrace();
    }
}
Also used : I2CPMessageException(net.i2p.data.i2cp.I2CPMessageException) Destination(net.i2p.data.Destination) Hash(net.i2p.data.Hash) DestReplyMessage(net.i2p.data.i2cp.DestReplyMessage)

Example 59 with Destination

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

the class KademliaNetworkDatabaseFacade method processStoreFailure.

/**
 *  If the validate fails, call this
 *  to determine if it was because of unsupported crypto.
 *
 *  If so, this will banlist-forever the router hash or permanently negative cache the dest hash,
 *  and then throw the exception. Otherwise it does nothing.
 *
 *  @throws UnsupportedCryptoException if that's why it failed.
 *  @since 0.9.16
 */
private void processStoreFailure(Hash h, DatabaseEntry entry) throws UnsupportedCryptoException {
    if (entry.getHash().equals(h)) {
        if (entry.getType() == DatabaseEntry.KEY_TYPE_LEASESET) {
            LeaseSet ls = (LeaseSet) entry;
            Destination d = ls.getDestination();
            Certificate c = d.getCertificate();
            if (c.getCertificateType() == Certificate.CERTIFICATE_TYPE_KEY) {
                try {
                    KeyCertificate kc = c.toKeyCertificate();
                    SigType type = kc.getSigType();
                    if (type == null || !type.isAvailable() || type.getBaseAlgorithm() == SigAlgo.RSA) {
                        failPermanently(d);
                        String stype = (type != null) ? type.toString() : Integer.toString(kc.getSigTypeCode());
                        if (_log.shouldLog(Log.WARN))
                            _log.warn("Unsupported sig type " + stype + " for destination " + h);
                        throw new UnsupportedCryptoException("Sig type " + stype);
                    }
                } catch (DataFormatException dfe) {
                }
            }
        } else if (entry.getType() == DatabaseEntry.KEY_TYPE_ROUTERINFO) {
            RouterInfo ri = (RouterInfo) entry;
            RouterIdentity id = ri.getIdentity();
            Certificate c = id.getCertificate();
            if (c.getCertificateType() == Certificate.CERTIFICATE_TYPE_KEY) {
                try {
                    KeyCertificate kc = c.toKeyCertificate();
                    SigType type = kc.getSigType();
                    if (type == null || !type.isAvailable()) {
                        String stype = (type != null) ? type.toString() : Integer.toString(kc.getSigTypeCode());
                        _context.banlist().banlistRouterForever(h, "Unsupported signature type " + stype);
                        if (_log.shouldLog(Log.WARN))
                            _log.warn("Unsupported sig type " + stype + " for router " + h);
                        throw new UnsupportedCryptoException("Sig type " + stype);
                    }
                } catch (DataFormatException dfe) {
                }
            }
        }
    }
    if (_log.shouldLog(Log.WARN))
        _log.warn("Verify fail, cause unknown: " + entry);
}
Also used : LeaseSet(net.i2p.data.LeaseSet) Destination(net.i2p.data.Destination) KeyCertificate(net.i2p.data.KeyCertificate) DataFormatException(net.i2p.data.DataFormatException) RouterInfo(net.i2p.data.router.RouterInfo) RouterIdentity(net.i2p.data.router.RouterIdentity) SigType(net.i2p.crypto.SigType) Certificate(net.i2p.data.Certificate) KeyCertificate(net.i2p.data.KeyCertificate)

Example 60 with Destination

use of net.i2p.data.Destination in project i2p.i2p-bote by i2p.

the class KademliaDHT method findDeleteAuthorizationKey.

@Override
public UniqueId findDeleteAuthorizationKey(Hash dhtKey, Hash verificationHash) throws InterruptedException {
    final Collection<Destination> closeNodes = getClosestNodes(dhtKey);
    log.info("Querying " + closeNodes.size() + " peers with DeletionQueries for Kademlia key " + dhtKey);
    DhtStorageHandler storageHandler = storageHandlers.get(EncryptedEmailPacket.class);
    if (storageHandler instanceof DeletionAwareDhtFolder) {
        DeletionAwareDhtFolder<?> folder = (DeletionAwareDhtFolder<?>) storageHandler;
        UniqueId delAuthorization = folder.getDeleteAuthorization(dhtKey);
        if (delAuthorization != null)
            return delAuthorization;
    } else
        log.error("StorageHandler for EncryptedEmailPackets is not a DeletionAwareDhtFolder!");
    // Send the DeletionQueries
    PacketBatch batch = new PacketBatch();
    for (Destination node : closeNodes) if (// local has already been taken care of
    !localDestination.equals(node))
        batch.putPacket(new DeletionQuery(dhtKey), node);
    sendQueue.send(batch);
    batch.awaitSendCompletion();
    // wait for replies
    batch.awaitFirstReply(RESPONSE_TIMEOUT, TimeUnit.SECONDS);
    log.info(batch.getResponses().size() + " response packets received for deletion query for hash " + dhtKey);
    sendQueue.remove(batch);
    Map<Destination, DataPacket> responses = batch.getResponses();
    for (DataPacket response : responses.values()) if (response instanceof DeletionInfoPacket) {
        DeletionInfoPacket delInfo = (DeletionInfoPacket) response;
        DeletionRecord delRecord = delInfo.getEntry(dhtKey);
        if (delRecord != null) {
            boolean valid = Util.isDeleteAuthorizationValid(verificationHash, delRecord.delAuthorization);
            if (valid)
                return delRecord.delAuthorization;
        }
    }
    return null;
}
Also used : Destination(net.i2p.data.Destination) UniqueId(i2p.bote.UniqueId) DeletionQuery(i2p.bote.packet.dht.DeletionQuery) DeletionInfoPacket(i2p.bote.packet.dht.DeletionInfoPacket) DeletionRecord(i2p.bote.packet.dht.DeletionRecord) DhtStorageHandler(i2p.bote.network.DhtStorageHandler) PacketBatch(i2p.bote.network.PacketBatch) DataPacket(i2p.bote.packet.DataPacket) DeletionAwareDhtFolder(i2p.bote.folder.DeletionAwareDhtFolder)

Aggregations

Destination (net.i2p.data.Destination)149 IOException (java.io.IOException)46 DataFormatException (net.i2p.data.DataFormatException)33 Properties (java.util.Properties)29 I2PException (net.i2p.I2PException)26 Hash (net.i2p.data.Hash)18 ArrayList (java.util.ArrayList)13 File (java.io.File)12 I2PSessionException (net.i2p.client.I2PSessionException)12 SigType (net.i2p.crypto.SigType)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 I2PSession (net.i2p.client.I2PSession)10 I2PSocket (net.i2p.client.streaming.I2PSocket)10 FileInputStream (java.io.FileInputStream)7 InputStream (java.io.InputStream)7 OutputStream (java.io.OutputStream)7 I2PClient (net.i2p.client.I2PClient)7 I2PSocketOptions (net.i2p.client.streaming.I2PSocketOptions)7 Test (org.junit.Test)6