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