Ejemplo n.º 1
0
        /// <summary>
        /// Decodes a ASN.1-encoded INTEGER to a unsigned 64-bit integer.
        /// </summary>
        /// <param name="rawData">ASN.1-encoded byte array.</param>
        /// <exception cref="InvalidDataException">The data is not valid ASN.1-encoded integer.</exception>
        /// <exception cref="OverflowException">
        /// The input data exceeds 8 bytes (max bytes that can be allocated for 64-bit integer).
        /// In order to decode large integers, use <see cref="DecodeInteger(Byte[], Boolean)"/> overloaded method.
        /// </exception>
        /// <exception cref="ArgumentNullException"><strong>rawData</strong> parameter is null reference.</exception>
        /// <returns>Unsigned 64-bit integer.</returns>
        public static Int64 DecodeInteger(Byte[] rawData)
        {
            if (rawData == null)
            {
                throw new ArgumentNullException();
            }
            var asn = new Asn1Reader(rawData);

            if (asn.Tag != (Byte)Asn1Type.INTEGER)
            {
                throw new InvalidDataException("Input data is not valid ASN.1-encoded INTEGER.");
            }
            if (asn.GetPayload().Length > 8)
            {
                throw new OverflowException();
            }
            var dummyPayload = new List <Byte>(asn.GetPayload());

            if (asn.GetPayload()[0] >= 128)
            {
                while (dummyPayload.Count < 8)
                {
                    dummyPayload.Insert(0, 255);
                }
            }
            var SB = new StringBuilder();

            foreach (Byte item in dummyPayload)
            {
                SB.Append(String.Format("{0:x2}", item));
            }
            return(Int64.Parse(SB.ToString(), NumberStyles.AllowHexSpecifier));
        }
        public PublicKey(byte[] certData)
        {
            HSMPublicKey = certData;
            //HSMPublicKey = new byte[certData.Length - 2];
            //Array.Copy(certData, 2, HSMPublicKey, 0, certData.Length-2);
            //public certificate
            Asn1.Asn1Reader reader = new Asn1.Asn1Reader(certData);
            reader.MoveNext();
            byte[] FirstDataSeq = reader.GetPayload();

            string FirstDataSeqHex = HexByteUtils.ByteArrayToHex(FirstDataSeq);

            keyBytesData  = FirstDataSeq;
            keyStringData = FirstDataSeqHex;

            //public modulus
            reader.MoveNext();
            byte[] SecondDataSeq    = reader.GetTagRawData();
            string SecondDataSeqHex = HexByteUtils.ByteArrayToHex(SecondDataSeq);

            byte[] modulus = new byte[3];
            Array.Copy(SecondDataSeq, 2, modulus, 0, 3);


            //create modulus object
            mod = new Modulus(modulus);
        }
