Package org.apache.ws.security.message

Examples of org.apache.ws.security.message.WSEncryptBody


     *
     * @param doc
     */
    //TODO :: Change the method signature.
    public void doEncryptProof(Document doc, Crypto crypto, String userInfo) throws WSSecurityException {
        WSEncryptBody wsEncrypt = new WSEncryptBody();
        try {
            wsEncrypt.setUserInfo(userInfo);
            wsEncrypt.setKeyIdentifierType(WSConstants.BST_DIRECT_REFERENCE);
            wsEncrypt.setParentNode(this.element);
            if (this.sharedSecret != null) {
                //wsEncrypt.setSymmetricKey(WSSecurityUtil.prepareSecretKey(WSConstants.TRIPLE_DES, this.sharedSecret));//TODO
            }
//            wsEncrypt.setParentNode(
//                (Element) (doc
//                    .getElementsByTagNameNS(
//                        WSConstants.WSSE_NS,
//                        "RequestedProofToken")
//                    .item(0)));
            wsEncrypt.build(doc, crypto);
            this.sharedSecret = wsEncrypt.getEncryptionKey().getEncoded();
        } catch (WSSecurityException e) {
            e.printStackTrace();
        }
        log.debug("RequestedProofToken :: Decryption Done");
    }
View Full Code Here


        }
    }

    protected void performENCRAction(int actionToDo, boolean mu, Document doc, RequestData reqData)
            throws WSSecurityException {
        WSEncryptBody wsEncrypt = new WSEncryptBody(reqData.getActor(), mu);
        if (reqData.getEncKeyId() != 0) {
            wsEncrypt.setKeyIdentifierType(reqData.getEncKeyId());
        }
        if (reqData.getEncKeyId() == WSConstants.EMBEDDED_KEYNAME) {
            String encKeyName = null;
            if ((encKeyName =
                    (String) getOption(WSHandlerConstants.ENC_KEY_NAME))
                    == null) {
                encKeyName =
                        (String) getProperty(reqData.getMsgContext(), WSHandlerConstants.ENC_KEY_NAME);
            }
            wsEncrypt.setEmbeddedKeyName(encKeyName);
            byte[] embeddedKey =
                    getPassword(reqData.getEncUser(),
                            actionToDo,
                            WSHandlerConstants.ENC_CALLBACK_CLASS,
                            WSHandlerConstants.ENC_CALLBACK_REF, reqData)
                    .getKey();
            wsEncrypt.setKey(embeddedKey);
        }
        if (reqData.getEncSymmAlgo() != null) {
            wsEncrypt.setSymmetricEncAlgorithm(reqData.getEncSymmAlgo());
        }
        if (reqData.getEncKeyTransport() != null) {
            wsEncrypt.setKeyEnc(reqData.getEncKeyTransport());
        }
        wsEncrypt.setUserInfo(reqData.getEncUser());
        wsEncrypt.setUseThisCert(reqData.getEncCert());
        if (reqData.getEncryptParts().size() > 0) {
            wsEncrypt.setParts(reqData.getEncryptParts());
        }
        try {
            wsEncrypt.build(doc, reqData.getEncCrypto());
        } catch (WSSecurityException e) {
            throw new WSSecurityException("WSHandler: Encryption: error during message processing"
                    + e);
        }
    }
View Full Code Here

     *
     * @param doc
     */
    //TODO :: Change the method signature.
    public void doEncryptProof(Document doc, Crypto crypto, String userInfo) throws WSSecurityException {
        WSEncryptBody wsEncrypt = new WSEncryptBody();
        try {
            wsEncrypt.setUserInfo(userInfo);
            wsEncrypt.setKeyIdentifierType(WSConstants.BST_DIRECT_REFERENCE);
            wsEncrypt.setParentNode(this.element);
            if (this.sharedSecret != null) {
                //wsEncrypt.setSymmetricKey(WSSecurityUtil.prepareSecretKey(WSConstants.TRIPLE_DES, this.sharedSecret));//TODO
            }
//            wsEncrypt.setParentNode(
//                (Element) (doc
//                    .getElementsByTagNameNS(
//                        WSConstants.WSSE_NS,
//                        "RequestedProofToken")
//                    .item(0)));
            wsEncrypt.build(doc, crypto);
            this.sharedSecret = wsEncrypt.getEncryptionKey().getEncoded();
        } catch (WSSecurityException e) {
            e.printStackTrace();
        }
        log.debug("RequestedProofToken :: Decryption Done");
    }
