services/sync/modules/keys.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 this.EXPORTED_SYMBOLS = [
michael@0 8 "BulkKeyBundle",
michael@0 9 "SyncKeyBundle"
michael@0 10 ];
michael@0 11
michael@0 12 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
michael@0 13
michael@0 14 Cu.import("resource://services-sync/constants.js");
michael@0 15 Cu.import("resource://gre/modules/Log.jsm");
michael@0 16 Cu.import("resource://services-sync/util.js");
michael@0 17
michael@0 18 /**
michael@0 19 * Represents a pair of keys.
michael@0 20 *
michael@0 21 * Each key stored in a key bundle is 256 bits. One key is used for symmetric
michael@0 22 * encryption. The other is used for HMAC.
michael@0 23 *
michael@0 24 * A KeyBundle by itself is just an anonymous pair of keys. Other types
michael@0 25 * deriving from this one add semantics, such as associated collections or
michael@0 26 * generating a key bundle via HKDF from another key.
michael@0 27 */
michael@0 28 function KeyBundle() {
michael@0 29 this._encrypt = null;
michael@0 30 this._encryptB64 = null;
michael@0 31 this._hmac = null;
michael@0 32 this._hmacB64 = null;
michael@0 33 this._hmacObj = null;
michael@0 34 this._sha256HMACHasher = null;
michael@0 35 }
michael@0 36 KeyBundle.prototype = {
michael@0 37 _encrypt: null,
michael@0 38 _encryptB64: null,
michael@0 39 _hmac: null,
michael@0 40 _hmacB64: null,
michael@0 41 _hmacObj: null,
michael@0 42 _sha256HMACHasher: null,
michael@0 43
michael@0 44 equals: function equals(bundle) {
michael@0 45 return bundle &&
michael@0 46 (bundle.hmacKey == this.hmacKey) &&
michael@0 47 (bundle.encryptionKey == this.encryptionKey);
michael@0 48 },
michael@0 49
michael@0 50 /*
michael@0 51 * Accessors for the two keys.
michael@0 52 */
michael@0 53 get encryptionKey() {
michael@0 54 return this._encrypt;
michael@0 55 },
michael@0 56
michael@0 57 set encryptionKey(value) {
michael@0 58 if (!value || typeof value != "string") {
michael@0 59 throw new Error("Encryption key can only be set to string values.");
michael@0 60 }
michael@0 61
michael@0 62 if (value.length < 16) {
michael@0 63 throw new Error("Encryption key must be at least 128 bits long.");
michael@0 64 }
michael@0 65
michael@0 66 this._encrypt = value;
michael@0 67 this._encryptB64 = btoa(value);
michael@0 68 },
michael@0 69
michael@0 70 get encryptionKeyB64() {
michael@0 71 return this._encryptB64;
michael@0 72 },
michael@0 73
michael@0 74 get hmacKey() {
michael@0 75 return this._hmac;
michael@0 76 },
michael@0 77
michael@0 78 set hmacKey(value) {
michael@0 79 if (!value || typeof value != "string") {
michael@0 80 throw new Error("HMAC key can only be set to string values.");
michael@0 81 }
michael@0 82
michael@0 83 if (value.length < 16) {
michael@0 84 throw new Error("HMAC key must be at least 128 bits long.");
michael@0 85 }
michael@0 86
michael@0 87 this._hmac = value;
michael@0 88 this._hmacB64 = btoa(value);
michael@0 89 this._hmacObj = value ? Utils.makeHMACKey(value) : null;
michael@0 90 this._sha256HMACHasher = value ? Utils.makeHMACHasher(
michael@0 91 Ci.nsICryptoHMAC.SHA256, this._hmacObj) : null;
michael@0 92 },
michael@0 93
michael@0 94 get hmacKeyB64() {
michael@0 95 return this._hmacB64;
michael@0 96 },
michael@0 97
michael@0 98 get hmacKeyObject() {
michael@0 99 return this._hmacObj;
michael@0 100 },
michael@0 101
michael@0 102 get sha256HMACHasher() {
michael@0 103 return this._sha256HMACHasher;
michael@0 104 },
michael@0 105
michael@0 106 /**
michael@0 107 * Populate this key pair with 2 new, randomly generated keys.
michael@0 108 */
michael@0 109 generateRandom: function generateRandom() {
michael@0 110 let generatedHMAC = Svc.Crypto.generateRandomKey();
michael@0 111 let generatedEncr = Svc.Crypto.generateRandomKey();
michael@0 112 this.keyPairB64 = [generatedEncr, generatedHMAC];
michael@0 113 },
michael@0 114
michael@0 115 };
michael@0 116
michael@0 117 /**
michael@0 118 * Represents a KeyBundle associated with a collection.
michael@0 119 *
michael@0 120 * This is just a KeyBundle with a collection attached.
michael@0 121 */
michael@0 122 this.BulkKeyBundle = function BulkKeyBundle(collection) {
michael@0 123 let log = Log.repository.getLogger("Sync.BulkKeyBundle");
michael@0 124 log.info("BulkKeyBundle being created for " + collection);
michael@0 125 KeyBundle.call(this);
michael@0 126
michael@0 127 this._collection = collection;
michael@0 128 }
michael@0 129
michael@0 130 BulkKeyBundle.prototype = {
michael@0 131 __proto__: KeyBundle.prototype,
michael@0 132
michael@0 133 get collection() {
michael@0 134 return this._collection;
michael@0 135 },
michael@0 136
michael@0 137 /**
michael@0 138 * Obtain the key pair in this key bundle.
michael@0 139 *
michael@0 140 * The returned keys are represented as raw byte strings.
michael@0 141 */
michael@0 142 get keyPair() {
michael@0 143 return [this.encryptionKey, this.hmacKey];
michael@0 144 },
michael@0 145
michael@0 146 set keyPair(value) {
michael@0 147 if (!Array.isArray(value) || value.length != 2) {
michael@0 148 throw new Error("BulkKeyBundle.keyPair value must be array of 2 keys.");
michael@0 149 }
michael@0 150
michael@0 151 this.encryptionKey = value[0];
michael@0 152 this.hmacKey = value[1];
michael@0 153 },
michael@0 154
michael@0 155 get keyPairB64() {
michael@0 156 return [this.encryptionKeyB64, this.hmacKeyB64];
michael@0 157 },
michael@0 158
michael@0 159 set keyPairB64(value) {
michael@0 160 if (!Array.isArray(value) || value.length != 2) {
michael@0 161 throw new Error("BulkKeyBundle.keyPairB64 value must be an array of 2 " +
michael@0 162 "keys.");
michael@0 163 }
michael@0 164
michael@0 165 this.encryptionKey = Utils.safeAtoB(value[0]);
michael@0 166 this.hmacKey = Utils.safeAtoB(value[1]);
michael@0 167 },
michael@0 168 };
michael@0 169
michael@0 170 /**
michael@0 171 * Represents a key pair derived from a Sync Key via HKDF.
michael@0 172 *
michael@0 173 * Instances of this type should be considered immutable. You create an
michael@0 174 * instance by specifying the username and 26 character "friendly" Base32
michael@0 175 * encoded Sync Key. The Sync Key is derived at instance creation time.
michael@0 176 *
michael@0 177 * If the username or Sync Key is invalid, an Error will be thrown.
michael@0 178 */
michael@0 179 this.SyncKeyBundle = function SyncKeyBundle(username, syncKey) {
michael@0 180 let log = Log.repository.getLogger("Sync.SyncKeyBundle");
michael@0 181 log.info("SyncKeyBundle being created.");
michael@0 182 KeyBundle.call(this);
michael@0 183
michael@0 184 this.generateFromKey(username, syncKey);
michael@0 185 }
michael@0 186 SyncKeyBundle.prototype = {
michael@0 187 __proto__: KeyBundle.prototype,
michael@0 188
michael@0 189 /*
michael@0 190 * If we've got a string, hash it into keys and store them.
michael@0 191 */
michael@0 192 generateFromKey: function generateFromKey(username, syncKey) {
michael@0 193 if (!username || (typeof username != "string")) {
michael@0 194 throw new Error("Sync Key cannot be generated from non-string username.");
michael@0 195 }
michael@0 196
michael@0 197 if (!syncKey || (typeof syncKey != "string")) {
michael@0 198 throw new Error("Sync Key cannot be generated from non-string key.");
michael@0 199 }
michael@0 200
michael@0 201 if (!Utils.isPassphrase(syncKey)) {
michael@0 202 throw new Error("Provided key is not a passphrase, cannot derive Sync " +
michael@0 203 "Key Bundle.");
michael@0 204 }
michael@0 205
michael@0 206 // Expand the base32 Sync Key to an AES 256 and 256 bit HMAC key.
michael@0 207 let prk = Utils.decodeKeyBase32(syncKey);
michael@0 208 let info = HMAC_INPUT + username;
michael@0 209 let okm = Utils.hkdfExpand(prk, info, 32 * 2);
michael@0 210 this.encryptionKey = okm.slice(0, 32);
michael@0 211 this.hmacKey = okm.slice(32, 64);
michael@0 212 },
michael@0 213 };
michael@0 214

mercurial