1 /* This case tests basic authentication. */
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_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", "name", "password", 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", "name", "password",
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_GET_DONE)
274             error_counter++;
275         else
276             nx_packet_release(recv_packet);
277 
278         status = nx_web_http_client_delete(&my_client);
279         if (status)
280             error_counter++;
281 
282         /* Create an HTTP client instance.  */
283         status = nx_web_http_client_create(&my_client, "HTTP Client", &client_ip, &client_pool, 1536);
284 
285         /* Check status.  */
286         if (status)
287             error_counter++;
288 
289         /* Send a GET request.  */
290         if (i == 0)
291         {
292             status = nx_web_http_client_get_start(&my_client, &server_ip_address,
293                                                   NX_WEB_HTTP_SERVER_PORT, "http://1.2.3.4/test.txt",
294                                                   "1.2.3.4", "name2", "password", NX_WAIT_FOREVER);
295         }
296 #ifdef NX_WEB_HTTPS_ENABLE
297         else
298         {
299             status = nx_web_http_client_get_secure_start(&my_client, &server_ip_address,
300                                                          NX_WEB_HTTPS_SERVER_PORT, "https://1.2.3.4/test.txt",
301                                                          "1.2.3.4", "name2", "password",
302                                                          tls_setup_callback, NX_WAIT_FOREVER);
303         }
304 #endif /* NX_WEB_HTTPS_ENABLE  */
305 
306         /* Check status.  */
307         if (status)
308             error_counter++;
309 
310         /* Get response from server.  */
311         status = nx_web_http_client_response_body_get(&my_client, &recv_packet, 1 * NX_IP_PERIODIC_RATE);
312 
313         /* Check status.  */
314         if (status != NX_WEB_HTTP_AUTHENTICATION_ERROR)
315             error_counter++;
316 
317         status = nx_web_http_client_delete(&my_client);
318         if (status)
319             error_counter++;
320 
321         /* Set the flag.  */
322         if (i == 0)
323         {
324             http_client_stop = 1;
325         }
326 #ifdef NX_WEB_HTTPS_ENABLE
327         else
328         {
329             https_client_stop = 1;
330         }
331 #endif /* NX_WEB_HTTPS_ENABLE  */
332     }
333 }
334 
335 /* Define the helper HTTP server thread.  */
thread_server_entry(ULONG thread_input)336 void    thread_server_entry(ULONG thread_input)
337 {
338 UINT            i;
339 UINT            status;
340 FX_FILE         my_file;
341 UINT            server_port = NX_WEB_HTTP_SERVER_PORT;
342 
343 
344     /* Print out test information banner.  */
345     printf("NetX Test:   Web Basic Authenticate Test...............................");
346 
347     /* Check for earlier error.  */
348     if(error_counter)
349     {
350         printf("ERROR!\n");
351         test_control_return(1);
352     }
353 
354     fx_media_format(&ram_disk,
355                     _fx_ram_driver,               // Driver entry
356                     ram_disk_memory,              // RAM disk memory pointer
357                     media_memory,                 // Media buffer pointer
358                     sizeof(media_memory),         // Media buffer size
359                     "MY_RAM_DISK",                // Volume Name
360                     1,                            // Number of FATs
361                     32,                           // Directory Entries
362                     0,                            // Hidden sectors
363                     256,                          // Total sectors
364                     512,                          // Sector size
365                     8,                            // Sectors per cluster
366                     1,                            // Heads
367                     1);                           // Sectors per track
368 
369     /* Open the RAM disk.  */
370     status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, media_memory, sizeof(media_memory)) ;
371     status += fx_file_create(&ram_disk, "TEST.TXT");
372     status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
373     status += fx_file_write(&my_file, "https server", 12);
374     status += fx_file_close(&my_file);
375     if(status)
376         error_counter++;
377 
378     /* Give NetX a chance to initialize the system.  */
379     tx_thread_sleep(NX_IP_PERIODIC_RATE);
380 
381     /* First loop test HTTP, second loop test HTTPS.  */
382     for (i = 0; i < loop; i++)
383     {
384 
385 #ifdef NX_WEB_HTTPS_ENABLE
386         if (i == 1)
387         {
388             server_port = NX_WEB_HTTPS_SERVER_PORT;
389         }
390 #endif
391 
392         /* Create the HTTP Server. */
393         status = nx_web_http_server_create(&my_server, "My HTTP Server", &server_ip, server_port, &ram_disk,
394                                            &server_stack, sizeof(server_stack), &server_pool,
395                                            authentication_check, NX_NULL);
396         if (status)
397             error_counter++;
398 
399 #ifdef NX_WEB_HTTPS_ENABLE
400         /* Set TLS for HTTPS.  */
401         if (i == 1)
402         {
403             /* Initialize device certificate (used for all sessions in HTTPS server).  */
404             memset(&certificate, 0, sizeof(certificate));
405             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);
406 
407             /* Setup TLS session data for the TCP server.  */
408             status = nx_web_http_server_secure_configure(&my_server, &nx_crypto_tls_ciphers,
409                                                          crypto_metadata_server, sizeof(crypto_metadata_server), tls_packet_buffer, sizeof(tls_packet_buffer),
410                                                          &certificate, NX_NULL, 0, NX_NULL, 0, NX_NULL, 0);
411             if (status)
412                 error_counter++;
413         }
414 #endif
415 
416         /* OK to start the HTTP Server.  */
417         status = nx_web_http_server_start(&my_server);
418         if (status)
419             error_counter++;
420 
421         /* Set the flag.  */
422         if (i == 0)
423         {
424             http_server_start = 1;
425 
426             /* Wait HTTP test finished.  */
427             while(!http_client_stop)
428             {
429                 tx_thread_sleep(NX_IP_PERIODIC_RATE);
430             }
431         }
432 #ifdef NX_WEB_HTTPS_ENABLE
433         else
434         {
435             https_server_start = 1;
436 
437             /* Wait HTTPS test finished.  */
438             while(!https_client_stop)
439             {
440                 tx_thread_sleep(NX_IP_PERIODIC_RATE);
441             }
442         }
443 #endif /* NX_WEB_HTTPS_ENABLE  */
444 
445         status = nx_web_http_server_delete(&my_server);
446         if (status)
447             error_counter++;
448     }
449 
450     /* Check packet pool.  */
451     if (server_pool.nx_packet_pool_available != server_pool.nx_packet_pool_total)
452     {
453         error_counter++;
454     }
455 
456     if (client_pool.nx_packet_pool_available != client_pool.nx_packet_pool_total)
457     {
458         error_counter++;
459     }
460 
461     if(error_counter)
462     {
463         printf("ERROR!\n");
464         test_control_return(1);
465     }
466     else
467     {
468         printf("SUCCESS!\n");
469         test_control_return(0);
470     }
471 }
472 
473 /* Define the application's authentication check.  This is called by
474    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)475 static UINT  authentication_check(NX_WEB_HTTP_SERVER *server_ptr, UINT request_type,
476                                   CHAR *resource, CHAR **name, CHAR **password, CHAR **realm)
477 {
478 
479     /* Just use a simple name, password, and realm for all
480        requests and resources.  */
481     *name =     "name";
482     *password = "password";
483     *realm =    "NetX Duo HTTP demo";
484 
485     /* Request basic authentication.  */
486     return(NX_WEB_HTTP_BASIC_AUTHENTICATE);
487 }
488 
489 #else
490 
491 #ifdef CTEST
test_application_define(void * first_unused_memory)492 VOID test_application_define(void *first_unused_memory)
493 #else
494 void    netx_web_basic_authenticate_test_application_define(void *first_unused_memory)
495 #endif
496 {
497 
498     /* Print out test information banner.  */
499     printf("NetX Test:   Web Basic Authenticate Test...............................N/A\n");
500 
501     test_control_return(3);
502 }
503 #endif
504 
505