예제 #1
0
  private void initialize(char[] knownHostsData) throws IOException {
    BufferedReader br = new BufferedReader(new CharArrayReader(knownHostsData));

    while (true) {
      String line = br.readLine();

      if (line == null) break;

      line = line.trim();

      if (line.startsWith("#")) continue;

      String[] arr = line.split(" ");

      if (arr.length >= 3) {
        if ((arr[1].compareTo("ssh-rsa") == 0) || (arr[1].compareTo("ssh-dss") == 0)) {
          String[] hostnames = arr[0].split(",");

          byte[] msg = Base64.decode(arr[2].toCharArray());

          addHostkey(hostnames, arr[1], msg);
        }
      }
    }
  }
예제 #2
0
  /**
   * Build list of known hosts for Trilead library.
   *
   * @return
   */
  public KnownHosts getKnownHosts() {
    KnownHosts known = new KnownHosts();

    synchronized (dbLock) {
      SQLiteDatabase db = this.getReadableDatabase();
      Cursor c =
          db.query(
              TABLE_HOSTS,
              new String[] {
                FIELD_HOST_HOSTNAME, FIELD_HOST_PORT, FIELD_HOST_HOSTKEYALGO, FIELD_HOST_HOSTKEY
              },
              null,
              null,
              null,
              null,
              null);

      if (c != null) {
        int COL_HOSTNAME = c.getColumnIndexOrThrow(FIELD_HOST_HOSTNAME),
            COL_PORT = c.getColumnIndexOrThrow(FIELD_HOST_PORT),
            COL_HOSTKEYALGO = c.getColumnIndexOrThrow(FIELD_HOST_HOSTKEYALGO),
            COL_HOSTKEY = c.getColumnIndexOrThrow(FIELD_HOST_HOSTKEY);

        while (c.moveToNext()) {
          String hostname = c.getString(COL_HOSTNAME), hostkeyalgo = c.getString(COL_HOSTKEYALGO);
          int port = c.getInt(COL_PORT);
          byte[] hostkey = c.getBlob(COL_HOSTKEY);

          if (hostkeyalgo == null || hostkeyalgo.length() == 0) continue;
          if (hostkey == null || hostkey.length == 0) continue;

          try {
            known.addHostkey(
                new String[] {String.format("%s:%d", hostname, port)}, hostkeyalgo, hostkey);
          } catch (Exception e) {
            Log.e(TAG, "Problem while adding a known host from database", e);
          }
        }

        c.close();
      }
    }

    return known;
  }