Line data Source code
1 : /**
2 : * Copyright Notice:
3 : * Copyright 2021-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_responder_lib.h"
9 : #include "internal/libspdm_requester_lib.h"
10 :
11 : #if LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP
12 :
13 : #pragma pack(1)
14 : typedef struct {
15 : spdm_message_header_t header;
16 : uint16_t req_session_id;
17 : uint8_t session_policy;
18 : uint8_t reserved;
19 : uint8_t random_data[SPDM_RANDOM_DATA_SIZE];
20 : uint8_t exchange_data[LIBSPDM_MAX_DHE_KEY_SIZE];
21 : uint16_t opaque_length;
22 : uint8_t opaque_data[SPDM_MAX_OPAQUE_DATA_SIZE];
23 : } libspdm_key_exchange_request_mine_t;
24 : #pragma pack()
25 :
26 : libspdm_key_exchange_request_mine_t m_libspdm_key_exchange_request1 = {
27 : { SPDM_MESSAGE_VERSION_11, SPDM_KEY_EXCHANGE,
28 : SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0 },
29 : };
30 : size_t m_libspdm_key_exchange_request1_size = sizeof(m_libspdm_key_exchange_request1);
31 :
32 : libspdm_key_exchange_request_mine_t m_libspdm_key_exchange_request2 = {
33 : { SPDM_MESSAGE_VERSION_11, SPDM_KEY_EXCHANGE,
34 : SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0 },
35 : };
36 : size_t m_libspdm_key_exchange_request2_size = sizeof(spdm_key_exchange_request_t);
37 :
38 : /* Request TCB measurement hash */
39 : libspdm_key_exchange_request_mine_t m_libspdm_key_exchange_request3 = {
40 : { SPDM_MESSAGE_VERSION_11, SPDM_KEY_EXCHANGE,
41 : SPDM_KEY_EXCHANGE_REQUEST_TCB_COMPONENT_MEASUREMENT_HASH, 0 },
42 : };
43 : size_t m_libspdm_key_exchange_request3_size = sizeof(m_libspdm_key_exchange_request3);
44 :
45 : /* Request all measurement hash */
46 : libspdm_key_exchange_request_mine_t m_libspdm_key_exchange_request4 = {
47 : { SPDM_MESSAGE_VERSION_11, SPDM_KEY_EXCHANGE,
48 : SPDM_KEY_EXCHANGE_REQUEST_ALL_MEASUREMENTS_HASH, 0 },
49 : };
50 : size_t m_libspdm_key_exchange_request4_size = sizeof(m_libspdm_key_exchange_request4);
51 :
52 : /* Uses a reserved value in measurement hash */
53 : libspdm_key_exchange_request_mine_t m_libspdm_key_exchange_request5 = {
54 : { SPDM_MESSAGE_VERSION_11, SPDM_KEY_EXCHANGE,
55 : 0x50, 0 },
56 : };
57 : size_t m_libspdm_key_exchange_request5_size = sizeof(m_libspdm_key_exchange_request5);
58 :
59 : /* Asks for certificate in slot 1 */
60 : libspdm_key_exchange_request_mine_t m_libspdm_key_exchange_request6 = {
61 : { SPDM_MESSAGE_VERSION_11, SPDM_KEY_EXCHANGE,
62 : SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 1 },
63 : };
64 : size_t m_libspdm_key_exchange_request6_size = sizeof(m_libspdm_key_exchange_request6);
65 :
66 : /* Asks for previously provisioned raw public key */
67 : libspdm_key_exchange_request_mine_t m_libspdm_key_exchange_request7 = {
68 : { SPDM_MESSAGE_VERSION_11, SPDM_KEY_EXCHANGE,
69 : SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0xFF },
70 : };
71 : size_t m_libspdm_key_exchange_request7_size = sizeof(m_libspdm_key_exchange_request7);
72 :
73 : libspdm_key_exchange_request_mine_t m_libspdm_key_exchange_request8 = {
74 : { SPDM_MESSAGE_VERSION_12, SPDM_KEY_EXCHANGE,
75 : SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0 },
76 : };
77 : size_t m_libspdm_key_exchange_request8_size = sizeof(m_libspdm_key_exchange_request8);
78 :
79 : libspdm_key_exchange_request_mine_t m_libspdm_key_exchange_request9 = {
80 : { SPDM_MESSAGE_VERSION_11, SPDM_KEY_EXCHANGE,
81 : SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 9 },
82 : };
83 : size_t m_libspdm_key_exchange_request9_size = sizeof(m_libspdm_key_exchange_request9);
84 :
85 : libspdm_key_exchange_request_mine_t m_libspdm_key_exchange_request10 = {
86 : { SPDM_MESSAGE_VERSION_13, SPDM_KEY_EXCHANGE,
87 : SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0 },
88 : };
89 : size_t m_libspdm_key_exchange_request10_size = sizeof(m_libspdm_key_exchange_request10);
90 :
91 : extern uint8_t g_key_exchange_start_mut_auth;
92 : extern uint8_t g_key_exchange_req_slot_id;
93 : extern bool g_mandatory_mut_auth;
94 : extern bool g_generate_key_exchange_opaque_data;
95 :
96 : extern bool g_event_all_subscribe;
97 : extern bool g_event_all_unsubscribe;
98 :
99 27 : static int rsp_key_exchange_rsp_setup(void **state)
100 : {
101 27 : libspdm_key_exchange_request_mine_t *const requests[] = {
102 : &m_libspdm_key_exchange_request1, &m_libspdm_key_exchange_request2,
103 : &m_libspdm_key_exchange_request3, &m_libspdm_key_exchange_request4,
104 : &m_libspdm_key_exchange_request5, &m_libspdm_key_exchange_request6,
105 : &m_libspdm_key_exchange_request7, &m_libspdm_key_exchange_request8,
106 : &m_libspdm_key_exchange_request9, &m_libspdm_key_exchange_request10,
107 : };
108 : libspdm_context_t *spdm_context;
109 : size_t index;
110 :
111 : /* SessionPolicy is the only request field that a test writes and a later user of the same
112 : * request does not write before sending it. */
113 297 : for (index = 0; index < LIBSPDM_ARRAY_SIZE(requests); index++) {
114 270 : requests[index]->session_policy = 0;
115 : }
116 :
117 : /* A test that turns on multi-key, or that narrows a slot's key usage, is the only one that
118 : * sets either field, so restore the values libspdm_init_context leaves them at. */
119 27 : spdm_context = libspdm_get_test_context()->spdm_context;
120 27 : spdm_context->connection_info.multi_key_conn_rsp = false;
121 243 : for (index = 0; index < SPDM_MAX_SLOT_COUNT; index++) {
122 216 : spdm_context->local_context.local_key_usage_bit_mask[index] = 0;
123 : }
124 : /* A test that starts an encapsulated flow leaves the response state set. */
125 27 : spdm_context->response_state = LIBSPDM_RESPONSE_STATE_NORMAL;
126 :
127 27 : g_key_exchange_start_mut_auth = 0;
128 27 : g_key_exchange_req_slot_id = 0;
129 27 : g_mandatory_mut_auth = false;
130 27 : g_generate_key_exchange_opaque_data = false;
131 :
132 27 : return 0;
133 : }
134 :
135 1 : static void rsp_key_exchange_rsp_case1(void **state)
136 : {
137 : libspdm_return_t status;
138 : libspdm_test_context_t *spdm_test_context;
139 : libspdm_context_t *spdm_context;
140 : size_t response_size;
141 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
142 : spdm_key_exchange_response_t *spdm_response;
143 : void *data1;
144 : size_t data_size1;
145 : uint8_t *ptr;
146 : size_t dhe_key_size;
147 : void *dhe_context;
148 : size_t opaque_key_exchange_req_size;
149 : uint32_t session_id;
150 :
151 1 : spdm_test_context = *state;
152 1 : spdm_context = spdm_test_context->spdm_context;
153 1 : spdm_test_context->case_id = 0x1;
154 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
155 1 : spdm_context->connection_info.capability.flags |=
156 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
157 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
158 1 : spdm_context->local_context.capability.flags |=
159 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
160 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP |
161 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
162 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
163 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
164 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
165 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
166 : m_libspdm_use_measurement_hash_algo;
167 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
168 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
169 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
170 : SPDM_VERSION_NUMBER_SHIFT_BIT;
171 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
172 : m_libspdm_use_asym_algo, &data1,
173 : &data_size1, NULL, NULL)) {
174 0 : return;
175 : }
176 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
177 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
178 :
179 1 : libspdm_reset_message_a(spdm_context);
180 :
181 1 : spdm_context->local_context.secured_message_version.secured_message_version_count = 1;
182 :
183 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request1.random_data);
184 1 : m_libspdm_key_exchange_request1.req_session_id = 0xFFFF;
185 1 : m_libspdm_key_exchange_request1.reserved = 0;
186 1 : ptr = m_libspdm_key_exchange_request1.exchange_data;
187 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
188 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
189 : false);
190 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
191 1 : ptr += dhe_key_size;
192 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
193 1 : opaque_key_exchange_req_size =
194 1 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
195 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
196 1 : ptr += sizeof(uint16_t);
197 1 : libspdm_build_opaque_data_supported_version_data(
198 : spdm_context, &opaque_key_exchange_req_size, ptr);
199 1 : ptr += opaque_key_exchange_req_size;
200 1 : response_size = sizeof(response);
201 1 : status = libspdm_get_response_key_exchange(
202 : spdm_context, m_libspdm_key_exchange_request1_size,
203 : &m_libspdm_key_exchange_request1, &response_size, response);
204 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
205 1 : assert_int_equal(
206 : libspdm_secured_message_get_session_state(
207 : spdm_context->session_info[0].secured_message_context),
208 : LIBSPDM_SESSION_STATE_HANDSHAKING);
209 1 : spdm_response = (void *)response;
210 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_KEY_EXCHANGE_RSP);
211 1 : assert_int_equal(spdm_response->rsp_session_id, 0xFFFF);
212 :
213 1 : session_id = (m_libspdm_key_exchange_request1.req_session_id << 16) |
214 1 : spdm_response->rsp_session_id;
215 1 : libspdm_free_session_id(spdm_context, session_id);
216 1 : free(data1);
217 : }
218 :
219 1 : static void rsp_key_exchange_rsp_case2(void **state)
220 : {
221 : libspdm_return_t status;
222 : libspdm_test_context_t *spdm_test_context;
223 : libspdm_context_t *spdm_context;
224 : size_t response_size;
225 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
226 : spdm_key_exchange_response_t *spdm_response;
227 : void *data1;
228 : size_t data_size1;
229 : uint8_t *ptr;
230 : size_t dhe_key_size;
231 : void *dhe_context;
232 : size_t opaque_key_exchange_req_size;
233 :
234 1 : spdm_test_context = *state;
235 1 : spdm_context = spdm_test_context->spdm_context;
236 1 : spdm_test_context->case_id = 0x2;
237 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
238 1 : spdm_context->connection_info.capability.flags |=
239 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
240 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
241 1 : spdm_context->local_context.capability.flags |=
242 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
243 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
244 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
245 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
246 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
247 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
248 : m_libspdm_use_measurement_hash_algo;
249 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
250 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
251 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
252 : SPDM_VERSION_NUMBER_SHIFT_BIT;
253 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
254 : m_libspdm_use_asym_algo, &data1,
255 : &data_size1, NULL, NULL)) {
256 0 : return;
257 : }
258 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
259 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
260 :
261 1 : libspdm_reset_message_a(spdm_context);
262 :
263 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request2.random_data);
264 1 : m_libspdm_key_exchange_request2.req_session_id = 0xFFFF;
265 1 : m_libspdm_key_exchange_request2.reserved = 0;
266 1 : ptr = m_libspdm_key_exchange_request2.exchange_data;
267 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
268 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
269 : false);
270 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
271 1 : ptr += dhe_key_size;
272 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
273 1 : opaque_key_exchange_req_size =
274 1 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
275 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
276 1 : ptr += sizeof(uint16_t);
277 1 : libspdm_build_opaque_data_supported_version_data(
278 : spdm_context, &opaque_key_exchange_req_size, ptr);
279 1 : ptr += opaque_key_exchange_req_size;
280 1 : response_size = sizeof(response);
281 1 : status = libspdm_get_response_key_exchange(
282 : spdm_context, m_libspdm_key_exchange_request2_size,
283 : &m_libspdm_key_exchange_request2, &response_size, response);
284 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
285 1 : assert_int_equal(response_size, sizeof(spdm_error_response_t));
286 1 : spdm_response = (void *)response;
287 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
288 1 : assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_INVALID_REQUEST);
289 1 : assert_int_equal(spdm_response->header.param2, 0);
290 1 : free(data1);
291 : }
292 :
293 1 : static void rsp_key_exchange_rsp_case3(void **state)
294 : {
295 : libspdm_return_t status;
296 : libspdm_test_context_t *spdm_test_context;
297 : libspdm_context_t *spdm_context;
298 : size_t response_size;
299 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
300 : spdm_key_exchange_response_t *spdm_response;
301 : void *data1;
302 : size_t data_size1;
303 : uint8_t *ptr;
304 : size_t dhe_key_size;
305 : void *dhe_context;
306 : size_t opaque_key_exchange_req_size;
307 :
308 1 : spdm_test_context = *state;
309 1 : spdm_context = spdm_test_context->spdm_context;
310 1 : spdm_test_context->case_id = 0x3;
311 1 : spdm_context->response_state = LIBSPDM_RESPONSE_STATE_BUSY;
312 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
313 1 : spdm_context->connection_info.capability.flags |=
314 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
315 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
316 1 : spdm_context->local_context.capability.flags |=
317 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
318 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
319 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
320 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
321 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
322 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
323 : m_libspdm_use_measurement_hash_algo;
324 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
325 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
326 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
327 : SPDM_VERSION_NUMBER_SHIFT_BIT;
328 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
329 : m_libspdm_use_asym_algo, &data1,
330 : &data_size1, NULL, NULL)) {
331 0 : return;
332 : }
333 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
334 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
335 :
336 1 : libspdm_reset_message_a(spdm_context);
337 :
338 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request1.random_data);
339 1 : m_libspdm_key_exchange_request1.req_session_id = 0xFFFF;
340 1 : m_libspdm_key_exchange_request1.reserved = 0;
341 1 : ptr = m_libspdm_key_exchange_request1.exchange_data;
342 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
343 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
344 : false);
345 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
346 1 : ptr += dhe_key_size;
347 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
348 1 : opaque_key_exchange_req_size =
349 1 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
350 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
351 1 : ptr += sizeof(uint16_t);
352 1 : libspdm_build_opaque_data_supported_version_data(
353 : spdm_context, &opaque_key_exchange_req_size, ptr);
354 1 : ptr += opaque_key_exchange_req_size;
355 1 : response_size = sizeof(response);
356 1 : status = libspdm_get_response_key_exchange(
357 : spdm_context, m_libspdm_key_exchange_request1_size,
358 : &m_libspdm_key_exchange_request1, &response_size, response);
359 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
360 1 : assert_int_equal(response_size, sizeof(spdm_error_response_t));
361 1 : spdm_response = (void *)response;
362 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
363 1 : assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_BUSY);
364 1 : assert_int_equal(spdm_response->header.param2, 0);
365 1 : assert_int_equal(spdm_context->response_state, LIBSPDM_RESPONSE_STATE_BUSY);
366 1 : free(data1);
367 : }
368 :
369 1 : static void rsp_key_exchange_rsp_case4(void **state)
370 : {
371 : libspdm_return_t status;
372 : libspdm_test_context_t *spdm_test_context;
373 : libspdm_context_t *spdm_context;
374 : size_t response_size;
375 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
376 : spdm_key_exchange_response_t *spdm_response;
377 : void *data1;
378 : size_t data_size1;
379 : uint8_t *ptr;
380 : size_t dhe_key_size;
381 : void *dhe_context;
382 : size_t opaque_key_exchange_req_size;
383 :
384 1 : spdm_test_context = *state;
385 1 : spdm_context = spdm_test_context->spdm_context;
386 1 : spdm_test_context->case_id = 0x4;
387 1 : spdm_context->response_state = LIBSPDM_RESPONSE_STATE_NEED_RESYNC;
388 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
389 1 : spdm_context->connection_info.capability.flags |=
390 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
391 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
392 1 : spdm_context->local_context.capability.flags |=
393 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
394 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
395 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
396 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
397 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
398 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
399 : m_libspdm_use_measurement_hash_algo;
400 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
401 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
402 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
403 : SPDM_VERSION_NUMBER_SHIFT_BIT;
404 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
405 : m_libspdm_use_asym_algo, &data1,
406 : &data_size1, NULL, NULL)) {
407 0 : return;
408 : }
409 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
410 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
411 :
412 1 : libspdm_reset_message_a(spdm_context);
413 :
414 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request1.random_data);
415 1 : m_libspdm_key_exchange_request1.req_session_id = 0xFFFF;
416 1 : m_libspdm_key_exchange_request1.reserved = 0;
417 1 : ptr = m_libspdm_key_exchange_request1.exchange_data;
418 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
419 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
420 : false);
421 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
422 1 : ptr += dhe_key_size;
423 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
424 1 : opaque_key_exchange_req_size =
425 1 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
426 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
427 1 : ptr += sizeof(uint16_t);
428 1 : libspdm_build_opaque_data_supported_version_data(
429 : spdm_context, &opaque_key_exchange_req_size, ptr);
430 1 : ptr += opaque_key_exchange_req_size;
431 1 : response_size = sizeof(response);
432 1 : status = libspdm_get_response_key_exchange(
433 : spdm_context, m_libspdm_key_exchange_request1_size,
434 : &m_libspdm_key_exchange_request1, &response_size, response);
435 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
436 1 : assert_int_equal(response_size, sizeof(spdm_error_response_t));
437 1 : spdm_response = (void *)response;
438 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
439 1 : assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_REQUEST_RESYNCH);
440 1 : assert_int_equal(spdm_response->header.param2, 0);
441 1 : assert_int_equal(spdm_context->response_state, LIBSPDM_RESPONSE_STATE_NEED_RESYNC);
442 1 : free(data1);
443 : }
444 :
445 : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
446 1 : static void rsp_key_exchange_rsp_case5(void **state)
447 : {
448 : libspdm_return_t status;
449 : libspdm_test_context_t *spdm_test_context;
450 : libspdm_context_t *spdm_context;
451 : size_t response_size;
452 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
453 : spdm_key_exchange_response_t *spdm_response;
454 : void *data1;
455 : size_t data_size1;
456 : spdm_error_data_response_not_ready_t *error_data;
457 : uint8_t *ptr;
458 : size_t dhe_key_size;
459 : void *dhe_context;
460 : size_t opaque_key_exchange_req_size;
461 :
462 1 : spdm_test_context = *state;
463 1 : spdm_context = spdm_test_context->spdm_context;
464 1 : spdm_test_context->case_id = 0x5;
465 1 : spdm_context->response_state = LIBSPDM_RESPONSE_STATE_NOT_READY;
466 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
467 1 : spdm_context->connection_info.capability.flags |=
468 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
469 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
470 1 : spdm_context->local_context.capability.flags |=
471 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
472 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
473 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
474 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
475 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
476 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
477 : m_libspdm_use_measurement_hash_algo;
478 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
479 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
480 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
481 : SPDM_VERSION_NUMBER_SHIFT_BIT;
482 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
483 : m_libspdm_use_asym_algo, &data1,
484 : &data_size1, NULL, NULL)) {
485 0 : return;
486 : }
487 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
488 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
489 :
490 1 : libspdm_reset_message_a(spdm_context);
491 :
492 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request1.random_data);
493 1 : m_libspdm_key_exchange_request1.req_session_id = 0xFFFF;
494 1 : m_libspdm_key_exchange_request1.reserved = 0;
495 1 : ptr = m_libspdm_key_exchange_request1.exchange_data;
496 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
497 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
498 : false);
499 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
500 1 : ptr += dhe_key_size;
501 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
502 1 : opaque_key_exchange_req_size =
503 1 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
504 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
505 1 : ptr += sizeof(uint16_t);
506 1 : libspdm_build_opaque_data_supported_version_data(
507 : spdm_context, &opaque_key_exchange_req_size, ptr);
508 1 : ptr += opaque_key_exchange_req_size;
509 1 : response_size = sizeof(response);
510 1 : status = libspdm_get_response_key_exchange(
511 : spdm_context, m_libspdm_key_exchange_request1_size,
512 : &m_libspdm_key_exchange_request1, &response_size, response);
513 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
514 1 : assert_int_equal(response_size,
515 : sizeof(spdm_error_response_t) +
516 : sizeof(spdm_error_data_response_not_ready_t));
517 1 : spdm_response = (void *)response;
518 1 : error_data = (spdm_error_data_response_not_ready_t
519 : *)(&spdm_response->rsp_session_id);
520 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
521 1 : assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_RESPONSE_NOT_READY);
522 1 : assert_int_equal(spdm_response->header.param2, 0);
523 1 : assert_int_equal(spdm_context->response_state, LIBSPDM_RESPONSE_STATE_NOT_READY);
524 1 : assert_int_equal(error_data->request_code, SPDM_KEY_EXCHANGE);
525 1 : free(data1);
526 : }
527 : #endif /* LIBSPDM_RESPOND_IF_READY_SUPPORT */
528 :
529 1 : static void rsp_key_exchange_rsp_case6(void **state)
530 : {
531 : libspdm_return_t status;
532 : libspdm_test_context_t *spdm_test_context;
533 : libspdm_context_t *spdm_context;
534 : size_t response_size;
535 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
536 : spdm_key_exchange_response_t *spdm_response;
537 : void *data1;
538 : size_t data_size1;
539 : uint8_t *ptr;
540 : size_t dhe_key_size;
541 : void *dhe_context;
542 : size_t opaque_key_exchange_req_size;
543 :
544 1 : spdm_test_context = *state;
545 1 : spdm_context = spdm_test_context->spdm_context;
546 1 : spdm_test_context->case_id = 0x6;
547 1 : spdm_context->response_state = LIBSPDM_RESPONSE_STATE_NORMAL;
548 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NOT_STARTED;
549 1 : spdm_context->connection_info.capability.flags |=
550 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
551 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
552 1 : spdm_context->local_context.capability.flags |=
553 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
554 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
555 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
556 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
557 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
558 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
559 : m_libspdm_use_measurement_hash_algo;
560 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
561 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
562 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
563 : SPDM_VERSION_NUMBER_SHIFT_BIT;
564 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
565 : m_libspdm_use_asym_algo, &data1,
566 : &data_size1, NULL, NULL)) {
567 0 : return;
568 : }
569 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
570 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
571 :
572 1 : libspdm_reset_message_a(spdm_context);
573 :
574 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request1.random_data);
575 1 : m_libspdm_key_exchange_request1.req_session_id = 0xFFFF;
576 1 : m_libspdm_key_exchange_request1.reserved = 0;
577 1 : ptr = m_libspdm_key_exchange_request1.exchange_data;
578 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
579 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
580 : false);
581 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
582 1 : ptr += dhe_key_size;
583 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
584 1 : opaque_key_exchange_req_size =
585 1 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
586 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
587 1 : ptr += sizeof(uint16_t);
588 1 : libspdm_build_opaque_data_supported_version_data(
589 : spdm_context, &opaque_key_exchange_req_size, ptr);
590 1 : ptr += opaque_key_exchange_req_size;
591 1 : response_size = sizeof(response);
592 1 : status = libspdm_get_response_key_exchange(
593 : spdm_context, m_libspdm_key_exchange_request1_size,
594 : &m_libspdm_key_exchange_request1, &response_size, response);
595 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
596 1 : assert_int_equal(response_size, sizeof(spdm_error_response_t));
597 1 : spdm_response = (void *)response;
598 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
599 1 : assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_UNEXPECTED_REQUEST);
600 1 : assert_int_equal(spdm_response->header.param2, 0);
601 1 : free(data1);
602 : }
603 :
604 1 : static void rsp_key_exchange_rsp_case7(void **state)
605 : {
606 : libspdm_return_t status;
607 : libspdm_test_context_t *spdm_test_context;
608 : libspdm_context_t *spdm_context;
609 : size_t response_size;
610 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
611 : spdm_key_exchange_response_t *spdm_response;
612 : void *data1;
613 : size_t data_size1;
614 : uint8_t *ptr;
615 : size_t dhe_key_size;
616 : void *dhe_context;
617 : size_t opaque_key_exchange_req_size;
618 :
619 1 : spdm_test_context = *state;
620 1 : spdm_context = spdm_test_context->spdm_context;
621 1 : spdm_test_context->case_id = 0x1;
622 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
623 1 : spdm_context->connection_info.capability.flags |=
624 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
625 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
626 1 : spdm_context->local_context.capability.flags |=
627 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
628 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
629 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
630 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
631 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
632 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
633 : m_libspdm_use_measurement_hash_algo;
634 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
635 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
636 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
637 : SPDM_VERSION_NUMBER_SHIFT_BIT;
638 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
639 : m_libspdm_use_asym_algo, &data1,
640 : &data_size1, NULL, NULL)) {
641 0 : return;
642 : }
643 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
644 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
645 :
646 1 : libspdm_reset_message_a(spdm_context);
647 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
648 : spdm_context->transcript.message_m.buffer_size =
649 : spdm_context->transcript.message_m.max_buffer_size;
650 : spdm_context->transcript.message_b.buffer_size =
651 : spdm_context->transcript.message_b.max_buffer_size;
652 : spdm_context->transcript.message_c.buffer_size =
653 : spdm_context->transcript.message_c.max_buffer_size;
654 : spdm_context->transcript.message_mut_b.buffer_size =
655 : spdm_context->transcript.message_mut_b.max_buffer_size;
656 : spdm_context->transcript.message_mut_c.buffer_size =
657 : spdm_context->transcript.message_mut_c.max_buffer_size;
658 : #endif
659 :
660 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request1.random_data);
661 1 : m_libspdm_key_exchange_request1.req_session_id = 0xFFFF;
662 1 : m_libspdm_key_exchange_request1.reserved = 0;
663 1 : ptr = m_libspdm_key_exchange_request1.exchange_data;
664 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
665 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
666 : false);
667 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
668 1 : ptr += dhe_key_size;
669 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
670 1 : opaque_key_exchange_req_size =
671 1 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
672 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
673 1 : ptr += sizeof(uint16_t);
674 1 : libspdm_build_opaque_data_supported_version_data(
675 : spdm_context, &opaque_key_exchange_req_size, ptr);
676 1 : ptr += opaque_key_exchange_req_size;
677 1 : response_size = sizeof(response);
678 1 : status = libspdm_get_response_key_exchange(
679 : spdm_context, m_libspdm_key_exchange_request1_size,
680 : &m_libspdm_key_exchange_request1, &response_size, response);
681 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
682 1 : assert_int_equal(
683 : libspdm_secured_message_get_session_state(
684 : spdm_context->session_info[0].secured_message_context),
685 : LIBSPDM_SESSION_STATE_HANDSHAKING);
686 1 : spdm_response = (void *)response;
687 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_KEY_EXCHANGE_RSP);
688 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
689 : assert_int_equal(spdm_context->transcript.message_m.buffer_size, 0);
690 : assert_int_equal(spdm_context->transcript.message_b.buffer_size, 0);
691 : assert_int_equal(spdm_context->transcript.message_c.buffer_size, 0);
692 : assert_int_equal(spdm_context->transcript.message_mut_b.buffer_size, 0);
693 : assert_int_equal(spdm_context->transcript.message_mut_c.buffer_size, 0);
694 : #endif
695 :
696 1 : free(data1);
697 : }
698 :
699 1 : static void rsp_key_exchange_rsp_case8(void **state)
700 : {
701 : libspdm_return_t status;
702 : libspdm_test_context_t *spdm_test_context;
703 : libspdm_context_t *spdm_context;
704 : size_t response_size;
705 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
706 : #if LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP
707 : uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
708 : #endif /* LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP */
709 : uint32_t measurement_summary_hash_size;
710 : spdm_key_exchange_response_t *spdm_response;
711 : void *data1;
712 : size_t data_size1;
713 : uint8_t *ptr;
714 : size_t dhe_key_size;
715 : void *dhe_context;
716 : size_t opaque_key_exchange_req_size;
717 : bool result;
718 :
719 1 : spdm_test_context = *state;
720 1 : spdm_context = spdm_test_context->spdm_context;
721 1 : spdm_test_context->case_id = 0x8;
722 :
723 : /* Clear previous sessions */
724 1 : if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
725 1 : libspdm_free_session_id(spdm_context,0xFFFFFFFF);
726 : }
727 :
728 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
729 1 : spdm_context->connection_info.capability.flags |=
730 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
731 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
732 1 : spdm_context->local_context.capability.flags |=
733 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
734 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
735 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP;
736 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
737 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
738 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
739 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
740 : m_libspdm_use_measurement_hash_algo;
741 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
742 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
743 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
744 : SPDM_VERSION_NUMBER_SHIFT_BIT;
745 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
746 : m_libspdm_use_asym_algo, &data1,
747 : &data_size1, NULL, NULL)) {
748 0 : return;
749 : }
750 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
751 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
752 :
753 1 : libspdm_reset_message_a(spdm_context);
754 :
755 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request3.random_data);
756 1 : m_libspdm_key_exchange_request3.req_session_id = 0xFFFF;
757 1 : m_libspdm_key_exchange_request3.reserved = 0;
758 1 : ptr = m_libspdm_key_exchange_request3.exchange_data;
759 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
760 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
761 : false);
762 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
763 1 : ptr += dhe_key_size;
764 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
765 1 : opaque_key_exchange_req_size =
766 1 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
767 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
768 1 : ptr += sizeof(uint16_t);
769 1 : libspdm_build_opaque_data_supported_version_data(
770 : spdm_context, &opaque_key_exchange_req_size, ptr);
771 1 : ptr += opaque_key_exchange_req_size;
772 1 : response_size = sizeof(response);
773 1 : status = libspdm_get_response_key_exchange(
774 : spdm_context, m_libspdm_key_exchange_request3_size,
775 : &m_libspdm_key_exchange_request3, &response_size, response);
776 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
777 1 : assert_int_equal(
778 : libspdm_secured_message_get_session_state(
779 : spdm_context->session_info[0].secured_message_context),
780 : LIBSPDM_SESSION_STATE_HANDSHAKING);
781 1 : spdm_response = (void *)response;
782 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_KEY_EXCHANGE_RSP);
783 1 : assert_int_equal(spdm_response->rsp_session_id, 0xFFFF);
784 :
785 1 : measurement_summary_hash_size = libspdm_get_measurement_summary_hash_size(
786 1 : spdm_context, false, m_libspdm_key_exchange_request3.header.param1);
787 :
788 : #if LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP
789 1 : result = libspdm_generate_measurement_summary_hash(
790 : spdm_context,
791 1 : spdm_context->connection_info.version,
792 : spdm_context->connection_info.algorithm.base_hash_algo,
793 1 : spdm_context->connection_info.algorithm.measurement_spec,
794 : spdm_context->connection_info.algorithm.measurement_hash_algo,
795 1 : m_libspdm_key_exchange_request3.header.param1,
796 : measurement_hash,
797 : measurement_summary_hash_size);
798 :
799 1 : assert_true(result);
800 :
801 1 : assert_memory_equal((uint8_t *)response + sizeof(spdm_key_exchange_response_t) + dhe_key_size,
802 : measurement_hash, measurement_summary_hash_size);
803 : #endif /* LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP */
804 1 : free(data1);
805 : }
806 :
807 1 : static void rsp_key_exchange_rsp_case9(void **state)
808 : {
809 : libspdm_return_t status;
810 : libspdm_test_context_t *spdm_test_context;
811 : libspdm_context_t *spdm_context;
812 : size_t response_size;
813 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
814 : #if LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP
815 : uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
816 : #endif /* LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP */
817 : uint32_t measurement_summary_hash_size;
818 : spdm_key_exchange_response_t *spdm_response;
819 : void *data1;
820 : size_t data_size1;
821 : uint8_t *ptr;
822 : size_t dhe_key_size;
823 : void *dhe_context;
824 : size_t opaque_key_exchange_req_size;
825 : uint32_t session_id;
826 : bool result;
827 :
828 1 : spdm_test_context = *state;
829 1 : spdm_context = spdm_test_context->spdm_context;
830 1 : spdm_test_context->case_id = 0x9;
831 :
832 : /* Clear previous sessions */
833 1 : if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
834 1 : libspdm_free_session_id(spdm_context,0xFFFFFFFF);
835 : }
836 :
837 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
838 1 : spdm_context->connection_info.capability.flags |=
839 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
840 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
841 1 : spdm_context->local_context.capability.flags |=
842 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
843 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
844 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP;
845 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
846 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
847 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
848 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
849 : m_libspdm_use_measurement_hash_algo;
850 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
851 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
852 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
853 : SPDM_VERSION_NUMBER_SHIFT_BIT;
854 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
855 : m_libspdm_use_asym_algo, &data1,
856 : &data_size1, NULL, NULL)) {
857 0 : return;
858 : }
859 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
860 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
861 :
862 1 : libspdm_reset_message_a(spdm_context);
863 :
864 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request4.random_data);
865 1 : m_libspdm_key_exchange_request4.req_session_id = 0xFFFF;
866 1 : m_libspdm_key_exchange_request4.reserved = 0;
867 1 : ptr = m_libspdm_key_exchange_request4.exchange_data;
868 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
869 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
870 : false);
871 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
872 1 : ptr += dhe_key_size;
873 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
874 1 : opaque_key_exchange_req_size =
875 1 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
876 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
877 1 : ptr += sizeof(uint16_t);
878 1 : libspdm_build_opaque_data_supported_version_data(
879 : spdm_context, &opaque_key_exchange_req_size, ptr);
880 1 : ptr += opaque_key_exchange_req_size;
881 1 : response_size = sizeof(response);
882 1 : status = libspdm_get_response_key_exchange(
883 : spdm_context, m_libspdm_key_exchange_request4_size,
884 : &m_libspdm_key_exchange_request4, &response_size, response);
885 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
886 1 : assert_int_equal(
887 : libspdm_secured_message_get_session_state(
888 : spdm_context->session_info[0].secured_message_context),
889 : LIBSPDM_SESSION_STATE_HANDSHAKING);
890 1 : spdm_response = (void *)response;
891 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_KEY_EXCHANGE_RSP);
892 1 : assert_int_equal(spdm_response->rsp_session_id, 0xFFFF);
893 :
894 1 : measurement_summary_hash_size = libspdm_get_measurement_summary_hash_size(
895 1 : spdm_context, false, m_libspdm_key_exchange_request4.header.param1);
896 : #if LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP
897 1 : result = libspdm_generate_measurement_summary_hash(
898 : spdm_context,
899 1 : spdm_context->connection_info.version,
900 : spdm_context->connection_info.algorithm.base_hash_algo,
901 1 : spdm_context->connection_info.algorithm.measurement_spec,
902 : spdm_context->connection_info.algorithm.measurement_hash_algo,
903 1 : m_libspdm_key_exchange_request4.header.param1,
904 : measurement_hash,
905 : measurement_summary_hash_size);
906 :
907 1 : assert_true(result);
908 :
909 1 : assert_memory_equal((uint8_t *)response + sizeof(spdm_key_exchange_response_t) + dhe_key_size,
910 : measurement_hash, measurement_summary_hash_size);
911 : #endif /* LIBSPDM_ENABLE_CAPABILITY_MEAS_CAP */
912 :
913 1 : session_id = (m_libspdm_key_exchange_request4.req_session_id << 16) |
914 1 : spdm_response->rsp_session_id;
915 1 : libspdm_free_session_id(spdm_context, session_id);
916 :
917 1 : free(data1);
918 : }
919 :
920 1 : static void rsp_key_exchange_rsp_case10(void **state)
921 : {
922 : libspdm_return_t status;
923 : libspdm_test_context_t *spdm_test_context;
924 : libspdm_context_t *spdm_context;
925 : size_t response_size;
926 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
927 : spdm_key_exchange_response_t *spdm_response;
928 : void *data1;
929 : size_t data_size1;
930 : uint8_t *ptr;
931 : size_t dhe_key_size;
932 : void *dhe_context;
933 : size_t opaque_key_exchange_req_size;
934 :
935 1 : spdm_test_context = *state;
936 1 : spdm_context = spdm_test_context->spdm_context;
937 1 : spdm_test_context->case_id = 0xA;
938 :
939 : /* Clear previous sessions */
940 1 : if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
941 0 : libspdm_free_session_id(spdm_context,0xFFFFFFFF);
942 : }
943 :
944 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
945 1 : spdm_context->connection_info.capability.flags |=
946 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
947 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
948 1 : spdm_context->local_context.capability.flags |=
949 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
950 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
951 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP;
952 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
953 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
954 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
955 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
956 : m_libspdm_use_measurement_hash_algo;
957 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
958 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
959 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
960 : SPDM_VERSION_NUMBER_SHIFT_BIT;
961 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
962 : m_libspdm_use_asym_algo, &data1,
963 : &data_size1, NULL, NULL)) {
964 0 : return;
965 : }
966 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
967 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
968 :
969 1 : libspdm_reset_message_a(spdm_context);
970 :
971 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request5.random_data);
972 1 : m_libspdm_key_exchange_request5.req_session_id = 0xFFFF;
973 1 : m_libspdm_key_exchange_request5.reserved = 0;
974 1 : ptr = m_libspdm_key_exchange_request5.exchange_data;
975 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
976 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
977 : false);
978 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
979 1 : ptr += dhe_key_size;
980 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
981 1 : opaque_key_exchange_req_size =
982 1 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
983 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
984 1 : ptr += sizeof(uint16_t);
985 1 : libspdm_build_opaque_data_supported_version_data(
986 : spdm_context, &opaque_key_exchange_req_size, ptr);
987 1 : ptr += opaque_key_exchange_req_size;
988 1 : response_size = sizeof(response);
989 1 : status = libspdm_get_response_key_exchange(
990 : spdm_context, m_libspdm_key_exchange_request5_size,
991 : &m_libspdm_key_exchange_request5, &response_size, response);
992 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
993 :
994 1 : spdm_response = (void *)response;
995 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
996 1 : assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_INVALID_REQUEST);
997 :
998 1 : free(data1);
999 : }
1000 :
1001 1 : static void rsp_key_exchange_rsp_case11(void **state)
1002 : {
1003 : libspdm_return_t status;
1004 : libspdm_test_context_t *spdm_test_context;
1005 : libspdm_context_t *spdm_context;
1006 : size_t response_size;
1007 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
1008 : spdm_key_exchange_response_t *spdm_response;
1009 : void *data1;
1010 : size_t data_size1;
1011 : uint8_t *ptr;
1012 : size_t dhe_key_size;
1013 : void *dhe_context;
1014 : size_t opaque_key_exchange_req_size;
1015 :
1016 1 : spdm_test_context = *state;
1017 1 : spdm_context = spdm_test_context->spdm_context;
1018 1 : spdm_test_context->case_id = 0xB;
1019 :
1020 : /* Clear previous sessions */
1021 1 : if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
1022 0 : libspdm_free_session_id(spdm_context,0xFFFFFFFF);
1023 : }
1024 :
1025 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1026 :
1027 : /* Clear capabilities flag */
1028 1 : spdm_context->local_context.capability.flags &= ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP;
1029 :
1030 : /*set capabilities flags */
1031 1 : spdm_context->connection_info.capability.flags |=
1032 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
1033 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
1034 1 : spdm_context->local_context.capability.flags |=
1035 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
1036 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
1037 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1038 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
1039 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
1040 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
1041 : m_libspdm_use_measurement_hash_algo;
1042 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
1043 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
1044 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
1045 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1046 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
1047 : m_libspdm_use_asym_algo, &data1,
1048 : &data_size1, NULL, NULL)) {
1049 0 : return;
1050 : }
1051 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
1052 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
1053 :
1054 1 : libspdm_reset_message_a(spdm_context);
1055 :
1056 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request3.random_data);
1057 1 : m_libspdm_key_exchange_request3.req_session_id = 0xFFFF;
1058 1 : m_libspdm_key_exchange_request3.reserved = 0;
1059 1 : ptr = m_libspdm_key_exchange_request3.exchange_data;
1060 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
1061 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
1062 : false);
1063 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
1064 1 : ptr += dhe_key_size;
1065 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
1066 1 : opaque_key_exchange_req_size =
1067 1 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
1068 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
1069 1 : ptr += sizeof(uint16_t);
1070 1 : libspdm_build_opaque_data_supported_version_data(
1071 : spdm_context, &opaque_key_exchange_req_size, ptr);
1072 1 : ptr += opaque_key_exchange_req_size;
1073 1 : response_size = sizeof(response);
1074 1 : status = libspdm_get_response_key_exchange(
1075 : spdm_context, m_libspdm_key_exchange_request3_size,
1076 : &m_libspdm_key_exchange_request3, &response_size, response);
1077 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
1078 :
1079 1 : spdm_response = (void *)response;
1080 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
1081 1 : assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_INVALID_REQUEST);
1082 :
1083 1 : free(data1);
1084 : }
1085 :
1086 1 : static void rsp_key_exchange_rsp_case14(void **state)
1087 : {
1088 : libspdm_return_t status;
1089 : libspdm_test_context_t *spdm_test_context;
1090 : libspdm_context_t *spdm_context;
1091 : size_t response_size;
1092 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
1093 : spdm_key_exchange_response_t *spdm_response;
1094 : void *data1;
1095 : size_t data_size1;
1096 : void *data2;
1097 : size_t data_size2;
1098 : uint8_t *ptr;
1099 : size_t dhe_key_size;
1100 : void *dhe_context;
1101 : size_t opaque_key_exchange_req_size;
1102 :
1103 1 : spdm_test_context = *state;
1104 1 : spdm_context = spdm_test_context->spdm_context;
1105 1 : spdm_test_context->case_id = 0xE;
1106 :
1107 : /* Clear previous sessions */
1108 1 : if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
1109 0 : libspdm_free_session_id(spdm_context,0xFFFFFFFF);
1110 : }
1111 :
1112 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1113 1 : spdm_context->connection_info.capability.flags =
1114 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
1115 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
1116 1 : spdm_context->local_context.capability.flags =
1117 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
1118 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
1119 1 : spdm_context->connection_info.capability.flags |=
1120 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PUB_KEY_ID_CAP;
1121 1 : spdm_context->local_context.capability.flags |=
1122 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_PUB_KEY_ID_CAP;
1123 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1124 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
1125 1 : spdm_context->connection_info.algorithm.req_base_asym_alg = m_libspdm_use_req_asym_algo;
1126 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
1127 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
1128 : m_libspdm_use_measurement_hash_algo;
1129 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
1130 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
1131 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
1132 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1133 1 : if (!libspdm_read_responder_public_key(m_libspdm_use_asym_algo, &data1, &data_size1)) {
1134 0 : return;
1135 : }
1136 1 : spdm_context->local_context.local_public_key_provision = data1;
1137 1 : spdm_context->local_context.local_public_key_provision_size = data_size1;
1138 1 : if (!libspdm_read_requester_public_key(m_libspdm_use_req_asym_algo, &data2, &data_size2)) {
1139 0 : return;
1140 : }
1141 1 : spdm_context->local_context.peer_public_key_provision = data2;
1142 1 : spdm_context->local_context.peer_public_key_provision_size = data_size2;
1143 :
1144 1 : libspdm_reset_message_a(spdm_context);
1145 :
1146 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request7.random_data);
1147 1 : m_libspdm_key_exchange_request7.req_session_id = 0xFFFF;
1148 1 : m_libspdm_key_exchange_request7.reserved = 0;
1149 1 : ptr = m_libspdm_key_exchange_request7.exchange_data;
1150 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
1151 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
1152 : false);
1153 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
1154 1 : ptr += dhe_key_size;
1155 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
1156 1 : opaque_key_exchange_req_size =
1157 1 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
1158 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
1159 1 : ptr += sizeof(uint16_t);
1160 1 : libspdm_build_opaque_data_supported_version_data(
1161 : spdm_context, &opaque_key_exchange_req_size, ptr);
1162 1 : ptr += opaque_key_exchange_req_size;
1163 1 : response_size = sizeof(response);
1164 1 : status = libspdm_get_response_key_exchange(
1165 : spdm_context, m_libspdm_key_exchange_request7_size,
1166 : &m_libspdm_key_exchange_request7, &response_size, response);
1167 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
1168 1 : assert_int_equal(
1169 : libspdm_secured_message_get_session_state(
1170 : spdm_context->session_info[0].secured_message_context),
1171 : LIBSPDM_SESSION_STATE_HANDSHAKING);
1172 1 : spdm_response = (void *)response;
1173 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_KEY_EXCHANGE_RSP);
1174 1 : assert_int_equal(spdm_response->rsp_session_id, 0xFFFF);
1175 1 : free(data1);
1176 1 : free(data2);
1177 : }
1178 :
1179 1 : static void rsp_key_exchange_rsp_case15(void **state)
1180 : {
1181 : libspdm_return_t status;
1182 : libspdm_test_context_t *spdm_test_context;
1183 : libspdm_context_t *spdm_context;
1184 : size_t response_size;
1185 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
1186 : spdm_key_exchange_response_t *spdm_response;
1187 : void *data1;
1188 : size_t data_size1;
1189 : uint8_t *ptr;
1190 : size_t dhe_key_size;
1191 : void *dhe_context;
1192 : size_t opaque_key_exchange_req_size;
1193 : size_t opaque_key_exchange_rsp_size;
1194 :
1195 1 : spdm_test_context = *state;
1196 1 : spdm_context = spdm_test_context->spdm_context;
1197 1 : spdm_test_context->case_id = 0xF;
1198 :
1199 : /* Clear previous sessions */
1200 1 : if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
1201 1 : libspdm_free_session_id(spdm_context,0xFFFFFFFF);
1202 : }
1203 :
1204 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1205 1 : spdm_context->connection_info.capability.flags =
1206 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
1207 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
1208 1 : spdm_context->local_context.capability.flags =
1209 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
1210 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
1211 1 : spdm_context->connection_info.capability.flags |=
1212 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP;
1213 1 : spdm_context->local_context.capability.flags |=
1214 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_HANDSHAKE_IN_THE_CLEAR_CAP |
1215 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
1216 :
1217 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1218 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
1219 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
1220 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
1221 : m_libspdm_use_measurement_hash_algo;
1222 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
1223 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
1224 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
1225 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1226 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
1227 : m_libspdm_use_asym_algo, &data1,
1228 : &data_size1, NULL, NULL)) {
1229 0 : return;
1230 : }
1231 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
1232 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
1233 :
1234 1 : libspdm_reset_message_a(spdm_context);
1235 :
1236 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request1.random_data);
1237 1 : m_libspdm_key_exchange_request1.req_session_id = 0xFFFF;
1238 1 : m_libspdm_key_exchange_request1.reserved = 0;
1239 1 : ptr = m_libspdm_key_exchange_request1.exchange_data;
1240 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
1241 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
1242 : false);
1243 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
1244 1 : ptr += dhe_key_size;
1245 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
1246 1 : opaque_key_exchange_req_size =
1247 1 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
1248 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
1249 1 : ptr += sizeof(uint16_t);
1250 1 : libspdm_build_opaque_data_supported_version_data(
1251 : spdm_context, &opaque_key_exchange_req_size, ptr);
1252 1 : ptr += opaque_key_exchange_req_size;
1253 1 : response_size = sizeof(response);
1254 :
1255 : /* Required to compute response size independently */
1256 : opaque_key_exchange_rsp_size =
1257 1 : libspdm_get_opaque_data_version_selection_data_size(spdm_context);
1258 :
1259 1 : status = libspdm_get_response_key_exchange(
1260 : spdm_context, m_libspdm_key_exchange_request1_size,
1261 : &m_libspdm_key_exchange_request1, &response_size, response);
1262 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
1263 1 : assert_int_equal(
1264 : libspdm_secured_message_get_session_state(
1265 : spdm_context->session_info[0].secured_message_context),
1266 : LIBSPDM_SESSION_STATE_HANDSHAKING);
1267 1 : spdm_response = (void *)response;
1268 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_KEY_EXCHANGE_RSP);
1269 1 : assert_int_equal(spdm_response->rsp_session_id, 0xFFFF);
1270 1 : assert_int_equal(response_size,
1271 : sizeof(spdm_key_exchange_response_t) +
1272 : dhe_key_size +
1273 : sizeof(uint16_t) +
1274 : opaque_key_exchange_rsp_size +
1275 : libspdm_get_asym_signature_size(
1276 : spdm_context->connection_info.algorithm.base_asym_algo)
1277 : );
1278 :
1279 1 : free(data1);
1280 : }
1281 :
1282 1 : static void rsp_key_exchange_rsp_case16(void **state)
1283 : {
1284 : libspdm_return_t status;
1285 : libspdm_test_context_t *spdm_test_context;
1286 : libspdm_context_t *spdm_context;
1287 : size_t current_request_size;
1288 : size_t response_size;
1289 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
1290 : spdm_key_exchange_response_t *spdm_response;
1291 : void *data1;
1292 : size_t data_size1;
1293 : uint8_t *ptr;
1294 : size_t dhe_key_size;
1295 : void *dhe_context;
1296 : size_t opaque_key_exchange_req_size;
1297 :
1298 1 : spdm_test_context = *state;
1299 1 : spdm_context = spdm_test_context->spdm_context;
1300 1 : spdm_test_context->case_id = 0x10;
1301 :
1302 1 : if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
1303 1 : libspdm_free_session_id(spdm_context,0xFFFFFFFF);
1304 : }
1305 :
1306 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1307 1 : spdm_context->connection_info.capability.flags |=
1308 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
1309 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
1310 1 : spdm_context->local_context.capability.flags |=
1311 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
1312 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
1313 :
1314 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1315 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
1316 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
1317 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
1318 : m_libspdm_use_measurement_hash_algo;
1319 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
1320 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
1321 :
1322 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
1323 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1324 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
1325 : m_libspdm_use_asym_algo, &data1,
1326 : &data_size1, NULL, NULL)) {
1327 0 : return;
1328 : }
1329 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
1330 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
1331 :
1332 1 : libspdm_reset_message_a(spdm_context);
1333 :
1334 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request1.random_data);
1335 1 : m_libspdm_key_exchange_request1.req_session_id = 0xFFFF;
1336 1 : m_libspdm_key_exchange_request1.reserved = 0;
1337 1 : ptr = m_libspdm_key_exchange_request1.exchange_data;
1338 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
1339 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
1340 : false);
1341 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
1342 1 : ptr += dhe_key_size;
1343 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
1344 1 : opaque_key_exchange_req_size =
1345 1 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
1346 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
1347 1 : ptr += sizeof(uint16_t);
1348 1 : libspdm_build_opaque_data_supported_version_data(
1349 : spdm_context, &opaque_key_exchange_req_size, ptr);
1350 1 : ptr += opaque_key_exchange_req_size;
1351 :
1352 1 : current_request_size = sizeof(spdm_key_exchange_request_t) + dhe_key_size +
1353 1 : sizeof(uint16_t) + opaque_key_exchange_req_size;
1354 1 : response_size = sizeof(response);
1355 1 : status = libspdm_get_response_key_exchange(
1356 : spdm_context, current_request_size, &m_libspdm_key_exchange_request1,
1357 : &response_size, response);
1358 :
1359 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
1360 1 : assert_int_equal(
1361 : libspdm_secured_message_get_session_state(
1362 : spdm_context->session_info[0].secured_message_context),
1363 : LIBSPDM_SESSION_STATE_HANDSHAKING);
1364 1 : spdm_response = (void *)response;
1365 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_KEY_EXCHANGE_RSP);
1366 1 : assert_int_equal(spdm_response->rsp_session_id, 0xFFFF);
1367 :
1368 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
1369 : assert_int_equal(spdm_context->session_info[0].session_transcript.message_k.buffer_size,
1370 : current_request_size + response_size);
1371 : assert_memory_equal(spdm_context->session_info[0].session_transcript.message_k.buffer,
1372 : &m_libspdm_key_exchange_request1, current_request_size);
1373 : assert_memory_equal(spdm_context->session_info[0].session_transcript.message_k.buffer +
1374 : current_request_size,
1375 : response, response_size);
1376 : #endif
1377 1 : free(data1);
1378 : }
1379 :
1380 1 : static void rsp_key_exchange_rsp_case17(void **state)
1381 : {
1382 : libspdm_return_t status;
1383 : libspdm_test_context_t *spdm_test_context;
1384 : libspdm_context_t *spdm_context;
1385 : size_t response_size;
1386 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
1387 : spdm_key_exchange_response_t *spdm_response;
1388 : void *data1;
1389 : size_t data_size1;
1390 : uint8_t *ptr;
1391 : size_t dhe_key_size;
1392 : void *dhe_context;
1393 : size_t opaque_key_exchange_req_size;
1394 :
1395 1 : spdm_test_context = *state;
1396 1 : spdm_context = spdm_test_context->spdm_context;
1397 1 : spdm_test_context->case_id = 0x11;
1398 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1399 1 : spdm_context->connection_info.capability.flags |=
1400 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
1401 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
1402 1 : spdm_context->local_context.capability.flags |=
1403 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
1404 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
1405 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1406 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
1407 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
1408 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
1409 : m_libspdm_use_measurement_hash_algo;
1410 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
1411 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
1412 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_12 <<
1413 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1414 1 : spdm_context->connection_info.algorithm.other_params_support =
1415 : SPDM_ALGORITHMS_OPAQUE_DATA_FORMAT_1;
1416 1 : spdm_context->local_context.secured_message_version.secured_message_version_count = 1;
1417 :
1418 1 : libspdm_session_info_init(spdm_context,
1419 1 : spdm_context->session_info,
1420 : 0,
1421 : INVALID_SESSION_ID, false);
1422 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
1423 : m_libspdm_use_asym_algo, &data1,
1424 : &data_size1, NULL, NULL)) {
1425 0 : return;
1426 : }
1427 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
1428 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
1429 :
1430 1 : libspdm_reset_message_a(spdm_context);
1431 :
1432 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request8.random_data);
1433 1 : m_libspdm_key_exchange_request8.req_session_id = 0xFFFF;
1434 1 : m_libspdm_key_exchange_request8.reserved = 0;
1435 1 : m_libspdm_key_exchange_request8.session_policy = 0xFF;
1436 1 : ptr = m_libspdm_key_exchange_request8.exchange_data;
1437 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
1438 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
1439 : false);
1440 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
1441 1 : ptr += dhe_key_size;
1442 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
1443 1 : opaque_key_exchange_req_size =
1444 1 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
1445 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
1446 1 : ptr += sizeof(uint16_t);
1447 1 : libspdm_build_opaque_data_supported_version_data(
1448 : spdm_context, &opaque_key_exchange_req_size, ptr);
1449 1 : ptr += opaque_key_exchange_req_size;
1450 1 : response_size = sizeof(response);
1451 1 : status = libspdm_get_response_key_exchange(
1452 : spdm_context, m_libspdm_key_exchange_request8_size,
1453 : &m_libspdm_key_exchange_request8, &response_size, response);
1454 1 : assert_int_equal(spdm_context->session_info[0].session_policy,
1455 : m_libspdm_key_exchange_request8.session_policy);
1456 1 : spdm_response = (void *)response;
1457 1 : assert_int_equal(spdm_response->header.spdm_version, SPDM_MESSAGE_VERSION_12);
1458 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
1459 1 : assert_int_equal(
1460 : libspdm_secured_message_get_session_state(
1461 : spdm_context->session_info[0].secured_message_context),
1462 : LIBSPDM_SESSION_STATE_HANDSHAKING);
1463 1 : spdm_response = (void *)response;
1464 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_KEY_EXCHANGE_RSP);
1465 1 : assert_int_equal(spdm_response->rsp_session_id, 0xFFFF);
1466 1 : free(data1);
1467 : }
1468 :
1469 : /**
1470 : * Test 18: SlotID in KEY_EXCHANGE request message is 9, but it should be 0xFF or between 0 and 7 inclusive.
1471 : * Expected Behavior: generate an ERROR_RESPONSE with code SPDM_ERROR_CODE_INVALID_REQUEST.
1472 : **/
1473 1 : static void rsp_key_exchange_rsp_case18(void **state)
1474 : {
1475 : libspdm_return_t status;
1476 : libspdm_test_context_t *spdm_test_context;
1477 : libspdm_context_t *spdm_context;
1478 : size_t response_size;
1479 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
1480 : spdm_key_exchange_response_t *spdm_response;
1481 : void *data1;
1482 : size_t data_size1;
1483 : uint8_t *ptr;
1484 : size_t dhe_key_size;
1485 : void *dhe_context;
1486 : size_t opaque_key_exchange_req_size;
1487 :
1488 1 : spdm_test_context = *state;
1489 1 : spdm_context = spdm_test_context->spdm_context;
1490 1 : spdm_test_context->case_id = 0x12;
1491 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1492 1 : spdm_context->connection_info.capability.flags |=
1493 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
1494 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
1495 1 : spdm_context->local_context.capability.flags |=
1496 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
1497 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
1498 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1499 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
1500 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
1501 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
1502 : m_libspdm_use_measurement_hash_algo;
1503 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
1504 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
1505 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
1506 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1507 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
1508 : m_libspdm_use_asym_algo, &data1,
1509 : &data_size1, NULL, NULL)) {
1510 0 : return;
1511 : }
1512 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
1513 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
1514 :
1515 1 : libspdm_reset_message_a(spdm_context);
1516 :
1517 1 : spdm_context->local_context.secured_message_version.secured_message_version_count = 1;
1518 :
1519 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request9.random_data);
1520 1 : m_libspdm_key_exchange_request9.req_session_id = 0xFFFF;
1521 1 : m_libspdm_key_exchange_request9.reserved = 0;
1522 1 : ptr = m_libspdm_key_exchange_request9.exchange_data;
1523 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
1524 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
1525 : false);
1526 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
1527 1 : ptr += dhe_key_size;
1528 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
1529 1 : opaque_key_exchange_req_size =
1530 1 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
1531 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
1532 1 : ptr += sizeof(uint16_t);
1533 1 : libspdm_build_opaque_data_supported_version_data(
1534 : spdm_context, &opaque_key_exchange_req_size, ptr);
1535 1 : ptr += opaque_key_exchange_req_size;
1536 1 : response_size = sizeof(response);
1537 1 : status = libspdm_get_response_key_exchange(
1538 : spdm_context, m_libspdm_key_exchange_request9_size,
1539 : &m_libspdm_key_exchange_request9, &response_size, response);
1540 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
1541 1 : assert_int_equal(response_size, sizeof(spdm_error_response_t));
1542 1 : spdm_response = (void *)response;
1543 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
1544 1 : assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_INVALID_REQUEST);
1545 1 : assert_int_equal(spdm_response->header.param2, 0);
1546 1 : free(data1);
1547 : }
1548 :
1549 1 : static void rsp_key_exchange_rsp_case19(void **state)
1550 : {
1551 : libspdm_return_t status;
1552 : libspdm_test_context_t *spdm_test_context;
1553 : libspdm_context_t *spdm_context;
1554 : size_t response_size;
1555 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
1556 : spdm_key_exchange_response_t *spdm_response;
1557 : void *data1;
1558 : size_t data_size1;
1559 : uint8_t *ptr;
1560 : size_t dhe_key_size;
1561 : void *dhe_context;
1562 :
1563 1 : spdm_test_context = *state;
1564 1 : spdm_context = spdm_test_context->spdm_context;
1565 1 : spdm_test_context->case_id = 0x13;
1566 1 : spdm_context->response_state = 0;
1567 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1568 1 : spdm_context->connection_info.capability.flags = 0;
1569 1 : spdm_context->connection_info.capability.flags |=
1570 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
1571 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
1572 1 : spdm_context->local_context.capability.flags = 0;
1573 1 : spdm_context->local_context.capability.flags |=
1574 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
1575 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
1576 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1577 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
1578 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
1579 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
1580 : m_libspdm_use_measurement_hash_algo;
1581 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
1582 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
1583 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_12 <<
1584 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1585 1 : spdm_context->connection_info.algorithm.other_params_support =
1586 : SPDM_ALGORITHMS_OPAQUE_DATA_FORMAT_1;
1587 :
1588 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
1589 : m_libspdm_use_asym_algo, &data1,
1590 : &data_size1, NULL, NULL)) {
1591 0 : return;
1592 : }
1593 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
1594 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
1595 :
1596 1 : libspdm_reset_message_a(spdm_context);
1597 :
1598 1 : spdm_context->local_context.secured_message_version.secured_message_version_count = 1;
1599 :
1600 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request8.random_data);
1601 1 : m_libspdm_key_exchange_request8.req_session_id = 0xFFFF;
1602 1 : m_libspdm_key_exchange_request8.reserved = 0;
1603 1 : ptr = m_libspdm_key_exchange_request8.exchange_data;
1604 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
1605 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
1606 : false);
1607 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
1608 1 : ptr += dhe_key_size;
1609 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
1610 :
1611 : size_t opaque_data_size;
1612 : spdm_general_opaque_data_table_header_t
1613 : *spdm_general_opaque_data_table_header;
1614 : secured_message_opaque_element_table_header_t
1615 : *opaque_element_table_header;
1616 : secured_message_opaque_element_header_t
1617 : * secured_message_element_header;
1618 : uint8_t element_num;
1619 : uint8_t element_index;
1620 : size_t current_element_len;
1621 :
1622 1 : spdm_general_opaque_data_table_header =
1623 : (spdm_general_opaque_data_table_header_t *)(ptr + sizeof(uint16_t));
1624 1 : spdm_general_opaque_data_table_header->total_elements = 2;
1625 1 : opaque_element_table_header = (void *)(spdm_general_opaque_data_table_header + 1);
1626 :
1627 1 : element_num = spdm_general_opaque_data_table_header->total_elements;
1628 1 : opaque_data_size = sizeof(spdm_general_opaque_data_table_header_t);
1629 :
1630 3 : for (element_index = 0; element_index < element_num; element_index++) {
1631 2 : opaque_element_table_header->id = SPDM_REGISTRY_ID_DMTF;
1632 2 : opaque_element_table_header->vendor_len = 0;
1633 : /* When opaque_element_data_len is not four byte aligned*/
1634 2 : opaque_element_table_header->opaque_element_data_len = 0xF;
1635 :
1636 2 : secured_message_element_header = (void *)(opaque_element_table_header + 1);
1637 2 : secured_message_element_header->sm_data_id =
1638 : SECURED_MESSAGE_OPAQUE_ELEMENT_SMDATA_ID_SUPPORTED_VERSION;
1639 2 : secured_message_element_header->sm_data_version =
1640 : SECURED_MESSAGE_OPAQUE_ELEMENT_SMDATA_DATA_VERSION;
1641 :
1642 2 : current_element_len = sizeof(secured_message_opaque_element_table_header_t) +
1643 2 : opaque_element_table_header->vendor_len +
1644 2 : sizeof(opaque_element_table_header->opaque_element_data_len) +
1645 2 : opaque_element_table_header->opaque_element_data_len;
1646 :
1647 : /*move to next element*/
1648 2 : opaque_element_table_header =
1649 : (secured_message_opaque_element_table_header_t *)
1650 : ((uint8_t *)opaque_element_table_header + current_element_len);
1651 :
1652 2 : opaque_data_size += current_element_len;
1653 : }
1654 :
1655 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_data_size);
1656 :
1657 1 : response_size = sizeof(response);
1658 1 : status = libspdm_get_response_key_exchange(
1659 : spdm_context, m_libspdm_key_exchange_request8_size,
1660 : &m_libspdm_key_exchange_request8, &response_size, response);
1661 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
1662 1 : assert_int_equal(response_size, sizeof(spdm_error_response_t));
1663 1 : spdm_response = (void *)response;
1664 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
1665 1 : assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_INVALID_REQUEST);
1666 1 : assert_int_equal(spdm_response->header.param2, 0);
1667 1 : free(data1);
1668 : }
1669 :
1670 1 : static void rsp_key_exchange_rsp_case20(void **state)
1671 : {
1672 : libspdm_return_t status;
1673 : libspdm_test_context_t *spdm_test_context;
1674 : libspdm_context_t *spdm_context;
1675 : size_t response_size;
1676 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
1677 : spdm_key_exchange_response_t *spdm_response;
1678 : void *data1;
1679 : size_t data_size1;
1680 : uint8_t *ptr;
1681 : size_t dhe_key_size;
1682 : void *dhe_context;
1683 : size_t opaque_key_exchange_req_size;
1684 :
1685 1 : spdm_test_context = *state;
1686 1 : spdm_context = spdm_test_context->spdm_context;
1687 1 : spdm_test_context->case_id = 0x14;
1688 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1689 1 : spdm_context->connection_info.capability.flags =
1690 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
1691 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
1692 1 : spdm_context->local_context.capability.flags =
1693 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
1694 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP |
1695 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
1696 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1697 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
1698 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
1699 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
1700 : m_libspdm_use_measurement_hash_algo;
1701 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
1702 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
1703 1 : spdm_context->connection_info.algorithm.other_params_support =
1704 : SPDM_ALGORITHMS_OPAQUE_DATA_FORMAT_1;
1705 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_12 <<
1706 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1707 :
1708 1 : spdm_context->local_context.secured_message_version.secured_message_version_count = 1;
1709 :
1710 1 : libspdm_session_info_init(spdm_context,
1711 1 : spdm_context->session_info,
1712 : 0,
1713 : INVALID_SESSION_ID, false);
1714 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
1715 : m_libspdm_use_asym_algo, &data1,
1716 : &data_size1, NULL, NULL)) {
1717 0 : return;
1718 : }
1719 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
1720 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
1721 :
1722 1 : libspdm_reset_message_a(spdm_context);
1723 :
1724 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request8.random_data);
1725 1 : m_libspdm_key_exchange_request8.req_session_id = 0xFFFF;
1726 1 : m_libspdm_key_exchange_request8.reserved = 0;
1727 1 : m_libspdm_key_exchange_request8.session_policy = 0xFF;
1728 1 : ptr = m_libspdm_key_exchange_request8.exchange_data;
1729 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
1730 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
1731 : false);
1732 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
1733 1 : ptr += dhe_key_size;
1734 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
1735 1 : opaque_key_exchange_req_size =
1736 1 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
1737 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
1738 1 : ptr += sizeof(uint16_t);
1739 1 : libspdm_build_opaque_data_supported_version_data(
1740 : spdm_context, &opaque_key_exchange_req_size, ptr);
1741 1 : ptr += opaque_key_exchange_req_size;
1742 1 : response_size = sizeof(response);
1743 1 : status = libspdm_get_response_key_exchange(
1744 : spdm_context, m_libspdm_key_exchange_request8_size,
1745 : &m_libspdm_key_exchange_request8, &response_size, response);
1746 1 : assert_int_equal(spdm_context->session_info[0].session_policy,
1747 : m_libspdm_key_exchange_request8.session_policy);
1748 1 : spdm_response = (void *)response;
1749 1 : assert_int_equal(spdm_response->header.spdm_version, SPDM_MESSAGE_VERSION_12);
1750 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
1751 1 : assert_int_equal(
1752 : libspdm_secured_message_get_session_state(
1753 : spdm_context->session_info[0].secured_message_context),
1754 : LIBSPDM_SESSION_STATE_HANDSHAKING);
1755 1 : spdm_response = (void *)response;
1756 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_KEY_EXCHANGE_RSP);
1757 1 : assert_int_equal(spdm_response->rsp_session_id, 0xFFFF);
1758 1 : free(data1);
1759 : }
1760 :
1761 : /**
1762 : * Test 21: The key usage bit mask is not set, the SlotID fields in KEY_EXCHANGE and KEY_EXCHANGE_RSP shall not specify this certificate slot
1763 : * Expected Behavior: get a SPDM_ERROR_CODE_INVALID_REQUEST return code
1764 : **/
1765 1 : static void rsp_key_exchange_rsp_case21(void **state)
1766 : {
1767 : libspdm_return_t status;
1768 : libspdm_test_context_t *spdm_test_context;
1769 : libspdm_context_t *spdm_context;
1770 : size_t response_size;
1771 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
1772 : spdm_key_exchange_response_t *spdm_response;
1773 : void *data1;
1774 : size_t data_size1;
1775 : uint8_t *ptr;
1776 : size_t dhe_key_size;
1777 : void *dhe_context;
1778 : size_t opaque_key_exchange_req_size;
1779 : uint8_t slot_id;
1780 :
1781 1 : spdm_test_context = *state;
1782 1 : spdm_context = spdm_test_context->spdm_context;
1783 1 : spdm_test_context->case_id = 0x15;
1784 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1785 1 : spdm_context->connection_info.capability.flags |=
1786 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
1787 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
1788 1 : spdm_context->local_context.capability.flags |=
1789 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
1790 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
1791 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1792 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
1793 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
1794 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
1795 : m_libspdm_use_measurement_hash_algo;
1796 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
1797 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
1798 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
1799 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1800 1 : spdm_context->connection_info.algorithm.other_params_support =
1801 : SPDM_ALGORITHMS_OPAQUE_DATA_FORMAT_1;
1802 1 : spdm_context->local_context.secured_message_version.secured_message_version_count = 1;
1803 1 : spdm_context->connection_info.multi_key_conn_rsp = true;
1804 :
1805 1 : libspdm_session_info_init(spdm_context,
1806 1 : spdm_context->session_info,
1807 : 0,
1808 : INVALID_SESSION_ID, false);
1809 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
1810 : m_libspdm_use_asym_algo, &data1,
1811 : &data_size1, NULL, NULL)) {
1812 0 : return;
1813 : }
1814 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
1815 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
1816 :
1817 1 : libspdm_reset_message_a(spdm_context);
1818 :
1819 : /* If set, the SlotID fields in KEY_EXCHANGE and KEY_EXCHANGE_RSP can specify this certificate slot. If not set,
1820 : * the SlotID fields in KEY_EXCHANGE and KEY_EXCHANGE_RSP shall not specify this certificate slot */
1821 1 : slot_id = 0;
1822 1 : m_libspdm_key_exchange_request10.header.param2 = slot_id;
1823 1 : spdm_context->local_context.local_key_usage_bit_mask[slot_id] =
1824 : SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE |
1825 : SPDM_KEY_USAGE_BIT_MASK_MEASUREMENT_USE;
1826 :
1827 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request10.random_data);
1828 1 : m_libspdm_key_exchange_request10.req_session_id = 0xFFFF;
1829 1 : m_libspdm_key_exchange_request10.reserved = 0;
1830 1 : m_libspdm_key_exchange_request10.session_policy = 0xFF;
1831 1 : ptr = m_libspdm_key_exchange_request10.exchange_data;
1832 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
1833 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
1834 : false);
1835 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
1836 1 : ptr += dhe_key_size;
1837 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
1838 1 : opaque_key_exchange_req_size =
1839 1 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
1840 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
1841 1 : ptr += sizeof(uint16_t);
1842 1 : libspdm_build_opaque_data_supported_version_data(
1843 : spdm_context, &opaque_key_exchange_req_size, ptr);
1844 1 : ptr += opaque_key_exchange_req_size;
1845 1 : response_size = sizeof(response);
1846 1 : status = libspdm_get_response_key_exchange(
1847 : spdm_context, m_libspdm_key_exchange_request10_size,
1848 : &m_libspdm_key_exchange_request10, &response_size, response);
1849 :
1850 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
1851 1 : assert_int_equal (response_size, sizeof(spdm_error_response_t));
1852 1 : spdm_response = (void *)response;
1853 1 : assert_int_equal (spdm_response->header.request_response_code, SPDM_ERROR);
1854 1 : assert_int_equal (spdm_response->header.param1, SPDM_ERROR_CODE_INVALID_REQUEST);
1855 1 : assert_int_equal (spdm_response->header.param2, 0);
1856 :
1857 1 : free(data1);
1858 : }
1859 :
1860 : /**
1861 : * Test 21: The Requester subscribes to all events supported by the Responder.
1862 : * Expected Behavior: Responder successfully subscribes the Requester to all events.
1863 : **/
1864 1 : static void rsp_key_exchange_rsp_case22(void **state)
1865 : {
1866 : #if LIBSPDM_ENABLE_CAPABILITY_EVENT_CAP
1867 : libspdm_return_t status;
1868 : libspdm_test_context_t *spdm_test_context;
1869 : libspdm_context_t *spdm_context;
1870 : size_t response_size;
1871 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
1872 : spdm_key_exchange_response_t *spdm_response;
1873 : void *data1;
1874 : size_t data_size1;
1875 : uint8_t *ptr;
1876 : size_t dhe_key_size;
1877 : void *dhe_context;
1878 : size_t opaque_key_exchange_req_size;
1879 :
1880 1 : spdm_test_context = *state;
1881 1 : spdm_context = spdm_test_context->spdm_context;
1882 1 : spdm_test_context->case_id = 0x16;
1883 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1884 1 : spdm_context->connection_info.capability.flags |=
1885 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
1886 1 : spdm_context->local_context.capability.flags |=
1887 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
1888 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_EVENT_CAP;
1889 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1890 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
1891 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
1892 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
1893 : m_libspdm_use_measurement_hash_algo;
1894 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
1895 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
1896 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
1897 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1898 1 : spdm_context->connection_info.algorithm.other_params_support =
1899 : SPDM_ALGORITHMS_OPAQUE_DATA_FORMAT_1;
1900 1 : spdm_context->local_context.secured_message_version.secured_message_version_count = 1;
1901 1 : spdm_context->connection_info.multi_key_conn_rsp = false;
1902 :
1903 1 : libspdm_session_info_init(spdm_context, spdm_context->session_info, INVALID_SESSION_ID,
1904 : 0, false);
1905 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
1906 : m_libspdm_use_asym_algo, &data1,
1907 : &data_size1, NULL, NULL)) {
1908 0 : return;
1909 : }
1910 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
1911 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
1912 :
1913 1 : libspdm_reset_message_a(spdm_context);
1914 :
1915 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request10.random_data);
1916 1 : m_libspdm_key_exchange_request10.req_session_id = 0xFFFF;
1917 1 : m_libspdm_key_exchange_request10.reserved = 0;
1918 1 : m_libspdm_key_exchange_request10.session_policy =
1919 : SPDM_KEY_EXCHANGE_REQUEST_SESSION_POLICY_EVENT_ALL_POLICY;
1920 1 : ptr = m_libspdm_key_exchange_request10.exchange_data;
1921 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
1922 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
1923 : false);
1924 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
1925 1 : ptr += dhe_key_size;
1926 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
1927 1 : opaque_key_exchange_req_size =
1928 1 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
1929 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
1930 1 : ptr += sizeof(uint16_t);
1931 1 : libspdm_build_opaque_data_supported_version_data(
1932 : spdm_context, &opaque_key_exchange_req_size, ptr);
1933 1 : ptr += opaque_key_exchange_req_size;
1934 1 : response_size = sizeof(response);
1935 :
1936 1 : status = libspdm_get_response_key_exchange(
1937 : spdm_context, m_libspdm_key_exchange_request10_size,
1938 : &m_libspdm_key_exchange_request10, &response_size, response);
1939 :
1940 1 : assert_int_equal(spdm_context->session_info[0].session_policy,
1941 : m_libspdm_key_exchange_request10.session_policy);
1942 1 : spdm_response = (void *)response;
1943 1 : assert_int_equal(spdm_response->header.spdm_version, SPDM_MESSAGE_VERSION_13);
1944 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
1945 1 : assert_int_equal(
1946 : libspdm_secured_message_get_session_state(
1947 : spdm_context->session_info[0].secured_message_context),
1948 : LIBSPDM_SESSION_STATE_HANDSHAKING);
1949 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_KEY_EXCHANGE_RSP);
1950 1 : assert_int_equal(spdm_response->rsp_session_id, 0xFFFF);
1951 1 : assert_true(g_event_all_subscribe && !g_event_all_unsubscribe);
1952 1 : free(data1);
1953 : #endif /* LIBSPDM_ENABLE_CAPABILITY_EVENT_CAP */
1954 : }
1955 :
1956 : #if LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP
1957 1 : static void rsp_key_exchange_rsp_case23(void **state)
1958 : {
1959 : libspdm_return_t status;
1960 : libspdm_test_context_t *spdm_test_context;
1961 : libspdm_context_t *spdm_context;
1962 : size_t response_size;
1963 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
1964 : spdm_key_exchange_response_t *spdm_response;
1965 : void *data1;
1966 : size_t data_size1;
1967 : uint8_t *ptr;
1968 : size_t dhe_key_size;
1969 : void *dhe_context;
1970 : size_t opaque_key_exchange_req_size;
1971 :
1972 1 : spdm_test_context = *state;
1973 1 : spdm_context = spdm_test_context->spdm_context;
1974 1 : spdm_test_context->case_id = 0x17;
1975 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
1976 1 : spdm_context->connection_info.capability.flags |=
1977 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
1978 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
1979 1 : spdm_context->local_context.capability.flags |=
1980 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
1981 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP |
1982 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP |
1983 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
1984 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1985 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
1986 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
1987 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
1988 : m_libspdm_use_measurement_hash_algo;
1989 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
1990 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
1991 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
1992 : SPDM_VERSION_NUMBER_SHIFT_BIT;
1993 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
1994 : m_libspdm_use_asym_algo, &data1,
1995 : &data_size1, NULL, NULL)) {
1996 0 : return;
1997 : }
1998 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
1999 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
2000 :
2001 1 : libspdm_reset_message_a(spdm_context);
2002 1 : g_key_exchange_start_mut_auth = SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED;
2003 1 : g_mandatory_mut_auth = true;
2004 :
2005 1 : spdm_context->local_context.secured_message_version.secured_message_version_count = 1;
2006 :
2007 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request1.random_data);
2008 1 : m_libspdm_key_exchange_request1.req_session_id = 0xFFFF;
2009 1 : m_libspdm_key_exchange_request1.reserved = 0;
2010 1 : ptr = m_libspdm_key_exchange_request1.exchange_data;
2011 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
2012 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
2013 : false);
2014 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
2015 1 : ptr += dhe_key_size;
2016 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
2017 1 : opaque_key_exchange_req_size =
2018 1 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
2019 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
2020 1 : ptr += sizeof(uint16_t);
2021 1 : libspdm_build_opaque_data_supported_version_data(
2022 : spdm_context, &opaque_key_exchange_req_size, ptr);
2023 1 : ptr += opaque_key_exchange_req_size;
2024 1 : response_size = sizeof(response);
2025 1 : status = libspdm_get_response_key_exchange(
2026 : spdm_context, m_libspdm_key_exchange_request1_size,
2027 : &m_libspdm_key_exchange_request1, &response_size, response);
2028 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
2029 1 : assert_int_equal(response_size, sizeof(spdm_error_response_t));
2030 1 : spdm_response = (void *)response;
2031 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
2032 1 : assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_UNSPECIFIED);
2033 1 : assert_int_equal(spdm_response->header.param2, 0);
2034 :
2035 1 : free(data1);
2036 : }
2037 :
2038 :
2039 1 : static void rsp_key_exchange_rsp_case24(void **state)
2040 : {
2041 : libspdm_return_t status;
2042 : libspdm_test_context_t *spdm_test_context;
2043 : libspdm_context_t *spdm_context;
2044 : size_t response_size;
2045 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
2046 : spdm_key_exchange_response_t *spdm_response;
2047 : void *data1;
2048 : size_t data_size1;
2049 : uint8_t *ptr;
2050 : size_t dhe_key_size;
2051 : void *dhe_context;
2052 : size_t opaque_key_exchange_req_size;
2053 :
2054 1 : spdm_test_context = *state;
2055 1 : spdm_context = spdm_test_context->spdm_context;
2056 1 : spdm_test_context->case_id = 0x18;
2057 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
2058 1 : spdm_context->connection_info.capability.flags |=
2059 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
2060 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
2061 1 : spdm_context->local_context.capability.flags |=
2062 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
2063 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP |
2064 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP |
2065 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
2066 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
2067 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
2068 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
2069 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
2070 : m_libspdm_use_measurement_hash_algo;
2071 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
2072 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
2073 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
2074 : SPDM_VERSION_NUMBER_SHIFT_BIT;
2075 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
2076 : m_libspdm_use_asym_algo, &data1,
2077 : &data_size1, NULL, NULL)) {
2078 0 : return;
2079 : }
2080 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
2081 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
2082 :
2083 1 : libspdm_reset_message_a(spdm_context);
2084 1 : g_key_exchange_start_mut_auth = SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED;
2085 1 : g_mandatory_mut_auth = true;
2086 :
2087 1 : spdm_context->local_context.secured_message_version.secured_message_version_count = 1;
2088 :
2089 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request10.random_data);
2090 1 : m_libspdm_key_exchange_request10.req_session_id = 0xFFFF;
2091 1 : m_libspdm_key_exchange_request10.reserved = 0;
2092 1 : ptr = m_libspdm_key_exchange_request10.exchange_data;
2093 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
2094 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
2095 : false);
2096 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
2097 1 : ptr += dhe_key_size;
2098 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
2099 1 : opaque_key_exchange_req_size =
2100 1 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
2101 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
2102 1 : ptr += sizeof(uint16_t);
2103 1 : libspdm_build_opaque_data_supported_version_data(
2104 : spdm_context, &opaque_key_exchange_req_size, ptr);
2105 1 : ptr += opaque_key_exchange_req_size;
2106 1 : response_size = sizeof(response);
2107 1 : status = libspdm_get_response_key_exchange(
2108 : spdm_context, m_libspdm_key_exchange_request10_size,
2109 : &m_libspdm_key_exchange_request10, &response_size, response);
2110 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
2111 1 : assert_int_equal(response_size, sizeof(spdm_error_response_t));
2112 1 : spdm_response = (void *)response;
2113 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
2114 1 : assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_INVALID_POLICY);
2115 1 : assert_int_equal(spdm_response->header.param2, 0);
2116 :
2117 1 : free(data1);
2118 : }
2119 : #endif /* LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP */
2120 :
2121 : /**
2122 : * Test 25: Successful response to a valid KEY_EXCHANGE request.
2123 : * Expected Behavior: get a valid KEY_EXCHANGE_RSP message
2124 : * with integrator defined opaque data in the response
2125 : **/
2126 1 : static void rsp_key_exchange_rsp_case25(void **state)
2127 : {
2128 : libspdm_return_t status;
2129 : libspdm_test_context_t *spdm_test_context;
2130 : libspdm_context_t *spdm_context;
2131 : size_t response_size;
2132 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
2133 : spdm_key_exchange_response_t *spdm_response;
2134 : void *data1;
2135 : size_t data_size1;
2136 : uint8_t *ptr;
2137 : size_t dhe_key_size;
2138 : void *dhe_context;
2139 : size_t opaque_key_exchange_req_size;
2140 : uint16_t opaque_length;
2141 :
2142 1 : spdm_test_context = *state;
2143 1 : spdm_context = spdm_test_context->spdm_context;
2144 1 : spdm_test_context->case_id = 0x19;
2145 1 : spdm_context->connection_info.connection_state =
2146 : LIBSPDM_CONNECTION_STATE_NEGOTIATED;
2147 1 : spdm_context->connection_info.capability.flags |=
2148 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
2149 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
2150 1 : spdm_context->local_context.capability.flags |=
2151 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
2152 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
2153 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
2154 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
2155 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
2156 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
2157 : m_libspdm_use_measurement_hash_algo;
2158 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
2159 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
2160 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_12 <<
2161 : SPDM_VERSION_NUMBER_SHIFT_BIT;
2162 1 : spdm_context->connection_info.algorithm.other_params_support =
2163 : SPDM_ALGORITHMS_OPAQUE_DATA_FORMAT_1;
2164 1 : spdm_context->local_context.secured_message_version.secured_message_version_count = 2;
2165 1 : g_generate_key_exchange_opaque_data = true;
2166 :
2167 1 : libspdm_session_info_init(spdm_context,
2168 1 : spdm_context->session_info,
2169 : 0,
2170 : INVALID_SESSION_ID, false);
2171 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
2172 : m_libspdm_use_asym_algo, &data1,
2173 : &data_size1, NULL, NULL)) {
2174 0 : return;
2175 : }
2176 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
2177 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
2178 :
2179 1 : libspdm_reset_message_a(spdm_context);
2180 :
2181 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request8.random_data);
2182 1 : m_libspdm_key_exchange_request8.req_session_id = 0xFFFF;
2183 1 : m_libspdm_key_exchange_request8.reserved = 0;
2184 1 : m_libspdm_key_exchange_request8.session_policy = 0xFF;
2185 1 : ptr = m_libspdm_key_exchange_request8.exchange_data;
2186 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
2187 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
2188 : false);
2189 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
2190 1 : ptr += dhe_key_size;
2191 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
2192 1 : opaque_key_exchange_req_size =
2193 1 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
2194 1 : *(uint16_t *)ptr = (uint16_t)opaque_key_exchange_req_size;
2195 1 : ptr += sizeof(uint16_t);
2196 1 : libspdm_build_opaque_data_supported_version_data(
2197 : spdm_context, &opaque_key_exchange_req_size, ptr);
2198 1 : ptr += opaque_key_exchange_req_size;
2199 1 : response_size = sizeof(response);
2200 1 : status = libspdm_get_response_key_exchange(
2201 : spdm_context, m_libspdm_key_exchange_request8_size,
2202 : &m_libspdm_key_exchange_request8, &response_size, response);
2203 1 : assert_int_equal(spdm_context->session_info[0].session_policy,
2204 : m_libspdm_key_exchange_request8.session_policy);
2205 1 : spdm_response = (void *)response;
2206 1 : assert_int_equal(spdm_response->header.spdm_version, SPDM_MESSAGE_VERSION_12);
2207 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
2208 1 : assert_int_equal(
2209 : libspdm_secured_message_get_session_state(
2210 : spdm_context->session_info[0].secured_message_context),
2211 : LIBSPDM_SESSION_STATE_HANDSHAKING);
2212 1 : spdm_response = (void *)response;
2213 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_KEY_EXCHANGE_RSP);
2214 1 : assert_int_equal(spdm_response->rsp_session_id, 0xFFFF);
2215 1 : ptr = (uint8_t *)(spdm_response + 1);
2216 1 : ptr += dhe_key_size;
2217 1 : opaque_length = *(uint16_t *)ptr;
2218 1 : assert_int_equal(opaque_length,
2219 : libspdm_get_opaque_data_version_selection_data_size(spdm_context));
2220 :
2221 1 : free(data1);
2222 : }
2223 :
2224 : #if LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP
2225 : /**
2226 : * Test 26: the Responder requests session-based mutual authentication with MUT_AUTH_REQUESTED
2227 : * (bit 0) and a non-zero ReqSlotID. This is legal when the Responder already possesses the
2228 : * Requester's certificate chain, so no encapsulated flow is needed to retrieve it.
2229 : * Expected behavior: KEY_EXCHANGE_RSP conveys the slot to the Requester, and the slot is recorded
2230 : * in the session so that FINISH is verified against the correct certificate chain.
2231 : **/
2232 1 : static void rsp_key_exchange_rsp_case26(void **state)
2233 : {
2234 : libspdm_return_t status;
2235 : libspdm_test_context_t *spdm_test_context;
2236 : libspdm_context_t *spdm_context;
2237 : libspdm_session_info_t *session_info;
2238 : size_t response_size;
2239 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
2240 : spdm_key_exchange_response_t *spdm_response;
2241 : void *data1;
2242 : size_t data_size1;
2243 : uint8_t *ptr;
2244 : size_t dhe_key_size;
2245 : void *dhe_context;
2246 : size_t opaque_key_exchange_req_size;
2247 :
2248 1 : spdm_test_context = *state;
2249 1 : spdm_context = spdm_test_context->spdm_context;
2250 1 : spdm_test_context->case_id = 0x1A;
2251 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
2252 1 : spdm_context->connection_info.capability.flags |=
2253 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
2254 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP |
2255 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP |
2256 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CERT_CAP;
2257 1 : spdm_context->local_context.capability.flags |=
2258 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
2259 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP |
2260 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP |
2261 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
2262 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
2263 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
2264 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
2265 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
2266 : m_libspdm_use_measurement_hash_algo;
2267 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
2268 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
2269 1 : spdm_context->connection_info.algorithm.req_base_asym_alg = m_libspdm_use_req_asym_algo;
2270 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
2271 : SPDM_VERSION_NUMBER_SHIFT_BIT;
2272 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
2273 : m_libspdm_use_asym_algo, &data1,
2274 : &data_size1, NULL, NULL)) {
2275 0 : return;
2276 : }
2277 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
2278 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
2279 :
2280 1 : libspdm_reset_message_a(spdm_context);
2281 1 : g_key_exchange_start_mut_auth = SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED;
2282 1 : g_mandatory_mut_auth = false;
2283 : /* The Responder already has the Requester's chain in a slot other than 0. */
2284 1 : g_key_exchange_req_slot_id = 1;
2285 :
2286 1 : spdm_context->local_context.secured_message_version.secured_message_version_count = 1;
2287 :
2288 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request1.random_data);
2289 1 : m_libspdm_key_exchange_request1.req_session_id = 0xFFFF;
2290 1 : m_libspdm_key_exchange_request1.reserved = 0;
2291 1 : ptr = m_libspdm_key_exchange_request1.exchange_data;
2292 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
2293 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
2294 : false);
2295 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
2296 1 : ptr += dhe_key_size;
2297 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
2298 1 : opaque_key_exchange_req_size =
2299 1 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
2300 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
2301 1 : ptr += sizeof(uint16_t);
2302 1 : libspdm_build_opaque_data_supported_version_data(
2303 : spdm_context, &opaque_key_exchange_req_size, ptr);
2304 1 : ptr += opaque_key_exchange_req_size;
2305 1 : response_size = sizeof(response);
2306 1 : status = libspdm_get_response_key_exchange(
2307 : spdm_context, m_libspdm_key_exchange_request1_size,
2308 : &m_libspdm_key_exchange_request1, &response_size, response);
2309 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
2310 :
2311 1 : spdm_response = (void *)response;
2312 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_KEY_EXCHANGE_RSP);
2313 1 : assert_int_equal(spdm_response->mut_auth_requested,
2314 : SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED);
2315 : /* The slot is conveyed to the Requester ... */
2316 1 : assert_int_equal(spdm_response->req_slot_id_param, 1);
2317 :
2318 : /* ... and recorded in the session, so FINISH verifies against the right chain. */
2319 1 : session_info = libspdm_get_session_info_via_session_id(
2320 : spdm_context, spdm_context->latest_session_id);
2321 1 : assert_non_null(session_info);
2322 1 : assert_int_equal(session_info->peer_used_cert_chain_slot_id, 1);
2323 :
2324 1 : free(data1);
2325 : }
2326 :
2327 : /**
2328 : * Test 27: the Requester's PUB_KEY_ID_CAP is set and the Integrator requests session-based mutual
2329 : * authentication with MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST (bit 1). The Requester has no
2330 : * certificate slots, so there is nothing for the encapsulated flow to retrieve.
2331 : * Expected behavior: Responder returns ERROR(Unspecified).
2332 : **/
2333 1 : static void rsp_key_exchange_rsp_case27(void **state)
2334 : {
2335 : libspdm_return_t status;
2336 : libspdm_test_context_t *spdm_test_context;
2337 : libspdm_context_t *spdm_context;
2338 : size_t response_size;
2339 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
2340 : spdm_error_response_t *spdm_response;
2341 : void *data1;
2342 : size_t data_size1;
2343 : uint8_t *ptr;
2344 : size_t dhe_key_size;
2345 : void *dhe_context;
2346 : size_t opaque_key_exchange_req_size;
2347 :
2348 1 : spdm_test_context = *state;
2349 1 : spdm_context = spdm_test_context->spdm_context;
2350 1 : spdm_test_context->case_id = 0x1B;
2351 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
2352 1 : spdm_context->connection_info.capability.flags |=
2353 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
2354 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP |
2355 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP |
2356 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCAP_CAP |
2357 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PUB_KEY_ID_CAP;
2358 1 : spdm_context->local_context.capability.flags |=
2359 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
2360 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP |
2361 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP |
2362 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCAP_CAP |
2363 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
2364 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
2365 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
2366 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
2367 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
2368 : m_libspdm_use_measurement_hash_algo;
2369 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
2370 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
2371 1 : spdm_context->connection_info.algorithm.req_base_asym_alg = m_libspdm_use_req_asym_algo;
2372 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
2373 : SPDM_VERSION_NUMBER_SHIFT_BIT;
2374 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
2375 : m_libspdm_use_asym_algo, &data1,
2376 : &data_size1, NULL, NULL)) {
2377 0 : return;
2378 : }
2379 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
2380 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
2381 :
2382 1 : libspdm_reset_message_a(spdm_context);
2383 1 : g_key_exchange_start_mut_auth =
2384 : SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST;
2385 1 : g_mandatory_mut_auth = false;
2386 :
2387 1 : spdm_context->local_context.secured_message_version.secured_message_version_count = 1;
2388 :
2389 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, m_libspdm_key_exchange_request1.random_data);
2390 1 : m_libspdm_key_exchange_request1.req_session_id = 0xFFFF;
2391 1 : m_libspdm_key_exchange_request1.reserved = 0;
2392 1 : ptr = m_libspdm_key_exchange_request1.exchange_data;
2393 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
2394 1 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version, m_libspdm_use_dhe_algo,
2395 : false);
2396 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
2397 1 : ptr += dhe_key_size;
2398 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
2399 1 : opaque_key_exchange_req_size =
2400 1 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
2401 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
2402 1 : ptr += sizeof(uint16_t);
2403 1 : libspdm_build_opaque_data_supported_version_data(
2404 : spdm_context, &opaque_key_exchange_req_size, ptr);
2405 1 : ptr += opaque_key_exchange_req_size;
2406 1 : response_size = sizeof(response);
2407 1 : status = libspdm_get_response_key_exchange(
2408 : spdm_context, m_libspdm_key_exchange_request1_size,
2409 : &m_libspdm_key_exchange_request1, &response_size, response);
2410 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
2411 :
2412 1 : assert_int_equal(response_size, sizeof(spdm_error_response_t));
2413 1 : spdm_response = (void *)response;
2414 1 : assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
2415 1 : assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_UNSPECIFIED);
2416 1 : assert_int_equal(spdm_response->header.param2, 0);
2417 :
2418 1 : free(data1);
2419 : }
2420 :
2421 : /**
2422 : * Test 28: the Integrator returns a MutAuthRequested value that is not one of the three legal
2423 : * values. The specification allows at most one of Bit 0, Bit 1, or Bit 2 to be set.
2424 : * Expected behavior: Responder returns ERROR(Unspecified) for every illegal value.
2425 : **/
2426 1 : static void rsp_key_exchange_rsp_case28(void **state)
2427 : {
2428 : libspdm_return_t status;
2429 : libspdm_test_context_t *spdm_test_context;
2430 : libspdm_context_t *spdm_context;
2431 : size_t response_size;
2432 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
2433 : spdm_error_response_t *spdm_response;
2434 : void *data1;
2435 : size_t data_size1;
2436 : uint8_t *ptr;
2437 : size_t dhe_key_size;
2438 : void *dhe_context;
2439 : size_t opaque_key_exchange_req_size;
2440 : size_t index;
2441 : /* Multiple bits set, then the reserved-bit singletons. A pure "at most one bit" test would
2442 : * accept the latter, so they are what distinguishes the whitelist from a popcount check. */
2443 1 : const uint8_t illegal_values[] = {0x03, 0x05, 0x06, 0x07, 0x08, 0x10, 0x80, 0xFF};
2444 :
2445 1 : spdm_test_context = *state;
2446 1 : spdm_context = spdm_test_context->spdm_context;
2447 1 : spdm_test_context->case_id = 0x1C;
2448 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
2449 : /* Tests in this group share one context and no per-test setup runs, so assign the
2450 : * capability flags rather than OR into whatever a previous test left behind. In particular
2451 : * PUB_KEY_ID_CAP, which an earlier test sets, would reject every value except
2452 : * MUT_AUTH_REQUESTED and mask what this test is checking. */
2453 1 : spdm_context->connection_info.capability.flags =
2454 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
2455 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP |
2456 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP |
2457 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCAP_CAP |
2458 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CERT_CAP;
2459 1 : spdm_context->local_context.capability.flags =
2460 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
2461 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP |
2462 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP |
2463 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCAP_CAP |
2464 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
2465 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
2466 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
2467 1 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
2468 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
2469 : m_libspdm_use_measurement_hash_algo;
2470 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
2471 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
2472 1 : spdm_context->connection_info.algorithm.req_base_asym_alg = m_libspdm_use_req_asym_algo;
2473 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
2474 : SPDM_VERSION_NUMBER_SHIFT_BIT;
2475 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
2476 : m_libspdm_use_asym_algo, &data1,
2477 : &data_size1, NULL, NULL)) {
2478 0 : return;
2479 : }
2480 1 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
2481 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
2482 :
2483 1 : g_mandatory_mut_auth = false;
2484 1 : spdm_context->local_context.secured_message_version.secured_message_version_count = 1;
2485 :
2486 9 : for (index = 0; index < LIBSPDM_ARRAY_SIZE(illegal_values); index++) {
2487 8 : g_key_exchange_start_mut_auth = illegal_values[index];
2488 :
2489 8 : libspdm_reset_message_a(spdm_context);
2490 :
2491 8 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
2492 : m_libspdm_key_exchange_request1.random_data);
2493 8 : m_libspdm_key_exchange_request1.req_session_id = 0xFFFF;
2494 8 : m_libspdm_key_exchange_request1.reserved = 0;
2495 8 : ptr = m_libspdm_key_exchange_request1.exchange_data;
2496 8 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
2497 8 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version,
2498 : m_libspdm_use_dhe_algo, false);
2499 8 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
2500 8 : ptr += dhe_key_size;
2501 8 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
2502 8 : opaque_key_exchange_req_size =
2503 8 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
2504 8 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
2505 8 : ptr += sizeof(uint16_t);
2506 8 : libspdm_build_opaque_data_supported_version_data(
2507 : spdm_context, &opaque_key_exchange_req_size, ptr);
2508 8 : ptr += opaque_key_exchange_req_size;
2509 :
2510 8 : response_size = sizeof(response);
2511 8 : status = libspdm_get_response_key_exchange(
2512 : spdm_context, m_libspdm_key_exchange_request1_size,
2513 : &m_libspdm_key_exchange_request1, &response_size, response);
2514 8 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
2515 :
2516 8 : assert_int_equal(response_size, sizeof(spdm_error_response_t));
2517 8 : spdm_response = (void *)response;
2518 8 : assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
2519 8 : assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_UNSPECIFIED);
2520 8 : assert_int_equal(spdm_response->header.param2, 0);
2521 : }
2522 :
2523 1 : free(data1);
2524 : }
2525 :
2526 : /**
2527 : * Test 29: the SlotIDParam that the Integrator returns alongside MutAuthRequested. It shall be
2528 : * 0xF when the Requester's public key was provisioned to the Responder, 0 through 7 when the
2529 : * Requester has a certificate chain, and 0 for the encapsulated flows, which convey the slot in
2530 : * the final ENCAPSULATED_RESPONSE_ACK instead.
2531 : * Expected behavior: a legal value is returned in KEY_EXCHANGE_RSP and recorded in the session;
2532 : * an illegal one is rejected with ERROR(Unspecified).
2533 : **/
2534 1 : static void rsp_key_exchange_rsp_case29(void **state)
2535 : {
2536 : libspdm_return_t status;
2537 : libspdm_test_context_t *spdm_test_context;
2538 : libspdm_context_t *spdm_context;
2539 : libspdm_session_info_t *session_info;
2540 : size_t response_size;
2541 : uint8_t response[LIBSPDM_MAX_SPDM_MSG_SIZE];
2542 : spdm_key_exchange_response_t *spdm_response;
2543 : void *data1;
2544 : size_t data_size1;
2545 : uint8_t *ptr;
2546 : size_t dhe_key_size;
2547 : void *dhe_context;
2548 : size_t opaque_key_exchange_req_size;
2549 : size_t index;
2550 : size_t session_index;
2551 : const struct {
2552 : bool pub_key_id_cap;
2553 : uint8_t mut_auth_requested;
2554 : uint8_t req_slot_id;
2555 : bool legal;
2556 : /* The slot recorded in the session. Only checked when legal. */
2557 : uint8_t peer_slot;
2558 1 : } cases[] = {
2559 : /* The Requester has a certificate chain, so the slot shall be 0 through 7. */
2560 : { false, SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED, 0, true, 0 },
2561 : { false, SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED, SPDM_MAX_SLOT_COUNT - 1, true,
2562 : SPDM_MAX_SLOT_COUNT - 1 },
2563 : { false, SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED, SPDM_MAX_SLOT_COUNT, false, 0 },
2564 : { false, SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED, 0xF, false, 0 },
2565 : /* The Requester's public key was provisioned, so the slot shall be 0xF. libspdm records
2566 : * that as 0xFF. */
2567 : { true, SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED, 0xF, true, 0xFF },
2568 : { true, SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED, 0, false, 0 },
2569 : /* The encapsulated flows convey the slot separately, so SlotIDParam shall be 0. */
2570 : #if LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP
2571 : /* The accepted row starts an encapsulated flow, which the Responder only supports when
2572 : * it is built with LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP. */
2573 : { false, SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST, 0, true, 0 },
2574 : #endif /* LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP */
2575 : { false, SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST, 1, false, 0 },
2576 : { false, SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_GET_DIGESTS, 1, false, 0 },
2577 : };
2578 :
2579 1 : spdm_test_context = *state;
2580 1 : spdm_context = spdm_test_context->spdm_context;
2581 1 : spdm_test_context->case_id = 0x1D;
2582 :
2583 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
2584 : m_libspdm_use_asym_algo, &data1,
2585 : &data_size1, NULL, NULL)) {
2586 0 : return;
2587 : }
2588 :
2589 10 : for (index = 0; index < LIBSPDM_ARRAY_SIZE(cases); index++) {
2590 : /* Release every session so that the loop does not exhaust them, and clear the
2591 : * encapsulated state that a preceding row starts, which would otherwise answer the next
2592 : * row with ERROR(RequestInFlight). */
2593 45 : for (session_index = 0; session_index < LIBSPDM_MAX_SESSION_COUNT; session_index++) {
2594 36 : libspdm_session_info_init(spdm_context, &spdm_context->session_info[session_index],
2595 : INVALID_SESSION_ID, 0, false);
2596 : }
2597 9 : spdm_context->response_state = LIBSPDM_RESPONSE_STATE_NORMAL;
2598 :
2599 9 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
2600 9 : spdm_context->connection_info.capability.flags |=
2601 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
2602 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP |
2603 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP |
2604 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_ENCAP_CAP;
2605 9 : if (cases[index].pub_key_id_cap) {
2606 2 : spdm_context->connection_info.capability.flags |=
2607 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PUB_KEY_ID_CAP;
2608 : } else {
2609 7 : spdm_context->connection_info.capability.flags &=
2610 : ~(uint32_t)SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PUB_KEY_ID_CAP;
2611 : }
2612 9 : spdm_context->local_context.capability.flags |=
2613 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
2614 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP |
2615 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP |
2616 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_ENCAP_CAP |
2617 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CERT_CAP;
2618 9 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
2619 9 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
2620 9 : spdm_context->connection_info.algorithm.measurement_spec = m_libspdm_use_measurement_spec;
2621 9 : spdm_context->connection_info.algorithm.measurement_hash_algo =
2622 : m_libspdm_use_measurement_hash_algo;
2623 9 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
2624 9 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
2625 9 : spdm_context->connection_info.algorithm.req_base_asym_alg = m_libspdm_use_req_asym_algo;
2626 9 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
2627 : SPDM_VERSION_NUMBER_SHIFT_BIT;
2628 9 : spdm_context->local_context.local_cert_chain_provision[0] = data1;
2629 9 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size1;
2630 9 : spdm_context->local_context.secured_message_version.secured_message_version_count = 1;
2631 :
2632 9 : libspdm_reset_message_a(spdm_context);
2633 9 : g_key_exchange_start_mut_auth = cases[index].mut_auth_requested;
2634 9 : g_key_exchange_req_slot_id = cases[index].req_slot_id;
2635 9 : g_mandatory_mut_auth = false;
2636 :
2637 9 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE,
2638 : m_libspdm_key_exchange_request1.random_data);
2639 9 : m_libspdm_key_exchange_request1.req_session_id = 0xFFFF;
2640 9 : m_libspdm_key_exchange_request1.reserved = 0;
2641 9 : ptr = m_libspdm_key_exchange_request1.exchange_data;
2642 9 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
2643 9 : dhe_context = libspdm_dhe_new(spdm_context->connection_info.version,
2644 : m_libspdm_use_dhe_algo, false);
2645 9 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
2646 9 : ptr += dhe_key_size;
2647 9 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
2648 9 : opaque_key_exchange_req_size =
2649 9 : libspdm_get_opaque_data_supported_version_data_size(spdm_context);
2650 9 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_req_size);
2651 9 : ptr += sizeof(uint16_t);
2652 9 : libspdm_build_opaque_data_supported_version_data(
2653 : spdm_context, &opaque_key_exchange_req_size, ptr);
2654 9 : ptr += opaque_key_exchange_req_size;
2655 :
2656 9 : response_size = sizeof(response);
2657 9 : status = libspdm_get_response_key_exchange(
2658 : spdm_context, m_libspdm_key_exchange_request1_size,
2659 : &m_libspdm_key_exchange_request1, &response_size, response);
2660 9 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
2661 9 : spdm_response = (void *)response;
2662 :
2663 9 : if (cases[index].legal) {
2664 4 : assert_int_equal(spdm_response->header.request_response_code, SPDM_KEY_EXCHANGE_RSP);
2665 4 : session_info = libspdm_get_session_info_via_session_id(
2666 : spdm_context, spdm_context->latest_session_id);
2667 4 : assert_non_null(session_info);
2668 4 : assert_int_equal(session_info->peer_used_cert_chain_slot_id, cases[index].peer_slot);
2669 : } else {
2670 5 : assert_int_equal(spdm_response->header.request_response_code, SPDM_ERROR);
2671 5 : assert_int_equal(spdm_response->header.param1, SPDM_ERROR_CODE_UNSPECIFIED);
2672 5 : assert_int_equal(spdm_response->header.param2, 0);
2673 : }
2674 : }
2675 :
2676 1 : free(data1);
2677 : }
2678 : #endif /* LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP */
2679 :
2680 1 : int libspdm_rsp_key_exchange_rsp_test(void)
2681 : {
2682 1 : const struct CMUnitTest test_cases[] = {
2683 : /* Success Case*/
2684 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case1, rsp_key_exchange_rsp_setup),
2685 : /* Bad request size*/
2686 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case2, rsp_key_exchange_rsp_setup),
2687 : /* response_state: SPDM_RESPONSE_STATE_BUSY*/
2688 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case3, rsp_key_exchange_rsp_setup),
2689 : /* response_state: SPDM_RESPONSE_STATE_NEED_RESYNC*/
2690 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case4, rsp_key_exchange_rsp_setup),
2691 : #if LIBSPDM_RESPOND_IF_READY_SUPPORT
2692 : /* response_state: SPDM_RESPONSE_STATE_NOT_READY*/
2693 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case5, rsp_key_exchange_rsp_setup),
2694 : #endif /* LIBSPDM_RESPOND_IF_READY_SUPPORT */
2695 : /* connection_state Check*/
2696 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case6, rsp_key_exchange_rsp_setup),
2697 : /* Buffer reset*/
2698 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case7, rsp_key_exchange_rsp_setup),
2699 : /* TCB measurement hash requested */
2700 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case8, rsp_key_exchange_rsp_setup),
2701 : /* All measurement hash requested */
2702 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case9, rsp_key_exchange_rsp_setup),
2703 : /* Reserved value in Measurement summary. Error + Invalid */
2704 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case10, rsp_key_exchange_rsp_setup),
2705 : /* TCB measurement hash requested, measurement flag not set */
2706 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case11, rsp_key_exchange_rsp_setup),
2707 : /* Request previously provisioned public key, slot 0xFF */
2708 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case14, rsp_key_exchange_rsp_setup),
2709 : /* HANDSHAKE_IN_THE_CLEAR set for requester and responder */
2710 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case15, rsp_key_exchange_rsp_setup),
2711 : /* Buffer verification*/
2712 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case16, rsp_key_exchange_rsp_setup),
2713 : /* Successful response V1.2*/
2714 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case17, rsp_key_exchange_rsp_setup),
2715 : /* Invalid SlotID in KEY_EXCHANGE request message*/
2716 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case18, rsp_key_exchange_rsp_setup),
2717 : /* Only OpaqueDataFmt1 is supported, Bytes not aligned*/
2718 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case19, rsp_key_exchange_rsp_setup),
2719 : /* OpaqueData only supports OpaqueDataFmt1, Success Case */
2720 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case20, rsp_key_exchange_rsp_setup),
2721 : /* The key usage bit mask is not set, failed Case*/
2722 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case21, rsp_key_exchange_rsp_setup),
2723 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case22, rsp_key_exchange_rsp_setup),
2724 : #if LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP
2725 : /* The Responder requires mutual authentication, but the Requester does not support it */
2726 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case23, rsp_key_exchange_rsp_setup),
2727 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case24, rsp_key_exchange_rsp_setup),
2728 : #endif /* LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP */
2729 : /* The Responder using integrator defined opaque data */
2730 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case25, rsp_key_exchange_rsp_setup),
2731 : #if LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP
2732 : /* MUT_AUTH_REQUESTED (bit 0) with a non-zero ReqSlotID is recorded in the session */
2733 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case26, rsp_key_exchange_rsp_setup),
2734 : /* Only MUT_AUTH_REQUESTED (bit 0) is legal when the Requester's PUB_KEY_ID_CAP is set */
2735 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case27, rsp_key_exchange_rsp_setup),
2736 : /* MutAuthRequested values that are not one of the three legal values */
2737 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case28, rsp_key_exchange_rsp_setup),
2738 : /* SlotIDParam values that go with each MutAuthRequested value */
2739 : cmocka_unit_test_setup(rsp_key_exchange_rsp_case29, rsp_key_exchange_rsp_setup),
2740 : #endif /* LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP */
2741 : };
2742 :
2743 1 : libspdm_test_context_t test_context = {
2744 : LIBSPDM_TEST_CONTEXT_VERSION,
2745 : false,
2746 : };
2747 :
2748 1 : libspdm_setup_test_context(&test_context);
2749 :
2750 1 : return cmocka_run_group_tests(test_cases,
2751 : libspdm_unit_test_group_setup,
2752 : libspdm_unit_test_group_teardown);
2753 : }
2754 :
2755 : #endif /* LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP*/
|