Title: Re: [WSS4J 1.5] CryptoFactory: Cannot load properties: crypto.properties

Hi Ruchith Fernando,

i've solved the problem I've attached below,I was wrong to create classpath, I've solved another problem of wrong key size but now there is a simple problem to resolve ;(
When I load crypto.properties and when I use this method signer.setUserInfo(alias, password) I do it without knowing the alias of my certificate and in effects it returns this message

org.apache.ws.security.WSSecurityException: General security error (Unexpected number of X509Data: for Signature)
        at org.apache.ws.security.message.WSSecSignature.prepare(WSSecSignature.java:274)
        at org.apache.ws.security.message.WSSecSignature.build(WSSecSignature.java:643)
        at Perform.signer.main(signer.java:93)

because the keystore pkcs12 was given me from a CA and I don't know the alias of cert, the class is ok because if I load another keystore that I create and I know alias of cert it works very well.
Now I suppose that the probelm is inherent to alias of my pkcs12, isn't it??

Thank You for your precious help

I attach code of my class:

package Perform;

import java.io.ByteArrayInputStream;
import java.util.Vector;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;
import org.apache.axis.utils.*;
import org.apache.axis.Message;
import org.apache.axis.message.SOAPEnvelope;
import org.apache.ws.security.SOAPConstants;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.WSEncryptionPart;
import org.apache.ws.security.components.crypto.CryptoFactory;
import org.apache.ws.security.message.WSSecHeader;
import org.apache.ws.security.message.WSSecSignature;
import org.apache.ws.security.util.WSSecurityUtil;
import org.apache.xml.security.c14n.Canonicalizer;
import org.w3c.dom.Document;


public class signer {
       
        public static void main(String arg[]){
               
                try{
                       
                        Message signedSOAPMsg=null;
                       
                        //Crea message factory
                        MessageFactory messageFactory = MessageFactory.newInstance();

                    // Creazione di un messaggio
                   
                    SOAPMessage message = messageFactory.createMessage();
                        SOAPEnvelope env= new SOAPEnvelope();
                   
                    Message axisMessage = new Message(env,null);
                        SOAPEnvelope unsignedEnvelope = axisMessage.getSOAPEnvelope();
                        Document doc = unsignedEnvelope.getAsDocument();
               
                        // WSSignEnvelope signs a SOAP envelope according to the
                        // WS Specification (X509 profile) and adds the signature data
                        // to the envelope.
                        WSSecSignature signer = new WSSecSignature();
                          
                        String alias = "";
                        String password = "1234567890";
                        signer.setUserInfo(alias, password);
                                 
                        // create a vector of WSEncryptPart parts to sign, both the soap body
                    //and the attachments
                        SOAPConstants soapConstants =WSSecurityUtil.getSOAPConstants(unsignedEnvelope);
                                   
                        Vector parts = new Vector();
                         
                        // add the body part
                        String localPart = soapConstants.getBodyQName().getLocalPart();
                        String envelopeURI = soapConstants.getEnvelopeURI();        
                        WSEncryptionPart body = new WSEncryptionPart(localPart, envelopeURI, "Content");
                         
                        parts.add(body);
                                 
                        // how to add the attachment part?????        
                        signer.setParts(parts);
                                 
                        // The "build" method, creates the signed SOAP envelope.
                        // It takes a SOAP Envelope as a W3C Document and adds
                        // a WSS Signature header to it. The signed elements
                        // depend on the signature parts that are specified by
                        // the WSBaseMessage.setParts(java.util.Vector parts)
                        // method. By default, SOAP Body is signed.
                        // The "crypto" parameter is the object that implements
                        // access to the keystore and handling of certificates.
                        // A default implementation is included:
                        // org.apache.ws.security.components.crypto.Merlin
                         
                        WSSecHeader secHeader = new WSSecHeader();
                secHeader.insertSecurityHeader(doc);
                        Document signedDoc = signer.build(doc, CryptoFactory.getInstance(),secHeader);
                       
                        // Convert the signed document into a SOAP message.
                        signedSOAPMsg =  (Message) toSOAPMessage(signedDoc);
                        System.out.println(signedSOAPMsg);
                        } catch (Exception e) {
                          e.printStackTrace();
                        }
           }
       
