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
use prost::Message;
use tink_core::{utils::wrap_err, TinkError};
pub const ECDSA_VERIFIER_KEY_VERSION: u32 = 0;
pub const ECDSA_VERIFIER_TYPE_URL: &str = "type.googleapis.com/google.crypto.tink.EcdsaPublicKey";
#[derive(Default)]
pub(crate) struct EcdsaVerifierKeyManager {}
impl tink_core::registry::KeyManager for EcdsaVerifierKeyManager {
fn primitive(&self, serialized_key: &[u8]) -> Result<tink_core::Primitive, TinkError> {
if serialized_key.is_empty() {
return Err("EcdsaVerifierKeyManager: invalid key".into());
}
let key = tink_proto::EcdsaPublicKey::decode(serialized_key)
.map_err(|e| wrap_err("EcdsaVerifierKeyManager: invalid key", e))?;
let params =
validate_ecdsa_public_key(&key).map_err(|e| wrap_err("EcdsaVerifierKeyManager", e))?;
let (hash, curve, encoding) = crate::get_ecdsa_param_ids(¶ms);
match crate::subtle::EcdsaVerifier::new(hash, curve, encoding, &key.x, &key.y) {
Ok(p) => Ok(tink_core::Primitive::Verifier(Box::new(p))),
Err(e) => Err(wrap_err("EcdsaVerifierKeyManager: invalid key", e)),
}
}
fn new_key(&self, _serialized_key_format: &[u8]) -> Result<Vec<u8>, TinkError> {
Err("EcdsaVerifierKeyManager: not implemented".into())
}
fn type_url(&self) -> &'static str {
ECDSA_VERIFIER_TYPE_URL
}
fn key_material_type(&self) -> tink_proto::key_data::KeyMaterialType {
tink_proto::key_data::KeyMaterialType::AsymmetricPublic
}
}
pub(crate) fn validate_ecdsa_public_key(
key: &tink_proto::EcdsaPublicKey,
) -> Result<tink_proto::EcdsaParams, TinkError> {
tink_core::keyset::validate_key_version(key.version, ECDSA_VERIFIER_KEY_VERSION)?;
let params = key
.params
.as_ref()
.ok_or_else(|| TinkError::new("no public key parameters"))?;
let (hash, curve, encoding) = crate::get_ecdsa_param_ids(¶ms);
crate::subtle::validate_ecdsa_params(hash, curve, encoding)?;
Ok(params.clone())
}