1 // -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
2 // This Source Code Form is subject to the terms of the Mozilla Public
3 // License, v. 2.0. If a copy of the MPL was not distributed with this
4 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 // Tests that the SDR implementation is able to decrypt strings encrypted using
9 // a preexisting NSS key database that has a password.
10 // To create such a database, run Firefox (or xpcshell), set a primary
11 // password, and then encrypt something using nsISecretDecoderRing.
14 passwordToTry: "password",
17 // This intentionally does not use arrow function syntax to avoid an issue
18 // where in the context of the arrow function, |this != gMockPrompter| due to
19 // how objects get wrapped when going across xpcom boundaries.
20 promptPassword(dialogTitle, text, password, checkMsg) {
22 if (this.numPrompts > 1) {
23 // don't keep retrying a bad password
28 "Please enter your Primary Password.",
29 "password prompt text should be as expected"
31 equal(checkMsg, null, "checkMsg should be null");
32 ok(this.passwordToTry, "passwordToTry should be non-null");
33 password.value = this.passwordToTry;
37 QueryInterface: ChromeUtils.generateQI(["nsIPrompt"]),
40 // Mock nsIWindowWatcher. PSM calls getNewPrompter on this to get an nsIPrompt
41 // to call promptPassword. We return the mock one, above.
42 var gWindowWatcher = {
43 getNewPrompter: () => gMockPrompter,
44 QueryInterface: ChromeUtils.generateQI(["nsIWindowWatcher"]),
48 let windowWatcherCID = MockRegistrar.register(
49 "@mozilla.org/embedcomp/window-watcher;1",
52 registerCleanupFunction(() => {
53 MockRegistrar.unregister(windowWatcherCID);
56 // Append a single quote and non-ASCII characters to the profile path.
57 let profd = Services.env.get("XPCSHELL_TEST_PROFILE_DIR");
58 let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
59 file.initWithPath(profd);
61 Services.env.set("XPCSHELL_TEST_PROFILE_DIR", file.path);
63 let profile = do_get_profile(); // must be called before getting nsIX509CertDB
65 /[^\x20-\x7f]/.test(profile.path),
66 "the profile path should contain a non-ASCII character"
69 let key4DBFile = do_get_file("test_sdr_preexisting_with_password/key4.db");
70 key4DBFile.copyTo(profile, "key4.db");
72 let sdr = Cc["@mozilla.org/security/sdr;1"].getService(
73 Ci.nsISecretDecoderRing
77 // a full padding block
80 "MDoEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECGeDHwVfyFqzBBAYvqMq/kDMsrARVNdC1C8d",
81 plaintext: "password",
86 "MDIEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECCAzLDVmYG2/BAh3IoIsMmT8dQ==",
92 "MDIEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECPN8zlZzn8FdBAiu2acpT8UHsg==",
98 "MDIEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECD5px1eMKkJQBAgUPp35GlrDvQ==",
101 // 2 bytes of padding
104 "MDIEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECMh0hLtKDyUdBAixw9UZsMt+vA==",
107 // long plaintext requiring more than two blocks
110 "MFoEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECDRX1qi+/FX1BDATFIcIneQjvBuq3wdFxzllJt2VtUD69ACdOKAXH3eA87oHDvuHqOeCDwRy4UzoG5s=",
111 plaintext: "thisismuchlongerandsotakesupmultipleblocks",
113 // this differs from the previous ciphertext by one bit and demonstrates
114 // that this implementation does not enforce message integrity
117 "MFoEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECDRX1qi+/FX1BDAbFIcIneQjvBuq3wdFxzllJt2VtUD69ACdOKAXH3eA87oHDvuHqOeCDwRy4UzoG5s=",
118 plaintext: "nnLbuwLRkhlongerandsotakesupmultipleblocks",
122 for (let testcase of testcases) {
123 let decrypted = sdr.decryptString(testcase.ciphertext);
127 "decrypted ciphertext should match expected plaintext"
131 gMockPrompter.numPrompts,
133 "Should have been prompted for a password once"