/// <summary>
        /// Decodes the object by BER.
        /// </summary>
        /// <param name="buffer">A buffer that contains a BER encoding result.</param>
        /// <param name="explicitTag">Indicates whether the tags should be encoded explicitly. In our Test Suites, it will always be true.</param>
        /// <returns>The number of the bytes consumed in the buffer to decode this object.</returns>
        /// <exception cref="Asn1ConstraintsNotSatisfied">
        /// Thrown when the constraints are not satisfied after decoding.
        /// </exception>
        /// <exception cref="Asn1DecodingUnexpectedData">
        /// Thrown when the data in the buffer can not be properly decoded.
        /// </exception>
        /// <remarks>Override this method in a user-defined class only if the procedure is not applicable in some special scenarios.</remarks>
        public override int BerDecode(IAsn1DecodingBuffer buffer, bool explicitTag = true)
        {
            int returnVal = 0;

            Asn1Tag topTag;

            returnVal += TagBerDecode(buffer, out topTag);

            Asn1Tag topTagInDefinition = this.TopTag;

            if (!topTag.Equals(topTagInDefinition))
            {
                throw new Asn1DecodingUnexpectedData(ExceptionMessages.DecodingUnexpectedData + " Top Most Tag decoding fail.");
            }

            //Decode length
            int lengthAfterUniTag;

            returnVal += LengthBerDecode(buffer, out lengthAfterUniTag);
            //Decode data
            returnVal += ValueBerDecode(buffer, lengthAfterUniTag);

            if (!VerifyConstraints())
            {
                throw new Asn1ConstraintsNotSatisfied(ExceptionMessages.ConstraintsNotSatisfied
                                                      + " Decode " + this.GetType().Name + ".");
            }

            return(returnVal);
        }
 public override int BerDecode(IAsn1DecodingBuffer buffer, bool explicitTag = true)
 {
     buffer.ReadBytes(2);
     int consumedLen = 2, len;
     consumedLen += LengthBerDecode(buffer, out len);
     return consumedLen + ValueBerDecode(buffer, len);
 }
        /// <summary>
        /// Decodes the object by PER.
        /// </summary>
        /// <param name="buffer">A buffer that contains a PER encoding result.</param>
        /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
        protected override void ValuePerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
        {
            Asn1Integer ai = new Asn1Integer(null, Constraints.Min, Constraints.Max);

            ai.PerDecode(buffer, aligned);
            Value = allowedValues[(int)ai.Value];
        }
