summaryrefslogtreecommitdiffstats
path: root/security/manager/ssl/tests/unit/test_signed_apps.js
blob: 4893bfd7148acaa1cfa91e13bb1d35b258f6c0fd (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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
"use strict";

// Tests the API nsIX509CertDB.openSignedAppFileAsync, which backs add-on
// signature verification. Testcases include various ways of tampering with
// add-ons as well as different hash algorithms used in the various
// signature/metadata files.

// from prio.h
const PR_RDWR = 0x04;
const PR_CREATE_FILE = 0x08;
const PR_TRUNCATE = 0x20;
const PR_USEC_PER_MSEC = 1000;

do_get_profile(); // must be called before getting nsIX509CertDB
const certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
  Ci.nsIX509CertDB
);

// Creates a new app package based in the inFilePath package, with a set of
// modifications (including possibly deletions) applied to the existing entries,
// and/or a set of new entries to be included.
function tamper(inFilePath, outFilePath, modifications, newEntries) {
  let writer = Cc["@mozilla.org/zipwriter;1"].createInstance(Ci.nsIZipWriter);
  writer.open(outFilePath, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE);
  try {
    let reader = Cc["@mozilla.org/libjar/zip-reader;1"].createInstance(
      Ci.nsIZipReader
    );
    reader.open(inFilePath);
    try {
      for (let entryName of reader.findEntries("")) {
        let inEntry = reader.getEntry(entryName);
        let entryInput = reader.getInputStream(entryName);
        try {
          let f = modifications[entryName];
          let outEntry, outEntryInput;
          if (f) {
            [outEntry, outEntryInput] = f(inEntry, entryInput);
            delete modifications[entryName];
          } else {
            [outEntry, outEntryInput] = [inEntry, entryInput];
          }
          // if f does not want the input entry to be copied to the output entry
          // at all (i.e. it wants it to be deleted), it will return null.
          if (outEntryInput) {
            try {
              writer.addEntryStream(
                entryName,
                outEntry.lastModifiedTime,
                outEntry.compression,
                outEntryInput,
                false
              );
            } finally {
              if (entryInput != outEntryInput) {
                outEntryInput.close();
              }
            }
          }
        } finally {
          entryInput.close();
        }
      }
    } finally {
      reader.close();
    }

    // Any leftover modification means that we were expecting to modify an entry
    // in the input file that wasn't there.
    for (let name in modifications) {
      if (modifications.hasOwnProperty(name)) {
        throw new Error("input file was missing expected entries: " + name);
      }
    }

    // Now, append any new entries to the end
    newEntries.forEach(function (newEntry) {
      let sis = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
        Ci.nsIStringInputStream
      );
      try {
        sis.setData(newEntry.content, newEntry.content.length);
        writer.addEntryStream(
          newEntry.name,
          new Date() * PR_USEC_PER_MSEC,
          Ci.nsIZipWriter.COMPRESSION_BEST,
          sis,
          false
        );
      } finally {
        sis.close();
      }
    });
  } finally {
    writer.close();
  }
}

function removeEntry(entry, entryInput) {
  return [null, null];
}

function truncateEntry(entry, entryInput) {
  if (entryInput.available() == 0) {
    throw new Error(
      "Truncating already-zero length entry will result in " +
        "identical entry."
    );
  }

  let content = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
    Ci.nsIStringInputStream
  );
  content.data = "";

  return [entry, content];
}

function check_open_result(name, expectedRv) {
  return function openSignedAppFileCallback(rv, aZipReader, aSignerCert) {
    info("openSignedAppFileCallback called for " + name);
    equal(rv, expectedRv, "Actual and expected return value should match");
    equal(
      aZipReader != null,
      Components.isSuccessCode(expectedRv),
      "ZIP reader should be null only if the return value denotes failure"
    );
    equal(
      aSignerCert != null,
      Components.isSuccessCode(expectedRv),
      "Signer cert should be null only if the return value denotes failure"
    );
    run_next_test();
  };
}

function original_app_path(test_name) {
  return do_get_file("test_signed_apps/" + test_name + ".zip", false);
}

function tampered_app_path(test_name) {
  return new FileUtils.File(
    PathUtils.join(
      Services.dirsvc.get("TmpD", Ci.nsIFile).path,
      `test_signed_app-${test_name}.zip`
    )
  );
}

