2 "EasyPG, yet another GnuPG interface.")
4 (defcustom epg-gpg-program "gpg"
5 "The `gpg' executable."
9 (defvar epg-user-id nil
10 "GnuPG ID of your default identity.")
12 (defvar epg-user-id-alist nil
13 "An alist mapping from key ID to user ID.")
15 (defvar epg-read-point nil)
16 (defvar epg-pending-status-list nil)
17 (defvar epg-key-id nil)
18 (defvar epg-context nil)
19 (defvar epg-debug nil)
21 (defvar epg-colons-pub-spec
23 (length "[0-9]+" 0 string-to-number)
24 (algorithm "[0-9]+" 0 string-to-number)
26 (creation-date "[0-9]+")
27 (expiration-date "[0-9]+")
32 (capability "[escaESCA]*"))
33 "The schema of keylisting output whose type is \"pub\".
34 This is used by `epg-list-keys'.")
36 (defvar epg-colons-sec-spec
38 (length "[0-9]+" 0 string-to-number)
39 (algorithm "[0-9]+" 0 string-to-number)
41 (creation-date "[0-9]+")
42 (expiration-date "[0-9]+")
45 "The schema of keylisting output whose type is \"sec\".
46 This is used by `epg-list-keys'.")
48 (defvar epg-colons-uid-spec
53 (creation-date "[0-9]+")
54 (expiration-date "[0-9]+")
58 "The schema of keylisting output whose type is \"uid\".
59 This is used by `epg-list-keys'.")
61 (defun epg-make-context (&optional protocol armor textmode include-certs)
62 "Return a context object."
63 (vector protocol armor textmode include-certs
64 (cons #'epg-passphrase-callback-function nil)
65 (cons #'epg-progress-callback-function nil)
68 (defun epg-context-protocol (context)
69 "Return the protocol used within the context."
72 (defun epg-context-armor (context)
73 "Return t if the output shouled be ASCII armored in the CONTEXT context."
76 (defun epg-context-textmode (context)
77 "Return t if canonical text mode should be used in the CONTEXT context."
80 (defun epg-context-include-certs (context)
81 "Return how many certificates should be included in an S/MIME signed
85 (defun epg-context-passphrase-callback-info (context)
86 "Return the function used to query passphrase."
89 (defun epg-context-progress-callback-info (context)
90 "Return the function which handles progress update."
93 (defun epg-context-signers (context)
94 "Return the list of key-id for singning."
97 (defun epg-context-process (context)
98 "Return the process object of `epg-gpg-program'.
99 This function is for internal use only."
102 (defun epg-context-output-file (context)
103 "Return the output file of `epg-gpg-program'.
104 This function is for internal use only."
107 (defun epg-context-result (context)
108 "Return the result of the previous cryptographic operation."
111 (defun epg-context-set-protocol (context protocol)
112 "Set the protocol used within the context."
113 (aset context 0 protocol))
115 (defun epg-context-set-armor (context armor)
116 "Specify if the output shouled be ASCII armored in the CONTEXT context."
117 (aset context 1 armor))
119 (defun epg-context-set-textmode (context textmode)
120 "Specify if canonical text mode should be used in the CONTEXT context."
121 (aset context 2 textmode))
123 (defun epg-context-set-include-certs (context include-certs)
124 "Set how many certificates should be included in an S/MIME signed message."
125 (aset context 3 include-certs))
127 (defun epg-context-set-passphrase-callback-info (context
128 passphrase-callback-info)
129 "Set the function used to query passphrase."
130 (aset context 4 passphrase-callback-info))
132 (defun epg-context-set-progress-callback-info (context progress-callback-info)
133 "Set the function which handles progress update."
134 (aset context 5 progress-callback-info))
136 (defun epg-context-set-signers (context signers)
137 "Set the list of key-id for singning."
138 (aset context 6 signers))
140 (defun epg-context-set-process (context process)
141 "Set the process object of `epg-gpg-program'.
142 This function is for internal use only."
143 (aset context 7 process))
145 (defun epg-context-set-output-file (context output-file)
146 "Set the output file of `epg-gpg-program'.
147 This function is for internal use only."
148 (aset context 8 output-file))
150 (defun epg-context-set-result (context result)
151 "Set the result of the previous cryptographic operation."
152 (aset context 9 result))
154 (defun epg-make-signature (status key-id user-id)
155 "Return a signature object."
156 (vector status key-id user-id nil nil))
158 (defun epg-signature-status (signature)
159 "Return the status code of SIGNATURE."
162 (defun epg-signature-key-id (signature)
163 "Return the key-id of SIGNATURE."
166 (defun epg-signature-user-id (signature)
167 "Return the user-id of SIGNATURE."
170 (defun epg-signature-validity (signature)
171 "Return the validity of SIGNATURE."
174 (defun epg-signature-fingerprint (signature)
175 "Return the fingerprint of SIGNATURE."
178 (defun epg-signature-set-status (signature status)
179 "Set the status code of SIGNATURE."
180 (aset signature 0 status))
182 (defun epg-signature-set-key-id (signature key-id)
183 "Set the key-id of SIGNATURE."
184 (aset signature 1 key-id))
186 (defun epg-signature-set-user-id (signature user-id)
187 "Set the user-id of SIGNATURE."
188 (aset signature 2 user-id))
190 (defun epg-signature-set-validity (signature validity)
191 "Set the validity of SIGNATURE."
192 (aset signature 3 validity))
194 (defun epg-signature-set-fingerprint (signature fingerprint)
195 "Set the fingerprint of SIGNATURE."
196 (aset signature 4 fingerprint))
198 (defun epg-context-result-for (context name)
199 (cdr (assq name (epg-context-result context))))
201 (defun epg-context-set-result-for (context name value)
202 (let* ((result (epg-context-result context))
203 (entry (assq name result)))
206 (epg-context-set-result context (cons (cons name value) result)))))
208 (defun epg-start (context args)
209 "Start `epg-gpg-program' in a subprocess with given ARGS."
210 (let* ((args (append (list "--no-tty"
214 (if (epg-context-armor context) '("--armor"))
215 (if (epg-context-textmode context) '("--textmode"))
216 (if (epg-context-output-file context)
217 (list "--output" (epg-context-output-file context)))
219 (coding-system-for-write 'binary)
220 process-connection-type
221 (orig-mode (default-file-modes))
222 (buffer (generate-new-buffer " *epg*"))
224 (with-current-buffer buffer
225 (make-local-variable 'epg-read-point)
226 (setq epg-read-point (point-min))
227 (make-local-variable 'epg-pending-status-list)
228 (setq epg-pending-status-list nil)
229 (make-local-variable 'epg-key-id)
230 (setq epg-key-id nil)
231 (make-local-variable 'epg-context)
232 (setq epg-context context))
235 (set-default-file-modes 448)
237 (apply #'start-process "epg" buffer epg-gpg-program args)))
238 (set-default-file-modes orig-mode))
239 (set-process-filter process #'epg-process-filter)
240 (epg-context-set-process context process)))
242 (defun epg-process-filter (process input)
245 (set-buffer (get-buffer-create " *epg-debug*"))
246 (goto-char (point-max))
248 (if (buffer-live-p (process-buffer process))
250 (set-buffer (process-buffer process))
251 (goto-char (point-max))
253 (goto-char epg-read-point)
255 (while (looking-at ".*\n") ;the input line is finished
257 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
258 (let* ((status (match-string 1))
259 (string (match-string 2))
260 (symbol (intern-soft (concat "epg-status-" status))))
261 (if (member status epg-pending-status-list)
262 (setq epg-pending-status-list nil))
265 (funcall symbol process string)))))
267 (setq epg-read-point (point)))))
269 (defun epg-read-output (context)
271 (if (fboundp 'set-buffer-multibyte)
272 (set-buffer-multibyte nil))
273 (if (file-exists-p (epg-context-output-file context))
274 (let ((coding-system-for-read (if (epg-context-textmode context)
277 (insert-file-contents (epg-context-output-file context))
280 (defun epg-wait-for-status (context status-list)
281 (with-current-buffer (process-buffer (epg-context-process context))
282 (setq epg-pending-status-list status-list)
283 (while (and (eq (process-status (epg-context-process context)) 'run)
284 epg-pending-status-list)
285 (accept-process-output (epg-context-process context) 1))))
287 (defun epg-wait-for-completion (context)
288 (if (eq (process-status (epg-context-process context)) 'run)
289 (process-send-eof (epg-context-process context)))
290 (while (eq (process-status (epg-context-process context)) 'run)
291 ;; We can't use accept-process-output instead of sit-for here
292 ;; because it may cause an interrupt during the sentinel execution.
295 (defun epg-reset (context)
296 (if (and (epg-context-process context)
297 (buffer-live-p (process-buffer (epg-context-process context))))
298 (kill-buffer (process-buffer (epg-context-process context))))
299 (epg-context-set-process context nil)
300 (if (file-exists-p (epg-context-output-file context))
301 (delete-file (epg-context-output-file context))))
303 (defun epg-status-USERID_HINT (process string)
304 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
305 (let* ((key-id (match-string 1 string))
306 (user-id (match-string 2 string))
307 (entry (assoc key-id epg-user-id-alist)))
309 (setcdr entry user-id)
310 (setq epg-user-id-alist (cons (cons key-id user-id)
311 epg-user-id-alist))))))
313 (defun epg-status-NEED_PASSPHRASE (process string)
314 (if (string-match "\\`\\([^ ]+\\)" string)
315 (setq epg-key-id (match-string 1 string))))
317 (defun epg-status-NEED_PASSPHRASE_SYM (process string)
318 (setq epg-key-id 'SYM))
320 (defun epg-status-NEED_PASSPHRASE_PIN (process string)
321 (setq epg-key-id 'PIN))
323 (defun epg-status-GET_HIDDEN (process string)
325 (funcall (car (epg-context-passphrase-callback-info epg-context))
327 (cdr (epg-context-passphrase-callback-info epg-context))))
332 (setq string (concat passphrase "\n"))
333 (fillarray passphrase 0)
334 (setq passphrase nil)
335 (process-send-string process string))
337 (fillarray string 0))))))
339 (defun epg-status-GOODSIG (process string)
340 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
341 (epg-context-set-result-for
344 (cons (epg-make-signature 'good
345 (match-string 1 string)
346 (match-string 2 string))
347 (epg-context-result-for epg-context 'verify)))))
349 (defun epg-status-EXPSIG (process string)
350 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
351 (epg-context-set-result-for
354 (cons (epg-make-signature 'expired
355 (match-string 1 string)
356 (match-string 2 string))
357 (epg-context-result-for epg-context 'verify)))))
359 (defun epg-status-EXPKEYSIG (process string)
360 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
361 (epg-context-set-result-for
364 (cons (epg-make-signature 'expired-key
365 (match-string 1 string)
366 (match-string 2 string))
367 (epg-context-result-for epg-context 'verify)))))
369 (defun epg-status-REVKEYSIG (process string)
370 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
371 (epg-context-set-result-for
374 (cons (epg-make-signature 'revoked-key
375 (match-string 1 string)
376 (match-string 2 string))
377 (epg-context-result-for epg-context 'verify)))))
379 (defun epg-status-BADSIG (process string)
380 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
381 (epg-context-set-result-for
384 (cons (epg-make-signature 'bad
385 (match-string 1 string)
386 (match-string 2 string))
387 (epg-context-result-for epg-context 'verify)))))
389 (defun epg-status-VALIDSIG (process string)
390 (let ((signature (car (epg-context-result-for epg-context 'verify))))
392 (eq (epg-signature-status signature) 'good)
393 (string-match "\\`\\([^ ]+\\) " string))
394 (epg-signature-set-fingerprint signature (match-string 1 string)))))
396 (defun epg-status-TRUST_UNDEFINED (process string)
397 (let ((signature (car (epg-context-result-for epg-context 'verify))))
399 (eq (epg-signature-status signature) 'good))
400 (epg-signature-set-validity signature 'undefined))))
402 (defun epg-status-TRUST_NEVER (process string)
403 (let ((signature (car (epg-context-result-for epg-context 'verify))))
405 (eq (epg-signature-status signature) 'good))
406 (epg-signature-set-validity signature 'never))))
408 (defun epg-status-TRUST_MARGINAL (process string)
409 (let ((signature (car (epg-context-result-for epg-context 'verify))))
411 (eq (epg-signature-status signature) 'marginal))
412 (epg-signature-set-validity signature 'marginal))))
414 (defun epg-status-TRUST_FULLY (process string)
415 (let ((signature (car (epg-context-result-for epg-context 'verify))))
417 (eq (epg-signature-status signature) 'good))
418 (epg-signature-set-validity signature 'fully))))
420 (defun epg-status-TRUST_ULTIMATE (process string)
421 (let ((signature (car (epg-context-result-for epg-context 'verify))))
423 (eq (epg-signature-status signature) 'good))
424 (epg-signature-set-validity signature 'ultimate))))
426 (defun epg-status-PROGRESS (process string)
427 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
429 (funcall (car (epg-context-progress-callback-info epg-context))
430 (match-string 1 string)
431 (match-string 2 string)
432 (string-to-number (match-string 3 string))
433 (string-to-number (match-string 4 string))
434 (cdr (epg-context-progress-callback-info epg-context)))))
436 (defun epg-status-DECRYPTION_FAILED (process string)
437 (epg-context-set-result-for
439 (cons 'decryption-failed
440 (epg-context-result-for epg-context 'error))))
442 (defun epg-status-NODATA (process string)
443 (epg-context-set-result-for
445 (cons (cons 'no-data (string-to-number string))
446 (epg-context-result-for epg-context 'error))))
448 (defun epg-status-UNEXPECTED (process string)
449 (epg-context-set-result-for
451 (cons (cons 'unexpected (string-to-number string))
452 (epg-context-result-for epg-context 'error))))
454 (defun epg-status-KEYEXPIRED (process string)
455 (epg-context-set-result-for
457 (cons (cons 'key-expired string)
458 (epg-context-result-for epg-context 'error))))
460 (defun epg-status-KEYREVOKED (process string)
461 (epg-context-set-result-for
464 (epg-context-result-for epg-context 'error))))
466 (defun epg-status-BADARMOR (process string)
467 (epg-context-set-result-for
470 (epg-context-result-for epg-context 'error))))
472 (defun epg-passphrase-callback-function (key-id handback)
475 "Passphrase for symmetric encryption: "
477 "Passphrase for PIN: "
478 (let ((entry (assoc key-id epg-user-id-alist)))
480 (format "Passphrase for %s %s: " key-id (cdr entry))
481 (format "Passphrase for %s: " key-id)))))))
483 (defun epg-progress-callback-function (what char current total handback)
484 (message "%s: %d%%/%d%%" what current total))
486 (defun epg-list-keys (name &optional secret)
487 "List keys associated with STRING."
488 (let ((args (list "--with-colons" "--no-greeting" "--batch"
490 (if secret "--list-secret-keys" "--list-keys")
492 keys type symbol pointer)
494 (apply #'call-process epg-gpg-program nil (list t nil) nil args)
495 (goto-char (point-min))
496 (while (looking-at "\\([a-z][a-z][a-z]\\):\\(.*\\)")
497 (setq type (match-string 1)
498 symbol (intern-soft (format "epg-colons-%s-spec" type)))
499 (if (member type '("pub" "sec"))
500 (setq keys (cons nil keys)))
503 (setcar keys (cons (cons (intern type)
505 (symbol-value symbol)
511 (setcar pointer (nreverse (car pointer)))
512 (setq pointer (cdr pointer)))
515 (defun epg-parse-colons (alist string)
519 (or (null (car alist))
522 (concat "\\(" (nth 1 (car alist)) "\\)?:")
526 (setq index (match-end 0))
527 (if (match-beginning 1)
529 (cons (cons (car (car alist))
530 (funcall (or (nth 3 (car alist)) #'identity)
532 (1+ (or (nth 2 (car alist)) 0))
535 (setq index (1+ index)))
536 (setq alist (cdr alist)))
539 (if (fboundp 'make-temp-file)
540 (defalias 'epg-make-temp-file 'make-temp-file)
541 ;; stolen from poe.el.
542 (defun epg-make-temp-file (prefix)
543 "Create a temporary file.
544 The returned file name (created by appending some random characters at the end
545 of PREFIX, and expanding against `temporary-file-directory' if necessary),
546 is guaranteed to point to a newly created empty file.
547 You can then use `write-region' to write new data into the file."
548 (let (tempdir tempfile)
551 ;; First, create a temporary directory.
552 (while (condition-case ()
554 (setq tempdir (make-temp-name
556 (file-name-directory prefix)
558 ;; return nil or signal an error.
559 (make-directory tempdir))
561 (file-already-exists t)))
562 (set-file-modes tempdir 448)
563 ;; Second, create a temporary file in the tempdir.
564 ;; There *is* a race condition between `make-temp-name'
565 ;; and `write-region', but we don't care it since we are
566 ;; in a private directory now.
567 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
568 (write-region "" nil tempfile nil 'silent)
569 (set-file-modes tempfile 384)
570 ;; Finally, make a hard-link from the tempfile.
571 (while (condition-case ()
573 (setq file (make-temp-name prefix))
574 ;; return nil or signal an error.
575 (add-name-to-file tempfile file))
577 (file-already-exists t)))
579 ;; Cleanup the tempfile.
581 (file-exists-p tempfile)
582 (delete-file tempfile))
583 ;; Cleanup the tempdir.
585 (file-directory-p tempdir)
586 (delete-directory tempdir))))))
589 (defun epg-start-decrypt (context input-file)
590 "Initiate a decrypt operation on INPUT-FILE.
592 If you use this function, you will need to wait for the completion of
593 `epg-gpg-program' by using `epg-wait-for-completion' and call
594 `epg-reset' to clear a temporaly output file.
595 If you are unsure, use synchronous version of this function
596 `epg-decrypt-string' instead."
597 (epg-context-set-result context nil)
598 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
600 (list "--decrypt" input-file))
601 (epg-wait-for-status context '("BEGIN_DECRYPTION")))
604 (defun epg-decrypt-file (context input-file)
605 "Decrypt INPUT-FILE and return the plain text."
608 (epg-start-decrypt context input-file)
609 (epg-wait-for-completion context)
610 (if (epg-context-result-for context 'error)
611 (error "Decryption failed"))
612 (epg-read-output context))
613 (epg-reset context)))
616 (defun epg-decrypt-string (context string)
617 "Decrypt STRING and return the plain text."
618 (let ((input-file (epg-make-temp-file "epg-input"))
619 (coding-system-for-write 'binary))
622 (write-region string nil input-file)
623 (epg-decrypt-file context input-file))
624 (if (file-exists-p input-file)
625 (delete-file input-file)))))
628 (defun epg-start-verify (context signature &optional string)
629 "Initiate a verify operation on SIGNATURE.
631 For a detached signature, both SIGNATURE and STRING should be string.
632 For a normal or a clear text signature, STRING should be nil.
634 If you use this function, you will need to wait for the completion of
635 `epg-gpg-program' by using `epg-wait-for-completion' and call
636 `epg-reset' to clear a temporaly output file.
637 If you are unsure, use synchronous version of this function
638 `epg-verify-string' instead."
639 (epg-context-set-result context nil)
640 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
642 ;; Detached signature.
645 (append (list "--verify")
646 (list signature "-")))
647 (if (eq (process-status (epg-context-process context)) 'run)
648 (process-send-string (epg-context-process context) string)))
649 ;; Normal (or cleartext) signature.
650 (epg-start context (list "--verify"))
651 (if (eq (process-status (epg-context-process context)) 'run)
652 (process-send-string (epg-context-process context) signature))))
655 (defun epg-verify-file (context input-file &optional string)
658 For a detached signature, both INPUT-FILE and STRING should be string.
659 For a normal or a clear text signature, STRING should be nil."
662 (epg-start-verify context input-file string)
663 (epg-wait-for-completion context)
664 (epg-context-result-for context 'verify))
665 (epg-reset context)))
668 (defun epg-verify-string (context signature &optional string)
671 For a detached signature, both SIGNATURE and STRING should be string.
672 For a normal or a clear text signature, STRING should be nil."
673 (let ((input-file (epg-make-temp-file "epg-input"))
674 (coding-system-for-write 'binary))
678 (write-region signature nil input-file))
679 (epg-verify-file context input-file string))
680 (if (file-exists-p input-file)
681 (delete-file input-file)))))
684 (defun epg-start-sign (context string &optional mode)
685 "Initiate a sign operation on STRING.
687 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
688 If MODE is t or 'detached, it makes a detached signature.
689 Otherwise, it makes a normal signature.
691 If you use this function, you will need to wait for the completion of
692 `epg-gpg-program' by using `epg-wait-for-completion' and call
693 `epg-reset' to clear a temporaly output file.
694 If you are unsure, use synchronous version of this function
695 `epg-sign-string' instead."
696 (epg-context-set-result context nil)
697 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
699 (append (list (if (eq mode 'clearsign)
701 (if (or (eq mode t) (eq mode 'detached))
705 (mapcar (lambda (signer)
707 (epg-context-signers context)))))
708 (epg-wait-for-status context '("BEGIN_SIGNING"))
709 (if (eq (process-status (epg-context-process context)) 'run)
710 (process-send-string (epg-context-process context) string)))
713 (defun epg-sign-string (context string &optional mode)
714 "Sign STRING and return the output as string.
715 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
716 If MODE is t or 'detached, it makes a detached signature.
717 Otherwise, it makes a normal signature."
720 (epg-start-sign context string mode)
721 (epg-wait-for-completion context)
722 (if (epg-context-result-for context 'error)
723 (error "Sign failed"))
724 (epg-read-output context))
725 (epg-reset context)))
728 (defun epg-start-encrypt (context string recipients
729 &optional sign always-trust)
730 "Initiate an encrypt operation on STRING.
731 If RECIPIENTS is nil, it performs symmetric encryption.
733 If you use this function, you will need to wait for the completion of
734 `epg-gpg-program' by using `epg-wait-for-completion' and call
735 `epg-reset' to clear a temporaly output file.
736 If you are unsure, use synchronous version of this function
737 `epg-encrypt-string' instead."
738 (epg-context-set-result context nil)
739 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
741 (append (if always-trust '("--always-trust"))
742 (if recipients '("--encrypt") '("--symmetric"))
746 (mapcar (lambda (signer)
748 (epg-context-signers context)))))
750 (mapcar (lambda (recipient)
751 (list "-r" recipient))
754 (epg-wait-for-status context '("BEGIN_SIGNING"))
755 (if (null recipients)
756 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
757 (if (eq (process-status (epg-context-process context)) 'run)
758 (process-send-string (epg-context-process context) string)))
761 (defun epg-encrypt-string (context string recipients
762 &optional sign always-trust)
764 If RECIPIENTS is nil, it performs symmetric encryption."
767 (epg-start-encrypt context string recipients sign always-trust)
768 (epg-wait-for-completion context)
769 (if (epg-context-result-for context 'error)
770 (error "Encrypt failed"))
771 (epg-read-output context))
772 (epg-reset context)))
775 (defun epg-start-export-keys (context pattern)
776 "Initiate an export keys operation.
778 If you use this function, you will need to wait for the completion of
779 `epg-gpg-program' by using `epg-wait-for-completion' and call
780 `epg-reset' to clear a temporaly output file.
781 If you are unsure, use synchronous version of this function
782 `epg-export-keys' instead."
783 (epg-context-set-result context nil)
784 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
785 (epg-start context (list "--export" pattern)))
788 (defun epg-export-keys (context pattern)
789 "Extract public keys matched with PATTERN and return them."
792 (epg-start-export-keys context pattern)
793 (epg-wait-for-completion context)
794 (if (epg-context-result-for context 'error)
795 (error "Export keys failed"))
796 (epg-read-output context))
797 (epg-reset context)))
800 (defun epg-start-import-keys (context keys)
801 "Initiate an import key operation.
803 If you use this function, you will need to wait for the completion of
804 `epg-gpg-program' by using `epg-wait-for-completion' and call
805 `epg-reset' to clear a temporaly output file.
806 If you are unsure, use synchronous version of this function
807 `epg-import-keys' instead."
808 (epg-context-set-result context nil)
809 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
810 (epg-start context (list "--import"))
811 (if (eq (process-status (epg-context-process context)) 'run)
812 (process-send-string (epg-context-process context) keys)))
815 (defun epg-import-keys (context keys)
819 (epg-start-import-keys context keys)
820 (epg-wait-for-completion context)
821 (if (epg-context-result-for context 'error)
822 (error "Import keys failed"))
823 (epg-read-output context))
824 (epg-reset context)))