subc_protocol/
policy_root.rs1use serde::{Deserialize, Serialize};
4use subc_config::{raw_digest, Digest};
5
6pub const GET_OP: &str = "policy_root.get";
7pub const ROTATE_OP: &str = "policy_root.rotate";
8
9#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
10#[serde(deny_unknown_fields)]
11pub struct GetPolicyRoot {}
12
13#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
14#[serde(deny_unknown_fields)]
15pub struct RotatePolicyRoot {
16 pub statement_b64: String,
17 pub signature_hex: String,
18}
19
20#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
21#[serde(deny_unknown_fields)]
22pub struct PolicyRootPin {
23 pub key_id: String,
24 pub public_key_hex: String,
25 pub retired_key_ids: Vec<String>,
26}
27
28#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
29#[serde(deny_unknown_fields)]
30pub struct PolicyRoot {
31 pub pin: Option<PolicyRootPin>,
32 pub generation: u64,
33}
34
35#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
36#[serde(rename_all = "snake_case")]
37pub enum PolicyRootChangeState {
38 Pending,
39}
40
41#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
42#[serde(deny_unknown_fields)]
43pub struct PolicyRootChange {
44 pub digest: Digest,
45 pub state: PolicyRootChangeState,
46}
47
48pub fn decode_public_key(hex: &str) -> Option<[u8; 32]> {
50 if hex.len() != 64
51 || !hex
52 .bytes()
53 .all(|b| b.is_ascii_digit() || matches!(b, b'a'..=b'f'))
54 {
55 return None;
56 }
57 let mut bytes = [0; 32];
58 for (index, byte) in bytes.iter_mut().enumerate() {
59 *byte = u8::from_str_radix(&hex[index * 2..index * 2 + 2], 16).ok()?;
60 }
61 Some(bytes)
62}
63
64pub fn key_id(public_key: &[u8; 32]) -> String {
66 raw_digest(public_key).as_str()[7..23].to_owned()
67}
68
69pub fn fingerprint(public_key: &[u8; 32]) -> String {
71 let hash = raw_digest(public_key);
72 let hex = &hash.as_str()[7..39];
73 (0..8)
74 .map(|i| &hex[i * 4..i * 4 + 4])
75 .collect::<Vec<_>>()
76 .join(" ")
77}