Class TestPki

java.lang.Object
com.irurueta.authserver.testsupport.TestPki
All Implemented Interfaces:
AutoCloseable

public final class TestPki extends Object implements AutoCloseable
A small public key infrastructure generated at run time for the tests of TLS client authentication: a certificate authority, and server and client certificates it signs, each kept in a PKCS#12 key store inside one temporary directory that close() deletes. It drives keytool (see Keytool); nothing is ever kept in the repository.
  • Field Details

    • CA_ALIAS

      private static final String CA_ALIAS
      Alias of the authority's key.
      See Also:
    • ENTITY_ALIAS

      private static final String ENTITY_ALIAS
      Alias of the key of an issued entity.
      See Also:
    • KEY_STORE_TYPE

      private static final String KEY_STORE_TYPE
      Type of the key stores.
      See Also:
    • PASSWORD_BYTES

      private static final int PASSWORD_BYTES
      Number of random bytes of the password of the key stores.
      See Also:
    • VALIDITY_DAYS

      private static final String VALIDITY_DAYS
      Days the issued certificates are valid.
      See Also:
    • KEYSTORE

      private static final String KEYSTORE
      The keytool option that names a key store.
      See Also:
    • STORE_PASSWORD_FILE

      private static final String STORE_PASSWORD_FILE
      The keytool option that names the file with a key store password.
      See Also:
    • ALIAS

      private static final String ALIAS
      The keytool option that names the alias of an entry.
      See Also:
    • INPUT

      private static final String INPUT
      The keytool option that names the input file.
      See Also:
    • RANDOM

      private static final SecureRandom RANDOM
      Source of the password.
    • directory

      private final Path directory
      Temporary directory that holds every file.
    • password

      private final String password
      Password of every key store.
    • passwordFile

      private final Path passwordFile
      File that holds password.
    • authorityKeyStore

      private final Path authorityKeyStore
      Key store of the authority.
    • authority

      private final X509Certificate authority
      Certificate of the authority.
    • authorityPem

      private final Path authorityPem
      Path of the authority's certificate in PEM.
    • issued

      private int issued
      Number of entities issued, to name their files.
  • Constructor Details

    • TestPki

      private TestPki(Path directory, String password, X509Certificate authority)
      Describes an infrastructure whose files already exist.
      Parameters:
      directory - temporary directory that holds every file.
      password - password of every key store.
      authority - certificate of the authority.
  • Method Details

    • create

      public static TestPki create(String authorityName)
      Creates the infrastructure with a new certificate authority.
      Parameters:
      authorityName - common name of the authority.
      Returns:
      the infrastructure; the caller closes it.
    • issueServer

      public TestPki.Identity issueServer(String commonName)
      Issues the certificate of a TLS server, valid for localhost and the loopback address.
      Parameters:
      commonName - common name of the subject.
      Returns:
      the server's key store and certificate.
    • issueClient

      public TestPki.Identity issueClient(String commonName)
      Issues the certificate of a client, signed by the authority.
      Parameters:
      commonName - common name of the subject.
      Returns:
      the client's key store and certificate.
    • clientContext

      public SSLContext clientContext(TestPki.Identity client)
      Gives the TLS context of a client that presents a certificate and trusts the authority.
      Parameters:
      client - the client's identity, null for a client with no certificate.
      Returns:
      the context.
    • issueExpiredClient

      public TestPki.Identity issueExpiredClient(String commonName)
      Gives the identity of a client with a self-signed certificate that expired days ago.
      Parameters:
      commonName - common name of the subject.
      Returns:
      the client's key store and certificate.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • issue

      private TestPki.Identity issue(String commonName, List<String> extensions)
      Issues an entity signed by the authority.
      Parameters:
      commonName - common name of the subject.
      extensions - the keytool extension options the certificate gets.
      Returns:
      the entity's key store and certificate.
    • sign

      private Path sign(Path keyStore, Path request, Path reply, List<String> extensions) throws IOException
      Has the authority sign a request and installs the answer in the entity's key store.
      Parameters:
      keyStore - key store of the entity.
      request - certificate request of the entity.
      reply - file the signed certificate is written to.
      extensions - the keytool extension options the certificate gets.
      Returns:
      the entity's key store.
      Throws:
      IOException - if keytool fails.
    • identityOf

      private TestPki.Identity identityOf(Path keyStore) throws IOException, GeneralSecurityException
      Reads an identity from its key store.
      Parameters:
      keyStore - the key store.
      Returns:
      the identity.
      Throws:
      IOException - if the key store cannot be read.
      GeneralSecurityException - if the key store cannot be loaded.
    • nextName

      private String nextName()
      Names the files of the next entity.
      Returns:
      the name.
    • readCertificate

      private static X509Certificate readCertificate(Path pem) throws IOException, GeneralSecurityException
      Reads a PEM certificate.
      Parameters:
      pem - the file.
      Returns:
      the certificate.
      Throws:
      IOException - if the file cannot be read.
      GeneralSecurityException - if it is not a certificate.