Example #1
0
        //ConnectionState readstate = new ConnectionState();
        //ConnectionState writestate = new ConnectionState();
        //ConnectionState pendingreadstate = new ConnectionState();
        //ConnectionState pendingwritestate = new ConnectionState();

        internal void SendAlert(AlertLevel level, AlertDescription desc)
        {
            TLSRecord       record = new TLSRecord();
            TLSAlertMessage msg    = new TLSAlertMessage();

            msg.AlertLevel       = level;
            msg.AlertDescription = desc;

            record.ContentType  = TLSContentType.Alert;
            record.MajorVersion = 0x03;
            record.MinorVersion = 0x01;
            record.Messages.Add(msg);
            Client.Send(record.Bytes, record.Bytes.Length, false);
        }
Example #2
0
        void ParseContentForRecords(byte [] bContent)
        {
            Messages.Clear();

            uint nIndexAt = 0;

            while (nIndexAt < bContent.Length) // Read all the sub records of type ContentType in this TLSRecord
            {
                if (ContentType == TLSContentType.Handshake)
                {
                    // Determine next handshake step
                    TLSHandShakeMessage msg = new TLSHandShakeMessage();
                    uint nRead = msg.ReadFromArray(bContent, (int)nIndexAt);
                    if (nRead == 0)
                    {
                        break;
                    }
                    Messages.Add(msg);

                    nIndexAt += nRead;
                }
                else if (ContentType == TLSContentType.Alert)
                {
                    TLSAlertMessage msg   = new TLSAlertMessage();
                    uint            nRead = msg.ReadFromArray(bContent, (int)nIndexAt);
                    if (nRead == 0)
                    {
                        break;
                    }
                    Messages.Add(msg);
                    nIndexAt += nRead;
                }
                else if (ContentType == TLSContentType.ChangeCipherSpec)
                {
                    TLSChangeCipherSpecMessage msg = new TLSChangeCipherSpecMessage();
                    uint nRead = msg.ReadFromArray(bContent, (int)nIndexAt);
                    if (nRead == 0)
                    {
                        break;
                    }
                    Messages.Add(msg);
                    nIndexAt += nRead;
                }
                else if (ContentType == TLSContentType.Application)
                {
                    // decrypt, add to ApplicationDataReturned
                    TLSApplicationMessage msg = new TLSApplicationMessage();
                    uint nRead = msg.ReadFromArray(bContent, (int)nIndexAt);
                    if (nRead == 0)
                    {
                        break;
                    }
                    Messages.Add(msg);
                    nIndexAt += nRead;
                }
                else
                {
                    break;
                }
            }
        }
Example #3
0
        void ParseContentForRecords(byte [] bContent)
        {
            Messages.Clear();

            uint nIndexAt = 0;
            while (nIndexAt < bContent.Length) // Read all the sub records of type ContentType in this TLSRecord
            {
                if (ContentType == TLSContentType.Handshake)
                {
                    // Determine next handshake step
                    TLSHandShakeMessage msg = new TLSHandShakeMessage();
                    uint nRead = msg.ReadFromArray(bContent, (int)nIndexAt);
                    if (nRead == 0)
                        break;
                    Messages.Add(msg);

                    nIndexAt += nRead;
                }
                else if (ContentType == TLSContentType.Alert)
                {
                    TLSAlertMessage msg = new TLSAlertMessage();
                    uint nRead = msg.ReadFromArray(bContent, (int)nIndexAt);
                    if (nRead == 0)
                        break;
                    Messages.Add(msg);
                    nIndexAt += nRead;
                }
                else if (ContentType == TLSContentType.ChangeCipherSpec)
                {
                    TLSChangeCipherSpecMessage msg = new TLSChangeCipherSpecMessage();
                    uint nRead = msg.ReadFromArray(bContent, (int)nIndexAt);
                    if (nRead == 0)
                        break;
                    Messages.Add(msg);
                    nIndexAt += nRead;
                }
                else if (ContentType == TLSContentType.Application)
                {
                    // decrypt, add to ApplicationDataReturned
                    TLSApplicationMessage msg = new TLSApplicationMessage();
                    uint nRead = msg.ReadFromArray(bContent, (int)nIndexAt);
                    if (nRead == 0)
                        break;
                    Messages.Add(msg);
                    nIndexAt += nRead;

                }
                else
                    break;
            }
        }
Example #4
0
        /// <summary>
        /// A new TLS record has been received... It may contain multiple messages, so these need to be parsed
        /// </summary>
        /// <param name="record"></param>
        List <byte[]> ParseAndHandleTLSRecords(TLSRecord record)
        {
            List <byte[]> ApplicationDataReturned = new List <byte[]>();

            /// Decrypt our message if we are at that stage
            ///
            try
            {
                record = state.DecompressRecord(record);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("!!! Exception decompressing record: {0}", ex);

                SendAlert(AlertLevel.fatal, AlertDescription.BadRecordMAC);
                this.Client.Disconnect();
                return(ApplicationDataReturned);
            }


            /// Let the record parse it's content - couldn't do that until now that it's decrypted
            record.Content = record.RawSetContent;
            if (SocketClient.ShowDebug == true)
            {
                record.DebugDump(true);
            }

            foreach (TLSMessage tlsmsg in record.Messages)
            {
                if (record.ContentType == TLSContentType.Handshake)
                {
                    // Determine next handshake step
                    TLSHandShakeMessage msg = tlsmsg as TLSHandShakeMessage;

                    if (msg.HandShakeMessageType != HandShakeMessageType.Finished)
                    {
                        AllHandShakeMessages.AppendData(msg.RawBytes);
                        if (SocketClient.ShowDebug == true)
                        {
                            System.Diagnostics.Debug.WriteLine("AllHandShakeMessages, adding record of length {0}, Length is now {1}", record.RawSetContent.Length, AllHandShakeMessages.Size);
                        }
                    }
                    else
                    {
                    }

                    HandleHandshakeMessage(msg);
                }
                else if (record.ContentType == TLSContentType.Alert)
                {
                    TLSAlertMessage msg = tlsmsg as TLSAlertMessage;
                }
                else if (record.ContentType == TLSContentType.ChangeCipherSpec)
                {
                    TLSChangeCipherSpecMessage msg = tlsmsg as TLSChangeCipherSpecMessage;
                    HandleChangeCipherSpecMessage(msg);
                }
                else if (record.ContentType == TLSContentType.Application)
                {
                    // decrypt, add to ApplicationDataReturned
                    TLSApplicationMessage msg = tlsmsg as TLSApplicationMessage;
                    ApplicationDataReturned.Add(msg.ApplicationData);
                }
            }

            return(ApplicationDataReturned);
        }
Example #5
0
        //ConnectionState readstate = new ConnectionState();
        //ConnectionState writestate = new ConnectionState();
        //ConnectionState pendingreadstate = new ConnectionState();
        //ConnectionState pendingwritestate = new ConnectionState();
        internal void SendAlert(AlertLevel level, AlertDescription desc)
        {
            TLSRecord record = new TLSRecord();
            TLSAlertMessage msg = new TLSAlertMessage();
            msg.AlertLevel = level;
            msg.AlertDescription = desc;

            record.ContentType = TLSContentType.Alert;
            record.MajorVersion = 0x03;
            record.MinorVersion = 0x01;
            record.Messages.Add(msg);
            Client.Send(record.Bytes, record.Bytes.Length, false);
        }