/**
   * 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();
  }
Пример #2
0
  /** 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();
  }
Пример #3
0
    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;
      }
    }
Пример #4
0
  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 *
     * *********************************************************
     */
  }