Package org.microproxy.tls
Class CertificateAuthority
java.lang.Object
org.microproxy.tls.CertificateAuthority
A certificate authority that issues server certificates on the fly. Install its certificate
(
getCertificatePem()) in clients that should accept intercepted connections.-
Constructor Summary
ConstructorsConstructorDescriptionCertificateAuthority(PrivateKey privateKey, X509Certificate certificate) Uses an existing CA private key and certificate. -
Method Summary
Modifier and TypeMethodDescriptionReturns a client-side SSLContext that trusts only this CA.static CertificateAuthorityGenerates a new CA with an EC P-256 key, valid for ten years.Returns the signing CA certificate.Returns the CA certificate in PEM format.Returns the CA private key.issue(PublicKey subjectKey, Collection<String> names) Issues a server certificate fornames(DNS names or IP literals; the first is also the common name), valid for at most a year and never beyond the CA's own expiry.static CertificateAuthorityLoads the first key entry of a PKCS#12 (or JKS) key store.static CertificateAuthorityloadOrCreate(Path keyStore, char[] password, String commonName) Loads the CA fromkeyStore, generating and saving a new one if it does not exist.voidSaves key and certificate as PKCS#12.serverContext(String... names) A server-side SSLContext presenting a fresh certificate fornames.voidwriteCertificatePem(Path file) Writes the CA certificate in PEM format.
-
Constructor Details
-
CertificateAuthority
Uses an existing CA private key and certificate.- Parameters:
privateKey- the CA signing private keycertificate- the CA certificate
-
-
Method Details
-
generate
Generates a new CA with an EC P-256 key, valid for ten years.- Parameters:
commonName- the certificate subject common name- Returns:
- the new certificate authority
-
load
Loads the first key entry of a PKCS#12 (or JKS) key store.- Parameters:
keyStore- the key-store filepassword- the key-store password- Returns:
- the loaded certificate authority
- Throws:
IOException- if the key store cannot be read or contains no usable CA key entry
-
loadOrCreate
public static CertificateAuthority loadOrCreate(Path keyStore, char[] password, String commonName) throws IOException Loads the CA fromkeyStore, generating and saving a new one if it does not exist.- Parameters:
keyStore- the key-store filepassword- the key-store passwordcommonName- the certificate subject common name- Returns:
- the loaded or newly generated certificate authority
- Throws:
IOException- if the CA key store cannot be loaded or saved
-
save
Saves key and certificate as PKCS#12.- Parameters:
keyStore- the key-store filepassword- the key-store password- Throws:
IOException- if the CA key store cannot be written
-
getCertificate
Returns the signing CA certificate.- Returns:
- the signing CA certificate
-
getPrivateKey
Returns the CA private key.- Returns:
- the CA private key
-
getCertificatePem
Returns the CA certificate in PEM format.- Returns:
- the CA certificate in PEM format
-
writeCertificatePem
Writes the CA certificate in PEM format.- Parameters:
file- the destination PEM file- Throws:
IOException- if the certificate file cannot be written
-
issue
Issues a server certificate fornames(DNS names or IP literals; the first is also the common name), valid for at most a year and never beyond the CA's own expiry.- Parameters:
subjectKey- the public key of the certificate subjectnames- the DNS names or IP literals to include in the certificate- Returns:
- the signed server certificate
-
serverContext
A server-side SSLContext presenting a fresh certificate fornames.- Parameters:
names- the DNS names or IP literals to include in the certificate- Returns:
- a server-side SSLContext presenting a fresh certificate for
names
-
clientContext
Returns a client-side SSLContext that trusts only this CA.- Returns:
- a client-side SSLContext that trusts only this CA
-