Validating digital signature based on .p7s files

 

The digital signature is a reality for many software applications. One typical format to store the digital signature is using the .p7s file (https://www.reviversoft.com/en/file-extensions/p7s) which contains information about how has signed a certain file, what algorithm was utilized for signing, and also what is the hash of produced file.

 

In this post we are going to demonstrate an example of how to validate a p7s file signature when we have the original file and the .p7s file. First, we need a library for this purpose. In this example we are going to use the demoiselle (https://github.com/demoiselle) library.

For the example we are going to execute you can download the dependency using maven, based on the example below of pom.xml file:

   <dependency>
                <groupId>br.gov.frameworkdemoiselle</groupId>
                <artifactId>demoiselle-core</artifactId>
                <version>2.5.2</version>
            </dependency>
 
            <dependency>
                <groupId>org.demoiselle.signer</groupId>
                <artifactId>policy-impl-cades</artifactId>
                <version>3.2.7</version>
   </dependency>

 

Once we have the dependency installed we can run the  code for this validation importing the following classes:

import org.demoiselle.signer.policy.impl.cades.SignatureInformations;

import org.demoiselle.signer.policy.impl.cades.pkcs7.impl.CAdESChecker;

 

Then, the method below run the validation. Its interface receives as parameter the byte[] of original data file and the .p7s file. The method checkDetattachedSignature will trigger an exception in case .p7s is not a valid signature for .pdf file. In case the signature is validated, then we can also extract several information from this p7s file, such as the person that has signed the file, When it has happened, and also check the signature HASHs.

      public static boolean validateSignature(byte[] signature,byte[] content ){
       CAdESChecker checker = new CAdESChecker();
       try{
       List<SignatureInformations> signaturesInfo = checker.checkDetattachedSignature(content, signature);
       if(signaturesInfo.size()==0){
           System.out.println("No information about signature...");
       }else{
           SignatureInformations info=signaturesInfo.get(0);
           System.out.println("Signature Date: "+info.getSignDateGMT());
           System.out.println("Signature HASH: "+info.getSignaturePolicy().getSignPolicyHash().getValueUTF8());
           System.out.println("Signature - Signed by: "+info.getSignersBasicCertificates().get(0).getNome());
           if(signaturesInfo.get(0).getValidatorErrors().size()==0){
               System.out.println("Success - no validation error...");
           }else{
               System.out.println("Warnings:");
               for(String error: signaturesInfo.get(0).getValidatorErrors()){
                 //  System.out.println(error);
               }
           }
       }
       }catch(Exception e){
          // e.printStackTrace();
           return false;
       }
       return true;
    }

 

You can download the full example of this code here:

https://github.com/rafaelqg/code/blob/main/P7SValidator.java

You may see a video class about this theme here:



Comments

Popular posts from this blog

Dart/Flutter: exception handling

Dart: implementing Object Oriented Programming (OOP)

Android – SearchView: Adding a Search component direct on your APP menu.