canlock.el 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. ;;; canlock.el --- functions for Cancel-Lock feature
  2. ;; Copyright (C) 1998-1999, 2001-2015 Free Software Foundation, Inc.
  3. ;; Author: Katsumi Yamaoka <yamaoka@jpl.org>
  4. ;; Keywords: news, cancel-lock, hmac, sha1, rfc2104
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Canlock is a library for generating and verifying Cancel-Lock and/or
  18. ;; Cancel-Key header in news articles. This is used to protect articles
  19. ;; from rogue cancel, supersede or replace attacks. The method is based
  20. ;; on draft-ietf-usefor-cancel-lock-01.txt which was released on November
  21. ;; 3rd 1998. For instance, you can add Cancel-Lock (and possibly Cancel-
  22. ;; Key) header in a news article by using a hook which will be evaluated
  23. ;; just before sending an article as follows:
  24. ;;
  25. ;; (add-hook '*e**a*e-header-hook 'canlock-insert-header t)
  26. ;;
  27. ;; Verifying Cancel-Lock is mainly a function of news servers, however,
  28. ;; you can verify your own article using the command `canlock-verify' in
  29. ;; the (raw) article buffer. You will be prompted for the password for
  30. ;; each time if the option `canlock-password' or
  31. ;; `canlock-password-for-verify' is nil. Note that setting these
  32. ;; options is a bit unsafe.
  33. ;;; Code:
  34. (eval-when-compile
  35. (require 'cl))
  36. (require 'sha1)
  37. (defvar mail-header-separator)
  38. (defgroup canlock nil
  39. "The Cancel-Lock feature."
  40. :group 'news)
  41. (defcustom canlock-password nil
  42. "Password to use when signing a Cancel-Lock or a Cancel-Key header."
  43. :type '(radio (const :format "Not specified " nil)
  44. (string :tag "Password"))
  45. :group 'canlock)
  46. (defcustom canlock-password-for-verify canlock-password
  47. "Password to use when verifying a Cancel-Lock or a Cancel-Key header."
  48. :type '(radio (const :format "Not specified " nil)
  49. (string :tag "Password"))
  50. :group 'canlock)
  51. (defcustom canlock-force-insert-header nil
  52. "If non-nil, insert a Cancel-Lock or a Cancel-Key header even if the
  53. buffer does not look like a news message."
  54. :type 'boolean
  55. :group 'canlock)
  56. (eval-when-compile
  57. (defmacro canlock-string-as-unibyte (string)
  58. "Return a unibyte string with the same individual bytes as STRING."
  59. (if (fboundp 'string-as-unibyte)
  60. (list 'string-as-unibyte string)
  61. string)))
  62. (defun canlock-sha1 (message)
  63. "Make a SHA-1 digest of MESSAGE as a unibyte string of length 20 bytes."
  64. (let (sha1-maximum-internal-length)
  65. (sha1 message nil nil 'binary)))
  66. (defun canlock-make-cancel-key (message-id password)
  67. "Make a Cancel-Key header."
  68. (when (> (length password) 20)
  69. (setq password (canlock-sha1 password)))
  70. (setq password (concat password (make-string (- 64 (length password)) 0)))
  71. (let ((ipad (mapconcat (lambda (byte)
  72. (char-to-string (logxor 54 byte)))
  73. password ""))
  74. (opad (mapconcat (lambda (byte)
  75. (char-to-string (logxor 92 byte)))
  76. password "")))
  77. (base64-encode-string
  78. (canlock-sha1
  79. (concat opad
  80. (canlock-sha1
  81. (concat ipad (canlock-string-as-unibyte message-id))))))))
  82. (defun canlock-narrow-to-header ()
  83. "Narrow the buffer to the head of the message."
  84. (let (case-fold-search)
  85. (narrow-to-region
  86. (goto-char (point-min))
  87. (goto-char (if (re-search-forward
  88. (format "^$\\|^%s$"
  89. (regexp-quote mail-header-separator))
  90. nil t)
  91. (match-beginning 0)
  92. (point-max))))))
  93. (defun canlock-delete-headers ()
  94. "Delete Cancel-Key or Cancel-Lock headers in the narrowed buffer."
  95. (let ((case-fold-search t))
  96. (goto-char (point-min))
  97. (while (re-search-forward "^Cancel-\\(Key\\|Lock\\):" nil t)
  98. (delete-region (match-beginning 0)
  99. (if (re-search-forward "^[^\t ]" nil t)
  100. (goto-char (match-beginning 0))
  101. (point-max))))))
  102. (defun canlock-fetch-fields (&optional key)
  103. "Return a list of the values of Cancel-Lock header.
  104. If KEY is non-nil, look for a Cancel-Key header instead. The buffer
  105. is expected to be narrowed to just the headers of the message."
  106. (let ((field (mail-fetch-field (if key "Cancel-Key" "Cancel-Lock")))
  107. fields rest
  108. (case-fold-search t))
  109. (when field
  110. (setq fields (split-string field "[\t\n\r ,]+"))
  111. (while fields
  112. (when (string-match "^sha1:" (setq field (pop fields)))
  113. (push (substring field 5) rest)))
  114. (nreverse rest))))
  115. (defun canlock-fetch-id-for-key ()
  116. "Return a Message-ID in Cancel, Supersedes or Replaces header.
  117. The buffer is expected to be narrowed to just the headers of the
  118. message."
  119. (or (let ((cancel (mail-fetch-field "Control")))
  120. (and cancel
  121. (string-match "^cancel[\t ]+\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
  122. cancel)
  123. (match-string 1 cancel)))
  124. (mail-fetch-field "Supersedes")
  125. (mail-fetch-field "Replaces")))
  126. ;;;###autoload
  127. (defun canlock-insert-header (&optional id-for-key id-for-lock password)
  128. "Insert a Cancel-Key and/or a Cancel-Lock header if possible."
  129. (let (news control key-for-key key-for-lock)
  130. (save-excursion
  131. (save-restriction
  132. (canlock-narrow-to-header)
  133. (when (setq news (or canlock-force-insert-header
  134. (mail-fetch-field "Newsgroups")))
  135. (unless id-for-key
  136. (setq id-for-key (canlock-fetch-id-for-key)))
  137. (if (and (setq control (mail-fetch-field "Control"))
  138. (string-match "^cancel[\t ]+<[^\t\n @<>]+@[^\t\n @<>]+>"
  139. control))
  140. (setq id-for-lock nil)
  141. (unless id-for-lock
  142. (setq id-for-lock (mail-fetch-field "Message-ID"))))
  143. (canlock-delete-headers)
  144. (goto-char (point-max))))
  145. (when news
  146. (if (not (or id-for-key id-for-lock))
  147. (message "There are no Message-ID(s)")
  148. (unless password
  149. (setq password (or canlock-password
  150. (read-passwd
  151. "Password for Canlock: "))))
  152. (if (or (not (stringp password)) (zerop (length password)))
  153. (message "Password for Canlock is bad")
  154. (setq key-for-key (when id-for-key
  155. (canlock-make-cancel-key
  156. id-for-key password))
  157. key-for-lock (when id-for-lock
  158. (canlock-make-cancel-key
  159. id-for-lock password)))
  160. (if (not (or key-for-key key-for-lock))
  161. (message "Couldn't insert Canlock header")
  162. (when key-for-key
  163. (insert "Cancel-Key: sha1:" key-for-key "\n"))
  164. (when key-for-lock
  165. (insert "Cancel-Lock: sha1:"
  166. (base64-encode-string (canlock-sha1 key-for-lock))
  167. "\n")))))))))
  168. ;;;###autoload
  169. (defun canlock-verify (&optional buffer)
  170. "Verify Cancel-Lock or Cancel-Key in BUFFER.
  171. If BUFFER is nil, the current buffer is assumed. Signal an error if
  172. it fails."
  173. (interactive)
  174. (let (keys locks errmsg id-for-key id-for-lock password
  175. key-for-key key-for-lock match)
  176. (save-excursion
  177. (when buffer
  178. (set-buffer buffer))
  179. (save-restriction
  180. (widen)
  181. (canlock-narrow-to-header)
  182. (setq keys (canlock-fetch-fields 'key)
  183. locks (canlock-fetch-fields))
  184. (if (not (or keys locks))
  185. (setq errmsg
  186. "There are neither Cancel-Lock nor Cancel-Key headers")
  187. (setq id-for-key (canlock-fetch-id-for-key)
  188. id-for-lock (mail-fetch-field "Message-ID"))
  189. (or id-for-key id-for-lock
  190. (setq errmsg "There are no Message-ID(s)")))))
  191. (if errmsg
  192. (error "%s" errmsg)
  193. (setq password (or canlock-password-for-verify
  194. (read-passwd "Password for Canlock: ")))
  195. (if (or (not (stringp password)) (zerop (length password)))
  196. (error "Password for Canlock is bad")
  197. (when keys
  198. (when id-for-key
  199. (setq key-for-key (canlock-make-cancel-key id-for-key password))
  200. (while (and keys (not match))
  201. (setq match (string-equal key-for-key (pop keys)))))
  202. (setq keys (if match "good" "bad")))
  203. (setq match nil)
  204. (when locks
  205. (when id-for-lock
  206. (setq key-for-lock
  207. (base64-encode-string
  208. (canlock-sha1 (canlock-make-cancel-key id-for-lock
  209. password))))
  210. (when (and locks (not match))
  211. (setq match (string-equal key-for-lock (pop locks)))))
  212. (setq locks (if match "good" "bad")))
  213. (prog1
  214. (when (member "bad" (list keys locks))
  215. "bad")
  216. (cond ((and keys locks)
  217. (message "Cancel-Key is %s, Cancel-Lock is %s" keys locks))
  218. (locks
  219. (message "Cancel-Lock is %s" locks))
  220. (keys
  221. (message "Cancel-Key is %s" keys))))))))
  222. (provide 'canlock)
  223. ;;; canlock.el ends here