@Test public void signSignRequest() throws IOException, XPathExpressionException { DssMetadataService dssMetadataService = new DssMetadataService(Configuration.getParserPool()); ReflectionTestUtils.setField(dssMetadataService, "dssServiceMetadataEntityId", "http: ReflectionTestUtils.setField(dssMetadataService, "dssServiceMetadataResource", new ClassPathResource("dss/dss_valid_metadata.xml")); ReflectionTestUtils.setField(dssMetadataService, "keystorePassword", "password"); dssMetadataService.initDssMetadata(); DssSignMessageService service = new DssSignMessageService(dssMetadataService); ReflectionTestUtils.setField(service, "keystoreAlias", "localhost"); ReflectionTestUtils.setField(service, "keystorePassword", "password"); ReflectionTestUtils.setField(service, "keystoreFile", new ClassPathResource("dss/localhost.p12")); service.init(); String signedSignRequest = service.signSignRequest(getSignRequest("dss/unsigned_signRequest.xml")); assertNotNull(signedSignRequest); XPathFactory factory = XPathFactory.newInstance(); XPath path = factory.newXPath(); Node node = (Node) path .evaluate("/ }
public String signSignRequest(SignRequest signRequest) { try { PrivateKey privateKey = (PrivateKey) clientKeyStore.getKey(keystoreAlias, keystorePassword.toCharArray()); X509Certificate cert = (X509Certificate) clientKeyStore.getCertificate(keystoreAlias); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); Document doc = dbf.newDocumentBuilder().parse(IOUtils.toInputStream(toXmlString(signRequest), StandardCharsets.UTF_8)); XMLSignature sig = new XMLSignature(doc, "", DEFAULT_SIGN_ALGORITHM, Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS); NodeList optionalInputsList = doc.getElementsByTagNameNS("urn:oasis:names:tc:dss:1.0:core:schema", "OptionalInputs"); Node optionalItemNode = optionalInputsList.item(0); optionalItemNode.appendChild(sig.getElement()); Transforms transforms = new Transforms(doc); transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE); sig.addDocument("", transforms, MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA256); sig.addKeyInfo(cert); sig.sign(privateKey); return documentToString(doc); } catch (Exception exception) { throw new RuntimeException(exception); } }
DssSignMessageService { public String signSignRequest(SignRequest signRequest) { try { PrivateKey privateKey = (PrivateKey) clientKeyStore.getKey(keystoreAlias, keystorePassword.toCharArray()); X509Certificate cert = (X509Certificate) clientKeyStore.getCertificate(keystoreAlias); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); Document doc = dbf.newDocumentBuilder().parse(IOUtils.toInputStream(toXmlString(signRequest), StandardCharsets.UTF_8)); XMLSignature sig = new XMLSignature(doc, "", DEFAULT_SIGN_ALGORITHM, Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS); NodeList optionalInputsList = doc.getElementsByTagNameNS("urn:oasis:names:tc:dss:1.0:core:schema", "OptionalInputs"); Node optionalItemNode = optionalInputsList.item(0); optionalItemNode.appendChild(sig.getElement()); Transforms transforms = new Transforms(doc); transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE); sig.addDocument("", transforms, MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA256); sig.addKeyInfo(cert); sig.sign(privateKey); return documentToString(doc); } catch (Exception exception) { throw new RuntimeException(exception); } } }
DssSignMessageService { public String signSignRequest(SignRequest signRequest) { try { PrivateKey privateKey = (PrivateKey) clientKeyStore.getKey(keystoreAlias, keystorePassword.toCharArray()); X509Certificate cert = (X509Certificate) clientKeyStore.getCertificate(keystoreAlias); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); Document doc = dbf.newDocumentBuilder().parse(IOUtils.toInputStream(toXmlString(signRequest), StandardCharsets.UTF_8)); XMLSignature sig = new XMLSignature(doc, "", DEFAULT_SIGN_ALGORITHM, Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS); NodeList optionalInputsList = doc.getElementsByTagNameNS("urn:oasis:names:tc:dss:1.0:core:schema", "OptionalInputs"); Node optionalItemNode = optionalInputsList.item(0); optionalItemNode.appendChild(sig.getElement()); Transforms transforms = new Transforms(doc); transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE); sig.addDocument("", transforms, MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA256); sig.addKeyInfo(cert); sig.sign(privateKey); return documentToString(doc); } catch (Exception exception) { throw new RuntimeException(exception); } } @Autowired  DssSignMessageService(DssMetadataService dssMetadataService); }
DssSignMessageService { public String signSignRequest(SignRequest signRequest) { try { PrivateKey privateKey = (PrivateKey) clientKeyStore.getKey(keystoreAlias, keystorePassword.toCharArray()); X509Certificate cert = (X509Certificate) clientKeyStore.getCertificate(keystoreAlias); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); Document doc = dbf.newDocumentBuilder().parse(IOUtils.toInputStream(toXmlString(signRequest), StandardCharsets.UTF_8)); XMLSignature sig = new XMLSignature(doc, "", DEFAULT_SIGN_ALGORITHM, Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS); NodeList optionalInputsList = doc.getElementsByTagNameNS("urn:oasis:names:tc:dss:1.0:core:schema", "OptionalInputs"); Node optionalItemNode = optionalInputsList.item(0); optionalItemNode.appendChild(sig.getElement()); Transforms transforms = new Transforms(doc); transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE); sig.addDocument("", transforms, MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA256); sig.addKeyInfo(cert); sig.sign(privateKey); return documentToString(doc); } catch (Exception exception) { throw new RuntimeException(exception); } } @Autowired  DssSignMessageService(DssMetadataService dssMetadataService); @PostConstruct void init(); String signSignRequest(SignRequest signRequest); ValidationResponse validateDssMessageSignature(String signResponseXmlString); }
DssSignMessageService { public String signSignRequest(SignRequest signRequest) { try { PrivateKey privateKey = (PrivateKey) clientKeyStore.getKey(keystoreAlias, keystorePassword.toCharArray()); X509Certificate cert = (X509Certificate) clientKeyStore.getCertificate(keystoreAlias); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); Document doc = dbf.newDocumentBuilder().parse(IOUtils.toInputStream(toXmlString(signRequest), StandardCharsets.UTF_8)); XMLSignature sig = new XMLSignature(doc, "", DEFAULT_SIGN_ALGORITHM, Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS); NodeList optionalInputsList = doc.getElementsByTagNameNS("urn:oasis:names:tc:dss:1.0:core:schema", "OptionalInputs"); Node optionalItemNode = optionalInputsList.item(0); optionalItemNode.appendChild(sig.getElement()); Transforms transforms = new Transforms(doc); transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE); sig.addDocument("", transforms, MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA256); sig.addKeyInfo(cert); sig.sign(privateKey); return documentToString(doc); } catch (Exception exception) { throw new RuntimeException(exception); } } @Autowired  DssSignMessageService(DssMetadataService dssMetadataService); @PostConstruct void init(); String signSignRequest(SignRequest signRequest); ValidationResponse validateDssMessageSignature(String signResponseXmlString); static final String DEFAULT_SIGN_ALGORITHM; }