Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increment the sign counter on signature #78

Merged
merged 2 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ SPDX-License-Identifier: CC0-1.0
### Bugfixes

- Fix the length of the Digital signature counter DO 0x93 ([#76][])
- PSO:CDS: Increment the signature counter ([#78][])

[#63]: https://github.com/Nitrokey/opcard-rs/pull/63
[#76]: https://github.com/Nitrokey/opcard-rs/pull/76
[#78]: https://github.com/Nitrokey/opcard-rs/pull/78

## v0.1.0 (2022-10-12)

Expand Down
7 changes: 7 additions & 0 deletions src/command/pso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ pub fn sign<const R: usize, T: trussed::Client>(
if !ctx.state.internal.pw1_valid_multiple() {
ctx.state.runtime.sign_verified = false;
}
ctx.state
.internal
.increment_sign_count(ctx.backend.client_mut())
.map_err(|_err| {
error!("Failed to increment sign count");
Status::UnspecifiedPersistentExecutionError
})?;

match ctx.state.internal.sign_alg() {
SignatureAlgorithm::Ed255 => sign_ec(ctx, key_id, Mechanism::Ed255),
Expand Down
9 changes: 9 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,15 @@ impl Internal {
self.sign_count
}

pub fn increment_sign_count(&mut self, client: &mut impl trussed::Client) -> Result<(), Error> {
self.sign_count += 1;
// Sign count is returned on 3 bytes
if self.sign_count & 0xffffff == 0 {
self.sign_count = 0xffffff;
}
self.save(client)
}

pub fn key_id(&self, ty: KeyType) -> Option<KeyId> {
match ty {
KeyType::Sign => self.signing_key,
Expand Down
8 changes: 4 additions & 4 deletions tests/crypto-gpg-import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ fn gpg_255() {
],
&[
vec![r"\[GNUPG:\] CARDCTRL \d D2760001240103040000000000000000"],
virt::gpg_status(virt::KeyType::Cv25519NoAut),
virt::gpg_status(virt::KeyType::Cv25519NoAut, 1),
vec![
r"\[GNUPG:\] GET_LINE cardedit.prompt",
r"\[GNUPG:\] GET_LINE cardedit.prompt",
Expand All @@ -277,7 +277,7 @@ fn gpg_255() {
r"\[GNUPG:\] GET_LINE cardedit.prompt",
],
virt::gpg_inquire_pin(),
virt::gpg_status(virt::KeyType::RsaNone),
virt::gpg_status(virt::KeyType::RsaNone, 0),
vec![r"\[GNUPG:\] GET_LINE cardedit.prompt"],
]
.into_iter()
Expand Down Expand Up @@ -546,7 +546,7 @@ fn gpg_p256() {
],
&[
vec![r"\[GNUPG:\] CARDCTRL \d D2760001240103040000000000000000"],
virt::gpg_status(virt::KeyType::P256NoAut),
virt::gpg_status(virt::KeyType::P256NoAut, 1),
vec![
r"\[GNUPG:\] GET_LINE cardedit.prompt",
r"\[GNUPG:\] GET_LINE cardedit.prompt",
Expand All @@ -555,7 +555,7 @@ fn gpg_p256() {
r"\[GNUPG:\] GET_LINE cardedit.prompt",
],
virt::gpg_inquire_pin(),
virt::gpg_status(virt::KeyType::RsaNone),
virt::gpg_status(virt::KeyType::RsaNone, 0),
vec![r"\[GNUPG:\] GET_LINE cardedit.prompt"],
]
.into_iter()
Expand Down
12 changes: 6 additions & 6 deletions tests/crypto-gpg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn gpg_255() {
],
&[
vec![r"\[GNUPG:\] CARDCTRL \d D2760001240103040000000000000000"],
virt::gpg_status(virt::KeyType::RsaNone,),
virt::gpg_status(virt::KeyType::RsaNone,0),
vec![
r"\[GNUPG:\] GET_LINE cardedit.prompt",
r"\[GNUPG:\] GET_LINE cardedit.prompt",
Expand Down Expand Up @@ -249,7 +249,7 @@ fn gpg_255() {
],
&[
vec![r"\[GNUPG:\] CARDCTRL \d D2760001240103040000000000000000"],
virt::gpg_status(virt::KeyType::Cv25519),
virt::gpg_status(virt::KeyType::Cv25519, 5),
vec![
r"\[GNUPG:\] GET_LINE cardedit.prompt",
r"\[GNUPG:\] GET_LINE cardedit.prompt",
Expand All @@ -258,7 +258,7 @@ fn gpg_255() {
r"\[GNUPG:\] GET_LINE cardedit.prompt",
],
virt::gpg_inquire_pin(),
virt::gpg_status(virt::KeyType::RsaNone),
virt::gpg_status(virt::KeyType::RsaNone, 0),
vec![r"\[GNUPG:\] GET_LINE cardedit.prompt"],
]
.into_iter()
Expand Down Expand Up @@ -328,7 +328,7 @@ fn gpg_p256() {
],
&[
vec![r"\[GNUPG:\] CARDCTRL \d D2760001240103040000000000000000"],
virt::gpg_status(virt::KeyType::RsaNone,),
virt::gpg_status(virt::KeyType::RsaNone,0),
vec![
r"\[GNUPG:\] GET_LINE cardedit.prompt",
r"\[GNUPG:\] GET_LINE cardedit.prompt",
Expand Down Expand Up @@ -486,7 +486,7 @@ fn gpg_p256() {
],
&[
vec![r"\[GNUPG:\] CARDCTRL \d D2760001240103040000000000000000"],
virt::gpg_status(virt::KeyType::P256),
virt::gpg_status(virt::KeyType::P256, 5),
vec![
r"\[GNUPG:\] GET_LINE cardedit.prompt",
r"\[GNUPG:\] GET_LINE cardedit.prompt",
Expand All @@ -495,7 +495,7 @@ fn gpg_p256() {
r"\[GNUPG:\] GET_LINE cardedit.prompt",
],
virt::gpg_inquire_pin(),
virt::gpg_status(virt::KeyType::RsaNone),
virt::gpg_status(virt::KeyType::RsaNone, 0),
vec![r"\[GNUPG:\] GET_LINE cardedit.prompt"],
]
.into_iter()
Expand Down
18 changes: 16 additions & 2 deletions tests/virt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub enum KeyType {
}

#[allow(unused)]
pub fn gpg_status(key: KeyType) -> Vec<&'static str> {
pub fn gpg_status(key: KeyType, sign_count: usize) -> Vec<&'static str> {
let (first, sec, third, fpr, grp) = match key {
KeyType::Cv25519 => (
r"keyattr:1:22:Ed25519:",
Expand Down Expand Up @@ -112,6 +112,20 @@ pub fn gpg_status(key: KeyType) -> Vec<&'static str> {
"grp:[0]{40}:[0]{40}:[0]{40}:",
),
};
// FIXME: This seems bad, but still less noisy than using `String` and adding `.to_string()` everywhere
let signcount = match sign_count {
0 => r"sigcount:0:::",
1 => r"sigcount:1:::",
2 => r"sigcount:2:::",
3 => r"sigcount:3:::",
4 => r"sigcount:4:::",
5 => r"sigcount:5:::",
6 => r"sigcount:6:::",
7 => r"sigcount:7:::",
8 => r"sigcount:8:::",
9 => r"sigcount:9:::",
_ => todo!(),
};

let fprtimes = r"fprtime:\d*:\d*:\d*:";

Expand All @@ -131,7 +145,7 @@ pub fn gpg_status(key: KeyType) -> Vec<&'static str> {
third,
r"maxpinlen:127:127:127:",
r"pinretry:3:3:3:",
r"sigcount:0:::",
signcount,
r"kdf:off:",
r"cafpr::::",
fpr,
Expand Down