Skip to content

Commit 7a87e11

Browse files
feat: Review c2pa-crypto crate API (#813)
1 parent 1c861b9 commit 7a87e11

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+142
-324
lines changed

internal/crypto/src/asn1/rfc3161.rs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ use crate::asn1::{rfc4210::PkiFreeText, rfc5652::ContentInfo};
2424
/// 1.2.840.113549.1.9.16.1.4
2525
pub const OID_CONTENT_TYPE_TST_INFO: ConstOid = Oid(&[42, 134, 72, 134, 247, 13, 1, 9, 16, 1, 4]);
2626

27-
/// id-aa-timeStampToken
28-
///
29-
/// 1.2.840.113549.1.9.16.2.14
30-
pub const OID_TIME_STAMP_TOKEN: ConstOid = Oid(&[42, 134, 72, 134, 247, 13, 1, 9, 16, 2, 14]);
31-
3227
/// A time-stamp request.
3328
///
3429
/// ```ASN.1
@@ -53,6 +48,7 @@ pub struct TimeStampReq {
5348
}
5449

5550
impl TimeStampReq {
51+
#[allow(dead_code)] // not used on all platforms
5652
pub fn take_from<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
5753
cons.take_sequence(|cons| {
5854
let version = Integer::take_from(cons)?;
@@ -149,17 +145,6 @@ impl TimeStampResp {
149145
})
150146
})
151147
}
152-
153-
pub fn encode_ref(&self) -> impl Values + '_ {
154-
encode::sequence((
155-
self.status.encode_ref(),
156-
if let Some(time_stamp_token) = &self.time_stamp_token {
157-
Some(time_stamp_token)
158-
} else {
159-
None
160-
},
161-
))
162-
}
163148
}
164149

165150
/// PKI status info
@@ -191,16 +176,6 @@ impl PkiStatusInfo {
191176
})
192177
})
193178
}
194-
195-
pub fn encode_ref(&self) -> impl Values + '_ {
196-
encode::sequence((
197-
self.status.encode(),
198-
self.status_string
199-
.as_ref()
200-
.map(|status_string| status_string.encode_ref()),
201-
self.fail_info.as_ref().map(|fail_info| fail_info.encode()),
202-
))
203-
}
204179
}
205180

206181
/// PKI status.

internal/crypto/src/asn1/rfc3281.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ impl AttributeCertificateInfo {
7979
}
8080

8181
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
82+
#[allow(unused)]
8283
pub enum AttCertVersion {
8384
V2 = 1,
8485
}
@@ -104,6 +105,7 @@ pub struct Holder {
104105
}
105106

