improve error handling in crypto handler
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Mon, 10 Apr 2017 15:37:33 +0000 (17:37 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 2 Apr 2018 11:34:08 +0000 (13:34 +0200)
Since invalid tags are some the most important bits of
information to be passed down, make the corresponding error
message human-readable.

deltatar/crypto.py

index 6970786..dfdb1d6 100755 (executable)
@@ -689,9 +689,9 @@ class Decrypt (Crypto):
                                             % type (tag))
                 data = self.enc.finalize_with_tag (self.tag)
         except cryptography.exceptions.InvalidTag:
-            raise InvalidGCMTag ("done: tag mismatch of object %d: %r "
+            raise InvalidGCMTag ("done: tag mismatch of object %d: %s "
                                   "rejected by finalize ()"
-                                  % (self.cnt, self.tag))
+                                  % (self.cnt, binascii.hexlify (self.tag)))
         self.ctsize += len (data)
         self.stats ["out"] += len (data)
         return data
@@ -770,8 +770,9 @@ def depdtcrypt (pw, ins, outs):
                 try:
                     pt = decr.done ()
                 except InvalidGCMTag as exn:
-                    raise DecryptionError ("error finalizing object (%s): %r"
-                                           % (pt, exn))
+                    raise DecryptionError ("error finalizing object %d (%d B): "
+                                           "%r" % (total_obj, len (pt), exn)) \
+                          from exn
                 out (pt)
                 if PDTCRYPT_VERBOSE is True:
                     noise ("PDT:\t· object validated")
@@ -929,8 +930,18 @@ def parse_argv (argv):
 
 def main (argv):
     pw, ins, outs  = parse_argv (argv)
-    total_read, total_obj, total_ct, total_pt = \
-        depdtcrypt (pw, ins, outs)
+    try:
+        total_read, total_obj, total_ct, total_pt = \
+            depdtcrypt (pw, ins, outs)
+    except DecryptionError as exn:
+        noise ("PDT: Decryption failed:")
+        noise ("PDT:")
+        noise ("PDT:    “%s”" % exn)
+        noise ("PDT:")
+        noise ("PDT: Did you specify the correct password?")
+        noise ("")
+        return 1
+
     if PDTCRYPT_VERBOSE is True:
         noise ("PDT: decryption successful"                 )
         noise ("PDT:   %.10d bytes read"        % total_read)
@@ -939,6 +950,8 @@ def main (argv):
         noise ("PDT:   %.10d bytes plaintext"   % total_pt  )
         noise (""                                           )
 
+    return 0
+
 
 if __name__ == "__main__":
     sys.exit (main (sys.argv))