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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use rand::{
Rng,
seq::SliceRandom,
};
use std::{
time::SystemTime,
collections::HashMap,
};
use crate::{
Error,
Jwk,
crypto::{
CryptoAlgorithm,
SignatureAlgorithm
},
};
use super::{MessageType, PriorClaims};
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct DidcommHeader {
pub id: usize,
#[serde(rename = "type")]
pub m_type: MessageType,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub to: Vec<String>,
pub from: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub created_time: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub expires_time: Option<u64>,
#[serde(flatten, skip_serializing_if = "HashMap::is_empty")]
pub(crate) other: HashMap<String, String>,
#[serde(skip_serializing_if = "Option::is_none")]
from_prior: Option<PriorClaims>,
}
impl DidcommHeader {
pub fn new() -> Self {
DidcommHeader {
id: DidcommHeader::gen_random_id(),
m_type: MessageType::DidcommUnknown,
to: vec!(String::default()),
from: Some(String::default()),
created_time: None,
expires_time: None,
from_prior: None,
other: HashMap::new(),
}
}
pub fn gen_random_id() -> usize {
rand::thread_rng().gen()
}
pub fn from_prior(&self) -> &Option<PriorClaims> {
&self.from_prior
}
pub fn forward(to: Vec<String>, from: Option<String>, expires_time: Option<u64>) -> Result<Self, Error> {
Ok(DidcommHeader {
id: rand::thread_rng().gen(),
m_type: MessageType::Forward,
to,
from,
created_time: Some(SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?.as_secs()),
expires_time,
..DidcommHeader::new()
})
}
}
impl Default for DidcommHeader {
fn default() -> Self {
DidcommHeader::new()
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JwmHeader {
pub typ: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub enc: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub kid: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub alg: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub jku: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub jwk: Option<Jwk>,
#[serde(skip_serializing_if = "Option::is_none")]
pub epk: Option<Jwk>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cty: Option<String>,
iv: Vec<u8>,
}
impl JwmHeader {
pub fn get_iv(&self) -> &[u8] {
&self.iv
}
pub fn as_signed(&mut self, alg: &SignatureAlgorithm) {
self.typ = String::from("JWM");
match alg {
SignatureAlgorithm::EdDsa => {
self.alg = Some(String::from("EdDSA"));
},
SignatureAlgorithm::Es256 => {
self.alg = Some(String::from("ES256"));
},
SignatureAlgorithm::Es256k => {
self.alg = Some(String::from("ES256K"));
},
}
}
pub fn as_encrypted(&mut self, alg: &CryptoAlgorithm) {
self.typ = String::from("JWM");
match alg {
CryptoAlgorithm::A256GCM => {
self.enc = Some("A256GCM".into());
self.alg = Some("ECDH-ES+A256KW".into());
},
CryptoAlgorithm::XC20P => {
self.enc = Some("XC20P".into());
self.alg = Some("ECDH-ES+A256KW".into());
}
}
}
pub fn kid(&mut self, kid: Option<String>) {
self.kid = kid;
}
}
impl Default for JwmHeader {
fn default() -> Self {
let mut rng = rand::thread_rng();
let mut a = rng.gen::<[u8; 24]>().to_vec();
a.shuffle(&mut rng);
JwmHeader {
typ: "JWM".into(),
iv: a,
enc: None,
kid: None,
epk: None,
alg: None,
cty: None,
jku: None,
jwk: None,
}
}
}
#[test]
fn default_jwm_header_with_random_iv() {
let not_expected: Vec<u8> = vec![0; 24];
let h = JwmHeader::default();
assert_ne!(not_expected, h.iv);
}