* sasl.el (sasl-scram-md5-make-salted-pass): New function.
[elisp/flim.git] / digest-md5.el
1 ;;; digest-md5.el --- Compute DIGEST-MD5.
2
3 ;; Copyright (C) 1999 Kenichi OKADA
4
5 ;; Author: Kenichi OKADA <okada@opaopa.org>
6 ;; Keywords: DIGEST-MD5, HMAC-MD5, SASL, IMAP, POP, ACAP
7
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or
13 ;; (at your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; NOW BUILDING.
28
29 ;; This program is implemented from draft-leach-digest-sasl-05.txt.
30 ;;
31 ;; It is caller's responsibility to base64-decode challenges and
32 ;; base64-encode responses in IMAP4 AUTHENTICATE command.
33 ;;
34 ;; Passphrase should be longer than 16 bytes. (See RFC 2195)
35
36 ;; Examples.
37 ;;
38 ;; (digest-md5-digest-response "chris" "elwood.innosoft.com"
39 ;;                        "OA6MG9tEQGm2hh" "OA6MHXh6VqTrRk"
40 ;;                        "imap/elwood.innosoft.com"
41 ;;                        "d388dad90d4bbd760a152321f2143af7"
42 ;;                        1 "auth" nil "utf-8")
43 ;; => "charset=utf-8,username=\"chris\",realm=\"elwood.innosoft.com\",nonce=\"OA6MG9tEQGm2hh\",nc=00000001,cnonce=\"OA6MHXh6VqTrRk\",digest-uri=\"imap/elwood.innosoft.com\",response=d388dad90d4bbd760a152321f2143af7,qop=auth"
44 ;;
45
46 ;;; Code:
47
48 (require 'hmac-md5)
49 (require 'unique-id)
50
51 (defun digest-md5-parse-digest-challenge (digest-challenge)
52   ;; return list of 
53   ;; (realm nonce qop-options stale maxbuf charset 
54   ;; algorithm cipher-opts auth-param).
55   (let (realm nonce qop-options stale maxbuf charset 
56               algorithm cipher-opts auth-param
57               challenges challenge)
58     (setq challenges
59           (split-string digest-challenge ","))
60     (while (car challenges)
61       (if (null (string-match
62                  "\\([a-z]+\\)=\"?\\(.+\\)\"?" (car challenges)))
63           (error "Parse error in digest-challenge1."))
64       (setq challenge (cons
65                        (match-string 1 (car challenges))
66                        (match-string 2 (car challenges))))
67       (cond
68        ((string= (car challenge) "realm")
69         (setq realm (cdr challenge)))
70        ((string= (car challenge) "nonce")
71         (setq nonce (cdr challenge)))
72        ((string= (car challenge) "qop")
73         (setq qop-options  (cdr challenge)))
74        ((string= (car challenge) "stale")
75         (setq stale (cdr challenge)))
76        ((string= (car challenge) "maxbuf")
77         (setq maxbuf (cdr challenge)))
78        ((string= (car challenge) "charset")
79         (setq charset (cdr challenge)))
80        ((string= (car challenge) "algorithm")
81         (setq algorithm (cdr challenge)))
82        ((string= (car challenge) "cipher")
83         (setq cipher-opts (cdr challenge)))
84        (t
85         (error "Parse error in digest-challenge.")))
86   (setq challenges (cdr challenges)))
87     (list realm nonce qop-options stale maxbuf charset 
88           algorithm cipher-opts auth-param)))
89
90 (defun digest-md5-digest-uri (serv-type host &optional serv-name)
91   (concat serv-type "/" host
92           (if (and serv-name
93                    (null (string= host serv-name)))
94               (concat "/" serv-name))))
95
96 (defun digest-md5-cnonce ()
97   ;; It is RECOMMENDED that it 
98   ;; contain at least 64 bits of entropy.
99   (concat (unique-id-m "") (unique-id-m "")))
100
101 (defun digest-md5-digest-response (username 
102                                    realm nonce cnonce
103                                    digest-uri response 
104                                    &optional nonce-count qop 
105                                    maxbuf charset cipher authzid)
106   (concat
107    (if charset
108        (concat "charset=" charset ","))
109    "username=\"" username "\""
110    ",realm=\"" realm "\""
111    ",nonce=\"" nonce "\""
112    (format ",nc=%08x"
113            (or nonce-count 1))
114    ",cnonce=\"" cnonce "\""
115    ",digest-uri=\"" digest-uri "\""
116    ",response=" response
117    (if qop
118        (concat ",qop=" qop))
119    (if maxbuf
120        (concat ",maxbuf=" maxbuf))
121    (if cipher
122        (concat ",cipher=" cipher))
123    (if authzid
124        (concat ",authzid=\"" authzid "\""))))
125
126   
127 (provide 'digest-md5)
128
129 ;;; digest-md5.el ends here