summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/resources/extensions/enterprise_platform_keys/key.js
blob: e46bbcb3522ac0eef4c2d5f4903d0308ae415731 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

var utils = require('utils');

/**
 * Enum of possible key types (subset of WebCrypto.KeyType).
 * @enum {string}
 */
var KeyType = {
  public: 'public',
  private: 'private'
};

/**
 * Enum of possible key usages (subset of WebCrypto.KeyUsage).
 * @enum {string}
 */
var KeyUsage = {
  sign: 'sign',
  verify: 'verify'
};

/**
 * Implementation of WebCrypto.Key used in enterprise.platformKeys.
 * @param {KeyType} type The type of the new key.
 * @param {ArrayBuffer} publicKeySpki The Subject Public Key Info in DER
 *   encoding.
 * @param {KeyAlgorithm} algorithm The algorithm identifier.
 * @param {KeyUsage[]} usages The allowed key usages.
 * @param {boolean} extractable Whether the key is extractable.
 * @constructor
 */
var KeyImpl = function(type, publicKeySpki, algorithm, usages, extractable) {
  this.type = type;
  this.spki = publicKeySpki;
  this.algorithm = algorithm;
  this.usages = usages;
  this.extractable = extractable;
};

var KeyBase = function() {};

Object.defineProperty(KeyBase.prototype, 'algorithm', {
  enumerable: true,
  get: function() {
    return utils.deepCopy(privates(this).impl.algorithm);
  }
});

var Key = utils.expose(
    'Key',
    KeyImpl,
    {superclass: KeyBase, readonly: ['extractable', 'type', 'usages']});

/**
 * Returns |key|'s Subject Public Key Info. Throws an exception if |key| is not
 * a valid Key object.
 * @param {Key} key
 * @return {ArrayBuffer} The Subject Public Key Info in DER encoding of |key|.
 */
function getSpki(key) {
  if (!privates(key))
    throw new Error('Invalid key object.');
  var keyImpl = privates(key).impl;
  if (!keyImpl || !keyImpl.spki)
    throw new Error('Invalid key object.');
  return keyImpl.spki;
}

exports.Key = Key;
exports.KeyType = KeyType;
exports.KeyUsage = KeyUsage;
exports.getSpki = getSpki;