@@ -5,7 +5,7 @@ use std::{
5
5
} ;
6
6
7
7
use drop_analytics:: DeveloperExceptionEventData ;
8
- use drop_auth:: { PublicKey , SecretKey } ;
8
+ use drop_auth:: { PublicKey , SecretKey , PUBLIC_KEY_LENGTH , SECRET_KEY_LENGTH } ;
9
9
use drop_config:: { Config , DropConfig , MooseConfig } ;
10
10
use drop_storage:: types:: Transfer as TransferInfo ;
11
11
use drop_transfer:: { auth, utils:: Hidden , Event , FileToSend , OutgoingTransfer , Service , Transfer } ;
@@ -15,7 +15,7 @@ use tokio::{
15
15
task:: JoinHandle ,
16
16
} ;
17
17
18
- use crate :: { event, TransferDescriptor } ;
18
+ use crate :: { event, KeyStore , TransferDescriptor } ;
19
19
20
20
pub type Result < T = ( ) > = std:: result:: Result < T , crate :: LibdropError > ;
21
21
@@ -52,15 +52,11 @@ impl EventDispatcher {
52
52
impl NordDropFFI {
53
53
pub ( super ) fn new (
54
54
event_cb : impl Fn ( crate :: Event ) + Send + Sync + ' static ,
55
- pubkey_cb : impl Fn ( IpAddr ) -> Option < PublicKey > + Send + ' static ,
56
- privkey : SecretKey ,
55
+ key_store : Arc < dyn KeyStore > ,
57
56
logger : Logger ,
58
57
) -> Result < Self > {
59
58
trace ! ( logger, "norddrop_new()" ) ;
60
59
61
- // It's a debug print. Not visible in the production build
62
- debug ! ( logger, "Private key: {:02X?}" , privkey. to_bytes( ) ) ;
63
-
64
60
Ok ( NordDropFFI {
65
61
instance : Arc :: default ( ) ,
66
62
logger : logger. clone ( ) ,
@@ -69,7 +65,7 @@ impl NordDropFFI {
69
65
cb : Arc :: new ( event_cb) as _ ,
70
66
} ,
71
67
config : DropConfig :: default ( ) ,
72
- keys : Arc :: new ( crate_key_context ( logger, privkey , pubkey_cb ) ) ,
68
+ keys : Arc :: new ( create_key_context ( logger, key_store ) ) ,
73
69
#[ cfg( unix) ]
74
70
fdresolv : None ,
75
71
} )
@@ -522,22 +518,33 @@ impl NordDropFFI {
522
518
}
523
519
}
524
520
525
- fn crate_key_context (
526
- logger : slog:: Logger ,
527
- privkey : SecretKey ,
528
- pubkey_cb : impl Fn ( IpAddr ) -> Option < PublicKey > + Send + ' static ,
529
- ) -> auth:: Context {
530
- let pubkey_cb = std:: sync:: Mutex :: new ( pubkey_cb) ;
531
- let public = move |ip : IpAddr | {
521
+ fn create_key_context ( logger : slog:: Logger , key_store : Arc < dyn KeyStore > ) -> auth:: Context {
522
+ let privkey = {
523
+ let key_store = key_store. clone ( ) ;
524
+ let logger = logger. clone ( ) ;
525
+ let privkey_cb = std:: sync:: Mutex :: new ( key_store) ;
526
+ move || {
527
+ let guard = privkey_cb. lock ( ) . expect ( "Failed to lock privkey callback" ) ;
528
+ let privkey: [ u8 ; SECRET_KEY_LENGTH ] = guard. privkey ( ) . try_into ( ) . ok ( ) ?;
529
+ drop ( guard) ;
530
+
531
+ debug ! ( logger, "Retrieved private key: {:?}" , privkey) ;
532
+ Some ( SecretKey :: from ( privkey) )
533
+ }
534
+ } ;
535
+
536
+ let pubkey_cb = std:: sync:: Mutex :: new ( key_store) ;
537
+ let pubkey = move |ip : IpAddr | {
532
538
let guard = pubkey_cb. lock ( ) . expect ( "Failed to lock pubkey callback" ) ;
533
- let key = guard ( ip) ?;
539
+ let pubkey = guard. on_pubkey ( ip. to_string ( ) ) ?;
534
540
drop ( guard) ;
535
541
536
- debug ! ( logger, "Public key for {ip:?}: {key:?}" ) ;
537
- Some ( key)
542
+ let pubkey: [ u8 ; PUBLIC_KEY_LENGTH ] = pubkey. try_into ( ) . ok ( ) ?;
543
+ debug ! ( logger, "Retrieved public key for: {} key: {:?}" , ip, pubkey) ;
544
+ Some ( PublicKey :: from ( pubkey) )
538
545
} ;
539
546
540
- auth:: Context :: new ( privkey, public )
547
+ auth:: Context :: new ( privkey, pubkey )
541
548
}
542
549
543
550
fn open_database (
0 commit comments