Merge branch 'master' into comment-cache
[emacs.git] / lisp / gnus / canlock.el
blob9e13ced4670342c33a757da0fff2bf2cb8e995b1
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 <http://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 (let (sha1-maximum-internal-length)
76 (sha1 message nil nil 'binary)))
78 (defun canlock-make-cancel-key (message-id password)
79 "Make a Cancel-Key header."
80 (when (> (length password) 20)
81 (setq password (canlock-sha1 password)))
82 (setq password (concat password (make-string (- 64 (length password)) 0)))
83 (let ((ipad (mapconcat (lambda (byte)
84 (char-to-string (logxor 54 byte)))
85 password ""))
86 (opad (mapconcat (lambda (byte)
87 (char-to-string (logxor 92 byte)))
88 password "")))
89 (base64-encode-string
90 (canlock-sha1
91 (concat opad
92 (canlock-sha1
93 (concat ipad (string-as-unibyte message-id))))))))
95 (defun canlock-narrow-to-header ()
96 "Narrow the buffer to the head of the message."
97 (let (case-fold-search)
98 (narrow-to-region
99 (goto-char (point-min))
100 (goto-char (if (re-search-forward
101 (format "^$\\|^%s$"
102 (regexp-quote mail-header-separator))
103 nil t)
104 (match-beginning 0)
105 (point-max))))))
107 (defun canlock-delete-headers ()
108 "Delete Cancel-Key or Cancel-Lock headers in the narrowed buffer."
109 (let ((case-fold-search t))
110 (goto-char (point-min))
111 (while (re-search-forward "^Cancel-\\(Key\\|Lock\\):" nil t)
112 (delete-region (match-beginning 0)
113 (if (re-search-forward "^[^\t ]" nil t)
114 (goto-char (match-beginning 0))
115 (point-max))))))
117 (defun canlock-fetch-fields (&optional key)
118 "Return a list of the values of Cancel-Lock header.
119 If KEY is non-nil, look for a Cancel-Key header instead. The buffer
120 is expected to be narrowed to just the headers of the message."
121 (let ((field (mail-fetch-field (if key "Cancel-Key" "Cancel-Lock")))
122 fields rest
123 (case-fold-search t))
124 (when field
125 (setq fields (split-string field "[\t\n\r ,]+"))
126 (while fields
127 (when (string-match "^sha1:" (setq field (pop fields)))
128 (push (substring field 5) rest)))
129 (nreverse rest))))
131 (defun canlock-fetch-id-for-key ()
132 "Return a Message-ID in Cancel, Supersedes or Replaces header.
133 The buffer is expected to be narrowed to just the headers of the
134 message."
135 (or (let ((cancel (mail-fetch-field "Control")))
136 (and cancel
137 (string-match "^cancel[\t ]+\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
138 cancel)
139 (match-string 1 cancel)))
140 (mail-fetch-field "Supersedes")
141 (mail-fetch-field "Replaces")))
143 ;;;###autoload
144 (defun canlock-insert-header (&optional id-for-key id-for-lock password)
145 "Insert a Cancel-Key and/or a Cancel-Lock header if possible."
146 (let (news control key-for-key key-for-lock)
147 (save-excursion
148 (save-restriction
149 (canlock-narrow-to-header)
150 (when (setq news (or canlock-force-insert-header
151 (mail-fetch-field "Newsgroups")))
152 (unless id-for-key
153 (setq id-for-key (canlock-fetch-id-for-key)))
154 (if (and (setq control (mail-fetch-field "Control"))
155 (string-match "^cancel[\t ]+<[^\t\n @<>]+@[^\t\n @<>]+>"
156 control))
157 (setq id-for-lock nil)
158 (unless id-for-lock
159 (setq id-for-lock (mail-fetch-field "Message-ID"))))
160 (canlock-delete-headers)
161 (goto-char (point-max))))
162 (when news
163 (if (not (or id-for-key id-for-lock))
164 (message "There are no Message-ID(s)")
165 (unless password
166 (setq password (or canlock-password
167 (read-passwd
168 "Password for Canlock: "))))
169 (if (or (not (stringp password)) (zerop (length password)))
170 (message "Password for Canlock is bad")
171 (setq key-for-key (when id-for-key
172 (canlock-make-cancel-key
173 id-for-key password))
174 key-for-lock (when id-for-lock
175 (canlock-make-cancel-key
176 id-for-lock password)))
177 (if (not (or key-for-key key-for-lock))
178 (message "Couldn't insert Canlock header")
179 (when key-for-key
180 (insert "Cancel-Key: sha1:" key-for-key "\n"))
181 (when key-for-lock
182 (insert "Cancel-Lock: sha1:"
183 (base64-encode-string (canlock-sha1 key-for-lock))
184 "\n")))))))))
186 ;;;###autoload
187 (defun canlock-verify (&optional buffer)
188 "Verify Cancel-Lock or Cancel-Key in BUFFER.
189 If BUFFER is nil, the current buffer is assumed. Signal an error if
190 it fails."
191 (interactive)
192 (let (keys locks errmsg id-for-key id-for-lock password
193 key-for-key key-for-lock match)
194 (save-excursion
195 (when buffer
196 (set-buffer buffer))
197 (save-restriction
198 (widen)
199 (canlock-narrow-to-header)
200 (setq keys (canlock-fetch-fields 'key)
201 locks (canlock-fetch-fields))
202 (if (not (or keys locks))
203 (setq errmsg
204 "There are neither Cancel-Lock nor Cancel-Key headers")
205 (setq id-for-key (canlock-fetch-id-for-key)
206 id-for-lock (mail-fetch-field "Message-ID"))
207 (or id-for-key id-for-lock
208 (setq errmsg "There are no Message-ID(s)")))))
209 (if errmsg
210 (error "%s" errmsg)
211 (setq password (or canlock-password-for-verify
212 (read-passwd "Password for Canlock: ")))
213 (if (or (not (stringp password)) (zerop (length password)))
214 (error "Password for Canlock is bad")
215 (when keys
216 (when id-for-key
217 (setq key-for-key (canlock-make-cancel-key id-for-key password))
218 (while (and keys (not match))
219 (setq match (string-equal key-for-key (pop keys)))))
220 (setq keys (if match "good" "bad")))
221 (setq match nil)
222 (when locks
223 (when id-for-lock
224 (setq key-for-lock
225 (base64-encode-string
226 (canlock-sha1 (canlock-make-cancel-key id-for-lock
227 password))))
228 (when (and locks (not match))
229 (setq match (string-equal key-for-lock (pop locks)))))
230 (setq locks (if match "good" "bad")))
231 (prog1
232 (when (member "bad" (list keys locks))
233 "bad")
234 (cond ((and keys locks)
235 (message "Cancel-Key is %s, Cancel-Lock is %s" keys locks))
236 (locks
237 (message "Cancel-Lock is %s" locks))
238 (keys
239 (message "Cancel-Key is %s" keys))))))))
241 (provide 'canlock)
243 ;;; canlock.el ends here