Skip to content

Commit 4ab1565

Browse files
committed
Convert PKCS#7 types to GATs
This does not currently work because rust-asn1 doesn't handle Asn1Definedby{Readable,Writable} correctly
1 parent e201c87 commit 4ab1565

File tree

5 files changed

+45
-63
lines changed

5 files changed

+45
-63
lines changed

src/rust/cryptography-x509/src/common.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ impl<T: asn1::SimpleAsn1Writable, U: asn1::SimpleAsn1Writable> asn1::SimpleAsn1W
265265

266266
pub trait Asn1Operation {
267267
type SequenceOfVec<'a, T>
268+
where
269+
T: 'a;
270+
type SetOf<'a, T>
268271
where
269272
T: 'a;
270273
type SetOfVec<'a, T>
@@ -281,6 +284,10 @@ impl Asn1Operation for Asn1Read {
281284
= asn1::SequenceOf<'a, T>
282285
where
283286
T: 'a;
287+
type SetOf<'a, T>
288+
= asn1::SetOf<'a, T>
289+
where
290+
T: 'a;
284291
type SetOfVec<'a, T>
285292
= asn1::SetOf<'a, T>
286293
where
@@ -292,6 +299,10 @@ impl Asn1Operation for Asn1Write {
292299
= asn1::SequenceOfWriter<'a, T, Vec<T>>
293300
where
294301
T: 'a;
302+
type SetOf<'a, T>
303+
= asn1::SetOfWriter<'a, T>
304+
where
305+
T: 'a;
295306
type SetOfVec<'a, T>
296307
= asn1::SetOfWriter<'a, T, Vec<T>>
297308
where

src/rust/cryptography-x509/src/pkcs12.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// 2.0, and the BSD License. See the LICENSE file in the root of this repository
33
// for complete details.
44

5-
use crate::common::{AlgorithmIdentifier, Utf8StoredBMPString};
5+
use crate::common::{AlgorithmIdentifier, Asn1Operation, Utf8StoredBMPString};
66
use crate::pkcs7;
77

88
pub const CERT_BAG_OID: asn1::ObjectIdentifier = asn1::oid!(1, 2, 840, 113549, 1, 12, 10, 1, 3);
@@ -14,9 +14,9 @@ pub const FRIENDLY_NAME_OID: asn1::ObjectIdentifier = asn1::oid!(1, 2, 840, 1135
1414
pub const LOCAL_KEY_ID_OID: asn1::ObjectIdentifier = asn1::oid!(1, 2, 840, 113549, 1, 9, 21);
1515

1616
#[derive(asn1::Asn1Write)]
17-
pub struct Pfx<'a> {
17+
pub struct Pfx<'a, Op: Asn1Operation> {
1818
pub version: u8,
19-
pub auth_safe: pkcs7::ContentInfo<'a>,
19+
pub auth_safe: pkcs7::ContentInfo<'a, Op>,
2020
pub mac_data: Option<MacData<'a>>,
2121
}
2222

src/rust/cryptography-x509/src/pkcs7.rs

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// 2.0, and the BSD License. See the LICENSE file in the root of this repository
33
// for complete details.
44

5+
use crate::common::Asn1Operation;
56
use crate::{certificate, common, csr, name};
67

78
pub const PKCS7_DATA_OID: asn1::ObjectIdentifier = asn1::oid!(1, 2, 840, 113549, 1, 7, 1);
@@ -10,54 +11,38 @@ pub const PKCS7_ENVELOPED_DATA_OID: asn1::ObjectIdentifier = asn1::oid!(1, 2, 84
1011
pub const PKCS7_ENCRYPTED_DATA_OID: asn1::ObjectIdentifier = asn1::oid!(1, 2, 840, 113549, 1, 7, 6);
1112

1213
#[derive(asn1::Asn1Write, asn1::Asn1Read)]
13-
pub struct ContentInfo<'a> {
14+
pub struct ContentInfo<'a, Op: Asn1Operation> {
1415
pub _content_type: asn1::DefinedByMarker<asn1::ObjectIdentifier>,
1516

1617
#[defined_by(_content_type)]
17-
pub content: Content<'a>,
18+
pub content: Content<'a, Op>,
1819
}
1920

2021
#[derive(asn1::Asn1DefinedByWrite, asn1::Asn1DefinedByRead)]
21-
pub enum Content<'a> {
22+
pub enum Content<'a, Op: Asn1Operation> {
2223
#[defined_by(PKCS7_ENVELOPED_DATA_OID)]
23-
EnvelopedData(asn1::Explicit<Box<EnvelopedData<'a>>, 0>),
24+
EnvelopedData(asn1::Explicit<Box<EnvelopedData<'a, Op>>, 0>),
2425
#[defined_by(PKCS7_SIGNED_DATA_OID)]
25-
SignedData(asn1::Explicit<Box<SignedData<'a>>, 0>),
26+
SignedData(asn1::Explicit<Box<SignedData<'a, Op>>, 0>),
2627
#[defined_by(PKCS7_DATA_OID)]
2728
Data(Option<asn1::Explicit<&'a [u8], 0>>),
2829
#[defined_by(PKCS7_ENCRYPTED_DATA_OID)]
2930
EncryptedData(asn1::Explicit<EncryptedData<'a>, 0>),
3031
}
3132

3233
#[derive(asn1::Asn1Write, asn1::Asn1Read)]
33-
pub struct SignedData<'a> {
34+
pub struct SignedData<'a, Op: Asn1Operation> {
3435
pub version: u8,
35-
pub digest_algorithms: common::Asn1ReadableOrWritable<
36-
asn1::SetOf<'a, common::AlgorithmIdentifier<'a>>,
37-
asn1::SetOfWriter<'a, common::AlgorithmIdentifier<'a>>,
38-
>,
39-
pub content_info: ContentInfo<'a>,
36+
pub digest_algorithms: Op::SetOf<'a, common::AlgorithmIdentifier<'a>>,
37+
pub content_info: ContentInfo<'a, Op>,
4038
#[implicit(0)]
41-
pub certificates: Option<
42-
common::Asn1ReadableOrWritable<
43-
asn1::SetOf<'a, certificate::Certificate<'a>>,
44-
asn1::SetOfWriter<'a, certificate::Certificate<'a>>,
45-
>,
46-
>,
39+
pub certificates: Option<Op::SetOf<'a, certificate::Certificate<'a>>>,
4740

4841
// We don't ever supply any of these, so for now, don't fill out the fields.
4942
#[implicit(1)]
50-
pub crls: Option<
51-
common::Asn1ReadableOrWritable<
52-
asn1::SetOf<'a, asn1::Sequence<'a>>,
53-
asn1::SetOfWriter<'a, asn1::Sequence<'a>>,
54-
>,
55-
>,
56-
57-
pub signer_infos: common::Asn1ReadableOrWritable<
58-
asn1::SetOf<'a, SignerInfo<'a>>,
59-
asn1::SetOfWriter<'a, SignerInfo<'a>>,
60-
>,
43+
pub crls: Option<Op::SetOf<'a, asn1::Sequence<'a>>>,
44+
45+
pub signer_infos: Op::SetOf<'a, SignerInfo<'a>>,
6146
}
6247

6348
#[derive(asn1::Asn1Write, asn1::Asn1Read)]
@@ -76,12 +61,9 @@ pub struct SignerInfo<'a> {
7661
}
7762

7863
#[derive(asn1::Asn1Write, asn1::Asn1Read)]
79-
pub struct EnvelopedData<'a> {
64+
pub struct EnvelopedData<'a, Op: Asn1Operation> {
8065
pub version: u8,
81-
pub recipient_infos: common::Asn1ReadableOrWritable<
82-
asn1::SetOf<'a, RecipientInfo<'a>>,
83-
asn1::SetOfWriter<'a, RecipientInfo<'a>>,
84-
>,
66+
pub recipient_infos: Op::SetOf<'a, RecipientInfo<'a>>,
8567
pub encrypted_content_info: EncryptedContentInfo<'a>,
8668
}
8769

src/rust/src/pkcs12.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::error::{CryptographyError, CryptographyResult};
88
use crate::padding::PKCS7PaddingContext;
99
use crate::x509::certificate::Certificate;
1010
use crate::{types, x509};
11-
use cryptography_x509::common::Utf8StoredBMPString;
11+
use cryptography_x509::common::{Asn1Write, Utf8StoredBMPString};
1212
use pyo3::types::{PyAnyMethods, PyBytesMethods, PyListMethods};
1313
use pyo3::IntoPyObject;
1414
use std::collections::hash_map::DefaultHasher;
@@ -577,7 +577,7 @@ fn serialize_key_and_certificates<'p>(
577577
&cert_bag_contents,
578578
)?;
579579

580-
auth_safe_contents.push(cryptography_x509::pkcs7::ContentInfo {
580+
auth_safe_contents.push(cryptography_x509::pkcs7::ContentInfo::<Asn1Write> {
581581
_content_type: asn1::DefinedByMarker::marker(),
582582
content: cryptography_x509::pkcs7::Content::EncryptedData(asn1::Explicit::new(
583583
cryptography_x509::pkcs7::EncryptedData {
@@ -595,7 +595,7 @@ fn serialize_key_and_certificates<'p>(
595595
)),
596596
})
597597
} else {
598-
auth_safe_contents.push(cryptography_x509::pkcs7::ContentInfo {
598+
auth_safe_contents.push(cryptography_x509::pkcs7::ContentInfo::<Asn1Write> {
599599
_content_type: asn1::DefinedByMarker::marker(),
600600
content: cryptography_x509::pkcs7::Content::Data(Some(asn1::Explicit::new(
601601
&cert_bag_contents,

src/rust/src/pkcs7.rs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::borrow::Cow;
66
use std::collections::HashMap;
77
use std::ops::Deref;
88

9-
use cryptography_x509::common::{AlgorithmIdentifier, AlgorithmParameters};
9+
use cryptography_x509::common::{AlgorithmIdentifier, AlgorithmParameters, Asn1Read, Asn1Write};
1010
use cryptography_x509::csr::Attribute;
1111
use cryptography_x509::pkcs7::PKCS7_DATA_OID;
1212
use cryptography_x509::{common, oid, pkcs7};
@@ -59,19 +59,17 @@ fn serialize_certificates<'p>(
5959

6060
let signed_data = pkcs7::SignedData {
6161
version: 1,
62-
digest_algorithms: common::Asn1ReadableOrWritable::new_write(asn1::SetOfWriter::new(&[])),
62+
digest_algorithms: asn1::SetOfWriter::new([].as_slice()),
6363
content_info: pkcs7::ContentInfo {
6464
_content_type: asn1::DefinedByMarker::marker(),
6565
content: pkcs7::Content::Data(None),
6666
},
67-
certificates: Some(common::Asn1ReadableOrWritable::new_write(
68-
asn1::SetOfWriter::new(&raw_certs),
69-
)),
67+
certificates: Some(asn1::SetOfWriter::new(raw_certs.as_slice())),
7068
crls: None,
71-
signer_infos: common::Asn1ReadableOrWritable::new_write(asn1::SetOfWriter::new(&[])),
69+
signer_infos: asn1::SetOfWriter::new([].as_slice()),
7270
};
7371

74-
let content_info = pkcs7::ContentInfo {
72+
let content_info = pkcs7::ContentInfo::<Asn1Write> {
7573
_content_type: asn1::DefinedByMarker::marker(),
7674
content: pkcs7::Content::SignedData(asn1::Explicit::new(Box::new(signed_data))),
7775
};
@@ -135,9 +133,7 @@ fn encrypt_and_serialize<'p>(
135133

136134
let enveloped_data = pkcs7::EnvelopedData {
137135
version: 0,
138-
recipient_infos: common::Asn1ReadableOrWritable::new_write(asn1::SetOfWriter::new(
139-
&recipient_infos,
140-
)),
136+
recipient_infos: asn1::SetOfWriter::new(recipient_infos.as_slice()),
141137

142138
encrypted_content_info: pkcs7::EncryptedContentInfo {
143139
content_type: PKCS7_DATA_OID,
@@ -149,7 +145,7 @@ fn encrypt_and_serialize<'p>(
149145
},
150146
};
151147

152-
let content_info = pkcs7::ContentInfo {
148+
let content_info = pkcs7::ContentInfo::<Asn1Write> {
153149
_content_type: asn1::DefinedByMarker::marker(),
154150
content: pkcs7::Content::EnvelopedData(asn1::Explicit::new(Box::new(enveloped_data))),
155151
};
@@ -218,18 +214,17 @@ fn decrypt_der<'p>(
218214
check_decrypt_parameters(py, &certificate, &private_key, options)?;
219215

220216
// Decrypt the data
221-
let content_info = asn1::parse_single::<pkcs7::ContentInfo<'_>>(data)?;
217+
let content_info = asn1::parse_single::<pkcs7::ContentInfo<'_, Asn1Read>>(data)?;
222218
let plain_content = match content_info.content {
223219
pkcs7::Content::EnvelopedData(data) => {
224220
// Extract enveloped data
225221
let enveloped_data = data.into_inner();
226222

227223
// Get recipients, and the one matching with the given certificate (if any)
228-
let mut recipient_infos = enveloped_data.recipient_infos.unwrap_read().clone();
229224
let recipient_certificate = certificate.get().raw.borrow_dependent();
230225
let recipient_serial_number = recipient_certificate.tbs_cert.serial;
231226
let recipient_issuer = recipient_certificate.tbs_cert.issuer.clone();
232-
let found_recipient_info = recipient_infos.find(|info| {
227+
let found_recipient_info = enveloped_data.recipient_infos.find(|info| {
233228
info.issuer_and_serial_number.serial_number == recipient_serial_number
234229
&& info.issuer_and_serial_number.issuer == recipient_issuer
235230
});
@@ -580,27 +575,21 @@ fn sign_and_serialize<'p>(
580575

581576
let signed_data = pkcs7::SignedData {
582577
version: 1,
583-
digest_algorithms: common::Asn1ReadableOrWritable::new_write(asn1::SetOfWriter::new(
584-
&digest_algs,
585-
)),
578+
digest_algorithms: asn1::SetOfWriter::new(digest_algs.as_slice()),
586579
content_info: pkcs7::ContentInfo {
587580
_content_type: asn1::DefinedByMarker::marker(),
588581
content: pkcs7::Content::Data(content.map(asn1::Explicit::new)),
589582
},
590583
certificates: if options.contains(types::PKCS7_NO_CERTS.get(py)?)? {
591584
None
592585
} else {
593-
Some(common::Asn1ReadableOrWritable::new_write(
594-
asn1::SetOfWriter::new(&certs),
595-
))
586+
Some(asn1::SetOfWriter::new(certs.as_slice()))
596587
},
597588
crls: None,
598-
signer_infos: common::Asn1ReadableOrWritable::new_write(asn1::SetOfWriter::new(
599-
&signer_infos,
600-
)),
589+
signer_infos: asn1::SetOfWriter::new(signer_infos.as_slice()),
601590
};
602591

603-
let content_info = pkcs7::ContentInfo {
592+
let content_info = pkcs7::ContentInfo::<Asn1Write> {
604593
_content_type: asn1::DefinedByMarker::marker(),
605594
content: pkcs7::Content::SignedData(asn1::Explicit::new(Box::new(signed_data))),
606595
};

0 commit comments

Comments
 (0)