[git] GPGME - branch, master, updated. gpgme-1.12.0-104-g2e7a14c

by Ben McGinnes cvs at cvs.gnupg.org
Mon Dec 10 21:17:55 CET 2018


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GnuPG Made Easy".

The branch, master has been updated
       via  2e7a14c9b369096775a035091c197f2d438142a0 (commit)
      from  fe7e01d164b64ad5e7f65cb80e4bf13f06d8d3ef (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 2e7a14c9b369096775a035091c197f2d438142a0
Author: Ben McGinnes <ben at adversary.org>
Date:   Tue Dec 11 07:14:28 2018 +1100

    python: HKP search and import updates
    
    * Tweaked the code again so that it can also handle the cases where
      someone has included a hexadecimal string in their user ID.
    * Updated the HOWTO to match.
    * Exported to .rst and .texi.

diff --git a/lang/python/doc/rst/gpgme-python-howto.rst b/lang/python/doc/rst/gpgme-python-howto.rst
index 1bd069c..7843965 100644
--- a/lang/python/doc/rst/gpgme-python-howto.rst
+++ b/lang/python/doc/rst/gpgme-python-howto.rst
@@ -932,6 +932,14 @@ IDs (e.g. ``0xDEADBEEF``) should no longer be used due to the relative
 ease by which such key IDs can be reproduced, as demonstrated by the
 Evil32 Project in 2014 (which was subsequently exploited in 2016).
 
+Testing for whether a string in any given search is or may be a
+hexadecimal value which may be missing the leading ``0x`` is a simple
+matter of using a try/except statement which attempts to convert the
+string as hex to an integer and then back to hex; then using that to
+search with. Raising a ValueError simply results in treating the string
+as a string. This is the method and logic utilised in the
+``import-keys-hkp.py`` script (see below).
+
 .. _import-protonmail:
 
 Working with ProtonMail
@@ -1068,6 +1076,7 @@ the keys found.
    c = gpg.Context()
    server = hkp4py.KeyServer("hkps://hkps.pool.sks-keyservers.net")
    results = []
+   keys = []
 
    if len(sys.argv) > 2:
        pattern = " ".join(sys.argv[1:])
@@ -1076,22 +1085,56 @@ the keys found.
    else:
        pattern = input("Enter the pattern to search for keys or user IDs: ")
 
-   try:
-       keys = server.search(pattern)
-       print("Found {0} key(s).".format(len(keys)))
-   except Exception as e:
-       keys = []
+
+   if pattern is not None:
+       try:
+           key = server.search(hex(int(pattern, 16)))
+           keyed = True
+       except ValueError as ve:
+           key = server.search(pattern)
+           keyed = False
+
+       if key is not None:
+           keys.append(key[0])
+           if keyed is True:
+               try:
+                   fob = server.search(pattern)
+               except:
+                   fob = None
+               if fob is not None:
+                   keys.append(fob[0])
+           else:
+               pass
+       else:
+           pass
+
        for logrus in pattern.split():
-           if logrus.startswith("0x") is True:
+           try:
+               key = server.search(hex(int(logrus, 16)))
+               hexed = True
+           except ValueError as ve:
                key = server.search(logrus)
+               hexed = False
+
+           if key is not None:
+               keys.append(key[0])
+               if hexed is True:
+                   try:
+                       fob = server.search(logrus)
+                   except:
+                       fob = None
+                   if fob is not None:
+                       keys.append(fob[0])
+               else:
+                   pass
            else:
-               key = server.search("0x{0}".format(logrus))
-           keys.append(key[0])
-       print("Found {0} key(s).".format(len(keys)))
+               pass
 
-   for key in keys:
-       import_result = c.key_import(key.key_blob)
-       results.append(import_result)
+
+   if len(keys) > 0:
+       for key in keys:
+           import_result = c.key_import(key.key_blob)
+           results.append(import_result)
 
    for result in results:
        if result is not None and hasattr(result, "considered") is False:
diff --git a/lang/python/doc/src/gpgme-python-howto b/lang/python/doc/src/gpgme-python-howto
index 9820d86..0c1239b 100644
--- a/lang/python/doc/src/gpgme-python-howto
+++ b/lang/python/doc/src/gpgme-python-howto
@@ -963,6 +963,14 @@ relative ease by which such key IDs can be reproduced, as demonstrated
 by the Evil32 Project in 2014 (which was subsequently exploited in
 2016).
 
+Testing for whether a string in any given search is or may be a
+hexadecimal value which may be missing the leading =0x= is a simple
+matter of using a try/except statement which attempts to convert the
+string as hex to an integer and then back to hex; then using that to
+search with.  Raising a ValueError simply results in treating the
+string as a string.  This is the method and logic utilised in the
+=import-keys-hkp.py= script (see below).
+
 
 *** Working with ProtonMail
     :PROPERTIES:
@@ -1097,6 +1105,7 @@ import sys
 c = gpg.Context()
 server = hkp4py.KeyServer("hkps://hkps.pool.sks-keyservers.net")
 results = []
+keys = []
 
 if len(sys.argv) > 2:
     pattern = " ".join(sys.argv[1:])
@@ -1105,22 +1114,56 @@ elif len(sys.argv) == 2:
 else:
     pattern = input("Enter the pattern to search for keys or user IDs: ")
 
-try:
-    keys = server.search(pattern)
-    print("Found {0} key(s).".format(len(keys)))
-except Exception as e:
-    keys = []
+
+if pattern is not None:
+    try:
+        key = server.search(hex(int(pattern, 16)))
+        keyed = True
+    except ValueError as ve:
+        key = server.search(pattern)
+        keyed = False
+
+    if key is not None:
+        keys.append(key[0])
+        if keyed is True:
+            try:
+                fob = server.search(pattern)
+            except:
+                fob = None
+            if fob is not None:
+                keys.append(fob[0])
+        else:
+            pass
+    else:
+        pass
+
     for logrus in pattern.split():
-        if logrus.startswith("0x") is True:
+        try:
+            key = server.search(hex(int(logrus, 16)))
+            hexed = True
+        except ValueError as ve:
             key = server.search(logrus)
+            hexed = False
+
+        if key is not None:
+            keys.append(key[0])
+            if hexed is True:
+                try:
+                    fob = server.search(logrus)
+                except:
+                    fob = None
+                if fob is not None:
+                    keys.append(fob[0])
+            else:
+                pass
         else:
-            key = server.search("0x{0}".format(logrus))
-        keys.append(key[0])
-    print("Found {0} key(s).".format(len(keys)))
+            pass
 
-for key in keys:
-    import_result = c.key_import(key.key_blob)
-    results.append(import_result)
+
+if len(keys) > 0:
+    for key in keys:
+        import_result = c.key_import(key.key_blob)
+        results.append(import_result)
 
 for result in results:
     if result is not None and hasattr(result, "considered") is False:
diff --git a/lang/python/doc/texinfo/gpgme-python-howto.texi b/lang/python/doc/texinfo/gpgme-python-howto.texi
index 420ea7d..552ef91 100644
--- a/lang/python/doc/texinfo/gpgme-python-howto.texi
+++ b/lang/python/doc/texinfo/gpgme-python-howto.texi
@@ -1134,6 +1134,14 @@ relative ease by which such key IDs can be reproduced, as demonstrated
 by the Evil32 Project in 2014 (which was subsequently exploited in
 2016).
 
+Testing for whether a string in any given search is or may be a
+hexadecimal value which may be missing the leading @samp{0x} is a simple
+matter of using a try/except statement which attempts to convert the
+string as hex to an integer and then back to hex; then using that to
+search with.  Raising a ValueError simply results in treating the
+string as a string.  This is the method and logic utilised in the
+ at samp{import-keys-hkp.py} script (see below).
+
 @menu
 * Working with ProtonMail::
 * Importing with HKP for Python::
@@ -1268,6 +1276,7 @@ import sys
 c = gpg.Context()
 server = hkp4py.KeyServer("hkps://hkps.pool.sks-keyservers.net")
 results = []
+keys = []
 
 if len(sys.argv) > 2:
     pattern = " ".join(sys.argv[1:])
@@ -1276,22 +1285,56 @@ elif len(sys.argv) == 2:
 else:
     pattern = input("Enter the pattern to search for keys or user IDs: ")
 
-try:
-    keys = server.search(pattern)
-    print("Found @{0@} key(s).".format(len(keys)))
-except Exception as e:
-    keys = []
+
+if pattern is not None:
+    try:
+        key = server.search(hex(int(pattern, 16)))
+        keyed = True
+    except ValueError as ve:
+        key = server.search(pattern)
+        keyed = False
+
+    if key is not None:
+        keys.append(key[0])
+        if keyed is True:
+            try:
+                fob = server.search(pattern)
+            except:
+                fob = None
+            if fob is not None:
+                keys.append(fob[0])
+        else:
+            pass
+    else:
+        pass
+
     for logrus in pattern.split():
-        if logrus.startswith("0x") is True:
+        try:
+            key = server.search(hex(int(logrus, 16)))
+            hexed = True
+        except ValueError as ve:
             key = server.search(logrus)
+            hexed = False
+
+        if key is not None:
+            keys.append(key[0])
+            if hexed is True:
+                try:
+                    fob = server.search(logrus)
+                except:
+                    fob = None
+                if fob is not None:
+                    keys.append(fob[0])
+            else:
+                pass
         else:
-            key = server.search("0x@{0@}".format(logrus))
-        keys.append(key[0])
-    print("Found @{0@} key(s).".format(len(keys)))
+            pass
 
-for key in keys:
-    import_result = c.key_import(key.key_blob)
-    results.append(import_result)
+
+if len(keys) > 0:
+    for key in keys:
+        import_result = c.key_import(key.key_blob)
+        results.append(import_result)
 
 for result in results:
     if result is not None and hasattr(result, "considered") is False:
diff --git a/lang/python/examples/howto/import-keys-hkp.py b/lang/python/examples/howto/import-keys-hkp.py
index 832a888..1abd0fd 100755
--- a/lang/python/examples/howto/import-keys-hkp.py
+++ b/lang/python/examples/howto/import-keys-hkp.py
@@ -44,18 +44,51 @@ elif len(sys.argv) == 2:
 else:
     pattern = input("Enter the pattern to search for keys or user IDs: ")
 
+
 if pattern is not None:
     try:
         key = server.search(hex(int(pattern, 16)))
-    except ValueError as e:
+        keyed = True
+    except ValueError as ve:
         key = server.search(pattern)
-    keys.append(key[0])
+        keyed = False
+
+    if key is not None:
+        keys.append(key[0])
+        if keyed is True:
+            try:
+                fob = server.search(pattern)
+            except:
+                fob = None
+            if fob is not None:
+                keys.append(fob[0])
+        else:
+            pass
+    else:
+        pass
+
     for logrus in pattern.split():
         try:
             key = server.search(hex(int(logrus, 16)))
-        except ValueErrer as ve:
+            hexed = True
+        except ValueError as ve:
             key = server.search(logrus)
-        keys.append(key[0])
+            hexed = False
+
+        if key is not None:
+            keys.append(key[0])
+            if hexed is True:
+                try:
+                    fob = server.search(logrus)
+                except:
+                    fob = None
+                if fob is not None:
+                    keys.append(fob[0])
+            else:
+                pass
+        else:
+            pass
+
 
 if len(keys) > 0:
     for key in keys:
@@ -90,4 +123,4 @@ The key IDs for all considered keys were:
             print(result.imports[i].fpr)
         print("")
     else:
-        pass
+        print("No keys were imported or found.")

-----------------------------------------------------------------------

Summary of changes:
 lang/python/doc/rst/gpgme-python-howto.rst      | 67 ++++++++++++++++++++-----
 lang/python/doc/src/gpgme-python-howto          | 67 ++++++++++++++++++++-----
 lang/python/doc/texinfo/gpgme-python-howto.texi | 67 ++++++++++++++++++++-----
 lang/python/examples/howto/import-keys-hkp.py   | 43 ++++++++++++++--
 4 files changed, 203 insertions(+), 41 deletions(-)


hooks/post-receive
-- 
GnuPG Made Easy
http://git.gnupg.org




More information about the Gnupg-commits mailing list