public static KeyPair loadKey(String pemString, String passwd) throws IOException, GeneralSecurityException { Object key = PEMDecoder.decode(pemString.toCharArray(), passwd); if (key instanceof com.trilead.ssh2.signature.RSAPrivateKey) { com.trilead.ssh2.signature.RSAPrivateKey x = (com.trilead.ssh2.signature.RSAPrivateKey)key; return x.toJCEKeyPair(); } if (key instanceof com.trilead.ssh2.signature.DSAPrivateKey) { com.trilead.ssh2.signature.DSAPrivateKey x = (com.trilead.ssh2.signature.DSAPrivateKey)key; KeyFactory kf = KeyFactory.getInstance("DSA"); return new KeyPair( kf.generatePublic(new DSAPublicKeySpec(x.getY(), x.getP(), x.getQ(), x.getG())), kf.generatePrivate(new DSAPrivateKeySpec(x.getX(), x.getP(), x.getQ(), x.getG()))); } throw new UnsupportedOperationException("Unrecognizable key format: " + key); }
public DSAPublicKey getPublicKey() { return new DSAPublicKey(p, q, g, y); } }
public RSAPublicKey getPublicKey() { return new RSAPublicKey(e, n); } }
private boolean verifySignature(byte[] sig, byte[] hostkey) throws IOException { for (KeyAlgorithm<PublicKey, PrivateKey> algorithm : KeyAlgorithmManager.getSupportedAlgorithms()) { if (algorithm.getKeyFormat().equals(kxs.np.server_host_key_algo)) { PublicKey publicKey = algorithm.decodePublicKey(hostkey); byte[] signature = algorithm.decodeSignature(sig); return algorithm.verifySignature(kxs.H, signature, publicKey); } } throw new IOException("Unknown server host key algorithm '" + kxs.np.server_host_key_algo + "'"); }
private static List<String> buildDefaultKeyAlgorithms() { List<String> algorithms = new ArrayList<>(); for (KeyAlgorithm<?, ?> algorithm : KeyAlgorithmManager.getSupportedAlgorithms()) { algorithms.add(algorithms.size(), algorithm.getKeyFormat()); } return algorithms; }
private static Collection<KeyAlgorithm<PublicKey, PrivateKey>> buildSupportAlgorithmsList() { List<KeyAlgorithm<?, ?>> algorithms = new ArrayList<>(); algorithms.add(new ED25519KeyAlgorithm()); try { KeyFactory.getInstance("EC"); algorithms.add(new ECDSAKeyAlgorithm.ECDSASha2Nistp521()); algorithms.add(new ECDSAKeyAlgorithm.ECDSASha2Nistp384()); algorithms.add(new ECDSAKeyAlgorithm.ECDSASha2Nistp256()); } catch (GeneralSecurityException ex) { // we don't use ECDSA algorithms in this case } algorithms.add(new RSAKeyAlgorithm()); algorithms.add(new DSAKeyAlgorithm()); return (Collection) Collections.unmodifiableCollection(algorithms); } }
private PublicKey decodeHostKey(String hostKeyAlgorithm, byte[] encodedHostKey) throws IOException { for (KeyAlgorithm<PublicKey, PrivateKey> algorithm : KeyAlgorithmManager.getSupportedAlgorithms()) { if (algorithm.getKeyFormat().equals(hostKeyAlgorithm)) { return algorithm.decodePublicKey(encodedHostKey); } } throw new IllegalArgumentException("Unknown hostkey type " + hostKeyAlgorithm); }
@Override public List<CertificateDecoder> getCertificateDecoders() { return Arrays.asList(new EcdsaCertificateDecoder("1.3.132.0.35", getEcParameterSpec()), new OpenSshEcdsaCertificateDecoder(getKeyFormat(), getCurveName(), getEcParameterSpec())); }
@Override public List<CertificateDecoder> getCertificateDecoders() { return Arrays.asList(new EcdsaCertificateDecoder("1.2.840.10045.3.1.7", getEcParameterSpec()), new OpenSshEcdsaCertificateDecoder(getKeyFormat(), getCurveName(), getEcParameterSpec())); } }
@Override public List<CertificateDecoder> getCertificateDecoders() { return Arrays.asList(new EcdsaCertificateDecoder("1.3.132.0.34", getEcParameterSpec()), new OpenSshEcdsaCertificateDecoder(getKeyFormat(), getCurveName(), getEcParameterSpec())); } }
public static void checkServerHostkeyAlgorithmsList(String[] algos) { for (String algo : algos) { boolean matched = false; for (KeyAlgorithm<?, ?> algorithm : KeyAlgorithmManager.getSupportedAlgorithms()) { if (algorithm.getKeyFormat().equals(algo)) { matched = true; break; } } if (!matched) { throw new IllegalArgumentException("Unknown server host key algorithm '" + algo + "'"); } } }
public DSAPublicKey getPublicKey() { return new DSAPublicKey(p, q, g, y); } }
public RSAPublicKey getPublicKey() { return new RSAPublicKey(e, n); } }
@Override public String[] getSupportedAlgorithms() { List<String> algorithms = new ArrayList<>(); for (KeyAlgorithm<?, ?> algorithm : KeyAlgorithmManager.getSupportedAlgorithms()) { algorithms.add(algorithm.getKeyFormat()); } return algorithms.toArray(new String[0]); }
public DSAPublicKey getPublicKey() { return new DSAPublicKey(p, q, g, y); } }
public RSAPublicKey getPublicKey() { return new RSAPublicKey(e, n); }
@Override public String[] getSupportedAlgorithms() { List<String> algorithms = new ArrayList<>(); for (KeyAlgorithm<?, ?> algorithm : KeyAlgorithmManager.getSupportedAlgorithms()) { algorithms.add(algorithm.getKeyFormat()); } return algorithms.toArray(new String[algorithms.size()]); }