Search in sources :

Example 16 with SigningPrivateKey

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

the class FamilyKeyCrypto method getPrivKey.

/**
 * Get the private key from the keystore
 * @return non-null, throws on all errors
 */
private SigningPrivateKey getPrivKey(File ks) throws GeneralSecurityException {
    String ksPass = _context.getProperty(PROP_KEYSTORE_PASSWORD, KeyStoreUtil.DEFAULT_KEYSTORE_PASSWORD);
    String keyPass = _context.getProperty(PROP_KEY_PASSWORD);
    if (keyPass == null)
        throw new GeneralSecurityException("No key password, set " + PROP_KEY_PASSWORD + " in " + (new File(_context.getConfigDir(), "router.config")).getAbsolutePath());
    try {
        PrivateKey pk = KeyStoreUtil.getPrivateKey(ks, ksPass, _fname, keyPass);
        if (pk == null)
            throw new GeneralSecurityException("Family key not found: " + _fname);
        return SigUtil.fromJavaKey(pk);
    } catch (IOException ioe) {
        throw new GeneralSecurityException("Error loading family key " + _fname, ioe);
    }
}
Also used : SigningPrivateKey(net.i2p.data.SigningPrivateKey) PrivateKey(java.security.PrivateKey) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) File(java.io.File)

Example 17 with SigningPrivateKey

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

the class PublishLocalRouterInfoJob method runJob.

public void runJob() {
    long last = getContext().netDb().getLastRouterInfoPublishTime();
    long now = getContext().clock().now();
    if (last + MIN_PUBLISH_DELAY > now) {
        long delay = getDelay();
        requeue(last + delay - now);
        return;
    }
    RouterInfo oldRI = getContext().router().getRouterInfo();
    if (_log.shouldLog(Log.DEBUG))
        _log.debug("Old routerInfo contains " + oldRI.getAddresses().size() + " addresses and " + oldRI.getOptionsMap().size() + " options");
    try {
        List<RouterAddress> oldAddrs = new ArrayList<RouterAddress>(oldRI.getAddresses());
        List<RouterAddress> newAddrs = getContext().commSystem().createAddresses();
        int count = _runCount.incrementAndGet();
        RouterInfo ri = new RouterInfo(oldRI);
        if (_notFirstTime && (count % 4) != 0 && oldAddrs.size() == newAddrs.size()) {
            // 3 times out of 4, we don't republish if everything is the same...
            // If something changed, including the cost, then publish,
            // otherwise don't.
            String newcaps = getContext().router().getCapabilities();
            boolean different = !oldRI.getCapabilities().equals(newcaps);
            if (!different) {
                Comparator<RouterAddress> comp = new AddrComparator();
                Collections.sort(oldAddrs, comp);
                Collections.sort(newAddrs, comp);
                for (int i = 0; i < oldAddrs.size(); i++) {
                    // deepEquals() includes cost
                    if (!oldAddrs.get(i).deepEquals(newAddrs.get(i))) {
                        different = true;
                        break;
                    }
                }
                if (!different) {
                    if (_log.shouldLog(Log.INFO))
                        _log.info("Not republishing early because costs and caps and addresses are the same");
                    requeue(getDelay());
                    return;
                }
            }
            if (_log.shouldLog(Log.INFO))
                _log.info("Republishing early because addresses or costs or caps have changed -" + " oldCaps: " + oldRI.getCapabilities() + " newCaps: " + newcaps + " old:\n" + oldAddrs + "\nnew:\n" + newAddrs);
        }
        ri.setPublished(getContext().clock().now());
        Properties stats = getContext().statPublisher().publishStatistics();
        ri.setOptions(stats);
        ri.setAddresses(newAddrs);
        SigningPrivateKey key = getContext().keyManager().getSigningPrivateKey();
        if (key == null) {
            _log.log(Log.CRIT, "Internal error - signing private key not known?  rescheduling publish for 30s");
            requeue(30 * 1000);
            return;
        }
        ri.sign(key);
        getContext().router().setRouterInfo(ri);
        if (_log.shouldLog(Log.INFO))
            _log.info("Newly updated routerInfo is published with " + stats.size() + "/" + ri.getOptionsMap().size() + " options on " + new Date(ri.getPublished()));
        try {
            // This won't really publish until the netdb is initialized.
            getContext().netDb().publish(ri);
        } catch (IllegalArgumentException iae) {
            _log.log(Log.CRIT, "Error publishing our identity - corrupt? Restart required", iae);
            getContext().router().rebuildNewIdentity();
        }
    } catch (DataFormatException dfe) {
        _log.error("Error signing the updated local router info!", dfe);
    }
    if (_notFirstTime) {
        requeue(getDelay());
    } else {
        requeue(FIRST_TIME_DELAY);
        _notFirstTime = true;
    }
}
Also used : RouterInfo(net.i2p.data.router.RouterInfo) ArrayList(java.util.ArrayList) RouterAddress(net.i2p.data.router.RouterAddress) Properties(java.util.Properties) Date(java.util.Date) SigningPrivateKey(net.i2p.data.SigningPrivateKey) DataFormatException(net.i2p.data.DataFormatException)

Example 18 with SigningPrivateKey

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

the class Router method locked_rebuildRouterInfo.

/**
 * Rebuild and republish our routerInfo since something significant
 * has changed.
 */
