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 hierynomus#907.
  • Loading branch information
dkocher committed Oct 23, 2023
1 parent b7dc869 commit 5e39fe9
Show file tree
Hide file tree
Showing 4 changed files with 41 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,16 @@ 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
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, publicKey);
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 @@ -80,6 +80,19 @@ 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);
}

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

0 comments on commit 5e39fe9

Please sign in to comment.