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 (setq process-environment
606 (cons (format "INSIDE_EMACS=%s,epg" emacs-version
)
607 process-environment
))
608 ;; Record modified time of gpg-agent socket to restore the Emacs
609 ;; frame on text terminal in `epg-wait-for-completion'.
611 ;; <http://lists.gnu.org/archive/html/emacs-devel/2007-02/msg00755.html>
613 (when (and agent-info
(string-match "\\(.*\\):[0-9]+:[0-9]+" agent-info
))
614 (setq agent-file
(match-string 1 agent-info
)
615 agent-mtime
(or (nth 5 (file-attributes agent-file
)) '(0 0 0 0))))
618 (unless epg-debug-buffer
619 (setq epg-debug-buffer
(generate-new-buffer " *epg-debug*")))
620 (set-buffer epg-debug-buffer
)
621 (goto-char (point-max))
622 (insert (if agent-info
623 (format "GPG_AGENT_INFO=%s\n" agent-info
)
624 "GPG_AGENT_INFO is not set\n")
626 (epg-context-program context
)
627 (mapconcat #'identity args
" ")))))
628 (with-current-buffer buffer
629 (if (fboundp 'set-buffer-multibyte
)
630 (set-buffer-multibyte nil
))
631 (make-local-variable 'epg-last-status
)
632 (setq epg-last-status nil
)
633 (make-local-variable 'epg-read-point
)
634 (setq epg-read-point
(point-min))
635 (make-local-variable 'epg-process-filter-running
)
636 (setq epg-process-filter-running nil
)
637 (make-local-variable 'epg-pending-status-list
)
638 (setq epg-pending-status-list nil
)
639 (make-local-variable 'epg-key-id
)
640 (setq epg-key-id nil
)
641 (make-local-variable 'epg-context
)
642 (setq epg-context context
)
643 (make-local-variable 'epg-agent-file
)
644 (setq epg-agent-file agent-file
)
645 (make-local-variable 'epg-agent-mtime
)
646 (setq epg-agent-mtime agent-mtime
))
648 (make-pipe-process :name
"epg-error"
649 :buffer
(generate-new-buffer " *epg-error*")
650 ;; Suppress "XXX finished" line.
653 (setf (epg-context-error-buffer context
) (process-buffer error-process
))
655 (setq process
(make-process :name
"epg"
657 :command
(cons (epg-context-program context
)
659 :connection-type
'pipe
660 :coding
'(binary . binary
)
661 :filter
#'epg--process-filter
662 :stderr error-process
664 (setf (epg-context-process context
) process
)))
666 (defun epg--process-filter (process input
)
670 (setq epg-debug-buffer
(generate-new-buffer " *epg-debug*")))
671 (goto-char (point-max))
673 (if (buffer-live-p (process-buffer process
))
674 (with-current-buffer (process-buffer process
)
676 (goto-char (point-max))
678 (unless epg-process-filter-running
679 (let ((epg-process-filter-running t
))
680 (goto-char epg-read-point
)
682 (while (looking-at ".*\n") ;the input line finished
683 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
684 (let ((status (match-string 1))
685 (string (match-string 2))
687 (if (member status epg-pending-status-list
)
688 (setq epg-pending-status-list nil
))
689 ;; When editing a key, delegate all interaction
691 (if (eq (epg-context-operation epg-context
) 'edit-key
)
692 (funcall (car (epg-context-edit-callback
697 (cdr (epg-context-edit-callback
699 ;; Otherwise call epg--status-STATUS function.
700 (setq symbol
(intern-soft (concat "epg--status-"
704 (funcall symbol epg-context string
)))
705 (setq epg-last-status
(cons status string
))))
707 (setq epg-read-point
(point)))))))))
709 (defun epg-read-output (context)
710 "Read the output file CONTEXT and return the content as a string."
712 (if (fboundp 'set-buffer-multibyte
)
713 (set-buffer-multibyte nil
))
714 (if (file-exists-p (epg-context-output-file context
))
715 (let ((coding-system-for-read 'binary
))
716 (insert-file-contents (epg-context-output-file context
))
719 (defun epg-wait-for-status (context status-list
)
720 "Wait until one of elements in STATUS-LIST arrives."
721 (with-current-buffer (process-buffer (epg-context-process context
))
722 (setq epg-pending-status-list status-list
)
723 (while (and (eq (process-status (epg-context-process context
)) 'run
)
724 epg-pending-status-list
)
725 (accept-process-output (epg-context-process context
) 1))
726 (if epg-pending-status-list
727 (epg-context-set-result-for
730 (epg-context-result-for context
'error
))))))
732 (defun epg-wait-for-completion (context)
733 "Wait until the `epg-gpg-program' process completes."
734 (while (eq (process-status (epg-context-process context
)) 'run
)
735 (accept-process-output (epg-context-process context
) 1))
736 ;; This line is needed to run the process-filter right now.
738 ;; Restore Emacs frame on text terminal, when pinentry-curses has terminated.
739 (if (with-current-buffer (process-buffer (epg-context-process context
))
741 (> (float-time (or (nth 5 (file-attributes epg-agent-file
))
743 (float-time epg-agent-mtime
))))
745 (epg-context-set-result-for
747 (nreverse (epg-context-result-for context
'error
)))
748 (setf (epg-context-error-output context
)
749 (with-current-buffer (epg-context-error-buffer context
)
752 (defun epg-reset (context)
754 (if (and (epg-context-process context
)
755 (buffer-live-p (process-buffer (epg-context-process context
))))
756 (kill-buffer (process-buffer (epg-context-process context
))))
757 (if (buffer-live-p (epg-context-error-buffer context
))
758 (kill-buffer (epg-context-error-buffer context
)))
759 (setf (epg-context-process context
) nil
)
760 (setf (epg-context-edit-callback context
) nil
))
762 (defun epg-delete-output-file (context)
763 "Delete the output file of CONTEXT."
764 (if (and (epg-context-output-file context
)
765 (file-exists-p (epg-context-output-file context
)))
766 (delete-file (epg-context-output-file context
))))
769 (if (fboundp 'decode-coding-string
)
770 (defalias 'epg--decode-coding-string
'decode-coding-string
)
771 (defalias 'epg--decode-coding-string
'identity
)))
773 (defun epg--status-USERID_HINT (_context string
)
774 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string
)
775 (let* ((key-id (match-string 1 string
))
776 (user-id (match-string 2 string
))
777 (entry (assoc key-id epg-user-id-alist
)))
779 (setq user-id
(epg--decode-coding-string
780 (epg--decode-percent-escape user-id
)
784 (setcdr entry user-id
)
785 (setq epg-user-id-alist
(cons (cons key-id user-id
)
786 epg-user-id-alist
))))))
788 (defun epg--status-NEED_PASSPHRASE (_context string
)
789 (if (string-match "\\`\\([^ ]+\\)" string
)
790 (setq epg-key-id
(match-string 1 string
))))
792 (defun epg--status-NEED_PASSPHRASE_SYM (_context _string
)
793 (setq epg-key-id
'SYM
))
795 (defun epg--status-NEED_PASSPHRASE_PIN (_context _string
)
796 (setq epg-key-id
'PIN
))
799 (if (fboundp 'clear-string
)
800 (defalias 'epg--clear-string
'clear-string
)
801 (defun epg--clear-string (string)
802 (fillarray string
0))))
805 (if (fboundp 'encode-coding-string
)
806 (defalias 'epg--encode-coding-string
'encode-coding-string
)
807 (defalias 'epg--encode-coding-string
'identity
)))
809 (defun epg--status-GET_HIDDEN (context string
)
810 (when (and epg-key-id
811 (string-match "\\`passphrase\\." string
))
812 (unless (epg-context-passphrase-callback context
)
813 (error "passphrase-callback not set"))
816 passphrase-with-new-line
817 encoded-passphrase-with-new-line
)
823 (car (epg-context-passphrase-callback context
))
826 (cdr (epg-context-passphrase-callback context
))))
828 (setq passphrase-with-new-line
(concat passphrase
"\n"))
829 (epg--clear-string passphrase
)
830 (setq passphrase nil
)
831 (if epg-passphrase-coding-system
833 (setq encoded-passphrase-with-new-line
834 (epg--encode-coding-string
835 passphrase-with-new-line
836 (coding-system-change-eol-conversion
837 epg-passphrase-coding-system
'unix
)))
838 (epg--clear-string passphrase-with-new-line
)
839 (setq passphrase-with-new-line nil
))
840 (setq encoded-passphrase-with-new-line
841 passphrase-with-new-line
842 passphrase-with-new-line nil
))
843 (process-send-string (epg-context-process context
)
844 encoded-passphrase-with-new-line
)))
846 (epg-context-set-result-for
849 (epg-context-result-for context
'error
)))
850 (delete-process (epg-context-process context
))))
852 (epg--clear-string passphrase
))
853 (if passphrase-with-new-line
854 (epg--clear-string passphrase-with-new-line
))
855 (if encoded-passphrase-with-new-line
856 (epg--clear-string encoded-passphrase-with-new-line
))))))
858 (defun epg--prompt-GET_BOOL (_context string
)
859 (let ((entry (assoc string epg-prompt-alist
)))
860 (y-or-n-p (if entry
(cdr entry
) (concat string
"? ")))))
862 (defun epg--prompt-GET_BOOL-untrusted_key.override
(_context _string
)
863 (y-or-n-p (if (and (equal (car epg-last-status
) "USERID_HINT")
864 (string-match "\\`\\([^ ]+\\) \\(.*\\)"
865 (cdr epg-last-status
)))
866 (let* ((key-id (match-string 1 (cdr epg-last-status
)))
867 (user-id (match-string 2 (cdr epg-last-status
)))
868 (entry (assoc key-id epg-user-id-alist
)))
870 (setq user-id
(cdr entry
)))
871 (format "Untrusted key %s %s. Use anyway? " key-id user-id
))
872 "Use untrusted key anyway? ")))
874 (defun epg--status-GET_BOOL (context string
)
877 (if (funcall (or (intern-soft (concat "epg--prompt-GET_BOOL-" string
))
878 #'epg--prompt-GET_BOOL
)
880 (process-send-string (epg-context-process context
) "y\n")
881 (process-send-string (epg-context-process context
) "n\n"))
883 (epg-context-set-result-for
886 (epg-context-result-for context
'error
)))
887 (delete-process (epg-context-process context
))))))
889 (defun epg--status-GET_LINE (context string
)
890 (let ((entry (assoc string epg-prompt-alist
))
893 (process-send-string (epg-context-process context
)
897 (concat string
": ")))
900 (epg-context-set-result-for
903 (epg-context-result-for context
'error
)))
904 (delete-process (epg-context-process context
))))))
906 (defun epg--status-*SIG
(context status string
)
907 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string
)
908 (let* ((key-id (match-string 1 string
))
909 (user-id (match-string 2 string
))
910 (entry (assoc key-id epg-user-id-alist
)))
911 (epg-context-set-result-for
914 (cons (epg-make-signature status key-id
)
915 (epg-context-result-for context
'verify
)))
917 (if (eq (epg-context-protocol context
) 'CMS
)
918 (setq user-id
(epg-dn-from-string user-id
))
919 (setq user-id
(epg--decode-coding-string
920 (epg--decode-percent-escape user-id
)
924 (setcdr entry user-id
)
925 (setq epg-user-id-alist
926 (cons (cons key-id user-id
) epg-user-id-alist
))))
927 (epg-context-set-result-for
930 (cons (epg-make-signature status
)
931 (epg-context-result-for context
'verify
)))))
933 (defun epg--status-GOODSIG (context string
)
934 (epg--status-*SIG context
'good string
))
936 (defun epg--status-EXPSIG (context string
)
937 (epg--status-*SIG context
'expired string
))
939 (defun epg--status-EXPKEYSIG (context string
)
940 (epg--status-*SIG context
'expired-key string
))
942 (defun epg--status-REVKEYSIG (context string
)
943 (epg--status-*SIG context
'revoked-key string
))
945 (defun epg--status-BADSIG (context string
)
946 (epg--status-*SIG context
'bad string
))
948 (defun epg--status-NO_PUBKEY (context string
)
949 (if (eq (epg-context-operation context
) 'verify
)
950 (let ((signature (car (epg-context-result-for context
'verify
))))
952 (eq (epg-signature-status signature
) 'error
)
953 (equal (epg-signature-key-id signature
) string
))
954 (setf (epg-signature-status signature
) 'no-pubkey
)))
955 (epg-context-set-result-for
957 (cons (cons 'no-pubkey string
)
958 (epg-context-result-for context
'error
)))))
960 (defun epg--status-NO_SECKEY (context string
)
961 (epg-context-set-result-for
963 (cons (cons 'no-seckey string
)
964 (epg-context-result-for context
'error
))))
966 (defun epg--time-from-seconds (seconds)
967 (let ((number-seconds (string-to-number (concat seconds
".0"))))
968 (cons (floor (/ number-seconds
65536))
969 (floor (mod number-seconds
65536)))))
971 (defun epg--status-ERRSIG (context string
)
972 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
973 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
975 (let ((signature (epg-make-signature 'error
)))
976 (epg-context-set-result-for
980 (epg-context-result-for context
'verify
)))
981 (setf (epg-signature-key-id signature
)
982 (match-string 1 string
))
983 (setf (epg-signature-pubkey-algorithm signature
)
984 (string-to-number (match-string 2 string
)))
985 (setf (epg-signature-digest-algorithm signature
)
986 (string-to-number (match-string 3 string
)))
987 (setf (epg-signature-class signature
)
988 (string-to-number (match-string 4 string
) 16))
989 (setf (epg-signature-creation-time signature
)
990 (epg--time-from-seconds (match-string 5 string
))))))
992 (defun epg--status-VALIDSIG (context string
)
993 (let ((signature (car (epg-context-result-for context
'verify
))))
995 (eq (epg-signature-status signature
) 'good
)
996 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
997 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
1000 (setf (epg-signature-fingerprint signature
)
1001 (match-string 1 string
))
1002 (setf (epg-signature-creation-time signature
)
1003 (epg--time-from-seconds (match-string 2 string
)))
1004 (unless (equal (match-string 3 string
) "0")
1005 (setf (epg-signature-expiration-time signature
)
1006 (epg--time-from-seconds (match-string 3 string
))))
1007 (setf (epg-signature-version signature
)
1008 (string-to-number (match-string 4 string
)))
1009 (setf (epg-signature-pubkey-algorithm signature
)
1010 (string-to-number (match-string 5 string
)))
1011 (setf (epg-signature-digest-algorithm signature
)
1012 (string-to-number (match-string 6 string
)))
1013 (setf (epg-signature-class signature
)
1014 (string-to-number (match-string 7 string
) 16)))))
1016 (defun epg--status-TRUST_UNDEFINED (context _string
)
1017 (let ((signature (car (epg-context-result-for context
'verify
))))
1019 (eq (epg-signature-status signature
) 'good
))
1020 (setf (epg-signature-validity signature
) 'undefined
))))
1022 (defun epg--status-TRUST_NEVER (context _string
)
1023 (let ((signature (car (epg-context-result-for context
'verify
))))
1025 (eq (epg-signature-status signature
) 'good
))
1026 (setf (epg-signature-validity signature
) 'never
))))
1028 (defun epg--status-TRUST_MARGINAL (context _string
)
1029 (let ((signature (car (epg-context-result-for context
'verify
))))
1031 (eq (epg-signature-status signature
) 'marginal
))
1032 (setf (epg-signature-validity signature
) 'marginal
))))
1034 (defun epg--status-TRUST_FULLY (context _string
)
1035 (let ((signature (car (epg-context-result-for context
'verify
))))
1037 (eq (epg-signature-status signature
) 'good
))
1038 (setf (epg-signature-validity signature
) 'full
))))
1040 (defun epg--status-TRUST_ULTIMATE (context _string
)
1041 (let ((signature (car (epg-context-result-for context
'verify
))))
1043 (eq (epg-signature-status signature
) 'good
))
1044 (setf (epg-signature-validity signature
) 'ultimate
))))
1046 (defun epg--status-NOTATION_NAME (context string
)
1047 (let ((signature (car (epg-context-result-for context
'verify
))))
1049 (push (epg-make-sig-notation string nil t nil
)
1050 (epg-signature-notations signature
)))))
1052 (defun epg--status-NOTATION_DATA (context string
)
1053 (let ((signature (car (epg-context-result-for context
'verify
)))
1056 (setq notation
(car (epg-signature-notations signature
))))
1057 (setf (epg-sig-notation-value notation
) string
))))
1059 (defun epg--status-POLICY_URL (context string
)
1060 (let ((signature (car (epg-context-result-for context
'verify
))))
1062 (push (epg-make-sig-notation nil string t nil
)
1063 (epg-signature-notations signature
)))))
1065 (defun epg--status-PROGRESS (context string
)
1066 (if (and (epg-context-progress-callback context
)
1067 (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1069 (funcall (car (epg-context-progress-callback context
))
1071 (match-string 1 string
)
1072 (match-string 2 string
)
1073 (string-to-number (match-string 3 string
))
1074 (string-to-number (match-string 4 string
))
1075 (cdr (epg-context-progress-callback context
)))))
1077 (defun epg--status-ENC_TO (context string
)
1078 (if (string-match "\\`\\([0-9A-Za-z]+\\) \\([0-9]+\\) \\([0-9]+\\)" string
)
1079 (epg-context-set-result-for
1080 context
'encrypted-to
1081 (cons (list (match-string 1 string
)
1082 (string-to-number (match-string 2 string
))
1083 (string-to-number (match-string 3 string
)))
1084 (epg-context-result-for context
'encrypted-to
)))))
1086 (defun epg--status-DECRYPTION_FAILED (context _string
)
1087 (epg-context-set-result-for context
'decryption-failed t
))
1089 (defun epg--status-DECRYPTION_OKAY (context _string
)
1090 (epg-context-set-result-for context
'decryption-okay t
))
1092 (defun epg--status-NODATA (context string
)
1093 (epg-context-set-result-for
1095 (cons (cons 'no-data
(string-to-number string
))
1096 (epg-context-result-for context
'error
))))
1098 (defun epg--status-UNEXPECTED (context string
)
1099 (epg-context-set-result-for
1101 (cons (cons 'unexpected
(string-to-number string
))
1102 (epg-context-result-for context
'error
))))
1104 (defun epg--status-KEYEXPIRED (context string
)
1105 (epg-context-set-result-for
1107 (cons (list 'key-expired
(cons 'expiration-time
1108 (epg--time-from-seconds string
)))
1109 (epg-context-result-for context
'key
))))
1111 (defun epg--status-KEYREVOKED (context _string
)
1112 (epg-context-set-result-for
1114 (cons '(key-revoked)
1115 (epg-context-result-for context
'key
))))
1117 (defun epg--status-BADARMOR (context _string
)
1118 (epg-context-set-result-for
1121 (epg-context-result-for context
'error
))))
1123 (defun epg--status-INV_RECP (context string
)
1124 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string
)
1125 (epg-context-set-result-for
1127 (cons (list 'invalid-recipient
1129 (string-to-number (match-string 1 string
)))
1131 (match-string 2 string
)))
1132 (epg-context-result-for context
'error
)))))
1134 (defun epg--status-INV_SGNR (context string
)
1135 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string
)
1136 (epg-context-set-result-for
1138 (cons (list 'invalid-signer
1140 (string-to-number (match-string 1 string
)))
1142 (match-string 2 string
)))
1143 (epg-context-result-for context
'error
)))))
1145 (defun epg--status-NO_RECP (context _string
)
1146 (epg-context-set-result-for
1148 (cons '(no-recipients)
1149 (epg-context-result-for context
'error
))))
1151 (defun epg--status-NO_SGNR (context _string
)
1152 (epg-context-set-result-for
1155 (epg-context-result-for context
'error
))))
1157 (defun epg--status-DELETE_PROBLEM (context string
)
1158 (if (string-match "\\`\\([0-9]+\\)" string
)
1159 (epg-context-set-result-for
1161 (cons (cons 'delete-problem
1162 (string-to-number (match-string 1 string
)))
1163 (epg-context-result-for context
'error
)))))
1165 (defun epg--status-SIG_CREATED (context string
)
1166 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1167 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string
)
1168 (epg-context-set-result-for
1170 (cons (epg-make-new-signature
1171 (cdr (assq (aref (match-string 1 string
) 0)
1172 epg-new-signature-type-alist
))
1173 (string-to-number (match-string 2 string
))
1174 (string-to-number (match-string 3 string
))
1175 (string-to-number (match-string 4 string
) 16)
1176 (epg--time-from-seconds (match-string 5 string
))
1177 (substring string
(match-end 0)))
1178 (epg-context-result-for context
'sign
)))))
1180 (defun epg--status-KEY_CREATED (context string
)
1181 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string
)
1182 (epg-context-set-result-for
1183 context
'generate-key
1184 (cons (list (cons 'type
(string-to-char (match-string 1 string
)))
1185 (cons 'fingerprint
(match-string 2 string
)))
1186 (epg-context-result-for context
'generate-key
)))))
1188 (defun epg--status-KEY_NOT_CREATED (context _string
)
1189 (epg-context-set-result-for
1191 (cons '(key-not-created)
1192 (epg-context-result-for context
'error
))))
1194 (defun epg--status-IMPORTED (_context string
)
1195 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string
)
1196 (let* ((key-id (match-string 1 string
))
1197 (user-id (match-string 2 string
))
1198 (entry (assoc key-id epg-user-id-alist
)))
1200 (setq user-id
(epg--decode-coding-string
1201 (epg--decode-percent-escape user-id
)
1205 (setcdr entry user-id
)
1206 (setq epg-user-id-alist
(cons (cons key-id user-id
)
1207 epg-user-id-alist
))))))
1209 (defun epg--status-IMPORT_OK (context string
)
1210 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string
)
1211 (let ((reason (string-to-number (match-string 1 string
))))
1212 (epg-context-set-result-for
1213 context
'import-status
1214 (cons (epg-make-import-status (if (match-beginning 2)
1215 (match-string 3 string
))
1217 (/= (logand reason
1) 0)
1218 (/= (logand reason
2) 0)
1219 (/= (logand reason
4) 0)
1220 (/= (logand reason
8) 0)
1221 (/= (logand reason
16) 0))
1222 (epg-context-result-for context
'import-status
))))))
1224 (defun epg--status-IMPORT_PROBLEM (context string
)
1225 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string
)
1226 (epg-context-set-result-for
1227 context
'import-status
1228 (cons (epg-make-import-status
1229 (if (match-beginning 2)
1230 (match-string 3 string
))
1231 (string-to-number (match-string 1 string
)))
1232 (epg-context-result-for context
'import-status
)))))
1234 (defun epg--status-IMPORT_RES (context string
)
1235 (when (string-match "\\`\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1236 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1237 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" string
)
1238 (epg-context-set-result-for
1240 (epg-make-import-result (string-to-number (match-string 1 string
))
1241 (string-to-number (match-string 2 string
))
1242 (string-to-number (match-string 3 string
))
1243 (string-to-number (match-string 4 string
))
1244 (string-to-number (match-string 5 string
))
1245 (string-to-number (match-string 6 string
))
1246 (string-to-number (match-string 7 string
))
1247 (string-to-number (match-string 8 string
))
1248 (string-to-number (match-string 9 string
))
1249 (string-to-number (match-string 10 string
))
1250 (string-to-number (match-string 11 string
))
1251 (string-to-number (match-string 12 string
))
1252 (string-to-number (match-string 13 string
))
1253 (epg-context-result-for context
'import-status
)))
1254 (epg-context-set-result-for context
'import-status nil
)))
1256 (defun epg-passphrase-callback-function (context key-id _handback
)
1257 (declare (obsolete epa-passphrase-callback-function
"23.1"))
1258 (if (eq key-id
'SYM
)
1259 (read-passwd "Passphrase for symmetric encryption: "
1260 (eq (epg-context-operation context
) 'encrypt
))
1262 (if (eq key-id
'PIN
)
1263 "Passphrase for PIN: "
1264 (let ((entry (assoc key-id epg-user-id-alist
)))
1266 (format "Passphrase for %s %s: " key-id
(cdr entry
))
1267 (format "Passphrase for %s: " key-id
)))))))
1269 (defun epg--list-keys-1 (context name mode
)
1270 (let ((args (append (if (epg-context-home-directory context
)
1272 (epg-context-home-directory context
)))
1273 '("--with-colons" "--no-greeting" "--batch"
1274 "--with-fingerprint" "--with-fingerprint")
1275 (unless (eq (epg-context-protocol context
) 'CMS
)
1276 '("--fixed-list-mode"))))
1277 (list-keys-option (if (memq mode
'(t secret
))
1278 "--list-secret-keys"
1279 (if (memq mode
'(nil public
))
1282 (coding-system-for-read 'binary
)
1283 keys string field index
)
1286 (unless (listp name
)
1287 (setq name
(list name
)))
1289 (setq args
(append args
(list list-keys-option
(car name
)))
1291 (setq args
(append args
(list list-keys-option
))))
1293 (apply #'call-process
1294 (epg-context-program context
)
1295 nil
(list t nil
) nil args
)
1296 (goto-char (point-min))
1297 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t
)
1298 (setq keys
(cons (make-vector 15 nil
) keys
)
1299 string
(match-string 0)
1302 (while (and (< field
(length (car keys
)))
1304 (string-match "\\([^:]+\\)?:" string index
)))
1305 (setq index
(match-end 0))
1306 (aset (car keys
) field
(match-string 1 string
))
1307 (setq field
(1+ field
))))
1310 (defun epg--make-sub-key-1 (line)
1313 (cdr (assq (string-to-char (aref line
1)) epg-key-validity-alist
)))
1315 (mapcar (lambda (char) (cdr (assq char epg-key-capability-alist
)))
1317 (member (aref line
0) '("sec" "ssb"))
1318 (string-to-number (aref line
3))
1319 (string-to-number (aref line
2))
1321 (epg--time-from-seconds (aref line
5))
1323 (epg--time-from-seconds (aref line
6)))))
1325 (defun epg-list-keys (context &optional name mode
)
1326 "Return a list of epg-key objects matched with NAME.
1327 If MODE is nil or 'public, only public keyring should be searched.
1328 If MODE is t or 'secret, only secret keyring should be searched.
1329 Otherwise, only public keyring should be searched and the key
1330 signatures should be included.
1331 NAME is either a string or a list of strings."
1332 (let ((lines (epg--list-keys-1 context name mode
))
1333 keys cert pointer pointer-1 index string
)
1336 ((member (aref (car lines
) 0) '("pub" "sec" "crt" "crs"))
1337 (setq cert
(member (aref (car lines
) 0) '("crt" "crs"))
1338 keys
(cons (epg-make-key
1339 (if (aref (car lines
) 8)
1340 (cdr (assq (string-to-char (aref (car lines
) 8))
1341 epg-key-validity-alist
))))
1343 (push (epg--make-sub-key-1 (car lines
))
1344 (epg-key-sub-key-list (car keys
))))
1345 ((member (aref (car lines
) 0) '("sub" "ssb"))
1346 (push (epg--make-sub-key-1 (car lines
))
1347 (epg-key-sub-key-list (car keys
))))
1348 ((equal (aref (car lines
) 0) "uid")
1349 ;; Decode the UID name as a backslash escaped UTF-8 string,
1350 ;; generated by GnuPG/GpgSM.
1351 (setq string
(copy-sequence (aref (car lines
) 9))
1353 (while (string-match "\"" string index
)
1354 (setq string
(replace-match "\\\"" t t string
)
1355 index
(1+ (match-end 0))))
1357 (setq string
(epg--decode-coding-string
1358 (car (read-from-string (concat "\"" string
"\"")))
1361 (setq string
(aref (car lines
) 9))))
1362 (push (epg-make-user-id
1363 (if (aref (car lines
) 1)
1364 (cdr (assq (string-to-char (aref (car lines
) 1))
1365 epg-key-validity-alist
)))
1368 (epg-dn-from-string string
)
1371 (epg-key-user-id-list (car keys
))))
1372 ((equal (aref (car lines
) 0) "fpr")
1373 (setf (epg-sub-key-fingerprint (car (epg-key-sub-key-list (car keys
))))
1374 (aref (car lines
) 9)))
1375 ((equal (aref (car lines
) 0) "sig")
1377 (epg-make-key-signature
1378 (if (aref (car lines
) 1)
1379 (cdr (assq (string-to-char (aref (car lines
) 1))
1380 epg-key-validity-alist
)))
1381 (string-to-number (aref (car lines
) 3))
1382 (aref (car lines
) 4)
1383 (epg--time-from-seconds (aref (car lines
) 5))
1384 (epg--time-from-seconds (aref (car lines
) 6))
1385 (aref (car lines
) 9)
1386 (string-to-number (aref (car lines
) 10) 16)
1387 (eq (aref (aref (car lines
) 10) 2) ?x
))
1388 (epg-user-id-signature-list
1389 (car (epg-key-user-id-list (car keys
)))))))
1390 (setq lines
(cdr lines
)))
1391 (setq keys
(nreverse keys
)
1394 (epg--gv-nreverse (epg-key-sub-key-list (car pointer
)))
1395 (setq pointer-1
(epg--gv-nreverse (epg-key-user-id-list (car pointer
))))
1397 (epg--gv-nreverse (epg-user-id-signature-list (car pointer-1
)))
1398 (setq pointer-1
(cdr pointer-1
)))
1399 (setq pointer
(cdr pointer
)))
1403 (if (fboundp 'make-temp-file
)
1404 (defalias 'epg--make-temp-file
'make-temp-file
)
1405 (defvar temporary-file-directory
)
1406 ;; stolen from poe.el.
1407 (defun epg--make-temp-file (prefix)
1408 "Create a temporary file.
1409 The returned file name (created by appending some random characters at the end
1410 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1411 is guaranteed to point to a newly created empty file.
1412 You can then use `write-region' to write new data into the file."
1413 (let ((orig-modes (default-file-modes))
1415 (setq prefix
(expand-file-name prefix
1416 (if (featurep 'xemacs
)
1418 temporary-file-directory
)))
1421 ;; First, create a temporary directory.
1422 (set-default-file-modes #o700
)
1423 (while (condition-case ()
1425 (setq tempdir
(make-temp-name
1427 (file-name-directory prefix
)
1429 ;; return nil or signal an error.
1430 (make-directory tempdir
))
1432 (file-already-exists t
)))
1433 ;; Second, create a temporary file in the tempdir.
1434 ;; There *is* a race condition between `make-temp-name'
1435 ;; and `write-region', but we don't care it since we are
1436 ;; in a private directory now.
1437 (setq tempfile
(make-temp-name (concat tempdir
"/EMU")))
1438 (write-region "" nil tempfile nil
'silent
)
1439 ;; Finally, make a hard-link from the tempfile.
1440 (while (condition-case ()
1442 (setq file
(make-temp-name prefix
))
1443 ;; return nil or signal an error.
1444 (add-name-to-file tempfile file
))
1446 (file-already-exists t
)))
1448 (set-default-file-modes orig-modes
)
1449 ;; Cleanup the tempfile.
1451 (file-exists-p tempfile
)
1452 (delete-file tempfile
))
1453 ;; Cleanup the tempdir.
1455 (file-directory-p tempdir
)
1456 (delete-directory tempdir
)))))))
1458 (defun epg--args-from-sig-notations (notations)
1462 (if (and (epg-sig-notation-name notation
)
1463 (not (epg-sig-notation-human-readable notation
)))
1464 (error "Unreadable"))
1465 (if (epg-sig-notation-name notation
)
1466 (list "--sig-notation"
1467 (if (epg-sig-notation-critical notation
)
1468 (concat "!" (epg-sig-notation-name notation
)
1469 "=" (epg-sig-notation-value notation
))
1470 (concat (epg-sig-notation-name notation
)
1471 "=" (epg-sig-notation-value notation
))))
1472 (list "--sig-policy-url"
1473 (if (epg-sig-notation-critical notation
)
1474 (concat "!" (epg-sig-notation-value notation
))
1475 (epg-sig-notation-value notation
)))))
1478 (defun epg-cancel (context)
1479 (if (buffer-live-p (process-buffer (epg-context-process context
)))
1480 (with-current-buffer (process-buffer (epg-context-process context
))
1481 (epg-context-set-result-for
1484 (epg-context-result-for epg-context
'error
)))))
1485 (if (eq (process-status (epg-context-process context
)) 'run
)
1486 (delete-process (epg-context-process context
))))
1488 (defun epg-start-decrypt (context cipher
)
1489 "Initiate a decrypt operation on CIPHER.
1490 CIPHER must be a file data object.
1492 If you use this function, you will need to wait for the completion of
1493 `epg-gpg-program' by using `epg-wait-for-completion' and call
1494 `epg-reset' to clear a temporary output file.
1495 If you are unsure, use synchronous version of this function
1496 `epg-decrypt-file' or `epg-decrypt-string' instead."
1497 (unless (epg-data-file cipher
)
1498 (error "Not a file"))
1499 (setf (epg-context-operation context
) 'decrypt
)
1500 (setf (epg-context-result context
) nil
)
1501 (epg--start context
(list "--decrypt" "--" (epg-data-file cipher
)))
1502 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1503 (unless (eq (epg-context-protocol context
) 'CMS
)
1504 (epg-wait-for-status context
'("BEGIN_DECRYPTION"))))
1506 (defun epg--check-error-for-decrypt (context)
1507 (let ((errors (epg-context-result-for context
'error
)))
1508 (if (epg-context-result-for context
'decryption-failed
)
1510 (list "Decryption failed" (epg-errors-to-string errors
))))
1511 (unless (epg-context-result-for context
'decryption-okay
)
1513 (list "Can't decrypt" (epg-errors-to-string errors
))))))
1515 (defun epg-decrypt-file (context cipher plain
)
1516 "Decrypt a file CIPHER and store the result to a file PLAIN.
1517 If PLAIN is nil, it returns the result as a string."
1520 (setf (epg-context-output-file context
)
1521 (or plain
(epg--make-temp-file "epg-output")))
1522 (epg-start-decrypt context
(epg-make-data-from-file cipher
))
1523 (epg-wait-for-completion context
)
1524 (epg--check-error-for-decrypt context
)
1526 (epg-read-output context
)))
1528 (epg-delete-output-file context
))
1529 (epg-reset context
)))
1531 (defun epg-decrypt-string (context cipher
)
1532 "Decrypt a string CIPHER and return the plain text."
1533 (let ((input-file (epg--make-temp-file "epg-input"))
1534 (coding-system-for-write 'binary
))
1537 (write-region cipher nil input-file nil
'quiet
)
1538 (setf (epg-context-output-file context
)
1539 (epg--make-temp-file "epg-output"))
1540 (epg-start-decrypt context
(epg-make-data-from-file input-file
))
1541 (epg-wait-for-completion context
)
1542 (epg--check-error-for-decrypt context
)
1543 (epg-read-output context
))
1544 (epg-delete-output-file context
)
1545 (if (file-exists-p input-file
)
1546 (delete-file input-file
))
1547 (epg-reset context
))))
1549 (defun epg-start-verify (context signature
&optional signed-text
)
1550 "Initiate a verify operation on SIGNATURE.
1551 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1553 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1554 For a normal or a cleartext signature, SIGNED-TEXT should be nil.
1556 If you use this function, you will need to wait for the completion of
1557 `epg-gpg-program' by using `epg-wait-for-completion' and call
1558 `epg-reset' to clear a temporary output file.
1559 If you are unsure, use synchronous version of this function
1560 `epg-verify-file' or `epg-verify-string' instead."
1561 (setf (epg-context-operation context
) 'verify
)
1562 (setf (epg-context-result context
) nil
)
1564 ;; Detached signature.
1565 (if (epg-data-file signed-text
)
1566 (epg--start context
(list "--verify" "--" (epg-data-file signature
)
1567 (epg-data-file signed-text
)))
1568 (epg--start context
(list "--verify" "--" (epg-data-file signature
)
1570 (if (eq (process-status (epg-context-process context
)) 'run
)
1571 (process-send-string (epg-context-process context
)
1572 (epg-data-string signed-text
)))
1573 (if (eq (process-status (epg-context-process context
)) 'run
)
1574 (process-send-eof (epg-context-process context
))))
1575 ;; Normal (or cleartext) signature.
1576 (if (epg-data-file signature
)
1577 (epg--start context
(if (eq (epg-context-protocol context
) 'CMS
)
1578 (list "--verify" "--" (epg-data-file signature
))
1579 (list "--" (epg-data-file signature
))))
1580 (epg--start context
(if (eq (epg-context-protocol context
) 'CMS
)
1583 (if (eq (process-status (epg-context-process context
)) 'run
)
1584 (process-send-string (epg-context-process context
)
1585 (epg-data-string signature
)))
1586 (if (eq (process-status (epg-context-process context
)) 'run
)
1587 (process-send-eof (epg-context-process context
))))))
1589 (defun epg-verify-file (context signature
&optional signed-text plain
)
1590 "Verify a file SIGNATURE.
1591 SIGNED-TEXT and PLAIN are also a file if they are specified.
1593 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1594 string. For a normal or a cleartext signature, SIGNED-TEXT should be
1595 nil. In the latter case, if PLAIN is specified, the plaintext is
1596 stored into the file after successful verification.
1598 Note that this function does not return verification result as t
1599 or nil, nor signal error on failure. That's a design decision to
1600 handle the case where SIGNATURE has multiple signature.
1602 To check the verification results, use `epg-context-result-for' as follows:
1604 \(epg-context-result-for context 'verify)
1606 which will return a list of `epg-signature' object."
1609 (setf (epg-context-output-file context
)
1610 (or plain
(epg--make-temp-file "epg-output")))
1612 (epg-start-verify context
1613 (epg-make-data-from-file signature
)
1614 (epg-make-data-from-file signed-text
))
1615 (epg-start-verify context
1616 (epg-make-data-from-file signature
)))
1617 (epg-wait-for-completion context
)
1619 (epg-read-output context
)))
1621 (epg-delete-output-file context
))
1622 (epg-reset context
)))
1624 (defun epg-verify-string (context signature
&optional signed-text
)
1625 "Verify a string SIGNATURE.
1626 SIGNED-TEXT is a string if it is specified.
1628 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1629 string. For a normal or a cleartext signature, SIGNED-TEXT should be
1630 nil. In the latter case, this function returns the plaintext after
1631 successful verification.
1633 Note that this function does not return verification result as t
1634 or nil, nor signal error on failure. That's a design decision to
1635 handle the case where SIGNATURE has multiple signature.
1637 To check the verification results, use `epg-context-result-for' as follows:
1639 \(epg-context-result-for context 'verify)
1641 which will return a list of `epg-signature' object."
1642 (let ((coding-system-for-write 'binary
)
1646 (setf (epg-context-output-file context
)
1647 (epg--make-temp-file "epg-output"))
1650 (setq input-file
(epg--make-temp-file "epg-signature"))
1651 (write-region signature nil input-file nil
'quiet
)
1652 (epg-start-verify context
1653 (epg-make-data-from-file input-file
)
1654 (epg-make-data-from-string signed-text
)))
1655 (epg-start-verify context
(epg-make-data-from-string signature
)))
1656 (epg-wait-for-completion context
)
1657 (epg-read-output context
))
1658 (epg-delete-output-file context
)
1660 (file-exists-p input-file
))
1661 (delete-file input-file
))
1662 (epg-reset context
))))
1664 (defun epg-start-sign (context plain
&optional mode
)
1665 "Initiate a sign operation on PLAIN.
1666 PLAIN is a data object.
1668 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1669 If it is nil or 'normal, it makes a normal signature.
1670 Otherwise, it makes a cleartext signature.
1672 If you use this function, you will need to wait for the completion of
1673 `epg-gpg-program' by using `epg-wait-for-completion' and call
1674 `epg-reset' to clear a temporary output file.
1675 If you are unsure, use synchronous version of this function
1676 `epg-sign-file' or `epg-sign-string' instead."
1677 (setf (epg-context-operation context
) 'sign
)
1678 (setf (epg-context-result context
) nil
)
1679 (unless (memq mode
'(t detached nil normal
)) ;i.e. cleartext
1680 (epg-context-set-armor context nil
)
1681 (epg-context-set-textmode context nil
))
1683 (append (list (if (memq mode
'(t detached
))
1685 (if (memq mode
'(nil normal
))
1693 (car (epg-key-sub-key-list signer
)))))
1694 (epg-context-signers context
)))
1695 (epg--args-from-sig-notations
1696 (epg-context-sig-notations context
))
1697 (if (epg-data-file plain
)
1698 (list "--" (epg-data-file plain
)))))
1699 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1700 (unless (eq (epg-context-protocol context
) 'CMS
)
1701 (epg-wait-for-status context
'("BEGIN_SIGNING")))
1702 (when (epg-data-string plain
)
1703 (if (eq (process-status (epg-context-process context
)) 'run
)
1704 (process-send-string (epg-context-process context
)
1705 (epg-data-string plain
)))
1706 (if (eq (process-status (epg-context-process context
)) 'run
)
1707 (process-send-eof (epg-context-process context
)))))
1709 (defun epg-sign-file (context plain signature
&optional mode
)
1710 "Sign a file PLAIN and store the result to a file SIGNATURE.
1711 If SIGNATURE is nil, it returns the result as a string.
1712 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1713 If it is nil or 'normal, it makes a normal signature.
1714 Otherwise, it makes a cleartext signature."
1717 (setf (epg-context-output-file context
)
1718 (or signature
(epg--make-temp-file "epg-output")))
1719 (epg-start-sign context
(epg-make-data-from-file plain
) mode
)
1720 (epg-wait-for-completion context
)
1721 (unless (epg-context-result-for context
'sign
)
1722 (let ((errors (epg-context-result-for context
'error
)))
1724 (list "Sign failed" (epg-errors-to-string errors
)))))
1726 (epg-read-output context
)))
1728 (epg-delete-output-file context
))
1729 (epg-reset context
)))
1731 (defun epg-sign-string (context plain
&optional mode
)
1732 "Sign a string PLAIN and return the output as string.
1733 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1734 If it is nil or 'normal, it makes a normal signature.
1735 Otherwise, it makes a cleartext signature."
1737 (unless (or (eq (epg-context-protocol context
) 'CMS
)
1740 (epg-check-configuration (epg-configuration))
1743 (epg--make-temp-file "epg-input")))
1744 (coding-system-for-write 'binary
))
1747 (setf (epg-context-output-file context
)
1748 (epg--make-temp-file "epg-output"))
1750 (write-region plain nil input-file nil
'quiet
))
1751 (epg-start-sign context
1753 (epg-make-data-from-file input-file
)
1754 (epg-make-data-from-string plain
))
1756 (epg-wait-for-completion context
)
1757 (unless (epg-context-result-for context
'sign
)
1758 (if (epg-context-result-for context
'error
)
1759 (let ((errors (epg-context-result-for context
'error
)))
1761 (list "Sign failed" (epg-errors-to-string errors
))))))
1762 (epg-read-output context
))
1763 (epg-delete-output-file context
)
1765 (delete-file input-file
))
1766 (epg-reset context
))))
1768 (defun epg-start-encrypt (context plain recipients
1769 &optional sign always-trust
)
1770 "Initiate an encrypt operation on PLAIN.
1771 PLAIN is a data object.
1772 If RECIPIENTS is nil, it performs symmetric encryption.
1774 If you use this function, you will need to wait for the completion of
1775 `epg-gpg-program' by using `epg-wait-for-completion' and call
1776 `epg-reset' to clear a temporary output file.
1777 If you are unsure, use synchronous version of this function
1778 `epg-encrypt-file' or `epg-encrypt-string' instead."
1779 (setf (epg-context-operation context
) 'encrypt
)
1780 (setf (epg-context-result context
) nil
)
1782 (append (if always-trust
'("--always-trust"))
1783 (if recipients
'("--encrypt") '("--symmetric"))
1784 (if sign
'("--sign"))
1791 (car (epg-key-sub-key-list
1793 (epg-context-signers context
))))
1795 (epg--args-from-sig-notations
1796 (epg-context-sig-notations context
)))
1802 (car (epg-key-sub-key-list recipient
)))))
1804 (if (epg-data-file plain
)
1805 (list "--" (epg-data-file plain
)))))
1806 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1807 (unless (eq (epg-context-protocol context
) 'CMS
)
1808 (epg-wait-for-status context
1809 (if sign
'("BEGIN_SIGNING") '("BEGIN_ENCRYPTION"))))
1810 (when (epg-data-string plain
)
1811 (if (eq (process-status (epg-context-process context
)) 'run
)
1812 (process-send-string (epg-context-process context
)
1813 (epg-data-string plain
)))
1814 (if (eq (process-status (epg-context-process context
)) 'run
)
1815 (process-send-eof (epg-context-process context
)))))
1817 (defun epg-encrypt-file (context plain recipients
1818 cipher
&optional sign always-trust
)
1819 "Encrypt a file PLAIN and store the result to a file CIPHER.
1820 If CIPHER is nil, it returns the result as a string.
1821 If RECIPIENTS is nil, it performs symmetric encryption."
1824 (setf (epg-context-output-file context
)
1825 (or cipher
(epg--make-temp-file "epg-output")))
1826 (epg-start-encrypt context
(epg-make-data-from-file plain
)
1827 recipients sign always-trust
)
1828 (epg-wait-for-completion context
)
1829 (let ((errors (epg-context-result-for context
'error
)))
1831 (not (epg-context-result-for context
'sign
)))
1833 (list "Sign failed" (epg-errors-to-string errors
))))
1836 (list "Encrypt failed" (epg-errors-to-string errors
)))))
1838 (epg-read-output context
)))
1840 (epg-delete-output-file context
))
1841 (epg-reset context
)))
1843 (defun epg-encrypt-string (context plain recipients
1844 &optional sign always-trust
)
1845 "Encrypt a string PLAIN.
1846 If RECIPIENTS is nil, it performs symmetric encryption."
1848 (unless (or (not sign
)
1849 (eq (epg-context-protocol context
) 'CMS
)
1852 (epg-check-configuration (epg-configuration))
1855 (epg--make-temp-file "epg-input")))
1856 (coding-system-for-write 'binary
))
1859 (setf (epg-context-output-file context
)
1860 (epg--make-temp-file "epg-output"))
1862 (write-region plain nil input-file nil
'quiet
))
1863 (epg-start-encrypt context
1865 (epg-make-data-from-file input-file
)
1866 (epg-make-data-from-string plain
))
1867 recipients sign always-trust
)
1868 (epg-wait-for-completion context
)
1869 (let ((errors (epg-context-result-for context
'error
)))
1871 (not (epg-context-result-for context
'sign
)))
1873 (list "Sign failed" (epg-errors-to-string errors
))))
1876 (list "Encrypt failed" (epg-errors-to-string errors
)))))
1877 (epg-read-output context
))
1878 (epg-delete-output-file context
)
1880 (delete-file input-file
))
1881 (epg-reset context
))))
1883 (defun epg-start-export-keys (context keys
)
1884 "Initiate an export keys operation.
1886 If you use this function, you will need to wait for the completion of
1887 `epg-gpg-program' by using `epg-wait-for-completion' and call
1888 `epg-reset' to clear a temporary output file.
1889 If you are unsure, use synchronous version of this function
1890 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1891 (setf (epg-context-operation context
) 'export-keys
)
1892 (setf (epg-context-result context
) nil
)
1893 (epg--start context
(cons "--export"
1897 (car (epg-key-sub-key-list key
))))
1900 (defun epg-export-keys-to-file (context keys file
)
1901 "Extract public KEYS."
1904 (setf (epg-context-output-file context
)
1905 (or file
(epg--make-temp-file "epg-output")))
1906 (epg-start-export-keys context keys
)
1907 (epg-wait-for-completion context
)
1908 (let ((errors (epg-context-result-for context
'error
)))
1911 (list "Export keys failed"
1912 (epg-errors-to-string errors
)))))
1914 (epg-read-output context
)))
1916 (epg-delete-output-file context
))
1917 (epg-reset context
)))
1919 (defun epg-export-keys-to-string (context keys
)
1920 "Extract public KEYS and return them as a string."
1921 (epg-export-keys-to-file context keys nil
))
1923 (defun epg-start-import-keys (context keys
)
1924 "Initiate an import keys operation.
1925 KEYS is a data object.
1927 If you use this function, you will need to wait for the completion of
1928 `epg-gpg-program' by using `epg-wait-for-completion' and call
1929 `epg-reset' to clear a temporary output file.
1930 If you are unsure, use synchronous version of this function
1931 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1932 (setf (epg-context-operation context
) 'import-keys
)
1933 (setf (epg-context-result context
) nil
)
1934 (epg--start context
(if (epg-data-file keys
)
1935 (list "--import" "--" (epg-data-file keys
))
1937 (when (epg-data-string keys
)
1938 (if (eq (process-status (epg-context-process context
)) 'run
)
1939 (process-send-string (epg-context-process context
)
1940 (epg-data-string keys
)))
1941 (if (eq (process-status (epg-context-process context
)) 'run
)
1942 (process-send-eof (epg-context-process context
)))))
1944 (defun epg--import-keys-1 (context keys
)
1947 (epg-start-import-keys context keys
)
1948 (epg-wait-for-completion context
)
1949 (let ((errors (epg-context-result-for context
'error
)))
1952 (list "Import keys failed"
1953 (epg-errors-to-string errors
))))))
1954 (epg-reset context
)))
1956 (defun epg-import-keys-from-file (context keys
)
1957 "Add keys from a file KEYS."
1958 (epg--import-keys-1 context
(epg-make-data-from-file keys
)))
1960 (defun epg-import-keys-from-string (context keys
)
1961 "Add keys from a string KEYS."
1962 (epg--import-keys-1 context
(epg-make-data-from-string keys
)))
1964 (defun epg-start-receive-keys (context key-id-list
)
1965 "Initiate a receive key operation.
1966 KEY-ID-LIST is a list of key IDs.
1968 If you use this function, you will need to wait for the completion of
1969 `epg-gpg-program' by using `epg-wait-for-completion' and call
1970 `epg-reset' to clear a temporary output file.
1971 If you are unsure, use synchronous version of this function
1972 `epg-receive-keys' instead."
1973 (setf (epg-context-operation context
) 'receive-keys
)
1974 (setf (epg-context-result context
) nil
)
1975 (epg--start context
(cons "--recv-keys" key-id-list
)))
1977 (defun epg-receive-keys (context keys
)
1978 "Add keys from server.
1979 KEYS is a list of key IDs"
1982 (epg-start-receive-keys context keys
)
1983 (epg-wait-for-completion context
)
1984 (let ((errors (epg-context-result-for context
'error
)))
1987 (list "Receive keys failed"
1988 (epg-errors-to-string errors
))))))
1989 (epg-reset context
)))
1991 (defalias 'epg-import-keys-from-server
'epg-receive-keys
)
1993 (defun epg-start-delete-keys (context keys
&optional allow-secret
)
1994 "Initiate a delete keys operation.
1996 If you use this function, you will need to wait for the completion of
1997 `epg-gpg-program' by using `epg-wait-for-completion' and call
1998 `epg-reset' to clear a temporary output file.
1999 If you are unsure, use synchronous version of this function
2000 `epg-delete-keys' instead."
2001 (setf (epg-context-operation context
) 'delete-keys
)
2002 (setf (epg-context-result context
) nil
)
2003 (epg--start context
(cons (if allow-secret
2004 "--delete-secret-key"
2009 (car (epg-key-sub-key-list key
))))
2012 (defun epg-delete-keys (context keys
&optional allow-secret
)
2013 "Delete KEYS from the key ring."
2016 (epg-start-delete-keys context keys allow-secret
)
2017 (epg-wait-for-completion context
)
2018 (let ((errors (epg-context-result-for context
'error
)))
2021 (list "Delete keys failed"
2022 (epg-errors-to-string errors
))))))
2023 (epg-reset context
)))
2025 (defun epg-start-sign-keys (context keys
&optional local
)
2026 "Initiate a sign keys operation.
2028 If you use this function, you will need to wait for the completion of
2029 `epg-gpg-program' by using `epg-wait-for-completion' and call
2030 `epg-reset' to clear a temporary output file.
2031 If you are unsure, use synchronous version of this function
2032 `epg-sign-keys' instead."
2033 (declare (obsolete nil
"23.1"))
2034 (setf (epg-context-operation context
) 'sign-keys
)
2035 (setf (epg-context-result context
) nil
)
2036 (epg--start context
(cons (if local
2042 (car (epg-key-sub-key-list key
))))
2045 (defun epg-sign-keys (context keys
&optional local
)
2046 "Sign KEYS from the key ring."
2047 (declare (obsolete nil
"23.1"))
2050 (epg-start-sign-keys context keys local
)
2051 (epg-wait-for-completion context
)
2052 (let ((errors (epg-context-result-for context
'error
)))
2055 (list "Sign keys failed"
2056 (epg-errors-to-string errors
))))))
2057 (epg-reset context
)))
2059 (defun epg-start-generate-key (context parameters
)
2060 "Initiate a key generation.
2061 PARAMETERS is a string which specifies parameters of the generated key.
2062 See Info node `(gnupg) Unattended GPG key generation' in the
2063 GnuPG manual for the format.
2065 If you use this function, you will need to wait for the completion of
2066 `epg-gpg-program' by using `epg-wait-for-completion' and call
2067 `epg-reset' to clear a temporary output file.
2068 If you are unsure, use synchronous version of this function
2069 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2070 (setf (epg-context-operation context
) 'generate-key
)
2071 (setf (epg-context-result context
) nil
)
2072 (if (epg-data-file parameters
)
2073 (epg--start context
(list "--batch" "--gen-key" "--"
2074 (epg-data-file parameters
)))
2075 (epg--start context
'("--batch" "--gen-key"))
2076 (if (eq (process-status (epg-context-process context
)) 'run
)
2077 (process-send-string (epg-context-process context
)
2078 (epg-data-string parameters
)))
2079 (if (eq (process-status (epg-context-process context
)) 'run
)
2080 (process-send-eof (epg-context-process context
)))))
2082 (defun epg-generate-key-from-file (context parameters
)
2083 "Generate a new key pair.
2084 PARAMETERS is a file which tells how to create the key."
2087 (epg-start-generate-key context
(epg-make-data-from-file parameters
))
2088 (epg-wait-for-completion context
)
2089 (let ((errors (epg-context-result-for context
'error
)))
2092 (list "Generate key failed"
2093 (epg-errors-to-string errors
))))))
2094 (epg-reset context
)))
2096 (defun epg-generate-key-from-string (context parameters
)
2097 "Generate a new key pair.
2098 PARAMETERS is a string which tells how to create the key."
2101 (epg-start-generate-key context
(epg-make-data-from-string parameters
))
2102 (epg-wait-for-completion context
)
2103 (let ((errors (epg-context-result-for context
'error
)))
2106 (list "Generate key failed"
2107 (epg-errors-to-string errors
))))))
2108 (epg-reset context
)))
2110 (defun epg-start-edit-key (context key edit-callback handback
)
2111 "Initiate an edit operation on KEY.
2113 EDIT-CALLBACK is called from process filter and takes 3
2114 arguments: the context, a status, an argument string, and the
2117 If you use this function, you will need to wait for the completion of
2118 `epg-gpg-program' by using `epg-wait-for-completion' and call
2119 `epg-reset' to clear a temporary output file.
2120 If you are unsure, use synchronous version of this function
2121 `epg-edit-key' instead."
2122 (setf (epg-context-operation context
) 'edit-key
)
2123 (setf (epg-context-result context
) nil
)
2124 (setf (epg-context-edit-callback context
) (cons edit-callback handback
))
2125 (epg--start context
(list "--edit-key"
2127 (car (epg-key-sub-key-list key
))))))
2129 (defun epg-edit-key (context key edit-callback handback
)
2130 "Edit KEY in the keyring."
2133 (epg-start-edit-key context key edit-callback handback
)
2134 (epg-wait-for-completion context
)
2135 (let ((errors (epg-context-result-for context
'error
)))
2138 (list "Edit key failed"
2139 (epg-errors-to-string errors
))))))
2140 (epg-reset context
)))
2142 (defun epg--decode-percent-escape (string)
2144 (while (string-match "%\\(\\(%\\)\\|\\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2146 (if (match-beginning 2)
2147 (setq string
(replace-match "%" t t string
)
2148 index
(1- (match-end 0)))
2149 (setq string
(replace-match
2150 (string (string-to-number (match-string 3 string
) 16))
2152 index
(- (match-end 0) 2))))
2155 (defun epg--decode-hexstring (string)
2157 (while (eq index
(string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index
))
2158 (setq string
(replace-match (string (string-to-number
2159 (match-string 0 string
) 16))
2161 index
(1- (match-end 0))))
2164 (defun epg--decode-quotedstring (string)
2166 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2167 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2169 (if (match-beginning 2)
2170 (setq string
(replace-match "\\2" t nil string
)
2171 index
(1- (match-end 0)))
2172 (if (match-beginning 3)
2173 (setq string
(replace-match (string (string-to-number
2174 (match-string 0 string
) 16))
2176 index
(- (match-end 0) 2)))))
2179 (defun epg-dn-from-string (string)
2180 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2181 The return value is an alist mapping from types to values."
2183 (length (length string
))
2184 alist type value group
)
2185 (while (< index length
)
2186 (if (eq index
(string-match "[ \t\n\r]*" string index
))
2187 (setq index
(match-end 0)))
2188 (if (eq index
(string-match
2189 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2191 (setq type
(match-string 1 string
)
2192 index
(match-end 0))
2193 (if (eq index
(string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2195 (setq type
(match-string 1 string
)
2196 index
(match-end 0))))
2198 (error "Invalid type"))
2199 (if (eq index
(string-match
2200 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2202 (setq index
(match-end 0)
2203 value
(epg--decode-quotedstring (match-string 0 string
)))
2204 (if (eq index
(string-match "#\\([0-9A-Fa-f]+\\)" string index
))
2205 (setq index
(match-end 0)
2206 value
(epg--decode-hexstring (match-string 1 string
)))
2207 (if (eq index
(string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2209 (setq index
(match-end 0)
2210 value
(epg--decode-quotedstring
2211 (match-string 0 string
))))))
2213 (if (stringp (car (car alist
)))
2214 (setcar alist
(list (cons type value
) (car alist
)))
2215 (setcar alist
(cons (cons type value
) (car alist
))))
2216 (if (consp (car (car alist
)))
2217 (setcar alist
(nreverse (car alist
))))
2218 (setq alist
(cons (cons type value
) alist
)
2221 (if (eq index
(string-match "[ \t\n\r]*\\([,;+]\\)" string index
))
2222 (setq index
(match-end 0)
2223 group
(eq (aref string
(match-beginning 1)) ?
+))))
2226 (defun epg-decode-dn (alist)
2227 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2228 Type names are resolved using `epg-dn-type-alist'."
2231 (if (stringp (car rdn
))
2232 (let ((entry (assoc (car rdn
) epg-dn-type-alist
)))
2234 (format "%s=%s" (cdr entry
) (cdr rdn
))
2235 (format "%s=%s" (car rdn
) (cdr rdn
))))
2236 (concat "(" (epg-decode-dn rdn
) ")")))
2242 ;;; epg.el ends here