Search in sources :

Example 21 with SecureDirectory

use of net.i2p.util.SecureDirectory in project i2p.i2p by i2p.

the class SnarkManager method commentFile.

/**
 *  The conmment file for a torrent
 *  @param confDir the config directory
 *  @param ih 20-byte infohash
 *  @since 0.9.31
 */
private static File commentFile(File confDir, byte[] ih) {
    String hex = I2PSnarkUtil.toHex(ih);
    File subdir = new SecureDirectory(confDir, SUBDIR_PREFIX + B64.charAt((ih[0] >> 2) & 0x3f));
    return new File(subdir, hex + COMMENT_FILE_SUFFIX);
}
Also used : SecureDirectory(net.i2p.util.SecureDirectory) File(java.io.File)

Example 22 with SecureDirectory

use of net.i2p.util.SecureDirectory in project i2p.i2p by i2p.

the class SSLClientUtil method verifyKeyStore.

/**
 *  Create a new selfsigned cert and keystore and pubkey cert if they don't exist.
 *  May take a while.
 *
 *  @param opts in/out, updated if rv is true
 *  @param optPfx add this prefix when getting/setting options
 *  @param altNames the Subject Alternative Names. May be null. May contain hostnames and/or IP addresses.
 *                  cname, localhost, 127.0.0.1, and ::1 will be automatically added.
 *  @return false if it already exists; if true, caller must save opts
 *  @throws IOException on creation fail
 *  @since 0.9.34 added altNames param
 */
public static boolean verifyKeyStore(Properties opts, String optPfx, Set<String> altNames) throws IOException {
    String name = opts.getProperty(optPfx + PROP_KEY_ALIAS);
    if (name == null) {
        name = KeyStoreUtil.randomString();
        opts.setProperty(optPfx + PROP_KEY_ALIAS, name);
    }
    String ksname = opts.getProperty(optPfx + PROP_KS_NAME);
    if (ksname == null) {
        ksname = PREFIX + name + KS_SUFFIX;
        opts.setProperty(optPfx + PROP_KS_NAME, ksname);
    }
    File ks = new File(ksname);
    if (!ks.isAbsolute()) {
        ks = new File(I2PAppContext.getGlobalContext().getConfigDir(), KS_DIR);
        ks = new File(ks, ksname);
    }
    if (ks.exists())
        return false;
    File dir = ks.getParentFile();
    if (!dir.exists()) {
        File sdir = new SecureDirectory(dir.getAbsolutePath());
        if (!sdir.mkdirs())
            throw new IOException("Unable to create keystore " + ks);
    }
    boolean rv = createKeyStore(ks, name, opts, optPfx, altNames);
    if (!rv)
        throw new IOException("Unable to create keystore " + ks);
    // Now read it back out of the new keystore and save it in ascii form
    // where the clients can get to it.
    // Failure of this part is not fatal.
    exportCert(ks, name, opts, optPfx);
    return true;
}
Also used : SecureDirectory(net.i2p.util.SecureDirectory) IOException(java.io.IOException) File(java.io.File)

Example 23 with SecureDirectory

use of net.i2p.util.SecureDirectory in project i2p.i2p by i2p.

the class SSLClientUtil method exportCert.

/**
 *  Pull the cert back OUT of the keystore and save it as ascii
 *  so the clients can get to it.
 *
 *  @param name used to generate output file name
 *  @param opts must contain optPfx + PROP_KEY_ALIAS
 *  @param optPfx add this prefix when getting options
 */
private static void exportCert(File ks, String name, Properties opts, String optPfx) {
    File sdir = new SecureDirectory(I2PAppContext.getGlobalContext().getConfigDir(), CERT_DIR);
    if (sdir.exists() || sdir.mkdirs()) {
        String keyAlias = opts.getProperty(optPfx + PROP_KEY_ALIAS);
        String ksPass = opts.getProperty(optPfx + PROP_KEYSTORE_PASSWORD, KeyStoreUtil.DEFAULT_KEYSTORE_PASSWORD);
        File out = new File(sdir, PREFIX + name + ASCII_KEYFILE_SUFFIX);
        boolean success = KeyStoreUtil.exportCert(ks, ksPass, keyAlias, out);
        if (!success)
            error("Error getting SSL cert to save as ASCII");
    } else {
        error("Error saving ASCII SSL keys");
    }
}
Also used : SecureDirectory(net.i2p.util.SecureDirectory) File(java.io.File)

