@@ -111,7 +111,7 @@ impl<'a> CryptLuks2ReencryptHandle<'a> {
111
111
name : Option < & str > ,
112
112
passphrase : & [ u8 ] ,
113
113
keyslot_old : Option < c_uint > ,
114
- keyslot_new : c_uint ,
114
+ keyslot_new : Option < c_uint > ,
115
115
cipher_and_mode : Option < ( & str , & str ) > ,
116
116
params : CryptParamsReencrypt ,
117
117
) -> Result < c_int , LibcryptErr > {
@@ -141,7 +141,7 @@ impl<'a> CryptLuks2ReencryptHandle<'a> {
141
141
to_byte_ptr!( passphrase) ,
142
142
passphrase. len( ) ,
143
143
keyslot_old. map( |k| k as c_int) . unwrap_or( CRYPT_ANY_SLOT ) ,
144
- keyslot_new as c_int,
144
+ keyslot_new. map ( |k| k as c_int) . unwrap_or ( CRYPT_ANY_SLOT ) ,
145
145
// NOTE: Must keep as_ref to avoid use after free error.
146
146
cipher_cstring
147
147
. as_ref( )
@@ -163,20 +163,27 @@ impl<'a> CryptLuks2ReencryptHandle<'a> {
163
163
name : Option < & str > ,
164
164
key_description : & str ,
165
165
keyslot_old : Option < c_uint > ,
166
- keyslot_new : c_uint ,
167
- cipher_and_mode : ( & str , & str ) ,
166
+ keyslot_new : Option < c_uint > ,
167
+ cipher_and_mode : Option < ( & str , & str ) > ,
168
168
params : CryptParamsReencrypt ,
169
169
) -> Result < c_int , LibcryptErr > {
170
170
let name_cstring = match name {
171
171
Some ( n) => Some ( to_cstring ! ( n) ?) ,
172
172
None => None ,
173
173
} ;
174
- let ( cipher, cipher_mode) = cipher_and_mode;
174
+ let ( cipher_cstring_err, cipher_mode_cstring_err) = cipher_and_mode
175
+ . map ( |( c, cm) | {
176
+ (
177
+ Some ( to_cstring ! ( c) ) . transpose ( ) ,
178
+ Some ( to_cstring ! ( cm) ) . transpose ( ) ,
179
+ )
180
+ } )
181
+ . unwrap_or_else ( || ( Ok ( None ) , Ok ( None ) ) ) ;
182
+ let cipher_cstring = cipher_cstring_err?;
183
+ let cipher_mode_cstring = cipher_mode_cstring_err?;
175
184
let params_reencrypt: CryptParamsReencryptRef < ' _ > = ( & params) . try_into ( ) ?;
176
185
177
186
let description_cstring = to_cstring ! ( key_description) ?;
178
- let cipher_cstring = to_cstring ! ( cipher) ?;
179
- let cipher_mode_cstring = to_cstring ! ( cipher_mode) ?;
180
187
errno_int_success ! ( mutex!(
181
188
libcryptsetup_rs_sys:: crypt_reencrypt_init_by_keyring(
182
189
self . reference. as_ptr( ) ,
@@ -186,9 +193,17 @@ impl<'a> CryptLuks2ReencryptHandle<'a> {
186
193
. unwrap_or( ptr:: null( ) ) ,
187
194
description_cstring. as_ptr( ) ,
188
195
keyslot_old. map( |k| k as c_int) . unwrap_or( CRYPT_ANY_SLOT ) ,
189
- keyslot_new as c_int,
190
- cipher_cstring. as_ptr( ) ,
191
- cipher_mode_cstring. as_ptr( ) ,
196
+ keyslot_new. map( |k| k as c_int) . unwrap_or( CRYPT_ANY_SLOT ) ,
197
+ // NOTE: Must keep as_ref to avoid use after free error.
198
+ cipher_cstring
199
+ . as_ref( )
200
+ . map( |s| s. as_ptr( ) )
201
+ . unwrap_or_else( ptr:: null) ,
202
+ // NOTE: Must keep as_ref to avoid use after free error.
203
+ cipher_mode_cstring
204
+ . as_ref( )
205
+ . map( |s| s. as_ptr( ) )
206
+ . unwrap_or_else( ptr:: null) ,
192
207
params_reencrypt. as_ptr( ) ,
193
208
)
194
209
) )
0 commit comments