Search in sources :

Example 16 with PrivateKey

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

the class CreateLeaseSetMessage method doReadMessage.

@Override
protected void doReadMessage(InputStream in, int size) throws I2CPMessageException, IOException {
    try {
        _sessionId = new SessionId();
        _sessionId.readBytes(in);
        // Revocation is unimplemented.
        // As the SPK comes before the LeaseSet, we don't know the key type.
        // We could have some sort of callback or state setting so we get the
        // expected type from the session. But for now, we just assume it's 20 bytes.
        // Clients outside router context should throw in a dummy 20 bytes.
        _signingPrivateKey = new SigningPrivateKey();
        _signingPrivateKey.readBytes(in);
        _privateKey = new PrivateKey();
        _privateKey.readBytes(in);
        _leaseSet = new LeaseSet();
        _leaseSet.readBytes(in);
    } catch (DataFormatException dfe) {
        throw new I2CPMessageException("Error reading the CreateLeaseSetMessage", dfe);
    }
}
Also used : SigningPrivateKey(net.i2p.data.SigningPrivateKey) LeaseSet(net.i2p.data.LeaseSet) SigningPrivateKey(net.i2p.data.SigningPrivateKey) PrivateKey(net.i2p.data.PrivateKey) DataFormatException(net.i2p.data.DataFormatException)

Example 17 with PrivateKey

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

the class TunnelController method createAltPrivateKey.

/**
 * Creates alternate Destination with the same encryption keys as the primary Destination,
 * but a different signing key.
 *
 * Must have already called createPrivateKey() successfully.
 * Does nothing unless option OPT_ALT_PKF is set with the privkey file name.
 * Does nothing if the file already exists.
 *
 * @return success
 * @since 0.9.30
 */
private boolean createAltPrivateKey() {
    if (PREFERRED_SIGTYPE == SigType.DSA_SHA1)
        return false;
    File keyFile = getPrivateKeyFile();
    if (keyFile == null)
        return false;
    if (!keyFile.exists())
        return false;
    File altFile = getAlternatePrivateKeyFile();
    if (altFile == null)
        return false;
    if (altFile.equals(keyFile))
        return false;
    if (altFile.exists())
        return true;
    PrivateKeyFile pkf = new PrivateKeyFile(keyFile);
    FileOutputStream out = null;
    try {
        Destination dest = pkf.getDestination();
        if (dest == null)
            return false;
        if (dest.getSigType() != SigType.DSA_SHA1)
            return false;
        PublicKey pub = dest.getPublicKey();
        PrivateKey priv = pkf.getPrivKey();
        SimpleDataStructure[] signingKeys = KeyGenerator.getInstance().generateSigningKeys(PREFERRED_SIGTYPE);
        SigningPublicKey signingPubKey = (SigningPublicKey) signingKeys[0];
        SigningPrivateKey signingPrivKey = (SigningPrivateKey) signingKeys[1];
        KeyCertificate cert = new KeyCertificate(signingPubKey);
        Destination d = new Destination();
        d.setPublicKey(pub);
        d.setSigningPublicKey(signingPubKey);
        d.setCertificate(cert);
        int len = signingPubKey.length();
        if (len < 128) {
            byte[] pad = new byte[128 - len];
            RandomSource.getInstance().nextBytes(pad);
            d.setPadding(pad);
        } else if (len > 128) {
        // copy of excess data handled in KeyCertificate constructor
        }
        out = new SecureFileOutputStream(altFile);
        d.writeBytes(out);
        priv.writeBytes(out);
        signingPrivKey.writeBytes(out);
        try {
            out.close();
        } catch (IOException ioe) {
        }
        String destStr = d.toBase64();
        log("Alternate private key created and saved in " + altFile.getAbsolutePath());
        log("You should backup this file in a secure place.");
        log("New alternate destination: " + destStr);
        String b32 = d.toBase32();
        log("Base32: " + b32);
        File backupDir = new SecureFile(I2PAppContext.getGlobalContext().getConfigDir(), KEY_BACKUP_DIR);
        if (backupDir.isDirectory() || backupDir.mkdir()) {
            String name = b32 + '-' + I2PAppContext.getGlobalContext().clock().now() + ".dat";
            File backup = new File(backupDir, name);
            if (FileUtil.copy(altFile, backup, false, true)) {
                SecureFileOutputStream.setPerms(backup);
                log("Alternate private key backup saved to " + backup.getAbsolutePath());
            }
        }
        return true;
    } catch (GeneralSecurityException e) {
        log("Error creating keys " + e);
        return false;
    } catch (I2PSessionException e) {
        log("Error creating keys " + e);
        return false;
    } catch (I2PException e) {
        log("Error creating keys " + e);
        return false;
    } catch (IOException e) {
        log("Error creating keys " + e);
        return false;
    } catch (RuntimeException e) {
        log("Error creating keys " + e);
        return false;
    } finally {
        if (out != null)
            try {
                out.close();
            } catch (IOException ioe) {
            }
    }
}
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) SecureFile(net.i2p.util.SecureFile) SigningPublicKey(net.i2p.data.SigningPublicKey) PublicKey(net.i2p.data.PublicKey) GeneralSecurityException(java.security.GeneralSecurityException) PrivateKeyFile(net.i2p.data.PrivateKeyFile) IOException(java.io.IOException) SigningPrivateKey(net.i2p.data.SigningPrivateKey) KeyCertificate(net.i2p.data.KeyCertificate) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) FileOutputStream(java.io.FileOutputStream) I2PSessionException(net.i2p.client.I2PSessionException) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) SecureFile(net.i2p.util.SecureFile) PrivateKeyFile(net.i2p.data.PrivateKeyFile) File(java.io.File) SimpleDataStructure(net.i2p.data.SimpleDataStructure)

