|
| 1 | +/* |
| 2 | + * Copyright (C)2009 - SSHJ Contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.hierynomus.sshj |
| 17 | + |
| 18 | +import com.hierynomus.sshj.key.KeyAlgorithms |
| 19 | +import net.schmizz.sshj.Config |
| 20 | +import net.schmizz.sshj.DefaultConfig |
| 21 | +import org.testcontainers.images.builder.dockerfile.DockerfileBuilder |
| 22 | +import spock.lang.Specification |
| 23 | +import spock.lang.Unroll |
| 24 | + |
| 25 | +import java.nio.file.Paths |
| 26 | + |
| 27 | +/** |
| 28 | + * Checks that SSHJ is able to work with OpenSSH 8.8, which removed ssh-rsa signature from the default setup. |
| 29 | + */ |
| 30 | +class RsaShaKeySignatureTest extends Specification { |
| 31 | + private static final Map<String, KeyAlgorithms.Factory> SSH_HOST_KEYS_AND_FACTORIES = [ |
| 32 | + 'ssh_host_ecdsa_256_key': KeyAlgorithms.ECDSASHANistp256(), |
| 33 | + 'ssh_host_ecdsa_384_key': KeyAlgorithms.ECDSASHANistp384(), |
| 34 | + 'ssh_host_ecdsa_521_key': KeyAlgorithms.ECDSASHANistp521(), |
| 35 | + 'ssh_host_ed25519_384_key': KeyAlgorithms.EdDSA25519(), |
| 36 | + 'ssh_host_rsa_2048_key': KeyAlgorithms.RSASHA512(), |
| 37 | + ] |
| 38 | + |
| 39 | + private static void dockerfileBuilder(DockerfileBuilder it, String hostKey, String pubkeyAcceptedAlgorithms) { |
| 40 | + it.from("archlinux:base") |
| 41 | + it.run('yes | pacman -Sy core/openssh' + |
| 42 | + ' && (' + |
| 43 | + ' V=$(echo $(/usr/sbin/sshd -h 2>&1) | grep -o \'OpenSSH_[0-9][0-9]*[.][0-9][0-9]*p[0-9]\');' + |
| 44 | + ' if [[ "$V" < OpenSSH_8.8p1 ]]; then' + |
| 45 | + ' echo $V is too old 1>&2;' + |
| 46 | + ' exit 1;' + |
| 47 | + ' fi' + |
| 48 | + ')' + |
| 49 | + ' && set -o pipefail ' + |
| 50 | + ' && useradd --create-home sshj' + |
| 51 | + ' && echo \"sshj:ultrapassword\" | chpasswd') |
| 52 | + it.add("authorized_keys", "/home/sshj/.ssh/") |
| 53 | + it.add(hostKey, '/etc/ssh/') |
| 54 | + it.run('chmod go-rwx /etc/ssh/ssh_host_*' + |
| 55 | + ' && chown -R sshj /home/sshj/.ssh' + |
| 56 | + ' && chmod -R go-rwx /home/sshj/.ssh') |
| 57 | + it.expose(22) |
| 58 | + |
| 59 | + def cmd = [ |
| 60 | + '/usr/sbin/sshd', |
| 61 | + '-D', |
| 62 | + '-e', |
| 63 | + '-f', '/dev/null', |
| 64 | + '-o', 'LogLevel=DEBUG2', |
| 65 | + '-o', "HostKey=/etc/ssh/$hostKey", |
| 66 | + ] |
| 67 | + if (pubkeyAcceptedAlgorithms != null) { |
| 68 | + cmd += ['-o', "PubkeyAcceptedAlgorithms=$pubkeyAcceptedAlgorithms"] |
| 69 | + } |
| 70 | + it.cmd(cmd as String[]) |
| 71 | + } |
| 72 | + |
| 73 | + private static SshdContainer makeSshdContainer(String hostKey, String pubkeyAcceptedAlgorithms) { |
| 74 | + return new SshdContainer(new SshdContainer.DebugLoggingImageFromDockerfile() |
| 75 | + .withFileFromPath("authorized_keys", Paths.get("src/itest/docker-image/authorized_keys")) |
| 76 | + .withFileFromPath(hostKey, Paths.get("src/itest/docker-image/test-container/host_keys/$hostKey")) |
| 77 | + .withDockerfileFromBuilder { |
| 78 | + dockerfileBuilder(it, hostKey, pubkeyAcceptedAlgorithms) |
| 79 | + }) |
| 80 | + } |
| 81 | + |
| 82 | + @Unroll |
| 83 | + def "connect to a server with host key #hostKey that does not support ssh-rsa"() { |
| 84 | + given: |
| 85 | + SshdContainer sshd = makeSshdContainer(hostKey, "rsa-sha2-512,rsa-sha2-256,ssh-ed25519") |
| 86 | + sshd.start() |
| 87 | + |
| 88 | + and: |
| 89 | + Config config = new DefaultConfig() |
| 90 | + config.keyAlgorithms = [ |
| 91 | + KeyAlgorithms.RSASHA512(), |
| 92 | + KeyAlgorithms.RSASHA256(), |
| 93 | + SSH_HOST_KEYS_AND_FACTORIES[hostKey], |
| 94 | + ] |
| 95 | + |
| 96 | + when: |
| 97 | + def sshClient = sshd.getConnectedClient(config) |
| 98 | + sshClient.authPublickey("sshj", "src/itest/resources/keyfiles/id_rsa_opensshv1") |
| 99 | + |
| 100 | + then: |
| 101 | + sshClient.isAuthenticated() |
| 102 | + |
| 103 | + cleanup: |
| 104 | + sshClient?.disconnect() |
| 105 | + sshd.stop() |
| 106 | + |
| 107 | + where: |
| 108 | + hostKey << SSH_HOST_KEYS_AND_FACTORIES.keySet() |
| 109 | + } |
| 110 | + |
| 111 | + @Unroll |
| 112 | + def "connect to a default server with host key #hostKey using a default config"() { |
| 113 | + given: |
| 114 | + SshdContainer sshd = makeSshdContainer(hostKey, null) |
| 115 | + sshd.start() |
| 116 | + |
| 117 | + when: |
| 118 | + def sshClient = sshd.getConnectedClient() |
| 119 | + sshClient.authPublickey("sshj", "src/itest/resources/keyfiles/id_rsa_opensshv1") |
| 120 | + |
| 121 | + then: |
| 122 | + sshClient.isAuthenticated() |
| 123 | + |
| 124 | + cleanup: |
| 125 | + sshClient?.disconnect() |
| 126 | + sshd.stop() |
| 127 | + |
| 128 | + where: |
| 129 | + hostKey << SSH_HOST_KEYS_AND_FACTORIES.keySet() |
| 130 | + } |
| 131 | + |
| 132 | + @Unroll |
| 133 | + def "connect to a server with host key #hostkey that supports only ssh-rsa"() { |
| 134 | + given: |
| 135 | + SshdContainer sshd = makeSshdContainer(hostKey, "ssh-rsa,ssh-ed25519") |
| 136 | + sshd.start() |
| 137 | + |
| 138 | + and: |
| 139 | + Config config = new DefaultConfig() |
| 140 | + config.keyAlgorithms = [ |
| 141 | + KeyAlgorithms.SSHRSA(), |
| 142 | + SSH_HOST_KEYS_AND_FACTORIES[hostKey], |
| 143 | + ] |
| 144 | + |
| 145 | + when: |
| 146 | + def sshClient = sshd.getConnectedClient(config) |
| 147 | + sshClient.authPublickey("sshj", "src/itest/resources/keyfiles/id_rsa_opensshv1") |
| 148 | + |
| 149 | + then: |
| 150 | + sshClient.isAuthenticated() |
| 151 | + |
| 152 | + cleanup: |
| 153 | + sshClient.disconnect() |
| 154 | + sshd.stop() |
| 155 | + |
| 156 | + where: |
| 157 | + hostKey << SSH_HOST_KEYS_AND_FACTORIES.keySet() |
| 158 | + } |
| 159 | +} |
0 commit comments