5353import de .hsheilbronn .mi .utils .crypto .io .PemWriter ;
5454import de .hsheilbronn .mi .utils .crypto .keypair .KeyPairValidator ;
5555
56- public class CertificateGenerator
56+ public class CertificateGenerator extends AbstractGenerator
5757{
5858 private static final Logger logger = LoggerFactory .getLogger (CertificateGenerator .class );
5959
@@ -109,7 +109,6 @@ public CertificateAndPrivateKey sign(CertificateAuthority ca)
109109 }
110110 }
111111
112- public static final String POSTFIX_PRIVATE_KEY = ".key" ;
113112 public static final String POSTFIX_CERTIFICATE = ".crt" ;
114113
115114 private static final String SUBJECT_C = "DE" ;
@@ -121,8 +120,6 @@ public CertificateAndPrivateKey sign(CertificateAuthority ca)
121120 private static final CertificationRequestConfig CERTIFICATION_REQUEST_ISSUING_CA = new CertificationRequestConfig (
122121 CertificateAuthority ::signClientServerIssuingCaCertificate , SUBJECT_CN_ISSUING_CA , null );
123122
124- private final Path certDir ;
125- private final char [] privateKeyPassword ;
126123 private final List <CertificationRequestConfig > certificationRequestConfigs = new ArrayList <>();
127124
128125 private CertificateAuthority rootCa ;
@@ -132,11 +129,7 @@ public CertificateAndPrivateKey sign(CertificateAuthority ca)
132129 public CertificateGenerator (Path certDir , char [] privateKeyPassword ,
133130 List <CertificationRequestConfig > certificationRequestConfigs )
134131 {
135- Objects .requireNonNull (certDir , "certDir" );
136- Objects .requireNonNull (privateKeyPassword , "privateKeyPassword" );
137-
138- this .certDir = certDir ;
139- this .privateKeyPassword = privateKeyPassword ;
132+ super (certDir , privateKeyPassword );
140133
141134 if (certificationRequestConfigs != null )
142135 this .certificationRequestConfigs .addAll (certificationRequestConfigs );
@@ -216,11 +209,6 @@ private String toHexThumbprint(X509Certificate certificate)
216209 }
217210 }
218211
219- private Path toPath (String commonName , String postFix )
220- {
221- return certDir .resolve (commonName .replaceAll (" " , "_" ) + postFix );
222- }
223-
224212 private Optional <X509Certificate > readCertificate (String commonName )
225213 {
226214 Path file = toPath (commonName , POSTFIX_CERTIFICATE );
@@ -241,26 +229,6 @@ private Optional<X509Certificate> readCertificate(String commonName)
241229 }
242230 }
243231
244- private Optional <PrivateKey > readPrivateKey (String commonName )
245- {
246- Path file = toPath (commonName , POSTFIX_PRIVATE_KEY );
247-
248- if (!Files .isReadable (file ))
249- return Optional .empty ();
250-
251- try
252- {
253- return Optional .of (PemReader .readPrivateKey (file , privateKeyPassword ));
254- }
255- catch (IOException e )
256- {
257- logger .error ("Unable to read private-key {}: {} - {}" , file .toAbsolutePath ().normalize (),
258- e .getClass ().getName (), e .getMessage ());
259-
260- throw new RuntimeException (e );
261- }
262- }
263-
264232 private Optional <CertificateAndPrivateKey > readCertificateAndPrivateKey (String commonName )
265233 {
266234 Optional <X509Certificate > crt = readCertificate (commonName );
@@ -333,21 +301,6 @@ private void writeCertificate(String commonName, X509Certificate crt)
333301 }
334302 }
335303
336- private void writePrivateKey (String commonName , PrivateKey privateKey )
337- {
338- Path file = toPath (commonName , POSTFIX_PRIVATE_KEY );
339-
340- try
341- {
342- PemWriter .writePrivateKey (privateKey ).asPkcs8 ().encryptedAes128 (privateKeyPassword ).toFile (file );
343- }
344- catch (IOException e )
345- {
346- logger .error ("Unable to write private-key {}: {} - {}" , file .toAbsolutePath ().normalize (),
347- e .getClass ().getName (), e .getMessage ());
348- throw new RuntimeException (e );
349- }
350- }
351304
352305 private void writeCertificateAndPrivateKey (String commonName , CertificateAndPrivateKey certificateAndPrivateKey )
353306 {
0 commit comments