1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use super::generated_ops::psa_generate_key::{Operation as OperationProto, Result as ResultProto};
use crate::operations::psa_generate_key::{Operation, Result};
use crate::requests::ResponseStatus;
use log::error;
use std::convert::{TryFrom, TryInto};
impl TryFrom<OperationProto> for Operation {
type Error = ResponseStatus;
fn try_from(proto_op: OperationProto) -> std::result::Result<Self, Self::Error> {
Ok(Operation {
key_name: proto_op.key_name,
attributes: proto_op
.attributes
.ok_or_else(|| {
error!("attributes field of PsaGenerateKey::Operation message is empty.");
ResponseStatus::InvalidEncoding
})?
.try_into()?,
})
}
}
impl TryFrom<Operation> for OperationProto {
type Error = ResponseStatus;
fn try_from(op: Operation) -> std::result::Result<Self, Self::Error> {
let mut proto: OperationProto = Default::default();
proto.key_name = op.key_name;
proto.attributes = Some(op.attributes.try_into()?);
Ok(proto)
}
}
impl TryFrom<Result> for ResultProto {
type Error = ResponseStatus;
fn try_from(_result: Result) -> std::result::Result<Self, Self::Error> {
Ok(Default::default())
}
}
impl TryFrom<ResultProto> for Result {
type Error = ResponseStatus;
fn try_from(_response: ResultProto) -> std::result::Result<Self, Self::Error> {
Ok(Result {})
}
}
#[cfg(test)]
mod test {
#![allow(deprecated)]
use super::super::generated_ops::psa_algorithm::{
self as algorithm_proto, Algorithm as AlgorithmProto,
};
use super::super::generated_ops::psa_generate_key::{
Operation as OperationProto, Result as ResultProto,
};
use super::super::generated_ops::psa_key_attributes::{
self as key_attributes_proto, KeyAttributes as KeyAttributesProto,
};
use super::super::{Convert, ProtobufConverter};
use crate::operations::psa_algorithm::{Algorithm, AsymmetricSignature, Hash};
use crate::operations::psa_generate_key::{Operation, Result};
use crate::operations::psa_key_attributes::{self, Attributes, Lifetime, Policy, UsageFlags};
use crate::operations::NativeOperation;
use crate::requests::Opcode;
use std::convert::TryInto;
static CONVERTER: ProtobufConverter = ProtobufConverter {};
#[test]
fn create_key_op_from_proto() {
let name = "test name".to_string();
let proto = OperationProto {
key_name: name.clone(),
attributes: Some(get_key_attrs_proto()),
};
let op: Operation = proto.try_into().expect("Failed conversion");
assert_eq!(op.key_name, name);
}
#[test]
fn create_key_op_to_proto() {
let name = "test name".to_string();
let op = Operation {
key_name: name.clone(),
attributes: get_key_attrs(),
};
let proto: OperationProto = op.try_into().expect("Failed conversion");
assert_eq!(proto.key_name, name);
}
#[test]
fn create_key_res_from_proto() {
let proto = ResultProto {};
let _res: Result = proto.try_into().expect("Failed conversion");
}
#[test]
fn create_key_res_to_proto() {
let res = Result {};
let _proto: ResultProto = res.try_into().expect("Failed conversion");
}
#[test]
fn create_key_op_e2e() {
let name = "test name".to_string();
let op = Operation {
key_name: name,
attributes: get_key_attrs(),
};
let body = CONVERTER
.operation_to_body(NativeOperation::PsaGenerateKey(op))
.expect("Failed to convert to body");
let _ = CONVERTER
.body_to_operation(body, Opcode::PsaGenerateKey)
.expect("Failed to convert to operation");
}
fn get_key_attrs() -> Attributes {
Attributes {
lifetime: Lifetime::Persistent,
key_type: psa_key_attributes::Type::RsaKeyPair,
bits: 1024,
policy: Policy {
usage_flags: UsageFlags {
export: true,
copy: true,
cache: true,
encrypt: true,
decrypt: true,
sign_message: true,
verify_message: true,
sign_hash: true,
verify_hash: true,
derive: true,
},
permitted_algorithms: Algorithm::AsymmetricSignature(
AsymmetricSignature::RsaPkcs1v15Sign {
hash_alg: Hash::Sha1.into(),
},
),
},
}
}
fn get_key_attrs_proto() -> KeyAttributesProto {
KeyAttributesProto {
key_type: Some(key_attributes_proto::KeyType {
variant: Some(key_attributes_proto::key_type::Variant::RsaKeyPair(key_attributes_proto::key_type::RsaKeyPair {})),
}),
key_bits: 1024,
key_policy: Some(key_attributes_proto::KeyPolicy {
key_usage_flags: Some(key_attributes_proto::UsageFlags {
export: true,
copy: true,
cache: true,
encrypt: true,
decrypt: true,
sign_message: true,
verify_message: true,
sign_hash: true,
verify_hash: true,
derive: true,
}),
key_algorithm: Some(AlgorithmProto {
variant: Some(algorithm_proto::algorithm::Variant::AsymmetricSignature(algorithm_proto::algorithm::AsymmetricSignature {
variant: Some(algorithm_proto::algorithm::asymmetric_signature::Variant::RsaPkcs1v15Sign(algorithm_proto::algorithm::asymmetric_signature::RsaPkcs1v15Sign {
hash_alg: Some(algorithm_proto::algorithm::asymmetric_signature::SignHash {
variant: Some(algorithm_proto::algorithm::asymmetric_signature::sign_hash::Variant::Specific(
algorithm_proto::algorithm::Hash::Sha1.into(),
)),
}),
})),
}))
}),
}),
}
}
}