Example 18 with PrivateKey

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

the class LoadRouterInfoJob method readKeyData.

/**
 *  @param rkf1 in router.keys format, tried second
 *  @param rkf2 in eepPriv.dat format, tried first
 *  @return non-null, throws IOE if neither exisits
 *  @since 0.9.16
 */
public static KeyData readKeyData(File rkf1, File rkf2) throws DataFormatException, IOException {
    RouterIdentity ri;
    PrivateKey privkey;
    SigningPrivateKey signingPrivKey;
    if (rkf2.exists()) {
        RouterPrivateKeyFile pkf = new RouterPrivateKeyFile(rkf2);
        ri = pkf.getRouterIdentity();
        if (!pkf.validateKeyPairs())
            throw new DataFormatException("Key pairs invalid");
        privkey = pkf.getPrivKey();
        signingPrivKey = pkf.getSigningPrivKey();
    } else {
        InputStream fis = null;
        try {
            fis = new BufferedInputStream(new FileInputStream(rkf1));
            privkey = new PrivateKey();
            privkey.readBytes(fis);
            signingPrivKey = new SigningPrivateKey();
            signingPrivKey.readBytes(fis);
            PublicKey pubkey = new PublicKey();
            pubkey.readBytes(fis);
            SigningPublicKey signingPubKey = new SigningPublicKey();
            signingPubKey.readBytes(fis);
            // validate
            try {
                if (!pubkey.equals(KeyGenerator.getPublicKey(privkey)))
                    throw new DataFormatException("Key pairs invalid");
                if (!signingPubKey.equals(KeyGenerator.getSigningPublicKey(signingPrivKey)))
                    throw new DataFormatException("Key pairs invalid");
            } catch (IllegalArgumentException iae) {
                throw new DataFormatException("Key pairs invalid", iae);
            }
            ri = new RouterIdentity();
            ri.setPublicKey(pubkey);
            ri.setSigningPublicKey(signingPubKey);
            ri.setCertificate(Certificate.NULL_CERT);
        } finally {
            if (fis != null)
                try {
                    fis.close();
                } catch (IOException ioe) {
                }
        }
    }
    return new KeyData(ri, privkey, signingPrivKey);
}
Also used : SigningPublicKey(net.i2p.data.SigningPublicKey) PrivateKey(net.i2p.data.PrivateKey) SigningPrivateKey(net.i2p.data.SigningPrivateKey) RouterIdentity(net.i2p.data.router.RouterIdentity) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SigningPublicKey(net.i2p.data.SigningPublicKey) PublicKey(net.i2p.data.PublicKey) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SigningPrivateKey(net.i2p.data.SigningPrivateKey) RouterPrivateKeyFile(net.i2p.data.router.RouterPrivateKeyFile) DataFormatException(net.i2p.data.DataFormatException) BufferedInputStream(java.io.BufferedInputStream)

