Search in sources :

Example 16 with SubjectAlternativeNameExtension

use of org.mozilla.jss.netscape.security.x509.SubjectAlternativeNameExtension in project scout.rt by eclipse-scout.

the class SunSecurityProvider method createSelfSignedCertificate.

@Override
public void createSelfSignedCertificate(String certificateAlias, String x500Name, String storePass, String keyPass, int keyBits, int validDays, OutputStream out) {
    try {
        sun.security.tools.keytool.CertAndKeyGen certGen = new sun.security.tools.keytool.CertAndKeyGen("RSA", "SHA256WithRSA", null);
        certGen.generate(keyBits);
        sun.security.x509.X500Name name = new sun.security.x509.X500Name(x500Name);
        long validSecs = (long) validDays * 24L * 3600L;
        GeneralNames generalNames = new GeneralNames().add(new GeneralName(new DNSName("localhost"))).add(new GeneralName(new IPAddressName("127.0.0.1")));
        if (!StringUtility.isNullOrEmpty(name.getCommonName())) {
            generalNames.add(new GeneralName(new DNSName(name.getCommonName())));
        }
        CertificateExtensions extensions = new CertificateExtensions();
        extensions.set(SubjectAlternativeNameExtension.NAME, new SubjectAlternativeNameExtension(generalNames));
        X509Certificate cert = certGen.getSelfCertificate(name, new Date(), validSecs, extensions);
        PrivateKey privateKey = certGen.getPrivateKey();
        KeyStore ks = KeyStore.getInstance("jks");
        ks.load(null, storePass.toCharArray());
        ks.setKeyEntry(certificateAlias, privateKey, keyPass.toCharArray(), new X509Certificate[] { cert });
        ks.store(out, storePass.toCharArray());
    } catch (GeneralSecurityException e) {
        throw new ProcessingException("Security issue", e);
    } catch (IOException e) {
        throw new ProcessingException("IO issue", e);
    }
}
Also used : PrivateKey(java.security.PrivateKey) SubjectAlternativeNameExtension(sun.security.x509.SubjectAlternativeNameExtension) GeneralSecurityException(java.security.GeneralSecurityException) CertificateExtensions(sun.security.x509.CertificateExtensions) IOException(java.io.IOException) DNSName(sun.security.x509.DNSName) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) Date(java.util.Date) GeneralNames(sun.security.x509.GeneralNames) IPAddressName(sun.security.x509.IPAddressName) GeneralName(sun.security.x509.GeneralName) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Aggregations

SubjectAlternativeNameExtension (sun.security.x509.SubjectAlternativeNameExtension)14 GeneralNames (sun.security.x509.GeneralNames)13 IOException (java.io.IOException)10 GeneralName (sun.security.x509.GeneralName)10 X509CertImpl (sun.security.x509.X509CertImpl)10 CertificateExtensions (sun.security.x509.CertificateExtensions)7 X500Name (sun.security.x509.X500Name)6 DNSName (sun.security.x509.DNSName)4 X509CertInfo (sun.security.x509.X509CertInfo)4 CertPathValidatorException (java.security.cert.CertPathValidatorException)3 X500Principal (javax.security.auth.x500.X500Principal)3 GeneralNameInterface (sun.security.x509.GeneralNameInterface)3 IPAddressName (sun.security.x509.IPAddressName)3 BigInteger (java.math.BigInteger)2 PrivateKey (java.security.PrivateKey)2 CertificateException (java.security.cert.CertificateException)2 X509Certificate (java.security.cert.X509Certificate)2 Date (java.util.Date)2 Iterator (java.util.Iterator)2 GeneralName (org.mozilla.jss.netscape.security.x509.GeneralName)2