Branch data 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 "internal/libspdm_crypt_lib.h"
8 : :
9 : 160 : uint32_t libspdm_get_cert_chain_length(
10 : : uint8_t spdm_version, const spdm_cert_chain_t *cert_chain_header)
11 : : {
12 [ + + ]: 160 : if (spdm_version < SPDM_MESSAGE_VERSION_14) {
13 : 152 : return cert_chain_header->length & 0xFFFF;
14 : : }
15 : 8 : return cert_chain_header->length;
16 : : }
17 : :
18 : : #if LIBSPDM_CERT_PARSE_SUPPORT
19 : :
20 : : /**pathLenConstraint is optional.
21 : : * In https://www.pkisolutions.com/basic-constraints-certificate-extension/:
22 : : * pathLenConstraint: How many CAs are allowed in the chain below current CA certificate.
23 : : * This setting has no meaning for end entity certificates.
24 : : **/
25 : :
26 : : /**
27 : : * leaf cert spdm extension len
28 : : * len > 2 * (spdm id-DMTF-spdm size + 2)
29 : : **/
30 : :
31 : : #ifndef LIBSPDM_MAX_EXTENSION_LEN
32 : : #define LIBSPDM_MAX_EXTENSION_LEN 30
33 : : #endif
34 : :
35 : : #ifndef LIBSPDM_MAX_NAME_SIZE
36 : : #define LIBSPDM_MAX_NAME_SIZE 100
37 : : #endif
38 : :
39 : : /*max public key encryption algo oid len*/
40 : : #ifndef LIBSPDM_MAX_ENCRYPTION_ALGO_OID_LEN
41 : : #define LIBSPDM_MAX_ENCRYPTION_ALGO_OID_LEN 10
42 : : #endif
43 : :
44 : : /* Maximum size of basicConstraints. This includes space for both cA and pathLen. */
45 : : #ifndef LIBSPDM_MAX_BASIC_CONSTRAINTS_CA_LEN
46 : : #define LIBSPDM_MAX_BASIC_CONSTRAINTS_CA_LEN 10
47 : : #endif
48 : :
49 : : /**
50 : : * 0x02 is integer;
51 : : * 0x82 indicates that the length is expressed in two bytes;
52 : : * 0x01 and 0x01 are rsa key len;
53 : : **/
54 : : #if (LIBSPDM_RSA_SSA_2048_SUPPORT) || (LIBSPDM_RSA_PSS_2048_SUPPORT)
55 : : #define KEY_ENCRY_ALGO_RSA2048_FLAG {0x02, 0x82, 0x01, 0x01}
56 : : /* the other case is ASN1 code different when integer is 1 on highest position*/
57 : : #define KEY_ENCRY_ALGO_RSA2048_FLAG_OTHER {0x02, 0x82, 0x01, 0x00}
58 : : #endif
59 : : #if (LIBSPDM_RSA_SSA_3072_SUPPORT) || (LIBSPDM_RSA_PSS_3072_SUPPORT)
60 : : #define KEY_ENCRY_ALGO_RSA3072_FLAG {0x02, 0x82, 0x01, 0x81}
61 : : /* the other case is ASN1 code different when integer is 1 on highest position*/
62 : : #define KEY_ENCRY_ALGO_RSA3072_FLAG_OTHER {0x02, 0x82, 0x01, 0x80}
63 : : #endif
64 : : #if (LIBSPDM_RSA_SSA_4096_SUPPORT) || (LIBSPDM_RSA_PSS_4096_SUPPORT)
65 : : #define KEY_ENCRY_ALGO_RSA4096_FLAG {0x02, 0x82, 0x02, 0x01}
66 : : /* the other case is ASN1 code different when integer is 1 on highest position*/
67 : : #define KEY_ENCRY_ALGO_RSA4096_FLAG_OTHER {0x02, 0x82, 0x02, 0x00}
68 : : #endif
69 : :
70 : : /**
71 : : * https://oidref.com/1.2.840.10045.3.1.7
72 : : * ECC256 curve OID: 1.2.840.10045.3.1.7
73 : : * https://oidref.com/1.3.132.0.34
74 : : * ECC384 curve OID: 1.3.132.0.34
75 : : * https://oidref.com/1.3.132.0.35
76 : : * ECC521 curve OID: 1.3.132.0.35
77 : : **/
78 : : #if LIBSPDM_ECDSA_P256_SUPPORT
79 : : #define KEY_ENCRY_ALGO_ECC256_OID {0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07}
80 : : #endif
81 : : #if LIBSPDM_ECDSA_P384_SUPPORT
82 : : #define KEY_ENCRY_ALGO_ECC384_OID {0x2B, 0x81, 0x04, 0x00, 0x22}
83 : : #endif
84 : : #if LIBSPDM_ECDSA_P521_SUPPORT
85 : : #define KEY_ENCRY_ALGO_ECC521_OID {0x2B, 0x81, 0x04, 0x00, 0x23}
86 : : #endif
87 : :
88 : : /**
89 : : * EDxxx OID: https://datatracker.ietf.org/doc/html/rfc8420
90 : : * ED448 OID: 1.3.101.113
91 : : * ED25519 OID: 1.3.101.112
92 : : **/
93 : : #if LIBSPDM_EDDSA_ED25519_SUPPORT
94 : : #define ENCRY_ALGO_ED25519_OID {0x2B, 0x65, 0x70}
95 : : #endif
96 : : #if LIBSPDM_EDDSA_ED448_SUPPORT
97 : : #define ENCRY_ALGO_ED448_OID {0x2B, 0x65, 0x71}
98 : : #endif
99 : :
100 : : /**
101 : : * MLDSA OID: RFC9881
102 : : * MLDSA44 OID: 2.16.840.1.101.3.4.3.17
103 : : * MLDSA65 OID: 2.16.840.1.101.3.4.3.18
104 : : * MLDSA87 OID: 2.16.840.1.101.3.4.3.19
105 : : **/
106 : : #if LIBSPDM_ML_DSA_44_SUPPORT
107 : : #define ALGO_MLDSA44_OID {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x11}
108 : : #endif
109 : : #if LIBSPDM_ML_DSA_65_SUPPORT
110 : : #define ALGO_MLDSA65_OID {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x12}
111 : : #endif
112 : : #if LIBSPDM_ML_DSA_87_SUPPORT
113 : : #define ALGO_MLDSA87_OID {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x13}
114 : : #endif
115 : :
116 : : /**
117 : : * SLHDSA OID: RFC9909
118 : : * SLHDSA_SHA2_128S OID: 2.16.840.1.101.3.4.3.20
119 : : * SLHDSA_SHA2_128F OID: 2.16.840.1.101.3.4.3.21
120 : : * SLHDSA_SHA2_192S OID: 2.16.840.1.101.3.4.3.22
121 : : * SLHDSA_SHA2_192F OID: 2.16.840.1.101.3.4.3.23
122 : : * SLHDSA_SHA2_256S OID: 2.16.840.1.101.3.4.3.24
123 : : * SLHDSA_SHA2_256F OID: 2.16.840.1.101.3.4.3.25
124 : : * SLHDSA_SHAKE_128S OID: 2.16.840.1.101.3.4.3.26
125 : : * SLHDSA_SHAKE_128F OID: 2.16.840.1.101.3.4.3.27
126 : : * SLHDSA_SHAKE_192S OID: 2.16.840.1.101.3.4.3.28
127 : : * SLHDSA_SHAKE_192F OID: 2.16.840.1.101.3.4.3.29
128 : : * SLHDSA_SHAKE_256S OID: 2.16.840.1.101.3.4.3.30
129 : : * SLHDSA_SHAKE_256F OID: 2.16.840.1.101.3.4.3.31
130 : : **/
131 : : #if LIBSPDM_SLH_DSA_SHA2_128S_SUPPORT
132 : : #define ALGO_SLHDSA_SHA2_128S_OID {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x14}
133 : : #endif
134 : : #if LIBSPDM_SLH_DSA_SHA2_128F_SUPPORT
135 : : #define ALGO_SLHDSA_SHA2_128F_OID {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x15}
136 : : #endif
137 : : #if LIBSPDM_SLH_DSA_SHA2_192S_SUPPORT
138 : : #define ALGO_SLHDSA_SHA2_192S_OID {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x16}
139 : : #endif
140 : : #if LIBSPDM_SLH_DSA_SHA2_192F_SUPPORT
141 : : #define ALGO_SLHDSA_SHA2_192F_OID {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x17}
142 : : #endif
143 : : #if LIBSPDM_SLH_DSA_SHA2_256S_SUPPORT
144 : : #define ALGO_SLHDSA_SHA2_256S_OID {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x18}
145 : : #endif
146 : : #if LIBSPDM_SLH_DSA_SHA2_256F_SUPPORT
147 : : #define ALGO_SLHDSA_SHA2_256F_OID {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x19}
148 : : #endif
149 : : #if LIBSPDM_SLH_DSA_SHAKE_128S_SUPPORT
150 : : #define ALGO_SLHDSA_SHAKE_128S_OID {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x1A}
151 : : #endif
152 : : #if LIBSPDM_SLH_DSA_SHAKE_128F_SUPPORT
153 : : #define ALGO_SLHDSA_SHAKE_128F_OID {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x1B}
154 : : #endif
155 : : #if LIBSPDM_SLH_DSA_SHAKE_192S_SUPPORT
156 : : #define ALGO_SLHDSA_SHAKE_192S_OID {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x1C}
157 : : #endif
158 : : #if LIBSPDM_SLH_DSA_SHAKE_192F_SUPPORT
159 : : #define ALGO_SLHDSA_SHAKE_192F_OID {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x1D}
160 : : #endif
161 : : #if LIBSPDM_SLH_DSA_SHAKE_256S_SUPPORT
162 : : #define ALGO_SLHDSA_SHAKE_256S_OID {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x1E}
163 : : #endif
164 : : #if LIBSPDM_SLH_DSA_SHAKE_256F_SUPPORT
165 : : #define ALGO_SLHDSA_SHAKE_256F_OID {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x1F}
166 : : #endif
167 : :
168 : : /* Leaf certificate basic constraints with cA field set to false. According to RFC5280, false is the
169 : : * default value, and according to DER encoding a sequence item with a default value must not be
170 : : * encoded. */
171 : : #define BASIC_CONSTRAINTS_CA_FALSE {0x30, 0x00}
172 : :
173 : : /* Leaf certificate basic constraints with cA field set to true. */
174 : : #define BASIC_CONSTRAINTS_CA_TRUE {0x30, 0x03, 0x01, 0x01, 0xFF}
175 : :
176 : : /**
177 : : * Retrieve the asymmetric public key from one DER-encoded X509 certificate.
178 : : *
179 : : * @param cert Pointer to the DER-encoded X509 certificate.
180 : : * @param cert_size Size of the X509 certificate in bytes.
181 : : * @param context Pointer to newly generated asymmetric context which contain the retrieved public
182 : : * key component. Use libspdm_asym_free() function to free the resource.
183 : : *
184 : : * @retval true public key was retrieved successfully.
185 : : * @retval false Fail to retrieve public key from X509 certificate.
186 : : **/
187 : : typedef bool (*libspdm_asym_get_public_key_from_x509_func)(const uint8_t *cert,
188 : : size_t cert_size,
189 : : void **context);
190 : :
191 : : /**
192 : : * Return asymmetric GET_PUBLIC_KEY_FROM_X509 function, based upon the negotiated asymmetric algorithm.
193 : : *
194 : : * @param base_asym_algo SPDM base_asym_algo
195 : : *
196 : : * @return asymmetric GET_PUBLIC_KEY_FROM_X509 function
197 : : **/
198 : : static libspdm_asym_get_public_key_from_x509_func
199 : 2520 : libspdm_get_asym_get_public_key_from_x509(uint32_t base_asym_algo)
200 : : {
201 [ + + + - : 2520 : switch (base_asym_algo) {
- ]
202 : 254 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_2048:
203 : : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_3072:
204 : : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_4096:
205 : : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_2048:
206 : : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_3072:
207 : : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_4096:
208 : : #if (LIBSPDM_RSA_SSA_SUPPORT) || (LIBSPDM_RSA_PSS_SUPPORT)
209 : : #if !LIBSPDM_RSA_SSA_2048_SUPPORT
210 : : LIBSPDM_ASSERT(base_asym_algo!= SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_2048);
211 : : #endif
212 : : #if !LIBSPDM_RSA_SSA_3072_SUPPORT
213 : : LIBSPDM_ASSERT(base_asym_algo!= SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_3072);
214 : : #endif
215 : : #if !LIBSPDM_RSA_SSA_4096_SUPPORT
216 : : LIBSPDM_ASSERT(base_asym_algo!= SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_4096);
217 : : #endif
218 : : #if !LIBSPDM_RSA_PSS_2048_SUPPORT
219 : : LIBSPDM_ASSERT(base_asym_algo!= SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_2048);
220 : : #endif
221 : : #if !LIBSPDM_RSA_PSS_3072_SUPPORT
222 : : LIBSPDM_ASSERT(base_asym_algo!= SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_3072);
223 : : #endif
224 : : #if !LIBSPDM_RSA_PSS_4096_SUPPORT
225 : : LIBSPDM_ASSERT(base_asym_algo!= SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_4096);
226 : : #endif
227 : 254 : return libspdm_rsa_get_public_key_from_x509;
228 : : #else
229 : : LIBSPDM_ASSERT(false);
230 : : break;
231 : : #endif
232 : 2260 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P256:
233 : : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P384:
234 : : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P521:
235 : : #if LIBSPDM_ECDSA_SUPPORT
236 : : #if !LIBSPDM_ECDSA_P256_SUPPORT
237 : : LIBSPDM_ASSERT(base_asym_algo!= SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P256);
238 : : #endif
239 : : #if !LIBSPDM_ECDSA_P384_SUPPORT
240 : : LIBSPDM_ASSERT(base_asym_algo!= SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P384);
241 : : #endif
242 : : #if !LIBSPDM_ECDSA_P521_SUPPORT
243 : : LIBSPDM_ASSERT(base_asym_algo!= SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P521);
244 : : #endif
245 : 2260 : return libspdm_ec_get_public_key_from_x509;
246 : : #else
247 : : LIBSPDM_ASSERT(false);
248 : : break;
249 : : #endif
250 : 6 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_EDDSA_ED25519:
251 : : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_EDDSA_ED448:
252 : : #if (LIBSPDM_EDDSA_ED25519_SUPPORT) || (LIBSPDM_EDDSA_ED448_SUPPORT)
253 : : #if !LIBSPDM_EDDSA_ED25519_SUPPORT
254 : : LIBSPDM_ASSERT(base_asym_algo!= SPDM_ALGORITHMS_BASE_ASYM_ALGO_EDDSA_ED25519);
255 : : #endif
256 : : #if !LIBSPDM_EDDSA_ED448_SUPPORT
257 : : LIBSPDM_ASSERT(base_asym_algo!= SPDM_ALGORITHMS_BASE_ASYM_ALGO_EDDSA_ED448);
258 : : #endif
259 : 6 : return libspdm_ecd_get_public_key_from_x509;
260 : : #else
261 : 0 : LIBSPDM_ASSERT(false);
262 : 0 : break;
263 : : #endif
264 : 0 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_SM2_ECC_SM2_P256:
265 : : #if LIBSPDM_SM2_DSA_SUPPORT
266 : : return libspdm_sm2_get_public_key_from_x509;
267 : : #else
268 : 0 : LIBSPDM_ASSERT(false);
269 : 0 : break;
270 : : #endif
271 : 0 : default:
272 : 0 : LIBSPDM_ASSERT(false);
273 : 0 : break;
274 : : }
275 : :
276 : 0 : return NULL;
277 : : }
278 : :
279 : 2500 : bool libspdm_asym_get_public_key_from_x509(uint32_t base_asym_algo,
280 : : const uint8_t *cert,
281 : : size_t cert_size,
282 : : void **context)
283 : : {
284 : : libspdm_asym_get_public_key_from_x509_func get_public_key_from_x509_function;
285 : 2500 : get_public_key_from_x509_function = libspdm_get_asym_get_public_key_from_x509(base_asym_algo);
286 [ - + ]: 2500 : if (get_public_key_from_x509_function == NULL) {
287 : 0 : return false;
288 : : }
289 : 2500 : return get_public_key_from_x509_function(cert, cert_size, context);
290 : : }
291 : :
292 : : /**
293 : : * Return requester asymmetric GET_PUBLIC_KEY_FROM_X509 function, based upon the negotiated requester asymmetric algorithm.
294 : : *
295 : : * @param req_base_asym_alg SPDM req_base_asym_alg
296 : : *
297 : : * @return requester asymmetric GET_PUBLIC_KEY_FROM_X509 function
298 : : **/
299 : : static libspdm_asym_get_public_key_from_x509_func
300 : 20 : libspdm_get_req_asym_get_public_key_from_x509(uint16_t req_base_asym_alg)
301 : : {
302 : 20 : return libspdm_get_asym_get_public_key_from_x509(req_base_asym_alg);
303 : : }
304 : :
305 : 20 : bool libspdm_req_asym_get_public_key_from_x509(uint16_t req_base_asym_alg,
306 : : const uint8_t *cert,
307 : : size_t cert_size,
308 : : void **context)
309 : : {
310 : : libspdm_asym_get_public_key_from_x509_func get_public_key_from_x509_function;
311 : : get_public_key_from_x509_function =
312 : 20 : libspdm_get_req_asym_get_public_key_from_x509(req_base_asym_alg);
313 [ - + ]: 20 : if (get_public_key_from_x509_function == NULL) {
314 : 0 : return false;
315 : : }
316 : 20 : return get_public_key_from_x509_function(cert, cert_size, context);
317 : : }
318 : :
319 : : /**
320 : : * Check the X509 DateTime is within a valid range.
321 : : *
322 : : * @param from notBefore Pointer to date_time object.
323 : : * @param from_size notBefore date_time object size.
324 : : * @param to notAfter Pointer to date_time object.
325 : : * @param to_size notAfter date_time object size.
326 : : *
327 : : * @retval true verification pass.
328 : : * @retval false verification fail.
329 : : **/
330 : 2135 : static bool libspdm_internal_x509_date_time_check(const uint8_t *from,
331 : : size_t from_size,
332 : : const uint8_t *to,
333 : : size_t to_size)
334 : : {
335 : : int32_t ret;
336 : : bool status;
337 : : uint8_t f0[64];
338 : : uint8_t t0[64];
339 : : size_t f0_size;
340 : : size_t t0_size;
341 : :
342 : 2135 : f0_size = 64;
343 : 2135 : t0_size = 64;
344 : :
345 : 2135 : status = libspdm_x509_set_date_time("19700101000000Z", f0, &f0_size);
346 [ - + ]: 2135 : if (!status) {
347 : 0 : return false;
348 : : }
349 : :
350 : 2135 : status = libspdm_x509_set_date_time("99991231235959Z", t0, &t0_size);
351 [ - + ]: 2135 : if (!status) {
352 : 0 : return false;
353 : : }
354 : :
355 : : /* from >= f0*/
356 : 2135 : ret = libspdm_x509_compare_date_time(from, f0);
357 [ - + ]: 2135 : if (ret < 0) {
358 : 0 : return false;
359 : : }
360 : :
361 : : /* to <= t0*/
362 : 2135 : ret = libspdm_x509_compare_date_time(t0, to);
363 [ - + ]: 2135 : if (ret < 0) {
364 : 0 : return false;
365 : : }
366 : :
367 : 2135 : return true;
368 : : }
369 : :
370 : : /**
371 : : * This function returns the SPDM public key encryption algorithm OID len.
372 : : *
373 : : * @param[in] base_asym_algo SPDM base_asym_algo
374 : : * @param[in] pqc_asym_algo SPDM pqc_asym_algo
375 : : *
376 : : * @return SPDM public key encryption algorithms OID len.
377 : : **/
378 : 4868 : static uint32_t libspdm_get_public_key_algo_OID_len(
379 : : uint32_t base_asym_algo, uint32_t pqc_asym_algo)
380 : : {
381 [ + + ]: 4868 : if (base_asym_algo != 0) {
382 [ + + + + : 4506 : switch (base_asym_algo) {
+ + + + +
+ + - ]
383 : 342 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_2048:
384 : : #if LIBSPDM_RSA_SSA_2048_SUPPORT
385 : 342 : return 4;
386 : : #else
387 : : return 0;
388 : : #endif
389 : 30 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_2048:
390 : : #if LIBSPDM_RSA_PSS_2048_SUPPORT
391 : 30 : return 4;
392 : : #else
393 : : return 0;
394 : : #endif
395 : 42 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_3072:
396 : : #if LIBSPDM_RSA_SSA_3072_SUPPORT
397 : 42 : return 4;
398 : : #else
399 : : return 0;
400 : : #endif
401 : 30 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_3072:
402 : : #if LIBSPDM_RSA_PSS_3072_SUPPORT
403 : 30 : return 4;
404 : : #else
405 : : return 0;
406 : : #endif
407 : 38 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_4096:
408 : : #if LIBSPDM_RSA_SSA_4096_SUPPORT
409 : 38 : return 4;
410 : : #else
411 : : return 0;
412 : : #endif
413 : 30 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_4096:
414 : : #if LIBSPDM_RSA_PSS_4096_SUPPORT
415 : 30 : return 4;
416 : : #else
417 : : return 0;
418 : : #endif
419 : 3878 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P256:
420 : : #if LIBSPDM_ECDSA_P256_SUPPORT
421 : 3878 : return 8;
422 : : #else
423 : : return 0;
424 : : #endif
425 : 34 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P384:
426 : : #if LIBSPDM_ECDSA_P384_SUPPORT
427 : 34 : return 5;
428 : : #else
429 : : return 0;
430 : : #endif
431 : 34 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P521:
432 : : #if LIBSPDM_ECDSA_P521_SUPPORT
433 : 34 : return 5;
434 : : #else
435 : : return 0;
436 : : #endif
437 : 24 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_EDDSA_ED25519:
438 : : #if LIBSPDM_EDDSA_ED25519_SUPPORT
439 : 18 : return 3;
440 : : #else
441 : 6 : return 0;
442 : : #endif
443 : 24 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_EDDSA_ED448:
444 : : #if LIBSPDM_EDDSA_ED448_SUPPORT
445 : 18 : return 3;
446 : : #else
447 : 6 : return 0;
448 : : #endif
449 : 0 : default:
450 : 0 : LIBSPDM_ASSERT(false);
451 : 0 : return 0;
452 : : }
453 : : }
454 [ + - ]: 362 : if (pqc_asym_algo != 0) {
455 [ + + + + : 362 : switch (pqc_asym_algo) {
+ + + + +
+ + + + +
+ - ]
456 : 24 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_ML_DSA_44:
457 : : #if LIBSPDM_ML_DSA_44_SUPPORT
458 : 18 : return 9;
459 : : #else
460 : 6 : return 0;
461 : : #endif
462 : 24 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_ML_DSA_65:
463 : : #if LIBSPDM_ML_DSA_65_SUPPORT
464 : 18 : return 9;
465 : : #else
466 : 6 : return 0;
467 : : #endif
468 : 24 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_ML_DSA_87:
469 : : #if LIBSPDM_ML_DSA_87_SUPPORT
470 : 18 : return 9;
471 : : #else
472 : 6 : return 0;
473 : : #endif
474 : 24 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_128S:
475 : : #if LIBSPDM_SLH_DSA_SHA2_128S_SUPPORT
476 : 18 : return 9;
477 : : #else
478 : 6 : return 0;
479 : : #endif
480 : 24 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_128S:
481 : : #if LIBSPDM_SLH_DSA_SHAKE_128S_SUPPORT
482 : 18 : return 9;
483 : : #else
484 : 6 : return 0;
485 : : #endif
486 : 24 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_128F:
487 : : #if LIBSPDM_SLH_DSA_SHA2_128F_SUPPORT
488 : 18 : return 9;
489 : : #else
490 : 6 : return 0;
491 : : #endif
492 : 24 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_128F:
493 : : #if LIBSPDM_SLH_DSA_SHAKE_128F_SUPPORT
494 : 18 : return 9;
495 : : #else
496 : 6 : return 0;
497 : : #endif
498 : 24 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_192S:
499 : : #if LIBSPDM_SLH_DSA_SHA2_192S_SUPPORT
500 : 18 : return 9;
501 : : #else
502 : 6 : return 0;
503 : : #endif
504 : 24 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_192S:
505 : : #if LIBSPDM_SLH_DSA_SHAKE_192S_SUPPORT
506 : 18 : return 9;
507 : : #else
508 : 6 : return 0;
509 : : #endif
510 : 24 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_192F:
511 : : #if LIBSPDM_SLH_DSA_SHA2_192F_SUPPORT
512 : 18 : return 9;
513 : : #else
514 : 6 : return 0;
515 : : #endif
516 : 24 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_192F:
517 : : #if LIBSPDM_SLH_DSA_SHAKE_192F_SUPPORT
518 : 18 : return 9;
519 : : #else
520 : 6 : return 0;
521 : : #endif
522 : 24 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_256S:
523 : : #if LIBSPDM_SLH_DSA_SHA2_256S_SUPPORT
524 : 18 : return 9;
525 : : #else
526 : 6 : return 0;
527 : : #endif
528 : 24 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_256S:
529 : : #if LIBSPDM_SLH_DSA_SHAKE_256S_SUPPORT
530 : 18 : return 9;
531 : : #else
532 : 6 : return 0;
533 : : #endif
534 : 26 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_256F:
535 : : #if LIBSPDM_SLH_DSA_SHA2_256F_SUPPORT
536 : 20 : return 9;
537 : : #else
538 : 6 : return 0;
539 : : #endif
540 : 24 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_256F:
541 : : #if LIBSPDM_SLH_DSA_SHAKE_256F_SUPPORT
542 : 18 : return 9;
543 : : #else
544 : 6 : return 0;
545 : : #endif
546 : 0 : default:
547 : 0 : LIBSPDM_ASSERT(false);
548 : 0 : return 0;
549 : : }
550 : : }
551 : 0 : LIBSPDM_ASSERT(false);
552 : 0 : return 0;
553 : : }
554 : :
555 : : /**
556 : : * This function get the SPDM public key encryption algorithm OID.
557 : : *
558 : : * @param[in] base_asym_algo SPDM base_asym_algo
559 : : * @param[in] pqc_asym_algo SPDM pqc_asym_algo
560 : : * @param[in,out] oid SPDM public key encryption algorithm OID
561 : : * @param[in,out] oid_other Other SPDM public key encryption algorithm OID
562 : : * because of ASN1 code for integer
563 : : *
564 : : * @retval true get OID successful.
565 : : * @retval false get OID fail.
566 : : **/
567 : 2383 : static bool libspdm_get_public_key_algo_OID(
568 : : uint32_t base_asym_algo, uint32_t pqc_asym_algo, uint8_t *oid,
569 : : uint8_t *oid_other)
570 : : {
571 : : uint32_t oid_len;
572 : 2383 : oid_len = libspdm_get_public_key_algo_OID_len(base_asym_algo, pqc_asym_algo);
573 [ - + ]: 2383 : if (oid_len == 0) {
574 : 0 : return false;
575 : : }
576 : :
577 [ + + ]: 2383 : if (base_asym_algo != 0) {
578 [ + + + + : 2247 : switch (base_asym_algo) {
+ + - + +
- ]
579 : 186 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_2048:
580 : : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_2048: {
581 : : #if (LIBSPDM_RSA_SSA_2048_SUPPORT) || (LIBSPDM_RSA_PSS_2048_SUPPORT)
582 : 186 : uint8_t encry_algo_oid_rsa2048[] = KEY_ENCRY_ALGO_RSA2048_FLAG;
583 : 186 : uint8_t encry_algo_oid_rsa2048_other[] = KEY_ENCRY_ALGO_RSA2048_FLAG_OTHER;
584 : 186 : libspdm_copy_mem(oid, oid_len, encry_algo_oid_rsa2048, oid_len);
585 : 186 : libspdm_copy_mem(oid_other, oid_len, encry_algo_oid_rsa2048_other, oid_len);
586 : 186 : return true;
587 : : #else
588 : : return false;
589 : : #endif
590 : : }
591 : 36 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_3072:
592 : : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_3072: {
593 : : #if (LIBSPDM_RSA_SSA_3072_SUPPORT) || (LIBSPDM_RSA_PSS_3072_SUPPORT)
594 : 36 : uint8_t encry_algo_oid_rsa3072[] = KEY_ENCRY_ALGO_RSA3072_FLAG;
595 : 36 : uint8_t encry_algo_oid_rsa3072_other[] = KEY_ENCRY_ALGO_RSA3072_FLAG_OTHER;
596 : 36 : libspdm_copy_mem(oid, oid_len, encry_algo_oid_rsa3072, oid_len);
597 : 36 : libspdm_copy_mem(oid_other, oid_len, encry_algo_oid_rsa3072_other, oid_len);
598 : 36 : return true;
599 : : #else
600 : : return false;
601 : : #endif
602 : : }
603 : 34 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_4096:
604 : : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_4096: {
605 : : #if (LIBSPDM_RSA_SSA_4096_SUPPORT) || (LIBSPDM_RSA_PSS_4096_SUPPORT)
606 : 34 : uint8_t encry_algo_oid_rsa4096[] = KEY_ENCRY_ALGO_RSA4096_FLAG;
607 : 34 : uint8_t encry_algo_oid_rsa4096_other[] = KEY_ENCRY_ALGO_RSA4096_FLAG_OTHER;
608 : 34 : libspdm_copy_mem(oid, oid_len, encry_algo_oid_rsa4096, oid_len);
609 : 34 : libspdm_copy_mem(oid_other, oid_len, encry_algo_oid_rsa4096_other, oid_len);
610 : 34 : return true;
611 : : #else
612 : : return false;
613 : : #endif
614 : : }
615 : :
616 : 1939 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P256: {
617 : : #if LIBSPDM_ECDSA_P256_SUPPORT
618 : 1939 : uint8_t encry_algo_oid_ecc256[] = KEY_ENCRY_ALGO_ECC256_OID;
619 : 1939 : libspdm_copy_mem(oid, oid_len, encry_algo_oid_ecc256, oid_len);
620 : 1939 : return true;
621 : : #else
622 : : return false;
623 : : #endif
624 : : }
625 : 17 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P384: {
626 : : #if LIBSPDM_ECDSA_P384_SUPPORT
627 : 17 : uint8_t encry_algo_oid_ecc384[] = KEY_ENCRY_ALGO_ECC384_OID;
628 : 17 : libspdm_copy_mem(oid, oid_len, encry_algo_oid_ecc384, oid_len);
629 : 17 : return true;
630 : : #else
631 : : return false;
632 : : #endif
633 : : }
634 : 17 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P521: {
635 : : #if LIBSPDM_ECDSA_P521_SUPPORT
636 : 17 : uint8_t encry_algo_oid_ecc521[] = KEY_ENCRY_ALGO_ECC521_OID;
637 : 17 : libspdm_copy_mem(oid, oid_len, encry_algo_oid_ecc521, oid_len);
638 : 17 : return true;
639 : : #else
640 : : return false;
641 : : #endif
642 : : }
643 : :
644 : : /*sm2 oid TBD*/
645 : 0 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_SM2_ECC_SM2_P256:
646 : 0 : return true;
647 : :
648 : 9 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_EDDSA_ED25519: {
649 : : #if LIBSPDM_EDDSA_ED25519_SUPPORT
650 : 9 : uint8_t encry_algo_oid_ed25519[] = ENCRY_ALGO_ED25519_OID;
651 : 9 : libspdm_copy_mem(oid, oid_len, encry_algo_oid_ed25519, oid_len);
652 : 9 : return true;
653 : : #else
654 : 0 : return false;
655 : : #endif
656 : : break;
657 : : }
658 : 9 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_EDDSA_ED448: {
659 : : #if LIBSPDM_EDDSA_ED448_SUPPORT
660 : 9 : uint8_t encry_algo_oid_ed448[] = ENCRY_ALGO_ED448_OID;
661 : 9 : libspdm_copy_mem(oid, oid_len, encry_algo_oid_ed448, oid_len);
662 : 9 : return true;
663 : : #else
664 : 0 : return false;
665 : : #endif
666 : : break;
667 : : }
668 : :
669 : 0 : default:
670 : 0 : LIBSPDM_ASSERT(false);
671 : 0 : return false;
672 : : }
673 : : }
674 [ + - ]: 136 : if (pqc_asym_algo != 0) {
675 [ + + + + : 136 : switch (pqc_asym_algo) {
+ + + + +
+ + + + +
+ - ]
676 : 9 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_ML_DSA_44: {
677 : : #if LIBSPDM_ML_DSA_44_SUPPORT
678 : 9 : uint8_t algo_oid_mldsa44[] = ALGO_MLDSA44_OID;
679 : 9 : libspdm_copy_mem(oid, oid_len, algo_oid_mldsa44, oid_len);
680 : 9 : return true;
681 : : #else
682 : 0 : return false;
683 : : #endif
684 : : break;
685 : : }
686 : 9 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_ML_DSA_65: {
687 : : #if LIBSPDM_ML_DSA_65_SUPPORT
688 : 9 : uint8_t algo_oid_mldsa65[] = ALGO_MLDSA65_OID;
689 : 9 : libspdm_copy_mem(oid, oid_len, algo_oid_mldsa65, oid_len);
690 : 9 : return true;
691 : : #else
692 : 0 : return false;
693 : : #endif
694 : : break;
695 : : }
696 : 9 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_ML_DSA_87: {
697 : : #if LIBSPDM_ML_DSA_87_SUPPORT
698 : 9 : uint8_t algo_oid_mldsa87[] = ALGO_MLDSA87_OID;
699 : 9 : libspdm_copy_mem(oid, oid_len, algo_oid_mldsa87, oid_len);
700 : 9 : return true;
701 : : #else
702 : 0 : return false;
703 : : #endif
704 : : break;
705 : : }
706 : 9 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_128S: {
707 : : #if LIBSPDM_SLH_DSA_SHA2_128S_SUPPORT
708 : 9 : uint8_t algo_oid_slhdsa_sha2_128s[] = ALGO_SLHDSA_SHA2_128S_OID;
709 : 9 : libspdm_copy_mem(oid, oid_len, algo_oid_slhdsa_sha2_128s, oid_len);
710 : 9 : return true;
711 : : #else
712 : 0 : return false;
713 : : #endif
714 : : break;
715 : : }
716 : 9 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_128S: {
717 : : #if LIBSPDM_SLH_DSA_SHAKE_128S_SUPPORT
718 : 9 : uint8_t algo_oid_slhdsa_shake_128s[] = ALGO_SLHDSA_SHAKE_128S_OID;
719 : 9 : libspdm_copy_mem(oid, oid_len, algo_oid_slhdsa_shake_128s, oid_len);
720 : 9 : return true;
721 : : #else
722 : 0 : return false;
723 : : #endif
724 : : break;
725 : : }
726 : 9 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_128F: {
727 : : #if LIBSPDM_SLH_DSA_SHA2_128F_SUPPORT
728 : 9 : uint8_t algo_oid_slhdsa_sha2_128f[] = ALGO_SLHDSA_SHA2_128F_OID;
729 : 9 : libspdm_copy_mem(oid, oid_len, algo_oid_slhdsa_sha2_128f, oid_len);
730 : 9 : return true;
731 : : #else
732 : 0 : return false;
733 : : #endif
734 : : break;
735 : : }
736 : 9 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_128F: {
737 : : #if LIBSPDM_SLH_DSA_SHAKE_128F_SUPPORT
738 : 9 : uint8_t algo_oid_slhdsa_shake_128f[] = ALGO_SLHDSA_SHAKE_128F_OID;
739 : 9 : libspdm_copy_mem(oid, oid_len, algo_oid_slhdsa_shake_128f, oid_len);
740 : 9 : return true;
741 : : #else
742 : 0 : return false;
743 : : #endif
744 : : break;
745 : : }
746 : 9 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_192S: {
747 : : #if LIBSPDM_SLH_DSA_SHA2_192S_SUPPORT
748 : 9 : uint8_t algo_oid_slhdsa_sha2_192s[] = ALGO_SLHDSA_SHA2_192S_OID;
749 : 9 : libspdm_copy_mem(oid, oid_len, algo_oid_slhdsa_sha2_192s, oid_len);
750 : 9 : return true;
751 : : #else
752 : 0 : return false;
753 : : #endif
754 : : break;
755 : : }
756 : 9 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_192S: {
757 : : #if LIBSPDM_SLH_DSA_SHAKE_192S_SUPPORT
758 : 9 : uint8_t algo_oid_slhdsa_shake_192s[] = ALGO_SLHDSA_SHAKE_192S_OID;
759 : 9 : libspdm_copy_mem(oid, oid_len, algo_oid_slhdsa_shake_192s, oid_len);
760 : 9 : return true;
761 : : #else
762 : 0 : return false;
763 : : #endif
764 : : break;
765 : : }
766 : 9 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_192F: {
767 : : #if LIBSPDM_SLH_DSA_SHA2_192F_SUPPORT
768 : 9 : uint8_t algo_oid_slhdsa_sha2_192f[] = ALGO_SLHDSA_SHA2_192F_OID;
769 : 9 : libspdm_copy_mem(oid, oid_len, algo_oid_slhdsa_sha2_192f, oid_len);
770 : 9 : return true;
771 : : #else
772 : 0 : return false;
773 : : #endif
774 : : break;
775 : : }
776 : 9 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_192F: {
777 : : #if LIBSPDM_SLH_DSA_SHAKE_192F_SUPPORT
778 : 9 : uint8_t algo_oid_slhdsa_shake_192f[] = ALGO_SLHDSA_SHAKE_192F_OID;
779 : 9 : libspdm_copy_mem(oid, oid_len, algo_oid_slhdsa_shake_192f, oid_len);
780 : 9 : return true;
781 : : #else
782 : 0 : return false;
783 : : #endif
784 : : break;
785 : : }
786 : 9 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_256S: {
787 : : #if LIBSPDM_SLH_DSA_SHA2_256S_SUPPORT
788 : 9 : uint8_t algo_oid_slhdsa_sha2_256s[] = ALGO_SLHDSA_SHA2_256S_OID;
789 : 9 : libspdm_copy_mem(oid, oid_len, algo_oid_slhdsa_sha2_256s, oid_len);
790 : 9 : return true;
791 : : #else
792 : 0 : return false;
793 : : #endif
794 : : break;
795 : : }
796 : 9 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_256S: {
797 : : #if LIBSPDM_SLH_DSA_SHAKE_256S_SUPPORT
798 : 9 : uint8_t algo_oid_slhdsa_shake_256s[] = ALGO_SLHDSA_SHAKE_256S_OID;
799 : 9 : libspdm_copy_mem(oid, oid_len, algo_oid_slhdsa_shake_256s, oid_len);
800 : 9 : return true;
801 : : #else
802 : 0 : return false;
803 : : #endif
804 : : break;
805 : : }
806 : 10 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_256F: {
807 : : #if LIBSPDM_SLH_DSA_SHA2_256F_SUPPORT
808 : 10 : uint8_t algo_oid_slhdsa_sha2_256f[] = ALGO_SLHDSA_SHA2_256F_OID;
809 : 10 : libspdm_copy_mem(oid, oid_len, algo_oid_slhdsa_sha2_256f, oid_len);
810 : 10 : return true;
811 : : #else
812 : 0 : return false;
813 : : #endif
814 : : break;
815 : : }
816 : 9 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_256F: {
817 : : #if LIBSPDM_SLH_DSA_SHAKE_256F_SUPPORT
818 : 9 : uint8_t algo_oid_slhdsa_shake_256f[] = ALGO_SLHDSA_SHAKE_256F_OID;
819 : 9 : libspdm_copy_mem(oid, oid_len, algo_oid_slhdsa_shake_256f, oid_len);
820 : 9 : return true;
821 : : #else
822 : 0 : return false;
823 : : #endif
824 : : break;
825 : : }
826 : 0 : default:
827 : 0 : LIBSPDM_ASSERT(false);
828 : 0 : return false;
829 : : }
830 : : }
831 : 0 : LIBSPDM_ASSERT(false);
832 : 0 : return false;
833 : : }
834 : :
835 : : /**
836 : : * Verify cert public key encryption algorithm is matched to negotiated base_asym algo
837 : : *
838 : : * @param[in] cert Pointer to the DER-encoded certificate data.
839 : : * @param[in] cert_size The size of certificate data in bytes.
840 : : * @param[out] oid cert public key encryption algorithm OID
841 : : * @param[in] oid_size the buffer size for required OID
842 : : * @param[in] base_asym_algo SPDM base_asym_algo
843 : : * @param[in] pqc_asym_algo SPDM pqc_asym_algo
844 : : *
845 : : * @retval true get public key oid from cert successfully
846 : : * @retval false get public key oid from cert fail
847 : : **/
848 : 2383 : static bool libspdm_get_public_key_oid(
849 : : const uint8_t *cert, size_t cert_size,
850 : : uint8_t *oid, size_t oid_size, uint32_t base_asym_algo, uint32_t pqc_asym_algo)
851 : : {
852 : : bool ret;
853 : : uint8_t *ptr;
854 : : int32_t length;
855 : : size_t obj_len;
856 : : uint8_t *end;
857 : : uint8_t index;
858 : : uint8_t sequence_time;
859 : :
860 : 2383 : length = (int32_t)cert_size;
861 : 2383 : ptr = (uint8_t*)(size_t)cert;
862 : 2383 : obj_len = 0;
863 : 2383 : end = ptr + length;
864 : 2383 : ret = true;
865 : :
866 : : /* TBSCertificate have 5 sequence before subjectPublicKeyInfo*/
867 : 2383 : sequence_time = 5;
868 : :
869 : : /*all cert sequence*/
870 : 2383 : ret = libspdm_asn1_get_tag(&ptr, end, &obj_len,
871 : : LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
872 [ - + ]: 2383 : if (!ret) {
873 : 0 : return false;
874 : : }
875 : :
876 : : /*TBSCertificate sequence*/
877 : 2383 : ret = libspdm_asn1_get_tag(&ptr, end, &obj_len,
878 : : LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
879 [ - + ]: 2383 : if (!ret) {
880 : 0 : return false;
881 : : }
882 : :
883 : 2383 : end = ptr + obj_len;
884 : : /*version*/
885 : 2383 : ret = libspdm_asn1_get_tag(&ptr, end, &obj_len,
886 : : LIBSPDM_CRYPTO_ASN1_CONTEXT_SPECIFIC |
887 : : LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
888 [ - + ]: 2383 : if (!ret) {
889 : 0 : return false;
890 : : }
891 : :
892 : 2383 : ptr += obj_len;
893 : : /*serialNumber*/
894 : 2383 : ret = libspdm_asn1_get_tag(&ptr, end, &obj_len, LIBSPDM_CRYPTO_ASN1_INTEGER);
895 [ - + ]: 2383 : if (!ret) {
896 : 0 : return false;
897 : : }
898 : :
899 : : /**
900 : : * signature AlgorithmIdentifier,
901 : : * issuer Name,
902 : : * validity Validity,
903 : : * subject Name,
904 : : * subjectPublicKeyInfo
905 : : **/
906 [ + + ]: 14298 : for (index = 0; index < sequence_time; index++) {
907 : 11915 : ptr += obj_len;
908 : 11915 : ret = libspdm_asn1_get_tag(&ptr, end, &obj_len,
909 : : LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
910 [ - + ]: 11915 : if (!ret) {
911 : 0 : return false;
912 : : }
913 : : }
914 : :
915 [ + + ]: 2383 : if (base_asym_algo != 0) {
916 [ + + + - ]: 2247 : switch (base_asym_algo)
917 : : {
918 : 256 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_2048:
919 : : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_2048:
920 : : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_3072:
921 : : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_3072:
922 : : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSASSA_4096:
923 : : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_RSAPSS_4096:
924 : 256 : ret = libspdm_asn1_get_tag(&ptr, end, &obj_len,
925 : : LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
926 [ - + ]: 256 : if (!ret) {
927 : 0 : return false;
928 : : }
929 : :
930 : 256 : ptr += obj_len;
931 : 256 : ret = libspdm_asn1_get_tag(&ptr, end, &obj_len, LIBSPDM_CRYPTO_ASN1_BIT_STRING);
932 [ - + ]: 256 : if (!ret) {
933 : 0 : return false;
934 : : }
935 : :
936 : : /*get rsa key len*/
937 : 256 : ptr++;
938 : 256 : ret = libspdm_asn1_get_tag(&ptr, end, &obj_len,
939 : : LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
940 [ + + ]: 256 : if (!ret) {
941 : 56 : return false;
942 : : }
943 : 200 : libspdm_copy_mem(oid, oid_size, ptr, oid_size);
944 : 200 : break;
945 : 1973 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P256:
946 : : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P384:
947 : : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_ECDSA_ECC_NIST_P521:
948 : 1973 : ret = libspdm_asn1_get_tag(&ptr, end, &obj_len,
949 : : LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
950 [ - + ]: 1973 : if (!ret) {
951 : 0 : return false;
952 : : }
953 : 1973 : ret = libspdm_asn1_get_tag(&ptr, end, &obj_len, LIBSPDM_CRYPTO_ASN1_OID);
954 [ - + ]: 1973 : if (!ret) {
955 : 0 : return false;
956 : : }
957 : :
958 : : /*get ecc second oid*/
959 : 1973 : ptr +=obj_len;
960 : 1973 : ret = libspdm_asn1_get_tag(&ptr, end, &obj_len, LIBSPDM_CRYPTO_ASN1_OID);
961 [ + + ]: 1973 : if (!ret) {
962 : 24 : return false;
963 : : }
964 : :
965 [ + + ]: 1949 : if (oid_size != obj_len) {
966 : 10 : return false;
967 : : }
968 : :
969 : 1939 : libspdm_copy_mem(oid, oid_size, ptr, obj_len);
970 : 1939 : break;
971 : 18 : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_EDDSA_ED25519:
972 : : case SPDM_ALGORITHMS_BASE_ASYM_ALGO_EDDSA_ED448:
973 : 18 : ret = libspdm_asn1_get_tag(&ptr, end, &obj_len,
974 : : LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
975 [ - + ]: 18 : if (!ret) {
976 : 0 : return false;
977 : : }
978 : :
979 : : /*get eddsa oid*/
980 : 18 : ret = libspdm_asn1_get_tag(&ptr, end, &obj_len, LIBSPDM_CRYPTO_ASN1_OID);
981 [ - + ]: 18 : if (!ret) {
982 : 0 : return false;
983 : : }
984 : :
985 [ + + ]: 18 : if (oid_size != obj_len) {
986 : 14 : return false;
987 : : }
988 : :
989 : 4 : libspdm_copy_mem(oid, oid_size, ptr, obj_len);
990 : 4 : break;
991 : 0 : default:
992 : 0 : LIBSPDM_ASSERT(false);
993 : 0 : return false;
994 : : }
995 : : }
996 [ + + ]: 2279 : if (pqc_asym_algo != 0) {
997 [ + - ]: 136 : switch (pqc_asym_algo)
998 : : {
999 : 136 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_ML_DSA_44:
1000 : : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_ML_DSA_65:
1001 : : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_ML_DSA_87:
1002 : : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_128S:
1003 : : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_128S:
1004 : : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_128F:
1005 : : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_128F:
1006 : : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_192S:
1007 : : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_192S:
1008 : : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_192F:
1009 : : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_192F:
1010 : : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_256S:
1011 : : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_256S:
1012 : : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_256F:
1013 : : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_256F:
1014 : : /* algorithm AlgorithmIdentifier SEQUENCE */
1015 : 136 : ret = libspdm_asn1_get_tag(&ptr, end, &obj_len,
1016 : : LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
1017 [ - + ]: 136 : if (!ret) {
1018 : 0 : return false;
1019 : : }
1020 : :
1021 : : /* OID */
1022 : 136 : ret = libspdm_asn1_get_tag(&ptr, end, &obj_len, LIBSPDM_CRYPTO_ASN1_OID);
1023 [ - + ]: 136 : if (!ret) {
1024 : 0 : return false;
1025 : : }
1026 : :
1027 [ + + ]: 136 : if (oid_size != obj_len) {
1028 : 90 : return false;
1029 : : }
1030 : :
1031 : 46 : libspdm_copy_mem(oid, oid_size, ptr, obj_len);
1032 : 46 : break;
1033 : 0 : default:
1034 : 0 : LIBSPDM_ASSERT(false);
1035 : 0 : return false;
1036 : : }
1037 : : }
1038 : 2189 : return true;
1039 : : }
1040 : :
1041 : : /**
1042 : : * Verify cert public key encryption algorithm is matched to negotiated base_asym algo
1043 : : *
1044 : : * @param[in] cert Pointer to the DER-encoded certificate data.
1045 : : * @param[in] cert_size The size of certificate data in bytes.
1046 : : * @param[in] base_asym_algo SPDM base_asym_algo
1047 : : * @param[in] pqc_asym_algo SPDM pqc_asym_algo
1048 : : *
1049 : : * @retval true verify pass
1050 : : * @retval false verify fail
1051 : : **/
1052 : 2485 : static bool libspdm_verify_cert_subject_public_key_info(const uint8_t *cert, size_t cert_size,
1053 : : uint32_t base_asym_algo, uint32_t pqc_asym_algo)
1054 : : {
1055 : : size_t oid_len;
1056 : : bool status;
1057 : :
1058 : : /*public key encrypt algo OID from cert*/
1059 : : uint8_t cert_public_key_crypt_algo_oid[LIBSPDM_MAX_ENCRYPTION_ALGO_OID_LEN];
1060 : : /*public key encrypt algo OID from libspdm stored*/
1061 : : uint8_t libspdm_public_key_crypt_algo_oid[LIBSPDM_MAX_ENCRYPTION_ALGO_OID_LEN];
1062 : : uint8_t libspdm_public_key_crypt_algo_oid_other[LIBSPDM_MAX_ENCRYPTION_ALGO_OID_LEN];
1063 : :
1064 : 2485 : libspdm_zero_mem(libspdm_public_key_crypt_algo_oid, LIBSPDM_MAX_ENCRYPTION_ALGO_OID_LEN);
1065 : 2485 : libspdm_zero_mem(libspdm_public_key_crypt_algo_oid_other, LIBSPDM_MAX_ENCRYPTION_ALGO_OID_LEN);
1066 : :
1067 : : /*work around: skip the sm2*/
1068 [ - + ]: 2485 : if (base_asym_algo == SPDM_ALGORITHMS_BASE_ASYM_ALGO_TPM_ALG_SM2_ECC_SM2_P256) {
1069 : 0 : return true;
1070 : : }
1071 : :
1072 : 2485 : oid_len = libspdm_get_public_key_algo_OID_len(base_asym_algo, pqc_asym_algo);
1073 [ + + ]: 2485 : if (oid_len == 0) {
1074 : 102 : return false;
1075 : : }
1076 : : /*get public key encrypt algo OID from libspdm stored*/
1077 : 2383 : status = libspdm_get_public_key_algo_OID(base_asym_algo, pqc_asym_algo,
1078 : : libspdm_public_key_crypt_algo_oid,
1079 : : libspdm_public_key_crypt_algo_oid_other);
1080 [ - + ]: 2383 : if (!status) {
1081 : 0 : return status;
1082 : : }
1083 : :
1084 : : /*get public key encrypt algo OID from cert*/
1085 : 2383 : status = libspdm_get_public_key_oid(cert, cert_size, cert_public_key_crypt_algo_oid, oid_len,
1086 : : base_asym_algo, pqc_asym_algo);
1087 [ + + + + ]: 2383 : if (!status || (!libspdm_consttime_is_mem_equal(cert_public_key_crypt_algo_oid,
1088 : 80 : libspdm_public_key_crypt_algo_oid, oid_len) &&
1089 [ + - ]: 80 : !libspdm_consttime_is_mem_equal(cert_public_key_crypt_algo_oid,
1090 : : libspdm_public_key_crypt_algo_oid_other,
1091 : : oid_len))) {
1092 : 274 : return false;
1093 : : }
1094 : :
1095 : 2109 : return status;
1096 : : }
1097 : :
1098 : : /**
1099 : : * Verifies that the basic_constraints of a leaf certificate, if present, has CA false.
1100 : : *
1101 : : * basic_constraints is optional in a leaf certificate for every SPDM version, since DMTF reverted
1102 : : * the requirement that SPDM 1.3 added.
1103 : : *
1104 : : * @param[in] cert Pointer to the DER-encoded certificate data.
1105 : : * @param[in] cert_size The size of certificate data in bytes.
1106 : : *
1107 : : * @retval true basic_constraints is not present, or its CA is false.
1108 : : * @retval false verify fail
1109 : : **/
1110 : 2093 : static bool libspdm_verify_leaf_cert_basic_constraints(const uint8_t *cert, size_t cert_size)
1111 : : {
1112 : : bool status;
1113 : : /*basic_constraints from cert*/
1114 : : uint8_t cert_basic_constraints[LIBSPDM_MAX_BASIC_CONSTRAINTS_CA_LEN];
1115 : : size_t len;
1116 : :
1117 : 2093 : uint8_t basic_constraints_false_case[] = BASIC_CONSTRAINTS_CA_FALSE;
1118 : :
1119 : 2093 : len = LIBSPDM_MAX_BASIC_CONSTRAINTS_CA_LEN;
1120 : :
1121 : 2093 : status = libspdm_x509_get_extended_basic_constraints(cert, cert_size,
1122 : : cert_basic_constraints, &len);
1123 [ - + ]: 2093 : if (!status) {
1124 : 0 : return false;
1125 [ + + ]: 2093 : } else if (len == 0) {
1126 : : /* basic constraints is not present in cert */
1127 : 24 : return true;
1128 : : }
1129 : :
1130 [ + + + - ]: 4114 : if ((len == sizeof(basic_constraints_false_case)) &&
1131 : 2045 : (libspdm_consttime_is_mem_equal(cert_basic_constraints,
1132 : : basic_constraints_false_case,
1133 : : sizeof(basic_constraints_false_case)))) {
1134 : 2045 : return true;
1135 : : }
1136 : :
1137 : 24 : return false;
1138 : : }
1139 : :
1140 : : /**
1141 : : * Verifies the basic_constraints of the last certificate in a SET_CERTIFICATE chain.
1142 : : *
1143 : : * If the certificate model is DeviceCert or GenericCert then the certificate is the leaf
1144 : : * certificate, so basic_constraints is optional and its CA must be false if it is present.
1145 : : *
1146 : : * If the certificate model is AliasCert then the certificate is the Device Certificate CA, so
1147 : : * basic_constraints must be present and its CA must be true.
1148 : : *
1149 : : * @param[in] cert Pointer to the DER-encoded certificate data.
1150 : : * @param[in] cert_size The size of certificate data in bytes.
1151 : : * @param[in] cert_model The certificate model.
1152 : : *
1153 : : * @retval true verify pass
1154 : : * @retval false verify fail
1155 : : **/
1156 : 72 : static bool libspdm_verify_set_cert_leaf_cert_basic_constraints(
1157 : : const uint8_t *cert, size_t cert_size, uint8_t cert_model)
1158 : : {
1159 : : bool status;
1160 : : /* basic_constraints from certificate. */
1161 : : uint8_t cert_basic_constraints[LIBSPDM_MAX_BASIC_CONSTRAINTS_CA_LEN];
1162 : : size_t len;
1163 : :
1164 : 72 : const uint8_t basic_constraints_true_case[] = BASIC_CONSTRAINTS_CA_TRUE;
1165 : :
1166 [ + + + + ]: 72 : if ((cert_model == SPDM_CERTIFICATE_INFO_CERT_MODEL_DEVICE_CERT) ||
1167 : : (cert_model == SPDM_CERTIFICATE_INFO_CERT_MODEL_GENERIC_CERT)) {
1168 : 46 : return libspdm_verify_leaf_cert_basic_constraints(cert, cert_size);
1169 : : }
1170 : :
1171 : : /* Alias certificate model. */
1172 : 26 : len = LIBSPDM_MAX_BASIC_CONSTRAINTS_CA_LEN;
1173 : :
1174 : 26 : status = libspdm_x509_get_extended_basic_constraints(cert, cert_size,
1175 : : cert_basic_constraints, &len);
1176 [ - + ]: 26 : if (!status) {
1177 : 0 : return false;
1178 : : }
1179 : :
1180 : : /* basicConstraints may include the pathLen field. Therefore do not check sequence length. */
1181 [ + + ]: 26 : if (len >= sizeof(basic_constraints_true_case)) {
1182 [ - + ]: 16 : if (cert_basic_constraints[0] != basic_constraints_true_case[0]) {
1183 : 0 : return false;
1184 : : }
1185 [ + - ]: 16 : if (libspdm_consttime_is_mem_equal(&cert_basic_constraints[2],
1186 : : &basic_constraints_true_case[2],
1187 : : sizeof(basic_constraints_true_case) - 2)) {
1188 : 16 : return true;
1189 : : }
1190 : : }
1191 : 10 : return false;
1192 : : }
1193 : :
1194 : : /**
1195 : : * Verify leaf cert spdm defined extended key usage
1196 : : *
1197 : : * @param[in] cert Pointer to the DER-encoded certificate data.
1198 : : * @param[in] cert_size The size of certificate data in bytes.
1199 : : * @param[in] is_requester_cert Is the function verifying requester or responder cert.
1200 : : *
1201 : : * @retval true verify pass, two cases:
1202 : : * 1. spdm defined eku is not present in cert;
1203 : : * 2. spdm defined eku is compliant with requester/responder identity;
1204 : : * @retval false verify fail, two cases:
1205 : : * 1. requester's cert has only responder auth oid in eku;
1206 : : * 2. responder's cert has only requester auth oid in eku;
1207 : : **/
1208 : 2135 : static bool libspdm_verify_leaf_cert_spdm_eku(const uint8_t *cert, size_t cert_size,
1209 : : bool is_requester_cert)
1210 : : {
1211 : : bool status;
1212 : : uint8_t eku[256];
1213 : : size_t eku_size;
1214 : : bool req_auth_oid_find_success;
1215 : : bool rsp_auth_oid_find_success;
1216 : : uint8_t *ptr;
1217 : : size_t obj_len;
1218 : :
1219 : : /* SPDM defined OID */
1220 : 2135 : uint8_t eku_requester_auth_oid[] = SPDM_OID_DMTF_EKU_REQUESTER_AUTH;
1221 : 2135 : uint8_t eku_responder_auth_oid[] = SPDM_OID_DMTF_EKU_RESPONDER_AUTH;
1222 : :
1223 : 2135 : eku_size = sizeof(eku);
1224 : 2135 : status = libspdm_x509_get_extended_key_usage(cert, cert_size, eku, &eku_size);
1225 [ - + ]: 2135 : if (!status) {
1226 : 0 : return false;
1227 [ - + ]: 2135 : } else if (eku_size == 0) {
1228 : : /* eku is not present in cert */
1229 : 0 : return true;
1230 : : }
1231 : :
1232 : 2135 : ptr = eku;
1233 : 2135 : obj_len = 0;
1234 : 2135 : req_auth_oid_find_success = false;
1235 : 2135 : rsp_auth_oid_find_success = false;
1236 : :
1237 : 2135 : status = libspdm_asn1_get_tag(&ptr, eku + eku_size, &obj_len,
1238 : : LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
1239 [ - + ]: 2135 : if (!status) {
1240 : 0 : return false;
1241 : : }
1242 : :
1243 [ + + ]: 8550 : while(ptr < eku + eku_size) {
1244 : 6415 : status = libspdm_asn1_get_tag(&ptr, eku + eku_size, &obj_len, LIBSPDM_CRYPTO_ASN1_OID);
1245 [ - + ]: 6415 : if (!status) {
1246 : 0 : return false;
1247 : : }
1248 : :
1249 [ + + + + ]: 6435 : if ((obj_len == sizeof(eku_requester_auth_oid)) &&
1250 : 20 : (libspdm_consttime_is_mem_equal(ptr, eku_requester_auth_oid,
1251 : : sizeof(eku_requester_auth_oid)))) {
1252 : 10 : req_auth_oid_find_success = true;
1253 : : }
1254 [ + + + + ]: 6435 : if ((obj_len == sizeof(eku_responder_auth_oid)) &&
1255 : 20 : (libspdm_consttime_is_mem_equal(ptr, eku_responder_auth_oid,
1256 : : sizeof(eku_responder_auth_oid)))) {
1257 : 10 : rsp_auth_oid_find_success = true;
1258 : : }
1259 : :
1260 : 6415 : ptr += obj_len;
1261 : : }
1262 : :
1263 [ - + ]: 2135 : if (ptr != eku + eku_size) {
1264 : 0 : return false;
1265 : : }
1266 : :
1267 [ + + ]: 2135 : if (is_requester_cert) {
1268 : : /* it should not only contain responder auth oid */
1269 [ + + + + ]: 74 : if (!req_auth_oid_find_success && rsp_auth_oid_find_success) {
1270 : 2 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "Requester certificate contains Responder OID.\n"));
1271 : 2 : return false;
1272 : : }
1273 : : } else {
1274 : : /* it should not only contain requester auth oid */
1275 [ + + + + ]: 2061 : if (req_auth_oid_find_success && !rsp_auth_oid_find_success) {
1276 : 4 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "Responder certificate contains Requester OID.\n"));
1277 : 4 : return false;
1278 : : }
1279 : : }
1280 : :
1281 : 2129 : return true;
1282 : : }
1283 : :
1284 : 2103 : bool libspdm_contains_hardware_id_oid(const uint8_t *cert, size_t cert_size)
1285 : : {
1286 : : bool status;
1287 : : bool find_successful;
1288 : : uint8_t spdm_extension[LIBSPDM_MAX_EXTENSION_LEN];
1289 : : size_t len;
1290 : : uint8_t *ptr;
1291 : : uint8_t *temptr;
1292 : : size_t obj_len;
1293 : :
1294 : : /* SPDM defined OID */
1295 : 2103 : uint8_t oid_spdm_extension[] = SPDM_OID_DMTF_SPDM_EXTENSION;
1296 : 2103 : uint8_t hardware_identity_oid[] = SPDM_OID_DMTF_HARDWARE_IDENTITY;
1297 : :
1298 : 2103 : len = LIBSPDM_MAX_EXTENSION_LEN;
1299 : :
1300 [ + - - + ]: 2103 : if (cert == NULL || cert_size == 0) {
1301 : 0 : return false;
1302 : : }
1303 : :
1304 : 2103 : status = libspdm_x509_get_extension_data(cert, cert_size,
1305 : : (const uint8_t *)oid_spdm_extension,
1306 : : sizeof(oid_spdm_extension),
1307 : : spdm_extension,
1308 : : &len);
1309 [ - + ]: 2103 : if (!status) {
1310 : 0 : return false;
1311 [ + + ]: 2103 : } else if (len == 0) {
1312 : 36 : return false;
1313 : : }
1314 : :
1315 : : /*find the spdm hardware identity OID*/
1316 : 2067 : find_successful = false;
1317 : 2067 : ptr = spdm_extension;
1318 : 2067 : obj_len = 0;
1319 : :
1320 : : /*id-spdm-cert-oids ::= SEQUENCE SIZE (1..MAX) OF id-spdm-cert-oid*/
1321 : 2067 : status = libspdm_asn1_get_tag(
1322 : : &ptr, spdm_extension + len, &obj_len,
1323 : : LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
1324 [ + + ]: 2067 : if (!status) {
1325 : 4 : return false;
1326 : : }
1327 : :
1328 [ + + ]: 4126 : while(ptr < spdm_extension + len) {
1329 : 2063 : status = libspdm_asn1_get_tag(
1330 : : &ptr, spdm_extension + len, &obj_len,
1331 : : LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
1332 [ - + ]: 2063 : if (!status) {
1333 : 0 : return false;
1334 : : }
1335 : :
1336 : 2063 : temptr = ptr + obj_len;
1337 : 2063 : status = libspdm_asn1_get_tag(
1338 : : &ptr, spdm_extension + len, &obj_len, LIBSPDM_CRYPTO_ASN1_OID);
1339 [ - + ]: 2063 : if (!status) {
1340 : 0 : return false;
1341 : : }
1342 [ + - + - ]: 4126 : if ((obj_len == sizeof(hardware_identity_oid)) &&
1343 : 2063 : (libspdm_consttime_is_mem_equal(ptr, hardware_identity_oid,
1344 : : sizeof(hardware_identity_oid)))) {
1345 : 2063 : find_successful = true;
1346 : : }
1347 : 2063 : ptr = temptr;
1348 : : }
1349 : :
1350 [ - + ]: 2063 : if (ptr != spdm_extension + len) {
1351 : 0 : return false;
1352 : : }
1353 : :
1354 : 2063 : return find_successful;
1355 : : }
1356 : :
1357 : : /**
1358 : : * Verify leaf cert spdm defined extension
1359 : : *
1360 : : * @param[in] cert Pointer to the DER-encoded certificate data.
1361 : : * @param[in] cert_size The size of certificate data in bytes.
1362 : : * @param[in] is_requester_cert Is the function verifying requester or responder cert.
1363 : : * @param[in] cert_model One of the SPDM_CERTIFICATE_INFO_CERT_MODEL_* macros.
1364 : : *
1365 : : * @retval true verify pass
1366 : : * @retval false verify fail, two cases: 1. Unable to get or validate extension data.
1367 : : * 2. hardware_identity_oid is found in AliasCert model;
1368 : : **/
1369 : 2091 : static bool libspdm_verify_leaf_cert_spdm_extension(const uint8_t *cert, size_t cert_size,
1370 : : bool is_requester_cert,
1371 : : uint8_t cert_model)
1372 : : {
1373 : 2091 : bool find_successful = libspdm_contains_hardware_id_oid(cert, cert_size);
1374 : :
1375 : : /* Responder does not determine Requester's certificate model */
1376 [ + + ]: 2091 : if (!is_requester_cert) {
1377 [ + + + + ]: 2023 : if ((find_successful) && (cert_model == SPDM_CERTIFICATE_INFO_CERT_MODEL_ALIAS_CERT)) {
1378 : : /* Hardware_identity_OID is found in alias cert model */
1379 : 10 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
1380 : : "Hardware identity OID present in alias leaf certificate.\n"));
1381 : 10 : return false;
1382 : : }
1383 : : }
1384 : :
1385 : 2081 : return true;
1386 : : }
1387 : :
1388 : : /**
1389 : : * Certificate common Check for SPDM leaf cert when get_cert and set_cert.
1390 : : *
1391 : : * @param[in] cert Pointer to the DER-encoded certificate data.
1392 : : * @param[in] cert_size The size of certificate data in bytes.
1393 : : * @param[in] base_asym_algo SPDM base_asym_algo
1394 : : * @param[in] pqc_asym_algo SPDM pqc_asym_algo
1395 : : * @param[in] is_requester_cert Is the function verifying requester or responder cert.
1396 : : * @param[in] cert_model One of the SPDM_CERTIFICATE_INFO_CERT_MODEL_* macros.
1397 : : * @param[in] set_cert Is the function verifying a set certificate operation.
1398 : : *
1399 : : * @retval true Success.
1400 : : * @retval false Certificate is not valid.
1401 : : **/
1402 : 2593 : static bool libspdm_x509_common_certificate_check(
1403 : : const uint8_t *cert, size_t cert_size,
1404 : : uint32_t base_asym_algo, uint32_t pqc_asym_algo,
1405 : : bool is_requester_cert, uint8_t cert_model,
1406 : : bool set_cert)
1407 : : {
1408 : : uint8_t end_cert_from[64];
1409 : : size_t end_cert_from_len;
1410 : : uint8_t end_cert_to[64];
1411 : : size_t end_cert_to_len;
1412 : : size_t asn1_buffer_len;
1413 : : bool status;
1414 : : size_t cert_version;
1415 : : void *context;
1416 : : size_t signature_algo_oid_size;
1417 : :
1418 [ + + - + ]: 2593 : if (cert == NULL || cert_size == 0) {
1419 : 4 : return false;
1420 : : }
1421 : :
1422 : 2589 : status = true;
1423 : 2589 : context = NULL;
1424 : 2589 : end_cert_from_len = 64;
1425 : 2589 : end_cert_to_len = 64;
1426 : :
1427 : : /* 1. Version */
1428 : 2589 : cert_version = 0;
1429 : 2589 : status = libspdm_x509_get_version(cert, cert_size, &cert_version);
1430 [ + + ]: 2589 : if (!status) {
1431 : 78 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "The mandatory Version field is not present.\n"));
1432 : 78 : goto cleanup;
1433 : : }
1434 [ - + ]: 2511 : if (cert_version != 2) {
1435 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
1436 : : "Expected Version to be equal to 2 but it is actually %zu.\n", cert_version));
1437 : 0 : status = false;
1438 : 0 : goto cleanup;
1439 : : }
1440 : :
1441 : : /* 2. Serial Number */
1442 : 2511 : asn1_buffer_len = 0;
1443 : 2511 : status = libspdm_x509_get_serial_number(cert, cert_size, NULL, &asn1_buffer_len);
1444 [ - + ]: 2511 : if (asn1_buffer_len == 0) {
1445 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "The mandatory Serial Number field is not present.\n"));
1446 : 0 : status = false;
1447 : 0 : goto cleanup;
1448 : : }
1449 : :
1450 : : /* 3. Signature Algorithm */
1451 : 2511 : signature_algo_oid_size = 0;
1452 : 2511 : status = libspdm_x509_get_signature_algorithm(cert, cert_size, NULL, &signature_algo_oid_size);
1453 [ - + ]: 2511 : if (status) {
1454 [ # # # # ]: 0 : if ((signature_algo_oid_size == 0) &&
1455 : : (cert_model != SPDM_CERTIFICATE_INFO_CERT_MODEL_GENERIC_CERT)) {
1456 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
1457 : : "The mandatory Signature Algorithm field is not present.\n"));
1458 : 0 : status = false;
1459 : 0 : goto cleanup;
1460 : : }
1461 : : } else {
1462 [ - + ]: 2511 : if (signature_algo_oid_size == 0) {
1463 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
1464 : : "The mandatory Signature Algorithm field is not present.\n"));
1465 : 0 : status = false;
1466 : 0 : goto cleanup;
1467 : : }
1468 : : }
1469 : :
1470 : : /* 4. Verify public key algorithm.
1471 : : * If this is a SET_CERTIFICATE operation and the endpoint uses the AliasCert model then the
1472 : : * check should be skipped as the Device Certificate CA's public key does not have to use
1473 : : * the same algorithms as the connection's negotiated algorithms. */
1474 [ + + + + ]: 2511 : if (!set_cert || (cert_model != SPDM_CERTIFICATE_INFO_CERT_MODEL_ALIAS_CERT)) {
1475 : 2485 : status = libspdm_verify_cert_subject_public_key_info(cert, cert_size, base_asym_algo,
1476 : : pqc_asym_algo);
1477 [ + + ]: 2485 : if (!status) {
1478 : 376 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
1479 : : "Error in verifying the Public Key Algorithm field.\n"));
1480 : 376 : goto cleanup;
1481 : : }
1482 : : }
1483 : :
1484 : : /* 5. Issuer */
1485 : 2135 : asn1_buffer_len = 0;
1486 : 2135 : status = libspdm_x509_get_issuer_name(cert, cert_size, NULL, &asn1_buffer_len);
1487 [ - + ]: 2135 : if (status) {
1488 [ # # # # ]: 0 : if ((asn1_buffer_len == 0) &&
1489 : : (cert_model != SPDM_CERTIFICATE_INFO_CERT_MODEL_GENERIC_CERT)) {
1490 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "The mandatory Issuer field is not present.\n"));
1491 : 0 : status = false;
1492 : 0 : goto cleanup;
1493 : : }
1494 : : } else {
1495 [ - + ]: 2135 : if (asn1_buffer_len == 0) {
1496 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "The mandatory Issuer field is not present.\n"));
1497 : 0 : status = false;
1498 : 0 : goto cleanup;
1499 : : }
1500 : : }
1501 : :
1502 : : /* 6. subject_name*/
1503 : 2135 : asn1_buffer_len = 0;
1504 : 2135 : status = libspdm_x509_get_subject_name(cert, cert_size, NULL, &asn1_buffer_len);
1505 [ - + ]: 2135 : if (status) {
1506 [ # # # # ]: 0 : if ((asn1_buffer_len == 0) &&
1507 : : (cert_model != SPDM_CERTIFICATE_INFO_CERT_MODEL_GENERIC_CERT)) {
1508 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "The mandatory Subject field is not present.\n"));
1509 : 0 : status = false;
1510 : 0 : goto cleanup;
1511 : : }
1512 : : } else {
1513 [ - + ]: 2135 : if (asn1_buffer_len == 0) {
1514 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "The mandatory Subject field is not present.\n"));
1515 : 0 : status = false;
1516 : 0 : goto cleanup;
1517 : : }
1518 : : }
1519 : :
1520 : : /* 7. Validity */
1521 : 2135 : status = libspdm_x509_get_validity(cert, cert_size, end_cert_from,
1522 : : &end_cert_from_len, end_cert_to,
1523 : : &end_cert_to_len);
1524 [ + - ]: 2135 : if (status) {
1525 [ - + - - ]: 2135 : if ((end_cert_from_len == 0) &&
1526 : : (cert_model != SPDM_CERTIFICATE_INFO_CERT_MODEL_GENERIC_CERT)) {
1527 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "The mandatory Validity field is not present.\n"));
1528 : 0 : status = false;
1529 : 0 : goto cleanup;
1530 : : }
1531 : : } else {
1532 [ # # ]: 0 : if (end_cert_from_len == 0) {
1533 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "The mandatory Validity field is not present.\n"));
1534 : 0 : status = false;
1535 : 0 : goto cleanup;
1536 : : }
1537 : : }
1538 : :
1539 [ + - ]: 2135 : if (end_cert_from_len != 0) {
1540 : 2135 : status = libspdm_internal_x509_date_time_check(
1541 : : end_cert_from, end_cert_from_len, end_cert_to, end_cert_to_len);
1542 [ - + ]: 2135 : if (!status) {
1543 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
1544 : : "The certificate is outside its validity period.\n"));
1545 : 0 : goto cleanup;
1546 : : }
1547 : : }
1548 : :
1549 : : /* 8. Subject Public Key Info */
1550 [ + + ]: 2135 : if (base_asym_algo != 0) {
1551 : 2134 : status = libspdm_asym_get_public_key_from_x509(base_asym_algo, cert, cert_size, &context);
1552 : : }
1553 [ + + ]: 2135 : if (pqc_asym_algo != 0) {
1554 : 1 : status = libspdm_pqc_asym_get_public_key_from_x509(pqc_asym_algo, cert, cert_size, &context);
1555 : : }
1556 [ - + ]: 2135 : if (!status) {
1557 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
1558 : : "The mandatory Subject Public Key Info field is not present.\n"));
1559 : 0 : goto cleanup;
1560 : : }
1561 : :
1562 : : /* 9. Key Usage
1563 : : * If this is a SET_CERTIFICATE operation and the endpoint uses the AliasCert model then the
1564 : : * check should be skipped as the SPDM specification does not specify the presence or absence
1565 : : * of the Device Certificate CA's keyUsage field. */
1566 [ + + + + ]: 2135 : if (!set_cert || (cert_model != SPDM_CERTIFICATE_INFO_CERT_MODEL_ALIAS_CERT)) {
1567 : 2109 : size_t value = 0;
1568 : :
1569 : 2109 : status = libspdm_x509_get_key_usage(cert, cert_size, &value);
1570 [ - + ]: 2109 : if (!status) {
1571 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "The mandatory Key Usage field is not present.\n"));
1572 : 0 : goto cleanup;
1573 : : } else {
1574 [ - + ]: 2109 : if (value == 0) {
1575 [ # # ]: 0 : if (cert_model != SPDM_CERTIFICATE_INFO_CERT_MODEL_GENERIC_CERT) {
1576 : 0 : status = false;
1577 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
1578 : : "The mandatory Key Usage field is not present.\n"));
1579 : 0 : goto cleanup;
1580 : : }
1581 : : } else {
1582 [ - + ]: 2109 : if ((LIBSPDM_CRYPTO_X509_KU_DIGITAL_SIGNATURE & value) == 0) {
1583 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
1584 : : "The Mandatory digital signature bit in Key Usage field is not set.\n"));
1585 : 0 : status = false;
1586 : 0 : goto cleanup;
1587 : : }
1588 : : }
1589 : : }
1590 : : }
1591 : :
1592 : : /* 10. Extended Key Usage */
1593 : 2135 : status = libspdm_verify_leaf_cert_spdm_eku(cert, cert_size, is_requester_cert);
1594 [ + + ]: 2135 : if (!status) {
1595 : 6 : goto cleanup;
1596 : : }
1597 : :
1598 [ + + + + ]: 2129 : if ((!set_cert) || (cert_model == SPDM_CERTIFICATE_INFO_CERT_MODEL_DEVICE_CERT)) {
1599 : : /* 11. verify spdm defined extension*/
1600 : 2091 : status = libspdm_verify_leaf_cert_spdm_extension(cert, cert_size,
1601 : : is_requester_cert, cert_model);
1602 [ + + ]: 2091 : if (!status) {
1603 : 10 : goto cleanup;
1604 : : }
1605 : : }
1606 : :
1607 : 2119 : cleanup:
1608 [ + + ]: 2589 : if (base_asym_algo != 0) {
1609 : 2318 : libspdm_asym_free(base_asym_algo, context);
1610 : : }
1611 [ + + ]: 2589 : if (pqc_asym_algo != 0) {
1612 : 271 : libspdm_pqc_asym_free(pqc_asym_algo, context);
1613 : : }
1614 : 2589 : return status;
1615 : : }
1616 : :
1617 : 2519 : bool libspdm_x509_certificate_check(
1618 : : uint8_t spdm_version,
1619 : : const uint8_t *cert, size_t cert_size,
1620 : : uint32_t base_asym_algo, uint32_t pqc_asym_algo, uint32_t base_hash_algo,
1621 : : bool is_requester, uint8_t cert_model)
1622 : : {
1623 : : bool status;
1624 : :
1625 : 2519 : status = libspdm_x509_common_certificate_check(
1626 : : cert, cert_size, base_asym_algo, pqc_asym_algo, is_requester, cert_model, false);
1627 [ + + ]: 2519 : if (!status) {
1628 : 472 : return false;
1629 : : }
1630 : :
1631 : 2047 : status = libspdm_verify_leaf_cert_basic_constraints(cert, cert_size);
1632 : 2047 : return status;
1633 : : }
1634 : :
1635 : 74 : bool libspdm_x509_set_cert_certificate_check(
1636 : : uint8_t spdm_version,
1637 : : const uint8_t *cert, size_t cert_size,
1638 : : uint32_t base_asym_algo, uint32_t pqc_asym_algo, uint32_t base_hash_algo,
1639 : : bool is_requester, uint8_t cert_model)
1640 : : {
1641 : : bool status;
1642 : :
1643 : 74 : status = libspdm_x509_common_certificate_check(
1644 : : cert, cert_size, base_asym_algo, pqc_asym_algo, is_requester, cert_model, true);
1645 [ + + ]: 74 : if (!status) {
1646 : 2 : return false;
1647 : : }
1648 : :
1649 : 72 : status = libspdm_verify_set_cert_leaf_cert_basic_constraints(cert, cert_size, cert_model);
1650 : :
1651 : 72 : return status;
1652 : : }
1653 : :
1654 : 2167 : bool libspdm_is_root_certificate(const uint8_t *cert, size_t cert_size)
1655 : : {
1656 : : uint8_t issuer_name[LIBSPDM_MAX_NAME_SIZE];
1657 : : size_t issuer_name_len;
1658 : : uint8_t subject_name[LIBSPDM_MAX_NAME_SIZE];
1659 : : size_t subject_name_len;
1660 : : bool result;
1661 : : uint8_t cert_basic_constraints[LIBSPDM_MAX_BASIC_CONSTRAINTS_CA_LEN];
1662 : : size_t cert_basic_constraints_len;
1663 : 2167 : const uint8_t basic_constraints_true_case[] = BASIC_CONSTRAINTS_CA_TRUE;
1664 : :
1665 [ + + - + ]: 2167 : if (cert == NULL || cert_size == 0) {
1666 : 2 : return false;
1667 : : }
1668 : :
1669 : : /* 1. issuer_name*/
1670 : 2165 : issuer_name_len = sizeof(issuer_name);
1671 : 2165 : result = libspdm_x509_get_issuer_name(cert, cert_size, issuer_name, &issuer_name_len);
1672 [ - + ]: 2165 : if (!result) {
1673 : 0 : return false;
1674 : : }
1675 : :
1676 : : /* 2. subject_name*/
1677 : 2165 : subject_name_len = sizeof(subject_name);
1678 : 2165 : result = libspdm_x509_get_subject_name(cert, cert_size, subject_name, &subject_name_len);
1679 [ - + ]: 2165 : if (!result) {
1680 : 0 : return false;
1681 : : }
1682 : :
1683 [ + + ]: 2165 : if (issuer_name_len != subject_name_len) {
1684 : 38 : return false;
1685 : : }
1686 [ - + ]: 2127 : if (!libspdm_consttime_is_mem_equal(issuer_name, subject_name, issuer_name_len)) {
1687 : 0 : return false;
1688 : : }
1689 : :
1690 : : /* 3. cA must be present in Basic Constraints */
1691 : 2127 : cert_basic_constraints_len = LIBSPDM_MAX_BASIC_CONSTRAINTS_CA_LEN;
1692 : 2127 : result = libspdm_x509_get_extended_basic_constraints(cert, cert_size,
1693 : : cert_basic_constraints,
1694 : : &cert_basic_constraints_len);
1695 [ - + ]: 2127 : if (!result) {
1696 : 0 : return false;
1697 : : }
1698 : :
1699 [ + - ]: 2127 : if ((cert_basic_constraints_len < sizeof(basic_constraints_true_case) ||
1700 [ - + ]: 2127 : (cert_basic_constraints[0] != basic_constraints_true_case[0]))) {
1701 : 0 : return false;
1702 : : }
1703 [ - + ]: 2127 : if (!libspdm_consttime_is_mem_equal(&cert_basic_constraints[2],
1704 : : &basic_constraints_true_case[2],
1705 : : sizeof(basic_constraints_true_case) - 2)) {
1706 : 0 : return false;
1707 : : }
1708 : :
1709 : : /* 4. certificate must be self-signed */
1710 : 2127 : result = libspdm_x509_verify_cert(cert, cert_size, cert, cert_size);
1711 [ + + ]: 2127 : if (!result) {
1712 : 2 : return false;
1713 : : }
1714 : :
1715 : 2125 : return true;
1716 : : }
1717 : :
1718 : 28 : bool libspdm_get_dmtf_subject_alt_name_from_bytes(
1719 : : uint8_t *buffer, size_t len, char *name_buffer,
1720 : : size_t *name_buffer_size, uint8_t *oid,
1721 : : size_t *oid_size)
1722 : : {
1723 : : uint8_t *ptr;
1724 : : int32_t length;
1725 : : size_t obj_len;
1726 : : int32_t ret;
1727 : :
1728 : : /*copy mem variable*/
1729 : : volatile uint8_t* dst;
1730 : : const volatile uint8_t* src;
1731 : : size_t dst_len;
1732 : : size_t src_len;
1733 : :
1734 : 28 : length = (int32_t)len;
1735 : 28 : ptr = buffer;
1736 : 28 : obj_len = 0;
1737 : :
1738 : : /* Sequence*/
1739 : 28 : ret = libspdm_asn1_get_tag(&ptr, ptr + length, &obj_len,
1740 : : LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
1741 [ + + ]: 28 : if (!ret) {
1742 : 2 : return false;
1743 : : }
1744 : :
1745 : 26 : ret = libspdm_asn1_get_tag(&ptr, ptr + obj_len, &obj_len,
1746 : : LIBSPDM_CRYPTO_ASN1_CONTEXT_SPECIFIC |
1747 : : LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
1748 : :
1749 : 26 : ret = libspdm_asn1_get_tag(&ptr, ptr + obj_len, &obj_len, LIBSPDM_CRYPTO_ASN1_OID);
1750 [ - + ]: 26 : if (!ret) {
1751 : 0 : return false;
1752 : : }
1753 : : /* CopyData to OID*/
1754 [ + + ]: 26 : if (*oid_size < (size_t)obj_len) {
1755 : 2 : *oid_size = (size_t)obj_len;
1756 : 2 : return false;
1757 : : }
1758 [ + - ]: 24 : if (oid != NULL) {
1759 : 24 : libspdm_copy_mem(oid, *oid_size, ptr, obj_len);
1760 : 24 : *oid_size = obj_len;
1761 : : }
1762 : :
1763 : : /* Move to next element*/
1764 : 24 : ptr += obj_len;
1765 : :
1766 : 24 : ret = libspdm_asn1_get_tag(&ptr, (uint8_t *)(buffer + length), &obj_len,
1767 : : LIBSPDM_CRYPTO_ASN1_CONTEXT_SPECIFIC |
1768 : : LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
1769 : 24 : ret = libspdm_asn1_get_tag(&ptr, (uint8_t *)(buffer + length), &obj_len,
1770 : : LIBSPDM_CRYPTO_ASN1_UTF8_STRING);
1771 [ - + ]: 24 : if (!ret) {
1772 : 0 : return false;
1773 : : }
1774 : :
1775 [ + + ]: 24 : if (*name_buffer_size < (size_t)obj_len + 1) {
1776 : 2 : *name_buffer_size = (size_t)obj_len + 1;
1777 : 2 : return false;
1778 : : }
1779 : :
1780 : : /* the src and dst address are overlap,
1781 : : * When the function is called by libspdm_get_dmtf_subject_alt_name.
1782 : : * libspdm_copy_mem can not be used. */
1783 [ + + + - ]: 22 : if ((name_buffer != NULL) && (ptr != NULL)) {
1784 : 20 : dst = (volatile uint8_t*) name_buffer;
1785 : 20 : src = (const volatile uint8_t*) ptr;
1786 : 20 : dst_len = *name_buffer_size;
1787 : 20 : src_len = obj_len;
1788 : :
1789 : : /* Check for case where "dst_len" may be invalid. Do not zero "dst" in this case. */
1790 [ - + ]: 20 : if (dst_len > (SIZE_MAX >> 1)) {
1791 : 0 : LIBSPDM_ASSERT(0);
1792 : 0 : return false;
1793 : : }
1794 : :
1795 : : /* Guard against invalid lengths. Zero "dst" in these cases. */
1796 [ + - ]: 20 : if (src_len > dst_len ||
1797 [ - + ]: 20 : src_len > (SIZE_MAX >> 1)) {
1798 : 0 : libspdm_zero_mem(name_buffer, dst_len);
1799 : 0 : LIBSPDM_ASSERT(0);
1800 : 0 : return false;
1801 : : }
1802 : :
1803 [ + + ]: 460 : while (src_len-- != 0) {
1804 : 440 : *(dst++) = *(src++);
1805 : : }
1806 : :
1807 : : /*encode name buffer to string*/
1808 : 20 : *name_buffer_size = obj_len + 1;
1809 : 20 : name_buffer[obj_len] = 0;
1810 : 20 : return true;
1811 : : }
1812 : :
1813 : 2 : return false;
1814 : : }
1815 : :
1816 : 18 : bool libspdm_get_dmtf_subject_alt_name(const uint8_t *cert, size_t cert_size,
1817 : : char *name_buffer,
1818 : : size_t *name_buffer_size,
1819 : : uint8_t *oid, size_t *oid_size)
1820 : : {
1821 : : bool status;
1822 : : size_t extension_data_size;
1823 : 18 : uint8_t oid_subject_alt_name[] = { 0x55, 0x1D, 0x11 };
1824 : :
1825 : 18 : extension_data_size = 0;
1826 : 18 : status = libspdm_x509_get_extension_data(cert, cert_size,
1827 : : oid_subject_alt_name,
1828 : : sizeof(oid_subject_alt_name), NULL,
1829 : : &extension_data_size);
1830 [ + + - + ]: 18 : if (status || (extension_data_size == 0)) {
1831 : 2 : *name_buffer_size = 0;
1832 : 2 : return false;
1833 : : }
1834 [ + + ]: 16 : if (extension_data_size > *name_buffer_size) {
1835 : 2 : *name_buffer_size = extension_data_size;
1836 : 2 : return false;
1837 : : }
1838 : : status =
1839 : 14 : libspdm_x509_get_extension_data(cert, cert_size,
1840 : : oid_subject_alt_name,
1841 : : sizeof(oid_subject_alt_name),
1842 : : (uint8_t *)name_buffer, name_buffer_size);
1843 [ - + ]: 14 : if (!status) {
1844 : 0 : return status;
1845 : : }
1846 : :
1847 : 14 : return libspdm_get_dmtf_subject_alt_name_from_bytes(
1848 : : (uint8_t *)name_buffer, *name_buffer_size, name_buffer,
1849 : : name_buffer_size, oid, oid_size);
1850 : : }
1851 : :
1852 : 1863 : bool libspdm_verify_cert_chain_data(
1853 : : uint8_t spdm_version,
1854 : : uint8_t *cert_chain_data, size_t cert_chain_data_size,
1855 : : uint32_t base_asym_algo, uint32_t pqc_asym_algo, uint32_t base_hash_algo,
1856 : : bool is_requester_cert, uint8_t cert_model)
1857 : : {
1858 : : const uint8_t *root_cert_buffer;
1859 : : size_t root_cert_buffer_size;
1860 : : const uint8_t *leaf_cert_buffer;
1861 : : size_t leaf_cert_buffer_size;
1862 : : size_t max_cert_chain_size;
1863 : :
1864 [ + + ]: 1863 : if (spdm_version >= SPDM_MESSAGE_VERSION_14) {
1865 : 1 : max_cert_chain_size = SPDM_MAX_CERTIFICATE_CHAIN_SIZE_14;
1866 : : } else {
1867 : 1862 : max_cert_chain_size = SPDM_MAX_CERTIFICATE_CHAIN_SIZE;
1868 : : }
1869 : 1863 : if (cert_chain_data_size >
1870 [ + + ]: 1863 : max_cert_chain_size - (sizeof(spdm_cert_chain_t) + LIBSPDM_MAX_HASH_SIZE)) {
1871 : 2 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
1872 : : "!!! VerifyCertificateChainData - FAIL (chain size too large) !!!\n"));
1873 : 2 : return false;
1874 : : }
1875 : :
1876 [ + + ]: 1861 : if (!libspdm_x509_get_cert_from_cert_chain(
1877 : : cert_chain_data, cert_chain_data_size, 0, &root_cert_buffer,
1878 : : &root_cert_buffer_size)) {
1879 : 2 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
1880 : : "!!! VerifyCertificateChainData - FAIL (get root certificate failed)!!!\n"));
1881 : 2 : return false;
1882 : : }
1883 : :
1884 [ + + ]: 1859 : if (!libspdm_x509_verify_cert_chain(root_cert_buffer, root_cert_buffer_size,
1885 : : cert_chain_data, cert_chain_data_size)) {
1886 : 4 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
1887 : : "!!! VerifyCertificateChainData - FAIL (cert chain verify failed)!!!\n"));
1888 : 4 : return false;
1889 : : }
1890 : :
1891 [ - + ]: 1855 : if (!libspdm_x509_get_cert_from_cert_chain(
1892 : : cert_chain_data, cert_chain_data_size, -1,
1893 : : &leaf_cert_buffer, &leaf_cert_buffer_size)) {
1894 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
1895 : : "!!! VerifyCertificateChainData - FAIL (get leaf certificate failed)!!!\n"));
1896 : 0 : return false;
1897 : : }
1898 : :
1899 [ + + ]: 1855 : if (!libspdm_x509_certificate_check(spdm_version,
1900 : : leaf_cert_buffer, leaf_cert_buffer_size,
1901 : : base_asym_algo, pqc_asym_algo, base_hash_algo,
1902 : : is_requester_cert, cert_model)) {
1903 : 2 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
1904 : : "!!! VerifyCertificateChainData - FAIL (leaf certificate check failed)!!!\n"));
1905 : 2 : return false;
1906 : : }
1907 : :
1908 : 1853 : return true;
1909 : : }
1910 : :
1911 : 142 : bool libspdm_verify_certificate_chain_buffer(
1912 : : uint8_t spdm_version,
1913 : : uint32_t base_hash_algo, uint32_t base_asym_algo, uint32_t pqc_asym_algo,
1914 : : const void *cert_chain_buffer,
1915 : : size_t cert_chain_buffer_size,
1916 : : bool is_requester_cert, uint8_t cert_model)
1917 : : {
1918 : : const uint8_t *cert_chain_data;
1919 : : size_t cert_chain_data_size;
1920 : : const uint8_t *first_cert_buffer;
1921 : : size_t first_cert_buffer_size;
1922 : : size_t hash_size;
1923 : : uint8_t calc_root_cert_hash[LIBSPDM_MAX_HASH_SIZE];
1924 : : const uint8_t *leaf_cert_buffer;
1925 : : size_t leaf_cert_buffer_size;
1926 : : bool result;
1927 : : const spdm_cert_chain_t *cert_chain_header;
1928 : :
1929 : 142 : hash_size = libspdm_get_hash_size(base_hash_algo);
1930 : :
1931 [ + + ]: 142 : if (cert_chain_buffer_size <= sizeof(spdm_cert_chain_t) + hash_size) {
1932 : 2 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
1933 : : "!!! VerifyCertificateChainBuffer - FAIL (buffer too small) !!!\n"));
1934 : 2 : return false;
1935 : : }
1936 : :
1937 : 140 : cert_chain_header = cert_chain_buffer;
1938 [ + + ]: 140 : if (libspdm_get_cert_chain_length(spdm_version, cert_chain_header) != cert_chain_buffer_size) {
1939 : 8 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
1940 : : "!!! VerifyCertificateChainBuffer - FAIL (cert_chain->length mismatch) !!!\n"));
1941 : 8 : return false;
1942 : : }
1943 : :
1944 : 132 : cert_chain_data = (const uint8_t *)cert_chain_buffer + sizeof(spdm_cert_chain_t) + hash_size;
1945 : 132 : cert_chain_data_size = cert_chain_buffer_size - sizeof(spdm_cert_chain_t) - hash_size;
1946 [ + + ]: 132 : if (!libspdm_x509_get_cert_from_cert_chain(
1947 : : cert_chain_data, cert_chain_data_size, 0, &first_cert_buffer,
1948 : : &first_cert_buffer_size)) {
1949 : 10 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
1950 : : "!!! VerifyCertificateChainBuffer - FAIL (get root certificate failed)!!!\n"));
1951 : 10 : return false;
1952 : : }
1953 : :
1954 [ + + ]: 122 : if (libspdm_is_root_certificate(first_cert_buffer, first_cert_buffer_size)) {
1955 : 116 : result = libspdm_hash_all(base_hash_algo, first_cert_buffer, first_cert_buffer_size,
1956 : : calc_root_cert_hash);
1957 [ - + ]: 116 : if (!result) {
1958 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
1959 : : "!!! VerifyCertificateChainBuffer - FAIL (hash calculation fail) !!!\n"));
1960 : 0 : return false;
1961 : : }
1962 [ + + ]: 116 : if (!libspdm_consttime_is_mem_equal((const uint8_t *)cert_chain_buffer +
1963 : : sizeof(spdm_cert_chain_t),
1964 : : calc_root_cert_hash, hash_size)) {
1965 : 4 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
1966 : : "!!! VerifyCertificateChainBuffer - FAIL (cert root hash mismatch) !!!\n"));
1967 : 4 : return false;
1968 : : }
1969 : 112 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
1970 : : "!!! VerifyCertificateChainBuffer - PASS (cert root hash match) !!!\n"));
1971 : : }
1972 : :
1973 : : /*If the number of certificates in the certificate chain is more than 1,
1974 : : * other certificates need to be verified.*/
1975 [ + - ]: 118 : if (cert_chain_data_size > first_cert_buffer_size) {
1976 [ + + ]: 118 : if (!libspdm_x509_verify_cert_chain(first_cert_buffer, first_cert_buffer_size,
1977 : : cert_chain_data + first_cert_buffer_size,
1978 : : cert_chain_data_size - first_cert_buffer_size)) {
1979 : 6 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
1980 : : "!!! VerifyCertificateChainBuffer - FAIL (cert chain verify failed)!!!\n"));
1981 : 6 : return false;
1982 : : }
1983 : : }
1984 : :
1985 [ - + ]: 112 : if (!libspdm_x509_get_cert_from_cert_chain(
1986 : : cert_chain_data, cert_chain_data_size, -1,
1987 : : &leaf_cert_buffer, &leaf_cert_buffer_size)) {
1988 : 0 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
1989 : : "!!! VerifyCertificateChainBuffer - FAIL (get leaf certificate failed)!!!\n"));
1990 : 0 : return false;
1991 : : }
1992 : :
1993 [ + + ]: 112 : if (!libspdm_x509_certificate_check(spdm_version,
1994 : : leaf_cert_buffer, leaf_cert_buffer_size,
1995 : : base_asym_algo, pqc_asym_algo, base_hash_algo,
1996 : : is_requester_cert, cert_model)) {
1997 : 6 : LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO,
1998 : : "!!! VerifyCertificateChainBuffer - FAIL (leaf certificate check failed)!!!\n"));
1999 : 6 : return false;
2000 : : }
2001 : :
2002 : 106 : return true;
2003 : : }
2004 : :
2005 : 356 : bool libspdm_get_leaf_cert_public_key_from_cert_chain(uint32_t base_hash_algo,
2006 : : uint32_t base_asym_alg,
2007 : : uint8_t *cert_chain_data,
2008 : : size_t cert_chain_data_size,
2009 : : void **public_key)
2010 : : {
2011 : : size_t hash_size;
2012 : : const uint8_t *cert_buffer;
2013 : : size_t cert_buffer_size;
2014 : : bool result;
2015 : :
2016 : 356 : hash_size = libspdm_get_hash_size(base_hash_algo);
2017 : :
2018 : 356 : cert_chain_data = cert_chain_data + sizeof(spdm_cert_chain_t) + hash_size;
2019 : 356 : cert_chain_data_size = cert_chain_data_size - (sizeof(spdm_cert_chain_t) + hash_size);
2020 : :
2021 : : /* Get leaf cert from cert chain */
2022 : 356 : result = libspdm_x509_get_cert_from_cert_chain(cert_chain_data,
2023 : : cert_chain_data_size, -1,
2024 : : &cert_buffer, &cert_buffer_size);
2025 [ + + ]: 356 : if (!result) {
2026 : 10 : return false;
2027 : : }
2028 : :
2029 : 346 : result = libspdm_asym_get_public_key_from_x509(
2030 : : base_asym_alg,
2031 : : cert_buffer, cert_buffer_size, public_key);
2032 [ - + ]: 346 : if (!result) {
2033 : 0 : return false;
2034 : : }
2035 : :
2036 : 346 : return true;
2037 : : }
2038 : :
2039 : 2 : bool libspdm_get_pqc_leaf_cert_public_key_from_cert_chain(uint32_t base_hash_algo,
2040 : : uint32_t pqc_asym_alg,
2041 : : uint8_t *cert_chain_data,
2042 : : size_t cert_chain_data_size,
2043 : : void **public_key)
2044 : : {
2045 : : size_t hash_size;
2046 : : const uint8_t *cert_buffer;
2047 : : size_t cert_buffer_size;
2048 : : bool result;
2049 : :
2050 : 2 : hash_size = libspdm_get_hash_size(base_hash_algo);
2051 : :
2052 : 2 : cert_chain_data = cert_chain_data + sizeof(spdm_cert_chain_t) + hash_size;
2053 : 2 : cert_chain_data_size = cert_chain_data_size - (sizeof(spdm_cert_chain_t) + hash_size);
2054 : :
2055 : : /* Get leaf cert from cert chain */
2056 : 2 : result = libspdm_x509_get_cert_from_cert_chain(cert_chain_data,
2057 : : cert_chain_data_size, -1,
2058 : : &cert_buffer, &cert_buffer_size);
2059 [ + - ]: 2 : if (!result) {
2060 : 2 : return false;
2061 : : }
2062 : :
2063 : 0 : result = libspdm_pqc_asym_get_public_key_from_x509(
2064 : : pqc_asym_alg,
2065 : : cert_buffer, cert_buffer_size, public_key);
2066 [ # # ]: 0 : if (!result) {
2067 : 0 : return false;
2068 : : }
2069 : :
2070 : 0 : return true;
2071 : : }
2072 : :
2073 : 80 : bool libspdm_verify_req_info(uint8_t *req_info, uint16_t req_info_len)
2074 : : {
2075 : : bool ret;
2076 : : uint8_t *ptr;
2077 : : int32_t length;
2078 : : size_t obj_len;
2079 : : uint8_t *end;
2080 : :
2081 : 80 : length = (int32_t)req_info_len;
2082 : 80 : ptr = req_info;
2083 : 80 : obj_len = 0;
2084 : 80 : end = ptr + length;
2085 : 80 : ret = true;
2086 : :
2087 [ + + ]: 80 : if (req_info_len == 0) {
2088 : 14 : return true;
2089 : : }
2090 : :
2091 : : /*req_info sequence*/
2092 : 66 : ret = libspdm_asn1_get_tag(&ptr, end, &obj_len,
2093 : : LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
2094 [ + + ]: 66 : if (!ret) {
2095 : 6 : return false;
2096 : : }
2097 : :
2098 : : /*integer:version*/
2099 : 60 : ret = libspdm_asn1_get_tag(&ptr, end, &obj_len, LIBSPDM_CRYPTO_ASN1_INTEGER);
2100 [ + + ]: 60 : if (!ret) {
2101 : 2 : return false;
2102 : : } else {
2103 : 58 : ptr += obj_len;
2104 : : }
2105 : :
2106 : : /*sequence:subject name*/
2107 : 58 : ret = libspdm_asn1_get_tag(&ptr, end, &obj_len,
2108 : : LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
2109 [ + + ]: 58 : if (!ret) {
2110 : 2 : return false;
2111 : : } else {
2112 : 56 : ptr += obj_len;
2113 : : }
2114 : :
2115 : : /*sequence:subject pkinfo*/
2116 : 56 : ret = libspdm_asn1_get_tag(&ptr, end, &obj_len,
2117 : : LIBSPDM_CRYPTO_ASN1_SEQUENCE | LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
2118 [ + + ]: 56 : if (!ret) {
2119 : 2 : return false;
2120 : : } else {
2121 : 54 : ptr += obj_len;
2122 : : }
2123 : :
2124 : : /*[0]: attributes*/
2125 : 54 : ret = libspdm_asn1_get_tag(&ptr, end, &obj_len,
2126 : : LIBSPDM_CRYPTO_ASN1_CONTEXT_SPECIFIC |
2127 : : LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
2128 : : /*req_info format error, don't have attributes tag*/
2129 [ + + ]: 54 : if (!ret) {
2130 : 2 : return false;
2131 : : }
2132 : :
2133 : : /*there is no attributes object*/
2134 [ + + ]: 52 : if (ptr == end) {
2135 : 2 : return true;
2136 : : }
2137 : :
2138 : : /*there is some attributes object: 0,1,2 ...*/
2139 [ + - ]: 98 : while (ret)
2140 : : {
2141 : 98 : ret = libspdm_asn1_get_tag(&ptr, end, &obj_len,
2142 : : LIBSPDM_CRYPTO_ASN1_SEQUENCE |
2143 : : LIBSPDM_CRYPTO_ASN1_CONSTRUCTED);
2144 [ + + ]: 98 : if (ret) {
2145 : 48 : ptr += obj_len;
2146 : : } else {
2147 : 50 : break;
2148 : : }
2149 : : }
2150 : :
2151 [ + + ]: 50 : if (ptr == end) {
2152 : 48 : return true;
2153 : : } else {
2154 : 2 : return false;
2155 : : }
2156 : : }
2157 : :
2158 : : #endif
|