public void testDefaultSecureSocketProtocol() throws Exception {
    SSLContextParameters scp = new SSLContextParameters();

    SSLContext context = scp.createSSLContext();

    assertEquals("TLS", context.getProtocol());
  }
Exemple #2
0
  /**
   * Tests whether this client can make an HTTP connection with TLS 1.2.
   *
   * @return true if connection is successful. false otherwise.
   */
  public static boolean testTls12Connection() {
    String protocol = "N/A";
    try {
      SSLContext sslContext = SSLContext.getInstance(getLatestProtocol().toString());
      protocol = sslContext.getProtocol();
      sslContext.init(null, null, null);
      HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());

      URL url = new URL("https://" + ENDPOINT);
      HttpsURLConnection httpsConnection = (HttpsURLConnection) url.openConnection();

      httpsConnection.connect();
      BufferedReader reader =
          new BufferedReader(new InputStreamReader(httpsConnection.getInputStream()));
      StringBuilder body = new StringBuilder();
      while (reader.ready()) {
        body.append(reader.readLine());
      }
      httpsConnection.disconnect();
      if (body.toString().equals("PayPal_Connection_OK")) {
        return true;
      }

    } catch (NoSuchAlgorithmException e) {
    } catch (UnknownHostException e) {
    } catch (IOException e) {
    } catch (KeyManagementException e) {
    }
    return false;
  }
  /**
   * Creates a new {@link SSLContext} using the receiver's configuration.
   *
   * @param context context for status messages
   * @return {@link SSLContext} object
   * @throws NoSuchProviderException if a provider specified for one of the JCA or JSSE components
   *     utilized in creating the context is not known to the platform
   * @throws NoSuchAlgorithmException if a JCA or JSSE algorithm, protocol, or type name specified
   *     for one of the context's components is not known to a given provider (or platform default
   *     provider for the component)
   * @throws KeyManagementException if an error occurs in creating a {@link KeyManager} for the
   *     context
   * @throws UnrecoverableKeyException if a private key needed by a {@link KeyManager} cannot be
   *     obtained from a key store
   * @throws KeyStoreException if an error occurs in reading the contents of a key store
   * @throws CertificateException if an error occurs in reading the contents of a certificate
   */
  public SSLContext createContext(ContextAware context)
      throws NoSuchProviderException, NoSuchAlgorithmException, KeyManagementException,
          UnrecoverableKeyException, KeyStoreException, CertificateException {

    SSLContext sslContext =
        getProvider() != null
            ? SSLContext.getInstance(getProtocol(), getProvider())
            : SSLContext.getInstance(getProtocol());

    context.addInfo(
        "SSL protocol '"
            + sslContext.getProtocol()
            + "' provider '"
            + sslContext.getProvider()
            + "'");

    KeyManager[] keyManagers = createKeyManagers(context);
    TrustManager[] trustManagers = createTrustManagers(context);
    SecureRandom secureRandom = createSecureRandom(context);
    sslContext.init(keyManagers, trustManagers, secureRandom);
    return sslContext;
  }