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 cryptographic
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-encoded 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 PEM type and implements
DEREncodablePREVIEW, as follows:
- CERTIFICATE :
X509Certificate - X509 CRL :
X509CRL - PUBLIC KEY :
PublicKey - PRIVATE KEY :
PrivateKeyorKeyPair(if the encoding contains a public key) - ENCRYPTED PRIVATE KEY :
EncryptedPrivateKeyInfo - Other types :
PEMPREVIEW
PEMDecoder instance configured for decryption:
- ENCRYPTED PRIVATE KEY :
PrivateKeyorKeyPair(if the encoding contains a public key)
For PublicKey and PrivateKey types, an algorithm-specific
subclass is returned if the algorithm is supported. For example, an
ECPublicKey or an ECPrivateKey for Elliptic Curve keys.
If the PEM type does not have a corresponding class,
decode(String) and decode(InputStream) will return a
PEM object.
The decode(String, Class) and decode(InputStream, Class)
methods take a class parameter that specifies the type of DEREncodable
to return. These methods are useful for avoiding casts when the PEM type is
known, or when extracting a specific type if there is more than one option.
For example, if the PEM contains both a public and private key, specifying
PrivateKey.class returns only the private key.
If the class parameter specifies X509EncodedKeySpec.class, the
public key encoding is returned as an instance of X509EncodedKeySpec
class. Any type of PEM data can be decoded into a PEM object by
specifying PEM.class. If the class parameter does not match the PEM
content, a ClassCastException is thrown.
In addition to the types listed above, these methods support the
following PEM types and DEREncodable classes when specified as
parameters:
- PUBLIC KEY :
X509EncodedKeySpec - PRIVATE KEY :
PKCS8EncodedKeySpec - PRIVATE KEY :
PublicKey(if the encoding contains a public key) - PRIVATE KEY :
X509EncodedKeySpec(if the encoding contains a public key)
PEMDecoder instance configured for decryption:
- ENCRYPTED PRIVATE KEY :
PKCS8EncodedKeySpec - ENCRYPTED PRIVATE KEY :
PublicKey(if the encoding contains a public key) - ENCRYPTED PRIVATE KEY :
X509EncodedKeySpec(if the encoding contains a public key)
A new PEMDecoder instance is created when configured
with withFactory(Provider) or withDecryption(char[]).
The withFactory(Provider) method uses the specified provider
to produce cryptographic objects from KeyFactory and
CertificateFactory. The withDecryption(char[]) method configures the
decoder to decrypt and decode encrypted private key PEM data using the given
password. If decryption fails, an IllegalArgumentException is thrown.
If an encrypted private key PEM is processed by a decoder not configured
for decryption, an EncryptedPrivateKeyInfo object is returned.
A PEMDecoder configured for decryption will decode unencrypted PEM.
This class is immutable and thread-safe.
Example: decode a private key:
PEMDecoder pd = PEMDecoder.of();
PrivateKey priKey = pd.decode(priKeyPEM, PrivateKey.class);
Example: configure decryption and a factory provider:
PEMDecoder pd = PEMDecoder.of().withDecryption(password).
withFactory(provider);
DEREncodable pemData = pd.decode(privKeyPEM);
- Implementation Note:
- This implementation decodes RSA PRIVATE KEY as
PrivateKey, X509 CERTIFICATE and X.509 CERTIFICATE asX509Certificate, and CRL asX509CRL. Other implementations may recognize additional PEM types. - Since:
- 25
- External Specifications
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondecode(InputStream is) Decodes and returns aDEREncodablefrom the givenInputStream.<S extends DEREncodablePREVIEW>
Sdecode(InputStream is, Class<S> tClass) Decodes and returns aDEREncodableof the specified class for the givenInputStream.Decodes and returns aDEREncodablefrom the givenString.<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.Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitModifier and TypeMethodDescriptionprotected Objectclone()Creates and returns a copy of this object.booleanIndicates whether some other object is "equal to" this one.protected voidfinalize()Deprecated, for removal: This API element is subject to removal in a future version.Finalization is deprecated and subject to removal in a future release.final Class<?> getClass()Returns the runtime class of thisObject.inthashCode()Returns a hash code value for this object.final voidnotify()Wakes up a single thread that is waiting on this object's monitor.final voidWakes up all threads that are waiting on this object's monitor.toString()Returns a string representation of the object.final voidwait()Causes the current thread to wait until it is awakened, typically by being notified or interrupted.final voidwait(long timeoutMillis) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.final voidwait(long timeoutMillis, int nanos) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
-
Method Details
-
of
Returns an instance ofPEMDecoder.- Returns:
- a
PEMDecoderinstance
-
decode
Decodes and returns aDEREncodablefrom 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.A
DEREncodablewill be returned that best represents the decoded data. If the PEM type is not supported, aPEMobject is returned containing the type identifier, Base64-encoded data, and any leading data preceding the PEM header. ForDEREncodabletypes other thanPEM, leading data is ignored and not returned as part of theDEREncodableobject.Input consumed by this method is read in as
UTF-8.- Parameters:
str- aStringcontaining PEM data- Returns:
- a
DEREncodable - Throws:
IllegalArgumentException- on error in decoding or no PEM data foundNullPointerException- whenstrisnull
-
decode
Decodes and returns aDEREncodablefrom the givenInputStream.This method reads from the
InputStreamuntil the end of a 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.A
DEREncodablewill be returned that best represents the decoded data. If the PEM type is not supported, aPEMobject is returned containing the type identifier, Base64-encoded data, and any leading data preceding the PEM header. ForDEREncodabletypes other thanPEM, leading data is ignored and not returned as part of theDEREncodableobject.If no PEM data is found, an
EOFExceptionis thrown.- Parameters:
is-InputStreamcontaining PEM data- Returns:
- a
DEREncodable - Throws:
IOException- on IO or PEM syntax error where theInputStreamdid not complete decodingEOFException- no PEM data found or unexpectedly reached the end of theInputStreamIllegalArgumentException- on error in decodingNullPointerException- whenisisnull
-
decode
Decodes and returns aDEREncodableof the specified class from the given PEM string.tClassmust 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
PEM.class, aPEMobject is returned containing the type identifier, Base64-encoded data, and any leading data preceding the PEM header. ForDEREncodabletypes other thanPEM, leading data is ignored and not returned as part of theDEREncodableobject.Input consumed by this method is read in as
UTF-8.- Type Parameters:
S- class type parameter that extendsDEREncodable- Parameters:
str- theStringcontaining PEM datatClass- the returned object class that extends or implementsDEREncodable- Returns:
- a
DEREncodablespecified bytClass - Throws:
IllegalArgumentException- on error in decoding or no PEM data foundClassCastException- iftClassdoes not represent the PEM typeNullPointerException- when any input values arenull
-
decode
Decodes and returns aDEREncodableof the specified class for the givenInputStream.tClassmust be an appropriate class for the PEM type.This method reads from the
InputStreamuntil the end of a 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
PEM.class, aPEMobject is returned containing the type identifier, Base64-encoded data, and any leading data preceding the PEM header. ForDEREncodabletypes other thanPEM, leading data is ignored and not returned as part of theDEREncodableobject.If no PEM data is found, an
EOFExceptionis thrown.- Type Parameters:
S- class type parameter that extendsDEREncodable- Parameters:
is- anInputStreamcontaining PEM datatClass- the returned object class that extends or implementsDEREncodable- Returns:
- a
DEREncodabletypecast totClass - Throws:
IOException- on IO or PEM syntax error where theInputStreamdid not complete decodingEOFException- no PEM data found or unexpectedly reached the end of theInputStreamIllegalArgumentException- on error in decodingClassCastException- iftClassdoes not represent the PEM typeNullPointerException- when any input values arenull- 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
PEMDecoderinstance configured with theProvider - Throws:
NullPointerException- ifproviderisnull
-
withDecryption
Returns a copy of thisPEMDecoderthat decodes and decrypts encrypted private keys using the specified password. Non-encrypted PEM can also be decoded from this instance.- Parameters:
password- the password to decrypt the encrypted PEM data. This array is cloned and stored in the new instance.- Returns:
- a new
PEMDecoderinstance configured for decryption - Throws:
NullPointerException- ifpasswordisnull
-
PEMDecoderwhen preview features are enabled.