formats: refactor parsing code

This commit is contained in:
Roman Zeyde
2015-07-20 18:28:00 +03:00
parent b7cc276259
commit 86b166ba08
2 changed files with 12 additions and 11 deletions

View File

@@ -51,16 +51,6 @@ def parse_pubkey(blob):
return result
def parse_public_key(data):
file_type, base64blob, name = data.split()
blob = base64.b64decode(base64blob)
result = parse_pubkey(blob)
result['name'] = name.encode('ascii')
assert result['type'] == file_type.encode('ascii')
log.debug('loaded %s %s', file_type, result['fingerprint'])
return result
def decompress_pubkey(pub):
P = curve.curve.p()
A = curve.curve.a()
@@ -81,3 +71,14 @@ def export_public_key(pubkey, label):
log.debug('fingerprint: %s', fingerprint(blob))
b64 = base64.b64encode(blob)
return '{} {} {}\n'.format(ECDSA_KEY_TYPE, b64, label)
def import_public_key(line):
''' Parse public key textual format, as saved at .pub file '''
file_type, base64blob, name = line.split()
blob = base64.b64decode(base64blob)
result = parse_pubkey(blob)
result['name'] = name.encode('ascii')
assert result['type'] == file_type.encode('ascii')
log.debug('loaded %s %s', file_type, result['fingerprint'])
return result

View File

@@ -72,7 +72,7 @@ def serve(public_keys, signer, sock_path=None):
if sock_path is None:
sock_path = tempfile.mktemp(prefix='ssh-agent-')
keys = [formats.parse_public_key(k) for k in public_keys]
keys = [formats.import_public_key(k) for k in public_keys]
environ = {'SSH_AUTH_SOCK': sock_path, 'SSH_AGENT_PID': str(os.getpid())}
with unix_domain_socket_server(sock_path) as server:
handler = protocol.Handler(keys=keys, signer=signer)