Improve KeyStore functionality
diff --git a/js/tools/build/ndn-js-uncomp.js b/js/tools/build/ndn-js-uncomp.js
index 2f6f4f8..5d23258 100644
--- a/js/tools/build/ndn-js-uncomp.js
+++ b/js/tools/build/ndn-js-uncomp.js
@@ -113,10 +113,18 @@
NDN.KeyStore = new Array();
-var KeyStoreEntry = function KeyStoreEntry(name, key, rsa) {
+var KeyStoreEntry = function KeyStoreEntry(name, rsa, time) {
this.keyName = name; // KeyName
- this.keyHex = key; // Raw key hex string
this.rsaKey = rsa; // RSA key
+ this.timeStamp = time; // Time Stamp
+};
+
+NDN.addKeyEntry = function(/* KeyStoreEntry */ keyEntry) {
+ var result = NDN.getKeyByName(keyEntry.keyName);
+ if (result == null)
+ NDN.KeyStore.push(keyEntry);
+ else
+ result = keyEntry;
};
NDN.getKeyByName = function(/* KeyName */ name) {
@@ -427,7 +435,6 @@
//console.log(currentClosure.timerID);
// Key verification
- var verified = false;
// Recursive key fetching & verification closure
var KeyFetchClosure = function KeyFetchClosure(content, closure, key, signature) {
@@ -444,19 +451,18 @@
console.log("In KeyFetchClosure.upcall: interest time out.");
} else if (kind == Closure.UPCALL_CONTENT) {
console.log("In KeyFetchClosure.upcall: signature verification passed");
- var keyHex = DataUtils.toHex(upcallInfo.contentObject.content).toLowerCase();
- //console.log("Key: " + keyHex);
- var kp = keyHex.slice(56, 314);
- var exp = keyHex.slice(318, 324);
-
- var rsakey = new RSAKey();
- rsakey.setPublic(kp, exp);
+ var rsakey = decodeSubjectPublicKeyInfo(upcallInfo.contentObject.content);
var verified = rsakey.verifyByteArray(this.contentObject.rawSignatureData, this.signature);
var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD;
//console.log("raise encapsulated closure");
this.closure.upcall(flag, new UpcallInfo(ndn, null, 0, this.contentObject));
+
+ // Store key in cache
+ var keyEntry = new KeyStoreEntry(keylocator.keyName, rsakey, new Date().getTime());
+ NDN.addKeyEntry(keyEntry);
+ //console.log(NDN.KeyStore);
}
};
@@ -473,22 +479,17 @@
if (nameStr.match(keyname)) {
console.log("Content is key itself");
- var keyHex = DataUtils.toHex(co.content).toLowerCase();
- console.log("Key content: " + keyHex);
-
- var kp = keyHex.slice(56, 314);
- var exp = keyHex.slice(318, 324);
-
- var rsakey = new RSAKey();
- rsakey.setPublic(kp, exp);
+ var rsakey = decodeSubjectPublicKeyInfo(co.content);
var verified = rsakey.verifyByteArray(co.rawSignatureData, sigHex);
var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD;
currentClosure.upcall(flag, new UpcallInfo(ndn, null, 0, co));
- // Store key in cache
- var keyEntry = new KeyStoreEntry(keylocator.keyName, keyHex, rsakey);
- NDN.KeyStore.push(keyEntry);
+ // SWT: We don't need to store key here since the same key will be
+ // stored again in the closure.
+ //var keyEntry = new KeyStoreEntry(keylocator.keyName, rsakey, new Date().getTime());
+ //NDN.addKeyEntry(keyEntry);
+ //console.log(NDN.KeyStore);
} else {
console.log("Fetch key according to keylocator");
@@ -498,12 +499,11 @@
// Key found, verify now
console.log("Local key cache hit");
var rsakey = keyEntry.rsaKey;
- verified = rsakey.verifyByteArray(co.rawSignatureData, sigHex);
-
+ var verified = rsakey.verifyByteArray(co.rawSignatureData, sigHex);
var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD;
// Raise callback
- currentClosure.upcall(Closure.UPCALL_CONTENT, new UpcallInfo(ndn, null, 0, co));
+ currentClosure.upcall(flag, new UpcallInfo(ndn, null, 0, co));
} else {
// Not found, fetch now
var nextClosure = new KeyFetchClosure(co, currentClosure, keyname, sigHex);
@@ -514,24 +514,16 @@
}
} else if (keylocator.type == KeyLocatorType.KEY) {
console.log("Keylocator contains KEY");
- var publickeyHex = DataUtils.toHex(keylocator.publicKey).toLowerCase();
- console.log(publickeyHex);
- var kp = publickeyHex.slice(56, 314);
- var exp = publickeyHex.slice(318, 324);
-
- var rsakey = new RSAKey();
- rsakey.setPublic(kp, exp);
- verified = rsakey.verifyByteArray(co.rawSignatureData, sigHex);
-
+ var rsakey = decodeSubjectPublicKeyInfo(co.signedInfo.locator.publicKey);
+ var verified = rsakey.verifyByteArray(co.rawSignatureData, sigHex);
var flag = (verified == true) ? Closure.UPCALL_CONTENT : Closure.UPCALL_CONTENT_BAD;
// Raise callback
currentClosure.upcall(Closure.UPCALL_CONTENT, new UpcallInfo(ndn, null, 0, co));
- // Store key in cache
- var keyEntry = new KeyStoreEntry(keylocator.keyName, publickeyHex, rsakey);
- NDN.KeyStore.push(keyEntry);
+ // Since KeyLocator does not contain key name for this key,
+ // we have no way to store it as a key entry in KeyStore.
} else {
var cert = keylocator.certificate;
console.log("KeyLocator contains CERT");