test_sdr_preexisting.js (2732B)
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/. 5 6 "use strict"; 7 8 // Tests that the SDR implementation is able to decrypt strings encrypted using 9 // a preexisting NSS key database. Creating the database is straight-forward: 10 // simply run Firefox (or xpcshell) and encrypt something using 11 // nsISecretDecoderRing (e.g. by saving a password or directly using the 12 // interface). The resulting key4.db file (in the profile directory) now 13 // contains the private key used to encrypt the data. 14 15 function run_test() { 16 const keyDBName = "key4.db"; 17 let profile = do_get_profile(); 18 let keyDBFile = do_get_file(`test_sdr_preexisting/${keyDBName}`); 19 keyDBFile.copyTo(profile, keyDBName); 20 21 let sdr = Cc["@mozilla.org/security/sdr;1"].getService( 22 Ci.nsISecretDecoderRing 23 ); 24 25 let testcases = [ 26 // a full padding block 27 { 28 ciphertext: 29 "MDoEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECGeDHwVfyFqzBBAYvqMq/kDMsrARVNdC1C8d", 30 plaintext: "password", 31 }, 32 // 7 bytes of padding 33 { 34 ciphertext: 35 "MDIEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECCAzLDVmYG2/BAh3IoIsMmT8dQ==", 36 plaintext: "a", 37 }, 38 // 6 bytes of padding 39 { 40 ciphertext: 41 "MDIEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECPN8zlZzn8FdBAiu2acpT8UHsg==", 42 plaintext: "bb", 43 }, 44 // 1 byte of padding 45 { 46 ciphertext: 47 "MDIEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECD5px1eMKkJQBAgUPp35GlrDvQ==", 48 plaintext: "!seven!", 49 }, 50 // 2 bytes of padding 51 { 52 ciphertext: 53 "MDIEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECMh0hLtKDyUdBAixw9UZsMt+vA==", 54 plaintext: "sixsix", 55 }, 56 // long plaintext requiring more than two blocks 57 { 58 ciphertext: 59 "MFoEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECDRX1qi+/FX1BDATFIcIneQjvBuq3wdFxzllJt2VtUD69ACdOKAXH3eA87oHDvuHqOeCDwRy4UzoG5s=", 60 plaintext: "thisismuchlongerandsotakesupmultipleblocks", 61 }, 62 // this differs from the previous ciphertext by one bit and demonstrates 63 // that this implementation does not enforce message integrity 64 { 65 ciphertext: 66 "MFoEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECDRX1qi+/FX1BDAbFIcIneQjvBuq3wdFxzllJt2VtUD69ACdOKAXH3eA87oHDvuHqOeCDwRy4UzoG5s=", 67 plaintext: "nnLbuwLRkhlongerandsotakesupmultipleblocks", 68 }, 69 ]; 70 71 for (let testcase of testcases) { 72 let decrypted = sdr.decryptString(testcase.ciphertext); 73 equal( 74 decrypted, 75 testcase.plaintext, 76 "decrypted ciphertext should match expected plaintext" 77 ); 78 } 79 }