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_requester_lib.h"
9 : #include "internal/libspdm_responder_lib.h"
10 :
11 : #if LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP
12 :
13 : static size_t m_libspdm_local_buffer_size;
14 : static uint8_t m_libspdm_local_buffer[LIBSPDM_MAX_MESSAGE_TH_BUFFER_SIZE];
15 : static uint8_t m_libspdm_zero_filled_buffer[LIBSPDM_MAX_HASH_SIZE];
16 :
17 : static libspdm_th_managed_buffer_t th_curr;
18 :
19 40 : static size_t libspdm_test_get_key_exchange_request_size(const void *spdm_context,
20 : const void *buffer,
21 : size_t buffer_size)
22 : {
23 : const spdm_key_exchange_request_t *spdm_request;
24 : size_t message_size;
25 : size_t dhe_key_size;
26 : uint16_t opaque_length;
27 :
28 40 : spdm_request = buffer;
29 40 : message_size = sizeof(spdm_message_header_t);
30 40 : if (buffer_size < message_size) {
31 0 : return buffer_size;
32 : }
33 :
34 40 : if (spdm_request->header.request_response_code != SPDM_KEY_EXCHANGE) {
35 1 : return buffer_size;
36 : }
37 :
38 39 : message_size = sizeof(spdm_key_exchange_request_t);
39 39 : if (buffer_size < message_size) {
40 0 : return buffer_size;
41 : }
42 :
43 39 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
44 39 : message_size += dhe_key_size + sizeof(uint16_t);
45 39 : if (buffer_size < message_size) {
46 0 : return buffer_size;
47 : }
48 :
49 : opaque_length =
50 39 : libspdm_read_uint16((const uint8_t *)buffer +
51 39 : sizeof(spdm_key_exchange_request_t) + dhe_key_size);
52 39 : message_size += opaque_length;
53 39 : if (buffer_size < message_size) {
54 0 : return buffer_size;
55 : }
56 :
57 : /* Good message, return actual size*/
58 39 : return message_size;
59 : }
60 :
61 41 : static libspdm_return_t send_message(
62 : void *spdm_context, size_t request_size, const void *request, uint64_t timeout)
63 : {
64 : libspdm_test_context_t *spdm_test_context;
65 : size_t header_size;
66 : size_t message_size;
67 :
68 41 : spdm_test_context = libspdm_get_test_context();
69 41 : header_size = sizeof(libspdm_test_message_header_t);
70 41 : switch (spdm_test_context->case_id) {
71 1 : case 0x1:
72 1 : return LIBSPDM_STATUS_SEND_FAIL;
73 1 : case 0x2:
74 1 : m_libspdm_local_buffer_size = 0;
75 1 : message_size = libspdm_test_get_key_exchange_request_size(
76 : spdm_context, (const uint8_t *)request + header_size,
77 : request_size - header_size);
78 1 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
79 : (const uint8_t *)request + header_size, message_size);
80 1 : m_libspdm_local_buffer_size += message_size;
81 1 : return LIBSPDM_STATUS_SUCCESS;
82 0 : case 0x3:
83 0 : m_libspdm_local_buffer_size = 0;
84 0 : message_size = libspdm_test_get_key_exchange_request_size(
85 : spdm_context, (const uint8_t *)request + header_size,
86 : request_size - header_size);
87 0 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
88 : (const uint8_t *)request + header_size, message_size);
89 0 : m_libspdm_local_buffer_size += message_size;
90 0 : return LIBSPDM_STATUS_SUCCESS;
91 1 : case 0x4:
92 1 : m_libspdm_local_buffer_size = 0;
93 1 : message_size = libspdm_test_get_key_exchange_request_size(
94 : spdm_context, (const uint8_t *)request + header_size,
95 : request_size - header_size);
96 1 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
97 : (const uint8_t *)request + header_size, message_size);
98 1 : m_libspdm_local_buffer_size += message_size;
99 1 : return LIBSPDM_STATUS_SUCCESS;
100 1 : case 0x5:
101 1 : m_libspdm_local_buffer_size = 0;
102 1 : message_size = libspdm_test_get_key_exchange_request_size(
103 : spdm_context, (const uint8_t *)request + header_size,
104 : request_size - header_size);
105 1 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
106 : (const uint8_t *)request + header_size, message_size);
107 1 : m_libspdm_local_buffer_size += message_size;
108 1 : return LIBSPDM_STATUS_SUCCESS;
109 0 : case 0x6:
110 0 : m_libspdm_local_buffer_size = 0;
111 0 : message_size = libspdm_test_get_key_exchange_request_size(
112 : spdm_context, (const uint8_t *)request + header_size,
113 : request_size - header_size);
114 0 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
115 : (const uint8_t *)request + header_size, message_size);
116 0 : m_libspdm_local_buffer_size += message_size;
117 0 : return LIBSPDM_STATUS_SUCCESS;
118 1 : case 0x7:
119 1 : m_libspdm_local_buffer_size = 0;
120 1 : message_size = libspdm_test_get_key_exchange_request_size(
121 : spdm_context, (const uint8_t *)request + header_size,
122 : request_size - header_size);
123 1 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
124 : (const uint8_t *)request + header_size, message_size);
125 1 : m_libspdm_local_buffer_size += message_size;
126 1 : return LIBSPDM_STATUS_SUCCESS;
127 2 : case 0x8:
128 2 : m_libspdm_local_buffer_size = 0;
129 2 : message_size = libspdm_test_get_key_exchange_request_size(
130 : spdm_context, (const uint8_t *)request + header_size,
131 : request_size - header_size);
132 2 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
133 : (const uint8_t *)request + header_size, message_size);
134 2 : m_libspdm_local_buffer_size += message_size;
135 2 : return LIBSPDM_STATUS_SUCCESS;
136 0 : case 0x9: {
137 : static size_t sub_index = 0;
138 0 : if (sub_index == 0) {
139 0 : m_libspdm_local_buffer_size = 0;
140 0 : message_size = libspdm_test_get_key_exchange_request_size(
141 : spdm_context, (const uint8_t *)request + header_size,
142 : request_size - header_size);
143 0 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
144 : (const uint8_t *)request + header_size, message_size);
145 0 : m_libspdm_local_buffer_size += message_size;
146 0 : sub_index++;
147 : }
148 : }
149 0 : return LIBSPDM_STATUS_SUCCESS;
150 18 : case 0xA:
151 18 : m_libspdm_local_buffer_size = 0;
152 18 : message_size = libspdm_test_get_key_exchange_request_size(
153 : spdm_context, (const uint8_t *)request + header_size,
154 : request_size - header_size);
155 18 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
156 : (const uint8_t *)request + header_size, message_size);
157 18 : m_libspdm_local_buffer_size += message_size;
158 18 : return LIBSPDM_STATUS_SUCCESS;
159 1 : case 0xB:
160 1 : m_libspdm_local_buffer_size = 0;
161 1 : message_size = libspdm_test_get_key_exchange_request_size(
162 : spdm_context, (const uint8_t *)request + header_size,
163 : request_size - header_size);
164 1 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
165 : (const uint8_t *)request + header_size, message_size);
166 1 : m_libspdm_local_buffer_size += message_size;
167 1 : return LIBSPDM_STATUS_SUCCESS;
168 1 : case 0xC:
169 1 : m_libspdm_local_buffer_size = 0;
170 1 : message_size = libspdm_test_get_key_exchange_request_size(
171 : spdm_context, (const uint8_t *)request + header_size,
172 : request_size - header_size);
173 1 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
174 : (const uint8_t *)request + header_size, message_size);
175 1 : m_libspdm_local_buffer_size += message_size;
176 1 : return LIBSPDM_STATUS_SUCCESS;
177 1 : case 0xD:
178 1 : m_libspdm_local_buffer_size = 0;
179 1 : message_size = libspdm_test_get_key_exchange_request_size(
180 : spdm_context, (const uint8_t *)request + header_size,
181 : request_size - header_size);
182 1 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
183 : (const uint8_t *)request + header_size, message_size);
184 1 : m_libspdm_local_buffer_size += message_size;
185 1 : return LIBSPDM_STATUS_SUCCESS;
186 1 : case 0xE:
187 1 : m_libspdm_local_buffer_size = 0;
188 1 : message_size = libspdm_test_get_key_exchange_request_size(
189 : spdm_context, (const uint8_t *)request + header_size,
190 : request_size - header_size);
191 1 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
192 : (const uint8_t *)request + header_size, message_size);
193 1 : m_libspdm_local_buffer_size += message_size;
194 1 : return LIBSPDM_STATUS_SUCCESS;
195 1 : case 0xF:
196 1 : m_libspdm_local_buffer_size = 0;
197 1 : message_size = libspdm_test_get_key_exchange_request_size(
198 : spdm_context, (const uint8_t *)request + header_size,
199 : request_size - header_size);
200 1 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
201 : (const uint8_t *)request + header_size, message_size);
202 1 : m_libspdm_local_buffer_size += message_size;
203 1 : return LIBSPDM_STATUS_SUCCESS;
204 1 : case 0x10:
205 1 : m_libspdm_local_buffer_size = 0;
206 1 : message_size = libspdm_test_get_key_exchange_request_size(
207 : spdm_context, (const uint8_t *)request + header_size,
208 : request_size - header_size);
209 1 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
210 : (const uint8_t *)request + header_size, message_size);
211 1 : m_libspdm_local_buffer_size += message_size;
212 1 : return LIBSPDM_STATUS_SUCCESS;
213 1 : case 0x11:
214 1 : m_libspdm_local_buffer_size = 0;
215 1 : message_size = libspdm_test_get_key_exchange_request_size(
216 : spdm_context, (const uint8_t *)request + header_size,
217 : request_size - header_size);
218 1 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
219 : (const uint8_t *)request + header_size, message_size);
220 1 : m_libspdm_local_buffer_size += message_size;
221 1 : return LIBSPDM_STATUS_SUCCESS;
222 1 : case 0x12:
223 1 : m_libspdm_local_buffer_size = 0;
224 1 : message_size = libspdm_test_get_key_exchange_request_size(
225 : spdm_context, (const uint8_t *)request + header_size,
226 : request_size - header_size);
227 1 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
228 : (const uint8_t *)request + header_size, message_size);
229 1 : m_libspdm_local_buffer_size += message_size;
230 1 : return LIBSPDM_STATUS_SUCCESS;
231 0 : case 0x13:
232 0 : m_libspdm_local_buffer_size = 0;
233 0 : message_size = libspdm_test_get_key_exchange_request_size(
234 : spdm_context, (const uint8_t *)request + header_size,
235 : request_size - header_size);
236 0 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
237 : (const uint8_t *)request + header_size, message_size);
238 0 : m_libspdm_local_buffer_size += message_size;
239 0 : return LIBSPDM_STATUS_SUCCESS;
240 1 : case 0x14:
241 1 : m_libspdm_local_buffer_size = 0;
242 1 : message_size = libspdm_test_get_key_exchange_request_size(
243 : spdm_context, (const uint8_t *)request + header_size,
244 : request_size - header_size);
245 1 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
246 : (const uint8_t *)request + header_size, message_size);
247 1 : m_libspdm_local_buffer_size += message_size;
248 1 : return LIBSPDM_STATUS_SUCCESS;
249 1 : case 0x15:
250 1 : m_libspdm_local_buffer_size = 0;
251 1 : message_size = libspdm_test_get_key_exchange_request_size(
252 : spdm_context, (const uint8_t *)request + header_size,
253 : request_size - header_size);
254 1 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
255 : (const uint8_t *)request + header_size, message_size);
256 1 : m_libspdm_local_buffer_size += message_size;
257 1 : return LIBSPDM_STATUS_SUCCESS;
258 1 : case 0x16:
259 1 : m_libspdm_local_buffer_size = 0;
260 1 : message_size = libspdm_test_get_key_exchange_request_size(
261 : spdm_context, (const uint8_t *)request + header_size,
262 : request_size - header_size);
263 1 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
264 : (const uint8_t *)request + header_size, message_size);
265 1 : m_libspdm_local_buffer_size += message_size;
266 1 : return LIBSPDM_STATUS_SUCCESS;
267 1 : case 0x17:
268 1 : m_libspdm_local_buffer_size = 0;
269 1 : message_size = libspdm_test_get_key_exchange_request_size(
270 : spdm_context, (const uint8_t *)request + header_size,
271 : request_size - header_size);
272 1 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
273 : (const uint8_t *)request + header_size, message_size);
274 1 : m_libspdm_local_buffer_size += message_size;
275 1 : return LIBSPDM_STATUS_SUCCESS;
276 0 : case 0x18:
277 0 : m_libspdm_local_buffer_size = 0;
278 0 : message_size = libspdm_test_get_key_exchange_request_size(
279 : spdm_context, (const uint8_t *)request + header_size,
280 : request_size - header_size);
281 0 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
282 : (const uint8_t *)request + header_size, message_size);
283 0 : m_libspdm_local_buffer_size += message_size;
284 0 : return LIBSPDM_STATUS_SUCCESS;
285 0 : case 0x19:
286 0 : m_libspdm_local_buffer_size = 0;
287 0 : message_size = libspdm_test_get_key_exchange_request_size(
288 : spdm_context, (const uint8_t *)request + header_size,
289 : request_size - header_size);
290 0 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
291 : (const uint8_t *)request + header_size, message_size);
292 0 : m_libspdm_local_buffer_size += message_size;
293 0 : return LIBSPDM_STATUS_SUCCESS;
294 1 : case 0x1A:
295 1 : m_libspdm_local_buffer_size = 0;
296 1 : message_size = libspdm_test_get_key_exchange_request_size(
297 : spdm_context, (const uint8_t *)request + header_size,
298 : request_size - header_size);
299 1 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
300 : (const uint8_t *)request + header_size, message_size);
301 1 : m_libspdm_local_buffer_size += message_size;
302 1 : return LIBSPDM_STATUS_SUCCESS;
303 1 : case 0x1B:
304 1 : m_libspdm_local_buffer_size = 0;
305 1 : message_size = libspdm_test_get_key_exchange_request_size(
306 : spdm_context, (const uint8_t *)request + header_size,
307 : request_size - header_size);
308 1 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
309 : (const uint8_t *)request + header_size, message_size);
310 1 : m_libspdm_local_buffer_size += message_size;
311 1 : return LIBSPDM_STATUS_SUCCESS;
312 1 : case 0x1C:
313 1 : m_libspdm_local_buffer_size = 0;
314 1 : message_size = libspdm_test_get_key_exchange_request_size(
315 : spdm_context, (const uint8_t *)request + header_size,
316 : request_size - header_size);
317 1 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
318 : (const uint8_t *)request + header_size, message_size);
319 1 : m_libspdm_local_buffer_size += message_size;
320 1 : return LIBSPDM_STATUS_SUCCESS;
321 0 : case 0x1D:
322 0 : m_libspdm_local_buffer_size = 0;
323 0 : message_size = libspdm_test_get_key_exchange_request_size(
324 : spdm_context, (const uint8_t *)request + header_size,
325 : request_size - header_size);
326 0 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
327 : (const uint8_t *)request + header_size, message_size);
328 0 : m_libspdm_local_buffer_size += message_size;
329 0 : return LIBSPDM_STATUS_SUCCESS;
330 0 : case 0x1E:
331 0 : m_libspdm_local_buffer_size = 0;
332 0 : message_size = libspdm_test_get_key_exchange_request_size(
333 : spdm_context, (const uint8_t *)request + header_size,
334 : request_size - header_size);
335 0 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
336 : (const uint8_t *)request + header_size, message_size);
337 0 : m_libspdm_local_buffer_size += message_size;
338 0 : return LIBSPDM_STATUS_SUCCESS;
339 1 : case 0x1F:
340 1 : m_libspdm_local_buffer_size = 0;
341 1 : message_size = libspdm_test_get_key_exchange_request_size(
342 : spdm_context, (const uint8_t *)request + header_size,
343 : request_size - header_size);
344 1 : libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
345 : (const uint8_t *)request + header_size, message_size);
346 1 : m_libspdm_local_buffer_size += message_size;
347 1 : return LIBSPDM_STATUS_SUCCESS;
348 0 : default:
349 0 : return LIBSPDM_STATUS_SEND_FAIL;
350 : }
351 : }
352 :
353 39 : static libspdm_return_t receive_message(
354 : void *spdm_context, size_t *response_size, void **response, uint64_t timeout)
355 : {
356 : libspdm_test_context_t *spdm_test_context;
357 :
358 39 : spdm_test_context = libspdm_get_test_context();
359 39 : switch (spdm_test_context->case_id) {
360 0 : case 0x1:
361 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
362 :
363 1 : case 0x2: {
364 : spdm_key_exchange_response_t *spdm_response;
365 : size_t dhe_key_size;
366 : uint32_t hash_size;
367 : size_t signature_size;
368 : uint32_t hmac_size;
369 : uint8_t *ptr;
370 : void *dhe_context;
371 : uint8_t final_key[LIBSPDM_MAX_DHE_SS_SIZE];
372 : size_t final_key_size;
373 : size_t opaque_key_exchange_rsp_size;
374 : void *data;
375 : size_t data_size;
376 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
377 : uint8_t *cert_buffer;
378 : size_t cert_buffer_size;
379 : uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
380 : uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
381 : uint8_t bin_str0[128];
382 : size_t bin_str0_size;
383 : uint8_t bin_str2[128];
384 : size_t bin_str2_size;
385 : uint8_t bin_str7[128];
386 : size_t bin_str7_size;
387 : uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
388 : uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
389 : uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
390 : size_t spdm_response_size;
391 : size_t transport_header_size;
392 :
393 :
394 : ((libspdm_context_t *)spdm_context)
395 1 : ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
396 : ((libspdm_context_t *)spdm_context)
397 1 : ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
398 : ((libspdm_context_t *)spdm_context)
399 1 : ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
400 : ((libspdm_context_t *)spdm_context)
401 1 : ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
402 1 : signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
403 1 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
404 1 : hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
405 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
406 1 : opaque_key_exchange_rsp_size =
407 1 : libspdm_get_opaque_data_version_selection_data_size( spdm_context);
408 1 : spdm_response_size = sizeof(spdm_key_exchange_response_t) +
409 1 : dhe_key_size + 0 + sizeof(uint16_t) +
410 1 : opaque_key_exchange_rsp_size + signature_size +
411 : hmac_size;
412 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
413 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
414 :
415 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
416 1 : spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
417 1 : spdm_response->header.param1 = 0;
418 1 : spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
419 1 : spdm_response->mut_auth_requested = 0;
420 1 : spdm_response->req_slot_id_param = 0;
421 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
422 1 : ptr = (void *)(spdm_response + 1);
423 1 : dhe_context = libspdm_dhe_new(
424 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
425 : m_libspdm_use_dhe_algo,
426 : true);
427 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
428 1 : final_key_size = sizeof(final_key);
429 1 : libspdm_dhe_compute_key(
430 : m_libspdm_use_dhe_algo, dhe_context,
431 1 : (uint8_t *)&m_libspdm_local_buffer[0] +
432 : sizeof(spdm_key_exchange_request_t),
433 : dhe_key_size, final_key, &final_key_size);
434 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
435 1 : ptr += dhe_key_size;
436 : /* libspdm_zero_mem (ptr, hash_size);
437 : * ptr += hash_size;*/
438 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_rsp_size);
439 1 : ptr += sizeof(uint16_t);
440 1 : libspdm_build_opaque_data_version_selection_data(
441 : spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
442 : &opaque_key_exchange_rsp_size, ptr);
443 1 : ptr += opaque_key_exchange_rsp_size;
444 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
445 : m_libspdm_use_asym_algo, &data,
446 : &data_size, NULL, NULL)) {
447 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
448 : }
449 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
450 : sizeof(m_libspdm_local_buffer)
451 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
452 : m_libspdm_local_buffer),
453 1 : spdm_response, (size_t)ptr - (size_t)spdm_response);
454 1 : m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
455 1 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
456 : m_libspdm_local_buffer_size));
457 1 : libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
458 1 : libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
459 1 : cert_buffer = (uint8_t *)data;
460 1 : cert_buffer_size = data_size;
461 1 : libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size, cert_buffer_hash);
462 : /* transcript.message_a size is 0*/
463 1 : libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
464 1 : libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
465 : m_libspdm_local_buffer_size);
466 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
467 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
468 1 : free(data);
469 1 : libspdm_responder_data_sign(
470 : spdm_context,
471 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
472 : 0, SPDM_KEY_EXCHANGE_RSP,
473 : m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
474 1 : false, libspdm_get_managed_buffer(&th_curr),
475 : libspdm_get_managed_buffer_size(&th_curr), ptr,
476 : &signature_size);
477 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
478 : sizeof(m_libspdm_local_buffer)
479 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
480 : m_libspdm_local_buffer),
481 : ptr, signature_size);
482 1 : m_libspdm_local_buffer_size += signature_size;
483 1 : libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
484 1 : ptr += signature_size;
485 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
486 : libspdm_get_managed_buffer_size(&th_curr),
487 : th_curr_hash_data);
488 1 : bin_str0_size = sizeof(bin_str0);
489 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
490 : SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
491 1 : NULL, (uint16_t)hash_size, hash_size, bin_str0,
492 : &bin_str0_size);
493 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
494 : m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
495 1 : bin_str2_size = sizeof(bin_str2);
496 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
497 : SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
498 1 : th_curr_hash_data, (uint16_t)hash_size, hash_size,
499 : bin_str2, &bin_str2_size);
500 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
501 : bin_str2, bin_str2_size,
502 : response_handshake_secret, hash_size);
503 1 : bin_str7_size = sizeof(bin_str7);
504 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
505 : SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
506 1 : NULL, (uint16_t)hash_size, hash_size, bin_str7,
507 : &bin_str7_size);
508 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
509 : hash_size, bin_str7, bin_str7_size,
510 : response_finished_key, hash_size);
511 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
512 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
513 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
514 : response_finished_key, hash_size, ptr);
515 1 : ptr += hmac_size;
516 :
517 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
518 : false, spdm_response_size,
519 : spdm_response, response_size,
520 : response);
521 : }
522 1 : return LIBSPDM_STATUS_SUCCESS;
523 :
524 0 : case 0x3: {
525 : spdm_key_exchange_response_t *spdm_response;
526 : size_t dhe_key_size;
527 : uint32_t hash_size;
528 : size_t signature_size;
529 : uint32_t hmac_size;
530 : uint8_t *ptr;
531 : void *dhe_context;
532 : uint8_t final_key[LIBSPDM_MAX_DHE_SS_SIZE];
533 : size_t final_key_size;
534 : size_t opaque_key_exchange_rsp_size;
535 : void *data;
536 : size_t data_size;
537 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
538 : uint8_t *cert_buffer;
539 : size_t cert_buffer_size;
540 : uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
541 : uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
542 : uint8_t bin_str0[128];
543 : size_t bin_str0_size;
544 : uint8_t bin_str2[128];
545 : size_t bin_str2_size;
546 : uint8_t bin_str7[128];
547 : size_t bin_str7_size;
548 : uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
549 : uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
550 : uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
551 : size_t spdm_response_size;
552 : size_t transport_header_size;
553 :
554 : ((libspdm_context_t *)spdm_context)
555 0 : ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
556 : ((libspdm_context_t *)spdm_context)
557 0 : ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
558 : ((libspdm_context_t *)spdm_context)
559 0 : ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
560 : ((libspdm_context_t *)spdm_context)
561 0 : ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
562 0 : signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
563 0 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
564 0 : hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
565 0 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
566 0 : opaque_key_exchange_rsp_size =
567 0 : libspdm_get_opaque_data_version_selection_data_size( spdm_context);
568 0 : spdm_response_size = sizeof(spdm_key_exchange_response_t) +
569 0 : dhe_key_size + 0 + sizeof(uint16_t) +
570 0 : opaque_key_exchange_rsp_size + signature_size +
571 : hmac_size;
572 0 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
573 0 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
574 :
575 0 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
576 0 : spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
577 0 : spdm_response->header.param1 = 0;
578 0 : spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
579 0 : spdm_response->mut_auth_requested = 0;
580 0 : spdm_response->req_slot_id_param = 0;
581 0 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
582 0 : ptr = (void *)(spdm_response + 1);
583 0 : dhe_context = libspdm_dhe_new(
584 0 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
585 : m_libspdm_use_dhe_algo,
586 : true);
587 0 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
588 0 : final_key_size = sizeof(final_key);
589 0 : libspdm_dhe_compute_key(
590 : m_libspdm_use_dhe_algo, dhe_context,
591 0 : (uint8_t *)&m_libspdm_local_buffer[0] +
592 : sizeof(spdm_key_exchange_request_t),
593 : dhe_key_size, final_key, &final_key_size);
594 0 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
595 0 : ptr += dhe_key_size;
596 : /* libspdm_zero_mem (ptr, hash_size);
597 : * ptr += hash_size;*/
598 0 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_rsp_size);
599 0 : ptr += sizeof(uint16_t);
600 0 : libspdm_build_opaque_data_version_selection_data(
601 : spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
602 : &opaque_key_exchange_rsp_size, ptr);
603 0 : ptr += opaque_key_exchange_rsp_size;
604 0 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
605 : m_libspdm_use_asym_algo, &data,
606 : &data_size, NULL, NULL)) {
607 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
608 : }
609 0 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
610 : sizeof(m_libspdm_local_buffer)
611 0 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
612 : m_libspdm_local_buffer),
613 0 : spdm_response, (size_t)ptr - (size_t)spdm_response);
614 0 : m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
615 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
616 : m_libspdm_local_buffer_size));
617 0 : libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
618 0 : libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
619 0 : cert_buffer = (uint8_t *)data;
620 0 : cert_buffer_size = data_size;
621 0 : libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size, cert_buffer_hash);
622 : /* transcript.message_a size is 0*/
623 0 : libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
624 0 : libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
625 : m_libspdm_local_buffer_size);
626 0 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
627 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
628 0 : free(data);
629 0 : libspdm_responder_data_sign(
630 : spdm_context,
631 0 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
632 : 0, SPDM_KEY_EXCHANGE_RSP,
633 : m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
634 0 : false, libspdm_get_managed_buffer(&th_curr),
635 : libspdm_get_managed_buffer_size(&th_curr), ptr,
636 : &signature_size);
637 0 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
638 : sizeof(m_libspdm_local_buffer)
639 0 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
640 : m_libspdm_local_buffer),
641 : ptr, signature_size);
642 0 : m_libspdm_local_buffer_size += signature_size;
643 0 : libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
644 0 : ptr += signature_size;
645 0 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
646 : libspdm_get_managed_buffer_size(&th_curr),
647 : th_curr_hash_data);
648 0 : bin_str0_size = sizeof(bin_str0);
649 0 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
650 : SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
651 0 : NULL, (uint16_t)hash_size, hash_size, bin_str0,
652 : &bin_str0_size);
653 0 : libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
654 : m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
655 0 : bin_str2_size = sizeof(bin_str2);
656 0 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
657 : SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
658 0 : th_curr_hash_data, (uint16_t)hash_size, hash_size,
659 : bin_str2, &bin_str2_size);
660 0 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
661 : bin_str2, bin_str2_size,
662 : response_handshake_secret, hash_size);
663 0 : bin_str7_size = sizeof(bin_str7);
664 0 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
665 : SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
666 0 : NULL, (uint16_t)hash_size, hash_size, bin_str7,
667 : &bin_str7_size);
668 0 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
669 : hash_size, bin_str7, bin_str7_size,
670 : response_finished_key, hash_size);
671 0 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
672 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
673 0 : libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
674 : response_finished_key, hash_size, ptr);
675 0 : ptr += hmac_size;
676 :
677 0 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
678 : false, spdm_response_size,
679 : spdm_response, response_size,
680 : response);
681 : }
682 0 : return LIBSPDM_STATUS_SUCCESS;
683 :
684 1 : case 0x4: {
685 : spdm_error_response_t *spdm_response;
686 : size_t spdm_response_size;
687 : size_t transport_header_size;
688 :
689 1 : spdm_response_size = sizeof(spdm_error_response_t);
690 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
691 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
692 :
693 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
694 1 : spdm_response->header.request_response_code = SPDM_ERROR;
695 1 : spdm_response->header.param1 = SPDM_ERROR_CODE_INVALID_REQUEST;
696 1 : spdm_response->header.param2 = 0;
697 :
698 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
699 : false, spdm_response_size,
700 : spdm_response,
701 : response_size, response);
702 : }
703 1 : return LIBSPDM_STATUS_SUCCESS;
704 :
705 1 : case 0x5: {
706 : spdm_error_response_t *spdm_response;
707 : size_t spdm_response_size;
708 : size_t transport_header_size;
709 :
710 1 : spdm_response_size = sizeof(spdm_error_response_t);
711 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
712 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
713 :
714 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
715 1 : spdm_response->header.request_response_code = SPDM_ERROR;
716 1 : spdm_response->header.param1 = SPDM_ERROR_CODE_BUSY;
717 1 : spdm_response->header.param2 = 0;
718 :
719 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
720 : false, spdm_response_size,
721 : spdm_response,
722 : response_size, response);
723 : }
724 1 : return LIBSPDM_STATUS_SUCCESS;
725 :
726 0 : case 0x6: {
727 : static size_t sub_index1 = 0;
728 0 : if (sub_index1 == 0) {
729 : spdm_error_response_t *spdm_response;
730 : size_t spdm_response_size;
731 : size_t transport_header_size;
732 :
733 0 : spdm_response_size = sizeof(spdm_error_response_t);
734 0 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
735 0 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
736 :
737 0 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
738 0 : spdm_response->header.request_response_code = SPDM_ERROR;
739 0 : spdm_response->header.param1 = SPDM_ERROR_CODE_BUSY;
740 0 : spdm_response->header.param2 = 0;
741 :
742 0 : libspdm_transport_test_encode_message(
743 : spdm_context, NULL, false, false,
744 : spdm_response_size, spdm_response,
745 : response_size, response);
746 0 : sub_index1++;
747 0 : } else if (sub_index1 == 1) {
748 : spdm_key_exchange_response_t *spdm_response;
749 : size_t dhe_key_size;
750 : uint32_t hash_size;
751 : size_t signature_size;
752 : uint32_t hmac_size;
753 : uint8_t *ptr;
754 : void *dhe_context;
755 : uint8_t final_key[LIBSPDM_MAX_DHE_SS_SIZE];
756 : size_t final_key_size;
757 : size_t opaque_key_exchange_rsp_size;
758 : void *data;
759 : size_t data_size;
760 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
761 : uint8_t *cert_buffer;
762 : size_t cert_buffer_size;
763 : uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
764 : uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
765 : uint8_t bin_str0[128];
766 : size_t bin_str0_size;
767 : uint8_t bin_str2[128];
768 : size_t bin_str2_size;
769 : uint8_t bin_str7[128];
770 : size_t bin_str7_size;
771 : uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
772 : uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
773 : uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
774 : size_t spdm_response_size;
775 : size_t transport_header_size;
776 :
777 : ((libspdm_context_t *)spdm_context)
778 0 : ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
779 : ((libspdm_context_t *)spdm_context)
780 0 : ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
781 : ((libspdm_context_t *)spdm_context)
782 0 : ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
783 : ((libspdm_context_t *)spdm_context)
784 : ->connection_info.algorithm
785 0 : .measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
786 0 : signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
787 0 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
788 0 : hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
789 0 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
790 0 : opaque_key_exchange_rsp_size =
791 0 : libspdm_get_opaque_data_version_selection_data_size( spdm_context);
792 0 : spdm_response_size = sizeof(spdm_key_exchange_response_t) +
793 0 : dhe_key_size + 0 + sizeof(uint16_t) +
794 0 : opaque_key_exchange_rsp_size +
795 0 : signature_size + hmac_size;
796 0 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
797 0 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
798 :
799 0 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
800 0 : spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
801 0 : spdm_response->header.param1 = 0;
802 0 : spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
803 0 : spdm_response->mut_auth_requested = 0;
804 0 : spdm_response->req_slot_id_param = 0;
805 0 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
806 0 : ptr = (void *)(spdm_response + 1);
807 0 : dhe_context = libspdm_dhe_new(
808 0 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
809 : m_libspdm_use_dhe_algo, true);
810 0 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
811 0 : final_key_size = sizeof(final_key);
812 0 : libspdm_dhe_compute_key(
813 : m_libspdm_use_dhe_algo, dhe_context,
814 0 : (uint8_t *)&m_libspdm_local_buffer[0] +
815 : sizeof(spdm_key_exchange_request_t),
816 : dhe_key_size, final_key, &final_key_size);
817 0 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
818 0 : ptr += dhe_key_size;
819 : /* libspdm_zero_mem (ptr, hash_size);
820 : * ptr += hash_size;*/
821 0 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_rsp_size);
822 0 : ptr += sizeof(uint16_t);
823 0 : libspdm_build_opaque_data_version_selection_data(
824 : spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
825 : &opaque_key_exchange_rsp_size, ptr);
826 0 : ptr += opaque_key_exchange_rsp_size;
827 0 : if (!libspdm_read_responder_public_certificate_chain(
828 : m_libspdm_use_hash_algo, m_libspdm_use_asym_algo, &data,
829 : &data_size, NULL, NULL)) {
830 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
831 : }
832 0 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
833 : sizeof(m_libspdm_local_buffer)
834 0 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
835 : m_libspdm_local_buffer),
836 0 : spdm_response, (size_t)ptr - (size_t)spdm_response);
837 0 : m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
838 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
839 : m_libspdm_local_buffer_size));
840 0 : libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
841 0 : libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
842 0 : cert_buffer = (uint8_t *)data;
843 0 : cert_buffer_size = data_size;
844 0 : libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer,
845 : cert_buffer_size, cert_buffer_hash);
846 : /* transcript.message_a size is 0*/
847 0 : libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
848 0 : libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
849 : m_libspdm_local_buffer_size);
850 0 : libspdm_hash_all(m_libspdm_use_hash_algo,
851 0 : libspdm_get_managed_buffer(&th_curr),
852 : libspdm_get_managed_buffer_size(&th_curr),
853 : hash_data);
854 0 : free(data);
855 0 : libspdm_responder_data_sign(
856 : spdm_context,
857 0 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
858 : 0, SPDM_KEY_EXCHANGE_RSP,
859 : m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
860 0 : false, libspdm_get_managed_buffer(&th_curr),
861 : libspdm_get_managed_buffer_size(&th_curr), ptr,
862 : &signature_size);
863 0 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
864 : sizeof(m_libspdm_local_buffer)
865 0 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
866 : m_libspdm_local_buffer),
867 : ptr, signature_size);
868 0 : m_libspdm_local_buffer_size += signature_size;
869 0 : libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
870 0 : ptr += signature_size;
871 0 : libspdm_hash_all(m_libspdm_use_hash_algo,
872 0 : libspdm_get_managed_buffer(&th_curr),
873 : libspdm_get_managed_buffer_size(&th_curr),
874 : th_curr_hash_data);
875 0 : bin_str0_size = sizeof(bin_str0);
876 0 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
877 : SPDM_BIN_STR_0_LABEL,
878 : sizeof(SPDM_BIN_STR_0_LABEL) - 1, NULL,
879 0 : (uint16_t)hash_size, hash_size, bin_str0,
880 : &bin_str0_size);
881 0 : libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
882 : m_libspdm_zero_filled_buffer, hash_size, handshake_secret);
883 0 : bin_str2_size = sizeof(bin_str2);
884 0 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
885 : SPDM_BIN_STR_2_LABEL,
886 : sizeof(SPDM_BIN_STR_2_LABEL) - 1,
887 0 : th_curr_hash_data, (uint16_t)hash_size,
888 : hash_size, bin_str2, &bin_str2_size);
889 0 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret,
890 : hash_size, bin_str2, bin_str2_size,
891 : response_handshake_secret, hash_size);
892 0 : bin_str7_size = sizeof(bin_str7);
893 0 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
894 : SPDM_BIN_STR_7_LABEL,
895 : sizeof(SPDM_BIN_STR_7_LABEL) - 1, NULL,
896 0 : (uint16_t)hash_size, hash_size, bin_str7,
897 : &bin_str7_size);
898 0 : libspdm_hkdf_expand(m_libspdm_use_hash_algo,
899 : response_handshake_secret, hash_size,
900 : bin_str7, bin_str7_size,
901 : response_finished_key, hash_size);
902 0 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
903 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
904 0 : libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
905 : response_finished_key, hash_size, ptr);
906 0 : ptr += hmac_size;
907 :
908 0 : libspdm_transport_test_encode_message(
909 : spdm_context, NULL, false, false, spdm_response_size,
910 : spdm_response, response_size, response);
911 : }
912 : }
913 0 : return LIBSPDM_STATUS_SUCCESS;
914 :
915 1 : case 0x7: {
916 : spdm_error_response_t *spdm_response;
917 : size_t spdm_response_size;
918 : size_t transport_header_size;
919 :
920 1 : spdm_response_size = sizeof(spdm_error_response_t);
921 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
922 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
923 :
924 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
925 1 : spdm_response->header.request_response_code = SPDM_ERROR;
926 1 : spdm_response->header.param1 = SPDM_ERROR_CODE_REQUEST_RESYNCH;
927 1 : spdm_response->header.param2 = 0;
928 :
929 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
930 : false, spdm_response_size,
931 : spdm_response,
932 : response_size, response);
933 : }
934 1 : return LIBSPDM_STATUS_SUCCESS;
935 :
936 2 : case 0x8: {
937 : spdm_error_response_data_response_not_ready_t *spdm_response;
938 : size_t spdm_response_size;
939 : size_t transport_header_size;
940 :
941 2 : spdm_response_size = sizeof(spdm_error_response_data_response_not_ready_t);
942 2 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
943 2 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
944 :
945 2 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
946 2 : spdm_response->header.request_response_code = SPDM_ERROR;
947 2 : spdm_response->header.param1 = SPDM_ERROR_CODE_RESPONSE_NOT_READY;
948 2 : spdm_response->header.param2 = 0;
949 2 : spdm_response->extend_error_data.rd_exponent = 1;
950 2 : spdm_response->extend_error_data.rd_tm = 2;
951 2 : spdm_response->extend_error_data.request_code = SPDM_KEY_EXCHANGE;
952 2 : spdm_response->extend_error_data.token = 0;
953 :
954 2 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
955 : false, spdm_response_size,
956 : spdm_response,
957 : response_size, response);
958 : }
959 2 : return LIBSPDM_STATUS_SUCCESS;
960 :
961 0 : case 0x9: {
962 : static size_t sub_index2 = 0;
963 0 : if (sub_index2 == 0) {
964 : spdm_error_response_data_response_not_ready_t
965 : *spdm_response;
966 : size_t spdm_response_size;
967 : size_t transport_header_size;
968 :
969 0 : spdm_response_size = sizeof(spdm_error_response_data_response_not_ready_t);
970 0 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
971 0 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
972 :
973 0 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
974 0 : spdm_response->header.request_response_code = SPDM_ERROR;
975 0 : spdm_response->header.param1 = SPDM_ERROR_CODE_RESPONSE_NOT_READY;
976 0 : spdm_response->header.param2 = 0;
977 0 : spdm_response->extend_error_data.rd_exponent = 1;
978 0 : spdm_response->extend_error_data.rd_tm = 2;
979 0 : spdm_response->extend_error_data.request_code = SPDM_KEY_EXCHANGE;
980 0 : spdm_response->extend_error_data.token = 1;
981 :
982 0 : libspdm_transport_test_encode_message(
983 : spdm_context, NULL, false, false,
984 : spdm_response_size, spdm_response,
985 : response_size, response);
986 0 : sub_index2++;
987 0 : } else if (sub_index2 == 1) {
988 : spdm_key_exchange_response_t *spdm_response;
989 : size_t dhe_key_size;
990 : uint32_t hash_size;
991 : size_t signature_size;
992 : uint32_t hmac_size;
993 : uint8_t *ptr;
994 : void *dhe_context;
995 : uint8_t final_key[LIBSPDM_MAX_DHE_SS_SIZE];
996 : size_t final_key_size;
997 : size_t opaque_key_exchange_rsp_size;
998 : void *data;
999 : size_t data_size;
1000 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
1001 : uint8_t *cert_buffer;
1002 : size_t cert_buffer_size;
1003 : uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
1004 : uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
1005 : uint8_t bin_str0[128];
1006 : size_t bin_str0_size;
1007 : uint8_t bin_str2[128];
1008 : size_t bin_str2_size;
1009 : uint8_t bin_str7[128];
1010 : size_t bin_str7_size;
1011 : uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
1012 : uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
1013 : uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
1014 : size_t spdm_response_size;
1015 : size_t transport_header_size;
1016 :
1017 : ((libspdm_context_t *)spdm_context)
1018 0 : ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
1019 : ((libspdm_context_t *)spdm_context)
1020 0 : ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1021 : ((libspdm_context_t *)spdm_context)
1022 0 : ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
1023 : ((libspdm_context_t *)spdm_context)
1024 : ->connection_info.algorithm
1025 0 : .measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
1026 0 : signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
1027 0 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
1028 0 : hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
1029 0 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
1030 0 : opaque_key_exchange_rsp_size =
1031 0 : libspdm_get_opaque_data_version_selection_data_size( spdm_context);
1032 0 : spdm_response_size = sizeof(spdm_key_exchange_response_t) +
1033 0 : dhe_key_size + 0 + sizeof(uint16_t) +
1034 0 : opaque_key_exchange_rsp_size +
1035 0 : signature_size + hmac_size;
1036 0 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
1037 0 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
1038 :
1039 0 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
1040 0 : spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
1041 0 : spdm_response->header.param1 = 0;
1042 0 : spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
1043 0 : spdm_response->mut_auth_requested = 0;
1044 0 : spdm_response->req_slot_id_param = 0;
1045 0 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
1046 0 : ptr = (void *)(spdm_response + 1);
1047 0 : dhe_context = libspdm_dhe_new(
1048 0 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
1049 : m_libspdm_use_dhe_algo, true);
1050 0 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
1051 0 : final_key_size = sizeof(final_key);
1052 0 : libspdm_dhe_compute_key(
1053 : m_libspdm_use_dhe_algo, dhe_context,
1054 0 : (uint8_t *)&m_libspdm_local_buffer[0] +
1055 : sizeof(spdm_key_exchange_request_t),
1056 : dhe_key_size, final_key, &final_key_size);
1057 0 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
1058 0 : ptr += dhe_key_size;
1059 : /* libspdm_zero_mem (ptr, hash_size);
1060 : * ptr += hash_size;*/
1061 0 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_rsp_size);
1062 0 : ptr += sizeof(uint16_t);
1063 0 : libspdm_build_opaque_data_version_selection_data(
1064 : spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
1065 : &opaque_key_exchange_rsp_size, ptr);
1066 0 : ptr += opaque_key_exchange_rsp_size;
1067 0 : if (!libspdm_read_responder_public_certificate_chain(
1068 : m_libspdm_use_hash_algo, m_libspdm_use_asym_algo, &data,
1069 : &data_size, NULL, NULL)) {
1070 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
1071 : }
1072 0 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
1073 : sizeof(m_libspdm_local_buffer)
1074 0 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
1075 : m_libspdm_local_buffer),
1076 0 : spdm_response, (size_t)ptr - (size_t)spdm_response);
1077 0 : m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
1078 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
1079 : m_libspdm_local_buffer_size));
1080 0 : libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
1081 0 : libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
1082 0 : cert_buffer = (uint8_t *)data;
1083 0 : cert_buffer_size = data_size;
1084 0 : libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer,
1085 : cert_buffer_size, cert_buffer_hash);
1086 : /* transcript.message_a size is 0*/
1087 0 : libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
1088 0 : libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
1089 : m_libspdm_local_buffer_size);
1090 0 : libspdm_hash_all(m_libspdm_use_hash_algo,
1091 0 : libspdm_get_managed_buffer(&th_curr),
1092 : libspdm_get_managed_buffer_size(&th_curr),
1093 : hash_data);
1094 0 : free(data);
1095 0 : libspdm_responder_data_sign(
1096 : spdm_context,
1097 0 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
1098 : 0, SPDM_KEY_EXCHANGE_RSP,
1099 : m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
1100 0 : false, libspdm_get_managed_buffer(&th_curr),
1101 : libspdm_get_managed_buffer_size(&th_curr), ptr,
1102 : &signature_size);
1103 0 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
1104 : sizeof(m_libspdm_local_buffer)
1105 0 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
1106 : m_libspdm_local_buffer),
1107 : ptr, signature_size);
1108 0 : m_libspdm_local_buffer_size += signature_size;
1109 0 : libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
1110 0 : ptr += signature_size;
1111 0 : libspdm_hash_all(m_libspdm_use_hash_algo,
1112 0 : libspdm_get_managed_buffer(&th_curr),
1113 : libspdm_get_managed_buffer_size(&th_curr),
1114 : th_curr_hash_data);
1115 0 : bin_str0_size = sizeof(bin_str0);
1116 0 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
1117 : SPDM_BIN_STR_0_LABEL,
1118 : sizeof(SPDM_BIN_STR_0_LABEL) - 1, NULL,
1119 0 : (uint16_t)hash_size, hash_size, bin_str0,
1120 : &bin_str0_size);
1121 0 : libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
1122 : m_libspdm_zero_filled_buffer, hash_size, handshake_secret);
1123 0 : bin_str2_size = sizeof(bin_str2);
1124 0 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
1125 : SPDM_BIN_STR_2_LABEL,
1126 : sizeof(SPDM_BIN_STR_2_LABEL) - 1,
1127 0 : th_curr_hash_data, (uint16_t)hash_size,
1128 : hash_size, bin_str2, &bin_str2_size);
1129 0 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret,
1130 : hash_size, bin_str2, bin_str2_size,
1131 : response_handshake_secret, hash_size);
1132 0 : bin_str7_size = sizeof(bin_str7);
1133 0 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
1134 : SPDM_BIN_STR_7_LABEL,
1135 : sizeof(SPDM_BIN_STR_7_LABEL) - 1, NULL,
1136 0 : (uint16_t)hash_size, hash_size, bin_str7,
1137 : &bin_str7_size);
1138 0 : libspdm_hkdf_expand(m_libspdm_use_hash_algo,
1139 : response_handshake_secret, hash_size,
1140 : bin_str7, bin_str7_size,
1141 : response_finished_key, hash_size);
1142 0 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
1143 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
1144 0 : libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
1145 : response_finished_key, hash_size, ptr);
1146 0 : ptr += hmac_size;
1147 :
1148 0 : libspdm_transport_test_encode_message(
1149 : spdm_context, NULL, false, false, spdm_response_size,
1150 : spdm_response, response_size, response);
1151 : }
1152 : }
1153 0 : return LIBSPDM_STATUS_SUCCESS;
1154 :
1155 18 : case 0xA:
1156 : {
1157 : static uint16_t error_code = LIBSPDM_ERROR_CODE_RESERVED_00;
1158 :
1159 : spdm_error_response_t *spdm_response;
1160 : size_t spdm_response_size;
1161 : size_t transport_header_size;
1162 :
1163 18 : spdm_response_size = sizeof(spdm_error_response_t);
1164 18 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
1165 18 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
1166 :
1167 18 : if(error_code <= 0xff) {
1168 18 : libspdm_zero_mem (spdm_response, spdm_response_size);
1169 18 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
1170 18 : spdm_response->header.request_response_code = SPDM_ERROR;
1171 18 : spdm_response->header.param1 = (uint8_t) error_code;
1172 18 : spdm_response->header.param2 = 0;
1173 :
1174 18 : libspdm_transport_test_encode_message (spdm_context, NULL, false, false,
1175 : spdm_response_size, spdm_response,
1176 : response_size, response);
1177 : }
1178 :
1179 18 : error_code++;
1180 18 : if(error_code == SPDM_ERROR_CODE_BUSY) { /*busy is treated in cases 5 and 6*/
1181 1 : error_code = SPDM_ERROR_CODE_UNEXPECTED_REQUEST;
1182 : }
1183 : /* skip some reserved error codes (0d to 3e) */
1184 18 : if(error_code == LIBSPDM_ERROR_CODE_RESERVED_0D) {
1185 1 : error_code = LIBSPDM_ERROR_CODE_RESERVED_3F;
1186 : }
1187 : /* skip response not ready, request resync, and some reserved codes (44 to fc) */
1188 18 : if(error_code == SPDM_ERROR_CODE_RESPONSE_NOT_READY) {
1189 1 : error_code = LIBSPDM_ERROR_CODE_RESERVED_FD;
1190 : }
1191 : }
1192 18 : return LIBSPDM_STATUS_SUCCESS;
1193 0 : case 0xB: {
1194 : spdm_key_exchange_response_t *spdm_response;
1195 : size_t dhe_key_size;
1196 : uint32_t hash_size;
1197 : size_t signature_size;
1198 : uint32_t hmac_size;
1199 : uint8_t *ptr;
1200 : void *dhe_context;
1201 : uint8_t final_key[LIBSPDM_MAX_DHE_SS_SIZE];
1202 : size_t final_key_size;
1203 : size_t opaque_key_exchange_rsp_size;
1204 : void *data;
1205 : size_t data_size;
1206 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
1207 : uint8_t *cert_buffer;
1208 : size_t cert_buffer_size;
1209 : uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
1210 : uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
1211 : uint8_t bin_str0[128];
1212 : size_t bin_str0_size;
1213 : uint8_t bin_str2[128];
1214 : size_t bin_str2_size;
1215 : uint8_t bin_str7[128];
1216 : size_t bin_str7_size;
1217 : uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
1218 : uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
1219 : uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
1220 : size_t spdm_response_size;
1221 : size_t transport_header_size;
1222 :
1223 : ((libspdm_context_t *)spdm_context)
1224 0 : ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
1225 : ((libspdm_context_t *)spdm_context)
1226 0 : ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1227 : ((libspdm_context_t *)spdm_context)
1228 0 : ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
1229 : ((libspdm_context_t *)spdm_context)
1230 0 : ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
1231 0 : signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
1232 0 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
1233 0 : hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
1234 0 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
1235 0 : opaque_key_exchange_rsp_size =
1236 0 : libspdm_get_opaque_data_version_selection_data_size( spdm_context);
1237 0 : spdm_response_size = sizeof(spdm_key_exchange_response_t) +
1238 0 : dhe_key_size + 0 + sizeof(uint16_t) +
1239 0 : opaque_key_exchange_rsp_size + signature_size +
1240 : hmac_size;
1241 0 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
1242 0 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
1243 :
1244 0 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
1245 0 : spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
1246 0 : spdm_response->header.param1 = 0;
1247 0 : spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
1248 0 : spdm_response->mut_auth_requested = 0;
1249 0 : spdm_response->req_slot_id_param = 0;
1250 0 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
1251 0 : ptr = (void *)(spdm_response + 1);
1252 0 : dhe_context = libspdm_dhe_new(
1253 0 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
1254 : m_libspdm_use_dhe_algo,
1255 : true);
1256 0 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
1257 0 : final_key_size = sizeof(final_key);
1258 0 : libspdm_dhe_compute_key(
1259 : m_libspdm_use_dhe_algo, dhe_context,
1260 0 : (uint8_t *)&m_libspdm_local_buffer[0] +
1261 : sizeof(spdm_key_exchange_request_t),
1262 : dhe_key_size, final_key, &final_key_size);
1263 0 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
1264 0 : ptr += dhe_key_size;
1265 : /* libspdm_zero_mem (ptr, hash_size);
1266 : * ptr += hash_size;*/
1267 0 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_rsp_size);
1268 0 : ptr += sizeof(uint16_t);
1269 0 : libspdm_build_opaque_data_version_selection_data(
1270 : spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
1271 : &opaque_key_exchange_rsp_size, ptr);
1272 0 : ptr += opaque_key_exchange_rsp_size;
1273 0 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
1274 : m_libspdm_use_asym_algo, &data,
1275 : &data_size, NULL, NULL)) {
1276 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
1277 : }
1278 0 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
1279 : sizeof(m_libspdm_local_buffer)
1280 0 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
1281 : m_libspdm_local_buffer),
1282 0 : spdm_response, (size_t)ptr - (size_t)spdm_response);
1283 0 : m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
1284 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
1285 : m_libspdm_local_buffer_size));
1286 0 : libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
1287 0 : libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
1288 0 : cert_buffer = (uint8_t *)data;
1289 0 : cert_buffer_size = data_size;
1290 0 : libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size, cert_buffer_hash);
1291 : /* transcript.message_a size is 0*/
1292 0 : libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
1293 0 : libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
1294 : m_libspdm_local_buffer_size);
1295 0 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
1296 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
1297 0 : free(data);
1298 0 : libspdm_responder_data_sign(
1299 : spdm_context,
1300 0 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
1301 : 0, SPDM_KEY_EXCHANGE_RSP,
1302 : m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
1303 0 : false, libspdm_get_managed_buffer(&th_curr),
1304 : libspdm_get_managed_buffer_size(&th_curr), ptr,
1305 : &signature_size);
1306 0 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
1307 : sizeof(m_libspdm_local_buffer)
1308 0 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
1309 : m_libspdm_local_buffer),
1310 : ptr, signature_size);
1311 0 : m_libspdm_local_buffer_size += signature_size;
1312 0 : libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
1313 0 : ptr += signature_size;
1314 0 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
1315 : libspdm_get_managed_buffer_size(&th_curr),
1316 : th_curr_hash_data);
1317 0 : bin_str0_size = sizeof(bin_str0);
1318 0 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
1319 : SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
1320 0 : NULL, (uint16_t)hash_size, hash_size, bin_str0,
1321 : &bin_str0_size);
1322 0 : libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
1323 : m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
1324 0 : bin_str2_size = sizeof(bin_str2);
1325 0 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
1326 : SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
1327 0 : th_curr_hash_data, (uint16_t)hash_size, hash_size,
1328 : bin_str2, &bin_str2_size);
1329 0 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
1330 : bin_str2, bin_str2_size,
1331 : response_handshake_secret, hash_size);
1332 0 : bin_str7_size = sizeof(bin_str7);
1333 0 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
1334 : SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
1335 0 : NULL, (uint16_t)hash_size, hash_size, bin_str7,
1336 : &bin_str7_size);
1337 0 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
1338 : hash_size, bin_str7, bin_str7_size,
1339 : response_finished_key, hash_size);
1340 0 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
1341 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
1342 0 : libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
1343 : response_finished_key, hash_size, ptr);
1344 0 : ptr += hmac_size;
1345 :
1346 0 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
1347 : false, spdm_response_size,
1348 : spdm_response, response_size,
1349 : response);
1350 : }
1351 0 : return LIBSPDM_STATUS_SUCCESS;
1352 1 : case 0xC: {
1353 : spdm_key_exchange_response_t *spdm_response;
1354 : size_t dhe_key_size;
1355 : uint32_t hash_size;
1356 : uint32_t measurement_hash_size;
1357 : size_t signature_size;
1358 : uint32_t hmac_size;
1359 : uint8_t *ptr;
1360 : void *dhe_context;
1361 : uint8_t final_key[LIBSPDM_MAX_DHE_SS_SIZE];
1362 : size_t final_key_size;
1363 : size_t opaque_key_exchange_rsp_size;
1364 : void *data;
1365 : size_t data_size;
1366 : uint8_t measurement_hash_data[LIBSPDM_MAX_HASH_SIZE];
1367 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
1368 : uint8_t *cert_buffer;
1369 : size_t cert_buffer_size;
1370 : uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
1371 : uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
1372 : uint8_t bin_str0[128];
1373 : size_t bin_str0_size;
1374 : uint8_t bin_str2[128];
1375 : size_t bin_str2_size;
1376 : uint8_t bin_str7[128];
1377 : size_t bin_str7_size;
1378 : uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
1379 : uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
1380 : uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
1381 : size_t spdm_response_size;
1382 : size_t transport_header_size;
1383 :
1384 : ((libspdm_context_t *)spdm_context)
1385 1 : ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
1386 : ((libspdm_context_t *)spdm_context)
1387 1 : ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1388 : ((libspdm_context_t *)spdm_context)
1389 1 : ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
1390 : ((libspdm_context_t *)spdm_context)
1391 1 : ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
1392 1 : signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
1393 1 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
1394 1 : measurement_hash_size = libspdm_get_hash_size( m_libspdm_use_hash_algo);
1395 1 : hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
1396 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
1397 1 : opaque_key_exchange_rsp_size =
1398 1 : libspdm_get_opaque_data_version_selection_data_size( spdm_context);
1399 1 : spdm_response_size = sizeof(spdm_key_exchange_response_t) +
1400 1 : dhe_key_size + measurement_hash_size + sizeof(uint16_t) +
1401 1 : opaque_key_exchange_rsp_size + signature_size +
1402 : hmac_size;
1403 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
1404 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
1405 1 : libspdm_zero_mem(spdm_response,spdm_response_size);
1406 :
1407 : /* Incorrect version. Should be 1.1. */
1408 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_10;
1409 1 : spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
1410 1 : spdm_response->header.param1 = 0;
1411 1 : spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
1412 1 : spdm_response->mut_auth_requested = 0;
1413 1 : spdm_response->req_slot_id_param = 0;
1414 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
1415 1 : ptr = (void *)(spdm_response + 1);
1416 1 : dhe_context = libspdm_dhe_new(
1417 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
1418 : m_libspdm_use_dhe_algo,
1419 : true);
1420 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
1421 1 : final_key_size = sizeof(final_key);
1422 1 : libspdm_dhe_compute_key(
1423 : m_libspdm_use_dhe_algo, dhe_context,
1424 1 : (uint8_t *)&m_libspdm_local_buffer[0] +
1425 : sizeof(spdm_key_exchange_request_t),
1426 : dhe_key_size, final_key, &final_key_size);
1427 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
1428 1 : ptr += dhe_key_size;
1429 : /*Mock measurement hash as TCB*/
1430 1 : libspdm_copy_mem(measurement_hash_data, sizeof(measurement_hash_data),
1431 : m_libspdm_use_tcb_hash_value, measurement_hash_size);
1432 1 : libspdm_copy_mem(ptr, spdm_response_size - (ptr - (uint8_t *)spdm_response),
1433 : measurement_hash_data, measurement_hash_size);
1434 : /*libspdm_zero_mem (ptr, measurement_hash_size);*/
1435 1 : ptr += measurement_hash_size;
1436 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_rsp_size);
1437 1 : ptr += sizeof(uint16_t);
1438 1 : libspdm_build_opaque_data_version_selection_data(
1439 : spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
1440 : &opaque_key_exchange_rsp_size, ptr);
1441 1 : ptr += opaque_key_exchange_rsp_size;
1442 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
1443 : m_libspdm_use_asym_algo, &data,
1444 : &data_size, NULL, NULL)) {
1445 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
1446 : }
1447 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
1448 : sizeof(m_libspdm_local_buffer)
1449 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
1450 : m_libspdm_local_buffer),
1451 1 : spdm_response, (size_t)ptr - (size_t)spdm_response);
1452 1 : m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
1453 1 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
1454 : m_libspdm_local_buffer_size));
1455 1 : libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
1456 1 : libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
1457 1 : cert_buffer = (uint8_t *)data;
1458 1 : cert_buffer_size = data_size;
1459 1 : libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size, cert_buffer_hash);
1460 : /* transcript.message_a size is 0*/
1461 1 : libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
1462 1 : libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
1463 : m_libspdm_local_buffer_size);
1464 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
1465 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
1466 1 : free(data);
1467 1 : libspdm_responder_data_sign(
1468 : spdm_context,
1469 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
1470 : 0, SPDM_KEY_EXCHANGE_RSP,
1471 : m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
1472 1 : false, libspdm_get_managed_buffer(&th_curr),
1473 : libspdm_get_managed_buffer_size(&th_curr), ptr,
1474 : &signature_size);
1475 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
1476 : sizeof(m_libspdm_local_buffer)
1477 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
1478 : m_libspdm_local_buffer),
1479 : ptr, signature_size);
1480 1 : m_libspdm_local_buffer_size += signature_size;
1481 1 : libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
1482 1 : ptr += signature_size;
1483 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
1484 : libspdm_get_managed_buffer_size(&th_curr),
1485 : th_curr_hash_data);
1486 1 : bin_str0_size = sizeof(bin_str0);
1487 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
1488 : SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
1489 1 : NULL, (uint16_t)hash_size, hash_size, bin_str0,
1490 : &bin_str0_size);
1491 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
1492 : m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
1493 1 : bin_str2_size = sizeof(bin_str2);
1494 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
1495 : SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
1496 1 : th_curr_hash_data, (uint16_t)hash_size, hash_size,
1497 : bin_str2, &bin_str2_size);
1498 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
1499 : bin_str2, bin_str2_size,
1500 : response_handshake_secret, hash_size);
1501 1 : bin_str7_size = sizeof(bin_str7);
1502 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
1503 : SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
1504 1 : NULL, (uint16_t)hash_size, hash_size, bin_str7,
1505 : &bin_str7_size);
1506 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
1507 : hash_size, bin_str7, bin_str7_size,
1508 : response_finished_key, hash_size);
1509 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
1510 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
1511 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
1512 : response_finished_key, hash_size, ptr);
1513 1 : ptr += hmac_size;
1514 :
1515 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
1516 : false, spdm_response_size,
1517 : spdm_response, response_size,
1518 : response);
1519 : }
1520 1 : return LIBSPDM_STATUS_SUCCESS;
1521 :
1522 1 : case 0xD: {
1523 : spdm_key_exchange_response_t *spdm_response;
1524 : size_t dhe_key_size;
1525 : uint32_t hash_size;
1526 : uint32_t measurement_hash_size;
1527 : size_t signature_size;
1528 : uint32_t hmac_size;
1529 : uint8_t *ptr;
1530 : void *dhe_context;
1531 : uint8_t final_key[LIBSPDM_MAX_DHE_SS_SIZE];
1532 : size_t final_key_size;
1533 : size_t opaque_key_exchange_rsp_size;
1534 : void *data;
1535 : size_t data_size;
1536 : uint8_t measurement_hash_data[LIBSPDM_MAX_HASH_SIZE];
1537 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
1538 : uint8_t *cert_buffer;
1539 : size_t cert_buffer_size;
1540 : uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
1541 : uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
1542 : uint8_t bin_str0[128];
1543 : size_t bin_str0_size;
1544 : uint8_t bin_str2[128];
1545 : size_t bin_str2_size;
1546 : uint8_t bin_str7[128];
1547 : size_t bin_str7_size;
1548 : uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
1549 : uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
1550 : uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
1551 : size_t spdm_response_size;
1552 : size_t transport_header_size;
1553 :
1554 : ((libspdm_context_t *)spdm_context)
1555 1 : ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
1556 : ((libspdm_context_t *)spdm_context)
1557 1 : ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1558 : ((libspdm_context_t *)spdm_context)
1559 1 : ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
1560 : ((libspdm_context_t *)spdm_context)
1561 1 : ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
1562 1 : signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
1563 1 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
1564 1 : measurement_hash_size = libspdm_get_hash_size( m_libspdm_use_hash_algo);
1565 1 : hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
1566 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
1567 1 : opaque_key_exchange_rsp_size =
1568 1 : libspdm_get_opaque_data_version_selection_data_size( spdm_context);
1569 1 : spdm_response_size = sizeof(spdm_key_exchange_response_t) +
1570 1 : dhe_key_size + measurement_hash_size + sizeof(uint16_t) +
1571 1 : opaque_key_exchange_rsp_size + signature_size +
1572 : hmac_size;
1573 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
1574 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
1575 1 : libspdm_zero_mem(spdm_response,spdm_response_size);
1576 :
1577 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
1578 : /* Incorrect response code. */
1579 1 : spdm_response->header.request_response_code = SPDM_DIGESTS;
1580 1 : spdm_response->header.param1 = 0;
1581 1 : spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
1582 1 : spdm_response->mut_auth_requested = 0;
1583 1 : spdm_response->req_slot_id_param = 0;
1584 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
1585 1 : ptr = (void *)(spdm_response + 1);
1586 1 : dhe_context = libspdm_dhe_new(
1587 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
1588 : m_libspdm_use_dhe_algo, true);
1589 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
1590 1 : final_key_size = sizeof(final_key);
1591 1 : libspdm_dhe_compute_key(
1592 : m_libspdm_use_dhe_algo, dhe_context,
1593 1 : (uint8_t *)&m_libspdm_local_buffer[0] +
1594 : sizeof(spdm_key_exchange_request_t),
1595 : dhe_key_size, final_key, &final_key_size);
1596 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
1597 1 : ptr += dhe_key_size;
1598 : /*Mock measurement hash as 0x00 array*/
1599 1 : libspdm_zero_mem(measurement_hash_data, measurement_hash_size);
1600 1 : libspdm_copy_mem(ptr, spdm_response_size - (ptr - (uint8_t *)spdm_response),
1601 : measurement_hash_data, measurement_hash_size);
1602 : /*libspdm_zero_mem (ptr, measurement_hash_size);*/
1603 1 : ptr += measurement_hash_size;
1604 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_rsp_size);
1605 1 : ptr += sizeof(uint16_t);
1606 1 : libspdm_build_opaque_data_version_selection_data(
1607 : spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
1608 : &opaque_key_exchange_rsp_size, ptr);
1609 1 : ptr += opaque_key_exchange_rsp_size;
1610 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
1611 : m_libspdm_use_asym_algo, &data,
1612 : &data_size, NULL, NULL)) {
1613 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
1614 : }
1615 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
1616 : sizeof(m_libspdm_local_buffer)
1617 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
1618 : m_libspdm_local_buffer),
1619 1 : spdm_response, (size_t)ptr - (size_t)spdm_response);
1620 1 : m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
1621 1 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
1622 : m_libspdm_local_buffer_size));
1623 1 : libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
1624 1 : libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
1625 1 : cert_buffer = (uint8_t *)data;
1626 1 : cert_buffer_size = data_size;
1627 1 : libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size, cert_buffer_hash);
1628 : /* transcript.message_a size is 0*/
1629 1 : libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
1630 1 : libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
1631 : m_libspdm_local_buffer_size);
1632 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
1633 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
1634 1 : free(data);
1635 1 : libspdm_responder_data_sign(
1636 : spdm_context,
1637 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
1638 : 0, SPDM_KEY_EXCHANGE_RSP,
1639 : m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
1640 1 : false, libspdm_get_managed_buffer(&th_curr),
1641 : libspdm_get_managed_buffer_size(&th_curr), ptr,
1642 : &signature_size);
1643 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
1644 : sizeof(m_libspdm_local_buffer)
1645 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
1646 : m_libspdm_local_buffer),
1647 : ptr, signature_size);
1648 1 : m_libspdm_local_buffer_size += signature_size;
1649 1 : libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
1650 1 : ptr += signature_size;
1651 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
1652 : libspdm_get_managed_buffer_size(&th_curr),
1653 : th_curr_hash_data);
1654 1 : bin_str0_size = sizeof(bin_str0);
1655 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
1656 : SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
1657 1 : NULL, (uint16_t)hash_size, hash_size, bin_str0,
1658 : &bin_str0_size);
1659 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
1660 : m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
1661 1 : bin_str2_size = sizeof(bin_str2);
1662 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
1663 : SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
1664 1 : th_curr_hash_data, (uint16_t)hash_size, hash_size,
1665 : bin_str2, &bin_str2_size);
1666 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
1667 : bin_str2, bin_str2_size,
1668 : response_handshake_secret, hash_size);
1669 1 : bin_str7_size = sizeof(bin_str7);
1670 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
1671 : SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
1672 1 : NULL, (uint16_t)hash_size, hash_size, bin_str7,
1673 : &bin_str7_size);
1674 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
1675 : hash_size, bin_str7, bin_str7_size,
1676 : response_finished_key, hash_size);
1677 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
1678 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
1679 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
1680 : response_finished_key, hash_size, ptr);
1681 1 : ptr += hmac_size;
1682 :
1683 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
1684 : false, spdm_response_size,
1685 : spdm_response, response_size,
1686 : response);
1687 : }
1688 1 : return LIBSPDM_STATUS_SUCCESS;
1689 :
1690 1 : case 0xE: {
1691 : spdm_key_exchange_response_t *spdm_response;
1692 : size_t dhe_key_size;
1693 : uint32_t hash_size;
1694 : uint32_t measurement_hash_size;
1695 : size_t signature_size;
1696 : uint32_t hmac_size;
1697 : uint8_t *ptr;
1698 : void *dhe_context;
1699 : uint8_t final_key[LIBSPDM_MAX_DHE_SS_SIZE];
1700 : size_t final_key_size;
1701 : size_t opaque_key_exchange_rsp_size;
1702 : void *data;
1703 : size_t data_size;
1704 : uint8_t measurement_hash_data[LIBSPDM_MAX_HASH_SIZE];
1705 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
1706 : uint8_t *cert_buffer;
1707 : size_t cert_buffer_size;
1708 : uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
1709 : uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
1710 : uint8_t bin_str0[128];
1711 : size_t bin_str0_size;
1712 : uint8_t bin_str2[128];
1713 : size_t bin_str2_size;
1714 : uint8_t bin_str7[128];
1715 : size_t bin_str7_size;
1716 : uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
1717 : uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
1718 : uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
1719 : size_t spdm_response_size;
1720 : size_t transport_header_size;
1721 :
1722 : ((libspdm_context_t *)spdm_context)
1723 1 : ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
1724 : ((libspdm_context_t *)spdm_context)
1725 1 : ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1726 : ((libspdm_context_t *)spdm_context)
1727 1 : ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
1728 : ((libspdm_context_t *)spdm_context)
1729 1 : ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
1730 1 : signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
1731 1 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
1732 1 : measurement_hash_size = libspdm_get_hash_size( m_libspdm_use_hash_algo);
1733 1 : hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
1734 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
1735 1 : opaque_key_exchange_rsp_size =
1736 1 : libspdm_get_opaque_data_version_selection_data_size( spdm_context);
1737 1 : spdm_response_size = sizeof(spdm_key_exchange_response_t) +
1738 1 : dhe_key_size + measurement_hash_size + sizeof(uint16_t) +
1739 1 : opaque_key_exchange_rsp_size + signature_size +
1740 : hmac_size;
1741 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
1742 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
1743 1 : libspdm_zero_mem(spdm_response,spdm_response_size);
1744 :
1745 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
1746 1 : spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
1747 1 : spdm_response->header.param1 = 0;
1748 1 : spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
1749 : /* Mutual authentication is requested even though it is not supported. */
1750 1 : spdm_response->mut_auth_requested =
1751 : SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST;
1752 1 : spdm_response->req_slot_id_param = 0;
1753 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
1754 1 : ptr = (void *)(spdm_response + 1);
1755 1 : dhe_context = libspdm_dhe_new(
1756 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
1757 : m_libspdm_use_dhe_algo,
1758 : true);
1759 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
1760 1 : final_key_size = sizeof(final_key);
1761 1 : libspdm_dhe_compute_key(
1762 : m_libspdm_use_dhe_algo, dhe_context,
1763 1 : (uint8_t *)&m_libspdm_local_buffer[0] +
1764 : sizeof(spdm_key_exchange_request_t),
1765 : dhe_key_size, final_key, &final_key_size);
1766 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
1767 1 : ptr += dhe_key_size;
1768 : /*Mock measurement hash*/
1769 1 : libspdm_copy_mem(measurement_hash_data, sizeof(measurement_hash_data),
1770 : m_libspdm_use_tcb_hash_value, measurement_hash_size);
1771 1 : libspdm_copy_mem(ptr, spdm_response_size - (ptr - (uint8_t *)spdm_response),
1772 : measurement_hash_data, measurement_hash_size);
1773 : /*libspdm_zero_mem (ptr, measurement_hash_size);*/
1774 1 : ptr += measurement_hash_size;
1775 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_rsp_size);
1776 1 : ptr += sizeof(uint16_t);
1777 1 : libspdm_build_opaque_data_version_selection_data(
1778 : spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
1779 : &opaque_key_exchange_rsp_size, ptr);
1780 1 : ptr += opaque_key_exchange_rsp_size;
1781 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
1782 : m_libspdm_use_asym_algo, &data,
1783 : &data_size, NULL, NULL)) {
1784 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
1785 : }
1786 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
1787 : sizeof(m_libspdm_local_buffer)
1788 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
1789 : m_libspdm_local_buffer),
1790 1 : spdm_response, (size_t)ptr - (size_t)spdm_response);
1791 1 : m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
1792 1 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
1793 : m_libspdm_local_buffer_size));
1794 1 : libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
1795 1 : libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
1796 1 : cert_buffer = (uint8_t *)data;
1797 1 : cert_buffer_size = data_size;
1798 1 : libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size, cert_buffer_hash);
1799 : /* transcript.message_a size is 0*/
1800 1 : libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
1801 1 : libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
1802 : m_libspdm_local_buffer_size);
1803 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
1804 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
1805 1 : free(data);
1806 1 : libspdm_responder_data_sign(
1807 : spdm_context,
1808 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
1809 : 0, SPDM_KEY_EXCHANGE_RSP,
1810 : m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
1811 1 : false, libspdm_get_managed_buffer(&th_curr),
1812 : libspdm_get_managed_buffer_size(&th_curr), ptr,
1813 : &signature_size);
1814 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
1815 : sizeof(m_libspdm_local_buffer)
1816 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
1817 : m_libspdm_local_buffer),
1818 : ptr, signature_size);
1819 1 : m_libspdm_local_buffer_size += signature_size;
1820 1 : libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
1821 1 : ptr += signature_size;
1822 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
1823 : libspdm_get_managed_buffer_size(&th_curr),
1824 : th_curr_hash_data);
1825 1 : bin_str0_size = sizeof(bin_str0);
1826 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
1827 : SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
1828 1 : NULL, (uint16_t)hash_size, hash_size, bin_str0,
1829 : &bin_str0_size);
1830 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
1831 : m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
1832 1 : bin_str2_size = sizeof(bin_str2);
1833 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
1834 : SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
1835 1 : th_curr_hash_data, (uint16_t)hash_size, hash_size,
1836 : bin_str2, &bin_str2_size);
1837 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
1838 : bin_str2, bin_str2_size,
1839 : response_handshake_secret, hash_size);
1840 1 : bin_str7_size = sizeof(bin_str7);
1841 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
1842 : SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
1843 1 : NULL, (uint16_t)hash_size, hash_size, bin_str7,
1844 : &bin_str7_size);
1845 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
1846 : hash_size, bin_str7, bin_str7_size,
1847 : response_finished_key, hash_size);
1848 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
1849 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
1850 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
1851 : response_finished_key, hash_size, ptr);
1852 1 : ptr += hmac_size;
1853 :
1854 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
1855 : false, spdm_response_size,
1856 : spdm_response, response_size,
1857 : response);
1858 : }
1859 1 : return LIBSPDM_STATUS_SUCCESS;
1860 :
1861 1 : case 0xF: {
1862 : spdm_key_exchange_response_t *spdm_response;
1863 : size_t dhe_key_size;
1864 : uint32_t hash_size;
1865 : size_t signature_size;
1866 : uint32_t hmac_size;
1867 : uint8_t *ptr;
1868 : void *dhe_context;
1869 : uint8_t final_key[LIBSPDM_MAX_DHE_SS_SIZE];
1870 : size_t final_key_size;
1871 : size_t opaque_key_exchange_rsp_size;
1872 : void *data;
1873 : size_t data_size;
1874 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
1875 : uint8_t *cert_buffer;
1876 : size_t cert_buffer_size;
1877 : uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
1878 : uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
1879 : uint8_t bin_str0[128];
1880 : size_t bin_str0_size;
1881 : uint8_t bin_str2[128];
1882 : size_t bin_str2_size;
1883 : uint8_t bin_str7[128];
1884 : size_t bin_str7_size;
1885 : uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
1886 : uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
1887 : uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
1888 : size_t spdm_response_size;
1889 : size_t transport_header_size;
1890 :
1891 : ((libspdm_context_t *)spdm_context)
1892 1 : ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
1893 : ((libspdm_context_t *)spdm_context)
1894 1 : ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
1895 : ((libspdm_context_t *)spdm_context)
1896 1 : ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
1897 : ((libspdm_context_t *)spdm_context)
1898 1 : ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
1899 1 : signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
1900 1 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
1901 1 : hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
1902 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
1903 1 : opaque_key_exchange_rsp_size =
1904 1 : libspdm_get_opaque_data_version_selection_data_size( spdm_context);
1905 1 : spdm_response_size = sizeof(spdm_key_exchange_response_t) +
1906 1 : dhe_key_size + 0 + sizeof(uint16_t) +
1907 1 : opaque_key_exchange_rsp_size + signature_size +
1908 : hmac_size;
1909 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
1910 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
1911 1 : libspdm_zero_mem(spdm_response,spdm_response_size);
1912 :
1913 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
1914 1 : spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
1915 1 : spdm_response->header.param1 = 0;
1916 1 : spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
1917 1 : spdm_response->mut_auth_requested = 0;
1918 1 : spdm_response->req_slot_id_param = 0;
1919 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
1920 1 : ptr = (void *)(spdm_response + 1);
1921 1 : dhe_context = libspdm_dhe_new(
1922 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
1923 : m_libspdm_use_dhe_algo,
1924 : true);
1925 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
1926 1 : final_key_size = sizeof(final_key);
1927 1 : libspdm_dhe_compute_key(
1928 : m_libspdm_use_dhe_algo, dhe_context,
1929 1 : (uint8_t *)&m_libspdm_local_buffer[0] +
1930 : sizeof(spdm_key_exchange_request_t),
1931 : dhe_key_size, final_key, &final_key_size);
1932 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
1933 1 : ptr += dhe_key_size;
1934 : /* libspdm_zero_mem (ptr, hash_size);
1935 : * ptr += hash_size;*/
1936 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_rsp_size);
1937 1 : ptr += sizeof(uint16_t);
1938 1 : libspdm_build_opaque_data_version_selection_data(
1939 : spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
1940 : &opaque_key_exchange_rsp_size, ptr);
1941 1 : ptr += opaque_key_exchange_rsp_size;
1942 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
1943 : m_libspdm_use_asym_algo, &data,
1944 : &data_size, NULL, NULL)) {
1945 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
1946 : }
1947 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
1948 : sizeof(m_libspdm_local_buffer)
1949 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
1950 : m_libspdm_local_buffer),
1951 1 : spdm_response, (size_t)ptr - (size_t)spdm_response);
1952 1 : m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
1953 1 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
1954 : m_libspdm_local_buffer_size));
1955 1 : libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
1956 1 : libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
1957 1 : cert_buffer = (uint8_t *)data;
1958 1 : cert_buffer_size = data_size;
1959 1 : libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size, cert_buffer_hash);
1960 : /* transcript.message_a size is 0*/
1961 1 : libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
1962 1 : libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
1963 : m_libspdm_local_buffer_size);
1964 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
1965 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
1966 1 : free(data);
1967 1 : libspdm_responder_data_sign(
1968 : spdm_context,
1969 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
1970 : 0, SPDM_KEY_EXCHANGE_RSP,
1971 : m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
1972 1 : false, libspdm_get_managed_buffer(&th_curr),
1973 : libspdm_get_managed_buffer_size(&th_curr), ptr,
1974 : &signature_size);
1975 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
1976 : sizeof(m_libspdm_local_buffer)
1977 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
1978 : m_libspdm_local_buffer),
1979 : ptr, signature_size);
1980 1 : m_libspdm_local_buffer_size += signature_size;
1981 1 : libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
1982 1 : ptr += signature_size;
1983 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
1984 : libspdm_get_managed_buffer_size(&th_curr),
1985 : th_curr_hash_data);
1986 1 : bin_str0_size = sizeof(bin_str0);
1987 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
1988 : SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
1989 1 : NULL, (uint16_t)hash_size, hash_size, bin_str0,
1990 : &bin_str0_size);
1991 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
1992 : m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
1993 1 : bin_str2_size = sizeof(bin_str2);
1994 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
1995 : SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
1996 1 : th_curr_hash_data, (uint16_t)hash_size, hash_size,
1997 : bin_str2, &bin_str2_size);
1998 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
1999 : bin_str2, bin_str2_size,
2000 : response_handshake_secret, hash_size);
2001 1 : bin_str7_size = sizeof(bin_str7);
2002 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
2003 : SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
2004 1 : NULL, (uint16_t)hash_size, hash_size, bin_str7,
2005 : &bin_str7_size);
2006 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
2007 : hash_size, bin_str7, bin_str7_size,
2008 : response_finished_key, hash_size);
2009 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
2010 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
2011 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
2012 : response_finished_key, hash_size, ptr);
2013 1 : ptr += hmac_size;
2014 :
2015 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
2016 : false, spdm_response_size,
2017 : spdm_response, response_size,
2018 : response);
2019 : }
2020 1 : return LIBSPDM_STATUS_SUCCESS;
2021 :
2022 1 : case 0x10: {
2023 : spdm_key_exchange_response_t *spdm_response;
2024 : size_t dhe_key_size;
2025 : uint32_t hash_size;
2026 : size_t signature_size;
2027 : uint32_t hmac_size;
2028 : uint8_t *ptr;
2029 : void *dhe_context;
2030 : uint8_t final_key[LIBSPDM_MAX_DHE_SS_SIZE];
2031 : size_t final_key_size;
2032 : size_t opaque_key_exchange_rsp_size;
2033 : void *data;
2034 : size_t data_size;
2035 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
2036 : uint8_t *cert_buffer;
2037 : size_t cert_buffer_size;
2038 : uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
2039 : uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
2040 : uint8_t bin_str0[128];
2041 : size_t bin_str0_size;
2042 : uint8_t bin_str2[128];
2043 : size_t bin_str2_size;
2044 : uint8_t bin_str7[128];
2045 : size_t bin_str7_size;
2046 : uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
2047 : uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
2048 : uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
2049 : size_t spdm_response_size;
2050 : size_t transport_header_size;
2051 :
2052 : ((libspdm_context_t *)spdm_context)
2053 1 : ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
2054 : ((libspdm_context_t *)spdm_context)
2055 1 : ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
2056 : ((libspdm_context_t *)spdm_context)
2057 1 : ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
2058 : ((libspdm_context_t *)spdm_context)
2059 1 : ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
2060 1 : signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
2061 1 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
2062 1 : hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
2063 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
2064 1 : opaque_key_exchange_rsp_size =
2065 1 : libspdm_get_opaque_data_version_selection_data_size( spdm_context);
2066 1 : spdm_response_size = sizeof(spdm_key_exchange_response_t) +
2067 1 : dhe_key_size + 0 + sizeof(uint16_t) +
2068 1 : opaque_key_exchange_rsp_size + signature_size +
2069 : hmac_size;
2070 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
2071 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
2072 1 : libspdm_zero_mem(spdm_response,spdm_response_size);
2073 :
2074 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
2075 1 : spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
2076 1 : spdm_response->header.param1 = 0;
2077 1 : spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
2078 1 : spdm_response->mut_auth_requested = 0;
2079 1 : spdm_response->req_slot_id_param = 0;
2080 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
2081 1 : ptr = (void *)(spdm_response + 1);
2082 1 : dhe_context = libspdm_dhe_new(
2083 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
2084 : m_libspdm_use_dhe_algo,
2085 : true);
2086 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
2087 1 : final_key_size = sizeof(final_key);
2088 1 : libspdm_dhe_compute_key(
2089 : m_libspdm_use_dhe_algo, dhe_context,
2090 1 : (uint8_t *)&m_libspdm_local_buffer[0] +
2091 : sizeof(spdm_key_exchange_request_t),
2092 : dhe_key_size, final_key, &final_key_size);
2093 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
2094 1 : ptr += dhe_key_size;
2095 : /* libspdm_zero_mem (ptr, hash_size);
2096 : * ptr += hash_size;*/
2097 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_rsp_size);
2098 1 : ptr += sizeof(uint16_t);
2099 1 : libspdm_build_opaque_data_version_selection_data(
2100 : spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
2101 : &opaque_key_exchange_rsp_size, ptr);
2102 1 : ptr += opaque_key_exchange_rsp_size;
2103 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
2104 : m_libspdm_use_asym_algo, &data,
2105 : &data_size, NULL, NULL)) {
2106 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
2107 : }
2108 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
2109 : sizeof(m_libspdm_local_buffer)
2110 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
2111 : m_libspdm_local_buffer),
2112 1 : spdm_response, (size_t)ptr - (size_t)spdm_response);
2113 1 : m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
2114 1 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
2115 : m_libspdm_local_buffer_size));
2116 1 : libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
2117 1 : libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
2118 1 : cert_buffer = (uint8_t *)data;
2119 1 : cert_buffer_size = data_size;
2120 1 : libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size, cert_buffer_hash);
2121 : /* transcript.message_a size is 0*/
2122 1 : libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
2123 1 : libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
2124 : m_libspdm_local_buffer_size);
2125 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
2126 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
2127 1 : free(data);
2128 1 : libspdm_responder_data_sign(
2129 : spdm_context,
2130 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
2131 : 0, SPDM_KEY_EXCHANGE_RSP,
2132 : m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
2133 1 : false, libspdm_get_managed_buffer(&th_curr),
2134 : libspdm_get_managed_buffer_size(&th_curr), ptr,
2135 : &signature_size);
2136 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
2137 : sizeof(m_libspdm_local_buffer)
2138 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
2139 : m_libspdm_local_buffer),
2140 : ptr, signature_size);
2141 1 : m_libspdm_local_buffer_size += signature_size;
2142 1 : libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
2143 1 : ptr += signature_size;
2144 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
2145 : libspdm_get_managed_buffer_size(&th_curr),
2146 : th_curr_hash_data);
2147 1 : bin_str0_size = sizeof(bin_str0);
2148 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
2149 : SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
2150 1 : NULL, (uint16_t)hash_size, hash_size, bin_str0,
2151 : &bin_str0_size);
2152 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
2153 : m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
2154 1 : bin_str2_size = sizeof(bin_str2);
2155 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
2156 : SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
2157 1 : th_curr_hash_data, (uint16_t)hash_size, hash_size,
2158 : bin_str2, &bin_str2_size);
2159 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
2160 : bin_str2, bin_str2_size,
2161 : response_handshake_secret, hash_size);
2162 1 : bin_str7_size = sizeof(bin_str7);
2163 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
2164 : SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
2165 1 : NULL, (uint16_t)hash_size, hash_size, bin_str7,
2166 : &bin_str7_size);
2167 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
2168 : hash_size, bin_str7, bin_str7_size,
2169 : response_finished_key, hash_size);
2170 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
2171 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
2172 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
2173 : response_finished_key, hash_size, ptr);
2174 1 : ptr += hmac_size;
2175 :
2176 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
2177 : false, spdm_response_size,
2178 : spdm_response, response_size,
2179 : response);
2180 : }
2181 1 : return LIBSPDM_STATUS_SUCCESS;
2182 :
2183 1 : case 0x11: {
2184 : spdm_key_exchange_response_t *spdm_response;
2185 : size_t dhe_key_size;
2186 : uint32_t hash_size;
2187 : uint32_t measurement_hash_size;
2188 : size_t signature_size;
2189 : uint32_t hmac_size;
2190 : uint8_t *ptr;
2191 : void *dhe_context;
2192 : uint8_t final_key[LIBSPDM_MAX_DHE_SS_SIZE];
2193 : size_t final_key_size;
2194 : size_t opaque_key_exchange_rsp_size;
2195 : void *data;
2196 : size_t data_size;
2197 : uint8_t measurement_hash_data[LIBSPDM_MAX_HASH_SIZE];
2198 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
2199 : uint8_t *cert_buffer;
2200 : size_t cert_buffer_size;
2201 : uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
2202 : uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
2203 : uint8_t bin_str0[128];
2204 : size_t bin_str0_size;
2205 : uint8_t bin_str2[128];
2206 : size_t bin_str2_size;
2207 : uint8_t bin_str7[128];
2208 : size_t bin_str7_size;
2209 : uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
2210 : uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
2211 : uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
2212 : size_t spdm_response_size;
2213 : size_t transport_header_size;
2214 :
2215 : ((libspdm_context_t *)spdm_context)
2216 1 : ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
2217 : ((libspdm_context_t *)spdm_context)
2218 1 : ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
2219 : ((libspdm_context_t *)spdm_context)
2220 1 : ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
2221 : ((libspdm_context_t *)spdm_context)
2222 1 : ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
2223 1 : signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
2224 1 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
2225 1 : measurement_hash_size = libspdm_get_hash_size( m_libspdm_use_hash_algo);
2226 1 : hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
2227 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
2228 1 : opaque_key_exchange_rsp_size =
2229 1 : libspdm_get_opaque_data_version_selection_data_size( spdm_context);
2230 1 : spdm_response_size = sizeof(spdm_key_exchange_response_t) +
2231 1 : dhe_key_size + measurement_hash_size + sizeof(uint16_t) +
2232 1 : opaque_key_exchange_rsp_size + signature_size +
2233 : hmac_size;
2234 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
2235 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
2236 1 : libspdm_zero_mem(spdm_response,spdm_response_size);
2237 :
2238 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
2239 1 : spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
2240 1 : spdm_response->header.param1 = 0;
2241 1 : spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
2242 1 : spdm_response->mut_auth_requested = 0;
2243 1 : spdm_response->req_slot_id_param = 0;
2244 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
2245 1 : ptr = (void *)(spdm_response + 1);
2246 1 : dhe_context = libspdm_dhe_new(
2247 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
2248 : m_libspdm_use_dhe_algo,
2249 : true);
2250 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
2251 1 : final_key_size = sizeof(final_key);
2252 1 : libspdm_dhe_compute_key(
2253 : m_libspdm_use_dhe_algo, dhe_context,
2254 1 : (uint8_t *)&m_libspdm_local_buffer[0] +
2255 : sizeof(spdm_key_exchange_request_t),
2256 : dhe_key_size, final_key, &final_key_size);
2257 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
2258 1 : ptr += dhe_key_size;
2259 : /*Mock measurement hash as TCB*/
2260 1 : libspdm_copy_mem(measurement_hash_data, sizeof(measurement_hash_data),
2261 : m_libspdm_use_tcb_hash_value, measurement_hash_size);
2262 1 : libspdm_copy_mem(ptr, spdm_response_size - (ptr - (uint8_t *)spdm_response),
2263 : measurement_hash_data, measurement_hash_size);
2264 : /*libspdm_zero_mem (ptr, measurement_hash_size);*/
2265 1 : ptr += measurement_hash_size;
2266 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_rsp_size);
2267 1 : ptr += sizeof(uint16_t);
2268 1 : libspdm_build_opaque_data_version_selection_data(
2269 : spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
2270 : &opaque_key_exchange_rsp_size, ptr);
2271 1 : ptr += opaque_key_exchange_rsp_size;
2272 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
2273 : m_libspdm_use_asym_algo, &data,
2274 : &data_size, NULL, NULL)) {
2275 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
2276 : }
2277 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
2278 : sizeof(m_libspdm_local_buffer)
2279 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
2280 : m_libspdm_local_buffer),
2281 1 : spdm_response, (size_t)ptr - (size_t)spdm_response);
2282 1 : m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
2283 1 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
2284 : m_libspdm_local_buffer_size));
2285 1 : libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
2286 1 : libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
2287 1 : cert_buffer = (uint8_t *)data;
2288 1 : cert_buffer_size = data_size;
2289 1 : libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size, cert_buffer_hash);
2290 : /* transcript.message_a size is 0*/
2291 1 : libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
2292 1 : libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
2293 : m_libspdm_local_buffer_size);
2294 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
2295 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
2296 1 : free(data);
2297 1 : libspdm_responder_data_sign(
2298 : spdm_context,
2299 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
2300 : 0, SPDM_KEY_EXCHANGE_RSP,
2301 : m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
2302 1 : false, libspdm_get_managed_buffer(&th_curr),
2303 : libspdm_get_managed_buffer_size(&th_curr), ptr,
2304 : &signature_size);
2305 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
2306 : sizeof(m_libspdm_local_buffer)
2307 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
2308 : m_libspdm_local_buffer),
2309 : ptr, signature_size);
2310 1 : m_libspdm_local_buffer_size += signature_size;
2311 1 : libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
2312 1 : ptr += signature_size;
2313 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
2314 : libspdm_get_managed_buffer_size(&th_curr),
2315 : th_curr_hash_data);
2316 1 : bin_str0_size = sizeof(bin_str0);
2317 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
2318 : SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
2319 1 : NULL, (uint16_t)hash_size, hash_size, bin_str0,
2320 : &bin_str0_size);
2321 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
2322 : m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
2323 1 : bin_str2_size = sizeof(bin_str2);
2324 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
2325 : SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
2326 1 : th_curr_hash_data, (uint16_t)hash_size, hash_size,
2327 : bin_str2, &bin_str2_size);
2328 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
2329 : bin_str2, bin_str2_size,
2330 : response_handshake_secret, hash_size);
2331 1 : bin_str7_size = sizeof(bin_str7);
2332 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
2333 : SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
2334 1 : NULL, (uint16_t)hash_size, hash_size, bin_str7,
2335 : &bin_str7_size);
2336 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
2337 : hash_size, bin_str7, bin_str7_size,
2338 : response_finished_key, hash_size);
2339 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
2340 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
2341 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
2342 : response_finished_key, hash_size, ptr);
2343 1 : ptr += hmac_size;
2344 :
2345 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
2346 : false, spdm_response_size,
2347 : spdm_response, response_size,
2348 : response);
2349 : }
2350 1 : return LIBSPDM_STATUS_SUCCESS;
2351 :
2352 1 : case 0x12: {
2353 : spdm_key_exchange_response_t *spdm_response;
2354 : size_t dhe_key_size;
2355 : uint32_t hash_size;
2356 : uint32_t measurement_hash_size;
2357 : size_t signature_size;
2358 : uint32_t hmac_size;
2359 : uint8_t *ptr;
2360 : void *dhe_context;
2361 : uint8_t final_key[LIBSPDM_MAX_DHE_SS_SIZE];
2362 : size_t final_key_size;
2363 : size_t opaque_key_exchange_rsp_size;
2364 : void *data;
2365 : size_t data_size;
2366 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
2367 : uint8_t *cert_buffer;
2368 : size_t cert_buffer_size;
2369 : uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
2370 : uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
2371 : uint8_t bin_str0[128];
2372 : size_t bin_str0_size;
2373 : uint8_t bin_str2[128];
2374 : size_t bin_str2_size;
2375 : uint8_t bin_str7[128];
2376 : size_t bin_str7_size;
2377 : uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
2378 : uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
2379 : uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
2380 : size_t spdm_response_size;
2381 : size_t transport_header_size;
2382 :
2383 : ((libspdm_context_t *)spdm_context)
2384 1 : ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
2385 : ((libspdm_context_t *)spdm_context)
2386 1 : ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
2387 : ((libspdm_context_t *)spdm_context)
2388 1 : ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
2389 : ((libspdm_context_t *)spdm_context)
2390 1 : ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
2391 1 : signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
2392 1 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
2393 1 : measurement_hash_size = 0;
2394 1 : hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
2395 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
2396 1 : opaque_key_exchange_rsp_size =
2397 1 : libspdm_get_opaque_data_version_selection_data_size( spdm_context);
2398 1 : spdm_response_size = sizeof(spdm_key_exchange_response_t) +
2399 1 : dhe_key_size + measurement_hash_size + sizeof(uint16_t) +
2400 1 : opaque_key_exchange_rsp_size + signature_size +
2401 : hmac_size;
2402 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
2403 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
2404 1 : libspdm_zero_mem(spdm_response,spdm_response_size);
2405 :
2406 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
2407 1 : spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
2408 1 : spdm_response->header.param1 = 0;
2409 1 : spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
2410 1 : spdm_response->mut_auth_requested = 0;
2411 1 : spdm_response->req_slot_id_param = 0;
2412 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
2413 1 : ptr = (void *)(spdm_response + 1);
2414 1 : dhe_context = libspdm_dhe_new(
2415 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
2416 : m_libspdm_use_dhe_algo,
2417 : true);
2418 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
2419 1 : final_key_size = sizeof(final_key);
2420 1 : libspdm_dhe_compute_key(
2421 : m_libspdm_use_dhe_algo, dhe_context,
2422 1 : (uint8_t *)&m_libspdm_local_buffer[0] +
2423 : sizeof(spdm_key_exchange_request_t),
2424 : dhe_key_size, final_key, &final_key_size);
2425 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
2426 1 : ptr += dhe_key_size;
2427 :
2428 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_rsp_size);
2429 1 : ptr += sizeof(uint16_t);
2430 1 : libspdm_build_opaque_data_version_selection_data(
2431 : spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
2432 : &opaque_key_exchange_rsp_size, ptr);
2433 1 : ptr += opaque_key_exchange_rsp_size;
2434 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
2435 : m_libspdm_use_asym_algo, &data,
2436 : &data_size, NULL, NULL)) {
2437 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
2438 : }
2439 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
2440 : sizeof(m_libspdm_local_buffer)
2441 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
2442 : m_libspdm_local_buffer),
2443 1 : spdm_response, (size_t)ptr - (size_t)spdm_response);
2444 1 : m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
2445 1 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
2446 : m_libspdm_local_buffer_size));
2447 1 : libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
2448 1 : libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
2449 1 : cert_buffer = (uint8_t *)data;
2450 1 : cert_buffer_size = data_size;
2451 1 : libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size, cert_buffer_hash);
2452 : /* transcript.message_a size is 0*/
2453 1 : libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
2454 1 : libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
2455 : m_libspdm_local_buffer_size);
2456 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
2457 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
2458 1 : free(data);
2459 1 : libspdm_responder_data_sign(
2460 : spdm_context,
2461 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
2462 : 0, SPDM_KEY_EXCHANGE_RSP,
2463 : m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
2464 1 : false, libspdm_get_managed_buffer(&th_curr),
2465 : libspdm_get_managed_buffer_size(&th_curr), ptr,
2466 : &signature_size);
2467 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
2468 : sizeof(m_libspdm_local_buffer)
2469 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
2470 : m_libspdm_local_buffer),
2471 : ptr, signature_size);
2472 1 : m_libspdm_local_buffer_size += signature_size;
2473 1 : libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
2474 1 : ptr += signature_size-1;
2475 1 : *ptr ^= 0xFF;
2476 1 : ptr++;
2477 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
2478 : libspdm_get_managed_buffer_size(&th_curr),
2479 : th_curr_hash_data);
2480 1 : bin_str0_size = sizeof(bin_str0);
2481 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
2482 : SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
2483 1 : NULL, (uint16_t)hash_size, hash_size, bin_str0,
2484 : &bin_str0_size);
2485 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
2486 : m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
2487 1 : bin_str2_size = sizeof(bin_str2);
2488 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
2489 : SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
2490 1 : th_curr_hash_data, (uint16_t)hash_size, hash_size,
2491 : bin_str2, &bin_str2_size);
2492 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
2493 : bin_str2, bin_str2_size,
2494 : response_handshake_secret, hash_size);
2495 1 : bin_str7_size = sizeof(bin_str7);
2496 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
2497 : SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
2498 1 : NULL, (uint16_t)hash_size, hash_size, bin_str7,
2499 : &bin_str7_size);
2500 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
2501 : hash_size, bin_str7, bin_str7_size,
2502 : response_finished_key, hash_size);
2503 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
2504 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
2505 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
2506 : response_finished_key, hash_size, ptr);
2507 1 : ptr += hmac_size;
2508 :
2509 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
2510 : false, spdm_response_size,
2511 : spdm_response, response_size,
2512 : response);
2513 : }
2514 1 : return LIBSPDM_STATUS_SUCCESS;
2515 :
2516 0 : case 0x13: {
2517 : }
2518 0 : return LIBSPDM_STATUS_SUCCESS;
2519 :
2520 1 : case 0x14: {
2521 : spdm_key_exchange_response_t *spdm_response;
2522 : size_t dhe_key_size;
2523 : uint32_t hash_size;
2524 : size_t signature_size;
2525 : uint32_t hmac_size;
2526 : uint8_t *ptr;
2527 : void *dhe_context;
2528 : uint8_t final_key[LIBSPDM_MAX_DHE_SS_SIZE];
2529 : size_t final_key_size;
2530 : size_t opaque_key_exchange_rsp_size;
2531 : void *data;
2532 : size_t data_size;
2533 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
2534 : uint8_t *cert_buffer;
2535 : size_t cert_buffer_size;
2536 : uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
2537 : uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
2538 : uint8_t bin_str0[128];
2539 : size_t bin_str0_size;
2540 : uint8_t bin_str2[128];
2541 : size_t bin_str2_size;
2542 : uint8_t bin_str7[128];
2543 : size_t bin_str7_size;
2544 : uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
2545 : uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
2546 : uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
2547 : size_t spdm_response_size;
2548 : size_t transport_header_size;
2549 :
2550 : ((libspdm_context_t *)spdm_context)
2551 1 : ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
2552 : ((libspdm_context_t *)spdm_context)
2553 1 : ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
2554 : ((libspdm_context_t *)spdm_context)
2555 1 : ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
2556 : ((libspdm_context_t *)spdm_context)
2557 1 : ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
2558 1 : signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
2559 1 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
2560 1 : hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
2561 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
2562 1 : opaque_key_exchange_rsp_size =
2563 1 : libspdm_get_opaque_data_version_selection_data_size( spdm_context);
2564 1 : spdm_response_size = sizeof(spdm_key_exchange_response_t) +
2565 1 : dhe_key_size + 0 + sizeof(uint16_t) +
2566 1 : opaque_key_exchange_rsp_size + signature_size +
2567 : hmac_size;
2568 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
2569 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
2570 1 : libspdm_zero_mem(spdm_response,spdm_response_size);
2571 :
2572 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
2573 1 : spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
2574 1 : spdm_response->header.param1 = 5;
2575 1 : spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
2576 1 : spdm_response->mut_auth_requested = 0;
2577 1 : spdm_response->req_slot_id_param = 0;
2578 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
2579 1 : ptr = (void *)(spdm_response + 1);
2580 1 : dhe_context = libspdm_dhe_new(
2581 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
2582 : m_libspdm_use_dhe_algo,
2583 : true);
2584 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
2585 1 : final_key_size = sizeof(final_key);
2586 1 : libspdm_dhe_compute_key(
2587 : m_libspdm_use_dhe_algo, dhe_context,
2588 1 : (uint8_t *)&m_libspdm_local_buffer[0] +
2589 : sizeof(spdm_key_exchange_request_t),
2590 : dhe_key_size, final_key, &final_key_size);
2591 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
2592 1 : ptr += dhe_key_size;
2593 : /* libspdm_zero_mem (ptr, hash_size);
2594 : * ptr += hash_size;*/
2595 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_rsp_size);
2596 1 : ptr += sizeof(uint16_t);
2597 1 : libspdm_build_opaque_data_version_selection_data(
2598 : spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
2599 : &opaque_key_exchange_rsp_size, ptr);
2600 1 : ptr += opaque_key_exchange_rsp_size;
2601 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
2602 : m_libspdm_use_asym_algo, &data,
2603 : &data_size, NULL, NULL)) {
2604 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
2605 : }
2606 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
2607 : sizeof(m_libspdm_local_buffer)
2608 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
2609 : m_libspdm_local_buffer),
2610 1 : spdm_response, (size_t)ptr - (size_t)spdm_response);
2611 1 : m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
2612 1 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
2613 : m_libspdm_local_buffer_size));
2614 1 : libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
2615 1 : libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
2616 1 : cert_buffer = (uint8_t *)data;
2617 1 : cert_buffer_size = data_size;
2618 1 : libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size, cert_buffer_hash);
2619 : /* transcript.message_a size is 0*/
2620 1 : libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
2621 1 : libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
2622 : m_libspdm_local_buffer_size);
2623 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
2624 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
2625 1 : free(data);
2626 1 : libspdm_responder_data_sign(
2627 : spdm_context,
2628 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
2629 : 0, SPDM_KEY_EXCHANGE_RSP,
2630 : m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
2631 1 : false, libspdm_get_managed_buffer(&th_curr),
2632 : libspdm_get_managed_buffer_size(&th_curr), ptr,
2633 : &signature_size);
2634 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
2635 : sizeof(m_libspdm_local_buffer)
2636 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
2637 : m_libspdm_local_buffer),
2638 : ptr, signature_size);
2639 1 : m_libspdm_local_buffer_size += signature_size;
2640 1 : libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
2641 1 : ptr += signature_size;
2642 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
2643 : libspdm_get_managed_buffer_size(&th_curr),
2644 : th_curr_hash_data);
2645 1 : bin_str0_size = sizeof(bin_str0);
2646 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
2647 : SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
2648 1 : NULL, (uint16_t)hash_size, hash_size, bin_str0,
2649 : &bin_str0_size);
2650 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
2651 : m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
2652 1 : bin_str2_size = sizeof(bin_str2);
2653 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
2654 : SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
2655 1 : th_curr_hash_data, (uint16_t)hash_size, hash_size,
2656 : bin_str2, &bin_str2_size);
2657 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
2658 : bin_str2, bin_str2_size,
2659 : response_handshake_secret, hash_size);
2660 1 : bin_str7_size = sizeof(bin_str7);
2661 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
2662 : SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
2663 1 : NULL, (uint16_t)hash_size, hash_size, bin_str7,
2664 : &bin_str7_size);
2665 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
2666 : hash_size, bin_str7, bin_str7_size,
2667 : response_finished_key, hash_size);
2668 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
2669 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
2670 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
2671 : response_finished_key, hash_size, ptr);
2672 1 : ptr += hmac_size;
2673 :
2674 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
2675 : false, spdm_response_size,
2676 : spdm_response, response_size,
2677 : response);
2678 : }
2679 1 : return LIBSPDM_STATUS_SUCCESS;
2680 :
2681 1 : case 0x15: {
2682 : spdm_key_exchange_response_t *spdm_response;
2683 : size_t dhe_key_size;
2684 : uint32_t hash_size;
2685 : size_t signature_size;
2686 : uint32_t hmac_size;
2687 : uint8_t *ptr;
2688 : void *dhe_context;
2689 : uint8_t final_key[LIBSPDM_MAX_DHE_SS_SIZE];
2690 : size_t final_key_size;
2691 : size_t opaque_key_exchange_rsp_size;
2692 : void *data;
2693 : size_t data_size;
2694 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
2695 : uint8_t *cert_buffer;
2696 : size_t cert_buffer_size;
2697 : uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
2698 : uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
2699 : uint8_t bin_str0[128];
2700 : size_t bin_str0_size;
2701 : uint8_t bin_str2[128];
2702 : size_t bin_str2_size;
2703 : uint8_t bin_str7[128];
2704 : size_t bin_str7_size;
2705 : uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
2706 : uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
2707 : uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
2708 : size_t spdm_response_size;
2709 : size_t transport_header_size;
2710 :
2711 : ((libspdm_context_t *)spdm_context)
2712 1 : ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
2713 : ((libspdm_context_t *)spdm_context)
2714 1 : ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
2715 : ((libspdm_context_t *)spdm_context)
2716 1 : ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
2717 : ((libspdm_context_t *)spdm_context)
2718 1 : ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
2719 1 : signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
2720 1 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
2721 1 : hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
2722 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
2723 1 : opaque_key_exchange_rsp_size =
2724 1 : libspdm_get_opaque_data_version_selection_data_size(spdm_context);
2725 1 : spdm_response_size = sizeof(spdm_key_exchange_response_t) +
2726 1 : dhe_key_size + 0 + sizeof(uint16_t) +
2727 1 : opaque_key_exchange_rsp_size + signature_size + hmac_size;
2728 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
2729 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
2730 1 : libspdm_zero_mem(spdm_response,spdm_response_size);
2731 :
2732 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
2733 1 : spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
2734 1 : spdm_response->header.param1 = 0;
2735 1 : spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
2736 1 : spdm_response->mut_auth_requested = 0;
2737 1 : spdm_response->req_slot_id_param = 0;
2738 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
2739 1 : ptr = (void *)(spdm_response + 1);
2740 1 : dhe_context = libspdm_dhe_new(
2741 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
2742 : m_libspdm_use_dhe_algo, true);
2743 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
2744 1 : final_key_size = sizeof(final_key);
2745 1 : libspdm_dhe_compute_key(
2746 : m_libspdm_use_dhe_algo, dhe_context,
2747 1 : (uint8_t *)&m_libspdm_local_buffer[0] +
2748 : sizeof(spdm_key_exchange_request_t),
2749 : dhe_key_size, final_key, &final_key_size);
2750 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
2751 1 : ptr += dhe_key_size;
2752 :
2753 1 : libspdm_write_uint16(ptr, SPDM_MAX_OPAQUE_DATA_SIZE + 1);
2754 1 : ptr += sizeof(uint16_t);
2755 1 : libspdm_build_opaque_data_version_selection_data(
2756 : spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
2757 : &opaque_key_exchange_rsp_size, ptr);
2758 1 : ptr += opaque_key_exchange_rsp_size;
2759 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
2760 : m_libspdm_use_asym_algo, &data,
2761 : &data_size, NULL, NULL)) {
2762 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
2763 : }
2764 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
2765 : sizeof(m_libspdm_local_buffer)
2766 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
2767 : m_libspdm_local_buffer),
2768 1 : spdm_response, (size_t)ptr - (size_t)spdm_response);
2769 1 : m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
2770 1 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
2771 : m_libspdm_local_buffer_size));
2772 1 : libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
2773 1 : libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
2774 1 : cert_buffer = (uint8_t *)data;
2775 1 : cert_buffer_size = data_size;
2776 1 : libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size, cert_buffer_hash);
2777 : /* transcript.message_a size is 0*/
2778 1 : libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
2779 1 : libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
2780 : m_libspdm_local_buffer_size);
2781 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
2782 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
2783 1 : free(data);
2784 1 : libspdm_responder_data_sign(
2785 : spdm_context,
2786 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
2787 : 0, SPDM_KEY_EXCHANGE_RSP,
2788 : m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
2789 1 : false, libspdm_get_managed_buffer(&th_curr),
2790 : libspdm_get_managed_buffer_size(&th_curr), ptr,
2791 : &signature_size);
2792 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
2793 : sizeof(m_libspdm_local_buffer)
2794 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
2795 : m_libspdm_local_buffer),
2796 : ptr, signature_size);
2797 1 : m_libspdm_local_buffer_size += signature_size;
2798 1 : libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
2799 1 : ptr += signature_size;
2800 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
2801 : libspdm_get_managed_buffer_size(&th_curr),
2802 : th_curr_hash_data);
2803 1 : bin_str0_size = sizeof(bin_str0);
2804 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
2805 : SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
2806 1 : NULL, (uint16_t)hash_size, hash_size, bin_str0,
2807 : &bin_str0_size);
2808 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
2809 : m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
2810 1 : bin_str2_size = sizeof(bin_str2);
2811 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
2812 : SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
2813 1 : th_curr_hash_data, (uint16_t)hash_size, hash_size,
2814 : bin_str2, &bin_str2_size);
2815 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
2816 : bin_str2, bin_str2_size,
2817 : response_handshake_secret, hash_size);
2818 1 : bin_str7_size = sizeof(bin_str7);
2819 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
2820 : SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
2821 1 : NULL, (uint16_t)hash_size, hash_size, bin_str7,
2822 : &bin_str7_size);
2823 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
2824 : hash_size, bin_str7, bin_str7_size,
2825 : response_finished_key, hash_size);
2826 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
2827 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
2828 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
2829 : response_finished_key, hash_size, ptr);
2830 1 : ptr += hmac_size;
2831 :
2832 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
2833 : false, spdm_response_size,
2834 : spdm_response, response_size,
2835 : response);
2836 : }
2837 1 : return LIBSPDM_STATUS_SUCCESS;
2838 :
2839 1 : case 0x16: {
2840 : spdm_key_exchange_response_t *spdm_response;
2841 : size_t dhe_key_size;
2842 : uint32_t hash_size;
2843 : size_t signature_size;
2844 : uint32_t hmac_size;
2845 : uint8_t *ptr;
2846 : void *dhe_context;
2847 : uint8_t final_key[LIBSPDM_MAX_DHE_SS_SIZE];
2848 : size_t final_key_size;
2849 : size_t opaque_key_exchange_rsp_size;
2850 : void *data;
2851 : size_t data_size;
2852 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
2853 : uint8_t *cert_buffer;
2854 : size_t cert_buffer_size;
2855 : uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
2856 : uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
2857 : uint8_t bin_str0[128];
2858 : size_t bin_str0_size;
2859 : uint8_t bin_str2[128];
2860 : size_t bin_str2_size;
2861 : uint8_t bin_str7[128];
2862 : size_t bin_str7_size;
2863 : uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
2864 : uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
2865 : uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
2866 : size_t spdm_response_size;
2867 : size_t transport_header_size;
2868 :
2869 : ((libspdm_context_t *)spdm_context)
2870 1 : ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
2871 : ((libspdm_context_t *)spdm_context)
2872 1 : ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
2873 : ((libspdm_context_t *)spdm_context)
2874 1 : ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
2875 : ((libspdm_context_t *)spdm_context)
2876 1 : ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
2877 1 : signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
2878 1 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
2879 1 : hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
2880 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
2881 1 : opaque_key_exchange_rsp_size =
2882 1 : libspdm_get_opaque_data_version_selection_data_size(spdm_context);
2883 1 : spdm_response_size = sizeof(spdm_key_exchange_response_t) +
2884 1 : dhe_key_size + 0 + sizeof(uint16_t) +
2885 1 : opaque_key_exchange_rsp_size + signature_size +
2886 : hmac_size;
2887 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
2888 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
2889 1 : libspdm_zero_mem(spdm_response,spdm_response_size);
2890 :
2891 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
2892 1 : spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
2893 1 : spdm_response->header.param1 = 0;
2894 1 : spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
2895 : /* Requester's PUB_KEY_ID_CAP is set and Responder requests the encapsulated flow. */
2896 1 : spdm_response->mut_auth_requested =
2897 : SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST;
2898 1 : spdm_response->req_slot_id_param = 0;
2899 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
2900 1 : ptr = (void *)(spdm_response + 1);
2901 1 : dhe_context = libspdm_dhe_new(
2902 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
2903 : m_libspdm_use_dhe_algo,
2904 : true);
2905 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
2906 1 : final_key_size = sizeof(final_key);
2907 1 : libspdm_dhe_compute_key(
2908 : m_libspdm_use_dhe_algo, dhe_context,
2909 1 : (uint8_t *)&m_libspdm_local_buffer[0] +
2910 : sizeof(spdm_key_exchange_request_t),
2911 : dhe_key_size, final_key, &final_key_size);
2912 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
2913 1 : ptr += dhe_key_size;
2914 : /* libspdm_zero_mem (ptr, hash_size);
2915 : * ptr += hash_size;*/
2916 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_rsp_size);
2917 1 : ptr += sizeof(uint16_t);
2918 1 : libspdm_build_opaque_data_version_selection_data(
2919 : spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
2920 : &opaque_key_exchange_rsp_size, ptr);
2921 1 : ptr += opaque_key_exchange_rsp_size;
2922 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
2923 : m_libspdm_use_asym_algo, &data,
2924 : &data_size, NULL, NULL)) {
2925 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
2926 : }
2927 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
2928 : sizeof(m_libspdm_local_buffer)
2929 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
2930 : m_libspdm_local_buffer),
2931 1 : spdm_response, (size_t)ptr - (size_t)spdm_response);
2932 1 : m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
2933 1 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
2934 : m_libspdm_local_buffer_size));
2935 1 : libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
2936 1 : libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
2937 1 : cert_buffer = (uint8_t *)data;
2938 1 : cert_buffer_size = data_size;
2939 1 : libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size, cert_buffer_hash);
2940 : /* transcript.message_a size is 0*/
2941 1 : libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
2942 1 : libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
2943 : m_libspdm_local_buffer_size);
2944 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
2945 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
2946 1 : free(data);
2947 1 : libspdm_responder_data_sign(
2948 : spdm_context,
2949 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
2950 : 0, SPDM_KEY_EXCHANGE_RSP,
2951 : m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
2952 1 : false, libspdm_get_managed_buffer(&th_curr),
2953 : libspdm_get_managed_buffer_size(&th_curr), ptr,
2954 : &signature_size);
2955 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
2956 : sizeof(m_libspdm_local_buffer)
2957 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
2958 : m_libspdm_local_buffer),
2959 : ptr, signature_size);
2960 1 : m_libspdm_local_buffer_size += signature_size;
2961 1 : libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
2962 1 : ptr += signature_size;
2963 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
2964 : libspdm_get_managed_buffer_size(&th_curr),
2965 : th_curr_hash_data);
2966 1 : bin_str0_size = sizeof(bin_str0);
2967 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
2968 : SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
2969 1 : NULL, (uint16_t)hash_size, hash_size, bin_str0,
2970 : &bin_str0_size);
2971 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
2972 : m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
2973 1 : bin_str2_size = sizeof(bin_str2);
2974 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
2975 : SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
2976 1 : th_curr_hash_data, (uint16_t)hash_size, hash_size,
2977 : bin_str2, &bin_str2_size);
2978 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
2979 : bin_str2, bin_str2_size,
2980 : response_handshake_secret, hash_size);
2981 1 : bin_str7_size = sizeof(bin_str7);
2982 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
2983 : SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
2984 1 : NULL, (uint16_t)hash_size, hash_size, bin_str7,
2985 : &bin_str7_size);
2986 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
2987 : hash_size, bin_str7, bin_str7_size,
2988 : response_finished_key, hash_size);
2989 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
2990 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
2991 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
2992 : response_finished_key, hash_size, ptr);
2993 1 : ptr += hmac_size;
2994 :
2995 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
2996 : false, spdm_response_size,
2997 : spdm_response, response_size,
2998 : response);
2999 : }
3000 1 : return LIBSPDM_STATUS_SUCCESS;
3001 :
3002 1 : case 0x17: {
3003 : spdm_key_exchange_response_t *spdm_response;
3004 : size_t dhe_key_size;
3005 : uint32_t hash_size;
3006 : size_t signature_size;
3007 : uint32_t hmac_size;
3008 : uint8_t *ptr;
3009 : void *dhe_context;
3010 : uint8_t final_key[LIBSPDM_MAX_DHE_SS_SIZE];
3011 : size_t final_key_size;
3012 : size_t opaque_key_exchange_rsp_size;
3013 : void *data;
3014 : size_t data_size;
3015 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
3016 : uint8_t *cert_buffer;
3017 : size_t cert_buffer_size;
3018 : uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
3019 : uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
3020 : uint8_t bin_str0[128];
3021 : size_t bin_str0_size;
3022 : uint8_t bin_str2[128];
3023 : size_t bin_str2_size;
3024 : uint8_t bin_str7[128];
3025 : size_t bin_str7_size;
3026 : uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
3027 : uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
3028 : uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
3029 : size_t spdm_response_size;
3030 : size_t transport_header_size;
3031 :
3032 : ((libspdm_context_t *)spdm_context)
3033 1 : ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
3034 : ((libspdm_context_t *)spdm_context)
3035 1 : ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
3036 : ((libspdm_context_t *)spdm_context)
3037 1 : ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
3038 : ((libspdm_context_t *)spdm_context)
3039 1 : ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
3040 1 : signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
3041 1 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
3042 1 : hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
3043 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
3044 1 : opaque_key_exchange_rsp_size =
3045 1 : libspdm_get_opaque_data_version_selection_data_size( spdm_context);
3046 1 : spdm_response_size = sizeof(spdm_key_exchange_response_t) +
3047 1 : dhe_key_size + 0 + sizeof(uint16_t) +
3048 1 : opaque_key_exchange_rsp_size + signature_size +
3049 : hmac_size;
3050 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
3051 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
3052 1 : libspdm_zero_mem(spdm_response,spdm_response_size);
3053 :
3054 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
3055 1 : spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
3056 1 : spdm_response->header.param1 = 0;
3057 1 : spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
3058 1 : spdm_response->mut_auth_requested =
3059 : SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST;
3060 1 : spdm_response->req_slot_id_param = 0x0;
3061 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
3062 1 : ptr = (void *)(spdm_response + 1);
3063 1 : dhe_context = libspdm_dhe_new(
3064 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
3065 : m_libspdm_use_dhe_algo,
3066 : true);
3067 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
3068 1 : final_key_size = sizeof(final_key);
3069 1 : libspdm_dhe_compute_key(
3070 : m_libspdm_use_dhe_algo, dhe_context,
3071 1 : (uint8_t *)&m_libspdm_local_buffer[0] +
3072 : sizeof(spdm_key_exchange_request_t),
3073 : dhe_key_size, final_key, &final_key_size);
3074 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
3075 1 : ptr += dhe_key_size;
3076 : /* libspdm_zero_mem (ptr, hash_size);
3077 : * ptr += hash_size;*/
3078 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_rsp_size);
3079 1 : ptr += sizeof(uint16_t);
3080 1 : libspdm_build_opaque_data_version_selection_data(
3081 : spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
3082 : &opaque_key_exchange_rsp_size, ptr);
3083 1 : ptr += opaque_key_exchange_rsp_size;
3084 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
3085 : m_libspdm_use_asym_algo, &data,
3086 : &data_size, NULL, NULL)) {
3087 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
3088 : }
3089 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
3090 : sizeof(m_libspdm_local_buffer)
3091 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
3092 : m_libspdm_local_buffer),
3093 1 : spdm_response, (size_t)ptr - (size_t)spdm_response);
3094 1 : m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
3095 1 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
3096 : m_libspdm_local_buffer_size));
3097 1 : libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
3098 1 : libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
3099 1 : cert_buffer = (uint8_t *)data;
3100 1 : cert_buffer_size = data_size;
3101 1 : libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size, cert_buffer_hash);
3102 : /* transcript.message_a size is 0*/
3103 1 : libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
3104 1 : libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
3105 : m_libspdm_local_buffer_size);
3106 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
3107 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
3108 1 : free(data);
3109 1 : libspdm_responder_data_sign(
3110 : spdm_context,
3111 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
3112 : 0, SPDM_KEY_EXCHANGE_RSP,
3113 : m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
3114 1 : false, libspdm_get_managed_buffer(&th_curr),
3115 : libspdm_get_managed_buffer_size(&th_curr), ptr,
3116 : &signature_size);
3117 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
3118 : sizeof(m_libspdm_local_buffer)
3119 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
3120 : m_libspdm_local_buffer),
3121 : ptr, signature_size);
3122 1 : m_libspdm_local_buffer_size += signature_size;
3123 1 : libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
3124 1 : ptr += signature_size;
3125 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
3126 : libspdm_get_managed_buffer_size(&th_curr),
3127 : th_curr_hash_data);
3128 1 : bin_str0_size = sizeof(bin_str0);
3129 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
3130 : SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
3131 1 : NULL, (uint16_t)hash_size, hash_size, bin_str0,
3132 : &bin_str0_size);
3133 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
3134 : m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
3135 1 : bin_str2_size = sizeof(bin_str2);
3136 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
3137 : SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
3138 1 : th_curr_hash_data, (uint16_t)hash_size, hash_size,
3139 : bin_str2, &bin_str2_size);
3140 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
3141 : bin_str2, bin_str2_size,
3142 : response_handshake_secret, hash_size);
3143 1 : bin_str7_size = sizeof(bin_str7);
3144 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
3145 : SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
3146 1 : NULL, (uint16_t)hash_size, hash_size, bin_str7,
3147 : &bin_str7_size);
3148 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
3149 : hash_size, bin_str7, bin_str7_size,
3150 : response_finished_key, hash_size);
3151 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
3152 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
3153 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
3154 : response_finished_key, hash_size, ptr);
3155 1 : ptr += hmac_size;
3156 :
3157 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
3158 : false, spdm_response_size,
3159 : spdm_response, response_size,
3160 : response);
3161 : }
3162 1 : return LIBSPDM_STATUS_SUCCESS;
3163 :
3164 0 : case 0x18: {
3165 : spdm_key_exchange_response_t *spdm_response;
3166 : size_t dhe_key_size;
3167 : uint32_t hash_size;
3168 : size_t signature_size;
3169 : uint32_t hmac_size;
3170 : uint8_t *ptr;
3171 : void *dhe_context;
3172 : uint8_t final_key[LIBSPDM_MAX_DHE_SS_SIZE];
3173 : size_t final_key_size;
3174 : size_t opaque_key_exchange_rsp_size;
3175 : void *data;
3176 : size_t data_size;
3177 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
3178 : uint8_t *cert_buffer;
3179 : size_t cert_buffer_size;
3180 : uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
3181 : uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
3182 : uint8_t bin_str0[128];
3183 : size_t bin_str0_size;
3184 : uint8_t bin_str2[128];
3185 : size_t bin_str2_size;
3186 : uint8_t bin_str7[128];
3187 : size_t bin_str7_size;
3188 : uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
3189 : uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
3190 : uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
3191 : size_t spdm_response_size;
3192 : size_t transport_header_size;
3193 :
3194 : ((libspdm_context_t *)spdm_context)
3195 0 : ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
3196 : ((libspdm_context_t *)spdm_context)
3197 0 : ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
3198 : ((libspdm_context_t *)spdm_context)
3199 0 : ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
3200 : ((libspdm_context_t *)spdm_context)
3201 0 : ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
3202 0 : signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
3203 0 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
3204 0 : hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
3205 0 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
3206 0 : opaque_key_exchange_rsp_size =
3207 0 : libspdm_get_opaque_data_version_selection_data_size( spdm_context);
3208 0 : spdm_response_size = sizeof(spdm_key_exchange_response_t) +
3209 0 : dhe_key_size + 0 + sizeof(uint16_t) +
3210 0 : opaque_key_exchange_rsp_size + signature_size +
3211 : hmac_size;
3212 0 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
3213 0 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
3214 0 : libspdm_zero_mem(spdm_response,spdm_response_size);
3215 :
3216 0 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
3217 0 : spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
3218 0 : spdm_response->header.param1 = 0;
3219 0 : spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
3220 0 : spdm_response->mut_auth_requested =
3221 : SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST;
3222 0 : spdm_response->req_slot_id_param = 0xF;
3223 0 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
3224 0 : ptr = (void *)(spdm_response + 1);
3225 0 : dhe_context = libspdm_dhe_new(
3226 0 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
3227 : m_libspdm_use_dhe_algo,
3228 : true);
3229 0 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
3230 0 : final_key_size = sizeof(final_key);
3231 0 : libspdm_dhe_compute_key(
3232 : m_libspdm_use_dhe_algo, dhe_context,
3233 0 : (uint8_t *)&m_libspdm_local_buffer[0] +
3234 : sizeof(spdm_key_exchange_request_t),
3235 : dhe_key_size, final_key, &final_key_size);
3236 0 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
3237 0 : ptr += dhe_key_size;
3238 : /* libspdm_zero_mem (ptr, hash_size);
3239 : * ptr += hash_size;*/
3240 0 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_rsp_size);
3241 0 : ptr += sizeof(uint16_t);
3242 0 : libspdm_build_opaque_data_version_selection_data(
3243 : spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
3244 : &opaque_key_exchange_rsp_size, ptr);
3245 0 : ptr += opaque_key_exchange_rsp_size;
3246 0 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
3247 : m_libspdm_use_asym_algo, &data,
3248 : &data_size, NULL, NULL)) {
3249 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
3250 : }
3251 0 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
3252 : sizeof(m_libspdm_local_buffer)
3253 0 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
3254 : m_libspdm_local_buffer),
3255 0 : spdm_response, (size_t)ptr - (size_t)spdm_response);
3256 0 : m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
3257 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
3258 : m_libspdm_local_buffer_size));
3259 0 : libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
3260 0 : libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
3261 0 : cert_buffer = (uint8_t *)data;
3262 0 : cert_buffer_size = data_size;
3263 0 : libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size, cert_buffer_hash);
3264 : /* transcript.message_a size is 0*/
3265 0 : libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
3266 0 : libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
3267 : m_libspdm_local_buffer_size);
3268 0 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
3269 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
3270 0 : free(data);
3271 0 : libspdm_responder_data_sign(
3272 : spdm_context,
3273 0 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
3274 : 0, SPDM_KEY_EXCHANGE_RSP,
3275 : m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
3276 0 : false, libspdm_get_managed_buffer(&th_curr),
3277 : libspdm_get_managed_buffer_size(&th_curr), ptr,
3278 : &signature_size);
3279 0 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
3280 : sizeof(m_libspdm_local_buffer)
3281 0 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
3282 : m_libspdm_local_buffer),
3283 : ptr, signature_size);
3284 0 : m_libspdm_local_buffer_size += signature_size;
3285 0 : libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
3286 0 : ptr += signature_size;
3287 0 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
3288 : libspdm_get_managed_buffer_size(&th_curr),
3289 : th_curr_hash_data);
3290 0 : bin_str0_size = sizeof(bin_str0);
3291 0 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
3292 : SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
3293 0 : NULL, (uint16_t)hash_size, hash_size, bin_str0,
3294 : &bin_str0_size);
3295 0 : libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
3296 : m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
3297 0 : bin_str2_size = sizeof(bin_str2);
3298 0 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
3299 : SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
3300 0 : th_curr_hash_data, (uint16_t)hash_size, hash_size,
3301 : bin_str2, &bin_str2_size);
3302 0 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
3303 : bin_str2, bin_str2_size,
3304 : response_handshake_secret, hash_size);
3305 0 : bin_str7_size = sizeof(bin_str7);
3306 0 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
3307 : SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
3308 0 : NULL, (uint16_t)hash_size, hash_size, bin_str7,
3309 : &bin_str7_size);
3310 0 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
3311 : hash_size, bin_str7, bin_str7_size,
3312 : response_finished_key, hash_size);
3313 0 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
3314 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
3315 0 : libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
3316 : response_finished_key, hash_size, ptr);
3317 0 : ptr += hmac_size;
3318 :
3319 0 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
3320 : false, spdm_response_size,
3321 : spdm_response, response_size,
3322 : response);
3323 : }
3324 0 : return LIBSPDM_STATUS_SUCCESS;
3325 :
3326 0 : case 0x19: {
3327 : spdm_key_exchange_response_t *spdm_response;
3328 : size_t dhe_key_size;
3329 : uint32_t hash_size;
3330 : size_t signature_size;
3331 : uint32_t hmac_size;
3332 : uint8_t *ptr;
3333 : void *dhe_context;
3334 : uint8_t final_key[LIBSPDM_MAX_DHE_SS_SIZE];
3335 : size_t final_key_size;
3336 : size_t opaque_key_exchange_rsp_size;
3337 : void *data;
3338 : size_t data_size;
3339 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
3340 : uint8_t *cert_buffer;
3341 : size_t cert_buffer_size;
3342 : uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
3343 : uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
3344 : uint8_t bin_str0[128];
3345 : size_t bin_str0_size;
3346 : uint8_t bin_str2[128];
3347 : size_t bin_str2_size;
3348 : uint8_t bin_str7[128];
3349 : size_t bin_str7_size;
3350 : uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
3351 : uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
3352 : uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
3353 : size_t spdm_response_size;
3354 : size_t transport_header_size;
3355 :
3356 : ((libspdm_context_t *)spdm_context)
3357 0 : ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
3358 : ((libspdm_context_t *)spdm_context)
3359 0 : ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
3360 : ((libspdm_context_t *)spdm_context)
3361 0 : ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
3362 : ((libspdm_context_t *)spdm_context)
3363 0 : ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
3364 0 : signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
3365 0 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
3366 0 : hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
3367 0 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
3368 0 : opaque_key_exchange_rsp_size =
3369 0 : libspdm_get_opaque_data_version_selection_data_size( spdm_context);
3370 0 : spdm_response_size = sizeof(spdm_key_exchange_response_t) +
3371 0 : dhe_key_size + 0 + sizeof(uint16_t) +
3372 0 : opaque_key_exchange_rsp_size + signature_size +
3373 : hmac_size;
3374 0 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
3375 0 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
3376 0 : libspdm_zero_mem(spdm_response,spdm_response_size);
3377 :
3378 0 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
3379 0 : spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
3380 0 : spdm_response->header.param1 = 0;
3381 0 : spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
3382 0 : spdm_response->mut_auth_requested =
3383 : SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_GET_DIGESTS;
3384 0 : spdm_response->req_slot_id_param = 0xF;
3385 0 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
3386 0 : ptr = (void *)(spdm_response + 1);
3387 0 : dhe_context = libspdm_dhe_new(
3388 0 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
3389 : m_libspdm_use_dhe_algo,
3390 : true);
3391 0 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
3392 0 : final_key_size = sizeof(final_key);
3393 0 : libspdm_dhe_compute_key(
3394 : m_libspdm_use_dhe_algo, dhe_context,
3395 0 : (uint8_t *)&m_libspdm_local_buffer[0] +
3396 : sizeof(spdm_key_exchange_request_t),
3397 : dhe_key_size, final_key, &final_key_size);
3398 0 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
3399 0 : ptr += dhe_key_size;
3400 : /* libspdm_zero_mem (ptr, hash_size);
3401 : * ptr += hash_size;*/
3402 0 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_rsp_size);
3403 0 : ptr += sizeof(uint16_t);
3404 0 : libspdm_build_opaque_data_version_selection_data(
3405 : spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
3406 : &opaque_key_exchange_rsp_size, ptr);
3407 0 : ptr += opaque_key_exchange_rsp_size;
3408 0 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
3409 : m_libspdm_use_asym_algo, &data,
3410 : &data_size, NULL, NULL)) {
3411 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
3412 : }
3413 0 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
3414 : sizeof(m_libspdm_local_buffer)
3415 0 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
3416 : m_libspdm_local_buffer),
3417 0 : spdm_response, (size_t)ptr - (size_t)spdm_response);
3418 0 : m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
3419 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
3420 : m_libspdm_local_buffer_size));
3421 0 : libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
3422 0 : libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
3423 0 : cert_buffer = (uint8_t *)data;
3424 0 : cert_buffer_size = data_size;
3425 0 : libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size, cert_buffer_hash);
3426 : /* transcript.message_a size is 0*/
3427 0 : libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
3428 0 : libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
3429 : m_libspdm_local_buffer_size);
3430 0 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
3431 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
3432 0 : free(data);
3433 0 : libspdm_responder_data_sign(
3434 : spdm_context,
3435 0 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
3436 : 0, SPDM_KEY_EXCHANGE_RSP,
3437 : m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
3438 0 : false, libspdm_get_managed_buffer(&th_curr),
3439 : libspdm_get_managed_buffer_size(&th_curr), ptr,
3440 : &signature_size);
3441 0 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
3442 : sizeof(m_libspdm_local_buffer)
3443 0 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
3444 : m_libspdm_local_buffer),
3445 : ptr, signature_size);
3446 0 : m_libspdm_local_buffer_size += signature_size;
3447 0 : libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
3448 0 : ptr += signature_size;
3449 0 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
3450 : libspdm_get_managed_buffer_size(&th_curr),
3451 : th_curr_hash_data);
3452 0 : bin_str0_size = sizeof(bin_str0);
3453 0 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
3454 : SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
3455 0 : NULL, (uint16_t)hash_size, hash_size, bin_str0,
3456 : &bin_str0_size);
3457 0 : libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
3458 : m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
3459 0 : bin_str2_size = sizeof(bin_str2);
3460 0 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
3461 : SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
3462 0 : th_curr_hash_data, (uint16_t)hash_size, hash_size,
3463 : bin_str2, &bin_str2_size);
3464 0 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
3465 : bin_str2, bin_str2_size,
3466 : response_handshake_secret, hash_size);
3467 0 : bin_str7_size = sizeof(bin_str7);
3468 0 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
3469 : SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
3470 0 : NULL, (uint16_t)hash_size, hash_size, bin_str7,
3471 : &bin_str7_size);
3472 0 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
3473 : hash_size, bin_str7, bin_str7_size,
3474 : response_finished_key, hash_size);
3475 0 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
3476 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
3477 0 : libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
3478 : response_finished_key, hash_size, ptr);
3479 0 : ptr += hmac_size;
3480 :
3481 0 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
3482 : false, spdm_response_size,
3483 : spdm_response, response_size,
3484 : response);
3485 : }
3486 0 : return LIBSPDM_STATUS_SUCCESS;
3487 :
3488 1 : case 0x1A: {
3489 : spdm_key_exchange_response_t *spdm_response;
3490 : size_t dhe_key_size;
3491 : uint32_t hash_size;
3492 : size_t signature_size;
3493 : uint32_t hmac_size;
3494 : uint8_t *ptr;
3495 : void *dhe_context;
3496 : uint8_t final_key[LIBSPDM_MAX_DHE_SS_SIZE];
3497 : size_t final_key_size;
3498 : size_t opaque_key_exchange_rsp_size;
3499 : void *data;
3500 : size_t data_size;
3501 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
3502 : uint8_t *cert_buffer;
3503 : size_t cert_buffer_size;
3504 : uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
3505 : uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
3506 : uint8_t bin_str0[128];
3507 : size_t bin_str0_size;
3508 : uint8_t bin_str2[128];
3509 : size_t bin_str2_size;
3510 : uint8_t bin_str7[128];
3511 : size_t bin_str7_size;
3512 : uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
3513 : uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
3514 : uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
3515 : size_t spdm_response_size;
3516 : size_t transport_header_size;
3517 :
3518 : ((libspdm_context_t *)spdm_context)
3519 1 : ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
3520 : ((libspdm_context_t *)spdm_context)
3521 1 : ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
3522 : ((libspdm_context_t *)spdm_context)
3523 1 : ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
3524 : ((libspdm_context_t *)spdm_context)
3525 1 : ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
3526 1 : signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
3527 1 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
3528 1 : hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
3529 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
3530 1 : opaque_key_exchange_rsp_size =
3531 1 : libspdm_get_opaque_data_version_selection_data_size( spdm_context);
3532 1 : spdm_response_size = sizeof(spdm_key_exchange_response_t) +
3533 1 : dhe_key_size + 0 + sizeof(uint16_t) +
3534 1 : opaque_key_exchange_rsp_size + signature_size +
3535 : hmac_size;
3536 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
3537 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
3538 1 : libspdm_zero_mem(spdm_response,spdm_response_size);
3539 :
3540 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
3541 1 : spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
3542 1 : spdm_response->header.param1 = 0;
3543 1 : spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
3544 : /* Illegal combination. At most one bit can be set. */
3545 1 : spdm_response->mut_auth_requested =
3546 : SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED |
3547 : SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST;
3548 1 : spdm_response->req_slot_id_param = 0xF;
3549 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
3550 1 : ptr = (void *)(spdm_response + 1);
3551 1 : dhe_context = libspdm_dhe_new(
3552 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
3553 : m_libspdm_use_dhe_algo,
3554 : true);
3555 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
3556 1 : final_key_size = sizeof(final_key);
3557 1 : libspdm_dhe_compute_key(
3558 : m_libspdm_use_dhe_algo, dhe_context,
3559 1 : (uint8_t *)&m_libspdm_local_buffer[0] +
3560 : sizeof(spdm_key_exchange_request_t),
3561 : dhe_key_size, final_key, &final_key_size);
3562 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
3563 1 : ptr += dhe_key_size;
3564 : /* libspdm_zero_mem (ptr, hash_size);
3565 : * ptr += hash_size;*/
3566 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_rsp_size);
3567 1 : ptr += sizeof(uint16_t);
3568 1 : libspdm_build_opaque_data_version_selection_data(
3569 : spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
3570 : &opaque_key_exchange_rsp_size, ptr);
3571 1 : ptr += opaque_key_exchange_rsp_size;
3572 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
3573 : m_libspdm_use_asym_algo, &data,
3574 : &data_size, NULL, NULL)) {
3575 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
3576 : }
3577 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
3578 : sizeof(m_libspdm_local_buffer)
3579 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
3580 : m_libspdm_local_buffer),
3581 1 : spdm_response, (size_t)ptr - (size_t)spdm_response);
3582 1 : m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
3583 1 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
3584 : m_libspdm_local_buffer_size));
3585 1 : libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
3586 1 : libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
3587 1 : cert_buffer = (uint8_t *)data;
3588 1 : cert_buffer_size = data_size;
3589 1 : libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size, cert_buffer_hash);
3590 : /* transcript.message_a size is 0*/
3591 1 : libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
3592 1 : libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
3593 : m_libspdm_local_buffer_size);
3594 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
3595 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
3596 1 : free(data);
3597 1 : libspdm_responder_data_sign(
3598 : spdm_context,
3599 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
3600 : 0, SPDM_KEY_EXCHANGE_RSP,
3601 : m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
3602 1 : false, libspdm_get_managed_buffer(&th_curr),
3603 : libspdm_get_managed_buffer_size(&th_curr), ptr,
3604 : &signature_size);
3605 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
3606 : sizeof(m_libspdm_local_buffer)
3607 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
3608 : m_libspdm_local_buffer),
3609 : ptr, signature_size);
3610 1 : m_libspdm_local_buffer_size += signature_size;
3611 1 : libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
3612 1 : ptr += signature_size;
3613 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
3614 : libspdm_get_managed_buffer_size(&th_curr),
3615 : th_curr_hash_data);
3616 1 : bin_str0_size = sizeof(bin_str0);
3617 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
3618 : SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
3619 1 : NULL, (uint16_t)hash_size, hash_size, bin_str0,
3620 : &bin_str0_size);
3621 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
3622 : m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
3623 1 : bin_str2_size = sizeof(bin_str2);
3624 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
3625 : SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
3626 1 : th_curr_hash_data, (uint16_t)hash_size, hash_size,
3627 : bin_str2, &bin_str2_size);
3628 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
3629 : bin_str2, bin_str2_size,
3630 : response_handshake_secret, hash_size);
3631 1 : bin_str7_size = sizeof(bin_str7);
3632 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
3633 : SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
3634 1 : NULL, (uint16_t)hash_size, hash_size, bin_str7,
3635 : &bin_str7_size);
3636 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
3637 : hash_size, bin_str7, bin_str7_size,
3638 : response_finished_key, hash_size);
3639 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
3640 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
3641 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
3642 : response_finished_key, hash_size, ptr);
3643 1 : ptr += hmac_size;
3644 :
3645 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
3646 : false, spdm_response_size,
3647 : spdm_response, response_size,
3648 : response);
3649 : }
3650 1 : return LIBSPDM_STATUS_SUCCESS;
3651 :
3652 1 : case 0x1B: {
3653 : spdm_key_exchange_response_t *spdm_response;
3654 : size_t dhe_key_size;
3655 : uint32_t hash_size;
3656 : size_t signature_size;
3657 : uint32_t hmac_size;
3658 : uint8_t *ptr;
3659 : void *dhe_context;
3660 : uint8_t final_key[LIBSPDM_MAX_DHE_SS_SIZE];
3661 : size_t final_key_size;
3662 : size_t opaque_key_exchange_rsp_size;
3663 : void *data;
3664 : size_t data_size;
3665 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
3666 : uint8_t *cert_buffer;
3667 : size_t cert_buffer_size;
3668 : uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
3669 : uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
3670 : uint8_t bin_str0[128];
3671 : size_t bin_str0_size;
3672 : uint8_t bin_str2[128];
3673 : size_t bin_str2_size;
3674 : uint8_t bin_str7[128];
3675 : size_t bin_str7_size;
3676 : uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
3677 : uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
3678 : uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
3679 : size_t spdm_response_size;
3680 : size_t transport_header_size;
3681 :
3682 : ((libspdm_context_t *)spdm_context)
3683 1 : ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
3684 : ((libspdm_context_t *)spdm_context)
3685 1 : ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
3686 : ((libspdm_context_t *)spdm_context)
3687 1 : ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
3688 : ((libspdm_context_t *)spdm_context)
3689 1 : ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
3690 1 : signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
3691 1 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
3692 1 : hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
3693 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
3694 1 : opaque_key_exchange_rsp_size =
3695 1 : libspdm_get_opaque_data_version_selection_data_size( spdm_context);
3696 1 : spdm_response_size = sizeof(spdm_key_exchange_response_t) +
3697 1 : dhe_key_size + 0 + sizeof(uint16_t) +
3698 1 : opaque_key_exchange_rsp_size + signature_size +
3699 : hmac_size;
3700 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
3701 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
3702 1 : libspdm_zero_mem(spdm_response,spdm_response_size);
3703 :
3704 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
3705 1 : spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
3706 1 : spdm_response->header.param1 = 0;
3707 1 : spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
3708 : /* Illegal combination. At most one bit can be set. */
3709 1 : spdm_response->mut_auth_requested =
3710 : SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED |
3711 : SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_GET_DIGESTS;
3712 1 : spdm_response->req_slot_id_param = 0xF;
3713 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
3714 1 : ptr = (void *)(spdm_response + 1);
3715 1 : dhe_context = libspdm_dhe_new(
3716 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
3717 : m_libspdm_use_dhe_algo,
3718 : true);
3719 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
3720 1 : final_key_size = sizeof(final_key);
3721 1 : libspdm_dhe_compute_key(
3722 : m_libspdm_use_dhe_algo, dhe_context,
3723 1 : (uint8_t *)&m_libspdm_local_buffer[0] +
3724 : sizeof(spdm_key_exchange_request_t),
3725 : dhe_key_size, final_key, &final_key_size);
3726 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
3727 1 : ptr += dhe_key_size;
3728 : /* libspdm_zero_mem (ptr, hash_size);
3729 : * ptr += hash_size;*/
3730 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_rsp_size);
3731 1 : ptr += sizeof(uint16_t);
3732 1 : libspdm_build_opaque_data_version_selection_data(
3733 : spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
3734 : &opaque_key_exchange_rsp_size, ptr);
3735 1 : ptr += opaque_key_exchange_rsp_size;
3736 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
3737 : m_libspdm_use_asym_algo, &data,
3738 : &data_size, NULL, NULL)) {
3739 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
3740 : }
3741 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
3742 : sizeof(m_libspdm_local_buffer)
3743 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
3744 : m_libspdm_local_buffer),
3745 1 : spdm_response, (size_t)ptr - (size_t)spdm_response);
3746 1 : m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
3747 1 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
3748 : m_libspdm_local_buffer_size));
3749 1 : libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
3750 1 : libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
3751 1 : cert_buffer = (uint8_t *)data;
3752 1 : cert_buffer_size = data_size;
3753 1 : libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size, cert_buffer_hash);
3754 : /* transcript.message_a size is 0*/
3755 1 : libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
3756 1 : libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
3757 : m_libspdm_local_buffer_size);
3758 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
3759 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
3760 1 : free(data);
3761 1 : libspdm_responder_data_sign(
3762 : spdm_context,
3763 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
3764 : 0, SPDM_KEY_EXCHANGE_RSP,
3765 : m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
3766 1 : false, libspdm_get_managed_buffer(&th_curr),
3767 : libspdm_get_managed_buffer_size(&th_curr), ptr,
3768 : &signature_size);
3769 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
3770 : sizeof(m_libspdm_local_buffer)
3771 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
3772 : m_libspdm_local_buffer),
3773 : ptr, signature_size);
3774 1 : m_libspdm_local_buffer_size += signature_size;
3775 1 : libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
3776 1 : ptr += signature_size;
3777 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
3778 : libspdm_get_managed_buffer_size(&th_curr),
3779 : th_curr_hash_data);
3780 1 : bin_str0_size = sizeof(bin_str0);
3781 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
3782 : SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
3783 1 : NULL, (uint16_t)hash_size, hash_size, bin_str0,
3784 : &bin_str0_size);
3785 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
3786 : m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
3787 1 : bin_str2_size = sizeof(bin_str2);
3788 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
3789 : SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
3790 1 : th_curr_hash_data, (uint16_t)hash_size, hash_size,
3791 : bin_str2, &bin_str2_size);
3792 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
3793 : bin_str2, bin_str2_size,
3794 : response_handshake_secret, hash_size);
3795 1 : bin_str7_size = sizeof(bin_str7);
3796 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
3797 : SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
3798 1 : NULL, (uint16_t)hash_size, hash_size, bin_str7,
3799 : &bin_str7_size);
3800 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
3801 : hash_size, bin_str7, bin_str7_size,
3802 : response_finished_key, hash_size);
3803 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
3804 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
3805 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
3806 : response_finished_key, hash_size, ptr);
3807 1 : ptr += hmac_size;
3808 :
3809 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
3810 : false, spdm_response_size,
3811 : spdm_response, response_size,
3812 : response);
3813 : }
3814 1 : return LIBSPDM_STATUS_SUCCESS;
3815 :
3816 1 : case 0x1C: {
3817 : spdm_key_exchange_response_t *spdm_response;
3818 : size_t dhe_key_size;
3819 : uint32_t hash_size;
3820 : size_t signature_size;
3821 : uint32_t hmac_size;
3822 : uint8_t *ptr;
3823 : void *dhe_context;
3824 : uint8_t final_key[LIBSPDM_MAX_DHE_SS_SIZE];
3825 : size_t final_key_size;
3826 : size_t opaque_key_exchange_rsp_size;
3827 : void *data;
3828 : size_t data_size;
3829 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
3830 : uint8_t *cert_buffer;
3831 : size_t cert_buffer_size;
3832 : uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
3833 : uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
3834 : uint8_t bin_str0[128];
3835 : size_t bin_str0_size;
3836 : uint8_t bin_str2[128];
3837 : size_t bin_str2_size;
3838 : uint8_t bin_str7[128];
3839 : size_t bin_str7_size;
3840 : uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
3841 : uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
3842 : uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
3843 : size_t spdm_response_size;
3844 : size_t transport_header_size;
3845 :
3846 : ((libspdm_context_t *)spdm_context)
3847 1 : ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
3848 : ((libspdm_context_t *)spdm_context)
3849 1 : ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
3850 : ((libspdm_context_t *)spdm_context)
3851 1 : ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
3852 : ((libspdm_context_t *)spdm_context)
3853 1 : ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
3854 1 : signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
3855 1 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
3856 1 : hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
3857 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
3858 1 : opaque_key_exchange_rsp_size =
3859 1 : libspdm_get_opaque_data_version_selection_data_size( spdm_context);
3860 1 : spdm_response_size = sizeof(spdm_key_exchange_response_t) +
3861 1 : dhe_key_size + 0 + sizeof(uint16_t) +
3862 1 : opaque_key_exchange_rsp_size + signature_size +
3863 : hmac_size;
3864 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
3865 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
3866 1 : libspdm_zero_mem(spdm_response,spdm_response_size);
3867 :
3868 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
3869 1 : spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
3870 1 : spdm_response->header.param1 = 0;
3871 1 : spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
3872 : /* Illegal combination. At most one bit can be set. */
3873 1 : spdm_response->mut_auth_requested =
3874 : SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_ENCAP_REQUEST |
3875 : SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED_WITH_GET_DIGESTS;
3876 1 : spdm_response->req_slot_id_param = 0xF;
3877 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
3878 1 : ptr = (void *)(spdm_response + 1);
3879 1 : dhe_context = libspdm_dhe_new(
3880 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
3881 : m_libspdm_use_dhe_algo,
3882 : true);
3883 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
3884 1 : final_key_size = sizeof(final_key);
3885 1 : libspdm_dhe_compute_key(
3886 : m_libspdm_use_dhe_algo, dhe_context,
3887 1 : (uint8_t *)&m_libspdm_local_buffer[0] +
3888 : sizeof(spdm_key_exchange_request_t),
3889 : dhe_key_size, final_key, &final_key_size);
3890 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
3891 1 : ptr += dhe_key_size;
3892 : /* libspdm_zero_mem (ptr, hash_size);
3893 : * ptr += hash_size;*/
3894 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_rsp_size);
3895 1 : ptr += sizeof(uint16_t);
3896 1 : libspdm_build_opaque_data_version_selection_data(
3897 : spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
3898 : &opaque_key_exchange_rsp_size, ptr);
3899 1 : ptr += opaque_key_exchange_rsp_size;
3900 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
3901 : m_libspdm_use_asym_algo, &data,
3902 : &data_size, NULL, NULL)) {
3903 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
3904 : }
3905 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
3906 : sizeof(m_libspdm_local_buffer)
3907 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
3908 : m_libspdm_local_buffer),
3909 1 : spdm_response, (size_t)ptr - (size_t)spdm_response);
3910 1 : m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
3911 1 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
3912 : m_libspdm_local_buffer_size));
3913 1 : libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
3914 1 : libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
3915 1 : cert_buffer = (uint8_t *)data;
3916 1 : cert_buffer_size = data_size;
3917 1 : libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size, cert_buffer_hash);
3918 : /* transcript.message_a size is 0*/
3919 1 : libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
3920 1 : libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
3921 : m_libspdm_local_buffer_size);
3922 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
3923 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
3924 1 : free(data);
3925 1 : libspdm_responder_data_sign(
3926 : spdm_context,
3927 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
3928 : 0, SPDM_KEY_EXCHANGE_RSP,
3929 : m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
3930 1 : false, libspdm_get_managed_buffer(&th_curr),
3931 : libspdm_get_managed_buffer_size(&th_curr), ptr,
3932 : &signature_size);
3933 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
3934 : sizeof(m_libspdm_local_buffer)
3935 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
3936 : m_libspdm_local_buffer),
3937 : ptr, signature_size);
3938 1 : m_libspdm_local_buffer_size += signature_size;
3939 1 : libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
3940 1 : ptr += signature_size;
3941 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
3942 : libspdm_get_managed_buffer_size(&th_curr),
3943 : th_curr_hash_data);
3944 1 : bin_str0_size = sizeof(bin_str0);
3945 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
3946 : SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
3947 1 : NULL, (uint16_t)hash_size, hash_size, bin_str0,
3948 : &bin_str0_size);
3949 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
3950 : m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
3951 1 : bin_str2_size = sizeof(bin_str2);
3952 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
3953 : SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
3954 1 : th_curr_hash_data, (uint16_t)hash_size, hash_size,
3955 : bin_str2, &bin_str2_size);
3956 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
3957 : bin_str2, bin_str2_size,
3958 : response_handshake_secret, hash_size);
3959 1 : bin_str7_size = sizeof(bin_str7);
3960 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
3961 : SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
3962 1 : NULL, (uint16_t)hash_size, hash_size, bin_str7,
3963 : &bin_str7_size);
3964 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
3965 : hash_size, bin_str7, bin_str7_size,
3966 : response_finished_key, hash_size);
3967 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
3968 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
3969 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
3970 : response_finished_key, hash_size, ptr);
3971 1 : ptr += hmac_size;
3972 :
3973 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
3974 : false, spdm_response_size,
3975 : spdm_response, response_size,
3976 : response);
3977 : }
3978 1 : return LIBSPDM_STATUS_SUCCESS;
3979 :
3980 0 : case 0x1D: {
3981 : spdm_key_exchange_response_t *spdm_response;
3982 : size_t dhe_key_size;
3983 : uint32_t hash_size;
3984 : size_t signature_size;
3985 : uint32_t hmac_size;
3986 : uint8_t *ptr;
3987 : void *dhe_context;
3988 : uint8_t final_key[LIBSPDM_MAX_DHE_SS_SIZE];
3989 : size_t final_key_size;
3990 : size_t opaque_key_exchange_rsp_size;
3991 : void *data;
3992 : size_t data_size;
3993 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
3994 : uint8_t *cert_buffer;
3995 : size_t cert_buffer_size;
3996 : uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
3997 : uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
3998 : uint8_t bin_str0[128];
3999 : size_t bin_str0_size;
4000 : uint8_t bin_str2[128];
4001 : size_t bin_str2_size;
4002 : uint8_t bin_str7[128];
4003 : size_t bin_str7_size;
4004 : uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
4005 : uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
4006 : uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
4007 : size_t spdm_response_size;
4008 : size_t transport_header_size;
4009 :
4010 0 : ((libspdm_context_t *)spdm_context)->connection_info.algorithm.base_asym_algo =
4011 : m_libspdm_use_asym_algo;
4012 0 : ((libspdm_context_t *)spdm_context)->connection_info.algorithm.base_hash_algo =
4013 : m_libspdm_use_hash_algo;
4014 0 : ((libspdm_context_t *)spdm_context)->connection_info.algorithm.dhe_named_group =
4015 : m_libspdm_use_dhe_algo;
4016 0 : ((libspdm_context_t *)spdm_context)->connection_info.algorithm.measurement_hash_algo =
4017 : m_libspdm_use_measurement_hash_algo;
4018 0 : signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
4019 0 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
4020 0 : hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
4021 0 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
4022 0 : opaque_key_exchange_rsp_size =
4023 0 : libspdm_get_opaque_data_version_selection_data_size(spdm_context);
4024 0 : spdm_response_size = sizeof(spdm_key_exchange_response_t) +
4025 0 : dhe_key_size + 0 + sizeof(uint16_t) +
4026 0 : opaque_key_exchange_rsp_size + signature_size +
4027 : hmac_size;
4028 0 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
4029 0 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
4030 :
4031 0 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
4032 0 : spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
4033 0 : spdm_response->header.param1 = 0;
4034 0 : spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
4035 0 : spdm_response->mut_auth_requested = 0;
4036 0 : spdm_response->req_slot_id_param = 0;
4037 0 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
4038 0 : ptr = (void *)(spdm_response + 1);
4039 0 : dhe_context = libspdm_dhe_new(
4040 0 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
4041 : m_libspdm_use_dhe_algo, true);
4042 0 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
4043 0 : final_key_size = sizeof(final_key);
4044 0 : libspdm_dhe_compute_key(
4045 : m_libspdm_use_dhe_algo, dhe_context,
4046 0 : (uint8_t *)&m_libspdm_local_buffer[0] + sizeof(spdm_key_exchange_request_t),
4047 : dhe_key_size, final_key, &final_key_size);
4048 0 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
4049 0 : ptr += dhe_key_size;
4050 : /* libspdm_zero_mem (ptr, hash_size);
4051 : * ptr += hash_size;*/
4052 0 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_rsp_size);
4053 0 : ptr += sizeof(uint16_t);
4054 0 : libspdm_build_opaque_data_version_selection_data(
4055 : spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
4056 : &opaque_key_exchange_rsp_size, ptr);
4057 0 : ptr += opaque_key_exchange_rsp_size;
4058 0 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
4059 : m_libspdm_use_asym_algo, &data,
4060 : &data_size, NULL, NULL)) {
4061 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
4062 : }
4063 0 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
4064 : sizeof(m_libspdm_local_buffer) - m_libspdm_local_buffer_size,
4065 0 : spdm_response, (size_t)ptr - (size_t)spdm_response);
4066 0 : m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
4067 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer (0x%zx):\n",
4068 : m_libspdm_local_buffer_size));
4069 0 : libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
4070 0 : libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
4071 0 : cert_buffer = (uint8_t *)data;
4072 0 : cert_buffer_size = data_size;
4073 0 : libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size, cert_buffer_hash);
4074 : /* transcript.message_a size is 0*/
4075 0 : libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
4076 0 : libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
4077 : m_libspdm_local_buffer_size);
4078 0 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
4079 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
4080 0 : free(data);
4081 0 : libspdm_responder_data_sign(
4082 : spdm_context,
4083 0 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
4084 : 0, SPDM_KEY_EXCHANGE_RSP,
4085 : m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
4086 0 : false, libspdm_get_managed_buffer(&th_curr),
4087 : libspdm_get_managed_buffer_size(&th_curr), ptr, &signature_size);
4088 0 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
4089 : sizeof(m_libspdm_local_buffer) - m_libspdm_local_buffer_size,
4090 : ptr, signature_size);
4091 0 : m_libspdm_local_buffer_size += signature_size;
4092 0 : libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
4093 0 : ptr += signature_size;
4094 0 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
4095 : libspdm_get_managed_buffer_size(&th_curr), th_curr_hash_data);
4096 0 : bin_str0_size = sizeof(bin_str0);
4097 0 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
4098 : SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
4099 0 : NULL, (uint16_t)hash_size, hash_size, bin_str0, &bin_str0_size);
4100 0 : libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
4101 : m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
4102 0 : bin_str2_size = sizeof(bin_str2);
4103 0 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
4104 : SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
4105 0 : th_curr_hash_data, (uint16_t)hash_size, hash_size,
4106 : bin_str2, &bin_str2_size);
4107 0 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
4108 : bin_str2, bin_str2_size,
4109 : response_handshake_secret, hash_size);
4110 0 : bin_str7_size = sizeof(bin_str7);
4111 0 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
4112 : SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
4113 0 : NULL, (uint16_t)hash_size, hash_size, bin_str7,
4114 : &bin_str7_size);
4115 0 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
4116 : hash_size, bin_str7, bin_str7_size,
4117 : response_finished_key, hash_size);
4118 0 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
4119 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
4120 0 : libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
4121 : response_finished_key, hash_size, ptr);
4122 0 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
4123 : sizeof(m_libspdm_local_buffer) - m_libspdm_local_buffer_size,
4124 : ptr, hmac_size);
4125 0 : m_libspdm_local_buffer_size += hmac_size;
4126 0 : ptr += hmac_size;
4127 :
4128 0 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
4129 : false, spdm_response_size,
4130 : spdm_response, response_size,
4131 : response);
4132 : }
4133 0 : return LIBSPDM_STATUS_SUCCESS;
4134 0 : case 0x1E: {
4135 : spdm_key_exchange_response_t *spdm_response;
4136 : size_t dhe_key_size;
4137 : uint32_t hash_size;
4138 : size_t signature_size;
4139 : uint32_t hmac_size;
4140 : uint8_t *ptr;
4141 : void *dhe_context;
4142 : uint8_t final_key[LIBSPDM_MAX_DHE_SS_SIZE];
4143 : size_t final_key_size;
4144 : size_t opaque_key_exchange_rsp_size;
4145 : void *data;
4146 : size_t data_size;
4147 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
4148 : uint8_t *cert_buffer;
4149 : size_t cert_buffer_size;
4150 : uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
4151 : uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
4152 : uint8_t bin_str0[128];
4153 : size_t bin_str0_size;
4154 : uint8_t bin_str2[128];
4155 : size_t bin_str2_size;
4156 : uint8_t bin_str7[128];
4157 : size_t bin_str7_size;
4158 : uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
4159 : uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
4160 : uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
4161 : size_t spdm_response_size;
4162 : size_t transport_header_size;
4163 :
4164 :
4165 : ((libspdm_context_t *)spdm_context)
4166 0 : ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
4167 : ((libspdm_context_t *)spdm_context)
4168 0 : ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
4169 : ((libspdm_context_t *)spdm_context)
4170 0 : ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
4171 : ((libspdm_context_t *)spdm_context)
4172 0 : ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
4173 0 : signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
4174 0 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
4175 0 : hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
4176 0 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
4177 0 : opaque_key_exchange_rsp_size =
4178 0 : libspdm_get_opaque_data_version_selection_data_size( spdm_context);
4179 0 : spdm_response_size = sizeof(spdm_key_exchange_response_t) +
4180 0 : dhe_key_size + 0 + sizeof(uint16_t) +
4181 0 : opaque_key_exchange_rsp_size + signature_size +
4182 : hmac_size;
4183 0 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
4184 0 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
4185 :
4186 0 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_12;
4187 0 : spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
4188 0 : spdm_response->header.param1 = 0;
4189 0 : spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
4190 0 : spdm_response->mut_auth_requested = 0;
4191 0 : spdm_response->req_slot_id_param = 0;
4192 0 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
4193 0 : ptr = (void *)(spdm_response + 1);
4194 0 : dhe_context = libspdm_dhe_new(
4195 0 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
4196 : m_libspdm_use_dhe_algo,
4197 : true);
4198 0 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
4199 0 : final_key_size = sizeof(final_key);
4200 0 : libspdm_dhe_compute_key(
4201 : m_libspdm_use_dhe_algo, dhe_context,
4202 0 : (uint8_t *)&m_libspdm_local_buffer[0] +
4203 : sizeof(spdm_key_exchange_request_t),
4204 : dhe_key_size, final_key, &final_key_size);
4205 0 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
4206 0 : ptr += dhe_key_size;
4207 : /* libspdm_zero_mem (ptr, hash_size);
4208 : * ptr += hash_size;*/
4209 0 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_rsp_size);
4210 0 : ptr += sizeof(uint16_t);
4211 0 : libspdm_build_opaque_data_version_selection_data(
4212 : spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
4213 : &opaque_key_exchange_rsp_size, ptr);
4214 0 : ptr += opaque_key_exchange_rsp_size;
4215 0 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
4216 : m_libspdm_use_asym_algo, &data,
4217 : &data_size, NULL, NULL)) {
4218 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
4219 : }
4220 0 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
4221 : sizeof(m_libspdm_local_buffer)
4222 0 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
4223 : m_libspdm_local_buffer),
4224 0 : spdm_response, (size_t)ptr - (size_t)spdm_response);
4225 0 : m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
4226 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
4227 : m_libspdm_local_buffer_size));
4228 0 : libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
4229 0 : libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
4230 0 : cert_buffer = (uint8_t *)data;
4231 0 : cert_buffer_size = data_size;
4232 0 : libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size, cert_buffer_hash);
4233 : /* transcript.message_a size is 0*/
4234 0 : libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
4235 0 : libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
4236 : m_libspdm_local_buffer_size);
4237 0 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
4238 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
4239 0 : free(data);
4240 0 : libspdm_responder_data_sign(
4241 : spdm_context,
4242 0 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
4243 : 0, SPDM_KEY_EXCHANGE_RSP,
4244 : m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
4245 0 : false, libspdm_get_managed_buffer(&th_curr),
4246 : libspdm_get_managed_buffer_size(&th_curr), ptr,
4247 : &signature_size);
4248 0 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
4249 : sizeof(m_libspdm_local_buffer)
4250 0 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
4251 : m_libspdm_local_buffer),
4252 : ptr, signature_size);
4253 0 : m_libspdm_local_buffer_size += signature_size;
4254 0 : libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
4255 0 : ptr += signature_size;
4256 0 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
4257 : libspdm_get_managed_buffer_size(&th_curr),
4258 : th_curr_hash_data);
4259 0 : bin_str0_size = sizeof(bin_str0);
4260 0 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
4261 : SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
4262 0 : NULL, (uint16_t)hash_size, hash_size, bin_str0,
4263 : &bin_str0_size);
4264 0 : libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
4265 : m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
4266 0 : bin_str2_size = sizeof(bin_str2);
4267 0 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
4268 : SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
4269 0 : th_curr_hash_data, (uint16_t)hash_size, hash_size,
4270 : bin_str2, &bin_str2_size);
4271 0 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
4272 : bin_str2, bin_str2_size,
4273 : response_handshake_secret, hash_size);
4274 0 : bin_str7_size = sizeof(bin_str7);
4275 0 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
4276 : SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
4277 0 : NULL, (uint16_t)hash_size, hash_size, bin_str7,
4278 : &bin_str7_size);
4279 0 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
4280 : hash_size, bin_str7, bin_str7_size,
4281 : response_finished_key, hash_size);
4282 0 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
4283 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
4284 0 : libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
4285 : response_finished_key, hash_size, ptr);
4286 0 : ptr += hmac_size;
4287 :
4288 0 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
4289 : false, spdm_response_size,
4290 : spdm_response, response_size,
4291 : response);
4292 : }
4293 0 : return LIBSPDM_STATUS_SUCCESS;
4294 :
4295 1 : case 0x1F: {
4296 : spdm_key_exchange_response_t *spdm_response;
4297 : size_t dhe_key_size;
4298 : uint32_t hash_size;
4299 : size_t signature_size;
4300 : uint32_t hmac_size;
4301 : uint8_t *ptr;
4302 : void *dhe_context;
4303 : uint8_t final_key[LIBSPDM_MAX_DHE_SS_SIZE];
4304 : size_t final_key_size;
4305 : size_t opaque_key_exchange_rsp_size;
4306 : void *data;
4307 : size_t data_size;
4308 : uint8_t hash_data[LIBSPDM_MAX_HASH_SIZE];
4309 : uint8_t *cert_buffer;
4310 : size_t cert_buffer_size;
4311 : uint8_t cert_buffer_hash[LIBSPDM_MAX_HASH_SIZE];
4312 : uint8_t th_curr_hash_data[LIBSPDM_MAX_HASH_SIZE];
4313 : uint8_t bin_str0[128];
4314 : size_t bin_str0_size;
4315 : uint8_t bin_str2[128];
4316 : size_t bin_str2_size;
4317 : uint8_t bin_str7[128];
4318 : size_t bin_str7_size;
4319 : uint8_t handshake_secret[LIBSPDM_MAX_HASH_SIZE];
4320 : uint8_t response_handshake_secret[LIBSPDM_MAX_HASH_SIZE];
4321 : uint8_t response_finished_key[LIBSPDM_MAX_HASH_SIZE];
4322 : size_t spdm_response_size;
4323 : size_t transport_header_size;
4324 :
4325 : ((libspdm_context_t *)spdm_context)
4326 1 : ->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
4327 : ((libspdm_context_t *)spdm_context)
4328 1 : ->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
4329 : ((libspdm_context_t *)spdm_context)
4330 1 : ->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
4331 : ((libspdm_context_t *)spdm_context)
4332 1 : ->connection_info.algorithm.measurement_hash_algo = m_libspdm_use_measurement_hash_algo;
4333 1 : signature_size = libspdm_get_asym_signature_size(m_libspdm_use_asym_algo);
4334 1 : hash_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
4335 1 : hmac_size = libspdm_get_hash_size(m_libspdm_use_hash_algo);
4336 1 : dhe_key_size = libspdm_get_dhe_pub_key_size(m_libspdm_use_dhe_algo);
4337 1 : opaque_key_exchange_rsp_size =
4338 1 : libspdm_get_opaque_data_version_selection_data_size( spdm_context);
4339 1 : spdm_response_size = sizeof(spdm_key_exchange_response_t) +
4340 1 : dhe_key_size + 0 + sizeof(uint16_t) +
4341 1 : opaque_key_exchange_rsp_size + signature_size +
4342 : hmac_size;
4343 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
4344 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
4345 1 : libspdm_zero_mem(spdm_response,spdm_response_size);
4346 :
4347 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_11;
4348 1 : spdm_response->header.request_response_code = SPDM_KEY_EXCHANGE_RSP;
4349 1 : spdm_response->header.param1 = 0;
4350 1 : spdm_response->rsp_session_id = libspdm_allocate_rsp_session_id(spdm_context, false);
4351 1 : spdm_response->mut_auth_requested = SPDM_KEY_EXCHANGE_RESPONSE_MUT_AUTH_REQUESTED;
4352 1 : spdm_response->req_slot_id_param = 9; /* Invalid SlotID */
4353 1 : libspdm_get_random_number(SPDM_RANDOM_DATA_SIZE, spdm_response->random_data);
4354 1 : ptr = (void *)(spdm_response + 1);
4355 1 : dhe_context = libspdm_dhe_new(
4356 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
4357 : m_libspdm_use_dhe_algo,
4358 : true);
4359 1 : libspdm_dhe_generate_key(m_libspdm_use_dhe_algo, dhe_context, ptr, &dhe_key_size);
4360 1 : final_key_size = sizeof(final_key);
4361 1 : libspdm_dhe_compute_key(
4362 : m_libspdm_use_dhe_algo, dhe_context,
4363 1 : (uint8_t *)&m_libspdm_local_buffer[0] +
4364 : sizeof(spdm_key_exchange_request_t),
4365 : dhe_key_size, final_key, &final_key_size);
4366 1 : libspdm_dhe_free(m_libspdm_use_dhe_algo, dhe_context);
4367 1 : ptr += dhe_key_size;
4368 : /* libspdm_zero_mem (ptr, hash_size);
4369 : * ptr += hash_size;*/
4370 1 : libspdm_write_uint16(ptr, (uint16_t)opaque_key_exchange_rsp_size);
4371 1 : ptr += sizeof(uint16_t);
4372 1 : libspdm_build_opaque_data_version_selection_data(
4373 : spdm_context, SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT,
4374 : &opaque_key_exchange_rsp_size, ptr);
4375 1 : ptr += opaque_key_exchange_rsp_size;
4376 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
4377 : m_libspdm_use_asym_algo, &data,
4378 : &data_size, NULL, NULL)) {
4379 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
4380 : }
4381 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
4382 : sizeof(m_libspdm_local_buffer)
4383 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
4384 : m_libspdm_local_buffer),
4385 1 : spdm_response, (size_t)ptr - (size_t)spdm_response);
4386 1 : m_libspdm_local_buffer_size += ((size_t)ptr - (size_t)spdm_response);
4387 1 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "m_libspdm_local_buffer_size (0x%zx):\n",
4388 : m_libspdm_local_buffer_size));
4389 1 : libspdm_dump_hex(m_libspdm_local_buffer, m_libspdm_local_buffer_size);
4390 1 : libspdm_init_managed_buffer(&th_curr, sizeof(th_curr.buffer));
4391 1 : cert_buffer = (uint8_t *)data;
4392 1 : cert_buffer_size = data_size;
4393 1 : libspdm_hash_all(m_libspdm_use_hash_algo, cert_buffer, cert_buffer_size, cert_buffer_hash);
4394 : /* transcript.message_a size is 0*/
4395 1 : libspdm_append_managed_buffer(&th_curr, cert_buffer_hash, hash_size);
4396 1 : libspdm_append_managed_buffer(&th_curr, m_libspdm_local_buffer,
4397 : m_libspdm_local_buffer_size);
4398 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
4399 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
4400 1 : free(data);
4401 1 : libspdm_responder_data_sign(
4402 : spdm_context,
4403 1 : spdm_response->header.spdm_version << SPDM_VERSION_NUMBER_SHIFT_BIT,
4404 : 0, SPDM_KEY_EXCHANGE_RSP,
4405 : m_libspdm_use_asym_algo, m_libspdm_use_pqc_asym_algo, m_libspdm_use_hash_algo,
4406 1 : false, libspdm_get_managed_buffer(&th_curr),
4407 : libspdm_get_managed_buffer_size(&th_curr), ptr,
4408 : &signature_size);
4409 1 : libspdm_copy_mem(&m_libspdm_local_buffer[m_libspdm_local_buffer_size],
4410 : sizeof(m_libspdm_local_buffer)
4411 1 : - (&m_libspdm_local_buffer[m_libspdm_local_buffer_size] -
4412 : m_libspdm_local_buffer),
4413 : ptr, signature_size);
4414 1 : m_libspdm_local_buffer_size += signature_size;
4415 1 : libspdm_append_managed_buffer(&th_curr, ptr, signature_size);
4416 1 : ptr += signature_size;
4417 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
4418 : libspdm_get_managed_buffer_size(&th_curr),
4419 : th_curr_hash_data);
4420 1 : bin_str0_size = sizeof(bin_str0);
4421 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
4422 : SPDM_BIN_STR_0_LABEL, sizeof(SPDM_BIN_STR_0_LABEL) - 1,
4423 1 : NULL, (uint16_t)hash_size, hash_size, bin_str0,
4424 : &bin_str0_size);
4425 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, final_key, final_key_size,
4426 : m_libspdm_zero_filled_buffer, hash_size,handshake_secret);
4427 1 : bin_str2_size = sizeof(bin_str2);
4428 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
4429 : SPDM_BIN_STR_2_LABEL, sizeof(SPDM_BIN_STR_2_LABEL) - 1,
4430 1 : th_curr_hash_data, (uint16_t)hash_size, hash_size,
4431 : bin_str2, &bin_str2_size);
4432 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, handshake_secret, hash_size,
4433 : bin_str2, bin_str2_size,
4434 : response_handshake_secret, hash_size);
4435 1 : bin_str7_size = sizeof(bin_str7);
4436 1 : libspdm_bin_concat(((libspdm_context_t *)spdm_context)->connection_info.version,
4437 : SPDM_BIN_STR_7_LABEL, sizeof(SPDM_BIN_STR_7_LABEL) - 1,
4438 1 : NULL, (uint16_t)hash_size, hash_size, bin_str7,
4439 : &bin_str7_size);
4440 1 : libspdm_hkdf_expand(m_libspdm_use_hash_algo, response_handshake_secret,
4441 : hash_size, bin_str7, bin_str7_size,
4442 : response_finished_key, hash_size);
4443 1 : libspdm_hash_all(m_libspdm_use_hash_algo, libspdm_get_managed_buffer(&th_curr),
4444 : libspdm_get_managed_buffer_size(&th_curr), hash_data);
4445 1 : libspdm_hmac_all(m_libspdm_use_hash_algo, hash_data, hash_size,
4446 : response_finished_key, hash_size, ptr);
4447 1 : ptr += hmac_size;
4448 :
4449 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
4450 : false, spdm_response_size,
4451 : spdm_response, response_size,
4452 : response);
4453 : }
4454 1 : return LIBSPDM_STATUS_SUCCESS;
4455 :
4456 0 : default:
4457 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
4458 : }
4459 : }
4460 :
4461 1 : static void libspdm_test_requester_key_exchange_err_case1(void **state)
4462 : {
4463 : libspdm_return_t status;
4464 : libspdm_test_context_t *spdm_test_context;
4465 : libspdm_context_t *spdm_context;
4466 : uint32_t session_id;
4467 : uint8_t heartbeat_period;
4468 : uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
4469 : uint8_t slot_id_param;
4470 : void *data;
4471 : size_t data_size;
4472 : void *hash;
4473 : size_t hash_size;
4474 :
4475 1 : spdm_test_context = *state;
4476 1 : spdm_context = spdm_test_context->spdm_context;
4477 1 : spdm_test_context->case_id = 0x1;
4478 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
4479 : SPDM_VERSION_NUMBER_SHIFT_BIT;
4480 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
4481 1 : spdm_context->connection_info.capability.flags |=
4482 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
4483 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
4484 1 : spdm_context->local_context.capability.flags |=
4485 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
4486 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
4487 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
4488 : m_libspdm_use_asym_algo, &data,
4489 : &data_size, &hash, &hash_size)) {
4490 0 : assert(false);
4491 : }
4492 1 : libspdm_reset_message_a(spdm_context);
4493 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
4494 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
4495 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
4496 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
4497 :
4498 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
4499 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
4500 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
4501 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
4502 : data, data_size);
4503 : #else
4504 1 : libspdm_hash_all(
4505 : spdm_context->connection_info.algorithm.base_hash_algo,
4506 : data, data_size,
4507 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
4508 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
4509 1 : libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
4510 1 : libspdm_get_leaf_cert_public_key_from_cert_chain(
4511 : spdm_context->connection_info.algorithm.base_hash_algo,
4512 : spdm_context->connection_info.algorithm.base_asym_algo,
4513 : data, data_size,
4514 : &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
4515 : #endif
4516 :
4517 1 : heartbeat_period = 0;
4518 1 : libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
4519 1 : status = libspdm_send_receive_key_exchange(
4520 : spdm_context,
4521 : SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
4522 : &session_id, &heartbeat_period, &slot_id_param,
4523 : measurement_hash);
4524 1 : assert_int_equal(status, LIBSPDM_STATUS_SEND_FAIL);
4525 1 : free(data);
4526 1 : }
4527 :
4528 1 : static void libspdm_test_requester_key_exchange_err_case2(void **state)
4529 : {
4530 : libspdm_return_t status;
4531 : libspdm_test_context_t *spdm_test_context;
4532 : libspdm_context_t *spdm_context;
4533 : uint32_t session_id;
4534 : uint8_t heartbeat_period;
4535 : uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
4536 : uint8_t slot_id_param;
4537 : void *data;
4538 : size_t data_size;
4539 : void *hash;
4540 : size_t hash_size;
4541 :
4542 1 : spdm_test_context = *state;
4543 1 : spdm_context = spdm_test_context->spdm_context;
4544 1 : spdm_test_context->case_id = 0x2;
4545 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
4546 : SPDM_VERSION_NUMBER_SHIFT_BIT;
4547 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
4548 1 : spdm_context->connection_info.capability.flags |=
4549 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
4550 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
4551 1 : spdm_context->local_context.capability.flags |=
4552 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
4553 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
4554 1 : spdm_context->local_context.secured_message_version.secured_message_version_count = 1;
4555 1 : spdm_context->local_context.secured_message_version.secured_message_version[0] =
4556 : SECURED_SPDM_VERSION_11 << SPDM_VERSION_NUMBER_SHIFT_BIT;
4557 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
4558 : m_libspdm_use_asym_algo, &data,
4559 : &data_size, &hash, &hash_size)) {
4560 0 : assert(false);
4561 : }
4562 1 : libspdm_reset_message_a(spdm_context);
4563 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
4564 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
4565 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
4566 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
4567 :
4568 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
4569 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
4570 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
4571 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
4572 : data, data_size);
4573 : #else
4574 1 : libspdm_hash_all(
4575 : spdm_context->connection_info.algorithm.base_hash_algo,
4576 : data, data_size,
4577 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
4578 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
4579 1 : libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
4580 1 : libspdm_get_leaf_cert_public_key_from_cert_chain(
4581 : spdm_context->connection_info.algorithm.base_hash_algo,
4582 : spdm_context->connection_info.algorithm.base_asym_algo,
4583 : data, data_size,
4584 : &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
4585 : #endif
4586 :
4587 1 : heartbeat_period = 0;
4588 1 : libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
4589 1 : status = libspdm_send_receive_key_exchange(
4590 : spdm_context,
4591 : SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
4592 : &session_id, &heartbeat_period, &slot_id_param,
4593 : measurement_hash);
4594 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
4595 1 : assert_int_equal(session_id, 0xFFFFFFFF);
4596 1 : assert_int_equal(
4597 : libspdm_secured_message_get_session_state(
4598 : spdm_context->session_info[0].secured_message_context),
4599 : LIBSPDM_SESSION_STATE_HANDSHAKING);
4600 1 : free(data);
4601 1 : }
4602 :
4603 1 : static void libspdm_test_requester_key_exchange_err_case3(void **state)
4604 : {
4605 : libspdm_return_t status;
4606 : libspdm_test_context_t *spdm_test_context;
4607 : libspdm_context_t *spdm_context;
4608 : uint32_t session_id;
4609 : uint8_t heartbeat_period;
4610 : uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
4611 : uint8_t slot_id_param;
4612 : void *data;
4613 : size_t data_size;
4614 : void *hash;
4615 : size_t hash_size;
4616 :
4617 1 : spdm_test_context = *state;
4618 1 : spdm_context = spdm_test_context->spdm_context;
4619 1 : spdm_test_context->case_id = 0x3;
4620 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
4621 : SPDM_VERSION_NUMBER_SHIFT_BIT;
4622 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NOT_STARTED;
4623 1 : spdm_context->connection_info.capability.flags |=
4624 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
4625 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
4626 1 : spdm_context->local_context.capability.flags |=
4627 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
4628 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
4629 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
4630 : m_libspdm_use_asym_algo, &data,
4631 : &data_size, &hash, &hash_size)) {
4632 0 : assert(false);
4633 : }
4634 1 : libspdm_reset_message_a(spdm_context);
4635 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
4636 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
4637 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
4638 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
4639 :
4640 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
4641 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
4642 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
4643 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
4644 : data, data_size);
4645 : #else
4646 1 : libspdm_hash_all(
4647 : spdm_context->connection_info.algorithm.base_hash_algo,
4648 : data, data_size,
4649 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
4650 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
4651 1 : libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
4652 1 : libspdm_get_leaf_cert_public_key_from_cert_chain(
4653 : spdm_context->connection_info.algorithm.base_hash_algo,
4654 : spdm_context->connection_info.algorithm.base_asym_algo,
4655 : data, data_size,
4656 : &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
4657 : #endif
4658 :
4659 1 : heartbeat_period = 0;
4660 1 : libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
4661 1 : status = libspdm_send_receive_key_exchange(
4662 : spdm_context,
4663 : SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
4664 : &session_id, &heartbeat_period, &slot_id_param,
4665 : measurement_hash);
4666 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_STATE_LOCAL);
4667 1 : free(data);
4668 1 : }
4669 :
4670 1 : static void libspdm_test_requester_key_exchange_err_case4(void **state)
4671 : {
4672 : libspdm_return_t status;
4673 : libspdm_test_context_t *spdm_test_context;
4674 : libspdm_context_t *spdm_context;
4675 : uint32_t session_id;
4676 : uint8_t heartbeat_period;
4677 : uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
4678 : uint8_t slot_id_param;
4679 : void *data;
4680 : size_t data_size;
4681 : void *hash;
4682 : size_t hash_size;
4683 :
4684 1 : spdm_test_context = *state;
4685 1 : spdm_context = spdm_test_context->spdm_context;
4686 1 : spdm_test_context->case_id = 0x4;
4687 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
4688 : SPDM_VERSION_NUMBER_SHIFT_BIT;
4689 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
4690 1 : spdm_context->connection_info.capability.flags |=
4691 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
4692 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
4693 1 : spdm_context->local_context.capability.flags |=
4694 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
4695 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
4696 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
4697 : m_libspdm_use_asym_algo, &data,
4698 : &data_size, &hash, &hash_size)) {
4699 0 : assert(false);
4700 : }
4701 1 : libspdm_reset_message_a(spdm_context);
4702 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
4703 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
4704 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
4705 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
4706 :
4707 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
4708 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
4709 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
4710 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
4711 : data, data_size);
4712 : #else
4713 1 : libspdm_hash_all(
4714 : spdm_context->connection_info.algorithm.base_hash_algo,
4715 : data, data_size,
4716 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
4717 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
4718 1 : libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
4719 1 : libspdm_get_leaf_cert_public_key_from_cert_chain(
4720 : spdm_context->connection_info.algorithm.base_hash_algo,
4721 : spdm_context->connection_info.algorithm.base_asym_algo,
4722 : data, data_size,
4723 : &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
4724 : #endif
4725 :
4726 1 : heartbeat_period = 0;
4727 1 : libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
4728 1 : status = libspdm_send_receive_key_exchange(
4729 : spdm_context,
4730 : SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
4731 : &session_id, &heartbeat_period, &slot_id_param,
4732 : measurement_hash);
4733 1 : assert_int_equal(status, LIBSPDM_STATUS_ERROR_PEER);
4734 1 : free(data);
4735 1 : }
4736 :
4737 1 : static void libspdm_test_requester_key_exchange_err_case5(void **state)
4738 : {
4739 : libspdm_return_t status;
4740 : libspdm_test_context_t *spdm_test_context;
4741 : libspdm_context_t *spdm_context;
4742 : uint32_t session_id;
4743 : uint8_t heartbeat_period;
4744 : uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
4745 : uint8_t slot_id_param;
4746 : void *data;
4747 : size_t data_size;
4748 : void *hash;
4749 : size_t hash_size;
4750 :
4751 1 : spdm_test_context = *state;
4752 1 : spdm_context = spdm_test_context->spdm_context;
4753 1 : spdm_test_context->case_id = 0x5;
4754 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
4755 : SPDM_VERSION_NUMBER_SHIFT_BIT;
4756 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
4757 1 : spdm_context->connection_info.capability.flags |=
4758 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
4759 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
4760 1 : spdm_context->local_context.capability.flags |=
4761 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
4762 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
4763 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
4764 : m_libspdm_use_asym_algo, &data,
4765 : &data_size, &hash, &hash_size)) {
4766 0 : assert(false);
4767 : }
4768 1 : libspdm_reset_message_a(spdm_context);
4769 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
4770 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
4771 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
4772 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
4773 :
4774 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
4775 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
4776 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
4777 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
4778 : data, data_size);
4779 : #else
4780 1 : libspdm_hash_all(
4781 : spdm_context->connection_info.algorithm.base_hash_algo,
4782 : data, data_size,
4783 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
4784 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
4785 1 : libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
4786 1 : libspdm_get_leaf_cert_public_key_from_cert_chain(
4787 : spdm_context->connection_info.algorithm.base_hash_algo,
4788 : spdm_context->connection_info.algorithm.base_asym_algo,
4789 : data, data_size,
4790 : &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
4791 : #endif
4792 :
4793 1 : heartbeat_period = 0;
4794 1 : libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
4795 1 : status = libspdm_send_receive_key_exchange(
4796 : spdm_context,
4797 : SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
4798 : &session_id, &heartbeat_period, &slot_id_param,
4799 : measurement_hash);
4800 1 : assert_int_equal(status, LIBSPDM_STATUS_BUSY_PEER);
4801 1 : free(data);
4802 1 : }
4803 :
4804 : /**
4805 : * Test 6: Requester / Responder do not support key exchange.
4806 : * Expected Behavior: returns with error LIBSPDM_STATUS_UNSUPPORTED_CAP.
4807 : **/
4808 1 : static void libspdm_test_requester_key_exchange_err_case6(void **state)
4809 : {
4810 : libspdm_return_t status;
4811 : libspdm_test_context_t *spdm_test_context;
4812 : libspdm_context_t *spdm_context;
4813 : uint32_t session_id;
4814 : uint8_t slot_id_param;
4815 :
4816 1 : spdm_test_context = *state;
4817 1 : spdm_context = spdm_test_context->spdm_context;
4818 1 : spdm_test_context->case_id = 0x6;
4819 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
4820 : SPDM_VERSION_NUMBER_SHIFT_BIT;
4821 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
4822 1 : spdm_context->connection_info.capability.flags = 0;
4823 1 : spdm_context->local_context.capability.flags = 0;
4824 :
4825 1 : status = libspdm_send_receive_key_exchange(
4826 : spdm_context, SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
4827 : &session_id, NULL, &slot_id_param, NULL);
4828 1 : assert_int_equal(status, LIBSPDM_STATUS_UNSUPPORTED_CAP);
4829 1 : }
4830 :
4831 1 : static void libspdm_test_requester_key_exchange_err_case7(void **state)
4832 : {
4833 : libspdm_return_t status;
4834 : libspdm_test_context_t *spdm_test_context;
4835 : libspdm_context_t *spdm_context;
4836 : uint32_t session_id;
4837 : uint8_t heartbeat_period;
4838 : uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
4839 : uint8_t slot_id_param;
4840 : void *data;
4841 : size_t data_size;
4842 : void *hash;
4843 : size_t hash_size;
4844 :
4845 1 : spdm_test_context = *state;
4846 1 : spdm_context = spdm_test_context->spdm_context;
4847 1 : spdm_test_context->case_id = 0x7;
4848 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
4849 : SPDM_VERSION_NUMBER_SHIFT_BIT;
4850 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
4851 1 : spdm_context->connection_info.capability.flags |=
4852 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
4853 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
4854 1 : spdm_context->local_context.capability.flags |=
4855 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
4856 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
4857 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
4858 : m_libspdm_use_asym_algo, &data,
4859 : &data_size, &hash, &hash_size)) {
4860 0 : assert(false);
4861 : }
4862 1 : libspdm_reset_message_a(spdm_context);
4863 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
4864 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
4865 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
4866 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
4867 :
4868 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
4869 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
4870 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
4871 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
4872 : data, data_size);
4873 : #else
4874 1 : libspdm_hash_all(
4875 : spdm_context->connection_info.algorithm.base_hash_algo,
4876 : data, data_size,
4877 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
4878 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
4879 1 : libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
4880 1 : libspdm_get_leaf_cert_public_key_from_cert_chain(
4881 : spdm_context->connection_info.algorithm.base_hash_algo,
4882 : spdm_context->connection_info.algorithm.base_asym_algo,
4883 : data, data_size,
4884 : &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
4885 : #endif
4886 :
4887 1 : heartbeat_period = 0;
4888 1 : libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
4889 1 : status = libspdm_send_receive_key_exchange(
4890 : spdm_context,
4891 : SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
4892 : &session_id, &heartbeat_period, &slot_id_param,
4893 : measurement_hash);
4894 1 : assert_int_equal(status, LIBSPDM_STATUS_RESYNCH_PEER);
4895 1 : assert_int_equal(spdm_context->connection_info.connection_state,
4896 : LIBSPDM_CONNECTION_STATE_NOT_STARTED);
4897 1 : free(data);
4898 1 : }
4899 :
4900 1 : static void libspdm_test_requester_key_exchange_err_case8(void **state)
4901 : {
4902 : libspdm_return_t status;
4903 : libspdm_test_context_t *spdm_test_context;
4904 : libspdm_context_t *spdm_context;
4905 : uint32_t session_id;
4906 : uint8_t heartbeat_period;
4907 : uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
4908 : uint8_t slot_id_param;
4909 : void *data;
4910 : size_t data_size;
4911 : void *hash;
4912 : size_t hash_size;
4913 :
4914 1 : spdm_test_context = *state;
4915 1 : spdm_context = spdm_test_context->spdm_context;
4916 1 : spdm_test_context->case_id = 0x8;
4917 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
4918 : SPDM_VERSION_NUMBER_SHIFT_BIT;
4919 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
4920 1 : spdm_context->connection_info.capability.flags |=
4921 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
4922 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
4923 1 : spdm_context->local_context.capability.flags |=
4924 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
4925 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
4926 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
4927 : m_libspdm_use_asym_algo, &data,
4928 : &data_size, &hash, &hash_size)) {
4929 0 : assert(false);
4930 : }
4931 1 : libspdm_reset_message_a(spdm_context);
4932 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
4933 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
4934 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
4935 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
4936 :
4937 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
4938 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
4939 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
4940 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
4941 : data, data_size);
4942 : #else
4943 1 : libspdm_hash_all(
4944 : spdm_context->connection_info.algorithm.base_hash_algo,
4945 : data, data_size,
4946 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
4947 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
4948 1 : libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
4949 1 : libspdm_get_leaf_cert_public_key_from_cert_chain(
4950 : spdm_context->connection_info.algorithm.base_hash_algo,
4951 : spdm_context->connection_info.algorithm.base_asym_algo,
4952 : data, data_size,
4953 : &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
4954 : #endif
4955 :
4956 1 : heartbeat_period = 0;
4957 1 : libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
4958 1 : status = libspdm_send_receive_key_exchange(
4959 : spdm_context,
4960 : SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
4961 : &session_id, &heartbeat_period, &slot_id_param,
4962 : measurement_hash);
4963 1 : assert_int_equal(status, LIBSPDM_STATUS_NOT_READY_PEER);
4964 1 : free(data);
4965 1 : }
4966 :
4967 : /**
4968 : * Test 9: Unable to acquire the sender buffer.
4969 : * Expected Behavior: returns with error LIBSPDM_STATUS_ACQUIRE_FAIL.
4970 : **/
4971 1 : static void libspdm_test_requester_key_exchange_err_case9(void **state)
4972 : {
4973 : libspdm_return_t status;
4974 : libspdm_test_context_t *spdm_test_context;
4975 : libspdm_context_t *spdm_context;
4976 : uint32_t session_id;
4977 : uint8_t slot_id_param;
4978 :
4979 1 : spdm_test_context = *state;
4980 1 : spdm_context = spdm_test_context->spdm_context;
4981 1 : spdm_test_context->case_id = 0x9;
4982 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
4983 : SPDM_VERSION_NUMBER_SHIFT_BIT;
4984 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
4985 1 : spdm_context->connection_info.capability.flags |=
4986 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
4987 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
4988 1 : spdm_context->local_context.capability.flags |=
4989 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
4990 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
4991 :
4992 1 : libspdm_force_error (LIBSPDM_ERR_ACQUIRE_SENDER_BUFFER);
4993 1 : status = libspdm_send_receive_key_exchange(
4994 : spdm_context, SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
4995 : &session_id, NULL, &slot_id_param, NULL);
4996 1 : libspdm_release_error (LIBSPDM_ERR_ACQUIRE_SENDER_BUFFER);
4997 :
4998 1 : assert_int_equal(status, LIBSPDM_STATUS_ACQUIRE_FAIL);
4999 1 : }
5000 :
5001 1 : static void libspdm_test_requester_key_exchange_err_case10(void **state) {
5002 : libspdm_return_t status;
5003 : libspdm_test_context_t *spdm_test_context;
5004 : libspdm_context_t *spdm_context;
5005 : uint32_t session_id;
5006 : uint8_t heartbeat_period;
5007 : uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
5008 : uint8_t slot_id_param;
5009 : void *data;
5010 : size_t data_size;
5011 : void *hash;
5012 : size_t hash_size;
5013 : uint16_t error_code;
5014 :
5015 1 : spdm_test_context = *state;
5016 1 : spdm_context = spdm_test_context->spdm_context;
5017 1 : spdm_test_context->case_id = 0xA;
5018 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
5019 : SPDM_VERSION_NUMBER_SHIFT_BIT;
5020 1 : spdm_context->connection_info.capability.flags |=
5021 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
5022 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
5023 1 : libspdm_read_responder_public_certificate_chain (m_libspdm_use_hash_algo,
5024 : m_libspdm_use_asym_algo,
5025 : &data, &data_size,
5026 : &hash, &hash_size);
5027 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
5028 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
5029 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
5030 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
5031 :
5032 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
5033 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
5034 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
5035 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
5036 : data, data_size);
5037 : #else
5038 1 : libspdm_hash_all(
5039 : spdm_context->connection_info.algorithm.base_hash_algo,
5040 : data, data_size,
5041 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
5042 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
5043 1 : libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
5044 1 : libspdm_get_leaf_cert_public_key_from_cert_chain(
5045 : spdm_context->connection_info.algorithm.base_hash_algo,
5046 : spdm_context->connection_info.algorithm.base_asym_algo,
5047 : data, data_size,
5048 : &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
5049 : #endif
5050 :
5051 1 : error_code = LIBSPDM_ERROR_CODE_RESERVED_00;
5052 19 : while(error_code <= 0xff) {
5053 18 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
5054 18 : libspdm_reset_message_a(spdm_context);
5055 :
5056 18 : heartbeat_period = 0;
5057 18 : libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
5058 18 : status = libspdm_send_receive_key_exchange (spdm_context,
5059 : SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH,
5060 : 0, 0, &session_id, &heartbeat_period,
5061 : &slot_id_param, measurement_hash);
5062 18 : LIBSPDM_ASSERT_INT_EQUAL_CASE (status, LIBSPDM_STATUS_ERROR_PEER, error_code);
5063 :
5064 18 : error_code++;
5065 18 : if(error_code == SPDM_ERROR_CODE_BUSY) { /*busy is treated in cases 5 and 6*/
5066 1 : error_code = SPDM_ERROR_CODE_UNEXPECTED_REQUEST;
5067 : }
5068 : /* skip some reserved error codes (0d to 3e) */
5069 18 : if(error_code == LIBSPDM_ERROR_CODE_RESERVED_0D) {
5070 1 : error_code = LIBSPDM_ERROR_CODE_RESERVED_3F;
5071 : }
5072 : /* skip response not ready, request resync, and some reserved codes (44 to fc) */
5073 18 : if(error_code == SPDM_ERROR_CODE_RESPONSE_NOT_READY) {
5074 1 : error_code = LIBSPDM_ERROR_CODE_RESERVED_FD;
5075 : }
5076 : }
5077 :
5078 1 : free(data);
5079 1 : }
5080 :
5081 : /**
5082 : * Test 11: Unable to acquire the receiver buffer.
5083 : * Expected Behavior: returns with error LIBSPDM_STATUS_ACQUIRE_FAIL.
5084 : **/
5085 1 : static void libspdm_test_requester_key_exchange_err_case11(void **state)
5086 : {
5087 : libspdm_return_t status;
5088 : libspdm_test_context_t *spdm_test_context;
5089 : libspdm_context_t *spdm_context;
5090 : uint32_t session_id;
5091 : uint8_t slot_id_param;
5092 :
5093 1 : spdm_test_context = *state;
5094 1 : spdm_context = spdm_test_context->spdm_context;
5095 1 : spdm_test_context->case_id = 0xb;
5096 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
5097 : SPDM_VERSION_NUMBER_SHIFT_BIT;
5098 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
5099 1 : spdm_context->connection_info.capability.flags |=
5100 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
5101 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
5102 1 : spdm_context->local_context.capability.flags |=
5103 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
5104 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
5105 :
5106 1 : libspdm_force_error (LIBSPDM_ERR_ACQUIRE_RECEIVER_BUFFER);
5107 1 : status = libspdm_send_receive_key_exchange(
5108 : spdm_context, SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
5109 : &session_id, NULL, &slot_id_param, NULL);
5110 1 : libspdm_release_error (LIBSPDM_ERR_ACQUIRE_RECEIVER_BUFFER);
5111 :
5112 1 : assert_int_equal(status, LIBSPDM_STATUS_ACQUIRE_FAIL);
5113 1 : }
5114 :
5115 : /**
5116 : * Test 12: Version in response is incorrect.
5117 : * Expected Behavior: returns with error LIBSPDM_STATUS_INVALID_MSG_FIELD.
5118 : **/
5119 1 : static void libspdm_test_requester_key_exchange_err_case12(void **state)
5120 : {
5121 : libspdm_return_t status;
5122 : libspdm_test_context_t *spdm_test_context;
5123 : libspdm_context_t *spdm_context;
5124 : uint32_t session_id;
5125 : uint8_t slot_id_param;
5126 :
5127 1 : spdm_test_context = *state;
5128 1 : spdm_context = spdm_test_context->spdm_context;
5129 1 : spdm_test_context->case_id = 0xc;
5130 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
5131 : SPDM_VERSION_NUMBER_SHIFT_BIT;
5132 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
5133 1 : spdm_context->connection_info.capability.flags |=
5134 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
5135 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
5136 1 : spdm_context->local_context.capability.flags |=
5137 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
5138 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
5139 :
5140 1 : status = libspdm_send_receive_key_exchange(
5141 : spdm_context, SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
5142 : &session_id, NULL, &slot_id_param, NULL);
5143 :
5144 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
5145 1 : }
5146 :
5147 : /**
5148 : * Test 13: Response code is incorrect.
5149 : * Expected Behavior: returns with error LIBSPDM_STATUS_INVALID_MSG_FIELD.
5150 : **/
5151 1 : static void libspdm_test_requester_key_exchange_err_case13(void **state)
5152 : {
5153 : libspdm_return_t status;
5154 : libspdm_test_context_t *spdm_test_context;
5155 : libspdm_context_t *spdm_context;
5156 : uint32_t session_id;
5157 : uint8_t slot_id_param;
5158 :
5159 1 : spdm_test_context = *state;
5160 1 : spdm_context = spdm_test_context->spdm_context;
5161 1 : spdm_test_context->case_id = 0xd;
5162 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
5163 : SPDM_VERSION_NUMBER_SHIFT_BIT;
5164 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
5165 1 : spdm_context->connection_info.capability.flags |=
5166 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
5167 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
5168 1 : spdm_context->local_context.capability.flags |=
5169 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
5170 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
5171 :
5172 1 : status = libspdm_send_receive_key_exchange(
5173 : spdm_context, SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
5174 : &session_id, NULL, &slot_id_param, NULL);
5175 :
5176 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
5177 1 : }
5178 :
5179 : /**
5180 : * Test 14: Responder requests mutual authentication when it is not supported.
5181 : * Expected Behavior: returns with error LIBSPDM_STATUS_INVALID_MSG_FIELD.
5182 : **/
5183 1 : static void libspdm_test_requester_key_exchange_err_case14(void **state)
5184 : {
5185 : libspdm_return_t status;
5186 : libspdm_test_context_t *spdm_test_context;
5187 : libspdm_context_t *spdm_context;
5188 : uint32_t session_id;
5189 : uint8_t slot_id_param;
5190 :
5191 1 : spdm_test_context = *state;
5192 1 : spdm_context = spdm_test_context->spdm_context;
5193 1 : spdm_test_context->case_id = 0xe;
5194 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
5195 : SPDM_VERSION_NUMBER_SHIFT_BIT;
5196 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
5197 1 : spdm_context->connection_info.capability.flags |=
5198 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
5199 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
5200 1 : spdm_context->local_context.capability.flags |=
5201 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
5202 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
5203 :
5204 1 : status = libspdm_send_receive_key_exchange(
5205 : spdm_context, SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
5206 : &session_id, NULL, &slot_id_param, NULL);
5207 :
5208 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
5209 1 : }
5210 :
5211 1 : static void libspdm_test_requester_key_exchange_err_case15(void **state)
5212 : {
5213 : libspdm_return_t status;
5214 : libspdm_test_context_t *spdm_test_context;
5215 : libspdm_context_t *spdm_context;
5216 : uint32_t session_id;
5217 : uint8_t heartbeat_period;
5218 : uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
5219 : uint8_t slot_id_param;
5220 : void *data;
5221 : size_t data_size;
5222 : void *hash;
5223 : size_t hash_size;
5224 :
5225 1 : spdm_test_context = *state;
5226 1 : spdm_context = spdm_test_context->spdm_context;
5227 :
5228 1 : if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
5229 1 : libspdm_free_session_id(spdm_context,0xFFFFFFFF);
5230 : }
5231 :
5232 1 : spdm_test_context->case_id = 0xF;
5233 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
5234 : SPDM_VERSION_NUMBER_SHIFT_BIT;
5235 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
5236 1 : spdm_context->connection_info.capability.flags |=
5237 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
5238 1 : spdm_context->connection_info.capability.flags |=
5239 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
5240 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
5241 :
5242 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
5243 : m_libspdm_use_asym_algo, &data,
5244 : &data_size, &hash, &hash_size)) {
5245 0 : assert(false);
5246 : }
5247 1 : libspdm_reset_message_a(spdm_context);
5248 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
5249 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
5250 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
5251 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
5252 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
5253 : m_libspdm_use_measurement_hash_algo;
5254 :
5255 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
5256 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
5257 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
5258 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
5259 : data, data_size);
5260 : #else
5261 1 : libspdm_hash_all(
5262 : spdm_context->connection_info.algorithm.base_hash_algo,
5263 : data, data_size,
5264 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
5265 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
5266 1 : libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
5267 1 : libspdm_get_leaf_cert_public_key_from_cert_chain(
5268 : spdm_context->connection_info.algorithm.base_hash_algo,
5269 : spdm_context->connection_info.algorithm.base_asym_algo,
5270 : data, data_size,
5271 : &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
5272 : #endif
5273 :
5274 1 : heartbeat_period = 0;
5275 1 : libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
5276 1 : status = libspdm_send_receive_key_exchange(
5277 : spdm_context,
5278 : SPDM_KEY_EXCHANGE_REQUEST_TCB_COMPONENT_MEASUREMENT_HASH, 0, 0,
5279 : &session_id, &heartbeat_period, &slot_id_param,
5280 : measurement_hash);
5281 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_SIZE);
5282 1 : free(data);
5283 1 : }
5284 :
5285 1 : static void libspdm_test_requester_key_exchange_err_case16(void **state)
5286 : {
5287 : libspdm_return_t status;
5288 : libspdm_test_context_t *spdm_test_context;
5289 : libspdm_context_t *spdm_context;
5290 : uint32_t session_id;
5291 : uint8_t heartbeat_period;
5292 : uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
5293 : uint8_t slot_id_param;
5294 : void *data;
5295 : size_t data_size;
5296 : void *hash;
5297 : size_t hash_size;
5298 :
5299 1 : spdm_test_context = *state;
5300 1 : spdm_context = spdm_test_context->spdm_context;
5301 :
5302 1 : if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
5303 0 : libspdm_free_session_id(spdm_context,0xFFFFFFFF);
5304 : }
5305 :
5306 1 : spdm_test_context->case_id = 0x10;
5307 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
5308 : SPDM_VERSION_NUMBER_SHIFT_BIT;
5309 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
5310 1 : spdm_context->connection_info.capability.flags |=
5311 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
5312 1 : spdm_context->connection_info.capability.flags |=
5313 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
5314 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
5315 :
5316 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
5317 : m_libspdm_use_asym_algo, &data,
5318 : &data_size, &hash, &hash_size)) {
5319 0 : assert(false);
5320 : }
5321 1 : libspdm_reset_message_a(spdm_context);
5322 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
5323 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
5324 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
5325 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
5326 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
5327 : m_libspdm_use_measurement_hash_algo;
5328 :
5329 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
5330 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
5331 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
5332 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
5333 : data, data_size);
5334 : #else
5335 1 : libspdm_hash_all(
5336 : spdm_context->connection_info.algorithm.base_hash_algo,
5337 : data, data_size,
5338 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
5339 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
5340 1 : libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
5341 1 : libspdm_get_leaf_cert_public_key_from_cert_chain(
5342 : spdm_context->connection_info.algorithm.base_hash_algo,
5343 : spdm_context->connection_info.algorithm.base_asym_algo,
5344 : data, data_size,
5345 : &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
5346 : #endif
5347 :
5348 1 : heartbeat_period = 0;
5349 1 : libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
5350 1 : status = libspdm_send_receive_key_exchange(
5351 : spdm_context,
5352 : SPDM_KEY_EXCHANGE_REQUEST_ALL_MEASUREMENTS_HASH, 0, 0,
5353 : &session_id, &heartbeat_period, &slot_id_param,
5354 : measurement_hash);
5355 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_SIZE);
5356 1 : free(data);
5357 1 : }
5358 :
5359 1 : static void libspdm_test_requester_key_exchange_err_case17(void **state)
5360 : {
5361 : libspdm_return_t status;
5362 : libspdm_test_context_t *spdm_test_context;
5363 : libspdm_context_t *spdm_context;
5364 : uint32_t session_id;
5365 : uint8_t heartbeat_period;
5366 : uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
5367 : uint8_t slot_id_param;
5368 : void *data;
5369 : size_t data_size;
5370 : void *hash;
5371 : size_t hash_size;
5372 :
5373 1 : spdm_test_context = *state;
5374 1 : spdm_context = spdm_test_context->spdm_context;
5375 :
5376 1 : if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
5377 0 : libspdm_free_session_id(spdm_context,0xFFFFFFFF);
5378 : }
5379 :
5380 1 : spdm_test_context->case_id = 0x11;
5381 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
5382 : SPDM_VERSION_NUMBER_SHIFT_BIT;
5383 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
5384 1 : spdm_context->connection_info.capability.flags |=
5385 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
5386 1 : spdm_context->connection_info.capability.flags |=
5387 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
5388 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
5389 :
5390 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
5391 : m_libspdm_use_asym_algo, &data,
5392 : &data_size, &hash, &hash_size)) {
5393 0 : assert(false);
5394 : }
5395 1 : libspdm_reset_message_a(spdm_context);
5396 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
5397 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
5398 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
5399 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
5400 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
5401 : m_libspdm_use_measurement_hash_algo;
5402 :
5403 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
5404 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
5405 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
5406 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
5407 : data, data_size);
5408 : #else
5409 1 : libspdm_hash_all(
5410 : spdm_context->connection_info.algorithm.base_hash_algo,
5411 : data, data_size,
5412 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
5413 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
5414 1 : libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
5415 1 : libspdm_get_leaf_cert_public_key_from_cert_chain(
5416 : spdm_context->connection_info.algorithm.base_hash_algo,
5417 : spdm_context->connection_info.algorithm.base_asym_algo,
5418 : data, data_size,
5419 : &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
5420 : #endif
5421 :
5422 1 : heartbeat_period = 0;
5423 1 : libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
5424 1 : status = libspdm_send_receive_key_exchange(
5425 : spdm_context,
5426 : SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
5427 : &session_id, &heartbeat_period, &slot_id_param,
5428 : measurement_hash);
5429 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_SIZE);
5430 1 : free(data);
5431 1 : }
5432 :
5433 1 : static void libspdm_test_requester_key_exchange_err_case18(void **state)
5434 : {
5435 : libspdm_return_t status;
5436 : libspdm_test_context_t *spdm_test_context;
5437 : libspdm_context_t *spdm_context;
5438 : uint32_t session_id;
5439 : uint8_t heartbeat_period;
5440 : uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
5441 : uint8_t slot_id_param;
5442 : void *data;
5443 : size_t data_size;
5444 : void *hash;
5445 : size_t hash_size;
5446 :
5447 1 : spdm_test_context = *state;
5448 1 : spdm_context = spdm_test_context->spdm_context;
5449 :
5450 1 : if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
5451 0 : libspdm_free_session_id(spdm_context,0xFFFFFFFF);
5452 : }
5453 :
5454 1 : spdm_test_context->case_id = 0x12;
5455 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
5456 : SPDM_VERSION_NUMBER_SHIFT_BIT;
5457 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
5458 1 : spdm_context->connection_info.capability.flags |=
5459 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
5460 1 : spdm_context->connection_info.capability.flags |=
5461 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
5462 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
5463 :
5464 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
5465 : m_libspdm_use_asym_algo, &data,
5466 : &data_size, &hash, &hash_size)) {
5467 0 : assert(false);
5468 : }
5469 1 : libspdm_reset_message_a(spdm_context);
5470 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
5471 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
5472 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
5473 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
5474 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
5475 : m_libspdm_use_measurement_hash_algo;
5476 :
5477 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
5478 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
5479 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
5480 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
5481 : data, data_size);
5482 : #else
5483 1 : libspdm_hash_all(
5484 : spdm_context->connection_info.algorithm.base_hash_algo,
5485 : data, data_size,
5486 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
5487 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
5488 1 : libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
5489 1 : libspdm_get_leaf_cert_public_key_from_cert_chain(
5490 : spdm_context->connection_info.algorithm.base_hash_algo,
5491 : spdm_context->connection_info.algorithm.base_asym_algo,
5492 : data, data_size,
5493 : &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
5494 : #endif
5495 :
5496 1 : heartbeat_period = 0;
5497 1 : libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
5498 1 : status = libspdm_send_receive_key_exchange(
5499 : spdm_context,
5500 : SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
5501 : &session_id, &heartbeat_period, &slot_id_param,
5502 : measurement_hash);
5503 1 : assert_int_equal(status, LIBSPDM_STATUS_VERIF_FAIL);
5504 1 : free(data);
5505 1 : }
5506 :
5507 : /**
5508 : * Test 19:
5509 : * Expected Behavior:
5510 : **/
5511 1 : static void libspdm_test_requester_key_exchange_err_case19(void **state)
5512 : {
5513 1 : }
5514 :
5515 1 : static void libspdm_test_requester_key_exchange_err_case20(void **state)
5516 : {
5517 : libspdm_return_t status;
5518 : libspdm_test_context_t *spdm_test_context;
5519 : libspdm_context_t *spdm_context;
5520 : uint32_t session_id;
5521 : uint8_t heartbeat_period;
5522 : uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
5523 : uint8_t slot_id_param;
5524 : void *data;
5525 : size_t data_size;
5526 : void *hash;
5527 : size_t hash_size;
5528 :
5529 1 : spdm_test_context = *state;
5530 1 : spdm_context = spdm_test_context->spdm_context;
5531 :
5532 1 : if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
5533 0 : libspdm_free_session_id(spdm_context,0xFFFFFFFF);
5534 : }
5535 :
5536 1 : spdm_test_context->case_id = 0x14;
5537 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
5538 : SPDM_VERSION_NUMBER_SHIFT_BIT;
5539 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
5540 1 : spdm_context->connection_info.capability.flags |=
5541 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
5542 1 : spdm_context->connection_info.capability.flags |=
5543 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
5544 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
5545 :
5546 :
5547 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
5548 : m_libspdm_use_asym_algo, &data,
5549 : &data_size, &hash, &hash_size)) {
5550 0 : assert(false);
5551 : }
5552 1 : libspdm_reset_message_a(spdm_context);
5553 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
5554 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
5555 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
5556 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
5557 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
5558 : m_libspdm_use_measurement_hash_algo;
5559 :
5560 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
5561 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
5562 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
5563 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
5564 : data, data_size);
5565 : #else
5566 1 : libspdm_hash_all(
5567 : spdm_context->connection_info.algorithm.base_hash_algo,
5568 : data, data_size,
5569 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
5570 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
5571 1 : libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
5572 1 : libspdm_get_leaf_cert_public_key_from_cert_chain(
5573 : spdm_context->connection_info.algorithm.base_hash_algo,
5574 : spdm_context->connection_info.algorithm.base_asym_algo,
5575 : data, data_size,
5576 : &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
5577 : #endif
5578 :
5579 1 : heartbeat_period = 0;
5580 1 : libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
5581 1 : status = libspdm_send_receive_key_exchange(
5582 : spdm_context,
5583 : SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
5584 : &session_id, &heartbeat_period, &slot_id_param,
5585 : measurement_hash);
5586 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
5587 1 : free(data);
5588 1 : }
5589 :
5590 : /**
5591 : * Test 21: OpaqueLength is larger than the recommended value.
5592 : * Expected Behavior: Returns with LIBSPDM_STATUS_INVALID_MSG_FIELD.
5593 : **/
5594 1 : static void libspdm_test_requester_key_exchange_err_case21(void **state)
5595 : {
5596 : libspdm_return_t status;
5597 : libspdm_test_context_t *spdm_test_context;
5598 : libspdm_context_t *spdm_context;
5599 : uint32_t session_id;
5600 : uint8_t slot_id_param;
5601 :
5602 1 : spdm_test_context = *state;
5603 1 : spdm_context = spdm_test_context->spdm_context;
5604 1 : spdm_test_context->case_id = 0x15;
5605 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
5606 : SPDM_VERSION_NUMBER_SHIFT_BIT;
5607 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
5608 1 : spdm_context->connection_info.capability.flags |=
5609 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
5610 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP;
5611 1 : spdm_context->local_context.capability.flags |=
5612 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
5613 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP;
5614 :
5615 1 : status = libspdm_send_receive_key_exchange(
5616 : spdm_context, SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
5617 : &session_id, NULL, &slot_id_param, NULL);
5618 :
5619 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
5620 1 : }
5621 :
5622 : /**
5623 : * Test 22: Requester's PUB_KEY_ID_CAP is 1 but Responder sets MutAuthRequested to 2 (encapsulated
5624 : * flow).
5625 : * Expected Behavior: Returns with LIBSPDM_STATUS_INVALID_MSG_FIELD.
5626 : **/
5627 1 : static void libspdm_test_requester_key_exchange_err_case22(void **state)
5628 : {
5629 : libspdm_return_t status;
5630 : libspdm_test_context_t *spdm_test_context;
5631 : libspdm_context_t *spdm_context;
5632 : uint32_t session_id;
5633 : uint8_t slot_id_param;
5634 :
5635 1 : spdm_test_context = *state;
5636 1 : spdm_context = spdm_test_context->spdm_context;
5637 1 : spdm_test_context->case_id = 0x16;
5638 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
5639 : SPDM_VERSION_NUMBER_SHIFT_BIT;
5640 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
5641 1 : spdm_context->connection_info.capability.flags |=
5642 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
5643 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
5644 1 : spdm_context->local_context.capability.flags |=
5645 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
5646 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP |
5647 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_PUB_KEY_ID_CAP;
5648 :
5649 1 : status = libspdm_send_receive_key_exchange(
5650 : spdm_context, SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
5651 : &session_id, NULL, &slot_id_param, NULL);
5652 :
5653 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
5654 1 : }
5655 :
5656 : /**
5657 : * Test 23: Responder attempts to initiate a mutual authentication encapsulated flow but both
5658 : * endpoints do not support ENCAP_CAP.
5659 : * Expected Behavior: Returns with LIBSPDM_STATUS_INVALID_MSG_FIELD.
5660 : **/
5661 1 : static void libspdm_test_requester_key_exchange_err_case23(void **state)
5662 : {
5663 : libspdm_return_t status;
5664 : libspdm_test_context_t *spdm_test_context;
5665 : libspdm_context_t *spdm_context;
5666 : uint32_t session_id;
5667 : uint8_t slot_id_param;
5668 :
5669 1 : spdm_test_context = *state;
5670 1 : spdm_context = spdm_test_context->spdm_context;
5671 1 : spdm_test_context->case_id = 0x17;
5672 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
5673 : SPDM_VERSION_NUMBER_SHIFT_BIT;
5674 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
5675 1 : spdm_context->connection_info.capability.flags =
5676 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP |
5677 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MAC_CAP |
5678 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
5679 1 : spdm_context->local_context.capability.flags =
5680 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP |
5681 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MAC_CAP |
5682 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP |
5683 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CERT_CAP;
5684 :
5685 1 : status = libspdm_send_receive_key_exchange(
5686 : spdm_context, SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
5687 : &session_id, NULL, &slot_id_param, NULL);
5688 :
5689 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
5690 1 : }
5691 :
5692 1 : static void libspdm_test_requester_key_exchange_err_case24(void **state)
5693 : {
5694 1 : }
5695 :
5696 1 : static void libspdm_test_requester_key_exchange_err_case25(void **state)
5697 : {
5698 1 : }
5699 :
5700 1 : static void libspdm_test_requester_key_exchange_err_case26(void **state)
5701 : {
5702 : libspdm_return_t status;
5703 : libspdm_test_context_t *spdm_test_context;
5704 : libspdm_context_t *spdm_context;
5705 : uint32_t session_id;
5706 : uint8_t heartbeat_period;
5707 : uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
5708 : uint8_t slot_id_param;
5709 : void *data;
5710 : size_t data_size;
5711 : void *hash;
5712 : size_t hash_size;
5713 :
5714 1 : spdm_test_context = *state;
5715 1 : spdm_context = spdm_test_context->spdm_context;
5716 :
5717 1 : if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
5718 0 : libspdm_free_session_id(spdm_context,0xFFFFFFFF);
5719 : }
5720 :
5721 1 : spdm_test_context->case_id = 0x1A;
5722 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
5723 : SPDM_VERSION_NUMBER_SHIFT_BIT;
5724 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
5725 1 : spdm_context->connection_info.capability.flags |=
5726 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
5727 1 : spdm_context->connection_info.capability.flags |=
5728 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
5729 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
5730 :
5731 1 : spdm_context->connection_info.capability.flags |=
5732 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
5733 1 : spdm_context->local_context.capability.flags |=
5734 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
5735 :
5736 :
5737 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
5738 : m_libspdm_use_asym_algo, &data,
5739 : &data_size, &hash, &hash_size)) {
5740 0 : assert(false);
5741 : }
5742 1 : libspdm_reset_message_a(spdm_context);
5743 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
5744 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
5745 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
5746 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
5747 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
5748 : m_libspdm_use_measurement_hash_algo;
5749 :
5750 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
5751 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
5752 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
5753 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
5754 : data, data_size);
5755 : #else
5756 1 : libspdm_hash_all(
5757 : spdm_context->connection_info.algorithm.base_hash_algo,
5758 : data, data_size,
5759 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
5760 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
5761 1 : libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
5762 1 : libspdm_get_leaf_cert_public_key_from_cert_chain(
5763 : spdm_context->connection_info.algorithm.base_hash_algo,
5764 : spdm_context->connection_info.algorithm.base_asym_algo,
5765 : data, data_size,
5766 : &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
5767 : #endif
5768 :
5769 1 : heartbeat_period = 0;
5770 1 : libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
5771 1 : status = libspdm_send_receive_key_exchange(
5772 : spdm_context,
5773 : SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
5774 : &session_id, &heartbeat_period, &slot_id_param,
5775 : measurement_hash);
5776 : /* Clear Mut_auth flags */
5777 1 : spdm_context->connection_info.capability.flags &=
5778 : ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
5779 1 : spdm_context->local_context.capability.flags &=
5780 : ~SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
5781 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
5782 1 : free(data);
5783 1 : }
5784 :
5785 1 : static void libspdm_test_requester_key_exchange_err_case27(void **state)
5786 : {
5787 : libspdm_return_t status;
5788 : libspdm_test_context_t *spdm_test_context;
5789 : libspdm_context_t *spdm_context;
5790 : uint32_t session_id;
5791 : uint8_t heartbeat_period;
5792 : uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
5793 : uint8_t slot_id_param;
5794 : void *data;
5795 : size_t data_size;
5796 : void *hash;
5797 : size_t hash_size;
5798 :
5799 1 : spdm_test_context = *state;
5800 1 : spdm_context = spdm_test_context->spdm_context;
5801 :
5802 1 : if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
5803 0 : libspdm_free_session_id(spdm_context,0xFFFFFFFF);
5804 : }
5805 :
5806 1 : spdm_test_context->case_id = 0x1B;
5807 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
5808 : SPDM_VERSION_NUMBER_SHIFT_BIT;
5809 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
5810 1 : spdm_context->connection_info.capability.flags |=
5811 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
5812 1 : spdm_context->connection_info.capability.flags |=
5813 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
5814 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
5815 :
5816 1 : spdm_context->connection_info.capability.flags |=
5817 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
5818 1 : spdm_context->local_context.capability.flags |=
5819 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
5820 :
5821 :
5822 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
5823 : m_libspdm_use_asym_algo, &data,
5824 : &data_size, &hash, &hash_size)) {
5825 0 : assert(false);
5826 : }
5827 1 : libspdm_reset_message_a(spdm_context);
5828 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
5829 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
5830 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
5831 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
5832 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
5833 : m_libspdm_use_measurement_hash_algo;
5834 :
5835 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
5836 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
5837 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
5838 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
5839 : data, data_size);
5840 : #else
5841 1 : libspdm_hash_all(
5842 : spdm_context->connection_info.algorithm.base_hash_algo,
5843 : data, data_size,
5844 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
5845 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
5846 1 : libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
5847 1 : libspdm_get_leaf_cert_public_key_from_cert_chain(
5848 : spdm_context->connection_info.algorithm.base_hash_algo,
5849 : spdm_context->connection_info.algorithm.base_asym_algo,
5850 : data, data_size,
5851 : &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
5852 : #endif
5853 :
5854 1 : heartbeat_period = 0;
5855 1 : libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
5856 1 : status = libspdm_send_receive_key_exchange(
5857 : spdm_context,
5858 : SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
5859 : &session_id, &heartbeat_period, &slot_id_param,
5860 : measurement_hash);
5861 : /*Clear Mut_auth flags*/
5862 1 : spdm_context->connection_info.capability.flags &=
5863 : ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
5864 1 : spdm_context->local_context.capability.flags &=
5865 : ~SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
5866 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
5867 1 : free(data);
5868 1 : }
5869 :
5870 1 : static void libspdm_test_requester_key_exchange_err_case28(void **state)
5871 : {
5872 : libspdm_return_t status;
5873 : libspdm_test_context_t *spdm_test_context;
5874 : libspdm_context_t *spdm_context;
5875 : uint32_t session_id;
5876 : uint8_t heartbeat_period;
5877 : uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
5878 : uint8_t slot_id_param;
5879 : void *data;
5880 : size_t data_size;
5881 : void *hash;
5882 : size_t hash_size;
5883 :
5884 1 : spdm_test_context = *state;
5885 1 : spdm_context = spdm_test_context->spdm_context;
5886 :
5887 1 : if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
5888 0 : libspdm_free_session_id(spdm_context,0xFFFFFFFF);
5889 : }
5890 :
5891 1 : spdm_test_context->case_id = 0x1C;
5892 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
5893 : SPDM_VERSION_NUMBER_SHIFT_BIT;
5894 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
5895 1 : spdm_context->connection_info.capability.flags |=
5896 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
5897 1 : spdm_context->connection_info.capability.flags |=
5898 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
5899 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
5900 :
5901 1 : spdm_context->connection_info.capability.flags |=
5902 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
5903 1 : spdm_context->local_context.capability.flags |=
5904 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
5905 :
5906 :
5907 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
5908 : m_libspdm_use_asym_algo, &data,
5909 : &data_size, &hash, &hash_size)) {
5910 0 : assert(false);
5911 : }
5912 1 : libspdm_reset_message_a(spdm_context);
5913 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
5914 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
5915 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
5916 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
5917 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
5918 : m_libspdm_use_measurement_hash_algo;
5919 :
5920 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
5921 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
5922 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
5923 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
5924 : data, data_size);
5925 : #else
5926 1 : libspdm_hash_all(
5927 : spdm_context->connection_info.algorithm.base_hash_algo,
5928 : data, data_size,
5929 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
5930 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
5931 1 : libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
5932 1 : libspdm_get_leaf_cert_public_key_from_cert_chain(
5933 : spdm_context->connection_info.algorithm.base_hash_algo,
5934 : spdm_context->connection_info.algorithm.base_asym_algo,
5935 : data, data_size,
5936 : &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
5937 : #endif
5938 :
5939 1 : heartbeat_period = 0;
5940 1 : libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
5941 1 : status = libspdm_send_receive_key_exchange(
5942 : spdm_context,
5943 : SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
5944 : &session_id, &heartbeat_period, &slot_id_param,
5945 : measurement_hash);
5946 : /* Clear Mut_auth flags */
5947 1 : spdm_context->connection_info.capability.flags &=
5948 : ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
5949 1 : spdm_context->local_context.capability.flags &=
5950 : ~SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
5951 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
5952 1 : free(data);
5953 1 : }
5954 :
5955 1 : static void libspdm_test_requester_key_exchange_err_case29(void **state)
5956 : {
5957 1 : }
5958 :
5959 1 : static void libspdm_test_requester_key_exchange_err_case30(void **state)
5960 : {
5961 1 : }
5962 :
5963 : /**
5964 : * Test 31: Fail case, MutAuthRequested bit 0 set, and SlotID is 9 in KEY_EXCHANGE_RSP response
5965 : * message but SlotID should be between 0 and 7 inclusive.
5966 : * Expected Behavior: returns a status of INVALID_MSG_FIELD.
5967 : **/
5968 1 : static void libspdm_test_requester_key_exchange_err_case31(void **state)
5969 : {
5970 : libspdm_return_t status;
5971 : libspdm_test_context_t *spdm_test_context;
5972 : libspdm_context_t *spdm_context;
5973 : uint32_t session_id;
5974 : uint8_t heartbeat_period;
5975 : uint8_t measurement_hash[LIBSPDM_MAX_HASH_SIZE];
5976 : uint8_t slot_id_param;
5977 : void *data;
5978 : size_t data_size;
5979 : void *hash;
5980 : size_t hash_size;
5981 :
5982 1 : spdm_test_context = *state;
5983 1 : spdm_context = spdm_test_context->spdm_context;
5984 :
5985 1 : if(spdm_context->session_info[0].session_id != INVALID_SESSION_ID) {
5986 0 : libspdm_free_session_id(spdm_context,0xFFFFFFFF);
5987 : }
5988 :
5989 1 : spdm_test_context->case_id = 0x1F;
5990 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_11 <<
5991 : SPDM_VERSION_NUMBER_SHIFT_BIT;
5992 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
5993 1 : spdm_context->connection_info.capability.flags |=
5994 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_KEY_EX_CAP;
5995 1 : spdm_context->connection_info.capability.flags |=
5996 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MEAS_CAP_SIG;
5997 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_KEY_EX_CAP;
5998 :
5999 1 : spdm_context->connection_info.capability.flags |=
6000 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
6001 1 : spdm_context->local_context.capability.flags |=
6002 : SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
6003 1 : spdm_context->local_context.capability.flags |= SPDM_GET_CAPABILITIES_REQUEST_FLAGS_CERT_CAP;
6004 :
6005 1 : if (!libspdm_read_responder_public_certificate_chain(m_libspdm_use_hash_algo,
6006 : m_libspdm_use_asym_algo, &data,
6007 : &data_size, &hash, &hash_size)) {
6008 0 : assert(false);
6009 : }
6010 1 : libspdm_reset_message_a(spdm_context);
6011 1 : spdm_context->connection_info.algorithm.base_hash_algo = m_libspdm_use_hash_algo;
6012 1 : spdm_context->connection_info.algorithm.base_asym_algo = m_libspdm_use_asym_algo;
6013 1 : spdm_context->connection_info.algorithm.dhe_named_group = m_libspdm_use_dhe_algo;
6014 1 : spdm_context->connection_info.algorithm.aead_cipher_suite = m_libspdm_use_aead_algo;
6015 1 : spdm_context->connection_info.algorithm.measurement_hash_algo =
6016 : m_libspdm_use_measurement_hash_algo;
6017 :
6018 : #if LIBSPDM_RECORD_TRANSCRIPT_DATA_SUPPORT
6019 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_size = data_size;
6020 : libspdm_copy_mem(spdm_context->connection_info.peer_used_cert_chain[0].buffer,
6021 : sizeof(spdm_context->connection_info.peer_used_cert_chain[0].buffer),
6022 : data, data_size);
6023 : #else
6024 1 : libspdm_hash_all(
6025 : spdm_context->connection_info.algorithm.base_hash_algo,
6026 : data, data_size,
6027 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash);
6028 1 : spdm_context->connection_info.peer_used_cert_chain[0].buffer_hash_size =
6029 1 : libspdm_get_hash_size(spdm_context->connection_info.algorithm.base_hash_algo);
6030 1 : libspdm_get_leaf_cert_public_key_from_cert_chain(
6031 : spdm_context->connection_info.algorithm.base_hash_algo,
6032 : spdm_context->connection_info.algorithm.base_asym_algo,
6033 : data, data_size,
6034 : &spdm_context->connection_info.peer_used_cert_chain[0].leaf_cert_public_key);
6035 : #endif
6036 :
6037 1 : heartbeat_period = 0;
6038 1 : libspdm_zero_mem(measurement_hash, sizeof(measurement_hash));
6039 1 : status = libspdm_send_receive_key_exchange(
6040 : spdm_context,
6041 : SPDM_KEY_EXCHANGE_REQUEST_NO_MEASUREMENT_SUMMARY_HASH, 0, 0,
6042 : &session_id, &heartbeat_period, &slot_id_param,
6043 : measurement_hash);
6044 : /* Clear Mut_auth flags */
6045 1 : spdm_context->connection_info.capability.flags &=
6046 : ~SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_MUT_AUTH_CAP;
6047 1 : spdm_context->local_context.capability.flags &=
6048 : ~SPDM_GET_CAPABILITIES_REQUEST_FLAGS_MUT_AUTH_CAP;
6049 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
6050 1 : free(data);
6051 1 : }
6052 :
6053 1 : int libspdm_req_key_exchange_error_test(void)
6054 : {
6055 1 : const struct CMUnitTest test_cases[] = {
6056 : /* SendRequest failed*/
6057 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case1),
6058 : /* Successful response*/
6059 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case2),
6060 : /* connection_state check failed*/
6061 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case3),
6062 : /* Error response: SPDM_ERROR_CODE_INVALID_REQUEST*/
6063 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case4),
6064 : /* Always SPDM_ERROR_CODE_BUSY*/
6065 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case5),
6066 : /* SPDM_ERROR_CODE_BUSY + Successful response*/
6067 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case6),
6068 : /* Error response: SPDM_ERROR_CODE_REQUEST_RESYNCH*/
6069 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case7),
6070 : /* Always SPDM_ERROR_CODE_RESPONSE_NOT_READY*/
6071 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case8),
6072 : /* SPDM_ERROR_CODE_RESPONSE_NOT_READY + Successful response*/
6073 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case9),
6074 : /* Unexpected errors*/
6075 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case10),
6076 : /* Buffer reset*/
6077 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case11),
6078 : /* Measurement hash 1, returns a measurement hash*/
6079 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case12),
6080 : /* Measurement hash 1, returns a 0x00 array (no TCB components)*/
6081 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case13),
6082 : /* Measurement hash FF, returns a measurement_hash*/
6083 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case14),
6084 : /* Measurement hash 1, returns no measurement_hash*/
6085 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case15),
6086 : /* Measurement hash FF, returns no measurement_hash*/
6087 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case16),
6088 : /* Measurement hash not requested, returns a measurement_hash*/
6089 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case17),
6090 : /* Wrong signature*/
6091 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case18),
6092 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case19),
6093 : /* Heartbeat not supported, heartbeat period different from 0 sent*/
6094 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case20),
6095 : /* Heartbeat supported, heartbeat period different from 0 sent*/
6096 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case21),
6097 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case22),
6098 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case23),
6099 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case24),
6100 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case25),
6101 : /* Muth Auth requested with Encapsulated request and bit 0 set*/
6102 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case26),
6103 : /* Muth Auth requested with implicit get digest and bit 0 set*/
6104 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case27),
6105 : /* Muth Auth requested with Encapsulated request and Muth Auth requested with implicit get digest simultaneously*/
6106 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case28),
6107 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case29),
6108 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case30),
6109 : /* Muth Auth requested bit 0 set, but Invalid SlotID in KEY_EXCHANGE_RSP response message*/
6110 : cmocka_unit_test(libspdm_test_requester_key_exchange_err_case31),
6111 : };
6112 :
6113 1 : libspdm_test_context_t test_context = {
6114 : LIBSPDM_TEST_CONTEXT_VERSION,
6115 : true,
6116 : send_message,
6117 : receive_message,
6118 : };
6119 :
6120 1 : libspdm_setup_test_context(&test_context);
6121 :
6122 1 : return cmocka_run_group_tests(test_cases,
6123 : libspdm_unit_test_group_setup,
6124 : libspdm_unit_test_group_teardown);
6125 : }
6126 :
6127 : #endif /* LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP*/
|