1 ;;; canlock.el --- functions for Cancel-Lock feature
3 ;; Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc.
5 ;; Author: Katsumi Yamaoka <yamaoka@jpl.org>
6 ;; Keywords: news, cancel-lock, hmac, sha1, rfc2104
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)
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.
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.
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:
33 ;; (add-hook '*e**a*e-header-hook 'canlock-insert-header t)
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.
43 (defconst canlock-version "0.8")
48 (autoload 'sha1-binary "sha1-el")
51 "The Cancel-Lock feature."
54 (defcustom canlock-sha1-function 'sha1-binary
55 "Function to call to make a SHA-1 message digest."
56 :type '(radio (function-item sha1-binary)
57 (function-item canlock-sha1-with-openssl)
58 (function :tag "Other"))
61 (defcustom canlock-sha1-function-for-verify canlock-sha1-function
62 "Function to call to make a SHA-1 message digest for verifying."
63 :type '(radio (function-item sha1-binary)
64 (function-item canlock-sha1-with-openssl)
65 (function :tag "Other"))
68 (defcustom canlock-openssl-program "openssl"
69 "Name of OpenSSL program."
73 (defcustom canlock-openssl-args '("sha1")
74 "Arguments passed to the OpenSSL program."
78 (defcustom canlock-ignore-errors nil
79 "If non-nil, ignore any error signals."
83 (defcustom canlock-password nil
84 "Password to use when signing a Cancel-Lock or a Cancel-Key header."
88 (defcustom canlock-password-for-verify canlock-password
89 "Password to use when verifying a Cancel-Lock or a Cancel-Key header."
93 (defcustom canlock-force-insert-header nil
94 "If non-nil, insert a Cancel-Lock or a Cancel-Key header even if the
95 buffer does not look like a news message."
100 (defmacro canlock-string-as-unibyte (string)
101 "Return a unibyte string with the same individual bytes as STRING."
102 (if (fboundp 'string-as-unibyte)
103 (list 'string-as-unibyte string)
106 (defun canlock-sha1-with-openssl (message)
107 "Make a SHA-1 digest of MESSAGE using OpenSSL."
108 (let (default-enable-multibyte-characters)
110 (let ((coding-system-for-read 'binary)
111 (coding-system-for-write 'binary)
113 (case-fold-search t))
115 (apply 'call-process-region (point-min) (point-max)
116 canlock-openssl-program t t nil canlock-openssl-args)
117 (goto-char (point-min))
119 (while (re-search-forward "[0-9a-f][0-9a-f]" nil t)
120 (replace-match (concat "\\\\x" (match-string 0))))
122 (goto-char (point-min))
123 (canlock-string-as-unibyte (read (current-buffer)))))))
125 (defvar canlock-read-passwd nil)
126 (defun canlock-read-passwd (prompt &rest args)
127 "Read a password using PROMPT.
128 If ARGS, PROMPT is used as an argument to `format'."
131 (apply 'format prompt args)
133 (unless canlock-read-passwd
134 (if (or (fboundp 'read-passwd) (load "passwd" t))
135 (setq canlock-read-passwd 'read-passwd)
136 (unless (fboundp 'ange-ftp-read-passwd)
137 (autoload 'ange-ftp-read-passwd "ange-ftp"))
138 (setq canlock-read-passwd 'ange-ftp-read-passwd)))
139 (funcall canlock-read-passwd prompt)))
141 (defun canlock-make-cancel-key (message-id password)
142 "Make a Cancel-Key header."
143 (cond ((> (length password) 20)
144 (setq password (funcall canlock-sha1-function password)))
145 ((< (length password) 20)
146 (setq password (concat
148 (make-string (- 20 (length password)) 0)))))
149 (setq password (concat password (make-string 44 0)))
150 (let ((ipad (mapconcat (lambda (char)
151 (char-to-string (logxor 54 char)))
153 (opad (mapconcat (lambda (char)
154 (char-to-string (logxor 92 char)))
156 (base64-encode-string
157 (funcall canlock-sha1-function
160 (funcall canlock-sha1-function
162 (canlock-string-as-unibyte message-id))))))))
164 (defun canlock-narrow-to-header ()
165 "Narrow the buffer to the head of the message."
166 (let (case-fold-search)
168 (goto-char (point-min))
169 (goto-char (if (re-search-forward
171 (regexp-quote mail-header-separator))
176 (defun canlock-delete-headers ()
177 "Delete Cancel-Key or Cancel-Lock headers in the narrowed buffer."
178 (let ((case-fold-search t))
179 (goto-char (point-min))
180 (while (re-search-forward "^Cancel-\\(Key\\|Lock\\):" nil t)
181 (delete-region (match-beginning 0)
182 (if (re-search-forward "^[^\t ]" nil t)
183 (goto-char (match-beginning 0))
186 (defun canlock-fetch-fields (&optional key)
187 "Return a list of the values of Cancel-Lock header.
188 If KEY is non-nil, look for a Cancel-Key header instead. The buffer
189 is expected to be narrowed to just the headers of the message."
190 (let ((field (mail-fetch-field (if key "Cancel-Key" "Cancel-Lock")))
192 (case-fold-search t))
194 (setq fields (split-string field "[\t\n\r ,]+"))
196 (when (string-match "^sha1:" (setq field (pop fields)))
197 (push (substring field 5) rest)))
200 (defun canlock-fetch-id-for-key ()
201 "Return a Message-ID in Cancel, Supersedes or Replaces header.
202 The buffer is expected to be narrowed to just the headers of the
204 (or (let ((cancel (mail-fetch-field "Control")))
206 (string-match "^cancel[\t ]+\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
208 (match-string 1 cancel)))
209 (mail-fetch-field "Supersedes")
210 (mail-fetch-field "Replaces")))
213 (defun canlock-insert-header (&optional id-for-key id-for-lock password)
214 "Insert a Cancel-Key and/or a Cancel-Lock header if possible."
215 (let (news control key-for-key key-for-lock)
218 (canlock-narrow-to-header)
219 (when (setq news (or canlock-force-insert-header
220 (mail-fetch-field "Newsgroups")))
222 (setq id-for-key (canlock-fetch-id-for-key)))
223 (if (and (setq control (mail-fetch-field "Control"))
225 "^cancel[\t ]+\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
227 (setq id-for-lock nil)
229 (setq id-for-lock (mail-fetch-field "Message-ID"))))
230 (canlock-delete-headers)
231 (goto-char (point-max))))
233 (if (not (or id-for-key id-for-lock))
234 (message "There are no Message-ID(s)")
236 (setq password (or canlock-password
238 "Password for Canlock: "))))
239 (if (or (not (stringp password)) (zerop (length password)))
240 (message "Password for Canlock is bad")
241 (setq key-for-key (when id-for-key
242 (canlock-make-cancel-key
243 id-for-key password))
244 key-for-lock (when id-for-lock
245 (canlock-make-cancel-key
246 id-for-lock password)))
247 (if (not (or key-for-key key-for-lock))
248 (message "Couldn't insert Canlock header")
250 (insert "Cancel-Key: sha1:" key-for-key "\n"))
252 (insert "Cancel-Lock: sha1:"
253 (base64-encode-string (funcall canlock-sha1-function
258 (defun canlock-verify (&optional buffer)
259 "Verify Cancel-Lock or Cancel-Key in BUFFER.
260 If BUFFER is nil, the current buffer is assumed. Signal an error if
261 it fails. You can modify the behavior of this function to return non-
262 nil instead of to signal an error by setting the option
263 `canlock-ignore-errors' to non-nil."
265 (let ((canlock-sha1-function (or canlock-sha1-function-for-verify
266 canlock-sha1-function))
267 keys locks errmsg id-for-key id-for-lock password
268 key-for-key key-for-lock match)
274 (canlock-narrow-to-header)
275 (setq keys (canlock-fetch-fields 'key)
276 locks (canlock-fetch-fields))
277 (if (not (or keys locks))
279 "There are neither Cancel-Lock nor Cancel-Key headers")
280 (setq id-for-key (canlock-fetch-id-for-key)
281 id-for-lock (mail-fetch-field "Message-ID"))
282 (or id-for-key id-for-lock
283 (setq errmsg "There are no Message-ID(s)")))))
286 (if canlock-ignore-errors
290 (setq password (or canlock-password-for-verify
291 (canlock-read-passwd "Password for Canlock: ")))
292 (if (or (not (stringp password)) (zerop (length password)))
294 (setq errmsg "Password for Canlock is bad")
295 (if canlock-ignore-errors
297 (error "%s" errmsg)))
301 (setq key-for-key (canlock-make-cancel-key id-for-key password))
302 (while (and keys (not match))
303 (setq match (string-equal key-for-key (pop keys)))))
304 (setq keys (if match "good" "bad")))
310 (base64-encode-string (funcall canlock-sha1-function
311 (canlock-make-cancel-key
312 id-for-lock password))))
313 (when (and locks (not match))
314 (setq match (string-equal key-for-lock (pop locks)))))
315 (setq locks (if match "good" "bad")))
318 (when (member "bad" (list keys locks))
320 (cond ((and keys locks)
321 (message "Cancel-Key is %s, Cancel-Lock is %s" keys locks))
323 (message "Cancel-Lock is %s" locks))
325 (message "Cancel-Key is %s" keys))))))))
329 ;;; canlock.el ends here