2016-02-19 08:48:36 +00:00
|
|
|
"""
|
|
|
|
SSH-agent protocol implementation library.
|
|
|
|
|
|
|
|
See https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.agent and
|
|
|
|
http://ptspts.blogspot.co.il/2010/06/how-to-use-ssh-agent-programmatically.html
|
|
|
|
for more details.
|
|
|
|
The server's source code can be found here:
|
|
|
|
https://github.com/openssh/openssh-portable/blob/master/authfd.c
|
|
|
|
"""
|
2016-01-09 14:06:47 +00:00
|
|
|
import io
|
2016-01-04 17:17:08 +00:00
|
|
|
import logging
|
2015-06-15 15:13:10 +00:00
|
|
|
|
2016-01-09 14:06:47 +00:00
|
|
|
from . import formats, util
|
2015-06-06 14:52:10 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-03-12 18:38:37 +00:00
|
|
|
# Taken from https://github.com/openssh/openssh-portable/blob/master/authfd.h
|
|
|
|
COMMANDS = dict(
|
|
|
|
SSH_AGENTC_REQUEST_RSA_IDENTITIES=1,
|
|
|
|
SSH_AGENT_RSA_IDENTITIES_ANSWER=2,
|
|
|
|
SSH_AGENTC_RSA_CHALLENGE=3,
|
|
|
|
SSH_AGENT_RSA_RESPONSE=4,
|
|
|
|
SSH_AGENT_FAILURE=5,
|
|
|
|
SSH_AGENT_SUCCESS=6,
|
|
|
|
SSH_AGENTC_ADD_RSA_IDENTITY=7,
|
|
|
|
SSH_AGENTC_REMOVE_RSA_IDENTITY=8,
|
|
|
|
SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES=9,
|
|
|
|
SSH2_AGENTC_REQUEST_IDENTITIES=11,
|
|
|
|
SSH2_AGENT_IDENTITIES_ANSWER=12,
|
|
|
|
SSH2_AGENTC_SIGN_REQUEST=13,
|
|
|
|
SSH2_AGENT_SIGN_RESPONSE=14,
|
|
|
|
SSH2_AGENTC_ADD_IDENTITY=17,
|
|
|
|
SSH2_AGENTC_REMOVE_IDENTITY=18,
|
|
|
|
SSH2_AGENTC_REMOVE_ALL_IDENTITIES=19,
|
|
|
|
SSH_AGENTC_ADD_SMARTCARD_KEY=20,
|
|
|
|
SSH_AGENTC_REMOVE_SMARTCARD_KEY=21,
|
|
|
|
SSH_AGENTC_LOCK=22,
|
|
|
|
SSH_AGENTC_UNLOCK=23,
|
|
|
|
SSH_AGENTC_ADD_RSA_ID_CONSTRAINED=24,
|
|
|
|
SSH2_AGENTC_ADD_ID_CONSTRAINED=25,
|
|
|
|
SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED=26,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def msg_code(name):
|
|
|
|
"""Convert string name into a integer message code."""
|
|
|
|
return COMMANDS[name]
|
|
|
|
|
|
|
|
|
|
|
|
def msg_name(code):
|
|
|
|
"""Convert integer message code into a string name."""
|
|
|
|
ids = {v: k for k, v in COMMANDS.items()}
|
|
|
|
return ids[code]
|
|
|
|
|
|
|
|
|
2016-03-12 19:06:57 +00:00
|
|
|
def failure():
|
|
|
|
"""Return error code to SSH binary."""
|
2016-03-12 18:38:37 +00:00
|
|
|
error_msg = util.pack('B', msg_code('SSH_AGENT_FAILURE'))
|
|
|
|
return util.frame(error_msg)
|
|
|
|
|
|
|
|
|
|
|
|
def _legacy_pubs(buf):
|
|
|
|
"""SSH v1 public keys are not supported."""
|
|
|
|
assert not buf.read()
|
|
|
|
code = util.pack('B', msg_code('SSH_AGENT_RSA_IDENTITIES_ANSWER'))
|
|
|
|
num = util.pack('L', 0) # no SSH v1 keys
|
|
|
|
return util.frame(code, num)
|
2015-06-06 14:52:10 +00:00
|
|
|
|
|
|
|
|
2015-06-16 07:34:02 +00:00
|
|
|
class Handler(object):
|
2016-02-19 08:48:36 +00:00
|
|
|
"""ssh-agent protocol handler."""
|
2015-06-16 07:34:02 +00:00
|
|
|
|
2016-01-26 19:14:27 +00:00
|
|
|
def __init__(self, keys, signer, debug=False):
|
2016-02-19 08:48:36 +00:00
|
|
|
"""
|
|
|
|
Create a protocol handler with specified public keys.
|
|
|
|
|
|
|
|
Use specified signer function to sign SSH authentication requests.
|
|
|
|
"""
|
2015-06-16 07:34:02 +00:00
|
|
|
self.public_keys = keys
|
|
|
|
self.signer = signer
|
2016-01-26 19:14:27 +00:00
|
|
|
self.debug = debug
|
2015-06-16 07:34:02 +00:00
|
|
|
|
|
|
|
self.methods = {
|
2016-03-12 18:38:37 +00:00
|
|
|
msg_code('SSH_AGENTC_REQUEST_RSA_IDENTITIES'): _legacy_pubs,
|
|
|
|
msg_code('SSH2_AGENTC_REQUEST_IDENTITIES'): self.list_pubs,
|
|
|
|
msg_code('SSH2_AGENTC_SIGN_REQUEST'): self.sign_message,
|
2015-06-16 07:34:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def handle(self, msg):
|
2016-02-19 08:48:36 +00:00
|
|
|
"""Handle SSH message from the SSH client and return the response."""
|
2016-01-26 19:14:27 +00:00
|
|
|
debug_msg = ': {!r}'.format(msg) if self.debug else ''
|
|
|
|
log.debug('request: %d bytes%s', len(msg), debug_msg)
|
2015-06-16 07:34:02 +00:00
|
|
|
buf = io.BytesIO(msg)
|
|
|
|
code, = util.recv(buf, '>B')
|
2016-03-12 18:38:37 +00:00
|
|
|
if code not in self.methods:
|
|
|
|
log.warning('Unsupported command: %s (%d)', msg_name(code), code)
|
2016-03-12 19:06:57 +00:00
|
|
|
return failure()
|
2016-03-12 18:38:37 +00:00
|
|
|
|
2015-06-16 07:34:02 +00:00
|
|
|
method = self.methods[code]
|
|
|
|
log.debug('calling %s()', method.__name__)
|
|
|
|
reply = method(buf=buf)
|
2016-01-26 19:14:27 +00:00
|
|
|
debug_reply = ': {!r}'.format(reply) if self.debug else ''
|
|
|
|
log.debug('reply: %d bytes%s', len(reply), debug_reply)
|
2015-06-16 07:34:02 +00:00
|
|
|
return reply
|
|
|
|
|
|
|
|
def list_pubs(self, buf):
|
2016-02-19 08:48:36 +00:00
|
|
|
"""SSH v2 public keys are serialized and returned."""
|
2015-06-16 07:34:02 +00:00
|
|
|
assert not buf.read()
|
|
|
|
keys = self.public_keys
|
2016-03-12 18:38:37 +00:00
|
|
|
code = util.pack('B', msg_code('SSH2_AGENT_IDENTITIES_ANSWER'))
|
2015-06-16 07:34:02 +00:00
|
|
|
num = util.pack('L', len(keys))
|
|
|
|
log.debug('available keys: %s', [k['name'] for k in keys])
|
|
|
|
for i, k in enumerate(keys):
|
|
|
|
log.debug('%2d) %s', i+1, k['fingerprint'])
|
|
|
|
pubs = [util.frame(k['blob']) + util.frame(k['name']) for k in keys]
|
|
|
|
return util.frame(code, num, *pubs)
|
|
|
|
|
|
|
|
def sign_message(self, buf):
|
2016-02-19 08:48:36 +00:00
|
|
|
"""
|
|
|
|
SSH v2 public key authentication is performed.
|
|
|
|
|
|
|
|
If the required key is not supported, raise KeyError
|
2016-10-11 18:29:54 +00:00
|
|
|
If the signature is invalid, raise ValueError
|
2016-02-19 08:48:36 +00:00
|
|
|
"""
|
2015-06-16 07:34:02 +00:00
|
|
|
key = formats.parse_pubkey(util.read_frame(buf))
|
|
|
|
log.debug('looking for %s', key['fingerprint'])
|
|
|
|
blob = util.read_frame(buf)
|
2015-07-03 13:35:21 +00:00
|
|
|
assert util.read_frame(buf) == b''
|
2015-07-04 18:15:49 +00:00
|
|
|
assert not buf.read()
|
2015-06-16 07:34:02 +00:00
|
|
|
|
|
|
|
for k in self.public_keys:
|
|
|
|
if (k['fingerprint']) == (key['fingerprint']):
|
|
|
|
log.debug('using key %r (%s)', k['name'], k['fingerprint'])
|
|
|
|
key = k
|
|
|
|
break
|
|
|
|
else:
|
2016-02-19 08:48:36 +00:00
|
|
|
raise KeyError('key not found')
|
2015-06-16 07:34:02 +00:00
|
|
|
|
2016-01-04 17:17:08 +00:00
|
|
|
label = key['name'].decode('ascii') # label should be a string
|
2016-10-28 07:53:53 +00:00
|
|
|
log.debug('signing %d-byte blob with "%s" key', len(blob), label)
|
2016-03-12 18:42:14 +00:00
|
|
|
try:
|
2016-11-03 20:00:43 +00:00
|
|
|
signature = self.signer(blob=blob, identity=key['identity'])
|
2016-03-12 18:42:14 +00:00
|
|
|
except IOError:
|
2016-03-12 19:06:57 +00:00
|
|
|
return failure()
|
2016-10-28 07:53:53 +00:00
|
|
|
log.debug('signature: %r', signature)
|
2015-06-16 07:34:02 +00:00
|
|
|
|
2015-08-13 14:49:22 +00:00
|
|
|
try:
|
2015-10-23 09:45:32 +00:00
|
|
|
sig_bytes = key['verifier'](sig=signature, msg=blob)
|
2015-08-13 14:49:22 +00:00
|
|
|
log.info('signature status: OK')
|
|
|
|
except formats.ecdsa.BadSignatureError:
|
|
|
|
log.exception('signature status: ERROR')
|
2016-02-19 08:48:36 +00:00
|
|
|
raise ValueError('invalid ECDSA signature')
|
2015-06-16 07:34:02 +00:00
|
|
|
|
|
|
|
log.debug('signature size: %d bytes', len(sig_bytes))
|
|
|
|
|
|
|
|
data = util.frame(util.frame(key['type']), util.frame(sig_bytes))
|
2016-03-12 18:38:37 +00:00
|
|
|
code = util.pack('B', msg_code('SSH2_AGENT_SIGN_RESPONSE'))
|
2015-06-16 07:34:02 +00:00
|
|
|
return util.frame(code, data)
|