24.12.2014 Views

Download - Svetlin Nakov

Download - Svetlin Nakov

Download - Svetlin Nakov

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

new PKCS11LibraryFileAndPINCodeDialog();<br />

boolean dialogConfirmed;<br />

try {<br />

dialogConfirmed = pkcs11Dialog.run();<br />

} finally {<br />

pkcs11Dialog.dispose();<br />

}<br />

if (dialogConfirmed) {<br />

String oldButtonLabel = mSignButton.getLabel();<br />

mSignButton.setLabel("Working...");<br />

mSignButton.setEnabled(false);<br />

try {<br />

String pkcs11LibraryFileName = pkcs11Dialog.getLibraryFileName();<br />

String pinCode = pkcs11Dialog.getSmartCardPINCode();<br />

}<br />

// Do the actual signing of the document with the smart card<br />

CertificationChainAndSignatureBase64 signingResult =<br />

signDocument(documentToSign, pkcs11LibraryFileName, pinCode);<br />

return signingResult;<br />

} finally {<br />

mSignButton.setLabel(oldButtonLabel);<br />

mSignButton.setEnabled(true);<br />

}<br />

}<br />

else {<br />

return null;<br />

}<br />

private CertificationChainAndSignatureBase64 signDocument(<br />

byte[] aDocumentToSign, String aPkcs11LibraryFileName, String aPinCode)<br />

throws DocumentSignException {<br />

if (aPkcs11LibraryFileName.length() == 0) {<br />

String errorMessage = "It is mandatory to choose a PCKS#11 native " +<br />

"implementation library for for smart card (.dll or .so file)!";<br />

throw new DocumentSignException(errorMessage);<br />

}<br />

// Load the keystore from the smart card using the specified PIN code<br />

KeyStore userKeyStore = null;<br />

try {<br />

userKeyStore = loadKeyStoreFromSmartCard(<br />

aPkcs11LibraryFileName, aPinCode);<br />

} catch (Exception ex) {<br />

String errorMessage = "Can not read the keystore from the smart card." +<br />

"\nPossible reasons:\n" +<br />

" - The smart card reader in not connected.\n" +<br />

" - The smart card is not inserted.\n" +<br />

" - The PKCS#11 implementation library is invalid.\n" +<br />

" - The PIN for the smart card is incorrect.\n" +<br />

"Problem details: " + ex.getMessage();<br />

throw new DocumentSignException(errorMessage, ex);<br />

}<br />

// Get the private key and its certification chain from the keystore<br />

PrivateKeyAndCertChain privateKeyAndCertChain = null;<br />

try {<br />

privateKeyAndCertChain =<br />

getPrivateKeyAndCertChain(userKeyStore);<br />

} catch (GeneralSecurityException gsex) {<br />

String errorMessage = "Can not extract the private key and " +<br />

"certificate from the smart card. Reason: " + gsex.getMessage();<br />

throw new DocumentSignException(errorMessage, gsex);<br />

98

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!