VerifyPKCS7Signature.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */


  20. package org.openspcoop2.utils.security;

  21. import java.security.Security;
  22. import java.security.cert.Certificate;
  23. import java.util.Collection;

  24. import org.bouncycastle.cert.X509CertificateHolder;
  25. import org.bouncycastle.cms.CMSProcessable;
  26. import org.bouncycastle.cms.CMSSignedData;
  27. import org.bouncycastle.cms.SignerInformation;
  28. import org.bouncycastle.cms.jcajce.JcaSimpleSignerInfoVerifierBuilder;
  29. import org.bouncycastle.jce.provider.BouncyCastleProvider;
  30. import org.openspcoop2.utils.UtilsException;
  31. import org.openspcoop2.utils.certificate.KeyStore;

  32. /**
  33.  * VerifySignature
  34.  *
  35.  * @author Poli Andrea (apoli@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */
  39. public class VerifyPKCS7Signature {

  40.     private KeyStore keystore;
  41.     private Certificate certificate;
  42.     private BouncyCastleProvider bouncyCastleProvider;
  43.     private byte[] originalContent;
  44.    
  45.     public VerifyPKCS7Signature(KeyStore keystore) throws UtilsException{
  46.         this.keystore = keystore;
  47.         this.certificate = this.keystore.getCertificate();
  48.     }
  49.     public VerifyPKCS7Signature(KeyStore keystore, String alias) throws UtilsException{
  50.         this.keystore = keystore;
  51.         this.certificate = this.keystore.getCertificate(alias);
  52.        
  53.         this.bouncyCastleProvider = new BouncyCastleProvider();
  54.         Security.addProvider(this.bouncyCastleProvider);
  55.     }
  56.    
  57.     public boolean verify(byte[] signatureData, String algorithm) throws UtilsException{
  58.         try{
  59.             CMSSignedData cmsSignedData = new CMSSignedData(signatureData);
  60.            
  61.             try{
  62.                 Collection<SignerInformation> signers = cmsSignedData.getSignerInfos().getSigners();
  63.                 X509CertificateHolder ch = new X509CertificateHolder(this.certificate.getEncoded());
  64.                 for (SignerInformation si : signers)
  65.                     if (si.getSID().match(ch))
  66.                         if (si.verify(new JcaSimpleSignerInfoVerifierBuilder().setProvider(this.bouncyCastleProvider).build(ch)))
  67.                             return true;
  68.                
  69.                 return false;
  70.             }finally {
  71.                  //Retrieve the json content from pkcs7
  72.                 CMSProcessable signedContent = cmsSignedData.getSignedContent();
  73.                 this.originalContent = (byte[]) signedContent.getContent();
  74.             }
  75.        
  76.         }catch(Exception e){
  77.             throw new UtilsException(e.getMessage(),e);
  78.         }
  79.     }

  80.     public byte[] getOriginalContent() {
  81.         return this.originalContent;
  82.     }
  83. }