Skip to content

Commit fb85654

Browse files
holgermetschulatGIC-de
authored andcommitted
fix for UTF-8 (#167)
1 parent 9b7c823 commit fb85654

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

pyrad/packet.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -710,10 +710,14 @@ def RequestPacket(self):
710710
return header + attr
711711

712712
def PwDecrypt(self, password):
713-
"""Obfuscate a RADIUS password. RADIUS hides passwords in packets by
713+
"""De-Obfuscate a RADIUS password. RADIUS hides passwords in packets by
714714
using an algorithm based on the MD5 hash of the packet authenticator
715715
and RADIUS secret. This function reverses the obfuscation process.
716716
717+
Although RFC2865 does not explicitly state UTF-8 for the password field,
718+
the rest of RFC2865 defines UTF-8 as the encoding expected for the decrypted password.
719+
720+
717721
:param password: obfuscated form of password
718722
:type password: binary string
719723
:return: plaintext password
@@ -729,10 +733,16 @@ def PwDecrypt(self, password):
729733
pw += bytes((hash[i] ^ buf[i],))
730734
(last, buf) = (buf[:16], buf[16:])
731735

736+
# This is safe even with UTF-8 encoding since no valid encoding of UTF-8
737+
# (other than encoding U+0000 NULL) will produce a bytestream containing 0x00 byte.
732738
while pw.endswith(b'\x00'):
733739
pw = pw[:-1]
734740

735-
return pw.decode('utf-8')
741+
# If the shared secret with the client is not the same, then de-obfuscating the password
742+
# field may yield illegal UTF-8 bytes. Therefore, in order not to provoke an Exception here
743+
# (which would be not consistently generated since this will depend on the random data chosen
744+
# by the client) we simply ignore un-parsable UTF-8 sequences.
745+
return pw.decode('utf-8', errors="ignore")
736746

737747
def PwCrypt(self, password):
738748
"""Obfuscate password.

0 commit comments

Comments
 (0)