Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion SE050Sim/sdk-test/run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ fi

# Platform SCP03 variant: provision the simulator and the SDK from the same
# key file so the two sides cannot drift.
if [ "$SE05X_AUTH" = "PlatfSCP03" ]; then
case "$SE05X_AUTH" in *PlatfSCP03) PLATFSCP=1 ;; *) PLATFSCP=0 ;; esac
if [ "$PLATFSCP" = 1 ]; then
echo "=== Platform SCP03 variant: provisioning keys from $KEYFILE ==="
export SE050_SIM_SCP03_ENC=$(awk '/^ENC /{print $2}' "$KEYFILE")
export SE050_SIM_SCP03_MAC=$(awk '/^MAC /{print $2}' "$KEYFILE")
Expand All @@ -25,6 +26,15 @@ if [ "$SE05X_AUTH" = "PlatfSCP03" ]; then
echo " simulator MAC=$SE050_SIM_SCP03_MAC"
fi

# Applet session variants: the SDK opens a UserID / AESKey / ECKey session with
# its demo Authentication Objects (ex_sss_auth.h); the simulator provisions them.
case "$SE05X_AUTH" in
UserID*|AESKey*|ECKey*)
echo "=== Applet session variant ($SE05X_AUTH): demo Authentication Objects ==="
export SE050_SIM_DEMO_AUTH=1
;;
esac

