Skip to content

Commit e65d751

Browse files
authored
pkcs8: enable workspace-level lint config (#2304)
Applies the workspace-level `clippy` and other lints added in #2231, then fixes the lint failures.
1 parent c50f584 commit e65d751

9 files changed

Lines changed: 87 additions & 31 deletions

File tree

pkcs8/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ encryption = ["alloc", "pkcs5/alloc", "pkcs5/pbes2", "rand_core"]
3939
pem = ["alloc", "der/pem", "spki/pem"]
4040
sha1-insecure = ["encryption", "pkcs5/sha1-insecure"]
4141

42+
[lints]
43+
workspace = true
44+
4245
[package.metadata.docs.rs]
4346
all-features = true
44-
rustdoc-args = ["--cfg", "docsrs"]

pkcs8/src/encrypted_private_key_info.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ where
5353
{
5454
/// Attempt to decrypt this encrypted private key using the provided
5555
/// password to derive an encryption key.
56+
///
57+
/// # Errors
58+
/// - Returns errors in the event the file could not be decrypted successfully.
59+
/// - Returns errors if the file decrypted but the resulting plaintext failed to decode.
5660
#[cfg(feature = "encryption")]
5761
pub fn decrypt(&self, password: impl AsRef<[u8]>) -> Result<SecretDocument> {
5862
Ok(self

pkcs8/src/lib.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@
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-
)]
179

1810
//! ## About this crate
1911
//! This library provides generalized PKCS#8 support designed to work with a
@@ -54,15 +46,16 @@
5446
//! private keys encrypted with DES-CBC and DES-EDE3-CBC (3DES or Triple DES) symmetric
5547
//! encryption, respectively.
5648
//!
57-
//! ⚠️ WARNING ⚠️
49+
//! <div class="warning">
50+
//! <b>Security Warning</b>
5851
//!
59-
//! DES support (gated behind the `des-insecure` feature) is implemented to
60-
//! allow for decryption of legacy PKCS#8 files only.
52+
//! DES support (gated behind the `des-insecure` feature) is implemented to allow for decryption of
53+
//! legacy PKCS#8 files only.
6154
//!
62-
//! Such PKCS#8 documents should be considered *INSECURE* due to the short
63-
//! 56-bit key size of DES.
55+
//! Such PKCS#8 documents should be considered *INSECURE* due to the short 56-bit key size of DES.
6456
//!
6557
//! New keys should use AES instead.
58+
//! </div>
6659
//!
6760
//! [RFC 5208]: https://tools.ietf.org/html/rfc5208
6861
//! [RFC 5958]: https://tools.ietf.org/html/rfc5958

pkcs8/src/private_key_info.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ where
147147
/// - r: 8
148148
/// - p: 1
149149
/// - Cipher: AES-256-CBC (best available option for PKCS#5 encryption)
150+
///
151+
/// # Errors
152+
/// - Propagates errors from calling [`Encode::to_der`] on `Self`.
153+
/// - Returns errors in the event encryption failed.
150154
#[cfg(feature = "encryption")]
151155
pub fn encrypt<R: CryptoRng>(
152156
&self,
@@ -159,6 +163,10 @@ where
159163

160164
/// Encrypt this private key using a symmetric encryption key derived
161165
/// from the provided password and [`pbes2::Parameters`].
166+
///
167+
/// # Errors
168+
/// - Propagates errors from calling [`Encode::to_der`] on `Self`.
169+
/// - Returns errors in the event encryption failed.
162170
#[cfg(feature = "encryption")]
163171
pub fn encrypt_with_params(
164172
&self,
@@ -341,7 +349,7 @@ where
341349
self.algorithm == other.algorithm && self.public_key == other.public_key;
342350

343351
self.private_key.as_ref().ct_eq(other.private_key.as_ref())
344-
& Choice::from(public_fields_eq as u8)
352+
& Choice::from(u8::from(public_fields_eq))
345353
}
346354
}
347355

@@ -373,9 +381,10 @@ pub type PrivateKeyInfoRef<'a> = PrivateKeyInfo<AnyRef<'a>, &'a OctetStringRef,
373381
#[cfg(feature = "alloc")]
374382
pub type PrivateKeyInfoOwned = PrivateKeyInfo<Any, OctetString, BitString>;
375383

376-
/// [`BitStringLike`] marks object that will act like a BitString.
384+
/// [`BitStringLike`] marks object that will act like a `BitString`.
377385
///
378386
/// It will allow to get a [`BitStringRef`] that points back to the underlying bytes.
387+
// TODO(tarcieri): replace this with `AsRef<BitStringRef>` when we can have `&BitStringRef`.
379388
pub trait BitStringLike {
380389
fn as_bit_string(&self) -> BitStringRef<'_>;
381390
}

pkcs8/src/traits.rs

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,18 @@ use std::path::Path;
2323

2424
/// Parse a private key object from a PKCS#8 encoded document.
2525
pub trait DecodePrivateKey: Sized {
26-
/// Deserialize PKCS#8 private key from ASN.1 DER-encoded data
27-
/// (binary format).
26+
/// Deserialize PKCS#8 private key from ASN.1 DER-encoded data (binary format).
27+
///
28+
/// # Errors
29+
/// Returns format-specific errors in the event the document failed to parse.
2830
fn from_pkcs8_der(bytes: &[u8]) -> Result<Self>;
2931

30-
/// Deserialize encrypted PKCS#8 private key from ASN.1 DER-encoded data
31-
/// (binary format) and attempt to decrypt it using the provided password.
32+
/// Deserialize encrypted PKCS#8 private key from ASN.1 DER-encoded data (binary format) and
33+
/// attempt to decrypt it using the provided password.
34+
///
35+
/// # Errors
36+
/// - Returns errors if the DER failed to decode
37+
/// - Returns errors if the ciphertext failed to decrypt under the given password
3238
#[cfg(feature = "encryption")]
3339
fn from_pkcs8_encrypted_der(bytes: &[u8], password: impl AsRef<[u8]>) -> Result<Self> {
3440
let doc = EncryptedPrivateKeyInfoRef::try_from(bytes)?.decrypt(password)?;
@@ -42,6 +48,10 @@ pub trait DecodePrivateKey: Sized {
4248
/// ```text
4349
/// -----BEGIN PRIVATE KEY-----
4450
/// ```
51+
///
52+
/// # Errors
53+
/// - Returns [`Error::Asn1`] in the event of a decoding error (PEM or DER).
54+
/// - Returns the same errors as [`DecodePrivateKey::from_pkcs8_der`].
4555
#[cfg(feature = "pem")]
4656
fn from_pkcs8_pem(s: &str) -> Result<Self> {
4757
// Validate PEM label
@@ -52,29 +62,42 @@ pub trait DecodePrivateKey: Sized {
5262
Self::from_pkcs8_der(doc.as_bytes())
5363
}
5464

55-
/// Deserialize encrypted PKCS#8-encoded private key from PEM and attempt
56-
/// to decrypt it using the provided password.
65+
/// Deserialize encrypted PKCS#8-encoded private key from PEM and attempt to decrypt it using
66+
/// the provided password.
5767
///
5868
/// Keys in this format begin with the following delimiter:
5969
///
6070
/// ```text
6171
/// -----BEGIN ENCRYPTED PRIVATE KEY-----
6272
/// ```
73+
///
74+
/// # Errors
75+
/// - Returns [`Error::Asn1`] in the event of a decoding error (PEM or DER).
76+
/// - Returns the same errors as [`DecodePrivateKey::from_pkcs8_encrypted_der`].
6377
#[cfg(all(feature = "encryption", feature = "pem"))]
6478
fn from_pkcs8_encrypted_pem(s: &str, password: impl AsRef<[u8]>) -> Result<Self> {
6579
let (label, doc) = SecretDocument::from_pem(s)?;
6680
EncryptedPrivateKeyInfoRef::validate_pem_label(label)?;
6781
Self::from_pkcs8_encrypted_der(doc.as_bytes(), password)
6882
}
6983

70-
/// Load PKCS#8 private key from an ASN.1 DER-encoded file on the local
71-
/// filesystem (binary format).
84+
/// Load PKCS#8 private key from an ASN.1 DER-encoded file (binary format) on the local
85+
/// filesystem.
86+
///
87+
/// # Errors
88+
/// - Returns the same errors as [`DecodePrivateKey::from_pkcs8_der`].
89+
/// - Returns errors in event the file cannot be read from the filesystem.
7290
#[cfg(feature = "std")]
7391
fn read_pkcs8_der_file(path: impl AsRef<Path>) -> Result<Self> {
7492
Self::from_pkcs8_der(SecretDocument::read_der_file(path)?.as_bytes())
7593
}
7694

7795
/// Load PKCS#8 private key from a PEM-encoded file on the local filesystem.
96+
///
97+
/// # Errors
98+
/// - Returns the same errors as [`SecretDocument::read_pem_file`].
99+
/// - Returns the same errors as [`DecodePrivateKey::from_pkcs8_der`].
100+
/// - Returns errors in event the file cannot be read from the filesystem.
78101
#[cfg(all(feature = "pem", feature = "std"))]
79102
fn read_pkcs8_pem_file(path: impl AsRef<Path>) -> Result<Self> {
80103
let (label, doc) = SecretDocument::read_pem_file(path)?;
@@ -96,10 +119,17 @@ where
96119
#[cfg(feature = "alloc")]
97120
pub trait EncodePrivateKey {
98121
/// Serialize a [`SecretDocument`] containing a PKCS#8-encoded private key.
122+
///
123+
/// # Errors
124+
/// Returns format-specific errors in the event the document failed to serialize.
99125
fn to_pkcs8_der(&self) -> Result<SecretDocument>;
100126

101-
/// Create an [`SecretDocument`] containing the ciphertext of
102-
/// a PKCS#8 encoded private key encrypted under the given `password`.
127+
/// Create an [`SecretDocument`] containing the ciphertext of a PKCS#8 encoded private key
128+
/// encrypted under the given `password`.
129+
///
130+
/// # Errors
131+
/// - Returns format-specific errors in the event the document failed to serialize.
132+
/// - Returns algorithm-specific errors in the event the document couldn't be encrypted.
103133
#[cfg(feature = "encryption")]
104134
fn to_pkcs8_encrypted_der<R: CryptoRng>(
105135
&self,
@@ -110,6 +140,10 @@ pub trait EncodePrivateKey {
110140
}
111141

112142
/// Serialize this private key as PEM-encoded PKCS#8 with the given [`LineEnding`].
143+
///
144+
/// # Errors
145+
/// - Returns the same errors as [`EncodePrivateKey::to_pkcs8_der`].
146+
/// - Returns the same errors as [`SecretDocument::to_pem`].
113147
#[cfg(feature = "pem")]
114148
fn to_pkcs8_pem(&self, line_ending: LineEnding) -> Result<Zeroizing<String>> {
115149
let doc = self.to_pkcs8_der()?;
@@ -118,6 +152,10 @@ pub trait EncodePrivateKey {
118152

119153
/// Serialize this private key as an encrypted PEM-encoded PKCS#8 private
120154
/// key using the `provided` to derive an encryption key.
155+
///
156+
/// # Errors
157+
/// - Returns the same errors as [`EncodePrivateKey::to_pkcs8_encrypted_der`].
158+
/// - Returns the same errors as [`SecretDocument::to_pem`].
121159
#[cfg(all(feature = "encryption", feature = "pem"))]
122160
fn to_pkcs8_encrypted_pem<R: CryptoRng>(
123161
&self,
@@ -129,13 +167,21 @@ pub trait EncodePrivateKey {
129167
Ok(doc.to_pem(EncryptedPrivateKeyInfoRef::PEM_LABEL, line_ending)?)
130168
}
131169

132-
/// Write ASN.1 DER-encoded PKCS#8 private key to the given path
170+
/// Write ASN.1 DER-encoded PKCS#8 private key to the given path.
171+
///
172+
/// # Errors
173+
/// - Returns the same errors as [`EncodePrivateKey::to_pkcs8_der`].
174+
/// - Returns errors in the event the file could not be written to the filesystem.
133175
#[cfg(feature = "std")]
134176
fn write_pkcs8_der_file(&self, path: impl AsRef<Path>) -> Result<()> {
135177
Ok(self.to_pkcs8_der()?.write_der_file(path)?)
136178
}
137179

138-
/// Write ASN.1 PEM-encoded PKCS#8 private key to the given path
180+
/// Write ASN.1 PEM-encoded PKCS#8 private key to the given path.
181+
///
182+
/// # Errors
183+
/// - Returns the same errors as [`EncodePrivateKey::to_pkcs8_der`].
184+
/// - Returns errors in the event the file could not be written to the filesystem.
139185
#[cfg(all(feature = "pem", feature = "std"))]
140186
fn write_pkcs8_pem_file(&self, path: impl AsRef<Path>, line_ending: LineEnding) -> Result<()> {
141187
let doc = self.to_pkcs8_der()?;

pkcs8/src/version.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub enum Version {
1717

1818
impl Version {
1919
/// Is this version expected to have a public key?
20+
#[must_use]
2021
pub fn has_public_key(self) -> bool {
2122
match self {
2223
Version::V1 => false,

pkcs8/tests/encrypted_private_key.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ fn decrypt_ed25519_der_encpriv_aes256_scrypt() {
170170
#[cfg(feature = "encryption")]
171171
#[test]
172172
fn encrypt_ed25519_der_encpriv_aes256_pbkdf2_sha256() {
173-
let pbes2_params = pkcs5::pbes2::Parameters::generate_pbkdf2_sha256_aes256cbc(
173+
let pbes2_params = pbes2::Parameters::generate_pbkdf2_sha256_aes256cbc(
174174
2048,
175175
&hex!("79d982e70df91a88"),
176176
hex!("b2d02d78b2efd9dff694cf8e0af40925"),
@@ -191,7 +191,7 @@ fn encrypt_ed25519_der_encpriv_aes256_pbkdf2_sha256() {
191191
#[cfg(feature = "encryption")]
192192
#[test]
193193
fn encrypt_ed25519_der_encpriv_aes256_scrypt() {
194-
let scrypt_params = pkcs5::pbes2::Parameters::generate_scrypt_aes256cbc(
194+
let scrypt_params = pbes2::Parameters::generate_scrypt_aes256cbc(
195195
pkcs5::scrypt::Params::new(15, 8, 1).unwrap(),
196196
&hex!("E6211E2348AD69E0"),
197197
hex!("9BD0A6251F2254F9FD5963887C27CF01"),

pkcs8/tests/private_key.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ fn decode_ec_bignp256_der() {
9999
"1F66B5B84B7339674533F0329C74F21834281FED0732429E0C79235FC273E269"
100100
))
101101
.unwrap()
102-
)
102+
);
103103
}
104104

105105
// Test vector from RFC8410 Section 10.3:

pkcs8/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-priv-pkcs8v1
2222
const ED25519_PEM_EXAMPLE: &str = include_str!("examples/ed25519-priv-pkcs8v1.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)