Skip to content

Commit 14f08ef

Browse files
authored
Merge pull request #34 from veraison/fix-binding
Explicitly check that binding has failed
2 parents cd2cac3 + 6fed615 commit 14f08ef

File tree

4 files changed

+74
-11
lines changed

4 files changed

+74
-11
lines changed

src/main.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use ccatoken::store::{
44
};
55
use ccatoken::token;
66
use clap::Parser;
7-
use ear::TrustVector;
7+
use ear::claim::TRUSTWORTHY_INSTANCE;
8+
use ear::{TrustTier, TrustVector};
89
use serde_json::value::RawValue;
910
use std::error::Error;
1011
use std::fs;
@@ -127,6 +128,19 @@ fn verify(args: &VerifyArgs) -> Result<(TrustVector, TrustVector), Box<dyn Error
127128
Ok(e.get_trust_vectors())
128129
}
129130

131+
fn trust_vector_status(tv: TrustVector) -> TrustTier {
132+
let mut status = TrustTier::None;
133+
134+
for claim in tv {
135+
let claim_tier = claim.tier();
136+
if status < claim_tier {
137+
status = claim_tier
138+
}
139+
}
140+
141+
status
142+
}
143+
130144
fn golden(args: &GoldenArgs) -> Result<(), Box<dyn Error>> {
131145
let c: Vec<u8> = fs::read(&args.evidence)?;
132146

@@ -137,6 +151,14 @@ fn golden(args: &GoldenArgs) -> Result<(), Box<dyn Error>> {
137151
let cpak = map_str_to_cpak(&e.platform_claims, &j)?;
138152
e.verify_with_cpak(cpak)?;
139153

154+
let (platform_tvec, realm_tvec) = e.get_trust_vectors();
155+
if platform_tvec.instance_identity != TRUSTWORTHY_INSTANCE {
156+
return Err("platform is not trustworthy".into());
157+
}
158+
if realm_tvec.instance_identity != TRUSTWORTHY_INSTANCE {
159+
return Err("realm is not trustworthy".into());
160+
}
161+
140162
let rv = map_evidence_to_refval(&e)?;
141163
fs::write(&args.rvstore, rv)?;
142164

@@ -192,7 +214,7 @@ fn map_evidence_to_realm_refval(p: &token::Realm) -> Result<RealmRefValue, Box<d
192214
};
193215

194216
for (i, other) in p.rem.iter().enumerate() {
195-
v.rem[i].value = other.clone();
217+
v.rem[i].value.clone_from(other);
196218
}
197219

198220
Ok(v)

src/token/evidence.rs

+38-9
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use openssl::error::ErrorStack;
2222
use openssl::hash::{Hasher, MessageDigest};
2323
use openssl::nid::Nid;
2424
use serde::Deserialize;
25-
use std::fs;
2625

2726
const CBOR_TAG: u64 = 399;
2827
const PLATFORM_LABEL: i128 = 44234;
@@ -467,10 +466,14 @@ impl Evidence {
467466

468467
match self.check_binding() {
469468
Err(_) => {
470-
/* swallow error */
471469
return Ok(());
472470
}
473-
_ => { /* we are done */ }
471+
_ => {
472+
// early return on binder errors
473+
if self.realm_tvec.instance_identity.get() == CRYPTO_VALIDATION_FAILED {
474+
return Ok(());
475+
}
476+
}
474477
}
475478

476479
self.realm_tvec.instance_identity.set(TRUSTWORTHY_INSTANCE);
@@ -487,6 +490,10 @@ impl Evidence {
487490
assert!(!self.realm.bytes.is_empty(), "Realm Token is Mandatory");
488491
self.verify_realm_token()?;
489492
self.check_binding()?;
493+
// early return on binder errors
494+
if self.realm_tvec.instance_identity.get() == CRYPTO_VALIDATION_FAILED {
495+
return Ok(());
496+
}
490497
self.realm_tvec.instance_identity.set(TRUSTWORTHY_INSTANCE);
491498
Ok(())
492499
}
@@ -521,13 +528,17 @@ impl Evidence {
521528

522529
let mut hasher = hasher_from_alg(realm_pub_key_hash_alg.as_str())?;
523530

524-
hasher
525-
.update(&realm_pub_key)
526-
.map_err(|e| Error::HashCalculateFail(format!("{e:?}")))?;
531+
hasher.update(&realm_pub_key).map_err(|e| {
532+
self.realm_tvec.set_all(VERIFIER_MALFUNCTION);
533+
534+
Error::HashCalculateFail(format!("{e:?}"))
535+
})?;
527536

528-
let sum = hasher
529-
.finish()
530-
.map_err(|e| Error::HashCalculateFail(format!("{:?}", e)))?;
537+
let sum = hasher.finish().map_err(|e| {
538+
self.realm_tvec.set_all(VERIFIER_MALFUNCTION);
539+
540+
Error::HashCalculateFail(format!("{e:?}"))
541+
})?;
531542

532543
if sum.to_vec() != *platform_nonce {
533544
self.realm_tvec.set_all(CRYPTO_VALIDATION_FAILED);
@@ -595,9 +606,11 @@ mod tests {
595606

596607
const TEST_CCA_TOKEN_1_OK: &[u8; 1222] = include_bytes!("../../testdata/cca-token-01.cbor");
597608
const TEST_CCA_TOKEN_2_OK: &[u8; 1125] = include_bytes!("../../testdata/cca-token-02.cbor");
609+
const TEST_CCA_TOKEN_BUG_33: &[u8; 2507] = include_bytes!("../../testdata/bug-33-repro.cbor");
598610
const TEST_CCA_RVS_OK: &str = include_str!("../../testdata/rv.json");
599611
const TEST_TA_2_OK: &str = include_str!("../../testdata/ta-02-ok.json");
600612
const TEST_TA_2_BAD: &str = include_str!("../../testdata/ta-02-bad.json");
613+
const TEST_TA_TFA: &str = include_str!("../../testdata/ta-tfa.json");
601614

602615
#[test]
603616
fn decode_good_token() {
@@ -688,4 +701,20 @@ mod tests {
688701
serde_json::to_string_pretty(&evidence.realm_tvec).unwrap()
689702
);
690703
}
704+
705+
#[test]
706+
fn bug_33_regression() {
707+
let mut evidence = Evidence::decode(&TEST_CCA_TOKEN_BUG_33.to_vec())
708+
.expect("decoding TEST_CCA_TOKEN_BUG_33");
709+
710+
let mut tas = MemoTrustAnchorStore::new();
711+
tas.load_json(TEST_TA_TFA).expect("loading trust anchors");
712+
713+
let r = evidence.verify(&tas);
714+
715+
assert!(r.is_ok());
716+
717+
assert!(evidence.realm_tvec.instance_identity.get() == CRYPTO_VALIDATION_FAILED);
718+
assert!(evidence.platform_tvec.instance_identity.get() == TRUSTWORTHY_INSTANCE);
719+
}
691720
}

testdata/bug-33-repro.cbor

2.45 KB
Binary file not shown.

testdata/ta-tfa.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"pkey": {
4+
"crv": "P-384",
5+
"kty": "EC",
6+
"x": "IShnxS4rlQiwpCCpBWDzlNLfqiG911FP8akBr-fh94uxHU5m-Kijivp2r2oxxN6M",
7+
"y": "hM4tr8mWQli1P61xh3T0ViDREbF26DGOEYfbAjWjGNN7pZf-6A4OTHYqEryz6m7U"
8+
},
9+
"implementation-id": "7f454c4602010100000000000000000003003e00010000005058000000000000",
10+
"instance-id": "0107060504030201000f0e0d0c0b0a090817161514131211101f1e1d1c1b1a1918"
11+
}
12+
]

0 commit comments

Comments
 (0)