}
File keyfile = (instance.getKeyfile() == null) ? null
: new File(instance.getKeyfile());
String passphrase = null;
SshPrivateKeyFile pkf = null;
SshPrivateKey key;
if ((keyfile == null) || !keyfile.exists()) {
JFileChooser chooser = new JFileChooser();
chooser.setFileHidingEnabled(false);
chooser.setDialogTitle("Select Private Key File For Authentication");
if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
keyfile = chooser.getSelectedFile();
} else {
return false;
}
}
FileInputStream in = null;
try {
pkf = SshPrivateKeyFile.parse(keyfile);
} catch (InvalidSshKeyException iske) {
JOptionPane.showMessageDialog(parent, iske.getMessage());
return false;
} catch (IOException ioe) {
JOptionPane.showMessageDialog(parent, ioe.getMessage());
}
// Now see if its passphrase protected
if (pkf.isPassphraseProtected()) {
// Show the passphrase dialog
Window w = (Window) SwingUtilities.getAncestorOfClass(Window.class,
parent);
PassphraseDialog dialog = null;
if (w instanceof Frame) {
dialog = new PassphraseDialog((Frame) w);
} else if (w instanceof Dialog) {
dialog = new PassphraseDialog((Dialog) w);
} else {
dialog = new PassphraseDialog();
}
do {
dialog.setVisible(true);
if (dialog.isCancelled()) {
return false;
}
passphrase = new String(dialog.getPassphrase());
try {
key = pkf.toPrivateKey(passphrase);
break;
} catch (InvalidSshKeyException ihke) {
dialog.setMessage("Passphrase Invalid! Try again");
dialog.setMessageForeground(Color.red);
}
} while (true);
} else {
try {
key = pkf.toPrivateKey(passphrase);
} catch (InvalidSshKeyException ihke) {
return false;
}
}