1 ;;; epg.el --- the EasyPG Library
2 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
3 ;; 2005, 2006 Free Software Foundation, Inc.
4 ;; Copyright (C) 2006 Daiki Ueno
6 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Keywords: PGP, GnuPG
9 ;; This file is part of EasyPG.
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
31 (defcustom epg-gpg-program "gpg"
32 "The `gpg' executable."
36 (defcustom epg-gpgsm-program "gpgsm"
37 "The `gpgsm' executable."
41 (defconst epg-version-number "0.0.0")
43 (defvar epg-user-id nil
44 "GnuPG ID of your default identity.")
46 (defvar epg-user-id-alist nil
47 "An alist mapping from key ID to user ID.")
49 (defvar epg-read-point nil)
50 (defvar epg-pending-status-list nil)
51 (defvar epg-key-id nil)
52 (defvar epg-context nil)
53 (defvar epg-debug nil)
54 (defvar epg-debug-buffer nil)
56 ;; from gnupg/include/cipher.h
57 (defconst epg-cipher-algorithm-alist
69 ;; from gnupg/include/cipher.h
70 (defconst epg-pubkey-algorithm-alist
78 ;; from gnupg/include/cipher.h
79 (defconst epg-digest-algorithm-alist
87 ;; from gnupg/include/cipher.h
88 (defconst epg-compress-algorithm-alist
94 (defconst epg-invalid-recipients-alist
95 '((0 . "No specific reason given")
97 (2 . "Ambigious specification")
98 (3 . "Wrong key usage")
103 (8 . "Policy mismatch")
104 (9 . "Not a secret key")
105 (10 . "Key not trusted")))
107 (defconst epg-delete-problem-alist
108 '((1 . "No such key")
109 (2 . "Must delete secret key first")
110 (3 . "Ambigious specification")))
112 (defvar epg-key-validity-alist
125 (defvar epg-key-capablity-alist
129 (?a . authentication)))
131 (defvar epg-dn-type-alist
132 '(("1.2.840.113549.1.9.1" . "EMail")
136 ("0.2.262.1.10.7.20" . "NameDistinguisher")
137 ("2.5.4.16" . "ADDR")
140 ("2.5.4.17" . "PostalCode")
141 ("2.5.4.65" . "Pseudo")
142 ("2.5.4.5" . "SerialNumber")))
144 (defvar epg-prompt-alist nil)
146 (defun epg-make-data-from-file (file)
147 "Make a data object from FILE."
150 (defun epg-make-data-from-string (string)
151 "Make a data object from STRING."
154 (defun epg-data-file (data)
155 "Return the file of DATA."
158 (defun epg-data-string (data)
159 "Return the string of DATA."
162 (defun epg-make-context (&optional protocol armor textmode include-certs
163 cipher-algorithm digest-algorithm
165 "Return a context object."
166 (vector protocol armor textmode include-certs
167 cipher-algorithm digest-algorithm compress-algorithm
168 #'epg-passphrase-callback-function
169 #'epg-progress-callback-function
172 (defun epg-context-protocol (context)
173 "Return the protocol used within CONTEXT."
176 (defun epg-context-armor (context)
177 "Return t if the output shouled be ASCII armored in CONTEXT."
180 (defun epg-context-textmode (context)
181 "Return t if canonical text mode should be used in CONTEXT."
184 (defun epg-context-include-certs (context)
185 "Return how many certificates should be included in an S/MIME signed
189 (defun epg-context-cipher-algorithm (context)
190 "Return the cipher algorithm in CONTEXT."
193 (defun epg-context-digest-algorithm (context)
194 "Return the digest algorithm in CONTEXT."
197 (defun epg-context-compress-algorithm (context)
198 "Return the compress algorithm in CONTEXT."
201 (defun epg-context-passphrase-callback (context)
202 "Return the function used to query passphrase."
205 (defun epg-context-progress-callback (context)
206 "Return the function which handles progress update."
209 (defun epg-context-signers (context)
210 "Return the list of key-id for singning."
213 (defun epg-context-process (context)
214 "Return the process object of `epg-gpg-program'.
215 This function is for internal use only."
218 (defun epg-context-output-file (context)
219 "Return the output file of `epg-gpg-program'.
220 This function is for internal use only."
223 (defun epg-context-result (context)
224 "Return the result of the previous cryptographic operation."
227 (defun epg-context-set-protocol (context protocol)
228 "Set the protocol used within CONTEXT."
229 (aset context 0 protocol))
231 (defun epg-context-set-armor (context armor)
232 "Specify if the output shouled be ASCII armored in CONTEXT."
233 (aset context 1 armor))
235 (defun epg-context-set-textmode (context textmode)
236 "Specify if canonical text mode should be used in CONTEXT."
237 (aset context 2 textmode))
239 (defun epg-context-set-include-certs (context include-certs)
240 "Set how many certificates should be included in an S/MIME signed message."
241 (aset context 3 include-certs))
243 (defun epg-context-set-cipher-algorithm (context cipher-algorithm)
244 "Set the cipher algorithm in CONTEXT."
245 (aset context 4 cipher-algorithm))
247 (defun epg-context-set-digest-algorithm (context digest-algorithm)
248 "Set the digest algorithm in CONTEXT."
249 (aset context 5 digest-algorithm))
251 (defun epg-context-set-compress-algorithm (context compress-algorithm)
252 "Set the compress algorithm in CONTEXT."
253 (aset context 6 compress-algorithm))
255 (defun epg-context-set-passphrase-callback (context
257 "Set the function used to query passphrase."
258 (aset context 7 passphrase-callback))
260 (defun epg-context-set-progress-callback (context progress-callback)
261 "Set the function which handles progress update."
262 (aset context 8 progress-callback))
264 (defun epg-context-set-signers (context signers)
265 "Set the list of key-id for singning."
266 (aset context 9 signers))
268 (defun epg-context-set-process (context process)
269 "Set the process object of `epg-gpg-program'.
270 This function is for internal use only."
271 (aset context 10 process))
273 (defun epg-context-set-output-file (context output-file)
274 "Set the output file of `epg-gpg-program'.
275 This function is for internal use only."
276 (aset context 11 output-file))
278 (defun epg-context-set-result (context result)
279 "Set the result of the previous cryptographic operation."
280 (aset context 12 result))
282 (defun epg-make-signature (status key-id user-id)
283 "Return a signature object."
284 (vector status key-id user-id nil nil))
286 (defun epg-signature-status (signature)
287 "Return the status code of SIGNATURE."
290 (defun epg-signature-key-id (signature)
291 "Return the key-id of SIGNATURE."
294 (defun epg-signature-user-id (signature)
295 "Return the user-id of SIGNATURE."
298 (defun epg-signature-validity (signature)
299 "Return the validity of SIGNATURE."
302 (defun epg-signature-fingerprint (signature)
303 "Return the fingerprint of SIGNATURE."
306 (defun epg-signature-set-status (signature status)
307 "Set the status code of SIGNATURE."
308 (aset signature 0 status))
310 (defun epg-signature-set-key-id (signature key-id)
311 "Set the key-id of SIGNATURE."
312 (aset signature 1 key-id))
314 (defun epg-signature-set-user-id (signature user-id)
315 "Set the user-id of SIGNATURE."
316 (aset signature 2 user-id))
318 (defun epg-signature-set-validity (signature validity)
319 "Set the validity of SIGNATURE."
320 (aset signature 3 validity))
322 (defun epg-signature-set-fingerprint (signature fingerprint)
323 "Set the fingerprint of SIGNATURE."
324 (aset signature 4 fingerprint))
326 (defun epg-make-key (owner-trust)
327 "Return a key object."
328 (vector owner-trust nil nil))
330 (defun epg-key-owner-trust (key)
331 "Return the owner trust of KEY."
334 (defun epg-key-sub-key-list (key)
335 "Return the sub key list of KEY."
338 (defun epg-key-user-id-list (key)
339 "Return the user ID list of KEY."
342 (defun epg-key-set-sub-key-list (key sub-key-list)
343 "Set the sub key list of KEY."
344 (aset key 1 sub-key-list))
346 (defun epg-key-set-user-id-list (key user-id-list)
347 "Set the user ID list of KEY."
348 (aset key 2 user-id-list))
350 (defun epg-make-sub-key (validity capability secret algorithm length id
351 creation-time expiration-time)
352 "Return a sub key object."
353 (vector validity capability secret algorithm length id creation-time
354 expiration-time nil))
356 (defun epg-sub-key-validity (sub-key)
357 "Return the validity of SUB-KEY."
360 (defun epg-sub-key-capability (sub-key)
361 "Return the capability of SUB-KEY."
364 (defun epg-sub-key-secret (sub-key)
365 "Return non-nil if SUB-KEY is a secret key."
368 (defun epg-sub-key-algorithm (sub-key)
369 "Return the algorithm of SUB-KEY."
372 (defun epg-sub-key-length (sub-key)
373 "Return the length of SUB-KEY."
376 (defun epg-sub-key-id (sub-key)
377 "Return the ID of SUB-KEY."
380 (defun epg-sub-key-creation-time (sub-key)
381 "Return the creation time of SUB-KEY."
384 (defun epg-sub-key-expiration-time (sub-key)
385 "Return the expiration time of SUB-KEY."
388 (defun epg-sub-key-fingerprint (sub-key)
389 "Return the fingerprint of SUB-KEY."
392 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
393 "Set the fingerprint of SUB-KEY.
394 This function is for internal use only."
395 (aset sub-key 8 fingerprint))
397 (defun epg-make-user-id (validity name)
398 "Return a user ID object."
399 (vector validity name nil))
401 (defun epg-user-id-validity (user-id)
402 "Return the validity of USER-ID."
405 (defun epg-user-id-name (user-id)
406 "Return the name of USER-ID."
409 (defun epg-user-id-signature-list (user-id)
410 "Return the signature list of USER-ID."
413 (defun epg-user-id-set-signature-list (user-id signature-list)
414 "Set the signature list of USER-ID."
415 (aset user-id 2 signature-list))
417 (defun epg-context-result-for (context name)
418 (cdr (assq name (epg-context-result context))))
420 (defun epg-context-set-result-for (context name value)
421 (let* ((result (epg-context-result context))
422 (entry (assq name result)))
425 (epg-context-set-result context (cons (cons name value) result)))))
427 (defun epg-signature-to-string (signature)
428 (format "%s signature from %s %s%s"
429 (capitalize (symbol-name (epg-signature-status signature)))
430 (epg-signature-key-id signature)
431 (epg-signature-user-id signature)
432 (if (epg-signature-validity signature)
433 (format " (trust %s)"
434 (epg-signature-validity signature))
437 (defun epg-verify-result-to-string (verify-result)
438 (mapconcat #'epg-signature-to-string verify-result "\n"))
440 (defun epg-start (context args)
441 "Start `epg-gpg-program' in a subprocess with given ARGS."
442 (let* ((args (append (list "--no-tty"
445 (unless (eq (epg-context-protocol context) 'CMS)
446 (list "--command-fd" "0"))
447 (if (epg-context-armor context) '("--armor"))
448 (if (epg-context-textmode context) '("--textmode"))
449 (if (epg-context-output-file context)
450 (list "--output" (epg-context-output-file context)))
452 (coding-system-for-write 'binary)
453 process-connection-type
454 (orig-mode (default-file-modes))
455 (buffer (generate-new-buffer " *epg*"))
459 (unless epg-debug-buffer
460 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
461 (set-buffer epg-debug-buffer)
462 (goto-char (point-max))
463 (insert (format "%s %s\n"
464 (if (eq (epg-context-protocol context) 'CMS)
467 (mapconcat #'identity args " ")))))
468 (with-current-buffer buffer
469 (make-local-variable 'epg-read-point)
470 (setq epg-read-point (point-min))
471 (make-local-variable 'epg-pending-status-list)
472 (setq epg-pending-status-list nil)
473 (make-local-variable 'epg-key-id)
474 (setq epg-key-id nil)
475 (make-local-variable 'epg-context)
476 (setq epg-context context))
479 (set-default-file-modes 448)
481 (apply #'start-process "epg" buffer
482 (if (eq (epg-context-protocol context) 'CMS)
486 (set-default-file-modes orig-mode))
487 (set-process-filter process #'epg-process-filter)
488 (set-process-sentinel process #'epg-process-sentinel)
489 (epg-context-set-process context process)))
491 (defun epg-process-filter (process input)
494 (unless epg-debug-buffer
495 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
496 (set-buffer epg-debug-buffer)
497 (goto-char (point-max))
499 (if (buffer-live-p (process-buffer process))
501 (set-buffer (process-buffer process))
502 (goto-char (point-max))
504 (goto-char epg-read-point)
506 (while (looking-at ".*\n") ;the input line finished
508 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
509 (let* ((status (match-string 1))
510 (string (match-string 2))
511 (symbol (intern-soft (concat "epg-status-" status))))
512 (if (member status epg-pending-status-list)
513 (setq epg-pending-status-list nil))
516 (funcall symbol process string)))))
518 (setq epg-read-point (point)))))
520 (defun epg-process-sentinel (process status)
521 (if (and (buffer-live-p (process-buffer process))
522 (not (equal status "finished\n")))
524 (set-buffer (process-buffer process))
525 ;; gpg process exited abnormally, but we have not received an
526 ;; error response from it. Set it here.
527 (unless (epg-context-result-for epg-context 'error)
528 (if (string-match "\\`exited abnormally with code \\(.*\\)\n" status)
529 (epg-context-set-result-for
531 (list (cons 'exit (string-to-number (match-string 1 status)))))
532 (epg-context-set-result-for epg-context 'error
533 (list (cons 'signal status))))))))
535 (defun epg-read-output (context)
537 (if (fboundp 'set-buffer-multibyte)
538 (set-buffer-multibyte nil))
539 (if (file-exists-p (epg-context-output-file context))
540 (let ((coding-system-for-read (if (epg-context-textmode context)
543 (insert-file-contents (epg-context-output-file context))
546 (defun epg-wait-for-status (context status-list)
547 (with-current-buffer (process-buffer (epg-context-process context))
548 (setq epg-pending-status-list status-list)
549 (while (and (eq (process-status (epg-context-process context)) 'run)
550 epg-pending-status-list)
551 (accept-process-output (epg-context-process context) 1))))
553 (defun epg-wait-for-completion (context)
554 (while (eq (process-status (epg-context-process context)) 'run)
555 ;; We can't use accept-process-output instead of sit-for here
556 ;; because it may cause an interrupt during the sentinel execution.
559 (defun epg-flush (context)
560 (if (eq (process-status (epg-context-process context)) 'run)
561 (process-send-eof (epg-context-process context))))
563 (defun epg-reset (context)
564 (if (and (epg-context-process context)
565 (buffer-live-p (process-buffer (epg-context-process context))))
566 (kill-buffer (process-buffer (epg-context-process context))))
567 (epg-context-set-process context nil))
569 (defun epg-delete-output-file (context)
570 (if (and (epg-context-output-file context)
571 (file-exists-p (epg-context-output-file context)))
572 (delete-file (epg-context-output-file context))))
574 (defun epg-status-USERID_HINT (process string)
575 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
576 (let* ((key-id (match-string 1 string))
577 (user-id (match-string 2 string))
578 (entry (assoc key-id epg-user-id-alist)))
580 (setcdr entry user-id)
581 (setq epg-user-id-alist (cons (cons key-id user-id)
582 epg-user-id-alist))))))
584 (defun epg-status-NEED_PASSPHRASE (process string)
585 (if (string-match "\\`\\([^ ]+\\)" string)
586 (setq epg-key-id (match-string 1 string))))
588 (defun epg-status-NEED_PASSPHRASE_SYM (process string)
589 (setq epg-key-id 'SYM))
591 (defun epg-status-NEED_PASSPHRASE_PIN (process string)
592 (setq epg-key-id 'PIN))
594 (defun epg-status-GET_HIDDEN (process string)
596 (string-match "\\`passphrase\\." string))
599 passphrase-with-new-line)
605 (if (consp (epg-context-passphrase-callback
607 (car (epg-context-passphrase-callback
609 (epg-context-passphrase-callback epg-context))
612 (if (consp (epg-context-passphrase-callback
614 (cdr (epg-context-passphrase-callback
617 (setq passphrase-with-new-line (concat passphrase "\n"))
618 (fillarray passphrase 0)
619 (setq passphrase nil)
620 (process-send-string process passphrase-with-new-line)))
622 (epg-context-set-result-for
625 (epg-context-result-for epg-context 'error)))
626 (delete-process process)))
628 (fillarray passphrase 0))
629 (if passphrase-with-new-line
630 (fillarray passphrase-with-new-line 0))))))
632 (defun epg-status-GET_BOOL (process string)
633 (let ((entry (assoc string epg-prompt-alist))
636 (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
637 (process-send-string process "y\n")
638 (process-send-string process "n\n"))
640 (epg-context-set-result-for
643 (epg-context-result-for epg-context 'error)))
644 (delete-process process)))))
646 (defun epg-status-GET_LINE (process string)
647 (let ((entry (assoc string epg-prompt-alist))
652 (concat (read-string (if entry (cdr entry) (concat string ": ")))
655 (epg-context-set-result-for
658 (epg-context-result-for epg-context 'error)))
659 (delete-process process)))))
661 (defun epg-status-GOODSIG (process string)
662 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
663 (epg-context-set-result-for
666 (cons (epg-make-signature
668 (match-string 1 string)
669 (if (eq (epg-context-protocol epg-context) 'CMS)
671 (epg-dn-from-string (match-string 2 string))
672 (error (match-string 2 string)))
673 (match-string 2 string)))
674 (epg-context-result-for epg-context 'verify)))))
676 (defun epg-status-EXPSIG (process string)
677 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
678 (epg-context-set-result-for
681 (cons (epg-make-signature
683 (match-string 1 string)
684 (if (eq (epg-context-protocol epg-context) 'CMS)
686 (epg-dn-from-string (match-string 2 string))
687 (error (match-string 2 string)))
688 (match-string 2 string)))
689 (epg-context-result-for epg-context 'verify)))))
691 (defun epg-status-EXPKEYSIG (process string)
692 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
693 (epg-context-set-result-for
696 (cons (epg-make-signature
698 (match-string 1 string)
699 (if (eq (epg-context-protocol epg-context) 'CMS)
701 (epg-dn-from-string (match-string 2 string))
702 (error (match-string 2 string)))
703 (match-string 2 string)))
704 (epg-context-result-for epg-context 'verify)))))
706 (defun epg-status-REVKEYSIG (process string)
707 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
708 (epg-context-set-result-for
711 (cons (epg-make-signature
713 (match-string 1 string)
714 (if (eq (epg-context-protocol epg-context) 'CMS)
716 (epg-dn-from-string (match-string 2 string))
717 (error (match-string 2 string)))
718 (match-string 2 string)))
719 (epg-context-result-for epg-context 'verify)))))
721 (defun epg-status-BADSIG (process string)
722 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
723 (epg-context-set-result-for
726 (cons (epg-make-signature
728 (match-string 1 string)
729 (if (eq (epg-context-protocol epg-context) 'CMS)
731 (epg-dn-from-string (match-string 2 string))
732 (error (match-string 2 string)))
733 (match-string 2 string)))
734 (epg-context-result-for epg-context 'verify)))))
736 (defun epg-status-VALIDSIG (process string)
737 (let ((signature (car (epg-context-result-for epg-context 'verify))))
739 (eq (epg-signature-status signature) 'good)
740 (string-match "\\`\\([^ ]+\\) " string))
741 (epg-signature-set-fingerprint signature (match-string 1 string)))))
743 (defun epg-status-TRUST_UNDEFINED (process string)
744 (let ((signature (car (epg-context-result-for epg-context 'verify))))
746 (eq (epg-signature-status signature) 'good))
747 (epg-signature-set-validity signature 'undefined))))
749 (defun epg-status-TRUST_NEVER (process string)
750 (let ((signature (car (epg-context-result-for epg-context 'verify))))
752 (eq (epg-signature-status signature) 'good))
753 (epg-signature-set-validity signature 'never))))
755 (defun epg-status-TRUST_MARGINAL (process string)
756 (let ((signature (car (epg-context-result-for epg-context 'verify))))
758 (eq (epg-signature-status signature) 'marginal))
759 (epg-signature-set-validity signature 'marginal))))
761 (defun epg-status-TRUST_FULLY (process string)
762 (let ((signature (car (epg-context-result-for epg-context 'verify))))
764 (eq (epg-signature-status signature) 'good))
765 (epg-signature-set-validity signature 'full))))
767 (defun epg-status-TRUST_ULTIMATE (process string)
768 (let ((signature (car (epg-context-result-for epg-context 'verify))))
770 (eq (epg-signature-status signature) 'good))
771 (epg-signature-set-validity signature 'ultimate))))
773 (defun epg-status-PROGRESS (process string)
774 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
776 (funcall (if (consp (epg-context-progress-callback epg-context))
777 (car (epg-context-progress-callback epg-context))
778 (epg-context-progress-callback epg-context))
780 (match-string 1 string)
781 (match-string 2 string)
782 (string-to-number (match-string 3 string))
783 (string-to-number (match-string 4 string))
784 (if (consp (epg-context-progress-callback epg-context))
785 (cdr (epg-context-progress-callback epg-context))))))
787 (defun epg-status-DECRYPTION_FAILED (process string)
788 (epg-context-set-result-for
790 (cons 'decryption-failed
791 (epg-context-result-for epg-context 'error))))
793 (defun epg-status-NODATA (process string)
794 (epg-context-set-result-for
796 (cons (cons 'no-data (string-to-number string))
797 (epg-context-result-for epg-context 'error))))
799 (defun epg-status-UNEXPECTED (process string)
800 (epg-context-set-result-for
802 (cons (cons 'unexpected (string-to-number string))
803 (epg-context-result-for epg-context 'error))))
805 (defun epg-status-KEYEXPIRED (process string)
806 (epg-context-set-result-for
808 (cons (cons 'key-expired string)
809 (epg-context-result-for epg-context 'error))))
811 (defun epg-status-KEYREVOKED (process string)
812 (epg-context-set-result-for
815 (epg-context-result-for epg-context 'error))))
817 (defun epg-status-BADARMOR (process string)
818 (epg-context-set-result-for
821 (epg-context-result-for epg-context 'error))))
823 (defun epg-status-INV_RECP (process string)
824 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
825 (epg-context-set-result-for
827 (cons (list 'invalid-recipient
828 (string-to-number (match-string 1 string))
829 (match-string 2 string))
830 (epg-context-result-for epg-context 'error)))))
832 (defun epg-status-NO_RECP (process string)
833 (epg-context-set-result-for
836 (epg-context-result-for epg-context 'error))))
838 (defun epg-status-DELETE_PROBLEM (process string)
839 (if (string-match "\\`\\([0-9]+\\)" string)
840 (epg-context-set-result-for
842 (cons (cons 'delete-problem (string-to-number (match-string 1 string)))
843 (epg-context-result-for epg-context 'error)))))
845 (defun epg-status-SIG_CREATED (process string)
846 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
847 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
848 (epg-context-set-result-for
850 (cons (list (cons 'type (string-to-char (match-string 1 string)))
851 (cons 'pubkey-algorithm
852 (string-to-number (match-string 2 string)))
853 (cons 'digest-algorithm
854 (string-to-number (match-string 3 string)))
855 (cons 'class (string-to-number (match-string 4 string) 16))
856 (cons 'creation-time (match-string 5 string))
857 (cons 'fingerprint (substring string (match-end 0))))
858 (epg-context-result-for epg-context 'sign)))))
860 (defun epg-passphrase-callback-function (context key-id handback)
863 "Passphrase for symmetric encryption: "
865 "Passphrase for PIN: "
866 (let ((entry (assoc key-id epg-user-id-alist)))
868 (format "Passphrase for %s %s: " key-id (cdr entry))
869 (format "Passphrase for %s: " key-id)))))))
871 (defun epg-progress-callback-function (context what char current total
873 (message "%s: %d%%/%d%%" what current total))
875 (defun epg-configuration ()
876 "Return a list of internal configuration parameters of `epg-gpg-program'."
879 (apply #'call-process epg-gpg-program nil (list t nil) nil
880 '("--with-colons" "--list-config"))
881 (goto-char (point-min))
882 (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
883 (setq type (intern (match-string 1))
884 config (cons (cons type
886 '(pubkey cipher digest compress))
887 (mapcar #'string-to-number
888 (delete "" (split-string
895 (defun epg-list-keys-1 (context name mode)
896 (let ((args (append (list "--with-colons" "--no-greeting" "--batch"
899 (if (or (eq mode t) (eq mode 'secret))
904 (unless (eq (epg-context-protocol context) 'CMS)
905 '("--fixed-list-mode"))
906 (if name (list name))))
907 keys string field index)
909 (apply #'call-process
910 (if (eq (epg-context-protocol context) 'CMS)
913 nil (list t nil) nil args)
914 (goto-char (point-min))
915 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
916 (setq keys (cons (make-vector 15 nil) keys)
917 string (match-string 0)
921 (string-match "\\([^:]+\\)?:" string index))
922 (setq index (match-end 0))
923 (aset (car keys) field (match-string 1 string))
924 (setq field (1+ field))))
927 (defun epg-make-sub-key-1 (line)
930 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
932 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
934 (member (aref line 0) '("sec" "ssb"))
935 (string-to-number (aref line 3))
936 (string-to-number (aref line 2))
941 (defun epg-list-keys (context &optional name mode)
942 "Return a list of epg-key objects matched with NAME.
943 If MODE is nil, only public keyring should be searched.
944 If MODE is t or 'secret, only secret keyring should be searched.
945 Otherwise, only public keyring should be searched and the key
946 signatures should be included."
947 (let ((lines (epg-list-keys-1 context name mode))
951 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
953 (epg-key-set-sub-key-list
955 (nreverse (epg-key-sub-key-list (car keys))))
956 (epg-key-set-user-id-list
958 (nreverse (epg-key-user-id-list (car keys)))))
959 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
960 keys (cons (epg-make-key
961 (if (aref (car lines) 8)
962 (cdr (assq (string-to-char (aref (car lines) 8))
963 epg-key-validity-alist))))
965 (epg-key-set-sub-key-list
967 (cons (epg-make-sub-key-1 (car lines))
968 (epg-key-sub-key-list (car keys)))))
969 ((member (aref (car lines) 0) '("sub" "ssb"))
970 (epg-key-set-sub-key-list
972 (cons (epg-make-sub-key-1 (car lines))
973 (epg-key-sub-key-list (car keys)))))
974 ((equal (aref (car lines) 0) "uid")
975 (epg-key-set-user-id-list
977 (cons (epg-make-user-id
978 (if (aref (car lines) 1)
979 (cdr (assq (string-to-char (aref (car lines) 1))
980 epg-key-validity-alist)))
983 (epg-dn-from-string (aref (car lines) 9))
984 (error (aref (car lines) 9)))
985 (aref (car lines) 9)))
986 (epg-key-user-id-list (car keys)))))
987 ((equal (aref (car lines) 0) "fpr")
988 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
989 (aref (car lines) 9))))
990 (setq lines (cdr lines)))
992 (epg-key-set-sub-key-list
994 (nreverse (epg-key-sub-key-list (car keys))))
995 (epg-key-set-user-id-list
997 (nreverse (epg-key-user-id-list (car keys)))))
1000 (if (fboundp 'make-temp-file)
1001 (defalias 'epg-make-temp-file 'make-temp-file)
1002 ;; stolen from poe.el.
1003 (defun epg-make-temp-file (prefix)
1004 "Create a temporary file.
1005 The returned file name (created by appending some random characters at the end
1006 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1007 is guaranteed to point to a newly created empty file.
1008 You can then use `write-region' to write new data into the file."
1009 (let (tempdir tempfile)
1012 ;; First, create a temporary directory.
1013 (while (condition-case ()
1015 (setq tempdir (make-temp-name
1017 (file-name-directory prefix)
1019 ;; return nil or signal an error.
1020 (make-directory tempdir))
1022 (file-already-exists t)))
1023 (set-file-modes tempdir 448)
1024 ;; Second, create a temporary file in the tempdir.
1025 ;; There *is* a race condition between `make-temp-name'
1026 ;; and `write-region', but we don't care it since we are
1027 ;; in a private directory now.
1028 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1029 (write-region "" nil tempfile nil 'silent)
1030 (set-file-modes tempfile 384)
1031 ;; Finally, make a hard-link from the tempfile.
1032 (while (condition-case ()
1034 (setq file (make-temp-name prefix))
1035 ;; return nil or signal an error.
1036 (add-name-to-file tempfile file))
1038 (file-already-exists t)))
1040 ;; Cleanup the tempfile.
1042 (file-exists-p tempfile)
1043 (delete-file tempfile))
1044 ;; Cleanup the tempdir.
1046 (file-directory-p tempdir)
1047 (delete-directory tempdir))))))
1050 (defun epg-cancel (context)
1051 (if (eq (process-status (epg-context-process context)) 'run)
1052 (delete-process (epg-context-process context))))
1055 (defun epg-start-decrypt (context cipher)
1056 "Initiate a decrypt operation on CIPHER.
1057 CIPHER is a data object.
1059 If you use this function, you will need to wait for the completion of
1060 `epg-gpg-program' by using `epg-wait-for-completion' and call
1061 `epg-reset' to clear a temporaly output file.
1062 If you are unsure, use synchronous version of this function
1063 `epg-decrypt-file' or `epg-decrypt-string' instead."
1064 (unless (epg-data-file cipher)
1065 (error "Not a file"))
1066 (epg-context-set-result context nil)
1067 (epg-start context (list "--decrypt" (epg-data-file cipher)))
1068 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1069 (unless (eq (epg-context-protocol context) 'CMS)
1070 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1073 (defun epg-decrypt-file (context cipher plain)
1074 "Decrypt a file CIPHER and store the result to a file PLAIN.
1075 If PLAIN is nil, it returns the result as a string."
1079 (epg-context-set-output-file context plain)
1080 (epg-context-set-output-file context
1081 (epg-make-temp-file "epg-output")))
1082 (epg-start-decrypt context (epg-make-data-from-file cipher))
1083 (epg-wait-for-completion context)
1084 (if (epg-context-result-for context 'error)
1085 (error "Decrypt failed: %S"
1086 (epg-context-result-for context 'error)))
1088 (epg-read-output context)))
1090 (epg-delete-output-file context))
1091 (epg-reset context)))
1094 (defun epg-decrypt-string (context cipher)
1095 "Decrypt a string CIPHER and return the plain text."
1096 (let ((input-file (epg-make-temp-file "epg-input"))
1097 (coding-system-for-write 'binary))
1100 (write-region cipher nil input-file nil 'quiet)
1101 (epg-context-set-output-file context
1102 (epg-make-temp-file "epg-output"))
1103 (epg-start-decrypt context (epg-make-data-from-file input-file))
1105 (epg-wait-for-completion context)
1106 (if (epg-context-result-for context 'error)
1107 (error "Decrypt failed: %S"
1108 (epg-context-result-for context 'error)))
1109 (epg-read-output context))
1110 (epg-delete-output-file context)
1111 (if (file-exists-p input-file)
1112 (delete-file input-file))
1113 (epg-reset context))))
1116 (defun epg-start-verify (context signature &optional signed-text)
1117 "Initiate a verify operation on SIGNATURE.
1118 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1120 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1121 For a normal or a clear text signature, SIGNED-TEXT should be nil.
1123 If you use this function, you will need to wait for the completion of
1124 `epg-gpg-program' by using `epg-wait-for-completion' and call
1125 `epg-reset' to clear a temporaly output file.
1126 If you are unsure, use synchronous version of this function
1127 `epg-verify-file' or `epg-verify-string' instead."
1128 (epg-context-set-result context nil)
1130 ;; Detached signature.
1131 (if (epg-data-file signed-text)
1132 (epg-start context (list "--verify" (epg-data-file signature)
1133 (epg-data-file signed-text)))
1134 (epg-start context (list "--verify" (epg-data-file signature) "-"))
1135 (if (eq (process-status (epg-context-process context)) 'run)
1136 (process-send-string (epg-context-process context)
1137 (epg-data-string signed-text))))
1138 ;; Normal (or cleartext) signature.
1139 (if (epg-data-file signature)
1140 (epg-start context (list "--verify" (epg-data-file signature)))
1141 (epg-start context (list "--verify"))
1142 (if (eq (process-status (epg-context-process context)) 'run)
1143 (process-send-string (epg-context-process context)
1144 (epg-data-string signature))))))
1147 (defun epg-verify-file (context signature &optional signed-text plain)
1148 "Verify a file SIGNATURE.
1149 SIGNED-TEXT and PLAIN are also a file if they are specified.
1151 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1152 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1156 (epg-context-set-output-file context plain)
1157 (epg-context-set-output-file context
1158 (epg-make-temp-file "epg-output")))
1160 (epg-start-verify context
1161 (epg-make-data-from-file signature)
1162 (epg-make-data-from-file signed-text))
1163 (epg-start-verify context
1164 (epg-make-data-from-file signature)))
1165 (epg-wait-for-completion context)
1167 (epg-read-output context)))
1169 (epg-delete-output-file context))
1170 (epg-reset context)))
1173 (defun epg-verify-string (context signature &optional signed-text)
1174 "Verify a string SIGNATURE.
1175 SIGNED-TEXT is a string if it is specified.
1177 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1178 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1179 (let ((coding-system-for-write 'binary)
1183 (epg-context-set-output-file context
1184 (epg-make-temp-file "epg-output"))
1187 (setq input-file (epg-make-temp-file "epg-signature"))
1188 (write-region signature nil input-file nil 'quiet)
1189 (epg-start-verify context
1190 (epg-make-data-from-file input-file)
1191 (epg-make-data-from-string signed-text)))
1192 (epg-start-verify context (epg-make-data-from-string signature)))
1194 (epg-wait-for-completion context)
1195 (epg-read-output context))
1196 (epg-delete-output-file context)
1198 (file-exists-p input-file))
1199 (delete-file input-file))
1200 (epg-reset context))))
1203 (defun epg-start-sign (context plain &optional mode)
1204 "Initiate a sign operation on PLAIN.
1205 PLAIN is a data object.
1207 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1208 If MODE is t or 'detached, it makes a detached signature.
1209 Otherwise, it makes a normal signature.
1211 If you use this function, you will need to wait for the completion of
1212 `epg-gpg-program' by using `epg-wait-for-completion' and call
1213 `epg-reset' to clear a temporaly output file.
1214 If you are unsure, use synchronous version of this function
1215 `epg-sign-file' or `epg-sign-string' instead."
1216 (epg-context-set-result context nil)
1218 (append (list (if (eq mode 'clearsign)
1220 (if (or (eq mode t) (eq mode 'detached))
1228 (car (epg-key-sub-key-list signer)))))
1229 (epg-context-signers context)))
1230 (if (epg-data-file plain)
1231 (list (epg-data-file plain)))))
1232 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1233 (unless (eq (epg-context-protocol context) 'CMS)
1234 (epg-wait-for-status context '("BEGIN_SIGNING")))
1235 (if (and (epg-data-string plain)
1236 (eq (process-status (epg-context-process context)) 'run))
1237 (process-send-string (epg-context-process context)
1238 (epg-data-string plain))))
1241 (defun epg-sign-file (context plain signature &optional mode)
1242 "Sign a file PLAIN and store the result to a file SIGNATURE.
1243 If SIGNATURE is nil, it returns the result as a string.
1244 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1245 If MODE is t or 'detached, it makes a detached signature.
1246 Otherwise, it makes a normal signature."
1250 (epg-context-set-output-file context signature)
1251 (epg-context-set-output-file context
1252 (epg-make-temp-file "epg-output")))
1253 (epg-start-sign context (epg-make-data-from-file plain) mode)
1254 (epg-wait-for-completion context)
1255 (if (epg-context-result-for context 'sign)
1256 (if (epg-context-result-for context 'error)
1257 (message "Sign warning: %S"
1258 (epg-context-result-for context 'error)))
1259 (if (epg-context-result-for context 'error)
1260 (error "Sign failed: %S"
1261 (epg-context-result-for context 'error))
1262 (error "Sign failed")))
1264 (epg-read-output context)))
1266 (epg-delete-output-file context))
1267 (epg-reset context)))
1270 (defun epg-sign-string (context plain &optional mode)
1271 "Sign a string PLAIN and return the output as string.
1272 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1273 If MODE is t or 'detached, it makes a detached signature.
1274 Otherwise, it makes a normal signature."
1277 (epg-context-set-output-file context
1278 (epg-make-temp-file "epg-output"))
1279 (epg-start-sign context (epg-make-data-from-string plain) mode)
1281 (epg-wait-for-completion context)
1282 (if (epg-context-result-for context 'sign)
1283 (if (epg-context-result-for context 'error)
1284 (message "Sign warning: %S"
1285 (epg-context-result-for context 'error)))
1286 (if (epg-context-result-for context 'error)
1287 (error "Sign failed: %S"
1288 (epg-context-result-for context 'error))
1289 (error "Sign failed")))
1290 (epg-read-output context))
1291 (epg-delete-output-file context)
1292 (epg-reset context)))
1295 (defun epg-start-encrypt (context plain recipients
1296 &optional sign always-trust)
1297 "Initiate an encrypt operation on PLAIN.
1298 PLAIN is a data object.
1299 If RECIPIENTS is nil, it performs symmetric encryption.
1301 If you use this function, you will need to wait for the completion of
1302 `epg-gpg-program' by using `epg-wait-for-completion' and call
1303 `epg-reset' to clear a temporaly output file.
1304 If you are unsure, use synchronous version of this function
1305 `epg-encrypt-file' or `epg-encrypt-string' instead."
1306 (epg-context-set-result context nil)
1308 (append (if always-trust '("--always-trust"))
1309 (if recipients '("--encrypt") '("--symmetric"))
1313 (mapcar (lambda (signer)
1315 (epg-context-signers context)))))
1321 (car (epg-key-sub-key-list recipient)))))
1323 (if (epg-data-file plain)
1324 (list (epg-data-file plain)))))
1325 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1326 (unless (eq (epg-context-protocol context) 'CMS)
1328 (epg-wait-for-status context '("BEGIN_SIGNING"))
1329 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
1330 (if (and (epg-data-string plain)
1331 (eq (process-status (epg-context-process context)) 'run))
1332 (process-send-string (epg-context-process context)
1333 (epg-data-string plain))))
1336 (defun epg-encrypt-file (context plain recipients
1337 cipher &optional sign always-trust)
1338 "Encrypt a file PLAIN and store the result to a file CIPHER.
1339 If CIPHER is nil, it returns the result as a string.
1340 If RECIPIENTS is nil, it performs symmetric encryption."
1344 (epg-context-set-output-file context cipher)
1345 (epg-context-set-output-file context
1346 (epg-make-temp-file "epg-output")))
1347 (epg-start-encrypt context (epg-make-data-from-file plain)
1348 recipients sign always-trust)
1349 (epg-wait-for-completion context)
1351 (if (epg-context-result-for context 'sign)
1352 (if (epg-context-result-for context 'error)
1353 (message "Sign warning: %S"
1354 (epg-context-result-for context 'error)))
1355 (if (epg-context-result-for context 'error)
1356 (error "Sign failed: %S"
1357 (epg-context-result-for context 'error))
1358 (error "Sign failed"))))
1359 (if (epg-context-result-for context 'error)
1360 (error "Encrypt failed: %S"
1361 (epg-context-result-for context 'error)))
1363 (epg-read-output context)))
1365 (epg-delete-output-file context))
1366 (epg-reset context)))
1369 (defun epg-encrypt-string (context plain recipients
1370 &optional sign always-trust)
1371 "Encrypt a string PLAIN.
1372 If RECIPIENTS is nil, it performs symmetric encryption."
1375 (epg-context-set-output-file context
1376 (epg-make-temp-file "epg-output"))
1377 (epg-start-encrypt context (epg-make-data-from-string plain)
1378 recipients sign always-trust)
1380 (epg-wait-for-completion context)
1382 (if (epg-context-result-for context 'sign)
1383 (if (epg-context-result-for context 'error)
1384 (message "Sign warning: %S"
1385 (epg-context-result-for context 'error)))
1386 (if (epg-context-result-for context 'error)
1387 (error "Sign failed: %S"
1388 (epg-context-result-for context 'error))
1389 (error "Sign failed"))))
1390 (if (epg-context-result-for context 'error)
1391 (error "Encrypt failed: %S"
1392 (epg-context-result-for context 'error)))
1393 (epg-read-output context))
1394 (epg-delete-output-file context)
1395 (epg-reset context)))
1398 (defun epg-start-export-keys (context keys)
1399 "Initiate an export keys operation.
1401 If you use this function, you will need to wait for the completion of
1402 `epg-gpg-program' by using `epg-wait-for-completion' and call
1403 `epg-reset' to clear a temporaly output file.
1404 If you are unsure, use synchronous version of this function
1405 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1406 (epg-context-set-result context nil)
1407 (epg-start context (cons "--export"
1411 (car (epg-key-sub-key-list key))))
1415 (defun epg-export-keys-to-file (context keys file)
1416 "Extract public KEYS."
1420 (epg-context-set-output-file context file)
1421 (epg-context-set-output-file context
1422 (epg-make-temp-file "epg-output")))
1423 (epg-start-export-keys context keys)
1424 (epg-wait-for-completion context)
1425 (if (epg-context-result-for context 'error)
1426 (error "Export keys failed: %S"
1427 (epg-context-result-for context 'error)))
1429 (epg-read-output context)))
1431 (epg-delete-output-file context))
1432 (epg-reset context)))
1435 (defun epg-export-keys-to-string (context keys)
1436 "Extract public KEYS and return them as a string."
1437 (epg-export-keys-to-file context keys nil))
1440 (defun epg-start-import-keys (context keys)
1441 "Initiate an import keys operation.
1442 KEYS is a data object.
1444 If you use this function, you will need to wait for the completion of
1445 `epg-gpg-program' by using `epg-wait-for-completion' and call
1446 `epg-reset' to clear a temporaly output file.
1447 If you are unsure, use synchronous version of this function
1448 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1449 (epg-context-set-result context nil)
1450 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
1451 (epg-start context (list "--import" (epg-data-file keys)))
1452 (if (and (epg-data-string keys)
1453 (eq (process-status (epg-context-process context)) 'run))
1454 (process-send-string (epg-context-process context)
1455 (epg-data-string keys))))
1457 (defun epg-import-keys-1 (context keys)
1460 (epg-start-import-keys context keys)
1461 (if (epg-data-file keys)
1462 (epg-flush context))
1463 (epg-wait-for-completion context)
1464 (if (epg-context-result-for context 'error)
1465 (error "Import keys failed: %S"
1466 (epg-context-result-for context 'error)))
1467 (epg-read-output context))
1468 (epg-reset context)))
1471 (defun epg-import-keys-from-file (context keys)
1472 "Add keys from a file KEYS."
1473 (epg-import-keys-1 context (epg-make-data-from-file keys)))
1476 (defun epg-import-keys-from-string (context keys)
1477 "Add keys from a string KEYS."
1478 (epg-import-keys-1 context (epg-make-data-from-string keys)))
1481 (defun epg-start-delete-keys (context keys &optional allow-secret)
1482 "Initiate an delete keys operation.
1484 If you use this function, you will need to wait for the completion of
1485 `epg-gpg-program' by using `epg-wait-for-completion' and call
1486 `epg-reset' to clear a temporaly output file.
1487 If you are unsure, use synchronous version of this function
1488 `epg-delete-keys' instead."
1489 (epg-context-set-result context nil)
1490 (epg-start context (cons (if allow-secret
1491 "--delete-secret-key"
1496 (car (epg-key-sub-key-list key))))
1500 (defun epg-delete-keys (context keys &optional allow-secret)
1501 "Delete KEYS from the key ring."
1504 (epg-start-delete-keys context keys allow-secret)
1505 (epg-wait-for-completion context)
1506 (if (epg-context-result-for context 'error)
1507 (error "Delete keys failed: %S"
1508 (epg-context-result-for context 'error))))
1509 (epg-reset context)))
1512 (defun epg-start-sign-keys (context keys &optional local)
1513 "Initiate an sign keys operation.
1515 If you use this function, you will need to wait for the completion of
1516 `epg-gpg-program' by using `epg-wait-for-completion' and call
1517 `epg-reset' to clear a temporaly output file.
1518 If you are unsure, use synchronous version of this function
1519 `epg-sign-keys' instead."
1520 (epg-context-set-result context nil)
1521 (epg-start context (cons (if local
1527 (car (epg-key-sub-key-list key))))
1531 (defun epg-sign-keys (context keys &optional local)
1532 "Sign KEYS from the key ring."
1535 (epg-start-sign-keys context keys local)
1536 (epg-wait-for-completion context)
1537 (if (epg-context-result-for context 'error)
1538 (error "Sign keys failed: %S"
1539 (epg-context-result-for context 'error))))
1540 (epg-reset context)))
1542 (defun epg-decode-hexstring (string)
1544 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
1545 (setq string (replace-match "\\x\\&" t nil string)
1547 (car (read-from-string (concat "\"" string "\"")))))
1549 (defun epg-decode-quotedstring (string)
1551 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
1552 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
1554 (if (match-beginning 2)
1555 (setq string (replace-match "\\2" t nil string)
1557 (if (match-beginning 3)
1558 (setq string (replace-match "\\x\\3" t nil string)
1560 (setq string (replace-match "\\\\\\\\\\4" t nil string)
1561 index (+ index 3)))))
1562 (car (read-from-string (concat "\"" string "\"")))))
1564 (defun epg-dn-from-string (string)
1565 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
1566 The return value is an alist mapping from types to values."
1568 (length (length string))
1569 alist type value group)
1570 (while (< index length)
1571 (if (eq index (string-match "[ \t\n\r]*" string index))
1572 (setq index (match-end 0)))
1573 (if (eq index (string-match
1574 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
1576 (setq type (match-string 1 string)
1577 index (match-end 0))
1578 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
1580 (setq type (match-string 1 string)
1581 index (match-end 0))))
1583 (error "Invalid type"))
1584 (if (eq index (string-match
1585 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
1587 (setq index (match-end 0)
1588 value (epg-decode-quotedstring (match-string 0 string)))
1589 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
1590 (setq index (match-end 0)
1591 value (epg-decode-hexstring (match-string 1 string)))
1592 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
1594 (setq index (match-end 0)
1595 value (epg-decode-quotedstring (match-string 0 string))))))
1597 (if (stringp (car (car alist)))
1598 (setcar alist (list (cons type value) (car alist)))
1599 (setcar alist (cons (cons type value) (car alist))))
1600 (if (consp (car (car alist)))
1601 (setcar alist (nreverse (car alist))))
1602 (setq alist (cons (cons type value) alist)
1605 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
1606 (setq index (match-end 0)
1607 group (eq (aref string (match-beginning 1)) ?+))))
1610 (defun epg-decode-dn (alist)
1611 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
1612 Type names are resolved using `epg-dn-type-alist'."
1615 (if (stringp (car rdn))
1616 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
1618 (format "%s=%s" (cdr entry) (cdr rdn))
1619 (format "%s=%s" (car rdn) (cdr rdn))))
1620 (concat "(" (epg-decode-dn rdn) ")")))
1626 ;;; epg.el ends here