edGen.addKeyTransRecipient(cacert);
CMSEnvelopedData ed = edGen.generate(envThis, SMIMECapability.dES_CBC.getId(), "BC");
return ed;
}
private CMSSignedData sign(CMSProcessable signThis, String messageType, String transactionId) throws NoSuchAlgorithmException, NoSuchProviderException, CMSException, IOException, InvalidAlgorithmParameterException, CertStoreException {
CMSSignedDataGenerator gen1 = new CMSSignedDataGenerator();
// add authenticated attributes...status, transactionId, sender- and more...
Hashtable attributes = new Hashtable();
DERObjectIdentifier oid;
Attribute attr;
DERSet value;
// Message type (certreq)
oid = new DERObjectIdentifier(ScepRequestMessage.id_messageType);
value = new DERSet(new DERPrintableString(messageType));
attr = new Attribute(oid, value);
attributes.put(attr.getAttrType(), attr);
// TransactionId
oid = new DERObjectIdentifier(ScepRequestMessage.id_transId);
value = new DERSet(new DERPrintableString(transactionId));
attr = new Attribute(oid, value);
attributes.put(attr.getAttrType(), attr);
// senderNonce
byte[] nonce = new byte[16];
randomSource.nextBytes(nonce);
senderNonce = new String(Base64.encode(nonce));
if (nonce != null) {
oid = new DERObjectIdentifier(ScepRequestMessage.id_senderNonce);
log.debug("Added senderNonce: " + senderNonce);
value = new DERSet(new DEROctetString(nonce));
attr = new Attribute(oid, value);
attributes.put(attr.getAttrType(), attr);
}
// Add our signer info and sign the message
ArrayList certList = new ArrayList();
certList.add(cert);
CertStore certs = CertStore.getInstance("Collection", new CollectionCertStoreParameters(certList), "BC");
gen1.addCertificatesAndCRLs(certs);
gen1.addSigner(keys.getPrivate(), cert, digestOid,
new AttributeTable(attributes), null);
// The signed data to be enveloped
CMSSignedData s = gen1.generate(signThis, true, "BC");
return s;
}