Line data Source code
1 : /**
2 : * Copyright Notice:
3 : * Copyright 2024-2025 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 16 : 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 16 : spdm_test_context = libspdm_get_test_context();
28 16 : switch (spdm_test_context->case_id) {
29 16 : case 0x1:
30 : case 0x2:
31 16 : return LIBSPDM_STATUS_SUCCESS;
32 0 : default:
33 0 : return LIBSPDM_STATUS_SEND_FAIL;
34 : }
35 : }
36 :
37 16 : static libspdm_return_t receive_message(
38 : void *spdm_context, size_t *response_size, void **response, uint64_t timeout)
39 : {
40 : libspdm_test_context_t *spdm_test_context;
41 :
42 16 : spdm_test_context = libspdm_get_test_context();
43 16 : switch (spdm_test_context->case_id) {
44 1 : case 0x1: {
45 : spdm_key_pair_info_response_t *spdm_response;
46 : size_t spdm_response_size;
47 : size_t transport_header_size;
48 :
49 : uint8_t total_key_pairs;
50 : uint8_t key_pair_id;
51 : uint16_t public_key_info_len;
52 : uint16_t capabilities;
53 : uint16_t key_usage_capabilities;
54 : uint16_t current_key_usage;
55 : uint32_t asym_algo_capabilities;
56 : uint32_t current_asym_algo;
57 : uint8_t assoc_cert_slot_mask;
58 :
59 1 : uint8_t public_key_info_rsa2048[] = {0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
60 : 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00};
61 :
62 1 : key_pair_id = 1;
63 1 : total_key_pairs = 1;
64 1 : public_key_info_len = (uint16_t)sizeof(public_key_info_rsa2048);
65 :
66 1 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
67 1 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
68 1 : spdm_response_size = sizeof(spdm_key_pair_info_response_t) + public_key_info_len;
69 :
70 1 : capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
71 1 : key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
72 1 : current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
73 1 : asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
74 1 : current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
75 :
76 : /*association with slot 1*/
77 1 : assoc_cert_slot_mask = 0x02;
78 :
79 1 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_13;
80 1 : spdm_response->header.request_response_code = SPDM_KEY_PAIR_INFO;
81 1 : spdm_response->header.param1 = 0;
82 1 : spdm_response->header.param2 = 0;
83 1 : spdm_response->total_key_pairs = total_key_pairs;
84 1 : spdm_response->key_pair_id = key_pair_id;
85 1 : spdm_response->capabilities = capabilities;
86 1 : spdm_response->key_usage_capabilities = key_usage_capabilities;
87 1 : spdm_response->current_key_usage = current_key_usage;
88 1 : spdm_response->asym_algo_capabilities = asym_algo_capabilities;
89 1 : spdm_response->current_asym_algo = current_asym_algo;
90 1 : spdm_response->public_key_info_len = public_key_info_len;
91 1 : spdm_response->assoc_cert_slot_mask = assoc_cert_slot_mask;
92 :
93 1 : libspdm_copy_mem((void*)(spdm_response + 1), public_key_info_len,
94 : public_key_info_rsa2048, public_key_info_len);
95 :
96 1 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
97 : false, spdm_response_size,
98 : spdm_response, response_size,
99 : response);
100 : }
101 1 : return LIBSPDM_STATUS_SUCCESS;
102 :
103 15 : case 0x2: {
104 : spdm_key_pair_info_response_t *spdm_response;
105 : size_t spdm_response_size;
106 : size_t transport_header_size;
107 :
108 : uint16_t public_key_info_len;
109 15 : uint8_t public_key_info_rsa2048[] = {0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
110 : 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00};
111 :
112 15 : public_key_info_len = (uint16_t)sizeof(public_key_info_rsa2048);
113 :
114 15 : transport_header_size = LIBSPDM_TEST_TRANSPORT_HEADER_SIZE;
115 15 : spdm_response = (void *)((uint8_t *)*response + transport_header_size);
116 15 : spdm_response_size = sizeof(spdm_key_pair_info_response_t) + public_key_info_len;
117 :
118 15 : spdm_response->header.spdm_version = SPDM_MESSAGE_VERSION_13;
119 15 : spdm_response->header.request_response_code = SPDM_KEY_PAIR_INFO;
120 15 : spdm_response->header.param1 = 0;
121 15 : spdm_response->header.param2 = 0;
122 15 : spdm_response->total_key_pairs = m_response_total_key_pairs;
123 15 : spdm_response->key_pair_id = m_response_key_pair_id;
124 15 : spdm_response->capabilities = m_response_capabilities;
125 15 : spdm_response->key_usage_capabilities = m_response_key_usage_capabilities;
126 15 : spdm_response->current_key_usage = m_response_current_key_usage;
127 15 : spdm_response->asym_algo_capabilities = m_response_asym_algo_capabilities;
128 15 : spdm_response->current_asym_algo = m_response_current_asym_algo;
129 15 : spdm_response->public_key_info_len = public_key_info_len;
130 15 : spdm_response->assoc_cert_slot_mask = m_response_assoc_cert_slot_mask;
131 :
132 :
133 15 : libspdm_copy_mem((void*)(spdm_response + 1), public_key_info_len,
134 : public_key_info_rsa2048, public_key_info_len);
135 15 : libspdm_transport_test_encode_message(spdm_context, NULL, false,
136 : false, spdm_response_size,
137 : spdm_response, response_size,
138 : response);
139 : }
140 15 : return LIBSPDM_STATUS_SUCCESS;
141 :
142 0 : default:
143 0 : return LIBSPDM_STATUS_RECEIVE_FAIL;
144 : }
145 : }
146 :
147 : /**
148 : * Test 1: Successful response to get key pair info
149 : * Expected Behavior: get a LIBSPDM_STATUS_SUCCESS return code
150 : **/
151 1 : static void req_get_key_pair_info_case1(void **state)
152 : {
153 : libspdm_return_t status;
154 : libspdm_test_context_t *spdm_test_context;
155 : libspdm_context_t *spdm_context;
156 :
157 : uint8_t key_pair_id;
158 : uint8_t associated_slot_id;
159 : uint8_t total_key_pairs;
160 : uint16_t capabilities;
161 : uint16_t key_usage_capabilities;
162 : uint16_t current_key_usage;
163 : uint32_t asym_algo_capabilities;
164 : uint32_t current_asym_algo;
165 : uint16_t public_key_info_len;
166 : uint8_t assoc_cert_slot_mask;
167 : uint8_t public_key_info[SPDM_MAX_PUBLIC_KEY_INFO_LEN];
168 :
169 1 : key_pair_id = 1;
170 1 : associated_slot_id = 1;
171 1 : spdm_test_context = *state;
172 1 : spdm_context = spdm_test_context->spdm_context;
173 1 : spdm_test_context->case_id = 0x1;
174 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
175 : SPDM_VERSION_NUMBER_SHIFT_BIT;
176 :
177 1 : spdm_context->connection_info.connection_state =
178 : LIBSPDM_CONNECTION_STATE_NEGOTIATED;
179 1 : spdm_context->connection_info.capability.flags |=
180 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP |
181 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_SET_KEY_PAIR_INFO_CAP;
182 :
183 1 : spdm_context->connection_info.peer_key_pair_id[associated_slot_id] = key_pair_id;
184 1 : public_key_info_len = SPDM_MAX_PUBLIC_KEY_INFO_LEN;
185 :
186 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
187 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
188 : &asym_algo_capabilities, ¤t_asym_algo,
189 : &assoc_cert_slot_mask, &public_key_info_len,
190 : public_key_info);
191 :
192 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
193 1 : assert_int_equal(capabilities, SPDM_KEY_PAIR_CAP_GEN_KEY_CAP);
194 1 : assert_int_equal(key_usage_capabilities, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE);
195 1 : assert_int_equal(current_key_usage, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE);
196 1 : assert_int_equal(asym_algo_capabilities, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
197 1 : assert_int_equal(current_asym_algo, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
198 1 : }
199 :
200 : /**
201 : * Test 2: The collection of multiple sub-cases for invalid combination.
202 : * Expected Behavior: get a LIBSPDM_STATUS_INVALID_MSG_FIELD return code
203 : **/
204 1 : void req_get_key_pair_info_case2(void **state)
205 : {
206 : libspdm_return_t status;
207 : libspdm_test_context_t *spdm_test_context;
208 : libspdm_context_t *spdm_context;
209 :
210 : uint8_t key_pair_id;
211 : uint8_t associated_slot_id;
212 : uint8_t total_key_pairs;
213 : uint16_t capabilities;
214 : uint16_t key_usage_capabilities;
215 : uint16_t current_key_usage;
216 : uint32_t asym_algo_capabilities;
217 : uint32_t current_asym_algo;
218 : uint16_t public_key_info_len;
219 : uint8_t assoc_cert_slot_mask;
220 : uint8_t public_key_info[SPDM_MAX_PUBLIC_KEY_INFO_LEN];
221 :
222 1 : spdm_test_context = *state;
223 1 : spdm_context = spdm_test_context->spdm_context;
224 1 : spdm_context->connection_info.version = SPDM_MESSAGE_VERSION_13 <<
225 : SPDM_VERSION_NUMBER_SHIFT_BIT;
226 :
227 1 : spdm_context->connection_info.connection_state =
228 : LIBSPDM_CONNECTION_STATE_NEGOTIATED;
229 1 : spdm_context->connection_info.capability.flags |=
230 : SPDM_GET_CAPABILITIES_RESPONSE_FLAGS_GET_KEY_PAIR_INFO_CAP;
231 :
232 1 : spdm_test_context->case_id = 0x2;
233 1 : public_key_info_len = SPDM_MAX_PUBLIC_KEY_INFO_LEN;
234 :
235 : /* Sub Case 1: key_pair_id > total_key_pairs*/
236 1 : key_pair_id = 2;
237 1 : associated_slot_id = 1;
238 1 : spdm_context->connection_info.peer_key_pair_id[associated_slot_id] = key_pair_id;
239 :
240 1 : m_response_total_key_pairs = 1;
241 1 : m_response_key_pair_id = 2;
242 1 : m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
243 1 : m_response_key_usage_capabilities =SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
244 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
245 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
246 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
247 : /* association with slot 1*/
248 1 : m_response_assoc_cert_slot_mask = 0x2;
249 :
250 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
251 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
252 : &asym_algo_capabilities, ¤t_asym_algo,
253 : &assoc_cert_slot_mask, &public_key_info_len,
254 : public_key_info);
255 :
256 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
257 :
258 : /* Sub Case 2: responder and requester, the KeyPairID are not consistent.*/
259 1 : key_pair_id = 0xA0;
260 1 : associated_slot_id = 1;
261 1 : spdm_context->connection_info.peer_key_pair_id[associated_slot_id] = key_pair_id;
262 :
263 1 : m_response_total_key_pairs = 1;
264 1 : m_response_key_pair_id = 1;
265 1 : m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
266 1 : m_response_key_usage_capabilities =SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
267 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
268 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
269 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
270 : /* association with slot 1*/
271 1 : m_response_assoc_cert_slot_mask = 0x2;
272 :
273 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
274 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
275 : &asym_algo_capabilities, ¤t_asym_algo,
276 : &assoc_cert_slot_mask, &public_key_info_len,
277 : public_key_info);
278 :
279 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
280 :
281 : /* Sub Case 3: not set capabilities.*/
282 1 : key_pair_id = 1;
283 1 : associated_slot_id = 1;
284 1 : spdm_context->connection_info.peer_key_pair_id[associated_slot_id] = key_pair_id;
285 :
286 1 : m_response_total_key_pairs = 1;
287 1 : m_response_key_pair_id = 1;
288 1 : m_response_capabilities = 0;
289 1 : m_response_key_usage_capabilities =SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
290 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
291 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
292 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
293 : /* association with slot 1*/
294 1 : m_response_assoc_cert_slot_mask = 0x2;
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 : &assoc_cert_slot_mask, &public_key_info_len,
300 : public_key_info);
301 :
302 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
303 1 : assert_int_equal(capabilities, 0);
304 1 : assert_int_equal(key_usage_capabilities, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE);
305 1 : assert_int_equal(current_key_usage, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE);
306 1 : assert_int_equal(asym_algo_capabilities, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
307 1 : assert_int_equal(current_asym_algo, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
308 :
309 : /* Sub Case 4: This bit shall not be set if CertAssocCap is not set.
310 : * Set the ShareableCap bit and not set the CertAssocCap bit.*/
311 1 : key_pair_id = 1;
312 1 : associated_slot_id = 1;
313 1 : spdm_context->connection_info.peer_key_pair_id[associated_slot_id] = key_pair_id;
314 :
315 1 : m_response_total_key_pairs = 1;
316 1 : m_response_key_pair_id = 1;
317 :
318 1 : m_response_capabilities = 0;
319 1 : m_response_capabilities |= SPDM_KEY_PAIR_CAP_SHAREABLE_CAP;
320 1 : m_response_capabilities &= ~SPDM_KEY_PAIR_CAP_CERT_ASSOC_CAP;
321 :
322 1 : m_response_key_usage_capabilities =SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
323 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
324 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
325 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
326 : /* association with slot 1*/
327 1 : m_response_assoc_cert_slot_mask = 0x2;
328 :
329 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
330 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
331 : &asym_algo_capabilities, ¤t_asym_algo,
332 : &assoc_cert_slot_mask, &public_key_info_len,
333 : public_key_info);
334 :
335 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
336 :
337 : /* Sub Case 5: KeyUsageCapabilities at least one bit shall be set, not set KeyUsageCapabilities.*/
338 1 : m_response_total_key_pairs = 1;
339 1 : m_response_key_pair_id = 1;
340 1 : m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
341 1 : m_response_key_usage_capabilities =0;
342 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
343 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
344 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
345 : /* association with slot 1*/
346 1 : m_response_assoc_cert_slot_mask = 0x2;
347 :
348 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
349 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
350 : &asym_algo_capabilities, ¤t_asym_algo,
351 : &assoc_cert_slot_mask, &public_key_info_len,
352 : public_key_info);
353 :
354 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
355 :
356 : /* Sub Case 6: KeyUsageCapabilities Set multiple bits.*/
357 1 : m_response_total_key_pairs = 1;
358 1 : m_response_key_pair_id = 1;
359 1 : m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
360 1 : m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE |
361 : SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE |
362 : SPDM_KEY_USAGE_BIT_MASK_MEASUREMENT_USE |
363 : SPDM_KEY_USAGE_BIT_MASK_ENDPOINT_INFO_USE |
364 : SPDM_KEY_USAGE_BIT_MASK_STANDARDS_KEY_USE |
365 : SPDM_KEY_USAGE_BIT_MASK_VENDOR_KEY_USE;
366 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
367 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
368 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
369 : /* association with slot 1*/
370 1 : m_response_assoc_cert_slot_mask = 0x2;
371 :
372 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
373 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
374 : &asym_algo_capabilities, ¤t_asym_algo,
375 : &assoc_cert_slot_mask, &public_key_info_len,
376 : public_key_info);
377 :
378 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
379 1 : assert_int_equal(capabilities, SPDM_KEY_PAIR_CAP_GEN_KEY_CAP);
380 1 : assert_int_equal(key_usage_capabilities, SPDM_KEY_USAGE_BIT_MASK);
381 1 : assert_int_equal(current_key_usage, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE);
382 1 : assert_int_equal(asym_algo_capabilities, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
383 1 : assert_int_equal(current_asym_algo, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
384 :
385 : /* Sub Case 7: not set CurrentKeyUsage*/
386 1 : m_response_total_key_pairs = 1;
387 1 : m_response_key_pair_id = 1;
388 1 : m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
389 1 : m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
390 1 : m_response_current_key_usage = 0;
391 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
392 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
393 : /* association with slot 1*/
394 1 : m_response_assoc_cert_slot_mask = 0x2;
395 :
396 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
397 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
398 : &asym_algo_capabilities, ¤t_asym_algo,
399 : &assoc_cert_slot_mask, &public_key_info_len,
400 : public_key_info);
401 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
402 1 : assert_int_equal(capabilities, SPDM_KEY_PAIR_CAP_GEN_KEY_CAP);
403 1 : assert_int_equal(key_usage_capabilities, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE);
404 1 : assert_int_equal(current_key_usage, 0);
405 1 : assert_int_equal(asym_algo_capabilities, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
406 1 : assert_int_equal(current_asym_algo, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
407 :
408 : /* Sub Case 8: CurrentKeyUsage Set multiple bits.*/
409 1 : m_response_total_key_pairs = 1;
410 1 : m_response_key_pair_id = 1;
411 1 : m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
412 1 : m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
413 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE |
414 : SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE |
415 : SPDM_KEY_USAGE_BIT_MASK_MEASUREMENT_USE |
416 : SPDM_KEY_USAGE_BIT_MASK_ENDPOINT_INFO_USE |
417 : SPDM_KEY_USAGE_BIT_MASK_STANDARDS_KEY_USE |
418 : SPDM_KEY_USAGE_BIT_MASK_VENDOR_KEY_USE;
419 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
420 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
421 : /* association with slot 1*/
422 1 : m_response_assoc_cert_slot_mask = 0x2;
423 :
424 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
425 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
426 : &asym_algo_capabilities, ¤t_asym_algo,
427 : &assoc_cert_slot_mask, &public_key_info_len,
428 : public_key_info);
429 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
430 :
431 : /* Sub Case 9: CurrentKeyUsage and KeyUsageCapabilities, Set multiple bits.*/
432 1 : m_response_total_key_pairs = 1;
433 1 : m_response_key_pair_id = 1;
434 1 : m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
435 1 : m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE |
436 : SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE;
437 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE |
438 : SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_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 : &assoc_cert_slot_mask, &public_key_info_len,
448 : public_key_info);
449 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
450 1 : assert_int_equal(capabilities, SPDM_KEY_PAIR_CAP_GEN_KEY_CAP);
451 1 : assert_int_equal(key_usage_capabilities, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE |
452 : SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE);
453 1 : assert_int_equal(current_key_usage, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE |
454 : SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE);
455 1 : assert_int_equal(asym_algo_capabilities, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
456 1 : assert_int_equal(current_asym_algo, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
457 :
458 : /* Sub Case 10: CurrentKeyUsage and KeyUsageCapabilities are not consistent.*/
459 1 : m_response_total_key_pairs = 1;
460 1 : m_response_key_pair_id = 1;
461 1 : m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
462 1 : m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_CHALLENGE_USE;
463 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
464 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
465 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
466 : /* association with slot 1*/
467 1 : m_response_assoc_cert_slot_mask = 0x2;
468 :
469 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
470 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
471 : &asym_algo_capabilities, ¤t_asym_algo,
472 : &assoc_cert_slot_mask, &public_key_info_len,
473 : public_key_info);
474 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
475 :
476 : /* Sub Case 11: AsymAlgoCapabilities, at least one bit shall be set.not set AsymAlgoCapabilities*/
477 1 : m_response_total_key_pairs = 1;
478 1 : m_response_key_pair_id = 1;
479 1 : m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
480 1 : m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
481 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
482 1 : m_response_asym_algo_capabilities = 0;
483 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
484 : /* association with slot 1*/
485 1 : m_response_assoc_cert_slot_mask = 0x2;
486 :
487 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
488 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
489 : &asym_algo_capabilities, ¤t_asym_algo,
490 : &assoc_cert_slot_mask, &public_key_info_len,
491 : public_key_info);
492 :
493 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
494 :
495 : /* Sub Case 12: AsymAlgoCapabilities Set multiple bits.*/
496 1 : m_response_total_key_pairs = 1;
497 1 : m_response_key_pair_id = 1;
498 1 : m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
499 1 : m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
500 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
501 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048 |
502 : SPDM_KEY_PAIR_ASYM_ALGO_CAP_ED448;
503 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
504 : /* association with slot 1*/
505 1 : m_response_assoc_cert_slot_mask = 0x2;
506 :
507 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
508 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
509 : &asym_algo_capabilities, ¤t_asym_algo,
510 : &assoc_cert_slot_mask, &public_key_info_len,
511 : public_key_info);
512 :
513 1 : assert_int_equal(status, LIBSPDM_STATUS_SUCCESS);
514 1 : assert_int_equal(capabilities, SPDM_KEY_PAIR_CAP_GEN_KEY_CAP);
515 1 : assert_int_equal(key_usage_capabilities, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE);
516 1 : assert_int_equal(current_key_usage, SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE);
517 1 : assert_int_equal(asym_algo_capabilities, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048 |
518 : SPDM_KEY_PAIR_ASYM_ALGO_CAP_ED448);
519 1 : assert_int_equal(current_asym_algo, SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048);
520 :
521 : /* Sub Case 13: CurrentAsymAlgo Set bits more than one bit.*/
522 1 : m_response_total_key_pairs = 1;
523 1 : m_response_key_pair_id = 1;
524 1 : m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
525 1 : m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
526 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
527 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
528 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048 |
529 : SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA3072;
530 : /* association with slot 1*/
531 1 : m_response_assoc_cert_slot_mask = 0x2;
532 :
533 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
534 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
535 : &asym_algo_capabilities, ¤t_asym_algo,
536 : &assoc_cert_slot_mask, &public_key_info_len,
537 : public_key_info);
538 :
539 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
540 :
541 : /* Sub Case 14: AsymAlgoCapabilities and AsymAlgoCapabilities are not consistent.*/
542 1 : m_response_total_key_pairs = 1;
543 1 : m_response_key_pair_id = 1;
544 1 : m_response_capabilities = SPDM_KEY_PAIR_CAP_GEN_KEY_CAP;
545 1 : m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
546 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
547 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA3072;
548 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
549 : /* association with slot 1*/
550 1 : m_response_assoc_cert_slot_mask = 0x2;
551 :
552 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
553 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
554 : &asym_algo_capabilities, ¤t_asym_algo,
555 : &assoc_cert_slot_mask, &public_key_info_len,
556 : public_key_info);
557 :
558 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
559 :
560 : /* Sub Case 15: AssocCertSlotMask set more than one bit, but ShareableCap is not set.*/
561 1 : m_response_total_key_pairs = 1;
562 1 : m_response_key_pair_id = 1;
563 1 : m_response_capabilities = 0;
564 1 : m_response_key_usage_capabilities = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
565 1 : m_response_current_key_usage = SPDM_KEY_USAGE_BIT_MASK_KEY_EX_USE;
566 1 : m_response_asym_algo_capabilities = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
567 1 : m_response_current_asym_algo = SPDM_KEY_PAIR_ASYM_ALGO_CAP_RSA2048;
568 : /* association with slot 1*/
569 1 : m_response_assoc_cert_slot_mask = 0xFF;
570 :
571 1 : status = libspdm_get_key_pair_info(spdm_context, NULL, key_pair_id, &total_key_pairs,
572 : &capabilities, &key_usage_capabilities, ¤t_key_usage,
573 : &asym_algo_capabilities, ¤t_asym_algo,
574 : &assoc_cert_slot_mask, &public_key_info_len,
575 : public_key_info);
576 :
577 1 : assert_int_equal(status, LIBSPDM_STATUS_INVALID_MSG_FIELD);
578 1 : }
579 :
580 1 : int libspdm_req_get_key_pair_info_test(void)
581 : {
582 1 : const struct CMUnitTest test_cases[] = {
583 : /* Successful response to get key pair info, key_pair_id is 1*/
584 : cmocka_unit_test(req_get_key_pair_info_case1),
585 : /* The collection of multiple sub-cases for invalid combination.*/
586 : cmocka_unit_test(req_get_key_pair_info_case2),
587 : };
588 :
589 1 : libspdm_test_context_t test_context = {
590 : LIBSPDM_TEST_CONTEXT_VERSION,
591 : true,
592 : send_message,
593 : receive_message,
594 : };
595 :
596 1 : libspdm_setup_test_context(&test_context);
597 :
598 1 : return cmocka_run_group_tests(test_cases,
599 : libspdm_unit_test_group_setup,
600 : libspdm_unit_test_group_teardown);
601 : }
602 :
603 : #endif /*LIBSPDM_ENABLE_CAPABILITY_GET_KEY_PAIR_INFO_CAP*/
|