package no.digipost.signature.client;

import no.digipost.signature.client.security.KeyStoreConfig;

public class TestCertificates {

    private static final String password = "yJPvczYAoirFfC9M";

    /**
     * Create Java Keystore from .p12-fil
     * <p>
     * Please note: The JKS keystore uses a proprietary format.
     * It is recommended to migrate to PKCS12 which is an industry standard format.
     * But if you want, this is how it is done for the Bring certificates used here:
     * <p>
     * <p>
     * keytool -importkeystore -srckeystore [MY_FILE.p12] -srcstoretype pkcs12
     * -srcalias [ALIAS_SRC] -destkeystore [MY_KEYSTORE.jks]
     * -deststoretype jks -deststorepass [PASSWORD_JKS] -destalias [ALIAS_DEST]
     * <p>
     * <p>
     * Example:
     * keytool -importkeystore -srckeystore Bring_Expired_Certificate_For_Testing.p12
     * -srcstoretype pkcs12 -srcalias 'digipost testintegrasjon for digital post'
     * -destkeystore bring-expired-keystore-for-testing.jks -deststoretype jks -deststorepass yJPvczYAoirFfC9M
     * -destalias 'digipost testintegrasjon for digital post'
     * <p>
     * The srcalias is often the friendly name of the certificate in lower case.
     */

    public static KeyStoreConfig getJavaKeyStore() {
        return KeyStoreConfig.fromOrganizationCertificate(
                TestCertificates.class.getResourceAsStream("/bring-expired-certificate-for-testing.p12"),
                password
        );
    }

    public static KeyStoreConfig getOrganizationCertificateKeyStore() {
        return KeyStoreConfig.fromJavaKeyStore(
                TestCertificates.class.getResourceAsStream("/bring-expired-keystore-for-testing.jks"),
                "digipost testintegrasjon for digital post", password, password
        );
    }


}

@@ -94,16 +94,73 @@
 onfig;%0A%0A
+import java.io.IOException;%0Aimport java.io.InputStream;%0A%0A
 public c
@@ -1329,74 +1329,41 @@
     
-return Key
+try (Input
 St
-o
 re
-Config.fromOrganizationCertificate(%0A               
+am p12InputStream =
  Tes
@@ -1447,17 +1447,19 @@
 ng.p12%22)
-,
+) %7B
 %0A       
@@ -1463,19 +1463,81 @@
         
-   
+return KeyStoreConfig.fromOrganizationCertificate(p12InputStream,
  passwor
@@ -1529,32 +1529,34 @@
 Stream, password
+);
 %0A        );%0A    
@@ -1540,34 +1540,126 @@
 sword);%0A        
-);
+%7D catch (IOException e) %7B%0A            throw new RuntimeException(e.getMessage(), e);%0A        %7D
 %0A    %7D%0A%0A    publ
@@ -1734,63 +1734,41 @@
     
-return Key
+try (Input
 St
-o
 re
-Config.fromJavaKey
+am jksInput
 St
-o
 re
-(%0A               
+am =
  Tes
@@ -1849,17 +1849,19 @@
 ng.jks%22)
-,
+) %7B
 %0A       
@@ -1865,19 +1865,70 @@
         
-   
+return KeyStoreConfig.fromJavaKeyStore(jksInputStream,
  %22digipo
@@ -1983,16 +1983,18 @@
 password
+);
 %0A       
@@ -1994,18 +1994,110 @@
         
-);
+%7D catch (IOException e) %7B%0A            throw new RuntimeException(e.getMessage(), e);%0A        %7D
 %0A    %7D%0A%0A

Read test certs with try-with-resources