1use base64::{engine::general_purpose, Engine};
7use serde::{Deserialize, Serialize};
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum SecurityMode {
19 Plain,
21 Signed,
23 AuthCrypt,
25 Any,
27}
28
29pub const PRESENTATION_MESSAGE_TYPE: &str = "https://tap.rsvp/schema/1.0#Presentation";
34
35pub const DIDCOMM_SIGNED: &str = "application/didcomm-signed+json";
36pub const DIDCOMM_ENCRYPTED: &str = "application/didcomm-encrypted+json";
37
38#[derive(Serialize, Deserialize, Debug)]
41pub struct Jws {
42 pub payload: String,
43 pub signatures: Vec<JwsSignature>,
44}
45
46#[derive(Serialize, Deserialize, Debug)]
47pub struct JwsSignature {
48 pub protected: String,
49 pub signature: String,
50}
51
52#[derive(Serialize, Deserialize, Debug, Clone)]
54pub struct JwsProtected {
55 #[serde(default = "default_didcomm_signed")]
56 pub typ: String,
57 pub alg: String,
58 pub kid: String,
59}
60
61fn default_didcomm_signed() -> String {
63 DIDCOMM_SIGNED.to_string()
64}
65
66impl JwsSignature {
67 pub fn get_kid(&self) -> Option<String> {
69 if let Ok(protected_bytes) = general_purpose::STANDARD.decode(&self.protected) {
71 if let Ok(protected) = serde_json::from_slice::<JwsProtected>(&protected_bytes) {
72 return Some(protected.kid);
73 }
74 }
75 None
76 }
77
78 pub fn get_protected_header(&self) -> Result<JwsProtected, Box<dyn std::error::Error>> {
80 let protected_bytes = general_purpose::STANDARD.decode(&self.protected)?;
81 let protected = serde_json::from_slice::<JwsProtected>(&protected_bytes)?;
82 Ok(protected)
83 }
84}
85#[derive(Serialize, Deserialize, Debug)]
88pub struct Jwe {
89 pub ciphertext: String,
90 pub protected: String,
91 pub recipients: Vec<JweRecipient>,
92 pub tag: String,
93 pub iv: String,
94}
95
96#[derive(Serialize, Deserialize, Debug)]
97pub struct JweRecipient {
98 pub encrypted_key: String,
99 pub header: JweHeader,
100}
101
102#[derive(Serialize, Deserialize, Debug)]
103pub struct JweHeader {
104 pub kid: String,
105 #[serde(skip_serializing_if = "Option::is_none")]
106 pub sender_kid: Option<String>,
107}
108
109#[derive(Serialize, Deserialize, Debug)]
111pub struct JweProtected {
112 pub epk: EphemeralPublicKey,
113 pub apv: String,
114 #[serde(default = "default_didcomm_encrypted")]
115 pub typ: String,
116 pub enc: String,
117 pub alg: String,
118}
119
120fn default_didcomm_encrypted() -> String {
122 DIDCOMM_ENCRYPTED.to_string()
123}
124
125#[derive(Serialize, Deserialize, Debug)]
127#[serde(tag = "kty")]
128pub enum EphemeralPublicKey {
129 #[serde(rename = "EC")]
130 Ec { crv: String, x: String, y: String },
131 #[serde(rename = "OKP")]
132 Okp { crv: String, x: String },
133}