public override int BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag = true)
        {
            int allLength = 0;

            if (referral != null)
            {
                allLength += referral.BerEncodeWithoutUnisersalTag(buffer);
                allLength += TagBerEncode(buffer,
                                          new Asn1Tag(Asn1TagType.Context, 3)
                {
                    EncodingWay = EncodingWay.Constructed
                });
            }

            allLength += errorMessage.BerEncode(buffer);
            allLength += matchedDN.BerEncode(buffer);
            allLength += resultCode.BerEncode(buffer);
            allLength += LengthBerEncode(buffer, allLength);
            allLength += TagBerEncode(buffer,
                                      new Asn1Tag(Asn1TagType.Universal, Asn1TagValue.Sequence)
            {
                EncodingWay = EncodingWay.Constructed
            });

            return(allLength);
        }
        /// <summary>
        /// Encodes the data of this object to the buffer.
        /// </summary>
        /// <param name="buffer">A buffer to which the encoding result will be written.</param>
        /// <returns>The length of the encoding result of the data.</returns>
        /// <remarks>
        /// For the TLV (Tag, Length, Value) triple in the encoding result,
        /// this method provides the functionality of encoding Value.
        /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
        /// </remarks>
        protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
        {
            var res = EncodeIdentifier(Value);

            buffer.WriteBytes(res);
            return(res.Length);
        }
 /// <summary>
 /// Encodes the data of this object to the buffer.
 /// </summary>
 /// <param name="buffer">A buffer to which the encoding result will be written.</param>
 /// <returns>The length of the encoding result of the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result,
 /// this method provides the functionality of encoding Value.
 /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
 /// </remarks>
 protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
 {
     //Ref. X.680: 8.6.2.2
     buffer.WriteBytes(ByteArrayValue);
     buffer.WriteByte(unusedBitsInLastByte);
     return(ByteArrayValue.Length + 1);
 }
        /// <summary>
        /// Encodes the object by BER.
        /// </summary>
        /// <param name="buffer">A buffer that stores the 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 length of the encoding result of this object.</returns>
        /// <exception cref="Asn1ConstraintsNotSatisfied">
        /// Thrown when the constraints are not satisfied before encoding.
        /// </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 BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag = true)
        {
            if (!VerifyConstraints())
            {
                throw new Asn1ConstraintsNotSatisfied(ExceptionMessages.ConstraintsNotSatisfied
                                                      + " Encode " + this.GetType().Name + ".");
            }

            int resultLen = 0;

            resultLen += requestValue.BerEncodeWithoutUnisersalTag(buffer);
            Asn1Tag valueTag = new Asn1Tag(Asn1TagType.Context, 1)
            {
                EncodingWay = EncodingWay.Primitive
            };

            resultLen += TagBerEncode(buffer, valueTag);

            resultLen += requestName.BerEncodeWithoutUnisersalTag(buffer);
            Asn1Tag nameTag = new Asn1Tag(Asn1TagType.Context, 0)
            {
                EncodingWay = EncodingWay.Primitive
            };

            resultLen += TagBerEncode(buffer, nameTag);

            resultLen += LengthBerEncode(buffer, resultLen);
            //Add the encoding result of the top most tag (in most cases it's Application Class Tag) to the front of buffer if it is defined.
            resultLen += TagBerEncode(buffer, this.TopTag);
            return(resultLen);
        }
 /// <summary>
 /// Encodes the data of this object to the buffer.
 /// </summary>
 /// <param name="buffer">A buffer to which the encoding result will be written.</param>
 /// <returns>The length of the encoding result of the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result,
 /// this method provides the functionality of encoding Value.
 /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
 /// </remarks>
 protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
 {
     //TODO: Check X.690: 11.7 for more encoding details.
     //Currently this works good only if there is no fractional portions in Value.
     //In kerberos test suite, the time shall not include any fractional portions of the seconds. Ref. RFC4120: 5.2.3.
     return(base.ValueBerEncode(buffer));
 }
        /// <summary>
        /// Encodes the object by BER.
        /// </summary>
        /// <param name="buffer">A buffer that stores the 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 length of the encoding result of this object.</returns>
        /// <exception cref="Asn1ConstraintsNotSatisfied">
        /// Thrown when the constraints are not satisfied before encoding.
        /// </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 BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag = true)
        {
            if (!VerifyConstraints())
            {
                throw new Asn1ConstraintsNotSatisfied(ExceptionMessages.ConstraintsNotSatisfied
                                                      + " Encode " + this.GetType().Name + ".");
            }

            // Encode newSuperior
            int resultLen = 0;

            if (newSuperior != null)
            {
                resultLen  = newSuperior.BerEncodeWithoutUnisersalTag(buffer);
                resultLen += TagBerEncode(buffer, new Asn1Tag(Asn1TagType.Context, 0)
                {
                    EncodingWay = EncodingWay.Primitive
                });
            }

            // Encode deleteoldrdn
            resultLen += deleteoldrdn.BerEncode(buffer, true);

            // Encode newrdn
            resultLen += newrdn.BerEncode(buffer, true);

            // Encode entry
            resultLen += entry.BerEncode(buffer, true);

            // Encode Length
            resultLen += LengthBerEncode(buffer, resultLen);

            resultLen += TagBerEncode(buffer, this.TopTag);
            return(resultLen);
        }
        public override int BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag = true)
        {
            int allLength = 0;

            if (controls != null)
            {
                allLength += controls.BerEncode(buffer, false);
                allLength += LengthBerEncode(buffer, allLength);
                allLength += TagBerEncode(buffer,
                                          new Asn1Tag(Asn1TagType.Context, 0)
                {
                    EncodingWay = EncodingWay.Constructed
                });
            }

            allLength += protocolOp.BerEncode(buffer, true);
            allLength += messageID.BerEncode(buffer, true);

            if (explicitTag)
            {
                allLength += LengthBerEncode(buffer, allLength);
                allLength += TagBerEncode(buffer,
                                          new Asn1Tag(Asn1TagType.Universal, Asn1TagValue.Sequence)
                {
                    EncodingWay = EncodingWay.Constructed
                });
            }

            return(allLength);
        }
 /// <summary>
 /// Encodes the data of this object to the buffer.
 /// </summary>
 /// <param name="buffer">A buffer to which the encoding result will be written.</param>
 /// <returns>The length of the encoding result of the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result,
 /// this method provides the functionality of encoding Value.
 /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
 /// </remarks>
 protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
 {
     //TODO: Check X.690: 11.7 for more encoding details.
     //Currently this works good only if there is no fractional portions in Value.
     //In kerberos test suite, the time shall not include any fractional portions of the seconds. Ref. RFC4120: 5.2.3.
     return base.ValueBerEncode(buffer);
 }
        /// <summary>
        /// Encodes the object by BER.
        /// </summary>
        /// <param name="buffer">A buffer that stores the 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 length of the encoding result of this object.</returns>
        /// <exception cref="Asn1ConstraintsNotSatisfied">
        /// Thrown when the constraints are not satisfied before encoding.
        /// </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 BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag = true)
        {
            //TODO: deal with explicitTag
            if (!VerifyConstraints())
            {
                throw new Asn1ConstraintsNotSatisfied(ExceptionMessages.ConstraintsNotSatisfied
                                                      + " Encode " + this.GetType().Name + ".");
            }

            //Encoding inversely since buffer is reversed.

            //Add the encoding result of Value to the front of buffer.
            int resultLen = ValueBerEncode(buffer);

            //Add the encoding result of Length to the front of buffer.
            resultLen += LengthBerEncode(buffer, resultLen);

            //Add the encoding result of Universal Class Tag to the front of buffer.
            Asn1Tag uniTag = this.UniversalTag;

            resultLen += TagBerEncode(buffer, uniTag);

            //Add the encoding result of the top most tag (in most cases it's Application Class Tag) to the front of buffer if it is defined.
            Asn1Tag topTag = this.TopTag;

            if (topTag.TagType != Asn1TagType.Universal)
            {
                resultLen += LengthBerEncode(buffer, resultLen);
                resultLen += TagBerEncode(buffer, topTag);
            }
            return(resultLen);
        }