        public static SOAPMessage toSOAPMessage(Document doc) throws Exception {
                Canonicalizer c14n =Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS);
                byte[] canonicalMessage = c14n.canonicalizeSubtree(doc);
                ByteArrayInputStream in = new ByteArrayInputStream(canonicalMessage);
                MessageFactory factory = MessageFactory.newInstance();
                return factory.createMessage(null, in);
       
        }
}

and crypto.properties:

org.apache.ws.security.crypto.provider=org.apache.ws.security.components.crypto.Merlin
org.apache.ws.security.crypto.merlin.keystore.type=pkcs12
org.apache.ws.security.crypto.merlin.keystore.password=1234567890
org.apache.ws.security.crypto.merlin.keystore.alias=
org.apache.ws.security.crypto.merlin.alias.password=1234567890
org.apache.ws.security.crypto.merlin.file=keys/x509pri.p12

-----Messaggio originale-----
Da: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
Inviato: lun 12/06/2006 19.18
A: Ruchith Fernando
Cc: [email protected]
Oggetto: R: R: [WSS4J 1.5] CryptoFactory: Cannot load properties: crypto.properties


> p.s. I'd really appreciate if you ask questions in the mailing list
> :-) This way a lot of others will learn from your experience as well

sorry for this when i reply i forgot to add Cc ;(

i do all your splendid info but the result is this:

java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.apache.ws.security.components.crypto.CryptoFactory.loadClass(CryptoFactory.java:142)
        at org.apache.ws.security.components.crypto.CryptoFactory.loadClass(CryptoFactory.java:119)
        at org.apache.ws.security.components.crypto.CryptoFactory.getInstance(CryptoFactory.java:101)
        at org.apache.ws.security.components.crypto.CryptoFactory.getInstance(CryptoFactory.java:54)
        at Perform.signer.main(signer.java:100)
Caused by: java.lang.NullPointerException: name
        at java.util.zip.ZipFile.getEntry(Unknown Source)
        at java.util.jar.JarFile.getEntry(Unknown Source)
        at java.util.jar.JarFile.getJarEntry(Unknown Source)
        at sun.misc.URLClassPath$JarLoader.getResource(Unknown Source)
        at sun.misc.URLClassPath.getResource(Unknown Source)
        at sun.misc.URLClassPath.getResource(Unknown Source)
        at java.lang.ClassLoader.getBootstrapResource(Unknown Source)
        at java.lang.ClassLoader.getResource(Unknown Source)
        at java.lang.ClassLoader.getResource(Unknown Source)
        at org.apache.commons.discovery.jdk.JDK12Hooks.getResources(JDK12Hooks.java:149)
        at org.apache.commons.discovery.resource.DiscoverResources$1.getNextResources(DiscoverResources.java:153)
        at org.apache.commons.discovery.resource.DiscoverResources$1.getNextResource(DiscoverResources.java:129)
        at org.apache.commons.discovery.resource.DiscoverResources$1.hasNext(DiscoverResources.java:116)
        at org.apache.ws.security.components.crypto.AbstractCrypto.<init>(AbstractCrypto.java:101)
        at org.apache.ws.security.components.crypto.Merlin.<init>(Merlin.java:62)
        ... 9 more
java.lang.InstantiationException: org.apache.ws.security.components.crypto.Merlin
        at java.lang.Class.newInstance0(Unknown Source)
        at java.lang.Class.newInstance(Unknown Source)
        at org.apache.ws.security.components.crypto.CryptoFactory.loadClass(CryptoFactory.java:150)
        at org.apache.ws.security.components.crypto.CryptoFactory.loadClass(CryptoFactory.java:119)
        at org.apache.ws.security.components.crypto.CryptoFactory.getInstance(CryptoFactory.java:101)
        at org.apache.ws.security.components.crypto.CryptoFactory.getInstance(CryptoFactory.java:54)
        at Perform.signer.main(signer.java:100)
java.lang.RuntimeException: org.apache.ws.security.components.crypto.Merlin cannot create instance
        at org.apache.ws.security.components.crypto.CryptoFactory.loadClass(CryptoFactory.java:155)
        at org.apache.ws.security.components.crypto.CryptoFactory.loadClass(CryptoFactory.java:119)
        at org.apache.ws.security.components.crypto.CryptoFactory.getInstance(CryptoFactory.java:101)
        at org.apache.ws.security.components.crypto.CryptoFactory.getInstance(CryptoFactory.java:54)
        at Perform.signer.main(signer.java:100)
- Unable to instantiate (1): org.apache.ws.security.components.crypto.Merlin
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.apache.ws.security.components.crypto.CryptoFactory.loadClass(CryptoFactory.java:142)
        at org.apache.ws.security.components.crypto.CryptoFactory.loadClass(CryptoFactory.java:119)
        at org.apache.ws.security.components.crypto.CryptoFactory.getInstance(CryptoFactory.java:101)
        at org.apache.ws.security.components.crypto.CryptoFactory.getInstance(CryptoFactory.java:54)
        at Perform.signer.main(signer.java:100)
Caused by: java.lang.NullPointerException: name
        at java.util.zip.ZipFile.getEntry(Unknown Source)
        at java.util.jar.JarFile.getEntry(Unknown Source)
        at java.util.jar.JarFile.getJarEntry(Unknown Source)
        at sun.misc.URLClassPath$JarLoader.getResource(Unknown Source)
        at sun.misc.URLClassPath.getResource(Unknown Source)
        at sun.misc.URLClassPath.getResource(Unknown Source)
        at java.lang.ClassLoader.getBootstrapResource(Unknown Source)
        at java.lang.ClassLoader.getResource(Unknown Source)
        at java.lang.ClassLoader.getResource(Unknown Source)
        at org.apache.commons.discovery.jdk.JDK12Hooks.getResources(JDK12Hooks.java:149)
        at org.apache.commons.discovery.resource.DiscoverResources$1.getNextResources(DiscoverResources.java:153)
        at org.apache.commons.discovery.resource.DiscoverResources$1.getNextResource(DiscoverResources.java:129)
        at org.apache.commons.discovery.resource.DiscoverResources$1.hasNext(DiscoverResources.java:116)
        at org.apache.ws.security.components.crypto.AbstractCrypto.<init>(AbstractCrypto.java:101)
        at org.apache.ws.security.components.crypto.Merlin.<init>(Merlin.java:62)
        ... 9 more
- Unable to instantiate (2): org.apache.ws.security.components.crypto.Merlin
java.lang.InstantiationException: org.apache.ws.security.components.crypto.Merlin
        at java.lang.Class.newInstance0(Unknown Source)
        at java.lang.Class.newInstance(Unknown Source)
        at org.apache.ws.security.components.crypto.CryptoFactory.loadClass(CryptoFactory.java:150)
        at org.apache.ws.security.components.crypto.CryptoFactory.loadClass(CryptoFactory.java:119)
        at org.apache.ws.security.components.crypto.CryptoFactory.getInstance(CryptoFactory.java:101)
        at org.apache.ws.security.components.crypto.CryptoFactory.getInstance(CryptoFactory.java:54)
        at Perform.signer.main(signer.java:100)


How it's possible? Where i mistake

Thanks a lot for your answer




-----Messaggio originale-----
Da: Ruchith Fernando [mailto:[EMAIL PROTECTED]]
Inviato: lun 12/06/2006 18.29
A: [EMAIL PROTECTED]
Oggetto: Re: R: [WSS4J 1.5] CryptoFactory: Cannot load properties: crypto.properties

>   thank you for your help, sorry if i ask you stupid things but it's the
> first time i use this class and for my bad english.

No problem at all :-)

>  Iin effect i hava a pkcs12 keystore and i want to add its certificate to
> soap message and sign it with private key the problem is that for the moment
> i use eclipse and i don't know where set classpath
>  Can you help me to do this??


First you can create a dir (lets call it "temp") and create a dir by
the name "keys" in it and copy the PKCS12 keystore file into the
"keys" dir.

In eclipse you can
 - Right click on the project -> select properties
 - Select "Java Build Path" -> Libraries

This "Libraries" tab is is where you can add directories and jars to
your classpath. Use "Add class folder" button to add the "temp" dir we
created to  the classpath.

Now copy the crypto.properties file into the "src" dir of the eclipse
project. (into any of the dirs listed in Project->Properties->Java
Build Path ->Source)

Thanks,
Ruchith

p.s. I'd really appreciate if you ask questions in the mailing list
:-) This way a lot of others will learn from your experience as well


Reply via email to