Skip to content

Commit 0e74e42

Browse files
author
Joe Ellis
committed
Add ListKeys implementation
Signed-off-by: Joe Ellis <[email protected]>
1 parent b2c9a08 commit 0e74e42

File tree

9 files changed

+133
-11
lines changed

9 files changed

+133
-11
lines changed

e2e_tests/tests/all_providers/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ fn list_opcodes() {
6565
let _ = core_provider_opcodes.insert(Opcode::ListProviders);
6666
let _ = core_provider_opcodes.insert(Opcode::ListAuthenticators);
6767
let _ = core_provider_opcodes.insert(Opcode::ListOpcodes);
68+
let _ = core_provider_opcodes.insert(Opcode::ListKeys);
6869

6970
assert_eq!(
7071
client

src/authenticators/unix_peer_credentials_authenticator/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl Authenticate for UnixPeerCredentialsAuthenticator {
3535
version_maj: 0,
3636
version_min: 1,
3737
version_rev: 0,
38-
id: AuthType::PeerCredentials,
38+
id: AuthType::UnixPeerCredentials,
3939
})
4040
}
4141

src/back/backend_handler.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,14 @@ impl BackEndHandler {
207207
trace!("list_authenticators egress");
208208
self.result_to_response(NativeResult::ListAuthenticators(result), header)
209209
}
210+
NativeOperation::ListKeys(op_list_keys) => {
211+
let app_name =
212+
unwrap_or_else_return!(app_name.ok_or(ResponseStatus::NotAuthenticated));
213+
let result =
214+
unwrap_or_else_return!(self.provider.list_keys(app_name, op_list_keys));
215+
trace!("list_keys egress");
216+
self.result_to_response(NativeResult::ListKeys(result), header)
217+
}
210218
NativeOperation::PsaHashCompute(op_hash_compute) => {
211219
let _app_name =
212220
unwrap_or_else_return!(app_name.ok_or(ResponseStatus::NotAuthenticated));

src/key_info_managers/mod.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,41 @@ pub trait ManageKeyInfo {
144144
///
145145
/// Returns an error as a String if there was a problem accessing the Key Info Manager.
146146
fn exists(&self, key_triple: &KeyTriple) -> Result<bool, String>;
147+
148+
/// Returns a Vec of the KeyInfo objects corresponding to the given application name and
149+
/// provider ID.
150+
///
151+
/// # Errors
152+
///
153+
/// Returns an error as a String if there was a problem accessing the Key Info Manager.
154+
fn list_key_info(
155+
&self,
156+
app_name: &ApplicationName,
157+
provider_id: ProviderID,
158+
) -> Result<Vec<parsec_interface::operations::list_keys::KeyInfo>, String> {
159+
use parsec_interface::operations::list_keys::KeyInfo;
160+
161+
let mut keys: Vec<KeyInfo> = Vec::new();
162+
let key_triples = self.get_all(provider_id)?;
163+
164+
for key_triple in key_triples {
165+
if key_triple.app_name() != app_name {
166+
continue;
167+
}
168+
169+
let key_info = self.get(key_triple)?;
170+
let key_info = match key_info {
171+
Some(key_info) => key_info,
172+
_ => continue,
173+
};
174+
175+
keys.push(KeyInfo {
176+
provider_id: ProviderID::MbedCrypto,
177+
name: key_triple.key_name().to_string(),
178+
attributes: key_info.attributes,
179+
});
180+
}
181+
182+
Ok(keys)
183+
}
147184
}

src/providers/core_provider/mod.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
//! aiding clients in discovering the capabilities offered by their underlying
77
//! platform.
88
use super::Provide;
9+
use crate::authenticators::ApplicationName;
910
use derivative::Derivative;
1011
use log::trace;
11-
use parsec_interface::operations::{list_authenticators, list_opcodes, list_providers, ping};
1212
use parsec_interface::operations::{
13-
list_authenticators::AuthenticatorInfo, list_providers::ProviderInfo,
13+
list_authenticators, list_keys, list_opcodes, list_providers, ping,
14+
};
15+
use parsec_interface::operations::{
16+
list_authenticators::AuthenticatorInfo, list_keys::KeyInfo, list_providers::ProviderInfo,
1417
};
1518
use parsec_interface::requests::{Opcode, ProviderID, ResponseStatus, Result};
1619
use std::collections::{HashMap, HashSet};
@@ -20,11 +23,12 @@ use std::sync::Arc;
2023
use uuid::Uuid;
2124
use version::{version, Version};
2225

23-
const SUPPORTED_OPCODES: [Opcode; 4] = [
26+
const SUPPORTED_OPCODES: [Opcode; 5] = [
2427
Opcode::ListProviders,
2528
Opcode::ListOpcodes,
2629
Opcode::Ping,
2730
Opcode::ListAuthenticators,
31+
Opcode::ListKeys,
2832
];
2933

3034
/// Service information provider
@@ -45,6 +49,15 @@ pub struct CoreProvider {
4549
}
4650

4751
impl Provide for CoreProvider {
52+
fn find_keys(&self, app_name: &ApplicationName) -> Result<Vec<KeyInfo>> {
53+
let mut keys: Vec<KeyInfo> = Vec::new();
54+
for provider in &self.prov_list {
55+
let mut provider_keys = provider.find_keys(app_name)?;
56+
keys.append(&mut provider_keys);
57+
}
58+
Ok(keys)
59+
}
60+
4861
fn list_opcodes(&self, op: list_opcodes::Operation) -> Result<list_opcodes::Result> {
4962
trace!("list_opcodes ingress");
5063
Ok(list_opcodes::Result {
@@ -73,6 +86,17 @@ impl Provide for CoreProvider {
7386
})
7487
}
7588

89+
fn list_keys(
90+
&self,
91+
app_name: ApplicationName,
92+
_op: list_keys::Operation,
93+
) -> Result<list_keys::Result> {
94+
trace!("list_keys ingress");
95+
96+
let keys = self.find_keys(&app_name)?;
97+
Ok(list_keys::Result { keys })
98+
}
99+
76100
fn ping(&self, _op: ping::Operation) -> Result<ping::Result> {
77101
trace!("ping ingress");
78102
let result = ping::Result {

src/providers/mbed_crypto_provider/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::authenticators::ApplicationName;
88
use crate::key_info_managers::{KeyTriple, ManageKeyInfo};
99
use derivative::Derivative;
1010
use log::{error, trace};
11-
use parsec_interface::operations::list_providers::ProviderInfo;
11+
use parsec_interface::operations::{list_keys::KeyInfo, list_providers::ProviderInfo};
1212
use parsec_interface::operations::{
1313
psa_aead_decrypt, psa_aead_encrypt, psa_asymmetric_decrypt, psa_asymmetric_encrypt,
1414
psa_destroy_key, psa_export_key, psa_export_public_key, psa_generate_key, psa_generate_random,
@@ -163,6 +163,16 @@ impl Provide for MbedCryptoProvider {
163163
}, SUPPORTED_OPCODES.iter().copied().collect()))
164164
}
165165

166+
fn find_keys(&self, app_name: &ApplicationName) -> Result<Vec<KeyInfo>> {
167+
let store_handle = self.key_info_store.read().expect("Key store lock poisoned");
168+
store_handle
169+
.list_key_info(app_name, ProviderID::MbedCrypto)
170+
.map_err(|e| {
171+
format_error!("Error occurred when fetching key information", e);
172+
ResponseStatus::KeyInfoManagerError
173+
})
174+
}
175+
166176
fn psa_generate_key(
167177
&self,
168178
app_name: ApplicationName,

src/providers/mod.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use serde::Deserialize;
1212
use std::collections::HashSet;
1313
use zeroize::Zeroize;
1414

15+
use parsec_interface::operations::list_keys::KeyInfo;
16+
1517
pub mod core_provider;
1618

1719
#[cfg(feature = "pkcs11-provider")]
@@ -91,9 +93,9 @@ impl ProviderConfig {
9193

9294
use crate::authenticators::ApplicationName;
9395
use parsec_interface::operations::{
94-
list_authenticators, list_opcodes, list_providers, ping, psa_aead_decrypt, psa_aead_encrypt,
95-
psa_asymmetric_decrypt, psa_asymmetric_encrypt, psa_destroy_key, psa_export_key,
96-
psa_export_public_key, psa_generate_key, psa_generate_random, psa_hash_compare,
96+
list_authenticators, list_keys, list_opcodes, list_providers, ping, psa_aead_decrypt,
97+
psa_aead_encrypt, psa_asymmetric_decrypt, psa_asymmetric_encrypt, psa_destroy_key,
98+
psa_export_key, psa_export_public_key, psa_generate_key, psa_generate_random, psa_hash_compare,
9799
psa_hash_compute, psa_import_key, psa_raw_key_agreement, psa_sign_hash, psa_verify_hash,
98100
};
99101
use parsec_interface::requests::{ResponseStatus, Result};
@@ -111,6 +113,14 @@ pub trait Provide {
111113
Err(ResponseStatus::PsaErrorNotSupported)
112114
}
113115

116+
/// Return the keys held by this provider.
117+
///
118+
/// This function facilitates the ListKeys operation supported by the Core Provider.
119+
fn find_keys(&self, _app_name: &ApplicationName) -> Result<Vec<KeyInfo>> {
120+
trace!("find_keys ingress");
121+
Err(ResponseStatus::PsaErrorNotSupported)
122+
}
123+
114124
/// List the providers running in the service.
115125
fn list_providers(&self, _op: list_providers::Operation) -> Result<list_providers::Result> {
116126
trace!("list_providers ingress");
@@ -132,6 +142,16 @@ pub trait Provide {
132142
Err(ResponseStatus::PsaErrorNotSupported)
133143
}
134144

145+
/// Lists all keys belonging to the application.
146+
fn list_keys(
147+
&self,
148+
_app_name: ApplicationName,
149+
_op: list_keys::Operation,
150+
) -> Result<list_keys::Result> {
151+
trace!("list_keys ingress");
152+
Err(ResponseStatus::PsaErrorNotSupported)
153+
}
154+
135155
/// Execute a Ping operation to get the wire protocol version major and minor information.
136156
///
137157
/// # Errors

src/providers/pkcs11_provider/mod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use crate::authenticators::ApplicationName;
99
use crate::key_info_managers::{KeyInfo, KeyTriple, ManageKeyInfo};
1010
use derivative::Derivative;
1111
use log::{error, info, trace, warn};
12-
use parsec_interface::operations::list_providers::ProviderInfo;
12+
use parsec_interface::operations::{
13+
list_keys::KeyInfo as InterfaceKeyInfo, list_providers::ProviderInfo,
14+
};
1315
use parsec_interface::operations::{
1416
psa_asymmetric_decrypt, psa_asymmetric_encrypt, psa_destroy_key, psa_export_public_key,
1517
psa_generate_key, psa_import_key, psa_sign_hash, psa_verify_hash,
@@ -212,6 +214,16 @@ impl Provide for Pkcs11Provider {
212214
))
213215
}
214216

217+
fn find_keys(&self, app_name: &ApplicationName) -> Result<Vec<InterfaceKeyInfo>> {
218+
let store_handle = self.key_info_store.read().expect("Key store lock poisoned");
219+
store_handle
220+
.list_key_info(app_name, ProviderID::Pkcs11)
221+
.map_err(|e| {
222+
format_error!("Error occurred when fetching key information", e);
223+
ResponseStatus::KeyInfoManagerError
224+
})
225+
}
226+
215227
fn psa_generate_key(
216228
&self,
217229
app_name: ApplicationName,

src/providers/tpm_provider/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019 Contributors to the Parsec project.
1+
//KeyInfoStore Copyright 2019 Contributors to the Parsec project.
22
// SPDX-License-Identifier: Apache-2.0
33
//! TPM 2.0 provider
44
//!
@@ -9,7 +9,7 @@ use crate::authenticators::ApplicationName;
99
use crate::key_info_managers::ManageKeyInfo;
1010
use derivative::Derivative;
1111
use log::{info, trace};
12-
use parsec_interface::operations::list_providers::ProviderInfo;
12+
use parsec_interface::operations::{list_keys::KeyInfo, list_providers::ProviderInfo};
1313
use parsec_interface::operations::{
1414
psa_asymmetric_decrypt, psa_asymmetric_encrypt, psa_destroy_key, psa_export_public_key,
1515
psa_generate_key, psa_import_key, psa_sign_hash, psa_verify_hash,
@@ -92,6 +92,16 @@ impl Provide for TpmProvider {
9292
}, SUPPORTED_OPCODES.iter().copied().collect()))
9393
}
9494

95+
fn find_keys(&self, app_name: &ApplicationName) -> Result<Vec<KeyInfo>> {
96+
let store_handle = self.key_info_store.read().expect("Key store lock poisoned");
97+
store_handle
98+
.list_key_info(app_name, ProviderID::Tpm)
99+
.map_err(|e| {
100+
format_error!("Error occurred when fetching key information", e);
101+
ResponseStatus::KeyInfoManagerError
102+
})
103+
}
104+
95105
fn psa_generate_key(
96106
&self,
97107
app_name: ApplicationName,

0 commit comments

Comments
 (0)