public Asn1BerDecodeContext(Asn1BerDecodeBuffer decodeBuffer, int elemLength) { _decodeBuffer = decodeBuffer; _decBufByteCount = decodeBuffer.ByteCount; _elemLength = elemLength; _tagHolder = new Asn1Tag(); }
public virtual void EncodeBmpString(string data, bool explicitTagging, Asn1Tag tag) { if (explicitTagging) { EncodeTag(tag); } if (data == null) { EncodeLength(0); } else { EncodeLength(data.Length * 2); var length = data.Length; for (var i = 0; i < length; i++) { var num3 = data[i]; var num2 = num3 / 0x100; var num = num3 % 0x100; OutputStream.WriteByte((byte)num2); OutputStream.WriteByte((byte)num); } } }
public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength) { var tag = new Asn1Tag(); buffer.Mark(); var num = buffer.DecodeTagAndLength(tag); if (tag.Equals(0, 0, NullTypeCode)) { buffer.Reset(); var element = new NullParams(); SetElement(Null, element); Element.Decode(buffer, true, num); } else { if (!tag.Equals(0, 0x20, SequenceTypeCode)) { throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidChoiceOptionTagException, tag, buffer.ByteCount); } buffer.Reset(); var parameters = new GostR34102001PublicKeyParameters(); SetElement(Params, parameters); Element.Decode(buffer, true, num); } }
public virtual void EncodeBitString(byte[] data, int numbits, bool explicitTagging, Asn1Tag tag) { if (explicitTagging) { EncodeTag(tag); } var count = (numbits + 7) / 8; EncodeLength(count + 1); var num2 = numbits % 8; if (num2 != 0) { num2 = 8 - num2; data[count - 1] = (byte)(data[count - 1] & ((byte)~((1 << num2) - 1))); } OutputStream.WriteByte((byte)num2); if (count > 0) { OutputStream.Write(data, 0, count); } }
public static int CalcIndefLen(byte[] data, int offset, int len) { Asn1BerDecodeBuffer buffer; if ((offset == 0) && (len == data.Length)) { buffer = new Asn1BerDecodeBuffer(data); } else { var destinationArray = new byte[len]; Array.Copy(data, offset, destinationArray, 0, len); buffer = new Asn1BerDecodeBuffer(destinationArray); } var tag = new Asn1Tag(); var num = buffer.DecodeTagAndLength(tag); if (num == Asn1Status.IndefiniteLength) { var num2 = 1; num = 0; while (num2 > 0) { var byteCount = buffer.ByteCount; var num4 = buffer.DecodeTagAndLength(tag); num += buffer.ByteCount - byteCount; if (num4 > 0) { buffer.Skip(num4); num += num4; } else { if (num4 == Asn1Status.IndefiniteLength) { num2++; continue; } if (tag.IsEoc() && (num4 == 0)) { num2--; } } } } return num; }
public override void EncodeBitString(byte[] value, int numbits, bool explicitTagging, Asn1Tag tag) { if ((((numbits + 7) / 8) + 1) <= 0x3e8) { base.EncodeBitString(value, numbits, explicitTagging, tag); } else { if (explicitTagging) { EncodeTagAndIndefLen(Asn1BitString.Tag.Class, 0x20, Asn1BitString.Tag.IdCode); } else { OutputStream.WriteByte(0x80); } var num = (numbits + 7) / 8; var num2 = numbits % 8; if (num2 != 0) { num2 = 8 - num2; value[num - 1] = (byte)(value[num - 1] & ((byte)~((1 << num2) - 1))); } for (var i = 0; i < num; i += 0x3e8) { var len = num - i; if (len > 0x3e8) { len = 0x3e8; EncodeTagAndLength(Asn1BitString.Tag, len); } else { EncodeTagAndLength(Asn1BitString.Tag, len + 1); OutputStream.WriteByte((byte)num2); } if (len > 0) { OutputStream.Write(value, i, len); } } EncodeEoc(); } }
public override void EncodeBmpString(string value, bool explicitTagging, Asn1Tag tag) { if ((value == null) || (value.Length <= 500)) { base.EncodeBmpString(value, explicitTagging, tag); } else { if (explicitTagging) { EncodeTagAndIndefLen(Asn1BmpString.Tag.Class, 0x20, Asn1BmpString.Tag.IdCode); } else { OutputStream.WriteByte(0x80); } for (var i = 0; i < value.Length; i += 500) { var num2 = value.Length - i; if (num2 > 500) { num2 = 500; } EncodeTagAndLength(Asn1OctetString.Tag, num2 * 2); for (var j = 0; j < num2; j++) { var num5 = value[j + i]; var num4 = num5 / 0x100; var num3 = num5 % 0x100; OutputStream.WriteByte((byte)num4); OutputStream.WriteByte((byte)num3); } } EncodeEoc(); } }
public virtual int DecodeTagAndLength(Asn1Tag tag) { DecodeTag(tag); return(DecodeLength()); }
public virtual bool MatchTag(Asn1Tag tag) { return(MatchTag(tag.Class, tag.Form, tag.IdCode, null, null)); }
public override void EncodeUnivString(int[] value, bool explicitTagging, Asn1Tag tag) { if ((value == null) || (value.Length <= 250)) { base.EncodeUnivString(value, explicitTagging, tag); } else { if (explicitTagging) { EncodeTagAndIndefLen(Asn1UniversalString.Tag.Class, 0x20, Asn1UniversalString.Tag.IdCode); } else { OutputStream.WriteByte(0x80); } for (var i = 0; i < value.Length; i += 250) { var num2 = value.Length - i; if (num2 > 250) { num2 = 250; } EncodeTagAndLength(Asn1OctetString.Tag, num2 * 4); for (int j = 0; j < num2; j++) { var number = value[j + i]; OutputStream.WriteByte((byte)(Asn1Util.UrShift(number, 0x18) & 0xff)); OutputStream.WriteByte((byte)(Asn1Util.UrShift(number, 0x10) & 0xff)); OutputStream.WriteByte((byte)(Asn1Util.UrShift(number, 8) & 0xff)); OutputStream.WriteByte((byte)(number & 0xff)); } } EncodeEoc(); } }
public override void EncodeOctetString(byte[] value, bool explicitTagging, Asn1Tag tag) { if ((value == null) || (value.Length <= 0x3e8)) { base.EncodeOctetString(value, explicitTagging, tag); } else { if (explicitTagging) { EncodeTagAndIndefLen(Asn1OctetString.Tag.Class, 0x20, Asn1OctetString.Tag.IdCode); } else { OutputStream.WriteByte(0x80); } for (var i = 0; i < value.Length; i += 0x3e8) { var len = value.Length - i; if (len > 0x3e8) { len = 0x3e8; } EncodeTagAndLength(Asn1OctetString.Tag, len); Write(value, i, len); } EncodeEoc(); } }
public virtual int EncodeTagAndLength(Asn1Tag tag, int len) { return(EncodeLength(len) + EncodeTag(tag)); }
private void ParseElement(IAsn1TaggedEventHandler handler, Asn1Tag tag, IntHolder len) { _parserCaptureBuffer.Seek(0L, SeekOrigin.Begin); _parserCaptureBuffer.SetLength(0L); len.Value = DecodeTagAndLength(tag); if (!tag.IsEoc() || (len.Value != 0)) { handler.StartElement(tag, len.Value, _parserCaptureBuffer.ToArray()); _parserCaptureBuffer.Seek(0L, SeekOrigin.Begin); _parserCaptureBuffer.SetLength(0L); if ((len.Value > 0) || (len.Value == Asn1Status.IndefiniteLength)) { if (tag.Constructed) { ParseCons(handler, len.Value); } else { ParsePrim(handler, len.Value); } } handler.EndElement(tag); } }
private void DecodeOpenTypeElement(Asn1Tag tag, IntHolder len, bool saveData) { var nbytes = DecodeTagAndLength(tag); var byteCount = base.ByteCount; if (nbytes > 0) { if (saveData) { Capture(nbytes); } else { Skip(nbytes); } } else if (nbytes == Asn1Status.IndefiniteLength) { MovePastEoc(saveData); } len.Value = base.ByteCount - byteCount; }
public virtual void EncodeTagAndLength(Asn1Tag tag, int len) { EncodeTag(tag); EncodeLength(len); }
public virtual void EncodeTagAndIndefLen(Asn1Tag tag) { EncodeTag(tag); OutputStream.WriteByte(0x80); }
protected virtual void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength, Asn1Tag tag) { int num2; var elemLength = explicitTagging ? MatchTag(buffer, tag) : implicitLength; var num3 = elemLength; var num4 = 0; if (StringBuffer == null) { StringBuffer = new StringBuilder(); } var lastTag = buffer.LastTag; if ((lastTag == null) || !lastTag.Constructed) { if (num3 < 0) { throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidLengthException); } StringBuffer.Length = num3; while (num3 > 0) { num2 = buffer.Read(); if (num2 == -1) { throw ExceptionUtility.CryptographicException(Resources.Asn1EndOfBufferException, buffer.ByteCount); } StringBuffer[num4++] = (char)num2; num3--; } } else { var capacity = 0; var context = new Asn1BerDecodeContext(buffer, elemLength); while (!context.Expired()) { var num5 = MatchTag(buffer, Asn1OctetString.Tag); if (num5 <= 0) { throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidFormatOfConstructedValue, buffer.ByteCount); } capacity += num5; StringBuffer.EnsureCapacity(capacity); while (num5 > 0) { num2 = buffer.Read(); if (num2 == -1) { throw ExceptionUtility.CryptographicException(Resources.Asn1EndOfBufferException, buffer.ByteCount); } StringBuffer.Append((char)num2); num5--; } } if (elemLength == Asn1Status.IndefiniteLength) { MatchTag(buffer, Asn1Tag.Eoc); } } Value = StringBuffer.ToString(); buffer.TypeCode = (short)tag.IdCode; }
protected virtual int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging, Asn1Tag tag) { var length = Value.Length; buffer.Copy(Value); if (explicitTagging) { length += buffer.EncodeTagAndLength(tag, length); } return(length); }
public virtual void PeekTag(Asn1Tag parsedTag) { Mark(); DecodeTag(parsedTag); Reset(); }
public virtual bool MatchTag(Asn1Tag tag, Asn1Tag parsedTag, IntHolder parsedLen) { return(MatchTag(tag.Class, tag.Form, tag.IdCode, parsedTag, parsedLen)); }
public Asn1BerDecodeBuffer(byte[] msgdata) : base(msgdata) { _tagHolder = new Asn1Tag(); _lenHolder = new IntHolder(); }
protected void MovePastEoc(bool saveData) { var tag = new Asn1Tag(); var num = 1; while (num > 0) { var nbytes = DecodeTagAndLength(tag); if (nbytes > 0) { if (saveData) { Capture(nbytes); } else { Skip(nbytes); } } else if (nbytes == Asn1Status.IndefiniteLength) { num++; } else if (tag.IsEoc() && (nbytes == 0)) { num--; } } }
private void ParseCons(IAsn1TaggedEventHandler handler, int len) { var tag2 = new Asn1Tag(); var holder = new IntHolder(); var byteCount = base.ByteCount; while (true) { ParseElement(handler, tag2, holder); if (len == Asn1Status.IndefiniteLength) { if (tag2.IsEoc() && (holder.Value == 0)) { return; } continue; } if ((base.ByteCount - byteCount) >= len) { return; } } }
public virtual int DecodeTagAndLength(Asn1Tag tag) { DecodeTag(tag); return DecodeLength(); }
public virtual int EncodeTagAndLength(short tagClass, short tagForm, int tagIdCode, int len) { var tag = new Asn1Tag(tagClass, tagForm, tagIdCode); return(EncodeTagAndLength(tag, len)); }
public virtual void EncodeOctetString(byte[] data, bool explicitTagging, Asn1Tag tag) { if (explicitTagging) { EncodeTag(tag); } if (data == null) { EncodeLength(0); } else { EncodeLength(data.Length); OutputStream.Write(data, 0, data.Length); } }
public override void EncodeCharString(string value, bool explicitTagging, Asn1Tag tag) { if ((value == null) || (value.Length <= 0x3e8)) { base.EncodeCharString(value, explicitTagging, tag); } else { var data = Asn1Util.ToByteArray(value); if (explicitTagging) { EncodeTag(tag.Class, 0x20, tag.IdCode); } EncodeOctetString(data, false, tag); } }
public virtual void EncodeStringTag(int nbytes, Asn1Tag tag) { if (nbytes <= 0x3e8) { EncodeTag(tag); } else { EncodeTag(tag.Class, 0x20, tag.IdCode); } }
public virtual void EncodeUnivString(int[] data, bool explicitTagging, Asn1Tag tag) { if (explicitTagging) { EncodeTag(tag); } if (data == null) { EncodeLength(0); } else { EncodeLength(data.Length * 4); var length = data.Length; for (var i = 0; i < length; ++i) { var number = data[i]; OutputStream.WriteByte((byte)(Asn1Util.UrShift(number, 0x18) & 0xff)); OutputStream.WriteByte((byte)(Asn1Util.UrShift(number, 0x10) & 0xff)); OutputStream.WriteByte((byte)(Asn1Util.UrShift(number, 8) & 0xff)); OutputStream.WriteByte((byte)(number & 0xff)); } } }
public virtual bool MatchTag(Asn1Tag tag) { return MatchTag(tag.Class, tag.Form, tag.IdCode, null, null); }
public Asn1BerDecodeBuffer(Stream inputStream) : base(inputStream) { _tagHolder = new Asn1Tag(); _lenHolder = new IntHolder(); }
protected virtual int MatchTag(Asn1BerDecodeBuffer buffer, Asn1Tag tag) { return MatchTag(buffer, tag.Class, tag.Form, tag.IdCode); }
public virtual bool MatchTag(Asn1Tag tag, Asn1Tag parsedTag, IntHolder parsedLen) { return MatchTag(tag.Class, tag.Form, tag.IdCode, parsedTag, parsedLen); }
public virtual void EncodeTag(Asn1Tag tag) { var num = (byte)(((byte)tag.Class) | ((byte)tag.Form)); if (tag.IdCode < 0x1f) { OutputStream.WriteByte((byte)(num | tag.IdCode)); } else { OutputStream.WriteByte((byte)(num | 0x1f)); EncodeIdentifier(tag.IdCode); } }
public virtual bool MatchTag(short tagClass, short tagForm, int tagIdCode, Asn1Tag parsedTag, IntHolder parsedLen) { Mark(); var tag = parsedTag ?? _tagHolder; var holder = parsedLen ?? _lenHolder; holder.Value = DecodeTagAndLength(tag); if (!tag.Equals(tagClass, tagForm, tagIdCode)) { Reset(); return false; } return true; }
public virtual Asn1Tag PeekTag() { var parsedTag = new Asn1Tag(); PeekTag(parsedTag); return parsedTag; }
public virtual void EncodeCharString(string data, bool explicitTagging, Asn1Tag tag) { if (explicitTagging) { EncodeTag(tag); } if (data == null) { EncodeLength(0); } else { EncodeLength(data.Length); var buffer = Asn1Util.ToByteArray(data); OutputStream.Write(buffer, 0, buffer.Length); } }