2012-07-04 01:53:37 +04:00
|
|
|
/* -*- Mode: js2; js2-basic-offset: 2; indent-tabs-mode: nil; -*- */
|
|
|
|
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
|
|
const Cu = Components.utils;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Cr = Components.results;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2013-03-27 21:01:46 +04:00
|
|
|
Cu.import("resource://gre/modules/identity/LogUtils.jsm");
|
2012-07-04 01:53:37 +04:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this,
|
|
|
|
"IdentityCryptoService",
|
|
|
|
"@mozilla.org/identity/crypto-service;1",
|
|
|
|
"nsIIdentityCryptoService");
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.EXPORTED_SYMBOLS = ["jwcrypto"];
|
2012-07-04 01:53:37 +04:00
|
|
|
|
|
|
|
const ALGORITHMS = { RS256: "RS256", DS160: "DS160" };
|
2014-01-24 06:04:38 +04:00
|
|
|
const DURATION_MS = 1000 * 60 * 2; // 2 minutes default assertion lifetime
|
2012-07-04 01:53:37 +04:00
|
|
|
|
2013-03-27 21:01:46 +04:00
|
|
|
function log(...aMessageArgs) {
|
|
|
|
Logger.log.apply(Logger, ["jwcrypto"].concat(aMessageArgs));
|
|
|
|
}
|
|
|
|
|
2012-07-04 01:53:37 +04:00
|
|
|
function generateKeyPair(aAlgorithmName, aCallback) {
|
2013-03-27 21:01:46 +04:00
|
|
|
log("Generate key pair; alg =", aAlgorithmName);
|
2012-07-04 01:53:37 +04:00
|
|
|
|
|
|
|
IdentityCryptoService.generateKeyPair(aAlgorithmName, function(rv, aKeyPair) {
|
|
|
|
if (!Components.isSuccessCode(rv)) {
|
|
|
|
return aCallback("key generation failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
var publicKey;
|
|
|
|
|
|
|
|
switch (aKeyPair.keyType) {
|
|
|
|
case ALGORITHMS.RS256:
|
|
|
|
publicKey = {
|
|
|
|
algorithm: "RS",
|
|
|
|
exponent: aKeyPair.hexRSAPublicKeyExponent,
|
|
|
|
modulus: aKeyPair.hexRSAPublicKeyModulus
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ALGORITHMS.DS160:
|
|
|
|
publicKey = {
|
|
|
|
algorithm: "DS",
|
|
|
|
y: aKeyPair.hexDSAPublicValue,
|
|
|
|
p: aKeyPair.hexDSAPrime,
|
|
|
|
q: aKeyPair.hexDSASubPrime,
|
|
|
|
g: aKeyPair.hexDSAGenerator
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return aCallback("unknown key type");
|
|
|
|
}
|
|
|
|
|
|
|
|
let keyWrapper = {
|
|
|
|
serializedPublicKey: JSON.stringify(publicKey),
|
|
|
|
_kp: aKeyPair
|
|
|
|
};
|
|
|
|
|
|
|
|
return aCallback(null, keyWrapper);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function sign(aPayload, aKeypair, aCallback) {
|
|
|
|
aKeypair._kp.sign(aPayload, function(rv, signature) {
|
|
|
|
if (!Components.isSuccessCode(rv)) {
|
2013-03-27 21:01:46 +04:00
|
|
|
log("ERROR: signer.sign failed");
|
2012-07-04 01:53:37 +04:00
|
|
|
return aCallback("Sign failed");
|
|
|
|
}
|
2013-03-27 21:01:46 +04:00
|
|
|
log("signer.sign: success");
|
2012-07-04 01:53:37 +04:00
|
|
|
return aCallback(null, signature);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function jwcryptoClass()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
jwcryptoClass.prototype = {
|
2014-01-24 06:04:38 +04:00
|
|
|
/*
|
|
|
|
* Determine the expiration of the assertion. Returns expiry date
|
|
|
|
* in milliseconds as integer.
|
|
|
|
*
|
|
|
|
* @param localtimeOffsetMsec (optional)
|
|
|
|
* The number of milliseconds that must be added to the local clock
|
|
|
|
* for it to agree with the server. For example, if the local clock
|
|
|
|
* if two minutes fast, localtimeOffsetMsec would be -120000
|
|
|
|
*
|
|
|
|
* @param now (options)
|
|
|
|
* Current date in milliseconds. Useful for mocking clock
|
|
|
|
* skew in testing.
|
|
|
|
*/
|
|
|
|
getExpiration: function(duration=DURATION_MS, localtimeOffsetMsec=0, now=Date.now()) {
|
|
|
|
return now + localtimeOffsetMsec + duration;
|
|
|
|
},
|
|
|
|
|
2012-07-04 01:53:37 +04:00
|
|
|
isCertValid: function(aCert, aCallback) {
|
|
|
|
// XXX check expiration, bug 769850
|
|
|
|
aCallback(true);
|
|
|
|
},
|
|
|
|
|
|
|
|
generateKeyPair: function(aAlgorithmName, aCallback) {
|
2013-03-27 21:01:46 +04:00
|
|
|
log("generating");
|
2012-07-04 01:53:37 +04:00
|
|
|
generateKeyPair(aAlgorithmName, aCallback);
|
|
|
|
},
|
|
|
|
|
2014-01-24 06:04:38 +04:00
|
|
|
/*
|
|
|
|
* Generate an assertion and return it through the provided callback.
|
|
|
|
*
|
|
|
|
* @param aCert
|
|
|
|
* Identity certificate
|
|
|
|
*
|
|
|
|
* @param aKeyPair
|
|
|
|
* KeyPair object
|
|
|
|
*
|
|
|
|
* @param aAudience
|
|
|
|
* Audience of the assertion
|
|
|
|
*
|
|
|
|
* @param aOptions (optional)
|
|
|
|
* Can include:
|
|
|
|
* {
|
|
|
|
* localtimeOffsetMsec: <clock offset in milliseconds>,
|
|
|
|
* now: <current date in milliseconds>
|
|
|
|
* duration: <validity duration for this assertion in milliseconds>
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* localtimeOffsetMsec is the number of milliseconds that need to be
|
|
|
|
* added to the local clock time to make it concur with the server.
|
|
|
|
* For example, if the local clock is two minutes fast, the offset in
|
|
|
|
* milliseconds would be -120000.
|
|
|
|
*
|
|
|
|
* @param aCallback
|
|
|
|
* Function to invoke with resulting assertion. Assertion
|
|
|
|
* will be string or null on failure.
|
|
|
|
*/
|
|
|
|
generateAssertion: function(aCert, aKeyPair, aAudience, aOptions, aCallback) {
|
|
|
|
if (typeof aOptions == "function") {
|
|
|
|
aCallback = aOptions;
|
|
|
|
aOptions = { };
|
|
|
|
}
|
|
|
|
|
2012-07-04 01:53:37 +04:00
|
|
|
// for now, we hack the algorithm name
|
|
|
|
// XXX bug 769851
|
|
|
|
var header = {"alg": "DS128"};
|
|
|
|
var headerBytes = IdentityCryptoService.base64UrlEncode(
|
|
|
|
JSON.stringify(header));
|
|
|
|
|
|
|
|
var payload = {
|
2014-01-24 06:04:38 +04:00
|
|
|
exp: this.getExpiration(
|
|
|
|
aOptions.duration, aOptions.localtimeOffsetMsec, aOptions.now),
|
2012-07-04 01:53:37 +04:00
|
|
|
aud: aAudience
|
|
|
|
};
|
|
|
|
var payloadBytes = IdentityCryptoService.base64UrlEncode(
|
|
|
|
JSON.stringify(payload));
|
|
|
|
|
2013-03-27 21:01:46 +04:00
|
|
|
log("payload bytes", payload, payloadBytes);
|
2012-07-04 01:53:37 +04:00
|
|
|
sign(headerBytes + "." + payloadBytes, aKeyPair, function(err, signature) {
|
|
|
|
if (err)
|
|
|
|
return aCallback(err);
|
|
|
|
|
|
|
|
var signedAssertion = headerBytes + "." + payloadBytes + "." + signature;
|
|
|
|
return aCallback(null, aCert + "~" + signedAssertion);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.jwcrypto = new jwcryptoClass();
|
2012-11-09 05:54:13 +04:00
|
|
|
this.jwcrypto.ALGORITHMS = ALGORITHMS;
|