1 ;;; canlock.el --- functions for Cancel-Lock feature
3 ;; Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004
4 ;; Free Software Foundation, Inc.
6 ;; Author: Katsumi Yamaoka <yamaoka@jpl.org>
7 ;; Keywords: news, cancel-lock, hmac, sha1, rfc2104
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;; Canlock is a library for generating and verifying Cancel-Lock and/or
27 ;; Cancel-Key header in news articles. This is used to protect articles
28 ;; from rogue cancel, supersede or replace attacks. The method is based
29 ;; on draft-ietf-usefor-cancel-lock-01.txt which was released on November
30 ;; 3rd 1998. For instance, you can add Cancel-Lock (and possibly Cancel-
31 ;; Key) header in a news article by using a hook which will be evaluated
32 ;; just before sending an article as follows:
34 ;; (add-hook '*e**a*e-header-hook 'canlock-insert-header t)
36 ;; Verifying Cancel-Lock is mainly a function of news servers, however,
37 ;; you can verify your own article using the command `canlock-verify' in
38 ;; the (raw) article buffer. You will be prompted for the password for
39 ;; each time if the option `canlock-password' or `canlock-password-for-
40 ;; verify' is nil. Note that setting these options is a bit unsafe.
49 (defvar mail-header-separator)
52 "The Cancel-Lock feature."
55 (defcustom canlock-password nil
56 "Password to use when signing a Cancel-Lock or a Cancel-Key header."
57 :type '(radio (const :format "Not specified " nil)
58 (string :tag "Password" :size 0))
61 (defcustom canlock-password-for-verify canlock-password
62 "Password to use when verifying a Cancel-Lock or a Cancel-Key header."
63 :type '(radio (const :format "Not specified " nil)
64 (string :tag "Password" :size 0))
67 (defcustom canlock-force-insert-header nil
68 "If non-nil, insert a Cancel-Lock or a Cancel-Key header even if the
69 buffer does not look like a news message."
74 (defmacro canlock-string-as-unibyte (string)
75 "Return a unibyte string with the same individual bytes as STRING."
76 (if (fboundp 'string-as-unibyte)
77 (list 'string-as-unibyte string)
80 (defun canlock-sha1 (message)
81 "Make a SHA-1 digest of MESSAGE as a unibyte string of length 20 bytes."
83 (let (sha1-maximum-internal-length)
84 (sha1 message nil nil 'binary))
85 (wrong-number-of-arguments
86 (canlock-string-as-unibyte (sha1-binary message)))))
88 (defun canlock-make-cancel-key (message-id password)
89 "Make a Cancel-Key header."
90 (when (> (length password) 20)
91 (setq password (canlock-sha1 password)))
92 (setq password (concat password (make-string (- 64 (length password)) 0)))
93 (let ((ipad (mapconcat (lambda (byte)
94 (char-to-string (logxor 54 byte)))
96 (opad (mapconcat (lambda (byte)
97 (char-to-string (logxor 92 byte)))
103 (concat ipad (canlock-string-as-unibyte message-id))))))))
105 (defun canlock-narrow-to-header ()
106 "Narrow the buffer to the head of the message."
107 (let (case-fold-search)
109 (goto-char (point-min))
110 (goto-char (if (re-search-forward
112 (regexp-quote mail-header-separator))
117 (defun canlock-delete-headers ()
118 "Delete Cancel-Key or Cancel-Lock headers in the narrowed buffer."
119 (let ((case-fold-search t))
120 (goto-char (point-min))
121 (while (re-search-forward "^Cancel-\\(Key\\|Lock\\):" nil t)
122 (delete-region (match-beginning 0)
123 (if (re-search-forward "^[^\t ]" nil t)
124 (goto-char (match-beginning 0))
127 (defun canlock-fetch-fields (&optional key)
128 "Return a list of the values of Cancel-Lock header.
129 If KEY is non-nil, look for a Cancel-Key header instead. The buffer
130 is expected to be narrowed to just the headers of the message."
131 (let ((field (mail-fetch-field (if key "Cancel-Key" "Cancel-Lock")))
133 (case-fold-search t))
135 (setq fields (split-string field "[\t\n\r ,]+"))
137 (when (string-match "^sha1:" (setq field (pop fields)))
138 (push (substring field 5) rest)))
141 (defun canlock-fetch-id-for-key ()
142 "Return a Message-ID in Cancel, Supersedes or Replaces header.
143 The buffer is expected to be narrowed to just the headers of the
145 (or (let ((cancel (mail-fetch-field "Control")))
147 (string-match "^cancel[\t ]+\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
149 (match-string 1 cancel)))
150 (mail-fetch-field "Supersedes")
151 (mail-fetch-field "Replaces")))
154 (defun canlock-insert-header (&optional id-for-key id-for-lock password)
155 "Insert a Cancel-Key and/or a Cancel-Lock header if possible."
156 (let (news control key-for-key key-for-lock)
159 (canlock-narrow-to-header)
160 (when (setq news (or canlock-force-insert-header
161 (mail-fetch-field "Newsgroups")))
163 (setq id-for-key (canlock-fetch-id-for-key)))
164 (if (and (setq control (mail-fetch-field "Control"))
165 (string-match "^cancel[\t ]+<[^\t\n @<>]+@[^\t\n @<>]+>"
167 (setq id-for-lock nil)
169 (setq id-for-lock (mail-fetch-field "Message-ID"))))
170 (canlock-delete-headers)
171 (goto-char (point-max))))
173 (if (not (or id-for-key id-for-lock))
174 (message "There are no Message-ID(s)")
176 (setq password (or canlock-password
178 "Password for Canlock: "))))
179 (if (or (not (stringp password)) (zerop (length password)))
180 (message "Password for Canlock is bad")
181 (setq key-for-key (when id-for-key
182 (canlock-make-cancel-key
183 id-for-key password))
184 key-for-lock (when id-for-lock
185 (canlock-make-cancel-key
186 id-for-lock password)))
187 (if (not (or key-for-key key-for-lock))
188 (message "Couldn't insert Canlock header")
190 (insert "Cancel-Key: sha1:" key-for-key "\n"))
192 (insert "Cancel-Lock: sha1:"
193 (base64-encode-string (canlock-sha1 key-for-lock))
197 (defun canlock-verify (&optional buffer)
198 "Verify Cancel-Lock or Cancel-Key in BUFFER.
199 If BUFFER is nil, the current buffer is assumed. Signal an error if
202 (let (keys locks errmsg id-for-key id-for-lock password
203 key-for-key key-for-lock match)
209 (canlock-narrow-to-header)
210 (setq keys (canlock-fetch-fields 'key)
211 locks (canlock-fetch-fields))
212 (if (not (or keys locks))
214 "There are neither Cancel-Lock nor Cancel-Key headers")
215 (setq id-for-key (canlock-fetch-id-for-key)
216 id-for-lock (mail-fetch-field "Message-ID"))
217 (or id-for-key id-for-lock
218 (setq errmsg "There are no Message-ID(s)")))))
221 (setq password (or canlock-password-for-verify
222 (read-passwd "Password for Canlock: ")))
223 (if (or (not (stringp password)) (zerop (length password)))
224 (error "Password for Canlock is bad")
227 (setq key-for-key (canlock-make-cancel-key id-for-key password))
228 (while (and keys (not match))
229 (setq match (string-equal key-for-key (pop keys)))))
230 (setq keys (if match "good" "bad")))
235 (base64-encode-string
236 (canlock-sha1 (canlock-make-cancel-key id-for-lock
238 (when (and locks (not match))
239 (setq match (string-equal key-for-lock (pop locks)))))
240 (setq locks (if match "good" "bad")))
242 (when (member "bad" (list keys locks))
244 (cond ((and keys locks)
245 (message "Cancel-Key is %s, Cancel-Lock is %s" keys locks))
247 (message "Cancel-Lock is %s" locks))
249 (message "Cancel-Key is %s" keys))))))))
253 ;;; canlock.el ends here