I catch the error bellow when try to call an WS if ws-security.
Anybody knows what is wrong?

Exception in thread "main" org.codehaus.xfire.fault.XFireFault:
WSHandler: Encryption: error during message
processingorg.apache.ws.security.WSSecurityException: General security
error (Unexpected number of X509Data: for Encryption)
        at
org.codehaus.xfire.security.wss4j.WSS4JOutHandler.invoke(WSS4JOutHandler
.java:180)
        at
com.neogrid.integrator.components.ws.WSSecurityTest$SecurityHandler.invo
ke(WSSecurityTest.java:151)
        at
org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:1
31)
        at
org.codehaus.xfire.client.Invocation.invoke(Invocation.java:79)
        at
org.codehaus.xfire.client.Invocation.invoke(Invocation.java:114)
        at org.codehaus.xfire.client.Client.invoke(Client.java:336)
        at
com.neogrid.integrator.components.ws.WSSecurityTest.main(WSSecurityTest.
java:80)
Caused by: org.apache.ws.security.WSSecurityException: WSHandler:
Encryption: error during message
processingorg.apache.ws.security.WSSecurityException: General security
error (Unexpected number of X509Data: for Encryption)
        at
org.apache.ws.security.action.EncryptionAction.execute(EncryptionAction.
java:64)
        at
org.apache.ws.security.handler.WSHandler.doSenderAction(WSHandler.java:1
92)
        at
org.codehaus.xfire.security.wss4j.WSS4JOutHandler.invoke(WSS4JOutHandler
.java:158)

*************************
My code is:

import java.net.*;
import java.security.*;
import java.util.*;

import javax.wsdl.*;
import javax.wsdl.factory.*;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.protocol.*;
import org.apache.commons.io.*;
import org.apache.commons.ssl.*;
import org.apache.ws.security.*;
import org.apache.ws.security.handler.*;
import org.bouncycastle.jce.provider.*;
import org.codehaus.xfire.*;
import org.codehaus.xfire.addressing.*;
import org.codehaus.xfire.client.*;
import org.codehaus.xfire.security.wss4j.*;
import org.codehaus.xfire.service.*;
import org.codehaus.xfire.transport.http.*;
import org.codehaus.xfire.util.dom.*;
import org.xml.sax.*;

import com.neogrid.integrator.components.ws.handlers.*;

public class WSSecurityTest {

        private static final String wsdl =
"https://treinamento.bbmnet.com.br/BBM/BBM_WebService/Service.asmx?wsdl";
;
        private static final int sslPort = 443;
        
        private static String operation = "IncluirEditais";
        
        private static String wssUsernameToken = "rodrigo1";
        private static String wssPasswordToken = "rodrigo12";
        
        private static String certificateName = "dev_test.cer";
        private static String keyStoreName = "dev_test.jks";

        private static String certificate;
        private static String keyStore;
        private static String keyStorePass = "dev_test";
        private static String alias = "dev_test";

        public static void main(String args[]) throws Exception {
                URL cert =
ClassLoader.getSystemResource(certificateName);
                certificate = cert.getPath();

                URL ks = ClassLoader.getSystemResource(keyStoreName);
                keyStore = ks.getPath();
                
                System.setProperty("proxySet", "true");
                System.setProperty("proxyHost", "host");
                System.setProperty("proxyPort", "8080");
                System.setProperty("proxyUser", "user");
                System.setProperty("proxyPass", "pass");

                byte[] b =
IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("test.xml"));
                
                Client wsClient = createSecureClient();
                setupClientProxySettings(wsClient);
                
                wsClient.addOutHandler(new DOMOutHandler());
                wsClient.addOutHandler(new AddressingOutHandler());
                wsClient.addOutHandler(createWSSecurityHandler());

                
                Object[] operationArgs = new Object[] {new String(b)};
                OperationInfo opInfo =
wsClient.getService().getServiceInfo().getOperation(operation); 

                AddressingOperationInfo aoi = new
AddressingOperationInfo(opInfo); 
        
opInfo.setProperty(AddressingOperationInfo.ADDRESSING_OPERATION_KEY,
aoi);
                
