* lisp/mouse.el (minor-mode-menu-from-indicator):
[emacs.git] / lisp / epg.el
blob601504882e60a94ffbcdda37b378666f10c0b3de
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 ;; Record modified time of gpg-agent socket to restore the Emacs
606 ;; frame on text terminal in `epg-wait-for-completion'.
607 ;; See
608 ;; <http://lists.gnu.org/archive/html/emacs-devel/2007-02/msg00755.html>
609 ;; for more details.
610 (when (and agent-info (string-match "\\(.*\\):[0-9]+:[0-9]+" agent-info))
611 (setq agent-file (match-string 1 agent-info)
612 agent-mtime (or (nth 5 (file-attributes agent-file)) '(0 0 0 0))))
613 (if epg-debug
614 (save-excursion
615 (unless epg-debug-buffer
616 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
617 (set-buffer epg-debug-buffer)
618 (goto-char (point-max))
619 (insert (if agent-info
620 (format "GPG_AGENT_INFO=%s\n" agent-info)
621 "GPG_AGENT_INFO is not set\n")
622 (format "%s %s\n"
623 (epg-context-program context)
624 (mapconcat #'identity args " ")))))
625 (with-current-buffer buffer
626 (if (fboundp 'set-buffer-multibyte)
627 (set-buffer-multibyte nil))
628 (make-local-variable 'epg-last-status)
629 (setq epg-last-status nil)
630 (make-local-variable 'epg-read-point)
631 (setq epg-read-point (point-min))
632 (make-local-variable 'epg-process-filter-running)
633 (setq epg-process-filter-running nil)
634 (make-local-variable 'epg-pending-status-list)
635 (setq epg-pending-status-list nil)
636 (make-local-variable 'epg-key-id)
637 (setq epg-key-id nil)
638 (make-local-variable 'epg-context)
639 (setq epg-context context)
640 (make-local-variable 'epg-agent-file)
641 (setq epg-agent-file agent-file)
642 (make-local-variable 'epg-agent-mtime)
643 (setq epg-agent-mtime agent-mtime))
644 (setq error-process
645 (make-pipe-process :name "epg-error"
646 :buffer (generate-new-buffer " *epg-error*")
647 ;; Suppress "XXX finished" line.
648 :sentinel #'ignore
649 :noquery t))
650 (setf (epg-context-error-buffer context) (process-buffer error-process))
651 (with-file-modes 448
652 (setq process (make-process :name "epg"
653 :buffer buffer
654 :command (cons (epg-context-program context)
655 args)
656 :connection-type 'pipe
657 :coding '(binary . binary)
658 :filter #'epg--process-filter
659 :stderr error-process
660 :noquery t)))
661 (setf (epg-context-process context) process)))
663 (defun epg--process-filter (process input)
664 (if epg-debug
665 (with-current-buffer
666 (or epg-debug-buffer
667 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
668 (goto-char (point-max))
669 (insert input)))
670 (if (buffer-live-p (process-buffer process))
671 (with-current-buffer (process-buffer process)
672 (save-excursion
673 (goto-char (point-max))
674 (insert input)
675 (unless epg-process-filter-running
676 (let ((epg-process-filter-running t))
677 (goto-char epg-read-point)
678 (beginning-of-line)
679 (while (looking-at ".*\n") ;the input line finished
680 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
681 (let ((status (match-string 1))
682 (string (match-string 2))
683 symbol)
684 (if (member status epg-pending-status-list)
685 (setq epg-pending-status-list nil))
686 ;; When editing a key, delegate all interaction
687 ;; to edit-callback.
688 (if (eq (epg-context-operation epg-context) 'edit-key)
689 (funcall (car (epg-context-edit-callback
690 epg-context))
691 epg-context
692 status
693 string
694 (cdr (epg-context-edit-callback
695 epg-context)))
696 ;; Otherwise call epg--status-STATUS function.
697 (setq symbol (intern-soft (concat "epg--status-"
698 status)))
699 (if (and symbol
700 (fboundp symbol))
701 (funcall symbol epg-context string)))
702 (setq epg-last-status (cons status string))))
703 (forward-line)
704 (setq epg-read-point (point)))))))))
706 (defun epg-read-output (context)
707 "Read the output file CONTEXT and return the content as a string."
708 (with-temp-buffer
709 (if (fboundp 'set-buffer-multibyte)
710 (set-buffer-multibyte nil))
711 (if (file-exists-p (epg-context-output-file context))
712 (let ((coding-system-for-read 'binary))
713 (insert-file-contents (epg-context-output-file context))
714 (buffer-string)))))
716 (defun epg-wait-for-status (context status-list)
717 "Wait until one of elements in STATUS-LIST arrives."
718 (with-current-buffer (process-buffer (epg-context-process context))
719 (setq epg-pending-status-list status-list)
720 (while (and (eq (process-status (epg-context-process context)) 'run)
721 epg-pending-status-list)
722 (accept-process-output (epg-context-process context) 1))
723 (if epg-pending-status-list
724 (epg-context-set-result-for
725 context 'error
726 (cons '(exit)
727 (epg-context-result-for context 'error))))))
729 (defun epg-wait-for-completion (context)
730 "Wait until the `epg-gpg-program' process completes."
731 (while (eq (process-status (epg-context-process context)) 'run)
732 (accept-process-output (epg-context-process context) 1))
733 ;; This line is needed to run the process-filter right now.
734 (sleep-for 0.1)
735 ;; Restore Emacs frame on text terminal, when pinentry-curses has terminated.
736 (if (with-current-buffer (process-buffer (epg-context-process context))
737 (and epg-agent-file
738 (> (float-time (or (nth 5 (file-attributes epg-agent-file))
739 '(0 0 0 0)))
740 (float-time epg-agent-mtime))))
741 (redraw-frame))
742 (epg-context-set-result-for
743 context 'error
744 (nreverse (epg-context-result-for context 'error)))
745 (setf (epg-context-error-output context)
746 (with-current-buffer (epg-context-error-buffer context)
747 (buffer-string))))
749 (defun epg-reset (context)
750 "Reset the CONTEXT."
751 (if (and (epg-context-process context)
752 (buffer-live-p (process-buffer (epg-context-process context))))
753 (kill-buffer (process-buffer (epg-context-process context))))
754 (if (buffer-live-p (epg-context-error-buffer context))
755 (kill-buffer (epg-context-error-buffer context)))
756 (setf (epg-context-process context) nil)
757 (setf (epg-context-edit-callback context) nil))
759 (defun epg-delete-output-file (context)
760 "Delete the output file of CONTEXT."
761 (if (and (epg-context-output-file context)
762 (file-exists-p (epg-context-output-file context)))
763 (delete-file (epg-context-output-file context))))
765 (eval-and-compile
766 (if (fboundp 'decode-coding-string)
767 (defalias 'epg--decode-coding-string 'decode-coding-string)
768 (defalias 'epg--decode-coding-string 'identity)))
770 (defun epg--status-USERID_HINT (_context string)
771 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
772 (let* ((key-id (match-string 1 string))
773 (user-id (match-string 2 string))
774 (entry (assoc key-id epg-user-id-alist)))
775 (condition-case nil
776 (setq user-id (epg--decode-coding-string
777 (epg--decode-percent-escape user-id)
778 'utf-8))
779 (error))
780 (if entry
781 (setcdr entry user-id)
782 (setq epg-user-id-alist (cons (cons key-id user-id)
783 epg-user-id-alist))))))
785 (defun epg--status-NEED_PASSPHRASE (_context string)
786 (if (string-match "\\`\\([^ ]+\\)" string)
787 (setq epg-key-id (match-string 1 string))))
789 (defun epg--status-NEED_PASSPHRASE_SYM (_context _string)
790 (setq epg-key-id 'SYM))
792 (defun epg--status-NEED_PASSPHRASE_PIN (_context _string)
793 (setq epg-key-id 'PIN))
795 (eval-and-compile
796 (if (fboundp 'clear-string)
797 (defalias 'epg--clear-string 'clear-string)
798 (defun epg--clear-string (string)
799 (fillarray string 0))))
801 (eval-and-compile
802 (if (fboundp 'encode-coding-string)
803 (defalias 'epg--encode-coding-string 'encode-coding-string)
804 (defalias 'epg--encode-coding-string 'identity)))
806 (defun epg--status-GET_HIDDEN (context string)
807 (when (and epg-key-id
808 (string-match "\\`passphrase\\." string))
809 (unless (epg-context-passphrase-callback context)
810 (error "passphrase-callback not set"))
811 (let (inhibit-quit
812 passphrase
813 passphrase-with-new-line
814 encoded-passphrase-with-new-line)
815 (unwind-protect
816 (condition-case nil
817 (progn
818 (setq passphrase
819 (funcall
820 (car (epg-context-passphrase-callback context))
821 context
822 epg-key-id
823 (cdr (epg-context-passphrase-callback context))))
824 (when passphrase
825 (setq passphrase-with-new-line (concat passphrase "\n"))
826 (epg--clear-string passphrase)
827 (setq passphrase nil)
828 (if epg-passphrase-coding-system
829 (progn
830 (setq encoded-passphrase-with-new-line
831 (epg--encode-coding-string
832 passphrase-with-new-line
833 (coding-system-change-eol-conversion
834 epg-passphrase-coding-system 'unix)))
835 (epg--clear-string passphrase-with-new-line)
836 (setq passphrase-with-new-line nil))
837 (setq encoded-passphrase-with-new-line
838 passphrase-with-new-line
839 passphrase-with-new-line nil))
840 (process-send-string (epg-context-process context)
841 encoded-passphrase-with-new-line)))
842 (quit
843 (epg-context-set-result-for
844 context 'error
845 (cons '(quit)
846 (epg-context-result-for context 'error)))
847 (delete-process (epg-context-process context))))
848 (if passphrase
849 (epg--clear-string passphrase))
850 (if passphrase-with-new-line
851 (epg--clear-string passphrase-with-new-line))
852 (if encoded-passphrase-with-new-line
853 (epg--clear-string encoded-passphrase-with-new-line))))))
855 (defun epg--prompt-GET_BOOL (_context string)
856 (let ((entry (assoc string epg-prompt-alist)))
857 (y-or-n-p (if entry (cdr entry) (concat string "? ")))))
859 (defun epg--prompt-GET_BOOL-untrusted_key.override (_context _string)
860 (y-or-n-p (if (and (equal (car epg-last-status) "USERID_HINT")
861 (string-match "\\`\\([^ ]+\\) \\(.*\\)"
862 (cdr epg-last-status)))
863 (let* ((key-id (match-string 1 (cdr epg-last-status)))
864 (user-id (match-string 2 (cdr epg-last-status)))
865 (entry (assoc key-id epg-user-id-alist)))
866 (if entry
867 (setq user-id (cdr entry)))
868 (format "Untrusted key %s %s. Use anyway? " key-id user-id))
869 "Use untrusted key anyway? ")))
871 (defun epg--status-GET_BOOL (context string)
872 (let (inhibit-quit)
873 (condition-case nil
874 (if (funcall (or (intern-soft (concat "epg--prompt-GET_BOOL-" string))
875 #'epg--prompt-GET_BOOL)
876 context string)
877 (process-send-string (epg-context-process context) "y\n")
878 (process-send-string (epg-context-process context) "n\n"))
879 (quit
880 (epg-context-set-result-for
881 context 'error
882 (cons '(quit)
883 (epg-context-result-for context 'error)))
884 (delete-process (epg-context-process context))))))
886 (defun epg--status-GET_LINE (context string)
887 (let ((entry (assoc string epg-prompt-alist))
888 inhibit-quit)
889 (condition-case nil
890 (process-send-string (epg-context-process context)
891 (concat (read-string
892 (if entry
893 (cdr entry)
894 (concat string ": ")))
895 "\n"))
896 (quit
897 (epg-context-set-result-for
898 context 'error
899 (cons '(quit)
900 (epg-context-result-for context 'error)))
901 (delete-process (epg-context-process context))))))
903 (defun epg--status-*SIG (context status string)
904 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
905 (let* ((key-id (match-string 1 string))
906 (user-id (match-string 2 string))
907 (entry (assoc key-id epg-user-id-alist)))
908 (epg-context-set-result-for
909 context
910 'verify
911 (cons (epg-make-signature status key-id)
912 (epg-context-result-for context 'verify)))
913 (condition-case nil
914 (if (eq (epg-context-protocol context) 'CMS)
915 (setq user-id (epg-dn-from-string user-id))
916 (setq user-id (epg--decode-coding-string
917 (epg--decode-percent-escape user-id)
918 'utf-8)))
919 (error))
920 (if entry
921 (setcdr entry user-id)
922 (setq epg-user-id-alist
923 (cons (cons key-id user-id) epg-user-id-alist))))
924 (epg-context-set-result-for
925 context
926 'verify
927 (cons (epg-make-signature status)
928 (epg-context-result-for context 'verify)))))
930 (defun epg--status-GOODSIG (context string)
931 (epg--status-*SIG context 'good string))
933 (defun epg--status-EXPSIG (context string)
934 (epg--status-*SIG context 'expired string))
936 (defun epg--status-EXPKEYSIG (context string)
937 (epg--status-*SIG context 'expired-key string))
939 (defun epg--status-REVKEYSIG (context string)
940 (epg--status-*SIG context 'revoked-key string))
942 (defun epg--status-BADSIG (context string)
943 (epg--status-*SIG context 'bad string))
945 (defun epg--status-NO_PUBKEY (context string)
946 (if (eq (epg-context-operation context) 'verify)
947 (let ((signature (car (epg-context-result-for context 'verify))))
948 (if (and signature
949 (eq (epg-signature-status signature) 'error)
950 (equal (epg-signature-key-id signature) string))
951 (setf (epg-signature-status signature) 'no-pubkey)))
952 (epg-context-set-result-for
953 context 'error
954 (cons (cons 'no-pubkey string)
955 (epg-context-result-for context 'error)))))
957 (defun epg--status-NO_SECKEY (context string)
958 (epg-context-set-result-for
959 context 'error
960 (cons (cons 'no-seckey string)
961 (epg-context-result-for context 'error))))
963 (defun epg--time-from-seconds (seconds)
964 (let ((number-seconds (string-to-number (concat seconds ".0"))))
965 (cons (floor (/ number-seconds 65536))
966 (floor (mod number-seconds 65536)))))
968 (defun epg--status-ERRSIG (context string)
969 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
970 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
971 string)
972 (let ((signature (epg-make-signature 'error)))
973 (epg-context-set-result-for
974 context
975 'verify
976 (cons signature
977 (epg-context-result-for context 'verify)))
978 (setf (epg-signature-key-id signature)
979 (match-string 1 string))
980 (setf (epg-signature-pubkey-algorithm signature)
981 (string-to-number (match-string 2 string)))
982 (setf (epg-signature-digest-algorithm signature)
983 (string-to-number (match-string 3 string)))
984 (setf (epg-signature-class signature)
985 (string-to-number (match-string 4 string) 16))
986 (setf (epg-signature-creation-time signature)
987 (epg--time-from-seconds (match-string 5 string))))))
989 (defun epg--status-VALIDSIG (context string)
990 (let ((signature (car (epg-context-result-for context 'verify))))
991 (when (and signature
992 (eq (epg-signature-status signature) 'good)
993 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
994 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
995 \\(.*\\)"
996 string))
997 (setf (epg-signature-fingerprint signature)
998 (match-string 1 string))
999 (setf (epg-signature-creation-time signature)
1000 (epg--time-from-seconds (match-string 2 string)))
1001 (unless (equal (match-string 3 string) "0")
1002 (setf (epg-signature-expiration-time signature)
1003 (epg--time-from-seconds (match-string 3 string))))
1004 (setf (epg-signature-version signature)
1005 (string-to-number (match-string 4 string)))
1006 (setf (epg-signature-pubkey-algorithm signature)
1007 (string-to-number (match-string 5 string)))
1008 (setf (epg-signature-digest-algorithm signature)
1009 (string-to-number (match-string 6 string)))
1010 (setf (epg-signature-class signature)
1011 (string-to-number (match-string 7 string) 16)))))
1013 (defun epg--status-TRUST_UNDEFINED (context _string)
1014 (let ((signature (car (epg-context-result-for context 'verify))))
1015 (if (and signature
1016 (eq (epg-signature-status signature) 'good))
1017 (setf (epg-signature-validity signature) 'undefined))))
1019 (defun epg--status-TRUST_NEVER (context _string)
1020 (let ((signature (car (epg-context-result-for context 'verify))))
1021 (if (and signature
1022 (eq (epg-signature-status signature) 'good))
1023 (setf (epg-signature-validity signature) 'never))))
1025 (defun epg--status-TRUST_MARGINAL (context _string)
1026 (let ((signature (car (epg-context-result-for context 'verify))))
1027 (if (and signature
1028 (eq (epg-signature-status signature) 'marginal))
1029 (setf (epg-signature-validity signature) 'marginal))))
1031 (defun epg--status-TRUST_FULLY (context _string)
1032 (let ((signature (car (epg-context-result-for context 'verify))))
1033 (if (and signature
1034 (eq (epg-signature-status signature) 'good))
1035 (setf (epg-signature-validity signature) 'full))))
1037 (defun epg--status-TRUST_ULTIMATE (context _string)
1038 (let ((signature (car (epg-context-result-for context 'verify))))
1039 (if (and signature
1040 (eq (epg-signature-status signature) 'good))
1041 (setf (epg-signature-validity signature) 'ultimate))))
1043 (defun epg--status-NOTATION_NAME (context string)
1044 (let ((signature (car (epg-context-result-for context 'verify))))
1045 (if signature
1046 (push (epg-make-sig-notation string nil t nil)
1047 (epg-signature-notations signature)))))
1049 (defun epg--status-NOTATION_DATA (context string)
1050 (let ((signature (car (epg-context-result-for context 'verify)))
1051 notation)
1052 (if (and signature
1053 (setq notation (car (epg-signature-notations signature))))
1054 (setf (epg-sig-notation-value notation) string))))
1056 (defun epg--status-POLICY_URL (context string)
1057 (let ((signature (car (epg-context-result-for context 'verify))))
1058 (if signature
1059 (push (epg-make-sig-notation nil string t nil)
1060 (epg-signature-notations signature)))))
1062 (defun epg--status-PROGRESS (context string)
1063 (if (and (epg-context-progress-callback context)
1064 (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1065 string))
1066 (funcall (car (epg-context-progress-callback context))
1067 context
1068 (match-string 1 string)
1069 (match-string 2 string)
1070 (string-to-number (match-string 3 string))
1071 (string-to-number (match-string 4 string))
1072 (cdr (epg-context-progress-callback context)))))
1074 (defun epg--status-ENC_TO (context string)
1075 (if (string-match "\\`\\([0-9A-Za-z]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
1076 (epg-context-set-result-for
1077 context 'encrypted-to
1078 (cons (list (match-string 1 string)
1079 (string-to-number (match-string 2 string))
1080 (string-to-number (match-string 3 string)))
1081 (epg-context-result-for context 'encrypted-to)))))
1083 (defun epg--status-DECRYPTION_FAILED (context _string)
1084 (epg-context-set-result-for context 'decryption-failed t))
1086 (defun epg--status-DECRYPTION_OKAY (context _string)
1087 (epg-context-set-result-for context 'decryption-okay t))
1089 (defun epg--status-NODATA (context string)
1090 (epg-context-set-result-for
1091 context 'error
1092 (cons (cons 'no-data (string-to-number string))
1093 (epg-context-result-for context 'error))))
1095 (defun epg--status-UNEXPECTED (context string)
1096 (epg-context-set-result-for
1097 context 'error
1098 (cons (cons 'unexpected (string-to-number string))
1099 (epg-context-result-for context 'error))))
1101 (defun epg--status-KEYEXPIRED (context string)
1102 (epg-context-set-result-for
1103 context 'key
1104 (cons (list 'key-expired (cons 'expiration-time
1105 (epg--time-from-seconds string)))
1106 (epg-context-result-for context 'key))))
1108 (defun epg--status-KEYREVOKED (context _string)
1109 (epg-context-set-result-for
1110 context 'key
1111 (cons '(key-revoked)
1112 (epg-context-result-for context 'key))))
1114 (defun epg--status-BADARMOR (context _string)
1115 (epg-context-set-result-for
1116 context 'error
1117 (cons '(bad-armor)
1118 (epg-context-result-for context 'error))))
1120 (defun epg--status-INV_RECP (context string)
1121 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1122 (epg-context-set-result-for
1123 context 'error
1124 (cons (list 'invalid-recipient
1125 (cons 'reason
1126 (string-to-number (match-string 1 string)))
1127 (cons 'requested
1128 (match-string 2 string)))
1129 (epg-context-result-for context 'error)))))
1131 (defun epg--status-INV_SGNR (context string)
1132 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1133 (epg-context-set-result-for
1134 context 'error
1135 (cons (list 'invalid-signer
1136 (cons 'reason
1137 (string-to-number (match-string 1 string)))
1138 (cons 'requested
1139 (match-string 2 string)))
1140 (epg-context-result-for context 'error)))))
1142 (defun epg--status-NO_RECP (context _string)
1143 (epg-context-set-result-for
1144 context 'error
1145 (cons '(no-recipients)
1146 (epg-context-result-for context 'error))))
1148 (defun epg--status-NO_SGNR (context _string)
1149 (epg-context-set-result-for
1150 context 'error
1151 (cons '(no-signers)
1152 (epg-context-result-for context 'error))))
1154 (defun epg--status-DELETE_PROBLEM (context string)
1155 (if (string-match "\\`\\([0-9]+\\)" string)
1156 (epg-context-set-result-for
1157 context 'error
1158 (cons (cons 'delete-problem
1159 (string-to-number (match-string 1 string)))
1160 (epg-context-result-for context 'error)))))
1162 (defun epg--status-SIG_CREATED (context string)
1163 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1164 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1165 (epg-context-set-result-for
1166 context 'sign
1167 (cons (epg-make-new-signature
1168 (cdr (assq (aref (match-string 1 string) 0)
1169 epg-new-signature-type-alist))
1170 (string-to-number (match-string 2 string))
1171 (string-to-number (match-string 3 string))
1172 (string-to-number (match-string 4 string) 16)
1173 (epg--time-from-seconds (match-string 5 string))
1174 (substring string (match-end 0)))
1175 (epg-context-result-for context 'sign)))))
1177 (defun epg--status-KEY_CREATED (context string)
1178 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1179 (epg-context-set-result-for
1180 context 'generate-key
1181 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1182 (cons 'fingerprint (match-string 2 string)))
1183 (epg-context-result-for context 'generate-key)))))
1185 (defun epg--status-KEY_NOT_CREATED (context _string)
1186 (epg-context-set-result-for
1187 context 'error
1188 (cons '(key-not-created)
1189 (epg-context-result-for context 'error))))
1191 (defun epg--status-IMPORTED (_context string)
1192 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1193 (let* ((key-id (match-string 1 string))
1194 (user-id (match-string 2 string))
1195 (entry (assoc key-id epg-user-id-alist)))
1196 (condition-case nil
1197 (setq user-id (epg--decode-coding-string
1198 (epg--decode-percent-escape user-id)
1199 'utf-8))
1200 (error))
1201 (if entry
1202 (setcdr entry user-id)
1203 (setq epg-user-id-alist (cons (cons key-id user-id)
1204 epg-user-id-alist))))))
1206 (defun epg--status-IMPORT_OK (context string)
1207 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1208 (let ((reason (string-to-number (match-string 1 string))))
1209 (epg-context-set-result-for
1210 context 'import-status
1211 (cons (epg-make-import-status (if (match-beginning 2)
1212 (match-string 3 string))
1214 (/= (logand reason 1) 0)
1215 (/= (logand reason 2) 0)
1216 (/= (logand reason 4) 0)
1217 (/= (logand reason 8) 0)
1218 (/= (logand reason 16) 0))
1219 (epg-context-result-for context 'import-status))))))
1221 (defun epg--status-IMPORT_PROBLEM (context string)
1222 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1223 (epg-context-set-result-for
1224 context 'import-status
1225 (cons (epg-make-import-status
1226 (if (match-beginning 2)
1227 (match-string 3 string))
1228 (string-to-number (match-string 1 string)))
1229 (epg-context-result-for context 'import-status)))))
1231 (defun epg--status-IMPORT_RES (context string)
1232 (when (string-match "\\`\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1233 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1234 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
1235 (epg-context-set-result-for
1236 context 'import
1237 (epg-make-import-result (string-to-number (match-string 1 string))
1238 (string-to-number (match-string 2 string))
1239 (string-to-number (match-string 3 string))
1240 (string-to-number (match-string 4 string))
1241 (string-to-number (match-string 5 string))
1242 (string-to-number (match-string 6 string))
1243 (string-to-number (match-string 7 string))
1244 (string-to-number (match-string 8 string))
1245 (string-to-number (match-string 9 string))
1246 (string-to-number (match-string 10 string))
1247 (string-to-number (match-string 11 string))
1248 (string-to-number (match-string 12 string))
1249 (string-to-number (match-string 13 string))
1250 (epg-context-result-for context 'import-status)))
1251 (epg-context-set-result-for context 'import-status nil)))
1253 (defun epg-passphrase-callback-function (context key-id _handback)
1254 (declare (obsolete epa-passphrase-callback-function "23.1"))
1255 (if (eq key-id 'SYM)
1256 (read-passwd "Passphrase for symmetric encryption: "
1257 (eq (epg-context-operation context) 'encrypt))
1258 (read-passwd
1259 (if (eq key-id 'PIN)
1260 "Passphrase for PIN: "
1261 (let ((entry (assoc key-id epg-user-id-alist)))
1262 (if entry
1263 (format "Passphrase for %s %s: " key-id (cdr entry))
1264 (format "Passphrase for %s: " key-id)))))))
1266 (defun epg--list-keys-1 (context name mode)
1267 (let ((args (append (if (epg-context-home-directory context)
1268 (list "--homedir"
1269 (epg-context-home-directory context)))
1270 '("--with-colons" "--no-greeting" "--batch"
1271 "--with-fingerprint" "--with-fingerprint")
1272 (unless (eq (epg-context-protocol context) 'CMS)
1273 '("--fixed-list-mode"))))
1274 (list-keys-option (if (memq mode '(t secret))
1275 "--list-secret-keys"
1276 (if (memq mode '(nil public))
1277 "--list-keys"
1278 "--list-sigs")))
1279 (coding-system-for-read 'binary)
1280 keys string field index)
1281 (if name
1282 (progn
1283 (unless (listp name)
1284 (setq name (list name)))
1285 (while name
1286 (setq args (append args (list list-keys-option (car name)))
1287 name (cdr name))))
1288 (setq args (append args (list list-keys-option))))
1289 (with-temp-buffer
1290 (apply #'call-process
1291 (epg-context-program context)
1292 nil (list t nil) nil args)
1293 (goto-char (point-min))
1294 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1295 (setq keys (cons (make-vector 15 nil) keys)
1296 string (match-string 0)
1297 index 0
1298 field 0)
1299 (while (and (< field (length (car keys)))
1300 (eq index
1301 (string-match "\\([^:]+\\)?:" string index)))
1302 (setq index (match-end 0))
1303 (aset (car keys) field (match-string 1 string))
1304 (setq field (1+ field))))
1305 (nreverse keys))))
1307 (defun epg--make-sub-key-1 (line)
1308 (epg-make-sub-key
1309 (if (aref line 1)
1310 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1311 (delq nil
1312 (mapcar (lambda (char) (cdr (assq char epg-key-capability-alist)))
1313 (aref line 11)))
1314 (member (aref line 0) '("sec" "ssb"))
1315 (string-to-number (aref line 3))
1316 (string-to-number (aref line 2))
1317 (aref line 4)
1318 (epg--time-from-seconds (aref line 5))
1319 (if (aref line 6)
1320 (epg--time-from-seconds (aref line 6)))))
1322 (defun epg-list-keys (context &optional name mode)
1323 "Return a list of epg-key objects matched with NAME.
1324 If MODE is nil or 'public, only public keyring should be searched.
1325 If MODE is t or 'secret, only secret keyring should be searched.
1326 Otherwise, only public keyring should be searched and the key
1327 signatures should be included.
1328 NAME is either a string or a list of strings."
1329 (let ((lines (epg--list-keys-1 context name mode))
1330 keys cert pointer pointer-1 index string)
1331 (while lines
1332 (cond
1333 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1334 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1335 keys (cons (epg-make-key
1336 (if (aref (car lines) 8)
1337 (cdr (assq (string-to-char (aref (car lines) 8))
1338 epg-key-validity-alist))))
1339 keys))
1340 (push (epg--make-sub-key-1 (car lines))
1341 (epg-key-sub-key-list (car keys))))
1342 ((member (aref (car lines) 0) '("sub" "ssb"))
1343 (push (epg--make-sub-key-1 (car lines))
1344 (epg-key-sub-key-list (car keys))))
1345 ((equal (aref (car lines) 0) "uid")
1346 ;; Decode the UID name as a backslash escaped UTF-8 string,
1347 ;; generated by GnuPG/GpgSM.
1348 (setq string (copy-sequence (aref (car lines) 9))
1349 index 0)
1350 (while (string-match "\"" string index)
1351 (setq string (replace-match "\\\"" t t string)
1352 index (1+ (match-end 0))))
1353 (condition-case nil
1354 (setq string (epg--decode-coding-string
1355 (car (read-from-string (concat "\"" string "\"")))
1356 'utf-8))
1357 (error
1358 (setq string (aref (car lines) 9))))
1359 (push (epg-make-user-id
1360 (if (aref (car lines) 1)
1361 (cdr (assq (string-to-char (aref (car lines) 1))
1362 epg-key-validity-alist)))
1363 (if cert
1364 (condition-case nil
1365 (epg-dn-from-string string)
1366 (error string))
1367 string))
1368 (epg-key-user-id-list (car keys))))
1369 ((equal (aref (car lines) 0) "fpr")
1370 (setf (epg-sub-key-fingerprint (car (epg-key-sub-key-list (car keys))))
1371 (aref (car lines) 9)))
1372 ((equal (aref (car lines) 0) "sig")
1373 (push
1374 (epg-make-key-signature
1375 (if (aref (car lines) 1)
1376 (cdr (assq (string-to-char (aref (car lines) 1))
1377 epg-key-validity-alist)))
1378 (string-to-number (aref (car lines) 3))
1379 (aref (car lines) 4)
1380 (epg--time-from-seconds (aref (car lines) 5))
1381 (epg--time-from-seconds (aref (car lines) 6))
1382 (aref (car lines) 9)
1383 (string-to-number (aref (car lines) 10) 16)
1384 (eq (aref (aref (car lines) 10) 2) ?x))
1385 (epg-user-id-signature-list
1386 (car (epg-key-user-id-list (car keys)))))))
1387 (setq lines (cdr lines)))
1388 (setq keys (nreverse keys)
1389 pointer keys)
1390 (while pointer
1391 (epg--gv-nreverse (epg-key-sub-key-list (car pointer)))
1392 (setq pointer-1 (epg--gv-nreverse (epg-key-user-id-list (car pointer))))
1393 (while pointer-1
1394 (epg--gv-nreverse (epg-user-id-signature-list (car pointer-1)))
1395 (setq pointer-1 (cdr pointer-1)))
1396 (setq pointer (cdr pointer)))
1397 keys))
1399 (eval-and-compile
1400 (if (fboundp 'make-temp-file)
1401 (defalias 'epg--make-temp-file 'make-temp-file)
1402 (defvar temporary-file-directory)
1403 ;; stolen from poe.el.
1404 (defun epg--make-temp-file (prefix)
1405 "Create a temporary file.
1406 The returned file name (created by appending some random characters at the end
1407 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1408 is guaranteed to point to a newly created empty file.
1409 You can then use `write-region' to write new data into the file."
1410 (let ((orig-modes (default-file-modes))
1411 tempdir tempfile)
1412 (setq prefix (expand-file-name prefix
1413 (if (featurep 'xemacs)
1414 (temp-directory)
1415 temporary-file-directory)))
1416 (unwind-protect
1417 (let (file)
1418 ;; First, create a temporary directory.
1419 (set-default-file-modes #o700)
1420 (while (condition-case ()
1421 (progn
1422 (setq tempdir (make-temp-name
1423 (concat
1424 (file-name-directory prefix)
1425 "DIR")))
1426 ;; return nil or signal an error.
1427 (make-directory tempdir))
1428 ;; let's try again.
1429 (file-already-exists t)))
1430 ;; Second, create a temporary file in the tempdir.
1431 ;; There *is* a race condition between `make-temp-name'
1432 ;; and `write-region', but we don't care it since we are
1433 ;; in a private directory now.
1434 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1435 (write-region "" nil tempfile nil 'silent)
1436 ;; Finally, make a hard-link from the tempfile.
1437 (while (condition-case ()
1438 (progn
1439 (setq file (make-temp-name prefix))
1440 ;; return nil or signal an error.
1441 (add-name-to-file tempfile file))
1442 ;; let's try again.
1443 (file-already-exists t)))
1444 file)
1445 (set-default-file-modes orig-modes)
1446 ;; Cleanup the tempfile.
1447 (and tempfile
1448 (file-exists-p tempfile)
1449 (delete-file tempfile))
1450 ;; Cleanup the tempdir.
1451 (and tempdir
1452 (file-directory-p tempdir)
1453 (delete-directory tempdir)))))))
1455 (defun epg--args-from-sig-notations (notations)
1456 (apply #'nconc
1457 (mapcar
1458 (lambda (notation)
1459 (if (and (epg-sig-notation-name notation)
1460 (not (epg-sig-notation-human-readable notation)))
1461 (error "Unreadable"))
1462 (if (epg-sig-notation-name notation)
1463 (list "--sig-notation"
1464 (if (epg-sig-notation-critical notation)
1465 (concat "!" (epg-sig-notation-name notation)
1466 "=" (epg-sig-notation-value notation))
1467 (concat (epg-sig-notation-name notation)
1468 "=" (epg-sig-notation-value notation))))
1469 (list "--sig-policy-url"
1470 (if (epg-sig-notation-critical notation)
1471 (concat "!" (epg-sig-notation-value notation))
1472 (epg-sig-notation-value notation)))))
1473 notations)))
1475 (defun epg-cancel (context)
1476 (if (buffer-live-p (process-buffer (epg-context-process context)))
1477 (with-current-buffer (process-buffer (epg-context-process context))
1478 (epg-context-set-result-for
1479 epg-context 'error
1480 (cons '(quit)
1481 (epg-context-result-for epg-context 'error)))))
1482 (if (eq (process-status (epg-context-process context)) 'run)
1483 (delete-process (epg-context-process context))))
1485 (defun epg-start-decrypt (context cipher)
1486 "Initiate a decrypt operation on CIPHER.
1487 CIPHER must be a file data object.
1489 If you use this function, you will need to wait for the completion of
1490 `epg-gpg-program' by using `epg-wait-for-completion' and call
1491 `epg-reset' to clear a temporary output file.
1492 If you are unsure, use synchronous version of this function
1493 `epg-decrypt-file' or `epg-decrypt-string' instead."
1494 (unless (epg-data-file cipher)
1495 (error "Not a file"))
1496 (setf (epg-context-operation context) 'decrypt)
1497 (setf (epg-context-result context) nil)
1498 (epg--start context (list "--decrypt" "--" (epg-data-file cipher)))
1499 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1500 (unless (eq (epg-context-protocol context) 'CMS)
1501 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1503 (defun epg--check-error-for-decrypt (context)
1504 (let ((errors (epg-context-result-for context 'error)))
1505 (if (epg-context-result-for context 'decryption-failed)
1506 (signal 'epg-error
1507 (list "Decryption failed" (epg-errors-to-string errors))))
1508 (unless (epg-context-result-for context 'decryption-okay)
1509 (signal 'epg-error
1510 (list "Can't decrypt" (epg-errors-to-string errors))))))
1512 (defun epg-decrypt-file (context cipher plain)
1513 "Decrypt a file CIPHER and store the result to a file PLAIN.
1514 If PLAIN is nil, it returns the result as a string."
1515 (unwind-protect
1516 (progn
1517 (setf (epg-context-output-file context)
1518 (or plain (epg--make-temp-file "epg-output")))
1519 (epg-start-decrypt context (epg-make-data-from-file cipher))
1520 (epg-wait-for-completion context)
1521 (epg--check-error-for-decrypt context)
1522 (unless plain
1523 (epg-read-output context)))
1524 (unless plain
1525 (epg-delete-output-file context))
1526 (epg-reset context)))
1528 (defun epg-decrypt-string (context cipher)
1529 "Decrypt a string CIPHER and return the plain text."
1530 (let ((input-file (epg--make-temp-file "epg-input"))
1531 (coding-system-for-write 'binary))
1532 (unwind-protect
1533 (progn
1534 (write-region cipher nil input-file nil 'quiet)
1535 (setf (epg-context-output-file context)
1536 (epg--make-temp-file "epg-output"))
1537 (epg-start-decrypt context (epg-make-data-from-file input-file))
1538 (epg-wait-for-completion context)
1539 (epg--check-error-for-decrypt context)
1540 (epg-read-output context))
1541 (epg-delete-output-file context)
1542 (if (file-exists-p input-file)
1543 (delete-file input-file))
1544 (epg-reset context))))
1546 (defun epg-start-verify (context signature &optional signed-text)
1547 "Initiate a verify operation on SIGNATURE.
1548 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1550 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1551 For a normal or a cleartext signature, SIGNED-TEXT should be nil.
1553 If you use this function, you will need to wait for the completion of
1554 `epg-gpg-program' by using `epg-wait-for-completion' and call
1555 `epg-reset' to clear a temporary output file.
1556 If you are unsure, use synchronous version of this function
1557 `epg-verify-file' or `epg-verify-string' instead."
1558 (setf (epg-context-operation context) 'verify)
1559 (setf (epg-context-result context) nil)
1560 (if signed-text
1561 ;; Detached signature.
1562 (if (epg-data-file signed-text)
1563 (epg--start context (list "--verify" "--" (epg-data-file signature)
1564 (epg-data-file signed-text)))
1565 (epg--start context (list "--verify" "--" (epg-data-file signature)
1566 "-"))
1567 (if (eq (process-status (epg-context-process context)) 'run)
1568 (process-send-string (epg-context-process context)
1569 (epg-data-string signed-text)))
1570 (if (eq (process-status (epg-context-process context)) 'run)
1571 (process-send-eof (epg-context-process context))))
1572 ;; Normal (or cleartext) signature.
1573 (if (epg-data-file signature)
1574 (epg--start context (if (eq (epg-context-protocol context) 'CMS)
1575 (list "--verify" "--" (epg-data-file signature))
1576 (list "--" (epg-data-file signature))))
1577 (epg--start context (if (eq (epg-context-protocol context) 'CMS)
1578 '("--verify" "-")
1579 '("-")))
1580 (if (eq (process-status (epg-context-process context)) 'run)
1581 (process-send-string (epg-context-process context)
1582 (epg-data-string signature)))
1583 (if (eq (process-status (epg-context-process context)) 'run)
1584 (process-send-eof (epg-context-process context))))))
1586 (defun epg-verify-file (context signature &optional signed-text plain)
1587 "Verify a file SIGNATURE.
1588 SIGNED-TEXT and PLAIN are also a file if they are specified.
1590 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1591 string. For a normal or a cleartext signature, SIGNED-TEXT should be
1592 nil. In the latter case, if PLAIN is specified, the plaintext is
1593 stored into the file after successful verification.
1595 Note that this function does not return verification result as t
1596 or nil, nor signal error on failure. That's a design decision to
1597 handle the case where SIGNATURE has multiple signature.
1599 To check the verification results, use `epg-context-result-for' as follows:
1601 \(epg-context-result-for context 'verify)
1603 which will return a list of `epg-signature' object."
1604 (unwind-protect
1605 (progn
1606 (setf (epg-context-output-file context)
1607 (or plain (epg--make-temp-file "epg-output")))
1608 (if signed-text
1609 (epg-start-verify context
1610 (epg-make-data-from-file signature)
1611 (epg-make-data-from-file signed-text))
1612 (epg-start-verify context
1613 (epg-make-data-from-file signature)))
1614 (epg-wait-for-completion context)
1615 (unless plain
1616 (epg-read-output context)))
1617 (unless plain
1618 (epg-delete-output-file context))
1619 (epg-reset context)))
1621 (defun epg-verify-string (context signature &optional signed-text)
1622 "Verify a string SIGNATURE.
1623 SIGNED-TEXT is a string if it is specified.
1625 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1626 string. For a normal or a cleartext signature, SIGNED-TEXT should be
1627 nil. In the latter case, this function returns the plaintext after
1628 successful verification.
1630 Note that this function does not return verification result as t
1631 or nil, nor signal error on failure. That's a design decision to
1632 handle the case where SIGNATURE has multiple signature.
1634 To check the verification results, use `epg-context-result-for' as follows:
1636 \(epg-context-result-for context 'verify)
1638 which will return a list of `epg-signature' object."
1639 (let ((coding-system-for-write 'binary)
1640 input-file)
1641 (unwind-protect
1642 (progn
1643 (setf (epg-context-output-file context)
1644 (epg--make-temp-file "epg-output"))
1645 (if signed-text
1646 (progn
1647 (setq input-file (epg--make-temp-file "epg-signature"))
1648 (write-region signature nil input-file nil 'quiet)
1649 (epg-start-verify context
1650 (epg-make-data-from-file input-file)
1651 (epg-make-data-from-string signed-text)))
1652 (epg-start-verify context (epg-make-data-from-string signature)))
1653 (epg-wait-for-completion context)
1654 (epg-read-output context))
1655 (epg-delete-output-file context)
1656 (if (and input-file
1657 (file-exists-p input-file))
1658 (delete-file input-file))
1659 (epg-reset context))))
1661 (defun epg-start-sign (context plain &optional mode)
1662 "Initiate a sign operation on PLAIN.
1663 PLAIN is a data object.
1665 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1666 If it is nil or 'normal, it makes a normal signature.
1667 Otherwise, it makes a cleartext signature.
1669 If you use this function, you will need to wait for the completion of
1670 `epg-gpg-program' by using `epg-wait-for-completion' and call
1671 `epg-reset' to clear a temporary output file.
1672 If you are unsure, use synchronous version of this function
1673 `epg-sign-file' or `epg-sign-string' instead."
1674 (setf (epg-context-operation context) 'sign)
1675 (setf (epg-context-result context) nil)
1676 (unless (memq mode '(t detached nil normal)) ;i.e. cleartext
1677 (epg-context-set-armor context nil)
1678 (epg-context-set-textmode context nil))
1679 (epg--start context
1680 (append (list (if (memq mode '(t detached))
1681 "--detach-sign"
1682 (if (memq mode '(nil normal))
1683 "--sign"
1684 "--clearsign")))
1685 (apply #'nconc
1686 (mapcar
1687 (lambda (signer)
1688 (list "-u"
1689 (epg-sub-key-id
1690 (car (epg-key-sub-key-list signer)))))
1691 (epg-context-signers context)))
1692 (epg--args-from-sig-notations
1693 (epg-context-sig-notations context))
1694 (if (epg-data-file plain)
1695 (list "--" (epg-data-file plain)))))
1696 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1697 (unless (eq (epg-context-protocol context) 'CMS)
1698 (epg-wait-for-status context '("BEGIN_SIGNING")))
1699 (when (epg-data-string plain)
1700 (if (eq (process-status (epg-context-process context)) 'run)
1701 (process-send-string (epg-context-process context)
1702 (epg-data-string plain)))
1703 (if (eq (process-status (epg-context-process context)) 'run)
1704 (process-send-eof (epg-context-process context)))))
1706 (defun epg-sign-file (context plain signature &optional mode)
1707 "Sign a file PLAIN and store the result to a file SIGNATURE.
1708 If SIGNATURE is nil, it returns the result as a string.
1709 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1710 If it is nil or 'normal, it makes a normal signature.
1711 Otherwise, it makes a cleartext signature."
1712 (unwind-protect
1713 (progn
1714 (setf (epg-context-output-file context)
1715 (or signature (epg--make-temp-file "epg-output")))
1716 (epg-start-sign context (epg-make-data-from-file plain) mode)
1717 (epg-wait-for-completion context)
1718 (unless (epg-context-result-for context 'sign)
1719 (let ((errors (epg-context-result-for context 'error)))
1720 (signal 'epg-error
1721 (list "Sign failed" (epg-errors-to-string errors)))))
1722 (unless signature
1723 (epg-read-output context)))
1724 (unless signature
1725 (epg-delete-output-file context))
1726 (epg-reset context)))
1728 (defun epg-sign-string (context plain &optional mode)
1729 "Sign a string PLAIN and return the output as string.
1730 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1731 If it is nil or 'normal, it makes a normal signature.
1732 Otherwise, it makes a cleartext signature."
1733 (let ((input-file
1734 (unless (or (eq (epg-context-protocol context) 'CMS)
1735 (condition-case nil
1736 (progn
1737 (epg-check-configuration (epg-configuration))
1739 (error)))
1740 (epg--make-temp-file "epg-input")))
1741 (coding-system-for-write 'binary))
1742 (unwind-protect
1743 (progn
1744 (setf (epg-context-output-file context)
1745 (epg--make-temp-file "epg-output"))
1746 (if input-file
1747 (write-region plain nil input-file nil 'quiet))
1748 (epg-start-sign context
1749 (if input-file
1750 (epg-make-data-from-file input-file)
1751 (epg-make-data-from-string plain))
1752 mode)
1753 (epg-wait-for-completion context)
1754 (unless (epg-context-result-for context 'sign)
1755 (if (epg-context-result-for context 'error)
1756 (let ((errors (epg-context-result-for context 'error)))
1757 (signal 'epg-error
1758 (list "Sign failed" (epg-errors-to-string errors))))))
1759 (epg-read-output context))
1760 (epg-delete-output-file context)
1761 (if input-file
1762 (delete-file input-file))
1763 (epg-reset context))))
1765 (defun epg-start-encrypt (context plain recipients
1766 &optional sign always-trust)
1767 "Initiate an encrypt operation on PLAIN.
1768 PLAIN is a data object.
1769 If RECIPIENTS is nil, it performs symmetric encryption.
1771 If you use this function, you will need to wait for the completion of
1772 `epg-gpg-program' by using `epg-wait-for-completion' and call
1773 `epg-reset' to clear a temporary output file.
1774 If you are unsure, use synchronous version of this function
1775 `epg-encrypt-file' or `epg-encrypt-string' instead."
1776 (setf (epg-context-operation context) 'encrypt)
1777 (setf (epg-context-result context) nil)
1778 (epg--start context
1779 (append (if always-trust '("--always-trust"))
1780 (if recipients '("--encrypt") '("--symmetric"))
1781 (if sign '("--sign"))
1782 (if sign
1783 (apply #'nconc
1784 (mapcar
1785 (lambda (signer)
1786 (list "-u"
1787 (epg-sub-key-id
1788 (car (epg-key-sub-key-list
1789 signer)))))
1790 (epg-context-signers context))))
1791 (if sign
1792 (epg--args-from-sig-notations
1793 (epg-context-sig-notations context)))
1794 (apply #'nconc
1795 (mapcar
1796 (lambda (recipient)
1797 (list "-r"
1798 (epg-sub-key-id
1799 (car (epg-key-sub-key-list recipient)))))
1800 recipients))
1801 (if (epg-data-file plain)
1802 (list "--" (epg-data-file plain)))))
1803 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1804 (unless (eq (epg-context-protocol context) 'CMS)
1805 (epg-wait-for-status context
1806 (if sign '("BEGIN_SIGNING") '("BEGIN_ENCRYPTION"))))
1807 (when (epg-data-string plain)
1808 (if (eq (process-status (epg-context-process context)) 'run)
1809 (process-send-string (epg-context-process context)
1810 (epg-data-string plain)))
1811 (if (eq (process-status (epg-context-process context)) 'run)
1812 (process-send-eof (epg-context-process context)))))
1814 (defun epg-encrypt-file (context plain recipients
1815 cipher &optional sign always-trust)
1816 "Encrypt a file PLAIN and store the result to a file CIPHER.
1817 If CIPHER is nil, it returns the result as a string.
1818 If RECIPIENTS is nil, it performs symmetric encryption."
1819 (unwind-protect
1820 (progn
1821 (setf (epg-context-output-file context)
1822 (or cipher (epg--make-temp-file "epg-output")))
1823 (epg-start-encrypt context (epg-make-data-from-file plain)
1824 recipients sign always-trust)
1825 (epg-wait-for-completion context)
1826 (let ((errors (epg-context-result-for context 'error)))
1827 (if (and sign
1828 (not (epg-context-result-for context 'sign)))
1829 (signal 'epg-error
1830 (list "Sign failed" (epg-errors-to-string errors))))
1831 (if errors
1832 (signal 'epg-error
1833 (list "Encrypt failed" (epg-errors-to-string errors)))))
1834 (unless cipher
1835 (epg-read-output context)))
1836 (unless cipher
1837 (epg-delete-output-file context))
1838 (epg-reset context)))
1840 (defun epg-encrypt-string (context plain recipients
1841 &optional sign always-trust)
1842 "Encrypt a string PLAIN.
1843 If RECIPIENTS is nil, it performs symmetric encryption."
1844 (let ((input-file
1845 (unless (or (not sign)
1846 (eq (epg-context-protocol context) 'CMS)
1847 (condition-case nil
1848 (progn
1849 (epg-check-configuration (epg-configuration))
1851 (error)))
1852 (epg--make-temp-file "epg-input")))
1853 (coding-system-for-write 'binary))
1854 (unwind-protect
1855 (progn
1856 (setf (epg-context-output-file context)
1857 (epg--make-temp-file "epg-output"))
1858 (if input-file
1859 (write-region plain nil input-file nil 'quiet))
1860 (epg-start-encrypt context
1861 (if input-file
1862 (epg-make-data-from-file input-file)
1863 (epg-make-data-from-string plain))
1864 recipients sign always-trust)
1865 (epg-wait-for-completion context)
1866 (let ((errors (epg-context-result-for context 'error)))
1867 (if (and sign
1868 (not (epg-context-result-for context 'sign)))
1869 (signal 'epg-error
1870 (list "Sign failed" (epg-errors-to-string errors))))
1871 (if errors
1872 (signal 'epg-error
1873 (list "Encrypt failed" (epg-errors-to-string errors)))))
1874 (epg-read-output context))
1875 (epg-delete-output-file context)
1876 (if input-file
1877 (delete-file input-file))
1878 (epg-reset context))))
1880 (defun epg-start-export-keys (context keys)
1881 "Initiate an export keys operation.
1883 If you use this function, you will need to wait for the completion of
1884 `epg-gpg-program' by using `epg-wait-for-completion' and call
1885 `epg-reset' to clear a temporary output file.
1886 If you are unsure, use synchronous version of this function
1887 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1888 (setf (epg-context-operation context) 'export-keys)
1889 (setf (epg-context-result context) nil)
1890 (epg--start context (cons "--export"
1891 (mapcar
1892 (lambda (key)
1893 (epg-sub-key-id
1894 (car (epg-key-sub-key-list key))))
1895 keys))))
1897 (defun epg-export-keys-to-file (context keys file)
1898 "Extract public KEYS."
1899 (unwind-protect
1900 (progn
1901 (setf (epg-context-output-file context)
1902 (or file (epg--make-temp-file "epg-output")))
1903 (epg-start-export-keys context keys)
1904 (epg-wait-for-completion context)
1905 (let ((errors (epg-context-result-for context 'error)))
1906 (if errors
1907 (signal 'epg-error
1908 (list "Export keys failed"
1909 (epg-errors-to-string errors)))))
1910 (unless file
1911 (epg-read-output context)))
1912 (unless file
1913 (epg-delete-output-file context))
1914 (epg-reset context)))
1916 (defun epg-export-keys-to-string (context keys)
1917 "Extract public KEYS and return them as a string."
1918 (epg-export-keys-to-file context keys nil))
1920 (defun epg-start-import-keys (context keys)
1921 "Initiate an import keys operation.
1922 KEYS is a data object.
1924 If you use this function, you will need to wait for the completion of
1925 `epg-gpg-program' by using `epg-wait-for-completion' and call
1926 `epg-reset' to clear a temporary output file.
1927 If you are unsure, use synchronous version of this function
1928 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1929 (setf (epg-context-operation context) 'import-keys)
1930 (setf (epg-context-result context) nil)
1931 (epg--start context (if (epg-data-file keys)
1932 (list "--import" "--" (epg-data-file keys))
1933 (list "--import")))
1934 (when (epg-data-string keys)
1935 (if (eq (process-status (epg-context-process context)) 'run)
1936 (process-send-string (epg-context-process context)
1937 (epg-data-string keys)))
1938 (if (eq (process-status (epg-context-process context)) 'run)
1939 (process-send-eof (epg-context-process context)))))
1941 (defun epg--import-keys-1 (context keys)
1942 (unwind-protect
1943 (progn
1944 (epg-start-import-keys context keys)
1945 (epg-wait-for-completion context)
1946 (let ((errors (epg-context-result-for context 'error)))
1947 (if errors
1948 (signal 'epg-error
1949 (list "Import keys failed"
1950 (epg-errors-to-string errors))))))
1951 (epg-reset context)))
1953 (defun epg-import-keys-from-file (context keys)
1954 "Add keys from a file KEYS."
1955 (epg--import-keys-1 context (epg-make-data-from-file keys)))
1957 (defun epg-import-keys-from-string (context keys)
1958 "Add keys from a string KEYS."
1959 (epg--import-keys-1 context (epg-make-data-from-string keys)))
1961 (defun epg-start-receive-keys (context key-id-list)
1962 "Initiate a receive key operation.
1963 KEY-ID-LIST is a list of key IDs.
1965 If you use this function, you will need to wait for the completion of
1966 `epg-gpg-program' by using `epg-wait-for-completion' and call
1967 `epg-reset' to clear a temporary output file.
1968 If you are unsure, use synchronous version of this function
1969 `epg-receive-keys' instead."
1970 (setf (epg-context-operation context) 'receive-keys)
1971 (setf (epg-context-result context) nil)
1972 (epg--start context (cons "--recv-keys" key-id-list)))
1974 (defun epg-receive-keys (context keys)
1975 "Add keys from server.
1976 KEYS is a list of key IDs"
1977 (unwind-protect
1978 (progn
1979 (epg-start-receive-keys context keys)
1980 (epg-wait-for-completion context)
1981 (let ((errors (epg-context-result-for context 'error)))
1982 (if errors
1983 (signal 'epg-error
1984 (list "Receive keys failed"
1985 (epg-errors-to-string errors))))))
1986 (epg-reset context)))
1988 (defalias 'epg-import-keys-from-server 'epg-receive-keys)
1990 (defun epg-start-delete-keys (context keys &optional allow-secret)
1991 "Initiate a delete keys operation.
1993 If you use this function, you will need to wait for the completion of
1994 `epg-gpg-program' by using `epg-wait-for-completion' and call
1995 `epg-reset' to clear a temporary output file.
1996 If you are unsure, use synchronous version of this function
1997 `epg-delete-keys' instead."
1998 (setf (epg-context-operation context) 'delete-keys)
1999 (setf (epg-context-result context) nil)
2000 (epg--start context (cons (if allow-secret
2001 "--delete-secret-key"
2002 "--delete-key")
2003 (mapcar
2004 (lambda (key)
2005 (epg-sub-key-id
2006 (car (epg-key-sub-key-list key))))
2007 keys))))
2009 (defun epg-delete-keys (context keys &optional allow-secret)
2010 "Delete KEYS from the key ring."
2011 (unwind-protect
2012 (progn
2013 (epg-start-delete-keys context keys allow-secret)
2014 (epg-wait-for-completion context)
2015 (let ((errors (epg-context-result-for context 'error)))
2016 (if errors
2017 (signal 'epg-error
2018 (list "Delete keys failed"
2019 (epg-errors-to-string errors))))))
2020 (epg-reset context)))
2022 (defun epg-start-sign-keys (context keys &optional local)
2023 "Initiate a sign keys operation.
2025 If you use this function, you will need to wait for the completion of
2026 `epg-gpg-program' by using `epg-wait-for-completion' and call
2027 `epg-reset' to clear a temporary output file.
2028 If you are unsure, use synchronous version of this function
2029 `epg-sign-keys' instead."
2030 (declare (obsolete nil "23.1"))
2031 (setf (epg-context-operation context) 'sign-keys)
2032 (setf (epg-context-result context) nil)
2033 (epg--start context (cons (if local
2034 "--lsign-key"
2035 "--sign-key")
2036 (mapcar
2037 (lambda (key)
2038 (epg-sub-key-id
2039 (car (epg-key-sub-key-list key))))
2040 keys))))
2042 (defun epg-sign-keys (context keys &optional local)
2043 "Sign KEYS from the key ring."
2044 (declare (obsolete nil "23.1"))
2045 (unwind-protect
2046 (progn
2047 (epg-start-sign-keys context keys local)
2048 (epg-wait-for-completion context)
2049 (let ((errors (epg-context-result-for context 'error)))
2050 (if errors
2051 (signal 'epg-error
2052 (list "Sign keys failed"
2053 (epg-errors-to-string errors))))))
2054 (epg-reset context)))
2056 (defun epg-start-generate-key (context parameters)
2057 "Initiate a key generation.
2058 PARAMETERS is a string which specifies parameters of the generated key.
2059 See Info node `(gnupg) Unattended GPG key generation' in the
2060 GnuPG manual for the format.
2062 If you use this function, you will need to wait for the completion of
2063 `epg-gpg-program' by using `epg-wait-for-completion' and call
2064 `epg-reset' to clear a temporary output file.
2065 If you are unsure, use synchronous version of this function
2066 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2067 (setf (epg-context-operation context) 'generate-key)
2068 (setf (epg-context-result context) nil)
2069 (if (epg-data-file parameters)
2070 (epg--start context (list "--batch" "--gen-key" "--"
2071 (epg-data-file parameters)))
2072 (epg--start context '("--batch" "--gen-key"))
2073 (if (eq (process-status (epg-context-process context)) 'run)
2074 (process-send-string (epg-context-process context)
2075 (epg-data-string parameters)))
2076 (if (eq (process-status (epg-context-process context)) 'run)
2077 (process-send-eof (epg-context-process context)))))
2079 (defun epg-generate-key-from-file (context parameters)
2080 "Generate a new key pair.
2081 PARAMETERS is a file which tells how to create the key."
2082 (unwind-protect
2083 (progn
2084 (epg-start-generate-key context (epg-make-data-from-file parameters))
2085 (epg-wait-for-completion context)
2086 (let ((errors (epg-context-result-for context 'error)))
2087 (if errors
2088 (signal 'epg-error
2089 (list "Generate key failed"
2090 (epg-errors-to-string errors))))))
2091 (epg-reset context)))
2093 (defun epg-generate-key-from-string (context parameters)
2094 "Generate a new key pair.
2095 PARAMETERS is a string which tells how to create the key."
2096 (unwind-protect
2097 (progn
2098 (epg-start-generate-key context (epg-make-data-from-string parameters))
2099 (epg-wait-for-completion context)
2100 (let ((errors (epg-context-result-for context 'error)))
2101 (if errors
2102 (signal 'epg-error
2103 (list "Generate key failed"
2104 (epg-errors-to-string errors))))))
2105 (epg-reset context)))
2107 (defun epg-start-edit-key (context key edit-callback handback)
2108 "Initiate an edit operation on KEY.
2110 EDIT-CALLBACK is called from process filter and takes 3
2111 arguments: the context, a status, an argument string, and the
2112 handback argument.
2114 If you use this function, you will need to wait for the completion of
2115 `epg-gpg-program' by using `epg-wait-for-completion' and call
2116 `epg-reset' to clear a temporary output file.
2117 If you are unsure, use synchronous version of this function
2118 `epg-edit-key' instead."
2119 (setf (epg-context-operation context) 'edit-key)
2120 (setf (epg-context-result context) nil)
2121 (setf (epg-context-edit-callback context) (cons edit-callback handback))
2122 (epg--start context (list "--edit-key"
2123 (epg-sub-key-id
2124 (car (epg-key-sub-key-list key))))))
2126 (defun epg-edit-key (context key edit-callback handback)
2127 "Edit KEY in the keyring."
2128 (unwind-protect
2129 (progn
2130 (epg-start-edit-key context key edit-callback handback)
2131 (epg-wait-for-completion context)
2132 (let ((errors (epg-context-result-for context 'error)))
2133 (if errors
2134 (signal 'epg-error
2135 (list "Edit key failed"
2136 (epg-errors-to-string errors))))))
2137 (epg-reset context)))
2139 (defun epg--decode-percent-escape (string)
2140 (let ((index 0))
2141 (while (string-match "%\\(\\(%\\)\\|\\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2142 string index)
2143 (if (match-beginning 2)
2144 (setq string (replace-match "%" t t string)
2145 index (1- (match-end 0)))
2146 (setq string (replace-match
2147 (string (string-to-number (match-string 3 string) 16))
2148 t t string)
2149 index (- (match-end 0) 2))))
2150 string))
2152 (defun epg--decode-hexstring (string)
2153 (let ((index 0))
2154 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
2155 (setq string (replace-match (string (string-to-number
2156 (match-string 0 string) 16))
2157 t t string)
2158 index (1- (match-end 0))))
2159 string))
2161 (defun epg--decode-quotedstring (string)
2162 (let ((index 0))
2163 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2164 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2165 string index)
2166 (if (match-beginning 2)
2167 (setq string (replace-match "\\2" t nil string)
2168 index (1- (match-end 0)))
2169 (if (match-beginning 3)
2170 (setq string (replace-match (string (string-to-number
2171 (match-string 0 string) 16))
2172 t t string)
2173 index (- (match-end 0) 2)))))
2174 string))
2176 (defun epg-dn-from-string (string)
2177 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2178 The return value is an alist mapping from types to values."
2179 (let ((index 0)
2180 (length (length string))
2181 alist type value group)
2182 (while (< index length)
2183 (if (eq index (string-match "[ \t\n\r]*" string index))
2184 (setq index (match-end 0)))
2185 (if (eq index (string-match
2186 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2187 string index))
2188 (setq type (match-string 1 string)
2189 index (match-end 0))
2190 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2191 string index))
2192 (setq type (match-string 1 string)
2193 index (match-end 0))))
2194 (unless type
2195 (error "Invalid type"))
2196 (if (eq index (string-match
2197 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2198 string index))
2199 (setq index (match-end 0)
2200 value (epg--decode-quotedstring (match-string 0 string)))
2201 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
2202 (setq index (match-end 0)
2203 value (epg--decode-hexstring (match-string 1 string)))
2204 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2205 string index))
2206 (setq index (match-end 0)
2207 value (epg--decode-quotedstring
2208 (match-string 0 string))))))
2209 (if group
2210 (if (stringp (car (car alist)))
2211 (setcar alist (list (cons type value) (car alist)))
2212 (setcar alist (cons (cons type value) (car alist))))
2213 (if (consp (car (car alist)))
2214 (setcar alist (nreverse (car alist))))
2215 (setq alist (cons (cons type value) alist)
2216 type nil
2217 value nil))
2218 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
2219 (setq index (match-end 0)
2220 group (eq (aref string (match-beginning 1)) ?+))))
2221 (nreverse alist)))
2223 (defun epg-decode-dn (alist)
2224 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2225 Type names are resolved using `epg-dn-type-alist'."
2226 (mapconcat
2227 (lambda (rdn)
2228 (if (stringp (car rdn))
2229 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
2230 (if entry
2231 (format "%s=%s" (cdr entry) (cdr rdn))
2232 (format "%s=%s" (car rdn) (cdr rdn))))
2233 (concat "(" (epg-decode-dn rdn) ")")))
2234 alist
2235 ", "))
2237 (provide 'epg)
2239 ;;; epg.el ends here