| 1 |
// Copyright 2011 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Provides a convenient API for data persistence with key and |
| 17 |
* object encryption. Without a valid secret, the existence of a particular |
| 18 |
* key can't be verified and values can't be decrypted. The value encryption |
| 19 |
* is salted, so subsequent writes of the same cleartext result in different |
| 20 |
* ciphertext. The ciphertext is *not* authenticated, so there is no protection |
| 21 |
* against data manipulation. |
| 22 |
* |
| 23 |
* The metadata is *not* encrypted, so expired keys can be cleaned up without |
| 24 |
* decrypting them. If sensitive metadata is added in subclasses, it is up |
| 25 |
* to the subclass to protect this information, perhaps by embedding it in |
| 26 |
* the object. |
| 27 |
* |
| 28 |
*/ |
| 29 |
|
| 30 |
goog.provide('goog.storage.EncryptedStorage'); |
| 31 |
|
| 32 |
goog.require('goog.crypt'); |
| 33 |
goog.require('goog.crypt.Arc4'); |
| 34 |
goog.require('goog.crypt.Sha1'); |
| 35 |
goog.require('goog.crypt.base64'); |
| 36 |
goog.require('goog.json'); |
| 37 |
goog.require('goog.json.Serializer'); |
| 38 |
goog.require('goog.storage.CollectableStorage'); |
| 39 |
goog.require('goog.storage.ErrorCode'); |
| 40 |
goog.require('goog.storage.RichStorage'); |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* Provides an encrypted storage. The keys are hashed with a secret, so |
| 46 |
* their existence cannot be verified without the knowledge of the secret. |
| 47 |
* The values are encrypted using the key, a salt, and the secret, so |
| 48 |
* stream cipher initialization varies for each stored value. |
| 49 |
* |
| 50 |
* @param {!goog.storage.mechanism.IterableMechanism} mechanism The underlying |
| 51 |
* storage mechanism. |
| 52 |
* @param {string} secret The secret key used to encrypt the storage. |
| 53 |
* @constructor |
| 54 |
* @struct |
| 55 |
* @extends {goog.storage.CollectableStorage} |
| 56 |
* @final |
| 57 |
*/ |
| 58 |
goog.storage.EncryptedStorage = function(mechanism, secret) { |
| 59 |
goog.storage.EncryptedStorage.base(this, 'constructor', mechanism); |
| 60 |
/** |
| 61 |
* The secret used to encrypt the storage. |
| 62 |
* |
| 63 |
* @private {!Array<number>} |
| 64 |
*/ |
| 65 |
this.secret_ = goog.crypt.stringToByteArray(secret); |
| 66 |
|
| 67 |
/** |
| 68 |
* The JSON serializer used to serialize values before encryption. This can |
| 69 |
* be potentially different from serializing for the storage mechanism (see |
| 70 |
* goog.storage.Storage), so a separate serializer is kept here. |
| 71 |
* |
| 72 |
* @private {!goog.json.Serializer} |
| 73 |
*/ |
| 74 |
this.cleartextSerializer_ = new goog.json.Serializer(); |
| 75 |
}; |
| 76 |
goog.inherits(goog.storage.EncryptedStorage, goog.storage.CollectableStorage); |
| 77 |
|
| 78 |
|
| 79 |
/** |
| 80 |
* Metadata key under which the salt is stored. |
| 81 |
* |
| 82 |
* @type {string} |
| 83 |
* @protected |
| 84 |
*/ |
| 85 |
goog.storage.EncryptedStorage.SALT_KEY = 'salt'; |
| 86 |
|
| 87 |
|
| 88 |
/** |
| 89 |
* Hashes a key using the secret. |
| 90 |
* |
| 91 |
* @param {string} key The key. |
| 92 |
* @return {string} The hash. |
| 93 |
* @private |
| 94 |
*/ |
| 95 |
goog.storage.EncryptedStorage.prototype.hashKeyWithSecret_ = function(key) { |
| 96 |
var sha1 = new goog.crypt.Sha1(); |
| 97 |
sha1.update(goog.crypt.stringToByteArray(key)); |
| 98 |
sha1.update(this.secret_); |
| 99 |
return goog.crypt.base64.encodeByteArray(sha1.digest(), true); |
| 100 |
}; |
| 101 |
|
| 102 |
|
| 103 |
/** |
| 104 |
* Encrypts a value using a key, a salt, and the secret. |
| 105 |
* |
| 106 |
* @param {!Array<number>} salt The salt. |
| 107 |
* @param {string} key The key. |
| 108 |
* @param {string} value The cleartext value. |
| 109 |
* @return {string} The encrypted value. |
| 110 |
* @private |
| 111 |
*/ |
| 112 |
goog.storage.EncryptedStorage.prototype.encryptValue_ = function( |
| 113 |
salt, key, value) { |
| 114 |
if (!(salt.length > 0)) { |
| 115 |
throw new Error('Non-empty salt must be provided'); |
| 116 |
} |
| 117 |
var sha1 = new goog.crypt.Sha1(); |
| 118 |
sha1.update(goog.crypt.stringToByteArray(key)); |
| 119 |
sha1.update(salt); |
| 120 |
sha1.update(this.secret_); |
| 121 |
var arc4 = new goog.crypt.Arc4(); |
| 122 |
arc4.setKey(sha1.digest()); |
| 123 |
// Warm up the streamcypher state, see goog.crypt.Arc4 for details. |
| 124 |
arc4.discard(1536); |
| 125 |
var bytes = goog.crypt.stringToByteArray(value); |
| 126 |
arc4.crypt(bytes); |
| 127 |
return goog.crypt.byteArrayToString(bytes); |
| 128 |
}; |
| 129 |
|
| 130 |
|
| 131 |
/** |
| 132 |
* Decrypts a value using a key, a salt, and the secret. |
| 133 |
* |
| 134 |
* @param {!Array<number>} salt The salt. |
| 135 |
* @param {string} key The key. |
| 136 |
* @param {string} value The encrypted value. |
| 137 |
* @return {string} The decrypted value. |
| 138 |
* @private |
| 139 |
*/ |
| 140 |
goog.storage.EncryptedStorage.prototype.decryptValue_ = function( |
| 141 |
salt, key, value) { |
| 142 |
// ARC4 is symmetric. |
| 143 |
return this.encryptValue_(salt, key, value); |
| 144 |
}; |
| 145 |
|
| 146 |
|
| 147 |
/** @override */ |
| 148 |
goog.storage.EncryptedStorage.prototype.set = function( |
| 149 |
key, value, opt_expiration) { |
| 150 |
if (!goog.isDef(value)) { |
| 151 |
goog.storage.EncryptedStorage.prototype.remove.call(this, key); |
| 152 |
return; |
| 153 |
} |
| 154 |
var salt = []; |
| 155 |
// 64-bit random salt. |
| 156 |
for (var i = 0; i < 8; ++i) { |
| 157 |
salt[i] = Math.floor(Math.random() * 0x100); |
| 158 |
} |
| 159 |
var wrapper = new goog.storage.RichStorage.Wrapper( |
| 160 |
this.encryptValue_( |
| 161 |
salt, key, this.cleartextSerializer_.serialize(value))); |
| 162 |
wrapper[goog.storage.EncryptedStorage.SALT_KEY] = salt; |
| 163 |
goog.storage.EncryptedStorage.base( |
| 164 |
this, 'set', this.hashKeyWithSecret_(key), wrapper, opt_expiration); |
| 165 |
}; |
| 166 |
|
| 167 |
|
| 168 |
/** @override */ |
| 169 |
goog.storage.EncryptedStorage.prototype.getWrapper = function( |
| 170 |
key, opt_expired) { |
| 171 |
var wrapper = goog.storage.EncryptedStorage.base( |
| 172 |
this, 'getWrapper', this.hashKeyWithSecret_(key), opt_expired); |
| 173 |
if (!wrapper) { |
| 174 |
return undefined; |
| 175 |
} |
| 176 |
var value = goog.storage.RichStorage.Wrapper.unwrap(wrapper); |
| 177 |
var salt = wrapper[goog.storage.EncryptedStorage.SALT_KEY]; |
| 178 |
if (!goog.isString(value) || !goog.isArray(salt) || !salt.length) { |
| 179 |
throw goog.storage.ErrorCode.INVALID_VALUE; |
| 180 |
} |
| 181 |
var json = this.decryptValue_(salt, key, value); |
| 182 |
|
| 183 |
try { |
| 184 |
wrapper[goog.storage.RichStorage.DATA_KEY] = JSON.parse(json); |
| 185 |
} catch (e) { |
| 186 |
throw goog.storage.ErrorCode.DECRYPTION_ERROR; |
| 187 |
} |
| 188 |
return wrapper; |
| 189 |
}; |
| 190 |
|
| 191 |
|
| 192 |
/** @override */ |
| 193 |
goog.storage.EncryptedStorage.prototype.remove = function(key) { |
| 194 |
goog.storage.EncryptedStorage.base( |
| 195 |
this, 'remove', this.hashKeyWithSecret_(key)); |
| 196 |
}; |