var hashTestcases = [
  // SHA-256 in PKCS#7 + SHA-256 present elsewhere => OK
  { name: "app_mf-1-256_sf-1-256_p7-1-256", expectedResult: Cr.NS_OK },
  { name: "app_mf-1-256_sf-1-256_p7-256", expectedResult: Cr.NS_OK },
  { name: "app_mf-1-256_sf-256_p7-1-256", expectedResult: Cr.NS_OK },
  { name: "app_mf-1-256_sf-256_p7-256", expectedResult: Cr.NS_OK },
  { name: "app_mf-256_sf-1-256_p7-1-256", expectedResult: Cr.NS_OK },
  { name: "app_mf-256_sf-1-256_p7-256", expectedResult: Cr.NS_OK },
  { name: "app_mf-256_sf-256_p7-1-256", expectedResult: Cr.NS_OK },
  { name: "app_mf-256_sf-256_p7-256", expectedResult: Cr.NS_OK },

  // SHA-1 in PKCS#7 + SHA-1 present elsewhere => OK
  { name: "app_mf-1-256_sf-1-256_p7-1", expectedResult: Cr.NS_OK },
  { name: "app_mf-1-256_sf-1_p7-1", expectedResult: Cr.NS_OK },
  { name: "app_mf-1_sf-1-256_p7-1", expectedResult: Cr.NS_OK },
  { name: "app_mf-1_sf-1_p7-1", expectedResult: Cr.NS_OK },

  // SHA-256 in PKCS#7 + SHA-256 not present elsewhere => INVALID
  {
    name: "app_mf-1-256_sf-1_p7-1-256",
    expectedResult: Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID,
  },
  {
    name: "app_mf-1-256_sf-1_p7-256",
    expectedResult: Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID,
  },
  {
    name: "app_mf-1_sf-1-256_p7-1-256",
    expectedResult: Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID,
  },
  {
    name: "app_mf-1_sf-1-256_p7-256",
    expectedResult: Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID,
  },
  {
    name: "app_mf-1_sf-1_p7-1-256",
    expectedResult: Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID,
  },
  {
    name: "app_mf-1_sf-1_p7-256",
    expectedResult: Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID,
  },
  {
    name: "app_mf-1_sf-256_p7-1-256",
    expectedResult: Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID,
  },
  {
    name: "app_mf-1_sf-256_p7-256",
    expectedResult: Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID,
  },
  {
    name: "app_mf-256_sf-1_p7-1-256",
    expectedResult: Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID,
  },
  {
    name: "app_mf-256_sf-1_p7-256",
    expectedResult: Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID,
  },

  // SHA-1 in PKCS#7 + SHA-1 not present elsewhere => INVALID
  {
    name: "app_mf-1-256_sf-256_p7-1",
    expectedResult: Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID,
  },
  {
    name: "app_mf-1_sf-256_p7-1",
    expectedResult: Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID,
  },
  {
    name: "app_mf-256_sf-1-256_p7-1",
    expectedResult: Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID,
  },
  {
    name: "app_mf-256_sf-1_p7-1",
    expectedResult: Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID,
  },
  {
    name: "app_mf-256_sf-256_p7-1",
    expectedResult: Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID,
  },
];

// Policy values for the preference "security.signed_app_signatures.policy"
const PKCS7WithSHA1OrSHA256 = 0b0;
const PKCS7WithSHA256 = 0b1;
const COSEAndPKCS7WithSHA1OrSHA256 = 0b10;
const COSEAndPKCS7WithSHA256 = 0b11;
const COSERequiredAndPKCS7WithSHA1OrSHA256 = 0b100;
const COSERequiredAndPKCS7WithSHA256 = 0b101;
const COSEOnly = 0b110;
const COSEOnlyAgain = 0b111;

function add_signature_test(policy, test) {
  // First queue up a test to set the desired policy:
  add_test(function () {
    Services.prefs.setIntPref("security.signed_app_signatures.policy", policy);
    run_next_test();
  });
  // Then queue up the test itself:
  add_test(test);
}

for (let testcase of hashTestcases) {
  add_signature_test(PKCS7WithSHA1OrSHA256, function () {
    certdb.openSignedAppFileAsync(
      Ci.nsIX509CertDB.AppXPCShellRoot,
      original_app_path(testcase.name),
      check_open_result(testcase.name, testcase.expectedResult)
    );
  });
}