Пример #4
0
        public override int BerDecode(IAsn1DecodingBuffer buffer, bool explicitTag = true)
        {
            int     allLen = 0;
            Asn1Tag contextTag;

            allLen += TagBerDecode(buffer, out contextTag);
            switch (contextTag.TagValue)
            {
            case 0:
                field0  = new LDAPString();
                allLen += field0.BerDecodeWithoutUnisersalTag(buffer);
                SetData(1, field0);
                break;

            case 1:
                field1  = new LDAPString();
                allLen += field1.BerDecodeWithoutUnisersalTag(buffer);
                SetData(2, field1);
                break;

            case 2:
                field2  = new LDAPString();
                allLen += field2.BerDecodeWithoutUnisersalTag(buffer);
                SetData(3, field2);
                break;

            default:
                throw new Asn1DecodingUnexpectedData(ExceptionMessages.DecodingUnexpectedData + " AuthenticationChoice");
            }
            return(allLen);
        }
        public override int BerDecode(IAsn1DecodingBuffer buffer, bool explicitTag = true)
        {
            int headLen = 0;
            Asn1Tag seqTag;
            headLen += TagBerDecode(buffer, out seqTag);
            int valueLen;
            headLen += LengthBerDecode(buffer, out valueLen);

            int valueLenDecode = 0;
            resultCode = new LDAPResult_resultCode();
            valueLenDecode += resultCode.BerDecode(buffer);
            matchedDN = new LDAPDN();
            valueLenDecode += matchedDN.BerDecode(buffer);
            errorMessage = new LDAPString();
            valueLenDecode += errorMessage.BerDecode(buffer);
            if(valueLenDecode == valueLen)
            {
                referral = null;
            }
            else
            {
                Asn1Tag contextTag;
                valueLenDecode += TagBerDecode(buffer, out contextTag);
                referral = new Referral();
                valueLenDecode += referral.BerDecodeWithoutUnisersalTag(buffer);
            }
            if(valueLen != valueLenDecode)
            {
                throw new Asn1DecodingUnexpectedData(ExceptionMessages.DecodingUnexpectedData + " LDAPResult.");
            }
            return headLen + valueLen;
        }
        /// <summary>
        /// Reads/decodes a length from the buffer.
        /// </summary>
        /// <param name="buffer">A buffer that stores a BER encoding result.</param>
        /// <param name="decodedLength">The decoded length will be retrieved by this param.</param>
        /// <returns>The number of the bytes consumed in the buffer to decode the length.</returns>
        public static int LengthBerDecode(IAsn1DecodingBuffer buffer, out int decodedLength)
        {
            byte firstByte = buffer.ReadByte();

            if (firstByte < 128)
            {
                decodedLength = firstByte;
                return(1);
            }
            else
            {
                int length = (firstByte & ((1 << 7) - 1));//set the first bit to 0
                if (length == 0)
                {
                    //Check X.690: 8.1.3.6
                    throw new NotImplementedException("Indefinite form of length is not implemented. Check X.690: 8.1.3.6.");
                }
                byte[] bigEndianLengthCode = buffer.ReadBytes(length);
                byte[] allContent          = new byte[sizeof(int)];//no need to use long since all of the Length property is int
                Array.Copy(bigEndianLengthCode, 0,
                           allContent, sizeof(int) - bigEndianLengthCode.Length, bigEndianLengthCode.Length);
                //allContent is big endian
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(allContent);
                }
                decodedLength = BitConverter.ToInt32(allContent, 0);
                return(1 + length);
            }
        }
        /// <summary>
        /// Decodes the object by BER.
        /// </summary>
        /// <param name="buffer">A buffer that contains a BER encoding result.</param>
        /// <param name="explicitTag">Indicates whether the tags should be encoded explicitly. In our Test Suites, it will always be true.</param>
        /// <returns>The number of the bytes consumed in the buffer to decode this object.</returns>
        /// <exception cref="Asn1ConstraintsNotSatisfied">
        /// Thrown when the constraints are not satisfied after decoding.
        /// </exception>
        /// <exception cref="Asn1DecodingUnexpectedData">
        /// Thrown when the data in the buffer can not be properly decoded.
        /// </exception>
        /// <remarks>Override this method in a user-defined class only if the procedure is not applicable in some special scenarios.</remarks>
        public override int BerDecode(IAsn1DecodingBuffer buffer, bool explicitTag = true)
        {
            int     headLen = 0;
            Asn1Tag seqTag;

            headLen += TagBerDecode(buffer, out seqTag);
            int valueLen;

            headLen += LengthBerDecode(buffer, out valueLen);

            // Decode Request Name
            int valueLenDecode = 0;

            Asn1Tag nameTag;

            valueLenDecode  += TagBerDecode(buffer, out nameTag);
            this.requestName = new LDAPOID();
            valueLenDecode  += this.requestName.BerDecodeWithoutUnisersalTag(buffer);

            // Decode Request Value
            Asn1Tag valueTag;

            valueLenDecode   += TagBerDecode(buffer, out valueTag);
            this.requestValue = new Asn1OctetString();
            valueLenDecode   += this.requestValue.BerDecodeWithoutUnisersalTag(buffer);

            if (!VerifyConstraints())
            {
                throw new Asn1ConstraintsNotSatisfied(ExceptionMessages.ConstraintsNotSatisfied
                                                      + " Decode " + this.GetType().Name + ".");
            }

            return(headLen + valueLen);
        }
 /// <summary>
 /// Decodes the object by PER.
 /// </summary>
 /// <param name="buffer">A buffer that contains a PER encoding result.</param>
 /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
 /// <remarks>Length is included.</remarks>
 protected override void ValuePerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
 {
     ByteArrayValue = Asn1StandardProcedure.PerDecodeArray<byte>(buffer,
         decodingBuffer =>
         {
             Asn1Integer ai = new Asn1Integer(null, 0, 15);
             ai.PerDecode(buffer);
             if (Constraint == null || Constraint.PermittedCharSet == null)
             {
                 if (ai.Value == 0)
                 {
                     return (byte)' ';
                 }
                 else if (ai.Value >= 1 && ai.Value <= 10)
                 {
                     return (byte)((byte)'0' + ai.Value - 1);
                 }
                 else
                 {
                     throw new Asn1DecodingUnexpectedData(ExceptionMessages.DecodingUnexpectedData +
                                                          "Only 0~9 and SPACE are allowed in NumericString.");
                 }
             }
             else
             {
                 int index = (int)ai.Value;
                 return (byte)CharSetInArray[(int)ai.Value];
             }
         },
     Constraint != null && Constraint.HasMinSize ? Constraint.MinSize : (long?)null, Constraint != null && Constraint.HasMaxSize ? Constraint.MaxSize : (long?)null,
     true);
 }
        /// <summary>
        /// Decodes the data from the buffer and stores the data in this object.
        /// </summary>
        /// <param name="buffer">A buffer that stores a BER encoding result.</param>
        /// <param name="length">The length of the encoding result of the data in the given buffer.</param>
        /// <returns>The number of the bytes consumed in the buffer to decode the data.</returns>
        /// <remarks>
        /// For the TLV (Tag, Length, Value) triple in the encoding result,
        /// this method provides the functionality of decoding Value.
        /// The decoding for Tag and Length will be done in Asn1Object::BerDecode method.
        /// </remarks>
        protected override int ValueBerDecode(IAsn1DecodingBuffer buffer, int length)
        {
            var all = buffer.ReadBytes(length);

            Value = DecodeIdentifiers(all);
            return(length);
        }
        public override int BerDecode(IAsn1DecodingBuffer buffer, bool explicitTag = true)
        {
            int headLen = 0;
            Asn1Tag seqTag;
            headLen += TagBerDecode(buffer, out seqTag);
            int valueLen;
            headLen += LengthBerDecode(buffer, out valueLen);

            int valueLenDecode = 0;
            messageID = new MessageID();
            valueLenDecode += messageID.BerDecode(buffer);
            protocolOp = new LDAPMessage_protocolOp();
            valueLenDecode += protocolOp.BerDecode(buffer);
            if (valueLenDecode == valueLen)
            {
                controls = null;
            }
            else
            {
                Asn1Tag contextTag;
                valueLenDecode += TagBerDecode(buffer, out contextTag);
                controls = new Controls();
                valueLenDecode += controls.BerDecodeWithoutUnisersalTag(buffer);
            }
            if (valueLen != valueLenDecode)
            {
                throw new Asn1DecodingUnexpectedData(ExceptionMessages.DecodingUnexpectedData + " LDAPResult.");
            }
            return headLen + valueLen;
        }
        public override int BerDecode(IAsn1DecodingBuffer buffer, bool explicitTag = true)
        {
            int     headLen = 0;
            Asn1Tag seqTag;

            headLen += TagBerDecode(buffer, out seqTag);
            int valueLen;

            headLen += LengthBerDecode(buffer, out valueLen);

            int valueLenDecode = 0;

            messageID       = new MessageID();
            valueLenDecode += messageID.BerDecode(buffer);
            protocolOp      = new LDAPMessage_protocolOp();
            valueLenDecode += protocolOp.BerDecode(buffer);
            if (valueLenDecode == valueLen)
            {
                controls = null;
            }
            else
            {
                Asn1Tag contextTag;
                valueLenDecode += TagBerDecode(buffer, out contextTag);
                controls        = new Controls();
                valueLenDecode += controls.BerDecodeWithoutUnisersalTag(buffer);
            }
            if (valueLen != valueLenDecode)
            {
                throw new Asn1DecodingUnexpectedData(ExceptionMessages.DecodingUnexpectedData + " LDAPResult.");
            }
            return(headLen + valueLen);
        }
        public override int BerDecode(IAsn1DecodingBuffer buffer, bool explicitTag = true)
        {
            Asn1Tag asn1Tag;
            int     headLen = 0, valueLen = 0, tagLength = 0;

            if (explicitTag)
            {
                headLen += TagBerDecode(buffer, out asn1Tag);
                headLen += LengthBerDecode(buffer, out valueLen);
            }

            int valueLenDecode = 0;

            messageID       = new MessageID();
            valueLenDecode += messageID.BerDecode(buffer, true);
            protocolOp      = new LDAPMessage_protocolOp();
            valueLenDecode += protocolOp.BerDecode(buffer, true);

            asn1Tag = new Asn1Tag(Asn1TagType.Context, 0)
            {
                EncodingWay = EncodingWay.Constructed
            };
            if (IsTagMatch(buffer, asn1Tag, out tagLength, true))
            {
                controls        = new Controls();
                valueLenDecode += controls.BerDecode(buffer, false);
            }
            valueLenDecode += tagLength;

            return(headLen + valueLenDecode);
        }
 /// <summary>
 /// Reads/decodes a length from the buffer.
 /// </summary>
 /// <param name="buffer">A buffer that stores a BER encoding result.</param>
 /// <param name="decodedLength">The decoded length will be retrieved by this param.</param>
 /// <returns>The number of the bytes consumed in the buffer to decode the length.</returns>
 public static int LengthBerDecode(IAsn1DecodingBuffer buffer, out int decodedLength)
 {
     byte firstByte = buffer.ReadByte();
     if (firstByte < 128)
     {
         decodedLength = firstByte;
         return 1;
     }
     else
     {
         int length = (firstByte & ((1 << 7) - 1));//set the first bit to 0
         if (length == 0)
         {
             //Check X.690: 8.1.3.6
             throw new NotImplementedException("Indefinite form of length is not implemented. Check X.690: 8.1.3.6.");
         }
         byte[] bigEndianLengthCode = buffer.ReadBytes(length);
         byte[] allContent = new byte[sizeof(int)];//no need to use long since all of the Length property is int
         Array.Copy(bigEndianLengthCode, 0,
             allContent, sizeof(int) - bigEndianLengthCode.Length, bigEndianLengthCode.Length);
         //allContent is big endian
         if (BitConverter.IsLittleEndian)
         {
             Array.Reverse(allContent);
         }
         decodedLength = BitConverter.ToInt32(allContent, 0);
         return 1 + length;
     }
 }
        /// <summary>
        /// Decodes the object by BER.
        /// </summary>
        /// <param name="buffer">A buffer that contains a BER encoding result.</param>
        /// <returns>The number of the bytes consumed in the buffer to decode this object.</returns>
        /// <exception cref="Asn1ConstraintsNotSatisfied">
        /// Thrown when the constraints are not satisfied after decoding.
        /// </exception>
        /// <exception cref="Asn1DecodingUnexpectedData">
        /// Thrown when the data in the buffer can not be properly decoded.
        /// </exception>
        /// <remarks>This method is overrode because Universal Class Tag is not included in the encoding result.</remarks>
        public override int BerDecode(IAsn1DecodingBuffer buffer, bool explicitTag = true)
        {
            int returnVal = 0;
            //Decode the top most tag and universal class tag
            Asn1Tag topTag;
            returnVal += TagBerDecode(buffer, out topTag);

            Asn1Tag topTagInDefinition = this.TopTag;
            if (!topTag.Equals(topTagInDefinition))
            {
                throw new Asn1DecodingUnexpectedData(ExceptionMessages.DecodingUnexpectedData + " Top Most Tag decoding fail.");
            }

            //Decode length
            int lengthAfterUniTag;
            returnVal += LengthBerDecode(buffer, out lengthAfterUniTag);
            //Decode data
            returnVal += ValueBerDecode(buffer, lengthAfterUniTag);

            if (!VerifyConstraints())
            {
                throw new Asn1ConstraintsNotSatisfied(ExceptionMessages.ConstraintsNotSatisfied
                    + " Decode " + this.GetType().Name + ".");
            }

            return returnVal;
        }
