summaryrefslogtreecommitdiffstats
path: root/security/manager/ssl/tests/unit/test_ocsp_caching.js
blob: b964018518c34fe468ef9bbf07fa112da4294a41 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
// 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/.
"use strict";

// Checks various aspects of the OCSP cache, mainly to to ensure we do not fetch
// responses more than necessary.

var gFetchCount = 0;
var gGoodOCSPResponse = null;
var gResponsePattern = [];

function respondWithGoodOCSP(request, response) {
  info("returning 200 OK");
  response.setStatusLine(request.httpVersion, 200, "OK");
  response.setHeader("Content-Type", "application/ocsp-response");
  response.write(gGoodOCSPResponse);
}

function respondWithSHA1OCSP(request, response) {
  info("returning 200 OK with sha-1 delegated response");
  response.setStatusLine(request.httpVersion, 200, "OK");
  response.setHeader("Content-Type", "application/ocsp-response");

  let args = [["good-delegated", "default-ee", "delegatedSHA1Signer", 0]];
  let responses = generateOCSPResponses(args, "ocsp_certs");
  response.write(responses[0]);
}

function respondWithError(request, response) {
  info("returning 500 Internal Server Error");
  response.setStatusLine(request.httpVersion, 500, "Internal Server Error");
  let body = "Refusing to return a response";
  response.bodyOutputStream.write(body, body.length);
}

function generateGoodOCSPResponse(thisUpdateSkew) {
  let args = [["good", "default-ee", "unused", thisUpdateSkew]];
  let responses = generateOCSPResponses(args, "ocsp_certs");
  return responses[0];
}

function add_ocsp_test(
  aHost,
  aExpectedResult,
  aResponses,
  aMessage,
  aOriginAttributes
) {
  add_connection_test(
    aHost,
    aExpectedResult,
    function () {
      clearSessionCache();
      gFetchCount = 0;
      gResponsePattern = aResponses;
    },
    function () {
      // check the number of requests matches the size of aResponses
      equal(gFetchCount, aResponses.length, aMessage);
    },
    null,
    aOriginAttributes
  );
}

function run_test() {
  do_get_profile();
  Services.prefs.setBoolPref("security.ssl.enable_ocsp_stapling", true);
  Services.prefs.setIntPref("security.OCSP.enabled", 1);
  add_tls_server_setup("OCSPStaplingServer", "ocsp_certs");

  let ocspResponder = new HttpServer();
  ocspResponder.registerPrefixHandler("/", function (request, response) {
    info("gFetchCount: " + gFetchCount);
    let responseFunction = gResponsePattern[gFetchCount];
    Assert.notEqual(undefined, responseFunction);

    ++gFetchCount;
    responseFunction(request, response);
  });
  ocspResponder.start(8888);

  add_tests();

  add_test(function () {
    ocspResponder.stop(run_next_test);
  });
  run_next_test();
}

