Search in sources :

Example 11 with SSLServerSocketFactory

use of javax.net.ssl.SSLServerSocketFactory in project opennms by OpenNMS.

the class SSLServer method init.

/**
 * <p>init</p>
 *
 * @throws java.lang.Exception if any.
 */
@Override
public void init() throws Exception {
    super.init();
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(getKeyManagerAlgorithm(), getKeyManagerProvider());
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    char[] password = getPassword().toCharArray();
    java.io.FileInputStream fis = null;
    try {
        fis = new java.io.FileInputStream(getPathToKeyStore());
        ks.load(fis, password);
    } finally {
        if (fis != null) {
            fis.close();
        }
    }
    kmf.init(ks, password);
    KeyManager[] km = kmf.getKeyManagers();
    SSLContext sslContext = SSLContext.getInstance(getSslContextProtocol());
    sslContext.init(km, null, new SecureRandom());
    SSLServerSocketFactory serverFactory = sslContext.getServerSocketFactory();
    setServerSocket(serverFactory.createServerSocket(getPort()));
    onInit();
}
Also used : SecureRandom(java.security.SecureRandom) SSLServerSocketFactory(javax.net.ssl.SSLServerSocketFactory) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) KeyManager(javax.net.ssl.KeyManager) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 12 with SSLServerSocketFactory

use of javax.net.ssl.SSLServerSocketFactory 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)

Example 13 with SSLServerSocketFactory

use of javax.net.ssl.SSLServerSocketFactory in project jetty.project by eclipse.

the class SslContextFactory method newSslServerSocket.

public SSLServerSocket newSslServerSocket(String host, int port, int backlog) throws IOException {
    checkIsStarted();
    SSLContext context = getSslContext();
    SSLServerSocketFactory factory = context.getServerSocketFactory();
    SSLServerSocket socket = (SSLServerSocket) (host == null ? factory.createServerSocket(port, backlog) : factory.createServerSocket(port, backlog, InetAddress.getByName(host)));
    socket.setSSLParameters(customize(socket.getSSLParameters()));
    return socket;
}
Also used : SSLServerSocketFactory(javax.net.ssl.SSLServerSocketFactory) SSLContext(javax.net.ssl.SSLContext) SSLServerSocket(javax.net.ssl.SSLServerSocket)

Example 14 with SSLServerSocketFactory

use of javax.net.ssl.SSLServerSocketFactory in project camel by apache.

the class HttpTestServer method start.

/**
     * Starts this test server.
     */
public void start() throws Exception {
    if (servicedSocket != null) {
        throw new IllegalStateException(this.toString() + " already running");
    }
    ServerSocket ssock;
    if (sslcontext != null) {
        SSLServerSocketFactory sf = sslcontext.getServerSocketFactory();
        ssock = sf.createServerSocket();
    } else {
        ssock = new ServerSocket();
    }
    // probably pointless for port '0'
    ssock.setReuseAddress(true);
    ssock.bind(TEST_SERVER_ADDR);
    servicedSocket = ssock;
    listenerThread = new ListenerThread();
    listenerThread.setDaemon(false);
    listenerThread.start();
}
Also used : SSLServerSocketFactory(javax.net.ssl.SSLServerSocketFactory) ServerSocket(java.net.ServerSocket)

Example 15 with SSLServerSocketFactory

use of javax.net.ssl.SSLServerSocketFactory in project android by cSploit.

the class HTTPSRedirector method getSSLSocket.

private SSLServerSocket getSSLSocket() throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException, UnrecoverableKeyException, KeyManagementException {
    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    keyStore.load(mContext.getAssets().open(KEYSTORE_FILE), KEYSTORE_PASS.toCharArray());
    KeyManagerFactory keyMan = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    keyMan.init(keyStore, KEYSTORE_PASS.toCharArray());
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(keyMan.getKeyManagers(), null, null);
    SSLServerSocketFactory sslFactory = sslContext.getServerSocketFactory();
    return (SSLServerSocket) sslFactory.createServerSocket(mPort, BACKLOG, mAddress);
}
Also used : SSLServerSocketFactory(javax.net.ssl.SSLServerSocketFactory) SSLContext(javax.net.ssl.SSLContext) SSLServerSocket(javax.net.ssl.SSLServerSocket) KeyStore(java.security.KeyStore) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Aggregations

SSLServerSocketFactory (javax.net.ssl.SSLServerSocketFactory)22 SSLServerSocket (javax.net.ssl.SSLServerSocket)13 SSLContext (javax.net.ssl.SSLContext)10 IOException (java.io.IOException)7 ServerSocket (java.net.ServerSocket)4 KeyStore (java.security.KeyStore)4 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)4 SSLSocket (javax.net.ssl.SSLSocket)3 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)3 InetAddress (java.net.InetAddress)2 SSLSessionContext (javax.net.ssl.SSLSessionContext)2 ObjectID (iaik.asn1.ObjectID)1 AlgorithmID (iaik.asn1.structures.AlgorithmID)1 Name (iaik.asn1.structures.Name)1 X509Certificate (iaik.x509.X509Certificate)1 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1