@@ -26,19 +26,23 @@ fn vec_to_array<T, const N: usize>(v: Vec<T>) -> [T; N] {
26
26
#[ derive( Clone , Debug , PartialEq , Eq , Hash ) ]
27
27
pub struct KeyStore {
28
28
store : PathBuf ,
29
+ pub key : [ u8 ; 16 ] ,
29
30
}
30
31
31
32
impl KeyStore {
32
33
pub fn create ( store : PathBuf , key : & str ) -> Result < Self > {
33
- _ = KeyStore :: str_to_key ( key) ?;
34
- std:: fs:: write ( & store, key) ?;
35
- Ok ( Self { store } )
34
+ let key = KeyStore :: str_to_key ( key) ?;
35
+ std:: fs:: write ( & store, key)
36
+ . expect ( "Unable to write to keystore" ) ;
37
+ Ok ( Self { store, key } )
36
38
}
37
39
38
40
pub fn _new ( store : PathBuf ) -> Result < Self > {
39
- let key = KeyStore :: key_to_str ( & KeyStore :: _random_key ( ) ) ;
40
- std:: fs:: write ( & store, key) ?;
41
- Ok ( Self { store } )
41
+ let key = KeyStore :: _random_key ( ) ;
42
+ let key_str = KeyStore :: key_to_str ( & key) ;
43
+ std:: fs:: write ( & store, key_str)
44
+ . expect ( "Unable to write to keystore" ) ;
45
+ Ok ( Self { store, key } )
42
46
}
43
47
44
48
pub fn _random_key ( ) -> [ u8 ; 16 ] {
@@ -68,16 +72,15 @@ impl KeyStore {
68
72
}
69
73
70
74
pub fn load ( & self ) -> Result < [ u8 ; 16 ] > {
71
- if !self . store . exists ( ) {
72
- anyhow:: bail!( "Store does not exist" ) ;
73
- }
74
- let s = std:: fs:: read_to_string ( self . store . as_path ( ) ) ?;
75
-
75
+ let s = std:: fs:: read_to_string ( & self . store )
76
+ . context ( format ! ( "keystore {} not found" , self . store. display( ) ) ) ?;
76
77
KeyStore :: str_to_key ( & s)
77
78
}
78
79
79
- pub fn store ( & self , key : [ u8 ; 16 ] ) -> Result < ( ) > {
80
- std:: fs:: write ( & self . store , KeyStore :: key_to_str ( & key) ) ?;
80
+ pub fn store ( & mut self , key : [ u8 ; 16 ] ) -> Result < ( ) > {
81
+ std:: fs:: write ( & self . store , KeyStore :: key_to_str ( & key) )
82
+ . expect ( "Unable to write to keystore" ) ;
83
+ self . key = key;
81
84
Ok ( ( ) )
82
85
}
83
86
}
@@ -103,8 +106,7 @@ impl CpConfig {
103
106
pub fn new ( config : & Ini , runtime_dir : & Path ) -> Result < Self > {
104
107
let num_pd = config. getuint ( "default" , "num_pd" ) . unwrap ( ) . unwrap ( ) as usize ;
105
108
let name = config. get ( "default" , "name" ) . unwrap ( ) ;
106
- let mut runtime_dir = runtime_dir. to_owned ( ) ;
107
- runtime_dir. push ( & name) ;
109
+ let runtime_dir = runtime_dir. to_owned ( ) ;
108
110
let mut pd_data = Vec :: new ( ) ;
109
111
for pd in 0 ..num_pd {
110
112
let section = format ! ( "pd-{pd}" ) ;
@@ -113,7 +115,7 @@ impl CpConfig {
113
115
name : config. get ( & section, "name" ) . unwrap ( ) ,
114
116
channel : config. get ( & section, "channel" ) . unwrap ( ) ,
115
117
address : config. getuint ( & section, "address" ) . unwrap ( ) . unwrap ( ) as i32 ,
116
- key_store : KeyStore :: create ( runtime_dir. join ( " key.store") , key) ?,
118
+ key_store : KeyStore :: create ( runtime_dir. join ( format ! ( "pd-{}- key.store", pd ) ) , key) ?,
117
119
flags : OsdpFlag :: empty ( ) ,
118
120
} ) ;
119
121
}
@@ -149,7 +151,7 @@ impl CpConfig {
149
151
. address ( d. address ) ?
150
152
. baud_rate ( 115200 ) ?
151
153
. flag ( d. flags )
152
- . secure_channel_key ( d. key_store . load ( ) ? ) ;
154
+ . secure_channel_key ( d. key_store . key ) ;
153
155
cp = cp. add_channel ( Box :: new ( channel) , vec ! [ pd_info] ) ;
154
156
}
155
157
Ok ( cp)
@@ -217,8 +219,7 @@ impl PdConfig {
217
219
} ;
218
220
let key = & config. get ( "default" , "scbk" ) . unwrap ( ) ;
219
221
let name = config. get ( "default" , "name" ) . unwrap ( ) ;
220
- let mut runtime_dir = runtime_dir. to_owned ( ) ;
221
- runtime_dir. push ( & name) ;
222
+ let runtime_dir = runtime_dir. to_owned ( ) ;
222
223
let key_store = KeyStore :: create ( runtime_dir. join ( "key.store" ) , key) ?;
223
224
Ok ( Self {
224
225
name,
@@ -247,7 +248,7 @@ impl PdConfig {
247
248
. flag ( self . flags )
248
249
. capabilities ( & self . pd_cap )
249
250
. id ( & self . pd_id )
250
- . secure_channel_key ( self . key_store . load ( ) ? ) ;
251
+ . secure_channel_key ( self . key_store . key ) ;
251
252
Ok ( ( Box :: new ( channel) , pd_info) )
252
253
}
253
254
}
@@ -287,9 +288,15 @@ impl DeviceConfig {
287
288
bail ! ( "Config {} does not exist!" , cfg. display( ) )
288
289
}
289
290
config. load ( cfg) . unwrap ( ) ;
291
+
292
+ let mut runtime_dir = runtime_dir. to_owned ( ) ;
293
+ let name = config. get ( "default" , "name" ) . unwrap ( ) ;
294
+ runtime_dir. push ( & name) ;
295
+ _ = std:: fs:: create_dir_all ( & runtime_dir) ;
296
+
290
297
let config = match config. get ( "default" , "num_pd" ) {
291
- Some ( _) => DeviceConfig :: CpConfig ( CpConfig :: new ( & config, runtime_dir) ?) ,
292
- None => DeviceConfig :: PdConfig ( PdConfig :: new ( & config, runtime_dir) ?) ,
298
+ Some ( _) => DeviceConfig :: CpConfig ( CpConfig :: new ( & config, & runtime_dir) ?) ,
299
+ None => DeviceConfig :: PdConfig ( PdConfig :: new ( & config, & runtime_dir) ?) ,
293
300
} ;
294
301
Ok ( config)
295
302
}
0 commit comments