@@ -10,6 +10,7 @@ use utils::{check_elements, get_db_path};
1010use crate :: cryptography;
1111use crate :: otp:: otp_element:: OTPElement ;
1212use crate :: utils;
13+ use zeroize:: Zeroize ;
1314
1415pub fn read_decrypted_text ( password : & str ) -> Result < String , String > {
1516 let encrypted_contents = read_to_string ( & get_db_path ( ) ) . unwrap ( ) ;
@@ -48,18 +49,20 @@ pub fn add_element(secret: &String, issuer: &String, label: &String, algorithm:
4849 Ok ( ( ) ) => { }
4950 Err ( error) => return Err ( error. to_string ( ) )
5051 }
51- let pw = & cryptography:: prompt_for_passwords ( "Password: " , 8 , false ) ;
52+ let mut pw = cryptography:: prompt_for_passwords ( "Password: " , 8 , false ) ;
5253 let otp_element = OTPElement :: new ( upper_secret. to_string ( ) , issuer. to_string ( ) , label. to_string ( ) , digits, String :: from ( "TOTP" ) , String :: from ( algorithm) . to_uppercase ( ) , String :: from ( "Default" ) , 0 , 0 , 30 , vec ! [ ] ) ;
5354 let mut elements;
54- match read_from_file ( pw) {
55+ match read_from_file ( & pw) {
5556 Ok ( result) => elements = result,
5657 Err ( e) => return Err ( e)
5758 }
5859 elements. push ( otp_element) ;
59- match overwrite_database ( elements, pw) {
60+ let result = match overwrite_database ( elements, & pw) {
6061 Ok ( ( ) ) => Ok ( ( ) ) ,
6162 Err ( e) => Err ( format ! ( "{}" , e) )
62- }
63+ } ;
64+ pw. zeroize ( ) ;
65+ result
6366}
6467
6568pub fn remove_element_from_db ( mut id : usize ) -> Result < ( ) , String > {
@@ -70,24 +73,26 @@ pub fn remove_element_from_db(mut id: usize) -> Result<(), String> {
7073 id -= 1 ;
7174
7275 let mut elements: Vec < OTPElement > ;
73- let pw = & cryptography:: prompt_for_passwords ( "Password: " , 8 , false ) ;
74- match read_from_file ( pw) {
76+ let mut pw = cryptography:: prompt_for_passwords ( "Password: " , 8 , false ) ;
77+ match read_from_file ( & pw) {
7578 Ok ( result) => elements = result,
7679 Err ( e) => {
7780 return Err ( e) ;
7881 }
7982 }
8083
81- match check_elements ( id, & elements) {
84+ let result = match check_elements ( id, & elements) {
8285 Ok ( ( ) ) => {
8386 elements. remove ( id) ;
84- match overwrite_database ( elements, pw) {
87+ match overwrite_database ( elements, & pw) {
8588 Ok ( ( ) ) => Ok ( ( ) ) ,
8689 Err ( e) => Err ( format ! ( "{}" , e) ) ,
8790 }
8891 }
8992 Err ( e) => Err ( e)
90- }
93+ } ;
94+ pw. zeroize ( ) ;
95+ result
9196}
9297
9398pub fn edit_element ( mut id : usize , secret : & str , issuer : & str , label : & str , algorithm : & str , digits : u64 ) -> Result < ( ) , String > {
@@ -97,13 +102,13 @@ pub fn edit_element(mut id: usize, secret: &str, issuer: &str, label: &str, algo
97102 id -= 1 ;
98103
99104 let mut elements: Vec < OTPElement > ;
100- let pw = & cryptography:: prompt_for_passwords ( "Password: " , 8 , false ) ;
101- match read_from_file ( pw) {
105+ let mut pw = cryptography:: prompt_for_passwords ( "Password: " , 8 , false ) ;
106+ match read_from_file ( & pw) {
102107 Ok ( result) => elements = result,
103108 Err ( _e) => return Err ( String :: from ( "Cannot decrypt existing database" ) )
104109 }
105110
106- match check_elements ( id, & elements) {
111+ let result = match check_elements ( id, & elements) {
107112 Ok ( ( ) ) => {
108113 if secret != "" {
109114 elements[ id] . set_secret ( secret. to_string ( ) ) ;
@@ -120,20 +125,24 @@ pub fn edit_element(mut id: usize, secret: &str, issuer: &str, label: &str, algo
120125 if digits != 0 {
121126 elements[ id] . set_digits ( digits) ;
122127 }
123- match overwrite_database ( elements, pw) {
128+ match overwrite_database ( elements, & pw) {
124129 Ok ( ( ) ) => Ok ( ( ) ) ,
125130 Err ( e) => Err ( format ! ( "{}" , e) ) ,
126131 }
127132 }
128133 Err ( e) => Err ( e)
129- }
134+ } ;
135+ pw. zeroize ( ) ;
136+ result
130137}
131138
132139pub fn export_database ( ) -> Result < PathBuf , String > {
133140 let exported_path = utils:: get_home_folder ( ) . join ( "exported.cotp" ) ;
134141 let mut file = File :: create ( & exported_path) . expect ( "Cannot create file" ) ;
135142 let encrypted_contents = read_to_string ( & get_db_path ( ) ) . unwrap ( ) ;
136- let contents = cryptography:: decrypt_string ( & encrypted_contents, & cryptography:: prompt_for_passwords ( "Password: " , 8 , false ) ) ;
143+ let mut pw = cryptography:: prompt_for_passwords ( "Password: " , 8 , false ) ;
144+ let contents = cryptography:: decrypt_string ( & encrypted_contents, & pw) ;
145+ pw. zeroize ( ) ;
137146 return match contents {
138147 Ok ( contents) => {
139148 if contents == "[]" {
0 commit comments