Skip to content

Commit a4ad7d0

Browse files
authored
der-derive: v0.7.3 (#1443)
* der-derive: avoid type inference when using default (#1442) * Revert "x509-cert: specify concrete types to help the compiler (#1441)" This reverts commit 7a2d38a. * der-derive: avoid type inference when using default This is another take on #1441. This is intended to make sure we can still continue to use `Default::default` and not break future releases by mistake. * der-derive: v0.7.3 Changed (2024-07-09) - avoid type inference when using default (#1443) * rust 1.78 fixups This fixes all `unnecessary qualification` warnings * x509-cert: ignore zlint error, fixed in main * x509-cert: fuzz: ignore unused_qualification errors
1 parent 9bf8809 commit a4ad7d0

File tree

15 files changed

+101
-35
lines changed

15 files changed

+101
-35
lines changed

.github/workflows/x509-cert.yml

+3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ jobs:
6666

6767
fuzz:
6868
runs-on: ubuntu-latest
69+
env:
70+
# ignore unused_qualification errors
71+
RUSTFLAGS: ""
6972
steps:
7073
- uses: actions/checkout@v3
7174
- uses: dtolnay/rust-toolchain@nightly

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

base16ct/src/error.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ impl fmt::Display for Error {
2525
#[cfg(feature = "std")]
2626
impl std::error::Error for Error {}
2727

28-
impl From<Error> for core::fmt::Error {
29-
fn from(_: Error) -> core::fmt::Error {
30-
core::fmt::Error::default()
28+
impl From<Error> for fmt::Error {
29+
fn from(_: Error) -> fmt::Error {
30+
fmt::Error::default()
3131
}
3232
}

base16ct/src/upper.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub fn encode_string(input: &[u8]) -> String {
4646
let res = encode(input, &mut dst).expect("dst length is correct");
4747

4848
debug_assert_eq!(elen, res.len());
49-
unsafe { crate::String::from_utf8_unchecked(dst) }
49+
unsafe { String::from_utf8_unchecked(dst) }
5050
}
5151

5252
/// Decode a single nibble of upper hex

der/derive/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## 0.7.3 (2024-07-09)
8+
### Changed
9+
- avoid type inference when using default ([#1443])
10+
11+
[#1443]: https://github.com/RustCrypto/formats/pull/1443
12+
713
## 0.7.2 (2023-08-07)
814
### Changed
915
- fix doc typo and use a valid tag number ([#1184])

der/derive/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "der_derive"
3-
version = "0.7.2"
3+
version = "0.7.3"
44
description = "Custom derive support for the `der` crate's `Choice` and `Sequence` traits"
55
authors = ["RustCrypto Developers"]
66
license = "Apache-2.0 OR MIT"

der/derive/src/sequence/field.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl SequenceField {
9797
!attrs.optional,
9898
"`default`, and `optional` are mutually exclusive"
9999
);
100-
lowerer.apply_default(&self.ident, default);
100+
lowerer.apply_default(&self.ident, default, &self.field_type);
101101
}
102102

103103
lowerer.into_tokens()
@@ -189,14 +189,17 @@ impl LowerFieldEncoder {
189189
}
190190

191191
/// Handle default value for a type.
192-
fn apply_default(&mut self, ident: &Ident, default: &Path) {
192+
fn apply_default(&mut self, ident: &Ident, default: &Path, field_type: &Type) {
193193
let encoder = &self.encoder;
194194

195195
self.encoder = quote! {
196-
if &self.#ident == &#default() {
197-
None
198-
} else {
199-
Some(#encoder)
196+
{
197+
let default_value: #field_type = #default();
198+
if &self.#ident == &default_value {
199+
None
200+
} else {
201+
Some(#encoder)
202+
}
200203
}
201204
};
202205
}

der/tests/derive.rs

+56
Original file line numberDiff line numberDiff line change
@@ -459,3 +459,59 @@ mod sequence {
459459
);
460460
}
461461
}
462+
463+
mod infer_default {
464+
//! When another crate might define a PartialEq for another type, the use of
465+
//! `default="Default::default"` in the der derivation will not provide enough
466+
//! information for `der_derive` crate to figure out.
467+
//!
468+
//! This provides a reproduction for that case. This is intended to fail when we
469+
//! compile tests.
470+
//! ```
471+
//! error[E0282]: type annotations needed
472+
//! --> der/tests/derive.rs:480:26
473+
//! |
474+
//!480 | #[asn1(default = "Default::default")]
475+
//! | ^^^^^^^^^^^^^^^^^^ cannot infer type
476+
//!
477+
//!error[E0283]: type annotations needed
478+
//! --> der/tests/derive.rs:478:14
479+
//! |
480+
//!478 | #[derive(Sequence)]
481+
//! | ^^^^^^^^ cannot infer type
482+
//! |
483+
//!note: multiple `impl`s satisfying `bool: PartialEq<_>` found
484+
//! --> der/tests/derive.rs:472:5
485+
//! |
486+
//!472 | impl PartialEq<BooleanIsh> for bool {
487+
//! | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
488+
//! = note: and another `impl` found in the `core` crate:
489+
//! - impl<host> PartialEq for bool
490+
//! where the constant `host` has type `bool`;
491+
//! = note: required for `&bool` to implement `PartialEq<&_>`
492+
//! = note: this error originates in the derive macro `Sequence` (in Nightly builds, run with -Z macro-backtrace for more info)
493+
//! ```
494+
495+
use der::Sequence;
496+
497+
struct BooleanIsh;
498+
499+
impl PartialEq<BooleanIsh> for bool {
500+
fn eq(&self, _other: &BooleanIsh) -> bool {
501+
unimplemented!("This is only here to mess up the compiler's type inference")
502+
}
503+
}
504+
505+
#[derive(Sequence)]
506+
struct Foo {
507+
#[asn1(default = "Default::default")]
508+
pub use_default_default: bool,
509+
510+
#[asn1(default = "something_true")]
511+
pub use_custom: bool,
512+
}
513+
514+
fn something_true() -> bool {
515+
todo!()
516+
}
517+
}

pkcs1/src/error.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,18 @@ impl From<pkcs8::Error> for Error {
7575
}
7676

7777
#[cfg(feature = "pkcs8")]
78-
impl From<Error> for pkcs8::spki::Error {
79-
fn from(err: Error) -> pkcs8::spki::Error {
78+
impl From<Error> for spki::Error {
79+
fn from(err: Error) -> spki::Error {
8080
match err {
81-
Error::Asn1(e) => pkcs8::spki::Error::Asn1(e),
82-
_ => pkcs8::spki::Error::KeyMalformed,
81+
Error::Asn1(e) => spki::Error::Asn1(e),
82+
_ => spki::Error::KeyMalformed,
8383
}
8484
}
8585
}
8686

8787
#[cfg(feature = "pkcs8")]
88-
impl From<pkcs8::spki::Error> for Error {
89-
fn from(err: pkcs8::spki::Error) -> Error {
88+
impl From<spki::Error> for Error {
89+
fn from(err: spki::Error) -> Error {
9090
Error::Pkcs8(pkcs8::Error::PublicKey(err))
9191
}
9292
}

pkcs1/src/traits.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ where
171171
#[cfg(feature = "pkcs8")]
172172
impl<T> DecodeRsaPublicKey for T
173173
where
174-
T: for<'a> TryFrom<pkcs8::SubjectPublicKeyInfoRef<'a>, Error = pkcs8::spki::Error>,
174+
T: for<'a> TryFrom<pkcs8::SubjectPublicKeyInfoRef<'a>, Error = spki::Error>,
175175
{
176176
fn from_pkcs1_der(public_key: &[u8]) -> Result<Self> {
177177
Ok(Self::try_from(pkcs8::SubjectPublicKeyInfoRef {

pkcs5/src/pbes1.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ impl<'a> TryFrom<AlgorithmIdentifierRef<'a>> for Algorithm {
9191

9292
fn try_from(alg: AlgorithmIdentifierRef<'a>) -> der::Result<Self> {
9393
// Ensure that we have a supported PBES1 algorithm identifier
94-
let encryption = EncryptionScheme::try_from(alg.oid)
95-
.map_err(|_| der::Tag::ObjectIdentifier.value_error())?;
94+
let encryption =
95+
EncryptionScheme::try_from(alg.oid).map_err(|_| Tag::ObjectIdentifier.value_error())?;
9696

9797
let parameters = alg
9898
.parameters
@@ -153,7 +153,7 @@ impl TryFrom<AnyRef<'_>> for Parameters {
153153
salt: OctetStringRef::decode(reader)?
154154
.as_bytes()
155155
.try_into()
156-
.map_err(|_| der::Tag::OctetString.value_error())?,
156+
.map_err(|_| Tag::OctetString.value_error())?,
157157
iteration_count: reader.decode()?,
158158
})
159159
})

pkcs5/src/pbes2.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -318,31 +318,25 @@ impl<'a> TryFrom<AlgorithmIdentifierRef<'a>> for EncryptionScheme<'a> {
318318

319319
match alg.oid {
320320
AES_128_CBC_OID => Ok(Self::Aes128Cbc {
321-
iv: iv
322-
.try_into()
323-
.map_err(|_| der::Tag::OctetString.value_error())?,
321+
iv: iv.try_into().map_err(|_| Tag::OctetString.value_error())?,
324322
}),
325323
AES_192_CBC_OID => Ok(Self::Aes192Cbc {
326-
iv: iv
327-
.try_into()
328-
.map_err(|_| der::Tag::OctetString.value_error())?,
324+
iv: iv.try_into().map_err(|_| Tag::OctetString.value_error())?,
329325
}),
330326
AES_256_CBC_OID => Ok(Self::Aes256Cbc {
331-
iv: iv
332-
.try_into()
333-
.map_err(|_| der::Tag::OctetString.value_error())?,
327+
iv: iv.try_into().map_err(|_| Tag::OctetString.value_error())?,
334328
}),
335329
#[cfg(feature = "des-insecure")]
336330
DES_CBC_OID => Ok(Self::DesCbc {
337331
iv: iv[0..DES_BLOCK_SIZE]
338332
.try_into()
339-
.map_err(|_| der::Tag::OctetString.value_error())?,
333+
.map_err(|_| Tag::OctetString.value_error())?,
340334
}),
341335
#[cfg(feature = "3des")]
342336
DES_EDE3_CBC_OID => Ok(Self::DesEde3Cbc {
343337
iv: iv[0..DES_BLOCK_SIZE]
344338
.try_into()
345-
.map_err(|_| der::Tag::OctetString.value_error())?,
339+
.map_err(|_| Tag::OctetString.value_error())?,
346340
}),
347341
oid => Err(ErrorKind::OidUnknown { oid }.into()),
348342
}

sec1/src/private_key.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl<'a> DecodeValue<'a> for EcPrivateKey<'a> {
9898
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> der::Result<Self> {
9999
reader.read_nested(header.length, |reader| {
100100
if u8::decode(reader)? != VERSION {
101-
return Err(der::Tag::Integer.value_error());
101+
return Err(Tag::Integer.value_error());
102102
}
103103

104104
let private_key = OctetStringRef::decode(reader)?.as_bytes();

x509-cert/src/request.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ pub mod attributes {
142142
pub trait AsAttribute: AssociatedOid + Tagged + EncodeValue + Sized {
143143
/// Returns the Attribute with the content encoded.
144144
fn to_attribute(&self) -> Result<Attribute> {
145-
let inner: Any = der::asn1::Any::encode_from(self)?;
145+
let inner: Any = Any::encode_from(self)?;
146146

147147
let values = SetOfVec::try_from(vec![inner])?;
148148

x509-cert/tests/builder.rs

+4
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ fn leaf_certificate() {
165165
"e_subject_common_name_not_exactly_from_san",
166166
// Extended key usage needs to be added by end-user and is use-case dependent
167167
"e_sub_cert_eku_missing",
168+
// Zlint got updated, fixed in master
169+
"w_subject_common_name_included",
168170
];
169171

170172
zlint::check_certificate(pem.as_bytes(), &ignored);
@@ -242,6 +244,8 @@ fn pss_certificate() {
242244
"e_sub_cert_eku_missing",
243245
// zlint warns on RSAPSS signature algorithms
244246
"e_signature_algorithm_not_supported",
247+
// Zlint got updated, fixed in master
248+
"w_subject_common_name_included",
245249
];
246250

247251
zlint::check_certificate(pem.as_bytes(), ignored);

0 commit comments

Comments
 (0)