Skip to content

Commit

Permalink
Merge pull request #46 from tomato42/human-readable
Browse files Browse the repository at this point in the history
Human readable reverse lookups for TLS IDs
  • Loading branch information
tomato42 committed Nov 26, 2015
2 parents c9d2296 + 2a9d17d commit 85481b8
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 11 deletions.
59 changes: 49 additions & 10 deletions tlslite/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,33 @@

"""Constants used in various places."""

class TLSEnum(object):
"""Base class for different enums of TLS IDs"""

@classmethod
def toRepr(cls, value, blacklist=None):
"""
Convert numeric type to string representation
name if found, None otherwise
"""
if blacklist is None:
blacklist = []
return next((key for key, val in cls.__dict__.items() \
if key not in ('__weakref__', '__dict__', '__doc__',
'__module__') and \
key not in blacklist and \
val == value), None)

@classmethod
def toStr(cls, value, blacklist=None):
"""Convert numeric type to human-readable string if possible"""
ret = cls.toRepr(value, blacklist)
if ret is not None:
return ret
else:
return '{0}'.format(value)

class CertificateType:
x509 = 0
openpgp = 1
Expand All @@ -19,8 +46,10 @@ class ClientCertificateType:
dss_sign = 2
rsa_fixed_dh = 3
dss_fixed_dh = 4

class HandshakeType:

class HandshakeType(TLSEnum):
"""Message types in TLS Handshake protocol"""

hello_request = 0
client_hello = 1
server_hello = 2
Expand All @@ -33,12 +62,22 @@ class HandshakeType:
finished = 20
next_protocol = 67

class ContentType:
class ContentType(TLSEnum):
"""TLS record layer content types of payloads"""

change_cipher_spec = 20
alert = 21
handshake = 22
application_data = 23
all = (20,21,22,23)
all = (20, 21, 22, 23)

@classmethod
def toRepr(cls, value, blacklist=None):
"""Convert numeric type to name representation"""
if blacklist is None:
blacklist = []
blacklist.append('all')
return super(ContentType, cls).toRepr(value, blacklist)

class ExtensionType: # RFC 6066 / 4366
server_name = 0 # RFC 6066 / 4366
Expand All @@ -52,8 +91,7 @@ class ExtensionType: # RFC 6066 / 4366
supports_npn = 13172
renegotiation_info = 0xff01

class HashAlgorithm:

class HashAlgorithm(TLSEnum):
"""Hash algorithm IDs used in TLSv1.2"""

none = 0
Expand All @@ -64,8 +102,7 @@ class HashAlgorithm:
sha384 = 5
sha512 = 6

class SignatureAlgorithm:

class SignatureAlgorithm(TLSEnum):
"""Signing algorithms used in TLSv1.2"""

anonymous = 0
Expand Down Expand Up @@ -136,11 +173,13 @@ class ECPointFormat(object):
class NameType:
host_name = 0

class AlertLevel:
class AlertLevel(TLSEnum):
"""Enumeration of TLS Alert protocol levels"""

warning = 1
fatal = 2

class AlertDescription:
class AlertDescription(TLSEnum):
"""
@cvar bad_record_mac: A TLS record failed to decrypt properly.
Expand Down
46 changes: 45 additions & 1 deletion unit_tests/test_tlslite_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,51 @@
except ImportError:
import unittest

from tlslite.constants import CipherSuite
from tlslite.constants import CipherSuite, HashAlgorithm, SignatureAlgorithm, \
ContentType, AlertDescription, AlertLevel, HandshakeType

class TestHashAlgorithm(unittest.TestCase):

def test_toRepr(self):
self.assertEqual(HashAlgorithm.toRepr(5), 'sha384')

def test_toRepr_with_invalid_id(self):
self.assertIsNone(HashAlgorithm.toRepr(None))

def test_toRepr_with_unknown_id(self):
self.assertIsNone(HashAlgorithm.toRepr(200))

def test_toStr_with_unknown_id(self):
self.assertEqual(HashAlgorithm.toStr(200), '200')

def test_toStr(self):
self.assertEqual(HashAlgorithm.toStr(6), 'sha512')

class TestSignatureAlgorithm(unittest.TestCase):

def test_toRepr(self):
self.assertEqual(SignatureAlgorithm.toRepr(1), 'rsa')

class TestContentType(unittest.TestCase):

def test_toRepr_with_invalid_value(self):
self.assertIsNone(ContentType.toRepr((20, 21, 22, 23)))

def test_toStr_with_invalid_value(self):
self.assertEqual(ContentType.toStr((20, 21, 22, 23)),
'(20, 21, 22, 23)')

class TestAlertDescription(unittest.TestCase):
def test_toRepr(self):
self.assertEqual(AlertDescription.toStr(40), 'handshake_failure')

class TestAlertLevel(unittest.TestCase):
def test_toRepr(self):
self.assertEqual(AlertLevel.toStr(1), 'warning')

class TestHandshakeType(unittest.TestCase):
def test_toRepr(self):
self.assertEqual(HandshakeType.toStr(1), 'client_hello')

class TestCipherSuite(unittest.TestCase):

Expand Down

0 comments on commit 85481b8

Please sign in to comment.