Document reserved keys
[emacs.git] / lisp / gnus / canlock.el
blob7c657ead78d16db796932d86830e574c64e71b12
1 ;;; canlock.el --- functions for Cancel-Lock feature
3 ;; Copyright (C) 1998-1999, 2001-2018 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/>.
23 ;;; Commentary:
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.
42 ;;; Code:
44 (eval-when-compile
45 (require 'cl))
47 (require 'sha1)
49 (defvar mail-header-separator)
51 (defgroup canlock nil
52 "The Cancel-Lock feature."
53 :group 'news)
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"))
59 :group 'canlock)
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"))
65 :group 'canlock)
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."
70 :type 'boolean
71 :group 'canlock)
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)))
84 password ""))
85 (opad (mapconcat (lambda (byte)
86 (char-to-string (logxor 92 byte)))
87 password "")))
88 (base64-encode-string
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)
94 (narrow-to-region
95 (goto-char (point-min))
96 (goto-char (if (re-search-forward
97 (format "^$\\|^%s$"
98 (regexp-quote mail-header-separator))
99 nil t)
100 (match-beginning 0)
101 (point-max))))))
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))
111 (point-max))))))
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")))
118 fields rest
119 (case-fold-search t))
120 (when field
121 (setq fields (split-string field "[\t\n\r ,]+"))
122 (while fields
123 (when (string-match "^sha1:" (setq field (pop fields)))
124 (push (substring field 5) rest)))
125 (nreverse 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
130 message."
131 (or (let ((cancel (mail-fetch-field "Control")))
132 (and cancel
133 (string-match "^cancel[\t ]+\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
134 cancel)
135 (match-string 1 cancel)))
136 (mail-fetch-field "Supersedes")
137 (mail-fetch-field "Replaces")))
139 ;;;###autoload
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)
143 (save-excursion
144 (save-restriction
145 (canlock-narrow-to-header)
146 (when (setq news (or canlock-force-insert-header
147 (mail-fetch-field "Newsgroups")))
148 (unless id-for-key
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 @<>]+>"
152 control))
153 (setq id-for-lock nil)
154 (unless id-for-lock
155 (setq id-for-lock (mail-fetch-field "Message-ID"))))
156 (canlock-delete-headers)
157 (goto-char (point-max))))
158 (when news
159 (if (not (or id-for-key id-for-lock))
160 (message "There are no Message-ID(s)")
161 (unless password
162 (setq password (or canlock-password
163 (read-passwd
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")
175 (when key-for-key
176 (insert "Cancel-Key: sha1:" key-for-key "\n"))
177 (when key-for-lock
178 (insert "Cancel-Lock: sha1:"
179 (base64-encode-string (canlock-sha1 key-for-lock))
180 "\n")))))))))
182 ;;;###autoload
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
186 it fails."
187 (interactive)
188 (let (keys locks errmsg id-for-key id-for-lock password
189 key-for-key key-for-lock match)
190 (save-excursion
191 (when buffer
192 (set-buffer buffer))
193 (save-restriction
194 (widen)
195 (canlock-narrow-to-header)
196 (setq keys (canlock-fetch-fields 'key)
197 locks (canlock-fetch-fields))
198 (if (not (or keys locks))
199 (setq errmsg
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)")))))
205 (if errmsg
206 (error "%s" errmsg)
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")
211 (when keys
212 (when id-for-key
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")))
217 (setq match nil)
218 (when locks
219 (when id-for-lock
220 (setq key-for-lock
221 (base64-encode-string
222 (canlock-sha1 (canlock-make-cancel-key id-for-lock
223 password))))
224 (when (and locks (not match))
225 (setq match (string-equal key-for-lock (pop locks)))))
226 (setq locks (if match "good" "bad")))
227 (prog1
228 (when (member "bad" (list keys locks))
229 "bad")
230 (cond ((and keys locks)
231 (message "Cancel-Key is %s, Cancel-Lock is %s" keys locks))
232 (locks
233 (message "Cancel-Lock is %s" locks))
234 (keys
235 (message "Cancel-Key is %s" keys))))))))
237 (provide 'canlock)
239 ;;; canlock.el ends here