Skip to content

Commit 9165bb3

Browse files
author
Joe Ellis
committed
Implement ListKeys operation
Signed-off-by: Joe Ellis <[email protected]>
1 parent cf4ba71 commit 9165bb3

File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-0
lines changed

src/operations/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub mod psa_aead_decrypt;
2626
pub mod list_opcodes;
2727
pub mod list_providers;
2828
pub mod list_authenticators;
29+
pub mod list_keys;
2930
pub mod psa_generate_random;
3031
pub mod psa_raw_key_agreement;
3132

@@ -44,6 +45,8 @@ pub enum NativeOperation {
4445
ListOpcodes(list_opcodes::Operation),
4546
/// ListAuthenticators operation
4647
ListAuthenticators(list_authenticators::Operation),
48+
/// ListKeys operation
49+
ListKeys(list_keys::Operation),
4750
/// Ping operation
4851
Ping(ping::Operation),
4952
/// PsaGenerateKey operation
@@ -95,6 +98,7 @@ impl NativeOperation {
9598
NativeOperation::ListOpcodes(_) => Opcode::ListOpcodes,
9699
NativeOperation::ListProviders(_) => Opcode::ListProviders,
97100
NativeOperation::ListAuthenticators(_) => Opcode::ListAuthenticators,
101+
NativeOperation::ListKeys(_) => Opcode::ListKeys,
98102
NativeOperation::PsaAsymmetricEncrypt(_) => Opcode::PsaAsymmetricEncrypt,
99103
NativeOperation::PsaAsymmetricDecrypt(_) => Opcode::PsaAsymmetricDecrypt,
100104
NativeOperation::PsaAeadEncrypt(_) => Opcode::PsaAeadEncrypt,
@@ -115,6 +119,8 @@ pub enum NativeResult {
115119
ListOpcodes(list_opcodes::Result),
116120
/// ListAuthenticators result
117121
ListAuthenticators(list_authenticators::Result),
122+
/// ListKeys result
123+
ListKeys(list_keys::Result),
118124
/// Ping result
119125
Ping(ping::Result),
120126
/// PsaGenerateKey result
@@ -166,6 +172,7 @@ impl NativeResult {
166172
NativeResult::ListOpcodes(_) => Opcode::ListOpcodes,
167173
NativeResult::ListProviders(_) => Opcode::ListProviders,
168174
NativeResult::ListAuthenticators(_) => Opcode::ListAuthenticators,
175+
NativeResult::ListKeys(_) => Opcode::ListKeys,
169176
NativeResult::PsaAsymmetricEncrypt(_) => Opcode::PsaAsymmetricEncrypt,
170177
NativeResult::PsaAsymmetricDecrypt(_) => Opcode::PsaAsymmetricDecrypt,
171178
NativeResult::PsaAeadEncrypt(_) => Opcode::PsaAeadEncrypt,
@@ -225,6 +232,12 @@ impl From<list_authenticators::Operation> for NativeOperation {
225232
}
226233
}
227234

235+
impl From<list_keys::Operation> for NativeOperation {
236+
fn from(op: list_keys::Operation) -> Self {
237+
NativeOperation::ListKeys(op)
238+
}
239+
}
240+
228241
impl From<ping::Operation> for NativeOperation {
229242
fn from(op: ping::Operation) -> Self {
230243
NativeOperation::Ping(op)
@@ -338,6 +351,12 @@ impl From<list_authenticators::Result> for NativeResult {
338351
}
339352
}
340353

354+
impl From<list_keys::Result> for NativeResult {
355+
fn from(op: list_keys::Result) -> Self {
356+
NativeResult::ListKeys(op)
357+
}
358+
}
359+
341360
impl From<ping::Result> for NativeResult {
342361
fn from(op: ping::Result) -> Self {
343362
NativeResult::Ping(op)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright 2020 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
use super::generated_ops::list_keys::{
4+
KeyInfo as KeyInfoProto, Operation as OperationProto, Result as ResultProto,
5+
};
6+
use crate::operations::list_keys::{KeyInfo, Operation, Result};
7+
use crate::requests::{ProviderID, ResponseStatus};
8+
use log::error;
9+
use num::FromPrimitive;
10+
use std::convert::{TryFrom, TryInto};
11+
12+
impl TryFrom<OperationProto> for Operation {
13+
type Error = ResponseStatus;
14+
15+
fn try_from(_proto_op: OperationProto) -> std::result::Result<Self, Self::Error> {
16+
Ok(Operation {})
17+
}
18+
}
19+
20+
impl TryFrom<Operation> for OperationProto {
21+
type Error = ResponseStatus;
22+
23+
fn try_from(_op: Operation) -> std::result::Result<Self, Self::Error> {
24+
Ok(Default::default())
25+
}
26+
}
27+
28+
impl TryFrom<KeyInfoProto> for KeyInfo {
29+
type Error = ResponseStatus;
30+
31+
fn try_from(proto_info: KeyInfoProto) -> std::result::Result<Self, Self::Error> {
32+
let id: ProviderID = match FromPrimitive::from_u32(proto_info.provider_id) {
33+
Some(id) => id,
34+
None => return Err(ResponseStatus::ProviderDoesNotExist),
35+
};
36+
37+
let attributes = proto_info
38+
.attributes
39+
.ok_or_else(|| {
40+
error!("attributes field of KeyInfo protobuf message is empty.");
41+
ResponseStatus::InvalidEncoding
42+
})?
43+
.try_into()?;
44+
45+
Ok(KeyInfo {
46+
provider_id: id as u32,
47+
name: proto_info.name,
48+
attributes,
49+
})
50+
}
51+
}
52+
53+
impl TryFrom<KeyInfo> for KeyInfoProto {
54+
type Error = ResponseStatus;
55+
56+
fn try_from(info: KeyInfo) -> std::result::Result<Self, Self::Error> {
57+
Ok(KeyInfoProto {
58+
provider_id: info.provider_id,
59+
name: info.name,
60+
attributes: Some(info.attributes.try_into()?),
61+
})
62+
}
63+
}
64+
65+
impl TryFrom<ResultProto> for Result {
66+
type Error = ResponseStatus;
67+
68+
fn try_from(proto_op: ResultProto) -> std::result::Result<Self, Self::Error> {
69+
let mut keys: Vec<KeyInfo> = Vec::new();
70+
for key in proto_op.keys {
71+
keys.push(key.try_into()?);
72+
}
73+
74+
Ok(Result { keys })
75+
}
76+
}
77+
78+
impl TryFrom<Result> for ResultProto {
79+
type Error = ResponseStatus;
80+
81+
fn try_from(op: Result) -> std::result::Result<Self, Self::Error> {
82+
let mut keys: Vec<KeyInfoProto> = Vec::new();
83+
for key in op.keys {
84+
keys.push(key.try_into()?);
85+
}
86+
87+
Ok(ResultProto { keys })
88+
}
89+
}

src/operations_protobuf/generated_ops.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ include_protobuf_as_module!(psa_import_key);
2929
include_protobuf_as_module!(list_opcodes);
3030
include_protobuf_as_module!(list_providers);
3131
include_protobuf_as_module!(list_authenticators);
32+
include_protobuf_as_module!(list_keys);
3233
include_protobuf_as_module!(ping);
3334
include_protobuf_as_module!(psa_key_attributes);
3435
include_protobuf_as_module!(psa_algorithm);
@@ -140,6 +141,8 @@ empty_clear_message!(list_providers::Operation);
140141
empty_clear_message!(list_providers::Result);
141142
empty_clear_message!(list_authenticators::Operation);
142143
empty_clear_message!(list_authenticators::Result);
144+
empty_clear_message!(list_keys::Operation);
145+
empty_clear_message!(list_keys::Result);
143146
empty_clear_message!(ping::Operation);
144147
empty_clear_message!(ping::Result);
145148
empty_clear_message!(psa_destroy_key::Operation);

src/operations_protobuf/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mod convert_psa_hash_compare;
1818
mod convert_list_providers;
1919
mod convert_list_opcodes;
2020
mod convert_list_authenticators;
21+
mod convert_list_keys;
2122
mod convert_psa_asymmetric_encrypt;
2223
mod convert_psa_asymmetric_decrypt;
2324
mod convert_psa_aead_encrypt;
@@ -34,6 +35,7 @@ use crate::requests::{
3435
request::RequestBody, response::ResponseBody, BodyType, Opcode, ResponseStatus, Result,
3536
};
3637
use generated_ops::list_authenticators as list_authenticators_proto;
38+
use generated_ops::list_keys as list_keys_proto;
3739
use generated_ops::list_opcodes as list_opcodes_proto;
3840
use generated_ops::list_providers as list_providers_proto;
3941
use generated_ops::ping as ping_proto;
@@ -103,6 +105,10 @@ impl Convert for ProtobufConverter {
103105
body.bytes(),
104106
list_authenticators_proto::Operation
105107
))),
108+
Opcode::ListKeys => Ok(NativeOperation::ListKeys(wire_to_native!(
109+
body.bytes(),
110+
list_keys_proto::Operation
111+
))),
106112
Opcode::Ping => Ok(NativeOperation::Ping(wire_to_native!(
107113
body.bytes(),
108114
ping_proto::Operation
@@ -179,6 +185,10 @@ impl Convert for ProtobufConverter {
179185
NativeOperation::ListAuthenticators(operation) => Ok(RequestBody::from_bytes(
180186
native_to_wire!(operation, list_authenticators_proto::Operation),
181187
)),
188+
NativeOperation::ListKeys(operation) => Ok(RequestBody::from_bytes(native_to_wire!(
189+
operation,
190+
list_keys_proto::Operation
191+
))),
182192
NativeOperation::Ping(operation) => Ok(RequestBody::from_bytes(native_to_wire!(
183193
operation,
184194
ping_proto::Operation
@@ -245,6 +255,10 @@ impl Convert for ProtobufConverter {
245255
body.bytes(),
246256
list_authenticators_proto::Result
247257
))),
258+
Opcode::ListKeys => Ok(NativeResult::ListKeys(wire_to_native!(
259+
body.bytes(),
260+
list_keys_proto::Result
261+
))),
248262
Opcode::Ping => Ok(NativeResult::Ping(wire_to_native!(
249263
body.bytes(),
250264
ping_proto::Result
@@ -323,6 +337,10 @@ impl Convert for ProtobufConverter {
323337
NativeResult::ListAuthenticators(result) => Ok(ResponseBody::from_bytes(
324338
native_to_wire!(result, list_authenticators_proto::Result),
325339
)),
340+
NativeResult::ListKeys(result) => Ok(ResponseBody::from_bytes(native_to_wire!(
341+
result,
342+
list_keys_proto::Result
343+
))),
326344
NativeResult::Ping(result) => Ok(ResponseBody::from_bytes(native_to_wire!(
327345
result,
328346
ping_proto::Result

src/requests/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ pub enum Opcode {
111111
PsaAeadDecrypt = 18,
112112
/// PsaRawKeyAgreement operation
113113
PsaRawKeyAgreement = 19,
114+
/// ListKeys operations
115+
ListKeys = 26,
114116
}
115117

116118
/// Listing of available authentication methods.

0 commit comments

Comments
 (0)