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