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 (pgg-read-passphrase "GnuPG passphrase for symmetric encryption: ")
38 (format "GnuPG passphrase for %s: "
42 (if (eq pgg-gpg-key-id 'PIN)
46 (pgg-add-passphrase-to-cache key-id passphrase)
47 (copy-sequence passphrase)
48 (setq pgg-epg-secret-key-id-list
49 (cons key-id pgg-epg-secret-key-id-list))))))
51 (defun pgg-epg-encrypt-region (start end recipients &optional sign passphrase)
52 "This function is for internal use only.
54 Encrypt the current region between START and END.
56 If optional argument SIGN is non-nil, do a combined sign and encrypt.
58 If optional PASSPHRASE is not specified, it will be obtained from the
59 passphrase cache or user."
60 (let ((context (epg-make-context))
62 (epg-context-set-armor context t)
63 (epg-context-set-textmode context pgg-text-mode)
64 (epg-context-set-passphrase-callback context #'pgg-epg-passphrase-callback)
67 (epg-encrypt-string context
68 (buffer-substring start end)
69 (if pgg-encrypt-for-me
70 (cons pgg-default-user-id recipients)
74 (while pgg-epg-secret-key-id-list
75 (pgg-remove-passphrase-from-cache (car pgg-epg-secret-key-id-list))
76 (setq pgg-epg-secret-key-id-list (cdr pgg-epg-secret-key-id-list)))
77 (signal (car error) (cdr error))))
79 (set-buffer (get-buffer-create pgg-output-buffer))
84 (defun pgg-epg-encrypt-symmetric-region (start end &optional passphrase)
85 "This function is for internal use only.
87 Encrypt the current region between START and END with symmetric cipher.
89 If optional PASSPHRASE is not specified, it will be obtained from the
90 passphrase cache or user."
91 (pgg-epg-encrypt-region start end nil))
93 (defun pgg-epg-decrypt-region (start end &optional passphrase)
94 "This function is for internal use only.
96 Decrypt the current region between START and END.
98 If optional PASSPHRASE is not specified, it will be obtained from the
99 passphrase cache or user."
100 (let ((context (epg-make-context))
102 (epg-context-set-armor context t)
103 (epg-context-set-textmode context pgg-text-mode)
104 (epg-context-set-passphrase-callback context #'pgg-epg-passphrase-callback)
105 (condition-case error
106 (setq plain (epg-decrypt-string context (buffer-substring start end)))
108 (while pgg-epg-secret-key-id-list
109 (pgg-remove-passphrase-from-cache (car pgg-epg-secret-key-id-list))
110 (setq pgg-epg-secret-key-id-list (cdr pgg-epg-secret-key-id-list)))
111 (signal (car error) (cdr error))))
113 (set-buffer (get-buffer-create pgg-output-buffer))
118 (defun pgg-epg-sign-region (start end &optional cleartext passphrase)
119 "This function is for internal use only.
121 Make detached signature from text between START and END.
123 If optional PASSPHRASE is not specified, it will be obtained from the
124 passphrase cache or user."
125 (let ((context (epg-make-context))
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)
130 (condition-case error
132 (epg-sign-string context
133 (buffer-substring start end)
138 (while pgg-epg-secret-key-id-list
139 (pgg-remove-passphrase-from-cache (car pgg-epg-secret-key-id-list))
140 (setq pgg-epg-secret-key-id-list (cdr pgg-epg-secret-key-id-list)))
141 (signal (car error) (cdr error))))
143 (set-buffer (get-buffer-create pgg-output-buffer))
148 (defvar pgg-epg-signatures nil)
150 (defun pgg-epg-verify-region (start end &optional signature)
151 "This function is for internal use only.
153 Verify region between START and END as the detached signature SIGNATURE."
154 (let ((context (epg-make-context)))
155 (epg-context-set-armor context t)
156 (epg-context-set-textmode context pgg-text-mode)
158 (epg-verify-string context
160 (insert-file-contents signature)
162 (buffer-substring start end))
163 (epg-verify-string context (buffer-substring start end)))
165 (set-buffer (get-buffer-create pgg-errors-buffer))
166 (make-local-variable 'pgg-epg-signatures)
167 (setq pgg-epg-signatures (epg-context-result-for context 'verify))
169 (insert (epg-verify-result-to-string pgg-epg-signatures)))
172 (defun pgg-epg-insert-key ()
173 "This function is for internal use only.
175 Insert public key at point."
176 (let ((context (epg-make-context))
178 (epg-context-set-armor context t)
179 (epg-context-set-textmode context pgg-text-mode)
180 (insert (epg-export-keys context pgg-default-user-id))))
182 (defun pgg-epg-snarf-keys-region (start end)
183 "This function is for internal use only.
185 Add all public keys in region between START and END to the keyring."
186 (let ((context (epg-make-context))
188 (epg-context-set-armor context t)
189 (epg-context-set-textmode context pgg-text-mode)
190 (epg-import-keys context (buffer-substring start end))))
192 (defun mml2015-gpg-extract-signature-details ()
193 (if pgg-epg-signatures
194 (let* ((expired (eq (epg-signature-status (car pgg-epg-signatures))
196 (signer (cons (epg-signature-key-id (car pgg-epg-signatures))
197 (epg-signature-user-id (car pgg-epg-signatures))))
198 (fprint (epg-signature-fingerprint (car pgg-epg-signatures)))
200 (memq (epg-signature-validity (car pgg-epg-signatures))
201 '(marginal fully ultimate))))
202 (cond ((and signer fprint)
204 (unless trust-good-enough-p
205 (concat "\nUntrusted, Fingerprint: "
206 (mml2015-gpg-pretty-print-fpr fprint)))
208 (format "\nWARNING: Signature from expired key (%s)"
211 "From unknown user")))
212 "From unknown user"))
216 ;;; pgg-epg.el ends here