Search in sources :

Example 66 with SessionKey

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

the class MessageWrapper method wrap.

/**
 *  Garlic wrap a message from a client or this router, destined for a router,
 *  to hide the contents from the OBEP.
 *  Caller must call acked() or fail() on the returned object.
 *
 *  @param from must be a local client with a session key manager,
 *              or null to use the router's session key manager
 *  @return null on encrypt failure
 */
static WrappedMessage wrap(RouterContext ctx, I2NPMessage m, Hash from, RouterInfo to) {
    PayloadGarlicConfig payload = new PayloadGarlicConfig();
    payload.setCertificate(Certificate.NULL_CERT);
    payload.setId(ctx.random().nextLong(I2NPMessage.MAX_ID_VALUE));
    payload.setPayload(m);
    payload.setRecipient(to);
    payload.setDeliveryInstructions(DeliveryInstructions.LOCAL);
    payload.setExpiration(m.getMessageExpiration());
    SessionKeyManager skm;
    if (from != null)
        skm = ctx.clientManager().getClientSessionKeyManager(from);
    else
        skm = ctx.sessionKeyManager();
    if (skm == null)
        return null;
    SessionKey sentKey = new SessionKey();
    Set<SessionTag> sentTags = new HashSet<SessionTag>();
    GarlicMessage msg = GarlicMessageBuilder.buildMessage(ctx, payload, sentKey, sentTags, NETDB_TAGS_TO_DELIVER, NETDB_LOW_THRESHOLD, skm);
    if (msg == null)
        return null;
    TagSetHandle tsh = null;
    PublicKey sentTo = to.getIdentity().getPublicKey();
    if (!sentTags.isEmpty())
        tsh = skm.tagsDelivered(sentTo, sentKey, sentTags);
    // _log.debug("Sent to: " + to.getIdentity().getHash() + " with key: " + sentKey + " and tags: " + sentTags.size());
    return new WrappedMessage(msg, skm, sentTo, sentKey, tsh);
}
Also used : PayloadGarlicConfig(net.i2p.router.message.PayloadGarlicConfig) SessionKey(net.i2p.data.SessionKey) PublicKey(net.i2p.data.PublicKey) SessionKeyManager(net.i2p.crypto.SessionKeyManager) GarlicMessage(net.i2p.data.i2np.GarlicMessage) SessionTag(net.i2p.data.SessionTag) HashSet(java.util.HashSet) TagSetHandle(net.i2p.crypto.TagSetHandle)

Example 67 with SessionKey

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

the class MessageWrapper method generateSession.

/**
 *  Create a single key and tag, for receiving a single encrypted message,
 *  and register it with the given session key manager, to expire in two minutes.
 *  The recipient can then send us an AES-encrypted message,
 *  avoiding ElGamal.
 *
 *  @return non-null
 *  @since 0.9.9
 */
public static OneTimeSession generateSession(RouterContext ctx, SessionKeyManager skm) {
    SessionKey key = ctx.keyGenerator().generateSessionKey();
    SessionTag tag = new SessionTag(true);
    Set<SessionTag> tags = new RemovableSingletonSet<SessionTag>(tag);
    skm.tagsReceived(key, tags, 2 * 60 * 1000);
    return new OneTimeSession(key, tag);
}
Also used : SessionKey(net.i2p.data.SessionKey) RemovableSingletonSet(net.i2p.router.util.RemovableSingletonSet) SessionTag(net.i2p.data.SessionTag)

Example 68 with SessionKey

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

the class GarlicMessageBuilder method buildMessage.

/**
 * Unused and probably a bad idea.
 *
 * Used below only on a recursive call if the garlic message contains a garlic message.
 * We don't need the SessionKey or SesssionTags returned
 * This uses the router's SKM, which is probably not what you want.
 * This isn't fully implemented, because the key and tags aren't saved - maybe
 * it should force elGamal?
 *
 * @param ctx scope
 * @param config how/what to wrap
 * @throws IllegalArgumentException on error
 */
private static GarlicMessage buildMessage(RouterContext ctx, GarlicConfig config) {
    Log log = ctx.logManager().getLog(GarlicMessageBuilder.class);
    log.error("buildMessage 2 args, using router SKM", new Exception("who did it"));
    return buildMessage(ctx, config, new SessionKey(), new HashSet<SessionTag>(), ctx.sessionKeyManager());
}
Also used : Log(net.i2p.util.Log) SessionKey(net.i2p.data.SessionKey) SessionTag(net.i2p.data.SessionTag) DataFormatException(net.i2p.data.DataFormatException) IOException(java.io.IOException)

Example 69 with SessionKey

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

the class EncryptedInputStream method readInputStream.

/**
 * If <code>cachedKey</code> is not <code>null</code>, this method assumes the
 * key has been generated from a valid password.
 * @param inputStream
 * @param password
 * @param cachedKey
 * @return the decrypted data
 * @throws IOException
 * @throws GeneralSecurityException
 * @throws PasswordException
 */
// for net.i2p.crypto.AESEngine
@SuppressWarnings("deprecation")
private byte[] readInputStream(InputStream inputStream, byte[] password, DerivedKey cachedKey) throws IOException, GeneralSecurityException, PasswordException {
    byte[] startOfFile = new byte[START_OF_FILE.length];
    inputStream.read(startOfFile);
    if (!Arrays.equals(START_OF_FILE, startOfFile))
        throw new IOException("Invalid header bytes: " + Arrays.toString(startOfFile) + ", expected: " + Arrays.toString(START_OF_FILE));
    int format = inputStream.read();
    if (format != FORMAT_VERSION)
        throw new IOException("Invalid file format identifier: " + format + ", expected: " + FORMAT_VERSION);
    SCryptParameters scryptParams = new SCryptParameters(inputStream);
    byte[] salt = new byte[SALT_LENGTH];
    inputStream.read(salt);
    // use the cached key if it is suitable, otherwise compute the key
    byte[] keyBytes;
    if (cachedKey != null && Arrays.equals(salt, cachedKey.salt) && scryptParams.equals(cachedKey.scryptParams))
        keyBytes = cachedKey.key;
    else
        keyBytes = FileEncryptionUtil.getEncryptionKey(password, salt, scryptParams);
    byte[] iv = new byte[BLOCK_SIZE];
    inputStream.read(iv);
    byte[] encryptedData = Util.readBytes(inputStream);
    SessionKey key = new SessionKey(keyBytes);
    I2PAppContext appContext = I2PAppContext.getGlobalContext();
    byte[] decryptedData = appContext.aes().safeDecrypt(encryptedData, key, iv);
    // null from safeDecrypt() means failure
    if (decryptedData == null)
        if (cachedKey == null)
            throw new PasswordException();
        else
            // we're assuming password and key are correct.
            throw new GeneralSecurityException("Can't decrypt using cached key.");
    return decryptedData;
}
Also used : I2PAppContext(net.i2p.I2PAppContext) SessionKey(net.i2p.data.SessionKey) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException)

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