Class PEMDecoder
PEMDecoder is a preview API of the Java platform.
PEMDecoder implements a decoder for Privacy-Enhanced Mail (PEM) data.
PEM is a textual encoding used to store and transfer security
objects, such as asymmetric keys, certificates, and certificate revocation
lists (CRLs). It is defined in RFC 1421 and RFC 7468. PEM consists of a
Base64-formatted binary encoding enclosed by a type-identifying header
and footer.
The decode(String) and decode(InputStream)
methods return an instance of a class that matches the data
type and implements DEREncodablePREVIEW.
The following lists the supported PEM types and the DEREncodable
types that each are decoded as:
- CERTIFICATE :
X509Certificate - X509 CRL :
X509CRL - PUBLIC KEY :
PublicKey - PUBLIC KEY :
X509EncodedKeySpec(Only supported when passed as aClassparameter) - PRIVATE KEY :
PrivateKey - PRIVATE KEY :
PKCS8EncodedKeySpec(Only supported when passed as aClassparameter) - PRIVATE KEY :
KeyPair(if the encoding also contains a public key) - ENCRYPTED PRIVATE KEY :
EncryptedPrivateKeyInfo - ENCRYPTED PRIVATE KEY :
PrivateKey(if configured with Decryption) - Other types :
PEMRecord
The PublicKey and PrivateKey types, an algorithm specific
subclass is returned if the underlying algorithm is supported. For example an
ECPublicKey and ECPrivateKey for Elliptic Curve keys.
If the PEM type does not have a corresponding class,
decode(String) and decode(InputStream) will return a
PEMRecordPREVIEW.
The decode(String, Class) and
decode(InputStream, Class) methods take a class parameter
which determines the type of DEREncodable that is returned. These
methods are useful when extracting or changing the return class.
For example, if the PEM contains both public and private keys, the
class parameter can specify which to return. Use
PrivateKey.class to return only the private key.
If the class parameter is set to X509EncodedKeySpec.class, the
public key will be returned in that format. Any type of PEM data can be
decoded into a PEMRecord by specifying PEMRecord.class.
If the class parameter doesn't match the PEM content, a
ClassCastException will be thrown.
A new PEMDecoder instance is created when configured
with withFactory(Provider) and/or
withDecryption(char[]). withFactory(Provider)
configures the decoder to use only KeyFactory and
CertificateFactory instances from the given Provider.
withDecryption(char[]) configures the decoder to decrypt all
encrypted private key PEM data using the given password.
Configuring an instance for decryption does not prevent decoding with
unencrypted PEM. Any encrypted PEM that fails decryption
will throw a RuntimeException. When an encrypted private key PEM is
used with a decoder not configured for decryption, an
EncryptedPrivateKeyInfo object is returned.
This class is immutable and thread-safe.
Here is an example of decoding a PrivateKey object:
PEMDecoder pd = PEMDecoder.of();
PrivateKey priKey = pd.decode(priKeyPEM, PrivateKey.class);
Here is an example of a PEMDecoder configured with decryption
and a factory provider:
PEMDecoder pd = PEMDecoder.of().withDecryption(password).
withFactory(provider);
byte[] pemData = pd.decode(privKey);
- Implementation Note:
- An implementation may support other PEM types and
DEREncodableobjects. This implementation additionally supports the following PEM types:X509 CERTIFICATE,X.509 CERTIFICATE,CRL, andRSA PRIVATE KEY. - Since:
- 25
- External Specifications
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondecode(InputStream is) <S extends DEREncodablePREVIEW>
Sdecode(InputStream is, Class<S> tClass) Decodes and returns the specified class for the givenInputStream.<S extends DEREncodablePREVIEW>
SDecodes and returns aDEREncodableof the specified class from the given PEM string.static PEMDecoderPREVIEWof()Returns an instance ofPEMDecoder.withDecryption(char[] password) Returns a copy of thisPEMDecoderthat decodes and decrypts encrypted private keys using the specified password.withFactory(Provider provider) Returns a copy of thisPEMDecoderinstance that usesKeyFactoryandCertificateFactoryimplementations from the specifiedProviderto produce cryptographic objects.
-
Method Details
-
of
Returns an instance ofPEMDecoder.- Returns:
- a
PEMDecoderinstance
-
decode
Decodes and returns aDEREncodablePREVIEW from the givenString.This method reads the
Stringuntil PEM data is found or the end of theStringis reached. If no PEM data is found, anIllegalArgumentExceptionis thrown.This method returns a Java API cryptographic object, such as a
PrivateKey, if the PEM type is supported. Any non-PEM data preceding the PEM header is ignored by the decoder. Otherwise, aPEMRecordPREVIEW will be returned containing the type identifier and Base64-encoded data. Any non-PEM data preceding the PEM header will be stored inleadingData.Input consumed by this method is read in as
UTF-8.- Parameters:
str- a String containing PEM data- Returns:
- a
DEREncodable - Throws:
IllegalArgumentException- on error in decoding or no PEM data foundNullPointerException- whenstris null
-
decode
Decodes and returns aDEREncodablePREVIEW from the givenInputStream.This method reads from the
InputStreamuntil the end of the PEM footer or the end of the stream. If an I/O error occurs, the read position in the stream may become inconsistent. It is recommended to perform no further decoding operations on theInputStream.This method returns a Java API cryptographic object, such as a
PrivateKey, if the PEM type is supported. Any non-PEM data preceding the PEM header is ignored by the decoder. Otherwise, aPEMRecordPREVIEW will be returned containing the type identifier and Base64-encoded data. Any non-PEM data preceding the PEM header will be stored inleadingData.If no PEM data is found, an
IllegalArgumentExceptionis thrown.- Parameters:
is- InputStream containing PEM data- Returns:
- a
DEREncodable - Throws:
IOException- on IO or PEM syntax error where theInputStreamdid not complete decoding.EOFException- at the end of theInputStreamIllegalArgumentException- on error in decodingNullPointerException- whenisis null
-
decode
Decodes and returns aDEREncodableof the specified class from the given PEM string.tClassmust extendDEREncodablePREVIEW and be an appropriate class for the PEM type.This method reads the
Stringuntil PEM data is found or the end of theStringis reached. If no PEM data is found, anIllegalArgumentExceptionis thrown.If the class parameter is
PEMRecord.class, a PEMRecord is returned containing the type identifier and Base64 encoding. Any non-PEM data preceding the PEM header will be stored inleadingData. Other class parameters will not return preceding non-PEM data.Input consumed by this method is read in as
UTF-8.- Type Parameters:
S- Class type parameter that extendsDEREncodable- Parameters:
str- the String containing PEM datatClass- the returned object class that implementsDEREncodable- Returns:
- a
DEREncodablespecified bytClass - Throws:
IllegalArgumentException- on error in decoding or no PEM data foundClassCastException- iftClassis invalid for the PEM typeNullPointerException- when any input values are null
-
decode
Decodes and returns the specified class for the givenInputStream. The class must extendDEREncodablePREVIEW and be an appropriate class for the PEM type.This method reads from the
InputStreamuntil the end of the PEM footer or the end of the stream. If an I/O error occurs, the read position in the stream may become inconsistent. It is recommended to perform no further decoding operations on theInputStream.If the class parameter is
PEMRecord.class, a PEMRecord is returned containing the type identifier and Base64 encoding. Any non-PEM data preceding the PEM header will be stored inleadingData. Other class parameters will not return preceding non-PEM data.If no PEM data is found, an
IllegalArgumentExceptionis thrown.- Type Parameters:
S- Class type parameter that extendsDEREncodable.- Parameters:
is- an InputStream containing PEM datatClass- the returned object class that implementsDEREncodable.- Returns:
- a
DEREncodabletypecast totClass - Throws:
IOException- on IO or PEM syntax error where theInputStreamdid not complete decoding.EOFException- at the end of theInputStreamIllegalArgumentException- on error in decodingClassCastException- iftClassis invalid for the PEM typeNullPointerException- when any input values are null- See Also:
-
withFactory
Returns a copy of thisPEMDecoderinstance that usesKeyFactoryandCertificateFactoryimplementations from the specifiedProviderto produce cryptographic objects. Any errors using theProviderwill occur during decoding.- Parameters:
provider- the factory provider- Returns:
- a new PEMEncoder instance configured to the
Provider. - Throws:
NullPointerException- ifprovideris null
-
withDecryption
Returns a copy of thisPEMDecoderthat decodes and decrypts encrypted private keys using the specified password. Non-encrypted PEM can still be decoded from this instance.- Parameters:
password- the password to decrypt encrypted PEM data. This array is cloned and stored in the new instance.- Returns:
- a new PEMEncoder instance configured for decryption
- Throws:
NullPointerException- ifpasswordis null
-
PEMDecoderwhen preview features are enabled.