Skip to content

Commit 8323916

Browse files
committed
Add 1.3 support for measurements
Signed-off-by: Jiewen Yao <[email protected]>
1 parent def8b31 commit 8323916

File tree

4 files changed

+224
-38
lines changed

4 files changed

+224
-38
lines changed

include/industry_standard/spdm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ typedef struct {
525525
uint8_t nonce[32];
526526
/* Below field is added in 1.1.*/
527527
uint8_t slot_id_param; /* BIT[0:3]=slot_id, BIT[4:7]=RSVD*/
528+
/*uint8_t requester_context[SPDM_REQ_CONTEXT_SIZE]; */
528529
} spdm_get_measurements_request_t;
529530

530531
#define SPDM_GET_MEASUREMENTS_REQUEST_SLOT_ID_MASK 0xF
@@ -617,6 +618,7 @@ typedef struct {
617618
* uint8_t nonce[32];
618619
* uint16_t opaque_length;
619620
* uint8_t opaque_data[opaque_length];
621+
* uint8_t requester_context[SPDM_REQ_CONTEXT_SIZE];
620622
* uint8_t signature[key_size];*/
621623
} spdm_measurements_response_t;
622624

include/library/spdm_requester_lib.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,48 @@ libspdm_return_t libspdm_get_measurement_ex(void *spdm_context, const uint32_t *
304304
void *opaque_data,
305305
size_t *opaque_data_size);
306306

307+
/**
308+
* This function sends GET_MEASUREMENT to get measurement from the device.
309+
*
310+
* If the signature is requested, this function verifies the signature of the measurement.
311+
*
312+
* @param spdm_context A pointer to the SPDM context.
313+
* @param session_id Indicates if it is a secured message protected via SPDM session.
314+
* If session_id is NULL, it is a normal message.
315+
* If session_id is NOT NULL, it is a secured message.
316+
* @param request_attribute The request attribute of the request message.
317+
* @param measurement_operation The measurement operation of the request message.
318+
* @param slot_id The number of slot for the certificate chain.
319+
* @param requester_context A buffer to hold the requester context (8 bytes) as input, if not NULL.
320+
* It is used only if the negotiated version >= 1.3.
321+
* @param content_changed The measurement content changed output param.
322+
* @param number_of_blocks The number of blocks of the measurement record.
323+
* @param measurement_record_length On input, indicate the size in bytes of the destination buffer to store the measurement record.
324+
* On output, indicate the size in bytes of the measurement record.
325+
* @param measurement_record A pointer to a destination buffer to store the measurement record.
326+
* @param requester_nonce_in A buffer to hold the requester nonce (32 bytes) as input, if not NULL.
327+
* @param requester_nonce A buffer to hold the requester nonce (32 bytes), if not NULL.
328+
* @param responder_nonce A buffer to hold the responder nonce (32 bytes), if not NULL.
329+
* @param opaque_data A buffer to hold the responder opaque data, if not NULL.
330+
* @param opaque_data_size On input, the size of the opaque data buffer.
331+
* Responder opaque data should be less than 1024 bytes.
332+
* On output, the size of the opaque data.
333+
**/
334+
libspdm_return_t libspdm_get_measurement_ex2(void *spdm_context, const uint32_t *session_id,
335+
uint8_t request_attribute,
336+
uint8_t measurement_operation,
337+
uint8_t slot_id,
338+
const void *requester_context,
339+
uint8_t *content_changed,
340+
uint8_t *number_of_blocks,
341+
uint32_t *measurement_record_length,
342+
void *measurement_record,
343+
const void *requester_nonce_in,
344+
void *requester_nonce,
345+
void *responder_nonce,
346+
void *opaque_data,
347+
size_t *opaque_data_size);
348+
307349
#if (LIBSPDM_ENABLE_CAPABILITY_KEY_EX_CAP) || (LIBSPDM_ENABLE_CAPABILITY_PSK_CAP)
308350
/**
309351
* This function sends KEY_EXCHANGE/FINISH or PSK_EXCHANGE/PSK_FINISH

library/spdm_requester_lib/libspdm_req_get_measurements.c

Lines changed: 153 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ bool libspdm_verify_measurement_signature(libspdm_context_t *spdm_context,
121121
* @param request_attribute The request attribute of the request message.
122122
* @param measurement_operation The measurement operation of the request message.
123123
* @param slot_id The number of slot for the certificate chain.
124+
* @param requester_context If not NULL, a buffer to hold the requester context (8 bytes).
125+
* It is used only if the negotiated version >= 1.3.
124126
* @param content_changed The measurement content changed output param.
125127
* @param number_of_blocks The number of blocks of the measurement record.
126128
* @param measurement_record_length On input, indicate the size in bytes of the destination buffer
@@ -137,6 +139,7 @@ static libspdm_return_t libspdm_try_get_measurement(libspdm_context_t *spdm_cont
137139
uint8_t request_attribute,
138140
uint8_t measurement_operation,
139141
uint8_t slot_id_param,
142+
const void *requester_context,
140143
uint8_t *content_changed,
141144
uint8_t *number_of_blocks,
142145
uint32_t *measurement_record_length,
@@ -267,6 +270,19 @@ static libspdm_return_t libspdm_try_get_measurement(libspdm_context_t *spdm_cont
267270
libspdm_zero_mem (requester_nonce, SPDM_NONCE_SIZE);
268271
}
269272
}
273+
if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_13) {
274+
if (requester_context == NULL) {
275+
libspdm_zero_mem((uint8_t *)spdm_request + spdm_request_size, SPDM_REQ_CONTEXT_SIZE);
276+
} else {
277+
libspdm_copy_mem((uint8_t *)spdm_request + spdm_request_size, SPDM_REQ_CONTEXT_SIZE,
278+
requester_context, SPDM_REQ_CONTEXT_SIZE);
279+
}
280+
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "RequesterContext - "));
281+
LIBSPDM_INTERNAL_DUMP_DATA((uint8_t *)spdm_request + spdm_request_size,
282+
SPDM_REQ_CONTEXT_SIZE);
283+
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
284+
spdm_request_size += SPDM_REQ_CONTEXT_SIZE;
285+
}
270286

271287
/* -=[Send Request Phase]=- */
272288
status = libspdm_send_spdm_request(spdm_context, session_id, spdm_request_size, spdm_request);
@@ -403,17 +419,53 @@ static libspdm_return_t libspdm_try_get_measurement(libspdm_context_t *spdm_cont
403419
}
404420
}
405421

