Search in sources :

Example 1 with X509KeyStore

use of com.github.tomakehurst.wiremock.http.ssl.X509KeyStore in project wiremock by wiremock.

the class SslContexts method buildKeyStore.

private static X509KeyStore buildKeyStore(KeyStoreSettings browserProxyCaKeyStore) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, CertificateGenerationUnsupportedException {
    final CertificateAuthority certificateAuthority = CertificateAuthority.generateCertificateAuthority();
    KeyStore keyStore = KeyStore.getInstance(browserProxyCaKeyStore.type());
    char[] password = browserProxyCaKeyStore.password().toCharArray();
    keyStore.load(null, password);
    keyStore.setKeyEntry("wiremock-ca", certificateAuthority.key(), password, certificateAuthority.certificateChain());
    browserProxyCaKeyStore.getSource().save(keyStore);
    return new X509KeyStore(keyStore, password);
}
Also used : X509KeyStore(com.github.tomakehurst.wiremock.http.ssl.X509KeyStore) CertificateAuthority(com.github.tomakehurst.wiremock.http.ssl.CertificateAuthority) X509KeyStore(com.github.tomakehurst.wiremock.http.ssl.X509KeyStore) KeyStore(java.security.KeyStore)

Example 2 with X509KeyStore

use of com.github.tomakehurst.wiremock.http.ssl.X509KeyStore in project wiremock by wiremock.

the class GetCaCertTask method execute.

@Override
public ResponseDefinition execute(Admin admin, Request request, PathParams pathParams) {
    BrowserProxySettings browserProxySettings = admin.getOptions().browserProxySettings();
    KeyStoreSettings caKeyStore = browserProxySettings.caKeyStore();
    try {
        X509KeyStore x509KeyStore = new X509KeyStore(caKeyStore.loadStore(), caKeyStore.password().toCharArray());
        X509Certificate certificate = x509KeyStore.getCertificateAuthority().certificateChain()[0];
        return new ResponseDefinitionBuilder().withStatus(HTTP_OK).withHeader("Content-Type", "application/x-pem-file").withBody("-----BEGIN CERTIFICATE-----\r\n" + BASE64_ENCODER.encodeToString(certificate.getEncoded()) + "\r\n" + "-----END CERTIFICATE-----").build();
    } catch (Exception e) {
        String message = "Failed to export certificate authority cert from " + caKeyStore.path();
        admin.getOptions().notifier().error(message, e);
        return new ResponseDefinition(HTTP_INTERNAL_ERROR, message);
    }
}
Also used : ResponseDefinitionBuilder(com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder) KeyStoreSettings(com.github.tomakehurst.wiremock.common.ssl.KeyStoreSettings) X509KeyStore(com.github.tomakehurst.wiremock.http.ssl.X509KeyStore) ResponseDefinition(com.github.tomakehurst.wiremock.http.ResponseDefinition) BrowserProxySettings(com.github.tomakehurst.wiremock.common.BrowserProxySettings) X509Certificate(java.security.cert.X509Certificate)

Example 3 with X509KeyStore

use of com.github.tomakehurst.wiremock.http.ssl.X509KeyStore in project wiremock by wiremock.

the class HttpsBrowserProxyAcceptanceTest method certificateAuthorityCertCanBeDownloaded.

@Test
@DisabledForJreRange(min = JRE.JAVA_17, disabledReason = "does not support generating certificates at runtime")
public void certificateAuthorityCertCanBeDownloaded() throws Exception {
    WireMockTestClient proxyTestClient = new WireMockTestClient(proxy.getPort());
    WireMockResponse certResponse = proxyTestClient.get("/__admin/certs/wiremock-ca.crt");
    assertEquals(200, certResponse.statusCode());
    assertEquals("application/x-pem-file", certResponse.firstHeader("Content-Type"));
    Certificate cert = decode(certResponse.content());
    X509KeyStore keyStore = new X509KeyStore(HttpsAcceptanceTest.readKeyStore(NO_PREEXISTING_KEYSTORE_PATH, "password"), "password".toCharArray());
    assertEquals(keyStore.getCertificateAuthority().certificateChain()[0], cert);
}
Also used : WireMockResponse(com.github.tomakehurst.wiremock.testsupport.WireMockResponse) WireMockTestClient(com.github.tomakehurst.wiremock.testsupport.WireMockTestClient) X509KeyStore(com.github.tomakehurst.wiremock.http.ssl.X509KeyStore) Certificate(java.security.cert.Certificate) Test(org.junit.jupiter.api.Test) DisabledForJreRange(org.junit.jupiter.api.condition.DisabledForJreRange)

Aggregations

X509KeyStore (com.github.tomakehurst.wiremock.http.ssl.X509KeyStore)3 ResponseDefinitionBuilder (com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder)1 BrowserProxySettings (com.github.tomakehurst.wiremock.common.BrowserProxySettings)1 KeyStoreSettings (com.github.tomakehurst.wiremock.common.ssl.KeyStoreSettings)1 ResponseDefinition (com.github.tomakehurst.wiremock.http.ResponseDefinition)1 CertificateAuthority (com.github.tomakehurst.wiremock.http.ssl.CertificateAuthority)1 WireMockResponse (com.github.tomakehurst.wiremock.testsupport.WireMockResponse)1 WireMockTestClient (com.github.tomakehurst.wiremock.testsupport.WireMockTestClient)1 KeyStore (java.security.KeyStore)1 Certificate (java.security.cert.Certificate)1 X509Certificate (java.security.cert.X509Certificate)1 Test (org.junit.jupiter.api.Test)1 DisabledForJreRange (org.junit.jupiter.api.condition.DisabledForJreRange)1