24.12.2014 Views

Download - Svetlin Nakov

Download - Svetlin Nakov

Download - Svetlin Nakov

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

*/<br />

public void init() {<br />

String signButtonCaption = this.getParameter(SIGN_BUTTON_CAPTION_PARAM);<br />

mSignButton = new Button(signButtonCaption);<br />

mSignButton.setLocation(0, 0);<br />

Dimension appletSize = this.getSize();<br />

mSignButton.setSize(appletSize);<br />

mSignButton.addActionListener(new ActionListener(){<br />

public void actionPerformed(ActionEvent e) {<br />

signSelectedFile();<br />

}<br />

});<br />

this.setLayout(null);<br />

this.add(mSignButton);<br />

}<br />

/**<br />

* Signs the selected file. The file name comes from a field in the HTML<br />

* document. The result consists of the calculated digital signature and<br />

* certification chain, both placed in fields in the HTML document, encoded<br />

* in Base64 format. The HTML document should contain only one HTML form.<br />

* The name of the field, that contains the name of the file to be signed is<br />

* obtained from FILE_NAME_FIELD_PARAM applet parameter. The names of the<br />

* output fields for the signature and the certification chain are obtained<br />

* from the parameters CERT_CHAIN_FIELD_PARAM and SIGNATURE_FIELD_PARAM.<br />

* The applet extracts the certificate, its chain and its private key from<br />

* a PFX file. The user is asked to select this PFX file and the password<br />

* for accessing it.<br />

*/<br />

private void signSelectedFile() {<br />

try {<br />

// Get the file name to be signed from the form in the HTML document<br />

JSObject browserWindow = JSObject.getWindow(this);<br />

JSObject mainForm = (JSObject) browserWindow.eval("document.forms[0]");<br />

String fileNameFieldName = this.getParameter(FILE_NAME_FIELD_PARAM);<br />

JSObject fileNameField =<br />

(JSObject) mainForm.getMember(fileNameFieldName);<br />

String fileName = (String) fileNameField.getMember("value");<br />

// Perform file signing<br />

CertificationChainAndSignatureInBase64 signingResult=signFile(fileName);<br />

if (signingResult != null) {<br />

// Document signed. Fill the certificate and signature fields<br />

String certChainFieldName=this.getParameter(CERT_CHAIN_FIELD_PARAM);<br />

JSObject certChainField =<br />

(JSObject) mainForm.getMember(certChainFieldName);<br />

certChainField.setMember("value", signingResult.mCertChain);<br />

String signatureFieldName = this.getParameter(SIGNATURE_FIELD_PARAM);<br />

JSObject signatureField =<br />

(JSObject) mainForm.getMember(signatureFieldName);<br />

signatureField.setMember("value", signingResult.mSignature);<br />

} else {<br />

// User canceled signing<br />

}<br />

}<br />

catch (DocumentSignException dse) {<br />

// Document signing failed. Display error message<br />

String errorMessage = dse.getMessage();<br />

JOptionPane.showMessageDialog(this, errorMessage);<br />

}<br />

catch (SecurityException se) {<br />

se.printStackTrace();<br />

JOptionPane.showMessageDialog(this,<br />

77

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

Saved successfully!

Ooh no, something went wrong!