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.

}<br />

"Unable to access the local file system.\n" +<br />

"This applet should be started with full security permissions.\n" +<br />

"Please accept to trust this applet when the Java Plug-In ask you.");<br />

}<br />

catch (JSException jse) {<br />

jse.printStackTrace();<br />

JOptionPane.showMessageDialog(this,<br />

"Unable to access some of the fields in the\n" +<br />

"HTML form. Please check applet parameters.");<br />

}<br />

catch (Exception e) {<br />

e.printStackTrace();<br />

JOptionPane.showMessageDialog(this, "Unexpected error: "+e.getMessage());<br />

}<br />

/**<br />

* Signs given local file. The certification chain and private key to be used for<br />

* signing are specified by the local user who choose a PFX file and password for<br />

* accessing it.<br />

* @param aFileName the name of the file to be signed.<br />

* @return the digital signature of the given file and the certification chain of<br />

* the certificate used for signing the file, both Base64-encoded or null if the<br />

* signing process is canceled by the user.<br />

* @throws DocumentSignException when a problem arise during the singing process<br />

* (e.g. invalid file format, invalid certificate, invalid password, etc.)<br />

*/<br />

private CertificationChainAndSignatureInBase64 signFile(String aFileName)<br />

throws DocumentSignException {<br />

// Load the file for signing<br />

byte[] documentToSign = null;<br />

try {<br />

documentToSign = readFileInByteArray(aFileName);<br />

} catch (IOException ioex) {<br />

String errorMsg = "Can not read the file for signing " + aFileName + ".";<br />

throw new DocumentSignException(errorMsg, ioex);<br />

}<br />

// Show a dialog for selecting PFX file and password<br />

CertificateFileAndPasswordDialog certFileAndPasswdDlg =<br />

new CertificateFileAndPasswordDialog();<br />

if (certFileAndPasswdDlg.run()) {<br />

// Load the keystore from specified file using the specified password<br />

String keyStoreFileName = certFileAndPasswdDlg.getCertificateFileName();<br />

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

String errorMessage = "It is mandatory to select a certificate " +<br />

"keystore (.PFX or .P12 file)!";<br />

throw new DocumentSignException(errorMessage);<br />

}<br />

String password = certFileAndPasswdDlg.getCertificatePassword();<br />

KeyStore userKeyStore = null;<br />

try {<br />

userKeyStore = loadKeyStoreFromPFXFile(keyStoreFileName, password);<br />

} catch (Exception ex) {<br />

String errorMessage = "Can not read certificate keystore file (" +<br />

keyStoreFileName + ").\nThe file is either not in PKCS#12 format"<br />

+ " (.P12 or .PFX) or is corrupted or the password is invalid.";<br />

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

}<br />

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

PrivateKeyAndCertChain privateKeyAndCertChain = null;<br />

78

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

Saved successfully!

Ooh no, something went wrong!