// load existing certificates
try {
loadPrivateKeys();
loadCertificates(true, true);
} catch (IOException e) {
throw new CMSMessageSignatureServiceException(
CMSMessageSignatureServiceException.SE_FAILURE);
}
// find certificate chains
Vector chains = getChains(nonRepudiation, caNames);
if (chains.size() == 0) {
throw new CMSMessageSignatureServiceException(
CMSMessageSignatureServiceException.CRYPTO_NO_CERTIFICATE);
}
// select certificate
Vector chain = selectChain(chains);
if (chain == null) {
return null;
}
Certificate cert = (Certificate) chain.elementAt(0);
PrivateKey key = getPrivateKey(cert.id);
PINAttributes pin = getPIN(key.authId);
int pinStatus = checkPIN(pin);
if (pinStatus == PIN_BLOCKED) {
throw new SecurityException();
}
if (pinStatus == PIN_CANCELLED) {
return null;
}
TLV signedAttrs = new TLV(TLV.SET_TYPE);
TLV t = signedAttrs.setChild(TLV.createSequence());
t.setChild(TLV.createOID("1.2.840.113549.1.9.3")). // ContentType
setNext(new TLV(TLV.SET_TYPE)).
setChild(TLV.createOID("1.2.840.113549.1.7.1")); // data
t.next = TLV.createSequence();
t = t.next;
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
t.setChild(TLV.createOID("1.2.840.113549.1.9.5")). // signingTime
setNext(new TLV(TLV.SET_TYPE)).
setChild(TLV.createUTCTime(calendar));
t.next = TLV.createSequence();
t = t.next;
t.setChild(TLV.createOID("1.2.840.113549.1.9.4")). // messageDigest
setNext(new TLV(TLV.SET_TYPE)).
setChild(TLV.createOctetString(Utils.getHash(data, 0, data.length)));
// generate signature
byte[] signature;
try {
signature = signData(key, signedAttrs.getDERData());
} catch (IOException e) {
throw new CMSMessageSignatureServiceException(
CMSMessageSignatureServiceException.CRYPTO_FAILURE);
}
// format the signature
/*