Java Applet JavaScript Communication

There is a Java Applet which digitally signs files after taking

private key from a smart card.This applet takes external files.

I want to do this "Generate digital signature of a web page with the

help of an applet of which the applet is itself a part"

I am trying using Java Applet to Javascript Communication.

I am able to access the object which represents the HTML document.

That abject is represented by document itself.

Now I have to somehow generate the whole webpage i.e. like we view in a

notepad like this:

<HTML>

<Title>my page</title>

Then write into a file and then that file can be signed.

This is what I feel.

In C#.net there is a method .innerhtml.

Does such kind of a method exists in Java.?

I m searching but cudn't get till now.

Will serialization work?

[894 byte] By [My_Queries_Javaa] at [2007-10-3 0:33:31]
# 1

in your applet

this.getAppletContext().showDocument(

new URL("javascript: JSFunction();"));

.....

public void getValueFromJScript(Object value){

String innerHTML = (String) value;

.....

}

your html page

function JSFunction(){

document.getElementById('idOfTheApplet').getValueFromJScript(document.body.innerHTML);

}

harmmeijera at 2007-7-14 17:27:06 > top of Java-index,Security,Signed Applets...
# 2

I tried by adding ur snippet of code but it doesn't work.

JSObject doc = (JSObject)browserWindow.getMember("document");

Here doc represents the object of the HTML document. Somehow from this object the HTML code has to be extracted and written into a file for signing.

CertificationChainAndSignatureBase64 signingResult = signFile(fileName);

this is the line which takes fileName as a parameter.

This is a small part of the whole applet . Just see first 10-30 lines..U will get the idea..

Kindly help me where and how this code should be added so that I can proceed in my work.

I will be highly grateful...

private void signSelectedFile() {

try {

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

JSObject browserWindow = JSObject.getWindow(this);

JSObject mainForm = (JSObject) browserWindow.eval("document.forms[0]");

String fileNameFieldName = this.getParameter(FILE_NAME_FIELD_PARAM);

JSObject fileNameField = (JSObject) mainForm.getMember(fileNameFieldName);

String fileName = (String) fileNameField.getMember("value");

//here we get the document......

JSObject doc = (JSObject)browserWindow.getMember("document");

// String docum = doc.toString();

// Perform the actual file signing

CertificationChainAndSignatureBase64 signingResult = signFile(fileName);

if (signingResult != null) {

// Document signed. Fill the certificate and signature fields

String certChainFieldName = this.getParameter(CERT_CHAIN_FIELD_PARAM);

JSObject certChainField = (JSObject) mainForm.getMember(certChainFieldName);

certChainField.setMember("value", signingResult.mCertificationChain);

String signatureFieldName = this.getParameter(SIGNATURE_FIELD_PARAM);

JSObject signatureField = (JSObject) mainForm.getMember(signatureFieldName);

signatureField.setMember("value", signingResult.mSignature);

} else {

// User canceled signing

}

}

catch (DocumentSignException dse) {

// Document signing failed. Display error message

String errorMessage = dse.getMessage();

JOptionPane.showMessageDialog(this, errorMessage);

}

catch (SecurityException se) {

se.printStackTrace();

JOptionPane.showMessageDialog(this,

"Unable to access the local file system.\n" +

"This applet should be started with full security permissions.\n" +

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

}

catch (JSException jse) {

jse.printStackTrace();

JOptionPane.showMessageDialog(this,

"Unable to access some of the fields of the\n" +

"HTML form. Please check the applet parameters.");

}

catch (Exception e) {

e.printStackTrace();

JOptionPane.showMessageDialog(this, "Unexpected error: " + e.getMessage());

}

}

/**

* Signs given local file. The certificate and private key to be used for signing

* come from the locally attached smart card. The user is requested to provide a

* PKCS#11 implementation library and the PIN code for accessing the smart card.

* @param aFileName the name of the file to be signed.

* @return the digital signature of the given file and the certification chain of

* the certificatie used for signing the file, both Base64-encoded or null if the

* signing process is canceled by the user.

* @throws DocumentSignException when a problem arised during the singing process

* (e.g. smart card access problem, invalid certificate, invalid PIN code, etc.)

*/

private CertificationChainAndSignatureBase64 signFile(String aFileName)

throws DocumentSignException {

// Load the file for signing

byte[] documentToSign = null;

try {

documentToSign = readFileInByteArray(aFileName);

} catch (IOException ioex) {

String errorMessage = "Can not read the file for signing " + aFileName + ".";

throw new DocumentSignException(errorMessage, ioex);

}

// Show a dialog for choosing PKCS#11 implementation library and smart card PIN

PKCS11LibraryFileAndPINCodeDialog pkcs11Dialog =

new PKCS11LibraryFileAndPINCodeDialog();

boolean dialogConfirmed;

try {

dialogConfirmed = pkcs11Dialog.run();

} finally {

pkcs11Dialog.dispose();

}

if (dialogConfirmed) {

String oldButtonLabel = mSignButton.getLabel();

mSignButton.setLabel("Working...");

mSignButton.setEnabled(false);

try {

String pkcs11LibraryFileName = pkcs11Dialog.getLibraryFileName();

String pinCode = pkcs11Dialog.getSmartCardPINCode();

// Do the actual signing of the document with the smart card

CertificationChainAndSignatureBase64 signingResult =

signDocument(documentToSign, pkcs11LibraryFileName, pinCode);

return signingResult;

} finally {

mSignButton.setLabel(oldButtonLabel);

mSignButton.setEnabled(true);

}

}

else {

return null;

}

}

My_Queries_Javaa at 2007-7-14 17:27:06 > top of Java-index,Security,Signed Applets...
# 3
Also I am not exactly able to figure out what's this 'idOfApplet'.
My_Queries_Javaa at 2007-7-14 17:27:06 > top of Java-index,Security,Signed Applets...
# 4

There were some bugs with JSObject a while ago, maybe you should not use it.

Id of the applet is the ID you gave the html tag.

<TAGTYPE id='idOfThisTag' ....

your html page

[script]

.....

function JSFunction(){

document.getElementById('idOfTheApplet').getValueFromJScript(document.body.innerHTML);

}

></script>

<OBJECT id='idOfTheApplet' .....

[/script]>

harmmeijera at 2007-7-14 17:27:06 > top of Java-index,Security,Signed Applets...