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.IconWrapperPanel;
029: import com.sshtools.common.ui.ResourceIcon;
030: import com.sshtools.common.ui.UIUtil;
031:
032: import com.sshtools.j2ssh.configuration.ConfigurationException;
033: import com.sshtools.j2ssh.configuration.ConfigurationLoader;
034: import com.sshtools.j2ssh.transport.publickey.OpenSSHPublicKeyFormat;
035: import com.sshtools.j2ssh.transport.publickey.SECSHPublicKeyFormat;
036: import com.sshtools.j2ssh.transport.publickey.SshKeyGenerator;
037:
038: import java.awt.BorderLayout;
039: import java.awt.FlowLayout;
040: import java.awt.GridBagConstraints;
041: import java.awt.GridBagLayout;
042: import java.awt.GridLayout;
043: import java.awt.Insets;
044: import java.awt.event.ActionEvent;
045: import java.awt.event.ActionListener;
046: import java.awt.event.WindowAdapter;
047: import java.awt.event.WindowEvent;
048:
049: import java.io.File;
050: import java.io.FileOutputStream;
051: import java.io.IOException;
052: import java.io.PrintWriter;
053:
054: import javax.swing.BorderFactory;
055: import javax.swing.JButton;
056: import javax.swing.JFrame;
057: import javax.swing.JOptionPane;
058: import javax.swing.JPanel;
059: import javax.swing.ProgressMonitor;
060: import javax.swing.SwingConstants;
061: import javax.swing.UIManager;
062:
063: /**
064: *
065: *
066: * @author $author$
067: * @version $Revision: 1.15 $
068: */
069: public class Main extends JFrame implements ActionListener {
070: // Statics
071: final static String ICON = "/com/sshtools/common/authentication/largepassphrase.png";
072: JButton close;
073: JButton generate;
074: KeygenPanel keygen;
075:
076: /**
077: * Creates a new Main object.
078: */
079: public Main() {
080: super ("ssh-keygen");
081:
082: /* try {
083: ConfigurationLoader.setLogfile(ConfigurationLoader.getHomeDirectory()
084: + "logs/ssh-keygen.log");
085: } catch (IOException ex) {
086: }*/
087: try {
088: ConfigurationLoader.initialize(false);
089: } catch (ConfigurationException ex) {
090: }
091:
092: // Set the frame icon
093: setIconImage(new ResourceIcon(ICON).getImage());
094:
095: //
096: keygen = new KeygenPanel();
097:
098: // Create the center banner panel
099: IconWrapperPanel centerPanel = new IconWrapperPanel(
100: new ResourceIcon(ICON), keygen);
101: centerPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4,
102: 4));
103:
104: // Button panel
105: JPanel buttonPanel = new JPanel(new GridBagLayout());
106: GridBagConstraints gbc = new GridBagConstraints();
107: gbc.fill = GridBagConstraints.HORIZONTAL;
108: gbc.anchor = GridBagConstraints.CENTER;
109: gbc.insets = new Insets(6, 6, 0, 0);
110: gbc.weighty = 1.0;
111: generate = new JButton("Generate");
112: generate.addActionListener(this );
113: generate.setMnemonic('g');
114: this .getRootPane().setDefaultButton(generate);
115: UIUtil.jGridBagAdd(buttonPanel, generate, gbc,
116: GridBagConstraints.RELATIVE);
117: close = new JButton("Close");
118: close.addActionListener(this );
119: close.setMnemonic('c');
120: UIUtil.jGridBagAdd(buttonPanel, close, gbc,
121: GridBagConstraints.REMAINDER);
122:
123: JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT,
124: 0, 0));
125: southPanel.add(buttonPanel);
126:
127: // Wrap the whole thing in an empty border
128: JPanel mainPanel = new JPanel(new BorderLayout());
129: mainPanel
130: .setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
131: mainPanel.add(centerPanel, BorderLayout.CENTER);
132: mainPanel.add(southPanel, BorderLayout.SOUTH);
133:
134: // Build the main panel
135: getContentPane().setLayout(new GridLayout(1, 1));
136: getContentPane().add(mainPanel);
137: }
138:
139: /**
140: *
141: *
142: * @param evt
143: */
144: public void actionPerformed(ActionEvent evt) {
145: // Close
146: if (evt.getSource() == close) {
147: dispose();
148:
149: return;
150: }
151:
152: final String newPassphrase = new String(keygen
153: .getNewPassphrase()).trim();
154: final String oldPassphrase = new String(keygen
155: .getOldPassphrase()).trim();
156:
157: if ((keygen.getAction() == KeygenPanel.GENERATE_KEY_PAIR)
158: || (keygen.getAction() == KeygenPanel.CHANGE_PASSPHRASE)) {
159: if (newPassphrase.length() == 0) {
160: if (JOptionPane.showConfirmDialog(this ,
161: "Passphrase is empty. Are you sure?",
162: "Empty Passphrase", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
163: return;
164: }
165: }
166: }
167:
168: final File inputFile = new File(keygen.getInputFilename());
169: final File outputFile = new File(keygen.getOutputFilename());
170: final File publicFile = new File(keygen.getOutputFilename()
171: + ".pub");
172:
173: // Check if the output file was supplied
174: if ((keygen.getAction() == KeygenPanel.CONVERT_IETF_SECSH_TO_OPENSSH)
175: || (keygen.getAction() == KeygenPanel.CONVERT_OPENSSH_TO_IETF_SECSH)
176: || (keygen.getAction() == KeygenPanel.GENERATE_KEY_PAIR)) {
177: if (keygen.getOutputFilename().length() == 0) {
178: JOptionPane.showMessageDialog(this ,
179: "No Output file supplied.", "Error",
180: JOptionPane.ERROR_MESSAGE);
181:
182: return;
183: }
184:
185: // Check if the output file exists, and confirm overwrit if it does
186: if (outputFile.exists()) {
187: if (JOptionPane.showConfirmDialog(this , "Output file "
188: + outputFile.getName()
189: + " exists. Are you sure?", "File exists",
190: JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
191: return;
192: }
193: }
194:
195: // Make sure the output file is writeable
196: if (outputFile.exists() && !outputFile.canWrite()) {
197: JOptionPane.showMessageDialog(this ,
198: "Output file " + outputFile.getName()
199: + " can not be written.",
200: "Unwriteable file", JOptionPane.ERROR_MESSAGE);
201:
202: return;
203: }
204: }
205:
206: // If this is a conversion, check the input file is provided
207: if ((keygen.getAction() == KeygenPanel.CONVERT_IETF_SECSH_TO_OPENSSH)
208: || (keygen.getAction() == KeygenPanel.CONVERT_OPENSSH_TO_IETF_SECSH)) {
209: if (keygen.getInputFilename().length() == 0) {
210: JOptionPane.showMessageDialog(this ,
211: "No Input file supplied.", "Error",
212: JOptionPane.ERROR_MESSAGE);
213:
214: return;
215: }
216: } else if (keygen.getAction() == KeygenPanel.GENERATE_KEY_PAIR) {
217: // Check if the public key file is writeable. We should test if it exists
218: // as thats just too many questions for the user
219: if (publicFile.exists() && !publicFile.canWrite()) {
220: JOptionPane.showMessageDialog(this ,
221: "Public key file " + publicFile.getName()
222: + " can not be written.",
223: "Unwriteable file", JOptionPane.ERROR_MESSAGE);
224:
225: return;
226: }
227: }
228:
229: // Now generate the key
230: final ProgressMonitor monitor = new ProgressMonitor(this ,
231: "Generating keys", "Generating", 0, 100);
232: monitor.setMillisToDecideToPopup(0);
233: monitor.setMillisToPopup(0);
234:
235: Runnable r = new Runnable() {
236: public void run() {
237: try {
238: if (keygen.getAction() == KeygenPanel.CHANGE_PASSPHRASE) {
239: monitor.setNote("Changing passphrase");
240: SshKeyGenerator.changePassphrase(inputFile,
241: oldPassphrase, newPassphrase);
242: monitor.setNote("Complete");
243: JOptionPane.showMessageDialog(Main.this ,
244: "Passphrase changed",
245: "Passphrase changed",
246: JOptionPane.INFORMATION_MESSAGE);
247: } else if (keygen.getAction() == KeygenPanel.CONVERT_IETF_SECSH_TO_OPENSSH) {
248: monitor.setNote("Converting key file");
249: writeString(outputFile, SshKeyGenerator
250: .convertPublicKeyFile(inputFile,
251: new OpenSSHPublicKeyFormat()));
252: monitor.setNote("Complete");
253: JOptionPane.showMessageDialog(Main.this ,
254: "Key converted", "Key converted",
255: JOptionPane.INFORMATION_MESSAGE);
256: } else if (keygen.getAction() == KeygenPanel.CONVERT_OPENSSH_TO_IETF_SECSH) {
257: monitor.setNote("Converting key file");
258: writeString(outputFile, SshKeyGenerator
259: .convertPublicKeyFile(inputFile,
260: new SECSHPublicKeyFormat()));
261: monitor.setNote("Complete");
262: JOptionPane.showMessageDialog(Main.this ,
263: "Key converted", "Key converted",
264: JOptionPane.INFORMATION_MESSAGE);
265: } else {
266: monitor.setNote("Creating generator");
267:
268: SshKeyGenerator generator = new SshKeyGenerator();
269: monitor.setNote("Generating");
270:
271: String username = System
272: .getProperty("user.name");
273: generator.generateKeyPair(keygen.getType(),
274: keygen.getBits(), outputFile
275: .getAbsolutePath(), username,
276: newPassphrase);
277: monitor.setNote("Complete");
278: JOptionPane.showMessageDialog(Main.this ,
279: "Key generated to "
280: + outputFile.getName(),
281: "Complete",
282: JOptionPane.INFORMATION_MESSAGE);
283: }
284: } catch (Exception e) {
285: JOptionPane.showMessageDialog(Main.this , e
286: .getMessage(), "Error",
287: JOptionPane.ERROR_MESSAGE);
288: } finally {
289: monitor.close();
290: }
291: }
292: };
293:
294: Thread t = new Thread(r);
295: t.start();
296: }
297:
298: /**
299: *
300: *
301: * @param args
302: */
303: public static void main(String[] args) {
304: try {
305: UIManager.setLookAndFeel(UIManager
306: .getSystemLookAndFeelClassName());
307: } catch (Exception e) {
308: }
309:
310: Main main = new Main();
311: main.addWindowListener(new WindowAdapter() {
312: public void windowClosing(WindowEvent evt) {
313: System.exit(0);
314: }
315: });
316: main.pack();
317: UIUtil.positionComponent(SwingConstants.CENTER, main);
318: main.setVisible(true);
319: }
320:
321: private void writeString(File file, String string)
322: throws IOException {
323: FileOutputStream out = null;
324:
325: try {
326: out = new FileOutputStream(file);
327:
328: PrintWriter w = new PrintWriter(out, true);
329: w.println(string);
330: } finally {
331: if (out != null) {
332: out.close();
333: }
334: }
335: }
336: }
|