                Object[] result = wsClient.invoke(opInfo,
operationArgs);
                
        }
        
        private static Client createSecureClient() throws Exception {
                
                Security.addProvider(new BouncyCastleProvider());
                HttpSecureProtocol protocolSocketFactory = new
HttpSecureProtocol();
                protocolSocketFactory.addTrustMaterial(new
TrustMaterial(certificate));
                Protocol trustHttps = new Protocol("https",
protocolSocketFactory, sslPort);
        
protocolSocketFactory.addTrustMaterial(TrustMaterial.TRUST_ALL);
                protocolSocketFactory.setKeyMaterial(new
KeyMaterial(keyStore, 
                                keyStorePass.toCharArray()));
                Protocol.registerProtocol("https", trustHttps);

                HttpClient httpclient = new HttpClient();
                GetMethod httpget = new GetMethod(wsdl);
                httpclient.executeMethod(httpget);
                
                InputSource src = new
InputSource(httpget.getResponseBodyAsStream());
                Definition def =
WSDLFactory.newInstance().newWSDLReader().readWSDL(null, src);

                return new Client(def, null);
        }
        
        private static void setupClientProxySettings(Client c) {
                if (Boolean.getBoolean("proxySet")) {
        
c.setProperty(CommonsHttpMessageSender.HTTP_PROXY_HOST,
System.getProperty("proxyHost"));
        
c.setProperty(CommonsHttpMessageSender.HTTP_PROXY_PORT,
System.getProperty("proxyPort"));
        
c.setProperty(CommonsHttpMessageSender.HTTP_PROXY_USER,
System.getProperty("proxyUser"));
        
c.setProperty(CommonsHttpMessageSender.HTTP_PROXY_PASS,
System.getProperty("proxyPass"));
                }
        }

        private static WSS4JOutHandler createWSSecurityHandler() {

                Properties config = new Properties();
                config.setProperty(WSHandlerConstants.ACTION,
WSHandlerConstants.USERNAME_TOKEN + 
                                " " + WSHandlerConstants.ENCRYPT);
                config.setProperty(WSHandlerConstants.PASSWORD_TYPE,
WSConstants.PW_TEXT);
                PasswordHandler.register(wssUsernameToken,
wssPasswordToken);

                config.setProperty(WSHandlerConstants.USER,
wssUsernameToken);
                config.setProperty(WSHandlerConstants.PW_CALLBACK_CLASS,
PasswordHandler.class.getName());

                SecurityHandler handler = new WSSecurityTest().new
SecurityHandler(config);
                return handler;         
        }

        public class SecurityHandler extends WSS4JOutHandler {

            public SecurityHandler(Map props) {
                super(props);
            }
                
            public void invoke(MessageContext mc) throws Exception {

                        Properties securityProps = new Properties();
        
securityProps.setProperty("org.apache.ws.security.crypto.provider",
"org.apache.ws.security.components.crypto.Merlin");
        
securityProps.setProperty("org.apache.ws.security.crypto.merlin.keystore
.type", "jks");
        
securityProps.setProperty("org.apache.ws.security.crypto.merlin.keystore
.password", keyStorePass);
        
securityProps.setProperty("org.apache.ws.security.crypto.merlin.alias.pa
ssword", keyStorePass);
        
securityProps.setProperty("org.apache.ws.security.crypto.merlin.keystore
.alias", alias);
        
securityProps.setProperty("org.apache.ws.security.crypto.merlin.file",
keyStore);
                        
                mc.setProperty("securityProps", securityProps);
                this.setProperty(WSHandlerConstants.ENC_PROP_REF_ID,
"securityProps");
                
                System.out.println(" ----- OUT MESSAGE:");
        
System.out.println(mc.getOutMessage().getBody().getClass().getName());
                
                super.invoke(mc);
                System.out.println(" ----- INVOKE FINISH:");
            }

        }
        
}

The certificate (dev_test.cer) I saved from the URL:
https://treinamento.bbmnet.com.br on the Internet Explorer.

The keystore (dev_test.jks) I generate with the commands:
keytool -genkey -dname "CN=Client machine name, OU=My Organisational
Unit, O=My Organisation, L=My Location, ST=My State, C=My Country"
-storepass dev_test -storetype JKS -keystore dev_test.jks -keyalg RSA
-keypass dev_test

keytool -import -alias dev_test -file dev_test.cer -keypass dev_test
-keystore dev_test.jks -storepass dev_test -storetype JKS

The test.xml file is a simple XML file.

Fabio Retzlaff
Desenvolvimento
[EMAIL PROTECTED]
Tel. +55 (47) 3802-8423
Cel. +55 (47) 9144-3712
www.neogrid.com.br




---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply via email to