Example 24 with SecureDirectory

use of net.i2p.util.SecureDirectory in project i2p.i2p by i2p.

the class SSLUtil method exportCert.

/**
 *  Pull the cert back OUT of the keystore and save it as ascii
 *  so the clients can get to it.
 *
 *  @param name used to generate output file name
 *  @param opts must contain PROP_KEY_ALIAS
 */
private static void exportCert(File ks, String name, Properties opts) {
    File sdir = new SecureDirectory(I2PAppContext.getGlobalContext().getConfigDir(), CERT_DIR);
    if (sdir.exists() || sdir.mkdirs()) {
        String keyAlias = opts.getProperty(PROP_KEY_ALIAS);
        String ksPass = opts.getProperty(PROP_KEYSTORE_PASSWORD, KeyStoreUtil.DEFAULT_KEYSTORE_PASSWORD);
        File out = new File(sdir, PREFIX + name + ASCII_KEYFILE_SUFFIX);
        boolean success = KeyStoreUtil.exportCert(ks, ksPass, keyAlias, out);
        if (!success)
            error("Error getting SSL cert to save as ASCII");
    } else {
        error("Error saving ASCII SSL keys");
    }
}
Also used : SecureDirectory(net.i2p.util.SecureDirectory) File(java.io.File)

Example 25 with SecureDirectory

use of net.i2p.util.SecureDirectory in project i2p.i2p by i2p.

the class SSLUtil method verifyKeyStore.

/**
 *  Create a new selfsigned cert and keystore and pubkey cert if they don't exist.
 *  May take a while.
 *
 *  @param opts in/out, updated if rv is true
 *  @return false if it already exists; if true, caller must save opts
 *  @throws IOException on creation fail
 */
public static boolean verifyKeyStore(Properties opts) throws IOException {
    String name = opts.getProperty(PROP_KEY_ALIAS);
    if (name == null) {
        name = KeyStoreUtil.randomString();
        opts.setProperty(PROP_KEY_ALIAS, name);
    }
    String ksname = opts.getProperty(PROP_KS_NAME);
    if (ksname == null) {
        ksname = PREFIX + name + KS_SUFFIX;
        opts.setProperty(PROP_KS_NAME, ksname);
    }
    File ks = new File(ksname);
    if (!ks.isAbsolute()) {
        ks = new File(I2PAppContext.getGlobalContext().getConfigDir(), KS_DIR);
        ks = new File(ks, ksname);
    }
    if (ks.exists())
        return false;
    File dir = ks.getParentFile();
    if (!dir.exists()) {
        File sdir = new SecureDirectory(dir.getAbsolutePath());
        if (!sdir.mkdirs())
            throw new IOException("Unable to create keystore " + ks);
    }
    boolean rv = createKeyStore(ks, name, opts);
    if (!rv)
        throw new IOException("Unable to create keystore " + ks);
    // Now read it back out of the new keystore and save it in ascii form
    // where the clients can get to it.
    // Failure of this part is not fatal.
    exportCert(ks, name, opts);
    return true;
}
Also used : SecureDirectory(net.i2p.util.SecureDirectory) IOException(java.io.IOException) File(java.io.File)

Aggregations

SecureDirectory (net.i2p.util.SecureDirectory)32 File (java.io.File)31 IOException (java.io.IOException)16 HashMap (java.util.HashMap)5 Properties (java.util.Properties)4 GeneralSecurityException (java.security.GeneralSecurityException)3 ArrayList (java.util.ArrayList)3 OrderedProperties (net.i2p.util.OrderedProperties)3 SecureFile (net.i2p.util.SecureFile)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 PrivateKey (java.security.PrivateKey)2 Map (java.util.Map)2 StringTokenizer (java.util.StringTokenizer)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 SU3File (net.i2p.crypto.SU3File)2 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)2 InputStream (java.io.InputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 InetSocketAddress (java.net.InetSocketAddress)1