Class PkiTools
java.lang.Object
org.codice.ddf.security.certificate.generator.PkiTools
This class is a home for helper functions that did not belong to other classes.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic StringGiven an X509 certificate, return a PEM encoded string representation of the certificate.static org.bouncycastle.asn1.x500.X500NameconvertDistinguishedName(String... tuples) static X509CertificatederToCertificate(byte[] certDer) Given a byte array that represents a DER encoded X509 certificate, return the certificate objectstatic StringderToPem(byte[] bytes) Convert a byte array to a Java String.static PrivateKeyderToPrivateKey(byte[] privateKeyDer) Given a byte array that represents a DER encoded private key, return the private key objectstatic KeyPairGenerate new RSA public/private key pair with 2048 bit keystatic StringGet the host name or DNS name associated with the machine running the JVM.static byte[]Serialize a Key object as a DER encoded byte array.static Stringstatic org.bouncycastle.asn1.x500.X500NamemakeDistinguishedName(String commonName) Create an X500 name with a single populated attribute, the "common name".static org.bouncycastle.asn1.x509.GeneralNamemakeGeneralName(String name) Create an X509 general name based on the specified string which supports a format similar to OpenSSL X509 configuration as:tag:namewhere tag can be one of: email - email subject URI - uniformed resource identifier RID - registered id DNS - hostname IP - ip address (V4 or V6) dirName - directory namestatic X509CertificatepemToCertificate(String certString) Given a PEM encoded X509 certificate, return an object representation of the certificatestatic byte[]Convert a Java String to a byte arraystatic PrivateKeypemToPrivateKey(String keyString) Convert a Java String to an private key
-
Field Details
-
RSA_KEY_LENGTH
public static final int RSA_KEY_LENGTH- See Also:
-
ALGORITHM
- See Also:
-
-
Method Details
-
derToPem
Convert a byte array to a Java String.- Parameters:
bytes- DER encoded bytes- Returns:
- PEM encoded bytes
-
certificateToPem
Given an X509 certificate, return a PEM encoded string representation of the certificate.- Parameters:
cert- certificate- Returns:
- PEM encoded String
-
derToCertificate
Given a byte array that represents a DER encoded X509 certificate, return the certificate object- Parameters:
certDer- byte array representing a DER encoded X509 certificate- Returns:
- instance of X509 certificate
-
derToPrivateKey
Given a byte array that represents a DER encoded private key, return the private key object- Parameters:
privateKeyDer- byte array representing a DER encoded private key- Returns:
- instance of private key
-
getHostName
Get the host name or DNS name associated with the machine running the JVM. This method is public so client code can easily check the name and decide if it should be used in the generated certificate.- Returns:
- String. Hostname of this machine. Hostname should be the same as the machine's DNS name.
-
generateRsaKeyPair
Generate new RSA public/private key pair with 2048 bit key- Returns:
- new generated key pair
- Throws:
CertificateGeneratorException
-
keyToDer
Serialize a Key object as a DER encoded byte array.- Parameters:
key- instance of Key object- Returns:
- byte[]
-
keyToPem
- Parameters:
key- object- Returns:
- PEM encoded string represents the bytes of the key
-
makeDistinguishedName
Create an X500 name with a single populated attribute, the "common name". An X500 name object details the identity of a machine, person, or organization. The name object is used as the "subject" of a certificate. SSL/TLS typically uses a subject's common name as the DNS name for a machine and this name must be correct or SSl/TLS will not trust the machine's certificate.TLS can use a different set of attributes to, the Subject Alternative Names. SANs are extensions to the X509 specification and can include IP addresses, DNS names and other machine information. This package does not use SANs.
- Parameters:
commonName- the fully qualified host name of the end entity- Returns:
- X500 name object with common name attribute set
- See Also:
-
convertDistinguishedName
-
makeGeneralName
Create an X509 general name based on the specified string which supports a format similar to OpenSSL X509 configuration as:tag:namewhere tag can be one of:- email - email subject
- URI - uniformed resource identifier
- RID - registered id
- DNS - hostname
- IP - ip address (V4 or V6)
- dirName - directory name
- Parameters:
name- a string representing an X509 general name in an openssl format- Returns:
- the corresponding parsed X509 general name
- Throws:
IllegalArgumentException- if the string isnull, empty or cannot be parsed properly- See Also:
-
pemToCertificate
Given a PEM encoded X509 certificate, return an object representation of the certificate- Parameters:
certString- PEM encoded X509 certificate- Returns:
- instance of X509 certificate
-
pemToDer
Convert a Java String to a byte array- Parameters:
string- PEM encoded bytes- Returns:
- DER encoded bytes
-
pemToPrivateKey
Convert a Java String to an private key- Parameters:
keyString- encoded RSA private key. Assume PKCS#8 format- Returns:
- Instance of PrivateKey
-