Java Code Examples for org.apache.cxf.ws.security.tokenstore.SecurityToken#getSHA1()

The following examples show how to use org.apache.cxf.ws.security.tokenstore.SecurityToken#getSHA1() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TokenStoreCallbackHandler.java    From cxf with Apache License 2.0 6 votes vote down vote up
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof WSPasswordCallback) {
            WSPasswordCallback pc = (WSPasswordCallback)callback;

            String id = pc.getIdentifier();
            SecurityToken tok = store.getToken(id);
            if (tok != null && !tok.isExpired()) {
                if (tok.getSHA1() == null && pc.getKey() != null) {
                    tok.setSHA1(getSHA1(pc.getKey()));
                    // Create another cache entry with the SHA1 Identifier as the key for easy retrieval
                    store.add(tok.getSHA1(), tok);
                }
                pc.setKey(tok.getSecret());
                pc.setKey(tok.getKey());
                pc.setCustomToken(tok.getToken());
                return;
            }
        }
    }
    if (internal != null) {
        internal.handle(callbacks);
    }
}
 
Example 2
Source File: AbstractBindingBuilder.java    From steady with Apache License 2.0 4 votes vote down vote up
private void doSymmSignatureDerived(Token policyToken, SecurityToken tok,
                             List<WSEncryptionPart> sigParts, boolean isTokenProtection)
    throws WSSecurityException, ConversationException {
    
    Document doc = saaj.getSOAPPart();
    WSSecDKSign dkSign = new WSSecDKSign(wssConfig);  
    
    //Check whether it is security policy 1.2 and use the secure conversation accordingly
    if (SP12Constants.INSTANCE == policyToken.getSPConstants()) {
        dkSign.setWscVersion(ConversationConstants.VERSION_05_12);
    }
                  
    //Check for whether the token is attached in the message or not
    boolean attached = false;
    if (includeToken(policyToken.getInclusion())) {
        attached = true;
    }
    
    // Setting the AttachedReference or the UnattachedReference according to the flag
    Element ref;
    if (attached) {
        ref = tok.getAttachedReference();
    } else {
        ref = tok.getUnattachedReference();
    }
    
    if (ref != null) {
        ref = cloneElement(ref);
        dkSign.setExternalKey(tok.getSecret(), ref);
    } else if (!isRequestor() && policyToken.isDerivedKeys()) { 
        // If the Encrypted key used to create the derived key is not
        // attached use key identifier as defined in WSS1.1 section
        // 7.7 Encrypted Key reference
        SecurityTokenReference tokenRef 
            = new SecurityTokenReference(doc);
        if (tok.getSHA1() != null) {
            tokenRef.setKeyIdentifierEncKeySHA1(tok.getSHA1());
            tokenRef.addTokenType(WSConstants.WSS_ENC_KEY_VALUE_TYPE);
        }
        dkSign.setExternalKey(tok.getSecret(), tokenRef.getElement());
    
    } else {
        dkSign.setExternalKey(tok.getSecret(), tok.getId());
    }

    //Set the algo info
    dkSign.setSignatureAlgorithm(binding.getAlgorithmSuite().getSymmetricSignature());
    dkSign.setDerivedKeyLength(binding.getAlgorithmSuite().getSignatureDerivedKeyLength() / 8);
    if (tok.getSHA1() != null) {
        //Set the value type of the reference
        dkSign.setCustomValueType(WSConstants.SOAPMESSAGE_NS11 + "#"
            + WSConstants.ENC_KEY_VALUE_TYPE);
    } else if (policyToken instanceof UsernameToken) {
        dkSign.setCustomValueType(WSConstants.WSS_USERNAME_TOKEN_VALUE_TYPE);
    } 
    
    dkSign.prepare(doc, secHeader);
    
    if (isTokenProtection) {
        //Hack to handle reference id issues
        //TODO Need a better fix
        String sigTokId = tok.getId();
        if (sigTokId.startsWith("#")) {
            sigTokId = sigTokId.substring(1);
        }
        sigParts.add(new WSEncryptionPart(sigTokId));
    }
    
    dkSign.setParts(sigParts);
    
    List<Reference> referenceList = dkSign.addReferencesToSign(sigParts, secHeader);
    
    //Add elements to header
    addSupportingElement(dkSign.getdktElement());
    
    //Do signature
    dkSign.computeSignature(referenceList, false, null);
    
    signatures.add(dkSign.getSignatureValue());
}
 