Ejemplo n.º 3
0
 static String DecodeInteger(Asn1Reader asn)
 {
     return(Asn1Integer.DecodeIntegerAsInteger
                         ? new BigInteger(asn.GetPayload().Reverse().ToArray()).ToString()
                         : AsnFormatter.BinaryToString(
                asn.RawData,
                EncodingType.HexRaw,
                EncodingFormat.NOCRLF, asn.PayloadStartOffset, asn.PayloadLength));
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Decodes <strong>VisibleString</strong> to it's textual representation.
        /// </summary>
        /// <param name="rawData">ASN.1-encoded VisibleString string.</param>
        /// <exception cref="InvalidDataException">The input data is not properly encoded VisibleString.</exception>
        /// <exception cref="ArgumentNullException"><strong>rawData</strong> parameter is null reference.</exception>
        /// <returns>Decoded string.</returns>
        public static String DecodeVisibleString(Byte[] rawData)
        {
            if (rawData == null)
            {
                throw new ArgumentNullException();
            }
            var asn = new Asn1Reader(rawData);

            if (asn.GetPayload().Any(b => b < 32 || b > 126))
            {
                throw new InvalidDataException();
            }
            if (asn.Tag == (Byte)Asn1Type.VisibleString)
            {
                return(Encoding.ASCII.GetString(asn.GetPayload()));
            }
            throw new InvalidDataException();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Decodes <strong>TeletexString</strong> to it's textual representation.
        /// </summary>
        /// <param name="rawData">ASN.1-encoded TeletexString.</param>
        /// <exception cref="InvalidDataException">The input data is not properly encoded TeletexString.</exception>
        /// <exception cref="ArgumentNullException"><strong>rawData</strong> parameter is null reference.</exception>
        /// <returns>Decoded string.</returns>
        public static String DecodeTeletexString(Byte[] rawData)
        {
            if (rawData == null)
            {
                throw new ArgumentNullException();
            }
            var asn = new Asn1Reader(rawData);

            if (asn.GetPayload().Any(@by => @by > 127))
            {
                throw new ArgumentException("The data is invalid.");
            }
            if (asn.Tag == (Byte)Asn1Type.TeletexString)
            {
                return(Encoding.ASCII.GetString(asn.GetPayload()));
            }
            throw new InvalidDataException();
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Decodes <strong>BMPString</strong> (UTF-16) to it's textual representation.
        /// </summary>
        /// <param name="rawData">ASN.1-encoded BMP string.</param>
        /// <exception cref="InvalidDataException">The input data is not properly encoded BMPString.</exception>
        /// <exception cref="ArgumentNullException"><strong>rawData</strong> parameter is null reference.</exception>
        /// <returns>Decoded string.</returns>
        public static String DecodeBMPString(Byte[] rawData)
        {
            if (rawData == null)
            {
                throw new ArgumentNullException();
            }
            var asn = new Asn1Reader(rawData);

            if (asn.Tag == (Byte)Asn1Type.BMPString)
            {
                return(Encoding.BigEndianUnicode.GetString(asn.GetPayload()));
            }
            throw new InvalidDataException();
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Decodes ASN.1-encoded <strong>UTF-8</strong> string to it's textual representation.
        /// </summary>
        /// <param name="rawData">ASN.1-encoded UTF-8 string.</param>
        /// <exception cref="InvalidDataException">The input data is not properly encoded UTF8String.</exception>
        /// <exception cref="ArgumentNullException"><strong>rawData</strong> parameter is null reference.</exception>
        /// <returns>Decoded string.</returns>
        public static String DecodeUTF8String(Byte[] rawData)
        {
            if (rawData == null)
            {
                throw new ArgumentNullException();
            }
            var asn = new Asn1Reader(rawData);

            if (asn.Tag != (Byte)Asn1Type.UTF8String)
            {
                throw new InvalidDataException();
            }
            return(Encoding.UTF8.GetString(asn.GetPayload()));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Decodes <strong>OCTET_STRING</strong> as a sequence of hexadecimal octets.
        /// </summary>
        /// <param name="rawData">ASN.1-encoded <strong>OCTET_STRING</strong>.</param>
        /// <exception cref="ArgumentNullException"><strong>rawData</strong> parameter is null reference.</exception>
        /// <exception cref="InvalidDataException">The data is not valid ASN.1-encoded octet string.</exception>
        /// <returns>A sequence of hexadecimal octets.</returns>
        public static String DecodeOctetString(Byte[] rawData)
        {
            if (rawData == null)
            {
                throw new ArgumentNullException();
            }
            var asn = new Asn1Reader(rawData);

            if (asn.Tag != (Byte)Asn1Type.OCTET_STRING)
            {
                throw new InvalidDataException("Input data is not valid OCTET_STRING.");
            }
            var SB = new StringBuilder();

            foreach (Byte item in asn.GetPayload())
            {
                SB.Append(String.Format("{0:x2}", item) + " ");
            }
            return(SB.ToString());
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Decodes a ASN.1-encoded INTEGER to a hex string that represents ASN.1-encoded integer. This method accepts large integers.
        /// </summary>
        /// <param name="rawData">ASN.1-encoded byte array.</param>
        /// <param name="allowLarge">
        ///		Specifies whether to allow large integers. If this parameter is set to <strong>True</strong>, method
        ///		returns integer in a hexadecimal form. If this parameter is set to <strong>False</strong>, method
        ///		attempts to convert encoded integer to an <see cref="UInt32"/> numerical value. Numerical value is
        ///		returned as a string.
        /// </param>
        /// <exception cref="InvalidDataException">The data is not valid ASN.1-encoded integer.</exception>
        /// <exception cref="ArgumentNullException"><strong>rawData</strong> parameter is null reference.</exception>
        /// <returns>A hex string that represents large integer.</returns>
        public static String DecodeInteger(Byte[] rawData, Boolean allowLarge)
        {
            if (rawData == null)
            {
                throw new ArgumentNullException();
            }
            var asn = new Asn1Reader(rawData);

            if (asn.Tag != (Byte)Asn1Type.INTEGER)
            {
                throw new InvalidDataException("Input data is not valid ASN.1-encoded INTEGER.");
            }
            var SB = new StringBuilder();

            foreach (Byte item in asn.GetPayload())
            {
                SB.AppendFormat("{0:x2}", item);
            }
            return(allowLarge
                                ? SB.ToString()
                                : DecodeInteger(rawData).ToString(CultureInfo.InvariantCulture));
        }