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))
614 (re-search-forward "^allow-emacs-pinentry:.*:1$" nil t
))))
616 (setq process-environment
617 (cons (format "INSIDE_EMACS=%s,epg" emacs-version
)
618 process-environment
))
619 ;; Record modified time of gpg-agent socket to restore the Emacs
620 ;; frame on text terminal in `epg-wait-for-completion'.
622 ;; <http://lists.gnu.org/archive/html/emacs-devel/2007-02/msg00755.html>
624 (when (and agent-info
(string-match "\\(.*\\):[0-9]+:[0-9]+" agent-info
))
625 (setq agent-file
(match-string 1 agent-info
)
626 agent-mtime
(or (nth 5 (file-attributes agent-file
)) '(0 0 0 0))))
629 (unless epg-debug-buffer
630 (setq epg-debug-buffer
(generate-new-buffer " *epg-debug*")))
631 (set-buffer epg-debug-buffer
)
632 (goto-char (point-max))
633 (insert (if agent-info
634 (format "GPG_AGENT_INFO=%s\n" agent-info
)
635 "GPG_AGENT_INFO is not set\n")
637 (epg-context-program context
)
638 (mapconcat #'identity args
" ")))))
639 (with-current-buffer buffer
640 (if (fboundp 'set-buffer-multibyte
)
641 (set-buffer-multibyte nil
))
642 (make-local-variable 'epg-last-status
)
643 (setq epg-last-status nil
)
644 (make-local-variable 'epg-read-point
)
645 (setq epg-read-point
(point-min))
646 (make-local-variable 'epg-process-filter-running
)
647 (setq epg-process-filter-running nil
)
648 (make-local-variable 'epg-pending-status-list
)
649 (setq epg-pending-status-list nil
)
650 (make-local-variable 'epg-key-id
)
651 (setq epg-key-id nil
)
652 (make-local-variable 'epg-context
)
653 (setq epg-context context
)
654 (make-local-variable 'epg-agent-file
)
655 (setq epg-agent-file agent-file
)
656 (make-local-variable 'epg-agent-mtime
)
657 (setq epg-agent-mtime agent-mtime
))
659 (make-pipe-process :name
"epg-error"
660 :buffer
(generate-new-buffer " *epg-error*")
661 ;; Suppress "XXX finished" line.
664 (setf (epg-context-error-buffer context
) (process-buffer error-process
))
666 (setq process
(make-process :name
"epg"
668 :command
(cons (epg-context-program context
)
670 :connection-type
'pipe
671 :coding
'(binary . binary
)
672 :filter
#'epg--process-filter
673 :stderr error-process
675 (setf (epg-context-process context
) process
)))
677 (defun epg--process-filter (process input
)
681 (setq epg-debug-buffer
(generate-new-buffer " *epg-debug*")))
682 (goto-char (point-max))
684 (if (buffer-live-p (process-buffer process
))
685 (with-current-buffer (process-buffer process
)
687 (goto-char (point-max))
689 (unless epg-process-filter-running
690 (let ((epg-process-filter-running t
))
691 (goto-char epg-read-point
)
693 (while (looking-at ".*\n") ;the input line finished
694 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
695 (let ((status (match-string 1))
696 (string (match-string 2))
698 (if (member status epg-pending-status-list
)
699 (setq epg-pending-status-list nil
))
700 ;; When editing a key, delegate all interaction
702 (if (eq (epg-context-operation epg-context
) 'edit-key
)
703 (funcall (car (epg-context-edit-callback
708 (cdr (epg-context-edit-callback
710 ;; Otherwise call epg--status-STATUS function.
711 (setq symbol
(intern-soft (concat "epg--status-"
715 (funcall symbol epg-context string
)))
716 (setq epg-last-status
(cons status string
))))
718 (setq epg-read-point
(point)))))))))
720 (defun epg-read-output (context)
721 "Read the output file CONTEXT and return the content as a string."
723 (if (fboundp 'set-buffer-multibyte
)
724 (set-buffer-multibyte nil
))
725 (if (file-exists-p (epg-context-output-file context
))
726 (let ((coding-system-for-read 'binary
))
727 (insert-file-contents (epg-context-output-file context
))
730 (defun epg-wait-for-status (context status-list
)
731 "Wait until one of elements in STATUS-LIST arrives."
732 (with-current-buffer (process-buffer (epg-context-process context
))
733 (setq epg-pending-status-list status-list
)
734 (while (and (eq (process-status (epg-context-process context
)) 'run
)
735 epg-pending-status-list
)
736 (accept-process-output (epg-context-process context
) 1))
737 (if epg-pending-status-list
738 (epg-context-set-result-for
741 (epg-context-result-for context
'error
))))))
743 (defun epg-wait-for-completion (context)
744 "Wait until the `epg-gpg-program' process completes."
745 (while (eq (process-status (epg-context-process context
)) 'run
)
746 (accept-process-output (epg-context-process context
) 1))
747 ;; This line is needed to run the process-filter right now.
749 ;; Restore Emacs frame on text terminal, when pinentry-curses has terminated.
750 (if (with-current-buffer (process-buffer (epg-context-process context
))
752 (> (float-time (or (nth 5 (file-attributes epg-agent-file
))
754 (float-time epg-agent-mtime
))))
756 (epg-context-set-result-for
758 (nreverse (epg-context-result-for context
'error
)))
759 (setf (epg-context-error-output context
)
760 (with-current-buffer (epg-context-error-buffer context
)
763 (defun epg-reset (context)
765 (if (and (epg-context-process context
)
766 (buffer-live-p (process-buffer (epg-context-process context
))))
767 (kill-buffer (process-buffer (epg-context-process context
))))
768 (if (buffer-live-p (epg-context-error-buffer context
))
769 (kill-buffer (epg-context-error-buffer context
)))
770 (setf (epg-context-process context
) nil
)
771 (setf (epg-context-edit-callback context
) nil
))
773 (defun epg-delete-output-file (context)
774 "Delete the output file of CONTEXT."
775 (if (and (epg-context-output-file context
)
776 (file-exists-p (epg-context-output-file context
)))
777 (delete-file (epg-context-output-file context
))))
780 (if (fboundp 'decode-coding-string
)
781 (defalias 'epg--decode-coding-string
'decode-coding-string
)
782 (defalias 'epg--decode-coding-string
'identity
)))
784 (defun epg--status-USERID_HINT (_context string
)
785 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string
)
786 (let* ((key-id (match-string 1 string
))
787 (user-id (match-string 2 string
))
788 (entry (assoc key-id epg-user-id-alist
)))
790 (setq user-id
(epg--decode-coding-string
791 (epg--decode-percent-escape user-id
)
795 (setcdr entry user-id
)
796 (setq epg-user-id-alist
(cons (cons key-id user-id
)
797 epg-user-id-alist
))))))
799 (defun epg--status-NEED_PASSPHRASE (_context string
)
800 (if (string-match "\\`\\([^ ]+\\)" string
)
801 (setq epg-key-id
(match-string 1 string
))))
803 (defun epg--status-NEED_PASSPHRASE_SYM (_context _string
)
804 (setq epg-key-id
'SYM
))
806 (defun epg--status-NEED_PASSPHRASE_PIN (_context _string
)
807 (setq epg-key-id
'PIN
))
810 (if (fboundp 'clear-string
)
811 (defalias 'epg--clear-string
'clear-string
)
812 (defun epg--clear-string (string)
813 (fillarray string
0))))
816 (if (fboundp 'encode-coding-string
)
817 (defalias 'epg--encode-coding-string
'encode-coding-string
)
818 (defalias 'epg--encode-coding-string
'identity
)))
820 (defun epg--status-GET_HIDDEN (context string
)
821 (when (and epg-key-id
822 (string-match "\\`passphrase\\." string
))
823 (unless (epg-context-passphrase-callback context
)
824 (error "passphrase-callback not set"))
827 passphrase-with-new-line
828 encoded-passphrase-with-new-line
)
834 (car (epg-context-passphrase-callback context
))
837 (cdr (epg-context-passphrase-callback context
))))
839 (setq passphrase-with-new-line
(concat passphrase
"\n"))
840 (epg--clear-string passphrase
)
841 (setq passphrase nil
)
842 (if epg-passphrase-coding-system
844 (setq encoded-passphrase-with-new-line
845 (epg--encode-coding-string
846 passphrase-with-new-line
847 (coding-system-change-eol-conversion
848 epg-passphrase-coding-system
'unix
)))
849 (epg--clear-string passphrase-with-new-line
)
850 (setq passphrase-with-new-line nil
))
851 (setq encoded-passphrase-with-new-line
852 passphrase-with-new-line
853 passphrase-with-new-line nil
))
854 (process-send-string (epg-context-process context
)
855 encoded-passphrase-with-new-line
)))
857 (epg-context-set-result-for
860 (epg-context-result-for context
'error
)))
861 (delete-process (epg-context-process context
))))
863 (epg--clear-string passphrase
))
864 (if passphrase-with-new-line
865 (epg--clear-string passphrase-with-new-line
))
866 (if encoded-passphrase-with-new-line
867 (epg--clear-string encoded-passphrase-with-new-line
))))))
869 (defun epg--prompt-GET_BOOL (_context string
)
870 (let ((entry (assoc string epg-prompt-alist
)))
871 (y-or-n-p (if entry
(cdr entry
) (concat string
"? ")))))
873 (defun epg--prompt-GET_BOOL-untrusted_key.override
(_context _string
)
874 (y-or-n-p (if (and (equal (car epg-last-status
) "USERID_HINT")
875 (string-match "\\`\\([^ ]+\\) \\(.*\\)"
876 (cdr epg-last-status
)))
877 (let* ((key-id (match-string 1 (cdr epg-last-status
)))
878 (user-id (match-string 2 (cdr epg-last-status
)))
879 (entry (assoc key-id epg-user-id-alist
)))
881 (setq user-id
(cdr entry
)))
882 (format "Untrusted key %s %s. Use anyway? " key-id user-id
))
883 "Use untrusted key anyway? ")))
885 (defun epg--status-GET_BOOL (context string
)
888 (if (funcall (or (intern-soft (concat "epg--prompt-GET_BOOL-" string
))
889 #'epg--prompt-GET_BOOL
)
891 (process-send-string (epg-context-process context
) "y\n")
892 (process-send-string (epg-context-process context
) "n\n"))
894 (epg-context-set-result-for
897 (epg-context-result-for context
'error
)))
898 (delete-process (epg-context-process context
))))))
900 (defun epg--status-GET_LINE (context string
)
901 (let ((entry (assoc string epg-prompt-alist
))
904 (process-send-string (epg-context-process context
)
908 (concat string
": ")))
911 (epg-context-set-result-for
914 (epg-context-result-for context
'error
)))
915 (delete-process (epg-context-process context
))))))
917 (defun epg--status-*SIG
(context status string
)
918 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string
)
919 (let* ((key-id (match-string 1 string
))
920 (user-id (match-string 2 string
))
921 (entry (assoc key-id epg-user-id-alist
)))
922 (epg-context-set-result-for
925 (cons (epg-make-signature status key-id
)
926 (epg-context-result-for context
'verify
)))
928 (if (eq (epg-context-protocol context
) 'CMS
)
929 (setq user-id
(epg-dn-from-string user-id
))
930 (setq user-id
(epg--decode-coding-string
931 (epg--decode-percent-escape user-id
)
935 (setcdr entry user-id
)
936 (setq epg-user-id-alist
937 (cons (cons key-id user-id
) epg-user-id-alist
))))
938 (epg-context-set-result-for
941 (cons (epg-make-signature status
)
942 (epg-context-result-for context
'verify
)))))
944 (defun epg--status-GOODSIG (context string
)
945 (epg--status-*SIG context
'good string
))
947 (defun epg--status-EXPSIG (context string
)
948 (epg--status-*SIG context
'expired string
))
950 (defun epg--status-EXPKEYSIG (context string
)
951 (epg--status-*SIG context
'expired-key string
))
953 (defun epg--status-REVKEYSIG (context string
)
954 (epg--status-*SIG context
'revoked-key string
))
956 (defun epg--status-BADSIG (context string
)
957 (epg--status-*SIG context
'bad string
))
959 (defun epg--status-NO_PUBKEY (context string
)
960 (if (eq (epg-context-operation context
) 'verify
)
961 (let ((signature (car (epg-context-result-for context
'verify
))))
963 (eq (epg-signature-status signature
) 'error
)
964 (equal (epg-signature-key-id signature
) string
))
965 (setf (epg-signature-status signature
) 'no-pubkey
)))
966 (epg-context-set-result-for
968 (cons (cons 'no-pubkey string
)
969 (epg-context-result-for context
'error
)))))
971 (defun epg--status-NO_SECKEY (context string
)
972 (epg-context-set-result-for
974 (cons (cons 'no-seckey string
)
975 (epg-context-result-for context
'error
))))
977 (defun epg--time-from-seconds (seconds)
978 (let ((number-seconds (string-to-number (concat seconds
".0"))))
979 (cons (floor (/ number-seconds
65536))
980 (floor (mod number-seconds
65536)))))
982 (defun epg--status-ERRSIG (context string
)
983 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
984 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
986 (let ((signature (epg-make-signature 'error
)))
987 (epg-context-set-result-for
991 (epg-context-result-for context
'verify
)))
992 (setf (epg-signature-key-id signature
)
993 (match-string 1 string
))
994 (setf (epg-signature-pubkey-algorithm signature
)
995 (string-to-number (match-string 2 string
)))
996 (setf (epg-signature-digest-algorithm signature
)
997 (string-to-number (match-string 3 string
)))
998 (setf (epg-signature-class signature
)
999 (string-to-number (match-string 4 string
) 16))
1000 (setf (epg-signature-creation-time signature
)
1001 (epg--time-from-seconds (match-string 5 string
))))))
1003 (defun epg--status-VALIDSIG (context string
)
1004 (let ((signature (car (epg-context-result-for context
'verify
))))
1005 (when (and signature
1006 (eq (epg-signature-status signature
) 'good
)
1007 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
1008 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
1011 (setf (epg-signature-fingerprint signature
)
1012 (match-string 1 string
))
1013 (setf (epg-signature-creation-time signature
)
1014 (epg--time-from-seconds (match-string 2 string
)))
1015 (unless (equal (match-string 3 string
) "0")
1016 (setf (epg-signature-expiration-time signature
)
1017 (epg--time-from-seconds (match-string 3 string
))))
1018 (setf (epg-signature-version signature
)
1019 (string-to-number (match-string 4 string
)))
1020 (setf (epg-signature-pubkey-algorithm signature
)
1021 (string-to-number (match-string 5 string
)))
1022 (setf (epg-signature-digest-algorithm signature
)
1023 (string-to-number (match-string 6 string
)))
1024 (setf (epg-signature-class signature
)
1025 (string-to-number (match-string 7 string
) 16)))))
1027 (defun epg--status-TRUST_UNDEFINED (context _string
)
1028 (let ((signature (car (epg-context-result-for context
'verify
))))
1030 (eq (epg-signature-status signature
) 'good
))
1031 (setf (epg-signature-validity signature
) 'undefined
))))
1033 (defun epg--status-TRUST_NEVER (context _string
)
1034 (let ((signature (car (epg-context-result-for context
'verify
))))
1036 (eq (epg-signature-status signature
) 'good
))
1037 (setf (epg-signature-validity signature
) 'never
))))
1039 (defun epg--status-TRUST_MARGINAL (context _string
)
1040 (let ((signature (car (epg-context-result-for context
'verify
))))
1042 (eq (epg-signature-status signature
) 'marginal
))
1043 (setf (epg-signature-validity signature
) 'marginal
))))
1045 (defun epg--status-TRUST_FULLY (context _string
)
1046 (let ((signature (car (epg-context-result-for context
'verify
))))
1048 (eq (epg-signature-status signature
) 'good
))
1049 (setf (epg-signature-validity signature
) 'full
))))
1051 (defun epg--status-TRUST_ULTIMATE (context _string
)
1052 (let ((signature (car (epg-context-result-for context
'verify
))))
1054 (eq (epg-signature-status signature
) 'good
))
1055 (setf (epg-signature-validity signature
) 'ultimate
))))
1057 (defun epg--status-NOTATION_NAME (context string
)
1058 (let ((signature (car (epg-context-result-for context
'verify
))))
1060 (push (epg-make-sig-notation string nil t nil
)
1061 (epg-signature-notations signature
)))))
1063 (defun epg--status-NOTATION_DATA (context string
)
1064 (let ((signature (car (epg-context-result-for context
'verify
)))
1067 (setq notation
(car (epg-signature-notations signature
))))
1068 (setf (epg-sig-notation-value notation
) string
))))
1070 (defun epg--status-POLICY_URL (context string
)
1071 (let ((signature (car (epg-context-result-for context
'verify
))))
1073 (push (epg-make-sig-notation nil string t nil
)
1074 (epg-signature-notations signature
)))))
1076 (defun epg--status-PROGRESS (context string
)
1077 (if (and (epg-context-progress-callback context
)
1078 (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1080 (funcall (car (epg-context-progress-callback context
))
1082 (match-string 1 string
)
1083 (match-string 2 string
)
1084 (string-to-number (match-string 3 string
))
1085 (string-to-number (match-string 4 string
))
1086 (cdr (epg-context-progress-callback context
)))))
1088 (defun epg--status-ENC_TO (context string
)
1089 (if (string-match "\\`\\([0-9A-Za-z]+\\) \\([0-9]+\\) \\([0-9]+\\)" string
)
1090 (epg-context-set-result-for
1091 context
'encrypted-to
1092 (cons (list (match-string 1 string
)
1093 (string-to-number (match-string 2 string
))
1094 (string-to-number (match-string 3 string
)))
1095 (epg-context-result-for context
'encrypted-to
)))))
1097 (defun epg--status-DECRYPTION_FAILED (context _string
)
1098 (epg-context-set-result-for context
'decryption-failed t
))
1100 (defun epg--status-DECRYPTION_OKAY (context _string
)
1101 (epg-context-set-result-for context
'decryption-okay t
))
1103 (defun epg--status-NODATA (context string
)
1104 (epg-context-set-result-for
1106 (cons (cons 'no-data
(string-to-number string
))
1107 (epg-context-result-for context
'error
))))
1109 (defun epg--status-UNEXPECTED (context string
)
1110 (epg-context-set-result-for
1112 (cons (cons 'unexpected
(string-to-number string
))
1113 (epg-context-result-for context
'error
))))
1115 (defun epg--status-KEYEXPIRED (context string
)
1116 (epg-context-set-result-for
1118 (cons (list 'key-expired
(cons 'expiration-time
1119 (epg--time-from-seconds string
)))
1120 (epg-context-result-for context
'key
))))
1122 (defun epg--status-KEYREVOKED (context _string
)
1123 (epg-context-set-result-for
1125 (cons '(key-revoked)
1126 (epg-context-result-for context
'key
))))
1128 (defun epg--status-BADARMOR (context _string
)
1129 (epg-context-set-result-for
1132 (epg-context-result-for context
'error
))))
1134 (defun epg--status-INV_RECP (context string
)
1135 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string
)
1136 (epg-context-set-result-for
1138 (cons (list 'invalid-recipient
1140 (string-to-number (match-string 1 string
)))
1142 (match-string 2 string
)))
1143 (epg-context-result-for context
'error
)))))
1145 (defun epg--status-INV_SGNR (context string
)
1146 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string
)
1147 (epg-context-set-result-for
1149 (cons (list 'invalid-signer
1151 (string-to-number (match-string 1 string
)))
1153 (match-string 2 string
)))
1154 (epg-context-result-for context
'error
)))))
1156 (defun epg--status-NO_RECP (context _string
)
1157 (epg-context-set-result-for
1159 (cons '(no-recipients)
1160 (epg-context-result-for context
'error
))))
1162 (defun epg--status-NO_SGNR (context _string
)
1163 (epg-context-set-result-for
1166 (epg-context-result-for context
'error
))))
1168 (defun epg--status-DELETE_PROBLEM (context string
)
1169 (if (string-match "\\`\\([0-9]+\\)" string
)
1170 (epg-context-set-result-for
1172 (cons (cons 'delete-problem
1173 (string-to-number (match-string 1 string
)))
1174 (epg-context-result-for context
'error
)))))
1176 (defun epg--status-SIG_CREATED (context string
)
1177 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1178 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string
)
1179 (epg-context-set-result-for
1181 (cons (epg-make-new-signature
1182 (cdr (assq (aref (match-string 1 string
) 0)
1183 epg-new-signature-type-alist
))
1184 (string-to-number (match-string 2 string
))
1185 (string-to-number (match-string 3 string
))
1186 (string-to-number (match-string 4 string
) 16)
1187 (epg--time-from-seconds (match-string 5 string
))
1188 (substring string
(match-end 0)))
1189 (epg-context-result-for context
'sign
)))))
1191 (defun epg--status-KEY_CREATED (context string
)
1192 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string
)
1193 (epg-context-set-result-for
1194 context
'generate-key
1195 (cons (list (cons 'type
(string-to-char (match-string 1 string
)))
1196 (cons 'fingerprint
(match-string 2 string
)))
1197 (epg-context-result-for context
'generate-key
)))))
1199 (defun epg--status-KEY_NOT_CREATED (context _string
)
1200 (epg-context-set-result-for
1202 (cons '(key-not-created)
1203 (epg-context-result-for context
'error
))))
1205 (defun epg--status-IMPORTED (_context string
)
1206 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string
)
1207 (let* ((key-id (match-string 1 string
))
1208 (user-id (match-string 2 string
))
1209 (entry (assoc key-id epg-user-id-alist
)))
1211 (setq user-id
(epg--decode-coding-string
1212 (epg--decode-percent-escape user-id
)
1216 (setcdr entry user-id
)
1217 (setq epg-user-id-alist
(cons (cons key-id user-id
)
1218 epg-user-id-alist
))))))
1220 (defun epg--status-IMPORT_OK (context string
)
1221 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string
)
1222 (let ((reason (string-to-number (match-string 1 string
))))
1223 (epg-context-set-result-for
1224 context
'import-status
1225 (cons (epg-make-import-status (if (match-beginning 2)
1226 (match-string 3 string
))
1228 (/= (logand reason
1) 0)
1229 (/= (logand reason
2) 0)
1230 (/= (logand reason
4) 0)
1231 (/= (logand reason
8) 0)
1232 (/= (logand reason
16) 0))
1233 (epg-context-result-for context
'import-status
))))))
1235 (defun epg--status-IMPORT_PROBLEM (context string
)
1236 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string
)
1237 (epg-context-set-result-for
1238 context
'import-status
1239 (cons (epg-make-import-status
1240 (if (match-beginning 2)
1241 (match-string 3 string
))
1242 (string-to-number (match-string 1 string
)))
1243 (epg-context-result-for context
'import-status
)))))
1245 (defun epg--status-IMPORT_RES (context string
)
1246 (when (string-match "\\`\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1247 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1248 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" string
)
1249 (epg-context-set-result-for
1251 (epg-make-import-result (string-to-number (match-string 1 string
))
1252 (string-to-number (match-string 2 string
))
1253 (string-to-number (match-string 3 string
))
1254 (string-to-number (match-string 4 string
))
1255 (string-to-number (match-string 5 string
))
1256 (string-to-number (match-string 6 string
))
1257 (string-to-number (match-string 7 string
))
1258 (string-to-number (match-string 8 string
))
1259 (string-to-number (match-string 9 string
))
1260 (string-to-number (match-string 10 string
))
1261 (string-to-number (match-string 11 string
))
1262 (string-to-number (match-string 12 string
))
1263 (string-to-number (match-string 13 string
))
1264 (epg-context-result-for context
'import-status
)))
1265 (epg-context-set-result-for context
'import-status nil
)))
1267 (defun epg-passphrase-callback-function (context key-id _handback
)
1268 (declare (obsolete epa-passphrase-callback-function
"23.1"))
1269 (if (eq key-id
'SYM
)
1270 (read-passwd "Passphrase for symmetric encryption: "
1271 (eq (epg-context-operation context
) 'encrypt
))
1273 (if (eq key-id
'PIN
)
1274 "Passphrase for PIN: "
1275 (let ((entry (assoc key-id epg-user-id-alist
)))
1277 (format "Passphrase for %s %s: " key-id
(cdr entry
))
1278 (format "Passphrase for %s: " key-id
)))))))
1280 (defun epg--list-keys-1 (context name mode
)
1281 (let ((args (append (if (epg-context-home-directory context
)
1283 (epg-context-home-directory context
)))
1284 '("--with-colons" "--no-greeting" "--batch"
1285 "--with-fingerprint" "--with-fingerprint")
1286 (unless (eq (epg-context-protocol context
) 'CMS
)
1287 '("--fixed-list-mode"))))
1288 (list-keys-option (if (memq mode
'(t secret
))
1289 "--list-secret-keys"
1290 (if (memq mode
'(nil public
))
1293 (coding-system-for-read 'binary
)
1294 keys string field index
)
1297 (unless (listp name
)
1298 (setq name
(list name
)))
1300 (setq args
(append args
(list list-keys-option
(car name
)))
1302 (setq args
(append args
(list list-keys-option
))))
1304 (apply #'call-process
1305 (epg-context-program context
)
1306 nil
(list t nil
) nil args
)
1307 (goto-char (point-min))
1308 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t
)
1309 (setq keys
(cons (make-vector 15 nil
) keys
)
1310 string
(match-string 0)
1313 (while (and (< field
(length (car keys
)))
1315 (string-match "\\([^:]+\\)?:" string index
)))
1316 (setq index
(match-end 0))
1317 (aset (car keys
) field
(match-string 1 string
))
1318 (setq field
(1+ field
))))
1321 (defun epg--make-sub-key-1 (line)
1324 (cdr (assq (string-to-char (aref line
1)) epg-key-validity-alist
)))
1326 (mapcar (lambda (char) (cdr (assq char epg-key-capability-alist
)))
1328 (member (aref line
0) '("sec" "ssb"))
1329 (string-to-number (aref line
3))
1330 (string-to-number (aref line
2))
1332 (epg--time-from-seconds (aref line
5))
1334 (epg--time-from-seconds (aref line
6)))))
1336 (defun epg-list-keys (context &optional name mode
)
1337 "Return a list of epg-key objects matched with NAME.
1338 If MODE is nil or 'public, only public keyring should be searched.
1339 If MODE is t or 'secret, only secret keyring should be searched.
1340 Otherwise, only public keyring should be searched and the key
1341 signatures should be included.
1342 NAME is either a string or a list of strings."
1343 (let ((lines (epg--list-keys-1 context name mode
))
1344 keys cert pointer pointer-1 index string
)
1347 ((member (aref (car lines
) 0) '("pub" "sec" "crt" "crs"))
1348 (setq cert
(member (aref (car lines
) 0) '("crt" "crs"))
1349 keys
(cons (epg-make-key
1350 (if (aref (car lines
) 8)
1351 (cdr (assq (string-to-char (aref (car lines
) 8))
1352 epg-key-validity-alist
))))
1354 (push (epg--make-sub-key-1 (car lines
))
1355 (epg-key-sub-key-list (car keys
))))
1356 ((member (aref (car lines
) 0) '("sub" "ssb"))
1357 (push (epg--make-sub-key-1 (car lines
))
1358 (epg-key-sub-key-list (car keys
))))
1359 ((equal (aref (car lines
) 0) "uid")
1360 ;; Decode the UID name as a backslash escaped UTF-8 string,
1361 ;; generated by GnuPG/GpgSM.
1362 (setq string
(copy-sequence (aref (car lines
) 9))
1364 (while (string-match "\"" string index
)
1365 (setq string
(replace-match "\\\"" t t string
)
1366 index
(1+ (match-end 0))))
1368 (setq string
(epg--decode-coding-string
1369 (car (read-from-string (concat "\"" string
"\"")))
1372 (setq string
(aref (car lines
) 9))))
1373 (push (epg-make-user-id
1374 (if (aref (car lines
) 1)
1375 (cdr (assq (string-to-char (aref (car lines
) 1))
1376 epg-key-validity-alist
)))
1379 (epg-dn-from-string string
)
1382 (epg-key-user-id-list (car keys
))))
1383 ((equal (aref (car lines
) 0) "fpr")
1384 (setf (epg-sub-key-fingerprint (car (epg-key-sub-key-list (car keys
))))
1385 (aref (car lines
) 9)))
1386 ((equal (aref (car lines
) 0) "sig")
1388 (epg-make-key-signature
1389 (if (aref (car lines
) 1)
1390 (cdr (assq (string-to-char (aref (car lines
) 1))
1391 epg-key-validity-alist
)))
1392 (string-to-number (aref (car lines
) 3))
1393 (aref (car lines
) 4)
1394 (epg--time-from-seconds (aref (car lines
) 5))
1395 (epg--time-from-seconds (aref (car lines
) 6))
1396 (aref (car lines
) 9)
1397 (string-to-number (aref (car lines
) 10) 16)
1398 (eq (aref (aref (car lines
) 10) 2) ?x
))
1399 (epg-user-id-signature-list
1400 (car (epg-key-user-id-list (car keys
)))))))
1401 (setq lines
(cdr lines
)))
1402 (setq keys
(nreverse keys
)
1405 (epg--gv-nreverse (epg-key-sub-key-list (car pointer
)))
1406 (setq pointer-1
(epg--gv-nreverse (epg-key-user-id-list (car pointer
))))
1408 (epg--gv-nreverse (epg-user-id-signature-list (car pointer-1
)))
1409 (setq pointer-1
(cdr pointer-1
)))
1410 (setq pointer
(cdr pointer
)))
1414 (if (fboundp 'make-temp-file
)
1415 (defalias 'epg--make-temp-file
'make-temp-file
)
1416 (defvar temporary-file-directory
)
1417 ;; stolen from poe.el.
1418 (defun epg--make-temp-file (prefix)
1419 "Create a temporary file.
1420 The returned file name (created by appending some random characters at the end
1421 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1422 is guaranteed to point to a newly created empty file.
1423 You can then use `write-region' to write new data into the file."
1424 (let ((orig-modes (default-file-modes))
1426 (setq prefix
(expand-file-name prefix
1427 (if (featurep 'xemacs
)
1429 temporary-file-directory
)))
1432 ;; First, create a temporary directory.
1433 (set-default-file-modes #o700
)
1434 (while (condition-case ()
1436 (setq tempdir
(make-temp-name
1438 (file-name-directory prefix
)
1440 ;; return nil or signal an error.
1441 (make-directory tempdir
))
1443 (file-already-exists t
)))
1444 ;; Second, create a temporary file in the tempdir.
1445 ;; There *is* a race condition between `make-temp-name'
1446 ;; and `write-region', but we don't care it since we are
1447 ;; in a private directory now.
1448 (setq tempfile
(make-temp-name (concat tempdir
"/EMU")))
1449 (write-region "" nil tempfile nil
'silent
)
1450 ;; Finally, make a hard-link from the tempfile.
1451 (while (condition-case ()
1453 (setq file
(make-temp-name prefix
))
1454 ;; return nil or signal an error.
1455 (add-name-to-file tempfile file
))
1457 (file-already-exists t
)))
1459 (set-default-file-modes orig-modes
)
1460 ;; Cleanup the tempfile.
1462 (file-exists-p tempfile
)
1463 (delete-file tempfile
))
1464 ;; Cleanup the tempdir.
1466 (file-directory-p tempdir
)
1467 (delete-directory tempdir
)))))))
1469 (defun epg--args-from-sig-notations (notations)
1473 (if (and (epg-sig-notation-name notation
)
1474 (not (epg-sig-notation-human-readable notation
)))
1475 (error "Unreadable"))
1476 (if (epg-sig-notation-name notation
)
1477 (list "--sig-notation"
1478 (if (epg-sig-notation-critical notation
)
1479 (concat "!" (epg-sig-notation-name notation
)
1480 "=" (epg-sig-notation-value notation
))
1481 (concat (epg-sig-notation-name notation
)
1482 "=" (epg-sig-notation-value notation
))))
1483 (list "--sig-policy-url"
1484 (if (epg-sig-notation-critical notation
)
1485 (concat "!" (epg-sig-notation-value notation
))
1486 (epg-sig-notation-value notation
)))))
1489 (defun epg-cancel (context)
1490 (if (buffer-live-p (process-buffer (epg-context-process context
)))
1491 (with-current-buffer (process-buffer (epg-context-process context
))
1492 (epg-context-set-result-for
1495 (epg-context-result-for epg-context
'error
)))))
1496 (if (eq (process-status (epg-context-process context
)) 'run
)
1497 (delete-process (epg-context-process context
))))
1499 (defun epg-start-decrypt (context cipher
)
1500 "Initiate a decrypt operation on CIPHER.
1501 CIPHER must be a file data object.
1503 If you use this function, you will need to wait for the completion of
1504 `epg-gpg-program' by using `epg-wait-for-completion' and call
1505 `epg-reset' to clear a temporary output file.
1506 If you are unsure, use synchronous version of this function
1507 `epg-decrypt-file' or `epg-decrypt-string' instead."
1508 (unless (epg-data-file cipher
)
1509 (error "Not a file"))
1510 (setf (epg-context-operation context
) 'decrypt
)
1511 (setf (epg-context-result context
) nil
)
1512 (epg--start context
(list "--decrypt" "--" (epg-data-file cipher
)))
1513 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1514 (unless (eq (epg-context-protocol context
) 'CMS
)
1515 (epg-wait-for-status context
'("BEGIN_DECRYPTION"))))
1517 (defun epg--check-error-for-decrypt (context)
1518 (let ((errors (epg-context-result-for context
'error
)))
1519 (if (epg-context-result-for context
'decryption-failed
)
1521 (list "Decryption failed" (epg-errors-to-string errors
))))
1522 (unless (epg-context-result-for context
'decryption-okay
)
1524 (list "Can't decrypt" (epg-errors-to-string errors
))))))
1526 (defun epg-decrypt-file (context cipher plain
)
1527 "Decrypt a file CIPHER and store the result to a file PLAIN.
1528 If PLAIN is nil, it returns the result as a string."
1531 (setf (epg-context-output-file context
)
1532 (or plain
(epg--make-temp-file "epg-output")))
1533 (epg-start-decrypt context
(epg-make-data-from-file cipher
))
1534 (epg-wait-for-completion context
)
1535 (epg--check-error-for-decrypt context
)
1537 (epg-read-output context
)))
1539 (epg-delete-output-file context
))
1540 (epg-reset context
)))
1542 (defun epg-decrypt-string (context cipher
)
1543 "Decrypt a string CIPHER and return the plain text."
1544 (let ((input-file (epg--make-temp-file "epg-input"))
1545 (coding-system-for-write 'binary
))
1548 (write-region cipher nil input-file nil
'quiet
)
1549 (setf (epg-context-output-file context
)
1550 (epg--make-temp-file "epg-output"))
1551 (epg-start-decrypt context
(epg-make-data-from-file input-file
))
1552 (epg-wait-for-completion context
)
1553 (epg--check-error-for-decrypt context
)
1554 (epg-read-output context
))
1555 (epg-delete-output-file context
)
1556 (if (file-exists-p input-file
)
1557 (delete-file input-file
))
1558 (epg-reset context
))))
1560 (defun epg-start-verify (context signature
&optional signed-text
)
1561 "Initiate a verify operation on SIGNATURE.
1562 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1564 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1565 For a normal or a cleartext signature, SIGNED-TEXT should be nil.
1567 If you use this function, you will need to wait for the completion of
1568 `epg-gpg-program' by using `epg-wait-for-completion' and call
1569 `epg-reset' to clear a temporary output file.
1570 If you are unsure, use synchronous version of this function
1571 `epg-verify-file' or `epg-verify-string' instead."
1572 (setf (epg-context-operation context
) 'verify
)
1573 (setf (epg-context-result context
) nil
)
1575 ;; Detached signature.
1576 (if (epg-data-file signed-text
)
1577 (epg--start context
(list "--verify" "--" (epg-data-file signature
)
1578 (epg-data-file signed-text
)))
1579 (epg--start context
(list "--verify" "--" (epg-data-file signature
)
1581 (if (eq (process-status (epg-context-process context
)) 'run
)
1582 (process-send-string (epg-context-process context
)
1583 (epg-data-string signed-text
)))
1584 (if (eq (process-status (epg-context-process context
)) 'run
)
1585 (process-send-eof (epg-context-process context
))))
1586 ;; Normal (or cleartext) signature.
1587 (if (epg-data-file signature
)
1588 (epg--start context
(if (eq (epg-context-protocol context
) 'CMS
)
1589 (list "--verify" "--" (epg-data-file signature
))
1590 (list "--" (epg-data-file signature
))))
1591 (epg--start context
(if (eq (epg-context-protocol context
) 'CMS
)
1594 (if (eq (process-status (epg-context-process context
)) 'run
)
1595 (process-send-string (epg-context-process context
)
1596 (epg-data-string signature
)))
1597 (if (eq (process-status (epg-context-process context
)) 'run
)
1598 (process-send-eof (epg-context-process context
))))))
1600 (defun epg-verify-file (context signature
&optional signed-text plain
)
1601 "Verify a file SIGNATURE.
1602 SIGNED-TEXT and PLAIN are also a file if they are specified.
1604 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1605 string. For a normal or a cleartext signature, SIGNED-TEXT should be
1606 nil. In the latter case, if PLAIN is specified, the plaintext is
1607 stored into the file after successful verification.
1609 Note that this function does not return verification result as t
1610 or nil, nor signal error on failure. That's a design decision to
1611 handle the case where SIGNATURE has multiple signature.
1613 To check the verification results, use `epg-context-result-for' as follows:
1615 \(epg-context-result-for context 'verify)
1617 which will return a list of `epg-signature' object."
1620 (setf (epg-context-output-file context
)
1621 (or plain
(epg--make-temp-file "epg-output")))
1623 (epg-start-verify context
1624 (epg-make-data-from-file signature
)
1625 (epg-make-data-from-file signed-text
))
1626 (epg-start-verify context
1627 (epg-make-data-from-file signature
)))
1628 (epg-wait-for-completion context
)
1630 (epg-read-output context
)))
1632 (epg-delete-output-file context
))
1633 (epg-reset context
)))
1635 (defun epg-verify-string (context signature
&optional signed-text
)
1636 "Verify a string SIGNATURE.
1637 SIGNED-TEXT is a string if it is specified.
1639 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1640 string. For a normal or a cleartext signature, SIGNED-TEXT should be
1641 nil. In the latter case, this function returns the plaintext after
1642 successful verification.
1644 Note that this function does not return verification result as t
1645 or nil, nor signal error on failure. That's a design decision to
1646 handle the case where SIGNATURE has multiple signature.
1648 To check the verification results, use `epg-context-result-for' as follows:
1650 \(epg-context-result-for context 'verify)
1652 which will return a list of `epg-signature' object."
1653 (let ((coding-system-for-write 'binary
)
1657 (setf (epg-context-output-file context
)
1658 (epg--make-temp-file "epg-output"))
1661 (setq input-file
(epg--make-temp-file "epg-signature"))
1662 (write-region signature nil input-file nil
'quiet
)
1663 (epg-start-verify context
1664 (epg-make-data-from-file input-file
)
1665 (epg-make-data-from-string signed-text
)))
1666 (epg-start-verify context
(epg-make-data-from-string signature
)))
1667 (epg-wait-for-completion context
)
1668 (epg-read-output context
))
1669 (epg-delete-output-file context
)
1671 (file-exists-p input-file
))
1672 (delete-file input-file
))
1673 (epg-reset context
))))
1675 (defun epg-start-sign (context plain
&optional mode
)
1676 "Initiate a sign operation on PLAIN.
1677 PLAIN is a data object.
1679 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1680 If it is nil or 'normal, it makes a normal signature.
1681 Otherwise, it makes a cleartext signature.
1683 If you use this function, you will need to wait for the completion of
1684 `epg-gpg-program' by using `epg-wait-for-completion' and call
1685 `epg-reset' to clear a temporary output file.
1686 If you are unsure, use synchronous version of this function
1687 `epg-sign-file' or `epg-sign-string' instead."
1688 (setf (epg-context-operation context
) 'sign
)
1689 (setf (epg-context-result context
) nil
)
1690 (unless (memq mode
'(t detached nil normal
)) ;i.e. cleartext
1691 (epg-context-set-armor context nil
)
1692 (epg-context-set-textmode context nil
))
1694 (append (list (if (memq mode
'(t detached
))
1696 (if (memq mode
'(nil normal
))
1704 (car (epg-key-sub-key-list signer
)))))
1705 (epg-context-signers context
)))
1706 (epg--args-from-sig-notations
1707 (epg-context-sig-notations context
))
1708 (if (epg-data-file plain
)
1709 (list "--" (epg-data-file plain
)))))
1710 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1711 (unless (eq (epg-context-protocol context
) 'CMS
)
1712 (epg-wait-for-status context
'("BEGIN_SIGNING")))
1713 (when (epg-data-string plain
)
1714 (if (eq (process-status (epg-context-process context
)) 'run
)
1715 (process-send-string (epg-context-process context
)
1716 (epg-data-string plain
)))
1717 (if (eq (process-status (epg-context-process context
)) 'run
)
1718 (process-send-eof (epg-context-process context
)))))
1720 (defun epg-sign-file (context plain signature
&optional mode
)
1721 "Sign a file PLAIN and store the result to a file SIGNATURE.
1722 If SIGNATURE is nil, it returns the result as a string.
1723 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1724 If it is nil or 'normal, it makes a normal signature.
1725 Otherwise, it makes a cleartext signature."
1728 (setf (epg-context-output-file context
)
1729 (or signature
(epg--make-temp-file "epg-output")))
1730 (epg-start-sign context
(epg-make-data-from-file plain
) mode
)
1731 (epg-wait-for-completion context
)
1732 (unless (epg-context-result-for context
'sign
)
1733 (let ((errors (epg-context-result-for context
'error
)))
1735 (list "Sign failed" (epg-errors-to-string errors
)))))
1737 (epg-read-output context
)))
1739 (epg-delete-output-file context
))
1740 (epg-reset context
)))
1742 (defun epg-sign-string (context plain
&optional mode
)
1743 "Sign a string PLAIN and return the output as string.
1744 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1745 If it is nil or 'normal, it makes a normal signature.
1746 Otherwise, it makes a cleartext signature."
1748 (unless (or (eq (epg-context-protocol context
) 'CMS
)
1751 (epg-check-configuration (epg-configuration))
1754 (epg--make-temp-file "epg-input")))
1755 (coding-system-for-write 'binary
))
1758 (setf (epg-context-output-file context
)
1759 (epg--make-temp-file "epg-output"))
1761 (write-region plain nil input-file nil
'quiet
))
1762 (epg-start-sign context
1764 (epg-make-data-from-file input-file
)
1765 (epg-make-data-from-string plain
))
1767 (epg-wait-for-completion context
)
1768 (unless (epg-context-result-for context
'sign
)
1769 (if (epg-context-result-for context
'error
)
1770 (let ((errors (epg-context-result-for context
'error
)))
1772 (list "Sign failed" (epg-errors-to-string errors
))))))
1773 (epg-read-output context
))
1774 (epg-delete-output-file context
)
1776 (delete-file input-file
))
1777 (epg-reset context
))))
1779 (defun epg-start-encrypt (context plain recipients
1780 &optional sign always-trust
)
1781 "Initiate an encrypt operation on PLAIN.
1782 PLAIN is a data object.
1783 If RECIPIENTS is nil, it performs symmetric encryption.
1785 If you use this function, you will need to wait for the completion of
1786 `epg-gpg-program' by using `epg-wait-for-completion' and call
1787 `epg-reset' to clear a temporary output file.
1788 If you are unsure, use synchronous version of this function
1789 `epg-encrypt-file' or `epg-encrypt-string' instead."
1790 (setf (epg-context-operation context
) 'encrypt
)
1791 (setf (epg-context-result context
) nil
)
1793 (append (if always-trust
'("--always-trust"))
1794 (if recipients
'("--encrypt") '("--symmetric"))
1795 (if sign
'("--sign"))
1802 (car (epg-key-sub-key-list
1804 (epg-context-signers context
))))
1806 (epg--args-from-sig-notations
1807 (epg-context-sig-notations context
)))
1813 (car (epg-key-sub-key-list recipient
)))))
1815 (if (epg-data-file plain
)
1816 (list "--" (epg-data-file plain
)))))
1817 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1818 (unless (eq (epg-context-protocol context
) 'CMS
)
1819 (epg-wait-for-status context
1820 (if sign
'("BEGIN_SIGNING") '("BEGIN_ENCRYPTION"))))
1821 (when (epg-data-string plain
)
1822 (if (eq (process-status (epg-context-process context
)) 'run
)
1823 (process-send-string (epg-context-process context
)
1824 (epg-data-string plain
)))
1825 (if (eq (process-status (epg-context-process context
)) 'run
)
1826 (process-send-eof (epg-context-process context
)))))
1828 (defun epg-encrypt-file (context plain recipients
1829 cipher
&optional sign always-trust
)
1830 "Encrypt a file PLAIN and store the result to a file CIPHER.
1831 If CIPHER is nil, it returns the result as a string.
1832 If RECIPIENTS is nil, it performs symmetric encryption."
1835 (setf (epg-context-output-file context
)
1836 (or cipher
(epg--make-temp-file "epg-output")))
1837 (epg-start-encrypt context
(epg-make-data-from-file plain
)
1838 recipients sign always-trust
)
1839 (epg-wait-for-completion context
)
1840 (let ((errors (epg-context-result-for context
'error
)))
1842 (not (epg-context-result-for context
'sign
)))
1844 (list "Sign failed" (epg-errors-to-string errors
))))
1847 (list "Encrypt failed" (epg-errors-to-string errors
)))))
1849 (epg-read-output context
)))
1851 (epg-delete-output-file context
))
1852 (epg-reset context
)))
1854 (defun epg-encrypt-string (context plain recipients
1855 &optional sign always-trust
)
1856 "Encrypt a string PLAIN.
1857 If RECIPIENTS is nil, it performs symmetric encryption."
1859 (unless (or (not sign
)
1860 (eq (epg-context-protocol context
) 'CMS
)
1863 (epg-check-configuration (epg-configuration))
1866 (epg--make-temp-file "epg-input")))
1867 (coding-system-for-write 'binary
))
1870 (setf (epg-context-output-file context
)
1871 (epg--make-temp-file "epg-output"))
1873 (write-region plain nil input-file nil
'quiet
))
1874 (epg-start-encrypt context
1876 (epg-make-data-from-file input-file
)
1877 (epg-make-data-from-string plain
))
1878 recipients sign always-trust
)
1879 (epg-wait-for-completion context
)
1880 (let ((errors (epg-context-result-for context
'error
)))
1882 (not (epg-context-result-for context
'sign
)))
1884 (list "Sign failed" (epg-errors-to-string errors
))))
1887 (list "Encrypt failed" (epg-errors-to-string errors
)))))
1888 (epg-read-output context
))
1889 (epg-delete-output-file context
)
1891 (delete-file input-file
))
1892 (epg-reset context
))))
1894 (defun epg-start-export-keys (context keys
)
1895 "Initiate an export keys operation.
1897 If you use this function, you will need to wait for the completion of
1898 `epg-gpg-program' by using `epg-wait-for-completion' and call
1899 `epg-reset' to clear a temporary output file.
1900 If you are unsure, use synchronous version of this function
1901 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1902 (setf (epg-context-operation context
) 'export-keys
)
1903 (setf (epg-context-result context
) nil
)
1904 (epg--start context
(cons "--export"
1908 (car (epg-key-sub-key-list key
))))
1911 (defun epg-export-keys-to-file (context keys file
)
1912 "Extract public KEYS."
1915 (setf (epg-context-output-file context
)
1916 (or file
(epg--make-temp-file "epg-output")))
1917 (epg-start-export-keys context keys
)
1918 (epg-wait-for-completion context
)
1919 (let ((errors (epg-context-result-for context
'error
)))
1922 (list "Export keys failed"
1923 (epg-errors-to-string errors
)))))
1925 (epg-read-output context
)))
1927 (epg-delete-output-file context
))
1928 (epg-reset context
)))
1930 (defun epg-export-keys-to-string (context keys
)
1931 "Extract public KEYS and return them as a string."
1932 (epg-export-keys-to-file context keys nil
))
1934 (defun epg-start-import-keys (context keys
)
1935 "Initiate an import keys operation.
1936 KEYS is a data object.
1938 If you use this function, you will need to wait for the completion of
1939 `epg-gpg-program' by using `epg-wait-for-completion' and call
1940 `epg-reset' to clear a temporary output file.
1941 If you are unsure, use synchronous version of this function
1942 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1943 (setf (epg-context-operation context
) 'import-keys
)
1944 (setf (epg-context-result context
) nil
)
1945 (epg--start context
(if (epg-data-file keys
)
1946 (list "--import" "--" (epg-data-file keys
))
1948 (when (epg-data-string keys
)
1949 (if (eq (process-status (epg-context-process context
)) 'run
)
1950 (process-send-string (epg-context-process context
)
1951 (epg-data-string keys
)))
1952 (if (eq (process-status (epg-context-process context
)) 'run
)
1953 (process-send-eof (epg-context-process context
)))))
1955 (defun epg--import-keys-1 (context keys
)
1958 (epg-start-import-keys context keys
)
1959 (epg-wait-for-completion context
)
1960 (let ((errors (epg-context-result-for context
'error
)))
1963 (list "Import keys failed"
1964 (epg-errors-to-string errors
))))))
1965 (epg-reset context
)))
1967 (defun epg-import-keys-from-file (context keys
)
1968 "Add keys from a file KEYS."
1969 (epg--import-keys-1 context
(epg-make-data-from-file keys
)))
1971 (defun epg-import-keys-from-string (context keys
)
1972 "Add keys from a string KEYS."
1973 (epg--import-keys-1 context
(epg-make-data-from-string keys
)))
1975 (defun epg-start-receive-keys (context key-id-list
)
1976 "Initiate a receive key operation.
1977 KEY-ID-LIST is a list of key IDs.
1979 If you use this function, you will need to wait for the completion of
1980 `epg-gpg-program' by using `epg-wait-for-completion' and call
1981 `epg-reset' to clear a temporary output file.
1982 If you are unsure, use synchronous version of this function
1983 `epg-receive-keys' instead."
1984 (setf (epg-context-operation context
) 'receive-keys
)
1985 (setf (epg-context-result context
) nil
)
1986 (epg--start context
(cons "--recv-keys" key-id-list
)))
1988 (defun epg-receive-keys (context keys
)
1989 "Add keys from server.
1990 KEYS is a list of key IDs"
1993 (epg-start-receive-keys context keys
)
1994 (epg-wait-for-completion context
)
1995 (let ((errors (epg-context-result-for context
'error
)))
1998 (list "Receive keys failed"
1999 (epg-errors-to-string errors
))))))
2000 (epg-reset context
)))
2002 (defalias 'epg-import-keys-from-server
'epg-receive-keys
)
2004 (defun epg-start-delete-keys (context keys
&optional allow-secret
)
2005 "Initiate a delete keys operation.
2007 If you use this function, you will need to wait for the completion of
2008 `epg-gpg-program' by using `epg-wait-for-completion' and call
2009 `epg-reset' to clear a temporary output file.
2010 If you are unsure, use synchronous version of this function
2011 `epg-delete-keys' instead."
2012 (setf (epg-context-operation context
) 'delete-keys
)
2013 (setf (epg-context-result context
) nil
)
2014 (epg--start context
(cons (if allow-secret
2015 "--delete-secret-key"
2020 (car (epg-key-sub-key-list key
))))
2023 (defun epg-delete-keys (context keys
&optional allow-secret
)
2024 "Delete KEYS from the key ring."
2027 (epg-start-delete-keys context keys allow-secret
)
2028 (epg-wait-for-completion context
)
2029 (let ((errors (epg-context-result-for context
'error
)))
2032 (list "Delete keys failed"
2033 (epg-errors-to-string errors
))))))
2034 (epg-reset context
)))
2036 (defun epg-start-sign-keys (context keys
&optional local
)
2037 "Initiate a sign keys operation.
2039 If you use this function, you will need to wait for the completion of
2040 `epg-gpg-program' by using `epg-wait-for-completion' and call
2041 `epg-reset' to clear a temporary output file.
2042 If you are unsure, use synchronous version of this function
2043 `epg-sign-keys' instead."
2044 (declare (obsolete nil
"23.1"))
2045 (setf (epg-context-operation context
) 'sign-keys
)
2046 (setf (epg-context-result context
) nil
)
2047 (epg--start context
(cons (if local
2053 (car (epg-key-sub-key-list key
))))
2056 (defun epg-sign-keys (context keys
&optional local
)
2057 "Sign KEYS from the key ring."
2058 (declare (obsolete nil
"23.1"))
2061 (epg-start-sign-keys context keys local
)
2062 (epg-wait-for-completion context
)
2063 (let ((errors (epg-context-result-for context
'error
)))
2066 (list "Sign keys failed"
2067 (epg-errors-to-string errors
))))))
2068 (epg-reset context
)))
2070 (defun epg-start-generate-key (context parameters
)
2071 "Initiate a key generation.
2072 PARAMETERS is a string which specifies parameters of the generated key.
2073 See Info node `(gnupg) Unattended GPG key generation' in the
2074 GnuPG manual for the format.
2076 If you use this function, you will need to wait for the completion of
2077 `epg-gpg-program' by using `epg-wait-for-completion' and call
2078 `epg-reset' to clear a temporary output file.
2079 If you are unsure, use synchronous version of this function
2080 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2081 (setf (epg-context-operation context
) 'generate-key
)
2082 (setf (epg-context-result context
) nil
)
2083 (if (epg-data-file parameters
)
2084 (epg--start context
(list "--batch" "--gen-key" "--"
2085 (epg-data-file parameters
)))
2086 (epg--start context
'("--batch" "--gen-key"))
2087 (if (eq (process-status (epg-context-process context
)) 'run
)
2088 (process-send-string (epg-context-process context
)
2089 (epg-data-string parameters
)))
2090 (if (eq (process-status (epg-context-process context
)) 'run
)
2091 (process-send-eof (epg-context-process context
)))))
2093 (defun epg-generate-key-from-file (context parameters
)
2094 "Generate a new key pair.
2095 PARAMETERS is a file which tells how to create the key."
2098 (epg-start-generate-key context
(epg-make-data-from-file parameters
))
2099 (epg-wait-for-completion context
)
2100 (let ((errors (epg-context-result-for context
'error
)))
2103 (list "Generate key failed"
2104 (epg-errors-to-string errors
))))))
2105 (epg-reset context
)))
2107 (defun epg-generate-key-from-string (context parameters
)
2108 "Generate a new key pair.
2109 PARAMETERS is a string which tells how to create the key."
2112 (epg-start-generate-key context
(epg-make-data-from-string parameters
))
2113 (epg-wait-for-completion context
)
2114 (let ((errors (epg-context-result-for context
'error
)))
2117 (list "Generate key failed"
2118 (epg-errors-to-string errors
))))))
2119 (epg-reset context
)))
2121 (defun epg-start-edit-key (context key edit-callback handback
)
2122 "Initiate an edit operation on KEY.
2124 EDIT-CALLBACK is called from process filter and takes 3
2125 arguments: the context, a status, an argument string, and the
2128 If you use this function, you will need to wait for the completion of
2129 `epg-gpg-program' by using `epg-wait-for-completion' and call
2130 `epg-reset' to clear a temporary output file.
2131 If you are unsure, use synchronous version of this function
2132 `epg-edit-key' instead."
2133 (setf (epg-context-operation context
) 'edit-key
)
2134 (setf (epg-context-result context
) nil
)
2135 (setf (epg-context-edit-callback context
) (cons edit-callback handback
))
2136 (epg--start context
(list "--edit-key"
2138 (car (epg-key-sub-key-list key
))))))
2140 (defun epg-edit-key (context key edit-callback handback
)
2141 "Edit KEY in the keyring."
2144 (epg-start-edit-key context key edit-callback handback
)
2145 (epg-wait-for-completion context
)
2146 (let ((errors (epg-context-result-for context
'error
)))
2149 (list "Edit key failed"
2150 (epg-errors-to-string errors
))))))
2151 (epg-reset context
)))
2153 (defun epg--decode-percent-escape (string)
2155 (while (string-match "%\\(\\(%\\)\\|\\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2157 (if (match-beginning 2)
2158 (setq string
(replace-match "%" t t string
)
2159 index
(1- (match-end 0)))
2160 (setq string
(replace-match
2161 (string (string-to-number (match-string 3 string
) 16))
2163 index
(- (match-end 0) 2))))
2166 (defun epg--decode-hexstring (string)
2168 (while (eq index
(string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index
))
2169 (setq string
(replace-match (string (string-to-number
2170 (match-string 0 string
) 16))
2172 index
(1- (match-end 0))))
2175 (defun epg--decode-quotedstring (string)
2177 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2178 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2180 (if (match-beginning 2)
2181 (setq string
(replace-match "\\2" t nil string
)
2182 index
(1- (match-end 0)))
2183 (if (match-beginning 3)
2184 (setq string
(replace-match (string (string-to-number
2185 (match-string 0 string
) 16))
2187 index
(- (match-end 0) 2)))))
2190 (defun epg-dn-from-string (string)
2191 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2192 The return value is an alist mapping from types to values."
2194 (length (length string
))
2195 alist type value group
)
2196 (while (< index length
)
2197 (if (eq index
(string-match "[ \t\n\r]*" string index
))
2198 (setq index
(match-end 0)))
2199 (if (eq index
(string-match
2200 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2202 (setq type
(match-string 1 string
)
2203 index
(match-end 0))
2204 (if (eq index
(string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2206 (setq type
(match-string 1 string
)
2207 index
(match-end 0))))
2209 (error "Invalid type"))
2210 (if (eq index
(string-match
2211 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2213 (setq index
(match-end 0)
2214 value
(epg--decode-quotedstring (match-string 0 string
)))
2215 (if (eq index
(string-match "#\\([0-9A-Fa-f]+\\)" string index
))
2216 (setq index
(match-end 0)
2217 value
(epg--decode-hexstring (match-string 1 string
)))
2218 (if (eq index
(string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2220 (setq index
(match-end 0)
2221 value
(epg--decode-quotedstring
2222 (match-string 0 string
))))))
2224 (if (stringp (car (car alist
)))
2225 (setcar alist
(list (cons type value
) (car alist
)))
2226 (setcar alist
(cons (cons type value
) (car alist
))))
2227 (if (consp (car (car alist
)))
2228 (setcar alist
(nreverse (car alist
))))
2229 (setq alist
(cons (cons type value
) alist
)
2232 (if (eq index
(string-match "[ \t\n\r]*\\([,;+]\\)" string index
))
2233 (setq index
(match-end 0)
2234 group
(eq (aref string
(match-beginning 1)) ?
+))))
2237 (defun epg-decode-dn (alist)
2238 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2239 Type names are resolved using `epg-dn-type-alist'."
2242 (if (stringp (car rdn
))
2243 (let ((entry (assoc (car rdn
) epg-dn-type-alist
)))
2245 (format "%s=%s" (cdr entry
) (cdr rdn
))
2246 (format "%s=%s" (car rdn
) (cdr rdn
))))
2247 (concat "(" (epg-decode-dn rdn
) ")")))
2253 ;;; epg.el ends here