From 2a613e87156b23c4aa6aa5ce38505cb285de6a18 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Fri, 5 Aug 2016 14:03:15 +0200 Subject: [PATCH] python: Clean up and modernize examples. * lang/python/examples/Examples.rst: Delete file. * lang/python/examples/t-edit.py: Likewise. This is actually a test case and has been moved to 'tests'. * lang/python/examples/assuan.py: New file. * lang/python/examples/decryption-filter.py: Likewise. * lang/python/examples/delkey.py: Modernize. * lang/python/examples/encrypt-to-all.py: Likewise. * lang/python/examples/exportimport.py: Likewise. * lang/python/examples/genkey.py: Likewise. * lang/python/examples/inter-edit.py: Likewise. * lang/python/examples/sign.py: Likewise. * lang/python/examples/signverify.py: Likewise. * lang/python/examples/simple.py: Likewise. * lang/python/examples/testCMSgetkey.py: Likewise. * lang/python/examples/verifydetails.py: Likewise. Signed-off-by: Justus Winter --- lang/python/examples/Examples.rst | 7 -- lang/python/examples/assuan.py | 25 ++++++ lang/python/examples/decryption-filter.py | 29 ++++++ lang/python/examples/delkey.py | 45 +++++----- lang/python/examples/encrypt-to-all.py | 80 +++++++---------- lang/python/examples/exportimport.py | 103 +++++++++------------- lang/python/examples/genkey.py | 41 ++++----- lang/python/examples/inter-edit.py | 94 ++++++++++---------- lang/python/examples/sign.py | 41 ++++----- lang/python/examples/signverify.py | 86 +++++------------- lang/python/examples/simple.py | 71 +++++++-------- lang/python/examples/t-edit.py | 62 ------------- lang/python/examples/testCMSgetkey.py | 55 +++++------- lang/python/examples/verifydetails.py | 89 ++++++++----------- 14 files changed, 344 insertions(+), 484 deletions(-) delete mode 100644 lang/python/examples/Examples.rst create mode 100644 lang/python/examples/assuan.py create mode 100644 lang/python/examples/decryption-filter.py delete mode 100644 lang/python/examples/t-edit.py diff --git a/lang/python/examples/Examples.rst b/lang/python/examples/Examples.rst deleted file mode 100644 index 18b03b27..00000000 --- a/lang/python/examples/Examples.rst +++ /dev/null @@ -1,7 +0,0 @@ -=============== -Example Scripts -=============== - -Most of the examples have been converted to work with Python 3, just as the original versions worked with Python 2. A small number produce errors on OS X, but may behave differently on other POSIX systems. The GTK based scripts (PyGtkGpgKeys.py and pygpa.py) have not been modified at all. - -When using or referring to the example scripts here, the most common change has been the byte encoded strings, so if something does not work then chances are that it will be related to the encoding or decoding of UTF-8. diff --git a/lang/python/examples/assuan.py b/lang/python/examples/assuan.py new file mode 100644 index 00000000..82b1e1d7 --- /dev/null +++ b/lang/python/examples/assuan.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2016 g10 Code GmbH +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . + +"""Demonstrate the use of the Assuan protocol engine""" + +import pyme + +with pyme.Context(protocol=pyme.constants.PROTOCOL_ASSUAN) as c: + # Invoke the pinentry to get a confirmation. + err = c.assuan_transact(['GET_CONFIRMATION', 'Hello there']) + print("You chose {}.".format("cancel" if err else "ok")) diff --git a/lang/python/examples/decryption-filter.py b/lang/python/examples/decryption-filter.py new file mode 100644 index 00000000..1647ca3b --- /dev/null +++ b/lang/python/examples/decryption-filter.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2016 g10 Code GmbH +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . + +"""A decryption filter + +This demonstrates decryption using pyme3 in three lines of code. To +be used like this: + +./decryption-filter.py message.plain + +""" + +import sys +import pyme +pyme.Context().decrypt(sys.stdin, sink=sys.stdout) diff --git a/lang/python/examples/delkey.py b/lang/python/examples/delkey.py index 3fb71eb4..e607f219 100755 --- a/lang/python/examples/delkey.py +++ b/lang/python/examples/delkey.py @@ -1,35 +1,30 @@ #!/usr/bin/env python3 +# +# Copyright (C) 2016 g10 Code GmbH # Copyright (C) 2004,2008 Igor Belyi # -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License as -# published by the Free Software Foundation; either version 2 of -# the License, or (at your option) any later version. +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. # -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. # -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA -# 02111-1307 USA +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . # Sample of key deletion -# It deletes keys for joe@example.org generated by genkey.pl script +# It deletes keys for joe@example.org generated by genkey.py script -from pyme import core +import pyme -core.check_version(None) +with pyme.Context() as c: + # Note: We must not modify the key store during iteration, + # therefore, we explicitly make a list. + keys = list(c.keylist("joe+pyme@example.org")) -# Note that we need to collect all keys out of the iterator return by -# c.op_keylist_all() method before starting to delete them. If you -# delete a key in the middle of iteration c.op_keylist_next() will -# raise INV_VALUE exception - -c = core.Context() -# 0 in keylist means to list not only public but secret keys as well. -for thekey in [x for x in c.op_keylist_all("joe+pyme@example.org", 0)]: - # 1 in delete means to delete not only public but secret keys as well. - c.op_delete(thekey, 1) + for k in keys: + c.op_delete(k, True) diff --git a/lang/python/examples/encrypt-to-all.py b/lang/python/examples/encrypt-to-all.py index 5e126760..4586f938 100755 --- a/lang/python/examples/encrypt-to-all.py +++ b/lang/python/examples/encrypt-to-all.py @@ -1,21 +1,21 @@ #!/usr/bin/env python3 +# +# Copyright (C) 2016 g10 Code GmbH # Copyright (C) 2008 Igor Belyi # Copyright (C) 2002 John Goerzen # -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License as -# published by the Free Software Foundation; either version 2 of -# the License, or (at your option) any later version. +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. # -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. # -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA -# 02111-1307 USA +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . """ This program will try to encrypt a simple message to each key on your @@ -24,44 +24,28 @@ be skipped and it will re-try the encryption.""" import sys import os -from pyme import core -from pyme.core import Data, Context +import pyme -core.check_version(None) - -plain = Data('This is my message.') - -c = Context() -c.set_armor(1) - -def sendto(keylist): - cipher = Data() - c.op_encrypt(keylist, 1, plain, cipher) - cipher.seek(0, os.SEEK_SET) - return cipher.read() - -names = [] -for key in c.op_keylist_all(None, 0): - try: - print(" *** Found key for %s" % key.uids[0].uid) +with pyme.Context(armor=True) as c: + recipients = list() + for key in c.keylist(): valid = 0 - for subkey in key.subkeys: - keyid = subkey.keyid - if keyid is None: - break - can_encrypt = subkey.can_encrypt - valid += can_encrypt - print(" Subkey %s: encryption %s" % - (keyid, can_encrypt and "enabled" or "disabled")) - except UnicodeEncodeError as e: - print(e) + if any(sk.can_encrypt for sk in key.subkeys): + recipients.append(key) + print("Adding recipient {0}.".format(key.uids[0].uid)) - if valid: - names.append(key) - else: - print(" This key cannot be used for encryption; skipping.") + ciphertext = None + while not ciphertext: + print("Encrypting to %d recipients" % len(recipients)) + try: + ciphertext, _, _ = c.encrypt(b'This is my message.', + recipients=recipients) + except pyme.errors.InvalidRecipients as e: + print("Encryption failed for these keys:\n{0!s}".format(e)) -passno = 0 + # filter out the bad keys + bad_keys = {bad.fpr for bad in e.recipients} + recipients = [r for r in recipients + if not r.subkeys[0].fpr in bad_keys] -print("Encrypting to %d recipients" % len(names)) -sys.stdout.buffer.write(sendto(names)) + sys.stdout.buffer.write(ciphertext) diff --git a/lang/python/examples/exportimport.py b/lang/python/examples/exportimport.py index d0e1fa89..39b1595c 100755 --- a/lang/python/examples/exportimport.py +++ b/lang/python/examples/exportimport.py @@ -1,75 +1,58 @@ #!/usr/bin/env python3 +# +# Copyright (C) 2016 g10 Code GmbH # Copyright (C) 2004,2008 Igor Belyi # -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License as -# published by the Free Software Foundation; either version 2 of -# the License, or (at your option) any later version. +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. # -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. # -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA -# 02111-1307 USA +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . # Sample of export and import of keys -# It uses keys for joe@example.org generated by genkey.pl script +# It uses keys for joe+pyme@example.org generated by genkey.py script import sys -from pyme import core +import os +import pyme -core.check_version(None) +user = "joe+pyme@example.org" -expkey = core.Data() -c = core.Context() -c.set_armor(1) -user = b"joe@example.org" +with pyme.Context(armor=True) as c, pyme.Data() as expkey: + print(" - Export %s's public keys - " % user) + c.op_export(user, 0, expkey) -print(" - Export %s's public keys - " % user) -c.op_export(user, 0, expkey) + # print out exported data to see how it looks in armor. + expkey.seek(0, os.SEEK_SET) + expstring = expkey.read() + if expstring: + sys.stdout.buffer.write(expstring) + else: + sys.exit("No %s's keys to export!" % user) -# print out exported data to see how it looks in armor. -expkey.seek(0, 0) -expstring = expkey.read() -if expstring: - print(expstring) -else: - print("No %s's keys to export!" % user) - sys.exit(0) +# delete keys to ensure that they came from our imported data. Note +# that if joe's key has private part as well we can only delete both +# of them. +with pyme.Context() as c: + # Note: We must not modify the key store during iteration, + # therfore, we explicitly make a list. + keys = list(c.keylist(user)) + for k in keys: + c.op_delete(k, True) -# delete keys to ensure that they came from our imported data. -# Note that since joe's key has private part as well we can only delete -# both of them. As a side effect joe won't have private key for this -# exported public one. If it's Ok with you uncomment the next 4 lines. -# print " - Delete %s's public keys - " % user -# for thekey in [x for x in c.op_keylist_all(user, 0)]: -# if not thekey.secret: -# c.op_delete(thekey, 1) - - -# initialize import data from a string as if it was read from a file. -newkey = core.Data(expstring) - -print(" - Import exported keys - ") -c.op_import(newkey) -result = c.op_import_result() - -# show the import result -if result: - print(" - Result of the import - ") - for k in dir(result): - if k not in result.__dict__ and not k.startswith("_"): - if k == "imports": - print(k, ":") - for impkey in result.__getattr__(k): - print(" fpr=%s result=%d status=%x" % - (impkey.fpr, impkey.result, impkey.status)) - else: - print(k, ":", result.__getattr__(k)) -else: - print(" - No import result - ") +with pyme.Context() as c: + print(" - Import exported keys - ") + c.op_import(expstring) + result = c.op_import_result() + if result: + print(result) + else: + sys.exit(" - No import result - ") diff --git a/lang/python/examples/genkey.py b/lang/python/examples/genkey.py index d5a88a70..66e382bc 100755 --- a/lang/python/examples/genkey.py +++ b/lang/python/examples/genkey.py @@ -1,31 +1,25 @@ #!/usr/bin/env python3 +# +# Copyright (C) 2016 g10 Code GmbH # Copyright (C) 2004 Igor Belyi # Copyright (C) 2002 John Goerzen # -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. # -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. # -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . -from pyme import core, callbacks +import pyme -# Initialize our context. -core.check_version(None) - -c = core.Context() -c.set_armor(1) -c.set_progress_cb(callbacks.progress_stdout, None) - -# This example from the GPGME manual +# This is the example from the GPGME manual. parms = """ Key-Type: RSA @@ -40,5 +34,8 @@ Expire-Date: 2020-12-31 """ -c.op_genkey(parms, None, None) -print(c.op_genkey_result().fpr) +with pyme.Context() as c: + c.set_progress_cb(pyme.callbacks.progress_stdout) + c.op_genkey(parms, None, None) + print("Generated key with fingerprint {0}.".format( + c.op_genkey_result().fpr)) diff --git a/lang/python/examples/inter-edit.py b/lang/python/examples/inter-edit.py index dcb47c25..8199cc63 100644 --- a/lang/python/examples/inter-edit.py +++ b/lang/python/examples/inter-edit.py @@ -1,58 +1,60 @@ #!/usr/bin/env python3 +# +# Copyright (C) 2016 g10 Code GmbH # Copyright (C) 2005 Igor Belyi # -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. # -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. # -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA -# 02111-1307 USA +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . + +"""Simple interactive editor to test editor scripts""" import sys -from pyme import core -from pyme.core import Data, Context -from pyme.constants import status - -core.check_version(None) +import pyme +import pyme.constants.status # Get names for the status codes -stat2str = {} -for name in dir(status): +status2str = {} +for name in dir(pyme.constants.status): if not name.startswith('__') and name != "util": - stat2str[getattr(status, name)] = name + status2str[getattr(pyme.constants.status, name)] = name - -# Print the output received since the last prompt before giving the new prompt -def edit_fnc(stat, args, helper): - global stat_strings - try: - while True: - helper["data"].seek(helper["skip"], 0) - data = helper["data"].read() - helper["skip"] += len(data) - sys.stdout.buffer.write(data) - return input("(%s) %s > " % (stat2str[stat], args)) - except EOFError: - pass - -# Simple interactive editor to test editor scripts if len(sys.argv) != 2: - sys.stderr.write("Usage: %s \n" % sys.argv[0]) -else: - c = Context() - out = Data() - c.op_keylist_start(sys.argv[1], 0) - key = c.op_keylist_next() - helper = {"skip": 0, "data": out} - c.op_edit(key, edit_fnc, helper, out) - print("[-- Final output --]") - out.seek(helper["skip"], 0) - sys.stdout.buffer.write(out.read()) + sys.exit("Usage: %s \n" % sys.argv[0]) + +name = sys.argv[1] + +with pyme.Context() as c: + keys = list(c.keylist(name)) + if len(keys) == 0: + sys.exit("No key matching {}.".format(name)) + if len(keys) > 1: + sys.exit("More than one key matching {}.".format(name)) + + key = keys[0] + print("Editing key {} ({}):".format(key.uids[0].uid, key.subkeys[0].fpr)) + + def edit_fnc(status, args): + print("Status: {} ({}), args: {} > ".format( + status2str[status], status, args), end='', flush=True) + + if not 'GET' in status2str[status]: + # no prompt + print() + return None + + try: + return input() + except EOFError: + return "quit" + + c.op_edit(key, edit_fnc, None, sys.stdout) diff --git a/lang/python/examples/sign.py b/lang/python/examples/sign.py index b6a1d3cd..0dd6a7c3 100755 --- a/lang/python/examples/sign.py +++ b/lang/python/examples/sign.py @@ -1,32 +1,25 @@ #!/usr/bin/env python3 -# Copyright (C) 2002 John Goerzen -# # -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. +# Copyright (C) 2016 g10 Code GmbH +# Copyright (C) 2002 John Goerzen # -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. # -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . import sys -import os -from pyme import core, callbacks +import pyme from pyme.constants.sig import mode -core.check_version(None) - -plain = core.Data(b"Test message") -sig = core.Data() -c = core.Context() -c.set_passphrase_cb(callbacks.passphrase_stdin, b'for signing') -c.op_sign(plain, sig, mode.CLEAR) -sig.seek(0, os.SEEK_SET) -sys.stdout.buffer.write(sig.read()) +with pyme.Context() as c: + signed, _ = c.sign(b"Test message", mode=mode.CLEAR) + sys.stdout.buffer.write(signed) diff --git a/lang/python/examples/signverify.py b/lang/python/examples/signverify.py index 6a63112a..7a24d719 100755 --- a/lang/python/examples/signverify.py +++ b/lang/python/examples/signverify.py @@ -1,77 +1,39 @@ #!/usr/bin/env python3 +# +# Copyright (C) 2016 g10 Code GmbH # Copyright (C) 2004,2008 Igor Belyi # -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. # -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. # -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . # Sample of unattended signing/verifying of a message. -# It uses keys for joe@example.org generated by genkey.pl script +# It uses keys for joe+pyme@example.org generated by genkey.py script import sys import os -from pyme import core +import pyme from pyme.constants.sig import mode -core.check_version(None) +user = "joe+pyme" -plain = core.Data(b"Test message") -sig = core.Data() -c = core.Context() -user = "joe" +with pyme.Context(pinentry_mode=pyme.constants.PINENTRY_MODE_LOOPBACK) as c: + keys = list(c.keylist(user)) + if len(keys) == 0: + sys.exit("No key matching {}.".format(user)) -c.signers_clear() -# Add joe@example.org's keys in the list of signers -for sigkey in c.op_keylist_all(user, 1): - if sigkey.can_sign: - c.signers_add(sigkey) -if not c.signers_enum(0): - print("No secret %s's keys suitable for signing!" % user) - sys.exit(0) + c.signers = keys[:1] + c.set_passphrase_cb(lambda *args: "Crypt0R0cks") + signed_data, _ = c.sign(b"Test message", mode=mode.CLEAR) -# This is a map between signer e-mail and its password -passlist = { - b"": b"Crypt0R0cks" - } - -# callback will return password based on the e-mail listed in the hint. -c.set_passphrase_cb(lambda x,y,z: passlist[x[x.rindex("<"):]]) - -c.op_sign(plain, sig, mode.CLEAR) - -# Print out the signature (don't forget to rewind since signing put sig at EOF) -sig.seek(0, os.SEEK_SET) -signedtext = sig.read() -sys.stdout.buffer.write(signedtext) - -# Create Data with signed text. -sig2 = core.Data(signedtext) -plain2 = core.Data() - -# Verify. -c.op_verify(sig2, None, plain2) -result = c.op_verify_result() - -# List results for all signatures. Status equal 0 means "Ok". -for index, sign in enumerate(result.signatures): - print("signature", index, ":") - print(" summary: ", sign.summary) - print(" status: ", sign.status) - print(" timestamp: ", sign.timestamp) - print(" fingerprint:", sign.fpr) - print(" uid: ", c.get_key(sign.fpr, 0).uids[0].uid) - -# Print "unsigned" text. Rewind since verify put plain2 at EOF. -plain2.seek(0, os.SEEK_SET) -print("\n") -sys.stdout.buffer.write(plain2.read()) + data, result = c.verify(signed_data, verify=keys[:1]) + print("Data: {!r}\nSignature: {!s}".format(data, result.signatures[0])) diff --git a/lang/python/examples/simple.py b/lang/python/examples/simple.py index 29a44492..50a3938b 100755 --- a/lang/python/examples/simple.py +++ b/lang/python/examples/simple.py @@ -1,52 +1,45 @@ #!/usr/bin/env python3 +# +# Copyright (C) 2016 g10 Code GmbH # Copyright (C) 2005 Igor Belyi # Copyright (C) 2002 John Goerzen # -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. # -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. # -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . import sys import os -from pyme import core, errors +import pyme -core.check_version(None) +with pyme.Context(armor=True) as c: + recipients = [] + print("Enter name of your recipient(s), end with a blank line.") + while True: + line = input() + if not line: + break + new = list(c.keylist(line)) + if not new: + print("Matched no known keys.") + else: + print("Adding {}.".format(", ".join(k.uids[0].name for k in new))) + recipients.extend(new) -# Set up our input and output buffers. + if not recipients: + sys.exit("No recipients.") -plain = core.Data('This is my message.') -cipher = core.Data() + print("Encrypting for {}.".format(", ".join(k.uids[0].name + for k in recipients))) -# Initialize our context. - -c = core.Context() -c.set_armor(1) - -# Set up the recipients. - -sys.stdout.write("Enter name of your recipient: ") -sys.stdout.flush() -name = sys.stdin.readline().strip() -c.op_keylist_start(name, 0) -r = c.op_keylist_next() - -if r == None: - print("The key for user \"%s\" was not found" % name) -else: - # Do the encryption. - try: - c.op_encrypt([r], 1, plain, cipher) - cipher.seek(0, os.SEEK_SET) - sys.stdout.buffer.write(cipher.read()) - except errors.GPGMEError as ex: - print(ex.getstring()) + ciphertext, _, _ = c.encrypt(b"This is my message,", recipients) + sys.stdout.buffer.write(ciphertext) diff --git a/lang/python/examples/t-edit.py b/lang/python/examples/t-edit.py deleted file mode 100644 index 4a3b8ac9..00000000 --- a/lang/python/examples/t-edit.py +++ /dev/null @@ -1,62 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (C) 2005 Igor Belyi -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -import sys -import os -from pyme import core -from pyme.core import Data, Context - -core.check_version(None) - -class KeyEditor: - def __init__(self): - self.steps = ["fpr", "expire", "1", "primary", "quit"] - self.step = 0 - - def edit_fnc(self, status, args, out): - print("[-- Response --]") - out.seek(0, os.SEEK_SET) - sys.stdout.buffer.write(out.read()) - print("[-- Code: %d, %s --]" % (status, args)) - - if args == "keyedit.prompt": - result = self.steps[self.step] - self.step += 1 - elif args == "keyedit.save.okay": - result = "Y" - elif args == "keygen.valid": - result = "0" - else: - result = None - - return result - -if not os.getenv("GNUPGHOME"): - print("Please, set GNUPGHOME env.var. pointing to GPGME's tests/gpg dir") -else: - c = Context() - c.set_passphrase_cb(lambda x,y,z: "abc") - out = Data() - c.op_keylist_start(b"Alpha", 0) - key = c.op_keylist_next() - if not key: - sys.exit("Key Alpha not found. " + - "Did you point GNUPGHOME to GPGME's tests/gpg dir?") - c.op_edit(key, KeyEditor().edit_fnc, out, out) - print("[-- Last response --]") - out.seek(0, os.SEEK_SET) - sys.stdout.buffer.write(out.read()) diff --git a/lang/python/examples/testCMSgetkey.py b/lang/python/examples/testCMSgetkey.py index 7c953012..7c642e68 100644 --- a/lang/python/examples/testCMSgetkey.py +++ b/lang/python/examples/testCMSgetkey.py @@ -1,47 +1,32 @@ #!/usr/bin/env python3 -# initial 20080124 bernhard@intevation.de -# 20080124-2: removed some superflous imports -# 20080703: adapted for pyme-0.8.0 -# This script is Free Software under GNU GPL v>=2. # -# No modification made for python3 port, Bernhard can field this one -# if it is still required. -- Ben McGinnes +# Copyright (C) 2016 g10 Code GmbH +# Copyright (C) 2008 Bernhard Reiter # -"""A test applicaton for gpg_get_key() protocol.CMS. +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . -Tested on Debian Etch with - pyme 0.8.0 (manually compiled) - libgpgme11 1.1.6-0kk2 - gpgsm 2.0.9-0kk2 -""" +"""A test applicaton for the CMS protocol.""" import sys -from pyme import core -from pyme.constants import protocol +import pyme -def printgetkeyresults(keyfpr): - """Run gpgme_get_key().""" +if len(sys.argv) != 2: + sys.exit("fingerprint or unique key ID for gpgme_get_key()") - # gpgme_check_version() necessary for initialisation according to - # gogme 1.1.6 and this is not done automatically in pyme-0.7.0 - print("gpgme version:", core.check_version(None)) - c = core.Context() - c.set_protocol(protocol.CMS) - - key = c.get_key(keyfpr, False) +with pyme.Context(protocol=pyme.constants.PROTOCOL_CMS) as c: + key = c.get_key(sys.argv[1], False) print("got key: ", key.subkeys[0].fpr) - for uid in key.uids: print(uid.uid) - -def main(): - if len(sys.argv) < 2: - print("fingerprint or unique key ID for gpgme_get_key()") - sys.exit(1) - - printgetkeyresults(sys.argv[1]) - - -if __name__ == "__main__": - main() diff --git a/lang/python/examples/verifydetails.py b/lang/python/examples/verifydetails.py index 99e5e0a7..b57ed84f 100755 --- a/lang/python/examples/verifydetails.py +++ b/lang/python/examples/verifydetails.py @@ -1,27 +1,21 @@ #!/usr/bin/env python3 -# initial 20080123 build from the example: -# very simple - probably INCOMPLETE -# 20080703 Bernhard -# added second usage for detached signatures. -# added output of signature.summary (another bitfield) -# printing signature bitfield in hex format -# # +# Copyright (C) 2016 g10 Code GmbH # Copyright (C) 2004,2008 Igor Belyi # Copyright (c) 2008 Bernhard Reiter # -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. # -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. # -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . import sys import os @@ -36,60 +30,47 @@ def print_engine_infos(): print(engine.file_name, engine.version) for proto in [protocol.OpenPGP, protocol.CMS]: - print(core.get_protocol_name(proto), core.engine_check_version(proto)) + print("Have {}? {}".format(core.get_protocol_name(proto), + core.engine_check_version(proto))) -def verifyprintdetails(sigfilename, filefilename=None): +def verifyprintdetails(filename, detached_sig_filename=None): """Verify a signature, print a lot of details.""" - c = core.Context() + with core.Context() as c: - # Create Data with signed text. - sig2 = core.Data(file=sigfilename) - if filefilename: - file2 = core.Data(file=filefilename) - plain2 = None - else: - file2 = None - plain2 = core.Data() + # Verify. + data, result = c.verify(open(filename), + open(detached_sig_filename) + if detached_sig_filename else None) - # Verify. - c.op_verify(sig2, file2, plain2) - result = c.op_verify_result() - - # List results for all signatures. Status equal 0 means "Ok". - for index, sign in enumerate(result.signatures): - print("signature", index, ":") - print(" summary: %#0x" % (sign.summary)) - print(" status: %#0x" % (sign.status)) - print(" timestamp: ", sign.timestamp) - print(" fingerprint:", sign.fpr) - print(" uid: ", c.get_key(sign.fpr, 0).uids[0].uid) + # List results for all signatures. Status equal 0 means "Ok". + for index, sign in enumerate(result.signatures): + print("signature", index, ":") + print(" summary: %#0x" % (sign.summary)) + print(" status: %#0x" % (sign.status)) + print(" timestamp: ", sign.timestamp) + print(" fingerprint:", sign.fpr) + print(" uid: ", c.get_key(sign.fpr, 0).uids[0].uid) # Print "unsigned" text if inline signature - if plain2: - #Rewind since verify put plain2 at EOF. - plain2.seek(0, os.SEEK_SET) - print("\n") - sys.stdout.buffer.write(plain2.read()) + if data: + sys.stdout.buffer.write(data) def main(): print_engine_infos() - print() - argc= len(sys.argv) + argc = len(sys.argv) if argc < 2 or argc > 3: - print("need a filename for inline signature") - print("or two filename for detached signature and file to check") - sys.exit(1) + sys.exit( + "Usage: {} [ ]".format( + sys.argv[0])) if argc == 2: - print("trying to verify file: " + sys.argv[1]) + print("trying to verify file {}.".format(sys.argv[1])) verifyprintdetails(sys.argv[1]) if argc == 3: - print("trying to verify signature %s for file %s" \ - % (sys.argv[1], sys.argv[2])) - + print("trying to verify signature {1} for file {0}.".format(*sys.argv)) verifyprintdetails(sys.argv[1], sys.argv[2]) if __name__ == "__main__":