Download - Svetlin Nakov
Download - Svetlin Nakov
Download - Svetlin Nakov
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
try {<br />
privateKeyAndCertChain =<br />
getPrivateKeyAndCertChain(userKeyStore, password);<br />
} catch (GeneralSecurityException gsex) {<br />
String errorMessage = "Can not extract certification chain and " +<br />
"corresponding private key from the specified keystore file " +<br />
"with given password. Probably the password is incorrect.";<br />
throw new DocumentSignException(errorMessage, gsex);<br />
}<br />
// Check if a private key is available in the keystore<br />
PrivateKey privateKey = privateKeyAndCertChain.mPrivateKey;<br />
if (privateKey == null) {<br />
String errorMessage = "Can not find the private key in the " +<br />
"specified file " + keyStoreFileName + ".";<br />
throw new DocumentSignException(errorMessage);<br />
}<br />
// Check if X.509 certification chain is available<br />
Certificate[] certChain =<br />
privateKeyAndCertChain.mCertificationChain;<br />
if (certChain == null) {<br />
String errorMessage = "Can not find neither certificate nor " +<br />
"certification chain in the file " + keyStoreFileName + ".";<br />
throw new DocumentSignException(errorMessage);<br />
}<br />
// Create the result object<br />
CertificationChainAndSignatureInBase64 signingResult =<br />
new CertificationChainAndSignatureInBase64();<br />
// Save X.509 certification chain in the result encoded in Base64<br />
try {<br />
signingResult.mCertChain = encodeX509CertChainToBase64(certChain);<br />
}<br />
catch (CertificateException cee) {<br />
String errorMessage = "Invalid certification chain found in the " +<br />
"file " + keyStoreFileName + ".";<br />
throw new DocumentSignException(errorMessage);<br />
}<br />
// Calculate the digital signature of the file,<br />
// encode it in Base64 and save it in the result<br />
try {<br />
byte[] signature = signDocument(documentToSign, privateKey);<br />
signingResult.mSignature = Base64Utils.base64Encode(signature);<br />
} catch (GeneralSecurityException gsex) {<br />
String errorMessage = "Error signing file " + aFileName + ".";<br />
throw new DocumentSignException(errorMessage, gsex);<br />
}<br />
}<br />
// Document signing completed succesfully<br />
return signingResult;<br />
}<br />
else {<br />
// Document signing canceled by the user<br />
return null;<br />
}<br />
/**<br />
* Loads a keystore from .PFX or .P12 file (file format should be PKCS#12)<br />
* using given keystore password.<br />
*/<br />
79