summaryrefslogtreecommitdiffstats
path: root/security/manager/ssl/tests/unit/test_ocsp_stapling.js
blob: 990f28628983fe2333066eab5dba84d0edf27a07 (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
// -*- 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";

// In which we connect to a number of domains (as faked by a server running
// locally) with and without OCSP stapling enabled to determine that good
// things happen and bad things don't.

var gExpectOCSPRequest;

function add_ocsp_test(
  aHost,
  aExpectedResult,
  aStaplingEnabled,
  aExpectOCSPRequest = false
) {
  add_connection_test(aHost, aExpectedResult, function () {
    gExpectOCSPRequest = aExpectOCSPRequest;
    clearOCSPCache();
    clearSessionCache();
    Services.prefs.setBoolPref(
      "security.ssl.enable_ocsp_stapling",
      aStaplingEnabled
    );
  });
}

function add_tests() {
  // In the absence of OCSP stapling, these should actually all work.
  add_ocsp_test(
    "ocsp-stapling-good.example.com",
    PRErrorCodeSuccess,
    false,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-revoked.example.com",
    PRErrorCodeSuccess,
    false,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-good-other-ca.example.com",
    PRErrorCodeSuccess,
    false,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-malformed.example.com",
    PRErrorCodeSuccess,
    false,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-srverr.example.com",
    PRErrorCodeSuccess,
    false,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-trylater.example.com",
    PRErrorCodeSuccess,
    false,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-needssig.example.com",
    PRErrorCodeSuccess,
    false,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-unauthorized.example.com",
    PRErrorCodeSuccess,
    false,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-unknown.example.com",
    PRErrorCodeSuccess,
    false,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-good-other.example.com",
    PRErrorCodeSuccess,
    false,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-none.example.com",
    PRErrorCodeSuccess,
    false,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-expired.example.com",
    PRErrorCodeSuccess,
    false,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-expired-fresh-ca.example.com",
    PRErrorCodeSuccess,
    false,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-skip-responseBytes.example.com",
    PRErrorCodeSuccess,
    false,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-critical-extension.example.com",
    PRErrorCodeSuccess,
    false,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-noncritical-extension.example.com",
    PRErrorCodeSuccess,
    false,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-empty-extensions.example.com",
    PRErrorCodeSuccess,
    false,
    true
  );

  // Now test OCSP stapling
  // The following error codes are defined in security/nss/lib/util/SECerrs.h

  add_ocsp_test("ocsp-stapling-good.example.com", PRErrorCodeSuccess, true);

  add_ocsp_test(
    "ocsp-stapling-revoked.example.com",
    SEC_ERROR_REVOKED_CERTIFICATE,
    true
  );

  // This stapled response is from a CA that is untrusted and did not issue
  // the server's certificate.
  let certDB = Cc["@mozilla.org/security/x509certdb;1"].getService(
    Ci.nsIX509CertDB
  );
  let otherTestCA = constructCertFromFile("ocsp_certs/other-test-ca.pem");
  add_test(function () {
    certDB.setCertTrust(
      otherTestCA,
      Ci.nsIX509Cert.CA_CERT,
      Ci.nsIX509CertDB.UNTRUSTED
    );
    run_next_test();
  });
  add_ocsp_test(
    "ocsp-stapling-good-other-ca.example.com",
    SEC_ERROR_OCSP_INVALID_SIGNING_CERT,
    true,
    true
  );

  // The stapled response is from a CA that is trusted but did not issue the
  // server's certificate.
  add_test(function () {
    certDB.setCertTrust(
      otherTestCA,
      Ci.nsIX509Cert.CA_CERT,
      Ci.nsIX509CertDB.TRUSTED_SSL
    );
    run_next_test();
  });
  // TODO(bug 979055): When using ByName instead of ByKey, the error here is
  // SEC_ERROR_OCSP_UNAUTHORIZED_RESPONSE. We should be testing both cases.
  add_ocsp_test(
    "ocsp-stapling-good-other-ca.example.com",
    SEC_ERROR_OCSP_INVALID_SIGNING_CERT,
    true,
    true
  );

  // TODO: Test the case where the signing cert can't be found at all, which
  // will result in SEC_ERROR_BAD_DATABASE in the NSS classic case.

  add_ocsp_test(
    "ocsp-stapling-malformed.example.com",
    SEC_ERROR_OCSP_MALFORMED_REQUEST,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-srverr.example.com",
    SEC_ERROR_OCSP_SERVER_ERROR,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-trylater.example.com",
    SEC_ERROR_OCSP_TRY_SERVER_LATER,
    true,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-needssig.example.com",
    SEC_ERROR_OCSP_REQUEST_NEEDS_SIG,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-unauthorized.example.com",
    SEC_ERROR_OCSP_UNAUTHORIZED_REQUEST,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-unknown.example.com",
    SEC_ERROR_OCSP_UNKNOWN_CERT,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-good-other.example.com",
    MOZILLA_PKIX_ERROR_OCSP_RESPONSE_FOR_CERT_MISSING,
    true
  );
  // If the server doesn't staple an OCSP response, we continue as normal
  // (this means that even though stapling is enabled, we expect an OCSP
  // request).
  add_connection_test(
    "ocsp-stapling-none.example.com",
    PRErrorCodeSuccess,
    function () {
      gExpectOCSPRequest = true;
      clearOCSPCache();
      clearSessionCache();
      Services.prefs.setBoolPref("security.ssl.enable_ocsp_stapling", true);
    }
  );
  add_ocsp_test(
    "ocsp-stapling-empty.example.com",
    SEC_ERROR_OCSP_MALFORMED_RESPONSE,
    true
  );

  add_ocsp_test(
    "ocsp-stapling-skip-responseBytes.example.com",
    SEC_ERROR_OCSP_MALFORMED_RESPONSE,
    true
  );

  add_ocsp_test(
    "ocsp-stapling-critical-extension.example.com",
    SEC_ERROR_UNKNOWN_CRITICAL_EXTENSION,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-noncritical-extension.example.com",
    PRErrorCodeSuccess,
    true
  );
  // TODO(bug 997994): Disallow empty Extensions in responses
  add_ocsp_test(
    "ocsp-stapling-empty-extensions.example.com",
    PRErrorCodeSuccess,
    true
  );

  add_ocsp_test(
    "ocsp-stapling-delegated-included.example.com",
    PRErrorCodeSuccess,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-delegated-included-last.example.com",
    PRErrorCodeSuccess,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-delegated-missing.example.com",
    SEC_ERROR_OCSP_INVALID_SIGNING_CERT,
    true,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-delegated-missing-multiple.example.com",
    SEC_ERROR_OCSP_INVALID_SIGNING_CERT,
    true,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-delegated-no-extKeyUsage.example.com",
    SEC_ERROR_OCSP_INVALID_SIGNING_CERT,
    true,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-delegated-from-intermediate.example.com",
    SEC_ERROR_OCSP_INVALID_SIGNING_CERT,
    true,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-delegated-keyUsage-crlSigning.example.com",
    SEC_ERROR_OCSP_INVALID_SIGNING_CERT,
    true,
    true
  );
  add_ocsp_test(
    "ocsp-stapling-delegated-wrong-extKeyUsage.example.com",
    SEC_ERROR_OCSP_INVALID_SIGNING_CERT,
    true,
    true
  );

  // ocsp-stapling-expired.example.com and
  // ocsp-stapling-expired-fresh-ca.example.com are handled in
  // test_ocsp_stapling_expired.js

  // Check that OCSP responder certificates with key sizes below 1024 bits are
  // rejected, even when the main certificate chain keys are at least 1024 bits.
  add_ocsp_test(
    "keysize-ocsp-delegated.example.com",
    SEC_ERROR_OCSP_INVALID_SIGNING_CERT,
    true,
    true
  );

  add_ocsp_test(
    "revoked-ca-cert-used-as-end-entity.example.com",
    SEC_ERROR_REVOKED_CERTIFICATE,
    true
  );
}

function check_ocsp_stapling_telemetry() {
  let histogram = Services.telemetry
    .getHistogramById("SSL_OCSP_STAPLING")
    .snapshot();
  equal(
    histogram.values[0],
    0,
    "Should have 0 connections for unused histogram bucket 0"
  );
  equal(
    histogram.values[1],
    5,
    "Actual and expected connections with a good response should match"
  );
  equal(
    histogram.values[2],
    18,
    "Actual and expected connections with no stapled response should match"
  );
  equal(
    histogram.values[3] || 0,
    0,
    "Actual and expected connections with an expired response should match"
  );
  equal(
    histogram.values[4],
    21,
    "Actual and expected connections with bad responses should match"
  );
  run_next_test();
}

function run_test() {
  do_get_profile();
  Services.prefs.setIntPref("security.OCSP.enabled", 1);
  // This test may sometimes fail on android due to an OCSP request timing out.
  // That aspect of OCSP requests is not what we're testing here, so we can just
  // bump the timeout and hopefully avoid these failures.
  Services.prefs.setIntPref("security.OCSP.timeoutMilliseconds.soft", 5000);

  let fakeOCSPResponder = new HttpServer();
  fakeOCSPResponder.registerPrefixHandler("/", function (request, response) {
    response.setStatusLine(request.httpVersion, 500, "Internal Server Error");
    ok(
      gExpectOCSPRequest,
      "Should be getting an OCSP request only when expected"
    );
  });
  fakeOCSPResponder.start(8888);

  add_tls_server_setup("OCSPStaplingServer", "ocsp_certs");

  add_tests();

  add_test(function () {
    fakeOCSPResponder.stop(check_ocsp_stapling_telemetry);
  });

  run_next_test();
}