Example 3
Source File: AbstractBindingBuilder.java    From steady with Apache License 2.0 4 votes vote down vote up
private void doSymmSignatureDerived(Token policyToken, SecurityToken tok,
                             List<WSEncryptionPart> sigParts, boolean isTokenProtection)
    throws WSSecurityException, ConversationException {
    
    Document doc = saaj.getSOAPPart();
    WSSecDKSign dkSign = new WSSecDKSign(wssConfig);  
    
    //Check whether it is security policy 1.2 and use the secure conversation accordingly
    if (SP12Constants.INSTANCE == policyToken.getSPConstants()) {
        dkSign.setWscVersion(ConversationConstants.VERSION_05_12);
    }
                  
    //Check for whether the token is attached in the message or not
    boolean attached = false;
    if (includeToken(policyToken.getInclusion())) {
        attached = true;
    }
    
    // Setting the AttachedReference or the UnattachedReference according to the flag
    Element ref;
    if (attached) {
        ref = tok.getAttachedReference();
    } else {
        ref = tok.getUnattachedReference();
    }
    
    if (ref != null) {
        ref = cloneElement(ref);
        dkSign.setExternalKey(tok.getSecret(), ref);
    } else if (!isRequestor() && policyToken.isDerivedKeys()) { 
        // If the Encrypted key used to create the derived key is not
        // attached use key identifier as defined in WSS1.1 section
        // 7.7 Encrypted Key reference
        SecurityTokenReference tokenRef 
            = new SecurityTokenReference(doc);
        if (tok.getSHA1() != null) {
            tokenRef.setKeyIdentifierEncKeySHA1(tok.getSHA1());
            tokenRef.addTokenType(WSConstants.WSS_ENC_KEY_VALUE_TYPE);
        }
        dkSign.setExternalKey(tok.getSecret(), tokenRef.getElement());
    
    } else {
        dkSign.setExternalKey(tok.getSecret(), tok.getId());
    }

    //Set the algo info
    dkSign.setSignatureAlgorithm(binding.getAlgorithmSuite().getSymmetricSignature());
    dkSign.setDerivedKeyLength(binding.getAlgorithmSuite().getSignatureDerivedKeyLength() / 8);
    if (tok.getSHA1() != null) {
        //Set the value type of the reference
        dkSign.setCustomValueType(WSConstants.SOAPMESSAGE_NS11 + "#"
            + WSConstants.ENC_KEY_VALUE_TYPE);
    } else if (policyToken instanceof UsernameToken) {
        dkSign.setCustomValueType(WSConstants.WSS_USERNAME_TOKEN_VALUE_TYPE);
    } 
    
    dkSign.prepare(doc, secHeader);
    
    if (isTokenProtection) {
        //Hack to handle reference id issues
        //TODO Need a better fix
        String sigTokId = tok.getId();
        if (sigTokId.startsWith("#")) {
            sigTokId = sigTokId.substring(1);
        }
        sigParts.add(new WSEncryptionPart(sigTokId));
    }
    
    dkSign.setParts(sigParts);
    
    List<Reference> referenceList = dkSign.addReferencesToSign(sigParts, secHeader);
    
    //Add elements to header
    addSupportingElement(dkSign.getdktElement());
    
    //Do signature
    dkSign.computeSignature(referenceList, false, null);
    
    signatures.add(dkSign.getSignatureValue());
}
 
