encypher-c2pa 1.3.0

Verification-only C2PA 2.4 and CAWG SDK, offline by default
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
// Copyright 2026 Encypher Corporation
// SPDX-License-Identifier: Apache-2.0

//! RFC 3161 timestamp-token verification for C2PA `sigTst2` headers.
//!
//! A timestamp is accepted only when its CMS signature, signed attributes,
//! message imprint, timestamping EKU, certificate validity, and chain to the
//! caller-supplied TSA trust list all verify.

use cms::{
    cert::CertificateChoices,
    content_info::ContentInfo,
    signed_data::{SignedData, SignerIdentifier, SignerInfo},
};
use const_oid::ObjectIdentifier;
use der::{asn1::OctetString, Decode, Encode};
use ecdsa::signature::hazmat::PrehashVerifier;
use rsa::{pkcs1::DecodeRsaPublicKey, signature::Verifier as _};
use sha2::{Digest, Sha256, Sha384, Sha512};
use time::{Duration, OffsetDateTime};
use x509_cert::{
    attr::Attribute,
    ext::pkix::{BasicConstraints, ExtendedKeyUsage, KeyUsage, SubjectKeyIdentifier},
    Certificate,
};
use x509_tsp::{TimeStampResp, TstInfo};

use super::{common_name, validate_chain, AnchorPurpose, TrustList, OID_KP_TIME_STAMPING};

const OID_SIGNED_DATA: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.113549.1.7.2");
const OID_TST_INFO: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.113549.1.9.16.1.4");
const OID_CONTENT_TYPE_ATTR: ObjectIdentifier =
    ObjectIdentifier::new_unwrap("1.2.840.113549.1.9.3");
const OID_MESSAGE_DIGEST_ATTR: ObjectIdentifier =
    ObjectIdentifier::new_unwrap("1.2.840.113549.1.9.4");
const OID_SUBJECT_KEY_IDENTIFIER: ObjectIdentifier = ObjectIdentifier::new_unwrap("2.5.29.14");
const OID_EXT_EKU: ObjectIdentifier = ObjectIdentifier::new_unwrap("2.5.29.37");
const OID_EXT_BASIC_CONSTRAINTS: ObjectIdentifier = ObjectIdentifier::new_unwrap("2.5.29.19");
const OID_EXT_KEY_USAGE: ObjectIdentifier = ObjectIdentifier::new_unwrap("2.5.29.15");
const OID_KP_TIME_STAMPING_OBJ: ObjectIdentifier =
    ObjectIdentifier::new_unwrap(OID_KP_TIME_STAMPING);

const OID_SHA256: ObjectIdentifier = ObjectIdentifier::new_unwrap("2.16.840.1.101.3.4.2.1");
const OID_SHA384: ObjectIdentifier = ObjectIdentifier::new_unwrap("2.16.840.1.101.3.4.2.2");
const OID_SHA512: ObjectIdentifier = ObjectIdentifier::new_unwrap("2.16.840.1.101.3.4.2.3");

const OID_EC_PUBLIC_KEY: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.10045.2.1");
const OID_RSA_ENCRYPTION: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.1");
const OID_RSASSA_PSS: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.10");
const OID_MGF1: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.8");
const OID_ED25519: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.3.101.112");
const OID_CURVE_P256: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.10045.3.1.7");
const OID_CURVE_P384: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.3.132.0.34");
const OID_CURVE_P521: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.3.132.0.35");
const OID_ECDSA_SHA256: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.10045.4.3.2");
const OID_ECDSA_SHA384: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.10045.4.3.3");
const OID_ECDSA_SHA512: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.10045.4.3.4");
const OID_RSA_SHA256: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.11");
const OID_RSA_SHA384: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.12");
const OID_RSA_SHA512: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.13");

const MAX_FUTURE_SKEW: Duration = Duration::minutes(5);

/// Result of verifying one RFC 3161 timestamp token.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TimestampResult {
    /// True only when every cryptographic and trust check passed.
    pub verified: bool,
    /// Trusted timestamp generation time.
    pub time: Option<OffsetDateTime>,
    /// Common Name of the verified TSA signer certificate, when present.
    pub tsa_common_name: Option<String>,
    /// Stable failure reason. `None` when `verified` is true.
    pub error: Option<&'static str>,
}

impl TimestampResult {
    fn failure(error: &'static str) -> Self {
        Self {
            verified: false,
            time: None,
            tsa_common_name: None,
            error: Some(error),
        }
    }

