Search in sources :

Example 26 with PublicKey

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

the class RouterInfoTest method createDataStructure.

@SuppressWarnings("deprecation")
public DataStructure createDataStructure() throws DataFormatException {
    RouterInfo info = new RouterInfo();
    HashSet<RouterAddress> addresses = new HashSet<RouterAddress>();
    DataStructure structure = (new RouterAddressTest()).createDataStructure();
    addresses.add((RouterAddress) structure);
    info.setAddresses(addresses);
    PublicKey pubKey = null;
    SigningPublicKey signingPubKey = null;
    PrivateKey privKey = null;
    SigningPrivateKey signingPrivKey = null;
    Object[] obj = KeyGenerator.getInstance().generatePKIKeypair();
    pubKey = (PublicKey) obj[0];
    privKey = (PrivateKey) obj[1];
    obj = KeyGenerator.getInstance().generateSigningKeypair();
    signingPubKey = (SigningPublicKey) obj[0];
    signingPrivKey = (SigningPrivateKey) obj[1];
    _log.debug("SigningPublicKey: " + signingPubKey);
    _log.debug("SigningPrivateKey: " + signingPrivKey);
    RouterIdentity ident = new RouterIdentity();
    ident.setCertificate(new Certificate(Certificate.CERTIFICATE_TYPE_NULL, null));
    ident.setPublicKey(pubKey);
    ident.setSigningPublicKey(signingPubKey);
    info.setIdentity(ident);
    Properties options = new Properties();
    for (int i = 0; i < 16; i++) {
        options.setProperty("option." + i, "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890$:." + i);
    }
    options.setProperty("netConnectionSpeed", "OC12");
    info.setOptions(options);
    HashSet<Hash> peers = new HashSet<Hash>();
    structure = (new HashTest()).createDataStructure();
    peers.add((Hash) structure);
    info.setPeers(peers);
    info.setPublished(System.currentTimeMillis());
    // info.setVersion(69);
    info.sign(signingPrivKey);
    return info;
}
Also used : SigningPublicKey(net.i2p.data.SigningPublicKey) PrivateKey(net.i2p.data.PrivateKey) SigningPrivateKey(net.i2p.data.SigningPrivateKey) SigningPublicKey(net.i2p.data.SigningPublicKey) PublicKey(net.i2p.data.PublicKey) DataStructure(net.i2p.data.DataStructure) Properties(java.util.Properties) Hash(net.i2p.data.Hash) HashTest(net.i2p.data.HashTest) SigningPrivateKey(net.i2p.data.SigningPrivateKey) HashSet(java.util.HashSet) Certificate(net.i2p.data.Certificate)

Example 27 with PublicKey

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

the class SessionEncryptionTest method testRekeying.

/**
 *  Run tagsIncluded    useTag  rekey
 *  1   yes (2)         no      no
 *  2   no              yes     no
 *  3   yes (2)         yes     yes
 *  4   no              yes     no
 *  5   no              yes     no
 */
public void testRekeying() throws Exception {
    Object[] keys = KeyGenerator.getInstance().generatePKIKeypair();
    PublicKey pubKey = (PublicKey) keys[0];
    PrivateKey privKey = (PrivateKey) keys[1];
    SessionKeyManager skm = new TransientSessionKeyManager(_context);
    SessionKey curKey = skm.createSession(pubKey);
    SessionKey nextKey = KeyGenerator.getInstance().generateSessionKey();
    SessionTag tag1 = new SessionTag(true);
    SessionTag tag2 = new SessionTag(true);
    SessionTag tag3 = new SessionTag(true);
    SessionTag tag4 = new SessionTag(true);
    HashSet<SessionTag> firstTags = new HashSet<SessionTag>();
    firstTags.add(tag1);
    firstTags.add(tag2);
    HashSet<SessionTag> secondTags = new HashSet<SessionTag>();
    secondTags.add(tag3);
    secondTags.add(tag4);
    byte[] msg1 = DataHelper.getASCII("msg 1");
    byte[] msg2 = DataHelper.getASCII("msg 2");
    byte[] msg3 = DataHelper.getASCII("msg 3");
    byte[] msg4 = DataHelper.getASCII("msg 4");
    byte[] msg5 = DataHelper.getASCII("msg 5");
    byte[] emsg1 = _context.elGamalAESEngine().encrypt(msg1, pubKey, curKey, firstTags, null, 64);
    byte[] dmsg1 = _context.elGamalAESEngine().decrypt(emsg1, privKey, skm);
    assertTrue(DataHelper.eq(dmsg1, msg1));
    TagSetHandle tsh = skm.tagsDelivered(pubKey, curKey, firstTags);
    skm.tagsAcked(pubKey, curKey, tsh);
    curKey = skm.getCurrentKey(pubKey);
    SessionTag curTag = skm.consumeNextAvailableTag(pubKey, curKey);
    assertNotNull(curTag);
    byte[] emsg2 = _context.elGamalAESEngine().encrypt(msg2, pubKey, curKey, null, curTag, 64);
    byte[] dmsg2 = _context.elGamalAESEngine().decrypt(emsg2, privKey, skm);
    assertTrue(DataHelper.eq(dmsg2, msg2));
    curKey = skm.getCurrentKey(pubKey);
    curTag = skm.consumeNextAvailableTag(pubKey, curKey);
    assertNotNull(curTag);
    assertNotNull(curKey);
    byte[] emsg3 = _context.elGamalAESEngine().encrypt(msg3, pubKey, curKey, secondTags, curTag, nextKey, 64);
    byte[] dmsg3 = _context.elGamalAESEngine().decrypt(emsg3, privKey, skm);
    assertTrue(DataHelper.eq(dmsg3, msg3));
    // note nextKey not curKey
    tsh = skm.tagsDelivered(pubKey, nextKey, secondTags);
    skm.tagsAcked(pubKey, nextKey, tsh);
    curKey = skm.getCurrentKey(pubKey);
    curTag = skm.consumeNextAvailableTag(pubKey, curKey);
    assertNotNull(curTag);
    assertNotNull(curKey);
    byte[] emsg4 = _context.elGamalAESEngine().encrypt(msg4, pubKey, curKey, null, curTag, 64);
    byte[] dmsg4 = _context.elGamalAESEngine().decrypt(emsg4, privKey, skm);
    assertTrue(DataHelper.eq(dmsg4, msg4));
    curKey = skm.getCurrentKey(pubKey);
    curTag = skm.consumeNextAvailableTag(pubKey, curKey);
    assertNotNull(curTag);
    assertNotNull(curKey);
    byte[] emsg5 = _context.elGamalAESEngine().encrypt(msg5, pubKey, curKey, null, curTag, 64);
    byte[] dmsg5 = _context.elGamalAESEngine().decrypt(emsg5, privKey, skm);
    assertTrue(DataHelper.eq(dmsg5, msg5));
}
Also used : PrivateKey(net.i2p.data.PrivateKey) PublicKey(net.i2p.data.PublicKey) SessionKey(net.i2p.data.SessionKey) SessionKeyManager(net.i2p.crypto.SessionKeyManager) SessionTag(net.i2p.data.SessionTag) HashSet(java.util.HashSet) TagSetHandle(net.i2p.crypto.TagSetHandle)

