|
10 | 10 |
|
11 | 11 | #include "port/posix/posix_transport_shm.h" |
12 | 12 | #include "port/posix/posix_transport_tcp.h" |
| 13 | +#ifdef WOLFHSM_CFG_TLS |
| 14 | +#include "port/posix/posix_transport_tls.h" |
| 15 | +#endif |
13 | 16 |
|
14 | 17 | #include <string.h> |
15 | 18 |
|
16 | 19 | posixTransportShmClientContext tccShm; |
17 | 20 | posixTransportTcpClientContext tccTcp; |
| 21 | +#ifdef WOLFHSM_CFG_TLS |
| 22 | +posixTransportTlsClientContext tccTls; |
| 23 | +#endif |
18 | 24 |
|
19 | 25 | posixTransportShmConfig shmConfig; |
20 | 26 | posixTransportTcpConfig tcpConfig; |
| 27 | +#ifdef WOLFHSM_CFG_TLS |
| 28 | +posixTransportTlsConfig tlsConfig; |
| 29 | +#endif |
21 | 30 |
|
22 | 31 | whCommClientConfig c_comm; |
23 | 32 |
|
24 | 33 | whTransportClientCb shmCb = POSIX_TRANSPORT_SHM_CLIENT_CB; |
25 | 34 | whTransportClientCb tcpCb = PTT_CLIENT_CB; |
| 35 | +#ifdef WOLFHSM_CFG_TLS |
| 36 | +whTransportClientCb tlsCb = PTTLS_CLIENT_CB; |
| 37 | +#endif |
26 | 38 |
|
27 | 39 | #ifdef WOLFSSL_STATIC_MEMORY |
28 | 40 | whTransportClientCb dmaCb = POSIX_TRANSPORT_SHM_CLIENT_CB; |
@@ -123,6 +135,85 @@ int wh_PosixClient_ExampleTcpConfig(void* conf) |
123 | 135 | return WH_ERROR_OK; |
124 | 136 | } |
125 | 137 |
|
| 138 | +#if defined(WOLFHSM_CFG_TLS) |
| 139 | +/* client configuration setup example for TLS transport */ |
| 140 | +#undef USE_CERT_BUFFERS_2048 |
| 141 | +#define USE_CERT_BUFFERS_2048 |
| 142 | +#include "wolfssl/certs_test.h" |
| 143 | + |
| 144 | +int wh_PosixClient_ExampleTlsConfig(void* conf) |
| 145 | +{ |
| 146 | + whClientConfig* c_conf = (whClientConfig*)conf; |
| 147 | + |
| 148 | + memset(&tccTls, 0, sizeof(posixTransportTlsClientContext)); |
| 149 | + |
| 150 | + /* Initialize TLS context fields that need specific values */ |
| 151 | + tccTls.state = 0; |
| 152 | + tccTls.connect_fd_p1 = 0; /* Invalid fd */ |
| 153 | + |
| 154 | + tlsConfig.server_ip_string = WH_POSIX_SERVER_TCP_IPSTRING; |
| 155 | + tlsConfig.server_port = WH_POSIX_SERVER_TCP_PORT; |
| 156 | + tlsConfig.disable_peer_verification = false; |
| 157 | + |
| 158 | + tlsConfig.ca_cert = ca_cert_der_2048; |
| 159 | + tlsConfig.ca_cert_len = sizeof_ca_cert_der_2048; |
| 160 | + tlsConfig.cert = client_cert_der_2048; |
| 161 | + tlsConfig.cert_len = sizeof_client_cert_der_2048; |
| 162 | + tlsConfig.key = client_key_der_2048; |
| 163 | + tlsConfig.key_len = sizeof_client_key_der_2048; |
| 164 | + tlsConfig.heap_hint = NULL; |
| 165 | + |
| 166 | + c_comm.transport_cb = &tlsCb; |
| 167 | + c_comm.transport_context = (void*)&tccTls; |
| 168 | + c_comm.transport_config = (void*)&tlsConfig; |
| 169 | + c_comm.client_id = WH_POSIX_CLIENT_ID; |
| 170 | + c_conf->comm = &c_comm; |
| 171 | + |
| 172 | + return WH_ERROR_OK; |
| 173 | +} |
| 174 | + |
| 175 | + |
| 176 | +#ifndef NO_PSK |
| 177 | +/* Simple PSK example callback */ |
| 178 | +static unsigned int psk_tls12_client_cb(WOLFSSL* ssl, const char* hint, |
| 179 | + char* identity, unsigned int id_max_len, |
| 180 | + unsigned char* key, |
| 181 | + unsigned int key_max_len) |
| 182 | +{ |
| 183 | + size_t len; |
| 184 | + |
| 185 | + memset(key, 0, key_max_len); |
| 186 | + const char* exampleIdentity = "PSK_EXAMPLE_CLIENT_IDENTITY"; |
| 187 | + |
| 188 | + printf("PSK server identity hint: %s\n", hint); |
| 189 | + printf("PSK using identity: %s\n", exampleIdentity); |
| 190 | + strncpy(identity, exampleIdentity, id_max_len); |
| 191 | + |
| 192 | + printf("Enter PSK password: "); |
| 193 | + if (fgets((char*)key, key_max_len - 1, stdin) == NULL) { |
| 194 | + memset(key, 0, key_max_len); |
| 195 | + return 0U; |
| 196 | + } |
| 197 | + |
| 198 | + (void)ssl; |
| 199 | + len = strcspn((char*)key, "\n"); |
| 200 | + ((char*)key)[len] = '\0'; |
| 201 | + return (unsigned int)len; |
| 202 | +} |
| 203 | + |
| 204 | + |
| 205 | +int wh_PosixClient_ExamplePskConfig(void* conf) |
| 206 | +{ |
| 207 | + if (wh_PosixClient_ExampleTlsConfig(conf) != WH_ERROR_OK) { |
| 208 | + return WH_ERROR_ABORTED; |
| 209 | + } |
| 210 | + tlsConfig.psk_client_cb = psk_tls12_client_cb; |
| 211 | + |
| 212 | + return WH_ERROR_OK; |
| 213 | +} |
| 214 | +#endif /* NO_PSK */ |
| 215 | +#endif /* WOLFHSM_CFG_TLS */ |
| 216 | + |
126 | 217 |
|
127 | 218 | /* client configuration setup example for transport */ |
128 | 219 | int wh_PosixClient_ExampleShmConfig(void* conf) |
|
0 commit comments