Revision: emacs@sv.gnu.org/gnus--devo--0--patch-135
[gnus] / lisp / canlock.el
1 ;;; canlock.el --- functions for Cancel-Lock feature
2
3 ;; Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006 Free Software Foundation, Inc.
5
6 ;; Author: Katsumi Yamaoka <yamaoka@jpl.org>
7 ;; Keywords: news, cancel-lock, hmac, sha1, rfc2104
8
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
23
24 ;;; Commentary:
25
26 ;; Canlock is a library for generating and verifying Cancel-Lock and/or
27 ;; Cancel-Key header in news articles.  This is used to protect articles
28 ;; from rogue cancel, supersede or replace attacks.  The method is based
29 ;; on draft-ietf-usefor-cancel-lock-01.txt which was released on November
30 ;; 3rd 1998.  For instance, you can add Cancel-Lock (and possibly Cancel-
31 ;; Key) header in a news article by using a hook which will be evaluated
32 ;; just before sending an article as follows:
33 ;;
34 ;; (add-hook '*e**a*e-header-hook 'canlock-insert-header t)
35 ;;
36 ;; Verifying Cancel-Lock is mainly a function of news servers, however,
37 ;; you can verify your own article using the command `canlock-verify' in
38 ;; the (raw) article buffer.  You will be prompted for the password for
39 ;; each time if the option `canlock-password' or `canlock-password-for-
40 ;; verify' is nil.  Note that setting these options is a bit unsafe.
41
42 ;;; Code:
43
44 (eval-when-compile
45   (require 'cl))
46
47 (require 'sha1)
48
49 (defvar mail-header-separator)
50
51 (defgroup canlock nil
52   "The Cancel-Lock feature."
53   :group 'news)
54
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)
60
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)
66
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)
72
73 (eval-when-compile
74   (defmacro canlock-string-as-unibyte (string)
75     "Return a unibyte string with the same individual bytes as STRING."
76     (if (fboundp 'string-as-unibyte)
77         (list 'string-as-unibyte string)
78       string)))
79
80 (defun canlock-sha1 (message)
81   "Make a SHA-1 digest of MESSAGE as a unibyte string of length 20 bytes."
82   (let (sha1-maximum-internal-length)
83     (sha1 message nil nil 'binary)))
84
85 (defun canlock-make-cancel-key (message-id password)
86   "Make a Cancel-Key header."
87   (when (> (length password) 20)
88     (setq password (canlock-sha1 password)))
89   (setq password (concat password (make-string (- 64 (length password)) 0)))