2016-02-19 08:07:33 +00:00
|
|
|
"""
|
|
|
|
Connection to hardware authentication device.
|
|
|
|
|
|
|
|
It is used for getting SSH public keys and ECDSA signing of server requests.
|
|
|
|
"""
|
2016-01-09 14:06:47 +00:00
|
|
|
import binascii
|
2015-08-17 14:59:05 +00:00
|
|
|
import io
|
2016-01-09 14:06:47 +00:00
|
|
|
import logging
|
2015-08-17 14:59:05 +00:00
|
|
|
import re
|
|
|
|
import struct
|
|
|
|
|
2016-02-15 18:53:14 +00:00
|
|
|
from . import factory, formats, util
|
2015-08-17 14:59:05 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Client(object):
|
2016-02-19 08:07:33 +00:00
|
|
|
"""Client wrapper for SSH authentication device."""
|
2015-08-17 14:59:05 +00:00
|
|
|
|
2016-01-15 11:07:01 +00:00
|
|
|
def __init__(self, loader=factory.load, curve=formats.CURVE_NIST256):
|
2016-02-19 08:07:33 +00:00
|
|
|
"""Connect to hardware device."""
|
2016-01-15 11:07:01 +00:00
|
|
|
client_wrapper = loader()
|
|
|
|
self.client = client_wrapper.connection
|
|
|
|
self.identity_type = client_wrapper.identity_type
|
|
|
|
self.device_name = client_wrapper.device_name
|
2016-03-06 19:59:17 +00:00
|
|
|
self.call_exception = client_wrapper.call_exception
|
2015-10-23 09:45:32 +00:00
|
|
|
self.curve = curve
|
2015-08-17 14:59:05 +00:00
|
|
|
|
|
|
|
def __enter__(self):
|
2016-02-19 08:07:33 +00:00
|
|
|
"""Start a session, and test connection."""
|
2015-08-18 18:30:58 +00:00
|
|
|
msg = 'Hello World!'
|
|
|
|
assert self.client.ping(msg) == msg
|
2015-08-17 14:59:05 +00:00
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, *args):
|
2016-02-19 08:07:33 +00:00
|
|
|
"""Forget PIN, shutdown screen and disconnect."""
|
2016-01-15 11:07:01 +00:00
|
|
|
log.info('disconnected from %s', self.device_name)
|
2016-02-19 08:07:33 +00:00
|
|
|
self.client.clear_session()
|
2015-08-17 14:59:05 +00:00
|
|
|
self.client.close()
|
|
|
|
|
2016-02-06 15:52:49 +00:00
|
|
|
def get_identity(self, label, index=0):
|
2016-02-19 08:07:33 +00:00
|
|
|
"""Parse label string into Identity protobuf."""
|
2016-01-15 11:07:01 +00:00
|
|
|
identity = string_to_identity(label, self.identity_type)
|
2015-08-19 14:40:16 +00:00
|
|
|
identity.proto = 'ssh'
|
2016-02-06 15:52:49 +00:00
|
|
|
identity.index = index
|
2015-08-17 14:59:05 +00:00
|
|
|
return identity
|
|
|
|
|
2015-10-23 09:45:32 +00:00
|
|
|
def get_public_key(self, label):
|
2016-02-19 08:07:33 +00:00
|
|
|
"""Get SSH public key corresponding to specified by label."""
|
2015-10-23 09:45:32 +00:00
|
|
|
identity = self.get_identity(label=label)
|
|
|
|
label = identity_to_string(identity) # canonize key label
|
2016-01-15 11:07:01 +00:00
|
|
|
log.info('getting "%s" public key (%s) from %s...',
|
|
|
|
label, self.curve, self.device_name)
|
2016-04-09 18:09:11 +00:00
|
|
|
addr = get_address(identity)
|
2015-08-18 14:03:28 +00:00
|
|
|
node = self.client.get_public_node(n=addr,
|
2015-10-23 09:45:32 +00:00
|
|
|
ecdsa_curve_name=self.curve)
|
2015-08-17 14:59:05 +00:00
|
|
|
|
|
|
|
pubkey = node.node.public_key
|
2015-12-18 14:03:50 +00:00
|
|
|
vk = formats.decompress_pubkey(pubkey=pubkey, curve_name=self.curve)
|
|
|
|
return formats.export_public_key(vk=vk, label=label)
|
2015-08-17 14:59:05 +00:00
|
|
|
|
2016-05-21 04:43:10 +00:00
|
|
|
def sign_ssh_challenge(self, label, blob):
|
2016-02-19 08:07:33 +00:00
|
|
|
"""Sign given blob using a private key, specified by the label."""
|
2015-10-23 09:45:32 +00:00
|
|
|
identity = self.get_identity(label=label)
|
2015-08-17 14:59:05 +00:00
|
|
|
msg = _parse_ssh_blob(blob)
|
2016-02-06 16:32:51 +00:00
|
|
|
log.debug('%s: user %r via %r (%r)',
|
|
|
|
msg['conn'], msg['user'], msg['auth'], msg['key_type'])
|
|
|
|
log.debug('nonce: %s', binascii.hexlify(msg['nonce']))
|
|
|
|
log.debug('fingerprint: %s', msg['public_key']['fingerprint'])
|
2016-02-27 18:09:03 +00:00
|
|
|
log.debug('hidden challenge size: %d bytes', len(blob))
|
2015-08-17 14:59:05 +00:00
|
|
|
|
2016-01-15 11:07:01 +00:00
|
|
|
log.info('please confirm user "%s" login to "%s" using %s...',
|
|
|
|
msg['user'], label, self.device_name)
|
2015-08-17 14:59:05 +00:00
|
|
|
|
2016-03-06 19:59:17 +00:00
|
|
|
try:
|
|
|
|
result = self.client.sign_identity(identity=identity,
|
|
|
|
challenge_hidden=blob,
|
2016-05-21 04:43:10 +00:00
|
|
|
challenge_visual='',
|
2016-03-06 19:59:17 +00:00
|
|
|
ecdsa_curve_name=self.curve)
|
|
|
|
except self.call_exception as e:
|
|
|
|
code, msg = e.args
|
|
|
|
log.warning('%s error #%s: %s', self.device_name, code, msg)
|
2016-03-12 18:42:14 +00:00
|
|
|
raise IOError(msg) # close current connection, keep server open
|
2015-10-23 09:45:32 +00:00
|
|
|
|
2015-12-18 14:03:50 +00:00
|
|
|
verifying_key = formats.decompress_pubkey(pubkey=result.public_key,
|
|
|
|
curve_name=self.curve)
|
2015-10-23 09:45:32 +00:00
|
|
|
key_type, blob = formats.serialize_verifying_key(verifying_key)
|
|
|
|
assert blob == msg['public_key']['blob']
|
|
|
|
assert key_type == msg['key_type']
|
2015-08-17 14:59:05 +00:00
|
|
|
assert len(result.signature) == 65
|
2015-08-18 14:54:36 +00:00
|
|
|
assert result.signature[:1] == bytearray([0])
|
2015-08-17 14:59:05 +00:00
|
|
|
|
2015-10-23 09:45:32 +00:00
|
|
|
return result.signature[1:]
|
2015-08-18 13:53:16 +00:00
|
|
|
|
|
|
|
|
2015-08-17 14:59:05 +00:00
|
|
|
_identity_regexp = re.compile(''.join([
|
|
|
|
'^'
|
|
|
|
r'(?:(?P<proto>.*)://)?',
|
|
|
|
r'(?:(?P<user>.*)@)?',
|
|
|
|
r'(?P<host>.*?)',
|
|
|
|
r'(?::(?P<port>\w*))?',
|
|
|
|
r'(?P<path>/.*)?',
|
|
|
|
'$'
|
|
|
|
]))
|
|
|
|
|
|
|
|
|
2015-08-18 15:52:29 +00:00
|
|
|
def string_to_identity(s, identity_type):
|
2016-02-19 08:07:33 +00:00
|
|
|
"""Parse string into Identity protobuf."""
|
2015-08-17 14:59:05 +00:00
|
|
|
m = _identity_regexp.match(s)
|
|
|
|
result = m.groupdict()
|
|
|
|
log.debug('parsed identity: %s', result)
|
|
|
|
kwargs = {k: v for k, v in result.items() if v}
|
|
|
|
return identity_type(**kwargs)
|
|
|
|
|
|
|
|
|
2015-08-18 15:52:29 +00:00
|
|
|
def identity_to_string(identity):
|
2016-02-19 08:07:33 +00:00
|
|
|
"""Dump Identity protobuf into its string representation."""
|
2015-08-17 14:59:05 +00:00
|
|
|
result = []
|
|
|
|
if identity.proto:
|
|
|
|
result.append(identity.proto + '://')
|
|
|
|
if identity.user:
|
|
|
|
result.append(identity.user + '@')
|
|
|
|
result.append(identity.host)
|
|
|
|
if identity.port:
|
|
|
|
result.append(':' + identity.port)
|
|
|
|
if identity.path:
|
|
|
|
result.append(identity.path)
|
|
|
|
return ''.join(result)
|
|
|
|
|
|
|
|
|
2016-06-17 06:29:05 +00:00
|
|
|
def get_address(identity, ecdh=False):
|
|
|
|
"""Compute BIP32 derivation address according to SLIP-0013/0017."""
|
2015-08-17 14:59:05 +00:00
|
|
|
index = struct.pack('<L', identity.index)
|
2015-08-18 15:52:29 +00:00
|
|
|
addr = index + identity_to_string(identity).encode('ascii')
|
2015-08-17 14:59:05 +00:00
|
|
|
log.debug('address string: %r', addr)
|
|
|
|
digest = formats.hashfunc(addr).digest()
|
|
|
|
s = io.BytesIO(bytearray(digest))
|
|
|
|
|
|
|
|
hardened = 0x80000000
|
2016-06-17 06:29:05 +00:00
|
|
|
addr_0 = [13, 17][bool(ecdh)]
|
|
|
|
address_n = [addr_0] + list(util.recv(s, '<LLLL'))
|
2015-08-17 14:59:05 +00:00
|
|
|
return [(hardened | value) for value in address_n]
|
|
|
|
|
|
|
|
|
|
|
|
def _parse_ssh_blob(data):
|
|
|
|
res = {}
|
2016-02-06 16:27:46 +00:00
|
|
|
i = io.BytesIO(data)
|
|
|
|
res['nonce'] = util.read_frame(i)
|
2016-02-13 13:25:18 +00:00
|
|
|
i.read(1) # SSH2_MSG_USERAUTH_REQUEST == 50 (from ssh2.h, line 108)
|
2016-02-06 16:27:46 +00:00
|
|
|
res['user'] = util.read_frame(i)
|
|
|
|
res['conn'] = util.read_frame(i)
|
|
|
|
res['auth'] = util.read_frame(i)
|
2016-02-13 13:25:18 +00:00
|
|
|
i.read(1) # have_sig == 1 (from sshconnect2.c, line 1056)
|
2016-02-06 16:27:46 +00:00
|
|
|
res['key_type'] = util.read_frame(i)
|
|
|
|
public_key = util.read_frame(i)
|
|
|
|
res['public_key'] = formats.parse_pubkey(public_key)
|
|
|
|
assert not i.read()
|
2015-08-17 14:59:05 +00:00
|
|
|
return res
|