Skip to content

Commit baea427

Browse files
authored
Use the DEFINED BY functionality from rust-asn1 in pkcs7.rs (#7848)
1 parent dcada6b commit baea427

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

src/rust/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ rust-version = "1.56.0"
1010
[dependencies]
1111
once_cell = "1"
1212
pyo3 = { version = "0.15.2" }
13-
asn1 = { version = "0.13.0", default-features = false }
13+
asn1 = { version = "0.13.0", default-features = false, features = ["const-generics"] }
1414
pem = "1.1"
1515
chrono = { version = "0.4.24", default-features = false, features = ["alloc", "clock"] }
1616
ouroboros = "0.15"

src/rust/src/pkcs7.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ const AES_256_CBC_OID: asn1::ObjectIdentifier = asn1::oid!(2, 16, 840, 1, 101, 3
2525
const AES_192_CBC_OID: asn1::ObjectIdentifier = asn1::oid!(2, 16, 840, 1, 101, 3, 4, 1, 22);
2626
const AES_128_CBC_OID: asn1::ObjectIdentifier = asn1::oid!(2, 16, 840, 1, 101, 3, 4, 1, 2);
2727

28-
static EMPTY_STRING_DER: Lazy<Vec<u8>> = Lazy::new(|| {
29-
// TODO: kind of verbose way to say "\x04\x00".
30-
asn1::write_single(&(&[] as &[u8])).unwrap()
31-
});
32-
static EMPTY_STRING_TLV: Lazy<asn1::Tlv<'static>> =
33-
Lazy::new(|| asn1::parse_single(&EMPTY_STRING_DER).unwrap());
34-
3528
static OIDS_TO_MIC_NAME: Lazy<HashMap<&asn1::ObjectIdentifier, &str>> = Lazy::new(|| {
3629
let mut h = HashMap::new();
3730
h.insert(&x509::oid::SHA224_OID, "sha-224");
@@ -43,9 +36,18 @@ static OIDS_TO_MIC_NAME: Lazy<HashMap<&asn1::ObjectIdentifier, &str>> = Lazy::ne
4336

4437
#[derive(asn1::Asn1Write)]
4538
struct ContentInfo<'a> {
46-
content_type: asn1::ObjectIdentifier,
47-
#[explicit(0)]
48-
content: Option<asn1::Tlv<'a>>,
39+
_content_type: asn1::DefinedByMarker<asn1::ObjectIdentifier>,
40+
41+
#[defined_by(_content_type)]
42+
content: Content<'a>,
43+
}
44+
45+
#[derive(asn1::Asn1DefinedByWrite)]
46+
enum Content<'a> {
47+
#[defined_by(PKCS7_SIGNED_DATA_OID)]
48+
SignedData(asn1::Explicit<'a, Box<SignedData<'a>>, 0>),
49+
#[defined_by(PKCS7_DATA_OID)]
50+
Data(Option<asn1::Explicit<'a, &'a [u8], 0>>),
4951
}
5052

5153
#[derive(asn1::Asn1Write)]
@@ -106,19 +108,17 @@ fn serialize_certificates<'p>(
106108
version: 1,
107109
digest_algorithms: asn1::SetOfWriter::new(&[]),
108110
content_info: ContentInfo {
109-
content_type: PKCS7_DATA_OID,
110-
content: Some(*EMPTY_STRING_TLV),
111+
_content_type: asn1::DefinedByMarker::marker(),
112+
content: Content::Data(Some(asn1::Explicit::new(b""))),
111113
},
112114
certificates: Some(asn1::SetOfWriter::new(&raw_certs)),
113115
crls: None,
114116
signer_infos: asn1::SetOfWriter::new(&[]),
115117
};
116118

117-
let signed_data_bytes = asn1::write_single(&signed_data)?;
118-
119119
let content_info = ContentInfo {
120-
content_type: PKCS7_SIGNED_DATA_OID,
121-
content: Some(asn1::parse_single(&signed_data_bytes).unwrap()),
120+
_content_type: asn1::DefinedByMarker::marker(),
121+
content: Content::SignedData(asn1::Explicit::new(Box::new(signed_data))),
122122
};
123123
let content_info_bytes = asn1::write_single(&content_info)?;
124124

@@ -276,8 +276,8 @@ fn sign_and_serialize<'p>(
276276
version: 1,
277277
digest_algorithms: asn1::SetOfWriter::new(&digest_algs),
278278
content_info: ContentInfo {
279-
content_type: PKCS7_DATA_OID,
280-
content,
279+
_content_type: asn1::DefinedByMarker::marker(),
280+
content: Content::Data(content.map(asn1::Explicit::new)),
281281
},
282282
certificates: if options.contains(pkcs7_options.getattr(crate::intern!(py, "NoCerts"))?)? {
283283
None
@@ -288,11 +288,9 @@ fn sign_and_serialize<'p>(
288288
signer_infos: asn1::SetOfWriter::new(&signer_infos),
289289
};
290290

291-
let signed_data_bytes = asn1::write_single(&signed_data)?;
292-
293291
let content_info = ContentInfo {
294-
content_type: PKCS7_SIGNED_DATA_OID,
295-
content: Some(asn1::parse_single(&signed_data_bytes).unwrap()),
292+
_content_type: asn1::DefinedByMarker::marker(),
293+
content: Content::SignedData(asn1::Explicit::new(Box::new(signed_data))),
296294
};
297295
let ci_bytes = asn1::write_single(&content_info)?;
298296

0 commit comments

Comments
 (0)