Examples of SSLContext


Examples of javax.net.ssl.SSLContext

        //TODO for performance reasons we should cache the KeymanagerFactory and TrustManagerFactory
        if ((keyStorePassword != null) && (keyPassword != null) && (!keyStorePassword.equals(keyPassword))) {
            LogUtils.log(LOG, Level.WARNING, "KEY_PASSWORD_NOT_SAME_KEYSTORE_PASSWORD");
        }
        try {
            SSLContext sslctx = SSLContext.getInstance(secureSocketProtocol);
            KeyManagerFactory kmf =
                KeyManagerFactory.getInstance(keystoreKeyManagerFactoryAlgorithm)
            KeyStore ks = KeyStore.getInstance(keyStoreType);
            KeyManager[] keystoreManagers = null;
           
           
            byte[] sslCert = loadClientCredential(keyStoreLocation);
           
            if (sslCert != null && sslCert.length > 0 && keyStorePassword != null) {
                ByteArrayInputStream bin = new ByteArrayInputStream(sslCert);
                try {
                    ks.load(bin, keyStorePassword.toCharArray());
                    kmf.init(ks, keyStorePassword.toCharArray());
                    keystoreManagers = kmf.getKeyManagers();
                    LogUtils.log(LOG, Level.INFO, "LOADED_KEYSTORE", new Object[]{keyStoreLocation});
                } catch (Exception e) {
                    LogUtils.log(LOG, Level.WARNING, "FAILED_TO_LOAD_KEYSTORE",
                                                     new Object[]{keyStoreLocation, e.getMessage()});
                }
            } 
            if ((keyStorePassword == null) && (keyStoreLocation != null)) {
                LogUtils.log(LOG, Level.WARNING, "FAILED_TO_LOAD_KEYSTORE_NULL_PASSWORD",
                             new Object[]{keyStoreLocation});
            }
           
            // ************************* Load Trusted CA file *************************
            //TODO could support multiple trust cas
            TrustManager[] trustStoreManagers = new TrustManager[1];
            
            KeyStore trustedCertStore = KeyStore.getInstance(trustStoreType);
            trustedCertStore.load(null, "".toCharArray());
            CertificateFactory cf = CertificateFactory.getInstance(CERTIFICATE_FACTORY_TYPE);
            byte[] caCert = loadCACert(trustStoreLocation);
            try {
                if (caCert != null) {
                    ByteArrayInputStream cabin = new ByteArrayInputStream(caCert);
                    X509Certificate cert = (X509Certificate)cf.generateCertificate(cabin);
                    trustedCertStore.setCertificateEntry(cert.getIssuerDN().toString(), cert);
                    cabin.close();
                }
            } catch (Exception e) {
                LogUtils.log(LOG, Level.WARNING, "FAILED_TO_LOAD_TRUST_STORE",
                             new Object[]{trustStoreLocation, e.getMessage()});
            }
            TrustManagerFactory tmf  =
                TrustManagerFactory.getInstance(trustStoreKeyManagerFactoryAlgorithm);

            tmf.init(trustedCertStore);
            LogUtils.log(LOG, Level.INFO, "LOADED_TRUST_STORE", new Object[]{trustStoreLocation});
           
            trustStoreManagers = tmf.getTrustManagers();

            sslctx.init(keystoreManagers, trustStoreManagers, null)
            httpsConnection.setSSLSocketFactory(new SSLSocketFactoryWrapper(sslctx.getSocketFactory(),
                                                                            cipherSuites));
           
        } catch (Exception e) {
            LogUtils.log(LOG, Level.SEVERE, "SSL_CONTEXT_INIT_FAILURE", new Object[]{e.getMessage()});
            return false;
View Full Code Here

Examples of javax.net.ssl.SSLContext

      kmf.init(ks, passphrase);
     
      TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
      tmf.init(ks);
 
      SSLContext sslContext = SSLContext.getInstance("TLS");
      sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

      return sslContext;

    } catch (Exception e) {
      return null;
View Full Code Here

Examples of javax.net.ssl.SSLContext

     * Gets a server socket - this gets as SSL socket instead of the standard
     * socket returned in the base class.
     */
    protected ServerSocket getServerSocket() throws IOException {
        // Just to make sure it's set before we start
        SSLContext context = getSSLContext(this.keystore, this.password);
        SSLServerSocketFactory factory = context.getServerSocketFactory();
        SSLServerSocket ss = (SSLServerSocket) (this.listenAddress == null ? factory
                .createServerSocket(this.listenPort, BACKLOG_COUNT)
                : factory.createServerSocket(this.listenPort, BACKLOG_COUNT,
                        InetAddress.getByName(this.listenAddress)));
        ss.setEnableSessionCreation(true);
View Full Code Here

Examples of javax.net.ssl.SSLContext

                Logger.log(Logger.FULL_DEBUG, SSL_RESOURCES,
                        "HttpsListener.KeyFound", new String[] { alias,
                                ks.getCertificate(alias) + "" });
            }

            SSLContext context = SSLContext.getInstance("SSL");
            context.init(kmf.getKeyManagers(), null, null);
            Arrays.fill(passwordChars, 'x');
            return context;
        } catch (IOException err) {
            throw err;
        } catch (Throwable err) {
View Full Code Here

Examples of javax.net.ssl.SSLContext

   *
   * @return The SSLContext
   */
  private static SSLContext createEasySSLContext() {
    try {
      SSLContext context = SSLContext.getInstance("SSL"); //$NON-NLS-1$
      context.init(null, new TrustManager[] { new EasyX509TrustManager(null) }, null);
      return context;
    } catch (Exception e) {
      throw new HttpClientError(e.toString());
    }
  }
View Full Code Here

Examples of javax.net.ssl.SSLContext

        super();
    }

    private static SSLContext createEasySSLContext() {
        try {
            SSLContext context = SSLContext.getInstance("SSL");
            context.init(
              null,
              new TrustManager[] {new EasyX509TrustManager(null)},
              null);
            return context;
        } catch (Exception e) {
View Full Code Here

Examples of javax.net.ssl.SSLContext

    * SecurityDomain.
    */
   static SSLContext forDomain(SecurityDomain securityDomain)
      throws IOException
   {
      SSLContext sslCtx = null;
      try
      {
         sslCtx = SSLContext.getInstance("TLS");
         KeyManagerFactory keyMgr = securityDomain.getKeyManagerFactory();
         if( keyMgr == null )
            throw new IOException("KeyManagerFactory is null for security domain: "+securityDomain.getSecurityDomain());
         TrustManagerFactory trustMgr = securityDomain.getTrustManagerFactory();
         TrustManager[] trustMgrs = null;
         if( trustMgr != null )
            trustMgrs = trustMgr.getTrustManagers();
         sslCtx.init(keyMgr.getKeyManagers(), trustMgrs, null);
         return sslCtx;
      }
      catch(NoSuchAlgorithmException e)
      {
         log.error("Failed to get SSLContext for TLS algorithm", e);
View Full Code Here

Examples of javax.net.ssl.SSLContext

      // Setup the test TrustManagerFactory.
      TrustManagerFactory trustMgr = TrustManagerFactory
          .getInstance(TrustManagerFactory.getDefaultAlgorithm());
      trustMgr.init(keyStore);
      // Setup the test SSLSocketFactory.
      SSLContext sslCtx = SSLContext.getInstance("TLS");
      sslCtx.init(null, trustMgr.getTrustManagers(), null);
      ((HttpsURLConnection) conn).setSSLSocketFactory(sslCtx.getSocketFactory());     
    }
   
    // Connect to the remote HTTP server.
    log.debug("Connecting to URL: " + url);
    byte[] buffer = new byte[1024];
View Full Code Here

Examples of javax.net.ssl.SSLContext

   }

   private String initServer() throws Exception
   {
      String httpsURL = null;
      SSLContext sslCtx = null;
      try
      {
         sslCtx = SSLContext.getInstance("TLS");
         ClassLoader loader = getClass().getClassLoader();
         URL keyStoreURL = loader.getResource("tst.keystore");
         if( keyStoreURL == null )
            throw new IOException("Failed to find resource tst.keystore");
         log.debug("Opening KeyStore: "+keyStoreURL);
         KeyStore keyStore = KeyStore.getInstance("JKS");
         InputStream is = keyStoreURL.openStream();
         keyStore.load(is, KEYSTORE_PASSWORD.toCharArray());
         String algorithm = KeyManagerFactory.getDefaultAlgorithm();
         KeyManagerFactory keyMgr = KeyManagerFactory.getInstance(algorithm);
         keyMgr.init(keyStore, KEYSTORE_PASSWORD.toCharArray());
         algorithm = TrustManagerFactory.getDefaultAlgorithm();
         TrustManagerFactory trustMgr = TrustManagerFactory.getInstance(algorithm);
         trustMgr.init(keyStore);
         TrustManager[] trustMgrs = trustMgr.getTrustManagers();
         sslCtx.init(keyMgr.getKeyManagers(), trustMgrs, null);
      }
      catch(Exception e)
      {
         log.error("Failed to init SSLContext", e);
         throw new IOException("Failed to get SSLContext for TLS algorithm");
      }

      ServerSocketFactory factory = sslCtx.getServerSocketFactory();
      ServerSocket serverSocket = factory.createServerSocket(0);
      getLog().debug("Created serverSocket: "+serverSocket);
      int port = serverSocket.getLocalPort();
      InetAddress addr = serverSocket.getInetAddress();
      httpsURL = "https://localhost:" + port + '/';
View Full Code Here

Examples of javax.net.ssl.SSLContext

                       
                        public void checkServerTrusted(final X509Certificate[] certs, final String authType) { /* NOP */ }
                      }
                    };
                                         
    SSLContext sslContext = SSLContext.getInstance("SSL");
    sslContext.init(null, trustAllCerts, null);
    return sslContext;
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.