1 ;;; epg.el --- the EasyPG Library -*- lexical-binding: t -*-
2 ;; Copyright (C) 1999-2000, 2002-2014 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
)
43 (defvar epg-error-output nil
)
45 ;; from gnupg/include/cipher.h
46 (defconst epg-cipher-algorithm-alist
60 ;; from gnupg/include/cipher.h
61 (defconst epg-pubkey-algorithm-alist
69 ;; from gnupg/include/cipher.h
70 (defconst epg-digest-algorithm-alist
79 ;; from gnupg/include/cipher.h
80 (defconst epg-compress-algorithm-alist
86 (defconst epg-invalid-recipients-reason-alist
87 '((0 .
"No specific reason given")
89 (2 .
"Ambiguous specification")
90 (3 .
"Wrong key usage")
95 (8 .
"Policy mismatch")
96 (9 .
"Not a secret key")
97 (10 .
"Key not trusted")))
99 (defconst epg-delete-problem-reason-alist
100 '((1 .
"No such key")
101 (2 .
"Must delete secret key first")
102 (3 .
"Ambiguous specification")))
104 (defconst epg-import-ok-reason-alist
105 '((0 .
"Not actually changed")
106 (1 .
"Entirely new key")
108 (4 .
"New signatures")
110 (16 .
"Contains private key")))
112 (defconst epg-import-problem-reason-alist
113 '((0 .
"No specific reason given")
114 (1 .
"Invalid Certificate")
115 (2 .
"Issuer Certificate missing")
116 (3 .
"Certificate Chain too long")
117 (4 .
"Error storing certificate")))
119 (defconst epg-no-data-reason-alist
120 '((1 .
"No armored data")
121 (2 .
"Expected a packet but did not found one")
122 (3 .
"Invalid packet found, this may indicate a non OpenPGP message")
123 (4 .
"Signature expected but not found")))
125 (defconst epg-unexpected-reason-alist nil
)
127 (defvar epg-key-validity-alist
140 (defvar epg-key-capability-alist
144 (?a . authentication
)
147 (defvar epg-new-signature-type-alist
152 (defvar epg-dn-type-alist
153 '(("1.2.840.113549.1.9.1" .
"EMail")
157 ("0.2.262.1.10.7.20" .
"NameDistinguisher")
158 ("2.5.4.16" .
"ADDR")
161 ("2.5.4.17" .
"PostalCode")
162 ("2.5.4.65" .
"Pseudo")
163 ("2.5.4.5" .
"SerialNumber")))
165 (defvar epg-prompt-alist nil
)
167 (define-error 'epg-error
"GPG error")
169 (cl-defstruct (epg-data
171 (:constructor epg-make-data-from-file
(file))
172 (:constructor epg-make-data-from-string
(string))
175 (file nil
:read-only t
)
176 (string nil
:read-only t
))
178 (defmacro epg--gv-nreverse
(place)
179 (gv-letplace (getter setter
) place
180 (funcall setter
`(nreverse ,getter
))))
182 (cl-defstruct (epg-context
184 (:constructor epg-context--make
185 (protocol &optional armor textmode include-certs
186 cipher-algorithm digest-algorithm
191 (`OpenPGP epg-gpg-program
)
192 (`CMS epg-gpgsm-program
)
193 (_ (signal 'epg-error
194 (list "unknown protocol" protocol
)))))))
199 (home-directory epg-gpg-home-directory
)
206 (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 (coding-system-for-write 'binary
)
585 (coding-system-for-read 'binary
)
586 process-connection-type
587 (process-environment process-environment
)
588 (buffer (generate-new-buffer " *epg*"))
592 (agent-mtime '(0 0 0 0)))
593 ;; Set GPG_TTY and TERM for pinentry-curses. Note that we can't
594 ;; use `terminal-name' here to get the real pty name for the child
595 ;; process, though /dev/fd/0" is not portable.
596 (unless (memq system-type
'(ms-dos windows-nt
))
599 (when (= (call-process "tty" "/dev/fd/0" t
) 0)
601 (setq terminal-name
(buffer-string)))
604 (setq process-environment
605 (cons (concat "GPG_TTY=" terminal-name
)
606 (cons "TERM=xterm" process-environment
))))
607 ;; Record modified time of gpg-agent socket to restore the Emacs
608 ;; frame on text terminal in `epg-wait-for-completion'.
610 ;; <http://lists.gnu.org/archive/html/emacs-devel/2007-02/msg00755.html>
612 (when (and agent-info
(string-match "\\(.*\\):[0-9]+:[0-9]+" agent-info
))
613 (setq agent-file
(match-string 1 agent-info
)
614 agent-mtime
(or (nth 5 (file-attributes agent-file
)) '(0 0 0 0))))
617 (unless epg-debug-buffer
618 (setq epg-debug-buffer
(generate-new-buffer " *epg-debug*")))
619 (set-buffer epg-debug-buffer
)
620 (goto-char (point-max))
621 (insert (if agent-info
622 (format "GPG_AGENT_INFO=%s\n" agent-info
)
623 "GPG_AGENT_INFO is not set\n")
625 (epg-context-program context
)
626 (mapconcat #'identity args
" ")))))
627 (with-current-buffer buffer
628 (if (fboundp 'set-buffer-multibyte
)
629 (set-buffer-multibyte nil
))
630 (make-local-variable 'epg-last-status
)
631 (setq epg-last-status nil
)
632 (make-local-variable 'epg-read-point
)
633 (setq epg-read-point
(point-min))
634 (make-local-variable 'epg-process-filter-running
)
635 (setq epg-process-filter-running nil
)
636 (make-local-variable 'epg-pending-status-list
)
637 (setq epg-pending-status-list nil
)
638 (make-local-variable 'epg-key-id
)
639 (setq epg-key-id nil
)
640 (make-local-variable 'epg-context
)
641 (setq epg-context context
)
642 (make-local-variable 'epg-agent-file
)
643 (setq epg-agent-file agent-file
)
644 (make-local-variable 'epg-agent-mtime
)
645 (setq epg-agent-mtime agent-mtime
)
646 (make-local-variable 'epg-error-output
)
647 (setq epg-error-output nil
))
649 (setq process
(apply #'start-process
"epg" buffer
650 (epg-context-program context
) args
)))
651 (set-process-filter process
#'epg--process-filter
)
652 (setf (epg-context-process context
) process
)))
654 (defun epg--process-filter (process input
)
658 (setq epg-debug-buffer
(generate-new-buffer " *epg-debug*")))
659 (goto-char (point-max))
661 (if (buffer-live-p (process-buffer process
))
662 (with-current-buffer (process-buffer process
)
664 (goto-char (point-max))
666 (unless epg-process-filter-running
667 (let ((epg-process-filter-running t
))
668 (goto-char epg-read-point
)
670 (while (looking-at ".*\n") ;the input line finished
671 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
672 (let ((status (match-string 1))
673 (string (match-string 2))
675 (if (member status epg-pending-status-list
)
676 (setq epg-pending-status-list nil
))
677 ;; When editing a key, delegate all interaction
679 (if (eq (epg-context-operation epg-context
) 'edit-key
)
680 (funcall (car (epg-context-edit-callback
685 (cdr (epg-context-edit-callback
687 ;; Otherwise call epg--status-STATUS function.
688 (setq symbol
(intern-soft (concat "epg--status-"
692 (funcall symbol epg-context string
)))
693 (setq epg-last-status
(cons status string
)))
694 ;; Record other lines sent to stderr. This assumes
695 ;; that the process-filter receives output only from
696 ;; stderr and the FD specified with --status-fd.
697 (setq epg-error-output
698 (cons (buffer-substring (point)
702 (setq epg-read-point
(point)))))))))
704 (defun epg-read-output (context)
705 "Read the output file CONTEXT and return the content as a string."
707 (if (fboundp 'set-buffer-multibyte
)
708 (set-buffer-multibyte nil
))
709 (if (file-exists-p (epg-context-output-file context
))
710 (let ((coding-system-for-read 'binary
))
711 (insert-file-contents (epg-context-output-file context
))
714 (defun epg-wait-for-status (context status-list
)
715 "Wait until one of elements in STATUS-LIST arrives."
716 (with-current-buffer (process-buffer (epg-context-process context
))
717 (setq epg-pending-status-list status-list
)
718 (while (and (eq (process-status (epg-context-process context
)) 'run
)
719 epg-pending-status-list
)
720 (accept-process-output (epg-context-process context
) 1))
721 (if epg-pending-status-list
722 (epg-context-set-result-for
725 (epg-context-result-for context
'error
))))))
727 (defun epg-wait-for-completion (context)
728 "Wait until the `epg-gpg-program' process completes."
729 (while (eq (process-status (epg-context-process context
)) 'run
)
730 (accept-process-output (epg-context-process context
) 1))
731 ;; This line is needed to run the process-filter right now.
733 ;; Restore Emacs frame on text terminal, when pinentry-curses has terminated.
734 (if (with-current-buffer (process-buffer (epg-context-process context
))
736 (> (float-time (or (nth 5 (file-attributes epg-agent-file
))
738 (float-time epg-agent-mtime
))))
740 (epg-context-set-result-for
742 (nreverse (epg-context-result-for context
'error
)))
743 (with-current-buffer (process-buffer (epg-context-process context
))
744 (setf (epg-context-error-output context
)
745 (mapconcat #'identity
(nreverse epg-error-output
) "\n"))))
747 (defun epg-reset (context)
749 (if (and (epg-context-process context
)
750 (buffer-live-p (process-buffer (epg-context-process context
))))
751 (kill-buffer (process-buffer (epg-context-process context
))))
752 (setf (epg-context-process context
) nil
)
753 (setf (epg-context-edit-callback context
) nil
))
755 (defun epg-delete-output-file (context)
756 "Delete the output file of CONTEXT."
757 (if (and (epg-context-output-file context
)
758 (file-exists-p (epg-context-output-file context
)))
759 (delete-file (epg-context-output-file context
))))
762 (if (fboundp 'decode-coding-string
)
763 (defalias 'epg--decode-coding-string
'decode-coding-string
)
764 (defalias 'epg--decode-coding-string
'identity
)))
766 (defun epg--status-USERID_HINT (_context string
)
767 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string
)
768 (let* ((key-id (match-string 1 string
))
769 (user-id (match-string 2 string
))
770 (entry (assoc key-id epg-user-id-alist
)))
772 (setq user-id
(epg--decode-coding-string
773 (epg--decode-percent-escape user-id
)
777 (setcdr entry user-id
)
778 (setq epg-user-id-alist
(cons (cons key-id user-id
)
779 epg-user-id-alist
))))))
781 (defun epg--status-NEED_PASSPHRASE (_context string
)
782 (if (string-match "\\`\\([^ ]+\\)" string
)
783 (setq epg-key-id
(match-string 1 string
))))
785 (defun epg--status-NEED_PASSPHRASE_SYM (_context _string
)
786 (setq epg-key-id
'SYM
))
788 (defun epg--status-NEED_PASSPHRASE_PIN (_context _string
)
789 (setq epg-key-id
'PIN
))
792 (if (fboundp 'clear-string
)
793 (defalias 'epg--clear-string
'clear-string
)
794 (defun epg--clear-string (string)
795 (fillarray string
0))))
798 (if (fboundp 'encode-coding-string
)
799 (defalias 'epg--encode-coding-string
'encode-coding-string
)
800 (defalias 'epg--encode-coding-string
'identity
)))
802 (defun epg--status-GET_HIDDEN (context string
)
803 (when (and epg-key-id
804 (string-match "\\`passphrase\\." string
))
805 (unless (epg-context-passphrase-callback context
)
806 (error "passphrase-callback not set"))
809 passphrase-with-new-line
810 encoded-passphrase-with-new-line
)
816 (car (epg-context-passphrase-callback context
))
819 (cdr (epg-context-passphrase-callback context
))))
821 (setq passphrase-with-new-line
(concat passphrase
"\n"))
822 (epg--clear-string passphrase
)
823 (setq passphrase nil
)
824 (if epg-passphrase-coding-system
826 (setq encoded-passphrase-with-new-line
827 (epg--encode-coding-string
828 passphrase-with-new-line
829 (coding-system-change-eol-conversion
830 epg-passphrase-coding-system
'unix
)))
831 (epg--clear-string passphrase-with-new-line
)
832 (setq passphrase-with-new-line nil
))
833 (setq encoded-passphrase-with-new-line
834 passphrase-with-new-line
835 passphrase-with-new-line nil
))
836 (process-send-string (epg-context-process context
)
837 encoded-passphrase-with-new-line
)))
839 (epg-context-set-result-for
842 (epg-context-result-for context
'error
)))
843 (delete-process (epg-context-process context
))))
845 (epg--clear-string passphrase
))
846 (if passphrase-with-new-line
847 (epg--clear-string passphrase-with-new-line
))
848 (if encoded-passphrase-with-new-line
849 (epg--clear-string encoded-passphrase-with-new-line
))))))
851 (defun epg--prompt-GET_BOOL (_context string
)
852 (let ((entry (assoc string epg-prompt-alist
)))
853 (y-or-n-p (if entry
(cdr entry
) (concat string
"? ")))))
855 (defun epg--prompt-GET_BOOL-untrusted_key.override
(_context _string
)
856 (y-or-n-p (if (and (equal (car epg-last-status
) "USERID_HINT")
857 (string-match "\\`\\([^ ]+\\) \\(.*\\)"
858 (cdr epg-last-status
)))
859 (let* ((key-id (match-string 1 (cdr epg-last-status
)))
860 (user-id (match-string 2 (cdr epg-last-status
)))
861 (entry (assoc key-id epg-user-id-alist
)))
863 (setq user-id
(cdr entry
)))
864 (format "Untrusted key %s %s. Use anyway? " key-id user-id
))
865 "Use untrusted key anyway? ")))
867 (defun epg--status-GET_BOOL (context string
)
870 (if (funcall (or (intern-soft (concat "epg--prompt-GET_BOOL-" string
))
871 #'epg--prompt-GET_BOOL
)
873 (process-send-string (epg-context-process context
) "y\n")
874 (process-send-string (epg-context-process context
) "n\n"))
876 (epg-context-set-result-for
879 (epg-context-result-for context
'error
)))
880 (delete-process (epg-context-process context
))))))
882 (defun epg--status-GET_LINE (context string
)
883 (let ((entry (assoc string epg-prompt-alist
))
886 (process-send-string (epg-context-process context
)
890 (concat string
": ")))
893 (epg-context-set-result-for
896 (epg-context-result-for context
'error
)))
897 (delete-process (epg-context-process context
))))))
899 (defun epg--status-*SIG
(context status string
)
900 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string
)
901 (let* ((key-id (match-string 1 string
))
902 (user-id (match-string 2 string
))
903 (entry (assoc key-id epg-user-id-alist
)))
904 (epg-context-set-result-for
907 (cons (epg-make-signature status key-id
)
908 (epg-context-result-for context
'verify
)))
910 (if (eq (epg-context-protocol context
) 'CMS
)
911 (setq user-id
(epg-dn-from-string user-id
))
912 (setq user-id
(epg--decode-coding-string
913 (epg--decode-percent-escape user-id
)
917 (setcdr entry user-id
)
918 (setq epg-user-id-alist
919 (cons (cons key-id user-id
) epg-user-id-alist
))))
920 (epg-context-set-result-for
923 (cons (epg-make-signature status
)
924 (epg-context-result-for context
'verify
)))))
926 (defun epg--status-GOODSIG (context string
)
927 (epg--status-*SIG context
'good string
))
929 (defun epg--status-EXPSIG (context string
)
930 (epg--status-*SIG context
'expired string
))
932 (defun epg--status-EXPKEYSIG (context string
)
933 (epg--status-*SIG context
'expired-key string
))
935 (defun epg--status-REVKEYSIG (context string
)
936 (epg--status-*SIG context
'revoked-key string
))
938 (defun epg--status-BADSIG (context string
)
939 (epg--status-*SIG context
'bad string
))
941 (defun epg--status-NO_PUBKEY (context string
)
942 (if (eq (epg-context-operation context
) 'verify
)
943 (let ((signature (car (epg-context-result-for context
'verify
))))
945 (eq (epg-signature-status signature
) 'error
)
946 (equal (epg-signature-key-id signature
) string
))
947 (setf (epg-signature-status signature
) 'no-pubkey
)))
948 (epg-context-set-result-for
950 (cons (cons 'no-pubkey string
)
951 (epg-context-result-for context
'error
)))))
953 (defun epg--status-NO_SECKEY (context string
)
954 (epg-context-set-result-for
956 (cons (cons 'no-seckey string
)
957 (epg-context-result-for context
'error
))))
959 (defun epg--time-from-seconds (seconds)
960 (let ((number-seconds (string-to-number (concat seconds
".0"))))
961 (cons (floor (/ number-seconds
65536))
962 (floor (mod number-seconds
65536)))))
964 (defun epg--status-ERRSIG (context string
)
965 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
966 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
968 (let ((signature (epg-make-signature 'error
)))
969 (epg-context-set-result-for
973 (epg-context-result-for context
'verify
)))
974 (setf (epg-signature-key-id signature
)
975 (match-string 1 string
))
976 (setf (epg-signature-pubkey-algorithm signature
)
977 (string-to-number (match-string 2 string
)))
978 (setf (epg-signature-digest-algorithm signature
)
979 (string-to-number (match-string 3 string
)))
980 (setf (epg-signature-class signature
)
981 (string-to-number (match-string 4 string
) 16))
982 (setf (epg-signature-creation-time signature
)
983 (epg--time-from-seconds (match-string 5 string
))))))
985 (defun epg--status-VALIDSIG (context string
)
986 (let ((signature (car (epg-context-result-for context
'verify
))))
988 (eq (epg-signature-status signature
) 'good
)
989 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
990 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
993 (setf (epg-signature-fingerprint signature
)
994 (match-string 1 string
))
995 (setf (epg-signature-creation-time signature
)
996 (epg--time-from-seconds (match-string 2 string
)))
997 (unless (equal (match-string 3 string
) "0")
998 (setf (epg-signature-expiration-time signature
)
999 (epg--time-from-seconds (match-string 3 string
))))
1000 (setf (epg-signature-version signature
)
1001 (string-to-number (match-string 4 string
)))
1002 (setf (epg-signature-pubkey-algorithm signature
)
1003 (string-to-number (match-string 5 string
)))
1004 (setf (epg-signature-digest-algorithm signature
)
1005 (string-to-number (match-string 6 string
)))
1006 (setf (epg-signature-class signature
)
1007 (string-to-number (match-string 7 string
) 16)))))
1009 (defun epg--status-TRUST_UNDEFINED (context _string
)
1010 (let ((signature (car (epg-context-result-for context
'verify
))))
1012 (eq (epg-signature-status signature
) 'good
))
1013 (setf (epg-signature-validity signature
) 'undefined
))))
1015 (defun epg--status-TRUST_NEVER (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
) 'never
))))
1021 (defun epg--status-TRUST_MARGINAL (context _string
)
1022 (let ((signature (car (epg-context-result-for context
'verify
))))
1024 (eq (epg-signature-status signature
) 'marginal
))
1025 (setf (epg-signature-validity signature
) 'marginal
))))
1027 (defun epg--status-TRUST_FULLY (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
) 'full
))))
1033 (defun epg--status-TRUST_ULTIMATE (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
) 'ultimate
))))
1039 (defun epg--status-NOTATION_NAME (context string
)
1040 (let ((signature (car (epg-context-result-for context
'verify
))))
1042 (push (epg-make-sig-notation string nil t nil
)
1043 (epg-signature-notations signature
)))))
1045 (defun epg--status-NOTATION_DATA (context string
)
1046 (let ((signature (car (epg-context-result-for context
'verify
)))
1049 (setq notation
(car (epg-signature-notations signature
))))
1050 (setf (epg-sig-notation-value notation
) string
))))
1052 (defun epg--status-POLICY_URL (context string
)
1053 (let ((signature (car (epg-context-result-for context
'verify
))))
1055 (push (epg-make-sig-notation nil string t nil
)
1056 (epg-signature-notations signature
)))))
1058 (defun epg--status-PROGRESS (context string
)
1059 (if (and (epg-context-progress-callback context
)
1060 (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1062 (funcall (car (epg-context-progress-callback context
))
1064 (match-string 1 string
)
1065 (match-string 2 string
)
1066 (string-to-number (match-string 3 string
))
1067 (string-to-number (match-string 4 string
))
1068 (cdr (epg-context-progress-callback context
)))))
1070 (defun epg--status-ENC_TO (context string
)
1071 (if (string-match "\\`\\([0-9A-Za-z]+\\) \\([0-9]+\\) \\([0-9]+\\)" string
)
1072 (epg-context-set-result-for
1073 context
'encrypted-to
1074 (cons (list (match-string 1 string
)
1075 (string-to-number (match-string 2 string
))
1076 (string-to-number (match-string 3 string
)))
1077 (epg-context-result-for context
'encrypted-to
)))))
1079 (defun epg--status-DECRYPTION_FAILED (context _string
)
1080 (epg-context-set-result-for context
'decryption-failed t
))
1082 (defun epg--status-DECRYPTION_OKAY (context _string
)
1083 (epg-context-set-result-for context
'decryption-okay t
))
1085 (defun epg--status-NODATA (context string
)
1086 (epg-context-set-result-for
1088 (cons (cons 'no-data
(string-to-number string
))
1089 (epg-context-result-for context
'error
))))
1091 (defun epg--status-UNEXPECTED (context string
)
1092 (epg-context-set-result-for
1094 (cons (cons 'unexpected
(string-to-number string
))
1095 (epg-context-result-for context
'error
))))
1097 (defun epg--status-KEYEXPIRED (context string
)
1098 (epg-context-set-result-for
1100 (cons (list 'key-expired
(cons 'expiration-time
1101 (epg--time-from-seconds string
)))
1102 (epg-context-result-for context
'key
))))
1104 (defun epg--status-KEYREVOKED (context _string
)
1105 (epg-context-set-result-for
1107 (cons '(key-revoked)
1108 (epg-context-result-for context
'key
))))
1110 (defun epg--status-BADARMOR (context _string
)
1111 (epg-context-set-result-for
1114 (epg-context-result-for context
'error
))))
1116 (defun epg--status-INV_RECP (context string
)
1117 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string
)
1118 (epg-context-set-result-for
1120 (cons (list 'invalid-recipient
1122 (string-to-number (match-string 1 string
)))
1124 (match-string 2 string
)))
1125 (epg-context-result-for context
'error
)))))
1127 (defun epg--status-INV_SGNR (context string
)
1128 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string
)
1129 (epg-context-set-result-for
1131 (cons (list 'invalid-signer
1133 (string-to-number (match-string 1 string
)))
1135 (match-string 2 string
)))
1136 (epg-context-result-for context
'error
)))))
1138 (defun epg--status-NO_RECP (context _string
)
1139 (epg-context-set-result-for
1141 (cons '(no-recipients)
1142 (epg-context-result-for context
'error
))))
1144 (defun epg--status-NO_SGNR (context _string
)
1145 (epg-context-set-result-for
1148 (epg-context-result-for context
'error
))))
1150 (defun epg--status-DELETE_PROBLEM (context string
)
1151 (if (string-match "\\`\\([0-9]+\\)" string
)
1152 (epg-context-set-result-for
1154 (cons (cons 'delete-problem
1155 (string-to-number (match-string 1 string
)))
1156 (epg-context-result-for context
'error
)))))
1158 (defun epg--status-SIG_CREATED (context string
)
1159 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1160 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string
)
1161 (epg-context-set-result-for
1163 (cons (epg-make-new-signature
1164 (cdr (assq (aref (match-string 1 string
) 0)
1165 epg-new-signature-type-alist
))
1166 (string-to-number (match-string 2 string
))
1167 (string-to-number (match-string 3 string
))
1168 (string-to-number (match-string 4 string
) 16)
1169 (epg--time-from-seconds (match-string 5 string
))
1170 (substring string
(match-end 0)))
1171 (epg-context-result-for context
'sign
)))))
1173 (defun epg--status-KEY_CREATED (context string
)
1174 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string
)
1175 (epg-context-set-result-for
1176 context
'generate-key
1177 (cons (list (cons 'type
(string-to-char (match-string 1 string
)))
1178 (cons 'fingerprint
(match-string 2 string
)))
1179 (epg-context-result-for context
'generate-key
)))))
1181 (defun epg--status-KEY_NOT_CREATED (context _string
)
1182 (epg-context-set-result-for
1184 (cons '(key-not-created)
1185 (epg-context-result-for context
'error
))))
1187 (defun epg--status-IMPORTED (_context string
)
1188 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string
)
1189 (let* ((key-id (match-string 1 string
))
1190 (user-id (match-string 2 string
))
1191 (entry (assoc key-id epg-user-id-alist
)))
1193 (setq user-id
(epg--decode-coding-string
1194 (epg--decode-percent-escape user-id
)
1198 (setcdr entry user-id
)
1199 (setq epg-user-id-alist
(cons (cons key-id user-id
)
1200 epg-user-id-alist
))))))
1202 (defun epg--status-IMPORT_OK (context string
)
1203 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string
)
1204 (let ((reason (string-to-number (match-string 1 string
))))
1205 (epg-context-set-result-for
1206 context
'import-status
1207 (cons (epg-make-import-status (if (match-beginning 2)
1208 (match-string 3 string
))
1210 (/= (logand reason
1) 0)
1211 (/= (logand reason
2) 0)
1212 (/= (logand reason
4) 0)
1213 (/= (logand reason
8) 0)
1214 (/= (logand reason
16) 0))
1215 (epg-context-result-for context
'import-status
))))))
1217 (defun epg--status-IMPORT_PROBLEM (context string
)
1218 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string
)
1219 (epg-context-set-result-for
1220 context
'import-status
1221 (cons (epg-make-import-status
1222 (if (match-beginning 2)
1223 (match-string 3 string
))
1224 (string-to-number (match-string 1 string
)))
1225 (epg-context-result-for context
'import-status
)))))
1227 (defun epg--status-IMPORT_RES (context string
)
1228 (when (string-match "\\`\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1229 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1230 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" string
)
1231 (epg-context-set-result-for
1233 (epg-make-import-result (string-to-number (match-string 1 string
))
1234 (string-to-number (match-string 2 string
))
1235 (string-to-number (match-string 3 string
))
1236 (string-to-number (match-string 4 string
))
1237 (string-to-number (match-string 5 string
))
1238 (string-to-number (match-string 6 string
))
1239 (string-to-number (match-string 7 string
))
1240 (string-to-number (match-string 8 string
))
1241 (string-to-number (match-string 9 string
))
1242 (string-to-number (match-string 10 string
))
1243 (string-to-number (match-string 11 string
))
1244 (string-to-number (match-string 12 string
))
1245 (string-to-number (match-string 13 string
))
1246 (epg-context-result-for context
'import-status
)))
1247 (epg-context-set-result-for context
'import-status nil
)))
1249 (defun epg-passphrase-callback-function (context key-id _handback
)
1250 (declare (obsolete epa-passphrase-callback-function
"23.1"))
1251 (if (eq key-id
'SYM
)
1252 (read-passwd "Passphrase for symmetric encryption: "
1253 (eq (epg-context-operation context
) 'encrypt
))
1255 (if (eq key-id
'PIN
)
1256 "Passphrase for PIN: "
1257 (let ((entry (assoc key-id epg-user-id-alist
)))
1259 (format "Passphrase for %s %s: " key-id
(cdr entry
))
1260 (format "Passphrase for %s: " key-id
)))))))
1262 (defun epg--list-keys-1 (context name mode
)
1263 (let ((args (append (if (epg-context-home-directory context
)
1265 (epg-context-home-directory context
)))
1266 '("--with-colons" "--no-greeting" "--batch"
1267 "--with-fingerprint" "--with-fingerprint")
1268 (unless (eq (epg-context-protocol context
) 'CMS
)
1269 '("--fixed-list-mode"))))
1270 (list-keys-option (if (memq mode
'(t secret
))
1271 "--list-secret-keys"
1272 (if (memq mode
'(nil public
))
1275 (coding-system-for-read 'binary
)
1276 keys string field index
)
1279 (unless (listp name
)
1280 (setq name
(list name
)))
1282 (setq args
(append args
(list list-keys-option
(car name
)))
1284 (setq args
(append args
(list list-keys-option
))))
1286 (apply #'call-process
1287 (epg-context-program context
)
1288 nil
(list t nil
) nil args
)
1289 (goto-char (point-min))
1290 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t
)
1291 (setq keys
(cons (make-vector 15 nil
) keys
)
1292 string
(match-string 0)
1295 (while (and (< field
(length (car keys
)))
1297 (string-match "\\([^:]+\\)?:" string index
)))
1298 (setq index
(match-end 0))
1299 (aset (car keys
) field
(match-string 1 string
))
1300 (setq field
(1+ field
))))
1303 (defun epg--make-sub-key-1 (line)
1306 (cdr (assq (string-to-char (aref line
1)) epg-key-validity-alist
)))
1308 (mapcar (lambda (char) (cdr (assq char epg-key-capability-alist
)))
1310 (member (aref line
0) '("sec" "ssb"))
1311 (string-to-number (aref line
3))
1312 (string-to-number (aref line
2))
1314 (epg--time-from-seconds (aref line
5))
1316 (epg--time-from-seconds (aref line
6)))))
1318 (defun epg-list-keys (context &optional name mode
)
1319 "Return a list of epg-key objects matched with NAME.
1320 If MODE is nil or 'public, only public keyring should be searched.
1321 If MODE is t or 'secret, only secret keyring should be searched.
1322 Otherwise, only public keyring should be searched and the key
1323 signatures should be included.
1324 NAME is either a string or a list of strings."
1325 (let ((lines (epg--list-keys-1 context name mode
))
1326 keys cert pointer pointer-1 index string
)
1329 ((member (aref (car lines
) 0) '("pub" "sec" "crt" "crs"))
1330 (setq cert
(member (aref (car lines
) 0) '("crt" "crs"))
1331 keys
(cons (epg-make-key
1332 (if (aref (car lines
) 8)
1333 (cdr (assq (string-to-char (aref (car lines
) 8))
1334 epg-key-validity-alist
))))
1336 (push (epg--make-sub-key-1 (car lines
))
1337 (epg-key-sub-key-list (car keys
))))
1338 ((member (aref (car lines
) 0) '("sub" "ssb"))
1339 (push (epg--make-sub-key-1 (car lines
))
1340 (epg-key-sub-key-list (car keys
))))
1341 ((equal (aref (car lines
) 0) "uid")
1342 ;; Decode the UID name as a backslash escaped UTF-8 string,
1343 ;; generated by GnuPG/GpgSM.
1344 (setq string
(copy-sequence (aref (car lines
) 9))
1346 (while (string-match "\"" string index
)
1347 (setq string
(replace-match "\\\"" t t string
)
1348 index
(1+ (match-end 0))))
1350 (setq string
(epg--decode-coding-string
1351 (car (read-from-string (concat "\"" string
"\"")))
1354 (setq string
(aref (car lines
) 9))))
1355 (push (epg-make-user-id
1356 (if (aref (car lines
) 1)
1357 (cdr (assq (string-to-char (aref (car lines
) 1))
1358 epg-key-validity-alist
)))
1361 (epg-dn-from-string string
)
1364 (epg-key-user-id-list (car keys
))))
1365 ((equal (aref (car lines
) 0) "fpr")
1366 (setf (epg-sub-key-fingerprint (car (epg-key-sub-key-list (car keys
))))
1367 (aref (car lines
) 9)))
1368 ((equal (aref (car lines
) 0) "sig")
1370 (epg-make-key-signature
1371 (if (aref (car lines
) 1)
1372 (cdr (assq (string-to-char (aref (car lines
) 1))
1373 epg-key-validity-alist
)))
1374 (string-to-number (aref (car lines
) 3))
1375 (aref (car lines
) 4)
1376 (epg--time-from-seconds (aref (car lines
) 5))
1377 (epg--time-from-seconds (aref (car lines
) 6))
1378 (aref (car lines
) 9)
1379 (string-to-number (aref (car lines
) 10) 16)
1380 (eq (aref (aref (car lines
) 10) 2) ?x
))
1381 (epg-user-id-signature-list
1382 (car (epg-key-user-id-list (car keys
)))))))
1383 (setq lines
(cdr lines
)))
1384 (setq keys
(nreverse keys
)
1387 (epg--gv-nreverse (epg-key-sub-key-list (car pointer
)))
1388 (setq pointer-1
(epg--gv-nreverse (epg-key-user-id-list (car pointer
))))
1390 (epg--gv-nreverse (epg-user-id-signature-list (car pointer-1
)))
1391 (setq pointer-1
(cdr pointer-1
)))
1392 (setq pointer
(cdr pointer
)))
1396 (if (fboundp 'make-temp-file
)
1397 (defalias 'epg--make-temp-file
'make-temp-file
)
1398 (defvar temporary-file-directory
)
1399 ;; stolen from poe.el.
1400 (defun epg--make-temp-file (prefix)
1401 "Create a temporary file.
1402 The returned file name (created by appending some random characters at the end
1403 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1404 is guaranteed to point to a newly created empty file.
1405 You can then use `write-region' to write new data into the file."
1406 (let ((orig-modes (default-file-modes))
1408 (setq prefix
(expand-file-name prefix
1409 (if (featurep 'xemacs
)
1411 temporary-file-directory
)))
1414 ;; First, create a temporary directory.
1415 (set-default-file-modes #o700
)
1416 (while (condition-case ()
1418 (setq tempdir
(make-temp-name
1420 (file-name-directory prefix
)
1422 ;; return nil or signal an error.
1423 (make-directory tempdir
))
1425 (file-already-exists t
)))
1426 ;; Second, create a temporary file in the tempdir.
1427 ;; There *is* a race condition between `make-temp-name'
1428 ;; and `write-region', but we don't care it since we are
1429 ;; in a private directory now.
1430 (setq tempfile
(make-temp-name (concat tempdir
"/EMU")))
1431 (write-region "" nil tempfile nil
'silent
)
1432 ;; Finally, make a hard-link from the tempfile.
1433 (while (condition-case ()
1435 (setq file
(make-temp-name prefix
))
1436 ;; return nil or signal an error.
1437 (add-name-to-file tempfile file
))
1439 (file-already-exists t
)))
1441 (set-default-file-modes orig-modes
)
1442 ;; Cleanup the tempfile.
1444 (file-exists-p tempfile
)
1445 (delete-file tempfile
))
1446 ;; Cleanup the tempdir.
1448 (file-directory-p tempdir
)
1449 (delete-directory tempdir
)))))))
1451 (defun epg--args-from-sig-notations (notations)
1455 (if (and (epg-sig-notation-name notation
)
1456 (not (epg-sig-notation-human-readable notation
)))
1457 (error "Unreadable"))
1458 (if (epg-sig-notation-name notation
)
1459 (list "--sig-notation"
1460 (if (epg-sig-notation-critical notation
)
1461 (concat "!" (epg-sig-notation-name notation
)
1462 "=" (epg-sig-notation-value notation
))
1463 (concat (epg-sig-notation-name notation
)
1464 "=" (epg-sig-notation-value notation
))))
1465 (list "--sig-policy-url"
1466 (if (epg-sig-notation-critical notation
)
1467 (concat "!" (epg-sig-notation-value notation
))
1468 (epg-sig-notation-value notation
)))))
1471 (defun epg-cancel (context)
1472 (if (buffer-live-p (process-buffer (epg-context-process context
)))
1473 (with-current-buffer (process-buffer (epg-context-process context
))
1474 (epg-context-set-result-for
1477 (epg-context-result-for epg-context
'error
)))))
1478 (if (eq (process-status (epg-context-process context
)) 'run
)
1479 (delete-process (epg-context-process context
))))
1481 (defun epg-start-decrypt (context cipher
)
1482 "Initiate a decrypt operation on CIPHER.
1483 CIPHER must be a file data object.
1485 If you use this function, you will need to wait for the completion of
1486 `epg-gpg-program' by using `epg-wait-for-completion' and call
1487 `epg-reset' to clear a temporary output file.
1488 If you are unsure, use synchronous version of this function
1489 `epg-decrypt-file' or `epg-decrypt-string' instead."
1490 (unless (epg-data-file cipher
)
1491 (error "Not a file"))
1492 (setf (epg-context-operation context
) 'decrypt
)
1493 (setf (epg-context-result context
) nil
)
1494 (epg--start context
(list "--decrypt" "--" (epg-data-file cipher
)))
1495 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1496 (unless (eq (epg-context-protocol context
) 'CMS
)
1497 (epg-wait-for-status context
'("BEGIN_DECRYPTION"))))
1499 (defun epg--check-error-for-decrypt (context)
1500 (let ((errors (epg-context-result-for context
'error
)))
1501 (if (epg-context-result-for context
'decryption-failed
)
1503 (list "Decryption failed" (epg-errors-to-string errors
))))
1504 (unless (epg-context-result-for context
'decryption-okay
)
1506 (list "Can't decrypt" (epg-errors-to-string errors
))))))
1508 (defun epg-decrypt-file (context cipher plain
)
1509 "Decrypt a file CIPHER and store the result to a file PLAIN.
1510 If PLAIN is nil, it returns the result as a string."
1513 (setf (epg-context-output-file context
)
1514 (or plain
(epg--make-temp-file "epg-output")))
1515 (epg-start-decrypt context
(epg-make-data-from-file cipher
))
1516 (epg-wait-for-completion context
)
1517 (epg--check-error-for-decrypt context
)
1519 (epg-read-output context
)))
1521 (epg-delete-output-file context
))
1522 (epg-reset context
)))
1524 (defun epg-decrypt-string (context cipher
)
1525 "Decrypt a string CIPHER and return the plain text."
1526 (let ((input-file (epg--make-temp-file "epg-input"))
1527 (coding-system-for-write 'binary
))
1530 (write-region cipher nil input-file nil
'quiet
)
1531 (setf (epg-context-output-file context
)
1532 (epg--make-temp-file "epg-output"))
1533 (epg-start-decrypt context
(epg-make-data-from-file input-file
))
1534 (epg-wait-for-completion context
)
1535 (epg--check-error-for-decrypt context
)
1536 (epg-read-output context
))
1537 (epg-delete-output-file context
)
1538 (if (file-exists-p input-file
)
1539 (delete-file input-file
))
1540 (epg-reset context
))))
1542 (defun epg-start-verify (context signature
&optional signed-text
)
1543 "Initiate a verify operation on SIGNATURE.
1544 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1546 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1547 For a normal or a cleartext signature, SIGNED-TEXT should be nil.
1549 If you use this function, you will need to wait for the completion of
1550 `epg-gpg-program' by using `epg-wait-for-completion' and call
1551 `epg-reset' to clear a temporary output file.
1552 If you are unsure, use synchronous version of this function
1553 `epg-verify-file' or `epg-verify-string' instead."
1554 (setf (epg-context-operation context
) 'verify
)
1555 (setf (epg-context-result context
) nil
)
1557 ;; Detached signature.
1558 (if (epg-data-file signed-text
)
1559 (epg--start context
(list "--verify" "--" (epg-data-file signature
)
1560 (epg-data-file signed-text
)))
1561 (epg--start context
(list "--verify" "--" (epg-data-file signature
)
1563 (if (eq (process-status (epg-context-process context
)) 'run
)
1564 (process-send-string (epg-context-process context
)
1565 (epg-data-string signed-text
)))
1566 (if (eq (process-status (epg-context-process context
)) 'run
)
1567 (process-send-eof (epg-context-process context
))))
1568 ;; Normal (or cleartext) signature.
1569 (if (epg-data-file signature
)
1570 (epg--start context
(if (eq (epg-context-protocol context
) 'CMS
)
1571 (list "--verify" "--" (epg-data-file signature
))
1572 (list "--" (epg-data-file signature
))))
1573 (epg--start context
(if (eq (epg-context-protocol context
) 'CMS
)
1576 (if (eq (process-status (epg-context-process context
)) 'run
)
1577 (process-send-string (epg-context-process context
)
1578 (epg-data-string signature
)))
1579 (if (eq (process-status (epg-context-process context
)) 'run
)
1580 (process-send-eof (epg-context-process context
))))))
1582 (defun epg-verify-file (context signature
&optional signed-text plain
)
1583 "Verify a file SIGNATURE.
1584 SIGNED-TEXT and PLAIN are also a file if they are specified.
1586 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1587 string. For a normal or a cleartext signature, SIGNED-TEXT should be
1588 nil. In the latter case, if PLAIN is specified, the plaintext is
1589 stored into the file after successful verification.
1591 Note that this function does not return verification result as t
1592 or nil, nor signal error on failure. That's a design decision to
1593 handle the case where SIGNATURE has multiple signature.
1595 To check the verification results, use `epg-context-result-for' as follows:
1597 \(epg-context-result-for context 'verify)
1599 which will return a list of `epg-signature' object."
1602 (setf (epg-context-output-file context
)
1603 (or plain
(epg--make-temp-file "epg-output")))
1605 (epg-start-verify context
1606 (epg-make-data-from-file signature
)
1607 (epg-make-data-from-file signed-text
))
1608 (epg-start-verify context
1609 (epg-make-data-from-file signature
)))
1610 (epg-wait-for-completion context
)
1612 (epg-read-output context
)))
1614 (epg-delete-output-file context
))
1615 (epg-reset context
)))
1617 (defun epg-verify-string (context signature
&optional signed-text
)
1618 "Verify a string SIGNATURE.
1619 SIGNED-TEXT is a string if it is specified.
1621 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1622 string. For a normal or a cleartext signature, SIGNED-TEXT should be
1623 nil. In the latter case, this function returns the plaintext after
1624 successful verification.
1626 Note that this function does not return verification result as t
1627 or nil, nor signal error on failure. That's a design decision to
1628 handle the case where SIGNATURE has multiple signature.
1630 To check the verification results, use `epg-context-result-for' as follows:
1632 \(epg-context-result-for context 'verify)
1634 which will return a list of `epg-signature' object."
1635 (let ((coding-system-for-write 'binary
)
1639 (setf (epg-context-output-file context
)
1640 (epg--make-temp-file "epg-output"))
1643 (setq input-file
(epg--make-temp-file "epg-signature"))
1644 (write-region signature nil input-file nil
'quiet
)
1645 (epg-start-verify context
1646 (epg-make-data-from-file input-file
)
1647 (epg-make-data-from-string signed-text
)))
1648 (epg-start-verify context
(epg-make-data-from-string signature
)))
1649 (epg-wait-for-completion context
)
1650 (epg-read-output context
))
1651 (epg-delete-output-file context
)
1653 (file-exists-p input-file
))
1654 (delete-file input-file
))
1655 (epg-reset context
))))
1657 (defun epg-start-sign (context plain
&optional mode
)
1658 "Initiate a sign operation on PLAIN.
1659 PLAIN is a data object.
1661 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1662 If it is nil or 'normal, it makes a normal signature.
1663 Otherwise, it makes a cleartext signature.
1665 If you use this function, you will need to wait for the completion of
1666 `epg-gpg-program' by using `epg-wait-for-completion' and call
1667 `epg-reset' to clear a temporary output file.
1668 If you are unsure, use synchronous version of this function
1669 `epg-sign-file' or `epg-sign-string' instead."
1670 (setf (epg-context-operation context
) 'sign
)
1671 (setf (epg-context-result context
) nil
)
1672 (unless (memq mode
'(t detached nil normal
)) ;i.e. cleartext
1673 (epg-context-set-armor context nil
)
1674 (epg-context-set-textmode context nil
))
1676 (append (list (if (memq mode
'(t detached
))
1678 (if (memq mode
'(nil normal
))
1686 (car (epg-key-sub-key-list signer
)))))
1687 (epg-context-signers context
)))
1688 (epg--args-from-sig-notations
1689 (epg-context-sig-notations context
))
1690 (if (epg-data-file plain
)
1691 (list "--" (epg-data-file plain
)))))
1692 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1693 (unless (eq (epg-context-protocol context
) 'CMS
)
1694 (epg-wait-for-status context
'("BEGIN_SIGNING")))
1695 (when (epg-data-string plain
)
1696 (if (eq (process-status (epg-context-process context
)) 'run
)
1697 (process-send-string (epg-context-process context
)
1698 (epg-data-string plain
)))
1699 (if (eq (process-status (epg-context-process context
)) 'run
)
1700 (process-send-eof (epg-context-process context
)))))
1702 (defun epg-sign-file (context plain signature
&optional mode
)
1703 "Sign a file PLAIN and store the result to a file SIGNATURE.
1704 If SIGNATURE is nil, it returns the result as a string.
1705 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1706 If it is nil or 'normal, it makes a normal signature.
1707 Otherwise, it makes a cleartext signature."
1710 (setf (epg-context-output-file context
)
1711 (or signature
(epg--make-temp-file "epg-output")))
1712 (epg-start-sign context
(epg-make-data-from-file plain
) mode
)
1713 (epg-wait-for-completion context
)
1714 (unless (epg-context-result-for context
'sign
)
1715 (let ((errors (epg-context-result-for context
'error
)))
1717 (list "Sign failed" (epg-errors-to-string errors
)))))
1719 (epg-read-output context
)))
1721 (epg-delete-output-file context
))
1722 (epg-reset context
)))
1724 (defun epg-sign-string (context plain
&optional mode
)
1725 "Sign a string PLAIN and return the output as string.
1726 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1727 If it is nil or 'normal, it makes a normal signature.
1728 Otherwise, it makes a cleartext signature."
1730 (unless (or (eq (epg-context-protocol context
) 'CMS
)
1733 (epg-check-configuration (epg-configuration))
1736 (epg--make-temp-file "epg-input")))
1737 (coding-system-for-write 'binary
))
1740 (setf (epg-context-output-file context
)
1741 (epg--make-temp-file "epg-output"))
1743 (write-region plain nil input-file nil
'quiet
))
1744 (epg-start-sign context
1746 (epg-make-data-from-file input-file
)
1747 (epg-make-data-from-string plain
))
1749 (epg-wait-for-completion context
)
1750 (unless (epg-context-result-for context
'sign
)
1751 (if (epg-context-result-for context
'error
)
1752 (let ((errors (epg-context-result-for context
'error
)))
1754 (list "Sign failed" (epg-errors-to-string errors
))))))
1755 (epg-read-output context
))
1756 (epg-delete-output-file context
)
1758 (delete-file input-file
))
1759 (epg-reset context
))))
1761 (defun epg-start-encrypt (context plain recipients
1762 &optional sign always-trust
)
1763 "Initiate an encrypt operation on PLAIN.
1764 PLAIN is a data object.
1765 If RECIPIENTS is nil, it performs symmetric encryption.
1767 If you use this function, you will need to wait for the completion of
1768 `epg-gpg-program' by using `epg-wait-for-completion' and call
1769 `epg-reset' to clear a temporary output file.
1770 If you are unsure, use synchronous version of this function
1771 `epg-encrypt-file' or `epg-encrypt-string' instead."
1772 (setf (epg-context-operation context
) 'encrypt
)
1773 (setf (epg-context-result context
) nil
)
1775 (append (if always-trust
'("--always-trust"))
1776 (if recipients
'("--encrypt") '("--symmetric"))
1777 (if sign
'("--sign"))
1784 (car (epg-key-sub-key-list
1786 (epg-context-signers context
))))
1788 (epg--args-from-sig-notations
1789 (epg-context-sig-notations context
)))
1795 (car (epg-key-sub-key-list recipient
)))))
1797 (if (epg-data-file plain
)
1798 (list "--" (epg-data-file plain
)))))
1799 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1800 (unless (eq (epg-context-protocol context
) 'CMS
)
1801 (epg-wait-for-status context
1802 (if sign
'("BEGIN_SIGNING") '("BEGIN_ENCRYPTION"))))
1803 (when (epg-data-string plain
)
1804 (if (eq (process-status (epg-context-process context
)) 'run
)
1805 (process-send-string (epg-context-process context
)
1806 (epg-data-string plain
)))
1807 (if (eq (process-status (epg-context-process context
)) 'run
)
1808 (process-send-eof (epg-context-process context
)))))
1810 (defun epg-encrypt-file (context plain recipients
1811 cipher
&optional sign always-trust
)
1812 "Encrypt a file PLAIN and store the result to a file CIPHER.
1813 If CIPHER is nil, it returns the result as a string.
1814 If RECIPIENTS is nil, it performs symmetric encryption."
1817 (setf (epg-context-output-file context
)
1818 (or cipher
(epg--make-temp-file "epg-output")))
1819 (epg-start-encrypt context
(epg-make-data-from-file plain
)
1820 recipients sign always-trust
)
1821 (epg-wait-for-completion context
)
1822 (let ((errors (epg-context-result-for context
'error
)))
1824 (not (epg-context-result-for context
'sign
)))
1826 (list "Sign failed" (epg-errors-to-string errors
))))
1829 (list "Encrypt failed" (epg-errors-to-string errors
)))))
1831 (epg-read-output context
)))
1833 (epg-delete-output-file context
))
1834 (epg-reset context
)))
1836 (defun epg-encrypt-string (context plain recipients
1837 &optional sign always-trust
)
1838 "Encrypt a string PLAIN.
1839 If RECIPIENTS is nil, it performs symmetric encryption."
1841 (unless (or (not sign
)
1842 (eq (epg-context-protocol context
) 'CMS
)
1845 (epg-check-configuration (epg-configuration))
1848 (epg--make-temp-file "epg-input")))
1849 (coding-system-for-write 'binary
))
1852 (setf (epg-context-output-file context
)
1853 (epg--make-temp-file "epg-output"))
1855 (write-region plain nil input-file nil
'quiet
))
1856 (epg-start-encrypt context
1858 (epg-make-data-from-file input-file
)
1859 (epg-make-data-from-string plain
))
1860 recipients sign always-trust
)
1861 (epg-wait-for-completion context
)
1862 (let ((errors (epg-context-result-for context
'error
)))
1864 (not (epg-context-result-for context
'sign
)))
1866 (list "Sign failed" (epg-errors-to-string errors
))))
1869 (list "Encrypt failed" (epg-errors-to-string errors
)))))
1870 (epg-read-output context
))
1871 (epg-delete-output-file context
)
1873 (delete-file input-file
))
1874 (epg-reset context
))))
1876 (defun epg-start-export-keys (context keys
)
1877 "Initiate an export keys operation.
1879 If you use this function, you will need to wait for the completion of
1880 `epg-gpg-program' by using `epg-wait-for-completion' and call
1881 `epg-reset' to clear a temporary output file.
1882 If you are unsure, use synchronous version of this function
1883 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1884 (setf (epg-context-operation context
) 'export-keys
)
1885 (setf (epg-context-result context
) nil
)
1886 (epg--start context
(cons "--export"
1890 (car (epg-key-sub-key-list key
))))
1893 (defun epg-export-keys-to-file (context keys file
)
1894 "Extract public KEYS."
1897 (setf (epg-context-output-file context
)
1898 (or file
(epg--make-temp-file "epg-output")))
1899 (epg-start-export-keys context keys
)
1900 (epg-wait-for-completion context
)
1901 (let ((errors (epg-context-result-for context
'error
)))
1904 (list "Export keys failed"
1905 (epg-errors-to-string errors
)))))
1907 (epg-read-output context
)))
1909 (epg-delete-output-file context
))
1910 (epg-reset context
)))
1912 (defun epg-export-keys-to-string (context keys
)
1913 "Extract public KEYS and return them as a string."
1914 (epg-export-keys-to-file context keys nil
))
1916 (defun epg-start-import-keys (context keys
)
1917 "Initiate an import keys operation.
1918 KEYS is a data object.
1920 If you use this function, you will need to wait for the completion of
1921 `epg-gpg-program' by using `epg-wait-for-completion' and call
1922 `epg-reset' to clear a temporary output file.
1923 If you are unsure, use synchronous version of this function
1924 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1925 (setf (epg-context-operation context
) 'import-keys
)
1926 (setf (epg-context-result context
) nil
)
1927 (epg--start context
(if (epg-data-file keys
)
1928 (list "--import" "--" (epg-data-file keys
))
1930 (when (epg-data-string keys
)
1931 (if (eq (process-status (epg-context-process context
)) 'run
)
1932 (process-send-string (epg-context-process context
)
1933 (epg-data-string keys
)))
1934 (if (eq (process-status (epg-context-process context
)) 'run
)
1935 (process-send-eof (epg-context-process context
)))))
1937 (defun epg--import-keys-1 (context keys
)
1940 (epg-start-import-keys context keys
)
1941 (epg-wait-for-completion context
)
1942 (let ((errors (epg-context-result-for context
'error
)))
1945 (list "Import keys failed"
1946 (epg-errors-to-string errors
))))))
1947 (epg-reset context
)))
1949 (defun epg-import-keys-from-file (context keys
)
1950 "Add keys from a file KEYS."
1951 (epg--import-keys-1 context
(epg-make-data-from-file keys
)))
1953 (defun epg-import-keys-from-string (context keys
)
1954 "Add keys from a string KEYS."
1955 (epg--import-keys-1 context
(epg-make-data-from-string keys
)))
1957 (defun epg-start-receive-keys (context key-id-list
)
1958 "Initiate a receive key operation.
1959 KEY-ID-LIST is a list of key IDs.
1961 If you use this function, you will need to wait for the completion of
1962 `epg-gpg-program' by using `epg-wait-for-completion' and call
1963 `epg-reset' to clear a temporary output file.
1964 If you are unsure, use synchronous version of this function
1965 `epg-receive-keys' instead."
1966 (setf (epg-context-operation context
) 'receive-keys
)
1967 (setf (epg-context-result context
) nil
)
1968 (epg--start context
(cons "--recv-keys" key-id-list
)))
1970 (defun epg-receive-keys (context keys
)
1971 "Add keys from server.
1972 KEYS is a list of key IDs"
1975 (epg-start-receive-keys context keys
)
1976 (epg-wait-for-completion context
)
1977 (let ((errors (epg-context-result-for context
'error
)))
1980 (list "Receive keys failed"
1981 (epg-errors-to-string errors
))))))
1982 (epg-reset context
)))
1984 (defalias 'epg-import-keys-from-server
'epg-receive-keys
)
1986 (defun epg-start-delete-keys (context keys
&optional allow-secret
)
1987 "Initiate a delete keys operation.
1989 If you use this function, you will need to wait for the completion of
1990 `epg-gpg-program' by using `epg-wait-for-completion' and call
1991 `epg-reset' to clear a temporary output file.
1992 If you are unsure, use synchronous version of this function
1993 `epg-delete-keys' instead."
1994 (setf (epg-context-operation context
) 'delete-keys
)
1995 (setf (epg-context-result context
) nil
)
1996 (epg--start context
(cons (if allow-secret
1997 "--delete-secret-key"
2002 (car (epg-key-sub-key-list key
))))
2005 (defun epg-delete-keys (context keys
&optional allow-secret
)
2006 "Delete KEYS from the key ring."
2009 (epg-start-delete-keys context keys allow-secret
)
2010 (epg-wait-for-completion context
)
2011 (let ((errors (epg-context-result-for context
'error
)))
2014 (list "Delete keys failed"
2015 (epg-errors-to-string errors
))))))
2016 (epg-reset context
)))
2018 (defun epg-start-sign-keys (context keys
&optional local
)
2019 "Initiate a sign keys operation.
2021 If you use this function, you will need to wait for the completion of
2022 `epg-gpg-program' by using `epg-wait-for-completion' and call
2023 `epg-reset' to clear a temporary output file.
2024 If you are unsure, use synchronous version of this function
2025 `epg-sign-keys' instead."
2026 (declare (obsolete nil
"23.1"))
2027 (setf (epg-context-operation context
) 'sign-keys
)
2028 (setf (epg-context-result context
) nil
)
2029 (epg--start context
(cons (if local
2035 (car (epg-key-sub-key-list key
))))
2038 (defun epg-sign-keys (context keys
&optional local
)
2039 "Sign KEYS from the key ring."
2040 (declare (obsolete nil
"23.1"))
2043 (epg-start-sign-keys context keys local
)
2044 (epg-wait-for-completion context
)
2045 (let ((errors (epg-context-result-for context
'error
)))
2048 (list "Sign keys failed"
2049 (epg-errors-to-string errors
))))))
2050 (epg-reset context
)))
2052 (defun epg-start-generate-key (context parameters
)
2053 "Initiate a key generation.
2054 PARAMETERS specifies parameters for the key.
2056 If you use this function, you will need to wait for the completion of
2057 `epg-gpg-program' by using `epg-wait-for-completion' and call
2058 `epg-reset' to clear a temporary output file.
2059 If you are unsure, use synchronous version of this function
2060 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2061 (setf (epg-context-operation context
) 'generate-key
)
2062 (setf (epg-context-result context
) nil
)
2063 (if (epg-data-file parameters
)
2064 (epg--start context
(list "--batch" "--genkey" "--"
2065 (epg-data-file parameters
)))
2066 (epg--start context
'("--batch" "--genkey"))
2067 (if (eq (process-status (epg-context-process context
)) 'run
)
2068 (process-send-string (epg-context-process context
)
2069 (epg-data-string parameters
)))
2070 (if (eq (process-status (epg-context-process context
)) 'run
)
2071 (process-send-eof (epg-context-process context
)))))
2073 (defun epg-generate-key-from-file (context parameters
)
2074 "Generate a new key pair.
2075 PARAMETERS is a file which tells how to create the key."
2078 (epg-start-generate-key context
(epg-make-data-from-file parameters
))
2079 (epg-wait-for-completion context
)
2080 (let ((errors (epg-context-result-for context
'error
)))
2083 (list "Generate key failed"
2084 (epg-errors-to-string errors
))))))
2085 (epg-reset context
)))
2087 (defun epg-generate-key-from-string (context parameters
)
2088 "Generate a new key pair.
2089 PARAMETERS is a string which tells how to create the key."
2092 (epg-start-generate-key context
(epg-make-data-from-string parameters
))
2093 (epg-wait-for-completion context
)
2094 (let ((errors (epg-context-result-for context
'error
)))
2097 (list "Generate key failed"
2098 (epg-errors-to-string errors
))))))
2099 (epg-reset context
)))
2101 (defun epg-start-edit-key (context key edit-callback handback
)
2102 "Initiate an edit operation on KEY.
2104 EDIT-CALLBACK is called from process filter and takes 3
2105 arguments: the context, a status, an argument string, and the
2108 If you use this function, you will need to wait for the completion of
2109 `epg-gpg-program' by using `epg-wait-for-completion' and call
2110 `epg-reset' to clear a temporary output file.
2111 If you are unsure, use synchronous version of this function
2112 `epg-edit-key' instead."
2113 (setf (epg-context-operation context
) 'edit-key
)
2114 (setf (epg-context-result context
) nil
)
2115 (setf (epg-context-edit-callback context
) (cons edit-callback handback
))
2116 (epg--start context
(list "--edit-key"
2118 (car (epg-key-sub-key-list key
))))))
2120 (defun epg-edit-key (context key edit-callback handback
)
2121 "Edit KEY in the keyring."
2124 (epg-start-edit-key context key edit-callback handback
)
2125 (epg-wait-for-completion context
)
2126 (let ((errors (epg-context-result-for context
'error
)))
2129 (list "Edit key failed"
2130 (epg-errors-to-string errors
))))))
2131 (epg-reset context
)))
2133 (defun epg--decode-percent-escape (string)
2135 (while (string-match "%\\(\\(%\\)\\|\\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2137 (if (match-beginning 2)
2138 (setq string
(replace-match "%" t t string
)
2139 index
(1- (match-end 0)))
2140 (setq string
(replace-match
2141 (string (string-to-number (match-string 3 string
) 16))
2143 index
(- (match-end 0) 2))))
2146 (defun epg--decode-hexstring (string)
2148 (while (eq index
(string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index
))
2149 (setq string
(replace-match (string (string-to-number
2150 (match-string 0 string
) 16))
2152 index
(1- (match-end 0))))
2155 (defun epg--decode-quotedstring (string)
2157 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2158 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2160 (if (match-beginning 2)
2161 (setq string
(replace-match "\\2" t nil string
)
2162 index
(1- (match-end 0)))
2163 (if (match-beginning 3)
2164 (setq string
(replace-match (string (string-to-number
2165 (match-string 0 string
) 16))
2167 index
(- (match-end 0) 2)))))
2170 (defun epg-dn-from-string (string)
2171 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2172 The return value is an alist mapping from types to values."
2174 (length (length string
))
2175 alist type value group
)
2176 (while (< index length
)
2177 (if (eq index
(string-match "[ \t\n\r]*" string index
))
2178 (setq index
(match-end 0)))
2179 (if (eq index
(string-match
2180 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2182 (setq type
(match-string 1 string
)
2183 index
(match-end 0))
2184 (if (eq index
(string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2186 (setq type
(match-string 1 string
)
2187 index
(match-end 0))))
2189 (error "Invalid type"))
2190 (if (eq index
(string-match
2191 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2193 (setq index
(match-end 0)
2194 value
(epg--decode-quotedstring (match-string 0 string
)))
2195 (if (eq index
(string-match "#\\([0-9A-Fa-f]+\\)" string index
))
2196 (setq index
(match-end 0)
2197 value
(epg--decode-hexstring (match-string 1 string
)))
2198 (if (eq index
(string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2200 (setq index
(match-end 0)
2201 value
(epg--decode-quotedstring
2202 (match-string 0 string
))))))
2204 (if (stringp (car (car alist
)))
2205 (setcar alist
(list (cons type value
) (car alist
)))
2206 (setcar alist
(cons (cons type value
) (car alist
))))
2207 (if (consp (car (car alist
)))
2208 (setcar alist
(nreverse (car alist
))))
2209 (setq alist
(cons (cons type value
) alist
)
2212 (if (eq index
(string-match "[ \t\n\r]*\\([,;+]\\)" string index
))
2213 (setq index
(match-end 0)
2214 group
(eq (aref string
(match-beginning 1)) ?
+))))
2217 (defun epg-decode-dn (alist)
2218 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2219 Type names are resolved using `epg-dn-type-alist'."
2222 (if (stringp (car rdn
))
2223 (let ((entry (assoc (car rdn
) epg-dn-type-alist
)))
2225 (format "%s=%s" (cdr entry
) (cdr rdn
))
2226 (format "%s=%s" (car rdn
) (cdr rdn
))))
2227 (concat "(" (epg-decode-dn rdn
) ")")))
2233 ;;; epg.el ends here