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
765 lines (681 loc) · 30.9 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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
use indy_api_types::{ErrorCode, CommandHandle, WalletHandle};
use crate::commands::{Command, CommandExecutor};
use crate::commands::crypto::CryptoCommand;
use crate::domain::crypto::pack::JWE;
use crate::domain::crypto::key::KeyInfo;
use indy_api_types::errors::prelude::*;
use indy_utils::ctypes;
use serde_json;
use libc::c_char;
/// Creates keys pair and stores in the wallet.
///
/// #Params
/// command_handle: Command handle to map callback to caller context.
/// wallet_handle: Wallet handle (created by open_wallet).
/// key_json: Key information as json. Example:
/// {
/// "seed": string, (optional) Seed that allows deterministic key creation (if not set random one will be created).
/// Can be UTF-8, base64 or hex string.
/// "crypto_type": string, // Optional (if not set then ed25519 curve is used); Currently only 'ed25519' value is supported for this field.
/// }
/// cb: Callback that takes command result as parameter.
///
/// #Returns
/// Error Code
/// cb:
/// - command_handle_: command handle to map callback to caller context.
/// - err: Error code.
/// - verkey: Ver key of generated key pair, also used as key identifier
///
/// #Errors
/// Common*
/// Wallet*
/// Crypto*
#[no_mangle]
pub extern fn indy_create_key(command_handle: CommandHandle,
wallet_handle: WalletHandle,
key_json: *const c_char,
cb: Option<extern fn(command_handle_: CommandHandle,
err: ErrorCode,
verkey: *const c_char)>) -> ErrorCode {
trace!("indy_create_key: >>> wallet_handle: {:?}, key_json: {:?}", wallet_handle, key_json);
check_useful_json!(key_json, ErrorCode::CommonInvalidParam3, KeyInfo);
check_useful_c_callback!(cb, ErrorCode::CommonInvalidParam4);
trace!("indy_create_key: entities >>> wallet_handle: {:?}, key_json: {:?}", wallet_handle, secret!(&key_json));
let result = CommandExecutor::instance()
.send(Command::Crypto(CryptoCommand::CreateKey(
wallet_handle,
key_json,
boxed_callback_string!("indy_create_key", cb, command_handle)
)));
let res = prepare_result!(result);
trace!("indy_create_key: <<< res: {:?}", res);
res
}
/// Saves/replaces the meta information for the giving key in the wallet.
///
/// #Params
/// command_handle: Command handle to map callback to caller context.
/// wallet_handle: Wallet handle (created by open_wallet).
/// verkey - the key (verkey, key id) to store metadata.
/// metadata - the meta information that will be store with the key.
/// cb: Callback that takes command result as parameter.
///
/// #Returns
/// Error Code
/// cb:
/// - command_handle_: command handle to map callback to caller context.
/// - err: Error code.
///
/// #Errors
/// Common*
/// Wallet*
/// Crypto*
#[no_mangle]
pub extern fn indy_set_key_metadata(command_handle: CommandHandle,
wallet_handle: WalletHandle,
verkey: *const c_char,
metadata: *const c_char,
cb: Option<extern fn(command_handle_: CommandHandle,
err: ErrorCode)>) -> ErrorCode {
trace!("indy_set_key_metadata: >>> wallet_handle: {:?}, verkey: {:?}, metadata: {:?}", wallet_handle, verkey, metadata);
check_useful_c_str!(verkey, ErrorCode::CommonInvalidParam3);
check_useful_c_str_empty_accepted!(metadata, ErrorCode::CommonInvalidParam4);
check_useful_c_callback!(cb, ErrorCode::CommonInvalidParam5);
trace!("indy_set_key_metadata: entities >>> wallet_handle: {:?}, verkey: {:?}, metadata: {:?}", wallet_handle, verkey, metadata);
let result = CommandExecutor::instance()
.send(Command::Crypto(CryptoCommand::SetKeyMetadata(
wallet_handle,
verkey,
metadata,
Box::new(move |result| {
let err = prepare_result!(result);
trace!("indy_set_key_metadata: ");
cb(command_handle, err)
})
)));
let res = prepare_result!(result);
trace!("indy_set_key_metadata: <<< res: {:?}", res);
res
}
/// Retrieves the meta information for the giving key in the wallet.
///
/// #Params
/// command_handle: Command handle to map callback to caller context.
/// wallet_handle: Wallet handle (created by open_wallet).
/// verkey - The key (verkey, key id) to retrieve metadata.
/// cb: Callback that takes command result as parameter.
///
/// #Returns
/// Error Code
/// cb:
/// - command_handle_: Command handle to map callback to caller context.
/// - err: Error code.
/// - metadata - The meta information stored with the key; Can be null if no metadata was saved for this key.
///
/// #Errors
/// Common*
/// Wallet*
/// Crypto*
#[no_mangle]
pub extern fn indy_get_key_metadata(command_handle: CommandHandle,
wallet_handle: WalletHandle,
verkey: *const c_char,
cb: Option<extern fn(command_handle_: CommandHandle,
err: ErrorCode,
metadata: *const c_char)>) -> ErrorCode {
trace!("indy_get_key_metadata: >>> wallet_handle: {:?}, verkey: {:?}", wallet_handle, verkey);
check_useful_c_str!(verkey, ErrorCode::CommonInvalidParam3);
check_useful_c_callback!(cb, ErrorCode::CommonInvalidParam4);
trace!("indy_get_key_metadata: entities >>> wallet_handle: {:?}, verkey: {:?}", wallet_handle, verkey);
let result = CommandExecutor::instance()
.send(Command::Crypto(CryptoCommand::GetKeyMetadata(
wallet_handle,
verkey,
boxed_callback_string!("indy_get_key_metadata", cb, command_handle)
)));
let res = prepare_result!(result);
trace!("indy_get_key_metadata: <<< res: {:?}", res);
res
}
/// Signs a message with a key.
///
/// Note to use DID keys with this function you can call indy_key_for_did to get key id (verkey)
/// for specific DID.
///
/// #Params
/// command_handle: command handle to map callback to user context.
/// wallet_handle: wallet handler (created by open_wallet).
/// signer_vk: id (verkey) of message signer. The key must be created by calling indy_create_key or indy_create_and_store_my_did
/// message_raw: a pointer to first byte of message to be signed
/// message_len: a message length
/// cb: Callback that takes command result as parameter.
///
/// #Returns
/// a signature string
///
/// #Errors
/// Common*
/// Wallet*
/// Crypto*
#[no_mangle]
pub extern fn indy_crypto_sign(command_handle: CommandHandle,
wallet_handle: WalletHandle,
signer_vk: *const c_char,
message_raw: *const u8,
message_len: u32,
cb: Option<extern fn(command_handle_: CommandHandle,
err: ErrorCode,
signature_raw: *const u8,
signature_len: u32)>) -> ErrorCode {
trace!("indy_crypto_sign: >>> wallet_handle: {:?}, signer_vk: {:?}, message_raw: {:?}, message_len: {:?}",
wallet_handle, signer_vk, message_raw, message_len);
check_useful_c_str!(signer_vk, ErrorCode::CommonInvalidParam3);
check_useful_c_byte_array!(message_raw, message_len, ErrorCode::CommonInvalidParam4, ErrorCode::CommonInvalidParam5);
check_useful_c_callback!(cb, ErrorCode::CommonInvalidParam6);
trace!("indy_crypto_sign: entities >>> wallet_handle: {:?}, signer_vk: {:?}, message_raw: {:?}, message_len: {:?}",
wallet_handle, signer_vk, message_raw, message_len);
let result = CommandExecutor::instance()
.send(Command::Crypto(CryptoCommand::CryptoSign(
wallet_handle,
signer_vk,
message_raw,
Box::new(move |result| {
let (err, signature) = prepare_result_1!(result, Vec::new());
trace!("indy_crypto_sign: signature: {:?}", signature);
let (signature_raw, signature_len) = ctypes::vec_to_pointer(&signature);
cb(command_handle, err, signature_raw, signature_len)
})
)));
let res = prepare_result!(result);
trace!("indy_crypto_sign: <<< res: {:?}", res);
res
}
/// Verify a signature with a verkey.
///
/// Note to use DID keys with this function you can call indy_key_for_did to get key id (verkey)
/// for specific DID.
///
/// #Params
/// command_handle: command handle to map callback to user context.
/// signer_vk: verkey of the message signer
/// message_raw: a pointer to first byte of message that has been signed
/// message_len: a message length
/// signature_raw: a pointer to first byte of signature to be verified
/// signature_len: a signature length
/// cb: Callback that takes command result as parameter.
///
/// #Returns
/// valid: true - if signature is valid, false - otherwise
///
/// #Errors
/// Common*
/// Wallet*
/// Ledger*
/// Crypto*
#[no_mangle]
pub extern fn indy_crypto_verify(command_handle: CommandHandle,
signer_vk: *const c_char,
message_raw: *const u8,
message_len: u32,
signature_raw: *const u8,
signature_len: u32,
cb: Option<extern fn(command_handle_: CommandHandle,
err: ErrorCode,
valid: bool)>) -> ErrorCode {
trace!("indy_crypto_verify: >>> signer_vk: {:?}, message_raw: {:?}, message_len: {:?}, signature_raw: {:?}, signature_len: {:?}",
signer_vk, message_raw, message_len, signature_raw, signature_len);
check_useful_c_str!(signer_vk, ErrorCode::CommonInvalidParam2);
check_useful_c_byte_array!(message_raw, message_len, ErrorCode::CommonInvalidParam3, ErrorCode::CommonInvalidParam4);
check_useful_c_byte_array!(signature_raw, signature_len, ErrorCode::CommonInvalidParam5, ErrorCode::CommonInvalidParam6);
check_useful_c_callback!(cb, ErrorCode::CommonInvalidParam7);
trace!("indy_crypto_verify: entities >>> signer_vk: {:?}, message_raw: {:?}, message_len: {:?}, signature_raw: {:?}, signature_len: {:?}",
signer_vk, message_raw, message_len, signature_raw, signature_len);
let result = CommandExecutor::instance()
.send(Command::Crypto(CryptoCommand::CryptoVerify(
signer_vk,
message_raw,
signature_raw,
Box::new(move |result| {
let (err, valid) = prepare_result_1!(result, false);
trace!("indy_crypto_verify: valid: {:?}", valid);
cb(command_handle, err, valid)
})
)));
let res = prepare_result!(result);
trace!("indy_crypto_verify: <<< res: {:?}", res);
res
}
/// **** THIS FUNCTION WILL BE DEPRECATED USE indy_pack_message() INSTEAD ****
/// Encrypt a message by authenticated-encryption scheme.
///
/// Sender can encrypt a confidential message specifically for Recipient, using Sender's public key.
/// Using Recipient's public key, Sender can compute a shared secret key.
/// Using Sender's public key and his secret key, Recipient can compute the exact same shared secret key.
/// That shared secret key can be used to verify that the encrypted message was not tampered with,
/// before eventually decrypting it.
///
/// Note to use DID keys with this function you can call indy_key_for_did to get key id (verkey)
/// for specific DID.
///
/// #Params
/// command_handle: command handle to map callback to user context.
/// wallet_handle: wallet handle (created by open_wallet).
/// sender_vk: id (verkey) of message sender. The key must be created by calling indy_create_key or indy_create_and_store_my_did
/// recipient_vk: id (verkey) of message recipient
/// message_raw: a pointer to first byte of message that to be encrypted
/// message_len: a message length
/// cb: Callback that takes command result as parameter.
///
/// #Returns
/// an encrypted message as a pointer to array of bytes.
///
/// #Errors
/// Common*
/// Wallet*
/// Ledger*
/// Crypto*
#[no_mangle]
pub extern fn indy_crypto_auth_crypt(command_handle: CommandHandle,
wallet_handle: WalletHandle,
sender_vk: *const c_char,
recipient_vk: *const c_char,
msg_data: *const u8,
msg_len: u32,
cb: Option<extern fn(command_handle_: CommandHandle,
err: ErrorCode,
encrypted_msg: *const u8,
encrypted_len: u32)>) -> ErrorCode {
trace!("indy_crypto_auth_crypt: >>> wallet_handle: {:?}, sender_vk: {:?}, recipient_vk: {:?}, msg_data: {:?}, msg_len: {:?}",
wallet_handle, sender_vk, recipient_vk, msg_data, msg_len);
check_useful_c_str!(sender_vk, ErrorCode::CommonInvalidParam3);
check_useful_c_str!(recipient_vk, ErrorCode::CommonInvalidParam4);
check_useful_c_byte_array!(msg_data, msg_len, ErrorCode::CommonInvalidParam5, ErrorCode::CommonInvalidParam6);
check_useful_c_callback!(cb, ErrorCode::CommonInvalidParam7);
trace!("indy_crypto_auth_crypt: entities >>> wallet_handle: {:?}, sender_vk: {:?}, recipient_vk: {:?}, msg_data: {:?}, msg_len: {:?}",
wallet_handle, sender_vk, recipient_vk, msg_data, msg_len);
let result = CommandExecutor::instance()
.send(Command::Crypto(CryptoCommand::AuthenticatedEncrypt(
wallet_handle,
sender_vk,
recipient_vk,
msg_data,
Box::new(move |result| {
let (err, encrypted_msg) = prepare_result_1!(result, Vec::new());
trace!("indy_crypto_auth_crypt: encrypted_msg: {:?}", encrypted_msg);
let (encrypted_msg_raw, encrypted_msg_len) = ctypes::vec_to_pointer(&encrypted_msg);
cb(command_handle, err, encrypted_msg_raw, encrypted_msg_len)
})
)));
let res = prepare_result!(result);
trace!("indy_crypto_auth_crypt: <<< res: {:?}", res);
res
}
/// **** THIS FUNCTION WILL BE DEPRECATED USE indy_unpack_message() INSTEAD ****
/// Decrypt a message by authenticated-encryption scheme.
///
/// Sender can encrypt a confidential message specifically for Recipient, using Sender's public key.
/// Using Recipient's public key, Sender can compute a shared secret key.
/// Using Sender's public key and his secret key, Recipient can compute the exact same shared secret key.
/// That shared secret key can be used to verify that the encrypted message was not tampered with,
/// before eventually decrypting it.
///
/// Note to use DID keys with this function you can call indy_key_for_did to get key id (verkey)
/// for specific DID.
///
/// #Params
/// command_handle: command handle to map callback to user context.
/// wallet_handle: wallet handler (created by open_wallet).
/// recipient_vk: id (verkey) of message recipient. The key must be created by calling indy_create_key or indy_create_and_store_my_did
/// encrypted_msg_raw: a pointer to first byte of message that to be decrypted
/// encrypted_msg_len: a message length
/// cb: Callback that takes command result as parameter.
///
/// #Returns
/// sender verkey and decrypted message as a pointer to array of bytes
///
/// #Errors
/// Common*
/// Wallet*
/// Crypto*
#[no_mangle]
pub extern fn indy_crypto_auth_decrypt(command_handle: CommandHandle,
wallet_handle: WalletHandle,
recipient_vk: *const c_char,
encrypted_msg: *const u8,
encrypted_len: u32,
cb: Option<extern fn(command_handle_: CommandHandle,
err: ErrorCode,
sender_vk: *const c_char,
msg_data: *const u8,
msg_len: u32)>) -> ErrorCode {
trace!("indy_crypto_auth_decrypt: >>> wallet_handle: {:?}, recipient_vk: {:?}, encrypted_msg: {:?}, encrypted_len: {:?}",
wallet_handle, recipient_vk, encrypted_msg, encrypted_len);
check_useful_c_str!(recipient_vk, ErrorCode::CommonInvalidParam3);
check_useful_c_byte_array!(encrypted_msg, encrypted_len, ErrorCode::CommonInvalidParam4, ErrorCode::CommonInvalidParam5);
check_useful_c_callback!(cb, ErrorCode::CommonInvalidParam6);
trace!("indy_crypto_auth_decrypt: entities >>> wallet_handle: {:?}, recipient_vk: {:?}, encrypted_msg: {:?}, encrypted_len: {:?}",
wallet_handle, recipient_vk, encrypted_msg, encrypted_len);
let result = CommandExecutor::instance()
.send(Command::Crypto(CryptoCommand::AuthenticatedDecrypt(
wallet_handle,
recipient_vk,
encrypted_msg,
Box::new(move |result| {
let (err, sender_vk, msg) = prepare_result_2!(result, String::new(), Vec::new());
trace!("indy_crypto_auth_decrypt: sender_vk: {:?}, msg: {:?}", sender_vk, msg);
let (msg_data, msg_len) = ctypes::vec_to_pointer(&msg);
let sender_vk = ctypes::string_to_cstring(sender_vk);
cb(command_handle, err, sender_vk.as_ptr(), msg_data, msg_len)
})
)));
let res = prepare_result!(result);
trace!("indy_crypto_auth_decrypt: <<< res: {:?}", res);
res
}
/// Encrypts a message by anonymous-encryption scheme.
///
/// Sealed boxes are designed to anonymously send messages to a Recipient given its public key.
/// Only the Recipient can decrypt these messages, using its private key.
/// While the Recipient can verify the integrity of the message, it cannot verify the identity of the Sender.
///
/// Note to use DID keys with this function you can call indy_key_for_did to get key id (verkey)
/// for specific DID.
///
/// Note: use indy_pack_message() function for A2A goals.
///
/// #Params
/// command_handle: command handle to map callback to user context.
/// recipient_vk: verkey of message recipient
/// message_raw: a pointer to first byte of message that to be encrypted
/// message_len: a message length
/// cb: Callback that takes command result as parameter.
///
/// #Returns
/// an encrypted message as a pointer to array of bytes
///
/// #Errors
/// Common*
/// Wallet*
/// Ledger*
/// Crypto*
#[no_mangle]
pub extern fn indy_crypto_anon_crypt(command_handle: CommandHandle,
recipient_vk: *const c_char,
msg_data: *const u8,
msg_len: u32,
cb: Option<extern fn(command_handle_: CommandHandle,
err: ErrorCode,
encrypted_msg: *const u8,
encrypted_len: u32)>) -> ErrorCode {
trace!("indy_crypto_anon_crypt: >>> recipient_vk: {:?}, msg_data: {:?}, msg_len: {:?}", recipient_vk, msg_data, msg_len);
check_useful_c_str!(recipient_vk, ErrorCode::CommonInvalidParam2);
check_useful_c_byte_array!(msg_data, msg_len, ErrorCode::CommonInvalidParam3, ErrorCode::CommonInvalidParam4);
check_useful_c_callback!(cb, ErrorCode::CommonInvalidParam5);
trace!("indy_crypto_anon_crypt: entities >>> recipient_vk: {:?}, msg_data: {:?}, msg_len: {:?}", recipient_vk, msg_data, msg_len);
let result = CommandExecutor::instance()
.send(Command::Crypto(CryptoCommand::AnonymousEncrypt(
recipient_vk,
msg_data,
Box::new(move |result| {
let (err, encrypted_msg) = prepare_result_1!(result, Vec::new());
trace!("indy_crypto_anon_crypt: encrypted_msg: {:?}", encrypted_msg);
let (encrypted_msg_raw, encrypted_msg_len) = ctypes::vec_to_pointer(&encrypted_msg);
cb(command_handle, err, encrypted_msg_raw, encrypted_msg_len)
})
)));
let res = prepare_result!(result);
trace!("indy_crypto_anon_crypt: <<< res: {:?}", res);
res
}
/// Decrypts a message by anonymous-encryption scheme.
///
/// Sealed boxes are designed to anonymously send messages to a Recipient given its public key.
/// Only the Recipient can decrypt these messages, using its private key.
/// While the Recipient can verify the integrity of the message, it cannot verify the identity of the Sender.
///
/// Note to use DID keys with this function you can call indy_key_for_did to get key id (verkey)
/// for specific DID.
///
/// Note: use indy_unpack_message() function for A2A goals.
///
/// #Params
/// command_handle: command handle to map callback to user context.
/// wallet_handle: wallet handler (created by open_wallet).
/// recipient_vk: id (verkey) of my key. The key must be created by calling indy_create_key or indy_create_and_store_my_did
/// encrypted_msg_raw: a pointer to first byte of message that to be decrypted
/// encrypted_msg_len: a message length
/// cb: Callback that takes command result as parameter.
///
/// #Returns
/// decrypted message as a pointer to an array of bytes
///
/// #Errors
/// Common*
/// Wallet*
/// Crypto*
#[no_mangle]
pub extern fn indy_crypto_anon_decrypt(command_handle: CommandHandle,
wallet_handle: WalletHandle,
recipient_vk: *const c_char,
encrypted_msg: *const u8,
encrypted_len: u32,
cb: Option<extern fn(command_handle_: CommandHandle,
err: ErrorCode,
msg_data: *const u8,
msg_len: u32)>) -> ErrorCode {
trace!("indy_crypto_anon_decrypt: >>> wallet_handle: {:?}, recipient_vk: {:?}, encrypted_msg: {:?}, encrypted_len: {:?}",
wallet_handle, recipient_vk, encrypted_msg, encrypted_len);
check_useful_c_str!(recipient_vk, ErrorCode::CommonInvalidParam3);
check_useful_c_byte_array!(encrypted_msg, encrypted_len, ErrorCode::CommonInvalidParam4, ErrorCode::CommonInvalidParam5);
check_useful_c_callback!(cb, ErrorCode::CommonInvalidParam6);
trace!("indy_crypto_anon_decrypt: entities >>> wallet_handle: {:?}, recipient_vk: {:?}, encrypted_msg: {:?}, encrypted_len: {:?}",
wallet_handle, recipient_vk, encrypted_msg, encrypted_len);
let result = CommandExecutor::instance()
.send(Command::Crypto(CryptoCommand::AnonymousDecrypt(
wallet_handle,
recipient_vk,
encrypted_msg,
Box::new(move |result| {
let (err, msg) = prepare_result_1!(result, Vec::new());
trace!("indy_crypto_anon_decrypt: msg: {:?}", msg);
let (msg_data, msg_len) = ctypes::vec_to_pointer(&msg);
cb(command_handle, err, msg_data, msg_len)
})
)));
let res = prepare_result!(result);
trace!("indy_crypto_anon_decrypt: <<< res: {:?}", res);
res
}
/// Packs a message by encrypting the message and serializes it in a JWE-like format (Experimental)
///
/// Note to use DID keys with this function you can call indy_key_for_did to get key id (verkey)
/// for specific DID.
///
/// #Params
/// command_handle: command handle to map callback to user context.
/// wallet_handle: wallet handle (created by open_wallet).
/// message: a pointer to the first byte of the message to be packed
/// message_len: the length of the message
/// receivers: a string in the format of a json list which will contain the list of receiver's keys
/// the message is being encrypted for.
/// Example:
/// "[<receiver edge_agent_1 verkey>, <receiver edge_agent_2 verkey>]"
/// sender: the sender's verkey as a string When null pointer is used in this parameter, anoncrypt is used
/// cb: Callback that takes command result as parameter.
///
/// #Returns
/// a JWE using authcrypt alg is defined below:
/// {
/// "protected": "b64URLencoded({
/// "enc": "xsalsa20poly1305",
/// "typ": "JWM/1.0",
/// "alg": "Authcrypt",
/// "recipients": [
/// {
/// "encrypted_key": base64URLencode(libsodium.crypto_box(my_key, their_vk, cek, cek_iv))
/// "header": {
/// "kid": "base58encode(recipient_verkey)",
/// "sender" : base64URLencode(libsodium.crypto_box_seal(their_vk, base58encode(sender_vk)),
/// "iv" : base64URLencode(cek_iv)
/// }
/// },
/// ],
/// })",
/// "iv": <b64URLencode(iv)>,
/// "ciphertext": b64URLencode(encrypt_detached({'@type'...}, protected_value_encoded, iv, cek),
/// "tag": <b64URLencode(tag)>
/// }
///
/// Alternative example in using anoncrypt alg is defined below:
/// {
/// "protected": "b64URLencoded({
/// "enc": "xsalsa20poly1305",
/// "typ": "JWM/1.0",
/// "alg": "Anoncrypt",
/// "recipients": [
/// {
/// "encrypted_key": base64URLencode(libsodium.crypto_box_seal(their_vk, cek)),
/// "header": {
/// "kid": base58encode(recipient_verkey),
/// }
/// },
/// ],
/// })",
/// "iv": b64URLencode(iv),
/// "ciphertext": b64URLencode(encrypt_detached({'@type'...}, protected_value_encoded, iv, cek),
/// "tag": b64URLencode(tag)
/// }
///
///
/// #Errors
/// Common*
/// Wallet*
/// Ledger*
/// Crypto*
#[no_mangle]
pub extern fn indy_pack_message(
command_handle: CommandHandle,
wallet_handle: WalletHandle,
message: *const u8,
message_len: u32,
receiver_keys: *const c_char,
sender: *const c_char,
cb: Option<extern fn(xcommand_handle: CommandHandle, err: ErrorCode, jwe_data: *const u8, jwe_len: u32)>,
) -> ErrorCode {
trace!("indy_pack_message: >>> wallet_handle: {:?}, message: {:?}, message_len {:?},\
receiver_keys: {:?}, sender: {:?}", wallet_handle, message, message_len, receiver_keys, sender);
check_useful_c_byte_array!(message, message_len, ErrorCode::CommonInvalidParam2, ErrorCode::CommonInvalidParam3);
check_useful_c_str!(receiver_keys, ErrorCode::CommonInvalidParam4);
check_useful_opt_c_str!(sender, ErrorCode::CommonInvalidParam5);
check_useful_c_callback!(cb, ErrorCode::CommonInvalidParam6);
trace!("indy_pack_message: entities >>> wallet_handle: {:?}, message: {:?}, message_len {:?},\
receiver_keys: {:?}, sender: {:?}", wallet_handle, message, message_len, receiver_keys, sender);
//parse json array of keys
let receiver_list = match serde_json::from_str::<Vec<String>>(&receiver_keys) {
Ok(x) => x,
Err(_) => {
return IndyError::from_msg(IndyErrorKind::InvalidParam(4), "Invalid RecipientKeys has been passed").into();
},
};
//break early and error out if no receivers keys are provided
if receiver_list.is_empty() {
return IndyError::from_msg(IndyErrorKind::InvalidParam(4), "Empty RecipientKeys has been passed").into();
}
let result = CommandExecutor::instance().send(Command::Crypto(CryptoCommand::PackMessage(
message,
receiver_list,
sender,
wallet_handle,
Box::new(move |result| {
let (err, jwe) = prepare_result_1!(result, Vec::new());
trace!("indy_auth_pack_message: jwe: {:?}", jwe);
let (jwe_data, jwe_len) = ctypes::vec_to_pointer(&jwe);
cb(command_handle, err, jwe_data, jwe_len)
}),
)));
let res = prepare_result!(result);
trace!("indy_auth_pack_message: <<< res: {:?}", res);
res
}
/// Unpacks a JWE-like formatted message outputted by indy_pack_message (Experimental)
///
/// #Params
/// command_handle: command handle to map callback to user context.
/// wallet_handle: wallet handle (created by open_wallet).
/// jwe_data: a pointer to the first byte of the JWE to be unpacked
/// jwe_len: the length of the JWE message in bytes
/// cb: Callback that takes command result as parameter.
///
/// #Returns
/// if authcrypt was used to pack the message returns this json structure:
/// {
/// message: <decrypted message>,
/// sender_verkey: <sender_verkey>,
/// recipient_verkey: <recipient_verkey>
/// }
///
/// OR
///
/// if anoncrypt was used to pack the message returns this json structure:
/// {
/// message: <decrypted message>,
/// recipient_verkey: <recipient_verkey>
/// }
///
///
/// #Errors
/// Common*
/// Wallet*
/// Ledger*
/// Crypto*
#[no_mangle]
pub extern fn indy_unpack_message(
command_handle: CommandHandle,
wallet_handle: WalletHandle,
jwe_data: *const u8,
jwe_len: u32,
cb: Option<
extern fn(
xcommand_handle: CommandHandle,
err: ErrorCode,
res_json_data : *const u8,
res_json_len : u32
),
>,
) -> ErrorCode {
trace!(
"indy_unpack_message: >>> wallet_handle: {:?}, jwe_data: {:?}, jwe_len {:?}",
wallet_handle,
jwe_data,
jwe_len
);
check_useful_c_byte_array!(jwe_data, jwe_len, ErrorCode::CommonInvalidParam2, ErrorCode::CommonInvalidParam3);
check_useful_c_callback!(cb, ErrorCode::CommonInvalidParam4);
trace!(
"indy_unpack_message: entities >>> wallet_handle: {:?}, jwe_data: {:?}, jwe_len {:?}",
wallet_handle,
jwe_data,
jwe_len
);
//serialize JWE to struct
let jwe_struct: JWE = match serde_json::from_slice(jwe_data.as_slice()) {
Ok(x) => x,
Err(_) => return ErrorCode::CommonInvalidParam3
};
let result = CommandExecutor::instance().send(Command::Crypto(CryptoCommand::UnpackMessage(
jwe_struct,
wallet_handle,
Box::new(move |result| {
let (err, res_json) = prepare_result_1!(result, Vec::new());
trace!("indy_unpack_message: cb command_handle: {:?}, err: {:?}, res_json: {:?}",
command_handle, err, res_json
);
let (res_json_data, res_json_len) = ctypes::vec_to_pointer(&res_json);
cb(command_handle, err, res_json_data, res_json_len)
}),
)));
let res = prepare_result!(result);
trace!("indy_unpack_message: <<< res: {:?}", res);
res
}