add_signature_test(PKCS7WithSHA1OrSHA256, function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("empty_signerInfos"),
    check_open_result(
      "the signerInfos in the PKCS#7 signature is empty",
      Cr.NS_ERROR_CMS_VERIFY_NOT_SIGNED
    )
  );
});

add_signature_test(PKCS7WithSHA1OrSHA256, function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("unsigned_app"),
    check_open_result("unsigned", Cr.NS_ERROR_SIGNED_JAR_NOT_SIGNED)
  );
});

add_signature_test(PKCS7WithSHA1OrSHA256, function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("unknown_issuer_app"),
    check_open_result(
      "unknown_issuer",
      getXPCOMStatusFromNSS(SEC_ERROR_UNKNOWN_ISSUER)
    )
  );
});

add_signature_test(COSEAndPKCS7WithSHA1OrSHA256, function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("cose_signed_with_pkcs7"),
    check_open_result("cose_signed_with_pkcs7", Cr.NS_OK)
  );
});

add_signature_test(COSEAndPKCS7WithSHA256, function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("app_mf-256_sf-256_p7-256"),
    check_open_result("no COSE but correct PK#7", Cr.NS_OK)
  );
});

add_signature_test(COSEAndPKCS7WithSHA256, function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("app_mf-1_sf-256_p7-256"),
    check_open_result(
      "no COSE and wrong PK#7 hash",
      Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID
    )
  );
});

add_signature_test(COSERequiredAndPKCS7WithSHA1OrSHA256, function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("app_mf-256_sf-256_p7-256"),
    check_open_result(
      "COSE signature missing (SHA1 or 256)",
      Cr.NS_ERROR_SIGNED_JAR_WRONG_SIGNATURE
    )
  );
});

add_signature_test(COSERequiredAndPKCS7WithSHA256, function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("app_mf-256_sf-256_p7-256"),
    check_open_result(
      "COSE signature missing (SHA256)",
      Cr.NS_ERROR_SIGNED_JAR_WRONG_SIGNATURE
    )
  );
});

add_signature_test(COSERequiredAndPKCS7WithSHA256, function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("only_cose_signed"),
    check_open_result(
      "COSE signature only (PK#7 allowed, not present)",
      Cr.NS_OK
    )
  );
});

add_signature_test(COSERequiredAndPKCS7WithSHA1OrSHA256, function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("only_cose_signed"),
    check_open_result(
      "COSE signature only (PK#7 allowed, not present)",
      Cr.NS_OK
    )
  );
});

add_signature_test(COSEAndPKCS7WithSHA1OrSHA256, function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("cose_multiple_signed_with_pkcs7"),
    check_open_result("cose_multiple_signed_with_pkcs7", Cr.NS_OK)
  );
});

add_signature_test(COSEAndPKCS7WithSHA1OrSHA256, function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("cose_int_signed_with_pkcs7"),
    check_open_result("COSE signed with an intermediate", Cr.NS_OK)
  );
});

add_signature_test(COSEAndPKCS7WithSHA1OrSHA256, function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("only_cose_signed"),
    check_open_result(
      "PK7 signature missing",
      Cr.NS_ERROR_SIGNED_JAR_NOT_SIGNED
    )
  );
});

add_signature_test(COSEOnly, function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("cose_multiple_signed_with_pkcs7"),
    check_open_result(
      "Expected only COSE signature",
      Cr.NS_ERROR_SIGNED_JAR_UNSIGNED_ENTRY
    )
  );
});

add_signature_test(COSEOnly, function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("only_cose_multiple_signed"),
    check_open_result("only Multiple COSE signatures", Cr.NS_OK)
  );
});

add_signature_test(COSEOnly, function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("only_cose_signed"),
    check_open_result("only_cose_signed", Cr.NS_OK)
  );
});

add_signature_test(COSEOnlyAgain, function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("only_cose_signed"),
    check_open_result("only_cose_signed (again)", Cr.NS_OK)
  );
});

add_signature_test(COSEOnly, function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("cose_signed_with_pkcs7"),
    check_open_result(
      "COSE only expected but also PK#7 signed",
      Cr.NS_ERROR_SIGNED_JAR_UNSIGNED_ENTRY
    )
  );
});

// Sanity check to ensure a no-op tampering gives a valid result
add_signature_test(PKCS7WithSHA1OrSHA256, function () {
  let tampered = tampered_app_path("identity_tampering");
  tamper(original_app_path("app_mf-1_sf-1_p7-1"), tampered, {}, []);
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("app_mf-1_sf-1_p7-1"),
    check_open_result("identity_tampering", Cr.NS_OK)
  );
});

