-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.c
560 lines (463 loc) · 18.9 KB
/
Client.c
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//==============================================================================
///
/// @file Client.c
///
///
/// @brief A test TLS client using several TLS libraries.
///
///
/// MIT License
///
/// Copyright (c) 2023 Rockwell Automation, Inc.
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all
/// copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
/// SOFTWARE.
///
//==============================================================================
//------------------------------------------------------------------------------
// Include files
//------------------------------------------------------------------------------
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <tlsbm/Environment.h> // Generated header, look into CMake.
#include "include/tlsbm/Common.h"
#include "include/tlsbm/Platform.h"
#include "include/tlsbm/TlsLib.h"
//-----------------------------------------------------------------------------
// Constants
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Macros
//-----------------------------------------------------------------------------
#define TLSBM_CLIENT_FLAG_NO_BASIC 0x01
#define TLSBM_CLIENT_FLAG_NO_1BY1 0x02
#define TLSBM_CLIENT_FLAG_NO_ATONCE 0x04
#define TLSBM_CLIENT_TESTSTR(payload, flags)\
tlsbm_Client_SendTestMsg(sd, pSecureSocketCtx, payload, sizeof(payload) - 1, flags)
#define TLSBM_CLIENT_TESTMSG(payloadBytes, payload, flags)\
tlsbm_Client_SendTestMsg(sd, pSecureSocketCtx, payload, payloadBytes, flags)
//-----------------------------------------------------------------------------
// Local data types
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Local constants
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Global references
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Forward function declarations
//-----------------------------------------------------------------------------
static void tlsbm_Client_TlsClient(uint32_t ip4Addr,
uint16_t port,
SuiteCfg_t* pSc);
static void tlsbm_Client_DtlsClient(uint32_t ip4Addr,
uint16_t port,
SuiteCfg_t* pSc);
//-----------------------------------------------------------------------------
// Variable definitions
//-----------------------------------------------------------------------------
static Msg_t tlsbm_Client_msg;
static MsgDtls_t tlsbm_Client_dtlsMsg;
//-----------------------------------------------------------------------------
// Function definitions
//-----------------------------------------------------------------------------
static void tlsbm_Client_SendTestMsg(int sd,
void* pSecureSocketCtx,
uint8_t* pData,
size_t dataBytes)
{
tlsbm_Client_msg.str.payloadBytesBe = TLSBM_Platform_Htons((uint16_t)dataBytes);
tlsbm_Client_msg.str.zero = 0;
memcpy(tlsbm_Client_msg.str.payload, pData, dataBytes);
printf("Testing following message (%u bytes):\n%s\n",
(unsigned int)dataBytes,
pData);
TLSBM_TlsLib_SendAll(sd,
pSecureSocketCtx,
tlsbm_Client_msg.msg,
dataBytes+2);
}
static void tlsbm_Client_SendToTestMsg(int sd,
void* pSecureSocketCtx,
uint32_t serverIp4,
uint16_t port,
uint8_t* pData,
size_t dataBytes)
{
tlsbm_Client_dtlsMsg.str.payloadBytesBe = TLSBM_Platform_Htons((uint16_t)dataBytes);
tlsbm_Client_dtlsMsg.str.zero = 0;
memcpy(tlsbm_Client_dtlsMsg.str.payload, pData, dataBytes);
printf("Testing following message (%u bytes):\n%s\n",
(unsigned int)dataBytes,
pData);
TLSBM_TlsLib_SendToAll(sd,
pSecureSocketCtx,
serverIp4,
port,
tlsbm_Client_dtlsMsg.msg,
dataBytes+2);
}
//-----------------------------------------------------------------------------
///
/// @brief main client function, reads inputBuffer from stdin and send it to the SSL server
///
/// @param[in] pServerIp4 - server IP
/// @param[in] port - comm port
/// @param[in] pCertDirPath - path to certificates
/// @return 0 on success
///
//-----------------------------------------------------------------------------
static void tlsbm_Client_TlsClient(uint32_t ip4Addr,
uint16_t port,
SuiteCfg_t* pSc)
{
TLSBM_Common_AllocLogMarkerBegin("Context");
uint8_t* pAllocaHint = TLSBM_Common_Allocacheck();
#if defined(TLSBM_ENV_DEBUG_ENABLE)
printf("Connecting server\n");
#endif // defined(TLSBM_ENV_DEBUG_ENABLE)
int sd = TLSBM_Platform_Socket(true);
if (sd == -1) // INVALID_SOCKET undef in Unix
{
TLSBM_Common_Die("can't get sd");
}
if (TLSBM_Platform_Connect(sd, ip4Addr, port) == -1)
{
TLSBM_Common_Die("sd connect failed");
}
#if defined(TLSBM_ENV_DEBUG_ENABLE)
printf("Server %d:%d connected\n", ip4Addr, port);
#endif // defined(TLSBM_ENV_DEBUG_ENABLE)
printf("TLS: Picking %s isEcc == %d\n",
pSc->pCipherSuite,
pSc->isEcc);
void* pSecurityCtx = NULL;
if (pSc->isEcc)
{
// ECC
pSecurityCtx = TLSBM_TlsLib_CreateSecurityContext(false,
TLSBM_CACERT_ECC_PATH,
TLSLIB_FILE_TYPE_PEM,
TLSBM_DEVCERT_ECC_PATH,
TLSLIB_FILE_TYPE_PEM,
TLSBM_DEVKEY_ECC_PATH,
TLSLIB_FILE_TYPE_DER,
pSc->pCipherSuite,
true);
}
else
{
// RSA
pSecurityCtx = TLSBM_TlsLib_CreateSecurityContext(false,
TLSBM_CACERT_RSA_PATH,
TLSLIB_FILE_TYPE_PEM,
TLSBM_DEVCERT_RSA_PATH,
TLSLIB_FILE_TYPE_PEM,
TLSBM_DEVKEY_RSA_PATH,
TLSLIB_FILE_TYPE_DER,
pSc->pCipherSuite,
true);
}
TLSBM_Common_AllocLogMarkerEnd("Context");
TLSBM_Common_AllocLogMarkerBegin("Handshake");
void* pSecureSocketCtx = TLSBM_TlsLib_MakeSocketSecure(sd, pSecurityCtx);
TLSBM_TlsLib_ClientHandshake(sd, pSecureSocketCtx);
TLSBM_Common_AllocLogMarkerEnd("Handshake");
TLSBM_Common_AllocLogMarkerBegin("Message");
// Let's test!
#if defined(TLSBM_ENV_DEBUG_ENABLE)
printf("Hello world test\n");
#endif // defined(TLSBM_ENV_DEBUG_ENABLE)
tlsbm_Client_SendTestMsg(sd,
pSecureSocketCtx,
"Hello world",
sizeof("Hello world")-1);
TLSBM_Common_AllocLogMarkerEnd("Message");
TLSBM_TlsLib_UnmakeSocketSecure(sd, pSecureSocketCtx);
TLSBM_Platform_CloseSocket(sd);
TLSBM_TlsLib_DestroySecureContext(pSecurityCtx);
TLSBM_Common_Allocaprint(pAllocaHint);
TLSBM_Platform_FlushStdout();
} // End: tlsbm_Client_TlsClient()
//-----------------------------------------------------------------------------
///
/// @brief main client function, reads inputBuffer from stdin and send it to the SSL server
///
/// @param[in] pServerIp4 - server IP
/// @param[in] port - comm port
/// @param[in] pCertDirPath - path to certificates
/// @return 0 on success
///
//-----------------------------------------------------------------------------
static void tlsbm_Client_DtlsClient(uint32_t ip4Addr,
uint16_t port,
SuiteCfg_t* pSc)
{
TLSBM_Common_AllocLogMarkerBegin("Context");
uint8_t* pAllocaHint = TLSBM_Common_Allocacheck();
int sd = TLSBM_Platform_Socket(false);
if (sd == -1) // INVALID_SOCKET undef in Unix
{
TLSBM_Common_Die("can't get sd");
}
printf("DTLS: Picking %s isEcc == %d\n",
pSc->pCipherSuite,
pSc->isEcc);
void* pSecurityCtx = NULL;
if (pSc->isEcc)
{
// ECC
pSecurityCtx = TLSBM_TlsLib_CreateSecurityContext(false,
TLSBM_CACERT_ECC_PATH,
TLSLIB_FILE_TYPE_PEM,
TLSBM_DEVCERT_ECC_PATH,
TLSLIB_FILE_TYPE_PEM,
TLSBM_DEVKEY_ECC_PATH,
TLSLIB_FILE_TYPE_DER,
pSc->pCipherSuite,
false);
}
else
{
// RSA
pSecurityCtx = TLSBM_TlsLib_CreateSecurityContext(false,
TLSBM_CACERT_RSA_PATH,
TLSLIB_FILE_TYPE_PEM,
TLSBM_DEVCERT_RSA_PATH,
TLSLIB_FILE_TYPE_PEM,
TLSBM_DEVKEY_RSA_PATH,
TLSLIB_FILE_TYPE_DER,
pSc->pCipherSuite,
false);
}
TLSBM_Common_AllocLogMarkerEnd("Context");
TLSBM_Common_AllocLogMarkerBegin("Handshake");
size_t peerAddrSize = 0;
void* pPeerAddr = TLSBM_Platform_CreatePeerAddr4(&peerAddrSize, ip4Addr, port);
void* pSecureSocketCtx = TLSBM_TlsLib_MakeDtlsSocketSecure(&sd,
pSecurityCtx,
pPeerAddr,
peerAddrSize);
TLSBM_TlsLib_ClientHandshake(sd, pSecureSocketCtx);
TLSBM_Common_AllocLogMarkerEnd("Handshake");
TLSBM_Common_AllocLogMarkerBegin("Message");
// Let's test!
#if defined(TLSBM_ENV_DEBUG_ENABLE)
printf("Hello world test\n");
#endif // defined(TLSBM_ENV_DEBUG_ENABLE)
tlsbm_Client_SendToTestMsg(sd,
pSecureSocketCtx,
ip4Addr,
port,
"Hello world",
sizeof("Hello world")-1);
TLSBM_Common_AllocLogMarkerEnd("Message");
TLSBM_TlsLib_UnmakeSocketSecure(sd, pSecureSocketCtx);
TLSBM_Platform_CloseSocket(sd);
TLSBM_TlsLib_DestroySecureContext(pSecurityCtx);
TLSBM_Platform_DeletePeerAddr4(pPeerAddr);
TLSBM_Common_Allocaprint(pAllocaHint);
TLSBM_Platform_FlushStdout();
} // End: tlsbm_Client_DtlsClient()
//------------------------------------------------------------------------------
///
/// @brief Entry point for the simple TLS Client
///
//------------------------------------------------------------------------------
int main(int argc, char** argv)
{
TLSBM_Platform_Startup();
TLSBM_Common_Startup("client", TLSBM_TlsLib_GetName());
TLSBM_TlsLib_Startup();
uint16_t port = SIMPLE_SSL_PORT;
char* pServerIp4;
if (argc == 2)
{
// use argv[1] as server IP
pServerIp4 = argv[1];
}
else
{
// tell user, server IP can be set
printf("USAGE: <tlsbm-XX-client.exe> [server_ip]\n");
exit(-1);
}
uint32_t ip4Addr = TLSBM_Platform_GetIp4Addr(pServerIp4);
TLSBM_Common_SetIp4Port(ip4Addr, port);
char testName[128];
for (int id = 0; ;id++)
{
SuiteCfg_t* pSc = TLSBM_Common_GetSuiteCfg(id);
if (pSc != NULL)
{
size_t wouldBeWritten = snprintf(testName, sizeof(testName),
"Test: TLS %s",
pSc->pCipherSuite);
if (wouldBeWritten > sizeof(testName))
{
TLSBM_Common_Die("cannot write test marker");
}
TLSBM_Common_AllocLogMarkerBegin(testName);
tlsbm_Client_TlsClient(ip4Addr, port, pSc);
TLSBM_Common_AllocLogMarkerEnd(testName);
TLSBM_Platform_Sleep(1);
}
else
{
break;
}
}
for (int id = 0; ;id++)
{
SuiteCfg_t* pSc = TLSBM_Common_GetSuiteCfg(id);
if (pSc != NULL)
{
size_t wouldBeWritten = snprintf(testName, sizeof(testName),
"Test: DTLS %s",
pSc->pCipherSuite);
if (wouldBeWritten > sizeof(testName))
{
TLSBM_Common_Die("cannot write test marker");
}
TLSBM_Common_AllocLogMarkerBegin(testName);
tlsbm_Client_DtlsClient(ip4Addr, port, pSc);
TLSBM_Common_AllocLogMarkerEnd(testName);
TLSBM_Platform_Sleep(1);
}
else
{
break;
}
}
printf("FINISHED\n");
TLSBM_TlsLib_Shutdown();
TLSBM_Common_Shutdown();
TLSBM_Platform_Shutdown();
return 0;
} // End: main()
#if 0
TLSBM_CLIENT_TESTSTR("Hello", 0);
TLSBM_CLIENT_TESTSTR("Testing Testing", 0);
printf("Test case 1: Smallest message\n");
TLSBM_CLIENT_TESTSTR("", 0);
printf("Test case 2: Largest message\n");
tlsbm_Client_msg.str.payloadBytesBe = UINT16_MAX;
for (uint16_t i = 0; i < UINT16_MAX; i++)
{
tlsbm_Client_msg.str.payload[i] = 'a';
}
for (uint32_t i = 0; i < UINT16_MAX+2; i++)
{
printf("%c\n", tlsbm_Client_msg.msg[i]);
}
printf("\t HAPPY\n");
TLSBM_TlsLib_SendAll(sd,
pSecurityCtx,
tlsbm_Client_msg.str.payload,
TLSBM_Platform_Ntohs(tlsbm_Client_msg.str.payloadBytesBe));
printf("\t one-by-one\n");
TLSBM_TlsLib_SendOneByOneByte(sd,
pSecurityCtx,
tlsbm_Client_msg.str.payload,
TLSBM_Platform_Ntohs(tlsbm_Client_msg.str.payloadBytesBe));
printf("\t at once!\n");
TLSBM_TlsLib_SendAllInOne(sd,
pSecurityCtx,
tlsbm_Client_msg.str.payload,
TLSBM_Platform_Ntohs(tlsbm_Client_msg.str.payloadBytesBe));
printf("\t DONE!\n");
printf("Test case 3: Zeroes and newlines\n");
tlsbm_Client_msg.str.payloadBytesBe = TLSBM_Platform_Htons(4);
tlsbm_Client_msg.str.payload[0] = '0';
tlsbm_Client_msg.str.payload[1] = '\0';
tlsbm_Client_msg.str.payload[2] = 'n';
tlsbm_Client_msg.str.payload[3] = '\n';
printf("\t HAPPY\n");
TLSBM_TlsLib_SendAll(sd,
pSecurityCtx,
tlsbm_Client_msg.str.payload,
TLSBM_Platform_Ntohs(tlsbm_Client_msg.str.payloadBytesBe));
printf("\t one-by-one\n");
TLSBM_TlsLib_SendOneByOneByte(sd,
pSecurityCtx,
tlsbm_Client_msg.str.payload,
TLSBM_Platform_Ntohs(tlsbm_Client_msg.str.payloadBytesBe));
printf("\t at once!\n");
TLSBM_TlsLib_SendAllInOne(sd,
pSecurityCtx,
tlsbm_Client_msg.str.payload,
TLSBM_Platform_Ntohs(tlsbm_Client_msg.str.payloadBytesBe));
printf("\t DONE!\n");
static void tlsbm_Client_SendTestMsg(int sd,
void* pSecureSocketCtx,
uint8_t* pData,
size_t dataBytes,
uint8_t flags)
{
tlsbm_Client_msg.str.payloadBytesBe = TLSBM_Platform_Htons((uint16_t)dataBytes);
tlsbm_Client_msg.str.zero = 0;
memcpy(tlsbm_Client_msg.str.payload, pData, dataBytes);
printf("Testing following message (%u bytes):\n%s\n",
(unsigned int)dataBytes,
pData);
if ((flags & TLSBM_CLIENT_FLAG_NO_BASIC) == 0)
{
printf("Basic test running.\n");
TLSBM_TlsLib_SendAll(sd,
pSecureSocketCtx,
tlsbm_Client_msg.msg,
dataBytes+2);
printf("Basic test DONE.\n");
}
else
{
printf("Basic test disabled.\n");
}
if ((flags & TLSBM_CLIENT_FLAG_NO_1BY1) == 0)
{
printf("1-by-1 test running.\n");
TLSBM_TlsLib_SendOneByOneByte(sd,
pSecureSocketCtx,
tlsbm_Client_msg.msg,
dataBytes+2);
printf("1-by-1 test DONE.\n");
}
else
{
printf("1-by-1 test disabled.\n");
}
if ((flags & TLSBM_CLIENT_FLAG_NO_ATONCE) == 0)
{
printf("All-at-once test running.\n");
TLSBM_TlsLib_SendAllInOne(sd,
pSecureSocketCtx,
tlsbm_Client_msg.msg,
dataBytes+2);
printf("All-at-once test DONE.\n");
}
else
{
printf("All-at-once test disabled.\n");
}
}
#endif