Skip to content

Commit

Permalink
Cater for Windows mess with backslashes in file path when rendering as
Browse files Browse the repository at this point in the history
string
  • Loading branch information
uhurusurfa committed Dec 5, 2024
1 parent 4b84dca commit d5a3798
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions Server/src/test/java/org/openas2/app/CertificatesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class CertificatesTest extends BaseServerSetup {
private static final String url = "https://localhost:" + receiverPort + "/";
private File customPropsFile = null;
private File sslCertsFile = null; // The private key and certificate for the HTTPS
private File sslTrustCertsFile = null; // The public key for the SSL private key
private String sslTrustCertsFilePath = null; // The public key for the SSL private key
private X509CertificateFactory trustFx = null; // The trust certificates
protected static Message testMsg; // Created just once for all tests to minimise runtime
private InternetHeaders ih = null;
Expand Down Expand Up @@ -84,21 +84,25 @@ public void setUp() throws Exception {
String tgtHostName = "test.openas2.org";
this.alias = tgtHostName;
// Create the SSL file for the server to use
String sslCertsFilePath = sslCertsFile.getAbsolutePath();
// Switch to forward slash to avoid backslash being dropped when creating property string when on Windows
String sslCertsFilePath = sslCertsFile.getAbsolutePath().replace("\\", "/");
this.certFx = genSelfSignedCert(alias, sslCertsFilePath, "RSA", "SHA256", 2048, tgtHostName);
// Create the trust store with the public key so the certificate returned from the server is trusted
sslTrustCertsFile = Files.createFile(Paths.get(tmpDirAbsolutePath, "ssl_trust_certs.p12")).toFile();
File sslTrustCertsFile = Files.createFile(Paths.get(tmpDirAbsolutePath, "ssl_trust_certs.p12")).toFile();
// Switch to forward slash to avoid backslash being dropped when creating property string when on Windows
sslTrustCertsFilePath = sslTrustCertsFile.getAbsolutePath().replace("\\", "/");
String trustAlias = "trust-" + tgtHostName;
this.certFx.exportPublicKey(sslTrustCertsFile.getAbsolutePath(), this.alias, trustAlias, password);
this.certFx.exportPublicKey(sslTrustCertsFilePath, this.alias, trustAlias, password);
this.trustFx = new X509CertificateFactory();
this.trustFx.setFilename(sslTrustCertsFile.getAbsolutePath());
this.trustFx.setFilename(sslTrustCertsFilePath);
this.trustFx.setPassword(password);
this.trustFx.setKeyStore(AS2Util.getCryptoHelper().getKeyStore());
this.trustFx.load();

customPropsFile = Files.createFile(Paths.get(tmpDirAbsolutePath, "openas2.properties")).toFile();
System.setProperty(Properties.OPENAS2_PROPERTIES_FILE_PROP, customPropsFile.getAbsolutePath());
FileOutputStream fos = new FileOutputStream(customPropsFile);
// Switch to forward slash to avoid backslash being dropped when creating property string when on Windows
fos.write(("ssl_keystore=" + sslCertsFilePath + "\n").getBytes());
fos.write(("ssl_keystore_password=" + new String(password) + "\n").getBytes());
//fos.write(("ssl_trust_keystore=" + sslTrustCertsFile.getAbsolutePath() + "\n").getBytes());
Expand Down Expand Up @@ -147,7 +151,7 @@ public void a1_shouldFailSSLConnect() throws Exception {
public void a2_shouldConnect() throws Exception {
FileOutputStream fos = new FileOutputStream(customPropsFile, true);
fos.write(("ssl_trust_keystore.enabled=true\n").getBytes());
fos.write(("ssl_trust_keystore=" + sslTrustCertsFile.getAbsolutePath() + "\n").getBytes());
fos.write(("ssl_trust_keystore=" + sslTrustCertsFilePath + "\n").getBytes());
fos.write(("ssl_trust_keystore_password=" + new String(password) + "\n").getBytes());
fos.close();
super.refresh();
Expand Down

0 comments on commit d5a3798

Please sign in to comment.