package io.github.olivierlemasle.tests.it;

import static org.junit.Assume.assumeTrue;
import io.github.olivierlemasle.ca.CA;
import io.github.olivierlemasle.ca.CSR;
import io.github.olivierlemasle.ca.CertificateAuthority;
import io.github.olivierlemasle.ca.DistinguishedName;

import java.io.File;
import java.io.IOException;
import java.lang.ProcessBuilder.Redirect;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class OpenSslIT {

  @BeforeClass
  @AfterClass
  public static void clean() {
    final File caCertPath = new File("ca.cer");
    caCertPath.delete();
    final File reqPath = new File("cert.req");
    reqPath.delete();
    final File certPath = new File("cert.cer");
    certPath.delete();
  }

  @Before
  public void checkPlatform() {
    assumeTrue(TestUtils.opensslExists());
  }

  @Test
  public void completeTest() throws IOException, InterruptedException, NoSuchAlgorithmException,
      KeyStoreException, KeyManagementException, CertificateException {
    // Create a CA
    System.out.println("Generate CA...");
    final DistinguishedName caName = CA.dn("CN=CA-Test");
    final CertificateAuthority ca = CA.init().setName(caName).build();
    // Export the CA certificate
    final X509Certificate caCert = ca.getCaCertificate();
    CA.export(caCert).saveCertificate("ca.cer");
    System.out.println("CA ready. CA certificate saved to \"ca.cer\".");

    // Generate CSR using OpenSSL
    System.out.println("Generate CSR with \"CSR.csr\"...");
    generateCsr();

    // Load the generated CSR, sign it and export the resulting certificate
    System.out.println("Sign CSR...");
    final CSR csr = CA.loadCsr("CSR.csr").getCsr();
    final X509Certificate cert = ca.sign(csr);
    CA.export(cert).saveCertificate("cert.cer");
    System.out.println("CSR signed. Certificate saved to \"cert.cer\".");
  }

  /**
   * {@code openssl req -nodes -newkey rsa:2048 -keyout private.key -out CSR.csr -subj "/CN=localhost"}
   * 
   * @throws IOException
   * @throws InterruptedException
   */
  private void generateCsr() throws IOException, InterruptedException {
    final Process process = new ProcessBuilder("openssl", "req", "-nodes", "-newkey rsa:2048",
        "-keyout private.key", "-out CSR.csr", "-subj \"/CN=localhost\"")
        .redirectError(Redirect.INHERIT)
        .redirectOutput(Redirect.INHERIT)
        .start();

    process.waitFor();
  }

}

@@ -2457,25 +2457,28 @@
 s%22, %22-newkey
- 
+%22, %22
 rsa:2048%22,%0A 
@@ -2488,25 +2488,28 @@
     %22-keyout
- 
+%22, %22
 private.key%22
@@ -2511,25 +2511,28 @@
 .key%22, %22-out
- 
+%22, %22
 CSR.csr%22, %22-
@@ -2535,17 +2535,20 @@
 , %22-subj
- 
+%22, %22
 %5C%22/CN=lo

Fix OpenSSL request