예제 #10
0
        /// <summary>
        /// Encodes the object by BER.
        /// </summary>
        /// <param name="buffer">A buffer that stores the 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 length of the encoding result of this object.</returns>
        /// <exception cref="Asn1EmptyDataException">
        /// Thrown when trying to encode an undefined data.
        /// </exception>
        public override int BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag = true)
        {
            if (SelectedChoice == UndefinedIndex)
            {
                throw new Asn1EmptyDataException(ExceptionMessages.EmptyData);
            }

            int length = GetData().BerEncode(buffer, explicitTag);

            Asn1Tag contextTag = metaDatas[(int)choiceIndexInFieldsMemberInfo].AttachedTag;

            if (contextTag != null)
            {
                length += LengthBerEncode(buffer, length);
                length += TagBerEncode(buffer, contextTag);
            }

            Object[] attrs = GetType().GetCustomAttributes(typeof(Asn1Tag), true);
            if (attrs.Length != 0)
            {
                Asn1Tag tag = attrs[0] as Asn1Tag;
                length += LengthBerEncode(buffer, length);
                length += TagBerEncode(buffer, tag);
            }
            return(length);
        }
예제 #11
0
        /// <summary>
        /// Encodes the object by BER.
        /// </summary>
        /// <param name="buffer">A buffer that stores the 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 length of the encoding result of this object.</returns>
        /// <exception cref="Asn1EmptyDataException">
        /// Thrown when trying to encode an undefined data.
        /// </exception>
        public override int BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag = true)
        {
            //TODO: deal with explicitTag
            if (currentChoice == undefinedIndex)
            {
                throw new Asn1EmptyDataException(ExceptionMessages.EmptyData);
            }

            int length = GetData().BerEncode(buffer);

            if (attachedTags[(int)choiceIndexInFieldsMemberInfo] != null)
            {
                length += LengthBerEncode(buffer, length);
                length += TagBerEncode(buffer, attachedTags[(int)choiceIndexInFieldsMemberInfo]);
            }

            Object[] attrs = this.GetType().GetCustomAttributes(typeof(Asn1Tag), true);
            if (attrs.Length != 0)
            {
                Asn1Tag tag = attrs[0] as Asn1Tag;
                length += LengthBerEncode(buffer, length);
                length += TagBerEncode(buffer, tag);
            }
            return(length);
        }
 public override int BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag)
 {
     int len = base.BerEncode(buffer);
     len += LengthBerEncode(buffer, len);
     len += TagBerEncode(buffer, new Asn1Tag(Asn1TagType.Universal,
         Asn1TagValue.Sequence) { EncodingWay = EncodingWay.Constructed });
     return len;
 }
예제 #13
0
        public override int BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag = true)
        {
            int allLen = 0;

            allLen += BerEncodeWithoutUnisersalTag(buffer);
            allLen += TagBerEncode(buffer, new Asn1Tag(Asn1TagType.Application, 0));
            return(allLen);
        }
