1 ;;; canlock.el --- functions for Cancel-Lock feature
3 ;; Copyright (C) 1998, 1999, 2001, 2002, 2003 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.
46 (autoload 'sha1-binary "sha1-el")
47 (autoload 'base64-encode-string "base64")
48 (autoload 'mail-fetch-field "mail-utils")
49 (defvar mail-header-separator)
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-binary)
58 (function-item canlock-sha1-with-openssl)
59 (function :tag "Other"))
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"))
69 (defcustom canlock-openssl-program "openssl"
70 "Name of OpenSSL program."
74 (defcustom canlock-openssl-args '("sha1")
75 "Arguments passed to the OpenSSL program."
79 (defcustom canlock-ignore-errors nil
80 "If non-nil, ignore any error signals."
84 (defcustom canlock-password nil
85 "Password to use when signing a Cancel-Lock or a Cancel-Key header."
89 (defcustom canlock-password-for-verify canlock-password
90 "Password to use when verifying a Cancel-Lock or a Cancel-Key header."
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."
100 (defun canlock-sha1-with-openssl (message)
101 "Make a SHA-1 digest of MESSAGE using OpenSSL."
102 (let (default-enable-multibyte-characters)
104 (let ((coding-system-for-read 'binary)
105 (coding-system-for-write 'binary)
107 (case-fold-search t))
109 (apply 'call-process-region (point-min) (point-max)
110 canlock-openssl-program t t nil canlock-openssl-args)
111 (goto-char (point-min))
113 (while (re-search-forward "\\([0-9a-f][0-9a-f]\\)" nil t)
114 (replace-match "\\\\x\\1"))
116 (goto-char (point-min))
117 (read (current-buffer))))))
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)
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)))
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)))
138 (opad (mapconcat (lambda (byte)
139 (char-to-string (logxor 92 byte)))
141 (base64-encode-string
145 (concat ipad (canlock-string-as-unibyte message-id))))))))
147 (defun canlock-narrow-to-header ()
148 "Narrow the buffer to the head of the message."
149 (let (case-fold-search)
151 (goto-char (point-min))
152 (goto-char (if (re-search-forward
154 (regexp-quote mail-header-separator))
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))
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")))
175 (case-fold-search t))
177 (setq fields (split-string field "[\t\n\r ,]+"))
179 (when (string-match "^sha1:" (setq field (pop fields)))
180 (push (substring field 5) rest)))
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
187 (or (let ((cancel (mail-fetch-field "Control")))
189 (string-match "^cancel[\t ]+\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
191 (match-string 1 cancel)))
192 (mail-fetch-field "Supersedes")
193 (mail-fetch-field "Replaces")))
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)
201 (canlock-narrow-to-header)
202 (when (setq news (or canlock-force-insert-header
203 (mail-fetch-field "Newsgroups")))
205 (setq id-for-key (canlock-fetch-id-for-key)))
206 (if (and (setq control (mail-fetch-field "Control"))
208 "^cancel[\t ]+\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
210 (setq id-for-lock nil)
212 (setq id-for-lock (mail-fetch-field "Message-ID"))))
213 (canlock-delete-headers)
214 (goto-char (point-max))))
216 (if (not (or id-for-key id-for-lock))
217 (message "There are no Message-ID(s)")
219 (setq password (or canlock-password
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")
233 (insert "Cancel-Key: sha1:" key-for-key "\n"))
235 (insert "Cancel-Lock: sha1:"
236 (base64-encode-string (canlock-sha1 key-for-lock))
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."
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)
256 (canlock-narrow-to-header)
257 (setq keys (canlock-fetch-fields 'key)
258 locks (canlock-fetch-fields))
259 (if (not (or keys locks))
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)")))))
268 (if canlock-ignore-errors
272 (setq password (or canlock-password-for-verify
273 (read-passwd "Password for Canlock: ")))
274 (if (or (not (stringp password)) (zerop (length password)))
276 (setq errmsg "Password for Canlock is bad")
277 (if canlock-ignore-errors
279 (error "%s" errmsg)))
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")))
292 (base64-encode-string
293 (canlock-sha1 (canlock-make-cancel-key id-for-lock
295 (when (and locks (not match))
296 (setq match (string-equal key-for-lock (pop locks)))))
297 (setq locks (if match "good" "bad")))
300 (when (member "bad" (list keys locks))
302 (cond ((and keys locks)
303 (message "Cancel-Key is %s, Cancel-Lock is %s" keys locks))
305 (message "Cancel-Lock is %s" locks))
307 (message "Cancel-Key is %s" keys))))))))
311 ;;; canlock.el ends here