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.keygen;
027:
028: import com.sshtools.common.ui.NumericTextField;
029: import com.sshtools.common.ui.UIUtil;
030: import com.sshtools.common.ui.XTextField;
031:
032: import java.awt.BorderLayout;
033: import java.awt.Color;
034: import java.awt.GridBagConstraints;
035: import java.awt.GridBagLayout;
036: import java.awt.GridLayout;
037: import java.awt.Insets;
038: import java.awt.event.ActionEvent;
039: import java.awt.event.ActionListener;
040:
041: import java.io.File;
042:
043: import javax.swing.BorderFactory;
044: import javax.swing.JButton;
045: import javax.swing.JComboBox;
046: import javax.swing.JFileChooser;
047: import javax.swing.JLabel;
048: import javax.swing.JPanel;
049: import javax.swing.JPasswordField;
050: import javax.swing.JProgressBar;
051: import javax.swing.event.DocumentEvent;
052: import javax.swing.event.DocumentListener;
053:
054: /**
055: *
056: *
057: * @author $author$
058: * @version $Revision: 1.14 $
059: */
060: public class KeygenPanel extends JPanel implements DocumentListener,
061: ActionListener {
062: /** */
063: public final static int GENERATE_KEY_PAIR = 0;
064:
065: /** */
066: public final static int CONVERT_IETF_SECSH_TO_OPENSSH = 1;
067:
068: /** */
069: public final static int CONVERT_OPENSSH_TO_IETF_SECSH = 2;
070:
071: /** */
072: public final static int CHANGE_PASSPHRASE = 3;
073:
074: // Private instance variables
075: private JButton browseInput;
076:
077: // Private instance variables
078: private JButton browseOutput;
079: private JComboBox action;
080: private JComboBox type;
081: private JLabel bitsLabel;
082: private JLabel inputFileLabel;
083: private JLabel newPassphraseLabel;
084: private JLabel oldPassphraseLabel;
085: private JLabel outputFileLabel;
086: private JLabel typeLabel;
087: private JPasswordField newPassphrase;
088: private JPasswordField oldPassphrase;
089: private JProgressBar strength;
090: private XTextField inputFile;
091: private XTextField outputFile;
092: private NumericTextField bits;
093:
094: /**
095: * Creates a new KeygenPanel object.
096: */
097: public KeygenPanel() {
098: super ();
099:
100: JPanel keyPanel = new JPanel(new GridBagLayout());
101: keyPanel.setBorder(BorderFactory.createTitledBorder("Key"));
102:
103: // Create the main panel
104: GridBagConstraints gbc = new GridBagConstraints();
105: gbc.fill = GridBagConstraints.NONE;
106: gbc.anchor = GridBagConstraints.WEST;
107:
108: Insets normalInsets = new Insets(0, 2, 4, 2);
109: Insets indentedInsets = new Insets(0, 26, 4, 2);
110: gbc.insets = normalInsets;
111: gbc.weightx = 0.0;
112:
113: // Action
114: UIUtil.jGridBagAdd(keyPanel, new JLabel("Action"), gbc, 1);
115: gbc.weightx = 1.0;
116: action = new JComboBox(new String[] { "Generate key pair",
117: "Convert IETF SECSH to OpenSSH",
118: "Convert OpenSSH to IETF SECSH", "Change passphrase" });
119: action.addActionListener(this );
120: gbc.weightx = 2.0;
121: UIUtil.jGridBagAdd(keyPanel, action, gbc,
122: GridBagConstraints.RELATIVE);
123: gbc.weightx = 0.0;
124: UIUtil.jGridBagAdd(keyPanel, new JLabel(), gbc,
125: GridBagConstraints.REMAINDER);
126: gbc.insets = indentedInsets;
127:
128: // File
129: inputFileLabel = new JLabel("Input File");
130: UIUtil.jGridBagAdd(keyPanel, inputFileLabel, gbc, 1);
131: gbc.insets = normalInsets;
132: gbc.weightx = 1.0;
133: inputFile = new XTextField(20);
134: UIUtil.jGridBagAdd(keyPanel, inputFile, gbc,
135: GridBagConstraints.RELATIVE);
136: inputFileLabel.setLabelFor(inputFile);
137: gbc.weightx = 0.0;
138: browseInput = new JButton("Browse");
139: browseInput.setMnemonic('b');
140: browseInput.addActionListener(this );
141: UIUtil.jGridBagAdd(keyPanel, browseInput, gbc,
142: GridBagConstraints.REMAINDER);
143:
144: // File
145: gbc.insets = indentedInsets;
146: outputFileLabel = new JLabel("Output File");
147: UIUtil.jGridBagAdd(keyPanel, outputFileLabel, gbc, 1);
148: gbc.insets = normalInsets;
149: gbc.weightx = 1.0;
150: outputFile = new XTextField(20);
151: UIUtil.jGridBagAdd(keyPanel, outputFile, gbc,
152: GridBagConstraints.RELATIVE);
153: gbc.weightx = 0.0;
154: outputFileLabel.setLabelFor(outputFile);
155: browseOutput = new JButton("Browse");
156: browseOutput.setMnemonic('r');
157: browseOutput.addActionListener(this );
158: UIUtil.jGridBagAdd(keyPanel, browseOutput, gbc,
159: GridBagConstraints.REMAINDER);
160:
161: // Old Passphrase
162: gbc.insets = indentedInsets;
163: oldPassphraseLabel = new JLabel("Old Passphrase");
164: UIUtil.jGridBagAdd(keyPanel, oldPassphraseLabel, gbc, 1);
165: gbc.insets = normalInsets;
166: gbc.weightx = 2.0;
167: oldPassphrase = new JPasswordField(20);
168: oldPassphrase.setBackground(Color.white);
169: oldPassphrase.getDocument().addDocumentListener(this );
170: oldPassphraseLabel.setLabelFor(oldPassphrase);
171: UIUtil.jGridBagAdd(keyPanel, oldPassphrase, gbc,
172: GridBagConstraints.RELATIVE);
173: UIUtil.jGridBagAdd(keyPanel, new JLabel(), gbc,
174: GridBagConstraints.REMAINDER);
175:
176: // Passphrase
177: gbc.insets = indentedInsets;
178: newPassphraseLabel = new JLabel("New Passphrase");
179: UIUtil.jGridBagAdd(keyPanel, newPassphraseLabel, gbc, 1);
180: gbc.insets = normalInsets;
181: gbc.weightx = 2.0;
182: newPassphrase = new JPasswordField(20);
183: newPassphrase.setBackground(Color.white);
184: newPassphrase.getDocument().addDocumentListener(this );
185: newPassphraseLabel.setLabelFor(newPassphrase);
186: UIUtil.jGridBagAdd(keyPanel, newPassphrase, gbc,
187: GridBagConstraints.RELATIVE);
188: UIUtil.jGridBagAdd(keyPanel, new JLabel(), gbc,
189: GridBagConstraints.REMAINDER);
190:
191: // Bits
192: gbc.insets = indentedInsets;
193: bitsLabel = new JLabel("Bits");
194: UIUtil.jGridBagAdd(keyPanel, bitsLabel, gbc, 1);
195: gbc.weightx = 2.0;
196: gbc.insets = normalInsets;
197: bits = new NumericTextField(new Integer(512),
198: new Integer(1024), new Integer(1024));
199: bitsLabel.setLabelFor(bits);
200: UIUtil.jGridBagAdd(keyPanel, bits, gbc,
201: GridBagConstraints.RELATIVE);
202: UIUtil.jGridBagAdd(keyPanel, new JLabel(), gbc,
203: GridBagConstraints.REMAINDER);
204:
205: // Type
206: gbc.insets = indentedInsets;
207: typeLabel = new JLabel("Type");
208: UIUtil.jGridBagAdd(keyPanel, typeLabel, gbc, 1);
209: gbc.insets = normalInsets;
210: gbc.weightx = 2.0;
211: type = new JComboBox(new String[] { "DSA", "RSA" });
212: type.setFont(inputFile.getFont());
213:
214: // Combo boxes look crap in metal
215: UIUtil.jGridBagAdd(keyPanel, type, gbc,
216: GridBagConstraints.RELATIVE);
217: UIUtil.jGridBagAdd(keyPanel, new JLabel(), gbc,
218: GridBagConstraints.REMAINDER);
219: strength = new JProgressBar(0, 40);
220: strength.setStringPainted(true);
221:
222: JPanel strengthPanel = new JPanel(new GridLayout(1, 1));
223: strengthPanel.setBorder(BorderFactory
224: .createCompoundBorder(BorderFactory
225: .createTitledBorder("Passphrase strength"),
226: BorderFactory.createEmptyBorder(4, 4, 4, 4)));
227: strengthPanel.add(strength);
228:
229: // Build this panel
230: setLayout(new BorderLayout());
231: add(keyPanel, BorderLayout.CENTER);
232: add(strengthPanel, BorderLayout.SOUTH);
233: calculateStrength();
234: setAvailableActions();
235: }
236:
237: /**
238: *
239: *
240: * @return
241: */
242: public int getAction() {
243: return action.getSelectedIndex();
244: }
245:
246: /**
247: *
248: *
249: * @return
250: */
251: public int getBits() {
252: return ((Integer) bits.getValue()).intValue();
253: }
254:
255: /**
256: *
257: *
258: * @return
259: */
260: public String getInputFilename() {
261: return inputFile.getText();
262: }
263:
264: /**
265: *
266: *
267: * @return
268: */
269: public char[] getNewPassphrase() {
270: return newPassphrase.getPassword();
271: }
272:
273: /**
274: *
275: *
276: * @return
277: */
278: public char[] getOldPassphrase() {
279: return oldPassphrase.getPassword();
280: }
281:
282: /**
283: *
284: *
285: * @return
286: */
287: public String getOutputFilename() {
288: return outputFile.getText();
289: }
290:
291: /**
292: *
293: *
294: * @return
295: */
296: public String getType() {
297: return (String) type.getSelectedItem();
298: }
299:
300: /**
301: *
302: *
303: * @param evt
304: */
305: public void actionPerformed(ActionEvent evt) {
306: if (evt.getSource() == browseOutput) {
307: File f = new File(outputFile.getText());
308: JFileChooser chooser = new JFileChooser(f);
309: chooser.setSelectedFile(f);
310: chooser.setDialogTitle("Choose output file ..");
311: chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
312:
313: if (chooser.showSaveDialog(this ) == JFileChooser.APPROVE_OPTION) {
314: outputFile.setText(chooser.getSelectedFile().getPath());
315: }
316: } else if (evt.getSource() == browseInput) {
317: File f = new File(inputFile.getText());
318: JFileChooser chooser = new JFileChooser(f);
319: chooser.setSelectedFile(f);
320: chooser.setDialogTitle("Choose input file ..");
321: chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
322:
323: if (chooser.showOpenDialog(this ) == JFileChooser.APPROVE_OPTION) {
324: inputFile.setText(chooser.getSelectedFile().getPath());
325: }
326: } else {
327: setAvailableActions();
328: }
329: }
330:
331: /**
332: *
333: *
334: * @param e
335: */
336: public void changedUpdate(DocumentEvent e) {
337: calculateStrength();
338: }
339:
340: /**
341: *
342: *
343: * @param e
344: */
345: public void insertUpdate(DocumentEvent e) {
346: calculateStrength();
347: }
348:
349: /**
350: *
351: *
352: * @param e
353: */
354: public void removeUpdate(DocumentEvent e) {
355: calculateStrength();
356: }
357:
358: private void setAvailableActions() {
359: inputFile
360: .setEnabled((getAction() == CONVERT_IETF_SECSH_TO_OPENSSH)
361: || (getAction() == CONVERT_OPENSSH_TO_IETF_SECSH)
362: || (getAction() == CHANGE_PASSPHRASE));
363: inputFileLabel.setEnabled(inputFile.isEnabled());
364: browseInput.setEnabled(inputFile.isEnabled());
365: bits.setEnabled(getAction() == GENERATE_KEY_PAIR);
366: bitsLabel.setEnabled(bits.isEnabled());
367: outputFile
368: .setEnabled((getAction() == CONVERT_IETF_SECSH_TO_OPENSSH)
369: || (getAction() == CONVERT_OPENSSH_TO_IETF_SECSH)
370: || (getAction() == GENERATE_KEY_PAIR));
371: outputFileLabel.setEnabled(outputFile.isEnabled());
372: browseOutput.setEnabled(outputFile.isEnabled());
373: newPassphrase.setEnabled((getAction() == GENERATE_KEY_PAIR)
374: || (getAction() == CHANGE_PASSPHRASE));
375: newPassphraseLabel.setEnabled(newPassphrase.isEnabled());
376: oldPassphrase.setEnabled(getAction() == CHANGE_PASSPHRASE);
377: oldPassphraseLabel.setEnabled(oldPassphrase.isEnabled());
378: type.setEnabled(getAction() == GENERATE_KEY_PAIR);
379: typeLabel.setEnabled(type.isEnabled());
380:
381: if (inputFile.isEnabled()) {
382: inputFile.requestFocus();
383: } else {
384: outputFile.requestFocus();
385: }
386: }
387:
388: private void calculateStrength() {
389: char[] pw = newPassphrase.getPassword();
390: strength.setValue((pw.length < 40) ? pw.length : 40);
391:
392: Color f;
393: String t;
394:
395: if (pw.length == 0) {
396: f = Color.red;
397: t = "Empty!!";
398: } else {
399: StringBuffer buf = new StringBuffer();
400: buf.append(pw.length);
401: buf.append(" characters - ");
402:
403: if (pw.length < 10) {
404: f = Color.red;
405: buf.append("Weak!");
406: } else if (pw.length < 20) {
407: f = Color.orange;
408: buf.append("Ok");
409: } else if (pw.length < 30) {
410: f = Color.green.darker();
411: buf.append("Strong");
412: } else {
413: f = Color.green;
414: buf.append("Very strong!");
415: }
416:
417: t = buf.toString();
418: }
419:
420: strength.setString(t);
421: strength.setForeground(f);
422: }
423: }
|