OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 var utils = require('utils'); | 5 var utils = require('utils'); |
6 | 6 |
7 /** | 7 /** |
8 * Enum of possible key types (subset of WebCrypto.KeyType). | 8 * Enum of possible key types (subset of WebCrypto.KeyType). |
9 * @enum {string} | 9 * @enum {string} |
10 */ | 10 */ |
11 var KeyType = { | 11 var KeyType = { |
12 public: 'public', | 12 public: 'public', |
13 private: 'private' | 13 private: 'private' |
14 }; | 14 }; |
15 | 15 |
16 /** | 16 /** |
17 * Enum of possible key usages (subset of WebCrypto.KeyUsage). | 17 * Enum of possible key usages (subset of WebCrypto.KeyUsage). |
18 * @enum {string} | 18 * @enum {string} |
19 */ | 19 */ |
20 var KeyUsage = { | 20 var KeyUsage = { |
21 sign: 'sign', | 21 sign: 'sign', |
22 verify: 'verify' | 22 verify: 'verify' |
23 }; | 23 }; |
24 | 24 |
25 // Freezes |object| recursively. |object| must not contain cyclic references. | |
26 // TODO(pneubeck): Freeze Uint8Arrays (currently used for the publicExponent | |
27 // only) also, once V8 supports it. | |
28 function deepFreeze(object) { | |
29 if (Object.isFrozen(object)) | |
30 return; | |
31 Object.freeze(object); | |
32 for (var key in object) { | |
33 var prop = object[key]; | |
34 if (!object.hasOwnProperty(key) || (typeof prop) !== 'object' || | |
35 prop.__proto__.constructor.name === 'Uint8Array') { | |
36 continue; | |
37 } | |
38 deepFreeze(prop); | |
39 } | |
40 } | |
41 | |
25 /** | 42 /** |
26 * Implementation of WebCrypto.Key used in enterprise.platformKeys. | 43 * Implementation of WebCrypto.Key used in enterprise.platformKeys. |
27 * @param {KeyType} type The type of the new key. | 44 * @param {KeyType} type The type of the new key. |
28 * @param {ArrayBuffer} publicKeySpki The Subject Public Key Info in DER | 45 * @param {ArrayBuffer} publicKeySpki The Subject Public Key Info in DER |
29 * encoding. | 46 * encoding. |
30 * @param {KeyAlgorithm} algorithm The algorithm identifier. | 47 * @param {KeyAlgorithm} algorithm The algorithm identifier. |
31 * @param {KeyUsage[]} usages The allowed key usages. | 48 * @param {KeyUsage[]} usages The allowed key usages. |
32 * @param {boolean} extractable Whether the key is extractable. | 49 * @param {boolean} extractable Whether the key is extractable. |
33 * @constructor | 50 * @constructor |
34 */ | 51 */ |
35 var KeyImpl = function(type, publicKeySpki, algorithm, usages, extractable) { | 52 var KeyImpl = function(type, publicKeySpki, algorithm, usages, extractable) { |
36 this.type = type; | 53 this.type = type; |
37 this.spki = publicKeySpki; | 54 this.spki = publicKeySpki; |
38 this.algorithm = algorithm; | 55 this.algorithm = algorithm; |
56 deepFreeze(this.algorithm); | |
not at google - send to devlin
2014/06/16 17:58:32
why do you need this? the way that you're exposing
pneubeck (no reviews)
2014/06/16 18:01:18
algorithm is an Object and defineProperty doesn't
not at google - send to devlin
2014/06/16 18:08:31
ah right. "deepFreeze" is a great name.
I find it
| |
39 this.usages = usages; | 57 this.usages = usages; |
40 this.extractable = extractable; | 58 this.extractable = extractable; |
41 }; | 59 }; |
42 | 60 |
43 var Key = | 61 var Key = |
44 utils.expose('Key', | 62 utils.expose('Key', |
45 KeyImpl, | 63 KeyImpl, |
46 {readonly:['extractable', 'type', 'algorithm', 'usages']}); | 64 {readonly:['extractable', 'type', 'algorithm', 'usages']}); |
47 | 65 |
48 /** | 66 /** |
49 * Returns |key|'s Subject Public Key Info. Throws an exception if |key| is not | 67 * Returns |key|'s Subject Public Key Info. Throws an exception if |key| is not |
50 * a valid Key object. | 68 * a valid Key object. |
51 * @param {Key} key | 69 * @param {Key} key |
52 * @return {ArrayBuffer} The Subject Public Key Info in DER encoding of |key|. | 70 * @return {ArrayBuffer} The Subject Public Key Info in DER encoding of |key|. |
53 */ | 71 */ |
54 function getSpki(key) { | 72 function getSpki(key) { |
55 if (!privates(key)) | 73 if (!privates(key)) |
56 throw new Error('Invalid key object.'); | 74 throw new Error('Invalid key object.'); |
57 var keyImpl = privates(key).impl; | 75 var keyImpl = privates(key).impl; |
58 if (!keyImpl || !keyImpl.spki) | 76 if (!keyImpl || !keyImpl.spki) |
59 throw new Error('Invalid key object.'); | 77 throw new Error('Invalid key object.'); |
60 return keyImpl.spki; | 78 return keyImpl.spki; |
61 } | 79 } |
62 | 80 |
63 exports.Key = Key; | 81 exports.Key = Key; |
64 exports.KeyType = KeyType; | 82 exports.KeyType = KeyType; |
65 exports.KeyUsage = KeyUsage; | 83 exports.KeyUsage = KeyUsage; |
66 exports.getSpki = getSpki; | 84 exports.getSpki = getSpki; |
OLD | NEW |