Пример #15
0
 /// <summary>
 /// Decodes the object by PER.
 /// </summary>
 /// <param name="buffer">A buffer that contains a PER encoding result.</param>
 /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
 /// <remarks>Length is included.</remarks>
 protected override void ValuePerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
 {
     ByteArrayValue = Asn1StandardProcedure.PerDecodeArray <byte>(buffer,
                                                                  decodingBuffer =>
     {
         Asn1Integer ai = new Asn1Integer(null, 0, 15);
         ai.PerDecode(buffer);
         if (Constraint == null || Constraint.PermittedCharSet == null)
         {
             if (ai.Value == 0)
             {
                 return((byte)' ');
             }
             else if (ai.Value >= 1 && ai.Value <= 10)
             {
                 return((byte)((byte)'0' + ai.Value - 1));
             }
             else
             {
                 throw new Asn1DecodingUnexpectedData(ExceptionMessages.DecodingUnexpectedData +
                                                      "Only 0~9 and SPACE are allowed in NumericString.");
             }
         }
         else
         {
             int index = (int)ai.Value;
             return((byte)CharSetInArray[(int)ai.Value]);
         }
     },
                                                                  Constraint != null && Constraint.HasMinSize ? Constraint.MinSize : (long?)null, Constraint != null && Constraint.HasMaxSize ? Constraint.MaxSize : (long?)null,
                                                                  true);
 }
 /// <summary>
 /// Decodes the object by PER.
 /// </summary>
 /// <param name="buffer">A buffer that contains a PER encoding result.</param>
 /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
 /// <remarks>Length is included.</remarks>
 protected override void ValuePerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
 {
     if (Constraint != null && Constraint.HasMinSize && Constraint.HasMinSize &&
         Constraint.MinSize == Constraint.MaxSize && Constraint.MinSize <= 2)    //Ref. X.691:16.6
     {
         long   minSize    = Constraint.MinSize;
         bool[] bitResult  = buffer.ReadBits((int)(8 * minSize));
         byte[] byteResult = new byte[(int)minSize];
         //Convert bool array to byte array
         int byteIndex = 0, bitIndex = 0;
         foreach (bool b in bitResult)
         {
             if (b)
             {
                 byteResult[byteIndex] |= (byte)(1 << (7 - bitIndex));
             }
             bitIndex++;
             if (bitIndex == 8)
             {
                 bitIndex = 0;
                 byteIndex++;
             }
         }
         ByteArrayValue = byteResult;
     }
     else
     {
         ByteArrayValue = Asn1StandardProcedure.PerDecodeArray(buffer,
                                                               decodingBuffer => decodingBuffer.ReadByte(),
                                                               Constraint != null && Constraint.HasMinSize ? Constraint.MinSize : (long?)null, Constraint != null && Constraint.HasMaxSize ? Constraint.MaxSize : (long?)null);
     }
 }
Пример #17
0
        public override int BerDecode(IAsn1DecodingBuffer buffer, bool explicitTag = true)
        {
            int     headLen = 0;
            Asn1Tag seqTag;

            headLen += TagBerDecode(buffer, out seqTag);
            int valueLen;

            headLen += LengthBerDecode(buffer, out valueLen);

            int valueLenDecode = 0;

            resultCode      = new LDAPResult_resultCode();
            valueLenDecode += resultCode.BerDecode(buffer);
            matchedDN       = new LDAPDN();
            valueLenDecode += matchedDN.BerDecode(buffer);
            errorMessage    = new LDAPString();
            valueLenDecode += errorMessage.BerDecode(buffer);
            if (valueLenDecode == valueLen)
            {
                referral = null;
            }
            else
            {
                Asn1Tag contextTag;
                valueLenDecode += TagBerDecode(buffer, out contextTag);
                referral        = new Referral();
                valueLenDecode += referral.BerDecodeWithoutUnisersalTag(buffer);
            }
            if (valueLen != valueLenDecode)
            {
                throw new Asn1DecodingUnexpectedData(ExceptionMessages.DecodingUnexpectedData + " LDAPResult.");
            }
            return(headLen + valueLen);
        }
        public override int BerDecode(IAsn1DecodingBuffer buffer, bool explicitTag = true)
        {
            buffer.ReadBytes(2);
            int consumedLen = 2, len;

            consumedLen += LengthBerDecode(buffer, out len);
            return(consumedLen + ValueBerDecode(buffer, len));
        }
        /// <summary>
        /// Decodes the object by BER.
        /// </summary>
        /// <param name="buffer">A buffer that contains a BER encoding result.</param>
        /// <returns>The number of the bytes consumed in the buffer to decode this object.</returns>
        /// <exception cref="Asn1ConstraintsNotSatisfied">
        /// Thrown when the constraints are not satisfied after decoding.
        /// </exception>
        /// <exception cref="Asn1DecodingUnexpectedData">
        /// Thrown when the data in the buffer can not be properly decoded.
        /// </exception>
        /// <remarks>Override this method in a user-defined class only if the procedure is not applicable in some special scenarios.</remarks>
        public override int BerDecode(IAsn1DecodingBuffer buffer, bool explicitTag)
        {
            int     decodeLen = 0;
            int     resultVal = LengthBerDecode(buffer, out decodeLen);
            Asn1Tag topTag;

            resultVal += TagBerDecode(buffer, out topTag);
            return(resultVal);
        }
 public override int BerDecode(IAsn1DecodingBuffer buffer)
 {
     int consumed = 0;
     int length;
     Asn1Tag tag;
     consumed += TagBerDecode(buffer, out tag);
     consumed += LengthBerDecode(buffer, out length);
     consumed += base.BerDecode(buffer);
     return consumed;
 }
Пример #21
0
 /// <summary>
 /// Decodes the content of the object by PER.
 /// </summary>
 /// <param name="buffer">A buffer that contains a PER encoding result.</param>
 /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
 /// <remarks>Length is included.</remarks>
 protected override void ValuePerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
 {
     Elements = Asn1StandardProcedure.PerDecodeArray(buffer, decodingBuffer =>
     {
         T curObj = new T();
         curObj.PerDecode(buffer);
         return(curObj);
     },
                                                     Constraint != null && Constraint.HasMinSize ? Constraint.MinSize : (long?)null, Constraint != null && Constraint.HasMaxSize ? Constraint.MaxSize : (long?)null);
 }
        public override int BerDecode(IAsn1DecodingBuffer buffer, bool explicitTag = true)
        {
            int     consumed = 0;
            int     length;
            Asn1Tag tag;

            consumed += TagBerDecode(buffer, out tag);
            consumed += LengthBerDecode(buffer, out length);
            consumed += base.BerDecode(buffer);
            return(consumed);
        }
Пример #23
0
        /// <summary>
        /// Decodes the object by PER.
        /// </summary>
        /// <param name="buffer">A buffer that contains a PER encoding result.</param>
        /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
        protected override void ValuePerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
        {
            bool result = buffer.ReadBit();

            if (result)
            {
                Value = true;
            }
            else
            {
                Value = false;
            }
        }
