Search in sources :

Example 11 with KeyInfoBean

use of org.apache.wss4j.common.saml.bean.KeyInfoBean in project cxf by apache.

the class SamlRoleCallbackHandler method createKeyInfo.

protected KeyInfoBean createKeyInfo() throws Exception {
    Crypto crypto = CryptoFactory.getInstance("alice.properties");
    CryptoType cryptoType = new CryptoType(CryptoType.TYPE.ALIAS);
    cryptoType.setAlias("alice");
    X509Certificate[] certs = crypto.getX509Certificates(cryptoType);
    KeyInfoBean keyInfo = new KeyInfoBean();
    keyInfo.setCertIdentifer(keyInfoIdentifier);
    if (keyInfoIdentifier == CERT_IDENTIFIER.X509_CERT) {
        keyInfo.setCertificate(certs[0]);
    } else if (keyInfoIdentifier == CERT_IDENTIFIER.KEY_VALUE) {
        keyInfo.setPublicKey(certs[0].getPublicKey());
    }
    return keyInfo;
}
Also used : Crypto(org.apache.wss4j.common.crypto.Crypto) KeyInfoBean(org.apache.wss4j.common.saml.bean.KeyInfoBean) CryptoType(org.apache.wss4j.common.crypto.CryptoType) X509Certificate(java.security.cert.X509Certificate)

Example 12 with KeyInfoBean

use of org.apache.wss4j.common.saml.bean.KeyInfoBean in project cxf by apache.

the class SamlRoleCallbackHandler method handle.

public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] instanceof SAMLCallback) {
            SAMLCallback callback = (SAMLCallback) callbacks[i];
            if (saml2) {
                callback.setSamlVersion(Version.SAML_20);
            } else {
                callback.setSamlVersion(Version.SAML_11);
            }
            callback.setIssuer("sts");
            String subjectName = "uid=sts-client,o=mock-sts.com";
            String subjectQualifier = "www.mock-sts.com";
            if (!saml2 && SAML2Constants.CONF_SENDER_VOUCHES.equals(confirmationMethod)) {
                confirmationMethod = SAML1Constants.CONF_SENDER_VOUCHES;
            }
            SubjectBean subjectBean = new SubjectBean(subjectName, subjectQualifier, confirmationMethod);
            if (SAML2Constants.CONF_HOLDER_KEY.equals(confirmationMethod) || SAML1Constants.CONF_HOLDER_KEY.equals(confirmationMethod)) {
                try {
                    KeyInfoBean keyInfo = createKeyInfo();
                    subjectBean.setKeyInfo(keyInfo);
                } catch (Exception ex) {
                    throw new IOException("Problem creating KeyInfo: " + ex.getMessage());
                }
            }
            callback.setSubject(subjectBean);
            AttributeStatementBean attrBean = new AttributeStatementBean();
            attrBean.setSubject(subjectBean);
            AttributeBean attributeBean = new AttributeBean();
            attributeBean.setNameFormat(SAML2Constants.ATTRNAME_FORMAT_UNSPECIFIED);
            if (saml2) {
                attributeBean.setQualifiedName(ROLE_URI);
                attributeBean.setNameFormat(SAML2Constants.ATTRNAME_FORMAT_UNSPECIFIED);
            } else {
                String uri = ROLE_URI;
                int lastSlash = uri.lastIndexOf('/');
                if (lastSlash == (uri.length() - 1)) {
                    uri = uri.substring(0, lastSlash);
                    lastSlash = uri.lastIndexOf('/');
                }
                String namespace = uri.substring(0, lastSlash);
                String name = uri.substring(lastSlash + 1, uri.length());
                attributeBean.setSimpleName(name);
                attributeBean.setQualifiedName(namespace);
            }
            attributeBean.addAttributeValue(roleName);
            attrBean.setSamlAttributes(Collections.singletonList(attributeBean));
            callback.setAttributeStatementData(Collections.singletonList(attrBean));
            try {
                Crypto crypto = CryptoFactory.getInstance(cryptoPropertiesFile);
                callback.setIssuerCrypto(crypto);
                callback.setIssuerKeyName(cryptoAlias);
                callback.setIssuerKeyPassword(cryptoPassword);
                callback.setSignAssertion(signAssertion);
            } catch (Exception ex) {
                throw new IOException("Problem creating KeyInfo: " + ex.getMessage());
            }
        }
    }
}
Also used : SubjectBean(org.apache.wss4j.common.saml.bean.SubjectBean) KeyInfoBean(org.apache.wss4j.common.saml.bean.KeyInfoBean) AttributeStatementBean(org.apache.wss4j.common.saml.bean.AttributeStatementBean) Crypto(org.apache.wss4j.common.crypto.Crypto) SAMLCallback(org.apache.wss4j.common.saml.SAMLCallback) IOException(java.io.IOException) AttributeBean(org.apache.wss4j.common.saml.bean.AttributeBean) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException)