    fn success(time: OffsetDateTime, tsa_common_name: Option<String>) -> Self {
        Self {
            verified: true,
            time: Some(time),
            tsa_common_name,
            error: None,
        }
    }
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum HashAlgorithm {
    Sha256,
    Sha384,
    Sha512,
}

impl HashAlgorithm {
    fn from_oid(oid: ObjectIdentifier) -> Option<Self> {
        match oid {
            OID_SHA256 => Some(Self::Sha256),
            OID_SHA384 => Some(Self::Sha384),
            OID_SHA512 => Some(Self::Sha512),
            _ => None,
        }
    }

    fn digest(self, data: &[u8]) -> Vec<u8> {
        match self {
            Self::Sha256 => Sha256::digest(data).to_vec(),
            Self::Sha384 => Sha384::digest(data).to_vec(),
            Self::Sha512 => Sha512::digest(data).to_vec(),
        }
    }
}

/// Extract the CMS `TimeStampToken` from a legacy RFC 3161 `TimeStampResp`.
pub fn token_from_timestamp_response(response_der: &[u8]) -> Result<Vec<u8>, &'static str> {
    let response =
        TimeStampResp::from_der(response_der).map_err(|_| "timestamp_response_parse_error")?;
    response
        .time_stamp_token
        .ok_or("timestamp_response_token_missing")?
        .to_der()
        .map_err(|_| "timestamp_response_token_invalid")
}

/// The CMS structures inside one RFC 3161 `TimeStampToken`.
struct TokenParts {
    signed_data: SignedData,
    /// DER of the `TSTInfo` eContent, which the signed `messageDigest`
    /// attribute covers.
    tst_octets: OctetString,
    tst_info: TstInfo,
}

/// Decode a `TimeStampToken` into its signed data and `TSTInfo`. Structural
/// decoding only: no imprint, signature, or trust check happens here.
fn parse_token_parts(token_der: &[u8]) -> Result<TokenParts, &'static str> {
    let content_info = ContentInfo::from_der(token_der).map_err(|_| "timestamp_parse_error")?;
    if content_info.content_type != OID_SIGNED_DATA {
        return Err("timestamp_not_signed_data");
    }
    let signed_data = content_info
        .content
        .decode_as::<SignedData>()
        .map_err(|_| "timestamp_signed_data_invalid")?;
    if signed_data.encap_content_info.econtent_type != OID_TST_INFO {
        return Err("timestamp_not_tst_info");
    }
    let econtent = signed_data
        .encap_content_info
        .econtent
        .as_ref()
        .ok_or("timestamp_tst_info_missing")?;
    let tst_octets = econtent
        .decode_as::<OctetString>()
        .map_err(|_| "timestamp_tst_info_invalid")?;
    let tst_info =
        TstInfo::from_der(tst_octets.as_bytes()).map_err(|_| "timestamp_tst_info_invalid")?;
    Ok(TokenParts {
        signed_data,
        tst_octets,
        tst_info,
    })
}

/// Locate the certificate a `SignerInfo` names inside the token's certificate set.
fn signer_certificate<'a>(
    signed_data: &'a SignedData,
    signer_info: &SignerInfo,
) -> Option<&'a Certificate> {
    signed_data
        .certificates
        .as_ref()?
        .0
        .iter()
        .find_map(|choice| match choice {
            CertificateChoices::Certificate(cert) if signer_matches(signer_info, cert) => {
                Some(cert)
            }
            _ => None,
        })
}

/// The time an RFC 3161 token asserts, plus the DER of the TSA certificate that
/// signed it.
///
/// This is a descriptive read for reporting surfaces such as the crJSON
/// `timeStampInfo` object. It performs no imprint, signature, or trust check,
/// so its presence is never evidence that a timestamp is valid:
/// [`verify_timestamp_token`] answers that question.
#[derive(Debug, Clone)]
pub struct TokenDescription {
    /// `TSTInfo.genTime`.
    pub generated_at: OffsetDateTime,
    /// DER of the TSA signer certificate carried by the token, when present.
    pub signer_cert_der: Option<Vec<u8>>,
}

