2018-02-27 09:17:53 +00:00
|
|
|
"""UIs for PIN/passphrase entry."""
|
2018-03-07 10:10:58 +00:00
|
|
|
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
|
2018-03-11 13:05:54 +00:00
|
|
|
from .. import util
|
2018-03-07 10:10:58 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class UI(object):
|
|
|
|
"""UI for PIN/passphrase entry (for TREZOR devices)."""
|
|
|
|
|
2018-03-11 11:34:50 +00:00
|
|
|
def __init__(self, device_type, config=None):
|
2018-03-07 10:10:58 +00:00
|
|
|
"""C-tor."""
|
2018-03-10 19:39:55 +00:00
|
|
|
default_pinentry = 'pinentry' # by default, use GnuPG pinentry tool
|
2018-03-11 11:34:50 +00:00
|
|
|
if config is None:
|
|
|
|
config = {}
|
2018-03-10 19:39:55 +00:00
|
|
|
self.pin_entry_binary = config.get('pin_entry_binary',
|
|
|
|
default_pinentry)
|
|
|
|
self.passphrase_entry_binary = config.get('passphrase_entry_binary',
|
|
|
|
default_pinentry)
|
2018-03-11 12:33:54 +00:00
|
|
|
self.options_getter = create_default_options_getter()
|
2018-03-10 19:39:55 +00:00
|
|
|
self.device_name = device_type.__name__
|
2018-03-07 10:10:58 +00:00
|
|
|
|
|
|
|
def get_pin(self):
|
|
|
|
"""Ask the user for (scrambled) PIN."""
|
2018-03-10 19:39:55 +00:00
|
|
|
description = (
|
2018-03-07 10:10:58 +00:00
|
|
|
'Use the numeric keypad to describe number positions.\n'
|
|
|
|
'The layout is:\n'
|
|
|
|
' 7 8 9\n'
|
|
|
|
' 4 5 6\n'
|
2018-03-10 19:39:55 +00:00
|
|
|
' 1 2 3')
|
2018-03-11 12:33:54 +00:00
|
|
|
return interact(
|
2018-03-10 19:39:55 +00:00
|
|
|
title='{} PIN'.format(self.device_name),
|
|
|
|
prompt='PIN:',
|
|
|
|
description=description,
|
2018-03-07 10:10:58 +00:00
|
|
|
binary=self.pin_entry_binary,
|
|
|
|
options=self.options_getter())
|
|
|
|
|
|
|
|
def get_passphrase(self):
|
|
|
|
"""Ask the user for passphrase."""
|
2018-03-11 12:33:54 +00:00
|
|
|
return interact(
|
2018-03-10 19:39:55 +00:00
|
|
|
title='{} passphrase'.format(self.device_name),
|
|
|
|
prompt='Passphrase:',
|
|
|
|
description=None,
|
2018-03-07 10:10:58 +00:00
|
|
|
binary=self.passphrase_entry_binary,
|
|
|
|
options=self.options_getter())
|
2018-03-11 12:33:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_default_options_getter():
|
|
|
|
"""Return current TTY and DISPLAY settings for GnuPG pinentry."""
|
|
|
|
options = []
|
|
|
|
try:
|
|
|
|
ttyname = subprocess.check_output(args=['tty']).strip()
|
|
|
|
options.append(b'ttyname=' + ttyname)
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
log.warning('no TTY found: %s', e)
|
|
|
|
|
|
|
|
display = os.environ.get('DISPLAY')
|
|
|
|
if display is not None:
|
|
|
|
options.append('display={}'.format(display).encode('ascii'))
|
|
|
|
else:
|
|
|
|
log.warning('DISPLAY not defined')
|
|
|
|
|
|
|
|
log.info('using %s for pinentry options', options)
|
|
|
|
return lambda: options
|
|
|
|
|
|
|
|
|
|
|
|
def write(p, line):
|
|
|
|
"""Send and flush a single line to the subprocess' stdin."""
|
|
|
|
log.debug('%s <- %r', p.args, line)
|
|
|
|
p.stdin.write(line)
|
|
|
|
p.stdin.flush()
|
|
|
|
|
|
|
|
|
2018-03-13 14:50:46 +00:00
|
|
|
class UnexpectedError(Exception):
|
|
|
|
"""Unexpected response."""
|
|
|
|
|
|
|
|
|
2018-03-14 10:13:44 +00:00
|
|
|
def expect(p, prefixes, confidential=False):
|
2018-03-11 12:33:54 +00:00
|
|
|
"""Read a line and return it without required prefix."""
|
|
|
|
resp = p.stdout.readline()
|
2018-03-14 10:13:44 +00:00
|
|
|
log.debug('%s -> %r', p.args, resp if not confidential else '********')
|
2018-03-11 12:33:54 +00:00
|
|
|
for prefix in prefixes:
|
|
|
|
if resp.startswith(prefix):
|
|
|
|
return resp[len(prefix):]
|
2018-03-13 14:50:46 +00:00
|
|
|
raise UnexpectedError(resp)
|
2018-03-11 12:33:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def interact(title, description, prompt, binary, options):
|
|
|
|
"""Use GPG pinentry program to interact with the user."""
|
2018-03-14 10:15:08 +00:00
|
|
|
args = [binary]
|
|
|
|
p = subprocess.Popen(args=args,
|
2018-03-11 12:33:54 +00:00
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
env=os.environ)
|
2018-03-14 10:15:08 +00:00
|
|
|
p.args = args # TODO: remove after Python 2 deprecation.
|
2018-03-11 12:33:54 +00:00
|
|
|
expect(p, [b'OK'])
|
|
|
|
|
2018-03-11 13:05:54 +00:00
|
|
|
title = util.assuan_serialize(title.encode('ascii'))
|
2018-03-11 12:33:54 +00:00
|
|
|
write(p, b'SETTITLE ' + title + b'\n')
|
|
|
|
expect(p, [b'OK'])
|
|
|
|
|
|
|
|
if description:
|
2018-03-11 13:05:54 +00:00
|
|
|
description = util.assuan_serialize(description.encode('ascii'))
|
2018-03-11 12:33:54 +00:00
|
|
|
write(p, b'SETDESC ' + description + b'\n')
|
|
|
|
expect(p, [b'OK'])
|
|
|
|
|
|
|
|
if prompt:
|
2018-03-11 13:05:54 +00:00
|
|
|
prompt = util.assuan_serialize(prompt.encode('ascii'))
|
2018-03-11 12:33:54 +00:00
|
|
|
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')
|
|
|
|
expect(p, [b'OK', b'ERR'])
|
|
|
|
|
|
|
|
write(p, b'GETPIN\n')
|
2018-03-14 10:13:44 +00:00
|
|
|
pin = expect(p, [b'OK', b'D '], confidential=True)
|
2018-03-11 12:33:54 +00:00
|
|
|
|
|
|
|
p.communicate() # close stdin and wait for the process to exit
|
|
|
|
exit_code = p.wait()
|
|
|
|
if exit_code:
|
|
|
|
raise subprocess.CalledProcessError(exit_code, binary)
|
|
|
|
|
|
|
|
return pin.decode('ascii').strip()
|