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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
use std::io;
use std::io::ErrorKind::InvalidData;
use Error;
use MarkerType;
use CompressType;
use super::{MAX_DOC_SIZE, Hash, Value, ValueRef};
use super::crypto::{HashState, Vault, Key, Identity, CryptoError};
use decode;
#[derive(Clone)]
pub struct Document {
hash_state: Option<HashState>,
doc_hash: Option<Hash>,
hash: Hash,
doc_len: usize,
doc: Vec<u8>,
compressed: Option<Vec<u8>>,
override_compression: bool,
compression: Option<i32>,
signed_by: Vec<Identity>,
schema_hash: Option<Hash>,
validated: bool,
}
impl PartialEq for Document {
fn eq(&self, other: &Self) -> bool {
self.hash() == other.hash()
}
}
impl Eq for Document {}
impl Document {
pub(crate) fn from_parts(
hash_state: Option<HashState>,
doc_hash: Option<Hash>,
hash: Hash,
doc_len: usize,
doc: Vec<u8>,
compressed: Option<Vec<u8>>,
override_compression: bool,
compression: Option<i32>,
signed_by: Vec<Identity>,
schema_hash: Option<Hash>,
) -> Document {
Document {
hash_state,
doc_hash,
hash,
doc_len,
doc,
compressed,
override_compression,
compression,
signed_by,
schema_hash,
validated: true
}
}
pub fn new(v: Value) -> Result<Document, ()> {
let (schema_hash, validated) = if let Some(obj) = v.as_obj() {
if let Some(val) = obj.get("") {
if let Some(hash) = val.as_hash() {
(Some(hash.clone()), false)
}
else {
return Err(());
}
}
else {
(None, true)
}
}
else {
return Err(());
};
let mut doc = Vec::new();
super::encode::write_value(&mut doc, &v);
let doc_len = doc.len();
if doc_len > MAX_DOC_SIZE {
return Err(());
}
let mut hash_state = HashState::new();
hash_state.update(&doc[..]);
let hash = hash_state.get_hash();
let doc_hash = Some(hash.clone());
Ok(Document {
hash_state: Some(hash_state),
doc_hash,
hash,
doc_len,
doc,
compressed: None,
override_compression: false,
compression: None,
signed_by: Vec::new(),
schema_hash,
validated,
})
}
pub fn sign(&mut self, vault: &Vault, key: &Key) -> Result<(), CryptoError> {
if self.hash_state.is_none() || self.doc_hash.is_none() {
let mut hash_state = HashState::new();
hash_state.update(&self.doc[..self.doc_len]);
let doc_hash = hash_state.get_hash();
if self.doc.len() > self.doc_len {
hash_state.update(&self.doc[self.doc_len..]);
}
self.hash_state = Some(hash_state);
self.doc_hash = Some(doc_hash);
}
let signature = vault.sign(self.doc_hash.as_ref().unwrap(), key)?;
self.signed_by.push(signature.signed_by().clone());
let len = self.doc.len();
signature.encode(&mut self.doc);
let new_len = self.doc.len();
if new_len > MAX_DOC_SIZE {
return Err(CryptoError::Io(io::Error::new(InvalidData, "Document is too large with signature")));
}
if new_len > len {
let hash_state = self.hash_state.as_mut().unwrap();
hash_state.update(&self.doc[len..]);
self.hash = hash_state.get_hash();
}
self.compressed = None;
Ok(())
}
pub fn set_compression(&mut self, compression: Option<i32>) {
self.override_compression = true;
self.compression = compression;
self.compressed = None;
}
pub fn reset_compression(&mut self) {
self.override_compression = false;
self.compressed = None;
}
pub fn clear_compress_cache(&mut self) {
self.compressed = None;
}
pub fn signed_by(&self) -> std::slice::Iter<Identity> {
self.signed_by.iter()
}
pub fn len(&self) -> usize {
self.doc.len()
}
pub fn hash(&self) -> &Hash {
&self.hash
}
pub fn schema_hash(&self) -> &Option<Hash> {
&self.schema_hash
}
pub fn compression(&self) -> Option<i32> {
self.compression
}
pub fn override_compression(&self) -> bool {
self.override_compression
}
pub fn validated(&self) -> bool {
self.validated
}
pub fn value(&self) -> ValueRef {
super::decode::read_value_ref(&mut &self.doc[..]).unwrap()
}
pub(crate) fn raw_doc(&self) -> &[u8] {
&self.doc
}
}
pub fn extract_schema_hash(buf: &[u8]) -> crate::Result<Option<Hash>> {
let mut buf: &[u8] = buf;
let compressed = CompressType::decode(&mut buf)?;
match compressed {
CompressType::CompressedNoSchema => Ok(None),
CompressType::Uncompressed | CompressType::Compressed | CompressType::DictCompressed
=> parse_schema_hash(&mut buf),
}
}
pub(crate) fn parse_schema_hash(buf: &mut &[u8]) -> crate::Result<Option<Hash>> {
let obj_len = if let MarkerType::Object(len) = decode::read_marker(buf)? {
len
}
else {
return Err(Error::BadEncode(buf.len(), "Raw document isn't a fogpack object"));
};
if obj_len == 0 { return Ok(None); }
let field = decode::read_str(buf)?;
if field.len() > 0 {
return Ok(None);
}
decode::read_hash(buf)
.map(|v| Some(v))
.map_err(|_e| Error::BadEncode(buf.len(), "Empty string field doesn't have a Hash as its value"))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::crypto::{Vault, PasswordLevel};
fn test_doc() -> Document {
let test: Value = fogpack!({
"test": true,
"boolean": true,
"positive": 1,
"negative": -1,
"string": "string",
"float32": 1.0f32,
"float64": 1.0f64,
"binary": vec![0u8,1u8,2u8],
"array": [Value::from(0), Value::from("an_array")]
});
Document::new(test).expect("Should've been able to encode as a document")
}
fn test_doc_with_schema() -> Document {
let fake_hash = Hash::new("test".as_bytes());
let test: Value = fogpack!({
"" : fake_hash,
"test": true,
"boolean": true,
});
Document::new(test).expect("Should've been able to encode as a document")
}
fn prep_vault() -> (Vault, Key) {
let mut vault = Vault::new_from_password(PasswordLevel::Interactive, "test".to_string())
.expect("Should have been able to make a new vault for testing");
let key = vault.new_key();
(vault, key)
}
#[test]
fn equality_checks() {
let test0 = test_doc_with_schema();
let test1 = test_doc();
let test2 = test_doc();
assert!(test0 != test1, "Different documents were considered equal");
assert!(test2 == test1, "Identically-generated documents weren't considered equal");
}
#[test]
fn large_data() {
let mut large_bin = Vec::new();
large_bin.resize(MAX_DOC_SIZE, 0u8);
let test: Value = fogpack!({
"b": large_bin.clone(),
});
let test = Document::new(test);
assert!(test.is_err(), "Should've been too large to encode as a document");
large_bin.resize(MAX_DOC_SIZE-8, 0u8);
let test: Value = fogpack!({
"b": large_bin,
});
let test = Document::new(test);
assert!(test.is_ok(), "Should've been just small enough to encode as a document");
let mut test = test.unwrap();
let (vault, key) = prep_vault();
assert!(test.sign(&vault, &key).is_err(), "Should've failed because signing put it past the maximum allowed size");
}
}