|
| 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.signature |
| 17 | + |
| 18 | +import com.hierynomus.sshj.SshdContainer |
| 19 | +import net.schmizz.sshj.DefaultConfig |
| 20 | +import net.schmizz.sshj.SSHClient |
| 21 | +import net.schmizz.sshj.transport.verification.OpenSSHKnownHosts |
| 22 | +import spock.lang.Specification |
| 23 | +import spock.lang.Unroll |
| 24 | + |
| 25 | +import java.nio.file.Files |
| 26 | + |
| 27 | +/** |
| 28 | + * This is a brief test for verifying connection to a server using keys with certificates. |
| 29 | + * |
| 30 | + * Also, take a look at the unit test {@link net.schmizz.sshj.transport.verification.KeyWithCertificateUnitSpec}. |
| 31 | + */ |
| 32 | +class HostKeyWithCertificateSpec extends Specification { |
| 33 | + @Unroll |
| 34 | + def "accepting a signed host public key #hostKey"() { |
| 35 | + given: |
| 36 | + SshdContainer sshd = new SshdContainer.Builder() |
| 37 | + .withSshdConfig(""" |
| 38 | + PasswordAuthentication yes |
| 39 | + HostKey /etc/ssh/$hostKey |
| 40 | + HostCertificate /etc/ssh/${hostKey}-cert.pub |
| 41 | + """.stripMargin()) |
| 42 | + .build() |
| 43 | + sshd.start() |
| 44 | + |
| 45 | + and: |
| 46 | + File knownHosts = Files.createTempFile("known_hosts", "").toFile() |
| 47 | + knownHosts.deleteOnExit() |
| 48 | + |
| 49 | + and: |
| 50 | + File caPubKey = new File("src/itest/resources/keyfiles/certificates/CA_rsa.pem.pub") |
| 51 | + def address = "127.0.0.1" |
| 52 | + String knownHostsFileContents = "" + |
| 53 | + "@cert-authority ${ address} ${caPubKey.text}" + |
| 54 | + "\n@cert-authority [${address}]:${sshd.firstMappedPort} ${caPubKey.text}" |
| 55 | + knownHosts.write(knownHostsFileContents) |
| 56 | + |
| 57 | + and: |
| 58 | + SSHClient sshClient = new SSHClient(new DefaultConfig()) |
| 59 | + sshClient.addHostKeyVerifier(new OpenSSHKnownHosts(knownHosts)) |
| 60 | + sshClient.connect(address, sshd.firstMappedPort) |
| 61 | + |
| 62 | + when: |
| 63 | + sshClient.authPassword("sshj", "ultrapassword") |
| 64 | + |
| 65 | + then: |
| 66 | + sshClient.authenticated |
| 67 | + |
| 68 | + and: |
| 69 | + knownHosts.getText() == knownHostsFileContents |
| 70 | + |
| 71 | + cleanup: |
| 72 | + sshd.stop() |
| 73 | + |
| 74 | + where: |
| 75 | + hostKey << [ |
| 76 | + "ssh_host_ecdsa_256_key", |
| 77 | + "ssh_host_ecdsa_384_key", |
| 78 | + "ssh_host_ecdsa_521_key", |
| 79 | + "ssh_host_ed25519_384_key", |
| 80 | + "ssh_host_rsa_2048_key", |
| 81 | + ] |
| 82 | + } |
| 83 | +} |
0 commit comments