Search in sources :

Example 1 with BasicCertificate

use of org.demoiselle.signer.core.extension.BasicCertificate in project signer by demoiselle.

the class CAdESCheckerTest method testVerifyDetachedSignature.

/**
 * Verifica assinatura desanexada do arquivo
 */
// @Test
public void testVerifyDetachedSignature() {
    String fileToVerifyDirName = "/home/{usuario}/arquivo";
    String fileSignatureDirName = "/home/{usuario}/arquivo.p7s";
    byte[] fileToVerify = readContent(fileToVerifyDirName);
    byte[] signatureFile = readContent(fileSignatureDirName);
    CAdESChecker checker = new CAdESChecker();
    System.out.println("Efetuando a validacao da assinatura");
    List<SignatureInformations> signaturesInfo = checker.checkDetachedSignature(fileToVerify, signatureFile);
    if (signaturesInfo != null) {
        System.out.println("A assinatura foi validada.");
        for (SignatureInformations si : signaturesInfo) {
            System.out.println(si.getSignDate());
            if (si.getTimeStampSigner() != null) {
                System.out.println("Serial" + si.getTimeStampSigner().toString());
            }
            for (X509Certificate cert : si.getChain()) {
                BasicCertificate certificate = new BasicCertificate(cert);
                if (!certificate.isCACertificate()) {
                    System.out.println(certificate.toString());
                }
            }
            for (String valErr : si.getValidatorErrors()) {
                System.out.println("++++++++++++++ ERROS ++++++++++++++++++");
                System.out.println(valErr);
            }
            if (si.getSignaturePolicy() != null) {
                System.out.println("------ Politica ----------------- ");
                System.out.println(si.getSignaturePolicy().toString());
            }
        }
        assertTrue(true);
    } else {
        System.out.println("A assinatura foi invalidada!");
        assertTrue(false);
    }
}
Also used : BasicCertificate(org.demoiselle.signer.core.extension.BasicCertificate) SignatureInformations(org.demoiselle.signer.policy.impl.cades.SignatureInformations) X509Certificate(java.security.cert.X509Certificate)

Example 2 with BasicCertificate

use of org.demoiselle.signer.core.extension.BasicCertificate in project signer by demoiselle.

the class CAdESCheckerTest method testVerifySignatureByHash.

/**
 * Verifica assinatura desanexada do arquivo, com envio apenas do Hash do arquivo anexado.
 * Neste exemplo, informa-se o arquivo que foi assinado para facilitar o teste.
 */
// @Test
public void testVerifySignatureByHash() {
    String fileSignatureDirName = "local_e_nome_do_arquivo_da_assinatura";
    // Apenas para gerar o HASH
    String fileToVerifyDirName = "local_e_nome_do_arquivo_assinado";
    byte[] fileToVerify = readContent(fileToVerifyDirName);
    byte[] signatureFile = readContent(fileSignatureDirName);
    java.security.MessageDigest md;
    try {
        md = java.security.MessageDigest.getInstance(DigestAlgorithmEnum.SHA_256.getAlgorithm());
        // gera o hash do arquivo que foi assinado
        byte[] hash = md.digest(fileToVerify);
        CAdESChecker checker = new CAdESChecker();
        System.out.println("Efetuando a validacao da assinatura");
        List<SignatureInformations> signaturesInfo = checker.checkSignatureByHash(SignerAlgorithmEnum.SHA256withRSA.getOIDAlgorithmHash(), hash, signatureFile);
        if (signaturesInfo != null) {
            System.out.println("A assinatura foi validada.");
            for (SignatureInformations si : signaturesInfo) {
                System.out.println(si.getSignDate());
                if (si.getTimeStampSigner() != null) {
                    System.out.println("Serial" + si.getTimeStampSigner().toString());
                }
                for (X509Certificate cert : si.getChain()) {
                    BasicCertificate certificate = new BasicCertificate(cert);
                    if (!certificate.isCACertificate()) {
                        System.out.println(certificate.toString());
                    }
                }
                for (String valErr : si.getValidatorErrors()) {
                    System.out.println("++++++++++++++ ERROS ++++++++++++++++++");
                    System.out.println(valErr);
                }
                System.out.println(si.getSignaturePolicy().toString());
            }
            assertTrue(true);
        } else {
            System.out.println("A assinatura foi invalidada!");
            assertTrue(false);
        }
    } catch (Exception e) {
        e.printStackTrace();
        assertTrue(false);
    }
}
Also used : BasicCertificate(org.demoiselle.signer.core.extension.BasicCertificate) SignatureInformations(org.demoiselle.signer.policy.impl.cades.SignatureInformations) X509Certificate(java.security.cert.X509Certificate) IOException(java.io.IOException)

Example 3 with BasicCertificate

use of org.demoiselle.signer.core.extension.BasicCertificate in project signer by demoiselle.

the class PDFVerify method test.

