Skip to content

Commit e28a8ea

Browse files
committed
[wip] pkcs1: make sure AlgorithmIdentifier is encoded with Any
reverts parts of #799 and #1010
1 parent 7203ea6 commit e28a8ea

File tree

1 file changed

+63
-58
lines changed

1 file changed

+63
-58
lines changed

pkcs1/src/params.rs

+63-58
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
use crate::{Error, Result};
44
use der::{
55
asn1::{AnyRef, ContextSpecificRef, ObjectIdentifier},
6-
oid::AssociatedOid,
76
Decode, DecodeValue, Encode, EncodeValue, FixedTag, Length, Reader, Sequence, Tag, TagMode,
87
TagNumber, Writer,
98
};
10-
use spki::{AlgorithmIdentifier, AlgorithmIdentifierRef};
9+
use spki::AlgorithmIdentifierRef;
1110

1211
const OID_SHA_1: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.3.14.3.2.26");
1312
const OID_MGF_1: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.8");
1413
const OID_PSPECIFIED: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.9");
1514

15+
// TODO(tarcieri): make `AlgorithmIdentifier` generic around params; use `OID_SHA_1`
16+
const SEQ_OID_SHA_1_DER: &[u8] = &[0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a];
17+
1618
const SHA_1_AI: AlgorithmIdentifierRef<'_> = AlgorithmIdentifierRef {
1719
oid: OID_SHA_1,
1820
parameters: Some(AnyRef::NULL),
@@ -82,7 +84,7 @@ pub struct RsaPssParams<'a> {
8284
pub hash: AlgorithmIdentifierRef<'a>,
8385

8486
/// Mask Generation Function (MGF)
85-
pub mask_gen: AlgorithmIdentifier<AlgorithmIdentifierRef<'a>>,
87+
pub mask_gen: AlgorithmIdentifierRef<'a>,
8688

