Search in sources :

Example 1 with Password

use of org.wildfly.security.password.Password in project fuse-karaf by jboss-fuse.

the class Activator method replaced.

/**
 * Replaces any value that is given in Credential Store reference format with the value from the Credential Store by
 * using {@link System#setProperty(String, String)}.
 *
 * @param credentialStore
 *            {@link CredentialStore} containing the secret values
 * @param key
 *            property key
 * @param value
 *            property value, expected to be in Credential store reference format
 * @return true if any replacement was done
 */
boolean replaced(final CredentialStore credentialStore, final String key, final String value) {
    if (!CredentialStoreHelper.couldBeCredentialStoreAlias(value)) {
        return false;
    }
    final String alias = CredentialStoreHelper.toCredentialStoreAlias(value);
    final PasswordCredential passwordCredential;
    try {
        passwordCredential = credentialStore.retrieve(alias, PasswordCredential.class);
    } catch (final CredentialStoreException e) {
        return false;
    }
    if (passwordCredential == null) {
        return false;
    }
    final Password password = passwordCredential.getPassword();
    final ClearPassword clearPassword = password.castAs(ClearPassword.class);
    final char[] rawClearPassword = clearPassword.getPassword();
    System.setProperty(key, String.valueOf(rawClearPassword));
    return true;
}
Also used : ClearPassword(org.wildfly.security.password.interfaces.ClearPassword) PasswordCredential(org.wildfly.security.credential.PasswordCredential) CredentialStoreException(org.wildfly.security.credential.store.CredentialStoreException) Password(org.wildfly.security.password.Password) ClearPassword(org.wildfly.security.password.interfaces.ClearPassword)

Example 2 with Password

use of org.wildfly.security.password.Password in project wildfly-swarm by wildfly-swarm.

the class AuthCallbackHandler method handle.

@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback current : callbacks) {
        if (current instanceof NameCallback) {
            NameCallback ncb = (NameCallback) current;
            ncb.setName(this.userName);
        } else if (current instanceof RealmCallback) {
            RealmCallback rcb = (RealmCallback) current;
            rcb.setText(rcb.getDefaultText());
        } else if (current instanceof CredentialCallback) {
            CredentialCallback ccb = (CredentialCallback) current;
            try {
                DigestPasswordAlgorithmSpec algoSpec = new DigestPasswordAlgorithmSpec(this.userName, this.realm);
                EncryptablePasswordSpec passwordSpec = new EncryptablePasswordSpec(this.password.toCharArray(), algoSpec);
                Password passwd = PasswordFactory.getInstance(ALGORITHM_DIGEST_MD5).generatePassword(passwordSpec);
                Credential creds = new PasswordCredential(passwd);
                ccb.setCredential(creds);
            } catch (InvalidKeySpecException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        } else if (current instanceof PasswordCallback) {
            PasswordCallback pcb = (PasswordCallback) current;
            pcb.setPassword(this.password.toCharArray());
        } else {
            throw new UnsupportedCallbackException(current);
        }
    }
}
Also used : PasswordCredential(org.wildfly.security.credential.PasswordCredential) Credential(org.wildfly.security.credential.Credential) PasswordCredential(org.wildfly.security.credential.PasswordCredential) CredentialCallback(org.wildfly.security.auth.callback.CredentialCallback) EncryptablePasswordSpec(org.wildfly.security.password.spec.EncryptablePasswordSpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) RealmCallback(javax.security.sasl.RealmCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) CredentialCallback(org.wildfly.security.auth.callback.CredentialCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) DigestPasswordAlgorithmSpec(org.wildfly.security.password.spec.DigestPasswordAlgorithmSpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) RealmCallback(javax.security.sasl.RealmCallback) Password(org.wildfly.security.password.Password)

Example 3 with Password

use of org.wildfly.security.password.Password in project wildfly by wildfly.

the class SubjectUtil method convertToSecurityIdentity.

