@@ -1097,6 +1097,33 @@ pub fn handle_setup_reencrypt(
10971097 luks2_path : & Path ,
10981098 encryption_info : & EncryptionInfo ,
10991099) -> StratisResult < ( u32 , SizedKeyMemory , u32 ) > {
1100+ fn set_up_reencryption_token (
1101+ device : & mut CryptDevice ,
1102+ new_keyslot : u32 ,
1103+ ts : u32 ,
1104+ mut token_contents : Value ,
1105+ ) -> StratisResult < ( ) > {
1106+ if let Some ( obj) = token_contents. as_object_mut ( ) {
1107+ let tokens = match obj. remove ( TOKEN_KEYSLOTS_KEY ) {
1108+ Some ( Value :: Array ( mut v) ) => {
1109+ v. push ( Value :: String ( new_keyslot. to_string ( ) ) ) ;
1110+ Value :: Array ( v)
1111+ }
1112+ Some ( _) | None => {
1113+ return Err ( StratisError :: Msg ( format ! (
1114+ "Could not find appropriate formatted value for {TOKEN_KEYSLOTS_KEY}"
1115+ ) ) ) ;
1116+ }
1117+ } ;
1118+ obj. insert ( TOKEN_KEYSLOTS_KEY . to_string ( ) , tokens) ;
1119+ }
1120+ device
1121+ . token_handle ( )
1122+ . json_set ( TokenInput :: ReplaceToken ( ts, & token_contents) ) ?;
1123+
1124+ Ok ( ( ) )
1125+ }
1126+
11001127 let mut device = acquire_crypt_device ( luks2_path) ?;
11011128
11021129 let mut keys = get_all_passphrases ( & mut device, encryption_info) ?;
@@ -1109,7 +1136,7 @@ pub fn handle_setup_reencrypt(
11091136 let ( single_ts, single_key) = keys
11101137 . pop ( )
11111138 . ok_or_else ( || StratisError :: Msg ( "No unlock methods found" . to_string ( ) ) ) ?;
1112- let mut single_token_contents = device. token_handle ( ) . json_get ( single_ts) ?;
1139+ let single_token_contents = device. token_handle ( ) . json_get ( single_ts) ?;
11131140 let single_keyslot = get_keyslot_number ( & mut device, single_ts) ?. ok_or_else ( || {
11141141 StratisError :: Msg ( format ! (
11151142 "Could not find keyslot associated with token slot {single_ts}"
@@ -1122,16 +1149,13 @@ pub fn handle_setup_reencrypt(
11221149 single_key. as_ref ( ) ,
11231150 CryptVolumeKey :: NO_SEGMENT ,
11241151 ) ?;
1125- if let Some ( obj) = single_token_contents. as_object_mut ( ) {
1126- obj. remove ( TOKEN_KEYSLOTS_KEY ) ;
1127- obj. insert (
1128- TOKEN_KEYSLOTS_KEY . to_string ( ) ,
1129- Value :: Array ( vec ! [ Value :: String ( single_new_keyslot. to_string( ) ) ] ) ,
1130- ) ;
1131- }
1132- device
1133- . token_handle ( )
1134- . json_set ( TokenInput :: ReplaceToken ( single_ts, & single_token_contents) ) ?;
1152+
1153+ set_up_reencryption_token (
1154+ & mut device,
1155+ single_new_keyslot,
1156+ single_ts,
1157+ single_token_contents,
1158+ ) ?;
11351159
11361160 let mut new_vk = SafeMemHandle :: alloc ( STRATIS_MEK_SIZE ) ?;
11371161 device. volume_key_handle ( ) . get (
@@ -1141,24 +1165,15 @@ pub fn handle_setup_reencrypt(
11411165 ) ?;
11421166
11431167 for ( ts, key) in other_keys {
1144- let mut token_contents = device. token_handle ( ) . json_get ( ts) ?;
1168+ let token_contents = device. token_handle ( ) . json_get ( ts) ?;
11451169
11461170 let new_keyslot = device. keyslot_handle ( ) . add_by_key (
11471171 None ,
11481172 Some ( Either :: Left ( new_vk. as_ref ( ) ) ) ,
11491173 key. as_ref ( ) ,
11501174 CryptVolumeKey :: NO_SEGMENT | CryptVolumeKey :: DIGEST_REUSE ,
11511175 ) ?;
1152- if let Some ( obj) = token_contents. as_object_mut ( ) {
1153- obj. remove ( TOKEN_KEYSLOTS_KEY ) ;
1154- obj. insert (
1155- TOKEN_KEYSLOTS_KEY . to_string ( ) ,
1156- Value :: Array ( vec ! [ Value :: String ( new_keyslot. to_string( ) ) ] ) ,
1157- ) ;
1158- }
1159- device
1160- . token_handle ( )
1161- . json_set ( TokenInput :: ReplaceToken ( ts, & token_contents) ) ?;
1176+ set_up_reencryption_token ( & mut device, new_keyslot, ts, token_contents) ?;
11621177 }
11631178
11641179 Ok ( ( single_keyslot, single_key, single_new_keyslot) )
@@ -1175,41 +1190,44 @@ pub fn handle_do_reencrypt(
11751190 single_key : SizedKeyMemory ,
11761191 single_new_keyslot : u32 ,
11771192) -> StratisResult < ( ) > {
1178- let mut device = acquire_crypt_device ( luks2_path) ?;
1179-
1180- let cipher = device. status_handle ( ) . get_cipher ( ) ?;
1181- let cipher_mode = device. status_handle ( ) . get_cipher_mode ( ) ?;
1182- let sector_size = convert_int ! ( get_sector_size( Some ( & mut device) ) , i32 , u32 ) ?;
1183- device. reencrypt_handle ( ) . reencrypt_init_by_passphrase (
1184- Some ( device_name) ,
1185- single_key. as_ref ( ) ,
1186- Some ( single_keyslot) ,
1187- Some ( single_new_keyslot) ,
1188- Some ( ( & cipher, & cipher_mode) ) ,
1189- CryptParamsReencrypt {
1190- mode : CryptReencryptModeInfo :: Reencrypt ,
1191- direction : CryptReencryptDirectionInfo :: Forward ,
1192- resilience : "checksum" . to_string ( ) ,
1193- hash : "sha256" . to_string ( ) ,
1194- data_shift : 0 ,
1195- max_hotzone_size : 0 ,
1196- device_size : 0 ,
1197- luks2 : Some ( CryptParamsLuks2 {
1198- data_alignment : 0 ,
1199- data_device : None ,
1200- integrity : None ,
1201- integrity_params : None ,
1202- pbkdf : None ,
1203- label : None ,
1204- sector_size,
1205- subsystem : None ,
1206- } ) ,
1207- flags : CryptReencrypt :: empty ( ) ,
1208- } ,
1209- ) ?;
1193+ {
1194+ let mut device = acquire_crypt_device ( luks2_path) ?;
1195+
1196+ let cipher = device. status_handle ( ) . get_cipher ( ) ?;
1197+ let cipher_mode = device. status_handle ( ) . get_cipher_mode ( ) ?;
1198+ let sector_size = convert_int ! ( get_sector_size( Some ( & mut device) ) , i32 , u32 ) ?;
1199+ device. reencrypt_handle ( ) . reencrypt_init_by_passphrase (
1200+ Some ( device_name) ,
1201+ single_key. as_ref ( ) ,
1202+ Some ( single_keyslot) ,
1203+ Some ( single_new_keyslot) ,
1204+ Some ( ( & cipher, & cipher_mode) ) ,
1205+ CryptParamsReencrypt {
1206+ mode : CryptReencryptModeInfo :: Reencrypt ,
1207+ direction : CryptReencryptDirectionInfo :: Forward ,
1208+ resilience : "checksum" . to_string ( ) ,
1209+ hash : "sha256" . to_string ( ) ,
1210+ data_shift : 0 ,
1211+ max_hotzone_size : 0 ,
1212+ device_size : 0 ,
1213+ luks2 : Some ( CryptParamsLuks2 {
1214+ data_alignment : 0 ,
1215+ data_device : None ,
1216+ integrity : None ,
1217+ integrity_params : None ,
1218+ pbkdf : None ,
1219+ label : None ,
1220+ sector_size,
1221+ subsystem : None ,
1222+ } ) ,
1223+ flags : CryptReencrypt :: empty ( ) ,
1224+ } ,
1225+ ) ?;
1226+ }
12101227
12111228 info ! ( "Starting reencryption operation on pool with UUID {pool_uuid}; may take a while" ) ;
1212- device. reencrypt_handle ( ) . reencrypt2 :: < ( ) > ( None , None ) ?;
1229+ // The corresponding libcryptsetup call is device.reencrypt_handle().reencrypt2::<()>(None, None)?;
1230+ cmd:: run_reencrypt ( luks2_path) ?;
12131231
12141232 Ok ( ( ) )
12151233}
0 commit comments