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
6 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Keywords: PGP, GnuPG
9 ;; This file is part of EasyPG.
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)
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.
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.
28 ;; To use, add (setq pgg-scheme 'epg) to your ~/.gnus.
33 (eval-when-compile (require 'pgg))
35 (defvar pgg-epg-secret-key-id-list nil)
37 (defun pgg-epg-passphrase-callback (context key-id ignore)
39 (epg-passphrase-callback-function context key-id nil)
40 (let* ((entry (assoc key-id epg-user-id-alist))
43 (format "GnuPG passphrase for %s: "
51 (pgg-add-passphrase-to-cache key-id passphrase)
52 (setq pgg-epg-secret-key-id-list
53 (cons key-id pgg-epg-secret-key-id-list))
54 (copy-sequence passphrase)))))
56 (defvar inhibit-redisplay)
57 (defun pgg-epg-encrypt-region (start end recipients &optional sign passphrase)
58 "This function is for internal use only.
60 Encrypt the current region between START and END.
62 If optional argument SIGN is non-nil, do a combined sign and encrypt.
64 If optional PASSPHRASE is not specified, it will be obtained from the
65 passphrase cache or user."
66 (let ((context (epg-make-context))
67 (inhibit-redisplay t) ;Gnus users don't like flickering
69 (epg-context-set-armor context t)
70 (epg-context-set-textmode context pgg-text-mode)
71 (epg-context-set-passphrase-callback context #'pgg-epg-passphrase-callback)
73 (set-buffer (get-buffer-create pgg-output-buffer))
75 (set-buffer (get-buffer-create pgg-errors-buffer))
79 (epg-encrypt-string context
80 (buffer-substring start end)
83 (car (epg-list-keys recipient)))
84 (if pgg-encrypt-for-me
85 (cons pgg-default-user-id recipients)
88 pgg-epg-secret-key-id-list nil)
90 (while pgg-epg-secret-key-id-list
91 (pgg-remove-passphrase-from-cache (car pgg-epg-secret-key-id-list))
92 (setq pgg-epg-secret-key-id-list (cdr pgg-epg-secret-key-id-list)))
93 (signal (car error) (cdr error))))
95 (set-buffer (get-buffer-create pgg-output-buffer))
99 (defun pgg-epg-encrypt-symmetric-region (start end &optional passphrase)
100 "This function is for internal use only.
102 Encrypt the current region between START and END with symmetric cipher.
104 If optional PASSPHRASE is not specified, it will be obtained from the
105 passphrase cache or user."
106 (pgg-epg-encrypt-region start end nil))
108 (defun pgg-epg-decrypt-region (start end &optional passphrase)
109 "This function is for internal use only.
111 Decrypt the current region between START and END.
113 If optional PASSPHRASE is not specified, it will be obtained from the
114 passphrase cache or user."
115 (let ((context (epg-make-context))
116 (inhibit-redisplay t) ;Gnus users don't like flickering
118 (epg-context-set-armor context t)
119 (epg-context-set-textmode context pgg-text-mode)
120 (epg-context-set-passphrase-callback context #'pgg-epg-passphrase-callback)
122 (set-buffer (get-buffer-create pgg-output-buffer))
124 (set-buffer (get-buffer-create pgg-errors-buffer))
126 (condition-case error
127 (setq plain (epg-decrypt-string context (buffer-substring start end))
128 pgg-epg-secret-key-id-list nil)
130 (while pgg-epg-secret-key-id-list
131 (pgg-remove-passphrase-from-cache (car pgg-epg-secret-key-id-list))
132 (setq pgg-epg-secret-key-id-list (cdr pgg-epg-secret-key-id-list)))
133 (signal (car error) (cdr error))))
135 (set-buffer (get-buffer-create pgg-output-buffer))
139 (defun pgg-epg-sign-region (start end &optional cleartext passphrase)
140 "This function is for internal use only.
142 Make detached signature from text between START and END.
144 If optional PASSPHRASE is not specified, it will be obtained from the
145 passphrase cache or user."
146 (let ((context (epg-make-context))
147 (inhibit-redisplay t) ;Gnus users don't like flickering
149 (epg-context-set-armor context t)
150 (epg-context-set-textmode context pgg-text-mode)
151 (epg-context-set-passphrase-callback context #'pgg-epg-passphrase-callback)
153 (set-buffer (get-buffer-create pgg-output-buffer))
155 (set-buffer (get-buffer-create pgg-errors-buffer))
157 (condition-case error
159 (epg-sign-string context
160 (buffer-substring start end)
164 pgg-epg-secret-key-id-list nil)
166 (while pgg-epg-secret-key-id-list
167 (pgg-remove-passphrase-from-cache (car pgg-epg-secret-key-id-list))
168 (setq pgg-epg-secret-key-id-list (cdr pgg-epg-secret-key-id-list)))
169 (signal (car error) (cdr error))))
171 (set-buffer (get-buffer-create pgg-output-buffer))
175 (defvar pgg-epg-signatures nil)
177 (defun pgg-epg-verify-region (start end &optional signature)
178 "This function is for internal use only.
180 Verify region between START and END as the detached signature SIGNATURE."
181 (let ((context (epg-make-context))
182 (inhibit-redisplay t)) ;Gnus users don't like flickering
183 (epg-context-set-armor context t)
184 (epg-context-set-textmode context pgg-text-mode)
186 (set-buffer (get-buffer-create pgg-output-buffer))
188 (set-buffer (get-buffer-create pgg-errors-buffer))
191 (epg-verify-string context
193 (insert-file-contents signature)
195 (buffer-substring start end))
196 (epg-verify-string context (buffer-substring start end)))
198 (set-buffer (get-buffer-create pgg-errors-buffer))
199 (make-local-variable 'pgg-epg-signatures)
200 (setq pgg-epg-signatures (epg-context-result-for context 'verify))
201 (insert (epg-verify-result-to-string pgg-epg-signatures)))
204 (defun pgg-epg-insert-key ()
205 "This function is for internal use only.
207 Insert public key at point."
208 (let ((context (epg-make-context))
209 (inhibit-redisplay t) ;Gnus users don't like flickering
211 (epg-context-set-armor context t)
212 (epg-context-set-textmode context pgg-text-mode)
214 (set-buffer (get-buffer-create pgg-output-buffer))
216 (set-buffer (get-buffer-create pgg-errors-buffer))
218 (insert (epg-export-keys-to-string context pgg-default-user-id))))
220 (defun pgg-epg-snarf-keys-region (start end)
221 "This function is for internal use only.
223 Add all public keys in region between START and END to the keyring."
224 (let ((context (epg-make-context))
225 (inhibit-redisplay t) ;Gnus users don't like flickering
227 (epg-context-set-armor context t)
228 (epg-context-set-textmode context pgg-text-mode)
230 (set-buffer (get-buffer-create pgg-output-buffer))
232 (set-buffer (get-buffer-create pgg-errors-buffer))
234 (epg-import-keys-from-string context (buffer-substring start end))))
237 (autoload 'mml2015-gpg-pretty-print-fpr "mml2015"))
238 (defun mml2015-gpg-extract-signature-details ()
239 (if pgg-epg-signatures
240 (let* ((expired (eq (epg-signature-status (car pgg-epg-signatures))
242 (signer (cons (epg-signature-key-id (car pgg-epg-signatures))
243 (cdr (assoc (epg-signature-key-id
244 (car pgg-epg-signatures))
245 epg-user-id-alist))))
246 (fprint (epg-signature-fingerprint (car pgg-epg-signatures)))
248 (memq (epg-signature-validity (car pgg-epg-signatures))
249 '(marginal fully ultimate))))
250 (cond ((and signer fprint)
252 (unless trust-good-enough-p
253 (concat "\nUntrusted, Fingerprint: "
254 (mml2015-gpg-pretty-print-fpr fprint)))
256 (format "\nWARNING: Signature from expired key (%s)"
259 "From unknown user")))
260 "From unknown user"))
264 ;;; pgg-epg.el ends here