Skip to Content
0

JAVA Mapping Message Signature

Dec 01, 2017 at 11:38 AM

21

avatar image

Hello, guys.

We are trying to sign a message using java mapping. Unfortunately we are getting exception:

Java mapping com/delaware/generateSignature has thrown a StreamTransformationException.
Thrown:
com.sap.aii.mapping.api.StreamTransformationException: java.security.NoSuchAlgorithmException: ID21109: Remote call failed
at com.delaware.generateSignature.execute(generateSignature.java:119)
at com.delaware.generateSignature.transform(generateSignature.java:257)
at com.sap.aii.ib.server.mapping.execution.JavaMapping.executeStep(JavaMapping.java:112)
at com.sap.aii.ib.server.mapping.execution.Mapping.execute(Mapping.java:60)
at com.sap.aii.ib.server.mapping.execution.MappingHandler.map(MappingHandler.java:87)
at com.sap.aii.ib.server.mapping.execution.MappingHandler.map(MappingHandler.java:71)
at com.sap.aii.ibrun.sbeans.mapping.MappingRequestHandler.handleMappingRequest(MappingRequestHandler.java:123)
at com.sap.aii.ibrun.sbeans.mapping.MappingRequestHandler.handleRequest(MappingRequestHandler.java:73)
at com.sap.aii.ibrun.sbeans.mapping.MappingServiceImpl.processFunction(MappingServiceImpl.java:79)
at sun.reflect.GeneratedMethodAccessor2065.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sap.engine.services.ejb3.runtime.impl.RequestInvocationContext.proceedFinal(RequestInvocationContext.java:47)
at com.sap.engine.services.ejb3.runtime.impl.AbstractInvocationContext.proceed(AbstractInvocationContext.java:166)
at com.sap.engine.services.ejb3.runtime.impl.Interceptors_StatesTransition.invoke(Interceptors_StatesTransition.java:19)
at com.sap.engine.services.ejb3.runtime.impl.AbstractInvocationContext.proceed(AbstractInvocationContext.java:179)
at com.sap.engine.services.ejb3.runtime.impl.Interceptors_Resource.invoke(Interceptors_Resource.java:50)
at com.sap.engine.services.ejb3.runtime.impl.AbstractInvocationContext.proceed(AbstractInvocationContext.java:179)
at com.sap.engine.services.ejb3.runtime.impl.Interceptors_Transaction.doWorkWithAttribute(Interceptors_Transaction.java:37)
at com.sap.engine.services.ejb3.runtime.impl.Interceptors_Transaction.invoke(Interceptors_Transaction.java:21)
at com.sap.engine.services.ejb3.runtime.impl.AbstractInvocationContext.proceed(AbstractInvocationContext.java:179)
at com.sap.engine.services.ejb3.runtime.impl.Interceptors_MethodRetry.invoke(Interceptors_MethodRetry.java:46)
at com.sap.engine.services.ejb3.runtime.impl.AbstractInvocationContext.proceed(AbstractInvocationContext.java:179)
at com.sap.engine.services.ejb3.runtime.impl.AbstractInvocationContext.proceed(AbstractInvocationContext.java:191)
at com.sap.engine.services.ejb3.runtime.impl.Interceptors_StatelessInstanceGetter.invoke(Interceptors_StatelessInstanceGetter.java:23)
at com.sap.engine.services.ejb3.runtime.impl.AbstractInvocationContext.proceed(AbstractInvocationContext.java:179)
at com.sap.engine.services.ejb3.runtime.impl.Interceptors_SecurityCheck.invoke(Interceptors_SecurityCheck.java:25)
at com.sap.engine.services.ejb3.runtime.impl.AbstractInvocationContext.proceed(AbstractInvocationContext.java:179)
at com.sap.engine.services.ejb3.runtime.impl.Interceptors_ExceptionTracer.invoke(Interceptors_ExceptionTracer.java:17)
at com.sap.engine.services.ejb3.runtime.impl.AbstractInvocationContext.proceed(AbstractInvocationContext.java:179)
at com.sap.engine.services.ejb3.runtime.impl.DefaultInvocationChainsManager.startChain(DefaultInvocationChainsManager.java:138)
at com.sap.engine.services.ejb3.runtime.impl.DefaultEJBProxyInvocationHandler.invoke(DefaultEJBProxyInvocationHandler.java:172)
at com.sap.engine.services.ejb3.runtime.impl.DefaultEJBProxyInvocationHandler.invoke(DefaultEJBProxyInvocationHandler.java:99)
at com.sun.proxy.$Proxy696.processFunction(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sap.engine.services.rfcengine.RFCDefaultRequestHandler.handleRequest(RFCDefaultRequestHandler.java:214)
at com.sap.engine.services.rfcengine.RFCJCOServer$J2EEApplicationRunnable.run(RFCJCOServer.java:214)
at com.sap.engine.core.thread.impl3.ActionObject.run(ActionObject.java:37)
at java.security.AccessController.doPrivileged(Native Method)
at com.sap.engine.core.thread.impl3.SingleThread.execute(SingleThread.java:185)
at com.sap.engine.core.thread.impl3.SingleThread.run(SingleThread.java:302)

Our code is:

publicclass generateSignature extends AbstractTransformation {

publicvoid execute(InputStream in, OutputStream out) throws StreamTransformationException {

String toRemove = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>";

try {

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder dbuilder = factory.newDocumentBuilder();

;

Document docin = dbuilder.parse(in);

Document docout = dbuilder.newDocument();

TransformerFactory tf = TransformerFactory.newInstance();

Transformer transform = tf.newTransformer();

InitialContext context = null;

try {

context = new InitialContext();

} catch (NamingException ex) {

getTrace().addInfo(ex.getMessage());

ex.printStackTrace();

}

KeystoreManager manager = null;

try {

manager = (KeystoreManager) context.lookup("keystore");

} catch (NamingException ex) {

getTrace().addInfo(ex.getMessage());

ex.printStackTrace();

}

KeyStore ks = null;

try {

ks = manager.getKeystore("NFE");

} catch (RemoteException ex) {

getTrace().addInfo(ex.getMessage());

ex.printStackTrace();

}

PrivateKey privatekey = (PrivateKey) ks.getKey("Leo", "".toCharArray());

String sDocin = generateDigitalSignature(docin, privatekey);

sDocin = sDocin.replace(toRemove, " ");

Text xmlIn = docout.createTextNode(sDocin);

Element root = docout.createElement("ReceberLoteEventos");

root.setAttribute("xmlns:ns1", "http://sped.fazenda.gov.br");

docout.appendChild(root);

Element loteEventos = docout.createElement("loteEventos");

root.appendChild(loteEventos);

loteEventos.appendChild(xmlIn);

Source source = new DOMSource(docout);

StreamResult result = new StreamResult(out);

transform.transform(source, result);

} catch (Exception exception) {

getTrace().addDebugMessage(exception.getMessage());

thrownew StreamTransformationException(exception.toString());

}

}

@SuppressWarnings("unused")

private String getSignature() {

String signature = "";

String view = "NFE";

String key = "Leo";

try {

getTrace().addInfo("Start Certificate Read...");

InitialContext context = new InitialContext();

KeystoreManager manager = (KeystoreManager) context.lookup("keystore");

if (!manager.existKeystoreView(view)) {

String[] aliases = manager.getKeystoreViewAliases();

for (String s : aliases) {

getTrace().addInfo(s);

}

}

KeyStore ks = manager.getKeystore(view);

if (!ks.containsAlias(key)) {

Enumeration<String> test = ks.aliases();

while (test.hasMoreElements()) {

String string = (String) test.nextElement();

getTrace().addInfo(string);

}

}

PrivateKey privatekey = (PrivateKey) ks.getKey(key, "".toCharArray());

if (privatekey == null) {

getTrace().addWarning("Certificate not found");

} else {

signature = Base64.encodeToString(privatekey.toString().getBytes(), true);

getTrace().addInfo(signature);

getTrace().addInfo(privatekey.getAlgorithm());

}

} catch (Exception e) {

e.printStackTrace();

}

returnsignature;

}

private String convertDocumentToString(Document doc) {

String output = "";

TransformerFactory tf = TransformerFactory.newInstance();

Transformer transformer;

try {

transformer = tf.newTransformer();

// below code to remove XML declaration

// transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,

// "yes");

StringWriter writer = new StringWriter();

transformer.transform(new DOMSource(doc), new StreamResult(writer));

output = writer.getBuffer().toString();

} catch (TransformerException e) {

e.printStackTrace();

}

returnoutput;

}

@SuppressWarnings("unused")

private Document convertStringToDocument(String xmlStr) {

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder;

try {

builder = factory.newDocumentBuilder();

Document doc = builder.parse(new InputSource(new StringReader(xmlStr)));

returndoc;

} catch (Exception e) {

e.printStackTrace();

}

returnnull;

}

private String generateDigitalSignature(Document docXML, PrivateKey privateKey) throws NoSuchAlgorithmException,

InvalidAlgorithmParameterException, IOException, KeyException, TransformerException,

javax.xml.crypto.MarshalException, InstantiationException, IllegalAccessException, ClassNotFoundException {

XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");

SignedInfo signedInfo = null;

DOMSignContext si = new DOMSignContext(privateKey, docXML.getDocumentElement());

try {

Reference ref = fac.newReference("", fac.newDigestMethod(DigestMethod.SHA1, null),

Collections.singletonList(fac.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)),

null, null);

signedInfo = fac.newSignedInfo(

fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS,

(C14NMethodParameterSpec) null),

fac.newSignatureMethod(SignatureMethod.DSA_SHA1, null), Collections.singletonList(ref));

} catch (NoSuchAlgorithmException ex) {

getTrace().addWarning("Error at NoSuch");

getTrace().addInfo(ex.getMessage());

ex.printStackTrace();

} catch (InvalidAlgorithmParameterException ex) {

getTrace().addInfo(ex.getMessage());

ex.printStackTrace();

}

KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");

kpg.initialize(512);

KeyPair kp = kpg.generateKeyPair();

KeyInfoFactory kif = fac.getKeyInfoFactory();

KeyValue kv = kif.newKeyValue(kp.getPublic());

KeyInfo ki = kif.newKeyInfo(Collections.singletonList(kv));

XMLSignature xmlSignature = fac.newXMLSignature(signedInfo, ki);

try {

xmlSignature.sign(si);

} catch (XMLSignatureException ex) {

getTrace().addDebugMessage(ex.getMessage());

ex.printStackTrace();

}

return convertDocumentToString(docXML);

}

We do not have great knowledge in java. Do you have any idea why or what is causing the exception?

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

0 Answers