summaryrefslogtreecommitdiffstats
path: root/services/crypto/tests/unit/test_jwcrypto.js
blob: 2c51395bb3fc95f54da247556abd370e6e9f256f (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");

ChromeUtils.defineModuleGetter(
  this,
  "jwcrypto",
  "resource://services-crypto/jwcrypto.jsm"
);

XPCOMUtils.defineLazyServiceGetter(
  this,
  "CryptoService",
  "@mozilla.org/identity/crypto-service;1",
  "nsIIdentityCryptoService"
);

Cu.importGlobalProperties(["crypto"]);

const RP_ORIGIN = "http://123done.org";
const INTERNAL_ORIGIN = "browserid://";

const SECOND_MS = 1000;
const MINUTE_MS = SECOND_MS * 60;
const HOUR_MS = MINUTE_MS * 60;

// Enable logging from jwcrypto.jsm.
Services.prefs.setCharPref("services.crypto.jwcrypto.log.level", "Debug");

function promisify(fn) {
  return (...args) => {
    return new Promise((res, rej) => {
      fn(...args, (err, result) => {
        err ? rej(err) : res(result);
      });
    });
  };
}
const generateKeyPair = promisify(jwcrypto.generateKeyPair);
const generateAssertion = promisify(jwcrypto.generateAssertion);

add_task(async function test_jwe_roundtrip_ecdh_es_encryption() {
  const plaintext = crypto.getRandomValues(new Uint8Array(123));
  const remoteKey = await crypto.subtle.generateKey(
    {
      name: "ECDH",
      namedCurve: "P-256",
    },
    true,
    ["deriveKey"]
  );
  const remoteJWK = await crypto.subtle.exportKey("jwk", remoteKey.publicKey);
  delete remoteJWK.key_ops;
  const jwe = await jwcrypto.generateJWE(remoteJWK, plaintext);
  const decrypted = await jwcrypto.decryptJWE(jwe, remoteKey.privateKey);
  Assert.deepEqual(plaintext, decrypted);
});

add_task(async function test_jwe_header_includes_key_id() {
  const plaintext = crypto.getRandomValues(new Uint8Array(123));
  const remoteKey = await crypto.subtle.generateKey(
    {
      name: "ECDH",
      namedCurve: "P-256",
    },
    true,
    ["deriveKey"]
  );
  const remoteJWK = await crypto.subtle.exportKey("jwk", remoteKey.publicKey);
  delete remoteJWK.key_ops;
  remoteJWK.kid = "key identifier";
  const jwe = await jwcrypto.generateJWE(remoteJWK, plaintext);
  let [header /* other items deliberately ignored */] = jwe.split(".");
  header = JSON.parse(
    new TextDecoder().decode(
      ChromeUtils.base64URLDecode(header, { padding: "reject" })
    )
  );
  Assert.equal(header.kid, "key identifier");
});

add_task(async function test_sanity() {
  // Shouldn't reject.
  await generateKeyPair("DS160");
});

add_task(async function test_generate() {
  let kp = await generateKeyPair("DS160");
  Assert.notEqual(kp, null);
});

add_task(async function test_get_assertion() {
  let kp = await generateKeyPair("DS160");
  let backedAssertion = await generateAssertion("fake-cert", kp, RP_ORIGIN);
  Assert.equal(backedAssertion.split("~").length, 2);
  Assert.equal(backedAssertion.split(".").length, 3);
});

add_task(async function test_rsa() {
  let kpo = await generateKeyPair("RS256");
  Assert.notEqual(kpo, undefined);
  info(kpo.serializedPublicKey);
  let pk = JSON.parse(kpo.serializedPublicKey);
  Assert.equal(pk.algorithm, "RS");
  /* TODO
  do_check_neq(kpo.sign, null);
  do_check_eq(typeof kpo.sign, "function");
  do_check_neq(kpo.userID, null);
  do_check_neq(kpo.url, null);
  do_check_eq(kpo.url, INTERNAL_ORIGIN);
  do_check_neq(kpo.exponent, null);
  do_check_neq(kpo.modulus, null);

  // TODO: should sign be async?
  let sig = kpo.sign("This is a message to sign");

  do_check_neq(sig, null);
  do_check_eq(typeof sig, "string");
  do_check_true(sig.length > 1);
  */
});

add_task(async function test_dsa() {
  let kpo = await generateKeyPair("DS160");
  info(kpo.serializedPublicKey);
  let pk = JSON.parse(kpo.serializedPublicKey);
  Assert.equal(pk.algorithm, "DS");
  /* TODO
  do_check_neq(kpo.sign, null);
  do_check_eq(typeof kpo.sign, "function");
  do_check_neq(kpo.userID, null);
  do_check_neq(kpo.url, null);
  do_check_eq(kpo.url, INTERNAL_ORIGIN);
  do_check_neq(kpo.generator, null);
  do_check_neq(kpo.prime, null);
  do_check_neq(kpo.subPrime, null);
  do_check_neq(kpo.publicValue, null);

  let sig = kpo.sign("This is a message to sign");

  do_check_neq(sig, null);
  do_check_eq(typeof sig, "string");
  do_check_true(sig.length > 1);
  */
});

add_task(async function test_get_assertion_with_offset() {
  // Use an arbitrary date in the past to ensure we don't accidentally pass
  // this test with current dates, missing offsets, etc.
  let serverMsec = Date.parse("Tue Oct 31 2000 00:00:00 GMT-0800");

  // local clock skew
  // clock is 12 hours fast; -12 hours offset must be applied
  let localtimeOffsetMsec = -1 * 12 * HOUR_MS;
  let localMsec = serverMsec - localtimeOffsetMsec;

  let kp = await generateKeyPair("DS160");
  let backedAssertion = await generateAssertion("fake-cert", kp, RP_ORIGIN, {
    duration: MINUTE_MS,
    localtimeOffsetMsec,
    now: localMsec,
  });
  // properly formed
  let cert;
  let assertion;
  [cert, assertion] = backedAssertion.split("~");

  Assert.equal(cert, "fake-cert");
  Assert.equal(assertion.split(".").length, 3);

  let components = extractComponents(assertion);

  // Expiry is within two minutes, corrected for skew
  let exp = parseInt(components.payload.exp, 10);
  Assert.ok(exp - serverMsec === MINUTE_MS);
});

add_task(async function test_assertion_lifetime() {
  let kp = await generateKeyPair("DS160");
  let backedAssertion = await generateAssertion("fake-cert", kp, RP_ORIGIN, {
    duration: MINUTE_MS,
  });
  // properly formed
  let cert;
  let assertion;
  [cert, assertion] = backedAssertion.split("~");

  Assert.equal(cert, "fake-cert");
  Assert.equal(assertion.split(".").length, 3);

  let components = extractComponents(assertion);

  // Expiry is within one minute, as we specified above
  let exp = parseInt(components.payload.exp, 10);
  Assert.ok(Math.abs(Date.now() - exp) > 50 * SECOND_MS);
  Assert.ok(Math.abs(Date.now() - exp) <= MINUTE_MS);
});

add_task(async function test_audience_encoding_bug972582() {
  let audience = "i-like-pie.com";
  let kp = await generateKeyPair("DS160");
  let backedAssertion = await generateAssertion("fake-cert", kp, audience);
  let [, /* cert */ assertion] = backedAssertion.split("~");
  let components = extractComponents(assertion);
  Assert.equal(components.payload.aud, audience);
});

function extractComponents(signedObject) {
  if (typeof signedObject != "string") {
    throw new Error("malformed signature " + typeof signedObject);
  }

  let parts = signedObject.split(".");
  if (parts.length != 3) {
    throw new Error(
      "signed object must have three parts, this one has " + parts.length
    );
  }

  let headerSegment = parts[0];
  let payloadSegment = parts[1];
  let cryptoSegment = parts[2];

  let header = JSON.parse(base64UrlDecode(headerSegment));
  let payload = JSON.parse(base64UrlDecode(payloadSegment));

  // Ensure well-formed header
  Assert.equal(Object.keys(header).length, 1);
  Assert.ok(!!header.alg);

  // Ensure well-formed payload
  for (let field of ["exp", "aud"]) {
    Assert.ok(!!payload[field]);
  }

  return { header, payload, headerSegment, payloadSegment, cryptoSegment };
}