RecipientInfo
using System;
namespace Org.BouncyCastle.Asn1.Cms
{
public class RecipientInfo : Asn1Encodable, IAsn1Choice
{
private readonly Asn1Encodable m_info;
[Obsolete("Will be removed")]
public DerInteger Version {
get {
Asn1TaggedObject optional = Asn1TaggedObject.GetOptional(m_info);
if (optional == null)
return KeyTransRecipientInfo.GetInstance(m_info).Version;
if (optional.HasContextTag()) {
switch (optional.TagNo) {
case 1:
return KeyAgreeRecipientInfo.GetTagged(optional, false).Version;
case 2:
return GetKekInfo(optional).Version;
case 3:
return PasswordRecipientInfo.GetTagged(optional, false).Version;
case 4:
return DerInteger.Zero;
}
}
throw new InvalidOperationException("unknown tag");
}
}
public bool IsTagged => m_info is Asn1TaggedObject;
public Asn1Encodable Info {
get {
Asn1TaggedObject optional = Asn1TaggedObject.GetOptional(m_info);
if (optional == null)
return KeyTransRecipientInfo.GetInstance(m_info);
if (optional.HasContextTag()) {
switch (optional.TagNo) {
case 1:
return KeyAgreeRecipientInfo.GetTagged(optional, false);
case 2:
return GetKekInfo(optional);
case 3:
return PasswordRecipientInfo.GetTagged(optional, false);
case 4:
return OtherRecipientInfo.GetTagged(optional, false);
}
}
throw new InvalidOperationException("unknown tag");
}
}
public static RecipientInfo GetInstance(object o)
{
return Asn1Utilities.GetInstanceChoice(o, GetOptional);
}
public static RecipientInfo GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
{
return Asn1Utilities.GetInstanceChoice(taggedObject, declaredExplicit, GetInstance);
}
public static RecipientInfo GetOptional(Asn1Encodable element)
{
if (element == null)
throw new ArgumentNullException("element");
RecipientInfo recipientInfo = element as RecipientInfo;
if (recipientInfo != null)
return recipientInfo;
KeyTransRecipientInfo optional = KeyTransRecipientInfo.GetOptional(element);
if (optional != null)
return new RecipientInfo(optional);
Asn1TaggedObject optional2 = Asn1TaggedObject.GetOptional(element);
if (optional2 != null) {
if (optional2.HasContextTag(1))
return new RecipientInfo(KeyAgreeRecipientInfo.GetTagged(optional2, false));
if (optional2.HasContextTag(2))
return new RecipientInfo(GetKekInfo(optional2));
if (optional2.HasContextTag(3))
return new RecipientInfo(PasswordRecipientInfo.GetTagged(optional2, false));
if (optional2.HasContextTag(4))
return new RecipientInfo(OtherRecipientInfo.GetTagged(optional2, false));
}
return null;
}
public static RecipientInfo GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit)
{
return Asn1Utilities.GetTaggedChoice(taggedObject, declaredExplicit, GetInstance);
}
public RecipientInfo(KeyTransRecipientInfo info)
{
if (info == null)
throw new ArgumentNullException("info");
m_info = info;
}
public RecipientInfo(KeyAgreeRecipientInfo info)
{
m_info = new DerTaggedObject(false, 1, info);
}
public RecipientInfo(KekRecipientInfo info)
{
m_info = new DerTaggedObject(false, 2, info);
}
public RecipientInfo(PasswordRecipientInfo info)
{
m_info = new DerTaggedObject(false, 3, info);
}
public RecipientInfo(OtherRecipientInfo info)
{
m_info = new DerTaggedObject(false, 4, info);
}
[Obsolete("Will be removed")]
public RecipientInfo(Asn1Object info)
{
if (info == null)
throw new ArgumentNullException("info");
m_info = info;
}
public override Asn1Object ToAsn1Object()
{
return m_info.ToAsn1Object();
}
internal bool IsKeyTransV0()
{
if (m_info is Asn1TaggedObject)
return false;
return KeyTransRecipientInfo.GetInstance(m_info).Version.HasValue(0);
}
internal bool IsPasswordOrOther()
{
Asn1TaggedObject asn1TaggedObject = m_info as Asn1TaggedObject;
if (asn1TaggedObject != null && asn1TaggedObject.HasContextTag()) {
int tagNo = asn1TaggedObject.TagNo;
if ((uint)(tagNo - 3) <= 1)
return true;
}
return false;
}
private static KekRecipientInfo GetKekInfo(Asn1TaggedObject tagged)
{
bool declaredExplicit = tagged.IsExplicit();
return KekRecipientInfo.GetTagged(tagged, declaredExplicit);
}
}
}