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) {
}
}
}
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;
}
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();
}
}
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");
}
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) {
}
}
}
}
Aggregations