gnus-art.el (gnus-button-alist): Also support quotes 'like this'
[gnus] / lisp / ntlm.el
index 4df8566..9ab83da 100644 (file)
@@ -1,6 +1,6 @@
 ;;; ntlm.el --- NTLM (NT LanManager) authentication support
 
-;; Copyright (C) 2001, 2007-2014 Free Software Foundation, Inc.
+;; Copyright (C) 2001, 2007-2015 Free Software Foundation, Inc.
 
 ;; Author: Taro Kawagishi <tarok@transpulse.org>
 ;; Keywords: NTLM, SASL
@@ -16,7 +16,7 @@
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
-;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PUR1POSE.  See the
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
@@ -110,8 +110,46 @@ is not given."
   (defmacro ntlm-string-as-unibyte (string)
     (if (fboundp 'string-as-unibyte)
        `(string-as-unibyte ,string)
+      string))
+  (defmacro ntlm-string-make-unibyte (string)
+    (if (fboundp 'string-make-unibyte)
+       `(string-make-unibyte ,string)
       string)))
 
+(eval-and-compile
+  (autoload 'sha1 "sha1")
+  (if (fboundp 'secure-hash)
+      (defalias 'ntlm-secure-hash 'secure-hash)
+    (defun ntlm-secure-hash (algorithm object &optional start end binary)
+      "Return the secure hash of OBJECT, a buffer or string.
+ALGORITHM is a symbol specifying the hash to use: md5, sha1.
+
+The two optional arguments START and END are positions specifying for
+which part of OBJECT to compute the hash.  If nil or omitted, uses the
+whole OBJECT.
+
+If BINARY is non-nil, returns a string in binary form."
+      (cond ((eq algorithm 'md5)
+            (if binary
+                (let* ((hex (md5 object start end))
+                       (len (length hex))
+                       (beg 0)
+                       rest)
+                  (while (< beg len)
+                    (push (ntlm-string-make-unibyte
+                           (char-to-string
+                            (string-to-number
+                             (substring hex beg (setq beg (+ beg 2)))
+                             16)))
+                          rest))
+                  (apply #'concat (nreverse rest)))
+              (md5 object start end)))
+           ((eq algorithm 'sha1)
+            (sha1 object start end binary))
+           (t
+            (error "(ntlm-secure-hash) Unsupported algorithm: %s"
+                   algorithm))))))
+
 (defun ntlm-build-auth-response (challenge user password-hashes)
   "Return the response string to a challenge string CHALLENGE given by
 the NTLM based server for the user USER and the password hash list
@@ -149,7 +187,7 @@ by PASSWORD-HASHES.  PASSWORD-HASHES should be a return value of
        (let (randomString
              sessionHash)
          ;; generate NTLM2 session response data
-         (setq randomString (string-make-unibyte
+         (setq randomString (ntlm-string-make-unibyte
                              (concat
                               (make-string 1 (random 256))
                               (make-string 1 (random 256))
@@ -159,9 +197,9 @@ by PASSWORD-HASHES.  PASSWORD-HASHES should be a return value of
                               (make-string 1 (random 256))
                               (make-string 1 (random 256))
                               (make-string 1 (random 256)))))
-         (setq sessionHash (secure-hash 'md5
-                                        (concat challengeData randomString)
-                                        nil nil t))
+         (setq sessionHash (ntlm-secure-hash
+                            'md5 (concat challengeData randomString)
+                            nil nil t))
          (setq sessionHash (substring sessionHash 0 8))
 
          (setq lmRespData (concat randomString (make-string 16 0)))