private void locked_rebuildRouterInfo(boolean blockingRebuild) {
    RouterInfo ri;
    if (_routerInfo != null)
        ri = new RouterInfo(_routerInfo);
    else
        ri = new RouterInfo();
    try {
        ri.setPublished(_context.clock().now());
        Properties stats = _context.statPublisher().publishStatistics();
        ri.setOptions(stats);
        // deadlock thru createAddresses() thru SSU REA... move outside lock?
        ri.setAddresses(_context.commSystem().createAddresses());
        SigningPrivateKey key = _context.keyManager().getSigningPrivateKey();
        if (key == null) {
            _log.log(Log.CRIT, "Internal error - signing private key not known? Impossible?");
            return;
        }
        ri.sign(key);
        setRouterInfo(ri);
        if (!ri.isValid())
            throw new DataFormatException("Our RouterInfo has a bad signature");
        Republish r = new Republish(_context);
        if (blockingRebuild)
            r.timeReached();
        else
            _context.simpleTimer2().addEvent(r, 0);
    } catch (DataFormatException dfe) {
        _log.log(Log.CRIT, "Internal error - unable to sign our own address?!", dfe);
    }
}
Also used : SigningPrivateKey(net.i2p.data.SigningPrivateKey) DataFormatException(net.i2p.data.DataFormatException) RouterInfo(net.i2p.data.router.RouterInfo) OrderedProperties(net.i2p.util.OrderedProperties) Properties(java.util.Properties)

Example 19 with SigningPrivateKey

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

the class SigUtil method fromJavaKey.

public static SigningPrivateKey fromJavaKey(ECPrivateKey pk, SigType type) throws GeneralSecurityException {
    BigInteger s = pk.getS();
    int len = type.getPrivkeyLen();
    byte[] bs = rectify(s, len);
    return new SigningPrivateKey(type, bs);
}
Also used : SigningPrivateKey(net.i2p.data.SigningPrivateKey) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger) ECPoint(java.security.spec.ECPoint)

Example 20 with SigningPrivateKey

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

the class TrustedUpdate method genKeysCLI.

/**
 * @return success
 */
private static final boolean genKeysCLI(String publicKeyFile, String privateKeyFile) {
    File pubFile = new File(publicKeyFile);
    File privFile = new File(privateKeyFile);
    if (pubFile.exists()) {
        System.out.println("Error: Not overwriting file " + publicKeyFile);
        return false;
    }
    if (privFile.exists()) {
        System.out.println("Error: Not overwriting file " + privateKeyFile);
        return false;
    }
    FileOutputStream fileOutputStream = null;
    I2PAppContext context = I2PAppContext.getGlobalContext();
    try {
        Object[] signingKeypair = context.keyGenerator().generateSigningKeypair();
        SigningPublicKey signingPublicKey = (SigningPublicKey) signingKeypair[0];
        SigningPrivateKey signingPrivateKey = (SigningPrivateKey) signingKeypair[1];
        fileOutputStream = new SecureFileOutputStream(pubFile);
        signingPublicKey.writeBytes(fileOutputStream);
        fileOutputStream.close();
        fileOutputStream = null;
        fileOutputStream = new SecureFileOutputStream(privFile);
        signingPrivateKey.writeBytes(fileOutputStream);
        System.out.println("\r\nPrivate key written to: " + privateKeyFile);
        System.out.println("Public key written to: " + publicKeyFile);
        System.out.println("\r\nPublic key: " + signingPublicKey.toBase64() + "\r\n");
    } catch (IOException e) {
        System.err.println("Error writing keys:");
        e.printStackTrace();
        return false;
    } catch (DataFormatException e) {
        System.err.println("Error writing keys:");
        e.printStackTrace();
        return false;
    } finally {
        if (fileOutputStream != null)
            try {
                fileOutputStream.close();
            } catch (IOException ioe) {
            }
    }
    return true;
}
Also used : SigningPrivateKey(net.i2p.data.SigningPrivateKey) SigningPublicKey(net.i2p.data.SigningPublicKey) DataFormatException(net.i2p.data.DataFormatException) I2PAppContext(net.i2p.I2PAppContext) FileOutputStream(java.io.FileOutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) IOException(java.io.IOException) File(java.io.File)

Aggregations

SigningPrivateKey (net.i2p.data.SigningPrivateKey)31 SigningPublicKey (net.i2p.data.SigningPublicKey)14 DataFormatException (net.i2p.data.DataFormatException)11 IOException (java.io.IOException)10 PrivateKey (net.i2p.data.PrivateKey)10 GeneralSecurityException (java.security.GeneralSecurityException)8 PublicKey (net.i2p.data.PublicKey)7 File (java.io.File)6 PrivateKey (java.security.PrivateKey)6 SigType (net.i2p.crypto.SigType)6 SimpleDataStructure (net.i2p.data.SimpleDataStructure)6 FileInputStream (java.io.FileInputStream)5 Properties (java.util.Properties)5 Destination (net.i2p.data.Destination)5 Signature (net.i2p.data.Signature)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 BigInteger (java.math.BigInteger)4 RouterInfo (net.i2p.data.router.RouterInfo)4 BufferedInputStream (java.io.BufferedInputStream)3 InputStream (java.io.InputStream)3