Skip to content

Commit

Permalink
Merge pull request #1490 from TranceLove/verify/issue1485
Browse files Browse the repository at this point in the history
Verification for support of "new format" OpenSSH keys
  • Loading branch information
EmmanuelMess committed Nov 2, 2018
1 parent cfeed83 commit 42e1ca6
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ ext {
supportLibVersion = '27.1.1'
robolectricVersion = '3.8'
glideVersion = '4.8.0'
sshjVersion = '0.26.0'
}

dependencies {
Expand Down Expand Up @@ -150,7 +151,7 @@ dependencies {
implementation 'com.github.npgall:concurrent-trees:2.6.1'//Concurrent tries

//SFTP
implementation 'com.hierynomus:sshj:0.23.0'
implementation "com.hierynomus:sshj:$sshjVersion"

implementation 'com.madgag.spongycastle:bcpkix-jdk15on:1.58.0.0'
implementation 'com.madgag.spongycastle:prov:1.58.0.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.amaze.filemanager.asynchronous.asynctasks.ssh;

import android.support.test.runner.AndroidJUnit4;

import net.schmizz.sshj.userauth.password.PasswordFinder;
import net.schmizz.sshj.userauth.password.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.lang.reflect.Field;
import java.util.concurrent.CountDownLatch;

import static org.junit.Assert.assertNotNull;

@RunWith(AndroidJUnit4.class)
public class PemToKeyPairTaskTest2 {

//public key for authorized_keys: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGxJHFewxU9tJn9hUq9e2C/+ELFw83NpmJ5NLFOzU7O3 test-openssh-key
private static final String unencryptedOpenSshKey = "-----BEGIN OPENSSH PRIVATE KEY-----\n" +
"b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW\n" +
"QyNTUxOQAAACBsSRxXsMVPbSZ/YVKvXtgv/hCxcPNzaZieTSxTs1OztwAAAJhX2WUxV9ll\n" +
"MQAAAAtzc2gtZWQyNTUxOQAAACBsSRxXsMVPbSZ/YVKvXtgv/hCxcPNzaZieTSxTs1Oztw\n" +
"AAAECjSjwwMXPzbZWq/EBoA4HA9Lr7B1/Tw78K+k1zqAJwA2xJHFewxU9tJn9hUq9e2C/+\n" +
"ELFw83NpmJ5NLFOzU7O3AAAADmFpcndhdmVAaHN2MDEwAQIDBAUGBw==\n" +
"-----END OPENSSH PRIVATE KEY-----";

//Passphrase = 12345678
//public key for authorized_keys: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHio1/33U0XoewL1qGLmTzxyVNeYP5b0Tunv/SQrQi92 test-openssh-key
private static final String encryptedOpenSshKey = "-----BEGIN OPENSSH PRIVATE KEY-----\n" +
"b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jdHIAAAAGYmNyeXB0AAAAGAAAABCwlfECA9\n" +
"+EGLwKVApTmomnAAAAZAAAAAEAAAAzAAAAC3NzaC1lZDI1NTE5AAAAIHio1/33U0XoewL1\n" +
"qGLmTzxyVNeYP5b0Tunv/SQrQi92AAAAoD2dysYInLaJgXIv6k/xFv7OblU9vWkCwcYnDW\n" +
"8Zj5+ke8QL2/r7EUBEvY1H02GenlEH1Ufct8ce7/eAWwd7aWukaSQXlKW9IBt5YrxW8+P/\n" +
"wrHcd/Z92eQ0E7NV6b6LnghGYlyCjpSBW+mxa0AAYPD21c95d/HvJF6zxQl/IKCCLdOrr/\n" +
"ilMCSIGQEdg71hA3MMZsRbUvazsnZTZXD9PLI=\n" +
"-----END OPENSSH PRIVATE KEY-----";

@Test
public void testUnencryptedKeyToKeyPair() throws InterruptedException {
CountDownLatch waiter = new CountDownLatch(1);
PemToKeyPairTask task = new PemToKeyPairTask(unencryptedOpenSshKey, result -> {
assertNotNull(result);
assertNotNull(result.getPublic());
assertNotNull(result.getPrivate());
waiter.countDown();
});
task.execute();
waiter.await();
}

@Test
public void testEncryptedKeyToKeyPair() throws InterruptedException, NoSuchFieldException, IllegalAccessException {
CountDownLatch waiter = new CountDownLatch(1);
PemToKeyPairTask task = new PemToKeyPairTask(encryptedOpenSshKey, result -> {
assertNotNull(result);
assertNotNull(result.getPublic());
assertNotNull(result.getPrivate());
waiter.countDown();
});
Field field = PemToKeyPairTask.class.getDeclaredField("passwordFinder");
field.setAccessible(true);
field.set(task, new PasswordFinder() {
@Override
public char[] reqPassword(Resource<?> resource) {
return "12345678".toCharArray();
}

@Override
public boolean shouldRetry(Resource<?> resource) {
return false;
}
});
task.execute();
waiter.await();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ public class CreateFileOnSshdTest extends AbstractSftpServerTest {

@Test
public void testCreateFileNormal() throws Exception {
tearDown();
createSshServer(new VirtualFileSystemFactory(Paths.get(Environment.getExternalStorageDirectory().getAbsolutePath())));
}

@Test
public void testCreateFilePermissionDenied() throws Exception{
tearDown();
createSshServer(new VirtualFileSystemFactory(){
@Override
public FileSystem createFileSystem(Session session) throws IOException {
Expand Down

0 comments on commit 42e1ca6

Please sign in to comment.