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 <patrick@puiterwijk.org>
This commit is contained in:
Patrick Uiterwijk 2020-08-27 14:04:33 +02:00
parent 6a37627500
commit e2f32d0fa0
2 changed files with 11 additions and 3 deletions

View file

@ -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 <patrick@puiterwijk.org>"]
edition = "2018"

View file

@ -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<serde_json::Value> = 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<Vec<serde_json::Value>, _> = vals
.iter()
.map(|x| match x {
serde_json::Value::String(val) => match val.parse::<serde_json::Number>() {
serde_json::Value::String(val) => match val.trim().parse::<serde_json::Number>() {
Ok(res) => {
let new = serde_json::Value::Number(res);
if !new.is_u64() {