javax.xml.crypto.dsig.CanonicalizationMethod Java Examples
The following examples show how to use
javax.xml.crypto.dsig.CanonicalizationMethod.
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: DigitalSignatures.java From org.hl7.fhir.core with Apache License 2.0 | 8 votes |
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, KeyException, MarshalException, XMLSignatureException, 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 #2
Source File: DigitalSignatures.java From org.hl7.fhir.core with Apache License 2.0 | 7 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 #3
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 #4
Source File: OpenDocumentLevelBInclusiveCanonicalizationTest.java From dss with GNU Lesser General Public License v2.1 | 6 votes |
@BeforeEach public void init() { signatureParameters = new ASiCWithXAdESSignatureParameters(); signatureParameters.bLevel().setSigningDate(new Date()); signatureParameters.setSigningCertificate(getSigningCert()); signatureParameters.setSignKeyInfo(true); signatureParameters.setCertificateChain(getCertificateChain()); signatureParameters.setSignatureLevel(SignatureLevel.XAdES_BASELINE_B); signatureParameters.setSignedInfoCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE); signatureParameters.setKeyInfoCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE); signatureParameters.setSignedPropertiesCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE); signatureParameters.aSiC().setContainerType(ASiCContainerType.ASiC_E); service = new ASiCWithXAdESService(getCompleteCertificateVerifier()); service.setTspSource(getGoodTsa()); }
Example #5
Source File: OpenDocumentLevelLTAWithKeyInfoTest.java From dss with GNU Lesser General Public License v2.1 | 6 votes |
@BeforeEach public void init() { signatureParameters = new ASiCWithXAdESSignatureParameters(); signatureParameters.bLevel().setSigningDate(new Date()); signatureParameters.setSigningCertificate(getSigningCert()); signatureParameters.setCertificateChain(getCertificateChain()); signatureParameters.setSignatureLevel(SignatureLevel.XAdES_BASELINE_LTA); signatureParameters.aSiC().setContainerType(ASiCContainerType.ASiC_E); // DSS-1548 signatureParameters.setSignKeyInfo(true); signatureParameters.setKeyInfoCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE); service = new ASiCWithXAdESService(getCompleteCertificateVerifier()); service.setTspSource(getGoodTsa()); }
Example #6
Source File: ASiCEXAdESLevelLTAWithKeyInfoTest.java From dss with GNU Lesser General Public License v2.1 | 6 votes |
@BeforeEach public void init() throws Exception { documentToSign = new InMemoryDocument("Hello World !".getBytes(), "test.text"); signatureParameters = new ASiCWithXAdESSignatureParameters(); signatureParameters.bLevel().setSigningDate(new Date()); signatureParameters.setSigningCertificate(getSigningCert()); signatureParameters.setCertificateChain(getCertificateChain()); signatureParameters.setSignatureLevel(SignatureLevel.XAdES_BASELINE_LTA); signatureParameters.aSiC().setContainerType(ASiCContainerType.ASiC_E); // DSS-1548 signatureParameters.setSignKeyInfo(true); signatureParameters.setKeyInfoCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE); service = new ASiCWithXAdESService(getCompleteCertificateVerifier()); service.setTspSource(getGoodTsa()); }
Example #7
Source File: XAdESLevelBBase64TransformTest.java From dss with GNU Lesser General Public License v2.1 | 6 votes |
@Test public void base64WithOtherReferencesTest() { List<DSSTransform> transforms = new ArrayList<>(); Base64Transform dssTransform = new Base64Transform(); transforms.add(dssTransform); CanonicalizationTransform canonicalizationTransform = new CanonicalizationTransform( CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS); transforms.add(canonicalizationTransform); List<DSSReference> refs = buildReferences(document, transforms); XAdESSignatureParameters signatureParameters = new XAdESSignatureParameters(); signatureParameters.bLevel().setSigningDate(new Date()); signatureParameters.setSigningCertificate(getSigningCert()); signatureParameters.setCertificateChain(getCertificateChain()); signatureParameters.setSignaturePackaging(SignaturePackaging.ENVELOPING); signatureParameters.setSignatureLevel(SignatureLevel.XAdES_BASELINE_B); signatureParameters.setReferences(refs); Exception exception = assertThrows(DSSException.class, () -> signAndValidate(document, signatureParameters)); assertEquals("Reference setting is not correct! Base64 transform cannot be used with other transformations.", exception.getMessage()); }
Example #8
Source File: TimestampTokenConverterTest.java From dss with GNU Lesser General Public License v2.1 | 6 votes |
@Test public void toTimestampDTOTest() throws Exception { TimestampToken timestampToken = new TimestampToken(Utils.fromBase64(timestampBinaries), TimestampType.CONTENT_TIMESTAMP); assertNotNull(timestampToken); timestampToken.setCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS); timestampToken.setTimestampIncludes(Arrays.asList(new TimestampInclude("reference-id-1", true))); TimestampDTO timestampDTO = TimestampTokenConverter.toTimestampDTO(timestampToken); assertNotNull(timestampDTO); assertEquals(timestampToken.getTimeStampType(), timestampDTO.getType()); assertEquals(timestampToken.getCanonicalizationMethod(), timestampDTO.getCanonicalizationMethod()); assertEquals(1, timestampDTO.getIncludes().size()); assertEquals("reference-id-1", timestampDTO.getIncludes().get(0).getURI()); assertTrue(timestampDTO.getIncludes().get(0).isReferencedData()); assertTrue(Arrays.equals(timestampToken.getEncoded(), timestampDTO.getBinaries())); }
Example #9
Source File: DigSigUtil.java From juddi with Apache License 2.0 | 6 votes |
private SignedInfo initSignedInfo(XMLSignatureFactory fac) throws Exception { Reference ref = initReference(fac); String cm = null; cm = map.getProperty(CANONICALIZATIONMETHOD); String sigmethod = null; sigmethod = map.getProperty(SIGNATURE_METHOD); if (sigmethod == null) { sigmethod = SignatureMethod.RSA_SHA1; } if (cm == null) { cm = CanonicalizationMethod.EXCLUSIVE; } SignedInfo si = fac.newSignedInfo(fac.newCanonicalizationMethod( cm, (C14NMethodParameterSpec) null), fac.newSignatureMethod(sigmethod, null), Collections.singletonList(ref)); return si; }
Example #10
Source File: XAdESLevelBEnvelopedInclusiveCanonicalizationWithXPathTest.java From dss with GNU Lesser General Public License v2.1 | 6 votes |
@BeforeEach public void init() throws Exception { documentToSign = new FileDocument(new File("src/test/resources/sample.xml")); signatureParameters = new XAdESSignatureParameters(); signatureParameters.bLevel().setSigningDate(new Date()); signatureParameters.setSigningCertificate(getSigningCert()); signatureParameters.setCertificateChain(getCertificateChain()); signatureParameters.setSignaturePackaging(SignaturePackaging.ENVELOPED); signatureParameters.setSignatureLevel(SignatureLevel.XAdES_BASELINE_B); signatureParameters.setSignedInfoCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE); signatureParameters.setSignedPropertiesCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE); // Will add the signature within the tr tag signatureParameters.setXPathLocationString("//*[local-name() = 'tr']"); service = new XAdESService(getOfflineCertificateVerifier()); }
Example #11
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 #12
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 #13
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 #14
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 #15
Source File: XAdESLevelBDetachedInclusiveCanonicalizationTest.java From dss with GNU Lesser General Public License v2.1 | 5 votes |
@BeforeEach public void init() throws Exception { documentToSign = new FileDocument(new File("src/test/resources/sample.xml")); signatureParameters = new XAdESSignatureParameters(); signatureParameters.bLevel().setSigningDate(new Date()); signatureParameters.setSigningCertificate(getSigningCert()); signatureParameters.setCertificateChain(getCertificateChain()); signatureParameters.setSignaturePackaging(SignaturePackaging.DETACHED); signatureParameters.setSignatureLevel(SignatureLevel.XAdES_BASELINE_B); signatureParameters.setSignedInfoCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE); signatureParameters.setSignedPropertiesCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE); service = new XAdESService(getOfflineCertificateVerifier()); }
Example #16
Source File: XmlMultiDocSignatureWithKeyInfoTest.java From dss with GNU Lesser General Public License v2.1 | 5 votes |
@BeforeEach public void init() throws Exception { documentToSigns = Arrays.<DSSDocument> asList(new FileDocument("src/test/resources/sample.xml"), new FileDocument("src/test/resources/sampleWithPlaceOfSignature.xml")); signatureParameters = new XAdESSignatureParameters(); signatureParameters.setSigningCertificate(getSigningCert()); signatureParameters.setCertificateChain(getCertificateChain()); signatureParameters.setSignaturePackaging(SignaturePackaging.ENVELOPING); signatureParameters.setSignatureLevel(SignatureLevel.XAdES_BASELINE_B); signatureParameters.setSignKeyInfo(true); signatureParameters.setKeyInfoCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS); }
Example #17
Source File: XAdESLevelBEnvelopedInclusiveCanonicalizationTest.java From dss with GNU Lesser General Public License v2.1 | 5 votes |
@BeforeEach public void init() throws Exception { documentToSign = new FileDocument(new File("src/test/resources/sample.xml")); signatureParameters = new XAdESSignatureParameters(); signatureParameters.bLevel().setSigningDate(new Date()); signatureParameters.setSigningCertificate(getSigningCert()); signatureParameters.setCertificateChain(getCertificateChain()); signatureParameters.setSignaturePackaging(SignaturePackaging.ENVELOPED); signatureParameters.setSignatureLevel(SignatureLevel.XAdES_BASELINE_B); signatureParameters.setSignedInfoCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE); signatureParameters.setSignedPropertiesCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE); service = new XAdESService(getOfflineCertificateVerifier()); }
Example #18
Source File: XAdESLevelLTAInternallyDetachedTest.java From dss with GNU Lesser General Public License v2.1 | 5 votes |
@BeforeEach public void init() throws Exception { service = new XAdESService(getCompleteCertificateVerifier()); service.setTspSource(getGoodTsa()); documentToSign = new FileDocument(new File("src/test/resources/sample-with-id.xml")); signatureParameters = new XAdESSignatureParameters(); signatureParameters.setSigningCertificate(getSigningCert()); signatureParameters.setCertificateChain(getCertificateChain()); signatureParameters.setSignaturePackaging(SignaturePackaging.INTERNALLY_DETACHED); signatureParameters.setSignatureLevel(SignatureLevel.XAdES_BASELINE_LTA); SignerLocation signerLocation = new SignerLocation(); signerLocation.setCountry("BE"); signerLocation.setLocality("Brussels"); signerLocation.setStreet("Anspach"); signatureParameters.bLevel().setSignerLocation(signerLocation); signatureParameters.bLevel() .setCommitmentTypeIndications(Arrays.asList(CommitmentTypeEnum.ProofOfSender, CommitmentTypeEnum.ProofOfCreation)); signatureParameters.bLevel().setClaimedSignerRoles(Arrays.asList("Manager", "Administrator")); signatureParameters.setAddX509SubjectName(true); XAdESTimestampParameters contentTimestampParameters = new XAdESTimestampParameters(); contentTimestampParameters.setCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE); signatureParameters.setContentTimestampParameters(contentTimestampParameters); TimestampToken contentTimestamp = service.getContentTimestamp(documentToSign, signatureParameters); contentTimestampParameters = new XAdESTimestampParameters(); contentTimestampParameters.setDigestAlgorithm(DigestAlgorithm.SHA512); contentTimestampParameters.setCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE); signatureParameters.setContentTimestampParameters(contentTimestampParameters); TimestampToken contentTimestamp2 = service.getContentTimestamp(documentToSign, signatureParameters); signatureParameters.setContentTimestamps(Arrays.asList(contentTimestamp, contentTimestamp2)); }
Example #19
Source File: XAdESLevelBDetachedTransformsTest.java From dss with GNU Lesser General Public License v2.1 | 5 votes |
@Test public void canonicalizationTest() throws Exception { List<DSSReference> references = buildReferences(document, new CanonicalizationTransform(CanonicalizationMethod.EXCLUSIVE)); XAdESSignatureParameters signatureParameters = getSignatureParameters(document, references); DSSDocument signed = sign(document, signatureParameters); DiagnosticData diagnosticData = validate(signed, signatureParameters, document); List<SignerDataWrapper> originalDocuments = diagnosticData.getOriginalSignerDocuments(); assertEquals(1, originalDocuments.size()); SignerDataWrapper originalDoc = originalDocuments.get(0); assertEquals(document.getDigest(originalDoc.getDigestAlgoAndValue().getDigestMethod()), Utils.toBase64(originalDoc.getDigestAlgoAndValue().getDigestValue())); }
Example #20
Source File: XAdESServiceTest.java From dss with GNU Lesser General Public License v2.1 | 5 votes |
@Test public void contentTstTest() throws Exception { XAdESSignatureParameters signatureParameters = new XAdESSignatureParameters(); service.getContentTimestamp(new InMemoryDocument(new byte[] {}), signatureParameters); signatureParameters.setContentTimestampParameters(null); service.getContentTimestamp(new InMemoryDocument(new byte[] {}), signatureParameters); XAdESTimestampParameters timestampParameters = new XAdESTimestampParameters(); Exception exception = assertThrows(IllegalArgumentException.class, () -> timestampParameters.setCanonicalizationMethod(null)); assertEquals("Canonicalization cannot be empty! See EN 319 132-1: 4.5 Managing canonicalization of XML nodesets.", exception.getMessage()); exception = assertThrows(IllegalArgumentException.class, () -> timestampParameters.setCanonicalizationMethod("")); assertEquals("Canonicalization cannot be empty! See EN 319 132-1: 4.5 Managing canonicalization of XML nodesets.", exception.getMessage()); InMemoryDocument document = new InMemoryDocument("Hello World!".getBytes()); timestampParameters.setCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE); signatureParameters.setContentTimestampParameters(timestampParameters); TimestampToken contentTimestamp = service.getContentTimestamp(document, signatureParameters); signatureParameters.setSigningCertificate(getSigningCert()); signatureParameters.setCertificateChain(getCertificateChain()); signatureParameters.setSignaturePackaging(SignaturePackaging.ENVELOPING); signatureParameters.setSignatureLevel(SignatureLevel.XAdES_BASELINE_B); contentTimestamp.setCanonicalizationMethod(null); signatureParameters.setContentTimestamps(Arrays.asList(contentTimestamp)); exception = assertThrows(DSSException.class, () -> service.getDataToSign(document, signatureParameters)); assertEquals("Unable to create a timestamp with empty canonicalization method. " + "See EN 319 132-1: 4.5 Managing canonicalization of XML nodesets.", exception.getMessage()); }
Example #21
Source File: ASiCSXAdESLevelBInclusiveCanonicalizationTest.java From dss with GNU Lesser General Public License v2.1 | 5 votes |
@BeforeEach public void init() throws Exception { documentToSign = new InMemoryDocument("Hello World !".getBytes(), "test.text"); signatureParameters = new ASiCWithXAdESSignatureParameters(); signatureParameters.bLevel().setSigningDate(new Date()); signatureParameters.setSigningCertificate(getSigningCert()); signatureParameters.setCertificateChain(getCertificateChain()); signatureParameters.setSignatureLevel(SignatureLevel.XAdES_BASELINE_B); signatureParameters.setSignedInfoCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE); signatureParameters.setSignedPropertiesCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE); signatureParameters.aSiC().setContainerType(ASiCContainerType.ASiC_S); service = new ASiCWithXAdESService(getCompleteCertificateVerifier()); }
Example #22
Source File: ASiCEXAdESLevelBInclusiveCanonicalizationTest.java From dss with GNU Lesser General Public License v2.1 | 5 votes |
@BeforeEach public void init() throws Exception { documentToSign = new InMemoryDocument("Hello World !".getBytes(), "test.text"); signatureParameters = new ASiCWithXAdESSignatureParameters(); signatureParameters.bLevel().setSigningDate(new Date()); signatureParameters.setSigningCertificate(getSigningCert()); signatureParameters.setCertificateChain(getCertificateChain()); signatureParameters.setSignatureLevel(SignatureLevel.XAdES_BASELINE_B); signatureParameters.setSignedInfoCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE); signatureParameters.setSignedPropertiesCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE); signatureParameters.aSiC().setContainerType(ASiCContainerType.ASiC_E); service = new ASiCWithXAdESService(getCompleteCertificateVerifier()); }
Example #23
Source File: TimestampTokenConverterTest.java From dss with GNU Lesser General Public License v2.1 | 5 votes |
@Test public void toTimestampTokenTest() throws Exception { TimestampDTO timestampDTO = new TimestampDTO(Utils.fromBase64(timestampBinaries), TimestampType.INDIVIDUAL_DATA_OBJECTS_TIMESTAMP); timestampDTO.setCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE); timestampDTO.setIncludes(Arrays.asList(new TimestampIncludeDTO("reference-id-1", true))); TimestampToken timestampToken = TimestampTokenConverter.toTimestampToken(timestampDTO); assertNotNull(timestampToken); assertEquals(TimestampType.INDIVIDUAL_DATA_OBJECTS_TIMESTAMP, timestampToken.getTimeStampType()); assertEquals(CanonicalizationMethod.INCLUSIVE, timestampToken.getCanonicalizationMethod()); assertEquals(1, timestampToken.getTimestampIncludes().size()); assertEquals("reference-id-1", timestampToken.getTimestampIncludes().get(0).getURI()); assertTrue(timestampToken.getTimestampIncludes().get(0).isReferencedData()); assertTrue(Arrays.equals(Utils.fromBase64(timestampBinaries), timestampToken.getEncoded())); }
Example #24
Source File: DigSigUtilTest.java From juddi with Apache License 2.0 | 5 votes |
static void validAllSignatureElementsArePresent(List<SignatureType> sigs) { Assert.assertNotNull(sigs); Assert.assertFalse(sigs.isEmpty()); for (int i = 0; i < sigs.size(); i++) { Assert.assertFalse(sigs.get(i).getKeyInfo().getContent().isEmpty()); for (int k = 0; k < sigs.get(i).getSignedInfo().getCanonicalizationMethod().getContent().size(); k++) { Assert.assertTrue(sigs.get(i).getSignedInfo().getCanonicalizationMethod().getContent().get(k).equals(CanonicalizationMethod.EXCLUSIVE)); } } }
Example #25
Source File: XmlSignatureApplet.java From juddi with Apache License 2.0 | 5 votes |
private SignedInfo initSignedInfo(XMLSignatureFactory fac) throws Exception { Reference ref = initReference(fac); SignedInfo si = fac.newSignedInfo(fac.newCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE, (C14NMethodParameterSpec) null), fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), Collections.singletonList(ref)); return si; }
Example #26
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 #27
Source File: XMLSignatureUtil.java From keycloak with Apache License 2.0 | 5 votes |
private static void signImpl(DOMSignContext dsc, String digestMethod, String signatureMethod, String referenceURI, String keyName, PublicKey publicKey, X509Certificate x509Certificate, String canonicalizationMethodType) throws GeneralSecurityException, MarshalException, XMLSignatureException { dsc.setDefaultNamespacePrefix("dsig"); DigestMethod digestMethodObj = fac.newDigestMethod(digestMethod, null); Transform transform1 = fac.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null); Transform transform2 = fac.newTransform("http://www.w3.org/2001/10/xml-exc-c14n#", (TransformParameterSpec) null); List<Transform> transformList = new ArrayList<>(); transformList.add(transform1); transformList.add(transform2); Reference ref = fac.newReference(referenceURI, digestMethodObj, transformList, null, null); CanonicalizationMethod canonicalizationMethod = fac.newCanonicalizationMethod(canonicalizationMethodType, (C14NMethodParameterSpec) null); List<Reference> referenceList = Collections.singletonList(ref); SignatureMethod signatureMethodObj = fac.newSignatureMethod(signatureMethod, null); SignedInfo si = fac.newSignedInfo(canonicalizationMethod, signatureMethodObj, referenceList); KeyInfo ki; if (includeKeyInfoInSignature) { ki = createKeyInfo(keyName, publicKey, x509Certificate); } else { ki = createKeyInfo(keyName, null, null); } XMLSignature signature = fac.newXMLSignature(si, ki); signature.sign(dsc); }
Example #28
Source File: XmlSignatureHelper.java From secure-data-service with Apache License 2.0 | 5 votes |
/** * Signs the SAML assertion using the specified public and private keys. * * @param document * SAML assertion be signed. * @param privateKey * Private key used to sign SAML assertion. * @param publicKey * Public key used to sign SAML asserion. * @return w3c element representation of specified document. * @throws NoSuchAlgorithmException * @throws InvalidAlgorithmParameterException * @throws KeyException * @throws MarshalException * @throws XMLSignatureException */ private Element signSamlAssertion(Document document, PrivateKey privateKey, X509Certificate certificate) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, KeyException, MarshalException, XMLSignatureException { XMLSignatureFactory signatureFactory = XMLSignatureFactory.getInstance("DOM"); List<Transform> envelopedTransform = Collections.singletonList(signatureFactory.newTransform( Transform.ENVELOPED, (TransformParameterSpec) null)); Reference ref = signatureFactory.newReference("", signatureFactory.newDigestMethod(DigestMethod.SHA1, null), envelopedTransform, null, null); SignatureMethod signatureMethod = null; if (certificate.getPublicKey() instanceof DSAPublicKey) { signatureMethod = signatureFactory.newSignatureMethod(SignatureMethod.DSA_SHA1, null); } else if (certificate.getPublicKey() instanceof RSAPublicKey) { signatureMethod = signatureFactory.newSignatureMethod(SignatureMethod.RSA_SHA1, null); } CanonicalizationMethod canonicalizationMethod = signatureFactory.newCanonicalizationMethod( CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS, (C14NMethodParameterSpec) null); SignedInfo signedInfo = signatureFactory.newSignedInfo(canonicalizationMethod, signatureMethod, Collections.singletonList(ref)); KeyInfoFactory keyInfoFactory = signatureFactory.getKeyInfoFactory(); X509Data data = keyInfoFactory.newX509Data(Collections.singletonList(certificate)); KeyInfo keyInfo = keyInfoFactory.newKeyInfo(Collections.singletonList(data)); Element w3cElement = document.getDocumentElement(); Node xmlSigInsertionPoint = getXmlSignatureInsertionLocation(w3cElement); DOMSignContext dsc = new DOMSignContext(privateKey, w3cElement, xmlSigInsertionPoint); XMLSignature signature = signatureFactory.newXMLSignature(signedInfo, keyInfo); signature.sign(dsc); return w3cElement; }
Example #29
Source File: BaseOpenApiGeneratorExampleTest.java From syndesis with Apache License 2.0 | 5 votes |
private static String c14Xml(final String xml) { if (xml == null) { return null; } try { final DocumentBuilder documentBuilder = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder(); final Document document = documentBuilder.parse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))); final TransformService transformation = TransformService.getInstance(CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS, "DOM"); transformation.init(null); final NodeList allElements = document.getElementsByTagName("*"); final List<Node> elements = new ArrayList<>(); for (int i = 0; i < allElements.getLength(); i++) { elements.add(allElements.item(i)); } final OctetStreamData data = (OctetStreamData) transformation.transform((NodeSetData) elements::iterator, null); try (final InputStream stream = data.getOctetStream()) { final byte[] buffy = new byte[stream.available()]; stream.read(buffy); return new String(buffy, StandardCharsets.UTF_8); } } catch (GeneralSecurityException | TransformException | SAXException | IOException | ParserConfigurationException e) { throw new AssertionError(e); } }
Example #30
Source File: XmlSignature.java From cstc with GNU General Public License v3.0 | 5 votes |
protected void createSignature(Document document) throws Exception { String signMethod = (String)signatureMethod.getSelectedItem(); PrivateKeyEntry keyEntry = this.selectedEntry; if( this.multiSignature ) this.validateIdAttributes(document); ArrayList<Reference> references = this.getReferences(); SignedInfo signatureInfo = signatureFac.newSignedInfo(signatureFac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec)null), signatureFac.newSignatureMethod(signatureMethods.get(signMethod), null), references); KeyInfo keyInfo = this.getKeyInfo(); XMLSignature signature = signatureFac.newXMLSignature(signatureInfo, keyInfo); DOMSignContext dsc = new DOMSignContext (keyEntry.getPrivateKey(), document.getDocumentElement()); signature.sign(dsc); }