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 3// found in the LICENSE file. 4 5var utils = require('utils'); 6 7/** 8 * Enum of possible key types (subset of WebCrypto.KeyType). 9 * @enum {string} 10 */ 11var KeyType = { 12 public: 'public', 13 private: 'private' 14}; 15 16/** 17 * Enum of possible key usages (subset of WebCrypto.KeyUsage). 18 * @enum {string} 19 */ 20var KeyUsage = { 21 sign: 'sign', 22 verify: 'verify' 23}; 24 25/** 26 * Implementation of WebCrypto.Key used in enterprise.platformKeys. 27 * @param {KeyType} type The type of the new key. 28 * @param {ArrayBuffer} publicKeySpki The Subject Public Key Info in DER 29 * encoding. 30 * @param {KeyAlgorithm} algorithm The algorithm identifier. 31 * @param {KeyUsage[]} usages The allowed key usages. 32 * @param {boolean} extractable Whether the key is extractable. 33 * @constructor 34 */ 35var KeyImpl = function(type, publicKeySpki, algorithm, usages, extractable) { 36 this.type = type; 37 this.spki = publicKeySpki; 38 this.algorithm = algorithm; 39 this.usages = usages; 40 this.extractable = extractable; 41}; 42 43var KeyBase = function() {}; 44 45Object.defineProperty(KeyBase.prototype, 'algorithm', { 46 enumerable: true, 47 get: function() { 48 return utils.deepCopy(privates(this).impl.algorithm); 49 } 50}); 51 52var Key = utils.expose( 53 'Key', 54 KeyImpl, 55 {superclass: KeyBase, readonly: ['extractable', 'type', 'usages']}); 56 57/** 58 * Returns |key|'s Subject Public Key Info. Throws an exception if |key| is not 59 * a valid Key object. 60 * @param {Key} key 61 * @return {ArrayBuffer} The Subject Public Key Info in DER encoding of |key|. 62 */ 63function getSpki(key) { 64 if (!privates(key)) 65 throw new Error('Invalid key object.'); 66 var keyImpl = privates(key).impl; 67 if (!keyImpl || !keyImpl.spki) 68 throw new Error('Invalid key object.'); 69 return keyImpl.spki; 70} 71 72exports.Key = Key; 73exports.KeyType = KeyType; 74exports.KeyUsage = KeyUsage; 75exports.getSpki = getSpki; 76