コード例 #1
0
ファイル: KeyStoreUtil.java プロジェクト: Juiceman/i2p.i2p
 /** 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);
 }
コード例 #2
0
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    if (!validPassphrase()) return;

    response.setContentType("text/html");
    PrintWriter out = null;
    try {
      out = response.getWriter();
      out.println("<html><head><title>Addresses</title></head>");
      out.println("<form method=POST>");
      out.println("<table>");
      for (Record record : RecordIndex.getInstance()) {
        out.println("<tr>");
        out.println(
            "<td><input name=\"checked\" type=\"checkbox\" value=\""
                + Long.toHexString(record.id)
                + "\" /></td>");
        out.println(" <td>" + record.getName() + "</td>");
        out.println(" <td>" + formatter.format(record.getModified()) + "</td>");
        out.println(" <td>" + record.getAddress().toBase64() + "</td>");
        out.println("</tr>");
      }
      out.println("</table>");

      out.println("<input name=\"delete\" type=\"submit\" value=\"Delete Selected\" />");
      out.println("</form>");
      out.println("<h3>Deleted addresses:</h3>");
      out.println("<form method=POST>");
      out.println("<table>");
      for (Hash hash : blacklist) {
        String shash = Base32.encode(hash.getData());
        out.println("<tr>");
        out.println(
            "  <td><input name=\"checked\" type=\"checkbox\" value=\"" + shash + "\" /></td>");
        out.println("  <td>" + shash + "</td>");
        out.println("</tr>");
      }
      out.println("</table>");

      out.println("<input type=\"submit\" name=\"submit\" value=\"Undelete Selected\" />");

      out.println("</form></body></html>");
    } finally {
      if (out != null) out.close();
    }
  }
コード例 #3
0
 /** Base64 Hash or Hash.i2p or name.i2p using naming service */
 Destination getDestination(String ip) {
   if (ip == null) return null;
   if (ip.endsWith(".i2p")) {
     if (ip.length() < 520) { // key + ".i2p"
       if (_manager != null && ip.length() == BASE32_HASH_LENGTH + 8 && ip.endsWith(".b32.i2p")) {
         // Use existing I2PSession for b32 lookups if we have it
         // This is much more efficient than using the naming service
         I2PSession sess = _manager.getSession();
         if (sess != null) {
           byte[] b = Base32.decode(ip.substring(0, BASE32_HASH_LENGTH));
           if (b != null) {
             // Hash h = new Hash(b);
             Hash h = Hash.create(b);
             if (_log.shouldLog(Log.INFO)) _log.info("Using existing session for lookup of " + ip);
             try {
               return sess.lookupDest(h, 15 * 1000);
             } catch (I2PSessionException ise) {
             }
           }
         }
       }
       if (_log.shouldLog(Log.INFO)) _log.info("Using naming service for lookup of " + ip);
       return _context.namingService().lookup(ip);
     }
     if (_log.shouldLog(Log.INFO)) _log.info("Creating Destination for " + ip);
     try {
       return new Destination(ip.substring(0, ip.length() - 4)); // sans .i2p
     } catch (DataFormatException dfe) {
       return null;
     }
   } else {
     if (_log.shouldLog(Log.INFO)) _log.info("Creating Destination for " + ip);
     try {
       return new Destination(ip);
     } catch (DataFormatException dfe) {
       return null;
     }
   }
 }
コード例 #4
0
  /**
   * Call out to keytool to create a new keystore with a keypair in it. Trying to do this
   * programatically is a nightmare, requiring either BouncyCastle libs or using proprietary Sun
   * libs, and it's a huge mess.
   *
   * @return success
   * @since 0.8.3
   */
  private boolean createKeyStore(File ks) {
    // make a random 48 character password (30 * 8 / 5)
    byte[] rand = new byte[30];
    _context.random().nextBytes(rand);
    String keyPassword = Base32.encode(rand);
    // and one for the cname
    _context.random().nextBytes(rand);
    String cname = Base32.encode(rand) + ".console.i2p.net";

    String keytool = (new File(System.getProperty("java.home"), "bin/keytool")).getAbsolutePath();
    String[] args =
        new String[] {
          keytool,
          "-genkey", // -genkeypair preferred in newer keytools, but this works with more
          "-storetype",
          KeyStore.getDefaultType(),
          "-keystore",
          ks.getAbsolutePath(),
          "-storepass",
          DEFAULT_KEYSTORE_PASSWORD,
          "-alias",
          "console",
          "-dname",
          "CN=" + cname + ",OU=Console,O=I2P Anonymous Network,L=XX,ST=XX,C=XX",
          "-validity",
          "3652", // 10 years
          "-keyalg",
          "DSA",
          "-keysize",
          "1024",
          "-keypass",
          keyPassword
        };
    boolean success = (new ShellCommand()).executeSilentAndWaitTimed(args, 30); // 30 secs
    if (success) {
      success = ks.exists();
      if (success) {
        SecureFileOutputStream.setPerms(ks);
        try {
          Map<String, String> changes = new HashMap();
          changes.put(PROP_KEYSTORE_PASSWORD, DEFAULT_KEYSTORE_PASSWORD);
          changes.put(PROP_KEY_PASSWORD, keyPassword);
          _context.router().saveConfig(changes, null);
        } catch (Exception e) {
        } // class cast exception
      }
    }
    if (success) {
      System.err.println(
          "Created self-signed certificate for "
              + cname
              + " in keystore: "
              + ks.getAbsolutePath()
              + "\n"
              + "The certificate name was generated randomly, and is not associated with your "
              + "IP address, host name, router identity, or destination keys.");
    } else {
      System.err.println("Failed to create console SSL keystore using command line:");
      StringBuilder buf = new StringBuilder(256);
      for (int i = 0; i < args.length; i++) {
        buf.append('"').append(args[i]).append("\" ");
      }
      System.err.println(buf.toString());
      System.err.println(
          "This is for the Sun/Oracle keytool, others may be incompatible.\n"
              + "If you create the keystore manually, you must add "
              + PROP_KEYSTORE_PASSWORD
              + " and "
              + PROP_KEY_PASSWORD
              + " to "
              + (new File(_context.getConfigDir(), "router.config")).getAbsolutePath());
    }
    return success;
  }