2013-02-26 11 views
7

Utworzono klienta protokołu OCSP przy użyciu interfejsu API Bouncy Castle. Mam problem ze znalezieniem statusu certyfikatu (powiedzmy, czy został on unieważniony lub nie) z odpowiedzi protokołu OCSP, którą otrzymałem. Wartość zwrócona przez resp.getCertStatus() jest zawsze pusta. Oto jak utworzyć żądanie protokołu OCSP.Odpowiedź OCSP nie daje statusu certyfikatu

private OCSPReq generateOCSPRequest(X509Certificate issuerCert, BigInteger serialNumber) 
     throws CertificateVerificationException { 

    //Add provider BC 
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); 
    try { 
     // CertID structure is used to uniquely identify certificates that are the subject of 
     // an OCSP request or response and has an ASN.1 definition. CertID structure is defined in RFC 2560 
     CertificateID id = new CertificateID(CertificateID.HASH_SHA1, issuerCert, serialNumber); 

     // basic request generation with nonce 
     OCSPReqGenerator generator = new OCSPReqGenerator(); 
     generator.addRequest(id); 

     // create details for nonce extension. The nonce extension is used to bind 
     // a request to a response to prevent replay attacks. As the name implies, 
     // the nonce value is something that the client should only use once within a reasonably small period. 
     BigInteger nonce = BigInteger.valueOf(System.currentTimeMillis()); 
     Vector objectIdentifiers = new Vector(); 
     Vector values = new Vector(); 

     //to create the request Extension 
     objectIdentifiers.add(OCSPObjectIdentifiers.id_pkix_ocsp_nonce); 
     values.add(new X509Extension(false, new DEROctetString(nonce.toByteArray()))); 
     generator.setRequestExtensions(new X509Extensions(objectIdentifiers, values)); 

     return generator.generate(); 
    } 
    catch (OCSPException e) { 
     e.printStackTrace(); 
     throw new CertificateVerificationException("Cannot generate OSCP Request with the given certificate",e); 
    } 
} 

Otrzymuję odpowiedź OCSP z adresu URL usługi w następujący sposób.

private OCSPResp getOCSPResponce(String serviceUrl, OCSPReq request) throws CertificateVerificationException { 

    try { 
     byte[] array = request.getEncoded(); 
     if (serviceUrl.startsWith("http")) { 
      HttpURLConnection con; 
      URL url = new URL(serviceUrl); 
      con = (HttpURLConnection) url.openConnection(); 
      con.setRequestProperty("Content-Type", "application/ocsp-request"); 
      con.setRequestProperty("Accept", "application/ocsp-response"); 
      con.setDoOutput(true); 
      OutputStream out = con.getOutputStream(); 
      DataOutputStream dataOut = new DataOutputStream(new BufferedOutputStream(out)); 
      dataOut.write(array); 

      dataOut.flush(); 
      dataOut.close(); 

      //Get Response 
      InputStream in = (InputStream) con.getContent(); 
      OCSPResp ocspResponse = new OCSPResp(in); 
      return ocspResponse; 
     } 
     else { 
      throw new CertificateVerificationException("Only http is supported for ocsp calls"); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
     throw new CertificateVerificationException("Cannot get ocspResponse from url: "+ serviceUrl, e); 
    } 
} 

Status unieważnienia jest sprawdzany w następujący sposób. W tym przypadku obiekt SingleResp (resp) pobrany z obiektu BasicOCSPResp (basicResponse) powinien mieć status Certyfikatu (dobry, odwołany lub nieznany). Ale tutaj status jest zawsze zerowy.

public void checkRevocationStatus(X509Certificate peerCert, X509Certificate issuerCert) 
throws CertificateVerificationException { 

    try { 

     OCSPReq request = generateOCSPRequest(issuerCert, peerCert.getSerialNumber()); 
     List<String> locations = getAIALocations(peerCert); 
     Iterator it = locations.iterator(); 

     if (it.hasNext()) { 

      String serviceUrl = (String) it.next(); 
      OCSPResp ocspResponse = getOCSPResponce(serviceUrl, request); 
      if(OCSPRespStatus.SUCCESSFUL==ocspResponse.getStatus()) 
       System.out.println("server gave response fine"); 

      BasicOCSPResp basicResponse = (BasicOCSPResp) ocspResponse.getResponseObject(); 
      SingleResp[] responses = (basicResponse==null) ? null : basicResponse.getResponses(); 

      if (responses!=null && responses.length == 1) { 
       SingleResp resp = responses[0]; 
       Object status = resp.getCertStatus(); 
       if(status!=null) { 
        if (status == CertificateStatus.GOOD) { 
         System.out.println("OCSP Status is good!"); 
        } else if (status instanceof org.bouncycastle.ocsp.RevokedStatus) { 
         System.out.println("OCSP Status is revoked!"); 
        } else if (status instanceof org.bouncycastle.ocsp.UnknownStatus) { 
         System.out.println("OCSP Status is unknown!"); 
        } 
       } 
      } 
     } 
    } 
    catch (Exception e) { 
     System.out.println(e); 
    } 
} 

Bardzo dziękuję za pomoc. Wielkie dzięki.

Odpowiedz

6

W rzeczywistości, jeśli spojrzysz na rzeczywistą wartość CertificateStatus.GOOD, zobaczysz, że jest ona faktycznie zerowa. Innymi słowy, resp.getCertStatus() zwraca wartość null oznaczającą DOBRY.

Prawdopodobnie wystarczy wykupić czek (status! = Null).

Powiązane problemy