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.
|
|
|
|
"""
|
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
|
|
|
|
2016-10-28 07:53:53 +00:00
|
|
|
from . import 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-10-28 07:53:53 +00:00
|
|
|
def __init__(self, device):
|
2016-02-19 08:07:33 +00:00
|
|
|
"""Connect to hardware device."""
|
2016-10-28 07:53:53 +00:00
|
|
|
self.device = device
|
2015-08-17 14:59:05 +00:00
|
|
|
|
2016-11-03 20:00:43 +00:00
|
|
|
def get_public_key(self, identity):
|
2016-10-28 07:53:53 +00:00
|
|
|
"""Get SSH public key from the device."""
|
|
|
|
with self.device:
|
2016-11-03 20:00:43 +00:00
|
|
|
pubkey = self.device.pubkey(identity)
|
2015-08-17 14:59:05 +00:00
|
|
|
|
2016-10-28 07:53:53 +00:00
|
|
|
vk = formats.decompress_pubkey(pubkey=pubkey,
|
2016-11-03 20:00:43 +00:00
|
|
|
curve_name=identity.curve_name)
|
2016-10-28 07:53:53 +00:00
|
|
|
return formats.export_public_key(vk=vk,
|
2016-11-03 20:00:43 +00:00
|
|
|
label=str(identity))
|
2015-08-17 14:59:05 +00:00
|
|
|
|
2016-11-03 20:00:43 +00:00
|
|
|
def sign_ssh_challenge(self, blob, identity):
|
2016-10-28 07:53:53 +00:00
|
|
|
"""Sign given blob using a private key on the device."""
|
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'])
|
2016-10-28 07:53:53 +00:00
|
|
|
log.debug('nonce: %r', msg['nonce'])
|
2016-11-03 20:00:43 +00:00
|
|
|
fp = msg['public_key']['fingerprint']
|
|
|
|
log.debug('fingerprint: %s', fp)
|
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...',
|
2016-11-03 20:00:43 +00:00
|
|
|
msg['user'].decode('ascii'), identity,
|
2016-10-28 07:53:53 +00:00
|
|
|
self.device)
|
2015-08-17 14:59:05 +00:00
|
|
|
|
2016-10-28 07:53:53 +00:00
|
|
|
with self.device:
|
2016-11-03 20:00:43 +00:00
|
|
|
return self.device.sign(blob=blob, identity=identity)
|
2015-08-18 13:53:16 +00:00
|
|
|
|
|
|
|
|
2015-08-17 14:59:05 +00:00
|
|
|
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
|