Import Oort Gnus v0.16.
[elisp/gnus.git-] / lisp / canlock.el
1 ;;; canlock.el --- functions for Cancel-Lock feature
2
3 ;; Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc.
4
5 ;; Author: Katsumi Yamaoka <yamaoka@jpl.org>
6 ;; Keywords: news, cancel-lock, hmac, sha1, rfc2104
7
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program; see the file COPYING.  If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;; Canlock is a library for generating and verifying Cancel-Lock and/or
26 ;; Cancel-Key header in news articles.  This is used to protect articles
27 ;; from rogue cancel, supersede or replace attacks.  The method is based
28 ;; on draft-ietf-usefor-cancel-lock-01.txt which was released on November
29 ;; 3rd 1998.  For instance, you can add Cancel-Lock (and possibly Cancel-
30 ;; Key) header in a news article by using a hook which will be evaluated
31 ;; just before sending an article as follows:
32 ;;
33 ;; (add-hook '*e**a*e-header-hook 'canlock-insert-header t)
34 ;;
35 ;; Verifying Cancel-Lock is mainly a function of news servers, however,
36 ;; you can verify your own article using the command `canlock-verify' in
37 ;; the (raw) article buffer.  You will be prompted for the password for
38 ;; each time if the option `canlock-password' or `canlock-password-for-
39 ;; verify' is nil.  Note that setting these options is a bit unsafe.
40
41 ;;; Code:
42
43 (eval-when-compile
44   (require 'cl))
45
46 (autoload 'sha1-binary "sha1-el")
47 (autoload 'base64-encode-string "base64")
48
49 (defgroup canlock nil
50   "The Cancel-Lock feature."
51   :group 'applications)
52
53 (defcustom canlock-sha1-function 'sha1-binary
54   "Function to call to make a SHA-1 message digest."
55   :type '(radio (function-item sha1-binary)
56                 (function-item canlock-sha1-with-openssl)
57                 (function :tag "Other"))
58   :group 'canlock)
59
60 (defcustom canlock-sha1-function-for-verify canlock-sha1-function
61   "Function to call to make a SHA-1 message digest for verifying."
62   :type '(radio (function-item sha1-binary)
63                 (function-item canlock-sha1-with-openssl)
64                 (function :tag "Other"))
65   :group 'canlock)
66
67 (defcustom canlock-openssl-program "openssl"
68   "Name of OpenSSL program."
69   :type 'string
70   :group 'canlock)
71
72 (defcustom canlock-openssl-args '("sha1")
73   "Arguments passed to the OpenSSL program."
74   :type 'sexp
75   :group 'canlock)
76
77 (defcustom canlock-ignore-errors nil
78   "If non-nil, ignore any error signals."
79   :type 'boolean
80   :group 'canlock)
81
82 (defcustom canlock-password nil
83   "Password to use when signing a Cancel-Lock or a Cancel-Key header."
84   :type 'string
85   :group 'canlock)
86
87 (defcustom canlock-password-for-verify canlock-password
88   "Password to use when verifying a Cancel-Lock or a Cancel-Key header."
89   :type 'string
90   :group 'canlock)
91
92 (defcustom canlock-force-insert-header nil
93   "If non-nil, insert a Cancel-Lock or a Cancel-Key header even if the
94 buffer does not look like a news message."
95   :type 'boolean
96   :group 'canlock)
97
98 (defun canlock-sha1-with-openssl (message)
99   "Make a SHA-1 digest of MESSAGE using OpenSSL."
100   (let (default-enable-multibyte-characters)
101     (with-temp-buffer
102       (let ((coding-system-for-read 'binary)
103             (coding-system-for-write 'binary)
104             selective-display
105             (case-fold-search t))
106         (insert message)
107         (apply 'call-process-region (point-min) (point-max)
108                canlock-openssl-program t t nil canlock-openssl-args)
109         (goto-char (point-min))
110         (insert "\"")
111         (while (re-search-forward "\\([0-9a-f][0-9a-f]\\)" nil t)
112           (replace-match "\\\\x\\1"))
113         (insert "\"")
114         (goto-char (point-min))
115         (read (current-buffer))))))
116
117 (eval-when-compile
118   (defmacro canlock-string-as-unibyte (string)
119     "Return a unibyte string with the same individual bytes as STRING."
120     (if (fboundp 'string-as-unibyte)
121         (list 'string-as-unibyte string)
122       string)))
123
124 (defun canlock-sha1 (message)
125   "Make a SHA-1 digest of MESSAGE as a unibyte string of length 20 bytes."
126   (canlock-string-as-unibyte (funcall canlock-sha1-function message)))
127
128 (defun canlock-make-cancel-key (message-id password)
129   "Make a Cancel-Key header."
130   (when (> (length password) 20)
131     (setq password (canlock-sha1 password)))
132   (setq password (concat password (make-string (- 64 (length password)) 0)))
133   (let ((ipad (mapconcat (lambda (byte)
134                            (char-to-string (logxor 54 byte)))
135                          password ""))
136         (opad (mapconcat (lambda (byte)
137                            (char-to-string (logxor 92 byte)))
138                          password "")))
139     (base64-encode-string
140      (canlock-sha1
141       (concat opad
142               (canlock-sha1
143                (concat ipad (canlock-string-as-unibyte message-id))))))))
144
145 (defun canlock-narrow-to-header ()
146   "Narrow the buffer to the head of the message."
147   (let (case-fold-search)
148     (narrow-to-region
149      (goto-char (point-min))
150      (goto-char (if (re-search-forward
151                      (format "^$\\|^%s$"
152                              (regexp-quote mail-header-separator))
153                      nil t)
154                     (match-beginning 0)
155                   (point-max))))))
156
157 (defun canlock-delete-headers ()
158   "Delete Cancel-Key or Cancel-Lock headers in the narrowed buffer."
159   (let ((case-fold-search t))
160     (goto-char (point-min))
161     (while (re-search-forward "^Cancel-\\(Key\\|Lock\\):" nil t)
162       (delete-region (match-beginning 0)
163                      (if (re-search-forward "^[^\t ]" nil t)
164                          (goto-char (match-beginning 0))
165                        (point-max))))))
166
167 (defun canlock-fetch-fields (&optional key)
168   "Return a list of the values of Cancel-Lock header.
169 If KEY is non-nil, look for a Cancel-Key header instead.  The buffer
170 is expected to be narrowed to just the headers of the message."
171   (let ((field (mail-fetch-field (if key "Cancel-Key" "Cancel-Lock")))
172         fields rest
173         (case-fold-search t))
174     (when field
175       (setq fields (split-string field "[\t\n\r ,]+"))
176       (while fields
177         (when (string-match "^sha1:" (setq field (pop fields)))
178           (push (substring field 5) rest)))
179       (nreverse rest))))
180
181 (defun canlock-fetch-id-for-key ()
182   "Return a Message-ID in Cancel, Supersedes or Replaces header.
183 The buffer is expected to be narrowed to just the headers of the
184 message."
185   (or (let ((cancel (mail-fetch-field "Control")))
186         (and cancel
187              (string-match "^cancel[\t ]+\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
188                            cancel)
189              (match-string 1 cancel)))
190       (mail-fetch-field "Supersedes")
191       (mail-fetch-field "Replaces")))
192
193 ;;;###autoload
194 (defun canlock-insert-header (&optional id-for-key id-for-lock password)
195   "Insert a Cancel-Key and/or a Cancel-Lock header if possible."
196   (let (news control key-for-key key-for-lock)
197     (save-excursion
198       (save-restriction
199         (canlock-narrow-to-header)
200         (when (setq news (or canlock-force-insert-header
201                              (mail-fetch-field "Newsgroups")))
202           (unless id-for-key
203             (setq id-for-key (canlock-fetch-id-for-key)))
204           (if (and (setq control (mail-fetch-field "Control"))
205                    (string-match
206                     "^cancel[\t ]+\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
207                     control))
208               (setq id-for-lock nil)
209             (unless id-for-lock
210               (setq id-for-lock (mail-fetch-field "Message-ID"))))
211           (canlock-delete-headers)
212           (goto-char (point-max))))
213       (when news
214         (if (not (or id-for-key id-for-lock))
215             (message "There are no Message-ID(s)")
216           (unless password
217             (setq password (or canlock-password
218                                (read-passwd
219                                 "Password for Canlock: "))))
220           (if (or (not (stringp password)) (zerop (length password)))
221               (message "Password for Canlock is bad")
222             (setq key-for-key (when id-for-key
223                                 (canlock-make-cancel-key
224                                  id-for-key password))
225                   key-for-lock (when id-for-lock
226                                  (canlock-make-cancel-key
227                                   id-for-lock password)))
228             (if (not (or key-for-key key-for-lock))
229                 (message "Couldn't insert Canlock header")
230               (when key-for-key
231                 (insert "Cancel-Key: sha1:" key-for-key "\n"))
232               (when key-for-lock
233                 (insert "Cancel-Lock: sha1:"
234                         (base64-encode-string (canlock-sha1 key-for-lock))
235                         "\n")))))))))
236
237 ;;;###autoload
238 (defun canlock-verify (&optional buffer)
239   "Verify Cancel-Lock or Cancel-Key in BUFFER.
240 If BUFFER is nil, the current buffer is assumed.  Signal an error if
241 it fails.  You can modify the behavior of this function to return non-
242 nil instead of to signal an error by setting the option
243 `canlock-ignore-errors' to non-nil."
244   (interactive)
245   (let ((canlock-sha1-function (or canlock-sha1-function-for-verify
246                                    canlock-sha1-function))
247         keys locks errmsg id-for-key id-for-lock password
248         key-for-key key-for-lock match)
249     (save-excursion
250       (when buffer
251         (set-buffer buffer))
252       (save-restriction
253         (widen)
254         (canlock-narrow-to-header)
255         (setq keys (canlock-fetch-fields 'key)
256               locks (canlock-fetch-fields))
257         (if (not (or keys locks))
258             (setq errmsg
259                   "There are neither Cancel-Lock nor Cancel-Key headers")
260           (setq id-for-key (canlock-fetch-id-for-key)
261                 id-for-lock (mail-fetch-field "Message-ID"))
262           (or id-for-key id-for-lock
263               (setq errmsg "There are no Message-ID(s)")))))
264
265     (if errmsg
266         (if canlock-ignore-errors
267             errmsg
268           (error "%s" errmsg))
269
270       (setq password (or canlock-password-for-verify
271                          (read-passwd "Password for Canlock: ")))
272       (if (or (not (stringp password)) (zerop (length password)))
273           (progn
274             (setq errmsg "Password for Canlock is bad")
275             (if canlock-ignore-errors
276                 errmsg
277               (error "%s" errmsg)))
278
279         (when keys
280           (when id-for-key
281             (setq key-for-key (canlock-make-cancel-key id-for-key password))
282             (while (and keys (not match))
283               (setq match (string-equal key-for-key (pop keys)))))
284           (setq keys (if match "good" "bad")))
285         (setq match nil)
286
287         (when locks
288           (when id-for-lock
289             (setq key-for-lock
290                   (base64-encode-string
291                    (canlock-sha1 (canlock-make-cancel-key id-for-lock
292                                                           password))))
293             (when (and locks (not match))
294               (setq match (string-equal key-for-lock (pop locks)))))
295           (setq locks (if match "good" "bad")))
296
297         (prog1
298             (when (member "bad" (list keys locks))
299               "bad")
300           (cond ((and keys locks)
301                  (message "Cancel-Key is %s, Cancel-Lock is %s" keys locks))
302                 (locks
303                  (message "Cancel-Lock is %s" locks))
304                 (keys
305                  (message "Cancel-Key is %s" keys))))))))
306
307 (provide 'canlock)
308
309 ;;; canlock.el ends here