(eieio-persistent-save): Don't ignore `file' arg (bug#20972)
[emacs.git] / lisp / epg.el
blobb3d39bb85053f72e230261368716ac3abe6f9b0c
1 ;;; epg.el --- the EasyPG Library -*- lexical-binding: t -*-
2 ;; Copyright (C) 1999-2000, 2002-2015 Free Software Foundation, Inc.
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: PGP, GnuPG
6 ;; Version: 1.0.0
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Code:
25 (require 'epg-config)
26 (eval-when-compile (require 'cl-lib))
28 (defvar epg-user-id nil
29 "GnuPG ID of your default identity.")
31 (defvar epg-user-id-alist nil
32 "An alist mapping from key ID to user ID.")
34 (defvar epg-last-status nil)
35 (defvar epg-read-point nil)
36 (defvar epg-process-filter-running nil)
37 (defvar epg-pending-status-list nil)
38 (defvar epg-key-id nil)
39 (defvar epg-context nil)
40 (defvar epg-debug-buffer nil)
41 (defvar epg-agent-file nil)
42 (defvar epg-agent-mtime nil)
44 ;; from gnupg/include/cipher.h
45 (defconst epg-cipher-algorithm-alist
46 '((0 . "NONE")
47 (1 . "IDEA")
48 (2 . "3DES")
49 (3 . "CAST5")
50 (4 . "BLOWFISH")
51 (7 . "AES")
52 (8 . "AES192")
53 (9 . "AES256")
54 (10 . "TWOFISH")
55 (11 . "CAMELLIA128")
56 (12 . "CAMELLIA256")
57 (110 . "DUMMY")))
59 ;; from gnupg/include/cipher.h
60 (defconst epg-pubkey-algorithm-alist
61 '((1 . "RSA")
62 (2 . "RSA_E")
63 (3 . "RSA_S")
64 (16 . "ELGAMAL_E")
65 (17 . "DSA")
66 (20 . "ELGAMAL")))
68 ;; from gnupg/include/cipher.h
69 (defconst epg-digest-algorithm-alist
70 '((1 . "MD5")
71 (2 . "SHA1")
72 (3 . "RIPEMD160")
73 (8 . "SHA256")
74 (9 . "SHA384")
75 (10 . "SHA512")
76 (11 . "SHA224")))
78 ;; from gnupg/include/cipher.h
79 (defconst epg-compress-algorithm-alist
80 '((0 . "NONE")
81 (1 . "ZIP")
82 (2 . "ZLIB")
83 (3 . "BZIP2")))
85 (defconst epg-invalid-recipients-reason-alist
86 '((0 . "No specific reason given")
87 (1 . "Not Found")
88 (2 . "Ambiguous specification")
89 (3 . "Wrong key usage")
90 (4 . "Key revoked")
91 (5 . "Key expired")
92 (6 . "No CRL known")
93 (7 . "CRL too old")
94 (8 . "Policy mismatch")
95 (9 . "Not a secret key")
96 (10 . "Key not trusted")))
98 (defconst epg-delete-problem-reason-alist
99 '((1 . "No such key")
100 (2 . "Must delete secret key first")
101 (3 . "Ambiguous specification")))
103 (defconst epg-import-ok-reason-alist
104 '((0 . "Not actually changed")
105 (1 . "Entirely new key")
106 (2 . "New user IDs")
107 (4 . "New signatures")
108 (8 . "New subkeys")
109 (16 . "Contains private key")))
111 (defconst epg-import-problem-reason-alist
112 '((0 . "No specific reason given")
113 (1 . "Invalid Certificate")
114 (2 . "Issuer Certificate missing")
115 (3 . "Certificate Chain too long")
116 (4 . "Error storing certificate")))
118 (defconst epg-no-data-reason-alist
119 '((1 . "No armored data")
120 (2 . "Expected a packet but did not found one")
121 (3 . "Invalid packet found, this may indicate a non OpenPGP message")
122 (4 . "Signature expected but not found")))
124 (defconst epg-unexpected-reason-alist nil)
126 (defvar epg-key-validity-alist
127 '((?o . unknown)
128 (?i . invalid)
129 (?d . disabled)
130 (?r . revoked)
131 (?e . expired)
132 (?- . none)
133 (?q . undefined)
134 (?n . never)
135 (?m . marginal)
136 (?f . full)
137 (?u . ultimate)))
139 (defvar epg-key-capability-alist
140 '((?e . encrypt)
141 (?s . sign)
142 (?c . certify)
143 (?a . authentication)
144 (?D . disabled)))
146 (defvar epg-new-signature-type-alist
147 '((?D . detached)
148 (?C . clear)
149 (?S . normal)))
151 (defvar epg-dn-type-alist
152 '(("1.2.840.113549.1.9.1" . "EMail")
153 ("2.5.4.12" . "T")
154 ("2.5.4.42" . "GN")
155 ("2.5.4.4" . "SN")
156 ("0.2.262.1.10.7.20" . "NameDistinguisher")
157 ("2.5.4.16" . "ADDR")
158 ("2.5.4.15" . "BC")
159 ("2.5.4.13" . "D")
160 ("2.5.4.17" . "PostalCode")
161 ("2.5.4.65" . "Pseudo")
162 ("2.5.4.5" . "SerialNumber")))
164 (defvar epg-prompt-alist nil)
166 (define-error 'epg-error "GPG error")
168 (cl-defstruct (epg-data
169 (:constructor nil)
170 (:constructor epg-make-data-from-file (file))
171 (:constructor epg-make-data-from-string (string))
172 (:copier nil)
173 (:predicate nil))
174 (file nil :read-only t)
175 (string nil :read-only t))
177 (defmacro epg--gv-nreverse (place)
178 (gv-letplace (getter setter) place
179 (funcall setter `(nreverse ,getter))))
181 (cl-defstruct (epg-context
182 (:constructor nil)
183 (:constructor epg-context--make
184 (protocol &optional armor textmode include-certs
185 cipher-algorithm digest-algorithm
186 compress-algorithm
187 &aux
188 (program
189 (pcase protocol
190 (`OpenPGP epg-gpg-program)
191 (`CMS epg-gpgsm-program)
192 (_ (signal 'epg-error
193 (list "unknown protocol" protocol)))))))
194 (:copier nil)
195 (:predicate nil))
196 protocol
197 program
198 (home-directory epg-gpg-home-directory)
199 armor
200 textmode
201 include-certs
202 cipher-algorithm
203 digest-algorithm
204 compress-algorithm
205 (passphrase-callback (list #'epg-passphrase-callback-function))
206 progress-callback
207 edit-callback
208 signers
209 sig-notations
210 process
211 output-file
212 result
213 operation
214 pinentry-mode
215 (error-output "")
216 error-buffer)
218 ;; This is not an alias, just so we can mark it as autoloaded.
219 ;;;###autoload
220 (defun epg-make-context (&optional protocol armor textmode include-certs
221 cipher-algorithm digest-algorithm
222 compress-algorithm)
223 "Return a context object."
224 (epg-context--make (or protocol 'OpenPGP)
225 armor textmode include-certs
226 cipher-algorithm digest-algorithm
227 compress-algorithm))
229 (defun epg-context-set-armor (context armor)
230 "Specify if the output should be ASCII armored in CONTEXT."
231 (declare (obsolete setf "25.1"))
232 (setf (epg-context-armor context) armor))
234 (defun epg-context-set-textmode (context textmode)
235 "Specify if canonical text mode should be used in CONTEXT."
236 (declare (obsolete setf "25.1"))
237 (setf (epg-context-textmode context) textmode))
239 (defun epg-context-set-passphrase-callback (context
240 passphrase-callback)
241 "Set the function used to query passphrase.
243 PASSPHRASE-CALLBACK is either a function, or a cons-cell whose
244 car is a function and cdr is a callback data.
246 The function gets three arguments: the context, the key-id in
247 question, and the callback data (if any).
249 The callback may not be called if you use GnuPG 2.x, which relies
250 on the external program called `gpg-agent' for passphrase query.
251 If you really want to intercept passphrase query, consider
252 installing GnuPG 1.x _along with_ GnuPG 2.x, which does passphrase
253 query by itself and Emacs can intercept them."
254 ;; (declare (obsolete setf "25.1"))
255 (setf (epg-context-passphrase-callback context)
256 (if (functionp passphrase-callback)
257 (list passphrase-callback)
258 passphrase-callback)))
260 (defun epg-context-set-progress-callback (context
261 progress-callback)
262 "Set the function which handles progress update.
264 PROGRESS-CALLBACK is either a function, or a cons-cell whose
265 car is a function and cdr is a callback data.
267 The function gets six arguments: the context, the operation
268 description, the character to display a progress unit, the
269 current amount done, the total amount to be done, and the
270 callback data (if any)."
271 (setf (epg-context-progress-callback context)
272 (if (functionp progress-callback)
273 (list progress-callback)
274 progress-callback)))
276 (defun epg-context-set-signers (context signers)
277 "Set the list of key-id for signing."
278 (declare (obsolete setf "25.1"))
279 (setf (epg-context-signers context) signers))
281 (cl-defstruct (epg-signature
282 (:constructor nil)
283 (:constructor epg-make-signature
284 (status &optional key-id))
285 (:copier nil)
286 (:predicate nil))
287 status
288 key-id
289 validity
290 fingerprint
291 creation-time
292 expiration-time
293 pubkey-algorithm
294 digest-algorithm
295 class
296 version
297 notations)
299 (cl-defstruct (epg-new-signature
300 (:constructor nil)
301 (:constructor epg-make-new-signature
302 (type pubkey-algorithm digest-algorithm
303 class creation-time fingerprint))
304 (:copier nil)
305 (:predicate nil))
306 (type nil :read-only t)
307 (pubkey-algorithm nil :read-only t)
308 (digest-algorithm nil :read-only t)
309 (class nil :read-only t)
310 (creation-time nil :read-only t)
311 (fingerprint nil :read-only t))
313 (cl-defstruct (epg-key
314 (:constructor nil)
315 (:constructor epg-make-key (owner-trust))
316 (:copier nil)
317 (:predicate nil))
318 (owner-trust nil :read-only t)
319 sub-key-list user-id-list)
321 (cl-defstruct (epg-sub-key
322 (:constructor nil)
323 (:constructor epg-make-sub-key
324 (validity capability secret-p algorithm length id
325 creation-time expiration-time))
326 (:copier nil)
327 (:predicate nil))
328 validity capability secret-p algorithm length id
329 creation-time expiration-time fingerprint)
331 (cl-defstruct (epg-user-id
332 (:constructor nil)
333 (:constructor epg-make-user-id (validity string))
334 (:copier nil)
335 (:predicate nil))
336 validity string signature-list)
338 (cl-defstruct (epg-key-signature
339 (:constructor nil)
340 (:constructor epg-make-key-signature
341 (validity pubkey-algorithm key-id creation-time
342 expiration-time user-id class
343 exportable-p))
344 (:copier nil)
345 (:predicate nil))
346 validity pubkey-algorithm key-id creation-time
347 expiration-time user-id class
348 exportable-p)
350 (cl-defstruct (epg-sig-notation
351 (:constructor nil)
352 (:constructor epg-make-sig-notation
353 (name value &optional human-readable critical))
354 (:copier nil)
355 (:predicate nil))
356 name value human-readable critical)
358 (cl-defstruct (epg-import-status
359 (:constructor nil)
360 (:constructor epg-make-import-status
361 (fingerprint
362 &optional reason new user-id signature sub-key secret))
363 (:copier nil)
364 (:predicate nil))
365 fingerprint reason new user-id signature sub-key secret)
367 (cl-defstruct (epg-import-result
368 (:constructor nil)
369 (:constructor epg-make-import-result
370 (considered no-user-id imported imported-rsa
371 unchanged new-user-ids new-sub-keys
372 new-signatures new-revocations
373 secret-read secret-imported
374 secret-unchanged not-imported
375 imports))
376 (:copier nil)
377 (:predicate nil))
378 considered no-user-id imported imported-rsa
379 unchanged new-user-ids new-sub-keys
380 new-signatures new-revocations
381 secret-read secret-imported
382 secret-unchanged not-imported
383 imports)
385 (defun epg-context-result-for (context name)
386 "Return the result of CONTEXT associated with NAME."
387 (cdr (assq name (epg-context-result context))))
389 (defun epg-context-set-result-for (context name value)
390 "Set the result of CONTEXT associated with NAME to VALUE."
391 (let* ((result (epg-context-result context))
392 (entry (assq name result)))
393 (if entry
394 (setcdr entry value)
395 (setf (epg-context-result context) (cons (cons name value) result)))))
397 (defun epg-signature-to-string (signature)
398 "Convert SIGNATURE to a human readable string."
399 (let* ((user-id (cdr (assoc (epg-signature-key-id signature)
400 epg-user-id-alist)))
401 (pubkey-algorithm (epg-signature-pubkey-algorithm signature))
402 (key-id (epg-signature-key-id signature)))
403 (concat
404 (cond ((eq (epg-signature-status signature) 'good)
405 "Good signature from ")
406 ((eq (epg-signature-status signature) 'bad)
407 "Bad signature from ")
408 ((eq (epg-signature-status signature) 'expired)
409 "Expired signature from ")
410 ((eq (epg-signature-status signature) 'expired-key)
411 "Signature made by expired key ")
412 ((eq (epg-signature-status signature) 'revoked-key)
413 "Signature made by revoked key ")
414 ((eq (epg-signature-status signature) 'no-pubkey)
415 "No public key for "))
416 key-id
417 (if user-id
418 (concat " "
419 (if (stringp user-id)
420 user-id
421 (epg-decode-dn user-id)))
423 (if (epg-signature-validity signature)
424 (format " (trust %s)" (epg-signature-validity signature))
426 (if (epg-signature-creation-time signature)
427 (format-time-string " created at %Y-%m-%dT%T%z"
428 (epg-signature-creation-time signature))
430 (if pubkey-algorithm
431 (concat " using "
432 (or (cdr (assq pubkey-algorithm epg-pubkey-algorithm-alist))
433 (format "(unknown algorithm %d)" pubkey-algorithm)))
434 ""))))
436 (defun epg-verify-result-to-string (verify-result)
437 "Convert VERIFY-RESULT to a human readable string."
438 (mapconcat #'epg-signature-to-string verify-result "\n"))
440 (defun epg-new-signature-to-string (new-signature)
441 "Convert NEW-SIGNATURE to a human readable string."
442 (concat
443 (cond ((eq (epg-new-signature-type new-signature) 'detached)
444 "Detached signature ")
445 ((eq (epg-new-signature-type new-signature) 'clear)
446 "Cleartext signature ")
448 "Signature "))
449 (cdr (assq (epg-new-signature-pubkey-algorithm new-signature)
450 epg-pubkey-algorithm-alist))
452 (cdr (assq (epg-new-signature-digest-algorithm new-signature)
453 epg-digest-algorithm-alist))
455 (format "%02X " (epg-new-signature-class new-signature))
456 (epg-new-signature-fingerprint new-signature)))
458 (defun epg-import-result-to-string (import-result)
459 "Convert IMPORT-RESULT to a human readable string."
460 (concat (format "Total number processed: %d\n"
461 (epg-import-result-considered import-result))
462 (if (> (epg-import-result-not-imported import-result) 0)
463 (format " skipped new keys: %d\n"
464 (epg-import-result-not-imported import-result)))
465 (if (> (epg-import-result-no-user-id import-result) 0)
466 (format " w/o user IDs: %d\n"
467 (epg-import-result-no-user-id import-result)))
468 (if (> (epg-import-result-imported import-result) 0)
469 (concat (format " imported: %d"
470 (epg-import-result-imported import-result))
471 (if (> (epg-import-result-imported-rsa import-result) 0)
472 (format " (RSA: %d)"
473 (epg-import-result-imported-rsa
474 import-result)))
475 "\n"))
476 (if (> (epg-import-result-unchanged import-result) 0)
477 (format " unchanged: %d\n"
478 (epg-import-result-unchanged import-result)))
479 (if (> (epg-import-result-new-user-ids import-result) 0)
480 (format " new user IDs: %d\n"
481 (epg-import-result-new-user-ids import-result)))
482 (if (> (epg-import-result-new-sub-keys import-result) 0)
483 (format " new subkeys: %d\n"
484 (epg-import-result-new-sub-keys import-result)))
485 (if (> (epg-import-result-new-signatures import-result) 0)
486 (format " new signatures: %d\n"
487 (epg-import-result-new-signatures import-result)))
488 (if (> (epg-import-result-new-revocations import-result) 0)
489 (format " new key revocations: %d\n"
490 (epg-import-result-new-revocations import-result)))
491 (if (> (epg-import-result-secret-read import-result) 0)
492 (format " secret keys read: %d\n"
493 (epg-import-result-secret-read import-result)))
494 (if (> (epg-import-result-secret-imported import-result) 0)
495 (format " secret keys imported: %d\n"
496 (epg-import-result-secret-imported import-result)))
497 (if (> (epg-import-result-secret-unchanged import-result) 0)
498 (format " secret keys unchanged: %d\n"
499 (epg-import-result-secret-unchanged import-result)))))
501 (defun epg-error-to-string (error)
502 (cond
503 ((eq (car error) 'exit)
504 "Exit")
505 ((eq (car error) 'quit)
506 "Canceled")
507 ((eq (car error) 'no-data)
508 (let ((entry (assq (cdr error) epg-no-data-reason-alist)))
509 (if entry
510 (format "No data (%s)" (downcase (cdr entry)))
511 "No data")))
512 ((eq (car error) 'unexpected)
513 (let ((entry (assq (cdr error) epg-unexpected-reason-alist)))
514 (if entry
515 (format "Unexpected (%s)" (downcase (cdr entry)))
516 "Unexpected")))
517 ((eq (car error) 'bad-armor)
518 "Bad armor")
519 ((memq (car error) '(invalid-recipient invalid-signer))
520 (concat
521 (if (eq (car error) 'invalid-recipient)
522 "Unusable public key"
523 "Unusable secret key")
524 (let ((entry (assq 'requested (cdr error))))
525 (if entry
526 (format ": %s" (cdr entry))
527 ": <unknown>"))
528 (let ((entry (assq 'reason (cdr error))))
529 (if (and entry
530 (> (cdr entry) 0) ;no specific reason given
531 (setq entry (assq (cdr entry)
532 epg-invalid-recipients-reason-alist)))
533 (format " (%s)" (downcase (cdr entry)))
534 ""))))
535 ((eq (car error) 'no-pubkey)
536 (format "No public key: %s" (cdr error)))
537 ((eq (car error) 'no-seckey)
538 (format "No secret key: %s" (cdr error)))
539 ((eq (car error) 'no-recipients)
540 "No recipients")
541 ((eq (car error) 'no-signers)
542 "No signers")
543 ((eq (car error) 'delete-problem)
544 (let ((entry (assq (cdr error) epg-delete-problem-reason-alist)))
545 (if entry
546 (format "Delete problem (%s)" (downcase (cdr entry)))
547 "Delete problem")))
548 ((eq (car error) 'key-not-created)
549 "Key not created")))
551 (defun epg-errors-to-string (errors)
552 (mapconcat #'epg-error-to-string errors "; "))
554 (defun epg--start (context args)
555 "Start `epg-gpg-program' in a subprocess with given ARGS."
556 (if (and (epg-context-process context)
557 (eq (process-status (epg-context-process context)) 'run))
558 (error "%s is already running in this context"
559 (epg-context-program context)))
560 (let* ((agent-info (getenv "GPG_AGENT_INFO"))
561 (args (append (list "--no-tty"
562 "--status-fd" "1"
563 "--yes")
564 (if (and (not (eq (epg-context-protocol context) 'CMS))
565 (string-match ":" (or agent-info "")))
566 '("--use-agent"))
567 (if (and (not (eq (epg-context-protocol context) 'CMS))
568 (epg-context-progress-callback context))
569 '("--enable-progress-filter"))
570 (if (epg-context-home-directory context)
571 (list "--homedir"
572 (epg-context-home-directory context)))
573 (unless (eq (epg-context-protocol context) 'CMS)
574 '("--command-fd" "0"))
575 (if (epg-context-armor context) '("--armor"))
576 (if (epg-context-textmode context) '("--textmode"))
577 (if (epg-context-output-file context)
578 (list "--output" (epg-context-output-file context)))
579 (if (epg-context-pinentry-mode context)
580 (list "--pinentry-mode"
581 (symbol-name (epg-context-pinentry-mode
582 context))))
583 args))
584 (process-environment process-environment)
585 (buffer (generate-new-buffer " *epg*"))
586 error-process
587 process
588 terminal-name
589 agent-file
590 (agent-mtime '(0 0 0 0)))
591 ;; Set GPG_TTY and TERM for pinentry-curses. Note that we can't
592 ;; use `terminal-name' here to get the real pty name for the child
593 ;; process, though /dev/fd/0" is not portable.
594 (unless (memq system-type '(ms-dos windows-nt))
595 (with-temp-buffer
596 (condition-case nil
597 (when (= (call-process "tty" "/dev/fd/0" t) 0)
598 (delete-char -1)
599 (setq terminal-name (buffer-string)))
600 (file-error))))
601 (when terminal-name
602 (setq process-environment
603 (cons (concat "GPG_TTY=" terminal-name)
604 (cons "TERM=xterm" process-environment))))
605 (setq process-environment
606 (cons (format "INSIDE_EMACS=%s,epg" emacs-version)
607 process-environment))
608 ;; Record modified time of gpg-agent socket to restore the Emacs
609 ;; frame on text terminal in `epg-wait-for-completion'.
610 ;; See
611 ;; <http://lists.gnu.org/archive/html/emacs-devel/2007-02/msg00755.html>
612 ;; for more details.
613 (when (and agent-info (string-match "\\(.*\\):[0-9]+:[0-9]+" agent-info))
614 (setq agent-file (match-string 1 agent-info)
615 agent-mtime (or (nth 5 (file-attributes agent-file)) '(0 0 0 0))))
616 (if epg-debug
617 (save-excursion
618 (unless epg-debug-buffer
619 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
620 (set-buffer epg-debug-buffer)
621 (goto-char (point-max))
622 (insert (if agent-info
623 (format "GPG_AGENT_INFO=%s\n" agent-info)
624 "GPG_AGENT_INFO is not set\n")
625 (format "%s %s\n"
626 (epg-context-program context)
627 (mapconcat #'identity args " ")))))
628 (with-current-buffer buffer
629 (if (fboundp 'set-buffer-multibyte)
630 (set-buffer-multibyte nil))
631 (make-local-variable 'epg-last-status)
632 (setq epg-last-status nil)
633 (make-local-variable 'epg-read-point)
634 (setq epg-read-point (point-min))
635 (make-local-variable 'epg-process-filter-running)
636 (setq epg-process-filter-running nil)
637 (make-local-variable 'epg-pending-status-list)
638 (setq epg-pending-status-list nil)
639 (make-local-variable 'epg-key-id)
640 (setq epg-key-id nil)
641 (make-local-variable 'epg-context)
642 (setq epg-context context)
643 (make-local-variable 'epg-agent-file)
644 (setq epg-agent-file agent-file)
645 (make-local-variable 'epg-agent-mtime)
646 (setq epg-agent-mtime agent-mtime))
647 (setq error-process
648 (make-pipe-process :name "epg-error"
649 :buffer (generate-new-buffer " *epg-error*")
650 ;; Suppress "XXX finished" line.
651 :sentinel #'ignore
652 :noquery t))
653 (setf (epg-context-error-buffer context) (process-buffer error-process))
654 (with-file-modes 448
655 (setq process (make-process :name "epg"
656 :buffer buffer
657 :command (cons (epg-context-program context)
658 args)
659 :connection-type 'pipe
660 :coding '(binary . binary)
661 :filter #'epg--process-filter
662 :stderr error-process
663 :noquery t)))
664 (setf (epg-context-process context) process)))
666 (defun epg--process-filter (process input)
667 (if epg-debug
668 (with-current-buffer
669 (or epg-debug-buffer
670 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
671 (goto-char (point-max))
672 (insert input)))
673 (if (buffer-live-p (process-buffer process))
674 (with-current-buffer (process-buffer process)
675 (save-excursion
676 (goto-char (point-max))
677 (insert input)
678 (unless epg-process-filter-running
679 (let ((epg-process-filter-running t))
680 (goto-char epg-read-point)
681 (beginning-of-line)
682 (while (looking-at ".*\n") ;the input line finished
683 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
684 (let ((status (match-string 1))
685 (string (match-string 2))
686 symbol)
687 (if (member status epg-pending-status-list)
688 (setq epg-pending-status-list nil))
689 ;; When editing a key, delegate all interaction
690 ;; to edit-callback.
691 (if (eq (epg-context-operation epg-context) 'edit-key)
692 (funcall (car (epg-context-edit-callback
693 epg-context))
694 epg-context
695 status
696 string
697 (cdr (epg-context-edit-callback
698 epg-context)))
699 ;; Otherwise call epg--status-STATUS function.
700 (setq symbol (intern-soft (concat "epg--status-"
701 status)))
702 (if (and symbol
703 (fboundp symbol))
704 (funcall symbol epg-context string)))
705 (setq epg-last-status (cons status string))))
706 (forward-line)
707 (setq epg-read-point (point)))))))))
709 (defun epg-read-output (context)
710 "Read the output file CONTEXT and return the content as a string."
711 (with-temp-buffer
712 (if (fboundp 'set-buffer-multibyte)
713 (set-buffer-multibyte nil))
714 (if (file-exists-p (epg-context-output-file context))
715 (let ((coding-system-for-read 'binary))
716 (insert-file-contents (epg-context-output-file context))
717 (buffer-string)))))
719 (defun epg-wait-for-status (context status-list)
720 "Wait until one of elements in STATUS-LIST arrives."
721 (with-current-buffer (process-buffer (epg-context-process context))
722 (setq epg-pending-status-list status-list)
723 (while (and (eq (process-status (epg-context-process context)) 'run)
724 epg-pending-status-list)
725 (accept-process-output (epg-context-process context) 1))
726 (if epg-pending-status-list
727 (epg-context-set-result-for
728 context 'error
729 (cons '(exit)
730 (epg-context-result-for context 'error))))))
732 (defun epg-wait-for-completion (context)
733 "Wait until the `epg-gpg-program' process completes."
734 (while (eq (process-status (epg-context-process context)) 'run)
735 (accept-process-output (epg-context-process context) 1))
736 ;; This line is needed to run the process-filter right now.
737 (sleep-for 0.1)
738 ;; Restore Emacs frame on text terminal, when pinentry-curses has terminated.
739 (if (with-current-buffer (process-buffer (epg-context-process context))
740 (and epg-agent-file
741 (> (float-time (or (nth 5 (file-attributes epg-agent-file))
742 '(0 0 0 0)))
743 (float-time epg-agent-mtime))))
744 (redraw-frame))
745 (epg-context-set-result-for
746 context 'error
747 (nreverse (epg-context-result-for context 'error)))
748 (setf (epg-context-error-output context)
749 (with-current-buffer (epg-context-error-buffer context)
750 (buffer-string))))
752 (defun epg-reset (context)
753 "Reset the CONTEXT."
754 (if (and (epg-context-process context)
755 (buffer-live-p (process-buffer (epg-context-process context))))
756 (kill-buffer (process-buffer (epg-context-process context))))
757 (if (buffer-live-p (epg-context-error-buffer context))
758 (kill-buffer (epg-context-error-buffer context)))
759 (setf (epg-context-process context) nil)
760 (setf (epg-context-edit-callback context) nil))
762 (defun epg-delete-output-file (context)
763 "Delete the output file of CONTEXT."
764 (if (and (epg-context-output-file context)
765 (file-exists-p (epg-context-output-file context)))
766 (delete-file (epg-context-output-file context))))
768 (eval-and-compile
769 (if (fboundp 'decode-coding-string)
770 (defalias 'epg--decode-coding-string 'decode-coding-string)
771 (defalias 'epg--decode-coding-string 'identity)))
773 (defun epg--status-USERID_HINT (_context string)
774 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
775 (let* ((key-id (match-string 1 string))
776 (user-id (match-string 2 string))
777 (entry (assoc key-id epg-user-id-alist)))
778 (condition-case nil
779 (setq user-id (epg--decode-coding-string
780 (epg--decode-percent-escape user-id)
781 'utf-8))
782 (error))
783 (if entry
784 (setcdr entry user-id)
785 (setq epg-user-id-alist (cons (cons key-id user-id)
786 epg-user-id-alist))))))
788 (defun epg--status-NEED_PASSPHRASE (_context string)
789 (if (string-match "\\`\\([^ ]+\\)" string)
790 (setq epg-key-id (match-string 1 string))))
792 (defun epg--status-NEED_PASSPHRASE_SYM (_context _string)
793 (setq epg-key-id 'SYM))
795 (defun epg--status-NEED_PASSPHRASE_PIN (_context _string)
796 (setq epg-key-id 'PIN))
798 (eval-and-compile
799 (if (fboundp 'clear-string)
800 (defalias 'epg--clear-string 'clear-string)
801 (defun epg--clear-string (string)
802 (fillarray string 0))))
804 (eval-and-compile
805 (if (fboundp 'encode-coding-string)
806 (defalias 'epg--encode-coding-string 'encode-coding-string)
807 (defalias 'epg--encode-coding-string 'identity)))
809 (defun epg--status-GET_HIDDEN (context string)
810 (when (and epg-key-id
811 (string-match "\\`passphrase\\." string))
812 (unless (epg-context-passphrase-callback context)
813 (error "passphrase-callback not set"))
814 (let (inhibit-quit
815 passphrase
816 passphrase-with-new-line
817 encoded-passphrase-with-new-line)
818 (unwind-protect
819 (condition-case nil
820 (progn
821 (setq passphrase
822 (funcall
823 (car (epg-context-passphrase-callback context))
824 context
825 epg-key-id
826 (cdr (epg-context-passphrase-callback context))))
827 (when passphrase
828 (setq passphrase-with-new-line (concat passphrase "\n"))
829 (epg--clear-string passphrase)
830 (setq passphrase nil)
831 (if epg-passphrase-coding-system
832 (progn
833 (setq encoded-passphrase-with-new-line
834 (epg--encode-coding-string
835 passphrase-with-new-line
836 (coding-system-change-eol-conversion
837 epg-passphrase-coding-system 'unix)))
838 (epg--clear-string passphrase-with-new-line)
839 (setq passphrase-with-new-line nil))
840 (setq encoded-passphrase-with-new-line
841 passphrase-with-new-line
842 passphrase-with-new-line nil))
843 (process-send-string (epg-context-process context)
844 encoded-passphrase-with-new-line)))
845 (quit
846 (epg-context-set-result-for
847 context 'error
848 (cons '(quit)
849 (epg-context-result-for context 'error)))
850 (delete-process (epg-context-process context))))
851 (if passphrase
852 (epg--clear-string passphrase))
853 (if passphrase-with-new-line
854 (epg--clear-string passphrase-with-new-line))
855 (if encoded-passphrase-with-new-line
856 (epg--clear-string encoded-passphrase-with-new-line))))))
858 (defun epg--prompt-GET_BOOL (_context string)
859 (let ((entry (assoc string epg-prompt-alist)))
860 (y-or-n-p (if entry (cdr entry) (concat string "? ")))))
862 (defun epg--prompt-GET_BOOL-untrusted_key.override (_context _string)
863 (y-or-n-p (if (and (equal (car epg-last-status) "USERID_HINT")
864 (string-match "\\`\\([^ ]+\\) \\(.*\\)"
865 (cdr epg-last-status)))
866 (let* ((key-id (match-string 1 (cdr epg-last-status)))
867 (user-id (match-string 2 (cdr epg-last-status)))
868 (entry (assoc key-id epg-user-id-alist)))
869 (if entry
870 (setq user-id (cdr entry)))
871 (format "Untrusted key %s %s. Use anyway? " key-id user-id))
872 "Use untrusted key anyway? ")))
874 (defun epg--status-GET_BOOL (context string)
875 (let (inhibit-quit)
876 (condition-case nil
877 (if (funcall (or (intern-soft (concat "epg--prompt-GET_BOOL-" string))
878 #'epg--prompt-GET_BOOL)
879 context string)
880 (process-send-string (epg-context-process context) "y\n")
881 (process-send-string (epg-context-process context) "n\n"))
882 (quit
883 (epg-context-set-result-for
884 context 'error
885 (cons '(quit)
886 (epg-context-result-for context 'error)))
887 (delete-process (epg-context-process context))))))
889 (defun epg--status-GET_LINE (context string)
890 (let ((entry (assoc string epg-prompt-alist))
891 inhibit-quit)
892 (condition-case nil
893 (process-send-string (epg-context-process context)
894 (concat (read-string
895 (if entry
896 (cdr entry)
897 (concat string ": ")))
898 "\n"))
899 (quit
900 (epg-context-set-result-for
901 context 'error
902 (cons '(quit)
903 (epg-context-result-for context 'error)))
904 (delete-process (epg-context-process context))))))
906 (defun epg--status-*SIG (context status string)
907 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
908 (let* ((key-id (match-string 1 string))
909 (user-id (match-string 2 string))
910 (entry (assoc key-id epg-user-id-alist)))
911 (epg-context-set-result-for
912 context
913 'verify
914 (cons (epg-make-signature status key-id)
915 (epg-context-result-for context 'verify)))
916 (condition-case nil
917 (if (eq (epg-context-protocol context) 'CMS)
918 (setq user-id (epg-dn-from-string user-id))
919 (setq user-id (epg--decode-coding-string
920 (epg--decode-percent-escape user-id)
921 'utf-8)))
922 (error))
923 (if entry
924 (setcdr entry user-id)
925 (setq epg-user-id-alist
926 (cons (cons key-id user-id) epg-user-id-alist))))
927 (epg-context-set-result-for
928 context
929 'verify
930 (cons (epg-make-signature status)
931 (epg-context-result-for context 'verify)))))
933 (defun epg--status-GOODSIG (context string)
934 (epg--status-*SIG context 'good string))
936 (defun epg--status-EXPSIG (context string)
937 (epg--status-*SIG context 'expired string))
939 (defun epg--status-EXPKEYSIG (context string)
940 (epg--status-*SIG context 'expired-key string))
942 (defun epg--status-REVKEYSIG (context string)
943 (epg--status-*SIG context 'revoked-key string))
945 (defun epg--status-BADSIG (context string)
946 (epg--status-*SIG context 'bad string))
948 (defun epg--status-NO_PUBKEY (context string)
949 (if (eq (epg-context-operation context) 'verify)
950 (let ((signature (car (epg-context-result-for context 'verify))))
951 (if (and signature
952 (eq (epg-signature-status signature) 'error)
953 (equal (epg-signature-key-id signature) string))
954 (setf (epg-signature-status signature) 'no-pubkey)))
955 (epg-context-set-result-for
956 context 'error
957 (cons (cons 'no-pubkey string)
958 (epg-context-result-for context 'error)))))
960 (defun epg--status-NO_SECKEY (context string)
961 (epg-context-set-result-for
962 context 'error
963 (cons (cons 'no-seckey string)
964 (epg-context-result-for context 'error))))
966 (defun epg--time-from-seconds (seconds)
967 (let ((number-seconds (string-to-number (concat seconds ".0"))))
968 (cons (floor (/ number-seconds 65536))
969 (floor (mod number-seconds 65536)))))
971 (defun epg--status-ERRSIG (context string)
972 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
973 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
974 string)
975 (let ((signature (epg-make-signature 'error)))
976 (epg-context-set-result-for
977 context
978 'verify
979 (cons signature
980 (epg-context-result-for context 'verify)))
981 (setf (epg-signature-key-id signature)
982 (match-string 1 string))
983 (setf (epg-signature-pubkey-algorithm signature)
984 (string-to-number (match-string 2 string)))
985 (setf (epg-signature-digest-algorithm signature)
986 (string-to-number (match-string 3 string)))
987 (setf (epg-signature-class signature)
988 (string-to-number (match-string 4 string) 16))
989 (setf (epg-signature-creation-time signature)
990 (epg--time-from-seconds (match-string 5 string))))))
992 (defun epg--status-VALIDSIG (context string)
993 (let ((signature (car (epg-context-result-for context 'verify))))
994 (when (and signature
995 (eq (epg-signature-status signature) 'good)
996 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
997 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
998 \\(.*\\)"
999 string))
1000 (setf (epg-signature-fingerprint signature)
1001 (match-string 1 string))
1002 (setf (epg-signature-creation-time signature)
1003 (epg--time-from-seconds (match-string 2 string)))
1004 (unless (equal (match-string 3 string) "0")
1005 (setf (epg-signature-expiration-time signature)
1006 (epg--time-from-seconds (match-string 3 string))))
1007 (setf (epg-signature-version signature)
1008 (string-to-number (match-string 4 string)))
1009 (setf (epg-signature-pubkey-algorithm signature)
1010 (string-to-number (match-string 5 string)))
1011 (setf (epg-signature-digest-algorithm signature)
1012 (string-to-number (match-string 6 string)))
1013 (setf (epg-signature-class signature)
1014 (string-to-number (match-string 7 string) 16)))))
1016 (defun epg--status-TRUST_UNDEFINED (context _string)
1017 (let ((signature (car (epg-context-result-for context 'verify))))
1018 (if (and signature
1019 (eq (epg-signature-status signature) 'good))
1020 (setf (epg-signature-validity signature) 'undefined))))
1022 (defun epg--status-TRUST_NEVER (context _string)
1023 (let ((signature (car (epg-context-result-for context 'verify))))
1024 (if (and signature
1025 (eq (epg-signature-status signature) 'good))
1026 (setf (epg-signature-validity signature) 'never))))
1028 (defun epg--status-TRUST_MARGINAL (context _string)
1029 (let ((signature (car (epg-context-result-for context 'verify))))
1030 (if (and signature
1031 (eq (epg-signature-status signature) 'marginal))
1032 (setf (epg-signature-validity signature) 'marginal))))
1034 (defun epg--status-TRUST_FULLY (context _string)
1035 (let ((signature (car (epg-context-result-for context 'verify))))
1036 (if (and signature
1037 (eq (epg-signature-status signature) 'good))
1038 (setf (epg-signature-validity signature) 'full))))
1040 (defun epg--status-TRUST_ULTIMATE (context _string)
1041 (let ((signature (car (epg-context-result-for context 'verify))))
1042 (if (and signature
1043 (eq (epg-signature-status signature) 'good))
1044 (setf (epg-signature-validity signature) 'ultimate))))
1046 (defun epg--status-NOTATION_NAME (context string)
1047 (let ((signature (car (epg-context-result-for context 'verify))))
1048 (if signature
1049 (push (epg-make-sig-notation string nil t nil)
1050 (epg-signature-notations signature)))))
1052 (defun epg--status-NOTATION_DATA (context string)
1053 (let ((signature (car (epg-context-result-for context 'verify)))
1054 notation)
1055 (if (and signature
1056 (setq notation (car (epg-signature-notations signature))))
1057 (setf (epg-sig-notation-value notation) string))))
1059 (defun epg--status-POLICY_URL (context string)
1060 (let ((signature (car (epg-context-result-for context 'verify))))
1061 (if signature
1062 (push (epg-make-sig-notation nil string t nil)
1063 (epg-signature-notations signature)))))
1065 (defun epg--status-PROGRESS (context string)
1066 (if (and (epg-context-progress-callback context)
1067 (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1068 string))
1069 (funcall (car (epg-context-progress-callback context))
1070 context
1071 (match-string 1 string)
1072 (match-string 2 string)
1073 (string-to-number (match-string 3 string))
1074 (string-to-number (match-string 4 string))
1075 (cdr (epg-context-progress-callback context)))))
1077 (defun epg--status-ENC_TO (context string)
1078 (if (string-match "\\`\\([0-9A-Za-z]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
1079 (epg-context-set-result-for
1080 context 'encrypted-to
1081 (cons (list (match-string 1 string)
1082 (string-to-number (match-string 2 string))
1083 (string-to-number (match-string 3 string)))
1084 (epg-context-result-for context 'encrypted-to)))))
1086 (defun epg--status-DECRYPTION_FAILED (context _string)
1087 (epg-context-set-result-for context 'decryption-failed t))
1089 (defun epg--status-DECRYPTION_OKAY (context _string)
1090 (epg-context-set-result-for context 'decryption-okay t))
1092 (defun epg--status-NODATA (context string)
1093 (epg-context-set-result-for
1094 context 'error
1095 (cons (cons 'no-data (string-to-number string))
1096 (epg-context-result-for context 'error))))
1098 (defun epg--status-UNEXPECTED (context string)
1099 (epg-context-set-result-for
1100 context 'error
1101 (cons (cons 'unexpected (string-to-number string))
1102 (epg-context-result-for context 'error))))
1104 (defun epg--status-KEYEXPIRED (context string)
1105 (epg-context-set-result-for
1106 context 'key
1107 (cons (list 'key-expired (cons 'expiration-time
1108 (epg--time-from-seconds string)))
1109 (epg-context-result-for context 'key))))
1111 (defun epg--status-KEYREVOKED (context _string)
1112 (epg-context-set-result-for
1113 context 'key
1114 (cons '(key-revoked)
1115 (epg-context-result-for context 'key))))
1117 (defun epg--status-BADARMOR (context _string)
1118 (epg-context-set-result-for
1119 context 'error
1120 (cons '(bad-armor)
1121 (epg-context-result-for context 'error))))
1123 (defun epg--status-INV_RECP (context string)
1124 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1125 (epg-context-set-result-for
1126 context 'error
1127 (cons (list 'invalid-recipient
1128 (cons 'reason
1129 (string-to-number (match-string 1 string)))
1130 (cons 'requested
1131 (match-string 2 string)))
1132 (epg-context-result-for context 'error)))))
1134 (defun epg--status-INV_SGNR (context string)
1135 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1136 (epg-context-set-result-for
1137 context 'error
1138 (cons (list 'invalid-signer
1139 (cons 'reason
1140 (string-to-number (match-string 1 string)))
1141 (cons 'requested
1142 (match-string 2 string)))
1143 (epg-context-result-for context 'error)))))
1145 (defun epg--status-NO_RECP (context _string)
1146 (epg-context-set-result-for
1147 context 'error
1148 (cons '(no-recipients)
1149 (epg-context-result-for context 'error))))
1151 (defun epg--status-NO_SGNR (context _string)
1152 (epg-context-set-result-for
1153 context 'error
1154 (cons '(no-signers)
1155 (epg-context-result-for context 'error))))
1157 (defun epg--status-DELETE_PROBLEM (context string)
1158 (if (string-match "\\`\\([0-9]+\\)" string)
1159 (epg-context-set-result-for
1160 context 'error
1161 (cons (cons 'delete-problem
1162 (string-to-number (match-string 1 string)))
1163 (epg-context-result-for context 'error)))))
1165 (defun epg--status-SIG_CREATED (context string)
1166 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1167 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1168 (epg-context-set-result-for
1169 context 'sign
1170 (cons (epg-make-new-signature
1171 (cdr (assq (aref (match-string 1 string) 0)
1172 epg-new-signature-type-alist))
1173 (string-to-number (match-string 2 string))
1174 (string-to-number (match-string 3 string))
1175 (string-to-number (match-string 4 string) 16)
1176 (epg--time-from-seconds (match-string 5 string))
1177 (substring string (match-end 0)))
1178 (epg-context-result-for context 'sign)))))
1180 (defun epg--status-KEY_CREATED (context string)
1181 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1182 (epg-context-set-result-for
1183 context 'generate-key
1184 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1185 (cons 'fingerprint (match-string 2 string)))
1186 (epg-context-result-for context 'generate-key)))))
1188 (defun epg--status-KEY_NOT_CREATED (context _string)
1189 (epg-context-set-result-for
1190 context 'error
1191 (cons '(key-not-created)
1192 (epg-context-result-for context 'error))))
1194 (defun epg--status-IMPORTED (_context string)
1195 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1196 (let* ((key-id (match-string 1 string))
1197 (user-id (match-string 2 string))
1198 (entry (assoc key-id epg-user-id-alist)))
1199 (condition-case nil
1200 (setq user-id (epg--decode-coding-string
1201 (epg--decode-percent-escape user-id)
1202 'utf-8))
1203 (error))
1204 (if entry
1205 (setcdr entry user-id)
1206 (setq epg-user-id-alist (cons (cons key-id user-id)
1207 epg-user-id-alist))))))
1209 (defun epg--status-IMPORT_OK (context string)
1210 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1211 (let ((reason (string-to-number (match-string 1 string))))
1212 (epg-context-set-result-for
1213 context 'import-status
1214 (cons (epg-make-import-status (if (match-beginning 2)
1215 (match-string 3 string))
1217 (/= (logand reason 1) 0)
1218 (/= (logand reason 2) 0)
1219 (/= (logand reason 4) 0)
1220 (/= (logand reason 8) 0)
1221 (/= (logand reason 16) 0))
1222 (epg-context-result-for context 'import-status))))))
1224 (defun epg--status-IMPORT_PROBLEM (context string)
1225 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1226 (epg-context-set-result-for
1227 context 'import-status
1228 (cons (epg-make-import-status
1229 (if (match-beginning 2)
1230 (match-string 3 string))
1231 (string-to-number (match-string 1 string)))
1232 (epg-context-result-for context 'import-status)))))
1234 (defun epg--status-IMPORT_RES (context string)
1235 (when (string-match "\\`\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1236 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1237 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
1238 (epg-context-set-result-for
1239 context 'import
1240 (epg-make-import-result (string-to-number (match-string 1 string))
1241 (string-to-number (match-string 2 string))
1242 (string-to-number (match-string 3 string))
1243 (string-to-number (match-string 4 string))
1244 (string-to-number (match-string 5 string))
1245 (string-to-number (match-string 6 string))
1246 (string-to-number (match-string 7 string))
1247 (string-to-number (match-string 8 string))
1248 (string-to-number (match-string 9 string))
1249 (string-to-number (match-string 10 string))
1250 (string-to-number (match-string 11 string))
1251 (string-to-number (match-string 12 string))
1252 (string-to-number (match-string 13 string))
1253 (epg-context-result-for context 'import-status)))
1254 (epg-context-set-result-for context 'import-status nil)))
1256 (defun epg-passphrase-callback-function (context key-id _handback)
1257 (declare (obsolete epa-passphrase-callback-function "23.1"))
1258 (if (eq key-id 'SYM)
1259 (read-passwd "Passphrase for symmetric encryption: "
1260 (eq (epg-context-operation context) 'encrypt))
1261 (read-passwd
1262 (if (eq key-id 'PIN)
1263 "Passphrase for PIN: "
1264 (let ((entry (assoc key-id epg-user-id-alist)))
1265 (if entry
1266 (format "Passphrase for %s %s: " key-id (cdr entry))
1267 (format "Passphrase for %s: " key-id)))))))
1269 (defun epg--list-keys-1 (context name mode)
1270 (let ((args (append (if (epg-context-home-directory context)
1271 (list "--homedir"
1272 (epg-context-home-directory context)))
1273 '("--with-colons" "--no-greeting" "--batch"
1274 "--with-fingerprint" "--with-fingerprint")
1275 (unless (eq (epg-context-protocol context) 'CMS)
1276 '("--fixed-list-mode"))))
1277 (list-keys-option (if (memq mode '(t secret))
1278 "--list-secret-keys"
1279 (if (memq mode '(nil public))
1280 "--list-keys"
1281 "--list-sigs")))
1282 (coding-system-for-read 'binary)
1283 keys string field index)
1284 (if name
1285 (progn
1286 (unless (listp name)
1287 (setq name (list name)))
1288 (while name
1289 (setq args (append args (list list-keys-option (car name)))
1290 name (cdr name))))
1291 (setq args (append args (list list-keys-option))))
1292 (with-temp-buffer
1293 (apply #'call-process
1294 (epg-context-program context)
1295 nil (list t nil) nil args)
1296 (goto-char (point-min))
1297 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1298 (setq keys (cons (make-vector 15 nil) keys)
1299 string (match-string 0)
1300 index 0
1301 field 0)
1302 (while (and (< field (length (car keys)))
1303 (eq index
1304 (string-match "\\([^:]+\\)?:" string index)))
1305 (setq index (match-end 0))
1306 (aset (car keys) field (match-string 1 string))
1307 (setq field (1+ field))))
1308 (nreverse keys))))
1310 (defun epg--make-sub-key-1 (line)
1311 (epg-make-sub-key
1312 (if (aref line 1)
1313 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1314 (delq nil
1315 (mapcar (lambda (char) (cdr (assq char epg-key-capability-alist)))
1316 (aref line 11)))
1317 (member (aref line 0) '("sec" "ssb"))
1318 (string-to-number (aref line 3))
1319 (string-to-number (aref line 2))
1320 (aref line 4)
1321 (epg--time-from-seconds (aref line 5))
1322 (if (aref line 6)
1323 (epg--time-from-seconds (aref line 6)))))
1325 (defun epg-list-keys (context &optional name mode)
1326 "Return a list of epg-key objects matched with NAME.
1327 If MODE is nil or 'public, only public keyring should be searched.
1328 If MODE is t or 'secret, only secret keyring should be searched.
1329 Otherwise, only public keyring should be searched and the key
1330 signatures should be included.
1331 NAME is either a string or a list of strings."
1332 (let ((lines (epg--list-keys-1 context name mode))
1333 keys cert pointer pointer-1 index string)
1334 (while lines
1335 (cond
1336 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1337 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1338 keys (cons (epg-make-key
1339 (if (aref (car lines) 8)
1340 (cdr (assq (string-to-char (aref (car lines) 8))
1341 epg-key-validity-alist))))
1342 keys))
1343 (push (epg--make-sub-key-1 (car lines))
1344 (epg-key-sub-key-list (car keys))))
1345 ((member (aref (car lines) 0) '("sub" "ssb"))
1346 (push (epg--make-sub-key-1 (car lines))
1347 (epg-key-sub-key-list (car keys))))
1348 ((equal (aref (car lines) 0) "uid")
1349 ;; Decode the UID name as a backslash escaped UTF-8 string,
1350 ;; generated by GnuPG/GpgSM.
1351 (setq string (copy-sequence (aref (car lines) 9))
1352 index 0)
1353 (while (string-match "\"" string index)
1354 (setq string (replace-match "\\\"" t t string)
1355 index (1+ (match-end 0))))
1356 (condition-case nil
1357 (setq string (epg--decode-coding-string
1358 (car (read-from-string (concat "\"" string "\"")))
1359 'utf-8))
1360 (error
1361 (setq string (aref (car lines) 9))))
1362 (push (epg-make-user-id
1363 (if (aref (car lines) 1)
1364 (cdr (assq (string-to-char (aref (car lines) 1))
1365 epg-key-validity-alist)))
1366 (if cert
1367 (condition-case nil
1368 (epg-dn-from-string string)
1369 (error string))
1370 string))
1371 (epg-key-user-id-list (car keys))))
1372 ((equal (aref (car lines) 0) "fpr")
1373 (setf (epg-sub-key-fingerprint (car (epg-key-sub-key-list (car keys))))
1374 (aref (car lines) 9)))
1375 ((equal (aref (car lines) 0) "sig")
1376 (push
1377 (epg-make-key-signature
1378 (if (aref (car lines) 1)
1379 (cdr (assq (string-to-char (aref (car lines) 1))
1380 epg-key-validity-alist)))
1381 (string-to-number (aref (car lines) 3))
1382 (aref (car lines) 4)
1383 (epg--time-from-seconds (aref (car lines) 5))
1384 (epg--time-from-seconds (aref (car lines) 6))
1385 (aref (car lines) 9)
1386 (string-to-number (aref (car lines) 10) 16)
1387 (eq (aref (aref (car lines) 10) 2) ?x))
1388 (epg-user-id-signature-list
1389 (car (epg-key-user-id-list (car keys)))))))
1390 (setq lines (cdr lines)))
1391 (setq keys (nreverse keys)
1392 pointer keys)
1393 (while pointer
1394 (epg--gv-nreverse (epg-key-sub-key-list (car pointer)))
1395 (setq pointer-1 (epg--gv-nreverse (epg-key-user-id-list (car pointer))))
1396 (while pointer-1
1397 (epg--gv-nreverse (epg-user-id-signature-list (car pointer-1)))
1398 (setq pointer-1 (cdr pointer-1)))
1399 (setq pointer (cdr pointer)))
1400 keys))
1402 (eval-and-compile
1403 (if (fboundp 'make-temp-file)
1404 (defalias 'epg--make-temp-file 'make-temp-file)
1405 (defvar temporary-file-directory)
1406 ;; stolen from poe.el.
1407 (defun epg--make-temp-file (prefix)
1408 "Create a temporary file.
1409 The returned file name (created by appending some random characters at the end
1410 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1411 is guaranteed to point to a newly created empty file.
1412 You can then use `write-region' to write new data into the file."
1413 (let ((orig-modes (default-file-modes))
1414 tempdir tempfile)
1415 (setq prefix (expand-file-name prefix
1416 (if (featurep 'xemacs)
1417 (temp-directory)
1418 temporary-file-directory)))
1419 (unwind-protect
1420 (let (file)
1421 ;; First, create a temporary directory.
1422 (set-default-file-modes #o700)
1423 (while (condition-case ()
1424 (progn
1425 (setq tempdir (make-temp-name
1426 (concat
1427 (file-name-directory prefix)
1428 "DIR")))
1429 ;; return nil or signal an error.
1430 (make-directory tempdir))
1431 ;; let's try again.
1432 (file-already-exists t)))
1433 ;; Second, create a temporary file in the tempdir.
1434 ;; There *is* a race condition between `make-temp-name'
1435 ;; and `write-region', but we don't care it since we are
1436 ;; in a private directory now.
1437 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1438 (write-region "" nil tempfile nil 'silent)
1439 ;; Finally, make a hard-link from the tempfile.
1440 (while (condition-case ()
1441 (progn
1442 (setq file (make-temp-name prefix))
1443 ;; return nil or signal an error.
1444 (add-name-to-file tempfile file))
1445 ;; let's try again.
1446 (file-already-exists t)))
1447 file)
1448 (set-default-file-modes orig-modes)
1449 ;; Cleanup the tempfile.
1450 (and tempfile
1451 (file-exists-p tempfile)
1452 (delete-file tempfile))
1453 ;; Cleanup the tempdir.
1454 (and tempdir
1455 (file-directory-p tempdir)
1456 (delete-directory tempdir)))))))
1458 (defun epg--args-from-sig-notations (notations)
1459 (apply #'nconc
1460 (mapcar
1461 (lambda (notation)
1462 (if (and (epg-sig-notation-name notation)
1463 (not (epg-sig-notation-human-readable notation)))
1464 (error "Unreadable"))
1465 (if (epg-sig-notation-name notation)
1466 (list "--sig-notation"
1467 (if (epg-sig-notation-critical notation)
1468 (concat "!" (epg-sig-notation-name notation)
1469 "=" (epg-sig-notation-value notation))
1470 (concat (epg-sig-notation-name notation)
1471 "=" (epg-sig-notation-value notation))))
1472 (list "--sig-policy-url"
1473 (if (epg-sig-notation-critical notation)
1474 (concat "!" (epg-sig-notation-value notation))
1475 (epg-sig-notation-value notation)))))
1476 notations)))
1478 (defun epg-cancel (context)
1479 (if (buffer-live-p (process-buffer (epg-context-process context)))
1480 (with-current-buffer (process-buffer (epg-context-process context))
1481 (epg-context-set-result-for
1482 epg-context 'error
1483 (cons '(quit)
1484 (epg-context-result-for epg-context 'error)))))
1485 (if (eq (process-status (epg-context-process context)) 'run)
1486 (delete-process (epg-context-process context))))
1488 (defun epg-start-decrypt (context cipher)
1489 "Initiate a decrypt operation on CIPHER.
1490 CIPHER must be a file data object.
1492 If you use this function, you will need to wait for the completion of
1493 `epg-gpg-program' by using `epg-wait-for-completion' and call
1494 `epg-reset' to clear a temporary output file.
1495 If you are unsure, use synchronous version of this function
1496 `epg-decrypt-file' or `epg-decrypt-string' instead."
1497 (unless (epg-data-file cipher)
1498 (error "Not a file"))
1499 (setf (epg-context-operation context) 'decrypt)
1500 (setf (epg-context-result context) nil)
1501 (epg--start context (list "--decrypt" "--" (epg-data-file cipher)))
1502 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1503 (unless (eq (epg-context-protocol context) 'CMS)
1504 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1506 (defun epg--check-error-for-decrypt (context)
1507 (let ((errors (epg-context-result-for context 'error)))
1508 (if (epg-context-result-for context 'decryption-failed)
1509 (signal 'epg-error
1510 (list "Decryption failed" (epg-errors-to-string errors))))
1511 (unless (epg-context-result-for context 'decryption-okay)
1512 (signal 'epg-error
1513 (list "Can't decrypt" (epg-errors-to-string errors))))))
1515 (defun epg-decrypt-file (context cipher plain)
1516 "Decrypt a file CIPHER and store the result to a file PLAIN.
1517 If PLAIN is nil, it returns the result as a string."
1518 (unwind-protect
1519 (progn
1520 (setf (epg-context-output-file context)
1521 (or plain (epg--make-temp-file "epg-output")))
1522 (epg-start-decrypt context (epg-make-data-from-file cipher))
1523 (epg-wait-for-completion context)
1524 (epg--check-error-for-decrypt context)
1525 (unless plain
1526 (epg-read-output context)))
1527 (unless plain
1528 (epg-delete-output-file context))
1529 (epg-reset context)))
1531 (defun epg-decrypt-string (context cipher)
1532 "Decrypt a string CIPHER and return the plain text."
1533 (let ((input-file (epg--make-temp-file "epg-input"))
1534 (coding-system-for-write 'binary))
1535 (unwind-protect
1536 (progn
1537 (write-region cipher nil input-file nil 'quiet)
1538 (setf (epg-context-output-file context)
1539 (epg--make-temp-file "epg-output"))
1540 (epg-start-decrypt context (epg-make-data-from-file input-file))
1541 (epg-wait-for-completion context)
1542 (epg--check-error-for-decrypt context)
1543 (epg-read-output context))
1544 (epg-delete-output-file context)
1545 (if (file-exists-p input-file)
1546 (delete-file input-file))
1547 (epg-reset context))))
1549 (defun epg-start-verify (context signature &optional signed-text)
1550 "Initiate a verify operation on SIGNATURE.
1551 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1553 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1554 For a normal or a cleartext signature, SIGNED-TEXT should be nil.
1556 If you use this function, you will need to wait for the completion of
1557 `epg-gpg-program' by using `epg-wait-for-completion' and call
1558 `epg-reset' to clear a temporary output file.
1559 If you are unsure, use synchronous version of this function
1560 `epg-verify-file' or `epg-verify-string' instead."
1561 (setf (epg-context-operation context) 'verify)
1562 (setf (epg-context-result context) nil)
1563 (if signed-text
1564 ;; Detached signature.
1565 (if (epg-data-file signed-text)
1566 (epg--start context (list "--verify" "--" (epg-data-file signature)
1567 (epg-data-file signed-text)))
1568 (epg--start context (list "--verify" "--" (epg-data-file signature)
1569 "-"))
1570 (if (eq (process-status (epg-context-process context)) 'run)
1571 (process-send-string (epg-context-process context)
1572 (epg-data-string signed-text)))
1573 (if (eq (process-status (epg-context-process context)) 'run)
1574 (process-send-eof (epg-context-process context))))
1575 ;; Normal (or cleartext) signature.
1576 (if (epg-data-file signature)
1577 (epg--start context (if (eq (epg-context-protocol context) 'CMS)
1578 (list "--verify" "--" (epg-data-file signature))
1579 (list "--" (epg-data-file signature))))
1580 (epg--start context (if (eq (epg-context-protocol context) 'CMS)
1581 '("--verify" "-")
1582 '("-")))
1583 (if (eq (process-status (epg-context-process context)) 'run)
1584 (process-send-string (epg-context-process context)
1585 (epg-data-string signature)))
1586 (if (eq (process-status (epg-context-process context)) 'run)
1587 (process-send-eof (epg-context-process context))))))
1589 (defun epg-verify-file (context signature &optional signed-text plain)
1590 "Verify a file SIGNATURE.
1591 SIGNED-TEXT and PLAIN are also a file if they are specified.
1593 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1594 string. For a normal or a cleartext signature, SIGNED-TEXT should be
1595 nil. In the latter case, if PLAIN is specified, the plaintext is
1596 stored into the file after successful verification.
1598 Note that this function does not return verification result as t
1599 or nil, nor signal error on failure. That's a design decision to
1600 handle the case where SIGNATURE has multiple signature.
1602 To check the verification results, use `epg-context-result-for' as follows:
1604 \(epg-context-result-for context 'verify)
1606 which will return a list of `epg-signature' object."
1607 (unwind-protect
1608 (progn
1609 (setf (epg-context-output-file context)
1610 (or plain (epg--make-temp-file "epg-output")))
1611 (if signed-text
1612 (epg-start-verify context
1613 (epg-make-data-from-file signature)
1614 (epg-make-data-from-file signed-text))
1615 (epg-start-verify context
1616 (epg-make-data-from-file signature)))
1617 (epg-wait-for-completion context)
1618 (unless plain
1619 (epg-read-output context)))
1620 (unless plain
1621 (epg-delete-output-file context))
1622 (epg-reset context)))
1624 (defun epg-verify-string (context signature &optional signed-text)
1625 "Verify a string SIGNATURE.
1626 SIGNED-TEXT is a string if it is specified.
1628 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1629 string. For a normal or a cleartext signature, SIGNED-TEXT should be
1630 nil. In the latter case, this function returns the plaintext after
1631 successful verification.
1633 Note that this function does not return verification result as t
1634 or nil, nor signal error on failure. That's a design decision to
1635 handle the case where SIGNATURE has multiple signature.
1637 To check the verification results, use `epg-context-result-for' as follows:
1639 \(epg-context-result-for context 'verify)
1641 which will return a list of `epg-signature' object."
1642 (let ((coding-system-for-write 'binary)
1643 input-file)
1644 (unwind-protect
1645 (progn
1646 (setf (epg-context-output-file context)
1647 (epg--make-temp-file "epg-output"))
1648 (if signed-text
1649 (progn
1650 (setq input-file (epg--make-temp-file "epg-signature"))
1651 (write-region signature nil input-file nil 'quiet)
1652 (epg-start-verify context
1653 (epg-make-data-from-file input-file)
1654 (epg-make-data-from-string signed-text)))
1655 (epg-start-verify context (epg-make-data-from-string signature)))
1656 (epg-wait-for-completion context)
1657 (epg-read-output context))
1658 (epg-delete-output-file context)
1659 (if (and input-file
1660 (file-exists-p input-file))
1661 (delete-file input-file))
1662 (epg-reset context))))
1664 (defun epg-start-sign (context plain &optional mode)
1665 "Initiate a sign operation on PLAIN.
1666 PLAIN is a data object.
1668 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1669 If it is nil or 'normal, it makes a normal signature.
1670 Otherwise, it makes a cleartext signature.
1672 If you use this function, you will need to wait for the completion of
1673 `epg-gpg-program' by using `epg-wait-for-completion' and call
1674 `epg-reset' to clear a temporary output file.
1675 If you are unsure, use synchronous version of this function
1676 `epg-sign-file' or `epg-sign-string' instead."
1677 (setf (epg-context-operation context) 'sign)
1678 (setf (epg-context-result context) nil)
1679 (unless (memq mode '(t detached nil normal)) ;i.e. cleartext
1680 (epg-context-set-armor context nil)
1681 (epg-context-set-textmode context nil))
1682 (epg--start context
1683 (append (list (if (memq mode '(t detached))
1684 "--detach-sign"
1685 (if (memq mode '(nil normal))
1686 "--sign"
1687 "--clearsign")))
1688 (apply #'nconc
1689 (mapcar
1690 (lambda (signer)
1691 (list "-u"
1692 (epg-sub-key-id
1693 (car (epg-key-sub-key-list signer)))))
1694 (epg-context-signers context)))
1695 (epg--args-from-sig-notations
1696 (epg-context-sig-notations context))
1697 (if (epg-data-file plain)
1698 (list "--" (epg-data-file plain)))))
1699 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1700 (unless (eq (epg-context-protocol context) 'CMS)
1701 (epg-wait-for-status context '("BEGIN_SIGNING")))
1702 (when (epg-data-string plain)
1703 (if (eq (process-status (epg-context-process context)) 'run)
1704 (process-send-string (epg-context-process context)
1705 (epg-data-string plain)))
1706 (if (eq (process-status (epg-context-process context)) 'run)
1707 (process-send-eof (epg-context-process context)))))
1709 (defun epg-sign-file (context plain signature &optional mode)
1710 "Sign a file PLAIN and store the result to a file SIGNATURE.
1711 If SIGNATURE is nil, it returns the result as a string.
1712 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1713 If it is nil or 'normal, it makes a normal signature.
1714 Otherwise, it makes a cleartext signature."
1715 (unwind-protect
1716 (progn
1717 (setf (epg-context-output-file context)
1718 (or signature (epg--make-temp-file "epg-output")))
1719 (epg-start-sign context (epg-make-data-from-file plain) mode)
1720 (epg-wait-for-completion context)
1721 (unless (epg-context-result-for context 'sign)
1722 (let ((errors (epg-context-result-for context 'error)))
1723 (signal 'epg-error
1724 (list "Sign failed" (epg-errors-to-string errors)))))
1725 (unless signature
1726 (epg-read-output context)))
1727 (unless signature
1728 (epg-delete-output-file context))
1729 (epg-reset context)))
1731 (defun epg-sign-string (context plain &optional mode)
1732 "Sign a string PLAIN and return the output as string.
1733 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1734 If it is nil or 'normal, it makes a normal signature.
1735 Otherwise, it makes a cleartext signature."
1736 (let ((input-file
1737 (unless (or (eq (epg-context-protocol context) 'CMS)
1738 (condition-case nil
1739 (progn
1740 (epg-check-configuration (epg-configuration))
1742 (error)))
1743 (epg--make-temp-file "epg-input")))
1744 (coding-system-for-write 'binary))
1745 (unwind-protect
1746 (progn
1747 (setf (epg-context-output-file context)
1748 (epg--make-temp-file "epg-output"))
1749 (if input-file
1750 (write-region plain nil input-file nil 'quiet))
1751 (epg-start-sign context
1752 (if input-file
1753 (epg-make-data-from-file input-file)
1754 (epg-make-data-from-string plain))
1755 mode)
1756 (epg-wait-for-completion context)
1757 (unless (epg-context-result-for context 'sign)
1758 (if (epg-context-result-for context 'error)
1759 (let ((errors (epg-context-result-for context 'error)))
1760 (signal 'epg-error
1761 (list "Sign failed" (epg-errors-to-string errors))))))
1762 (epg-read-output context))
1763 (epg-delete-output-file context)
1764 (if input-file
1765 (delete-file input-file))
1766 (epg-reset context))))
1768 (defun epg-start-encrypt (context plain recipients
1769 &optional sign always-trust)
1770 "Initiate an encrypt operation on PLAIN.
1771 PLAIN is a data object.
1772 If RECIPIENTS is nil, it performs symmetric encryption.
1774 If you use this function, you will need to wait for the completion of
1775 `epg-gpg-program' by using `epg-wait-for-completion' and call
1776 `epg-reset' to clear a temporary output file.
1777 If you are unsure, use synchronous version of this function
1778 `epg-encrypt-file' or `epg-encrypt-string' instead."
1779 (setf (epg-context-operation context) 'encrypt)
1780 (setf (epg-context-result context) nil)
1781 (epg--start context
1782 (append (if always-trust '("--always-trust"))
1783 (if recipients '("--encrypt") '("--symmetric"))
1784 (if sign '("--sign"))
1785 (if sign
1786 (apply #'nconc
1787 (mapcar
1788 (lambda (signer)
1789 (list "-u"
1790 (epg-sub-key-id
1791 (car (epg-key-sub-key-list
1792 signer)))))
1793 (epg-context-signers context))))
1794 (if sign
1795 (epg--args-from-sig-notations
1796 (epg-context-sig-notations context)))
1797 (apply #'nconc
1798 (mapcar
1799 (lambda (recipient)
1800 (list "-r"
1801 (epg-sub-key-id
1802 (car (epg-key-sub-key-list recipient)))))
1803 recipients))
1804 (if (epg-data-file plain)
1805 (list "--" (epg-data-file plain)))))
1806 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1807 (unless (eq (epg-context-protocol context) 'CMS)
1808 (epg-wait-for-status context
1809 (if sign '("BEGIN_SIGNING") '("BEGIN_ENCRYPTION"))))
1810 (when (epg-data-string plain)
1811 (if (eq (process-status (epg-context-process context)) 'run)
1812 (process-send-string (epg-context-process context)
1813 (epg-data-string plain)))
1814 (if (eq (process-status (epg-context-process context)) 'run)
1815 (process-send-eof (epg-context-process context)))))
1817 (defun epg-encrypt-file (context plain recipients
1818 cipher &optional sign always-trust)
1819 "Encrypt a file PLAIN and store the result to a file CIPHER.
1820 If CIPHER is nil, it returns the result as a string.
1821 If RECIPIENTS is nil, it performs symmetric encryption."
1822 (unwind-protect
1823 (progn
1824 (setf (epg-context-output-file context)
1825 (or cipher (epg--make-temp-file "epg-output")))
1826 (epg-start-encrypt context (epg-make-data-from-file plain)
1827 recipients sign always-trust)
1828 (epg-wait-for-completion context)
1829 (let ((errors (epg-context-result-for context 'error)))
1830 (if (and sign
1831 (not (epg-context-result-for context 'sign)))
1832 (signal 'epg-error
1833 (list "Sign failed" (epg-errors-to-string errors))))
1834 (if errors
1835 (signal 'epg-error
1836 (list "Encrypt failed" (epg-errors-to-string errors)))))
1837 (unless cipher
1838 (epg-read-output context)))
1839 (unless cipher
1840 (epg-delete-output-file context))
1841 (epg-reset context)))
1843 (defun epg-encrypt-string (context plain recipients
1844 &optional sign always-trust)
1845 "Encrypt a string PLAIN.
1846 If RECIPIENTS is nil, it performs symmetric encryption."
1847 (let ((input-file
1848 (unless (or (not sign)
1849 (eq (epg-context-protocol context) 'CMS)
1850 (condition-case nil
1851 (progn
1852 (epg-check-configuration (epg-configuration))
1854 (error)))
1855 (epg--make-temp-file "epg-input")))
1856 (coding-system-for-write 'binary))
1857 (unwind-protect
1858 (progn
1859 (setf (epg-context-output-file context)
1860 (epg--make-temp-file "epg-output"))
1861 (if input-file
1862 (write-region plain nil input-file nil 'quiet))
1863 (epg-start-encrypt context
1864 (if input-file
1865 (epg-make-data-from-file input-file)
1866 (epg-make-data-from-string plain))
1867 recipients sign always-trust)
1868 (epg-wait-for-completion context)
1869 (let ((errors (epg-context-result-for context 'error)))
1870 (if (and sign
1871 (not (epg-context-result-for context 'sign)))
1872 (signal 'epg-error
1873 (list "Sign failed" (epg-errors-to-string errors))))
1874 (if errors
1875 (signal 'epg-error
1876 (list "Encrypt failed" (epg-errors-to-string errors)))))
1877 (epg-read-output context))
1878 (epg-delete-output-file context)
1879 (if input-file
1880 (delete-file input-file))
1881 (epg-reset context))))
1883 (defun epg-start-export-keys (context keys)
1884 "Initiate an export keys operation.
1886 If you use this function, you will need to wait for the completion of
1887 `epg-gpg-program' by using `epg-wait-for-completion' and call
1888 `epg-reset' to clear a temporary output file.
1889 If you are unsure, use synchronous version of this function
1890 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1891 (setf (epg-context-operation context) 'export-keys)
1892 (setf (epg-context-result context) nil)
1893 (epg--start context (cons "--export"
1894 (mapcar
1895 (lambda (key)
1896 (epg-sub-key-id
1897 (car (epg-key-sub-key-list key))))
1898 keys))))
1900 (defun epg-export-keys-to-file (context keys file)
1901 "Extract public KEYS."
1902 (unwind-protect
1903 (progn
1904 (setf (epg-context-output-file context)
1905 (or file (epg--make-temp-file "epg-output")))
1906 (epg-start-export-keys context keys)
1907 (epg-wait-for-completion context)
1908 (let ((errors (epg-context-result-for context 'error)))
1909 (if errors
1910 (signal 'epg-error
1911 (list "Export keys failed"
1912 (epg-errors-to-string errors)))))
1913 (unless file
1914 (epg-read-output context)))
1915 (unless file
1916 (epg-delete-output-file context))
1917 (epg-reset context)))
1919 (defun epg-export-keys-to-string (context keys)
1920 "Extract public KEYS and return them as a string."
1921 (epg-export-keys-to-file context keys nil))
1923 (defun epg-start-import-keys (context keys)
1924 "Initiate an import keys operation.
1925 KEYS is a data object.
1927 If you use this function, you will need to wait for the completion of
1928 `epg-gpg-program' by using `epg-wait-for-completion' and call
1929 `epg-reset' to clear a temporary output file.
1930 If you are unsure, use synchronous version of this function
1931 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1932 (setf (epg-context-operation context) 'import-keys)
1933 (setf (epg-context-result context) nil)
1934 (epg--start context (if (epg-data-file keys)
1935 (list "--import" "--" (epg-data-file keys))
1936 (list "--import")))
1937 (when (epg-data-string keys)
1938 (if (eq (process-status (epg-context-process context)) 'run)
1939 (process-send-string (epg-context-process context)
1940 (epg-data-string keys)))
1941 (if (eq (process-status (epg-context-process context)) 'run)
1942 (process-send-eof (epg-context-process context)))))
1944 (defun epg--import-keys-1 (context keys)
1945 (unwind-protect
1946 (progn
1947 (epg-start-import-keys context keys)
1948 (epg-wait-for-completion context)
1949 (let ((errors (epg-context-result-for context 'error)))
1950 (if errors
1951 (signal 'epg-error
1952 (list "Import keys failed"
1953 (epg-errors-to-string errors))))))
1954 (epg-reset context)))
1956 (defun epg-import-keys-from-file (context keys)
1957 "Add keys from a file KEYS."
1958 (epg--import-keys-1 context (epg-make-data-from-file keys)))
1960 (defun epg-import-keys-from-string (context keys)
1961 "Add keys from a string KEYS."
1962 (epg--import-keys-1 context (epg-make-data-from-string keys)))
1964 (defun epg-start-receive-keys (context key-id-list)
1965 "Initiate a receive key operation.
1966 KEY-ID-LIST is a list of key IDs.
1968 If you use this function, you will need to wait for the completion of
1969 `epg-gpg-program' by using `epg-wait-for-completion' and call
1970 `epg-reset' to clear a temporary output file.
1971 If you are unsure, use synchronous version of this function
1972 `epg-receive-keys' instead."
1973 (setf (epg-context-operation context) 'receive-keys)
1974 (setf (epg-context-result context) nil)
1975 (epg--start context (cons "--recv-keys" key-id-list)))
1977 (defun epg-receive-keys (context keys)
1978 "Add keys from server.
1979 KEYS is a list of key IDs"
1980 (unwind-protect
1981 (progn
1982 (epg-start-receive-keys context keys)
1983 (epg-wait-for-completion context)
1984 (let ((errors (epg-context-result-for context 'error)))
1985 (if errors
1986 (signal 'epg-error
1987 (list "Receive keys failed"
1988 (epg-errors-to-string errors))))))
1989 (epg-reset context)))
1991 (defalias 'epg-import-keys-from-server 'epg-receive-keys)
1993 (defun epg-start-delete-keys (context keys &optional allow-secret)
1994 "Initiate a delete keys operation.
1996 If you use this function, you will need to wait for the completion of
1997 `epg-gpg-program' by using `epg-wait-for-completion' and call
1998 `epg-reset' to clear a temporary output file.
1999 If you are unsure, use synchronous version of this function
2000 `epg-delete-keys' instead."
2001 (setf (epg-context-operation context) 'delete-keys)
2002 (setf (epg-context-result context) nil)
2003 (epg--start context (cons (if allow-secret
2004 "--delete-secret-key"
2005 "--delete-key")
2006 (mapcar
2007 (lambda (key)
2008 (epg-sub-key-id
2009 (car (epg-key-sub-key-list key))))
2010 keys))))
2012 (defun epg-delete-keys (context keys &optional allow-secret)
2013 "Delete KEYS from the key ring."
2014 (unwind-protect
2015 (progn
2016 (epg-start-delete-keys context keys allow-secret)
2017 (epg-wait-for-completion context)
2018 (let ((errors (epg-context-result-for context 'error)))
2019 (if errors
2020 (signal 'epg-error
2021 (list "Delete keys failed"
2022 (epg-errors-to-string errors))))))
2023 (epg-reset context)))
2025 (defun epg-start-sign-keys (context keys &optional local)
2026 "Initiate a sign keys operation.
2028 If you use this function, you will need to wait for the completion of
2029 `epg-gpg-program' by using `epg-wait-for-completion' and call
2030 `epg-reset' to clear a temporary output file.
2031 If you are unsure, use synchronous version of this function
2032 `epg-sign-keys' instead."
2033 (declare (obsolete nil "23.1"))
2034 (setf (epg-context-operation context) 'sign-keys)
2035 (setf (epg-context-result context) nil)
2036 (epg--start context (cons (if local
2037 "--lsign-key"
2038 "--sign-key")
2039 (mapcar
2040 (lambda (key)
2041 (epg-sub-key-id
2042 (car (epg-key-sub-key-list key))))
2043 keys))))
2045 (defun epg-sign-keys (context keys &optional local)
2046 "Sign KEYS from the key ring."
2047 (declare (obsolete nil "23.1"))
2048 (unwind-protect
2049 (progn
2050 (epg-start-sign-keys context keys local)
2051 (epg-wait-for-completion context)
2052 (let ((errors (epg-context-result-for context 'error)))
2053 (if errors
2054 (signal 'epg-error
2055 (list "Sign keys failed"
2056 (epg-errors-to-string errors))))))
2057 (epg-reset context)))
2059 (defun epg-start-generate-key (context parameters)
2060 "Initiate a key generation.
2061 PARAMETERS is a string which specifies parameters of the generated key.
2062 See Info node `(gnupg) Unattended GPG key generation' in the
2063 GnuPG manual for the format.
2065 If you use this function, you will need to wait for the completion of
2066 `epg-gpg-program' by using `epg-wait-for-completion' and call
2067 `epg-reset' to clear a temporary output file.
2068 If you are unsure, use synchronous version of this function
2069 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2070 (setf (epg-context-operation context) 'generate-key)
2071 (setf (epg-context-result context) nil)
2072 (if (epg-data-file parameters)
2073 (epg--start context (list "--batch" "--gen-key" "--"
2074 (epg-data-file parameters)))
2075 (epg--start context '("--batch" "--gen-key"))
2076 (if (eq (process-status (epg-context-process context)) 'run)
2077 (process-send-string (epg-context-process context)
2078 (epg-data-string parameters)))
2079 (if (eq (process-status (epg-context-process context)) 'run)
2080 (process-send-eof (epg-context-process context)))))
2082 (defun epg-generate-key-from-file (context parameters)
2083 "Generate a new key pair.
2084 PARAMETERS is a file which tells how to create the key."
2085 (unwind-protect
2086 (progn
2087 (epg-start-generate-key context (epg-make-data-from-file parameters))
2088 (epg-wait-for-completion context)
2089 (let ((errors (epg-context-result-for context 'error)))
2090 (if errors
2091 (signal 'epg-error
2092 (list "Generate key failed"
2093 (epg-errors-to-string errors))))))
2094 (epg-reset context)))
2096 (defun epg-generate-key-from-string (context parameters)
2097 "Generate a new key pair.
2098 PARAMETERS is a string which tells how to create the key."
2099 (unwind-protect
2100 (progn
2101 (epg-start-generate-key context (epg-make-data-from-string parameters))
2102 (epg-wait-for-completion context)
2103 (let ((errors (epg-context-result-for context 'error)))
2104 (if errors
2105 (signal 'epg-error
2106 (list "Generate key failed"
2107 (epg-errors-to-string errors))))))
2108 (epg-reset context)))
2110 (defun epg-start-edit-key (context key edit-callback handback)
2111 "Initiate an edit operation on KEY.
2113 EDIT-CALLBACK is called from process filter and takes 3
2114 arguments: the context, a status, an argument string, and the
2115 handback argument.
2117 If you use this function, you will need to wait for the completion of
2118 `epg-gpg-program' by using `epg-wait-for-completion' and call
2119 `epg-reset' to clear a temporary output file.
2120 If you are unsure, use synchronous version of this function
2121 `epg-edit-key' instead."
2122 (setf (epg-context-operation context) 'edit-key)
2123 (setf (epg-context-result context) nil)
2124 (setf (epg-context-edit-callback context) (cons edit-callback handback))
2125 (epg--start context (list "--edit-key"
2126 (epg-sub-key-id
2127 (car (epg-key-sub-key-list key))))))
2129 (defun epg-edit-key (context key edit-callback handback)
2130 "Edit KEY in the keyring."
2131 (unwind-protect
2132 (progn
2133 (epg-start-edit-key context key edit-callback handback)
2134 (epg-wait-for-completion context)
2135 (let ((errors (epg-context-result-for context 'error)))
2136 (if errors
2137 (signal 'epg-error
2138 (list "Edit key failed"
2139 (epg-errors-to-string errors))))))
2140 (epg-reset context)))
2142 (defun epg--decode-percent-escape (string)
2143 (let ((index 0))
2144 (while (string-match "%\\(\\(%\\)\\|\\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2145 string index)
2146 (if (match-beginning 2)
2147 (setq string (replace-match "%" t t string)
2148 index (1- (match-end 0)))
2149 (setq string (replace-match
2150 (string (string-to-number (match-string 3 string) 16))
2151 t t string)
2152 index (- (match-end 0) 2))))
2153 string))
2155 (defun epg--decode-hexstring (string)
2156 (let ((index 0))
2157 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
2158 (setq string (replace-match (string (string-to-number
2159 (match-string 0 string) 16))
2160 t t string)
2161 index (1- (match-end 0))))
2162 string))
2164 (defun epg--decode-quotedstring (string)
2165 (let ((index 0))
2166 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2167 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2168 string index)
2169 (if (match-beginning 2)
2170 (setq string (replace-match "\\2" t nil string)
2171 index (1- (match-end 0)))
2172 (if (match-beginning 3)
2173 (setq string (replace-match (string (string-to-number
2174 (match-string 0 string) 16))
2175 t t string)
2176 index (- (match-end 0) 2)))))
2177 string))
2179 (defun epg-dn-from-string (string)
2180 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2181 The return value is an alist mapping from types to values."
2182 (let ((index 0)
2183 (length (length string))
2184 alist type value group)
2185 (while (< index length)
2186 (if (eq index (string-match "[ \t\n\r]*" string index))
2187 (setq index (match-end 0)))
2188 (if (eq index (string-match
2189 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2190 string index))
2191 (setq type (match-string 1 string)
2192 index (match-end 0))
2193 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2194 string index))
2195 (setq type (match-string 1 string)
2196 index (match-end 0))))
2197 (unless type
2198 (error "Invalid type"))
2199 (if (eq index (string-match
2200 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2201 string index))
2202 (setq index (match-end 0)
2203 value (epg--decode-quotedstring (match-string 0 string)))
2204 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
2205 (setq index (match-end 0)
2206 value (epg--decode-hexstring (match-string 1 string)))
2207 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2208 string index))
2209 (setq index (match-end 0)
2210 value (epg--decode-quotedstring
2211 (match-string 0 string))))))
2212 (if group
2213 (if (stringp (car (car alist)))
2214 (setcar alist (list (cons type value) (car alist)))
2215 (setcar alist (cons (cons type value) (car alist))))
2216 (if (consp (car (car alist)))
2217 (setcar alist (nreverse (car alist))))
2218 (setq alist (cons (cons type value) alist)
2219 type nil
2220 value nil))
2221 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
2222 (setq index (match-end 0)
2223 group (eq (aref string (match-beginning 1)) ?+))))
2224 (nreverse alist)))
2226 (defun epg-decode-dn (alist)
2227 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2228 Type names are resolved using `epg-dn-type-alist'."
2229 (mapconcat
2230 (lambda (rdn)
2231 (if (stringp (car rdn))
2232 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
2233 (if entry
2234 (format "%s=%s" (cdr entry) (cdr rdn))
2235 (format "%s=%s" (car rdn) (cdr rdn))))
2236 (concat "(" (epg-decode-dn rdn) ")")))
2237 alist
2238 ", "))
2240 (provide 'epg)
2242 ;;; epg.el ends here