Search in sources :

Example 26 with I2PAppContext

use of net.i2p.I2PAppContext in project i2p.i2p by i2p.

the class SU3File method main.

/**
 * Parses command line arguments when this class is used from the command
 * line.
 * Exits 1 on failure so this can be used in scripts.
 *
 * @param args Command line parameters.
 */
public static void main(String[] args) {
    boolean ok = false;
    try {
        // defaults
        String stype = null;
        String ctype = null;
        String ftype = null;
        String kfile = null;
        String crlfile = null;
        String kspass = KeyStoreUtil.DEFAULT_KEYSTORE_PASSWORD;
        boolean error = false;
        boolean shouldVerify = true;
        Getopt g = new Getopt("SU3File", args, "t:c:f:k:xp:r:");
        int c;
        while ((c = g.getopt()) != -1) {
            switch(c) {
                case 't':
                    stype = g.getOptarg();
                    break;
                case 'c':
                    ctype = g.getOptarg();
                    break;
                case 'f':
                    ftype = g.getOptarg();
                    break;
                case 'k':
                    kfile = g.getOptarg();
                    break;
                case 'r':
                    crlfile = g.getOptarg();
                    break;
                case 'x':
                    shouldVerify = false;
                    break;
                case 'p':
                    kspass = g.getOptarg();
                    break;
                case '?':
                case ':':
                default:
                    error = true;
            }
        }
        int idx = g.getOptind();
        String cmd = args[idx];
        List<String> a = new ArrayList<String>(Arrays.asList(args).subList(idx + 1, args.length));
        if (error) {
            showUsageCLI();
        } else if ("showversion".equals(cmd)) {
            ok = showVersionCLI(a.get(0));
        } else if ("sign".equals(cmd)) {
            // speed things up by specifying a small PRNG buffer size
            Properties props = new Properties();
            props.setProperty("prng.bufferSize", "16384");
            new I2PAppContext(props);
            ok = signCLI(stype, ctype, ftype, a.get(0), a.get(1), a.get(2), a.get(3), a.get(4), "", kspass);
        } else if ("bulksign".equals(cmd)) {
            Properties props = new Properties();
            props.setProperty("prng.bufferSize", "16384");
            new I2PAppContext(props);
            ok = bulkSignCLI(stype, ctype, a.get(0), a.get(1), a.get(2), a.get(3), kspass);
        } else if ("verifysig".equals(cmd)) {
            ok = verifySigCLI(a.get(0), kfile);
        } else if ("keygen".equals(cmd)) {
            Properties props = new Properties();
            props.setProperty("prng.bufferSize", "16384");
            new I2PAppContext(props);
            ok = genKeysCLI(stype, a.get(0), a.get(1), crlfile, a.get(2), kspass);
        } else if ("extract".equals(cmd)) {
            ok = extractCLI(a.get(0), a.get(1), shouldVerify, kfile);
        } else {
            showUsageCLI();
        }
    } catch (NoSuchElementException nsee) {
        showUsageCLI();
    } catch (IndexOutOfBoundsException ioobe) {
        showUsageCLI();
    }
    if (!ok)
        System.exit(1);
}
Also used : Getopt(gnu.getopt.Getopt) I2PAppContext(net.i2p.I2PAppContext) ArrayList(java.util.ArrayList) Properties(java.util.Properties) NoSuchElementException(java.util.NoSuchElementException)

Example 27 with I2PAppContext

use of net.i2p.I2PAppContext in project i2p.i2p by i2p.

the class DatabaseEntry method getRoutingKey.

/**
 * Get the routing key for the structure using the current modifier in the RoutingKeyGenerator.
 * This only calculates a new one when necessary though (if the generator's key modifier changes)
 *
 * @throws IllegalStateException if not in RouterContext
 */
public Hash getRoutingKey() {
    I2PAppContext ctx = I2PAppContext.getGlobalContext();
    if (!ctx.isRouterContext())
        throw new IllegalStateException("Not in router context");
    RoutingKeyGenerator gen = ctx.routingKeyGenerator();
    long mod = gen.getLastChanged();
    if (mod != _routingKeyGenMod) {
        _currentRoutingKey = gen.getRoutingKey(getHash());
        _routingKeyGenMod = mod;
    }
    return _currentRoutingKey;
}
Also used : I2PAppContext(net.i2p.I2PAppContext)

Example 28 with I2PAppContext

use of net.i2p.I2PAppContext in project i2p.i2p by i2p.

the class DatabaseEntry method validateRoutingKey.

/**
 * @throws IllegalStateException if not in RouterContext
 */
public boolean validateRoutingKey() {
    I2PAppContext ctx = I2PAppContext.getGlobalContext();
    if (!ctx.isRouterContext())
        throw new IllegalStateException("Not in router context");
    RoutingKeyGenerator gen = ctx.routingKeyGenerator();
    Hash destKey = getHash();
    Hash rk = gen.getRoutingKey(destKey);
    return rk.equals(getRoutingKey());
}
Also used : I2PAppContext(net.i2p.I2PAppContext)

Example 29 with I2PAppContext

