Skip to content

Commit

Permalink
PKCS5 support is now part of PKCS8 classes. Also refer to hierynomus/…
Browse files Browse the repository at this point in the history
  • Loading branch information
ylangisc committed Aug 11, 2022
1 parent da88ab4 commit 664d237
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import net.schmizz.sshj.userauth.keyprovider.KeyFormat;
import net.schmizz.sshj.userauth.keyprovider.KeyProviderUtil;
import net.schmizz.sshj.userauth.keyprovider.OpenSSHKeyFile;
import net.schmizz.sshj.userauth.keyprovider.PKCS5KeyFile;
import net.schmizz.sshj.userauth.keyprovider.PKCS8KeyFile;
import net.schmizz.sshj.userauth.keyprovider.PuTTYKeyFile;
import net.schmizz.sshj.userauth.password.PasswordFinder;
Expand Down Expand Up @@ -76,39 +75,39 @@ public String authenticate(final Host bookmark, final LoginCallback prompt, fina
log.info(String.format("Reading private key %s with key format %s", identity, format));
}
provider.init(
new InputStreamReader(identity.getInputStream(), StandardCharsets.UTF_8),
new PasswordFinder() {
@Override
public char[] reqPassword(Resource<?> resource) {
if(StringUtils.isEmpty(credentials.getIdentityPassphrase())) {
try {
// Use password prompt
final Credentials input = prompt.prompt(bookmark,
LocaleFactory.localizedString("Private key password protected", "Credentials"),
String.format("%s (%s)",
LocaleFactory.localizedString("Enter the passphrase for the private key file", "Credentials"),
identity.getAbbreviatedPath()),
new LoginOptions()
.icon(bookmark.getProtocol().disk())
.user(false).password(true)
);
credentials.setSaved(input.isSaved());
credentials.setIdentityPassphrase(input.getPassword());
}
catch(LoginCanceledException e) {
// Return null if user cancels
return StringUtils.EMPTY.toCharArray();
new InputStreamReader(identity.getInputStream(), StandardCharsets.UTF_8),
new PasswordFinder() {
@Override
public char[] reqPassword(Resource<?> resource) {
if(StringUtils.isEmpty(credentials.getIdentityPassphrase())) {
try {
// Use password prompt
final Credentials input = prompt.prompt(bookmark,
LocaleFactory.localizedString("Private key password protected", "Credentials"),
String.format("%s (%s)",
LocaleFactory.localizedString("Enter the passphrase for the private key file", "Credentials"),
identity.getAbbreviatedPath()),
new LoginOptions()
.icon(bookmark.getProtocol().disk())
.user(false).password(true)
);
credentials.setSaved(input.isSaved());
credentials.setIdentityPassphrase(input.getPassword());
}
catch(LoginCanceledException e) {
// Return null if user cancels
return StringUtils.EMPTY.toCharArray();
}
}
config.setPassword(credentials.getIdentityPassphrase());
return credentials.getIdentityPassphrase().toCharArray();
}
config.setPassword(credentials.getIdentityPassphrase());
return credentials.getIdentityPassphrase().toCharArray();
}

@Override
public boolean shouldRetry(Resource<?> resource) {
return false;
@Override
public boolean shouldRetry(Resource<?> resource) {
return false;
}
}
}
);
return this.computeFingerprint(provider);
}
Expand Down Expand Up @@ -137,8 +136,6 @@ private String computeFingerprint(final FileKeyProvider provider) throws Backgro

private FileKeyProvider buildProvider(final Local identity, final KeyFormat format) throws InteroperabilityException {
switch(format) {
case PKCS5:
return new PKCS5KeyFile.Factory().create();
case PKCS8:
return new PKCS8KeyFile.Factory().create();
case OpenSSH:
Expand All @@ -156,8 +153,8 @@ private KeyFormat detectKeyFormat(final Local identity) throws BackgroundExcepti
final KeyFormat format;
try (InputStream is = identity.getInputStream()) {
format = KeyProviderUtil.detectKeyFileFormat(
new InputStreamReader(is, StandardCharsets.UTF_8),
true);
new InputStreamReader(is, StandardCharsets.UTF_8),
true);
}
catch(IOException e) {
throw new DefaultIOExceptionMappingService().map(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import net.schmizz.sshj.userauth.keyprovider.KeyFormat;
import net.schmizz.sshj.userauth.keyprovider.KeyProviderUtil;
import net.schmizz.sshj.userauth.keyprovider.OpenSSHKeyFile;
import net.schmizz.sshj.userauth.keyprovider.PKCS5KeyFile;
import net.schmizz.sshj.userauth.keyprovider.PKCS8KeyFile;
import net.schmizz.sshj.userauth.keyprovider.PuTTYKeyFile;
import net.schmizz.sshj.userauth.method.AuthPublickey;
Expand Down Expand Up @@ -71,14 +70,11 @@ public Boolean authenticate(final Host bookmark, final LoginCallback prompt, fin
final AtomicBoolean canceled = new AtomicBoolean();
try {
final KeyFormat format = KeyProviderUtil.detectKeyFileFormat(
new InputStreamReader(identity.getInputStream(), StandardCharsets.UTF_8), true);
new InputStreamReader(identity.getInputStream(), StandardCharsets.UTF_8), true);
if(log.isInfoEnabled()) {
log.info(String.format("Reading private key %s with key format %s", identity, format));
}
switch(format) {
case PKCS5:
provider = new PKCS5KeyFile.Factory().create();
break;
case PKCS8:
provider = new PKCS8KeyFile.Factory().create();
break;
Expand All @@ -101,13 +97,13 @@ public char[] reqPassword(Resource<?> resource) {
try {
// Use password prompt
final Credentials input = prompt.prompt(bookmark,
LocaleFactory.localizedString("Private key password protected", "Credentials"),
String.format("%s (%s)",
LocaleFactory.localizedString("Enter the passphrase for the private key file", "Credentials"),
identity.getAbbreviatedPath()),
new LoginOptions()
.icon(bookmark.getProtocol().disk())
.user(false).password(true)
LocaleFactory.localizedString("Private key password protected", "Credentials"),
String.format("%s (%s)",
LocaleFactory.localizedString("Enter the passphrase for the private key file", "Credentials"),
identity.getAbbreviatedPath()),
new LoginOptions()
.icon(bookmark.getProtocol().disk())
.user(false).password(true)
);
credentials.setSaved(input.isSaved());
credentials.setIdentityPassphrase(input.getPassword());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,13 @@ public Pattern toPattern() {
final KeyFormat format;
try {
format = KeyProviderUtil.detectKeyFileFormat(
new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8), true);
new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8), true);
}
catch(AccessDeniedException | IOException e) {
log.debug(String.format("Ignore file %s with unknown format. %s", file, e.getMessage()));
continue;
}
switch(format) {
case PKCS5:
case PKCS8:
case OpenSSH:
case OpenSSHv1:
Expand Down

0 comments on commit 664d237

Please sign in to comment.