|
10 | 10 |
|
11 | 11 | #include "port/posix/posix_transport_shm.h" |
12 | 12 | #include "port/posix/posix_transport_tcp.h" |
| 13 | +#ifndef WOLFHSM_CFG_NO_CRYPTO |
| 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 | +#ifndef WOLFHSM_CFG_NO_CRYPTO |
| 22 | +posixTransportTlsClientContext tccTls; |
| 23 | +#endif |
18 | 24 |
|
19 | 25 | posixTransportShmConfig shmConfig; |
20 | 26 | posixTransportTcpConfig tcpConfig; |
| 27 | +#ifndef WOLFHSM_CFG_NO_CRYPTO |
| 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 | +#ifndef WOLFHSM_CFG_NO_CRYPTO |
| 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,166 @@ int wh_PosixClient_ExampleTcpConfig(void* conf) |
123 | 135 | return WH_ERROR_OK; |
124 | 136 | } |
125 | 137 |
|
| 138 | +#ifndef WOLFHSM_CFG_NO_CRYPTO |
| 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 | +static int |
| 144 | +wh_PosixClient_ExampleTlsContextSetup(posixTransportTlsClientContext* ctx) |
| 145 | +{ |
| 146 | + int rc; |
| 147 | + |
| 148 | + /* uncomment and compile with DEBUG_WOLFSSL for debugging */ |
| 149 | + /* wolfSSL_Debugging_ON(); */ |
| 150 | + |
| 151 | + /* Create a new wolfSSL context to use with this connection */ |
| 152 | + ctx->ssl_ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()); |
| 153 | + if (!ctx->ssl_ctx) { |
| 154 | + return WH_ERROR_ABORTED; |
| 155 | + } |
| 156 | + |
| 157 | + /* don't use wolfHSM for TLS crypto when communicating with wolfHSM */ |
| 158 | + wolfSSL_CTX_SetDevId(ctx->ssl_ctx, INVALID_DEVID); |
| 159 | + |
| 160 | + /* Load CA certificate for server verification */ |
| 161 | + rc = wolfSSL_CTX_load_verify_buffer(ctx->ssl_ctx, ca_cert_der_2048, |
| 162 | + sizeof_ca_cert_der_2048, |
| 163 | + CTC_FILETYPE_ASN1); |
| 164 | + if (rc != WOLFSSL_SUCCESS) { |
| 165 | + wolfSSL_CTX_free(ctx->ssl_ctx); |
| 166 | + ctx->ssl_ctx = NULL; |
| 167 | + return WH_ERROR_ABORTED; |
| 168 | + } |
| 169 | + |
| 170 | + rc = wolfSSL_CTX_use_certificate_buffer(ctx->ssl_ctx, client_cert_der_2048, |
| 171 | + sizeof(client_cert_der_2048), |
| 172 | + CTC_FILETYPE_ASN1); |
| 173 | + if (rc != WOLFSSL_SUCCESS) { |
| 174 | + wolfSSL_CTX_free(ctx->ssl_ctx); |
| 175 | + ctx->ssl_ctx = NULL; |
| 176 | + return WH_ERROR_ABORTED; |
| 177 | + } |
| 178 | + |
| 179 | + /* load private key for TLS connection */ |
| 180 | + rc = wolfSSL_CTX_use_PrivateKey_buffer(ctx->ssl_ctx, client_key_der_2048, |
| 181 | + sizeof(client_key_der_2048), |
| 182 | + CTC_FILETYPE_ASN1); |
| 183 | + if (rc != WOLFSSL_SUCCESS) { |
| 184 | + wolfSSL_CTX_free(ctx->ssl_ctx); |
| 185 | + ctx->ssl_ctx = NULL; |
| 186 | + return WH_ERROR_ABORTED; |
| 187 | + } |
| 188 | + /* Set verification mode */ |
| 189 | + wolfSSL_CTX_set_verify(ctx->ssl_ctx, WOLFSSL_VERIFY_PEER, NULL); |
| 190 | + |
| 191 | + return WH_ERROR_OK; |
| 192 | +} |
| 193 | + |
| 194 | +#ifndef NO_PSK |
| 195 | +/* Simple PSK example callback */ |
| 196 | +static unsigned int psk_tls12_client_cb(WOLFSSL* ssl, const char* hint, |
| 197 | + char* identity, unsigned int id_max_len, |
| 198 | + unsigned char* key, |
| 199 | + unsigned int key_max_len) |
| 200 | +{ |
| 201 | + size_t len; |
| 202 | + |
| 203 | + memset(key, 0, key_max_len); |
| 204 | + const char* exampleIdentity = "PSK_EXAMPLE_CLIENT_IDENTITY"; |
| 205 | + |
| 206 | + printf("PSK server identity hint: %s\n", hint); |
| 207 | + printf("PSK using identity: %s\n", exampleIdentity); |
| 208 | + strncpy(identity, exampleIdentity, id_max_len); |
| 209 | + |
| 210 | + printf("Enter PSK password: "); |
| 211 | + if (fgets((char*)key, key_max_len - 1, stdin) == NULL) { |
| 212 | + memset(key, 0, key_max_len); |
| 213 | + return 0U; |
| 214 | + } |
| 215 | + |
| 216 | + (void)ssl; |
| 217 | + len = strcspn((char*)key, "\n"); |
| 218 | + ((char*)key)[len] = '\0'; |
| 219 | + return (unsigned int)len; |
| 220 | +} |
| 221 | + |
| 222 | +/* Setup WOLFSSL_CTX for use with PSK */ |
| 223 | +static int |
| 224 | +wh_PosixClient_ExamplePskContextSetup(posixTransportTlsClientContext* ctx) |
| 225 | +{ |
| 226 | + /* uncomment and compile with DEBUG_WOLFSSL for debugging */ |
| 227 | + /* wolfSSL_Debugging_ON(); */ |
| 228 | + |
| 229 | + /* Create a new wolfSSL context to use with this connection */ |
| 230 | + ctx->ssl_ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()); |
| 231 | + if (!ctx->ssl_ctx) { |
| 232 | + return WH_ERROR_ABORTED; |
| 233 | + } |
| 234 | + |
| 235 | + /* don't use wolfHSM for TLS crypto when communicating with wolfHSM */ |
| 236 | + wolfSSL_CTX_SetDevId(ctx->ssl_ctx, INVALID_DEVID); |
| 237 | + |
| 238 | + wolfSSL_CTX_set_psk_client_callback(ctx->ssl_ctx, psk_tls12_client_cb); |
| 239 | + /* Set verification mode */ |
| 240 | + wolfSSL_CTX_set_verify(ctx->ssl_ctx, WOLFSSL_VERIFY_PEER, NULL); |
| 241 | + |
| 242 | + return WH_ERROR_OK; |
| 243 | +} |
| 244 | +#endif /* NO_PSK */ |
| 245 | + |
| 246 | +static int wh_PosixClient_ExampleTlsCommonConfig(void* conf) |
| 247 | +{ |
| 248 | + whClientConfig* c_conf = (whClientConfig*)conf; |
| 249 | + |
| 250 | + memset(&tccTls, 0, sizeof(posixTransportTlsClientContext)); |
| 251 | + |
| 252 | + /* Initialize TCP context fields that need specific values */ |
| 253 | + tccTls.state = 0; |
| 254 | + tccTls.connect_fd_p1 = 0; /* Invalid fd */ |
| 255 | + tccTls.request_sent = 0; |
| 256 | + tccTls.buffer_offset = 0; |
| 257 | + |
| 258 | + tlsConfig.server_ip_string = WH_POSIX_SERVER_TCP_IPSTRING; |
| 259 | + tlsConfig.server_port = WH_POSIX_SERVER_TCP_PORT; |
| 260 | + tlsConfig.verify_peer = true; |
| 261 | + |
| 262 | + c_comm.transport_cb = &tlsCb; |
| 263 | + c_comm.transport_context = (void*)&tccTls; |
| 264 | + c_comm.transport_config = (void*)&tlsConfig; |
| 265 | + c_comm.client_id = WH_POSIX_CLIENT_ID; |
| 266 | + c_conf->comm = &c_comm; |
| 267 | + |
| 268 | + return WH_ERROR_OK; |
| 269 | +} |
| 270 | + |
| 271 | +int wh_PosixClient_ExampleTlsConfig(void* conf) |
| 272 | +{ |
| 273 | + if (wh_PosixClient_ExampleTlsCommonConfig(conf) != WH_ERROR_OK) { |
| 274 | + return WH_ERROR_ABORTED; |
| 275 | + } |
| 276 | + |
| 277 | + if (wh_PosixClient_ExampleTlsContextSetup(&tccTls) != WH_ERROR_OK) { |
| 278 | + return WH_ERROR_ABORTED; |
| 279 | + } |
| 280 | + return WH_ERROR_OK; |
| 281 | +} |
| 282 | + |
| 283 | +#ifndef NO_PSK |
| 284 | +int wh_PosixClient_ExamplePskConfig(void* conf) |
| 285 | +{ |
| 286 | + if (wh_PosixClient_ExampleTlsCommonConfig(conf) != WH_ERROR_OK) { |
| 287 | + return WH_ERROR_ABORTED; |
| 288 | + } |
| 289 | + |
| 290 | + if (wh_PosixClient_ExamplePskContextSetup(&tccTls) != WH_ERROR_OK) { |
| 291 | + return WH_ERROR_ABORTED; |
| 292 | + } |
| 293 | + return WH_ERROR_OK; |
| 294 | +} |
| 295 | +#endif /* NO_PSK */ |
| 296 | +#endif /* WOLFHSM_CFG_NO_CRYPTO */ |
| 297 | + |
126 | 298 |
|
127 | 299 | /* client configuration setup example for transport */ |
128 | 300 | int wh_PosixClient_ExampleShmConfig(void* conf) |
|
0 commit comments