public void ReadEncryptedApplicationData_TLS_AES_128_GCM_SHA256_ResultIsExpected()
        {
            //Arrange
            var messageData      = GetEncryptedApplicationData();
            var verifyData       = GetVerifyData();
            var encryptionData   = GetEncryptionData();
            var parsedVerifyData = new Memory <byte>();

            using var aead = Cipher.TLS_AES_128_GCM_SHA256.CreateAead(Utils.ParseHexString(encryptionData.Iv), Utils.ParseHexString(encryptionData.Key));

            //Act
            var cursor = new MemoryCursor(Utils.ParseHexString(messageData));
            var result = TlsRecord.TryParseEncrypted(cursor, aead, encryptionData.SeqNum, out var record);

            using (record.Payload.SetCursor(cursor))
            {
                result          &= Finished.TryParse(cursor, out var parsedVerifyDataBuffer);
                parsedVerifyData = parsedVerifyDataBuffer.Read(cursor);

                result &= cursor.IsEnd();
            }

            result &= cursor.IsEnd();

            //Assert
            Assert.True(result);
            Assert.Equal(RecordType.Handshake, record.Type);
            Assert.Equal(ProtocolVersion.Tls12, record.ProtocolVersion);
            Assert.Equal(verifyData, Utils.ToHexString(parsedVerifyData.ToArray()), true);
        }
        public void ReadEncryptedApplicationData_TLS_AES_128_GCM_SHA256_ResultIsExpected()
        {
            //Arrange
            var messageData                   = GetEncryptedApplicationData();
            var cerificateData                = GetCertificateData();
            var signatureData                 = GetSignatureData();
            var verifyData                    = GetVerifyData();
            var encryptionData                = GetEncryptionData();
            var parsedCertificateEntries      = new List <CertificateEntry>();
            var parsedSignatureData           = new Memory <byte>();
            var parsedCertificateVerifyScheme = new SignatureScheme();
            var parsedVerifyData              = new Memory <byte>();

            using var aead = Cipher.TLS_AES_128_GCM_SHA256.CreateAead(Utils.ParseHexString(encryptionData.Iv), Utils.ParseHexString(encryptionData.Key));

            //Act
            var cursor = new MemoryCursor(Utils.ParseHexString(messageData));
            var result = TlsRecord.TryParseEncrypted(cursor, aead, encryptionData.SeqNum, out var record);

            using (record.Payload.SetCursor(cursor))
            {
                result &= EncryptedExtensions.TrySlice(cursor);

                result &= Certificate.TryParse(cursor, out var certificate);
                foreach (var entry in certificate.Payload.GetCertificateEntryReader(cursor))
                {
                    parsedCertificateEntries.Add(entry);
                }

                result &= CertificateVerify.TryParse(cursor, out var certificateVerify);
                parsedSignatureData           = certificateVerify.Signature.Read(cursor);
                parsedCertificateVerifyScheme = certificateVerify.Scheme;

                result          &= Finished.TryParse(cursor, out var parsedVerifyDataBuffer);
                parsedVerifyData = parsedVerifyDataBuffer.Read(cursor);

                result &= cursor.IsEnd();
            }

            result &= cursor.IsEnd();

            //Assert
            Assert.True(result);
            Assert.Equal(RecordType.Handshake, record.Type);
            Assert.Equal(ProtocolVersion.Tls12, record.ProtocolVersion);
            var certificateEntry = Assert.Single(parsedCertificateEntries);

            Assert.Equal(cerificateData, Utils.ToHexString(certificateEntry.Data.Read(cursor).ToArray()), true);
            Assert.Equal(signatureData, Utils.ToHexString(parsedSignatureData.ToArray()), true);
            Assert.Equal(SignatureScheme.RSA_PSS_RSAE_SHA256, parsedCertificateVerifyScheme);
            Assert.Equal(verifyData, Utils.ToHexString(parsedVerifyData.ToArray()), true);
        }
Exemplo n.º 3
0
        public void ReadEncryptedApplicationData_TLS_AES_128_GCM_SHA256_ResultIsExpected(string encryptedData, string key, string iv, ulong seq, string decryptedPayload)
        {
            //Arrange
            var buffer = Utils.ParseHexString(encryptedData);

            using var aead = Cipher.TLS_AES_128_GCM_SHA256.CreateAead(Utils.ParseHexString(iv), Utils.ParseHexString(key));

            //Act
            var cursor          = new MemoryCursor(buffer);
            var result          = TlsRecord.TryParseEncrypted(cursor, aead, seq, out var record);
            var decryptedResult = record.Payload.Read(cursor);

            //Assert
            Assert.True(result);
            Assert.Equal(RecordType.ApplicationData, record.Type);
            Assert.Equal(ProtocolVersion.Tls12, record.ProtocolVersion);
            Assert.Equal(decryptedPayload, Utils.ToHexString(decryptedResult.ToArray()), true);
        }