1 ;;; canlock.el --- functions for Cancel-Lock feature
3 ;; Copyright (C) 1998-1999, 2001-2017 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 <https://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
39 ;; `canlock-password-for-verify' is nil. Note that setting these
40 ;; 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"))
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"))
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."
73 (defun canlock-sha1 (message)
74 "Make a SHA-1 digest of MESSAGE as a unibyte string of length 20 bytes."
75 (sha1 message nil nil
'binary
))
77 (defun canlock-make-cancel-key (message-id password
)
78 "Make a Cancel-Key header."
79 (when (> (length password
) 20)
80 (setq password
(canlock-sha1 password
)))
81 (setq password
(concat password
(make-string (- 64 (length password
)) 0)))
82 (let ((ipad (mapconcat (lambda (byte)
83 (char-to-string (logxor 54 byte
)))
85 (opad (mapconcat (lambda (byte)
86 (char-to-string (logxor 92 byte
)))
89 (canlock-sha1 (concat opad
(canlock-sha1 (concat ipad message-id
)))))))
91 (defun canlock-narrow-to-header ()
92 "Narrow the buffer to the head of the message."
93 (let (case-fold-search)
95 (goto-char (point-min))
96 (goto-char (if (re-search-forward
98 (regexp-quote mail-header-separator
))
103 (defun canlock-delete-headers ()
104 "Delete Cancel-Key or Cancel-Lock headers in the narrowed buffer."
105 (let ((case-fold-search t
))
106 (goto-char (point-min))
107 (while (re-search-forward "^Cancel-\\(Key\\|Lock\\):" nil t
)
108 (delete-region (match-beginning 0)
109 (if (re-search-forward "^[^\t ]" nil t
)
110 (goto-char (match-beginning 0))
113 (defun canlock-fetch-fields (&optional key
)
114 "Return a list of the values of Cancel-Lock header.
115 If KEY is non-nil, look for a Cancel-Key header instead. The buffer
116 is expected to be narrowed to just the headers of the message."
117 (let ((field (mail-fetch-field (if key
"Cancel-Key" "Cancel-Lock")))
119 (case-fold-search t
))
121 (setq fields
(split-string field
"[\t\n\r ,]+"))
123 (when (string-match "^sha1:" (setq field
(pop fields
)))
124 (push (substring field
5) rest
)))
127 (defun canlock-fetch-id-for-key ()
128 "Return a Message-ID in Cancel, Supersedes or Replaces header.
129 The buffer is expected to be narrowed to just the headers of the
131 (or (let ((cancel (mail-fetch-field "Control")))
133 (string-match "^cancel[\t ]+\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
135 (match-string 1 cancel
)))
136 (mail-fetch-field "Supersedes")
137 (mail-fetch-field "Replaces")))
140 (defun canlock-insert-header (&optional id-for-key id-for-lock password
)
141 "Insert a Cancel-Key and/or a Cancel-Lock header if possible."
142 (let (news control key-for-key key-for-lock
)
145 (canlock-narrow-to-header)
146 (when (setq news
(or canlock-force-insert-header
147 (mail-fetch-field "Newsgroups")))
149 (setq id-for-key
(canlock-fetch-id-for-key)))
150 (if (and (setq control
(mail-fetch-field "Control"))
151 (string-match "^cancel[\t ]+<[^\t\n @<>]+@[^\t\n @<>]+>"
153 (setq id-for-lock nil
)
155 (setq id-for-lock
(mail-fetch-field "Message-ID"))))
156 (canlock-delete-headers)
157 (goto-char (point-max))))
159 (if (not (or id-for-key id-for-lock
))
160 (message "There are no Message-ID(s)")
162 (setq password
(or canlock-password
164 "Password for Canlock: "))))
165 (if (or (not (stringp password
)) (zerop (length password
)))
166 (message "Password for Canlock is bad")
167 (setq key-for-key
(when id-for-key
168 (canlock-make-cancel-key
169 id-for-key password
))
170 key-for-lock
(when id-for-lock
171 (canlock-make-cancel-key
172 id-for-lock password
)))
173 (if (not (or key-for-key key-for-lock
))
174 (message "Couldn't insert Canlock header")
176 (insert "Cancel-Key: sha1:" key-for-key
"\n"))
178 (insert "Cancel-Lock: sha1:"
179 (base64-encode-string (canlock-sha1 key-for-lock
))
183 (defun canlock-verify (&optional buffer
)
184 "Verify Cancel-Lock or Cancel-Key in BUFFER.
185 If BUFFER is nil, the current buffer is assumed. Signal an error if
188 (let (keys locks errmsg id-for-key id-for-lock password
189 key-for-key key-for-lock match
)
195 (canlock-narrow-to-header)
196 (setq keys
(canlock-fetch-fields 'key
)
197 locks
(canlock-fetch-fields))
198 (if (not (or keys locks
))
200 "There are neither Cancel-Lock nor Cancel-Key headers")
201 (setq id-for-key
(canlock-fetch-id-for-key)
202 id-for-lock
(mail-fetch-field "Message-ID"))
203 (or id-for-key id-for-lock
204 (setq errmsg
"There are no Message-ID(s)")))))
207 (setq password
(or canlock-password-for-verify
208 (read-passwd "Password for Canlock: ")))
209 (if (or (not (stringp password
)) (zerop (length password
)))
210 (error "Password for Canlock is bad")
213 (setq key-for-key
(canlock-make-cancel-key id-for-key password
))
214 (while (and keys
(not match
))
215 (setq match
(string-equal key-for-key
(pop keys
)))))
216 (setq keys
(if match
"good" "bad")))
221 (base64-encode-string
222 (canlock-sha1 (canlock-make-cancel-key id-for-lock
224 (when (and locks
(not match
))
225 (setq match
(string-equal key-for-lock
(pop locks
)))))
226 (setq locks
(if match
"good" "bad")))
228 (when (member "bad" (list keys locks
))
230 (cond ((and keys locks
)
231 (message "Cancel-Key is %s, Cancel-Lock is %s" keys locks
))
233 (message "Cancel-Lock is %s" locks
))
235 (message "Cancel-Key is %s" keys
))))))))
239 ;;; canlock.el ends here