TLS1.3 extension for handling key negotiation.
| 108 | |
| 109 | |
| 110 | class KeyShareExtension(tlslite.extensions.TLSExtension): |
| 111 | """TLS1.3 extension for handling key negotiation.""" |
| 112 | |
| 113 | def __init__(self): |
| 114 | """Create key share extension object.""" |
| 115 | super(KeyShareExtension, self).__init__( |
| 116 | extType=ExtensionType.key_share) |
| 117 | self.client_shares = None |
| 118 | |
| 119 | def create(self, shares): |
| 120 | """ |
| 121 | Set the list of key shares to send. |
| 122 | |
| 123 | @type shares: list of tuples |
| 124 | @param shares: a list of tuples where the first element is a NamedGroup |
| 125 | ID while the second element in a tuple is an opaque bytearray encoding |
| 126 | of the key share. |
| 127 | """ |
| 128 | self.client_shares = shares |
| 129 | return self |
| 130 | |
| 131 | @property |
| 132 | def extData(self): |
| 133 | """Serialise the extension.""" |
| 134 | if self.client_shares is None: |
| 135 | return bytearray(0) |
| 136 | |
| 137 | writer = Writer() |
| 138 | for group_id, share in self.client_shares: |
| 139 | writer.add(group_id, 2) |
| 140 | if group_id in GroupName.allFF: |
| 141 | share_length_length = 2 |
| 142 | else: |
| 143 | share_length_length = 1 |
| 144 | writer.addVarSeq(share, 1, share_length_length) |
| 145 | ext_writer = Writer() |
| 146 | ext_writer.add(len(writer.bytes), 2) |
| 147 | ext_writer.bytes += writer.bytes |
| 148 | return ext_writer.bytes |
| 149 | |
| 150 | def parse(self, parser): |
| 151 | """Deserialise the extension.""" |
| 152 | if parser.getRemainingLength() == 0: |
| 153 | self.client_shares = None |
| 154 | return |
| 155 | |
| 156 | self.client_shares = [] |
| 157 | |
| 158 | parser.startLengthCheck(2) |
| 159 | while not parser.atLengthCheck(): |
| 160 | group_id = parser.get(2) |
| 161 | if group_id in GroupName.allFF: |
| 162 | share_length_length = 2 |
| 163 | else: |
| 164 | share_length_length = 1 |
| 165 | share = parser.getVarBytes(share_length_length) |
| 166 | self.client_shares.append((group_id, share)) |
| 167 |
no outgoing calls