CARVIEW |
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 DEREncodable
PREVIEW.
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 aClass
parameter) - PRIVATE KEY :
PrivateKey
- PRIVATE KEY :
PKCS8EncodedKeySpec
(Only supported when passed as aClass
parameter) - 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
PEMRecord
PREVIEW.
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, an
IllegalArgumentException
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:
PEMEncoder pe = PEMDecoder.of().withDecryption(password).
withFactory(provider);
byte[] pemData = pe.decode(privKey);
- Implementation Note:
- An implementation may support other PEM types and
DEREncodables
. This implementation additionally supports 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 aDEREncodable
of the specified class from the given PEM string.static PEMDecoderPREVIEW
of()
Returns an instance ofPEMDecoder
.withDecryption
(char[] password) Returns a copy of thisPEMDecoder
that decodes and decrypts encrypted private keys using the specified password.withFactory
(Provider provider) Returns a copy of thisPEMDecoder
instance that usesKeyFactory
andCertificateFactory
implementations from the specifiedProvider
to produce cryptographic objects.
-
Method Details
-
of
Returns an instance ofPEMDecoder
.- Returns:
- a
PEMDecoder
instance
-
decode
Decodes and returns aDEREncodable
PREVIEW from the givenString
.This method reads the
String
until PEM data is found or the end of theString
is reached. If no PEM data is found, anIllegalArgumentException
is 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, aPEMRecord
PREVIEW 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
- whenstr
is null
-
decode
Decodes and returns aDEREncodable
PREVIEW from the givenInputStream
.This method reads from the
InputStream
until 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, aPEMRecord
PREVIEW 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
IllegalArgumentException
is thrown.- Parameters:
is
- InputStream containing PEM data- Returns:
- a
DEREncodable
- Throws:
IOException
- on IO or PEM syntax error where theInputStream
did not complete decoding.EOFException
- at the end of theInputStream
IllegalArgumentException
- on error in decodingNullPointerException
- whenis
is null
-
decode
Decodes and returns aDEREncodable
of the specified class from the given PEM string.tClass
must extendDEREncodable
PREVIEW and be an appropriate class for the PEM type.This method reads the
String
until PEM data is found or the end of theString
is reached. If no PEM data is found, anIllegalArgumentException
is 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
DEREncodable
specified bytClass
- Throws:
IllegalArgumentException
- on error in decoding or no PEM data foundClassCastException
- iftClass
is invalid for the PEM typeNullPointerException
- when any input values are null
-
decode
Decodes and returns the specified class for the givenInputStream
. The class must extendDEREncodable
PREVIEW and be an appropriate class for the PEM type.This method reads from the
InputStream
until 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
IllegalArgumentException
is thrown.- Type Parameters:
S
- Class type parameter that extendsDEREncodable
.- Parameters:
is
- an InputStream containing PEM datatClass
- the returned object class that implementsDEREncodable
.- Returns:
- a
DEREncodable
typecast totClass
- Throws:
IOException
- on IO or PEM syntax error where theInputStream
did not complete decoding.EOFException
- at the end of theInputStream
IllegalArgumentException
- on error in decodingClassCastException
- iftClass
is invalid for the PEM typeNullPointerException
- when any input values are null- See Also:
-
withFactory
Returns a copy of thisPEMDecoder
instance that usesKeyFactory
andCertificateFactory
implementations from the specifiedProvider
to produce cryptographic objects. Any errors using theProvider
will occur during decoding.If
provider
isnull
, a new instance is returned with the default provider configuration.- Parameters:
provider
- the factory provider- Returns:
- a new PEMEncoder instance configured to the
Provider
. - Throws:
NullPointerException
- ifprovider
is null
-
withDecryption
Returns a copy of thisPEMDecoder
that 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
- ifpassword
is null
-
PEMDecoder
when preview features are enabled.