@Test void autoGeneratedCertsAreValid(@TempDirectory Path tempDir) throws Exception { Path certificate = tempDir.resolve("server3.crt"); Path key = tempDir.resolve("server3.key"); boolean wasCreated = TLS.createSelfSignedCertificateIfMissing(key, certificate); assertTrue(wasCreated); checkKeyPair(key, certificate); }
public static boolean createSelfSignedCertificateIfMissing(Path key, Path certificate) throws IOException { if (Files.exists(certificate) || Files.exists(key)) { return false; } createDirectories(certificate.getParent()); createDirectories(key.getParent()); Path keyFile = Files.createTempFile(key.getParent(), "client-key", ".tmp"); Path certFile = Files.createTempFile(certificate.getParent(), "client-cert", ".tmp"); try { createSelfSignedCertificate(new Date(), keyFile, certFile); } catch (CertificateException | NoSuchAlgorithmException | OperatorCreationException e) { throw new TLSEnvironmentException("Could not generate certificate: " + e.getMessage(), e); } Files.move(keyFile, key, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); Files.move(certFile, certificate, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); return true; }
TLS { public static boolean createSelfSignedCertificateIfMissing(Path key, Path certificate) throws IOException { if (Files.exists(certificate) || Files.exists(key)) { return false; } createDirectories(certificate.getParent()); createDirectories(key.getParent()); Path keyFile = Files.createTempFile(key.getParent(), "client-key", ".tmp"); Path certFile = Files.createTempFile(certificate.getParent(), "client-cert", ".tmp"); try { createSelfSignedCertificate(new Date(), keyFile, certFile); } catch (CertificateException | NoSuchAlgorithmException | OperatorCreationException e) { throw new TLSEnvironmentException("Could not generate certificate: " + e.getMessage(), e); } Files.move(keyFile, key, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); Files.move(certFile, certificate, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); return true; } }
TLS { public static boolean createSelfSignedCertificateIfMissing(Path key, Path certificate) throws IOException { if (Files.exists(certificate) || Files.exists(key)) { return false; } createDirectories(certificate.getParent()); createDirectories(key.getParent()); Path keyFile = Files.createTempFile(key.getParent(), "client-key", ".tmp"); Path certFile = Files.createTempFile(certificate.getParent(), "client-cert", ".tmp"); try { createSelfSignedCertificate(new Date(), keyFile, certFile); } catch (CertificateException | NoSuchAlgorithmException | OperatorCreationException e) { throw new TLSEnvironmentException("Could not generate certificate: " + e.getMessage(), e); } Files.move(keyFile, key, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); Files.move(certFile, certificate, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); return true; } private  TLS(); }
TLS { public static boolean createSelfSignedCertificateIfMissing(Path key, Path certificate) throws IOException { if (Files.exists(certificate) || Files.exists(key)) { return false; } createDirectories(certificate.getParent()); createDirectories(key.getParent()); Path keyFile = Files.createTempFile(key.getParent(), "client-key", ".tmp"); Path certFile = Files.createTempFile(certificate.getParent(), "client-cert", ".tmp"); try { createSelfSignedCertificate(new Date(), keyFile, certFile); } catch (CertificateException | NoSuchAlgorithmException | OperatorCreationException e) { throw new TLSEnvironmentException("Could not generate certificate: " + e.getMessage(), e); } Files.move(keyFile, key, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); Files.move(certFile, certificate, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); return true; } private  TLS(); static boolean createSelfSignedCertificateIfMissing(Path key, Path certificate); static byte[] readPemFile(Path certificate); static byte[] certificateFingerprint(Path certificate); static String certificateHexFingerprint(Path certificate); static byte[] certificateFingerprint(Certificate certificate); static String certificateHexFingerprint(Certificate certificate); }
TLS { public static boolean createSelfSignedCertificateIfMissing(Path key, Path certificate) throws IOException { if (Files.exists(certificate) || Files.exists(key)) { return false; } createDirectories(certificate.getParent()); createDirectories(key.getParent()); Path keyFile = Files.createTempFile(key.getParent(), "client-key", ".tmp"); Path certFile = Files.createTempFile(certificate.getParent(), "client-cert", ".tmp"); try { createSelfSignedCertificate(new Date(), keyFile, certFile); } catch (CertificateException | NoSuchAlgorithmException | OperatorCreationException e) { throw new TLSEnvironmentException("Could not generate certificate: " + e.getMessage(), e); } Files.move(keyFile, key, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); Files.move(certFile, certificate, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); return true; } private  TLS(); static boolean createSelfSignedCertificateIfMissing(Path key, Path certificate); static byte[] readPemFile(Path certificate); static byte[] certificateFingerprint(Path certificate); static String certificateHexFingerprint(Path certificate); static byte[] certificateFingerprint(Certificate certificate); static String certificateHexFingerprint(Certificate certificate);  }