// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.cert.crmf;

import org.bouncycastle.cms.RecipientInformation;
import org.bouncycastle.asn1.cmp.CMPCertificate;
import org.bouncycastle.cms.Recipient;
import org.bouncycastle.cms.CMSException;
import org.bouncycastle.asn1.cms.ContentInfo;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.cms.CMSEnvelopedData;
import org.bouncycastle.asn1.cmp.CertResponse;

public class CertificateResponse
{
    private final CertResponse certResponse;
    
    public CertificateResponse(final CertResponse certResponse) {
        this.certResponse = certResponse;
    }
    
    public boolean hasEncryptedCertificate() {
        return this.certResponse.getCertifiedKeyPair().getCertOrEncCert().hasEncryptedCertificate();
    }
    
    public CMSEnvelopedData getEncryptedCertificate() throws CMSException {
        if (!this.hasEncryptedCertificate()) {
            throw new IllegalStateException("encrypted certificate asked for, none found");
        }
        final CMSEnvelopedData cmsEnvelopedData = new CMSEnvelopedData(new ContentInfo(PKCSObjectIdentifiers.envelopedData, this.certResponse.getCertifiedKeyPair().getCertOrEncCert().getEncryptedCert().getValue()));
        if (cmsEnvelopedData.getRecipientInfos().size() != 1) {
            throw new IllegalStateException("data encrypted for more than one recipient");
        }
        return cmsEnvelopedData;
    }
    
    public CMPCertificate getCertificate(final Recipient recipient) throws CMSException {
        return CMPCertificate.getInstance(this.getEncryptedCertificate().getRecipientInfos().getRecipients().iterator().next().getContent(recipient));
    }
    
    public CMPCertificate getCertificate() throws CMSException {
        if (this.hasEncryptedCertificate()) {
            throw new IllegalStateException("plaintext certificate asked for, none found");
        }
        return this.certResponse.getCertifiedKeyPair().getCertOrEncCert().getCertificate();
    }
    
    public CertResponse toASN1Structure() {
        return this.certResponse;
    }
}
