Skip to content

Commit 20ec4d1

Browse files
committed
ASN.1: Use new error class relevant to ASN.1 conversions
Signed-off-by: Sebastien Awwad <sebastien.awwad@gmail.com>
1 parent c768756 commit 20ec4d1

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

tuf/encoding/asn1_convert.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def hex_str_to_pyasn1_octets(hex_string):
248248
# TODO: Verify hex_string type.
249249

250250
if len(hex_string) % 2:
251-
raise tuf.exceptions.Error(
251+
raise tuf.exceptions.ASN1ConversionError(
252252
'Expecting hex strings with an even number of digits, since hex '
253253
'strings provide 2 characters per byte. We prefer not to pad values '
254254
'implicitly.')
@@ -283,7 +283,7 @@ def hex_str_from_pyasn1_octets(octets_pyasn1):
283283

284284
for x in octets:
285285
if x < 0 or x > 255:
286-
raise tuf.exceptions.Error(
286+
raise tuf.exceptions.ASN1ConversionError(
287287
'Unable to generate hex string from OctetString: integer value of '
288288
'octet provided is not in range: ' + str(x))
289289
hex_string += '%.2x' % x
@@ -359,7 +359,7 @@ def to_pyasn1(data, datatype):
359359
debug('Converting a (hopefully-)primitive value to ' + str(datatype)) # DEBUG
360360
tuf.formats.HEX_SCHEMA.check_match(data)
361361
if len(data) % 2:
362-
raise tuf.exceptions.Error(
362+
raise tuf.exceptions.ASN1ConversionError(
363363
'Expecting hex strings with an even number of digits, since hex '
364364
'strings provide 2 characters per byte. We prefer not to pad values '
365365
'implicitly.')
@@ -436,9 +436,9 @@ def to_pyasn1(data, datatype):
436436
return pyasn1_obj
437437

438438
else:
439-
# TODO: Use a better error class for ASN.1 conversion errors.
440439
recursion_level -= 1
441-
raise tuf.exceptions.Error('Unable to determine how to automatically '
440+
raise tuf.exceptions.ASN1ConversionError(
441+
'Unable to determine how to automatically '
442442
'convert data into pyasn1 data. Can only handle primitives to Integer/'
443443
'VisibleString/OctetString, or list to list-like pyasn1, or list-like '
444444
'dict to list-like pyasn1, or struct-like dict to struct-like pyasn1. '
@@ -531,8 +531,8 @@ def _structlike_dict_to_pyasn1(data, datatype):
531531
# pyasn1_obj[element_name] = len(data[relevant_element_name_python])
532532

533533
# else:
534-
# # TODO: Use a better exception class, relevant to ASN1 conversion.
535-
# raise tuf.exceptions.Error('When converting dict into pyasn1, '
534+
# raise tuf.exceptions.ASN1ConversionError(
535+
# 'When converting dict into pyasn1, '
536536
# 'found an element that appeared to be a "num-"-prefixed '
537537
# 'length-of-list for another element; however, did not find '
538538
# 'corresponding element to calculate length of. Element name: ' +
@@ -542,8 +542,8 @@ def _structlike_dict_to_pyasn1(data, datatype):
542542
else:
543543
# Found an element name in datatype that does not match anything in
544544
# data (MOOT: and does not begin with 'num-').
545-
# TODO: Use a better exception class, relevant to ASN1 conversion.
546-
raise tuf.exceptions.Error('Unable to convert dict into pyasn1: it '
545+
raise tuf.exceptions.ASN1ConversionError(
546+
'Unable to convert dict into pyasn1: it '
547547
'seems to be missing elements. dict does not contain "' +
548548
element_name + '". Datatype for conversion: ' + str(datatype) +
549549
'; dict contents: ' + str(data))
@@ -572,10 +572,11 @@ def _list_to_pyasn1(data, datatype):
572572
pyasn1_obj = datatype() # DEBUG
573573

574574
if None is getattr(datatype, 'componentType', None):
575-
# TODO: Use a better exception class, if you decide to keep this.
575+
# TODO: Determine whether or not to keep this error.
576576
# It's useful in debugging because the error we get if we don't
577577
# specifically detect this may be misleading.
578-
raise tuf.exceptions.Error('Unable to determine type of component in a '
578+
raise tuf.exceptions.ASN1ConversionError(
579+
'Unable to determine type of component in a '
579580
'list. datatype of list: ' + str(datatype) + '; componentType '
580581
'appears to be None')
581582

@@ -660,7 +661,7 @@ def _listlike_dict_to_pyasn1(data, datatype):
660661
# We are assuming that we can convert in={k1: v1, k2: v2, ...} to
661662
# out[i][0] = k1, out[0][1] = v1,
662663
# TODO: more useful error message and conversion-specific exception class
663-
raise tuf.exceptions.Error()
664+
raise tuf.exceptions.ASN1ConversionError()
664665

665666
i = 0
666667
for key in data:
@@ -746,9 +747,9 @@ def from_pyasn1(data, datatype):
746747

747748

748749
else:
749-
# TODO: Use a better error class for ASN.1 conversion errors.
750750
recursion_level -= 1
751-
raise tuf.exceptions.Error('Unable to determine how to automatically '
751+
raise tuf.exceptions.ASN1ConversionError(
752+
'Unable to determine how to automatically '
752753
'convert data from pyasn1 data. Can only handle primitives to Integer/'
753754
'VisibleString/OctetString, or list-like dict from list-like pyasn1, '
754755
'or struct-like dict to struct-like pyasn1. '
@@ -831,10 +832,11 @@ def _list_from_pyasn1(data, datatype):
831832
list_python = []
832833

833834
if None is getattr(datatype, 'componentType', None):
834-
# TODO: Use a better exception class, if you decide to keep this.
835+
# TODO: Determine whether or not to keep this error.
835836
# It's useful in debugging because the error we get if we don't
836837
# specifically detect this may be misleading.
837-
raise tuf.exceptions.Error('Unable to determine type of component in a '
838+
raise tuf.exceptions.ASN1ConversionError(
839+
'Unable to determine type of component in a '
838840
'list. datatype of list: ' + str(datatype) + '; componentType '
839841
'appears to be None')
840842

tuf/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,11 @@ class InvalidConfigurationError(Error):
279279
"""If a configuration object does not match the expected format."""
280280
pass
281281

282+
283+
class ASN1ConversionError(Error):
284+
"""
285+
If an object cannot be converted from TUF's internal metadata format (as
286+
defined in the specification) to the ASN.1 format this implementation allows,
287+
or vice versa.
288+
"""
289+
pass

0 commit comments

Comments
 (0)