Java Code Examples for javax.xml.crypto.dsig.dom.DOMValidateContext#setURIDereferencer()
The following examples show how to use
javax.xml.crypto.dsig.dom.DOMValidateContext#setURIDereferencer() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ErrorHandlerPermissions.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); dbf.setValidating(false); dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE); Document doc = dbf.newDocumentBuilder().parse(new File(SIGNATURE)); NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature"); if (nl.getLength() == 0) { throw new RuntimeException("Couldn't find 'Signature' element"); } Element element = (Element) nl.item(0); byte[] keyBytes = Base64.getDecoder().decode(validationKey); X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); KeyFactory kf = KeyFactory.getInstance("RSA"); PublicKey key = kf.generatePublic(spec); KeySelector ks = KeySelector.singletonKeySelector(key); DOMValidateContext vc = new DOMValidateContext(ks, element); // disable secure validation mode vc.setProperty("org.jcp.xml.dsig.secureValidation", Boolean.FALSE); // set a dummy dereferencer to be able to get content by references vc.setURIDereferencer(dereferencer); XMLSignatureFactory factory = XMLSignatureFactory.getInstance(); XMLSignature signature = factory.unmarshalXMLSignature(vc); // run validation signature.validate(vc); }
Example 2
Source File: ErrorHandlerPermissions.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); dbf.setValidating(false); dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE); Document doc = dbf.newDocumentBuilder().parse(new File(SIGNATURE)); NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature"); if (nl.getLength() == 0) { throw new RuntimeException("Couldn't find 'Signature' element"); } Element element = (Element) nl.item(0); byte[] keyBytes = Base64.getDecoder().decode(validationKey); X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); KeyFactory kf = KeyFactory.getInstance("RSA"); PublicKey key = kf.generatePublic(spec); KeySelector ks = KeySelector.singletonKeySelector(key); DOMValidateContext vc = new DOMValidateContext(ks, element); // disable secure validation mode vc.setProperty("org.jcp.xml.dsig.secureValidation", Boolean.FALSE); // set a dummy dereferencer to be able to get content by references vc.setURIDereferencer(dereferencer); XMLSignatureFactory factory = XMLSignatureFactory.getInstance(); XMLSignature signature = factory.unmarshalXMLSignature(vc); // run validation signature.validate(vc); }
Example 3
Source File: GenerationTests.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
private static void test_create_signature_external (SignatureMethod sm, KeyInfo ki, Key signingKey, KeySelector ks, boolean b64) throws Exception { // create reference Reference ref; if (b64) { ref = fac.newReference (STYLESHEET_B64, sha1, Collections.singletonList (fac.newTransform(Transform.BASE64, (TransformParameterSpec) null)), null, null); } else { ref = fac.newReference(STYLESHEET, sha1); } // create SignedInfo SignedInfo si = fac.newSignedInfo(withoutComments, sm, Collections.singletonList(ref)); Document doc = db.newDocument(); // create XMLSignature XMLSignature sig = fac.newXMLSignature(si, ki); DOMSignContext dsc = new DOMSignContext(signingKey, doc); dsc.setURIDereferencer(httpUd); sig.sign(dsc); DOMValidateContext dvc = new DOMValidateContext (ks, doc.getDocumentElement()); File f = new File(DATA_DIR); dvc.setBaseURI(f.toURI().toString()); dvc.setURIDereferencer(httpUd); XMLSignature sig2 = fac.unmarshalXMLSignature(dvc); if (sig.equals(sig2) == false) { throw new Exception ("Unmarshalled signature is not equal to generated signature"); } if (sig2.validate(dvc) == false) { throw new Exception("Validation of generated signature failed"); } }
Example 4
Source File: GenerationTests.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
private static void test_create_signature_external (SignatureMethod sm, KeyInfo ki, Key signingKey, KeySelector ks, boolean b64) throws Exception { // create reference Reference ref; if (b64) { ref = fac.newReference (STYLESHEET_B64, sha1, Collections.singletonList (fac.newTransform(Transform.BASE64, (TransformParameterSpec) null)), null, null); } else { ref = fac.newReference(STYLESHEET, sha1); } // create SignedInfo SignedInfo si = fac.newSignedInfo(withoutComments, sm, Collections.singletonList(ref)); Document doc = db.newDocument(); // create XMLSignature XMLSignature sig = fac.newXMLSignature(si, ki); DOMSignContext dsc = new DOMSignContext(signingKey, doc); dsc.setURIDereferencer(httpUd); sig.sign(dsc); DOMValidateContext dvc = new DOMValidateContext (ks, doc.getDocumentElement()); File f = new File(DATA_DIR); dvc.setBaseURI(f.toURI().toString()); dvc.setURIDereferencer(httpUd); XMLSignature sig2 = fac.unmarshalXMLSignature(dvc); if (sig.equals(sig2) == false) { throw new Exception ("Unmarshalled signature is not equal to generated signature"); } if (sig2.validate(dvc) == false) { throw new Exception("Validation of generated signature failed"); } }
Example 5
Source File: SignatureValidator.java From openjdk-8 with GNU General Public License v2.0 | 4 votes |
boolean validate(String fn, KeySelector ks, URIDereferencer ud, boolean cache) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); dbf.setValidating(false); Document doc = dbf.newDocumentBuilder().parse(new File(dir, fn)); NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature"); if (nl.getLength() == 0) { throw new Exception("Couldn't find signature Element"); } Element sigElement = (Element) nl.item(0); DOMValidateContext vc = new DOMValidateContext(ks, sigElement); vc.setBaseURI(dir.toURI().toString()); if (cache) { vc.setProperty("javax.xml.crypto.dsig.cacheReference", Boolean.TRUE); } XMLSignatureFactory factory = XMLSignatureFactory.getInstance(); XMLSignature signature = factory.unmarshalXMLSignature(vc); if (ud != null) { vc.setURIDereferencer(ud); } boolean coreValidity = signature.validate(vc); // Check reference cache if (cache) { Iterator i = signature.getSignedInfo().getReferences().iterator(); for (int j=0; i.hasNext(); j++) { Reference ref = (Reference) i.next(); if (!digestInputEqual(ref)) { throw new Exception ("cached data for Reference[" + j + "] is not correct"); } // check that dereferenced data does not contain comment nodes if (ref.getURI() == "") { System.out.println("checking deref data"); NodeSetData data = (NodeSetData) ref.getDereferencedData(); Iterator ni = data.iterator(); while (ni.hasNext()) { Node n = (Node) ni.next(); if (n.getNodeType() == Node.COMMENT_NODE) { throw new Exception("dereferenced data for " + " Reference[" + j + " contains comment node"); } } } } } return coreValidity; }
Example 6
Source File: GenerationTests.java From jdk8u_jdk with GNU General Public License v2.0 | 4 votes |
private static void test_create_signature_external (SignatureMethod sm, KeyInfo ki, Key signingKey, KeySelector ks, boolean b64) throws Exception { // create reference Reference ref; if (b64) { ref = fac.newReference (STYLESHEET_B64, sha1, Collections.singletonList (fac.newTransform(Transform.BASE64, (TransformParameterSpec) null)), null, null); } else { ref = fac.newReference(STYLESHEET, sha1); } // create SignedInfo SignedInfo si = fac.newSignedInfo(withoutComments, sm, Collections.singletonList(ref)); Document doc = db.newDocument(); // create XMLSignature XMLSignature sig = fac.newXMLSignature(si, ki); DOMSignContext dsc = new DOMSignContext(signingKey, doc); dsc.setURIDereferencer(httpUd); sig.sign(dsc); DOMValidateContext dvc = new DOMValidateContext (ks, doc.getDocumentElement()); File f = new File(DATA_DIR); dvc.setBaseURI(f.toURI().toString()); dvc.setURIDereferencer(httpUd); XMLSignature sig2 = fac.unmarshalXMLSignature(dvc); if (sig.equals(sig2) == false) { throw new Exception ("Unmarshalled signature is not equal to generated signature"); } if (sig2.validate(dvc) == false) { throw new Exception("Validation of generated signature failed"); } }
Example 7
Source File: SignatureValidator.java From hottub with GNU General Public License v2.0 | 4 votes |
boolean validate(String fn, KeySelector ks, URIDereferencer ud, boolean cache) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); dbf.setValidating(false); Document doc = dbf.newDocumentBuilder().parse(new File(dir, fn)); NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature"); if (nl.getLength() == 0) { throw new Exception("Couldn't find signature Element"); } Element sigElement = (Element) nl.item(0); DOMValidateContext vc = new DOMValidateContext(ks, sigElement); vc.setBaseURI(dir.toURI().toString()); if (cache) { vc.setProperty("javax.xml.crypto.dsig.cacheReference", Boolean.TRUE); } XMLSignatureFactory factory = XMLSignatureFactory.getInstance(); XMLSignature signature = factory.unmarshalXMLSignature(vc); if (ud != null) { vc.setURIDereferencer(ud); } boolean coreValidity = signature.validate(vc); // Check reference cache if (cache) { Iterator i = signature.getSignedInfo().getReferences().iterator(); for (int j=0; i.hasNext(); j++) { Reference ref = (Reference) i.next(); if (!digestInputEqual(ref)) { throw new Exception ("cached data for Reference[" + j + "] is not correct"); } // check that dereferenced data does not contain comment nodes if (ref.getURI() == "") { System.out.println("checking deref data"); NodeSetData data = (NodeSetData) ref.getDereferencedData(); Iterator ni = data.iterator(); while (ni.hasNext()) { Node n = (Node) ni.next(); if (n.getNodeType() == Node.COMMENT_NODE) { throw new Exception("dereferenced data for " + " Reference[" + j + " contains comment node"); } } } } } return coreValidity; }
Example 8
Source File: GenerationTests.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
private static void test_create_signature_external (SignatureMethod sm, KeyInfo ki, Key signingKey, KeySelector ks, boolean b64) throws Exception { // create reference Reference ref; if (b64) { ref = fac.newReference (STYLESHEET_B64, sha1, Collections.singletonList (fac.newTransform(Transform.BASE64, (TransformParameterSpec) null)), null, null); } else { ref = fac.newReference(STYLESHEET, sha1); } // create SignedInfo SignedInfo si = fac.newSignedInfo(withoutComments, sm, Collections.singletonList(ref)); Document doc = db.newDocument(); // create XMLSignature XMLSignature sig = fac.newXMLSignature(si, ki); DOMSignContext dsc = new DOMSignContext(signingKey, doc); dsc.setURIDereferencer(httpUd); sig.sign(dsc); DOMValidateContext dvc = new DOMValidateContext (ks, doc.getDocumentElement()); File f = new File(DATA_DIR); dvc.setBaseURI(f.toURI().toString()); dvc.setURIDereferencer(httpUd); XMLSignature sig2 = fac.unmarshalXMLSignature(dvc); if (sig.equals(sig2) == false) { throw new Exception ("Unmarshalled signature is not equal to generated signature"); } if (sig2.validate(dvc) == false) { throw new Exception("Validation of generated signature failed"); } }
Example 9
Source File: GenerationTests.java From openjdk-8 with GNU General Public License v2.0 | 4 votes |
private static void test_create_signature_external (SignatureMethod sm, KeyInfo ki, Key signingKey, KeySelector ks, boolean b64) throws Exception { // create reference Reference ref; if (b64) { ref = fac.newReference (STYLESHEET_B64, sha1, Collections.singletonList (fac.newTransform(Transform.BASE64, (TransformParameterSpec) null)), null, null); } else { ref = fac.newReference(STYLESHEET, sha1); } // create SignedInfo SignedInfo si = fac.newSignedInfo(withoutComments, sm, Collections.singletonList(ref)); Document doc = db.newDocument(); // create XMLSignature XMLSignature sig = fac.newXMLSignature(si, ki); DOMSignContext dsc = new DOMSignContext(signingKey, doc); dsc.setURIDereferencer(httpUd); sig.sign(dsc); DOMValidateContext dvc = new DOMValidateContext (ks, doc.getDocumentElement()); File f = new File(DATA_DIR); dvc.setBaseURI(f.toURI().toString()); dvc.setURIDereferencer(httpUd); XMLSignature sig2 = fac.unmarshalXMLSignature(dvc); if (sig.equals(sig2) == false) { throw new Exception ("Unmarshalled signature is not equal to generated signature"); } if (sig2.validate(dvc) == false) { throw new Exception("Validation of generated signature failed"); } }
Example 10
Source File: GenerationTests.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
private static void test_create_signature_external (SignatureMethod sm, KeyInfo ki, Key signingKey, KeySelector ks, boolean b64) throws Exception { // create reference Reference ref; if (b64) { ref = fac.newReference (STYLESHEET_B64, sha1, Collections.singletonList (fac.newTransform(Transform.BASE64, (TransformParameterSpec) null)), null, null); } else { ref = fac.newReference(STYLESHEET, sha1); } // create SignedInfo SignedInfo si = fac.newSignedInfo(withoutComments, sm, Collections.singletonList(ref)); Document doc = db.newDocument(); // create XMLSignature XMLSignature sig = fac.newXMLSignature(si, ki); DOMSignContext dsc = new DOMSignContext(signingKey, doc); dsc.setURIDereferencer(httpUd); sig.sign(dsc); DOMValidateContext dvc = new DOMValidateContext (ks, doc.getDocumentElement()); File f = new File(DATA_DIR); dvc.setBaseURI(f.toURI().toString()); dvc.setURIDereferencer(httpUd); XMLSignature sig2 = fac.unmarshalXMLSignature(dvc); if (sig.equals(sig2) == false) { throw new Exception ("Unmarshalled signature is not equal to generated signature"); } if (sig2.validate(dvc) == false) { throw new Exception("Validation of generated signature failed"); } }
Example 11
Source File: SignatureValidator.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 4 votes |
boolean validate(String fn, KeySelector ks, URIDereferencer ud, boolean cache) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); dbf.setValidating(false); Document doc = dbf.newDocumentBuilder().parse(new File(dir, fn)); NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature"); if (nl.getLength() == 0) { throw new Exception("Couldn't find signature Element"); } Element sigElement = (Element) nl.item(0); DOMValidateContext vc = new DOMValidateContext(ks, sigElement); vc.setBaseURI(dir.toURI().toString()); if (cache) { vc.setProperty("javax.xml.crypto.dsig.cacheReference", Boolean.TRUE); } XMLSignatureFactory factory = XMLSignatureFactory.getInstance(); XMLSignature signature = factory.unmarshalXMLSignature(vc); if (ud != null) { vc.setURIDereferencer(ud); } boolean coreValidity = signature.validate(vc); // Check reference cache if (cache) { Iterator i = signature.getSignedInfo().getReferences().iterator(); for (int j=0; i.hasNext(); j++) { Reference ref = (Reference) i.next(); if (!digestInputEqual(ref)) { throw new Exception ("cached data for Reference[" + j + "] is not correct"); } // check that dereferenced data does not contain comment nodes if (ref.getURI() == "") { System.out.println("checking deref data"); NodeSetData data = (NodeSetData) ref.getDereferencedData(); Iterator ni = data.iterator(); while (ni.hasNext()) { Node n = (Node) ni.next(); if (n.getNodeType() == Node.COMMENT_NODE) { throw new Exception("dereferenced data for " + " Reference[" + j + " contains comment node"); } } } } } return coreValidity; }
Example 12
Source File: SignatureValidator.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
boolean validate(String fn, KeySelector ks, URIDereferencer ud, boolean cache) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); dbf.setValidating(false); Document doc = dbf.newDocumentBuilder().parse(new File(dir, fn)); NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature"); if (nl.getLength() == 0) { throw new Exception("Couldn't find signature Element"); } Element sigElement = (Element) nl.item(0); DOMValidateContext vc = new DOMValidateContext(ks, sigElement); vc.setBaseURI(dir.toURI().toString()); if (cache) { vc.setProperty("javax.xml.crypto.dsig.cacheReference", Boolean.TRUE); } XMLSignatureFactory factory = XMLSignatureFactory.getInstance(); XMLSignature signature = factory.unmarshalXMLSignature(vc); if (ud != null) { vc.setURIDereferencer(ud); } boolean coreValidity = signature.validate(vc); // Check reference cache if (cache) { Iterator i = signature.getSignedInfo().getReferences().iterator(); for (int j=0; i.hasNext(); j++) { Reference ref = (Reference) i.next(); if (!digestInputEqual(ref)) { throw new Exception ("cached data for Reference[" + j + "] is not correct"); } // check that dereferenced data does not contain comment nodes if (ref.getURI() == "") { System.out.println("checking deref data"); NodeSetData data = (NodeSetData) ref.getDereferencedData(); Iterator ni = data.iterator(); while (ni.hasNext()) { Node n = (Node) ni.next(); if (n.getNodeType() == Node.COMMENT_NODE) { throw new Exception("dereferenced data for " + " Reference[" + j + " contains comment node"); } } } } } return coreValidity; }
Example 13
Source File: GenerationTests.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
private static void test_create_signature_external (SignatureMethod sm, KeyInfo ki, Key signingKey, KeySelector ks, boolean b64) throws Exception { // create reference Reference ref; if (b64) { ref = fac.newReference (STYLESHEET_B64, sha1, Collections.singletonList (fac.newTransform(Transform.BASE64, (TransformParameterSpec) null)), null, null); } else { ref = fac.newReference(STYLESHEET, sha1); } // create SignedInfo SignedInfo si = fac.newSignedInfo(withoutComments, sm, Collections.singletonList(ref)); Document doc = db.newDocument(); // create XMLSignature XMLSignature sig = fac.newXMLSignature(si, ki); DOMSignContext dsc = new DOMSignContext(signingKey, doc); dsc.setURIDereferencer(httpUd); sig.sign(dsc); DOMValidateContext dvc = new DOMValidateContext (ks, doc.getDocumentElement()); File f = new File(DATA_DIR); dvc.setBaseURI(f.toURI().toString()); dvc.setURIDereferencer(httpUd); XMLSignature sig2 = fac.unmarshalXMLSignature(dvc); if (sig.equals(sig2) == false) { throw new Exception ("Unmarshalled signature is not equal to generated signature"); } if (sig2.validate(dvc) == false) { throw new Exception("Validation of generated signature failed"); } }
Example 14
Source File: SignatureValidator.java From jdk8u_jdk with GNU General Public License v2.0 | 4 votes |
boolean validate(String fn, KeySelector ks, URIDereferencer ud, boolean cache) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); dbf.setValidating(false); Document doc = dbf.newDocumentBuilder().parse(new File(dir, fn)); NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature"); if (nl.getLength() == 0) { throw new Exception("Couldn't find signature Element"); } Element sigElement = (Element) nl.item(0); DOMValidateContext vc = new DOMValidateContext(ks, sigElement); vc.setBaseURI(dir.toURI().toString()); if (cache) { vc.setProperty("javax.xml.crypto.dsig.cacheReference", Boolean.TRUE); } XMLSignatureFactory factory = XMLSignatureFactory.getInstance(); XMLSignature signature = factory.unmarshalXMLSignature(vc); if (ud != null) { vc.setURIDereferencer(ud); } boolean coreValidity = signature.validate(vc); // Check reference cache if (cache) { Iterator i = signature.getSignedInfo().getReferences().iterator(); for (int j=0; i.hasNext(); j++) { Reference ref = (Reference) i.next(); if (!digestInputEqual(ref)) { throw new Exception ("cached data for Reference[" + j + "] is not correct"); } // check that dereferenced data does not contain comment nodes if (ref.getURI() == "") { System.out.println("checking deref data"); NodeSetData data = (NodeSetData) ref.getDereferencedData(); Iterator ni = data.iterator(); while (ni.hasNext()) { Node n = (Node) ni.next(); if (n.getNodeType() == Node.COMMENT_NODE) { throw new Exception("dereferenced data for " + " Reference[" + j + " contains comment node"); } } } } } return coreValidity; }
Example 15
Source File: SignatureValidator.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
boolean validate(String fn, KeySelector ks, URIDereferencer ud, boolean cache) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); dbf.setValidating(false); Document doc = dbf.newDocumentBuilder().parse(new File(dir, fn)); NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature"); if (nl.getLength() == 0) { throw new Exception("Couldn't find signature Element"); } Element sigElement = (Element) nl.item(0); DOMValidateContext vc = new DOMValidateContext(ks, sigElement); vc.setBaseURI(dir.toURI().toString()); if (cache) { vc.setProperty("javax.xml.crypto.dsig.cacheReference", Boolean.TRUE); } XMLSignatureFactory factory = XMLSignatureFactory.getInstance(); XMLSignature signature = factory.unmarshalXMLSignature(vc); if (ud != null) { vc.setURIDereferencer(ud); } boolean coreValidity = signature.validate(vc); // Check reference cache if (cache) { Iterator i = signature.getSignedInfo().getReferences().iterator(); for (int j=0; i.hasNext(); j++) { Reference ref = (Reference) i.next(); if (!digestInputEqual(ref)) { throw new Exception ("cached data for Reference[" + j + "] is not correct"); } // check that dereferenced data does not contain comment nodes if (ref.getURI() == "") { System.out.println("checking deref data"); NodeSetData data = (NodeSetData) ref.getDereferencedData(); Iterator ni = data.iterator(); while (ni.hasNext()) { Node n = (Node) ni.next(); if (n.getNodeType() == Node.COMMENT_NODE) { throw new Exception("dereferenced data for " + " Reference[" + j + " contains comment node"); } } } } } return coreValidity; }
Example 16
Source File: GenerationTests.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
private static void test_create_signature_external (SignatureMethod sm, KeyInfo ki, Key signingKey, KeySelector ks, boolean b64) throws Exception { // create reference Reference ref; if (b64) { ref = fac.newReference (STYLESHEET_B64, sha1, Collections.singletonList (fac.newTransform(Transform.BASE64, (TransformParameterSpec) null)), null, null); } else { ref = fac.newReference(STYLESHEET, sha1); } // create SignedInfo SignedInfo si = fac.newSignedInfo(withoutComments, sm, Collections.singletonList(ref)); Document doc = db.newDocument(); // create XMLSignature XMLSignature sig = fac.newXMLSignature(si, ki); DOMSignContext dsc = new DOMSignContext(signingKey, doc); dsc.setURIDereferencer(httpUd); sig.sign(dsc); DOMValidateContext dvc = new DOMValidateContext (ks, doc.getDocumentElement()); File f = new File(DATA_DIR); dvc.setBaseURI(f.toURI().toString()); dvc.setURIDereferencer(httpUd); XMLSignature sig2 = fac.unmarshalXMLSignature(dvc); if (sig.equals(sig2) == false) { throw new Exception ("Unmarshalled signature is not equal to generated signature"); } if (sig2.validate(dvc) == false) { throw new Exception("Validation of generated signature failed"); } }
Example 17
Source File: SignatureValidator.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
boolean validate(String fn, KeySelector ks, URIDereferencer ud, boolean cache) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); dbf.setValidating(false); Document doc = dbf.newDocumentBuilder().parse(new File(dir, fn)); NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature"); if (nl.getLength() == 0) { throw new Exception("Couldn't find signature Element"); } Element sigElement = (Element) nl.item(0); DOMValidateContext vc = new DOMValidateContext(ks, sigElement); vc.setBaseURI(dir.toURI().toString()); if (cache) { vc.setProperty("javax.xml.crypto.dsig.cacheReference", Boolean.TRUE); } XMLSignatureFactory factory = XMLSignatureFactory.getInstance(); XMLSignature signature = factory.unmarshalXMLSignature(vc); if (ud != null) { vc.setURIDereferencer(ud); } boolean coreValidity = signature.validate(vc); // Check reference cache if (cache) { Iterator i = signature.getSignedInfo().getReferences().iterator(); for (int j=0; i.hasNext(); j++) { Reference ref = (Reference) i.next(); if (!digestInputEqual(ref)) { throw new Exception ("cached data for Reference[" + j + "] is not correct"); } // check that dereferenced data does not contain comment nodes if (ref.getURI() == "") { System.out.println("checking deref data"); NodeSetData data = (NodeSetData) ref.getDereferencedData(); Iterator ni = data.iterator(); while (ni.hasNext()) { Node n = (Node) ni.next(); if (n.getNodeType() == Node.COMMENT_NODE) { throw new Exception("dereferenced data for " + " Reference[" + j + " contains comment node"); } } } } } return coreValidity; }
Example 18
Source File: SignatureValidator.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
boolean validate(String fn, KeySelector ks, URIDereferencer ud, boolean cache) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); dbf.setValidating(false); Document doc = dbf.newDocumentBuilder().parse(new File(dir, fn)); NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature"); if (nl.getLength() == 0) { throw new Exception("Couldn't find signature Element"); } Element sigElement = (Element) nl.item(0); DOMValidateContext vc = new DOMValidateContext(ks, sigElement); vc.setBaseURI(dir.toURI().toString()); if (cache) { vc.setProperty("javax.xml.crypto.dsig.cacheReference", Boolean.TRUE); } XMLSignatureFactory factory = XMLSignatureFactory.getInstance(); XMLSignature signature = factory.unmarshalXMLSignature(vc); if (ud != null) { vc.setURIDereferencer(ud); } boolean coreValidity = signature.validate(vc); // Check reference cache if (cache) { Iterator i = signature.getSignedInfo().getReferences().iterator(); for (int j=0; i.hasNext(); j++) { Reference ref = (Reference) i.next(); if (!digestInputEqual(ref)) { throw new Exception ("cached data for Reference[" + j + "] is not correct"); } // check that dereferenced data does not contain comment nodes if (ref.getURI() == "") { System.out.println("checking deref data"); NodeSetData data = (NodeSetData) ref.getDereferencedData(); Iterator ni = data.iterator(); while (ni.hasNext()) { Node n = (Node) ni.next(); if (n.getNodeType() == Node.COMMENT_NODE) { throw new Exception("dereferenced data for " + " Reference[" + j + " contains comment node"); } } } } } return coreValidity; }
Example 19
Source File: SignatureValidator.java From TencentKona-8 with GNU General Public License v2.0 | 4 votes |
boolean validate(String fn, KeySelector ks, URIDereferencer ud, boolean cache) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); dbf.setValidating(false); Document doc = dbf.newDocumentBuilder().parse(new File(dir, fn)); NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature"); if (nl.getLength() == 0) { throw new Exception("Couldn't find signature Element"); } Element sigElement = (Element) nl.item(0); DOMValidateContext vc = new DOMValidateContext(ks, sigElement); vc.setBaseURI(dir.toURI().toString()); if (cache) { vc.setProperty("javax.xml.crypto.dsig.cacheReference", Boolean.TRUE); } XMLSignatureFactory factory = XMLSignatureFactory.getInstance(); XMLSignature signature = factory.unmarshalXMLSignature(vc); if (ud != null) { vc.setURIDereferencer(ud); } boolean coreValidity = signature.validate(vc); // Check reference cache if (cache) { Iterator i = signature.getSignedInfo().getReferences().iterator(); for (int j=0; i.hasNext(); j++) { Reference ref = (Reference) i.next(); if (!digestInputEqual(ref)) { throw new Exception ("cached data for Reference[" + j + "] is not correct"); } // check that dereferenced data does not contain comment nodes if (ref.getURI() == "") { System.out.println("checking deref data"); NodeSetData data = (NodeSetData) ref.getDereferencedData(); Iterator ni = data.iterator(); while (ni.hasNext()) { Node n = (Node) ni.next(); if (n.getNodeType() == Node.COMMENT_NODE) { throw new Exception("dereferenced data for " + " Reference[" + j + " contains comment node"); } } } } } return coreValidity; }
Example 20
Source File: GenerationTests.java From dragonwell8_jdk with GNU General Public License v2.0 | 4 votes |
private static void test_create_signature_external (SignatureMethod sm, KeyInfo ki, Key signingKey, KeySelector ks, boolean b64) throws Exception { // create reference Reference ref; if (b64) { ref = fac.newReference (STYLESHEET_B64, sha1, Collections.singletonList (fac.newTransform(Transform.BASE64, (TransformParameterSpec) null)), null, null); } else { ref = fac.newReference(STYLESHEET, sha1); } // create SignedInfo SignedInfo si = fac.newSignedInfo(withoutComments, sm, Collections.singletonList(ref)); Document doc = db.newDocument(); // create XMLSignature XMLSignature sig = fac.newXMLSignature(si, ki); DOMSignContext dsc = new DOMSignContext(signingKey, doc); dsc.setURIDereferencer(httpUd); sig.sign(dsc); DOMValidateContext dvc = new DOMValidateContext (ks, doc.getDocumentElement()); File f = new File(DATA_DIR); dvc.setBaseURI(f.toURI().toString()); dvc.setURIDereferencer(httpUd); XMLSignature sig2 = fac.unmarshalXMLSignature(dvc); if (sig.equals(sig2) == false) { throw new Exception ("Unmarshalled signature is not equal to generated signature"); } if (sig2.validate(dvc) == false) { throw new Exception("Validation of generated signature failed"); } }