Search in sources :

Example 1 with PublicKey

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

the class I2PSocketManagerFull method addSubsession.

/**
 *  For a server, you must call connect() on the returned object.
 *  Connecting the primary session does NOT connect any subsessions.
 *  If the primary session is not connected, connecting a subsession will connect the primary session first.
 *
 *  @return a new subsession, non-null
 *  @param privateKeyStream null for transient, if non-null must have same encryption keys as primary session
 *                          and different signing keys
 *  @param opts subsession options if any, may be null
 *  @since 0.9.21
 */
public I2PSession addSubsession(InputStream privateKeyStream, Properties opts) throws I2PSessionException {
    if (privateKeyStream == null) {
        // We don't actually need the same pubkey in the dest, just in the LS.
        // The dest one is unused. But this is how we find the LS keys
        // to reuse in RequestLeaseSetMessageHandler.
        ByteArrayOutputStream keyStream = new ByteArrayOutputStream(1024);
        try {
            SigType type = getSigType(opts);
            if (type != SigType.DSA_SHA1) {
                // hassle, have to set up the padding and cert, see I2PClientImpl
                throw new I2PSessionException("type " + type + " unsupported");
            }
            PublicKey pub = _session.getMyDestination().getPublicKey();
            PrivateKey priv = _session.getDecryptionKey();
            SimpleDataStructure[] keys = _context.keyGenerator().generateSigningKeys(type);
            pub.writeBytes(keyStream);
            // signing pub
            keys[0].writeBytes(keyStream);
            Certificate.NULL_CERT.writeBytes(keyStream);
            priv.writeBytes(keyStream);
            // signing priv
            keys[1].writeBytes(keyStream);
        } catch (GeneralSecurityException e) {
            throw new I2PSessionException("Error creating keys", e);
        } catch (I2PException e) {
            throw new I2PSessionException("Error creating keys", e);
        } catch (IOException e) {
            throw new I2PSessionException("Error creating keys", e);
        } catch (RuntimeException e) {
            throw new I2PSessionException("Error creating keys", e);
        }
        privateKeyStream = new ByteArrayInputStream(keyStream.toByteArray());
    }
    I2PSession rv = _session.addSubsession(privateKeyStream, opts);
    boolean added = _subsessions.add(rv);
    if (!added) {
        // shouldn't happen
        _session.removeSubsession(rv);
        throw new I2PSessionException("dup");
    }
    ConnectionOptions defaultOptions = new ConnectionOptions(opts);
    int protocol = defaultOptions.getEnforceProtocol() ? I2PSession.PROTO_STREAMING : I2PSession.PROTO_ANY;
    rv.addMuxedSessionListener(_connectionManager.getMessageHandler(), protocol, defaultOptions.getLocalPort());
    if (_log.shouldLog(Log.WARN))
        _log.warn("Added subsession " + rv);
    return rv;
}
Also used : I2PException(net.i2p.I2PException) PrivateKey(net.i2p.data.PrivateKey) PublicKey(net.i2p.data.PublicKey) GeneralSecurityException(java.security.GeneralSecurityException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SigType(net.i2p.crypto.SigType) ByteArrayInputStream(java.io.ByteArrayInputStream) I2PSessionException(net.i2p.client.I2PSessionException) I2PSession(net.i2p.client.I2PSession) SimpleDataStructure(net.i2p.data.SimpleDataStructure)

Example 2 with PublicKey

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

the class I2PClientImpl method createDestination.

/**
 * Create the destination with the given payload and write it out along with
 * the PrivateKey and SigningPrivateKey to the destKeyStream
 *
 * If cert is a KeyCertificate, the signing keypair will be of the specified type.
 * The KeyCertificate data must be .............................
 * The padding if any will be randomized. The extra key data if any will be set in the
 * key cert.
 *
 * Caller must close stream.
 *
 * @param destKeyStream location to write out the destination, PrivateKey, and SigningPrivateKey,
 *                      format is specified in {@link net.i2p.data.PrivateKeyFile PrivateKeyFile}
 */
public Destination createDestination(OutputStream destKeyStream, Certificate cert) throws I2PException, IOException {
    Destination d = new Destination();
    Object[] keypair = KeyGenerator.getInstance().generatePKIKeypair();
    PublicKey publicKey = (PublicKey) keypair[0];
    PrivateKey privateKey = (PrivateKey) keypair[1];
    SimpleDataStructure[] signingKeys;
    if (cert.getCertificateType() == Certificate.CERTIFICATE_TYPE_KEY) {
        KeyCertificate kcert = cert.toKeyCertificate();
        SigType type = kcert.getSigType();
        try {
            signingKeys = KeyGenerator.getInstance().generateSigningKeys(type);
        } catch (GeneralSecurityException gse) {
            throw new I2PException("keygen fail", gse);
        }
    } else {
        signingKeys = KeyGenerator.getInstance().generateSigningKeys();
    }
    SigningPublicKey signingPubKey = (SigningPublicKey) signingKeys[0];
    SigningPrivateKey signingPrivKey = (SigningPrivateKey) signingKeys[1];
    d.setPublicKey(publicKey);
    d.setSigningPublicKey(signingPubKey);
    if (cert.getCertificateType() == Certificate.CERTIFICATE_TYPE_KEY) {
        // fix up key certificate or padding
        KeyCertificate kcert = cert.toKeyCertificate();
        SigType type = kcert.getSigType();
        int len = type.getPubkeyLen();
        if (len < 128) {
            byte[] pad = new byte[128 - len];
            RandomSource.getInstance().nextBytes(pad);
            d.setPadding(pad);
        } else if (len > 128) {
            System.arraycopy(signingPubKey.getData(), 128, kcert.getPayload(), KeyCertificate.HEADER_LENGTH, len - 128);
        }
    }
    d.setCertificate(cert);
    d.writeBytes(destKeyStream);
    privateKey.writeBytes(destKeyStream);
    signingPrivKey.writeBytes(destKeyStream);
    destKeyStream.flush();
    return d;
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) 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) GeneralSecurityException(java.security.GeneralSecurityException) SigType(net.i2p.crypto.SigType) SigningPrivateKey(net.i2p.data.SigningPrivateKey) KeyCertificate(net.i2p.data.KeyCertificate) SimpleDataStructure(net.i2p.data.SimpleDataStructure)

