Note
Never use the certificate and private key generated by this class in production. It is purely for testing purposes, and thus it is very insecure. It even uses an insecure pseudo-random generator for faster generation internally.
An X.509 certificate file and a EC/RSA private key file are generated in a system's temporary directory using
java., and they are deleted when the JVM exits using
java..
At first, this method tries to use OpenJDK's X.509 implementation (the sun.security.x509 package).
If it fails, it tries to use Bouncy Castle as a fallback.
| Modifier and Type | Field and Description |
|---|---|
| private final X509Certificate | |
| private final File | |
| private static final int | DEFAULT_KEY_LENGTH_BITS
FIPS 140-2 encryption requires the RSA key length to be 2048 bits or greater. |
| private static final Date | DEFAULT_NOT_AFTER
The maximum possible value in X.509 specification: 9999-12-31 23:59:59 |
| private static final Date | DEFAULT_NOT_BEFORE
Current time minus 1 year, just in case software clock goes back due to time synchronization |
| private final PrivateKey | |
| private static final InternalLogger | |
| private final File |
| Access | Constructor and Description |
|---|---|
| public | |
| public | SelfSignedCertificate(Date
Certificate is not valid before this time notBefore, Date Certificate is not valid after this time notAfter)Creates a new instance. |
| public | SelfSignedCertificate(Date
Certificate is not valid before this time notBefore, Date Certificate is not valid after this time notAfter, String Key pair algorithm algorithm, int the number of bits of the generated private key bits)Creates a new instance. |
| public | |
| public | SelfSignedCertificate(String
a fully qualified domain name fqdn, String Key pair algorithm algorithm, int the number of bits of the generated private key bits)Creates a new instance. |
| public | SelfSignedCertificate(String
a fully qualified domain name fqdn, Date Certificate is not valid before this time notBefore, Date Certificate is not valid after this time notAfter)Creates a new instance. |
| public | SelfSignedCertificate(String
a fully qualified domain name fqdn, Date Certificate is not valid before this time notBefore, Date Certificate is not valid after this time notAfter, String Key pair algorithm algorithm, int the number of bits of the generated private key bits)Creates a new instance. |
| public | SelfSignedCertificate(String
a fully qualified domain name fqdn, SecureRandom the random, int SecureRandom to usethe number of bits of the generated private key bits)Creates a new instance. |
| public | SelfSignedCertificate(String
a fully qualified domain name fqdn, SecureRandom the random, String SecureRandom to useKey pair algorithm algorithm, int the number of bits of the generated private key bits)Creates a new instance. |
| public | SelfSignedCertificate(String
a fully qualified domain name fqdn, SecureRandom the random, int SecureRandom to usethe number of bits of the generated private key bits, Date Certificate is not valid before this time notBefore, Date Certificate is not valid after this time notAfter)Creates a new instance. |
| public | SelfSignedCertificate(String
a fully qualified domain name fqdn, SecureRandom the random, int SecureRandom to usethe number of bits of the generated private key bits, Date Certificate is not valid before this time notBefore, Date Certificate is not valid after this time notAfter, String Key pair algorithm algorithm)Creates a new instance. |
| Modifier and Type | Method and Description |
|---|---|
| public X509Certificate | |
| public File | |
| public void | |
| private static boolean | |
| public PrivateKey | |
| pack-priv static String[] | |
| public File | |
| private static void | |
| private static void |
| cert | back to summary |
|---|---|
| private final X509Certificate cert | |
| certificate | back to summary |
|---|---|
| private final File certificate | |
| DEFAULT_KEY_LENGTH_BITS | back to summary |
|---|---|
| private static final int DEFAULT_KEY_LENGTH_BITS FIPS 140-2 encryption requires the RSA key length to be 2048 bits or greater. Let's use that as a sane default but allow the default to be set dynamically for those that need more stringent security requirements. | |
| DEFAULT_NOT_AFTER | back to summary |
|---|---|
| private static final Date DEFAULT_NOT_AFTER The maximum possible value in X.509 specification: 9999-12-31 23:59:59 | |
| DEFAULT_NOT_BEFORE | back to summary |
|---|---|
| private static final Date DEFAULT_NOT_BEFORE Current time minus 1 year, just in case software clock goes back due to time synchronization | |
| key | back to summary |
|---|---|
| private final PrivateKey key | |
| logger | back to summary |
|---|---|
| private static final InternalLogger logger | |
| privateKey | back to summary |
|---|---|
| private final File privateKey | |
| SelfSignedCertificate | back to summary |
|---|---|
| public SelfSignedCertificate() throws CertificateException Creates a new instance. Algorithm: RSA | |
| SelfSignedCertificate | back to summary |
|---|---|
| public SelfSignedCertificate(Date notBefore, Date notAfter) throws CertificateException Creates a new instance. Algorithm: RSA | |
| SelfSignedCertificate | back to summary |
|---|---|
| public SelfSignedCertificate(Date notBefore, Date notAfter, String algorithm, int bits) throws CertificateException Creates a new instance. | |
| SelfSignedCertificate | back to summary |
|---|---|
| public SelfSignedCertificate(String fqdn) throws CertificateException Creates a new instance. Algorithm: RSA
| |
| SelfSignedCertificate | back to summary |
|---|---|
| public SelfSignedCertificate(String fqdn, String algorithm, int bits) throws CertificateException Creates a new instance. | |
| SelfSignedCertificate | back to summary |
|---|---|
| public SelfSignedCertificate(String fqdn, Date notBefore, Date notAfter) throws CertificateException Creates a new instance. Algorithm: RSA | |
| SelfSignedCertificate | back to summary |
|---|---|
| public SelfSignedCertificate(String fqdn, Date notBefore, Date notAfter, String algorithm, int bits) throws CertificateException Creates a new instance. | |
| SelfSignedCertificate | back to summary |
|---|---|
| public SelfSignedCertificate(String fqdn, SecureRandom random, int bits) throws CertificateException Creates a new instance. Algorithm: RSA
| |
| SelfSignedCertificate | back to summary |
|---|---|
| public SelfSignedCertificate(String fqdn, SecureRandom random, String algorithm, int bits) throws CertificateException Creates a new instance.
| |
| SelfSignedCertificate | back to summary |
|---|---|
| public SelfSignedCertificate(String fqdn, SecureRandom random, int bits, Date notBefore, Date notAfter) throws CertificateException Creates a new instance. Algorithm: RSA
| |
| SelfSignedCertificate | back to summary |
|---|---|
| public SelfSignedCertificate(String fqdn, SecureRandom random, int bits, Date notBefore, Date notAfter, String algorithm) throws CertificateException Creates a new instance.
| |
| cert | back to summary |
|---|---|
| public X509Certificate cert() Returns the generated X.509 certificate. | |
| certificate | back to summary |
|---|---|
| public File certificate() Returns the generated X.509 certificate file in PEM format. | |
| delete | back to summary |
|---|---|
| public void delete() Deletes the generated X.509 certificate file and EC/RSA private key file. | |
| isBouncyCastleAvailable | back to summary |
|---|---|
| private static boolean isBouncyCastleAvailable() | |
| key | back to summary |
|---|---|
| public PrivateKey key() Returns the generated EC/RSA private key. | |
| newSelfSignedCertificate | back to summary |
|---|---|
| pack-priv static String[] newSelfSignedCertificate(String fqdn, PrivateKey key, X509Certificate cert) throws IOException, CertificateEncodingException | |
| privateKey | back to summary |
|---|---|
| public File privateKey() Returns the generated EC/RSA private key file in PEM format. | |
| safeClose | back to summary |
|---|---|
| private static void safeClose(File keyFile, OutputStream keyOut) | |
| safeDelete | back to summary |
|---|---|
| private static void safeDelete(File certFile) | |