add_signature_test(PKCS7WithSHA1OrSHA256, function () {
  let tampered = tampered_app_path("missing_rsa");
  tamper(
    original_app_path("app_mf-1_sf-1_p7-1"),
    tampered,
    { "META-INF/A.RSA": removeEntry },
    []
  );
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    tampered,
    check_open_result("missing_rsa", Cr.NS_ERROR_SIGNED_JAR_NOT_SIGNED)
  );
});

add_signature_test(PKCS7WithSHA1OrSHA256, function () {
  let tampered = tampered_app_path("missing_sf");
  tamper(
    original_app_path("app_mf-1_sf-1_p7-1"),
    tampered,
    { "META-INF/A.SF": removeEntry },
    []
  );
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    tampered,
    check_open_result("missing_sf", Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID)
  );
});

add_signature_test(PKCS7WithSHA1OrSHA256, function () {
  let tampered = tampered_app_path("missing_manifest_mf");
  tamper(
    original_app_path("app_mf-1_sf-1_p7-1"),
    tampered,
    { "META-INF/MANIFEST.MF": removeEntry },
    []
  );
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    tampered,
    check_open_result(
      "missing_manifest_mf",
      Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID
    )
  );
});

add_signature_test(PKCS7WithSHA1OrSHA256, function () {
  let tampered = tampered_app_path("missing_entry");
  tamper(
    original_app_path("app_mf-1_sf-1_p7-1"),
    tampered,
    { "manifest.json": removeEntry },
    []
  );
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    tampered,
    check_open_result("missing_entry", Cr.NS_ERROR_SIGNED_JAR_ENTRY_MISSING)
  );
});

add_signature_test(PKCS7WithSHA1OrSHA256, function () {
  let tampered = tampered_app_path("truncated_entry");
  tamper(
    original_app_path("app_mf-1_sf-1_p7-1"),
    tampered,
    { "manifest.json": truncateEntry },
    []
  );
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    tampered,
    check_open_result("truncated_entry", Cr.NS_ERROR_SIGNED_JAR_MODIFIED_ENTRY)
  );
});

add_signature_test(PKCS7WithSHA1OrSHA256, function () {
  let tampered = tampered_app_path("truncated_manifestFile");
  tamper(
    original_app_path("app_mf-1_sf-1_p7-1"),
    tampered,
    { "META-INF/MANIFEST.MF": truncateEntry },
    []
  );
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    tampered,
    check_open_result(
      "truncated_manifestFile",
      Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID
    )
  );
});

add_signature_test(PKCS7WithSHA1OrSHA256, function () {
  let tampered = tampered_app_path("truncated_signatureFile");
  tamper(
    original_app_path("app_mf-1_sf-1_p7-1"),
    tampered,
    { "META-INF/A.SF": truncateEntry },
    []
  );
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    tampered,
    check_open_result(
      "truncated_signatureFile",
      getXPCOMStatusFromNSS(SEC_ERROR_PKCS7_BAD_SIGNATURE)
    )
  );
});

add_signature_test(PKCS7WithSHA1OrSHA256, function () {
  let tampered = tampered_app_path("truncated_pkcs7File");
  tamper(
    original_app_path("app_mf-1_sf-1_p7-1"),
    tampered,
    { "META-INF/A.RSA": truncateEntry },
    []
  );
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    tampered,
    check_open_result("truncated_pkcs7File", Cr.NS_ERROR_CMS_VERIFY_NOT_SIGNED)
  );
});

add_signature_test(PKCS7WithSHA1OrSHA256, function () {
  let tampered = tampered_app_path("unsigned_entry");
  tamper(original_app_path("app_mf-1_sf-1_p7-1"), tampered, {}, [
    { name: "unsigned.txt", content: "unsigned content!" },
  ]);
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    tampered,
    check_open_result("unsigned_entry", Cr.NS_ERROR_SIGNED_JAR_UNSIGNED_ENTRY)
  );
});

add_signature_test(PKCS7WithSHA1OrSHA256, function () {
  let tampered = tampered_app_path("unsigned_metainf_entry");
  tamper(original_app_path("app_mf-1_sf-1_p7-1"), tampered, {}, [
    { name: "META-INF/unsigned.txt", content: "unsigned content!" },
  ]);
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    tampered,
    check_open_result(
      "unsigned_metainf_entry",
      Cr.NS_ERROR_SIGNED_JAR_UNSIGNED_ENTRY
    )
  );
});

