* gnus.el: Don't test for `mm-guess-mime-charset'.
[gnus] / lisp / canlock.el
1 ;;; canlock.el --- functions for Cancel-Lock feature
2
3 ;; Copyright (C) 1998, 1999, 2001, 2002, 2003 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 (autoload 'mail-fetch-field "mail-utils")
49 (defvar mail-header-separator)
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-binary)
58                 (function-item canlock-sha1-with-openssl)
59                 (function :tag "Other"))
60   :group 'canlock)
61
62 (defcustom canlock-sha1-function-for-verify canlock-sha1-function
63   "Function to call to make a SHA-1 message digest for verifying."
64   :type '(radio (function-item sha1-binary)
65                 (function-item canlock-sha1-with-openssl)
66                 (function :tag "Other"))
67   :group 'canlock)
68
69 (defcustom canlock-openssl-program "openssl"
70   "Name of OpenSSL program."
71   :type 'string
72   :group 'canlock)
73
74 (defcustom canlock-openssl-args '("sha1")
75   "Arguments passed to the OpenSSL program."
76   :type 'sexp
77   :group 'canlock)
78
79 (defcustom canlock-ignore-errors nil
80   "If non-nil, ignore any error signals."
81   :type 'boolean
82   :group 'canlock)
83
84 (defcustom canlock-password nil
85   "Password to use when signing a Cancel-Lock or a Cancel-Key header."
86   :type 'string
87   :group 'canlock)
88
89 (defcustom canlock-password-for-verify canlock-password
90   "Password to use when verifying a Cancel-Lock or a Cancel-Key header."
91   :type 'string
92   :group 'canlock)
93
94 (defcustom canlock-force-insert-header nil
95   "If non-nil, insert a Cancel-Lock or a Cancel-Key header even if the
96 buffer does not look like a news message."
97   :type 'boolean
98   :group 'canlock)
99
100 (defun canlock-sha1-with-openssl (message)
101   "Make a SHA-1 digest of MESSAGE using OpenSSL."
102   (let (default-enable-multibyte-characters)
103     (with-temp-buffer
104       (let ((coding-system-for-read 'binary)
105             (coding-system-for-write 'binary)
106             selective-display
107             (case-fold-search t))
108         (insert message)
109         (apply 'call-process-region (point-min) (point-max)
110                canlock-openssl-program t t nil canlock-openssl-args)
111         (goto-char (point-min))
112         (insert "\"")
113         (while (re-search-forward "\\([0-9a-f][0-9a-f]\\)" nil t)
114           (replace-match "\\\\x\\1"))
115         (insert "\"")
116         (goto-char (point-min))
117         (read (current-buffer))))))
118
119 (eval-when-compile
120   (defmacro canlock-string-as-unibyte (string)
121     "Return a unibyte string with the same individual bytes as STRING."
122     (if (fboundp 'string-as-unibyte)
123         (list 'string-as-unibyte string)
124       string)))
125
126 (defun canlock-sha1 (message)
127   "Make a SHA-1 digest of MESSAGE as a unibyte string of length 20 bytes."
128   (canlock-string-as-unibyte (funcall canlock-sha1-function message)))
129
130 (defun canlock-make-cancel-key (message-id password)
131   "Make a Cancel-Key header."
132   (when (> (length password) 20)
133     (setq password (canlock-sha1 password)))
134   (setq password (concat password (make-string (- 64 (length password)) 0)))
135   (let ((ipad (mapconcat (lambda (byte)
136                            (char-to-string (logxor 54 byte)))
137                          password ""))
138         (opad (mapconcat (lambda (byte)
139                            (char-to-string (logxor 92 byte)))
140                          password "")))
141     (base64-encode-string
142      (canlock-sha1
143       (concat opad
144               (canlock-sha1
145                (concat ipad (canlock-string-as-unibyte message-id))))))))
146
147 (defun canlock-narrow-to-header ()
148   "Narrow the buffer to the head of the message."
149   (let (case-fold-search)
150     (narrow-to-region
151      (goto-char (point-min))
152      (goto-char (if (re-search-forward
153                      (format "^$\\|^%s$"
154                              (regexp-quote mail-header-separator))
155                      nil t)
156                     (match-beginning 0)
157                   (point-max))))))
158
159 (defun canlock-delete-headers ()
160   "Delete Cancel-Key or Cancel-Lock headers in the narrowed buffer."
161   (let ((case-fold-search t))
162     (goto-char (point-min))
163     (while (re-search-forward "^Cancel-\\(Key\\|Lock\\):" nil t)
164       (delete-region (match-beginning 0)
165                      (if (re-search-forward "^[^\t ]" nil t)
166                          (goto-char (match-beginning 0))
167                        (point-max))))))
168
169 (defun canlock-fetch-fields (&optional key)
170   "Return a list of the values of Cancel-Lock header.
171 If KEY is non-nil, look for a Cancel-Key header instead.  The buffer
172 is expected to be narrowed to just the headers of the message."
173   (let ((field (mail-fetch-field (if key "Cancel-Key" "Cancel-Lock")))
174         fields rest
175         (case-fold-search t))
176     (when field
177       (setq fields (split-string field "[\t\n\r ,]+"))
178       (while fields
179         (when (string-match "^sha1:" (setq field (pop fields)))
180           (push (substring field 5) rest)))
181       (nreverse rest))))
182
183 (defun canlock-fetch-id-for-key ()
184   "Return a Message-ID in Cancel, Supersedes or Replaces header.
185 The buffer is expected to be narrowed to just the headers of the
186 message."
187   (or (let ((cancel (mail-fetch-field "Control")))
188         (and cancel
189              (string-match "^cancel[\t ]+\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
190                            cancel)
191              (match-string 1 cancel)))
192       (mail-fetch-field "Supersedes")
193       (mail-fetch-field "Replaces")))
194
195 ;;;###autoload
196 (defun canlock-insert-header (&optional id-for-key id-for-lock password)
197   "Insert a Cancel-Key and/or a Cancel-Lock header if possible."
198   (let (news control key-for-key key-for-lock)
199     (save-excursion
200       (save-restriction
201         (canlock-narrow-to-header)
202         (when (setq news (or canlock-force-insert-header
203                              (mail-fetch-field "Newsgroups")))
204           (unless id-for-key
205             (setq id-for-key (canlock-fetch-id-for-key)))
206           (if (and (setq control (mail-fetch-field "Control"))
207                    (string-match
208                     "^cancel[\t ]+\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
209                     control))
210               (setq id-for-lock nil)
211             (unless id-for-lock
212               (setq id-for-lock (mail-fetch-field "Message-ID"))))
213           (canlock-delete-headers)
214           (goto-char (point-max))))
215       (when news
216         (if (not (or id-for-key id-for-lock))
217             (message "There are no Message-ID(s)")
218           (unless password
219             (setq password (or canlock-password
220                                (read-passwd
221                                 "Password for Canlock: "))))
222           (if (or (not (stringp password)) (zerop (length password)))
223               (message "Password for Canlock is bad")
224             (setq key-for-key (when id-for-key
225                                 (canlock-make-cancel-key
226                                  id-for-key password))
227                   key-for-lock (when id-for-lock
228                                  (canlock-make-cancel-key
229                                   id-for-lock password)))
230             (if (not (or key-for-key key-for-lock))
231                 (message "Couldn't insert Canlock header")
232               (when key-for-key
233                 (insert "Cancel-Key: sha1:" key-for-key "\n"))
234               (when key-for-lock
235                 (insert "Cancel-Lock: sha1:"
236                         (base64-encode-string (canlock-sha1 key-for-lock))
237                         "\n")))))))))
238
239 ;;;###autoload
240 (defun canlock-verify (&optional buffer)
241   "Verify Cancel-Lock or Cancel-Key in BUFFER.
242 If BUFFER is nil, the current buffer is assumed.  Signal an error if
243 it fails.  You can modify the behavior of this function to return non-
244 nil instead of to signal an error by setting the option
245 `canlock-ignore-errors' to non-nil."
246   (interactive)
247   (let ((canlock-sha1-function (or canlock-sha1-function-for-verify
248                                    canlock-sha1-function))
249         keys locks errmsg id-for-key id-for-lock password
250         key-for-key key-for-lock match)
251     (save-excursion
252       (when buffer
253         (set-buffer buffer))
254       (save-restriction
255         (widen)
256         (canlock-narrow-to-header)
257         (setq keys (canlock-fetch-fields 'key)
258               locks (canlock-fetch-fields))
259         (if (not (or keys locks))
260             (setq errmsg
261                   "There are neither Cancel-Lock nor Cancel-Key headers")
262           (setq id-for-key (canlock-fetch-id-for-key)
263                 id-for-lock (mail-fetch-field "Message-ID"))
264           (or id-for-key id-for-lock
265               (setq errmsg "There are no Message-ID(s)")))))
266
267     (if errmsg
268         (if canlock-ignore-errors
269             errmsg
270           (error "%s" errmsg))
271
272       (setq password (or canlock-password-for-verify
273                          (read-passwd "Password for Canlock: ")))
274       (if (or (not (stringp password)) (zerop (length password)))
275           (progn
276             (setq errmsg "Password for Canlock is bad")
277             (if canlock-ignore-errors
278                 errmsg
279               (error "%s" errmsg)))
280
281         (when keys
282           (when id-for-key
283             (setq key-for-key (canlock-make-cancel-key id-for-key password))
284             (while (and keys (not match))
285               (setq match (string-equal key-for-key (pop keys)))))
286           (setq keys (if match "good" "bad")))
287         (setq match nil)
288
289         (when locks
290           (when id-for-lock
291             (setq key-for-lock
292                   (base64-encode-string
293                    (canlock-sha1 (canlock-make-cancel-key id-for-lock
294                                                           password))))
295             (when (and locks (not match))
296               (setq match (string-equal key-for-lock (pop locks)))))
297           (setq locks (if match "good" "bad")))
298
299         (prog1
300             (when (member "bad" (list keys locks))
301               "bad")
302           (cond ((and keys locks)
303                  (message "Cancel-Key is %s, Cancel-Lock is %s" keys locks))
304                 (locks
305                  (message "Cancel-Lock is %s" locks))
306                 (keys
307                  (message "Cancel-Key is %s" keys))))))))
308
309 (provide 'canlock)
310
311 ;;; canlock.el ends here