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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
|
/*
* Copyright 2007-2026 The OpenSSL Project Authors. All Rights Reserved.
* Copyright Nokia 2007-2019
* Copyright Siemens AG 2015-2019
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include "helpers/cmp_testlib.h"
#include "../crypto/crmf/crmf_local.h" /* for manipulating the CertId issuer */
#include "cmp_mock_srv.h"
static const char *server_key_f;
static const char *server_cert_f;
static const char *client_key_f;
static const char *client_cert_f;
static const char *pkcs10_f;
typedef struct test_fixture {
const char *test_case_name;
OSSL_CMP_CTX *cmp_ctx;
OSSL_CMP_SRV_CTX *srv_ctx;
int req_type;
int expected;
STACK_OF(X509) *caPubs;
} CMP_SES_TEST_FIXTURE;
static OSSL_LIB_CTX *libctx = NULL;
static OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL;
static EVP_PKEY *server_key = NULL;
static X509 *server_cert = NULL;
static EVP_PKEY *client_key = NULL;
static X509 *client_cert = NULL;
static unsigned char ref[CMP_TEST_REFVALUE_LENGTH]; /* not actually used */
/*
* For these unit tests, the client abandons message protection, and for
* error messages the mock server does so as well.
* Message protection and verification is tested in cmp_lib_test.c
*/
static void tear_down(CMP_SES_TEST_FIXTURE *fixture)
{
OSSL_CMP_CTX_free(fixture->cmp_ctx);
ossl_cmp_mock_srv_free(fixture->srv_ctx);
sk_X509_free(fixture->caPubs);
OPENSSL_free(fixture);
}
static int set_simple_trust(OSSL_CMP_CTX *ctx, X509 *trusted)
{
X509_STORE *ts = X509_STORE_new();
X509_VERIFY_PARAM *vpm;
/*
* not simply using OSSL_CMP_CTX_set1_srvCert() (to pin the server cert)
* in order to make sure that validated server cert gets cached,
* which is needed for the negative test case test_exec_KUR_bad_pkiConf_protection
*/
if (ts == NULL || !X509_STORE_add_cert(ts, trusted))
goto err;
vpm = X509_STORE_get0_param(ts);
if (!X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_NO_CHECK_TIME | X509_V_FLAG_PARTIAL_CHAIN)
|| !OSSL_CMP_CTX_set0_trusted(ctx, ts))
goto err;
return 1;
err:
X509_STORE_free(ts);
return 0;
}
static CMP_SES_TEST_FIXTURE *set_up(const char *const test_case_name)
{
CMP_SES_TEST_FIXTURE *fixture;
OSSL_CMP_CTX *srv_cmp_ctx = NULL;
OSSL_CMP_CTX *ctx = NULL; /* for client */
if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
return NULL;
fixture->test_case_name = test_case_name;
if (!TEST_ptr(fixture->srv_ctx = ossl_cmp_mock_srv_new(libctx, NULL))
|| !OSSL_CMP_SRV_CTX_set_accept_unprotected(fixture->srv_ctx, 1)
|| !ossl_cmp_mock_srv_set1_refCert(fixture->srv_ctx, client_cert)
|| !ossl_cmp_mock_srv_set1_certOut(fixture->srv_ctx, client_cert)
|| (srv_cmp_ctx = OSSL_CMP_SRV_CTX_get0_cmp_ctx(fixture->srv_ctx)) == NULL
|| !OSSL_CMP_CTX_set1_cert(srv_cmp_ctx, server_cert)
|| !OSSL_CMP_CTX_set1_pkey(srv_cmp_ctx, server_key))
goto err;
if (!TEST_ptr(fixture->cmp_ctx = ctx = OSSL_CMP_CTX_new(libctx, NULL))
|| !OSSL_CMP_CTX_set_log_cb(fixture->cmp_ctx, print_to_bio_out)
/* using default verbosity: OSSL_CMP_LOG_INFO */
|| !OSSL_CMP_CTX_set_transfer_cb(ctx, ossl_cmp_mock_server_perform)
|| !OSSL_CMP_CTX_set_transfer_cb_arg(ctx, fixture->srv_ctx)
|| !OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_SEND, 1)
|| !OSSL_CMP_CTX_set1_oldCert(ctx, client_cert)
|| !OSSL_CMP_CTX_set1_pkey(ctx, client_key)
/* client_key is by default used also for newPkey */
|| !set_simple_trust(ctx, server_cert)
|| !OSSL_CMP_CTX_set1_referenceValue(ctx, ref, sizeof(ref))) /* not actually needed */
goto err;
fixture->req_type = -1;
return fixture;
err:
tear_down(fixture);
return NULL;
}
static int execute_exec_RR_ses_test(CMP_SES_TEST_FIXTURE *fixt)
{
return TEST_int_eq(OSSL_CMP_CTX_get_status(fixt->cmp_ctx),
OSSL_CMP_PKISTATUS_unspecified)
&& TEST_int_eq(OSSL_CMP_exec_RR_ses(fixt->cmp_ctx),
fixt->expected == OSSL_CMP_PKISTATUS_accepted)
&& TEST_int_eq(OSSL_CMP_CTX_get_status(fixt->cmp_ctx), fixt->expected);
}
static int execute_exec_GENM_ses_test_single(CMP_SES_TEST_FIXTURE *fixture)
{
OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
ASN1_OBJECT *type = OBJ_txt2obj("1.3.6.1.5.5.7.4.2", 1);
OSSL_CMP_ITAV *itav = OSSL_CMP_ITAV_create(type, NULL);
STACK_OF(OSSL_CMP_ITAV) *itavs;
int ret;
OSSL_CMP_CTX_push0_genm_ITAV(ctx, itav);
itavs = OSSL_CMP_exec_GENM_ses(ctx);
ret = TEST_int_eq(OSSL_CMP_CTX_get_status(ctx), fixture->expected)
&& (fixture->expected == OSSL_CMP_PKISTATUS_accepted
? TEST_ptr(itavs)
: TEST_ptr_null(itavs));
sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
return ret;
}
static int execute_exec_GENM_ses_test(CMP_SES_TEST_FIXTURE *fixture)
{
return execute_exec_GENM_ses_test_single(fixture)
&& OSSL_CMP_CTX_reinit(fixture->cmp_ctx)
&& execute_exec_GENM_ses_test_single(fixture);
}
static int execute_exec_certrequest_ses_test(CMP_SES_TEST_FIXTURE *fixture)
{
OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
X509 *res = OSSL_CMP_exec_certreq(ctx, fixture->req_type, NULL);
int status = OSSL_CMP_CTX_get_status(ctx);
OSSL_CMP_CTX_print_errors(ctx);
if (!TEST_int_eq(status, fixture->expected))
return 0;
if (fixture->expected != OSSL_CMP_PKISTATUS_accepted)
return TEST_ptr_null(res);
if (!TEST_ptr(res) || !TEST_int_eq(X509_cmp(res, client_cert), 0))
return 0;
if (fixture->caPubs != NULL) {
STACK_OF(X509) *caPubs = OSSL_CMP_CTX_get1_caPubs(fixture->cmp_ctx);
int ret = TEST_int_eq(STACK_OF_X509_cmp(fixture->caPubs, caPubs), 0);
OSSL_STACK_OF_X509_free(caPubs);
return ret;
}
return 1;
}
static int test_exec_RR_ses(int request_error)
{
SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
if (request_error)
OSSL_CMP_CTX_set1_oldCert(fixture->cmp_ctx, NULL);
fixture->expected = request_error ? OSSL_CMP_PKISTATUS_request
: OSSL_CMP_PKISTATUS_accepted;
EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
return result;
}
static int test_exec_RR_ses_ok(void)
{
return test_exec_RR_ses(0);
}
static int test_exec_RR_ses_request_error(void)
{
return test_exec_RR_ses(1);
}
static int test_exec_RR_ses_receive_error(void)
{
SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
ossl_cmp_mock_srv_set_statusInfo(fixture->srv_ctx,
OSSL_CMP_PKISTATUS_rejection,
OSSL_CMP_CTX_FAILINFO_signerNotTrusted,
"test string");
ossl_cmp_mock_srv_set_sendError(fixture->srv_ctx, OSSL_CMP_PKIBODY_RR);
fixture->expected = OSSL_CMP_PKISTATUS_rejection;
EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
return result;
}
/*
* Create a CertId the issuer of which is not a directoryName, such that
* OSSL_CRMF_CERTID_get0_issuer() yields NULL for it, while
* OSSL_CRMF_CERTID_get0_serialNumber() yields the serial number as usual.
*/
static OSSL_CRMF_CERTID *certid_new_non_dirName_issuer(void)
{
OSSL_CRMF_CERTID *cid = OSSL_CRMF_CERTID_new();
ASN1_IA5STRING *dns = ASN1_IA5STRING_new();
if (cid == NULL || dns == NULL)
goto err;
if (!ASN1_STRING_set(dns, "server.example", -1))
goto err;
GENERAL_NAME_set0_value(cid->issuer, GEN_DNS, dns);
dns = NULL; /* ownership transferred to cid->issuer */
if (!ASN1_INTEGER_set(cid->serialNumber, 1))
goto err;
return cid;
err:
ASN1_IA5STRING_free(dns);
OSSL_CRMF_CERTID_free(cid);
return NULL;
}
/*
* Transfer callback wrapping the mock server: add a CertId to the revCerts
* field of the RP, which the server side omits for an RR request derived from
* a PKCS#10 CSR because such a request contains no issuer and serial number.
*/
static OSSL_CMP_MSG *transfer_add_revCerts(OSSL_CMP_CTX *ctx,
const OSSL_CMP_MSG *req)
{
OSSL_CMP_SRV_CTX *srv_ctx = OSSL_CMP_CTX_get_transfer_cb_arg(ctx);
OSSL_CMP_MSG *rp = ossl_cmp_mock_server_perform(ctx, req);
OSSL_CRMF_CERTID *cid;
if (rp == NULL || OSSL_CMP_MSG_get_bodytype(rp) != OSSL_CMP_PKIBODY_RP)
return rp;
if ((cid = certid_new_non_dirName_issuer()) == NULL)
goto err;
if (!sk_OSSL_CRMF_CERTID_push(rp->body->value.rp->revCerts, cid)) {
OSSL_CRMF_CERTID_free(cid);
goto err;
}
/* the body has been modified after the server protected the message */
if (!ossl_cmp_msg_protect(OSSL_CMP_SRV_CTX_get0_cmp_ctx(srv_ctx), rp))
goto err;
return rp;
err:
OSSL_CMP_MSG_free(rp);
return NULL;
}
/*
* The certificate to be revoked is given by a PKCS#10 CSR, so the RR contains
* neither issuer nor serial number, yet the RP contains a CertId in revCerts.
* The client cannot compare the CertId with what it did not send and thus
* must not reject the response.
* The CertId issuer is not a directoryName, such that the issuer comparison
* compares NULL with NULL and succeeds, which makes the client go on
* comparing the serial numbers with the one it did not send being NULL.
*/
static int test_exec_RR_ses_p10CSR_revCerts(void)
{
OSSL_CMP_CTX *ctx;
X509_REQ *csr = NULL;
SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
ctx = fixture->cmp_ctx;
fixture->expected = OSSL_CMP_PKISTATUS_accepted;
if (!TEST_ptr(csr = load_csr_der(pkcs10_f, libctx))
/* drop the reference cert such that the CSR is used instead */
|| !TEST_true(OSSL_CMP_CTX_set1_oldCert(ctx, NULL))
|| !TEST_true(OSSL_CMP_CTX_set1_p10CSR(ctx, csr))
/* no recipient can be derived from just a CSR */
|| !TEST_true(OSSL_CMP_CTX_set1_recipient(ctx,
X509_get_subject_name(server_cert)))
|| !TEST_true(OSSL_CMP_CTX_set_transfer_cb(ctx,
transfer_add_revCerts))) {
tear_down(fixture);
fixture = NULL;
}
X509_REQ_free(csr);
EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
return result;
}
static OSSL_CMP_MSG *transfer_rr_partial_certDetails(OSSL_CMP_CTX *ctx,
const OSSL_CMP_MSG *req, int drop_serial)
{
OSSL_CMP_MSG *copy;
OSSL_CMP_MSG *rsp;
OSSL_CMP_REVDETAILS *rd;
OSSL_CRMF_CERTTEMPLATE *tmpl;
if (OSSL_CMP_MSG_get_bodytype(req) != OSSL_CMP_PKIBODY_RR)
return ossl_cmp_mock_server_perform(ctx, req);
if ((copy = OSSL_CMP_MSG_dup(req)) == NULL)
return NULL;
if ((rd = sk_OSSL_CMP_REVDETAILS_value(copy->body->value.rr, 0)) == NULL
|| (tmpl = rd->certDetails) == NULL) {
OSSL_CMP_MSG_free(copy);
return NULL;
}
if (drop_serial != 0) {
ASN1_INTEGER_free(tmpl->serialNumber);
tmpl->serialNumber = NULL;
} else {
X509_NAME_free((X509_NAME *)tmpl->issuer);
tmpl->issuer = NULL;
}
/* the client sends the request unprotected, so no need to re-protect it */
rsp = ossl_cmp_mock_server_perform(ctx, copy);
OSSL_CMP_MSG_free(copy);
return rsp;
}
static OSSL_CMP_MSG *transfer_rr_no_serial(OSSL_CMP_CTX *ctx,
const OSSL_CMP_MSG *req)
{
return transfer_rr_partial_certDetails(ctx, req, 1);
}
static OSSL_CMP_MSG *transfer_rr_no_issuer(OSSL_CMP_CTX *ctx,
const OSSL_CMP_MSG *req)
{
return transfer_rr_partial_certDetails(ctx, req, 0);
}
/*
* The RR identifies the cert to be revoked only partially, by just its issuer
* or just its serial number. The mock server has a reference cert, so it must
* reject the request, and in particular it must not dereference the absent
* component when comparing the request with the reference cert.
*/
static int test_exec_RR_ses_partial_certDetails(OSSL_CMP_transfer_cb_t cb)
{
SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
fixture->expected = OSSL_CMP_PKISTATUS_rejection;
if (!TEST_true(OSSL_CMP_CTX_set_transfer_cb(fixture->cmp_ctx, cb))) {
tear_down(fixture);
fixture = NULL;
}
EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
return result;
}
static int test_exec_RR_ses_no_serial(void)
{
return test_exec_RR_ses_partial_certDetails(transfer_rr_no_serial);
}
static int test_exec_RR_ses_no_issuer(void)
{
return test_exec_RR_ses_partial_certDetails(transfer_rr_no_issuer);
}
static int test_exec_IR_ses(void)
{
SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
fixture->req_type = OSSL_CMP_PKIBODY_IR;
fixture->expected = OSSL_CMP_PKISTATUS_accepted;
fixture->caPubs = sk_X509_new_null();
if (!sk_X509_push(fixture->caPubs, server_cert)
|| !sk_X509_push(fixture->caPubs, server_cert)) {
tear_down(fixture);
return 0;
}
ossl_cmp_mock_srv_set1_caPubsOut(fixture->srv_ctx, fixture->caPubs);
EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
return result;
}
static int test_exec_REQ_ses_poll(int req_type, int check_after,
int poll_count, int total_timeout,
int expect)
{
SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
fixture->req_type = req_type;
fixture->expected = expect;
ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, check_after);
ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, poll_count);
OSSL_CMP_CTX_set_option(fixture->cmp_ctx,
OSSL_CMP_OPT_TOTAL_TIMEOUT, total_timeout);
if (req_type == OSSL_CMP_PKIBODY_IR) {
EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
} else if (req_type == OSSL_CMP_PKIBODY_GENM) {
EXECUTE_TEST(execute_exec_GENM_ses_test, tear_down);
}
return result;
}
static const int checkAfter = 1;
static const int pollCount = 3;
static int test_exec_IR_ses_poll_ok(void)
{
return test_exec_REQ_ses_poll(OSSL_CMP_PKIBODY_IR, checkAfter, 2, 0,
OSSL_CMP_PKISTATUS_accepted);
}
static int test_exec_IR_ses_poll_no_timeout(void)
{
return test_exec_REQ_ses_poll(OSSL_CMP_PKIBODY_IR, checkAfter,
2 /* pollCount */,
checkAfter + 14, /* usually 4 is sufficient */
OSSL_CMP_PKISTATUS_accepted);
}
static int test_exec_IR_ses_poll_total_timeout(void)
{
return test_exec_REQ_ses_poll(OSSL_CMP_PKIBODY_IR, checkAfter,
pollCount, (pollCount - 1) * checkAfter,
OSSL_CMP_PKISTATUS_trans);
}
static int test_exec_CR_ses(int implicit_confirm, int granted, int reject)
{
SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
fixture->req_type = OSSL_CMP_PKIBODY_CR;
OSSL_CMP_CTX_set_option(fixture->cmp_ctx,
OSSL_CMP_OPT_IMPLICIT_CONFIRM, implicit_confirm);
OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(fixture->srv_ctx, granted);
ossl_cmp_mock_srv_set_sendError(fixture->srv_ctx,
reject ? OSSL_CMP_PKIBODY_CERTCONF : -1);
fixture->expected = reject ? OSSL_CMP_PKISTATUS_rejection
: OSSL_CMP_PKISTATUS_accepted;
EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
return result;
}
static int test_exec_CR_ses_explicit_confirm(void)
{
return test_exec_CR_ses(0, 0, 0)
&& test_exec_CR_ses(0, 0, 1 /* reject */);
}
static int test_exec_CR_ses_implicit_confirm(void)
{
return test_exec_CR_ses(1, 0, 0)
&& test_exec_CR_ses(1, 1 /* granted */, 0);
}
/* the KUR transactions include certConf/pkiConf */
static int test_exec_KUR_ses(int transfer_error, int server_use_bad_protection,
int pubkey, int raverified)
{
SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
fixture->req_type = OSSL_CMP_PKIBODY_KUR;
/* ctx->oldCert has already been set */
if (transfer_error)
OSSL_CMP_CTX_set_transfer_cb_arg(fixture->cmp_ctx, NULL);
(void)ossl_cmp_mock_srv_set_useBadProtection(fixture->srv_ctx, server_use_bad_protection);
if (pubkey) {
EVP_PKEY *key = raverified /* wrong key */ ? server_key : client_key;
if (!EVP_PKEY_up_ref(key))
return 0;
OSSL_CMP_CTX_set0_newPkey(fixture->cmp_ctx, 0 /* not priv */, key);
OSSL_CMP_SRV_CTX_set_accept_raverified(fixture->srv_ctx, 1);
}
if (pubkey || raverified)
OSSL_CMP_CTX_set_option(fixture->cmp_ctx, OSSL_CMP_OPT_POPO_METHOD,
OSSL_CRMF_POPO_RAVERIFIED);
fixture->expected = transfer_error ? OSSL_CMP_PKISTATUS_trans : raverified ? (pubkey ? OSSL_CMP_PKISTATUS_rejected_by_client : OSSL_CMP_PKISTATUS_rejection)
: server_use_bad_protection != -1 ? OSSL_CMP_PKISTATUS_checking_response
: OSSL_CMP_PKISTATUS_accepted;
EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
return result;
}
static int test_exec_KUR_ses_ok(void)
{
return test_exec_KUR_ses(0, -1, 0, 0);
}
static int test_exec_KUR_ses_transfer_error(void)
{
return test_exec_KUR_ses(1, -1, 0, 0);
}
static int test_exec_KUR_bad_pkiConf_protection(void)
{
return test_exec_KUR_ses(0, -1 /* disabled: OSSL_CMP_PKIBODY_PKICONF */, 0, 0);
}
static int test_exec_KUR_ses_wrong_popo(void)
{
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION /* cf ossl_cmp_verify_popo() */
return test_exec_KUR_ses(0, -1, 0, 1);
#else
return 1;
#endif
}
static int test_exec_KUR_ses_pub(void)
{
return test_exec_KUR_ses(0, -1, 1, 0);
}
static int test_exec_KUR_ses_wrong_pub(void)
{
return test_exec_KUR_ses(0, -1, 1, 1);
}
static int test_certConf_cb(OSSL_CMP_CTX *ctx, X509 *cert, int fail_info,
const char **txt)
{
int *reject = OSSL_CMP_CTX_get_certConf_cb_arg(ctx);
if (*reject) {
*txt = "not to my taste";
fail_info = OSSL_CMP_PKIFAILUREINFO_badCertTemplate;
}
return fail_info;
}
static int test_exec_P10CR_ses(int reject)
{
OSSL_CMP_CTX *ctx;
X509_REQ *csr = NULL;
SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
fixture->req_type = OSSL_CMP_PKIBODY_P10CR;
fixture->expected = reject ? OSSL_CMP_PKISTATUS_rejected_by_client
: OSSL_CMP_PKISTATUS_accepted;
ctx = fixture->cmp_ctx;
if (!TEST_ptr(csr = load_csr_der(pkcs10_f, libctx))
|| !TEST_true(OSSL_CMP_CTX_set1_p10CSR(ctx, csr))
|| !TEST_true(OSSL_CMP_CTX_set_certConf_cb(ctx, test_certConf_cb))
|| !TEST_true(OSSL_CMP_CTX_set_certConf_cb_arg(ctx, &reject))) {
tear_down(fixture);
fixture = NULL;
}
X509_REQ_free(csr);
EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
return result;
}
static int test_exec_P10CR_ses_ok(void)
{
return test_exec_P10CR_ses(0);
}
static int test_exec_P10CR_ses_reject(void)
{
return test_exec_P10CR_ses(1);
}
static int execute_try_certreq_poll_test(CMP_SES_TEST_FIXTURE *fixture)
{
OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
int check_after;
const int CHECK_AFTER = 0;
const int TYPE = OSSL_CMP_PKIBODY_KUR;
ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
&& check_after == CHECK_AFTER
&& TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
&& TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
&& check_after == CHECK_AFTER
&& TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
&& TEST_int_eq(fixture->expected,
OSSL_CMP_try_certreq(ctx, TYPE, NULL, NULL))
&& TEST_int_eq(0,
X509_cmp(OSSL_CMP_CTX_get0_newCert(ctx), client_cert));
}
static int test_try_certreq_poll(void)
{
SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
fixture->expected = 1;
EXECUTE_TEST(execute_try_certreq_poll_test, tear_down);
return result;
}
static int execute_try_certreq_poll_abort_test(CMP_SES_TEST_FIXTURE *fixture)
{
OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
int check_after;
const int CHECK_AFTER = 99;
const int TYPE = OSSL_CMP_PKIBODY_CR;
ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
&& check_after == CHECK_AFTER
&& TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
&& TEST_int_eq(fixture->expected,
OSSL_CMP_try_certreq(ctx, -1 /* abort */, NULL, NULL))
&& TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(fixture->cmp_ctx), NULL);
}
static int test_try_certreq_poll_abort(void)
{
SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
fixture->expected = 1;
EXECUTE_TEST(execute_try_certreq_poll_abort_test, tear_down);
return result;
}
static int test_exec_GENM_ses_poll_ok(void)
{
return test_exec_REQ_ses_poll(OSSL_CMP_PKIBODY_GENM, checkAfter, 2, 0,
OSSL_CMP_PKISTATUS_accepted);
}
static int test_exec_GENM_ses_poll_no_timeout(void)
{
return test_exec_REQ_ses_poll(OSSL_CMP_PKIBODY_GENM, checkAfter,
1 /* pollCount */, checkAfter + 1,
OSSL_CMP_PKISTATUS_accepted);
}
static int test_exec_GENM_ses_poll_total_timeout(void)
{
return test_exec_REQ_ses_poll(OSSL_CMP_PKIBODY_GENM, checkAfter,
pollCount, (pollCount - 1) * checkAfter,
OSSL_CMP_PKISTATUS_trans);
}
static int test_exec_GENM_ses(int transfer_error, int total_timeout, int expect)
{
SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
if (transfer_error)
OSSL_CMP_CTX_set_transfer_cb_arg(fixture->cmp_ctx, NULL);
/*
* cannot use OSSL_CMP_CTX_set_option(... OSSL_CMP_OPT_TOTAL_TIMEOUT)
* here because this will correct total_timeout to be >= 0
*/
fixture->cmp_ctx->total_timeout = total_timeout;
fixture->expected = expect;
EXECUTE_TEST(execute_exec_GENM_ses_test, tear_down);
return result;
}
static int test_exec_GENM_ses_ok(void)
{
return test_exec_GENM_ses(0, 0, OSSL_CMP_PKISTATUS_accepted);
}
static int test_exec_GENM_ses_transfer_error(void)
{
return test_exec_GENM_ses(1, 0, OSSL_CMP_PKISTATUS_trans);
}
static int test_exec_GENM_ses_total_timeout(void)
{
return test_exec_GENM_ses(0, -1, OSSL_CMP_PKISTATUS_trans);
}
static int execute_exchange_certConf_test(CMP_SES_TEST_FIXTURE *fixture)
{
int res = ossl_cmp_exchange_certConf(fixture->cmp_ctx, OSSL_CMP_CERTREQID,
OSSL_CMP_PKIFAILUREINFO_addInfoNotAvailable,
"abcdefg");
return TEST_int_eq(fixture->expected, res);
}
static int execute_exchange_error_test(CMP_SES_TEST_FIXTURE *fixture)
{
int res = ossl_cmp_exchange_error(fixture->cmp_ctx,
OSSL_CMP_PKISTATUS_rejection,
1 << OSSL_CMP_PKIFAILUREINFO_unsupportedVersion,
"foo_status", 999, "foo_details");
return TEST_int_eq(fixture->expected, res);
}
static int test_exchange_certConf(void)
{
SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
fixture->expected = 0; /* client should not send certConf immediately */
if (!ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx, X509_dup(client_cert))) {
tear_down(fixture);
fixture = NULL;
}
EXECUTE_TEST(execute_exchange_certConf_test, tear_down);
return result;
}
static int test_exchange_error(void)
{
SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
fixture->expected = 1; /* client may send error any time */
EXECUTE_TEST(execute_exchange_error_test, tear_down);
return result;
}
void cleanup_tests(void)
{
X509_free(server_cert);
EVP_PKEY_free(server_key);
X509_free(client_cert);
EVP_PKEY_free(client_key);
OSSL_PROVIDER_unload(default_null_provider);
OSSL_PROVIDER_unload(provider);
OSSL_LIB_CTX_free(libctx);
return;
}
#define USAGE "server.key server.crt client.key client.crt client.csr module_name [module_conf_file]\n"
OPT_TEST_DECLARE_USAGE(USAGE)
int setup_tests(void)
{
if (!test_skip_common_options()) {
TEST_error("Error parsing test options\n");
return 0;
}
if (!TEST_ptr(server_key_f = test_get_argument(0))
|| !TEST_ptr(server_cert_f = test_get_argument(1))
|| !TEST_ptr(client_key_f = test_get_argument(2))
|| !TEST_ptr(client_cert_f = test_get_argument(3))
|| !TEST_ptr(pkcs10_f = test_get_argument(4))) {
TEST_error("usage: cmp_client_test %s", USAGE);
return 0;
}
if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 5, USAGE))
return 0;
if (!TEST_ptr(server_key = load_pkey_pem(server_key_f, libctx))
|| !TEST_ptr(server_cert = load_cert_pem(server_cert_f, libctx))
|| !TEST_ptr(client_key = load_pkey_pem(client_key_f, libctx))
|| !TEST_ptr(client_cert = load_cert_pem(client_cert_f, libctx))
|| !TEST_int_eq(1, RAND_bytes_ex(libctx, ref, sizeof(ref), 0))) { /* not actually used */
cleanup_tests();
return 0;
}
ADD_TEST(test_exec_RR_ses_ok);
ADD_TEST(test_exec_RR_ses_request_error);
ADD_TEST(test_exec_RR_ses_receive_error);
ADD_TEST(test_exec_RR_ses_p10CSR_revCerts);
ADD_TEST(test_exec_RR_ses_no_serial);
ADD_TEST(test_exec_RR_ses_no_issuer);
ADD_TEST(test_exec_CR_ses_explicit_confirm);
ADD_TEST(test_exec_CR_ses_implicit_confirm);
ADD_TEST(test_exec_IR_ses);
ADD_TEST(test_exec_IR_ses_poll_ok);
ADD_TEST(test_exec_IR_ses_poll_no_timeout);
ADD_TEST(test_exec_IR_ses_poll_total_timeout);
ADD_TEST(test_exec_KUR_ses_ok);
ADD_TEST(test_exec_KUR_ses_transfer_error);
ADD_TEST(test_exec_KUR_bad_pkiConf_protection);
ADD_TEST(test_exec_KUR_ses_wrong_popo);
ADD_TEST(test_exec_KUR_ses_pub);
ADD_TEST(test_exec_KUR_ses_wrong_pub);
ADD_TEST(test_exec_P10CR_ses_ok);
ADD_TEST(test_exec_P10CR_ses_reject);
ADD_TEST(test_try_certreq_poll);
ADD_TEST(test_try_certreq_poll_abort);
ADD_TEST(test_exec_GENM_ses_ok);
ADD_TEST(test_exec_GENM_ses_transfer_error);
ADD_TEST(test_exec_GENM_ses_total_timeout);
ADD_TEST(test_exec_GENM_ses_poll_ok);
ADD_TEST(test_exec_GENM_ses_poll_no_timeout);
ADD_TEST(test_exec_GENM_ses_poll_total_timeout);
ADD_TEST(test_exchange_certConf);
ADD_TEST(test_exchange_error);
return 1;
}
|