Search in sources :

Example 1 with Certificate

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

the class IndexBean method modifyDestination.

/**
 * Modify or create a destination
 */
private String modifyDestination() {
    String privKeyFile = _config.getPrivKeyFile();
    if (privKeyFile == null)
        return "Private Key File not specified";
    TunnelController tun = getController(_tunnel);
    Properties config = getConfig();
    if (tun == null) {
        // creating new
        tun = new TunnelController(config, "", true);
        _group.addController(tun);
        saveChanges();
    } else if (tun.getIsRunning() || tun.getIsStarting()) {
        return "Tunnel must be stopped before modifying destination";
    }
    File keyFile = new File(privKeyFile);
    if (!keyFile.isAbsolute())
        keyFile = new File(_context.getConfigDir(), privKeyFile);
    PrivateKeyFile pkf = new PrivateKeyFile(keyFile);
    try {
        pkf.createIfAbsent();
    } catch (I2PException e) {
        return "Create private key file failed: " + e;
    } catch (IOException e) {
        return "Create private key file failed: " + e;
    }
    switch(_certType) {
        case Certificate.CERTIFICATE_TYPE_NULL:
        case Certificate.CERTIFICATE_TYPE_HIDDEN:
            pkf.setCertType(_certType);
            break;
        case Certificate.CERTIFICATE_TYPE_HASHCASH:
            pkf.setHashCashCert(_hashCashValue);
            break;
        case Certificate.CERTIFICATE_TYPE_SIGNED:
            if (_certSigner == null || _certSigner.trim().length() <= 0)
                return "No signing destination specified";
            // find the signer's key file...
            String signerPKF = null;
            for (int i = 0; i < getTunnelCount(); i++) {
                TunnelController c = getController(i);
                if (_certSigner.equals(c.getConfig("").getProperty(TunnelController.PROP_NAME)) || _certSigner.equals(c.getConfig("").getProperty(TunnelController.PROP_SPOOFED_HOST))) {
                    signerPKF = c.getConfig("").getProperty(TunnelController.PROP_FILE);
                    break;
                }
            }
            if (signerPKF == null || signerPKF.length() <= 0)
                return "Signing destination " + _certSigner + " not found";
            if (privKeyFile.equals(signerPKF))
                return "Self-signed destinations not allowed";
            Certificate c = pkf.setSignedCert(new PrivateKeyFile(signerPKF));
            if (c == null)
                return "Signing failed - does signer destination exist?";
            break;
        default:
            return "Unknown certificate type";
    }
    Destination newdest;
    try {
        pkf.write();
        newdest = pkf.getDestination();
    } catch (I2PException e) {
        return "Modification failed: " + e;
    } catch (IOException e) {
        return "Modification failed: " + e;
    }
    return "Destination modified - " + "New Base32 is " + newdest.toBase32() + "New Destination is " + newdest.toBase64();
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) TunnelController(net.i2p.i2ptunnel.TunnelController) PrivateKeyFile(net.i2p.data.PrivateKeyFile) IOException(java.io.IOException) Properties(java.util.Properties) PrivateKeyFile(net.i2p.data.PrivateKeyFile) File(java.io.File) Certificate(net.i2p.data.Certificate)

Example 2 with Certificate

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

the class RouterIdentityTest method testNullSigningKey.

@Test
public void testNullSigningKey() throws Exception {
    RouterIdentity ident = new RouterIdentity();
    Certificate cert = (Certificate) (new CertificateTest()).createDataStructure();
    ident.setCertificate(cert);
    PublicKey pk = (PublicKey) (new PublicKeyTest()).createDataStructure();
    ident.setPublicKey(pk);
    ident.setSigningPublicKey(null);
    exception.expect(DataFormatException.class);
    exception.expectMessage("Not enough data to format the router identity");
    ident.writeBytes(new ByteArrayOutputStream());
}
Also used : 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) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Certificate(net.i2p.data.Certificate) StructureTest(net.i2p.data.StructureTest) CertificateTest(net.i2p.data.CertificateTest) Test(org.junit.Test) PublicKeyTest(net.i2p.data.PublicKeyTest) SigningPublicKeyTest(net.i2p.data.SigningPublicKeyTest)

