Search in sources :

Example 21 with SecureFileOutputStream

use of net.i2p.util.SecureFileOutputStream in project i2p.i2p-bote by i2p.

the class KademliaDHT method writePeers.

private void writePeers(List<KademliaPeer> peers, File file) {
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(file.getAbsolutePath())));
        writer.write("# Each line is one Base64-encoded I2P destination.");
        writer.newLine();
        writer.write("# Do not edit while I2P-Bote is running as it will be overwritten.");
        writer.newLine();
        for (KademliaPeer peer : peers) {
            writer.write(peer.toBase64());
            writer.newLine();
        }
    } catch (IOException e) {
        log.error("Can't write peers to file <" + file.getAbsolutePath() + ">", e);
    } finally {
        if (writer != null)
            try {
                writer.close();
            } catch (IOException e) {
                log.error("Can't close BufferedWriter for file <" + file.getAbsolutePath() + ">", e);
            }
    }
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Example 22 with SecureFileOutputStream

use of net.i2p.util.SecureFileOutputStream in project i2p.i2p-bote by i2p.

the class I2PBote method saveLocalDestinationKeys.

/**
 * Writes private + public keys for the local destination out to a file.
 * @param keyFile
 * @param localDestinationArray
 * @throws DataFormatException
 * @throws IOException
 */
private void saveLocalDestinationKeys(File keyFile, byte[] localDestinationArray) throws DataFormatException, IOException {
    keyFile = new SecureFile(keyFile.getAbsolutePath());
    if (keyFile.exists()) {
        File oldKeyFile = new File(keyFile.getPath() + "_backup");
        if (!keyFile.renameTo(oldKeyFile))
            log.error("Cannot rename destination key file <" + keyFile.getAbsolutePath() + "> to <" + oldKeyFile.getAbsolutePath() + ">");
    } else if (!keyFile.createNewFile())
        log.error("Cannot create destination key file: <" + keyFile.getAbsolutePath() + ">");
    BufferedWriter fileWriter = new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(keyFile)));
    try {
        fileWriter.write(Base64.encode(localDestinationArray));
    } finally {
        fileWriter.close();
    }
}
Also used : SecureFile(net.i2p.util.SecureFile) OutputStreamWriter(java.io.OutputStreamWriter) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) SecureFile(net.i2p.util.SecureFile) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 23 with SecureFileOutputStream

use of net.i2p.util.SecureFileOutputStream in project i2p.i2p-bote by i2p.

the class AddressBook method save.

public void save() throws IOException, PasswordException, GeneralSecurityException {
    initializeIfNeeded();
    OutputStream encryptedStream = new EncryptedOutputStream(new SecureFileOutputStream(addressFile), passwordHolder);
    try {
        Properties properties = saveToProperties();
        properties.store(new OutputStreamWriter(encryptedStream, "UTF-8"), null);
    } catch (IOException e) {
        log.error("Can't save email identities to file <" + addressFile.getAbsolutePath() + ">.", e);
        throw e;
    } finally {
        encryptedStream.close();
    }
}
Also used : EncryptedOutputStream(i2p.bote.fileencryption.EncryptedOutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) OutputStream(java.io.OutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) Properties(java.util.Properties) SortedProperties(i2p.bote.util.SortedProperties) EncryptedOutputStream(i2p.bote.fileencryption.EncryptedOutputStream)

Example 24 with SecureFileOutputStream

use of net.i2p.util.SecureFileOutputStream in project i2p.i2p-bote by i2p.

the class PasswordCache method createDerivedKey.

/**
 * Reads salt and <code>scrypt</code> parameters from the cache file, or chooses
 * a new salt array if the file doesn't exist. The encryption key is then computed
 * and the variable <code>derivedKey</code> is populated.
 * @throws IOException
 * @throws GeneralSecurityException
 */
