Search in sources :

Example 1 with I2PClient

use of net.i2p.client.I2PClient in project i2p.i2p by i2p.

the class SAMUtils method genRandomKey.

/**
 * Generate a random destination key.
 * Caller must close streams. Fails silently.
 *
 * @param priv Stream used to write the destination and private keys
 * @param pub Stream used to write the destination (may be null)
 * @param sigType what signature type
 * @since 0.9.14
 */
public static void genRandomKey(OutputStream priv, OutputStream pub, SigType sigType) {
    // _log.debug("Generating random keys...");
    try {
        I2PClient c = I2PClientFactory.createClient();
        Destination d = c.createDestination(priv, sigType);
        priv.flush();
        if (pub != null) {
            d.writeBytes(pub);
            pub.flush();
        }
    } catch (I2PException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) I2PClient(net.i2p.client.I2PClient) IOException(java.io.IOException)

Example 2 with I2PClient

use of net.i2p.client.I2PClient in project i2p.i2p by i2p.

the class TunnelController method createPrivateKey.

/**
 * @return success
 */
private boolean createPrivateKey() {
    I2PClient client = I2PClientFactory.createClient();
    File keyFile = getPrivateKeyFile();
    if (keyFile == null) {
        log("No filename specified for the private key");
        return false;
    }
    if (keyFile.exists()) {
        // log("Not overwriting existing private keys in " + keyFile.getAbsolutePath());
        return true;
    } else {
        File parent = keyFile.getParentFile();
        if ((parent != null) && (!parent.exists()))
            parent.mkdirs();
    }
    FileOutputStream fos = null;
    try {
        fos = new SecureFileOutputStream(keyFile);
        SigType stype = PREFERRED_SIGTYPE;
        String st = _config.getProperty(OPT_SIG_TYPE);
        if (st != null) {
            SigType type = SigType.parseSigType(st);
            if (type != null && type.isAvailable())
                stype = type;
            else
                log("Unsupported sig type " + st + ", reverting to " + stype);
        }
        Destination dest = client.createDestination(fos, stype);
        String destStr = dest.toBase64();
        log("Private key created and saved in " + keyFile.getAbsolutePath());
        log("You should backup this file in a secure place.");
        log("New destination: " + destStr);
        String b32 = dest.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(keyFile, backup, false, true)) {
                SecureFileOutputStream.setPerms(backup);
                log("Private key backup saved to " + backup.getAbsolutePath());
            }
        }
    } catch (I2PException ie) {
        if (_log.shouldLog(Log.ERROR))
            _log.error("Error creating new destination", ie);
        log("Error creating new destination: " + ie.getMessage());
        return false;
    } catch (IOException ioe) {
        if (_log.shouldLog(Log.ERROR))
            _log.error("Error creating writing the destination to " + keyFile.getAbsolutePath(), ioe);
        log("Error writing the keys to " + keyFile.getAbsolutePath());
        return false;
    } finally {
        if (fos != null)
            try {
                fos.close();
            } catch (IOException ioe) {
            }
    }
    return true;
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) SecureFile(net.i2p.util.SecureFile) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) FileOutputStream(java.io.FileOutputStream) I2PClient(net.i2p.client.I2PClient) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) IOException(java.io.IOException) SecureFile(net.i2p.util.SecureFile) PrivateKeyFile(net.i2p.data.PrivateKeyFile) File(java.io.File) SigType(net.i2p.crypto.SigType)

Example 3 with I2PClient

use of net.i2p.client.I2PClient in project i2p.i2p by i2p.

the class I2PSocketManagerFactory method createManager.

/**
 * Create a socket manager using the destination loaded from the given private key
 * stream and connected to the I2CP router on the specified machine on the given
 * port.
 *
 * Blocks for a long time while the router builds tunnels if connect is true.
 *
 * @param myPrivateKeyStream private key stream, format is specified in {@link net.i2p.data.PrivateKeyFile PrivateKeyFile}
 *                           non-null. Caller must close.
 * @param i2cpHost I2CP host null to use default, ignored if in router context
 * @param i2cpPort I2CP port <= 0 to use default, ignored if in router context
 * @param opts Streaming and I2CP options, may be null
 * @param connect true to connect (blocking)
 * @return the newly created socket manager, non-null (throws on error)
 * @since 0.9.7
 */
