Commit d5283e45 authored by lucas.parsy's avatar lucas.parsy

added support of json crypto key in cryptstorage

modified example provided in cryptstorage.js comments.
corrected error introduced by last commit
(renaming of variable causing errors).
modified tests to comply with cryptstorage.js changes
parent 8d3d5de3
...@@ -7,17 +7,24 @@ ...@@ -7,17 +7,24 @@
/*jslint nomen: true*/ /*jslint nomen: true*/
/*global jIO, RSVP, DOMException, Blob, crypto, Uint8Array, ArrayBuffer*/ /*global jIO, RSVP, DOMException, Blob, crypto, Uint8Array, ArrayBuffer*/
(function (jIO, RSVP, DOMException, Blob) { (function (jIO, RSVP, DOMException, Blob, crypto, Uint8Array, ArrayBuffer) {
"use strict"; "use strict";
// you the cryptography system used by this storage is AES-GCM. // you the cryptography system used by this storage is AES-GCM.
// here is an example of how to generate a key. // here is an example of how to generate a key to the json format.
// var key; // var key,
// jsonKey;
// crypto.subtle.generateKey({name: "AES-GCM",length: 256}, // crypto.subtle.generateKey({name: "AES-GCM",length: 256},
// (true), ["encrypt", "decrypt"]) // (true), ["encrypt", "decrypt"])
// .then(function(res){key = res;}); // .then(function(res){key = res;});
//
// window.crypto.subtle.exportKey("jwk", key)
// .then(function(res){jsonKey = val})
//
//var storage = jIO.createJIO({type: "crypt", key: jsonKey,
// sub_storage: {...}});
// find more informations about this cryptography system on // find more informations about this cryptography system on
// https://github.com/diafygi/webcrypto-examples#aes-gcm // https://github.com/diafygi/webcrypto-examples#aes-gcm
...@@ -32,13 +39,29 @@ ...@@ -32,13 +39,29 @@
var MIME_TYPE = "application/x-jio-aes-gcm-encryption"; var MIME_TYPE = "application/x-jio-aes-gcm-encryption";
function CryptStorage(spec) { function CryptStorage(spec) {
if (!spec.key || typeof spec.key !== "object") {
throw new TypeError("'key' must be a CryptoKey object");
}
this._key = spec.key; this._key = spec.key;
this._jsonKey = true;
this._sub_storage = jIO.createJIO(spec.sub_storage); this._sub_storage = jIO.createJIO(spec.sub_storage);
} }
function convertKey(that) {
return new RSVP.Queue()
.push(function () {
return crypto.subtle.importKey("jwk", that._key,
"AES-GCM", false,
["encrypt", "decrypt"]);
})
.push(function (res) {
that._key = res;
that._jsonKey = false;
return;
}, function () {
throw new TypeError(
"'key' must be a CryptoKey to JSON Web Key format"
);
});
}
CryptStorage.prototype.get = function () { CryptStorage.prototype.get = function () {
return this._sub_storage.get.apply(this._sub_storage, return this._sub_storage.get.apply(this._sub_storage,
arguments); arguments);
...@@ -75,6 +98,12 @@ ...@@ -75,6 +98,12 @@
that = this; that = this;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () {
if (that._jsonKey === true) {
return convertKey(that);
}
return;
})
.push(function () { .push(function () {
return jIO.util.readBlobAsDataURL(blob); return jIO.util.readBlobAsDataURL(blob);
}) })
...@@ -91,7 +120,7 @@ ...@@ -91,7 +120,7 @@
} }
return crypto.subtle.encrypt({ return crypto.subtle.encrypt({
name : "AES-GCM", name : "AES-GCM",
initializaton_vector : initializaton_vector iv : initializaton_vector
}, },
that._key, buf); that._key, buf);
}) })
...@@ -110,6 +139,12 @@ ...@@ -110,6 +139,12 @@
return blob; return blob;
} }
return new RSVP.Queue() return new RSVP.Queue()
.push(function () {
if (that._jsonKey === true) {
return convertKey(that);
}
return;
})
.push(function () { .push(function () {
return jIO.util.readBlobAsArrayBuffer(blob); return jIO.util.readBlobAsArrayBuffer(blob);
}) })
...@@ -120,7 +155,7 @@ ...@@ -120,7 +155,7 @@
initializaton_vector = new Uint8Array(coded.slice(0, 12)); initializaton_vector = new Uint8Array(coded.slice(0, 12));
return crypto.subtle.decrypt({ return crypto.subtle.decrypt({
name : "AES-GCM", name : "AES-GCM",
initializaton_vector : initializaton_vector iv : initializaton_vector
}, },
that._key, coded.slice(12)); that._key, coded.slice(12));
}) })
...@@ -135,7 +170,7 @@ ...@@ -135,7 +170,7 @@
} }
throw error; throw error;
} }
}); }, function () { return blob; });
}); });
}; };
...@@ -151,4 +186,4 @@ ...@@ -151,4 +186,4 @@
jIO.addStorage('crypt', CryptStorage); jIO.addStorage('crypt', CryptStorage);
}(jIO, RSVP, DOMException, Blob)); }(jIO, RSVP, DOMException, Blob, crypto, Uint8Array, ArrayBuffer));
...@@ -11,25 +11,9 @@ ...@@ -11,25 +11,9 @@
equal = QUnit.equal, equal = QUnit.equal,
throws = QUnit.throws, throws = QUnit.throws,
module = QUnit.module, module = QUnit.module,
key; key = {"alg": "A256GCM", "ext": true,
"k": "seeaLzpu8dHG07bO2ANH2GywbTqs_zrs4Vq8zmtYeE4",
crypto.subtle.importKey( "key_ops": ["encrypt", "decrypt"], "kty": "oct"};
"jwk",
{
kty: "oct",
k: "L6hUS9PdMP5AIxXyiFM0GOBukp0heD5wHPRctvWBcVg",
alg: "A256GCM",
ext: true
},
{
name: "AES-GCM"
},
true,
["encrypt", "decrypt"]
)
.then(function (res) {
key = res;
});
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// Custom test substorage definition // Custom test substorage definition
...@@ -50,6 +34,7 @@ ...@@ -50,6 +34,7 @@
key: key, key: key,
sub_storage: {type : "cryptstorage200"} sub_storage: {type : "cryptstorage200"}
}); });
equal(jio.__type, "crypt"); equal(jio.__type, "crypt");
equal(jio.__storage._sub_storage.__type, "cryptstorage200"); equal(jio.__storage._sub_storage.__type, "cryptstorage200");
}); });
...@@ -405,10 +390,11 @@ ...@@ -405,10 +390,11 @@
var id = "/", var id = "/",
attachment = "stringattachment", attachment = "stringattachment",
value = "azertyuio\npàç_è-('é&", value = "azertyuio\npàç_è-('é&",
tocheck = "data:application/x-jio-aes-gcm-encryption;base64,L3" + tocheck = "data:application/x-jio-aes-gcm-encryption;base64" +
"LcvzpAlxu8/xd0fW7lPHZs5AP0ncexWoTfH57PCVkvrtp1JoB" + ",+p/Ho+KgGHZC2zDLMbQQS2tXcsy0g+Ho41VZnlPEkXdmG9zm36c8iLCkv" +
"wDzUYO+DHsfjAkzXkxhHHNUmxAtDiiSkRSvcbderS9FfIC7U6" + "lanyWCN510NK4hj1EgWQ6WrLS5pCmA/yeAWh+HyfPkYKDRHVBl6+Hxd53I" +
"KoGcqiP3OkEseL9Rd7F+qBwGuuDJyg==", "TmiWQ6Vix2jaIQg==",
blob = jIO.util.dataURItoBlob(tocheck); blob = jIO.util.dataURItoBlob(tocheck);
...@@ -455,7 +441,17 @@ ...@@ -455,7 +441,17 @@
}); });
function decodeAES(blob) { function decodeAES(blob) {
var decryptKey;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () {
return crypto.subtle.importKey("jwk", key,
"AES-GCM", false, ["decrypt"]);
})
.push(function (res) {
decryptKey = res;
return;
})
.push(function () { .push(function () {
return jIO.util.readBlobAsArrayBuffer(blob); return jIO.util.readBlobAsArrayBuffer(blob);
}) })
...@@ -465,7 +461,7 @@ ...@@ -465,7 +461,7 @@
coded = coded.currentTarget.result; coded = coded.currentTarget.result;
iv = new Uint8Array(coded.slice(0, 12)); iv = new Uint8Array(coded.slice(0, 12));
return crypto.subtle.decrypt({name : "AES-GCM", iv : iv}, return crypto.subtle.decrypt({name : "AES-GCM", iv : iv},
key, coded.slice(12)); decryptKey, coded.slice(12));
}) })
.push(function (arr) { .push(function (arr) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment