Exemple #1
0
            public static Asn1GeneralizedTime FromRawData(Byte[] rawData)
            {
                if (rawData[0] != (Byte)Asn1Type.Generalizedtime)
                {
                    //throw new Asn1InvalidTagException("Invalid Tag");
                }
                Asn1GeneralizedTime asn1GeneralizedTime = new Asn1GeneralizedTime();

                asn1GeneralizedTime.tagName = "Generalized Time";
                asn1GeneralizedTime.RawData = rawData;
                return(asn1GeneralizedTime);
            }
Exemple #2
0
        public void WriteGeneralizedTimeFromString(string correctResultHex, string valueToTest)
        {
            var encoded  = Helpers.GetExampleBytes(correctResultHex);
            var datetime = Helpers.ConvertFromGeneralizedTime(valueToTest);

            using (var ms = new MemoryStream())
            {
                var asn1Obj = new Asn1GeneralizedTime(datetime);
                new DerWriter(ms).Write(asn1Obj);

                var res = Enumerable.SequenceEqual(encoded, ms.ToArray());
                Assert.True(res);
            }
        }
Exemple #3
0
        public void WriteGeneralizedTime(string correctResultHex, string valueToTest)
        {
            var encoded  = Helpers.GetExampleBytes(correctResultHex);
            var datetime = DateTimeOffset.ParseExact(valueToTest, "yyyyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);

            using (var ms = new MemoryStream())
            {
                var asn1Obj = new Asn1GeneralizedTime(datetime);
                new DerWriter(ms).Write(asn1Obj);

                var res = Enumerable.SequenceEqual(encoded, ms.ToArray());
                Assert.True(res);
            }
        }
Exemple #4
0
        void decodeTstInfo(Asn1Reader asn)
        {
            asn.MoveNextAndExpectTags((Byte)Asn1Type.INTEGER);
            Version = (Int32) new Asn1Integer(asn).Value;
            asn.MoveNextAndExpectTags((Byte)Asn1Type.OBJECT_IDENTIFIER);
            PolicyID = new Asn1ObjectIdentifier(asn).Value;
            asn.MoveNextAndExpectTags(48);
            RequestMessage = new TspMessageImprint(asn.GetTagRawData());
            asn.MoveNextCurrentLevelAndExpectTags((Byte)Asn1Type.INTEGER);
            SerialNumber = new Asn1Integer(asn).Value;
            asn.MoveNextAndExpectTags((Byte)Asn1Type.GeneralizedTime);
            GenerationTimestamp = new Asn1GeneralizedTime(asn).Value;

            decodeOptionalFields(asn);
        }
Exemple #5
0
 static void Init(Asn1Reader asn1Reader, Asn1GeneralizedTime asn1GeneralizedTime)
 {
     asn1GeneralizedTime.Tag     = asn1Reader.Tag;
     asn1GeneralizedTime.TagName = asn1Reader.TagName;
     asn1GeneralizedTime.RawData = Asn1ReaderT.GetTagRawData(asn1Reader);
 }
Exemple #6
0
 static Asn1Reader Decode(Asn1Reader asn1Reader, Asn1GeneralizedTime asn1GeneralizedTime)
 {
     Init(asn1Reader, asn1GeneralizedTime);
     asn1GeneralizedTime.tagValue = DateTimeUtils.Decode(asn1Reader);
     return(asn1Reader);
 }
Exemple #7
0
        static String DecodeGeneralizedTime(Asn1Reader asn)
        {
            DateTime dt = Asn1GeneralizedTime.Decode(asn);

            return(dt.ToShortDateString() + " " + dt.ToShortTimeString());
        }
Exemple #8
0
        static String DecodeGeneralizedTime(Asn1Reader asn)
        {
            DateTime dt = new Asn1GeneralizedTime(asn).Value;

            return(dt.ToString("dd.MM.yyyy hh:mm:ss"));
        }
        static String DecodeGeneralizedTime(Asn1Reader asn)
        {
            var dt = new Asn1GeneralizedTime(asn).Value;

            return(dt.ToShortDateString() + " " + dt.ToShortTimeString());
        }
Exemple #10
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);
        }