diff options
Diffstat (limited to 'crypto/src/asn1/Asn1Utilities.cs')
-rw-r--r-- | crypto/src/asn1/Asn1Utilities.cs | 302 |
1 files changed, 298 insertions, 4 deletions
diff --git a/crypto/src/asn1/Asn1Utilities.cs b/crypto/src/asn1/Asn1Utilities.cs index 5605d4c54..ae395e4ef 100644 --- a/crypto/src/asn1/Asn1Utilities.cs +++ b/crypto/src/asn1/Asn1Utilities.cs @@ -1,10 +1,35 @@ using System; using System.IO; +using Org.BouncyCastle.Utilities; + namespace Org.BouncyCastle.Asn1 { + // TODO[api] Make static 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)) @@ -29,6 +54,48 @@ namespace Org.BouncyCastle.Asn1 } + internal static TChoice GetInstanceFromChoice<TChoice>(Asn1TaggedObject taggedObject, bool declaredExplicit, + Func<object, TChoice> 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); @@ -49,13 +116,13 @@ namespace Org.BouncyCastle.Asn1 switch (tagClass) { case Asn1Tags.Application: - return "[APPLICATION " + tagNo + "]"; + return string.Format("[APPLICATION {0}]", tagNo); case Asn1Tags.ContextSpecific: - return "[CONTEXT " + tagNo + "]"; + return string.Format("[CONTEXT {0}]", tagNo); case Asn1Tags.Private: - return "[PRIVATE " + tagNo + "]"; + return string.Format("[PRIVATE {0}]", tagNo); default: - return "[UNIVERSAL " + tagNo + "]"; + return string.Format("[UNIVERSAL {0}]", tagNo); } } @@ -74,6 +141,7 @@ namespace Org.BouncyCastle.Asn1 return GetExplicitBaseObject(taggedObject, Asn1Tags.ContextSpecific, tagNo); } + [Obsolete("Will be removed")] public static Asn1Encodable TryGetExplicitBaseObject(Asn1TaggedObject taggedObject, int tagClass, int tagNo) { if (!taggedObject.HasTag(tagClass, tagNo)) @@ -82,26 +150,69 @@ namespace Org.BouncyCastle.Asn1 return taggedObject.GetExplicitBaseObject(); } + public static bool TryGetExplicitBaseObject(Asn1TaggedObject taggedObject, int tagClass, int tagNo, + out Asn1Encodable baseObject) + { + bool result = taggedObject.HasTag(tagClass, tagNo); + baseObject = result ? taggedObject.GetExplicitBaseObject() : null; + return result; + } + + [Obsolete("Will be removed")] public static Asn1Encodable TryGetExplicitContextBaseObject(Asn1TaggedObject taggedObject, int tagNo) { return TryGetExplicitBaseObject(taggedObject, Asn1Tags.ContextSpecific, tagNo); } + public static bool TryGetExplicitContextBaseObject(Asn1TaggedObject taggedObject, int tagNo, + out Asn1Encodable baseObject) + { + return TryGetExplicitBaseObject(taggedObject, Asn1Tags.ContextSpecific, tagNo, out baseObject); + } + /* * 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); } + [Obsolete("Will be removed")] + public static Asn1TaggedObject TryGetExplicitBaseTagged(Asn1TaggedObject taggedObject, int tagClass) + { + if (!taggedObject.HasTagClass(tagClass)) + return null; + + return taggedObject.GetExplicitBaseTagged(); + } + + public static bool TryGetExplicitBaseTagged(Asn1TaggedObject taggedObject, int tagClass, + out Asn1TaggedObject baseTagged) + { + bool result = taggedObject.HasTagClass(tagClass); + baseTagged = result ? taggedObject.GetExplicitBaseTagged() : null; + return result; + } + + [Obsolete("Will be removed")] public static Asn1TaggedObject TryGetExplicitBaseTagged(Asn1TaggedObject taggedObject, int tagClass, int tagNo) { if (!taggedObject.HasTag(tagClass, tagNo)) @@ -110,11 +221,38 @@ namespace Org.BouncyCastle.Asn1 return taggedObject.GetExplicitBaseTagged(); } + public static bool TryGetExplicitBaseTagged(Asn1TaggedObject taggedObject, int tagClass, int tagNo, + out Asn1TaggedObject baseTagged) + { + bool result = taggedObject.HasTag(tagClass, tagNo); + baseTagged = result ? taggedObject.GetExplicitBaseTagged() : null; + return result; + } + + [Obsolete("Will be removed")] + public static Asn1TaggedObject TryGetExplicitContextBaseTagged(Asn1TaggedObject taggedObject) + { + return TryGetExplicitBaseTagged(taggedObject, Asn1Tags.ContextSpecific); + } + + public static bool TryGetExplicitContextBaseTagged(Asn1TaggedObject taggedObject, + out Asn1TaggedObject baseTagged) + { + return TryGetExplicitBaseTagged(taggedObject, Asn1Tags.ContextSpecific, out baseTagged); + } + + [Obsolete("Will be removed")] public static Asn1TaggedObject TryGetExplicitContextBaseTagged(Asn1TaggedObject taggedObject, int tagNo) { return TryGetExplicitBaseTagged(taggedObject, Asn1Tags.ContextSpecific, tagNo); } + public static bool TryGetExplicitContextBaseTagged(Asn1TaggedObject taggedObject, int tagNo, + out Asn1TaggedObject baseTagged) + { + return TryGetExplicitBaseTagged(taggedObject, Asn1Tags.ContextSpecific, tagNo, out baseTagged); + } + /* * Wrappers for Asn1TaggedObject.GetImplicitBaseTagged @@ -132,6 +270,7 @@ namespace Org.BouncyCastle.Asn1 return GetImplicitBaseTagged(taggedObject, Asn1Tags.ContextSpecific, tagNo, baseTagClass, baseTagNo); } + [Obsolete("Will be removed")] public static Asn1TaggedObject TryGetImplicitBaseTagged(Asn1TaggedObject taggedObject, int tagClass, int tagNo, int baseTagClass, int baseTagNo) { @@ -141,12 +280,28 @@ namespace Org.BouncyCastle.Asn1 return taggedObject.GetImplicitBaseTagged(baseTagClass, baseTagNo); } + public static bool TryGetImplicitBaseTagged(Asn1TaggedObject taggedObject, int tagClass, int tagNo, + int baseTagClass, int baseTagNo, out Asn1TaggedObject baseTagged) + { + bool result = taggedObject.HasTag(tagClass, tagNo); + baseTagged = result ? taggedObject.GetImplicitBaseTagged(baseTagClass, baseTagNo) : null; + return result; + } + + [Obsolete("Will be removed")] public static Asn1TaggedObject TryGetImplicitContextBaseTagged(Asn1TaggedObject taggedObject, int tagNo, int baseTagClass, int baseTagNo) { return TryGetImplicitBaseTagged(taggedObject, Asn1Tags.ContextSpecific, tagNo, baseTagClass, baseTagNo); } + public static bool TryGetImplicitContextBaseTagged(Asn1TaggedObject taggedObject, int tagNo, int baseTagClass, + int baseTagNo, out Asn1TaggedObject baseTagged) + { + return TryGetImplicitBaseTagged(taggedObject, Asn1Tags.ContextSpecific, tagNo, baseTagClass, baseTagNo, + out baseTagged); + } + /* * Wrappers for Asn1TaggedObject.GetBaseUniversal @@ -164,6 +319,7 @@ namespace Org.BouncyCastle.Asn1 return GetBaseUniversal(taggedObject, Asn1Tags.ContextSpecific, tagNo, declaredExplicit, baseTagNo); } + [Obsolete("Will be removed")] public static Asn1Object TryGetBaseUniversal(Asn1TaggedObject taggedObject, int tagClass, int tagNo, bool declaredExplicit, int baseTagNo) { @@ -173,12 +329,28 @@ namespace Org.BouncyCastle.Asn1 return taggedObject.GetBaseUniversal(declaredExplicit, baseTagNo); } + public static bool TryGetBaseUniversal(Asn1TaggedObject taggedObject, int tagClass, int tagNo, + bool declaredExplicit, int baseTagNo, out Asn1Object baseUniversal) + { + bool result = taggedObject.HasTag(tagClass, tagNo); + baseUniversal = result ? taggedObject.GetBaseUniversal(declaredExplicit, baseTagNo) : null; + return result; + } + + [Obsolete("Will be removed")] public static Asn1Object TryGetContextBaseUniversal(Asn1TaggedObject taggedObject, int tagNo, bool declaredExplicit, int baseTagNo) { return TryGetBaseUniversal(taggedObject, Asn1Tags.ContextSpecific, tagNo, declaredExplicit, baseTagNo); } + public static bool TryGetContextBaseUniversal(Asn1TaggedObject taggedObject, int tagNo, bool declaredExplicit, + int baseTagNo, out Asn1Object baseUniversal) + { + return TryGetBaseUniversal(taggedObject, Asn1Tags.ContextSpecific, tagNo, declaredExplicit, baseTagNo, + out baseUniversal); + } + /* * Wrappers for Asn1TaggedObjectParser.ParseExplicitBaseTagged @@ -186,12 +358,25 @@ namespace Org.BouncyCastle.Asn1 /// <exception cref="IOException"/> public static Asn1TaggedObjectParser ParseExplicitBaseTagged(Asn1TaggedObjectParser taggedObjectParser, + int tagClass) + { + return CheckTagClass(taggedObjectParser, tagClass).ParseExplicitBaseTagged(); + } + + /// <exception cref="IOException"/> + public static Asn1TaggedObjectParser ParseExplicitBaseTagged(Asn1TaggedObjectParser taggedObjectParser, int tagClass, int tagNo) { return CheckTag(taggedObjectParser, tagClass, tagNo).ParseExplicitBaseTagged(); } /// <exception cref="IOException"/> + public static Asn1TaggedObjectParser ParseExplicitContextBaseTagged(Asn1TaggedObjectParser taggedObjectParser) + { + return ParseExplicitBaseTagged(taggedObjectParser, Asn1Tags.ContextSpecific); + } + + /// <exception cref="IOException"/> public static Asn1TaggedObjectParser ParseExplicitContextBaseTagged(Asn1TaggedObjectParser taggedObjectParser, int tagNo) { @@ -199,6 +384,27 @@ namespace Org.BouncyCastle.Asn1 } /// <exception cref="IOException"/> + [Obsolete("Will be removed")] + public static Asn1TaggedObjectParser TryParseExplicitBaseTagged(Asn1TaggedObjectParser taggedObjectParser, + int tagClass) + { + if (taggedObjectParser.TagClass != tagClass) + return null; + + return taggedObjectParser.ParseExplicitBaseTagged(); + } + + /// <exception cref="IOException"/> + public static bool TryParseExplicitBaseTagged(Asn1TaggedObjectParser taggedObjectParser, int tagClass, + out Asn1TaggedObjectParser baseTagged) + { + bool result = taggedObjectParser.TagClass == tagClass; + baseTagged = result ? taggedObjectParser.ParseExplicitBaseTagged() : null; + return result; + } + + /// <exception cref="IOException"/> + [Obsolete("Will be removed")] public static Asn1TaggedObjectParser TryParseExplicitBaseTagged(Asn1TaggedObjectParser taggedObjectParser, int tagClass, int tagNo) { @@ -209,12 +415,44 @@ namespace Org.BouncyCastle.Asn1 } /// <exception cref="IOException"/> + public static bool TryParseExplicitBaseTagged(Asn1TaggedObjectParser taggedObjectParser, int tagClass, + int tagNo, out Asn1TaggedObjectParser baseTagged) + { + bool result = taggedObjectParser.HasTag(tagClass, tagNo); + baseTagged = result ? taggedObjectParser.ParseExplicitBaseTagged() : null; + return result; + } + + /// <exception cref="IOException"/> + [Obsolete("Will be removed")] + public static Asn1TaggedObjectParser TryParseExplicitContextBaseTagged( + Asn1TaggedObjectParser taggedObjectParser) + { + return TryParseExplicitBaseTagged(taggedObjectParser, Asn1Tags.ContextSpecific); + } + + /// <exception cref="IOException"/> + public static bool TryParseExplicitContextBaseTagged(Asn1TaggedObjectParser taggedObjectParser, + out Asn1TaggedObjectParser baseTagged) + { + return TryParseExplicitBaseTagged(taggedObjectParser, Asn1Tags.ContextSpecific, out baseTagged); + } + + /// <exception cref="IOException"/> + [Obsolete("Will be removed")] public static Asn1TaggedObjectParser TryParseExplicitContextBaseTagged( Asn1TaggedObjectParser taggedObjectParser, int tagNo) { return TryParseExplicitBaseTagged(taggedObjectParser, Asn1Tags.ContextSpecific, tagNo); } + /// <exception cref="IOException"/> + public static bool TryParseExplicitContextBaseTagged(Asn1TaggedObjectParser taggedObjectParser, int tagNo, + out Asn1TaggedObjectParser baseTagged) + { + return TryParseExplicitBaseTagged(taggedObjectParser, Asn1Tags.ContextSpecific, tagNo, out baseTagged); + } + /* * Wrappers for Asn1TaggedObjectParser.ParseImplicitBaseTagged @@ -236,6 +474,7 @@ namespace Org.BouncyCastle.Asn1 } /// <exception cref="IOException"/> + [Obsolete("Will be removed")] public static Asn1TaggedObjectParser TryParseImplicitBaseTagged(Asn1TaggedObjectParser taggedObjectParser, int tagClass, int tagNo, int baseTagClass, int baseTagNo) { @@ -246,6 +485,16 @@ namespace Org.BouncyCastle.Asn1 } /// <exception cref="IOException"/> + public static bool TryParseImplicitBaseTagged(Asn1TaggedObjectParser taggedObjectParser, int tagClass, + int tagNo, int baseTagClass, int baseTagNo, out Asn1TaggedObjectParser baseTagged) + { + bool result = taggedObjectParser.HasTag(tagClass, tagNo); + baseTagged = result ? taggedObjectParser.ParseImplicitBaseTagged(baseTagClass, baseTagNo) : null; + return result; + } + + /// <exception cref="IOException"/> + [Obsolete("Will be removed")] public static Asn1TaggedObjectParser TryParseImplicitContextBaseTagged( Asn1TaggedObjectParser taggedObjectParser, int tagNo, int baseTagClass, int baseTagNo) { @@ -253,6 +502,14 @@ namespace Org.BouncyCastle.Asn1 baseTagNo); } + /// <exception cref="IOException"/> + public static bool TryParseImplicitContextBaseTagged(Asn1TaggedObjectParser taggedObjectParser, int tagNo, + int baseTagClass, int baseTagNo, out Asn1TaggedObjectParser baseTagged) + { + return TryParseImplicitBaseTagged(taggedObjectParser, Asn1Tags.ContextSpecific, tagNo, baseTagClass, + baseTagNo, out baseTagged); + } + /* * Wrappers for Asn1TaggedObjectParser.ParseBaseUniversal @@ -273,6 +530,7 @@ namespace Org.BouncyCastle.Asn1 } /// <exception cref="IOException"/> + [Obsolete("Will be removed")] public static IAsn1Convertible TryParseBaseUniversal(Asn1TaggedObjectParser taggedObjectParser, int tagClass, int tagNo, bool declaredExplicit, int baseTagNo) { @@ -283,6 +541,16 @@ namespace Org.BouncyCastle.Asn1 } /// <exception cref="IOException"/> + public static bool TryParseBaseUniversal(Asn1TaggedObjectParser taggedObjectParser, int tagClass, int tagNo, + bool declaredExplicit, int baseTagNo, out IAsn1Convertible baseUniversal) + { + bool result = taggedObjectParser.HasTag(tagClass, tagNo); + baseUniversal = result ? taggedObjectParser.ParseBaseUniversal(declaredExplicit, baseTagNo) : null; + return result; + } + + /// <exception cref="IOException"/> + [Obsolete("Will be removed")] public static IAsn1Convertible TryParseContextBaseUniversal(Asn1TaggedObjectParser taggedObjectParser, int tagNo, bool declaredExplicit, int baseTagNo) { @@ -290,6 +558,14 @@ namespace Org.BouncyCastle.Asn1 baseTagNo); } + /// <exception cref="IOException"/> + public static bool TryParseContextBaseUniversal(Asn1TaggedObjectParser taggedObjectParser, int tagNo, + bool declaredExplicit, int baseTagNo, out IAsn1Convertible baseUniversal) + { + return TryParseBaseUniversal(taggedObjectParser, Asn1Tags.ContextSpecific, tagNo, declaredExplicit, + baseTagNo, out baseUniversal); + } + /* * Wrappers for Asn1TaggedObjectParser.ParseExplicitBaseObject @@ -310,6 +586,7 @@ namespace Org.BouncyCastle.Asn1 } /// <exception cref="IOException"/> + [Obsolete("Will be removed")] public static IAsn1Convertible TryParseExplicitBaseObject(Asn1TaggedObjectParser taggedObjectParser, int tagClass, int tagNo) { @@ -320,10 +597,27 @@ namespace Org.BouncyCastle.Asn1 } /// <exception cref="IOException"/> + public static bool TryParseExplicitBaseObject(Asn1TaggedObjectParser taggedObjectParser, int tagClass, + int tagNo, out IAsn1Convertible baseObject) + { + bool result = taggedObjectParser.HasTag(tagClass, tagNo); + baseObject = result ? taggedObjectParser.ParseExplicitBaseObject() : null; + return result; + } + + /// <exception cref="IOException"/> + [Obsolete("Will be removed")] public static IAsn1Convertible TryParseExplicitContextBaseObject(Asn1TaggedObjectParser taggedObjectParser, int tagNo) { return TryParseExplicitBaseObject(taggedObjectParser, Asn1Tags.ContextSpecific, tagNo); } + + /// <exception cref="IOException"/> + public static bool TryParseExplicitContextBaseObject(Asn1TaggedObjectParser taggedObjectParser, int tagNo, + out IAsn1Convertible baseObject) + { + return TryParseExplicitBaseObject(taggedObjectParser, Asn1Tags.ContextSpecific, tagNo, out baseObject); + } } } |