public TLSRecordHandlerTest(string server, int port)
        {
            string path      = System.Reflection.Assembly.GetAssembly(typeof(TLSRecordHandlerTest)).Location;
            string directory = Path.GetDirectoryName(path);

            _server = server;
            _port   = port;

            _pluginManager = new CipherSuitePluginManager(directory);
            _cipherSuite   = _pluginManager.GetCipherSuite(VERSION, CIPHER_SUITE);
            if (_cipherSuite != null)
            {
                Console.WriteLine("Got cipher suite");
            }
            else
            {
                throw new Exception("Error finding cipher suite!");
            }
            _recordHandler = new RecordHandler(VERSION, true);
        }
        private static CipherSuite SelectCipherSuite(CipherSuitePluginManager pluginManager,
                                                     ProtocolVersion clientVersion, ProtocolVersion minVersion, ProtocolVersion maxVersion,
                                                     List <UInt16> clientSuites, List <UInt16> serverSuites,
                                                     ServerCertificateSelectionCallback certificateSelectionCallback,
                                                     List <X509CertificateCollection> availableCertificates)
        {
            if (clientVersion < minVersion)
            {
                throw new AlertException(AlertDescription.ProtocolVersion,
                                         "Offered client version " + clientVersion +
                                         " lower than minimum supported version " + minVersion);
            }

            // Initialize our return value as null
            CipherSuite selectedCipherSuite = null;

            // Run as long as we either select a cipher suite or run out of versions
            ProtocolVersion selectedVersion = clientVersion < maxVersion ? clientVersion : maxVersion;

            while (selectedCipherSuite == null)
            {
                foreach (UInt16 id in clientSuites)
                {
                    if (!serverSuites.Contains(id))
                    {
                        continue;
                    }

                    // Try initializing the cipher suite based on ID
                    selectedCipherSuite = pluginManager.GetCipherSuite(selectedVersion, id);
                    if (selectedCipherSuite == null)
                    {
                        continue;
                    }

                    // Try selecting a suitable certificate for this cipher suite
                    int certificateIndex = certificateSelectionCallback(selectedCipherSuite, availableCertificates.ToArray());
                    if (certificateIndex >= 0 && certificateIndex < availableCertificates.Count)
                    {
                        // We finally found the valid suite, break out from the loop
                        break;
                    }
                    // No certificate was found for the suite, ignore
                    selectedCipherSuite = null;
                }

                if (selectedCipherSuite != null)
                {
                    break;
                }
                if (selectedVersion == minVersion)
                {
                    break;
                }
                selectedVersion = selectedVersion.PreviousProtocolVersion;
            }

            if (selectedCipherSuite == null)
            {
                throw new AlertException(AlertDescription.HandshakeFailure,
                                         "None of the cipher suites offered by client is accepted");
            }
            return(selectedCipherSuite);
        }
