Package org.bouncycastle.openssl.jcajce
Class JcaPrivateKeyReader
- java.lang.Object
-
- org.bouncycastle.openssl.jcajce.JcaPrivateKeyReader
-
public class JcaPrivateKeyReader extends java.lang.ObjectReads a private key in any of the common OpenSSL on-disk forms and converts it to a JCAPrivateKey, transparently decrypting the password-protected variants.The following encodings are recognised, on the PEM side by the type
PEMParserhands back and on the DER side by the structure of the outermost SEQUENCE:- traditional (PKCS#1) RSA keys -
-----BEGIN RSA PRIVATE KEY-----and their DER body; - PKCS#8
PrivateKeyInfo------BEGIN PRIVATE KEY-----and DER; - password-protected traditional keys (
-----BEGIN ... PRIVATE KEY-----with aProc-Type/DEK-Infoheader), decrypted with the supplied password; - encrypted PKCS#8 (
-----BEGIN ENCRYPTED PRIVATE KEY-----and DEREncryptedPrivateKeyInfo), decrypted with the supplied password.
This is the JCA-aware read-side companion to
JcaPKIXIdentityBuilderand lives in the.jcajcepackage because it produces aPrivateKey. It is a convenience overPEMParser/JcaPEMKeyConverteronly - the standards-compliant writers (JcaPKCS8Generator,JcePEMEncryptorBuilder,JceOpenSSLPKCS8EncryptorBuilder) remain the way to emit keys. - traditional (PKCS#1) RSA keys -
-
-
Constructor Summary
Constructors Constructor Description JcaPrivateKeyReader()Base constructor for reading unencrypted keys.JcaPrivateKeyReader(char[] password)Construct a reader carrying the password used to decrypt password-protected keys.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.security.PrivateKeyreadKey(byte[] encoding)Read a private key from a buffer of PEM or DER bytes, auto-detecting the encoding.java.security.PrivateKeyreadKey(java.io.File keyFile)Read a private key from a PEM or DER file, auto-detecting the encoding.java.security.PrivateKeyreadKey(java.io.InputStream keyStream)Read a private key from a stream of PEM or DER bytes, auto-detecting the encoding.java.security.PrivateKeyreadKey(java.io.Reader reader)Read a private key from a PEM reader.JcaPrivateKeyReadersetProvider(java.lang.String providerName)JcaPrivateKeyReadersetProvider(java.security.Provider provider)
-
-
-
Constructor Detail
-
JcaPrivateKeyReader
public JcaPrivateKeyReader()
Base constructor for reading unencrypted keys. Supplying an encrypted key to a reader built this way results in aPEMException.
-
JcaPrivateKeyReader
public JcaPrivateKeyReader(char[] password)
Construct a reader carrying the password used to decrypt password-protected keys. The password is ignored when the key turns out to be unencrypted.- Parameters:
password- the password to decrypt with, or null if no encrypted key is expected.
-
-
Method Detail
-
setProvider
public JcaPrivateKeyReader setProvider(java.security.Provider provider)
-
setProvider
public JcaPrivateKeyReader setProvider(java.lang.String providerName)
-
readKey
public java.security.PrivateKey readKey(java.io.File keyFile) throws java.io.IOExceptionRead a private key from a PEM or DER file, auto-detecting the encoding.- Parameters:
keyFile- the file containing the key.- Returns:
- the recovered private key.
- Throws:
java.io.IOException- on a read, parse, or decryption failure.
-
readKey
public java.security.PrivateKey readKey(java.io.InputStream keyStream) throws java.io.IOExceptionRead a private key from a stream of PEM or DER bytes, auto-detecting the encoding. The stream is fully drained but not closed.- Parameters:
keyStream- the stream containing the key.- Returns:
- the recovered private key.
- Throws:
java.io.IOException- on a read, parse, or decryption failure.
-
readKey
public java.security.PrivateKey readKey(byte[] encoding) throws java.io.IOExceptionRead a private key from a buffer of PEM or DER bytes, auto-detecting the encoding.- Parameters:
encoding- the key bytes.- Returns:
- the recovered private key.
- Throws:
java.io.IOException- on a parse or decryption failure.
-
readKey
public java.security.PrivateKey readKey(java.io.Reader reader) throws java.io.IOExceptionRead a private key from a PEM reader. The reader is consumed but not closed.- Parameters:
reader- a reader positioned at a PEM private key object.- Returns:
- the recovered private key.
- Throws:
java.io.IOException- on a parse or decryption failure.
-
-