using System; using System.IO; using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Asn1 { public abstract class Asn1Utilities { internal static Asn1TaggedObject CheckTagClass(Asn1TaggedObject taggedObject, int tagClass) { if (!taggedObject.HasTagClass(tagClass)) { string expected = GetTagClassText(tagClass); string found = GetTagClassText(taggedObject); throw new InvalidOperationException("Expected " + expected + " tag but found " + found); } return taggedObject; } internal static Asn1TaggedObjectParser CheckTagClass(Asn1TaggedObjectParser taggedObjectParser, int tagClass) { if (taggedObjectParser.TagClass != tagClass) { string expected = GetTagClassText(tagClass); string found = GetTagClassText(taggedObjectParser); throw new InvalidOperationException("Expected " + expected + " tag but found " + found); } return taggedObjectParser; } internal static Asn1TaggedObject CheckTag(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; } internal static Asn1TaggedObjectParser CheckTag(Asn1TaggedObjectParser taggedObjectParser, int tagClass, int tagNo) { if (!taggedObjectParser.HasTag(tagClass, tagNo)) { string expected = GetTagText(tagClass, tagNo); string found = GetTagText(taggedObjectParser); throw new InvalidOperationException("Expected " + expected + " tag but found " + found); } return taggedObjectParser; } internal static TChoice GetInstanceFromChoice(Asn1TaggedObject taggedObject, bool declaredExplicit, Func constructor) where TChoice : Asn1Encodable, IAsn1Choice { if (!declaredExplicit) { var message = string.Format( "Implicit tagging cannot be used with untagged choice type {0} (X.680 30.6, 30.8).", Platform.GetTypeName(typeof(TChoice))); throw new ArgumentException(message, nameof(declaredExplicit)); } return constructor(taggedObject.GetExplicitBaseObject()); } public static string GetTagClassText(int tagClass) { switch (tagClass) { case Asn1Tags.Application: return "APPLICATION"; case Asn1Tags.ContextSpecific: return "CONTEXT"; case Asn1Tags.Private: return "PRIVATE"; default: return "UNIVERSAL"; } } public static string GetTagClassText(Asn1TaggedObject taggedObject) { return GetTagClassText(taggedObject.TagClass); } public static string GetTagClassText(Asn1TaggedObjectParser taggedObjectParser) { return GetTagClassText(taggedObjectParser.TagClass); } internal static string GetTagText(Asn1Tag tag) { return GetTagText(tag.TagClass, tag.TagNo); } public static string GetTagText(Asn1TaggedObject taggedObject) { return GetTagText(taggedObject.TagClass, taggedObject.TagNo); } public static string GetTagText(Asn1TaggedObjectParser taggedObjectParser) { return GetTagText(taggedObjectParser.TagClass, taggedObjectParser.TagNo); } public static string GetTagText(int tagClass, int tagNo) { switch (tagClass) { case Asn1Tags.Application: return string.Format("[APPLICATION {0}]", tagNo); case Asn1Tags.ContextSpecific: return string.Format("[CONTEXT {0}]", tagNo); case Asn1Tags.Private: return string.Format("[PRIVATE {0}]", tagNo); default: return string.Format("[UNIVERSAL {0}]", tagNo); } } /* * Wrappers for Asn1TaggedObject.GetExplicitBaseObject */ public static Asn1Encodable GetExplicitBaseObject(Asn1TaggedObject taggedObject, int tagClass, int tagNo) { return CheckTag(taggedObject, tagClass, tagNo).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) { return CheckTagClass(taggedObject, tagClass).GetExplicitBaseTagged(); } public static Asn1TaggedObject GetExplicitBaseTagged(Asn1TaggedObject taggedObject, int tagClass, int tagNo) { return CheckTag(taggedObject, tagClass, tagNo).GetExplicitBaseTagged(); } public static Asn1TaggedObject GetExplicitContextBaseTagged(Asn1TaggedObject taggedObject) { return GetExplicitBaseTagged(taggedObject, Asn1Tags.ContextSpecific); } public static Asn1TaggedObject GetExplicitContextBaseTagged(Asn1TaggedObject taggedObject, int tagNo) { return GetExplicitBaseTagged(taggedObject, Asn1Tags.ContextSpecific, tagNo); } public static Asn1TaggedObject TryGetExplicitBaseTagged(Asn1TaggedObject taggedObject, int tagClass) { if (!taggedObject.HasTagClass(tagClass)) return null; return taggedObject.GetExplicitBaseTagged(); } 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) { return TryGetExplicitBaseTagged(taggedObject, Asn1Tags.ContextSpecific); } public static Asn1TaggedObject TryGetExplicitContextBaseTagged(Asn1TaggedObject taggedObject, int tagNo) { return TryGetExplicitBaseTagged(taggedObject, Asn1Tags.ContextSpecific, tagNo); } /* * Wrappers for Asn1TaggedObject.GetImplicitBaseTagged */ public static Asn1TaggedObject GetImplicitBaseTagged(Asn1TaggedObject taggedObject, int tagClass, int tagNo, int baseTagClass, int baseTagNo) { return CheckTag(taggedObject, tagClass, tagNo).GetImplicitBaseTagged(baseTagClass, baseTagNo); } public static Asn1TaggedObject GetImplicitContextBaseTagged(Asn1TaggedObject taggedObject, int tagNo, int baseTagClass, int baseTagNo) { return GetImplicitBaseTagged(taggedObject, Asn1Tags.ContextSpecific, tagNo, baseTagClass, baseTagNo); } public static Asn1TaggedObject TryGetImplicitBaseTagged(Asn1TaggedObject taggedObject, int tagClass, int tagNo, int baseTagClass, int baseTagNo) { if (!taggedObject.HasTag(tagClass, tagNo)) return null; return taggedObject.GetImplicitBaseTagged(baseTagClass, baseTagNo); } public static Asn1TaggedObject TryGetImplicitContextBaseTagged(Asn1TaggedObject taggedObject, int tagNo, int baseTagClass, int baseTagNo) { return TryGetImplicitBaseTagged(taggedObject, Asn1Tags.ContextSpecific, tagNo, baseTagClass, baseTagNo); } /* * Wrappers for Asn1TaggedObject.GetBaseUniversal */ public static Asn1Object GetBaseUniversal(Asn1TaggedObject taggedObject, int tagClass, int tagNo, bool declaredExplicit, int baseTagNo) { return CheckTag(taggedObject, tagClass, tagNo).GetBaseUniversal(declaredExplicit, baseTagNo); } public static Asn1Object GetContextBaseUniversal(Asn1TaggedObject taggedObject, int tagNo, bool declaredExplicit, int baseTagNo) { return GetBaseUniversal(taggedObject, Asn1Tags.ContextSpecific, tagNo, declaredExplicit, baseTagNo); } public static Asn1Object TryGetBaseUniversal(Asn1TaggedObject taggedObject, int tagClass, int tagNo, bool declaredExplicit, int baseTagNo) { if (!taggedObject.HasTag(tagClass, tagNo)) return null; return taggedObject.GetBaseUniversal(declaredExplicit, baseTagNo); } public static Asn1Object TryGetContextBaseUniversal(Asn1TaggedObject taggedObject, int tagNo, bool declaredExplicit, int baseTagNo) { return TryGetBaseUniversal(taggedObject, Asn1Tags.ContextSpecific, tagNo, declaredExplicit, baseTagNo); } /* * Wrappers for Asn1TaggedObjectParser.ParseExplicitBaseTagged */ /// public static Asn1TaggedObjectParser ParseExplicitBaseTagged(Asn1TaggedObjectParser taggedObjectParser, int tagClass) { return CheckTagClass(taggedObjectParser, tagClass).ParseExplicitBaseTagged(); } /// public static Asn1TaggedObjectParser ParseExplicitBaseTagged(Asn1TaggedObjectParser taggedObjectParser, int tagClass, int tagNo) { return CheckTag(taggedObjectParser, tagClass, tagNo).ParseExplicitBaseTagged(); } /// public static Asn1TaggedObjectParser ParseExplicitContextBaseTagged(Asn1TaggedObjectParser taggedObjectParser) { return ParseExplicitBaseTagged(taggedObjectParser, Asn1Tags.ContextSpecific); } /// public static Asn1TaggedObjectParser ParseExplicitContextBaseTagged(Asn1TaggedObjectParser taggedObjectParser, int tagNo) { return ParseExplicitBaseTagged(taggedObjectParser, Asn1Tags.ContextSpecific, tagNo); } /// public static Asn1TaggedObjectParser TryParseExplicitBaseTagged(Asn1TaggedObjectParser taggedObjectParser, int tagClass) { if (taggedObjectParser.TagClass != tagClass) return null; return taggedObjectParser.ParseExplicitBaseTagged(); } /// public static Asn1TaggedObjectParser TryParseExplicitBaseTagged(Asn1TaggedObjectParser taggedObjectParser, int tagClass, int tagNo) { if (!taggedObjectParser.HasTag(tagClass, tagNo)) return null; return taggedObjectParser.ParseExplicitBaseTagged(); } /// public static Asn1TaggedObjectParser TryParseExplicitContextBaseTagged( Asn1TaggedObjectParser taggedObjectParser) { return TryParseExplicitBaseTagged(taggedObjectParser, Asn1Tags.ContextSpecific); } /// public static Asn1TaggedObjectParser TryParseExplicitContextBaseTagged( Asn1TaggedObjectParser taggedObjectParser, int tagNo) { return TryParseExplicitBaseTagged(taggedObjectParser, Asn1Tags.ContextSpecific, tagNo); } /* * Wrappers for Asn1TaggedObjectParser.ParseImplicitBaseTagged */ /// public static Asn1TaggedObjectParser ParseImplicitBaseTagged(Asn1TaggedObjectParser taggedObjectParser, int tagClass, int tagNo, int baseTagClass, int baseTagNo) { return CheckTag(taggedObjectParser, tagClass, tagNo).ParseImplicitBaseTagged(baseTagClass, baseTagNo); } /// public static Asn1TaggedObjectParser ParseImplicitContextBaseTagged(Asn1TaggedObjectParser taggedObjectParser, int tagNo, int baseTagClass, int baseTagNo) { return ParseImplicitBaseTagged(taggedObjectParser, Asn1Tags.ContextSpecific, tagNo, baseTagClass, baseTagNo); } /// public static Asn1TaggedObjectParser TryParseImplicitBaseTagged(Asn1TaggedObjectParser taggedObjectParser, int tagClass, int tagNo, int baseTagClass, int baseTagNo) { if (!taggedObjectParser.HasTag(tagClass, tagNo)) return null; return taggedObjectParser.ParseImplicitBaseTagged(baseTagClass, baseTagNo); } /// public static Asn1TaggedObjectParser TryParseImplicitContextBaseTagged( Asn1TaggedObjectParser taggedObjectParser, int tagNo, int baseTagClass, int baseTagNo) { return TryParseImplicitBaseTagged(taggedObjectParser, Asn1Tags.ContextSpecific, tagNo, baseTagClass, baseTagNo); } /* * Wrappers for Asn1TaggedObjectParser.ParseBaseUniversal */ /// public static IAsn1Convertible ParseBaseUniversal(Asn1TaggedObjectParser taggedObjectParser, int tagClass, int tagNo, bool declaredExplicit, int baseTagNo) { return CheckTag(taggedObjectParser, tagClass, tagNo).ParseBaseUniversal(declaredExplicit, baseTagNo); } /// public static IAsn1Convertible ParseContextBaseUniversal(Asn1TaggedObjectParser taggedObjectParser, int tagNo, bool declaredExplicit, int baseTagNo) { return ParseBaseUniversal(taggedObjectParser, Asn1Tags.ContextSpecific, tagNo, declaredExplicit, baseTagNo); } /// public static IAsn1Convertible TryParseBaseUniversal(Asn1TaggedObjectParser taggedObjectParser, int tagClass, int tagNo, bool declaredExplicit, int baseTagNo) { if (!taggedObjectParser.HasTag(tagClass, tagNo)) return null; return taggedObjectParser.ParseBaseUniversal(declaredExplicit, baseTagNo); } /// public static IAsn1Convertible TryParseContextBaseUniversal(Asn1TaggedObjectParser taggedObjectParser, int tagNo, bool declaredExplicit, int baseTagNo) { return TryParseBaseUniversal(taggedObjectParser, Asn1Tags.ContextSpecific, tagNo, declaredExplicit, baseTagNo); } /* * Wrappers for Asn1TaggedObjectParser.ParseExplicitBaseObject */ /// public static IAsn1Convertible ParseExplicitBaseObject(Asn1TaggedObjectParser taggedObjectParser, int tagClass, int tagNo) { return CheckTag(taggedObjectParser, tagClass, tagNo).ParseExplicitBaseObject(); } /// public static IAsn1Convertible ParseExplicitContextBaseObject(Asn1TaggedObjectParser taggedObjectParser, int tagNo) { return ParseExplicitBaseObject(taggedObjectParser, Asn1Tags.ContextSpecific, tagNo); } /// public static IAsn1Convertible TryParseExplicitBaseObject(Asn1TaggedObjectParser taggedObjectParser, int tagClass, int tagNo) { if (!taggedObjectParser.HasTag(tagClass, tagNo)) return null; return taggedObjectParser.ParseExplicitBaseObject(); } /// public static IAsn1Convertible TryParseExplicitContextBaseObject(Asn1TaggedObjectParser taggedObjectParser, int tagNo) { return TryParseExplicitBaseObject(taggedObjectParser, Asn1Tags.ContextSpecific, tagNo); } } }