title: How to bypass the Certificate Expired validating when verifying the digital signature
body: <p>I have a problem when I verify a digital signature (PKCS#7) in Java using an expired public certificate.
  For more detail, it's OK in case of signing data in .NET and then <strong>verify in Java</strong>, but there will be a Certificate Expired error in case of signing data in Java and <strong>verify in Java</strong>. In both of cases, I used the same verify method, the same private key and public certificate.</p>
  
  <p>Please tell me why? And what should I do if I want to bypass the Certificate Expired validating when verifying the digital signature?</p>
  
  <p>Thanks,</p>
  
  <p>Sign method in C#.NET</p>
  
  <pre><code>private static string SignData(byte[] data, string pkcs12FileUrl, string pkcs12Password)
      {
          X509Certificate2 signerCert = new X509Certificate2(pkcs12FileUrl, pkcs12Password);
          ContentInfo content = new ContentInfo(data);
          SignedCms signed = new SignedCms(content, true);
          CmsSigner signer = new CmsSigner(signerCert);
          signer.IncludeOption = X509IncludeOption.None;
          signed.ComputeSignature(signer);
  
          return Convert.ToBase64String(signed.Encode());
      }
  </code></pre>
  
  <p>Sign method in Java</p>
  
  <pre><code>public static String SignData(byte[] data, String pkcs12File, String pkcs12Password)
      throws Exception
  {
          if(Security.getProvider("BC")==null)
          {
              Security.addProvider(new BouncyCastleProvider());
          }
          KeyStore keyStore = getPkcs12Info(pkcs12File, pkcs12Password);
  
          Enumeration&lt;String&gt; aliasesList = keyStore.aliases();
          String aliasName = "";
          while (aliasesList.hasMoreElements())
          {
              aliasName = aliasesList.nextElement().toString();
          }
  
          X509Certificate signerCert = (X509Certificate) keyStore.getCertificate(aliasName);
          PrivateKey privateKey = (PrivateKey) keyStore.getKey(aliasName,pkcs12Password.toCharArray());
  
          CMSSignedDataGenerator Signer = new CMSSignedDataGenerator();
          Signer.addSigner(privateKey, signerCert, CMSSignedDataGenerator.DIGEST_SHA1);
  
          CMSProcessableByteArray digestContent = new CMSProcessableByteArray(data);
  
          CMSSignedData Signed = Signer.generate(digestContent, false, "BC");
          return Base64.encode(Signed.getEncoded());
  
  }
  
  private static  KeyStore getPkcs12Info(String pkcs12File, String pkcs12Password) throws Exception
  {
      //KeyStore keyStore = KeyStore.getInstance("PKCS12", "BC");
          KeyStore keyStore = KeyStore.getInstance("PKCS12");
          keyStore.load(new FileInputStream(pkcs12File), pkcs12Password.toCharArray());
          return keyStore;
  }
  </code></pre>
  
  <p>Verify method in Java</p>
  
  <pre><code>public static boolean VerifySignature(byte[] data, String digitalSignature, InputStream publicCertFile) throws Exception
  {
      if(Security.getProvider("BC")==null)
      {
          Security.addProvider(new BouncyCastleProvider());
      }
      CertificateFactory factory = CertificateFactory.getInstance("X509","BC");
      X509Certificate publicCert = (X509Certificate)factory.generateCertificate(publicCertFile);
      CMSProcessableByteArray digestContent = new CMSProcessableByteArray(data);
      CMSSignedData Signed = new CMSSignedData(digestContent,Base64.decode(digitalSignature));
      SignerInformation Signer=(SignerInformation)Signed.getSignerInfos().getSigners().iterator().next();
      return Signer.verify(publicCert, "BC");
  }
  </code></pre>