Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

digestMd5.py 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. """
  2. """
  3. # Created on 2014.01.04
  4. #
  5. # Author: Giovanni Cannata
  6. #
  7. # Copyright 2014 - 2018 Giovanni Cannata
  8. #
  9. # This file is part of ldap3.
  10. #
  11. # ldap3 is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU Lesser General Public License as published
  13. # by the Free Software Foundation, either version 3 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # ldap3 is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU Lesser General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU Lesser General Public License
  22. # along with ldap3 in the COPYING and COPYING.LESSER files.
  23. # If not, see <http://www.gnu.org/licenses/>.
  24. from binascii import hexlify
  25. import hashlib
  26. import hmac
  27. from ... import SEQUENCE_TYPES
  28. from ...protocol.sasl.sasl import abort_sasl_negotiation, send_sasl_negotiation, random_hex_string
  29. STATE_KEY = 0
  30. STATE_VALUE = 1
  31. def md5_h(value):
  32. if not isinstance(value, bytes):
  33. value = value.encode()
  34. return hashlib.md5(value).digest()
  35. def md5_kd(k, s):
  36. if not isinstance(k, bytes):
  37. k = k.encode()
  38. if not isinstance(s, bytes):
  39. s = s.encode()
  40. return md5_h(k + b':' + s)
  41. def md5_hex(value):
  42. if not isinstance(value, bytes):
  43. value = value.encode()
  44. return hexlify(value)
  45. def md5_hmac(k, s):
  46. if not isinstance(k, bytes):
  47. k = k.encode()
  48. if not isinstance(s, bytes):
  49. s = s.encode()
  50. return hmac.new(k, s).hexdigest()
  51. def sasl_digest_md5(connection, controls):
  52. # sasl_credential must be a tuple made up of the following elements: (realm, user, password, authorization_id)
  53. # if realm is None will be used the realm received from the server, if available
  54. if not isinstance(connection.sasl_credentials, SEQUENCE_TYPES) or not len(connection.sasl_credentials) == 4:
  55. return None
  56. # step One of RFC2831
  57. result = send_sasl_negotiation(connection, controls, None)
  58. if 'saslCreds' in result and result['saslCreds'] is not None:
  59. server_directives = decode_directives(result['saslCreds'])
  60. else:
  61. return None
  62. if 'realm' not in server_directives or 'nonce' not in server_directives or 'algorithm' not in server_directives: # mandatory directives, as per RFC2831
  63. abort_sasl_negotiation(connection, controls)
  64. return None
  65. # step Two of RFC2831
  66. charset = server_directives['charset'] if 'charset' in server_directives and server_directives['charset'].lower() == 'utf-8' else 'iso8859-1'
  67. user = connection.sasl_credentials[1].encode(charset)
  68. realm = (connection.sasl_credentials[0] if connection.sasl_credentials[0] else (server_directives['realm'] if 'realm' in server_directives else '')).encode(charset)
  69. password = connection.sasl_credentials[2].encode(charset)
  70. authz_id = connection.sasl_credentials[3].encode(charset) if connection.sasl_credentials[3] else b''
  71. nonce = server_directives['nonce'].encode(charset)
  72. cnonce = random_hex_string(16).encode(charset)
  73. uri = b'ldap/'
  74. qop = b'auth'
  75. digest_response = b'username="' + user + b'",'
  76. digest_response += b'realm="' + realm + b'",'
  77. digest_response += (b'authzid="' + authz_id + b'",') if authz_id else b''
  78. digest_response += b'nonce="' + nonce + b'",'
  79. digest_response += b'cnonce="' + cnonce + b'",'
  80. digest_response += b'digest-uri="' + uri + b'",'
  81. digest_response += b'qop=' + qop + b','
  82. digest_response += b'nc=00000001' + b','
  83. if charset == 'utf-8':
  84. digest_response += b'charset="utf-8",'
  85. a0 = md5_h(b':'.join([user, realm, password]))
  86. a1 = b':'.join([a0, nonce, cnonce, authz_id]) if authz_id else b':'.join([a0, nonce, cnonce])
  87. a2 = b'AUTHENTICATE:' + uri + (':00000000000000000000000000000000' if qop in [b'auth-int', b'auth-conf'] else b'')
  88. digest_response += b'response="' + md5_hex(md5_kd(md5_hex(md5_h(a1)), b':'.join([nonce, b'00000001', cnonce, qop, md5_hex(md5_h(a2))]))) + b'"'
  89. result = send_sasl_negotiation(connection, controls, digest_response)
  90. return result
  91. def decode_directives(directives_string):
  92. """
  93. converts directives to dict, unquote values
  94. """
  95. # old_directives = dict((attr[0], attr[1].strip('"')) for attr in [line.split('=') for line in directives_string.split(',')])
  96. state = STATE_KEY
  97. tmp_buffer = ''
  98. quoting = False
  99. key = ''
  100. directives = dict()
  101. for c in directives_string.decode('utf-8'):
  102. if state == STATE_KEY and c == '=':
  103. key = tmp_buffer
  104. tmp_buffer = ''
  105. state = STATE_VALUE
  106. elif state == STATE_VALUE and c == '"' and not quoting and not tmp_buffer:
  107. quoting = True
  108. elif state == STATE_VALUE and c == '"' and quoting:
  109. quoting = False
  110. elif state == STATE_VALUE and c == ',' and not quoting:
  111. directives[key] = tmp_buffer
  112. tmp_buffer = ''
  113. key = ''
  114. state = STATE_KEY
  115. else:
  116. tmp_buffer += c
  117. if key and tmp_buffer:
  118. directives[key] = tmp_buffer
  119. return directives