Skip to content

Commit fed4478

Browse files
feat: Move signing_alg_from_sign1 into c2pa-crypto (#795)
1 parent f8ba3fb commit fed4478

File tree

5 files changed

+48
-58
lines changed

5 files changed

+48
-58
lines changed

internal/crypto/src/cose/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ pub enum CoseError {
3333
#[error("no time stamp token found in sigTst or sigTst2 header")]
3434
NoTimeStampToken,
3535

36+
/// Unsupported signing algorithm found.
37+
#[error("the certificate was signed using an unsupported signature algorithm")]
38+
UnsupportedSigningAlgorithm,
39+
3640
/// An error occurred while parsing CBOR.
3741
#[error("error while parsing CBOR ({0})")]
3842
CborParsingError(String),

internal/crypto/src/cose/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ mod ocsp;
3030
pub use ocsp::{check_ocsp_status, check_ocsp_status_async, OcspFetchPolicy};
3131

3232
mod sign1;
33-
pub use sign1::parse_cose_sign1;
33+
pub use sign1::{parse_cose_sign1, signing_alg_from_sign1};
3434

3535
mod sigtst;
3636
pub use sigtst::{

internal/crypto/src/cose/sign1.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
// each license.
1313

1414
use c2pa_status_tracker::{log_item, validation_codes::CLAIM_SIGNATURE_MISMATCH, StatusTracker};
15-
use coset::{CoseSign1, TaggedCborSerializable};
15+
use coset::{iana::Algorithm, CoseSign1, RegisteredLabelWithPrivate, TaggedCborSerializable};
1616

17-
use crate::cose::CoseError;
17+
use crate::{cose::CoseError, SigningAlg};
1818

1919
/// Parse a byte slice as a COSE Sign1 data structure.
2020
///
@@ -45,3 +45,36 @@ pub fn parse_cose_sign1(
4545

4646
Ok(sign1)
4747
}
48+
49+
/// TEMPORARILY PUBLIC while refactoring.
50+
pub fn signing_alg_from_sign1(sign1: &coset::CoseSign1) -> Result<SigningAlg, CoseError> {
51+
let Some(ref alg) = sign1.protected.header.alg else {
52+
return Err(CoseError::UnsupportedSigningAlgorithm);
53+
};
54+
55+
match alg {
56+
RegisteredLabelWithPrivate::PrivateUse(a) => match a {
57+
-39 => Ok(SigningAlg::Ps512),
58+
-38 => Ok(SigningAlg::Ps384),
59+
-37 => Ok(SigningAlg::Ps256),
60+
-36 => Ok(SigningAlg::Es512),
61+
-35 => Ok(SigningAlg::Es384),
62+
-7 => Ok(SigningAlg::Es256),
63+
-8 => Ok(SigningAlg::Ed25519),
64+
_ => Err(CoseError::UnsupportedSigningAlgorithm),
65+
},
66+
67+
RegisteredLabelWithPrivate::Assigned(a) => match a {
68+
Algorithm::PS512 => Ok(SigningAlg::Ps512),
69+
Algorithm::PS384 => Ok(SigningAlg::Ps384),
70+
Algorithm::PS256 => Ok(SigningAlg::Ps256),
71+
Algorithm::ES512 => Ok(SigningAlg::Es512),
72+
Algorithm::ES384 => Ok(SigningAlg::Es384),
73+
Algorithm::ES256 => Ok(SigningAlg::Es256),
74+
Algorithm::EdDSA => Ok(SigningAlg::Ed25519),
75+
_ => Err(CoseError::UnsupportedSigningAlgorithm),
76+
},
77+
78+
_ => Err(CoseError::UnsupportedSigningAlgorithm),
79+
}
80+
}

sdk/src/cose_validator.rs

Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ use async_generic::async_generic;
1717
use c2pa_crypto::{
1818
asn1::rfc3161::TstInfo,
1919
cose::{
20-
check_certificate_profile, parse_cose_sign1, validate_cose_tst_info,
21-
validate_cose_tst_info_async, CertificateTrustError, CertificateTrustPolicy, CoseError,
22-
OcspFetchPolicy,
20+
check_certificate_profile, parse_cose_sign1, signing_alg_from_sign1,
21+
validate_cose_tst_info, validate_cose_tst_info_async, CertificateTrustError,
22+
CertificateTrustPolicy, CoseError, OcspFetchPolicy,
2323
},
2424
ocsp::OcspResponse,
2525
p1363::parse_ec_der_sig,
@@ -33,7 +33,7 @@ use coset::{
3333
iana::{self, EnumI64},
3434
sig_structure_data, Label,
3535
};
36-
use x509_parser::{der_parser::oid, num_bigint::BigUint, oid_registry::Oid, prelude::*};
36+
use x509_parser::{num_bigint::BigUint, prelude::*};
3737

3838
use crate::{
3939
error::{Error, Result},
@@ -134,54 +134,6 @@ fn check_trust(
134134

135135
// ---- TEMPORARY MARKER: Above this line will not move to c2pa-crypto
136136

137-
#[allow(dead_code)] // used only in WASM build
138-
pub(crate) const SHA1_OID: Oid<'static> = oid!(1.3.14 .3 .2 .26);
139-
140-
/********************** Supported Validators ***************************************
141-
RS256 RSASSA-PKCS1-v1_5 using SHA-256 - not recommended
142-
RS384 RSASSA-PKCS1-v1_5 using SHA-384 - not recommended
143-
RS512 RSASSA-PKCS1-v1_5 using SHA-512 - not recommended
144-
PS256 RSASSA-PSS using SHA-256 and MGF1 with SHA-256
145-
PS384 RSASSA-PSS using SHA-384 and MGF1 with SHA-384
146-
PS512 RSASSA-PSS using SHA-512 and MGF1 with SHA-512
147-
ES256 ECDSA using P-256 and SHA-256
148-
ES384 ECDSA using P-384 and SHA-384
149-
ES512 ECDSA using P-521 and SHA-512
150-
ED25519 Edwards Curve 25519
151-
**********************************************************************************/
152-
153-
pub(crate) fn get_signing_alg(cs1: &coset::CoseSign1) -> Result<SigningAlg> {
154-
// find the supported handler for the algorithm
155-
match cs1.protected.header.alg {
156-
Some(ref alg) => match alg {
157-
coset::RegisteredLabelWithPrivate::PrivateUse(a) => match a {
158-
-39 => Ok(SigningAlg::Ps512),
159-
-38 => Ok(SigningAlg::Ps384),
160-
-37 => Ok(SigningAlg::Ps256),
161-
-36 => Ok(SigningAlg::Es512),
162-
-35 => Ok(SigningAlg::Es384),
163-
-7 => Ok(SigningAlg::Es256),
164-
-8 => Ok(SigningAlg::Ed25519),
165-
_ => Err(Error::CoseSignatureAlgorithmNotSupported),
166-
},
167-
coset::RegisteredLabelWithPrivate::Assigned(a) => match a {
168-
coset::iana::Algorithm::PS512 => Ok(SigningAlg::Ps512),
169-
coset::iana::Algorithm::PS384 => Ok(SigningAlg::Ps384),
170-
coset::iana::Algorithm::PS256 => Ok(SigningAlg::Ps256),
171-
coset::iana::Algorithm::ES512 => Ok(SigningAlg::Es512),
172-
coset::iana::Algorithm::ES384 => Ok(SigningAlg::Es384),
173-
coset::iana::Algorithm::ES256 => Ok(SigningAlg::Es256),
174-
coset::iana::Algorithm::EdDSA => Ok(SigningAlg::Ed25519),
175-
_ => Err(Error::CoseSignatureAlgorithmNotSupported),
176-
},
177-
coset::RegisteredLabelWithPrivate::Text(a) => a
178-
.parse()
179-
.map_err(|_| Error::CoseSignatureAlgorithmNotSupported),
180-
},
181-
None => Err(Error::CoseSignatureAlgorithmNotSupported),
182-
}
183-
}
184-
185137
fn get_sign_cert(sign1: &coset::CoseSign1) -> Result<Vec<u8>> {
186138
// element 0 is the signing cert
187139
let certs = get_sign_certs(sign1)?;
@@ -378,7 +330,7 @@ pub(crate) async fn verify_cose_async(
378330
) -> Result<ValidationInfo> {
379331
let mut sign1 = parse_cose_sign1(&cose_bytes, &data, validation_log)?;
380332

381-
let alg = match get_signing_alg(&sign1) {
333+
let alg = match signing_alg_from_sign1(&sign1) {
382334
Ok(a) => a,
383335
Err(_) => {
384336
log_item!(
@@ -534,7 +486,7 @@ pub(crate) fn get_signing_info(
534486
};
535487
issuer_org = extract_subject_from_cert(&signcert).ok();
536488
cert_serial_number = Some(extract_serial_from_cert(&signcert));
537-
if let Ok(a) = get_signing_alg(&sign1) {
489+
if let Ok(a) = signing_alg_from_sign1(&sign1) {
538490
alg = Some(a);
539491
}
540492
};
@@ -581,7 +533,7 @@ pub(crate) fn verify_cose(
581533
) -> Result<ValidationInfo> {
582534
let sign1 = parse_cose_sign1(cose_bytes, data, validation_log)?;
583535

584-
let alg = match get_signing_alg(&sign1) {
536+
let alg = match signing_alg_from_sign1(&sign1) {
585537
Ok(a) => a,
586538
Err(_) => {
587539
log_item!(

sdk/src/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ impl From<CoseError> for Error {
351351
CoseError::MissingSigningCertificateChain => Self::CoseX5ChainMissing,
352352
CoseError::MultipleSigningCertificateChains => Self::CoseVerifier,
353353
CoseError::NoTimeStampToken => Self::NotFound,
354+
CoseError::UnsupportedSigningAlgorithm => Self::CoseSignatureAlgorithmNotSupported,
354355
CoseError::CborParsingError(_) => Self::CoseTimeStampGeneration,
355356
CoseError::TimeStampError(e) => e.into(),
356357
CoseError::CertificateProfileError(e) => e.into(),

0 commit comments

Comments
 (0)