Skip to content

Commit c9f348e

Browse files
authored
Merge pull request #468 from MattDavis00/feature-trusted-service
Added psa_export_key & psa_generate_random to TS Provider
2 parents 7584db5 + 1ea4e06 commit c9f348e

File tree

7 files changed

+105
-6
lines changed

7 files changed

+105
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2021 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
use super::error::Error;
4+
use super::ts_protobuf::{GenerateRandomIn, GenerateRandomOut};
5+
use super::Context;
6+
use log::info;
7+
use std::convert::TryInto;
8+
9+
impl Context {
10+
pub fn generate_random(&self, size: usize) -> Result<Vec<u8>, Error> {
11+
info!("Handling GenerateRandom request");
12+
let open_req: GenerateRandomIn = GenerateRandomIn {
13+
size: size.try_into()?,
14+
};
15+
let result: GenerateRandomOut = self.send_request(&open_req)?;
16+
Ok(result.random_bytes)
17+
}
18+
}

src/providers/trusted_service/context/key_management.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// SPDX-License-Identifier: Apache-2.0
33
use super::error::Error;
44
use super::ts_protobuf::{
5-
DestroyKeyIn, DestroyKeyOut, ExportPublicKeyIn, GenerateKeyIn, ImportKeyIn, KeyAttributes,
6-
KeyLifetime, KeyPolicy,
5+
DestroyKeyIn, DestroyKeyOut, ExportKeyIn, ExportPublicKeyIn, GenerateKeyIn, ImportKeyIn,
6+
KeyAttributes, KeyLifetime, KeyPolicy,
77
};
88
use super::Context;
99
use log::info;
@@ -85,6 +85,13 @@ impl Context {
8585
self.send_request(&req)
8686
}
8787

