Exemplo n.º 1
0
        /// <inheritdoc/>
        public override byte[] Write()
        {
            if (Constructed == false)
            {
                throw new FormatException("The encoding of a sequence value shall be constructed.");
            }

            var contentBytes = new List <byte>();
            var comparer     = new SetComparer();

            var orderedContent = this.Content.OrderBy(p => p, comparer);

            foreach (var item in orderedContent)
            {
                contentBytes.AddRange(item.Write());
            }

            var resBytes = new List <byte>();

            resBytes.AddRange(DerWriter.WriteTag(this.Asn1Class, this.Asn1Tag, this.Constructed));
            resBytes.AddRange(DerWriter.WriteLength(contentBytes, (Asn1Type)this.Asn1Tag));
            resBytes.AddRange(contentBytes);

            return(resBytes.ToArray());
        }
Exemplo n.º 2
0
        /// <inheritdoc/>
        public override byte[] Write()
        {
            if (Constructed)
            {
                throw new FormatException("The encoding of an object identifier value shall be primitive.");
            }

            var resBytes = new List <byte>();
            var oidParts = this.Content.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries)
                           .Select(p => Convert.ToInt32(p, CultureInfo.InvariantCulture)).ToArray();

            var  res       = new List <byte>(oidParts.Length - 1);
            byte firstByte = (byte)((40 * oidParts[0]) + oidParts[1]);

            res.Add(firstByte);

            // first 2 subidentifiers were already computed
            DerWriterUtils.ParseSubIdentifiers(oidParts.Skip(2).ToArray(), res);

            resBytes.AddRange(DerWriter.WriteTag(this.Asn1Class, this.Asn1Tag, this.Constructed));
            resBytes.AddRange(DerWriter.WriteLength(res, (Asn1Type)this.Asn1Tag));
            resBytes.AddRange(res);

            return(resBytes.ToArray());
        }
Exemplo n.º 3
0
        /// <inheritdoc/>
        public override byte[] Write()
        {
            var res = new List <byte>();

            res.AddRange(DerWriter.WriteTag(this.Asn1Class, this.Asn1Tag, this.Constructed));
            res.AddRange(DerWriter.WriteLength(this.Content, (Asn1Type)this.Asn1Tag));
            res.AddRange(this.Content);

            return(res.ToArray());
        }
Exemplo n.º 4
0
        /// <inheritdoc/>
        public override byte[] Write()
        {
            var res = new List <byte>();
            var val = this.Content.ToByteArray().Reverse().ToArray();

            res.AddRange(DerWriter.WriteTag(this.Asn1Class, this.Asn1Tag, this.Constructed));
            res.AddRange(DerWriter.WriteLength(val, (Asn1Type)this.Asn1Tag));
            res.AddRange(val);

            return(res.ToArray());
        }
Exemplo n.º 5
0
        /// <inheritdoc/>
        public override byte[] Write()
        {
            var res = new List <byte>();

            byte[] valBytes = null;

            // SpecialValues
            if (this.Content == 0)
            {
                // If the real value is the value plus zero, there shall be no contents octets in the encoding.
                return(new byte[] { 0x09, 0x00 });
            }
            else if (double.IsPositiveInfinity(this.Content))
            {
                valBytes = new byte[] { 0x40 };
            }
            else if (double.IsNegativeInfinity(this.Content))
            {
                valBytes = new byte[] { 0x41 };
            }
            else if (double.IsNaN(this.Content))
            {
                valBytes = new byte[] { 0x42 };
            }
            else if (this.Content == -0.0d)
            {
                valBytes = new byte[] { 0x43 };
            }
            else
            {
                // ToString to find out how many decimal digits value has
                // to achieve a format that looks like this ####.E-0
                // TrimStart will take care of doubles line 0.12314
                // Then remove full stop and we will have number of # to add to format string
                var tmpVal = this.Content.ToString(CultureInfo.InvariantCulture.NumberFormat).TrimStart('0').Replace(".", string.Empty);
                var format = FormattableString.Invariant($"{new string('#', tmpVal.Length)}\\..E+0");

                // Everything was taken care of with format string to meet ITU X690 specification for REAL type.
                var val = this.Content.ToString(format, CultureInfo.InvariantCulture.NumberFormat);

                var valEncoded = Encoding.ASCII.GetBytes(val);
                valBytes    = new byte[valEncoded.Length + 1];
                valBytes[0] = 0x03; // base 10 encoding
                valEncoded.CopyTo(valBytes, 1);
            }

            res.AddRange(DerWriter.WriteTag(this.Asn1Class, this.Asn1Tag, this.Constructed));
            res.AddRange(DerWriter.WriteLength(valBytes, (Asn1Type)this.Asn1Tag));
            res.AddRange(valBytes);

            return(res.ToArray());
        }
