Esempio n. 1
0
  /**
   * Reads configuration entries
   *
   * @param rdr The {@link BufferedReader} to use
   * @return The {@link List} of read {@link KnownHostEntry}-ies
   * @throws IOException If failed to parse the read configuration
   */
  public static List<KnownHostEntry> readKnownHostEntries(BufferedReader rdr) throws IOException {
    List<KnownHostEntry> entries = null;

    int lineNumber = 1;
    for (String line = rdr.readLine(); line != null; line = rdr.readLine(), lineNumber++) {
      line = GenericUtils.trimToEmpty(line);
      if (GenericUtils.isEmpty(line)) {
        continue;
      }

      int pos = line.indexOf(SshConfigFileReader.COMMENT_CHAR);
      if (pos == 0) {
        continue;
      }

      if (pos > 0) {
        line = line.substring(0, pos);
        line = line.trim();
      }

      try {
        KnownHostEntry entry = parseKnownHostEntry(line);
        if (entry == null) {
          continue;
        }

        if (entries == null) {
          entries = new ArrayList<>();
        }
        entries.add(entry);
      } catch (RuntimeException | Error e) { // TODO consider consulting a user callback
        throw new StreamCorruptedException(
            "Failed ("
                + e.getClass().getSimpleName()
                + ")"
                + " to parse line #"
                + lineNumber
                + " '"
                + line
                + "': "
                + e.getMessage());
      }
    }

    if (entries == null) {
      return Collections.emptyList();
    } else {
      return entries;
    }
  }
  @Override
  public Collection<KeyPair> loadKeyPairs(
      String resourceKey, FilePasswordProvider passwordProvider, List<String> lines)
      throws IOException, GeneralSecurityException {
    List<String> pubLines = Collections.emptyList();
    List<String> prvLines = Collections.emptyList();
    String prvEncryption = null;
    for (int index = 0, numLines = lines.size(); index < numLines; index++) {
      String l = lines.get(index);
      l = GenericUtils.trimToEmpty(l);
      int pos = l.indexOf(':');
      if ((pos <= 0) || (pos >= (l.length() - 1))) {
        continue;
      }

      String hdrName = l.substring(0, pos).trim();
      String hdrValue = l.substring(pos + 1).trim();
      switch (hdrName) {
        case ENCRYPTION_HEADER:
          if (prvEncryption != null) {
            throw new StreamCorruptedException("Duplicate " + hdrName + " in" + resourceKey);
          }
          prvEncryption = hdrValue;
          break;
        case PUBLIC_LINES_HEADER:
          pubLines = extractDataLines(resourceKey, lines, index + 1, hdrName, hdrValue, pubLines);
          index += pubLines.size();
          break;
        case PRIVATE_LINES_HEADER:
          prvLines = extractDataLines(resourceKey, lines, index + 1, hdrName, hdrValue, prvLines);
          index += prvLines.size();
          break;
        default: // ignored
      }
    }

    return loadKeyPairs(resourceKey, pubLines, prvLines, prvEncryption, passwordProvider);
  }
  @Override
  public boolean canExtractKeyPairs(String resourceKey, List<String> lines)
      throws IOException, GeneralSecurityException {
    if (!PuttyKeyPairResourceParser.super.canExtractKeyPairs(resourceKey, lines)) {
      return false;
    }

    for (String l : lines) {
      l = GenericUtils.trimToEmpty(l);
      if (!l.startsWith(KEY_FILE_HEADER_PREFIX)) {
        continue;
      }

      int pos = l.indexOf(':');
      if ((pos <= 0) || (pos >= (l.length() - 1))) {
        return false;
      }

      String typeValue = l.substring(pos + 1).trim();
      return Objects.equals(getKeyType(), typeValue);
    }

    return false;
  }