Class CertificateHelpers
java.lang.Object
eu.emi.security.authn.x509.helpers.CertificateHelpers
Utility methods for certificates handling and reading/writing PEM files.
- Author:
- K. Benedyczak
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
checkKeysMatching
(PrivateKey privKey, PublicKey pubKey) Throws an exception if the private key is not matching the public key.static byte[]
getExtensionBytes
(X509Certificate cert, String oid) Gets the certificate extension identified by the oid and returns the value bytes unwrapped by the ASN1OctetString.getPEMType
(String name) Assumes that the input is the contents of the PEM identification line, after '-----BEGIN ' prefix.static Certificate
readDERCertificate
(InputStream input) static Collection<? extends Certificate>
readDERCertificates
(InputStream input) static X509Certificate[]
sortChain
(List<X509Certificate> certificates) Creates a chain of certificates, where the top-most certificate (the one without issuing certificate) is the last in the returned array.static CertPath
toCertPath
(X509Certificate[] in) Converts certificates array toCertPath
static org.bouncycastle.asn1.x500.X500Name
toX500Name
(X500Principal srcDn)
-
Constructor Details
-
CertificateHelpers
public CertificateHelpers()
-
-
Method Details
-
getPEMType
Assumes that the input is the contents of the PEM identification line, after '-----BEGIN ' prefix.- Parameters:
name
- PEM first line to be checked.- Returns:
- the type
-
readDERCertificates
public static Collection<? extends Certificate> readDERCertificates(InputStream input) throws IOException - Throws:
IOException
-
readDERCertificate
- Throws:
IOException
-
sortChain
Creates a chain of certificates, where the top-most certificate (the one without issuing certificate) is the last in the returned array.- Parameters:
certificates
- unsorted certificates of one chain- Returns:
- sorted certificate chain
- Throws:
IOException
- if the passed chain is inconsistent
-
toCertPath
Converts certificates array toCertPath
- Parameters:
in
- array- Returns:
- converted object
- Throws:
CertificateException
- certificate exception
-
toX500Name
- Parameters:
srcDn
- source object- Returns:
- converted object
-
getExtensionBytes
Gets the certificate extension identified by the oid and returns the value bytes unwrapped by the ASN1OctetString.- Parameters:
cert
- The certificate to inspect.oid
- The extension OID to fetch.- Returns:
- The value bytes of the extension, returns null in case the extension was not present or was empty.
- Throws:
IOException
- thrown in case the certificate parsing fails.
-
checkKeysMatching
public static void checkKeysMatching(PrivateKey privKey, PublicKey pubKey) throws InvalidKeyException Throws an exception if the private key is not matching the public key. The check is done only for known types of keys - RSA and DSA currently.- Parameters:
privKey
- first key to matchpubKey
- 2nd key to match- Throws:
InvalidKeyException
- invalid key exception
-