From 17ea941add7fc243b97dc9d38470ed5e053d32bd Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Wed, 7 Mar 2018 13:19:02 +0200 Subject: [PATCH] gpg: use pinentry UI for initialization and agent --- libagent/gpg/__init__.py | 13 ++++++++++++- libagent/gpg/agent.py | 8 ++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/libagent/gpg/__init__.py b/libagent/gpg/__init__.py index f9ba437..a37636b 100644 --- a/libagent/gpg/__init__.py +++ b/libagent/gpg/__init__.py @@ -158,7 +158,9 @@ default-key \"{1}\" f.write("""# Hardware-based GPG agent emulator log-file {0}/gpg-agent.log verbosity 2 -""".format(homedir)) +pin_entry_binary {1} +passphrase_entry_binary {2} +""".format(homedir, args.pin_entry_binary, args.passphrase_entry_binary)) # Prepare a helper script for setting up the new identity with open(os.path.join(homedir, 'env'), 'w') as f: @@ -223,6 +225,7 @@ def run_agent(device_type): env = {'GNUPGHOME': args.homedir} sock_path = keyring.get_agent_sock_path(env=env) pubkey_bytes = keyring.export_public_keys(env=env) + device_type.ui = device.ui.UI.from_config_dict(config) handler = agent.Handler(device=device_type(), pubkey_bytes=pubkey_bytes) with server.unix_domain_socket_server(sock_path) as sock: for conn in agent.yield_connections(sock): @@ -259,6 +262,12 @@ def main(device_type): p.add_argument('-t', '--time', type=int, default=int(time.time())) p.add_argument('-v', '--verbose', default=0, action='count') p.add_argument('-s', '--subkey', default=False, action='store_true') + + p.add_argument('--pin-entry-binary', type=str, default='pinentry', + help='Path to PIN entry UI helper.') + p.add_argument('--passphrase-entry-binary', type=str, default='pinentry', + help='Path to passphrase entry UI helper.') + p.set_defaults(func=run_init) p = subparsers.add_parser('unlock', help='unlock the hardware device') @@ -266,4 +275,6 @@ def main(device_type): p.set_defaults(func=run_unlock) args = parser.parse_args() + device_type.ui = device.ui.UI.from_config_dict(vars(args)) + return args.func(device_type=device_type, args=args) diff --git a/libagent/gpg/agent.py b/libagent/gpg/agent.py index 04e3182..c4271e5 100644 --- a/libagent/gpg/agent.py +++ b/libagent/gpg/agent.py @@ -77,19 +77,23 @@ class AgentStop(Exception): """Raised to close the agent.""" +# pylint: disable=too-many-instance-attributes class Handler(object): """GPG agent requests' handler.""" - # pylint: disable=too-many-instance-attributes + def _get_options(self): + return self.options + def __init__(self, device, pubkey_bytes): """C-tor.""" + self.reset() + device.ui.options_getter = self._get_options self.client = client.Client(device=device) # Cache public keys from GnuPG self.pubkey_bytes = pubkey_bytes # "Clone" existing GPG version self.version = keyring.gpg_version() - self.reset() self.handlers = { b'RESET': lambda *_: self.reset(), b'OPTION': lambda _, args: self.handle_option(*args),