/** * Get the SSLSocketFactory for the current keystore and truststore specified in the connection * * @return The SSLSocketFactory constructed * @throws Exception */ protected SSLSocketFactory getSSLSocketFactory() throws Exception { KeyManager[] km = getKeyManagers(keyStoreFile, keyStorePassword); TrustManager[] tm = getTrustManagers(trustStoreFile, trustStorePassword); SSLContext sslContext = SSLContext.getInstance("SSL"); sslContext.init(km, tm, null); return sslContext.getSocketFactory(); }
/** Creates a new URL to use as the basis of a connection. */ public MsgRpcImpl( String username, String password, String host, int port, boolean ssl, boolean debugf) throws MalformedURLException { if (ssl) { // Install the all-trusting trust manager & HostnameVerifier try { SSLContext sc = SSLContext.getInstance("SSL"); sc.init( null, new TrustManager[] { new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType) {} public void checkServerTrusted( java.security.cert.X509Certificate[] certs, String authType) {} } }, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier() { public boolean verify(String string, SSLSession ssls) { return true; } }); } catch (Exception e) { } u = new URL("https", host, port, "/api/1.0/"); } else { u = new URL("http", host, port, "/api/1.0/"); } /* login to msf server */ Object[] params = new Object[] {username, password}; Map results = exec("auth.login", params); /* save the temp token (lasts for 5 minutes of inactivity) */ rpcToken = results.get("token").toString(); /* generate a non-expiring token and use that */ params = new Object[] {rpcToken}; results = exec("auth.token_generate", params); rpcToken = results.get("token").toString(); }
JSSEServer(CipherTest cipherTest) throws Exception { super(cipherTest); SSLContext serverContext = SSLContext.getInstance("TLS"); serverContext.init( new KeyManager[] {cipherTest.keyManager}, new TrustManager[] {cipherTest.trustManager}, cipherTest.secureRandom); SSLServerSocketFactory factory = (SSLServerSocketFactory) serverContext.getServerSocketFactory(); serverSocket = (SSLServerSocket) factory.createServerSocket(cipherTest.serverPort); cipherTest.serverPort = serverSocket.getLocalPort(); serverSocket.setEnabledCipherSuites(factory.getSupportedCipherSuites()); serverSocket.setWantClientAuth(true); }
public static void main(PeerFactory peerFactory, KeyStore keyStore, String[] args) throws Exception { long time = System.currentTimeMillis(); String relPath; if ((args != null) && (args.length > 0) && args[0].equals("sh")) { relPath = pathToStoresSH; } else { relPath = pathToStores; } PATH = new File(System.getProperty("test.src", "."), relPath); CipherTest.peerFactory = peerFactory; System.out.print("Initializing test '" + peerFactory.getName() + "'..."); // secureRandom = new SecureRandom(); // secureRandom.nextInt(); // trustStore = readKeyStore(trustStoreFile); CipherTest.keyStore = keyStore; // keyStore = readKeyStore(keyStoreFile); KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyFactory.init(keyStore, "test12".toCharArray()); keyManager = (X509ExtendedKeyManager) keyFactory.getKeyManagers()[0]; TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(keyStore); trustManager = (X509TrustManager) tmf.getTrustManagers()[0]; // trustManager = new AlwaysTrustManager(); SSLContext context = SSLContext.getInstance("TLS"); context.init(new KeyManager[] {keyManager}, new TrustManager[] {trustManager}, null); SSLContext.setDefault(context); CipherTest cipherTest = new CipherTest(peerFactory); Thread serverThread = new Thread(peerFactory.newServer(cipherTest), "Server"); serverThread.setDaemon(true); serverThread.start(); System.out.println("Done"); cipherTest.run(); time = System.currentTimeMillis() - time; System.out.println("Done. (" + time + " ms)"); }
public void run() { try { URL url = new URL(protocol + "://localhost:" + port + "/test1/" + f); HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); if (urlc instanceof HttpsURLConnection) { HttpsURLConnection urlcs = (HttpsURLConnection) urlc; urlcs.setHostnameVerifier( new HostnameVerifier() { public boolean verify(String s, SSLSession s1) { return true; } }); urlcs.setSSLSocketFactory(ctx.getSocketFactory()); } byte[] buf = new byte[4096]; if (fixedLen) { urlc.setRequestProperty("XFixed", "yes"); } InputStream is = urlc.getInputStream(); File temp = File.createTempFile("Test1", null); temp.deleteOnExit(); OutputStream fout = new BufferedOutputStream(new FileOutputStream(temp)); int c, count = 0; while ((c = is.read(buf)) != -1) { count += c; fout.write(buf, 0, c); } is.close(); fout.close(); if (count != size) { throw new RuntimeException("wrong amount of data returned"); } String orig = root + "/" + f; compare(new File(orig), temp); temp.delete(); } catch (Exception e) { e.printStackTrace(); fail = true; } }
public SickBeard( String hostname, String port, String api, boolean https, String extraPath, String user, String password) { this.hostname = hostname; this.port = port; this.extraPath = "/" + extraPath + "/"; this.path = this.extraPath + "/api/" + api + "/"; try { this.https = https; this.scheme = "http"; Authenticator.setDefault(new SickAuthenticator(user, password)); if (https) { SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init( new KeyManager[0], new TrustManager[] {new DefaultTrustManager()}, new SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory()); HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier() { @Override public boolean verify(String arg0, SSLSession arg1) { return true; } }); scheme = "https"; } } catch (Exception e) {; } /** * ********************************************************* ANDROID SPECIFIC START * * ********************************************************* */ // start a AsyncTask to try and find the actual api version number AsyncTask<Void, Void, CommandsJson> task = new AsyncTask<Void, Void, CommandsJson>() { @Override protected CommandsJson doInBackground(Void... arg0) { try { return SickBeard.this.sbGetCommands(); } catch (Exception e) { Log.e("SickBeard", e.getMessage(), e); return null; } } @Override protected void onPostExecute(CommandsJson result) { // do nothing because this is a network error if (result == null) return; try { // if we get a version use it SickBeard.this.apiVersion = Integer.valueOf(result.api_version); } catch (NumberFormatException e) { // 2 was the odd float so assume its 2 if we cant get an int SickBeard.this.apiVersion = 2; } } }; task.execute(); /** * ********************************************************* ANDROID SPECIFIC END * * ********************************************************* */ }