406-
if (spdm_response_size <
407-
sizeof(spdm_measurements_response_t) +
408-
measurement_record_data_length + SPDM_NONCE_SIZE +
409-
sizeof(uint16_t) + opaque_length + signature_size) {
410-
status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
411-
goto receive_done;
422+
if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_13) {
423+
if (spdm_response_size <
424+
sizeof(spdm_measurements_response_t) +
425+
measurement_record_data_length + SPDM_NONCE_SIZE +
426+
sizeof(uint16_t) + opaque_length + SPDM_REQ_CONTEXT_SIZE + signature_size) {
427+
status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
428+
goto receive_done;
429+
}
430+
spdm_response_size = sizeof(spdm_measurements_response_t) +
431+
measurement_record_data_length +
432+
SPDM_NONCE_SIZE + sizeof(uint16_t) +
433+
opaque_length + SPDM_REQ_CONTEXT_SIZE + signature_size;
434+
} else {
435+
if (spdm_response_size <
436+
sizeof(spdm_measurements_response_t) +
437+
measurement_record_data_length + SPDM_NONCE_SIZE +
438+
sizeof(uint16_t) + opaque_length + signature_size) {
439+
status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
440+
goto receive_done;
441+
}
442+
spdm_response_size = sizeof(spdm_measurements_response_t) +
443+
measurement_record_data_length +
444+
SPDM_NONCE_SIZE + sizeof(uint16_t) +
445+
opaque_length + signature_size;
446+
}
447+
448+
if ((opaque_data != NULL) && (opaque_data_size != NULL)) {
449+
if (opaque_length >= *opaque_data_size) {
450+
status = LIBSPDM_STATUS_BUFFER_TOO_SMALL;
451+
goto receive_done;
452+
}
453+
libspdm_copy_mem(opaque_data, *opaque_data_size, ptr, opaque_length);
454+
*opaque_data_size = opaque_length;
455+
}
456+
ptr += opaque_length;
457+
if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_13) {
458+
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "RequesterContext - "));
459+
LIBSPDM_INTERNAL_DUMP_DATA(ptr, SPDM_REQ_CONTEXT_SIZE);
460+
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
461+
if (!libspdm_consttime_is_mem_equal((uint8_t *)spdm_request +
462+
spdm_request_size - SPDM_REQ_CONTEXT_SIZE,
463+
ptr, SPDM_REQ_CONTEXT_SIZE)) {
464+
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
465+
goto receive_done;
466+
}
467+
ptr += SPDM_REQ_CONTEXT_SIZE;
412468
}
413-
spdm_response_size = sizeof(spdm_measurements_response_t) +
414-
measurement_record_data_length +
415-
SPDM_NONCE_SIZE + sizeof(uint16_t) +
416-
opaque_length + signature_size;
417469