function add_tests() {
  // Test that verifying a certificate with a "short lifetime" doesn't result
  // in OCSP fetching. Due to longevity requirements in our testing
  // infrastructure, the certificate we encounter is valid for a very long
  // time, so we have to define a "short lifetime" as something very long.
  add_test(function () {
    Services.prefs.setIntPref(
      "security.pki.cert_short_lifetime_in_days",
      12000
    );
    run_next_test();
  });

  add_ocsp_test(
    "ocsp-stapling-none.example.com",
    PRErrorCodeSuccess,
    [],
    "expected zero OCSP requests for a short-lived certificate"
  );

  add_test(function () {
    Services.prefs.setIntPref("security.pki.cert_short_lifetime_in_days", 100);
    run_next_test();
  });

  // If a "short lifetime" is something more reasonable, ensure that we do OCSP
  // fetching for this long-lived certificate.

  add_ocsp_test(
    "ocsp-stapling-none.example.com",
    PRErrorCodeSuccess,
    [respondWithError],
    "expected one OCSP request for a long-lived certificate"
  );
  add_test(function () {
    Services.prefs.clearUserPref("security.pki.cert_short_lifetime_in_days");
    run_next_test();
  });
  // ---------------------------------------------------------------------------

  // Reset state
  add_test(function () {
    clearOCSPCache();
    run_next_test();
  });

  // This test assumes that OCSPStaplingServer uses the same cert for
  // ocsp-stapling-unknown.example.com and ocsp-stapling-none.example.com.

  // Get an Unknown response for the *.example.com cert and put it in the
  // OCSP cache.
  add_ocsp_test(
    "ocsp-stapling-unknown.example.com",
    SEC_ERROR_OCSP_UNKNOWN_CERT,
    [],
    "Stapled Unknown response -> a fetch should not have been attempted"
  );

  // A failure to retrieve an OCSP response must result in the cached Unknown
  // response being recognized and honored.
  add_ocsp_test(
    "ocsp-stapling-none.example.com",
    SEC_ERROR_OCSP_UNKNOWN_CERT,
    [respondWithError, respondWithError],
    "No stapled response -> a fetch should have been attempted"
  );

  // A valid Good response from the OCSP responder must override the cached
  // Unknown response.
  //
  // Note that We need to make sure that the Unknown response and the Good
  // response have different thisUpdate timestamps; otherwise, the Good
  // response will be seen as "not newer" and it won't replace the existing
  // entry.
  add_test(function () {
    gGoodOCSPResponse = generateGoodOCSPResponse(1200);
    run_next_test();
  });
  add_ocsp_test(
    "ocsp-stapling-none.example.com",
    PRErrorCodeSuccess,
    [respondWithGoodOCSP],
    "Cached Unknown response, no stapled response -> a fetch" +
      " should have been attempted"
  );

  // The Good response retrieved from the previous fetch must have replaced
  // the Unknown response in the cache, resulting in the catched Good response
  // being returned and no fetch.
  add_ocsp_test(
    "ocsp-stapling-none.example.com",
    PRErrorCodeSuccess,
    [],
    "Cached Good response -> a fetch should not have been attempted"
  );

  // ---------------------------------------------------------------------------

  // Reset state
  add_test(function () {
    clearOCSPCache();
    run_next_test();
  });

  // A failure to retrieve an OCSP response will result in an error entry being
  // added to the cache.
  add_ocsp_test(
    "ocsp-stapling-none.example.com",
    PRErrorCodeSuccess,
    [respondWithError],
    "No stapled response -> a fetch should have been attempted"
  );

  // The error entry will prevent a fetch from happening for a while.
  add_ocsp_test(
    "ocsp-stapling-none.example.com",
    PRErrorCodeSuccess,
    [],
    "Noted OCSP server failure -> a fetch should not have been attempted"
  );

  // The error entry must not prevent a stapled OCSP response from being
  // honored.
  add_ocsp_test(
    "ocsp-stapling-revoked.example.com",
    SEC_ERROR_REVOKED_CERTIFICATE,
    [],
    "Stapled Revoked response -> a fetch should not have been attempted"
  );

  // ---------------------------------------------------------------------------

  // Ensure OCSP responses from signers with SHA1 certificates are OK. This
  // is included in the OCSP caching tests since there were OCSP cache-related
  // regressions when sha-1 telemetry probes were added.
  add_test(function () {
    clearOCSPCache();
    // set security.OCSP.require so that checking the OCSP signature fails
    Services.prefs.setBoolPref("security.OCSP.require", true);
    run_next_test();
  });

  add_ocsp_test(
    "ocsp-stapling-none.example.com",
    SEC_ERROR_OCSP_INVALID_SIGNING_CERT,
    [respondWithSHA1OCSP],
    "OCSP signing cert was issued with sha1 - should fail"
  );

  add_test(function () {
    Services.prefs.setBoolPref("security.OCSP.require", false);
    run_next_test();
  });

  // ---------------------------------------------------------------------------

  // Reset state
  add_test(function () {
    clearOCSPCache();
    run_next_test();
  });

  // This test makes sure that OCSP cache are isolated by firstPartyDomain.

  let gObservedCnt = 0;
  let protocolProxyService = Cc[
    "@mozilla.org/network/protocol-proxy-service;1"
  ].getService(Ci.nsIProtocolProxyService);

  // Observe all channels and make sure the firstPartyDomain in their loadInfo's
  // origin attributes are aFirstPartyDomain.
  function startObservingChannels(aFirstPartyDomain) {
    // We use a dummy proxy filter to catch all channels, even those that do not
    // generate an "http-on-modify-request" notification.
    let proxyFilter = {
      applyFilter(aChannel, aProxy, aCallback) {
        // We have the channel; provide it to the callback.
        if (aChannel.originalURI.spec == "http://localhost:8888/") {
          gObservedCnt++;
          equal(
            aChannel.loadInfo.originAttributes.firstPartyDomain,
            aFirstPartyDomain,
            "firstPartyDomain should match"
          );
        }
        // Pass on aProxy unmodified.
        aCallback.onProxyFilterResult(aProxy);
      },
    };
    protocolProxyService.registerChannelFilter(proxyFilter, 0);
    // Return the stop() function:
    return () => protocolProxyService.unregisterChannelFilter(proxyFilter);
  }

  let stopObservingChannels;
  add_test(function () {
    stopObservingChannels = startObservingChannels("foo.com");
    run_next_test();
  });

  // A good OCSP response will be cached.
  add_ocsp_test(
    "ocsp-stapling-none.example.com",
    PRErrorCodeSuccess,
    [respondWithGoodOCSP],
    "No stapled response (firstPartyDomain = foo.com) -> a fetch " +
      "should have been attempted",
    { firstPartyDomain: "foo.com" }
  );

  // The cache will prevent a fetch from happening.
  add_ocsp_test(
    "ocsp-stapling-none.example.com",
    PRErrorCodeSuccess,
    [],
    "Noted OCSP server failure (firstPartyDomain = foo.com) -> a " +
      "fetch should not have been attempted",
    { firstPartyDomain: "foo.com" }
  );

  add_test(function () {
    stopObservingChannels();
    equal(gObservedCnt, 1, "should have observed only 1 OCSP requests");
    gObservedCnt = 0;
    run_next_test();
  });

  add_test(function () {
    stopObservingChannels = startObservingChannels("bar.com");
    run_next_test();
  });

  // But using a different firstPartyDomain should result in a fetch.
  add_ocsp_test(
    "ocsp-stapling-none.example.com",
    PRErrorCodeSuccess,
    [respondWithGoodOCSP],
    "No stapled response (firstPartyDomain = bar.com) -> a fetch " +
      "should have been attempted",
    { firstPartyDomain: "bar.com" }
  );

  add_test(function () {
    stopObservingChannels();
    equal(gObservedCnt, 1, "should have observed only 1 OCSP requests");
    gObservedCnt = 0;
    run_next_test();
  });

  // ---------------------------------------------------------------------------

  // Reset state
  add_test(function () {
    clearOCSPCache();
    run_next_test();
  });

  // Test that the OCSP cache is not isolated by userContextId.

  // A good OCSP response will be cached.
  add_ocsp_test(
    "ocsp-stapling-none.example.com",
    PRErrorCodeSuccess,
    [respondWithGoodOCSP],
    "No stapled response (userContextId = 1) -> a fetch " +
      "should have been attempted",
    { userContextId: 1 }
  );

  // The cache will prevent a fetch from happening.
  add_ocsp_test(
    "ocsp-stapling-none.example.com",
    PRErrorCodeSuccess,
    [],
    "Noted OCSP server failure (userContextId = 1) -> a " +
      "fetch should not have been attempted",
    { userContextId: 1 }
  );

  // Fetching is prevented even if in a different userContextId.
  add_ocsp_test(
    "ocsp-stapling-none.example.com",
    PRErrorCodeSuccess,
    [],
    "Noted OCSP server failure (userContextId = 2) -> a " +
      "fetch should not have been attempted",
    { userContextId: 2 }
  );

  // ---------------------------------------------------------------------------

  // Reset state
  add_test(function () {
    clearOCSPCache();
    run_next_test();
  });

  // This test makes sure that OCSP cache are isolated by partitionKey.

  add_test(function () {
    Services.prefs.setBoolPref(
      "privacy.partition.network_state.ocsp_cache",
      true
    );
    run_next_test();
  });

  // A good OCSP response will be cached.
  add_ocsp_test(
    "ocsp-stapling-none.example.com",
    PRErrorCodeSuccess,
    [respondWithGoodOCSP],
    "No stapled response (partitionKey = (https,foo.com)) -> a fetch " +
      "should have been attempted",
    { partitionKey: "(https,foo.com)" }
  );

  // The cache will prevent a fetch from happening.
  add_ocsp_test(
    "ocsp-stapling-none.example.com",
    PRErrorCodeSuccess,
    [],
    "Noted OCSP server failure (partitionKey = (https,foo.com)) -> a " +
      "fetch should not have been attempted",
    { partitionKey: "(https,foo.com)" }
  );

  // Using a different partitionKey should result in a fetch.
  add_ocsp_test(
    "ocsp-stapling-none.example.com",
    PRErrorCodeSuccess,
    [respondWithGoodOCSP],
    "Noted OCSP server failure (partitionKey = (https,bar.com)) -> a " +
      "fetch should have been attempted",
    { partitionKey: "(https,bar.com)" }
  );

  // ---------------------------------------------------------------------------

  // Reset state
  add_test(function () {
    Services.prefs.clearUserPref("privacy.partition.network_state.ocsp_cache");
    clearOCSPCache();
    run_next_test();
  });

  // This test makes sure that OCSP cache are isolated by partitionKey in
  // private mode.

  // A good OCSP response will be cached.
  add_ocsp_test(
    "ocsp-stapling-none.example.com",
    PRErrorCodeSuccess,
    [respondWithGoodOCSP],
    "No stapled response (partitionKey = (https,foo.com)) -> a fetch " +
      "should have been attempted",
    { partitionKey: "(https,foo.com)", privateBrowsingId: 1 }
  );

  // The cache will prevent a fetch from happening.
  add_ocsp_test(
    "ocsp-stapling-none.example.com",
    PRErrorCodeSuccess,
    [],
    "Noted OCSP server failure (partitionKey = (https,foo.com)) -> a " +
      "fetch should not have been attempted",
    { partitionKey: "(https,foo.com)", privateBrowsingId: 1 }
  );

  // Using a different partitionKey should result in a fetch.
  add_ocsp_test(
    "ocsp-stapling-none.example.com",
    PRErrorCodeSuccess,
    [respondWithGoodOCSP],
    "Noted OCSP server failure (partitionKey = (https,bar.com)) -> a " +
      "fetch should have been attempted",
    { partitionKey: "(https,bar.com)", privateBrowsingId: 1 }
  );

  // ---------------------------------------------------------------------------

  // Reset state
  add_test(function () {
    clearOCSPCache();
    run_next_test();
  });
}