use of net.i2p.I2PAppContext in project i2p.i2p-bote by i2p.
the class EncryptedOutputStream method encryptAndWrite.
/**
* Writes the header, then encrypts the internal buffer and writes the encrypted
* data to the underlying <code>OutputStream</code>.
* @throws IOException
*/
// for net.i2p.crypto.AESEngine
@SuppressWarnings("deprecation")
private void encryptAndWrite() throws IOException {
downstream.write(START_OF_FILE);
downstream.write(FORMAT_VERSION);
FileEncryptionConstants.KDF_PARAMETERS.writeTo(downstream);
downstream.write(derivedKey.salt);
byte[] iv = new byte[BLOCK_SIZE];
I2PAppContext appContext = I2PAppContext.getGlobalContext();
appContext.random().nextBytes(iv);
downstream.write(iv);
byte[] data = outputBuffer.toByteArray();
SessionKey key = new SessionKey(derivedKey.key);
byte[] encryptedData = appContext.aes().safeEncrypt(data, key, iv, 0);
downstream.write(encryptedData);
}
use of net.i2p.I2PAppContext 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();
}
}
}
use of net.i2p.I2PAppContext in project i2p.i2p by i2p.
the class WebAppConfiguration method configureClassPath.
/**
* This was the interface in Jetty 5, in Jetty 6 was configureClassLoader(),
* now it's configure()
*/
private void configureClassPath(WebAppContext wac) throws Exception {
String ctxPath = wac.getContextPath();
// System.err.println("Configure Class Path " + ctxPath);
if (ctxPath.equals("/"))
return;
String appName = ctxPath.substring(1);
/**
**
* if (ctxPath.equals("/susimail")) {
* // allow certain Jetty classes, restricted as of Jetty 7
* // See http://wiki.eclipse.org/Jetty/Reference/Jetty_Classloading
* //System.err.println("Allowing Jetty utils in classpath for " + appName);
* //System.err.println("System classes before: " + Arrays.toString(wac.getSystemClasses()));
* //System.err.println("Server classes before: " + Arrays.toString(wac.getServerClasses()));
* wac.addSystemClass("org.eclipse.jetty.http.");
* wac.addSystemClass("org.eclipse.jetty.io.");
* wac.addSystemClass("org.eclipse.jetty.util.");
* // org.eclipse.jetty.webapp.ClasspathPattern looks in-order, and
* // WebAppContext doesn't provide a remove method, so we must
* // convert to a list, remove the wildcard entry, add ours, then
* // add the wildcard back, then reset.
* List<String> classes = new ArrayList<String>(16);
* classes.addAll(Arrays.asList(wac.getServerClasses()));
* classes.remove("org.eclipse.jetty.");
* classes.add("-org.eclipse.jetty.http.");
* classes.add("-org.eclipse.jetty.io.");
* classes.add("-org.eclipse.jetty.util.");
* classes.add("org.eclipse.jetty.");
* wac.setServerClasses(classes.toArray(new String[classes.size()]));
* //System.err.println("System classes after: " + Arrays.toString(wac.getSystemClasses()));
* //System.err.println("Server classes after: " + Arrays.toString(wac.getServerClasses()));
* }
***
*/
I2PAppContext i2pContext = I2PAppContext.getGlobalContext();
File libDir = new File(i2pContext.getBaseDir(), "lib");
// FIXME this only works if war is the same name as the plugin
File pluginDir = new File(i2pContext.getConfigDir(), PluginStarter.PLUGIN_DIR + ctxPath);
File dir = libDir;
String cp;
/**
**
* if (ctxPath.equals("/susimail")) {
* // Ticket #957... don't know why...
* // Only really required if started manually, but we don't know that from here
* cp = "jetty-util.jar";
***
*/
if (ctxPath.equals("/susidns")) {
// Old installs don't have this in their wrapper.config classpath
cp = "addressbook.jar";
} else if (pluginDir.exists()) {
File consoleDir = new File(pluginDir, "console");
Properties props = RouterConsoleRunner.webAppProperties(consoleDir.getAbsolutePath());
cp = props.getProperty(RouterConsoleRunner.PREFIX + appName + CLASSPATH);
dir = pluginDir;
} else {
Properties props = RouterConsoleRunner.webAppProperties(i2pContext);
cp = props.getProperty(RouterConsoleRunner.PREFIX + appName + CLASSPATH);
}
if (cp == null)
return;
StringTokenizer tok = new StringTokenizer(cp, " ,");
StringBuilder buf = new StringBuilder();
Set<URI> systemCP = getSystemClassPath(i2pContext);
while (tok.hasMoreTokens()) {
if (buf.length() > 0)
buf.append(',');
String elem = tok.nextToken().trim();
String path;
if (elem.startsWith("$I2P"))
path = i2pContext.getBaseDir().getAbsolutePath() + elem.substring(4);
else if (elem.startsWith("$PLUGIN"))
path = dir.getAbsolutePath() + elem.substring(7);
else
path = dir.getAbsolutePath() + '/' + elem;
// As of Jetty 6, we can't add dups to the class path, or
// else it screws up statics
// This is not a complete solution because the Windows no-wrapper classpath is set
// by the launchi2p.jar (i2p.exe) manifest and is not detected below.
// TODO: Add a classpath to the command line in i2pstandalone.xml?
File jfile = new File(path);
File jdir = jfile.getParentFile();
if (systemCP.contains(jfile.toURI()) || (jdir != null && systemCP.contains(jdir.toURI()))) {
// Ticket #957... don't know why...
if (!ctxPath.equals("/susimail"))
continue;
}
// System.err.println("Adding " + path + " to classpath for " + appName);
buf.append(path);
}
if (buf.length() <= 0)
return;
ClassLoader cl = wac.getClassLoader();
if (cl != null && cl instanceof WebAppClassLoader) {
WebAppClassLoader wacl = (WebAppClassLoader) cl;
wacl.addClassPath(buf.toString());
} else {
// This was not working because the WebAppClassLoader already exists
// and it calls getExtraClasspath in its constructor
// Not sure why WACL already exists...
wac.setExtraClasspath(buf.toString());
}
}
use of net.i2p.I2PAppContext in project i2p.i2p by i2p.
the class NewsXMLParser method main.
public static void main(String[] args) {
if (args.length <= 0 || args.length > 2) {
System.err.println("Usage: NewsXMLParser file.xml [parserMode]");
System.exit(1);
}
try {
I2PAppContext ctx = new I2PAppContext();
Debug.initialize(ctx);
NewsXMLParser parser = new NewsXMLParser(ctx);
if (args.length > 1) {
XHTMLMode mode = XHTMLMode.valueOf(args[1]);
parser.setXHTMLMode(mode);
} else {
parser.setXHTMLMode(XHTMLMode.ABORT);
}
parser.parse(new File(args[0]));
NewsMetadata ud = parser.getMetadata();
List<NewsEntry> entries = parser.getEntries();
NewsMetadata.Release latestRelease = ud.releases.get(0);
System.out.println("Latest version is " + latestRelease.i2pVersion);
System.out.println("Release timestamp: " + latestRelease.date);
System.out.println("Feed timestamp: " + ud.feedUpdated);
System.out.println("Found " + entries.size() + " news entries");
Set<String> uuids = new HashSet<String>(entries.size());
for (int i = 0; i < entries.size(); i++) {
NewsEntry e = entries.get(i);
System.out.println("\n****** News #" + (i + 1) + ": " + e.title + '\n' + e.content);
if (e.id == null)
throw new IOException("missing ID");
if (e.title == null)
throw new IOException("missing title");
if (e.content == null)
throw new IOException("missing content");
if (e.authorName == null)
throw new IOException("missing author");
if (e.updated == 0)
throw new IOException("missing updated");
if (!uuids.add(e.id))
throw new IOException("duplicate ID");
}
} catch (IOException ioe) {
ioe.printStackTrace();
System.exit(1);
}
}
use of net.i2p.I2PAppContext in project i2p.i2p by i2p.
the class TrustedUpdate method genKeysCLI.
/**
* @return success
*/
private static final boolean genKeysCLI(String publicKeyFile, String privateKeyFile) {
File pubFile = new File(publicKeyFile);
File privFile = new File(privateKeyFile);
if (pubFile.exists()) {
System.out.println("Error: Not overwriting file " + publicKeyFile);
return false;
}
if (privFile.exists()) {
System.out.println("Error: Not overwriting file " + privateKeyFile);
return false;
}
FileOutputStream fileOutputStream = null;
I2PAppContext context = I2PAppContext.getGlobalContext();
try {
Object[] signingKeypair = context.keyGenerator().generateSigningKeypair();
SigningPublicKey signingPublicKey = (SigningPublicKey) signingKeypair[0];
SigningPrivateKey signingPrivateKey = (SigningPrivateKey) signingKeypair[1];
fileOutputStream = new SecureFileOutputStream(pubFile);
signingPublicKey.writeBytes(fileOutputStream);
fileOutputStream.close();
fileOutputStream = null;
fileOutputStream = new SecureFileOutputStream(privFile);
signingPrivateKey.writeBytes(fileOutputStream);
System.out.println("\r\nPrivate key written to: " + privateKeyFile);
System.out.println("Public key written to: " + publicKeyFile);
System.out.println("\r\nPublic key: " + signingPublicKey.toBase64() + "\r\n");
} catch (IOException e) {
System.err.println("Error writing keys:");
e.printStackTrace();
return false;
} catch (DataFormatException e) {
System.err.println("Error writing keys:");
e.printStackTrace();
return false;
} finally {
if (fileOutputStream != null)
try {
fileOutputStream.close();
} catch (IOException ioe) {
}
}
return true;
}
Aggregations