Oppure

Loading
10/06/06 9:43
ermasto
ok ci sono riuscito avevo un errore nel codice e non me ne ero accorto.

ora un altro passo che devo fare è il seguente:

devo creare un messaggio soap e aggiungere ad esso un security token che è il certificato x509 e poi tutto questo messaggio soap lo devo firmare con la chiave privata che appartiene al certificato

Per quanto riguarda la creazione del messaggio soap non ci sono problemi ma non sono riuscito a trovare nulla su come aggiungere il token x509, ho letto in giro che devo usare wss4j ma non ho trovato una buona guida e cercare in tutte le API è uno sbattimento assurdo, che tu sappia esiste una guida o qualche sito che lo spiega meglio magari con qualche esempio??

Grazie
aaa
10/06/06 12:03
netarrow
non saprei aiutarti.
aaa
11/06/06 18:48
ermasto
allora ragazzi ho trovato un metodo che fa al caso mio per firmare un messaggio soap.

Il metodo è il seguente:

[QUOTE]
**
* Sign a soap message.
* We wish to sign both the body and the attachment
* <p/>
*
* @param msg - SOAPMessage object to sign - there is one attachment
present
* @param msgStream - the SOAPMessage represented as an input stream
* @return new Signed SOAPMessage
* @throws Exception
*/

private SOAPMessage signSOAPMessage(SOAPMessage msg, InputStream msgStream) {
            
             Message signedSOAPMsg=null;
             Iterator attachmentsIter=null;
             FileInputStream attachmentStream = null;
            
             try {
             // create an AxisMessage from the SOAPMessage InputStream
             // pass false for arg2 as the msgStream contains the ENTIRE message.
             Message axisMessage = new Message(msgStream, false,msg.getMimeHeaders());
             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 = "16c73ab6-b892-458f-abf5-2f875f74882e";
             String password = "security";
             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
            
             Document signedDoc = signer.build(doc, CryptoFactory.getInstance(),null);
            
// Convert the signed document into a SOAP message.
             signedSOAPMsg = (Message) toSOAPMessage(signedDoc);
            
             } catch (Exception e) {
             e.printStackTrace();
             }
             return signedSOAPMsg;
             }
[/QUOTE]

allora io ho costruito la parte rimanente di codice per farlo funzionare, eccola qua:

[QUOTE]
public class sign {
    public static void main(String[] args) {
try {
    
// Crea message factory
MessageFactory messageFactory = MessageFactory.newInstance();

// Creazione di un messaggio
SOAPMessage message = messageFactory.createMessage();

// Creazione attachment part per il certificcato
URL url = new URL("file:/…;);
DataHandler dataHandler = new DataHandler(url);
AttachmentPart attachment = message.createAttachmentPart(dataHandler);
attachment.setContentId("Certificato";);
message.addAttachmentPart(attachment);
int i=message.countAttachments();
System.out.println("Numero Allegati al messaggio:"+i);

InputStream inStream = new FileInputStream([B]cosa ci metto qui??[/B]);
inStream.close();

[B] questa è la chiamata a metodo
sign mess = null;
mess.signSOAPMessage(message,inStream);
[/B]
} catch (IOException e) {
System.out.println("I/O exception: " + e.toString());
System.exit(1);
} catch (Exception ex) {
ex.printStackTrace();
}
}
[/QUOTE]

ho creato un messaggio soap e aggiunto un allegato
il problema sorge quando nel main vado a richiamare il metodo che come vedo accetta 2 parametri il messaggio soap e fin qui ci siamo e poi accetta sempre il messaggio soap ma stavolta come [B]inputstream[/B] e questo è il mio problema come glielo passo??
aaa
12/06/06 16:01
netarrow
Guardando qui:
ws.apache.org/axis/java/apiDocs/org/apache/axis/…

e in particolare qui:

ws.apache.org/axis/java/apiDocs/org/apache/axis/…(java.lang.Object,%20boolean,%20javax.xml.soap.MimeHeaders)

Ho trovato il significato dei tre parametri:
(mi riferisco al costruttore di Message dentro il metodo)

initialContents - may be String, byte[], InputStream, SOAPEnvelope, or AxisFault.

bodyInStream - is true if initialContents is an InputStream containing just the SOAP body (no SOAP-ENV).

headers - Mime Headers.

Spero ti possa servire.

Prova a passarci all'nput stream il nome del file certificato, file:/…; non sono sicuro ma prova.

ciao
Ultima modifica effettuata da netarrow 12/06/06 16:02
aaa
16/06/06 17:57
ermasto
grazie mille dei consigli sono riuscito a creare un metodo che firma il messaggio ma ora ho sempre un problema con il keystore pkcs12 e cioè questo metodo per fimare il messaggio soap deve conoscere l'alias del certificato contenente nel keystore e se uso un keystore creato da me tutto funziona perfettamente perche so che alias ho messo ma i keystore che uso non sono stati creati da me ma dalla CA e non so che alias ha inserito c'è qualche metodo che mi fa risalire all' alias del certificato nel keystore??

Grazie mille
aaa
17/06/06 12:04
netarrow
Ho guardato sul libro "Sicurezza in Java" e l'alias viene inserito dall'utente, cmq dovrebbe funzionare il metodo per ottenere la lista di alias presenti nel keystore, ora cerco.
aliasses() sembra la cosa giusta, l'errore forse te lo da dopo.
Ultima modifica effettuata da netarrow 17/06/06 12:13
aaa
17/06/06 12:17
netarrow
cmq per l'errore

UnrecoverableKeyException

sulla documentazione java danno questa piegazione:

UnrecoverableKeyException - if the key cannot be recovered (e.g., the given password is wrong).
aaa
31/12/07 13:52
manu
Salve,
anche io ho lo stesso errore:

"byte[] encodedCert = cert.getEncoded();
mi dice:
Exception in thread "main" java.lang.NullPointerException
at Perform.key.main(key.java:43) "

Mi potete indicare il codice corretto????

Vi ringrazio in anticipo
aaa