Example 4
Source File: AbstractBindingBuilder.java    From steady with Apache License 2.0 4 votes vote down vote up
private void doSymmSignatureDerived(Token policyToken, SecurityToken tok,
                             List<WSEncryptionPart> sigParts, boolean isTokenProtection)
    throws WSSecurityException, ConversationException {
    
    Document doc = saaj.getSOAPPart();
    WSSecDKSign dkSign = new WSSecDKSign(wssConfig);  
    
    //Check whether it is security policy 1.2 and use the secure conversation accordingly
    if (SP12Constants.INSTANCE == policyToken.getSPConstants()) {
        dkSign.setWscVersion(ConversationConstants.VERSION_05_12);
    }
                  
    //Check for whether the token is attached in the message or not
    boolean attached = false;
    if (includeToken(policyToken.getInclusion())) {
        attached = true;
    }
    
    // Setting the AttachedReference or the UnattachedReference according to the flag
    Element ref;
    if (attached) {
        ref = tok.getAttachedReference();
    } else {
        ref = tok.getUnattachedReference();
    }
    
    if (ref != null) {
        ref = cloneElement(ref);
        dkSign.setExternalKey(tok.getSecret(), ref);
    } else if (!isRequestor() && policyToken.isDerivedKeys()) { 
        // If the Encrypted key used to create the derived key is not
        // attached use key identifier as defined in WSS1.1 section
        // 7.7 Encrypted Key reference
        SecurityTokenReference tokenRef 
            = new SecurityTokenReference(doc);
        if (tok.getSHA1() != null) {
            tokenRef.setKeyIdentifierEncKeySHA1(tok.getSHA1());
            tokenRef.addTokenType(WSConstants.WSS_ENC_KEY_VALUE_TYPE);
        }
        dkSign.setExternalKey(tok.getSecret(), tokenRef.getElement());
    
    } else {
        dkSign.setExternalKey(tok.getSecret(), tok.getId());
    }

    //Set the algo info
    dkSign.setSignatureAlgorithm(binding.getAlgorithmSuite().getSymmetricSignature());
    dkSign.setDerivedKeyLength(binding.getAlgorithmSuite().getSignatureDerivedKeyLength() / 8);
    if (tok.getSHA1() != null) {
        //Set the value type of the reference
        dkSign.setCustomValueType(WSConstants.SOAPMESSAGE_NS11 + "#"
            + WSConstants.ENC_KEY_VALUE_TYPE);
    } else if (policyToken instanceof UsernameToken) {
        dkSign.setCustomValueType(WSConstants.WSS_USERNAME_TOKEN_VALUE_TYPE);
    } 
    
    dkSign.prepare(doc, secHeader);
    
    if (isTokenProtection) {
        //Hack to handle reference id issues
        //TODO Need a better fix
        String sigTokId = tok.getId();
        if (sigTokId.startsWith("#")) {
            sigTokId = sigTokId.substring(1);
        }
        sigParts.add(new WSEncryptionPart(sigTokId));
    }
    
    dkSign.setParts(sigParts);
    
    List<Reference> referenceList = dkSign.addReferencesToSign(sigParts, secHeader);
    
    //Add elements to header
    addSupportingElement(dkSign.getdktElement());
    
    //Do signature
    dkSign.computeSignature(referenceList, false, null);
    
    signatures.add(dkSign.getSignatureValue());
}
 
Example 5
Source File: AbstractBindingBuilder.java    From steady with Apache License 2.0 4 votes vote down vote up
private void doSymmSignatureDerived(Token policyToken, SecurityToken tok,
                             List<WSEncryptionPart> sigParts, boolean isTokenProtection)
    throws WSSecurityException, ConversationException {
    
    Document doc = saaj.getSOAPPart();
    WSSecDKSign dkSign = new WSSecDKSign(wssConfig);  
    
    //Check whether it is security policy 1.2 and use the secure conversation accordingly
    if (SP12Constants.INSTANCE == policyToken.getSPConstants()) {
        dkSign.setWscVersion(ConversationConstants.VERSION_05_12);
    }
                  
    //Check for whether the token is attached in the message or not
    boolean attached = false;
    if (includeToken(policyToken.getInclusion())) {
        attached = true;
    }
    
    // Setting the AttachedReference or the UnattachedReference according to the flag
    Element ref;
    if (attached) {
        ref = tok.getAttachedReference();
    } else {
        ref = tok.getUnattachedReference();
    }
    
    if (ref != null) {
        ref = cloneElement(ref);
        dkSign.setExternalKey(tok.getSecret(), ref);
    } else if (!isRequestor() && policyToken.isDerivedKeys()) { 
        // If the Encrypted key used to create the derived key is not
        // attached use key identifier as defined in WSS1.1 section
        // 7.7 Encrypted Key reference
        SecurityTokenReference tokenRef 
            = new SecurityTokenReference(doc);
        if (tok.getSHA1() != null) {
            tokenRef.setKeyIdentifierEncKeySHA1(tok.getSHA1());
            tokenRef.addTokenType(WSConstants.WSS_ENC_KEY_VALUE_TYPE);
        }
        dkSign.setExternalKey(tok.getSecret(), tokenRef.getElement());
    
    } else {
        dkSign.setExternalKey(tok.getSecret(), tok.getId());
    }

    //Set the algo info
    dkSign.setSignatureAlgorithm(binding.getAlgorithmSuite().getSymmetricSignature());
    dkSign.setDerivedKeyLength(binding.getAlgorithmSuite().getSignatureDerivedKeyLength() / 8);
    if (tok.getSHA1() != null) {
        //Set the value type of the reference
        dkSign.setCustomValueType(WSConstants.SOAPMESSAGE_NS11 + "#"
            + WSConstants.ENC_KEY_VALUE_TYPE);
    } else if (policyToken instanceof UsernameToken) {
        dkSign.setCustomValueType(WSConstants.WSS_USERNAME_TOKEN_VALUE_TYPE);
    } 
    
    dkSign.prepare(doc, secHeader);
    
    if (isTokenProtection) {
        //Hack to handle reference id issues
        //TODO Need a better fix
        String sigTokId = tok.getId();
        if (sigTokId.startsWith("#")) {
            sigTokId = sigTokId.substring(1);
        }
        sigParts.add(new WSEncryptionPart(sigTokId));
    }
    
    dkSign.setParts(sigParts);
    
    List<Reference> referenceList = dkSign.addReferencesToSign(sigParts, secHeader);
    
    //Add elements to header
    addSupportingElement(dkSign.getdktElement());
    
    //Do signature
    dkSign.computeSignature(referenceList, false, null);
    
    signatures.add(dkSign.getSignatureValue());
}
 
