From e2f32d0fa0ea5917049e7c852945dbfcdb4e6be1 Mon Sep 17 00:00:00 2001 From: Patrick Uiterwijk Date: Thu, 27 Aug 2020 14:04:33 +0200 Subject: [PATCH] Support some more weird pcr_id specifications This change makes it pass all the different possible values provided by the clevis tpm2 pin test suite. Signed-off-by: Patrick Uiterwijk --- Cargo.toml | 2 +- src/cli.rs | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1ed3006..b1f8bde 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "clevis-pin-tpm2" -version = "0.1.2" +version = "0.1.3" description = "Clevis TPM2 PIN with policy support" authors = ["Patrick Uiterwijk "] edition = "2018" diff --git a/src/cli.rs b/src/cli.rs index 71dda0e..b861ed9 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -110,12 +110,20 @@ impl TPM2Config { } fn normalize_pcr_ids(&mut self) -> Result<(), PinError> { + // Normalize from array with one string to just string + if let Some(serde_json::Value::Array(vals)) = &self.pcr_ids { + if vals.len() == 1 { + if let serde_json::Value::String(val) = &vals[0] { + self.pcr_ids = Some(serde_json::Value::String(val.to_string())); + } + } + } // Normalize pcr_ids from comma-separated string to array if let Some(serde_json::Value::String(val)) = &self.pcr_ids { // Was a string, do a split let newval: Vec = val .split(',') - .map(|x| serde_json::Value::String(x.to_string())) + .map(|x| serde_json::Value::String(x.trim().to_string())) .collect(); self.pcr_ids = Some(serde_json::Value::Array(newval)); } @@ -124,7 +132,7 @@ impl TPM2Config { let newvals: Result, _> = vals .iter() .map(|x| match x { - serde_json::Value::String(val) => match val.parse::() { + serde_json::Value::String(val) => match val.trim().parse::() { Ok(res) => { let new = serde_json::Value::Number(res); if !new.is_u64() {