Beispiel #3
0
        public void PaddingTest()
        {
            CipherSuitePluginManager pluginManager = GetPluginManager();
            CipherSuite cipherSuite;

            RecordHandler clientHandler = new RecordHandler(ProtocolVersion.SSL3_0, true);
            RecordHandler serverHandler = new RecordHandler(ProtocolVersion.SSL3_0, false);

            cipherSuite = pluginManager.GetCipherSuite(ProtocolVersion.SSL3_0, 0x002f);
            Assert.IsNotNull(cipherSuite);

            ConnectionState connectionState = new ConnectionState(new byte[32], new byte[32], new byte[48]);

            clientHandler.SetCipherSuite(cipherSuite, connectionState);
            serverHandler.SetCipherSuite(cipherSuite, connectionState);
            clientHandler.ChangeLocalState();
            serverHandler.ChangeRemoteState();

            Record record    = new Record(22, ProtocolVersion.SSL3_0);
            int    blockSize = cipherSuite.BulkCipherAlgorithm.BlockSize;

            for (int i = 0; i < blockSize * 2; i++)
            {
                byte[] data = new byte[i];
                record.Fragment = (byte[])data.Clone();
                clientHandler.ProcessOutputRecord(record);
                Assert.AreEqual(0, record.Fragment.Length % blockSize);
                serverHandler.ProcessInputRecord(record);
                Assert.AreEqual(data, record.Fragment);
            }

            cipherSuite = pluginManager.GetCipherSuite(ProtocolVersion.TLS1_0, 0x002f);
            Assert.IsNotNull(cipherSuite);

            clientHandler.SetCipherSuite(cipherSuite, connectionState);
            serverHandler.SetCipherSuite(cipherSuite, connectionState);
            clientHandler.ChangeLocalState();
            serverHandler.ChangeRemoteState();

            record = new Record(22, ProtocolVersion.TLS1_0);
            for (int i = 0; i < blockSize * 2; i++)
            {
                byte[] data = new byte[i];
                record.Fragment = (byte[])data.Clone();
                clientHandler.ProcessOutputRecord(record);
                Assert.AreEqual(0, record.Fragment.Length % blockSize);
                serverHandler.ProcessInputRecord(record);
                Assert.AreEqual(data, record.Fragment);
            }

            cipherSuite = pluginManager.GetCipherSuite(ProtocolVersion.TLS1_2, 0x00a3);
            Assert.IsNotNull(cipherSuite);

            clientHandler.SetCipherSuite(cipherSuite, connectionState);
            serverHandler.SetCipherSuite(cipherSuite, connectionState);
            clientHandler.ChangeLocalState();
            serverHandler.ChangeRemoteState();

            record = new Record(22, ProtocolVersion.TLS1_2);
            for (int i = 0; i < blockSize * 2; i++)
            {
                byte[] data = new byte[i];
                record.Fragment = (byte[])data.Clone();
                clientHandler.ProcessOutputRecord(record);
                Assert.AreEqual(8 + data.Length + 16, record.Fragment.Length);
                serverHandler.ProcessInputRecord(record);
                Assert.AreEqual(data, record.Fragment);
            }
        }