/// Read the descriptive fields of an RFC 3161 `TimeStampToken`.
///
/// Returns `None` when the token does not decode or carries no representable
/// generation time.
pub fn describe_timestamp_token(token_der: &[u8]) -> Option<TokenDescription> {
    let parts = parse_token_parts(token_der).ok()?;
    let generated_at = OffsetDateTime::from_unix_timestamp(
        parts.tst_info.gen_time.to_unix_duration().as_secs() as i64,
    )
    .ok()?;
    let signer_cert_der = parts
        .signed_data
        .signer_infos
        .0
        .iter()
        .next()
        .and_then(|signer_info| signer_certificate(&parts.signed_data, signer_info))
        .and_then(|cert| cert.to_der().ok());
    Some(TokenDescription {
        generated_at,
        signer_cert_der,
    })
}

/// Verify an RFC 3161 `TimeStampToken` against the exact C2PA timestamp input.
///
/// `timestamp_input` is the C2PA v2 CounterSignature `ToBeSigned` value, not the
/// raw COSE signature bytes. `tsa_trust` must contain the permitted TSA roots.
pub fn verify_timestamp_token(
    token_der: &[u8],
    timestamp_input: &[u8],
    tsa_trust: &TrustList,
    verification_time: OffsetDateTime,
) -> TimestampResult {
    verify_timestamp_token_inner(token_der, timestamp_input, tsa_trust, verification_time)
        .unwrap_or_else(TimestampResult::failure)
}

/// Validate a timestamp token's structure, imprint, CMS signature, and TSA
/// leaf profile without deciding whether its certificate chain is trusted.
pub fn inspect_timestamp_token(
    token_der: &[u8],
    timestamp_input: &[u8],
    verification_time: OffsetDateTime,
) -> Result<(), &'static str> {
    match verify_timestamp_token_inner(
        token_der,
        timestamp_input,
        &TrustList::default(),
        verification_time,
    ) {
        Err("no_tsa_anchors") => Ok(()),
        Err(error) => Err(error),
        Ok(_) => Ok(()),
    }
}

fn verify_timestamp_token_inner(
    token_der: &[u8],
    timestamp_input: &[u8],
    tsa_trust: &TrustList,
    verification_time: OffsetDateTime,
) -> Result<TimestampResult, &'static str> {
    let TokenParts {
        signed_data,
        tst_octets,
        tst_info,
    } = parse_token_parts(token_der)?;
    let tst_bytes = tst_octets.as_bytes();

    let generated_at =
        OffsetDateTime::from_unix_timestamp(tst_info.gen_time.to_unix_duration().as_secs() as i64)
            .map_err(|_| "timestamp_time_invalid")?;
    if generated_at > verification_time + MAX_FUTURE_SKEW {
        return Err("timestamp_time_in_future");
    }

    let imprint_hash = HashAlgorithm::from_oid(tst_info.message_imprint.hash_algorithm.oid)
        .ok_or("timestamp_imprint_hash_unsupported")?;
    if imprint_hash.digest(timestamp_input) != tst_info.message_imprint.hashed_message.as_bytes() {
        return Err("timestamp_imprint_mismatch");
    }

    if signed_data.signer_infos.0.len() != 1 {
        return Err("timestamp_signer_info_count_invalid");
    }
    let signer_info = signed_data
        .signer_infos
        .0
        .iter()
        .next()
        .ok_or("timestamp_signer_info_count_invalid")?;
    let signed_attrs = signer_info
        .signed_attrs
        .as_ref()
        .ok_or("timestamp_signed_attrs_missing")?;
    let content_type = single_attribute(signed_attrs.iter(), OID_CONTENT_TYPE_ATTR)
        .ok_or("timestamp_content_type_attr_invalid")?
        .decode_as::<ObjectIdentifier>()
        .map_err(|_| "timestamp_content_type_attr_invalid")?;
    if content_type != OID_TST_INFO {
        return Err("timestamp_content_type_attr_invalid");
    }

    let signer_hash = HashAlgorithm::from_oid(signer_info.digest_alg.oid)
        .ok_or("timestamp_digest_hash_unsupported")?;
    let message_digest = single_attribute(signed_attrs.iter(), OID_MESSAGE_DIGEST_ATTR)
        .ok_or("timestamp_message_digest_attr_invalid")?
        .decode_as::<OctetString>()
        .map_err(|_| "timestamp_message_digest_attr_invalid")?;
    if signer_hash.digest(tst_bytes) != message_digest.as_bytes() {
        return Err("timestamp_message_digest_mismatch");
    }

    let signer_cert =
        signer_certificate(&signed_data, signer_info).ok_or("timestamp_signer_cert_missing")?;
    let signer_der = signer_cert
        .to_der()
        .map_err(|_| "timestamp_signer_cert_invalid")?;

    let signed_attrs_der = signed_attrs
        .to_der()
        .map_err(|_| "timestamp_signed_attrs_invalid")?;
    if !verify_signer_signature(signer_cert, signer_info, signer_hash, &signed_attrs_der) {
        return Err("timestamp_signature_invalid");
    }
    if !has_strict_timestamping_eku(signer_cert) {
        return Err("timestamp_eku_invalid");
    }
    if !tsa_leaf_profile_acceptable(signer_cert) {
        return Err("timestamp_tsa_leaf_profile_invalid");
    }

    let included_der: Vec<Vec<u8>> = signed_data
        .certificates
        .as_ref()
        .map(|certificates| {
            certificates
                .0
                .iter()
                .filter_map(|choice| match choice {
                    CertificateChoices::Certificate(cert) => cert.to_der().ok(),
                    _ => None,
                })
                .filter(|der| der != &signer_der)
                .collect()
        })
        .unwrap_or_default();
    if tsa_trust.anchors.is_empty() {
        return Err("no_tsa_anchors");
    }
    let chain = validate_chain(
        &signer_der,
        &included_der,
        tsa_trust,
        AnchorPurpose::TimeStamping,
        Some(generated_at),
    );
    if !chain.chain_validity_ok {
        return Err("timestamp_tsa_outside_validity");
    }
    if !chain.trusted {
        return Err("timestamp_tsa_untrusted");
    }

    Ok(TimestampResult::success(
        generated_at,
        common_name(signer_cert),
    ))
}

