Package org.wildfly.security.pem
Class Pem
- java.lang.Object
-
- org.wildfly.security.pem.Pem
-
public final class Pem extends Object
A class containing utilities which can handle the PEM format. See RFC 7468 for more information.- Author:
- David M. Lloyd
-
-
Field Summary
Fields Modifier and Type Field Description private static String
CERTIFICATE_FORMAT
private static String
CERTIFICATE_REQUEST_FORMAT
static String
OPENSSH_PRIVATE_KEY_FORMAT
private static String
PRIVATE_KEY_FORMAT
private static String
PUBLIC_KEY_FORMAT
private static String
RSA_PRIVATE_KEY_FORMAT
private static Pattern
VALID_LABEL
-
Constructor Summary
Constructors Constructor Description Pem()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static byte[]
extractDerContent(org.wildfly.common.iteration.CodePointIterator pemContent)
Extracts the DER content from the givenpemContent
.static void
generatePemContent(org.wildfly.common.bytes.ByteStringBuilder target, String type, org.wildfly.common.iteration.ByteIterator content)
Generate PEM content to the given byte string builder.static void
generatePemPKCS10CertificateSigningRequest(org.wildfly.common.bytes.ByteStringBuilder target, PKCS10CertificateSigningRequest certificateSigningRequest)
Generate PEM content containing a PKCS #10 certificate signing request.static void
generatePemPublicKey(org.wildfly.common.bytes.ByteStringBuilder target, PublicKey publicKey)
Generate PEM content containing aPublicKey
.static void
generatePemX509Certificate(org.wildfly.common.bytes.ByteStringBuilder target, X509Certificate certificate)
Generate PEM content containing an X.509 certificate.private static KeyPair
parseOpenSSHKeys(org.wildfly.common.iteration.ByteIterator byteIterator, org.apache.sshd.common.config.keys.FilePasswordProvider passphraseProvider)
static Iterator<PemEntry<?>>
parsePemContent(org.wildfly.common.iteration.CodePointIterator pemContent)
Iterate over the contents of a PEM file, returning each entry in sequence.static <R> R
parsePemContent(org.wildfly.common.iteration.CodePointIterator pemContent, BiFunction<String,org.wildfly.common.iteration.ByteIterator,R> contentFunction)
Parse arbitrary PEM content.static Iterator<PemEntry<?>>
parsePemOpenSSHContent(org.wildfly.common.iteration.CodePointIterator pemContent, org.apache.sshd.common.config.keys.FilePasswordProvider passphraseProvider)
Iterate over the contents of a key file in OpenSSH format, returning each entry in sequence.private static PrivateKey
parsePemPrivateKey(String type, org.wildfly.common.iteration.ByteIterator byteIterator)
private static PublicKey
parsePemPublicKey(String type, org.wildfly.common.iteration.ByteIterator byteIterator)
static PublicKey
parsePemPublicKey(org.wildfly.common.iteration.CodePointIterator pemContent)
Parse aPublicKey
in PEM format.private static PrivateKey
parsePemRsaPrivateKey(String type, org.wildfly.common.iteration.ByteIterator byteIterator)
static X509Certificate
parsePemX509Certificate(org.wildfly.common.iteration.CodePointIterator pemContent)
Parse an X.509 certificate in PEM format.private static X509Certificate
parsePemX509CertificateContent(String type, org.wildfly.common.iteration.ByteIterator byteIterator)
-
-
-
Field Detail
-
VALID_LABEL
private static final Pattern VALID_LABEL
-
PUBLIC_KEY_FORMAT
private static final String PUBLIC_KEY_FORMAT
- See Also:
- Constant Field Values
-
CERTIFICATE_FORMAT
private static final String CERTIFICATE_FORMAT
- See Also:
- Constant Field Values
-
PRIVATE_KEY_FORMAT
private static final String PRIVATE_KEY_FORMAT
- See Also:
- Constant Field Values
-
RSA_PRIVATE_KEY_FORMAT
private static final String RSA_PRIVATE_KEY_FORMAT
- See Also:
- Constant Field Values
-
CERTIFICATE_REQUEST_FORMAT
private static final String CERTIFICATE_REQUEST_FORMAT
- See Also:
- Constant Field Values
-
OPENSSH_PRIVATE_KEY_FORMAT
public static final String OPENSSH_PRIVATE_KEY_FORMAT
- See Also:
- Constant Field Values
-
-
Method Detail
-
parsePemContent
public static <R> R parsePemContent(org.wildfly.common.iteration.CodePointIterator pemContent, BiFunction<String,org.wildfly.common.iteration.ByteIterator,R> contentFunction) throws IllegalArgumentException
Parse arbitrary PEM content. The given function is used to parse the content of the PEM representation and produce some result. The PEM type string is passed to the function. If the function throws an exception, that exception is propagated to the caller of this method. If the PEM content is malformed, an exception is thrown. If the trailing PEM content is found to be invalid after the function returns, the function result is discarded and an exception is thrown.- Type Parameters:
R
- the value return type- Parameters:
pemContent
- the content to parse (must not benull
)contentFunction
- a function to consume the PEM content and produce a result (must not benull
)- Returns:
- the return value of the function or
null
if there is no PEM content to parse - Throws:
IllegalArgumentException
- if there is a problem with processing the content of the PEM data
-
parsePemContent
public static Iterator<PemEntry<?>> parsePemContent(org.wildfly.common.iteration.CodePointIterator pemContent)
Iterate over the contents of a PEM file, returning each entry in sequence.- Parameters:
pemContent
- the code point iterator over the content (must not benull
)- Returns:
- the iterator (not
null
)
-
parsePemOpenSSHContent
public static Iterator<PemEntry<?>> parsePemOpenSSHContent(org.wildfly.common.iteration.CodePointIterator pemContent, org.apache.sshd.common.config.keys.FilePasswordProvider passphraseProvider) throws IllegalArgumentException
Iterate over the contents of a key file in OpenSSH format, returning each entry in sequence.- Parameters:
pemContent
- the code point iterator over the content (must not benull
)passphraseProvider
- provides the passphrase used to decrypt the private key(may benull
)- Returns:
- the iterator (not
null
) - Throws:
IllegalArgumentException
- if there is a problem with the data or the key
-
generatePemContent
public static void generatePemContent(org.wildfly.common.bytes.ByteStringBuilder target, String type, org.wildfly.common.iteration.ByteIterator content) throws IllegalArgumentException
Generate PEM content to the given byte string builder. The appropriate header and footer surrounds the base-64 encoded value.- Parameters:
target
- the target byte string builder (must not benull
)type
- the content type (must not benull
)content
- the content iterator (must not benull
)- Throws:
IllegalArgumentException
- if there is a problem with the data or the type
-
extractDerContent
public static byte[] extractDerContent(org.wildfly.common.iteration.CodePointIterator pemContent)
Extracts the DER content from the givenpemContent
.- Parameters:
pemContent
- aCodePointIterator
with the PEM content- Returns:
- a byte array with the DER content
-
parsePemX509CertificateContent
private static X509Certificate parsePemX509CertificateContent(String type, org.wildfly.common.iteration.ByteIterator byteIterator) throws IllegalArgumentException
- Throws:
IllegalArgumentException
-
parsePemPublicKey
private static PublicKey parsePemPublicKey(String type, org.wildfly.common.iteration.ByteIterator byteIterator) throws IllegalArgumentException
- Throws:
IllegalArgumentException
-
parsePemPrivateKey
private static PrivateKey parsePemPrivateKey(String type, org.wildfly.common.iteration.ByteIterator byteIterator) throws IllegalArgumentException
- Throws:
IllegalArgumentException
-
parsePemRsaPrivateKey
private static PrivateKey parsePemRsaPrivateKey(String type, org.wildfly.common.iteration.ByteIterator byteIterator) throws IllegalArgumentException
- Throws:
IllegalArgumentException
-
parseOpenSSHKeys
private static KeyPair parseOpenSSHKeys(org.wildfly.common.iteration.ByteIterator byteIterator, org.apache.sshd.common.config.keys.FilePasswordProvider passphraseProvider) throws IllegalArgumentException
- Throws:
IllegalArgumentException
-
parsePemX509Certificate
public static X509Certificate parsePemX509Certificate(org.wildfly.common.iteration.CodePointIterator pemContent) throws IllegalArgumentException
Parse an X.509 certificate in PEM format.- Parameters:
pemContent
- the PEM content (must not benull
)- Returns:
- the certificate (not
null
) - Throws:
IllegalArgumentException
- if the certificate could not be parsed for some reason
-
parsePemPublicKey
public static PublicKey parsePemPublicKey(org.wildfly.common.iteration.CodePointIterator pemContent) throws IllegalArgumentException
Parse aPublicKey
in PEM format.- Parameters:
pemContent
- the PEM content (must not benull
)- Returns:
- the public key (not
null
) - Throws:
IllegalArgumentException
- if the public key could not be parsed for some reason
-
generatePemX509Certificate
public static void generatePemX509Certificate(org.wildfly.common.bytes.ByteStringBuilder target, X509Certificate certificate)
Generate PEM content containing an X.509 certificate.- Parameters:
target
- the target byte string builder (must not benull
)certificate
- the X.509 certificate (must not benull
)
-
generatePemPublicKey
public static void generatePemPublicKey(org.wildfly.common.bytes.ByteStringBuilder target, PublicKey publicKey)
Generate PEM content containing aPublicKey
.- Parameters:
target
- the target byte string builder (must not benull
)publicKey
- thePublicKey
(must not benull
)
-
generatePemPKCS10CertificateSigningRequest
public static void generatePemPKCS10CertificateSigningRequest(org.wildfly.common.bytes.ByteStringBuilder target, PKCS10CertificateSigningRequest certificateSigningRequest)
Generate PEM content containing a PKCS #10 certificate signing request.- Parameters:
target
- the target byte string builder (must not benull
)certificateSigningRequest
- the PKCS #10 certificate signing request (must not benull
)- Since:
- 1.2.0
-
-