Пример #24
0
        /// <summary>
        /// Decodes the content of the object by PER.
        /// </summary>
        /// <param name="buffer">A buffer that contains a PER encoding result.</param>
        /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
        protected override void ValuePerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
        {
            //Decode the index specifying the chosen alternative, Ref: X.691: 22
            Asn1Integer ai = new Asn1Integer(null, 0, definedAllowedIndices.Count - 1);

            ai.PerDecode(buffer);
            //Decode the chosen alternative
            Asn1Object[] instances = ChoiceTypeInstances;
            Asn1Object   obj       = instances[(int)ai.Value];

            obj.PerDecode(buffer);
            SetData(definedAllowedIndices[(int)ai.Value], obj);
        }
Пример #25
0
        /// <summary>
        /// Decodes the object by PER.
        /// </summary>
        /// <param name="buffer">A buffer that contains a PER encoding result.</param>
        /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
        public override void PerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
        {
            if (HasExternalObjects)
            {
                buffer.ReadBit();
                //TODO: if true is read, store the decoding value to external object
            }
            Asn1Integer ai = new Asn1Integer(null, Min, Max);

            ai.PerDecode(buffer, aligned);
            Debug.Assert(ai.Value != null, "ai.Value != null");
            Value = allowedValues[(int)ai.Value];
        }
Пример #26
0
 /// <summary>
 /// Decodes the object by PER.
 /// </summary>
 /// <param name="buffer">A buffer that contains a PER encoding result.</param>
 /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
 /// <remarks>Length is included.</remarks>
 protected override void ValuePerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
 {
     char[] result = Asn1StandardProcedure.PerDecodeArray <char>(buffer,
                                                                 decodingBuffer =>
     {
         byte[] encodingResult = buffer.ReadBytes(2);
         string s = Encoding.BigEndianUnicode.GetString(encodingResult);
         return(s[0]);
     },
                                                                 Constraint != null && Constraint.HasMinSize ? Constraint.MinSize : (long?)null, Constraint != null && Constraint.HasMaxSize ? Constraint.MaxSize : (long?)null,
                                                                 true);
     Value = new string(result);
 }
Пример #27
0
        /// <summary>
        /// Check if tag from buffer is match with input Asn1Tag
        /// </summary>
        /// <param name="buffer">decode buffer</param>
        /// <param name="tag">compare Asn1Tag</param>
        /// <param name="length">Tag Length</param>
        /// <param name="IsForward">If true Postion + 1, otherwise not change Postion</param>
        /// <returns>Tag from Buffer is match with input Tag</returns>
        public virtual bool IsTagMatch(IAsn1DecodingBuffer buffer, Asn1Tag tag, out int length, bool IsForward = false)
        {
            length = 0;
            if (buffer.IsNomoreData())
            {
                return(false);
            }

            byte tagByte       = Asn1StandardProcedure.GetEncodeTag(tag);
            byte bufferTagByte = IsForward ? buffer.ReadByte() : buffer.PeekByte();

            length = IsForward ? 1 : 0;
            return(tagByte == bufferTagByte);
        }
        /// <summary>
        /// Decodes a tag from the buffer.
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="tag"></param>
        /// <returns>The number of the bytes consumed in the buffer to decode the tag.</returns>
        public static int TagBerDecode(IAsn1DecodingBuffer buffer, out Asn1Tag tag)
        {
            byte        prefix       = buffer.ReadByte();
            int         firstTwoBits = (prefix >> 6);
            Asn1TagType tagType      = Asn1TagType.Private;
            long        tagValue;

            switch (firstTwoBits)
            {
            case 0:
            {
                tagType = Asn1TagType.Universal;
            } break;

            case 1:
            {
                tagType = Asn1TagType.Application;
            } break;

            case 2:
            {
                tagType = Asn1TagType.Context;
            } break;

            case 3:
            {
                throw new NotImplementedException(ExceptionMessages.Unreachable);
            };
            }
            tagValue = prefix & ((byte)(1 << 5) - 1);
            if (tagValue <= 30)
            {
                tag = new Asn1Tag(tagType, tagValue);
                if ((prefix & (1 << 5)) != 0)
                {
                    tag.EncodingWay = EncodingWay.Constructed;
                }
                else
                {
                    tag.EncodingWay = EncodingWay.Primitive;
                }
                return(1);
            }
            else
            {
                throw new NotImplementedException("Case tag > 30 is not implemented. Check X.690: 8.1.2.4.3.");
            }
        }
        /// <summary>
        /// Decodes the object by BER.
        /// </summary>
        /// <param name="buffer">A buffer that contains a BER encoding result.</param>
        /// <returns>The number of the bytes consumed in the buffer to decode this object.</returns>
        /// <exception cref="Asn1ConstraintsNotSatisfied">
        /// Thrown when the constraints are not satisfied after decoding.
        /// </exception>
        /// <exception cref="Asn1DecodingUnexpectedData">
        /// Thrown when the data in the buffer can not be properly decoded.
        /// </exception>
        /// <remarks>Override this method in a user-defined class only if the procedure is not applicable in some special scenarios.</remarks>
        public virtual int BerDecode(IAsn1DecodingBuffer buffer)
        {
            int returnVal = 0;
            //Decode the top most tag and universal class tag
            int     lengthAfterTopTag;
            Asn1Tag topTag;

            returnVal += TagBerDecode(buffer, out topTag);

            Asn1Tag topTagInDefinition = this.TopTag;

            if (!topTag.Equals(topTagInDefinition))
            {
                throw new Asn1DecodingUnexpectedData(ExceptionMessages.DecodingUnexpectedData + " Top Most Tag decoding fail.");
            }
            else
            {
                if (topTag.TagType != Asn1TagType.Universal)
                {
                    returnVal += LengthBerDecode(buffer, out lengthAfterTopTag);
                    Asn1Tag uniTag;
                    returnVal += TagBerDecode(buffer, out uniTag);
                    Asn1Tag uniTagInDefinition = this.UniversalTag;
                    if (!uniTag.Equals(uniTagInDefinition))
                    {
                        throw new Asn1DecodingUnexpectedData(ExceptionMessages.DecodingUnexpectedData + " Universal Class Tag decoding fail.");
                    }
                }
            }

            //Decode length
            int lengthAfterUniTag;

            returnVal += LengthBerDecode(buffer, out lengthAfterUniTag);
            //Decode data
            returnVal += ValueBerDecode(buffer, lengthAfterUniTag);

            if (!VerifyConstraints())
            {
                throw new Asn1ConstraintsNotSatisfied(ExceptionMessages.ConstraintsNotSatisfied
                                                      + " Decode " + this.GetType().Name + ".");
            }

            return(returnVal);
        }
Пример #30
0
        /// <summary>
        /// Decodes the data and length of the data.
        /// </summary>
        /// <param name="buffer">A buffer that contains a BER encoding result without tags.</param>
        public int BerDecodeWithoutUnisersalTag(IAsn1DecodingBuffer buffer)
        {
            int returnVal = 0;
            //Decode length
            int length;

            returnVal += LengthBerDecode(buffer, out length);
            //Decode data
            returnVal += ValueBerDecode(buffer, length);

            if (!VerifyConstraints())
            {
                throw new Asn1ConstraintsNotSatisfied(ExceptionMessages.ConstraintsNotSatisfied
                                                      + " Decode " + this.GetType().Name + ".");
            }

            return(returnVal);
        }