fn single_attribute<'a>(
    attrs: impl Iterator<Item = &'a Attribute>,
    oid: ObjectIdentifier,
) -> Option<&'a der::Any> {
    let mut matches = attrs.filter(|attr| attr.oid == oid);
    let attr = matches.next()?;
    if matches.next().is_some() || attr.values.len() != 1 {
        return None;
    }
    attr.values.iter().next()
}

fn signer_matches(signer_info: &SignerInfo, cert: &Certificate) -> bool {
    match &signer_info.sid {
        SignerIdentifier::IssuerAndSerialNumber(sid) => {
            cert.tbs_certificate.issuer == sid.issuer
                && cert.tbs_certificate.serial_number == sid.serial_number
        }
        SignerIdentifier::SubjectKeyIdentifier(sid) => cert
            .tbs_certificate
            .extensions
            .as_ref()
            .and_then(|extensions| {
                extensions
                    .iter()
                    .find(|extension| extension.extn_id == OID_SUBJECT_KEY_IDENTIFIER)
            })
            .and_then(|extension| {
                SubjectKeyIdentifier::from_der(extension.extn_value.as_bytes()).ok()
            })
            .map(|cert_sid| cert_sid.0.as_bytes() == sid.0.as_bytes())
            .unwrap_or(false),
    }
}

fn has_strict_timestamping_eku(cert: &Certificate) -> bool {
    let Some(extension) = cert
        .tbs_certificate
        .extensions
        .as_ref()
        .and_then(|extensions| {
            extensions
                .iter()
                .find(|extension| extension.extn_id == OID_EXT_EKU)
        })
    else {
        return false;
    };
    if !extension.critical {
        return false;
    }
    ExtendedKeyUsage::from_der(extension.extn_value.as_bytes())
        .map(|eku| eku.0.len() == 1 && eku.0[0] == OID_KP_TIME_STAMPING_OBJ)
        .unwrap_or(false)
}

/// The C2PA end-entity certificate profile as it applies to a TSA signing
/// certificate. The time-stamping EKU itself is checked by
/// [`has_strict_timestamping_eku`]; this covers the rest of the profile, which
/// the spec applies to every certificate regardless of purpose.
fn tsa_leaf_profile_acceptable(cert: &Certificate) -> bool {
    super::profile::profile_violation(cert, super::profile::CertRole::EndEntity).is_none()
}

