From 39a846768e82cd1439bcb165665593c9baae5ffb Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Mon, 8 Nov 2021 16:44:49 +0700 Subject: More tagged object work from bc-java --- crypto/src/asn1/Asn1Utilities.cs | 69 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) (limited to 'crypto/src/asn1/Asn1Utilities.cs') diff --git a/crypto/src/asn1/Asn1Utilities.cs b/crypto/src/asn1/Asn1Utilities.cs index dca2253ec..fe6db2df7 100644 --- a/crypto/src/asn1/Asn1Utilities.cs +++ b/crypto/src/asn1/Asn1Utilities.cs @@ -23,5 +23,74 @@ namespace Org.BouncyCastle.Asn1 return "[UNIVERSAL " + tagNo + "]"; } } + + /* + * Wrappers for Asn1TaggedObject.GetExplicitBaseObject + */ + + public static Asn1Encodable GetExplicitBaseObject(Asn1TaggedObject taggedObject, int tagClass, int tagNo) + { + if (!taggedObject.HasTag(tagClass, tagNo)) + { + string expected = GetTagText(tagClass, tagNo); + string found = GetTagText(taggedObject); + throw new InvalidOperationException("Expected " + expected + " tag but found " + found); + } + + return taggedObject.GetExplicitBaseObject(); + } + + public static Asn1Encodable GetExplicitContextBaseObject(Asn1TaggedObject taggedObject, int tagNo) + { + return GetExplicitBaseObject(taggedObject, Asn1Tags.ContextSpecific, tagNo); + } + + public static Asn1Encodable TryGetExplicitBaseObject(Asn1TaggedObject taggedObject, int tagClass, int tagNo) + { + if (!taggedObject.HasTag(tagClass, tagNo)) + return null; + + return taggedObject.GetExplicitBaseObject(); + } + + public static Asn1Encodable TryGetExplicitContextBaseObject(Asn1TaggedObject taggedObject, int tagNo) + { + return TryGetExplicitBaseObject(taggedObject, Asn1Tags.ContextSpecific, tagNo); + } + + + /* + * Wrappers for Asn1TaggedObject.GetExplicitBaseTagged + */ + + public static Asn1TaggedObject GetExplicitBaseTagged(Asn1TaggedObject taggedObject, int tagClass, int tagNo) + { + if (!taggedObject.HasTag(tagClass, tagNo)) + { + string expected = GetTagText(tagClass, tagNo); + string found = GetTagText(taggedObject); + throw new InvalidOperationException("Expected " + expected + " tag but found " + found); + } + + return taggedObject.GetExplicitBaseTagged(); + } + + public static Asn1TaggedObject GetExplicitContextBaseTagged(Asn1TaggedObject taggedObject, int tagNo) + { + return GetExplicitBaseTagged(taggedObject, Asn1Tags.ContextSpecific, tagNo); + } + + public static Asn1TaggedObject TryGetExplicitBaseTagged(Asn1TaggedObject taggedObject, int tagClass, int tagNo) + { + if (!taggedObject.HasTag(tagClass, tagNo)) + return null; + + return taggedObject.GetExplicitBaseTagged(); + } + + public static Asn1TaggedObject TryGetExplicitContextBaseTagged(Asn1TaggedObject taggedObject, int tagNo) + { + return TryGetExplicitBaseTagged(taggedObject, Asn1Tags.ContextSpecific, tagNo); + } } } -- cgit 1.4.1