예제 #14
0
        public override int BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag = true)
        {
            int     allLen = 0;
            Asn1Tag contextTag;

            switch (SelectedChoice)
            {
            case 1:
                allLen    += field0.BerEncodeWithoutUnisersalTag(buffer);
                contextTag = new Asn1Tag(Asn1TagType.Context, 0)
                {
                    EncodingWay = EncodingWay.Primitive
                };
                allLen += TagBerEncode(buffer, contextTag);
                break;

            case 2:
                allLen    += field1.BerEncodeWithoutUnisersalTag(buffer);
                contextTag = new Asn1Tag(Asn1TagType.Context, 3)
                {
                    EncodingWay = EncodingWay.Constructed
                };
                allLen += TagBerEncode(buffer, contextTag);
                break;

            case 3:
                allLen    += field2.BerEncodeWithoutUnisersalTag(buffer);
                contextTag = new Asn1Tag(Asn1TagType.Context, 9)
                {
                    EncodingWay = EncodingWay.Primitive
                };
                allLen += TagBerEncode(buffer, contextTag);
                break;

            case 4:
                allLen    += field3.BerEncodeWithoutUnisersalTag(buffer);
                contextTag = new Asn1Tag(Asn1TagType.Context, 10)
                {
                    EncodingWay = EncodingWay.Primitive
                };
                allLen += TagBerEncode(buffer, contextTag);
                break;

            case 5:
                allLen    += field4.BerEncodeWithoutUnisersalTag(buffer);
                contextTag = new Asn1Tag(Asn1TagType.Context, 11)
                {
                    EncodingWay = EncodingWay.Primitive
                };
                allLen += TagBerEncode(buffer, contextTag);
                break;

            default:
                throw new Asn1ConstraintsNotSatisfied(ExceptionMessages.InvalidChoiceIndex + " AuthenticationChoice");
            }
            return(allLen);
        }
        public override int BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag = true)
        {
            int length = ValueBerEncode(buffer);

            length += LengthBerEncode(buffer, length);
            buffer.WriteByte(101);
            buffer.WriteByte(127);
            return(length + 2);
        }
예제 #16
0
        //Constraint is not needed for primitive type UniversalString.
        //Therefore no need to override VerifyConstraints method.

        #region BER

        /// <summary>
        /// Encodes the data of this object to the buffer.
        /// </summary>
        /// <param name="buffer">A buffer to which the encoding result will be written.</param>
        /// <returns>The length of the encoding result of the data.</returns>
        /// <remarks>
        /// For the TLV (Tag, Length, Value) triple in the encoding result,
        /// this method provides the functionality of encoding Value.
        /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
        /// </remarks>
        protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
        {
            if (this.Value == null)
            {
                throw new Asn1EmptyDataException(ExceptionMessages.EmptyData);
            }
            byte[] result = Encoding.BigEndianUnicode.GetBytes(this.Value);
            buffer.WriteBytes(result);
            return(result.Length);
        }
 /// <summary>
 /// Encodes the data of this object to the buffer.
 /// </summary>
 /// <param name="buffer">A buffer to which the encoding result will be written.</param>
 /// <returns>The length of the encoding result of the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result,
 /// this method provides the functionality of encoding Value.
 /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
 /// </remarks>
 protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
 {
     if (this.Value == null)
     {
         throw new Asn1EmptyDataException(ExceptionMessages.EmptyData);
     }
     byte[] result = UniversalStringEncode(this.Value);
     buffer.WriteBytes(result);
     return result.Length;
 }
        /// <summary>
        /// Encodes the object by BER.
        /// </summary>
        /// <param name="buffer">A buffer that stores the 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 length of the encoding result of this object.</returns>
        /// <exception cref="Asn1ConstraintsNotSatisfied">
        /// Thrown when the constraints are not satisfied before encoding.
        /// </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 BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag = true)
        {
            int resultLen = 0;

            //Add the encoding result of Length to the front of buffer.
            resultLen += LengthBerEncode(buffer, 0);

            //Add the encoding result of the top most tag (in most cases it's Application Class Tag) to the front of buffer if it is defined.

            resultLen += TagBerEncode(buffer, this.TopTag);
            return(resultLen);
        }
        public override int BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag)
        {
            int len = base.BerEncode(buffer);

            len += LengthBerEncode(buffer, len);
            len += TagBerEncode(buffer, new Asn1Tag(Asn1TagType.Universal,
                                                    Asn1TagValue.Sequence)
            {
                EncodingWay = EncodingWay.Constructed
            });
            return(len);
        }
예제 #20
0
        /// <summary>
        /// Encodes the object by BER.
        /// </summary>
        /// <param name="buffer">A buffer that stores the 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 length of the encoding result of this object.</returns>
        /// <exception cref="Asn1ConstraintsNotSatisfied">
        /// Thrown when the constraints are not satisfied before encoding.
        /// </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 BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag = true)
        {
            if (!VerifyConstraints())
            {
                throw new Asn1ConstraintsNotSatisfied(ExceptionMessages.ConstraintsNotSatisfied
                                                      + " Encode " + this.GetType().Name + ".");
            }
            Asn1Tag valueTag;
            int     resultLen = 0;

            resultLen += dnAttributes.BerEncodeWithoutUnisersalTag(buffer);
            valueTag   = new Asn1Tag(Asn1TagType.Context, 4)
            {
                EncodingWay = EncodingWay.Primitive
            };
            resultLen += TagBerEncode(buffer, valueTag);

            resultLen += matchValue.BerEncodeWithoutUnisersalTag(buffer);
            valueTag   = new Asn1Tag(Asn1TagType.Context, 3)
            {
                EncodingWay = EncodingWay.Primitive
            };
            resultLen += TagBerEncode(buffer, valueTag);

            if (type != null)
            {
                resultLen += type.BerEncodeWithoutUnisersalTag(buffer);
                valueTag   = new Asn1Tag(Asn1TagType.Context, 2)
                {
                    EncodingWay = EncodingWay.Primitive
                };
                resultLen += TagBerEncode(buffer, valueTag);
            }

            if (matchingRule != null)
            {
                resultLen += matchingRule.BerEncodeWithoutUnisersalTag(buffer);
                valueTag   = new Asn1Tag(Asn1TagType.Context, 1)
                {
                    EncodingWay = EncodingWay.Primitive
                };
                resultLen += TagBerEncode(buffer, valueTag);
            }

            if (explicitTag)
            {
                resultLen += LengthBerEncode(buffer, resultLen);
                //Add the encoding result of the top most tag (in most cases it's Application Class Tag) to the front of buffer if it is defined.
                resultLen += TagBerEncode(buffer, this.TopTag);
            }

            return(resultLen);
        }
