1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
# Copyright (c) 2015-2024 by Ron Frederick <ronf@timeheart.net> and others.
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License v2.0 which accompanies this
# distribution and is available at:
#
# http://www.eclipse.org/legal/epl-2.0/
#
# This program may also be made available under the following secondary
# licenses when the conditions for such availability set forth in the
# Eclipse Public License v2.0 are satisfied:
#
# GNU General Public License, Version 2.0, or any later versions of
# that license
#
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
#
# Contributors:
# Ron Frederick - initial implementation, API, and documentation
"""Unit tests for matching against known_hosts file"""
import binascii
import hashlib
import hmac
import os
import asyncssh
from .util import TempDirTestCase, get_test_key, x509_available
if x509_available: # pragma: no branch
from asyncssh.crypto import X509NamePattern
def _hash(host):
"""Return a hashed version of a hostname in a known_hosts file"""
salt = os.urandom(20)
hosthash = hmac.new(salt, host.encode(), hashlib.sha1).digest()
entry = b'|'.join((b'', b'1', binascii.b2a_base64(salt)[:-1],
binascii.b2a_base64(hosthash)[:-1]))
return entry.decode()
class _TestKnownHosts(TempDirTestCase):
"""Unit tests for known_hosts module"""
keylists = ([], [], [], [], [], [], [])
imported_keylists = ([], [], [], [], [], [], [])
@classmethod
def setUpClass(cls):
"""Create public keys needed for test"""
super().setUpClass()
for keylist, imported_keylist in zip(cls.keylists[:3],
cls.imported_keylists[:3]):
for i in range(3):
key = get_test_key('ssh-rsa', i)
keylist.append(key.export_public_key().decode('ascii'))
imported_keylist.append(key.convert_to_public())
if x509_available: # pragma: no branch
for keylist, imported_keylist in zip(cls.keylists[3:5],
cls.imported_keylists[3:5]):
for i in range(3, 5):
key = get_test_key('ssh-rsa', i)
cert = key.generate_x509_user_certificate(key, 'OU=user',
'OU=user')
keylist.append(
cert.export_certificate('openssh').decode('ascii'))
imported_keylist.append(cert)
for keylist, imported_keylist in zip(cls.keylists[5:],
cls.imported_keylists[5:]):
for name in ('OU=user', 'OU=revoked'):
keylist.append('x509v3-ssh-rsa subject=' + name + '\n')
imported_keylist.append(X509NamePattern(name))
def check_match(self, known_hosts, results=None, host='host',
addr='1.2.3.4', port=22):
"""Check the result of calling match_known_hosts"""
if results:
results = tuple([kl[r] for r in result]
for kl, result in zip(self.imported_keylists,
results))
matches = asyncssh.match_known_hosts(known_hosts, host, addr, port)
self.assertEqual(matches, results)
def check_hosts(self, patlists, results=None, host='host', addr='1.2.3.4',
port=22, from_file=False, from_bytes=False,
as_callable=False, as_tuple=False):
"""Check a known_hosts file built from the specified patterns"""
def call_match(host, addr, port):
"""Test passing callable as known_hosts"""
return asyncssh.match_known_hosts(_known_hosts, host, addr, port)
prefixes = ('', '@cert-authority ', '@revoked ',
'', '@revoked ', '', '@revoked ')
known_hosts = '# Comment line\n # Comment line with whitespace\n\n'
for prefix, patlist, keys in zip(prefixes, patlists, self.keylists):
for pattern, key in zip(patlist, keys):
known_hosts += f'{prefix}{pattern} {key}'
if from_file:
with open('known_hosts', 'w') as f:
f.write(known_hosts)
known_hosts = 'known_hosts'
elif from_bytes:
known_hosts = known_hosts.encode()
elif as_callable:
_known_hosts = asyncssh.import_known_hosts(known_hosts)
known_hosts = call_match
elif as_tuple:
known_hosts = asyncssh.import_known_hosts(known_hosts)
known_hosts = asyncssh.match_known_hosts(known_hosts, host,
addr, port)
else:
known_hosts = asyncssh.import_known_hosts(known_hosts)
return self.check_match(known_hosts, results, host, addr, port)
def test_match(self):
"""Test known host matching"""
matches = (
('Empty file', ([], [], [], [], [], [], []),
([], [], [], [], [], [], [])),
('Exact host and port', (['[host]:22'], [], [], [], [], [], []),
([0], [], [], [], [], [], [])),
('Exact host', (['host'], [], [], [], [], [], []),
([0], [], [], [], [], [], [])),
('Exact host CA', ([], ['host'], [], [], [], [], []),
([], [0], [], [], [], [], [])),
('Exact host revoked', ([], [], ['host'], [], [], [], []),
([], [], [0], [], [], [], [])),
('Multiple exact', (['host'], ['host'], [], [], [], [], []),
([0], [0], [], [], [], [], [])),
('Wildcard host', (['hos*'], [], [], [], [], [], []),
([0], [], [], [], [], [], [])),
('Mismatched port', (['[host]:23'], [], [], [], [], [], []),
([], [], [], [], [], [], [])),
('Negative host', (['hos*,!host'], [], [], [], [], [], []),
([], [], [], [], [], [], [])),
('Exact addr and port', (['[1.2.3.4]:22'], [], [], [], [], [], []),
([0], [], [], [], [], [], [])),
('Exact addr', (['1.2.3.4'], [], [], [], [], [], []),
([0], [], [], [], [], [], [])),
('Subnet', (['1.2.3.0/24'], [], [], [], [], [], []),
([0], [], [], [], [], [], [])),
('Negative addr', (['1.2.3.0/24,!1.2.3.4', [], [], []], [], [], []),
([], [], [], [], [], [], [])),
('Hashed host', ([_hash('host')], [], [], [], [], [], []),
([0], [], [], [], [], [], [])),
('Hashed addr', ([_hash('1.2.3.4')], [], [], [], [], [], []),
([0], [], [], [], [], [], []))
)
if x509_available: # pragma: no branch
matches += (
('Exact host X.509', ([], [], [], ['host'], [], [], []),
([], [], [], [0], [], [], [])),
('Exact host X.509 revoked', ([], [], [], [], ['host'], [], []),
([], [], [], [], [0], [], [])),
('Exact host subject', ([], [], [], [], [], ['host'], []),
([], [], [], [], [], [0], [])),
('Exact host revoked subject',
([], [], [], [], [], [], ['host']),
([], [], [], [], [], [], [0])),
)
for testname, patlists, result in matches:
with self.subTest(testname):
self.check_hosts(patlists, result)
def test_no_addr(self):
"""Test match without providing addr"""
self.check_hosts((['host'], [], [], [], [], [], []),
([0], [], [], [], [], [], []), addr='')
self.check_hosts((['1.2.3.4'], [], [], [], [], [], []),
([], [], [], [], [], [], []), addr='')
def test_no_port(self):
"""Test match without providing port"""
self.check_hosts((['host'], [], [], [], [], [], []),
([0], [], [], [], [], [], []), port=None)
self.check_hosts((['[host]:22'], [], [], [], [], [], []),
([], [], [], [], [], [], []), port=None)
def test_no_match(self):
"""Test for cases where no match is found"""
no_match = (([], [], [], [], [], [], []),
(['host1', 'host2'], [], [], [], [], [], []),
(['2.3.4.5', '3.4.5.6'], [], [], [], [], [], []),
(['[host]:2222', '[host]:22222'], [], [], [], [], [], []))
for patlists in no_match:
self.check_hosts(patlists, ([], [], [], [], [], [], []))
def test_scoped_addr(self):
"""Test match on scoped addresses"""
self.check_hosts((['fe80::1%1'], [], [], [], [], [], []),
([0], [], [], [], [], [], []), addr='fe80::1%1')
self.check_hosts((['fe80::%1/64'], [], [], [], [], [], []),
([0], [], [], [], [], [], []), addr='fe80::1%1')
self.check_hosts((['fe80::1%2'], [], [], [], [], [], []),
([], [], [], [], [], [], []), addr='fe80::1%1')
self.check_hosts((['2001:2::%3/64'], [], [], [], [], [], []),
([0], [], [], [], [], [], []), addr='2001:2::1')
def test_missing_key(self):
"""Test for line with missing key data"""
with self.assertRaises(ValueError):
self.check_match(b'xxx\n')
def test_missing_key_with_tag(self):
"""Test for line with tag with missing key data"""
with self.assertRaises(ValueError):
self.check_match(b'@cert-authority xxx\n')
def test_invalid_key(self):
"""Test for line with invalid key"""
self.check_match(b'xxx yyy\n', ([], [], [], [], [], [], []))
def test_invalid_marker(self):
"""Test for line with invalid marker"""
with self.assertRaises(ValueError):
self.check_match(b'@xxx yyy zzz\n')
def test_incomplete_hash(self):
"""Test for line with incomplete host hash"""
with self.assertRaises(ValueError):
self.check_hosts((['|1|aaaa'], [], [], [], [], [], [], [], []))
def test_invalid_hash(self):
"""Test for line with invalid host hash"""
with self.assertRaises(ValueError):
self.check_hosts((['|1|aaa'], [], [], [], [], [], [], [], []))
def test_unknown_hash_type(self):
"""Test for line with unknown host hash type"""
with self.assertRaises(ValueError):
self.check_hosts((['|2|aaaa|'], [], [], [], [], [], [], [], []))
def test_file(self):
"""Test match against file"""
self.check_hosts((['host'], [], [], [], [], [], []),
([0], [], [], [], [], [], []), from_file=True)
def test_bytes(self):
"""Test match against byte string"""
self.check_hosts((['host'], [], [], [], [], [], []),
([0], [], [], [], [], [], []), from_bytes=True)
def test_callable(self):
"""Test match using callable"""
self.check_hosts((['host'], [], [], [], [], [], []),
([0], [], [], [], [], [], []), as_callable=True)
def test_tuple(self):
"""Test passing already constructed tuple of keys"""
self.check_hosts((['host'], [], [], [], [], [], []),
([0], [], [], [], [], [], []), as_tuple=True)
|