add_signature_test(PKCS7WithSHA256, function testSHA1Disabled() {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("app_mf-1_sf-1_p7-1"),
    check_open_result(
      "SHA-1 should not be accepted if disabled by policy",
      Cr.NS_ERROR_SIGNED_JAR_WRONG_SIGNATURE
    )
  );
});

add_signature_test(PKCS7WithSHA256, function testSHA256WorksWithSHA1Disabled() {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("app_mf-256_sf-256_p7-256"),
    check_open_result(
      "SHA-256 should work if SHA-1 is disabled by policy",
      Cr.NS_OK
    )
  );
});

add_signature_test(
  PKCS7WithSHA256,
  function testMultipleSignaturesWorkWithSHA1Disabled() {
    certdb.openSignedAppFileAsync(
      Ci.nsIX509CertDB.AppXPCShellRoot,
      original_app_path("app_mf-1-256_sf-1-256_p7-1-256"),
      check_open_result(
        "Multiple signatures should work if SHA-1 is " +
          "disabled by policy (if SHA-256 signature verifies)",
        Cr.NS_OK
      )
    );
  }
);

var cosePolicies = [
  COSEAndPKCS7WithSHA1OrSHA256,
  COSERequiredAndPKCS7WithSHA1OrSHA256,
];

// PS256 is not yet supported.
var coseTestcasesStage = [
  {
    name: "autograph-714ba248-stage-tomato-clock-PKCS7-SHA1-ES256-ES384",
    expectedResult: Cr.NS_OK,
    root: Ci.nsIX509CertDB.AddonsStageRoot,
  },
  {
    name: "autograph-714ba248-stage-tomato-clock-PKCS7-SHA1-ES256-PS256",
    expectedResult: Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID,
    root: Ci.nsIX509CertDB.AddonsStageRoot,
  },
  {
    name: "autograph-714ba248-stage-tomato-clock-PKCS7-SHA1-ES256",
    expectedResult: Cr.NS_OK,
    root: Ci.nsIX509CertDB.AddonsStageRoot,
  },
  {
    name: "autograph-714ba248-stage-tomato-clock-PKCS7-SHA1-PS256",
    expectedResult: Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID,
    root: Ci.nsIX509CertDB.AddonsStageRoot,
  },
];

var coseTestcasesProd = [
  {
    name: "autograph-714ba248-prod-tomato-clock-PKCS7-SHA1-ES256-ES384",
    expectedResult: Cr.NS_OK,
    root: Ci.nsIX509CertDB.AddonsPublicRoot,
  },
  {
    name: "autograph-714ba248-prod-tomato-clock-PKCS7-SHA1-ES256-PS256",
    expectedResult: Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID,
    root: Ci.nsIX509CertDB.AddonsPublicRoot,
  },
  {
    name: "autograph-714ba248-prod-tomato-clock-PKCS7-SHA1-ES256",
    expectedResult: Cr.NS_OK,
    root: Ci.nsIX509CertDB.AddonsPublicRoot,
  },
  {
    name: "autograph-714ba248-prod-tomato-clock-PKCS7-SHA1-PS256",
    expectedResult: Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID,
    root: Ci.nsIX509CertDB.AddonsPublicRoot,
  },
];

for (let policy of cosePolicies) {
  for (let testcase of [...coseTestcasesStage, ...coseTestcasesProd]) {
    add_signature_test(policy, function () {
      certdb.openSignedAppFileAsync(
        testcase.root,
        original_app_path(testcase.name),
        check_open_result(testcase.name, testcase.expectedResult)
      );
    });
  }
}

add_signature_test(COSEAndPKCS7WithSHA256, function testCOSESigTampered() {
  let tampered = tampered_app_path("cose_sig_tampered");
  tamper(
    original_app_path("cose_signed_with_pkcs7"),
    tampered,
    { "META-INF/cose.sig": truncateEntry },
    []
  );
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    tampered,
    check_open_result(
      "cose_sig_tampered",
      Cr.NS_ERROR_SIGNED_JAR_MODIFIED_ENTRY
    )
  );
});

