4
4
import org .junit .Assert ;
5
5
import org .junit .Before ;
6
6
import org .junit .Test ;
7
+
7
8
import sun .security .x509 .AlgorithmId ;
8
9
import sun .security .x509 .CertificateAlgorithmId ;
9
10
import sun .security .x509 .CertificateSerialNumber ;
10
11
import sun .security .x509 .CertificateValidity ;
11
12
import sun .security .x509 .CertificateVersion ;
12
13
import sun .security .x509 .CertificateX509Key ;
14
+ import sun .security .x509 .SubjectAlternativeNameExtension ;
13
15
import sun .security .x509 .X500Name ;
14
16
import sun .security .x509 .X509CertImpl ;
15
17
import sun .security .x509 .X509CertInfo ;
18
+ import sun .security .x509 .CertificateExtensions ;
19
+ import sun .security .x509 .GeneralNames ;
20
+ import sun .security .x509 .GeneralName ;
21
+ import sun .security .x509 .GeneralNameInterface ;
22
+ import sun .security .x509 .DNSName ;
23
+ import sun .security .x509 .IPAddressName ;
24
+ import sun .security .util .DerOutputStream ;
16
25
17
26
import javax .crypto .KeyGenerator ;
18
27
import javax .crypto .SecretKey ;
@@ -188,6 +197,23 @@ private X509Certificate generateSelfSignedCertificate(KeyPair keyPair) throws Ce
188
197
Date validTo = new Date (validFrom .getTime () + 50L * 365L * 24L * 60L * 60L * 1000L ); //50 years
189
198
CertificateValidity validity = new CertificateValidity (validFrom , validTo );
190
199
certInfo .set (X509CertInfo .VALIDITY , validity );
200
+
201
+ GeneralNameInterface dnsName = new DNSName ("baeldung.com" );
202
+ DerOutputStream dnsNameOutputStream = new DerOutputStream ();
203
+ dnsName .encode (dnsNameOutputStream );
204
+
205
+ GeneralNameInterface ipAddress = new IPAddressName ("127.0.0.1" );
206
+ DerOutputStream ipAddressOutputStream = new DerOutputStream ();
207
+ ipAddress .encode (ipAddressOutputStream );
208
+
209
+ GeneralNames generalNames = new GeneralNames ();
210
+ generalNames .add (new GeneralName (dnsName ));
211
+ generalNames .add (new GeneralName (ipAddress ));
212
+
213
+ CertificateExtensions ext = new CertificateExtensions ();
214
+ ext .set (SubjectAlternativeNameExtension .NAME , new SubjectAlternativeNameExtension (generalNames ));
215
+
216
+ certInfo .set (X509CertInfo .EXTENSIONS , ext );
191
217
192
218
// Create certificate and sign it
193
219
X509CertImpl cert = new X509CertImpl (certInfo );
@@ -202,4 +228,5 @@ private X509Certificate generateSelfSignedCertificate(KeyPair keyPair) throws Ce
202
228
203
229
return newCert ;
204
230
}
231
+
205
232
}
0 commit comments