check.py (9050B)
1 from cryptography.hazmat.backends import default_backend 2 from cryptography.hazmat.primitives import serialization 3 import os, prompts 4 5 def password_check(first_pass, second_pass): 6 """Returns `True` is the two passed strings match, 7 `False` otherwise. 8 9 Keyword arguments: 10 first_pass -- the first password/string 11 second_pass -- the second password/string 12 """ 13 if first_pass == second_pass: 14 return True 15 else: 16 return False 17 18 def key_enc(files, pass1, pass2, key_dir): 19 """Return `True` if all of the conditions are valid 20 for fresh-key passworded encryption. Otherwise, return 21 `False` and prompt the user of the errors. 22 23 Keyword arguments: 24 files -- a tuple of filepaths 25 pass1 -- the first password 26 pass2 -- the second password confirmation 27 key_dir -- the directory where the keys are to be saved 28 """ 29 broken_paths = "" 30 for fl in files: 31 if not os.access(fl, os.W_OK): 32 broken_paths += (fl + "\n") 33 else: 34 continue 35 password_match = password_check(pass1, pass2) 36 key_dir_access = True if os.access(key_dir, os.W_OK) else False 37 priv = key_dir + "/private_key.pem" 38 pub = key_dir + "/public_key.pem" 39 sym = key_dir + "/symmetric_key.key" 40 write_key = prompts.overwrite_prompt() if os.path.exists(priv) or os.path.exists(pub) or os.path.exists(sym) else True 41 if broken_paths == "" and password_match and key_dir_access and write_key: 42 return True 43 elif not write_key: 44 return False 45 else: 46 if broken_paths != "": 47 prompts.file_access_error(broken_paths) 48 if not password_match: 49 prompts.password_error() 50 if not key_dir_access: 51 prompts.key_dir_error(key_dir) 52 return False 53 54 55 def weak_key_enc(files, key_dir): 56 """Return `True` if all of the conditions are valid 57 for fresh-key passwordless encryption. Otherwise, return 58 `False` and prompt the user of the errors. 59 60 Keyword arguments: 61 files -- a tuple of filepaths 62 key_dir -- the directory where the keys are to be saved 63 """ 64 broken_paths = "" 65 for fl in files: 66 if not os.access(fl, os.W_OK): 67 broken_paths += (fl + "\n") 68 else: 69 continue 70 key_dir_access = True if os.access(key_dir, os.W_OK) else False 71 priv = key_dir + "/private_key.pem" 72 pub = key_dir + "/public_key.pem" 73 sym = key_dir + "/symmetric_key.key" 74 write_key = prompts.overwrite_prompt() if os.path.exists(priv) or os.path.exists(pub) or os.path.exists(sym) else True 75 if broken_paths == "" and key_dir_access and write_key: 76 return True 77 elif not write_key: 78 return False 79 else: 80 if broken_paths != "": 81 prompts.file_access_error(broken_paths) 82 if not key_dir_access: 83 prompts.key_dir_error(key_dir) 84 return False 85 86 87 def enc(files, key_dir): 88 """Return `True` if all of the conditions are valid 89 for generated key encryption. Otherwise, return 90 `False` and prompt the user of the errors. 91 92 Keyword arguments: 93 files -- a tuple of filepaths 94 key_dir -- the directory where the keys are located 95 """ 96 broken_paths = "" 97 rsa = True 98 for fl in files: 99 if not os.access(fl, os.W_OK): 100 broken_paths += (fl + "\n") 101 if os.path.getsize(fl) > 446: 102 rsa = False 103 pub = key_dir + "/public_key.pem" 104 sym = key_dir + "/symmetric_key.key" 105 proper_keys = True if (os.path.exists(pub) and rsa) or (not rsa and os.path.exists(pub) and os.path.exists(sym)) else False 106 if broken_paths == "" and proper_keys: 107 return True 108 else: 109 if broken_paths != "": 110 prompts.file_access_error(broken_paths) 111 if not proper_keys: 112 prompts.missing_keys(key_dir) 113 114 115 def key(key_dir, pass1, pass2): 116 """Return `True` if all of the conditions are valid 117 for generating passworded keys. Otherwise, return 118 `False` and prompt the user of the errors. 119 120 Keyword arguments: 121 key_dir -- the directory where are to be saved 122 pass1 -- the first password 123 pass2 -- the second confirmation password 124 """ 125 key_dir_access = True if os.access(key_dir, os.W_OK) else False 126 password_match = password_check(pass1, pass2) 127 priv = key_dir + "/private_key.pem" 128 pub = key_dir + "/public_key.pem" 129 sym = key_dir + "/symmetric_key.key" 130 write_key = prompts.overwrite_prompt() if os.path.exists(priv) or os.path.exists(pub) or os.path.exists(sym) else True 131 if key_dir_access and write_key and password_match: 132 return True 133 else: 134 if not key_dir_access: 135 prompts.key_dir_error(key_dir) 136 if not password_match: 137 prompts.password_error() 138 return False 139 140 141 def weak_key(key_dir): 142 """Return `True` if all of the conditions are valid 143 for generating passwordless keys. Otherwise, return 144 `False` and prompt the user of the errors. 145 146 Keyword arguments: 147 key_dir -- the directory where are to be saved 148 """ 149 key_dir_access = True if os.access(key_dir, os.W_OK) else False 150 priv = key_dir + "/private_key.pem" 151 pub = key_dir + "/public_key.pem" 152 sym = key_dir + "/symmetric_key.key" 153 write_key = prompts.overwrite_prompt() if os.path.exists(priv) or os.path.exists(pub) or os.path.exists(sym) else True 154 if key_dir_access and write_key: 155 return True 156 else: 157 if not key_dir_access: 158 prompts.key_dir_error(key_dir) 159 return False 160 161 162 def dec(files, pass1, pass2, key_dir): 163 """Return `True` if all of the conditions are valid 164 for passworded decryption. Otherwise, return 165 `False` and prompt the user of the errors. 166 167 Keyword arguments: 168 files -- a tuple of filepaths 169 pass1 -- the first password 170 pass2 -- the second confirmation password 171 key_dir -- the directory where the keys are located 172 """ 173 broken_paths = "" 174 rsa = True 175 for fl in files: 176 if not os.access(fl, os.W_OK): 177 broken_paths += fl + "\n" 178 with open(fl, "rb") as read_file: 179 tag = read_file.read()[-1] 180 if tag == 49: 181 rsa = False 182 password_match = password_check(pass1, pass2) 183 key_dir_access = True if os.access(key_dir, os.W_OK) else False 184 priv = key_dir + "/private_key.pem" 185 sym = key_dir + "/symmetric_key.key" 186 proper_keys = True if (rsa and os.path.exists(priv)) or (not rsa and os.path.exists(priv) and os.path.exists(sym)) else False 187 correct_pass = True 188 with open(priv, "rb") as priv_src: 189 try: 190 serialization.load_pem_private_key( 191 priv_src.read(), 192 password=bytes(pass1, "utf-8"), 193 backend=default_backend() 194 ) 195 except ValueError: 196 correct_pass = False 197 if ( 198 broken_paths == "" and key_dir_access 199 and password_match and proper_keys 200 and correct_pass 201 ): 202 return True 203 else: 204 if broken_paths != "": 205 prompts.file_access_error(broken_paths) 206 if not key_dir_access: 207 prompts.key_dir_error(key_dir) 208 if not password_match: 209 prompts.password_error() 210 if not proper_keys: 211 prompts.missing_keys(key_dir) 212 if not correct_pass: 213 prompts.wrong_pass() 214 return False 215 216 217 def weak_dec(files, key_dir): 218 """Return `True` if all of the conditions are valid 219 for passwordless decryption. Otherwise, return 220 `False` and prompt the user of the errors. 221 222 Keyword arguments: 223 files -- a tuple of filepaths 224 key_dir -- the directory where the keys are located 225 """ 226 broken_paths = "" 227 rsa = True 228 for fl in files: 229 if not os.access(fl, os.W_OK): 230 broken_paths += fl + "\n" 231 with open(fl, "rb") as read_file: 232 tag = read_file.read()[-1] 233 if tag == 49: 234 rsa = False 235 key_dir_access = True if os.access(key_dir, os.W_OK) else False 236 priv = key_dir + "/private_key.pem" 237 sym = key_dir + "/symmetric_key.key" 238 proper_keys = True if (rsa and os.path.exists(priv)) or (not rsa and os.path.exists(priv) and os.path.exists(sym)) else False 239 if proper_keys: 240 with open(priv, "rb") as priv_src: 241 try: 242 serialization.load_pem_private_key( 243 priv_src.read(), 244 password=None, 245 backend=default_backend() 246 ) 247 need_pass = False 248 except TypeError: 249 need_pass = True 250 else: 251 need_pass = False 252 if broken_paths == "" and key_dir_access and proper_keys and not need_pass: 253 return True 254 else: 255 if broken_paths != "": 256 prompts.file_access_error(broken_paths) 257 if not key_dir_access: 258 prompts.key_dir_error(key_dir) 259 if not proper_keys: 260 prompts.missing_keys(key_dir) 261 if need_pass: 262 prompts.encrypted_keys() 263 return False