Skip to content

Commit adaf587

Browse files
authored
Merge pull request parallaxsecond#206 from ionut-arm/fix-clippy
Fix clippy errors
2 parents 9e622c7 + 00855d7 commit adaf587

File tree

12 files changed

+56
-78
lines changed

12 files changed

+56
-78
lines changed

e2e_tests/tests/per_provider/normal_tests/asym_encryption.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn simple_asym_encrypt_rsa_pkcs() {
4848
.generate_rsa_encryption_keys_rsapkcs1v15crypt(key_name.clone())
4949
.unwrap();
5050
let _ciphertext = client
51-
.asymmetric_encrypt_message_with_rsapkcs1v15(key_name.clone(), PLAINTEXT_MESSAGE.to_vec())
51+
.asymmetric_encrypt_message_with_rsapkcs1v15(key_name, PLAINTEXT_MESSAGE.to_vec())
5252
.unwrap();
5353
}
5454

@@ -95,7 +95,7 @@ fn asym_encrypt_wrong_algorithm() {
9595
.generate_rsa_encryption_keys_rsaoaep_sha256(key_name.clone())
9696
.unwrap();
9797
let status = client
98-
.asymmetric_encrypt_message_with_rsapkcs1v15(key_name.clone(), PLAINTEXT_MESSAGE.to_vec())
98+
.asymmetric_encrypt_message_with_rsapkcs1v15(key_name, PLAINTEXT_MESSAGE.to_vec())
9999
.unwrap_err();
100100
assert_eq!(status, ResponseStatus::PsaErrorNotPermitted);
101101
}
@@ -142,10 +142,10 @@ fn asym_encrypt_decrypt_rsa_pkcs_different_keys() {
142142
.generate_rsa_encryption_keys_rsapkcs1v15crypt(key_name_2.clone())
143143
.unwrap();
144144
let ciphertext = client
145-
.asymmetric_encrypt_message_with_rsapkcs1v15(key_name_1.clone(), PLAINTEXT_MESSAGE.to_vec())
145+
.asymmetric_encrypt_message_with_rsapkcs1v15(key_name_1, PLAINTEXT_MESSAGE.to_vec())
146146
.unwrap();
147147
let _res = client
148-
.asymmetric_decrypt_message_with_rsapkcs1v15(key_name_2.clone(), ciphertext)
148+
.asymmetric_decrypt_message_with_rsapkcs1v15(key_name_2, ciphertext)
149149
.unwrap_err();
150150
}
151151

