List of usage examples for com.itextpdf.text.pdf.security CertificateUtil getOCSPURL
public static String getOCSPURL(X509Certificate certificate)
From source file:controller.CCInstance.java
License:Open Source License
private OCSPResp getOcspResponse(X509Certificate checkCert, X509Certificate rootCert) throws GeneralSecurityException, OCSPException, IOException, OperatorException { if (checkCert == null || rootCert == null) { return null; }//from w w w . ja va 2s . c o m String url = CertificateUtil.getOCSPURL(checkCert); if (url == null) { return null; } try { OCSPReq request = generateOCSPRequest(rootCert, checkCert.getSerialNumber()); byte[] array = request.getEncoded(); URL urlt = new URL(url); HttpURLConnection con = (HttpURLConnection) urlt.openConnection(); con.setRequestProperty("Content-Type", "application/ocsp-request"); con.setRequestProperty("Accept", "application/ocsp-response"); con.setDoOutput(true); OutputStream out = con.getOutputStream(); try (DataOutputStream dataOut = new DataOutputStream(new BufferedOutputStream(out))) { dataOut.write(array); dataOut.flush(); } if (con.getResponseCode() / 100 != 2) { throw new IOException( MessageLocalization.getComposedMessage("invalid.http.response.1", con.getResponseCode())); } //Get Response InputStream in = (InputStream) con.getContent(); return new OCSPResp(in); } catch (Exception e) { return null; } }
From source file:ec.rubrica.ocsp.ValidadorOCSP.java
License:Open Source License
public static void check(X509Certificate issuerCert, X509Certificate x509Cert) throws OcspValidationException, OcspTimeoutException { try {/* ww w. j a v a 2s . c o m*/ BigInteger serialNumber = x509Cert.getSerialNumber(); X509CertificateHolder holder; try { holder = new X509CertificateHolder(issuerCert.getEncoded()); } catch (IOException e) { throw new RuntimeException(e); } CertificateID id = new CertificateID(new JcaDigestCalculatorProviderBuilder() .setProvider(BouncyCastleProvider.PROVIDER_NAME).build().get(CertificateID.HASH_SHA1), holder, serialNumber); OCSPReqBuilder ocspGen = new OCSPReqBuilder(); ocspGen.addRequest(id); OCSPReq ocspReq = ocspGen.build(); // Ir al OCSP String ocspUrl = CertificateUtil.getOCSPURL(x509Cert); if (ocspUrl == null) { logger.info("URL de OCSP is null"); return; } URL url; try { url = new URL(ocspUrl); } catch (MalformedURLException e) { throw new RuntimeException(e); } HttpURLConnection con; OCSPResp ocspResponse; try { 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(ocspReq.getEncoded()); dataOut.flush(); dataOut.close(); /* * Se parsea la respuesta y se obtiene el estado del certificado * retornado por el OCSP */ InputStream in = (InputStream) con.getContent(); byte[] resp = read(in); // Read the reponse ocspResponse = new OCSPResp(resp); } catch (IOException e) { throw new OcspTimeoutException(url); } int status = ocspResponse.getStatus(); System.out.println("status=" + status); BasicOCSPResp basicResponse = (BasicOCSPResp) ocspResponse.getResponseObject(); if (basicResponse != null) { SingleResp[] responses = basicResponse.getResponses(); SingleResp response = responses[0]; CertificateStatus certStatus = response.getCertStatus(); if (certStatus instanceof RevokedStatus) { System.out.println("REVOKED"); RevokedStatus revokedStatus = (RevokedStatus) certStatus; System.out.println("Reason: " + revokedStatus.getRevocationReason()); System.out.println("Date: " + revokedStatus.getRevocationTime()); throw new OcspValidationException(revokedStatus.getRevocationReason(), revokedStatus.getRevocationTime()); } } } catch (OCSPException e) { throw new RuntimeException(e); } catch (CertificateEncodingException e) { throw new RuntimeException(e); } catch (OperatorCreationException e) { throw new RuntimeException(e); } }
From source file:ec.rubrica.pdf.VerificadorFirmaPdf.java
License:Open Source License
public Verificacion verificar() throws OcspValidationException, SignatureException { int totalRevisiones = af.getTotalRevisions(); Verificacion verificacion = new Verificacion(totalRevisiones); ArrayList<String> nombres = af.getSignatureNames(); System.out.println("Cuantos nombres=" + nombres.size()); for (String nombre1 : nombres) { System.out.println("nombre=" + nombre1); PdfPKCS7 pk = af.verifySignature(nombre1); X509Certificate certificadoFirmante = pk.getSigningCertificate(); log.info("Subject: " + CertificateInfo.getSubjectFields(pk.getSigningCertificate())); Certificate[] chain = pk.getSignCertificateChain(); // Verificar OCSP: try {/* ww w. j a v a 2 s . c om*/ verificarOscp(certificadoFirmante); } catch (OcspTimeoutException e) { throw new SignatureException(e); } } for (String nombre : nombres) { PdfPKCS7 pk = af.verifySignature(nombre); boolean firmaCubreTodoDocumento = af.signatureCoversWholeDocument(nombre); int revision = af.getRevision(nombre); X509Certificate certificadoFirmante = pk.getSigningCertificate(); log.info("Subject: " + CertificateInfo.getSubjectFields(pk.getSigningCertificate())); Calendar fechaFirma = pk.getSignDate(); TimeStampToken tst = pk.getTimeStampToken(); if (tst != null) { log.fine("La firma Tiene Time Stamp"); fechaFirma = pk.getTimeStampDate(); } boolean selladoTiempoCorrecto = false; if (!pk.isTsp() && tst != null) { try { selladoTiempoCorrecto = pk.verifyTimestampImprint(); } catch (NoSuchAlgorithmException e) { throw new SignatureException(e); } } Certificate[] certificados = pk.getCertificates(); // TODO: DEBUG Certificate[] chain = pk.getSignCertificateChain(); for (int i = 0; i < chain.length; i++) { X509Certificate cert = (X509Certificate) chain[i]; System.out.println(String.format("[%s] %s", i, cert.getSubjectDN())); System.out.println(CertificateUtil.getOCSPURL(cert)); } // TODO: DEBUG boolean documentoModificado = !pk.verify(); Firma firma = new Firma(nombre, firmaCubreTodoDocumento, revision, certificadoFirmante, fechaFirma, selladoTiempoCorrecto, certificados, documentoModificado); // TODO: Implementar CRLs Collection<CRL> crls = null; Object error[] = CertificateVerification.verifyCertificates(certificados, cacerts, crls, fechaFirma); // TODO: Quitar el mensaje y usar una Enum if (error != null) { Object objetoConFalla = error[0]; String mensaje = (String) error[1]; Falla falla; if (objetoConFalla != null) { Certificate certConFalla = (Certificate) objetoConFalla; falla = new Falla(certConFalla, mensaje); } else { falla = new Falla(mensaje); } firma.setFalla(falla); } verificacion.addFirma(firma); } return verificacion; }
From source file:ec.rubrica.pdf.VerificadorFirmaPdf.java
License:Open Source License
private void verificarSiTieneOCSP(Certificate[] chain) { for (int i = 0; i < chain.length; i++) { X509Certificate cert = (X509Certificate) chain[i]; System.out.println(String.format("[%s] %s", i, cert.getSubjectDN())); System.out.println(CertificateUtil.getOCSPURL(cert)); }//w w w . j a v a2s . c om }