| 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 // Copies |value| recursively. |value| must not contain cyclic references. |
| 26 // Copies only nested structures of non-object types (like number), Array, |
| 27 // Uint8Array and the directly owned properties of objects (it ignores |
| 28 // prototypes). |
| 29 function deepCopy(value) { |
| 30 if (value === null) |
| 31 return null; |
| 32 if (typeof(value) !== 'object') |
| 33 return value; |
| 34 |
| 35 var copy = null; |
| 36 if (Array.isArray(value) ) { |
| 37 copy = new Array(); |
| 38 for (var i = 0; i < value.length; i++) |
| 39 copy[i] = deepCopy(value[i]); |
| 40 } else if (value.__proto__.constructor.name === 'Uint8Array') { |
| 41 copy = new Uint8Array(value.length); |
| 42 for (var i = 0; i < value.length; i++) |
| 43 copy[i] = value[i]; |
| 44 } else { |
| 45 copy = new Object(); |
| 46 for (var key in value) { |
| 47 if (!value.hasOwnProperty(key)) |
| 48 continue; |
| 49 copy[key] = deepCopy(value[key]); |
| 50 } |
| 51 } |
| 52 return copy; |
| 53 } |
| 54 |
| 25 /** | 55 /** |
| 26 * Implementation of WebCrypto.Key used in enterprise.platformKeys. | 56 * Implementation of WebCrypto.Key used in enterprise.platformKeys. |
| 27 * @param {KeyType} type The type of the new key. | 57 * @param {KeyType} type The type of the new key. |
| 28 * @param {ArrayBuffer} publicKeySpki The Subject Public Key Info in DER | 58 * @param {ArrayBuffer} publicKeySpki The Subject Public Key Info in DER |
| 29 * encoding. | 59 * encoding. |
| 30 * @param {KeyAlgorithm} algorithm The algorithm identifier. | 60 * @param {KeyAlgorithm} algorithm The algorithm identifier. |
| 31 * @param {KeyUsage[]} usages The allowed key usages. | 61 * @param {KeyUsage[]} usages The allowed key usages. |
| 32 * @param {boolean} extractable Whether the key is extractable. | 62 * @param {boolean} extractable Whether the key is extractable. |
| 33 * @constructor | 63 * @constructor |
| 34 */ | 64 */ |
| 35 var KeyImpl = function(type, publicKeySpki, algorithm, usages, extractable) { | 65 var KeyImpl = function(type, publicKeySpki, algorithm, usages, extractable) { |
| 36 this.type = type; | 66 this.type = type; |
| 37 this.spki = publicKeySpki; | 67 this.spki = publicKeySpki; |
| 38 this.algorithm = algorithm; | 68 this.algorithm = algorithm; |
| 39 this.usages = usages; | 69 this.usages = usages; |
| 40 this.extractable = extractable; | 70 this.extractable = extractable; |
| 41 }; | 71 }; |
| 42 | 72 |
| 43 var Key = | 73 var KeyBase = function() {}; |
| 44 utils.expose('Key', | 74 |
| 45 KeyImpl, | 75 Object.defineProperty(KeyBase.prototype, 'algorithm', { |
| 46 {readonly:['extractable', 'type', 'algorithm', 'usages']}); | 76 enumerable: true, |
| 77 get: function() { |
| 78 return deepCopy(privates(this).impl.algorithm); |
| 79 } |
| 80 }); |
| 81 |
| 82 var Key = utils.expose( |
| 83 'Key', |
| 84 KeyImpl, |
| 85 {superclass: KeyBase, readonly: ['extractable', 'type', 'usages']}); |
| 47 | 86 |
| 48 /** | 87 /** |
| 49 * Returns |key|'s Subject Public Key Info. Throws an exception if |key| is not | 88 * Returns |key|'s Subject Public Key Info. Throws an exception if |key| is not |
| 50 * a valid Key object. | 89 * a valid Key object. |
| 51 * @param {Key} key | 90 * @param {Key} key |
| 52 * @return {ArrayBuffer} The Subject Public Key Info in DER encoding of |key|. | 91 * @return {ArrayBuffer} The Subject Public Key Info in DER encoding of |key|. |
| 53 */ | 92 */ |
| 54 function getSpki(key) { | 93 function getSpki(key) { |
| 55 if (!privates(key)) | 94 if (!privates(key)) |
| 56 throw new Error('Invalid key object.'); | 95 throw new Error('Invalid key object.'); |
| 57 var keyImpl = privates(key).impl; | 96 var keyImpl = privates(key).impl; |
| 58 if (!keyImpl || !keyImpl.spki) | 97 if (!keyImpl || !keyImpl.spki) |
| 59 throw new Error('Invalid key object.'); | 98 throw new Error('Invalid key object.'); |
| 60 return keyImpl.spki; | 99 return keyImpl.spki; |
| 61 } | 100 } |
| 62 | 101 |
| 63 exports.Key = Key; | 102 exports.Key = Key; |
| 64 exports.KeyType = KeyType; | 103 exports.KeyType = KeyType; |
| 65 exports.KeyUsage = KeyUsage; | 104 exports.KeyUsage = KeyUsage; |
| 66 exports.getSpki = getSpki; | 105 exports.getSpki = getSpki; |
| OLD | NEW |