private void createDerivedKey() throws IOException, GeneralSecurityException {
    byte[] salt = null;
    derivedKey = null;
    // read salt + scrypt parameters from file if available
    File derivParamFile = configuration.getKeyDerivationParametersFile();
    if (derivParamFile.exists())
        derivedKey = FileEncryptionUtil.getEncryptionKey(password, derivParamFile);
    // if necessary, create a new salt and key and write the derivation parameters to the cache file
    if (derivedKey == null || !derivedKey.scryptParams.equals(KDF_PARAMETERS)) {
        I2PAppContext appContext = I2PAppContext.getGlobalContext();
        salt = new byte[SALT_LENGTH];
        appContext.random().nextBytes(salt);
        DataOutputStream outputStream = null;
        try {
            byte[] key = FileEncryptionUtil.getEncryptionKey(password, salt, KDF_PARAMETERS);
            derivedKey = new DerivedKey(salt, KDF_PARAMETERS, key);
            outputStream = new DataOutputStream(new SecureFileOutputStream(derivParamFile));
            KDF_PARAMETERS.writeTo(outputStream);
            outputStream.write(salt);
        } finally {
            if (outputStream != null)
                outputStream.close();
        }
    }
}
Also used : I2PAppContext(net.i2p.I2PAppContext) DataOutputStream(java.io.DataOutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) File(java.io.File)

Example 25 with SecureFileOutputStream

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

the class PersistNews method store.

/**
 *  Store each entry.
 *  Old entries are always overwritten, as they may change even without the updated date changing.
 *
 *  @param entries each one should be "entry" at the root
 *  @return success
 */
public static boolean store(I2PAppContext ctx, List<Node> entries) {
    Log log = ctx.logManager().getLog(PersistNews.class);
    File dir = new SecureDirectory(ctx.getConfigDir(), DIR);
    if (!dir.exists())
        dir.mkdirs();
    StringBuilder buf = new StringBuilder();
    boolean rv = true;
    for (Node entry : entries) {
        Node nid = entry.getNode("id");
        if (nid == null) {
            if (log.shouldWarn())
                log.warn("entry without UUID");
            continue;
        }
        String id = nid.getValue();
        if (id == null) {
            if (log.shouldWarn())
                log.warn("entry without UUID");
            continue;
        }
        String name = idToName(ctx, id);
        File file = new File(dir, name);
        Writer out = null;
        try {
            out = new OutputStreamWriter(new GZIPOutputStream(new SecureFileOutputStream(file)));
            out.write(XML_START);
            XMLParser.toString(buf, entry);
            out.write(buf.toString());
            buf.setLength(0);
        } catch (IOException ioe) {
            if (log.shouldWarn())
                log.warn("failed store to " + file, ioe);
            rv = false;
        } finally {
            if (out != null)
                try {
                    out.close();
                } catch (IOException ioe) {
                }
        }
    }
    return rv;
}
Also used : Log(net.i2p.util.Log) SecureDirectory(net.i2p.util.SecureDirectory) GZIPOutputStream(java.util.zip.GZIPOutputStream) Node(org.cybergarage.xml.Node) OutputStreamWriter(java.io.OutputStreamWriter) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) IOException(java.io.IOException) File(java.io.File) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Aggregations

SecureFileOutputStream (net.i2p.util.SecureFileOutputStream)55 IOException (java.io.IOException)50 File (java.io.File)33 OutputStream (java.io.OutputStream)22 OutputStreamWriter (java.io.OutputStreamWriter)21 FileOutputStream (java.io.FileOutputStream)19 BufferedWriter (java.io.BufferedWriter)16 FileInputStream (java.io.FileInputStream)11 SecureFile (net.i2p.util.SecureFile)9 BufferedOutputStream (java.io.BufferedOutputStream)8 PrintWriter (java.io.PrintWriter)7 InputStream (java.io.InputStream)6 GeneralSecurityException (java.security.GeneralSecurityException)6 DataFormatException (net.i2p.data.DataFormatException)6 EncryptedOutputStream (i2p.bote.fileencryption.EncryptedOutputStream)4 KeyStore (java.security.KeyStore)4 X509Certificate (java.security.cert.X509Certificate)4 Properties (java.util.Properties)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Writer (java.io.Writer)3