View Full Code Here

                               Document doc,
                               SecurityTokenReference secRef,
                               DerivedKeyCallbackHandler dkcbHandler, Vector parts,
                               String symAlgo)
            throws ConversationException {
        WSEncryptBody wsEncrypt = new WSEncryptBody(actor, mu);

        /*
         * Here we want to add a wsse:SecurityTokenReference element into <KeyInfo>.
         * Rest is as same as EMBEDDED_KEYNAME , i.e. we want to encrypt the message
         * using a symmetric key and the result would be an <EncryptedData> element.
         * Steps are
         * step 1: Adding SecurityTokenReference pointing to DkToken
         * step 2: Adding the key into wsEncrypt
         * step 3: Setting the user.
         */
        wsEncrypt.setKeyIdentifierType(WSConstants.EMBED_SECURITY_TOKEN_REF);
   
        /*
         * step 1: Adding SecurityTokenReference pointing to DkToken.
         */
        wsEncrypt.setSecurityTokenReference(secRef);

        /*
         * step 2: Generating the key, and setting it in the the wsEncrypt
         */
        WSPasswordCallback pwCb =
                new WSPasswordCallback(encUser, WSPasswordCallback.UNKNOWN);
        Callback[] callbacks = new Callback[1];
        callbacks[0] = (Callback) pwCb;

        try {
            dkcbHandler.handle(callbacks);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
            throw new ConversationException("ConversationManager :: PasswordCallback failed");
        }

        wsEncrypt.setKey(pwCb.getKey());
        /*
         * step 3: set the user.
         */
        wsEncrypt.setUserInfo(encUser);
       
        /*
         * step 4 : Setting encryption parts
         */
         wsEncrypt.setParts(parts)
       
         wsEncrypt.setSymmetricEncAlgorithm(symAlgo);
        
        try {
            wsEncrypt.build(doc, null);
        } catch (WSSecurityException e) {
          e.printStackTrace();
            throw new ConversationException("ConversationManager :: Encryption: error during message processing");
        }

View Full Code Here

                               Document doc,
                               SecurityTokenReference secRef,
                               DerivedKeyCallbackHandler dkcbHandler, Vector parts,
                               String symAlgo)
            throws ConversationException {
        WSEncryptBody wsEncrypt = new WSEncryptBody(actor, mu);

        /*
         * Here we want to add a wsse:SecurityTokenReference element into <KeyInfo>.
         * Rest is as same as EMBEDDED_KEYNAME , i.e. we want to encrypt the message
         * using a symmetric key and the result would be an <EncryptedData> element.
         * Steps are
         * step 1: Adding SecurityTokenReference pointing to DkToken
         * step 2: Adding the key into wsEncrypt
         * step 3: Setting the user.
         */
        wsEncrypt.setKeyIdentifierType(WSConstants.EMBED_SECURITY_TOKEN_REF);
   
        /*
         * step 1: Adding SecurityTokenReference pointing to DkToken.
         */
        wsEncrypt.setSecurityTokenReference(secRef);

        /*
         * step 2: Generating the key, and setting it in the the wsEncrypt
         */
        WSPasswordCallback pwCb =
                new WSPasswordCallback(encUser, WSPasswordCallback.UNKNOWN);
        Callback[] callbacks = new Callback[1];
        callbacks[0] = (Callback) pwCb;

        try {
            dkcbHandler.handle(callbacks);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
            throw new ConversationException("ConversationManager :: PasswordCallback failed");
        }

        wsEncrypt.setKey(pwCb.getKey());
        /*
         * step 3: set the user.
         */
        wsEncrypt.setUserInfo(encUser);
       
        /*
         * step 4 : Setting encryption parts
         */
         wsEncrypt.setParts(parts)
       
         wsEncrypt.setSymmetricEncAlgorithm(symAlgo);
        
        try {
            wsEncrypt.build(doc, null);
        } catch (WSSecurityException e) {
          e.printStackTrace();
            throw new ConversationException("ConversationManager :: Encryption: error during message processing");
        }

View Full Code Here

        }
    }

    private void performENCRAction(boolean mu, int actionToDo, Document doc, RequestData reqData)
            throws AxisFault {
        WSEncryptBody wsEncrypt = new WSEncryptBody(reqData.actor, mu);
        if (reqData.encKeyId != 0) {
            wsEncrypt.setKeyIdentifierType(reqData.encKeyId);
        }
        if (reqData.encKeyId == WSConstants.EMBEDDED_KEYNAME) {
            String encKeyName = null;
            if ((encKeyName =
                    (String) getOption(WSHandlerConstants.ENC_KEY_NAME))
                    == null) {
                encKeyName =
                        (String) reqData.msgContext.getProperty(WSHandlerConstants.ENC_KEY_NAME);
            }
            wsEncrypt.setEmbeddedKeyName(encKeyName);
            byte[] embeddedKey =
                    getPassword(reqData.encUser,
                            actionToDo,
                            WSHandlerConstants.ENC_CALLBACK_CLASS,
                            WSHandlerConstants.ENC_CALLBACK_REF, reqData)
                    .getKey();
            wsEncrypt.setKey(embeddedKey);
        }
        if (reqData.encSymmAlgo != null) {
            wsEncrypt.setSymmetricEncAlgorithm(reqData.encSymmAlgo);
        }
        if (reqData.encKeyTransport != null) {
            wsEncrypt.setKeyEnc(reqData.encKeyTransport);
        }
        wsEncrypt.setUserInfo(reqData.encUser);
        wsEncrypt.setUseThisCert(reqData.encCert);
        if (reqData.encryptParts.size() > 0) {
            wsEncrypt.setParts(reqData.encryptParts);
        }
        try {
            wsEncrypt.build(doc, reqData.encCrypto);
        } catch (WSSecurityException e) {
            throw new AxisFault("WSDoAllSender: Encryption: error during message processing"
                    + e);
        }
    }
