Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: userc865b
Added: Jul 15, 2018 7:34 PM
Modified: Jul 15, 2018 7:37 PM
Views: 11
Tags: java
add an certifiacte!
  1.  
  2.  
  3.  SSLContext setCertificateFromFile(Context context)
  4.  HttpsURLConnection request = (HttpsURLConnection) url.openConnection();
  5.  request.setSSLSocketFactory(context2.getSocketFactory());
  6.  // further code here
  7.  
  8. private static SSLContext setCertificateFromFile(Context context) throws IOException, CertificateException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
  9.         AssetManager am = context.getAssets();
  10.         InputStream is = am.open("strato.crt");
  11.         // Load CAs from an InputStream
  12.         // (could be from a resource or ByteArrayInputStream or ...)
  13.         CertificateFactory cf = CertificateFactory.getInstance("X.509");
  14.         // From https://www.washington.edu/itconnect/security/ca/load-der.crt
  15.         InputStream caInput = new BufferedInputStream(is);
  16.         Certificate ca;
  17.         try {
  18.             ca = cf.generateCertificate(caInput);
  19.         } finally {
  20.             caInput.close();
  21.         }
  22.  
  23.         // Create a KeyStore containing our trusted CAs
  24.         String keyStoreType = KeyStore.getDefaultType();
  25.         KeyStore keyStore = KeyStore.getInstance(keyStoreType);
  26.         keyStore.load(null, null);
  27.         keyStore.setCertificateEntry("ca", ca);
  28.  
  29.         // Create a TrustManager that trusts the CAs in our KeyStore
  30.         String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
  31.         TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
  32.         tmf.init(keyStore);
  33.  
  34.         // Create an SSLContext that uses our TrustManager
  35.         SSLContext context2 = SSLContext.getInstance("TLS");
  36.         context2.init(null, tmf.getTrustManagers(), null);
  37.         return context2;
  38.     }
  39.  
  40.  
  41.  // HERE IS THE SOLUTION : https://developer.android.com/training/articles/security-ssl#java
  42.  
  43.  // Load CAs from an InputStream
  44. // (could be from a resource or ByteArrayInputStream or ...)
  45. CertificateFactory cf = CertificateFactory.getInstance("X.509");
  46. // From https://www.washington.edu/itconnect/security/ca/load-der.crt
  47. InputStream caInput = new BufferedInputStream(new FileInputStream("load-der.crt"));
  48. try {
  49.     ca = cf.generateCertificate(caInput);
  50.     System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
  51. } finally {
  52.     caInput.close();
  53. }
  54.  
  55. // Create a KeyStore containing our trusted CAs
  56. String keyStoreType = KeyStore.getDefaultType();
  57. KeyStore keyStore = KeyStore.getInstance(keyStoreType);
  58. keyStore.load(null, null);
  59. keyStore.setCertificateEntry("ca", ca);
  60.  
  61. // Create a TrustManager that trusts the CAs in our KeyStore
  62. String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
  63. TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
  64. tmf.init(keyStore);
  65.  
  66. // Create an SSLContext that uses our TrustManager
  67. SSLContext context = SSLContext.getInstance("TLS");
  68. context.init(null, tmf.getTrustManagers(), null);
  69.  
  70. // Tell the URLConnection to use a SocketFactory from our SSLContext
  71. URL url = new URL("https://certs.cac.washington.edu/CAtest/");
  72. HttpsURLConnection urlConnection =
  73.     (HttpsURLConnection)url.openConnection();
  74. urlConnection.setSSLSocketFactory(context.getSocketFactory());
  75. InputStream in = urlConnection.getInputStream();
  76. copyInputStreamToOutputStream(in, System.out);
  77.  
  78.  
  79.