Пример #31
0
        /// <summary>
        /// Decodes the data from the buffer and stores the data in this object.
        /// </summary>
        /// <param name="buffer">A buffer that stores a BER encoding result.</param>
        /// <param name="length">The length of the encoding result of the data in the given buffer.</param>
        /// <returns>The number of the bytes consumed in the buffer to decode the data.</returns>
        /// <remarks>
        /// For the TLV (Tag, Length, Value) triple in the encoding result,
        /// this method provides the functionality of decoding Value.
        /// The decoding for Tag and Length will be done in Asn1Object::BerDecode method.
        /// </remarks>
        protected override int ValueBerDecode(IAsn1DecodingBuffer buffer, int length)
        {
            if (length != 1)
            {
                throw new Asn1DecodingUnexpectedData(ExceptionMessages.DecodingUnexpectedData);
            }
            byte readContent = buffer.ReadByte();//Since length == 1

            if (readContent == 0)
            {
                Value = false;
            }
            else
            {
                Value = true;
            }
            return(length);
        }
Пример #32
0
        /// <summary>
        /// Decodes the object by BER.
        /// </summary>
        /// <param name="buffer">A buffer that contains a BER encoding result.</param>
        /// <param name="explicitTag">Indicates whether the tags should be encoded explicitly. In our Test Suites, it will always be true.</param>
        /// <returns>The number of the bytes consumed in the buffer to decode this object.</returns>
        /// <exception cref="Asn1ConstraintsNotSatisfied">
        /// Thrown when the constraints are not satisfied after decoding.
        /// </exception>
        /// <exception cref="Asn1DecodingUnexpectedData">
        /// Thrown when the data in the buffer can not be properly decoded.
        /// </exception>
        /// <remarks>Override this method in a user-defined class only if the procedure is not applicable in some special scenarios.</remarks>
        public override int BerDecode(IAsn1DecodingBuffer buffer, bool explicitTag = true)
        {
            int headLen = 0, valueLen = 0;

            if (explicitTag)
            {
                Asn1Tag seqTag;
                headLen += TagBerDecode(buffer, out seqTag);
                headLen += LengthBerDecode(buffer, out valueLen);
            }

            int     valueLenDecode = 0;
            Asn1Tag valueTag;

            // Decode referral
            valueLenDecode   += TagBerDecode(buffer, out valueTag);
            this.matchingRule = new MatchingRuleId();
            valueLenDecode   += this.matchingRule.BerDecodeWithoutUnisersalTag(buffer);

            // Decode type
            valueLenDecode += TagBerDecode(buffer, out valueTag);
            this.type       = new AttributeDescription();
            valueLenDecode += this.type.BerDecodeWithoutUnisersalTag(buffer);

            // Decode matchValue
            valueLenDecode += TagBerDecode(buffer, out valueTag);
            this.matchValue = new AssertionValue();
            valueLenDecode += this.matchValue.BerDecodeWithoutUnisersalTag(buffer);

            // Decode dnAttributes
            valueLenDecode   += TagBerDecode(buffer, out valueTag);
            this.dnAttributes = new Asn1Boolean();
            valueLenDecode   += this.dnAttributes.BerDecodeWithoutUnisersalTag(buffer);

            if (!VerifyConstraints())
            {
                throw new Asn1ConstraintsNotSatisfied(ExceptionMessages.ConstraintsNotSatisfied
                                                      + " Decode " + this.GetType().Name + ".");
            }

            return(headLen + valueLen);
        }
Пример #33
0
 /// <summary>
 /// Decodes the object by PER.
 /// </summary>
 /// <param name="buffer">A buffer that contains a PER encoding result.</param>
 /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
 /// <remarks>
 /// Includes the constraints verification and external object check.
 /// </remarks>
 public virtual void PerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
 {
     if (aligned == false)
     {
         throw new NotImplementedException(ExceptionMessages.UnalignedNotImplemented);
     }
     if (HasExternalObjects)
     {
         bool hasExt = buffer.ReadBit();
         if (hasExt)
         {
             throw new NotImplementedException("External marker is not implemented yet.");
         }
     }
     ValuePerDecode(buffer, aligned);
     if (!VerifyConstraints())
     {
         throw new Asn1ConstraintsNotSatisfied(ExceptionMessages.ConstraintsNotSatisfied);
     }
 }
        public override int BerDecode(IAsn1DecodingBuffer buffer, bool explicitTag = true)
        {
            int headLen = 0;
            Asn1Tag appTag;
            headLen += TagBerDecode(buffer, out appTag);
            int valueLen;
            headLen += LengthBerDecode(buffer, out valueLen);

            int valueLenDecode = 0;
            version = new Asn1Integer();
            valueLenDecode += version.BerDecode(buffer);
            name = new LDAPDN();
            valueLenDecode += name.BerDecode(buffer);
            authentication = new AuthenticationChoice();
            valueLenDecode += authentication.BerDecode(buffer);
            if (valueLen != valueLenDecode)
            {
                throw new Asn1DecodingUnexpectedData(ExceptionMessages.DecodingUnexpectedData + " LDAPResult.");
            }
            return headLen + valueLen;
        }
        /// <summary>
        /// Decodes the object by BER.
        /// </summary>
        /// <param name="buffer">A buffer that contains a BER encoding result.</param>
        /// <param name="explicitTag">Indicates whether the tags should be encoded explicitly. In our Test Suites, it will always be true.</param>
        /// <returns>The number of the bytes consumed in the buffer to decode this object.</returns>
        /// <exception cref="Asn1ConstraintsNotSatisfied">
        /// Thrown when the constraints are not satisfied after decoding.
        /// </exception>
        /// <exception cref="Asn1DecodingUnexpectedData">
        /// Thrown when the data in the buffer can not be properly decoded.
        /// </exception>
        /// <remarks>Override this method in a user-defined class only if the procedure is not applicable in some special scenarios.</remarks>
        public override int BerDecode(IAsn1DecodingBuffer buffer, bool explicitTag = true)
        {
            //Decode the top most tag and universal class tag
            int     headLen = 0;
            Asn1Tag seqTag;

            headLen += TagBerDecode(buffer, out seqTag);
            int valueLen;

            headLen += LengthBerDecode(buffer, out valueLen);

            int valueLenDecode = 0;

            this.entry      = new LDAPDN();
            valueLenDecode += this.entry.BerDecode(buffer, true);

            this.newrdn     = new RelativeLDAPDN();
            valueLenDecode += this.newrdn.BerDecode(buffer, true);

            this.deleteoldrdn = new Asn1Boolean();
            valueLenDecode   += this.deleteoldrdn.BerDecode(buffer, true);

            if (valueLenDecode == valueLen)
            {
                newSuperior = null;
            }
            else
            {
                Asn1Tag contextTag;
                valueLenDecode += TagBerDecode(buffer, out contextTag);
                newSuperior     = new LDAPDN();
                valueLenDecode += newSuperior.BerDecodeWithoutUnisersalTag(buffer);
            }

            if (valueLen != valueLenDecode)
            {
                throw new Asn1DecodingUnexpectedData(ExceptionMessages.DecodingUnexpectedData + " ModifyDNRequest.");
            }
            return(headLen + valueLen);
        }
        /// <summary>
        /// Decodes the content of the object by PER.
        /// </summary>
        /// <param name="buffer">A buffer that contains a PER encoding result.</param>
        /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
        protected override void ValuePerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
        {
            int optionalFieldsCount = 0;

            foreach (var v in fieldsMetaData)
            {
                if (v.Optional)
                {
                    optionalFieldsCount++;
                }
            }
            if (optionalFieldsCount >= 64 * 1024) //64k
            {
                throw new NotImplementedException("More than 64K optional fields are not supported yet.");
                //Ref. X.691: 18.3
            }
            bool[]       bitMap               = buffer.ReadBits(optionalFieldsCount);
            Asn1Object[] decodingResult       = FieldsTypeInstances;
            int          curOptionalFlagIndex = 0; //index in bitMap

            for (int i = 0; i < decodingResult.Length; i++)
            {
                if (fieldsMetaData[i].Optional == false ||
                    fieldsMetaData[i].Optional && bitMap[curOptionalFlagIndex++])
                {
                    decodingResult[i].PerDecode(buffer, aligned);
                }
                else
                {
                    //fieldOptionalFlags[i] equals to true and bitMap[curOptionalFlagIndex] equals to false.
                    decodingResult[i] = null;
                }
            }

            for (int i = 0; i < decodingResult.Length; i++)
            {
                fieldsMetaData[i].ValueInOutObject = decodingResult[i];
            }
        }
