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-file context signature (buffer-substring start end) nil)
115       (epg-verify-string context (buffer-substring start end)))
116     (setq signature (reverse (epg-context-result-for context 'verify))
117           pointer signature)
118     (save-excursion
119       (set-buffer (get-buffer-create pgg-errors-buffer))
120       (make-local-variable 'pgg-epg-signature)
121       (setq pgg-epg-signature (car signature))
122       (erase-buffer)
123       (while pointer
124         (insert (format "%s: %s %s %s\n"
125                         (epg-signature-status (car pointer))
126                         (epg-signature-key-id (car pointer))
127                         (epg-signature-user-id (car pointer))
128                         (epg-signature-validity (car pointer))))
129         (setq pointer (cdr pointer))))
130     signature))
131
132 (defun pgg-epg-insert-key ()
133   "This function is for internal use only.
134
135 Insert public key at point."
136   (let ((context (epg-make-context))
137         pointer)
138     (epg-context-set-armor context t)
139     (epg-context-set-textmode context pgg-text-mode)
140     (insert (epg-export-keys context pgg-default-user-id))))
141
142 (defun pgg-epg-snarf-keys-region (start end)
143   "This function is for internal use only.
144
145 Add all public keys in region between START and END to the keyring."
146   (let ((context (epg-make-context))
147         pointer)
148     (epg-context-set-armor context t)
149     (epg-context-set-textmode context pgg-text-mode)
150     (epg-import-keys context (buffer-substring start end))))
151
152 (defun mml2015-gpg-extract-signature-details ()
153   (if pgg-epg-signature
154       (let* ((expired (eq (epg-signature-status pgg-epg-signature)
155                           'key-expired))
156              (signer (cons (epg-signature-key-id pgg-epg-signature)
157                            (epg-signature-user-id pgg-epg-signature)))
158              (fprint (epg-signature-fingerprint pgg-epg-signature))
159              (trust-good-enough-p
160               (memq (epg-signature-validity pgg-epg-signature)
161                     '(marginal fully ultimate))))
162         (cond ((and signer fprint)
163                (concat (cdr signer)
164                        (unless trust-good-enough-p
165                          (concat "\nUntrusted, Fingerprint: "
166                                  (mml2015-gpg-pretty-print-fpr fprint)))
167                        (when expired
168                          (format "\nWARNING: Signature from expired key (%s)"
169                                  (car signer)))))
170               (t
171                "From unknown user")))
172     "From unknown user"))
173
174 (provide 'pgg-epg)
175
176 ;;; pgg-epg.el ends here