title: S/MIME verification with x509 certificate
body: <p>I have some problems with verifying S/Mime signed message with x509 certificate. This is my code:</p>
  
  <pre><code>public class verifyMsg {
  
  private static void verify(SMIMESignedParser s) throws Exception {
  
      Security.addProvider(new BouncyCastleProvider());
      System.out.println("wbilem");
      CertStore certs = s.getCertificatesAndCRLs("Collection", "BC");
  
      SignerInformationStore signers = s.getSignerInfos();
      Collection c = signers.getSigners();
      Iterator it = c.iterator();
  
      while (it.hasNext()) {
          File f = new File("signature.crt");
          FileInputStream fis = new FileInputStream(f);
          DataInputStream dis = new DataInputStream(fis);
          byte[] keyBytes = new byte[(int) f.length()];
          dis.readFully(keyBytes);
          dis.close();
          fis.close();
          SignerInformation signer = (SignerInformation) it.next();
          Collection certCollection = certs.getCertificates(signer.getSID());
          Iterator certIt = certCollection.iterator();
          FileInputStream fr = new FileInputStream("signature.crt");
          CertificateFactory cf = CertificateFactory.getInstance("X509");
          X509Certificate cert = (X509Certificate) cf.generateCertificate(fr);
  
  
          if (signer.verify(cert, "BC")) { //problem is there...
              System.out.println("signature verified");
          } else {
              System.out.println("signature failed!");
          }
  
      }
  }
  
  public static void main(String[] args) throws Exception {
      Properties props = System.getProperties();
      Session session = Session.getDefaultInstance(props, null);
  
      try {
          FileInputStream fr = new FileInputStream("signature.crt");
          CertificateFactory cf = CertificateFactory.getInstance("X509");
          X509Certificate c = (X509Certificate) cf.generateCertificate(fr);
          System.out.println("Read in the following certificate:");
          System.out.println("\tCertificate for: " + c.getSubjectDN());
          System.out.println("\tCertificate issued by: " + c.getIssuerDN());
          System.out.println("\tThe certificate is valid from " + c.getNotBefore() + " to " + c.getNotAfter());
          System.out.println("\tCertificate SN# " + c.getSerialNumber());
          System.out.println("\tGenerated with " + c.getSigAlgName());
          System.out.println(c.getPublicKey());
      } catch (Exception e) {
          e.printStackTrace();
      }
  
      try {
          MimeMessage msg = new MimeMessage(session, new SharedFileInputStream("G:\\MIME.txt"));
          if (msg.isMimeType("multipart/signed")) {
  
              SMIMESignedParser s = new SMIMESignedParser((MimeMultipart) msg.getContent());
              System.out.println("Status:");
              verify(s);
          } else if (msg.isMimeType("application/pkcs7-mime")) {
  
              // in this case the content is wrapped in the signature block.
              //
              SMIMESignedParser s = new SMIMESignedParser(msg);
              System.out.println("Status1:");
              verify(s);
          } else {
              System.err.println("Not a signed message!");
          }
  
      } catch (MessagingException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (CMSException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And i have problem with this exception:</p>
  
  <p>CMSSignerDigestMismatchException: message-digest attribute value does not match calculated value. I don't know what am i doing wrong. I use jdk 1.4.2.</p>