1 ;;; sasl-digest.el --- DIGEST-MD5 module for the SASL client framework
3 ;; Copyright (C) 2000 Free Software Foundation, Inc.
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Kenichi OKADA <okada@opaopa.org>
7 ;; Keywords: SASL, DIGEST-MD5
9 ;; This file is part of FLIM (Faithful Library about Internet Message).
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;; This program is implemented from draft-leach-digest-sasl-05.txt.
28 ;; It is caller's responsibility to base64-decode challenges and
29 ;; base64-encode responses in IMAP4 AUTHENTICATE command.
31 ;; Passphrase should be longer than 16 bytes. (See RFC 2195)
38 (defvar sasl-digest-md5-nonce-count 1)
39 (defvar sasl-digest-md5-unique-id-function
40 sasl-unique-id-function)
42 (defvar sasl-digest-md5-syntax-table
43 (let ((table (make-syntax-table)))
44 (modify-syntax-entry ?= "." table)
45 (modify-syntax-entry ?, "." table)
47 "A syntax table for parsing digest-challenge attributes.")
49 (defconst sasl-digest-md5-steps
50 '(ignore ;no initial response
51 sasl-digest-md5-response
54 (defun sasl-digest-md5-parse-string (string)
55 "Parse STRING and return a property list.
56 The value is a cons cell of the form \(realm nonce qop-options stale maxbuf
57 charset algorithm cipher-opts auth-param)."
59 (set-syntax-table sasl-digest-md5-syntax-table)
62 (goto-char (point-min))
64 (while (progn (forward-sexp) (not (eobp)))
68 (read (point-min-marker)))))
70 (defun sasl-digest-md5-digest-uri (serv-type host &optional serv-name)
71 (concat serv-type "/" host
73 (not (string= host serv-name)))
74 (concat "/" serv-name))))
76 (defun sasl-digest-md5-cnonce ()
77 (let ((sasl-unique-id-function sasl-digest-md5-unique-id-function))
80 (defun sasl-digest-md5-response-value (username
90 (format "DIGEST-MD5 passphrase for %s: "
97 (md5-binary (concat (md5-binary
98 (concat username ":" realm ":" passphrase))
101 (concat ":" authzid)))))
103 ":" (format "%08x" nonce-count) ":" cnonce ":" qop ":"
106 (concat "AUTHENTICATE:" digest-uri
107 (if (member qop '("auth-int" "auth-conf"))
108 ":00000000000000000000000000000000")))))))
109 (fillarray passphrase 0))))
111 (defun sasl-digest-md5-response (client step)
113 (sasl-digest-md5-parse-string (sasl-step-data step)))
115 (or (sasl-client-property client 'realm)
116 (plist-get plist 'realm))) ;need to check
118 (or (sasl-client-property client 'nonce-count)
119 sasl-digest-md5-nonce-count))
121 (or (sasl-client-property client 'qop)
124 (sasl-digest-md5-digest-uri
125 (sasl-client-service client)(sasl-client-server client)))
127 (or (sasl-client-property client 'cnonce)
128 (sasl-digest-md5-cnonce))))
129 (sasl-client-set-property client 'nonce-count (1+ nonce-count))
130 (unless (string= qop "auth")
131 (sasl-error (format "Unsupported \"qop-value\": %s" qop)))
133 "username=\"" (sasl-client-name client) "\","
134 "realm=\"" realm "\","
135 "nonce=\"" (plist-get plist 'nonce) "\","
136 "cnonce=\"" cnonce "\","
137 (format "nc=%08x," nonce-count)
138 "digest-uri=\"" digest-uri "\","
141 (sasl-digest-md5-response-value
142 (sasl-client-name client)
144 (plist-get plist 'nonce)
149 (plist-get plist 'authzid)))))
151 (put 'sasl-digest 'sasl-mechanism
152 (sasl-make-mechanism "DIGEST-MD5" sasl-digest-md5-steps))
154 (provide 'sasl-digest)
156 ;;; sasl-digest.el ends here