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