Permalink
Browse files

Raise custom PgpdumpException when encountering errors

Rather than the generic Exception class, we can subclass our own custom
exception to raise on parsing and other errors.

Signed-off-by: Dan McGee <dpmcgee@gmail.com>
  • Loading branch information...
1 parent 1aebeaf commit b690f52cbdd363cfaa6dcbddd1d90fb034a3558f @toofishes committed Apr 9, 2012
Showing with 29 additions and 20 deletions.
  1. +9 −7 pgpdump/data.py
  2. +8 −7 pgpdump/packet.py
  3. +6 −6 pgpdump/test.py
  4. +6 −0 pgpdump/utils.py
View
@@ -1,7 +1,7 @@
from base64 import b64decode
from .packet import construct_packet
-from .utils import crc24
+from .utils import PgpdumpException, crc24
class BinaryData(object):
@@ -11,15 +11,15 @@ class BinaryData(object):
def __init__(self, data):
if not data:
- raise Exception("no data to parse")
+ raise PgpdumpException("no data to parse")
if len(data) <= 1:
- raise Exception("data too short")
+ raise PgpdumpException("data too short")
data = bytearray(data)
# 7th bit of the first byte must be a 1
if not bool(data[0] & self.binary_tag_flag):
- raise Exception("incorrect binary data")
+ raise PgpdumpException("incorrect binary data")
self.data = data
self.length = len(data)
@@ -48,8 +48,9 @@ def __init__(self, data):
# verify it if we could find it
actual_crc = crc24(data)
if known_crc != actual_crc:
- raise Exception("CRC failure: known 0x%x, actual 0x%x" % (
- known_crc, actual_crc))
+ raise PgpdumpException(
+ "CRC failure: known 0x%x, actual 0x%x" % (
+ known_crc, actual_crc))
super(AsciiData, self).__init__(data)
@staticmethod
@@ -74,7 +75,8 @@ def strip_magic(data):
if nl_idx < 0:
nl_idx = data.find(b'\r\n\r\n', idx)
if nl_idx < 0:
- raise Exception("found magic, could not find start of data")
+ raise PgpdumpException(
+ "found magic, could not find start of data")
# now find the end of the data.
end_idx = data.find(b'-----', nl_idx)
if end_idx:
View
@@ -3,7 +3,8 @@
import re
from warnings import warn
-from .utils import get_int2, get_int4, get_mpi, get_key_id, get_int_bytes
+from .utils import (PgpdumpException, get_int2, get_int4, get_mpi,
+ get_key_id, get_int_bytes)
class Packet(object):
@@ -159,7 +160,7 @@ def parse(self):
# "hash material" byte must be 0x05
if self.data[offset] != 0x05:
- raise Exception("Invalid v3 signature packet")
+ raise PgpdumpException("Invalid v3 signature packet")
offset += 1
self.raw_sig_type = self.data[offset]
@@ -212,7 +213,7 @@ def parse(self):
self.hash2 = self.data[offset:offset + 2]
offset += 2
else:
- raise Exception("Unsupported signature packet, version %d" %
+ raise PgpdumpException("Unsupported signature packet, version %d" %
self.sig_version)
def parse_subpackets(self, outer_offset, outer_length, hashed=False):
@@ -230,7 +231,7 @@ def parse_subpackets(self, outer_offset, outer_length, hashed=False):
sub_data = self.data[offset:offset + sub_len]
if len(sub_data) != sub_len:
- raise Exception(
+ raise PgpdumpException(
"Unexpected subpackets length: expected %d, got %d" % (
sub_len, len(sub_data)))
subpacket = SignatureSubpacket(subtype, hashed, sub_data)
@@ -330,7 +331,7 @@ def parse(self):
offset = self.parse_key_material(offset)
# Key type must be RSA for v2 and v3 public keys
if self.pub_algorithm_type != "rsa":
- raise Exception("Invalid non-RSA v%d public key" %
+ raise PgpdumpException("Invalid non-RSA v%d public key" %
self.pubkey_version)
self.key_id = ('%X' % self.modulus)[-8:].zfill(8).encode('ascii')
@@ -356,7 +357,7 @@ def parse(self):
offset = self.parse_key_material(offset)
else:
- raise Exception("Unsupported public key packet, version %d" %
+ raise PgpdumpException("Unsupported public key packet, version %d" %
self.pubkey_version)
def parse_key_material(self, offset):
@@ -382,7 +383,7 @@ def parse_key_material(self, offset):
# Private/Experimental algorithms, just move on
pass
else:
- raise Exception("Unsupported public key algorithm %d" %
+ raise PgpdumpException("Unsupported public key algorithm %d" %
self.raw_pub_algorithm)
return offset
View
@@ -7,8 +7,8 @@
from pgpdump import AsciiData, BinaryData
from pgpdump.packet import (TAG_TYPES, SignaturePacket, PublicKeyPacket,
PublicSubkeyPacket, UserIDPacket, old_tag_length, new_tag_length)
-from pgpdump.utils import (crc24, get_int8, get_mpi, get_key_id,
- get_int_bytes, same_key)
+from pgpdump.utils import (PgpdumpException, crc24, get_int8, get_mpi,
+ get_key_id, get_int_bytes, same_key)
def load_data(filename):
@@ -82,15 +82,15 @@ def test_same_key(self):
class ParseTestCase(TestCase):
def test_parse_empty(self):
- with self.assertRaises(Exception):
+ with self.assertRaises(PgpdumpException):
BinaryData(None)
def test_parse_short(self):
- with self.assertRaises(Exception):
+ with self.assertRaises(PgpdumpException):
BinaryData([0x00])
def test_parse_invalid(self):
- with self.assertRaises(Exception):
+ with self.assertRaises(PgpdumpException):
BinaryData([0x00, 0x00])
def check_sig_packet(self, packet, length, version, typ,
@@ -157,7 +157,7 @@ def test_parse_bad_crc(self):
gMsAoLGOjudliDT9u0UqxN9KeJ22JdnX
=KYol
-----END PGP SIGNATURE-----'''
- self.assertRaises(Exception, AsciiData, asc_data)
+ self.assertRaises(PgpdumpException, AsciiData, asc_data)
def test_parse_v3_sig(self):
asc_data = b'''
View
@@ -1,5 +1,11 @@
import binascii
+
+class PgpdumpException(Exception):
+ '''Base exception class raised by any parsing errors, etc.'''
+ pass
+
+
# 256 values corresponding to each possible byte
CRC24_TABLE = (
0x000000, 0x864cfb, 0x8ad50d, 0x0c99f6, 0x93e6e1, 0x15aa1a, 0x1933ec,

0 comments on commit b690f52

Please sign in to comment.