javax.xml.crypto.dsig.dom.DOMSignContext Java Examples
The following examples show how to use
javax.xml.crypto.dsig.dom.DOMSignContext.
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: STSServiceImpl.java From freehealth-connector with GNU Affero General Public License v3.0 | 6 votes |
private void signRequest(Element requestElement, PrivateKey privateKey, Object keyInfoValue) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, MarshalException, XMLSignatureException, KeyException { DOMSignContext domSignContext = new DOMSignContext(privateKey, requestElement, requestElement.getFirstChild()); String requestId = requestElement.getAttribute("RequestID"); requestElement.setIdAttribute("RequestID", true); List<Transform> transforms = new LinkedList(); transforms.add(xmlSignatureFactory.newTransform("http://www.w3.org/2000/09/xmldsig#enveloped-signature", (TransformParameterSpec)null)); transforms.add(xmlSignatureFactory.newTransform("http://www.w3.org/2001/10/xml-exc-c14n#", (C14NMethodParameterSpec)null)); Reference reference = xmlSignatureFactory.newReference("#" + requestId, xmlSignatureFactory.newDigestMethod("http://www.w3.org/2000/09/xmldsig#sha1", (DigestMethodParameterSpec)null), transforms, (String)null, (String)null); CanonicalizationMethod canonicalizationMethod = xmlSignatureFactory.newCanonicalizationMethod("http://www.w3.org/2001/10/xml-exc-c14n#", (C14NMethodParameterSpec)null); SignatureMethod signatureMethod = xmlSignatureFactory.newSignatureMethod("http://www.w3.org/2000/09/xmldsig#rsa-sha1", (SignatureMethodParameterSpec)null); SignedInfo signedInfo = xmlSignatureFactory.newSignedInfo(canonicalizationMethod, signatureMethod, Collections.singletonList(reference)); KeyInfoFactory keyInfoFactory = xmlSignatureFactory.getKeyInfoFactory(); KeyInfo keyInfo = null; if (keyInfoValue instanceof PublicKey) { keyInfo = keyInfoFactory.newKeyInfo(Collections.singletonList(keyInfoFactory.newKeyValue((PublicKey)keyInfoValue))); } else { if (!(keyInfoValue instanceof X509Certificate)) { throw new IllegalArgumentException("Unsupported keyinfo type [" + keyInfoValue.getClass() + "]"); } keyInfo = keyInfoFactory.newKeyInfo(Collections.singletonList(keyInfoFactory.newX509Data(Collections.singletonList(keyInfoValue)))); } XMLSignature xmlSignature = xmlSignatureFactory.newXMLSignature(signedInfo, keyInfo); xmlSignature.sign(domSignContext); }
Example #2
Source File: STSServiceImpl.java From freehealth-connector with GNU Affero General Public License v3.0 | 6 votes |
private void signRequest(Element requestElement, PrivateKey privateKey, Object keyInfoValue) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, MarshalException, XMLSignatureException, KeyException { DOMSignContext domSignContext = new DOMSignContext(privateKey, requestElement, requestElement.getFirstChild()); String requestId = requestElement.getAttribute("RequestID"); requestElement.setIdAttribute("RequestID", true); List<Transform> transforms = new LinkedList(); transforms.add(xmlSignatureFactory.newTransform("http://www.w3.org/2000/09/xmldsig#enveloped-signature", (TransformParameterSpec)null)); transforms.add(xmlSignatureFactory.newTransform("http://www.w3.org/2001/10/xml-exc-c14n#", (C14NMethodParameterSpec)null)); Reference reference = xmlSignatureFactory.newReference("#" + requestId, xmlSignatureFactory.newDigestMethod("http://www.w3.org/2000/09/xmldsig#sha1", (DigestMethodParameterSpec)null), transforms, (String)null, (String)null); CanonicalizationMethod canonicalizationMethod = xmlSignatureFactory.newCanonicalizationMethod("http://www.w3.org/2001/10/xml-exc-c14n#", (C14NMethodParameterSpec)null); SignatureMethod signatureMethod = xmlSignatureFactory.newSignatureMethod("http://www.w3.org/2000/09/xmldsig#rsa-sha1", (SignatureMethodParameterSpec)null); SignedInfo signedInfo = xmlSignatureFactory.newSignedInfo(canonicalizationMethod, signatureMethod, Collections.singletonList(reference)); KeyInfoFactory keyInfoFactory = xmlSignatureFactory.getKeyInfoFactory(); KeyInfo keyInfo = null; if (keyInfoValue instanceof PublicKey) { keyInfo = keyInfoFactory.newKeyInfo(Collections.singletonList(keyInfoFactory.newKeyValue((PublicKey)keyInfoValue))); } else { if (!(keyInfoValue instanceof X509Certificate)) { throw new IllegalArgumentException("Unsupported keyinfo type [" + keyInfoValue.getClass() + "]"); } keyInfo = keyInfoFactory.newKeyInfo(Collections.singletonList(keyInfoFactory.newX509Data(Collections.singletonList(keyInfoValue)))); } XMLSignature xmlSignature = xmlSignatureFactory.newXMLSignature(signedInfo, keyInfo); xmlSignature.sign(domSignContext); }
Example #3
Source File: XMLSignatureUtil.java From keycloak with Apache License 2.0 | 6 votes |
/** * Sign the root element * * @param doc * @param digestMethod * @param signatureMethod * @param referenceURI * * @return * * @throws GeneralSecurityException * @throws XMLSignatureException * @throws MarshalException * @since 2.5.0 */ public static Document sign(Document doc, String keyName, KeyPair keyPair, String digestMethod, String signatureMethod, String referenceURI, X509Certificate x509Certificate, String canonicalizationMethodType) throws GeneralSecurityException, MarshalException, XMLSignatureException { if (logger.isTraceEnabled()) { logger.trace("Document to be signed=" + DocumentUtil.asString(doc)); } PrivateKey signingKey = keyPair.getPrivate(); PublicKey publicKey = keyPair.getPublic(); DOMSignContext dsc = new DOMSignContext(signingKey, doc.getDocumentElement()); signImpl(dsc, digestMethod, signatureMethod, referenceURI, keyName, publicKey, x509Certificate, canonicalizationMethodType); return doc; }
Example #4
Source File: GenerationTests.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
static void test_create_signature_with_empty_id() throws Exception { System.out.println("* Generating signature-with-empty-id.xml"); // create references List<Reference> refs = Collections.singletonList (fac.newReference("#", sha1)); // create SignedInfo SignedInfo si = fac.newSignedInfo(withoutComments, rsaSha1, refs); // create object with empty id Document doc = db.newDocument(); XMLObject obj = fac.newXMLObject(Collections.singletonList (new DOMStructure(doc.createTextNode("I am the text."))), "", "text/plain", null); // create XMLSignature XMLSignature sig = fac.newXMLSignature(si, rsa, Collections.singletonList(obj), "signature", null); DOMSignContext dsc = new DOMSignContext(getPrivateKey("RSA", 512), doc); sig.sign(dsc); }
Example #5
Source File: GenerationTests.java From jdk8u_jdk with GNU General Public License v2.0 | 6 votes |
static void test_create_signature_with_empty_id() throws Exception { System.out.println("* Generating signature-with-empty-id.xml"); // create references List<Reference> refs = Collections.singletonList (fac.newReference("#", sha1)); // create SignedInfo SignedInfo si = fac.newSignedInfo(withoutComments, rsaSha1, refs); // create object with empty id Document doc = db.newDocument(); XMLObject obj = fac.newXMLObject(Collections.singletonList (new DOMStructure(doc.createTextNode("I am the text."))), "", "text/plain", null); // create XMLSignature XMLSignature sig = fac.newXMLSignature(si, rsa, Collections.singletonList(obj), "signature", null); DOMSignContext dsc = new DOMSignContext(getPrivateKey("RSA", 512), doc); sig.sign(dsc); }
Example #6
Source File: GenerationTests.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
static void test_create_signature_with_empty_id() throws Exception { System.out.println("* Generating signature-with-empty-id.xml"); // create references List<Reference> refs = Collections.singletonList (fac.newReference("#", sha1)); // create SignedInfo SignedInfo si = fac.newSignedInfo(withoutComments, rsaSha1, refs); // create object with empty id Document doc = db.newDocument(); XMLObject obj = fac.newXMLObject(Collections.singletonList (new DOMStructure(doc.createTextNode("I am the text."))), "", "text/plain", null); // create XMLSignature XMLSignature sig = fac.newXMLSignature(si, rsa, Collections.singletonList(obj), "signature", null); DOMSignContext dsc = new DOMSignContext(getPrivateKey("RSA", 512), doc); sig.sign(dsc); }
Example #7
Source File: XmlSignatureApplet.java From juddi with Apache License 2.0 | 5 votes |
private void signDOM(Node node, PrivateKey privateKey, Certificate origCert) { XMLSignatureFactory fac = initXMLSigFactory(); X509Certificate cert = (X509Certificate) origCert; // Create the KeyInfo containing the X509Data. KeyInfoFactory kif = fac.getKeyInfoFactory(); List<Object> x509Content = new ArrayList<Object>(); //x509Content.add(cert.getSubjectX500Principal().getName()); x509Content.add(cert); X509Data xd = kif.newX509Data(x509Content); KeyInfo ki = kif.newKeyInfo(Collections.singletonList(xd)); // Create a DOMSignContext and specify the RSA PrivateKey and // location of the resulting XMLSignature's parent element. DOMSignContext dsc = new DOMSignContext(privateKey, node); dsc.putNamespacePrefix(XML_DIGSIG_NS, "ns2"); // Create the XMLSignature, but don't sign it yet. try { SignedInfo si = initSignedInfo(fac); XMLSignature signature = fac.newXMLSignature(si, ki); // Marshal, generate, and sign the enveloped signature. signature.sign(dsc); } catch (Exception e) { throw new RuntimeException(e); } }
Example #8
Source File: TckSigningUtil.java From juddi with Apache License 2.0 | 5 votes |
public static void signDOM(Node node, PrivateKey privateKey, Certificate origCert) { XMLSignatureFactory fac = initXMLSigFactory(); X509Certificate cert = (X509Certificate) origCert; // Create the KeyInfo containing the X509Data. KeyInfoFactory kif = fac.getKeyInfoFactory(); List<Object> x509Content = new ArrayList<Object>(); x509Content.add(cert.getSubjectX500Principal().getName()); x509Content.add(cert); X509Data xd = kif.newX509Data(x509Content); KeyInfo ki = kif.newKeyInfo(Collections.singletonList(xd)); // Create a DOMSignContext and specify the RSA PrivateKey and // location of the resulting XMLSignature's parent element. DOMSignContext dsc = new DOMSignContext(privateKey, node); dsc.putNamespacePrefix("http://www.w3.org/2000/09/xmldsig#", "ns2"); // Create the XMLSignature, but don't sign it yet. try { SignedInfo si = initSignedInfo(fac); XMLSignature signature = fac.newXMLSignature(si, ki); // Marshal, generate, and sign the enveloped signature. signature.sign(dsc); } catch (Exception e) { throw new RuntimeException(e); } }
Example #9
Source File: DigitalSignatures.java From org.hl7.fhir.core with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception { // http://docs.oracle.com/javase/7/docs/technotes/guides/security/xmldsig/XMLDigitalSignature.html // byte[] inputXml = "<Envelope xmlns=\"urn:envelope\">\r\n</Envelope>\r\n".getBytes(); // load the document that's going to be signed DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder builder = dbf.newDocumentBuilder(); Document doc = builder.parse(new ByteArrayInputStream(inputXml)); // // create a key pair // KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); // kpg.initialize(512); // KeyPair kp = kpg.generateKeyPair(); PublicKey pub = getPublicKey("C:\\work\\fhirserver\\tests\\signatures\\public_key.der"); PrivateKey priv = getPrivateKey("C:\\work\\fhirserver\\tests\\signatures\\private_key.der"); // sign the document DOMSignContext dsc = new DOMSignContext(priv, doc.getDocumentElement()); XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM"); Reference ref = fac.newReference("", fac.newDigestMethod(DigestMethod.SHA1, null), Collections.singletonList(fac.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)), null, null); SignedInfo si = fac.newSignedInfo(fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null), fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), Collections.singletonList(ref)); KeyInfoFactory kif = fac.getKeyInfoFactory(); KeyValue kv = kif.newKeyValue(pub); KeyInfo ki = kif.newKeyInfo(Collections.singletonList(kv)); XMLSignature signature = fac.newXMLSignature(si, ki); signature.sign(dsc); OutputStream os = new FileOutputStream("c:\\temp\\java-digsig.xml"); new XmlGenerator().generate(doc.getDocumentElement(), os); }
Example #10
Source File: DigitalSignatures.java From org.hl7.fhir.core with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, KeyException, MarshalException, XMLSignatureException, FHIRException, org.hl7.fhir.exceptions.FHIRException { // http://docs.oracle.com/javase/7/docs/technotes/guides/security/xmldsig/XMLDigitalSignature.html // byte[] inputXml = "<Envelope xmlns=\"urn:envelope\">\r\n</Envelope>\r\n".getBytes(); // load the document that's going to be signed DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder builder = dbf.newDocumentBuilder(); Document doc = builder.parse(new ByteArrayInputStream(inputXml)); // create a key pair KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(512); KeyPair kp = kpg.generateKeyPair(); // sign the document DOMSignContext dsc = new DOMSignContext(kp.getPrivate(), doc.getDocumentElement()); XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM"); Reference ref = fac.newReference("", fac.newDigestMethod(DigestMethod.SHA1, null), Collections.singletonList(fac.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)), null, null); SignedInfo si = fac.newSignedInfo(fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null), fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), Collections.singletonList(ref)); KeyInfoFactory kif = fac.getKeyInfoFactory(); KeyValue kv = kif.newKeyValue(kp.getPublic()); KeyInfo ki = kif.newKeyInfo(Collections.singletonList(kv)); XMLSignature signature = fac.newXMLSignature(si, ki); signature.sign(dsc); OutputStream os = System.out; new XmlGenerator().generate(doc.getDocumentElement(), os); }
Example #11
Source File: XMLSignatureUtil.java From keycloak with Apache License 2.0 | 5 votes |
/** * Sign the root element * * * @return * * @throws GeneralSecurityException * @throws XMLSignatureException * @throws MarshalException */ public static Document sign(SignatureUtilTransferObject dto, String canonicalizationMethodType) throws GeneralSecurityException, MarshalException, XMLSignatureException { Document doc = dto.getDocumentToBeSigned(); String keyName = dto.getKeyName(); KeyPair keyPair = dto.getKeyPair(); Node nextSibling = dto.getNextSibling(); String digestMethod = dto.getDigestMethod(); String referenceURI = dto.getReferenceURI(); String signatureMethod = dto.getSignatureMethod(); if (logger.isTraceEnabled()) { logger.trace("Document to be signed=" + DocumentUtil.asString(doc)); } PrivateKey signingKey = keyPair.getPrivate(); PublicKey publicKey = keyPair.getPublic(); DOMSignContext dsc = new DOMSignContext(signingKey, doc.getDocumentElement(), nextSibling); signImpl(dsc, digestMethod, signatureMethod, referenceURI, keyName, publicKey, dto.getX509Certificate(), canonicalizationMethodType); if (logger.isTraceEnabled()) { logger.trace("Signed document=" + DocumentUtil.asString(doc)); } return doc; }
Example #12
Source File: XMLSignatureBuilder.java From development with Apache License 2.0 | 5 votes |
public Document sign(FileInputStream fileStream, KeyPair keyPair) throws ParserConfigurationException, SAXException, IOException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, KeyException, MarshalException, XMLSignatureException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(fileStream); DOMSignContext signContext = new DOMSignContext(keyPair.getPrivate(), document.getDocumentElement()); XMLSignatureFactory signFactory = XMLSignatureFactory .getInstance("DOM"); Reference ref = signFactory.newReference("", signFactory .newDigestMethod(digestMethod, null), Collections .singletonList(signFactory.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)), null, null); SignedInfo si = signFactory.newSignedInfo(signFactory .newCanonicalizationMethod( CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS, (C14NMethodParameterSpec) null), signFactory .newSignatureMethod(signatureMethod, null), Collections .singletonList(ref)); KeyInfoFactory kif = signFactory.getKeyInfoFactory(); KeyValue kv = kif.newKeyValue(keyPair.getPublic()); KeyInfo ki = kif.newKeyInfo(Collections.singletonList(kv)); XMLSignature signature = signFactory.newXMLSignature(si, ki); signature.sign(signContext); return document; }
Example #13
Source File: XML.java From restcommander with Apache License 2.0 | 5 votes |
/** * Sign the XML document using xmldsig. * @param document the document to sign; it will be modified by the method. * @param publicKey the public key from the key pair to sign the document. * @param privateKey the private key from the key pair to sign the document. * @return the signed document for chaining. */ public static Document sign(Document document, RSAPublicKey publicKey, RSAPrivateKey privateKey) { XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM"); KeyInfoFactory keyInfoFactory = fac.getKeyInfoFactory(); try { Reference ref =fac.newReference( "", fac.newDigestMethod(DigestMethod.SHA1, null), Collections.singletonList(fac.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)), null, null); SignedInfo si = fac.newSignedInfo(fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null), fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), Collections.singletonList(ref)); DOMSignContext dsc = new DOMSignContext(privateKey, document.getDocumentElement()); KeyValue keyValue = keyInfoFactory.newKeyValue(publicKey); KeyInfo ki = keyInfoFactory.newKeyInfo(Collections.singletonList(keyValue)); XMLSignature signature = fac.newXMLSignature(si, ki); signature.sign(dsc); } catch (Exception e) { Logger.warn("Error while signing an XML document.", e); } return document; }
Example #14
Source File: RequestSigner.java From neoscada with Eclipse Public License 1.0 | 5 votes |
synchronized void sign ( final Key privateKey, final PublicKey publicKey, final Certificate cert, final Document doc ) throws Exception { final DOMSignContext dsc = new DOMSignContext ( privateKey, doc.getDocumentElement () ); final SignatureMethod sm = this.fac.newSignatureMethod ( fromAlg ( privateKey.getAlgorithm () ), null ); final SignedInfo si = this.fac.newSignedInfo ( this.cm, sm, Collections.singletonList ( this.ref ) ); final List<Object> data = new LinkedList<Object> (); if ( cert != null ) { data.add ( this.kif.newKeyValue ( cert.getPublicKey () ) ); data.add ( this.kif.newX509Data ( Collections.singletonList ( cert ) ) ); } else { data.add ( this.kif.newKeyValue ( publicKey ) ); } final KeyInfo ki = this.kif.newKeyInfo ( data ); final XMLSignature signature = this.fac.newXMLSignature ( si, ki ); // finally sign signature.sign ( dsc ); }
Example #15
Source File: SoapMultiSignature.java From cstc with GNU General Public License v3.0 | 5 votes |
protected byte[] perform(byte[] input) throws Exception { String signMethod = (String)signatureMethod.getSelectedItem(); PrivateKeyEntry keyEntry = this.selectedEntry; XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM"); ArrayList<Reference> references = getReferences(fac); SignedInfo signatureInfo = fac.newSignedInfo(fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec)null), fac.newSignatureMethod(signatureMethods.get(signMethod), null), references); KeyInfo keyInfo = this.getKeyInfo(fac, keyEntry); XMLSignature signature = fac.newXMLSignature(signatureInfo, keyInfo); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); Document doc = dbf.newDocumentBuilder().parse(new ByteArrayInputStream(input)); try { validateIdAttributes(doc); } catch( Exception e ) { throw new IllegalArgumentException("Provided Id identifier seems to be invalid."); } DOMSignContext dsc = new DOMSignContext (keyEntry.getPrivateKey(), doc.getDocumentElement()); signature.sign(dsc); DOMSource source = new DOMSource(doc); ByteArrayOutputStream bos = new ByteArrayOutputStream(); StreamResult result = new StreamResult(bos); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.transform(source, result); return bos.toByteArray(); }
Example #16
Source File: XMLDSigWithSecMgr.java From jdk8u_jdk with GNU General Public License v2.0 | 4 votes |
XMLDSigWithSecMgr() throws Exception { setup(); Document doc = db.newDocument(); Element envelope = doc.createElementNS ("http://example.org/envelope", "Envelope"); envelope.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "http://example.org/envelope"); doc.appendChild(envelope); KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); KeyPair kp = kpg.genKeyPair(); // the policy only grants this test SocketPermission to accept, resolve // and connect to localhost so that it can dereference 2nd reference System.setProperty("java.security.policy", System.getProperty("test.src", ".") + File.separator + "policy"); System.setSecurityManager(new SecurityManager()); try { // generate a signature with SecurityManager enabled ArrayList refs = new ArrayList(); refs.add(fac.newReference ("", sha1, Collections.singletonList (fac.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)), null, null)); refs.add(fac.newReference("http://localhost:" + ss.getLocalPort() + "/anything.txt", sha1)); SignedInfo si = fac.newSignedInfo(withoutComments, fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), refs); XMLSignature sig = fac.newXMLSignature(si, null); DOMSignContext dsc = new DOMSignContext(kp.getPrivate(), envelope); sig.sign(dsc); // validate a signature with SecurityManager enabled DOMValidateContext dvc = new DOMValidateContext (kp.getPublic(), envelope.getFirstChild()); // disable secure validation mode so that http reference will work dvc.setProperty("org.jcp.xml.dsig.secureValidation", Boolean.FALSE); sig = fac.unmarshalXMLSignature(dvc); if (!sig.validate(dvc)) { throw new Exception ("XMLDSigWithSecMgr signature validation FAILED"); } } catch (SecurityException se) { throw new Exception("XMLDSigWithSecMgr FAILED", se); } ss.close(); }
Example #17
Source File: GenerationTests.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
static void test_create_signature_enveloped_dsa(int size) throws Exception { System.out.println("* Generating signature-enveloped-dsa-" + size + ".xml"); SignatureMethod sm = null; KeyInfo ki = null; Key privKey; if (size == 1024) { sm = dsaSha1; ki = dsa1024; privKey = signingKey; } else if (size == 2048) { sm = dsaSha256; ki = dsa2048; privKey = getPrivateKey("DSA", 2048); } else throw new RuntimeException("unsupported keysize:" + size); // create SignedInfo SignedInfo si = fac.newSignedInfo (withoutComments, sm, Collections.singletonList (fac.newReference ("", sha1, Collections.singletonList (fac.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)), null, null))); // create XMLSignature XMLSignature sig = fac.newXMLSignature(si, ki); Document doc = db.newDocument(); Element envelope = doc.createElementNS ("http://example.org/envelope", "Envelope"); envelope.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns", "http://example.org/envelope"); doc.appendChild(envelope); DOMSignContext dsc = new DOMSignContext(privKey, envelope); sig.sign(dsc); // StringWriter sw = new StringWriter(); // dumpDocument(doc, sw); // System.out.println(sw.toString()); DOMValidateContext dvc = new DOMValidateContext (kvks, envelope.getFirstChild()); 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"); } System.out.println(); }
Example #18
Source File: GenerationTests.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
static void test_create_signature_with_attr_in_no_namespace() throws Exception { System.out.println ("* Generating signature-with-attr-in-no-namespace.xml"); // create references List<Reference> refs = Collections.singletonList (fac.newReference("#unknown", sha1)); // create SignedInfo SignedInfo si = fac.newSignedInfo(withoutComments, rsaSha1, refs); // create object-1 Document doc = db.newDocument(); Element nc = doc.createElementNS(null, "NonCommentandus"); // add attribute with no namespace nc.setAttribute("Id", "unknown"); XMLObject obj = fac.newXMLObject(Collections.singletonList (new DOMStructure(nc)), "object-1", null, null); // create XMLSignature XMLSignature sig = fac.newXMLSignature(si, rsa, Collections.singletonList(obj), "signature", null); DOMSignContext dsc = new DOMSignContext(getPrivateKey("RSA"), doc); dsc.setIdAttributeNS(nc, null, "Id"); sig.sign(dsc); // dumpDocument(doc, new PrintWriter(System.out)); DOMValidateContext dvc = new DOMValidateContext (kvks, doc.getDocumentElement()); dvc.setIdAttributeNS(nc, null, "Id"); 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"); } System.out.println(); }
Example #19
Source File: GenerationTests.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
static void test_create_signature_reference_dependency() throws Exception { System.out.println("* Generating signature-reference-dependency.xml"); // create references List<Reference> refs = Collections.singletonList (fac.newReference("#object-1", sha1)); // create SignedInfo SignedInfo si = fac.newSignedInfo(withoutComments, rsaSha1, refs); // create objects List<XMLStructure> objs = new ArrayList<XMLStructure>(); // Object 1 List<Reference> manRefs = Collections.singletonList (fac.newReference("#object-2", sha1)); objs.add(fac.newXMLObject(Collections.singletonList (fac.newManifest(manRefs, "manifest-1")), "object-1", null, null)); // Object 2 Document doc = db.newDocument(); Element nc = doc.createElementNS(null, "NonCommentandus"); nc.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", ""); nc.appendChild(doc.createComment(" Commentandum ")); objs.add(fac.newXMLObject(Collections.singletonList (new DOMStructure(nc)), "object-2", null, null)); // create XMLSignature XMLSignature sig = fac.newXMLSignature(si, rsa, objs, "signature", null); DOMSignContext dsc = new DOMSignContext(getPrivateKey("RSA", 512), doc); sig.sign(dsc); // dumpDocument(doc, new PrintWriter(System.out)); DOMValidateContext dvc = new DOMValidateContext (kvks, doc.getDocumentElement()); 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"); } System.out.println(); }
Example #20
Source File: SamlUtils.java From cas4.0.x-server-wechat with Apache License 2.0 | 4 votes |
private static Element signSamlElement(final Element element, final PrivateKey privKey, final PublicKey pubKey) { try { final String providerName = System.getProperty("jsr105Provider", JSR_105_PROVIDER); final XMLSignatureFactory sigFactory = XMLSignatureFactory .getInstance("DOM", (Provider) Class.forName(providerName) .newInstance()); final List envelopedTransform = Collections .singletonList(sigFactory.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)); final Reference ref = sigFactory.newReference("", sigFactory .newDigestMethod(DigestMethod.SHA1, null), envelopedTransform, null, null); // Create the SignatureMethod based on the type of key SignatureMethod signatureMethod; if (pubKey instanceof DSAPublicKey) { signatureMethod = sigFactory.newSignatureMethod( SignatureMethod.DSA_SHA1, null); } else if (pubKey instanceof RSAPublicKey) { signatureMethod = sigFactory.newSignatureMethod( SignatureMethod.RSA_SHA1, null); } else { throw new RuntimeException( "Error signing SAML element: Unsupported type of key"); } final CanonicalizationMethod canonicalizationMethod = sigFactory .newCanonicalizationMethod( CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS, (C14NMethodParameterSpec) null); // Create the SignedInfo final SignedInfo signedInfo = sigFactory.newSignedInfo( canonicalizationMethod, signatureMethod, Collections .singletonList(ref)); // Create a KeyValue containing the DSA or RSA PublicKey final KeyInfoFactory keyInfoFactory = sigFactory .getKeyInfoFactory(); final KeyValue keyValuePair = keyInfoFactory.newKeyValue(pubKey); // Create a KeyInfo and add the KeyValue to it final KeyInfo keyInfo = keyInfoFactory.newKeyInfo(Collections .singletonList(keyValuePair)); // Convert the JDOM document to w3c (Java XML signature API requires // w3c // representation) org.w3c.dom.Element w3cElement = toDom(element); // Create a DOMSignContext and specify the DSA/RSA PrivateKey and // location of the resulting XMLSignature's parent element DOMSignContext dsc = new DOMSignContext(privKey, w3cElement); org.w3c.dom.Node xmlSigInsertionPoint = getXmlSignatureInsertLocation(w3cElement); dsc.setNextSibling(xmlSigInsertionPoint); // Marshal, generate (and sign) the enveloped signature XMLSignature signature = sigFactory.newXMLSignature(signedInfo, keyInfo); signature.sign(dsc); return toJdom(w3cElement); } catch (final Exception e) { throw new RuntimeException("Error signing SAML element: " + e.getMessage(), e); } }
Example #21
Source File: GenerationTests.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
static void test_create_signature_with_attr_in_no_namespace() throws Exception { System.out.println ("* Generating signature-with-attr-in-no-namespace.xml"); // create references List<Reference> refs = Collections.singletonList (fac.newReference("#unknown", sha1)); // create SignedInfo SignedInfo si = fac.newSignedInfo(withoutComments, rsaSha1, refs); // create object-1 Document doc = db.newDocument(); Element nc = doc.createElementNS(null, "NonCommentandus"); // add attribute with no namespace nc.setAttribute("Id", "unknown"); XMLObject obj = fac.newXMLObject(Collections.singletonList (new DOMStructure(nc)), "object-1", null, null); // create XMLSignature XMLSignature sig = fac.newXMLSignature(si, rsa, Collections.singletonList(obj), "signature", null); DOMSignContext dsc = new DOMSignContext(getPrivateKey("RSA", 512), doc); dsc.setIdAttributeNS(nc, null, "Id"); sig.sign(dsc); // dumpDocument(doc, new PrintWriter(System.out)); DOMValidateContext dvc = new DOMValidateContext (kvks, doc.getDocumentElement()); dvc.setIdAttributeNS(nc, null, "Id"); 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"); } System.out.println(); }
Example #22
Source File: GenerationTests.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
static void test_create_sign_spec() throws Exception { System.out.println("* Generating sign-spec.xml"); List<Reference> refs = new ArrayList<Reference>(2); // create reference 1 List<XPathType> types = new ArrayList<XPathType>(3); types.add(new XPathType(" //ToBeSigned ", XPathType.Filter.INTERSECT)); types.add(new XPathType(" //NotToBeSigned ", XPathType.Filter.SUBTRACT)); types.add(new XPathType(" //ReallyToBeSigned ", XPathType.Filter.UNION)); XPathFilter2ParameterSpec xp1 = new XPathFilter2ParameterSpec(types); refs.add(fac.newReference ("", fac.newDigestMethod(DigestMethod.SHA1, null), Collections.singletonList(fac.newTransform(Transform.XPATH2, xp1)), null, null)); // create reference 2 List<Transform> trans2 = new ArrayList<Transform>(2); trans2.add(fac.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)); XPathFilter2ParameterSpec xp2 = new XPathFilter2ParameterSpec (Collections.singletonList (new XPathType(" / ", XPathType.Filter.UNION))); trans2.add(fac.newTransform(Transform.XPATH2, xp2)); refs.add(fac.newReference("#signature-value", fac.newDigestMethod(DigestMethod.SHA1, null), trans2, null, null)); // create SignedInfo SignedInfo si = fac.newSignedInfo( fac.newCanonicalizationMethod (CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null), fac.newSignatureMethod(SignatureMethod.DSA_SHA1, null), refs); // create KeyInfo List<XMLStructure> kits = new ArrayList<XMLStructure>(2); kits.add(kifac.newKeyValue(validatingKey)); List<Object> xds = new ArrayList<Object>(2); xds.add("CN=User"); xds.add(signingCert); kits.add(kifac.newX509Data(xds)); KeyInfo ki = kifac.newKeyInfo(kits); // create XMLSignature XMLSignature sig = fac.newXMLSignature (si, ki, null, null, "signature-value"); Document doc = db.newDocument(); Element tbs1 = doc.createElementNS(null, "ToBeSigned"); Comment tbs1Com = doc.createComment(" comment "); Element tbs1Data = doc.createElementNS(null, "Data"); Element tbs1ntbs = doc.createElementNS(null, "NotToBeSigned"); Element tbs1rtbs = doc.createElementNS(null, "ReallyToBeSigned"); Comment tbs1rtbsCom = doc.createComment(" comment "); Element tbs1rtbsData = doc.createElementNS(null, "Data"); tbs1rtbs.appendChild(tbs1rtbsCom); tbs1rtbs.appendChild(tbs1rtbsData); tbs1ntbs.appendChild(tbs1rtbs); tbs1.appendChild(tbs1Com); tbs1.appendChild(tbs1Data); tbs1.appendChild(tbs1ntbs); Element tbs2 = doc.createElementNS(null, "ToBeSigned"); Element tbs2Data = doc.createElementNS(null, "Data"); Element tbs2ntbs = doc.createElementNS(null, "NotToBeSigned"); Element tbs2ntbsData = doc.createElementNS(null, "Data"); tbs2ntbs.appendChild(tbs2ntbsData); tbs2.appendChild(tbs2Data); tbs2.appendChild(tbs2ntbs); Element document = doc.createElementNS(null, "Document"); document.appendChild(tbs1); document.appendChild(tbs2); doc.appendChild(document); DOMSignContext dsc = new DOMSignContext(signingKey, document); sig.sign(dsc); // dumpDocument(doc, new FileWriter("/tmp/foo.xml")); DOMValidateContext dvc = new DOMValidateContext (new KeySelectors.KeyValueKeySelector(), document.getLastChild()); 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"); } System.out.println(); }
Example #23
Source File: MetadataWriter.java From cxf with Apache License 2.0 | 4 votes |
private static Document signMetaInfo(X509Certificate signingCert, Key signingKey, Document doc, String referenceID ) throws Exception { String signatureMethod = null; if ("SHA1withDSA".equals(signingCert.getSigAlgName())) { signatureMethod = SignatureMethod.DSA_SHA1; } else if ("SHA1withRSA".equals(signingCert.getSigAlgName())) { signatureMethod = SignatureMethod.RSA_SHA1; } else if ("SHA256withRSA".equals(signingCert.getSigAlgName())) { signatureMethod = SignatureMethod.RSA_SHA1; } else { LOG.error("Unsupported signature method: " + signingCert.getSigAlgName()); throw new RuntimeException("Unsupported signature method: " + signingCert.getSigAlgName()); } List<Transform> transformList = new ArrayList<>(); transformList.add(XML_SIGNATURE_FACTORY.newTransform(Transform.ENVELOPED, (TransformParameterSpec)null)); transformList.add(XML_SIGNATURE_FACTORY.newCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE, (C14NMethodParameterSpec)null)); // Create a Reference to the enveloped document (in this case, // you are signing the whole document, so a URI of "" signifies // that, and also specify the SHA1 digest algorithm and // the ENVELOPED Transform. Reference ref = XML_SIGNATURE_FACTORY.newReference("#" + referenceID, XML_SIGNATURE_FACTORY.newDigestMethod(DigestMethod.SHA1, null), transformList, null, null); // Create the SignedInfo. SignedInfo si = XML_SIGNATURE_FACTORY.newSignedInfo( XML_SIGNATURE_FACTORY.newCanonicalizationMethod( CanonicalizationMethod.EXCLUSIVE, (C14NMethodParameterSpec)null), XML_SIGNATURE_FACTORY.newSignatureMethod(signatureMethod, null), Collections.singletonList(ref)); // Create the KeyInfo containing the X509Data. KeyInfoFactory kif = XML_SIGNATURE_FACTORY.getKeyInfoFactory(); List<Object> x509Content = new ArrayList<>(); x509Content.add(signingCert.getSubjectX500Principal().getName()); x509Content.add(signingCert); X509Data xd = kif.newX509Data(x509Content); KeyInfo ki = kif.newKeyInfo(Collections.singletonList(xd)); // Create a DOMSignContext and specify the RSA PrivateKey and // location of the resulting XMLSignature's parent element. //DOMSignContext dsc = new DOMSignContext(keyEntry.getPrivateKey(), doc.getDocumentElement()); DOMSignContext dsc = new DOMSignContext(signingKey, doc.getDocumentElement()); dsc.setIdAttributeNS(doc.getDocumentElement(), null, "ID"); dsc.setNextSibling(doc.getDocumentElement().getFirstChild()); // Create the XMLSignature, but don't sign it yet. XMLSignature signature = XML_SIGNATURE_FACTORY.newXMLSignature(si, ki); // Marshal, generate, and sign the enveloped signature. signature.sign(dsc); // Output the resulting document. return doc; }
Example #24
Source File: GenerationTests.java From hottub 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 #25
Source File: GenerationTests.java From hottub with GNU General Public License v2.0 | 4 votes |
static void test_create_exc_signature() throws Exception { System.out.println("* Generating exc_signature.xml"); List<Reference> refs = new ArrayList<Reference>(4); // create reference 1 refs.add(fac.newReference ("#xpointer(id('to-be-signed'))", fac.newDigestMethod(DigestMethod.SHA1, null), Collections.singletonList (fac.newTransform(CanonicalizationMethod.EXCLUSIVE, (TransformParameterSpec) null)), null, null)); // create reference 2 List<String> prefixList = new ArrayList<String>(2); prefixList.add("bar"); prefixList.add("#default"); ExcC14NParameterSpec params = new ExcC14NParameterSpec(prefixList); refs.add(fac.newReference ("#xpointer(id('to-be-signed'))", fac.newDigestMethod(DigestMethod.SHA1, null), Collections.singletonList (fac.newTransform(CanonicalizationMethod.EXCLUSIVE, params)), null, null)); // create reference 3 refs.add(fac.newReference ("#xpointer(id('to-be-signed'))", fac.newDigestMethod(DigestMethod.SHA1, null), Collections.singletonList(fac.newTransform (CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS, (TransformParameterSpec) null)), null, null)); // create reference 4 prefixList = new ArrayList<String>(2); prefixList.add("bar"); prefixList.add("#default"); params = new ExcC14NParameterSpec(prefixList); refs.add(fac.newReference ("#xpointer(id('to-be-signed'))", fac.newDigestMethod(DigestMethod.SHA1, null), Collections.singletonList(fac.newTransform (CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS, params)), null, null)); // create SignedInfo SignedInfo si = fac.newSignedInfo( fac.newCanonicalizationMethod (CanonicalizationMethod.EXCLUSIVE, (C14NMethodParameterSpec) null), fac.newSignatureMethod(SignatureMethod.DSA_SHA1, null), refs); // create KeyInfo List<XMLStructure> kits = new ArrayList<XMLStructure>(2); kits.add(kifac.newKeyValue(validatingKey)); KeyInfo ki = kifac.newKeyInfo(kits); // create Objects Document doc = db.newDocument(); Element baz = doc.createElementNS("urn:bar", "bar:Baz"); Comment com = doc.createComment(" comment "); baz.appendChild(com); XMLObject obj = fac.newXMLObject(Collections.singletonList (new DOMStructure(baz)), "to-be-signed", null, null); // create XMLSignature XMLSignature sig = fac.newXMLSignature (si, ki, Collections.singletonList(obj), null, null); Element foo = doc.createElementNS("urn:foo", "Foo"); foo.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "urn:foo"); foo.setAttributeNS ("http://www.w3.org/2000/xmlns/", "xmlns:bar", "urn:bar"); doc.appendChild(foo); DOMSignContext dsc = new DOMSignContext(signingKey, foo); dsc.putNamespacePrefix(XMLSignature.XMLNS, "dsig"); sig.sign(dsc); // dumpDocument(doc, new FileWriter("/tmp/foo.xml")); DOMValidateContext dvc = new DOMValidateContext (new KeySelectors.KeyValueKeySelector(), foo.getLastChild()); 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"); } System.out.println(); }
Example #26
Source File: GenerationTests.java From hottub with GNU General Public License v2.0 | 4 votes |
static void test_create_sign_spec() throws Exception { System.out.println("* Generating sign-spec.xml"); List<Reference> refs = new ArrayList<Reference>(2); // create reference 1 List<XPathType> types = new ArrayList<XPathType>(3); types.add(new XPathType(" //ToBeSigned ", XPathType.Filter.INTERSECT)); types.add(new XPathType(" //NotToBeSigned ", XPathType.Filter.SUBTRACT)); types.add(new XPathType(" //ReallyToBeSigned ", XPathType.Filter.UNION)); XPathFilter2ParameterSpec xp1 = new XPathFilter2ParameterSpec(types); refs.add(fac.newReference ("", fac.newDigestMethod(DigestMethod.SHA1, null), Collections.singletonList(fac.newTransform(Transform.XPATH2, xp1)), null, null)); // create reference 2 List<Transform> trans2 = new ArrayList<Transform>(2); trans2.add(fac.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)); XPathFilter2ParameterSpec xp2 = new XPathFilter2ParameterSpec (Collections.singletonList (new XPathType(" / ", XPathType.Filter.UNION))); trans2.add(fac.newTransform(Transform.XPATH2, xp2)); refs.add(fac.newReference("#signature-value", fac.newDigestMethod(DigestMethod.SHA1, null), trans2, null, null)); // create SignedInfo SignedInfo si = fac.newSignedInfo( fac.newCanonicalizationMethod (CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null), fac.newSignatureMethod(SignatureMethod.DSA_SHA1, null), refs); // create KeyInfo List<XMLStructure> kits = new ArrayList<XMLStructure>(2); kits.add(kifac.newKeyValue(validatingKey)); List<Object> xds = new ArrayList<Object>(2); xds.add("CN=User"); xds.add(signingCert); kits.add(kifac.newX509Data(xds)); KeyInfo ki = kifac.newKeyInfo(kits); // create XMLSignature XMLSignature sig = fac.newXMLSignature (si, ki, null, null, "signature-value"); Document doc = db.newDocument(); Element tbs1 = doc.createElementNS(null, "ToBeSigned"); Comment tbs1Com = doc.createComment(" comment "); Element tbs1Data = doc.createElementNS(null, "Data"); Element tbs1ntbs = doc.createElementNS(null, "NotToBeSigned"); Element tbs1rtbs = doc.createElementNS(null, "ReallyToBeSigned"); Comment tbs1rtbsCom = doc.createComment(" comment "); Element tbs1rtbsData = doc.createElementNS(null, "Data"); tbs1rtbs.appendChild(tbs1rtbsCom); tbs1rtbs.appendChild(tbs1rtbsData); tbs1ntbs.appendChild(tbs1rtbs); tbs1.appendChild(tbs1Com); tbs1.appendChild(tbs1Data); tbs1.appendChild(tbs1ntbs); Element tbs2 = doc.createElementNS(null, "ToBeSigned"); Element tbs2Data = doc.createElementNS(null, "Data"); Element tbs2ntbs = doc.createElementNS(null, "NotToBeSigned"); Element tbs2ntbsData = doc.createElementNS(null, "Data"); tbs2ntbs.appendChild(tbs2ntbsData); tbs2.appendChild(tbs2Data); tbs2.appendChild(tbs2ntbs); Element document = doc.createElementNS(null, "Document"); document.appendChild(tbs1); document.appendChild(tbs2); doc.appendChild(document); DOMSignContext dsc = new DOMSignContext(signingKey, document); sig.sign(dsc); // dumpDocument(doc, new FileWriter("/tmp/foo.xml")); DOMValidateContext dvc = new DOMValidateContext (new KeySelectors.KeyValueKeySelector(), document.getLastChild()); 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"); } System.out.println(); }
Example #27
Source File: GenerationTests.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
static void test_create_sign_spec() throws Exception { System.out.println("* Generating sign-spec.xml"); List<Reference> refs = new ArrayList<Reference>(2); // create reference 1 List<XPathType> types = new ArrayList<XPathType>(3); types.add(new XPathType(" //ToBeSigned ", XPathType.Filter.INTERSECT)); types.add(new XPathType(" //NotToBeSigned ", XPathType.Filter.SUBTRACT)); types.add(new XPathType(" //ReallyToBeSigned ", XPathType.Filter.UNION)); XPathFilter2ParameterSpec xp1 = new XPathFilter2ParameterSpec(types); refs.add(fac.newReference ("", fac.newDigestMethod(DigestMethod.SHA1, null), Collections.singletonList(fac.newTransform(Transform.XPATH2, xp1)), null, null)); // create reference 2 List<Transform> trans2 = new ArrayList<Transform>(2); trans2.add(fac.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)); XPathFilter2ParameterSpec xp2 = new XPathFilter2ParameterSpec (Collections.singletonList (new XPathType(" / ", XPathType.Filter.UNION))); trans2.add(fac.newTransform(Transform.XPATH2, xp2)); refs.add(fac.newReference("#signature-value", fac.newDigestMethod(DigestMethod.SHA1, null), trans2, null, null)); // create SignedInfo SignedInfo si = fac.newSignedInfo( fac.newCanonicalizationMethod (CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null), fac.newSignatureMethod(SignatureMethod.DSA_SHA1, null), refs); // create KeyInfo List<XMLStructure> kits = new ArrayList<XMLStructure>(2); kits.add(kifac.newKeyValue(validatingKey)); List<Object> xds = new ArrayList<Object>(2); xds.add("CN=User"); xds.add(signingCert); kits.add(kifac.newX509Data(xds)); KeyInfo ki = kifac.newKeyInfo(kits); // create XMLSignature XMLSignature sig = fac.newXMLSignature (si, ki, null, null, "signature-value"); Document doc = db.newDocument(); Element tbs1 = doc.createElementNS(null, "ToBeSigned"); Comment tbs1Com = doc.createComment(" comment "); Element tbs1Data = doc.createElementNS(null, "Data"); Element tbs1ntbs = doc.createElementNS(null, "NotToBeSigned"); Element tbs1rtbs = doc.createElementNS(null, "ReallyToBeSigned"); Comment tbs1rtbsCom = doc.createComment(" comment "); Element tbs1rtbsData = doc.createElementNS(null, "Data"); tbs1rtbs.appendChild(tbs1rtbsCom); tbs1rtbs.appendChild(tbs1rtbsData); tbs1ntbs.appendChild(tbs1rtbs); tbs1.appendChild(tbs1Com); tbs1.appendChild(tbs1Data); tbs1.appendChild(tbs1ntbs); Element tbs2 = doc.createElementNS(null, "ToBeSigned"); Element tbs2Data = doc.createElementNS(null, "Data"); Element tbs2ntbs = doc.createElementNS(null, "NotToBeSigned"); Element tbs2ntbsData = doc.createElementNS(null, "Data"); tbs2ntbs.appendChild(tbs2ntbsData); tbs2.appendChild(tbs2Data); tbs2.appendChild(tbs2ntbs); Element document = doc.createElementNS(null, "Document"); document.appendChild(tbs1); document.appendChild(tbs2); doc.appendChild(document); DOMSignContext dsc = new DOMSignContext(signingKey, document); sig.sign(dsc); // dumpDocument(doc, new FileWriter("/tmp/foo.xml")); DOMValidateContext dvc = new DOMValidateContext (new KeySelectors.KeyValueKeySelector(), document.getLastChild()); 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"); } System.out.println(); }
Example #28
Source File: GenerationTests.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
static void test_create_signature_reference_dependency() throws Exception { System.out.println("* Generating signature-reference-dependency.xml"); // create references List<Reference> refs = Collections.singletonList (fac.newReference("#object-1", sha1)); // create SignedInfo SignedInfo si = fac.newSignedInfo(withoutComments, rsaSha1, refs); // create objects List<XMLStructure> objs = new ArrayList<XMLStructure>(); // Object 1 List<Reference> manRefs = Collections.singletonList (fac.newReference("#object-2", sha1)); objs.add(fac.newXMLObject(Collections.singletonList (fac.newManifest(manRefs, "manifest-1")), "object-1", null, null)); // Object 2 Document doc = db.newDocument(); Element nc = doc.createElementNS(null, "NonCommentandus"); nc.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", ""); nc.appendChild(doc.createComment(" Commentandum ")); objs.add(fac.newXMLObject(Collections.singletonList (new DOMStructure(nc)), "object-2", null, null)); // create XMLSignature XMLSignature sig = fac.newXMLSignature(si, rsa, objs, "signature", null); DOMSignContext dsc = new DOMSignContext(getPrivateKey("RSA"), doc); sig.sign(dsc); // dumpDocument(doc, new PrintWriter(System.out)); DOMValidateContext dvc = new DOMValidateContext (kvks, doc.getDocumentElement()); 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"); } System.out.println(); }
Example #29
Source File: GenerationTests.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
static void test_create_signature_with_attr_in_no_namespace() throws Exception { System.out.println ("* Generating signature-with-attr-in-no-namespace.xml"); // create references List<Reference> refs = Collections.singletonList (fac.newReference("#unknown", sha1)); // create SignedInfo SignedInfo si = fac.newSignedInfo(withoutComments, rsaSha1, refs); // create object-1 Document doc = db.newDocument(); Element nc = doc.createElementNS(null, "NonCommentandus"); // add attribute with no namespace nc.setAttribute("Id", "unknown"); XMLObject obj = fac.newXMLObject(Collections.singletonList (new DOMStructure(nc)), "object-1", null, null); // create XMLSignature XMLSignature sig = fac.newXMLSignature(si, rsa, Collections.singletonList(obj), "signature", null); DOMSignContext dsc = new DOMSignContext(getPrivateKey("RSA", 512), doc); dsc.setIdAttributeNS(nc, null, "Id"); sig.sign(dsc); // dumpDocument(doc, new PrintWriter(System.out)); DOMValidateContext dvc = new DOMValidateContext (kvks, doc.getDocumentElement()); dvc.setIdAttributeNS(nc, null, "Id"); 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"); } System.out.println(); }
Example #30
Source File: GenerationTests.java From jdk8u_jdk with GNU General Public License v2.0 | 4 votes |
static void test_create_exc_signature() throws Exception { System.out.println("* Generating exc_signature.xml"); List<Reference> refs = new ArrayList<Reference>(4); // create reference 1 refs.add(fac.newReference ("#xpointer(id('to-be-signed'))", fac.newDigestMethod(DigestMethod.SHA1, null), Collections.singletonList (fac.newTransform(CanonicalizationMethod.EXCLUSIVE, (TransformParameterSpec) null)), null, null)); // create reference 2 List<String> prefixList = new ArrayList<String>(2); prefixList.add("bar"); prefixList.add("#default"); ExcC14NParameterSpec params = new ExcC14NParameterSpec(prefixList); refs.add(fac.newReference ("#xpointer(id('to-be-signed'))", fac.newDigestMethod(DigestMethod.SHA1, null), Collections.singletonList (fac.newTransform(CanonicalizationMethod.EXCLUSIVE, params)), null, null)); // create reference 3 refs.add(fac.newReference ("#xpointer(id('to-be-signed'))", fac.newDigestMethod(DigestMethod.SHA1, null), Collections.singletonList(fac.newTransform (CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS, (TransformParameterSpec) null)), null, null)); // create reference 4 prefixList = new ArrayList<String>(2); prefixList.add("bar"); prefixList.add("#default"); params = new ExcC14NParameterSpec(prefixList); refs.add(fac.newReference ("#xpointer(id('to-be-signed'))", fac.newDigestMethod(DigestMethod.SHA1, null), Collections.singletonList(fac.newTransform (CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS, params)), null, null)); // create SignedInfo SignedInfo si = fac.newSignedInfo( fac.newCanonicalizationMethod (CanonicalizationMethod.EXCLUSIVE, (C14NMethodParameterSpec) null), fac.newSignatureMethod(SignatureMethod.DSA_SHA1, null), refs); // create KeyInfo List<XMLStructure> kits = new ArrayList<XMLStructure>(2); kits.add(kifac.newKeyValue(validatingKey)); KeyInfo ki = kifac.newKeyInfo(kits); // create Objects Document doc = db.newDocument(); Element baz = doc.createElementNS("urn:bar", "bar:Baz"); Comment com = doc.createComment(" comment "); baz.appendChild(com); XMLObject obj = fac.newXMLObject(Collections.singletonList (new DOMStructure(baz)), "to-be-signed", null, null); // create XMLSignature XMLSignature sig = fac.newXMLSignature (si, ki, Collections.singletonList(obj), null, null); Element foo = doc.createElementNS("urn:foo", "Foo"); foo.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "urn:foo"); foo.setAttributeNS ("http://www.w3.org/2000/xmlns/", "xmlns:bar", "urn:bar"); doc.appendChild(foo); DOMSignContext dsc = new DOMSignContext(signingKey, foo); dsc.putNamespacePrefix(XMLSignature.XMLNS, "dsig"); sig.sign(dsc); // dumpDocument(doc, new FileWriter("/tmp/foo.xml")); DOMValidateContext dvc = new DOMValidateContext (new KeySelectors.KeyValueKeySelector(), foo.getLastChild()); 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"); } System.out.println(); }