start_sim() {
/app/se050-sim-server &
SIM_PID=$!
Expand Down
882 changes: 882 additions & 0 deletions SE050Sim/se050-sim/src/applet_session.rs

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions SE050Sim/se050-sim/src/handlers/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,18 @@ pub fn handle_write_aes_key(apdu: &ParsedApdu, store: &mut ObjectStore) -> ApduR
creation_policy,
if generated { 0x02 } else { 0x01 },
);
// INS_AUTH_OBJECT: an AESKey Authentication Object (AES-128 only).
if apdu.ins & 0x40 != 0 {
if !matches!(store.get(&obj_id), Some(SecureObject::AESKey { key }) if key.len() == 16) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 922a22d: the key length is computed before anything is stored, and an Authentication Object, new or existing, only takes a 16-byte key. Otherwise 0x6A80 and the old key stays. Covered by aes_authentication_object_policies_and_key_length.

store.remove(&obj_id);
return ApduResponse::error(SW_WRONG_DATA);
}
let max = tlv::find_tlv(&tlvs, crate::tlv::TAG_MAX_ATTEMPTS)
.filter(|t| t.value.len() == 2)
.map(|t| u16::from_be_bytes([t.value[0], t.value[1]]) & 0x7FFF)
.unwrap_or(0);
store.set_auth_object(obj_id, max);
}
}
response
}
Expand Down
9 changes: 9 additions & 0 deletions SE050Sim/se050-sim/src/handlers/ec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ pub fn handle_write_ec_key(
creation_policy,
if generated { 0x02 } else { 0x01 },
);
// INS_AUTH_OBJECT: an ECKey Authentication Object, with its maximum
// number of failed attempts (0 = unlimited, at most 0x7FFF).
if apdu.ins & 0x40 != 0 {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 6149ca8: ECKey Authentication Objects must be NIST P-256, checked before key import or generation, and an existing one cannot be rewritten on another curve. Covered by authentication_object_parameters_are_validated.

let max = tlv::find_tlv(&tlvs, crate::tlv::TAG_MAX_ATTEMPTS)
.filter(|t| t.value.len() == 2)
.map(|t| u16::from_be_bytes([t.value[0], t.value[1]]) & 0x7FFF)
.unwrap_or(0);
store.set_auth_object(obj_id, max);
}
}
response
}
Expand Down
12 changes: 12 additions & 0 deletions SE050Sim/se050-sim/src/handlers/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ pub fn handle_oneshot(apdu: &ParsedApdu, store: &mut ObjectStore, validate: bool
Some(obj) => obj.clone(),
None => return ApduResponse::error(SW_CONDITIONS_NOT_SATISFIED),
};
// Generating a MAC needs POLICY_OBJ_ALLOW_SIGN for the current session
// owner; validating needs POLICY_OBJ_ALLOW_VERIFY (AN12543 policy table).
let needed = if validate { 0x0800_0000 } else { crate::policy::POLICY_OBJ_ALLOW_SIGN };
if !store.policy_allows(&key_id, needed) {
return ApduResponse::error(SW_COMMAND_NOT_ALLOWED);
}
match compute_mac(algo, &key_obj, &data) {
Ok(mac) => mac_response(mac, expected.as_deref()),
Err(sw) => ApduResponse::error(sw),
Expand Down Expand Up @@ -260,6 +266,12 @@ pub fn handle_final(apdu: &ParsedApdu, store: &mut ObjectStore) -> ApduResponse
Some(obj) => obj.clone(),
None => return ApduResponse::error(SW_CONDITIONS_NOT_SATISFIED),
};
// Generating a MAC needs POLICY_OBJ_ALLOW_SIGN for the current session
// owner; validating needs POLICY_OBJ_ALLOW_VERIFY (AN12543 policy table).
let needed = if validate { 0x0800_0000 } else { crate::policy::POLICY_OBJ_ALLOW_SIGN };
if !store.policy_allows(&key_id, needed) {
return ApduResponse::error(SW_COMMAND_NOT_ALLOWED);
}
match compute_mac(algo, &key_obj, &data) {
Ok(mac) => mac_response(mac, expected.as_deref()),
Err(sw) => ApduResponse::error(sw),
Expand Down
80 changes: 68 additions & 12 deletions SE050Sim/se050-sim/src/handlers/object_mgmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,53 @@ use crate::object_store::types::SecureObject;
use crate::object_store::ObjectStore;
use crate::tlv::{self, Tlv, TAG_1, TAG_2, TAG_3, TAG_4, TAG_POLICY};

/// WriteUserID (AN12543 4.7.1.5): policy and maximum attempts (< 256) only on
/// creation, TAG_1 object ID, TAG_2 value of 4 to 16 bytes. An existing UserID
/// cannot be updated.
fn handle_write_userid(apdu: &ParsedApdu, store: &mut ObjectStore) -> ApduResponse {
let Ok(tlvs) = apdu.parse_tlvs() else {
return ApduResponse::error(SW_WRONG_DATA);
};
let Some(obj_id) = extract_object_id(&tlvs) else {
return ApduResponse::error(SW_WRONG_DATA);
};
let Some(value) = tlv::find_tlv(&tlvs, TAG_2).map(|t| t.value.clone()) else {
return ApduResponse::error(SW_WRONG_DATA);
};
if !(4..=16).contains(&value.len()) {
return ApduResponse::error(SW_WRONG_DATA);
}
if store.exists(&obj_id) {
return ApduResponse::error(SW_CONDITIONS_NOT_SATISFIED);
}
let max = tlv::find_tlv(&tlvs, crate::tlv::TAG_MAX_ATTEMPTS)
.filter(|t| t.value.len() == 2)
.map(|t| u16::from_be_bytes([t.value[0], t.value[1]]))
.unwrap_or(0);
if max > 255 {
return ApduResponse::error(SW_WRONG_DATA);
}
let policy = match crate::policy::creation_policy(&tlvs) {
Ok(p) => p,
Err(_) => return ApduResponse::error(SW_WRONG_DATA),
};
store.insert(obj_id, SecureObject::UserID { value });
store.set_creation_metadata(obj_id, policy, 0x01);
store.set_auth_object(obj_id, max);
ApduResponse::success()
}

/// Handle WRITE commands for Binary, UserID, and Counter objects.
pub fn handle_write(apdu: &ParsedApdu, store: &mut ObjectStore) -> ApduResponse {
match apdu.cred_type() {
P1_BINARY => handle_write_binary(apdu, store),
// WriteUserID is refused in plain (unauthenticated) sessions:
// bench-verified 0x6985 on SE050C applet 3.1.1 and SE051
// applet 7.2.0 alike, via both the raw APDU and the sss layer.
// The simulator only models plain sessions, so the write is
// always refused.
// applet 7.2.0 alike, via both the raw APDU and the sss layer
// (the transport refuses Authentication Object writes outside an
// authenticated channel, see applet_session::intercept). UserIDs
// exist only as Authentication Objects.
P1_USERID if apdu.ins & 0x40 != 0 => handle_write_userid(apdu, store),
P1_USERID => ApduResponse::error(SW_CONDITIONS_NOT_SATISFIED),
P1_COUNTER => handle_write_counter(apdu, store),
_ => ApduResponse::error(SW_WRONG_P1P2),
Expand Down Expand Up @@ -433,17 +471,36 @@ fn handle_read_attributes(
Some(id) => id,
None => return ApduResponse::error(SW_WRONG_DATA),
};
let Some(object) = store.get(&obj_id) else {
return ApduResponse::error(SW_CONDITIONS_NOT_SATISFIED);
};
match object_attributes(store, &obj_id) {
Some(attributes) => ApduResponse::success_with_tlvs(&[Tlv::new(TAG_3, &attributes)]),
None => ApduResponse::error(SW_CONDITIONS_NOT_SATISFIED),
}
}

/// Attribute bytes of an object (AN12543 tables 3 and 4), for
/// ReadObjectAttributes and attested reads.
pub fn object_attributes(store: &ObjectStore, obj_id: &[u8; 4]) -> Option<Vec<u8>> {
let obj_id = *obj_id;
let object = store.get(&obj_id)?;

let mut attributes = Vec::new();
attributes.extend_from_slice(&obj_id);
attributes.push(object.type_code(true));
attributes.push(0x01); // kSE05x_SetIndicator_NOT_SET
attributes.extend_from_slice(&[0x00, 0x00]); // minimum AEAD tag length
attributes.extend_from_slice(&[0x00; 4]); // owner: unauthenticated session
attributes.extend_from_slice(&[0x00, 0x00]); // RFU
if let Some(auth) = store.auth_state(&obj_id) {
// Authentication Object layout (AN12543 table 3): indicator SET,
// failed attempts, owner, maximum attempts.
attributes.push(0x02); // kSE05x_SetIndicator_SET
// UserID attempts are counted but not reported (AN12543 3.2.4.4).
let shown = if matches!(object, SecureObject::UserID { .. }) { 0 } else { auth.attempts };
attributes.extend_from_slice(&shown.to_be_bytes());
attributes.extend_from_slice(&[0x00; 4]);
attributes.extend_from_slice(&auth.max_attempts.to_be_bytes());
} else {
attributes.push(0x01); // kSE05x_SetIndicator_NOT_SET
attributes.extend_from_slice(&[0x00, 0x00]); // minimum AEAD tag length
attributes.extend_from_slice(&[0x00; 4]); // owner: unauthenticated session
attributes.extend_from_slice(&[0x00, 0x00]); // RFU
}
if let Some(metadata) = store.metadata(&obj_id) {
if let Some(policy) = &metadata.policy {
attributes.extend_from_slice(policy);
Expand All @@ -453,8 +510,7 @@ fn handle_read_attributes(
attributes.push(0x01); // legacy store: treat as externally created
}
attributes.extend_from_slice(&[0x00; 4]); // object version

ApduResponse::success_with_tlvs(&[Tlv::new(TAG_3, &attributes)])
Some(attributes)
}

fn handle_check_exists(apdu: &ParsedApdu, store: &mut ObjectStore) -> ApduResponse {
Expand Down
1 change: 1 addition & 0 deletions SE050Sim/se050-sim/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

pub mod apdu;
pub mod applet;
pub mod applet_session;
pub mod dispatch;
pub mod handlers;
pub mod object_store;
Expand Down
Loading