예제 #21
0
        /// <summary>
        /// Encodes the data of this object to the buffer.
        /// </summary>
        /// <param name="buffer">A buffer to which the encoding result will be written.</param>
        /// <returns>The length of the encoding result of the data.</returns>
        /// <remarks>
        /// For the TLV (Tag, Length, Value) triple in the encoding result,
        /// this method provides the functionality of encoding Value.
        /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
        /// </remarks>
        protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
        {
            //Encode data
            if (Value == false)
            {
                buffer.WriteByte(0);
            }
            else
            {
                buffer.WriteByte(255);
            }

            return(1);
        }
예제 #22
0
        /// <summary>
        /// Encodes the data of this object to the buffer.
        /// </summary>
        /// <param name="buffer">A buffer to which the encoding result will be written.</param>
        /// <returns>The length of the encoding result of the data.</returns>
        /// <remarks>
        /// For the TLV (Tag, Length, Value) triple in the encoding result,
        /// this method provides the functionality of encoding Value.
        /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
        /// </remarks>
        protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
        {
            if (Elements == null)
            {
                throw new Asn1EmptyDataException(ExceptionMessages.EmptyData);
            }
            int resultLen = 0;

            //Encode inversely
            for (int i = Elements.Length - 1; i >= 0; i--)
            {
                resultLen += Elements[i].BerEncode(buffer);
            }
            return(resultLen);
        }
        /// <summary>
        /// Encodes a tag to the buffer.
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="tag"></param>
        /// <returns>The length of the encoding result.</returns>
        public static int TagBerEncode(IAsn1BerEncodingBuffer buffer, Asn1Tag tag)
        {
            //Ref. X.690: 8.1.2.2, there are four kinds of tags
            byte prefix = 0;

            switch (tag.TagType)
            {
            case Asn1TagType.Universal:
            {
                prefix = 0;        //00
            } break;

            case Asn1TagType.Application:
            {
                prefix = 1;        //01
            } break;

            case Asn1TagType.Context:
            {
                prefix = 2;        //10
            } break;

            case Asn1TagType.Private:
            {
                prefix = 3;        //11
                //Ref. X.680: G.2.12.4
                throw new Asn1UnreachableExcpetion(ExceptionMessages.Unreachable);
            };
            }
            prefix <<= 6;
            if (tag.EncodingWay == EncodingWay.Constructed)
            {
                prefix |= (1 << 5); //Set the sixth bit to 1 if it is encoded in constructed way. Ref. X.690: 8.1.2.3
            }
            if (tag.TagValue <= 30) //Use one byte to store the encoding result
            //Ref. X.690: 8.1.2.3
            {
                prefix |= (byte)tag.TagValue;
                buffer.WriteByte(prefix);
                return(1);
            }
            else//Use more than one bytes to store.
            {
                //Ref. X.690: 8.1.2.4.3
                throw new NotImplementedException("Case tag > 30 is not implemented. Check X.690: 8.1.2.4.3.");
            }
        }
        /// <summary>
        /// Encodes a length to the buffer.
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="length">The length to be written to the buffer.</param>
        /// <returns>The length of "the encoded length's encoding result".</returns>
        public static int LengthBerEncode(IAsn1BerEncodingBuffer buffer, int length)
        {
            if (length < 0)
            {
                throw new Asn1InvalidArgument(ExceptionMessages.LengthNegative);
            }
            if (length < 128)
            {
                buffer.WriteByte((byte)length);
                return 1;
            }
            else
            {
                //The encoding result of a length that no less than 128 will be stored in at least two bytes.
                //The first byte:  1xxxxxxx, xxxxxxx is the length of the following encoding result.
                //The next bytes:  the length's encoding result, big endian.
                //Ref. X.690: 8.1.3
                byte[] bytes = BitConverter.GetBytes(length);
                //ensure bytes is big endian
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(bytes);
                }

                //delete the redundant zeros
                int zeroNum = 0;
                for (int i = 0; i < bytes.Length; i++)
                {
                    if (bytes[i] == 0)
                    {
                        zeroNum++;
                    }
                    else
                    {
                        break;
                    }
                }
                //zeroNum won't be equal to bytes.Length since length>=128.

                //Encode data first, then encode length of the data's encoding result since buffer is reversed.
                buffer.WriteBytes(bytes, zeroNum, bytes.Length - zeroNum);
                buffer.WriteByte((byte)((bytes.Length - zeroNum) | (1 << 7)));//set the first bit to 1
                return 1 + bytes.Length - zeroNum;
            }
        }
        /// <summary>
        /// Encodes a length to the buffer.
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="length">The length to be written to the buffer.</param>
        /// <returns>The length of "the encoded length's encoding result".</returns>
        public static int LengthBerEncode(IAsn1BerEncodingBuffer buffer, int length)
        {
            if (length < 0)
            {
                throw new Asn1InvalidArgument(ExceptionMessages.LengthNegative);
            }
            if (length < 128)
            {
                buffer.WriteByte((byte)length);
                return(1);
            }
            else
            {
                //The encoding result of a length that no less than 128 will be stored in at least two bytes.
                //The first byte:  1xxxxxxx, xxxxxxx is the length of the following encoding result.
                //The next bytes:  the length's encoding result, big endian.
                //Ref. X.690: 8.1.3
                byte[] bytes = BitConverter.GetBytes(length);
                //ensure bytes is big endian
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(bytes);
                }

                //delete the redundant zeros
                int zeroNum = 0;
                for (int i = 0; i < bytes.Length; i++)
                {
                    if (bytes[i] == 0)
                    {
                        zeroNum++;
                    }
                    else
                    {
                        break;
                    }
                }
                //zeroNum won't be equal to bytes.Length since length>=128.

                //Encode data first, then encode length of the data's encoding result since buffer is reversed.
                buffer.WriteBytes(bytes, zeroNum, bytes.Length - zeroNum);
                buffer.WriteByte((byte)((bytes.Length - zeroNum) | (1 << 7)));//set the first bit to 1
                return(1 + bytes.Length - zeroNum);
            }
        }
