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 (cl-defstruct (epg-context
179 (:constructor epg-context--make
180 (protocol &optional armor textmode include-certs
181 cipher-algorithm digest-algorithm
185 (let ((configuration (epg-find-configuration protocol
)))
186 (unless configuration
188 (list "no usable configuration" protocol
)))
189 (alist-get 'program configuration
)))))
194 (home-directory epg-gpg-home-directory
)
201 (passphrase-callback (list #'epg-passphrase-callback-function
))
214 ;; This is not an alias, just so we can mark it as autoloaded.
216 (defun epg-make-context (&optional protocol armor textmode include-certs
217 cipher-algorithm digest-algorithm
219 "Return a context object."
220 (epg-context--make (or protocol
'OpenPGP
)
221 armor textmode include-certs
222 cipher-algorithm digest-algorithm
225 (defun epg-context-set-armor (context armor
)
226 "Specify if the output should be ASCII armored in CONTEXT."
227 (declare (obsolete setf
"25.1"))
228 (setf (epg-context-armor context
) armor
))
230 (defun epg-context-set-textmode (context textmode
)
231 "Specify if canonical text mode should be used in CONTEXT."
232 (declare (obsolete setf
"25.1"))
233 (setf (epg-context-textmode context
) textmode
))
235 (defun epg-context-set-passphrase-callback (context
237 "Set the function used to query passphrase.
239 PASSPHRASE-CALLBACK is either a function, or a cons-cell whose
240 car is a function and cdr is a callback data.
242 The function gets three arguments: the context, the key-id in
243 question, and the callback data (if any).
245 The callback may not be called if you use GnuPG 2.x, which relies
246 on the external program called `gpg-agent' for passphrase query.
247 If you really want to intercept passphrase query, consider
248 installing GnuPG 1.x _along with_ GnuPG 2.x, which does passphrase
249 query by itself and Emacs can intercept them."
250 ;; (declare (obsolete setf "25.1"))
251 (setf (epg-context-passphrase-callback context
)
252 (if (functionp passphrase-callback
)
253 (list passphrase-callback
)
254 passphrase-callback
)))
256 (defun epg-context-set-progress-callback (context
258 "Set the function which handles progress update.
260 PROGRESS-CALLBACK is either a function, or a cons-cell whose
261 car is a function and cdr is a callback data.
263 The function gets six arguments: the context, the operation
264 description, the character to display a progress unit, the
265 current amount done, the total amount to be done, and the
266 callback data (if any)."
267 (setf (epg-context-progress-callback context
)
268 (if (functionp progress-callback
)
269 (list progress-callback
)
272 (defun epg-context-set-signers (context signers
)
273 "Set the list of key-id for signing."
274 (declare (obsolete setf
"25.1"))
275 (setf (epg-context-signers context
) signers
))
277 (cl-defstruct (epg-signature
279 (:constructor epg-make-signature
280 (status &optional key-id
))
295 (cl-defstruct (epg-new-signature
297 (:constructor epg-make-new-signature
298 (type pubkey-algorithm digest-algorithm
299 class creation-time fingerprint
))
302 (type nil
:read-only t
)
303 (pubkey-algorithm nil
:read-only t
)
304 (digest-algorithm nil
:read-only t
)
305 (class nil
:read-only t
)
306 (creation-time nil
:read-only t
)
307 (fingerprint nil
:read-only t
))
309 (cl-defstruct (epg-key
311 (:constructor epg-make-key
(owner-trust))
314 (owner-trust nil
:read-only t
)
315 sub-key-list user-id-list
)
317 (cl-defstruct (epg-sub-key
319 (:constructor epg-make-sub-key
320 (validity capability secret-p algorithm length id
321 creation-time expiration-time
))
324 validity capability secret-p algorithm length id
325 creation-time expiration-time fingerprint
)
327 (cl-defstruct (epg-user-id
329 (:constructor epg-make-user-id
(validity string
))
332 validity string signature-list
)
334 (cl-defstruct (epg-key-signature
336 (:constructor epg-make-key-signature
337 (validity pubkey-algorithm key-id creation-time
338 expiration-time user-id class
342 validity pubkey-algorithm key-id creation-time
343 expiration-time user-id class
346 (cl-defstruct (epg-sig-notation
348 (:constructor epg-make-sig-notation
349 (name value
&optional human-readable critical
))
352 name value human-readable critical
)
354 (cl-defstruct (epg-import-status
356 (:constructor epg-make-import-status
358 &optional reason new user-id signature sub-key secret
))
361 fingerprint reason new user-id signature sub-key secret
)
363 (cl-defstruct (epg-import-result
365 (:constructor epg-make-import-result
366 (considered no-user-id imported imported-rsa
367 unchanged new-user-ids new-sub-keys
368 new-signatures new-revocations
369 secret-read secret-imported
370 secret-unchanged not-imported
374 considered no-user-id imported imported-rsa
375 unchanged new-user-ids new-sub-keys
376 new-signatures new-revocations
377 secret-read secret-imported
378 secret-unchanged not-imported
381 (defun epg-context-result-for (context name
)
382 "Return the result of CONTEXT associated with NAME."
383 (cdr (assq name
(epg-context-result context
))))
385 (defun epg-context-set-result-for (context name value
)
386 "Set the result of CONTEXT associated with NAME to VALUE."
387 (let* ((result (epg-context-result context
))
388 (entry (assq name result
)))
391 (setf (epg-context-result context
) (cons (cons name value
) result
)))))
393 (defun epg-signature-to-string (signature)
394 "Convert SIGNATURE to a human readable string."
395 (let* ((user-id (cdr (assoc (epg-signature-key-id signature
)
397 (pubkey-algorithm (epg-signature-pubkey-algorithm signature
))
398 (key-id (epg-signature-key-id signature
)))
400 (cond ((eq (epg-signature-status signature
) 'good
)
401 "Good signature from ")
402 ((eq (epg-signature-status signature
) 'bad
)
403 "Bad signature from ")
404 ((eq (epg-signature-status signature
) 'expired
)
405 "Expired signature from ")
406 ((eq (epg-signature-status signature
) 'expired-key
)
407 "Signature made by expired key ")
408 ((eq (epg-signature-status signature
) 'revoked-key
)
409 "Signature made by revoked key ")
410 ((eq (epg-signature-status signature
) 'no-pubkey
)
411 "No public key for "))
415 (if (stringp user-id
)
417 (epg-decode-dn user-id
)))
419 (if (epg-signature-validity signature
)
420 (format " (trust %s)" (epg-signature-validity signature
))
422 (if (epg-signature-creation-time signature
)
423 (format-time-string " created at %Y-%m-%dT%T%z"
424 (epg-signature-creation-time signature
))
428 (or (cdr (assq pubkey-algorithm epg-pubkey-algorithm-alist
))
429 (format "(unknown algorithm %d)" pubkey-algorithm
)))
432 (defun epg-verify-result-to-string (verify-result)
433 "Convert VERIFY-RESULT to a human readable string."
434 (mapconcat #'epg-signature-to-string verify-result
"\n"))
436 (defun epg-new-signature-to-string (new-signature)
437 "Convert NEW-SIGNATURE to a human readable string."
439 (cond ((eq (epg-new-signature-type new-signature
) 'detached
)
440 "Detached signature ")
441 ((eq (epg-new-signature-type new-signature
) 'clear
)
442 "Cleartext signature ")
445 (cdr (assq (epg-new-signature-pubkey-algorithm new-signature
)
446 epg-pubkey-algorithm-alist
))
448 (cdr (assq (epg-new-signature-digest-algorithm new-signature
)
449 epg-digest-algorithm-alist
))
451 (format "%02X " (epg-new-signature-class new-signature
))
452 (epg-new-signature-fingerprint new-signature
)))
454 (defun epg-import-result-to-string (import-result)
455 "Convert IMPORT-RESULT to a human readable string."
456 (concat (format "Total number processed: %d\n"
457 (epg-import-result-considered import-result
))
458 (if (> (epg-import-result-not-imported import-result
) 0)
459 (format " skipped new keys: %d\n"
460 (epg-import-result-not-imported import-result
)))
461 (if (> (epg-import-result-no-user-id import-result
) 0)
462 (format " w/o user IDs: %d\n"
463 (epg-import-result-no-user-id import-result
)))
464 (if (> (epg-import-result-imported import-result
) 0)
465 (concat (format " imported: %d"
466 (epg-import-result-imported import-result
))
467 (if (> (epg-import-result-imported-rsa import-result
) 0)
469 (epg-import-result-imported-rsa
472 (if (> (epg-import-result-unchanged import-result
) 0)
473 (format " unchanged: %d\n"
474 (epg-import-result-unchanged import-result
)))
475 (if (> (epg-import-result-new-user-ids import-result
) 0)
476 (format " new user IDs: %d\n"
477 (epg-import-result-new-user-ids import-result
)))
478 (if (> (epg-import-result-new-sub-keys import-result
) 0)
479 (format " new subkeys: %d\n"
480 (epg-import-result-new-sub-keys import-result
)))
481 (if (> (epg-import-result-new-signatures import-result
) 0)
482 (format " new signatures: %d\n"
483 (epg-import-result-new-signatures import-result
)))
484 (if (> (epg-import-result-new-revocations import-result
) 0)
485 (format " new key revocations: %d\n"
486 (epg-import-result-new-revocations import-result
)))
487 (if (> (epg-import-result-secret-read import-result
) 0)
488 (format " secret keys read: %d\n"
489 (epg-import-result-secret-read import-result
)))
490 (if (> (epg-import-result-secret-imported import-result
) 0)
491 (format " secret keys imported: %d\n"
492 (epg-import-result-secret-imported import-result
)))
493 (if (> (epg-import-result-secret-unchanged import-result
) 0)
494 (format " secret keys unchanged: %d\n"
495 (epg-import-result-secret-unchanged import-result
)))))
497 (defun epg-error-to-string (error)
499 ((eq (car error
) 'exit
)
501 ((eq (car error
) 'quit
)
503 ((eq (car error
) 'no-data
)
504 (let ((entry (assq (cdr error
) epg-no-data-reason-alist
)))
506 (format "No data (%s)" (downcase (cdr entry
)))
508 ((eq (car error
) 'unexpected
)
509 (let ((entry (assq (cdr error
) epg-unexpected-reason-alist
)))
511 (format "Unexpected (%s)" (downcase (cdr entry
)))
513 ((eq (car error
) 'bad-armor
)
515 ((memq (car error
) '(invalid-recipient invalid-signer
))
517 (if (eq (car error
) 'invalid-recipient
)
518 "Unusable public key"
519 "Unusable secret key")
520 (let ((entry (assq 'requested
(cdr error
))))
522 (format ": %s" (cdr entry
))
524 (let ((entry (assq 'reason
(cdr error
))))
526 (> (cdr entry
) 0) ;no specific reason given
527 (setq entry
(assq (cdr entry
)
528 epg-invalid-recipients-reason-alist
)))
529 (format " (%s)" (downcase (cdr entry
)))
531 ((eq (car error
) 'no-pubkey
)
532 (format "No public key: %s" (cdr error
)))
533 ((eq (car error
) 'no-seckey
)
534 (format "No secret key: %s" (cdr error
)))
535 ((eq (car error
) 'no-recipients
)
537 ((eq (car error
) 'no-signers
)
539 ((eq (car error
) 'delete-problem
)
540 (let ((entry (assq (cdr error
) epg-delete-problem-reason-alist
)))
542 (format "Delete problem (%s)" (downcase (cdr entry
)))
544 ((eq (car error
) 'key-not-created
)
547 (defun epg-errors-to-string (errors)
548 (mapconcat #'epg-error-to-string errors
"; "))
550 (defun epg--start (context args
)
551 "Start `epg-gpg-program' in a subprocess with given ARGS."
552 (if (and (epg-context-process context
)
553 (eq (process-status (epg-context-process context
)) 'run
))
554 (error "%s is already running in this context"
555 (epg-context-program context
)))
556 (let* ((agent-info (getenv "GPG_AGENT_INFO"))
557 (args (append (list "--no-tty"
560 (if (and (not (eq (epg-context-protocol context
) 'CMS
))
561 (string-match ":" (or agent-info
"")))
563 (if (and (not (eq (epg-context-protocol context
) 'CMS
))
564 (epg-context-progress-callback context
))
565 '("--enable-progress-filter"))
566 (if (epg-context-home-directory context
)
568 (epg-context-home-directory context
)))
569 (unless (eq (epg-context-protocol context
) 'CMS
)
570 '("--command-fd" "0"))
571 (if (epg-context-armor context
) '("--armor"))
572 (if (epg-context-textmode context
) '("--textmode"))
573 (if (epg-context-output-file context
)
574 (list "--output" (epg-context-output-file context
)))
575 (if (epg-context-pinentry-mode context
)
576 (list "--pinentry-mode"
577 (symbol-name (epg-context-pinentry-mode
580 (process-environment process-environment
)
581 (buffer (generate-new-buffer " *epg*"))
586 (agent-mtime '(0 0 0 0)))
587 ;; Set GPG_TTY and TERM for pinentry-curses. Note that we can't
588 ;; use `terminal-name' here to get the real pty name for the child
589 ;; process, though /dev/fd/0" is not portable.
590 (unless (memq system-type
'(ms-dos windows-nt
))
593 (when (= (call-process "tty" "/dev/fd/0" t
) 0)
595 (setq terminal-name
(buffer-string)))
598 (setq process-environment
599 (cons (concat "GPG_TTY=" terminal-name
)
600 (cons "TERM=xterm" process-environment
))))
601 (setq process-environment
602 (cons (format "INSIDE_EMACS=%s,epg" emacs-version
)
603 process-environment
))
604 ;; Record modified time of gpg-agent socket to restore the Emacs
605 ;; frame on text terminal in `epg-wait-for-completion'.
607 ;; <https://lists.gnu.org/r/emacs-devel/2007-02/msg00755.html>
609 (when (and agent-info
(string-match "\\(.*\\):[0-9]+:[0-9]+" agent-info
))
610 (setq agent-file
(match-string 1 agent-info
)
611 agent-mtime
(or (nth 5 (file-attributes agent-file
)) '(0 0 0 0))))
614 (unless epg-debug-buffer
615 (setq epg-debug-buffer
(generate-new-buffer " *epg-debug*")))
616 (set-buffer epg-debug-buffer
)
617 (goto-char (point-max))
618 (insert (if agent-info
619 (format "GPG_AGENT_INFO=%s\n" agent-info
)
620 "GPG_AGENT_INFO is not set\n")
622 (epg-context-program context
)
623 (mapconcat #'identity args
" ")))))
624 (with-current-buffer buffer
625 (if (fboundp 'set-buffer-multibyte
)
626 (set-buffer-multibyte nil
))
627 (make-local-variable 'epg-last-status
)
628 (setq epg-last-status nil
)
629 (make-local-variable 'epg-read-point
)
630 (setq epg-read-point
(point-min))
631 (make-local-variable 'epg-process-filter-running
)
632 (setq epg-process-filter-running nil
)
633 (make-local-variable 'epg-pending-status-list
)
634 (setq epg-pending-status-list nil
)
635 (make-local-variable 'epg-key-id
)
636 (setq epg-key-id nil
)
637 (make-local-variable 'epg-context
)
638 (setq epg-context context
)
639 (make-local-variable 'epg-agent-file
)
640 (setq epg-agent-file agent-file
)
641 (make-local-variable 'epg-agent-mtime
)
642 (setq epg-agent-mtime agent-mtime
))
644 (make-pipe-process :name
"epg-error"
645 :buffer
(generate-new-buffer " *epg-error*")
646 ;; Suppress "XXX finished" line.
649 (setf (epg-context-error-buffer context
) (process-buffer error-process
))
651 (setq process
(make-process :name
"epg"
653 :command
(cons (epg-context-program context
)
655 :connection-type
'pipe
656 :coding
'(binary . binary
)
657 :filter
#'epg--process-filter
658 :stderr error-process
660 (setf (epg-context-process context
) process
)))
662 (defun epg--process-filter (process input
)
666 (setq epg-debug-buffer
(generate-new-buffer " *epg-debug*")))
667 (goto-char (point-max))
669 (if (buffer-live-p (process-buffer process
))
670 (with-current-buffer (process-buffer process
)
672 (goto-char (point-max))
674 (unless epg-process-filter-running
675 (let ((epg-process-filter-running t
))
676 (goto-char epg-read-point
)
678 (while (looking-at ".*\n") ;the input line finished
679 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
680 (let ((status (match-string 1))
681 (string (match-string 2))
683 (if (member status epg-pending-status-list
)
684 (setq epg-pending-status-list nil
))
685 ;; When editing a key, delegate all interaction
687 (if (eq (epg-context-operation epg-context
) 'edit-key
)
688 (funcall (car (epg-context-edit-callback
693 (cdr (epg-context-edit-callback
695 ;; Otherwise call epg--status-STATUS function.
696 (setq symbol
(intern-soft (concat "epg--status-"
700 (funcall symbol epg-context string
)))
701 (setq epg-last-status
(cons status string
))))
703 (setq epg-read-point
(point)))))))))
705 (defun epg-read-output (context)
706 "Read the output file CONTEXT and return the content as a string."
708 (if (fboundp 'set-buffer-multibyte
)
709 (set-buffer-multibyte nil
))
710 (if (file-exists-p (epg-context-output-file context
))
711 (let ((coding-system-for-read 'binary
))
712 (insert-file-contents (epg-context-output-file context
))
715 (defun epg-wait-for-status (context status-list
)
716 "Wait until one of elements in STATUS-LIST arrives."
717 (with-current-buffer (process-buffer (epg-context-process context
))
718 (setq epg-pending-status-list status-list
)
719 (while (and (eq (process-status (epg-context-process context
)) 'run
)
720 epg-pending-status-list
)
721 (accept-process-output (epg-context-process context
) 1))
722 (if epg-pending-status-list
723 (epg-context-set-result-for
726 (epg-context-result-for context
'error
))))))
728 (defun epg-wait-for-completion (context)
729 "Wait until the `epg-gpg-program' process completes."
730 (while (eq (process-status (epg-context-process context
)) 'run
)
731 (accept-process-output (epg-context-process context
) 1))
732 ;; This line is needed to run the process-filter right now.
734 ;; Restore Emacs frame on text terminal, when pinentry-curses has terminated.
735 (if (with-current-buffer (process-buffer (epg-context-process context
))
737 (time-less-p epg-agent-mtime
738 (or (nth 5 (file-attributes epg-agent-file
)) 0))))
740 (epg-context-set-result-for
742 (nreverse (epg-context-result-for context
'error
)))
743 (setf (epg-context-error-output context
)
744 (with-current-buffer (epg-context-error-buffer context
)
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 (if (buffer-live-p (epg-context-error-buffer context
))
753 (kill-buffer (epg-context-error-buffer context
)))
754 (setf (epg-context-process context
) nil
)
755 (setf (epg-context-edit-callback context
) nil
))
757 (defun epg-delete-output-file (context)
758 "Delete the output file of CONTEXT."
759 (if (and (epg-context-output-file context
)
760 (file-exists-p (epg-context-output-file context
)))
761 (delete-file (epg-context-output-file context
))))
763 (defun epg--status-USERID_HINT (_context string
)
764 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string
)
765 (let* ((key-id (match-string 1 string
))
766 (user-id (match-string 2 string
))
767 (entry (assoc key-id epg-user-id-alist
)))
769 (setq user-id
(decode-coding-string
770 (epg--decode-percent-escape user-id
)
774 (setcdr entry user-id
)
775 (setq epg-user-id-alist
(cons (cons key-id user-id
)
776 epg-user-id-alist
))))))
778 (defun epg--status-NEED_PASSPHRASE (_context string
)
779 (if (string-match "\\`\\([^ ]+\\)" string
)
780 (setq epg-key-id
(match-string 1 string
))))
782 (defun epg--status-NEED_PASSPHRASE_SYM (_context _string
)
783 (setq epg-key-id
'SYM
))
785 (defun epg--status-NEED_PASSPHRASE_PIN (_context _string
)
786 (setq epg-key-id
'PIN
))
788 (defun epg--status-GET_HIDDEN (context string
)
789 (when (and epg-key-id
790 (string-match "\\`passphrase\\." string
))
791 (unless (epg-context-passphrase-callback context
)
792 (error "passphrase-callback not set"))
795 passphrase-with-new-line
796 encoded-passphrase-with-new-line
)
802 (car (epg-context-passphrase-callback context
))
805 (cdr (epg-context-passphrase-callback context
))))
807 (setq passphrase-with-new-line
(concat passphrase
"\n"))
808 (clear-string passphrase
)
809 (setq passphrase nil
)
810 (if epg-passphrase-coding-system
812 (setq encoded-passphrase-with-new-line
813 (encode-coding-string
814 passphrase-with-new-line
815 (coding-system-change-eol-conversion
816 epg-passphrase-coding-system
'unix
)))
817 (clear-string passphrase-with-new-line
)
818 (setq passphrase-with-new-line nil
))
819 (setq encoded-passphrase-with-new-line
820 passphrase-with-new-line
821 passphrase-with-new-line nil
))
822 (process-send-string (epg-context-process context
)
823 encoded-passphrase-with-new-line
)))
825 (epg-context-set-result-for
828 (epg-context-result-for context
'error
)))
829 (delete-process (epg-context-process context
))))
831 (clear-string passphrase
))
832 (if passphrase-with-new-line
833 (clear-string passphrase-with-new-line
))
834 (if encoded-passphrase-with-new-line
835 (clear-string encoded-passphrase-with-new-line
))))))
837 (defun epg--prompt-GET_BOOL (_context string
)
838 (let ((entry (assoc string epg-prompt-alist
)))
839 (y-or-n-p (if entry
(cdr entry
) (concat string
"? ")))))
841 (defun epg--prompt-GET_BOOL-untrusted_key.override
(_context _string
)
842 (y-or-n-p (if (and (equal (car epg-last-status
) "USERID_HINT")
843 (string-match "\\`\\([^ ]+\\) \\(.*\\)"
844 (cdr epg-last-status
)))
845 (let* ((key-id (match-string 1 (cdr epg-last-status
)))
846 (user-id (match-string 2 (cdr epg-last-status
)))
847 (entry (assoc key-id epg-user-id-alist
)))
849 (setq user-id
(cdr entry
)))
850 (format "Untrusted key %s %s. Use anyway? " key-id user-id
))
851 "Use untrusted key anyway? ")))
853 (defun epg--status-GET_BOOL (context string
)
856 (if (funcall (or (intern-soft (concat "epg--prompt-GET_BOOL-" string
))
857 #'epg--prompt-GET_BOOL
)
859 (process-send-string (epg-context-process context
) "y\n")
860 (process-send-string (epg-context-process context
) "n\n"))
862 (epg-context-set-result-for
865 (epg-context-result-for context
'error
)))
866 (delete-process (epg-context-process context
))))))
868 (defun epg--status-GET_LINE (context string
)
869 (let ((entry (assoc string epg-prompt-alist
))
872 (process-send-string (epg-context-process context
)
876 (concat string
": ")))
879 (epg-context-set-result-for
882 (epg-context-result-for context
'error
)))
883 (delete-process (epg-context-process context
))))))
885 (defun epg--status-*SIG
(context status string
)
886 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string
)
887 (let* ((key-id (match-string 1 string
))
888 (user-id (match-string 2 string
))
889 (entry (assoc key-id epg-user-id-alist
)))
890 (epg-context-set-result-for
893 (cons (epg-make-signature status key-id
)
894 (epg-context-result-for context
'verify
)))
896 (if (eq (epg-context-protocol context
) 'CMS
)
897 (setq user-id
(epg-dn-from-string user-id
))
898 (setq user-id
(decode-coding-string
899 (epg--decode-percent-escape user-id
)
903 (setcdr entry user-id
)
904 (setq epg-user-id-alist
905 (cons (cons key-id user-id
) epg-user-id-alist
))))
906 (epg-context-set-result-for
909 (cons (epg-make-signature status
)
910 (epg-context-result-for context
'verify
)))))
912 (defun epg--status-GOODSIG (context string
)
913 (epg--status-*SIG context
'good string
))
915 (defun epg--status-EXPSIG (context string
)
916 (epg--status-*SIG context
'expired string
))
918 (defun epg--status-EXPKEYSIG (context string
)
919 (epg--status-*SIG context
'expired-key string
))
921 (defun epg--status-REVKEYSIG (context string
)
922 (epg--status-*SIG context
'revoked-key string
))
924 (defun epg--status-BADSIG (context string
)
925 (epg--status-*SIG context
'bad string
))
927 (defun epg--status-NO_PUBKEY (context string
)
928 (if (eq (epg-context-operation context
) 'verify
)
929 (let ((signature (car (epg-context-result-for context
'verify
))))
931 (eq (epg-signature-status signature
) 'error
)
932 (equal (epg-signature-key-id signature
) string
))
933 (setf (epg-signature-status signature
) 'no-pubkey
)))
934 (epg-context-set-result-for
936 (cons (cons 'no-pubkey string
)
937 (epg-context-result-for context
'error
)))))
939 (defun epg--status-NO_SECKEY (context string
)
940 (epg-context-set-result-for
942 (cons (cons 'no-seckey string
)
943 (epg-context-result-for context
'error
))))
945 (defun epg--time-from-seconds (seconds)
946 (let ((number-seconds (string-to-number (concat seconds
".0"))))
947 (cons (floor (/ number-seconds
65536))
948 (floor (mod number-seconds
65536)))))
950 (defun epg--status-ERRSIG (context string
)
951 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
952 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
954 (let ((signature (epg-make-signature 'error
)))
955 (epg-context-set-result-for
959 (epg-context-result-for context
'verify
)))
960 (setf (epg-signature-key-id signature
)
961 (match-string 1 string
))
962 (setf (epg-signature-pubkey-algorithm signature
)
963 (string-to-number (match-string 2 string
)))
964 (setf (epg-signature-digest-algorithm signature
)
965 (string-to-number (match-string 3 string
)))
966 (setf (epg-signature-class signature
)
967 (string-to-number (match-string 4 string
) 16))
968 (setf (epg-signature-creation-time signature
)
969 (epg--time-from-seconds (match-string 5 string
))))))
971 (defun epg--status-VALIDSIG (context string
)
972 (let ((signature (car (epg-context-result-for context
'verify
))))
974 (eq (epg-signature-status signature
) 'good
)
975 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
976 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
979 (setf (epg-signature-fingerprint signature
)
980 (match-string 1 string
))
981 (setf (epg-signature-creation-time signature
)
982 (epg--time-from-seconds (match-string 2 string
)))
983 (unless (equal (match-string 3 string
) "0")
984 (setf (epg-signature-expiration-time signature
)
985 (epg--time-from-seconds (match-string 3 string
))))
986 (setf (epg-signature-version signature
)
987 (string-to-number (match-string 4 string
)))
988 (setf (epg-signature-pubkey-algorithm signature
)
989 (string-to-number (match-string 5 string
)))
990 (setf (epg-signature-digest-algorithm signature
)
991 (string-to-number (match-string 6 string
)))
992 (setf (epg-signature-class signature
)
993 (string-to-number (match-string 7 string
) 16)))))
995 (defun epg--status-TRUST_UNDEFINED (context _string
)
996 (let ((signature (car (epg-context-result-for context
'verify
))))
998 (eq (epg-signature-status signature
) 'good
))
999 (setf (epg-signature-validity signature
) 'undefined
))))
1001 (defun epg--status-TRUST_NEVER (context _string
)
1002 (let ((signature (car (epg-context-result-for context
'verify
))))
1004 (eq (epg-signature-status signature
) 'good
))
1005 (setf (epg-signature-validity signature
) 'never
))))
1007 (defun epg--status-TRUST_MARGINAL (context _string
)
1008 (let ((signature (car (epg-context-result-for context
'verify
))))
1010 (eq (epg-signature-status signature
) 'good
))
1011 (setf (epg-signature-validity signature
) 'marginal
))))
1013 (defun epg--status-TRUST_FULLY (context _string
)
1014 (let ((signature (car (epg-context-result-for context
'verify
))))
1016 (eq (epg-signature-status signature
) 'good
))
1017 (setf (epg-signature-validity signature
) 'full
))))
1019 (defun epg--status-TRUST_ULTIMATE (context _string
)
1020 (let ((signature (car (epg-context-result-for context
'verify
))))
1022 (eq (epg-signature-status signature
) 'good
))
1023 (setf (epg-signature-validity signature
) 'ultimate
))))
1025 (defun epg--status-NOTATION_NAME (context string
)
1026 (let ((signature (car (epg-context-result-for context
'verify
))))
1028 (push (epg-make-sig-notation string nil t nil
)
1029 (epg-signature-notations signature
)))))
1031 (defun epg--status-NOTATION_DATA (context string
)
1032 (let ((signature (car (epg-context-result-for context
'verify
)))
1035 (setq notation
(car (epg-signature-notations signature
))))
1036 (setf (epg-sig-notation-value notation
) string
))))
1038 (defun epg--status-POLICY_URL (context string
)
1039 (let ((signature (car (epg-context-result-for context
'verify
))))
1041 (push (epg-make-sig-notation nil string t nil
)
1042 (epg-signature-notations signature
)))))
1044 (defun epg--status-PROGRESS (context string
)
1045 (if (and (epg-context-progress-callback context
)
1046 (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1048 (funcall (car (epg-context-progress-callback context
))
1050 (match-string 1 string
)
1051 (match-string 2 string
)
1052 (string-to-number (match-string 3 string
))
1053 (string-to-number (match-string 4 string
))
1054 (cdr (epg-context-progress-callback context
)))))
1056 (defun epg--status-ENC_TO (context string
)
1057 (if (string-match "\\`\\([0-9A-Za-z]+\\) \\([0-9]+\\) \\([0-9]+\\)" string
)
1058 (epg-context-set-result-for
1059 context
'encrypted-to
1060 (cons (list (match-string 1 string
)
1061 (string-to-number (match-string 2 string
))
1062 (string-to-number (match-string 3 string
)))
1063 (epg-context-result-for context
'encrypted-to
)))))
1065 (defun epg--status-DECRYPTION_FAILED (context _string
)
1066 (epg-context-set-result-for context
'decryption-failed t
))
1068 (defun epg--status-DECRYPTION_OKAY (context _string
)
1069 (epg-context-set-result-for context
'decryption-okay t
))
1071 (defun epg--status-NODATA (context string
)
1072 (epg-context-set-result-for
1074 (cons (cons 'no-data
(string-to-number string
))
1075 (epg-context-result-for context
'error
))))
1077 (defun epg--status-UNEXPECTED (context string
)
1078 (epg-context-set-result-for
1080 (cons (cons 'unexpected
(string-to-number string
))
1081 (epg-context-result-for context
'error
))))
1083 (defun epg--status-KEYEXPIRED (context string
)
1084 (epg-context-set-result-for
1086 (cons (list 'key-expired
(cons 'expiration-time
1087 (epg--time-from-seconds string
)))
1088 (epg-context-result-for context
'key
))))
1090 (defun epg--status-KEYREVOKED (context _string
)
1091 (epg-context-set-result-for
1093 (cons '(key-revoked)
1094 (epg-context-result-for context
'key
))))
1096 (defun epg--status-BADARMOR (context _string
)
1097 (epg-context-set-result-for
1100 (epg-context-result-for context
'error
))))
1102 (defun epg--status-INV_RECP (context string
)
1103 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string
)
1104 (epg-context-set-result-for
1106 (cons (list 'invalid-recipient
1108 (string-to-number (match-string 1 string
)))
1110 (match-string 2 string
)))
1111 (epg-context-result-for context
'error
)))))
1113 (defun epg--status-INV_SGNR (context string
)
1114 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string
)
1115 (epg-context-set-result-for
1117 (cons (list 'invalid-signer
1119 (string-to-number (match-string 1 string
)))
1121 (match-string 2 string
)))
1122 (epg-context-result-for context
'error
)))))
1124 (defun epg--status-NO_RECP (context _string
)
1125 (epg-context-set-result-for
1127 (cons '(no-recipients)
1128 (epg-context-result-for context
'error
))))
1130 (defun epg--status-NO_SGNR (context _string
)
1131 (epg-context-set-result-for
1134 (epg-context-result-for context
'error
))))
1136 (defun epg--status-DELETE_PROBLEM (context string
)
1137 (if (string-match "\\`\\([0-9]+\\)" string
)
1138 (epg-context-set-result-for
1140 (cons (cons 'delete-problem
1141 (string-to-number (match-string 1 string
)))
1142 (epg-context-result-for context
'error
)))))
1144 (defun epg--status-SIG_CREATED (context string
)
1145 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1146 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string
)
1147 (epg-context-set-result-for
1149 (cons (epg-make-new-signature
1150 (cdr (assq (aref (match-string 1 string
) 0)
1151 epg-new-signature-type-alist
))
1152 (string-to-number (match-string 2 string
))
1153 (string-to-number (match-string 3 string
))
1154 (string-to-number (match-string 4 string
) 16)
1155 (epg--time-from-seconds (match-string 5 string
))
1156 (substring string
(match-end 0)))
1157 (epg-context-result-for context
'sign
)))))
1159 (defun epg--status-KEY_CREATED (context string
)
1160 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string
)
1161 (epg-context-set-result-for
1162 context
'generate-key
1163 (cons (list (cons 'type
(string-to-char (match-string 1 string
)))
1164 (cons 'fingerprint
(match-string 2 string
)))
1165 (epg-context-result-for context
'generate-key
)))))
1167 (defun epg--status-KEY_NOT_CREATED (context _string
)
1168 (epg-context-set-result-for
1170 (cons '(key-not-created)
1171 (epg-context-result-for context
'error
))))
1173 (defun epg--status-IMPORTED (_context string
)
1174 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string
)
1175 (let* ((key-id (match-string 1 string
))
1176 (user-id (match-string 2 string
))
1177 (entry (assoc key-id epg-user-id-alist
)))
1179 (setq user-id
(decode-coding-string
1180 (epg--decode-percent-escape user-id
)
1184 (setcdr entry user-id
)
1185 (setq epg-user-id-alist
(cons (cons key-id user-id
)
1186 epg-user-id-alist
))))))
1188 (defun epg--status-IMPORT_OK (context string
)
1189 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string
)
1190 (let ((reason (string-to-number (match-string 1 string
))))
1191 (epg-context-set-result-for
1192 context
'import-status
1193 (cons (epg-make-import-status (if (match-beginning 2)
1194 (match-string 3 string
))
1196 (/= (logand reason
1) 0)
1197 (/= (logand reason
2) 0)
1198 (/= (logand reason
4) 0)
1199 (/= (logand reason
8) 0)
1200 (/= (logand reason
16) 0))
1201 (epg-context-result-for context
'import-status
))))))
1203 (defun epg--status-IMPORT_PROBLEM (context string
)
1204 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string
)
1205 (epg-context-set-result-for
1206 context
'import-status
1207 (cons (epg-make-import-status
1208 (if (match-beginning 2)
1209 (match-string 3 string
))
1210 (string-to-number (match-string 1 string
)))
1211 (epg-context-result-for context
'import-status
)))))
1213 (defun epg--status-IMPORT_RES (context string
)
1214 (when (string-match "\\`\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1215 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1216 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" string
)
1217 (epg-context-set-result-for
1219 (epg-make-import-result (string-to-number (match-string 1 string
))
1220 (string-to-number (match-string 2 string
))
1221 (string-to-number (match-string 3 string
))
1222 (string-to-number (match-string 4 string
))
1223 (string-to-number (match-string 5 string
))
1224 (string-to-number (match-string 6 string
))
1225 (string-to-number (match-string 7 string
))
1226 (string-to-number (match-string 8 string
))
1227 (string-to-number (match-string 9 string
))
1228 (string-to-number (match-string 10 string
))
1229 (string-to-number (match-string 11 string
))
1230 (string-to-number (match-string 12 string
))
1231 (string-to-number (match-string 13 string
))
1232 (epg-context-result-for context
'import-status
)))
1233 (epg-context-set-result-for context
'import-status nil
)))
1235 (defun epg-passphrase-callback-function (context key-id _handback
)
1236 (declare (obsolete epa-passphrase-callback-function
"23.1"))
1237 (if (eq key-id
'SYM
)
1238 (read-passwd "Passphrase for symmetric encryption: "
1239 (eq (epg-context-operation context
) 'encrypt
))
1241 (if (eq key-id
'PIN
)
1242 "Passphrase for PIN: "
1243 (let ((entry (assoc key-id epg-user-id-alist
)))
1245 (format "Passphrase for %s %s: " key-id
(cdr entry
))
1246 (format "Passphrase for %s: " key-id
)))))))
1248 (defun epg--list-keys-1 (context name mode
)
1249 (let ((args (append (if (epg-context-home-directory context
)
1251 (epg-context-home-directory context
)))
1252 '("--with-colons" "--no-greeting" "--batch"
1253 "--with-fingerprint" "--with-fingerprint")
1254 (unless (eq (epg-context-protocol context
) 'CMS
)
1255 '("--fixed-list-mode"))))
1256 (list-keys-option (if (memq mode
'(t secret
))
1257 "--list-secret-keys"
1258 (if (memq mode
'(nil public
))
1261 (coding-system-for-read 'binary
)
1262 keys string field index
)
1265 (unless (listp name
)
1266 (setq name
(list name
)))
1268 (setq args
(append args
(list list-keys-option
(car name
)))
1270 (setq args
(append args
(list list-keys-option
))))
1272 (apply #'call-process
1273 (epg-context-program context
)
1274 nil
(list t nil
) nil args
)
1275 (goto-char (point-min))
1276 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t
)
1277 (setq keys
(cons (make-vector 15 nil
) keys
)
1278 string
(match-string 0)
1281 (while (and (< field
(length (car keys
)))
1283 (string-match "\\([^:]+\\)?:" string index
)))
1284 (setq index
(match-end 0))
1285 (aset (car keys
) field
(match-string 1 string
))
1286 (setq field
(1+ field
))))
1289 (defun epg--make-sub-key-1 (line)
1292 (cdr (assq (string-to-char (aref line
1)) epg-key-validity-alist
)))
1294 (mapcar (lambda (char) (cdr (assq char epg-key-capability-alist
)))
1296 (member (aref line
0) '("sec" "ssb"))
1297 (string-to-number (aref line
3))
1298 (string-to-number (aref line
2))
1300 (epg--time-from-seconds (aref line
5))
1302 (epg--time-from-seconds (aref line
6)))))
1304 (defun epg-list-keys (context &optional name mode
)
1305 "Return a list of epg-key objects matched with NAME.
1306 If MODE is nil or `public', only public keyring should be searched.
1307 If MODE is t or `secret', only secret keyring should be searched.
1308 Otherwise, only public keyring should be searched and the key
1309 signatures should be included.
1310 NAME is either a string or a list of strings."
1311 (let ((lines (epg--list-keys-1 context name mode
))
1312 keys cert pointer pointer-1 index string
)
1315 ((member (aref (car lines
) 0) '("pub" "sec" "crt" "crs"))
1316 (setq cert
(member (aref (car lines
) 0) '("crt" "crs"))
1317 keys
(cons (epg-make-key
1318 (if (aref (car lines
) 8)
1319 (cdr (assq (string-to-char (aref (car lines
) 8))
1320 epg-key-validity-alist
))))
1322 (push (epg--make-sub-key-1 (car lines
))
1323 (epg-key-sub-key-list (car keys
))))
1324 ((member (aref (car lines
) 0) '("sub" "ssb"))
1325 (push (epg--make-sub-key-1 (car lines
))
1326 (epg-key-sub-key-list (car keys
))))
1327 ((equal (aref (car lines
) 0) "uid")
1328 ;; Decode the UID name as a backslash escaped UTF-8 string,
1329 ;; generated by GnuPG/GpgSM.
1330 (setq string
(copy-sequence (aref (car lines
) 9))
1332 (while (string-match "\"" string index
)
1333 (setq string
(replace-match "\\\"" t t string
)
1334 index
(1+ (match-end 0))))
1336 (setq string
(decode-coding-string
1337 (car (read-from-string (concat "\"" string
"\"")))
1340 (setq string
(aref (car lines
) 9))))
1341 (push (epg-make-user-id
1342 (if (aref (car lines
) 1)
1343 (cdr (assq (string-to-char (aref (car lines
) 1))
1344 epg-key-validity-alist
)))
1347 (epg-dn-from-string string
)
1350 (epg-key-user-id-list (car keys
))))
1351 ((equal (aref (car lines
) 0) "fpr")
1352 (setf (epg-sub-key-fingerprint (car (epg-key-sub-key-list (car keys
))))
1353 (aref (car lines
) 9)))
1354 ((equal (aref (car lines
) 0) "sig")
1356 (epg-make-key-signature
1357 (if (aref (car lines
) 1)
1358 (cdr (assq (string-to-char (aref (car lines
) 1))
1359 epg-key-validity-alist
)))
1360 (string-to-number (aref (car lines
) 3))
1361 (aref (car lines
) 4)
1362 (epg--time-from-seconds (aref (car lines
) 5))
1363 (epg--time-from-seconds (aref (car lines
) 6))
1364 (aref (car lines
) 9)
1365 (string-to-number (aref (car lines
) 10) 16)
1366 (eq (aref (aref (car lines
) 10) 2) ?x
))
1367 (epg-user-id-signature-list
1368 (car (epg-key-user-id-list (car keys
)))))))
1369 (setq lines
(cdr lines
)))
1370 (setq keys
(nreverse keys
)
1373 (cl-callf nreverse
(epg-key-sub-key-list (car pointer
)))
1374 (setq pointer-1
(cl-callf nreverse
(epg-key-user-id-list (car pointer
))))
1376 (cl-callf nreverse
(epg-user-id-signature-list (car pointer-1
)))
1377 (setq pointer-1
(cdr pointer-1
)))
1378 (setq pointer
(cdr pointer
)))
1381 (defun epg--args-from-sig-notations (notations)
1385 (if (and (epg-sig-notation-name notation
)
1386 (not (epg-sig-notation-human-readable notation
)))
1387 (error "Unreadable"))
1388 (if (epg-sig-notation-name notation
)
1389 (list "--sig-notation"
1390 (if (epg-sig-notation-critical notation
)
1391 (concat "!" (epg-sig-notation-name notation
)
1392 "=" (epg-sig-notation-value notation
))
1393 (concat (epg-sig-notation-name notation
)
1394 "=" (epg-sig-notation-value notation
))))
1395 (list "--sig-policy-url"
1396 (if (epg-sig-notation-critical notation
)
1397 (concat "!" (epg-sig-notation-value notation
))
1398 (epg-sig-notation-value notation
)))))
1401 (defun epg-cancel (context)
1402 (if (buffer-live-p (process-buffer (epg-context-process context
)))
1403 (with-current-buffer (process-buffer (epg-context-process context
))
1404 (epg-context-set-result-for
1407 (epg-context-result-for epg-context
'error
)))))
1408 (if (eq (process-status (epg-context-process context
)) 'run
)
1409 (delete-process (epg-context-process context
))))
1411 (defun epg-start-decrypt (context cipher
)
1412 "Initiate a decrypt operation on CIPHER.
1413 CIPHER must be a file data object.
1415 If you use this function, you will need to wait for the completion of
1416 `epg-gpg-program' by using `epg-wait-for-completion' and call
1417 `epg-reset' to clear a temporary output file.
1418 If you are unsure, use synchronous version of this function
1419 `epg-decrypt-file' or `epg-decrypt-string' instead."
1420 (unless (epg-data-file cipher
)
1421 (error "Not a file"))
1422 (setf (epg-context-operation context
) 'decrypt
)
1423 (setf (epg-context-result context
) nil
)
1424 (epg--start context
(list "--decrypt" "--" (epg-data-file cipher
)))
1425 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1426 (unless (eq (epg-context-protocol context
) 'CMS
)
1427 (epg-wait-for-status context
'("BEGIN_DECRYPTION"))))
1429 (defun epg--check-error-for-decrypt (context)
1430 (let ((errors (epg-context-result-for context
'error
)))
1431 (if (epg-context-result-for context
'decryption-failed
)
1433 (list "Decryption failed" (epg-errors-to-string errors
))))
1434 (unless (epg-context-result-for context
'decryption-okay
)
1436 (list "Can't decrypt" (epg-errors-to-string errors
))))))
1438 (defun epg-decrypt-file (context cipher plain
)
1439 "Decrypt a file CIPHER and store the result to a file PLAIN.
1440 If PLAIN is nil, it returns the result as a string."
1443 (setf (epg-context-output-file context
)
1444 (or plain
(make-temp-file "epg-output")))
1445 (epg-start-decrypt context
(epg-make-data-from-file cipher
))
1446 (epg-wait-for-completion context
)
1447 (epg--check-error-for-decrypt context
)
1449 (epg-read-output context
)))
1451 (epg-delete-output-file context
))
1452 (epg-reset context
)))
1454 (defun epg-decrypt-string (context cipher
)
1455 "Decrypt a string CIPHER and return the plain text."
1456 (let ((input-file (make-temp-file "epg-input"))
1457 (coding-system-for-write 'binary
))
1460 (write-region cipher nil input-file nil
'quiet
)
1461 (setf (epg-context-output-file context
)
1462 (make-temp-file "epg-output"))
1463 (epg-start-decrypt context
(epg-make-data-from-file input-file
))
1464 (epg-wait-for-completion context
)
1465 (epg--check-error-for-decrypt context
)
1466 (epg-read-output context
))
1467 (epg-delete-output-file context
)
1468 (if (file-exists-p input-file
)
1469 (delete-file input-file
))
1470 (epg-reset context
))))
1472 (defun epg-start-verify (context signature
&optional signed-text
)
1473 "Initiate a verify operation on SIGNATURE.
1474 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1476 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1477 For a normal or a cleartext signature, SIGNED-TEXT should be nil.
1479 If you use this function, you will need to wait for the completion of
1480 `epg-gpg-program' by using `epg-wait-for-completion' and call
1481 `epg-reset' to clear a temporary output file.
1482 If you are unsure, use synchronous version of this function
1483 `epg-verify-file' or `epg-verify-string' instead."
1484 (setf (epg-context-operation context
) 'verify
)
1485 (setf (epg-context-result context
) nil
)
1487 ;; Detached signature.
1488 (if (epg-data-file signed-text
)
1489 (epg--start context
(list "--verify" "--" (epg-data-file signature
)
1490 (epg-data-file signed-text
)))
1491 (epg--start context
(list "--verify" "--" (epg-data-file signature
)
1493 (if (eq (process-status (epg-context-process context
)) 'run
)
1494 (process-send-string (epg-context-process context
)
1495 (epg-data-string signed-text
)))
1496 (if (eq (process-status (epg-context-process context
)) 'run
)
1497 (process-send-eof (epg-context-process context
))))
1498 ;; Normal (or cleartext) signature.
1499 (if (epg-data-file signature
)
1500 (epg--start context
(if (eq (epg-context-protocol context
) 'CMS
)
1501 (list "--verify" "--" (epg-data-file signature
))
1502 (list "--" (epg-data-file signature
))))
1503 (epg--start context
(if (eq (epg-context-protocol context
) 'CMS
)
1506 (if (eq (process-status (epg-context-process context
)) 'run
)
1507 (process-send-string (epg-context-process context
)
1508 (epg-data-string signature
)))
1509 (if (eq (process-status (epg-context-process context
)) 'run
)
1510 (process-send-eof (epg-context-process context
))))))
1512 (defun epg-verify-file (context signature
&optional signed-text plain
)
1513 "Verify a file SIGNATURE.
1514 SIGNED-TEXT and PLAIN are also a file if they are specified.
1516 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1517 string. For a normal or a cleartext signature, SIGNED-TEXT should be
1518 nil. In the latter case, if PLAIN is specified, the plaintext is
1519 stored into the file after successful verification.
1521 Note that this function does not return verification result as t
1522 or nil, nor signal error on failure. That's a design decision to
1523 handle the case where SIGNATURE has multiple signature.
1525 To check the verification results, use `epg-context-result-for' as follows:
1527 \(epg-context-result-for context \\='verify)
1529 which will return a list of `epg-signature' object."
1532 (setf (epg-context-output-file context
)
1533 (or plain
(make-temp-file "epg-output")))
1535 (epg-start-verify context
1536 (epg-make-data-from-file signature
)
1537 (epg-make-data-from-file signed-text
))
1538 (epg-start-verify context
1539 (epg-make-data-from-file signature
)))
1540 (epg-wait-for-completion context
)
1542 (epg-read-output context
)))
1544 (epg-delete-output-file context
))
1545 (epg-reset context
)))
1547 (defun epg-verify-string (context signature
&optional signed-text
)
1548 "Verify a string SIGNATURE.
1549 SIGNED-TEXT is a string if it is specified.
1551 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1552 string. For a normal or a cleartext signature, SIGNED-TEXT should be
1553 nil. In the latter case, this function returns the plaintext after
1554 successful verification.
1556 Note that this function does not return verification result as t
1557 or nil, nor signal error on failure. That's a design decision to
1558 handle the case where SIGNATURE has multiple signature.
1560 To check the verification results, use `epg-context-result-for' as follows:
1562 \(epg-context-result-for context \\='verify)
1564 which will return a list of `epg-signature' object."
1565 (let ((coding-system-for-write 'binary
)
1569 (setf (epg-context-output-file context
)
1570 (make-temp-file "epg-output"))
1573 (setq input-file
(make-temp-file "epg-signature"))
1574 (write-region signature nil input-file nil
'quiet
)
1575 (epg-start-verify context
1576 (epg-make-data-from-file input-file
)
1577 (epg-make-data-from-string signed-text
)))
1578 (epg-start-verify context
(epg-make-data-from-string signature
)))
1579 (epg-wait-for-completion context
)
1580 (epg-read-output context
))
1581 (epg-delete-output-file context
)
1583 (file-exists-p input-file
))
1584 (delete-file input-file
))
1585 (epg-reset context
))))
1587 (defun epg-start-sign (context plain
&optional mode
)
1588 "Initiate a sign operation on PLAIN.
1589 PLAIN is a data object.
1591 If optional 3rd argument MODE is t or `detached', it makes a detached signature.
1592 If it is nil or `normal', it makes a normal signature.
1593 Otherwise, it makes a cleartext signature.
1595 If you use this function, you will need to wait for the completion of
1596 `epg-gpg-program' by using `epg-wait-for-completion' and call
1597 `epg-reset' to clear a temporary output file.
1598 If you are unsure, use synchronous version of this function
1599 `epg-sign-file' or `epg-sign-string' instead."
1600 (setf (epg-context-operation context
) 'sign
)
1601 (setf (epg-context-result context
) nil
)
1602 (unless (memq mode
'(t detached nil normal
)) ;i.e. cleartext
1603 (setf (epg-context-armor context
) nil
)
1604 (setf (epg-context-textmode context
) nil
))
1606 (append (list (if (memq mode
'(t detached
))
1608 (if (memq mode
'(nil normal
))
1616 (car (epg-key-sub-key-list signer
)))))
1617 (epg-context-signers context
)))
1618 (epg--args-from-sig-notations
1619 (epg-context-sig-notations context
))
1620 (if (epg-data-file plain
)
1621 (list "--" (epg-data-file plain
)))))
1622 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1623 (unless (eq (epg-context-protocol context
) 'CMS
)
1624 (epg-wait-for-status context
'("BEGIN_SIGNING")))
1625 (when (epg-data-string plain
)
1626 (if (eq (process-status (epg-context-process context
)) 'run
)
1627 (process-send-string (epg-context-process context
)
1628 (epg-data-string plain
)))
1629 (if (eq (process-status (epg-context-process context
)) 'run
)
1630 (process-send-eof (epg-context-process context
)))))
1632 (defun epg-sign-file (context plain signature
&optional mode
)
1633 "Sign a file PLAIN and store the result to a file SIGNATURE.
1634 If SIGNATURE is nil, it returns the result as a string.
1635 If optional 3rd argument MODE is t or `detached', it makes a detached signature.
1636 If it is nil or `normal', it makes a normal signature.
1637 Otherwise, it makes a cleartext signature."
1640 (setf (epg-context-output-file context
)
1641 (or signature
(make-temp-file "epg-output")))
1642 (epg-start-sign context
(epg-make-data-from-file plain
) mode
)
1643 (epg-wait-for-completion context
)
1644 (unless (epg-context-result-for context
'sign
)
1645 (let ((errors (epg-context-result-for context
'error
)))
1647 (list "Sign failed" (epg-errors-to-string errors
)))))
1649 (epg-read-output context
)))
1651 (epg-delete-output-file context
))
1652 (epg-reset context
)))
1654 (defun epg-sign-string (context plain
&optional mode
)
1655 "Sign a string PLAIN and return the output as string.
1656 If optional 3rd argument MODE is t or `detached', it makes a detached signature.
1657 If it is nil or `normal', it makes a normal signature.
1658 Otherwise, it makes a cleartext signature."
1660 (unless (eq (epg-context-protocol context
) 'CMS
)
1661 (make-temp-file "epg-input")))
1662 (coding-system-for-write 'binary
))
1665 (setf (epg-context-output-file context
)
1666 (make-temp-file "epg-output"))
1668 (write-region plain nil input-file nil
'quiet
))
1669 (epg-start-sign context
1671 (epg-make-data-from-file input-file
)
1672 (epg-make-data-from-string plain
))
1674 (epg-wait-for-completion context
)
1675 (unless (epg-context-result-for context
'sign
)
1676 (if (epg-context-result-for context
'error
)
1677 (let ((errors (epg-context-result-for context
'error
)))
1679 (list "Sign failed" (epg-errors-to-string errors
))))))
1680 (epg-read-output context
))
1681 (epg-delete-output-file context
)
1683 (delete-file input-file
))
1684 (epg-reset context
))))
1686 (defun epg-start-encrypt (context plain recipients
1687 &optional sign always-trust
)
1688 "Initiate an encrypt operation on PLAIN.
1689 PLAIN is a data object.
1690 If RECIPIENTS is nil, it performs symmetric encryption.
1692 If you use this function, you will need to wait for the completion of
1693 `epg-gpg-program' by using `epg-wait-for-completion' and call
1694 `epg-reset' to clear a temporary output file.
1695 If you are unsure, use synchronous version of this function
1696 `epg-encrypt-file' or `epg-encrypt-string' instead."
1697 (setf (epg-context-operation context
) 'encrypt
)
1698 (setf (epg-context-result context
) nil
)
1700 (append (if always-trust
'("--always-trust"))
1701 (if recipients
'("--encrypt") '("--symmetric"))
1702 (if sign
'("--sign"))
1709 (car (epg-key-sub-key-list
1711 (epg-context-signers context
))))
1713 (epg--args-from-sig-notations
1714 (epg-context-sig-notations context
)))
1720 (car (epg-key-sub-key-list recipient
)))))
1722 (if (epg-data-file plain
)
1723 (list "--" (epg-data-file plain
)))))
1724 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1725 (unless (eq (epg-context-protocol context
) 'CMS
)
1726 (epg-wait-for-status context
1727 (if sign
'("BEGIN_SIGNING") '("BEGIN_ENCRYPTION"))))
1728 (when (epg-data-string plain
)
1729 (if (eq (process-status (epg-context-process context
)) 'run
)
1730 (process-send-string (epg-context-process context
)
1731 (epg-data-string plain
)))
1732 (if (eq (process-status (epg-context-process context
)) 'run
)
1733 (process-send-eof (epg-context-process context
)))))
1735 (defun epg-encrypt-file (context plain recipients
1736 cipher
&optional sign always-trust
)
1737 "Encrypt a file PLAIN and store the result to a file CIPHER.
1738 If CIPHER is nil, it returns the result as a string.
1739 If RECIPIENTS is nil, it performs symmetric encryption."
1742 (setf (epg-context-output-file context
)
1743 (or cipher
(make-temp-file "epg-output")))
1744 (epg-start-encrypt context
(epg-make-data-from-file plain
)
1745 recipients sign always-trust
)
1746 (epg-wait-for-completion context
)
1747 (let ((errors (epg-context-result-for context
'error
)))
1749 (not (epg-context-result-for context
'sign
)))
1751 (list "Sign failed" (epg-errors-to-string errors
))))
1754 (list "Encrypt failed" (epg-errors-to-string errors
)))))
1756 (epg-read-output context
)))
1758 (epg-delete-output-file context
))
1759 (epg-reset context
)))
1761 (defun epg-encrypt-string (context plain recipients
1762 &optional sign always-trust
)
1763 "Encrypt a string PLAIN.
1764 If RECIPIENTS is nil, it performs symmetric encryption."
1766 (unless (or (not sign
)
1767 (eq (epg-context-protocol context
) 'CMS
))
1768 (make-temp-file "epg-input")))
1769 (coding-system-for-write 'binary
))
1772 (setf (epg-context-output-file context
)
1773 (make-temp-file "epg-output"))
1775 (write-region plain nil input-file nil
'quiet
))
1776 (epg-start-encrypt context
1778 (epg-make-data-from-file input-file
)
1779 (epg-make-data-from-string plain
))
1780 recipients sign always-trust
)
1781 (epg-wait-for-completion context
)
1782 (let ((errors (epg-context-result-for context
'error
)))
1784 (not (epg-context-result-for context
'sign
)))
1786 (list "Sign failed" (epg-errors-to-string errors
))))
1789 (list "Encrypt failed" (epg-errors-to-string errors
)))))
1790 (epg-read-output context
))
1791 (epg-delete-output-file context
)
1793 (delete-file input-file
))
1794 (epg-reset context
))))
1796 (defun epg-start-export-keys (context keys
)
1797 "Initiate an export keys operation.
1799 If you use this function, you will need to wait for the completion of
1800 `epg-gpg-program' by using `epg-wait-for-completion' and call
1801 `epg-reset' to clear a temporary output file.
1802 If you are unsure, use synchronous version of this function
1803 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1804 (setf (epg-context-operation context
) 'export-keys
)
1805 (setf (epg-context-result context
) nil
)
1806 (epg--start context
(cons "--export"
1810 (car (epg-key-sub-key-list key
))))
1813 (defun epg-export-keys-to-file (context keys file
)
1814 "Extract public KEYS."
1817 (setf (epg-context-output-file context
)
1818 (or file
(make-temp-file "epg-output")))
1819 (epg-start-export-keys context keys
)
1820 (epg-wait-for-completion context
)
1821 (let ((errors (epg-context-result-for context
'error
)))
1824 (list "Export keys failed"
1825 (epg-errors-to-string errors
)))))
1827 (epg-read-output context
)))
1829 (epg-delete-output-file context
))
1830 (epg-reset context
)))
1832 (defun epg-export-keys-to-string (context keys
)
1833 "Extract public KEYS and return them as a string."
1834 (epg-export-keys-to-file context keys nil
))
1836 (defun epg-start-import-keys (context keys
)
1837 "Initiate an import keys operation.
1838 KEYS is a data object.
1840 If you use this function, you will need to wait for the completion of
1841 `epg-gpg-program' by using `epg-wait-for-completion' and call
1842 `epg-reset' to clear a temporary output file.
1843 If you are unsure, use synchronous version of this function
1844 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1845 (setf (epg-context-operation context
) 'import-keys
)
1846 (setf (epg-context-result context
) nil
)
1847 (epg--start context
(if (epg-data-file keys
)
1848 (list "--import" "--" (epg-data-file keys
))
1850 (when (epg-data-string keys
)
1851 (if (eq (process-status (epg-context-process context
)) 'run
)
1852 (process-send-string (epg-context-process context
)
1853 (epg-data-string keys
)))
1854 (if (eq (process-status (epg-context-process context
)) 'run
)
1855 (process-send-eof (epg-context-process context
)))))
1857 (defun epg--import-keys-1 (context keys
)
1860 (epg-start-import-keys context keys
)
1861 (epg-wait-for-completion context
)
1862 (let ((errors (epg-context-result-for context
'error
)))
1865 (list "Import keys failed"
1866 (epg-errors-to-string errors
))))))
1867 (epg-reset context
)))
1869 (defun epg-import-keys-from-file (context keys
)
1870 "Add keys from a file KEYS."
1871 (epg--import-keys-1 context
(epg-make-data-from-file keys
)))
1873 (defun epg-import-keys-from-string (context keys
)
1874 "Add keys from a string KEYS."
1875 (epg--import-keys-1 context
(epg-make-data-from-string keys
)))
1877 (defun epg-start-receive-keys (context key-id-list
)
1878 "Initiate a receive key operation.
1879 KEY-ID-LIST is a list of key IDs.
1881 If you use this function, you will need to wait for the completion of
1882 `epg-gpg-program' by using `epg-wait-for-completion' and call
1883 `epg-reset' to clear a temporary output file.
1884 If you are unsure, use synchronous version of this function
1885 `epg-receive-keys' instead."
1886 (setf (epg-context-operation context
) 'receive-keys
)
1887 (setf (epg-context-result context
) nil
)
1888 (epg--start context
(cons "--recv-keys" key-id-list
)))
1890 (defun epg-receive-keys (context keys
)
1891 "Add keys from server.
1892 KEYS is a list of key IDs"
1895 (epg-start-receive-keys context keys
)
1896 (epg-wait-for-completion context
)
1897 (let ((errors (epg-context-result-for context
'error
)))
1900 (list "Receive keys failed"
1901 (epg-errors-to-string errors
))))))
1902 (epg-reset context
)))
1904 (defalias 'epg-import-keys-from-server
'epg-receive-keys
)
1906 (defun epg-start-delete-keys (context keys
&optional allow-secret
)
1907 "Initiate a delete keys operation.
1909 If you use this function, you will need to wait for the completion of
1910 `epg-gpg-program' by using `epg-wait-for-completion' and call
1911 `epg-reset' to clear a temporary output file.
1912 If you are unsure, use synchronous version of this function
1913 `epg-delete-keys' instead."
1914 (setf (epg-context-operation context
) 'delete-keys
)
1915 (setf (epg-context-result context
) nil
)
1916 (epg--start context
(cons (if allow-secret
1917 "--delete-secret-key"
1922 (car (epg-key-sub-key-list key
))))
1925 (defun epg-delete-keys (context keys
&optional allow-secret
)
1926 "Delete KEYS from the key ring."
1929 (epg-start-delete-keys context keys allow-secret
)
1930 (epg-wait-for-completion context
)
1931 (let ((errors (epg-context-result-for context
'error
)))
1934 (list "Delete keys failed"
1935 (epg-errors-to-string errors
))))))
1936 (epg-reset context
)))
1938 (defun epg-start-sign-keys (context keys
&optional local
)
1939 "Initiate a sign keys operation.
1941 If you use this function, you will need to wait for the completion of
1942 `epg-gpg-program' by using `epg-wait-for-completion' and call
1943 `epg-reset' to clear a temporary output file.
1944 If you are unsure, use synchronous version of this function
1945 `epg-sign-keys' instead."
1946 (declare (obsolete nil
"23.1"))
1947 (setf (epg-context-operation context
) 'sign-keys
)
1948 (setf (epg-context-result context
) nil
)
1949 (epg--start context
(cons (if local
1955 (car (epg-key-sub-key-list key
))))
1958 (defun epg-sign-keys (context keys
&optional local
)
1959 "Sign KEYS from the key ring."
1960 (declare (obsolete nil
"23.1"))
1963 (epg-start-sign-keys context keys local
)
1964 (epg-wait-for-completion context
)
1965 (let ((errors (epg-context-result-for context
'error
)))
1968 (list "Sign keys failed"
1969 (epg-errors-to-string errors
))))))
1970 (epg-reset context
)))
1972 (defun epg-start-generate-key (context parameters
)
1973 "Initiate a key generation.
1974 PARAMETERS is a string which specifies parameters of the generated key.
1975 See Info node `(gnupg) Unattended GPG key generation' in the
1976 GnuPG manual for the format.
1978 If you use this function, you will need to wait for the completion of
1979 `epg-gpg-program' by using `epg-wait-for-completion' and call
1980 `epg-reset' to clear a temporary output file.
1981 If you are unsure, use synchronous version of this function
1982 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
1983 (setf (epg-context-operation context
) 'generate-key
)
1984 (setf (epg-context-result context
) nil
)
1985 (if (epg-data-file parameters
)
1986 (epg--start context
(list "--batch" "--gen-key" "--"
1987 (epg-data-file parameters
)))
1988 (epg--start context
'("--batch" "--gen-key"))
1989 (if (eq (process-status (epg-context-process context
)) 'run
)
1990 (process-send-string (epg-context-process context
)
1991 (epg-data-string parameters
)))
1992 (if (eq (process-status (epg-context-process context
)) 'run
)
1993 (process-send-eof (epg-context-process context
)))))
1995 (defun epg-generate-key-from-file (context parameters
)
1996 "Generate a new key pair.
1997 PARAMETERS is a file which tells how to create the key."
2000 (epg-start-generate-key context
(epg-make-data-from-file parameters
))
2001 (epg-wait-for-completion context
)
2002 (let ((errors (epg-context-result-for context
'error
)))
2005 (list "Generate key failed"
2006 (epg-errors-to-string errors
))))))
2007 (epg-reset context
)))
2009 (defun epg-generate-key-from-string (context parameters
)
2010 "Generate a new key pair.
2011 PARAMETERS is a string which tells how to create the key."
2014 (epg-start-generate-key context
(epg-make-data-from-string parameters
))
2015 (epg-wait-for-completion context
)
2016 (let ((errors (epg-context-result-for context
'error
)))
2019 (list "Generate key failed"
2020 (epg-errors-to-string errors
))))))
2021 (epg-reset context
)))
2023 (defun epg-start-edit-key (context key edit-callback handback
)
2024 "Initiate an edit operation on KEY.
2026 EDIT-CALLBACK is called from process filter and takes 3
2027 arguments: the context, a status, an argument string, and the
2030 If you use this function, you will need to wait for the completion of
2031 `epg-gpg-program' by using `epg-wait-for-completion' and call
2032 `epg-reset' to clear a temporary output file.
2033 If you are unsure, use synchronous version of this function
2034 `epg-edit-key' instead."
2035 (setf (epg-context-operation context
) 'edit-key
)
2036 (setf (epg-context-result context
) nil
)
2037 (setf (epg-context-edit-callback context
) (cons edit-callback handback
))
2038 (epg--start context
(list "--edit-key"
2040 (car (epg-key-sub-key-list key
))))))
2042 (defun epg-edit-key (context key edit-callback handback
)
2043 "Edit KEY in the keyring."
2046 (epg-start-edit-key context key edit-callback handback
)
2047 (epg-wait-for-completion context
)
2048 (let ((errors (epg-context-result-for context
'error
)))
2051 (list "Edit key failed"
2052 (epg-errors-to-string errors
))))))
2053 (epg-reset context
)))
2055 (defun epg--decode-percent-escape (string)
2057 (while (string-match "%\\(\\(%\\)\\|\\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2059 (if (match-beginning 2)
2060 (setq string
(replace-match "%" t t string
)
2061 index
(1- (match-end 0)))
2062 (setq string
(replace-match
2063 (string (string-to-number (match-string 3 string
) 16))
2065 index
(- (match-end 0) 2))))
2068 (defun epg--decode-hexstring (string)
2070 (while (eq index
(string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index
))
2071 (setq string
(replace-match (string (string-to-number
2072 (match-string 0 string
) 16))
2074 index
(1- (match-end 0))))
2077 (defun epg--decode-quotedstring (string)
2079 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2080 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2082 (if (match-beginning 2)
2083 (setq string
(replace-match "\\2" t nil string
)
2084 index
(1- (match-end 0)))
2085 (if (match-beginning 3)
2086 (setq string
(replace-match (string (string-to-number
2087 (match-string 0 string
) 16))
2089 index
(- (match-end 0) 2)))))
2092 (defun epg-dn-from-string (string)
2093 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2094 The return value is an alist mapping from types to values."
2096 (length (length string
))
2097 alist type value group
)
2098 (while (< index length
)
2099 (if (eq index
(string-match "[ \t\n\r]*" string index
))
2100 (setq index
(match-end 0)))
2101 (if (eq index
(string-match
2102 "\\([0-9]+\\(\\.[0-9]+\\)*\\)[ \t\n\r]*=[ \t\n\r]*"
2104 (setq type
(match-string 1 string
)
2105 index
(match-end 0))
2106 (if (eq index
(string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2108 (setq type
(match-string 1 string
)
2109 index
(match-end 0))))
2111 (error "Invalid type"))
2112 (if (eq index
(string-match
2113 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2115 (setq index
(match-end 0)
2116 value
(epg--decode-quotedstring (match-string 0 string
)))
2117 (if (eq index
(string-match "#\\([0-9A-Fa-f]+\\)" string index
))
2118 (setq index
(match-end 0)
2119 value
(epg--decode-hexstring (match-string 1 string
)))
2120 (if (eq index
(string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2122 (setq index
(match-end 0)
2123 value
(epg--decode-quotedstring
2124 (match-string 0 string
))))))
2126 (if (stringp (car (car alist
)))
2127 (setcar alist
(list (cons type value
) (car alist
)))
2128 (setcar alist
(cons (cons type value
) (car alist
))))
2129 (if (consp (car (car alist
)))
2130 (setcar alist
(nreverse (car alist
))))
2131 (setq alist
(cons (cons type value
) alist
)
2134 (if (eq index
(string-match "[ \t\n\r]*\\([,;+]\\)" string index
))
2135 (setq index
(match-end 0)
2136 group
(eq (aref string
(match-beginning 1)) ?
+))))
2139 (defun epg-decode-dn (alist)
2140 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2141 Type names are resolved using `epg-dn-type-alist'."
2144 (if (stringp (car rdn
))
2145 (let ((entry (assoc (car rdn
) epg-dn-type-alist
)))
2147 (format "%s=%s" (cdr entry
) (cdr rdn
))
2148 (format "%s=%s" (car rdn
) (cdr rdn
))))
2149 (concat "(" (epg-decode-dn rdn
) ")")))
2155 ;;; epg.el ends here