Skip to content

Commit 819fc64

Browse files
committed
code improve
1 parent c696813 commit 819fc64

File tree

5 files changed

+68
-36
lines changed

5 files changed

+68
-36
lines changed

.github/workflows/build.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ jobs:
193193
uses: dtolnay/rust-toolchain@stable
194194
with:
195195
components: clippy
196-
- run: cargo clippy --locked --package rustls-mbedcrypto-provider --all-features --all-targets -- --deny warnings
197-
- run: cargo clippy --locked --package rustls-mbedcrypto-provider --no-default-features --all-targets -- --deny warnings
196+
- run: cargo clippy --locked --all-features --all-targets -- --deny warnings
197+
- run: cargo clippy --locked --no-default-features --all-targets -- --deny warnings
198198

199199
clippy-nightly:
200200
name: Clippy (Nightly)
@@ -212,5 +212,5 @@ jobs:
212212
uses: dtolnay/rust-toolchain@nightly
213213
with:
214214
components: clippy
215-
- run: cargo clippy --locked --package rustls-mbedcrypto-provider --all-features --all-targets
216-
- run: cargo clippy --locked --package rustls-mbedcrypto-provider --no-default-features --all-targets
215+
- run: cargo clippy --locked --all-features --all-targets
216+
- run: cargo clippy --locked --no-default-features --all-targets

