Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_network_ssl_hardened.c File Reference
#include "nilorea/n_list.h"
#include "nilorea/n_str.h"
#include "nilorea/n_log.h"
#include "nilorea/n_network.h"
#include "nilorea/n_thread_pool.h"
#include "nilorea/n_signals.h"
+ Include dependency graph for ex_network_ssl_hardened.c:

Go to the source code of this file.

Data Structures

struct  NETWORK_SSL_THREAD_PARAMS
 structure of a NETWORK_SSL_THREAD_PARAMS More...
 

Macros

#define CONNECTION_TIMEOUT_SECONDS   10
 
#define MAX_HTTP_REQUEST_SIZE_LIMIT   1048576
 
#define SECURITY_HEADERS
 Network for managing connections.
 

Functions

void action_on_sig (int recvd_signal)
 
int build_secure_response (N_STR **response, int status_code, const char *content_type, char *extra_headers, N_STR *body)
 
void handle_request (NETWORK *netw_ptr, LIST *routes_ptr)
 
int main (int argc, char *argv[])
 
void process_args (int argc_nb, char **argv_ptr, char **addr_ptr, char **port_ptr, char **key_ptr, char **cert_ptr, char **ca_file_ptr, int *ssl_verify_ptr, LIST *routes_ptr, int *ip_version_ptr, int *max_http_request_size_ptr, char **root_dir_ptr)
 
int set_socket_timeout (SOCKET sock, int timeout_seconds)
 Set socket read/write timeout to prevent slow-loris attacks.
 
int ssl_harden_context (NETWORK *ssl_netw)
 Harden the SSL context with modern security settings.
 
void * ssl_network_thread (void *params)
 
void usage (void)
 
int validate_path (const char *requested_path, const char *root, char *resolved, size_t resolved_size)
 Validate that a resolved path stays within the document root.
 

Variables

char * addr = NULL
 
char * ca_file = NULL
 
char * cert = NULL
 
bool done = 0
 
int ip_version = 0
 
char * key = NULL
 
int max_connections = 0
 
int max_http_request_size = 16384
 
NETWORKnetw = NULL
 Network for server mode, accepting incomming.
 
char * port = NULL
 
char * root_dir = NULL
 
LISTroutes = NULL
 
NETWORKserver = NULL
 
int ssl_verify = 0
 

Data Structure Documentation

◆ NETWORK_SSL_THREAD_PARAMS

struct NETWORK_SSL_THREAD_PARAMS

structure of a NETWORK_SSL_THREAD_PARAMS

Examples
ex_network_ssl.c, and ex_network_ssl_hardened.c.

Definition at line 412 of file ex_network_ssl.c.

+ Collaboration diagram for NETWORK_SSL_THREAD_PARAMS:
Data Fields
NETWORK * netw network to use for the receiving thread
LIST * routes virtual routes for the server

Macro Definition Documentation

◆ CONNECTION_TIMEOUT_SECONDS

#define CONNECTION_TIMEOUT_SECONDS   10
Examples
ex_network_ssl_hardened.c.

Definition at line 56 of file ex_network_ssl_hardened.c.

◆ MAX_HTTP_REQUEST_SIZE_LIMIT

#define MAX_HTTP_REQUEST_SIZE_LIMIT   1048576
Examples
ex_network_ssl_hardened.c.

Definition at line 55 of file ex_network_ssl_hardened.c.

◆ SECURITY_HEADERS

#define SECURITY_HEADERS
Value:
"Strict-Transport-Security: max-age=31536000\r\n" \
"X-Content-Type-Options: nosniff\r\n" \
"X-Frame-Options: DENY\r\n" \
"Content-Security-Policy: default-src 'self'\r\n" \
"Referrer-Policy: strict-origin-when-cross-origin\r\n" \
"Permissions-Policy: geolocation=(), camera=(), microphone=()\r\n"

Network for managing connections.

security headers appended to every response

Examples
ex_network_ssl_hardened.c.

Definition at line 65 of file ex_network_ssl_hardened.c.

Function Documentation

◆ action_on_sig()

void action_on_sig ( int  recvd_signal)

Definition at line 375 of file ex_network_ssl_hardened.c.

References done, LOG_ERR, LOG_NOTICE, and n_log.

◆ build_secure_response()

int build_secure_response ( N_STR **  response,
int  status_code,
const char *  content_type,
char *  extra_headers,
N_STR body 
)
Examples
ex_network_ssl_hardened.c.

Definition at line 425 of file ex_network_ssl_hardened.c.

References netw_build_http_response(), and SECURITY_HEADERS.

Referenced by handle_request().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handle_request()

◆ main()

◆ process_args()

