Skip to main content

co_didcomm/messages/headers/
jwk.rs

1use std::collections::HashMap;
2
3/// Encryption public key
4#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Default)]
5pub struct Epk {
6    pub kty: String,
7
8    pub crv: String,
9
10    pub x: String,
11
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub y: Option<String>,
14}
15
16/// Json Web Keys structure defined by [RFC](https://tools.ietf.org/html/rfc7517)
17#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Default)]
18pub struct Jwk {
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub kty: Option<String>,
21
22    #[serde(rename = "use", skip_serializing_if = "Option::is_none")]
23    pub use_: Option<String>,
24
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub crv: Option<String>,
27
28    pub key_ops: Vec<KeyOps>,
29
30    pub alg: KeyAlgorithm,
31
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub kid: Option<String>,
34
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub epk: Option<Epk>,
37
38    #[serde(flatten)]
39    pub(crate) other: HashMap<String, String>,
40}
41
42impl Jwk {
43    /// Constructor with all default, empty or `None` values.
44    pub fn new() -> Self {
45        Self::default()
46    }
47
48    /// Creates `epk` jwk entry with required properties.
49    /// Correctness is not verified by this constructor and totally rely on caller.
50    pub fn ephemeral(mut self, kty: String, crv: String, x: String, y: Option<String>) -> Self {
51        self.epk = Some(Epk { kty, crv, x, y });
52        self
53    }
54
55    /// Insert new custom, non-defined by spec, header.
56    pub fn add_other_header(&mut self, k: String, v: String) {
57        self.other.insert(k, v);
58    }
59}
60
61// WARN: Does not support other key operation types ATM.
62#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
63#[serde(rename_all = "camelCase")]
64pub enum KeyOps {
65    Sign,
66    Verify,
67    Encrypt,
68    Decrypt,
69    WrapKey,
70    UnwrapKey,
71    DeriveKey,
72    DeriveBits,
73    Other,
74}
75
76/// `alg` field values provided by [RFC](https://tools.ietf.org/html/rfc7518)
77///
78#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
79pub enum KeyAlgorithm {
80    HS256,
81    HS384,
82    HS512,
83    RS256,
84    RS384,
85    RS512,
86    ES256,
87    ES384,
88    ES512,
89    PS384,
90    PS512,
91    RSA1_5,
92    #[serde(rename = "RSA-OAEP")]
93    RsaOaep,
94    #[serde(rename = "RSA-OAEP-256")]
95    RsaOaep256,
96    A128KW,
97    A256KW,
98    #[serde(rename = "dir")]
99    Dir,
100    #[serde(rename = "ECDH-ES")]
101    EcdhEs,
102    #[serde(rename = "ECDH-ES+A128KW")]
103    EcdhEsPlusA128kw,
104    #[serde(rename = "ECDH-ES+A192KW")]
105    EcdhEsPulsA192kw,
106    #[serde(rename = "ECDH-ES+A256KW")]
107    EcdhEsA256kw,
108    A128GCMKW,
109    A192GCMKW,
110    A256GCMKW,
111    #[serde(rename = "ECDH-1PU+A256KW")]
112    Ecdh1puA256kw,
113    #[serde(rename = "ECDH-1PU+XC20PKW")]
114    Ecdh1puXc20pkw,
115    #[serde(rename = "PBES2-HS256+A128KW")]
116    Pbes2Hs256A128kw,
117    #[serde(rename = "PBES2-HS384+A192KW")]
118    Pbes2Hs384A192kw,
119    #[serde(rename = "PBES2-HS512+A256KW")]
120    Pbes2Hs512A256kw,
121    #[serde(rename = "EdDSA")]
122    EdDsa,
123    #[serde(rename = "none")]
124    None,
125}
126
127impl std::string::ToString for KeyAlgorithm {
128    fn to_string(&self) -> String {
129        // can't fail on enums
130        serde_json::to_string(&self).unwrap()
131    }
132}
133
134impl std::default::Default for KeyAlgorithm {
135    fn default() -> Self {
136        KeyAlgorithm::None
137    }
138}