Skip to Content
0
Jun 24, 2019 at 09:01 AM

Need help in Java mapping for signing the XML. Digital signing

528 Views

Hello Experts,

i am trying to sign the XML using the SSL certificate stored in keystore. i am trying to invoke the certificate using the below code.but it always giving the nullPointer Exception when i try to trace the loaded certificate.

package com.nets.e2e; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.security.InvalidKeyException; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.Security; import java.security.Signature; import java.security.UnrecoverableEntryException; import java.security.cert.Certificate; import java.security.cert.X509Certificate; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.apache.commons.codec.binary.Base64; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.xml.sax.SAXException; import com.sap.aii.mapping.api.AbstractTrace; import com.sap.aii.mapping.api.AbstractTransformation; import com.sap.aii.mapping.api.MappingTrace; import com.sap.aii.mapping.api.StreamTransformationConstants; import com.sap.aii.mapping.api.StreamTransformationException; import com.sap.aii.mapping.api.TransformationInput; import com.sap.aii.mapping.api.TransformationOutput; import com.sap.aii.security.lib.KeyStoreManager; import com.sap.security.api.ssf.ISsfData; import com.sap.security.api.ssf.ISsfProfile; import com.sap.security.core.server.ssf.SsfDataPKCS7; import com.sap.security.core.server.ssf.SsfInvalidKeyException; import com.sap.security.core.server.ssf.SsfProfileKeyStore; import com.sap.aii.af.service.resource.SAPSecurityResources; public class XadesSignature extends AbstractTransformation{ @SuppressWarnings("deprecation") public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput) throws StreamTransformationException { try{ InputStream inputstream = transformationInput.getInputPayload().getInputStream(); OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream(); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(inputstream); byte[] b = new byte[inputstream.available()]; inputstream.read(b); ISsfData data = new SsfDataPKCS7(inputstream); getTrace().addInfo("ISsfdata Object Created"); KeyStoreManager manager=null; manager = com.sap.aii.af.service.resource.SAPSecurityResources.getInstance().getKeyStoreManager( com.sap.aii.security.lib.PermissionMode.SYSTEM_LEVEL); KeyStore keyStore = manager.getKeyStore("ESigning_API_SSL");

SsfProfileKeyStore profile = new SsfProfileKeyStore(keyStore,"ESigning_API_SSL" , null); //getTrace().addInfo(profile.getCertificate().getSignature().toString()); //profile = manager.getISsfProfile(keyStore,"ESigning_API_SSL" , null); getTrace().addInfo(profile.toString()); data.sign(profile); ByteArrayOutputStream baos = new ByteArrayOutputStream(); data.writeTo(baos); String signedString = new String(baos.toByteArray()); doc.getElementsByTagName("TrustB2BMessage").item(0).setTextContent(signedString); doc.getDocumentElement().normalize(); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(outputstream); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(source, result); } catch (KeyStoreException | SsfInvalidKeyException | IOException | ParserConfigurationException | SAXException | TransformerException ex) { ex.printStackTrace(); } getTrace().addInfo("End of Program"); } }