Skip to content

Commit fe0fdfb

Browse files
authored
spki: enable workspace-level lint config (#2231) (#2275)
Applies the workspace-level `clippy` and other lints added in #2231 for the `spki` crate, then fixes the lint failures.
1 parent 223f522 commit fe0fdfb

File tree

9 files changed

+74
-25
lines changed

9 files changed

+74
-25
lines changed

spki/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ base64 = ["dep:base64ct"]
3737
fingerprint = ["digest", "sha2"]
3838
pem = ["alloc", "der/pem"]
3939

40+
[lints]
41+
workspace = true
42+
4043
[package.metadata.docs.rs]
4144
all-features = true
42-
rustdoc-args = ["--cfg", "docsrs"]

spki/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[![Project Chat][chat-image]][chat-link]
99

1010
[X.509] Subject Public Key Info types describing public keys as well as their
11-
associated AlgorithmIdentifiers (i.e. OIDs).
11+
associated `AlgorithmIdentifier`s (i.e. OIDs).
1212

1313
Specified in [RFC 5280 § 4.1].
1414

spki/src/algorithm.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ pub type AlgorithmIdentifierOwned = AlgorithmIdentifier<Any>;
100100

101101
impl<Params> AlgorithmIdentifier<Params> {
102102
/// Assert the `algorithm` OID is an expected value.
103+
///
104+
/// # Errors
105+
/// Returns [`Error::OidUnknown`] if `self` does not match `expected_oid`.
103106
pub fn assert_algorithm_oid(&self, expected_oid: ObjectIdentifier) -> Result<ObjectIdentifier> {
104107
if self.oid == expected_oid {
105108
Ok(expected_oid)
@@ -111,6 +114,9 @@ impl<Params> AlgorithmIdentifier<Params> {
111114

112115
impl<'a> AlgorithmIdentifierRef<'a> {
113116
/// Assert `parameters` is an OID and has the expected value.
117+
///
118+
/// # Errors
119+
/// Returns [`Error::OidUnknown`] if `self.parameters_oid()` doesn't match `expected_oid`.
114120
pub fn assert_parameters_oid(
115121
&self,
116122
expected_oid: ObjectIdentifier,
@@ -125,6 +131,9 @@ impl<'a> AlgorithmIdentifierRef<'a> {
125131
}
126132

127133
/// Assert the values of the `algorithm` and `parameters` OIDs.
134+
///
135+
/// # Errors
136+
/// Returns [`Error::OidUnknown`] if `algorithm` and/or `parameters` aren't the expected values.
128137
pub fn assert_oids(
129138
&self,
130139
algorithm: ObjectIdentifier,
@@ -137,14 +146,17 @@ impl<'a> AlgorithmIdentifierRef<'a> {
137146

138147
/// Get the `parameters` field as an [`AnyRef`].
139148
///
140-
/// Returns an error if `parameters` are `None`.
149+
/// # Errors
150+
/// Returns [`Error::AlgorithmParametersMissing`] error if `self.parameters` are `None`.
141151
pub fn parameters_any(&self) -> Result<AnyRef<'a>> {
142152
self.parameters.ok_or(Error::AlgorithmParametersMissing)
143153
}
144154

145155
/// Get the `parameters` field as an [`ObjectIdentifier`].
146156
///
147-
/// Returns an error if it is absent or not an OID.
157+
/// # Errors
158+
/// - Returns [`Error::AlgorithmParametersMissing`] error if `self.parameters` are `None`.
159+
/// - Returns [`Error::Asn1`] if `self.parameters` is not an OID.
148160
pub fn parameters_oid(&self) -> Result<ObjectIdentifier> {
149161
Ok(ObjectIdentifier::try_from(self.parameters_any()?)?)
150162
}
@@ -155,6 +167,7 @@ impl<'a> AlgorithmIdentifierRef<'a> {
155167
/// particular that `NULL` parameters are treated the same as missing
156168
/// parameters.
157169
///
170+
/// # Errors
158171
/// Returns an error if parameters are present but not an OID.
159172
pub fn oids(&self) -> der::Result<(ObjectIdentifier, Option<ObjectIdentifier>)> {
160173
Ok((

spki/src/digest.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use der::{Result, Writer};
22
use digest::Digest;
33

44
/// Adapter object to write to a digest backend
5+
#[derive(Debug)]
56
pub struct DigestWriter<'d, D>(pub &'d mut D);
67

78
impl<D> Writer for DigestWriter<'_, D>

spki/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub enum Error {
3333
}
3434

3535
impl core::error::Error for Error {
36-
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
36+
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
3737
match self {
3838
Error::Asn1(err) => Some(err),
3939
_ => None,

spki/src/lib.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,7 @@
66
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
77
)]
88
#![forbid(unsafe_code)]
9-
#![warn(
10-
clippy::mod_module_files,
11-
clippy::unwrap_used,
12-
missing_docs,
13-
rust_2018_idioms,
14-
unused_lifetimes,
15-
unused_qualifications
16-
)]
9+
1710
//! # Usage
1811
//! The following example demonstrates how to use an OID as the `parameters`
1912
//! of an [`AlgorithmIdentifier`].

spki/src/spki.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ where
6969
///
7070
/// See [RFC7469 § 2.1.1] for more information.
7171
///
72+
/// # Errors
73+
/// Propagates errors that occur during encoding.
74+
///
7275
/// [RFC7469 § 2.1.1]: https://datatracker.ietf.org/doc/html/rfc7469#section-2.1.1
7376
#[cfg(all(feature = "fingerprint", feature = "alloc", feature = "base64"))]
7477
pub fn fingerprint_base64(&self) -> Result<alloc::string::String> {
@@ -81,6 +84,9 @@ where
8184
///
8285
/// See [RFC7469 § 2.1.1] for more information.
8386
///
87+
/// # Errors
88+
/// Propagates errors that occur during encoding.
89+
///
8490
/// [RFC7469 § 2.1.1]: https://datatracker.ietf.org/doc/html/rfc7469#section-2.1.1
8591
#[cfg(feature = "fingerprint")]
8692
pub fn fingerprint_bytes(&self) -> Result<FingerprintBytes> {
@@ -215,10 +221,10 @@ mod allocating {
215221
impl SubjectPublicKeyInfoOwned {
216222
/// Create a [`SubjectPublicKeyInfoOwned`] from any object that implements
217223
/// [`EncodePublicKey`].
218-
pub fn from_key<T>(source: &T) -> Result<Self>
219-
where
220-
T: EncodePublicKey,
221-
{
224+
///
225+
/// # Errors
226+
/// Propagates encoding and decoding errors.
227+
pub fn from_key<T: EncodePublicKey>(source: &T) -> Result<Self> {
222228
Ok(source.to_public_key_der()?.decode_msg::<Self>()?)
223229
}
224230
}

spki/src/traits.rs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ use crate::SubjectPublicKeyInfo;
2525
pub trait DecodePublicKey: Sized {
2626
/// Deserialize object from ASN.1 DER-encoded [`SubjectPublicKeyInfo`]
2727
/// (binary format).
28+
///
29+
/// # Errors
30+
/// Returns decoding errors specific to the concrete type which impls this trait.
2831
fn from_public_key_der(bytes: &[u8]) -> Result<Self>;
2932

3033
/// Deserialize PEM-encoded [`SubjectPublicKeyInfo`].
@@ -34,6 +37,9 @@ pub trait DecodePublicKey: Sized {
3437
/// ```text
3538
/// -----BEGIN PUBLIC KEY-----
3639
/// ```
40+
///
41+
/// # Errors
42+
/// Returns decoding errors specific to the concrete type which impls this trait.
3743
#[cfg(feature = "pem")]
3844
fn from_public_key_pem(s: &str) -> Result<Self> {
3945
let (label, doc) = Document::from_pem(s)?;
@@ -43,13 +49,19 @@ pub trait DecodePublicKey: Sized {
4349

4450
/// Load public key object from an ASN.1 DER-encoded file on the local
4551
/// filesystem (binary format).
52+
///
53+
/// # Errors
54+
/// Returns decoding errors specific to the concrete type which impls this trait.
4655
#[cfg(feature = "std")]
4756
fn read_public_key_der_file(path: impl AsRef<Path>) -> Result<Self> {
4857
let doc = Document::read_der_file(path)?;
4958
Self::from_public_key_der(doc.as_bytes())
5059
}
5160

5261
/// Load public key object from a PEM-encoded file on the local filesystem.
62+
///
63+
/// # Errors
64+
/// Returns decoding errors specific to the concrete type which impls this trait.
5365
#[cfg(all(feature = "pem", feature = "std"))]
5466
fn read_public_key_pem_file(path: impl AsRef<Path>) -> Result<Self> {
5567
let (label, doc) = Document::read_pem_file(path)?;
@@ -71,22 +83,34 @@ where
7183
#[cfg(feature = "alloc")]
7284
pub trait EncodePublicKey {
7385
/// Serialize a [`Document`] containing a SPKI-encoded public key.
86+
///
87+
/// # Errors
88+
/// Returns encoding errors specific to the concrete type which impls this trait.
7489
fn to_public_key_der(&self) -> Result<Document>;
7590

7691
/// Serialize this public key as PEM-encoded SPKI with the given [`LineEnding`].
92+
///
93+
/// # Errors
94+
/// Returns encoding errors specific to the concrete type which impls this trait.
7795
#[cfg(feature = "pem")]
7896
fn to_public_key_pem(&self, line_ending: LineEnding) -> Result<String> {
7997
let doc = self.to_public_key_der()?;
8098
Ok(doc.to_pem(SubjectPublicKeyInfoRef::PEM_LABEL, line_ending)?)
8199
}
82100

83-
/// Write ASN.1 DER-encoded public key to the given path
101+
/// Write ASN.1 DER-encoded public key to the given path.
102+
///
103+
/// # Errors
104+
/// Returns encoding errors specific to the concrete type which impls this trait.
84105
#[cfg(feature = "std")]
85106
fn write_public_key_der_file(&self, path: impl AsRef<Path>) -> Result<()> {
86107
Ok(self.to_public_key_der()?.write_der_file(path)?)
87108
}
88109

89-
/// Write ASN.1 PEM-encoded public key to the given path
110+
/// Write ASN.1 PEM-encoded public key to the given path.
111+
///
112+
/// # Errors
113+
/// Returns encoding errors specific to the concrete type which impls this trait.
90114
#[cfg(all(feature = "pem", feature = "std"))]
91115
fn write_public_key_pem_file(
92116
&self,
@@ -115,6 +139,9 @@ pub trait AssociatedAlgorithmIdentifier {
115139
#[cfg(feature = "alloc")]
116140
pub trait DynAssociatedAlgorithmIdentifier {
117141
/// `AlgorithmIdentifier` for this structure.
142+
///
143+
/// # Errors
144+
/// Returns errors specific to the concrete type which impls this trait.
118145
fn algorithm_identifier(&self) -> Result<AlgorithmIdentifierOwned>;
119146
}
120147

@@ -135,9 +162,9 @@ where
135162
}
136163
}
137164

138-
/// Returns `AlgorithmIdentifier` associated with the signature system.
165+
/// Returns [`AlgorithmIdentifier`] associated with the signature system.
139166
///
140-
/// Unlike AssociatedAlgorithmIdentifier this is intended to be implemented for public and/or
167+
/// Unlike [`AssociatedAlgorithmIdentifier`] this is intended to be implemented for public and/or
141168
/// private keys.
142169
pub trait SignatureAlgorithmIdentifier {
143170
/// Algorithm parameters.
@@ -147,13 +174,16 @@ pub trait SignatureAlgorithmIdentifier {
147174
const SIGNATURE_ALGORITHM_IDENTIFIER: AlgorithmIdentifier<Self::Params>;
148175
}
149176

150-
/// Returns `AlgorithmIdentifier` associated with the signature system.
177+
/// Returns [`AlgorithmIdentifierOwned`] associated with the signature system.
151178
///
152-
/// Unlike AssociatedAlgorithmIdentifier this is intended to be implemented for public and/or
179+
/// Unlike [`AssociatedAlgorithmIdentifier`] this is intended to be implemented for public and/or
153180
/// private keys.
154181
#[cfg(feature = "alloc")]
155182
pub trait DynSignatureAlgorithmIdentifier {
156183
/// `AlgorithmIdentifier` for the corresponding signature system.
184+
///
185+
/// # Errors
186+
/// Returns errors specific to the concrete type which impls this trait.
157187
fn signature_algorithm_identifier(&self) -> Result<AlgorithmIdentifierOwned>;
158188
}
159189

@@ -174,11 +204,14 @@ where
174204
}
175205
}
176206

177-
/// Returns the `BitString` encoding of the signature.
207+
/// Returns the [`BitString`] encoding of the signature.
178208
///
179-
/// X.509 and CSR structures require signatures to be BitString encoded.
209+
/// X.509 and CSR structures require signatures to be `BitString` encoded.
180210
#[cfg(feature = "alloc")]
181211
pub trait SignatureBitStringEncoding {
182212
/// `BitString` encoding for this signature.
213+
///
214+
/// # Errors
215+
/// Returns errors specific to the concrete type which impls this trait.
183216
fn to_bitstring(&self) -> der::Result<BitString>;
184217
}

spki/tests/traits.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const ED25519_DER_EXAMPLE: &[u8] = include_bytes!("examples/ed25519-pub.der");
2222
const ED25519_PEM_EXAMPLE: &str = include_str!("examples/ed25519-pub.pem");
2323

2424
/// Mock key type for testing trait impls against.
25+
#[derive(Debug)]
2526
pub struct MockKey(Vec<u8>);
2627

2728
impl AsRef<[u8]> for MockKey {

0 commit comments

Comments
 (0)