/*
 * 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 java.security.cert.X509Certificate;
import org.wildfly.common.Assert;
import org.wildfly.security._private.ElytronMessages;

abstract class AbstractX509CertificateChainCredential implements X509CertificateChainCredential {

    final X509Certificate[] certificateChain;

    AbstractX509CertificateChainCredential(X509Certificate... certificateChain) {
        Assert.checkNotNullParam("certificateChain", certificateChain);
        if (certificateChain.length > 0) {
            this.certificateChain = certificateChain.clone();
            final int length = this.certificateChain.length;
            for (int i = 0; i < length; i++) {
                Assert.checkNotNullArrayParam("certificateChain", i, this.certificateChain[i]);
            }
        } else {
            throw ElytronMessages.log.certificateChainIsEmpty();
        }
    }

    public String getAlgorithm() {
        return getLastCertificate().getPublicKey().getAlgorithm();
    }

    public X509Certificate[] getCertificateChain() {
        return certificateChain.clone();
    }

    public X509Certificate getFirstCertificate() {
        return certificateChain[0];
    }

    public X509Certificate getLastCertificate() {
        return certificateChain[certificateChain.length - 1];
    }
}

@@ -773,111 +773,306 @@
 ert.
-X509Certificate;%0Aimport org.wildfly.common.Assert;%0Aimport org.wildfly.security._private.ElytronMessages
+CertificateEncodingException;%0Aimport java.security.cert.X509Certificate;%0Aimport java.util.Arrays;%0A%0Aimport org.wildfly.common.Assert;%0Aimport org.wildfly.security._private.ElytronMessages;%0Aimport org.wildfly.security.evidence.Evidence;%0Aimport org.wildfly.security.evidence.X509PeerCertificateEvidence
 ;%0A%0Aa
@@ -1784,32 +1784,804 @@
        %7D%0A    %7D%0A%0A
+    public boolean canVerify(final Class%3C? extends Evidence%3E evidenceClass, final String algorithmName) %7B%0A        return evidenceClass == X509PeerCertificateEvidence.class && getAlgorithm().equals(algorithmName);%0A    %7D%0A%0A    public boolean verify(final Evidence evidence) %7B%0A        if (evidence instanceof X509PeerCertificateEvidence) %7B%0A            final X509PeerCertificateEvidence peerCertificateEvidence = (X509PeerCertificateEvidence) evidence;%0A            try %7B%0A                return getAlgorithm().equals(peerCertificateEvidence.getAlgorithm()) && Arrays.equals(getFirstCertificate().getEncoded(), peerCertificateEvidence.getPeerCertificate().getEncoded());%0A            %7D catch (CertificateEncodingException e) %7B%0A            %7D%0A        %7D%0A        return false;%0A    %7D%0A%0A
     public Strin

Allow X509 credentials to verify X509 evidence directly