|
44 | 44 |
|
45 | 45 | import securesystemslib.exceptions
|
46 | 46 | import securesystemslib.formats
|
| 47 | +import securesystemslib.exceptions |
47 | 48 | import securesystemslib.hash
|
48 | 49 | import securesystemslib.interface as interface
|
49 | 50 |
|
| 51 | +from securesystemslib import KEY_TYPE_RSA, KEY_TYPE_ED25519, KEY_TYPE_ECDSA |
| 52 | + |
50 | 53 | import six
|
51 | 54 |
|
52 | 55 |
|
@@ -614,6 +617,58 @@ def test_import_ecdsa_privatekey_from_file(self):
|
614 | 617 | interface.import_ecdsa_privatekey_from_file, ecdsa_keypath, 'pw')
|
615 | 618 |
|
616 | 619 |
|
| 620 | + |
| 621 | + def test_import_public_keys_from_file(self): |
| 622 | + """Test import multiple public keys with different types. """ |
| 623 | + temporary_directory = tempfile.mkdtemp(dir=self.temporary_directory) |
| 624 | + path_rsa = os.path.join(temporary_directory, "rsa_key") |
| 625 | + path_ed25519 = os.path.join(temporary_directory, "ed25519_key") |
| 626 | + path_ecdsa = os.path.join(temporary_directory, "ecdsa_key") |
| 627 | + |
| 628 | + interface.generate_and_write_rsa_keypair(path_rsa, password="pw") |
| 629 | + interface.generate_and_write_ed25519_keypair(path_ed25519, password="pw") |
| 630 | + interface.generate_and_write_ecdsa_keypair(path_ecdsa, password="pw") |
| 631 | + |
| 632 | + # Successfully import key dict with one key per supported key type |
| 633 | + key_dict = interface.import_public_keys_from_file([ |
| 634 | + path_rsa + ".pub", |
| 635 | + path_ed25519 + ".pub", |
| 636 | + path_ecdsa + ".pub"], |
| 637 | + [KEY_TYPE_RSA, KEY_TYPE_ED25519, KEY_TYPE_ECDSA]) |
| 638 | + |
| 639 | + securesystemslib.formats.ANY_PUBKEY_DICT_SCHEMA.check_match(key_dict) |
| 640 | + self.assertListEqual( |
| 641 | + sorted([key["keytype"] for key in key_dict.values()]), |
| 642 | + sorted([KEY_TYPE_RSA, KEY_TYPE_ED25519, KEY_TYPE_ECDSA]) |
| 643 | + ) |
| 644 | + |
| 645 | + # Successfully import default rsa key |
| 646 | + key_dict = interface.import_public_keys_from_file([path_rsa + ".pub"]) |
| 647 | + securesystemslib.formats.ANY_PUBKEY_DICT_SCHEMA.check_match(key_dict) |
| 648 | + securesystemslib.formats.RSAKEY_SCHEMA.check_match( |
| 649 | + list(key_dict.values()).pop()) |
| 650 | + |
| 651 | + # Bad default rsa key type for ed25519 |
| 652 | + with self.assertRaises(securesystemslib.exceptions.Error): |
| 653 | + interface.import_public_keys_from_file([path_ed25519 + ".pub"]) |
| 654 | + |
| 655 | + # Bad ed25519 key type for rsa key |
| 656 | + with self.assertRaises(securesystemslib.exceptions.Error): |
| 657 | + interface.import_public_keys_from_file( |
| 658 | + [path_rsa + ".pub"], [KEY_TYPE_ED25519]) |
| 659 | + |
| 660 | + # Unsupported key type |
| 661 | + with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 662 | + interface.import_public_keys_from_file( |
| 663 | + [path_ed25519 + ".pub"], ["KEY_TYPE_UNSUPPORTED"]) |
| 664 | + |
| 665 | + # Mismatching arguments lists lenghts |
| 666 | + with self.assertRaises(securesystemslib.exceptions.FormatError): |
| 667 | + interface.import_public_keys_from_file( |
| 668 | + [path_rsa + ".pub", path_ed25519 + ".pub"], [KEY_TYPE_ED25519]) |
| 669 | + |
| 670 | + |
| 671 | + |
617 | 672 | # Run the test cases.
|
618 | 673 | if __name__ == '__main__':
|
619 | 674 | unittest.main()
|
0 commit comments