Example 6
Source File: AbstractBindingBuilder.java    From cxf with Apache License 2.0 4 votes vote down vote up
private void doSymmSignatureDerived(AbstractToken policyToken, SecurityToken tok,
                             List<WSEncryptionPart> sigParts, boolean isTokenProtection,
                             boolean isSigProtect)
    throws WSSecurityException {

    Document doc = saaj.getSOAPPart();
    WSSecDKSign dkSign = new WSSecDKSign(secHeader);
    dkSign.setIdAllocator(wssConfig.getIdAllocator());
    dkSign.setCallbackLookup(callbackLookup);
    dkSign.setStoreBytesInAttachment(storeBytesInAttachment);
    dkSign.setExpandXopInclude(isExpandXopInclude());

    //Check whether it is security policy 1.2 and use the secure conversation accordingly
    if (policyToken.getVersion() == SPConstants.SPVersion.SP11) {
        dkSign.setWscVersion(ConversationConstants.VERSION_05_02);
    }

    //Check for whether the token is attached in the message or not
    boolean attached = false;
    if (isTokenRequired(policyToken.getIncludeTokenType())) {
        attached = true;
    }

    // Setting the AttachedReference or the UnattachedReference according to the flag
    Element ref;
    if (attached) {
        ref = tok.getAttachedReference();
    } else {
        ref = tok.getUnattachedReference();
    }

    if (ref != null) {
        ref = cloneElement(ref);
        dkSign.setStrElem(ref);
    } else if (!isRequestor() && policyToken.getDerivedKeys() == DerivedKeys.RequireDerivedKeys) {
        // If the Encrypted key used to create the derived key is not
        // attached use key identifier as defined in WSS1.1 section
        // 7.7 Encrypted Key reference
        SecurityTokenReference tokenRef
            = new SecurityTokenReference(doc);
        if (tok.getSHA1() != null) {
            tokenRef.setKeyIdentifierEncKeySHA1(tok.getSHA1());
            tokenRef.addTokenType(WSS4JConstants.WSS_ENC_KEY_VALUE_TYPE);
        }
        dkSign.setStrElem(tokenRef.getElement());

    } else {
        dkSign.setTokenIdentifier(tok.getId());
    }

    //Set the algo info
    dkSign.setSignatureAlgorithm(binding.getAlgorithmSuite().getAlgorithmSuiteType().getSymmetricSignature());
    dkSign.setSigCanonicalization(binding.getAlgorithmSuite().getC14n().getValue());
    AlgorithmSuiteType algType = binding.getAlgorithmSuite().getAlgorithmSuiteType();
    dkSign.setDerivedKeyLength(algType.getSignatureDerivedKeyLength() / 8);
    if (tok.getSHA1() != null) {
        //Set the value type of the reference
        dkSign.setCustomValueType(WSS4JConstants.SOAPMESSAGE_NS11 + "#"
            + WSS4JConstants.ENC_KEY_VALUE_TYPE);
    } else if (policyToken instanceof UsernameToken) {
        dkSign.setCustomValueType(WSS4JConstants.WSS_USERNAME_TOKEN_VALUE_TYPE);
    }

    dkSign.prepare(tok.getSecret());

    if (isTokenProtection) {
        String sigTokId = XMLUtils.getIDFromReference(tok.getId());
        sigParts.add(new WSEncryptionPart(sigTokId));
    }

    dkSign.getParts().addAll(sigParts);

    List<Reference> referenceList = dkSign.addReferencesToSign(sigParts);

    //Add elements to header
    addSupportingElement(dkSign.getdktElement());

    //Do signature
    dkSign.computeSignature(referenceList, false, null);

    if (isSigProtect) {
        WSEncryptionPart part = new WSEncryptionPart(dkSign.getSignatureId(), "Element");
        encryptedTokensList.add(part);
    }

    addSig(dkSign.getSignatureValue());
    dkSign.clean();
}