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.
29 (eval-when-compile (require 'pgg))
31 (defvar pgg-epg-secret-key-id-list nil)
33 (defun pgg-epg-passphrase-callback (key-id ignore)
35 (epg-passphrase-callback-function key-id nil)
36 (let* ((entry (assoc key-id epg-user-id-alist))
39 (format "GnuPG passphrase for %s: "
47 (pgg-add-passphrase-to-cache key-id passphrase)
48 (setq pgg-epg-secret-key-id-list
49 (cons key-id pgg-epg-secret-key-id-list))
50 (copy-sequence passphrase)))))
52 (defun pgg-epg-encrypt-region (start end recipients &optional sign passphrase)
53 "This function is for internal use only.
55 Encrypt the current region between START and END.
57 If optional argument SIGN is non-nil, do a combined sign and encrypt.
59 If optional PASSPHRASE is not specified, it will be obtained from the
60 passphrase cache or user."
61 (let ((context (epg-make-context))
63 (epg-context-set-armor context t)
64 (epg-context-set-textmode context pgg-text-mode)
65 (epg-context-set-passphrase-callback context #'pgg-epg-passphrase-callback)
68 (epg-encrypt-string context
69 (buffer-substring start end)
70 (if pgg-encrypt-for-me
71 (cons pgg-default-user-id recipients)
74 pgg-epg-secret-key-id-list nil)
76 (while pgg-epg-secret-key-id-list
77 (pgg-remove-passphrase-from-cache (car pgg-epg-secret-key-id-list))
78 (setq pgg-epg-secret-key-id-list (cdr pgg-epg-secret-key-id-list)))
79 (signal (car error) (cdr error))))
81 (set-buffer (get-buffer-create pgg-output-buffer))
86 (defun pgg-epg-encrypt-symmetric-region (start end &optional passphrase)
87 "This function is for internal use only.
89 Encrypt the current region between START and END with symmetric cipher.
91 If optional PASSPHRASE is not specified, it will be obtained from the
92 passphrase cache or user."
93 (pgg-epg-encrypt-region start end nil))
95 (defun pgg-epg-decrypt-region (start end &optional passphrase)
96 "This function is for internal use only.
98 Decrypt the current region between START and END.
100 If optional PASSPHRASE is not specified, it will be obtained from the
101 passphrase cache or user."
102 (let ((context (epg-make-context))
104 (epg-context-set-armor context t)
105 (epg-context-set-textmode context pgg-text-mode)
106 (epg-context-set-passphrase-callback context #'pgg-epg-passphrase-callback)
107 (condition-case error
108 (setq plain (epg-decrypt-string context (buffer-substring start end))
109 pgg-epg-secret-key-id-list nil)
111 (while pgg-epg-secret-key-id-list
112 (pgg-remove-passphrase-from-cache (car pgg-epg-secret-key-id-list))
113 (setq pgg-epg-secret-key-id-list (cdr pgg-epg-secret-key-id-list)))
114 (signal (car error) (cdr error))))
116 (set-buffer (get-buffer-create pgg-output-buffer))
121 (defun pgg-epg-sign-region (start end &optional cleartext passphrase)
122 "This function is for internal use only.
124 Make detached signature from text between START and END.
126 If optional PASSPHRASE is not specified, it will be obtained from the
127 passphrase cache or user."
128 (let ((context (epg-make-context))
130 (epg-context-set-armor context t)
131 (epg-context-set-textmode context pgg-text-mode)
132 (epg-context-set-passphrase-callback context #'pgg-epg-passphrase-callback)
133 (condition-case error
135 (epg-sign-string context
136 (buffer-substring start end)
140 pgg-epg-secret-key-id-list nil)
142 (while pgg-epg-secret-key-id-list
143 (pgg-remove-passphrase-from-cache (car pgg-epg-secret-key-id-list))
144 (setq pgg-epg-secret-key-id-list (cdr pgg-epg-secret-key-id-list)))
145 (signal (car error) (cdr error))))
147 (set-buffer (get-buffer-create pgg-output-buffer))
152 (defvar pgg-epg-signatures nil)
154 (defun pgg-epg-verify-region (start end &optional signature)
155 "This function is for internal use only.
157 Verify region between START and END as the detached signature SIGNATURE."
158 (let ((context (epg-make-context)))
159 (epg-context-set-armor context t)
160 (epg-context-set-textmode context pgg-text-mode)
162 (epg-verify-string context
164 (insert-file-contents signature)
166 (buffer-substring start end))
167 (epg-verify-string context (buffer-substring start end)))
169 (set-buffer (get-buffer-create pgg-errors-buffer))
170 (make-local-variable 'pgg-epg-signatures)
171 (setq pgg-epg-signatures (epg-context-result-for context 'verify))
173 (insert (epg-verify-result-to-string pgg-epg-signatures)))
176 (defun pgg-epg-insert-key ()
177 "This function is for internal use only.
179 Insert public key at point."
180 (let ((context (epg-make-context))
182 (epg-context-set-armor context t)
183 (epg-context-set-textmode context pgg-text-mode)
184 (insert (epg-export-keys context pgg-default-user-id))))
186 (defun pgg-epg-snarf-keys-region (start end)
187 "This function is for internal use only.
189 Add all public keys in region between START and END to the keyring."
190 (let ((context (epg-make-context))
192 (epg-context-set-armor context t)
193 (epg-context-set-textmode context pgg-text-mode)
194 (epg-import-keys context (buffer-substring start end))))
196 (defun mml2015-gpg-extract-signature-details ()
197 (if pgg-epg-signatures
198 (let* ((expired (eq (epg-signature-status (car pgg-epg-signatures))
200 (signer (cons (epg-signature-key-id (car pgg-epg-signatures))
201 (epg-signature-user-id (car pgg-epg-signatures))))
202 (fprint (epg-signature-fingerprint (car pgg-epg-signatures)))
204 (memq (epg-signature-validity (car pgg-epg-signatures))
205 '(marginal fully ultimate))))
206 (cond ((and signer fprint)
208 (unless trust-good-enough-p
209 (concat "\nUntrusted, Fingerprint: "
210 (mml2015-gpg-pretty-print-fpr fprint)))
212 (format "\nWARNING: Signature from expired key (%s)"
215 "From unknown user")))
216 "From unknown user"))
220 ;;; pgg-epg.el ends here