canlock.el 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. ;;; canlock.el --- functions for Cancel-Lock feature
  2. ;; Copyright (C) 1998-1999, 2001-2012 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 `canlock-password-for-
  31. ;; verify' is nil. Note that setting these options is a bit unsafe.
  32. ;;; Code:
  33. (eval-when-compile
  34. (require 'cl))
  35. (require 'sha1)
  36. (defvar mail-header-separator)
  37. (defgroup canlock nil
  38. "The Cancel-Lock feature."
  39. :group 'news)
  40. (defcustom canlock-password nil
  41. "Password to use when signing a Cancel-Lock or a Cancel-Key header."
  42. :type '(radio (const :format "Not specified " nil)
  43. (string :tag "Password"))
  44. :group 'canlock)
  45. (defcustom canlock-password-for-verify canlock-password
  46. "Password to use when verifying a Cancel-Lock or a Cancel-Key header."
  47. :type '(radio (const :format "Not specified " nil)
  48. (string :tag "Password"))
  49. :group 'canlock)
  50. (defcustom canlock-force-insert-header nil
  51. "If non-nil, insert a Cancel-Lock or a Cancel-Key header even if the
  52. buffer does not look like a news message."
  53. :type 'boolean
  54. :group 'canlock)
  55. (eval-when-compile
  56. (defmacro canlock-string-as-unibyte (string)
  57. "Return a unibyte string with the same individual bytes as STRING."
  58. (if (fboundp 'string-as-unibyte)
  59. (list 'string-as-unibyte string)
  60. string)))
  61. (defun canlock-sha1 (message)
  62. "Make a SHA-1 digest of MESSAGE as a unibyte string of length 20 bytes."
  63. (let (sha1-maximum-internal-length)
  64. (sha1 message nil nil 'binary)))
  65. (defun canlock-make-cancel-key (message-id password)
  66. "Make a Cancel-Key header."
  67. (when (> (length password) 20)
  68. (setq password (canlock-sha1 password)))
  69. (setq password (concat password (make-string (- 64 (length password)) 0)))
  70. (let ((ipad (mapconcat (lambda (byte)
  71. (char-to-string (logxor 54 byte)))
  72. password ""))
  73. (opad (mapconcat (lambda (byte)
  74. (char-to-string (logxor 92 byte)))
  75. password "")))
  76. (base64-encode-string
  77. (canlock-sha1
  78. (concat opad
  79. (canlock-sha1
  80. (concat ipad (canlock-string-as-unibyte message-id))))))))
  81. (defun canlock-narrow-to-header ()
  82. "Narrow the buffer to the head of the message."
  83. (let (case-fold-search)
  84. (narrow-to-region
  85. (goto-char (point-min))
  86. (goto-char (if (re-search-forward
  87. (format "^$\\|^%s$"
  88. (regexp-quote mail-header-separator))
  89. nil t)
  90. (match-beginning 0)
  91. (point-max))))))
  92. (defun canlock-delete-headers ()
  93. "Delete Cancel-Key or Cancel-Lock headers in the narrowed buffer."
  94. (let ((case-fold-search t))
  95. (goto-char (point-min))
  96. (while (re-search-forward "^Cancel-\\(Key\\|Lock\\):" nil t)
  97. (delete-region (match-beginning 0)
  98. (if (re-search-forward "^[^\t ]" nil t)
  99. (goto-char (match-beginning 0))
  100. (point-max))))))
  101. (defun canlock-fetch-fields (&optional key)
  102. "Return a list of the values of Cancel-Lock header.
  103. If KEY is non-nil, look for a Cancel-Key header instead. The buffer
  104. is expected to be narrowed to just the headers of the message."
  105. (let ((field (mail-fetch-field (if key "Cancel-Key" "Cancel-Lock")))
  106. fields rest
  107. (case-fold-search t))
  108. (when field
  109. (setq fields (split-string field "[\t\n\r ,]+"))
  110. (while fields
  111. (when (string-match "^sha1:" (setq field (pop fields)))
  112. (push (substring field 5) rest)))
  113. (nreverse rest))))
  114. (defun canlock-fetch-id-for-key ()
  115. "Return a Message-ID in Cancel, Supersedes or Replaces header.
  116. The buffer is expected to be narrowed to just the headers of the
  117. message."
  118. (or (let ((cancel (mail-fetch-field "Control")))
  119. (and cancel
  120. (string-match "^cancel[\t ]+\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
  121. cancel)
  122. (match-string 1 cancel)))
  123. (mail-fetch-field "Supersedes")
  124. (mail-fetch-field "Replaces")))
  125. ;;;###autoload
  126. (defun canlock-insert-header (&optional id-for-key id-for-lock password)
  127. "Insert a Cancel-Key and/or a Cancel-Lock header if possible."
  128. (let (news control key-for-key key-for-lock)
  129. (save-excursion
  130. (save-restriction
  131. (canlock-narrow-to-header)
  132. (when (setq news (or canlock-force-insert-header
  133. (mail-fetch-field "Newsgroups")))
  134. (unless id-for-key
  135. (setq id-for-key (canlock-fetch-id-for-key)))
  136. (if (and (setq control (mail-fetch-field "Control"))
  137. (string-match "^cancel[\t ]+<[^\t\n @<>]+@[^\t\n @<>]+>"
  138. control))
  139. (setq id-for-lock nil)
  140. (unless id-for-lock
  141. (setq id-for-lock (mail-fetch-field "Message-ID"))))
  142. (canlock-delete-headers)
  143. (goto-char (point-max))))
  144. (when news
  145. (if (not (or id-for-key id-for-lock))
  146. (message "There are no Message-ID(s)")
  147. (unless password
  148. (setq password (or canlock-password
  149. (read-passwd
  150. "Password for Canlock: "))))
  151. (if (or (not (stringp password)) (zerop (length password)))
  152. (message "Password for Canlock is bad")
  153. (setq key-for-key (when id-for-key
  154. (canlock-make-cancel-key
  155. id-for-key password))
  156. key-for-lock (when id-for-lock
  157. (canlock-make-cancel-key
  158. id-for-lock password)))
  159. (if (not (or key-for-key key-for-lock))
  160. (message "Couldn't insert Canlock header")
  161. (when key-for-key
  162. (insert "Cancel-Key: sha1:" key-for-key "\n"))
  163. (when key-for-lock
  164. (insert "Cancel-Lock: sha1:"
  165. (base64-encode-string (canlock-sha1 key-for-lock))
  166. "\n")))))))))
  167. ;;;###autoload
  168. (defun canlock-verify (&optional buffer)
  169. "Verify Cancel-Lock or Cancel-Key in BUFFER.
  170. If BUFFER is nil, the current buffer is assumed. Signal an error if
  171. it fails."
  172. (interactive)
  173. (let (keys locks errmsg id-for-key id-for-lock password
  174. key-for-key key-for-lock match)
  175. (save-excursion
  176. (when buffer
  177. (set-buffer buffer))
  178. (save-restriction
  179. (widen)
  180. (canlock-narrow-to-header)
  181. (setq keys (canlock-fetch-fields 'key)
  182. locks (canlock-fetch-fields))
  183. (if (not (or keys locks))
  184. (setq errmsg
  185. "There are neither Cancel-Lock nor Cancel-Key headers")
  186. (setq id-for-key (canlock-fetch-id-for-key)
  187. id-for-lock (mail-fetch-field "Message-ID"))
  188. (or id-for-key id-for-lock
  189. (setq errmsg "There are no Message-ID(s)")))))
  190. (if errmsg
  191. (error "%s" errmsg)
  192. (setq password (or canlock-password-for-verify
  193. (read-passwd "Password for Canlock: ")))
  194. (if (or (not (stringp password)) (zerop (length password)))
  195. (error "Password for Canlock is bad")
  196. (when keys
  197. (when id-for-key
  198. (setq key-for-key (canlock-make-cancel-key id-for-key password))
  199. (while (and keys (not match))
  200. (setq match (string-equal key-for-key (pop keys)))))
  201. (setq keys (if match "good" "bad")))
  202. (setq match nil)
  203. (when locks
  204. (when id-for-lock
  205. (setq key-for-lock
  206. (base64-encode-string
  207. (canlock-sha1 (canlock-make-cancel-key id-for-lock
  208. password))))
  209. (when (and locks (not match))
  210. (setq match (string-equal key-for-lock (pop locks)))))
  211. (setq locks (if match "good" "bad")))
  212. (prog1
  213. (when (member "bad" (list keys locks))
  214. "bad")
  215. (cond ((and keys locks)
  216. (message "Cancel-Key is %s, Cancel-Lock is %s" keys locks))
  217. (locks
  218. (message "Cancel-Lock is %s" locks))
  219. (keys
  220. (message "Cancel-Key is %s" keys))))))))
  221. (provide 'canlock)
  222. ;;; canlock.el ends here