-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrustls.py
94 lines (79 loc) · 2.31 KB
/
rustls.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import tls.protocol_types as TY
import tls.base as BASE
import sys
start_enums = """/// This file is autogenerated. See https://github.com/ctz/tls-hacking/
use msgs::codec::{encode_u8, read_u8, encode_u16, read_u16, Reader, Codec};"""
enum_def = """
enum_builder! {
/// The `%(name)s` TLS protocol enum. Values in this enum are taken
/// from the various RFCs covering TLS, and are listed by IANA.
/// The `Unknown` item is used when processing unrecognised ordinals.
@%(underlying_type)s
EnumName: %(name)s;
EnumVal{
%(enum_items)s
}
}"""
def convert_enum(ty):
table = ty.table()
items = list(table.items())
items = list(filter(lambda x: isinstance(x[0], int), items))
items.sort(key = lambda x: x[0])
name = ty.__name__
if issubclass(ty, BASE.Enum8):
underlying_type = 'u8'
val_format = lambda x: '0x%02x' % x
else:
underlying_type = 'u16'
val_format = lambda x: '0x%04x' % x
enum_items = ',\n'.join(' %s => %s' % (x, val_format(v)) for v, x in items)
data = dict(
name = name,
enum_items = enum_items,
underlying_type = underlying_type.upper(),
)
print(enum_def % data)
def test_enum(ty):
name = ty.__name__
table = ty.table()
items = list(table.items())
items = list(filter(lambda x: isinstance(x[0], int), items))
items.sort(key = lambda x: x[0])
first, last = items[0][1], items[-1][1]
if issubclass(ty, BASE.Enum8):
test_fn = 'test_enum8'
else:
test_fn = 'test_enum16'
print('%(test_fn)s::<%(name)s>(%(name)s::%(first)s, %(name)s::%(last)s);' %
locals())
types = [
TY.ProtocolVersion,
TY.HashAlgorithm,
TY.SignatureAlgorithm,
TY.ClientCertificateType,
TY.Compression,
TY.ContentType,
TY.HandshakeType,
TY.AlertLevel,
TY.AlertDescription,
TY.HeartbeatMessageType,
TY.ExtensionType,
TY.ServerNameType,
TY.NamedCurve,
TY.NamedGroup,
TY.CipherSuite,
TY.ECPointFormat,
TY.HeartbeatMode,
TY.ECCurveType,
TY.SignatureScheme,
TY.PSKKeyExchangeMode,
TY.KeyUpdateRequest,
TY.CertificateStatusType,
]
if sys.argv[-1] == 'test':
for ty in types:
test_enum(ty)
else:
print(start_enums)
for ty in types:
convert_enum(ty)