Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | function run_test() { |
michael@0 | 5 | |
michael@0 | 6 | /** |
michael@0 | 7 | * Signs a MAR file. |
michael@0 | 8 | * |
michael@0 | 9 | * @param inMAR The MAR file that should be signed |
michael@0 | 10 | * @param outMAR The MAR file to create |
michael@0 | 11 | */ |
michael@0 | 12 | function signMAR(inMAR, outMAR, certs, wantSuccess, useShortHandCmdLine) { |
michael@0 | 13 | // Get a process to the signmar binary from the dist/bin directory. |
michael@0 | 14 | let process = Cc["@mozilla.org/process/util;1"]. |
michael@0 | 15 | createInstance(Ci.nsIProcess); |
michael@0 | 16 | let signmarBin = do_get_file("signmar" + BIN_SUFFIX); |
michael@0 | 17 | |
michael@0 | 18 | // Make sure the signmar binary exists and is an executable. |
michael@0 | 19 | do_check_true(signmarBin.exists()); |
michael@0 | 20 | do_check_true(signmarBin.isExecutable()); |
michael@0 | 21 | |
michael@0 | 22 | // Setup the command line arguments to sign the MAR. |
michael@0 | 23 | let NSSConfigDir = do_get_file("data"); |
michael@0 | 24 | let args = ["-d", NSSConfigDir.path]; |
michael@0 | 25 | if (certs.length == 1 && useShortHandCmdLine) { |
michael@0 | 26 | args.push("-n", certs[0]); |
michael@0 | 27 | } else { |
michael@0 | 28 | for (var i = 0; i < certs.length; i++) { |
michael@0 | 29 | args.push("-n" + i, certs[i]); |
michael@0 | 30 | } |
michael@0 | 31 | } |
michael@0 | 32 | args.push("-s", inMAR.path, outMAR.path); |
michael@0 | 33 | |
michael@0 | 34 | process.init(signmarBin); |
michael@0 | 35 | try { |
michael@0 | 36 | process.run(true, args, args.length); |
michael@0 | 37 | } catch(e) { |
michael@0 | 38 | // On Windows negative return value throws an exception |
michael@0 | 39 | process.exitValue = -1; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | // Verify signmar returned 0 for success. |
michael@0 | 43 | if (wantSuccess) { |
michael@0 | 44 | do_check_eq(process.exitValue, 0); |
michael@0 | 45 | } else { |
michael@0 | 46 | do_check_neq(process.exitValue, 0); |
michael@0 | 47 | } |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | |
michael@0 | 51 | /** |
michael@0 | 52 | * Extract a MAR signature. |
michael@0 | 53 | * |
michael@0 | 54 | * @param inMAR The MAR file who's signature should be extracted |
michael@0 | 55 | * @param sigIndex The index of the signature to extract |
michael@0 | 56 | * @param extractedSig The file where the extracted signature will be stored |
michael@0 | 57 | * @param wantSuccess True if a successful signmar return code is desired |
michael@0 | 58 | */ |
michael@0 | 59 | function extractMARSignature(inMAR, sigIndex, extractedSig, wantSuccess) { |
michael@0 | 60 | // Get a process to the signmar binary from the dist/bin directory. |
michael@0 | 61 | let process = Cc["@mozilla.org/process/util;1"]. |
michael@0 | 62 | createInstance(Ci.nsIProcess); |
michael@0 | 63 | let signmarBin = do_get_file("signmar" + BIN_SUFFIX); |
michael@0 | 64 | |
michael@0 | 65 | // Make sure the signmar binary exists and is an executable. |
michael@0 | 66 | do_check_true(signmarBin.exists()); |
michael@0 | 67 | do_check_true(signmarBin.isExecutable()); |
michael@0 | 68 | |
michael@0 | 69 | // Setup the command line arguments to extract the signature in the MAR. |
michael@0 | 70 | let args = ["-n" + sigIndex, "-X", inMAR.path, extractedSig.path]; |
michael@0 | 71 | |
michael@0 | 72 | process.init(signmarBin); |
michael@0 | 73 | try { |
michael@0 | 74 | process.run(true, args, args.length); |
michael@0 | 75 | } catch(e) { |
michael@0 | 76 | // On Windows negative return value throws an exception |
michael@0 | 77 | process.exitValue = -1; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | // Verify signmar returned 0 for success. |
michael@0 | 81 | if (wantSuccess) { |
michael@0 | 82 | do_check_eq(process.exitValue, 0); |
michael@0 | 83 | } else { |
michael@0 | 84 | do_check_neq(process.exitValue, 0); |
michael@0 | 85 | } |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | /** |
michael@0 | 89 | * Import a MAR signature. |
michael@0 | 90 | * |
michael@0 | 91 | * @param inMAR The MAR file who's signature should be imported to |
michael@0 | 92 | * @param sigIndex The index of the signature to import to |
michael@0 | 93 | * @param sigFile The file where the base64 signature exists |
michael@0 | 94 | * @param outMAR The same as inMAR but with the specified signature |
michael@0 | 95 | * swapped at the specified index. |
michael@0 | 96 | * @param wantSuccess True if a successful signmar return code is desired |
michael@0 | 97 | */ |
michael@0 | 98 | function importMARSignature(inMAR, sigIndex, sigFile, outMAR, wantSuccess) { |
michael@0 | 99 | // Get a process to the signmar binary from the dist/bin directory. |
michael@0 | 100 | let process = Cc["@mozilla.org/process/util;1"]. |
michael@0 | 101 | createInstance(Ci.nsIProcess); |
michael@0 | 102 | let signmarBin = do_get_file("signmar" + BIN_SUFFIX); |
michael@0 | 103 | |
michael@0 | 104 | // Make sure the signmar binary exists and is an executable. |
michael@0 | 105 | do_check_true(signmarBin.exists()); |
michael@0 | 106 | do_check_true(signmarBin.isExecutable()); |
michael@0 | 107 | |
michael@0 | 108 | // Setup the command line arguments to import the signature in the MAR. |
michael@0 | 109 | let args = ["-n" + sigIndex, "-I", inMAR.path, sigFile.path, outMAR.path]; |
michael@0 | 110 | |
michael@0 | 111 | process.init(signmarBin); |
michael@0 | 112 | try { |
michael@0 | 113 | process.run(true, args, args.length); |
michael@0 | 114 | } catch(e) { |
michael@0 | 115 | // On Windows negative return value throws an exception |
michael@0 | 116 | process.exitValue = -1; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | // Verify signmar returned 0 for success. |
michael@0 | 120 | if (wantSuccess) { |
michael@0 | 121 | do_check_eq(process.exitValue, 0); |
michael@0 | 122 | } else { |
michael@0 | 123 | do_check_neq(process.exitValue, 0); |
michael@0 | 124 | } |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | /** |
michael@0 | 128 | * Verifies a MAR file. |
michael@0 | 129 | * |
michael@0 | 130 | * @param signedMAR Verifies a MAR file |
michael@0 | 131 | */ |
michael@0 | 132 | function verifyMAR(signedMAR, wantSuccess, certs, useShortHandCmdLine) { |
michael@0 | 133 | // Get a process to the signmar binary from the dist/bin directory. |
michael@0 | 134 | let process = Cc["@mozilla.org/process/util;1"]. |
michael@0 | 135 | createInstance(Ci.nsIProcess); |
michael@0 | 136 | let signmarBin = do_get_file("signmar" + BIN_SUFFIX); |
michael@0 | 137 | |
michael@0 | 138 | // Make sure the signmar binary exists and is an executable. |
michael@0 | 139 | do_check_true(signmarBin.exists()); |
michael@0 | 140 | do_check_true(signmarBin.isExecutable()); |
michael@0 | 141 | |
michael@0 | 142 | // Will reference the arguments to use for verification in signmar |
michael@0 | 143 | let args = []; |
michael@0 | 144 | |
michael@0 | 145 | // The XPCShell test wiki indicates this is the preferred way for |
michael@0 | 146 | // Windows and OSX detection. |
michael@0 | 147 | var isWindows = ("@mozilla.org/windows-registry-key;1" in Cc); |
michael@0 | 148 | var isOSX = ("nsILocalFileMac" in Components.interfaces); |
michael@0 | 149 | |
michael@0 | 150 | // Setup the command line arguments to create the MAR. |
michael@0 | 151 | // Windows & Mac vs. Linux/... have different command line for verification |
michael@0 | 152 | // since on Windows we verify with CryptoAPI, on Mac with Security |
michael@0 | 153 | // Transforms or CDSA/CSSM and on all other platforms we verify with NSS. So |
michael@0 | 154 | // on Windows and Mac we use an exported DER file and on other platforms we |
michael@0 | 155 | // use the NSS config db. |
michael@0 | 156 | if (isWindows || isOSX) { |
michael@0 | 157 | if (certs.length == 1 && useShortHandCmdLine) { |
michael@0 | 158 | args.push("-D", "data/" + certs[0] + ".der"); |
michael@0 | 159 | } else { |
michael@0 | 160 | for (var i = 0; i < certs.length; i++) { |
michael@0 | 161 | args.push("-D" + i, "data/" + certs[i] + ".der"); |
michael@0 | 162 | } |
michael@0 | 163 | } |
michael@0 | 164 | } else { |
michael@0 | 165 | let NSSConfigDir = do_get_file("data"); |
michael@0 | 166 | args = ["-d", NSSConfigDir.path]; |
michael@0 | 167 | if (certs.length == 1 && useShortHandCmdLine) { |
michael@0 | 168 | args.push("-n", certs[0]); |
michael@0 | 169 | } else { |
michael@0 | 170 | for (var i = 0; i < certs.length; i++) { |
michael@0 | 171 | args.push("-n" + i, certs[i]); |
michael@0 | 172 | } |
michael@0 | 173 | } |
michael@0 | 174 | } |
michael@0 | 175 | args.push("-v", signedMAR.path); |
michael@0 | 176 | |
michael@0 | 177 | process.init(signmarBin); |
michael@0 | 178 | try { |
michael@0 | 179 | // We put this in a try block because nsIProcess doesn't like -1 returns |
michael@0 | 180 | process.run(true, args, args.length); |
michael@0 | 181 | } catch (e) { |
michael@0 | 182 | // On Windows negative return value throws an exception |
michael@0 | 183 | process.exitValue = -1; |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | // Verify signmar returned 0 for success. |
michael@0 | 187 | if (wantSuccess) { |
michael@0 | 188 | do_check_eq(process.exitValue, 0); |
michael@0 | 189 | } else { |
michael@0 | 190 | do_check_neq(process.exitValue, 0); |
michael@0 | 191 | } |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | /** |
michael@0 | 195 | * Strips a MAR signature. |
michael@0 | 196 | * |
michael@0 | 197 | * @param signedMAR The MAR file that should be signed |
michael@0 | 198 | * @param outMAR The MAR file to write to with signature stripped |
michael@0 | 199 | */ |
michael@0 | 200 | function stripMARSignature(signedMAR, outMAR, wantSuccess) { |
michael@0 | 201 | // Get a process to the signmar binary from the dist/bin directory. |
michael@0 | 202 | let process = Cc["@mozilla.org/process/util;1"]. |
michael@0 | 203 | createInstance(Ci.nsIProcess); |
michael@0 | 204 | let signmarBin = do_get_file("signmar" + BIN_SUFFIX); |
michael@0 | 205 | |
michael@0 | 206 | // Make sure the signmar binary exists and is an executable. |
michael@0 | 207 | do_check_true(signmarBin.exists()); |
michael@0 | 208 | do_check_true(signmarBin.isExecutable()); |
michael@0 | 209 | |
michael@0 | 210 | // Setup the command line arguments to create the MAR. |
michael@0 | 211 | let args = ["-r", signedMAR.path, outMAR.path]; |
michael@0 | 212 | |
michael@0 | 213 | process.init(signmarBin); |
michael@0 | 214 | try { |
michael@0 | 215 | process.run(true, args, args.length); |
michael@0 | 216 | } catch (e) { |
michael@0 | 217 | // On Windows negative return value throws an exception |
michael@0 | 218 | process.exitValue = -1; |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | // Verify signmar returned 0 for success. |
michael@0 | 222 | if (wantSuccess) { |
michael@0 | 223 | do_check_eq(process.exitValue, 0); |
michael@0 | 224 | } else { |
michael@0 | 225 | do_check_neq(process.exitValue, 0); |
michael@0 | 226 | } |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | |
michael@0 | 230 | function cleanup() { |
michael@0 | 231 | let outMAR = tempDir.clone(); |
michael@0 | 232 | outMAR.append("signed_out.mar"); |
michael@0 | 233 | if (outMAR.exists()) { |
michael@0 | 234 | outMAR.remove(false); |
michael@0 | 235 | } |
michael@0 | 236 | outMAR = tempDir.clone(); |
michael@0 | 237 | outMAR.append("multiple_signed_out.mar"); |
michael@0 | 238 | if (outMAR.exists()) { |
michael@0 | 239 | outMAR.remove(false); |
michael@0 | 240 | } |
michael@0 | 241 | outMAR = tempDir.clone(); |
michael@0 | 242 | outMAR.append("out.mar"); |
michael@0 | 243 | if (outMAR.exists()) { |
michael@0 | 244 | outMAR.remove(false); |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | let outDir = tempDir.clone(); |
michael@0 | 248 | outDir.append("out"); |
michael@0 | 249 | if (outDir.exists()) { |
michael@0 | 250 | outDir.remove(true); |
michael@0 | 251 | } |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | const wantFailure = false; |
michael@0 | 255 | const wantSuccess = true; |
michael@0 | 256 | // Define the unit tests to run. |
michael@0 | 257 | let tests = { |
michael@0 | 258 | // Test signing a MAR file with a single signature |
michael@0 | 259 | test_sign_single: function() { |
michael@0 | 260 | let inMAR = do_get_file("data/" + refMARPrefix + "binary_data_mar.mar"); |
michael@0 | 261 | let outMAR = tempDir.clone(); |
michael@0 | 262 | outMAR.append("signed_out.mar"); |
michael@0 | 263 | if (outMAR.exists()) { |
michael@0 | 264 | outMAR.remove(false); |
michael@0 | 265 | } |
michael@0 | 266 | signMAR(inMAR, outMAR, ["mycert"], wantSuccess, true); |
michael@0 | 267 | do_check_true(outMAR.exists()); |
michael@0 | 268 | let outMARData = getBinaryFileData(outMAR); |
michael@0 | 269 | let refMAR = do_get_file("data/" + refMARPrefix + "signed_pib_mar.mar"); |
michael@0 | 270 | let refMARData = getBinaryFileData(refMAR); |
michael@0 | 271 | compareBinaryData(outMARData, refMARData); |
michael@0 | 272 | }, |
michael@0 | 273 | // Test signing a MAR file with multiple signatures |
michael@0 | 274 | test_sign_multiple: function() { |
michael@0 | 275 | let inMAR = do_get_file("data/" + refMARPrefix + "binary_data_mar.mar"); |
michael@0 | 276 | let outMAR = tempDir.clone(); |
michael@0 | 277 | outMAR.append("multiple_signed_out.mar"); |
michael@0 | 278 | if (outMAR.exists()) { |
michael@0 | 279 | outMAR.remove(false); |
michael@0 | 280 | } |
michael@0 | 281 | do_check_false(outMAR.exists()); |
michael@0 | 282 | signMAR(inMAR, outMAR, ["mycert", "mycert2", "mycert3"], |
michael@0 | 283 | wantSuccess, true); |
michael@0 | 284 | do_check_true(outMAR.exists()); |
michael@0 | 285 | let outMARData = getBinaryFileData(outMAR); |
michael@0 | 286 | let refMAR = do_get_file("data/" + refMARPrefix + "multiple_signed_pib_mar.mar"); |
michael@0 | 287 | let refMARData = getBinaryFileData(refMAR); |
michael@0 | 288 | compareBinaryData(outMARData, refMARData); |
michael@0 | 289 | }, |
michael@0 | 290 | // Test verifying a signed MAR file |
michael@0 | 291 | test_verify_single: function() { |
michael@0 | 292 | let signedMAR = do_get_file("data/signed_pib_mar.mar"); |
michael@0 | 293 | verifyMAR(signedMAR, wantSuccess, ["mycert"], true); |
michael@0 | 294 | verifyMAR(signedMAR, wantSuccess, ["mycert"], false); |
michael@0 | 295 | }, |
michael@0 | 296 | // Test verifying a signed MAR file with too many certs fails. |
michael@0 | 297 | // Or if you want to look at it another way, One mycert signature |
michael@0 | 298 | // is missing. |
michael@0 | 299 | test_verify_single_too_many_certs: function() { |
michael@0 | 300 | let signedMAR = do_get_file("data/signed_pib_mar.mar"); |
michael@0 | 301 | verifyMAR(signedMAR, wantFailure, ["mycert", "mycert"], true); |
michael@0 | 302 | verifyMAR(signedMAR, wantFailure, ["mycert", "mycert"], false); |
michael@0 | 303 | }, |
michael@0 | 304 | // Test verifying a signed MAR file fails when using a wrong cert |
michael@0 | 305 | test_verify_single_wrong_cert: function() { |
michael@0 | 306 | let signedMAR = do_get_file("data/signed_pib_mar.mar"); |
michael@0 | 307 | verifyMAR(signedMAR, wantFailure, ["mycert2"], true); |
michael@0 | 308 | verifyMAR(signedMAR, wantFailure, ["mycert2"], false); |
michael@0 | 309 | }, |
michael@0 | 310 | // Test verifying a signed MAR file with multiple signatures |
michael@0 | 311 | test_verify_multiple: function() { |
michael@0 | 312 | let signedMAR = do_get_file("data/multiple_signed_pib_mar.mar"); |
michael@0 | 313 | verifyMAR(signedMAR, wantSuccess, ["mycert", "mycert2", "mycert3"]); |
michael@0 | 314 | }, |
michael@0 | 315 | // Test verifying an unsigned MAR file fails |
michael@0 | 316 | test_verify_unsigned_mar_file_fails: function() { |
michael@0 | 317 | let unsignedMAR = do_get_file("data/binary_data_mar.mar"); |
michael@0 | 318 | verifyMAR(unsignedMAR, wantFailure, ["mycert", "mycert2", "mycert3"]); |
michael@0 | 319 | }, |
michael@0 | 320 | // Test verifying a signed MAR file with the same signature multiple |
michael@0 | 321 | // times fails. The input MAR has: mycert, mycert2, mycert3. |
michael@0 | 322 | // we're checking to make sure the number of verified signatures |
michael@0 | 323 | // is only 1 and not 3. Each signature should be verified once. |
michael@0 | 324 | test_verify_multiple_same_cert: function() { |
michael@0 | 325 | let signedMAR = do_get_file("data/multiple_signed_pib_mar.mar"); |
michael@0 | 326 | verifyMAR(signedMAR, wantFailure, ["mycert", "mycert", "mycert"]); |
michael@0 | 327 | }, |
michael@0 | 328 | // Test verifying a signed MAR file with the correct signatures but in |
michael@0 | 329 | // a different order fails |
michael@0 | 330 | test_verify_multiple_wrong_order: function() { |
michael@0 | 331 | let signedMAR = do_get_file("data/multiple_signed_pib_mar.mar"); |
michael@0 | 332 | verifyMAR(signedMAR, wantSuccess, ["mycert", "mycert2", "mycert3"]); |
michael@0 | 333 | verifyMAR(signedMAR, wantFailure, ["mycert", "mycert3", "mycert2"]); |
michael@0 | 334 | verifyMAR(signedMAR, wantFailure, ["mycert2", "mycert", "mycert3"]); |
michael@0 | 335 | verifyMAR(signedMAR, wantFailure, ["mycert2", "mycert3", "mycert"]); |
michael@0 | 336 | verifyMAR(signedMAR, wantFailure, ["mycert3", "mycert", "mycert2"]); |
michael@0 | 337 | verifyMAR(signedMAR, wantFailure, ["mycert3", "mycert2", "mycert"]); |
michael@0 | 338 | }, |
michael@0 | 339 | // Test verifying a signed MAR file without a PIB |
michael@0 | 340 | test_verify_no_pib: function() { |
michael@0 | 341 | let signedMAR = do_get_file("data/signed_no_pib_mar.mar"); |
michael@0 | 342 | verifyMAR(signedMAR, wantSuccess, ["mycert"], true); |
michael@0 | 343 | verifyMAR(signedMAR, wantSuccess, ["mycert"], false); |
michael@0 | 344 | }, |
michael@0 | 345 | // Test verifying a signed MAR file with multiple signatures without a PIB |
michael@0 | 346 | test_verify_no_pib_multiple: function() { |
michael@0 | 347 | let signedMAR = do_get_file("data/multiple_signed_no_pib_mar.mar"); |
michael@0 | 348 | verifyMAR(signedMAR, wantSuccess, ["mycert", "mycert2", "mycert3"]); |
michael@0 | 349 | }, |
michael@0 | 350 | // Test verifying a crafted MAR file where the attacker tried to adjust |
michael@0 | 351 | // the version number manually. |
michael@0 | 352 | test_crafted_mar: function() { |
michael@0 | 353 | let signedBadMAR = do_get_file("data/manipulated_signed_mar.mar"); |
michael@0 | 354 | verifyMAR(signedBadMAR, wantFailure, ["mycert"], true); |
michael@0 | 355 | verifyMAR(signedBadMAR, wantFailure, ["mycert"], false); |
michael@0 | 356 | }, |
michael@0 | 357 | // Test verifying a file that doesn't exist fails |
michael@0 | 358 | test_bad_path_verify_fails: function() { |
michael@0 | 359 | let noMAR = do_get_file("data/does_not_exist_.mar", true); |
michael@0 | 360 | do_check_false(noMAR.exists()); |
michael@0 | 361 | verifyMAR(noMAR, wantFailure, ["mycert"], true); |
michael@0 | 362 | }, |
michael@0 | 363 | // Test to make sure a stripped MAR is the same as the original MAR |
michael@0 | 364 | test_strip_signature: function() { |
michael@0 | 365 | let originalMAR = do_get_file("data/" + |
michael@0 | 366 | refMARPrefix + |
michael@0 | 367 | "binary_data_mar.mar"); |
michael@0 | 368 | let signedMAR = tempDir.clone(); |
michael@0 | 369 | signedMAR.append("signed_out.mar"); |
michael@0 | 370 | let outMAR = tempDir.clone(); |
michael@0 | 371 | outMAR.append("out.mar", true); |
michael@0 | 372 | stripMARSignature(signedMAR, outMAR, wantSuccess); |
michael@0 | 373 | |
michael@0 | 374 | // Verify that the stripped MAR matches the original data MAR exactly |
michael@0 | 375 | let outMARData = getBinaryFileData(outMAR); |
michael@0 | 376 | let originalMARData = getBinaryFileData(originalMAR); |
michael@0 | 377 | compareBinaryData(outMARData, originalMARData); |
michael@0 | 378 | }, |
michael@0 | 379 | // Test to make sure a stripped multi-signature-MAR is the same as the original MAR |
michael@0 | 380 | test_strip_multiple_signatures: function() { |
michael@0 | 381 | let originalMAR = do_get_file("data/" + |
michael@0 | 382 | refMARPrefix + |
michael@0 | 383 | "binary_data_mar.mar"); |
michael@0 | 384 | let signedMAR = tempDir.clone(); |
michael@0 | 385 | signedMAR.append("multiple_signed_out.mar"); |
michael@0 | 386 | let outMAR = tempDir.clone(); |
michael@0 | 387 | outMAR.append("out.mar"); |
michael@0 | 388 | stripMARSignature(signedMAR, outMAR, wantSuccess); |
michael@0 | 389 | |
michael@0 | 390 | // Verify that the stripped MAR matches the original data MAR exactly |
michael@0 | 391 | let outMARData = getBinaryFileData(outMAR); |
michael@0 | 392 | let originalMARData = getBinaryFileData(originalMAR); |
michael@0 | 393 | compareBinaryData(outMARData, originalMARData); |
michael@0 | 394 | }, |
michael@0 | 395 | // Test extracting the first signature in a MAR that has only a single signature |
michael@0 | 396 | test_extract_sig_single: function() { |
michael@0 | 397 | let inMAR = do_get_file("data/signed_pib_mar.mar"); |
michael@0 | 398 | let extractedSig = do_get_file("extracted_signature", true); |
michael@0 | 399 | if (extractedSig.exists()) { |
michael@0 | 400 | extractedSig.remove(false); |
michael@0 | 401 | } |
michael@0 | 402 | extractMARSignature(inMAR, 0, extractedSig, wantSuccess); |
michael@0 | 403 | do_check_true(extractedSig.exists()); |
michael@0 | 404 | |
michael@0 | 405 | let referenceSig = do_get_file("data/signed_pib_mar.signature.0"); + |
michael@0 | 406 | compareBinaryData(extractedSig, referenceSig); |
michael@0 | 407 | }, |
michael@0 | 408 | // Test extracting the all signatures in a multi signature MAR |
michael@0 | 409 | // The input MAR has 3 signatures. |
michael@0 | 410 | test_extract_sig_multi: function() { |
michael@0 | 411 | for (let i = 0; i < 3; i++) { |
michael@0 | 412 | let inMAR = do_get_file("data/multiple_signed_pib_mar.mar"); |
michael@0 | 413 | let extractedSig = do_get_file("extracted_signature", true); |
michael@0 | 414 | if (extractedSig.exists()) { |
michael@0 | 415 | extractedSig.remove(false); |
michael@0 | 416 | } |
michael@0 | 417 | extractMARSignature(inMAR, i, extractedSig, wantSuccess); |
michael@0 | 418 | do_check_true(extractedSig.exists()); |
michael@0 | 419 | |
michael@0 | 420 | let referenceSig = do_get_file("data/multiple_signed_pib_mar.sig." + i); + |
michael@0 | 421 | compareBinaryData(extractedSig, referenceSig); |
michael@0 | 422 | } |
michael@0 | 423 | }, |
michael@0 | 424 | // Test extracting a signature that is out of range fails |
michael@0 | 425 | test_extract_sig_out_of_range: function() { |
michael@0 | 426 | let inMAR = do_get_file("data/signed_pib_mar.mar"); |
michael@0 | 427 | let extractedSig = do_get_file("extracted_signature", true); |
michael@0 | 428 | if (extractedSig.exists()) { |
michael@0 | 429 | extractedSig.remove(false); |
michael@0 | 430 | } |
michael@0 | 431 | const outOfBoundsIndex = 5; |
michael@0 | 432 | extractMARSignature(inMAR, outOfBoundsIndex, extractedSig, wantFailure); |
michael@0 | 433 | do_check_false(extractedSig.exists()); |
michael@0 | 434 | }, |
michael@0 | 435 | // Test signing a file that doesn't exist fails |
michael@0 | 436 | test_bad_path_sign_fails: function() { |
michael@0 | 437 | let inMAR = do_get_file("data/does_not_exist_.mar", true); |
michael@0 | 438 | let outMAR = tempDir.clone(); |
michael@0 | 439 | outMAR.append("signed_out.mar"); |
michael@0 | 440 | do_check_false(inMAR.exists()); |
michael@0 | 441 | signMAR(inMAR, outMAR, ["mycert"], wantFailure, true); |
michael@0 | 442 | do_check_false(outMAR.exists()); |
michael@0 | 443 | }, |
michael@0 | 444 | // Test verifying only a subset of the signatures fails. |
michael@0 | 445 | // The input MAR has: mycert, mycert2, mycert3. |
michael@0 | 446 | // We're only verifying 2 of the 3 signatures and that should fail. |
michael@0 | 447 | test_verify_multiple_subset: function() { |
michael@0 | 448 | let signedMAR = do_get_file("data/multiple_signed_pib_mar.mar"); |
michael@0 | 449 | verifyMAR(signedMAR, wantFailure, ["mycert", "mycert2"]); |
michael@0 | 450 | }, |
michael@0 | 451 | // Test importing the first signature in a MAR that has only |
michael@0 | 452 | // a single signature |
michael@0 | 453 | test_import_sig_single: function() { |
michael@0 | 454 | // Make sure the input MAR was signed with mycert only |
michael@0 | 455 | let inMAR = do_get_file("data/signed_pib_mar.mar"); |
michael@0 | 456 | verifyMAR(inMAR, wantSuccess, ["mycert"], false); |
michael@0 | 457 | verifyMAR(inMAR, wantFailure, ["mycert2"], false); |
michael@0 | 458 | verifyMAR(inMAR, wantFailure, ["mycert3"], false); |
michael@0 | 459 | |
michael@0 | 460 | // Get the signature file for this MAR signed with the key from mycert2 |
michael@0 | 461 | let sigFile = do_get_file("data/signed_pib_mar.signature.mycert2"); |
michael@0 | 462 | do_check_true(sigFile.exists()); |
michael@0 | 463 | let outMAR = tempDir.clone(); |
michael@0 | 464 | outMAR.append("sigchanged_signed_pib_mar.mar"); |
michael@0 | 465 | if (outMAR.exists()) { |
michael@0 | 466 | outMAR.remove(false); |
michael@0 | 467 | } |
michael@0 | 468 | |
michael@0 | 469 | //Run the import operation |
michael@0 | 470 | importMARSignature(inMAR, 0, sigFile, outMAR, wantSuccess); |
michael@0 | 471 | |
michael@0 | 472 | // Verify we have a new MAR file and that mycert no longer verifies |
michael@0 | 473 | // and that mycert2 does verify |
michael@0 | 474 | do_check_true(outMAR.exists()); |
michael@0 | 475 | verifyMAR(outMAR, wantFailure, ["mycert"], false); |
michael@0 | 476 | verifyMAR(outMAR, wantSuccess, ["mycert2"], false); |
michael@0 | 477 | verifyMAR(outMAR, wantFailure, ["mycert3"], false); |
michael@0 | 478 | |
michael@0 | 479 | // Compare the binary data to something that was signed originally |
michael@0 | 480 | // with the private key from mycert2 |
michael@0 | 481 | let refMAR = do_get_file("data/signed_pib_mar_with_mycert2.mar"); |
michael@0 | 482 | do_check_true(refMAR.exists()); |
michael@0 | 483 | let refMARData = getBinaryFileData(refMAR); |
michael@0 | 484 | let outMARData = getBinaryFileData(outMAR); |
michael@0 | 485 | compareBinaryData(outMARData, refMARData); |
michael@0 | 486 | }, |
michael@0 | 487 | // Test importing a signature that doesn't belong to the file |
michael@0 | 488 | // fails to verify. |
michael@0 | 489 | test_import_wrong_sig: function() { |
michael@0 | 490 | // Make sure the input MAR was signed with mycert only |
michael@0 | 491 | let inMAR = do_get_file("data/signed_pib_mar.mar"); |
michael@0 | 492 | verifyMAR(inMAR, wantSuccess, ["mycert"], false); |
michael@0 | 493 | verifyMAR(inMAR, wantFailure, ["mycert2"], false); |
michael@0 | 494 | verifyMAR(inMAR, wantFailure, ["mycert3"], false); |
michael@0 | 495 | |
michael@0 | 496 | // Get the signature file for this MAR signed with the key from mycert2 |
michael@0 | 497 | let sigFile = do_get_file("data/multiple_signed_pib_mar.sig.0"); |
michael@0 | 498 | do_check_true(sigFile.exists()); |
michael@0 | 499 | let outMAR = tempDir.clone(); |
michael@0 | 500 | outMAR.append("sigchanged_signed_pib_mar.mar"); |
michael@0 | 501 | if (outMAR.exists()) { |
michael@0 | 502 | outMAR.remove(false); |
michael@0 | 503 | } |
michael@0 | 504 | |
michael@0 | 505 | //Run the import operation |
michael@0 | 506 | importMARSignature(inMAR, 0, sigFile, outMAR, wantSuccess); |
michael@0 | 507 | |
michael@0 | 508 | // Verify we have a new MAR file and that mycert no longer verifies |
michael@0 | 509 | // and that mycert2 does verify |
michael@0 | 510 | do_check_true(outMAR.exists()); |
michael@0 | 511 | verifyMAR(outMAR, wantFailure, ["mycert"], false); |
michael@0 | 512 | verifyMAR(outMAR, wantFailure, ["mycert2"], false); |
michael@0 | 513 | verifyMAR(outMAR, wantFailure, ["mycert3"], false); |
michael@0 | 514 | }, |
michael@0 | 515 | // Test importing to the second signature in a MAR that has multiple |
michael@0 | 516 | // signature |
michael@0 | 517 | test_import_sig_multiple: function() { |
michael@0 | 518 | // Make sure the input MAR was signed with mycert only |
michael@0 | 519 | let inMAR = do_get_file("data/multiple_signed_pib_mar.mar"); |
michael@0 | 520 | verifyMAR(inMAR, wantSuccess, ["mycert", "mycert2", "mycert3"], false); |
michael@0 | 521 | verifyMAR(inMAR, wantFailure, ["mycert", "mycert", "mycert3"], false); |
michael@0 | 522 | |
michael@0 | 523 | // Get the signature file for this MAR signed with the key from mycert |
michael@0 | 524 | let sigFile = do_get_file("data/multiple_signed_pib_mar.sig.0"); |
michael@0 | 525 | do_check_true(sigFile.exists()); |
michael@0 | 526 | let outMAR = tempDir.clone(); |
michael@0 | 527 | outMAR.append("sigchanged_signed_pib_mar.mar"); |
michael@0 | 528 | if (outMAR.exists()) { |
michael@0 | 529 | outMAR.remove(false); |
michael@0 | 530 | } |
michael@0 | 531 | |
michael@0 | 532 | //Run the import operation |
michael@0 | 533 | const secondSigPos = 1; |
michael@0 | 534 | importMARSignature(inMAR, secondSigPos, sigFile, outMAR, wantSuccess); |
michael@0 | 535 | |
michael@0 | 536 | // Verify we have a new MAR file and that mycert no longer verifies |
michael@0 | 537 | // and that mycert2 does verify |
michael@0 | 538 | do_check_true(outMAR.exists()); |
michael@0 | 539 | verifyMAR(outMAR, wantSuccess, ["mycert", "mycert", "mycert3"], false); |
michael@0 | 540 | verifyMAR(outMAR, wantFailure, ["mycert", "mycert2", "mycert3"], false); |
michael@0 | 541 | |
michael@0 | 542 | // Compare the binary data to something that was signed originally |
michael@0 | 543 | // with the private keys from mycert, mycert, mycert3 |
michael@0 | 544 | let refMAR = do_get_file("data/multiple_signed_pib_mar_2.mar"); |
michael@0 | 545 | do_check_true(refMAR.exists()); |
michael@0 | 546 | let refMARData = getBinaryFileData(refMAR); |
michael@0 | 547 | let outMARData = getBinaryFileData(outMAR); |
michael@0 | 548 | compareBinaryData(outMARData, refMARData); |
michael@0 | 549 | }, |
michael@0 | 550 | // Test stripping a MAR that doesn't exist fails |
michael@0 | 551 | test_bad_path_strip_fails: function() { |
michael@0 | 552 | let noMAR = do_get_file("data/does_not_exist_mar", true); |
michael@0 | 553 | do_check_false(noMAR.exists()); |
michael@0 | 554 | let outMAR = tempDir.clone(); |
michael@0 | 555 | outMAR.append("out.mar"); |
michael@0 | 556 | stripMARSignature(noMAR, outMAR, wantFailure); |
michael@0 | 557 | }, |
michael@0 | 558 | // Test extracting from a bad path fails |
michael@0 | 559 | test_extract_bad_path: function() { |
michael@0 | 560 | let noMAR = do_get_file("data/does_not_exist.mar", true); |
michael@0 | 561 | let extractedSig = do_get_file("extracted_signature", true); |
michael@0 | 562 | do_check_false(noMAR.exists()); |
michael@0 | 563 | if (extractedSig.exists()) { |
michael@0 | 564 | extractedSig.remove(false); |
michael@0 | 565 | } |
michael@0 | 566 | extractMARSignature(noMAR, 0, extractedSig, wantFailure); |
michael@0 | 567 | do_check_false(extractedSig.exists()); |
michael@0 | 568 | }, |
michael@0 | 569 | // Between each test make sure the out MAR does not exist. |
michael@0 | 570 | cleanup_per_test: function() { |
michael@0 | 571 | } |
michael@0 | 572 | }; |
michael@0 | 573 | |
michael@0 | 574 | cleanup(); |
michael@0 | 575 | |
michael@0 | 576 | // Run all the tests |
michael@0 | 577 | do_check_eq(run_tests(tests), Object.keys(tests).length - 1); |
michael@0 | 578 | |
michael@0 | 579 | do_register_cleanup(cleanup); |
michael@0 | 580 | } |