Fixed.
[elisp/epg.git] / pgg-epg.el
1 ;;; pgg-epg.el --- Gnus/PGG backend of EasyPG.
2 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
3 ;;   2005, 2006 Free Software Foundation, Inc.
4 ;; Copyright (C) 2006 Daiki Ueno
5
6 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Keywords: PGP, GnuPG
8
9 ;; This file is part of EasyPG.
10
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Code:
27
28 (require 'epg)
29 (eval-when-compile (require 'pgg))
30
31 (defun pgg-epg-encrypt-region (start end recipients &optional sign passphrase)
32   "This function is for internal use only.
33
34 Encrypt the current region between START and END.
35
36 If optional argument SIGN is non-nil, do a combined sign and encrypt.
37
38 If optional PASSPHRASE is not specified, it will be obtained from the
39 passphrase cache or user."
40   (let ((context (epg-make-context))
41         cipher)
42     (epg-context-set-armor context t)
43     (epg-context-set-textmode context pgg-text-mode)
44     (setq cipher (epg-encrypt-string context (buffer-substring start end)
45                                      (if pgg-encrypt-for-me
46                                          (cons pgg-default-user-id recipients)
47                                        recipients)
48                                      sign t))
49     (save-excursion
50       (set-buffer (get-buffer-create pgg-output-buffer))
51       (erase-buffer)
52       (insert cipher))
53     t))
54
55 (defun pgg-epg-encrypt-symmetric-region (start end &optional passphrase)
56   "This function is for internal use only.
57
58 Encrypt the current region between START and END with symmetric cipher.
59
60 If optional PASSPHRASE is not specified, it will be obtained from the
61 passphrase cache or user."
62   (pgg-epg-encrypt-region start end nil))
63
64 (defun pgg-epg-decrypt-region (start end &optional passphrase)
65   "This function is for internal use only.
66
67 Decrypt the current region between START and END.
68
69 If optional PASSPHRASE is not specified, it will be obtained from the
70 passphrase cache or user."
71   (let ((context (epg-make-context))
72         plain)
73     (epg-context-set-armor context t)
74     (epg-context-set-textmode context pgg-text-mode)
75     (setq plain (epg-decrypt-string context (buffer-substring start end)))
76     (save-excursion
77       (set-buffer (get-buffer-create pgg-output-buffer))
78       (erase-buffer)
79       (insert plain))
80     t))
81
82 (defun pgg-epg-sign-region (start end &optional cleartext passphrase)
83   "This function is for internal use only.
84
85 Make detached signature from text between START and END.
86
87 If optional PASSPHRASE is not specified, it will be obtained from the
88 passphrase cache or user."
89   (let ((context (epg-make-context))
90         signature)
91     (epg-context-set-armor context t)
92     (epg-context-set-textmode context pgg-text-mode)
93     (setq signature (epg-sign-string context (buffer-substring start end)
94                                      (if cleartext
95                                          'clearsign
96                                        'detached)))
97     (save-excursion
98       (set-buffer (get-buffer-create pgg-output-buffer))
99       (erase-buffer)
100       (insert signature))
101     t))
102
103 (defvar pgg-epg-signature nil)
104
105 (defun pgg-epg-verify-region (start end &optional signature)
106   "This function is for internal use only.
107
108 Verify region between START and END as the detached signature SIGNATURE."
109   (let ((context (epg-make-context))
110         pointer)
111     (epg-context-set-armor context t)
112     (epg-context-set-textmode context pgg-text-mode)
113     (if signature
114         (epg-verify-string context
115                            (with-temp-buffer
116                              (insert-file-contents signature)
117                              (buffer-string))
118                            (buffer-substring start end))
119       (epg-verify-string context (buffer-substring start end)))
120     (setq signature (reverse (epg-context-result-for context 'verify))
121           pointer signature)
122     (save-excursion
123       (set-buffer (get-buffer-create pgg-errors-buffer))
124       (make-local-variable 'pgg-epg-signature)
125       (setq pgg-epg-signature (car signature))
126       (erase-buffer)
127       (while pointer
128         (insert (format "%s: %s %s %s\n"
129                         (epg-signature-status (car pointer))
130                         (epg-signature-key-id (car pointer))
131                         (epg-signature-user-id (car pointer))
132                         (epg-signature-validity (car pointer))))
133         (setq pointer (cdr pointer))))
134     signature))
135
136 (defun pgg-epg-insert-key ()
137   "This function is for internal use only.
138
139 Insert public key at point."
140   (let ((context (epg-make-context))
141         pointer)
142     (epg-context-set-armor context t)
143     (epg-context-set-textmode context pgg-text-mode)
144     (insert (epg-export-keys context pgg-default-user-id))))
145
146 (defun pgg-epg-snarf-keys-region (start end)
147   "This function is for internal use only.
148
149 Add all public keys in region between START and END to the keyring."
150   (let ((context (epg-make-context))
151         pointer)
152     (epg-context-set-armor context t)
153     (epg-context-set-textmode context pgg-text-mode)
154     (epg-import-keys context (buffer-substring start end))))
155
156 (defun mml2015-gpg-extract-signature-details ()
157   (if pgg-epg-signature
158       (let* ((expired (eq (epg-signature-status pgg-epg-signature)
159                           'key-expired))
160              (signer (cons (epg-signature-key-id pgg-epg-signature)
161                            (epg-signature-user-id pgg-epg-signature)))
162              (fprint (epg-signature-fingerprint pgg-epg-signature))
163              (trust-good-enough-p
164               (memq (epg-signature-validity pgg-epg-signature)
165                     '(marginal fully ultimate))))
166         (cond ((and signer fprint)
167                (concat (cdr signer)
168                        (unless trust-good-enough-p
169                          (concat "\nUntrusted, Fingerprint: "
170                                  (mml2015-gpg-pretty-print-fpr fprint)))
171                        (when expired
172                          (format "\nWARNING: Signature from expired key (%s)"
173                                  (car signer)))))
174               (t
175                "From unknown user")))
176     "From unknown user"))
177
178 (provide 'pgg-epg)
179
180 ;;; pgg-epg.el ends here