This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 734
/
Copy pathcrypto.rs
624 lines (518 loc) · 21.8 KB
/
crypto.rs
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
use std::collections::HashMap;
use crate::domain::crypto::key::{Key, KeyInfo, KeyMetadata};
use crate::domain::crypto::pack::*;
use indy_api_types::errors::prelude::*;
use crate::services::crypto::CryptoService;
use indy_wallet::{RecordOptions, WalletService};
use std::rc::Rc;
use std::str;
use indy_utils::crypto::base64;
use indy_utils::crypto::chacha20poly1305_ietf;
use crate::domain::crypto::combo_box::ComboBox;
use indy_api_types::WalletHandle;
pub const PROTECTED_HEADER_ENC: &str = "xchacha20poly1305_ietf";
pub const PROTECTED_HEADER_TYP: &str = "JWM/1.0";
pub const PROTECTED_HEADER_ALG_AUTH: &str = "Authcrypt";
pub const PROTECTED_HEADER_ALG_ANON: &str = "Anoncrypt";
pub enum CryptoCommand {
CreateKey(
WalletHandle,
KeyInfo, // key info
Box<dyn Fn(IndyResult<String /*verkey*/>) + Send>,
),
SetKeyMetadata(
WalletHandle,
String, // verkey
String, // metadata
Box<dyn Fn(IndyResult<()>) + Send>,
),
GetKeyMetadata(
WalletHandle,
String, // verkey
Box<dyn Fn(IndyResult<String>) + Send>,
),
CryptoSign(
WalletHandle,
String, // my vk
Vec<u8>, // msg
Box<dyn Fn(IndyResult<Vec<u8>>) + Send>,
),
CryptoVerify(
String, // their vk
Vec<u8>, // msg
Vec<u8>, // signature
Box<dyn Fn(IndyResult<bool>) + Send>,
),
AuthenticatedEncrypt(
WalletHandle,
String, // my vk
String, // their vk
Vec<u8>, // msg
Box<dyn Fn(IndyResult<Vec<u8>>) + Send>,
),
AuthenticatedDecrypt(
WalletHandle,
String, // my vk
Vec<u8>, // encrypted msg
Box<dyn Fn(IndyResult<(String, Vec<u8>)>) + Send>,
),
AnonymousEncrypt(
String, // their vk
Vec<u8>, // msg
Box<dyn Fn(IndyResult<Vec<u8>>) + Send>,
),
AnonymousDecrypt(
WalletHandle,
String, // my vk
Vec<u8>, // msg
Box<dyn Fn(IndyResult<Vec<u8>>) + Send>,
),
PackMessage(
Vec<u8>, // plaintext message
Vec<String>, // list of receiver's keys
Option<String>, // senders verkey
WalletHandle,
Box<dyn Fn(IndyResult<Vec<u8>>) + Send>,
),
UnpackMessage(
JWE,
WalletHandle,
Box<dyn Fn(IndyResult<Vec<u8>>) + Send>,
),
}
pub struct CryptoCommandExecutor {
wallet_service: Rc<WalletService>,
crypto_service: Rc<CryptoService>,
}
impl CryptoCommandExecutor {
pub fn new(
wallet_service: Rc<WalletService>,
crypto_service: Rc<CryptoService>,
) -> CryptoCommandExecutor {
CryptoCommandExecutor {
wallet_service,
crypto_service,
}
}
pub fn execute(&self, command: CryptoCommand) {
match command {
CryptoCommand::CreateKey(wallet_handle, key_info, cb) => {
debug!("CreateKey command received");
cb(self.create_key(wallet_handle, &key_info));
}
CryptoCommand::SetKeyMetadata(wallet_handle, verkey, metadata, cb) => {
debug!("SetKeyMetadata command received");
cb(self.set_key_metadata(wallet_handle, &verkey, &metadata));
}
CryptoCommand::GetKeyMetadata(wallet_handle, verkey, cb) => {
debug!("GetKeyMetadata command received");
cb(self.get_key_metadata(wallet_handle, &verkey));
}
CryptoCommand::CryptoSign(wallet_handle, my_vk, msg, cb) => {
debug!("CryptoSign command received");
cb(self.crypto_sign(wallet_handle, &my_vk, &msg));
}
CryptoCommand::CryptoVerify(their_vk, msg, signature, cb) => {
debug!("CryptoVerify command received");
cb(self.crypto_verify(&their_vk, &msg, &signature));
}
CryptoCommand::AuthenticatedEncrypt(wallet_handle, my_vk, their_vk, msg, cb) => {
debug!("AuthenticatedEncrypt command received");
cb(self.authenticated_encrypt(wallet_handle, &my_vk, &their_vk, &msg));
}
CryptoCommand::AuthenticatedDecrypt(wallet_handle, my_vk, encrypted_msg, cb) => {
debug!("AuthenticatedDecrypt command received");
cb(self.authenticated_decrypt(wallet_handle, &my_vk, &encrypted_msg));
}
CryptoCommand::AnonymousEncrypt(their_vk, msg, cb) => {
debug!("AnonymousEncrypt command received");
cb(self.anonymous_encrypt(&their_vk, &msg));
}
CryptoCommand::AnonymousDecrypt(wallet_handle, my_vk, encrypted_msg, cb) => {
debug!("AnonymousDecrypt command received");
cb(self.anonymous_decrypt(wallet_handle, &my_vk, &encrypted_msg));
}
CryptoCommand::PackMessage(message, receivers, sender_vk, wallet_handle, cb) => {
debug!("PackMessage command received");
cb(self.pack_msg(message, receivers, sender_vk, wallet_handle));
}
CryptoCommand::UnpackMessage(jwe_json, wallet_handle, cb) => {
debug!("UnpackMessage command received");
cb(self.unpack_msg(jwe_json, wallet_handle));
}
};
}
fn create_key(&self, wallet_handle: WalletHandle, key_info: &KeyInfo) -> IndyResult<String> {
debug!(
"create_key >>> wallet_handle: {:?}, key_info: {:?}",
wallet_handle,
secret!(key_info)
);
let key = self.crypto_service.create_key(key_info)?;
self.wallet_service
.add_indy_object(wallet_handle, &key.verkey, &key, &HashMap::new())?;
let res = key.verkey.to_string();
debug!("create_key <<< res: {:?}", res);
Ok(res)
}
fn crypto_sign(&self, wallet_handle: WalletHandle, my_vk: &str, msg: &[u8]) -> IndyResult<Vec<u8>> {
trace!(
"crypto_sign >>> wallet_handle: {:?}, sender_vk: {:?}, msg: {:?}",
wallet_handle, my_vk, msg
);
self.crypto_service.validate_key(my_vk)?;
let key: Key = self.wallet_service.get_indy_object(
wallet_handle,
&my_vk,
&RecordOptions::id_value(),
)?;
let res = self.crypto_service.sign(&key, msg)?;
trace!("crypto_sign <<< res: {:?}", res);
Ok(res)
}
fn crypto_verify(&self,
their_vk: &str,
msg: &[u8],
signature: &[u8]) -> IndyResult<bool> {
trace!(
"crypto_verify >>> their_vk: {:?}, msg: {:?}, signature: {:?}",
their_vk, msg, signature
);
self.crypto_service.validate_key(their_vk)?;
let res = self.crypto_service.verify(their_vk, msg, signature)?;
trace!("crypto_verify <<< res: {:?}", res);
Ok(res)
}
//TODO begin deprecation process this function. It will be replaced by pack
fn authenticated_encrypt(
&self,
wallet_handle: WalletHandle,
my_vk: &str,
their_vk: &str,
msg: &[u8],
) -> IndyResult<Vec<u8>> {
trace!("authenticated_encrypt >>> wallet_handle: {:?}, my_vk: {:?}, their_vk: {:?}, msg: {:?}", wallet_handle, my_vk, their_vk, msg);
self.crypto_service.validate_key(my_vk)?;
self.crypto_service.validate_key(their_vk)?;
let my_key: Key = self.wallet_service.get_indy_object(
wallet_handle,
my_vk,
&RecordOptions::id_value(),
)?;
let msg = self.crypto_service.create_combo_box(&my_key, &their_vk, msg)?;
let msg = msg.to_msg_pack()
.map_err(|e| err_msg(IndyErrorKind::InvalidState, format!("Can't serialize ComboBox: {:?}", e)))?;
let res = self.crypto_service.crypto_box_seal(&their_vk, &msg)?;
trace!("authenticated_encrypt <<< res: {:?}", res);
Ok(res)
}
//TODO begin deprecation process this function. It will be replaced by unpack
fn authenticated_decrypt(
&self,
wallet_handle: WalletHandle,
my_vk: &str,
msg: &[u8],
) -> IndyResult<(String, Vec<u8>)> {
trace!("authenticated_decrypt >>> wallet_handle: {:?}, my_vk: {:?}, msg: {:?}", wallet_handle, my_vk, msg);
self.crypto_service.validate_key(my_vk)?;
let my_key: Key = self.wallet_service.get_indy_object(
wallet_handle,
my_vk,
&RecordOptions::id_value(),
)?;
let decrypted_msg = self.crypto_service.crypto_box_seal_open(&my_key, &msg)?;
let parsed_msg = ComboBox::from_msg_pack(decrypted_msg.as_slice())
.map_err(|err| err_msg(IndyErrorKind::InvalidStructure, format!("Can't deserialize ComboBox: {:?}", err)))?;
let doc: Vec<u8> = base64::decode(&parsed_msg.msg)
.map_err(|err| err_msg(IndyErrorKind::InvalidStructure, format!("Can't decode internal msg filed from base64 {}", err)))?;
let nonce: Vec<u8> = base64::decode(&parsed_msg.nonce)
.map_err(|err| err_msg(IndyErrorKind::InvalidStructure, format!("Can't decode nonce from base64 {}", err)))?;
let decrypted_msg = self.crypto_service.crypto_box_open(&my_key, &parsed_msg.sender, &doc, &nonce)?;
let res = (parsed_msg.sender, decrypted_msg);
trace!("authenticated_decrypt <<< res: {:?}", res);
Ok(res)
}
fn anonymous_encrypt(&self,
their_vk: &str,
msg: &[u8]) -> IndyResult<Vec<u8>> {
trace!(
"anonymous_encrypt >>> their_vk: {:?}, msg: {:?}",
their_vk, msg
);
self.crypto_service.validate_key(their_vk)?;
let res = self.crypto_service.crypto_box_seal(their_vk, &msg)?;
trace!("anonymous_encrypt <<< res: {:?}", res);
Ok(res)
}
fn anonymous_decrypt(&self,
wallet_handle: WalletHandle,
my_vk: &str,
encrypted_msg: &[u8]) -> IndyResult<Vec<u8>> {
trace!(
"anonymous_decrypt >>> wallet_handle: {:?}, my_vk: {:?}, encrypted_msg: {:?}",
wallet_handle, my_vk, encrypted_msg
);
self.crypto_service.validate_key(&my_vk)?;
let my_key: Key = self.wallet_service.get_indy_object(
wallet_handle,
&my_vk,
&RecordOptions::id_value(),
)?;
let res = self
.crypto_service
.crypto_box_seal_open(&my_key, &encrypted_msg)?;
trace!("anonymous_decrypt <<< res: {:?}", res);
Ok(res)
}
fn set_key_metadata(&self, wallet_handle: WalletHandle, verkey: &str, metadata: &str) -> IndyResult<()> {
debug!(
"set_key_metadata >>> wallet_handle: {:?}, verkey: {:?}, metadata: {:?}",
wallet_handle, verkey, metadata
);
self.crypto_service.validate_key(verkey)?;
let metadata = KeyMetadata {
value: metadata.to_string(),
};
self.wallet_service
.upsert_indy_object(wallet_handle, &verkey, &metadata)?;
debug!("set_key_metadata <<<");
Ok(())
}
fn get_key_metadata(&self, wallet_handle: WalletHandle, verkey: &str) -> IndyResult<String> {
debug!(
"get_key_metadata >>> wallet_handle: {:?}, verkey: {:?}",
wallet_handle, verkey
);
self.crypto_service.validate_key(verkey)?;
let metadata = self.wallet_service.get_indy_object::<KeyMetadata>(
wallet_handle,
&verkey,
&RecordOptions::id_value(),
)?;
let res = metadata.value;
debug!("get_key_metadata <<< res: {:?}", res);
Ok(res)
}
//TODO: Refactor pack to be more modular to version changes or crypto_scheme changes
//this match statement is super messy, but the easiest way to comply with current architecture
pub fn pack_msg(
&self,
message: Vec<u8>,
receiver_list: Vec<String>,
sender_vk: Option<String>,
wallet_handle: WalletHandle,
) -> IndyResult<Vec<u8>> {
//break early and error out if no receivers keys are provided
if receiver_list.is_empty() {
return Err(err_msg(IndyErrorKind::InvalidStructure, "No receiver keys found".to_string()));
}
//generate content encryption key that will encrypt `message`
let cek = chacha20poly1305_ietf::gen_key();
let base64_protected = if let Some(sender_vk) = sender_vk {
self.crypto_service.validate_key(&sender_vk)?;
//returns authcrypted pack_message format. See Wire message format HIPE for details
self._prepare_protected_authcrypt(&cek, receiver_list, &sender_vk, wallet_handle)?
} else {
//returns anoncrypted pack_message format. See Wire message format HIPE for details
self._prepare_protected_anoncrypt(&cek, receiver_list)?
};
// Use AEAD to encrypt `message` with "protected" data as "associated data"
let (ciphertext, iv, tag) =
self.crypto_service
.encrypt_plaintext(message, &base64_protected, &cek);
self._format_pack_message(&base64_protected, &ciphertext, &iv, &tag)
}
fn _prepare_protected_anoncrypt(&self,
cek: &chacha20poly1305_ietf::Key,
receiver_list: Vec<String>,
) -> IndyResult<String> {
let mut encrypted_recipients_struct : Vec<Recipient> = Vec::with_capacity(receiver_list.len());
for their_vk in receiver_list {
//encrypt sender verkey
let enc_cek = self.crypto_service.crypto_box_seal(&their_vk, &cek[..])?;
//create recipient struct and push to encrypted list
encrypted_recipients_struct.push(Recipient {
encrypted_key: base64::encode_urlsafe(enc_cek.as_slice()),
header: Header {
kid: their_vk,
sender: None,
iv: None
},
});
} // end for-loop
Ok(self._base64_encode_protected(encrypted_recipients_struct, false)?)
}
fn _prepare_protected_authcrypt(&self,
cek: &chacha20poly1305_ietf::Key,
receiver_list: Vec<String>, sender_vk: &str,
wallet_handle: WalletHandle,
) -> IndyResult<String> {
let mut encrypted_recipients_struct : Vec<Recipient> = vec![];
//get my_key from my wallet
let my_key = self.wallet_service.get_indy_object(
wallet_handle,
sender_vk,
&RecordOptions::id_value()
)?;
//encrypt cek for recipient
for their_vk in receiver_list {
let (enc_cek, iv) = self.crypto_service.crypto_box(&my_key, &their_vk, &cek[..])?;
let enc_sender = self.crypto_service.crypto_box_seal(&their_vk, sender_vk.as_bytes())?;
//create recipient struct and push to encrypted list
encrypted_recipients_struct.push(Recipient {
encrypted_key: base64::encode_urlsafe(enc_cek.as_slice()),
header: Header {
kid: their_vk,
sender: Some(base64::encode_urlsafe(enc_sender.as_slice())),
iv: Some(base64::encode_urlsafe(iv.as_slice()))
},
});
} // end for-loop
Ok(self._base64_encode_protected(encrypted_recipients_struct, true)?)
}
fn _base64_encode_protected(&self, encrypted_recipients_struct: Vec<Recipient>, alg_is_authcrypt: bool) -> IndyResult<String> {
let alg_val = if alg_is_authcrypt { String::from(PROTECTED_HEADER_ALG_AUTH) } else { String::from(PROTECTED_HEADER_ALG_ANON) };
//structure protected and base64URL encode it
let protected_struct = Protected {
enc: PROTECTED_HEADER_ENC.to_string(),
typ: PROTECTED_HEADER_TYP.to_string(),
alg: alg_val,
recipients: encrypted_recipients_struct,
};
let protected_encoded = serde_json::to_string(&protected_struct).map_err(|err| {
err_msg(IndyErrorKind::InvalidStructure, format!(
"Failed to serialize protected field {}",
err
))
})?;
Ok(base64::encode_urlsafe(protected_encoded.as_bytes()))
}
fn _format_pack_message(
&self,
base64_protected: &str,
ciphertext: &str,
iv: &str,
tag: &str
) -> IndyResult<Vec<u8>> {
//serialize pack message and return as vector of bytes
let jwe_struct = JWE {
protected: base64_protected.to_string(),
iv: iv.to_string(),
ciphertext: ciphertext.to_string(),
tag: tag.to_string()
};
serde_json::to_vec(&jwe_struct).map_err(|err| {
err_msg(IndyErrorKind::InvalidStructure, format!(
"Failed to serialize JWE {}",
err
))
})
}
pub fn unpack_msg(&self, jwe_struct: JWE, wallet_handle: WalletHandle) -> IndyResult<Vec<u8>> {
//decode protected data
let protected_decoded_vec = base64::decode_urlsafe(&jwe_struct.protected)?;
let protected_decoded_str = String::from_utf8(protected_decoded_vec).map_err(|err| {
err_msg(IndyErrorKind::InvalidStructure, format!(
"Failed to utf8 encode data {}",
err
))
})?;
//convert protected_data_str to struct
let protected_struct: Protected = serde_json::from_str(&protected_decoded_str).map_err(|err| {
err_msg(IndyErrorKind::InvalidStructure, format!(
"Failed to deserialize protected data {}",
err
))
})?;
//extract recipient that matches a key in the wallet
let (recipient, is_auth_recipient) = self._find_correct_recipient(protected_struct, wallet_handle)?;
//get cek and sender data
let (sender_verkey_option, cek) = if is_auth_recipient {
self._unpack_cek_authcrypt(recipient.clone(), wallet_handle)
} else {
self._unpack_cek_anoncrypt(recipient.clone(), wallet_handle)
}?; //close cek and sender_data match statement
//decrypt message
let message = self.crypto_service.decrypt_ciphertext(
&jwe_struct.ciphertext,
&jwe_struct.protected,
&jwe_struct.iv,
&jwe_struct.tag,
&cek,
)?;
//serialize and return decrypted message
let res = UnpackMessage {
message,
sender_verkey: sender_verkey_option,
recipient_verkey: recipient.header.kid
};
serde_json::to_vec(&res).map_err(|err| {
err_msg(IndyErrorKind::InvalidStructure, format!(
"Failed to serialize message {}",
err
))
})
}
fn _find_correct_recipient(&self, protected_struct: Protected, wallet_handle: WalletHandle) -> IndyResult<(Recipient, bool)>{
for recipient in protected_struct.recipients {
let my_key_res = self.wallet_service.get_indy_object::<Key>(
wallet_handle,
&recipient.header.kid,
&RecordOptions::id_value()
);
if my_key_res.is_ok() {
return Ok((recipient.clone(), recipient.header.sender.is_some()))
}
}
Err(IndyError::from(IndyErrorKind::WalletItemNotFound))
}
fn _unpack_cek_authcrypt(&self, recipient: Recipient, wallet_handle: WalletHandle) -> IndyResult<(Option<String>, chacha20poly1305_ietf::Key)> {
let encrypted_key_vec = base64::decode_urlsafe(&recipient.encrypted_key)?;
let iv = base64::decode_urlsafe(&recipient.header.iv.unwrap())?;
let enc_sender_vk = base64::decode_urlsafe(&recipient.header.sender.unwrap())?;
//get my private key
let my_key = self.wallet_service.get_indy_object(
wallet_handle,
&recipient.header.kid,
&RecordOptions::id_value(),
)?;
//decrypt sender_vk
let sender_vk_vec = self.crypto_service.crypto_box_seal_open(&my_key, enc_sender_vk.as_slice())?;
let sender_vk = String::from_utf8(sender_vk_vec)
.map_err(|err| err_msg(IndyErrorKind::InvalidStructure, format!("Failed to utf-8 encode sender_vk {}", err)))?;
//decrypt cek
let cek_as_vec = self.crypto_service.crypto_box_open(
&my_key,
&sender_vk,
encrypted_key_vec.as_slice(),
iv.as_slice())?;
//convert cek to chacha Key struct
let cek: chacha20poly1305_ietf::Key =
chacha20poly1305_ietf::Key::from_slice(&cek_as_vec[..]).map_err(
|err| {
err_msg(IndyErrorKind::InvalidStructure, format!("Failed to decrypt cek {}", err))
},
)?;
Ok((Some(sender_vk), cek))
}
fn _unpack_cek_anoncrypt(&self, recipient: Recipient, wallet_handle: WalletHandle) -> IndyResult<(Option<String>, chacha20poly1305_ietf::Key)> {
let encrypted_key_vec = base64::decode_urlsafe(&recipient.encrypted_key)?;
//get my private key
let my_key : Key = self.wallet_service.get_indy_object(
wallet_handle,
&recipient.header.kid,
&RecordOptions::id_value(),
)?;
//decrypt cek
let cek_as_vec = self.crypto_service
.crypto_box_seal_open(&my_key, encrypted_key_vec.as_slice())?;
//convert cek to chacha Key struct
let cek: chacha20poly1305_ietf::Key =
chacha20poly1305_ietf::Key::from_slice(&cek_as_vec[..]).map_err(
|err| {
err_msg(IndyErrorKind::InvalidStructure, format!("Failed to decrypt cek {}", err))
},
)?;
Ok((None, cek))
}
}