|
1 | 1 | /* |
2 | | - * Copyright 2022 the original author or authors. |
| 2 | + * Copyright 2022-2023 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
@@ -66,18 +66,19 @@ public ResourceKnownHostsServerKeyVerifier(Resource knownHostsResource) { |
66 | 66 | @Override |
67 | 67 | public boolean verifyServerKey(ClientSession clientSession, SocketAddress remoteAddress, PublicKey serverKey) { |
68 | 68 | Collection<KnownHostsServerKeyVerifier.HostEntryPair> knownHosts = this.keysSupplier.get(); |
69 | | - KnownHostsServerKeyVerifier.HostEntryPair match = findKnownHostEntry(clientSession, remoteAddress, knownHosts); |
70 | | - if (match == null) { |
| 69 | + List<KnownHostsServerKeyVerifier.HostEntryPair> matches = |
| 70 | + findKnownHostEntries(clientSession, remoteAddress, knownHosts); |
| 71 | + |
| 72 | + if (matches.isEmpty()) { |
71 | 73 | return false; |
72 | 74 | } |
73 | 75 |
|
74 | | - KnownHostEntry entry = match.getHostEntry(); |
75 | | - PublicKey expected = match.getServerKey(); |
76 | | - if (KeyUtils.compareKeys(expected, serverKey)) { |
77 | | - return !"revoked".equals(entry.getMarker()); |
78 | | - } |
| 76 | + String serverKeyType = KeyUtils.getKeyType(serverKey); |
79 | 77 |
|
80 | | - return false; |
| 78 | + return matches.stream() |
| 79 | + .filter(match -> serverKeyType.equals(match.getHostEntry().getKeyEntry().getKeyType())) |
| 80 | + .filter(match -> KeyUtils.compareKeys(match.getServerKey(), serverKey)) |
| 81 | + .anyMatch(match -> !"revoked".equals(match.getHostEntry().getMarker())); |
81 | 82 | } |
82 | 83 |
|
83 | 84 | private static Supplier<Collection<KnownHostsServerKeyVerifier.HostEntryPair>> getKnownHostSupplier( |
@@ -106,26 +107,32 @@ private static PublicKey resolveHostKey(KnownHostEntry entry) throws IOException |
106 | 107 | return authEntry.resolvePublicKey(null, PublicKeyEntryResolver.IGNORING); |
107 | 108 | } |
108 | 109 |
|
109 | | - private static KnownHostsServerKeyVerifier.HostEntryPair findKnownHostEntry( |
| 110 | + private static List<KnownHostsServerKeyVerifier.HostEntryPair> findKnownHostEntries( |
110 | 111 | ClientSession clientSession, SocketAddress remoteAddress, |
111 | 112 | Collection<KnownHostsServerKeyVerifier.HostEntryPair> knownHosts) { |
112 | 113 |
|
| 114 | + if (GenericUtils.isEmpty(knownHosts)) { |
| 115 | + return Collections.emptyList(); |
| 116 | + } |
| 117 | + |
113 | 118 | Collection<SshdSocketAddress> candidates = resolveHostNetworkIdentities(clientSession, remoteAddress); |
114 | 119 |
|
115 | 120 | if (GenericUtils.isEmpty(candidates)) { |
116 | | - return null; |
| 121 | + return Collections.emptyList(); |
117 | 122 | } |
118 | 123 |
|
| 124 | + List<KnownHostsServerKeyVerifier.HostEntryPair> matches = new ArrayList<>(); |
119 | 125 | for (KnownHostsServerKeyVerifier.HostEntryPair match : knownHosts) { |
120 | 126 | KnownHostEntry entry = match.getHostEntry(); |
121 | 127 | for (SshdSocketAddress host : candidates) { |
122 | 128 | if (entry.isHostMatch(host.getHostName(), host.getPort())) { |
123 | | - return match; |
| 129 | + matches.add(match); |
| 130 | + break; |
124 | 131 | } |
125 | 132 | } |
126 | 133 | } |
127 | 134 |
|
128 | | - return null; // no match found |
| 135 | + return matches; |
129 | 136 | } |
130 | 137 |
|
131 | 138 | private static Collection<SshdSocketAddress> resolveHostNetworkIdentities( |
|
0 commit comments