Exemplo n.º 1
0
    public DomainCertificate(byte[] data, uint offset, uint length, bool privateKeyIncluded = false)
        : base(0, DateTime.MinValue, DateTime.MinValue, HashFunctionType.MD5)
    {
        var oOffset = offset;

        this.id = DC.GetUInt64(data, offset, Endian.Little);
        offset += 8;

        // load IPs
        this.ip  = DC.GetUInt32(data, offset, Endian.Little);
        offset  += 4;
        this.ip6 = DC.Clip(data, offset, 16);

        offset += 16;

        this.issueDate  = DC.GetDateTime(data, offset, Endian.Little);
        offset         += 8;
        this.expireDate = DC.GetDateTime(data, offset, Endian.Little);
        offset         += 8;

        this.domain = Encoding.ASCII.GetString(data, (int)offset + 1, data[offset]);
        offset     += (uint)data[offset] + 1;

        this.authorityName = (Encoding.ASCII.GetString(data, (int)offset + 1, data[offset]));
        offset            += (uint)data[offset] + 1;

        caId    = DC.GetUInt64(data, offset, Endian.Little);
        offset += 8;

        var aea = (AsymetricEncryptionAlgorithmType)(data[offset] >> 5);

        if (aea == AsymetricEncryptionAlgorithmType.RSA)
        {
            var  key            = new RSAParameters();
            uint exponentLength = (uint)data[offset++] & 0x1F;

            key.Exponent = DC.Clip(data, offset, exponentLength);
            offset      += exponentLength;

            uint keySize = DC.GetUInt16(data, offset, Endian.Little);
            offset += 2;

            key.Modulus = DC.Clip(data, offset, keySize);

            offset += keySize;

            // copy cert data
            publicRawData = new byte[offset - oOffset];
            Buffer.BlockCopy(data, (int)oOffset, publicRawData, 0, publicRawData.Length);

            if (privateKeyIncluded)
            {
                uint privateKeyLength = (keySize * 3) + (keySize / 2);
                privateRawData = DC.Clip(data, offset, privateKeyLength);

                uint halfKeySize = keySize / 2;

                key.D        = DC.Clip(data, offset, keySize);
                offset      += keySize;
                key.DP       = DC.Clip(data, offset, halfKeySize);
                offset      += halfKeySize;
                key.DQ       = DC.Clip(data, offset, halfKeySize);
                offset      += halfKeySize;
                key.InverseQ = DC.Clip(data, offset, halfKeySize);
                offset      += halfKeySize;
                key.P        = DC.Clip(data, offset, halfKeySize);
                offset      += halfKeySize;
                key.Q        = DC.Clip(data, offset, halfKeySize);
                offset      += halfKeySize;
            }

            // setup rsa
            rsa = RSA.Create();// new RSACryptoServiceProvider();
            rsa.ImportParameters(key);

            this.signature = DC.Clip(data, offset, length - (offset - oOffset));
        }
    }
Exemplo n.º 2
0
        public override long Parse(byte[] data, uint offset, uint ends)
        {
            try
            {
                long needed = 2;
                var  length = (ends - offset);
                if (length < needed)
                {
                    //Console.WriteLine("stage 1 " + needed);
                    return(length - needed);
                }

                uint oOffset = offset;
                FIN           = ((data[offset] & 0x80) == 0x80);
                RSV1          = ((data[offset] & 0x40) == 0x40);
                RSV2          = ((data[offset] & 0x20) == 0x20);
                RSV3          = ((data[offset] & 0x10) == 0x10);
                Opcode        = (WSOpcode)(data[offset++] & 0xF);
                Mask          = ((data[offset] & 0x80) == 0x80);
                PayloadLength = (long)(data[offset++] & 0x7F);

                if (Mask)
                {
                    needed += 4;
                }

                if (PayloadLength == 126)
                {
                    needed += 2;
                    if (length < needed)
                    {
                        //Console.WriteLine("stage 2 " + needed);
                        return(length - needed);
                    }
                    PayloadLength = DC.GetUInt16(data, offset);
                    offset       += 2;
                }
                else if (PayloadLength == 127)
                {
                    needed += 8;
                    if (length < needed)
                    {
                        //Console.WriteLine("stage 3 " + needed);
                        return(length - needed);
                    }

                    PayloadLength = DC.GetInt64(data, offset);
                    offset       += 8;
                }

                /*
                 * if (Mask)
                 * {
                 *                  MaskKey = new byte[4];
                 *  MaskKey[0] = data[offset++];
                 *  MaskKey[1] = data[offset++];
                 *  MaskKey[2] = data[offset++];
                 *  MaskKey[3] = data[offset++];
                 *
                 *  //MaskKey = DC.GetUInt32(data, offset);
                 *  //offset += 4;
                 * }
                 */

                needed += PayloadLength;
                if (length < needed)
                {
                    //Console.WriteLine("stage 4");
                    return(length - needed);
                }
                else
                {
                    if (Mask)
                    {
                        MaskKey    = new byte[4];
                        MaskKey[0] = data[offset++];
                        MaskKey[1] = data[offset++];
                        MaskKey[2] = data[offset++];
                        MaskKey[3] = data[offset++];

                        Message = DC.Clip(data, offset, (uint)PayloadLength);

                        //var aMask = BitConverter.GetBytes(MaskKey);
                        for (int i = 0; i < Message.Length; i++)
                        {
                            Message[i] = (byte)(Message[i] ^ MaskKey[i % 4]);
                        }
                    }
                    else
                    {
                        Message = DC.Clip(data, offset, (uint)PayloadLength);
                    }


                    return((offset - oOffset) + (int)PayloadLength);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.WriteLine(offset + "::" + DC.ToHex(data));
                throw ex;
            }
        }