Search in sources :

Example 16 with SecureFileOutputStream

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

the class ClientAppConfig method writeClientAppConfig.

/**
 * classpath and stopargs not supported
 */
public static void writeClientAppConfig(RouterContext ctx, List<ClientAppConfig> apps) {
    File cfgFile = configFile(ctx);
    FileOutputStream fos = null;
    try {
        fos = new SecureFileOutputStream(cfgFile);
        StringBuilder buf = new StringBuilder(2048);
        for (int i = 0; i < apps.size(); i++) {
            ClientAppConfig app = apps.get(i);
            buf.append(PREFIX).append(i).append(".main=").append(app.className).append("\n");
            buf.append(PREFIX).append(i).append(".name=").append(app.clientName).append("\n");
            if (app.args != null)
                buf.append(PREFIX).append(i).append(".args=").append(app.args).append("\n");
            buf.append(PREFIX).append(i).append(".delay=").append(app.delay / 1000).append("\n");
            buf.append(PREFIX).append(i).append(".startOnLoad=").append(!app.disabled).append("\n");
        }
        fos.write(buf.toString().getBytes("UTF-8"));
    } catch (IOException ioe) {
    } finally {
        if (fos != null)
            try {
                fos.close();
            } catch (IOException ioe) {
            }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 17 with SecureFileOutputStream

use of net.i2p.util.SecureFileOutputStream 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 18 with SecureFileOutputStream

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

the class WorkingDir method migrateClientsConfig.

/**
 *  Copy over the clients.config file with modifications
 */
private static boolean migrateClientsConfig(File olddir, File todir) {
    File oldFile = new File(olddir, "clients.config");
    File newFile = new File(todir, "clients.config");
    FileInputStream in = null;
    PrintWriter out = null;
    try {
        in = new FileInputStream(oldFile);
        out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(newFile), "UTF-8")));
        out.println("# Modified by I2P User dir migration script");
        String s = null;
        boolean isDaemon = SystemVersion.isLinuxService();
        while ((s = DataHelper.readLine(in)) != null) {
            // readLine() doesn't strip \r
            if (s.endsWith("\r"))
                s = s.substring(0, s.length() - 1);
            if (s.endsWith("=\"eepsite/jetty.xml\"")) {
                s = s.replace("=\"eepsite/jetty.xml\"", "=\"" + todir.getAbsolutePath() + File.separatorChar + "eepsite" + File.separatorChar + "jetty.xml\"");
            } else if (isDaemon && s.equals("clientApp.4.startOnLoad=true")) {
                // disable browser launch for daemon
                s = "clientApp.4.startOnLoad=false";
            }
            out.println(s);
        }
        System.err.println("Copied " + oldFile + " with modifications");
        if (out.checkError())
            throw new IOException("Failed write to " + newFile);
        return true;
    } catch (IOException ioe) {
        if (in != null) {
            System.err.println("FAILED copy " + oldFile + ": " + ioe);
        }
        return false;
    } finally {
        if (in != null)
            try {
                in.close();
            } catch (IOException ioe) {
            }
        if (out != null)
            out.close();
    }
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 19 with SecureFileOutputStream

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

the class ProfilePersistenceHelper method writeProfile.

/**
 * write out the data from the profile to the stream
 */
public void writeProfile(PeerProfile profile) {
    if (isExpired(profile.getLastSendSuccessful()))
        return;
    File f = pickFile(profile);
    long before = _context.clock().now();
    OutputStream fos = null;
    try {
        fos = new BufferedOutputStream(new GZIPOutputStream(new SecureFileOutputStream(f)));
        writeProfile(profile, fos);
    } catch (IOException ioe) {
        _log.error("Error writing profile to " + f);
    } finally {
        if (fos != null)
            try {
                fos.close();
            } catch (IOException ioe) {
            }
    }
    long delay = _context.clock().now() - before;
    if (_log.shouldLog(Log.DEBUG))
        _log.debug("Writing the profile to " + f.getName() + " took " + delay + "ms");
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStream(java.io.OutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 20 with SecureFileOutputStream

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

the class PersistRouterInfoJob method runJob.

public void runJob() {
    Log _log = getContext().logManager().getLog(PersistRouterInfoJob.class);
    if (_log.shouldLog(Log.DEBUG))
        _log.debug("Persisting updated router info");
    File infoFile = new File(getContext().getRouterDir(), CreateRouterInfoJob.INFO_FILENAME);
    RouterInfo info = getContext().router().getRouterInfo();
    FileOutputStream fos = null;
    synchronized (getContext().router().routerInfoFileLock) {
        try {
            fos = new SecureFileOutputStream(infoFile);
            info.writeBytes(fos);
        } catch (DataFormatException dfe) {
            _log.error("Error rebuilding the router information", dfe);
        } catch (IOException ioe) {
            _log.error("Error writing out the rebuilt router information", ioe);
        } finally {
            if (fos != null)
                try {
                    fos.close();
                } catch (IOException ioe) {
                }
        }
    }
}
Also used : DataFormatException(net.i2p.data.DataFormatException) Log(net.i2p.util.Log) RouterInfo(net.i2p.data.router.RouterInfo) FileOutputStream(java.io.FileOutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) IOException(java.io.IOException) File(java.io.File)

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