1 ;;; canlock.el --- Functions for Cancel-Lock feature
2 ;; Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.
4 ;; Author: Katsumi Yamaoka <yamaoka@jpl.org>
5 ;; Keywords: news, cancel-lock, hmac, sha1, rfc2104
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)
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.
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.
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:
32 ;; (add-hook '*e**a*e-header-hook 'canlock-insert-header t)
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.
42 (defconst canlock-version "0.8")
47 (autoload 'sha1-binary "sha1-el")
48 (autoload 'sha1-encode-binary "sha1")
49 (autoload 'base64-encode "base64")
52 "The Cancel-Lock feature."
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"))
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"))
71 (defcustom canlock-openssl-program "openssl"
72 "Name of OpenSSL program."
76 (defcustom canlock-openssl-args '("sha1")
77 "Arguments passed to the OpenSSL program."
81 (defcustom canlock-ignore-errors nil
82 "If non-nil, ignore any error signals."
86 (defcustom canlock-password nil
87 "Password to use when signing a Cancel-Lock or a Cancel-Key header."
91 (defcustom canlock-password-for-verify canlock-password
92 "Password to use when verifying a Cancel-Lock or a Cancel-Key header."
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."
102 (defun canlock-sha1-with-openssl (message)
103 "Make a SHA-1 digest of MESSAGE using OpenSSL."
105 (let ((coding-system-for-read 'binary)
106 (coding-system-for-write 'binary)
108 (case-fold-search t))
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)))))
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'."
123 (apply 'format prompt args)
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)))
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
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)))
145 (opad (mapconcat (lambda (char)
146 (char-to-string (logxor 92 char)))
148 (base64-encode-string (funcall canlock-sha1-function
151 (funcall canlock-sha1-function
152 (concat ipad message-id)))))))
154 (defun canlock-narrow-to-header ()
155 "Narrow the buffer to the head of the message."
156 (let (case-fold-search)
158 (goto-char (point-min))
159 (goto-char (if (re-search-forward
161 (regexp-quote mail-header-separator))
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))
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")))
182 (case-fold-search t))
184 (setq fields (split-string field "[\t\n\r ,]+"))
186 (when (string-match "^sha1:" (setq field (pop fields)))
187 (push (substring field 5) rest)))
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
194 (or (let ((cancel (mail-fetch-field "Control")))
196 (string-match "^cancel[\t ]+\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
198 (match-string 1 cancel)))
199 (mail-fetch-field "Supersedes")
200 (mail-fetch-field "Replaces")))
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)
208 (canlock-narrow-to-header)
209 (when (setq news (or canlock-force-insert-header
210 (mail-fetch-field "Newsgroups")))
212 (setq id-for-key (canlock-fetch-id-for-key)))
213 (if (and (setq control (mail-fetch-field "Control"))
215 "^cancel[\t ]+\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
217 (setq id-for-lock nil)
219 (setq id-for-lock (mail-fetch-field "Message-ID"))))
220 (canlock-delete-headers)
221 (goto-char (point-max))))
223 (if (not (or id-for-key id-for-lock))
224 (message "There are no Message-ID(s)")
226 (setq password (or canlock-password
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")
240 (insert "Cancel-Key: sha1:" key-for-key "\n"))
242 (insert "Cancel-Lock: sha1:"
243 (base64-encode-string (funcall canlock-sha1-function
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."
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)
264 (canlock-narrow-to-header)
265 (setq keys (canlock-fetch-fields 'key)
266 locks (canlock-fetch-fields))
267 (if (not (or keys locks))
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)")))))
276 (if canlock-ignore-errors
280 (setq password (or canlock-password-for-verify
281 (canlock-read-passwd "Password for Canlock: ")))
282 (if (or (not (stringp password)) (zerop (length password)))
284 (setq errmsg "Password for Canlock is bad")
285 (if canlock-ignore-errors
287 (error "%s" errmsg)))
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")))
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")))
308 (when (member "bad" (list keys locks))
310 (cond ((and keys locks)
311 (message "Cancel-Key is %s, Cancel-Lock is %s" keys locks))
313 (message "Cancel-Lock is %s" locks))
315 (message "Cancel-Key is %s" keys))))))))
319 ;;; canlock.el ends here