Skip to content

Commit 946a48d

Browse files
embediverleongross
authored andcommitted
Implement < v1.3 compatibility for challenge command
1 parent 7659fdf commit 946a48d

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

src/commands/challenge/mod.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,29 @@ impl TryFrom<u8> for MeasurementSummaryHashType {
4343

4444
#[derive(FromBytes, IntoBytes, Immutable)]
4545
#[repr(C)]
46-
// TODO: check backwards compatibility of this struct with the original ChallengeReq struct
46+
/// CHALLENGE request message base
47+
///
48+
/// # Version specific fields for CHALLENGE:
49+
/// Following fields have to be appended, depending on the SPDM version.
50+
/// ## >= v1.3
51+
/// - `Context`: 8-byte application specific context.
52+
/// Should be all zeros if no context is provided.
4753
struct ChallengeReq {
54+
/// `Param1`: `SlotID`
55+
///
4856
/// Slot number of the Responder certificate chain that shall be used for authentication.
4957
/// If the public key of the Responder was provisioned to the Requester in a
5058
/// trusted environment, the value in this field shall be 0xFF ; otherwise it
5159
/// shall be between 0 and 7 inclusive.
5260
slot_id: u8,
5361

62+
/// `Param2`: Requested measurement summary hash
63+
///
5464
/// Shall be the type of measurement summary hash requested.
5565
measurement_hash_type: u8,
5666

5767
/// The Requester should choose a random value.
5868
nonce: [u8; NONCE_LEN],
59-
60-
/// The Requester can include application-specific information in Context.
61-
/// The Requester should fill this field with zeros if it has no context to provide.
62-
context: [u8; CONTEXT_LEN],
6369
}
6470
impl CommonCodec for ChallengeReq {}
6571

@@ -74,19 +80,15 @@ impl ChallengeReq {
7480
/// * `measurement_hash_type` - The type of measurement summary hash requested from the
7581
/// Responder.
7682
/// * `nonce` - A random 32-byte value chosen by the Requester for freshness.
77-
/// * `context` - Optional 8-byte application-specific context. Defaults to all zeros when
78-
/// `None`.
7983
pub fn new(
8084
slot_id: u8,
8185
measurement_hash_type: MeasurementSummaryHashType,
8286
nonce: [u8; NONCE_LEN],
83-
context: Option<[u8; CONTEXT_LEN]>,
8487
) -> Self {
8588
Self {
8689
slot_id,
8790
measurement_hash_type: measurement_hash_type as u8,
8891
nonce,
89-
context: context.unwrap_or([0; CONTEXT_LEN]),
9092
}
9193
}
9294
}

src/commands/challenge/request.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Licensed under the Apache-2.0 license
2-
use crate::codec::{Codec, MessageBuf};
2+
use crate::codec::{encode_u8_slice, Codec, MessageBuf};
33
use crate::commands::challenge::{
44
ChallengeAuthRspBase, ChallengeReq, MeasurementSummaryHashType, CONTEXT_LEN, NONCE_LEN,
55
OPAQUE_DATA_MAX,
@@ -25,7 +25,7 @@ use crate::transcript::TranscriptContext;
2525
/// Responder (`None`, `Tcb`, or `All`).
2626
/// * `nonce` - A 32-byte random value chosen by the Requester for freshness.
2727
/// * `context` - Optional 8-byte application-specific context. Defaults to all zeros when
28-
/// `None`.
28+
/// `None`, ignored for spdm versions < v1.3.
2929
///
3030
/// # Errors
3131
///
@@ -45,10 +45,20 @@ pub fn generate_challenge_request<'a>(
4545
.encode(message_buffer)
4646
.map_err(|e| (false, CommandError::Codec(e)))?;
4747

48-
ChallengeReq::new(slot_id, measurement_hash_type.clone(), nonce, context)
48+
ChallengeReq::new(slot_id, measurement_hash_type.clone(), nonce)
4949
.encode(message_buffer)
5050
.map_err(|e| (false, CommandError::Codec(e)))?;
5151

52+
// Encode 8-byte context string if version >= v1.3
53+
if ctx.connection_info().version_number() >= SpdmVersion::V13 {
54+
if let Some(ctx_str) = context {
55+
encode_u8_slice(&ctx_str, message_buffer)
56+
.map_err(|e| (true, CommandError::Codec(e)))?;
57+
} else {
58+
encode_u8_slice(&[0; 8], message_buffer).map_err(|e| (true, CommandError::Codec(e)))?;
59+
}
60+
}
61+
5262
ctx.state
5363
.peer_cert_store
5464
.as_mut()

0 commit comments

Comments
 (0)