88+
/// Export the key given its ID.
89+
pub fn export_key(&self, id: u32) -> Result<Vec<u8>, Error> {
90+
info!("Handling ExportKey request");
91+
let req = ExportKeyIn { id };
92+
self.send_request(&req)
93+
}
94+
8895
/// Destroy a key given its ID.
8996
pub fn destroy_key(&self, key_id: u32) -> Result<(), Error> {
9097
info!("Handling DestroyKey request");

src/providers/trusted_service/context/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub mod ts_binding {
3232

3333
mod asym_sign;
3434
pub mod error;
35+
mod generate_random;
3536
mod key_management;
3637
mod ts_protobuf;
3738

src/providers/trusted_service/context/ts_protobuf.rs

+8
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,21 @@ opcode_impl!(SignHashIn, SignHashOut, SignHash);
5454
opcode_impl!(VerifyHashIn, VerifyHashOut, VerifyHash);
5555
opcode_impl!(ImportKeyIn, ImportKeyOut, ImportKey);
5656
opcode_impl!(ExportPublicKeyIn, ExportPublicKeyOut, ExportPublicKey);
57+
opcode_impl!(ExportKeyIn, ExportKeyOut, ExportKey);
58+
opcode_impl!(GenerateRandomIn, GenerateRandomOut, GenerateRandom);
5759

5860
impl Drop for ImportKeyIn {
5961
fn drop(&mut self) {
6062
self.data.zeroize();
6163
}
6264
}
6365

66+
impl Drop for ExportKeyOut {
67+
fn drop(&mut self) {
68+
self.data.zeroize();
69+
}
70+
}
71+
6472
impl Drop for SignHashIn {
6573
fn drop(&mut self) {
6674
self.hash.zeroize();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2021 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
use super::Provider;
4+
use parsec_interface::operations::psa_generate_random;
5+
use parsec_interface::requests::Result;
6+
7+
impl Provider {
8+
pub(super) fn psa_generate_random_internal(
9+
&self,
10+
op: psa_generate_random::Operation,
11+
) -> Result<psa_generate_random::Result> {
12+
let size = op.size;
13+
14+
match self.context.generate_random(size) {
15+
Ok(random_bytes) => Ok(psa_generate_random::Result {
16+
random_bytes: random_bytes.into(),
17+
}),
18+
Err(error) => {
19+
format_error!("Generate random status: ", error);
20+
Err(error.into())
21+
}
22+
}
23+
}
24+
}

src/providers/trusted_service/key_management.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ use crate::authenticators::ApplicationName;
55
use crate::key_info_managers::KeyTriple;
66
use log::error;
77
use parsec_interface::operations::{
8-
psa_destroy_key, psa_export_public_key, psa_generate_key, psa_import_key,
8+
psa_destroy_key, psa_export_key, psa_export_public_key, psa_generate_key, psa_import_key,
99
};
1010
use parsec_interface::requests::{ProviderId, ResponseStatus, Result};
1111
use parsec_interface::secrecy::ExposeSecret;
12+
use parsec_interface::secrecy::Secret;
1213
use psa_crypto::types::key::PSA_KEY_ID_USER_MAX;
1314
use std::sync::atomic::{AtomicU32, Ordering::Relaxed};
1415

@@ -112,6 +113,26 @@ impl Provider {
112113
Ok(pub_key) => Ok(psa_export_public_key::Result {
113114
data: pub_key.into(),
114115
}),
116+
Err(error) => {
117+
format_error!("Export public key status: ", error);
118+
Err(error.into())
119+
}
120+
}
121+
}
122+
123+
pub(super) fn psa_export_key_internal(
124+
&self,
125+
app_name: ApplicationName,
126+
op: psa_export_key::Operation,
127+
) -> Result<psa_export_key::Result> {
128+
let key_name = op.key_name;
129+
let key_triple = KeyTriple::new(app_name, ProviderId::TrustedService, key_name);
130+
let key_id = self.key_info_store.get_key_id(&key_triple)?;
131+
132+
match self.context.export_key(key_id) {
133+
Ok(key) => Ok(psa_export_key::Result {
134+
data: Secret::new(key),
135+
}),
115136
Err(error) => {
116137
format_error!("Export key status: ", error);
117138
Err(error.into())

src/providers/trusted_service/mod.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use derivative::Derivative;
1111
use log::{error, trace};
1212
use parsec_interface::operations::list_providers::ProviderInfo;
1313
use parsec_interface::operations::{
14-
list_clients, list_keys, psa_destroy_key, psa_export_public_key, psa_generate_key,
15-
psa_import_key, psa_sign_hash, psa_verify_hash,
14+
list_clients, list_keys, psa_destroy_key, psa_export_key, psa_export_public_key,
15+
psa_generate_key, psa_generate_random, psa_import_key, psa_sign_hash, psa_verify_hash,
1616
};
1717
use parsec_interface::requests::{Opcode, ProviderId, Result};
1818
use psa_crypto::types::key;
@@ -22,15 +22,18 @@ use uuid::Uuid;
2222
mod asym_sign;
2323
mod context;
2424
mod error;
25+
mod generate_random;
2526
mod key_management;
2627

27-
const SUPPORTED_OPCODES: [Opcode; 6] = [
28+
const SUPPORTED_OPCODES: [Opcode; 8] = [
2829
Opcode::PsaDestroyKey,
2930
Opcode::PsaGenerateKey,
3031
Opcode::PsaSignHash,
3132
Opcode::PsaVerifyHash,
3233
Opcode::PsaImportKey,
3334
Opcode::PsaExportPublicKey,
35+
Opcode::PsaExportKey,
36+
Opcode::PsaGenerateRandom,
3437
];
3538

3639
/// Trusted Service provider structure
@@ -174,6 +177,23 @@ impl Provide for Provider {
174177
self.psa_export_public_key_internal(app_name, op)
175178
}
176179

180+
fn psa_export_key(
181+
&self,
182+
app_name: ApplicationName,
183+
op: psa_export_key::Operation,
184+
) -> Result<psa_export_key::Result> {
185+
trace!("psa_export_key ingress");
186+
self.psa_export_key_internal(app_name, op)
187+
}
188+
189+
fn psa_generate_random(
190+
&self,
191+
op: psa_generate_random::Operation,
192+
) -> Result<psa_generate_random::Result> {
193+
trace!("psa_generate_random ingress");
194+
self.psa_generate_random_internal(op)
195+
}
196+
177197
fn psa_sign_hash(
178198
&self,
179199
app_name: ApplicationName,

0 commit comments

Comments
 (0)