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.

* All rights reserved. This code is freeware. It can be used<br />

* for any purpose as long as this copyright statement is not<br />

* removed or modified.<br />

*/<br />

public class SmartCardSignerApplet extends Applet {<br />

private static final String FILE_NAME_FIELD_PARAM = "fileNameField";<br />

private static final String CERT_CHAIN_FIELD_PARAM = "certificationChainField";<br />

private static final String SIGNATURE_FIELD_PARAM = "signatureField";<br />

private static final String SIGN_BUTTON_CAPTION_PARAM = "signButtonCaption";<br />

private static final String PKCS11_KEYSTORE_TYPE = "PKCS11";<br />

private static final String X509_CERTIFICATE_TYPE = "X.509";<br />

private static final String CERTIFICATION_CHAIN_ENCODING = "PkiPath";<br />

private static final String DIGITAL_SIGNATURE_ALGORITHM_NAME = "SHA1withRSA";<br />

private static final String SUN_PKCS11_PROVIDER_CLASS =<br />

"sun.security.pkcs11.SunPKCS11";<br />

private Button mSignButton;<br />

/**<br />

* Initializes the applet - creates and initializes its graphical user interface.<br />

* Actually the applet consists of a single button, that fills its all surface.<br />

* The button's caption is taken from the applet param SIGN_BUTTON_CAPTION_PARAM.<br />

*/<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<br />

* is 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. The<br />

* user is asket to choose a PKCS#11 implementation library and a PIN code<br />

* for accessing the smart card.<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 the actual file signing<br />

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

96

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

Saved successfully!

Ooh no, something went wrong!