Example 13 with KeyInfoBean

use of org.apache.wss4j.common.saml.bean.KeyInfoBean in project cxf by apache.

the class SAML2CallbackHandler method handle.

public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] instanceof SAMLCallback) {
            SAMLCallback callback = (SAMLCallback) callbacks[i];
            callback.setIssuer("www.example.com");
            callback.setSamlVersion(Version.SAML_20);
            SubjectBean subjectBean = new SubjectBean(subjectName, subjectQualifier, confirmationMethod);
            if (SAML2Constants.CONF_HOLDER_KEY.equals(confirmationMethod)) {
                try {
                    KeyInfoBean keyInfo = createKeyInfo();
                    subjectBean.setKeyInfo(keyInfo);
                } catch (Exception ex) {
                    throw new IOException("Problem creating KeyInfo: " + ex.getMessage());
                }
            }
            callback.setSubject(subjectBean);
            createAndSetStatement(null, callback);
            try {
                Crypto crypto = CryptoFactory.getInstance("outsecurity.properties");
                callback.setIssuerCrypto(crypto);
                callback.setIssuerKeyName("myalias");
                callback.setIssuerKeyPassword("myAliasPassword");
                callback.setSignAssertion(signAssertion);
            } catch (WSSecurityException e) {
                throw new IOException(e);
            }
        } else {
            throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
        }
    }
}
Also used : SubjectBean(org.apache.wss4j.common.saml.bean.SubjectBean) KeyInfoBean(org.apache.wss4j.common.saml.bean.KeyInfoBean) Crypto(org.apache.wss4j.common.crypto.Crypto) SAMLCallback(org.apache.wss4j.common.saml.SAMLCallback) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException)

Example 14 with KeyInfoBean

use of org.apache.wss4j.common.saml.bean.KeyInfoBean in project testcases by coheigea.

the class SamlCallbackHandler method handle.

public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] instanceof SAMLCallback) {
            SAMLCallback callback = (SAMLCallback) callbacks[i];
            if (saml2) {
                callback.setSamlVersion(Version.SAML_20);
            } else {
                callback.setSamlVersion(Version.SAML_11);
            }
            if (conditions != null) {
                callback.setConditions(conditions);
            }
            callback.setIssuer("sts");
            if (!saml2 && SAML2Constants.CONF_SENDER_VOUCHES.equals(confirmationMethod)) {
                confirmationMethod = SAML1Constants.CONF_SENDER_VOUCHES;
            }
            SubjectBean subjectBean = new SubjectBean(subjectName, null, confirmationMethod);
            if (SAML2Constants.CONF_HOLDER_KEY.equals(confirmationMethod) || SAML1Constants.CONF_HOLDER_KEY.equals(confirmationMethod)) {
                try {
                    KeyInfoBean keyInfo = createKeyInfo();
                    subjectBean.setKeyInfo(keyInfo);
                } catch (Exception ex) {
                    throw new IOException("Problem creating KeyInfo: " + ex.getMessage());
                }
            }
            callback.setSubject(subjectBean);
            AttributeStatementBean attrBean = new AttributeStatementBean();
            attrBean.setSubject(subjectBean);
            AttributeBean attributeBean = new AttributeBean();
            if (saml2) {
                attributeBean.setQualifiedName("subject-role");
            } else {
                attributeBean.setSimpleName("subject-role");
                attributeBean.setQualifiedName("http://custom-ns");
            }
            attributeBean.addAttributeValue("system-user");
            attrBean.setSamlAttributes(Collections.singletonList(attributeBean));
            callback.setAttributeStatementData(Collections.singletonList(attrBean));
            callback.setSignatureAlgorithm(signatureAlgorithm);
            callback.setSignatureDigestAlgorithm(digestAlgorithm);
            try {
                Crypto crypto = CryptoFactory.getInstance(cryptoPropertiesFile);
                callback.setIssuerCrypto(crypto);
                callback.setIssuerKeyName(cryptoAlias);
                callback.setIssuerKeyPassword(cryptoPassword);
                callback.setSignAssertion(signAssertion);
            } catch (WSSecurityException e) {
                throw new IOException(e);
            }
        }
    }
}
Also used : SubjectBean(org.apache.wss4j.common.saml.bean.SubjectBean) KeyInfoBean(org.apache.wss4j.common.saml.bean.KeyInfoBean) AttributeStatementBean(org.apache.wss4j.common.saml.bean.AttributeStatementBean) Crypto(org.apache.wss4j.common.crypto.Crypto) SAMLCallback(org.apache.wss4j.common.saml.SAMLCallback) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) IOException(java.io.IOException) AttributeBean(org.apache.wss4j.common.saml.bean.AttributeBean) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException)

