-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathwallet.rs
937 lines (817 loc) · 37.5 KB
/
wallet.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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
// Copyright 2019. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
use std::{
convert::TryFrom,
mem::size_of,
str::{from_utf8, FromStr},
sync::{Arc, RwLock},
};
use argon2::{
password_hash::{rand_core::OsRng, Decimal, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
Argon2,
};
use chacha20poly1305::{Key, KeyInit, Tag, XChaCha20Poly1305, XNonce};
use diesel::{prelude::*, SqliteConnection};
use log::*;
use tari_common_types::chain_metadata::ChainMetadata;
use tari_comms::{
multiaddr::Multiaddr,
peer_manager::{IdentitySignature, PeerFeatures},
tor::TorIdentity,
};
use tari_key_manager::cipher_seed::CipherSeed;
use tari_utilities::{
hex::{from_hex, Hex},
safe_array::SafeArray,
Hidden,
SafePassword,
};
use tokio::time::Instant;
use zeroize::Zeroize;
use crate::{
error::WalletStorageError,
schema::{client_key_values, wallet_settings},
storage::{
database::{DbKey, DbKeyValuePair, DbValue, WalletBackend, WriteOperation},
sqlite_db::scanned_blocks::ScannedBlockSql,
sqlite_utilities::wallet_db_connection::WalletDbConnection,
},
util::encryption::{decrypt_bytes_integral_nonce, encrypt_bytes_integral_nonce, Encryptable},
utxo_scanner_service::service::ScannedBlock,
};
const LOG_TARGET: &str = "wallet::storage::wallet";
/// A Sqlite backend for the Output Manager Service. The Backend is accessed via a connection pool to the Sqlite file.
#[derive(Clone)]
pub struct WalletSqliteDatabase {
database_connection: WalletDbConnection,
cipher: Arc<RwLock<XChaCha20Poly1305>>,
}
impl WalletSqliteDatabase {
pub fn new(database_connection: WalletDbConnection, passphrase: SafePassword) -> Result<Self, WalletStorageError> {
let cipher = get_db_encryption(&database_connection, passphrase)?;
Ok(Self {
database_connection,
cipher: Arc::new(RwLock::new(cipher)),
})
}
fn set_master_seed(&self, seed: &CipherSeed, conn: &SqliteConnection) -> Result<(), WalletStorageError> {
let cipher = acquire_read_lock!(self.cipher);
if WalletSettingSql::get(&DbKey::WalletBirthday, conn)?.is_none() {
let birthday = seed.birthday();
WalletSettingSql::new(DbKey::WalletBirthday, birthday.to_string()).set(conn)?;
}
let seed_bytes = Hidden::hide(seed.encipher(None)?);
let ciphertext_integral_nonce =
encrypt_bytes_integral_nonce(&cipher, b"wallet_setting_master_seed".to_vec(), seed_bytes)
.map_err(|e| WalletStorageError::AeadError(format!("Encryption Error:{}", e)))?;
WalletSettingSql::new(DbKey::MasterSeed, ciphertext_integral_nonce.to_hex()).set(conn)?;
Ok(())
}
fn get_master_seed(&self, conn: &SqliteConnection) -> Result<Option<CipherSeed>, WalletStorageError> {
let cipher = acquire_read_lock!(self.cipher);
if let Some(seed_str) = WalletSettingSql::get(&DbKey::MasterSeed, conn)? {
let seed = {
// Decrypted_key_bytes contains sensitive data regarding decrypted
// seed words. For this reason, we should zeroize the underlying data buffer
let decrypted_key_bytes = Hidden::hide(
decrypt_bytes_integral_nonce(
&cipher,
b"wallet_setting_master_seed".to_vec(),
&from_hex(seed_str.as_str())?,
)
.map_err(|e| WalletStorageError::AeadError(format!("Decryption Error:{}", e)))?,
);
CipherSeed::from_enciphered_bytes(decrypted_key_bytes.reveal(), None)?
};
Ok(Some(seed))
} else {
Ok(None)
}
}
fn decrypt_value<T: Encryptable<XChaCha20Poly1305>>(&self, o: T) -> Result<T, WalletStorageError> {
let cipher = acquire_read_lock!(self.cipher);
let o = o
.decrypt(&cipher)
.map_err(|e| WalletStorageError::AeadError(format!("Decryption Error:{}", e)))?;
Ok(o)
}
#[allow(dead_code)]
fn encrypt_value<T: Encryptable<XChaCha20Poly1305>>(&self, o: T) -> Result<T, WalletStorageError> {
let cipher = acquire_read_lock!(self.cipher);
o.encrypt(&cipher)
.map_err(|e| WalletStorageError::AeadError(format!("Encryption Error:{}", e)))
}
fn get_comms_address(&self, conn: &SqliteConnection) -> Result<Option<Multiaddr>, WalletStorageError> {
if let Some(key_str) = WalletSettingSql::get(&DbKey::CommsAddress, conn)? {
Ok(Some(
Multiaddr::from_str(key_str.as_str())
.map_err(|e| WalletStorageError::ConversionError(e.to_string()))?,
))
} else {
Ok(None)
}
}
fn get_comms_features(&self, conn: &SqliteConnection) -> Result<Option<PeerFeatures>, WalletStorageError> {
if let Some(key_str) = WalletSettingSql::get(&DbKey::CommsFeatures, conn)? {
let features = u64::from_str(&key_str).map_err(|e| WalletStorageError::ConversionError(e.to_string()))?;
let peer_features = PeerFeatures::from_bits(features);
Ok(peer_features)
} else {
Ok(None)
}
}
fn set_tor_id(&self, tor: TorIdentity, conn: &SqliteConnection) -> Result<(), WalletStorageError> {
let cipher = acquire_read_lock!(self.cipher);
let bytes =
Hidden::hide(bincode::serialize(&tor).map_err(|e| WalletStorageError::ConversionError(e.to_string()))?);
let ciphertext_integral_nonce = encrypt_bytes_integral_nonce(&cipher, b"wallet_setting_tor_id".to_vec(), bytes)
.map_err(|e| WalletStorageError::AeadError(format!("Encryption Error:{}", e)))?;
WalletSettingSql::new(DbKey::TorId, ciphertext_integral_nonce.to_hex()).set(conn)?;
Ok(())
}
fn get_tor_id(&self, conn: &SqliteConnection) -> Result<Option<DbValue>, WalletStorageError> {
let cipher = acquire_read_lock!(self.cipher);
if let Some(key_str) = WalletSettingSql::get(&DbKey::TorId, conn)? {
let id = {
// we must zeroize decrypted_key_bytes, as this contains sensitive data,
// including private key informations
let decrypted_key_bytes = Hidden::hide(
decrypt_bytes_integral_nonce(&cipher, b"wallet_setting_tor_id".to_vec(), &from_hex(&key_str)?)
.map_err(|e| WalletStorageError::AeadError(format!("Decryption Error:{}", e)))?,
);
bincode::deserialize(decrypted_key_bytes.reveal())
.map_err(|e| WalletStorageError::ConversionError(e.to_string()))?
};
Ok(Some(DbValue::TorId(id)))
} else {
Ok(None)
}
}
fn set_chain_metadata(&self, chain: ChainMetadata, conn: &SqliteConnection) -> Result<(), WalletStorageError> {
let bytes = bincode::serialize(&chain).map_err(|e| WalletStorageError::ConversionError(e.to_string()))?;
WalletSettingSql::new(DbKey::BaseNodeChainMetadata, bytes.to_hex()).set(conn)?;
Ok(())
}
fn get_chain_metadata(&self, conn: &SqliteConnection) -> Result<Option<ChainMetadata>, WalletStorageError> {
if let Some(key_str) = WalletSettingSql::get(&DbKey::BaseNodeChainMetadata, conn)? {
let chain_metadata = bincode::deserialize(&from_hex(&key_str)?)
.map_err(|e| WalletStorageError::ConversionError(e.to_string()))?;
Ok(Some(chain_metadata))
} else {
Ok(None)
}
}
fn insert_key_value_pair(&self, kvp: DbKeyValuePair) -> Result<Option<DbValue>, WalletStorageError> {
let start = Instant::now();
let conn = self.database_connection.get_pooled_connection()?;
let acquire_lock = start.elapsed();
let cipher = acquire_read_lock!(self.cipher);
let kvp_text;
match kvp {
DbKeyValuePair::MasterSeed(seed) => {
kvp_text = "MasterSeed";
self.set_master_seed(&seed, &conn)?;
},
DbKeyValuePair::TorId(node_id) => {
kvp_text = "TorId";
self.set_tor_id(node_id, &conn)?;
},
DbKeyValuePair::BaseNodeChainMetadata(metadata) => {
kvp_text = "BaseNodeChainMetadata";
self.set_chain_metadata(metadata, &conn)?;
},
DbKeyValuePair::ClientKeyValue(k, v) => {
// First see if we will overwrite a value so we can return the old value
let value_to_return = if let Some(found_value) = ClientKeyValueSql::get(&k, &conn)? {
let found_value = self.decrypt_value(found_value)?;
Some(found_value)
} else {
None
};
let client_key_value = ClientKeyValueSql::new(k, v, &cipher)?;
client_key_value.set(&conn)?;
if start.elapsed().as_millis() > 0 {
trace!(
target: LOG_TARGET,
"sqlite profile - insert_key_value_pair 'ClientKeyValue': lock {} + db_op {} = {} ms",
acquire_lock.as_millis(),
(start.elapsed() - acquire_lock).as_millis(),
start.elapsed().as_millis()
);
}
return Ok(value_to_return.map(|v| DbValue::ClientValue(v.value)));
},
DbKeyValuePair::CommsAddress(ca) => {
kvp_text = "CommsAddress";
WalletSettingSql::new(DbKey::CommsAddress, ca.to_string()).set(&conn)?;
},
DbKeyValuePair::CommsFeatures(cf) => {
kvp_text = "CommsFeatures";
WalletSettingSql::new(DbKey::CommsFeatures, cf.bits().to_string()).set(&conn)?;
},
DbKeyValuePair::CommsIdentitySignature(identity_sig) => {
kvp_text = "CommsIdentitySignature";
WalletSettingSql::new(DbKey::CommsIdentitySignature, identity_sig.to_bytes().to_hex()).set(&conn)?;
},
}
if start.elapsed().as_millis() > 0 {
trace!(
target: LOG_TARGET,
"sqlite profile - insert_key_value_pair '{}': lock {} + db_op {} = {} ms",
kvp_text,
acquire_lock.as_millis(),
(start.elapsed() - acquire_lock).as_millis(),
start.elapsed().as_millis()
);
}
Ok(None)
}
fn remove_key(&self, k: DbKey) -> Result<Option<DbValue>, WalletStorageError> {
let start = Instant::now();
let conn = self.database_connection.get_pooled_connection()?;
let acquire_lock = start.elapsed();
match k {
DbKey::MasterSeed => {
let _ = WalletSettingSql::clear(&DbKey::MasterSeed, &conn)?;
},
DbKey::ClientKey(ref k) => {
if ClientKeyValueSql::clear(k, &conn)? {
return Ok(Some(DbValue::ValueCleared));
}
},
DbKey::TorId => {
let _ = WalletSettingSql::clear(&DbKey::TorId, &conn)?;
},
DbKey::CommsFeatures |
DbKey::CommsAddress |
DbKey::BaseNodeChainMetadata |
DbKey::PassphraseHash |
DbKey::EncryptionSalt |
DbKey::WalletBirthday |
DbKey::CommsIdentitySignature => {
return Err(WalletStorageError::OperationNotSupported);
},
};
if start.elapsed().as_millis() > 0 {
trace!(
target: LOG_TARGET,
"sqlite profile - remove_key '{}': lock {} + db_op {} = {} &ms",
k.to_key_string(),
acquire_lock.as_millis(),
(start.elapsed() - acquire_lock).as_millis(),
start.elapsed().as_millis()
);
}
Ok(None)
}
pub fn cipher(&self) -> XChaCha20Poly1305 {
let cipher = acquire_read_lock!(self.cipher);
(*cipher).clone()
}
}
impl WalletBackend for WalletSqliteDatabase {
fn fetch(&self, key: &DbKey) -> Result<Option<DbValue>, WalletStorageError> {
let start = Instant::now();
let conn = self.database_connection.get_pooled_connection()?;
let acquire_lock = start.elapsed();
let result = match key {
DbKey::MasterSeed => self.get_master_seed(&conn)?.map(DbValue::MasterSeed),
DbKey::ClientKey(k) => match ClientKeyValueSql::get(k, &conn)? {
None => None,
Some(v) => {
let v = self.decrypt_value(v)?;
Some(DbValue::ClientValue(v.value))
},
},
DbKey::CommsAddress => self.get_comms_address(&conn)?.map(DbValue::CommsAddress),
DbKey::TorId => self.get_tor_id(&conn)?,
DbKey::CommsFeatures => self.get_comms_features(&conn)?.map(DbValue::CommsFeatures),
DbKey::BaseNodeChainMetadata => self.get_chain_metadata(&conn)?.map(DbValue::BaseNodeChainMetadata),
DbKey::PassphraseHash => WalletSettingSql::get(key, &conn)?.map(DbValue::PassphraseHash),
DbKey::EncryptionSalt => WalletSettingSql::get(key, &conn)?.map(DbValue::EncryptionSalt),
DbKey::WalletBirthday => WalletSettingSql::get(key, &conn)?.map(DbValue::WalletBirthday),
DbKey::CommsIdentitySignature => WalletSettingSql::get(key, &conn)?
.and_then(|s| from_hex(&s).ok())
.and_then(|bytes| IdentitySignature::from_bytes(&bytes).ok())
.map(Box::new)
.map(DbValue::CommsIdentitySignature),
};
if start.elapsed().as_millis() > 0 {
trace!(
target: LOG_TARGET,
"sqlite profile - fetch '{}': lock {} + db_op {} = {} ms",
key.to_key_string(),
acquire_lock.as_millis(),
(start.elapsed() - acquire_lock).as_millis(),
start.elapsed().as_millis()
);
}
Ok(result)
}
fn write(&self, op: WriteOperation) -> Result<Option<DbValue>, WalletStorageError> {
match op {
WriteOperation::Insert(kvp) => self.insert_key_value_pair(kvp),
WriteOperation::Remove(k) => self.remove_key(k),
}
}
fn get_scanned_blocks(&self) -> Result<Vec<ScannedBlock>, WalletStorageError> {
let conn = self.database_connection.get_pooled_connection()?;
let sql_blocks = ScannedBlockSql::index(&conn)?;
sql_blocks
.into_iter()
.map(ScannedBlock::try_from)
.collect::<Result<Vec<_>, _>>()
.map_err(WalletStorageError::ConversionError)
}
fn save_scanned_block(&self, scanned_block: ScannedBlock) -> Result<(), WalletStorageError> {
let conn = self.database_connection.get_pooled_connection()?;
ScannedBlockSql::from(scanned_block).commit(&conn)
}
fn clear_scanned_blocks(&self) -> Result<(), WalletStorageError> {
let conn = self.database_connection.get_pooled_connection()?;
ScannedBlockSql::clear_all(&conn)
}
fn clear_scanned_blocks_from_and_higher(&self, height: u64) -> Result<(), WalletStorageError> {
let conn = self.database_connection.get_pooled_connection()?;
ScannedBlockSql::clear_from_and_higher(height, &conn)
}
fn clear_scanned_blocks_before_height(
&self,
height: u64,
exclude_recovered: bool,
) -> Result<(), WalletStorageError> {
let conn = self.database_connection.get_pooled_connection()?;
ScannedBlockSql::clear_before_height(height, exclude_recovered, &conn)
}
}
/// Confirm if database is encrypted or not and if a cipher is provided confirm the cipher is correct.
/// Unencrypted the database should contain a MasterSecretKey and associated MasterPublicKey
/// Encrypted the data should contain a Master Public Key in the clear and an encrypted MasterSecretKey
/// To confirm if the provided Cipher is correct we decrypt the Master PrivateSecretKey and see if it produces the same
/// Master Public Key that is stored in the db
#[allow(clippy::too_many_lines)]
fn get_db_encryption(
database_connection: &WalletDbConnection,
passphrase: SafePassword,
) -> Result<XChaCha20Poly1305, WalletStorageError> {
let start = Instant::now();
let conn = database_connection.get_pooled_connection()?;
let acquire_lock = start.elapsed();
let db_passphrase_hash = WalletSettingSql::get(&DbKey::PassphraseHash, &conn)?;
let db_encryption_salt = WalletSettingSql::get(&DbKey::EncryptionSalt, &conn)?;
let secret_seed = WalletSettingSql::get(&DbKey::MasterSeed, &conn)?;
let cipher = get_cipher_for_db_encryption(passphrase, db_passphrase_hash, db_encryption_salt, &secret_seed, &conn)?;
if let Some(mut sk) = secret_seed {
// We need to make sure the secret key was encrypted. Try to decrypt it
let mut sk_bytes: Vec<u8> = from_hex(sk.as_str())?;
if sk_bytes.len() < size_of::<XNonce>() + size_of::<Tag>() {
// zeroize sk and sk_bytes, so no memory leak happens, as sk could have been decrypted
sk.zeroize();
sk_bytes.zeroize();
return Err(WalletStorageError::MissingNonce);
}
// We try to decrypt the secret seed data, using our computed cipher. Moreover,
// decrypted key contains sensitive data, we make sure we appropriately zeroize
// the corresponding data buffer, when leaving the current scope
let decrypted_key = Hidden::hide(
decrypt_bytes_integral_nonce(&cipher, b"wallet_setting_master_seed".to_vec(), &sk_bytes).map_err(|e| {
error!(target: LOG_TARGET, "Incorrect passphrase ({})", e);
// zeroize sk and sk_bytes, so no memory leak happens, as sk could have been decrypted
sk.zeroize();
sk_bytes.zeroize();
WalletStorageError::InvalidPassphrase
})?,
);
// from this point on, we are sure that sk and thus sk_bytes were encrypted, so we might safely not zeroize them
let _cipher_seed = CipherSeed::from_enciphered_bytes(decrypted_key.reveal(), None).map_err(|_| {
error!(
target: LOG_TARGET,
"Decrypted Master Secret Key cannot be parsed into a Cipher Seed"
);
WalletStorageError::InvalidEncryptionCipher
})?;
}
if start.elapsed().as_millis() > 0 {
trace!(
target: LOG_TARGET,
"sqlite profile - get_db_encryption: lock {} + db_op {} = {} ms",
acquire_lock.as_millis(),
(start.elapsed() - acquire_lock).as_millis(),
start.elapsed().as_millis()
);
}
Ok(cipher)
}
fn get_cipher_for_db_encryption(
passphrase: SafePassword,
passphrase_hash: Option<String>,
encryption_salt: Option<String>,
secret_seed: &Option<String>,
conn: &SqliteConnection,
) -> Result<XChaCha20Poly1305, WalletStorageError> {
let encryption_salt = match (passphrase_hash, encryption_salt) {
(None, None) => {
// Use the recommended OWASP parameters, which are not the default:
// https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#argon2id
// These are the parameters for the passphrase hash
let params_passphrase = argon2::Params::new(
46 * 1024, // m-cost: 46 MiB, converted to KiB
1, // t-cost
1, // p-cost
None, // output length: default is fine for this use
)
.map_err(|e| WalletStorageError::AeadError(e.to_string()))?;
// Hash the passphrase to a PHC string for later verification
let passphrase_salt = SaltString::generate(&mut OsRng);
let passphrase_hash = argon2::Argon2::default()
.hash_password_customized(
passphrase.reveal(),
Some(argon2::Algorithm::Argon2id.ident()),
Some(argon2::Version::V0x13 as Decimal), // the API requires the numerical version representation
params_passphrase,
&passphrase_salt,
)
.map_err(|e| WalletStorageError::AeadError(e.to_string()))?
.to_string();
// Hash the passphrase to produce a ChaCha20-Poly1305 key
let encryption_salt = SaltString::generate(&mut OsRng);
// insert passphrase hash and encryption salt on the wallet db
WalletSettingSql::new(DbKey::PassphraseHash, passphrase_hash).set(conn)?;
WalletSettingSql::new(DbKey::EncryptionSalt, encryption_salt.to_string()).set(conn)?;
encryption_salt.to_string()
},
(Some(ph), Some(es)) => {
let argon2 = Argon2::default();
let stored_hash = PasswordHash::new(&ph).map_err(|e| WalletStorageError::AeadError(e.to_string()))?;
// Check the passphrase PHC string against the provided passphrase
if let Err(e) = argon2.verify_password(passphrase.reveal(), &stored_hash) {
error!(target: LOG_TARGET, "Incorrect passphrase ({})", e);
return Err(WalletStorageError::InvalidPassphrase);
}
if secret_seed.is_none() {
error!(
target: LOG_TARGET,
"Cipher is provided but there is no Master Secret Key in DB to decrypt"
);
return Err(WalletStorageError::InvalidEncryptionCipher);
}
es
},
_ => {
error!(
target: LOG_TARGET,
"Only passphrase hash or encryption hash were provided, need both values for successful encryption"
);
return Err(WalletStorageError::UnexpectedResult(
"Encryption should be possible only by providing both passphrase hash and encrypted salt".into(),
));
},
};
// Use the recommended OWASP parameters, which are not the default:
// https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#argon2id
let params_encryption = argon2::Params::new(
46 * 1024, // m-cost: 46 MiB, converted to KiB
1, // t-cost
1, // p-cost
Some(size_of::<Key>()), // output length: ChaCha20-Poly1305 key size
)
.map_err(|e| WalletStorageError::AeadError(e.to_string()))?;
// Hash the passphrase to produce a ChaCha20-Poly1305 key
let mut derived_encryption_key = Hidden::hide(SafeArray::<u8, { size_of::<Key>() }>::default());
argon2::Argon2::new(argon2::Algorithm::Argon2id, argon2::Version::V0x13, params_encryption)
.hash_password_into(
passphrase.reveal(),
encryption_salt.as_bytes(),
derived_encryption_key.reveal_mut(),
)
.map_err(|e| WalletStorageError::AeadError(e.to_string()))?;
Ok(XChaCha20Poly1305::new(Key::from_slice(derived_encryption_key.reveal())))
}
/// A Sql version of the wallet setting key-value table
#[derive(Clone, Debug, Queryable, Insertable, PartialEq)]
#[table_name = "wallet_settings"]
pub(crate) struct WalletSettingSql {
key: String,
value: String,
}
impl WalletSettingSql {
pub fn new(key: DbKey, value: String) -> Self {
Self {
key: key.to_key_string(),
value,
}
}
pub fn set(&self, conn: &SqliteConnection) -> Result<(), WalletStorageError> {
diesel::replace_into(wallet_settings::table)
.values(self)
.execute(conn)?;
Ok(())
}
pub fn get(key: &DbKey, conn: &SqliteConnection) -> Result<Option<String>, WalletStorageError> {
wallet_settings::table
.filter(wallet_settings::key.eq(key.to_key_string()))
.first::<WalletSettingSql>(conn)
.map(|v: WalletSettingSql| Some(v.value))
.or_else(|err| match err {
diesel::result::Error::NotFound => Ok(None),
err => Err(err.into()),
})
}
pub fn clear(key: &DbKey, conn: &SqliteConnection) -> Result<bool, WalletStorageError> {
let num_deleted = diesel::delete(wallet_settings::table.filter(wallet_settings::key.eq(key.to_key_string())))
.execute(conn)?;
Ok(num_deleted > 0)
}
}
/// A Sql version of the wallet setting key-value table
#[derive(Clone, Debug, Queryable, Insertable, PartialEq)]
#[table_name = "client_key_values"]
struct ClientKeyValueSql {
key: String,
value: String,
}
impl ClientKeyValueSql {
pub fn new(key: String, value: String, cipher: &XChaCha20Poly1305) -> Result<Self, WalletStorageError> {
let client_kv = Self { key, value };
client_kv.encrypt(cipher).map_err(WalletStorageError::AeadError)
}
#[allow(dead_code)]
pub fn index(conn: &SqliteConnection) -> Result<Vec<Self>, WalletStorageError> {
Ok(client_key_values::table.load::<ClientKeyValueSql>(conn)?)
}
pub fn set(&self, conn: &SqliteConnection) -> Result<(), WalletStorageError> {
diesel::replace_into(client_key_values::table)
.values(self)
.execute(conn)?;
Ok(())
}
pub fn get(key: &str, conn: &SqliteConnection) -> Result<Option<Self>, WalletStorageError> {
client_key_values::table
.filter(client_key_values::key.eq(key))
.first::<ClientKeyValueSql>(conn)
.map(Some)
.or_else(|err| match err {
diesel::result::Error::NotFound => Ok(None),
err => Err(err.into()),
})
}
pub fn clear(key: &str, conn: &SqliteConnection) -> Result<bool, WalletStorageError> {
let num_deleted =
diesel::delete(client_key_values::table.filter(client_key_values::key.eq(key))).execute(conn)?;
Ok(num_deleted > 0)
}
}
impl Encryptable<XChaCha20Poly1305> for ClientKeyValueSql {
fn domain(&self, field_name: &'static str) -> Vec<u8> {
[Self::CLIENT_KEY_VALUE, self.key.as_bytes(), field_name.as_bytes()]
.concat()
.to_vec()
}
#[allow(unused_assignments)]
fn encrypt(mut self, cipher: &XChaCha20Poly1305) -> Result<Self, String> {
self.value = encrypt_bytes_integral_nonce(
cipher,
self.domain("value"),
Hidden::hide(self.value.as_bytes().to_vec()),
)?
.to_hex();
Ok(self)
}
#[allow(unused_assignments)]
fn decrypt(mut self, cipher: &XChaCha20Poly1305) -> Result<Self, String> {
let mut decrypted_value = decrypt_bytes_integral_nonce(
cipher,
self.domain("value"),
&from_hex(self.value.as_str()).map_err(|e| e.to_string())?,
)?;
self.value = from_utf8(decrypted_value.as_slice())
.map_err(|e| e.to_string())?
.to_string();
// we zeroize the decrypted value
decrypted_value.zeroize();
Ok(self)
}
}
#[cfg(test)]
mod test {
use tari_key_manager::cipher_seed::CipherSeed;
use tari_test_utils::random::string;
use tari_utilities::{
hex::{from_hex, Hex},
ByteArray,
Hidden,
SafePassword,
};
use tempfile::tempdir;
use crate::{
error::WalletStorageError,
storage::{
database::{DbKey, DbValue, WalletBackend},
sqlite_db::wallet::{ClientKeyValueSql, WalletSettingSql, WalletSqliteDatabase},
sqlite_utilities::run_migration_and_create_sqlite_connection,
},
util::encryption::{decrypt_bytes_integral_nonce, encrypt_bytes_integral_nonce, Encryptable},
};
#[test]
fn test_unencrypted_secret_public_key_setting() {
let db_name = format!("{}.sqlite3", string(8).as_str());
let tempdir = tempdir().unwrap();
let db_folder = tempdir.path().to_str().unwrap().to_string();
let connection = run_migration_and_create_sqlite_connection(format!("{}{}", db_folder, db_name), 16).unwrap();
let secret_seed1 = CipherSeed::new();
{
let conn = connection.get_pooled_connection().unwrap();
WalletSettingSql::new(DbKey::MasterSeed, secret_seed1.encipher(None).unwrap().to_hex())
.set(&conn)
.unwrap();
}
let passphrase = SafePassword::from("an example very very secret key.".to_string());
match WalletSqliteDatabase::new(connection, passphrase) {
Err(WalletStorageError::MissingNonce) => (),
Ok(_) => panic!("we should not be able to have a non encrypted master seed in the db"),
_ => panic!("unrecognized error"),
};
}
#[test]
pub fn test_encrypted_seed_validation_during_startup() {
let db_name = format!("{}.sqlite3", string(8).as_str());
let db_tempdir = tempdir().unwrap();
let db_folder = db_tempdir.path().to_str().unwrap().to_string();
let connection = run_migration_and_create_sqlite_connection(format!("{}{}", db_folder, db_name), 16).unwrap();
let passphrase = SafePassword::from("an example very very secret key.".to_string());
let wallet = WalletSqliteDatabase::new(connection.clone(), passphrase.clone()).unwrap();
let seed = CipherSeed::new();
{
let conn = connection.get_pooled_connection().unwrap();
let encrypted_seed_bytes = seed.encipher(None).unwrap();
let encrypted_seed = encrypt_bytes_integral_nonce(
&wallet.cipher(),
b"wallet_setting_master_seed".to_vec(),
Hidden::hide(encrypted_seed_bytes),
)
.unwrap();
WalletSettingSql::new(DbKey::MasterSeed, encrypted_seed.to_hex())
.set(&conn)
.unwrap();
}
assert!(WalletSqliteDatabase::new(connection, passphrase).is_ok());
}
#[test]
fn test_encryption_is_forced() {
let db_name = format!("{}.sqlite3", string(8).as_str());
let db_tempdir = tempdir().unwrap();
let db_folder = db_tempdir.path().to_str().unwrap().to_string();
let db_path = format!("{}/{}", db_folder, db_name);
let connection = run_migration_and_create_sqlite_connection(db_path, 16).unwrap();
let seed = CipherSeed::new();
let passphrase = "a very very secret key example.".to_string().into();
let db = WalletSqliteDatabase::new(connection.clone(), passphrase).unwrap();
let cipher = db.cipher();
let mut key_values = vec![
ClientKeyValueSql::new("key1".to_string(), "value1".to_string(), &cipher).unwrap(),
ClientKeyValueSql::new("key2".to_string(), "value2".to_string(), &cipher).unwrap(),
ClientKeyValueSql::new("key3".to_string(), "value3".to_string(), &cipher).unwrap(),
];
{
let conn = connection.get_pooled_connection().unwrap();
db.set_master_seed(&seed, &conn).unwrap();
for kv in &mut key_values {
kv.set(&conn).unwrap();
}
}
let read_seed1 = match db.fetch(&DbKey::MasterSeed).unwrap().unwrap() {
DbValue::MasterSeed(sk) => sk,
_ => {
panic!("Should be able to read Key");
},
};
assert_eq!(seed, read_seed1);
let read_seed2 = match db.fetch(&DbKey::MasterSeed).unwrap().unwrap() {
DbValue::MasterSeed(sk) => sk,
_ => {
panic!("Should be able to read Key");
},
};
for kv in &mut key_values {
*kv = kv.clone().decrypt(&db.cipher()).unwrap();
match db.fetch(&DbKey::ClientKey(kv.key.clone())).unwrap().unwrap() {
DbValue::ClientValue(v) => {
assert_eq!(kv.value, v);
},
_ => {
panic!("Should be able to read Key/Value");
},
}
}
assert_eq!(seed, read_seed2);
{
let conn = connection.get_pooled_connection().unwrap();
let secret_key_str = WalletSettingSql::get(&DbKey::MasterSeed, &conn).unwrap().unwrap();
assert!(secret_key_str.len() > 64);
db.set_master_seed(&seed, &conn).unwrap();
let secret_key_str = WalletSettingSql::get(&DbKey::MasterSeed, &conn).unwrap().unwrap();
assert!(secret_key_str.len() > 64);
}
let read_seed3 = match db.fetch(&DbKey::MasterSeed).unwrap().unwrap() {
DbValue::MasterSeed(sk) => sk,
_ => {
panic!("Should be able to read Key");
},
};
assert_eq!(seed, read_seed3);
for kv in &key_values {
match db.fetch(&DbKey::ClientKey(kv.key.clone())).unwrap().unwrap() {
DbValue::ClientValue(v) => {
assert_eq!(kv.value, v);
},
_ => {
panic!("Should be able to read Key/Value2");
},
}
}
}
#[test]
fn test_client_key_value_store() {
let db_name = format!("{}.sqlite3", string(8).as_str());
let db_tempdir = tempdir().unwrap();
let db_folder = db_tempdir.path().to_str().unwrap().to_string();
let connection = run_migration_and_create_sqlite_connection(format!("{}{}", db_folder, db_name), 16).unwrap();
let conn = connection.get_pooled_connection().unwrap();
let key1 = "key1".to_string();
let value1 = "value1".to_string();
let key2 = "key2".to_string();
let value2 = "value2".to_string();
let passphrase = "a very very secret key example.".to_string().into();
let db = WalletSqliteDatabase::new(connection, passphrase).unwrap();
let cipher = db.cipher();
ClientKeyValueSql::new(key1.clone(), value1.clone(), &cipher)
.unwrap()
.set(&conn)
.unwrap();
assert!(ClientKeyValueSql::get(&key2, &conn).unwrap().is_none());
if let Some(ckv) = ClientKeyValueSql::get(&key1, &conn).unwrap() {
let ckv = ckv.decrypt(&cipher).unwrap();
assert_eq!(ckv.value, value1);
} else {
panic!("Should find value");
}
assert!(!ClientKeyValueSql::clear(&key2, &conn).unwrap());
ClientKeyValueSql::new(key2.clone(), value2.clone(), &cipher)
.unwrap()
.set(&conn)
.unwrap();
let values = ClientKeyValueSql::index(&conn).unwrap();
assert_eq!(values.len(), 2);
assert_eq!(values[0].clone().decrypt(&cipher).unwrap().value, value1);
assert_eq!(values[1].clone().decrypt(&cipher).unwrap().value, value2);
assert!(ClientKeyValueSql::clear(&key1, &conn).unwrap());
assert!(ClientKeyValueSql::get(&key1, &conn).unwrap().is_none());
if let Some(ckv) = ClientKeyValueSql::get(&key2, &conn).unwrap() {
let ckv = ckv.decrypt(&cipher).unwrap();
assert_eq!(ckv.value, value2);
} else {
panic!("Should find value2");
}
}
#[test]
fn test_set_master_seed() {
let db_name = format!("{}.sqlite3", string(8).as_str());
let db_tempdir = tempdir().unwrap();
let db_folder = db_tempdir.path().to_str().unwrap().to_string();
let connection = run_migration_and_create_sqlite_connection(format!("{}{}", db_folder, db_name), 16).unwrap();
let passphrase = SafePassword::from("an example very very secret key.".to_string());
let wallet = WalletSqliteDatabase::new(connection.clone(), passphrase).unwrap();
let seed = CipherSeed::new();
let conn = connection.get_pooled_connection().unwrap();
wallet.set_master_seed(&seed, &conn).unwrap();
let seed_bytes = seed.encipher(None).unwrap();
let db_seed = WalletSettingSql::get(&DbKey::MasterSeed, &conn).unwrap().unwrap();
assert_eq!(db_seed.len(), 146);
let decrypted_db_seed = decrypt_bytes_integral_nonce(
&wallet.cipher(),
b"wallet_setting_master_seed".to_vec(),
&from_hex(db_seed.as_str()).unwrap(),
)
.unwrap();
assert_eq!(decrypted_db_seed, seed_bytes);
}
}