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