418470
/* -=[Process Response Phase]=- */
419471
status = libspdm_append_message_m(spdm_context, session_info, spdm_request,
@@ -428,16 +480,6 @@ static libspdm_return_t libspdm_try_get_measurement(libspdm_context_t *spdm_cont
428480
goto receive_done;
429481
}
430482

431-
if ((opaque_data != NULL) && (opaque_data_size != NULL)) {
432-
if (opaque_length >= *opaque_data_size) {
433-
status = LIBSPDM_STATUS_BUFFER_TOO_SMALL;
434-
goto receive_done;
435-
}
436-
libspdm_copy_mem(opaque_data, *opaque_data_size, ptr, opaque_length);
437-
*opaque_data_size = opaque_length;
438-
}
439-
ptr += opaque_length;
440-
441483
signature = ptr;
442484
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "signature (0x%x):\n", signature_size));
443485
LIBSPDM_INTERNAL_DUMP_HEX(signature, signature_size);
@@ -482,17 +524,53 @@ static libspdm_return_t libspdm_try_get_measurement(libspdm_context_t *spdm_cont
482524
}
483525
}
484526

485-
if (spdm_response_size <
486-
sizeof(spdm_measurements_response_t) +
487-
measurement_record_data_length + SPDM_NONCE_SIZE +
488-
sizeof(uint16_t) + opaque_length) {
489-
status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
490-
goto receive_done;
527+
if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_13) {
528+
if (spdm_response_size <
529+
sizeof(spdm_measurements_response_t) +
530+
measurement_record_data_length + SPDM_NONCE_SIZE +
531+
sizeof(uint16_t) + opaque_length + SPDM_REQ_CONTEXT_SIZE) {
532+
status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
533+
goto receive_done;
534+
}
535+
spdm_response_size = sizeof(spdm_measurements_response_t) +
536+
measurement_record_data_length +
537+
SPDM_NONCE_SIZE + sizeof(uint16_t) +
538+
opaque_length + SPDM_REQ_CONTEXT_SIZE;
539+
} else {
540+
if (spdm_response_size <
541+
sizeof(spdm_measurements_response_t) +
542+
measurement_record_data_length + SPDM_NONCE_SIZE +
543+
sizeof(uint16_t) + opaque_length) {
544+
status = LIBSPDM_STATUS_INVALID_MSG_SIZE;
545+
goto receive_done;
546+
}
547+
spdm_response_size = sizeof(spdm_measurements_response_t) +
548+
measurement_record_data_length +
549+
SPDM_NONCE_SIZE + sizeof(uint16_t) +
550+
opaque_length;
551+
}
552+
553+
if ((opaque_data != NULL) && (opaque_data_size != NULL)) {
554+
if (opaque_length >= *opaque_data_size) {
555+
status = LIBSPDM_STATUS_BUFFER_TOO_SMALL;
556+
goto receive_done;
557+
}
558+
libspdm_copy_mem(opaque_data, *opaque_data_size, ptr, opaque_length);
559+
*opaque_data_size = opaque_length;
560+
}
561+
ptr += opaque_length;
562+
if (spdm_request->header.spdm_version >= SPDM_MESSAGE_VERSION_13) {
563+
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "RequesterContext - "));
564+
LIBSPDM_INTERNAL_DUMP_DATA(ptr, SPDM_REQ_CONTEXT_SIZE);
565+
LIBSPDM_DEBUG((LIBSPDM_DEBUG_INFO, "\n"));
566+
if (!libspdm_consttime_is_mem_equal((uint8_t *)spdm_request +
567+
spdm_request_size - SPDM_REQ_CONTEXT_SIZE,
568+
ptr, SPDM_REQ_CONTEXT_SIZE)) {
569+
status = LIBSPDM_STATUS_INVALID_MSG_FIELD;
570+
goto receive_done;
571+
}
572+
ptr += SPDM_REQ_CONTEXT_SIZE;
491573
}
492-
spdm_response_size = sizeof(spdm_measurements_response_t) +
493-
measurement_record_data_length +
494-
SPDM_NONCE_SIZE + sizeof(uint16_t) +
495-
opaque_length;
496574

