List of usage examples for javax.net.ssl KeyManagerFactory getKeyManagers
public final KeyManager[] getKeyManagers()
From source file:org.apache.felix.karaf.jaas.config.impl.ResourceKeystoreInstance.java
public KeyManager[] getKeyManager(String algorithm, String keyAlias) throws KeystoreIsLocked, NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException { if (isKeystoreLocked()) { throw new KeystoreIsLocked("Keystore '" + name + "' is locked."); }/* ww w . ja v a 2 s . co m*/ if (!loadKeystoreData()) { return null; } KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(algorithm); keyFactory.init(keystore, (char[]) keyPasswords.get(keyAlias)); return keyFactory.getKeyManagers(); }
From source file:io.github.thefishlive.updater.HttpServer.java
public void run() { try {/*from w w w .j ava2 s. c om*/ int port = GitUpdater.port; // Set up the HTTP protocol processor HttpProcessor httpproc = HttpProcessorBuilder.create().add(new ResponseDate()) .add(new ResponseServer("GitUpdater/1.0-SNAPSHOT")).add(new ResponseContent()) .add(new ResponseConnControl()).build(); // Set up request handlers UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper(); reqistry.register("*", new ResponceHandler()); // Set up the HTTP service HttpService httpService = new HttpService(httpproc, reqistry); SSLServerSocketFactory sf = null; if (port == 8443) { // Initialize SSL context ClassLoader cl = getClass().getClassLoader(); URL url = cl.getResource("my.keystore"); if (url == null) { System.out.println("Keystore not found"); System.exit(1); } KeyStore keystore = KeyStore.getInstance("jks"); keystore.load(url.openStream(), "secret".toCharArray()); KeyManagerFactory kmfactory = KeyManagerFactory .getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmfactory.init(keystore, "secret".toCharArray()); KeyManager[] keymanagers = kmfactory.getKeyManagers(); SSLContext sslcontext = SSLContext.getInstance("TLS"); sslcontext.init(keymanagers, null, null); sf = sslcontext.getServerSocketFactory(); } try { Thread t = new RequestListenerThread(port, httpService, sf); t.setDaemon(false); t.start(); } catch (BindException ex) { System.out.println("Error binding to port " + port); System.out.println("Perhaps another server is running on that port"); return; } catch (IOException ex) { ex.printStackTrace(); } } catch (Exception ex) { ex.printStackTrace(); } }
From source file:org.apache.axis2.transport.nhttp.HttpCoreNIOSSLListener.java
/** * Create the SSLContext to be used by this listener * @param transportIn the Axis2 transport description * @return the SSLContext to be used//from w ww . j ava 2 s.c om */ protected SSLContext getSSLContext(TransportInDescription transportIn) throws AxisFault { KeyManager[] keymanagers = null; TrustManager[] trustManagers = null; Parameter keyParam = transportIn.getParameter("keystore"); Parameter trustParam = transportIn.getParameter("truststore"); if (keyParam != null) { OMElement ksEle = keyParam.getParameterElement().getFirstElement(); String location = ksEle.getFirstChildWithName(new QName("Location")).getText(); String type = ksEle.getFirstChildWithName(new QName("Type")).getText(); String storePassword = ksEle.getFirstChildWithName(new QName("Password")).getText(); String keyPassword = ksEle.getFirstChildWithName(new QName("KeyPassword")).getText(); try { KeyStore keyStore = KeyStore.getInstance(type); URL url = getClass().getClassLoader().getResource(location); log.debug("Loading Key Store from URL : " + url); keyStore.load(url.openStream(), storePassword.toCharArray()); KeyManagerFactory kmfactory = KeyManagerFactory .getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmfactory.init(keyStore, keyPassword.toCharArray()); keymanagers = kmfactory.getKeyManagers(); } catch (GeneralSecurityException gse) { log.error("Error loading Key store : " + location, gse); throw new AxisFault("Error loading Key store : " + location, gse); } catch (IOException ioe) { log.error("Error opening Key store : " + location, ioe); throw new AxisFault("Error opening Key store : " + location, ioe); } } if (trustParam != null) { OMElement tsEle = trustParam.getParameterElement().getFirstElement(); String location = tsEle.getFirstChildWithName(new QName("Location")).getText(); String type = tsEle.getFirstChildWithName(new QName("Type")).getText(); String storePassword = tsEle.getFirstChildWithName(new QName("Password")).getText(); try { KeyStore trustStore = KeyStore.getInstance(type); URL url = getClass().getClassLoader().getResource(location); log.debug("Loading Trust Key Store from URL : " + url); trustStore.load(url.openStream(), storePassword.toCharArray()); TrustManagerFactory trustManagerfactory = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerfactory.init(trustStore); trustManagers = trustManagerfactory.getTrustManagers(); } catch (GeneralSecurityException gse) { log.error("Error loading Key store : " + location, gse); throw new AxisFault("Error loading Key store : " + location, gse); } catch (IOException ioe) { log.error("Error opening Key store : " + location, ioe); throw new AxisFault("Error opening Key store : " + location, ioe); } } try { SSLContext sslcontext = SSLContext.getInstance("TLS"); sslcontext.init(keymanagers, trustManagers, null); return sslcontext; } catch (GeneralSecurityException gse) { log.error("Unable to create SSL context with the given configuration", gse); throw new AxisFault("Unable to create SSL context with the given configuration", gse); } }
From source file:org.eclipse.mylyn.internal.commons.net.PollingSslProtocolSocketFactory.java
public PollingSslProtocolSocketFactory() { KeyManager[] keymanagers = null; if (System.getProperty(KEY_STORE) != null && System.getProperty(KEY_STORE_PASSWORD) != null) { try {/*ww w .j av a 2 s . co m*/ String type = System.getProperty(KEY_STORE_TYPE, KeyStore.getDefaultType()); KeyStore keyStore = KeyStore.getInstance(type); char[] password = System.getProperty(KEY_STORE_PASSWORD).toCharArray(); keyStore.load(new FileInputStream(System.getProperty(KEY_STORE)), password); KeyManagerFactory keyManagerFactory = KeyManagerFactory .getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(keyStore, password); keymanagers = keyManagerFactory.getKeyManagers(); } catch (Exception e) { CommonsNetPlugin.log(IStatus.ERROR, "Could not initialize keystore", e); //$NON-NLS-1$ } } hasKeyManager = keymanagers != null; try { SSLContext sslContext = SSLContext.getInstance("SSL"); //$NON-NLS-1$ sslContext.init(keymanagers, new TrustManager[] { new TrustAllTrustManager() }, null); this.socketFactory = sslContext.getSocketFactory(); } catch (Exception e) { CommonsNetPlugin.log(IStatus.ERROR, "Could not initialize SSL context", e); //$NON-NLS-1$ } }
From source file:com.github.mrstampy.gameboot.otp.OtpTestConfiguration.java
private SSLContext createContext(KeyStore keystore, KeyManagerFactory kmf) throws Exception { TrustManagerFactory trustFactory = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustFactory.init(keystore);//from w ww . jav a 2 s. co m SSLContext sslContext = SSLContext.getInstance(PROTOCOL); sslContext.init(kmf == null ? null : kmf.getKeyManagers(), trustFactory.getTrustManagers(), null); return sslContext; }
From source file:org.apache.axis2.transport.nhttp.HttpCoreNIOSSLSender.java
protected SSLContext getSSLContext(TransportOutDescription transportOut) throws AxisFault { KeyManager[] keymanagers = null; TrustManager[] trustManagers = null; Parameter keyParam = transportOut.getParameter("keystore"); Parameter trustParam = transportOut.getParameter("truststore"); if (keyParam != null) { OMElement ksEle = keyParam.getParameterElement().getFirstElement(); String location = ksEle.getFirstChildWithName(new QName("Location")).getText(); String type = ksEle.getFirstChildWithName(new QName("Type")).getText(); String storePassword = ksEle.getFirstChildWithName(new QName("Password")).getText(); String keyPassword = ksEle.getFirstChildWithName(new QName("KeyPassword")).getText(); try {/* www .j ava 2 s . c om*/ KeyStore keyStore = KeyStore.getInstance(type); URL url = getClass().getClassLoader().getResource(location); log.debug("Loading Key Store from URL : " + url); keyStore.load(url.openStream(), storePassword.toCharArray()); KeyManagerFactory kmfactory = KeyManagerFactory .getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmfactory.init(keyStore, keyPassword.toCharArray()); keymanagers = kmfactory.getKeyManagers(); } catch (GeneralSecurityException gse) { log.error("Error loading Key store : " + location, gse); throw new AxisFault("Error loading Key store : " + location, gse); } catch (IOException ioe) { log.error("Error opening Key store : " + location, ioe); throw new AxisFault("Error opening Key store : " + location, ioe); } } if (trustParam != null) { OMElement tsEle = trustParam.getParameterElement().getFirstElement(); String location = tsEle.getFirstChildWithName(new QName("Location")).getText(); String type = tsEle.getFirstChildWithName(new QName("Type")).getText(); String storePassword = tsEle.getFirstChildWithName(new QName("Password")).getText(); try { KeyStore trustStore = KeyStore.getInstance(type); URL url = getClass().getClassLoader().getResource(location); log.debug("Loading Trust Key Store from URL : " + url); trustStore.load(url.openStream(), storePassword.toCharArray()); TrustManagerFactory trustManagerfactory = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerfactory.init(trustStore); trustManagers = trustManagerfactory.getTrustManagers(); } catch (GeneralSecurityException gse) { log.error("Error loading Key store : " + location, gse); throw new AxisFault("Error loading Key store : " + location, gse); } catch (IOException ioe) { log.error("Error opening Key store : " + location, ioe); throw new AxisFault("Error opening Key store : " + location, ioe); } } try { SSLContext sslcontext = SSLContext.getInstance("TLS"); sslcontext.init(keymanagers, trustManagers, null); return sslcontext; } catch (GeneralSecurityException gse) { log.error("Unable to create SSL context with the given configuration", gse); throw new AxisFault("Unable to create SSL context with the given configuration", gse); } }
From source file:org.apache.streams.cassandra.CassandraClient.java
public void start() throws Exception { Objects.nonNull(config);//from w w w .java 2 s . co m LOGGER.info("CassandraClient.start {}", config); Cluster.Builder builder = Cluster.builder().withPort(config.getPort().intValue()).withoutJMXReporting() .withoutMetrics() .withSocketOptions(new SocketOptions().setConnectTimeoutMillis(DEFAULT_CONNECT_TIMEOUT_MILLIS * 10) .setReadTimeoutMillis(DEFAULT_READ_TIMEOUT_MILLIS * 10)); if (config.getSsl() != null && config.getSsl().getEnabled() == true) { Ssl ssl = config.getSsl(); KeyStore ks = KeyStore.getInstance("JKS"); InputStream trustStore = new FileInputStream(ssl.getTrustStore()); ks.load(trustStore, ssl.getTrustStorePassword().toCharArray()); InputStream keyStore = new FileInputStream(ssl.getKeyStore()); ks.load(keyStore, ssl.getKeyStorePassword().toCharArray()); TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(ks); KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(ks, ssl.getKeyStorePassword().toCharArray()); SSLContext sslContext = SSLContext.getInstance("SSLv3"); sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); SSLOptions sslOptions = JdkSSLOptions.builder().withSSLContext(sslContext).build(); builder = builder.withSSL(sslOptions); } Collection<InetSocketAddress> addresses = new ArrayList<>(); for (String h : config.getHosts()) { LOGGER.info("Adding Host: {}", h); InetSocketAddress socketAddress = new InetSocketAddress(h, config.getPort().intValue()); addresses.add(socketAddress); } builder.addContactPointsWithPorts(addresses); if (StringUtils.isNotBlank(config.getUser()) && StringUtils.isNotBlank(config.getPassword())) { builder.withCredentials(config.getUser(), config.getPassword()); } cluster = builder.build(); Objects.nonNull(cluster); try { Metadata metadata = cluster.getMetadata(); LOGGER.info("Connected to cluster: {}\n", metadata.getClusterName()); for (Host host : metadata.getAllHosts()) { LOGGER.info("Datacenter: {}; Host: {}; Rack: {}\n", host.getDatacenter(), host.getAddress(), host.getRack()); } } catch (Exception e) { LOGGER.error("Exception: {}", e); throw e; } try { session = cluster.connect(); } catch (Exception e) { LOGGER.error("Exception: {}", e); throw e; } Objects.nonNull(session); }
From source file:opendap.dap.http.EasySSLProtocolSocketFactory.java
private SSLContext createSSLContext() throws HTTPException { try {//w w w . j a va2 s . c o m KeyManager[] keymanagers = null; KeyStore keystore = null; KeyStore truststore = null; TrustManager[] trustmanagers = null; String keypassword = getpassword("key"); String keypath = getstorepath("key"); String trustpassword = getpassword("trust"); String trustpath = getstorepath("trust"); keystore = buildstore(keypath, keypassword, "key"); if (keystore != null) { KeyManagerFactory kmfactory = KeyManagerFactory.getInstance("SunX509"); kmfactory.init(keystore, keypassword.toCharArray()); keymanagers = kmfactory.getKeyManagers(); } truststore = buildstore(trustpath, trustpassword, "trust"); if (truststore != null) { //TrustManagerFactory trfactory = TrustManagerFactory.getInstance("SunX509"); //trfactory.init(truststore, trustpassword.toCharArray()); //trustmanagers = trfactory.getTrustManagers(); trustmanagers = new TrustManager[] { new EasyX509TrustManager(truststore) }; } SSLContext sslcontext = SSLContext.getInstance("SSL"); sslcontext.init(keymanagers, trustmanagers, null); return sslcontext; } catch (NoSuchAlgorithmException e) { throw new HTTPException("Unsupported algorithm exception: " + e.getMessage()); } catch (KeyStoreException e) { throw new HTTPException("Keystore exception: " + e.getMessage()); } catch (GeneralSecurityException e) { throw new HTTPException("Key management exception: " + e.getMessage()); } catch (IOException e) { throw new HTTPException("I/O error reading keystore/truststore file: " + e.getMessage()); } }
From source file:org.wildfly.elytron.web.undertow.server.ClientCertAuthenticationTest.java
/** * Get the key manager backed by the specified key store. * * @param keystoreName the name of the key store to load. * @return the initialised key manager./*from w w w. ja va2 s .c om*/ */ private X509ExtendedKeyManager getKeyManager(final String keystorePath) throws Exception { KeyManagerFactory keyManagerFactory = KeyManagerFactory .getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(loadKeyStore(keystorePath), "Elytron".toCharArray()); for (KeyManager current : keyManagerFactory.getKeyManagers()) { if (current instanceof X509ExtendedKeyManager) { return (X509ExtendedKeyManager) current; } } throw new IllegalStateException("Unable to obtain X509ExtendedKeyManager."); }
From source file:org.lockss.util.urlconn.AuthSSLProtocolSocketFactory.java
private SSLContext createSSLContext() throws IOException { LockssDaemon daemon = LockssDaemon.getLockssDaemon(); LockssKeyStoreManager keystoreMgr;/*from w ww .jav a 2s . com*/ SecureRandom rng; try { if (daemon.isDaemonRunning()) { keystoreMgr = daemon.getKeystoreManager(); RandomManager rmgr = daemon.getRandomManager(); rng = rmgr.getSecureRandom(); } else { rng = getSecureRandom(); keystoreMgr = new LockssKeyStoreManager(); keystoreMgr.initService(daemon); keystoreMgr.startService(); Configuration platConfig = ConfigManager.getPlatformConfig(); keystoreMgr.setConfig(platConfig, null, platConfig.differences(null)); } KeyManager[] kma = null; if (privateKeyStoreName != null) { KeyManagerFactory kmf = keystoreMgr.getKeyManagerFactory(privateKeyStoreName, "ClientAuth"); if (kmf != null) { kma = kmf.getKeyManagers(); } else if (false) { throw new IllegalArgumentException("Private keystore not found: " + privateKeyStoreName); } } TrustManager[] tma = null; if (publicKeyStoreName != null) { TrustManagerFactory tmf = keystoreMgr.getTrustManagerFactory(publicKeyStoreName, "ServerAuth"); if (tmf != null) { tma = tmf.getTrustManagers(); } else if (false) { throw new IllegalArgumentException("Public keystore not found: " + publicKeyStoreName); } } // Now create an SSLContext from the KeyManager SSLContext ctxt = null; ctxt = SSLContext.getInstance(sslProtocol); // "SSL" ctxt.init(kma, tma, rng); log.debug2("createSSLContext: " + ctxt); hasKeyManagers = kma != null && kma.length != 0; hasTrustManagers = tma != null && tma.length != 0; return ctxt; } catch (NoSuchAlgorithmException ex) { throw new IOException("Can't create SSL Context", ex); } catch (NoSuchProviderException ex) { throw new IOException("Can't create SSL Context", ex); } catch (KeyManagementException ex) { throw new IOException("Can't create SSL Context", ex); } }