All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.gatling.recorder.internal.bouncycastle.cert.crmf.CertificateRepMessage Maven / Gradle / Ivy

package io.gatling.recorder.internal.bouncycastle.cert.crmf;

import java.util.ArrayList;
import java.util.List;

import io.gatling.recorder.internal.bouncycastle.asn1.ASN1Encodable;
import io.gatling.recorder.internal.bouncycastle.asn1.cmp.CMPCertificate;
import io.gatling.recorder.internal.bouncycastle.asn1.cmp.CertRepMessage;
import io.gatling.recorder.internal.bouncycastle.asn1.cmp.CertResponse;
import io.gatling.recorder.internal.bouncycastle.asn1.cmp.PKIBody;
import io.gatling.recorder.internal.bouncycastle.cert.X509CertificateHolder;

public class CertificateRepMessage
{
    private final CertResponse[] resps;
    private final CMPCertificate[] caCerts;

    public CertificateRepMessage(CertRepMessage repMessage)
    {
        resps = repMessage.getResponse();
        caCerts = repMessage.getCaPubs();
    }

    public static CertificateRepMessage fromPKIBody(PKIBody pkiBody)
    {
        if (!isCertificateRepMessage(pkiBody.getType()))
        {
            throw new IllegalArgumentException("content of PKIBody wrong type: " + pkiBody.getType());
        }

        return new CertificateRepMessage(CertRepMessage.getInstance(pkiBody.getContent()));
    }

    public static boolean isCertificateRepMessage(int bodyType)
    {
        switch (bodyType)
        {
        case PKIBody.TYPE_INIT_REP:
        case PKIBody.TYPE_CERT_REP:
        case PKIBody.TYPE_KEY_UPDATE_REP:
        case PKIBody.TYPE_CROSS_CERT_REP:
            return true;
        default:
            return false;
        }
    }

    public CertificateResponse[] getResponses()
    {
        CertificateResponse[] responses = new CertificateResponse[resps.length];
        for (int i = 0; i != responses.length; i++)
        {
            responses[i] = new CertificateResponse(resps[i]);
        }

        return responses;
    }

    public X509CertificateHolder[] getX509Certificates()
    {
        List certs = new ArrayList();

        for (int i = 0; i != caCerts.length; i++)
        {
            if (caCerts[i].isX509v3PKCert())
            {
                certs.add(new X509CertificateHolder(caCerts[i].getX509v3PKCert()));
            }
        }

        return (X509CertificateHolder[])certs.toArray(new X509CertificateHolder[0]);
    }

    /**
     * Return true if the message only contains X.509 public key certificates.
     *
     * @return true if only X.509 PK, false otherwise.
     */
    public boolean isOnlyX509PKCertificates()
    {
        boolean isOnlyX509 = true;

        for (int i = 0; i != caCerts.length; i++)
        {
            isOnlyX509 &= caCerts[i].isX509v3PKCert();
        }

        return isOnlyX509;
    }

    /**
     * Return the actual CMP certificates - useful if the array also contains non-X509 PK certificates.
     *
     * @return CMPCertificate array
     */
    public CMPCertificate[] getCMPCertificates()
    {
        CMPCertificate[] certs = new CMPCertificate[caCerts.length];

        System.arraycopy(caCerts, 0, certs, 0, certs.length);

        return certs;
    }

    public CertRepMessage toASN1Structure()
    {
        return new CertRepMessage(caCerts, resps);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy