diff options
Diffstat (limited to 'crypto/src/asn1/cms/RecipientInfo.cs')
-rw-r--r-- | crypto/src/asn1/cms/RecipientInfo.cs | 138 |
1 files changed, 76 insertions, 62 deletions
diff --git a/crypto/src/asn1/cms/RecipientInfo.cs b/crypto/src/asn1/cms/RecipientInfo.cs index 85bd7e585..424ea4d10 100644 --- a/crypto/src/asn1/cms/RecipientInfo.cs +++ b/crypto/src/asn1/cms/RecipientInfo.cs @@ -29,107 +29,100 @@ namespace Org.BouncyCastle.Asn1.Cms return Asn1Utilities.GetInstanceFromChoice(taggedObject, declaredExplicit, GetInstance); } - internal Asn1Encodable info; + private readonly Asn1Encodable m_info; - public RecipientInfo( - KeyTransRecipientInfo info) + public RecipientInfo(KeyTransRecipientInfo info) { - this.info = info; + m_info = info ?? throw new ArgumentNullException(nameof(info)); } - public RecipientInfo( - KeyAgreeRecipientInfo info) + public RecipientInfo(KeyAgreeRecipientInfo info) { - this.info = new DerTaggedObject(false, 1, info); + m_info = new DerTaggedObject(false, 1, info); } - public RecipientInfo( - KekRecipientInfo info) + public RecipientInfo(KekRecipientInfo info) { - this.info = new DerTaggedObject(false, 2, info); + m_info = new DerTaggedObject(false, 2, info); } - public RecipientInfo( - PasswordRecipientInfo info) + public RecipientInfo(PasswordRecipientInfo info) { - this.info = new DerTaggedObject(false, 3, info); + m_info = new DerTaggedObject(false, 3, info); } - public RecipientInfo( - OtherRecipientInfo info) + public RecipientInfo(OtherRecipientInfo info) { - this.info = new DerTaggedObject(false, 4, info); + m_info = new DerTaggedObject(false, 4, info); } - public RecipientInfo( - Asn1Object info) + public RecipientInfo(Asn1Object info) { - this.info = info; + m_info = info ?? throw new ArgumentNullException(nameof(info)); } + [Obsolete("Will be removed")] public DerInteger Version { get { - if (info is Asn1TaggedObject o) - { - switch (o.TagNo) - { - case 1: - return KeyAgreeRecipientInfo.GetInstance(o, false).Version; - case 2: - return GetKekInfo(o).Version; - case 3: - return PasswordRecipientInfo.GetInstance(o, false).Version; - case 4: - return DerInteger.Zero; // no syntax version for OtherRecipientInfo - default: - throw new InvalidOperationException("unknown tag"); - } - } - - return KeyTransRecipientInfo.GetInstance(info).Version; + if (!(m_info is Asn1TaggedObject tagged)) + return KeyTransRecipientInfo.GetInstance(m_info).Version; + + if (tagged.HasContextTag()) + { + switch (tagged.TagNo) + { + case 1: + return KeyAgreeRecipientInfo.GetInstance(tagged, false).Version; + case 2: + return GetKekInfo(tagged).Version; + case 3: + return PasswordRecipientInfo.GetInstance(tagged, false).Version; + case 4: + return DerInteger.Zero; // no syntax version for OtherRecipientInfo + } + } + throw new InvalidOperationException("unknown tag"); } } - public bool IsTagged - { - get { return info is Asn1TaggedObject; } - } + public bool IsTagged => m_info is Asn1TaggedObject; public Asn1Encodable Info { get { - if (info is Asn1TaggedObject o) - { - switch (o.TagNo) + if (!(m_info is Asn1TaggedObject tagged)) + return KeyTransRecipientInfo.GetInstance(m_info); + + if (tagged.HasContextTag()) + { + switch (tagged.TagNo) { case 1: - return KeyAgreeRecipientInfo.GetInstance(o, false); + return KeyAgreeRecipientInfo.GetInstance(tagged, false); case 2: - return GetKekInfo(o); + return GetKekInfo(tagged); case 3: - return PasswordRecipientInfo.GetInstance(o, false); + return PasswordRecipientInfo.GetInstance(tagged, false); case 4: - return OtherRecipientInfo.GetInstance(o, false); - default: - throw new InvalidOperationException("unknown tag"); + return OtherRecipientInfo.GetInstance(tagged, false); } - } - - return KeyTransRecipientInfo.GetInstance(info); + } + throw new InvalidOperationException("unknown tag"); } } - private KekRecipientInfo GetKekInfo( - Asn1TaggedObject o) - { - // For compatibility with erroneous version, we don't always pass 'false' here - return KekRecipientInfo.GetInstance(o, o.IsExplicit()); - } + private KekRecipientInfo GetKekInfo(Asn1TaggedObject tagged) + { + // For compatibility with erroneous version, we don't always pass 'false' here + bool declaredExplicit = tagged.IsExplicit(); + + return KekRecipientInfo.GetInstance(tagged, declaredExplicit); + } - /** + /** * Produce an object suitable for an Asn1OutputStream. * <pre> * RecipientInfo ::= CHOICE { @@ -140,9 +133,30 @@ namespace Org.BouncyCastle.Asn1.Cms * ori [4] OtherRecipientInfo } * </pre> */ - public override Asn1Object ToAsn1Object() + public override Asn1Object ToAsn1Object() => m_info.ToAsn1Object(); + + internal bool IsKeyTransV0() + { + if (m_info is Asn1TaggedObject) + return false; + + var ktri = KeyTransRecipientInfo.GetInstance(m_info); + + return ktri.Version.HasValue(0); + } + + internal bool IsPasswordOrOther() { - return info.ToAsn1Object(); + if (m_info is Asn1TaggedObject tagged && tagged.HasContextTag()) + { + switch (tagged.TagNo) + { + case 3: + case 4: + return true; + } + } + return false; } } } |