001: package tide.export;
002:
003: import tide.editor.*;
004: import tide.project.*;
005: import snow.utils.*;
006: import snow.utils.gui.*;
007:
008: import javax.swing.*;
009: import javax.swing.event.*;
010: import java.io.*;
011: import java.awt.*;
012: import java.awt.event.*;
013: import java.util.*;
014:
015: /** generates a new keystore
016: */
017: public class KeystoreGenerator extends JDialog {
018: final private JTextField commonNameTF = new JTextField("", 12);
019: final private JTextField organizationUnitTF = new JTextField("", 12);
020: final private JTextField organizationNameTF = new JTextField("", 12);
021: final private JTextField localityNameTF = new JTextField("", 12);
022: final private JTextField stateNameTF = new JTextField("", 12);
023: final private JTextField countryTF = new JTextField("", 6);
024: final private JTextField emailAddressTF = new JTextField("", 18);
025:
026: final private JPasswordField passwordTF = new JPasswordField(12);
027:
028: // final private JTextField fileTF = new JTextField("", 12);
029: final private JTextField aliasTF = new JTextField("", 12);
030:
031: final private FileField keystoreLocationTF = new FileField("",
032: true, "Choose the keystore location",
033: JFileChooser.FILES_ONLY);
034:
035: public boolean wasSuccessfulyTerminated = false;
036:
037: public File getKeystoreLocation() {
038: return keystoreLocationTF.getPath();
039: }
040:
041: /**
042: * return the same password is used for the keystore and for the alias
043: */
044: public char[] getKeystorePassword() {
045: return passwordTF.getPassword();
046: }
047:
048: final private JButton goButton = new JButton("Generate Keys");
049:
050: /** generates a new keystore
051: *
052: */
053: public KeystoreGenerator(JDialog parent) {
054: super (parent, "Keystore Generator (leave blank to ignore)",
055: true);
056:
057: if (MainEditorFrame.instance == null)
058: return;
059: ProjectSettings actualProject = MainEditorFrame.instance
060: .getActualProject();
061:
062: String cn = actualProject.getProperty("x500.cn", System
063: .getProperty("user.name", "?"));
064: String alias = actualProject.getProperty("x500.alias", cn);
065: String ou = actualProject.getProperty("x500.ou", "");
066: String o = actualProject.getProperty("x500.o", "");
067: String l = actualProject.getProperty("x500.l", "");
068: String s = actualProject.getProperty("x500.s", "");
069: String c = actualProject.getProperty("x500.c", System
070: .getProperty("user.country", "CH"));
071:
072: File defLoc = new File(actualProject.getSources_Home()
073: .getParentFile(), "dev/keystore");
074: construct(actualProject.getKeyTool_TOOL(), alias,
075: new char[] {}, cn, ou, o, l, s, c, new File(
076: actualProject.getProperty(
077: "keystoreCreatedDest", "" + defLoc))
078: // model
079: );
080: }
081:
082: private void construct(final File keytoolpath, String alias,
083: char[] aliasPassword, String commonName,
084: String organizationUnit, String organizationName,
085: String localityName, String stateName, String country,
086: File defaultKeystoreFile) {
087: setLayout(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.setPath(defaultKeystoreFile
100: .getAbsolutePath());
101:
102: JPanel keyToolPanel = new JPanel(new FlowLayout(
103: FlowLayout.LEFT, 0, 0));
104: add(keyToolPanel);
105:
106: JPanel namePanel = new JPanel();
107: GridLayout3 gl3 = new GridLayout3(2, namePanel);
108: add(namePanel);
109: namePanel.setBorder(BorderFactory.createCompoundBorder(
110: BorderFactory.createTitledBorder("Name" + "[X.500]"),
111: BorderFactory.createEmptyBorder(2, 5, 2, 2)));
112: gl3.add(new JLabel("commonName [CN] "));
113: gl3.add(commonNameTF);
114: gl3.add(new JLabel("organizationUnit [OU]"));
115: gl3.add(organizationUnitTF);
116: gl3.add(new JLabel("organizationName [O]"));
117: gl3.add(organizationNameTF);
118: gl3.add(new JLabel("localityName [L] "));
119: gl3.add(localityNameTF);
120: gl3.add(new JLabel("stateName [S] "));
121: gl3.add(stateNameTF);
122: gl3.add("country [C] ");
123: gl3.add(countryTF);
124: gl3.add("E-Mail [Email] ");
125: gl3.add(emailAddressTF);
126:
127: JPanel aliasPanel = new JPanel();
128: ;
129: gl3 = new GridLayout3(2, aliasPanel);
130: add(aliasPanel);
131: aliasPanel.setBorder(BorderFactory.createTitledBorder("Alias"));
132: gl3.add(new JLabel("Alias "));
133: gl3.add(aliasTF);
134: gl3.add(new JLabel("Password"));
135: gl3.add(passwordTF);
136:
137: JPanel ksPanel = new JPanel();
138: gl3 = new GridLayout3(2, ksPanel);
139: add(ksPanel);
140: ksPanel.setBorder(BorderFactory.createTitledBorder("Keystore"));
141: gl3.add(new JLabel("Location "));
142: gl3.add(keystoreLocationTF, true);
143:
144: JPanel goPanel = new JPanel();
145: add(goPanel);
146: JButton cancelButton = new JButton("Cancel");
147: goPanel.add(cancelButton);
148: cancelButton.setFocusPainted(false);
149: cancelButton.setMargin(new Insets(0, 2, 0, 2));
150: cancelButton.addActionListener(new ActionListener() {
151: public void actionPerformed(ActionEvent e) {
152: setVisible(false);
153: }
154: });
155:
156: goPanel.add(goButton);
157: goButton.setFocusPainted(false);
158: goButton.setMargin(new Insets(0, 2, 0, 2));
159: goButton.addActionListener(new ActionListener() {
160: public void actionPerformed(ActionEvent e) {
161: String error = null;
162: if (passwordTF.getPassword().length < 6) {
163: error = "Password must be at least 6 chars length";
164: }
165:
166: File keyStoreFile = keystoreLocationTF.getPath();
167: if (keyStoreFile.exists()) {
168: error = "The keystorefile already exists, you can only generate new keystores here.\nPlease Choose a new keystore destination file or delete the actual file.";
169: }
170:
171: if (error != null) {
172: JOptionPane.showMessageDialog(getParent(),
173: "ERROR: " + error);
174: return;
175: }
176:
177: String cn = commonNameTF.getText();
178: String ou = organizationUnitTF.getText();
179: String o = organizationNameTF.getText();
180: String l = localityNameTF.getText();
181: String s = stateNameTF.getText();
182: String c = countryTF.getText();
183: String ma = emailAddressTF.getText();
184:
185: String[] arguments = new String[] {
186: keytoolpath.getAbsolutePath(),
187: "-J-Duser.language=en",
188: "-genkey",
189: "-v",
190: "-dname",
191: "cn="
192: + cn
193: + (ou.equals("") ? "" : ", ou=" + ou)
194: + (o.equals("") ? "" : ", o=" + o)
195: + (l.equals("") ? "" : ", l=" + l)
196: + (s.equals("") ? "" : ", s=" + s)
197: + (ma.equals("") ? "" : ", Email=" + ma)
198: + ", c=" + c + "", "-alias",
199: aliasTF.getText(), "-keypass",
200: new String(passwordTF.getPassword()),
201: "-keystore",
202: keystoreLocationTF.getPath().getAbsolutePath(),
203: "-storepass",
204: new String(passwordTF.getPassword()),
205: "-validity", "720" };
206:
207: try {
208: if (!keytoolpath.exists())
209: throw new Exception(
210: "The KeyTool tool path\n "
211: + keytoolpath
212: + "\nis not valid, please check your project settings");
213:
214: Process proc = Runtime.getRuntime().exec(arguments);
215: // Redirect both javadoc outputstreams to the OutputStream :
216: ByteArrayOutputStream bos = new ByteArrayOutputStream();
217: BufferedOutputStream buos = new BufferedOutputStream(
218: bos, 4000);
219: StreamGobbler outGobbler = new StreamGobbler(proc
220: .getInputStream(), buos); //System.out );
221: outGobbler.start();
222: StreamGobbler errGobbler = new StreamGobbler(proc
223: .getErrorStream(), buos); //System.out );
224: errGobbler.start();
225:
226: // Wait, until this thread has been terminated.
227: int exitValue = proc.waitFor();
228: if (exitValue != 0) {
229: buos.flush();
230: System.out
231: .println("Keytool returned With Error:\n Complete Stack:");
232: String stack = new String(bos.toByteArray());
233: System.out.println(stack);
234:
235: throw new Exception(
236: "Keytool bad termination, exit value= "
237: + exitValue + "\nStack:"
238: + stack);
239: } else {
240: wasSuccessfulyTerminated = true;
241: setVisible(false);
242: //dispose();
243:
244: // save values...
245: ProjectSettings actualProject = MainEditorFrame.instance
246: .getActualProject();
247: actualProject.setProperty("x500.cn", cn);
248: actualProject.setProperty("x500.ou", ou);
249: actualProject.setProperty("x500.o", o);
250: actualProject.setProperty("x500.l", l);
251: actualProject.setProperty("x500.s", s);
252: actualProject.setProperty("x500.c", c);
253: actualProject.setProperty("x500.alias", aliasTF
254: .getText());
255: }
256: } catch (Exception ee) {
257: JOptionPane.showMessageDialog(getParent(),
258: "ERROR: " + ee.getMessage());
259: }
260: }
261: });
262: }
263:
264: }
|