Search in sources :

Example 1 with AlgorithmID

use of iaik.asn1.structures.AlgorithmID in project apjp by jvansteirteghem.

the class HTTPS method createSSLServerSocket.

public static synchronized SSLServerSocket createSSLServerSocket(String remoteAddress, int remotePort) throws HTTPSException {
    try {
        KeyStore defaultKeyStore = getDefaultKeyStore();
        PrivateKey privateKey = (PrivateKey) defaultKeyStore.getKey("APJP", "APJP".toCharArray());
        Certificate certificateAuthority = defaultKeyStore.getCertificate("APJP");
        String certificateAlias;
        if (remotePort == 443) {
            certificateAlias = remoteAddress;
        } else {
            certificateAlias = remoteAddress + ":" + remotePort;
        }
        Certificate certificate = defaultKeyStore.getCertificate(certificateAlias);
        if (certificate == null) {
            X509Certificate x509CertificateAuthority = new X509Certificate(certificateAuthority.getEncoded());
            X509Certificate x509Certificate = new X509Certificate();
            Name name = new Name();
            //CN
            name.addRDN(new ObjectID("2.5.4.3"), certificateAlias);
            // O
            name.addRDN(new ObjectID("2.5.4.10"), "APJP");
            // OU
            name.addRDN(new ObjectID("2.5.4.11"), "APJP");
            x509Certificate.setSubjectDN(name);
            x509Certificate.setIssuerDN(x509CertificateAuthority.getIssuerDN());
            x509Certificate.setValidNotBefore(new Date(new Date().getTime() - 1 * (1000L * 60 * 60 * 24 * 365)));
            x509Certificate.setValidNotAfter(new Date(new Date().getTime() + 10 * (1000L * 60 * 60 * 24 * 365)));
            x509Certificate.setSerialNumber(BigInteger.valueOf(new Date().getTime()));
            x509Certificate.setPublicKey(x509CertificateAuthority.getPublicKey());
            // SHA1_WITH_RSA_ENCRYPTION
            x509Certificate.sign(new AlgorithmID(new ObjectID("1.2.840.113549.1.1.5")), privateKey);
            X509Certificate[] x509CertificateArray = new X509Certificate[2];
            x509CertificateArray[0] = x509Certificate;
            x509CertificateArray[1] = x509CertificateAuthority;
            defaultKeyStore.setCertificateEntry(certificateAlias, x509Certificate);
            defaultKeyStore.setKeyEntry(certificateAlias, privateKey, "APJP".toCharArray(), x509CertificateArray);
            certificate = x509Certificate;
        }
        Certificate[] certificateArray = new Certificate[2];
        certificateArray[0] = certificate;
        certificateArray[1] = certificateAuthority;
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(null, "APJP".toCharArray());
        keyStore.setCertificateEntry("APJP", certificate);
        keyStore.setKeyEntry("APJP", privateKey, "APJP".toCharArray(), certificateArray);
        SSLContext sslContext = SSLContext.getInstance("TLS");
        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore, "APJP".toCharArray());
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(keyStore);
        sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
        SSLServerSocketFactory sslServerSocketFactory = (SSLServerSocketFactory) sslContext.getServerSocketFactory();
        return (SSLServerSocket) sslServerSocketFactory.createServerSocket();
    } catch (Exception e) {
        logger.log(2, "HTTPS/CREATE_SSL_SERVER_SOCKET: EXCEPTION", e);
        throw new HTTPSException("HTTPS/CREATE_SSL_SERVER_SOCKET", e);
    }
}
Also used : PrivateKey(java.security.PrivateKey) ObjectID(iaik.asn1.ObjectID) SSLContext(javax.net.ssl.SSLContext) SSLServerSocket(javax.net.ssl.SSLServerSocket) KeyStore(java.security.KeyStore) X509Certificate(iaik.x509.X509Certificate) Date(java.util.Date) Name(iaik.asn1.structures.Name) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) AlgorithmID(iaik.asn1.structures.AlgorithmID) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLServerSocketFactory(javax.net.ssl.SSLServerSocketFactory) Certificate(java.security.cert.Certificate) X509Certificate(iaik.x509.X509Certificate)

Aggregations

ObjectID (iaik.asn1.ObjectID)1 AlgorithmID (iaik.asn1.structures.AlgorithmID)1 Name (iaik.asn1.structures.Name)1 X509Certificate (iaik.x509.X509Certificate)1 KeyStore (java.security.KeyStore)1 PrivateKey (java.security.PrivateKey)1 Certificate (java.security.cert.Certificate)1 Date (java.util.Date)1 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)1 SSLContext (javax.net.ssl.SSLContext)1 SSLServerSocket (javax.net.ssl.SSLServerSocket)1 SSLServerSocketFactory (javax.net.ssl.SSLServerSocketFactory)1 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)1