Example 3 with Certificate

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

the class CreateRouterInfoJob method createRouterInfo.

/**
 *  Writes 6 files: router.info (standard RI format),
 *  router.keys.dat, and 4 individual key files under keyBackup/
 *
 *  router.keys.dat file format: This is the
 *  same "eepPriv.dat" format used by the client code,
 *  as documented in PrivateKeyFile.
 *
 *  Old router.keys file format: Note that this is NOT the
 *  same "eepPriv.dat" format used by the client code.
 *<pre>
 *   - Private key (256 bytes)
 *   - Signing Private key (20 bytes)
 *   - Public key (256 bytes)
 *   - Signing Public key (128 bytes)
 *  Total 660 bytes
 *</pre>
 *
 *  Caller must hold Router.routerInfoFileLock.
 */
RouterInfo createRouterInfo() {
    SigType type = getSigTypeConfig(getContext());
    RouterInfo info = new RouterInfo();
    OutputStream fos1 = null;
    try {
        info.setAddresses(getContext().commSystem().createAddresses());
        // not necessary, in constructor
        // info.setPeers(new HashSet());
        info.setPublished(getCurrentPublishDate(getContext()));
        Object[] keypair = getContext().keyGenerator().generatePKIKeypair();
        PublicKey pubkey = (PublicKey) keypair[0];
        PrivateKey privkey = (PrivateKey) keypair[1];
        SimpleDataStructure[] signingKeypair = getContext().keyGenerator().generateSigningKeys(type);
        SigningPublicKey signingPubKey = (SigningPublicKey) signingKeypair[0];
        SigningPrivateKey signingPrivKey = (SigningPrivateKey) signingKeypair[1];
        RouterIdentity ident = new RouterIdentity();
        Certificate cert = createCertificate(getContext(), signingPubKey);
        ident.setCertificate(cert);
        ident.setPublicKey(pubkey);
        ident.setSigningPublicKey(signingPubKey);
        byte[] padding;
        int padLen = SigningPublicKey.KEYSIZE_BYTES - signingPubKey.length();
        if (padLen > 0) {
            padding = new byte[padLen];
            getContext().random().nextBytes(padding);
            ident.setPadding(padding);
        } else {
            padding = null;
        }
        info.setIdentity(ident);
        Properties stats = getContext().statPublisher().publishStatistics(ident.getHash());
        info.setOptions(stats);
        info.sign(signingPrivKey);
        if (!info.isValid())
            throw new DataFormatException("RouterInfo we just built is invalid: " + info);
        // remove router.keys
        (new File(getContext().getRouterDir(), KEYS_FILENAME)).delete();
        // write router.info
        File ifile = new File(getContext().getRouterDir(), INFO_FILENAME);
        fos1 = new BufferedOutputStream(new SecureFileOutputStream(ifile));
        info.writeBytes(fos1);
        // write router.keys.dat
        File kfile = new File(getContext().getRouterDir(), KEYS2_FILENAME);
        PrivateKeyFile pkf = new PrivateKeyFile(kfile, pubkey, signingPubKey, cert, privkey, signingPrivKey, padding);
        pkf.write();
        // set or overwrite old random keys
        Map<String, String> map = new HashMap<String, String>(2);
        byte[] rk = new byte[32];
        getContext().random().nextBytes(rk);
        map.put(Router.PROP_IB_RANDOM_KEY, Base64.encode(rk));
        getContext().random().nextBytes(rk);
        map.put(Router.PROP_OB_RANDOM_KEY, Base64.encode(rk));
        getContext().router().saveConfig(map, null);
        getContext().keyManager().setKeys(pubkey, privkey, signingPubKey, signingPrivKey);
        if (_log.shouldLog(Log.INFO))
            _log.info("Router info created and stored at " + ifile.getAbsolutePath() + " with private keys stored at " + kfile.getAbsolutePath() + " [" + info + "]");
        getContext().router().eventLog().addEvent(EventLog.REKEYED, ident.calculateHash().toBase64());
    } catch (GeneralSecurityException gse) {
        _log.log(Log.CRIT, "Error building the new router information", gse);
    } catch (DataFormatException dfe) {
        _log.log(Log.CRIT, "Error building the new router information", dfe);
    } catch (IOException ioe) {
        _log.log(Log.CRIT, "Error writing out the new router information", ioe);
    } finally {
        if (fos1 != null)
            try {
                fos1.close();
            } catch (IOException ioe) {
            }
    }
    return info;
}
Also used : PrivateKey(net.i2p.data.PrivateKey) SigningPrivateKey(net.i2p.data.SigningPrivateKey) HashMap(java.util.HashMap) RouterInfo(net.i2p.data.router.RouterInfo) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) Properties(java.util.Properties) SimpleDataStructure(net.i2p.data.SimpleDataStructure) BufferedOutputStream(java.io.BufferedOutputStream) SigningPublicKey(net.i2p.data.SigningPublicKey) SigningPublicKey(net.i2p.data.SigningPublicKey) PublicKey(net.i2p.data.PublicKey) RouterIdentity(net.i2p.data.router.RouterIdentity) GeneralSecurityException(java.security.GeneralSecurityException) PrivateKeyFile(net.i2p.data.PrivateKeyFile) IOException(java.io.IOException) SigType(net.i2p.crypto.SigType) SigningPrivateKey(net.i2p.data.SigningPrivateKey) DataFormatException(net.i2p.data.DataFormatException) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) PrivateKeyFile(net.i2p.data.PrivateKeyFile) File(java.io.File) Certificate(net.i2p.data.Certificate) KeyCertificate(net.i2p.data.KeyCertificate)