Example 19 with PrivateKey

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

the class SAMUtils method checkPrivateDestination.

/**
 * Check whether a base64-encoded dest is valid
 *
 * @param dest The base64-encoded destination to be checked
 *
 * @return True if the destination is valid, false otherwise
 */
/**
 **
 *    public static boolean checkDestination(String dest) {
 *        try {
 *            Destination d = new Destination();
 *            d.fromBase64(dest);
 *
 *            return true;
 *        } catch (DataFormatException e) {
 *            return false;
 *        }
 *    }
 ***
 */
/**
 * Check whether a base64-encoded {dest,privkey,signingprivkey} is valid
 *
 * @param dest The base64-encoded destination and keys to be checked (same format as PrivateKeyFile)
 * @return true if valid
 */
public static boolean checkPrivateDestination(String dest) {
    byte[] b = Base64.decode(dest);
    if (b == null || b.length < 663)
        return false;
    ByteArrayInputStream destKeyStream = new ByteArrayInputStream(b);
    try {
        Destination d = Destination.create(destKeyStream);
        new PrivateKey().readBytes(destKeyStream);
        SigningPrivateKey spk = new SigningPrivateKey(d.getSigningPublicKey().getType());
        spk.readBytes(destKeyStream);
    } catch (DataFormatException e) {
        return false;
    } catch (IOException e) {
        return false;
    }
    return destKeyStream.available() == 0;
}
Also used : SigningPrivateKey(net.i2p.data.SigningPrivateKey) Destination(net.i2p.data.Destination) PrivateKey(net.i2p.data.PrivateKey) SigningPrivateKey(net.i2p.data.SigningPrivateKey) DataFormatException(net.i2p.data.DataFormatException) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException)

Example 20 with PrivateKey

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

the class ElGamalTest method testVerifySelf.

public void testVerifySelf() {
    Object[] keypair = _context.keyGenerator().generatePKIKeypair();
    PublicKey pub = (PublicKey) keypair[0];
    PrivateKey priv = (PrivateKey) keypair[1];
    for (int i = 0; i < UNENCRYPTED.length; i++) {
        byte[] orig = DataHelper.getASCII(UNENCRYPTED[i]);
        byte[] encrypted = _context.elGamalEngine().encrypt(orig, pub);
        byte[] decrypted = _context.elGamalEngine().decrypt(encrypted, priv);
        assertTrue(DataHelper.eq(decrypted, orig));
    }
}
Also used : PrivateKey(net.i2p.data.PrivateKey) PublicKey(net.i2p.data.PublicKey)

Aggregations

PrivateKey (net.i2p.data.PrivateKey)28 PublicKey (net.i2p.data.PublicKey)23 DataFormatException (net.i2p.data.DataFormatException)11 SigningPrivateKey (net.i2p.data.SigningPrivateKey)11 SessionKey (net.i2p.data.SessionKey)10 IOException (java.io.IOException)8 SigningPublicKey (net.i2p.data.SigningPublicKey)8 SessionKeyManager (net.i2p.crypto.SessionKeyManager)6 SigType (net.i2p.crypto.SigType)6 SimpleDataStructure (net.i2p.data.SimpleDataStructure)6 HashSet (java.util.HashSet)5 Destination (net.i2p.data.Destination)5 GeneralSecurityException (java.security.GeneralSecurityException)4 SessionTag (net.i2p.data.SessionTag)4 BufferedInputStream (java.io.BufferedInputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 Properties (java.util.Properties)3