Package com.sun.xml.ws.security.opt.api

Examples of com.sun.xml.ws.security.opt.api.SecurityHeaderElement


        }
    }
   
    private void laxTimestampLast(){
        strict();
        SecurityHeaderElement timestamp = this.secHeaderContent.get(0);
        if(timestamp != null && (timestamp instanceof Timestamp )){
            this.secHeaderContent.remove(0);
            this.secHeaderContent.add(timestamp);
        }
    }
View Full Code Here


        ArrayList<SecurityHeaderElement> primaryElementList  = new ArrayList<SecurityHeaderElement> ();
        ArrayList<SecurityHeaderElement> topElementList  = new ArrayList<SecurityHeaderElement> ();
        int len = secHeaderContent.size();
        print(secHeaderContent);
       
        SecurityHeaderElement timeStamp = null;
        for(int i=0;i<len;i++){
            SecurityHeaderElement she = secHeaderContent.get(i);
            if(she.getLocalPart() == MessageConstants.TIMESTAMP_LNAME){
                timeStamp = she;
                continue;
            }
            if(isTopLevelElement(she)){
                topElementList.add(she);
            }else{
                primaryElementList.add(0,she);
            }
        }
       
        print(topElementList);
       // topElementList = orderList(topElementList);
       
        print(primaryElementList);
        primaryElementList = orderList(primaryElementList);
       
        ArrayList<SecurityHeaderElement> tmpList =   new ArrayList<SecurityHeaderElement> ();
        for(int i=0;i<primaryElementList.size();i++){
            SecurityHeaderElement she = primaryElementList.get(i);
            if(she.getLocalPart() == MessageConstants.XENC_REFERENCE_LIST_LNAME ||
                    she.getLocalPart() == MessageConstants.ENCRYPTEDKEY_LNAME){
                int tLen = topElementList.size();
                for(int j=tLen-1;j>=0;j--){
                    SecurityHeaderElement tk = topElementList.get(j);
                    if(she.refersToSecHdrWithId(tk.getId())){
                        topElementList.add(j+1,she);
                        //topElementList.add(j,she);
                        tmpList.add(she);
                        break;
                    }
View Full Code Here

    }
   
    private ArrayList<SecurityHeaderElement> orderList(ArrayList<SecurityHeaderElement> list){
        ArrayList<SecurityHeaderElement> tmp = new ArrayList<SecurityHeaderElement>();
        for(int i=0;i<list.size();i++){
            SecurityHeaderElement securityElementOne = list.get(i);
           
            int wLen = tmp.size();
            int index = 0;
            if(wLen == 0){
                tmp.add(securityElementOne);
                continue;
            }
           
            int setIndex = -1;
            for(int j=0;j<wLen;j++){
                SecurityHeaderElement securityElementTwo = tmp.get(j);
                if(securityElementOne.refersToSecHdrWithId(securityElementTwo.getId())){
                    if(securityElementTwo instanceof JAXBEncryptedData){
                        if(securityElementOne instanceof JAXBEncryptedKey || securityElementOne.getLocalPart() == MessageConstants.XENC_REFERENCE_LIST_LNAME){
                            setIndex = j+1;
                        }else{
                            setIndex = j;
                        }
                    } else{
                        setIndex = j;
                    }
                }else if(securityElementTwo instanceof JAXBEncryptedData  && refersToEncryptedElement(securityElementOne,securityElementTwo)){
                    setIndex = j;
                }else if(securityElementTwo.refersToSecHdrWithId(securityElementOne.getId())){
                    if(securityElementTwo instanceof JAXBEncryptedKey && securityElementOne instanceof JAXBEncryptedData){
                        setIndex = j;
                    }else{
                        setIndex = j+1;
                    }
View Full Code Here

        }
        return false;
    }
    private void movePrevHeader(SecurityHeaderElement toBeMoved, int index){
        int prevIndex = secHeaderContent.indexOf(toBeMoved);
        SecurityHeaderElement prev = (SecurityHeaderElement)secHeaderContent.get(prevIndex-1);
        String prevId = prev.getId();
        secHeaderContent.remove(toBeMoved);
        secHeaderContent.add(index, toBeMoved);
        if(toBeMoved.refersToSecHdrWithId(prevId)){
            movePrevHeader(prev, index);
        }
View Full Code Here

       
        boolean isOptimized = false;
        SecurableSoapMessage secureMessage = null;
        SecurityHeader securityHeader = null;
        com.sun.xml.ws.security.opt.impl.outgoing.SecurityHeader optSecHeader = null;
        SecurityHeaderElement she = null;
        if(context instanceof JAXBFilterProcessingContext){
            isOptimized = true;
            optSecHeader = ((JAXBFilterProcessingContext)context).getSecurityHeader();
        } else{
            secureMessage = context.getSecurableSoapMessage();
            securityHeader = secureMessage.findOrCreateSecurityHeader();
        }
       
        AuthenticationTokenPolicy policy =
                (AuthenticationTokenPolicy)context.getSecurityPolicy();
        AuthenticationTokenPolicy.SAMLAssertionBinding samlPolicy =
                (AuthenticationTokenPolicy.SAMLAssertionBinding)policy.getFeatureBinding();
       
        if (samlPolicy.getIncludeToken() == samlPolicy.INCLUDE_ONCE) {
            throw new XWSSecurityException("Include Token ONCE not supported for SAMLToken Assertions");
        }
       
        if (samlPolicy.getAssertionType() !=
                AuthenticationTokenPolicy.SAMLAssertionBinding.SV_ASSERTION) {
            // should never be called this way
            throw new XWSSecurityException(
                    "Internal Error: ExportSamlAssertionFilter called for HOK assertion");
        }
       
        //AuthenticationTokenPolicy policyClone = (AuthenticationTokenPolicy)policy.clone();
        samlPolicy =
                (AuthenticationTokenPolicy.SAMLAssertionBinding)policy.getFeatureBinding();
        samlPolicy.isReadOnly(true);
       
        DynamicApplicationContext dynamicContext =
                new DynamicApplicationContext(context.getPolicyContext());
        dynamicContext.setMessageIdentifier(context.getMessageIdentifier());
        dynamicContext.inBoundMessage(false);
       
        AuthenticationTokenPolicy.SAMLAssertionBinding resolvedPolicy =
                context.getSecurityEnvironment().populateSAMLPolicy(context.getExtraneousProperties(), samlPolicy, dynamicContext);
       
        Assertion _assertion = null;
        Element assertionElement = resolvedPolicy.getAssertion();
        Element _authorityBinding = resolvedPolicy.getAuthorityBinding();
               
        if (assertionElement == null) {
            reader = resolvedPolicy.getAssertionReader();
            if (reader != null) {
                try {
                    reader.next(); //start document , so move to next event
                    id = reader.getAttributeValue(null, "AssertionID");
                    if (id == null) {
                        id = reader.getAttributeValue(null, "ID");
                    }
                    version = reader.getAttributeValue(null, "Version");
                    buffer = new MutableXMLStreamBuffer();
                    StreamWriterBufferCreator bCreator = new StreamWriterBufferCreator(buffer);
                    XMLStreamWriter writer_tmp = (XMLStreamWriter) bCreator;
                    while (!(XMLStreamReader.END_DOCUMENT == reader.getEventType())) {
                       com.sun.xml.ws.security.opt.impl.util.StreamUtil.writeCurrentEvent(reader, writer_tmp);
                       reader.next();                      
                    }
                } catch (XMLStreamException ex) {
                   throw new XWSSecurityException(ex);
                }
            }
        } else {
            try {
                if (System.getProperty("com.sun.xml.wss.saml.binding.jaxb") == null) {
                    if (assertionElement.getAttributeNode("ID") != null) {
                        _assertion = (Assertion) com.sun.xml.wss.saml.assertion.saml20.jaxb20.Assertion.fromElement(assertionElement);
                    } else {
                        _assertion = (Assertion) com.sun.xml.wss.saml.assertion.saml11.jaxb20.Assertion.fromElement(assertionElement);
                    }
                } else {
                    _assertion = (Assertion) com.sun.xml.wss.saml.assertion.saml11.jaxb10.Assertion.fromElement(assertionElement);
                }
            } catch (SAMLException ex) {
                //ignore
            }
        }

        if (samlPolicy.getIncludeToken() == samlPolicy.INCLUDE_NEVER ||
               samlPolicy.getIncludeToken() == samlPolicy.INCLUDE_NEVER_VER2 ) {
            if (_authorityBinding != null) {
                //nullify the assertion set by Callback since IncludeToken is never
                // do this because we have to maintain BackwardCompat with XWSS2.0
                assertionElement = null;
            }
        }
       
        if ((_assertion == null) && (_authorityBinding == null) && reader == null) {
            throw new XWSSecurityException(
                    "None of SAML Assertion,SAML Assertion Reader or  SAML AuthorityBinding information was set into " +
                    " the Policy by the CallbackHandler");
        }
       
        //TODO: check that the Confirmation Method of the assertion is indeed SV
        if (_assertion != null){
            if(_assertion.getVersion() == null && _authorityBinding == null){
                if(!isOptimized){
                    if ( System.getProperty("com.sun.xml.wss.saml.binding.jaxb") == null) {
                        ((com.sun.xml.wss.saml.assertion.saml11.jaxb20.Assertion)_assertion).toElement(securityHeader);
                    } else {
                        ((com.sun.xml.wss.saml.assertion.saml11.jaxb10.Assertion)_assertion).toElement(securityHeader);
                    }
                } else {
                    she = new GSHeaderElement(assertionElement, ((JAXBFilterProcessingContext) context).getSOAPVersion());
                    if (optSecHeader.getChildElement(she.getId()) == null) {
                        optSecHeader.add(she);
                    } else {
                        return;
                    }
                }
                HashMap tokenCache = context.getTokenCache();
                //assuming unique IDs
                tokenCache.put(((com.sun.xml.wss.saml.Assertion)_assertion).getAssertionID(), _assertion);
            } else if (_assertion.getVersion() != null){
                if(!isOptimized){
                    ((com.sun.xml.wss.saml.assertion.saml20.jaxb20.Assertion)_assertion).toElement(securityHeader);
                } else {
                    she = new GSHeaderElement(assertionElement, ((JAXBFilterProcessingContext) context).getSOAPVersion());
                    if (optSecHeader.getChildElement(she.getId()) == null) {
                        optSecHeader.add(she);
                    } else {
                        return;
                    }
                }
                HashMap tokenCache = context.getTokenCache();
                //assuming unique IDs
                tokenCache.put(((com.sun.xml.wss.saml.Assertion)_assertion).getID(), _assertion);
            else {
                //Authoritybinding is set. So the Assertion should not be exported
                if (null == resolvedPolicy.getSTRID()) {
                    throw new XWSSecurityException(
                            "Unsupported configuration: required wsu:Id value " +
                            " for SecurityTokenReference to Remote SAML Assertion not found " +
                            " in Policy");
                }
            }
        } else if(reader != null) {
            she = new GSHeaderElement(buffer);
            she.setId(id)// set the ID again to bring it to top
            if (optSecHeader.getChildElement(she.getId()) == null) {
                optSecHeader.add(she);
            } else {
                return;
            }
        }
View Full Code Here

            throws XWSSecurityException {
        String id = getID(binding);
        if (logger.isLoggable(Level.FINEST)) {
            logger.log(Level.FINEST, "Username Token id: " + id);
        }
        SecurityHeaderElement token = (SecurityHeaderElement) securityHeader.getChildElement(id);
        if (token != null) {
            if (token instanceof UsernameToken) {
                return (UsernameToken) token;
            }
            logger.log(Level.SEVERE, LogStringsMessages.WSS_1854_TWO_TOKENS_WITH_SAME_ID());
View Full Code Here

        }

        // look in processed headers
        ArrayList processedHeaders = sc.getProcessedSecurityHeaders();
        for (int j = 0; j < processedHeaders.size(); j++) {
            SecurityHeaderElement header = (SecurityHeaderElement) processedHeaders.get(j);
            if (id.equals(header.getId())) {
                return header.getLocalPart();
            }
        }

        // look in buffered headers
        ArrayList bufferedHeaders = sc.getBufferedSecurityHeaders();
        for (int j = 0; j < bufferedHeaders.size(); j++) {
            SecurityHeaderElement header = (SecurityHeaderElement) bufferedHeaders.get(j);
            if (id.equals(header.getId())) {
                return header.getLocalPart();
            }
        }
        return null;
    }
View Full Code Here

        }

        // look in processed headers
        ArrayList processedHeaders = sc.getProcessedSecurityHeaders();
        for (int j = 0; j < processedHeaders.size(); j++) {
            SecurityHeaderElement header = (SecurityHeaderElement) processedHeaders.get(j);
            if (header != null && header.getLocalPart().equals(targetInPolicy)) {
                return true;
            }
        }

        // look in buffered headers
        ArrayList bufferedHeaders = sc.getBufferedSecurityHeaders();
        for (int j = 0; j < bufferedHeaders.size(); j++) {
            SecurityHeaderElement header = (SecurityHeaderElement) bufferedHeaders.get(j);
            if (header != null && header.getLocalPart().equals(targetInPolicy)) {
                return true;
            }
        }
        return false;
    }
View Full Code Here

                }
                if (logger.isLoggable(Level.FINEST)) {
                    logger.log(Level.FINEST, "UsernameToken for SymmetricBinding is: " + unt);
                    logger.log(Level.FINEST, "Token ID for SymmetricBinding is: " + unTokenId);
                }
                SecurityHeaderElement ek = null;
                HashMap ekCache = context.getEncryptedKeyCache();
                String ekId = (String) ekCache.get(unTokenId);
                keyProtectionKey = untBinding.getSecretKey();
                if (ekId == null) {
                    TokenBuilder builder = new UsernameTokenBuilder(context, untBinding);
                    result = builder.process();
                    KeyInfo ekKI = (com.sun.xml.ws.security.opt.crypto.dsig.keyinfo.KeyInfo) result.getKeyInfo();
                    context.setExtraneousProperty("SecretKey", dataProtectionKey);
                    //Truncating 20 byte Key to 16 byte Key;
                    byte[] secretKey = untBinding.getSecretKey().getEncoded();
                    PasswordDerivedKey pdk = new PasswordDerivedKey();                   
                    Key dpKey = pdk.generate16ByteKeyforEncryption(secretKey);
                    ek = (SecurityHeaderElement) elementFactory.createEncryptedKey(context.generateID(), context.getAlgorithmSuite().getSymmetricKeyAlgorithm(), ekKI, dpKey, dataProtectionKey);
                    context.getSecurityHeader().add(ek);
                    ekId = ek.getId();
                    ekCache.put(unTokenId, ekId);
                    context.addToCurrentSecretMap(ekId, dataProtectionKey);
                    try {
                        byte[] cipherVal = ((JAXBEncryptedKey) ek).getCipherValue();
                        byte[] ekSha1 = MessageDigest.getInstance("SHA-1").digest(cipherVal);
                        //byte[] ekSha1 = MessageDigest.getInstance("SHA-1").digest(cipherVal);
                        String encEkSha1 = Base64.encode(ekSha1);
                        context.setExtraneousProperty("EncryptedKeySHA1", encEkSha1);
                    } catch (java.security.NoSuchAlgorithmException nsa) {
                        throw new XWSSecurityException(nsa);
                    }
                } else {
                    if (ekId == null || ekId.length() == 0) {
                        logger.log(Level.SEVERE, LogStringsMessages.WSS_1804_WRONG_ENCRYPTED_KEY());
                        throw new XWSSecurityException("Invalid EncryptedKey Id ");
                    }
                    dataProtectionKey = context.getCurrentSecretFromMap(ekId);
                }
                String valType = null;
                if (wss11Sender) {
                    valType = MessageConstants.EncryptedKey_NS;
                }
                com.sun.xml.ws.security.opt.api.keyinfo.SecurityTokenReference str = buildSTR(untBinding.getUUID(), buildDirectReference(ekId, valType));
                //str.setTokenType(MessageConstants.EncryptedKey_NS);
                buildKeyInfo((SecurityTokenReference) str);
                stbResult.setDataProtectionKey(dataProtectionKey);
                stbResult.setKeyInfo(super.keyInfo);
                stbResult.setEncryptedKey((EncryptedKey) ek);
            }
        } else if (!PolicyTypeUtil.kerberosTokenBinding(ckBinding)) {
            if (!binding.getKeyIdentifier().equals(MessageConstants._EMPTY)) {
                if (keyProtectionAlg != null && !"".equals(keyProtectionAlg)) {
                    dataProtectionKey = SecurityUtil.generateSymmetricKey(dataProtectionAlg);
                }

                keyProtectionKey = binding.getSecretKey();
                if (dataProtectionKey == null) {
                    dataProtectionKey = keyProtectionKey;
                    keyProtectionKey = null;
                    buildKIWithKeyName(binding.getKeyIdentifier());
                }
                stbResult.setKeyInfo(super.keyInfo);
                stbResult.setDataProtectionKey(dataProtectionKey);
            } else if (sendEKSHA1) {
                //get the signing key and EKSHA1 reference from the Subject, it was stored from the incoming message
                String ekSha1Ref = (String) context.getExtraneousProperty(MessageConstants.EK_SHA1_VALUE);
                buildKeyInfoWithEKSHA1(ekSha1Ref);
                dataProtectionKey = binding.getSecretKey();
                stbResult.setKeyInfo(super.keyInfo);
                stbResult.setDataProtectionKey(dataProtectionKey);
            } else if (wss11Sender || wss10) {
                dataProtectionKey = binding.getSecretKey();
                //TODO :: REMOVE ONCE THE CHANGE IS MADE IN FITERS
                AuthenticationTokenPolicy.X509CertificateBinding certificateBinding = null;
                if (!binding.getCertAlias().equals(MessageConstants._EMPTY)) {
                    certificateBinding = new AuthenticationTokenPolicy.X509CertificateBinding();
                    //x509Binding.newPrivateKeyBinding();
                    certificateBinding.setCertificateIdentifier(binding.getCertAlias());
                    X509Certificate x509Cert = context.getSecurityEnvironment().getCertificate(context.getExtraneousProperties(), certificateBinding.getCertificateIdentifier(), false);
                    certificateBinding.setX509Certificate(x509Cert);
                    certificateBinding.setReferenceType("Direct");
                } else if (context.getX509CertificateBinding() != null) {
                    certificateBinding = context.getX509CertificateBinding();
                    context.setX509CertificateBinding(null);
                } else {
                    throw new XWSSecurityException("Internal Error: X509CertificateBinding not set on context");
                }

                X509Certificate x509Cert = certificateBinding.getX509Certificate();
                String x509TokenId = certificateBinding.getUUID();
                if (x509TokenId == null || x509TokenId.equals("")) {
                    x509TokenId = context.generateID();
                }

                SecurityUtil.checkIncludeTokenPolicyOpt(context, certificateBinding, x509TokenId);

                if (logger.isLoggable(Level.FINEST)) {
                    logger.log(Level.FINEST, "Certificate for SymmetricBinding is: " + x509Cert);
                    logger.log(Level.FINEST, "BinaryToken ID for SymmetricBinding is: " + x509TokenId);
                }
                BinarySecurityToken bst = null;
                SecurityHeaderElement ek = null;

                HashMap ekCache = context.getEncryptedKeyCache();
                String ekId = (String) ekCache.get(x509TokenId);

                keyProtectionKey = x509Cert.getPublicKey();
                if (ekId == null) {

                    TokenBuilder builder = new X509TokenBuilder(context, certificateBinding);
                    BuilderResult bResult = builder.process();
                    KeyInfo ekKI = (com.sun.xml.ws.security.opt.crypto.dsig.keyinfo.KeyInfo) bResult.getKeyInfo();
                    context.setExtraneousProperty("SecretKey", dataProtectionKey);
                    ek = (SecurityHeaderElement) elementFactory.createEncryptedKey(context.generateID(), keyProtectionAlg, ekKI, keyProtectionKey, dataProtectionKey);
                    context.getSecurityHeader().add(ek);
                    ekId = ek.getId();
                    ekCache.put(x509TokenId, ekId);
                    context.addToCurrentSecretMap(ekId, dataProtectionKey);
                    stbResult.setEncryptedKey((EncryptedKey) ek);
                    //store EKSHA1 of KeyValue contents in context
                    try {
View Full Code Here

            final String uri) throws JAXBException, XMLStreamException,
            XWSSecurityException {
        SecuredMessage secMessage = context.getSecuredMessage();
        ArrayList headerList = secMessage.getHeaders();
        // Look for Id or wsu:Id attribute in all elements
        SecurityHeaderElement reqdHeader = null;
        for (int i = 0; i < headerList.size(); i++) {
            Object header = headerList.get(i);
            if (header instanceof SecurityHeaderElement) {
                // header already wrapped by a SecurityheaderElement
                SecurityHeaderElement she = (SecurityHeaderElement) header;
                if (uri.equals(she.getId())) {
                    reqdHeader = she;
                    break;
                }
            }
        }

        // check inside the Securityheader
        if (reqdHeader == null) {
            SecurityHeader secHeader = context.getSecurityHeader();
            SecurityHeaderElement she = secHeader.getChildElement(uri);
            if (she != null && !(MessageConstants.WSSE_SECURITY_TOKEN_REFERENCE_LNAME.equals(she.getLocalPart()) &&
                    MessageConstants.WSSE_NS.equals(she.getNamespaceURI()))) {
                reqdHeader = she;
            }
        }

        // if matches, convert the element to JAXBData
View Full Code Here

TOP

Related Classes of com.sun.xml.ws.security.opt.api.SecurityHeaderElement

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.