Example 4 with Certificate

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

the class BuildTestMessageJob method buildAckClove.

/**
 * Build a clove that sends a DeliveryStatusMessage to us
 */
private PayloadGarlicConfig buildAckClove() {
    PayloadGarlicConfig ackClove = new PayloadGarlicConfig();
    DeliveryInstructions ackInstructions = new DeliveryInstructions();
    ackInstructions.setDeliveryMode(DeliveryInstructions.DELIVERY_MODE_ROUTER);
    // yikes!
    ackInstructions.setRouter(_replyTo);
    DeliveryStatusMessage msg = new DeliveryStatusMessage(getContext());
    msg.setArrival(getContext().clock().now());
    msg.setMessageId(_testMessageKey);
    if (_log.shouldLog(Log.DEBUG))
        _log.debug("Delivery status message key: " + _testMessageKey + " arrival: " + msg.getArrival());
    ackClove.setCertificate(new Certificate(Certificate.CERTIFICATE_TYPE_NULL, null));
    ackClove.setDeliveryInstructions(ackInstructions);
    ackClove.setExpiration(_timeoutMs + getContext().clock().now());
    ackClove.setId(getContext().random().nextLong(I2NPMessage.MAX_ID_VALUE));
    ackClove.setPayload(msg);
    ackClove.setRecipient(_target);
    return ackClove;
}
Also used : DeliveryInstructions(net.i2p.data.i2np.DeliveryInstructions) DeliveryStatusMessage(net.i2p.data.i2np.DeliveryStatusMessage) Certificate(net.i2p.data.Certificate)

Example 5 with Certificate

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

the class KademliaNetworkDatabaseFacade method processStoreFailure.

