2
|
1 |
/*
|
|
2 |
* Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
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 |
*
|
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
import java.io.*;
|
|
25 |
import java.util.*;
|
|
26 |
import java.security.MessageDigest;
|
|
27 |
import javax.xml.crypto.*;
|
|
28 |
import javax.xml.crypto.dsig.*;
|
|
29 |
import javax.xml.crypto.dsig.dom.DOMValidateContext;
|
|
30 |
import javax.xml.crypto.dom.*;
|
|
31 |
import javax.xml.parsers.DocumentBuilderFactory;
|
|
32 |
import javax.xml.parsers.DocumentBuilder;
|
|
33 |
import org.w3c.dom.Document;
|
|
34 |
import org.w3c.dom.Node;
|
|
35 |
import org.w3c.dom.NodeList;
|
|
36 |
import org.w3c.dom.Element;
|
|
37 |
|
|
38 |
/**
|
|
39 |
* This is a class which performs xml signature validation upon request
|
|
40 |
*/
|
|
41 |
class SignatureValidator {
|
|
42 |
|
|
43 |
private File dir;
|
|
44 |
|
|
45 |
SignatureValidator(File base) {
|
|
46 |
dir = base;
|
|
47 |
}
|
|
48 |
|
|
49 |
boolean validate(String fn, KeySelector ks, boolean cache)
|
|
50 |
throws Exception {
|
|
51 |
return validate(fn, ks, null, cache);
|
|
52 |
}
|
|
53 |
|
|
54 |
boolean validate(String fn, KeySelector ks, URIDereferencer ud,
|
|
55 |
boolean cache) throws Exception {
|
|
56 |
|
|
57 |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
|
58 |
dbf.setNamespaceAware(true);
|
|
59 |
dbf.setValidating(false);
|
|
60 |
Document doc = dbf.newDocumentBuilder().parse(new File(dir, fn));
|
|
61 |
NodeList nl =
|
|
62 |
doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
|
|
63 |
if (nl.getLength() == 0) {
|
|
64 |
throw new Exception("Couldn't find signature Element");
|
|
65 |
}
|
|
66 |
Element sigElement = (Element) nl.item(0);
|
|
67 |
DOMValidateContext vc = new DOMValidateContext(ks, sigElement);
|
|
68 |
vc.setBaseURI(dir.toURI().toString());
|
|
69 |
if (cache) {
|
|
70 |
vc.setProperty("javax.xml.crypto.dsig.cacheReference", Boolean.TRUE);
|
|
71 |
}
|
|
72 |
XMLSignatureFactory factory = XMLSignatureFactory.getInstance();
|
|
73 |
XMLSignature signature = factory.unmarshalXMLSignature(vc);
|
|
74 |
if (ud != null) {
|
|
75 |
vc.setURIDereferencer(ud);
|
|
76 |
}
|
|
77 |
boolean coreValidity = signature.validate(vc);
|
|
78 |
|
|
79 |
// Check reference cache
|
|
80 |
if (cache) {
|
|
81 |
Iterator i = signature.getSignedInfo().getReferences().iterator();
|
|
82 |
for (int j=0; i.hasNext(); j++) {
|
|
83 |
Reference ref = (Reference) i.next();
|
|
84 |
if (!digestInputEqual(ref)) {
|
|
85 |
throw new Exception
|
|
86 |
("cached data for Reference[" + j + "] is not correct");
|
|
87 |
}
|
|
88 |
// check that dereferenced data does not contain comment nodes
|
|
89 |
if (ref.getURI() == "") {
|
|
90 |
System.out.println("checking deref data");
|
|
91 |
NodeSetData data = (NodeSetData) ref.getDereferencedData();
|
|
92 |
Iterator ni = data.iterator();
|
|
93 |
while (ni.hasNext()) {
|
|
94 |
Node n = (Node) ni.next();
|
|
95 |
if (n.getNodeType() == Node.COMMENT_NODE) {
|
|
96 |
throw new Exception("dereferenced data for " +
|
|
97 |
" Reference[" + j + " contains comment node");
|
|
98 |
}
|
|
99 |
}
|
|
100 |
}
|
|
101 |
}
|
|
102 |
}
|
|
103 |
return coreValidity;
|
|
104 |
}
|
|
105 |
|
|
106 |
private boolean digestInputEqual(Reference ref) throws Exception {
|
|
107 |
MessageDigest md = MessageDigest.getInstance("SHA1");
|
|
108 |
InputStream is = ref.getDigestInputStream();
|
|
109 |
int nbytes;
|
|
110 |
byte[] buf = new byte[256];
|
|
111 |
while ((nbytes = is.read(buf, 0, buf.length)) != -1) {
|
|
112 |
md.update(buf, 0, nbytes);
|
|
113 |
}
|
|
114 |
return Arrays.equals(md.digest(), ref.getDigestValue());
|
|
115 |
}
|
|
116 |
}
|