Пример #37
0
        /// <summary>
        /// Decodes the data from the buffer and stores the data in this object.
        /// </summary>
        /// <param name="buffer">A buffer that stores a BER encoding result.</param>
        /// <param name="length">The length of the encoding result of the data in the given buffer.</param>
        /// <returns>The number of the bytes consumed in the buffer to decode the data.</returns>
        /// <remarks>
        /// For the TLV (Tag, Length, Value) triple in the encoding result,
        /// this method provides the functionality of decoding Value.
        /// The decoding for Tag and Length will be done in Asn1Object::BerDecode method.
        /// </remarks>
        protected override int ValueBerDecode(IAsn1DecodingBuffer buffer, int length)
        {
            int      consumedLen = 0;
            List <T> list        = new List <T>();

            while (consumedLen < length)
            {
                T curObj = new T();
                consumedLen += curObj.BerDecode(buffer);
                list.Add(curObj);
            }
            Elements = list.ToArray();

            //Ensure consumedLen equals to length
            if (consumedLen > length)
            {
                Elements = null;
                throw new Asn1DecodingUnexpectedData(ExceptionMessages.DecodingUnexpectedData);
            }

            return(consumedLen);
        }
Пример #38
0
        public override int BerDecode(IAsn1DecodingBuffer buffer, bool explicitTag = true)
        {
            int     headLen = 0;
            Asn1Tag appTag;

            headLen += TagBerDecode(buffer, out appTag);
            int valueLen;

            headLen += LengthBerDecode(buffer, out valueLen);

            int valueLenDecode = 0;

            version         = new Asn1Integer();
            valueLenDecode += version.BerDecode(buffer);
            name            = new LDAPDN();
            valueLenDecode += name.BerDecode(buffer);
            authentication  = new AuthenticationChoice();
            valueLenDecode += authentication.BerDecode(buffer);
            if (valueLen != valueLenDecode)
            {
                throw new Asn1DecodingUnexpectedData(ExceptionMessages.DecodingUnexpectedData + " LDAPResult.");
            }
            return(headLen + valueLen);
        }
 /// <summary>
 /// Decodes the data from the buffer and stores the data in this object.
 /// </summary>
 /// <param name="buffer">A buffer that stores a BER encoding result.</param>
 /// <param name="length">The length of the encoding result of the data in the given buffer.</param>
 /// <returns>The number of the bytes consumed in the buffer to decode the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result, 
 /// this method provides the functionality of decoding Value.
 /// The decoding for Tag and Length will be done in Asn1Object::BerDecode method.
 /// </remarks>
 protected override int ValueBerDecode(IAsn1DecodingBuffer buffer, int length)
 {
     byte[] result = buffer.ReadBytes(length);
     this.Value = UniversalStringDecode(result);
     return length;
 }
 /// <summary>
 /// Decodes the object by PER.
 /// </summary>
 /// <param name="buffer">A buffer that contains a PER encoding result.</param>
 /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
 /// <remarks>Length is included.</remarks>
 protected override void ValuePerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
 {
     if (Constraint != null && Constraint.HasMinSize && Constraint.HasMinSize
         && Constraint.MinSize == Constraint.MaxSize && Constraint.MinSize <= 2) //Ref. X.691:16.6
     {
         long minSize = Constraint.MinSize;
         bool[] bitResult = buffer.ReadBits((int)(8 * minSize));
         byte[] byteResult = new byte[(int)minSize];
         //Convert bool array to byte array
         int byteIndex = 0, bitIndex = 0;
         foreach (bool b in bitResult)
         {
             if (b)
             {
                 byteResult[byteIndex] |= (byte)(1 << (7 - bitIndex));
             }
             bitIndex++;
             if (bitIndex == 8)
             {
                 bitIndex = 0;
                 byteIndex++;
             }
         }
         ByteArrayValue = byteResult;
     }
     else
     {
         ByteArrayValue = Asn1StandardProcedure.PerDecodeArray(buffer,
         decodingBuffer => decodingBuffer.ReadByte(),
         Constraint != null && Constraint.HasMinSize ? Constraint.MinSize : (long?)null, Constraint != null && Constraint.HasMaxSize ? Constraint.MaxSize : (long?)null);
     }
 }
 public override void PerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
 {
     Value = buffer.ReadBits(2);
 }
        /// <summary>
        /// Decodes the object by BER.
        /// </summary>
        /// <param name="buffer">A buffer that contains a BER encoding result.</param>
        /// <returns>The number of the bytes consumed in the buffer to decode this object.</returns>
        /// <exception cref="Asn1ConstraintsNotSatisfied">
        /// Thrown when the constraints are not satisfied after decoding.
        /// </exception>
        /// <exception cref="Asn1DecodingUnexpectedData">
        /// Thrown when the data in the buffer can not be properly decoded.
        /// </exception>
        /// <remarks>Override this method in a user-defined class only if the procedure is not applicable in some special scenarios.</remarks>
        public virtual int BerDecode(IAsn1DecodingBuffer buffer)
        {
            int returnVal = 0;
            //Decode the top most tag and universal class tag
            int lengthAfterTopTag;
            Asn1Tag topTag;
            returnVal += TagBerDecode(buffer, out topTag);

            Asn1Tag topTagInDefinition = this.TopTag;
            if (!topTag.Equals(topTagInDefinition))
            {
                throw new Asn1DecodingUnexpectedData(ExceptionMessages.DecodingUnexpectedData + " Top Most Tag decoding fail.");
            }
            else
            {
                if (topTag.TagType != Asn1TagType.Universal)
                {
                    returnVal += LengthBerDecode(buffer, out lengthAfterTopTag);
                    Asn1Tag uniTag;
                    returnVal += TagBerDecode(buffer, out uniTag);
                    Asn1Tag uniTagInDefinition = this.UniversalTag;
                    if (!uniTag.Equals(uniTagInDefinition))
                    {
                        throw new Asn1DecodingUnexpectedData(ExceptionMessages.DecodingUnexpectedData + " Universal Class Tag decoding fail.");
                    }
                }
            }

            //Decode length
            int lengthAfterUniTag;
            returnVal += LengthBerDecode(buffer, out lengthAfterUniTag);
            //Decode data
            returnVal += ValueBerDecode(buffer, lengthAfterUniTag);

            if (!VerifyConstraints())
            {
                throw new Asn1ConstraintsNotSatisfied(ExceptionMessages.ConstraintsNotSatisfied
                    + " Decode " + this.GetType().Name + ".");
            }

            return returnVal;
        }
Пример #43
0
 /// <summary>
 /// Decodes the content of the object by PER.
 /// </summary>
 /// <param name="buffer">A buffer that contains a PER encoding result.</param>
 /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
 protected virtual void ValuePerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
 {
     throw new NotImplementedException("PER decoding is not implemented yet.");
 }
