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
|
/*
* Ouroboros - Copyright (C) 2016 - 2026
*
* Common test helper functions for OAP tests
*
* Dimitri Staessens <dimitri@ouroboros.rocks>
* Sander Vrijders <sander@ouroboros.rocks>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., http://www.fsf.org/about/contact/.
*/
#include "common.h"
#include <ouroboros/crypt.h>
#include "oap.h"
#include <string.h>
#include <stdio.h>
int load_srv_kex_config(const struct name_info * info,
struct sec_config * cfg)
{
(void) info;
memset(cfg, 0, sizeof(*cfg));
if (test_cfg.srv.kex == NID_undef)
return 0;
SET_KEX_ALGO_NID(cfg, test_cfg.srv.kex);
SET_KEX_CIPHER_NID(cfg, test_cfg.srv.cipher);
SET_KEX_KDF_NID(cfg, test_cfg.srv.kdf);
SET_KEX_DIGEST_NID(cfg, test_cfg.srv.md);
SET_KEX_KEM_MODE(cfg, test_cfg.srv.kem_mode);
return 0;
}
int load_cli_kex_config(const struct name_info * info,
struct sec_config * cfg)
{
(void) info;
memset(cfg, 0, sizeof(*cfg));
if (test_cfg.cli.kex == NID_undef)
return 0;
SET_KEX_ALGO_NID(cfg, test_cfg.cli.kex);
SET_KEX_CIPHER_NID(cfg, test_cfg.cli.cipher);
SET_KEX_KDF_NID(cfg, test_cfg.cli.kdf);
SET_KEX_DIGEST_NID(cfg, test_cfg.cli.md);
SET_KEX_KEM_MODE(cfg, test_cfg.cli.kem_mode);
return 0;
}
int load_srv_credentials(const struct name_info * info,
void ** pkp,
void ** crt)
{
(void) info;
*pkp = NULL;
*crt = NULL;
if (!test_cfg.srv.auth)
return 0;
return mock_load_credentials(pkp, crt);
}
int load_cli_credentials(const struct name_info * info,
void ** pkp,
void ** crt)
{
(void) info;
*pkp = NULL;
*crt = NULL;
if (!test_cfg.cli.auth)
return 0;
return mock_load_credentials(pkp, crt);
}
int oap_test_setup(struct oap_test_ctx * ctx,
const char * root_ca_str,
const char * im_ca_str)
{
memset(ctx, 0, sizeof(*ctx));
strcpy(ctx->srv.info.name, "test-1.unittest.o7s");
strcpy(ctx->cli.info.name, "test-1.unittest.o7s");
if (oap_auth_init() < 0) {
printf("Failed to init OAP.\n");
goto fail_init;
}
if (crypt_load_crt_str(root_ca_str, &ctx->root_ca) < 0) {
printf("Failed to load root CA cert.\n");
goto fail_root_ca;
}
if (crypt_load_crt_str(im_ca_str, &ctx->im_ca) < 0) {
printf("Failed to load intermediate CA cert.\n");
goto fail_im_ca;
}
if (oap_auth_add_ca_crt(ctx->root_ca) < 0) {
printf("Failed to add root CA cert to store.\n");
goto fail_add_ca;
}
if (oap_auth_add_ca_crt(ctx->im_ca) < 0) {
printf("Failed to add intermediate CA cert to store.\n");
goto fail_add_ca;
}
return 0;
fail_add_ca:
crypt_free_crt(ctx->im_ca);
fail_im_ca:
crypt_free_crt(ctx->root_ca);
fail_root_ca:
oap_auth_fini();
fail_init:
memset(ctx, 0, sizeof(*ctx));
return -1;
}
void oap_test_teardown(struct oap_test_ctx * ctx)
{
struct crypt_sk res;
buffer_t dummy = BUF_INIT;
if (ctx->cli.state != NULL) {
res.key = ctx->cli.key;
oap_cli_complete(ctx->cli.state, &ctx->cli.info, dummy,
&ctx->data, &res);
ctx->cli.state = NULL;
}
freebuf(ctx->data);
freebuf(ctx->resp_hdr);
freebuf(ctx->req_hdr);
crypt_free_crt(ctx->im_ca);
crypt_free_crt(ctx->root_ca);
oap_auth_fini();
memset(ctx, 0, sizeof(*ctx));
}
int oap_cli_prepare_ctx(struct oap_test_ctx * ctx)
{
return oap_cli_prepare(&ctx->cli.state, &ctx->cli.info, &ctx->req_hdr,
ctx->data);
}
int oap_srv_process_ctx(struct oap_test_ctx * ctx)
{
struct crypt_sk res = { .nid = NID_undef, .key = ctx->srv.key };
int ret;
ret = oap_srv_process(&ctx->srv.info, ctx->req_hdr,
&ctx->resp_hdr, &ctx->data, &res);
if (ret == 0)
ctx->srv.nid = res.nid;
return ret;
}
int oap_cli_complete_ctx(struct oap_test_ctx * ctx)
{
struct crypt_sk res = { .nid = NID_undef, .key = ctx->cli.key };
int ret;
ret = oap_cli_complete(ctx->cli.state, &ctx->cli.info, ctx->resp_hdr,
&ctx->data, &res);
ctx->cli.state = NULL;
if (ret == 0)
ctx->cli.nid = res.nid;
return ret;
}
int roundtrip_auth_only(const char * root_ca,
const char * im_ca_str)
{
struct oap_test_ctx ctx;
TEST_START();
if (oap_test_setup(&ctx, root_ca, im_ca_str) < 0)
goto fail;
if (oap_cli_prepare_ctx(&ctx) < 0) {
printf("Client prepare failed.\n");
goto fail_cleanup;
}
if (oap_srv_process_ctx(&ctx) < 0) {
printf("Server process failed.\n");
goto fail_cleanup;
}
if (oap_cli_complete_ctx(&ctx) < 0) {
printf("Client complete failed.\n");
goto fail_cleanup;
}
if (ctx.cli.nid != NID_undef || ctx.srv.nid != NID_undef) {
printf("Cipher should not be set for auth-only.\n");
goto fail_cleanup;
}
oap_test_teardown(&ctx);
TEST_SUCCESS();
return TEST_RC_SUCCESS;
fail_cleanup:
oap_test_teardown(&ctx);
fail:
TEST_FAIL();
return TEST_RC_FAIL;
}
int roundtrip_kex_only(void)
{
struct name_info cli_info;
struct name_info srv_info;
struct crypt_sk res;
uint8_t cli_key[SYMMKEYSZ];
uint8_t srv_key[SYMMKEYSZ];
int cli_nid;
int srv_nid;
buffer_t req_hdr = BUF_INIT;
buffer_t resp_hdr = BUF_INIT;
buffer_t data = BUF_INIT;
void * cli_state = NULL;
TEST_START();
memset(&cli_info, 0, sizeof(cli_info));
memset(&srv_info, 0, sizeof(srv_info));
strcpy(cli_info.name, "test-1.unittest.o7s");
strcpy(srv_info.name, "test-1.unittest.o7s");
if (oap_auth_init() < 0) {
printf("Failed to init OAP.\n");
goto fail;
}
if (oap_cli_prepare(&cli_state, &cli_info, &req_hdr,
data) < 0) {
printf("Client prepare failed.\n");
goto fail_cleanup;
}
res.key = srv_key;
if (oap_srv_process(&srv_info, req_hdr, &resp_hdr, &data, &res) < 0) {
printf("Server process failed.\n");
goto fail_cleanup;
}
srv_nid = res.nid;
res.key = cli_key;
if (oap_cli_complete(cli_state, &cli_info, resp_hdr, &data, &res) < 0) {
printf("Client complete failed.\n");
cli_state = NULL;
goto fail_cleanup;
}
cli_nid = res.nid;
cli_state = NULL;
if (memcmp(cli_key, srv_key, SYMMKEYSZ) != 0) {
printf("Client and server keys do not match!\n");
goto fail_cleanup;
}
if (cli_nid == NID_undef || srv_nid == NID_undef) {
printf("Cipher should be set for kex-only.\n");
goto fail_cleanup;
}
freebuf(resp_hdr);
freebuf(req_hdr);
oap_auth_fini();
TEST_SUCCESS();
return TEST_RC_SUCCESS;
fail_cleanup:
if (cli_state != NULL) {
res.key = cli_key;
oap_cli_complete(cli_state, &cli_info, resp_hdr, &data, &res);
}
freebuf(resp_hdr);
freebuf(req_hdr);
oap_auth_fini();
fail:
TEST_FAIL();
return TEST_RC_FAIL;
}
int corrupted_request(const char * root_ca,
const char * im_ca_str)
{
struct oap_test_ctx ctx;
TEST_START();
if (oap_test_setup(&ctx, root_ca, im_ca_str) < 0)
goto fail;
if (oap_cli_prepare_ctx(&ctx) < 0) {
printf("Client prepare failed.\n");
goto fail_cleanup;
}
/* Corrupt the request */
if (ctx.req_hdr.len > 100) {
ctx.req_hdr.data[50] ^= 0xFF;
ctx.req_hdr.data[51] ^= 0xAA;
ctx.req_hdr.data[52] ^= 0x55;
}
if (oap_srv_process_ctx(&ctx) == 0) {
printf("Server should reject corrupted request.\n");
goto fail_cleanup;
}
oap_test_teardown(&ctx);
TEST_SUCCESS();
return TEST_RC_SUCCESS;
fail_cleanup:
oap_test_teardown(&ctx);
fail:
TEST_FAIL();
return TEST_RC_FAIL;
}
int corrupted_response(const char * root_ca,
const char * im_ca_str)
{
struct oap_test_ctx ctx;
struct crypt_sk res;
TEST_START();
if (oap_test_setup(&ctx, root_ca, im_ca_str) < 0)
goto fail;
if (oap_cli_prepare_ctx(&ctx) < 0) {
printf("Client prepare failed.\n");
goto fail_cleanup;
}
if (oap_srv_process_ctx(&ctx) < 0) {
printf("Server process failed.\n");
goto fail_cleanup;
}
/* Corrupt the response */
if (ctx.resp_hdr.len > 100) {
ctx.resp_hdr.data[50] ^= 0xFF;
ctx.resp_hdr.data[51] ^= 0xAA;
ctx.resp_hdr.data[52] ^= 0x55;
}
res.key = ctx.cli.key;
if (oap_cli_complete(ctx.cli.state, &ctx.cli.info, ctx.resp_hdr,
&ctx.data, &res) == 0) {
printf("Client should reject corrupted response.\n");
ctx.cli.state = NULL;
goto fail_cleanup;
}
ctx.cli.state = NULL;
oap_test_teardown(&ctx);
TEST_SUCCESS();
return TEST_RC_SUCCESS;
fail_cleanup:
oap_test_teardown(&ctx);
fail:
TEST_FAIL();
return TEST_RC_FAIL;
}
int truncated_request(const char * root_ca,
const char * im_ca_str)
{
struct oap_test_ctx ctx;
size_t orig_len;
TEST_START();
if (oap_test_setup(&ctx, root_ca, im_ca_str) < 0)
goto fail;
if (oap_cli_prepare_ctx(&ctx) < 0) {
printf("Client prepare failed.\n");
goto fail_cleanup;
}
/* Truncate the request buffer */
orig_len = ctx.req_hdr.len;
ctx.req_hdr.len = orig_len / 2;
if (oap_srv_process_ctx(&ctx) == 0) {
printf("Server should reject truncated request.\n");
ctx.req_hdr.len = orig_len;
goto fail_cleanup;
}
ctx.req_hdr.len = orig_len;
oap_test_teardown(&ctx);
TEST_SUCCESS();
return TEST_RC_SUCCESS;
fail_cleanup:
oap_test_teardown(&ctx);
fail:
TEST_FAIL();
return TEST_RC_FAIL;
}
|