示例#1
0
        public void WriteTest()
        {
            var node = new Asn1Ia5String("StartCom Class 2 Primary Intermediate Object Signing Certificates");
            var data = node.GetBytes();

            AreEqual(_etalon, data);
        }
        public void WriteIa5String(string correctResultHex, string valueToTest)
        {
            var encoded = Helpers.GetExampleBytes(correctResultHex);

            using (var ms = new MemoryStream())
            {
                var asn1Obj = new Asn1Ia5String(valueToTest);
                new DerWriter(ms).Write(asn1Obj);

                var res = Enumerable.SequenceEqual(encoded, ms.ToArray());
                Assert.True(res);
            }
        }
示例#3
0
        private static T MakeInstanceOfAsn1Object <T>(long nodeStartOffset, Asn1Class asn1Class, bool constructed, int tag, SubStream content)
            where T : Asn1ObjectBase
        {
            T foundObject;
            Asn1ObjectBase shouldBeType = null;

            if (asn1Class == Asn1Class.ContextSpecific)
            {
                shouldBeType = new Asn1ContextSpecific(constructed, tag, content);
            }
            else
            {
                switch ((Asn1Type)tag)
                {
                case Asn1Type.Eoc:
                    shouldBeType = new Asn1Eoc();
                    break;

                case Asn1Type.Boolean:
                    shouldBeType = new Asn1Boolean(content, constructed);
                    break;

                case Asn1Type.Integer:
                    shouldBeType = new Asn1Integer(content, constructed);
                    break;

                case Asn1Type.BitString:
                    shouldBeType = new Asn1BitString(content, constructed);
                    break;

                case Asn1Type.OctetString:
                    shouldBeType = new Asn1OctetString(content, constructed);
                    break;

                case Asn1Type.Null:
                    shouldBeType = new Asn1Null(constructed);
                    break;

                case Asn1Type.ObjectIdentifier:
                    shouldBeType = new Asn1ObjectIdentifier(content, constructed);
                    break;

                case Asn1Type.Real:
                    shouldBeType = new Asn1Real(content, constructed);
                    break;

                case Asn1Type.Enumerated:
                    shouldBeType = new Asn1Enumerated(content, constructed);
                    break;

                case Asn1Type.Utf8String:
                    shouldBeType = new Asn1Utf8String(content, constructed);
                    break;

                case Asn1Type.RelativeOid:
                    shouldBeType = new Asn1RelativeOid(content, constructed);
                    break;

                case Asn1Type.Sequence:
                    shouldBeType = new Asn1Sequence(asn1Class, content, constructed);
                    break;

                case Asn1Type.Set:
                    shouldBeType = new Asn1Set(asn1Class, content, constructed);
                    break;

                case Asn1Type.NumericString:
                    shouldBeType = new Asn1NumericString(content, constructed);
                    break;

                case Asn1Type.PrintableString:
                    shouldBeType = new Asn1PrintableString(content, constructed);
                    break;

                case Asn1Type.T61String:
                    shouldBeType = new Asn1T61String(content, constructed);
                    break;

                case Asn1Type.Ia5String:
                    shouldBeType = new Asn1Ia5String(content, constructed);
                    break;

                case Asn1Type.UtcTime:
                    shouldBeType = new Asn1UtcTime(content, constructed);
                    break;

                case Asn1Type.GeneralizedTime:
                    shouldBeType = new Asn1GeneralizedTime(content, constructed);
                    break;

                case Asn1Type.GraphicString:
                    shouldBeType = new Asn1GraphicString(content, constructed);
                    break;

                case Asn1Type.GeneralString:
                    shouldBeType = new Asn1GeneralString(content, constructed);
                    break;

                case Asn1Type.UniversalString:
                    shouldBeType = new Asn1UniversalString(content, constructed);
                    break;

                case Asn1Type.BmpString:
                    shouldBeType = new Asn1BmpString(content, constructed);
                    break;

                case Asn1Type.ObjectDescriptor:
                case Asn1Type.External:
                case Asn1Type.EmbeddedPdv:
                case Asn1Type.VideotexString:
                case Asn1Type.VisibleString:
                case Asn1Type.CharacterString:
                case Asn1Type.LongForm:
                default:
                    throw new NotSupportedException($"ASN.1 tag {tag} is unsupported.");
                }
            }

            foundObject = shouldBeType as T;

            // sanity check
            if (foundObject == null)
            {
                throw new FormatException(FormattableString.Invariant($"ASN.1 node at offset {nodeStartOffset} is of type {shouldBeType.GetType()} but expected type was {typeof(T)}"));
            }

            return(foundObject);
        }