예제 #26
0
        /// <summary>
        /// Encodes only the data and length of the data.
        /// </summary>
        /// <param name="buffer">A buffer that stores the BER encoding result.</param>
        public int BerEncodeWithoutUnisersalTag(IAsn1BerEncodingBuffer buffer)
        {
            if (!VerifyConstraints())
            {
                throw new Asn1ConstraintsNotSatisfied(ExceptionMessages.ConstraintsNotSatisfied
                                                      + " Encode " + this.GetType().Name + ".");
            }

            //Encoding inversely since buffer is reversed.

            //Add the encoding result of Value to the front of buffer.
            int resultLen = ValueBerEncode(buffer);

            //Add the encoding result of Length to the front of buffer.
            resultLen += LengthBerEncode(buffer, resultLen);

            return(resultLen);
        }
        /// <summary>
        /// Encodes the data of this object to the buffer.
        /// </summary>
        /// <param name="buffer">A buffer to which the encoding result will be written.</param>
        /// <returns>The length of the encoding result of the data.</returns>
        /// <remarks>
        /// For the TLV (Tag, Length, Value) triple in the encoding result,
        /// this method provides the functionality of encoding Value.
        /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
        /// </remarks>
        protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
        {
            int resultLen = 0;

            //Encode inversely
            Asn1Object[] allFields = Fields;
            for (int i = fieldsMetaData.Length - 1; i >= 0; i--)
            {
                if (!(fieldsMetaData[i].Optional && allFields[i] == null))
                {
                    //The field is not optional or its optional but it exists
                    int curFieldLen = allFields[i].BerEncode(buffer);
                    resultLen += curFieldLen;
                    //Encode the context tag if the field has one
                    if (fieldsMetaData[i].AttachedTag != null)
                    {
                        resultLen += LengthBerEncode(buffer, curFieldLen);
                        resultLen += TagBerEncode(buffer, fieldsMetaData[i].AttachedTag);
                    }
                }
            }
            return(resultLen);
        }
        /// <summary>
        /// Encodes the object by BER.
        /// </summary>
        /// <param name="buffer">A buffer that stores the 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 length of the encoding result of this object.</returns>
        /// <exception cref="Asn1ConstraintsNotSatisfied">
        /// Thrown when the constraints are not satisfied before encoding.
        /// </exception>
        /// <remarks>This method is overrode because Universal Class Tag is not needed in the encoding result.</remarks>
        public override int BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag = true)
        {
            //TODO: deal with explicitTag
            if (!VerifyConstraints())
            {
                throw new Asn1ConstraintsNotSatisfied(ExceptionMessages.ConstraintsNotSatisfied
                    + " Encode " + this.GetType().Name + ".");
            }

            //Encoding inversely since buffer is reversed.

            //Add the encoding result of Value to the front of buffer.
            int resultLen = ValueBerEncode(buffer);

            //No Universal Tag.
            //TODO: Need to check the original definition.

            //Add the encoding result of the top most tag (in most cases it's Application Class Tag) to the front of buffer if it is defined.
            Asn1Tag topTag = this.TopTag;
            resultLen += LengthBerEncode(buffer, resultLen);
            resultLen += TagBerEncode(buffer, topTag);
            return resultLen;
        }
