public Collection<KeyPair> loadKeyPairs( String resourceKey, List<String> pubLines, List<String> prvLines, String prvEncryption, FilePasswordProvider passwordProvider) throws IOException, GeneralSecurityException { return loadKeyPairs(resourceKey, KeyPairResourceParser.joinDataLines(pubLines), KeyPairResourceParser.joinDataLines(prvLines), prvEncryption, passwordProvider); }
@Override public Collection<KeyPair> loadKeyPairs( SessionContext session, NamedResource resourceKey, FilePasswordProvider passwordProvider, List<String> lines) throws IOException, GeneralSecurityException { @SuppressWarnings("synthetic-access") KeyPairResourceParser proxy = PROXY_HOLDER.get(); return (proxy == null) ? Collections.emptyList() : proxy.loadKeyPairs(session, resourceKey, passwordProvider, lines); }
public static KeyPairResourceParser getKeyPairResourceParser() { KeyPairResourceParser parser; synchronized (KEYPAIRS_PARSER_HODLER) { parser = KEYPAIRS_PARSER_HODLER.get(); if (parser != null) { return parser; } parser = KeyPairResourceParser.aggregate( PEMResourceParserUtils.PROXY, OpenSSHKeyPairResourceParser.INSTANCE); KEYPAIRS_PARSER_HODLER.set(parser); } return parser; }
@Override public Collection<KeyPair> loadKeyPairs(String resourceKey, FilePasswordProvider passwordProvider, List<String> lines) throws IOException, GeneralSecurityException { Collection<KeyPair> keyPairs = Collections.emptyList(); for (KeyPairResourceParser p : parsers) { if (!p.canExtractKeyPairs(resourceKey, lines)) { continue; } Collection<KeyPair> kps = p.loadKeyPairs(resourceKey, passwordProvider, lines); if (GenericUtils.isEmpty(kps)) { continue; } if (GenericUtils.isEmpty(keyPairs)) { keyPairs = new LinkedList<>(kps); } else { keyPairs.addAll(kps); } } return keyPairs; }
/** * Attempts to locate a line that contains one of the markers * * @param lines The list of lines to scan - ignored if {@code null}/empty * @param markers The markers to match - ignored if {@code null}/empty * @return A {@link SimpleImmutableEntry} whose key is the <U>first</U> line index * that matched and value the matched marker index - {@code null} if no match found * @see #findMarkerLine(List, int, List) */ static SimpleImmutableEntry<Integer, Integer> findMarkerLine(List<String> lines, List<String> markers) { return findMarkerLine(lines, 0, markers); }
@Override public boolean canExtractKeyPairs(String resourceKey, List<String> lines) throws IOException, GeneralSecurityException { for (KeyPairResourceParser p : parsers) { if (p.canExtractKeyPairs(resourceKey, lines)) { return true; } } return false; }
@Override public boolean canExtractKeyPairs(NamedResource resourceKey, List<String> lines) throws IOException, GeneralSecurityException { return KeyPairResourceParser.containsMarkerLine(lines, getBeginners()); }
/** * Extracts the key pairs within a <U>single</U> delimited by markers block of lines. By * default cleans up the empty lines, joins them and converts them from BASE64 * * @param resourceKey A hint as to the origin of the text lines * @param beginMarker The line containing the begin marker * @param endMarker The line containing the end marker * @param passwordProvider The {@link FilePasswordProvider} to use * in case the data is encrypted - may be {@code null} if no encrypted * @param lines The block of lines between the markers * @return The extracted {@link KeyPair}s - may be {@code null}/empty if none. * @throws IOException If failed to parse the data * @throws GeneralSecurityException If failed to generate the keys * @see #extractKeyPairs(String, String, String, FilePasswordProvider, byte[]) */ public Collection<KeyPair> extractKeyPairs( String resourceKey, String beginMarker, String endMarker, FilePasswordProvider passwordProvider, List<String> lines) throws IOException, GeneralSecurityException { return extractKeyPairs(resourceKey, beginMarker, endMarker, passwordProvider, KeyPairResourceParser.extractDataBytes(lines)); }
@Override public Collection<KeyPair> loadKeyPairs( SessionContext session, NamedResource resourceKey, FilePasswordProvider passwordProvider, List<String> lines) throws IOException, GeneralSecurityException { Collection<KeyPair> keyPairs = Collections.emptyList(); for (KeyPairResourceParser p : parsers) { if (!p.canExtractKeyPairs(resourceKey, lines)) { continue; } Collection<KeyPair> kps = p.loadKeyPairs(session, resourceKey, passwordProvider, lines); if (GenericUtils.isEmpty(kps)) { continue; } if (GenericUtils.isEmpty(keyPairs)) { keyPairs = new LinkedList<>(kps); } else { keyPairs.addAll(kps); } } return keyPairs; }
/** * Attempts to locate a line that contains one of the markers * * @param lines The list of lines to scan - ignored if {@code null}/empty * @param markers The markers to match - ignored if {@code null}/empty * @return A {@link SimpleImmutableEntry} whose key is the <U>first</U> line index * that matched and value the matched marker index - {@code null} if no match found * @see #findMarkerLine(List, int, List) */ static SimpleImmutableEntry<Integer, Integer> findMarkerLine(List<String> lines, List<String> markers) { return findMarkerLine(lines, 0, markers); }
@Override public boolean canExtractKeyPairs(NamedResource resourceKey, List<String> lines) throws IOException, GeneralSecurityException { for (KeyPairResourceParser p : parsers) { if (p.canExtractKeyPairs(resourceKey, lines)) { return true; } } return false; }
static boolean containsMarkerLine(List<String> lines, String marker) { return containsMarkerLine( lines, Collections.singletonList(ValidateUtils.checkNotNullAndNotEmpty(marker, "No marker"))); }
/** * Extracts the key pairs within a <U>single</U> delimited by markers block of lines. By * default cleans up the empty lines, joins them and converts them from BASE64 * * @param session The {@link SessionContext} for invoking this load command - may * be {@code null} if not invoked within a session context (e.g., offline tool or session unknown). * @param resourceKey A hint as to the origin of the text lines * @param beginMarker The line containing the begin marker * @param endMarker The line containing the end marker * @param passwordProvider The {@link FilePasswordProvider} to use * in case the data is encrypted - may be {@code null} if no encrypted * @param lines The block of lines between the markers * @return The extracted {@link KeyPair}s - may be {@code null}/empty if none. * @throws IOException If failed to parse the data * @throws GeneralSecurityException If failed to generate the keys */ public Collection<KeyPair> extractKeyPairs( SessionContext session, NamedResource resourceKey, String beginMarker, String endMarker, FilePasswordProvider passwordProvider, List<String> lines) throws IOException, GeneralSecurityException { byte[] dataBytes = KeyPairResourceParser.extractDataBytes(lines); try { return extractKeyPairs(session, resourceKey, beginMarker, endMarker, passwordProvider, dataBytes); } finally { Arrays.fill(dataBytes, (byte) 0); // clean up sensitive data a.s.a.p. } }
@Override public Collection<KeyPair> loadKeyPairs( String resourceKey, FilePasswordProvider passwordProvider, List<String> lines) throws IOException, GeneralSecurityException { @SuppressWarnings("synthetic-access") KeyPairResourceParser proxy = PROXY_HOLDER.get(); return (proxy == null) ? Collections.<KeyPair>emptyList() : proxy.loadKeyPairs(resourceKey, passwordProvider, lines); }
/** * Converts the lines assumed to contain BASE-64 encoded data into * the actual content bytes. * * @param lines The data lines - empty lines and spaces are automatically * deleted <U>before</U> BASE-64 decoding takes place. * @return The decoded data bytes * @see #joinDataLines(Collection) */ static byte[] extractDataBytes(Collection<String> lines) { String data = joinDataLines(lines); Base64.Decoder decoder = Base64.getDecoder(); return decoder.decode(data); }
static boolean containsMarkerLine(List<String> lines, List<String> markers) { return findMarkerLine(lines, markers) != null; }
public static KeyPairResourceParser getKeyPairResourceParser() { KeyPairResourceParser parser; synchronized (KEYPAIRS_PARSER_HODLER) { parser = KEYPAIRS_PARSER_HODLER.get(); if (parser != null) { return parser; } parser = KeyPairResourceParser.aggregate( PEMResourceParserUtils.PROXY, OpenSSHKeyPairResourceParser.INSTANCE); KEYPAIRS_PARSER_HODLER.set(parser); } return parser; }
@Override public boolean canExtractKeyPairs(NamedResource resourceKey, List<String> lines) throws IOException, GeneralSecurityException { @SuppressWarnings("synthetic-access") KeyPairResourceParser proxy = PROXY_HOLDER.get(); return (proxy != null) && proxy.canExtractKeyPairs(resourceKey, lines); } };
@Override public boolean canExtractKeyPairs(String resourceKey, List<String> lines) throws IOException, GeneralSecurityException { return KeyPairResourceParser.containsMarkerLine(lines, getBeginners()); }
encContext.setPassword(password); encContext.setInitVector(initVector); byte[] encryptedData = KeyPairResourceParser.extractDataBytes(dataLines); byte[] decodedData = applyPrivateKeyCipher(encryptedData, encContext, false); try (InputStream bais = new ByteArrayInputStream(decodedData)) {