/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2015 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.wildfly.security.credential;

import org.wildfly.common.Assert;
import org.wildfly.security.evidence.AlgorithmEvidence;
import org.wildfly.security.evidence.Evidence;

/**
 * A credential is a piece of information that can be used to verify or produce evidence.
 */
public interface Credential {

    /**
     * Determine whether this credential can, generally speaking, verify the given evidence type.
     *
     * @param evidenceClass the evidence type (must not be {@code null})
     * @param algorithmName the evidence algorithm name (may be {@code null} if the type of evidence does not support
     * algorithm names)
     *
     * @return {@code true} if the evidence can be verified by this credential, {@code false} otherwise
     */
    default boolean canVerify(Class<? extends Evidence> evidenceClass, String algorithmName) {
        Assert.checkNotNullParam("evidenceClass", evidenceClass);
        return false;
    }

    /**
     * Determine whether this credential can verify the given evidence.
     *
     * @param evidence the evidence (must not be {@code null})
     *
     * @return {@code true} if the evidence can be verified by this credential, {@code false} otherwise
     */
    default boolean canVerify(Evidence evidence) {
        Assert.checkNotNullParam("evidence", evidence);
        return canVerify(evidence.getClass(), evidence instanceof AlgorithmEvidence ? ((AlgorithmEvidence) evidence).getAlgorithm() : null);
    }

    /**
     * Verify the given evidence.
     *
     * @param evidence the evidence to verify (must not be {@code null})
     *
     * @return {@code true} if the evidence is verified, {@code false} otherwise
     */
    default boolean verify(Evidence evidence) {
        Assert.checkNotNullParam("evidence", evidence);
        return false;
    }
}

@@ -743,16 +743,91 @@
 ntial;%0A%0A
+import java.security.KeyStore;%0Aimport java.security.cert.X509Certificate;%0A%0A
 import o
@@ -954,16 +954,107 @@
 vidence;
+%0Aimport org.wildfly.security.keystore.PasswordEntry;%0Aimport org.wildfly.security.x500.X500;
 %0A%0A/**%0A *
@@ -2686,11 +2686,1312 @@
 e;%0A    %7D
+%0A%0A    /**%0A     * Convert a key store entry into a credential object.%0A     *%0A     * @param keyStoreEntry the key store entry to convert (must not be %7B@code null%7D)%0A     * @return the corresponding credential, or %7B@code null%7D if the entry type is unrecognized%0A     */%0A    static Credential fromKeyStoreEntry(KeyStore.Entry keyStoreEntry) %7B%0A        Assert.checkNotNullParam(%22keyStoreEntry%22, keyStoreEntry);%0A        if (keyStoreEntry instanceof PasswordEntry) %7B%0A            return new PasswordCredential(((PasswordEntry) keyStoreEntry).getPassword());%0A        %7D else if (keyStoreEntry instanceof KeyStore.PrivateKeyEntry) %7B%0A            return new X509CertificateChainPrivateCredential(((KeyStore.PrivateKeyEntry) keyStoreEntry).getPrivateKey(), X500.asX509CertificateArray(((KeyStore.PrivateKeyEntry) keyStoreEntry).getCertificateChain()));%0A        %7D else if (keyStoreEntry instanceof KeyStore.TrustedCertificateEntry) %7B%0A            return new X509CertificateChainPublicCredential((X509Certificate) ((KeyStore.TrustedCertificateEntry) keyStoreEntry).getTrustedCertificate());%0A        %7D else if (keyStoreEntry instanceof KeyStore.SecretKeyEntry) %7B%0A            return new SecretKeyCredential(((KeyStore.SecretKeyEntry) keyStoreEntry).getSecretKey());%0A        %7D else %7B%0A            return null;%0A        %7D%0A    %7D
 %0A%7D%0A

Add utility to construct a credential from a key store entry