Class TestPki
java.lang.Object
com.irurueta.authserver.testsupport.TestPki
- All Implemented Interfaces:
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.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordAn identity of the infrastructure. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final StringThekeytooloption that names the alias of an entry.private final X509CertificateCertificate of the authority.private final PathKey store of the authority.private final PathPath of the authority's certificate in PEM.private static final StringAlias of the authority's key.private final PathTemporary directory that holds every file.private static final StringAlias of the key of an issued entity.private static final StringThekeytooloption that names the input file.private intNumber of entities issued, to name their files.private static final StringType of the key stores.private static final StringThekeytooloption that names a key store.private final StringPassword of every key store.private static final intNumber of random bytes of the password of the key stores.private final PathFile that holdspassword.private static final SecureRandomSource of the password.private static final StringThekeytooloption that names the file with a key store password.private static final StringDays the issued certificates are valid. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateTestPki(Path directory, String password, X509Certificate authority) Describes an infrastructure whose files already exist. -
Method Summary
Modifier and TypeMethodDescriptionclientContext(TestPki.Identity client) Gives the TLS context of a client that presents a certificate and trusts the authority.voidclose()static TestPkiCreates the infrastructure with a new certificate authority.private TestPki.IdentityidentityOf(Path keyStore) Reads an identity from its key store.private TestPki.IdentityIssues an entity signed by the authority.issueClient(String commonName) Issues the certificate of a client, signed by the authority.issueExpiredClient(String commonName) Gives the identity of a client with a self-signed certificate that expired days ago.issueServer(String commonName) Issues the certificate of a TLS server, valid forlocalhostand the loopback address.private StringnextName()Names the files of the next entity.private static X509CertificatereadCertificate(Path pem) Reads a PEM certificate.private PathHas the authority sign a request and installs the answer in the entity's key store.
-
Field Details
-
CA_ALIAS
Alias of the authority's key.- See Also:
-
ENTITY_ALIAS
Alias of the key of an issued entity.- See Also:
-
KEY_STORE_TYPE
Type of the key stores.- See Also:
-
PASSWORD_BYTES
private static final int PASSWORD_BYTESNumber of random bytes of the password of the key stores.- See Also:
-
VALIDITY_DAYS
Days the issued certificates are valid.- See Also:
-
KEYSTORE
Thekeytooloption that names a key store.- See Also:
-
STORE_PASSWORD_FILE
Thekeytooloption that names the file with a key store password.- See Also:
-
ALIAS
Thekeytooloption that names the alias of an entry.- See Also:
-
INPUT
Thekeytooloption that names the input file.- See Also:
-
RANDOM
Source of the password. -
directory
Temporary directory that holds every file. -
password
Password of every key store. -
passwordFile
File that holdspassword. -
authorityKeyStore
Key store of the authority. -
authority
Certificate of the authority. -
authorityPem
Path of the authority's certificate in PEM. -
issued
private int issuedNumber of entities issued, to name their files.
-
-
Constructor Details
-
TestPki
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
Creates the infrastructure with a new certificate authority.- Parameters:
authorityName- common name of the authority.- Returns:
- the infrastructure; the caller closes it.
-
issueServer
Issues the certificate of a TLS server, valid forlocalhostand the loopback address.- Parameters:
commonName- common name of the subject.- Returns:
- the server's key store and certificate.
-
issueClient
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
Gives the TLS context of a client that presents a certificate and trusts the authority.- Parameters:
client- the client's identity,nullfor a client with no certificate.- Returns:
- the context.
-
issueExpiredClient
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:
closein interfaceAutoCloseable
-
issue
Issues an entity signed by the authority.- Parameters:
commonName- common name of the subject.extensions- thekeytoolextension 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- thekeytoolextension options the certificate gets.- Returns:
- the entity's key store.
- Throws:
IOException- ifkeytoolfails.
-
identityOf
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
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.
-