rustls-mbedpki-provider/src/client_cert_verifier.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::{
2424
#[derive(Clone)]
2525
pub struct MbedTlsClientCertVerifier {
2626
trusted_cas: mbedtls::alloc::List<mbedtls::x509::Certificate>,
27-
root_subjects: Vec<rustls::DistinguishedName>,
27+
root_subjects: Vec<DistinguishedName>,
2828
verify_callback: Option<Arc<dyn mbedtls::x509::VerifyCallback + 'static>>,
2929
cert_active_check: CertActiveCheck,
3030
}
@@ -105,7 +105,7 @@ impl ClientCertVerifier for MbedTlsClientCertVerifier {
105105
end_entity: &CertificateDer,
106106
intermediates: &[CertificateDer],
107107
now: UnixTime,
108-
) -> Result<rustls::server::danger::ClientCertVerified, rustls::Error> {
108+
) -> Result<ClientCertVerified, rustls::Error> {
109109
let now = NaiveDateTime::from_timestamp_opt(
110110
now.as_secs()
111111
.try_into()
@@ -128,7 +128,7 @@ impl ClientCertVerifier for MbedTlsClientCertVerifier {
128128
let mut error_msg = String::default();
129129
match &self.verify_callback {
130130
Some(callback) => {
131-
let callback = callback.clone();
131+
let callback = Arc::clone(callback);
132132
mbedtls::x509::Certificate::verify_with_callback(
133133
&chain,
134134
&self.trusted_cas,

rustls-mbedpki-provider/src/lib.rs

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,34 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
77

8+
//! rustls-mbedpki-provider
9+
//!
10+
//! rustls-mbedpki-provider is a pki provider for rustls based on [mbedtls].
11+
//!
12+
//! [mbedtls]: https://github.com/fortanix/rust-mbedtls
13+
14+
// Require docs for public APIs, deny unsafe code, etc.
15+
#![forbid(unsafe_code, unused_must_use)]
16+
#![cfg_attr(not(bench), forbid(unstable_features))]
17+
#![deny(
18+
clippy::alloc_instead_of_core,
19+
clippy::clone_on_ref_ptr,
20+
clippy::std_instead_of_core,
21+
clippy::use_self,
22+
clippy::upper_case_acronyms,
23+
trivial_casts,
24+
trivial_numeric_casts,
25+
missing_docs,
26+
unreachable_pub,
27+
unused_import_braces,
28+
unused_extern_crates,
29+
unused_qualifications
30+
)]
31+
32+
// Enable documentation for all features on docs.rs
33+
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
34+
#![cfg_attr(bench, feature(test))]
35+
836
use chrono::NaiveDateTime;
937
use mbedtls::hash::Type;
1038
use pki_types::CertificateDer;
@@ -14,27 +42,24 @@ use std::sync::Arc;
1442
#[cfg(test)]
1543
mod tests_common;
1644

45+
/// module for implementation of [ClientCertVerifier]
1746
pub mod client_cert_verifier;
47+
/// module for implementation of [ServerCertVerifier]
1848
pub mod server_cert_verifier;
1949

2050
pub use client_cert_verifier::MbedTlsClientCertVerifier;
2151
pub use server_cert_verifier::MbedTlsServerCertVerifier;
2252

2353
/// A config about whether to check certificate validity period
24-
#[derive(Debug, PartialEq, Eq, Clone)]
54+
#[derive(Debug, PartialEq, Eq, Clone, Default)]
2555
pub struct CertActiveCheck {
26-
/// If accept expired certificate, default to false
56+
/// Accept expired certificates
2757
pub ignore_expired: bool,
28-
/// If accept not active certificate, default to false
58+
/// Accept certificates that are not yet active
2959
pub ignore_not_active_yet: bool,
3060
}
3161

32-
impl Default for CertActiveCheck {
33-
fn default() -> Self {
34-
Self { ignore_expired: false, ignore_not_active_yet: false }
35-
}
36-
}
37-
62+
/// Helper function to convert a [`CertificateDer`] to [`mbedtls::x509::Certificate`]
3863
pub fn rustls_cert_to_mbedtls_cert(cert: &CertificateDer) -> mbedtls::Result<mbedtls::alloc::Box<mbedtls::x509::Certificate>> {
3964
let cert = mbedtls::x509::Certificate::from_der(cert)?;
4065
Ok(cert)
@@ -45,6 +70,7 @@ pub fn mbedtls_err_into_rustls_err(err: mbedtls::Error) -> rustls::Error {
4570
mbedtls_err_into_rustls_err_with_error_msg(err, "")
4671
}
4772

73+
/// All supported signature schemas
4874
pub const SUPPORTED_SIGNATURE_SCHEMA: [SignatureScheme; 9] = [
4975
rustls::SignatureScheme::RSA_PSS_SHA512,
5076
rustls::SignatureScheme::RSA_PSS_SHA384,
@@ -90,7 +116,8 @@ pub fn mbedtls_err_into_rustls_err_with_error_msg(err: mbedtls::Error, msg: &str
90116
}
91117
}
92118

93-
pub fn rustls_signature_scheme_to_mbedtls_hash_type(signature_scheme: SignatureScheme) -> mbedtls::hash::Type {
119+
/// Helper function to convert rustls [`SignatureScheme`] to mbedtls [`Type`]
120+
pub fn rustls_signature_scheme_to_mbedtls_hash_type(signature_scheme: SignatureScheme) -> Type {
94121
match signature_scheme {
95122
SignatureScheme::RSA_PKCS1_SHA1 => Type::Sha1,
96123
SignatureScheme::ECDSA_SHA1_Legacy => Type::Sha1,
@@ -110,6 +137,7 @@ pub fn rustls_signature_scheme_to_mbedtls_hash_type(signature_scheme: SignatureS
110137
}
111138
}
112139

140+
/// Helper function to convert rustls [`SignatureScheme`] to mbedtls [`mbedtls::pk::Options`]
113141
pub fn rustls_signature_scheme_to_mbedtls_pk_options(signature_scheme: SignatureScheme) -> Option<mbedtls::pk::Options> {
114142
use mbedtls::pk::Options;
115143
use mbedtls::pk::RsaPadding;
@@ -133,7 +161,8 @@ pub fn rustls_signature_scheme_to_mbedtls_pk_options(signature_scheme: Signature
133161
}
134162
}
135163

136-
fn rustls_signature_scheme_to_mbedtls_curve_id(signature_scheme: SignatureScheme) -> mbedtls::pk::EcGroupId {
164+
/// Helper function to convert rustls [`SignatureScheme`] to mbedtls [`mbedtls::pk::EcGroupId`]
165+
pub fn rustls_signature_scheme_to_mbedtls_curve_id(signature_scheme: SignatureScheme) -> mbedtls::pk::EcGroupId {
137166
// reference: https://www.rfc-editor.org/rfc/rfc8446.html#section-4.2.3
138167
use mbedtls::pk::EcGroupId;
139168
match signature_scheme {
@@ -156,7 +185,7 @@ fn rustls_signature_scheme_to_mbedtls_curve_id(signature_scheme: SignatureScheme
156185
}
157186

158187
/// Returns the size of the message digest given the hash type.
159-
fn hash_size_bytes(hash_type: mbedtls::hash::Type) -> Option<usize> {
188+
fn hash_size_bytes(hash_type: Type) -> Option<usize> {
160189
match hash_type {
161190
mbedtls::hash::Type::None => None,
162191
mbedtls::hash::Type::Md2 => Some(16),
@@ -171,7 +200,8 @@ fn hash_size_bytes(hash_type: mbedtls::hash::Type) -> Option<usize> {
171200
}
172201
}
173202

174-
pub fn buffer_for_hash_type(hash_type: mbedtls::hash::Type) -> Option<Vec<u8>> {
203+
/// Returns the a ready to use empty [`Vec<u8>`] for the message digest with given hash type.
204+
pub fn buffer_for_hash_type(hash_type: Type) -> Option<Vec<u8>> {
175205
let size = hash_size_bytes(hash_type)?;
176206
Some(vec![0; size])
177207
}

rustls-mbedpki-provider/src/server_cert_verifier.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl MbedTlsServerCertVerifier {
8686
}
8787
}
8888

89-
fn server_name_to_str(server_name: &rustls::ServerName) -> String {
89+
fn server_name_to_str(server_name: &ServerName) -> String {
9090
match server_name {
9191
ServerName::DnsName(name) => name.as_ref().to_string(),
9292
ServerName::IpAddress(addr) => addr.to_string(),
@@ -102,11 +102,11 @@ impl ServerCertVerifier for MbedTlsServerCertVerifier {
102102
&self,
103103
end_entity: &CertificateDer,
104104
intermediates: &[CertificateDer],
105-
server_name: &rustls::ServerName,
105+
server_name: &ServerName,
106106
// Mbedtls does not support OSCP (Online Certificate Status Protocol).
107107
_ocsp_response: &[u8],
108108
now: UnixTime,
109-
) -> Result<rustls::client::danger::ServerCertVerified, rustls::Error> {
109+
) -> Result<ServerCertVerified, rustls::Error> {
110110
let now = NaiveDateTime::from_timestamp_opt(
111111
now.as_secs()
112112
.try_into()
@@ -130,7 +130,7 @@ impl ServerCertVerifier for MbedTlsServerCertVerifier {
130130
let mut error_msg = String::default();
131131
match &self.verify_callback {
132132
Some(callback) => {
133-
let callback = callback.clone();
133+
let callback = Arc::clone(callback);
134134
mbedtls::x509::Certificate::verify_with_callback_expected_common_name(
135135
&chain,
136136
&self.trusted_cas,
@@ -233,7 +233,7 @@ mod tests {
233233
}
234234

235235
fn test_connection_server_cert_verifier(
236-
supported_verify_schemes: Vec<rustls::SignatureScheme>,
236+
supported_verify_schemes: Vec<SignatureScheme>,
237237
protocol_versions: &[&'static SupportedProtocolVersion],
238238
) {
239239
let root_ca = CertificateDer::from(include_bytes!("../test-data/rsa/ca.der").to_vec());

rustls-mbedpki-provider/src/tests_common.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
77

8-
use std::{
9-
io,
10-
ops::{Deref, DerefMut},
11-
};
8+
use std::io;
9+
10+
use core::ops::{Deref, DerefMut};
1211

1312
use pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer, UnixTime};
1413
use rustls::{client::danger::ServerCertVerifier, ClientConnection, ConnectionCommon, ServerConnection, SideData};
1514

1615
/// Get a certificate chain from the contents of a pem file
17-
pub fn get_chain(bytes: &[u8]) -> Vec<CertificateDer> {
16+
pub(crate) fn get_chain(bytes: &[u8]) -> Vec<CertificateDer> {
1817
rustls_pemfile::certs(&mut io::BufReader::new(bytes))
1918
.unwrap()
2019
.into_iter()
@@ -23,7 +22,7 @@ pub fn get_chain(bytes: &[u8]) -> Vec<CertificateDer> {
2322
}
2423

2524
/// Get a private key from the contents of a pem file
26-
pub fn get_key(bytes: &[u8]) -> PrivateKeyDer {
25+
pub(crate) fn get_key(bytes: &[u8]) -> PrivateKeyDer {
2726
let value = rustls_pemfile::pkcs8_private_keys(&mut io::BufReader::new(bytes))
2827
.unwrap()
2928
.into_iter()
@@ -33,7 +32,7 @@ pub fn get_key(bytes: &[u8]) -> PrivateKeyDer {
3332
}
3433

3534
// Copied from rustls repo
36-
pub fn transfer(
35+
pub(crate) fn transfer(
3736
left: &mut (impl DerefMut + Deref<Target = ConnectionCommon<impl SideData>>),
3837
right: &mut (impl DerefMut + Deref<Target = ConnectionCommon<impl SideData>>),
3938
) -> usize {
@@ -64,7 +63,10 @@ pub fn transfer(
6463
}
6564

6665
// Copied from rustls repo
67-
pub fn do_handshake_until_error(client: &mut ClientConnection, server: &mut ServerConnection) -> Result<(), rustls::Error> {
66+
pub(crate) fn do_handshake_until_error(
67+
client: &mut ClientConnection,
68+
server: &mut ServerConnection,
69+
) -> Result<(), rustls::Error> {
6870
while server.is_handshaking() || client.is_handshaking() {
6971
transfer(client, server);
7072
server.process_new_packets()?;
@@ -74,9 +76,9 @@ pub fn do_handshake_until_error(client: &mut ClientConnection, server: &mut Serv
7476
Ok(())
7577
}
7678

77-
pub struct VerifierWithSupportedVerifySchemes<V> {
78-
pub verifier: V,
79-
pub supported_verify_schemes: Vec<rustls::SignatureScheme>,
79+
pub(crate) struct VerifierWithSupportedVerifySchemes<V> {
80+
pub(crate) verifier: V,
81+
pub(crate) supported_verify_schemes: Vec<rustls::SignatureScheme>,
8082
}
8183

8284
impl<V: ServerCertVerifier> ServerCertVerifier for VerifierWithSupportedVerifySchemes<V> {

0 commit comments

Comments
 (0)