Skip to content

Commit 99c187a

Browse files
authored
Merge pull request #489 from hug-dev/pkcs11-no-login
Do not login if no user pin was entered
2 parents 239ef31 + 47a4b45 commit 99c187a

File tree

5 files changed

+56
-6
lines changed

5 files changed

+56
-6
lines changed

config.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ key_info_manager = "on-disk-manager"
121121
#library_path = "/usr/local/lib/softhsm/libsofthsm2.so"
122122
# (Required) PKCS 11 slot that will be used by Parsec.
123123
#slot_number = 123456789
124-
# (Optional) User pin for authentication with the specific slot. If not set, no authentication will
125-
# be used.
124+
# (Optional) User pin for authentication with the specific slot. If not set, the sessions will not
125+
# be logged in. It might prevent some operations to execute successfully on some tokens.
126126
#user_pin = "123456"
127127
# (Optional) Control whether missing public key operation (such as verifying signatures or asymmetric
128128
# encryption) are fully performed in software.

e2e_tests/tests/all_providers/config/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,13 @@ fn ts_pkcs11_cross() {
299299
signature.clone(),
300300
);
301301
}
302+
303+
#[test]
304+
fn no_user_pin() {
305+
set_config("no_user_pin.toml");
306+
// The service should still start, without the user pin.
307+
reload_service();
308+
309+
let mut client = TestClient::new();
310+
let _ = client.ping().unwrap();
311+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[core_settings]
2+
# The CI already timestamps the logs
3+
log_timestamp = false
4+
log_error_details = true
5+
6+
# The container runs the Parsec service as root, so make sure we disable root
7+
# checks.
8+
allow_root = true
9+
10+
[listener]
11+
listener_type = "DomainSocket"
12+
# The timeout needs to be smaller than the test client timeout (five seconds) as it is testing
13+
# that the service does not hang for very big values of body or authentication length.
14+
timeout = 3000 # in milliseconds
15+
socket_path = "/tmp/parsec.sock"
16+
17+
[authenticator]
18+
auth_type = "Direct"
19+
20+
[[key_manager]]
21+
name = "on-disk-manager"
22+
manager_type = "OnDisk"
23+
store_path = "./mappings"
24+
25+
[[provider]]
26+
provider_type = "Pkcs11"
27+
key_info_manager = "on-disk-manager"
28+
library_path = "/usr/local/lib/softhsm/libsofthsm2.so"
29+
# The service should start without the user pin
30+
#user_pin = "123456"
31+
# The slot_number mandatory field is going to replace the following line with a valid number
32+
# slot_number

src/providers/pkcs11/mod.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ pub struct Provider {
6565
slot_number: Slot,
6666
software_public_operations: bool,
6767
allow_export: bool,
68+
need_login: bool,
6869
}
6970

7071
impl Provider {
@@ -80,9 +81,13 @@ impl Provider {
8081
software_public_operations: bool,
8182
allow_export: bool,
8283
) -> Option<Provider> {
83-
if let Some(pin) = user_pin {
84+
let need_login = if let Some(pin) = user_pin {
8485
backend.set_pin(slot_number, pin.expose_secret()).ok()?;
85-
}
86+
true
87+
} else {
88+
warn!("No user pin has been set in the configuration file, sessions will not be logged in.");
89+
false
90+
};
8691

8792
#[allow(clippy::mutex_atomic)]
8893
let pkcs11_provider = Provider {
@@ -92,6 +97,7 @@ impl Provider {
9297
slot_number,
9398
software_public_operations,
9499
allow_export,
100+
need_login,
95101
};
96102
{
97103
let mut local_ids_handle = pkcs11_provider
@@ -197,7 +203,9 @@ impl Provider {
197203
.open_session_no_callback(self.slot_number, flags)
198204
.map_err(to_response_status)?;
199205

200-
session.login(UserType::User).map_err(to_response_status)?;
206+
if self.need_login {
207+
session.login(UserType::User).map_err(to_response_status)?;
208+
}
201209

202210
Ok(session)
203211
}

src/providers/pkcs11/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub fn to_response_status(error: Error) -> ResponseStatus {
3838
Error::TryFromSlice(e) => ResponseStatus::from(e),
3939
Error::NulError(e) => ResponseStatus::from(e),
4040
error => {
41-
error!("Conversion of {} to PsaErrorCommunicationFailure", error);
41+
format_error!("Conversion of error to PsaErrorCommunicationFailure", error);
4242
ResponseStatus::PsaErrorCommunicationFailure
4343
}
4444
}

0 commit comments

Comments
 (0)