Debian Codegraph @

tests/test_in_toto_sign.py

raw
1 # Copyright New York University and the in-toto contributors
2 # SPDX-License-Identifier: Apache-2.0
3
4 """
5 <Program Name>
6 test_in_toto_sign.py
7 <Author>
8 Sachit Malik <[email protected]>
9 Lukas Puehringer <[email protected]>
10 <Started>
11 Wed Jun 21, 2017
12 <Copyright>
13 See LICENSE for licensing information.
14 <Purpose>
15 Test in_toto_sign command line tool.
16 """
17
18 import json
19 import os
20 import shutil
21 import unittest
22 from unittest import mock
23
24 import securesystemslib.interface # pylint: disable=unused-import
25
26 from in_toto.in_toto_sign import main as in_toto_sign_main
27 from tests.common import CliTestCase, GenKeysMixin, GPGKeysMixin, TmpDirMixin
28
29
30 class TestInTotoSignTool(CliTestCase, TmpDirMixin, GPGKeysMixin, GenKeysMixin):
31 """Test in_toto_sign's main() - requires sys.argv patching; error logs/exits
32 on Exception."""
33
34 cli_main_func = staticmethod(in_toto_sign_main)
35
36 @classmethod
37 def setUpClass(self):
38 # Find demo files
39 demo_files = os.path.join(
40 os.path.dirname(os.path.realpath(__file__)), "demo_files"
41 )
42
43 # Create and change into temporary directory
44 self.set_up_test_dir()
45 self.set_up_gpg_keys()
46 self.set_up_keys()
47
48 # Copy demo files to temp dir
49 for file_path in os.listdir(demo_files):
50 shutil.copy(os.path.join(demo_files, file_path), self.test_dir)
51
52 self.layout_path = "demo.layout.template"
53 self.link_path = "package.2f89b927.link"
54 self.alice_path = "alice"
55 self.alice_pub_path = "alice.pub"
56 self.bob_path = "bob"
57 self.bob_pub_path = "bob.pub"
58 self.carl_path = "carl"
59 self.carl_pub_path = "carl.pub"
60 self.danny_path = "danny"
61 self.danny_pub_path = "danny.pub"
62
63 @classmethod
64 def tearDownClass(self):
65 self.tear_down_test_dir()
66
67 def test_sign_and_verify(self):
68 """Test signing and verifying Layout and Link metadata with
69 different combinations of arguments."""
70
71 # Sign Layout with multiple keys and write to "tmp.layout"
72 self.assert_cli_sys_exit(
73 [
74 "-f",
75 self.layout_path,
76 "-k",
77 self.alice_path,
78 self.bob_path,
79 "-o",
80 "tmp.layout",
81 ],
82 0,
83 )
84
85 # Verify "tmp.layout" (requires all keys)
86 self.assert_cli_sys_exit(
87 [
88 "-f",
89 "tmp.layout",
90 "-k",
91 self.alice_pub_path,
92 self.bob_pub_path,
93 "--verify",
94 ],
95 0,
96 )
97
98 # Sign Layout "tmp.layout", appending new signature, write to "tmp.layout"
99 self.assert_cli_sys_exit(
100 ["-f", "tmp.layout", "-k", self.carl_path, "-a"], 0
101 )
102
103 # Verify "tmp.layout" (has three signatures now)
104 self.assert_cli_sys_exit(
105 [
106 "-f",
107 "tmp.layout",
108 "-k",
109 self.alice_pub_path,
110 self.bob_pub_path,
111 self.carl_pub_path,
112 "--verify",
113 ],
114 0,
115 )
116
117 # Sign Layout "tmp.layout" with ed25519 key, appending new signature,
118 # write to "tmp.layout"
119 self.assert_cli_sys_exit(
120 ["-f", "tmp.layout", "-k", self.danny_path, "-t", "ed25519", "-a"],
121 0,
122 )
123
124 # Verify "tmp.layout" (has four signatures now)
125 self.assert_cli_sys_exit(
126 [
127 "-f",
128 "tmp.layout",
129 "-k",
130 self.alice_pub_path,
131 self.bob_pub_path,
132 self.carl_pub_path,
133 self.danny_pub_path,
134 "-t",
135 "rsa",
136 "rsa",
137 "rsa",
138 "ed25519",
139 "--verify",
140 ],
141 0,
142 )
143
144 # Sign Link, replacing old signature
145 # and write to same file as input
146 self.assert_cli_sys_exit(
147 [
148 "-f",
149 self.link_path,
150 "-k",
151 self.bob_path,
152 "-o",
153 self.link_path,
154 ],
155 0,
156 )
157
158 # Verify Link
159 self.assert_cli_sys_exit(
160 ["-f", self.link_path, "-k", self.bob_pub_path, "--verify"], 0
161 )
162
163 # Replace signature to Link and store to new file using passed
164 # key's (alice) id as infix
165 self.assert_cli_sys_exit(
166 ["-f", self.link_path, "-k", self.alice_path], 0
167 )
168 # Verify Link with alice's keyid as infix
169 self.assert_cli_sys_exit(
170 [
171 "-f",
172 "package.556caebd.link",
173 "-k",
174 self.alice_pub_path,
175 "--verify",
176 ],
177 0,
178 )
179
180 # Sign Layout with default gpg key
181 self.assert_cli_sys_exit(
182 [
183 "-f",
184 self.layout_path,
185 "-g",
186 "-o",
187 "tmp_gpg.layout",
188 "--gpg-home",
189 self.gnupg_home,
190 ],
191 0,
192 )
193 # Verify Layout signed with default gpg key
194 self.assert_cli_sys_exit(
195 [
196 "-f",
197 "tmp_gpg.layout",
198 "-g",
199 self.gpg_key_0C8A17,
200 "--gpg-home",
201 self.gnupg_home,
202 "--verify",
203 ],
204 0,
205 )
206
207 # Sign Layout with two gpg keys
208 self.assert_cli_sys_exit(
209 [
210 "-f",
211 self.layout_path,
212 "-g",
213 self.gpg_key_768C43,
214 self.gpg_key_85DA58,
215 "-o",
216 "tmp_gpg.layout",
217 "--gpg-home",
218 self.gnupg_home,
219 ],
220 0,
221 )
222 self.assert_cli_sys_exit(
223 [
224 "-f",
225 "tmp_gpg.layout",
226 "-g",
227 self.gpg_key_768C43,
228 self.gpg_key_85DA58,
229 "--gpg-home",
230 self.gnupg_home,
231 "--verify",
232 ],
233 0,
234 )
235
236 # Sign Layout with encrypted rsa/ed25519 keys, prompting for pw, and verify
237 with mock.patch(
238 "securesystemslib.interface.get_password", return_value=self.key_pw
239 ):
240 self.assert_cli_sys_exit(
241 [
242 "-f",
243 self.layout_path,
244 "-k",
245 self.rsa_key_enc_path,
246 self.ed25519_key_enc_path,
247 "-t",
248 "rsa",
249 "ed25519",
250 "--prompt",
251 "-o",
252 "signed_with_encrypted_keys.layout",
253 ],
254 0,
255 )
256 self.assert_cli_sys_exit(
257 [
258 "-f",
259 "signed_with_encrypted_keys.layout",
260 "-k",
261 self.rsa_key_enc_path + ".pub",
262 self.ed25519_key_enc_path + ".pub",
263 "-t",
264 "rsa",
265 "ed25519",
266 "--verify",
267 ],
268 0,
269 )
270
271 def test_fail_signing(self):
272 # Fail signing with invalid key
273 self.assert_cli_sys_exit(
274 [
275 "-f",
276 self.layout_path,
277 "-k",
278 self.carl_path,
279 self.link_path,
280 ],
281 2,
282 )
283
284 # Fail with encrypted rsa key but no password
285 self.assert_cli_sys_exit(
286 ["-f", self.layout_path, "-k", self.rsa_key_enc_path], 2
287 )
288
289 # Fail with encrypted ed25519 key but no password
290 self.assert_cli_sys_exit(
291 [
292 "-f",
293 self.layout_path,
294 "-k",
295 self.ed25519_key_enc_path,
296 "-t",
297 "ed25519",
298 ],
299 2,
300 )
301
302 def test_fail_verification(self):
303 """Fail signature verification."""
304 # Fail with wrong key (not used for signing)
305 self.assert_cli_sys_exit(
306 ["-f", self.layout_path, "-k", self.carl_pub_path, "--verify"], 1
307 )
308
309 # Fail with wrong key (not a valid pub key)
310 self.assert_cli_sys_exit(
311 ["-f", self.layout_path, "-k", self.carl_path, "--verify"], 2
312 )
313
314 # Fail with wrong gpg keyid (not used for signing)
315 self.assert_cli_sys_exit(
316 [
317 "-f",
318 self.layout_path,
319 "-g",
320 self.gpg_key_0C8A17,
321 "--gpg-home",
322 self.gnupg_home,
323 "--verify",
324 ],
325 1,
326 )
327
328 # Fail with wrong gpg keyid (not a valid keyid)
329 self.assert_cli_sys_exit(
330 [
331 "-f",
332 self.layout_path,
333 "-g",
334 "bogus-gpg-keyid",
335 "--gpg-home",
336 self.gnupg_home,
337 "--verify",
338 ],
339 2,
340 )
341
342 def test_bad_args(self):
343 """Fail with wrong combination of arguments."""
344
345 # Conflicting "verify" and signing options (--verify -o)
346 self.assert_cli_sys_exit(
347 [
348 "-f",
349 self.layout_path,
350 "-k",
351 "key-not-used",
352 "--verify",
353 "-o",
354 "file-not-written",
355 ],
356 2,
357 )
358
359 # Conflicting "verify" and signing options (--verify -oa)
360 self.assert_cli_sys_exit(
361 [
362 "-f",
363 self.layout_path,
364 "-k",
365 "key-not-used",
366 "--verify",
367 "-a",
368 ],
369 2,
370 )
371
372 # Wrong "append" option for Link metadata
373 self.assert_cli_sys_exit(
374 ["-f", self.link_path, "-k", "key-not-used", "-a"], 2
375 )
376
377 # Wrong multiple keys for Link metadata
378 self.assert_cli_sys_exit(
379 [
380 "-f",
381 self.link_path,
382 "-k",
383 self.alice_path,
384 self.bob_path,
385 ],
386 2,
387 )
388
389 # Wrong number of multiple key types
390 self.assert_cli_sys_exit(
391 [
392 "-f",
393 self.layout_path,
394 "-k",
395 self.alice_path,
396 self.bob_path,
397 "-t",
398 "rsa",
399 "-o",
400 "tmp.layout",
401 ],
402 2,
403 )
404
405 # Wrong multiple gpg keys for Link metadata
406 self.assert_cli_sys_exit(
407 [
408 "-f",
409 self.link_path,
410 "-g",
411 self.gpg_key_768C43,
412 self.gpg_key_85DA58,
413 ],
414 2,
415 )
416
417 # Only one of gpg or regular key can be passed
418 self.assert_cli_sys_exit(
419 ["-f", self.layout_path, "-k", self.alice_path, "-g"], 2
420 )
421
422 # At least one of gpg or regular key must be passed
423 self.assert_cli_sys_exit(
424 [
425 "-f",
426 self.layout_path,
427 ],
428 2,
429 )
430
431 # For verification if gpg option is passed there must be a key id argument
432 self.assert_cli_sys_exit(["-f", self.layout_path, "--verify", "-g"], 2)
433
434 def test_bad_metadata(self):
435 """Fail with wrong metadata."""
436
437 # Not valid JSON
438 self.assert_cli_sys_exit(
439 [
440 "-f",
441 self.alice_pub_path,
442 "-k",
443 "key-not-used",
444 ],
445 2,
446 )
447
448 # Valid JSON but not valid Link or Layout
449 with open("tmp.json", "wb") as f:
450 f.write(json.dumps({}).encode("utf-8"))
451
452 self.assert_cli_sys_exit(
453 [
454 "-f",
455 "tmp.json",
456 "-k",
457 "key-not-used",
458 ],
459 2,
460 )
461
462
463 class TestInTotoSignToolWithDSSE(
464 CliTestCase, TmpDirMixin, GPGKeysMixin, GenKeysMixin
465 ):
466 """Test in_toto_sign's main() for dsse metadata files - requires sys.argv
467 patching; error logs/exits on Exception."""
468
469 cli_main_func = staticmethod(in_toto_sign_main)
470
471 @classmethod
472 def setUpClass(self):
473 # Find demo files
474 demo_files = os.path.join(
475 os.path.dirname(os.path.realpath(__file__)), "demo_files"
476 )
477
478 # Demo DSSE Metadata Files
479 demo_dsse_files = os.path.join(
480 os.path.dirname(os.path.realpath(__file__)), "demo_dsse_files"
481 )
482
483 # Create and change into temporary directory
484 self.set_up_test_dir()
485 self.set_up_gpg_keys()
486 self.set_up_keys()
487
488 # Copy demo files to temp dir
489 for file_path in os.listdir(demo_files):
490 shutil.copy(os.path.join(demo_files, file_path), self.test_dir)
491
492 # Copy demo DSSE files to temp dir
493 for file_path in os.listdir(demo_dsse_files):
494 shutil.copy(os.path.join(demo_dsse_files, file_path), self.test_dir)
495
496 self.layout_path = "demo.layout.template"
497 self.link_path = "package.2f89b927.link"
498 self.alice_path = "alice"
499 self.alice_pub_path = "alice.pub"
500 self.bob_path = "bob"
501 self.bob_pub_path = "bob.pub"
502 self.carl_path = "carl"
503 self.carl_pub_path = "carl.pub"
504 self.danny_path = "danny"
505 self.danny_pub_path = "danny.pub"
506
507 @classmethod
508 def tearDownClass(self):
509 self.tear_down_test_dir()
510
511 def test_sign_and_verify(self):
512 """Test signing and verifying Layout and Link metadata with
513 different combinations of arguments."""
514
515 # Sign Layout with multiple keys and write to "tmp.layout"
516 self.assert_cli_sys_exit(
517 [
518 "-f",
519 self.layout_path,
520 "-k",
521 self.alice_path,
522 self.bob_path,
523 "-o",
524 "tmp.layout",
525 ],
526 0,
527 )
528
529 # Verify "tmp.layout" (requires all keys)
530 self.assert_cli_sys_exit(
531 [
532 "-f",
533 "tmp.layout",
534 "-k",
535 self.alice_pub_path,
536 self.bob_pub_path,
537 "--verify",
538 ],
539 0,
540 )
541
542 # Sign Layout "tmp.layout", appending new signature, write to "tmp.layout"
543 self.assert_cli_sys_exit(
544 ["-f", "tmp.layout", "-k", self.carl_path, "-a"], 0
545 )
546
547 # Verify "tmp.layout" (has three signatures now)
548 self.assert_cli_sys_exit(
549 [
550 "-f",
551 "tmp.layout",
552 "-k",
553 self.alice_pub_path,
554 self.bob_pub_path,
555 self.carl_pub_path,
556 "--verify",
557 ],
558 0,
559 )
560
561 # Sign Layout "tmp.layout" with ed25519 key, appending new signature,
562 # write to "tmp.layout"
563 self.assert_cli_sys_exit(
564 ["-f", "tmp.layout", "-k", self.danny_path, "-t", "ed25519", "-a"],
565 0,
566 )
567
568 # Verify "tmp.layout" (has four signatures now)
569 self.assert_cli_sys_exit(
570 [
571 "-f",
572 "tmp.layout",
573 "-k",
574 self.alice_pub_path,
575 self.bob_pub_path,
576 self.carl_pub_path,
577 self.danny_pub_path,
578 "-t",
579 "rsa",
580 "rsa",
581 "rsa",
582 "ed25519",
583 "--verify",
584 ],
585 0,
586 )
587
588 # Sign Link, replacing old signature
589 # and write to same file as input
590 self.assert_cli_sys_exit(
591 [
592 "-f",
593 self.link_path,
594 "-k",
595 self.bob_path,
596 "-o",
597 self.link_path,
598 ],
599 0,
600 )
601
602 # Verify Link
603 self.assert_cli_sys_exit(
604 ["-f", self.link_path, "-k", self.bob_pub_path, "--verify"], 0
605 )
606
607 # Replace signature to Link and store to new file using passed
608 # key's (alice) id as infix
609 self.assert_cli_sys_exit(
610 ["-f", self.link_path, "-k", self.alice_path], 0
611 )
612 # Verify Link with alice's keyid as infix
613 self.assert_cli_sys_exit(
614 [
615 "-f",
616 "package.556caebd.link",
617 "-k",
618 self.alice_pub_path,
619 "--verify",
620 ],
621 0,
622 )
623
624 def test_fail_verification(self):
625 """Fail signature verification."""
626 # Fail with wrong key (not used for signing)
627 self.assert_cli_sys_exit(
628 ["-f", self.layout_path, "-k", self.carl_pub_path, "--verify"], 1
629 )
630
631 # Fail with wrong gpg keyid (not used for signing)
632 self.assert_cli_sys_exit(
633 [
634 "-f",
635 self.layout_path,
636 "-g",
637 self.gpg_key_0C8A17,
638 "--gpg-home",
639 self.gnupg_home,
640 "--verify",
641 ],
642 2,
643 )
644
645
646 if __name__ == "__main__":
647 unittest.main()