summaryrefslogtreecommitdiffstats
path: root/dom/crypto/test/util.js
blob: b3e8657c267164027519a98dd0c74b0ab1b7a1f0 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var util = {
  // Compare the contents of two ArrayBuffer(View)s
  memcmp: function util_memcmp(x, y) {
    if (!x || !y) {
      return false;
    }

    var xb = new Uint8Array(x);
    var yb = new Uint8Array(y);
    if (x.byteLength !== y.byteLength) {
      return false;
    }

    for (var i = 0; i < xb.byteLength; ++i) {
      if (xb[i] !== yb[i]) {
        return false;
      }
    }
    return true;
  },

  // Convert an ArrayBufferView to a hex string
  abv2hex: function util_abv2hex(abv) {
    var b = new Uint8Array(abv);
    var hex = "";
    for (var i = 0; i < b.length; ++i) {
      var zeropad = b[i] < 0x10 ? "0" : "";
      hex += zeropad + b[i].toString(16);
    }
    return hex;
  },

  // Convert a hex string to an ArrayBufferView
  hex2abv: function util_hex2abv(hex) {
    if (hex.length % 2 !== 0) {
      hex = "0" + hex;
    }

    var abv = new Uint8Array(hex.length / 2);
    for (var i = 0; i < abv.length; ++i) {
      abv[i] = parseInt(hex.substr(2 * i, 2), 16);
    }
    return abv;
  },

  clone(obj) {
    return new Promise(resolve => {
      let { port1, port2 } = new MessageChannel();

      // Wait for the cloned object to arrive.
      port1.onmessage = msg => resolve(msg.data);

      // Clone the object.
      port2.postMessage(obj);
    });
  },

  cloneExportCompareKeys(key) {
    return util.clone(key).then(clone => {
      var exports = [];

      if (key instanceof CryptoKey) {
        exports.push(crypto.subtle.exportKey("raw", key));
        exports.push(crypto.subtle.exportKey("raw", clone));
      } else {
        exports.push(crypto.subtle.exportKey("spki", key.publicKey));
        exports.push(crypto.subtle.exportKey("spki", clone.publicKey));
        exports.push(crypto.subtle.exportKey("pkcs8", key.privateKey));
        exports.push(crypto.subtle.exportKey("pkcs8", clone.privateKey));
      }

      return Promise.all(exports).then(pairs => {
        for (var i = 0; i < pairs.length; i += 2) {
          if (!util.memcmp(pairs[i], pairs[i + 1])) {
            throw new Error("keys don't match");
          }
        }

        return clone;
      });
    });
  },
};

function exists(x) {
  return x !== undefined;
}

function hasFields(object, fields) {
  return fields.map(x => exists(object[x])).reduce((x, y) => x && y);
}

function hasKeyFields(x) {
  return hasFields(x, ["algorithm", "extractable", "type", "usages"]);
}

function hasBaseJwkFields(x) {
  return hasFields(x, ["kty", "alg", "ext", "key_ops"]);
}

function shallowArrayEquals(x, y) {
  if (x.length != y.length) {
    return false;
  }

  for (let i in x) {
    if (x[i] != y[i]) {
      return false;
    }
  }

  return true;
}