001: package Schmortopf.Main.OutputComponents.JarCreation;
002:
003: import sun.security.tools.KeyTool;
004: import javax.swing.*;
005: import javax.swing.event.*;
006: import java.io.*;
007: import java.awt.*;
008: import java.awt.event.*;
009: import java.util.*;
010:
011: import Schmortopf.Utility.gui.*;
012: import Schmortopf.Utility.*;
013: import Schmortopf.Main.IDE_ProjectFrameProvider;
014: import Schmortopf.Main.ProjectDefinition.ProjectDefinition; // Contains all pathes the IDE knows.
015: import Schmortopf.Main.ProjectDefinition.ProjectDefinitionEntry;
016: import Schmortopf.OutputManager.StreamGobbler;
017: import Language.Language;
018: import Shared.Logging.Log;
019:
020: public class KeystoreGenerator extends JDialog {
021: JTextField keytoolLocTF = new JTextField("", 37);
022: JTextField commonNameTF = new JTextField("", 12);
023: JTextField organizationUnitTF = new JTextField("", 12);
024: JTextField organizationNameTF = new JTextField("", 12);
025: JTextField localityNameTF = new JTextField("", 12);
026: JTextField stateNameTF = new JTextField("", 12);
027: JTextField countryTF = new JTextField("", 12);
028:
029: JPasswordField passwordTF = new JPasswordField(16);
030:
031: JTextField fileTF = new JTextField("", 12);
032: JTextField aliasTF = new JTextField("", 12);
033:
034: JTextField keystoreLocationTF = new JTextField("", 12);
035:
036: public String getKeystoreLocation() {
037: return keystoreLocationTF.getText();
038: }
039:
040: /**
041: * return the same password is used for the keystore and for the alias
042: */
043: public char[] getKeystorePassword() {
044: return passwordTF.getPassword();
045: }
046:
047: JSenseButton goButton = new JSenseButton(Language
048: .Translate("Generate Keys"), true, true, null);
049:
050: /**
051: *
052: */
053: public KeystoreGenerator(final JarCreationManager model) {
054: super (model.getProjectFrameProvider()
055: .getParentFrameForChildren(), Language
056: .Translate("Keystore Generator"), true);
057: ProjectDefinitionEntry pde = model.getProjectDefinition()
058: .getProjectDefinitionEntry(
059: ProjectDefinition.KeyTool_EXEpath_Key);
060: String keyToolExePath = (String) pde.getValue();
061:
062: String cn = model.getIniFile().getStringValue("x500.cn",
063: System.getProperty("user.name", "?"));
064: String alias = model.getIniFile().getStringValue("x500.alias",
065: cn);
066: String ou = model.getIniFile().getStringValue("x500.ou", "");
067: String o = model.getIniFile().getStringValue("x500.o", "");
068: String l = model.getIniFile().getStringValue("x500.l", "");
069: String s = model.getIniFile().getStringValue("x500.s", "");
070: String c = model.getIniFile().getStringValue("x500.c",
071: System.getProperty("user.country", "CH"));
072:
073: construct(keyToolExePath, alias, new char[] {},
074: //username,
075: cn, ou, o, l, s, c,
076: //userCountry,
077: new File(model.getIniFile().getStringValue(
078: "keystoreCreatedDest", "c:\\keystore")), model);
079: }
080:
081: private void construct(final String keytoolpath, String alias,
082: char[] aliasPassword, String commonName,
083: String organizationUnit, String organizationName,
084: String localityName, String stateName, String country,
085: File file, final JarCreationManager model) {
086: getContentPane().setLayout(
087: new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
088:
089: aliasTF.setText(alias);
090: passwordTF.setText(new String(aliasPassword));
091:
092: commonNameTF.setText(commonName);
093: organizationUnitTF.setText(organizationUnit);
094: organizationNameTF.setText(organizationName);
095: localityNameTF.setText(localityName);
096: stateNameTF.setText(stateName);
097: countryTF.setText(country);
098:
099: keystoreLocationTF.setText(file.getAbsolutePath());
100: //keystorePasswordTF.setText(new String(filePass));
101:
102: JPanel keyToolPanel = new JPanel(new FlowLayout(
103: FlowLayout.LEFT, 0, 0));
104: keyToolPanel.setBorder(BorderFactory
105: .createTitledBorder(Language
106: .Translate("Keytool location")));
107: keyToolPanel.add(keytoolLocTF);
108: JSenseButton browseBT = new JSenseButton("...", true, true,
109: model.getProjectFrameProvider().getMainFrameProvider());
110: browseBT.setPreferredSize(new Dimension((int) keytoolLocTF
111: .getPreferredSize().getHeight(), (int) keytoolLocTF
112: .getPreferredSize().getHeight()));
113:
114: browseBT.addActionListener(new ActionListener() {
115: public void actionPerformed(ActionEvent ee) {
116: FileChooser chooser = new FileChooser(model
117: .getProjectFrameProvider()
118: .getParentFrameForChildren());
119: String file = chooser.chooseFile(
120: keytoolLocTF.getText(), new String[] {},
121: Language.Translate("KeyTool Location"), true,
122: true, JFileChooser.FILES_ONLY);
123:
124: if (file != null && !file.equals("")) {
125: keytoolLocTF.setText(file);
126: }
127: }
128: });
129:
130: keyToolPanel.add(browseBT);
131:
132: keytoolLocTF.setText(keytoolpath);
133: getContentPane().add(keyToolPanel);
134:
135: JPanel namePanel = new JPanel(new GridLayout2(6, 2, 15, 5));
136: getContentPane().add(namePanel);
137: namePanel.setBorder(BorderFactory.createTitledBorder(Language
138: .Translate("Name")
139: + "[X.500]"));
140: namePanel.add(new JLabel(Language.Translate("commonName")
141: + " [CN] "));
142: namePanel.add(commonNameTF);
143: namePanel.add(new JLabel(Language.Translate("organizationUnit")
144: + " [OU]"));
145: namePanel.add(organizationUnitTF);
146: namePanel.add(new JLabel(Language.Translate("organizationName")
147: + " [O]"));
148: namePanel.add(organizationNameTF);
149: namePanel.add(new JLabel(Language.Translate("localityName")
150: + " [L] "));
151: namePanel.add(localityNameTF);
152: namePanel.add(new JLabel(Language.Translate("stateName")
153: + " [S] "));
154: namePanel.add(stateNameTF);
155: namePanel.add(new JLabel(Language.Translate("country")
156: + " [C] "));
157: namePanel.add(countryTF);
158:
159: JPanel aliasPanel = new JPanel(new GridLayout2(2, 2, 15, 5));
160: getContentPane().add(aliasPanel);
161: aliasPanel.setBorder(BorderFactory.createTitledBorder(Language
162: .Translate("Alias")));
163: aliasPanel.add(new JLabel(Language.Translate("Alias") + " "));
164: aliasPanel.add(aliasTF);
165: aliasPanel.add(new JLabel(Language
166: .Translate("Password (At least 6 chars)")));
167: aliasPanel.add(passwordTF);
168:
169: JPanel ksPanel = new JPanel(new GridLayout2(1, 2, 15, 5));
170: getContentPane().add(ksPanel);
171: ksPanel.setBorder(BorderFactory.createTitledBorder(Language
172: .Translate("Keystore")));
173: ksPanel.add(new JLabel(Language.Translate("Location") + " "));
174: ksPanel.add(keystoreLocationTF);
175: /* ksPanel.add(new JLabel("Password "));
176: ksPanel.add(keystorePasswordTF);*/
177:
178: JPanel goPanel = new JPanel();
179: getContentPane().add(goPanel);
180: JSenseButton cancelButton = new JSenseButton(Language
181: .Translate("Cancel"), true, true, null);
182: goPanel.add(cancelButton);
183: cancelButton.addActionListener(new ActionListener() {
184: public void actionPerformed(ActionEvent e) {
185: setVisible(false);
186: }
187: });
188:
189: goPanel.add(goButton);
190: goButton.addActionListener(new ActionListener() {
191: public void actionPerformed(ActionEvent e) {
192: String error = null;
193: if (passwordTF.getPassword().length < 6) {
194: error = Language
195: .Translate("Password must be at least 6 chars length");
196: }
197: /* else if(keystorePasswordTF.getPassword ().length<6)
198: {
199: error = "Password must be at least 6 chars length";
200: }*/
201: File keyStoreFile = new File(keystoreLocationTF
202: .getText());
203: if (keyStoreFile.exists()) {
204: error = Language
205: .Translate("The keystorefile already exists, you can only generate new keystores here.\nPlease Choose a new keystore destination file or delete the actual file.");
206: }
207:
208: if (error != null) {
209: JOptionPane.showMessageDialog(model
210: .getProjectFrameProvider()
211: .getParentFrameForChildren(), Language
212: .Translate("ERROR:")
213: + " " + error);
214: return;
215: }
216:
217: /*String SEPARATOR = System.getProperty("file.separator");
218: String binPath = System.getProperty("java.home")+SEPARATOR+"bin"+SEPARATOR+"javaw";
219: String userDir = System.getProperty("user.dir");
220: if (!userDir.endsWith(SEPARATOR)) userDir += SEPARATOR;
221: File f = new File(binPath);
222: String JAVA_COMMAND = (f.exists()) ? "javaw" : "java";
223: //String[] args = new String[]{JAVA_COMMAND, "-jar", userDir+"/lib/JavaMail.jar", netControl.getUserName()};
224: */
225:
226: String cn = commonNameTF.getText();
227: String ou = organizationUnitTF.getText();
228: String o = organizationNameTF.getText();
229: String l = localityNameTF.getText();
230: String s = stateNameTF.getText();
231: String c = countryTF.getText();
232:
233: String[] arguments = new String[] {
234: keytoolLocTF.getText(),
235: "-genkey",
236: "-v",
237: "-dname",
238: "cn=" + cn
239: + (ou.equals("") ? "" : ", ou=" + ou)
240: + (o.equals("") ? "" : ", o=" + o)
241: + (l.equals("") ? "" : ", l=" + l)
242: + (s.equals("") ? "" : ", s=" + s)
243: + ", c=" + c + "", "-alias",
244: aliasTF.getText(), "-keypass",
245: new String(passwordTF.getPassword()),
246: "-keystore", keystoreLocationTF.getText(),
247: "-storepass",
248: new String(passwordTF.getPassword()),
249: "-validity", "720" };
250:
251: try {
252: if (!(new File(keytoolLocTF.getText()).exists()))
253: throw new Exception(
254: "The KeyTool tool path\n "
255: + keytoolLocTF.getText()
256: + "\nis not valid, please check your project settings");
257:
258: Process proc = Runtime.getRuntime().exec(arguments);
259:
260: // Redirect both javadoc outputstreams to the OutputStream :
261: ByteArrayOutputStream bos = new ByteArrayOutputStream();
262: BufferedOutputStream buos = new BufferedOutputStream(
263: bos, 4000);
264: StreamGobbler outGobbler = new StreamGobbler(proc
265: .getInputStream(), buos, false);
266: outGobbler.start();
267: StreamGobbler errGobbler = new StreamGobbler(proc
268: .getErrorStream(), buos, false);
269: errGobbler.start();
270:
271: // Wait, until this thread has been terminated.
272: int exitValue = proc.waitFor();
273: if (exitValue != 0) {
274: buos.flush();
275: Log
276: .Warn("JarSigner Returned With Error:\n Complete Stack:");
277: String stack = new String(bos.toByteArray());
278: Log.Warn(stack);
279:
280: throw new Exception(
281: Language
282: .Translate(
283: "Keytool bad termination, exit value= %",
284: "" + exitValue)
285: + Language
286: .Translate("\nStack:")
287: + stack);
288: } else {
289: wasSuccessfulyTerminated = true;
290: setVisible(false);
291: //dispose();
292:
293: // save values...
294: //model.getIniFile().setStringValue("username", );
295: model.getIniFile()
296: .setStringValue("x500.cn", cn);
297: model.getIniFile()
298: .setStringValue("x500.ou", ou);
299: model.getIniFile().setStringValue("x500.o", o);
300: model.getIniFile().setStringValue("x500.l", l);
301: model.getIniFile().setStringValue("x500.s", s);
302: model.getIniFile().setStringValue("x500.c", c);
303: model.getIniFile().setStringValue("x500.alias",
304: aliasTF.getText());
305: }
306: } catch (Exception ee) {
307: JOptionPane.showMessageDialog(model
308: .getProjectFrameProvider()
309: .getParentFrameForChildren(), "ERROR: "
310: + ee.getMessage());
311: }
312: }
313: });
314: }
315:
316: public boolean wasSuccessfulyTerminated = false;
317:
318: }
|