예제 #29
0
        public override int BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag = true)
        {
            int allLength = 0;

            if (serverSaslCreds != null)
            {
                allLength += serverSaslCreds.BerEncodeWithoutUnisersalTag(buffer);
                allLength += TagBerEncode(buffer,
                                          new Asn1Tag(Asn1TagType.Context, 7)
                {
                    EncodingWay = EncodingWay.Primitive
                });
            }

            if (referral != null)
            {
                allLength += referral.BerEncodeWithoutUnisersalTag(buffer);
                allLength += TagBerEncode(buffer,
                                          new Asn1Tag(Asn1TagType.Context, 3)
                {
                    EncodingWay = EncodingWay.Constructed
                });
            }

            allLength += errorMessage.BerEncode(buffer);
            allLength += matchedDN.BerEncode(buffer);
            allLength += resultCode.BerEncode(buffer);
            allLength += LengthBerEncode(buffer, allLength);
            allLength += TagBerEncode(buffer,
                                      new Asn1Tag(Asn1TagType.Application, 1)
            {
                EncodingWay = EncodingWay.Constructed
            });

            return(allLength);
        }
 /// <summary>
 /// Encodes a tag to the buffer.
 /// </summary>
 /// <param name="buffer"></param>
 /// <param name="tag"></param>
 /// <returns>The length of the encoding result.</returns>
 public static int TagBerEncode(IAsn1BerEncodingBuffer buffer, Asn1Tag tag)
 {
     //Ref. X.690: 8.1.2.2, there are four kinds of tags
     byte prefix = 0;
     switch (tag.TagType)
     {
         case Asn1TagType.Universal:
             {
                 prefix = 0;//00
             } break;
         case Asn1TagType.Application:
             {
                 prefix = 1;//01
             } break;
         case Asn1TagType.Context:
             {
                 prefix = 2;//10
             } break;
         case Asn1TagType.Private:
             {
                 prefix = 3;//11
                 //Ref. X.680: G.2.12.4
                 throw new Asn1UnreachableExcpetion(ExceptionMessages.Unreachable);
             };
     }
     prefix <<= 6;
     if (tag.EncodingWay == EncodingWay.Constructed)
     {
         prefix |= (1 << 5);//Set the sixth bit to 1 if it is encoded in constructed way. Ref. X.690: 8.1.2.3
     }
     if (tag.TagValue <= 30)//Use one byte to store the encoding result
     //Ref. X.690: 8.1.2.3
     {
         prefix |= (byte)tag.TagValue;
         buffer.WriteByte(prefix);
         return 1;
     }
     else//Use more than one bytes to store.
     {
         //Ref. X.690: 8.1.2.4.3
         throw new NotImplementedException("Case tag > 30 is not implemented. Check X.690: 8.1.2.4.3.");
     }
 }
        public override int BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag = true)
        {
            int allLength = 0;

            if(serverSaslCreds != null)
            {
                allLength += serverSaslCreds.BerEncodeWithoutUnisersalTag(buffer);
                allLength += TagBerEncode(buffer,
                    new Asn1Tag(Asn1TagType.Context, 7) { EncodingWay = EncodingWay.Primitive });
            }

            if (referral != null)
            {
                allLength += referral.BerEncodeWithoutUnisersalTag(buffer);
                allLength += TagBerEncode(buffer,
                    new Asn1Tag(Asn1TagType.Context, 3) { EncodingWay = EncodingWay.Constructed });
            }

            allLength += errorMessage.BerEncode(buffer);
            allLength += matchedDN.BerEncode(buffer);
            allLength += resultCode.BerEncode(buffer);
            allLength += LengthBerEncode(buffer, allLength);
            allLength += TagBerEncode(buffer,
                new Asn1Tag(Asn1TagType.Application, 1) { EncodingWay = EncodingWay.Constructed });

            return allLength;
        }
 /// <summary>
 /// Encodes the data of this object to the buffer.
 /// </summary>
 /// <param name="buffer">A buffer to which the encoding result will be written.</param>
 /// <returns>The length of the encoding result of the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result,
 /// this method provides the functionality of encoding Value.
 /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
 /// </remarks>
 protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
 {
     buffer.WriteBytes(ByteArrayValue);
     return ByteArrayValue == null ? 0 : ByteArrayValue.Length;
 }
        public override int BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag = true)
        {
            int allLength = 0;

            if (controls != null)
            {
                allLength += controls.BerEncodeWithoutUnisersalTag(buffer);
                allLength += TagBerEncode(buffer,
                    new Asn1Tag(Asn1TagType.Context, 0) { EncodingWay = EncodingWay.Constructed });
            }

            allLength += protocolOp.BerEncode(buffer);
            allLength += messageID.BerEncode(buffer);
            allLength += LengthBerEncode(buffer, allLength);
            allLength += TagBerEncode(buffer,
                new Asn1Tag(Asn1TagType.Universal, Asn1TagValue.Sequence) { EncodingWay = EncodingWay.Constructed });

            return allLength;
        }
 /// <summary>
 /// Encodes the data of this object to the buffer.
 /// </summary>
 /// <param name="buffer">A buffer to which the encoding result will be written.</param>
 /// <returns>The length of the encoding result of the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result,
 /// this method provides the functionality of encoding Value.
 /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
 /// </remarks>
 protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
 {
     //Ref. X.680: 8.6.2.2
     buffer.WriteBytes(ByteArrayValue);
     buffer.WriteByte(unusedBitsInLastByte);
     return ByteArrayValue.Length + 1;
 }
예제 #35
0
 /// <summary>
 /// Encodes a tag to the buffer.
 /// </summary>
 /// <param name="buffer"></param>
 /// <param name="tag"></param>
 /// <returns>The length of the encoding result.</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 TagBerEncode(IAsn1BerEncodingBuffer buffer, Asn1Tag tag)
 {
     return Asn1StandardProcedure.TagBerEncode(buffer, tag);
 }
 /// <summary>
 /// Encodes the data of this object to the buffer.
 /// </summary>
 /// <param name="buffer">A buffer to which the encoding result will be written.</param>
 /// <returns>The length of the encoding result of the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result,
 /// this method provides the functionality of encoding Value.
 /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
 /// </remarks>
 protected virtual int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
 {
     throw new NotImplementedException("ValueBerEncode should be implemented to support BER Encode.");
 }
 /// <summary>
 /// Encodes a tag to the buffer.
 /// </summary>
 /// <param name="buffer"></param>
 /// <param name="tag"></param>
 /// <returns>The length of the encoding result.</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 TagBerEncode(IAsn1BerEncodingBuffer buffer, Asn1Tag tag)
 {
     return(Asn1StandardProcedure.TagBerEncode(buffer, tag));
 }
 /// <summary>
 /// Encodes a length to the buffer.
 /// </summary>
 /// <param name="buffer"></param>
 /// <param name="length">The length to be written to the buffer.</param>
 /// <returns>The length of "the encoded length's encoding result".</returns>
 /// <remarks>
 /// This method are implementd by Asn1StandardBerProcedure.LengthBerEncode.
 /// Override this method in a user-defined class only if the standard procedure is not applicable in some special scenarios.
 /// </remarks>
 protected virtual int LengthBerEncode(IAsn1BerEncodingBuffer buffer, int length)
 {
     return(Asn1StandardProcedure.LengthBerEncode(buffer, length));
 }
 /// <summary>
 /// Encodes the object by BER.
 /// </summary>
 /// <param name="buffer">A buffer that stores the 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 length of the encoding result of this object.</returns>
 /// <exception cref="Asn1ConstraintsNotSatisfied">
 /// Thrown when the constraints are not satisfied before encoding.
 /// </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 BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag = true)
 {
     return 0;
 }
예제 #40
0
        public override int BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag = true)
        {
            int allLength = 0;

            if (referral != null)
            {
                allLength += referral.BerEncodeWithoutUnisersalTag(buffer);
                allLength += TagBerEncode(buffer,
                    new Asn1Tag(Asn1TagType.Context, 3) { EncodingWay = EncodingWay.Constructed });
            }

            allLength += errorMessage.BerEncode(buffer);
            allLength += matchedDN.BerEncode(buffer);
            allLength += resultCode.BerEncode(buffer);
            allLength += LengthBerEncode(buffer, allLength);
            allLength += TagBerEncode(buffer,
                new Asn1Tag(Asn1TagType.Universal, Asn1TagValue.Sequence) { EncodingWay = EncodingWay.Constructed });

            return allLength;
        }