106107
#[derive(Clone, Debug, Eq, PartialEq)]
108+
#[allow(unused)]
107109
pub enum DigestedObjectType {
108110
PublicKey = 0,
109111
PublicKeyCert = 1,
@@ -142,6 +144,7 @@ pub struct ObjectDigestInfo {
142144
/// }
143145
/// ```
144146
#[derive(Clone, Debug, Eq, PartialEq)]
147+
#[allow(unused)]
145148
pub enum AttCertIssuer {
146149
V1Form(GeneralNames),
147150
V2Form(Box<V2Form>),

internal/crypto/src/asn1/rfc4210.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
99
use bcder::{
1010
decode::{Constructed, DecodeError, Source},
11-
encode::{self, Values},
1211
Tag, Utf8String,
1312
};
1413

@@ -27,10 +26,6 @@ impl PkiFreeText {
2726
cons.take_opt_sequence(|cons| Self::from_sequence(cons))
2827
}
2928

30-
pub fn take_from<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
31-
cons.take_sequence(|cons| Self::from_sequence(cons))
32-
}
33-
3429
pub fn from_sequence<S: Source>(
3530
cons: &mut Constructed<S>,
3631
) -> Result<Self, DecodeError<S::Error>> {
@@ -44,8 +39,4 @@ impl PkiFreeText {
4439

4540
Ok(Self(res))
4641
}
47-
48-
pub fn encode_ref(&self) -> impl Values + '_ {
49-
encode::sequence(encode::slice(&self.0, |x| x.clone().encode()))
50-
}
5142
}

internal/crypto/src/asn1/rfc5652.rs

Lines changed: 6 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -29,43 +29,11 @@ use x509_certificate::{asn1time::*, rfc3280::*, rfc5280::*, rfc5652::*};
2929

3030
use crate::asn1::rfc3281::AttributeCertificate;
3131

32-
/// The data content type.
33-
///
34-
/// `id-data` in the specification.
35-
///
36-
/// 1.2.840.113549.1.7.1
37-
pub const OID_ID_DATA: ConstOid = Oid(&[42, 134, 72, 134, 247, 13, 1, 7, 1]);
38-
3932
/// The signed-data content type.
4033
///
4134
/// 1.2.840.113549.1.7.2
4235
pub const OID_ID_SIGNED_DATA: ConstOid = Oid(&[42, 134, 72, 134, 247, 13, 1, 7, 2]);
4336

44-
/// Enveloped data content type.
45-
///
46-
/// 1.2.840.113549.1.7.3
47-
pub const OID_ENVELOPE_DATA: ConstOid = Oid(&[42, 134, 72, 134, 247, 13, 1, 7, 3]);
48-
49-
/// Digested-data content type.
50-
///
51-
/// 1.2.840.113549.1.7.5
52-
pub const OID_DIGESTED_DATA: ConstOid = Oid(&[42, 134, 72, 134, 247, 13, 1, 7, 5]);
53-
54-
/// Encrypted-data content type.
55-
///
56-
/// 1.2.840.113549.1.7.6
57-
pub const OID_ENCRYPTED_DATA: ConstOid = Oid(&[42, 134, 72, 134, 247, 13, 1, 7, 6]);
58-
59-
/// Authenticated-data content type.
60-
///
61-
/// 1.2.840.113549.1.9.16.1.2
62-
pub const OID_AUTHENTICATED_DATA: ConstOid = Oid(&[42, 134, 72, 134, 247, 13, 1, 9, 16, 1, 2]);
63-
64-
/// Identifies the content-type attribute.
65-
///
66-
/// 1.2.840.113549.1.9.3
67-
pub const OID_CONTENT_TYPE: ConstOid = Oid(&[42, 134, 72, 134, 247, 13, 1, 9, 3]);
68-
6937
/// Identifies the message-digest attribute.
7038
///
7139
/// 1.2.840.113549.1.9.4
@@ -76,11 +44,6 @@ pub const OID_MESSAGE_DIGEST: ConstOid = Oid(&[42, 134, 72, 134, 247, 13, 1, 9,
7644
/// 1.2.840.113549.1.9.5
7745
pub const OID_SIGNING_TIME: ConstOid = Oid(&[42, 134, 72, 134, 247, 13, 1, 9, 5]);
7846

79-
/// Identifies the countersignature attribute.
80-
///
81-
/// 1.2.840.113549.1.9.6
82-
pub const OID_COUNTER_SIGNATURE: ConstOid = Oid(&[42, 134, 72, 134, 247, 13, 1, 9, 6]);
83-
8447
/// Content info.
8548
///
8649
/// ```ASN.1
@@ -158,23 +121,6 @@ pub struct SignedData {
158121
}
159122

160123
impl SignedData {
161-
/// Attempt to decode BER encoded bytes to a parsed data structure.
162-
pub fn decode_ber(data: &[u8]) -> Result<Self, DecodeError<std::convert::Infallible>> {
163-
Constructed::decode(data, bcder::Mode::Ber, Self::decode)
164-
}
165-
166-
pub fn decode<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
167-
cons.take_sequence(|cons| {
168-
let oid = Oid::take_from(cons)?;
169-
170-
if oid != OID_ID_SIGNED_DATA {
171-
return Err(cons.content_err("expected signed data OID"));
172-
}
173-
174-
cons.take_constructed_if(Tag::CTX_0, Self::take_from)
175-
})
176-
}
177-
178124
pub fn take_from<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
179125
cons.take_sequence(|cons| {
180126
let version = CmsVersion::take_from(cons)?;
@@ -197,25 +143,6 @@ impl SignedData {
197143
})
198144
})
199145
}
200-
201-
pub fn encode_ref(&self) -> impl Values + '_ {
202-
encode::sequence((
203-
OID_ID_SIGNED_DATA.encode_ref(),
204-
encode::sequence_as(
205-
Tag::CTX_0,
206-
encode::sequence((
207-
self.version.encode(),
208-
self.digest_algorithms.encode_ref(),
209-
self.content_info.encode_ref(),
210-
self.certificates
211-
.as_ref()
212-
.map(|certs| certs.encode_ref_as(Tag::CTX_0)),
213-
// TODO crls.
214-
self.signer_infos.encode_ref(),
215-
)),
216-
),
217-
))
218-
}
219146
}
220147