/**
 *  If the validate fails, call this
 *  to determine if it was because of unsupported crypto.
 *
 *  If so, this will banlist-forever the router hash or permanently negative cache the dest hash,
 *  and then throw the exception. Otherwise it does nothing.
 *
 *  @throws UnsupportedCryptoException if that's why it failed.
 *  @since 0.9.16
 */
private void processStoreFailure(Hash h, DatabaseEntry entry) throws UnsupportedCryptoException {
    if (entry.getHash().equals(h)) {
        if (entry.getType() == DatabaseEntry.KEY_TYPE_LEASESET) {
            LeaseSet ls = (LeaseSet) entry;
            Destination d = ls.getDestination();
            Certificate c = d.getCertificate();
            if (c.getCertificateType() == Certificate.CERTIFICATE_TYPE_KEY) {
                try {
                    KeyCertificate kc = c.toKeyCertificate();
                    SigType type = kc.getSigType();
                    if (type == null || !type.isAvailable() || type.getBaseAlgorithm() == SigAlgo.RSA) {
                        failPermanently(d);
                        String stype = (type != null) ? type.toString() : Integer.toString(kc.getSigTypeCode());
                        if (_log.shouldLog(Log.WARN))
                            _log.warn("Unsupported sig type " + stype + " for destination " + h);
                        throw new UnsupportedCryptoException("Sig type " + stype);
                    }
                } catch (DataFormatException dfe) {
                }
            }
        } else if (entry.getType() == DatabaseEntry.KEY_TYPE_ROUTERINFO) {
            RouterInfo ri = (RouterInfo) entry;
            RouterIdentity id = ri.getIdentity();
            Certificate c = id.getCertificate();
            if (c.getCertificateType() == Certificate.CERTIFICATE_TYPE_KEY) {
                try {
                    KeyCertificate kc = c.toKeyCertificate();
                    SigType type = kc.getSigType();
                    if (type == null || !type.isAvailable()) {
                        String stype = (type != null) ? type.toString() : Integer.toString(kc.getSigTypeCode());
                        _context.banlist().banlistRouterForever(h, "Unsupported signature type " + stype);
                        if (_log.shouldLog(Log.WARN))
                            _log.warn("Unsupported sig type " + stype + " for router " + h);
                        throw new UnsupportedCryptoException("Sig type " + stype);
                    }
                } catch (DataFormatException dfe) {
                }
            }
        }
    }
    if (_log.shouldLog(Log.WARN))
        _log.warn("Verify fail, cause unknown: " + entry);
}
Also used : LeaseSet(net.i2p.data.LeaseSet) Destination(net.i2p.data.Destination) KeyCertificate(net.i2p.data.KeyCertificate) DataFormatException(net.i2p.data.DataFormatException) RouterInfo(net.i2p.data.router.RouterInfo) RouterIdentity(net.i2p.data.router.RouterIdentity) SigType(net.i2p.crypto.SigType) Certificate(net.i2p.data.Certificate) KeyCertificate(net.i2p.data.KeyCertificate)

Aggregations

Certificate (net.i2p.data.Certificate)13 SigningPublicKey (net.i2p.data.SigningPublicKey)6 PublicKey (net.i2p.data.PublicKey)5 CertificateTest (net.i2p.data.CertificateTest)4 PublicKeyTest (net.i2p.data.PublicKeyTest)4 SigningPublicKeyTest (net.i2p.data.SigningPublicKeyTest)4 RouterInfo (net.i2p.data.router.RouterInfo)4 Properties (java.util.Properties)3 DataFormatException (net.i2p.data.DataFormatException)3 Destination (net.i2p.data.Destination)3 StructureTest (net.i2p.data.StructureTest)3 RouterIdentity (net.i2p.data.router.RouterIdentity)3 Test (org.junit.Test)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 IOException (java.io.IOException)2 SigType (net.i2p.crypto.SigType)2 Hash (net.i2p.data.Hash)2 KeyCertificate (net.i2p.data.KeyCertificate)2 PrivateKey (net.i2p.data.PrivateKey)2