Skip to content

Commit f8ba3fb

Browse files
feat: Move get_cose_sign1 into c2pa-crypto crate (#794)
1 parent eea9068 commit f8ba3fb

File tree

5 files changed

+169
-134
lines changed

5 files changed

+169
-134
lines changed

internal/crypto/src/cose/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ pub use error::CoseError;
2929
mod ocsp;
3030
pub use ocsp::{check_ocsp_status, check_ocsp_status_async, OcspFetchPolicy};
3131

32+
mod sign1;
33+
pub use sign1::parse_cose_sign1;
34+
3235
mod sigtst;
3336
pub use sigtst::{
3437
cose_countersign_data, parse_and_validate_sigtst, parse_and_validate_sigtst_async,

internal/crypto/src/cose/sign1.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2022 Adobe. All rights reserved.
2+
// This file is licensed to you under the Apache License,
3+
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
4+
// or the MIT license (http://opensource.org/licenses/MIT),
5+
// at your option.
6+
7+
// Unless required by applicable law or agreed to in writing,
8+
// this software is distributed on an "AS IS" BASIS, WITHOUT
9+
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
10+
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
11+
// specific language governing permissions and limitations under
12+
// each license.
13+
14+
use c2pa_status_tracker::{log_item, validation_codes::CLAIM_SIGNATURE_MISMATCH, StatusTracker};
15+
use coset::{CoseSign1, TaggedCborSerializable};
16+
17+
use crate::cose::CoseError;
18+
19+
/// Parse a byte slice as a COSE Sign1 data structure.
20+
///
21+
/// Log errors that might occur to the provided [`StatusTracker`].
22+
pub fn parse_cose_sign1(
23+
cose_bytes: &[u8],
24+
data: &[u8],
25+
validation_log: &mut impl StatusTracker,
26+
) -> Result<CoseSign1, CoseError> {
27+
let mut sign1 = <coset::CoseSign1 as TaggedCborSerializable>::from_tagged_slice(cose_bytes)
28+
.map_err(|coset_error| {
29+
log_item!(
30+
"Cose_Sign1",
31+
"could not parse signature",
32+
"parse_cose_sign1"
33+
)
34+
.validation_status(CLAIM_SIGNATURE_MISMATCH)
35+
.failure_no_throw(
36+
validation_log,
37+
CoseError::CborParsingError(coset_error.to_string()),
38+
);
39+
40+
CoseError::CborParsingError(coset_error.to_string())
41+
})?;
42+
43+
// Temporarily restore the payload into the signature for verification check.
44+
sign1.payload = Some(data.to_vec());
45+
46+
Ok(sign1)
47+
}

sdk/src/claim.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ use std::path::Path;
1616
use std::{collections::HashMap, fmt};
1717

1818
use async_generic::async_generic;
19-
use c2pa_crypto::{base64, cose::CertificateTrustPolicy, ValidationInfo};
19+
use c2pa_crypto::{
20+
base64,
21+
cose::{parse_cose_sign1, CertificateTrustPolicy},
22+
ValidationInfo,
23+
};
2024
use c2pa_status_tracker::{log_item, OneShotStatusTracker, StatusTracker};
2125
use chrono::{DateTime, Utc};
2226
use serde::{Deserialize, Serialize};
@@ -35,8 +39,8 @@ use crate::{
3539
},
3640
asset_io::CAIRead,
3741
cose_validator::{
38-
check_ocsp_status, check_ocsp_status_async, get_cose_sign1, get_signing_info,
39-
get_signing_info_async, verify_cose, verify_cose_async,
42+
check_ocsp_status, check_ocsp_status_async, get_signing_info, get_signing_info_async,
43+
verify_cose, verify_cose_async,
4044
},
4145
error::{Error, Result},
4246
hashed_uri::HashedUri,
@@ -1068,7 +1072,7 @@ impl Claim {
10681072
}
10691073

10701074
// check certificate revocation
1071-
let sign1 = get_cose_sign1(&sig, &data, validation_log)?;
1075+
let sign1 = parse_cose_sign1(&sig, &data, validation_log)?;
10721076
check_ocsp_status_async(&sign1, &data, ctp, validation_log).await?;
10731077

10741078
let verified =
@@ -1114,7 +1118,7 @@ impl Claim {
11141118
};
11151119

11161120
// check certificate revocation
1117-
let sign1 = get_cose_sign1(sig, data, validation_log)?;
1121+
let sign1 = parse_cose_sign1(sig, data, validation_log)?;
11181122
check_ocsp_status(&sign1, data, ctp, validation_log)?;
11191123

11201124
let verified = verify_cose(

0 commit comments

Comments
 (0)