public static SecurityIdentity convertToSecurityIdentity(Subject subject, Principal principal, SecurityDomain domain, String roleCategory) {
    SecurityIdentity identity = null;
    for (Object obj : subject.getPrivateCredentials()) {
        if (obj instanceof SecurityIdentity) {
            identity = (SecurityIdentity) obj;
            break;
        }
    }
    if (identity == null) {
        identity = domain.createAdHocIdentity(principal);
    }
    // convert public credentials
    IdentityCredentials publicCredentials = IdentityCredentials.NONE;
    for (Object credential : subject.getPublicCredentials()) {
        if (credential instanceof PublicKey) {
            publicCredentials = publicCredentials.withCredential(new PublicKeyCredential((PublicKey) credential));
        } else if (credential instanceof X509Certificate) {
            publicCredentials = publicCredentials.withCredential(new X509CertificateChainPublicCredential((X509Certificate) credential));
        } else if (credential instanceof Credential) {
            publicCredentials = publicCredentials.withCredential((Credential) credential);
        }
    }
    if (!publicCredentials.equals(IdentityCredentials.NONE)) {
        identity = identity.withPublicCredentials(publicCredentials);
    }
    // convert private credentials
    IdentityCredentials privateCredentials = IdentityCredentials.NONE;
    for (Object credential : subject.getPrivateCredentials()) {
        if (credential instanceof Password) {
            privateCredentials = privateCredentials.withCredential(new PasswordCredential((Password) credential));
        } else if (credential instanceof SecretKey) {
            privateCredentials = privateCredentials.withCredential(new SecretKeyCredential((SecretKey) credential));
        } else if (credential instanceof KeyPair) {
            privateCredentials = privateCredentials.withCredential(new KeyPairCredential((KeyPair) credential));
        } else if (credential instanceof PrivateKey) {
            privateCredentials = privateCredentials.withCredential(new X509CertificateChainPrivateCredential((PrivateKey) credential));
        } else if (credential instanceof Credential) {
            privateCredentials = privateCredentials.withCredential((Credential) credential);
        }
    }
    if (!privateCredentials.equals(IdentityCredentials.NONE)) {
        identity = identity.withPrivateCredentials(privateCredentials);
    }
    return identity;
}
Also used : X509CertificateChainPrivateCredential(org.wildfly.security.credential.X509CertificateChainPrivateCredential) X509CertificateChainPublicCredential(org.wildfly.security.credential.X509CertificateChainPublicCredential) PublicKeyCredential(org.wildfly.security.credential.PublicKeyCredential) KeyPairCredential(org.wildfly.security.credential.KeyPairCredential) PasswordCredential(org.wildfly.security.credential.PasswordCredential) SecretKeyCredential(org.wildfly.security.credential.SecretKeyCredential) Credential(org.wildfly.security.credential.Credential) KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) X509CertificateChainPrivateCredential(org.wildfly.security.credential.X509CertificateChainPrivateCredential) PublicKey(java.security.PublicKey) PasswordCredential(org.wildfly.security.credential.PasswordCredential) PublicKeyCredential(org.wildfly.security.credential.PublicKeyCredential) X509Certificate(java.security.cert.X509Certificate) SecretKeyCredential(org.wildfly.security.credential.SecretKeyCredential) SecurityIdentity(org.wildfly.security.auth.server.SecurityIdentity) SecretKey(javax.crypto.SecretKey) KeyPairCredential(org.wildfly.security.credential.KeyPairCredential) X509CertificateChainPublicCredential(org.wildfly.security.credential.X509CertificateChainPublicCredential) IdentityCredentials(org.wildfly.security.auth.server.IdentityCredentials) Password(org.wildfly.security.password.Password)

Example 4 with Password

use of org.wildfly.security.password.Password in project wildfly by wildfly.

the class ReadCredentialServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("text/plain");
    resp.setCharacterEncoding("UTF-8");
    final PrintWriter writer = resp.getWriter();
    final String credentialStore = req.getParameter(PARAM_CREDENTIAL_STORE);
    final String alias = req.getParameter(PARAM_ALIAS);
    String separator = req.getParameter(PARAM_SEPARATOR);
    if (separator == null) {
        separator = PARAM_SEPARATOR_DEFAULT;
    }
    ServiceRegistry registry = CurrentServiceContainer.getServiceContainer();
    if (credentialStore == null || credentialStore.length() == 0) {
        for (ServiceName name : registry.getServiceNames()) {
            if (SERVICE_NAME_CRED_STORE.equals(name.getParent())) {
                writer.print(name.getSimpleName());
                writer.print(separator);
            }
        }
        return;
    }
    ServiceController<?> credStoreService = registry.getService(ServiceName.of(SERVICE_NAME_CRED_STORE, credentialStore));
    if (credStoreService == null) {
        resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
        writer.print(credentialStore + " not found");
        return;
    }
    CredentialStore cs = (CredentialStore) credStoreService.getValue();
    if (alias == null || alias.length() == 0) {
        try {
            for (String csAlias : cs.getAliases()) {
                writer.print(csAlias);
                writer.print(separator);
            }
        } catch (UnsupportedOperationException | CredentialStoreException e) {
            throw new ServletException("Unable to list aliases", e);
        }
        return;
    }
    String clearPassword = null;
    try {
        if (cs.exists(alias, PasswordCredential.class)) {
            Password password = cs.retrieve(alias, PasswordCredential.class).getPassword();
            if (password instanceof ClearPassword) {
                clearPassword = new String(((ClearPassword) password).getPassword());
            }
        }
    } catch (CredentialStoreException | IllegalStateException e) {
        throw new ServletException("Unable to retrieve password  from credential store", e);
    }
    if (clearPassword == null) {
        resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
        writer.print(alias + " password not found in " + credentialStore);
    } else {
        writer.print(clearPassword);
    }
}
Also used : ClearPassword(org.wildfly.security.password.interfaces.ClearPassword) PasswordCredential(org.wildfly.security.credential.PasswordCredential) CredentialStoreException(org.wildfly.security.credential.store.CredentialStoreException) ServletException(javax.servlet.ServletException) ServiceName(org.jboss.msc.service.ServiceName) CredentialStore(org.wildfly.security.credential.store.CredentialStore) ServiceRegistry(org.jboss.msc.service.ServiceRegistry) PrintWriter(java.io.PrintWriter) Password(org.wildfly.security.password.Password) ClearPassword(org.wildfly.security.password.interfaces.ClearPassword)