Exemplo n.º 6
0
        /// <inheritdoc/>
        public override byte[] Write()
        {
            // because getter of content already tried to parse the value
            var contentBytes = this.Content;

            var resBytes = new List <byte>();

            resBytes.AddRange(DerWriter.WriteTag(this.Asn1Class, this.Asn1Tag, this.Constructed));
            resBytes.AddRange(DerWriter.WriteLength(contentBytes, (Asn1Type)this.Asn1Tag));
            resBytes.AddRange(contentBytes);

            return(resBytes.ToArray());
        }
Exemplo n.º 7
0
        /// <inheritdoc/>
        public override byte[] Write()
        {
            if (Constructed)
            {
                throw new FormatException("For bit string, octet string and restricted character string types, the constructed form of encoding shall not be used.");
            }

            var res = new List <byte>();
            var val = Encoding.ASCII.GetBytes(this.Content);

            res.AddRange(DerWriter.WriteTag(this.Asn1Class, this.Asn1Tag, this.Constructed));
            res.AddRange(DerWriter.WriteLength(val, (Asn1Type)this.Asn1Tag));
            res.AddRange(val);

            return(res.ToArray());
        }
Exemplo n.º 8
0
        /// <inheritdoc/>
        public override byte[] Write()
        {
            var resBytes = new List <byte>();

            resBytes.AddRange(DerWriter.WriteTag(this.Asn1Class, this.Asn1Tag, this.Constructed));
            resBytes.AddRange(DerWriter.WriteLength(this.Content, (Asn1Type)this.Asn1Tag));

            // The contents octets for the primitive encoding shall contain an initial octet followed by zero, one or more subsequent octets.
            if (Constructed == false)
            {
                resBytes.Add((byte)this.UnusedBits);
            }

            resBytes.AddRange(this.Content);

            return(resBytes.ToArray());
        }
Exemplo n.º 9
0
        /// <inheritdoc/>
        public override byte[] Write()
        {
            if (Constructed)
            {
                throw new FormatException("The encoding of the TIME type shall be primitive.");
            }

            var utcDateTime    = this.Content.ToUniversalTime();
            var dateTimeString = utcDateTime.ToString("yyMMddHHmmss", CultureInfo.InvariantCulture);

            dateTimeString += "Z";
            var res = Encoding.UTF8.GetBytes(dateTimeString);

            var resBytes = new List <byte>();

            resBytes.AddRange(DerWriter.WriteTag(this.Asn1Class, this.Asn1Tag, this.Constructed));
            resBytes.AddRange(DerWriter.WriteLength(res, (Asn1Type)this.Asn1Tag));
            resBytes.AddRange(res);

            return(resBytes.ToArray());
        }
Exemplo n.º 10
0
        /// <inheritdoc/>
        public override byte[] Write()
        {
            if (Constructed)
            {
                throw new FormatException("The encoding of a relative object identifier value shall be primitive.");
            }

            var resBytes = new List <byte>();
            var oidParts = this.Content.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries)
                           .Select(p => Convert.ToInt32(p, CultureInfo.InvariantCulture)).ToArray();

            var res = new List <byte>(oidParts.Length);

            DerWriterUtils.ParseSubIdentifiers(oidParts, res);

            resBytes.AddRange(DerWriter.WriteTag(this.Asn1Class, this.Asn1Tag, this.Constructed));
            resBytes.AddRange(DerWriter.WriteLength(res, (Asn1Type)this.Asn1Tag));
            resBytes.AddRange(res);

            return(resBytes.ToArray());
        }
Exemplo n.º 11
0
        /// <inheritdoc/>
        public override byte[] Write()
        {
            if (Constructed)
            {
                throw new FormatException("For bit string, octet string and restricted character string types, the constructed form of encoding shall not be used.");
            }

            var enc = Encoding.GetEncoding("utf-32BE");

            if (enc == null)
            {
                throw new PlatformNotSupportedException("UTF-32 encoding is not supported on this platform.");
            }

            var val = enc.GetBytes(this.Content);

            var res = new List <byte>();

            res.AddRange(DerWriter.WriteTag(this.Asn1Class, this.Asn1Tag, this.Constructed));
            res.AddRange(DerWriter.WriteLength(val, (Asn1Type)this.Asn1Tag));
            res.AddRange(val);

            return(res.ToArray());
        }