sasl-digest.el 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. ;;; sasl-digest.el --- DIGEST-MD5 module for the SASL client framework
  2. ;; Copyright (C) 2000, 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: Daiki Ueno <ueno@unixuser.org>
  4. ;; Kenichi OKADA <okada@opaopa.org>
  5. ;; Keywords: SASL, DIGEST-MD5
  6. ;; Package: sasl
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This program is implemented from draft-leach-digest-sasl-05.txt.
  20. ;;
  21. ;; It is caller's responsibility to base64-decode challenges and
  22. ;; base64-encode responses in IMAP4 AUTHENTICATE command.
  23. ;;
  24. ;; Passphrase should be longer than 16 bytes. (See RFC 2195)
  25. ;;; Commentary:
  26. (require 'sasl)
  27. (require 'hmac-md5)
  28. (defvar sasl-digest-md5-nonce-count 1)
  29. (defvar sasl-digest-md5-unique-id-function
  30. sasl-unique-id-function)
  31. (defvar sasl-digest-md5-syntax-table
  32. (let ((table (make-syntax-table)))
  33. (modify-syntax-entry ?= "." table)
  34. (modify-syntax-entry ?, "." table)
  35. table)
  36. "A syntax table for parsing digest-challenge attributes.")
  37. (defconst sasl-digest-md5-steps
  38. '(ignore ;no initial response
  39. sasl-digest-md5-response
  40. ignore)) ;""
  41. (defun sasl-digest-md5-parse-string (string)
  42. "Parse STRING and return a property list.
  43. The value is a cons cell of the form \(realm nonce qop-options stale maxbuf
  44. charset algorithm cipher-opts auth-param)."
  45. (with-temp-buffer
  46. (set-syntax-table sasl-digest-md5-syntax-table)
  47. (save-excursion
  48. (insert string)
  49. (goto-char (point-min))
  50. (insert "(")
  51. (while (progn (forward-sexp) (not (eobp)))
  52. (delete-char 1)
  53. (insert " "))
  54. (insert ")")
  55. (read (point-min-marker)))))
  56. (defun sasl-digest-md5-digest-uri (serv-type host &optional serv-name)
  57. (concat serv-type "/" host
  58. (if (and serv-name
  59. (not (string= host serv-name)))
  60. (concat "/" serv-name))))
  61. (defun sasl-digest-md5-cnonce ()
  62. (let ((sasl-unique-id-function sasl-digest-md5-unique-id-function))
  63. (sasl-unique-id)))
  64. (defun sasl-digest-md5-response-value (username
  65. realm
  66. nonce
  67. cnonce
  68. nonce-count
  69. qop
  70. digest-uri
  71. authzid)
  72. (let ((passphrase
  73. (sasl-read-passphrase
  74. (format "DIGEST-MD5 passphrase for %s: "
  75. username))))
  76. (unwind-protect
  77. (encode-hex-string
  78. (md5-binary
  79. (concat
  80. (encode-hex-string
  81. (md5-binary (concat (md5-binary
  82. (concat username ":" realm ":" passphrase))
  83. ":" nonce ":" cnonce
  84. (if authzid
  85. (concat ":" authzid)))))
  86. ":" nonce
  87. ":" (format "%08x" nonce-count) ":" cnonce ":" qop ":"
  88. (encode-hex-string
  89. (md5-binary
  90. (concat "AUTHENTICATE:" digest-uri
  91. (if (member qop '("auth-int" "auth-conf"))
  92. ":00000000000000000000000000000000")))))))
  93. (fillarray passphrase 0))))
  94. (defun sasl-digest-md5-response (client step)
  95. (let* ((plist
  96. (sasl-digest-md5-parse-string (sasl-step-data step)))
  97. (realm
  98. (or (sasl-client-property client 'realm)
  99. (plist-get plist 'realm))) ;need to check
  100. (nonce-count
  101. (or (sasl-client-property client 'nonce-count)
  102. sasl-digest-md5-nonce-count))
  103. (qop
  104. (or (sasl-client-property client 'qop)
  105. "auth"))
  106. (digest-uri
  107. (sasl-digest-md5-digest-uri
  108. (sasl-client-service client)(sasl-client-server client)))
  109. (cnonce
  110. (or (sasl-client-property client 'cnonce)
  111. (sasl-digest-md5-cnonce))))
  112. (sasl-client-set-property client 'nonce-count (1+ nonce-count))
  113. (unless (string= qop "auth")
  114. (sasl-error (format "Unsupported \"qop-value\": %s" qop)))
  115. (concat
  116. "username=\"" (sasl-client-name client) "\","
  117. "realm=\"" realm "\","
  118. "nonce=\"" (plist-get plist 'nonce) "\","
  119. "cnonce=\"" cnonce "\","
  120. (format "nc=%08x," nonce-count)
  121. "digest-uri=\"" digest-uri "\","
  122. "qop=" qop ","
  123. "response="
  124. (sasl-digest-md5-response-value
  125. (sasl-client-name client)
  126. realm
  127. (plist-get plist 'nonce)
  128. cnonce
  129. nonce-count
  130. qop
  131. digest-uri
  132. (plist-get plist 'authzid)))))
  133. (put 'sasl-digest 'sasl-mechanism
  134. (sasl-make-mechanism "DIGEST-MD5" sasl-digest-md5-steps))
  135. (provide 'sasl-digest)
  136. ;;; sasl-digest.el ends here