/usr/lib/python3/dist-packages/tlslite/extensions.py is in python3-tlslite-ng 0.5.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 | # Copyright (c) 2014, Hubert Kario
#
# See the LICENSE file for legal information regarding use of this file.
""" Helper package for handling TLS extensions encountered in ClientHello
and ServerHello messages.
"""
from __future__ import generators
from .utils.codec import Writer, Parser
from collections import namedtuple
from .constants import NameType, ExtensionType
from .errors import TLSInternalError
class TLSExtension(object):
"""
This class handles the generic information about TLS extensions used by
both sides of connection in Client Hello and Server Hello messages.
See U{RFC 4366<https://tools.ietf.org/html/rfc4366>} for more info.
It is used as a base class for specific users and as a way to store
extensions that are not implemented in library.
@type extType: int
@ivar extType: a 2^16-1 limited integer specifying the type of the
extension that it contains, e.g. 0 indicates server name extension
@type extData: bytearray
@ivar extData: a byte array containing the value of the extension as
to be written on the wire
@type serverType: boolean
@ivar serverType: indicates that the extension was parsed with ServerHello
specific parser, otherwise it used universal or ClientHello specific
parser
@type _universalExtensions: dict
@cvar _universalExtensions: dictionary with concrete implementations of
specific TLS extensions where key is the numeric value of the extension
ID. Contains ClientHello version of extensions or universal
implementations
@type _serverExtensions: dict
@cvar _serverExtensions: dictionary with concrete implementations of
specific TLS extensions where key is the numeric value of the extension
ID. Includes only those extensions that require special handlers for
ServerHello versions.
"""
# actual definition at the end of file, after definitions of all classes
_universalExtensions = {}
_serverExtensions = {}
def __init__(self, server=False):
"""
Creates a generic TLS extension that can be used either for
client hello or server hello message parsing or creation.
You'll need to use L{create} or L{parse} methods to create an extension
that is actually usable.
@type server: boolean
@param server: whatever to select ClientHello or ServerHello version
for parsing
"""
self.extType = None
self.extData = bytearray(0)
self.serverType = server
def create(self, extType, data):
"""
Initializes a generic TLS extension that can later be used in
client hello or server hello messages
@type extType: int
@param extType: type of the extension encoded as an integer between
M{0} and M{2^16-1}
@type data: bytearray
@param data: raw data representing extension on the wire
@rtype: L{TLSExtension}
"""
self.extType = extType
self.extData = data
return self
def write(self):
""" Returns encoded extension, as encoded on the wire
@rtype: bytearray
@return: An array of bytes formatted as is supposed to be written on
the wire, including the extension_type, length and the extension
data
@raise AssertionError: when the object was not initialized
"""
assert self.extType is not None
w = Writer()
w.add(self.extType, 2)
w.add(len(self.extData), 2)
w.addFixSeq(self.extData, 1)
return w.bytes
def parse(self, p):
""" Parses extension from the wire format
@type p: L{tlslite.util.codec.Parser}
@param p: data to be parsed
@raise SyntaxError: when the size of the passed element doesn't match
the internal representation
@rtype: L{TLSExtension}
"""
extType = p.get(2)
ext_length = p.get(2)
# first check if we shouldn't use server side parser
if self.serverType and extType in self._serverExtensions:
ext = self._serverExtensions[extType]()
ext_parser = Parser(p.getFixBytes(ext_length))
ext = ext.parse(ext_parser)
return ext
# then fallback to universal/ClientHello-specific parsers
if extType in self._universalExtensions:
ext = self._universalExtensions[extType]()
ext_parser = Parser(p.getFixBytes(ext_length))
ext = ext.parse(ext_parser)
return ext
# finally, just save the extension data as there are extensions which
# don't require specific handlers and indicate option by mere presence
self.extType = extType
self.extData = p.getFixBytes(ext_length)
assert len(self.extData) == ext_length
return self
def __eq__(self, that):
""" Test if two TLS extensions will result in the same on the wire
representation.
Will return False for every object that's not an extension.
"""
if hasattr(that, 'extType') and hasattr(that, 'extData'):
return self.extType == that.extType and \
self.extData == that.extData
else:
return False
def __repr__(self):
""" Output human readable representation of object
@rtype: str
"""
return "TLSExtension(extType={0!r}, extData={1!r},"\
" serverType={2!r})".format(self.extType, self.extData,
self.serverType)
class SNIExtension(TLSExtension):
"""
Class for handling Server Name Indication (server_name) extension from
RFC 4366.
Note that while usually the client does advertise just one name, it is
possible to provide a list of names, each of different type.
The type is a single byte value (represented by ints), the names are
opaque byte strings, in case of DNS host names (records of type 0) they
are UTF-8 encoded domain names (without the ending dot).
@type hostNames: tuple of bytearrays
@ivar hostNames: tuple of hostnames (server name records of type 0)
advertised in the extension. Note that it may not include all names
from client hello as the client can advertise other types. Also note
that while it's not possible to change the returned array in place, it
is possible to assign a new set of names. IOW, this won't work::
sni_extension.hostNames[0] = bytearray(b'example.com')
while this will work::
names = list(sni_extension.hostNames)
names[0] = bytearray(b'example.com')
sni_extension.hostNames = names
@type serverNames: list of L{ServerName}
@ivar serverNames: list of all names advertised in extension.
L{ServerName} is a namedtuple with two elements, the first
element (type) defines the type of the name (encoded as int)
while the other (name) is a bytearray that carries the value.
Known types are defined in L{tlslite.constants.NameType}.
The list will be empty if the on the wire extension had and empty
list while it will be None if the extension was empty.
@type extType: int
@ivar extType: numeric type of SNIExtension, i.e. 0
@type extData: bytearray
@ivar extData: raw representation of the extension
"""
ServerName = namedtuple('ServerName', 'name_type name')
def __init__(self):
"""
Create an instance of SNIExtension.
See also: L{create} and L{parse}.
"""
self.serverNames = None
def __repr__(self):
"""
Return programmer-readable representation of extension
@rtype: str
"""
return "SNIExtension(serverNames={0!r})".format(self.serverNames)
def create(self, hostname=None, hostNames=None, serverNames=None):
"""
Initializes an instance with provided hostname, host names or
raw server names.
Any of the parameters may be None, in that case the list inside the
extension won't be defined, if either hostNames or serverNames is
an empty list, then the extension will define a list of lenght 0.
If multiple parameters are specified at the same time, then the
resulting list of names will be concatenated in order of hostname,
hostNames and serverNames last.
@type hostname: bytearray
@param hostname: raw UTF-8 encoding of the host name
@type hostNames: list of bytearrays
@param hostNames: list of raw UTF-8 encoded host names
@type serverNames: list of L{ServerName}
@param serverNames: pairs of name_type and name encoded as a namedtuple
@rtype: L{SNIExtension}
"""
if hostname is None and hostNames is None and serverNames is None:
self.serverNames = None
return self
else:
self.serverNames = []
if hostname:
self.serverNames += [SNIExtension.ServerName(NameType.host_name,\
hostname)]
if hostNames:
self.serverNames +=\
[SNIExtension.ServerName(NameType.host_name, x) for x in\
hostNames]
if serverNames:
self.serverNames += serverNames
return self
@property
def extType(self):
""" Return the type of TLS extension, in this case - 0
@rtype: int
"""
return ExtensionType.server_name
@property
def hostNames(self):
""" Returns a simulated list of hostNames from the extension.
@rtype: tuple of bytearrays
"""
# because we can't simulate assignments to array elements we return
# an immutable type
if self.serverNames is None:
return tuple()
else:
return tuple([x.name for x in self.serverNames if \
x.name_type == NameType.host_name])
@hostNames.setter
def hostNames(self, hostNames):
""" Removes all host names from the extension and replaces them by
names in X{hostNames} parameter.
Newly added parameters will be added at the I{beginning} of the list
of extensions.
@type hostNames: iterable of bytearrays
@param hostNames: host names to replace the old server names of type 0
"""
self.serverNames = \
[SNIExtension.ServerName(NameType.host_name, x) for x in \
hostNames] + \
[x for x in self.serverNames if \
x.name_type != NameType.host_name]
@hostNames.deleter
def hostNames(self):
""" Remove all host names from extension, leaves other name types
unmodified
"""
self.serverNames = [x for x in self.serverNames if \
x.name_type != NameType.host_name]
@property
def extData(self):
""" raw encoding of extension data, without type and length header
@rtype: bytearray
"""
if self.serverNames is None:
return bytearray(0)
w2 = Writer()
for server_name in self.serverNames:
w2.add(server_name.name_type, 1)
w2.add(len(server_name.name), 2)
w2.bytes += server_name.name
# note that when the array is empty we write it as array of length 0
w = Writer()
w.add(len(w2.bytes), 2)
w.bytes += w2.bytes
return w.bytes
def write(self):
""" Returns encoded extension, as encoded on the wire
@rtype: bytearray
@return: an array of bytes formatted as they are supposed to be written
on the wire, including the type, length and extension data
"""
raw_data = self.extData
w = Writer()
w.add(self.extType, 2)
w.add(len(raw_data), 2)
w.bytes += raw_data
return w.bytes
def parse(self, p):
"""
Deserialise the extension from on-the-wire data
The parser should not include the type or length of extension!
@type p: L{tlslite.util.codec.Parser}
@param p: data to be parsed
@rtype: L{SNIExtension}
@raise SyntaxError: when the internal sizes don't match the attached
data
"""
if p.getRemainingLength() == 0:
return self
self.serverNames = []
p.startLengthCheck(2)
while not p.atLengthCheck():
sn_type = p.get(1)
sn_name = p.getVarBytes(2)
self.serverNames += [SNIExtension.ServerName(sn_type, sn_name)]
p.stopLengthCheck()
return self
class ClientCertTypeExtension(TLSExtension):
"""
This class handles the Certificate Type extension (variant sent by client)
defined in RFC 6091.
@type extType: int
@ivar extType: numeric type of Certificate Type extension, i.e. 9
@type extData: bytearray
@ivar extData: raw representation of the extension data
@type certTypes: list of int
@ivar certTypes: list of certificate type identifiers (each one byte long)
"""
def __init__(self):
"""
Create an instance of ClientCertTypeExtension
See also: L{create} and L{parse}
"""
self.certTypes = None
def __repr__(self):
""" Return programmer-centric representation of extension
@rtype: str
"""
return "ClientCertTypeExtension(certTypes={0!r})"\
.format(self.certTypes)
@property
def extType(self):
"""
Return the type of TLS extension, in this case - 9
@rtype: int
"""
return ExtensionType.cert_type
@property
def extData(self):
"""
Return the raw encoding of this extension
@rtype: bytearray
"""
if self.certTypes is None:
return bytearray(0)
w = Writer()
w.add(len(self.certTypes), 1)
for c_type in self.certTypes:
w.add(c_type, 1)
return w.bytes
def create(self, certTypes=None):
"""
Return instance of this extension with specified certificate types
@type certTypes: iterable list of int
@param certTypes: list of certificate types to advertise, all values
should be between 0 and 2^8-1 inclusive
@raises ValueError: when the list includes too big or negative integers
"""
self.certTypes = certTypes
return self
def parse(self, p):
"""
Parse the extension from binary data
@type p: L{tlslite.util.codec.Parser}
@param p: data to be parsed
@raise SyntaxError: when the size of the passed element doesn't match
the internal representation
@rtype: L{ClientCertTypeExtension}
"""
self.certTypes = p.getVarList(1, 1)
return self
class ServerCertTypeExtension(TLSExtension):
"""
This class handles the Certificate Type extension (variant sent by server)
defined in RFC 6091.
@type extType: int
@ivar extType: byneruc ttoe if Certificate Type extension, i.e. 9
@type extData: bytearray
@ivar extData: raw representation of the extension data
@type cert_type: int
@ivar cert_type: the certificate type selected by server
"""
def __init__(self):
"""
Create an instance of ServerCertTypeExtension
See also: L{create} and L{parse}
"""
self.cert_type = None
def __repr__(self):
""" Return programmer-centric description of object
@rtype: str
"""
return "ServerCertTypeExtension(cert_type={0!r})".format(self.cert_type)
@property
def extType(self):
"""
Return the type of TLS extension, in this case - 9
@rtype: int
"""
return ExtensionType.cert_type
@property
def extData(self):
"""
Return the raw encoding of the extension data
@rtype: bytearray
"""
if self.cert_type is None:
return bytearray(0)
w = Writer()
w.add(self.cert_type, 1)
return w.bytes
def create(self, val):
"""Create an instance for sending the extension to client.
@type val: int
@param val: selected type of certificate
"""
self.cert_type = val
return self
def parse(self, p):
"""Parse the extension from on the wire format
@type p: L{Parser}
@param p: parser with data
"""
self.cert_type = p.get(1)
if p.getRemainingLength() > 0:
raise SyntaxError()
return self
class SRPExtension(TLSExtension):
"""
This class handles the Secure Remote Password protocol TLS extension
defined in RFC 5054.
@type extType: int
@ivar extType: numeric type of SRPExtension, i.e. 12
@type extData: bytearray
@ivar extData: raw representation of extension data
@type identity: bytearray
@ivar identity: UTF-8 encoding of user name
"""
def __init__(self):
"""
Create an instance of SRPExtension
See also: L{create} and L{parse}
"""
self.identity = None
def __repr__(self):
"""
Return programmer-centric description of extension
@rtype: str
"""
return "SRPExtension(identity={0!r})".format(self.identity)
@property
def extType(self):
"""
Return the type of TLS extension, in this case - 12
@rtype: int
"""
return ExtensionType.srp
@property
def extData(self):
"""
Return raw data encoding of the extension
@rtype: bytearray
"""
if self.identity is None:
return bytearray(0)
w = Writer()
w.add(len(self.identity), 1)
w.addFixSeq(self.identity, 1)
return w.bytes
def create(self, identity=None):
""" Create and instance of SRPExtension with specified protocols
@type identity: bytearray
@param identity: UTF-8 encoded identity (user name) to be provided
to user. MUST be shorter than 2^8-1.
@raise ValueError: when the identity lenght is longer than 2^8-1
"""
if identity is None:
return self
if len(identity) >= 2**8:
raise ValueError()
self.identity = identity
return self
def parse(self, p):
"""
Parse the extension from on the wire format
@type p: L{tlslite.util.codec.Parser}
@param p: data to be parsed
@raise SyntaxError: when the data is internally inconsistent
@rtype: L{SRPExtension}
"""
self.identity = p.getVarBytes(1)
return self
class NPNExtension(TLSExtension):
"""
This class handles the unofficial Next Protocol Negotiation TLS extension.
@type protocols: list of bytearrays
@ivar protocols: list of protocol names supported by the server
@type extType: int
@ivar extType: numeric type of NPNExtension, i.e. 13172
@type extData: bytearray
@ivar extData: raw representation of extension data
"""
def __init__(self):
"""
Create an instance of NPNExtension
See also: L{create} and L{parse}
"""
self.protocols = None
def __repr__(self):
"""
Create programmer-readable version of representation
@rtype: str
"""
return "NPNExtension(protocols={0!r})".format(self.protocols)
@property
def extType(self):
""" Return the type of TLS extension, in this case - 13172
@rtype: int
"""
return ExtensionType.supports_npn
@property
def extData(self):
""" Return the raw data encoding of the extension
@rtype: bytearray
"""
if self.protocols is None:
return bytearray(0)
w = Writer()
for prot in self.protocols:
w.add(len(prot), 1)
w.addFixSeq(prot, 1)
return w.bytes
def create(self, protocols=None):
""" Create an instance of NPNExtension with specified protocols
@type protocols: list of bytearray
@param protocols: list of protocol names that are supported
"""
self.protocols = protocols
return self
def parse(self, p):
""" Parse the extension from on the wire format
@type p: L{tlslite.util.codec.Parser}
@param p: data to be parsed
@raise SyntaxError: when the size of the passed element doesn't match
the internal representation
@rtype: L{NPNExtension}
"""
self.protocols = []
while p.getRemainingLength() > 0:
self.protocols += [p.getVarBytes(1)]
return self
class TACKExtension(TLSExtension):
"""
This class handles the server side TACK extension (see
draft-perrin-tls-tack-02).
@type tacks: list
@ivar tacks: list of L{TACK}'s supported by server
@type activation_flags: int
@ivar activation_flags: activation flags for the tacks
"""
class TACK(object):
"""
Implementation of the single TACK
"""
def __init__(self):
"""
Create a single TACK object
"""
self.public_key = bytearray(64)
self.min_generation = 0
self.generation = 0
self.expiration = 0
self.target_hash = bytearray(32)
self.signature = bytearray(64)
def __repr__(self):
"""
Return programmmer readable representation of TACK object
@rtype: str
"""
return "TACK(public_key={0!r}, min_generation={1!r}, "\
"generation={2!r}, expiration={3!r}, target_hash={4!r}, "\
"signature={5!r})".format(
self.public_key, self.min_generation,
self.generation, self.expiration, self.target_hash,
self.signature)
def create(self, public_key, min_generation, generation, expiration,
target_hash, signature):
"""
Initialise the TACK with data
"""
self.public_key = public_key
self.min_generation = min_generation
self.generation = generation
self.expiration = expiration
self.target_hash = target_hash
self.signature = signature
return self
def write(self):
"""
Convert the TACK into on the wire format
@rtype: bytearray
"""
w = Writer()
if len(self.public_key) != 64:
raise TLSInternalError("Public_key must be 64 bytes long")
w.bytes += self.public_key
w.add(self.min_generation, 1)
w.add(self.generation, 1)
w.add(self.expiration, 4)
if len(self.target_hash) != 32:
raise TLSInternalError("Target_hash must be 32 bytes long")
w.bytes += self.target_hash
if len(self.signature) != 64:
raise TLSInternalError("Signature must be 64 bytes long")
w.bytes += self.signature
return w.bytes
def parse(self, p):
"""
Parse the TACK from on the wire format
@type p: L{tlslite.util.codec.Parser}
@param p: data to be parsed
@rtype: L{TACK}
@raise SyntaxError: when the internal sizes don't match the
provided data
"""
self.public_key = p.getFixBytes(64)
self.min_generation = p.get(1)
self.generation = p.get(1)
self.expiration = p.get(4)
self.target_hash = p.getFixBytes(32)
self.signature = p.getFixBytes(64)
return self
def __eq__(self, other):
"""
Tests if the other object is equivalent to this TACK
Returns False for every object that's not a TACK
"""
if hasattr(other, 'public_key') and\
hasattr(other, 'min_generation') and\
hasattr(other, 'generation') and\
hasattr(other, 'expiration') and\
hasattr(other, 'target_hash') and\
hasattr(other, 'signature'):
if self.public_key == other.public_key and\
self.min_generation == other.min_generation and\
self.generation == other.generation and\
self.expiration == other.expiration and\
self.target_hash == other.target_hash and\
self.signature == other.signature:
return True
else:
return False
else:
return False
def __init__(self):
"""
Create an instance of TACKExtension
See also: L{create} and L{parse}
"""
self.tacks = []
self.activation_flags = 0
def __repr__(self):
"""
Create a programmer readable representation of TACK extension
@rtype: str
"""
return "TACKExtension(activation_flags={0!r}, tacks={1!r})".format(
self.activation_flags, self.tacks)
@property
def extType(self):
"""
Returns the type of TLS extension, in this case - 62208
@rtype: int
"""
return ExtensionType.tack
@property
def extData(self):
"""
Return the raw data encoding of the extension
@rtype: bytearray
"""
w2 = Writer()
for t in self.tacks:
w2.bytes += t.write()
w = Writer()
w.add(len(w2.bytes), 2)
w.bytes += w2.bytes
w.add(self.activation_flags, 1)
return w.bytes
def create(self, tacks, activation_flags):
"""
Initialize the insance of TACKExtension
@rtype: TACKExtension
"""
self.tacks = tacks
self.activation_flags = activation_flags
return self
def parse(self, p):
"""
Parse the extension from on the wire format
@type p: L{tlslite.util.codec.Parser}
@param p: data to be parsed
@rtype: L{TACKExtension}
"""
self.tacks = []
p.startLengthCheck(2)
while not p.atLengthCheck():
tack = TACKExtension.TACK().parse(p)
self.tacks += [tack]
p.stopLengthCheck()
self.activation_flags = p.get(1)
return self
class SupportedGroupsExtension(TLSExtension):
"""
Client side list of supported groups of (EC)DHE key exchage.
See RFC4492, RFC7027 and RFC-ietf-tls-negotiated-ff-dhe-10
@type groups: int
@ivar groups: list of groups that the client supports
"""
def __init__(self):
"""Create instance of class"""
self.groups = None
@property
def extType(self):
"""
Type of extension, in this case - 10
@rtype: int
"""
return ExtensionType.supported_groups
@property
def extData(self):
"""
Return raw data encoding of the extension
@rtype: bytearray
"""
if self.groups is None:
return bytearray(0)
writer = Writer()
# encode length of two bytes per group in two bytes
writer.add(len(self.groups) * 2, 2)
for group in self.groups:
writer.add(group, 2)
return writer.bytes
def create(self, groups):
"""
Set the supported groups in the extension
@type groups: list of int
@param groups: list of supported groups
"""
self.groups = groups
return self
def parse(self, parser):
"""
Deserialise extension from on-the-wire data
@type parser: L{Parser}
@rtype: SupportedGroupsExtension
"""
if parser.getRemainingLength() == 0:
self.groups = None
return self
self.groups = []
parser.startLengthCheck(2)
while not parser.atLengthCheck():
self.groups.append(parser.get(2))
parser.stopLengthCheck()
return self
class ECPointFormatsExtension(TLSExtension):
"""
Client side list of supported ECC point formats.
See RFC4492.
"""
def __init__(self):
"""Create instance of class"""
self.formats = None
@property
def extType(self):
"""
Type of extension, in this case - 11
@rtype: int
"""
return ExtensionType.ec_point_formats
@property
def extData(self):
"""
Return raw encoding of the extension
@rtype: bytearray
"""
if self.formats is None:
return bytearray(0)
writer = Writer()
# the length is number of formats encoded in one byte
writer.add(len(self.formats), 1)
for fmt in self.formats:
writer.add(fmt, 1)
return writer.bytes
def create(self, formats):
"""
Set the list of supported EC point formats
@type formats: list of int
@param formats: list of supported EC point formats
"""
self.formats = formats
return self
def parse(self, parser):
"""
Deserialise extension from on the wire data
@type parser: L{Parser}
@rtype: ECPointFormatsExtension
"""
if parser.getRemainingLength() == 0:
self.formats = None
return self
self.formats = []
parser.startLengthCheck(1)
while not parser.atLengthCheck():
self.formats.append(parser.get(1))
parser.stopLengthCheck()
return self
class SignatureAlgorithmsExtension(TLSExtension):
"""
Client side list of supported signature algorithms.
Should be used by server to select certificate and signing method for
Server Key Exchange messages. In practice used only for the latter.
See RFC5246.
"""
def __init__(self):
"""Create instance of class"""
self.sigalgs = None
@property
def extType(self):
"""
Type of extension, in this case - 13
@rtype: int
"""
return ExtensionType.signature_algorithms
@property
def extData(self):
"""
Return raw encoding of the exteion
@rtype: bytearray
"""
if self.sigalgs is None:
return bytearray(0)
writer = Writer()
# elements 1 byte each, overall length encoded in 2 bytes
writer.addVarTupleSeq(self.sigalgs, 1, 2)
return writer.bytes
def create(self, sigalgs):
"""
Set the list of supported algorithm types
@type sigalgs: list of tuples
@param sigalgs: list of pairs of a hash algorithm and signature
algorithm
"""
self.sigalgs = sigalgs
return self
def parse(self, parser):
"""
Deserialise extension from on the wire data
@type parser: L{Parser}
@rtype: SignatureAlgorithmsExtension
"""
if parser.getRemainingLength() == 0:
self.sigalgs = None
return self
self.sigalgs = parser.getVarTupleList(1, 2, 2)
if parser.getRemainingLength() != 0:
raise SyntaxError()
return self
TLSExtension._universalExtensions = \
{
ExtensionType.server_name : SNIExtension,
ExtensionType.cert_type : ClientCertTypeExtension,
ExtensionType.supported_groups : SupportedGroupsExtension,
ExtensionType.ec_point_formats : ECPointFormatsExtension,
ExtensionType.srp : SRPExtension,
ExtensionType.signature_algorithms : SignatureAlgorithmsExtension,
ExtensionType.supports_npn : NPNExtension}
TLSExtension._serverExtensions = \
{
ExtensionType.cert_type : ServerCertTypeExtension,
ExtensionType.tack : TACKExtension}
|