Example 15 with KeyInfoBean

use of org.apache.wss4j.common.saml.bean.KeyInfoBean in project cxf by apache.

the class AbstractStaxBindingHandler method addIssuedToken.

protected SecurePart addIssuedToken(AbstractToken token, SecurityToken secToken, boolean signed, boolean endorsing) {
    assertToken(token);
    if (isTokenRequired(token.getIncludeTokenType())) {
        final Element el = secToken.getToken();
        if (el != null && "Assertion".equals(el.getLocalName()) && (WSSConstants.NS_SAML.equals(el.getNamespaceURI()) || WSSConstants.NS_SAML2.equals(el.getNamespaceURI()))) {
            WSSConstants.Action actionToPerform = WSSConstants.SAML_TOKEN_UNSIGNED;
            if (endorsing) {
                actionToPerform = WSSConstants.SAML_TOKEN_SIGNED;
            }
            properties.addAction(actionToPerform);
            // Mock up a Subject so that the SAMLTokenOutProcessor can get access to the certificate
            final SubjectBean subjectBean;
            if (signed || endorsing) {
                KeyInfoBean keyInfo = new KeyInfoBean();
                keyInfo.setCertificate(secToken.getX509Certificate());
                keyInfo.setEphemeralKey(secToken.getSecret());
                subjectBean = new SubjectBean("", "", "");
                subjectBean.setKeyInfo(keyInfo);
            } else {
                subjectBean = null;
            }
            CallbackHandler callbackHandler = new CallbackHandler() {

                @Override
                public void handle(Callback[] callbacks) {
                    for (Callback callback : callbacks) {
                        if (callback instanceof SAMLCallback) {
                            SAMLCallback samlCallback = (SAMLCallback) callback;
                            samlCallback.setAssertionElement(el);
                            samlCallback.setSubject(subjectBean);
                            if (WSS4JConstants.SAML_NS.equals(el.getNamespaceURI())) {
                                samlCallback.setSamlVersion(Version.SAML_11);
                            } else {
                                samlCallback.setSamlVersion(Version.SAML_20);
                            }
                        }
                    }
                }
            };
            properties.setSamlCallbackHandler(callbackHandler);
            QName qname = WSSConstants.TAG_SAML2_ASSERTION;
            if (WSS4JConstants.SAML_NS.equals(el.getNamespaceURI())) {
                qname = WSSConstants.TAG_SAML_ASSERTION;
            }
            return new SecurePart(qname, Modifier.Element);
        } else if (isRequestor()) {
            // An Encrypted Token...just include it as is
            properties.addAction(WSSConstants.CUSTOM_TOKEN);
        }
    }
    return null;
}
Also used : SecurePart(org.apache.xml.security.stax.ext.SecurePart) SubjectBean(org.apache.wss4j.common.saml.bean.SubjectBean) KeyInfoBean(org.apache.wss4j.common.saml.bean.KeyInfoBean) CallbackHandler(javax.security.auth.callback.CallbackHandler) WSSConstants(org.apache.wss4j.stax.ext.WSSConstants) WSPasswordCallback(org.apache.wss4j.common.ext.WSPasswordCallback) SAMLCallback(org.apache.wss4j.common.saml.SAMLCallback) Callback(javax.security.auth.callback.Callback) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) SAMLCallback(org.apache.wss4j.common.saml.SAMLCallback)

Aggregations

KeyInfoBean (org.apache.wss4j.common.saml.bean.KeyInfoBean)22 Crypto (org.apache.wss4j.common.crypto.Crypto)15 SubjectBean (org.apache.wss4j.common.saml.bean.SubjectBean)12 SAMLCallback (org.apache.wss4j.common.saml.SAMLCallback)11 IOException (java.io.IOException)10 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)10 X509Certificate (java.security.cert.X509Certificate)7 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)7 AttributeBean (org.apache.wss4j.common.saml.bean.AttributeBean)7 AttributeStatementBean (org.apache.wss4j.common.saml.bean.AttributeStatementBean)7 CryptoType (org.apache.wss4j.common.crypto.CryptoType)6 Element (org.w3c.dom.Element)4 KeyGenerator (javax.crypto.KeyGenerator)3 SecretKey (javax.crypto.SecretKey)3 WSSecEncryptedKey (org.apache.wss4j.dom.message.WSSecEncryptedKey)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 Document (org.w3c.dom.Document)2 InputStream (java.io.InputStream)1 Principal (java.security.Principal)1