Skip to content

Commit 5674072

Browse files
Replace PKCS5 Key File Class with PKCS8 (#793)
* Replaced PKCS5 parsing with PKCS8 - Moved tests for PEM-encoded PKCS1 files to PKCS8 - Removed PKCS5 Key File implementation * Added PKCS8 test to retry password after initial failure Co-authored-by: Jeroen van Erp <[email protected]>
1 parent f33bfec commit 5674072

File tree

12 files changed

+88
-430
lines changed

12 files changed

+88
-430
lines changed

src/main/java/net/schmizz/sshj/DefaultConfig.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@
3636
import net.schmizz.sshj.transport.kex.DHGexSHA1;
3737
import net.schmizz.sshj.transport.kex.DHGexSHA256;
3838
import net.schmizz.sshj.transport.kex.ECDHNistP;
39-
import net.schmizz.sshj.transport.random.BouncyCastleRandom;
4039
import net.schmizz.sshj.transport.random.JCERandom;
4140
import net.schmizz.sshj.transport.random.SingletonRandomFactory;
4241
import net.schmizz.sshj.userauth.keyprovider.OpenSSHKeyFile;
43-
import net.schmizz.sshj.userauth.keyprovider.PKCS5KeyFile;
4442
import net.schmizz.sshj.userauth.keyprovider.PKCS8KeyFile;
4543
import net.schmizz.sshj.userauth.keyprovider.PuTTYKeyFile;
4644
import org.slf4j.Logger;
@@ -162,7 +160,6 @@ protected void initFileKeyProviderFactories(boolean bouncyCastleRegistered) {
162160
setFileKeyProviderFactories(
163161
new OpenSSHKeyV1KeyFile.Factory(),
164162
new PKCS8KeyFile.Factory(),
165-
new PKCS5KeyFile.Factory(),
166163
new OpenSSHKeyFile.Factory(),
167164
new PuTTYKeyFile.Factory());
168165
}

src/main/java/net/schmizz/sshj/SSHClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ public KeyProvider loadKeys(String location, char[] passphrase)
551551
* Creates a {@link KeyProvider} instance from given location on the file system. Currently the following private key files are supported:
552552
* <ul>
553553
* <li>PKCS8 (OpenSSH uses this format)</li>
554-
* <li>PKCS5</li>
554+
* <li>PEM-encoded PKCS1</li>
555555
* <li>Putty keyfile</li>
556556
* <li>openssh-key-v1 (New OpenSSH keyfile format)</li>
557557
* </ul>

src/main/java/net/schmizz/sshj/userauth/keyprovider/KeyFormat.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
package net.schmizz.sshj.userauth.keyprovider;
1717

1818
/**
19-
* @version $Id:$
19+
* Key File Formats
2020
*/
2121
public enum KeyFormat {
22-
PKCS5,
2322
PKCS8,
2423
OpenSSH,
2524
OpenSSHv1,

src/main/java/net/schmizz/sshj/userauth/keyprovider/KeyProviderUtil.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public class KeyProviderUtil {
2727
* <p/>
2828
* Return values are consistent with the {@code NamedFactory} implementations in the {@code keyprovider} package.
2929
*
30-
* @param location
30+
* @param location File Path to key
3131
* @return name of the key file format
32-
* @throws java.io.IOException
32+
* @throws java.io.IOException Thrown on file processing failures
3333
*/
3434
public static KeyFormat detectKeyFileFormat(File location)
3535
throws IOException {
@@ -45,7 +45,7 @@ public static KeyFormat detectKeyFileFormat(File location)
4545
* @param privateKey Private key stored in a string
4646
* @param separatePubKey Is the public key stored separately from the private key
4747
* @return name of the key file format
48-
* @throws java.io.IOException
48+
* @throws java.io.IOException Thrown on file processing failures
4949
*/
5050
public static KeyFormat detectKeyFileFormat(String privateKey, boolean separatePubKey)
5151
throws IOException {
@@ -60,7 +60,7 @@ public static KeyFormat detectKeyFileFormat(String privateKey, boolean separateP
6060
* @param privateKey Private key accessible through a {@code Reader}
6161
* @param separatePubKey Is the public key stored separately from the private key
6262
* @return name of the key file format
63-
* @throws java.io.IOException
63+
* @throws java.io.IOException Thrown on file processing failures
6464
*/
6565
public static KeyFormat detectKeyFileFormat(Reader privateKey, boolean separatePubKey)
6666
throws IOException {
@@ -94,10 +94,8 @@ private static KeyFormat keyFormatFromHeader(String header, boolean separatePubK
9494
} else if (separatePubKey) {
9595
// Can delay asking for password since have unencrypted pubkey
9696
return KeyFormat.OpenSSH;
97-
} else if (header.contains("BEGIN PRIVATE KEY") || header.contains("BEGIN ENCRYPTED PRIVATE KEY")) {
98-
return KeyFormat.PKCS8;
9997
} else {
100-
return KeyFormat.PKCS5;
98+
return KeyFormat.PKCS8;
10199
}
102100
} else if (header.startsWith("PuTTY-User-Key-File-")) {
103101
return KeyFormat.PuTTY;

src/main/java/net/schmizz/sshj/userauth/keyprovider/PKCS5KeyFile.java

Lines changed: 0 additions & 272 deletions
This file was deleted.

src/main/java/net/schmizz/sshj/userauth/keyprovider/PKCS8KeyFile.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
import java.io.IOException;
4040
import java.security.KeyPair;
4141

42-
/** Represents a PKCS8-encoded key file. This is the format used by (old-style) OpenSSH and OpenSSL. */
42+
/**
43+
* Key File implementation supporting PEM-encoded PKCS8 and PKCS1 formats with or without password-based encryption
44+
*/
4345
public class PKCS8KeyFile extends BaseFileKeyProvider {
4446

4547
public static class Factory

src/test/groovy/com/hierynomus/sshj/userauth/keyprovider/FileKeyProviderSpec.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class FileKeyProviderSpec extends Specification {
6060

6161
where:
6262
format | keyfile
63-
KeyFormat.PKCS5 | "src/test/resources/keyformats/pkcs5"
63+
KeyFormat.PKCS8 | "src/test/resources/keyformats/pkcs8"
6464
KeyFormat.OpenSSH | "src/test/resources/keyformats/openssh"
6565
}
6666
}

src/test/java/net/schmizz/sshj/keyprovider/KeyProviderUtilTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public void testOpenSsh() throws IOException {
3535
}
3636

3737
@Test
38-
public void testPkcs5() throws IOException {
39-
KeyFormat format = KeyProviderUtil.detectKeyFileFormat(new File(ROOT, "pkcs5"));
40-
assertEquals(KeyFormat.PKCS5, format);
38+
public void testPkcs1Rsa() throws IOException {
39+
KeyFormat format = KeyProviderUtil.detectKeyFileFormat(new File(ROOT, "pkcs1-rsa"));
40+
assertEquals(KeyFormat.PKCS8, format);
4141
}
4242

4343
@Test

0 commit comments

Comments
 (0)