[elbe-devel] [PATCH v2 6/9] egpg: Implement unarmor_openpgp_keyring

Bastian Germann bage at linutronix.de
Mon Mar 6 12:47:43 CET 2023


Add a function to convert an ascii-armored OpenPGP key to its binary
representation.

Signed-off-by: Bastian Germann <bage at linutronix.de>
---
 elbepack/egpg.py | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/elbepack/egpg.py b/elbepack/egpg.py
index 653d02d856..36e85e3f61 100644
--- a/elbepack/egpg.py
+++ b/elbepack/egpg.py
@@ -5,6 +5,8 @@
 #
 # SPDX-License-Identifier: GPL-3.0-or-later
 
+import binascii
+import logging
 import os
 
 from gpg import core
@@ -12,7 +14,7 @@ from gpg.constants import sigsum, sig, PROTOCOL_OpenPGP
 from gpg.errors import GPGMEError, KeyNotFound, InvalidSigners
 
 from elbepack.filesystem import hostfs
-from elbepack.shellhelper import system
+from elbepack.shellhelper import command_out_stderr, system
 
 elbe_internal_key_param = """
 <GnupgKeyParms format="internal">
@@ -272,3 +274,15 @@ def generate_elbe_internal_key():
 def export_key(fingerprint, outfile):
     system(f"/usr/bin/gpg -a -o {outfile} --export {fingerprint}",
            env_add={"GNUPGHOME": "/var/cache/elbe/gnupg"})
+
+def unarmor_openpgp_keyring(armored):
+    """
+    Unarmors one ascii-armored (string) OpenPGP keyring.
+    Returns a binary string (empty for invalid keys).
+    """
+    conv_cmd = command_out_stderr('/usr/bin/gpg --no-options --dearmor', stdin=armored)
+    if conv_cmd[0]:
+        logging.error(conv_cmd[2])
+        return b""
+
+    return conv_cmd[1]
-- 
2.39.2



More information about the elbe-devel mailing list