Exemple #1
0
        void SendTLSRecord(TLSRecord record, bool bAppend)
        {
            if (SocketClient.ShowDebug == true)
            {
                record.DebugDump(false);
            }

            if (bAppend == true)
            {
                AllHandShakeMessages.AppendData(record.Content);
                if (SocketClient.ShowDebug == true)
                {
                    System.Diagnostics.Debug.WriteLine("AllHandShakeMessages Length is now {0}", AllHandShakeMessages.Size);
                }
            }
            else
            {
            }

            /// Encrypt the record if we are in that state
            byte[] bEncryptedGenericBlockCipher = state.CompressEncryptOutgoingData(record);
            byte[] bSend = record.GetBytesWithEncryptedContent(bEncryptedGenericBlockCipher);


            Client.Send(bSend, bSend.Length, false);
        }
Exemple #2
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);
        }