497575
/* If a signature is not requested then content_changed must be 0. */
498576
if (spdm_response->header.spdm_version >= SPDM_MESSAGE_VERSION_12) {
@@ -639,7 +717,7 @@ libspdm_return_t libspdm_get_measurement(void *spdm_context, const uint32_t *ses
639717
do {
640718
status = libspdm_try_get_measurement(
641719
context, session_id, request_attribute,
642-
measurement_operation, slot_id_param, content_changed, number_of_blocks,
720+
measurement_operation, slot_id_param, NULL, content_changed, number_of_blocks,
643721
measurement_record_length, measurement_record,
644722
NULL, NULL, NULL, NULL, NULL);
645723
if ((status != LIBSPDM_STATUS_BUSY_PEER) || (retry == 0)) {
@@ -678,7 +756,50 @@ libspdm_return_t libspdm_get_measurement_ex(void *spdm_context, const uint32_t *
678756
do {
679757
status = libspdm_try_get_measurement(
680758
context, session_id, request_attribute,
681-
measurement_operation, slot_id_param, content_changed, number_of_blocks,
759+
measurement_operation, slot_id_param, NULL, content_changed, number_of_blocks,
760+
measurement_record_length, measurement_record,
761+
requester_nonce_in,
762+
requester_nonce, responder_nonce,
763+
opaque_data, opaque_data_size);
764+
if ((status != LIBSPDM_STATUS_BUSY_PEER) || (retry == 0)) {
765+
return status;
766+
}
767+
768+
libspdm_sleep(retry_delay_time);
769+
} while (retry-- != 0);
770+
771+
return status;
772+
}
773+
774+
libspdm_return_t libspdm_get_measurement_ex2(void *spdm_context, const uint32_t *session_id,
775+
uint8_t request_attribute,
776+
uint8_t measurement_operation,
777+
uint8_t slot_id_param,
778+
const void *requester_context,
779+
uint8_t *content_changed,
780+
uint8_t *number_of_blocks,
781+
uint32_t *measurement_record_length,
782+
void *measurement_record,
783+
const void *requester_nonce_in,
784+
void *requester_nonce,
785+
void *responder_nonce,
786+
void *opaque_data,
787+
size_t *opaque_data_size)
788+
{
789+
libspdm_context_t *context;
790+
size_t retry;
791+
uint64_t retry_delay_time;
792+
libspdm_return_t status;
793+
794+
context = spdm_context;
795+
context->crypto_request = true;
796+
retry = context->retry_times;
797+
retry_delay_time = context->retry_delay_time;
798+
do {
799+
status = libspdm_try_get_measurement(
800+
context, session_id, request_attribute,
801+
measurement_operation, slot_id_param, requester_context,
802+
content_changed, number_of_blocks,
682803
measurement_record_length, measurement_record,
683804
requester_nonce_in,
684805
requester_nonce, responder_nonce,

0 commit comments

Comments
 (0)