-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement OpenSSH strict key exchange extension (#917)
- Loading branch information
Showing
7 changed files
with
180 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/itest/java/com/hierynomus/sshj/transport/kex/StrictKeyExchangeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright (C)2009 - SSHJ Contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.hierynomus.sshj.transport.kex; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import ch.qos.logback.classic.Logger; | ||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import ch.qos.logback.core.read.ListAppender; | ||
import com.hierynomus.sshj.SshdContainer; | ||
import net.schmizz.sshj.SSHClient; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.LoggerFactory; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
@Testcontainers | ||
class StrictKeyExchangeTest { | ||
|
||
@Container | ||
private static final SshdContainer sshd = new SshdContainer(); | ||
|
||
private final List<Logger> watchedLoggers = new ArrayList<>(); | ||
private final ListAppender<ILoggingEvent> logWatcher = new ListAppender<>(); | ||
|
||
@BeforeEach | ||
void setUpLogWatcher() { | ||
logWatcher.start(); | ||
setUpLogger("net.schmizz.sshj.transport.Decoder"); | ||
setUpLogger("net.schmizz.sshj.transport.Encoder"); | ||
setUpLogger("net.schmizz.sshj.transport.KeyExchanger"); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
watchedLoggers.forEach(Logger::detachAndStopAllAppenders); | ||
} | ||
|
||
private void setUpLogger(String className) { | ||
Logger logger = ((Logger) LoggerFactory.getLogger(className)); | ||
logger.addAppender(logWatcher); | ||
watchedLoggers.add(logger); | ||
} | ||
|
||
@Test | ||
void strictKeyExchange() throws Throwable { | ||
try (SSHClient client = sshd.getConnectedClient()) { | ||
client.authPublickey("sshj", "src/itest/resources/keyfiles/id_rsa_opensshv1"); | ||
assertTrue(client.isAuthenticated()); | ||
} | ||
List<String> keyExchangerLogs = getLogs("KeyExchanger"); | ||
assertThat(keyExchangerLogs).containsSequence( | ||
"Initiating key exchange", | ||
"Sending SSH_MSG_KEXINIT", | ||
"Received SSH_MSG_KEXINIT", | ||
"Enabling strict key exchange extension" | ||
); | ||
List<String> decoderLogs = getLogs("Decoder").stream() | ||
.map(log -> log.split(":")[0]) | ||
.collect(Collectors.toList()); | ||
assertThat(decoderLogs).containsExactly( | ||
"Received packet #0", | ||
"Received packet #1", | ||
"Received packet #2", | ||
"Received packet #0", | ||
"Received packet #1", | ||
"Received packet #2", | ||
"Received packet #3" | ||
); | ||
List<String> encoderLogs = getLogs("Encoder").stream() | ||
.map(log -> log.split(":")[0]) | ||
.collect(Collectors.toList()); | ||
assertThat(encoderLogs).containsExactly( | ||
"Encoding packet #0", | ||
"Encoding packet #1", | ||
"Encoding packet #2", | ||
"Encoding packet #0", | ||
"Encoding packet #1", | ||
"Encoding packet #2", | ||
"Encoding packet #3" | ||
); | ||
} | ||
|
||
private List<String> getLogs(String className) { | ||
return logWatcher.list.stream() | ||
.filter(event -> event.getLoggerName().endsWith(className)) | ||
.map(ILoggingEvent::getFormattedMessage) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,8 +37,11 @@ class Proposal { | |
private final List<String> s2cComp; | ||
private final SSHPacket packet; | ||
|
||
public Proposal(Config config, List<String> knownHostAlgs) { | ||
public Proposal(Config config, List<String> knownHostAlgs, boolean initialKex) { | ||
kex = Factory.Named.Util.getNames(config.getKeyExchangeFactories()); | ||
if (initialKex) { | ||
kex.add("[email protected]"); | ||
} | ||
sig = filterKnownHostKeyAlgorithms(Factory.Named.Util.getNames(config.getKeyAlgorithms()), knownHostAlgs); | ||
c2sCipher = s2cCipher = Factory.Named.Util.getNames(config.getCipherFactories()); | ||
c2sMAC = s2cMAC = Factory.Named.Util.getNames(config.getMACFactories()); | ||
|
@@ -91,6 +94,10 @@ public List<String> getKeyExchangeAlgorithms() { | |
return kex; | ||
} | ||
|
||
public boolean isStrictKeyExchangeSupportedByServer() { | ||
return kex.contains("[email protected]"); | ||
} | ||
|
||
public List<String> getHostKeyAlgorithms() { | ||
return sig; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters