001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.common.authentication;
027:
028: import com.sshtools.j2ssh.authentication.AuthenticationProtocolException;
029: import com.sshtools.j2ssh.authentication.PublicKeyAuthenticationClient;
030: import com.sshtools.j2ssh.authentication.SshAuthenticationClient;
031: import com.sshtools.j2ssh.authentication.SshAuthenticationPrompt;
032: import com.sshtools.j2ssh.transport.publickey.InvalidSshKeyException;
033: import com.sshtools.j2ssh.transport.publickey.SshPrivateKey;
034: import com.sshtools.j2ssh.transport.publickey.SshPrivateKeyFile;
035:
036: import java.awt.Color;
037: import java.awt.Component;
038: import java.awt.Dialog;
039: import java.awt.Frame;
040: import java.awt.Window;
041:
042: import java.io.File;
043: import java.io.FileInputStream;
044: import java.io.IOException;
045:
046: import javax.swing.JFileChooser;
047: import javax.swing.JOptionPane;
048: import javax.swing.SwingUtilities;
049:
050: /**
051: *
052: *
053: * @author $author$
054: * @version $Revision: 1.14 $
055: */
056: public class PublicKeyAuthenticationPrompt implements
057: SshAuthenticationPrompt {
058: private Component parent;
059: private PublicKeyAuthenticationClient instance;
060:
061: /**
062: * Creates a new PublicKeyAuthenticationPrompt object.
063: *
064: * @param parent
065: */
066: public PublicKeyAuthenticationPrompt(Component parent) {
067: this .parent = parent;
068: }
069:
070: /**
071: *
072: *
073: * @param instance
074: *
075: * @throws AuthenticationProtocolException
076: */
077: public void setInstance(SshAuthenticationClient instance)
078: throws AuthenticationProtocolException {
079: if (instance instanceof PublicKeyAuthenticationClient) {
080: this .instance = (PublicKeyAuthenticationClient) instance;
081: } else {
082: throw new AuthenticationProtocolException(
083: "PublicKeyAuthenticationClient instance required");
084: }
085: }
086:
087: /**
088: *
089: *
090: * @return
091: */
092: public boolean showPrompt(SshAuthenticationClient inst)
093: throws AuthenticationProtocolException {
094: if (inst instanceof PublicKeyAuthenticationClient) {
095: instance = (PublicKeyAuthenticationClient) inst;
096: } else {
097: throw new AuthenticationProtocolException(
098: "PublicKeyAuthenticationClient instance required");
099: }
100:
101: File keyfile = (instance.getKeyfile() == null) ? null
102: : new File(instance.getKeyfile());
103: String passphrase = null;
104: SshPrivateKeyFile pkf = null;
105: SshPrivateKey key;
106:
107: if ((keyfile == null) || !keyfile.exists()) {
108: JFileChooser chooser = new JFileChooser();
109: chooser.setFileHidingEnabled(false);
110: chooser
111: .setDialogTitle("Select Private Key File For Authentication");
112:
113: if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
114: keyfile = chooser.getSelectedFile();
115: } else {
116: return false;
117: }
118: }
119:
120: FileInputStream in = null;
121:
122: try {
123: pkf = SshPrivateKeyFile.parse(keyfile);
124: } catch (InvalidSshKeyException iske) {
125: JOptionPane.showMessageDialog(parent, iske.getMessage());
126:
127: return false;
128: } catch (IOException ioe) {
129: JOptionPane.showMessageDialog(parent, ioe.getMessage());
130: }
131:
132: // Now see if its passphrase protected
133: if (pkf.isPassphraseProtected()) {
134: // Show the passphrase dialog
135: Window w = (Window) SwingUtilities.getAncestorOfClass(
136: Window.class, parent);
137: PassphraseDialog dialog = null;
138:
139: if (w instanceof Frame) {
140: dialog = new PassphraseDialog((Frame) w);
141: } else if (w instanceof Dialog) {
142: dialog = new PassphraseDialog((Dialog) w);
143: } else {
144: dialog = new PassphraseDialog();
145: }
146:
147: do {
148: dialog.setVisible(true);
149:
150: if (dialog.isCancelled()) {
151: return false;
152: }
153:
154: passphrase = new String(dialog.getPassphrase());
155:
156: try {
157: key = pkf.toPrivateKey(passphrase);
158:
159: break;
160: } catch (InvalidSshKeyException ihke) {
161: dialog.setMessage("Passphrase Invalid! Try again");
162: dialog.setMessageForeground(Color.red);
163: }
164: } while (true);
165: } else {
166: try {
167: key = pkf.toPrivateKey(passphrase);
168: } catch (InvalidSshKeyException ihke) {
169: return false;
170: }
171: }
172:
173: instance.setKey(key);
174: instance.setKeyfile(keyfile.getAbsolutePath());
175:
176: return true;
177: }
178: }
|