update.
[elisp/flim.git] / sasl.el
1 ;;; sasl.el --- basic functions for SASL
2
3 ;; Copyright (C) 1995, 1996, 1998, 1999 Free Software Foundation, Inc.
4
5 ;; Author: Kenichi OKADA <okada@opaopa.org>
6 ;; Keywords: SMTP, SASL, RFC2222
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 (at
13 ;; your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; 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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; Example.
28 ;;
29 ;; (base64-encode-string
30 ;;  (sasl-scram-md5-client-msg-2
31 ;;   (base64-decode-string "dGVzdHNhbHQBAAAAaW1hcEBlbGVhbm9yLmlubm9zb2Z0LmNvbQBqaGNOWmxSdVBiemlGcCt2TFYrTkN3")
32 ;;   (base64-decode-string "AGNocmlzADx0NG40UGFiOUhCMEFtL1FMWEI3MmVnQGVsZWFub3IuaW5ub3NvZnQuY29tPg==")
33 ;;   (scram-md5-make-salted-pass
34 ;;    "secret stuff" "testsalt")))
35 ;; => "AQAAAMg9jU8CeB4KOfk7sUhSQPs="
36 ;;
37 ;; (base64-encode-string
38 ;;  (scram-md5-make-server-msg-2
39 ;;   (base64-decode-string "dGVzdHNhbHQBAAAAaW1hcEBlbGVhbm9yLmlubm9zb2Z0LmNvbQBqaGNOWmxSdVBiemlGcCt2TFYrTkN3")
40 ;;   (base64-decode-string "AGNocmlzADx0NG40UGFiOUhCMEFtL1FMWEI3MmVnQGVsZWFub3IuaW5ub3NvZnQuY29tPg==")
41 ;;   (scram-make-security-info nil t 0)
42 ;;   "testsalt"
43 ;;   (scram-md5-make-salted-pass
44 ;;    "secret stuff" "testsalt")))
45 ;; => "U0odqYw3B7XIIW0oSz65OQ=="
46
47 ;;; Code:
48
49 (require 'hmac-md5)
50
51 (eval-when-compile
52   (require 'scram-md5)
53   (require 'digest-md5))
54
55 (eval-and-compile
56   (autoload 'open-ssl-stream "ssl")
57   (autoload 'base64-decode-string "base64")
58   (autoload 'base64-encode-string "base64")
59   (autoload 'starttls-open-stream "starttls")
60   (autoload 'starttls-negotiate "starttls")
61   (autoload 'digest-md5-parse-digest-challenge "digest-md5")
62   (autoload 'digest-md5-digest-response "digest-md5")
63   (autoload 'scram-md5-make-salted-pass "scram-md5")
64   (autoload 'scram-md5-parse-server-msg-1 "scram-md5")
65   (autoload 'scram-md5-make-client-msg-1 "scram-md5"))
66
67 ;;; CRAM-MD5
68 (defun sasl-cram-md5 (username passphrase challenge)
69   (let ((secure-word (copy-sequence passphrase)))
70     (setq secure-word (unwind-protect
71                           (hmac-md5 challenge secure-word)
72                         (fillarray secure-word 0))
73           secure-word (unwind-protect
74                           (encode-hex-string secure-word)
75                         (fillarray secure-word 0))
76           secure-word (unwind-protect
77                           (concat username " " secure-word)
78                         (fillarray secure-word 0)))))
79
80 ;;; PLAIN
81 (defun sasl-plain (authorid authenid passphrase)
82   (concat authorid "\0" authenid "\0" passphrase))
83
84 ;;; SCRAM-MD5
85 (defvar sasl-scram-md5-client-security-info
86   (eval-when-compile
87     (scram-make-security-info nil t 0)))
88
89 (defun sasl-scram-md5-make-salted-pass (server-msg-1 passphrase)
90   (scram-md5-make-salted-pass
91    passphrase
92    (car
93     (scram-md5-parse-server-msg-1 server-msg-1))))
94
95 (defun sasl-scram-md5-client-msg-1 (authenticate-id &optional authorize-id)
96   (scram-md5-make-client-msg-1 authenticate-id authorize-id))
97
98 (defun sasl-scram-md5-client-msg-2 (server-msg-1 client-msg-1 salted-pass)
99   (let (client-proof client-key shared-key client-verifier)
100     (setq client-key
101           (scram-md5-make-client-key salted-pass))
102     (setq client-verifier
103           (scram-md5-make-client-verifier client-key))
104     (setq shared-key
105           (unwind-protect
106               (scram-md5-make-shared-key
107                server-msg-1
108                client-msg-1
109                sasl-scram-md5-client-security-info
110                client-verifier)
111             (fillarray client-verifier 0)))
112     (setq client-proof
113           (unwind-protect
114               (scram-md5-make-client-proof
115                client-key shared-key)
116             (fillarray client-key 0)
117             (fillarray shared-key 0)))
118     (unwind-protect
119         (scram-md5-make-client-msg-2
120          sasl-scram-md5-client-security-info
121          client-proof)
122       (fillarray client-proof 0))))
123              
124 (defun sasl-scram-md5-authenticate-server (server-msg-1
125                                            server-msg-2
126                                            client-msg-1
127                                            salted-pass)
128   (string= server-msg-2
129            (scram-md5-make-server-msg-2
130             server-msg-1
131             client-msg-1
132             sasl-scram-md5-client-security-info
133             (car
134              (scram-md5-parse-server-msg-1 server-msg-1))
135             salted-pass)))
136
137 ;;; DIGEST-MD5
138
139 (defvar sasl-digest-md5-nonce-count 1)
140
141 (defun sasl-digest-md5-digest-response (digest-challenge username passwd
142                                                          serv-type host &optional realm)
143   (digest-md5-parse-digest-challenge digest-challenge)
144   (digest-md5-digest-response
145    username
146    (or realm (digest-md5-challenge 'realm)) ;; need to check.
147    passwd
148    (digest-md5-challenge 'nonce)
149    (digest-md5-cnonce)
150    sasl-digest-md5-nonce-count
151    (digest-md5-digest-uri serv-type host) ;; MX host
152    ))
153
154 (provide 'sasl)
155
156 ;;; sasl.el ends here