void process_args ( int  argc_nb,
char **  argv_ptr,
char **  addr_ptr,
char **  port_ptr,
char **  key_ptr,
char **  cert_ptr,
char **  ca_file_ptr,
int *  ssl_verify_ptr,
LIST routes_ptr,
int *  ip_version_ptr,
int *  max_http_request_size_ptr,
char **  root_dir_ptr 
)

◆ set_socket_timeout()

int set_socket_timeout ( SOCKET  sock,
int  timeout_seconds 
)

Set socket read/write timeout to prevent slow-loris attacks.

Parameters
sockthe socket file descriptor
timeout_secondstimeout in seconds
Returns
TRUE on success, FALSE on failure
Examples
ex_network_ssl_hardened.c.

Definition at line 298 of file ex_network_ssl_hardened.c.

References LOG_ERR, and n_log.

Referenced by handle_request().

+ Here is the caller graph for this function:

◆ ssl_harden_context()

int ssl_harden_context ( NETWORK ssl_netw)

Harden the SSL context with modern security settings.

Applied after netw_set_crypto/netw_set_crypto_chain to tighten TLS configuration:

  • Enforce TLS 1.2 minimum
  • Restrict cipher suites to strong AEAD ciphers with forward secrecy
  • Disable compression (CRIME attack)
  • Server cipher preference
  • Disable session tickets for forward secrecy
  • Configure ECDH curves
Parameters
ssl_netwthe network whose SSL_CTX to harden
Returns
TRUE on success, FALSE on failure
Examples
ex_network_ssl_hardened.c.

Definition at line 234 of file ex_network_ssl_hardened.c.

References __n_assert, NETWORK::ctx, LOG_ERR, LOG_INFO, and n_log.

Referenced by main().

+ Here is the caller graph for this function:

◆ ssl_network_thread()

void * ssl_network_thread ( void *  params)

Definition at line 643 of file ex_network_ssl_hardened.c.

References __n_assert, Free, handle_request(), NETWORK_SSL_THREAD_PARAMS::netw, netw_close(), and NETWORK_SSL_THREAD_PARAMS::routes.

+ Here is the call graph for this function:

◆ usage()

void usage ( void  )

Definition at line 73 of file ex_network_ssl_hardened.c.

References max_http_request_size, and MAX_HTTP_REQUEST_SIZE_LIMIT.

◆ validate_path()

int validate_path ( const char *  requested_path,
const char *  root,
char *  resolved,
size_t  resolved_size 
)

Validate that a resolved path stays within the document root.

Resolves the requested path using realpath() and checks that it starts with the resolved root directory, preventing path traversal attacks (e.g., GET /../../etc/passwd).

Parameters
requested_paththe filesystem path constructed from the URL
rootthe document root directory
resolvedoutput buffer for the validated absolute path
resolved_sizesize of the output buffer
Returns
TRUE if the path is safe, FALSE if it escapes the root
Examples
ex_network_ssl_hardened.c.

Definition at line 338 of file ex_network_ssl_hardened.c.

References LOG_ERR, and n_log.

Referenced by handle_request().

+ Here is the caller graph for this function:

Variable Documentation

◆ addr

char* addr = NULL

Definition at line 47 of file ex_network_ssl_hardened.c.

◆ ca_file

char* ca_file = NULL

Definition at line 50 of file ex_network_ssl_hardened.c.

◆ cert

char* cert = NULL

Definition at line 49 of file ex_network_ssl_hardened.c.

◆ done

bool done = 0

Definition at line 59 of file ex_network_ssl_hardened.c.

◆ ip_version

int ip_version = 0

Definition at line 53 of file ex_network_ssl_hardened.c.

◆ key

char* key = NULL

Definition at line 48 of file ex_network_ssl_hardened.c.

◆ max_connections

int max_connections = 0

Definition at line 58 of file ex_network_ssl_hardened.c.

◆ max_http_request_size

int max_http_request_size = 16384

Definition at line 54 of file ex_network_ssl_hardened.c.

◆ netw

NETWORK * netw = NULL

Network for server mode, accepting incomming.

Definition at line 62 of file ex_network_ssl_hardened.c.

◆ port

char* port = NULL

Definition at line 46 of file ex_network_ssl_hardened.c.

◆ root_dir

char* root_dir = NULL

Definition at line 51 of file ex_network_ssl_hardened.c.

◆ routes

LIST* routes = NULL

Definition at line 52 of file ex_network_ssl_hardened.c.

◆ server

NETWORK* server = NULL

Definition at line 61 of file ex_network_ssl_hardened.c.

◆ ssl_verify

int ssl_verify = 0

Definition at line 57 of file ex_network_ssl_hardened.c.