feat: Basic verify certificate creation in creator

This commit is contained in:
Magnus Leßmann (@MarkL4YG) 2024-11-17 22:15:29 +01:00
parent 9ec619a380
commit 722092588f
3 changed files with 65 additions and 6 deletions

View file

@ -11,12 +11,12 @@ import java.nio.file.StandardOpenOption;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.Nullable; import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.zeroturnaround.exec.ProcessExecutor; import org.zeroturnaround.exec.ProcessExecutor;
import org.zeroturnaround.exec.StartedProcess; import org.zeroturnaround.exec.StartedProcess;
@ -59,7 +59,7 @@ public class OpenSSLCertificateCreator {
return certSubject; return certSubject;
} }
@Nullable @NonNull
public OpenSSLCertificateResult createCertificate(CertificateRequest request) public OpenSSLCertificateResult createCertificate(CertificateRequest request)
throws CommandLineOperationException, InterruptedException { throws CommandLineOperationException, InterruptedException {
Path tmpDir; Path tmpDir;
@ -73,9 +73,9 @@ public class OpenSSLCertificateCreator {
Path rootCert = createCertificate(request, keyFile, tmpDir.resolve("root.crt")); Path rootCert = createCertificate(request, keyFile, tmpDir.resolve("root.crt"));
Path childKey = createKeyfile(request, tmpDir.resolve("child.key")); Path childKey = createKeyfile(request, tmpDir.resolve("child.key"));
Path unsignedCert = createCertificate(request, childKey, tmpDir.resolve("child.csr")); Path unsignedCert = createSigningRequest(request, childKey, tmpDir.resolve("child.csr"));
Path signedCert = signCertificate(request, rootCert, keyFile, unsignedCert); Path signedCert = signCertificate(request, rootCert, keyFile, unsignedCert);
return new OpenSSLCertificateResult(tmpDir); return new OpenSSLCertificateResult(tmpDir, signedCert, childKey);
} }
private Path createKeyfile(CertificateRequest request, Path outFile) private Path createKeyfile(CertificateRequest request, Path outFile)
@ -117,6 +117,7 @@ public class OpenSSLCertificateCreator {
.command( .command(
resolveOpenSSL(), resolveOpenSSL(),
"req", "req",
"-x509",
"-new", "-new",
"-passin", "-passin",
"env:KEY_PASS", "env:KEY_PASS",
@ -146,6 +147,57 @@ public class OpenSSLCertificateCreator {
return outFile; return outFile;
} }
private Path createSigningRequest(CertificateRequest request, Path keyFile, Path outFile)
throws CommandLineOperationException, InterruptedException {
log.atDebug().log("Writing new certificate signing request file {}", outFile);
String certSubject = buildSubjectArg(request);
try {
StartedProcess certGenProc = new ProcessExecutor()
.command(
resolveOpenSSL(),
"req",
"-new",
"-passin",
"env:KEY_PASS",
"-key",
keyFile.toString(),
"-sha256",
"-out",
outFile.toString(),
"-passout",
"env:KEY_PASS",
"-utf8",
"-subj",
certSubject
)
.environment("KEY_PASS", request.getOid())
.redirectOutput(Slf4jStream.ofCaller().asDebug())
.redirectError(Slf4jStream.ofCaller().asError())
.start();
certGenProc.getFuture().get();
} catch (IOException e) {
throw new CommandLineOperationException("Failure running OpenSSL req command.", e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
return outFile;
}
public boolean verifyCertificate(Path certFile) throws CommandLineOperationException {
try {
StartedProcess verifyCommand = new ProcessExecutor()
.command(resolveOpenSSL(), "x509", "-in", certFile.toString(), "-text", "-noout")
.redirectOutput(Slf4jStream.ofCaller().asDebug())
.redirectError(Slf4jStream.ofCaller().asError())
.start();
var verifyResult = verifyCommand.getFuture().get();
return verifyResult.getExitValue() == 0;
} catch (IOException | InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
private Path signCertificate(CertificateRequest request, Path caCert, Path caKey, Path csrFile) private Path signCertificate(CertificateRequest request, Path caCert, Path caKey, Path csrFile)
throws CommandLineOperationException, InterruptedException { throws CommandLineOperationException, InterruptedException {
Path outFile = csrFile.resolveSibling(csrFile.getFileName().toString().replace(".csr", ".crt")); Path outFile = csrFile.resolveSibling(csrFile.getFileName().toString().replace(".csr", ".crt"));

View file

@ -6,14 +6,18 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Set; import java.util.Set;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
@RequiredArgsConstructor(access = AccessLevel.PACKAGE) @RequiredArgsConstructor(access = AccessLevel.PACKAGE)
@Getter
public class OpenSSLCertificateResult implements AutoCloseable { public class OpenSSLCertificateResult implements AutoCloseable {
private final Path tmpDir; private final Path tmpDir;
private final Path certificatePath;
private final Path privateKeyPath;
@Override @Override
public void close() throws IOException { public void close() throws IOException {

View file

@ -1,5 +1,7 @@
package de.mlessmann.certassist; package de.mlessmann.certassist;
import static org.assertj.core.api.Assertions.*;
import de.mlessmann.certassist.openssl.CertificateRequest; import de.mlessmann.certassist.openssl.CertificateRequest;
import de.mlessmann.certassist.openssl.CertificateRequest.RequestType; import de.mlessmann.certassist.openssl.CertificateRequest.RequestType;
import de.mlessmann.certassist.openssl.CertificateRequestExtension; import de.mlessmann.certassist.openssl.CertificateRequestExtension;
@ -29,6 +31,7 @@ class TestOpenSSLCertificateCreator {
.build(); .build();
try (var cert = openSSLCertificateCreator.createCertificate(certRequest)) { try (var cert = openSSLCertificateCreator.createCertificate(certRequest)) {
assertThat(openSSLCertificateCreator.verifyCertificate(cert.getCertificatePath())).isEqualTo(true);
System.out.println("Certificate created: " + cert); System.out.println("Certificate created: " + cert);
} }
} }