using System;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Cms;
using Org.BouncyCastle.Asn1.Crmf;
using Org.BouncyCastle.Cms;
namespace Org.BouncyCastle.Crmf
{
public class PkiArchiveControl
: IControl
{
public static readonly int encryptedPrivKey = PkiArchiveOptions.encryptedPrivKey;
public static readonly int keyGenParameters = PkiArchiveOptions.keyGenParameters;
public static readonly int archiveRemGenPrivKey = PkiArchiveOptions.archiveRemGenPrivKey;
private readonly PkiArchiveOptions m_pkiArchiveOptions;
///
/// Basic constructor - build from an PKIArchiveOptions structure.
///
/// the ASN.1 structure that will underlie this control.
public PkiArchiveControl(PkiArchiveOptions pkiArchiveOptions)
{
m_pkiArchiveOptions = pkiArchiveOptions;
}
///
/// Return the type of this control.
///
/// CRMFObjectIdentifiers.id_regCtrl_pkiArchiveOptions
public DerObjectIdentifier Type => CrmfObjectIdentifiers.id_regCtrl_pkiArchiveOptions;
///
/// Return the underlying ASN.1 object.
///
/// a PKIArchiveOptions structure.
public Asn1Encodable Value => m_pkiArchiveOptions;
///
/// Return the archive control type, one of: encryptedPrivKey,keyGenParameters,or archiveRemGenPrivKey.
///
/// the archive control type.
public int ArchiveType => m_pkiArchiveOptions.Type;
///
/// Return whether this control contains enveloped data.
///
/// true if the control contains enveloped data, false otherwise.
[Obsolete("Use 'IsEnvelopedData' instead")]
public bool EnvelopedData => IsEnvelopedData();
///
/// Return whether this control contains enveloped data.
///
/// true if the control contains enveloped data, false otherwise.
public bool IsEnvelopedData()
{
EncryptedKey encKey = EncryptedKey.GetInstance(m_pkiArchiveOptions.Value);
return !encKey.IsEncryptedValue;
}
///
/// Return the enveloped data structure contained in this control.
///
/// a CMSEnvelopedData object.
public CmsEnvelopedData GetEnvelopedData()
{
try
{
EncryptedKey encKey = EncryptedKey.GetInstance(m_pkiArchiveOptions.Value);
EnvelopedData data = Asn1.Cms.EnvelopedData.GetInstance(encKey.Value);
return new CmsEnvelopedData(new ContentInfo(CmsObjectIdentifiers.EnvelopedData, data));
}
catch (CmsException e)
{
throw new CrmfException("CMS parsing error: " + e.Message, e);
}
catch (Exception e)
{
throw new CrmfException("CRMF parsing error: " + e.Message, e);
}
}
}
}