S/MIME verification with x509 certificate

2019-07-26 17:00发布

I have some problems with verifying S/Mime signed message with x509 certificate. This is my 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();
    }
}

}

And i have problem with this exception:

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.

2条回答
霸刀☆藐视天下
2楼-- · 2019-07-26 17:10

I solved this issue.If you are signing data with S/MIME then according S/MIME standard we should have MIME-Version,Content-Type...etc and headers are separated from message to be signed with a blank line.Most of the time when message is of type "text/plain" we will not add headers to the message,which is obvious.But if you are using S/MIME to sign data it will look for S/MIME standard headers and issues like miss mach in digest will occur.

Add following headers to message before signing MIME-Version: 1.0 Content-Type: text/plain;

Or just add one blank line before the message.

Above approach is working fine for me.

查看更多
爷、活的狠高调
3楼-- · 2019-07-26 17:34

I just find out that the problem is with message. I converted byte array to string and then this string into input stream. Now i give to inputstream byte array without conversion and everything is ok :)

查看更多
登录 后发表回答