Skip to content

Commit a95e236

Browse files
fix: Turn on trust by default (#1483)
Turn on trust by default Change c2patool to never disable trust (can only enable trust) Fix unit tests to use configuration files (now get trust by default) Fix logItem to not automatically write to logger Fix some error reporting to match the spec better Fix default test toml to correctly initialize trust anchors
1 parent f746613 commit a95e236

File tree

14 files changed

+74
-50
lines changed

14 files changed

+74
-50
lines changed

c2pa_c_ffi/src/json_api.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ pub fn sign_file(
7878
mod tests {
7979
use std::{ffi::CString, fs::remove_dir_all, path::PathBuf};
8080

81+
use c2pa::settings::Settings;
82+
8183
use super::*;
8284
use crate::c_api::c2pa_load_settings;
8385

@@ -89,6 +91,8 @@ mod tests {
8991

9092
#[test]
9193
fn test_verify_from_file_no_base() {
94+
let _ = Settings::from_toml(include_str!("../../sdk/tests/fixtures/test_settings.toml"));
95+
9296
let path = test_path("tests/fixtures/C.jpg");
9397
let result = read_file(&path, None);
9498
assert!(result.is_ok());
@@ -100,6 +104,8 @@ mod tests {
100104

101105
#[test]
102106
fn test_read_from_file_with_base() {
107+
let _ = Settings::from_toml(include_str!("../../sdk/tests/fixtures/test_settings.toml"));
108+
103109
let path = test_path("tests/fixtures/C.jpg");
104110
let data_dir = "../target/data_dir";
105111
if PathBuf::from(data_dir).exists() {

cli/src/main.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ fn configure_sdk(args: &CliArgs) -> Result<()> {
449449
}
450450

451451
// if any trust setting is provided enable the trust checks
452+
// there is no disabling of default setting only the ability to enable if they were internally disabled
452453
if enable_trust_checks {
453454
Settings::from_toml(
454455
&toml::toml! {
@@ -457,14 +458,6 @@ fn configure_sdk(args: &CliArgs) -> Result<()> {
457458
}
458459
.to_string(),
459460
)?;
460-
} else {
461-
Settings::from_toml(
462-
&toml::toml! {
463-
[verify]
464-
verify_trust = false
465-
}
466-
.to_string(),
467-
)?;
468461
}
469462

470463
// enable or disable verification after signing

sdk/examples/v2api.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ fn main() -> Result<()> {
8484
}
8585
.to_string();
8686

87+
Settings::from_toml(include_str!("../tests/fixtures/test_settings.toml"))?;
88+
8789
Settings::from_toml(&modified_core)?;
8890

8991
let json = manifest_def(title, format);
@@ -152,7 +154,7 @@ fn main() -> Result<()> {
152154
}
153155

154156
println!("{}", reader.json());
155-
assert_ne!(reader.validation_state(), ValidationState::Invalid);
157+
assert_eq!(reader.validation_state(), ValidationState::Trusted);
156158
assert_eq!(reader.active_manifest().unwrap().title().unwrap(), title);
157159

158160
Ok(())

sdk/src/crypto/cose/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub enum CoseError {
7878
#[error(transparent)]
7979
RawSignatureValidationError(#[from] RawSignatureValidationError),
8080

81-
/// An unexpected internal error occured while requesting the time stamp
81+
/// An unexpected internal error occurred while requesting the time stamp
8282
/// response.
8383
#[error("internal error ({0})")]
8484
InternalError(String),

sdk/src/crypto/cose/sigtst.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ use crate::{
2626
raw_signature::{AsyncRawSigner, RawSigner},
2727
time_stamp::{verify_time_stamp, verify_time_stamp_async, ContentInfo, TimeStampResponse},
2828
},
29+
log_item,
2930
status_tracker::StatusTracker,
31+
validation_status,
3032
};
3133

3234
/// Given a COSE signature, retrieve the `sigTst` header from it and validate
@@ -109,6 +111,18 @@ pub(crate) fn parse_and_validate_sigtst(
109111

110112
let mut tstinfos: Vec<TstInfo> = vec![];
111113

114+
// only a single value is allowed in tstTokens
115+
if tst_container.tst_tokens.len() > 1 {
116+
log_item!(
117+
"",
118+
"only a single timestamp response is allowed in a manifest",
119+
"parse_and_validate_sigtst"
120+
)
121+
.validation_status(validation_status::TIMESTAMP_MALFORMED)
122+
.informational(validation_log);
123+
return Err(CoseError::NoTimeStampToken);
124+
}
125+
112126
for token in &tst_container.tst_tokens {
113127
let tbs = cose_countersign_data(data, p_header);
114128

sdk/src/settings/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl Default for Verify {
228228
Self {
229229
verify_after_reading: true,
230230
verify_after_sign: true,
231-
verify_trust: cfg!(test),
231+
verify_trust: true,
232232
verify_timestamp_trust: !cfg!(test), // verify timestamp trust unless in test mode
233233
ocsp_fetch: false,
234234
remote_manifest_fetch: true,

sdk/src/status_tracker/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use std::{fmt::Debug, iter::Iterator};
1717

18-
use log::{error, info};
18+
use log::info;
1919

2020
/// A `StatusTracker` is used in the validation logic of c2pa-rs and
2121
/// related crates to control error-handling behavior and optionally
@@ -90,7 +90,7 @@ impl StatusTracker {
9090
log_item.label = std::borrow::Cow::Owned(current_uri.to_string());
9191
}
9292
}
93-
error!("Validation error: {log_item:#?}");
93+
9494
self.logged_items.push(log_item);
9595

9696
match self.error_behavior {

sdk/src/store.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,7 +1998,7 @@ impl Store {
19981998
.validation_status(validation_status::ASSERTION_TIMESTAMP_MALFORMED)
19991999
.failure_as_err(
20002000
validation_log,
2001-
Error::OtherError("timestamp assertion malformed".into()),
2001+
Error::ValidationRule("timestamp assertion malformed".into()),
20022002
)
20032003
})?;
20042004

@@ -2012,19 +2012,8 @@ impl Store {
20122012
validation_log,
20132013
) {
20142014
svi.timestamps.insert(rc.label().to_owned(), tst_info);
2015-
continue;
20162015
}
20172016
}
2018-
log_item!(
2019-
to_manifest_uri(referenced_claim),
2020-
"could not validate timestamp assertion",
2021-
"get_claim_referenced_manifests"
2022-
)
2023-
.validation_status(validation_status::ASSERTION_TIMESTAMP_MALFORMED)
2024-
.failure(
2025-
validation_log,
2026-
Error::OtherError("timestamp assertion malformed".into()),
2027-
)?;
20282017
}
20292018
}
20302019

sdk/tests/fixtures/test_settings.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ version_minor = 0
99
# String to user-provided trust anchors (PEM format).
1010
# user_anchors = ""
1111
# String to system trust anchors (PEM format).
12+
# trust_anchors = ""
13+
[trust]
1214
trust_anchors = """-----BEGIN CERTIFICATE-----
1315
MIICEzCCAcWgAwIBAgIUW4fUnS38162x10PCnB8qFsrQuZgwBQYDK2VwMHcxCzAJ
1416
BgNVBAYTAlVTMQswCQYDVQQIDAJDQTESMBAGA1UEBwwJU29tZXdoZXJlMRowGAYD

sdk/tests/integration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ mod integration_1 {
377377
//println!("{reader}");
378378
// ensure certificate status assertion was created
379379
assert!(reader_json.contains(r#"label": "c2pa.certificate-status"#));
380-
assert_eq!(reader.validation_state(), ValidationState::Valid);
380+
assert_eq!(reader.validation_state(), ValidationState::Trusted);
381381
assert!(reader_json.contains("signingCredential.ocsp.notRevoked"));
382382
Ok(())
383383
}

0 commit comments

Comments
 (0)