Class TestSslUtils
java.lang.Object
org.apache.kafka.test.TestSslUtils
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic classstatic final class -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Certificate>
voidaddCertificateToTrustStore(String filename, org.apache.kafka.common.config.types.Password password, String alias, T cert) static voidconvertToPem(Map<String, Object> sslProps, boolean writeToFile, boolean encryptPrivateKey) static voidconvertToPemWithoutFiles(Properties sslProps) static StringcreateCrlPem(String algorithm, KeyPair keyPair, X509Certificate caCert, List<X509Certificate> revokedCerts) Create CRL for a CA and return the PEM encoded formatstatic voidcreateKeyStore(String filename, org.apache.kafka.common.config.types.Password password, org.apache.kafka.common.config.types.Password keyPassword, String alias, Key privateKey, Certificate cert) Creates a keystore with a single key and saves it to a file.static voidcreateKeyStore(String filename, org.apache.kafka.common.config.types.Password password, org.apache.kafka.common.config.types.Password keyPassword, String alias, Key privateKey, Certificate[] certChain) createSslConfig(boolean useClientCert, boolean trustStore, org.apache.kafka.common.network.ConnectionMode connectionMode, File trustStoreFile, String certAlias) createSslConfig(boolean useClientCert, boolean trustStore, org.apache.kafka.common.network.ConnectionMode connectionMode, File trustStoreFile, String certAlias, String cn) createSslConfig(boolean useClientCert, boolean createTrustStore, org.apache.kafka.common.network.ConnectionMode connectionMode, File trustStoreFile, String certAlias, String cn, TestSslUtils.CertificateBuilder certBuilder) createSslConfig(String keyManagerAlgorithm, String trustManagerAlgorithm, String tlsProtocol) static <T extends Certificate>
voidcreateTrustStore(String filename, org.apache.kafka.common.config.types.Password password, Map<String, T> certs) static org.apache.kafka.common.config.types.PasswordexportCertificates(String storePath, org.apache.kafka.common.config.types.Password storePassword, String storeType) static org.apache.kafka.common.config.types.PasswordexportPrivateKey(String storePath, org.apache.kafka.common.config.types.Password storePassword, org.apache.kafka.common.config.types.Password keyPassword, String storeType, org.apache.kafka.common.config.types.Password pemKeyPassword) static X509CertificategenerateCertificate(String dn, KeyPair pair, int days, String algorithm) Create a self-signed X.509 Certificate.generateConfigsWithCertificateChains(String tlsProtocol) method to generate ssl configs for keystore with large number of entries.static X509CRLgenerateCrl(String algorithm, KeyPair keyPair, X509Certificate caCert, List<X509Certificate> revokedCerts) Create CRL for a CA and return the PEM encoded formatstatic KeyPairgenerateKeyPair(String algorithm) static X509CertificategenerateSignedCertificate(String dn, KeyPair keyPair, int daysBeforeNow, int daysAfterNow, String issuer, KeyPair parentKeyPair, String algorithm, boolean isCA, boolean isServerCert, boolean isClientCert) Generate a signed certificate.static X509CertificategenerateSignedCertificate(String dn, KeyPair keyPair, int daysBeforeNow, int daysAfterNow, String issuer, KeyPair parentKeyPair, String algorithm, boolean isCA, boolean isServerCert, boolean isClientCert, byte[] rawSan) static X509CertificategenerateSignedCertificate(String dn, KeyPair keyPair, int daysBeforeNow, int daysAfterNow, String issuer, KeyPair parentKeyPair, String algorithm, boolean isCA, boolean isServerCert, boolean isClientCert, String[] hostNames) static X509CertificategenerateSignedCertificate(String dn, KeyPair keyPair, int daysBeforeNow, int daysAfterNow, String issuer, KeyPair parentKeyPair, String algorithm, boolean isCA, boolean isServerCert, boolean isClientCert, InetAddress hostAddress) static X509CertificategenerateSignedCertificateMilliUnit(String dn, KeyPair keyPair, long millisBeforeNow, long millisAfterNow, String issuer, KeyPair parentKeyPair, String algorithm, boolean isCA, boolean isServerCert, boolean isClientCert) Generate a signed certificate.static X509CertificategenerateSignedCertificateWithUriSans(String dn, KeyPair keyPair, int daysBeforeNow, int daysAfterNow, String issuer, KeyPair parentKeyPair, String algorithm, boolean isCA, boolean isServerCert, boolean isClientCert, String... uris) Generate a signed certificate with URI SANs (e.g., for SPIFFE IDs).static Stringpem(Certificate cert) static Stringstatic Stringpem(PrivateKey privateKey, org.apache.kafka.common.config.types.Password password)
-
Field Details
-
TRUST_STORE_PASSWORD
- See Also:
-
DEFAULT_TLS_PROTOCOL_FOR_TESTS
- See Also:
-
-
Constructor Details
-
TestSslUtils
public TestSslUtils()
-
-
Method Details
-
generateCertificate
public static X509Certificate generateCertificate(String dn, KeyPair pair, int days, String algorithm) throws CertificateException Create a self-signed X.509 Certificate. From http://bfo.com/blog/2011/03/08/odds_and_ends_creating_a_new_x_509_certificate.html.- Parameters:
dn- the X.509 Distinguished Name, eg "CN=Test, L=London, C=GB"pair- the KeyPairdays- how many days from now the Certificate is valid for, or - for negative values - how many days before nowalgorithm- the signing algorithm, eg "SHA1withRSA"- Returns:
- the self-signed certificate
- Throws:
CertificateException- thrown if a security error or an IO error occurred.
-
generateSignedCertificate
public static X509Certificate generateSignedCertificate(String dn, KeyPair keyPair, int daysBeforeNow, int daysAfterNow, String issuer, KeyPair parentKeyPair, String algorithm, boolean isCA, boolean isServerCert, boolean isClientCert) throws CertificateException Generate a signed certificate. Self-signed, if no issuer and parentKeyPair are supplied- Parameters:
dn- The distinguished name of this certificatekeyPair- A key pairdaysBeforeNow- how many days before now the Certificate is valid fordaysAfterNow- how many days from now the Certificate is valid forissuer- The issuer who signs the certificate. Leave null if you want to generate a root CA.parentKeyPair- The key pair of the issuer. Leave null if you want to generate a root CA.algorithm- the signing algorithm, eg "SHA1withRSA"- Returns:
- the signed certificate
- Throws:
CertificateException
-
generateSignedCertificateMilliUnit
public static X509Certificate generateSignedCertificateMilliUnit(String dn, KeyPair keyPair, long millisBeforeNow, long millisAfterNow, String issuer, KeyPair parentKeyPair, String algorithm, boolean isCA, boolean isServerCert, boolean isClientCert) throws CertificateException Generate a signed certificate. Self-signed, if no issuer and parentKeyPair are supplied- Parameters:
dn- The distinguished name of this certificatekeyPair- A key pairmillisBeforeNow- how many milliseconds before now the Certificate is valid formillisAfterNow- how many milliseconds from now the Certificate is valid forissuer- The issuer who signs the certificate. Leave null if you want to generate a root CA.parentKeyPair- The key pair of the issuer. Leave null if you want to generate a root CA.algorithm- the signing algorithm, eg "SHA1withRSA"- Returns:
- the signed certificate
- Throws:
CertificateException
-
generateSignedCertificate
public static X509Certificate generateSignedCertificate(String dn, KeyPair keyPair, int daysBeforeNow, int daysAfterNow, String issuer, KeyPair parentKeyPair, String algorithm, boolean isCA, boolean isServerCert, boolean isClientCert, String[] hostNames) throws CertificateException, IOException - Throws:
CertificateExceptionIOException
-
generateSignedCertificate
public static X509Certificate generateSignedCertificate(String dn, KeyPair keyPair, int daysBeforeNow, int daysAfterNow, String issuer, KeyPair parentKeyPair, String algorithm, boolean isCA, boolean isServerCert, boolean isClientCert, byte[] rawSan) throws CertificateException, IOException - Throws:
CertificateExceptionIOException
-
generateSignedCertificate
public static X509Certificate generateSignedCertificate(String dn, KeyPair keyPair, int daysBeforeNow, int daysAfterNow, String issuer, KeyPair parentKeyPair, String algorithm, boolean isCA, boolean isServerCert, boolean isClientCert, InetAddress hostAddress) throws CertificateException, IOException - Throws:
CertificateExceptionIOException
-
generateSignedCertificateWithUriSans
public static X509Certificate generateSignedCertificateWithUriSans(String dn, KeyPair keyPair, int daysBeforeNow, int daysAfterNow, String issuer, KeyPair parentKeyPair, String algorithm, boolean isCA, boolean isServerCert, boolean isClientCert, String... uris) throws CertificateException, IOException Generate a signed certificate with URI SANs (e.g., for SPIFFE IDs).- Throws:
CertificateExceptionIOException
-
generateKeyPair
- Throws:
NoSuchAlgorithmException
-
createKeyStore
public static void createKeyStore(String filename, org.apache.kafka.common.config.types.Password password, org.apache.kafka.common.config.types.Password keyPassword, String alias, Key privateKey, Certificate cert) throws GeneralSecurityException, IOException Creates a keystore with a single key and saves it to a file.- Parameters:
filename- String file to savepassword- String store password to set on keystorekeyPassword- String key password to set on keyalias- String alias to use for the keyprivateKey- Key to save in keystorecert- Certificate to use as certificate chain associated to key- Throws:
GeneralSecurityException- for any error with the security APIsIOException- if there is an I/O error saving the file
-
createKeyStore
public static void createKeyStore(String filename, org.apache.kafka.common.config.types.Password password, org.apache.kafka.common.config.types.Password keyPassword, String alias, Key privateKey, Certificate[] certChain) throws GeneralSecurityException, IOException - Throws:
GeneralSecurityExceptionIOException
-
createTrustStore
public static <T extends Certificate> void createTrustStore(String filename, org.apache.kafka.common.config.types.Password password, Map<String, T> certs) throws GeneralSecurityException, IOException- Throws:
GeneralSecurityExceptionIOException
-
addCertificateToTrustStore
public static <T extends Certificate> void addCertificateToTrustStore(String filename, org.apache.kafka.common.config.types.Password password, String alias, T cert) throws GeneralSecurityException, IOException - Throws:
GeneralSecurityExceptionIOException
-
createSslConfig
-
createSslConfig
public static Map<String,Object> createSslConfig(boolean useClientCert, boolean trustStore, org.apache.kafka.common.network.ConnectionMode connectionMode, File trustStoreFile, String certAlias) throws IOException, GeneralSecurityException - Throws:
IOExceptionGeneralSecurityException
-
createSslConfig
public static Map<String,Object> createSslConfig(boolean useClientCert, boolean trustStore, org.apache.kafka.common.network.ConnectionMode connectionMode, File trustStoreFile, String certAlias, String cn) throws IOException, GeneralSecurityException - Throws:
IOExceptionGeneralSecurityException
-
createSslConfig
public static Map<String,Object> createSslConfig(boolean useClientCert, boolean createTrustStore, org.apache.kafka.common.network.ConnectionMode connectionMode, File trustStoreFile, String certAlias, String cn, TestSslUtils.CertificateBuilder certBuilder) throws IOException, GeneralSecurityException - Throws:
IOExceptionGeneralSecurityException
-
convertToPem
-
convertToPemWithoutFiles
- Throws:
Exception
-
exportCertificates
-
exportPrivateKey
public static org.apache.kafka.common.config.types.Password exportPrivateKey(String storePath, org.apache.kafka.common.config.types.Password storePassword, org.apache.kafka.common.config.types.Password keyPassword, String storeType, org.apache.kafka.common.config.types.Password pemKeyPassword) throws Exception - Throws:
Exception
-
pem
- Throws:
IOException
-
pem
public static String pem(PrivateKey privateKey, org.apache.kafka.common.config.types.Password password) throws IOException - Throws:
IOException
-
createCrlPem
public static String createCrlPem(String algorithm, KeyPair keyPair, X509Certificate caCert, List<X509Certificate> revokedCerts) throws Exception Create CRL for a CA and return the PEM encoded format- Parameters:
algorithm- algorithm of CA keypairkeyPair- CA keypaircaCert- CA certrevokedCerts- list of revoked certs- Returns:
- PEM encoded format of CRL
- Throws:
Exception
-
generateCrl
public static X509CRL generateCrl(String algorithm, KeyPair keyPair, X509Certificate caCert, List<X509Certificate> revokedCerts) throws Exception Create CRL for a CA and return the PEM encoded format- Parameters:
algorithm- algorithm of CA keypairkeyPair- CA keypaircaCert- CA certrevokedCerts- list of revoked certs- Returns:
- X509CRL crl
- Throws:
Exception
-
pem
- Throws:
IOException
-
generateConfigsWithCertificateChains
public static Map<String,Object> generateConfigsWithCertificateChains(String tlsProtocol) throws Exception method to generate ssl configs for keystore with large number of entries. This is used to verify large key stores and post-handshake messages in SslEngineValidator with TLSv3/JDK17+- Parameters:
tlsProtocol-- Returns:
- ssl configs
- Throws:
Exception
-