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
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/>.
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
59 ;; from gnupg/include/cipher.h
60 (defconst epg-pubkey-algorithm-alist
68 ;; from gnupg/include/cipher.h
69 (defconst epg-digest-algorithm-alist
78 ;; from gnupg/include/cipher.h
79 (defconst epg-compress-algorithm-alist
85 (defconst epg-invalid-recipients-reason-alist
86 '((0 .
"No specific reason given")
88 (2 .
"Ambiguous specification")
89 (3 .
"Wrong key usage")
94 (8 .
"Policy mismatch")
95 (9 .
"Not a secret key")
96 (10 .
"Key not trusted")))
98 (defconst epg-delete-problem-reason-alist
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")
107 (4 .
"New signatures")
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
139 (defvar epg-key-capability-alist
143 (?a . authentication
)
146 (defvar epg-new-signature-type-alist
151 (defvar epg-dn-type-alist
152 '(("1.2.840.113549.1.9.1" .
"EMail")
156 ("0.2.262.1.10.7.20" .
"NameDistinguisher")
157 ("2.5.4.16" .
"ADDR")
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
170 (:constructor epg-make-data-from-file
(file))
171 (:constructor epg-make-data-from-string
(string))
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
183 (:constructor epg-context--make
184 (protocol &optional armor textmode include-certs
185 cipher-algorithm digest-algorithm
190 (`OpenPGP epg-gpg-program
)
191 (`CMS epg-gpgsm-program
)
192 (_ (signal 'epg-error
193 (list "unknown protocol" protocol
)))))))
198 (home-directory epg-gpg-home-directory
)
205 (passphrase-callback (list #'epg-passphrase-callback-function
))
218 ;; This is not an alias, just so we can mark it as autoloaded.
220 (defun epg-make-context (&optional protocol armor textmode include-certs
221 cipher-algorithm digest-algorithm
223 "Return a context object."
224 (epg-context--make (or protocol
'OpenPGP
)
225 armor textmode include-certs
226 cipher-algorithm digest-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
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
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
)
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
283 (:constructor epg-make-signature
284 (status &optional key-id
))
299 (cl-defstruct (epg-new-signature
301 (:constructor epg-make-new-signature
302 (type pubkey-algorithm digest-algorithm
303 class creation-time fingerprint
))
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
315 (:constructor epg-make-key
(owner-trust))
318 (owner-trust nil
:read-only t
)
319 sub-key-list user-id-list
)
321 (cl-defstruct (epg-sub-key
323 (:constructor epg-make-sub-key
324 (validity capability secret-p algorithm length id
325 creation-time expiration-time
))
328 validity capability secret-p algorithm length id
329 creation-time expiration-time fingerprint
)
331 (cl-defstruct (epg-user-id
333 (:constructor epg-make-user-id
(validity string
))
336 validity string signature-list
)
338 (cl-defstruct (epg-key-signature
340 (:constructor epg-make-key-signature
341 (validity pubkey-algorithm key-id creation-time
342 expiration-time user-id class
346 validity pubkey-algorithm key-id creation-time
347 expiration-time user-id class
350 (cl-defstruct (epg-sig-notation
352 (:constructor epg-make-sig-notation
353 (name value
&optional human-readable critical
))
356 name value human-readable critical
)
358 (cl-defstruct (epg-import-status
360 (:constructor epg-make-import-status
362 &optional reason new user-id signature sub-key secret
))
365 fingerprint reason new user-id signature sub-key secret
)
367 (cl-defstruct (epg-import-result
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
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
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
)))
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
)
401 (pubkey-algorithm (epg-signature-pubkey-algorithm signature
))
402 (key-id (epg-signature-key-id signature
)))
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 "))
419 (if (stringp 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
))
432 (or (cdr (assq pubkey-algorithm epg-pubkey-algorithm-alist
))
433 (format "(unknown algorithm %d)" pubkey-algorithm
)))
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."
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 ")
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)
473 (epg-import-result-imported-rsa
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)
503 ((eq (car error
) 'exit
)
505 ((eq (car error
) 'quit
)
507 ((eq (car error
) 'no-data
)
508 (let ((entry (assq (cdr error
) epg-no-data-reason-alist
)))
510 (format "No data (%s)" (downcase (cdr entry
)))
512 ((eq (car error
) 'unexpected
)
513 (let ((entry (assq (cdr error
) epg-unexpected-reason-alist
)))
515 (format "Unexpected (%s)" (downcase (cdr entry
)))
517 ((eq (car error
) 'bad-armor
)
519 ((memq (car error
) '(invalid-recipient invalid-signer
))
521 (if (eq (car error
) 'invalid-recipient
)
522 "Unusable public key"
523 "Unusable secret key")
524 (let ((entry (assq 'requested
(cdr error
))))
526 (format ": %s" (cdr entry
))
528 (let ((entry (assq 'reason
(cdr error
))))
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
)))
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
)
541 ((eq (car error
) 'no-signers
)
543 ((eq (car error
) 'delete-problem
)
544 (let ((entry (assq (cdr error
) epg-delete-problem-reason-alist
)))
546 (format "Delete problem (%s)" (downcase (cdr entry
)))
548 ((eq (car error
) '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"
564 (if (and (not (eq (epg-context-protocol context
) 'CMS
))
565 (string-match ":" (or agent-info
"")))
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
)
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
584 (process-environment process-environment
)
585 (buffer (generate-new-buffer " *epg*"))
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
))
597 (when (= (call-process "tty" "/dev/fd/0" t
) 0)
599 (setq terminal-name
(buffer-string)))
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
)
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
))))
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'.
621 ;; <http://lists.gnu.org/archive/html/emacs-devel/2007-02/msg00755.html>
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))))
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")
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
))
658 (make-pipe-process :name
"epg-error"
659 :buffer
(generate-new-buffer " *epg-error*")
660 ;; Suppress "XXX finished" line.
663 (setf (epg-context-error-buffer context
) (process-buffer error-process
))
665 (setq process
(make-process :name
"epg"
667 :command
(cons (epg-context-program context
)
669 :connection-type
'pipe
670 :coding
'(binary . binary
)
671 :filter
#'epg--process-filter
672 :stderr error-process
674 (setf (epg-context-process context
) process
)))
676 (defun epg--process-filter (process input
)
680 (setq epg-debug-buffer
(generate-new-buffer " *epg-debug*")))
681 (goto-char (point-max))
683 (if (buffer-live-p (process-buffer process
))
684 (with-current-buffer (process-buffer process
)
686 (goto-char (point-max))
688 (unless epg-process-filter-running
689 (let ((epg-process-filter-running t
))
690 (goto-char epg-read-point
)
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))
697 (if (member status epg-pending-status-list
)
698 (setq epg-pending-status-list nil
))
699 ;; When editing a key, delegate all interaction
701 (if (eq (epg-context-operation epg-context
) 'edit-key
)
702 (funcall (car (epg-context-edit-callback
707 (cdr (epg-context-edit-callback
709 ;; Otherwise call epg--status-STATUS function.
710 (setq symbol
(intern-soft (concat "epg--status-"
714 (funcall symbol epg-context string
)))
715 (setq epg-last-status
(cons status string
))))
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."
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
))
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
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.
748 ;; Restore Emacs frame on text terminal, when pinentry-curses has terminated.
749 (if (with-current-buffer (process-buffer (epg-context-process context
))
751 (> (float-time (or (nth 5 (file-attributes epg-agent-file
))
753 (float-time epg-agent-mtime
))))
755 (epg-context-set-result-for
757 (nreverse (epg-context-result-for context
'error
)))
758 (setf (epg-context-error-output context
)
759 (with-current-buffer (epg-context-error-buffer context
)
762 (defun epg-reset (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
))))
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
)))
789 (setq user-id
(epg--decode-coding-string
790 (epg--decode-percent-escape user-id
)
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
))
809 (if (fboundp 'clear-string
)
810 (defalias 'epg--clear-string
'clear-string
)
811 (defun epg--clear-string (string)
812 (fillarray string
0))))
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"))
826 passphrase-with-new-line
827 encoded-passphrase-with-new-line
)
833 (car (epg-context-passphrase-callback context
))
836 (cdr (epg-context-passphrase-callback context
))))
838 (setq passphrase-with-new-line
(concat passphrase
"\n"))
839 (epg--clear-string passphrase
)
840 (setq passphrase nil
)
841 (if epg-passphrase-coding-system
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
)))
856 (epg-context-set-result-for
859 (epg-context-result-for context
'error
)))
860 (delete-process (epg-context-process context
))))
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
)))
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
)
887 (if (funcall (or (intern-soft (concat "epg--prompt-GET_BOOL-" string
))
888 #'epg--prompt-GET_BOOL
)
890 (process-send-string (epg-context-process context
) "y\n")
891 (process-send-string (epg-context-process context
) "n\n"))
893 (epg-context-set-result-for
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
))
903 (process-send-string (epg-context-process context
)
907 (concat string
": ")))
910 (epg-context-set-result-for
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
924 (cons (epg-make-signature status key-id
)
925 (epg-context-result-for context
'verify
)))
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
)
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
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
))))
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
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
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]+\\)"
985 (let ((signature (epg-make-signature 'error
)))
986 (epg-context-set-result-for
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]\\) \
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
))))
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
))))
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
))))
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
))))
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
))))
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
))))
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
)))
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
))))
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]+\\)"
1079 (funcall (car (epg-context-progress-callback 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
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
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
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
1124 (cons '(key-revoked)
1125 (epg-context-result-for context
'key
))))
1127 (defun epg--status-BADARMOR (context _string
)
1128 (epg-context-set-result-for
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
1137 (cons (list 'invalid-recipient
1139 (string-to-number (match-string 1 string
)))
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
1148 (cons (list 'invalid-signer
1150 (string-to-number (match-string 1 string
)))
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
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
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
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
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
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
)))
1210 (setq user-id
(epg--decode-coding-string
1211 (epg--decode-percent-escape user-id
)
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
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
))
1272 (if (eq key-id
'PIN
)
1273 "Passphrase for PIN: "
1274 (let ((entry (assoc key-id epg-user-id-alist
)))
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
)
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
))
1292 (coding-system-for-read 'binary
)
1293 keys string field index
)
1296 (unless (listp name
)
1297 (setq name
(list name
)))
1299 (setq args
(append args
(list list-keys-option
(car name
)))
1301 (setq args
(append args
(list list-keys-option
))))
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)
1312 (while (and (< field
(length (car keys
)))
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
))))
1320 (defun epg--make-sub-key-1 (line)
1323 (cdr (assq (string-to-char (aref line
1)) epg-key-validity-alist
)))
1325 (mapcar (lambda (char) (cdr (assq char epg-key-capability-alist
)))
1327 (member (aref line
0) '("sec" "ssb"))
1328 (string-to-number (aref line
3))
1329 (string-to-number (aref line
2))
1331 (epg--time-from-seconds (aref line
5))
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
)
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
))))
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))
1363 (while (string-match "\"" string index
)
1364 (setq string
(replace-match "\\\"" t t string
)
1365 index
(1+ (match-end 0))))
1367 (setq string
(epg--decode-coding-string
1368 (car (read-from-string (concat "\"" string
"\"")))
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
)))
1378 (epg-dn-from-string 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")
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
)
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
))))
1407 (epg--gv-nreverse (epg-user-id-signature-list (car pointer-1
)))
1408 (setq pointer-1
(cdr pointer-1
)))
1409 (setq pointer
(cdr pointer
)))
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))
1425 (setq prefix
(expand-file-name prefix
1426 (if (featurep 'xemacs
)
1428 temporary-file-directory
)))
1431 ;; First, create a temporary directory.
1432 (set-default-file-modes #o700
)
1433 (while (condition-case ()
1435 (setq tempdir
(make-temp-name
1437 (file-name-directory prefix
)
1439 ;; return nil or signal an error.
1440 (make-directory tempdir
))
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 ()
1452 (setq file
(make-temp-name prefix
))
1453 ;; return nil or signal an error.
1454 (add-name-to-file tempfile file
))
1456 (file-already-exists t
)))
1458 (set-default-file-modes orig-modes
)
1459 ;; Cleanup the tempfile.
1461 (file-exists-p tempfile
)
1462 (delete-file tempfile
))
1463 ;; Cleanup the tempdir.
1465 (file-directory-p tempdir
)
1466 (delete-directory tempdir
)))))))
1468 (defun epg--args-from-sig-notations (notations)
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
)))))
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
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
)
1520 (list "Decryption failed" (epg-errors-to-string errors
))))
1521 (unless (epg-context-result-for context
'decryption-okay
)
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."
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
)
1536 (epg-read-output context
)))
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
))
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
)
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
)
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
)
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."
1619 (setf (epg-context-output-file context
)
1620 (or plain
(epg--make-temp-file "epg-output")))
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
)
1629 (epg-read-output context
)))
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
)
1656 (setf (epg-context-output-file context
)
1657 (epg--make-temp-file "epg-output"))
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
)
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
))
1693 (append (list (if (memq mode
'(t detached
))
1695 (if (memq mode
'(nil normal
))
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."
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
)))
1734 (list "Sign failed" (epg-errors-to-string errors
)))))
1736 (epg-read-output context
)))
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."
1747 (unless (or (eq (epg-context-protocol context
) 'CMS
)
1750 (epg-check-configuration (epg-configuration))
1753 (epg--make-temp-file "epg-input")))
1754 (coding-system-for-write 'binary
))
1757 (setf (epg-context-output-file context
)
1758 (epg--make-temp-file "epg-output"))
1760 (write-region plain nil input-file nil
'quiet
))
1761 (epg-start-sign context
1763 (epg-make-data-from-file input-file
)
1764 (epg-make-data-from-string plain
))
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
)))
1771 (list "Sign failed" (epg-errors-to-string errors
))))))
1772 (epg-read-output context
))
1773 (epg-delete-output-file context
)
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
)
1792 (append (if always-trust
'("--always-trust"))
1793 (if recipients
'("--encrypt") '("--symmetric"))
1794 (if sign
'("--sign"))
1801 (car (epg-key-sub-key-list
1803 (epg-context-signers context
))))
1805 (epg--args-from-sig-notations
1806 (epg-context-sig-notations context
)))
1812 (car (epg-key-sub-key-list recipient
)))))
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."
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
)))
1841 (not (epg-context-result-for context
'sign
)))
1843 (list "Sign failed" (epg-errors-to-string errors
))))
1846 (list "Encrypt failed" (epg-errors-to-string errors
)))))
1848 (epg-read-output context
)))
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."
1858 (unless (or (not sign
)
1859 (eq (epg-context-protocol context
) 'CMS
)
1862 (epg-check-configuration (epg-configuration))
1865 (epg--make-temp-file "epg-input")))
1866 (coding-system-for-write 'binary
))
1869 (setf (epg-context-output-file context
)
1870 (epg--make-temp-file "epg-output"))
1872 (write-region plain nil input-file nil
'quiet
))
1873 (epg-start-encrypt context
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
)))
1881 (not (epg-context-result-for context
'sign
)))
1883 (list "Sign failed" (epg-errors-to-string errors
))))
1886 (list "Encrypt failed" (epg-errors-to-string errors
)))))
1887 (epg-read-output context
))
1888 (epg-delete-output-file context
)
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"
1907 (car (epg-key-sub-key-list key
))))
1910 (defun epg-export-keys-to-file (context keys file
)
1911 "Extract public KEYS."
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
)))
1921 (list "Export keys failed"
1922 (epg-errors-to-string errors
)))))
1924 (epg-read-output context
)))
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
))
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
)
1957 (epg-start-import-keys context keys
)
1958 (epg-wait-for-completion context
)
1959 (let ((errors (epg-context-result-for context
'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"
1992 (epg-start-receive-keys context keys
)
1993 (epg-wait-for-completion context
)
1994 (let ((errors (epg-context-result-for context
'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"
2019 (car (epg-key-sub-key-list key
))))
2022 (defun epg-delete-keys (context keys
&optional allow-secret
)
2023 "Delete KEYS from the key ring."
2026 (epg-start-delete-keys context keys allow-secret
)
2027 (epg-wait-for-completion context
)
2028 (let ((errors (epg-context-result-for context
'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
2052 (car (epg-key-sub-key-list key
))))
2055 (defun epg-sign-keys (context keys
&optional local
)
2056 "Sign KEYS from the key ring."
2057 (declare (obsolete nil
"23.1"))
2060 (epg-start-sign-keys context keys local
)
2061 (epg-wait-for-completion context
)
2062 (let ((errors (epg-context-result-for context
'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."
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
)))
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."
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
)))
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
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"
2137 (car (epg-key-sub-key-list key
))))))
2139 (defun epg-edit-key (context key edit-callback handback
)
2140 "Edit KEY in the keyring."
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
)))
2148 (list "Edit key failed"
2149 (epg-errors-to-string errors
))))))
2150 (epg-reset context
)))
2152 (defun epg--decode-percent-escape (string)
2154 (while (string-match "%\\(\\(%\\)\\|\\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
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))
2162 index
(- (match-end 0) 2))))
2165 (defun epg--decode-hexstring (string)
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))
2171 index
(1- (match-end 0))))
2174 (defun epg--decode-quotedstring (string)
2176 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2177 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
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))
2186 index
(- (match-end 0) 2)))))
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."
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]*"
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]*"
2205 (setq type
(match-string 1 string
)
2206 index
(match-end 0))))
2208 (error "Invalid type"))
2209 (if (eq index
(string-match
2210 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
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 "\"\\([^\\\"]\\|\\\\.\\)*\""
2219 (setq index
(match-end 0)
2220 value
(epg--decode-quotedstring
2221 (match-string 0 string
))))))
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
)
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)) ?
+))))
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'."
2241 (if (stringp (car rdn
))
2242 (let ((entry (assoc (car rdn
) epg-dn-type-alist
)))
2244 (format "%s=%s" (cdr entry
) (cdr rdn
))
2245 (format "%s=%s" (car rdn
) (cdr rdn
))))
2246 (concat "(" (epg-decode-dn rdn
) ")")))
2252 ;;; epg.el ends here