Example 5 with Password

use of org.wildfly.security.password.Password in project fuse-karaf by jboss-fuse.

the class MaskedPasswordHelper method createCredentialSource.

@Override
public CredentialSource createCredentialSource(final Map<String, String> configuration) throws GeneralSecurityException, IOException {
    final String algorithmParamsBase64 = option(configuration, CREDENTIAL_STORE_PROTECTION_PARAMS, "");
    final Decoder decoder = Base64.getDecoder();
    final byte[] encodedAlgorithmParams = decoder.decode(algorithmParamsBase64);
    final String algorithm = option(configuration, CREDENTIAL_STORE_PROTECTION_ALGORITHM, DEFAULT_ALGORITHM);
    final Provider provider = ProviderHelper.provider(option(configuration, CREDENTIAL_STORE_PROTECTION_PROVIDER, ProviderHelper.WILDFLY_PROVIDER));
    final AlgorithmParameters algorithmParameters = AlgorithmParameters.getInstance(algorithm, provider);
    algorithmParameters.init(encodedAlgorithmParams);
    final MaskedPasswordAlgorithmSpec maskedPasswordAlgorithmSpec = algorithmParameters.getParameterSpec(MaskedPasswordAlgorithmSpec.class);
    final char[] initialKeyMaterial = maskedPasswordAlgorithmSpec.getInitialKeyMaterial();
    final int iterationCount = maskedPasswordAlgorithmSpec.getIterationCount();
    final byte[] salt = maskedPasswordAlgorithmSpec.getSalt();
    final String maskedPasswordBase64 = option(configuration, CREDENTIAL_STORE_PROTECTION, "");
    final byte[] maskedPasswordBytes = decoder.decode(maskedPasswordBase64);
    final MaskedPasswordSpec maskedPasswordSpec = new MaskedPasswordSpec(initialKeyMaterial, iterationCount, salt, maskedPasswordBytes);
    final PasswordFactory passwordFactory = PasswordFactory.getInstance(algorithm, provider);
    final Password maskedPassword = passwordFactory.generatePassword(maskedPasswordSpec);
    final PasswordFactory clearPasswordFactory = PasswordFactory.getInstance(ClearPassword.ALGORITHM_CLEAR, provider);
    final ClearPasswordSpec clearPasswordSpec = passwordFactory.getKeySpec(maskedPassword, ClearPasswordSpec.class);
    final Password password = clearPasswordFactory.generatePassword(clearPasswordSpec);
    final PasswordCredential passwordCredential = new PasswordCredential(password);
    return IdentityCredentials.NONE.withCredential(passwordCredential);
}
Also used : MaskedPasswordSpec(org.wildfly.security.password.spec.MaskedPasswordSpec) PasswordCredential(org.wildfly.security.credential.PasswordCredential) ClearPasswordSpec(org.wildfly.security.password.spec.ClearPasswordSpec) Decoder(java.util.Base64.Decoder) Provider(java.security.Provider) PasswordFactory(org.wildfly.security.password.PasswordFactory) MaskedPasswordAlgorithmSpec(org.wildfly.security.password.spec.MaskedPasswordAlgorithmSpec) AlgorithmParameters(java.security.AlgorithmParameters) MaskedPassword(org.wildfly.security.password.interfaces.MaskedPassword) Password(org.wildfly.security.password.Password) ClearPassword(org.wildfly.security.password.interfaces.ClearPassword)

Aggregations

PasswordCredential (org.wildfly.security.credential.PasswordCredential)9 Password (org.wildfly.security.password.Password)9 ClearPassword (org.wildfly.security.password.interfaces.ClearPassword)6 ClearPasswordSpec (org.wildfly.security.password.spec.ClearPasswordSpec)5 CredentialStore (org.wildfly.security.credential.store.CredentialStore)4 PasswordFactory (org.wildfly.security.password.PasswordFactory)4 HashMap (java.util.HashMap)3 WildFlyElytronProvider (org.wildfly.security.WildFlyElytronProvider)3 Credential (org.wildfly.security.credential.Credential)3 CredentialSource (org.wildfly.security.credential.source.CredentialSource)3 Provider (java.security.Provider)2 Test (org.junit.Test)2 CredentialStoreException (org.wildfly.security.credential.store.CredentialStoreException)2 MaskedPassword (org.wildfly.security.password.interfaces.MaskedPassword)2 File (java.io.File)1 PrintWriter (java.io.PrintWriter)1 AlgorithmParameters (java.security.AlgorithmParameters)1 KeyPair (java.security.KeyPair)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 PrivateKey (java.security.PrivateKey)1