예제 #1
0
        private static void CheckTag(DerTag expected, byte[] data, int position)
        {
            if (position >= data.Length)
            {
                throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
            }

            byte actual = data[position];

            // Context-specific datatypes cannot be tag-verified
            if ((actual & ContextSpecificTagFlag) != 0)
            {
                return;
            }

            byte relevant     = (byte)(actual & TagNumberMask);
            byte expectedByte = (byte)((byte)expected & TagNumberMask);

            if (expectedByte != relevant)
            {
                throw new CryptographicException(
                          SR.Cryptography_Der_Invalid_Encoding
#if DEBUG
                          ,
                          new InvalidOperationException(
                              "Expected tag '0x" + expectedByte.ToString("X2") +
                              "', got '0x" + actual.ToString("X2") +
                              "' at position " + position)
#endif
                          );
            }
        }
예제 #2
0
        private static void CheckTag(DerTag expected, byte[] data, int position)
        {
            if (position >= data.Length)
            {
                throw new CryptographicException("Invalid Der-Encoding");
            }

            byte actual   = data[position];
            byte relevant = (byte)(actual & TagNumberMask);

            // Multi-byte tags are not supported by this implementation.
            if (relevant == TagNumberMask)
            {
                throw new CryptographicException("Invalid Der-Encoding");
            }

            // Context-specific datatypes cannot be tag-verified
            if ((actual & ContextSpecificTagFlag) != 0)
            {
                return;
            }

            byte expectedByte = (byte)((byte)expected & TagNumberMask);

            if (expectedByte != relevant)
            {
                throw new CryptographicException("Invalid Der-Encoding");
            }
        }
예제 #3
0
        private void EatTag(DerTag expected)
        {
            if (!HasData)
            {
                throw new CryptographicException("Invalid Der-Encoding");
            }

            CheckTag(expected, _data, _position);
            _position++;
        }
예제 #4
0
        private void EatTag(DerTag expected)
        {
            if (!HasData)
            {
                throw new CryptographicException(SR.GetString(SR.Cryptography_Der_Invalid_Encoding));
            }

            CheckTag(expected, _data, _position);
            _position++;
        }
예제 #5
0
        private DerSequenceReader ReadCollectionWithTag(DerTag expected)
        {
            // DerSequenceReader wants to read its own tag, so don't EatTag here.
            CheckTag(expected, _data, _position);

            int lengthLength;
            int contentLength = ScanContentLength(_data, _position + 1, _end, out lengthLength);
            int totalLength   = 1 + lengthLength + contentLength;

            DerSequenceReader reader = new DerSequenceReader(expected, _data, _position, totalLength);

            _position += totalLength;
            return(reader);
        }
예제 #6
0
        internal DerSequenceReader(DerTag tagToEat, byte[] data, int offset, int length)
        {
            _data = data;
            _end  = offset + length;

            Debug.Assert(data != null, "Data is null");
            Debug.Assert(offset >= 0, "Offset is negative");

            if (length < 2 || length > data.Length - offset)
            {
                throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
            }

            _position = offset;
            EatTag(tagToEat);
            ContentLength = EatLength();
        }
예제 #7
0
        private DerSequenceReader(DerTag tagToEat, byte[] data, int offset, int length)
        {
            Debug.Assert(data != null, "Data is null");

            if (offset < 0 || length < 2 || length > data.Length - offset)
            {
                throw new CryptographicException("Invalid Der-Encoding");
            }

            _data     = data;
            _end      = offset + length;
            _position = offset;
            EatTag(tagToEat);
            int contentLength = EatLength();

            Debug.Assert(_end - contentLength >= _position);
            ContentLength = contentLength;

            // If the sequence reports being smaller than the buffer, shrink the end-of-validity.
            _end = _position + contentLength;
        }
예제 #8
0
        private static void CheckTag(DerTag expected, byte[] data, int position)
        {
            byte actual = data[position];

            // Context-specific datatypes cannot be tag-verified
            if ((actual & ContextSpecificTagFlag) != 0)
            {
                return;
            }

            byte relevant     = (byte)(actual & 0x1F);
            byte expectedByte = (byte)expected;

            if (expectedByte != relevant)
            {
                throw new InvalidOperationException(
                          "Expected tag '0x" + expectedByte.ToString("X2") +
                          "', got '0x" + actual.ToString("X2") +
                          "' at position " + position);
            }
        }
예제 #9
0
        private DateTime ReadTime(DerTag timeTag, string formatString)
        {
            EatTag(timeTag);
            int contentLength = EatLength();

            string decodedTime = System.Text.Encoding.ASCII.GetString(_data, _position, contentLength);

            _position += contentLength;

            Debug.Assert(
                decodedTime[decodedTime.Length - 1] == 'Z',
                $"The date doesn't follow the X.690 format, ending with {decodedTime[decodedTime.Length - 1]}");

            DateTime time;

            DateTimeFormatInfo fi = LazyInitializer.EnsureInitialized(
                ref s_validityDateTimeFormatInfo,
                () =>
            {
                var clone = (DateTimeFormatInfo)CultureInfo.InvariantCulture.DateTimeFormat.Clone();
                clone.Calendar.TwoDigitYearMax = 2049;

                return(clone);
            });

            if (!DateTime.TryParseExact(
                    decodedTime,
                    formatString,
                    fi,
                    DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal,
                    out time))
            {
                throw new CryptographicException("Invalid Der-Encoding");
            }

            return(time);
        }
예제 #10
0
 internal bool HasTag(DerTag expectedTag)
 {
     return(HasTag((byte)expectedTag));
 }
예제 #11
0
 private void EatTag(DerTag expected)
 {
     CheckTag(expected, _data, _position);
     _position++;
 }
예제 #12
0
        private static void CheckTag(DerTag expected, byte[] data, int position)
        {
            byte actual = data[position];

            // Context-specific datatypes cannot be tag-verified
            if ((actual & ContextSpecificTagFlag) != 0)
            {
                return;
            }

            byte relevant = (byte)(actual & 0x1F);
            byte expectedByte = (byte)expected;

            if (expectedByte != relevant)
            {
                throw new InvalidOperationException(
                    "Expected tag '0x" + expectedByte.ToString("X2") +
                        "', got '0x" + actual.ToString("X2") +
                        "' at position " + position);
            }
        }
예제 #13
0
 private void EatTag(DerTag expected)
 {
     CheckTag(expected, _data, _position);
     _position++;
 }
예제 #14
0
        internal byte[] ReadValue(DerTag tag)
        {
            EatTag(tag);

            return(ReadContentAsBytes());
        }