Line data Source code
1 : /**
2 : * Copyright Notice:
3 : * Copyright 2021-2024 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 :
10 : #if (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP) && (LIBSPDM_ENABLE_CAPABILITY_ENCAP_CAP) && \
11 : (LIBSPDM_SEND_CHALLENGE_SUPPORT)
12 :
13 : static uint8_t m_requester_context[SPDM_REQ_CONTEXT_SIZE];
14 :
15 1 : void libspdm_test_responder_encap_challenge_case1(void **state)
16 : {
17 : libspdm_return_t status;
18 : libspdm_test_context_t *spdm_test_context;
19 : libspdm_context_t *spdm_context;
20 : spdm_challenge_auth_response_t *spdm_response;
21 : uint8_t temp_buf[LIBSPDM_SENDER_BUFFER_SIZE];
22 : uint8_t *ptr;
23 : size_t response_size;
24 : size_t sig_size;
25 : bool need_continue;
26 : void *data;
27 : size_t data_size;
28 :
29 1 : spdm_test_context = *state;
30 1 : spdm_context = spdm_test_context->spdm_context;
31 :
32 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
33 : SPDM_VERSION_NUMBER_SHIFT_BIT;
34 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
35 1 : spdm_context->connection_info.capability.flags = 0;
36 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHAL_CAP;
37 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
38 1 : spdm_context->connection_info.algorithm.req_base_asym_alg = m_libspdm_use_req_asym_algo;
39 1 : libspdm_read_requester_public_certificate_chain(m_libspdm_use_hash_algo,
40 : m_libspdm_use_req_asym_algo, &data,
41 : &data_size,
42 : NULL, NULL);
43 1 : libspdm_reset_message_mut_c(spdm_context);
44 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
45 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
46 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
47 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
48 : data, data_size);
49 : #else
50 1 : libspdm_hash_all(
51 : spdm_context->connection_info.algorithm.base_hash_algo,
52 : data, data_size,
53 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
54 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
55 1 : libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
56 1 : libspdm_get_leaf_cert_public_key_from_cert_chain(
57 : spdm_context->connection_info.algorithm.base_hash_algo,
58 1 : spdm_context->connection_info.algorithm.req_base_asym_alg,
59 : data, data_size,
60 : &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
61 : #endif
62 1 : spdm_context->encap_context.req_slot_id = 0;
63 :
64 1 : sig_size = libspdm_get_asym_signature_size(m_libspdm_use_req_asym_algo);
65 1 : response_size = sizeof(spdm_challenge_auth_response_t) +
66 1 : libspdm_get_hash_size(m_libspdm_use_hash_algo) +
67 1 : SPDM_NONCE_SIZE + 0 + sizeof(uint16_t) + 0 + sig_size;
68 1 : spdm_response = (void *)temp_buf;
69 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
70 1 : spdm_response->header.request_response_code = SPDM_CHALLENGE_AUTH;
71 1 : spdm_response->header.param1 = 0;
72 1 : spdm_response->header.param2 = (1 << 0);
73 :
74 1 : ptr = (void *)(spdm_response + 1);
75 1 : libspdm_hash_all(m_libspdm_use_hash_algo, data, data_size, ptr);
76 1 : ptr += libspdm_get_hash_size(m_libspdm_use_hash_algo);
77 1 : libspdm_get_random_number(SPDM_NONCE_SIZE, ptr);
78 1 : ptr += SPDM_NONCE_SIZE;
79 :
80 1 : *(uint16_t *)ptr = 0;
81 1 : ptr += sizeof(uint16_t);
82 :
83 1 : libspdm_requester_data_sign(
84 : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
85 : spdm_context,
86 : #endif
87 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
88 : SPDM_CHALLENGE_AUTH,
89 : m_libspdm_use_req_asym_algo, m_libspdm_use_hash_algo,
90 : false, (uint8_t*)spdm_response, response_size - sig_size,
91 : ptr, &sig_size);
92 :
93 1 : status = libspdm_process_encap_response_challenge_auth(spdm_context, response_size,
94 : spdm_response,
95 : &need_continue);
96 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
97 : /* Completion of CHALLENGE sets M1/M2 to null. */
98 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
99 : assert_int_equal(spdm_context->transcript.message_mut_c.buffer_size, 0);
100 : #else
101 1 : assert_null(spdm_context->transcript.digest_context_mut_m1m2);
102 : #endif
103 1 : free(data);
104 1 : }
105 :
106 1 : void libspdm_test_responder_encap_challenge_case2(void **state)
107 : {
108 : libspdm_return_t status;
109 : libspdm_test_context_t *spdm_test_context;
110 : libspdm_context_t *spdm_context;
111 : spdm_error_response_t *spdm_response;
112 : uint8_t temp_buf[LIBSPDM_MAX_SPDM_MSG_SIZE];
113 : size_t response_size;
114 : void *data;
115 : size_t data_size;
116 :
117 1 : spdm_test_context = *state;
118 1 : spdm_context = spdm_test_context->spdm_context;
119 :
120 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
121 1 : spdm_context->connection_info.capability.flags = 0;
122 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHAL_CAP;
123 1 : libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
124 : m_libspdm_use_asym_algo, &data,
125 : &data_size,
126 : NULL, NULL);
127 1 : libspdm_reset_message_a(spdm_context);
128 1 : libspdm_reset_message_b(spdm_context);
129 1 : libspdm_reset_message_c(spdm_context);
130 :
131 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
132 1 : spdm_context->connection_info.algorithm.req_base_asym_alg = m_libspdm_use_req_asym_algo;
133 :
134 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
135 : SPDM_VERSION_NUMBER_SHIFT_BIT;
136 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
137 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
138 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
139 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
140 : data, data_size);
141 : #else
142 1 : libspdm_hash_all(
143 : spdm_context->connection_info.algorithm.base_hash_algo,
144 : data, data_size,
145 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
146 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
147 1 : libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
148 1 : libspdm_get_leaf_cert_public_key_from_cert_chain(
149 : spdm_context->connection_info.algorithm.base_hash_algo,
150 1 : spdm_context->connection_info.algorithm.req_base_asym_alg,
151 : data, data_size,
152 : &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
153 : #endif
154 1 : spdm_context->encap_context.req_slot_id = 0;
155 :
156 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size;
157 1 : spdm_context->local_context.local_cert_chain_provision[0] = data;
158 :
159 1 : response_size = sizeof(spdm_error_response_t);
160 1 : spdm_response = (void *)temp_buf;
161 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
162 1 : spdm_response->header.request_response_code = SPDM_ERROR;
163 1 : spdm_response->header.param1 = 0;
164 1 : spdm_response->header.param2 = 0;
165 :
166 1 : status = libspdm_process_encap_response_challenge_auth(spdm_context, response_size,
167 : spdm_response,
168 : NULL);
169 1 : assert_int_equal(status, LIBSPDM_STATUS_UNSUPPORTED_CAP);
170 1 : free(data);
171 1 : }
172 :
173 :
174 1 : void libspdm_test_responder_encap_challenge_case3(void **state)
175 : {
176 : libspdm_return_t status;
177 : libspdm_test_context_t *spdm_test_context;
178 : libspdm_context_t *spdm_context;
179 : spdm_challenge_auth_response_t *spdm_response;
180 : uint8_t temp_buf[LIBSPDM_MAX_SPDM_MSG_SIZE];
181 : size_t response_size;
182 : void *data;
183 : size_t data_size;
184 :
185 1 : spdm_test_context = *state;
186 1 : spdm_context = spdm_test_context->spdm_context;
187 :
188 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
189 1 : spdm_context->connection_info.capability.flags = 0;
190 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHAL_CAP;
191 1 : libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
192 : m_libspdm_use_asym_algo, &data,
193 : &data_size,
194 : NULL, NULL);
195 1 : libspdm_reset_message_a(spdm_context);
196 1 : libspdm_reset_message_b(spdm_context);
197 1 : libspdm_reset_message_c(spdm_context);
198 :
199 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
200 1 : spdm_context->connection_info.algorithm.req_base_asym_alg = m_libspdm_use_req_asym_algo;
201 :
202 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
203 : SPDM_VERSION_NUMBER_SHIFT_BIT;
204 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
205 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
206 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
207 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
208 : data, data_size);
209 : #else
210 1 : libspdm_hash_all(
211 : spdm_context->connection_info.algorithm.base_hash_algo,
212 : data, data_size,
213 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
214 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
215 1 : libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
216 1 : libspdm_get_leaf_cert_public_key_from_cert_chain(
217 : spdm_context->connection_info.algorithm.base_hash_algo,
218 1 : spdm_context->connection_info.algorithm.req_base_asym_alg,
219 : data, data_size,
220 : &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
221 : #endif
222 1 : spdm_context->encap_context.req_slot_id = 0;
223 :
224 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size;
225 1 : spdm_context->local_context.local_cert_chain_provision[0] = data;
226 :
227 1 : response_size = sizeof(spdm_error_response_t);
228 1 : spdm_response = (void *)temp_buf;
229 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
230 1 : spdm_response->header.request_response_code = SPDM_CERTIFICATE;
231 1 : spdm_response->header.param1 = 0;
232 1 : spdm_response->header.param2 = 0;
233 :
234 1 : status = libspdm_process_encap_response_challenge_auth(spdm_context, response_size,
235 : spdm_response,
236 : NULL);
237 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
238 1 : free(data);
239 1 : }
240 :
241 1 : void libspdm_test_responder_encap_challenge_case4(void **state)
242 : {
243 : libspdm_return_t status;
244 : libspdm_test_context_t *spdm_test_context;
245 : libspdm_context_t *spdm_context;
246 : spdm_challenge_auth_response_t *spdm_response;
247 : uint8_t temp_buf[LIBSPDM_MAX_SPDM_MSG_SIZE];
248 : size_t response_size;
249 : void *data;
250 : size_t data_size;
251 :
252 1 : spdm_test_context = *state;
253 1 : spdm_context = spdm_test_context->spdm_context;
254 :
255 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
256 1 : spdm_context->connection_info.capability.flags = 0;
257 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHAL_CAP;
258 1 : libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
259 : m_libspdm_use_asym_algo, &data,
260 : &data_size,
261 : NULL, NULL);
262 1 : libspdm_reset_message_a(spdm_context);
263 1 : libspdm_reset_message_b(spdm_context);
264 1 : libspdm_reset_message_c(spdm_context);
265 :
266 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
267 1 : spdm_context->connection_info.algorithm.req_base_asym_alg = m_libspdm_use_req_asym_algo;
268 :
269 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
270 : SPDM_VERSION_NUMBER_SHIFT_BIT;
271 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
272 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
273 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
274 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
275 : data, data_size);
276 : #else
277 1 : libspdm_hash_all(
278 : spdm_context->connection_info.algorithm.base_hash_algo,
279 : data, data_size,
280 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
281 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
282 1 : libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
283 1 : libspdm_get_leaf_cert_public_key_from_cert_chain(
284 : spdm_context->connection_info.algorithm.base_hash_algo,
285 1 : spdm_context->connection_info.algorithm.req_base_asym_alg,
286 : data, data_size,
287 : &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
288 : #endif
289 1 : spdm_context->encap_context.req_slot_id = 0;
290 :
291 1 : spdm_context->local_context.local_cert_chain_provision_size[0] = data_size;
292 1 : spdm_context->local_context.local_cert_chain_provision[0] = data;
293 :
294 1 : response_size = sizeof(spdm_error_response_t);
295 1 : spdm_response = (void *)temp_buf;
296 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
297 1 : spdm_response->header.request_response_code = SPDM_CHALLENGE_AUTH;
298 1 : spdm_response->header.param1 = 0;
299 1 : spdm_response->header.param2 = 0;
300 :
301 1 : status = libspdm_process_encap_response_challenge_auth(spdm_context, response_size,
302 : spdm_response,
303 : NULL);
304 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
305 1 : free(data);
306 1 : }
307 :
308 1 : void libspdm_test_responder_encap_challenge_case5(void **state)
309 : {
310 : libspdm_return_t status;
311 : libspdm_test_context_t *spdm_test_context;
312 : libspdm_context_t *spdm_context;
313 : spdm_challenge_auth_response_t *spdm_response;
314 : uint8_t temp_buf[LIBSPDM_MAX_SPDM_MSG_SIZE];
315 : size_t response_size;
316 : size_t sig_size;
317 : uint8_t *ptr;
318 : bool need_continue;
319 : void *data;
320 : size_t data_size;
321 :
322 1 : spdm_test_context = *state;
323 1 : spdm_context = spdm_test_context->spdm_context;
324 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
325 : SPDM_VERSION_NUMBER_SHIFT_BIT;
326 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
327 1 : spdm_context->connection_info.capability.flags = 0;
328 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHAL_CAP;
329 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
330 1 : spdm_context->connection_info.algorithm.req_base_asym_alg = m_libspdm_use_req_asym_algo;
331 :
332 1 : libspdm_read_requester_public_key(m_libspdm_use_req_asym_algo, &data, &data_size);
333 1 : spdm_context->local_context.peer_public_key_provision = data;
334 1 : spdm_context->local_context.peer_public_key_provision_size = data_size;
335 :
336 1 : spdm_context->encap_context.req_slot_id = 0xFF;
337 :
338 1 : libspdm_reset_message_a(spdm_context);
339 1 : libspdm_reset_message_b(spdm_context);
340 1 : libspdm_reset_message_c(spdm_context);
341 :
342 1 : sig_size = libspdm_get_asym_signature_size(m_libspdm_use_req_asym_algo);
343 1 : response_size = sizeof(spdm_challenge_auth_response_t) +
344 1 : libspdm_get_hash_size(m_libspdm_use_hash_algo) +
345 1 : SPDM_NONCE_SIZE + 0 + sizeof(uint16_t) + 0 + sig_size;
346 1 : spdm_response = (void *)temp_buf;
347 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
348 1 : spdm_response->header.request_response_code = SPDM_CHALLENGE_AUTH;
349 1 : spdm_response->header.param1 = (0xFF & SPDM_CHALLENGE_AUTH_RESPONSE_ATTRIBUTE_SLOT_ID_MASK);
350 1 : spdm_response->header.param2 = 0;
351 :
352 1 : ptr = (void *)(spdm_response + 1);
353 1 : libspdm_hash_all(m_libspdm_use_hash_algo, data, data_size, ptr);
354 1 : ptr += libspdm_get_hash_size(m_libspdm_use_hash_algo);
355 1 : libspdm_get_random_number(SPDM_NONCE_SIZE, ptr);
356 1 : ptr += SPDM_NONCE_SIZE;
357 :
358 1 : *(uint16_t *)ptr = 0;
359 1 : ptr += sizeof(uint16_t);
360 :
361 1 : libspdm_requester_data_sign(
362 : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
363 : spdm_context,
364 : #endif
365 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
366 : SPDM_CHALLENGE_AUTH,
367 : m_libspdm_use_req_asym_algo, m_libspdm_use_hash_algo,
368 : false, (uint8_t*)spdm_response, response_size - sig_size,
369 : ptr, &sig_size);
370 :
371 1 : status = libspdm_process_encap_response_challenge_auth(spdm_context, response_size,
372 : spdm_response,
373 : &need_continue);
374 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
375 1 : free(data);
376 1 : }
377 :
378 : /**
379 : * Test 6: Successful case , With the correct challenge context field
380 : * Expected Behavior: client returns a status of RETURN_SUCCESS.
381 : **/
382 1 : void libspdm_test_responder_encap_challenge_case6(void **state)
383 : {
384 : libspdm_return_t status;
385 : libspdm_test_context_t *spdm_test_context;
386 : libspdm_context_t *spdm_context;
387 : spdm_challenge_auth_response_t *spdm_response;
388 : uint8_t temp_buf[LIBSPDM_SENDER_BUFFER_SIZE];
389 : uint8_t *ptr;
390 : size_t response_size;
391 : size_t sig_size;
392 : bool need_continue;
393 : void *data;
394 : size_t data_size;
395 :
396 1 : spdm_test_context = *state;
397 1 : spdm_context = spdm_test_context->spdm_context;
398 :
399 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
400 : SPDM_VERSION_NUMBER_SHIFT_BIT;
401 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
402 1 : spdm_context->connection_info.capability.flags = 0;
403 1 : spdm_context->connection_info.capability.flags |= SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_CHAL_CAP;
404 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
405 1 : spdm_context->connection_info.algorithm.req_base_asym_alg = m_libspdm_use_req_asym_algo;
406 1 : libspdm_read_requester_public_certificate_chain(m_libspdm_use_hash_algo,
407 : m_libspdm_use_req_asym_algo, &data,
408 : &data_size,
409 : NULL, NULL);
410 1 : libspdm_reset_message_mut_c(spdm_context);
411 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
412 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
413 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
414 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
415 : data, data_size);
416 : #else
417 1 : libspdm_hash_all(
418 : spdm_context->connection_info.algorithm.base_hash_algo,
419 : data, data_size,
420 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
421 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
422 1 : libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
423 1 : libspdm_get_leaf_cert_public_key_from_cert_chain(
424 : spdm_context->connection_info.algorithm.base_hash_algo,
425 1 : spdm_context->connection_info.algorithm.req_base_asym_alg,
426 : data, data_size,
427 : &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
428 : #endif
429 1 : spdm_context->encap_context.req_slot_id = 0;
430 1 : libspdm_set_mem(m_requester_context, SPDM_REQ_CONTEXT_SIZE, 0xAA);
431 1 : libspdm_copy_mem(spdm_context->encap_context.req_context, SPDM_REQ_CONTEXT_SIZE,
432 : m_requester_context, SPDM_REQ_CONTEXT_SIZE);
433 :
434 :
435 1 : sig_size = libspdm_get_asym_signature_size(m_libspdm_use_req_asym_algo);
436 1 : response_size = sizeof(spdm_challenge_auth_response_t) +
437 1 : libspdm_get_hash_size(m_libspdm_use_hash_algo) +
438 1 : SPDM_NONCE_SIZE + 0 + sizeof(uint16_t) + 0 + SPDM_REQ_CONTEXT_SIZE + sig_size;
439 1 : spdm_response = (void *)temp_buf;
440 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_13;
441 1 : spdm_response->header.request_response_code = SPDM_CHALLENGE_AUTH;
442 1 : spdm_response->header.param1 = 0;
443 1 : spdm_response->header.param2 = (1 << 0);
444 :
445 1 : ptr = (void *)(spdm_response + 1);
446 1 : libspdm_hash_all(m_libspdm_use_hash_algo, data, data_size, ptr);
447 1 : ptr += libspdm_get_hash_size(m_libspdm_use_hash_algo);
448 1 : libspdm_get_random_number(SPDM_NONCE_SIZE, ptr);
449 1 : ptr += SPDM_NONCE_SIZE;
450 :
451 1 : *(uint16_t *)ptr = 0;
452 1 : ptr += sizeof(uint16_t);
453 :
454 1 : libspdm_set_mem(ptr, SPDM_REQ_CONTEXT_SIZE, 0xAA);
455 1 : ptr += SPDM_REQ_CONTEXT_SIZE;
456 :
457 1 : libspdm_requester_data_sign(
458 : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
459 : spdm_context,
460 : #endif
461 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
462 : SPDM_CHALLENGE_AUTH,
463 : m_libspdm_use_req_asym_algo, m_libspdm_use_hash_algo,
464 : false, (uint8_t*)spdm_response, response_size - sig_size,
465 : ptr, &sig_size);
466 :
467 1 : status = libspdm_process_encap_response_challenge_auth(spdm_context, response_size,
468 : spdm_response,
469 : &need_continue);
470 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
471 :
472 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
473 : assert_int_equal(spdm_context->transcript.message_mut_c.buffer_size, 0);
474 : #else
475 1 : assert_null(spdm_context->transcript.digest_context_mut_m1m2);
476 : #endif
477 1 : free(data);
478 1 : }
479 :
480 1 : int libspdm_responder_encap_challenge_auth_test_main(void)
481 : {
482 1 : const struct CMUnitTest spdm_responder_challenge_tests[] = {
483 : cmocka_unit_test(libspdm_test_responder_encap_challenge_case1),
484 : /* Error response: SPDM_ERROR*/
485 : cmocka_unit_test(libspdm_test_responder_encap_challenge_case2),
486 : /* Error request_response_code : SPDM_CERTIFICATE */
487 : cmocka_unit_test(libspdm_test_responder_encap_challenge_case3),
488 : /* Error spdm_response_size */
489 : cmocka_unit_test(libspdm_test_responder_encap_challenge_case4),
490 : /* Success Case, use provisioned public key (slot 0xFF) */
491 : cmocka_unit_test(libspdm_test_responder_encap_challenge_case5),
492 : /* Success Case, V1.3 With the correct challenge context field */
493 : cmocka_unit_test(libspdm_test_responder_encap_challenge_case6),
494 : };
495 :
496 1 : libspdm_test_context_t test_context = {
497 : LIBSPDM_TEST_CONTEXT_VERSION,
498 : false,
499 : };
500 :
501 1 : libspdm_setup_test_context(&test_context);
502 :
503 1 : return cmocka_run_group_tests(spdm_responder_challenge_tests,
504 : libspdm_unit_test_group_setup,
505 : libspdm_unit_test_group_teardown);
506 : }
507 :
508 : #endif /* (LIBSPDM_ENABLE_CAPABILITY_MUT_AUTH_CAP) && (...) */
|