This project has retired. For details please refer to its
Attic page.
XmlSignatureApplet xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.juddi.gui.dsig;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.StringReader;
21 import java.io.StringWriter;
22 import java.security.InvalidAlgorithmParameterException;
23 import java.security.Key;
24 import java.security.KeyStore;
25 import java.security.NoSuchAlgorithmException;
26 import java.security.PrivateKey;
27 import java.security.cert.Certificate;
28 import java.security.cert.X509Certificate;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.Enumeration;
32 import java.util.List;
33 import java.util.Vector;
34 import java.util.logging.Level;
35 import java.util.logging.Logger;
36
37 import javax.swing.JOptionPane;
38 import javax.xml.bind.JAXB;
39 import javax.xml.crypto.dsig.CanonicalizationMethod;
40 import javax.xml.crypto.dsig.DigestMethod;
41 import javax.xml.crypto.dsig.Reference;
42 import javax.xml.crypto.dsig.SignatureMethod;
43 import javax.xml.crypto.dsig.SignedInfo;
44 import javax.xml.crypto.dsig.Transform;
45 import javax.xml.crypto.dsig.XMLSignature;
46 import javax.xml.crypto.dsig.XMLSignatureFactory;
47 import javax.xml.crypto.dsig.dom.DOMSignContext;
48 import javax.xml.crypto.dsig.keyinfo.KeyInfo;
49 import javax.xml.crypto.dsig.keyinfo.KeyInfoFactory;
50 import javax.xml.crypto.dsig.keyinfo.X509Data;
51 import javax.xml.crypto.dsig.spec.C14NMethodParameterSpec;
52 import javax.xml.crypto.dsig.spec.TransformParameterSpec;
53 import javax.xml.parsers.DocumentBuilder;
54 import javax.xml.parsers.DocumentBuilderFactory;
55 import javax.xml.parsers.ParserConfigurationException;
56 import netscape.javascript.JSObject;
57
58
59
60 import org.apache.juddi.v3.client.cryptor.DigSigUtil;
61 import org.apache.juddi.v3.client.cryptor.XmlUtils;
62 import org.uddi.api_v3.BindingTemplate;
63 import org.uddi.api_v3.BusinessEntity;
64 import org.uddi.api_v3.BusinessService;
65 import org.uddi.api_v3.TModel;
66 import org.w3c.dom.Document;
67 import org.w3c.dom.Element;
68 import org.w3c.dom.Node;
69 import org.w3c.dom.ls.DOMImplementationLS;
70 import org.w3c.dom.ls.LSSerializer;
71 import org.xml.sax.InputSource;
72 import org.xml.sax.SAXException;
73
74
75
76
77
78
79
80
81 public class XmlSignatureApplet extends java.applet.Applet {
82
83 private static final long serialVersionUID = 1L;
84
85
86
87
88 public void init() {
89 try {
90 java.awt.EventQueue.invokeAndWait(new Runnable() {
91 public void run() {
92 initComponents();
93 }
94 });
95 } catch (Exception ex) {
96 ex.printStackTrace();
97 }
98 setupCertificates();
99 }
100
101 private XMLSignatureFactory initXMLSigFactory() {
102 XMLSignatureFactory fac = XMLSignatureFactory.getInstance();
103 return fac;
104 }
105
106 private Reference initReference(XMLSignatureFactory fac) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
107 List transformers = new ArrayList();
108 transformers.add(fac.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null));
109
110
111
112 String dm = DigestMethod.SHA1;
113
114 Reference ref = fac.newReference("", fac.newDigestMethod(dm, null), transformers, null, null);
115 return ref;
116 }
117
118 private SignedInfo initSignedInfo(XMLSignatureFactory fac) throws Exception {
119 Reference ref = initReference(fac);
120 SignedInfo si = fac.newSignedInfo(fac.newCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE,
121 (C14NMethodParameterSpec) null),
122 fac.newSignatureMethod(SignatureMethod.RSA_SHA1,
123 null),
124 Collections.singletonList(ref));
125 return si;
126 }
127
128 private static Document stringToDom(String xmlSource)
129 throws SAXException, ParserConfigurationException, IOException {
130 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
131 factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
132 DocumentBuilder builder = factory.newDocumentBuilder();
133 return builder.parse(new InputSource(new StringReader(xmlSource)));
134 }
135
136 private String sign(String xml) throws Exception {
137
138
139
140
141 Document doc = stringToDom(xml);
142 Element docElement = doc.getDocumentElement();
143
144
145
146 PrivateKey key = (PrivateKey) keyStore.getKey((String) jList1.getSelectedValue(), null);
147
148
149 Certificate origCert = keyStore.getCertificate((String) jList1.getSelectedValue());
150
151 this.signDOM(docElement, key, origCert);
152
153
154
155 return getStringFromDoc(doc);
156
157 }
158
159
160
161
162
163
164 public String getStringFromDoc(org.w3c.dom.Document doc) {
165 DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
166 LSSerializer lsSerializer = domImplementation.createLSSerializer();
167 lsSerializer.getDomConfig().setParameter("xml-declaration", false);
168
169
170 return lsSerializer.writeToString(doc);
171 }
172 KeyStore keyStore = null;
173 KeyStore firefox = null;
174
175 private void setupCertificates() {
176
177 this.jList1.clearSelection();
178 this.jList1.removeAll();
179 Vector<String> certs = new Vector<String>();
180
181
182
183
184
185
186
187
188
189
190 try {
191 keyStore = KeyStore.getInstance("Windows-MY");
192 keyStore.load(null, null);
193 } catch (Exception ex) {
194 ex.printStackTrace();
195
196 }
197
198 if (keyStore != null) {
199
200 try {
201
202 String strCfg = System.getProperty("user.home") + File.separator
203 + "jdk6-nss-mozilla.cfg";
204
205
206 keyStore = KeyStore.getInstance("PKCS11");
207 keyStore.load(null, "password".toCharArray());
208 } catch (Exception ex) {
209
210 ex.printStackTrace();
211 }
212 }
213
214 if (keyStore != null) {
215 try {
216 keyStore = KeyStore.getInstance("KeychainStore");
217 keyStore.load(null, null);
218
219 } catch (Exception ex) {
220
221 ex.printStackTrace();
222 }
223 }
224 try {
225
226 Enumeration<String> aliases = keyStore.aliases();
227
228 while (aliases.hasMoreElements()) {
229 String a = aliases.nextElement();
230 X509Certificate certificate = (X509Certificate) keyStore.getCertificate(a);
231
232
233 try {
234 Key key = keyStore.getKey(a, null);
235 certs.add(a);
236
237 } catch (Exception x) {
238 System.out.println("error loading certificate " + a + " " + x.getMessage());
239 }
240 }
241
242 } catch (Exception e) {
243 e.printStackTrace();
244 JOptionPane.showMessageDialog(this, e.getMessage());
245 }
246 jList1.setListData(certs);
247 if (!certs.isEmpty()) {
248 jList1.setSelectedIndex(0);
249 }
250 }
251
252
253
254
255 public final static String XML_DIGSIG_NS = "http://www.w3.org/2000/09/xmldsig#";
256
257 private void signDOM(Node node, PrivateKey privateKey, Certificate origCert) {
258 XMLSignatureFactory fac = initXMLSigFactory();
259 X509Certificate cert = (X509Certificate) origCert;
260
261 KeyInfoFactory kif = fac.getKeyInfoFactory();
262 List<Object> x509Content = new ArrayList<Object>();
263
264 x509Content.add(cert);
265 X509Data xd = kif.newX509Data(x509Content);
266 KeyInfo ki = kif.newKeyInfo(Collections.singletonList(xd));
267
268
269
270 DOMSignContext dsc = new DOMSignContext(privateKey, node);
271 dsc.putNamespacePrefix(XML_DIGSIG_NS, "ns2");
272
273
274 try {
275 SignedInfo si = initSignedInfo(fac);
276 XMLSignature signature = fac.newXMLSignature(si, ki);
277
278
279 signature.sign(dsc);
280 } catch (Exception e) {
281 throw new RuntimeException(e);
282 }
283 }
284
285
286
287
288
289
290
291 private void initComponents() {
292
293 jButton1 = new javax.swing.JButton();
294 jScrollPane1 = new javax.swing.JScrollPane();
295 jList1 = new javax.swing.JList();
296
297 setLayout(new java.awt.BorderLayout());
298
299 jButton1.setText("Sign");
300 jButton1.addActionListener(new java.awt.event.ActionListener() {
301 public void actionPerformed(java.awt.event.ActionEvent evt) {
302 jButton1ActionPerformed(evt);
303 }
304 });
305 add(jButton1, java.awt.BorderLayout.NORTH);
306
307 jScrollPane1.setViewportView(jList1);
308
309 add(jScrollPane1, java.awt.BorderLayout.CENTER);
310 }
311
312 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
313
314 String signedXml = "error!";
315 JSObject window = JSObject.getWindow(this);
316 try {
317 if (keyStore == null || keyStore.size() == 0) {
318 signedXml = "Unforunately, it looks as if you don't have any certificates to choose from.";
319 window.call("writeXml", new Object[]{signedXml});
320 return;
321 }
322 } catch (Exception ex) {
323 signedXml = "Unforunately, it looks as if you don't have any certificates to choose from.";
324 window.call("writeXml", new Object[]{signedXml});
325 return;
326 }
327 if (jList1.getSelectedValue() == null) {
328 signedXml = "You must pick a certificate first";
329 window.call("writeXml", new Object[]{signedXml});
330 return;
331 }
332
333 Object object2 = window.call("getBrowserName", null);
334 Object object1 = window.call("getOsName", null);
335 Object object3 = window.call("getObjectType", null);
336 String browserName = (String) object2;
337 String osName = (String) object2;
338 String objecttype = (String) object3;
339
340
341 String xml = (String) window.call("getXml", new Object[]{});
342 Object j = null;
343 StringReader sr = new StringReader(xml);
344 if (objecttype.equalsIgnoreCase("business")) {
345 try {
346 j = (BusinessEntity) XmlUtils.unmarshal(sr, BusinessEntity.class);
347 } catch (Exception ex) {
348 }
349 }
350 if (objecttype.equalsIgnoreCase("service")) {
351 try {
352 j = (BusinessService) XmlUtils.unmarshal(sr, BusinessService.class);
353 } catch (Exception ex) {
354 }
355 }
356 if (objecttype.equalsIgnoreCase("binding")) {
357 try {
358 j = (BindingTemplate) XmlUtils.unmarshal(sr, BindingTemplate.class);
359 } catch (Exception ex) {
360 }
361 }
362 if (objecttype.equalsIgnoreCase("tmodel")) {
363 try {
364 j = (TModel) XmlUtils.unmarshal(sr, TModel.class);
365 } catch (Exception ex) {
366 }
367 }
368
369
370 if (j != null) {
371 try {
372
373 org.apache.juddi.v3.client.cryptor.DigSigUtil ds = new DigSigUtil();
374 ds.put(DigSigUtil.SIGNATURE_OPTION_CERT_INCLUSION_BASE64, "true");
375 ds.put(DigSigUtil.SIGNATURE_METHOD, "RSA_SHA1");
376 PrivateKey key = (PrivateKey) keyStore.getKey((String) jList1.getSelectedValue(), null);
377
378 Certificate publickey = keyStore.getCertificate((String) jList1.getSelectedValue());
379
380
381 j = ds.signUddiEntity(j, publickey, key);
382 ds.clear();
383 StringWriter sw = new StringWriter();
384 JAXB.marshal(j, sw);
385 signedXml = sw.toString();
386 } catch (Exception ex) {
387 Logger.getLogger(XmlSignatureApplet.class.getName()).log(Level.SEVERE, null, ex);
388 signedXml = "Sorry I couldn't sign the data. " + ex.getMessage();
389 }
390 } else {
391 signedXml = "Unable to determine which type of object that we're signing";
392 }
393
394
395
396
397
398
399
400
401
402
403
404 window.call("writeXml", new Object[]{signedXml});
405 }
406
407 private javax.swing.JButton jButton1;
408 private javax.swing.JList jList1;
409 private javax.swing.JScrollPane jScrollPane1;
410
411 }