1 ;;; epg.el --- the EasyPG Library -*- lexical-binding: t -*-
2 ;; Copyright (C) 1999-2000, 2002-2018 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 <https://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
189 (let ((configuration (epg-find-configuration protocol
)))
190 (unless configuration
192 (list "no usable configuration" protocol
)))
193 (alist-get 'program configuration
)))))
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 ;; <https://lists.gnu.org/r/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 (time-less-p epg-agent-mtime
742 (or (nth 5 (file-attributes epg-agent-file
)) 0))))
744 (epg-context-set-result-for
746 (nreverse (epg-context-result-for context
'error
)))
747 (setf (epg-context-error-output context
)
748 (with-current-buffer (epg-context-error-buffer context
)
751 (defun epg-reset (context)
753 (if (and (epg-context-process context
)
754 (buffer-live-p (process-buffer (epg-context-process context
))))
755 (kill-buffer (process-buffer (epg-context-process context
))))
756 (if (buffer-live-p (epg-context-error-buffer context
))
757 (kill-buffer (epg-context-error-buffer context
)))
758 (setf (epg-context-process context
) nil
)
759 (setf (epg-context-edit-callback context
) nil
))
761 (defun epg-delete-output-file (context)
762 "Delete the output file of CONTEXT."
763 (if (and (epg-context-output-file context
)
764 (file-exists-p (epg-context-output-file context
)))
765 (delete-file (epg-context-output-file context
))))
768 (if (fboundp 'decode-coding-string
)
769 (defalias 'epg--decode-coding-string
'decode-coding-string
)
770 (defalias 'epg--decode-coding-string
'identity
)))
772 (defun epg--status-USERID_HINT (_context string
)
773 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string
)
774 (let* ((key-id (match-string 1 string
))
775 (user-id (match-string 2 string
))
776 (entry (assoc key-id epg-user-id-alist
)))
778 (setq user-id
(epg--decode-coding-string
779 (epg--decode-percent-escape user-id
)
783 (setcdr entry user-id
)
784 (setq epg-user-id-alist
(cons (cons key-id user-id
)
785 epg-user-id-alist
))))))
787 (defun epg--status-NEED_PASSPHRASE (_context string
)
788 (if (string-match "\\`\\([^ ]+\\)" string
)
789 (setq epg-key-id
(match-string 1 string
))))
791 (defun epg--status-NEED_PASSPHRASE_SYM (_context _string
)
792 (setq epg-key-id
'SYM
))
794 (defun epg--status-NEED_PASSPHRASE_PIN (_context _string
)
795 (setq epg-key-id
'PIN
))
798 (if (fboundp 'clear-string
)
799 (defalias 'epg--clear-string
'clear-string
)
800 (defun epg--clear-string (string)
801 (fillarray string
0))))
804 (if (fboundp 'encode-coding-string
)
805 (defalias 'epg--encode-coding-string
'encode-coding-string
)
806 (defalias 'epg--encode-coding-string
'identity
)))
808 (defun epg--status-GET_HIDDEN (context string
)
809 (when (and epg-key-id
810 (string-match "\\`passphrase\\." string
))
811 (unless (epg-context-passphrase-callback context
)
812 (error "passphrase-callback not set"))
815 passphrase-with-new-line
816 encoded-passphrase-with-new-line
)
822 (car (epg-context-passphrase-callback context
))
825 (cdr (epg-context-passphrase-callback context
))))
827 (setq passphrase-with-new-line
(concat passphrase
"\n"))
828 (epg--clear-string passphrase
)
829 (setq passphrase nil
)
830 (if epg-passphrase-coding-system
832 (setq encoded-passphrase-with-new-line
833 (epg--encode-coding-string
834 passphrase-with-new-line
835 (coding-system-change-eol-conversion
836 epg-passphrase-coding-system
'unix
)))
837 (epg--clear-string passphrase-with-new-line
)
838 (setq passphrase-with-new-line nil
))
839 (setq encoded-passphrase-with-new-line
840 passphrase-with-new-line
841 passphrase-with-new-line nil
))
842 (process-send-string (epg-context-process context
)
843 encoded-passphrase-with-new-line
)))
845 (epg-context-set-result-for
848 (epg-context-result-for context
'error
)))
849 (delete-process (epg-context-process context
))))
851 (epg--clear-string passphrase
))
852 (if passphrase-with-new-line
853 (epg--clear-string passphrase-with-new-line
))
854 (if encoded-passphrase-with-new-line
855 (epg--clear-string encoded-passphrase-with-new-line
))))))
857 (defun epg--prompt-GET_BOOL (_context string
)
858 (let ((entry (assoc string epg-prompt-alist
)))
859 (y-or-n-p (if entry
(cdr entry
) (concat string
"? ")))))
861 (defun epg--prompt-GET_BOOL-untrusted_key.override
(_context _string
)
862 (y-or-n-p (if (and (equal (car epg-last-status
) "USERID_HINT")
863 (string-match "\\`\\([^ ]+\\) \\(.*\\)"
864 (cdr epg-last-status
)))
865 (let* ((key-id (match-string 1 (cdr epg-last-status
)))
866 (user-id (match-string 2 (cdr epg-last-status
)))
867 (entry (assoc key-id epg-user-id-alist
)))
869 (setq user-id
(cdr entry
)))
870 (format "Untrusted key %s %s. Use anyway? " key-id user-id
))
871 "Use untrusted key anyway? ")))
873 (defun epg--status-GET_BOOL (context string
)
876 (if (funcall (or (intern-soft (concat "epg--prompt-GET_BOOL-" string
))
877 #'epg--prompt-GET_BOOL
)
879 (process-send-string (epg-context-process context
) "y\n")
880 (process-send-string (epg-context-process context
) "n\n"))
882 (epg-context-set-result-for
885 (epg-context-result-for context
'error
)))
886 (delete-process (epg-context-process context
))))))
888 (defun epg--status-GET_LINE (context string
)
889 (let ((entry (assoc string epg-prompt-alist
))
892 (process-send-string (epg-context-process context
)
896 (concat string
": ")))
899 (epg-context-set-result-for
902 (epg-context-result-for context
'error
)))
903 (delete-process (epg-context-process context
))))))
905 (defun epg--status-*SIG
(context status string
)
906 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string
)
907 (let* ((key-id (match-string 1 string
))
908 (user-id (match-string 2 string
))
909 (entry (assoc key-id epg-user-id-alist
)))
910 (epg-context-set-result-for
913 (cons (epg-make-signature status key-id
)
914 (epg-context-result-for context
'verify
)))
916 (if (eq (epg-context-protocol context
) 'CMS
)
917 (setq user-id
(epg-dn-from-string user-id
))
918 (setq user-id
(epg--decode-coding-string
919 (epg--decode-percent-escape user-id
)
923 (setcdr entry user-id
)
924 (setq epg-user-id-alist
925 (cons (cons key-id user-id
) epg-user-id-alist
))))
926 (epg-context-set-result-for
929 (cons (epg-make-signature status
)
930 (epg-context-result-for context
'verify
)))))
932 (defun epg--status-GOODSIG (context string
)
933 (epg--status-*SIG context
'good string
))
935 (defun epg--status-EXPSIG (context string
)
936 (epg--status-*SIG context
'expired string
))
938 (defun epg--status-EXPKEYSIG (context string
)
939 (epg--status-*SIG context
'expired-key string
))
941 (defun epg--status-REVKEYSIG (context string
)
942 (epg--status-*SIG context
'revoked-key string
))
944 (defun epg--status-BADSIG (context string
)
945 (epg--status-*SIG context
'bad string
))
947 (defun epg--status-NO_PUBKEY (context string
)
948 (if (eq (epg-context-operation context
) 'verify
)
949 (let ((signature (car (epg-context-result-for context
'verify
))))
951 (eq (epg-signature-status signature
) 'error
)
952 (equal (epg-signature-key-id signature
) string
))
953 (setf (epg-signature-status signature
) 'no-pubkey
)))
954 (epg-context-set-result-for
956 (cons (cons 'no-pubkey string
)
957 (epg-context-result-for context
'error
)))))
959 (defun epg--status-NO_SECKEY (context string
)
960 (epg-context-set-result-for
962 (cons (cons 'no-seckey string
)
963 (epg-context-result-for context
'error
))))
965 (defun epg--time-from-seconds (seconds)
966 (let ((number-seconds (string-to-number (concat seconds
".0"))))
967 (cons (floor (/ number-seconds
65536))
968 (floor (mod number-seconds
65536)))))
970 (defun epg--status-ERRSIG (context string
)
971 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
972 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
974 (let ((signature (epg-make-signature 'error
)))
975 (epg-context-set-result-for
979 (epg-context-result-for context
'verify
)))
980 (setf (epg-signature-key-id signature
)
981 (match-string 1 string
))
982 (setf (epg-signature-pubkey-algorithm signature
)
983 (string-to-number (match-string 2 string
)))
984 (setf (epg-signature-digest-algorithm signature
)
985 (string-to-number (match-string 3 string
)))
986 (setf (epg-signature-class signature
)
987 (string-to-number (match-string 4 string
) 16))
988 (setf (epg-signature-creation-time signature
)
989 (epg--time-from-seconds (match-string 5 string
))))))
991 (defun epg--status-VALIDSIG (context string
)
992 (let ((signature (car (epg-context-result-for context
'verify
))))
994 (eq (epg-signature-status signature
) 'good
)
995 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
996 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
999 (setf (epg-signature-fingerprint signature
)
1000 (match-string 1 string
))
1001 (setf (epg-signature-creation-time signature
)
1002 (epg--time-from-seconds (match-string 2 string
)))
1003 (unless (equal (match-string 3 string
) "0")
1004 (setf (epg-signature-expiration-time signature
)
1005 (epg--time-from-seconds (match-string 3 string
))))
1006 (setf (epg-signature-version signature
)
1007 (string-to-number (match-string 4 string
)))
1008 (setf (epg-signature-pubkey-algorithm signature
)
1009 (string-to-number (match-string 5 string
)))
1010 (setf (epg-signature-digest-algorithm signature
)
1011 (string-to-number (match-string 6 string
)))
1012 (setf (epg-signature-class signature
)
1013 (string-to-number (match-string 7 string
) 16)))))
1015 (defun epg--status-TRUST_UNDEFINED (context _string
)
1016 (let ((signature (car (epg-context-result-for context
'verify
))))
1018 (eq (epg-signature-status signature
) 'good
))
1019 (setf (epg-signature-validity signature
) 'undefined
))))
1021 (defun epg--status-TRUST_NEVER (context _string
)
1022 (let ((signature (car (epg-context-result-for context
'verify
))))
1024 (eq (epg-signature-status signature
) 'good
))
1025 (setf (epg-signature-validity signature
) 'never
))))
1027 (defun epg--status-TRUST_MARGINAL (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
) 'marginal
))))
1033 (defun epg--status-TRUST_FULLY (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
) 'full
))))
1039 (defun epg--status-TRUST_ULTIMATE (context _string
)
1040 (let ((signature (car (epg-context-result-for context
'verify
))))
1042 (eq (epg-signature-status signature
) 'good
))
1043 (setf (epg-signature-validity signature
) 'ultimate
))))
1045 (defun epg--status-NOTATION_NAME (context string
)
1046 (let ((signature (car (epg-context-result-for context
'verify
))))
1048 (push (epg-make-sig-notation string nil t nil
)
1049 (epg-signature-notations signature
)))))
1051 (defun epg--status-NOTATION_DATA (context string
)
1052 (let ((signature (car (epg-context-result-for context
'verify
)))
1055 (setq notation
(car (epg-signature-notations signature
))))
1056 (setf (epg-sig-notation-value notation
) string
))))
1058 (defun epg--status-POLICY_URL (context string
)
1059 (let ((signature (car (epg-context-result-for context
'verify
))))
1061 (push (epg-make-sig-notation nil string t nil
)
1062 (epg-signature-notations signature
)))))
1064 (defun epg--status-PROGRESS (context string
)
1065 (if (and (epg-context-progress-callback context
)
1066 (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1068 (funcall (car (epg-context-progress-callback context
))
1070 (match-string 1 string
)
1071 (match-string 2 string
)
1072 (string-to-number (match-string 3 string
))
1073 (string-to-number (match-string 4 string
))
1074 (cdr (epg-context-progress-callback context
)))))
1076 (defun epg--status-ENC_TO (context string
)
1077 (if (string-match "\\`\\([0-9A-Za-z]+\\) \\([0-9]+\\) \\([0-9]+\\)" string
)
1078 (epg-context-set-result-for
1079 context
'encrypted-to
1080 (cons (list (match-string 1 string
)
1081 (string-to-number (match-string 2 string
))
1082 (string-to-number (match-string 3 string
)))
1083 (epg-context-result-for context
'encrypted-to
)))))
1085 (defun epg--status-DECRYPTION_FAILED (context _string
)
1086 (epg-context-set-result-for context
'decryption-failed t
))
1088 (defun epg--status-DECRYPTION_OKAY (context _string
)
1089 (epg-context-set-result-for context
'decryption-okay t
))
1091 (defun epg--status-NODATA (context string
)
1092 (epg-context-set-result-for
1094 (cons (cons 'no-data
(string-to-number string
))
1095 (epg-context-result-for context
'error
))))
1097 (defun epg--status-UNEXPECTED (context string
)
1098 (epg-context-set-result-for
1100 (cons (cons 'unexpected
(string-to-number string
))
1101 (epg-context-result-for context
'error
))))
1103 (defun epg--status-KEYEXPIRED (context string
)
1104 (epg-context-set-result-for
1106 (cons (list 'key-expired
(cons 'expiration-time
1107 (epg--time-from-seconds string
)))
1108 (epg-context-result-for context
'key
))))
1110 (defun epg--status-KEYREVOKED (context _string
)
1111 (epg-context-set-result-for
1113 (cons '(key-revoked)
1114 (epg-context-result-for context
'key
))))
1116 (defun epg--status-BADARMOR (context _string
)
1117 (epg-context-set-result-for
1120 (epg-context-result-for context
'error
))))
1122 (defun epg--status-INV_RECP (context string
)
1123 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string
)
1124 (epg-context-set-result-for
1126 (cons (list 'invalid-recipient
1128 (string-to-number (match-string 1 string
)))
1130 (match-string 2 string
)))
1131 (epg-context-result-for context
'error
)))))
1133 (defun epg--status-INV_SGNR (context string
)
1134 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string
)
1135 (epg-context-set-result-for
1137 (cons (list 'invalid-signer
1139 (string-to-number (match-string 1 string
)))
1141 (match-string 2 string
)))
1142 (epg-context-result-for context
'error
)))))
1144 (defun epg--status-NO_RECP (context _string
)
1145 (epg-context-set-result-for
1147 (cons '(no-recipients)
1148 (epg-context-result-for context
'error
))))
1150 (defun epg--status-NO_SGNR (context _string
)
1151 (epg-context-set-result-for
1154 (epg-context-result-for context
'error
))))
1156 (defun epg--status-DELETE_PROBLEM (context string
)
1157 (if (string-match "\\`\\([0-9]+\\)" string
)
1158 (epg-context-set-result-for
1160 (cons (cons 'delete-problem
1161 (string-to-number (match-string 1 string
)))
1162 (epg-context-result-for context
'error
)))))
1164 (defun epg--status-SIG_CREATED (context string
)
1165 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1166 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string
)
1167 (epg-context-set-result-for
1169 (cons (epg-make-new-signature
1170 (cdr (assq (aref (match-string 1 string
) 0)
1171 epg-new-signature-type-alist
))
1172 (string-to-number (match-string 2 string
))
1173 (string-to-number (match-string 3 string
))
1174 (string-to-number (match-string 4 string
) 16)
1175 (epg--time-from-seconds (match-string 5 string
))
1176 (substring string
(match-end 0)))
1177 (epg-context-result-for context
'sign
)))))
1179 (defun epg--status-KEY_CREATED (context string
)
1180 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string
)
1181 (epg-context-set-result-for
1182 context
'generate-key
1183 (cons (list (cons 'type
(string-to-char (match-string 1 string
)))
1184 (cons 'fingerprint
(match-string 2 string
)))
1185 (epg-context-result-for context
'generate-key
)))))
1187 (defun epg--status-KEY_NOT_CREATED (context _string
)
1188 (epg-context-set-result-for
1190 (cons '(key-not-created)
1191 (epg-context-result-for context
'error
))))
1193 (defun epg--status-IMPORTED (_context string
)
1194 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string
)
1195 (let* ((key-id (match-string 1 string
))
1196 (user-id (match-string 2 string
))
1197 (entry (assoc key-id epg-user-id-alist
)))
1199 (setq user-id
(epg--decode-coding-string
1200 (epg--decode-percent-escape user-id
)
1204 (setcdr entry user-id
)
1205 (setq epg-user-id-alist
(cons (cons key-id user-id
)
1206 epg-user-id-alist
))))))
1208 (defun epg--status-IMPORT_OK (context string
)
1209 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string
)
1210 (let ((reason (string-to-number (match-string 1 string
))))
1211 (epg-context-set-result-for
1212 context
'import-status
1213 (cons (epg-make-import-status (if (match-beginning 2)
1214 (match-string 3 string
))
1216 (/= (logand reason
1) 0)
1217 (/= (logand reason
2) 0)
1218 (/= (logand reason
4) 0)
1219 (/= (logand reason
8) 0)
1220 (/= (logand reason
16) 0))
1221 (epg-context-result-for context
'import-status
))))))
1223 (defun epg--status-IMPORT_PROBLEM (context string
)
1224 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string
)
1225 (epg-context-set-result-for
1226 context
'import-status
1227 (cons (epg-make-import-status
1228 (if (match-beginning 2)
1229 (match-string 3 string
))
1230 (string-to-number (match-string 1 string
)))
1231 (epg-context-result-for context
'import-status
)))))
1233 (defun epg--status-IMPORT_RES (context string
)
1234 (when (string-match "\\`\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1235 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1236 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" string
)
1237 (epg-context-set-result-for
1239 (epg-make-import-result (string-to-number (match-string 1 string
))
1240 (string-to-number (match-string 2 string
))
1241 (string-to-number (match-string 3 string
))
1242 (string-to-number (match-string 4 string
))
1243 (string-to-number (match-string 5 string
))
1244 (string-to-number (match-string 6 string
))
1245 (string-to-number (match-string 7 string
))
1246 (string-to-number (match-string 8 string
))
1247 (string-to-number (match-string 9 string
))
1248 (string-to-number (match-string 10 string
))
1249 (string-to-number (match-string 11 string
))
1250 (string-to-number (match-string 12 string
))
1251 (string-to-number (match-string 13 string
))
1252 (epg-context-result-for context
'import-status
)))
1253 (epg-context-set-result-for context
'import-status nil
)))
1255 (defun epg-passphrase-callback-function (context key-id _handback
)
1256 (declare (obsolete epa-passphrase-callback-function
"23.1"))
1257 (if (eq key-id
'SYM
)
1258 (read-passwd "Passphrase for symmetric encryption: "
1259 (eq (epg-context-operation context
) 'encrypt
))
1261 (if (eq key-id
'PIN
)
1262 "Passphrase for PIN: "
1263 (let ((entry (assoc key-id epg-user-id-alist
)))
1265 (format "Passphrase for %s %s: " key-id
(cdr entry
))
1266 (format "Passphrase for %s: " key-id
)))))))
1268 (defun epg--list-keys-1 (context name mode
)
1269 (let ((args (append (if (epg-context-home-directory context
)
1271 (epg-context-home-directory context
)))
1272 '("--with-colons" "--no-greeting" "--batch"
1273 "--with-fingerprint" "--with-fingerprint")
1274 (unless (eq (epg-context-protocol context
) 'CMS
)
1275 '("--fixed-list-mode"))))
1276 (list-keys-option (if (memq mode
'(t secret
))
1277 "--list-secret-keys"
1278 (if (memq mode
'(nil public
))
1281 (coding-system-for-read 'binary
)
1282 keys string field index
)
1285 (unless (listp name
)
1286 (setq name
(list name
)))
1288 (setq args
(append args
(list list-keys-option
(car name
)))
1290 (setq args
(append args
(list list-keys-option
))))
1292 (apply #'call-process
1293 (epg-context-program context
)
1294 nil
(list t nil
) nil args
)
1295 (goto-char (point-min))
1296 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t
)
1297 (setq keys
(cons (make-vector 15 nil
) keys
)
1298 string
(match-string 0)
1301 (while (and (< field
(length (car keys
)))
1303 (string-match "\\([^:]+\\)?:" string index
)))
1304 (setq index
(match-end 0))
1305 (aset (car keys
) field
(match-string 1 string
))
1306 (setq field
(1+ field
))))
1309 (defun epg--make-sub-key-1 (line)
1312 (cdr (assq (string-to-char (aref line
1)) epg-key-validity-alist
)))
1314 (mapcar (lambda (char) (cdr (assq char epg-key-capability-alist
)))
1316 (member (aref line
0) '("sec" "ssb"))
1317 (string-to-number (aref line
3))
1318 (string-to-number (aref line
2))
1320 (epg--time-from-seconds (aref line
5))
1322 (epg--time-from-seconds (aref line
6)))))
1324 (defun epg-list-keys (context &optional name mode
)
1325 "Return a list of epg-key objects matched with NAME.
1326 If MODE is nil or `public', only public keyring should be searched.
1327 If MODE is t or `secret', only secret keyring should be searched.
1328 Otherwise, only public keyring should be searched and the key
1329 signatures should be included.
1330 NAME is either a string or a list of strings."
1331 (let ((lines (epg--list-keys-1 context name mode
))
1332 keys cert pointer pointer-1 index string
)
1335 ((member (aref (car lines
) 0) '("pub" "sec" "crt" "crs"))
1336 (setq cert
(member (aref (car lines
) 0) '("crt" "crs"))
1337 keys
(cons (epg-make-key
1338 (if (aref (car lines
) 8)
1339 (cdr (assq (string-to-char (aref (car lines
) 8))
1340 epg-key-validity-alist
))))
1342 (push (epg--make-sub-key-1 (car lines
))
1343 (epg-key-sub-key-list (car keys
))))
1344 ((member (aref (car lines
) 0) '("sub" "ssb"))
1345 (push (epg--make-sub-key-1 (car lines
))
1346 (epg-key-sub-key-list (car keys
))))
1347 ((equal (aref (car lines
) 0) "uid")
1348 ;; Decode the UID name as a backslash escaped UTF-8 string,
1349 ;; generated by GnuPG/GpgSM.
1350 (setq string
(copy-sequence (aref (car lines
) 9))
1352 (while (string-match "\"" string index
)
1353 (setq string
(replace-match "\\\"" t t string
)
1354 index
(1+ (match-end 0))))
1356 (setq string
(epg--decode-coding-string
1357 (car (read-from-string (concat "\"" string
"\"")))
1360 (setq string
(aref (car lines
) 9))))
1361 (push (epg-make-user-id
1362 (if (aref (car lines
) 1)
1363 (cdr (assq (string-to-char (aref (car lines
) 1))
1364 epg-key-validity-alist
)))
1367 (epg-dn-from-string string
)
1370 (epg-key-user-id-list (car keys
))))
1371 ((equal (aref (car lines
) 0) "fpr")
1372 (setf (epg-sub-key-fingerprint (car (epg-key-sub-key-list (car keys
))))
1373 (aref (car lines
) 9)))
1374 ((equal (aref (car lines
) 0) "sig")
1376 (epg-make-key-signature
1377 (if (aref (car lines
) 1)
1378 (cdr (assq (string-to-char (aref (car lines
) 1))
1379 epg-key-validity-alist
)))
1380 (string-to-number (aref (car lines
) 3))
1381 (aref (car lines
) 4)
1382 (epg--time-from-seconds (aref (car lines
) 5))
1383 (epg--time-from-seconds (aref (car lines
) 6))
1384 (aref (car lines
) 9)
1385 (string-to-number (aref (car lines
) 10) 16)
1386 (eq (aref (aref (car lines
) 10) 2) ?x
))
1387 (epg-user-id-signature-list
1388 (car (epg-key-user-id-list (car keys
)))))))
1389 (setq lines
(cdr lines
)))
1390 (setq keys
(nreverse keys
)
1393 (epg--gv-nreverse (epg-key-sub-key-list (car pointer
)))
1394 (setq pointer-1
(epg--gv-nreverse (epg-key-user-id-list (car pointer
))))
1396 (epg--gv-nreverse (epg-user-id-signature-list (car pointer-1
)))
1397 (setq pointer-1
(cdr pointer-1
)))
1398 (setq pointer
(cdr pointer
)))
1402 (if (fboundp 'make-temp-file
)
1403 (defalias 'epg--make-temp-file
'make-temp-file
)
1404 (defvar temporary-file-directory
)
1405 ;; stolen from poe.el.
1406 (defun epg--make-temp-file (prefix)
1407 "Create a temporary file.
1408 The returned file name (created by appending some random characters at the end
1409 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1410 is guaranteed to point to a newly created empty file.
1411 You can then use `write-region' to write new data into the file."
1412 (let ((orig-modes (default-file-modes))
1414 (setq prefix
(expand-file-name prefix
1415 (if (featurep 'xemacs
)
1417 temporary-file-directory
)))
1420 ;; First, create a temporary directory.
1421 (set-default-file-modes #o700
)
1422 (while (condition-case ()
1424 (setq tempdir
(make-temp-name
1426 (file-name-directory prefix
)
1428 ;; return nil or signal an error.
1429 (make-directory tempdir
))
1431 (file-already-exists t
)))
1432 ;; Second, create a temporary file in the tempdir.
1433 ;; There *is* a race condition between `make-temp-name'
1434 ;; and `write-region', but we don't care it since we are
1435 ;; in a private directory now.
1436 (setq tempfile
(make-temp-name (concat tempdir
"/EMU")))
1437 (write-region "" nil tempfile nil
'silent
)
1438 ;; Finally, make a hard-link from the tempfile.
1439 (while (condition-case ()
1441 (setq file
(make-temp-name prefix
))
1442 ;; return nil or signal an error.
1443 (add-name-to-file tempfile file
))
1445 (file-already-exists t
)))
1447 (set-default-file-modes orig-modes
)
1448 ;; Cleanup the tempfile.
1450 (file-exists-p tempfile
)
1451 (delete-file tempfile
))
1452 ;; Cleanup the tempdir.
1454 (file-directory-p tempdir
)
1455 (delete-directory tempdir
)))))))
1457 (defun epg--args-from-sig-notations (notations)
1461 (if (and (epg-sig-notation-name notation
)
1462 (not (epg-sig-notation-human-readable notation
)))
1463 (error "Unreadable"))
1464 (if (epg-sig-notation-name notation
)
1465 (list "--sig-notation"
1466 (if (epg-sig-notation-critical notation
)
1467 (concat "!" (epg-sig-notation-name notation
)
1468 "=" (epg-sig-notation-value notation
))
1469 (concat (epg-sig-notation-name notation
)
1470 "=" (epg-sig-notation-value notation
))))
1471 (list "--sig-policy-url"
1472 (if (epg-sig-notation-critical notation
)
1473 (concat "!" (epg-sig-notation-value notation
))
1474 (epg-sig-notation-value notation
)))))
1477 (defun epg-cancel (context)
1478 (if (buffer-live-p (process-buffer (epg-context-process context
)))
1479 (with-current-buffer (process-buffer (epg-context-process context
))
1480 (epg-context-set-result-for
1483 (epg-context-result-for epg-context
'error
)))))
1484 (if (eq (process-status (epg-context-process context
)) 'run
)
1485 (delete-process (epg-context-process context
))))
1487 (defun epg-start-decrypt (context cipher
)
1488 "Initiate a decrypt operation on CIPHER.
1489 CIPHER must be a file data object.
1491 If you use this function, you will need to wait for the completion of
1492 `epg-gpg-program' by using `epg-wait-for-completion' and call
1493 `epg-reset' to clear a temporary output file.
1494 If you are unsure, use synchronous version of this function
1495 `epg-decrypt-file' or `epg-decrypt-string' instead."
1496 (unless (epg-data-file cipher
)
1497 (error "Not a file"))
1498 (setf (epg-context-operation context
) 'decrypt
)
1499 (setf (epg-context-result context
) nil
)
1500 (epg--start context
(list "--decrypt" "--" (epg-data-file cipher
)))
1501 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1502 (unless (eq (epg-context-protocol context
) 'CMS
)
1503 (epg-wait-for-status context
'("BEGIN_DECRYPTION"))))
1505 (defun epg--check-error-for-decrypt (context)
1506 (let ((errors (epg-context-result-for context
'error
)))
1507 (if (epg-context-result-for context
'decryption-failed
)
1509 (list "Decryption failed" (epg-errors-to-string errors
))))
1510 (unless (epg-context-result-for context
'decryption-okay
)
1512 (list "Can't decrypt" (epg-errors-to-string errors
))))))
1514 (defun epg-decrypt-file (context cipher plain
)
1515 "Decrypt a file CIPHER and store the result to a file PLAIN.
1516 If PLAIN is nil, it returns the result as a string."
1519 (setf (epg-context-output-file context
)
1520 (or plain
(epg--make-temp-file "epg-output")))
1521 (epg-start-decrypt context
(epg-make-data-from-file cipher
))
1522 (epg-wait-for-completion context
)
1523 (epg--check-error-for-decrypt context
)
1525 (epg-read-output context
)))
1527 (epg-delete-output-file context
))
1528 (epg-reset context
)))
1530 (defun epg-decrypt-string (context cipher
)
1531 "Decrypt a string CIPHER and return the plain text."
1532 (let ((input-file (epg--make-temp-file "epg-input"))
1533 (coding-system-for-write 'binary
))
1536 (write-region cipher nil input-file nil
'quiet
)
1537 (setf (epg-context-output-file context
)
1538 (epg--make-temp-file "epg-output"))
1539 (epg-start-decrypt context
(epg-make-data-from-file input-file
))
1540 (epg-wait-for-completion context
)
1541 (epg--check-error-for-decrypt context
)
1542 (epg-read-output context
))
1543 (epg-delete-output-file context
)
1544 (if (file-exists-p input-file
)
1545 (delete-file input-file
))
1546 (epg-reset context
))))
1548 (defun epg-start-verify (context signature
&optional signed-text
)
1549 "Initiate a verify operation on SIGNATURE.
1550 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1552 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1553 For a normal or a cleartext signature, SIGNED-TEXT should be nil.
1555 If you use this function, you will need to wait for the completion of
1556 `epg-gpg-program' by using `epg-wait-for-completion' and call
1557 `epg-reset' to clear a temporary output file.
1558 If you are unsure, use synchronous version of this function
1559 `epg-verify-file' or `epg-verify-string' instead."
1560 (setf (epg-context-operation context
) 'verify
)
1561 (setf (epg-context-result context
) nil
)
1563 ;; Detached signature.
1564 (if (epg-data-file signed-text
)
1565 (epg--start context
(list "--verify" "--" (epg-data-file signature
)
1566 (epg-data-file signed-text
)))
1567 (epg--start context
(list "--verify" "--" (epg-data-file signature
)
1569 (if (eq (process-status (epg-context-process context
)) 'run
)
1570 (process-send-string (epg-context-process context
)
1571 (epg-data-string signed-text
)))
1572 (if (eq (process-status (epg-context-process context
)) 'run
)
1573 (process-send-eof (epg-context-process context
))))
1574 ;; Normal (or cleartext) signature.
1575 (if (epg-data-file signature
)
1576 (epg--start context
(if (eq (epg-context-protocol context
) 'CMS
)
1577 (list "--verify" "--" (epg-data-file signature
))
1578 (list "--" (epg-data-file signature
))))
1579 (epg--start context
(if (eq (epg-context-protocol context
) 'CMS
)
1582 (if (eq (process-status (epg-context-process context
)) 'run
)
1583 (process-send-string (epg-context-process context
)
1584 (epg-data-string signature
)))
1585 (if (eq (process-status (epg-context-process context
)) 'run
)
1586 (process-send-eof (epg-context-process context
))))))
1588 (defun epg-verify-file (context signature
&optional signed-text plain
)
1589 "Verify a file SIGNATURE.
1590 SIGNED-TEXT and PLAIN are also a file if they are specified.
1592 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1593 string. For a normal or a cleartext signature, SIGNED-TEXT should be
1594 nil. In the latter case, if PLAIN is specified, the plaintext is
1595 stored into the file after successful verification.
1597 Note that this function does not return verification result as t
1598 or nil, nor signal error on failure. That's a design decision to
1599 handle the case where SIGNATURE has multiple signature.
1601 To check the verification results, use `epg-context-result-for' as follows:
1603 \(epg-context-result-for context \\='verify)
1605 which will return a list of `epg-signature' object."
1608 (setf (epg-context-output-file context
)
1609 (or plain
(epg--make-temp-file "epg-output")))
1611 (epg-start-verify context
1612 (epg-make-data-from-file signature
)
1613 (epg-make-data-from-file signed-text
))
1614 (epg-start-verify context
1615 (epg-make-data-from-file signature
)))
1616 (epg-wait-for-completion context
)
1618 (epg-read-output context
)))
1620 (epg-delete-output-file context
))
1621 (epg-reset context
)))
1623 (defun epg-verify-string (context signature
&optional signed-text
)
1624 "Verify a string SIGNATURE.
1625 SIGNED-TEXT is a string if it is specified.
1627 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1628 string. For a normal or a cleartext signature, SIGNED-TEXT should be
1629 nil. In the latter case, this function returns the plaintext after
1630 successful verification.
1632 Note that this function does not return verification result as t
1633 or nil, nor signal error on failure. That's a design decision to
1634 handle the case where SIGNATURE has multiple signature.
1636 To check the verification results, use `epg-context-result-for' as follows:
1638 \(epg-context-result-for context \\='verify)
1640 which will return a list of `epg-signature' object."
1641 (let ((coding-system-for-write 'binary
)
1645 (setf (epg-context-output-file context
)
1646 (epg--make-temp-file "epg-output"))
1649 (setq input-file
(epg--make-temp-file "epg-signature"))
1650 (write-region signature nil input-file nil
'quiet
)
1651 (epg-start-verify context
1652 (epg-make-data-from-file input-file
)
1653 (epg-make-data-from-string signed-text
)))
1654 (epg-start-verify context
(epg-make-data-from-string signature
)))
1655 (epg-wait-for-completion context
)
1656 (epg-read-output context
))
1657 (epg-delete-output-file context
)
1659 (file-exists-p input-file
))
1660 (delete-file input-file
))
1661 (epg-reset context
))))
1663 (defun epg-start-sign (context plain
&optional mode
)
1664 "Initiate a sign operation on PLAIN.
1665 PLAIN is a data object.
1667 If optional 3rd argument MODE is t or `detached', it makes a detached signature.
1668 If it is nil or `normal', it makes a normal signature.
1669 Otherwise, it makes a cleartext signature.
1671 If you use this function, you will need to wait for the completion of
1672 `epg-gpg-program' by using `epg-wait-for-completion' and call
1673 `epg-reset' to clear a temporary output file.
1674 If you are unsure, use synchronous version of this function
1675 `epg-sign-file' or `epg-sign-string' instead."
1676 (setf (epg-context-operation context
) 'sign
)
1677 (setf (epg-context-result context
) nil
)
1678 (unless (memq mode
'(t detached nil normal
)) ;i.e. cleartext
1679 (setf (epg-context-armor context
) nil
)
1680 (setf (epg-context-textmode context
) nil
))
1682 (append (list (if (memq mode
'(t detached
))
1684 (if (memq mode
'(nil normal
))
1692 (car (epg-key-sub-key-list signer
)))))
1693 (epg-context-signers context
)))
1694 (epg--args-from-sig-notations
1695 (epg-context-sig-notations context
))
1696 (if (epg-data-file plain
)
1697 (list "--" (epg-data-file plain
)))))
1698 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1699 (unless (eq (epg-context-protocol context
) 'CMS
)
1700 (epg-wait-for-status context
'("BEGIN_SIGNING")))
1701 (when (epg-data-string plain
)
1702 (if (eq (process-status (epg-context-process context
)) 'run
)
1703 (process-send-string (epg-context-process context
)
1704 (epg-data-string plain
)))
1705 (if (eq (process-status (epg-context-process context
)) 'run
)
1706 (process-send-eof (epg-context-process context
)))))
1708 (defun epg-sign-file (context plain signature
&optional mode
)
1709 "Sign a file PLAIN and store the result to a file SIGNATURE.
1710 If SIGNATURE is nil, it returns the result as a string.
1711 If optional 3rd argument MODE is t or `detached', it makes a detached signature.
1712 If it is nil or `normal', it makes a normal signature.
1713 Otherwise, it makes a cleartext signature."
1716 (setf (epg-context-output-file context
)
1717 (or signature
(epg--make-temp-file "epg-output")))
1718 (epg-start-sign context
(epg-make-data-from-file plain
) mode
)
1719 (epg-wait-for-completion context
)
1720 (unless (epg-context-result-for context
'sign
)
1721 (let ((errors (epg-context-result-for context
'error
)))
1723 (list "Sign failed" (epg-errors-to-string errors
)))))
1725 (epg-read-output context
)))
1727 (epg-delete-output-file context
))
1728 (epg-reset context
)))
1730 (defun epg-sign-string (context plain
&optional mode
)
1731 "Sign a string PLAIN and return the output as string.
1732 If optional 3rd argument MODE is t or `detached', it makes a detached signature.
1733 If it is nil or `normal', it makes a normal signature.
1734 Otherwise, it makes a cleartext signature."
1736 (unless (eq (epg-context-protocol context
) 'CMS
)
1737 (epg--make-temp-file "epg-input")))
1738 (coding-system-for-write 'binary
))
1741 (setf (epg-context-output-file context
)
1742 (epg--make-temp-file "epg-output"))
1744 (write-region plain nil input-file nil
'quiet
))
1745 (epg-start-sign context
1747 (epg-make-data-from-file input-file
)
1748 (epg-make-data-from-string plain
))
1750 (epg-wait-for-completion context
)
1751 (unless (epg-context-result-for context
'sign
)
1752 (if (epg-context-result-for context
'error
)
1753 (let ((errors (epg-context-result-for context
'error
)))
1755 (list "Sign failed" (epg-errors-to-string errors
))))))
1756 (epg-read-output context
))
1757 (epg-delete-output-file context
)
1759 (delete-file input-file
))
1760 (epg-reset context
))))
1762 (defun epg-start-encrypt (context plain recipients
1763 &optional sign always-trust
)
1764 "Initiate an encrypt operation on PLAIN.
1765 PLAIN is a data object.
1766 If RECIPIENTS is nil, it performs symmetric encryption.
1768 If you use this function, you will need to wait for the completion of
1769 `epg-gpg-program' by using `epg-wait-for-completion' and call
1770 `epg-reset' to clear a temporary output file.
1771 If you are unsure, use synchronous version of this function
1772 `epg-encrypt-file' or `epg-encrypt-string' instead."
1773 (setf (epg-context-operation context
) 'encrypt
)
1774 (setf (epg-context-result context
) nil
)
1776 (append (if always-trust
'("--always-trust"))
1777 (if recipients
'("--encrypt") '("--symmetric"))
1778 (if sign
'("--sign"))
1785 (car (epg-key-sub-key-list
1787 (epg-context-signers context
))))
1789 (epg--args-from-sig-notations
1790 (epg-context-sig-notations context
)))
1796 (car (epg-key-sub-key-list recipient
)))))
1798 (if (epg-data-file plain
)
1799 (list "--" (epg-data-file plain
)))))
1800 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1801 (unless (eq (epg-context-protocol context
) 'CMS
)
1802 (epg-wait-for-status context
1803 (if sign
'("BEGIN_SIGNING") '("BEGIN_ENCRYPTION"))))
1804 (when (epg-data-string plain
)
1805 (if (eq (process-status (epg-context-process context
)) 'run
)
1806 (process-send-string (epg-context-process context
)
1807 (epg-data-string plain
)))
1808 (if (eq (process-status (epg-context-process context
)) 'run
)
1809 (process-send-eof (epg-context-process context
)))))
1811 (defun epg-encrypt-file (context plain recipients
1812 cipher
&optional sign always-trust
)
1813 "Encrypt a file PLAIN and store the result to a file CIPHER.
1814 If CIPHER is nil, it returns the result as a string.
1815 If RECIPIENTS is nil, it performs symmetric encryption."
1818 (setf (epg-context-output-file context
)
1819 (or cipher
(epg--make-temp-file "epg-output")))
1820 (epg-start-encrypt context
(epg-make-data-from-file plain
)
1821 recipients sign always-trust
)
1822 (epg-wait-for-completion context
)
1823 (let ((errors (epg-context-result-for context
'error
)))
1825 (not (epg-context-result-for context
'sign
)))
1827 (list "Sign failed" (epg-errors-to-string errors
))))
1830 (list "Encrypt failed" (epg-errors-to-string errors
)))))
1832 (epg-read-output context
)))
1834 (epg-delete-output-file context
))
1835 (epg-reset context
)))
1837 (defun epg-encrypt-string (context plain recipients
1838 &optional sign always-trust
)
1839 "Encrypt a string PLAIN.
1840 If RECIPIENTS is nil, it performs symmetric encryption."
1842 (unless (or (not sign
)
1843 (eq (epg-context-protocol context
) 'CMS
))
1844 (epg--make-temp-file "epg-input")))
1845 (coding-system-for-write 'binary
))
1848 (setf (epg-context-output-file context
)
1849 (epg--make-temp-file "epg-output"))
1851 (write-region plain nil input-file nil
'quiet
))
1852 (epg-start-encrypt context
1854 (epg-make-data-from-file input-file
)
1855 (epg-make-data-from-string plain
))
1856 recipients sign always-trust
)
1857 (epg-wait-for-completion context
)
1858 (let ((errors (epg-context-result-for context
'error
)))
1860 (not (epg-context-result-for context
'sign
)))
1862 (list "Sign failed" (epg-errors-to-string errors
))))
1865 (list "Encrypt failed" (epg-errors-to-string errors
)))))
1866 (epg-read-output context
))
1867 (epg-delete-output-file context
)
1869 (delete-file input-file
))
1870 (epg-reset context
))))
1872 (defun epg-start-export-keys (context keys
)
1873 "Initiate an export keys operation.
1875 If you use this function, you will need to wait for the completion of
1876 `epg-gpg-program' by using `epg-wait-for-completion' and call
1877 `epg-reset' to clear a temporary output file.
1878 If you are unsure, use synchronous version of this function
1879 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1880 (setf (epg-context-operation context
) 'export-keys
)
1881 (setf (epg-context-result context
) nil
)
1882 (epg--start context
(cons "--export"
1886 (car (epg-key-sub-key-list key
))))
1889 (defun epg-export-keys-to-file (context keys file
)
1890 "Extract public KEYS."
1893 (setf (epg-context-output-file context
)
1894 (or file
(epg--make-temp-file "epg-output")))
1895 (epg-start-export-keys context keys
)
1896 (epg-wait-for-completion context
)
1897 (let ((errors (epg-context-result-for context
'error
)))
1900 (list "Export keys failed"
1901 (epg-errors-to-string errors
)))))
1903 (epg-read-output context
)))
1905 (epg-delete-output-file context
))
1906 (epg-reset context
)))
1908 (defun epg-export-keys-to-string (context keys
)
1909 "Extract public KEYS and return them as a string."
1910 (epg-export-keys-to-file context keys nil
))
1912 (defun epg-start-import-keys (context keys
)
1913 "Initiate an import keys operation.
1914 KEYS is a data object.
1916 If you use this function, you will need to wait for the completion of
1917 `epg-gpg-program' by using `epg-wait-for-completion' and call
1918 `epg-reset' to clear a temporary output file.
1919 If you are unsure, use synchronous version of this function
1920 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1921 (setf (epg-context-operation context
) 'import-keys
)
1922 (setf (epg-context-result context
) nil
)
1923 (epg--start context
(if (epg-data-file keys
)
1924 (list "--import" "--" (epg-data-file keys
))
1926 (when (epg-data-string keys
)
1927 (if (eq (process-status (epg-context-process context
)) 'run
)
1928 (process-send-string (epg-context-process context
)
1929 (epg-data-string keys
)))
1930 (if (eq (process-status (epg-context-process context
)) 'run
)
1931 (process-send-eof (epg-context-process context
)))))
1933 (defun epg--import-keys-1 (context keys
)
1936 (epg-start-import-keys context keys
)
1937 (epg-wait-for-completion context
)
1938 (let ((errors (epg-context-result-for context
'error
)))
1941 (list "Import keys failed"
1942 (epg-errors-to-string errors
))))))
1943 (epg-reset context
)))
1945 (defun epg-import-keys-from-file (context keys
)
1946 "Add keys from a file KEYS."
1947 (epg--import-keys-1 context
(epg-make-data-from-file keys
)))
1949 (defun epg-import-keys-from-string (context keys
)
1950 "Add keys from a string KEYS."
1951 (epg--import-keys-1 context
(epg-make-data-from-string keys
)))
1953 (defun epg-start-receive-keys (context key-id-list
)
1954 "Initiate a receive key operation.
1955 KEY-ID-LIST is a list of key IDs.
1957 If you use this function, you will need to wait for the completion of
1958 `epg-gpg-program' by using `epg-wait-for-completion' and call
1959 `epg-reset' to clear a temporary output file.
1960 If you are unsure, use synchronous version of this function
1961 `epg-receive-keys' instead."
1962 (setf (epg-context-operation context
) 'receive-keys
)
1963 (setf (epg-context-result context
) nil
)
1964 (epg--start context
(cons "--recv-keys" key-id-list
)))
1966 (defun epg-receive-keys (context keys
)
1967 "Add keys from server.
1968 KEYS is a list of key IDs"
1971 (epg-start-receive-keys context keys
)
1972 (epg-wait-for-completion context
)
1973 (let ((errors (epg-context-result-for context
'error
)))
1976 (list "Receive keys failed"
1977 (epg-errors-to-string errors
))))))
1978 (epg-reset context
)))
1980 (defalias 'epg-import-keys-from-server
'epg-receive-keys
)
1982 (defun epg-start-delete-keys (context keys
&optional allow-secret
)
1983 "Initiate a delete keys operation.
1985 If you use this function, you will need to wait for the completion of
1986 `epg-gpg-program' by using `epg-wait-for-completion' and call
1987 `epg-reset' to clear a temporary output file.
1988 If you are unsure, use synchronous version of this function
1989 `epg-delete-keys' instead."
1990 (setf (epg-context-operation context
) 'delete-keys
)
1991 (setf (epg-context-result context
) nil
)
1992 (epg--start context
(cons (if allow-secret
1993 "--delete-secret-key"
1998 (car (epg-key-sub-key-list key
))))
2001 (defun epg-delete-keys (context keys
&optional allow-secret
)
2002 "Delete KEYS from the key ring."
2005 (epg-start-delete-keys context keys allow-secret
)
2006 (epg-wait-for-completion context
)
2007 (let ((errors (epg-context-result-for context
'error
)))
2010 (list "Delete keys failed"
2011 (epg-errors-to-string errors
))))))
2012 (epg-reset context
)))
2014 (defun epg-start-sign-keys (context keys
&optional local
)
2015 "Initiate a sign keys operation.
2017 If you use this function, you will need to wait for the completion of
2018 `epg-gpg-program' by using `epg-wait-for-completion' and call
2019 `epg-reset' to clear a temporary output file.
2020 If you are unsure, use synchronous version of this function
2021 `epg-sign-keys' instead."
2022 (declare (obsolete nil
"23.1"))
2023 (setf (epg-context-operation context
) 'sign-keys
)
2024 (setf (epg-context-result context
) nil
)
2025 (epg--start context
(cons (if local
2031 (car (epg-key-sub-key-list key
))))
2034 (defun epg-sign-keys (context keys
&optional local
)
2035 "Sign KEYS from the key ring."
2036 (declare (obsolete nil
"23.1"))
2039 (epg-start-sign-keys context keys local
)
2040 (epg-wait-for-completion context
)
2041 (let ((errors (epg-context-result-for context
'error
)))
2044 (list "Sign keys failed"
2045 (epg-errors-to-string errors
))))))
2046 (epg-reset context
)))
2048 (defun epg-start-generate-key (context parameters
)
2049 "Initiate a key generation.
2050 PARAMETERS is a string which specifies parameters of the generated key.
2051 See Info node `(gnupg) Unattended GPG key generation' in the
2052 GnuPG manual for the format.
2054 If you use this function, you will need to wait for the completion of
2055 `epg-gpg-program' by using `epg-wait-for-completion' and call
2056 `epg-reset' to clear a temporary output file.
2057 If you are unsure, use synchronous version of this function
2058 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2059 (setf (epg-context-operation context
) 'generate-key
)
2060 (setf (epg-context-result context
) nil
)
2061 (if (epg-data-file parameters
)
2062 (epg--start context
(list "--batch" "--gen-key" "--"
2063 (epg-data-file parameters
)))
2064 (epg--start context
'("--batch" "--gen-key"))
2065 (if (eq (process-status (epg-context-process context
)) 'run
)
2066 (process-send-string (epg-context-process context
)
2067 (epg-data-string parameters
)))
2068 (if (eq (process-status (epg-context-process context
)) 'run
)
2069 (process-send-eof (epg-context-process context
)))))
2071 (defun epg-generate-key-from-file (context parameters
)
2072 "Generate a new key pair.
2073 PARAMETERS is a file which tells how to create the key."
2076 (epg-start-generate-key context
(epg-make-data-from-file parameters
))
2077 (epg-wait-for-completion context
)
2078 (let ((errors (epg-context-result-for context
'error
)))
2081 (list "Generate key failed"
2082 (epg-errors-to-string errors
))))))
2083 (epg-reset context
)))
2085 (defun epg-generate-key-from-string (context parameters
)
2086 "Generate a new key pair.
2087 PARAMETERS is a string which tells how to create the key."
2090 (epg-start-generate-key context
(epg-make-data-from-string parameters
))
2091 (epg-wait-for-completion context
)
2092 (let ((errors (epg-context-result-for context
'error
)))
2095 (list "Generate key failed"
2096 (epg-errors-to-string errors
))))))
2097 (epg-reset context
)))
2099 (defun epg-start-edit-key (context key edit-callback handback
)
2100 "Initiate an edit operation on KEY.
2102 EDIT-CALLBACK is called from process filter and takes 3
2103 arguments: the context, a status, an argument string, and the
2106 If you use this function, you will need to wait for the completion of
2107 `epg-gpg-program' by using `epg-wait-for-completion' and call
2108 `epg-reset' to clear a temporary output file.
2109 If you are unsure, use synchronous version of this function
2110 `epg-edit-key' instead."
2111 (setf (epg-context-operation context
) 'edit-key
)
2112 (setf (epg-context-result context
) nil
)
2113 (setf (epg-context-edit-callback context
) (cons edit-callback handback
))
2114 (epg--start context
(list "--edit-key"
2116 (car (epg-key-sub-key-list key
))))))
2118 (defun epg-edit-key (context key edit-callback handback
)
2119 "Edit KEY in the keyring."
2122 (epg-start-edit-key context key edit-callback handback
)
2123 (epg-wait-for-completion context
)
2124 (let ((errors (epg-context-result-for context
'error
)))
2127 (list "Edit key failed"
2128 (epg-errors-to-string errors
))))))
2129 (epg-reset context
)))
2131 (defun epg--decode-percent-escape (string)
2133 (while (string-match "%\\(\\(%\\)\\|\\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2135 (if (match-beginning 2)
2136 (setq string
(replace-match "%" t t string
)
2137 index
(1- (match-end 0)))
2138 (setq string
(replace-match
2139 (string (string-to-number (match-string 3 string
) 16))
2141 index
(- (match-end 0) 2))))
2144 (defun epg--decode-hexstring (string)
2146 (while (eq index
(string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index
))
2147 (setq string
(replace-match (string (string-to-number
2148 (match-string 0 string
) 16))
2150 index
(1- (match-end 0))))
2153 (defun epg--decode-quotedstring (string)
2155 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2156 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2158 (if (match-beginning 2)
2159 (setq string
(replace-match "\\2" t nil string
)
2160 index
(1- (match-end 0)))
2161 (if (match-beginning 3)
2162 (setq string
(replace-match (string (string-to-number
2163 (match-string 0 string
) 16))
2165 index
(- (match-end 0) 2)))))
2168 (defun epg-dn-from-string (string)
2169 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2170 The return value is an alist mapping from types to values."
2172 (length (length string
))
2173 alist type value group
)
2174 (while (< index length
)
2175 (if (eq index
(string-match "[ \t\n\r]*" string index
))
2176 (setq index
(match-end 0)))
2177 (if (eq index
(string-match
2178 "\\([0-9]+\\(\\.[0-9]+\\)*\\)[ \t\n\r]*=[ \t\n\r]*"
2180 (setq type
(match-string 1 string
)
2181 index
(match-end 0))
2182 (if (eq index
(string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2184 (setq type
(match-string 1 string
)
2185 index
(match-end 0))))
2187 (error "Invalid type"))
2188 (if (eq index
(string-match
2189 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2191 (setq index
(match-end 0)
2192 value
(epg--decode-quotedstring (match-string 0 string
)))
2193 (if (eq index
(string-match "#\\([0-9A-Fa-f]+\\)" string index
))
2194 (setq index
(match-end 0)
2195 value
(epg--decode-hexstring (match-string 1 string
)))
2196 (if (eq index
(string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2198 (setq index
(match-end 0)
2199 value
(epg--decode-quotedstring
2200 (match-string 0 string
))))))
2202 (if (stringp (car (car alist
)))
2203 (setcar alist
(list (cons type value
) (car alist
)))
2204 (setcar alist
(cons (cons type value
) (car alist
))))
2205 (if (consp (car (car alist
)))
2206 (setcar alist
(nreverse (car alist
))))
2207 (setq alist
(cons (cons type value
) alist
)
2210 (if (eq index
(string-match "[ \t\n\r]*\\([,;+]\\)" string index
))
2211 (setq index
(match-end 0)
2212 group
(eq (aref string
(match-beginning 1)) ?
+))))
2215 (defun epg-decode-dn (alist)
2216 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2217 Type names are resolved using `epg-dn-type-alist'."
2220 (if (stringp (car rdn
))
2221 (let ((entry (assoc (car rdn
) epg-dn-type-alist
)))
2223 (format "%s=%s" (cdr entry
) (cdr rdn
))
2224 (format "%s=%s" (car rdn
) (cdr rdn
))))
2225 (concat "(" (epg-decode-dn rdn
) ")")))
2231 ;;; epg.el ends here