2017-04-25 11 views
9

이미 프로젝트에서 leaf 인증서를 구현 했으므로 잘 작동합니다. 아래 코드를 확인하십시오. 이제 잎 증명서가 1 년 후에 내 서버에서 만료되므로 잎 인증서의 유효성을 검사하여 유효 기간이 만료되었거나 유효하지 않을 때 중간 인증서를 사용할 수 있습니까?Android에서 리프/중간 인증서 고정을 구현하는 방법은 무엇입니까?

중간 인증서를 구현하는 예가 있습니까?

도와주세요!

코드 : -

SSLContext sslContext = null; 
     try { 
      CertificateFactory cf = CertificateFactory.getInstance("X.509"); 
      InputStream caInput = context.getResources().openRawResource(certRawRef); 
      Certificate ca; 
      try { 
       ca = cf.generateCertificate(caInput); 
      } finally { 
       caInput.close(); 
      } 
      // Create a KeyStore containing our trusted CAs 
      String keyStoreType = KeyStore.getDefaultType(); 
      KeyStore keyStore = KeyStore.getInstance(keyStoreType); 
      keyStore.load(null, null); 
      keyStore.setCertificateEntry("ca", ca); 
      // Create a TrustManager that trusts the CAs in our KeyStore 
      String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); 
      TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); 
      tmf.init(keyStore); 
      // Create an SSLContext that uses our TrustManager 

      sslContext = SSLContext.getInstance("TLSv1.2"); 
      sslContext.init(null, tmf.getTrustManagers(), null); 
      return sslContext; 
     } catch (Exception e) { 
      Log.e("EXCEPTION",e.toString()); 
      //Print here right certificate failure issue 
     } 
+1

이 문서가 당신을 도울 수 ... HTTPS : //medium.com/@appmattus/android-security- ssl-pinning-1db8acb6621e – PN10

답변

1

마지막으로 내가 대답을 발견 : -

try { 
      CertificateFactory cf = CertificateFactory.getInstance("X.509"); 
      InputStream caInputLeaf = context.getResources().openRawResource(leafCert); 
      InputStream caInputInter = context.getResources().openRawResource(interCert); 
      try { 
       if (cf != null) { 
        ca = cf.generateCertificate(caInputLeaf); 

        URL url = new URL(URL); 
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); 
        conn.setRequestMethod("GET"); 
        conn.connect(); 

        chain = conn.getServerCertificates(); 
        if(chain!=null && chain[0].equals(ca)) {   //Return Leaf certificate 
         return ca; 
        } 
        else{         //Return Intermediate certificate 
         ca = cf.generateCertificate(caInputInter); 
         return ca; 
        } 
       } 
      } catch (Exception cee) { 
       ca = cf.generateCertificate(caInputInter); 
       return ca; 
      } 
     } catch (Exception e) { 
      Log.e("EXCEPTION", e.toString()); 
     }