Beispiel #4
0
        public void SSLv3Test()
        {
            byte[] finished1eb = new byte[] {
                0x16, 0x03, 0x00, 0x00, 0x40, 0x3c, 0xcd, 0xad,
                0xec, 0xd6, 0x18, 0x75, 0x2e, 0x6e, 0x37, 0xdf,
                0x19, 0x6d, 0x4f, 0xd4, 0x9c, 0x2c, 0x42, 0x1c,
                0xe7, 0x74, 0x45, 0xf9, 0x77, 0xb1, 0xde, 0x79,
                0x0f, 0xc4, 0x8b, 0xaf, 0x85, 0x7b, 0xa3, 0x73,
                0x92, 0x4c, 0xb7, 0xf4, 0xa3, 0x45, 0x78, 0x53,
                0x88, 0x18, 0xff, 0x2f, 0x3f, 0x7d, 0x27, 0xa1,
                0x4c, 0xa4, 0x1a, 0x5d, 0x81, 0x8d, 0xf3, 0xc8,
                0x3b, 0xb3, 0x71, 0x77, 0x59
            };
            byte[] finished2eb = new byte[] {
                0x16, 0x03, 0x00, 0x00, 0x40, 0xaa, 0xd4, 0x9e,
                0x02, 0x07, 0x52, 0x0f, 0xb0, 0x82, 0x79, 0x9a,
                0xbe, 0x53, 0xce, 0xfe, 0x66, 0xb3, 0xd5, 0xb1,
                0x58, 0x84, 0x5e, 0xe4, 0x7a, 0xb7, 0x86, 0x4c,
                0xbe, 0x33, 0x75, 0xc0, 0x57, 0xab, 0x78, 0xba,
                0x18, 0x02, 0xa8, 0xcb, 0x26, 0x2b, 0x0d, 0x3e,
                0x3a, 0xdb, 0xc5, 0x7a, 0xb8, 0x33, 0x6c, 0x35,
                0xe3, 0xa6, 0x09, 0xfe, 0x54, 0x45, 0xd0, 0x66,
                0xad, 0x95, 0xff, 0xb0, 0x3c
            };
            byte[] finished1db = new byte[] {
                0x16, 0x03, 0x00, 0x00, 0x28, 0x14, 0x00, 0x00,
                0x24, 0x98, 0x29, 0xF5, 0xAB, 0x95, 0xFD, 0x76,
                0x14, 0xDA, 0xA0, 0xA7, 0x97, 0x22, 0xCF, 0xB5,
                0x27, 0x8C, 0x96, 0xC6, 0xC7, 0x37, 0xDF, 0xD6,
                0x25, 0xB9, 0x19, 0x65, 0x2B, 0xA5, 0x06, 0x7D,
                0x10, 0x73, 0x7A, 0x59, 0xEA
            };
            byte[] finished2db = new byte[] {
                0x16, 0x03, 0x00, 0x00, 0x28, 0x14, 0x00, 0x00,
                0x24, 0x34, 0xCC, 0xA5, 0xD0, 0x08, 0x84, 0x26,
                0x19, 0xD9, 0xE5, 0x20, 0x68, 0x7B, 0xE5, 0x27,
                0xC8, 0x82, 0x71, 0x4E, 0xB1, 0x0E, 0xED, 0xE9,
                0x52, 0x01, 0xF3, 0xBE, 0x10, 0x46, 0x52, 0xDE,
                0x68, 0x2B, 0x42, 0xF8, 0x5F
            };
            byte[] crandom = new byte[] {
                0x4e, 0x60, 0xc6, 0xbd, 0x66, 0xbb, 0x66, 0x74,
                0xc8, 0xa2, 0x65, 0x5a, 0x1c, 0x3d, 0xf3, 0x7d,
                0xb0, 0xbc, 0x39, 0x60, 0x82, 0x6b, 0xec, 0xe9,
                0x62, 0xc6, 0xaa, 0x92, 0x5f, 0x7c, 0x3d, 0x3a
            };
            byte[] srandom = new byte[] {
                0x4e, 0x60, 0xc6, 0xb9, 0x1f, 0xff, 0xb5, 0x62,
                0xad, 0x3b, 0xe9, 0x16, 0xd9, 0x80, 0x04, 0xc5,
                0x91, 0x92, 0xa7, 0x1b, 0xe1, 0x39, 0x9e, 0x91,
                0x16, 0x76, 0xe1, 0x71, 0x2a, 0x9e, 0x23, 0x62
            };
            byte[] master = new byte[] {
                0x7C, 0xEE, 0xFE, 0xE3, 0x6A, 0x10, 0x82, 0xDC,
                0x69, 0xB0, 0xF2, 0x7C, 0xB6, 0xC1, 0xE9, 0x93,
                0xD8, 0x53, 0x40, 0x52, 0x7F, 0xAB, 0x2A, 0x45,
                0x3B, 0x4D, 0x69, 0xEF, 0x67, 0x60, 0xC2, 0x0E,
                0x97, 0x23, 0x40, 0x2B, 0x5A, 0x99, 0x90, 0x06,
                0xA7, 0x13, 0x10, 0x47, 0x5A, 0xC7, 0x6A, 0xAA
            };

            Record finished1 = GetRecord(finished1db);
            Record finished2 = GetRecord(finished2eb);

            CipherSuitePluginManager pluginManager = GetPluginManager();
            RecordHandler            recordHandler = new RecordHandler(ProtocolVersion.SSL3_0, true);

            CipherSuite cipherSuite = pluginManager.GetCipherSuite(ProtocolVersion.SSL3_0, 0x000a);

            Assert.IsNotNull(cipherSuite);

            // Set correct cipher suite
            ConnectionState connectionState = new ConnectionState(crandom, srandom, master);

            recordHandler.SetCipherSuite(cipherSuite, connectionState);

            // Test that initial state doesn't modify records
            recordHandler.ProcessOutputRecord(finished1);
            recordHandler.ProcessInputRecord(finished2);
            Assert.AreEqual(finished1db, finished1.GetBytes());
            Assert.AreEqual(finished2eb, finished2.GetBytes());

            recordHandler.ChangeLocalState();
            recordHandler.ProcessOutputRecord(finished1);
            recordHandler.ChangeRemoteState();
            recordHandler.ProcessInputRecord(finished2);

            // Ignore last block, it has some random padding
            int bsize = cipherSuite.BulkCipherAlgorithm.BlockSize;

            byte[] tmp1 = new byte[finished1eb.Length - bsize];
            byte[] tmp2 = new byte[finished1eb.Length - bsize];
            Buffer.BlockCopy(finished1eb, 0, tmp1, 0, tmp1.Length);
            Buffer.BlockCopy(finished1.GetBytes(), 0, tmp2, 0, tmp2.Length);

            Assert.AreEqual(tmp1, tmp2);
            Assert.AreEqual(finished2db, finished2.GetBytes());
        }