Пример #44
0
 /// <summary>
 /// Decodes the data from the buffer and stores the data in this object.
 /// </summary>
 /// <param name="buffer">A buffer that stores a BER encoding result.</param>
 /// <param name="length">The length of the encoding result of the data in the given buffer.</param>
 /// <returns>The number of the bytes consumed in the buffer to decode the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result, 
 /// this method provides the functionality of decoding Value.
 /// The decoding for Tag and Length will be done in Asn1Object::BerDecode method.
 /// </remarks>
 protected virtual int ValueBerDecode(IAsn1DecodingBuffer buffer, int length)
 {
     throw new NotImplementedException("ValueBerDecode should be implemented to support BER Decode.");
 }
 /// <summary>
 /// Decodes the object by PER.
 /// </summary>
 /// <param name="buffer">A buffer that contains a PER encoding result.</param>
 /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
 protected override void ValuePerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
 {
     Asn1Integer ai = new Asn1Integer(null, Constraints.Min, Constraints.Max);
     ai.PerDecode(buffer, aligned);
     Value = allowedValues[(int)ai.Value];
 }
 /// <summary>
 /// Decodes the data from the buffer and stores the data in this object.
 /// </summary>
 /// <param name="buffer">A buffer that stores a BER encoding result.</param>
 /// <param name="length">The length of the encoding result of the data in the given buffer.</param>
 /// <returns>The number of the bytes consumed in the buffer to decode the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result, 
 /// this method provides the functionality of decoding Value.
 /// The decoding for Tag and Length will be done in Asn1Object::BerDecode method.
 /// </remarks>
 protected override int ValueBerDecode(IAsn1DecodingBuffer buffer, int length)
 {
     this.unusedBitsInLastByte = buffer.ReadByte();
     this.data = buffer.ReadBytes(length - 1);
     return length;
 }
 /// <summary>
 /// Decodes the data from the buffer and stores the data in this object.
 /// </summary>
 /// <param name="buffer">A buffer that stores a BER encoding result.</param>
 /// <param name="length">The length of the encoding result of the data in the given buffer.</param>
 /// <returns>The number of the bytes consumed in the buffer to decode the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result, 
 /// this method provides the functionality of decoding Value.
 /// The decoding for Tag and Length will be done in Asn1Object::BerDecode method.
 /// </remarks>
 protected override int ValueBerDecode(IAsn1DecodingBuffer buffer, int length)
 {
     byte[] result = buffer.ReadBytes(length);
     Value = IntegerDecoding(result);
     return length;
 }
 /// <summary>
 /// Decodes the data from the buffer and stores the data in this object.
 /// </summary>
 /// <param name="buffer">A buffer that stores a BER encoding result.</param>
 /// <param name="length">The length of the encoding result of the data in the given buffer.</param>
 /// <returns>The number of the bytes consumed in the buffer to decode the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result, 
 /// this method provides the functionality of decoding Value.
 /// The decoding for Tag and Length will be done in Asn1Object::BerDecode method.
 /// </remarks>
 protected override int ValueBerDecode(IAsn1DecodingBuffer buffer, int length)
 {
     byte[] all = buffer.ReadBytes(length);
     this.Value = DecodeSubidentifiers(all);
     return length;
 }
Пример #49
0
        /// <summary>
        /// Decodes the object by BER.
        /// </summary>
        /// <param name="buffer">A buffer that contains a BER encoding result.</param>
        /// <param name="explicitTag">Indicates whether the tags should be encoded explicitly. In our Test Suites, it will always be true.</param>
        /// <returns>The number of the bytes consumed in the buffer to decode this object.</returns>
        /// <exception cref="Asn1DecodingUnexpectedData">
        /// Thrown when the data in the buffer can not be properly decoded.
        /// </exception>
        public override int BerDecode(IAsn1DecodingBuffer buffer, bool explicitTag = true)
        {
            int length = 0;
            Object[] attrs = GetType().GetCustomAttributes(typeof(Asn1Tag), true);
            Asn1Tag topTag;
            if (attrs.Length != 0)
            {
                Asn1Tag topTagInDefinition = attrs[0] as Asn1Tag;
                length += TagBerDecode(buffer, out topTag);
                if (!topTag.Equals(topTagInDefinition))
                {
                    throw new Asn1DecodingUnexpectedData(ExceptionMessages.DecodingUnexpectedData);
                }
                int tempLength;
                length += LengthBerDecode(buffer, out tempLength);
            }

            //Determine which choice should be decoded.
            int? decodingTargetIndex = null;
            int tagLen = TagBerDecode(buffer, out topTag);
            Asn1Object[] choiceInstances = ChoiceTypeInstances;
            for (int i = 0; i < metaDatas.Length; i++)
            {
                if (metaDatas[i].AttachedTag != null)
                {
                    if (topTag.Equals(metaDatas[i].AttachedTag))
                    {
                        //Decode this choice.
                        length += tagLen;
                        int lengthAfterTopTag;
                        length += LengthBerDecode(buffer, out lengthAfterTopTag);
                        decodingTargetIndex = i;
                        break;
                    }
                    //else check the next choice.
                }
                else
                {
                    if (topTag.Equals(choiceInstances[i].TopTag))
                    {
                        //Decode this choice.
                        decodingTargetIndex = i;
                        buffer.SeekBytePosition(-tagLen);
                        break;
                    }
                    //else check the next choice.
                }
            }
            if (decodingTargetIndex == null)
            {
                throw new Asn1DecodingUnexpectedData(ExceptionMessages.DecodingUnexpectedData
                    + " None of the choices fit the data in the buffer.");
            }
            //Decode the data.
            length += choiceInstances[(int)decodingTargetIndex].BerDecode(buffer);
            //Store the data in the CHOICE.
            SetData(metaDatas[(int)decodingTargetIndex].AttachedIndex, choiceInstances[(int)decodingTargetIndex]);
            return length;
        }
Пример #50
0
 /// <summary>
 /// Decodes the content of the object by PER.
 /// </summary>
 /// <param name="buffer">A buffer that contains a PER encoding result.</param>
 /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
 protected override void ValuePerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
 {
     //Decode the index specifying the chosen alternative, Ref: X.691: 22
     Asn1Integer ai = new Asn1Integer(null, 0, definedAllowedIndices.Count - 1);
     ai.PerDecode(buffer);
     //Decode the chosen alternative
     Asn1Object[] instances = ChoiceTypeInstances;
     Asn1Object obj = instances[(int)ai.Value];
     obj.PerDecode(buffer);
     SetData(definedAllowedIndices[(int)ai.Value], obj);
 }
 /// <summary>
 /// Decodes the data from the buffer and stores the data in this object.
 /// </summary>
 /// <param name="buffer">A buffer that stores a BER encoding result.</param>
 /// <param name="length">The length of the encoding result of the data in the given buffer.</param>
 /// <returns>The number of the bytes consumed in the buffer to decode the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result, 
 /// this method provides the functionality of decoding Value.
 /// The decoding for Tag and Length will be done in Asn1Object::BerDecode method.
 /// </remarks>
 protected override int ValueBerDecode(IAsn1DecodingBuffer buffer, int length)
 {
     if (length != 1)
     {
         throw new Asn1DecodingUnexpectedData(ExceptionMessages.DecodingUnexpectedData);
     }
     byte readContent = buffer.ReadByte();//Since length == 1
     if (readContent == 0)
     {
         Value = false;
     }
     else
     {
         Value = true;
     }
     return length;
 }
 /// <summary>
 /// Decodes the object by PER.
 /// </summary>
 /// <param name="buffer">A buffer that contains a PER encoding result.</param>
 /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
 protected override void ValuePerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
 {
     bool result = buffer.ReadBit();
     if (result)
     {
         Value = true;
     }
     else
     {
         Value = false;
     }
 }
