Search in sources :

Example 26 with SessionKey

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

the class MessageWrapper method wrap.

/**
 *  Garlic wrap a message from nobody, destined for a router,
 *  to hide the contents from the OBEP.
 *  Forces ElGamal.
 *
 *  @return null on encrypt failure
 *  @since 0.9.5
 */
static GarlicMessage wrap(RouterContext ctx, I2NPMessage m, 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());
    SessionKey sentKey = ctx.keyGenerator().generateSessionKey();
    PublicKey key = to.getIdentity().getPublicKey();
    GarlicMessage msg = GarlicMessageBuilder.buildMessage(ctx, payload, null, null, key, sentKey, null);
    return msg;
}
Also used : PayloadGarlicConfig(net.i2p.router.message.PayloadGarlicConfig) SessionKey(net.i2p.data.SessionKey) PublicKey(net.i2p.data.PublicKey) GarlicMessage(net.i2p.data.i2np.GarlicMessage)

Example 27 with SessionKey

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

the class Util method encrypt.

/**
 * Encrypts data with an I2P public key
 */
public static byte[] encrypt(byte[] data, PublicKey key) {
    I2PAppContext appContext = I2PAppContext.getGlobalContext();
    SessionKeyManager sessionKeyMgr = new net.i2p.crypto.SessionKeyManager(appContext) {
    };
    SessionKey sessionKey = sessionKeyMgr.createSession(key);
    return appContext.elGamalAESEngine().encrypt(data, key, sessionKey, null, null, null, 0);
}
Also used : I2PAppContext(net.i2p.I2PAppContext) SessionKey(net.i2p.data.SessionKey) SessionKeyManager(net.i2p.crypto.SessionKeyManager)

Example 28 with SessionKey

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

the class EncryptedOutputStream method encryptAndWrite.

/**
 * Writes the header, then encrypts the internal buffer and writes the encrypted
 * data to the underlying <code>OutputStream</code>.
 * @throws IOException
 */
// for net.i2p.crypto.AESEngine
@SuppressWarnings("deprecation")
private void encryptAndWrite() throws IOException {
    downstream.write(START_OF_FILE);
    downstream.write(FORMAT_VERSION);
    FileEncryptionConstants.KDF_PARAMETERS.writeTo(downstream);
    downstream.write(derivedKey.salt);
    byte[] iv = new byte[BLOCK_SIZE];
    I2PAppContext appContext = I2PAppContext.getGlobalContext();
    appContext.random().nextBytes(iv);
    downstream.write(iv);
    byte[] data = outputBuffer.toByteArray();
    SessionKey key = new SessionKey(derivedKey.key);
    byte[] encryptedData = appContext.aes().safeEncrypt(data, key, iv, 0);
    downstream.write(encryptedData);
}
Also used : I2PAppContext(net.i2p.I2PAppContext) SessionKey(net.i2p.data.SessionKey)

Example 29 with SessionKey

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

the class ConfigKeyringHandler method processForm.

@Override
protected void processForm() {
    if (_action == null)
        return;
    boolean adding = _action.equals(_t("Add key"));
    if (adding || _action.equals(_t("Delete key"))) {
        if (_peer == null)
            addFormError(_t("You must enter a destination"));
        if (_key == null && adding)
            addFormError(_t("You must enter a key"));
        if (_peer == null || (_key == null && adding))
            return;
        Hash h = ConvertToHash.getHash(_peer);
        if (adding) {
            SessionKey sk = new SessionKey();
            try {
                sk.fromBase64(_key);
            } catch (DataFormatException dfe) {
            }
            if (h == null || h.getData() == null) {
                addFormError(_t("Invalid destination"));
            } else if (_context.clientManager().isLocal(h)) {
                // don't bother translating
                addFormError("Cannot add key for local destination. Enable encryption in the Hidden Services Manager.");
            } else if (sk.getData() == null) {
                addFormError(_t("Invalid key"));
            } else {
                _context.keyRing().put(h, sk);
                addFormNotice(_t("Key for {0} added to keyring", h.toBase32()));
            }
        } else {
            // Delete
            if (h != null && h.getData() != null) {
                if (_context.clientManager().isLocal(h)) {
                    // don't bother translating
                    addFormError("Cannot remove key for local destination. Disable encryption in the Hidden Services Manager.");
                } else if (_context.keyRing().remove(h) != null) {
                    addFormNotice(_t("Key for {0} removed from keyring", h.toBase32()));
                } else {
                    addFormNotice(_t("Key for {0} not found in keyring", h.toBase32()));
                }
            } else {
                addFormError(_t("Invalid destination"));
            }
        }
    } else {
    // addFormError(_t("Unsupported"));
    }
}
Also used : DataFormatException(net.i2p.data.DataFormatException) SessionKey(net.i2p.data.SessionKey) Hash(net.i2p.data.Hash) ConvertToHash(net.i2p.util.ConvertToHash)

Example 30 with SessionKey

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

the class SessionKeyManager method createSession.

/**
 * Generate a new session key and associate it with the specified target.
 *
 * Racy if called after getCurrentKey() to check for a current session;
 * use getCurrentOrNewKey() in that case.
 */
public SessionKey createSession(PublicKey target) {
    SessionKey key = KeyGenerator.getInstance().generateSessionKey();
    createSession(target, key);
    return key;
}
Also used : SessionKey(net.i2p.data.SessionKey)

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