Search in sources :

Example 1 with CMSSignedDataGenerator

use of org.openecard.bouncycastle.cms.CMSSignedDataGenerator in project open-ecard by ecsec.

the class SignatureTest method createSignature.

private CMSSignedData createSignature(String alias, byte[] challenge) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException, InvalidKeyException, SignatureException, OperatorCreationException, CertificateEncodingException, CMSException {
    PrivateKey privKey = (PrivateKey) signStore.getKey(alias, pass.toCharArray());
    X509Certificate cert = (X509Certificate) signStore.getCertificate(alias);
    Certificate[] certChain = (Certificate[]) signStore.getCertificateChain(alias);
    Store certs = new JcaCertStore(Arrays.asList(certChain));
    // Signature signature = Signature.getInstance("SHA256WithRSA");
    // signature.initSign(privKey);
    // signature.update(challenge);
    // byte[] signedBytes = signature.sign();
    CMSTypedData msg = new CMSProcessableByteArray(challenge);
    CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
    ContentSigner signer = new JcaContentSignerBuilder("SHA256withRSA").build(privKey);
    DigestCalculatorProvider dgProv = new JcaDigestCalculatorProviderBuilder().build();
    gen.addSignerInfoGenerator(new JcaSignerInfoGeneratorBuilder(dgProv).build(signer, cert));
    gen.addCertificates(certs);
    CMSSignedData sigData = gen.generate(msg, false);
    return sigData;
}
Also used : CMSSignedDataGenerator(org.openecard.bouncycastle.cms.CMSSignedDataGenerator) CMSProcessableByteArray(org.openecard.bouncycastle.cms.CMSProcessableByteArray) PrivateKey(java.security.PrivateKey) CMSTypedData(org.openecard.bouncycastle.cms.CMSTypedData) JcaContentSignerBuilder(org.openecard.bouncycastle.operator.jcajce.JcaContentSignerBuilder) ContentSigner(org.openecard.bouncycastle.operator.ContentSigner) KeyStore(java.security.KeyStore) Store(org.openecard.bouncycastle.util.Store) JcaCertStore(org.openecard.bouncycastle.cert.jcajce.JcaCertStore) JcaCertStore(org.openecard.bouncycastle.cert.jcajce.JcaCertStore) CMSSignedData(org.openecard.bouncycastle.cms.CMSSignedData) X509Certificate(java.security.cert.X509Certificate) JcaSignerInfoGeneratorBuilder(org.openecard.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder) DigestCalculatorProvider(org.openecard.bouncycastle.operator.DigestCalculatorProvider) JcaDigestCalculatorProviderBuilder(org.openecard.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Aggregations

KeyStore (java.security.KeyStore)1 PrivateKey (java.security.PrivateKey)1 Certificate (java.security.cert.Certificate)1 X509Certificate (java.security.cert.X509Certificate)1 JcaCertStore (org.openecard.bouncycastle.cert.jcajce.JcaCertStore)1 CMSProcessableByteArray (org.openecard.bouncycastle.cms.CMSProcessableByteArray)1 CMSSignedData (org.openecard.bouncycastle.cms.CMSSignedData)1 CMSSignedDataGenerator (org.openecard.bouncycastle.cms.CMSSignedDataGenerator)1 CMSTypedData (org.openecard.bouncycastle.cms.CMSTypedData)1 JcaSignerInfoGeneratorBuilder (org.openecard.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder)1 ContentSigner (org.openecard.bouncycastle.operator.ContentSigner)1 DigestCalculatorProvider (org.openecard.bouncycastle.operator.DigestCalculatorProvider)1 JcaContentSignerBuilder (org.openecard.bouncycastle.operator.jcajce.JcaContentSignerBuilder)1 JcaDigestCalculatorProviderBuilder (org.openecard.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder)1 Store (org.openecard.bouncycastle.util.Store)1