crypto object from JavaScript Mozilla defines a special JavaScript object to allow web pages access to certain cryptographic related services. These services are a balance between the functionality web pages need, and the requirement to protect users from malicious web sites. Most of these services are available via the JavaScript window object as window.crypto. For instance, to obtain a ten byte random number using the cryptographic engine, simply call:
var myrandom = window.crypto.random(10);
window.crypto.random() is not yet implemented in Mozilla browsers.
Services are provided to enable: smart card events, generating certificate requests, importing user certs, generating random numbers, logging out of your tokens, and signing text.
In Gecko-based browsers, such as Mozilla and Firefox, websites can make themselves more SmartCard friendly by listening for SmartCard insertion and SmartCard removal events. To enable your document to receive these events, you must first tell the crypto system you are interested. You can do this by setting window.crypto.enableSmartCardEvents to true. This tells the crypto system that you are interested in listening for these events. You can then register event handlers for these events with the document.addEventListener.
Two smart card related events are generated:
Websites which use SSL clientAuth loggin can use the following code to refresh the page on token insertions and removals:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
....
<script language="javascript">
function onSmartCardChange() {
window.location.reload();
}
function register() {
window.crypto.enableSmartCardEvents=true;
document.addEventListener("smartcard-insert",onSmartCardChange,false);
document.addEventListener("smartcard-remove",onSmartCardChange,false);
};
function deregister() {
document.removeEventListener("smartcard-insert",onSmartCardChange,false);
document.removeEventListener("smartcard-remove",onSmartCardChange,false);
};
</script>
</head>
<body .... onload=register() onunload=deregister()>
.....
</body>
</html>
With the above example, your website will automatically reload anytime a SmartCard is inserted or removed. This allows the page to automatically login and logout based on the presence of the SmartCard.
There are several crypto object methods used in generating keys for certificates: generateCRMFRequest(), importUserCertificates(), and popChallengeResponse().
The generateCRMFRequest() function generates a key and creates a CRMF Request object. This object can be passed to a CA using a webform.
The importUserCertificates() function loads certificates into the NSS database or SmartCard if the corresponding key is found there.
The popChallengeResponse() function returns a proof of key posession signed challenge which a CA can use to verify the client has the private key associated with it's claimed public key.
The CA's enrollment page could look something like this:
<html>
<SCRIPT LANGUAGE="JavaScript">
/* the following values could be filled in by the server CGI */
var authenticator = "server_magic";
var keyTransportCert = null;
function validate()
{
with (document.forms[0]) {
var keyTransportCert = null;
// generate keys for nsm.
if (typeof(crypto.version) != "undefined") {
crmfObject = crypto.generateCRMFRequest(
"CN=" + name.value,
password.value,
authenticator,
keyTransportCert,
"setCRMFRequest();",
1024, null, "rsa-dual-use");
}
return false;
}
}
function setCRMFRequest()
{
with (document.forms[0]) {
cert_request.value = crmfObject.request;
submit();
}
}
</script>
<H2> Request a cert </H2>
<form name="ReqForm" onSubmit="return validate();" method="post" action="http://your.ra.site.org'">
<input type=hidden name=cert_request value="">
Name: <input type=text name=name value=""><br>
Password: <input type=password name="password" value="" ><br>
<input type=submit Name="Send" value="Submit">
</form>
</html>
On completion of the request, the CA may submit a page that looks something like this:
<html>
<SCRIPT LANGUAGE="JavaScript">
/* the following values could be filled in by the server CGI */
var nickname= "MyCertNickname";
var cert = "Mkjflakdjfiwjflaksufklasf ...";
var forceBackup = false;
function LoadCertificate()
{
window.crypto.importUserCertificates(nickname, cert, forceBackup)
}
</script>
<H2> Certificate Request Successful </H2>
Hit 'load' to load your certificate
<form name="ReqForm" onSubmit="return LoadCertificate();">
<input type=submit Name="Load" value="Submit">
</form>
</html>
long deletemodule(in DOMString moduleName);
long addmodule(in DOMString moduleName,
in DOMString libraryFullPath,
in long cryptoMechanismFlags,
in long cipherFlags);
Loads or removes a new PKCS #11 module. In the add case, the module will be placed in the NSS secmod.db database and will be loaded automatically on application restart. In the delete case, the module is removed from the NSS secmod.db. This function will issue a user prompt to confirm the operation before the add or delete actually occurs
Parameters
ModuleName Name of the module. LibraryFullPath The filename of the library prepended with its full path. CryptoMechanismFlags A bit vector indicating all cryptographic mechanisms should be turned on by default (see below). CipherFlags A bit vector indicating all SSL or S/MIME cipher functions supported by the module (see below).
Mechanism Flag Definitions
In general, most tokens should not set any of the cipher flags. Setting these flags means you want your token to supply the default implementation for these functions. Normally Mozilla uses its own internal module to supply these functions. These flags override that preference. If you choose to implement these flags, your module must supply the following additional functions for each flag:
PKCS11_MECH_RSA_FLAG = 0x1<<0; PKCS11_MECH_DSA_FLAG = 0x1<<1; PKCS11_MECH_RC2_FLAG = 0x1<<2; PKCS11_MECH_RC4_FLAG = 0x1<<3; PKCS11_MECH_DES_FLAG = 0x1<<4; PKCS11_MECH_DH_FLAG = 0x1<<5; //Diffie-Hellman PKCS11_MECH_SKIPJACK_FLAG = 0x1<<6; //SKIPJACK algorithm as in Fortezza cards PKCS11_MECH_RC5_FLAG = 0x1<<7; PKCS11_MECH_SHA1_FLAG = 0x1<<8; PKCS11_MECH_MD5_FLAG = 0x1<<9; PKCS11_MECH_MD2_FLAG = 0x1<<10; PKCS11_MECH_RANDOM_FLAG = 0x1<<27; //Random number generator PKCS11_PUB_READABLE_CERT_FLAG = 0x1<<28; //Stored certs can be read off the token w/o logging in PKCS11_DISABLE_FLAG = 0x1<<30; //tell Mozilla to disable this slot by default
Cipher Flags
Reserved
Important for CryptoMechanismFlags
0x1<<11, 0x1<<12, ... , 0x1<<26, 0x1<<29, and 0x1<<31 are reserved for internal use in Mozilla. Therefore, these bits should always be set to 0; otherwise, Mozilla might exhibit unpredictable behavior.
Important for CipherFlags
All values are reserved for internal use in Mozilla. Therefore, this flag should always be set to 0; otherwise, Mozilla might exhibit unpredictable behavior.
Example of CryptoMechanismFlags and CipherFlags
pkcs11MechanismFlags = PKCS11_MECH_DSA_FLAG | PKCS11_MECH_SKIPJACK_FLAG | PKCS11_MECH_RANDOM_FLAG; pkcs11CipherFlags = 0;
Return Values
JS_OK_ADD_MODULE = 3 // Successfully added a module
JS_ERR_OTHER = -1 // Errors other than the following
JS_ERR_USER_CANCEL_ACTION = -2 // User abort an action
JS_ERR_INCORRECT_NUM_OF_ARGUMENTS= -3 // Calling a method w/ incorrect # of arguments
JS_ERR_ADD_MODULE = -5 // Error adding a module
JS_ERR_BAD_MODULE_NAME = -6 // The module name is invalid
JS_ERR_ADD_MODULE_DUPLICATE =-10 // The module being installed has the same name as
// one of the modules that has already been installed
Page last modified 18:05, 18 Jun 2008 by Grimholtz