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 ); } }
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"); } }
private void EatTag(DerTag expected) { if (!HasData) { throw new CryptographicException("Invalid Der-Encoding"); } CheckTag(expected, _data, _position); _position++; }
private void EatTag(DerTag expected) { if (!HasData) { throw new CryptographicException(SR.GetString(SR.Cryptography_Der_Invalid_Encoding)); } CheckTag(expected, _data, _position); _position++; }
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); }
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(); }
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; }
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); } }
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); }
internal bool HasTag(DerTag expectedTag) { return(HasTag((byte)expectedTag)); }
private void EatTag(DerTag expected) { CheckTag(expected, _data, _position); _position++; }
internal byte[] ReadValue(DerTag tag) { EatTag(tag); return(ReadContentAsBytes()); }