<PackageReference Include="BouncyCastle.Cryptography" Version="2.6.2" />

CertificateResponse

public class CertificateResponse
High level wrapper for the CertResponse CRMF structure.
using Org.BouncyCastle.Asn1.Cmp; using Org.BouncyCastle.Asn1.Cms; using Org.BouncyCastle.Asn1.Pkcs; using Org.BouncyCastle.Cms; using System; namespace Org.BouncyCastle.Crmf { public class CertificateResponse { private readonly CertResponse m_certResponse; public virtual bool HasEncryptedCertificate => m_certResponse.CertifiedKeyPair.CertOrEncCert.HasEncryptedCertificate; public CertificateResponse(CertResponse certResponse) { m_certResponse = certResponse; } public virtual CmsEnvelopedData GetEncryptedCertificate() { if (!HasEncryptedCertificate) throw new InvalidOperationException("encrypted certificate asked for, none found"); CertifiedKeyPair certifiedKeyPair = m_certResponse.CertifiedKeyPair; CmsEnvelopedData cmsEnvelopedData = new CmsEnvelopedData(new Org.BouncyCastle.Asn1.Cms.ContentInfo(PkcsObjectIdentifiers.EnvelopedData, certifiedKeyPair.CertOrEncCert.EncryptedCert.Value)); if (cmsEnvelopedData.GetRecipientInfos().Count != 1) throw new InvalidOperationException("data encrypted for more than one recipient"); return cmsEnvelopedData; } public virtual CmpCertificate GetCertificate() { if (HasEncryptedCertificate) throw new InvalidOperationException("plaintext certificate asked for, none found"); return m_certResponse.CertifiedKeyPair.CertOrEncCert.Certificate; } public virtual CertResponse ToAsn1Structure() { return m_certResponse; } } }