예제 #41
0
        /// <summary>
        /// Encodes only the data and length of the data.
        /// </summary>
        /// <param name="buffer">A buffer that stores the BER encoding result.</param>
        public int BerEncodeWithoutUnisersalTag(IAsn1BerEncodingBuffer buffer)
        {
            if (!VerifyConstraints())
            {
                throw new Asn1ConstraintsNotSatisfied(ExceptionMessages.ConstraintsNotSatisfied
                    + " Encode " + this.GetType().Name + ".");
            }

            //Encoding inversely since buffer is reversed.

            //Add the encoding result of Value to the front of buffer.
            int resultLen = ValueBerEncode(buffer);

            //Add the encoding result of Length to the front of buffer.
            resultLen += LengthBerEncode(buffer, resultLen);

            return resultLen;
        }
예제 #42
0
 /// <summary>
 /// Encodes a length to the buffer.
 /// </summary>
 /// <param name="buffer"></param>
 /// <param name="length">The length to be written to the buffer.</param>
 /// <returns>The length of "the encoded length's encoding result".</returns>
 /// <remarks>
 /// This method are implementd by Asn1StandardBerProcedure.LengthBerEncode.
 /// Override this method in a user-defined class only if the standard procedure is not applicable in some special scenarios.
 /// </remarks>
 protected virtual int LengthBerEncode(IAsn1BerEncodingBuffer buffer, int length)
 {
     return Asn1StandardProcedure.LengthBerEncode(buffer, length);
 }
        /// <summary>
        /// Encodes the object by BER.
        /// </summary>
        /// <param name="buffer">A buffer that stores the 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 length of the encoding result of this object.</returns>
        /// <exception cref="Asn1ConstraintsNotSatisfied">
        /// Thrown when the constraints are not satisfied before encoding.
        /// </exception>
        /// <remarks>This method is overrode because Universal Class Tag is not needed in the encoding result.</remarks>
        public override int BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag = true)
        {
            //TODO: deal with explicitTag
            if (!VerifyConstraints())
            {
                throw new Asn1ConstraintsNotSatisfied(ExceptionMessages.ConstraintsNotSatisfied
                    + " Encode " + this.GetType().Name + ".");
            }

            //Encoding inversely since buffer is reversed.

            //Add the encoding result of Value to the front of buffer.
            int resultLen = ValueBerEncode(buffer);

            //No Universal Tags.
            //TODO: Need to check the original definition.

            //Add the encoding result of the top most tag (in most cases it's Application Class Tag) to the front of buffer if it is defined.
            Asn1Tag topTag = this.TopTag;
            resultLen += LengthBerEncode(buffer, resultLen);
            resultLen += TagBerEncode(buffer, topTag);
            return resultLen;
        }
예제 #44
0
 /// <summary>
 /// Encodes the data of this object to the buffer.
 /// </summary>
 /// <param name="buffer">A buffer to which the encoding result will be written.</param>
 /// <returns>The length of the encoding result of the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result,
 /// this method provides the functionality of encoding Value.
 /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
 /// </remarks>
 protected virtual int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
 {
     throw new NotImplementedException("ValueBerEncode should be implemented to support BER Encode.");
 }
 public override int BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag = true)
 {
     int length = ValueBerEncode(buffer);
     length += LengthBerEncode(buffer, length);
     buffer.WriteByte(101);
     buffer.WriteByte(127);
     return length + 2;
 }
 public override int BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag = true)
 {
     int allLen = 0;
     allLen += BerEncodeWithoutUnisersalTag(buffer);
     allLen += TagBerEncode(buffer, new Asn1Tag(Asn1TagType.Application, 0));
     return allLen;
 }
        /// <summary>
        /// Encodes the data of this object to the buffer.
        /// </summary>
        /// <param name="buffer">A buffer to which the encoding result will be written.</param>
        /// <returns>The length of the encoding result of the data.</returns>
        /// <remarks>
        /// For the TLV (Tag, Length, Value) triple in the encoding result,
        /// this method provides the functionality of encoding Value.
        /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
        /// </remarks>
        protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
        {
            //Encode data
            if (Value == false)
            {
                buffer.WriteByte(0);
            }
            else
            {
                buffer.WriteByte(255);
            }

            return 1;
        }
        /// <summary>
        /// Encodes the data of this object to the buffer.
        /// </summary>
        /// <param name="buffer">A buffer to which the encoding result will be written.</param>
        /// <returns>The length of the encoding result of the data.</returns>
        /// <remarks>
        /// For the TLV (Tag, Length, Value) triple in the encoding result,
        /// this method provides the functionality of encoding Value.
        /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
        /// </remarks>
        protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
        {
            int resultLen = 0;

            //Encode inversely
            Asn1Object[] allFields = Fields;
            for (int i = attachedTags.Length - 1; i >= 0; i--)
            {
                if (!(fieldOptionalFlags[i] == true && allFields[i] == null))
                {
                    //The field is not optional or its optional but it exists
                    int curFieldLen = allFields[i].BerEncode(buffer);
                    resultLen += curFieldLen;
                    //Encode the context tag if the field has one
                    if (attachedTags[i] != null)
                    {
                        resultLen += LengthBerEncode(buffer, curFieldLen);
                        resultLen += TagBerEncode(buffer, attachedTags[i]);
                    }
                }
            }
            return resultLen;
        }
