Search in sources :

Example 1 with PKCS9Attributes

use of sun.security.pkcs.PKCS9Attributes in project jdk8u_jdk by JetBrains.

the class NonStandardNames method main.

public static void main(String[] args) throws Exception {
    byte[] data = "Hello".getBytes();
    X500Name n = new X500Name("cn=Me");
    CertAndKeyGen cakg = new CertAndKeyGen("RSA", "SHA256withRSA");
    cakg.generate(1024);
    X509Certificate cert = cakg.getSelfCertificate(n, 1000);
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    PKCS9Attributes authed = new PKCS9Attributes(new PKCS9Attribute[] { new PKCS9Attribute(PKCS9Attribute.CONTENT_TYPE_OID, ContentInfo.DATA_OID), new PKCS9Attribute(PKCS9Attribute.MESSAGE_DIGEST_OID, md.digest(data)) });
    Signature s = Signature.getInstance("SHA256withRSA");
    s.initSign(cakg.getPrivateKey());
    s.update(authed.getDerEncoding());
    byte[] sig = s.sign();
    SignerInfo signerInfo = new SignerInfo(n, cert.getSerialNumber(), AlgorithmId.get("SHA-256"), authed, AlgorithmId.get("SHA256withRSA"), sig, null);
    PKCS7 pkcs7 = new PKCS7(new AlgorithmId[] { signerInfo.getDigestAlgorithmId() }, new ContentInfo(data), new X509Certificate[] { cert }, new SignerInfo[] { signerInfo });
    if (pkcs7.verify(signerInfo, data) == null) {
        throw new Exception("Not verified");
    }
}
Also used : SignerInfo(sun.security.pkcs.SignerInfo) PKCS9Attribute(sun.security.pkcs.PKCS9Attribute) ContentInfo(sun.security.pkcs.ContentInfo) PKCS7(sun.security.pkcs.PKCS7) CertAndKeyGen(sun.security.tools.keytool.CertAndKeyGen) Signature(java.security.Signature) X500Name(sun.security.x509.X500Name) MessageDigest(java.security.MessageDigest) PKCS9Attributes(sun.security.pkcs.PKCS9Attributes) X509Certificate(java.security.cert.X509Certificate)

Aggregations

MessageDigest (java.security.MessageDigest)1 Signature (java.security.Signature)1 X509Certificate (java.security.cert.X509Certificate)1 ContentInfo (sun.security.pkcs.ContentInfo)1 PKCS7 (sun.security.pkcs.PKCS7)1 PKCS9Attribute (sun.security.pkcs.PKCS9Attribute)1 PKCS9Attributes (sun.security.pkcs.PKCS9Attributes)1 SignerInfo (sun.security.pkcs.SignerInfo)1 CertAndKeyGen (sun.security.tools.keytool.CertAndKeyGen)1 X500Name (sun.security.x509.X500Name)1