fn verify_signer_signature(
    cert: &Certificate,
    signer_info: &SignerInfo,
    digest_hash: HashAlgorithm,
    signed_attrs_der: &[u8],
) -> bool {
    let spki = &cert.tbs_certificate.subject_public_key_info;
    let Some(public_key) = spki.subject_public_key.as_bytes() else {
        return false;
    };
    let signature = signer_info.signature.as_bytes();
    let signature_oid = signer_info.signature_algorithm.oid;

    if spki.algorithm.oid == OID_EC_PUBLIC_KEY {
        let signature_hash = match signature_oid {
            OID_ECDSA_SHA256 => HashAlgorithm::Sha256,
            OID_ECDSA_SHA384 => HashAlgorithm::Sha384,
            OID_ECDSA_SHA512 => HashAlgorithm::Sha512,
            _ => return false,
        };
        if signature_hash != digest_hash {
            return false;
        }
        let Some(curve) = spki
            .algorithm
            .parameters
            .as_ref()
            .and_then(|parameters| parameters.decode_as::<ObjectIdentifier>().ok())
        else {
            return false;
        };
        verify_ecdsa_signature(
            curve,
            signature_hash,
            public_key,
            signature,
            signed_attrs_der,
        )
    } else if spki.algorithm.oid == OID_RSA_ENCRYPTION || spki.algorithm.oid == OID_RSASSA_PSS {
        if signature_oid == OID_RSASSA_PSS {
            let Some(parameters) = signer_info.signature_algorithm.parameters.as_ref() else {
                return false;
            };
            return verify_rsa_pss_signature(
                digest_hash,
                public_key,
                signature,
                signed_attrs_der,
                parameters,
            );
        }
        let signature_hash = match signature_oid {
            OID_RSA_ENCRYPTION => digest_hash,
            OID_RSA_SHA256 => HashAlgorithm::Sha256,
            OID_RSA_SHA384 => HashAlgorithm::Sha384,
            OID_RSA_SHA512 => HashAlgorithm::Sha512,
            _ => return false,
        };
        signature_hash == digest_hash
            && verify_rsa_signature(signature_hash, public_key, signature, signed_attrs_der)
    } else if spki.algorithm.oid == OID_ED25519 && signature_oid == OID_ED25519 {
        verify_ed25519_signature(public_key, signature, signed_attrs_der)
    } else {
        false
    }
}

fn verify_ecdsa_signature(
    curve: ObjectIdentifier,
    hash: HashAlgorithm,
    public_key: &[u8],
    signature_der: &[u8],
    message: &[u8],
) -> bool {
    let prehash = hash.digest(message);
    if curve == OID_CURVE_P256 {
        let (Ok(key), Ok(signature)) = (
            p256::ecdsa::VerifyingKey::from_sec1_bytes(public_key),
            p256::ecdsa::Signature::from_der(signature_der),
        ) else {
            return false;
        };
        key.verify_prehash(&prehash, &signature).is_ok()
    } else if curve == OID_CURVE_P384 {
        let (Ok(key), Ok(signature)) = (
            p384::ecdsa::VerifyingKey::from_sec1_bytes(public_key),
            p384::ecdsa::Signature::from_der(signature_der),
        ) else {
            return false;
        };
        key.verify_prehash(&prehash, &signature).is_ok()
    } else if curve == OID_CURVE_P521 {
        let (Ok(key), Ok(signature)) = (
            p521::ecdsa::VerifyingKey::from_sec1_bytes(public_key),
            p521::ecdsa::Signature::from_der(signature_der),
        ) else {
            return false;
        };
        key.verify_prehash(&prehash, &signature).is_ok()
    } else {
        false
    }
}

fn verify_rsa_signature(
    hash: HashAlgorithm,
    public_key_der: &[u8],
    signature: &[u8],
    message: &[u8],
) -> bool {
    let Ok(public_key) = rsa::RsaPublicKey::from_pkcs1_der(public_key_der) else {
        return false;
    };
    let Ok(signature) = rsa::pkcs1v15::Signature::try_from(signature) else {
        return false;
    };
    match hash {
        HashAlgorithm::Sha256 => rsa::pkcs1v15::VerifyingKey::<Sha256>::new(public_key)
            .verify(message, &signature)
            .is_ok(),
        HashAlgorithm::Sha384 => rsa::pkcs1v15::VerifyingKey::<Sha384>::new(public_key)
            .verify(message, &signature)
            .is_ok(),
        HashAlgorithm::Sha512 => rsa::pkcs1v15::VerifyingKey::<Sha512>::new(public_key)
            .verify(message, &signature)
            .is_ok(),
    }
}

