/***************************************************************************/ /* Copyright (c) 2024 Microsoft Corporation */ /* Copyright (c) 2026 Eclipse ThreadX contributors */ /* */ /* This program and the accompanying materials are made available under */ /* the terms of the MIT License which is available at */ /* https://opensource.org/licenses/MIT. */ /* */ /* SPDX-License-Identifier: MIT */ /***************************************************************************/ #include "tx_api.h" #include "nx_api.h" #include "nx_tcp.h" #include "nx_secure_tls.h" #include "nx_secure_tls_api.h" #include "tls_test_utility.h" #include "nx_secure_tls_test_init_functions.h" extern void test_control_return(UINT status); #if !defined(NX_SECURE_TLS_CLIENT_DISABLED) && !defined(NX_SECURE_TLS_SERVER_DISABLED) && !defined(NX_SECURE_DISABLE_X509) #define __LINUX__ /* Define the ThreadX and NetX object control blocks... */ static TX_THREAD ntest_0; static TX_THREAD ntest_1; static NX_PACKET_POOL pool_0; static NX_PACKET_POOL unused_pool; static NX_IP ip_0; static NX_IP ip_1; static NX_TCP_SOCKET client_socket; static NX_TCP_SOCKET server_socket; static NX_SECURE_TLS_SESSION client_tls_session; static NX_SECURE_TLS_SESSION server_tls_session; static NX_SECURE_TLS_SESSION not_created_tls_session; static NX_SECURE_X509_CERT certificate; static NX_SECURE_X509_CERT ica_certificate; static NX_SECURE_X509_CERT client_certificate; static NX_SECURE_X509_CERT remote_certificate, remote_issuer; static NX_SECURE_X509_CERT client_remote_certificate, client_remote_issuer; static NX_SECURE_X509_CERT trusted_certificate; UCHAR remote_cert_buffer[2000]; UCHAR remote_issuer_buffer[2000]; UCHAR client_remote_cert_buffer[2000]; UCHAR client_remote_issuer_buffer[2000]; UCHAR server_packet_buffer[4000]; UCHAR client_packet_buffer[4000]; CHAR server_crypto_metadata[16000]; CHAR client_crypto_metadata[16000]; /* Test PKI (3-level). */ #include "test_ca_cert.c" #define ca_cert_der test_ca_cert_der #define ca_cert_der_len test_ca_cert_der_len #define TLS_CONNECT_TIMES (4) /* Cryptographic routines. */ extern NX_SECURE_TLS_CRYPTO nx_crypto_tls_ciphers; static unsigned char ica_cert_der[] = { 0x30, 0x82, 0x03, 0xdf, 0x30, 0x82, 0x02, 0xc7, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x01, 0x02, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x7a, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x02, 0x43, 0x41, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x09, 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x31, 0x16, 0x30, 0x14, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0d, 0x45, 0x78, 0x70, 0x72, 0x65, 0x73, 0x73, 0x20, 0x4c, 0x6f, 0x67, 0x69, 0x63, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x4e, 0x65, 0x74, 0x58, 0x20, 0x53, 0x65, 0x63, 0x75, 0x72, 0x65, 0x31, 0x1c, 0x30, 0x1a, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x13, 0x4e, 0x65, 0x74, 0x58, 0x20, 0x53, 0x65, 0x63, 0x75, 0x72, 0x65, 0x20, 0x54, 0x65, 0x73, 0x74, 0x20, 0x43, 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x37, 0x30, 0x33, 0x33, 0x31, 0x32, 0x31, 0x30, 0x30, 0x30, 0x32, 0x5a, 0x17, 0x0d, 0x32, 0x37, 0x30, 0x33, 0x32, 0x39, 0x32, 0x31, 0x30, 0x30, 0x30, 0x32, 0x5a, 0x30, 0x6f, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x02, 0x43, 0x41, 0x31, 0x16, 0x30, 0x14, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0d, 0x45, 0x78, 0x70, 0x72, 0x65, 0x73, 0x73, 0x20, 0x4c, 0x6f, 0x67, 0x69, 0x63, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x09, 0x4e, 0x58, 0x20, 0x53, 0x65, 0x63, 0x75, 0x72, 0x65, 0x31, 0x27, 0x30, 0x25, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x1e, 0x4e, 0x58, 0x20, 0x53, 0x65, 0x63, 0x75, 0x72, 0x65, 0x20, 0x54, 0x65, 0x73, 0x74, 0x20, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6d, 0x65, 0x64, 0x69, 0x61, 0x74, 0x65, 0x20, 0x43, 0x41, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xa7, 0xa4, 0xd9, 0xd0, 0x19, 0x84, 0x4e, 0xab, 0x8e, 0x08, 0xad, 0x30, 0xe6, 0x79, 0x46, 0x46, 0xe2, 0xd3, 0x13, 0xc1, 0x69, 0xa9, 0x4a, 0xc9, 0xc8, 0x95, 0x54, 0xa8, 0x76, 0x41, 0x63, 0x10, 0xc4, 0xe4, 0xfd, 0x08, 0x69, 0x7f, 0x89, 0x79, 0x82, 0x62, 0xd7, 0x90, 0x73, 0x55, 0x06, 0x0d, 0x62, 0xec, 0x1b, 0x2d, 0xaf, 0xf2, 0x39, 0x14, 0x47, 0x5b, 0x9d, 0x7a, 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xf0, 0x5b, 0xe8, 0x59, 0xf7, 0xc9, 0x08, 0xab, 0x09, 0x71, 0x78, 0x75, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x7b, 0x30, 0x79, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x2c, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x86, 0xf8, 0x42, 0x01, 0x0d, 0x04, 0x1f, 0x16, 0x1d, 0x4f, 0x70, 0x65, 0x6e, 0x53, 0x53, 0x4c, 0x20, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x64, 0x20, 0x43, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x65, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x8e, 0x6a, 0xb0, 0xfe, 0x49, 0xc5, 0x6a, 0x96, 0x9e, 0xba, 0xb8, 0xb6, 0x58, 0xfc, 0x52, 0x7f, 0xab, 0x51, 0xf2, 0x3a, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0x1b, 0x8d, 0x06, 0xd9, 0x6b, 0xad, 0xee, 0x82, 0x24, 0x26, 0x55, 0x9a, 0x1b, 0x03, 0x44, 0x92, 0x0a, 0x06, 0x92, 0x48, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x18, 0x5a, 0x50, 0xb6, 0x44, 0xba, 0x68, 0xac, 0x7f, 0x06, 0x0f, 0xbc, 0x1a, 0x58, 0x59, 0xf1, 0xf1, 0xe3, 0xb8, 0x0d, 0x74, 0xdd, 0x74, 0x26, 0x6a, 0xea, 0xfc, 0xfe, 0x06, 0x53, 0xe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xbf, 0xba, 0xef, 0x20, 0x36, 0x6f, 0x00, 0x40, 0xa2, 0xa3, 0x24, 0x5c, 0x61, 0xd5, 0xa5, 0x64, 0xbf, 0xbe, 0x29, 0xdf, 0x47, 0x4b, 0xc8, 0x76, 0x4f, 0x2e, 0x4c, 0x5c, 0xed, 0x99, 0x26, 0xe8, 0xf1, 0x72, 0x5e, 0x9b, 0x86, 0x73, 0xda, 0xfb }; static unsigned int ica_cert_der_len = 995; static unsigned char test_device_cert_der[] = { 0x30, 0x82, 0x03, 0xdb, 0x30, 0x82, 0x02, 0xc3, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x02, 0x10, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x6f, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x02, 0x43, 0x41, 0x31, 0x16, 0x30, 0x14, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0d, 0x45, 0x78, 0x70, 0x72, 0x65, 0x73, 0x73, 0x20, 0x4c, 0x6f, 0x67, 0x69, 0x63, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x09, 0x4e, 0x58, 0x20, 0x53, 0x65, 0x63, 0x75, 0x72, 0x65, 0x31, 0x27, 0x30, 0x25, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x1e, 0x4e, 0x58, 0x20, 0x53, 0x65, 0x63, 0x75, 0x72, 0x65, 0x20, 0x54, 0x65, 0x73, 0x74, 0x20, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6d, 0x65, 0x64, 0x69, 0x61, 0x74, 0x65, 0x20, 0x43, 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x37, 0x30, 0x33, 0x33, 0x31, 0x32, 0x31, 0x30, 0x39, 0x30, 0x39, 0x5a, 0x17, 0x0d, 0x32, 0x37, 0x30, 0x33, 0x32, 0x39, 0x32, 0x31, 0x30, 0x39, 0x30, 0x39, 0x5a, 0x30, 0x75, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61, 0x31, 0x16, 0x30, 0x14, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0d, 0x45, 0x78, 0x70, 0x72, 0x65, 0x73, 0x73, 0x20, 0x4c, 0x6f, 0x67, 0x69, 0x63, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x09, 0x4e, 0x58, 0x20, 0x53, 0x65, 0x63, 0x75, 0x72, 0x65, 0x31, 0x25, 0x30, 0x23, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x1c, 0x4e, 0x58, 0x20, 0x53, 0x65, 0x63, 0x75, 0x72, 0x65, 0x20, 0x44, 0x65, 0x76, 0x69, 0x63, 0x65, 0x20, 0x43, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x65, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xc4, 0x7e, 0xd4, 0xe3, 0xa2, 0x0c, 0x78, 0xe6, 0x0f, 0xa5, 0x3c, 0x01, 0x9b, 0x01, 0xdf, 0x96, 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cf, 0x72, 0x94, 0x65, 0x52, 0xf0, 0x88, 0x17, 0xf6, 0x00, 0xf4, 0xd0, 0x9a, 0x66, 0xe7, 0x1e, 0xa5, 0xfd, 0x4b, 0x95, 0xf3, 0x0e, 0x0f, 0x10, 0x60, 0x74, 0x54, 0xda, 0xb0, 0x91, 0x0a, 0x10, 0x07, 0x19, 0x30, 0x71, 0x34, 0x5f, 0xe2, 0x1c, 0x08, 0x77, 0x2e, 0xbc, 0xa3, 0x13, 0x18, 0x22, 0x5e, 0xf9, 0xe0, 0x67, 0x8e, 0xbc, 0xc0, 0x7d, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x7b, 0x30, 0x79, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x2c, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x86, 0xf8, 0x42, 0x01, 0x0d, 0x04, 0x1f, 0x16, 0x1d, 0x4f, 0x70, 0x65, 0x6e, 0x53, 0x53, 0x4c, 0x20, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x64, 0x20, 0x43, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x65, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x5c, 0xac, 0x85, 0x36, 0xd2, 0x6c, 0x5b, 0x71, 0xd0, 0x7e, 0x78, 0xe6, 0x4b, 0xc7, 0x9e, 0x0b, 0x06, 0xaa, 0x70, 0x8b, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0x8e, 0x6a, 0xb0, 0xfe, 0x49, 0xc5, 0x6a, 0x96, 0x9e, 0xba, 0xb8, 0xb6, 0x58, 0xfc, 0x52, 0x7f, 0xab, 0x51, 0xf2, 0x3a, 0x30, 0x0d, 0x06, 0x09, 0x2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0x2e, 0x95, 0x81, 0xb7, 0x2c, 0x8f, 0x2f, 0xcf, 0xdd, 0xc4, 0xcf, 0x1b, 0x2e, 0x2b, 0xe8, 0x4d, 0x92, 0xf6, 0x8f, 0xba, 0x08, 0x1a, 0x8a, 0x13, 0x2c, 0x2d, 0x38, 0xce, 0xfe, 0xa5, 0x59, 0xc5, 0xea, 0x3a, 0x07, 0x8b, 0xf9, 0x8d, 0x33, 0x25, 0xc0, 0x4e, 0x75, 0x51, 0xfc, 0xd1, 0x12, 0xec, 0x85, 0xb1, 0xb5, 0xf4, 0xc2, 0xb8, 0x05, 0x3c, 0x00, 0x4a, 0x1b, 0xab, 0xb2, 0xab, 0x69, 0x58, 0x33, 0x93, 0xf8, 0x47, 0x12, 0x75, 0xc2, 0x3a, 0x82, 0xe1, 0x2d, 0x95, 0xa9, 0x9c, 0xb9, 0xe3, 0x3d, 0x29, 0x93, 0x65, 0xa1, 0x79, 0xee }; static unsigned int test_device_cert_der_len = 991; static unsigned char test_device_cert_key_der[] = { 0x30, 0x82, 0x04, 0xa3, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00, 0xc4, 0x7e, 0xd4, 0xe3, 0xa2, 0x0c, 0x78, 0xe6, 0x0f, 0xa5, 0x3c, 0x01, 0x9b, 0x01, 0xdf, 0x96, 0xf6, 0x8e, 0xa0, 0x80, 0x4b, 0xb1, 0xc7, 0x20, 0x3c, 0xca, 0x81, 0x9a, 0xce, 0x08, 0x5f, 0x70, 0x99, 0x8d, 0x44, 0x46, 0xbc, 0xf5, 0xb1, 0x80, 0x3c, 0xe0, 0xc9, 0x09, 0x11, 0xb4, 0x07, 0x89, 0x5f, 0x06, 0x89, 0xec, 0xbe, 0xf8, 0x29, 0x48, 0x40, 0x55, 0x60, 0x1d, 0x59, 0xb3, 0x7b, 0x84, 0x77, 0xc3, 0x34, 0xf4, 0xd3, 0x51, 0x3e, 0x2f, 0x9d, 0x10, 0xbb, 0xaf, 0x47, 0xb2, 0x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1, 0xcd, 0x71, 0xeb, 0x56, 0x84, 0x57, 0xf2, 0xf9, 0xb9, 0x36, 0xa2, 0x1a, 0x17, 0x23, 0xdc, 0xab, 0x84, 0xe1, 0x8f, 0x86, 0x34, 0x8a, 0xb4, 0x85, 0x08, 0x7a, 0x27, 0x43, 0x76, 0xf2, 0x3f, 0x39, 0x03, 0x1c, 0xdf, 0x4e, 0x4d, 0xf1, 0xef, 0xa9, 0x02, 0x81, 0x80, 0x5c, 0x2d, 0x0a, 0x0d, 0x51, 0x48, 0xcd, 0x38, 0x64, 0xf3, 0x7a, 0xfd, 0x1a, 0x19, 0x42, 0x28, 0x68, 0xca, 0xda, 0x98, 0x2b, 0x46, 0xbb, 0x85, 0xdd, 0x55, 0x3d, 0xd8, 0x61, 0x7d, 0xc4, 0x90, 0x9d, 0xee, 0xe8, 0xb2, 0xc8, 0x85, 0xc9, 0x43, 0x14, 0xf7, 0x74, 0x01, 0xeb, 0x03, 0x0b, 0x7f, 0x24, 0x13, 0x99, 0x04, 0xff, 0x95, 0xf0, 0x08, 0x45, 0x76, 0xce, 0x00, 0xe8, 0xc5, 0x76, 0x34, 0xf9, 0x91, 0xa0, 0xc8, 0x83, 0xa9, 0x78, 0x87, 0x1d, 0xac, 0xc9, 0x66, 0x4d, 0x90, 0x46, 0x4e, 0x99, 0xef, 0xdd, 0x16, 0xa9, 0xc9, 0x43, 0xd6, 0xbf, 0xa1, 0x6b, 0x33, 0x8b, 0x68, 0x1d, 0x25, 0xd2, 0x95, 0x0d, 0xd0, 0x4d, 0xe2, 0xd9, 0x1e, 0x9c, 0xe0, 0x4a, 0xad, 0x1e, 0xda, 0xfd, 0x0d, 0xc1, 0xf7, 0x9c, 0xa7, 0x55, 0xb1, 0x70, 0xad, 0x33, 0x15, 0x1e, 0xe5, 0xc0, 0x44, 0x78, 0xd1 }; static unsigned int test_device_cert_key_der_len = 1191; #ifdef NX_SECURE_ENABLE_PSK_CIPHERSUITES static UCHAR tls_psk[] = { 0x1a, 0x2b, 0x3c, 0x4d }; #endif #define DEMO_STACK_SIZE 4096 // (3 * 1024 / sizeof(ULONG)) /* Define the IP thread's stack area. */ #define IP_STACK_SIZE 4096 //(2 * 1024 / sizeof(ULONG)) /* Define packet pool for the demonstration. */ #define NX_PACKET_POOL_BYTES ((1536 + sizeof(NX_PACKET)) * 20) #define NX_PACKET_POOL_SIZE (NX_PACKET_POOL_BYTES/sizeof(ULONG) + 64 / sizeof(ULONG)) /* Define the ARP cache area. */ #define ARP_AREA_SIZE 1024 // (512 / sizeof(ULONG)) #define TOTAL_STACK_SPACE (2 * (DEMO_STACK_SIZE + IP_STACK_SIZE + NX_PACKET_POOL_SIZE + ARP_AREA_SIZE)) #ifndef __LINUX__ ULONG test_stack_area[TOTAL_STACK_SPACE + 2000]; #endif /* Define the counters used in the demo application... */ ULONG error_counter; /* Define thread prototypes. */ static void ntest_0_entry(ULONG thread_input); static void ntest_1_entry(ULONG thread_input); static void ntest_0_connect_received(NX_TCP_SOCKET *server_socket, UINT port); static void ntest_0_disconnect_received(NX_TCP_SOCKET *server_socket); extern void _nx_ram_network_driver_1500(struct NX_IP_DRIVER_STRUCT *driver_req); /* Define what the initial system looks like. */ #ifndef __LINUX__ void tx_application_define(void *first_unused_memory) #else #ifdef CTEST void test_application_define(void *first_unused_memory); void test_application_define(void *first_unused_memory) #else void nx_secure_tls_error_checking_test_application_define(void *first_unused_memory) #endif #endif { CHAR *pointer; UINT status; /* Setup the working pointer. */ #ifndef __LINUX__ pointer = (CHAR*)test_stack_area; #else pointer = (CHAR *) first_unused_memory; #endif error_counter = 0; /* Create the main thread. */ tx_thread_create(&ntest_0, "thread 0", ntest_0_entry, 0, pointer, DEMO_STACK_SIZE, 3, 3, TX_NO_TIME_SLICE, TX_AUTO_START); pointer = pointer + DEMO_STACK_SIZE; /* Create the main thread. */ tx_thread_create(&ntest_1, "thread 1", ntest_1_entry, 0, pointer, DEMO_STACK_SIZE, 4, 4, TX_NO_TIME_SLICE, TX_AUTO_START); pointer = pointer + DEMO_STACK_SIZE; /* Initialize the NetX system. */ nx_system_initialize(); /* Create a packet pool. */ status = nx_packet_pool_create(&pool_0, "NetX Main Packet Pool", 1536, pointer, NX_PACKET_POOL_BYTES); pointer = pointer + NX_PACKET_POOL_BYTES; if(status) { printf("Failed nx_packet_pool_create with error: 0x%0x\n", status); error_counter++; } status = nx_packet_pool_create(&unused_pool, "Unused Packet Pool", 1536, pointer, NX_PACKET_POOL_BYTES); pointer = pointer + NX_PACKET_POOL_BYTES; if(status) { printf("Failed nx_packet_pool_create with error: 0x%0x\n", status); error_counter++; } /* Create an IP instance. */ status = nx_ip_create(&ip_0, "NetX IP Instance 0", IP_ADDRESS(1, 2, 3, 4), 0xFFFFFF00UL, &pool_0, _nx_ram_network_driver_1500, pointer, IP_STACK_SIZE, 1); pointer = pointer + IP_STACK_SIZE; /* Create another IP instance. */ status += nx_ip_create(&ip_1, "NetX IP Instance 1", IP_ADDRESS(1, 2, 3, 5), 0xFFFFFF00UL, &pool_0, _nx_ram_network_driver_1500, pointer, IP_STACK_SIZE, 1); pointer = pointer + IP_STACK_SIZE; if(status) { printf("Failed nx_ip_create with error: 0x%0x\n", status); error_counter++; } /* Enable ARP and supply ARP cache memory for IP Instance 0. */ status = nx_arp_enable(&ip_0, (void *) pointer, ARP_AREA_SIZE); pointer = pointer + ARP_AREA_SIZE; /* Enable ARP and supply ARP cache memory for IP Instance 1. */ status += nx_arp_enable(&ip_1, (void *) pointer, ARP_AREA_SIZE); pointer = pointer + ARP_AREA_SIZE; /* Check ARP enable status. */ if(status) { printf("Failed nx_arp_enable with error: 0x%0x\n", status); error_counter++; } /* Enable TCP processing for both IP instances. */ status = nx_tcp_enable(&ip_0); status += nx_tcp_enable(&ip_1); /* Check TCP enable status. */ if(status) { printf("Failed nx_tcp_enable with error: 0x%0x\n", status); error_counter++; } nx_secure_tls_initialize(); } /* Define callbacks used by TLS. */ /* Include CRL associated with Verisign root CA (for AWS) for demo purposes. */ #include "test_ca.crl.der.c" /* Timestamp function - should return Unix time formatted 32-bit integer. */ static ULONG tls_timestamp_function(void) { // Return a fixed epoch - 1500939067 seconds = 07/24/2017 @ 11:31pm (UTC) return(0x5976833BL); } /* Callback invoked whenever TLS has to validate a certificate from a remote host. Additional checking of the certificate may be done by the application here. */ static ULONG certificate_verification_callback(NX_SECURE_TLS_SESSION *session, NX_SECURE_X509_CERT* certificate) { const CHAR *dns_tld = "NX Secure Device Certificate"; UINT status; NX_SECURE_X509_CERTIFICATE_STORE *store; NX_SECURE_X509_CERT *issuer_certificate; UINT issuer_location; /* Check DNS entry string. */ status = nx_secure_x509_common_name_dns_check(certificate, (UCHAR*)dns_tld, strlen(dns_tld)); if(status != NX_SUCCESS) { printf("Error in certificate verification: DNS name did not match CN\n"); return(status); } /* Check CRL revocation status. */ store = &session -> nx_secure_tls_credentials.nx_secure_tls_certificate_store; status = nx_secure_x509_crl_revocation_check(test_ca_crl_der, test_ca_crl_der_len, store, certificate); return(NX_SUCCESS); } /* Define the test threads. */ /* -----===== SERVER =====----- */ static CHAR *html_data = "HTTP/1.1 200 OK\r\n" \ "Date: Fri, 15 Sep 2016 23:59:59 GMT\r\n" \ "Content-Type: text/html\r\n" \ "Content-Length: 200\r\n\r\n" \ "\r\n"\ "\r\n"\ "Hello NetX Secure User!\r\n"\ "This is a simple webpage\r\n"\ "served up using NetX Secure!\r\n"\ "\r\n"\ "\r\n"; static void ntest_0_entry(ULONG thread_input) { UINT status, packet_available; ULONG actual_status; NX_PACKET *send_packet; NX_PACKET *receive_packet; UCHAR receive_buffer[100]; ULONG bytes; UINT connect_count; /* Print out test information banner. */ printf("NetX Secure Test: TLS Error Checking Test............................"); /* Ensure the IP instance has been initialized. */ status = nx_ip_status_check(&ip_0, NX_IP_INITIALIZE_DONE, &actual_status, NX_IP_PERIODIC_RATE); /* Check for error. */ if(status) { printf("Failed nx_ip_status_check with error: 0x%0x\n", status); error_counter++; } /* Create a socket. */ status = nx_tcp_socket_create(&ip_0, &server_socket, "Server Socket", NX_IP_NORMAL, NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE * 100, 16*1024, NX_NULL, ntest_0_disconnect_received); /* Check for error. */ if(status) { printf("Failed nx_tcp_socket_create with error: 0x%0x\n", status); error_counter++; } status = _nxe_secure_tls_session_create(&server_tls_session, &nx_crypto_tls_ciphers, NX_NULL, 0); EXPECT_EQ(status, NX_PTR_ERROR); /* Create a TLS session for our socket. */ status = nx_secure_tls_session_create(&server_tls_session, &nx_crypto_tls_ciphers, server_crypto_metadata, sizeof(server_crypto_metadata)); /* Check for error. */ if(status) { printf("Failed nx_secure_tls_session_create with error: 0x%0x\n", status); error_counter++; } /* Setup our packet reassembly buffer. */ nx_secure_tls_session_packet_buffer_set(&server_tls_session, server_packet_buffer, sizeof(server_packet_buffer)); /* Enable Client Certificate Verification. */ nx_secure_tls_session_client_verify_enable(&server_tls_session); /* Add a timestamp function for time checking and timestamps in the TLS handshake. */ _nx_secure_tls_session_time_function_set(&server_tls_session, tls_timestamp_function); /* Setup the callback invoked when TLS has a certificate it wants to verify so we can do additional checks not done automatically by TLS. */ _nx_secure_tls_session_certificate_callback_set(&server_tls_session, certificate_verification_callback); ///////////////////////////////////////////////////////////////////////////////////////////////////////// // Initialize our certificate nx_secure_x509_certificate_initialize(&certificate, test_device_cert_der, test_device_cert_der_len, NX_NULL, 0, test_device_cert_key_der, test_device_cert_key_der_len, NX_SECURE_X509_KEY_TYPE_RSA_PKCS1_DER); nx_secure_tls_local_certificate_add(&server_tls_session, &certificate); nx_secure_x509_certificate_initialize(&ica_certificate, ica_cert_der, ica_cert_der_len, NX_NULL, 0, NULL, 0, NX_SECURE_X509_KEY_TYPE_NONE); nx_secure_tls_local_certificate_add(&server_tls_session, &ica_certificate); // If we are testing client certificate verify, allocate remote certificate space. nx_secure_tls_remote_certificate_allocate(&server_tls_session, &client_remote_certificate, client_remote_cert_buffer, sizeof(client_remote_cert_buffer)); nx_secure_tls_remote_certificate_allocate(&server_tls_session, &client_remote_issuer, client_remote_issuer_buffer, sizeof(client_remote_issuer_buffer)); /* Add a CA Certificate to our trusted store for verifying incoming client certificates. */ nx_secure_x509_certificate_initialize(&trusted_certificate, ca_cert_der, ca_cert_der_len, NX_NULL, 0, NULL, 0, NX_SECURE_X509_KEY_TYPE_NONE); nx_secure_tls_trusted_certificate_add(&server_tls_session, &trusted_certificate); ///////////////////////////////////////////////////////////////////////////////////////////////////////// /* Setup this thread to listen. */ status = nx_tcp_server_socket_listen(&ip_0, 12, &server_socket, 5, ntest_0_connect_received); /* Check for error. */ if(status) { printf("Failed nx_tcp_server_socket_listen with error: 0x%0x\n", status); error_counter++; } for(connect_count = 0; connect_count < TLS_CONNECT_TIMES; ++connect_count) { #ifdef NX_SECURE_ENABLE_PSK_CIPHERSUITES /* For PSK ciphersuites, add a PSK and identity hint. */ nx_secure_tls_psk_add(&server_tls_session, tls_psk, sizeof(tls_psk), "Client_identity", 15, "12345678", 8); #endif /* Accept a client socket connection. */ status = nx_tcp_server_socket_accept(&server_socket, NX_IP_PERIODIC_RATE); tx_thread_suspend(&ntest_0); /* Check for error. */ if(status) { printf("Failed nx_tcp_server_socket_accept with error: 0x%0x\n", status); error_counter++; } /* Start the TLS Session now that we have a connected socket. */ status = nx_secure_tls_session_start(&server_tls_session, &server_socket, NX_WAIT_FOREVER); /* Check for error. */ if (status) { printf("TLS Server Session start failed, error: %x\n", status); error_counter++; } /* Receive the HTTP request, and print it out. */ status = nx_secure_tls_session_receive(&server_tls_session, &receive_packet, NX_WAIT_FOREVER); /* Check for error. */ if (status) { printf("TLS Server receive failed, error: %x\n", status); error_counter++; } else { status = nx_packet_data_extract_offset(receive_packet, 0, receive_buffer, 100, &bytes); receive_buffer[bytes] = 0; if(bytes != 14) { printf("ERROR: data received by server does not match that sent by client.\n"); error_counter++; } status = strncmp((CHAR *)receive_buffer, "Hello there!\r\n", bytes); if(status) { printf("ERROR: data received by server does not match that sent by client.\n"); error_counter++; } //printf("Received data: %s\n", receive_buffer); nx_packet_release(receive_packet); } /* Test for nx_secure_tls_allocate_handshake_packet with null pool pointer. */ tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER); unused_pool.nx_packet_pool_available = 0; status = _nx_secure_tls_allocate_handshake_packet(&server_tls_session, &unused_pool, &send_packet, NX_NO_WAIT); tx_mutex_put(&_nx_secure_tls_protection); EXPECT_EQ(status, NX_SECURE_TLS_ALLOCATE_PACKET_FAILED); /* Allocate a return packet and send our HTML data back to the client. */ nx_secure_tls_packet_allocate(&server_tls_session, &pool_0, &send_packet, NX_WAIT_FOREVER); nx_packet_data_append(send_packet, html_data, strlen(html_data), &pool_0, NX_WAIT_FOREVER); /* TLS send the HTML/HTTPS data back to the client. */ status = nx_secure_tls_session_send(&server_tls_session, send_packet, NX_IP_PERIODIC_RATE); /* Check for error. */ if (status) { printf("Error in Server TLS send: %x\n", status); /* Release the packet. */ nx_packet_release(send_packet); error_counter++; } /* End the TLS session. This is required to properly shut down the TLS connection. */ status = nx_secure_tls_session_end(&server_tls_session, NX_WAIT_FOREVER); /* If the session did not shut down cleanly, this is a possible security issue. */ if (status) { printf("Error in TLS Server session end: %x\n", status); error_counter++; } /* Disconnect the server socket. */ status = nx_tcp_socket_disconnect(&server_socket, NX_IP_PERIODIC_RATE); /* Check for error. */ if(status) { printf("Failed nx_tcp_socket_disconnect with error: 0x%0x\n", status); error_counter++; } /* Attempt to allocate nx_packet while the tcp connection is not established. */ status = nx_secure_tls_packet_allocate(&server_tls_session, &pool_0, &send_packet, NX_WAIT_FOREVER); EXPECT_EQ(status, NX_SECURE_TLS_SESSION_UNINITIALIZED); /* Unaccept the server socket. */ status = nx_tcp_server_socket_unaccept(&server_socket); /* Check for error. */ if(status) { printf("Failed nx_tcp_server_socket_unaccept with error: 0x%0x\n", status); error_counter++; } /* Attempt to allocate nx_packet while the tcp socket is unaccepted. */ status = nx_secure_tls_packet_allocate(&server_tls_session, &pool_0, &send_packet, NX_WAIT_FOREVER); EXPECT_EQ(status, NX_SECURE_TLS_SESSION_UNINITIALIZED); /* Unlisten on the server port. */ status = nx_tcp_server_socket_relisten(&ip_0, 12, &server_socket); /* Check for error. */ if (status) { printf("Failed nx_tcp_server_socket_relisten with error: 0x%0x\n", status); error_counter++; } } /* End connect loop. */ /* Unlisten on the server port. */ status = nx_tcp_server_socket_unlisten(&ip_0, 12); /* Check for error. */ if (status) { printf("Failed nx_tcp_server_socket_unlisten with error: 0x%0x\n", status); error_counter++; } /* Delete TLS session. */ status = nx_secure_tls_session_delete(&server_tls_session); /* Check for error. */ if(status) { printf("Failed nx_secure_tls_session_delete with error: 0x%0x\n", status); error_counter++; } /* Delete the socket. */ status = nx_tcp_socket_delete(&server_socket); /* Check for error. */ if(status) { printf("Failed nx_tcp_socket_delete with error: 0x%0x\n", status); error_counter++; } } extern NX_CRYPTO_METHOD crypto_method_sha256; extern NX_SECURE_TLS_CRYPTO nx_crypto_tls_ciphers; extern NX_SECURE_TLS_CIPHERSUITE_INFO _nx_crypto_ciphersuite_lookup_table[]; /* -----===== CLIENT =====----- */ static void ntest_1_entry(ULONG thread_input) { UINT status; NX_PACKET *send_packet; NX_PACKET *receive_packet; NX_PACKET unused_packet; UCHAR receive_buffer[400]; ULONG bytes; UINT connect_count, backup; NX_SECURE_TLS_CRYPTO ciphers; USHORT iv_size; NX_IP unused_ip; NX_TCP_SOCKET unused_socket; USHORT message_type, header_length; UINT length; UCHAR header_data[5]; NX_SECURE_X509_CERT certificate; int hash_size; nx_secure_tls_test_init_functions(&client_tls_session); #if (!defined(NX_SECURE_ENABLE_PSK_CIPHERSUITES) && !defined(NX_SECURE_ENABLE_ECJPAKE_CIPHERSUITE) && !defined(NX_SECURE_ENABLE_ECC_CIPHERSUITE)) /* This function is only used in PSK. */ status = _nx_secure_tls_process_server_key_exchange(&client_tls_session, NX_NULL, 0); EXPECT_EQ(NX_SECURE_TLS_UNEXPECTED_MESSAGE, status); #endif /* Invalid certificate pointer. */ status = _nx_secure_x509_certificate_chain_verify(NX_NULL, NX_NULL, 0); EXPECT_EQ(NX_SECURE_X509_CHAIN_VERIFY_FAILURE, status); nx_packet_allocate(&pool_0, &send_packet, 0, NX_WAIT_FOREVER); /* Invalid hash size. */ length = 0; /* Test Line 92 */ client_tls_session.nx_secure_tls_session_ciphersuite = NX_NULL; status = _nx_secure_tls_verify_mac(&client_tls_session, receive_buffer, 0, send_packet, 0, &length); EXPECT_EQ(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE, status); /* Set up the correct ciphersuite and crypto table. */ client_tls_session.nx_secure_tls_session_ciphersuite = &_nx_crypto_ciphersuite_lookup_table[3]; client_tls_session.nx_secure_tls_crypto_table = &nx_crypto_tls_ciphers; /* Test Line 118 */ hash_size = client_tls_session.nx_secure_tls_session_ciphersuite -> nx_secure_tls_hash_size; receive_buffer[0] = NX_SECURE_TLS_APPLICATION_DATA; length = hash_size; status = _nx_secure_tls_verify_mac(&client_tls_session, receive_buffer, 0, send_packet, 0, &length); EXPECT_EQ(NX_SUCCESS, status); length = 0; status = _nx_secure_tls_verify_mac(&client_tls_session, receive_buffer, 0, send_packet, 0, &length); EXPECT_EQ(NX_SECURE_TLS_HASH_MAC_VERIFY_FAILURE, status); receive_buffer[0] = 0; length = hash_size; status = _nx_secure_tls_verify_mac(&client_tls_session, receive_buffer, 0, send_packet, 0, &length); EXPECT_EQ(NX_SECURE_TLS_HASH_MAC_VERIFY_FAILURE, status); length = 1; status = _nx_secure_tls_verify_mac(&client_tls_session, receive_buffer, 0, send_packet, 0, &length); EXPECT_EQ(NX_SECURE_TLS_HASH_MAC_VERIFY_FAILURE, status); /* Test line 146 */ length = hash_size + 1; status = _nx_secure_tls_verify_mac(&client_tls_session, receive_buffer, 7, send_packet, 0, &length); EXPECT_EQ(NX_SECURE_TLS_HASH_MAC_VERIFY_FAILURE, status); /* Test line 161 */ client_tls_session.nx_secure_tls_remote_sequence_number[0] = 0xFFFFFFFF; status = _nx_secure_tls_verify_mac(&client_tls_session, receive_buffer, 0, send_packet, 0, &length); EXPECT_EQ(NX_SECURE_TLS_PADDING_CHECK_FAILED, status); /* Test line 176 */ client_tls_session.nx_secure_tls_remote_sequence_number[0] = 0; status = _nx_secure_tls_verify_mac(&client_tls_session, receive_buffer, 0, send_packet, 0, &length); EXPECT_EQ(NX_SECURE_TLS_PADDING_CHECK_FAILED, status); /* Test Line 186 */ send_packet -> nx_packet_append_ptr = send_packet->nx_packet_prepend_ptr + 15; send_packet -> nx_packet_length = 15; length = 30; status = _nx_secure_tls_verify_mac(&client_tls_session, receive_buffer, 0, send_packet, 0, &length); EXPECT_EQ(NX_SECURE_TLS_PADDING_CHECK_FAILED, status); /* Invalid hash size. */ send_packet->nx_packet_append_ptr = send_packet->nx_packet_prepend_ptr; send_packet->nx_packet_length = 0; length = 0; client_tls_session.nx_secure_tls_session_ciphersuite = &_nx_crypto_ciphersuite_lookup_table[3]; client_tls_session.nx_secure_tls_crypto_table = &nx_crypto_tls_ciphers; receive_buffer[0] = 0xff; /* record type. */ status = _nx_secure_tls_verify_mac(&client_tls_session, receive_buffer, 0, send_packet, 0, &length); EXPECT_EQ(NX_SECURE_TLS_HASH_MAC_VERIFY_FAILURE, status); /* Empty application record. */ length = 20; /* Hash size of the current session. */ receive_buffer[0] = NX_SECURE_TLS_APPLICATION_DATA; client_tls_session.nx_secure_tls_remote_sequence_number[0] = 1; status = _nx_secure_tls_verify_mac(&client_tls_session, receive_buffer, 0, send_packet, 0, &length); EXPECT_EQ(NX_SUCCESS, status); length = 20; /* Hash size of the current session. */ client_tls_session.nx_secure_tls_remote_sequence_number[0] = -1; /* sequence overflow. */ status = _nx_secure_tls_verify_mac(&client_tls_session, receive_buffer, 0, send_packet, 0, &length); EXPECT_EQ(NX_SUCCESS, status); /* record length is less than hash size. */ length = 1; status = _nx_secure_tls_verify_mac(&client_tls_session, receive_buffer, 0, send_packet, 0, &length); EXPECT_EQ(NX_SECURE_TLS_HASH_MAC_VERIFY_FAILURE, status); nx_packet_release(send_packet); #ifndef NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION /* Attempt to allocate packets from null packet pool. */ client_tls_session.nx_secure_tls_packet_pool = &unused_pool; unused_pool.nx_packet_pool_available = 0; client_tls_session.nx_secure_tls_socket_type = NX_SECURE_TLS_SESSION_TYPE_CLIENT; client_tls_session.nx_secure_tls_tcp_socket = &unused_socket; client_tls_session.nx_secure_tls_remote_session_active = NX_TRUE; client_tls_session.nx_secure_tls_local_session_active = NX_TRUE; client_tls_session.nx_secure_tls_secure_renegotiation = NX_TRUE; status = _nx_secure_tls_session_renegotiate(&client_tls_session, NX_NO_WAIT); EXPECT_EQ(NX_SECURE_TLS_ALLOCATE_PACKET_FAILED, status); client_tls_session.nx_secure_tls_remote_session_active = NX_FALSE; client_tls_session.nx_secure_tls_local_session_active = NX_FALSE; client_tls_session.nx_secure_tls_secure_renegotiation = NX_FALSE; /* Unable to renegotiate. */ client_tls_session.nx_secure_tls_remote_session_active = NX_TRUE; client_tls_session.nx_secure_tls_local_session_active = NX_FALSE; status = _nx_secure_tls_session_renegotiate(&client_tls_session, NX_NO_WAIT); EXPECT_EQ(NX_SECURE_TLS_RENEGOTIATION_SESSION_INACTIVE, status); client_tls_session.nx_secure_tls_local_session_active = NX_TRUE; client_tls_session.nx_secure_tls_secure_renegotiation = 0; status = _nx_secure_tls_session_renegotiate(&client_tls_session, NX_NO_WAIT); EXPECT_EQ(NX_SECURE_TLS_RENEGOTIATION_FAILURE, status); #endif /* NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION */ /* Attempt to set keys while nx_secure_tls_session_ciphersuite is invalid. */ client_tls_session.nx_secure_tls_session_ciphersuite = 0; client_tls_session.nx_secure_tls_crypto_table = &nx_crypto_tls_ciphers; status = _nx_secure_tls_session_keys_set(&client_tls_session, NX_SECURE_TLS_KEY_SET_LOCAL); client_tls_session.nx_secure_tls_local_session_active = 0; client_tls_session.nx_secure_tls_remote_session_active = 0; EXPECT_EQ(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE, status); /* Attempt to fill ClientKeyExchange packets while nx_secure_tls_session_ciphersuite is invalid. */ status = _nx_secure_tls_send_client_key_exchange(&client_tls_session, &unused_packet); EXPECT_EQ(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE, status); /* Attempt to encrypt records' payload while nx_secure_tls_session_ciphersuite is invalid. */ status = _nx_secure_tls_record_payload_encrypt(&client_tls_session, &unused_packet, NX_NULL, 0); EXPECT_EQ(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE, status); /* Attempt to decrypt records' payload while nx_secure_tls_session_ciphersuite is invalid. */ status = _nx_secure_tls_record_payload_decrypt(&client_tls_session, NX_NULL, 0, 0, NX_NULL, NX_NULL, 0, 0); EXPECT_EQ(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE, status); /* Attempt to process client key exchange records while nx_secure_tls_session_ciphersuite is invalid. */ status = _nx_secure_tls_process_client_key_exchange(&client_tls_session, NX_NULL, 0, NX_SECURE_TLS); EXPECT_EQ(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE, status); /* Attempt to verify mac while nx_secure_tls_session_ciphersuite is invalid. */ status = _nx_secure_tls_verify_mac(&client_tls_session, NX_NULL, 0, NX_NULL, 0, &length); EXPECT_EQ(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE, status); /* Attempt to process remote certificate while nx_secure_tls_session_ciphersuite is invalid. */ receive_buffer[0] = 233; status = _nx_secure_tls_process_remote_certificate(&client_tls_session, receive_buffer, 0, 0); EXPECT_EQ(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH, status); /* Call tls_process_finished with invalid message length. */ status = _nx_secure_tls_process_finished(&client_tls_session, NX_NULL, 0); EXPECT_EQ(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH, status); #ifndef NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION /* Assign tls session pointer as NULL. */ status = nx_secure_tls_session_renegotiate(NX_NULL, 0); EXPECT_EQ(NX_PTR_ERROR, status); #endif /* NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION */ /* Call tls_process_finished with invalid tls session. */ receive_buffer[0] = 233; status = _nx_secure_tls_process_finished(&client_tls_session, receive_buffer, NX_SECURE_TLS_FINISHED_HASH_SIZE); EXPECT_EQ(NX_SECURE_TLS_UNEXPECTED_MESSAGE, status); /* Bad input for process_CCS. */ status = _nx_secure_tls_process_changecipherspec(NX_NULL, receive_buffer, 0); EXPECT_EQ(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH, status); receive_buffer[0] = 0; status = _nx_secure_tls_process_changecipherspec(NX_NULL, receive_buffer, 1); EXPECT_EQ(NX_SECURE_TLS_BAD_CIPHERSPEC, status); #if (!NX_SECURE_TLS_TLS_1_3_ENABLED) /* Bad input for tls_process_clienthello. */ status = _nx_secure_tls_process_clienthello(NX_NULL, NX_NULL, 0); EXPECT_EQ(NX_SECURE_TLS_INCORRECT_MESSAGE_LENGTH, status); #endif #ifndef NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION /* Call tls_send_hellorequest anyway. */ status = _nx_secure_tls_send_hellorequest(&client_tls_session, NX_NULL); EXPECT_EQ(NX_SECURE_TLS_SUCCESS, status); #endif /* NX_SECURE_TLS_DISABLE_SECURE_RENEGOTIATION */ /* Attempt to start tls session before tcp connection established. */ client_tls_session.nx_secure_tls_id = NX_SECURE_TLS_ID; unused_ip.nx_ip_default_packet_pool = &unused_pool; unused_pool.nx_packet_pool_available = 0; client_socket.nx_tcp_socket_ip_ptr = &unused_ip; client_socket.nx_tcp_socket_client_type = 1; status = nx_secure_tls_session_start(&client_tls_session, &client_socket, NX_NO_WAIT); EXPECT_EQ(NX_SECURE_TLS_ALLOCATE_PACKET_FAILED, status); client_tls_session.nx_secure_tls_id = 0; /* Create a socket. */ status = nx_tcp_socket_create(&ip_1, &client_socket, "Client Socket", NX_IP_NORMAL, NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE * 100, 1024*16, NX_NULL, NX_NULL); /* Check for error. */ if(status) { printf("Failed nx_tcp_socket_create with error: 0x%0x\n", status); error_counter++; } /* Bind the socket. */ status = nx_tcp_client_socket_bind(&client_socket, 12, NX_IP_PERIODIC_RATE); /* Check for error. */ if(status) { printf("Failed nx_tcp_client_socket_bind with error: 0x%0x\n", status); error_counter++; } /* Create a TLS session with an invalid crypto table. */ memcpy(&ciphers, &nx_crypto_tls_ciphers, sizeof(NX_SECURE_TLS_CRYPTO)); ciphers.nx_secure_tls_ciphersuite_lookup_table_size = 0; backup = crypto_method_sha256.nx_crypto_metadata_area_size; crypto_method_sha256.nx_crypto_metadata_area_size = 0xffffff; status = nx_secure_tls_session_create(&client_tls_session, &ciphers, client_crypto_metadata, 0); crypto_method_sha256.nx_crypto_metadata_area_size = backup; EXPECT_EQ(status, NX_INVALID_PARAMETERS); /* Create a TLS session for our socket. */ status = nx_secure_tls_session_create(&client_tls_session, &nx_crypto_tls_ciphers, client_crypto_metadata, sizeof(client_crypto_metadata)); /* Check for error. */ if(status) { printf("Failed nx_secure_tls_session_create with error: 0x%0x\n", status); error_counter++; } /* Get iv size while the current session is not active. */ iv_size = 0xFFFF; status = _nx_secure_tls_session_iv_size_get(&client_tls_session, &iv_size); EXPECT_EQ(status, NX_SUCCESS); EXPECT_EQ(iv_size, 0); /* JIRA NETXSECUR-48. Uninitialized iv_size. */ client_tls_session.nx_secure_tls_local_session_active = 1; client_tls_session.nx_secure_tls_session_ciphersuite = &_nx_crypto_ciphersuite_lookup_table[3]; client_tls_session.nx_secure_tls_crypto_table = &nx_crypto_tls_ciphers; client_tls_session.nx_secure_tls_protocol_version = NX_SECURE_TLS_VERSION_TLS_1_0; iv_size = 0xFFFF; status = _nx_secure_tls_session_iv_size_get(&client_tls_session, &iv_size); EXPECT_EQ(status, NX_SUCCESS); EXPECT_EQ(iv_size, 0); client_tls_session.nx_secure_tls_local_session_active = 0; client_tls_session.nx_secure_tls_protocol_version = 0; /* Attempt to get iv size with invalid ciphersuite. */ backup = client_tls_session.nx_secure_tls_local_session_active; client_tls_session.nx_secure_tls_local_session_active = 1; client_tls_session.nx_secure_tls_session_ciphersuite = NX_NULL; client_tls_session.nx_secure_tls_tcp_socket = &client_socket; tx_mutex_get(&_nx_secure_tls_protection, NX_WAIT_FOREVER); status = _nx_secure_tls_send_record(&client_tls_session, &unused_packet, 0, NX_WAIT_FOREVER); tx_mutex_put(&_nx_secure_tls_protection); EXPECT_EQ(status, NX_SECURE_TLS_UNKNOWN_CIPHERSUITE); NX_PACKET* packet_ptr; client_tls_session.nx_secure_tls_tcp_socket = &unused_socket; unused_pool.nx_packet_pool_available = 1; status = _nx_secure_tls_packet_allocate(&client_tls_session, &unused_pool, &packet_ptr, NX_NO_WAIT); nx_packet_release(packet_ptr); EXPECT_EQ(status, NX_SECURE_TLS_UNKNOWN_CIPHERSUITE); status = _nx_secure_tls_record_hash_initialize(&client_tls_session, NX_NULL, NX_NULL, 0, NX_NULL, NX_NULL); EXPECT_EQ(status, NX_SECURE_TLS_UNKNOWN_CIPHERSUITE); client_tls_session.nx_secure_tls_local_session_active = backup; /* Attempt to end tls session before start it. */ client_tls_session.nx_secure_tls_socket_type = NX_SECURE_TLS_SESSION_TYPE_CLIENT; client_tls_session.nx_secure_tls_client_state = NX_SECURE_TLS_CLIENT_STATE_HANDSHAKE_FINISHED; client_tls_session.nx_secure_tls_tcp_socket = &unused_socket; client_tls_session.nx_secure_tls_packet_pool = &unused_pool; unused_pool.nx_packet_pool_available = 0; status = nx_secure_tls_session_end(&client_tls_session, NX_NO_WAIT); client_tls_session.nx_secure_tls_socket_type = 0; client_tls_session.nx_secure_tls_client_state = 0; client_tls_session.nx_secure_tls_tcp_socket = NX_NULL; client_tls_session.nx_secure_tls_packet_pool = NX_NULL; unused_pool.nx_packet_pool_available = 10; EXPECT_EQ(NX_NO_PACKET, status); /* Attempt to process the header of a null packet. */ status = _nx_secure_tls_process_header(NX_NULL, NX_NULL, 0, NX_NULL, NX_NULL, NX_NULL, &iv_size); EXPECT_EQ(status, NX_SECURE_TLS_INVALID_PACKET); /* Attempt to process the header of an invalid packet. */ unused_packet.nx_packet_append_ptr = unused_packet.nx_packet_prepend_ptr; unused_packet.nx_packet_length = 1; status = _nx_secure_tls_process_header(&client_tls_session, &unused_packet, 0, &message_type, &length, header_data, &header_length); EXPECT_EQ(status, NX_SECURE_TLS_INVALID_PACKET); /* Setup our packet reassembly buffer. */ nx_secure_tls_session_packet_buffer_set(&client_tls_session, client_packet_buffer, sizeof(client_packet_buffer)); /* Make sure client certificate verification is disabled. */ nx_secure_tls_session_client_verify_disable(&client_tls_session); /* Need to allocate space for the certificate coming in from the remote host. */ nx_secure_tls_remote_certificate_allocate(&client_tls_session, &remote_certificate, remote_cert_buffer, sizeof(remote_cert_buffer)); nx_secure_tls_remote_certificate_allocate(&client_tls_session, &remote_issuer, remote_issuer_buffer, sizeof(remote_issuer_buffer)); //nx_secure_x509_certificate_initialize(&certificate, cert_der, cert_der_len, NX_NULL, 0, private_key_der, private_key_der_len, NX_SECURE_X509_KEY_TYPE_RSA_PKCS1_DER); nx_secure_x509_certificate_initialize(&client_certificate, test_device_cert_der, test_device_cert_der_len, NX_NULL, 0, test_device_cert_key_der, test_device_cert_key_der_len, NX_SECURE_X509_KEY_TYPE_RSA_PKCS1_DER); nx_secure_tls_local_certificate_add(&client_tls_session, &client_certificate); /* Add a CA Certificate to our trusted store for verifying incoming server certificates. */ nx_secure_x509_certificate_initialize(&trusted_certificate, ca_cert_der, ca_cert_der_len, NX_NULL, 0, NULL, 0, NX_SECURE_X509_KEY_TYPE_NONE); nx_secure_tls_trusted_certificate_add(&client_tls_session, &trusted_certificate); /* Add a timestamp function for time checking and timestamps in the TLS handshake. */ _nx_secure_tls_session_time_function_set(&client_tls_session, tls_timestamp_function); /* Setup the callback invoked when TLS has a certificate it wants to verify so we can do additional checks not done automatically by TLS. */ _nx_secure_tls_session_certificate_callback_set(&client_tls_session, certificate_verification_callback); /* Connect multiple times. */ for(connect_count = 0; connect_count < TLS_CONNECT_TIMES; ++connect_count) { #ifdef NX_SECURE_ENABLE_PSK_CIPHERSUITES /* For PSK ciphersuites, add a PSK and identity hint. For the client, we need to add the identity and set it for the particular server with which we want to communicate. "Client_identity" is the identity hint used by default in the OpenSSL s_server application when uisng PSK ciphersuites. */ nx_secure_tls_psk_add(&client_tls_session, tls_psk, sizeof(tls_psk), "Client_identity", 15, "12345678", 8); /* Our target server will use this PSK entry. */ nx_secure_tls_client_psk_set(&client_tls_session, tls_psk, sizeof(tls_psk), "Client_identity", 15, "12345678", 8); #endif status = nx_tcp_client_socket_connect(&client_socket, IP_ADDRESS(1, 2, 3, 4), 12, 5 * NX_IP_PERIODIC_RATE); if(status) { printf("Failed nx_tcp_client_socket_connect with error: 0x%0x\n", status); error_counter++; } tx_thread_resume(&ntest_0); status = nx_secure_tls_session_start(&client_tls_session, &client_socket, NX_WAIT_FOREVER); /* Check for error. */ if (status) { printf("Error in Client TLS handshake: 0x%02X\n", status); printf("ERROR!\n"); test_control_return(1); } /* Send some data to be read by the server thread instance. */ nx_secure_tls_packet_allocate(&client_tls_session, &pool_0, &send_packet, NX_WAIT_FOREVER); nx_packet_data_append(send_packet, "Hello there!\r\n", 14, &pool_0, NX_WAIT_FOREVER); status = nx_secure_tls_session_send(&client_tls_session, send_packet, NX_IP_PERIODIC_RATE); /* Check for error. */ if (status) { printf("Error in Client TLS send: %x\n", status); /* Release the packet. */ nx_packet_release(send_packet); error_counter++; } /* Receive the echoed and reversed data, and print it out. */ status = nx_secure_tls_session_receive(&client_tls_session, &receive_packet, NX_WAIT_FOREVER); /* Check for error. */ if (status) { printf("TLS receive failed in Client, error: %x\n", status); error_counter++; } else { status = nx_packet_data_extract_offset(receive_packet, 0, receive_buffer, 400, &bytes); if(status) { printf("Error in nx_packet_data_extract_offset: 0x%x\n", status); error_counter++; } receive_buffer[bytes] = 0; if(strlen(html_data) != bytes) { printf("Error: received data on client does not match that sent by server. \n"); error_counter++; } status = strncmp((CHAR *)receive_buffer, html_data, strlen(html_data)); if(status) { printf("Error: received data on client does not match that sent by server. \n"); error_counter++; } //printf("Received data: %s\n", receive_buffer); } /* End the TLS session. This is required to properly shut down the TLS connection. */ status = nx_secure_tls_session_end(&client_tls_session, NX_WAIT_FOREVER); /* If the session did not shut down cleanly, this is a possible security issue. */ if (status) { printf("Error in TLS Client session end: %x\n", status); error_counter++; } /* Disconnect this socket. */ status = nx_tcp_socket_disconnect(&client_socket, NX_IP_PERIODIC_RATE); /* Check for error. */ if(status) { printf("Failed nx_tcp_socket_disconnect with error: 0x%0x\n", status); error_counter++; } } /* End connect loop. */ /* Delete TLS session. */ status = nx_secure_tls_session_delete(&client_tls_session); /* Check for error. */ if(status) { printf("Failed nx_secure_tls_session_delete with error: 0x%0x\n", status); error_counter++; } /* Unbind the socket. */ status = nx_tcp_client_socket_unbind(&client_socket); /* Check for error. */ if(status) { printf("Failed nx_tcp_client_socket_unbind with error: 0x%0x\n", status); error_counter++; } /* Delete the socket. */ status = nx_tcp_socket_delete(&client_socket); /* Check for error. */ if(status) { printf("Failed nx_tcp_socket_delete with error: 0x%0x\n", status); error_counter++; } /* Delete the TLS session before it is created. */ status = nx_secure_tls_session_delete(¬_created_tls_session); if(status == NX_SUCCESS) { error_counter++; } /* Determine if the test was successful. */ if(error_counter) { printf("ERROR!\n"); test_control_return(1); } else { printf("SUCCESS!\n"); test_control_return(0); } } static void ntest_0_connect_received(NX_TCP_SOCKET *socket_ptr, UINT port) { /* Check for the proper socket and port. */ if((socket_ptr != &server_socket) || (port != 12)) { printf("ERROR: Improper socket and port on receive...\n"); error_counter++; } } static void ntest_0_disconnect_received(NX_TCP_SOCKET *socket) { /* Check for proper disconnected socket. */ if(socket != &server_socket) { printf("Error: Improper socket on disconnect...\n"); error_counter++; } } #else #ifdef CTEST void test_application_define(void *first_unused_memory); void test_application_define(void *first_unused_memory) #else VOID nx_secure_tls_error_checking_test_application_define(void *first_unused_memory) #endif { /* Print out test information banner. */ printf("NetX Secure Test: TLS Error Checking Test............................N/A\n"); test_control_return(3); } #endif