// PKCS7 is processed before COSE, so if a COSE signature file is removed or
// tampered with, this appears as a PKCS7 signature verification failure.
add_signature_test(COSEAndPKCS7WithSHA256, function testCOSESigRemoved() {
  let tampered = tampered_app_path("cose_sig_removed");
  tamper(
    original_app_path("cose_signed_with_pkcs7"),
    tampered,
    { "META-INF/cose.sig": removeEntry },
    []
  );
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    tampered,
    check_open_result("cose_sig_removed", Cr.NS_ERROR_SIGNED_JAR_ENTRY_MISSING)
  );
});

add_signature_test(COSEAndPKCS7WithSHA256, function testCOSEManifestTampered() {
  let tampered = tampered_app_path("cose_manifest_tampered");
  tamper(
    original_app_path("cose_signed_with_pkcs7"),
    tampered,
    { "META-INF/cose.manifest": truncateEntry },
    []
  );
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    tampered,
    check_open_result(
      "cose_manifest_tampered",
      Cr.NS_ERROR_SIGNED_JAR_MODIFIED_ENTRY
    )
  );
});

add_signature_test(COSEAndPKCS7WithSHA256, function testCOSEManifestRemoved() {
  let tampered = tampered_app_path("cose_manifest_removed");
  tamper(
    original_app_path("cose_signed_with_pkcs7"),
    tampered,
    { "META-INF/cose.manifest": removeEntry },
    []
  );
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    tampered,
    check_open_result(
      "cose_manifest_removed",
      Cr.NS_ERROR_SIGNED_JAR_ENTRY_MISSING
    )
  );
});

add_signature_test(COSEAndPKCS7WithSHA256, function testCOSEFileAdded() {
  let tampered = tampered_app_path("cose_file_added");
  tamper(original_app_path("cose_signed_with_pkcs7"), tampered, {}, [
    { name: "unsigned.txt", content: "unsigned content!" },
  ]);
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    tampered,
    check_open_result("cose_file_added", Cr.NS_ERROR_SIGNED_JAR_UNSIGNED_ENTRY)
  );
});

add_signature_test(COSEAndPKCS7WithSHA256, function testCOSEFileRemoved() {
  let tampered = tampered_app_path("cose_file_removed");
  tamper(
    original_app_path("cose_signed_with_pkcs7"),
    tampered,
    { "manifest.json": removeEntry },
    []
  );
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    tampered,
    check_open_result("cose_file_removed", Cr.NS_ERROR_SIGNED_JAR_ENTRY_MISSING)
  );
});

add_signature_test(COSEAndPKCS7WithSHA256, function testCOSEFileTampered() {
  let tampered = tampered_app_path("cose_file_tampered");
  tamper(
    original_app_path("cose_signed_with_pkcs7"),
    tampered,
    { "manifest.json": truncateEntry },
    []
  );
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    tampered,
    check_open_result(
      "cose_file_tampered",
      Cr.NS_ERROR_SIGNED_JAR_MODIFIED_ENTRY
    )
  );
});

add_signature_test(COSEOnly, function testOnlyCOSESigTampered() {
  let tampered = tampered_app_path("only_cose_sig_tampered");
  tamper(
    original_app_path("only_cose_signed"),
    tampered,
    { "META-INF/cose.sig": truncateEntry },
    []
  );
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    tampered,
    check_open_result(
      "only_cose_sig_tampered",
      Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID
    )
  );
});

add_signature_test(COSEOnly, function testOnlyCOSESigRemoved() {
  let tampered = tampered_app_path("only_cose_sig_removed");
  tamper(
    original_app_path("only_cose_signed"),
    tampered,
    { "META-INF/cose.sig": removeEntry },
    []
  );
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    tampered,
    check_open_result(
      "only_cose_sig_removed",
      Cr.NS_ERROR_SIGNED_JAR_WRONG_SIGNATURE
    )
  );
});

add_signature_test(COSEOnly, function testOnlyCOSEManifestTampered() {
  let tampered = tampered_app_path("only_cose_manifest_tampered");
  tamper(
    original_app_path("only_cose_signed"),
    tampered,
    { "META-INF/cose.manifest": truncateEntry },
    []
  );
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    tampered,
    check_open_result(
      "only_cose_manifest_tampered",
      Cr.NS_ERROR_SIGNED_JAR_MANIFEST_INVALID
    )
  );
});