예제 #49
0
 /// <summary>
 /// Encodes the data of this object to the buffer.
 /// </summary>
 /// <param name="buffer">A buffer to which the encoding result will be written.</param>
 /// <returns>The length of the encoding result of the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result,
 /// this method provides the functionality of encoding Value.
 /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
 /// </remarks>
 protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
 {
     byte[] result = IntegerEncoding(Value);
     buffer.WriteBytes(result);
     return(result.Length);
 }
 /// <summary>
 /// Encodes the data of this object to the buffer.
 /// </summary>
 /// <param name="buffer">A buffer to which the encoding result will be written.</param>
 /// <returns>The length of the encoding result of the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result,
 /// this method provides the functionality of encoding Value.
 /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
 /// </remarks>
 protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
 {
     byte[] res = EncodeIdentifier(this.Value);
     buffer.WriteBytes(res);
     return res.Length;
 }
예제 #51
0
        /// <summary>
        /// Encodes the object by BER.
        /// </summary>
        /// <param name="buffer">A buffer that stores the 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 length of the encoding result of this object.</returns>
        /// <exception cref="Asn1EmptyDataException">
        /// Thrown when trying to encode an undefined data. 
        /// </exception>
        public override int BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag = true)
        {
            //TODO: deal with explicitTag
            if (SelectedChoice == UndefinedIndex)
            {
                throw new Asn1EmptyDataException(ExceptionMessages.EmptyData);
            }

            int length = GetData().BerEncode(buffer);

            Asn1Tag contextTag = metaDatas[(int)choiceIndexInFieldsMemberInfo].AttachedTag;
            if (contextTag != null)
            {
                length += LengthBerEncode(buffer, length);
                length += TagBerEncode(buffer, contextTag);
            }

            Object[] attrs = GetType().GetCustomAttributes(typeof(Asn1Tag), true);
            if (attrs.Length != 0)
            {
                Asn1Tag tag = attrs[0] as Asn1Tag;
                length += LengthBerEncode(buffer, length);
                length += TagBerEncode(buffer, tag);
            }
            return length;
        }
 /// <summary>
 /// Encodes the data of this object to the buffer.
 /// </summary>
 /// <param name="buffer">A buffer to which the encoding result will be written.</param>
 /// <returns>The length of the encoding result of the data.</returns>
 /// <remarks>
 /// For the TLV (Tag, Length, Value) triple in the encoding result,
 /// this method provides the functionality of encoding Value.
 /// The encoding for Tag and Length will be done in Asn1Object::BerEncode method.
 /// </remarks>
 protected override int ValueBerEncode(IAsn1BerEncodingBuffer buffer)
 {
     byte[] result = IntegerEncoding(Value);
     buffer.WriteBytes(result);
     return result.Length;
 }
예제 #53
0
        /// <summary>
        /// Encodes the object by BER.
        /// </summary>
        /// <param name="buffer">A buffer that stores the 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 length of the encoding result of this object.</returns>
        /// <exception cref="Asn1ConstraintsNotSatisfied">
        /// Thrown when the constraints are not satisfied before encoding.
        /// </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 BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag = true)
        {
            //TODO: deal with explicitTag
            if (!VerifyConstraints())
            {
                throw new Asn1ConstraintsNotSatisfied(ExceptionMessages.ConstraintsNotSatisfied
                    + " Encode " + this.GetType().Name + ".");
            }

            //Encoding inversely since buffer is reversed.

            //Add the encoding result of Value to the front of buffer.
            int resultLen = ValueBerEncode(buffer);

            //Add the encoding result of Length to the front of buffer.
            resultLen += LengthBerEncode(buffer, resultLen);

            //Add the encoding result of Universal Class Tag to the front of buffer.
            Asn1Tag uniTag = this.UniversalTag;
            resultLen += TagBerEncode(buffer, uniTag);

            //Add the encoding result of the top most tag (in most cases it's Application Class Tag) to the front of buffer if it is defined.
            Asn1Tag topTag = this.TopTag;
            if (topTag.TagType != Asn1TagType.Universal)
            {
                resultLen += LengthBerEncode(buffer, resultLen);
                resultLen += TagBerEncode(buffer, topTag);
            }
            return resultLen;
        }
 public override int BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag = true)
 {
     int allLen = 0;
     Asn1Tag contextTag;
     switch (SelectedChoice)
     {
         case 0:
             allLen += field0.BerEncodeWithoutUnisersalTag(buffer);
             contextTag = new Asn1Tag(Asn1TagType.Context, 0) { EncodingWay = EncodingWay.Primitive };
             allLen += TagBerEncode(buffer, contextTag);
             break;
         case 1:
             allLen += field1.BerEncodeWithoutUnisersalTag(buffer);
             contextTag = new Asn1Tag(Asn1TagType.Context, 3) { EncodingWay = EncodingWay.Constructed };
             allLen += TagBerEncode(buffer, contextTag);
             break;
         case 2:
             allLen += field2.BerEncodeWithoutUnisersalTag(buffer);
             contextTag = new Asn1Tag(Asn1TagType.Context, 9) { EncodingWay = EncodingWay.Primitive };
             allLen += TagBerEncode(buffer, contextTag);
             break;
         case 3:
             allLen += field3.BerEncodeWithoutUnisersalTag(buffer);
             contextTag = new Asn1Tag(Asn1TagType.Context, 10) { EncodingWay = EncodingWay.Primitive };
             allLen += TagBerEncode(buffer, contextTag);
             break;
         case 4:
             allLen += field4.BerEncodeWithoutUnisersalTag(buffer);
             contextTag = new Asn1Tag(Asn1TagType.Context, 11) { EncodingWay = EncodingWay.Primitive };
             allLen += TagBerEncode(buffer, contextTag);
             break;
         default:
             throw new Asn1ConstraintsNotSatisfied(ExceptionMessages.InvalidChoiceIndex + " AuthenticationChoice");
     }
     return allLen;
 }
예제 #55
0
 public override int BerEncode(IAsn1BerEncodingBuffer buffer, bool explicitTag)
 {
     return(0);
 }