Skip to content

Commit

Permalink
Add overloaded init methods that take the public key from a stream an…
Browse files Browse the repository at this point in the history
…d properly initialize. Resolves #907.
  • Loading branch information
dkocher committed Oct 23, 2023
1 parent b7dc869 commit a10bf3c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import net.schmizz.sshj.userauth.keyprovider.BaseFileKeyProvider;
import net.schmizz.sshj.userauth.keyprovider.FileKeyProvider;
import net.schmizz.sshj.userauth.keyprovider.KeyFormat;
import net.schmizz.sshj.userauth.password.PasswordFinder;
import org.bouncycastle.asn1.nist.NISTNamedCurves;
import org.bouncycastle.asn1.x9.X9ECParameters;
import org.bouncycastle.jce.spec.ECNamedCurveSpec;
Expand Down Expand Up @@ -118,6 +119,26 @@ public void init(File location) {
super.init(location);
}

@Override
public void init(Reader privateKey, Reader publicKey) {
try {
initPubKey(publicKey);
} catch (IOException e) {
log.warn("Error reading public key file: {}", e.toString());
}
super.init(privateKey, (Reader) null);
}

@Override
public void init(Reader privateKey, Reader publicKey, PasswordFinder pwdf) {
try {
initPubKey(publicKey);
} catch (IOException e) {
log.warn("Error reading public key file: {}", e.toString());
}
super.init(privateKey, null, pwdf);
}

@Override
protected KeyPair readKeyPair() throws IOException {
final BufferedReader reader = new BufferedReader(resource.getReader());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ public void init(Reader location, PasswordFinder pwdf) {
this.pwdf = pwdf;
}

@Override
public void init(Reader privateKey, Reader publicKey) {
assert publicKey == null;
init(privateKey);
}

@Override
public void init(Reader privateKey, Reader publicKey, PasswordFinder pwdf) {
assert publicKey == null;
init(privateKey);
this.pwdf = pwdf;
}

@Override
public void init(File location) {
assert location != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public interface FileKeyProvider

void init(Reader location);

void init(Reader privateKey, Reader publicKey);

void init(Reader privateKey, Reader publicKey, PasswordFinder pwdf);

void init(Reader location, PasswordFinder pwdf);

void init(String privateKey, String publicKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package net.schmizz.sshj.userauth.keyprovider;

import com.hierynomus.sshj.userauth.keyprovider.OpenSSHKeyFileUtil;
import net.schmizz.sshj.userauth.password.PasswordFinder;

import java.io.*;
import java.security.PublicKey;
Expand Down Expand Up @@ -80,6 +81,32 @@ public void init(String privateKey, String publicKey) {
super.init(privateKey, null);
}

@Override
public void init(Reader privateKey, Reader publicKey) {
if (publicKey != null) {
try {
initPubKey(publicKey);
} catch (IOException e) {
// let super provide both public & private key
log.warn("Error reading public key: {}", e.toString());
}
}
super.init(privateKey, (Reader) null);
}

@Override
public void init(Reader privateKey, Reader publicKey, PasswordFinder pwdf) {
if (publicKey != null) {
try {
initPubKey(publicKey);
} catch (IOException e) {
// let super provide both public & private key
log.warn("Error reading public key: {}", e.toString());
}
}
super.init(privateKey, null, pwdf);
}

/**
* Read and store the separate public key provided alongside the private key
*
Expand Down

0 comments on commit a10bf3c

Please sign in to comment.