Package jenkins.bouncycastle.api
Class PEMEncodable
java.lang.Object
jenkins.bouncycastle.api.PEMEncodable
A class that provides an API to manage PEM format, providing additional methods to handle Keys, Certificates,
Fingerprints, etc The supported algorithms will depend on the underlying version of BouncyCastle
- Since:
- 1.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic PEMEncodablecreate(Certificate certificate) Creates aPEMEncodablefrom aCertificateobjectstatic PEMEncodableCreates aPEMEncodablefrom aKeyobjectstatic PEMEncodableCreates aPEMEncodablefrom aKeyPairobjectstatic PEMEncodableCreates aPEMEncodableby decoding PEM formated data from aStringstatic PEMEncodableCreates aPEMEncodableby decoding PEM formated data from aStringstatic List<PEMEncodable>Creates a list ofPEMEncodables by decoding PEM formated data from aStringencode()Encodes the current stored information in PEM format and returns it as aStringstatic byte[]getKeyDigest(Key k, String algorithm) Generates an digest from a Key object in the specified digest format.static byte[]Generates an MD5 digest from a Key objectstatic byte[]Generates an SHA1 digest from a Key objectObtains the fingerprint of the private key in the "ab:cd:ef:...:12" format, which basically is an SHA1 digest from the key, encoded in hex format.Obtains the fingerprint of the public key in the "ab:cd:ef:...:12" format, which basically is an MD5 digest from the key, encoded in hex format.Obtains raw JCA or BouncyCastleObjectfrom the read PEM.static PEMEncodableCreates aPEMEncodableby reading a PEM filestatic PEMEncodableCreates aPEMEncodableby reading a PEM filestatic List<PEMEncodable>CreatesPEMEncodables by reading a PEM filestatic List<PEMEncodable>Creates aPEMEncodables by reading a PEM fileObtainCertificateobject from the read PEM.ObtainKeyPairobject with the public and private key from the decoded PEM.ObtainPrivateKeyobject from the read PEM.ObtainPublicKeyobject from the read PEM.voidWrites the current stored information in PEM formatedFile
-
Method Details
-
create
Creates aPEMEncodablefrom aKeyobject- Parameters:
key-Keyobject with the key- Returns:
PEMEncodableobject
-
create
Creates aPEMEncodablefrom aKeyPairobject- Parameters:
keyPair-KeyPairobject with the key pair- Returns:
PEMEncodableobject
-
create
Creates aPEMEncodablefrom aCertificateobject- Parameters:
certificate-Certificateobject with the certificate- Returns:
PEMEncodableobject
-
decode
@NonNull public static PEMEncodable decode(@NonNull String pem) throws IOException, UnrecoverableKeyException Creates aPEMEncodableby decoding PEM formated data from aString- Parameters:
pem-Stringwith the PEM data- Returns:
PEMEncodableobject- Throws:
IOException- launched if a problem exists reading the PEM informationUnrecoverableKeyException- in case PEM is passphrase protected and none or wrong is provided
-
decode
@NonNull public static PEMEncodable decode(@NonNull String pem, @Nullable char[] passphrase) throws IOException, UnrecoverableKeyException Creates aPEMEncodableby decoding PEM formated data from aString- Parameters:
pem-Stringwith the PEM datapassphrase- passphrase for the encrypted PEM data. null if PEM data is not passphrase protected. The caller is responsible for zeroing out the char[] after use to ensure the password does not stay in memory, e.g. withArrays.fill(passphrase, (char)0)- Returns:
PEMEncodableobject- Throws:
IOException- launched if a problem exists reading the PEM informationUnrecoverableKeyException- in case PEM is passphrase protected and none or wrong is provided
-
decodeAll
@NonNull public static List<PEMEncodable> decodeAll(@NonNull String pem, @Nullable char[] passphrase) throws IOException, UnrecoverableKeyException Creates a list ofPEMEncodables by decoding PEM formated data from aString- Parameters:
pem-Stringwith the PEM datapassphrase- passphrase for the encrypted PEM data.nullif PEM data is not passphrase protected. The caller is responsible for zeroing out the char[] after use to ensure the password does not stay in memory, e.g. withArrays.fill(passphrase, (char)0)- Returns:
- a list of
PEMEncodableobjects - Throws:
IOException- launched if a problem exists reading the PEM informationUnrecoverableKeyException- in case PEM is passphrase protected and none or wrong is provided
-
encode
Encodes the current stored information in PEM format and returns it as aString- Returns:
- PEM encoded data
- Throws:
IOException- launched if a problem exists generating the PEM information
-
read
@NonNull public static PEMEncodable read(@NonNull File pemFile) throws IOException, UnrecoverableKeyException Creates aPEMEncodableby reading a PEM file- Parameters:
pemFile-Filepointing to the PEM file to read- Returns:
PEMEncodableobject- Throws:
IOException- launched if a problem exists reading the PEM information or theFileUnrecoverableKeyException- in case PEM is passphrase protected and none or wrong is provided
-
read
@NonNull public static PEMEncodable read(@NonNull File pemFile, @Nullable char[] passphrase) throws IOException, UnrecoverableKeyException Creates aPEMEncodableby reading a PEM file- Parameters:
pemFile-Filepointing to the PEM file to readpassphrase- passphrase for the encrypted PEM data. null if PEM data is not passphrase protected. The caller is responsible for zeroing out the char[] after use to ensure the password does not stay in memory, e.g. withArrays.fill(passphrase, (char)0)- Returns:
PEMEncodableobject- Throws:
IOException- launched if a problem exists reading the PEM information or theFileUnrecoverableKeyException- in case PEM is passphrase protected and none or wrong is provided
-
readAll
@NonNull public static List<PEMEncodable> readAll(@NonNull File pemFile) throws IOException, UnrecoverableKeyException CreatesPEMEncodables by reading a PEM file- Parameters:
pemFile-Filepointing to the PEM file to read- Returns:
- A list of
PEMEncodableobjects. - Throws:
IOException- launched if a problem exists reading the PEM information or theFileUnrecoverableKeyException- in case PEM is passphrase protected
-
readAll
@NonNull public static List<PEMEncodable> readAll(@NonNull File pemFile, @Nullable char[] passphrase) throws IOException, UnrecoverableKeyException Creates aPEMEncodables by reading a PEM file- Parameters:
pemFile-Filepointing to the PEM file to readpassphrase- passphrase for the encrypted PEM data.nullif PEM data is not passphrase protected. The caller is responsible for zeroing out the char[] after use to ensure the password does not stay in memory, e.g. withArrays.fill(passphrase, (char)0)- Returns:
- a list of
PEMEncodableobjects - Throws:
IOException- launched if a problem exists reading the PEM information or theFileUnrecoverableKeyException- in case PEM is passphrase protected and none or wrong is provided
-
write
Writes the current stored information in PEM formatedFile- Parameters:
pemFile- PEMFileto read- Throws:
IOException- launched if a problem exists generating the PEM information or writing theFile
-
toKeyPair
ObtainKeyPairobject with the public and private key from the decoded PEM. No conversion is performed, the read PEM must contain private and public key in order to obtain aKeyPairobject, null will be returned in all the other cases.- Returns:
KeyPairobject with public and private keys or null if the read PEM didn't contain private and public keys.
-
toPublicKey
ObtainPublicKeyobject from the read PEM. If the PEM data contained other object type likeKeyPairorCertificate, the public key will be extracted from them.- Returns:
PublicKeywith the public key, null if a public key could not be obtained from the current data
-
toCertificate
ObtainCertificateobject from the read PEM.- Returns:
Certificatewith the certificate, null if a certificate could not be obtained from the current data
-
toPrivateKey
ObtainPrivateKeyobject from the read PEM. If the PEM data contained other object type likeKeyPair, the private key will be extracted from them.- Returns:
PrivateKeywith the private key, null if a private key could not be obtained from the current data
-
getRawObject
Obtains raw JCA or BouncyCastleObjectfrom the read PEM. Depending on the PEM nature or the object passed to thePEMEncodable(Object pemObject), the returned object can be one of the following (not exhaustive list) and any classes that inherit from them:- Bouncy Castle
ContentInfoECNamedCurveParameterSpecPKCS10CertificationRequestX509CertificateObjectX509V2AttributeCertificate
- Returns:
Objectread from the PEM
- Bouncy Castle
-
getPrivateKeyFingerprint
Obtains the fingerprint of the private key in the "ab:cd:ef:...:12" format, which basically is an SHA1 digest from the key, encoded in hex format.- Returns:
- private key fingerprint in hex format "ab:cd:ef:...:12", null if the private key could not be obtained from the current PEM data.
-
getPublicKeyFingerprint
Obtains the fingerprint of the public key in the "ab:cd:ef:...:12" format, which basically is an MD5 digest from the key, encoded in hex format.- Returns:
- public key fingerprint in hex format "ab:cd:ef:...:12", null if the public key could not be obtained from the current PEM data.
-
getKeyDigestSHA1
Generates an SHA1 digest from a Key object- Parameters:
k- the key to generate the digest from- Returns:
- the generated digest
-
getKeyDigestMD5
Generates an MD5 digest from a Key object- Parameters:
k- the key to generate the digest from- Returns:
- the generated digest
-
getKeyDigest
@NonNull public static byte[] getKeyDigest(@NonNull Key k, @NonNull String algorithm) throws NoSuchAlgorithmException Generates an digest from a Key object in the specified digest format. The supported digest formats will depend on the JVM API.- Parameters:
k- key to generate the digest fromalgorithm- digest format- Returns:
- the generated digest
- Throws:
NoSuchAlgorithmException- when provided digest algorithm is not available
-