16
16
*/
17
17
package net .sberg .openkim .gateway ;
18
18
19
- import io .netty .handler .ssl .util .SelfSignedCertificate ;
20
- import net .sberg .openkim .common .FileUtils ;
21
19
import net .sberg .openkim .common .ICommonConstants ;
20
+ import org .bouncycastle .asn1 .oiw .OIWObjectIdentifiers ;
21
+ import org .bouncycastle .asn1 .x500 .X500Name ;
22
+ import org .bouncycastle .asn1 .x509 .*;
23
+ import org .bouncycastle .cert .X509ExtensionUtils ;
24
+ import org .bouncycastle .cert .X509v3CertificateBuilder ;
25
+ import org .bouncycastle .cert .jcajce .JcaX509CertificateConverter ;
26
+ import org .bouncycastle .cert .jcajce .JcaX509v3CertificateBuilder ;
27
+ import org .bouncycastle .jce .provider .BouncyCastleProvider ;
28
+ import org .bouncycastle .operator .ContentSigner ;
29
+ import org .bouncycastle .operator .DigestCalculator ;
30
+ import org .bouncycastle .operator .OperatorCreationException ;
31
+ import org .bouncycastle .operator .bc .BcDigestCalculatorProvider ;
32
+ import org .bouncycastle .operator .jcajce .JcaContentSignerBuilder ;
22
33
import org .slf4j .Logger ;
23
34
import org .slf4j .LoggerFactory ;
24
35
import org .springframework .beans .factory .annotation .Value ;
25
36
import org .springframework .stereotype .Service ;
26
37
27
38
import java .io .ByteArrayInputStream ;
28
39
import java .io .File ;
29
- import java .io .FileInputStream ;
30
40
import java .io .FileOutputStream ;
31
- import java .security .KeyFactory ;
32
- import java .security .KeyStore ;
33
- import java .security .PrivateKey ;
34
- import java .security .SecureRandom ;
41
+ import java .math .BigInteger ;
42
+ import java .security .*;
35
43
import java .security .cert .Certificate ;
36
- import java .security .cert .CertificateFactory ;
37
44
import java .security .cert .X509Certificate ;
45
+ import java .security .spec .ECGenParameterSpec ;
38
46
import java .security .spec .PKCS8EncodedKeySpec ;
47
+ import java .time .Duration ;
48
+ import java .time .Instant ;
39
49
import java .util .Base64 ;
50
+ import java .util .Date ;
40
51
41
52
@ Service
42
53
public class GatewayKeystoreService {
@@ -57,35 +68,59 @@ public class GatewayKeystoreService {
57
68
@ Value ("${gatewaykeystore.password}" )
58
69
private String password ;
59
70
60
- public void createSelfSigned () throws Exception {
61
- if (log .isInfoEnabled ()) {
62
- log .info ("***generateSelfSigned keys and cert - BEGIN***" );
63
- }
64
-
65
- SelfSignedCertificate selfSignedCertificate = new SelfSignedCertificate ("eldix4kim.sberg.net" , SecureRandom .getInstance ("NativePRNG" ), keysize );
71
+ private SubjectKeyIdentifier createSubjectKeyId (final PublicKey publicKey ) throws OperatorCreationException {
72
+ final SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo .getInstance (publicKey .getEncoded ());
73
+ final DigestCalculator digCalc =
74
+ new BcDigestCalculatorProvider ().get (new AlgorithmIdentifier (OIWObjectIdentifiers .idSHA1 ));
66
75
67
- CertificateFactory fact = CertificateFactory .getInstance ("X.509" );
68
- FileInputStream is = new FileInputStream (selfSignedCertificate .certificate ());
69
- X509Certificate cer = (X509Certificate ) fact .generateCertificate (is );
70
- is .close ();
76
+ return new X509ExtensionUtils (digCalc ).createSubjectKeyIdentifier (publicKeyInfo );
77
+ }
71
78
72
- Certificate [] chain = new Certificate []{cer };
79
+ private AuthorityKeyIdentifier createAuthorityKeyId (final PublicKey publicKey )
80
+ throws OperatorCreationException
81
+ {
82
+ final SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo .getInstance (publicKey .getEncoded ());
83
+ final DigestCalculator digCalc =
84
+ new BcDigestCalculatorProvider ().get (new AlgorithmIdentifier (OIWObjectIdentifiers .idSHA1 ));
73
85
74
- String privKeyContent = FileUtils .readFileContent (selfSignedCertificate .privateKey ().getAbsolutePath ());
75
- String privateKeyPEM = privKeyContent
76
- .replace (BEGIN_KEY , "" )
77
- .replaceAll (System .lineSeparator (), "" )
78
- .replace (END_KEY , "" );
86
+ return new X509ExtensionUtils (digCalc ).createAuthorityKeyIdentifier (publicKeyInfo );
87
+ }
79
88
80
- byte [] encoded = Base64 .getDecoder ().decode (privateKeyPEM .getBytes ("UTF-8" ));
89
+ public void createSelfSigned () throws Exception {
90
+ if (log .isInfoEnabled ()) {
91
+ log .info ("***generateSelfSigned keys and cert - BEGIN***" );
92
+ }
81
93
82
- PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec (encoded );
83
- KeyFactory kf = KeyFactory .getInstance ("RSA" );
84
- PrivateKey privKey = kf .generatePrivate (spec );
94
+ KeyPairGenerator kg = KeyPairGenerator .getInstance ("EC" , "BC" );
95
+ ECGenParameterSpec kpgparams = new ECGenParameterSpec ("brainpoolP256r1" );
96
+ kg .initialize (kpgparams );
97
+
98
+ KeyPair keyPair = kg .generateKeyPair ();
99
+
100
+ final Instant now = Instant .now ();
101
+ final Date notBefore = Date .from (now );
102
+ final Date notAfter = Date .from (now .plus (Duration .ofDays (365 )));
103
+ final ContentSigner contentSigner = new JcaContentSignerBuilder ("SHA256withECDSA" ).build (keyPair .getPrivate ());
104
+ final X500Name x500Name = new X500Name ("CN=" + "eldix4kim.sberg.net" );
105
+
106
+ final X509v3CertificateBuilder certificateBuilder =
107
+ new JcaX509v3CertificateBuilder (x500Name ,
108
+ BigInteger .valueOf (now .toEpochMilli ()),
109
+ notBefore ,
110
+ notAfter ,
111
+ x500Name ,
112
+ keyPair .getPublic ())
113
+ .addExtension (Extension .subjectKeyIdentifier , false , createSubjectKeyId (keyPair .getPublic ()))
114
+ .addExtension (Extension .authorityKeyIdentifier , false , createAuthorityKeyId (keyPair .getPublic ()))
115
+ .addExtension (Extension .basicConstraints , true , new BasicConstraints (true ));
116
+
117
+ X509Certificate x509Certificate = new JcaX509CertificateConverter ()
118
+ .setProvider (new BouncyCastleProvider ()).getCertificate (certificateBuilder .build (contentSigner ));
119
+ Certificate [] chain = new Certificate []{x509Certificate };
85
120
86
121
KeyStore keyStore = KeyStore .getInstance ("PKCS12" , "BC" );
87
122
keyStore .load (null , null );
88
- keyStore .setKeyEntry (ICommonConstants .OPENKIM_SERVER_KEYSTORE_ALIAS , privKey , password .toCharArray (), chain );
123
+ keyStore .setKeyEntry (ICommonConstants .OPENKIM_SERVER_KEYSTORE_ALIAS , keyPair . getPrivate () , password .toCharArray (), chain );
89
124
keyStore .store (
90
125
new FileOutputStream (new File (ICommonConstants .BASE_DIR + ICommonConstants .OPENKIM_SERVER_KEYSTORE_FILENAME )),
91
126
password .toCharArray ()
0 commit comments