Skip to content

Commit

Permalink
Merge pull request #38 from Nitrokey/change-puk
Browse files Browse the repository at this point in the history
Fix changing PUK
  • Loading branch information
sosthene-nitrokey committed Dec 13, 2023
2 parents caed9e4 + c0efb7b commit 7b6942c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ impl Persistent {
) -> bool {
let old_puk = Bytes::from_slice(&old_value.0).expect("Convertion of static array");
let new_puk = Bytes::from_slice(&new_value.0).expect("Convertion of static array");
try_syscall!(client.change_pin(PinType::UserPin, old_puk, new_puk))
try_syscall!(client.change_pin(PinType::Puk, old_puk, new_puk))
.map(|r| r.success)
.unwrap_or(false)
}
Expand Down
19 changes: 18 additions & 1 deletion tests/command_response.ron
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,22 @@
output: Data("53 3b 3019d4e739d821086c1084210d8360d8210842108421804210c3f33410B0BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB30839393939313233313e00fe00"),
),
]
)
),
IoTest(
name: "Pin and Puk",
uuid_config: WithBoth("00112233445566778899AABBCCDDEEFF"),
cmd_resp: [
ChangePin(
new: "01020304FFFFFFFF",
),
ChangePuk(
new: "0102030405060708",
),
VerifyApplicationPin(pin: "0102030405060708", expected_status: RemainingRetries(2)),
ChangePuk(
old: "0102030405060708",
new: "AABBCCDDEEFF0011",
),
]
),
]
50 changes: 49 additions & 1 deletion tests/command_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ fn default_app_pin() -> String {
"313233343536FFFF".into()
}

fn default_puk() -> String {
"3132333435363738".into()
}

#[derive(Deserialize, Debug)]
#[serde(deny_unknown_fields)]
enum IoCmd {
Expand Down Expand Up @@ -303,6 +307,20 @@ enum IoCmd {
#[serde(default)]
expected_status_response: Status,
},
ChangePin {
#[serde(default = "default_app_pin")]
old: String,
new: String,
#[serde(default)]
expected_status: Status,
},
ChangePuk {
#[serde(default = "default_puk")]
old: String,
new: String,
#[serde(default)]
expected_status: Status,
},
Select,
Reset {
#[serde(default)]
Expand All @@ -315,6 +333,7 @@ const MATCH_ANY: OutputMatcher = OutputMatcher::All(Cow::Borrowed(&[]), ());

impl IoCmd {
fn run(&self, card: &mut setup::Piv) {
println!("Running {self:?}");
match self {
Self::IoData {
input,
Expand Down Expand Up @@ -354,6 +373,16 @@ impl IoCmd {
key,
expected_status,
} => Self::run_set_administration_key(key.algorithm, &key.key, *expected_status, card),
Self::ChangePin {
old,
new,
expected_status,
} => Self::run_change_pin(old, new, *expected_status, card),
Self::ChangePuk {
old,
new,
expected_status,
} => Self::run_change_puk(old, new, *expected_status, card),
Self::Select => Self::run_select(card),
Self::Reset { expected_status } => Self::run_reset(*expected_status, card),
}
Expand Down Expand Up @@ -405,7 +434,7 @@ impl IoCmd {
panic!("Bad output. Expected {output:02x?}");
}
if status != expected_status {
panic!("Bad status. Expected {expected_status:?}");
panic!("Bad status. Expected {expected_status:?}, got {status:?}");
}
rep
}
Expand Down Expand Up @@ -534,6 +563,25 @@ impl IoCmd {
fn run_reset(expected_status: Status, card: &mut setup::Piv) {
Self::run_bytes(&hex!("00 FB 00 00"), &MATCH_EMPTY, expected_status, card);
}

fn run_change_pin(old: &str, new: &str, status: Status, card: &mut setup::Piv) {
let command = parse_hex(&format!("{old}{new}"));
Self::run_bytes(
&build_command(0, 0x24, 0x00, 0x80, &command, 0x00),
&MATCH_EMPTY,
status,
card,
);
}
fn run_change_puk(old: &str, new: &str, status: Status, card: &mut setup::Piv) {
let command = parse_hex(&format!("{old}{new}"));
Self::run_bytes(
&build_command(0, 0x24, 0x00, 0x81, &command, 0x00),
&MATCH_EMPTY,
status,
card,
);
}
}

#[derive(Deserialize, Debug, PartialEq, Clone)]
Expand Down

0 comments on commit 7b6942c

Please sign in to comment.