forked from microsoft/mu_plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MfciMultipleCertsHostTest.c
492 lines (413 loc) · 17 KB
/
MfciMultipleCertsHostTest.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
/** @file
This module tests MFCI XDR formatted certificate extraction
logic for MfciDxe driver.
Note: This module does NOT test signature validation
step, which is covered by MfciPolicyParsingUnitTest.
Copyright (c) Microsoft Corporation
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <Uefi.h>
#include <MfciPolicyType.h>
#include <MfciVariables.h>
#include <MfciPolicyFields.h>
#include <Guid/MuVarPolicyFoundationDxe.h>
#include <Protocol/MfciProtocol.h>
#include <Library/MfciDeviceIdSupportLib.h>
#include <Library/MfciPolicyParsingLib.h>
#include <Library/MfciRetrievePolicyLib.h>
#include <Library/BaseCryptLib.h>
#include <Library/BaseLib.h> // CpuDeadLoop()
#include <Library/DebugLib.h> // DEBUG tracing
#include <Library/BaseMemoryLib.h> // CopyGuid()
#include <Library/VariablePolicyHelperLib.h> // NotifyMfciPolicyChange()
#include <Library/MemoryAllocationLib.h>
#include <Library/UnitTestLib.h>
#include <UnitTests/MfciPolicyParsingUnitTest/data/certs/CA.cer.h>
#include <UnitTests/MfciPolicyParsingUnitTest/data/certs/CA.cer.xdr.h>
#include <UnitTests/MfciPolicyParsingUnitTest/data/certs/Root.cer.h>
#include <UnitTests/MfciPolicyParsingUnitTest/data/certs/CA-Root.cer.xdr.h>
#include "../MfciDxe.h"
#define UNIT_TEST_NAME "Mfci Multiple Certificates Host Test"
#define UNIT_TEST_VERSION "0.1"
CONST CHAR16 gPolicyTargetFieldVarNames[TARGET_POLICY_COUNT][MFCI_VAR_NAME_MAX_LENGTH] = { 0 };
BOOLEAN
EFIAPI
GetRandomNumber64 (
OUT UINT64 *Rand
)
{
// Not used
ASSERT (FALSE);
return FALSE;
}
VOID
EFIAPI
ResetSystemWithSubtype (
IN EFI_RESET_TYPE ResetType,
IN CONST GUID *ResetSubtype
)
{
// Not used
ASSERT (FALSE);
}
EFI_STATUS
EFIAPI
InitPublicInterface (
VOID
)
{
// Not used
ASSERT (FALSE);
return EFI_DEVICE_ERROR;
}
EFI_STATUS
EFIAPI
VerifyTargeting (
VOID *PolicyBlob,
UINTN PolicyBlobSize,
UINT64 ExpectedNonce,
MFCI_POLICY_TYPE *ExtractedPolicy
)
{
// Not used
ASSERT (FALSE);
return EFI_DEVICE_ERROR;
}
EFI_STATUS
EFIAPI
NotifyMfciPolicyChange (
IN MFCI_POLICY_TYPE NewPolicy
)
{
// Not used
ASSERT (FALSE);
return EFI_DEVICE_ERROR;
}
EFI_STATUS
EFIAPI
InitSecureBootListener (
VOID
)
{
// Not used
ASSERT (FALSE);
return EFI_DEVICE_ERROR;
}
EFI_STATUS
EFIAPI
InitTpmListener (
VOID
)
{
// Not used
ASSERT (FALSE);
return EFI_DEVICE_ERROR;
}
EFI_RUNTIME_SERVICES mMockRuntime;
/**
* Validate blob on each certificate from preset XDR buffer.
*
* @param SignedPolicy Pointer to hold the policy buffer to be validated.
* @param SignedPolicySize Size of SignedPolicy, in bytes.
* @param Certificates Pointer to hold the XDR formatted buffer of certificates.
* @param CertificatesSize Size of Certificates, in bytes.
*
* @retval EFI_SUCCESS The one certificate from Certificate is valid for input policy validation.
* @retval EFI_ABORTED SignedPolicy is null data or at least one certificate from incoming Certificates is
* malformatted.
* @retval Others Other errors from the underlying ValidateBlob function.
*/
EFI_STATUS
ValidateBlobWithXdrCertificates (
IN CONST UINT8 *SignedPolicy,
IN UINTN SignedPolicySize,
IN CONST UINT8 *Certificates,
IN UINTN CertificatesSize
);
EFI_STATUS
EFIAPI
ValidateBlob (
IN CONST UINT8 *SignedPolicy,
UINTN SignedPolicySize,
IN CONST UINT8 *TrustAnchorCert,
IN UINTN TrustAnchorCertSize,
IN CONST CHAR8 *EKU
)
{
assert_ptr_equal (EKU, (CHAR8 *)FixedPcdGetPtr (PcdMfciPkcs7RequiredLeafEKU));
check_expected (SignedPolicy);
check_expected (SignedPolicySize);
check_expected (TrustAnchorCert);
check_expected (TrustAnchorCertSize);
return (EFI_STATUS)mock ();
}
/**
Unit test for ValidateBlobWithXdrCertificates () API with single certificate
from the input XDR buffer.
@param[in] Context [Optional] An optional parameter that enables:
1) test-case reuse with varied parameters and
2) test-case re-entry for Target tests that need a
reboot. This parameter is a VOID* and it is the
responsibility of the test author to ensure that the
contents are well understood by all test cases that may
consume it.
@retval UNIT_TEST_PASSED The Unit test has completed and the test
case was successful.
@retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
**/
UNIT_TEST_STATUS
EFIAPI
MfciMultipleCertificatesShouldParseSingleCert (
IN UNIT_TEST_CONTEXT Context
)
{
EFI_STATUS Status;
UINT8 Dummy;
Dummy = 0;
expect_value (ValidateBlob, SignedPolicy, &Dummy);
expect_value (ValidateBlob, SignedPolicySize, sizeof (Dummy));
expect_value (ValidateBlob, TrustAnchorCertSize, sizeof (mCert_Trusted_CA));
expect_memory (ValidateBlob, TrustAnchorCert, mCert_Trusted_CA, sizeof (mCert_Trusted_CA));
will_return (ValidateBlob, EFI_SUCCESS);
Status = ValidateBlobWithXdrCertificates (&Dummy, sizeof (Dummy), mCert_Trusted_CA_xdr, sizeof (mCert_Trusted_CA_xdr));
UT_ASSERT_NOT_EFI_ERROR (Status);
return UNIT_TEST_PASSED;
}
/**
Unit test for ValidateBlobWithXdrCertificates () API with multiple certificates
from the input XDR buffer.
@param[in] Context [Optional] An optional parameter that enables:
1) test-case reuse with varied parameters and
2) test-case re-entry for Target tests that need a
reboot. This parameter is a VOID* and it is the
responsibility of the test author to ensure that the
contents are well understood by all test cases that may
consume it.
@retval UNIT_TEST_PASSED The Unit test has completed and the test
case was successful.
@retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
**/
UNIT_TEST_STATUS
EFIAPI
MfciMultipleCertificatesShouldParseMultipleCert (
IN UNIT_TEST_CONTEXT Context
)
{
EFI_STATUS Status;
UINT8 Dummy;
expect_value (ValidateBlob, SignedPolicy, &Dummy);
expect_value (ValidateBlob, SignedPolicySize, sizeof (Dummy));
expect_value (ValidateBlob, TrustAnchorCertSize, sizeof (mCert_Trusted_CA));
expect_memory (ValidateBlob, TrustAnchorCert, mCert_Trusted_CA, sizeof (mCert_Trusted_CA));
will_return (ValidateBlob, EFI_SECURITY_VIOLATION);
expect_value (ValidateBlob, SignedPolicy, &Dummy);
expect_value (ValidateBlob, SignedPolicySize, sizeof (Dummy));
expect_value (ValidateBlob, TrustAnchorCertSize, sizeof (mCertRoot_cer));
expect_memory (ValidateBlob, TrustAnchorCert, mCertRoot_cer, sizeof (mCertRoot_cer));
will_return (ValidateBlob, EFI_SUCCESS);
Status = ValidateBlobWithXdrCertificates (&Dummy, sizeof (Dummy), mCert_Trusted_CA_Root_xdr, sizeof (mCert_Trusted_CA_Root_xdr));
UT_ASSERT_NOT_EFI_ERROR (Status);
return UNIT_TEST_PASSED;
}
/**
Unit test for ValidateBlobWithXdrCertificates () API with all failed validation.
@param[in] Context [Optional] An optional parameter that enables:
1) test-case reuse with varied parameters and
2) test-case re-entry for Target tests that need a
reboot. This parameter is a VOID* and it is the
responsibility of the test author to ensure that the
contents are well understood by all test cases that may
consume it.
@retval UNIT_TEST_PASSED The Unit test has completed and the test
case was successful.
@retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
**/
UNIT_TEST_STATUS
EFIAPI
MfciMultipleCertificatesShouldPropagateResult (
IN UNIT_TEST_CONTEXT Context
)
{
EFI_STATUS Status;
UINT8 Dummy;
expect_value (ValidateBlob, SignedPolicy, &Dummy);
expect_value (ValidateBlob, SignedPolicySize, sizeof (Dummy));
expect_value (ValidateBlob, TrustAnchorCertSize, sizeof (mCert_Trusted_CA));
expect_memory (ValidateBlob, TrustAnchorCert, mCert_Trusted_CA, sizeof (mCert_Trusted_CA));
will_return (ValidateBlob, EFI_SECURITY_VIOLATION);
expect_value (ValidateBlob, SignedPolicy, &Dummy);
expect_value (ValidateBlob, SignedPolicySize, sizeof (Dummy));
expect_value (ValidateBlob, TrustAnchorCertSize, sizeof (mCertRoot_cer));
expect_memory (ValidateBlob, TrustAnchorCert, mCertRoot_cer, sizeof (mCertRoot_cer));
will_return (ValidateBlob, EFI_COMPROMISED_DATA);
Status = ValidateBlobWithXdrCertificates (&Dummy, sizeof (Dummy), mCert_Trusted_CA_Root_xdr, sizeof (mCert_Trusted_CA_Root_xdr));
UT_ASSERT_STATUS_EQUAL (Status, EFI_COMPROMISED_DATA);
return UNIT_TEST_PASSED;
}
/**
Unit test for ValidateBlobWithXdrCertificates () API with empty inputs.
@param[in] Context [Optional] An optional parameter that enables:
1) test-case reuse with varied parameters and
2) test-case re-entry for Target tests that need a
reboot. This parameter is a VOID* and it is the
responsibility of the test author to ensure that the
contents are well understood by all test cases that may
consume it.
@retval UNIT_TEST_PASSED The Unit test has completed and the test
case was successful.
@retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
**/
UNIT_TEST_STATUS
EFIAPI
MfciMultipleCertificatesShouldCheckInputs (
IN UNIT_TEST_CONTEXT Context
)
{
EFI_STATUS Status;
UINT8 Dummy;
Dummy = 0;
Status = ValidateBlobWithXdrCertificates (&Dummy, sizeof (Dummy), NULL, sizeof (mCert_Trusted_CA_Root_xdr));
UT_ASSERT_STATUS_EQUAL (Status, EFI_ABORTED);
Status = ValidateBlobWithXdrCertificates (&Dummy, sizeof (Dummy), mCert_Trusted_CA_Root_xdr, 0);
UT_ASSERT_STATUS_EQUAL (Status, EFI_ABORTED);
Status = ValidateBlobWithXdrCertificates (NULL, sizeof (Dummy), mCert_Trusted_CA_Root_xdr, sizeof (mCert_Trusted_CA_Root_xdr));
UT_ASSERT_STATUS_EQUAL (Status, EFI_ABORTED);
Status = ValidateBlobWithXdrCertificates (&Dummy, 0, mCert_Trusted_CA_Root_xdr, sizeof (mCert_Trusted_CA_Root_xdr));
UT_ASSERT_STATUS_EQUAL (Status, EFI_ABORTED);
return UNIT_TEST_PASSED;
}
/**
Unit test for ValidateBlobWithXdrCertificates () API should generally inspect
incoming certificates.
@param[in] Context [Optional] An optional parameter that enables:
1) test-case reuse with varied parameters and
2) test-case re-entry for Target tests that need a
reboot. This parameter is a VOID* and it is the
responsibility of the test author to ensure that the
contents are well understood by all test cases that may
consume it.
@retval UNIT_TEST_PASSED The Unit test has completed and the test
case was successful.
@retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
**/
UNIT_TEST_STATUS
EFIAPI
MfciMultipleCertificatesShouldCheckGeneralCertificates (
IN UNIT_TEST_CONTEXT Context
)
{
EFI_STATUS Status;
UINT8 Dummy;
UINT8 FakeCertificate;
Dummy = 0;
FakeCertificate = 0;
Status = ValidateBlobWithXdrCertificates (&Dummy, sizeof (Dummy), &FakeCertificate, sizeof (FakeCertificate));
UT_ASSERT_STATUS_EQUAL (Status, EFI_ABORTED);
return UNIT_TEST_PASSED;
}
/**
Unit test for ValidateBlobWithXdrCertificates () API should inspect individual
incoming certificates.
@param[in] Context [Optional] An optional parameter that enables:
1) test-case reuse with varied parameters and
2) test-case re-entry for Target tests that need a
reboot. This parameter is a VOID* and it is the
responsibility of the test author to ensure that the
contents are well understood by all test cases that may
consume it.
@retval UNIT_TEST_PASSED The Unit test has completed and the test
case was successful.
@retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
**/
UNIT_TEST_STATUS
EFIAPI
MfciMultipleCertificatesShouldCheckIndividualCertificate (
IN UNIT_TEST_CONTEXT Context
)
{
EFI_STATUS Status;
UINT8 Dummy;
Dummy = 0;
// Not enough for individual size field
UINT8 FakeCertificate1[] = { 0x00, 0x01, 0x02, 0x03, 0x04 };
// No content individual certificate
UINT8 FakeCertificate2[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
// Content shorter than noted size
UINT8 FakeCertificate3[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
Status = ValidateBlobWithXdrCertificates (&Dummy, sizeof (Dummy), FakeCertificate1, sizeof (FakeCertificate1));
UT_ASSERT_STATUS_EQUAL (Status, EFI_ABORTED);
Status = ValidateBlobWithXdrCertificates (&Dummy, sizeof (Dummy), FakeCertificate2, sizeof (FakeCertificate2));
UT_ASSERT_STATUS_EQUAL (Status, EFI_ABORTED);
Status = ValidateBlobWithXdrCertificates (&Dummy, sizeof (Dummy), FakeCertificate3, sizeof (FakeCertificate3));
UT_ASSERT_STATUS_EQUAL (Status, EFI_ABORTED);
return UNIT_TEST_PASSED;
}
/**
Initialize the unit test framework, suite, and unit tests for the
sample unit tests and run the unit tests.
@retval EFI_SUCCESS All test cases were dispatched.
@retval EFI_OUT_OF_RESOURCES There are not enough resources available to
initialize the unit tests.
**/
EFI_STATUS
EFIAPI
UefiTestMain (
VOID
)
{
EFI_STATUS Status;
UNIT_TEST_FRAMEWORK_HANDLE Framework;
UNIT_TEST_SUITE_HANDLE MultipleCertificatesSuite;
Framework = NULL;
DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_NAME, UNIT_TEST_VERSION));
//
// Start setting up the test framework for running the tests.
//
Status = InitUnitTestFramework (&Framework, UNIT_TEST_NAME, gEfiCallerBaseName, UNIT_TEST_VERSION);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));
goto EXIT;
}
// The blob parsing part is tested in MfciPolicyParsingUnitTest, so will not go through those here.
//
// Populate the MultipleCertificatesSuite Unit Test Suite.
//
Status = CreateUnitTestSuite (&MultipleCertificatesSuite, Framework, "TargetVerifyPhase", "ReportRouter.Phase", NULL, NULL);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MultipleCertificatesSuite\n"));
Status = EFI_OUT_OF_RESOURCES;
goto EXIT;
}
AddTestCase (MultipleCertificatesSuite, "ValidateBlobWithXdrCertificates should succeed with correct single XDR cert", "GoodSingle", MfciMultipleCertificatesShouldParseSingleCert, NULL, NULL, NULL);
AddTestCase (MultipleCertificatesSuite, "ValidateBlobWithXdrCertificates should succeed with correct multiple XDR certs", "GoodMultiple", MfciMultipleCertificatesShouldParseMultipleCert, NULL, NULL, NULL);
AddTestCase (MultipleCertificatesSuite, "ValidateBlobWithXdrCertificates should propagate result if failure", "PropagateResult", MfciMultipleCertificatesShouldPropagateResult, NULL, NULL, NULL);
AddTestCase (MultipleCertificatesSuite, "ValidateBlobWithXdrCertificates should check inputs for validity", "CheckInputs", MfciMultipleCertificatesShouldCheckInputs, NULL, NULL, NULL);
AddTestCase (MultipleCertificatesSuite, "ValidateBlobWithXdrCertificates should inspect general input certificates", "CheckGeneral", MfciMultipleCertificatesShouldCheckGeneralCertificates, NULL, NULL, NULL);
AddTestCase (MultipleCertificatesSuite, "ValidateBlobWithXdrCertificates should inspect individual input certificates", "CheckIndividual", MfciMultipleCertificatesShouldCheckIndividualCertificate, NULL, NULL, NULL);
//
// Execute the tests.
//
Status = RunAllTestSuites (Framework);
EXIT:
if (Framework) {
FreeUnitTestFramework (Framework);
}
return Status;
}
/**
Standard POSIX C entry point for host based unit test execution.
**/
int
main (
int argc,
char *argv[]
)
{
return UefiTestMain ();
}