KeyStore keyStore = null;
Key privateKey;
CMSSignedDataGenerator generator;
CMSProcessable processableData;
CMSSignedData signedContainer;
MethodResult loadingKeyStoreResult;
MethodResult signingDataResult;
byte[] signedData = null;
int resultValue = UNKNOWN_ERROR;
Throwable th = null;
loadingKeyStoreResult = loadKeyStore(keyStorePath, keyStorePassword);
// Log.d(TAG, "keystore: " + loadingKeyStoreResult.getResultObject());
if (loadingKeyStoreResult.getResultValue() == OPERATION_SUCCESSFUL) {
try {
keyStore = (KeyStore) loadingKeyStoreResult.getResultObject();
privateKey = keyStore.getKey(keyAlias, privateKeyPassword);
generator = new CMSSignedDataGenerator();
generator.addSigner((PrivateKey) privateKey,
(X509Certificate) keyStore.getCertificate(keyAlias),
digestOID);
List<X509Certificate> certList = new ArrayList<X509Certificate>();
certList.add((X509Certificate) keyStore.getCertificate(keyAlias));
CertStoreParameters params = new CollectionCertStoreParameters(certList);
CertStore cs = CertStore.getInstance("Collection", params, "BC");
generator.addCertificatesAndCRLs(cs);
processableData = new CMSProcessableByteArray(data2beSigned);
signedContainer = generator.generate(processableData, true, "BC");
signedData = signedContainer.getEncoded();
resultValue = OPERATION_SUCCESSFUL;
} catch (UnrecoverableKeyException uke) {
uke.printStackTrace();
resultValue = PRIVATE_KEY_CORRUPTED;
th = uke;
} catch (KeyStoreException kse) {
kse.printStackTrace();
resultValue = KEYSTORE_CORRUPTED;
th = kse;
} catch (NoSuchAlgorithmException nsae) {
nsae.printStackTrace();
resultValue = ALGORITHM_NOT_FOUND;
th = nsae;
} catch (InvalidAlgorithmParameterException iape) {
iape.printStackTrace();
resultValue = ALGORITHM_NOT_FOUND;
th = iape;
} catch (NoSuchProviderException nspe) {
nspe.printStackTrace();
resultValue = CRYPTO_PROVIDER_NOT_FOUND;
th = nspe;
} catch (CertStoreException cse) {
cse.printStackTrace();
resultValue = CERTIFICATE_CORRUPTED;
th = cse;
} catch (CMSException cmse) {
cmse.printStackTrace();
resultValue = MESSAGE_CORRUPTED;
th = cmse;
} catch (IOException ioe) {
ioe.printStackTrace();
resultValue = IO_ERROR;
th = ioe;
} catch (Throwable t) {
t.printStackTrace();
resultValue = UNKNOWN_ERROR;
th = t;
}
}
else { // loading of keystore was not successful...
resultValue = loadingKeyStoreResult.getResultValue();
th = loadingKeyStoreResult.getT();
}
signingDataResult = new MethodResult(resultValue, signedData, th);
return signingDataResult;
}