1 ;;; canlock.el --- functions for Cancel-Lock feature
3 ;; Copyright (C) 1998-1999, 2001-2013 Free Software Foundation, Inc.
5 ;; Author: Katsumi Yamaoka <yamaoka@jpl.org>
6 ;; Keywords: news, cancel-lock, hmac, sha1, rfc2104
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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.
48 (defvar mail-header-separator
)
51 "The Cancel-Lock feature."
54 (defcustom canlock-password nil
55 "Password to use when signing a Cancel-Lock or a Cancel-Key header."
56 :type
'(radio (const :format
"Not specified " nil
)
57 (string :tag
"Password"))
60 (defcustom canlock-password-for-verify canlock-password
61 "Password to use when verifying a Cancel-Lock or a Cancel-Key header."
62 :type
'(radio (const :format
"Not specified " nil
)
63 (string :tag
"Password"))
66 (defcustom canlock-force-insert-header nil
67 "If non-nil, insert a Cancel-Lock or a Cancel-Key header even if the
68 buffer does not look like a news message."
73 (defmacro canlock-string-as-unibyte
(string)
74 "Return a unibyte string with the same individual bytes as STRING."
75 (if (fboundp 'string-as-unibyte
)
76 (list 'string-as-unibyte string
)
79 (defun canlock-sha1 (message)
80 "Make a SHA-1 digest of MESSAGE as a unibyte string of length 20 bytes."
81 (let (sha1-maximum-internal-length)
82 (sha1 message nil nil
'binary
)))
84 (defun canlock-make-cancel-key (message-id password
)
85 "Make a Cancel-Key header."
86 (when (> (length password
) 20)
87 (setq password
(canlock-sha1 password
)))
88 (setq password
(concat password
(make-string (- 64 (length password
)) 0)))
89 (let ((ipad (mapconcat (lambda (byte)
90 (char-to-string (logxor 54 byte
)))
92 (opad (mapconcat (lambda (byte)
93 (char-to-string (logxor 92 byte
)))
99 (concat ipad
(canlock-string-as-unibyte message-id
))))))))
101 (defun canlock-narrow-to-header ()
102 "Narrow the buffer to the head of the message."
103 (let (case-fold-search)
105 (goto-char (point-min))
106 (goto-char (if (re-search-forward
108 (regexp-quote mail-header-separator
))
113 (defun canlock-delete-headers ()
114 "Delete Cancel-Key or Cancel-Lock headers in the narrowed buffer."
115 (let ((case-fold-search t
))
116 (goto-char (point-min))
117 (while (re-search-forward "^Cancel-\\(Key\\|Lock\\):" nil t
)
118 (delete-region (match-beginning 0)
119 (if (re-search-forward "^[^\t ]" nil t
)
120 (goto-char (match-beginning 0))
123 (defun canlock-fetch-fields (&optional key
)
124 "Return a list of the values of Cancel-Lock header.
125 If KEY is non-nil, look for a Cancel-Key header instead. The buffer
126 is expected to be narrowed to just the headers of the message."
127 (let ((field (mail-fetch-field (if key
"Cancel-Key" "Cancel-Lock")))
129 (case-fold-search t
))
131 (setq fields
(split-string field
"[\t\n\r ,]+"))
133 (when (string-match "^sha1:" (setq field
(pop fields
)))
134 (push (substring field
5) rest
)))
137 (defun canlock-fetch-id-for-key ()
138 "Return a Message-ID in Cancel, Supersedes or Replaces header.
139 The buffer is expected to be narrowed to just the headers of the
141 (or (let ((cancel (mail-fetch-field "Control")))
143 (string-match "^cancel[\t ]+\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
145 (match-string 1 cancel
)))
146 (mail-fetch-field "Supersedes")
147 (mail-fetch-field "Replaces")))
150 (defun canlock-insert-header (&optional id-for-key id-for-lock password
)
151 "Insert a Cancel-Key and/or a Cancel-Lock header if possible."
152 (let (news control key-for-key key-for-lock
)
155 (canlock-narrow-to-header)
156 (when (setq news
(or canlock-force-insert-header
157 (mail-fetch-field "Newsgroups")))
159 (setq id-for-key
(canlock-fetch-id-for-key)))
160 (if (and (setq control
(mail-fetch-field "Control"))
161 (string-match "^cancel[\t ]+<[^\t\n @<>]+@[^\t\n @<>]+>"
163 (setq id-for-lock nil
)
165 (setq id-for-lock
(mail-fetch-field "Message-ID"))))
166 (canlock-delete-headers)
167 (goto-char (point-max))))
169 (if (not (or id-for-key id-for-lock
))
170 (message "There are no Message-ID(s)")
172 (setq password
(or canlock-password
174 "Password for Canlock: "))))
175 (if (or (not (stringp password
)) (zerop (length password
)))
176 (message "Password for Canlock is bad")
177 (setq key-for-key
(when id-for-key
178 (canlock-make-cancel-key
179 id-for-key password
))
180 key-for-lock
(when id-for-lock
181 (canlock-make-cancel-key
182 id-for-lock password
)))
183 (if (not (or key-for-key key-for-lock
))
184 (message "Couldn't insert Canlock header")
186 (insert "Cancel-Key: sha1:" key-for-key
"\n"))
188 (insert "Cancel-Lock: sha1:"
189 (base64-encode-string (canlock-sha1 key-for-lock
))
193 (defun canlock-verify (&optional buffer
)
194 "Verify Cancel-Lock or Cancel-Key in BUFFER.
195 If BUFFER is nil, the current buffer is assumed. Signal an error if
198 (let (keys locks errmsg id-for-key id-for-lock password
199 key-for-key key-for-lock match
)
205 (canlock-narrow-to-header)
206 (setq keys
(canlock-fetch-fields 'key
)
207 locks
(canlock-fetch-fields))
208 (if (not (or keys locks
))
210 "There are neither Cancel-Lock nor Cancel-Key headers")
211 (setq id-for-key
(canlock-fetch-id-for-key)
212 id-for-lock
(mail-fetch-field "Message-ID"))
213 (or id-for-key id-for-lock
214 (setq errmsg
"There are no Message-ID(s)")))))
217 (setq password
(or canlock-password-for-verify
218 (read-passwd "Password for Canlock: ")))
219 (if (or (not (stringp password
)) (zerop (length password
)))
220 (error "Password for Canlock is bad")
223 (setq key-for-key
(canlock-make-cancel-key id-for-key password
))
224 (while (and keys
(not match
))
225 (setq match
(string-equal key-for-key
(pop keys
)))))
226 (setq keys
(if match
"good" "bad")))
231 (base64-encode-string
232 (canlock-sha1 (canlock-make-cancel-key id-for-lock
234 (when (and locks
(not match
))
235 (setq match
(string-equal key-for-lock
(pop locks
)))))
236 (setq locks
(if match
"good" "bad")))
238 (when (member "bad" (list keys locks
))
240 (cond ((and keys locks
)
241 (message "Cancel-Key is %s, Cancel-Lock is %s" keys locks
))
243 (message "Cancel-Lock is %s" locks
))
245 (message "Cancel-Key is %s" keys
))))))))
249 ;;; canlock.el ends here