author | mullan |
Mon, 23 Jan 2012 12:17:04 -0500 | |
changeset 11674 | a657f8ba55fc |
parent 5506 | 202f599c92aa |
child 24251 | da7dc40edb67 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
11674
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
import java.io.*; |
|
11674
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
25 |
import java.security.Key; |
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
26 |
import java.security.KeyException; |
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
27 |
import java.security.PublicKey; |
2 | 28 |
import java.security.cert.*; |
29 |
import java.util.*; |
|
30 |
import javax.crypto.SecretKey; |
|
31 |
import javax.xml.crypto.*; |
|
32 |
import javax.xml.crypto.dsig.*; |
|
33 |
import javax.xml.crypto.dom.*; |
|
34 |
import javax.xml.crypto.dsig.keyinfo.*; |
|
35 |
import javax.xml.parsers.DocumentBuilderFactory; |
|
36 |
import javax.xml.parsers.DocumentBuilder; |
|
37 |
import org.w3c.dom.Document; |
|
38 |
import org.w3c.dom.Node; |
|
39 |
import org.w3c.dom.Element; |
|
40 |
import org.w3c.dom.traversal.*; |
|
41 |
import sun.security.util.DerValue; |
|
42 |
import sun.security.x509.X500Name; |
|
43 |
||
44 |
/** |
|
45 |
* This is a class which supplies several KeySelector implementations |
|
46 |
*/ |
|
47 |
class KeySelectors { |
|
48 |
||
49 |
/** |
|
50 |
* KeySelector which would always return the secret key specified in its |
|
51 |
* constructor. |
|
52 |
*/ |
|
53 |
static class SecretKeySelector extends KeySelector { |
|
54 |
private SecretKey key; |
|
55 |
SecretKeySelector(byte[] bytes) { |
|
56 |
key = wrapBytes(bytes); |
|
57 |
} |
|
58 |
SecretKeySelector(SecretKey key) { |
|
59 |
this.key = key; |
|
60 |
} |
|
61 |
||
62 |
public KeySelectorResult select(KeyInfo ki, |
|
63 |
KeySelector.Purpose purpose, |
|
64 |
AlgorithmMethod method, |
|
65 |
XMLCryptoContext context) |
|
66 |
throws KeySelectorException { |
|
67 |
return new SimpleKSResult(key); |
|
68 |
} |
|
69 |
||
70 |
private SecretKey wrapBytes(final byte[] bytes) { |
|
71 |
return new SecretKey() { |
|
72 |
public String getFormat() { |
|
73 |
return "RAW"; |
|
74 |
} |
|
75 |
||
76 |
public String getAlgorithm() { |
|
77 |
return "Secret key"; |
|
78 |
} |
|
79 |
||
80 |
public byte[] getEncoded() { |
|
11674
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
81 |
return bytes.clone(); |
2 | 82 |
} |
83 |
}; |
|
84 |
} |
|
85 |
} |
|
86 |
||
87 |
/** |
|
88 |
* KeySelector which would retrieve the X509Certificate out of the |
|
89 |
* KeyInfo element and return the public key. |
|
90 |
* NOTE: If there is an X509CRL in the KeyInfo element, then revoked |
|
91 |
* certificate will be ignored. |
|
92 |
*/ |
|
93 |
static class RawX509KeySelector extends KeySelector { |
|
94 |
||
95 |
public KeySelectorResult select(KeyInfo keyInfo, |
|
96 |
KeySelector.Purpose purpose, |
|
97 |
AlgorithmMethod method, |
|
98 |
XMLCryptoContext context) |
|
99 |
throws KeySelectorException { |
|
100 |
if (keyInfo == null) { |
|
101 |
throw new KeySelectorException("Null KeyInfo object!"); |
|
102 |
} |
|
103 |
// search for X509Data in keyinfo |
|
104 |
Iterator iter = keyInfo.getContent().iterator(); |
|
105 |
while (iter.hasNext()) { |
|
106 |
XMLStructure kiType = (XMLStructure) iter.next(); |
|
107 |
if (kiType instanceof X509Data) { |
|
108 |
X509Data xd = (X509Data) kiType; |
|
109 |
Object[] entries = xd.getContent().toArray(); |
|
110 |
X509CRL crl = null; |
|
111 |
// Looking for CRL before finding certificates |
|
112 |
for (int i = 0; (i<entries.length&&crl != null); i++) { |
|
113 |
if (entries[i] instanceof X509CRL) { |
|
114 |
crl = (X509CRL) entries[i]; |
|
115 |
} |
|
116 |
} |
|
117 |
Iterator xi = xd.getContent().iterator(); |
|
118 |
boolean hasCRL = false; |
|
119 |
while (xi.hasNext()) { |
|
120 |
Object o = xi.next(); |
|
121 |
// skip non-X509Certificate entries |
|
122 |
if (o instanceof X509Certificate) { |
|
123 |
if ((purpose != KeySelector.Purpose.VERIFY) && |
|
124 |
(crl != null) && |
|
125 |
crl.isRevoked((X509Certificate)o)) { |
|
126 |
continue; |
|
127 |
} else { |
|
128 |
return new SimpleKSResult |
|
129 |
(((X509Certificate)o).getPublicKey()); |
|
130 |
} |
|
131 |
} |
|
132 |
} |
|
133 |
} |
|
134 |
} |
|
135 |
throw new KeySelectorException("No X509Certificate found!"); |
|
136 |
} |
|
137 |
} |
|
138 |
||
139 |
/** |
|
140 |
* KeySelector which would retrieve the public key out of the |
|
141 |
* KeyValue element and return it. |
|
142 |
* NOTE: If the key algorithm doesn't match signature algorithm, |
|
143 |
* then the public key will be ignored. |
|
144 |
*/ |
|
145 |
static class KeyValueKeySelector extends KeySelector { |
|
146 |
public KeySelectorResult select(KeyInfo keyInfo, |
|
147 |
KeySelector.Purpose purpose, |
|
148 |
AlgorithmMethod method, |
|
149 |
XMLCryptoContext context) |
|
150 |
throws KeySelectorException { |
|
151 |
if (keyInfo == null) { |
|
152 |
throw new KeySelectorException("Null KeyInfo object!"); |
|
153 |
} |
|
154 |
SignatureMethod sm = (SignatureMethod) method; |
|
155 |
List list = keyInfo.getContent(); |
|
156 |
||
157 |
for (int i = 0; i < list.size(); i++) { |
|
158 |
XMLStructure xmlStructure = (XMLStructure) list.get(i); |
|
159 |
if (xmlStructure instanceof KeyValue) { |
|
160 |
PublicKey pk = null; |
|
161 |
try { |
|
162 |
pk = ((KeyValue)xmlStructure).getPublicKey(); |
|
163 |
} catch (KeyException ke) { |
|
164 |
throw new KeySelectorException(ke); |
|
165 |
} |
|
166 |
// make sure algorithm is compatible with method |
|
167 |
if (algEquals(sm.getAlgorithm(), pk.getAlgorithm())) { |
|
168 |
return new SimpleKSResult(pk); |
|
169 |
} |
|
170 |
} |
|
171 |
} |
|
172 |
throw new KeySelectorException("No KeyValue element found!"); |
|
173 |
} |
|
174 |
||
175 |
//@@@FIXME: this should also work for key types other than DSA/RSA |
|
176 |
static boolean algEquals(String algURI, String algName) { |
|
177 |
if (algName.equalsIgnoreCase("DSA") && |
|
178 |
algURI.equals(SignatureMethod.DSA_SHA1)) { |
|
179 |
return true; |
|
180 |
} else if (algName.equalsIgnoreCase("RSA") && |
|
181 |
(algURI.equals(SignatureMethod.RSA_SHA1) || |
|
182 |
algURI.equals |
|
183 |
("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256") || |
|
184 |
algURI.equals |
|
185 |
("http://www.w3.org/2001/04/xmldsig-more#rsa-sha384") || |
|
186 |
algURI.equals |
|
187 |
("http://www.w3.org/2001/04/xmldsig-more#rsa-sha512"))) { |
|
188 |
return true; |
|
189 |
} else { |
|
190 |
return false; |
|
191 |
} |
|
192 |
} |
|
193 |
} |
|
194 |
||
195 |
/** |
|
196 |
* KeySelector which would perform special lookup as documented |
|
197 |
* by the ie/baltimore/merlin-examples testcases and return the |
|
198 |
* matching public key. |
|
199 |
*/ |
|
200 |
static class CollectionKeySelector extends KeySelector { |
|
11674
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
201 |
private CertificateFactory cf; |
2 | 202 |
private File certDir; |
11674
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
203 |
private Vector<X509Certificate> certs; |
2 | 204 |
private static final int MATCH_SUBJECT = 0; |
205 |
private static final int MATCH_ISSUER = 1; |
|
206 |
private static final int MATCH_SERIAL = 2; |
|
207 |
private static final int MATCH_SUBJECT_KEY_ID = 3; |
|
208 |
private static final int MATCH_CERTIFICATE = 4; |
|
209 |
||
210 |
CollectionKeySelector(File dir) { |
|
211 |
certDir = dir; |
|
212 |
try { |
|
11674
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
213 |
cf = CertificateFactory.getInstance("X509"); |
2 | 214 |
} catch (CertificateException ex) { |
215 |
// not going to happen |
|
216 |
} |
|
11674
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
217 |
certs = new Vector<X509Certificate>(); |
2 | 218 |
File[] files = new File(certDir, "certs").listFiles(); |
219 |
for (int i = 0; i < files.length; i++) { |
|
11674
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
220 |
try (FileInputStream fis = new FileInputStream(files[i])) { |
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
221 |
certs.add((X509Certificate)cf.generateCertificate(fis)); |
2 | 222 |
} catch (Exception ex) { } |
223 |
} |
|
224 |
} |
|
225 |
||
11674
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
226 |
Vector<X509Certificate> match(int matchType, Object value, |
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
227 |
Vector<X509Certificate> pool) { |
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
228 |
Vector<X509Certificate> matchResult = new Vector<>(); |
2 | 229 |
for (int j=0; j < pool.size(); j++) { |
11674
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
230 |
X509Certificate c = pool.get(j); |
2 | 231 |
switch (matchType) { |
232 |
case MATCH_SUBJECT: |
|
233 |
try { |
|
234 |
if (c.getSubjectDN().equals(new X500Name((String)value))) { |
|
235 |
matchResult.add(c); |
|
236 |
} |
|
237 |
} catch (IOException ioe) { } |
|
238 |
break; |
|
239 |
case MATCH_ISSUER: |
|
240 |
try { |
|
241 |
if (c.getIssuerDN().equals(new X500Name((String)value))) { |
|
242 |
matchResult.add(c); |
|
243 |
} |
|
244 |
} catch (IOException ioe) { } |
|
245 |
break; |
|
246 |
case MATCH_SERIAL: |
|
247 |
if (c.getSerialNumber().equals(value)) { |
|
248 |
matchResult.add(c); |
|
249 |
} |
|
250 |
||
251 |
break; |
|
252 |
case MATCH_SUBJECT_KEY_ID: |
|
253 |
byte[] extension = c.getExtensionValue("2.5.29.14"); |
|
254 |
if (extension != null) { |
|
255 |
try { |
|
256 |
DerValue derValue = new DerValue(extension); |
|
257 |
DerValue derValue2 = new DerValue(derValue.getOctetString()); |
|
258 |
byte[] extVal = derValue2.getOctetString(); |
|
259 |
||
260 |
if (Arrays.equals(extVal, (byte[]) value)) { |
|
261 |
matchResult.add(c); |
|
262 |
} |
|
263 |
} catch (IOException ex) { } |
|
264 |
} |
|
265 |
break; |
|
266 |
case MATCH_CERTIFICATE: |
|
267 |
if (c.equals(value)) { |
|
268 |
matchResult.add(c); |
|
269 |
} |
|
270 |
break; |
|
271 |
} |
|
272 |
} |
|
273 |
return matchResult; |
|
274 |
} |
|
275 |
||
276 |
public KeySelectorResult select(KeyInfo keyInfo, |
|
277 |
KeySelector.Purpose purpose, |
|
278 |
AlgorithmMethod method, |
|
279 |
XMLCryptoContext context) |
|
280 |
throws KeySelectorException { |
|
281 |
if (keyInfo == null) { |
|
282 |
throw new KeySelectorException("Null KeyInfo object!"); |
|
283 |
} |
|
284 |
Iterator iter = keyInfo.getContent().iterator(); |
|
285 |
while (iter.hasNext()) { |
|
286 |
XMLStructure xmlStructure = (XMLStructure) iter.next(); |
|
287 |
try { |
|
288 |
if (xmlStructure instanceof KeyName) { |
|
289 |
String name = ((KeyName)xmlStructure).getName(); |
|
290 |
PublicKey pk = null; |
|
11674
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
291 |
File certFile = new File(new File(certDir, "certs"), |
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
292 |
name.toLowerCase() + ".crt"); |
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
293 |
try (FileInputStream fis = new FileInputStream(certFile)) { |
2 | 294 |
// Lookup the public key using the key name 'Xxx', |
295 |
// i.e. the public key is in "certs/xxx.crt". |
|
296 |
X509Certificate cert = (X509Certificate) |
|
11674
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
297 |
cf.generateCertificate(fis); |
2 | 298 |
pk = cert.getPublicKey(); |
299 |
} catch (FileNotFoundException e) { |
|
300 |
// assume KeyName contains subject DN and search |
|
301 |
// collection of certs for match |
|
11674
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
302 |
Vector<X509Certificate> result = |
2 | 303 |
match(MATCH_SUBJECT, name, certs); |
304 |
int numOfMatches = (result==null? 0:result.size()); |
|
305 |
if (numOfMatches != 1) { |
|
306 |
throw new KeySelectorException |
|
307 |
((numOfMatches==0?"No":"More than one") + |
|
308 |
" match found"); |
|
309 |
} |
|
11674
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
310 |
pk = result.get(0).getPublicKey(); |
2 | 311 |
} |
312 |
return new SimpleKSResult(pk); |
|
313 |
} else if (xmlStructure instanceof RetrievalMethod) { |
|
314 |
// Lookup the public key using the retrievel method. |
|
315 |
// NOTE: only X509Certificate type is supported. |
|
316 |
RetrievalMethod rm = (RetrievalMethod) xmlStructure; |
|
317 |
String type = rm.getType(); |
|
318 |
if (type.equals(X509Data.RAW_X509_CERTIFICATE_TYPE)) { |
|
319 |
String uri = rm.getURI(); |
|
11674
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
320 |
try (FileInputStream fis = |
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
321 |
new FileInputStream(new File(certDir, uri))) { |
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
322 |
X509Certificate cert = (X509Certificate) |
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
323 |
cf.generateCertificate(fis); |
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
324 |
return new SimpleKSResult(cert.getPublicKey()); |
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
325 |
} |
2 | 326 |
} else { |
327 |
throw new KeySelectorException |
|
328 |
("Unsupported RetrievalMethod type"); |
|
329 |
} |
|
330 |
} else if (xmlStructure instanceof X509Data) { |
|
331 |
List content = ((X509Data)xmlStructure).getContent(); |
|
332 |
int size = content.size(); |
|
11674
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
333 |
Vector<X509Certificate> result = null; |
2 | 334 |
// Lookup the public key using the information |
335 |
// specified in X509Data element, i.e. searching |
|
336 |
// over the collection of certificate files under |
|
337 |
// "certs" subdirectory and return those match. |
|
338 |
for (int k = 0; k<size; k++) { |
|
339 |
Object obj = content.get(k); |
|
340 |
if (obj instanceof String) { |
|
341 |
result = match(MATCH_SUBJECT, obj, certs); |
|
342 |
} else if (obj instanceof byte[]) { |
|
343 |
result = match(MATCH_SUBJECT_KEY_ID, obj, |
|
344 |
certs); |
|
345 |
} else if (obj instanceof X509Certificate) { |
|
346 |
result = match(MATCH_CERTIFICATE, obj, certs); |
|
347 |
} else if (obj instanceof X509IssuerSerial) { |
|
348 |
X509IssuerSerial is = (X509IssuerSerial) obj; |
|
349 |
result = match(MATCH_SERIAL, |
|
350 |
is.getSerialNumber(), certs); |
|
351 |
result = match(MATCH_ISSUER, |
|
352 |
is.getIssuerName(), result); |
|
353 |
} else { |
|
354 |
throw new KeySelectorException("Unsupported X509Data: " + obj); |
|
355 |
} |
|
356 |
} |
|
357 |
int numOfMatches = (result==null? 0:result.size()); |
|
358 |
if (numOfMatches != 1) { |
|
359 |
throw new KeySelectorException |
|
360 |
((numOfMatches==0?"No":"More than one") + |
|
361 |
" match found"); |
|
362 |
} |
|
11674
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
5506
diff
changeset
|
363 |
return new SimpleKSResult(result.get(0).getPublicKey()); |
2 | 364 |
} |
365 |
} catch (Exception ex) { |
|
366 |
throw new KeySelectorException(ex); |
|
367 |
} |
|
368 |
} |
|
369 |
throw new KeySelectorException("No matching key found!"); |
|
370 |
} |
|
371 |
} |
|
372 |
||
373 |
static class ByteUtil { |
|
374 |
||
375 |
private static String mapping = "0123456789ABCDEF"; |
|
376 |
private static int numBytesPerRow = 6; |
|
377 |
||
378 |
private static String getHex(byte value) { |
|
379 |
int low = value & 0x0f; |
|
380 |
int high = ((value >> 4) & 0x0f); |
|
381 |
char[] res = new char[2]; |
|
382 |
res[0] = mapping.charAt(high); |
|
383 |
res[1] = mapping.charAt(low); |
|
384 |
return new String(res); |
|
385 |
} |
|
386 |
||
387 |
static String dumpArray(byte[] in) { |
|
388 |
int numDumped = 0; |
|
389 |
StringBuffer buf = new StringBuffer(512); |
|
390 |
buf.append("{"); |
|
391 |
for (int i=0;i<(in.length/numBytesPerRow); i++) { |
|
392 |
for (int j=0; j<(numBytesPerRow); j++) { |
|
393 |
buf.append("(byte)0x" + getHex(in[i*numBytesPerRow+j]) + |
|
394 |
", "); |
|
395 |
} |
|
396 |
numDumped += numBytesPerRow; |
|
397 |
} |
|
398 |
while (numDumped < in.length) { |
|
399 |
buf.append("(byte)0x" + getHex(in[numDumped]) + " "); |
|
400 |
numDumped += 1; |
|
401 |
} |
|
402 |
buf.append("}"); |
|
403 |
return buf.toString(); |
|
404 |
} |
|
405 |
} |
|
406 |
} |
|
407 |
||
408 |
class SimpleKSResult implements KeySelectorResult { |
|
409 |
private final Key key; |
|
410 |
||
411 |
SimpleKSResult(Key key) { this.key = key; } |
|
412 |
||
413 |
public Key getKey() { return key; } |
|
414 |
} |