Search in sources :

Example 6 with PublicKey

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

the class ElGamalTest method testRoundTrip.

public void testRoundTrip() {
    Object[] keys = KeyGenerator.getInstance().generatePKIKeypair();
    PublicKey pubKey = (PublicKey) keys[0];
    PrivateKey privKey = (PrivateKey) keys[1];
    String msg = "Hello world";
    Set toBeDelivered = new HashSet();
    SessionKey key = _context.sessionKeyManager().getCurrentKey(pubKey);
    if (key == null)
        key = _context.sessionKeyManager().createSession(pubKey);
    byte[] encrypted = _context.elGamalAESEngine().encrypt(DataHelper.getASCII(msg), pubKey, key, null, null, 64);
    byte[] decrypted = null;
    try {
        decrypted = _context.elGamalAESEngine().decrypt(encrypted, privKey, _context.sessionKeyManager());
    } catch (DataFormatException dfe) {
        dfe.printStackTrace();
        fail();
    }
    assertNotNull(decrypted);
    String read = new String(decrypted);
    assertEquals(msg, read);
}
Also used : PrivateKey(net.i2p.data.PrivateKey) Set(java.util.Set) HashSet(java.util.HashSet) DataFormatException(net.i2p.data.DataFormatException) PublicKey(net.i2p.data.PublicKey) SessionKey(net.i2p.data.SessionKey) HashSet(java.util.HashSet)

Example 7 with PublicKey

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

the class ElGamalTest method testVerifyCompatability.

public void testVerifyCompatability() {
    PublicKey pub = new PublicKey();
    PrivateKey priv = new PrivateKey();
    try {
        pub.fromBase64(PUBLIC_KEY);
        priv.fromBase64(PRIVATE_KEY);
    } catch (DataFormatException dfe) {
        dfe.printStackTrace();
        fail();
    }
    for (int i = 0; i < ENCRYPTED.length; i++) {
        byte[] enc = Base64.decode(ENCRYPTED[i]);
        byte[] decrypted = _context.elGamalEngine().decrypt(enc, priv);
        assertTrue(DataHelper.eq(decrypted, DataHelper.getASCII(UNENCRYPTED[i])));
    }
}
Also used : PrivateKey(net.i2p.data.PrivateKey) DataFormatException(net.i2p.data.DataFormatException) PublicKey(net.i2p.data.PublicKey)

Example 8 with PublicKey

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

the class RouterIdentityTest method testNullCert.