Beispiel #5
0
        public void TLSv10Test()
        {
            byte[] finished1eb1 = new byte[] {
                0x16, 0x03, 0x01, 0x00, 0x30, 0x78, 0x02, 0x93,
                0x89, 0x26, 0x70, 0xDD, 0x57, 0x12, 0xD5, 0x47,
                0xC5, 0xE3, 0x9E, 0xE7, 0x03, 0x2A, 0x41, 0x35,
                0x46, 0xE3, 0x13, 0x06, 0xBB, 0x4F, 0x21, 0xEB,
                0x3D, 0x5F, 0x42, 0xF6, 0xBF, 0x89, 0x4B, 0x43,
                0x8C, 0x77, 0x29, 0xF9, 0x54, 0xDC, 0xCB, 0x0E,
                0xE6, 0xA4, 0xA0, 0x13, 0xF8
            };
            byte[] finished1eb2 = new byte[] {
                0x16, 0x03, 0x01, 0x00, 0x40, 0x78, 0x02, 0x93,
                0x89, 0x26, 0x70, 0xdd, 0x57, 0x12, 0xd5, 0x47,
                0xc5, 0xe3, 0x9e, 0xe7, 0x03, 0x2a, 0x41, 0x35,
                0x46, 0xe3, 0x13, 0x06, 0xbb, 0x4f, 0x21, 0xeb,
                0x3d, 0x5f, 0x42, 0xf6, 0xbf, 0x1f, 0x91, 0x7c,
                0xf3, 0xf5, 0x07, 0x84, 0xc4, 0x51, 0x89, 0x73,
                0xbb, 0xb1, 0x88, 0x35, 0xcd, 0x21, 0xa9, 0x2a,
                0x1a, 0x08, 0xfc, 0x23, 0x4b, 0x9f, 0x9b, 0x0a,
                0x83, 0x47, 0x2c, 0x2e, 0x81
            };
            byte[] finished2eb = new byte[] {
                0x16, 0x03, 0x01, 0x00, 0x30, 0xde, 0x54, 0x89,
                0x2f, 0x60, 0x2f, 0x3e, 0x88, 0x6f, 0x78, 0xac,
                0xed, 0xf9, 0xf8, 0x56, 0x31, 0xbd, 0x55, 0x3f,
                0x7f, 0x82, 0x41, 0xfa, 0x49, 0x4c, 0x16, 0x01,
                0x13, 0x91, 0xcb, 0xcc, 0x78, 0x3f, 0x89, 0x86,
                0xaa, 0x58, 0xd1, 0x73, 0x76, 0xb3, 0xa2, 0x82,
                0x99, 0x02, 0x07, 0x95, 0x56
            };
            byte[] finished1db = new byte[] {
                0x16, 0x03, 0x01, 0x00, 0x10, 0x14, 0x00, 0x00,
                0x0C, 0x21, 0x39, 0xB0, 0xD0, 0x3F, 0x12, 0x1F,
                0x3B, 0x76, 0x1A, 0xE7, 0x71
            };
            byte[] finished2db = new byte[] {
                0x16, 0x03, 0x01, 0x00, 0x10, 0x14, 0x00, 0x00,
                0x0C, 0xDE, 0x0C, 0x18, 0x26, 0x54, 0x99, 0x6F,
                0x29, 0xA2, 0x72, 0x91, 0xC4
            };
            byte[] crandom = new byte[] {
                0x4e, 0x60, 0xe1, 0xed, 0x13, 0x2c, 0x19, 0xe5,
                0x44, 0xaf, 0xb2, 0xe1, 0x1d, 0x97, 0x67, 0x47,
                0xaf, 0x5a, 0xb8, 0xf4, 0x7e, 0xcb, 0xbe, 0x42,
                0x4f, 0x3b, 0x24, 0x9e, 0xff, 0x0f, 0x5d, 0x87
            };
            byte[] srandom = new byte[] {
                0x4e, 0x60, 0xe2, 0xa4, 0x9e, 0x37, 0xea, 0x2a,
                0xc1, 0xbe, 0xe9, 0x3d, 0x05, 0x04, 0xb9, 0x83,
                0x57, 0x2a, 0x03, 0xf0, 0x1a, 0x6a, 0xe5, 0x53,
                0x08, 0xa9, 0x01, 0xb0, 0x06, 0x2a, 0x9a, 0x79
            };
            byte[] master = new byte[] {
                0xB2, 0xEC, 0xD1, 0x59, 0x9C, 0x97, 0x49, 0x58,
                0xFB, 0x86, 0x9A, 0x65, 0xEB, 0x8F, 0x91, 0x31,
                0x54, 0xAC, 0x41, 0xCD, 0xF8, 0x12, 0x8F, 0xCA,
                0x25, 0xF0, 0x06, 0x91, 0xFE, 0xF3, 0x09, 0x7F,
                0x04, 0x6F, 0x9D, 0x75, 0x24, 0x52, 0xCD, 0xC5,
                0x69, 0xE2, 0x0C, 0xB2, 0xB3, 0xCD, 0xA0, 0x02
            };

            Record finished1 = GetRecord(finished1db);
            Record finished2 = GetRecord(finished2eb);

            CipherSuitePluginManager pluginManager = GetPluginManager();
            RecordHandler            recordHandler = new RecordHandler(ProtocolVersion.SSL3_0, true);

            CipherSuite cipherSuite = pluginManager.GetCipherSuite(ProtocolVersion.TLS1_0, 0x002f);

            Assert.IsNotNull(cipherSuite);

            // Set correct cipher suite
            ConnectionState connectionState = new ConnectionState(crandom, srandom, master);

            recordHandler.SetCipherSuite(cipherSuite, connectionState);

            // Test that initial state doesn't modify records
            recordHandler.ProcessOutputRecord(finished1);
            recordHandler.ProcessInputRecord(finished2);
            Assert.AreEqual(finished1db, finished1.GetBytes());
            Assert.AreEqual(finished2eb, finished2.GetBytes());

            recordHandler.ChangeLocalState();
            recordHandler.ProcessOutputRecord(finished1);
            recordHandler.ChangeRemoteState();
            recordHandler.ProcessInputRecord(finished2);

            if (finished1.GetBytes().Length == finished1eb1.Length)
            {
                Assert.AreEqual(finished1eb1, finished1.GetBytes());
            }
            else
            {
                Assert.AreEqual(finished1eb2, finished1.GetBytes());
            }
            Assert.AreEqual(finished2db, finished2.GetBytes());
        }