// @Test
public void test() {
    String filePath = "/home/{usuario}/arquivo";
    PDDocument document;
    try {
        document = PDDocument.load(new File(filePath));
        List<SignatureInformations> result = null;
        List<SignatureInformations> results = new ArrayList<SignatureInformations>();
        for (PDSignature sig : document.getSignatureDictionaries()) {
            COSDictionary sigDict = sig.getCOSObject();
            COSString contents = (COSString) sigDict.getDictionaryObject(COSName.CONTENTS);
            FileInputStream fis = new FileInputStream(filePath);
            byte[] buf = null;
            try {
                buf = sig.getSignedContent(fis);
            } finally {
                fis.close();
            }
            CAdESChecker checker = new CAdESChecker();
            result = checker.checkDetachedSignature(buf, contents.getBytes());
            if (result == null || result.isEmpty()) {
                assertTrue(false);
            // Erro
            }
            results.addAll(checker.getSignaturesInfo());
        }
        for (SignatureInformations sis : results) {
            for (String valErr : sis.getValidatorErrors()) {
                System.out.println("++++++++++++++ ERROS ++++++++++++++++++");
                System.out.println(valErr);
            }
            for (X509Certificate cert : sis.getChain()) {
                BasicCertificate certificate = new BasicCertificate(cert);
                if (!certificate.isCACertificate()) {
                    System.out.println(certificate.toString());
                }
            }
        /*
				for (BasicCertificate bc : sis.getSignersBasicCertificates()){
					if (bc.hasCertificatePF()){
						System.out.println(bc.getICPBRCertificatePF().getCPF());
					}
					if (bc.hasCertificatePJ()){
						System.out.println(bc.getICPBRCertificatePJ().getCNPJ());
						System.out.println(bc.getICPBRCertificatePJ().getResponsibleCPF());
					}					 
				}*/
        }
        assertTrue(true);
    } catch (IOException e) {
        e.printStackTrace();
        assertTrue(false);
    }
}
Also used : COSDictionary(org.apache.pdfbox.cos.COSDictionary) ArrayList(java.util.ArrayList) COSString(org.apache.pdfbox.cos.COSString) IOException(java.io.IOException) PDSignature(org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature) FileInputStream(java.io.FileInputStream) X509Certificate(java.security.cert.X509Certificate) BasicCertificate(org.demoiselle.signer.core.extension.BasicCertificate) SignatureInformations(org.demoiselle.signer.policy.impl.cades.SignatureInformations) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) File(java.io.File) COSString(org.apache.pdfbox.cos.COSString)

Example 4 with BasicCertificate

use of org.demoiselle.signer.core.extension.BasicCertificate in project signer by demoiselle.

the class OnLineCRLRepository method getX509CRL.

@Override
public Collection<ICPBR_CRL> getX509CRL(X509Certificate certificate) {
    Collection<ICPBR_CRL> list = new ArrayList<ICPBR_CRL>();
    try {
        BasicCertificate cert = new BasicCertificate(certificate);
        List<String> ListaURLCRL = cert.getCRLDistributionPoint();
        if (ListaURLCRL == null || ListaURLCRL.isEmpty()) {
            throw new CRLRepositoryException(coreMessagesBundle.getString("error.invalid.crl"));
        }
        ICPBR_CRL validCrl = null;
        for (String URLCRL : ListaURLCRL) {
            // Achou uma CRL válida
            validCrl = getICPBR_CRL(URLCRL);
            if (validCrl != null) {
                list.add(validCrl);
                logger.info(coreMessagesBundle.getString("info.crl.found", URLCRL));
                break;
            }
        }
        if (validCrl == null) {
            throw new CRLRepositoryException(coreMessagesBundle.getString("error.validate.on.crl", ListaURLCRL));
        }
    } catch (IOException e) {
        throw new CRLRepositoryException(coreMessagesBundle.getString("error.invalid.crl") + e);
    }
    return list;
}
Also used : ICPBR_CRL(org.demoiselle.signer.core.extension.ICPBR_CRL) BasicCertificate(org.demoiselle.signer.core.extension.BasicCertificate) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 5 with BasicCertificate

use of org.demoiselle.signer.core.extension.BasicCertificate in project signer by demoiselle.

the class OffLineCRLRepository method getX509CRL.

/**
 *  Returns a CRL (Certificate Revoked List)  from a given authority of IPC-Brasil.
 */
@Override
public Collection<ICPBR_CRL> getX509CRL(X509Certificate certificate) {
    Collection<ICPBR_CRL> list = new ArrayList<ICPBR_CRL>();
    try {
        BasicCertificate cert = new BasicCertificate(certificate);
        List<String> ListaURLCRL = cert.getCRLDistributionPoint();
        if (ListaURLCRL == null || ListaURLCRL.isEmpty()) {
            throw new CRLRepositoryException(coreMessagesBundle.getString("error.invalid.crl"));
        }
        for (String URLCRL : ListaURLCRL) {
            // Achou uma CRL válida
            ICPBR_CRL crl = getICPBR_CRL(URLCRL);
            if (crl != null) {
                list.add(crl);
                logger.info(coreMessagesBundle.getString("info.crl.found", URLCRL));
                break;
            }
        }
    } catch (IOException e) {
        throw new CRLRepositoryException(coreMessagesBundle.getString("error.invalid.crl") + e);
    }
    return list;
}
Also used : ICPBR_CRL(org.demoiselle.signer.core.extension.ICPBR_CRL) BasicCertificate(org.demoiselle.signer.core.extension.BasicCertificate) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Aggregations

BasicCertificate (org.demoiselle.signer.core.extension.BasicCertificate)6 IOException (java.io.IOException)4 X509Certificate (java.security.cert.X509Certificate)4 SignatureInformations (org.demoiselle.signer.policy.impl.cades.SignatureInformations)4 ArrayList (java.util.ArrayList)3 ICPBR_CRL (org.demoiselle.signer.core.extension.ICPBR_CRL)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 COSDictionary (org.apache.pdfbox.cos.COSDictionary)1 COSString (org.apache.pdfbox.cos.COSString)1 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)1 PDSignature (org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature)1