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