@Test
public void testNullCert() throws Exception {
    RouterIdentity ident = new RouterIdentity();
    ident.setCertificate(null);
    PublicKey pk = (PublicKey) (new PublicKeyTest()).createDataStructure();
    ident.setPublicKey(pk);
    SigningPublicKey k = (SigningPublicKey) (new SigningPublicKeyTest()).createDataStructure();
    ident.setSigningPublicKey(k);
    exception.expect(DataFormatException.class);
    exception.expectMessage("Not enough data to format the router identity");
    ident.writeBytes(new ByteArrayOutputStream());
}
Also used : SigningPublicKey(net.i2p.data.SigningPublicKey) SigningPublicKeyTest(net.i2p.data.SigningPublicKeyTest) SigningPublicKey(net.i2p.data.SigningPublicKey) PublicKey(net.i2p.data.PublicKey) PublicKeyTest(net.i2p.data.PublicKeyTest) SigningPublicKeyTest(net.i2p.data.SigningPublicKeyTest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StructureTest(net.i2p.data.StructureTest) CertificateTest(net.i2p.data.CertificateTest) Test(org.junit.Test) PublicKeyTest(net.i2p.data.PublicKeyTest) SigningPublicKeyTest(net.i2p.data.SigningPublicKeyTest)

Example 9 with PublicKey

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

the class RouterIdentityTest method testNullSigningKey.

@Test
public void testNullSigningKey() throws Exception {
    RouterIdentity ident = new RouterIdentity();
    Certificate cert = (Certificate) (new CertificateTest()).createDataStructure();
    ident.setCertificate(cert);
    PublicKey pk = (PublicKey) (new PublicKeyTest()).createDataStructure();
    ident.setPublicKey(pk);
    ident.setSigningPublicKey(null);
    exception.expect(DataFormatException.class);
    exception.expectMessage("Not enough data to format the router identity");
    ident.writeBytes(new ByteArrayOutputStream());
}
Also used : SigningPublicKey(net.i2p.data.SigningPublicKey) PublicKey(net.i2p.data.PublicKey) PublicKeyTest(net.i2p.data.PublicKeyTest) SigningPublicKeyTest(net.i2p.data.SigningPublicKeyTest) CertificateTest(net.i2p.data.CertificateTest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Certificate(net.i2p.data.Certificate) StructureTest(net.i2p.data.StructureTest) CertificateTest(net.i2p.data.CertificateTest) Test(org.junit.Test) PublicKeyTest(net.i2p.data.PublicKeyTest) SigningPublicKeyTest(net.i2p.data.SigningPublicKeyTest)

Example 10 with PublicKey

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

the class BuildRequestor method createTunnelBuildMessage.

/**
 * @since 0.7.12
 */
/**
 **
 *we can assume everybody supports variable now...
 *keep this here for the next time we change the build protocol
 *    private static boolean supportsVariable(RouterContext ctx, Hash h) {
 *        RouterInfo ri = ctx.netDb().lookupRouterInfoLocally(h);
 *        if (ri == null)
 *            return false;
 *        String v = ri.getVersion();
 *        return VersionComparator.comp(v, MIN_VARIABLE_VERSION) >= 0;
 *    }
 ***
 */
/**
 *  If the tunnel is short enough, and everybody in the tunnel, and the
 *  OBEP or IBGW for the paired tunnel, all support the new variable-sized tunnel build message,
 *  then use that, otherwise the old 8-entry version.
 *  @return null on error
 */
private static TunnelBuildMessage createTunnelBuildMessage(RouterContext ctx, TunnelPool pool, PooledTunnelCreatorConfig cfg, TunnelInfo pairedTunnel, BuildExecutor exec) {
    Log log = ctx.logManager().getLog(BuildRequestor.class);
    long replyTunnel = 0;
    Hash replyRouter;
    boolean useVariable = SEND_VARIABLE && cfg.getLength() <= MEDIUM_RECORDS;
    if (cfg.isInbound()) {
        // replyTunnel = 0; // as above
        replyRouter = ctx.routerHash();
    /**
     **
     *we can assume everybody supports variable now...
     *keep this here for the next time we change the build protocol
     *            if (useVariable) {
     *                // check the reply OBEP and all the tunnel peers except ourselves
     *                if (!supportsVariable(ctx, pairedTunnel.getPeer(pairedTunnel.getLength() - 1))) {
     *                    useVariable = false;
     *                } else {
     *                    for (int i = 0; i < cfg.getLength() - 1; i++) {
     *                        if (!supportsVariable(ctx, cfg.getPeer(i))) {
     *                            useVariable = false;
     *                            break;
     *                        }
     *                    }
     *                }
     *            }
     ***
     */
    } else {
        replyTunnel = pairedTunnel.getReceiveTunnelId(0).getTunnelId();
        replyRouter = pairedTunnel.getPeer(0);
    /**
     **
     *we can assume everybody supports variable now
     *keep this here for the next time we change the build protocol
     *            if (useVariable) {
     *                // check the reply IBGW and all the tunnel peers except ourselves
     *                if (!supportsVariable(ctx, replyRouter)) {
     *                    useVariable = false;
     *                } else {
     *                    for (int i = 1; i < cfg.getLength() - 1; i++) {
     *                        if (!supportsVariable(ctx, cfg.getPeer(i))) {
     *                            useVariable = false;
     *                            break;
     *                        }
     *                    }
     *                }
     *            }
     ***
     */
    }
    // populate and encrypt the message
    TunnelBuildMessage msg;
    List<Integer> order;
    if (useVariable) {
        if (cfg.getLength() <= SHORT_RECORDS) {
            msg = new VariableTunnelBuildMessage(ctx, SHORT_RECORDS);
            order = new ArrayList<Integer>(SHORT_ORDER);
        } else {
            msg = new VariableTunnelBuildMessage(ctx, MEDIUM_RECORDS);
            order = new ArrayList<Integer>(MEDIUM_ORDER);
        }
    } else {
        msg = new TunnelBuildMessage(ctx);
        order = new ArrayList<Integer>(ORDER);
    }
    // This is in BuildExecutor.buildTunnel() now
    // long replyMessageId = ctx.random().nextLong(I2NPMessage.MAX_ID_VALUE);
    // cfg.setReplyMessageId(replyMessageId);
    // randomized placement within the message
    Collections.shuffle(order, ctx.random());
    cfg.setReplyOrder(order);
    if (log.shouldLog(Log.DEBUG))
        log.debug("Build order: " + order + " for " + cfg);
    for (int i = 0; i < msg.getRecordCount(); i++) {
        int hop = order.get(i).intValue();
        PublicKey key = null;
        if (BuildMessageGenerator.isBlank(cfg, hop)) {
        // erm, blank
        } else {
            Hash peer = cfg.getPeer(hop);
            RouterInfo peerInfo = ctx.netDb().lookupRouterInfoLocally(peer);
            if (peerInfo == null) {
                if (log.shouldLog(Log.WARN))
                    log.warn("Peer selected for hop " + i + "/" + hop + " was not found locally: " + peer + " for " + cfg);
                return null;
            } else {
                key = peerInfo.getIdentity().getPublicKey();
            }
        }
        if (log.shouldLog(Log.DEBUG))
            log.debug(cfg.getReplyMessageId() + ": record " + i + "/" + hop + " has key " + key);
        BuildMessageGenerator.createRecord(i, hop, msg, cfg, replyRouter, replyTunnel, ctx, key);
    }
    BuildMessageGenerator.layeredEncrypt(ctx, msg, cfg, order);
    return msg;
}
Also used : Log(net.i2p.util.Log) PublicKey(net.i2p.data.PublicKey) RouterInfo(net.i2p.data.router.RouterInfo) VariableTunnelBuildMessage(net.i2p.data.i2np.VariableTunnelBuildMessage) TunnelBuildMessage(net.i2p.data.i2np.TunnelBuildMessage) VariableTunnelBuildMessage(net.i2p.data.i2np.VariableTunnelBuildMessage) Hash(net.i2p.data.Hash)

Aggregations

PublicKey (net.i2p.data.PublicKey)36 PrivateKey (net.i2p.data.PrivateKey)23 SessionKey (net.i2p.data.SessionKey)14 SigningPublicKey (net.i2p.data.SigningPublicKey)13 DataFormatException (net.i2p.data.DataFormatException)8 SigningPrivateKey (net.i2p.data.SigningPrivateKey)8 HashSet (java.util.HashSet)7 SessionKeyManager (net.i2p.crypto.SessionKeyManager)7 SessionTag (net.i2p.data.SessionTag)7 IOException (java.io.IOException)6 SimpleDataStructure (net.i2p.data.SimpleDataStructure)6 Certificate (net.i2p.data.Certificate)5 Hash (net.i2p.data.Hash)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 GeneralSecurityException (java.security.GeneralSecurityException)4 SigType (net.i2p.crypto.SigType)4 TagSetHandle (net.i2p.crypto.TagSetHandle)4 CertificateTest (net.i2p.data.CertificateTest)4 PublicKeyTest (net.i2p.data.PublicKeyTest)4 SigningPublicKeyTest (net.i2p.data.SigningPublicKeyTest)4