8789
/// Salt length
8890
pub salt_len: u8,
@@ -95,27 +97,27 @@ impl<'a> RsaPssParams<'a> {
9597
/// Default RSA PSS Salt length in RsaPssParams
9698
pub const SALT_LEN_DEFAULT: u8 = 20;
9799

98-
/// Create new RsaPssParams for the provided digest and salt len
99-
pub fn new<D>(salt_len: u8) -> Self
100-
where
101-
D: AssociatedOid,
102-
{
103-
Self {
104-
hash: AlgorithmIdentifierRef {
105-
oid: D::OID,
106-
parameters: Some(AnyRef::NULL),
107-
},
108-
mask_gen: AlgorithmIdentifier {
109-
oid: OID_MGF_1,
110-
parameters: Some(AlgorithmIdentifierRef {
111-
oid: D::OID,
112-
parameters: Some(AnyRef::NULL),
113-
}),
114-
},
115-
salt_len,
116-
trailer_field: Default::default(),
117-
}
118-
}
100+
// /// Create new RsaPssParams for the provided digest and salt len
101+
// pub fn new<D>(salt_len: u8) -> Self
102+
// where
103+
// D: AssociatedOid,
104+
// {
105+
// Self {
106+
// hash: AlgorithmIdentifierRef {
107+
// oid: D::OID,
108+
// parameters: Some(AnyRef::NULL),
109+
// },
110+
// mask_gen: AlgorithmIdentifier {
111+
// oid: OID_MGF_1,
112+
// parameters: Some(AlgorithmIdentifierRef {
113+
// oid: D::OID,
114+
// parameters: Some(AnyRef::NULL),
115+
// }),
116+
// },
117+
// salt_len,
118+
// trailer_field: Default::default(),
119+
// }
120+
// }
119121

120122
fn context_specific_hash(&self) -> Option<ContextSpecificRef<'_, AlgorithmIdentifierRef<'a>>> {
121123
if self.hash == SHA_1_AI {
@@ -131,7 +133,7 @@ impl<'a> RsaPssParams<'a> {
131133

132134
fn context_specific_mask_gen(
133135
&self,
134-
) -> Option<ContextSpecificRef<'_, AlgorithmIdentifier<AlgorithmIdentifierRef<'a>>>> {
136+
) -> Option<ContextSpecificRef<'_, AlgorithmIdentifierRef<'a>>> {
135137
if self.mask_gen == default_mgf1_sha1() {
136138
None
137139
} else {
@@ -230,10 +232,13 @@ impl<'a> TryFrom<&'a [u8]> for RsaPssParams<'a> {
230232
}
231233

232234
/// Default Mask Generation Function (MGF): SHA-1.
233-
fn default_mgf1_sha1<'a>() -> AlgorithmIdentifier<AlgorithmIdentifierRef<'a>> {
234-
AlgorithmIdentifier::<AlgorithmIdentifierRef<'a>> {
235+
fn default_mgf1_sha1<'a>() -> AlgorithmIdentifierRef<'a> {
236+
AlgorithmIdentifierRef {
235237
oid: OID_MGF_1,
236-
parameters: Some(SHA_1_AI),
238+
parameters: Some(
239+
AnyRef::new(Tag::Sequence, SEQ_OID_SHA_1_DER)
240+
.expect("error creating default MGF1 params"),
241+
),
237242
}
238243
}
239244

@@ -258,41 +263,41 @@ pub struct RsaOaepParams<'a> {
258263
pub hash: AlgorithmIdentifierRef<'a>,
259264

260265
/// Mask Generation Function (MGF)
261-
pub mask_gen: AlgorithmIdentifier<AlgorithmIdentifierRef<'a>>,
266+
pub mask_gen: AlgorithmIdentifierRef<'a>,
262267

263268
/// The source (and possibly the value) of the label L
264269
pub p_source: AlgorithmIdentifierRef<'a>,
265270
}
266271

267272
impl<'a> RsaOaepParams<'a> {
268-
/// Create new RsaPssParams for the provided digest and default (empty) label
269-
pub fn new<D>() -> Self
270-
where
271-
D: AssociatedOid,
272-
{
273-
Self::new_with_label::<D>(&[])
274-
}
275-
276-
/// Create new RsaPssParams for the provided digest and specified label
277-
pub fn new_with_label<D>(label: &'a impl AsRef<[u8]>) -> Self
278-
where
279-
D: AssociatedOid,
280-
{
281-
Self {
282-
hash: AlgorithmIdentifierRef {
283-
oid: D::OID,
284-
parameters: Some(AnyRef::NULL),
285-
},
286-
mask_gen: AlgorithmIdentifier {
287-
oid: OID_MGF_1,
288-
parameters: Some(AlgorithmIdentifierRef {
289-
oid: D::OID,
290-
parameters: Some(AnyRef::NULL),
291-
}),
292-
},
293-
p_source: pspecicied_algorithm_identifier(label),
294-
}
295-
}
273+
// /// Create new RsaPssParams for the provided digest and default (empty) label
274+
// pub fn new<D>() -> Self
275+
// where
276+
// D: AssociatedOid,
277+
// {
278+
// Self::new_with_label::<D>(&[])
279+
// }
280+
//
281+
// /// Create new RsaPssParams for the provided digest and specified label
282+
// pub fn new_with_label<D>(label: &'a impl AsRef<[u8]>) -> Self
283+
// where
284+
// D: AssociatedOid,
285+
// {
286+
// Self {
287+
// hash: AlgorithmIdentifierRef {
288+
// oid: D::OID,
289+
// parameters: Some(AnyRef::NULL),
290+
// },
291+
// mask_gen: AlgorithmIdentifier {
292+
// oid: OID_MGF_1,
293+
// parameters: Some(AlgorithmIdentifierRef {
294+
// oid: D::OID,
295+
// parameters: Some(AnyRef::NULL),
296+
// }),
297+
// },
298+
// p_source: pspecicied_algorithm_identifier(label),
299+
// }
300+
// }
296301

297302
fn context_specific_hash(&self) -> Option<ContextSpecificRef<'_, AlgorithmIdentifierRef<'a>>> {
298303
if self.hash == SHA_1_AI {
@@ -308,7 +313,7 @@ impl<'a> RsaOaepParams<'a> {
308313

309314
fn context_specific_mask_gen(
310315
&self,
311-
) -> Option<ContextSpecificRef<'_, AlgorithmIdentifier<AlgorithmIdentifierRef<'a>>>> {
316+
) -> Option<ContextSpecificRef<'_, AlgorithmIdentifierRef<'a>>> {
312317
if self.mask_gen == default_mgf1_sha1() {
313318
None
314319
} else {

0 commit comments

Comments
 (0)