fn verify_rsa_pss_signature(
    digest_hash: HashAlgorithm,
    public_key_der: &[u8],
    signature: &[u8],
    message: &[u8],
    parameters: &der::Any,
) -> bool {
    let Ok(parameters_der) = parameters.to_der() else {
        return false;
    };
    let Ok(parameters) = rsa::pkcs1::RsaPssParams::try_from(parameters_der.as_slice()) else {
        return false;
    };
    let Some(hash) = HashAlgorithm::from_oid(parameters.hash.oid) else {
        return false;
    };
    let Some(mask_hash) = parameters.mask_gen.parameters.as_ref() else {
        return false;
    };
    if hash != digest_hash
        || parameters.mask_gen.oid != OID_MGF1
        || HashAlgorithm::from_oid(mask_hash.oid) != Some(hash)
        || parameters.trailer_field != Default::default()
    {
        return false;
    }
    let Ok(public_key) = rsa::RsaPublicKey::from_pkcs1_der(public_key_der) else {
        return false;
    };
    let Ok(signature) = rsa::pss::Signature::try_from(signature) else {
        return false;
    };
    let salt_len = parameters.salt_len as usize;
    match hash {
        HashAlgorithm::Sha256 => {
            rsa::pss::VerifyingKey::<Sha256>::new_with_salt_len(public_key, salt_len)
                .verify(message, &signature)
                .is_ok()
        }
        HashAlgorithm::Sha384 => {
            rsa::pss::VerifyingKey::<Sha384>::new_with_salt_len(public_key, salt_len)
                .verify(message, &signature)
                .is_ok()
        }
        HashAlgorithm::Sha512 => {
            rsa::pss::VerifyingKey::<Sha512>::new_with_salt_len(public_key, salt_len)
                .verify(message, &signature)
                .is_ok()
        }
    }
}

fn verify_ed25519_signature(public_key: &[u8], signature: &[u8], message: &[u8]) -> bool {
    let Ok(key_bytes): Result<[u8; 32], _> = public_key.try_into() else {
        return false;
    };
    let Ok(key) = ed25519_dalek::VerifyingKey::from_bytes(&key_bytes) else {
        return false;
    };
    let Ok(signature) = ed25519_dalek::Signature::from_slice(signature) else {
        return false;
    };
    key.verify_strict(message, &signature).is_ok()
}

#[cfg(test)]
mod tests {
    use super::*;

    const PAYLOAD: &[u8] = b"C2PA timestamp payload fixture";

    fn token() -> Vec<u8> {
        hex::decode(include_str!("tests/fixtures/rfc3161_token.hex").trim()).unwrap()
    }

    fn trust() -> TrustList {
        TrustList::from_pem_for(
            AnchorPurpose::TimeStamping,
            include_str!("tests/fixtures/rfc3161_root.pem"),
        )
        .unwrap()
    }

    fn verification_time() -> OffsetDateTime {
        OffsetDateTime::from_unix_timestamp(1_785_839_000).unwrap()
    }

    #[test]
    fn verifies_cms_imprint_eku_chain_and_generation_time() {
        let result = verify_timestamp_token(&token(), PAYLOAD, &trust(), verification_time());

        assert_eq!(result.error, None);
        assert!(result.verified);
        assert_eq!(
            result.time.unwrap().unix_timestamp(),
            1_785_838_908,
            "fixture genTime must be returned as the trusted validation instant"
        );
        assert_eq!(result.tsa_common_name.as_deref(), Some("CAWG Fixture TSA"));
    }

    #[test]
    fn rejects_an_imprint_for_different_c2pa_timestamp_input() {
        let result = verify_timestamp_token(
            &token(),
            b"different payload",
            &trust(),
            verification_time(),
        );

        assert!(!result.verified);
        assert_eq!(result.error, Some("timestamp_imprint_mismatch"));
        assert_eq!(result.time, None);
    }

    #[test]
    fn rejects_a_tampered_cms_signature() {
        let mut token = token();
        *token.last_mut().unwrap() ^= 1;
        let result = verify_timestamp_token(&token, PAYLOAD, &trust(), verification_time());

        assert!(!result.verified);
        assert_eq!(result.error, Some("timestamp_signature_invalid"));
    }

    #[test]
    fn rejects_a_valid_token_under_an_unrelated_trust_list() {
        let result = verify_timestamp_token(
            &token(),
            PAYLOAD,
            &TrustList::from_certificates(AnchorPurpose::TimeStamping, [vec![0x30, 0x00]]),
            verification_time(),
        );

        assert!(!result.verified);
        assert_eq!(result.error, Some("timestamp_tsa_untrusted"));
    }

    #[test]
    fn rejects_a_token_generated_after_the_supplied_validation_time() {
        let before_token = OffsetDateTime::from_unix_timestamp(1_785_838_000).unwrap();
        let result = verify_timestamp_token(&token(), PAYLOAD, &trust(), before_token);

        assert!(!result.verified);
        assert_eq!(result.error, Some("timestamp_time_in_future"));
    }
}