Line data Source code
1 : /**
2 : * Copyright Notice:
3 : * Copyright 2026 DMTF. All rights reserved.
4 : * License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
5 : **/
6 :
7 : #include "spdm_unit_test.h"
8 : #include "internal/libspdm_requester_lib.h"
9 :
10 : #if (LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP) && (LIBSPDM_ENABLE_CAPABILITY_CERT_CAP)
11 :
12 : /* GET_CERTIFICATE for slot 1. */
13 : static spdm_get_certificate_request_t m_libspdm_get_certificate_request_err1 = {
14 : {SPDM_MESSAGE_VERSION_11, SPDM_GET_CERTIFICATE, 1, 0},
15 : 0,
16 : LIBSPDM_MAX_CERT_CHAIN_BLOCK_LEN
17 : };
18 : static size_t m_libspdm_get_certificate_request_err1_size =
19 : sizeof(m_libspdm_get_certificate_request_err1);
20 :
21 : /**
22 : * Test 1: Error case, the Responder requests the certificate chain of a slot that holds none. The
23 : * Requester's DIGESTS told the Responder which slots are populated, so the request is the
24 : * Responder's mistake rather than a failure of the Requester's own.
25 : * Expected Behavior: generate an ERROR_RESPONSE with code SPDM_ERROR_CODE_INVALID_REQUEST.
26 : **/
27 1 : static void req_encap_certificate_err_case1(void **state)
28 : {
29 : libspdm_return_t status;
30 : libspdm_test_context_t *spdm_test_context;
31 : libspdm_context_t *spdm_context;
32 : size_t response_size;
33 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
34 : spdm_error_response_t *spdm_response;
35 : void *data;
36 : size_t data_size;
37 :
38 1 : spdm_test_context = *state;
39 1 : spdm_context = spdm_test_context->spdm_context;
40 1 : spdm_test_context->case_id = 0x1;
41 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
42 : SPDM_VERSION_NUMBER_SHIFT_BIT;
43 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_AFTER_DIGESTS;
44 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CERT_CAP;
45 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
46 :
47 : /* Slot 0 holds a certificate chain and slot 1 does not. */
48 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
49 : m_libspdm_use_asym_algo,
50 : &data, &data_size, NULL, NULL)) {
51 0 : assert_true(false);
52 : }
53 1 : spdm_context->local_context.local_cert_chain_provision[0] = data;
54 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size;
55 1 : spdm_context->local_context.local_cert_chain_provision[1] = NULL;
56 1 : spdm_context->local_context.local_cert_chain_provision_size[1] = 0;
57 :
58 1 : response_size = sizeof(response);
59 1 : status = libspdm_get_encap_response_certificate(
60 : spdm_context, m_libspdm_get_certificate_request_err1_size,
61 : &m_libspdm_get_certificate_request_err1, &response_size, response);
62 :
63 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
64 1 : assert_int_equal(response_size, sizeof(spdm_error_response_t));
65 1 : spdm_response = (void *)response;
66 1 : assert_int_equal(spdm_response->header.spdm_version, SPDM_MESSAGE_VERSION_11);
67 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
68 1 : assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_INVALID_REQUEST);
69 1 : assert_int_equal(spdm_response->header.param2, 0);
70 :
71 1 : spdm_context->local_context.local_cert_chain_provision[0] = NULL;
72 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = 0;
73 1 : free(data);
74 1 : }
75 :
76 1 : int libspdm_req_encap_certificate_error_test(void)
77 : {
78 1 : const struct CMUnitTest test_cases[] = {
79 : cmocka_unit_test(req_encap_certificate_err_case1),
80 : };
81 :
82 1 : libspdm_test_context_t test_context = {
83 : LIBSPDM_TEST_CONTEXT_VERSION,
84 : true,
85 : };
86 :
87 1 : libspdm_setup_test_context(&test_context);
88 :
89 1 : return cmocka_run_group_tests(test_cases,
90 : libspdm_unit_test_group_setup,
91 : libspdm_unit_test_group_teardown);
92 : }
93 :
94 : #endif /* (LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP) && (LIBSPDM_ENABLE_CAPABILITY_CERT_CAP) */
|