Line data Source code
1 : /**
2 : * Copyright Notice:
3 : * Copyright 2024-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_secured_message_lib.h"
10 :
11 : #if LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP
12 :
13 : uint8_t m_response_total_key_pairs;
14 : uint8_t m_response_key_pair_id;
15 : uint16_t m_response_capabilities;
16 : uint16_t m_response_key_usage_capabilities;
17 : uint16_t m_response_current_key_usage;
18 : uint32_t m_response_asym_algo_capabilities;
19 : uint32_t m_response_current_asym_algo;
20 : uint8_t m_response_assoc_cert_slot_mask;
21 :
22 19 : static libspdm_return_t send_message(
23 : void *spdm_context, size_t request_size, const void *request, uint64_t timeout)
24 : {
25 : libspdm_test_context_t *spdm_test_context;
26 :
27 19 : spdm_test_context = libspdm_get_test_context();
28 19 : switch (spdm_test_context->case_id) {
29 19 : case 0x1:
30 : case 0x2:
31 : case 0x3:
32 : case 0x4:
33 : case 0x5:
34 19 : return LIBSPDM_STATUS_SUCCESS;
35 0 : default:
36 0 : return LIBSPDM_STATUS_SEND_FAIL;
37 : }
38 : }
39 :
40 19 : static libspdm_return_t receive_message(
41 : void *spdm_context, size_t *response_size, void **response, uint64_t timeout)
42 : {
43 : libspdm_test_context_t *spdm_test_context;
44 :
45 19 : spdm_test_context = libspdm_get_test_context();
46 19 : switch (spdm_test_context->case_id) {
47 2 : case 0x1:
48 : case 0x5: {
49 : spdm_key_pair_info_response_t *spdm_response;
50 : size_t spdm_response_size;
51 : size_t transport_header_size;
52 :
53 : uint8_t total_key_pairs;
54 : uint8_t key_pair_id;
55 : uint16_t public_key_info_len;
56 : uint16_t capabilities;
57 : uint16_t key_usage_capabilities;
58 : uint16_t current_key_usage;
59 : uint32_t asym_algo_capabilities;
60 : uint32_t current_asym_algo;
61 : uint8_t assoc_cert_slot_mask;
62 :
63 2 : uint8_t public_key_info_rsa2048[] = {0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
64 : 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00};
65 :
66 2 : key_pair_id = 1;
67 2 : total_key_pairs = 1;
68 2 : public_key_info_len = (uint16_t)sizeof(public_key_info_rsa2048);
69 :
70 2 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
71 2 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
72 2 : spdm_response_size = sizeof(spdm_key_pair_info_response_t) + public_key_info_len;
73 :
74 2 : capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
75 2 : key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
76 2 : current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
77 2 : asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
78 2 : current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
79 :
80 : /*association with slot 1*/
81 2 : assoc_cert_slot_mask = 0x02;
82 :
83 2 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_13;
84 2 : spdm_response->header.request_response_code = SPDM_KEY_PAIR_INFO;
85 2 : spdm_response->header.param1 = 0;
86 2 : spdm_response->header.param2 = 0;
87 2 : spdm_response->total_key_pairs = total_key_pairs;
88 2 : spdm_response->key_pair_id = key_pair_id;
89 2 : spdm_response->capabilities = capabilities;
90 2 : spdm_response->key_usage_capabilities = key_usage_capabilities;
91 2 : spdm_response->current_key_usage = current_key_usage;
92 2 : spdm_response->asym_algo_capabilities = asym_algo_capabilities;
93 2 : spdm_response->current_asym_algo = current_asym_algo;
94 2 : spdm_response->public_key_info_len = public_key_info_len;
95 2 : spdm_response->assoc_cert_slot_mask = assoc_cert_slot_mask;
96 :
97 2 : libspdm_copy_mem((void*)(spdm_response + 1), public_key_info_len,
98 : public_key_info_rsa2048, public_key_info_len);
99 :
100 2 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
101 : false, spdm_response_size,
102 : spdm_response, response_size,
103 : response);
104 : }
105 2 : return LIBSPDM_STATUS_SUCCESS;
106 :
107 15 : case 0x2: {
108 : spdm_key_pair_info_response_t *spdm_response;
109 : size_t spdm_response_size;
110 : size_t transport_header_size;
111 :
112 : uint16_t public_key_info_len;
113 15 : uint8_t public_key_info_rsa2048[] = {0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
114 : 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00};
115 :
116 15 : public_key_info_len = (uint16_t)sizeof(public_key_info_rsa2048);
117 :
118 15 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
119 15 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
120 15 : spdm_response_size = sizeof(spdm_key_pair_info_response_t) + public_key_info_len;
121 :
122 15 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_13;
123 15 : spdm_response->header.request_response_code = SPDM_KEY_PAIR_INFO;
124 15 : spdm_response->header.param1 = 0;
125 15 : spdm_response->header.param2 = 0;
126 15 : spdm_response->total_key_pairs = m_response_total_key_pairs;
127 15 : spdm_response->key_pair_id = m_response_key_pair_id;
128 15 : spdm_response->capabilities = m_response_capabilities;
129 15 : spdm_response->key_usage_capabilities = m_response_key_usage_capabilities;
130 15 : spdm_response->current_key_usage = m_response_current_key_usage;
131 15 : spdm_response->asym_algo_capabilities = m_response_asym_algo_capabilities;
132 15 : spdm_response->current_asym_algo = m_response_current_asym_algo;
133 15 : spdm_response->public_key_info_len = public_key_info_len;
134 15 : spdm_response->assoc_cert_slot_mask = m_response_assoc_cert_slot_mask;
135 :
136 :
137 15 : libspdm_copy_mem((void*)(spdm_response + 1), public_key_info_len,
138 : public_key_info_rsa2048, public_key_info_len);
139 15 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
140 : false, spdm_response_size,
141 : spdm_response, response_size,
142 : response);
143 : }
144 15 : return LIBSPDM_STATUS_SUCCESS;
145 :
146 1 : case 0x3: {
147 : spdm_key_pair_info_response_t *spdm_response;
148 : size_t spdm_response_size;
149 : size_t transport_header_size;
150 :
151 : uint8_t total_key_pairs;
152 : uint8_t key_pair_id;
153 : uint16_t public_key_info_len;
154 : uint16_t capabilities;
155 : uint16_t key_usage_capabilities;
156 : uint16_t current_key_usage;
157 : uint32_t asym_algo_capabilities;
158 : uint32_t current_asym_algo;
159 : uint8_t assoc_cert_slot_mask;
160 : uint8_t *ptr;
161 :
162 1 : uint8_t public_key_info_rsa2048[] = {0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
163 : 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00};
164 :
165 1 : key_pair_id = 1;
166 1 : total_key_pairs = 1;
167 1 : public_key_info_len = (uint16_t)sizeof(public_key_info_rsa2048);
168 :
169 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
170 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
171 1 : spdm_response_size = sizeof(spdm_key_pair_info_response_t) + public_key_info_len +
172 1 : sizeof(uint8_t) + 5 + sizeof(uint8_t) + 4;
173 :
174 1 : capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
175 1 : key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
176 1 : current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
177 1 : asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
178 1 : current_asym_algo = 0;
179 :
180 : /* association with slot 1 */
181 1 : assoc_cert_slot_mask = 0x02;
182 :
183 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_14;
184 1 : spdm_response->header.request_response_code = SPDM_KEY_PAIR_INFO;
185 1 : spdm_response->header.param1 = 0;
186 1 : spdm_response->header.param2 = 0;
187 1 : spdm_response->total_key_pairs = total_key_pairs;
188 1 : spdm_response->key_pair_id = key_pair_id;
189 1 : spdm_response->capabilities = capabilities;
190 1 : spdm_response->key_usage_capabilities = key_usage_capabilities;
191 1 : spdm_response->current_key_usage = current_key_usage;
192 1 : spdm_response->asym_algo_capabilities = asym_algo_capabilities;
193 1 : spdm_response->current_asym_algo = current_asym_algo;
194 1 : spdm_response->public_key_info_len = public_key_info_len;
195 1 : spdm_response->assoc_cert_slot_mask = assoc_cert_slot_mask;
196 :
197 1 : libspdm_copy_mem((void*)(spdm_response + 1), public_key_info_len,
198 : public_key_info_rsa2048, public_key_info_len);
199 :
200 1 : ptr = (uint8_t *)(spdm_response + 1) + public_key_info_len;
201 1 : *ptr = 5;
202 1 : ptr += sizeof(uint8_t);
203 :
204 : /* First 4 bytes carry ML_DSA_44 capability, 5th byte is non-zero to expose cursor bugs. */
205 1 : ptr[0] = 0x01;
206 1 : ptr[1] = 0x00;
207 1 : ptr[2] = 0x00;
208 1 : ptr[3] = 0x00;
209 1 : ptr[4] = 0x04;
210 1 : ptr += 5;
211 :
212 1 : *ptr = 4;
213 1 : ptr += sizeof(uint8_t);
214 :
215 1 : ptr[0] = 0x01;
216 1 : ptr[1] = 0x00;
217 1 : ptr[2] = 0x00;
218 1 : ptr[3] = 0x00;
219 :
220 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
221 : false, spdm_response_size,
222 : spdm_response, response_size,
223 : response);
224 : }
225 1 : return LIBSPDM_STATUS_SUCCESS;
226 :
227 1 : case 0x4: {
228 : spdm_key_pair_info_response_t *spdm_response;
229 : size_t spdm_response_size;
230 : size_t transport_header_size;
231 :
232 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
233 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
234 :
235 : /* Truncated response carrying only the SPDM header. total_key_pairs and
236 : * key_pair_id are absent. */
237 1 : spdm_response_size = sizeof(spdm_message_header_t);
238 :
239 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_13;
240 1 : spdm_response->header.request_response_code = SPDM_KEY_PAIR_INFO;
241 1 : spdm_response->header.param1 = 0;
242 1 : spdm_response->header.param2 = 0;
243 :
244 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
245 : false, spdm_response_size,
246 : spdm_response, response_size,
247 : response);
248 : }
249 1 : return LIBSPDM_STATUS_SUCCESS;
250 :
251 0 : default:
252 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
253 : }
254 : }
255 :
256 : /**
257 : * Test 1: Successful response to get key pair info
258 : * Expected Behavior: get a LIBSPDM_STATUS_SUCCESS return code
259 : **/
260 1 : static void req_get_key_pair_info_case1(void **state)
261 : {
262 : libspdm_return_t status;
263 : libspdm_test_context_t *spdm_test_context;
264 : libspdm_context_t *spdm_context;
265 :
266 : uint8_t key_pair_id;
267 : uint8_t associated_slot_id;
268 : uint8_t total_key_pairs;
269 : uint16_t capabilities;
270 : uint16_t key_usage_capabilities;
271 : uint16_t current_key_usage;
272 : uint32_t asym_algo_capabilities;
273 : uint32_t current_asym_algo;
274 : uint32_t pqc_asym_algo_capabilities;
275 : uint32_t current_pqc_asym_algo;
276 : uint16_t public_key_info_len;
277 : uint8_t assoc_cert_slot_mask;
278 : uint8_t public_key_info[SPDM_MAX_PUBLIC_KEY_INFO_LEN];
279 :
280 1 : key_pair_id = 1;
281 1 : associated_slot_id = 1;
282 1 : spdm_test_context = *state;
283 1 : spdm_context = spdm_test_context->spdm_context;
284 1 : spdm_test_context->case_id = 0x1;
285 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
286 : SPDM_VERSION_NUMBER_SHIFT_BIT;
287 :
288 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
289 1 : spdm_context->connection_info.capability.flags |=
290 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP |
291 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_INFO_CAP;
292 :
293 1 : spdm_context->connection_info.peer_key_pair_id[associated_slot_id] = key_pair_id;
294 1 : public_key_info_len = SPDM_MAX_PUBLIC_KEY_INFO_LEN;
295 :
296 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
297 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
298 : &asym_algo_capabilities, ¤t_asym_algo,
299 : &pqc_asym_algo_capabilities, ¤t_pqc_asym_algo,
300 : &assoc_cert_slot_mask, &public_key_info_len,
301 : public_key_info);
302 :
303 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
304 1 : assert_int_equal(capabilities, SPDM_KEY_PAIR_CAP_GEN_KEY_CAP);
305 1 : assert_int_equal(key_usage_capabilities, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE);
306 1 : assert_int_equal(current_key_usage, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE);
307 1 : assert_int_equal(asym_algo_capabilities, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
308 1 : assert_int_equal(current_asym_algo, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
309 1 : }
310 :
311 : /**
312 : * Test 2: The collection of multiple sub-cases for invalid combination.
313 : * Expected Behavior: get a LIBSPDM_STATUS_INVALID_MSG_FIELD return code
314 : **/
315 1 : static void req_get_key_pair_info_case2(void **state)
316 : {
317 : libspdm_return_t status;
318 : libspdm_test_context_t *spdm_test_context;
319 : libspdm_context_t *spdm_context;
320 :
321 : uint8_t key_pair_id;
322 : uint8_t associated_slot_id;
323 : uint8_t total_key_pairs;
324 : uint16_t capabilities;
325 : uint16_t key_usage_capabilities;
326 : uint16_t current_key_usage;
327 : uint32_t asym_algo_capabilities;
328 : uint32_t current_asym_algo;
329 : uint32_t pqc_asym_algo_capabilities;
330 : uint32_t current_pqc_asym_algo;
331 : uint16_t public_key_info_len;
332 : uint8_t assoc_cert_slot_mask;
333 : uint8_t public_key_info[SPDM_MAX_PUBLIC_KEY_INFO_LEN];
334 :
335 1 : spdm_test_context = *state;
336 1 : spdm_context = spdm_test_context->spdm_context;
337 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
338 : SPDM_VERSION_NUMBER_SHIFT_BIT;
339 :
340 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
341 1 : spdm_context->connection_info.capability.flags |=
342 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP;
343 :
344 1 : spdm_test_context->case_id = 0x2;
345 1 : public_key_info_len = SPDM_MAX_PUBLIC_KEY_INFO_LEN;
346 :
347 : /* Sub Case 1: key_pair_id > total_key_pairs*/
348 1 : key_pair_id = 2;
349 1 : associated_slot_id = 1;
350 1 : spdm_context->connection_info.peer_key_pair_id[associated_slot_id] = key_pair_id;
351 :
352 1 : m_response_total_key_pairs = 1;
353 1 : m_response_key_pair_id = 2;
354 1 : m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
355 1 : m_response_key_usage_capabilities =SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
356 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
357 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
358 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
359 : /* association with slot 1*/
360 1 : m_response_assoc_cert_slot_mask = 0x2;
361 :
362 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
363 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
364 : &asym_algo_capabilities, ¤t_asym_algo,
365 : &pqc_asym_algo_capabilities, ¤t_pqc_asym_algo,
366 : &assoc_cert_slot_mask, &public_key_info_len,
367 : public_key_info);
368 :
369 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
370 :
371 : /* Sub Case 2: responder and requester, the KeyPairID are not consistent.*/
372 1 : key_pair_id = 0xA0;
373 1 : associated_slot_id = 1;
374 1 : spdm_context->connection_info.peer_key_pair_id[associated_slot_id] = key_pair_id;
375 :
376 1 : m_response_total_key_pairs = 1;
377 1 : m_response_key_pair_id = 1;
378 1 : m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
379 1 : m_response_key_usage_capabilities =SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
380 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
381 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
382 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
383 : /* association with slot 1*/
384 1 : m_response_assoc_cert_slot_mask = 0x2;
385 :
386 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
387 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
388 : &asym_algo_capabilities, ¤t_asym_algo,
389 : &pqc_asym_algo_capabilities, ¤t_pqc_asym_algo,
390 : &assoc_cert_slot_mask, &public_key_info_len,
391 : public_key_info);
392 :
393 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
394 :
395 : /* Sub Case 3: not set capabilities.*/
396 1 : key_pair_id = 1;
397 1 : associated_slot_id = 1;
398 1 : spdm_context->connection_info.peer_key_pair_id[associated_slot_id] = key_pair_id;
399 :
400 1 : m_response_total_key_pairs = 1;
401 1 : m_response_key_pair_id = 1;
402 1 : m_response_capabilities = 0;
403 1 : m_response_key_usage_capabilities =SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
404 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
405 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
406 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
407 : /* association with slot 1*/
408 1 : m_response_assoc_cert_slot_mask = 0x2;
409 :
410 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
411 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
412 : &asym_algo_capabilities, ¤t_asym_algo,
413 : &pqc_asym_algo_capabilities, ¤t_pqc_asym_algo,
414 : &assoc_cert_slot_mask, &public_key_info_len,
415 : public_key_info);
416 :
417 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
418 1 : assert_int_equal(capabilities, 0);
419 1 : assert_int_equal(key_usage_capabilities, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE);
420 1 : assert_int_equal(current_key_usage, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE);
421 1 : assert_int_equal(asym_algo_capabilities, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
422 1 : assert_int_equal(current_asym_algo, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
423 :
424 : /* Sub Case 4: This bit shall not be set if CertAssocCap is not set.
425 : * Set the ShareableCap bit and not set the CertAssocCap bit.*/
426 1 : key_pair_id = 1;
427 1 : associated_slot_id = 1;
428 1 : spdm_context->connection_info.peer_key_pair_id[associated_slot_id] = key_pair_id;
429 :
430 1 : m_response_total_key_pairs = 1;
431 1 : m_response_key_pair_id = 1;
432 :
433 1 : m_response_capabilities = 0;
434 1 : m_response_capabilities |= SPDM_KEY_PAIR_CAP_SHAREABLE_CAP;
435 1 : m_response_capabilities &= ~SPDM_KEY_PAIR_CAP_CERT_ASSOC_CAP;
436 :
437 1 : m_response_key_usage_capabilities =SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
438 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
439 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
440 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
441 : /* association with slot 1*/
442 1 : m_response_assoc_cert_slot_mask = 0x2;
443 :
444 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
445 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
446 : &asym_algo_capabilities, ¤t_asym_algo,
447 : &pqc_asym_algo_capabilities, ¤t_pqc_asym_algo,
448 : &assoc_cert_slot_mask, &public_key_info_len,
449 : public_key_info);
450 :
451 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
452 :
453 : /* Sub Case 5: KeyUsageCapabilities at least one bit shall be set, not set KeyUsageCapabilities.*/
454 1 : m_response_total_key_pairs = 1;
455 1 : m_response_key_pair_id = 1;
456 1 : m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
457 1 : m_response_key_usage_capabilities =0;
458 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
459 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
460 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
461 : /* association with slot 1*/
462 1 : m_response_assoc_cert_slot_mask = 0x2;
463 :
464 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
465 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
466 : &asym_algo_capabilities, ¤t_asym_algo,
467 : &pqc_asym_algo_capabilities, ¤t_pqc_asym_algo,
468 : &assoc_cert_slot_mask, &public_key_info_len,
469 : public_key_info);
470 :
471 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
472 :
473 : /* Sub Case 6: KeyUsageCapabilities Set multiple bits.*/
474 1 : m_response_total_key_pairs = 1;
475 1 : m_response_key_pair_id = 1;
476 1 : m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
477 1 : m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE |
478 : SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE |
479 : SPDM_KEY_USAGE_BIT_MASK_MEASUREMENT_USE |
480 : SPDM_KEY_USAGE_BIT_MASK_ENDPOINT_INFO_USE |
481 : SPDM_KEY_USAGE_BIT_MASK_STANDARDS_KEY_USE |
482 : SPDM_KEY_USAGE_BIT_MASK_VENDOR_KEY_USE;
483 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
484 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
485 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
486 : /* association with slot 1*/
487 1 : m_response_assoc_cert_slot_mask = 0x2;
488 :
489 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
490 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
491 : &asym_algo_capabilities, ¤t_asym_algo,
492 : &pqc_asym_algo_capabilities, ¤t_pqc_asym_algo,
493 : &assoc_cert_slot_mask, &public_key_info_len,
494 : public_key_info);
495 :
496 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
497 1 : assert_int_equal(capabilities, SPDM_KEY_PAIR_CAP_GEN_KEY_CAP);
498 1 : assert_int_equal(key_usage_capabilities, SPDM_KEY_USAGE_BIT_MASK);
499 1 : assert_int_equal(current_key_usage, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE);
500 1 : assert_int_equal(asym_algo_capabilities, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
501 1 : assert_int_equal(current_asym_algo, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
502 :
503 : /* Sub Case 7: not set CurrentKeyUsage*/
504 1 : m_response_total_key_pairs = 1;
505 1 : m_response_key_pair_id = 1;
506 1 : m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
507 1 : m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
508 1 : m_response_current_key_usage = 0;
509 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
510 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
511 : /* association with slot 1*/
512 1 : m_response_assoc_cert_slot_mask = 0x2;
513 :
514 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
515 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
516 : &asym_algo_capabilities, ¤t_asym_algo,
517 : &pqc_asym_algo_capabilities, ¤t_pqc_asym_algo,
518 : &assoc_cert_slot_mask, &public_key_info_len,
519 : public_key_info);
520 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
521 1 : assert_int_equal(capabilities, SPDM_KEY_PAIR_CAP_GEN_KEY_CAP);
522 1 : assert_int_equal(key_usage_capabilities, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE);
523 1 : assert_int_equal(current_key_usage, 0);
524 1 : assert_int_equal(asym_algo_capabilities, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
525 1 : assert_int_equal(current_asym_algo, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
526 :
527 : /* Sub Case 8: CurrentKeyUsage Set multiple bits.*/
528 1 : m_response_total_key_pairs = 1;
529 1 : m_response_key_pair_id = 1;
530 1 : m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
531 1 : m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
532 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE |
533 : SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE |
534 : SPDM_KEY_USAGE_BIT_MASK_MEASUREMENT_USE |
535 : SPDM_KEY_USAGE_BIT_MASK_ENDPOINT_INFO_USE |
536 : SPDM_KEY_USAGE_BIT_MASK_STANDARDS_KEY_USE |
537 : SPDM_KEY_USAGE_BIT_MASK_VENDOR_KEY_USE;
538 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
539 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
540 : /* association with slot 1*/
541 1 : m_response_assoc_cert_slot_mask = 0x2;
542 :
543 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
544 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
545 : &asym_algo_capabilities, ¤t_asym_algo,
546 : &pqc_asym_algo_capabilities, ¤t_pqc_asym_algo,
547 : &assoc_cert_slot_mask, &public_key_info_len,
548 : public_key_info);
549 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
550 :
551 : /* Sub Case 9: CurrentKeyUsage and KeyUsageCapabilities, Set multiple bits.*/
552 1 : m_response_total_key_pairs = 1;
553 1 : m_response_key_pair_id = 1;
554 1 : m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
555 1 : m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE |
556 : SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE;
557 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE |
558 : SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE;
559 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
560 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
561 : /* association with slot 1*/
562 1 : m_response_assoc_cert_slot_mask = 0x2;
563 :
564 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
565 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
566 : &asym_algo_capabilities, ¤t_asym_algo,
567 : &pqc_asym_algo_capabilities, ¤t_pqc_asym_algo,
568 : &assoc_cert_slot_mask, &public_key_info_len,
569 : public_key_info);
570 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
571 1 : assert_int_equal(capabilities, SPDM_KEY_PAIR_CAP_GEN_KEY_CAP);
572 1 : assert_int_equal(key_usage_capabilities, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE |
573 : SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE);
574 1 : assert_int_equal(current_key_usage, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE |
575 : SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE);
576 1 : assert_int_equal(asym_algo_capabilities, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
577 1 : assert_int_equal(current_asym_algo, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
578 :
579 : /* Sub Case 10: CurrentKeyUsage and KeyUsageCapabilities are not consistent.*/
580 1 : m_response_total_key_pairs = 1;
581 1 : m_response_key_pair_id = 1;
582 1 : m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
583 1 : m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE;
584 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
585 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
586 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
587 : /* association with slot 1*/
588 1 : m_response_assoc_cert_slot_mask = 0x2;
589 :
590 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
591 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
592 : &asym_algo_capabilities, ¤t_asym_algo,
593 : &pqc_asym_algo_capabilities, ¤t_pqc_asym_algo,
594 : &assoc_cert_slot_mask, &public_key_info_len,
595 : public_key_info);
596 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
597 :
598 : /* Sub Case 11: AsymAlgoCapabilities, at least one bit shall be set.not set AsymAlgoCapabilities*/
599 1 : m_response_total_key_pairs = 1;
600 1 : m_response_key_pair_id = 1;
601 1 : m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
602 1 : m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
603 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
604 1 : m_response_asym_algo_capabilities = 0;
605 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
606 : /* association with slot 1*/
607 1 : m_response_assoc_cert_slot_mask = 0x2;
608 :
609 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
610 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
611 : &asym_algo_capabilities, ¤t_asym_algo,
612 : &pqc_asym_algo_capabilities, ¤t_pqc_asym_algo,
613 : &assoc_cert_slot_mask, &public_key_info_len,
614 : public_key_info);
615 :
616 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
617 :
618 : /* Sub Case 12: AsymAlgoCapabilities Set multiple bits.*/
619 1 : m_response_total_key_pairs = 1;
620 1 : m_response_key_pair_id = 1;
621 1 : m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
622 1 : m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
623 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
624 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048 |
625 : SPDM_KEY_PAIR_ASYM_ALGO_CAP_ED448;
626 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
627 : /* association with slot 1*/
628 1 : m_response_assoc_cert_slot_mask = 0x2;
629 :
630 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
631 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
632 : &asym_algo_capabilities, ¤t_asym_algo,
633 : &pqc_asym_algo_capabilities, ¤t_pqc_asym_algo,
634 : &assoc_cert_slot_mask, &public_key_info_len,
635 : public_key_info);
636 :
637 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
638 1 : assert_int_equal(capabilities, SPDM_KEY_PAIR_CAP_GEN_KEY_CAP);
639 1 : assert_int_equal(key_usage_capabilities, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE);
640 1 : assert_int_equal(current_key_usage, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE);
641 1 : assert_int_equal(asym_algo_capabilities, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048 |
642 : SPDM_KEY_PAIR_ASYM_ALGO_CAP_ED448);
643 1 : assert_int_equal(current_asym_algo, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
644 :
645 : /* Sub Case 13: CurrentAsymAlgo Set bits more than one bit.*/
646 1 : m_response_total_key_pairs = 1;
647 1 : m_response_key_pair_id = 1;
648 1 : m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
649 1 : m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
650 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
651 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
652 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048 |
653 : SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA3072;
654 : /* association with slot 1*/
655 1 : m_response_assoc_cert_slot_mask = 0x2;
656 :
657 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
658 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
659 : &asym_algo_capabilities, ¤t_asym_algo,
660 : &pqc_asym_algo_capabilities, ¤t_pqc_asym_algo,
661 : &assoc_cert_slot_mask, &public_key_info_len,
662 : public_key_info);
663 :
664 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
665 :
666 : /* Sub Case 14: AsymAlgoCapabilities and AsymAlgoCapabilities are not consistent.*/
667 1 : m_response_total_key_pairs = 1;
668 1 : m_response_key_pair_id = 1;
669 1 : m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
670 1 : m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
671 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
672 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA3072;
673 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
674 : /* association with slot 1*/
675 1 : m_response_assoc_cert_slot_mask = 0x2;
676 :
677 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
678 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
679 : &asym_algo_capabilities, ¤t_asym_algo,
680 : &pqc_asym_algo_capabilities, ¤t_pqc_asym_algo,
681 : &assoc_cert_slot_mask, &public_key_info_len,
682 : public_key_info);
683 :
684 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
685 :
686 : /* Sub Case 15: AssocCertSlotMask set more than one bit, but ShareableCap is not set.*/
687 1 : m_response_total_key_pairs = 1;
688 1 : m_response_key_pair_id = 1;
689 1 : m_response_capabilities = 0;
690 1 : m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
691 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
692 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
693 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
694 : /* association with slot 1*/
695 1 : m_response_assoc_cert_slot_mask = 0xFF;
696 :
697 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
698 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
699 : &asym_algo_capabilities, ¤t_asym_algo,
700 : &pqc_asym_algo_capabilities, ¤t_pqc_asym_algo,
701 : &assoc_cert_slot_mask, &public_key_info_len,
702 : public_key_info);
703 :
704 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
705 1 : }
706 :
707 : /**
708 : * Test 3: SPDM 1.4 KEY_PAIR_INFO parser must advance using raw PQC lengths.
709 : * Expected Behavior: get a LIBSPDM_STATUS_SUCCESS return code
710 : **/
711 1 : static void req_get_key_pair_info_case3(void **state)
712 : {
713 : libspdm_return_t status;
714 : libspdm_test_context_t *spdm_test_context;
715 : libspdm_context_t *spdm_context;
716 :
717 : uint8_t key_pair_id;
718 : uint8_t associated_slot_id;
719 : uint8_t total_key_pairs;
720 : uint16_t capabilities;
721 : uint16_t key_usage_capabilities;
722 : uint16_t current_key_usage;
723 : uint32_t asym_algo_capabilities;
724 : uint32_t current_asym_algo;
725 : uint32_t pqc_asym_algo_capabilities;
726 : uint32_t current_pqc_asym_algo;
727 : uint16_t public_key_info_len;
728 : uint8_t assoc_cert_slot_mask;
729 : uint8_t public_key_info[SPDM_MAX_PUBLIC_KEY_INFO_LEN];
730 :
731 1 : key_pair_id = 1;
732 1 : associated_slot_id = 1;
733 1 : spdm_test_context = *state;
734 1 : spdm_context = spdm_test_context->spdm_context;
735 1 : spdm_test_context->case_id = 0x3;
736 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_14 <<
737 : SPDM_VERSION_NUMBER_SHIFT_BIT;
738 :
739 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
740 1 : spdm_context->connection_info.capability.flags |=
741 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP |
742 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_INFO_CAP;
743 :
744 1 : spdm_context->connection_info.peer_key_pair_id[associated_slot_id] = key_pair_id;
745 1 : public_key_info_len = SPDM_MAX_PUBLIC_KEY_INFO_LEN;
746 :
747 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
748 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
749 : &asym_algo_capabilities, ¤t_asym_algo,
750 : &pqc_asym_algo_capabilities, ¤t_pqc_asym_algo,
751 : &assoc_cert_slot_mask, &public_key_info_len,
752 : public_key_info);
753 :
754 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
755 1 : assert_int_equal(pqc_asym_algo_capabilities, SPDM_KEY_PAIR_PQC_ASYM_ALGO_CAP_ML_DSA_44);
756 1 : assert_int_equal(current_pqc_asym_algo, SPDM_KEY_PAIR_PQC_ASYM_ALGO_CAP_ML_DSA_44);
757 1 : }
758 :
759 : /**
760 : * Test 4: Response truncated to the SPDM header, so total_key_pairs and key_pair_id
761 : * are not present.
762 : * Expected Behavior: rejected with LIBSPDM_STATUS_INVALID_MSG_SIZE, without reading
763 : * the absent fields.
764 : **/
765 1 : static void req_get_key_pair_info_case4(void **state)
766 : {
767 : libspdm_return_t status;
768 : libspdm_test_context_t *spdm_test_context;
769 : libspdm_context_t *spdm_context;
770 :
771 : uint8_t key_pair_id;
772 : uint8_t associated_slot_id;
773 : uint8_t total_key_pairs;
774 : uint16_t capabilities;
775 : uint16_t key_usage_capabilities;
776 : uint16_t current_key_usage;
777 : uint32_t asym_algo_capabilities;
778 : uint32_t current_asym_algo;
779 : uint32_t pqc_asym_algo_capabilities;
780 : uint32_t current_pqc_asym_algo;
781 : uint16_t public_key_info_len;
782 : uint8_t assoc_cert_slot_mask;
783 : uint8_t public_key_info[SPDM_MAX_PUBLIC_KEY_INFO_LEN];
784 :
785 1 : key_pair_id = 1;
786 1 : associated_slot_id = 1;
787 1 : spdm_test_context = *state;
788 1 : spdm_context = spdm_test_context->spdm_context;
789 1 : spdm_test_context->case_id = 0x4;
790 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
791 : SPDM_VERSION_NUMBER_SHIFT_BIT;
792 :
793 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
794 1 : spdm_context->connection_info.capability.flags |=
795 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP |
796 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_INFO_CAP;
797 :
798 1 : spdm_context->connection_info.peer_key_pair_id[associated_slot_id] = key_pair_id;
799 1 : public_key_info_len = SPDM_MAX_PUBLIC_KEY_INFO_LEN;
800 :
801 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
802 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
803 : &asym_algo_capabilities, ¤t_asym_algo,
804 : &pqc_asym_algo_capabilities, ¤t_pqc_asym_algo,
805 : &assoc_cert_slot_mask, &public_key_info_len,
806 : public_key_info);
807 :
808 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_SIZE);
809 1 : }
810 :
811 : /**
812 : * Test 5: The Requester's PublicKeyInfo buffer is smaller than the KEY_PAIR_INFO response's
813 : * PublicKeyInfoLen.
814 : * Expected Behavior: get a LIBSPDM_STATUS_BUFFER_TOO_SMALL return code
815 : **/
816 1 : static void req_get_key_pair_info_case5(void **state)
817 : {
818 : libspdm_return_t status;
819 : libspdm_test_context_t *spdm_test_context;
820 : libspdm_context_t *spdm_context;
821 :
822 : uint8_t key_pair_id;
823 : uint8_t associated_slot_id;
824 : uint8_t total_key_pairs;
825 : uint16_t capabilities;
826 : uint16_t key_usage_capabilities;
827 : uint16_t current_key_usage;
828 : uint32_t asym_algo_capabilities;
829 : uint32_t current_asym_algo;
830 : uint32_t pqc_asym_algo_capabilities;
831 : uint32_t current_pqc_asym_algo;
832 : uint16_t public_key_info_len;
833 : uint8_t assoc_cert_slot_mask;
834 : uint8_t public_key_info[SPDM_MAX_PUBLIC_KEY_INFO_LEN];
835 :
836 1 : key_pair_id = 1;
837 1 : associated_slot_id = 1;
838 1 : spdm_test_context = *state;
839 1 : spdm_context = spdm_test_context->spdm_context;
840 1 : spdm_test_context->case_id = 0x5;
841 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
842 : SPDM_VERSION_NUMBER_SHIFT_BIT;
843 :
844 1 : spdm_context->connection_info.connection_state = LIBSPDM_CONNECTION_STATE_NEGOTIATED;
845 1 : spdm_context->connection_info.capability.flags |=
846 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP |
847 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_INFO_CAP;
848 :
849 1 : spdm_context->connection_info.peer_key_pair_id[associated_slot_id] = key_pair_id;
850 :
851 : /* Case 0x5 responds with the same 15-byte RSA 2048 PublicKeyInfo as case 0x1. */
852 1 : public_key_info_len = 14;
853 :
854 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
855 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
856 : &asym_algo_capabilities, ¤t_asym_algo,
857 : &pqc_asym_algo_capabilities, ¤t_pqc_asym_algo,
858 : &assoc_cert_slot_mask, &public_key_info_len,
859 : public_key_info);
860 :
861 1 : assert_int_equal(status, LIBSPDM_STATUS_BUFFER_TOO_SMALL);
862 1 : }
863 :
864 1 : int libspdm_req_get_key_pair_info_test(void)
865 : {
866 1 : const struct CMUnitTest test_cases[] = {
867 : /* Successful response to get key pair info, key_pair_id is 1*/
868 : cmocka_unit_test(req_get_key_pair_info_case1),
869 : /* The collection of multiple sub-cases for invalid combination.*/
870 : cmocka_unit_test(req_get_key_pair_info_case2),
871 : /* SPDM 1.4 oversized PQC length uses raw_len for cursor advancement */
872 : cmocka_unit_test(req_get_key_pair_info_case3),
873 : /* Header-only response is rejected before reading the body fields */
874 : cmocka_unit_test(req_get_key_pair_info_case4),
875 : /* The Requester's PublicKeyInfo buffer is too small */
876 : cmocka_unit_test(req_get_key_pair_info_case5),
877 : };
878 :
879 1 : libspdm_test_context_t test_context = {
880 : LIBSPDM_TEST_CONTEXT_VERSION,
881 : true,
882 : send_message,
883 : receive_message,
884 : };
885 :
886 1 : libspdm_setup_test_context(&test_context);
887 :
888 1 : return cmocka_run_group_tests(test_cases,
889 : libspdm_unit_test_group_setup,
890 : libspdm_unit_test_group_teardown);
891 : }
892 :
893 : #endif /*LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP*/
|