Beispiel #6
0
        public void TLSv12Test()
        {
            byte[] finished2eb = new byte[] {
                0x16, 0x03, 0x03, 0x00, 0x28, 0x77, 0xb7, 0x66,
                0x18, 0x6f, 0x70, 0x52, 0x14, 0xc9, 0xa4, 0xbc,
                0xbf, 0x26, 0x05, 0xf7, 0x99, 0x42, 0xb1, 0xc5,
                0xc3, 0xcf, 0x3c, 0xb6, 0x48, 0x34, 0xfc, 0xd6,
                0x35, 0x45, 0xf0, 0x12, 0xce, 0x1b, 0x0b, 0xff,
                0x94, 0x15, 0x8f, 0x56, 0xda
            };
            byte[] finished1db = new byte[] {
                0x16, 0x03, 0x03, 0x00, 0x10, 0x14, 0x00, 0x00,
                0x0C, 0x01, 0xE4, 0xC1, 0xFF, 0x6B, 0x7E, 0x2F,
                0x6F, 0x6B, 0xC5, 0x16, 0xE6
            };
            byte[] finished2db = new byte[] {
                0x16, 0x03, 0x03, 0x00, 0x10, 0x14, 0x00, 0x00,
                0x0C, 0x83, 0x51, 0x75, 0x4D, 0x01, 0x37, 0x51,
                0xEE, 0x72, 0x2F, 0xAE, 0x90
            };
            byte[] crandom = new byte[] {
                0x4e, 0x61, 0xfd, 0x0b, 0x16, 0x0e, 0x3c, 0x92,
                0xa0, 0x15, 0x90, 0x61, 0xfb, 0x2e, 0x51, 0xb4,
                0x8f, 0xb0, 0xf5, 0xf2, 0x67, 0x4e, 0x20, 0x7f,
                0xc3, 0x0d, 0x8e, 0x85, 0xdb, 0xfb, 0x58, 0xac
            };
            byte[] srandom = new byte[] {
                0x4e, 0x61, 0xfd, 0xe6, 0xf9, 0x06, 0x4d, 0x7c,
                0x4c, 0x5b, 0x0b, 0x72, 0x6a, 0xba, 0x86, 0x76,
                0xd9, 0x7d, 0xce, 0xcc, 0xd5, 0x24, 0x60, 0x6e,
                0xaf, 0x16, 0x0f, 0x85, 0x52, 0x8d, 0xba, 0x86
            };
            byte[] master = new byte[] {
                0xCA, 0xCB, 0xE5, 0x65, 0x39, 0x78, 0xFC, 0xCE,
                0x18, 0xD8, 0x1C, 0xD8, 0xAD, 0x4E, 0x5E, 0x91,
                0x18, 0x2B, 0x0E, 0xEF, 0x60, 0x13, 0xBB, 0xBF,
                0xD8, 0x62, 0x77, 0x7F, 0xA3, 0xEF, 0x76, 0xAB,
                0x7E, 0x3A, 0x26, 0xF2, 0xE1, 0xB8, 0x50, 0x46,
                0x65, 0x85, 0x04, 0x23, 0xCC, 0x52, 0x2C, 0x87
            };

            Record finished1 = GetRecord(finished1db);
            Record finished2 = GetRecord(finished2eb);

            CipherSuitePluginManager pluginManager = GetPluginManager();
            RecordHandler            recordHandler = new RecordHandler(ProtocolVersion.SSL3_0, true);

            // Check that GCM suite is not available in TLS 1.1
            CipherSuite nullSuite = pluginManager.GetCipherSuite(ProtocolVersion.TLS1_1, 0x00a2);

            Assert.IsNull(nullSuite);

            // Check that GCM suite is available in TLS 1.2
            CipherSuite cipherSuite = pluginManager.GetCipherSuite(ProtocolVersion.TLS1_2, 0x00a2);

            Assert.IsNotNull(cipherSuite);

            // Set correct cipher suite
            ConnectionState connectionState = new ConnectionState(crandom, srandom, master);

            recordHandler.SetCipherSuite(cipherSuite, connectionState);

            // Test that initial state doesn't modify records
            recordHandler.ProcessOutputRecord(finished1);
            recordHandler.ProcessInputRecord(finished2);
            Assert.AreEqual(finished1db, finished1.GetBytes());
            Assert.AreEqual(finished2eb, finished2.GetBytes());

            recordHandler.ChangeLocalState();
            recordHandler.ProcessOutputRecord(finished1);
            recordHandler.ChangeRemoteState();
            recordHandler.ProcessInputRecord(finished2);

            Assert.AreEqual(finished2db, finished2.GetBytes());
        }