add_signature_test(COSEOnly, function testOnlyCOSEManifestRemoved() {
  let tampered = tampered_app_path("only_cose_manifest_removed");
  tamper(
    original_app_path("only_cose_signed"),
    tampered,
    { "META-INF/cose.manifest": removeEntry },
    []
  );
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    tampered,
    check_open_result(
      "only_cose_manifest_removed",
      Cr.NS_ERROR_SIGNED_JAR_WRONG_SIGNATURE
    )
  );
});

add_signature_test(COSEOnly, function testOnlyCOSEFileAdded() {
  let tampered = tampered_app_path("only_cose_file_added");
  tamper(original_app_path("only_cose_signed"), tampered, {}, [
    { name: "unsigned.txt", content: "unsigned content!" },
  ]);
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    tampered,
    check_open_result(
      "only_cose_file_added",
      Cr.NS_ERROR_SIGNED_JAR_UNSIGNED_ENTRY
    )
  );
});

add_signature_test(COSEOnly, function testOnlyCOSEFileRemoved() {
  let tampered = tampered_app_path("only_cose_file_removed");
  tamper(
    original_app_path("only_cose_signed"),
    tampered,
    { "manifest.json": removeEntry },
    []
  );
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    tampered,
    check_open_result(
      "only_cose_file_removed",
      Cr.NS_ERROR_SIGNED_JAR_ENTRY_MISSING
    )
  );
});

add_signature_test(COSEOnly, function testOnlyCOSEFileTampered() {
  let tampered = tampered_app_path("only_cose_file_tampered");
  tamper(
    original_app_path("only_cose_signed"),
    tampered,
    { "manifest.json": truncateEntry },
    []
  );
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    tampered,
    check_open_result(
      "only_cose_file_tampered",
      Cr.NS_ERROR_SIGNED_JAR_MODIFIED_ENTRY
    )
  );
});

// This was signed with only COSE first, and then the contents were tampered
// with (making the signature invalid). Then, the file was signed with
// PKCS7/SHA1. We need to ensure that if we're configured to process COSE, this
// verification fails.
add_signature_test(COSEAndPKCS7WithSHA1OrSHA256, function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("cose_tampered_good_pkcs7"),
    check_open_result(
      "tampered COSE with good PKCS7 signature should fail " +
        "when COSE and PKCS7 is processed",
      Cr.NS_ERROR_SIGNED_JAR_MODIFIED_ENTRY
    )
  );
});

add_signature_test(COSEOnly, function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("cose_tampered_good_pkcs7"),
    check_open_result(
      "tampered COSE with good PKCS7 signature should fail " +
        "when only COSE is processed",
      Cr.NS_ERROR_SIGNED_JAR_MODIFIED_ENTRY
    )
  );
});

// If we're not processing COSE, this should verify successfully.
add_signature_test(PKCS7WithSHA1OrSHA256, function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("cose_tampered_good_pkcs7"),
    check_open_result(
      "tampered COSE with good PKCS7 signature should succeed" +
        "when COSE is not processed",
      Cr.NS_OK
    )
  );
});

add_test(function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("bug_1411458"),
    check_open_result("bug 1411458", Cr.NS_ERROR_CMS_VERIFY_NO_CONTENT_INFO)
  );
});

// This has a big manifest file (~2MB). It should verify correctly.
add_test(function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("big_manifest"),
    check_open_result("add-on with big manifest file", Cr.NS_OK)
  );
});

// This has a huge manifest file (~10MB). Manifest files this large are not
// supported (8MB is the limit). It should not verify correctly.
add_test(function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("huge_manifest"),
    check_open_result(
      "add-on with huge manifest file",
      Cr.NS_ERROR_SIGNED_JAR_ENTRY_INVALID
    )
  );
});

// Verification should pass despite a not-yet-valid EE certificate.
// Regression test for bug 1713628
add_test(function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("validity_not_yet_valid"),
    check_open_result("validity_not_yet_valid", Cr.NS_OK)
  );
});

// Verification should pass despite an expired EE certificate.
// Regression test for bug 1267318 and bug 1548973
add_test(function () {
  certdb.openSignedAppFileAsync(
    Ci.nsIX509CertDB.AppXPCShellRoot,
    original_app_path("validity_expired"),
    check_open_result("validity_expired", Cr.NS_OK)
  );
});

// TODO: tampered MF, tampered SF
// TODO: too-large MF, too-large RSA, too-large SF
// TODO: MF and SF that end immediately after the last main header
//       (no CR nor LF)
// TODO: broken headers to exercise the parser