Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/xml/crypto/dsig/SignatureValidator.java
41152 views
1
/*
2
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. 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 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.
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<Reference> i =
82
signature.getSignedInfo().getReferences().iterator();
83
for (int j = 0; i.hasNext(); j++) {
84
Reference ref = i.next();
85
if (!digestInputEqual(ref)) {
86
throw new Exception
87
("cached data for Reference[" + j + "] is not correct");
88
}
89
// check that dereferenced data does not contain comment nodes
90
if (ref.getURI() == "") {
91
System.out.println("checking deref data");
92
@SuppressWarnings("unchecked")
93
NodeSetData<Node> data =
94
(NodeSetData<Node>)ref.getDereferencedData();
95
for (Node n : data) {
96
if (n.getNodeType() == Node.COMMENT_NODE) {
97
throw new Exception("dereferenced data for " +
98
" Reference[" + j + " contains comment node");
99
}
100
}
101
}
102
}
103
}
104
return coreValidity;
105
}
106
107
private boolean digestInputEqual(Reference ref) throws Exception {
108
MessageDigest md = MessageDigest.getInstance("SHA1");
109
InputStream is = ref.getDigestInputStream();
110
int nbytes;
111
byte[] buf = new byte[256];
112
while ((nbytes = is.read(buf, 0, buf.length)) != -1) {
113
md.update(buf, 0, nbytes);
114
}
115
return Arrays.equals(md.digest(), ref.getDigestValue());
116
}
117
}
118
119