2016-03-21 54 views
15

Ich habe in den letzten paar Tagen meinen Kopf an die Wand geschlagen, versucht OCSP Validierung in Android zu implementieren.OCSP Zertifikat Heften in Android

Bisher in iOS war einfach zu implementieren, aber für Android jedes einzelne Stück von Informationen, die ich zufällig gerade nicht funktioniert. Ich habe sowohl den API-Endpunkt meines Kunden als auch this website verwendet, um Tests für die Zertifikatsperrung auszuführen, und bisher war ich nicht glücklich, ein widerrufenes Zertifikat in meiner Android-Anwendung zu finden. Ich verwende OKHTTPClient. Hier ist das Verfahren, in dem ich Zertifizierung Widerruf

public void checkServerTrusted(X509Certificate[] chain, String authType) 
      throws CertificateException { 

     assert (chain != null); 
     if (chain == null) { 
      throw new IllegalArgumentException(
        "checkServerTrusted: X509Certificate array is null"); 
     } 

     assert (chain.length > 0); 
     if (!(chain.length > 0)) { 
      throw new IllegalArgumentException(
        "checkServerTrusted: X509Certificate is empty"); 
     } 

     if (VERIFY_AUTHTYPE) { 
      assert (null != authType && authType.equalsIgnoreCase(AUTH_TYPE)); 
      if (!(null != authType && authType.equalsIgnoreCase(AUTH_TYPE))) { 
       throw new CertificateException(
         "checkServerTrusted: AuthType is not " + AUTH_TYPE); 
      } 
     } 

     if(chain[0]!=null){ 
      try { 
       X509Certificate issuerCert = chain[1]; 
       X509Certificate c1 = chain[0]; 
       TrustAnchor anchor = new TrustAnchor(issuerCert, null); 
       Set anchors = Collections.singleton(anchor); 
       CertificateFactory cf = CertificateFactory.getInstance("X.509"); 
       List list = Arrays.asList(new Certificate[]{c1}); 
       CertPath path = cf.generateCertPath(list); 
       PKIXParameters params = new PKIXParameters(anchors); 
       // Activate certificate revocation checking 
       params.setRevocationEnabled(false); 
       // Activate OCSP 
       Security.setProperty("ocsp.enable", "true"); 

       // Ensure that the ocsp.responderURL property is not set. 
       if (Security.getProperty("ocsp.responderURL") != null) { 
        throw new 
          Exception("The ocsp.responderURL property must not be set"); 
       } 
       CertPathValidator validator = CertPathValidator.getInstance("PKIX"); 
       PKIXCertPathValidatorResult result = (PKIXCertPathValidatorResult) validator 
         .validate(path, params); 

       System.out.println("VALID"); 
      } catch (Exception e) { 
       System.out.println("EXCEPTION " + e.getMessage()); 
       e.printStackTrace(); 
      } 

Antwort

0

Statt mit OkHttp versuchen Android mit dem in Build HttpURLConnection oder HttpsURLConnection bei https validieren: //