Drop some epg/epa backwards compat code for ancient Emacs versions
[emacs.git] / lisp / epg.el
blobe6b4af2b36e8feeb816484001ae44840597ec102
1 ;;; epg.el --- the EasyPG Library -*- lexical-binding: t -*-
2 ;; Copyright (C) 1999-2000, 2002-2018 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 <https://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 (cl-defstruct (epg-context
178 (:constructor nil)
179 (:constructor epg-context--make
180 (protocol &optional armor textmode include-certs
181 cipher-algorithm digest-algorithm
182 compress-algorithm
183 &aux
184 (program
185 (let ((configuration (epg-find-configuration protocol)))
186 (unless configuration
187 (signal 'epg-error
188 (list "no usable configuration" protocol)))
189 (alist-get 'program configuration)))))
190 (:copier nil)
191 (:predicate nil))
192 protocol
193 program
194 (home-directory epg-gpg-home-directory)
195 armor
196 textmode
197 include-certs
198 cipher-algorithm
199 digest-algorithm
200 compress-algorithm
201 (passphrase-callback (list #'epg-passphrase-callback-function))
202 progress-callback
203 edit-callback
204 signers
205 sig-notations
206 process
207 output-file
208 result
209 operation
210 pinentry-mode
211 (error-output "")
212 error-buffer)
214 ;; This is not an alias, just so we can mark it as autoloaded.
215 ;;;###autoload
216 (defun epg-make-context (&optional protocol armor textmode include-certs
217 cipher-algorithm digest-algorithm
218 compress-algorithm)
219 "Return a context object."
220 (epg-context--make (or protocol 'OpenPGP)
221 armor textmode include-certs
222 cipher-algorithm digest-algorithm
223 compress-algorithm))
225 (defun epg-context-set-armor (context armor)
226 "Specify if the output should be ASCII armored in CONTEXT."
227 (declare (obsolete setf "25.1"))
228 (setf (epg-context-armor context) armor))
230 (defun epg-context-set-textmode (context textmode)
231 "Specify if canonical text mode should be used in CONTEXT."
232 (declare (obsolete setf "25.1"))
233 (setf (epg-context-textmode context) textmode))
235 (defun epg-context-set-passphrase-callback (context
236 passphrase-callback)
237 "Set the function used to query passphrase.
239 PASSPHRASE-CALLBACK is either a function, or a cons-cell whose
240 car is a function and cdr is a callback data.
242 The function gets three arguments: the context, the key-id in
243 question, and the callback data (if any).
245 The callback may not be called if you use GnuPG 2.x, which relies
246 on the external program called `gpg-agent' for passphrase query.
247 If you really want to intercept passphrase query, consider
248 installing GnuPG 1.x _along with_ GnuPG 2.x, which does passphrase
249 query by itself and Emacs can intercept them."
250 ;; (declare (obsolete setf "25.1"))
251 (setf (epg-context-passphrase-callback context)
252 (if (functionp passphrase-callback)
253 (list passphrase-callback)
254 passphrase-callback)))
256 (defun epg-context-set-progress-callback (context
257 progress-callback)
258 "Set the function which handles progress update.
260 PROGRESS-CALLBACK is either a function, or a cons-cell whose
261 car is a function and cdr is a callback data.
263 The function gets six arguments: the context, the operation
264 description, the character to display a progress unit, the
265 current amount done, the total amount to be done, and the
266 callback data (if any)."
267 (setf (epg-context-progress-callback context)
268 (if (functionp progress-callback)
269 (list progress-callback)
270 progress-callback)))
272 (defun epg-context-set-signers (context signers)
273 "Set the list of key-id for signing."
274 (declare (obsolete setf "25.1"))
275 (setf (epg-context-signers context) signers))
277 (cl-defstruct (epg-signature
278 (:constructor nil)
279 (:constructor epg-make-signature
280 (status &optional key-id))
281 (:copier nil)
282 (:predicate nil))
283 status
284 key-id
285 validity
286 fingerprint
287 creation-time
288 expiration-time
289 pubkey-algorithm
290 digest-algorithm
291 class
292 version
293 notations)
295 (cl-defstruct (epg-new-signature
296 (:constructor nil)
297 (:constructor epg-make-new-signature
298 (type pubkey-algorithm digest-algorithm
299 class creation-time fingerprint))
300 (:copier nil)
301 (:predicate nil))
302 (type nil :read-only t)
303 (pubkey-algorithm nil :read-only t)
304 (digest-algorithm nil :read-only t)
305 (class nil :read-only t)
306 (creation-time nil :read-only t)
307 (fingerprint nil :read-only t))
309 (cl-defstruct (epg-key
310 (:constructor nil)
311 (:constructor epg-make-key (owner-trust))
312 (:copier nil)
313 (:predicate nil))
314 (owner-trust nil :read-only t)
315 sub-key-list user-id-list)
317 (cl-defstruct (epg-sub-key
318 (:constructor nil)
319 (:constructor epg-make-sub-key
320 (validity capability secret-p algorithm length id
321 creation-time expiration-time))
322 (:copier nil)
323 (:predicate nil))
324 validity capability secret-p algorithm length id
325 creation-time expiration-time fingerprint)
327 (cl-defstruct (epg-user-id
328 (:constructor nil)
329 (:constructor epg-make-user-id (validity string))
330 (:copier nil)
331 (:predicate nil))
332 validity string signature-list)
334 (cl-defstruct (epg-key-signature
335 (:constructor nil)
336 (:constructor epg-make-key-signature
337 (validity pubkey-algorithm key-id creation-time
338 expiration-time user-id class
339 exportable-p))
340 (:copier nil)
341 (:predicate nil))
342 validity pubkey-algorithm key-id creation-time
343 expiration-time user-id class
344 exportable-p)
346 (cl-defstruct (epg-sig-notation
347 (:constructor nil)
348 (:constructor epg-make-sig-notation
349 (name value &optional human-readable critical))
350 (:copier nil)
351 (:predicate nil))
352 name value human-readable critical)
354 (cl-defstruct (epg-import-status
355 (:constructor nil)
356 (:constructor epg-make-import-status
357 (fingerprint
358 &optional reason new user-id signature sub-key secret))
359 (:copier nil)
360 (:predicate nil))
361 fingerprint reason new user-id signature sub-key secret)
363 (cl-defstruct (epg-import-result
364 (:constructor nil)
365 (:constructor epg-make-import-result
366 (considered no-user-id imported imported-rsa
367 unchanged new-user-ids new-sub-keys
368 new-signatures new-revocations
369 secret-read secret-imported
370 secret-unchanged not-imported
371 imports))
372 (:copier nil)
373 (:predicate nil))
374 considered no-user-id imported imported-rsa
375 unchanged new-user-ids new-sub-keys
376 new-signatures new-revocations
377 secret-read secret-imported
378 secret-unchanged not-imported
379 imports)
381 (defun epg-context-result-for (context name)
382 "Return the result of CONTEXT associated with NAME."
383 (cdr (assq name (epg-context-result context))))
385 (defun epg-context-set-result-for (context name value)
386 "Set the result of CONTEXT associated with NAME to VALUE."
387 (let* ((result (epg-context-result context))
388 (entry (assq name result)))
389 (if entry
390 (setcdr entry value)
391 (setf (epg-context-result context) (cons (cons name value) result)))))
393 (defun epg-signature-to-string (signature)
394 "Convert SIGNATURE to a human readable string."
395 (let* ((user-id (cdr (assoc (epg-signature-key-id signature)
396 epg-user-id-alist)))
397 (pubkey-algorithm (epg-signature-pubkey-algorithm signature))
398 (key-id (epg-signature-key-id signature)))
399 (concat
400 (cond ((eq (epg-signature-status signature) 'good)
401 "Good signature from ")
402 ((eq (epg-signature-status signature) 'bad)
403 "Bad signature from ")
404 ((eq (epg-signature-status signature) 'expired)
405 "Expired signature from ")
406 ((eq (epg-signature-status signature) 'expired-key)
407 "Signature made by expired key ")
408 ((eq (epg-signature-status signature) 'revoked-key)
409 "Signature made by revoked key ")
410 ((eq (epg-signature-status signature) 'no-pubkey)
411 "No public key for "))
412 key-id
413 (if user-id
414 (concat " "
415 (if (stringp user-id)
416 user-id
417 (epg-decode-dn user-id)))
419 (if (epg-signature-validity signature)
420 (format " (trust %s)" (epg-signature-validity signature))
422 (if (epg-signature-creation-time signature)
423 (format-time-string " created at %Y-%m-%dT%T%z"
424 (epg-signature-creation-time signature))
426 (if pubkey-algorithm
427 (concat " using "
428 (or (cdr (assq pubkey-algorithm epg-pubkey-algorithm-alist))
429 (format "(unknown algorithm %d)" pubkey-algorithm)))
430 ""))))
432 (defun epg-verify-result-to-string (verify-result)
433 "Convert VERIFY-RESULT to a human readable string."
434 (mapconcat #'epg-signature-to-string verify-result "\n"))
436 (defun epg-new-signature-to-string (new-signature)
437 "Convert NEW-SIGNATURE to a human readable string."
438 (concat
439 (cond ((eq (epg-new-signature-type new-signature) 'detached)
440 "Detached signature ")
441 ((eq (epg-new-signature-type new-signature) 'clear)
442 "Cleartext signature ")
444 "Signature "))
445 (cdr (assq (epg-new-signature-pubkey-algorithm new-signature)
446 epg-pubkey-algorithm-alist))
448 (cdr (assq (epg-new-signature-digest-algorithm new-signature)
449 epg-digest-algorithm-alist))
451 (format "%02X " (epg-new-signature-class new-signature))
452 (epg-new-signature-fingerprint new-signature)))
454 (defun epg-import-result-to-string (import-result)
455 "Convert IMPORT-RESULT to a human readable string."
456 (concat (format "Total number processed: %d\n"
457 (epg-import-result-considered import-result))
458 (if (> (epg-import-result-not-imported import-result) 0)
459 (format " skipped new keys: %d\n"
460 (epg-import-result-not-imported import-result)))
461 (if (> (epg-import-result-no-user-id import-result) 0)
462 (format " w/o user IDs: %d\n"
463 (epg-import-result-no-user-id import-result)))
464 (if (> (epg-import-result-imported import-result) 0)
465 (concat (format " imported: %d"
466 (epg-import-result-imported import-result))
467 (if (> (epg-import-result-imported-rsa import-result) 0)
468 (format " (RSA: %d)"
469 (epg-import-result-imported-rsa
470 import-result)))
471 "\n"))
472 (if (> (epg-import-result-unchanged import-result) 0)
473 (format " unchanged: %d\n"
474 (epg-import-result-unchanged import-result)))
475 (if (> (epg-import-result-new-user-ids import-result) 0)
476 (format " new user IDs: %d\n"
477 (epg-import-result-new-user-ids import-result)))
478 (if (> (epg-import-result-new-sub-keys import-result) 0)
479 (format " new subkeys: %d\n"
480 (epg-import-result-new-sub-keys import-result)))
481 (if (> (epg-import-result-new-signatures import-result) 0)
482 (format " new signatures: %d\n"
483 (epg-import-result-new-signatures import-result)))
484 (if (> (epg-import-result-new-revocations import-result) 0)
485 (format " new key revocations: %d\n"
486 (epg-import-result-new-revocations import-result)))
487 (if (> (epg-import-result-secret-read import-result) 0)
488 (format " secret keys read: %d\n"
489 (epg-import-result-secret-read import-result)))
490 (if (> (epg-import-result-secret-imported import-result) 0)
491 (format " secret keys imported: %d\n"
492 (epg-import-result-secret-imported import-result)))
493 (if (> (epg-import-result-secret-unchanged import-result) 0)
494 (format " secret keys unchanged: %d\n"
495 (epg-import-result-secret-unchanged import-result)))))
497 (defun epg-error-to-string (error)
498 (cond
499 ((eq (car error) 'exit)
500 "Exit")
501 ((eq (car error) 'quit)
502 "Canceled")
503 ((eq (car error) 'no-data)
504 (let ((entry (assq (cdr error) epg-no-data-reason-alist)))
505 (if entry
506 (format "No data (%s)" (downcase (cdr entry)))
507 "No data")))
508 ((eq (car error) 'unexpected)
509 (let ((entry (assq (cdr error) epg-unexpected-reason-alist)))
510 (if entry
511 (format "Unexpected (%s)" (downcase (cdr entry)))
512 "Unexpected")))
513 ((eq (car error) 'bad-armor)
514 "Bad armor")
515 ((memq (car error) '(invalid-recipient invalid-signer))
516 (concat
517 (if (eq (car error) 'invalid-recipient)
518 "Unusable public key"
519 "Unusable secret key")
520 (let ((entry (assq 'requested (cdr error))))
521 (if entry
522 (format ": %s" (cdr entry))
523 ": <unknown>"))
524 (let ((entry (assq 'reason (cdr error))))
525 (if (and entry
526 (> (cdr entry) 0) ;no specific reason given
527 (setq entry (assq (cdr entry)
528 epg-invalid-recipients-reason-alist)))
529 (format " (%s)" (downcase (cdr entry)))
530 ""))))
531 ((eq (car error) 'no-pubkey)
532 (format "No public key: %s" (cdr error)))
533 ((eq (car error) 'no-seckey)
534 (format "No secret key: %s" (cdr error)))
535 ((eq (car error) 'no-recipients)
536 "No recipients")
537 ((eq (car error) 'no-signers)
538 "No signers")
539 ((eq (car error) 'delete-problem)
540 (let ((entry (assq (cdr error) epg-delete-problem-reason-alist)))
541 (if entry
542 (format "Delete problem (%s)" (downcase (cdr entry)))
543 "Delete problem")))
544 ((eq (car error) 'key-not-created)
545 "Key not created")))
547 (defun epg-errors-to-string (errors)
548 (mapconcat #'epg-error-to-string errors "; "))
550 (defun epg--start (context args)
551 "Start `epg-gpg-program' in a subprocess with given ARGS."
552 (if (and (epg-context-process context)
553 (eq (process-status (epg-context-process context)) 'run))
554 (error "%s is already running in this context"
555 (epg-context-program context)))
556 (let* ((agent-info (getenv "GPG_AGENT_INFO"))
557 (args (append (list "--no-tty"
558 "--status-fd" "1"
559 "--yes")
560 (if (and (not (eq (epg-context-protocol context) 'CMS))
561 (string-match ":" (or agent-info "")))
562 '("--use-agent"))
563 (if (and (not (eq (epg-context-protocol context) 'CMS))
564 (epg-context-progress-callback context))
565 '("--enable-progress-filter"))
566 (if (epg-context-home-directory context)
567 (list "--homedir"
568 (epg-context-home-directory context)))
569 (unless (eq (epg-context-protocol context) 'CMS)
570 '("--command-fd" "0"))
571 (if (epg-context-armor context) '("--armor"))
572 (if (epg-context-textmode context) '("--textmode"))
573 (if (epg-context-output-file context)
574 (list "--output" (epg-context-output-file context)))
575 (if (epg-context-pinentry-mode context)
576 (list "--pinentry-mode"
577 (symbol-name (epg-context-pinentry-mode
578 context))))
579 args))
580 (process-environment process-environment)
581 (buffer (generate-new-buffer " *epg*"))
582 error-process
583 process
584 terminal-name
585 agent-file
586 (agent-mtime '(0 0 0 0)))
587 ;; Set GPG_TTY and TERM for pinentry-curses. Note that we can't
588 ;; use `terminal-name' here to get the real pty name for the child
589 ;; process, though /dev/fd/0" is not portable.
590 (unless (memq system-type '(ms-dos windows-nt))
591 (with-temp-buffer
592 (condition-case nil
593 (when (= (call-process "tty" "/dev/fd/0" t) 0)
594 (delete-char -1)
595 (setq terminal-name (buffer-string)))
596 (file-error))))
597 (when terminal-name
598 (setq process-environment
599 (cons (concat "GPG_TTY=" terminal-name)
600 (cons "TERM=xterm" process-environment))))
601 (setq process-environment
602 (cons (format "INSIDE_EMACS=%s,epg" emacs-version)
603 process-environment))
604 ;; Record modified time of gpg-agent socket to restore the Emacs
605 ;; frame on text terminal in `epg-wait-for-completion'.
606 ;; See
607 ;; <https://lists.gnu.org/r/emacs-devel/2007-02/msg00755.html>
608 ;; for more details.
609 (when (and agent-info (string-match "\\(.*\\):[0-9]+:[0-9]+" agent-info))
610 (setq agent-file (match-string 1 agent-info)
611 agent-mtime (or (nth 5 (file-attributes agent-file)) '(0 0 0 0))))
612 (if epg-debug
613 (save-excursion
614 (unless epg-debug-buffer
615 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
616 (set-buffer epg-debug-buffer)
617 (goto-char (point-max))
618 (insert (if agent-info
619 (format "GPG_AGENT_INFO=%s\n" agent-info)
620 "GPG_AGENT_INFO is not set\n")
621 (format "%s %s\n"
622 (epg-context-program context)
623 (mapconcat #'identity args " ")))))
624 (with-current-buffer buffer
625 (if (fboundp 'set-buffer-multibyte)
626 (set-buffer-multibyte nil))
627 (make-local-variable 'epg-last-status)
628 (setq epg-last-status nil)
629 (make-local-variable 'epg-read-point)
630 (setq epg-read-point (point-min))
631 (make-local-variable 'epg-process-filter-running)
632 (setq epg-process-filter-running nil)
633 (make-local-variable 'epg-pending-status-list)
634 (setq epg-pending-status-list nil)
635 (make-local-variable 'epg-key-id)
636 (setq epg-key-id nil)
637 (make-local-variable 'epg-context)
638 (setq epg-context context)
639 (make-local-variable 'epg-agent-file)
640 (setq epg-agent-file agent-file)
641 (make-local-variable 'epg-agent-mtime)
642 (setq epg-agent-mtime agent-mtime))
643 (setq error-process
644 (make-pipe-process :name "epg-error"
645 :buffer (generate-new-buffer " *epg-error*")
646 ;; Suppress "XXX finished" line.
647 :sentinel #'ignore
648 :noquery t))
649 (setf (epg-context-error-buffer context) (process-buffer error-process))
650 (with-file-modes 448
651 (setq process (make-process :name "epg"
652 :buffer buffer
653 :command (cons (epg-context-program context)
654 args)
655 :connection-type 'pipe
656 :coding '(binary . binary)
657 :filter #'epg--process-filter
658 :stderr error-process
659 :noquery t)))
660 (setf (epg-context-process context) process)))
662 (defun epg--process-filter (process input)
663 (if epg-debug
664 (with-current-buffer
665 (or epg-debug-buffer
666 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
667 (goto-char (point-max))
668 (insert input)))
669 (if (buffer-live-p (process-buffer process))
670 (with-current-buffer (process-buffer process)
671 (save-excursion
672 (goto-char (point-max))
673 (insert input)
674 (unless epg-process-filter-running
675 (let ((epg-process-filter-running t))
676 (goto-char epg-read-point)
677 (beginning-of-line)
678 (while (looking-at ".*\n") ;the input line finished
679 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
680 (let ((status (match-string 1))
681 (string (match-string 2))
682 symbol)
683 (if (member status epg-pending-status-list)
684 (setq epg-pending-status-list nil))
685 ;; When editing a key, delegate all interaction
686 ;; to edit-callback.
687 (if (eq (epg-context-operation epg-context) 'edit-key)
688 (funcall (car (epg-context-edit-callback
689 epg-context))
690 epg-context
691 status
692 string
693 (cdr (epg-context-edit-callback
694 epg-context)))
695 ;; Otherwise call epg--status-STATUS function.
696 (setq symbol (intern-soft (concat "epg--status-"
697 status)))
698 (if (and symbol
699 (fboundp symbol))
700 (funcall symbol epg-context string)))
701 (setq epg-last-status (cons status string))))
702 (forward-line)
703 (setq epg-read-point (point)))))))))
705 (defun epg-read-output (context)
706 "Read the output file CONTEXT and return the content as a string."
707 (with-temp-buffer
708 (if (fboundp 'set-buffer-multibyte)
709 (set-buffer-multibyte nil))
710 (if (file-exists-p (epg-context-output-file context))
711 (let ((coding-system-for-read 'binary))
712 (insert-file-contents (epg-context-output-file context))
713 (buffer-string)))))
715 (defun epg-wait-for-status (context status-list)
716 "Wait until one of elements in STATUS-LIST arrives."
717 (with-current-buffer (process-buffer (epg-context-process context))
718 (setq epg-pending-status-list status-list)
719 (while (and (eq (process-status (epg-context-process context)) 'run)
720 epg-pending-status-list)
721 (accept-process-output (epg-context-process context) 1))
722 (if epg-pending-status-list
723 (epg-context-set-result-for
724 context 'error
725 (cons '(exit)
726 (epg-context-result-for context 'error))))))
728 (defun epg-wait-for-completion (context)
729 "Wait until the `epg-gpg-program' process completes."
730 (while (eq (process-status (epg-context-process context)) 'run)
731 (accept-process-output (epg-context-process context) 1))
732 ;; This line is needed to run the process-filter right now.
733 (sleep-for 0.1)
734 ;; Restore Emacs frame on text terminal, when pinentry-curses has terminated.
735 (if (with-current-buffer (process-buffer (epg-context-process context))
736 (and epg-agent-file
737 (time-less-p epg-agent-mtime
738 (or (nth 5 (file-attributes epg-agent-file)) 0))))
739 (redraw-frame))
740 (epg-context-set-result-for
741 context 'error
742 (nreverse (epg-context-result-for context 'error)))
743 (setf (epg-context-error-output context)
744 (with-current-buffer (epg-context-error-buffer context)
745 (buffer-string))))
747 (defun epg-reset (context)
748 "Reset the CONTEXT."
749 (if (and (epg-context-process context)
750 (buffer-live-p (process-buffer (epg-context-process context))))
751 (kill-buffer (process-buffer (epg-context-process context))))
752 (if (buffer-live-p (epg-context-error-buffer context))
753 (kill-buffer (epg-context-error-buffer context)))
754 (setf (epg-context-process context) nil)
755 (setf (epg-context-edit-callback context) nil))
757 (defun epg-delete-output-file (context)
758 "Delete the output file of CONTEXT."
759 (if (and (epg-context-output-file context)
760 (file-exists-p (epg-context-output-file context)))
761 (delete-file (epg-context-output-file context))))
763 (defun epg--status-USERID_HINT (_context string)
764 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
765 (let* ((key-id (match-string 1 string))
766 (user-id (match-string 2 string))
767 (entry (assoc key-id epg-user-id-alist)))
768 (condition-case nil
769 (setq user-id (decode-coding-string
770 (epg--decode-percent-escape user-id)
771 'utf-8))
772 (error))
773 (if entry
774 (setcdr entry user-id)
775 (setq epg-user-id-alist (cons (cons key-id user-id)
776 epg-user-id-alist))))))
778 (defun epg--status-NEED_PASSPHRASE (_context string)
779 (if (string-match "\\`\\([^ ]+\\)" string)
780 (setq epg-key-id (match-string 1 string))))
782 (defun epg--status-NEED_PASSPHRASE_SYM (_context _string)
783 (setq epg-key-id 'SYM))
785 (defun epg--status-NEED_PASSPHRASE_PIN (_context _string)
786 (setq epg-key-id 'PIN))
788 (defun epg--status-GET_HIDDEN (context string)
789 (when (and epg-key-id
790 (string-match "\\`passphrase\\." string))
791 (unless (epg-context-passphrase-callback context)
792 (error "passphrase-callback not set"))
793 (let (inhibit-quit
794 passphrase
795 passphrase-with-new-line
796 encoded-passphrase-with-new-line)
797 (unwind-protect
798 (condition-case nil
799 (progn
800 (setq passphrase
801 (funcall
802 (car (epg-context-passphrase-callback context))
803 context
804 epg-key-id
805 (cdr (epg-context-passphrase-callback context))))
806 (when passphrase
807 (setq passphrase-with-new-line (concat passphrase "\n"))
808 (clear-string passphrase)
809 (setq passphrase nil)
810 (if epg-passphrase-coding-system
811 (progn
812 (setq encoded-passphrase-with-new-line
813 (encode-coding-string
814 passphrase-with-new-line
815 (coding-system-change-eol-conversion
816 epg-passphrase-coding-system 'unix)))
817 (clear-string passphrase-with-new-line)
818 (setq passphrase-with-new-line nil))
819 (setq encoded-passphrase-with-new-line
820 passphrase-with-new-line
821 passphrase-with-new-line nil))
822 (process-send-string (epg-context-process context)
823 encoded-passphrase-with-new-line)))
824 (quit
825 (epg-context-set-result-for
826 context 'error
827 (cons '(quit)
828 (epg-context-result-for context 'error)))
829 (delete-process (epg-context-process context))))
830 (if passphrase
831 (clear-string passphrase))
832 (if passphrase-with-new-line
833 (clear-string passphrase-with-new-line))
834 (if encoded-passphrase-with-new-line
835 (clear-string encoded-passphrase-with-new-line))))))
837 (defun epg--prompt-GET_BOOL (_context string)
838 (let ((entry (assoc string epg-prompt-alist)))
839 (y-or-n-p (if entry (cdr entry) (concat string "? ")))))
841 (defun epg--prompt-GET_BOOL-untrusted_key.override (_context _string)
842 (y-or-n-p (if (and (equal (car epg-last-status) "USERID_HINT")
843 (string-match "\\`\\([^ ]+\\) \\(.*\\)"
844 (cdr epg-last-status)))
845 (let* ((key-id (match-string 1 (cdr epg-last-status)))
846 (user-id (match-string 2 (cdr epg-last-status)))
847 (entry (assoc key-id epg-user-id-alist)))
848 (if entry
849 (setq user-id (cdr entry)))
850 (format "Untrusted key %s %s. Use anyway? " key-id user-id))
851 "Use untrusted key anyway? ")))
853 (defun epg--status-GET_BOOL (context string)
854 (let (inhibit-quit)
855 (condition-case nil
856 (if (funcall (or (intern-soft (concat "epg--prompt-GET_BOOL-" string))
857 #'epg--prompt-GET_BOOL)
858 context string)
859 (process-send-string (epg-context-process context) "y\n")
860 (process-send-string (epg-context-process context) "n\n"))
861 (quit
862 (epg-context-set-result-for
863 context 'error
864 (cons '(quit)
865 (epg-context-result-for context 'error)))
866 (delete-process (epg-context-process context))))))
868 (defun epg--status-GET_LINE (context string)
869 (let ((entry (assoc string epg-prompt-alist))
870 inhibit-quit)
871 (condition-case nil
872 (process-send-string (epg-context-process context)
873 (concat (read-string
874 (if entry
875 (cdr entry)
876 (concat string ": ")))
877 "\n"))
878 (quit
879 (epg-context-set-result-for
880 context 'error
881 (cons '(quit)
882 (epg-context-result-for context 'error)))
883 (delete-process (epg-context-process context))))))
885 (defun epg--status-*SIG (context status string)
886 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
887 (let* ((key-id (match-string 1 string))
888 (user-id (match-string 2 string))
889 (entry (assoc key-id epg-user-id-alist)))
890 (epg-context-set-result-for
891 context
892 'verify
893 (cons (epg-make-signature status key-id)
894 (epg-context-result-for context 'verify)))
895 (condition-case nil
896 (if (eq (epg-context-protocol context) 'CMS)
897 (setq user-id (epg-dn-from-string user-id))
898 (setq user-id (decode-coding-string
899 (epg--decode-percent-escape user-id)
900 'utf-8)))
901 (error))
902 (if entry
903 (setcdr entry user-id)
904 (setq epg-user-id-alist
905 (cons (cons key-id user-id) epg-user-id-alist))))
906 (epg-context-set-result-for
907 context
908 'verify
909 (cons (epg-make-signature status)
910 (epg-context-result-for context 'verify)))))
912 (defun epg--status-GOODSIG (context string)
913 (epg--status-*SIG context 'good string))
915 (defun epg--status-EXPSIG (context string)
916 (epg--status-*SIG context 'expired string))
918 (defun epg--status-EXPKEYSIG (context string)
919 (epg--status-*SIG context 'expired-key string))
921 (defun epg--status-REVKEYSIG (context string)
922 (epg--status-*SIG context 'revoked-key string))
924 (defun epg--status-BADSIG (context string)
925 (epg--status-*SIG context 'bad string))
927 (defun epg--status-NO_PUBKEY (context string)
928 (if (eq (epg-context-operation context) 'verify)
929 (let ((signature (car (epg-context-result-for context 'verify))))
930 (if (and signature
931 (eq (epg-signature-status signature) 'error)
932 (equal (epg-signature-key-id signature) string))
933 (setf (epg-signature-status signature) 'no-pubkey)))
934 (epg-context-set-result-for
935 context 'error
936 (cons (cons 'no-pubkey string)
937 (epg-context-result-for context 'error)))))
939 (defun epg--status-NO_SECKEY (context string)
940 (epg-context-set-result-for
941 context 'error
942 (cons (cons 'no-seckey string)
943 (epg-context-result-for context 'error))))
945 (defun epg--time-from-seconds (seconds)
946 (let ((number-seconds (string-to-number (concat seconds ".0"))))
947 (cons (floor (/ number-seconds 65536))
948 (floor (mod number-seconds 65536)))))
950 (defun epg--status-ERRSIG (context string)
951 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
952 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
953 string)
954 (let ((signature (epg-make-signature 'error)))
955 (epg-context-set-result-for
956 context
957 'verify
958 (cons signature
959 (epg-context-result-for context 'verify)))
960 (setf (epg-signature-key-id signature)
961 (match-string 1 string))
962 (setf (epg-signature-pubkey-algorithm signature)
963 (string-to-number (match-string 2 string)))
964 (setf (epg-signature-digest-algorithm signature)
965 (string-to-number (match-string 3 string)))
966 (setf (epg-signature-class signature)
967 (string-to-number (match-string 4 string) 16))
968 (setf (epg-signature-creation-time signature)
969 (epg--time-from-seconds (match-string 5 string))))))
971 (defun epg--status-VALIDSIG (context string)
972 (let ((signature (car (epg-context-result-for context 'verify))))
973 (when (and signature
974 (eq (epg-signature-status signature) 'good)
975 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
976 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
977 \\(.*\\)"
978 string))
979 (setf (epg-signature-fingerprint signature)
980 (match-string 1 string))
981 (setf (epg-signature-creation-time signature)
982 (epg--time-from-seconds (match-string 2 string)))
983 (unless (equal (match-string 3 string) "0")
984 (setf (epg-signature-expiration-time signature)
985 (epg--time-from-seconds (match-string 3 string))))
986 (setf (epg-signature-version signature)
987 (string-to-number (match-string 4 string)))
988 (setf (epg-signature-pubkey-algorithm signature)
989 (string-to-number (match-string 5 string)))
990 (setf (epg-signature-digest-algorithm signature)
991 (string-to-number (match-string 6 string)))
992 (setf (epg-signature-class signature)
993 (string-to-number (match-string 7 string) 16)))))
995 (defun epg--status-TRUST_UNDEFINED (context _string)
996 (let ((signature (car (epg-context-result-for context 'verify))))
997 (if (and signature
998 (eq (epg-signature-status signature) 'good))
999 (setf (epg-signature-validity signature) 'undefined))))
1001 (defun epg--status-TRUST_NEVER (context _string)
1002 (let ((signature (car (epg-context-result-for context 'verify))))
1003 (if (and signature
1004 (eq (epg-signature-status signature) 'good))
1005 (setf (epg-signature-validity signature) 'never))))
1007 (defun epg--status-TRUST_MARGINAL (context _string)
1008 (let ((signature (car (epg-context-result-for context 'verify))))
1009 (if (and signature
1010 (eq (epg-signature-status signature) 'good))
1011 (setf (epg-signature-validity signature) 'marginal))))
1013 (defun epg--status-TRUST_FULLY (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) 'full))))
1019 (defun epg--status-TRUST_ULTIMATE (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) 'ultimate))))
1025 (defun epg--status-NOTATION_NAME (context string)
1026 (let ((signature (car (epg-context-result-for context 'verify))))
1027 (if signature
1028 (push (epg-make-sig-notation string nil t nil)
1029 (epg-signature-notations signature)))))
1031 (defun epg--status-NOTATION_DATA (context string)
1032 (let ((signature (car (epg-context-result-for context 'verify)))
1033 notation)
1034 (if (and signature
1035 (setq notation (car (epg-signature-notations signature))))
1036 (setf (epg-sig-notation-value notation) string))))
1038 (defun epg--status-POLICY_URL (context string)
1039 (let ((signature (car (epg-context-result-for context 'verify))))
1040 (if signature
1041 (push (epg-make-sig-notation nil string t nil)
1042 (epg-signature-notations signature)))))
1044 (defun epg--status-PROGRESS (context string)
1045 (if (and (epg-context-progress-callback context)
1046 (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1047 string))
1048 (funcall (car (epg-context-progress-callback context))
1049 context
1050 (match-string 1 string)
1051 (match-string 2 string)
1052 (string-to-number (match-string 3 string))
1053 (string-to-number (match-string 4 string))
1054 (cdr (epg-context-progress-callback context)))))
1056 (defun epg--status-ENC_TO (context string)
1057 (if (string-match "\\`\\([0-9A-Za-z]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
1058 (epg-context-set-result-for
1059 context 'encrypted-to
1060 (cons (list (match-string 1 string)
1061 (string-to-number (match-string 2 string))
1062 (string-to-number (match-string 3 string)))
1063 (epg-context-result-for context 'encrypted-to)))))
1065 (defun epg--status-DECRYPTION_FAILED (context _string)
1066 (epg-context-set-result-for context 'decryption-failed t))
1068 (defun epg--status-DECRYPTION_OKAY (context _string)
1069 (epg-context-set-result-for context 'decryption-okay t))
1071 (defun epg--status-NODATA (context string)
1072 (epg-context-set-result-for
1073 context 'error
1074 (cons (cons 'no-data (string-to-number string))
1075 (epg-context-result-for context 'error))))
1077 (defun epg--status-UNEXPECTED (context string)
1078 (epg-context-set-result-for
1079 context 'error
1080 (cons (cons 'unexpected (string-to-number string))
1081 (epg-context-result-for context 'error))))
1083 (defun epg--status-KEYEXPIRED (context string)
1084 (epg-context-set-result-for
1085 context 'key
1086 (cons (list 'key-expired (cons 'expiration-time
1087 (epg--time-from-seconds string)))
1088 (epg-context-result-for context 'key))))
1090 (defun epg--status-KEYREVOKED (context _string)
1091 (epg-context-set-result-for
1092 context 'key
1093 (cons '(key-revoked)
1094 (epg-context-result-for context 'key))))
1096 (defun epg--status-BADARMOR (context _string)
1097 (epg-context-set-result-for
1098 context 'error
1099 (cons '(bad-armor)
1100 (epg-context-result-for context 'error))))
1102 (defun epg--status-INV_RECP (context string)
1103 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1104 (epg-context-set-result-for
1105 context 'error
1106 (cons (list 'invalid-recipient
1107 (cons 'reason
1108 (string-to-number (match-string 1 string)))
1109 (cons 'requested
1110 (match-string 2 string)))
1111 (epg-context-result-for context 'error)))))
1113 (defun epg--status-INV_SGNR (context string)
1114 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1115 (epg-context-set-result-for
1116 context 'error
1117 (cons (list 'invalid-signer
1118 (cons 'reason
1119 (string-to-number (match-string 1 string)))
1120 (cons 'requested
1121 (match-string 2 string)))
1122 (epg-context-result-for context 'error)))))
1124 (defun epg--status-NO_RECP (context _string)
1125 (epg-context-set-result-for
1126 context 'error
1127 (cons '(no-recipients)
1128 (epg-context-result-for context 'error))))
1130 (defun epg--status-NO_SGNR (context _string)
1131 (epg-context-set-result-for
1132 context 'error
1133 (cons '(no-signers)
1134 (epg-context-result-for context 'error))))
1136 (defun epg--status-DELETE_PROBLEM (context string)
1137 (if (string-match "\\`\\([0-9]+\\)" string)
1138 (epg-context-set-result-for
1139 context 'error
1140 (cons (cons 'delete-problem
1141 (string-to-number (match-string 1 string)))
1142 (epg-context-result-for context 'error)))))
1144 (defun epg--status-SIG_CREATED (context string)
1145 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1146 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1147 (epg-context-set-result-for
1148 context 'sign
1149 (cons (epg-make-new-signature
1150 (cdr (assq (aref (match-string 1 string) 0)
1151 epg-new-signature-type-alist))
1152 (string-to-number (match-string 2 string))
1153 (string-to-number (match-string 3 string))
1154 (string-to-number (match-string 4 string) 16)
1155 (epg--time-from-seconds (match-string 5 string))
1156 (substring string (match-end 0)))
1157 (epg-context-result-for context 'sign)))))
1159 (defun epg--status-KEY_CREATED (context string)
1160 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1161 (epg-context-set-result-for
1162 context 'generate-key
1163 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1164 (cons 'fingerprint (match-string 2 string)))
1165 (epg-context-result-for context 'generate-key)))))
1167 (defun epg--status-KEY_NOT_CREATED (context _string)
1168 (epg-context-set-result-for
1169 context 'error
1170 (cons '(key-not-created)
1171 (epg-context-result-for context 'error))))
1173 (defun epg--status-IMPORTED (_context string)
1174 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1175 (let* ((key-id (match-string 1 string))
1176 (user-id (match-string 2 string))
1177 (entry (assoc key-id epg-user-id-alist)))
1178 (condition-case nil
1179 (setq user-id (decode-coding-string
1180 (epg--decode-percent-escape user-id)
1181 'utf-8))
1182 (error))
1183 (if entry
1184 (setcdr entry user-id)
1185 (setq epg-user-id-alist (cons (cons key-id user-id)
1186 epg-user-id-alist))))))
1188 (defun epg--status-IMPORT_OK (context string)
1189 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1190 (let ((reason (string-to-number (match-string 1 string))))
1191 (epg-context-set-result-for
1192 context 'import-status
1193 (cons (epg-make-import-status (if (match-beginning 2)
1194 (match-string 3 string))
1196 (/= (logand reason 1) 0)
1197 (/= (logand reason 2) 0)
1198 (/= (logand reason 4) 0)
1199 (/= (logand reason 8) 0)
1200 (/= (logand reason 16) 0))
1201 (epg-context-result-for context 'import-status))))))
1203 (defun epg--status-IMPORT_PROBLEM (context string)
1204 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1205 (epg-context-set-result-for
1206 context 'import-status
1207 (cons (epg-make-import-status
1208 (if (match-beginning 2)
1209 (match-string 3 string))
1210 (string-to-number (match-string 1 string)))
1211 (epg-context-result-for context 'import-status)))))
1213 (defun epg--status-IMPORT_RES (context string)
1214 (when (string-match "\\`\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1215 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1216 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
1217 (epg-context-set-result-for
1218 context 'import
1219 (epg-make-import-result (string-to-number (match-string 1 string))
1220 (string-to-number (match-string 2 string))
1221 (string-to-number (match-string 3 string))
1222 (string-to-number (match-string 4 string))
1223 (string-to-number (match-string 5 string))
1224 (string-to-number (match-string 6 string))
1225 (string-to-number (match-string 7 string))
1226 (string-to-number (match-string 8 string))
1227 (string-to-number (match-string 9 string))
1228 (string-to-number (match-string 10 string))
1229 (string-to-number (match-string 11 string))
1230 (string-to-number (match-string 12 string))
1231 (string-to-number (match-string 13 string))
1232 (epg-context-result-for context 'import-status)))
1233 (epg-context-set-result-for context 'import-status nil)))
1235 (defun epg-passphrase-callback-function (context key-id _handback)
1236 (declare (obsolete epa-passphrase-callback-function "23.1"))
1237 (if (eq key-id 'SYM)
1238 (read-passwd "Passphrase for symmetric encryption: "
1239 (eq (epg-context-operation context) 'encrypt))
1240 (read-passwd
1241 (if (eq key-id 'PIN)
1242 "Passphrase for PIN: "
1243 (let ((entry (assoc key-id epg-user-id-alist)))
1244 (if entry
1245 (format "Passphrase for %s %s: " key-id (cdr entry))
1246 (format "Passphrase for %s: " key-id)))))))
1248 (defun epg--list-keys-1 (context name mode)
1249 (let ((args (append (if (epg-context-home-directory context)
1250 (list "--homedir"
1251 (epg-context-home-directory context)))
1252 '("--with-colons" "--no-greeting" "--batch"
1253 "--with-fingerprint" "--with-fingerprint")
1254 (unless (eq (epg-context-protocol context) 'CMS)
1255 '("--fixed-list-mode"))))
1256 (list-keys-option (if (memq mode '(t secret))
1257 "--list-secret-keys"
1258 (if (memq mode '(nil public))
1259 "--list-keys"
1260 "--list-sigs")))
1261 (coding-system-for-read 'binary)
1262 keys string field index)
1263 (if name
1264 (progn
1265 (unless (listp name)
1266 (setq name (list name)))
1267 (while name
1268 (setq args (append args (list list-keys-option (car name)))
1269 name (cdr name))))
1270 (setq args (append args (list list-keys-option))))
1271 (with-temp-buffer
1272 (apply #'call-process
1273 (epg-context-program context)
1274 nil (list t nil) nil args)
1275 (goto-char (point-min))
1276 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1277 (setq keys (cons (make-vector 15 nil) keys)
1278 string (match-string 0)
1279 index 0
1280 field 0)
1281 (while (and (< field (length (car keys)))
1282 (eq index
1283 (string-match "\\([^:]+\\)?:" string index)))
1284 (setq index (match-end 0))
1285 (aset (car keys) field (match-string 1 string))
1286 (setq field (1+ field))))
1287 (nreverse keys))))
1289 (defun epg--make-sub-key-1 (line)
1290 (epg-make-sub-key
1291 (if (aref line 1)
1292 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1293 (delq nil
1294 (mapcar (lambda (char) (cdr (assq char epg-key-capability-alist)))
1295 (aref line 11)))
1296 (member (aref line 0) '("sec" "ssb"))
1297 (string-to-number (aref line 3))
1298 (string-to-number (aref line 2))
1299 (aref line 4)
1300 (epg--time-from-seconds (aref line 5))
1301 (if (aref line 6)
1302 (epg--time-from-seconds (aref line 6)))))
1304 (defun epg-list-keys (context &optional name mode)
1305 "Return a list of epg-key objects matched with NAME.
1306 If MODE is nil or `public', only public keyring should be searched.
1307 If MODE is t or `secret', only secret keyring should be searched.
1308 Otherwise, only public keyring should be searched and the key
1309 signatures should be included.
1310 NAME is either a string or a list of strings."
1311 (let ((lines (epg--list-keys-1 context name mode))
1312 keys cert pointer pointer-1 index string)
1313 (while lines
1314 (cond
1315 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1316 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1317 keys (cons (epg-make-key
1318 (if (aref (car lines) 8)
1319 (cdr (assq (string-to-char (aref (car lines) 8))
1320 epg-key-validity-alist))))
1321 keys))
1322 (push (epg--make-sub-key-1 (car lines))
1323 (epg-key-sub-key-list (car keys))))
1324 ((member (aref (car lines) 0) '("sub" "ssb"))
1325 (push (epg--make-sub-key-1 (car lines))
1326 (epg-key-sub-key-list (car keys))))
1327 ((equal (aref (car lines) 0) "uid")
1328 ;; Decode the UID name as a backslash escaped UTF-8 string,
1329 ;; generated by GnuPG/GpgSM.
1330 (setq string (copy-sequence (aref (car lines) 9))
1331 index 0)
1332 (while (string-match "\"" string index)
1333 (setq string (replace-match "\\\"" t t string)
1334 index (1+ (match-end 0))))
1335 (condition-case nil
1336 (setq string (decode-coding-string
1337 (car (read-from-string (concat "\"" string "\"")))
1338 'utf-8))
1339 (error
1340 (setq string (aref (car lines) 9))))
1341 (push (epg-make-user-id
1342 (if (aref (car lines) 1)
1343 (cdr (assq (string-to-char (aref (car lines) 1))
1344 epg-key-validity-alist)))
1345 (if cert
1346 (condition-case nil
1347 (epg-dn-from-string string)
1348 (error string))
1349 string))
1350 (epg-key-user-id-list (car keys))))
1351 ((equal (aref (car lines) 0) "fpr")
1352 (setf (epg-sub-key-fingerprint (car (epg-key-sub-key-list (car keys))))
1353 (aref (car lines) 9)))
1354 ((equal (aref (car lines) 0) "sig")
1355 (push
1356 (epg-make-key-signature
1357 (if (aref (car lines) 1)
1358 (cdr (assq (string-to-char (aref (car lines) 1))
1359 epg-key-validity-alist)))
1360 (string-to-number (aref (car lines) 3))
1361 (aref (car lines) 4)
1362 (epg--time-from-seconds (aref (car lines) 5))
1363 (epg--time-from-seconds (aref (car lines) 6))
1364 (aref (car lines) 9)
1365 (string-to-number (aref (car lines) 10) 16)
1366 (eq (aref (aref (car lines) 10) 2) ?x))
1367 (epg-user-id-signature-list
1368 (car (epg-key-user-id-list (car keys)))))))
1369 (setq lines (cdr lines)))
1370 (setq keys (nreverse keys)
1371 pointer keys)
1372 (while pointer
1373 (cl-callf nreverse (epg-key-sub-key-list (car pointer)))
1374 (setq pointer-1 (cl-callf nreverse (epg-key-user-id-list (car pointer))))
1375 (while pointer-1
1376 (cl-callf nreverse (epg-user-id-signature-list (car pointer-1)))
1377 (setq pointer-1 (cdr pointer-1)))
1378 (setq pointer (cdr pointer)))
1379 keys))
1381 (defun epg--args-from-sig-notations (notations)
1382 (apply #'nconc
1383 (mapcar
1384 (lambda (notation)
1385 (if (and (epg-sig-notation-name notation)
1386 (not (epg-sig-notation-human-readable notation)))
1387 (error "Unreadable"))
1388 (if (epg-sig-notation-name notation)
1389 (list "--sig-notation"
1390 (if (epg-sig-notation-critical notation)
1391 (concat "!" (epg-sig-notation-name notation)
1392 "=" (epg-sig-notation-value notation))
1393 (concat (epg-sig-notation-name notation)
1394 "=" (epg-sig-notation-value notation))))
1395 (list "--sig-policy-url"
1396 (if (epg-sig-notation-critical notation)
1397 (concat "!" (epg-sig-notation-value notation))
1398 (epg-sig-notation-value notation)))))
1399 notations)))
1401 (defun epg-cancel (context)
1402 (if (buffer-live-p (process-buffer (epg-context-process context)))
1403 (with-current-buffer (process-buffer (epg-context-process context))
1404 (epg-context-set-result-for
1405 epg-context 'error
1406 (cons '(quit)
1407 (epg-context-result-for epg-context 'error)))))
1408 (if (eq (process-status (epg-context-process context)) 'run)
1409 (delete-process (epg-context-process context))))
1411 (defun epg-start-decrypt (context cipher)
1412 "Initiate a decrypt operation on CIPHER.
1413 CIPHER must be a file data object.
1415 If you use this function, you will need to wait for the completion of
1416 `epg-gpg-program' by using `epg-wait-for-completion' and call
1417 `epg-reset' to clear a temporary output file.
1418 If you are unsure, use synchronous version of this function
1419 `epg-decrypt-file' or `epg-decrypt-string' instead."
1420 (unless (epg-data-file cipher)
1421 (error "Not a file"))
1422 (setf (epg-context-operation context) 'decrypt)
1423 (setf (epg-context-result context) nil)
1424 (epg--start context (list "--decrypt" "--" (epg-data-file cipher)))
1425 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1426 (unless (eq (epg-context-protocol context) 'CMS)
1427 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1429 (defun epg--check-error-for-decrypt (context)
1430 (let ((errors (epg-context-result-for context 'error)))
1431 (if (epg-context-result-for context 'decryption-failed)
1432 (signal 'epg-error
1433 (list "Decryption failed" (epg-errors-to-string errors))))
1434 (unless (epg-context-result-for context 'decryption-okay)
1435 (signal 'epg-error
1436 (list "Can't decrypt" (epg-errors-to-string errors))))))
1438 (defun epg-decrypt-file (context cipher plain)
1439 "Decrypt a file CIPHER and store the result to a file PLAIN.
1440 If PLAIN is nil, it returns the result as a string."
1441 (unwind-protect
1442 (progn
1443 (setf (epg-context-output-file context)
1444 (or plain (make-temp-file "epg-output")))
1445 (epg-start-decrypt context (epg-make-data-from-file cipher))
1446 (epg-wait-for-completion context)
1447 (epg--check-error-for-decrypt context)
1448 (unless plain
1449 (epg-read-output context)))
1450 (unless plain
1451 (epg-delete-output-file context))
1452 (epg-reset context)))
1454 (defun epg-decrypt-string (context cipher)
1455 "Decrypt a string CIPHER and return the plain text."
1456 (let ((input-file (make-temp-file "epg-input"))
1457 (coding-system-for-write 'binary))
1458 (unwind-protect
1459 (progn
1460 (write-region cipher nil input-file nil 'quiet)
1461 (setf (epg-context-output-file context)
1462 (make-temp-file "epg-output"))
1463 (epg-start-decrypt context (epg-make-data-from-file input-file))
1464 (epg-wait-for-completion context)
1465 (epg--check-error-for-decrypt context)
1466 (epg-read-output context))
1467 (epg-delete-output-file context)
1468 (if (file-exists-p input-file)
1469 (delete-file input-file))
1470 (epg-reset context))))
1472 (defun epg-start-verify (context signature &optional signed-text)
1473 "Initiate a verify operation on SIGNATURE.
1474 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1476 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1477 For a normal or a cleartext signature, SIGNED-TEXT should be nil.
1479 If you use this function, you will need to wait for the completion of
1480 `epg-gpg-program' by using `epg-wait-for-completion' and call
1481 `epg-reset' to clear a temporary output file.
1482 If you are unsure, use synchronous version of this function
1483 `epg-verify-file' or `epg-verify-string' instead."
1484 (setf (epg-context-operation context) 'verify)
1485 (setf (epg-context-result context) nil)
1486 (if signed-text
1487 ;; Detached signature.
1488 (if (epg-data-file signed-text)
1489 (epg--start context (list "--verify" "--" (epg-data-file signature)
1490 (epg-data-file signed-text)))
1491 (epg--start context (list "--verify" "--" (epg-data-file signature)
1492 "-"))
1493 (if (eq (process-status (epg-context-process context)) 'run)
1494 (process-send-string (epg-context-process context)
1495 (epg-data-string signed-text)))
1496 (if (eq (process-status (epg-context-process context)) 'run)
1497 (process-send-eof (epg-context-process context))))
1498 ;; Normal (or cleartext) signature.
1499 (if (epg-data-file signature)
1500 (epg--start context (if (eq (epg-context-protocol context) 'CMS)
1501 (list "--verify" "--" (epg-data-file signature))
1502 (list "--" (epg-data-file signature))))
1503 (epg--start context (if (eq (epg-context-protocol context) 'CMS)
1504 '("--verify" "-")
1505 '("-")))
1506 (if (eq (process-status (epg-context-process context)) 'run)
1507 (process-send-string (epg-context-process context)
1508 (epg-data-string signature)))
1509 (if (eq (process-status (epg-context-process context)) 'run)
1510 (process-send-eof (epg-context-process context))))))
1512 (defun epg-verify-file (context signature &optional signed-text plain)
1513 "Verify a file SIGNATURE.
1514 SIGNED-TEXT and PLAIN are also a file if they are specified.
1516 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1517 string. For a normal or a cleartext signature, SIGNED-TEXT should be
1518 nil. In the latter case, if PLAIN is specified, the plaintext is
1519 stored into the file after successful verification.
1521 Note that this function does not return verification result as t
1522 or nil, nor signal error on failure. That's a design decision to
1523 handle the case where SIGNATURE has multiple signature.
1525 To check the verification results, use `epg-context-result-for' as follows:
1527 \(epg-context-result-for context \\='verify)
1529 which will return a list of `epg-signature' object."
1530 (unwind-protect
1531 (progn
1532 (setf (epg-context-output-file context)
1533 (or plain (make-temp-file "epg-output")))
1534 (if signed-text
1535 (epg-start-verify context
1536 (epg-make-data-from-file signature)
1537 (epg-make-data-from-file signed-text))
1538 (epg-start-verify context
1539 (epg-make-data-from-file signature)))
1540 (epg-wait-for-completion context)
1541 (unless plain
1542 (epg-read-output context)))
1543 (unless plain
1544 (epg-delete-output-file context))
1545 (epg-reset context)))
1547 (defun epg-verify-string (context signature &optional signed-text)
1548 "Verify a string SIGNATURE.
1549 SIGNED-TEXT is a string if it is specified.
1551 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1552 string. For a normal or a cleartext signature, SIGNED-TEXT should be
1553 nil. In the latter case, this function returns the plaintext after
1554 successful verification.
1556 Note that this function does not return verification result as t
1557 or nil, nor signal error on failure. That's a design decision to
1558 handle the case where SIGNATURE has multiple signature.
1560 To check the verification results, use `epg-context-result-for' as follows:
1562 \(epg-context-result-for context \\='verify)
1564 which will return a list of `epg-signature' object."
1565 (let ((coding-system-for-write 'binary)
1566 input-file)
1567 (unwind-protect
1568 (progn
1569 (setf (epg-context-output-file context)
1570 (make-temp-file "epg-output"))
1571 (if signed-text
1572 (progn
1573 (setq input-file (make-temp-file "epg-signature"))
1574 (write-region signature nil input-file nil 'quiet)
1575 (epg-start-verify context
1576 (epg-make-data-from-file input-file)
1577 (epg-make-data-from-string signed-text)))
1578 (epg-start-verify context (epg-make-data-from-string signature)))
1579 (epg-wait-for-completion context)
1580 (epg-read-output context))
1581 (epg-delete-output-file context)
1582 (if (and input-file
1583 (file-exists-p input-file))
1584 (delete-file input-file))
1585 (epg-reset context))))
1587 (defun epg-start-sign (context plain &optional mode)
1588 "Initiate a sign operation on PLAIN.
1589 PLAIN is a data object.
1591 If optional 3rd argument MODE is t or `detached', it makes a detached signature.
1592 If it is nil or `normal', it makes a normal signature.
1593 Otherwise, it makes a cleartext signature.
1595 If you use this function, you will need to wait for the completion of
1596 `epg-gpg-program' by using `epg-wait-for-completion' and call
1597 `epg-reset' to clear a temporary output file.
1598 If you are unsure, use synchronous version of this function
1599 `epg-sign-file' or `epg-sign-string' instead."
1600 (setf (epg-context-operation context) 'sign)
1601 (setf (epg-context-result context) nil)
1602 (unless (memq mode '(t detached nil normal)) ;i.e. cleartext
1603 (setf (epg-context-armor context) nil)
1604 (setf (epg-context-textmode context) nil))
1605 (epg--start context
1606 (append (list (if (memq mode '(t detached))
1607 "--detach-sign"
1608 (if (memq mode '(nil normal))
1609 "--sign"
1610 "--clearsign")))
1611 (apply #'nconc
1612 (mapcar
1613 (lambda (signer)
1614 (list "-u"
1615 (epg-sub-key-id
1616 (car (epg-key-sub-key-list signer)))))
1617 (epg-context-signers context)))
1618 (epg--args-from-sig-notations
1619 (epg-context-sig-notations context))
1620 (if (epg-data-file plain)
1621 (list "--" (epg-data-file plain)))))
1622 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1623 (unless (eq (epg-context-protocol context) 'CMS)
1624 (epg-wait-for-status context '("BEGIN_SIGNING")))
1625 (when (epg-data-string plain)
1626 (if (eq (process-status (epg-context-process context)) 'run)
1627 (process-send-string (epg-context-process context)
1628 (epg-data-string plain)))
1629 (if (eq (process-status (epg-context-process context)) 'run)
1630 (process-send-eof (epg-context-process context)))))
1632 (defun epg-sign-file (context plain signature &optional mode)
1633 "Sign a file PLAIN and store the result to a file SIGNATURE.
1634 If SIGNATURE is nil, it returns the result as a string.
1635 If optional 3rd argument MODE is t or `detached', it makes a detached signature.
1636 If it is nil or `normal', it makes a normal signature.
1637 Otherwise, it makes a cleartext signature."
1638 (unwind-protect
1639 (progn
1640 (setf (epg-context-output-file context)
1641 (or signature (make-temp-file "epg-output")))
1642 (epg-start-sign context (epg-make-data-from-file plain) mode)
1643 (epg-wait-for-completion context)
1644 (unless (epg-context-result-for context 'sign)
1645 (let ((errors (epg-context-result-for context 'error)))
1646 (signal 'epg-error
1647 (list "Sign failed" (epg-errors-to-string errors)))))
1648 (unless signature
1649 (epg-read-output context)))
1650 (unless signature
1651 (epg-delete-output-file context))
1652 (epg-reset context)))
1654 (defun epg-sign-string (context plain &optional mode)
1655 "Sign a string PLAIN and return the output as string.
1656 If optional 3rd argument MODE is t or `detached', it makes a detached signature.
1657 If it is nil or `normal', it makes a normal signature.
1658 Otherwise, it makes a cleartext signature."
1659 (let ((input-file
1660 (unless (eq (epg-context-protocol context) 'CMS)
1661 (make-temp-file "epg-input")))
1662 (coding-system-for-write 'binary))
1663 (unwind-protect
1664 (progn
1665 (setf (epg-context-output-file context)
1666 (make-temp-file "epg-output"))
1667 (if input-file
1668 (write-region plain nil input-file nil 'quiet))
1669 (epg-start-sign context
1670 (if input-file
1671 (epg-make-data-from-file input-file)
1672 (epg-make-data-from-string plain))
1673 mode)
1674 (epg-wait-for-completion context)
1675 (unless (epg-context-result-for context 'sign)
1676 (if (epg-context-result-for context 'error)
1677 (let ((errors (epg-context-result-for context 'error)))
1678 (signal 'epg-error
1679 (list "Sign failed" (epg-errors-to-string errors))))))
1680 (epg-read-output context))
1681 (epg-delete-output-file context)
1682 (if input-file
1683 (delete-file input-file))
1684 (epg-reset context))))
1686 (defun epg-start-encrypt (context plain recipients
1687 &optional sign always-trust)
1688 "Initiate an encrypt operation on PLAIN.
1689 PLAIN is a data object.
1690 If RECIPIENTS is nil, it performs symmetric encryption.
1692 If you use this function, you will need to wait for the completion of
1693 `epg-gpg-program' by using `epg-wait-for-completion' and call
1694 `epg-reset' to clear a temporary output file.
1695 If you are unsure, use synchronous version of this function
1696 `epg-encrypt-file' or `epg-encrypt-string' instead."
1697 (setf (epg-context-operation context) 'encrypt)
1698 (setf (epg-context-result context) nil)
1699 (epg--start context
1700 (append (if always-trust '("--always-trust"))
1701 (if recipients '("--encrypt") '("--symmetric"))
1702 (if sign '("--sign"))
1703 (if sign
1704 (apply #'nconc
1705 (mapcar
1706 (lambda (signer)
1707 (list "-u"
1708 (epg-sub-key-id
1709 (car (epg-key-sub-key-list
1710 signer)))))
1711 (epg-context-signers context))))
1712 (if sign
1713 (epg--args-from-sig-notations
1714 (epg-context-sig-notations context)))
1715 (apply #'nconc
1716 (mapcar
1717 (lambda (recipient)
1718 (list "-r"
1719 (epg-sub-key-id
1720 (car (epg-key-sub-key-list recipient)))))
1721 recipients))
1722 (if (epg-data-file plain)
1723 (list "--" (epg-data-file plain)))))
1724 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1725 (unless (eq (epg-context-protocol context) 'CMS)
1726 (epg-wait-for-status context
1727 (if sign '("BEGIN_SIGNING") '("BEGIN_ENCRYPTION"))))
1728 (when (epg-data-string plain)
1729 (if (eq (process-status (epg-context-process context)) 'run)
1730 (process-send-string (epg-context-process context)
1731 (epg-data-string plain)))
1732 (if (eq (process-status (epg-context-process context)) 'run)
1733 (process-send-eof (epg-context-process context)))))
1735 (defun epg-encrypt-file (context plain recipients
1736 cipher &optional sign always-trust)
1737 "Encrypt a file PLAIN and store the result to a file CIPHER.
1738 If CIPHER is nil, it returns the result as a string.
1739 If RECIPIENTS is nil, it performs symmetric encryption."
1740 (unwind-protect
1741 (progn
1742 (setf (epg-context-output-file context)
1743 (or cipher (make-temp-file "epg-output")))
1744 (epg-start-encrypt context (epg-make-data-from-file plain)
1745 recipients sign always-trust)
1746 (epg-wait-for-completion context)
1747 (let ((errors (epg-context-result-for context 'error)))
1748 (if (and sign
1749 (not (epg-context-result-for context 'sign)))
1750 (signal 'epg-error
1751 (list "Sign failed" (epg-errors-to-string errors))))
1752 (if errors
1753 (signal 'epg-error
1754 (list "Encrypt failed" (epg-errors-to-string errors)))))
1755 (unless cipher
1756 (epg-read-output context)))
1757 (unless cipher
1758 (epg-delete-output-file context))
1759 (epg-reset context)))
1761 (defun epg-encrypt-string (context plain recipients
1762 &optional sign always-trust)
1763 "Encrypt a string PLAIN.
1764 If RECIPIENTS is nil, it performs symmetric encryption."
1765 (let ((input-file
1766 (unless (or (not sign)
1767 (eq (epg-context-protocol context) 'CMS))
1768 (make-temp-file "epg-input")))
1769 (coding-system-for-write 'binary))
1770 (unwind-protect
1771 (progn
1772 (setf (epg-context-output-file context)
1773 (make-temp-file "epg-output"))
1774 (if input-file
1775 (write-region plain nil input-file nil 'quiet))
1776 (epg-start-encrypt context
1777 (if input-file
1778 (epg-make-data-from-file input-file)
1779 (epg-make-data-from-string plain))
1780 recipients sign always-trust)
1781 (epg-wait-for-completion context)
1782 (let ((errors (epg-context-result-for context 'error)))
1783 (if (and sign
1784 (not (epg-context-result-for context 'sign)))
1785 (signal 'epg-error
1786 (list "Sign failed" (epg-errors-to-string errors))))
1787 (if errors
1788 (signal 'epg-error
1789 (list "Encrypt failed" (epg-errors-to-string errors)))))
1790 (epg-read-output context))
1791 (epg-delete-output-file context)
1792 (if input-file
1793 (delete-file input-file))
1794 (epg-reset context))))
1796 (defun epg-start-export-keys (context keys)
1797 "Initiate an export keys operation.
1799 If you use this function, you will need to wait for the completion of
1800 `epg-gpg-program' by using `epg-wait-for-completion' and call
1801 `epg-reset' to clear a temporary output file.
1802 If you are unsure, use synchronous version of this function
1803 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1804 (setf (epg-context-operation context) 'export-keys)
1805 (setf (epg-context-result context) nil)
1806 (epg--start context (cons "--export"
1807 (mapcar
1808 (lambda (key)
1809 (epg-sub-key-id
1810 (car (epg-key-sub-key-list key))))
1811 keys))))
1813 (defun epg-export-keys-to-file (context keys file)
1814 "Extract public KEYS."
1815 (unwind-protect
1816 (progn
1817 (setf (epg-context-output-file context)
1818 (or file (make-temp-file "epg-output")))
1819 (epg-start-export-keys context keys)
1820 (epg-wait-for-completion context)
1821 (let ((errors (epg-context-result-for context 'error)))
1822 (if errors
1823 (signal 'epg-error
1824 (list "Export keys failed"
1825 (epg-errors-to-string errors)))))
1826 (unless file
1827 (epg-read-output context)))
1828 (unless file
1829 (epg-delete-output-file context))
1830 (epg-reset context)))
1832 (defun epg-export-keys-to-string (context keys)
1833 "Extract public KEYS and return them as a string."
1834 (epg-export-keys-to-file context keys nil))
1836 (defun epg-start-import-keys (context keys)
1837 "Initiate an import keys operation.
1838 KEYS is a data object.
1840 If you use this function, you will need to wait for the completion of
1841 `epg-gpg-program' by using `epg-wait-for-completion' and call
1842 `epg-reset' to clear a temporary output file.
1843 If you are unsure, use synchronous version of this function
1844 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1845 (setf (epg-context-operation context) 'import-keys)
1846 (setf (epg-context-result context) nil)
1847 (epg--start context (if (epg-data-file keys)
1848 (list "--import" "--" (epg-data-file keys))
1849 (list "--import")))
1850 (when (epg-data-string keys)
1851 (if (eq (process-status (epg-context-process context)) 'run)
1852 (process-send-string (epg-context-process context)
1853 (epg-data-string keys)))
1854 (if (eq (process-status (epg-context-process context)) 'run)
1855 (process-send-eof (epg-context-process context)))))
1857 (defun epg--import-keys-1 (context keys)
1858 (unwind-protect
1859 (progn
1860 (epg-start-import-keys context keys)
1861 (epg-wait-for-completion context)
1862 (let ((errors (epg-context-result-for context 'error)))
1863 (if errors
1864 (signal 'epg-error
1865 (list "Import keys failed"
1866 (epg-errors-to-string errors))))))
1867 (epg-reset context)))
1869 (defun epg-import-keys-from-file (context keys)
1870 "Add keys from a file KEYS."
1871 (epg--import-keys-1 context (epg-make-data-from-file keys)))
1873 (defun epg-import-keys-from-string (context keys)
1874 "Add keys from a string KEYS."
1875 (epg--import-keys-1 context (epg-make-data-from-string keys)))
1877 (defun epg-start-receive-keys (context key-id-list)
1878 "Initiate a receive key operation.
1879 KEY-ID-LIST is a list of key IDs.
1881 If you use this function, you will need to wait for the completion of
1882 `epg-gpg-program' by using `epg-wait-for-completion' and call
1883 `epg-reset' to clear a temporary output file.
1884 If you are unsure, use synchronous version of this function
1885 `epg-receive-keys' instead."
1886 (setf (epg-context-operation context) 'receive-keys)
1887 (setf (epg-context-result context) nil)
1888 (epg--start context (cons "--recv-keys" key-id-list)))
1890 (defun epg-receive-keys (context keys)
1891 "Add keys from server.
1892 KEYS is a list of key IDs"
1893 (unwind-protect
1894 (progn
1895 (epg-start-receive-keys context keys)
1896 (epg-wait-for-completion context)
1897 (let ((errors (epg-context-result-for context 'error)))
1898 (if errors
1899 (signal 'epg-error
1900 (list "Receive keys failed"
1901 (epg-errors-to-string errors))))))
1902 (epg-reset context)))
1904 (defalias 'epg-import-keys-from-server 'epg-receive-keys)
1906 (defun epg-start-delete-keys (context keys &optional allow-secret)
1907 "Initiate a delete keys operation.
1909 If you use this function, you will need to wait for the completion of
1910 `epg-gpg-program' by using `epg-wait-for-completion' and call
1911 `epg-reset' to clear a temporary output file.
1912 If you are unsure, use synchronous version of this function
1913 `epg-delete-keys' instead."
1914 (setf (epg-context-operation context) 'delete-keys)
1915 (setf (epg-context-result context) nil)
1916 (epg--start context (cons (if allow-secret
1917 "--delete-secret-key"
1918 "--delete-key")
1919 (mapcar
1920 (lambda (key)
1921 (epg-sub-key-id
1922 (car (epg-key-sub-key-list key))))
1923 keys))))
1925 (defun epg-delete-keys (context keys &optional allow-secret)
1926 "Delete KEYS from the key ring."
1927 (unwind-protect
1928 (progn
1929 (epg-start-delete-keys context keys allow-secret)
1930 (epg-wait-for-completion context)
1931 (let ((errors (epg-context-result-for context 'error)))
1932 (if errors
1933 (signal 'epg-error
1934 (list "Delete keys failed"
1935 (epg-errors-to-string errors))))))
1936 (epg-reset context)))
1938 (defun epg-start-sign-keys (context keys &optional local)
1939 "Initiate a sign keys operation.
1941 If you use this function, you will need to wait for the completion of
1942 `epg-gpg-program' by using `epg-wait-for-completion' and call
1943 `epg-reset' to clear a temporary output file.
1944 If you are unsure, use synchronous version of this function
1945 `epg-sign-keys' instead."
1946 (declare (obsolete nil "23.1"))
1947 (setf (epg-context-operation context) 'sign-keys)
1948 (setf (epg-context-result context) nil)
1949 (epg--start context (cons (if local
1950 "--lsign-key"
1951 "--sign-key")
1952 (mapcar
1953 (lambda (key)
1954 (epg-sub-key-id
1955 (car (epg-key-sub-key-list key))))
1956 keys))))
1958 (defun epg-sign-keys (context keys &optional local)
1959 "Sign KEYS from the key ring."
1960 (declare (obsolete nil "23.1"))
1961 (unwind-protect
1962 (progn
1963 (epg-start-sign-keys context keys local)
1964 (epg-wait-for-completion context)
1965 (let ((errors (epg-context-result-for context 'error)))
1966 (if errors
1967 (signal 'epg-error
1968 (list "Sign keys failed"
1969 (epg-errors-to-string errors))))))
1970 (epg-reset context)))
1972 (defun epg-start-generate-key (context parameters)
1973 "Initiate a key generation.
1974 PARAMETERS is a string which specifies parameters of the generated key.
1975 See Info node `(gnupg) Unattended GPG key generation' in the
1976 GnuPG manual for the format.
1978 If you use this function, you will need to wait for the completion of
1979 `epg-gpg-program' by using `epg-wait-for-completion' and call
1980 `epg-reset' to clear a temporary output file.
1981 If you are unsure, use synchronous version of this function
1982 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
1983 (setf (epg-context-operation context) 'generate-key)
1984 (setf (epg-context-result context) nil)
1985 (if (epg-data-file parameters)
1986 (epg--start context (list "--batch" "--gen-key" "--"
1987 (epg-data-file parameters)))
1988 (epg--start context '("--batch" "--gen-key"))
1989 (if (eq (process-status (epg-context-process context)) 'run)
1990 (process-send-string (epg-context-process context)
1991 (epg-data-string parameters)))
1992 (if (eq (process-status (epg-context-process context)) 'run)
1993 (process-send-eof (epg-context-process context)))))
1995 (defun epg-generate-key-from-file (context parameters)
1996 "Generate a new key pair.
1997 PARAMETERS is a file which tells how to create the key."
1998 (unwind-protect
1999 (progn
2000 (epg-start-generate-key context (epg-make-data-from-file parameters))
2001 (epg-wait-for-completion context)
2002 (let ((errors (epg-context-result-for context 'error)))
2003 (if errors
2004 (signal 'epg-error
2005 (list "Generate key failed"
2006 (epg-errors-to-string errors))))))
2007 (epg-reset context)))
2009 (defun epg-generate-key-from-string (context parameters)
2010 "Generate a new key pair.
2011 PARAMETERS is a string which tells how to create the key."
2012 (unwind-protect
2013 (progn
2014 (epg-start-generate-key context (epg-make-data-from-string parameters))
2015 (epg-wait-for-completion context)
2016 (let ((errors (epg-context-result-for context 'error)))
2017 (if errors
2018 (signal 'epg-error
2019 (list "Generate key failed"
2020 (epg-errors-to-string errors))))))
2021 (epg-reset context)))
2023 (defun epg-start-edit-key (context key edit-callback handback)
2024 "Initiate an edit operation on KEY.
2026 EDIT-CALLBACK is called from process filter and takes 3
2027 arguments: the context, a status, an argument string, and the
2028 handback argument.
2030 If you use this function, you will need to wait for the completion of
2031 `epg-gpg-program' by using `epg-wait-for-completion' and call
2032 `epg-reset' to clear a temporary output file.
2033 If you are unsure, use synchronous version of this function
2034 `epg-edit-key' instead."
2035 (setf (epg-context-operation context) 'edit-key)
2036 (setf (epg-context-result context) nil)
2037 (setf (epg-context-edit-callback context) (cons edit-callback handback))
2038 (epg--start context (list "--edit-key"
2039 (epg-sub-key-id
2040 (car (epg-key-sub-key-list key))))))
2042 (defun epg-edit-key (context key edit-callback handback)
2043 "Edit KEY in the keyring."
2044 (unwind-protect
2045 (progn
2046 (epg-start-edit-key context key edit-callback handback)
2047 (epg-wait-for-completion context)
2048 (let ((errors (epg-context-result-for context 'error)))
2049 (if errors
2050 (signal 'epg-error
2051 (list "Edit key failed"
2052 (epg-errors-to-string errors))))))
2053 (epg-reset context)))
2055 (defun epg--decode-percent-escape (string)
2056 (let ((index 0))
2057 (while (string-match "%\\(\\(%\\)\\|\\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2058 string index)
2059 (if (match-beginning 2)
2060 (setq string (replace-match "%" t t string)
2061 index (1- (match-end 0)))
2062 (setq string (replace-match
2063 (string (string-to-number (match-string 3 string) 16))
2064 t t string)
2065 index (- (match-end 0) 2))))
2066 string))
2068 (defun epg--decode-hexstring (string)
2069 (let ((index 0))
2070 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
2071 (setq string (replace-match (string (string-to-number
2072 (match-string 0 string) 16))
2073 t t string)
2074 index (1- (match-end 0))))
2075 string))
2077 (defun epg--decode-quotedstring (string)
2078 (let ((index 0))
2079 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2080 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2081 string index)
2082 (if (match-beginning 2)
2083 (setq string (replace-match "\\2" t nil string)
2084 index (1- (match-end 0)))
2085 (if (match-beginning 3)
2086 (setq string (replace-match (string (string-to-number
2087 (match-string 0 string) 16))
2088 t t string)
2089 index (- (match-end 0) 2)))))
2090 string))
2092 (defun epg-dn-from-string (string)
2093 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2094 The return value is an alist mapping from types to values."
2095 (let ((index 0)
2096 (length (length string))
2097 alist type value group)
2098 (while (< index length)
2099 (if (eq index (string-match "[ \t\n\r]*" string index))
2100 (setq index (match-end 0)))
2101 (if (eq index (string-match
2102 "\\([0-9]+\\(\\.[0-9]+\\)*\\)[ \t\n\r]*=[ \t\n\r]*"
2103 string index))
2104 (setq type (match-string 1 string)
2105 index (match-end 0))
2106 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2107 string index))
2108 (setq type (match-string 1 string)
2109 index (match-end 0))))
2110 (unless type
2111 (error "Invalid type"))
2112 (if (eq index (string-match
2113 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2114 string index))
2115 (setq index (match-end 0)
2116 value (epg--decode-quotedstring (match-string 0 string)))
2117 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
2118 (setq index (match-end 0)
2119 value (epg--decode-hexstring (match-string 1 string)))
2120 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2121 string index))
2122 (setq index (match-end 0)
2123 value (epg--decode-quotedstring
2124 (match-string 0 string))))))
2125 (if group
2126 (if (stringp (car (car alist)))
2127 (setcar alist (list (cons type value) (car alist)))
2128 (setcar alist (cons (cons type value) (car alist))))
2129 (if (consp (car (car alist)))
2130 (setcar alist (nreverse (car alist))))
2131 (setq alist (cons (cons type value) alist)
2132 type nil
2133 value nil))
2134 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
2135 (setq index (match-end 0)
2136 group (eq (aref string (match-beginning 1)) ?+))))
2137 (nreverse alist)))
2139 (defun epg-decode-dn (alist)
2140 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2141 Type names are resolved using `epg-dn-type-alist'."
2142 (mapconcat
2143 (lambda (rdn)
2144 (if (stringp (car rdn))
2145 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
2146 (if entry
2147 (format "%s=%s" (cdr entry) (cdr rdn))
2148 (format "%s=%s" (car rdn) (cdr rdn))))
2149 (concat "(" (epg-decode-dn rdn) ")")))
2150 alist
2151 ", "))
2153 (provide 'epg)
2155 ;;; epg.el ends here