private static I2PSocketManager createManager(InputStream myPrivateKeyStream, String i2cpHost, int i2cpPort, Properties opts, boolean connect) throws I2PSessionException {
    I2PClient client = I2PClientFactory.createClient();
    if (opts == null)
        opts = new Properties();
    Properties syscopy = (Properties) System.getProperties().clone();
    for (Map.Entry<Object, Object> e : syscopy.entrySet()) {
        String name = (String) e.getKey();
        if (opts.getProperty(name) == null)
            opts.setProperty(name, (String) e.getValue());
    }
    // as of 0.8.1 (I2CP default is BestEffort)
    if (opts.getProperty(I2PClient.PROP_RELIABILITY) == null)
        opts.setProperty(I2PClient.PROP_RELIABILITY, I2PClient.PROP_RELIABILITY_NONE);
    if (i2cpHost != null)
        opts.setProperty(I2PClient.PROP_TCP_HOST, i2cpHost);
    if (i2cpPort > 0)
        opts.setProperty(I2PClient.PROP_TCP_PORT, "" + i2cpPort);
    I2PSession session = client.createSession(myPrivateKeyStream, opts);
    if (connect)
        session.connect();
    I2PSocketManager sockMgr = createManager(session, opts, "manager");
    return sockMgr;
}
Also used : I2PSession(net.i2p.client.I2PSession) I2PClient(net.i2p.client.I2PClient) Properties(java.util.Properties) Map(java.util.Map)

Example 4 with I2PClient

use of net.i2p.client.I2PClient in project i2p.i2p by i2p.

the class DatagramTest method testBadagram.

public void testBadagram() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    I2PClient client = I2PClientFactory.createClient();
    Destination d = client.createDestination(out);
    I2PSession session = client.createSession(new ByteArrayInputStream(out.toByteArray()), null);
    DSAEngine dsaEng = DSAEngine.getInstance();
    ByteArrayOutputStream dout = new ByteArrayOutputStream();
    d.writeBytes(dout);
    dsaEng.sign(Hash.FAKE_HASH.toByteArray(), session.getPrivateKey()).writeBytes(dout);
    dout.write(DataHelper.getASCII("blah"));
    byte[] data = dout.toByteArray();
    I2PDatagramDissector dd = new I2PDatagramDissector();
    dd.loadI2PDatagram(data);
    boolean error = false;
    try {
        dd.getPayload();
    } catch (I2PInvalidDatagramException i2pide) {
        error = true;
    }
    assertTrue(error);
    error = false;
    try {
        dd.getSender();
    } catch (I2PInvalidDatagramException i2pide) {
        error = true;
    }
    assertTrue(error);
    error = false;
    try {
        dd.getHash();
    } catch (I2PInvalidDatagramException i2pide) {
        error = true;
    }
    assertTrue(error);
}
Also used : Destination(net.i2p.data.Destination) DSAEngine(net.i2p.crypto.DSAEngine) ByteArrayInputStream(java.io.ByteArrayInputStream) I2PSession(net.i2p.client.I2PSession) I2PClient(net.i2p.client.I2PClient) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 5 with I2PClient

use of net.i2p.client.I2PClient in project i2p.i2p by i2p.

the class PingIT method createSession.

private I2PSession createSession() throws Exception {
    I2PClient client = I2PClientFactory.createClient();
    ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
    client.createDestination(baos);
    I2PSession sess = client.createSession(new ByteArrayInputStream(baos.toByteArray()), new Properties());
    sess.connect();
    return sess;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) I2PSession(net.i2p.client.I2PSession) I2PClient(net.i2p.client.I2PClient) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(java.util.Properties)

Aggregations

I2PClient (net.i2p.client.I2PClient)16 I2PSession (net.i2p.client.I2PSession)9 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 IOException (java.io.IOException)7 Destination (net.i2p.data.Destination)7 Properties (java.util.Properties)6 I2PException (net.i2p.I2PException)6 I2PSessionException (net.i2p.client.I2PSessionException)4 I2PSimpleClient (net.i2p.client.I2PSimpleClient)3 File (java.io.File)2 SigType (net.i2p.crypto.SigType)2 DataFormatException (net.i2p.data.DataFormatException)2 OrderedProperties (net.i2p.util.OrderedProperties)2 Getopt (gnu.getopt.Getopt)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 GeneralSecurityException (java.security.GeneralSecurityException)1 KeyException (java.security.KeyException)1