Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | Cu.import("resource://services-sync/constants.js"); |
michael@0 | 5 | Cu.import("resource://services-sync/identity.js"); |
michael@0 | 6 | Cu.import("resource://services-sync/keys.js"); |
michael@0 | 7 | Cu.import("resource://services-sync/record.js"); |
michael@0 | 8 | Cu.import("resource://services-sync/util.js"); |
michael@0 | 9 | |
michael@0 | 10 | let collectionKeys = new CollectionKeyManager(); |
michael@0 | 11 | |
michael@0 | 12 | function sha256HMAC(message, key) { |
michael@0 | 13 | let h = Utils.makeHMACHasher(Ci.nsICryptoHMAC.SHA256, key); |
michael@0 | 14 | return Utils.digestBytes(message, h); |
michael@0 | 15 | } |
michael@0 | 16 | |
michael@0 | 17 | function do_check_array_eq(a1, a2) { |
michael@0 | 18 | do_check_eq(a1.length, a2.length); |
michael@0 | 19 | for (let i = 0; i < a1.length; ++i) { |
michael@0 | 20 | do_check_eq(a1[i], a2[i]); |
michael@0 | 21 | } |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | function do_check_keypair_eq(a, b) { |
michael@0 | 25 | do_check_eq(2, a.length); |
michael@0 | 26 | do_check_eq(2, b.length); |
michael@0 | 27 | do_check_eq(a[0], b[0]); |
michael@0 | 28 | do_check_eq(a[1], b[1]); |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | function test_time_keyFromString(iterations) { |
michael@0 | 32 | let k; |
michael@0 | 33 | let o; |
michael@0 | 34 | let b = new BulkKeyBundle("dummy"); |
michael@0 | 35 | let d = Utils.decodeKeyBase32("ababcdefabcdefabcdefabcdef"); |
michael@0 | 36 | b.generateRandom(); |
michael@0 | 37 | |
michael@0 | 38 | _("Running " + iterations + " iterations of hmacKeyObject + sha256HMAC."); |
michael@0 | 39 | for (let i = 0; i < iterations; ++i) { |
michael@0 | 40 | let k = b.hmacKeyObject; |
michael@0 | 41 | o = sha256HMAC(d, k); |
michael@0 | 42 | } |
michael@0 | 43 | do_check_true(!!o); |
michael@0 | 44 | _("Done."); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | add_test(function test_set_invalid_values() { |
michael@0 | 48 | _("Ensure that setting invalid encryption and HMAC key values is caught."); |
michael@0 | 49 | |
michael@0 | 50 | let bundle = new BulkKeyBundle("foo"); |
michael@0 | 51 | |
michael@0 | 52 | let thrown = false; |
michael@0 | 53 | try { |
michael@0 | 54 | bundle.encryptionKey = null; |
michael@0 | 55 | } catch (ex) { |
michael@0 | 56 | thrown = true; |
michael@0 | 57 | do_check_eq(ex.message.indexOf("Encryption key can only be set to"), 0); |
michael@0 | 58 | } finally { |
michael@0 | 59 | do_check_true(thrown); |
michael@0 | 60 | thrown = false; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | try { |
michael@0 | 64 | bundle.encryptionKey = ["trollololol"]; |
michael@0 | 65 | } catch (ex) { |
michael@0 | 66 | thrown = true; |
michael@0 | 67 | do_check_eq(ex.message.indexOf("Encryption key can only be set to"), 0); |
michael@0 | 68 | } finally { |
michael@0 | 69 | do_check_true(thrown); |
michael@0 | 70 | thrown = false; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | try { |
michael@0 | 74 | bundle.hmacKey = Utils.generateRandomBytes(15); |
michael@0 | 75 | } catch (ex) { |
michael@0 | 76 | thrown = true; |
michael@0 | 77 | do_check_eq(ex.message.indexOf("HMAC key must be at least 128"), 0); |
michael@0 | 78 | } finally { |
michael@0 | 79 | do_check_true(thrown); |
michael@0 | 80 | thrown = false; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | try { |
michael@0 | 84 | bundle.hmacKey = null; |
michael@0 | 85 | } catch (ex) { |
michael@0 | 86 | thrown = true; |
michael@0 | 87 | do_check_eq(ex.message.indexOf("HMAC key can only be set to string"), 0); |
michael@0 | 88 | } finally { |
michael@0 | 89 | do_check_true(thrown); |
michael@0 | 90 | thrown = false; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | try { |
michael@0 | 94 | bundle.hmacKey = ["trollolol"]; |
michael@0 | 95 | } catch (ex) { |
michael@0 | 96 | thrown = true; |
michael@0 | 97 | do_check_eq(ex.message.indexOf("HMAC key can only be set to"), 0); |
michael@0 | 98 | } finally { |
michael@0 | 99 | do_check_true(thrown); |
michael@0 | 100 | thrown = false; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | try { |
michael@0 | 104 | bundle.hmacKey = Utils.generateRandomBytes(15); |
michael@0 | 105 | } catch (ex) { |
michael@0 | 106 | thrown = true; |
michael@0 | 107 | do_check_eq(ex.message.indexOf("HMAC key must be at least 128"), 0); |
michael@0 | 108 | } finally { |
michael@0 | 109 | do_check_true(thrown); |
michael@0 | 110 | thrown = false; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | run_next_test(); |
michael@0 | 114 | }); |
michael@0 | 115 | |
michael@0 | 116 | add_test(function test_repeated_hmac() { |
michael@0 | 117 | let testKey = "ababcdefabcdefabcdefabcdef"; |
michael@0 | 118 | let k = Utils.makeHMACKey("foo"); |
michael@0 | 119 | let one = sha256HMAC(Utils.decodeKeyBase32(testKey), k); |
michael@0 | 120 | let two = sha256HMAC(Utils.decodeKeyBase32(testKey), k); |
michael@0 | 121 | do_check_eq(one, two); |
michael@0 | 122 | |
michael@0 | 123 | run_next_test(); |
michael@0 | 124 | }); |
michael@0 | 125 | |
michael@0 | 126 | add_test(function test_sync_key_bundle_derivation() { |
michael@0 | 127 | _("Ensure derivation from known values works."); |
michael@0 | 128 | |
michael@0 | 129 | // The known values in this test were originally verified against Firefox |
michael@0 | 130 | // Home. |
michael@0 | 131 | let bundle = new SyncKeyBundle("st3fan", "q7ynpwq7vsc9m34hankbyi3s3i"); |
michael@0 | 132 | |
michael@0 | 133 | // These should be compared to the results from Home, as they once were. |
michael@0 | 134 | let e = "14b8c09fa84e92729ee695160af6e0385f8f6215a25d14906e1747bdaa2de426"; |
michael@0 | 135 | let h = "370e3566245d79fe602a3adb5137e42439cd2a571235197e0469d7d541b07875"; |
michael@0 | 136 | |
michael@0 | 137 | let realE = Utils.bytesAsHex(bundle.encryptionKey); |
michael@0 | 138 | let realH = Utils.bytesAsHex(bundle.hmacKey); |
michael@0 | 139 | |
michael@0 | 140 | _("Real E: " + realE); |
michael@0 | 141 | _("Real H: " + realH); |
michael@0 | 142 | do_check_eq(realH, h); |
michael@0 | 143 | do_check_eq(realE, e); |
michael@0 | 144 | |
michael@0 | 145 | run_next_test(); |
michael@0 | 146 | }); |
michael@0 | 147 | |
michael@0 | 148 | add_test(function test_keymanager() { |
michael@0 | 149 | let testKey = "ababcdefabcdefabcdefabcdef"; |
michael@0 | 150 | let username = "john@example.com"; |
michael@0 | 151 | |
michael@0 | 152 | // Decode the key here to mirror what generateEntry will do, |
michael@0 | 153 | // but pass it encoded into the KeyBundle call below. |
michael@0 | 154 | |
michael@0 | 155 | let sha256inputE = "" + HMAC_INPUT + username + "\x01"; |
michael@0 | 156 | let key = Utils.makeHMACKey(Utils.decodeKeyBase32(testKey)); |
michael@0 | 157 | let encryptKey = sha256HMAC(sha256inputE, key); |
michael@0 | 158 | |
michael@0 | 159 | let sha256inputH = encryptKey + HMAC_INPUT + username + "\x02"; |
michael@0 | 160 | let hmacKey = sha256HMAC(sha256inputH, key); |
michael@0 | 161 | |
michael@0 | 162 | // Encryption key is stored in base64 for WeaveCrypto convenience. |
michael@0 | 163 | do_check_eq(encryptKey, new SyncKeyBundle(username, testKey).encryptionKey); |
michael@0 | 164 | do_check_eq(hmacKey, new SyncKeyBundle(username, testKey).hmacKey); |
michael@0 | 165 | |
michael@0 | 166 | // Test with the same KeyBundle for both. |
michael@0 | 167 | let obj = new SyncKeyBundle(username, testKey); |
michael@0 | 168 | do_check_eq(hmacKey, obj.hmacKey); |
michael@0 | 169 | do_check_eq(encryptKey, obj.encryptionKey); |
michael@0 | 170 | |
michael@0 | 171 | run_next_test(); |
michael@0 | 172 | }); |
michael@0 | 173 | |
michael@0 | 174 | add_test(function test_collections_manager() { |
michael@0 | 175 | let log = Log.repository.getLogger("Test"); |
michael@0 | 176 | Log.repository.rootLogger.addAppender(new Log.DumpAppender()); |
michael@0 | 177 | |
michael@0 | 178 | let identity = new IdentityManager(); |
michael@0 | 179 | |
michael@0 | 180 | identity.account = "john@example.com"; |
michael@0 | 181 | identity.syncKey = "a-bbbbb-ccccc-ddddd-eeeee-fffff"; |
michael@0 | 182 | |
michael@0 | 183 | let keyBundle = identity.syncKeyBundle; |
michael@0 | 184 | |
michael@0 | 185 | /* |
michael@0 | 186 | * Build a test version of storage/crypto/keys. |
michael@0 | 187 | * Encrypt it with the sync key. |
michael@0 | 188 | * Pass it into the CollectionKeyManager. |
michael@0 | 189 | */ |
michael@0 | 190 | |
michael@0 | 191 | log.info("Building storage keys..."); |
michael@0 | 192 | let storage_keys = new CryptoWrapper("crypto", "keys"); |
michael@0 | 193 | let default_key64 = Svc.Crypto.generateRandomKey(); |
michael@0 | 194 | let default_hmac64 = Svc.Crypto.generateRandomKey(); |
michael@0 | 195 | let bookmarks_key64 = Svc.Crypto.generateRandomKey(); |
michael@0 | 196 | let bookmarks_hmac64 = Svc.Crypto.generateRandomKey(); |
michael@0 | 197 | |
michael@0 | 198 | storage_keys.cleartext = { |
michael@0 | 199 | "default": [default_key64, default_hmac64], |
michael@0 | 200 | "collections": {"bookmarks": [bookmarks_key64, bookmarks_hmac64]}, |
michael@0 | 201 | }; |
michael@0 | 202 | storage_keys.modified = Date.now()/1000; |
michael@0 | 203 | storage_keys.id = "keys"; |
michael@0 | 204 | |
michael@0 | 205 | log.info("Encrypting storage keys..."); |
michael@0 | 206 | |
michael@0 | 207 | // Use passphrase (sync key) itself to encrypt the key bundle. |
michael@0 | 208 | storage_keys.encrypt(keyBundle); |
michael@0 | 209 | |
michael@0 | 210 | // Sanity checking. |
michael@0 | 211 | do_check_true(null == storage_keys.cleartext); |
michael@0 | 212 | do_check_true(null != storage_keys.ciphertext); |
michael@0 | 213 | |
michael@0 | 214 | log.info("Updating collection keys."); |
michael@0 | 215 | |
michael@0 | 216 | // updateContents decrypts the object, releasing the payload for us to use. |
michael@0 | 217 | // Returns true, because the default key has changed. |
michael@0 | 218 | do_check_true(collectionKeys.updateContents(keyBundle, storage_keys)); |
michael@0 | 219 | let payload = storage_keys.cleartext; |
michael@0 | 220 | |
michael@0 | 221 | _("CK: " + JSON.stringify(collectionKeys._collections)); |
michael@0 | 222 | |
michael@0 | 223 | // Test that the CollectionKeyManager returns a similar WBO. |
michael@0 | 224 | let wbo = collectionKeys.asWBO("crypto", "keys"); |
michael@0 | 225 | |
michael@0 | 226 | _("WBO: " + JSON.stringify(wbo)); |
michael@0 | 227 | _("WBO cleartext: " + JSON.stringify(wbo.cleartext)); |
michael@0 | 228 | |
michael@0 | 229 | // Check the individual contents. |
michael@0 | 230 | do_check_eq(wbo.collection, "crypto"); |
michael@0 | 231 | do_check_eq(wbo.id, "keys"); |
michael@0 | 232 | do_check_eq(undefined, wbo.modified); |
michael@0 | 233 | do_check_eq(collectionKeys.lastModified, storage_keys.modified); |
michael@0 | 234 | do_check_true(!!wbo.cleartext.default); |
michael@0 | 235 | do_check_keypair_eq(payload.default, wbo.cleartext.default); |
michael@0 | 236 | do_check_keypair_eq(payload.collections.bookmarks, wbo.cleartext.collections.bookmarks); |
michael@0 | 237 | |
michael@0 | 238 | do_check_true('bookmarks' in collectionKeys._collections); |
michael@0 | 239 | do_check_false('tabs' in collectionKeys._collections); |
michael@0 | 240 | |
michael@0 | 241 | _("Updating contents twice with the same data doesn't proceed."); |
michael@0 | 242 | storage_keys.encrypt(keyBundle); |
michael@0 | 243 | do_check_false(collectionKeys.updateContents(keyBundle, storage_keys)); |
michael@0 | 244 | |
michael@0 | 245 | /* |
michael@0 | 246 | * Test that we get the right keys out when we ask for |
michael@0 | 247 | * a collection's tokens. |
michael@0 | 248 | */ |
michael@0 | 249 | let b1 = new BulkKeyBundle("bookmarks"); |
michael@0 | 250 | b1.keyPairB64 = [bookmarks_key64, bookmarks_hmac64]; |
michael@0 | 251 | let b2 = collectionKeys.keyForCollection("bookmarks"); |
michael@0 | 252 | do_check_keypair_eq(b1.keyPair, b2.keyPair); |
michael@0 | 253 | |
michael@0 | 254 | // Check key equality. |
michael@0 | 255 | do_check_true(b1.equals(b2)); |
michael@0 | 256 | do_check_true(b2.equals(b1)); |
michael@0 | 257 | |
michael@0 | 258 | b1 = new BulkKeyBundle("[default]"); |
michael@0 | 259 | b1.keyPairB64 = [default_key64, default_hmac64]; |
michael@0 | 260 | |
michael@0 | 261 | do_check_false(b1.equals(b2)); |
michael@0 | 262 | do_check_false(b2.equals(b1)); |
michael@0 | 263 | |
michael@0 | 264 | b2 = collectionKeys.keyForCollection(null); |
michael@0 | 265 | do_check_keypair_eq(b1.keyPair, b2.keyPair); |
michael@0 | 266 | |
michael@0 | 267 | /* |
michael@0 | 268 | * Checking for update times. |
michael@0 | 269 | */ |
michael@0 | 270 | let info_collections = {}; |
michael@0 | 271 | do_check_true(collectionKeys.updateNeeded(info_collections)); |
michael@0 | 272 | info_collections["crypto"] = 5000; |
michael@0 | 273 | do_check_false(collectionKeys.updateNeeded(info_collections)); |
michael@0 | 274 | info_collections["crypto"] = 1 + (Date.now()/1000); // Add one in case computers are fast! |
michael@0 | 275 | do_check_true(collectionKeys.updateNeeded(info_collections)); |
michael@0 | 276 | |
michael@0 | 277 | collectionKeys.lastModified = null; |
michael@0 | 278 | do_check_true(collectionKeys.updateNeeded({})); |
michael@0 | 279 | |
michael@0 | 280 | /* |
michael@0 | 281 | * Check _compareKeyBundleCollections. |
michael@0 | 282 | */ |
michael@0 | 283 | function newBundle(name) { |
michael@0 | 284 | let r = new BulkKeyBundle(name); |
michael@0 | 285 | r.generateRandom(); |
michael@0 | 286 | return r; |
michael@0 | 287 | } |
michael@0 | 288 | let k1 = newBundle("k1"); |
michael@0 | 289 | let k2 = newBundle("k2"); |
michael@0 | 290 | let k3 = newBundle("k3"); |
michael@0 | 291 | let k4 = newBundle("k4"); |
michael@0 | 292 | let k5 = newBundle("k5"); |
michael@0 | 293 | let coll1 = {"foo": k1, "bar": k2}; |
michael@0 | 294 | let coll2 = {"foo": k1, "bar": k2}; |
michael@0 | 295 | let coll3 = {"foo": k1, "bar": k3}; |
michael@0 | 296 | let coll4 = {"foo": k4}; |
michael@0 | 297 | let coll5 = {"baz": k5, "bar": k2}; |
michael@0 | 298 | let coll6 = {}; |
michael@0 | 299 | |
michael@0 | 300 | let d1 = collectionKeys._compareKeyBundleCollections(coll1, coll2); // [] |
michael@0 | 301 | let d2 = collectionKeys._compareKeyBundleCollections(coll1, coll3); // ["bar"] |
michael@0 | 302 | let d3 = collectionKeys._compareKeyBundleCollections(coll3, coll2); // ["bar"] |
michael@0 | 303 | let d4 = collectionKeys._compareKeyBundleCollections(coll1, coll4); // ["bar", "foo"] |
michael@0 | 304 | let d5 = collectionKeys._compareKeyBundleCollections(coll5, coll2); // ["baz", "foo"] |
michael@0 | 305 | let d6 = collectionKeys._compareKeyBundleCollections(coll6, coll1); // ["bar", "foo"] |
michael@0 | 306 | let d7 = collectionKeys._compareKeyBundleCollections(coll5, coll5); // [] |
michael@0 | 307 | let d8 = collectionKeys._compareKeyBundleCollections(coll6, coll6); // [] |
michael@0 | 308 | |
michael@0 | 309 | do_check_true(d1.same); |
michael@0 | 310 | do_check_false(d2.same); |
michael@0 | 311 | do_check_false(d3.same); |
michael@0 | 312 | do_check_false(d4.same); |
michael@0 | 313 | do_check_false(d5.same); |
michael@0 | 314 | do_check_false(d6.same); |
michael@0 | 315 | do_check_true(d7.same); |
michael@0 | 316 | do_check_true(d8.same); |
michael@0 | 317 | |
michael@0 | 318 | do_check_array_eq(d1.changed, []); |
michael@0 | 319 | do_check_array_eq(d2.changed, ["bar"]); |
michael@0 | 320 | do_check_array_eq(d3.changed, ["bar"]); |
michael@0 | 321 | do_check_array_eq(d4.changed, ["bar", "foo"]); |
michael@0 | 322 | do_check_array_eq(d5.changed, ["baz", "foo"]); |
michael@0 | 323 | do_check_array_eq(d6.changed, ["bar", "foo"]); |
michael@0 | 324 | |
michael@0 | 325 | run_next_test(); |
michael@0 | 326 | }); |
michael@0 | 327 | |
michael@0 | 328 | function run_test() { |
michael@0 | 329 | // Only do 1,000 to avoid a 5-second pause in test runs. |
michael@0 | 330 | test_time_keyFromString(1000); |
michael@0 | 331 | |
michael@0 | 332 | run_next_test(); |
michael@0 | 333 | } |