1 /* This case tests basic authentication with empty name or password. */
2 #include    "tx_api.h"
3 #include    "nx_api.h"
4 #include    "fx_api.h"
5 #include    "nx_web_http_client.h"
6 #include    "nx_web_http_server.h"
7 
8 extern void test_control_return(UINT);
9 
10 #if !defined(NX_DISABLE_IPV4)
11 
12 #include "test_device_cert.c"
13 #include "test_ca_cert.c"
14 #define ca_cert_der test_ca_cert_der
15 #define ca_cert_der_len test_ca_cert_der_len
16 
17 #define     DEMO_STACK_SIZE         4096
18 
19 /* Set up FileX and file memory resources. */
20 static CHAR             ram_disk_memory[4096];
21 static FX_MEDIA         ram_disk;
22 static UCHAR            media_memory[4096];
23 
24 static UCHAR            server_stack[16000];
25 
26 /* Define device drivers.  */
27 extern void _fx_ram_driver(FX_MEDIA *media_ptr);
28 extern void _nx_ram_network_driver_1024(NX_IP_DRIVER *driver_req_ptr);
29 
30 /* Set up the HTTP client global variables. */
31 
32 #define         CLIENT_PACKET_SIZE  (NX_WEB_HTTP_CLIENT_MIN_PACKET_SIZE * 2)
33 
34 static TX_THREAD           client_thread;
35 static NX_PACKET_POOL      client_pool;
36 static NX_WEB_HTTP_CLIENT  my_client;
37 static NX_IP               client_ip;
38 static UINT                error_counter;
39 
40 /* Set up the HTTP server global variables */
41 
42 #define         SERVER_PACKET_SIZE  (NX_WEB_HTTP_SERVER_MIN_PACKET_SIZE * 2)
43 
44 static NX_WEB_HTTP_SERVER  my_server;
45 static NX_PACKET_POOL      server_pool;
46 static TX_THREAD           server_thread;
47 static NX_IP               server_ip;
48 static NXD_ADDRESS         server_ip_address;
49 static UINT                http_server_start = 0;
50 static UINT                http_client_stop = 0;
51 
52 static void thread_client_entry(ULONG thread_input);
53 static void thread_server_entry(ULONG thread_input);
54 
55 #define HTTP_SERVER_ADDRESS  IP_ADDRESS(1,2,3,4)
56 #define HTTP_CLIENT_ADDRESS  IP_ADDRESS(1,2,3,5)
57 
58 #ifdef NX_WEB_HTTPS_ENABLE
59 static UINT https_server_start = 0;
60 static UINT https_client_stop = 0;
61 static UINT loop = 2;
62 extern const NX_SECURE_TLS_CRYPTO nx_crypto_tls_ciphers;
63 static CHAR crypto_metadata_server[20000 * NX_WEB_HTTP_SERVER_SESSION_MAX];
64 static CHAR crypto_metadata_client[20000 * NX_WEB_HTTP_SERVER_SESSION_MAX];
65 static UCHAR tls_packet_buffer[18500];
66 static NX_SECURE_X509_CERT certificate;
67 static NX_SECURE_X509_CERT trusted_certificate;
68 static NX_SECURE_X509_CERT remote_certificate, remote_issuer;
69 static UCHAR remote_cert_buffer[2000];
70 static UCHAR remote_issuer_buffer[2000];
71 
72 static UINT tls_setup_callback(NX_WEB_HTTP_CLIENT *client_ptr, NX_SECURE_TLS_SESSION *tls_session);
73 #else
74 static UINT loop = 1;
75 #endif
76 
77 static UINT  authentication_check(NX_WEB_HTTP_SERVER *server_ptr, UINT request_type,
78                                   CHAR *resource, CHAR **name, CHAR **password, CHAR **realm);
79 
80 #ifdef CTEST
test_application_define(void * first_unused_memory)81 VOID test_application_define(void *first_unused_memory)
82 #else
83 void    netx_web_basic_authenticate_empty_test_application_define(void *first_unused_memory)
84 #endif
85 {
86 CHAR    *pointer;
87 UINT    status;
88 
89 
90     error_counter = 0;
91 
92     /* Setup the working pointer.  */
93     pointer =  (CHAR *) first_unused_memory;
94 
95     /* Create a helper thread for the server. */
96     tx_thread_create(&server_thread, "HTTP Server thread", thread_server_entry, 0,
97                      pointer, DEMO_STACK_SIZE,
98                      NX_WEB_HTTP_SERVER_PRIORITY, NX_WEB_HTTP_SERVER_PRIORITY, TX_NO_TIME_SLICE, TX_AUTO_START);
99 
100     pointer =  pointer + DEMO_STACK_SIZE;
101 
102     /* Initialize the NetX system.  */
103     nx_system_initialize();
104 
105     /* Create the server packet pool.  */
106     status =  nx_packet_pool_create(&server_pool, "HTTP Server Packet Pool", SERVER_PACKET_SIZE,
107                                     pointer, SERVER_PACKET_SIZE*8);
108     pointer = pointer + SERVER_PACKET_SIZE * 8;
109     if (status)
110         error_counter++;
111 
112     /* Create an IP instance.  */
113     status = nx_ip_create(&server_ip, "HTTP Server IP", HTTP_SERVER_ADDRESS,
114                           0xFFFFFF00UL, &server_pool, _nx_ram_network_driver_1024,
115                           pointer, 4096, 1);
116     pointer =  pointer + 4096;
117     if (status)
118         error_counter++;
119 
120     /* Enable ARP and supply ARP cache memory for the server IP instance.  */
121     status = nx_arp_enable(&server_ip, (void *) pointer, 1024);
122     pointer = pointer + 1024;
123     if (status)
124         error_counter++;
125 
126 
127      /* Enable TCP traffic.  */
128     status = nx_tcp_enable(&server_ip);
129     if (status)
130         error_counter++;
131 
132     /* Create the HTTP Client thread. */
133     status = tx_thread_create(&client_thread, "HTTP Client", thread_client_entry, 0,
134                               pointer, DEMO_STACK_SIZE,
135                               NX_WEB_HTTP_SERVER_PRIORITY + 2, NX_WEB_HTTP_SERVER_PRIORITY + 2, TX_NO_TIME_SLICE, TX_AUTO_START);
136     pointer =  pointer + DEMO_STACK_SIZE;
137     if (status)
138         error_counter++;
139 
140     /* Create the Client packet pool.  */
141     status =  nx_packet_pool_create(&client_pool, "HTTP Client Packet Pool", CLIENT_PACKET_SIZE,
142                                     pointer, CLIENT_PACKET_SIZE*8);
143     pointer = pointer + CLIENT_PACKET_SIZE * 8;
144     if (status)
145         error_counter++;
146 
147     /* Create an IP instance.  */
148     status = nx_ip_create(&client_ip, "HTTP Client IP", HTTP_CLIENT_ADDRESS,
149                           0xFFFFFF00UL, &client_pool, _nx_ram_network_driver_1024,
150                           pointer, 2048, 1);
151     pointer =  pointer + 2048;
152     if (status)
153         error_counter++;
154 
155     status  = nx_arp_enable(&client_ip, (void *) pointer, 1024);
156     pointer =  pointer + 2048;
157     if (status)
158         error_counter++;
159 
160      /* Enable TCP traffic.  */
161     status = nx_tcp_enable(&client_ip);
162     if (status)
163         error_counter++;
164 }
165 
166 #ifdef NX_WEB_HTTPS_ENABLE
167 /* Define the TLS setup callback function.  */
tls_setup_callback(NX_WEB_HTTP_CLIENT * client_ptr,NX_SECURE_TLS_SESSION * tls_session)168 static UINT tls_setup_callback(NX_WEB_HTTP_CLIENT *client_ptr, NX_SECURE_TLS_SESSION *tls_session)
169 {
170 UINT status;
171 
172 
173     /* Initialize and create TLS session.  */
174     status = nx_secure_tls_session_create(tls_session, &nx_crypto_tls_ciphers, crypto_metadata_client, sizeof(crypto_metadata_client));
175 
176     /* Check status.  */
177     if (status)
178     {
179         return(status);
180     }
181 
182     /* Allocate space for packet reassembly.  */
183     status = nx_secure_tls_session_packet_buffer_set(&(client_ptr -> nx_web_http_client_tls_session), tls_packet_buffer, sizeof(tls_packet_buffer));
184 
185     /* Check status.  */
186     if (status)
187     {
188         return(status);
189     }
190 
191     /* Add a CA Certificate to our trusted store for verifying incoming server certificates.  */
192     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);
193     nx_secure_tls_trusted_certificate_add(&(client_ptr -> nx_web_http_client_tls_session), &trusted_certificate);
194 
195     /* Need to allocate space for the certificate coming in from the remote host.  */
196     nx_secure_tls_remote_certificate_allocate(&(client_ptr -> nx_web_http_client_tls_session), &remote_certificate, remote_cert_buffer, sizeof(remote_cert_buffer));
197     nx_secure_tls_remote_certificate_allocate(&(client_ptr -> nx_web_http_client_tls_session), &remote_issuer, remote_issuer_buffer, sizeof(remote_issuer_buffer));
198 
199     return(NX_SUCCESS);
200 }
201 #endif /* NX_WEB_HTTPS_ENABLE  */
202 
thread_client_entry(ULONG thread_input)203 void thread_client_entry(ULONG thread_input)
204 {
205 UINT            i;
206 UINT            status;
207 NX_PACKET       *recv_packet;
208 
209 
210     /* Give IP task and driver a chance to initialize the system.  */
211     tx_thread_sleep(NX_IP_PERIODIC_RATE);
212 
213     /* Set server IP address.  */
214     server_ip_address.nxd_ip_address.v4 = HTTP_SERVER_ADDRESS;
215     server_ip_address.nxd_ip_version = NX_IP_VERSION_V4;
216 
217     /* First loop test HTTP, second loop test HTTPS.  */
218     for (i = 0; i < loop ; i++)
219     {
220         if (i == 0)
221         {
222 
223             /* Wait HTTP server started.  */
224             while(!http_server_start)
225             {
226                 tx_thread_sleep(NX_IP_PERIODIC_RATE);
227             }
228         }
229 #ifdef NX_WEB_HTTPS_ENABLE
230         else
231         {
232 
233             /* Wait HTTPS server started.  */
234             while(!https_server_start)
235             {
236                 tx_thread_sleep(NX_IP_PERIODIC_RATE);
237             }
238         }
239 #endif /* NX_WEB_HTTPS_ENABLE  */
240 
241         /* Create an HTTP client instance.  */
242         status = nx_web_http_client_create(&my_client, "HTTP Client", &client_ip, &client_pool, 1536);
243 
244         /* Check status.  */
245         if (status)
246             error_counter++;
247 
248         /* Send a GET request.  */
249         if (i == 0)
250         {
251             status = nx_web_http_client_get_start(&my_client, &server_ip_address,
252                                                   NX_WEB_HTTP_SERVER_PORT, "http://1.2.3.4/test.txt",
253                                                   "1.2.3.4", "Placeholdername", "", NX_WAIT_FOREVER);
254         }
255 #ifdef NX_WEB_HTTPS_ENABLE
256         else
257         {
258             status = nx_web_http_client_get_secure_start(&my_client, &server_ip_address,
259                                                          NX_WEB_HTTPS_SERVER_PORT, "https://1.2.3.4/test.txt",
260                                                          "1.2.3.4", "Placeholdername", "",
261                                                          tls_setup_callback, NX_WAIT_FOREVER);
262         }
263 #endif /* NX_WEB_HTTPS_ENABLE  */
264 
265         /* Check status.  */
266         if (status)
267             error_counter++;
268 
269         /* Get response from server.  */
270         status = nx_web_http_client_response_body_get(&my_client, &recv_packet, 1 * NX_IP_PERIODIC_RATE);
271 
272         /* Check status.  */
273         if (status != NX_WEB_HTTP_AUTHENTICATION_ERROR)
274             error_counter++;
275 
276         status = nx_web_http_client_delete(&my_client);
277         if (status)
278             error_counter++;
279 
280         /* Create an HTTP client instance.  */
281         status = nx_web_http_client_create(&my_client, "HTTP Client", &client_ip, &client_pool, 1536);
282 
283         /* Check status.  */
284         if (status)
285             error_counter++;
286 
287         /* Send a GET request.  */
288         if (i == 0)
289         {
290             status = nx_web_http_client_get_start(&my_client, &server_ip_address,
291                                                   NX_WEB_HTTP_SERVER_PORT, "http://1.2.3.4/test.txt",
292                                                   "1.2.3.4", "", "Placeholderpassword", NX_WAIT_FOREVER);
293         }
294 #ifdef NX_WEB_HTTPS_ENABLE
295         else
296         {
297             status = nx_web_http_client_get_secure_start(&my_client, &server_ip_address,
298                                                          NX_WEB_HTTPS_SERVER_PORT, "https://1.2.3.4/test.txt",
299                                                          "1.2.3.4", "", "Placeholderpassword",
300                                                          tls_setup_callback, NX_WAIT_FOREVER);
301         }
302 #endif /* NX_WEB_HTTPS_ENABLE  */
303 
304         /* Check status.  */
305         if (status)
306             error_counter++;
307 
308         /* Get response from server.  */
309         status = nx_web_http_client_response_body_get(&my_client, &recv_packet, 1 * NX_IP_PERIODIC_RATE);
310 
311         /* Check status.  */
312         if (status != NX_WEB_HTTP_AUTHENTICATION_ERROR)
313             error_counter++;
314 
315         status = nx_web_http_client_delete(&my_client);
316         if (status)
317             error_counter++;
318 
319         /* Set the flag.  */
320         if (i == 0)
321         {
322             http_client_stop = 1;
323         }
324 #ifdef NX_WEB_HTTPS_ENABLE
325         else
326         {
327             https_client_stop = 1;
328         }
329 #endif /* NX_WEB_HTTPS_ENABLE  */
330     }
331 }
332 
333 /* Define the helper HTTP server thread.  */
thread_server_entry(ULONG thread_input)334 void    thread_server_entry(ULONG thread_input)
335 {
336 UINT            i;
337 UINT            status;
338 FX_FILE         my_file;
339 UINT            server_port = NX_WEB_HTTP_SERVER_PORT;
340 
341 
342     /* Print out test information banner.  */
343     printf("NetX Test:   Web Basic Authenticate Empty Test.........................");
344 
345     /* Check for earlier error.  */
346     if(error_counter)
347     {
348         printf("ERROR!\n");
349         test_control_return(1);
350     }
351 
352     fx_media_format(&ram_disk,
353                     _fx_ram_driver,               // Driver entry
354                     ram_disk_memory,              // RAM disk memory pointer
355                     media_memory,                 // Media buffer pointer
356                     sizeof(media_memory),         // Media buffer size
357                     "MY_RAM_DISK",                // Volume Name
358                     1,                            // Number of FATs
359                     32,                           // Directory Entries
360                     0,                            // Hidden sectors
361                     256,                          // Total sectors
362                     512,                          // Sector size
363                     8,                            // Sectors per cluster
364                     1,                            // Heads
365                     1);                           // Sectors per track
366 
367     /* Open the RAM disk.  */
368     status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, media_memory, sizeof(media_memory)) ;
369     status += fx_file_create(&ram_disk, "TEST.TXT");
370     status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
371     status += fx_file_write(&my_file, "https server", 12);
372     status += fx_file_close(&my_file);
373     if(status)
374         error_counter++;
375 
376     /* Give NetX a chance to initialize the system.  */
377     tx_thread_sleep(NX_IP_PERIODIC_RATE);
378 
379     /* First loop test HTTP, second loop test HTTPS.  */
380     for (i = 0; i < loop; i++)
381     {
382 
383 #ifdef NX_WEB_HTTPS_ENABLE
384         if (i == 1)
385         {
386             server_port = NX_WEB_HTTPS_SERVER_PORT;
387         }
388 #endif
389 
390         /* Create the HTTP Server. */
391         status = nx_web_http_server_create(&my_server, "My HTTP Server", &server_ip, server_port, &ram_disk,
392                                            &server_stack, sizeof(server_stack), &server_pool,
393                                            authentication_check, NX_NULL);
394         if (status)
395             error_counter++;
396 
397 #ifdef NX_WEB_HTTPS_ENABLE
398         /* Set TLS for HTTPS.  */
399         if (i == 1)
400         {
401             /* Initialize device certificate (used for all sessions in HTTPS server).  */
402             memset(&certificate, 0, sizeof(certificate));
403             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);
404 
405             /* Setup TLS session data for the TCP server.  */
406             status = nx_web_http_server_secure_configure(&my_server, &nx_crypto_tls_ciphers,
407                                                          crypto_metadata_server, sizeof(crypto_metadata_server), tls_packet_buffer, sizeof(tls_packet_buffer),
408                                                          &certificate, NX_NULL, 0, NX_NULL, 0, NX_NULL, 0);
409             if (status)
410                 error_counter++;
411         }
412 #endif
413 
414         /* OK to start the HTTP Server.  */
415         status = nx_web_http_server_start(&my_server);
416         if (status)
417             error_counter++;
418 
419         /* Set the flag.  */
420         if (i == 0)
421         {
422             http_server_start = 1;
423 
424             /* Wait HTTP test finished.  */
425             while(!http_client_stop)
426             {
427                 tx_thread_sleep(NX_IP_PERIODIC_RATE);
428             }
429         }
430 #ifdef NX_WEB_HTTPS_ENABLE
431         else
432         {
433             https_server_start = 1;
434 
435             /* Wait HTTPS test finished.  */
436             while(!https_client_stop)
437             {
438                 tx_thread_sleep(NX_IP_PERIODIC_RATE);
439             }
440         }
441 #endif /* NX_WEB_HTTPS_ENABLE  */
442 
443         status = nx_web_http_server_delete(&my_server);
444         if (status)
445             error_counter++;
446     }
447 
448     /* Check packet pool.  */
449     if (server_pool.nx_packet_pool_available != server_pool.nx_packet_pool_total)
450     {
451         error_counter++;
452     }
453 
454     if (client_pool.nx_packet_pool_available != client_pool.nx_packet_pool_total)
455     {
456         error_counter++;
457     }
458 
459     if(error_counter)
460     {
461         printf("ERROR!\n");
462         test_control_return(1);
463     }
464     else
465     {
466         printf("SUCCESS!\n");
467         test_control_return(0);
468     }
469 }
470 
471 /* Define the application's authentication check.  This is called by
472    the HTTP server whenever a new request is received.  */
authentication_check(NX_WEB_HTTP_SERVER * server_ptr,UINT request_type,CHAR * resource,CHAR ** name,CHAR ** password,CHAR ** realm)473 static UINT  authentication_check(NX_WEB_HTTP_SERVER *server_ptr, UINT request_type,
474                                   CHAR *resource, CHAR **name, CHAR **password, CHAR **realm)
475 {
476 
477     /* Just use a simple name, password, and realm for all
478        requests and resources.  */
479     *name =     "Placeholdername";
480     *password = "Placeholderpassword";
481     *realm =    "NetX Duo HTTP demo";
482 
483     /* Request basic authentication.  */
484     return(NX_WEB_HTTP_BASIC_AUTHENTICATE);
485 }
486 
487 #else
488 
489 #ifdef CTEST
test_application_define(void * first_unused_memory)490 VOID test_application_define(void *first_unused_memory)
491 #else
492 void    netx_web_basic_authenticate_empty_test_application_define(void *first_unused_memory)
493 #endif
494 {
495 
496     /* Print out test information banner.  */
497     printf("NetX Test:   Web Basic Authenticate Empty Test.........................N/A\n");
498 
499     test_control_return(3);
500 }
501 #endif
502 
503