diff --git a/src/main/java/net/schmizz/sshj/DefaultConfig.java b/src/main/java/net/schmizz/sshj/DefaultConfig.java index b9cd651c..9275f72c 100644 --- a/src/main/java/net/schmizz/sshj/DefaultConfig.java +++ b/src/main/java/net/schmizz/sshj/DefaultConfig.java @@ -34,6 +34,8 @@ import net.schmizz.sshj.transport.random.JCERandom; import net.schmizz.sshj.transport.random.SingletonRandomFactory; import net.schmizz.sshj.userauth.keyprovider.OpenSSHKeyFile; import com.hierynomus.sshj.userauth.keyprovider.OpenSSHKeyV1KeyFile; + +import net.schmizz.sshj.userauth.keyprovider.PKCS5KeyFile; import net.schmizz.sshj.userauth.keyprovider.PKCS8KeyFile; import net.schmizz.sshj.userauth.keyprovider.PuTTYKeyFile; import org.slf4j.Logger; @@ -122,7 +124,12 @@ public class DefaultConfig protected void initFileKeyProviderFactories(boolean bouncyCastleRegistered) { if (bouncyCastleRegistered) { - setFileKeyProviderFactories(new OpenSSHKeyV1KeyFile.Factory(), new PKCS8KeyFile.Factory(), new OpenSSHKeyFile.Factory(), new PuTTYKeyFile.Factory()); + setFileKeyProviderFactories( + new OpenSSHKeyV1KeyFile.Factory(), + new PKCS8KeyFile.Factory(), + new PKCS5KeyFile.Factory(), + new OpenSSHKeyFile.Factory(), + new PuTTYKeyFile.Factory()); } } diff --git a/src/main/java/net/schmizz/sshj/userauth/keyprovider/KeyProviderUtil.java b/src/main/java/net/schmizz/sshj/userauth/keyprovider/KeyProviderUtil.java index 4df8aca2..2459d83f 100644 --- a/src/main/java/net/schmizz/sshj/userauth/keyprovider/KeyProviderUtil.java +++ b/src/main/java/net/schmizz/sshj/userauth/keyprovider/KeyProviderUtil.java @@ -96,7 +96,7 @@ public class KeyProviderUtil { return KeyFormat.OpenSSH; } else if (header.contains("BEGIN PRIVATE KEY") || header.contains("BEGIN ENCRYPTED PRIVATE KEY")) { return KeyFormat.PKCS8; - } else { + } else { return KeyFormat.PKCS5; } } else if (header.startsWith("PuTTY-User-Key-File-")) { diff --git a/src/test/groovy/com/hierynomus/sshj/userauth/keyprovider/FileKeyProviderSpec.groovy b/src/test/groovy/com/hierynomus/sshj/userauth/keyprovider/FileKeyProviderSpec.groovy new file mode 100644 index 00000000..e81b6e2c --- /dev/null +++ b/src/test/groovy/com/hierynomus/sshj/userauth/keyprovider/FileKeyProviderSpec.groovy @@ -0,0 +1,58 @@ +/* + * 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.userauth.keyprovider + +import com.hierynomus.sshj.test.SshFixture +import net.schmizz.sshj.SSHClient +import net.schmizz.sshj.userauth.keyprovider.KeyFormat +import org.apache.sshd.server.auth.pubkey.AcceptAllPublickeyAuthenticator +import org.junit.Rule +import spock.lang.Specification +import spock.lang.Unroll + +class FileKeyProviderSpec extends Specification { + @Rule + SshFixture fixture = new SshFixture(false) + + def setup() { + fixture.getServer().setPublickeyAuthenticator(AcceptAllPublickeyAuthenticator.INSTANCE) + fixture.start() + } + + def cleanup() { + fixture.stopServer() + } + + @Unroll + def "should have #format FileKeyProvider enabled by default"() { + given: + SSHClient client = fixture.setupConnectedDefaultClient() + + when: + client.authPublickey("jeroen", keyfile) + + then: + client.isAuthenticated() + + cleanup: + client.disconnect() + + where: + format | keyfile + KeyFormat.PKCS5 | "src/test/resources/keyformats/pkcs5" + KeyFormat.OpenSSH | "src/test/resources/keyformats/openssh" + } +}