Verify the public keys we're sending and handle parse failure

This commit is contained in:
Juhani Krekelä 2020-01-05 18:58:57 +02:00
parent fe4ea1ab24
commit 2d6298de90
1 changed files with 24 additions and 2 deletions

View File

@ -186,6 +186,10 @@ def parse_pubkey(pubkey):
pubkey = pubkey[:-1]
fields = pubkey.split(b' ')
# There should be no newlines after this
if b'\n' in pubkey:
raise PubkeyParseError
# algorithm keymaterial [comment]
if len(fields) < 2:
raise PubkeyParseError
@ -305,11 +309,20 @@ def main():
except IOError as err:
error('Could not read server public key: %s' % err)
try:
parse_pubkey(server_pubkey)
except PubkeyParseError:
error('Public key is in an unrecognized format')
client_pubkey = server(server_pubkey, port)
verify(client_pubkey, server_pubkey)
algorithm, keymaterial, comment = parse_pubkey(client_pubkey)
try:
algorithm, keymaterial, comment = parse_pubkey(client_pubkey)
except PubkeyParseError:
error('Parse error on client\'s pubkey')
authorized_keys_entry = serialize_authorized_keys(algorithm, keymaterial, comment)
if output_file is None:
@ -355,6 +368,11 @@ def main():
except IOError as err:
error('Could not read client public key: %s' % err)
try:
parse_pubkey(client_pubkey)
except PubkeyParseError:
error('Public key is in an unrecognized format')
host, = fixed
# Support internationalized domain names
host = host.encode('idna').decode()
@ -363,7 +381,11 @@ def main():
verify(client_pubkey, server_pubkey)
algorithm, keymaterial, comment = parse_pubkey(server_pubkey)
try:
algorithm, keymaterial, comment = parse_pubkey(server_pubkey)
except PubkeyParseError:
error('Parse error on server\'s pubkey')
known_hosts_entry = serialize_known_hosts(host.encode(), ssh_port, algorithm, keymaterial)
if output_file is None: