-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.h
120 lines (96 loc) · 2.91 KB
/
main.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#ifndef MAIN_H
#define MAIN_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
// MbedTLS headers
#include "mbedtls/net_sockets.h"
#include "mbedtls/debug.h"
#include "mbedtls/ssl.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
// PSA Crypto header
#include "psa/crypto.h"
#define SERVER_NAME "www.example.com"
#define SERVER_PORT "443"
#define GET_HEADER "GET /mtls-endpoint?query=1 HTTP/1.1"
#define HOST_HEADER "Host: www.example.com"
#define GET_REQUEST GET_HEADER "\r\n" HOST_HEADER "\r\n\r\n"
#define DEBUG_LEVEL 1
#define MAX_RESPONSE_SIZE 200
// Structure to hold all the context variables for TLS communication
typedef struct {
// Network socket
mbedtls_net_context server_file_descriptor;
// Entropy context for random number generation
mbedtls_entropy_context entropy;
// Deterministic Random Bit Generator
// ctr = Counter
// drbg = Deterministic Random Bit Generator.
mbedtls_ctr_drbg_context ctr_drbg;
// SSL context
mbedtls_ssl_context ssl;
// SSL configuration
mbedtls_ssl_config conf;
// CA certificate
mbedtls_x509_crt ca_cert;
// Client certificate
mbedtls_x509_crt client_cert;
// Private key context
mbedtls_pk_context private_key;
} TLSContext;
// Function prototypes
/**
* @brief Custom debug function to output debug information
*
* @param context Context (usually stdout)
* @param level Debug level
* @param file Source file where the debug message originated
* @param line Line number in the source file
* @param str Debug message
*/
static void my_debug(void *context, int level, const char *file, int line, const char *str);
/**
* @brief Initialise the TLS context and seed the random number generator
*
* @param context Pointer to the TLSContext structure
* @return int 0 on success, non-zero on failure
*/
static int initialise_context(TLSContext *context);
/**
* @brief Load and parse certificates and private key
*
* @param context Pointer to the TLSContext structure
* @return int 0 on success, non-zero on failure
*/
static int setup_certificates(TLSContext *context);
/**
* @brief Set up the network connection and configure SSL settings
*
* @param context Pointer to the TLSContext structure
* @return int 0 on success, non-zero on failure
*/
static int setup_connection(TLSContext *context);
/**
* @brief Perform the SSL/TLS handshake
*
* @param context Pointer to the TLSContext structure
* @return int 0 on success, non-zero on failure
*/
static int perform_handshake(TLSContext *context);
/**
* @brief Exchange data with the server
*
* @param context Pointer to the TLSContext structure
* @return int 0 on success, non-zero on failure
*/
static int exchange_data(TLSContext *context);
/**
* @brief Clean up and free resources
*
* @param context Pointer to the TLSContext structure
*/
static void cleanup(TLSContext *context);
#endif //MAIN_H