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, Gnus
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 (epa-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
68 cipher recipient-keys)
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))
81 (buffer-substring start end)
86 (epg-list-keys context recipient))
87 (unless (or recipient-keys
89 (format "No public key for %s; skip it? "
91 (error "No public key for %s" recipient))
93 (if pgg-encrypt-for-me
94 (cons pgg-default-user-id recipients)
97 pgg-epg-secret-key-id-list nil)
99 (while pgg-epg-secret-key-id-list
100 (pgg-remove-passphrase-from-cache (car pgg-epg-secret-key-id-list))
101 (setq pgg-epg-secret-key-id-list (cdr pgg-epg-secret-key-id-list)))
102 (signal (car error) (cdr error))))
104 (set-buffer (get-buffer-create pgg-output-buffer))
108 (defun pgg-epg-encrypt-symmetric-region (start end &optional passphrase)
109 "This function is for internal use only.
111 Encrypt the current region between START and END with symmetric cipher.
113 If optional PASSPHRASE is not specified, it will be obtained from the
114 passphrase cache or user."
115 (pgg-epg-encrypt-region start end nil))
117 (defun pgg-epg-decrypt-region (start end &optional passphrase)
118 "This function is for internal use only.
120 Decrypt the current region between START and END.
122 If optional PASSPHRASE is not specified, it will be obtained from the
123 passphrase cache or user."
124 (let ((context (epg-make-context))
125 (inhibit-redisplay t) ;Gnus users don't like flickering
127 (epg-context-set-armor context t)
128 (epg-context-set-textmode context pgg-text-mode)
129 (epg-context-set-passphrase-callback context #'pgg-epg-passphrase-callback)
131 (set-buffer (get-buffer-create pgg-output-buffer))
133 (set-buffer (get-buffer-create pgg-errors-buffer))
135 (condition-case error
137 (epg-decrypt-string context (buffer-substring start end))
138 pgg-epg-secret-key-id-list nil)
140 (while pgg-epg-secret-key-id-list
141 (pgg-remove-passphrase-from-cache (car pgg-epg-secret-key-id-list))
142 (setq pgg-epg-secret-key-id-list (cdr pgg-epg-secret-key-id-list)))
143 (signal (car error) (cdr error))))
144 (if (and pgg-text-mode
145 (fboundp 'decode-coding-string))
146 (setq plain (decode-coding-string plain 'raw-text)))
148 (set-buffer (get-buffer-create pgg-output-buffer))
152 (defun pgg-epg-sign-region (start end &optional cleartext passphrase)
153 "This function is for internal use only.
155 Make detached signature from text between START and END.
157 If optional PASSPHRASE is not specified, it will be obtained from the
158 passphrase cache or user."
159 (let ((context (epg-make-context))
160 (inhibit-redisplay t) ;Gnus users don't like flickering
162 (epg-context-set-armor context t)
163 (epg-context-set-textmode context pgg-text-mode)
164 (epg-context-set-passphrase-callback context #'pgg-epg-passphrase-callback)
165 (epg-context-set-signers
167 (list (car (epg-list-keys context pgg-default-user-id t))))
169 (set-buffer (get-buffer-create pgg-output-buffer))
171 (set-buffer (get-buffer-create pgg-errors-buffer))
173 (condition-case error
175 (epg-sign-string context
176 (buffer-substring start end)
180 pgg-epg-secret-key-id-list nil)
182 (while pgg-epg-secret-key-id-list
183 (pgg-remove-passphrase-from-cache (car pgg-epg-secret-key-id-list))
184 (setq pgg-epg-secret-key-id-list (cdr pgg-epg-secret-key-id-list)))
185 (signal (car error) (cdr error))))
187 (set-buffer (get-buffer-create pgg-output-buffer))
191 (defvar pgg-epg-signatures nil)
193 (defun pgg-epg-verify-region (start end &optional signature)
194 "This function is for internal use only.
196 Verify region between START and END as the detached signature SIGNATURE."
197 (let ((context (epg-make-context))
198 (inhibit-redisplay t)) ;Gnus users don't like flickering
199 (epg-context-set-armor context t)
200 (epg-context-set-textmode context pgg-text-mode)
202 (set-buffer (get-buffer-create pgg-output-buffer))
204 (set-buffer (get-buffer-create pgg-errors-buffer))
207 (epg-verify-string context
209 (insert-file-contents signature)
211 (buffer-substring start end))
212 (epg-verify-string context (buffer-substring start end)))
214 (set-buffer (get-buffer-create pgg-errors-buffer))
215 (make-local-variable 'pgg-epg-signatures)
216 (setq pgg-epg-signatures (epg-context-result-for context 'verify))
217 (insert (epg-verify-result-to-string pgg-epg-signatures)))
220 (defun pgg-epg-insert-key ()
221 "This function is for internal use only.
223 Insert public key at point."
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 (insert (epg-export-keys-to-string context pgg-default-user-id))))
236 (defun pgg-epg-snarf-keys-region (start end)
237 "This function is for internal use only.
239 Add all public keys in region between START and END to the keyring."
240 (let ((context (epg-make-context))
241 (inhibit-redisplay t) ;Gnus users don't like flickering
243 (epg-context-set-armor context t)
244 (epg-context-set-textmode context pgg-text-mode)
246 (set-buffer (get-buffer-create pgg-output-buffer))
248 (set-buffer (get-buffer-create pgg-errors-buffer))
250 (epg-import-keys-from-string context (buffer-substring start end))))
253 (autoload 'mml2015-gpg-pretty-print-fpr "mml2015"))
254 (defun mml2015-gpg-extract-signature-details ()
255 (if pgg-epg-signatures
256 (let* ((expired (eq (epg-signature-status (car pgg-epg-signatures))
258 (signer (cons (epg-signature-key-id (car pgg-epg-signatures))
259 (cdr (assoc (epg-signature-key-id
260 (car pgg-epg-signatures))
261 epg-user-id-alist))))
262 (fprint (epg-signature-fingerprint (car pgg-epg-signatures)))
264 (memq (epg-signature-validity (car pgg-epg-signatures))
265 '(marginal full ultimate))))
266 (cond ((and signer fprint)
268 (unless trust-good-enough-p
269 (concat "\nUntrusted, Fingerprint: "
270 (mml2015-gpg-pretty-print-fpr fprint)))
272 (format "\nWARNING: Signature from expired key (%s)"
275 "From unknown user")))
276 "From unknown user"))
278 (defun pgg-epg-lookup-key (string &optional type)
279 "Search keys associated with STRING."
280 (mapcar (lambda (key)
281 (epg-sub-key-id (car (epg-key-sub-key-list key))))
282 (epg-list-keys (epg-make-context) string (not (null type)))))
286 ;;; pgg-epg.el ends here