cancel
Showing results for 
Search instead for 
Did you mean: 

Problem signing messages using S/MIME protocol in Java mapping

davelo
Discoverer
0 Kudos

Dear Experts,

I'm currently facing an issue while trying to sign my message using S/MIME protocol in my Java mapping while signing messages using XML and PKCS7 is working properly.

I'm running on PI 7.1 & NWDS 7.1 and the issue appears during the the following call:

SsfDataSMIME encryptData = new SsfDataSMIME(msg, session);

res = encryptData.sign(profile, "SHA",2 ,true);

the result of the sign function is always false.

In order to understand that result I decompiled the Java class hosting the sign methode and copied the code to my developement as a local function. This time when I call the sign methode the boolean is set to true and the structure of my Multipart message is changed to hold the signature.

The resulting message is not signed properly neither because the copied code makes a call to another function which at his turn returns always a wrong result, but no mather the return of that last call the structure of my initial message got changed when calling the sign method at the contrary of my initial call which prooves that my multipart message is well formed and that the parameters I send to the function are okay too.

Did anyone already achieve to sign a message using the S/MIME protocol ? I've found a previous post on sdn with exactly the same issue: http://forums.sdn.sap.com/thread.jspa?threadID=982256

Could you please provide support/recommendations on how to handle this issue ?

Thank you in advance for your replies.

Here is the code I used in NWDS:



package sample;

import java.io.*;
import com.sap.security.core.server.ssf.*;
import com.sap.aii.mapping.api.*;
import com.sap.engine.interfaces.keystore.*;
import javax.naming.*;
import java.security.*;
import java.util.Hashtable;
import javax.mail.internet.*;
import javax.mail.*;
import java.util.*;
//import javax.activation.*;
//import iaik.security.smime.*;


public class Certipost_SignMessage extends AbstractTransformation{

	  public void transform(TransformationInput arg0,TransformationOutput arg1)
			 throws StreamTransformationException {
		  this.execute(arg0.getInputPayload().getInputStream(),
				  	arg1.getOutputPayload().getOutputStream());
	  }

	  public void execute(InputStream in, OutputStream out)throws StreamTransformationException {
		//  open test data file
		  try {
			  InitialContext context = null;
			  SsfProfileKeyStore profile = null;
			
			  Hashtable env = new Hashtable();
			  env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sap.engine.services.jndi.InitialContextFactoryImpl");
			  env.put(Context.PROVIDER_URL, "xxxx:50704");
			  env.put("force_remote", "true");
			  env.put(Context.SECURITY_PRINCIPAL, "username");
			  env.put(Context.SECURITY_CREDENTIALS, "password");
			
			  try {
				  context = new InitialContext(env);
			  } catch (NamingException ne){
				  ne.printStackTrace();
			  }
			
			  KeystoreManager manager = (KeystoreManager) context.lookup("keystore");
			  KeyStore keyStore = manager.getKeystore("DEFAULT");
			  String alias = "XD4-SSL";

			  try {
				  profile = new SsfProfileKeyStore(keyStore, alias, null);
			  } catch (Exception e) {
				  e.printStackTrace();
				  throw new StreamTransformationException("Error while accessing keystore", e);
			  }
			  
			  SsfPabKeyStore pab = new SsfPabKeyStore(keyStore);
			  SsfSigRcpList signer = new SsfSigRcpList();
			  
			  Properties props = new Properties(); 
			  Session session = Session.getDefaultInstance(props, null);			  
		      
			  // the message to be parsed
			  MimeMessage msg = new MimeMessage(session);			  
			  MimeMultipart multipart = new MimeMultipart();
			  MimeBodyPart messageBodyPart = new MimeBodyPart();            
			  messageBodyPart.setText("this is a test");
			  multipart.addBodyPart(messageBodyPart);
			  msg.setContent(multipart);
			  
			  SsfDataSMIME encryptData = new SsfDataSMIME(msg, session);

			  // Sign the data
			  boolean res = false;
			  try {
				  //res = encryptData.sign(profile)
				  res = encryptData.sign(profile, "SHA",2 ,true);
			  } catch (SsfInvalidKeyException e) {
				  System.err.println("Error while signing data " + e);
			  }
			  if (!res) {
				  System.err.println("Creation of signature failed");
			  }

			  //encryptData.verify(pab,signer);
			  encryptData.writeTo(out);

		  } catch (Throwable t) {
			  t.printStackTrace();
		  }
	  	}
	  
	  public static void main(String[] args) {
		  try {
		      InputStream in = new FileInputStream(new File("in.txt"));
		      OutputStream out = new FileOutputStream(new File("out.txt"));
		      Certipost_SignMessage myMapping = new Certipost_SignMessage();
		      myMapping.execute(in, out);
		  } catch (Exception e) {
		      e.printStackTrace();
		  }
	  }
}

Accepted Solutions (0)

Answers (1)

Answers (1)

davelo
Discoverer
0 Kudos

Any hint how I could tackle this issue ?

Is it my multipart message which is not correctly formed ? Did I forget to initialize some parameters ? Is it an import conflict?

Regards,

David

0 Kudos

David,

Were you able to solve the problem?

I am facing the same Issue...

Best regards,

Adam