221148
/// Digest algorithm identifiers.
@@ -252,10 +179,6 @@ impl DigestAlgorithmIdentifiers {
252179
Ok(Self(identifiers))
253180
})
254181
}
255-
256-
pub fn encode_ref(&self) -> impl Values + '_ {
257-
encode::set(&self.0)
258-
}
259182
}
260183

261184
pub type DigestAlgorithmIdentifier = AlgorithmIdentifier;
@@ -294,10 +217,6 @@ impl SignerInfos {
294217
Ok(Self(infos))
295218
})
296219
}
297-
298-
pub fn encode_ref(&self) -> impl Values + '_ {
299-
encode::set(&self.0)
300-
}
301220
}
302221

303222
/// Encapsulated content info.
@@ -343,15 +262,6 @@ impl EncapsulatedContentInfo {
343262
})
344263
})
345264
}
346-
347-
pub fn encode_ref(&self) -> impl Values + '_ {
348-
encode::sequence((
349-
self.content_type.encode_ref(),
350-
self.content
351-
.as_ref()
352-
.map(|content| encode::sequence_as(Tag::CTX_0, content.encode_ref())),
353-
))
354-
}
355265
}
356266

357267
/// Per-signer information.
@@ -647,10 +557,6 @@ impl DerefMut for SignedAttributes {
647557
}
648558

