@@ -539,29 +539,56 @@ def _deb_copy_content_domain(
539539 return _deb_copy_content_domain
540540
541541
542- def import_signing_key (key_url , gpg_home ):
543- """Import a PGP key into a GPG home directory and trust it .
542+ def import_signing_key (key_url , home , * , backend = "gpg" ):
543+ """Import a PGP key into a keyring and return metadata .
544544
545- Returns ``(gpg, fingerprint, keyid)``.
545+ Returns `(gpg_instance_or_none, fingerprint, keyid)`. The first element
546+ is a `gnupg.GPG` instance when `backend` is `"gpg"`, or `None` when
547+ `backend` is `"sq"`.
546548 """
547- try :
548- import gnupg
549- except ImportError :
550- pytest .skip ("python-gnupg not installed" )
551-
552- gpg = gnupg .GPG (gnupghome = gpg_home )
553-
554549 response = requests .get (key_url )
555550 response .raise_for_status ()
556- result = gpg .import_keys (response .content )
557- assert result .count >= 1 , f"Failed to import key from { key_url } "
558551
559- key_info = gpg .list_keys ()[0 ]
560- fingerprint = key_info ["fingerprint" ]
561- keyid = key_info ["keyid" ]
562- gpg .trust_keys (fingerprint , "TRUST_ULTIMATE" )
552+ if backend == "sq" :
553+ from pysequoia import Cert
554+
555+ def openpgp_key_id (fingerprint ):
556+ """Return the OpenPGP key ID for a hexadecimal fingerprint.
557+
558+ OpenPGP v4 key IDs use the low-order 64 bits, while v6 key IDs use the
559+ high-order 64 bits. The fingerprint length distinguishes these versions.
560+ """
561+ return (fingerprint [:16 ] if len (fingerprint ) == 64 else fingerprint [- 16 :]).upper ()
562+
563+ completed = subprocess .run (
564+ ("sq" , "--home" , str (home ), "key" , "import" ),
565+ input = response .content ,
566+ capture_output = True ,
567+ )
568+ assert completed .returncode == 0 , completed .stderr .decode ()
569+
570+ cert = Cert .from_bytes (response .content )
571+ fingerprint = cert .fingerprint .upper ()
572+ keyid = openpgp_key_id (fingerprint )
573+
574+ return None , fingerprint , keyid
575+ else :
576+ try :
577+ import gnupg
578+ except ImportError :
579+ pytest .skip ("python-gnupg not installed" )
580+
581+ gpg = gnupg .GPG (gnupghome = home )
582+
583+ result = gpg .import_keys (response .content )
584+ assert result .count >= 1 , f"Failed to import key from { key_url } "
585+
586+ key_info = gpg .list_keys ()[0 ]
587+ fingerprint = key_info ["fingerprint" ]
588+ keyid = key_info ["keyid" ]
589+ gpg .trust_keys (fingerprint , "TRUST_ULTIMATE" )
563590
564- return gpg , fingerprint , keyid
591+ return gpg , fingerprint , keyid
565592
566593
567594def create_signing_service (
0 commit comments