java.lang.Object
org.codice.ddf.security.certificate.generator.PkiTools

public abstract class PkiTools extends Object
This class is a home for helper functions that did not belong to other classes.
  • Field Details

  • Method Details

    • derToPem

      public static String derToPem(byte[] bytes)
      Convert a byte array to a Java String.
      Parameters:
      bytes - DER encoded bytes
      Returns:
      PEM encoded bytes
    • certificateToPem

      public static String certificateToPem(X509Certificate cert)
      Given an X509 certificate, return a PEM encoded string representation of the certificate.
      Parameters:
      cert - certificate
      Returns:
      PEM encoded String
    • derToCertificate

      public static X509Certificate derToCertificate(byte[] certDer)
      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

      public static PrivateKey derToPrivateKey(byte[] privateKeyDer)
      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

      public static String 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

      public static KeyPair generateRsaKeyPair()
      Generate new RSA public/private key pair with 2048 bit key
      Returns:
      new generated key pair
      Throws:
      CertificateGeneratorException
    • keyToDer

      public static byte[] keyToDer(Key key)
      Serialize a Key object as a DER encoded byte array.
      Parameters:
      key - instance of Key object
      Returns:
      byte[]
    • keyToPem

      public static String keyToPem(Key key)
      Parameters:
      key - object
      Returns:
      PEM encoded string represents the bytes of the key
    • makeDistinguishedName

      public static org.bouncycastle.asn1.x500.X500Name makeDistinguishedName(String commonName)
      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

      public static org.bouncycastle.asn1.x500.X500Name convertDistinguishedName(String... tuples)
    • makeGeneralName

      public static org.bouncycastle.asn1.x509.GeneralName makeGeneralName(String name)
      Create an X509 general name based on the specified string which supports a format similar to OpenSSL X509 configuration as: tag:name where 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 is null, empty or cannot be parsed properly
      See Also:
    • pemToCertificate

      public static X509Certificate pemToCertificate(String certString)
      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

      public static byte[] pemToDer(String string)
      Convert a Java String to a byte array
      Parameters:
      string - PEM encoded bytes
      Returns:
      DER encoded bytes
    • pemToPrivateKey

      public static PrivateKey pemToPrivateKey(String keyString)
      Convert a Java String to an private key
      Parameters:
      keyString - encoded RSA private key. Assume PKCS#8 format
      Returns:
      Instance of PrivateKey