tink_core/keyset/
validation.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//! Keyset validation functions.
18
19use crate::TinkError;
20
21/// Check whether the given version is valid. The version is valid
22/// only if it is the range [0..max_expected].
23pub fn validate_key_version(version: u32, max_expected: u32) -> Result<(), TinkError> {
24    if version > max_expected {
25        Err(format!(
26            "key has version {version}; only keys with version in range [0..{max_expected}] are supported",
27        )
28        .into())
29    } else {
30        Ok(())
31    }
32}
33
34/// Validate the given key set.
35pub fn validate(keyset: &tink_proto::Keyset) -> Result<(), TinkError> {
36    if keyset.key.is_empty() {
37        return Err("empty keyset".into());
38    }
39    let primary_key_id = keyset.primary_key_id;
40    let mut has_primary_key = false;
41    let mut contains_only_pub = true;
42    let mut num_enabled_keys = 0;
43    for key in &keyset.key {
44        validate_key(key)?;
45        if key.status != tink_proto::KeyStatusType::Enabled as i32 {
46            continue;
47        }
48        if key.key_id == primary_key_id {
49            if has_primary_key {
50                return Err("keyset contains multiple primary keys".into());
51            }
52            has_primary_key = true;
53        }
54        if let Some(key_data) = &key.key_data {
55            if key_data.key_material_type
56                != tink_proto::key_data::KeyMaterialType::AsymmetricPublic as i32
57            {
58                contains_only_pub = false;
59            }
60        }
61        num_enabled_keys += 1;
62    }
63    if num_enabled_keys == 0 {
64        Err("keyset must contain at least one ENABLED key".into())
65    } else if !has_primary_key && !contains_only_pub {
66        Err("keyset does not contain a valid primary key".into())
67    } else {
68        Ok(())
69    }
70}
71
72/// Validate the given key.
73pub fn validate_key(key: &tink_proto::keyset::Key) -> Result<(), TinkError> {
74    if key.key_id == 0 {
75        Err(format!("key has zero key id: {}", key.key_id).into())
76    } else if key.key_data.is_none() {
77        Err(format!("key {} has no key data", key.key_id).into())
78    } else if key.output_prefix_type != tink_proto::OutputPrefixType::Tink as i32
79        && key.output_prefix_type != tink_proto::OutputPrefixType::Legacy as i32
80        && key.output_prefix_type != tink_proto::OutputPrefixType::Raw as i32
81        && key.output_prefix_type != tink_proto::OutputPrefixType::Crunchy as i32
82    {
83        Err(format!("key {} has unknown prefix", key.key_id).into())
84    } else if key.status != tink_proto::KeyStatusType::Enabled as i32
85        && key.status != tink_proto::KeyStatusType::Disabled as i32
86        && key.status != tink_proto::KeyStatusType::Destroyed as i32
87    {
88        Err(format!("key {} has unknown status", key.key_id).into())
89    } else {
90        Ok(())
91    }
92}