649559
impl SignedAttributes {
650-
pub fn take_from<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
651-
cons.take_set(|cons| Self::take_from_set(cons))
652-
}
653-
654560
pub fn take_from_set<S: Source>(
655561
cons: &mut Constructed<S>,
656562
) -> Result<Self, DecodeError<S::Error>> {
@@ -759,10 +665,6 @@ impl DerefMut for UnsignedAttributes {
759665
}
760666

761667
impl UnsignedAttributes {
762-
pub fn take_from<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
763-
cons.take_set(|cons| Self::take_from_set(cons))
764-
}
765-
766668
pub fn take_from_set<S: Source>(
767669
cons: &mut Constructed<S>,
768670
) -> Result<Self, DecodeError<S::Error>> {
@@ -846,6 +748,8 @@ pub type UnprotectedAttributes = Vec<Attribute>;
846748
/// ori [4] OtherRecipientInfo }
847749
/// ```
848750
#[derive(Clone, Debug, Eq, PartialEq)]
751+
#[allow(unused)]
752+
#[allow(clippy::enum_variant_names)]
849753
pub enum RecipientInfo {
850754
KeyTransRecipientInfo(KeyTransRecipientInfo),
851755
KeyAgreeRecipientInfo(KeyAgreeRecipientInfo),
@@ -881,6 +785,7 @@ pub struct KeyTransRecipientInfo {
881785
/// subjectKeyIdentifier [0] SubjectKeyIdentifier }
882786
/// ```
883787
#[derive(Clone, Debug, Eq, PartialEq)]
788+
#[allow(unused)]
884789
pub enum RecipientIdentifier {
885790
IssuerAndSerialNumber(IssuerAndSerialNumber),
886791
SubjectKeyIdentifier(SubjectKeyIdentifier),
@@ -914,6 +819,7 @@ pub struct KeyAgreeRecipientInfo {
914819
/// originatorKey [1] OriginatorPublicKey }
915820
/// ```
916821
#[derive(Clone, Debug, Eq, PartialEq)]
822+
#[allow(unused)]
917823
pub enum OriginatorIdentifierOrKey {
918824
IssuerAndSerialNumber(IssuerAndSerialNumber),
919825
SubjectKeyIdentifier(SubjectKeyIdentifier),
@@ -957,6 +863,7 @@ pub struct RecipientEncryptedKey {
957863
/// rKeyId [0] IMPLICIT RecipientKeyIdentifier }
958864
/// ```
959865
#[derive(Clone, Debug, Eq, PartialEq)]
866+
#[allow(unused)]
960867
pub enum KeyAgreeRecipientIdentifier {
961868
IssuerAndSerialNumber(IssuerAndSerialNumber),
962869
RKeyId(RecipientKeyIdentifier),
@@ -1135,6 +1042,7 @@ impl RevocationInfoChoices {
11351042
/// other [1] IMPLICIT OtherRevocationInfoFormat }
11361043
/// ```
11371044
#[derive(Clone, Debug, Eq, PartialEq)]
1045+
#[allow(unused)]
11381046
pub enum RevocationInfoChoice {
11391047
Crl(Box<CertificateList>),
11401048
Other(OtherRevocationInfoFormat),
@@ -1268,10 +1176,6 @@ impl CertificateSet {
12681176

12691177
Ok(Self(certs))
12701178
}
1271-
1272-
pub fn encode_ref_as(&self, tag: Tag) -> impl Values + '_ {
1273-
encode::set_as(tag, &self.0)
1274-
}
12751179
}
12761180

12771181
/// Issuer and serial number.
@@ -1372,10 +1276,6 @@ pub struct OtherKeyAttribute {
13721276

13731277
pub type ContentType = Oid;
13741278

1375-
pub type MessageDigest = OctetString;
1376-
1377-
pub type SigningTime = Time;
1378-
13791279
/// Time variant.
13801280
///
13811281
/// ```ASN.1
@@ -1416,6 +1316,4 @@ impl From<Time> for chrono::DateTime<chrono::Utc> {
14161316
}
14171317
}
14181318

1419-
pub type CounterSignature = SignerInfo;
1420-
14211319
pub type AttributeCertificateV2 = AttributeCertificate;

internal/crypto/src/cose/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@ pub use sign1::{
3939
};
4040

4141
mod sigtst;
42-
pub use sigtst::{
43-
add_sigtst_header, add_sigtst_header_async, cose_countersign_data, parse_and_validate_sigtst,
44-
parse_and_validate_sigtst_async, TstToken,
42+
pub(crate) use sigtst::{
43+
add_sigtst_header, add_sigtst_header_async, validate_cose_tst_info,
44+
validate_cose_tst_info_async,
4545
};
46-
pub(crate) use sigtst::{validate_cose_tst_info, validate_cose_tst_info_async};
4746

4847
mod time_stamp_storage;
4948
pub use time_stamp_storage::TimeStampStorage;
5049

50+
mod validation_info;
51+
pub use validation_info::ValidationInfo;
52+
5153
mod verifier;
5254
pub use verifier::Verifier;

internal/crypto/src/cose/ocsp.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ use crate::{
2727

2828
/// Given a COSE signature, extract the OCSP data and validate the status of
2929
/// that report.
30-
///
31-
/// TO DO: Determine if this needs to remain fully public after refactoring.
3230
#[async_generic]
3331
pub fn check_ocsp_status(
3432
sign1: &CoseSign1,

internal/crypto/src/cose/sign.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ use serde_bytes::ByteBuf;
2323
use crate::{
2424
cose::{add_sigtst_header, add_sigtst_header_async, CoseError, TimeStampStorage},
2525
p1363::{der_to_p1363, parse_ec_der_sig},
26-
raw_signature::{AsyncRawSigner, RawSigner},
27-
SigningAlg,
26+
raw_signature::{AsyncRawSigner, RawSigner, SigningAlg},
2827
};
2928

3029
/// Given an arbitrary block of data and a [`RawSigner`] or [`AsyncRawSigner`]

0 commit comments

Comments
 (0)