tink_core/registry/key_manager.rs
1// Copyright 2020 The Tink-Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15////////////////////////////////////////////////////////////////////////////////
16
17//! Trait definition for key managers.
18
19use crate::TinkError;
20
21/// `KeyManager` "understands" keys of a specific key types: it can generate keys of a supported
22/// type and create primitives for supported keys.
23///
24/// A key type is identified by the global name of the protocol buffer that holds the corresponding
25/// key material, and is given by `type_url`-field of [`KeyData`](tink_proto::KeyData)-protocol
26/// buffer.
27pub trait KeyManager: Send + Sync {
28 /// Construct a primitive instance for the key given in `serialized_key`, which must be a
29 /// serialized key protocol buffer handled by this manager.
30 fn primitive(&self, serialized_key: &[u8]) -> Result<crate::Primitive, TinkError>;
31
32 /// Generate a new key according to specification in `serialized_key_format`, which must be
33 /// supported by this manager, returned as a serialized protocol buffer.
34 fn new_key(&self, serialized_key_format: &[u8]) -> Result<Vec<u8>, TinkError>;
35
36 /// Return true iff this [`KeyManager`] supports key type identified by `type_url`.
37 fn does_support(&self, type_url: &str) -> bool {
38 type_url == self.type_url()
39 }
40
41 /// Return the type URL that identifes the key type of keys managed by this key manager.
42 fn type_url(&self) -> &'static str;
43
44 /// Return the key material type handled by this key manager
45 fn key_material_type(&self) -> tink_proto::key_data::KeyMaterialType;
46
47 // APIs for Key Management
48
49 /// Generate a new [`KeyData`](tink_proto::KeyData) according to specification in
50 /// `serialized_key_format`. This should be used solely by the key management API.
51 fn new_key_data(&self, serialized_key_format: &[u8]) -> Result<tink_proto::KeyData, TinkError> {
52 let serialized_key = self.new_key(serialized_key_format)?;
53 Ok(tink_proto::KeyData {
54 type_url: self.type_url().to_string(),
55 value: serialized_key,
56 key_material_type: self.key_material_type() as i32,
57 })
58 }
59
60 /// Indicate whether this `KeyManager` understands private key types.
61 fn supports_private_keys(&self) -> bool {
62 false
63 }
64
65 /// Extract the public key data from the private key. If `supports_private_keys` returns
66 /// false, this method will always return an error.
67 fn public_key_data(&self, _serialized_key: &[u8]) -> Result<tink_proto::KeyData, TinkError> {
68 Err("private keys not supported".into())
69 }
70}