pinentry: specify device name at PIN/passphrase entry UI

This commit is contained in:
Roman Zeyde
2018-03-10 21:39:55 +02:00
parent 870152a7af
commit d0497b0137
4 changed files with 33 additions and 23 deletions

View File

@@ -30,37 +30,36 @@ def _create_default_options_getter():
class UI(object):
"""UI for PIN/passphrase entry (for TREZOR devices)."""
def __init__(self):
def __init__(self, device_type, config):
"""C-tor."""
default_pinentry = 'pinentry' # by default, use GnuPG pinentry tool
self.pin_entry_binary = config.get('pin_entry_binary',
default_pinentry)
self.passphrase_entry_binary = config.get('passphrase_entry_binary',
default_pinentry)
self.options_getter = _create_default_options_getter()
self.pin_entry_binary = 'pinentry'
self.passphrase_entry_binary = 'pinentry'
@classmethod
def from_config_dict(cls, d):
"""Simple c-tor from configuration dictionary."""
obj = cls()
obj.pin_entry_binary = d.get('pin_entry_binary',
obj.pin_entry_binary)
obj.passphrase_entry_binary = d.get('passphrase_entry_binary',
obj.passphrase_entry_binary)
return obj
self.device_name = device_type.__name__
def get_pin(self):
"""Ask the user for (scrambled) PIN."""
return pinentry.interact(
description = (
'Use the numeric keypad to describe number positions.\n'
'The layout is:\n'
' 7 8 9\n'
' 4 5 6\n'
' 1 2 3\n'
'Please enter PIN:',
' 1 2 3')
return pinentry.interact(
title='{} PIN'.format(self.device_name),
prompt='PIN:',
description=description,
binary=self.pin_entry_binary,
options=self.options_getter())
def get_passphrase(self):
"""Ask the user for passphrase."""
return pinentry.interact(
'Please enter passphrase:',
title='{} passphrase'.format(self.device_name),
prompt='Passphrase:',
description=None,
binary=self.passphrase_entry_binary,
options=self.options_getter())

View File

@@ -26,7 +26,7 @@ def expect(p, prefixes):
raise ValueError('Unexpected response: {}'.format(resp))
def interact(description, binary, options):
def interact(title, description, prompt, binary, options):
"""Use GPG pinentry program to interact with the user."""
p = subprocess.Popen(args=[binary],
stdin=subprocess.PIPE,
@@ -34,10 +34,20 @@ def interact(description, binary, options):
env=os.environ)
expect(p, [b'OK'])
description = libagent.gpg.agent.serialize(description.encode('ascii'))
write(p, b'SETDESC ' + description + b'\n')
title = libagent.gpg.agent.serialize(title.encode('ascii'))
write(p, b'SETTITLE ' + title + b'\n')
expect(p, [b'OK'])
if description:
description = libagent.gpg.agent.serialize(description.encode('ascii'))
write(p, b'SETDESC ' + description + b'\n')
expect(p, [b'OK'])
if prompt:
prompt = libagent.gpg.agent.serialize(prompt.encode('ascii'))
write(p, b'SETPROMPT ' + prompt + b'\n')
expect(p, [b'OK'])
log.debug('setting %d options', len(options))
for opt in options:
write(p, b'OPTION ' + opt + b'\n')