Пример #53
0
        /// <summary>
        /// Decodes the data and length of the data.
        /// </summary>
        /// <param name="buffer">A buffer that contains a BER encoding result without tags.</param>
        public int BerDecodeWithoutUnisersalTag(IAsn1DecodingBuffer buffer)
        {
            int returnVal = 0;
            //Decode length
            int length;
            returnVal += LengthBerDecode(buffer, out length);
            //Decode data
            returnVal += ValueBerDecode(buffer, length);

            if (!VerifyConstraints())
            {
                throw new Asn1ConstraintsNotSatisfied(ExceptionMessages.ConstraintsNotSatisfied
                    + " Decode " + this.GetType().Name + ".");
            }

            return returnVal;
        }
Пример #54
0
 /// <summary>
 /// Decodes the object by PER.
 /// </summary>
 /// <param name="buffer">A buffer that contains a PER encoding result.</param>
 /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
 /// <remarks>
 /// Includes the constraints verification and external object check.
 /// </remarks> 
 public virtual void PerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
 {
     if (aligned == false)
     {
         throw new NotImplementedException(ExceptionMessages.UnalignedNotImplemented);
     }
     if (HasExternalObjects)
     {
         bool hasExt = buffer.ReadBit();
         //TODO: if true is read, store the decoding value to external object
         if (hasExt)
         {
             throw new NotImplementedException("External marker is not implemented yet.");
         }
     }
     ValuePerDecode(buffer, aligned);
     //TODO: decode the external object.
     if (!VerifyConstraints())
     {
         throw new Asn1ConstraintsNotSatisfied(ExceptionMessages.ConstraintsNotSatisfied);
     }
 }
        /// <summary>
        /// Decodes the object by PER.
        /// </summary>
        /// <param name="buffer">A buffer that contains a PER encoding result.</param>
        /// <param name="aligned">Indicating whether the PER decoding is aligned.</param>
        public override void PerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
        {
            if (aligned == false)
            {
                throw new NotImplementedException(ExceptionMessages.UnalignedNotImplemented);
            }
            long baseNum = Min ?? 0;
            //Ref. X.691: 10.5.2
            if (Min == null || Max == null) //range is equal to null
            {
                byte length = buffer.ReadByte();
                if (length == 0)
                {
                    Value = baseNum;
                    return;
                }

                byte[] result = buffer.ReadBytes(length);
                if (Min == null)
                {
                    //No base
                    Value = IntegerDecoding(result);
                }
                else
                {
                    Value = baseNum + NonNegativeBinaryIntegerPerDeocde(result);
                }
            }
            else
            {
                if (Range == 1)
                {
                    this.Value = Min;
                }
                else if (Range <= 255) //10.5.7: a)
                {
                    int bitFieldSize = 1;
                    while (Range > (1 << (bitFieldSize)))
                    {
                        bitFieldSize++;
                    }
                    bool[] result = buffer.ReadBits(bitFieldSize);
                    byte offset = 0;
                    foreach (var b in result)
                    {
                        offset <<= 1;
                        offset += (byte)(b ? 1 : 0);
                    }
                    Value = baseNum + offset;
                }
                else if (Range == 256) //10.5.7: b)
                {
                    Value = baseNum + buffer.ReadByte();
                }
                else if (Range <= 256 * 256) //10.5.7: c)
                {
                    byte[] bytes = buffer.ReadBytes(2);
                    Value = baseNum + bytes[0] * 256 + bytes[1];
                }
                else //10.5.7: d)
                {
                    Asn1Integer len = new Asn1Integer();
                    len.Min = 1;
                    len.Max = 4;
                    len.PerDecode(buffer);
                    Debug.Assert(len.Value != null, "len.Value != null");
                    byte[] bytes = buffer.ReadBytes((int)len.Value);
                    Value = baseNum + NonNegativeBinaryIntegerPerDeocde(bytes);
                }
            }

            if (!VerifyConstraints())
            {
                throw new Asn1ConstraintsNotSatisfied(ExceptionMessages.ConstraintsNotSatisfied);
            }
        }
Пример #56
0
 /// <summary>
 /// Reads/decodes a length from the buffer.
 /// </summary>
 /// <param name="buffer">A buffer that stores a BER encoding result.</param>
 /// <param name="decodedLength">The decoded length will be retrieved by this param.</param>
 /// <returns>The number of the bytes consumed in the buffer to decode the length.</returns>
 /// <remarks>
 /// This method are implementd by Asn1StandardBerProcedure.LengthBerDecode.
 /// Override this method in a user-defined class only if the standard procedure is not applicable in some special scenarios.
 /// </remarks>
 protected virtual int LengthBerDecode(IAsn1DecodingBuffer buffer, out int decodedLength)
 {
     return Asn1StandardProcedure.LengthBerDecode(buffer, out decodedLength);
 }
 public override void PerDecode(IAsn1DecodingBuffer buffer, bool aligned = true)
 {
     base.PerDecode(buffer, aligned);
 }
Пример #58
0
 /// <summary>
 /// Decodes a tag from the buffer.
 /// </summary>
 /// <param name="buffer"></param>
 /// <param name="tag"></param>
 /// <returns>The number of the bytes consumed in the buffer to decode the tag.</returns>
 /// <remarks>Override this method in a user-defined class only if the procedure is not applicable in some special scenarios.</remarks>
 protected virtual int TagBerDecode(IAsn1DecodingBuffer buffer, out Asn1Tag tag)
 {
     return Asn1StandardProcedure.TagBerDecode(buffer, out tag);
 }
 /// <summary>
 /// Decodes the object by BER.
 /// </summary>
 /// <param name="buffer">A buffer that contains a BER encoding result.</param>
 /// <returns>The number of the bytes consumed in the buffer to decode this object.</returns>
 /// <exception cref="Asn1ConstraintsNotSatisfied">
 /// Thrown when the constraints are not satisfied after decoding.
 /// </exception>
 /// <exception cref="Asn1DecodingUnexpectedData">
 /// Thrown when the data in the buffer can not be properly decoded.
 /// </exception>
 /// <remarks>Override this method in a user-defined class only if the procedure is not applicable in some special scenarios.</remarks>
 public override int BerDecode(IAsn1DecodingBuffer buffer, bool explicitTag = true)
 {
     return 0;
 }
 /// <summary>
 /// Decodes a tag from the buffer.
 /// </summary>
 /// <param name="buffer"></param>
 /// <param name="tag"></param>
 /// <returns>The number of the bytes consumed in the buffer to decode the tag.</returns>
 public static int TagBerDecode(IAsn1DecodingBuffer buffer, out Asn1Tag tag)
 {
     byte prefix = buffer.ReadByte();
     int firstTwoBits = (prefix >> 6);
     Asn1TagType tagType = Asn1TagType.Private;
     long tagValue;
     switch (firstTwoBits)
     {
         case 0:
             {
                 tagType = Asn1TagType.Universal;
             } break;
         case 1:
             {
                 tagType = Asn1TagType.Application;
             } break;
         case 2:
             {
                 tagType = Asn1TagType.Context;
             } break;
         case 3:
             {
                 throw new NotImplementedException(ExceptionMessages.Unreachable);
             };
     }
     tagValue = prefix & ((byte)(1 << 5) - 1);
     if (tagValue <= 30)
     {
         tag = new Asn1Tag(tagType, tagValue);
         if ((prefix & (1 << 5)) != 0)
         {
             tag.EncodingWay = EncodingWay.Constructed;
         }
         else
         {
             tag.EncodingWay = EncodingWay.Primitive;
         }
         return 1;
     }
     else
     {
         throw new NotImplementedException("Case tag > 30 is not implemented. Check X.690: 8.1.2.4.3.");
     }
 }