/** * Reads a DER or PEM-encoded public key from a file. * * @param path Path to DER or PEM-encoded public key file. * * @return Public key. * * @throws EncodingException on key encoding errors. * @throws StreamException on IO errors. */ public static PublicKey readPublicKey(final String path) throws EncodingException, StreamException { return readPublicKey(new File(path)); }
/** * Decodes an encoded private key in either PKCS#8 or OpenSSL "traditional" format in either DER or PEM encoding. Keys * from the following asymmetric algorithms are supported: * * <ul> * <li>DSA</li> * <li>RSA</li> * <li>Elliptic curve</li> * </ul> * * @param encodedKey Encoded private key data. * * @return Private key. * * @throws EncodingException on key encoding errors. */ public static PrivateKey decodePrivateKey(final byte[] encodedKey) throws EncodingException { return decodePrivateKey(encodedKey, null); }
@Override public PublicKey newInstance() throws EncodingException { return KeyPairUtil.decodePublicKey(PemUtil.decode(encodedKey)); } }
/** * Reads an encoded private key from a file at the given path. Both PKCS#8 and OpenSSL "traditional" formats are * supported in DER or PEM encoding. See {@link #decodePrivateKey(byte[])} for supported asymmetric algorithms. * * @param path Path to private key file. * * @return Private key. * * @throws EncodingException on key encoding errors. * @throws StreamException on IO errors reading data from file. */ public static PrivateKey readPrivateKey(final String path) throws EncodingException, StreamException { return readPrivateKey(new File(path)); }
/** * Finds a certificate whose public key is paired with the given private key. * * @param key Private key used to find matching public key. * @param candidates Collection of candidate certificates. * * @return Certificate whose public key forms a keypair with the private key or null if no match is found. * * @throws EncodingException on cert field extraction. */ public static X509Certificate findEntityCertificate( final PrivateKey key, final Collection<X509Certificate> candidates) throws EncodingException { for (X509Certificate candidate : candidates) { if (KeyPairUtil.isKeyPair(candidate.getPublicKey(), key)) { return candidate; } } return null; }
/** * Reads an encrypted private key from a file at the given path. Both PKCS#8 and OpenSSL "traditional" formats are * supported in DER or PEM encoding. See {@link #decodePrivateKey(byte[])} for supported asymmetric algorithms. * * @param path Path to private key file. * @param password Password used to encrypt private key. * * @return Private key. * * @throws EncodingException on key encoding errors. * @throws StreamException on IO errors. */ public static PrivateKey readPrivateKey(final String path, final char[] password) throws EncodingException, StreamException { return readPrivateKey(new File(path), password); }
/** {@inheritDoc} */ @Override public PublicKey getObject() throws Exception { if (key == null) { if (resource == null) { throw new BeanCreationException("Public key resource must be provided in order to use this factory."); } try (InputStream is = resource.getInputStream()) { key = KeyPairUtil.readPublicKey(is); } } return key; }
/** {@inheritDoc} */ @Override public PrivateKey getObject() throws Exception { if (key == null) { if (resource == null) { throw new BeanCreationException("Private key resource must be provided in order to use this factory."); } try (InputStream is = resource.getInputStream()) { if (keyPass == null) { key = KeyPairUtil.readPrivateKey(is); } else { key = KeyPairUtil.readPrivateKey(is, keyPass.toCharArray()); } } } return key; }
/** {@inheritDoc} */ @Override @Nullable protected PrivateKey getPrivateKey() { if (null == privateKey) { return null; } return KeyPairUtil.decodePrivateKey(privateKey, getPrivateKeyPassword()); }
/** * Reads a DER or PEM-encoded public key from data in the given stream. The {@link InputStream} parameter is closed by * this method. * * @param in Input stream containing an encoded key. * * @return Public key. * * @throws EncodingException on key encoding errors. * @throws StreamException on IO errors. */ public static PublicKey readPublicKey(final InputStream in) throws EncodingException, StreamException { return decodePublicKey(StreamUtil.readAll(in)); }
/** * Reads a DER or PEM-encoded public key from a file. * * @param file DER or PEM-encoded public key file. * * @return Public key. * * @throws EncodingException on key encoding errors. * @throws StreamException on IO errors. */ public static PublicKey readPublicKey(final File file) throws EncodingException, StreamException { try { return readPublicKey(new FileInputStream(file)); } catch (FileNotFoundException e) { throw new StreamException("File not found: " + file); } }
@Override public PrivateKey newInstance() throws EncodingException, StreamException { try { if (password != null) { return KeyPairUtil.readPrivateKey(resource.getInputStream(), password.toCharArray()); } return KeyPairUtil.readPrivateKey(resource.getInputStream()); } catch (IOException e) { throw new StreamException(e); } } }
@Override public PrivateKey newInstance() throws EncodingException { return KeyPairUtil.decodePrivateKey(PemUtil.decode(encodedKey)); } }
/** {@inheritDoc} */ @Override @Nullable protected PublicKey getPublicKey() { if (null == getPublicKeyInfo()) { return null; } return KeyPairUtil.decodePublicKey(getPublicKeyInfo()); }
@Override public PublicKey newInstance() throws EncodingException, StreamException { try { return KeyPairUtil.readPublicKey(resource.getInputStream()); } catch (IOException e) { throw new StreamException(e); } } }
/** * Reads an encrypted private key from a file. Both PKCS#8 and OpenSSL "traditional" formats are supported in DER or * PEM encoding. See {@link #decodePrivateKey(byte[])} for supported asymmetric algorithms. * * @param file Private key file. * @param password Password used to encrypt private key. * * @return Private key. * * @throws EncodingException on key encoding errors. * @throws StreamException on IO errors. */ public static PrivateKey readPrivateKey(final File file, final char[] password) throws EncodingException, StreamException { try { return readPrivateKey(new FileInputStream(file), password); } catch (FileNotFoundException e) { throw new StreamException("File not found: " + file); } }
/** * Reads an encoded private key from an input stream. Both PKCS#8 and OpenSSL "traditional" formats are supported in * DER or PEM encoding. See {@link #decodePrivateKey(byte[])} for supported asymmetric algorithms. The {@link * InputStream} parameter is closed by this method. * * @param in Input stream containing private key data. * * @return Private key. * * @throws EncodingException on key encoding errors. * @throws StreamException on IO errors reading data from file. */ public static PrivateKey readPrivateKey(final InputStream in) throws EncodingException, StreamException { return decodePrivateKey(StreamUtil.readAll(in)); }
/** {@inheritDoc} */ @Override @Nullable protected PublicKey getPublicKey() { if (null == getPublicKeyInfo()) { return null; } try (InputStream is = getPublicKeyInfo().getInputStream()) { return KeyPairUtil.readPublicKey(is); } catch (final IOException e) { log.error("{}: Could not decode public key", getConfigDescription(), e); throw new FatalBeanException("Could not decode public key", e); } }
/** * Reads an encoded private key from a file. Both PKCS#8 and OpenSSL "traditional" formats are supported in DER or PEM * encoding. See {@link #decodePrivateKey(byte[])} for supported asymmetric algorithms. * * @param file Private key file. * * @return Private key. * * @throws EncodingException on key encoding errors. * @throws StreamException on IO errors reading data from file. */ public static PrivateKey readPrivateKey(final File file) throws EncodingException, StreamException { try { return readPrivateKey(new FileInputStream(file)); } catch (FileNotFoundException e) { throw new StreamException("File not found: " + file); } }