Example 3 with PublicKey

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

the class KeyGenerator method generatePKIKeys.

/**
 *  Same as above but different return type
 *  @since 0.8.7
 */
public SimpleDataStructure[] generatePKIKeys() {
    BigInteger a = new NativeBigInteger(getElGamalExponentSize(), _context.random());
    BigInteger aalpha = CryptoConstants.elgg.modPow(a, CryptoConstants.elgp);
    SimpleDataStructure[] keys = new SimpleDataStructure[2];
    keys[0] = new PublicKey();
    keys[1] = new PrivateKey();
    try {
        keys[0].setData(SigUtil.rectify(aalpha, PublicKey.KEYSIZE_BYTES));
        keys[1].setData(SigUtil.rectify(a, PrivateKey.KEYSIZE_BYTES));
    } catch (InvalidKeyException ike) {
        throw new IllegalArgumentException(ike);
    }
    return keys;
}
Also used : NativeBigInteger(net.i2p.util.NativeBigInteger) PrivateKey(net.i2p.data.PrivateKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) EdDSAPrivateKey(net.i2p.crypto.eddsa.EdDSAPrivateKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) SigningPrivateKey(net.i2p.data.SigningPrivateKey) SigningPublicKey(net.i2p.data.SigningPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) EdDSAPublicKey(net.i2p.crypto.eddsa.EdDSAPublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) PublicKey(net.i2p.data.PublicKey) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger) InvalidKeyException(java.security.InvalidKeyException) SimpleDataStructure(net.i2p.data.SimpleDataStructure)

Example 4 with PublicKey

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

the class ElGamalTest method testElGamalEngine.

public void testElGamalEngine() {
    int numRuns = 10;
    RandomSource.getInstance().nextBoolean();
    I2PAppContext context = I2PAppContext.getGlobalContext();
    for (int i = 0; i < numRuns; i++) {
        Object[] pair = KeyGenerator.getInstance().generatePKIKeypair();
        PublicKey pubkey = (PublicKey) pair[0];
        PrivateKey privkey = (PrivateKey) pair[1];
        byte[] buf = new byte[128];
        RandomSource.getInstance().nextBytes(buf);
        byte[] encr = context.elGamalEngine().encrypt(buf, pubkey);
        byte[] decr = context.elGamalEngine().decrypt(encr, privkey);
        assertTrue(DataHelper.eq(decr, buf));
    }
}
Also used : PrivateKey(net.i2p.data.PrivateKey) I2PAppContext(net.i2p.I2PAppContext) PublicKey(net.i2p.data.PublicKey)

Example 5 with PublicKey

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

the class ElGamalTest method testElGamalAESEngine.

public void testElGamalAESEngine() throws Exception {
    I2PAppContext ctx = I2PAppContext.getGlobalContext();
    ElGamalAESEngine e = new ElGamalAESEngine(ctx);
    Object[] kp = ctx.keyGenerator().generatePKIKeypair();
    PublicKey pubKey = (PublicKey) kp[0];
    PrivateKey privKey = (PrivateKey) kp[1];
    SessionKey sessionKey = ctx.keyGenerator().generateSessionKey();
    for (int i = 0; i < 10; i++) {
        Set<SessionTag> tags = new HashSet<SessionTag>(5);
        if (i == 0) {
            for (int j = 0; j < 5; j++) tags.add(new SessionTag(true));
        }
        byte[] encrypted = e.encrypt(DataHelper.getASCII("blah"), pubKey, sessionKey, tags, null, 1024);
        byte[] decrypted = e.decrypt(encrypted, privKey, _context.sessionKeyManager());
        assertEquals("blah", new String(decrypted));
        ctx.sessionKeyManager().tagsDelivered(pubKey, sessionKey, tags);
    }
}
Also used : PrivateKey(net.i2p.data.PrivateKey) I2PAppContext(net.i2p.I2PAppContext) PublicKey(net.i2p.data.PublicKey) SessionKey(net.i2p.data.SessionKey) SessionTag(net.i2p.data.SessionTag) HashSet(java.util.HashSet)

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