Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / tests / hwsim / radius_das.py
1 # RADIUS DAS extensions to pyrad
2 # Copyright (c) 2014, Jouni Malinen <j@w1.fi>
3 #
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
6
7 import hashlib
8 import random
9 import struct
10 import pyrad.packet
11
12 class DisconnectPacket(pyrad.packet.Packet):
13     def __init__(self, code=pyrad.packet.DisconnectRequest, id=None,
14                  secret=None, authenticator=None, **attributes):
15         pyrad.packet.Packet.__init__(self, code, id, secret, authenticator,
16                                      **attributes)
17
18     def RequestPacket(self):
19         attr = self._PktEncodeAttributes()
20
21         if self.id is None:
22             self.id = random.randrange(0, 256)
23
24         header = struct.pack('!BBH', self.code, self.id, (20 + len(attr)))
25         self.authenticator = hashlib.md5(header[0:4] + 16 * b'\x00' + attr
26             + self.secret).digest()
27         return header + self.authenticator + attr
28
29 class CoAPacket(pyrad.packet.Packet):
30     def __init__(self, code=pyrad.packet.CoARequest, id=None,
31                  secret=None, authenticator=None, **attributes):
32         pyrad.packet.Packet.__init__(self, code, id, secret, authenticator,
33                                      **attributes)
34
35     def RequestPacket(self):
36         attr = self._PktEncodeAttributes()
37
38         if self.id is None:
39             self.id = random.randrange(0, 256)
40
41         header = struct.pack('!BBH', self.code, self.id, (20 + len(attr)))
42         self.authenticator = hashlib.md5(header[0:4] + 16 * b'\x00' + attr
43             + self.secret).digest()
44         return header + self.authenticator + attr