| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
# Copyright New York University and the in-toto contributors |
| 4 |
# SPDX-License-Identifier: Apache-2.0 |
| 5 |
|
| 6 |
""" |
| 7 |
<Program Name> |
| 8 |
test_signer.py |
| 9 |
|
| 10 |
<Author> |
| 11 |
Pradyumna Krishna <[email protected]> |
| 12 |
|
| 13 |
<Started> |
| 14 |
Jan 28, 2023 |
| 15 |
|
| 16 |
<Copyright> |
| 17 |
See LICENSE for licensing information. |
| 18 |
|
| 19 |
<Purpose> |
| 20 |
Test GPGKey, GPGSigner and GPGSignature class methods. |
| 21 |
""" |
| 22 |
|
| 23 |
import unittest |
| 24 |
|
| 25 |
from securesystemslib.exceptions import ( |
| 26 |
UnverifiedSignatureError, |
| 27 |
VerificationError, |
| 28 |
) |
| 29 |
from securesystemslib.gpg.constants import have_gpg |
| 30 |
from securesystemslib.gpg.functions import export_pubkey |
| 31 |
|
| 32 |
from in_toto.models._signer import GPGKey, GPGSignature, GPGSigner |
| 33 |
from tests.common import GPGKeysMixin, TmpDirMixin |
| 34 |
|
| 35 |
|
| 36 |
@unittest.skipIf(not have_gpg(), "gpg not found") |
| 37 |
class TestLegacyGPGKeyAndSigner(unittest.TestCase, TmpDirMixin, GPGKeysMixin): |
| 38 |
"""Test RSA gpg signature creation and verification.""" |
| 39 |
|
| 40 |
@classmethod |
| 41 |
def setUpClass(cls): |
| 42 |
cls.set_up_test_dir() |
| 43 |
cls.set_up_gpg_keys() |
| 44 |
|
| 45 |
cls.test_data = b"test_data" |
| 46 |
cls.wrong_data = b"something malicious" |
| 47 |
|
| 48 |
cls.default_keyid = cls.gpg_key_0C8A17 |
| 49 |
cls.signing_subkey_keyid = cls.gpg_key_D924E9 |
| 50 |
cls.expired_keyid = "e8ac80c924116dabb51d4b987cb07d6d2c199c7c" |
| 51 |
|
| 52 |
cls.default_key_dict = export_pubkey(cls.default_keyid, cls.gnupg_home) |
| 53 |
|
| 54 |
@classmethod |
| 55 |
def tearDownClass(cls): |
| 56 |
cls.tear_down_test_dir() |
| 57 |
|
| 58 |
def test_gpg_sign_and_verify_object_with_default_key(self): |
| 59 |
"""Create and verify a signature using the default key on the keyring.""" |
| 60 |
|
| 61 |
# Create a signature. |
| 62 |
signer = GPGSigner(homedir=self.gnupg_home) |
| 63 |
signature = signer.sign(self.test_data) |
| 64 |
|
| 65 |
# Generate Key from gnupg keyring. |
| 66 |
key = GPGKey.from_keyring(self.default_keyid, self.gnupg_home) |
| 67 |
|
| 68 |
key.verify_signature(signature, self.test_data) |
| 69 |
with self.assertRaises(UnverifiedSignatureError): |
| 70 |
key.verify_signature(signature, self.wrong_data) |
| 71 |
|
| 72 |
# Generate Key from dict. |
| 73 |
key = GPGKey.from_legacy_dict(self.default_key_dict) |
| 74 |
|
| 75 |
key.verify_signature(signature, self.test_data) |
| 76 |
with self.assertRaises(UnverifiedSignatureError): |
| 77 |
key.verify_signature(signature, self.wrong_data) |
| 78 |
|
| 79 |
def test_gpg_sign_and_verify_object(self): |
| 80 |
"""Create and verify a signature using the specific key on the keyring.""" |
| 81 |
|
| 82 |
# Create a signature. |
| 83 |
signer = GPGSigner(self.signing_subkey_keyid, self.gnupg_home) |
| 84 |
signature = signer.sign(self.test_data) |
| 85 |
|
| 86 |
# Generate Key from gnupg keyring. |
| 87 |
key = GPGKey.from_keyring(self.signing_subkey_keyid, self.gnupg_home) |
| 88 |
|
| 89 |
key.verify_signature(signature, self.test_data) |
| 90 |
with self.assertRaises(UnverifiedSignatureError): |
| 91 |
key.verify_signature(signature, self.wrong_data) |
| 92 |
|
| 93 |
# Generate Key from dict. |
| 94 |
key_dict = export_pubkey(self.signing_subkey_keyid, self.gnupg_home) |
| 95 |
key = GPGKey.from_dict(key_dict["keyid"], key_dict) |
| 96 |
|
| 97 |
key.verify_signature(signature, self.test_data) |
| 98 |
with self.assertRaises(UnverifiedSignatureError): |
| 99 |
key.verify_signature(signature, self.wrong_data) |
| 100 |
|
| 101 |
def test_verify_using_expired_keyid(self): |
| 102 |
"""Creates and verifies a signature using expired key on the keyring.""" |
| 103 |
|
| 104 |
# Create a signature. |
| 105 |
signer = GPGSigner(self.signing_subkey_keyid, self.gnupg_home) |
| 106 |
signature = signer.sign(self.test_data) |
| 107 |
|
| 108 |
# Verify signature using expired key. |
| 109 |
key = GPGKey.from_keyring(self.expired_keyid, self.gnupg_home) |
| 110 |
with self.assertRaises(VerificationError): |
| 111 |
key.verify_signature(signature, self.test_data) |
| 112 |
|
| 113 |
def test_gpg_signature_serialization(self): |
| 114 |
"""Tests from_dict and to_dict methods of GPGSignature.""" |
| 115 |
|
| 116 |
sig_dict = { |
| 117 |
"keyid": "f4f90403af58eef6", |
| 118 |
"signature": "c39f86e70e12e70e11d87eb7e3ab7d3b", |
| 119 |
"other_headers": "d8f8a89b5d71f07b842a", |
| 120 |
} |
| 121 |
|
| 122 |
signature = GPGSignature.from_dict(sig_dict) |
| 123 |
self.assertEqual(sig_dict, signature.to_dict()) |
| 124 |
|
| 125 |
def test_gpg_key_serialization(self): |
| 126 |
"""Test to check serialization methods of GPGKey.""" |
| 127 |
|
| 128 |
# Test loading and dumping of GPGKey. |
| 129 |
key = GPGKey.from_legacy_dict(self.default_key_dict) |
| 130 |
self.assertEqual(key.to_dict(), self.default_key_dict) |
| 131 |
|
| 132 |
# Test loading and dumping of GPGKey from keyring. |
| 133 |
key = GPGKey.from_keyring(self.default_keyid, self.gnupg_home) |
| 134 |
self.assertEqual(key.to_dict(), self.default_key_dict) |
| 135 |
|
| 136 |
def test_gpg_key_equality(self): |
| 137 |
"""Test to check equality between two GPGKey.""" |
| 138 |
|
| 139 |
# Generate two GPGkey. |
| 140 |
key1 = GPGKey.from_legacy_dict(self.default_key_dict) |
| 141 |
key2 = GPGKey.from_legacy_dict(self.default_key_dict) |
| 142 |
|
| 143 |
self.assertNotEqual(self.default_key_dict, key1) |
| 144 |
self.assertEqual(key2, key1) |
| 145 |
|
| 146 |
# Assert equality of key created from dict of first GPGKey. |
| 147 |
key2 = GPGKey.from_legacy_dict(key1.to_dict()) |
| 148 |
self.assertEqual(key2, key1) |
| 149 |
|
| 150 |
# Assert Inequalities. |
| 151 |
key2.type = "invalid" |
| 152 |
self.assertNotEqual(key2, key1) |
| 153 |
key2.type = key1.type |
| 154 |
|
| 155 |
key2.subkeys = {} |
| 156 |
self.assertNotEqual(key2, key1) |
| 157 |
key2.subkeys = key1.subkeys |
| 158 |
|
| 159 |
key2.keyval = {} |
| 160 |
self.assertNotEqual(key2, key1) |
| 161 |
key2.keyval = key1.keyval |
| 162 |
|
| 163 |
self.assertEqual(key2, key1) |