Skip to content

Commit 42171a2

Browse files
committed
Add gpg.export_pubkeys function
Add convenience function to export multiple public keys from a gpg keyring into an sslib dict format at once and tests. Note: Uses the new pseudo-standard docstring style suggested in secure-systems-lab/code-style-guidelines#20. All new interface functions should use that style (existing docstrings will be converted in separate PRs).
1 parent 94d83b9 commit 42171a2

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

securesystemslib/gpg/functions.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,29 @@ def export_pubkey(keyid, homedir=None):
299299
key_bundle = securesystemslib.gpg.common.get_pubkey_bundle(key_packet, keyid)
300300

301301
return key_bundle
302+
303+
304+
def export_pubkeys(keyids, homedir=None):
305+
"""Export multiple public keys from a GnuPG keyring.
306+
307+
Arguments:
308+
keyid: A list of OpenPGP keyids in KEYID_SCHEMA format.
309+
homedir (optional): A path to the GnuPG home directory. If not set the
310+
default GnuPG home directory is used.
311+
312+
Raises:
313+
TypeError: Keyids is not iterable.
314+
See 'export_pubkey' for other exceptions.
315+
316+
Returns:
317+
A dict with OpenPGP keyids for dict keys and keys in GPG_PUBKEY_SCHEMA
318+
format for values.
319+
320+
"""
321+
public_key_dict = {}
322+
for gpg_keyid in keyids:
323+
public_key = export_pubkey(gpg_keyid, homedir=homedir)
324+
keyid = public_key["keyid"]
325+
public_key_dict[keyid] = public_key
326+
327+
return public_key_dict

tests/check_public_interfaces_gpg.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def test_gpg_functions(self):
4848
export_pubkey('f00')
4949
self.assertEqual(NO_GPG_MSG, str(ctx.exception))
5050

51+
with self.assertRaises(UnsupportedLibraryError) as ctx:
52+
export_pubkeys(['f00'])
53+
self.assertEqual(NO_GPG_MSG, str(ctx.exception))
54+
5155
with self.assertRaises(UnsupportedLibraryError) as ctx:
5256
get_version()
5357
self.assertEqual(NO_GPG_MSG, str(ctx.exception))

tests/test_gpg.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
from securesystemslib import process
4242
from securesystemslib.gpg.functions import (create_signature, export_pubkey,
43-
verify_signature)
43+
verify_signature, export_pubkeys)
4444
from securesystemslib.gpg.util import (get_version, is_version_fully_supported,
4545
get_hashing_class, parse_packet_header, parse_subpacket_header)
4646
from securesystemslib.gpg.rsa import create_pubkey as rsa_create_pubkey
@@ -55,7 +55,8 @@
5555
from securesystemslib.gpg.exceptions import (PacketParsingError,
5656
PacketVersionNotSupportedError, SignatureAlgorithmNotSupportedError,
5757
KeyNotFoundError, CommandError, KeyExpirationError)
58-
from securesystemslib.formats import GPG_PUBKEY_SCHEMA
58+
from securesystemslib.formats import (GPG_PUBKEY_SCHEMA,
59+
ANY_PUBKEY_DICT_SCHEMA)
5960

6061

6162
@unittest.skipIf(not HAVE_GPG, "gpg not found")
@@ -481,6 +482,8 @@ class TestGPGRSA(unittest.TestCase):
481482
unsupported_subkey_keyid = "611A9B648E16F54E8A7FAD5DA51E8CDF3B06524F"
482483
expired_key_keyid = "E8AC80C924116DABB51D4B987CB07D6D2C199C7C"
483484

485+
keyid_768C43 = "7B3ABB26B97B655AB9296BD15B0BD02E1C768C43"
486+
484487
@classmethod
485488
def setUpClass(self):
486489
# Create directory to run the tests without having everything blow up
@@ -547,6 +550,17 @@ def test_export_pubkey(self):
547550
self.assertDictEqual(key_data, key_data2)
548551

549552

553+
def test_export_pubkeys(self):
554+
"""Test export multiple pubkeys at once. """
555+
key_dict = export_pubkeys([self.default_keyid, self.keyid_768C43],
556+
homedir=self.gnupg_home)
557+
558+
ANY_PUBKEY_DICT_SCHEMA.check_match(key_dict)
559+
self.assertListEqual(
560+
sorted([self.default_keyid.lower(), self.keyid_768C43.lower()]),
561+
sorted(key_dict.keys()))
562+
563+
550564
def test_gpg_sign_and_verify_object_with_default_key(self):
551565
"""Create a signature using the default key on the keyring """
552566

0 commit comments

Comments
 (0)