@@ -173,7 +173,7 @@ fn asym_encrypt_verify_decrypt_with_rsa_crate() {
173173
.unwrap();
174174

175175
let plaintext = client
176-
.asymmetric_decrypt_message_with_rsapkcs1v15(key_name.clone(), ciphertext)
176+
.asymmetric_decrypt_message_with_rsapkcs1v15(key_name, ciphertext)
177177
.unwrap();
178178

179179
assert_eq!(&PLAINTEXT_MESSAGE[..], &plaintext[..]);

src/bin/main.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ fn main() -> Result<()> {
7272
let _ = flag::register(SIGHUP, reload_signal.clone())?;
7373

7474
let mut config_file = ::std::fs::read_to_string(opts.config.clone())?;
75-
let mut config: ServiceConfig = toml::from_str(&config_file).or_else(|e| {
76-
Err(Error::new(
75+
let mut config: ServiceConfig = toml::from_str(&config_file).map_err(|e| {
76+
Error::new(
7777
ErrorKind::InvalidInput,
7878
format!("Failed to parse service configuration ({})", e),
79-
))
79+
)
8080
})?;
8181

8282
log_setup(&config);
@@ -111,11 +111,11 @@ fn main() -> Result<()> {
111111
drop(threadpool);
112112

113113
config_file = ::std::fs::read_to_string(opts.config.clone())?;
114-
config = toml::from_str(&config_file).or_else(|e| {
115-
Err(Error::new(
114+
config = toml::from_str(&config_file).map_err(|e| {
115+
Error::new(
116116
ErrorKind::InvalidInput,
117117
format!("Failed to parse service configuration ({})", e),
118-
))
118+
)
119119
})?;
120120
front_end_handler = Arc::from(ServiceBuilder::build_service(&config)?);
121121
listener = ServiceBuilder::start_listener(config.listener)?;

src/key_info_managers/on_disk_manager/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ impl OnDiskKeyInfoManager {
190190
let mut key_info = Vec::new();
191191
let mut key_info_file = File::open(&key_name_file_path)?;
192192
let _ = key_info_file.read_to_end(&mut key_info)?;
193-
let key_info = bincode::deserialize(&key_info[..]).or_else(|e| {
193+
let key_info = bincode::deserialize(&key_info[..]).map_err(|e| {
194194
format_error!("Error deserializing key info", e);
195-
Err(Error::new(ErrorKind::Other, "error deserializing key info"))
195+
Error::new(ErrorKind::Other, "error deserializing key info")
196196
})?;
197197
match base64_data_triple_to_key_triple(
198198
os_str_to_u8_ref(app_name_dir_path.file_name().expect(
@@ -257,9 +257,9 @@ impl OnDiskKeyInfoManager {
257257
}
258258

259259
let mut mapping_file = fs::File::create(&key_name_file_path)?;
260-
mapping_file.write_all(&bincode::serialize(key_info).or_else(|e| {
260+
mapping_file.write_all(&bincode::serialize(key_info).map_err(|e| {
261261
format_error!("Error serializing key info", e);
262-
Err(Error::new(ErrorKind::Other, "error serializing key info"))
262+
Error::new(ErrorKind::Other, "error serializing key info")
263263
})?)
264264
}
265265

src/providers/core_provider/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,19 @@ impl CoreProviderBuilder {
105105
}
106106

107107
pub fn build(mut self) -> std::io::Result<CoreProvider> {
108-
let crate_version: Version = Version::from_str(version!()).or_else(|e| {
108+
let crate_version: Version = Version::from_str(version!()).map_err(|e| {
109109
format_error!("Error parsing the crate version", e);
110-
Err(Error::new(
110+
Error::new(
111111
ErrorKind::InvalidData,
112112
"crate version number has invalid format",
113-
))
113+
)
114114
})?;
115115
self.provider_info.push(ProviderInfo {
116116
// Assigned UUID for this provider: 47049873-2a43-4845-9d72-831eab668784
117-
uuid: Uuid::parse_str("47049873-2a43-4845-9d72-831eab668784").or_else(|_| Err(Error::new(
117+
uuid: Uuid::parse_str("47049873-2a43-4845-9d72-831eab668784").map_err(|_| Error::new(
118118
ErrorKind::InvalidData,
119119
"provider UUID is invalid",
120-
)))?,
120+
))?,
121121
description: String::from("Software provider that implements only administrative (i.e. no cryptographic) operations"),
122122
vendor: String::new(),
123123
version_maj: crate_version.major,

src/providers/mbed_provider/key_management.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn remove_key_id(key_triple: &KeyTriple, store_handle: &mut dyn ManageKeyInfo) -
8787
pub fn key_info_exists(key_triple: &KeyTriple, store_handle: &dyn ManageKeyInfo) -> Result<bool> {
8888
store_handle
8989
.exists(key_triple)
90-
.or_else(|e| Err(key_info_managers::to_response_status(e)))
90+
.map_err(key_info_managers::to_response_status)
9191
}
9292

9393
impl MbedProvider {

src/providers/pkcs11_provider/key_management.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl Pkcs11Provider {
153153
let key_name = op.key_name;
154154
let key_attributes = op.attributes;
155155
// This should never panic on 32 bits or more machines.
156-
let key_size = std::convert::TryFrom::try_from(op.attributes.bits).unwrap();
156+
let key_size = op.attributes.bits;
157157

158158
let key_triple = KeyTriple::new(app_name, ProviderID::Pkcs11, key_name);
159159
let mut store_handle = self
@@ -463,9 +463,9 @@ impl Pkcs11Provider {
463463
modulus,
464464
public_exponent,
465465
};
466-
let data = picky_asn1_der::to_vec(&key).or_else(|err| {
466+
let data = picky_asn1_der::to_vec(&key).map_err(|err| {
467467
format_error!("Could not serialise key elements", err);
468-
Err(ResponseStatus::PsaErrorCommunicationFailure)
468+
ResponseStatus::PsaErrorCommunicationFailure
469469
})?;
470470
Ok(psa_export_public_key::Result { data: data.into() })
471471
}

src/providers/pkcs11_provider/mod.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,9 @@ impl Pkcs11ProviderBuilder {
319319
let slot_number = self
320320
.slot_number
321321
.ok_or_else(|| Error::new(ErrorKind::InvalidData, "missing slot number"))?;
322-
let mut backend = Ctx::new(library_path).or_else(|e| {
322+
let mut backend = Ctx::new(library_path).map_err(|e| {
323323
format_error!("Error creating a PKCS 11 context", e);
324-
Err(Error::new(
325-
ErrorKind::InvalidData,
326-
"error creating PKCS 11 context",
327-
))
324+
Error::new(ErrorKind::InvalidData, "error creating PKCS 11 context")
328325
})?;
329326
let mut args = CK_C_INITIALIZE_ARGS::new();
330327
// Allow the PKCS 11 library to use OS native locking mechanism.
@@ -334,12 +331,12 @@ impl Pkcs11ProviderBuilder {
334331
args.UnlockMutex = None;
335332
args.flags = CKF_OS_LOCKING_OK;
336333
trace!("Initialize command");
337-
backend.initialize(Some(args)).or_else(|e| {
334+
backend.initialize(Some(args)).map_err(|e| {
338335
format_error!("Error initializing the PKCS 11 backend", e);
339-
Err(Error::new(
336+
Error::new(
340337
ErrorKind::InvalidData,
341338
"PKCS 11 backend initializing failed",
342-
))
339+
)
343340
})?;
344341
Ok(Pkcs11Provider::new(
345342
self.key_info_store

src/providers/tpm_provider/asym_sign.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ impl TpmProvider {
4949
&password_context.auth_value,
5050
&op.hash,
5151
)
52-
.or_else(|e| {
52+
.map_err(|e| {
5353
if crate::utils::GlobalConfig::log_error_details() {
5454
error!("Error signing: {}.", e);
5555
}
56-
Err(utils::to_response_status(e))
56+
utils::to_response_status(e)
5757
})?;
5858

5959
Ok(psa_sign_hash::Result {

src/providers/tpm_provider/key_management.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub fn get_password_context(
5353
) -> Result<(PasswordContext, Attributes)> {
5454
let key_info = store_handle
5555
.get(&key_triple)
56-
.or_else(|e| Err(key_info_managers::to_response_status(e)))?
56+
.map_err(key_info_managers::to_response_status)?
5757
.ok_or_else(|| {
5858
if crate::utils::GlobalConfig::log_error_details() {
5959
error!(
@@ -89,9 +89,9 @@ impl TpmProvider {
8989

9090
let (key_context, auth_value) = esapi_context
9191
.create_signing_key(utils::parsec_to_tpm_params(attributes)?, AUTH_VAL_LEN)
92-
.or_else(|e| {
92+
.map_err(|e| {
9393
format_error!("Error creating a RSA signing key", e);
94-
Err(utils::to_response_status(e))
94+
utils::to_response_status(e)
9595
})?;
9696

9797
insert_password_context(
@@ -132,9 +132,9 @@ impl TpmProvider {
132132
.expect("ESAPI Context lock poisoned");
133133

134134
let public_key: RSAPublicKey = picky_asn1_der::from_bytes(key_data.expose_secret())
135-
.or_else(|err| {
135+
.map_err(|err| {
136136
format_error!("Could not deserialise key elements", err);
137-
Err(ResponseStatus::PsaErrorInvalidArgument)
137+
ResponseStatus::PsaErrorInvalidArgument
138138
})?;
139139

140140
if public_key.modulus.is_negative() || public_key.public_exponent.is_negative() {
@@ -183,9 +183,9 @@ impl TpmProvider {
183183

184184
let pub_key_context = esapi_context
185185
.load_external_rsa_public_key(&key_data)
186-
.or_else(|e| {
186+
.map_err(|e| {
187187
format_error!("Error creating a RSA signing key", e);
188-
Err(utils::to_response_status(e))
188+
utils::to_response_status(e)
189189
})?;
190190

191191
insert_password_context(
@@ -219,9 +219,9 @@ impl TpmProvider {
219219

220220
let pub_key_data = esapi_context
221221
.read_public_key(password_context.context)
222-
.or_else(|e| {
222+
.map_err(|e| {
223223
format_error!("Error reading a public key", e);
224-
Err(utils::to_response_status(e))
224+
utils::to_response_status(e)
225225
})?;
226226

227227
Ok(psa_export_public_key::Result {

src/providers/tpm_provider/mod.rs

+10-25
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,8 @@ impl TpmProviderBuilder {
201201
Some(mut auth) if auth.starts_with(AUTH_HEX_PREFIX) => Ok(hex::decode(
202202
auth.split_off(AUTH_STRING_PREFIX.len()),
203203
)
204-
.or_else(|_| {
205-
Err(std::io::Error::new(
206-
ErrorKind::InvalidData,
207-
"invalid hex owner hierarchy auth",
208-
))
204+
.map_err(|_| {
205+
std::io::Error::new(ErrorKind::InvalidData, "invalid hex owner hierarchy auth")
209206
})?),
210207
Some(auth) => Ok(auth.into()),
211208
}
@@ -225,19 +222,13 @@ impl TpmProviderBuilder {
225222
Tcti::from_str(self.tcti.as_ref().ok_or_else(|| {
226223
std::io::Error::new(ErrorKind::InvalidData, "Invalid TCTI configuration string")
227224
})?)
228-
.or_else(|_| {
229-
Err(std::io::Error::new(
230-
ErrorKind::InvalidData,
231-
"Invalid TCTI configuration string",
232-
))
225+
.map_err(|_| {
226+
std::io::Error::new(ErrorKind::InvalidData, "Invalid TCTI configuration string")
233227
})?,
234228
)
235-
.or_else(|e| {
229+
.map_err(|e| {
236230
format_error!("Error when creating TSS Context", e);
237-
Err(std::io::Error::new(
238-
ErrorKind::InvalidData,
239-
"failed initializing TSS context",
240-
))
231+
std::io::Error::new(ErrorKind::InvalidData, "failed initializing TSS context")
241232
})?;
242233
for cipher in ciphers.iter() {
243234
if ctx
@@ -265,11 +256,8 @@ impl TpmProviderBuilder {
265256
let tcti = Tcti::from_str(self.tcti.as_ref().ok_or_else(|| {
266257
std::io::Error::new(ErrorKind::InvalidData, "Invalid TCTI configuration string")
267258
})?)
268-
.or_else(|_| {
269-
Err(std::io::Error::new(
270-
ErrorKind::InvalidData,
271-
"Invalid TCTI configuration string",
272-
))
259+
.map_err(|_| {
260+
std::io::Error::new(ErrorKind::InvalidData, "Invalid TCTI configuration string")
273261
})?;
274262
TpmProvider::new(
275263
self.key_info_store.ok_or_else(|| {
@@ -286,12 +274,9 @@ impl TpmProviderBuilder {
286274
)
287275
.with_default_context_cipher(default_cipher)
288276
.build()
289-
.or_else(|e| {
277+
.map_err(|e| {
290278
format_error!("Error creating TSS Transient Object Context", e);
291-
Err(std::io::Error::new(
292-
ErrorKind::InvalidData,
293-
"failed initializing TSS context",
294-
))
279+
std::io::Error::new(ErrorKind::InvalidData, "failed initializing TSS context")
295280
})?,
296281
)
297282
.ok_or_else(|| {

src/providers/tpm_provider/utils.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use parsec_interface::requests::{ResponseStatus, Result};
88
use picky_asn1::wrapper::IntegerAsn1;
99
use picky_asn1_x509::RSAPublicKey;
1010
use serde::{Deserialize, Serialize};
11-
use std::convert::TryFrom;
1211
use std::convert::TryInto;
1312
use tss_esapi::abstraction::transient::KeyParams;
1413
use tss_esapi::response_code::{Error, Tss2ResponseCodeKind};
@@ -167,7 +166,7 @@ pub fn pub_key_to_bytes(pub_key: PublicKey, key_attributes: Attributes) -> Resul
167166
})
168167
.or(Err(ResponseStatus::PsaErrorGenericError)),
169168
PublicKey::Ecc { x, y } => {
170-
let p_byte_size = usize::try_from(key_attributes.bits / 8).unwrap(); // should not fail for valid keys
169+
let p_byte_size = key_attributes.bits / 8; // should not fail for valid keys
171170
if x.len() != p_byte_size || y.len() != p_byte_size {
172171
if crate::utils::GlobalConfig::log_error_details() {
173172
error!(
@@ -201,7 +200,7 @@ pub fn signature_data_to_bytes(data: SignatureData, key_attributes: Attributes)
201200
// ECDSA signature data is represented the concatenation of the two result values, r and s,
202201
// in big endian format, as described here:
203202
// https://parallaxsecond.github.io/parsec-book/parsec_client/operations/psa_algorithm.html#asymmetricsignature-algorithm
204-
let p_byte_size = usize::try_from(key_attributes.bits / 8).unwrap(); // should not fail for valid keys
203+
let p_byte_size = key_attributes.bits / 8; // should not fail for valid keys
205204
if r.len() != p_byte_size || s.len() != p_byte_size {
206205
if crate::utils::GlobalConfig::log_error_details() {
207206
error!(
@@ -244,7 +243,7 @@ fn bytes_to_signature_data(
244243
// ECDSA signature data is represented the concatenation of the two result values, r and s,
245244
// in big endian format, as described here:
246245
// https://parallaxsecond.github.io/parsec-book/parsec_client/operations/psa_algorithm.html#asymmetricsignature-algorithm
247-
let p_size = usize::try_from(key_attributes.bits / 8).unwrap();
246+
let p_size = key_attributes.bits / 8;
248247
if data.len() != p_size * 2 {
249248
return Err(ResponseStatus::PsaErrorInvalidArgument);
250249
}

src/utils/service_builder.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,9 @@ fn build_backend_handlers(
158158
.with_wire_protocol_version(WIRE_PROTOCOL_VERSION_MINOR, WIRE_PROTOCOL_VERSION_MAJOR);
159159

160160
for (provider_id, provider) in providers.drain(..) {
161-
let (info, opcodes) = provider.describe().or_else(|_| {
162-
Err(Error::new(
163-
ErrorKind::InvalidData,
164-
"error describing provider",
165-
))
166-
})?;
161+
let (info, opcodes) = provider
162+
.describe()
163+
.map_err(|_| Error::new(ErrorKind::InvalidData, "error describing provider"))?;
167164
core_provider_builder = core_provider_builder.with_provider_details(info, opcodes);
168165

169166
let backend_handler = BackEndHandlerBuilder::new()

0 commit comments

Comments
 (0)