use of net.i2p.I2PAppContext in project i2p.i2p by i2p.

the class KeyStoreUtil method randomString.

/**
 * 48 char b32 string (30 bytes of entropy)
 */
public static String randomString() {
    I2PAppContext ctx = I2PAppContext.getGlobalContext();
    // make a random 48 character password (30 * 8 / 5)
    byte[] rand = new byte[30];
    ctx.random().nextBytes(rand);
    return Base32.encode(rand);
}
Also used : I2PAppContext(net.i2p.I2PAppContext)

Example 30 with I2PAppContext

use of net.i2p.I2PAppContext in project i2p.i2p by i2p.

the class WebMail method processComposeButtons.

/**
 * process buttons of compose message dialog
 * This must be called BEFORE processStateChangeButtons so we can add the attachment before SEND
 *
 * @param sessionObject
 * @param request
 * @return new state, or null if unknown
 */
private static State processComposeButtons(SessionObject sessionObject, RequestWrapper request) {
    State state = null;
    String filename = request.getFilename(NEW_FILENAME);
    // We handle an attachment whether sending or uploading
    if (filename != null && (buttonPressed(request, NEW_UPLOAD) || buttonPressed(request, SEND))) {
        int i = filename.lastIndexOf('/');
        if (i != -1)
            filename = filename.substring(i + 1);
        i = filename.lastIndexOf('\\');
        if (i != -1)
            filename = filename.substring(i + 1);
        if (filename != null && filename.length() > 0) {
            InputStream in = null;
            OutputStream out = null;
            I2PAppContext ctx = I2PAppContext.getGlobalContext();
            File f = new File(ctx.getTempDir(), "susimail-attachment-" + ctx.random().nextLong());
            try {
                in = request.getInputStream(NEW_FILENAME);
                if (in == null)
                    throw new IOException("no stream");
                out = new SecureFileOutputStream(f);
                DataHelper.copy(in, out);
                String contentType = request.getContentType(NEW_FILENAME);
                String encodeTo;
                String ctlc = contentType.toLowerCase(Locale.US);
                if (ctlc.startsWith("text/")) {
                    encodeTo = "quoted-printable";
                    // interpret it as ISO-8859-1
                    if (!ctlc.contains("charset="))
                        contentType += "; charset=\"utf-8\"";
                } else {
                    encodeTo = "base64";
                }
                Encoding encoding = EncodingFactory.getEncoding(encodeTo);
                if (encoding != null) {
                    if (sessionObject.attachments == null)
                        sessionObject.attachments = new ArrayList<Attachment>();
                    sessionObject.attachments.add(new Attachment(filename, contentType, encodeTo, f));
                } else {
                    sessionObject.error += _t("No Encoding found for {0}", encodeTo) + '\n';
                }
            } catch (IOException e) {
                sessionObject.error += _t("Error reading uploaded file: {0}", e.getMessage()) + '\n';
                f.delete();
            } finally {
                if (in != null)
                    try {
                        in.close();
                    } catch (IOException ioe) {
                    }
                if (out != null)
                    try {
                        out.close();
                    } catch (IOException ioe) {
                    }
            }
        }
        state = State.NEW;
    } else if (sessionObject.attachments != null && buttonPressed(request, DELETE_ATTACHMENT)) {
        for (String item : getCheckedItems(request)) {
            try {
                int n = Integer.parseInt(item);
                for (int i = 0; i < sessionObject.attachments.size(); i++) {
                    Attachment attachment = sessionObject.attachments.get(i);
                    if (attachment.hashCode() == n) {
                        sessionObject.attachments.remove(i);
                        break;
                    }
                }
            } catch (NumberFormatException nfe) {
            }
        }
        state = State.NEW;
    }
    return state;
}
Also used : I2PAppContext(net.i2p.I2PAppContext) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) DecodingOutputStream(i2p.susi.util.DecodingOutputStream) WriterOutputStream(net.i2p.servlet.util.WriterOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) EscapeHTMLOutputStream(i2p.susi.util.EscapeHTMLOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) Encoding(i2p.susi.webmail.encoding.Encoding) IOException(java.io.IOException) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) File(java.io.File)

Aggregations

I2PAppContext (net.i2p.I2PAppContext)55 SessionKey (net.i2p.data.SessionKey)13 File (java.io.File)11 IOException (java.io.IOException)11 Properties (java.util.Properties)9 Test (org.junit.Test)7 Getopt (gnu.getopt.Getopt)5 ArrayList (java.util.ArrayList)5 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 InputStream (java.io.InputStream)3 DataFormatException (net.i2p.data.DataFormatException)3 Log (net.i2p.util.Log)3 SecureFileOutputStream (net.i2p.util.SecureFileOutputStream)3 Encoding (i2p.susi.webmail.encoding.Encoding)2 OutputStream (java.io.OutputStream)2 GeneralSecurityException (java.security.GeneralSecurityException)2 HashSet (java.util.HashSet)2 I2PSession (net.i2p.client.I2PSession)2 NamingService (net.i2p.client.naming.NamingService)2