Skip to content

Commit 233f376

Browse files
authored
Added PKCS5 key file support (#262)
2 parents 5f292d3 + e78ae4d commit 233f376

File tree

11 files changed

+478
-36
lines changed

11 files changed

+478
-36
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
# Output dirs
1313
target/
1414
build/
15+
docs/
1516
.gradle/
17+
sshj.jar
1618

17-
19+
# MacOS X
20+
.DS_Store

src/main/java/net/schmizz/sshj/common/StreamCopier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void run() {
107107
log.debug("Done copying from {}", in);
108108
doneEvent.set();
109109
} catch (IOException ioe) {
110-
log.error("In pipe from {} to {}: {}", in, out, ioe);
110+
log.error(String.format("In pipe from %1$s to %2$s", in.toString(), out.toString()), ioe);
111111
doneEvent.deliverError(ioe);
112112
}
113113
}
@@ -136,7 +136,7 @@ public long copy()
136136

137137
final double timeSeconds = (System.currentTimeMillis() - startTime) / 1000.0;
138138
final double sizeKiB = count / 1024.0;
139-
log.debug("{} KiB transferred in {} seconds ({} KiB/s)", sizeKiB, timeSeconds, (sizeKiB / timeSeconds));
139+
log.debug(String.format("%1$,.1f KiB transferred in %2$,.1f seconds (%3$,.2f KiB/s)", sizeKiB, timeSeconds, (sizeKiB / timeSeconds)));
140140

141141
if (length != -1 && read == -1)
142142
throw new IOException("Encountered EOF, could not transfer " + length + " bytes");

src/main/java/net/schmizz/sshj/transport/verification/OpenSSHKnownHosts.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,13 @@ public static boolean isHashed(String line) {
262262
}
263263

264264
public interface HostEntry {
265+
KeyType getType();
266+
267+
String getFingerprint();
268+
269+
boolean appliesTo(String host)
270+
throws IOException;
271+
265272
boolean appliesTo(KeyType type, String host)
266273
throws IOException;
267274

@@ -279,6 +286,22 @@ public CommentEntry(String comment) {
279286
this.comment = comment;
280287
}
281288

289+
@Override
290+
public KeyType getType() {
291+
return KeyType.UNKNOWN;
292+
}
293+
294+
@Override
295+
public String getFingerprint() {
296+
return null;
297+
}
298+
299+
@Override
300+
public boolean appliesTo(String host)
301+
throws IOException {
302+
return false;
303+
}
304+
282305
@Override
283306
public boolean appliesTo(KeyType type, String host) {
284307
return false;
@@ -308,6 +331,16 @@ public AbstractEntry(Marker marker, KeyType type, PublicKey key) {
308331
this.key = key;
309332
}
310333

334+
@Override
335+
public KeyType getType() {
336+
return type;
337+
}
338+
339+
@Override
340+
public String getFingerprint() {
341+
return SecurityUtils.getFingerprint(key);
342+
}
343+
311344
@Override
312345
public boolean verify(PublicKey key)
313346
throws IOException {
@@ -349,6 +382,12 @@ protected String getHostPart() {
349382
return hostnames;
350383
}
351384

385+
@Override
386+
public boolean appliesTo(String host)
387+
throws IOException {
388+
return hosts.contains(host);
389+
}
390+
352391
@Override
353392
public boolean appliesTo(KeyType type, String host)
354393
throws IOException {
@@ -377,6 +416,12 @@ public HashedEntry(Marker marker, String hash, KeyType type, PublicKey key)
377416
}
378417
}
379418

419+
@Override
420+
public boolean appliesTo(String host)
421+
throws IOException {
422+
return hashedHost.equals(hashHost(host));
423+
}
424+
380425
@Override
381426
public boolean appliesTo(KeyType type, String host)
382427
throws IOException {
@@ -426,4 +471,4 @@ public static Marker fromString(String str) {
426471

427472
}
428473

429-
}
474+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* @version $Id:$
2020
*/
2121
public enum KeyFormat {
22+
PKCS5,
2223
PKCS8,
2324
OpenSSH,
2425
PuTTY,

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ private static KeyFormat keyFormatFromHeader(String header, boolean separatePubK
9696
if (separatePubKey) {
9797
// Can delay asking for password since have unencrypted pubkey
9898
return KeyFormat.OpenSSH;
99-
} else {
100-
// More general
99+
} else if (header.indexOf("BEGIN PRIVATE KEY") != -1 || header.indexOf("BEGIN ENCRYPTED PRIVATE KEY") != -1) {
101100
return KeyFormat.PKCS8;
101+
} else {
102+
return KeyFormat.PKCS5;
102103
}
103104
} else if (header.startsWith("PuTTY-User-Key-File-")) {
104105
return KeyFormat.PuTTY;

0 commit comments

Comments
 (0)