Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | // -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | // This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | // License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | // file, You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | let gFetchCount = 0; |
michael@0 | 8 | let gGoodOCSPResponse = null; |
michael@0 | 9 | |
michael@0 | 10 | function generateGoodOCSPResponse() { |
michael@0 | 11 | let args = [ ["good", "localhostAndExampleCom", "unused" ] ]; |
michael@0 | 12 | let responses = generateOCSPResponses(args, "tlsserver"); |
michael@0 | 13 | return responses[0]; |
michael@0 | 14 | } |
michael@0 | 15 | |
michael@0 | 16 | function run_test() { |
michael@0 | 17 | do_get_profile(); |
michael@0 | 18 | Services.prefs.setBoolPref("security.ssl.enable_ocsp_stapling", true); |
michael@0 | 19 | add_tls_server_setup("OCSPStaplingServer"); |
michael@0 | 20 | |
michael@0 | 21 | let ocspResponder = new HttpServer(); |
michael@0 | 22 | ocspResponder.registerPrefixHandler("/", function(request, response) { |
michael@0 | 23 | ++gFetchCount; |
michael@0 | 24 | |
michael@0 | 25 | do_print("gFetchCount: " + gFetchCount); |
michael@0 | 26 | |
michael@0 | 27 | if (gFetchCount != 2) { |
michael@0 | 28 | do_print("returning 500 Internal Server Error"); |
michael@0 | 29 | |
michael@0 | 30 | response.setStatusLine(request.httpVersion, 500, "Internal Server Error"); |
michael@0 | 31 | let body = "Refusing to return a response"; |
michael@0 | 32 | response.bodyOutputStream.write(body, body.length); |
michael@0 | 33 | return; |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | do_print("returning 200 OK"); |
michael@0 | 37 | response.setStatusLine(request.httpVersion, 200, "OK"); |
michael@0 | 38 | response.setHeader("Content-Type", "application/ocsp-response"); |
michael@0 | 39 | response.write(gGoodOCSPResponse); |
michael@0 | 40 | }); |
michael@0 | 41 | ocspResponder.start(8080); |
michael@0 | 42 | |
michael@0 | 43 | add_tests_in_mode(true); |
michael@0 | 44 | add_tests_in_mode(false); |
michael@0 | 45 | |
michael@0 | 46 | add_test(function() { ocspResponder.stop(run_next_test); }); |
michael@0 | 47 | run_next_test(); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | function add_tests_in_mode(useMozillaPKIX) { |
michael@0 | 51 | add_test(function () { |
michael@0 | 52 | Services.prefs.setBoolPref("security.use_mozillapkix_verification", |
michael@0 | 53 | useMozillaPKIX); |
michael@0 | 54 | run_next_test(); |
michael@0 | 55 | }); |
michael@0 | 56 | |
michael@0 | 57 | // This test assumes that OCSPStaplingServer uses the same cert for |
michael@0 | 58 | // ocsp-stapling-unknown.example.com and ocsp-stapling-none.example.com. |
michael@0 | 59 | |
michael@0 | 60 | // Get an Unknown response for the *.exmaple.com cert and put it in the |
michael@0 | 61 | // OCSP cache. |
michael@0 | 62 | add_connection_test("ocsp-stapling-unknown.example.com", |
michael@0 | 63 | getXPCOMStatusFromNSS(SEC_ERROR_OCSP_UNKNOWN_CERT), |
michael@0 | 64 | clearSessionCache); |
michael@0 | 65 | add_test(function() { do_check_eq(gFetchCount, 0); run_next_test(); }); |
michael@0 | 66 | |
michael@0 | 67 | // A failure to retrieve an OCSP response must result in the cached Unkown |
michael@0 | 68 | // response being recognized and honored. |
michael@0 | 69 | add_connection_test("ocsp-stapling-none.example.com", |
michael@0 | 70 | getXPCOMStatusFromNSS(SEC_ERROR_OCSP_UNKNOWN_CERT), |
michael@0 | 71 | clearSessionCache); |
michael@0 | 72 | add_test(function() { do_check_eq(gFetchCount, 1); run_next_test(); }); |
michael@0 | 73 | |
michael@0 | 74 | // A valid Good response from the OCSP responder must override the cached |
michael@0 | 75 | // Unknown response. |
michael@0 | 76 | // |
michael@0 | 77 | // Note that We need to make sure that the Unknown response and the Good |
michael@0 | 78 | // response have different thisUpdate timestamps; otherwise, the Good |
michael@0 | 79 | // response will be seen as "not newer" and it won't replace the existing |
michael@0 | 80 | // entry. |
michael@0 | 81 | add_test(function() { |
michael@0 | 82 | let duration = 1200; |
michael@0 | 83 | do_print("Sleeping for " + duration + "ms"); |
michael@0 | 84 | let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); |
michael@0 | 85 | timer.initWithCallback(run_next_test, duration, Ci.nsITimer.TYPE_ONE_SHOT); |
michael@0 | 86 | }); |
michael@0 | 87 | add_test(function() { |
michael@0 | 88 | gGoodOCSPResponse = generateGoodOCSPResponse(); |
michael@0 | 89 | run_next_test(); |
michael@0 | 90 | }); |
michael@0 | 91 | add_connection_test("ocsp-stapling-none.example.com", Cr.NS_OK, |
michael@0 | 92 | clearSessionCache); |
michael@0 | 93 | add_test(function() { do_check_eq(gFetchCount, 2); run_next_test(); }); |
michael@0 | 94 | |
michael@0 | 95 | // The Good response retrieved from the previous fetch must have replaced |
michael@0 | 96 | // the Unknown response in the cache, resulting in the catched Good response |
michael@0 | 97 | // being returned and no fetch. |
michael@0 | 98 | add_connection_test("ocsp-stapling-none.example.com", Cr.NS_OK, |
michael@0 | 99 | clearSessionCache); |
michael@0 | 100 | add_test(function() { do_check_eq(gFetchCount, 2); run_next_test(); }); |
michael@0 | 101 | |
michael@0 | 102 | |
michael@0 | 103 | //--------------------------------------------------------------------------- |
michael@0 | 104 | |
michael@0 | 105 | // Reset state |
michael@0 | 106 | add_test(function() { clearOCSPCache(); gFetchCount = 0; run_next_test(); }); |
michael@0 | 107 | |
michael@0 | 108 | // A failure to retrieve an OCSP response will result in an error entry being |
michael@0 | 109 | // added to the cache. |
michael@0 | 110 | add_connection_test("ocsp-stapling-none.example.com", Cr.NS_OK, |
michael@0 | 111 | clearSessionCache); |
michael@0 | 112 | add_test(function() { do_check_eq(gFetchCount, 1); run_next_test(); }); |
michael@0 | 113 | |
michael@0 | 114 | // The error entry will prevent a fetch from happening for a while. |
michael@0 | 115 | add_connection_test("ocsp-stapling-none.example.com", Cr.NS_OK, |
michael@0 | 116 | clearSessionCache); |
michael@0 | 117 | add_test(function() { do_check_eq(gFetchCount, 1); run_next_test(); }); |
michael@0 | 118 | |
michael@0 | 119 | // The error entry must not prevent a stapled OCSP response from being |
michael@0 | 120 | // honored. |
michael@0 | 121 | add_connection_test("ocsp-stapling-revoked.example.com", |
michael@0 | 122 | getXPCOMStatusFromNSS(SEC_ERROR_REVOKED_CERTIFICATE), |
michael@0 | 123 | clearSessionCache); |
michael@0 | 124 | add_test(function() { do_check_eq(gFetchCount, 1); run_next_test(); }); |
michael@0 | 125 | |
michael@0 | 126 | //--------------------------------------------------------------------------- |
michael@0 | 127 | |
michael@0 | 128 | // Reset state |
michael@0 | 129 | add_test(function() { clearOCSPCache(); gFetchCount = 0; run_next_test(); }); |
michael@0 | 130 | } |