View Full Code Here

     * @throws Exception Thrown when there is any problem in encryption or verification
     */
    public void testEncryptionEncryption() throws Exception {
        SOAPEnvelope unsignedEnvelope = message.getSOAPEnvelope();
        SOAPEnvelope envelope = null;
        WSEncryptBody encrypt = new WSEncryptBody();
        encrypt.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e");
        log.info("Before Encryption....");
        Document doc = unsignedEnvelope.getAsDocument();
        Document encryptedDoc = encrypt.build(doc, crypto);
        Document encryptedEncryptedDoc = encrypt.build(encryptedDoc, crypto);

        /*
         * convert the resulting document into a message first. The toSOAPMessage()
         * mehtod performs the necessary c14n call to properly set up the signed
         * document and convert it into a SOAP message. After that we extract it
View Full Code Here

     *                   decryption, or verification
     */
    public void testEncryptionSigning() throws Exception {
        SOAPEnvelope unsignedEnvelope = message.getSOAPEnvelope();
        SOAPEnvelope envelope = null;
        WSEncryptBody encrypt = new WSEncryptBody();
        WSSignEnvelope sign = new WSSignEnvelope();
        encrypt.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e");
        sign.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e", "security");
        log.info("Before Encryption....");
        Document doc = unsignedEnvelope.getAsDocument();
        Document encryptedDoc = encrypt.build(doc, crypto);
        Document encryptedSignedDoc = sign.build(encryptedDoc, crypto);
        /*
         * convert the resulting document into a message first. The toSOAPMessage()
         * mehtod performs the necessary c14n call to properly set up the signed
         * document and convert it into a SOAP message. After that we extract it
View Full Code Here

     */
    public void testEMBED_SECURITY_TOKEN_REF() throws Exception {

        SOAPEnvelope unsignedEnvelope = message.getSOAPEnvelope();
        SOAPEnvelope envelope = null;
        WSEncryptBody wsEncrypt = new WSEncryptBody();

        //Get the message as document
        log.info("Before Encryption....");
        Document doc = unsignedEnvelope.getAsDocument();

        /* Step 1 :: Add a UserNameToken.
         * Step 2 :: Add an Id to it.
         * Step 3 :: Create a Reference to the UserNameToken.
         * Step 4 :: Setting necessary parameters in WSEncryptBody.
         * Step 5 :: Encrypt using the using the password of UserNameToken.
         */

        //Step 1
        String username = "Dimthu";
        String password = "Sri Lanka Sri Lanka UOM ";
        byte[] key = password.getBytes();

        WSSAddUsernameToken builder = new WSSAddUsernameToken("", false);
        builder.setPasswordType(WSConstants.PASSWORD_TEXT);
        builder.build(doc, username, password);

        //Step 2
        // I should add wsu:Id here but I am not adding it since
        Element usrEle =
            (Element) (doc
                .getElementsByTagNameNS(WSConstants.WSSE_NS, "UsernameToken")
                .item(0));
        String idValue = "1234";
        usrEle.setAttribute("Id", idValue);

        //Step 3 ::
        Reference ref = new Reference(WSSConfig.getDefaultWSConfig(), doc);
        ref.setURI("#" + idValue);
        ref.setValueType("UsernameToken");
        SecurityTokenReference secRef =
            new SecurityTokenReference(WSSConfig.getDefaultWSConfig(), doc);
        secRef.setReference(ref);

        // adding the namespace
        WSSecurityUtil.setNamespace(
            secRef.getElement(),
            WSConstants.WSSE_NS,
            WSConstants.WSSE_PREFIX);

        //Step 4 ::
        wsEncrypt.setKeyIdentifierType(WSConstants.EMBED_SECURITY_TOKEN_REF);
        wsEncrypt.setSecurityTokenReference(secRef);
        wsEncrypt.setKey(key);

        //Step 4 :: Encrypting using the key.
        Document encDoc = wsEncrypt.build(doc, crypto);

        /*
         * convert the resulting document into a message first. The toSOAPMessage()
         * mehtod performs the necessary c14n call to properly set up the signed
         * document and convert it into a SOAP message. After that we extract it
View Full Code Here

     *                   decryption, or verification
     */
    public void testSigningEncryptionEmbedded() throws Exception {
        SOAPEnvelope unsignedEnvelope = message.getSOAPEnvelope();
        SOAPEnvelope envelope = null;
        WSEncryptBody encrypt = new WSEncryptBody();
        WSSignEnvelope sign = new WSSignEnvelope();
       
        encrypt.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e");
        encrypt.setKeyIdentifierType(WSConstants.EMBEDDED_KEYNAME);
        encrypt.setKey(key);

        sign.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e", "security");
        log.info("Before Encryption....");
        Document doc = unsignedEnvelope.getAsDocument();
        Document signedDoc = sign.build(doc, crypto);
        Document encryptedSignedDoc = encrypt.build(signedDoc, crypto);
        /*
         * convert the resulting document into a message first. The toSOAPMessage()
         * mehtod performs the necessary c14n call to properly set up the signed
         * document and convert it into a SOAP message. After that we extract it
         * as a document again for further processing.
View Full Code Here

TOP

Related Classes of org.apache.ws.security.message.WSEncryptBody

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.