(base64-encode-region): Allow the optional second arg `no-line-break'.
[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 (require 'scram-md5)
51
52 ;;; CRAM-MD5
53 (defun sasl-cram-md5 (username passphrase challenge)
54   (let ((secure-word (copy-sequence passphrase)))
55     (setq secure-word (unwind-protect
56                           (hmac-md5 challenge secure-word)
57                         (fillarray secure-word 0))
58           secure-word (unwind-protect
59                           (encode-hex-string secure-word)
60                         (fillarray secure-word 0))
61           secure-word (unwind-protect
62                           (concat username " " secure-word)
63                         (fillarray secure-word 0)))))
64
65 ;;; PLAIN
66 (defun sasl-plain (authorid authenid passphrase)
67   (concat authorid "\0" authenid "\0" passphrase))
68
69 ;;; SCRAM-MD5
70 (defvar sasl-scram-md5-client-security-info
71   (scram-make-security-info nil t 0))
72
73 (defun sasl-scram-md5-make-salted-pass (server-msg-1 passphrase)
74   (scram-md5-make-salted-pass
75    passphrase
76    (car
77     (scram-md5-parse-server-msg-1 server-msg-1))))
78
79 (defun sasl-scram-md5-client-msg-1 (authenticate-id &optional authorize-id)
80   (scram-md5-make-client-msg-1 authenticate-id authorize-id))
81
82 (defun sasl-scram-md5-client-msg-2 (server-msg-1 client-msg-1 salted-pass)
83   (let (client-proof client-key shared-key client-verifier)
84     (setq client-key
85           (scram-md5-make-client-key salted-pass))
86     (setq client-verifier
87           (scram-md5-make-client-verifier client-key))
88     (setq shared-key
89           (unwind-protect
90               (scram-md5-make-shared-key
91                server-msg-1
92                client-msg-1
93                sasl-scram-md5-client-security-info
94                client-verifier)
95             (fillarray client-verifier 0)))
96     (setq client-proof
97           (unwind-protect
98               (scram-md5-make-client-proof
99                client-key shared-key)
100             (fillarray client-key 0)
101             (fillarray shared-key 0)))
102     (unwind-protect
103         (scram-md5-make-client-msg-2
104          sasl-scram-md5-client-security-info
105          client-proof)
106       (fillarray client-proof 0))))
107              
108 (defun sasl-scram-md5-authenticate-server (server-msg-1 
109                                            server-msg-2
110                                            client-msg-1
111                                            salted-pass)
112   (string= server-msg-2
113            (scram-md5-make-server-msg-2
114             server-msg-1
115             client-msg-1
116             sasl-scram-md5-client-security-info
117             (car
118              (scram-md5-parse-server-msg-1 server-msg-1))
119             salted-pass)))
120
121 (provide 'sasl)
122
123 ;;; sasl.el ends here