Example 28 with PublicKey

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

the class SessionEncryptionTest method testLongSession.

/**
 *  20 tags every 10 messages, rekey every 50
 */
public void testLongSession() throws Exception {
    Object[] keys = KeyGenerator.getInstance().generatePKIKeypair();
    PublicKey pubKey = (PublicKey) keys[0];
    PrivateKey privKey = (PrivateKey) keys[1];
    SessionKeyManager skm = new TransientSessionKeyManager(_context);
    SessionKey curKey = skm.createSession(pubKey);
    for (int i = 0; i < 1000; i++) {
        Set<SessionTag> tags = null;
        SessionKey nextKey = null;
        curKey = skm.getCurrentKey(pubKey);
        SessionTag curTag = skm.consumeNextAvailableTag(pubKey, curKey);
        int availTags = skm.getAvailableTags(pubKey, curKey);
        if ((availTags < 1)) {
            tags = generateNewTags(50);
        }
        if (i % 50 == 0)
            nextKey = KeyGenerator.getInstance().generateSessionKey();
        byte[] msg = DataHelper.getASCII("msg " + i);
        byte[] emsg = _context.elGamalAESEngine().encrypt(msg, pubKey, curKey, tags, curTag, nextKey, 64);
        byte[] dmsg = _context.elGamalAESEngine().decrypt(emsg, privKey, skm);
        assertTrue(DataHelper.eq(dmsg, msg));
        if ((tags != null) && (tags.size() > 0)) {
            if (nextKey == null) {
                TagSetHandle tsh = skm.tagsDelivered(pubKey, curKey, tags);
                skm.tagsAcked(pubKey, curKey, tsh);
            } else {
                TagSetHandle tsh = skm.tagsDelivered(pubKey, nextKey, tags);
                skm.tagsAcked(pubKey, nextKey, tsh);
            }
        }
    }
}
Also used : PrivateKey(net.i2p.data.PrivateKey) PublicKey(net.i2p.data.PublicKey) SessionKey(net.i2p.data.SessionKey) SessionKeyManager(net.i2p.crypto.SessionKeyManager) SessionTag(net.i2p.data.SessionTag) TagSetHandle(net.i2p.crypto.TagSetHandle)

Example 29 with PublicKey

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

the class SessionEncryptionTest method testNoSessions2.

public void testNoSessions2() throws Exception {
    Object[] keys = KeyGenerator.getInstance().generatePKIKeypair();
    PublicKey pubKey = (PublicKey) keys[0];
    PrivateKey privKey = (PrivateKey) keys[1];
    SessionKeyManager skm = new TransientSessionKeyManager(_context);
    SessionKey curKey = skm.createSession(pubKey);
    byte[] msg = DataHelper.getASCII("msg 2");
    byte[] emsg = _context.elGamalAESEngine().encrypt(msg, pubKey, curKey, null, null, 64);
    byte[] dmsg = _context.elGamalAESEngine().decrypt(emsg, privKey, skm);
    assertTrue(DataHelper.eq(dmsg, msg));
}
Also used : PrivateKey(net.i2p.data.PrivateKey) PublicKey(net.i2p.data.PublicKey) SessionKey(net.i2p.data.SessionKey) SessionKeyManager(net.i2p.crypto.SessionKeyManager)

Example 30 with PublicKey

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

the class RouterIdentityTest method createDataStructure.

public DataStructure createDataStructure() throws DataFormatException {
    RouterIdentity ident = new RouterIdentity();
    Certificate cert = (Certificate) (new CertificateTest()).createDataStructure();
    ident.setCertificate(cert);
    PublicKey pk = (PublicKey) (new PublicKeyTest()).createDataStructure();
    ident.setPublicKey(pk);
    SigningPublicKey k = (SigningPublicKey) (new SigningPublicKeyTest()).createDataStructure();
    ident.setSigningPublicKey(k);
    return ident;
}
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) CertificateTest(net.i2p.data.CertificateTest) Certificate(net.i2p.data.Certificate)

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