Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added PKCS5 key file support (without requiring BouncyCastle) #262

Merged
merged 9 commits into from
Aug 15, 2016
Merged
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
# Output dirs
target/
build/
docs/
.gradle/
sshj.jar


# MacOS X
.DS_Store
4 changes: 2 additions & 2 deletions src/main/java/net/schmizz/sshj/common/StreamCopier.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void run() {
log.debug("Done copying from {}", in);
doneEvent.set();
} catch (IOException ioe) {
log.error("In pipe from {} to {}: {}", in, out, ioe);
log.error(String.format("In pipe from %1$s to %2$s", in.toString(), out.toString()), ioe);
doneEvent.deliverError(ioe);
}
}
Expand Down Expand Up @@ -136,7 +136,7 @@ public long copy()

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

if (length != -1 && read == -1)
throw new IOException("Encountered EOF, could not transfer " + length + " bytes");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,13 @@ public static boolean isHashed(String line) {
}

public interface HostEntry {
KeyType getType();

String getFingerprint();

boolean appliesTo(String host)
throws IOException;

boolean appliesTo(KeyType type, String host)
throws IOException;

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

@Override
public KeyType getType() {
return KeyType.UNKNOWN;
}

@Override
public String getFingerprint() {
return null;
}

@Override
public boolean appliesTo(String host)
throws IOException {
return false;
}

@Override
public boolean appliesTo(KeyType type, String host) {
return false;
Expand Down Expand Up @@ -308,6 +331,16 @@ public AbstractEntry(Marker marker, KeyType type, PublicKey key) {
this.key = key;
}

@Override
public KeyType getType() {
return type;
}

@Override
public String getFingerprint() {
return SecurityUtils.getFingerprint(key);
}

@Override
public boolean verify(PublicKey key)
throws IOException {
Expand Down Expand Up @@ -349,6 +382,12 @@ protected String getHostPart() {
return hostnames;
}

@Override
public boolean appliesTo(String host)
throws IOException {
return hosts.contains(host);
}

@Override
public boolean appliesTo(KeyType type, String host)
throws IOException {
Expand Down Expand Up @@ -377,6 +416,12 @@ public HashedEntry(Marker marker, String hash, KeyType type, PublicKey key)
}
}

@Override
public boolean appliesTo(String host)
throws IOException {
return hashedHost.equals(hashHost(host));
}

@Override
public boolean appliesTo(KeyType type, String host)
throws IOException {
Expand Down Expand Up @@ -426,4 +471,4 @@ public static Marker fromString(String str) {

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* @version $Id:$
*/
public enum KeyFormat {
PKCS5,
PKCS8,
OpenSSH,
PuTTY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ private static KeyFormat keyFormatFromHeader(String header, boolean separatePubK
if (separatePubKey) {
// Can delay asking for password since have unencrypted pubkey
return KeyFormat.OpenSSH;
} else {
// More general
} else if (header.indexOf("BEGIN PRIVATE KEY") != -1 || header.indexOf("BEGIN ENCRYPTED PRIVATE KEY") != -1) {
return KeyFormat.PKCS8;
} else {
return KeyFormat.PKCS5;
}
} else if (header.startsWith("PuTTY-User-Key-File-")) {
return KeyFormat.PuTTY;
Expand Down
Loading