Search in sources :

Example 1 with RouterPrivateKeyFile

use of net.i2p.data.router.RouterPrivateKeyFile 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)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 DataFormatException (net.i2p.data.DataFormatException)1 PrivateKey (net.i2p.data.PrivateKey)1 PublicKey (net.i2p.data.PublicKey)1 SigningPrivateKey (net.i2p.data.SigningPrivateKey)1 SigningPublicKey (net.i2p.data.SigningPublicKey)1 RouterIdentity (net.i2p.data.router.RouterIdentity)1 RouterPrivateKeyFile (net.i2p.data.router.RouterPrivateKeyFile)1