bzr branch
http://darksoft.org/webbzr/xmlbench/trunk
1
by Suren A. Chilingaryan
Initial import |
1 |
import java.io.*; |
2 |
import java.util.*; |
|
3 |
import java.text.*; |
|
4 |
import javax.xml.parsers.DocumentBuilderFactory; |
|
5 |
import javax.xml.parsers.DocumentBuilder; |
|
6 |
import javax.crypto.SecretKey; |
|
7 |
import javax.crypto.KeyGenerator; |
|
8 |
import org.w3c.dom.*; |
|
9 |
||
10 |
import java.security.*; |
|
11 |
import java.security.cert.*; |
|
12 |
||
13 |
import org.apache.xml.security.algorithms.MessageDigestAlgorithm; |
|
14 |
import org.apache.xml.security.c14n.*; |
|
15 |
import org.apache.xml.security.exceptions.XMLSecurityException; |
|
16 |
import org.apache.xml.security.signature.*; |
|
17 |
import org.apache.xml.security.keys.*; |
|
18 |
import org.apache.xml.security.keys.content.*; |
|
19 |
import org.apache.xml.security.keys.content.x509.*; |
|
20 |
import org.apache.xml.security.keys.keyresolver.*; |
|
21 |
import org.apache.xml.security.keys.storage.*; |
|
22 |
import org.apache.xml.security.keys.storage.implementations.*; |
|
23 |
import org.apache.xml.security.utils.*; |
|
24 |
import org.apache.xml.security.transforms.*; |
|
25 |
import org.apache.xml.security.transforms.params.XPathContainer; |
|
26 |
import org.apache.xml.security.Init; |
|
27 |
import org.apache.xml.serialize.*; |
|
28 |
||
29 |
//import org.apache.xml.security.algorithms.encryption.*;
|
|
30 |
import org.apache.xml.security.encryption.EncryptedData; |
|
31 |
import org.apache.xml.security.encryption.CipherData; |
|
32 |
import org.apache.xml.security.encryption.EncryptionProperties; |
|
33 |
||
34 |
import org.apache.xpath.XPathAPI; |
|
35 |
||
36 |
import org.apache.xml.security.keys.KeyInfo; |
|
37 |
import org.apache.xml.security.encryption.XMLCipher; |
|
38 |
import org.apache.xml.security.encryption.EncryptedData; |
|
39 |
import org.apache.xml.security.encryption.EncryptedKey; |
|
40 |
import org.apache.xml.security.utils.Constants; |
|
41 |
||
42 |
public class sun_dom2 extends bench { |
|
43 |
DocumentBuilderFactory factory; |
|
44 |
DocumentBuilder parser; |
|
45 |
Document doc; |
|
46 |
XMLSignature sig; |
|
47 |
KeyPair key; |
|
48 |
||
49 |
Key symmetricKey; |
|
50 |
XMLCipher keyCipher; |
|
51 |
XMLCipher xmlCipher; |
|
52 |
XMLCipher xmlDecipher; |
|
53 |
||
54 |
public void InitXML() { |
|
55 |
KeyPairGenerator kpg; |
|
56 |
factory = DocumentBuilderFactory.newInstance(); |
|
57 |
factory.setNamespaceAware(true); |
|
58 |
factory.setValidating(false); |
|
59 |
try { |
|
60 |
parser = factory.newDocumentBuilder(); |
|
61 |
kpg = KeyPairGenerator.getInstance("RSA"); |
|
62 |
key = kpg.genKeyPair(); |
|
63 |
||
64 |
}
|
|
65 |
catch (Throwable err) { |
|
66 |
err.printStackTrace (); |
|
67 |
return; |
|
68 |
}
|
|
69 |
}
|
|
70 |
||
71 |
public void InitXML_Security() { |
|
72 |
try { |
|
73 |
org.apache.xml.security.Init.init(); |
|
74 |
||
75 |
||
76 |
try { |
|
77 |
KeyGenerator deskg = KeyGenerator.getInstance("TRIPLEDES"); |
|
78 |
symmetricKey = deskg.generateKey(); |
|
79 |
||
80 |
keyCipher = XMLCipher.getInstance(XMLCipher.RSA_v1dot5); |
|
81 |
keyCipher.init(XMLCipher.WRAP_MODE, key.getPrivate()); |
|
82 |
||
83 |
xmlCipher = XMLCipher.getInstance(XMLCipher.TRIPLEDES); |
|
84 |
xmlCipher.init(XMLCipher.ENCRYPT_MODE, symmetricKey); |
|
85 |
||
86 |
xmlDecipher = XMLCipher.getInstance(); |
|
87 |
xmlDecipher.init(XMLCipher.DECRYPT_MODE, null); |
|
88 |
xmlDecipher.setKEK(key.getPublic()); |
|
89 |
||
90 |
}
|
|
91 |
catch (Throwable err) { |
|
92 |
err.printStackTrace (); |
|
93 |
return; |
|
94 |
}
|
|
95 |
||
96 |
// Constants.setSignatureSpecNSprefix("");
|
|
97 |
}
|
|
98 |
catch (Exception e) { |
|
99 |
System.err.println(e.toString()); |
|
100 |
}
|
|
101 |
}
|
|
102 |
||
103 |
||
104 |
public void ParseXML(FileInputStream is, int iters, int iter) { |
|
105 |
try { |
|
106 |
doc=parser.parse(is); |
|
107 |
}
|
|
108 |
catch (Exception e) |
|
109 |
{
|
|
110 |
System.out.println(e.toString()); |
|
111 |
}
|
|
112 |
}
|
|
113 |
||
114 |
public void SignXML(FileInputStream is, int iters, int iter) { |
|
115 |
Element root; |
|
116 |
||
117 |
try { |
|
118 |
sig = new XMLSignature(doc,"", XMLSignature.ALGO_ID_SIGNATURE_RSA); |
|
119 |
root=doc.getDocumentElement(); |
|
120 |
root.appendChild(sig.getElement()); |
|
121 |
Transforms transforms = new Transforms(doc); |
|
122 |
transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE); |
|
123 |
// transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
|
|
124 |
sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1); |
|
125 |
sig.sign(key.getPrivate()); |
|
126 |
}
|
|
127 |
catch (Exception e) { |
|
128 |
System.err.println(e.toString()); |
|
129 |
}
|
|
130 |
||
131 |
/* try {
|
|
132 |
java.io.StringWriter sw = new StringWriter();
|
|
133 |
org.apache.xml.serialize.XMLSerializer dw = new org.apache.xml.serialize.XMLSerializer(sw,null);
|
|
134 |
dw.serialize(doc);
|
|
135 |
dw.asDOMSerializer();
|
|
136 |
String mem = sw.toString();
|
|
137 |
System.out.println(mem);
|
|
138 |
}
|
|
139 |
catch (Exception e) {
|
|
140 |
System.err.println(e.toString());
|
|
141 |
}*/
|
|
142 |
}
|
|
143 |
||
144 |
public void VerifyXML(FileInputStream is, int iters, int iter) { |
|
145 |
/*
|
|
146 |
doc.getDocumentElement().appendChild(doc.createElement("test"));
|
|
147 |
*/
|
|
148 |
try { |
|
149 |
Element nscontext = XMLUtils.createDSctx(doc, "ds", Constants.SignatureSpecNS); |
|
150 |
Element root=(Element) XPathAPI.selectSingleNode(doc,"//ds:Signature[1]", nscontext); |
|
151 |
sig = new XMLSignature(root,""); |
|
152 |
if (!sig.checkSignatureValue(key.getPublic())) { |
|
153 |
System.err.println("Error verifying signature"); |
|
154 |
}
|
|
155 |
}
|
|
156 |
catch (Exception e) { |
|
157 |
System.err.println(e.toString()); |
|
158 |
}
|
|
159 |
||
160 |
}
|
|
161 |
||
162 |
public void EncryptXML(FileInputStream is, int iters, int iter) { |
|
163 |
Element rootElement = doc.getDocumentElement(); |
|
164 |
||
165 |
try { |
|
166 |
EncryptedKey encryptedKey = keyCipher.encryptKey(doc, symmetricKey); |
|
167 |
||
168 |
KeyInfo keyInfo = new KeyInfo(doc); |
|
169 |
keyInfo.add(encryptedKey); |
|
170 |
||
171 |
||
172 |
EncryptedData encryptedData = xmlCipher.getEncryptedData(); |
|
173 |
encryptedData.setKeyInfo(keyInfo); |
|
174 |
||
175 |
xmlCipher.doFinal(doc, rootElement, true); |
|
176 |
}
|
|
177 |
catch (Exception e) { |
|
178 |
System.err.println(e.toString()); |
|
179 |
}
|
|
180 |
/*
|
|
181 |
try {
|
|
182 |
java.io.StringWriter sw = new StringWriter();
|
|
183 |
org.apache.xml.serialize.XMLSerializer dw = new org.apache.xml.serialize.XMLSerializer(sw,null);
|
|
184 |
dw.serialize(doc);
|
|
185 |
dw.asDOMSerializer();
|
|
186 |
String mem = sw.toString();
|
|
187 |
System.out.println(mem);
|
|
188 |
}
|
|
189 |
catch (Exception e) {
|
|
190 |
System.err.println(e.toString());
|
|
191 |
}
|
|
192 |
*/
|
|
193 |
}
|
|
194 |
||
195 |
public void DecryptXML(FileInputStream is, int iters, int iter) { |
|
196 |
try { |
|
197 |
Element encryptedDataElement = |
|
198 |
(Element) doc.getElementsByTagNameNS( |
|
199 |
EncryptionConstants.EncryptionSpecNS, |
|
200 |
EncryptionConstants._TAG_ENCRYPTEDDATA).item(0); |
|
201 |
||
202 |
xmlDecipher.doFinal(doc, encryptedDataElement); |
|
203 |
||
204 |
}
|
|
205 |
catch (Exception e) { |
|
206 |
System.err.println(e.toString()); |
|
207 |
}
|
|
208 |
||
209 |
/*
|
|
210 |
try {
|
|
211 |
java.io.StringWriter sw = new StringWriter();
|
|
212 |
org.apache.xml.serialize.XMLSerializer dw = new org.apache.xml.serialize.XMLSerializer(sw,null);
|
|
213 |
dw.serialize(doc);
|
|
214 |
dw.asDOMSerializer();
|
|
215 |
String mem = sw.toString();
|
|
216 |
System.out.println(mem);
|
|
217 |
}
|
|
218 |
catch (Exception e) {
|
|
219 |
System.err.println(e.toString());
|
|
220 |
}
|
|
221 |
*/
|
|
222 |
}
|
|
223 |
||
224 |
static public void main(String argv[]) throws IOException { |
|
225 |
bench mybench = new sun_dom2(); |
|
226 |
mybench.Bench(argv); |
|
227 |
}
|
|
228 |
}
|