Fixed.
[elisp/epg.git] / epg.el
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
5
6 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Keywords: PGP, GnuPG
8
9 ;; This file is part of EasyPG.
10
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)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Code:
27
28 (defgroup epg ()
29   "The EasyPG Library")
30
31 (defcustom epg-gpg-program "gpg"
32   "The `gpg' executable."
33   :group 'epg
34   :type 'string)
35
36 (defcustom epg-gpgsm-program "gpgsm"
37   "The `gpgsm' executable."
38   :group 'epg
39   :type 'string)
40
41 (defconst epg-version-number "0.0.1")
42
43 (defvar epg-user-id nil
44   "GnuPG ID of your default identity.")
45
46 (defvar epg-user-id-alist nil
47   "An alist mapping from key ID to user ID.")
48
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)
55
56 ;; from gnupg/include/cipher.h
57 (defconst epg-cipher-algorithm-alist
58   '((0 . "NONE")
59     (1 . "IDEA")
60     (2 . "3DES")
61     (3 . "CAST5")
62     (4 . "BLOWFISH")
63     (7 . "AES")
64     (8 . "AES192")
65     (9 . "AES256")
66     (10 . "TWOFISH")
67     (110 . "DUMMY")))
68
69 ;; from gnupg/include/cipher.h
70 (defconst epg-pubkey-algorithm-alist
71   '((1 . "RSA")
72     (2 . "RSA_E")
73     (3 . "RSA_S")
74     (16 . "ELGAMAL_E")
75     (17 . "DSA")
76     (20 . "ELGAMAL")))
77
78 ;; from gnupg/include/cipher.h
79 (defconst epg-digest-algorithm-alist
80   '((1 . "MD5")
81     (2 . "SHA1")
82     (3 . "RMD160")
83     (8 . "SHA256")
84     (9 . "SHA384")
85     (10 . "SHA512")))
86
87 ;; from gnupg/include/cipher.h
88 (defconst epg-compress-algorithm-alist
89   '((0 . "NONE")
90     (1 . "ZIP")
91     (2 . "ZLIB")
92     (3 . "BZIP2")))
93
94 (defconst epg-invalid-recipients-alist
95   '((0 . "No specific reason given")
96     (1 . "Not Found")
97     (2 . "Ambigious specification")
98     (3 . "Wrong key usage")
99     (4 . "Key revoked")
100     (5 . "Key expired")
101     (6 . "No CRL known")
102     (7 . "CRL too old")
103     (8 . "Policy mismatch")
104     (9 . "Not a secret key")
105     (10 . "Key not trusted")))
106
107 (defconst epg-delete-problem-alist
108   '((1 . "No such key")
109     (2 . "Must delete secret key first")
110     (3 . "Ambigious specification")))
111
112 (defvar epg-key-validity-alist
113   '((?o . unknown)
114     (?i . invalid)
115     (?d . disabled)
116     (?r . revoked)
117     (?e . expired)
118     (?- . none)
119     (?q . undefined)
120     (?n . never)
121     (?m . marginal)
122     (?f . full)
123     (?u . ultimate)))
124
125 (defvar epg-key-capablity-alist
126   '((?e . encrypt)
127     (?s . sign)
128     (?c . certify)
129     (?a . authentication)))
130
131 (defvar epg-dn-type-alist
132   '(("1.2.840.113549.1.9.1" . "EMail")
133     ("2.5.4.12" . "T")
134     ("2.5.4.42" . "GN")
135     ("2.5.4.4" . "SN")
136     ("0.2.262.1.10.7.20" . "NameDistinguisher")
137     ("2.5.4.16" . "ADDR")
138     ("2.5.4.15" . "BC")
139     ("2.5.4.13" . "D")
140     ("2.5.4.17" . "PostalCode")
141     ("2.5.4.65" . "Pseudo")
142     ("2.5.4.5" . "SerialNumber")))
143
144 (defvar epg-prompt-alist nil)
145
146 (defun epg-make-data-from-file (file)
147   "Make a data object from FILE."
148   (cons 'epg-data (vector file nil)))
149
150 (defun epg-make-data-from-string (string)
151   "Make a data object from STRING."
152   (cons 'epg-data (vector nil string)))
153
154 (defun epg-data-file (data)
155   "Return the file of DATA."
156   (unless (eq (car data) 'epg-data)
157     (signal 'wrong-type-argument (list 'epg-data-p data)))
158   (aref (cdr data) 0))
159
160 (defun epg-data-string (data)
161   "Return the string of DATA."
162   (unless (eq (car data) 'epg-data)
163     (signal 'wrong-type-argument (list 'epg-data-p data)))
164   (aref (cdr data) 1))
165
166 (defun epg-make-context (&optional protocol armor textmode include-certs
167                                    cipher-algorithm digest-algorithm
168                                    compress-algorithm)
169   "Return a context object."
170   (cons 'epg-context
171         (vector (or protocol 'OpenPGP) armor textmode include-certs
172                 cipher-algorithm digest-algorithm compress-algorithm
173                 #'epg-passphrase-callback-function
174                 #'epg-progress-callback-function
175                 nil nil nil nil)))
176
177 (defun epg-context-protocol (context)
178   "Return the protocol used within CONTEXT."
179   (unless (eq (car context) 'epg-context)
180     (signal 'wrong-type-argument (list 'epg-context-p context)))
181   (aref (cdr context) 0))
182
183 (defun epg-context-armor (context)
184   "Return t if the output shouled be ASCII armored in CONTEXT."
185   (unless (eq (car context) 'epg-context)
186     (signal 'wrong-type-argument (list 'epg-context-p context)))
187   (aref (cdr context) 1))
188
189 (defun epg-context-textmode (context)
190   "Return t if canonical text mode should be used in CONTEXT."
191   (unless (eq (car context) 'epg-context)
192     (signal 'wrong-type-argument (list 'epg-context-p context)))
193   (aref (cdr context) 2))
194
195 (defun epg-context-include-certs (context)
196   "Return how many certificates should be included in an S/MIME signed
197 message."
198   (unless (eq (car context) 'epg-context)
199     (signal 'wrong-type-argument (list 'epg-context-p context)))
200   (aref (cdr context) 3))
201
202 (defun epg-context-cipher-algorithm (context)
203   "Return the cipher algorithm in CONTEXT."
204   (unless (eq (car context) 'epg-context)
205     (signal 'wrong-type-argument (list 'epg-context-p context)))
206   (aref (cdr context) 4))
207
208 (defun epg-context-digest-algorithm (context)
209   "Return the digest algorithm in CONTEXT."
210   (unless (eq (car context) 'epg-context)
211     (signal 'wrong-type-argument (list 'epg-context-p context)))
212   (aref (cdr context) 5))
213
214 (defun epg-context-compress-algorithm (context)
215   "Return the compress algorithm in CONTEXT."
216   (unless (eq (car context) 'epg-context)
217     (signal 'wrong-type-argument (list 'epg-context-p context)))
218   (aref (cdr context) 6))
219
220 (defun epg-context-passphrase-callback (context)
221   "Return the function used to query passphrase."
222   (unless (eq (car context) 'epg-context)
223     (signal 'wrong-type-argument (list 'epg-context-p context)))
224   (aref (cdr context) 7))
225
226 (defun epg-context-progress-callback (context)
227   "Return the function which handles progress update."
228   (unless (eq (car context) 'epg-context)
229     (signal 'wrong-type-argument (list 'epg-context-p context)))
230   (aref (cdr context) 8))
231
232 (defun epg-context-signers (context)
233   "Return the list of key-id for singning."
234   (unless (eq (car context) 'epg-context)
235     (signal 'wrong-type-argument (list 'epg-context-p context)))
236   (aref (cdr context) 9))
237
238 (defun epg-context-process (context)
239   "Return the process object of `epg-gpg-program'.
240 This function is for internal use only."
241   (unless (eq (car context) 'epg-context)
242     (signal 'wrong-type-argument (list 'epg-context-p context)))
243   (aref (cdr context) 10))
244
245 (defun epg-context-output-file (context)
246   "Return the output file of `epg-gpg-program'.
247 This function is for internal use only."
248   (unless (eq (car context) 'epg-context)
249     (signal 'wrong-type-argument (list 'epg-context-p context)))
250   (aref (cdr context) 11))
251
252 (defun epg-context-result (context)
253   "Return the result of the previous cryptographic operation."
254   (unless (eq (car context) 'epg-context)
255     (signal 'wrong-type-argument (list 'epg-context-p context)))
256   (aref (cdr context) 12))
257
258 (defun epg-context-set-protocol (context protocol)
259   "Set the protocol used within CONTEXT."
260   (unless (eq (car context) 'epg-context)
261     (signal 'wrong-type-argument (list 'epg-context-p context)))
262   (aset (cdr context) 0 protocol))
263
264 (defun epg-context-set-armor (context armor)
265   "Specify if the output shouled be ASCII armored in CONTEXT."
266   (unless (eq (car context) 'epg-context)
267     (signal 'wrong-type-argument (list 'epg-context-p context)))
268   (aset (cdr context) 1 armor))
269
270 (defun epg-context-set-textmode (context textmode)
271   "Specify if canonical text mode should be used in CONTEXT."
272   (unless (eq (car context) 'epg-context)
273     (signal 'wrong-type-argument (list 'epg-context-p context)))
274   (aset (cdr context) 2 textmode))
275
276 (defun epg-context-set-include-certs (context include-certs)
277  "Set how many certificates should be included in an S/MIME signed message."
278   (unless (eq (car context) 'epg-context)
279     (signal 'wrong-type-argument (list 'epg-context-p context)))
280   (aset (cdr context) 3 include-certs))
281
282 (defun epg-context-set-cipher-algorithm (context cipher-algorithm)
283  "Set the cipher algorithm in CONTEXT."
284   (unless (eq (car context) 'epg-context)
285     (signal 'wrong-type-argument (list 'epg-context-p context)))
286   (aset (cdr context) 4 cipher-algorithm))
287
288 (defun epg-context-set-digest-algorithm (context digest-algorithm)
289  "Set the digest algorithm in CONTEXT."
290   (unless (eq (car context) 'epg-context)
291     (signal 'wrong-type-argument (list 'epg-context-p context)))
292   (aset (cdr context) 5 digest-algorithm))
293
294 (defun epg-context-set-compress-algorithm (context compress-algorithm)
295  "Set the compress algorithm in CONTEXT."
296   (unless (eq (car context) 'epg-context)
297     (signal 'wrong-type-argument (list 'epg-context-p context)))
298   (aset (cdr context) 6 compress-algorithm))
299
300 (defun epg-context-set-passphrase-callback (context
301                                                  passphrase-callback)
302   "Set the function used to query passphrase."
303   (unless (eq (car context) 'epg-context)
304     (signal 'wrong-type-argument (list 'epg-context-p context)))
305   (aset (cdr context) 7 passphrase-callback))
306
307 (defun epg-context-set-progress-callback (context progress-callback)
308   "Set the function which handles progress update."
309   (unless (eq (car context) 'epg-context)
310     (signal 'wrong-type-argument (list 'epg-context-p context)))
311   (aset (cdr context) 8 progress-callback))
312
313 (defun epg-context-set-signers (context signers)
314  "Set the list of key-id for singning."
315   (unless (eq (car context) 'epg-context)
316     (signal 'wrong-type-argument (list 'epg-context-p context)))
317   (aset (cdr context) 9 signers))
318
319 (defun epg-context-set-process (context process)
320   "Set the process object of `epg-gpg-program'.
321 This function is for internal use only."
322   (unless (eq (car context) 'epg-context)
323     (signal 'wrong-type-argument (list 'epg-context-p context)))
324   (aset (cdr context) 10 process))
325
326 (defun epg-context-set-output-file (context output-file)
327   "Set the output file of `epg-gpg-program'.
328 This function is for internal use only."
329   (unless (eq (car context) 'epg-context)
330     (signal 'wrong-type-argument (list 'epg-context-p context)))
331   (aset (cdr context) 11 output-file))
332
333 (defun epg-context-set-result (context result)
334   "Set the result of the previous cryptographic operation."
335   (unless (eq (car context) 'epg-context)
336     (signal 'wrong-type-argument (list 'epg-context-p context)))
337   (aset (cdr context) 12 result))
338
339 (defun epg-make-signature (status &optional key-id)
340   "Return a signature object."
341   (cons 'epg-signature (vector status key-id nil nil nil nil nil nil)))
342
343 (defun epg-signature-status (signature)
344   "Return the status code of SIGNATURE."
345   (unless (eq (car signature) 'epg-signature)
346     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
347   (aref (cdr signature) 0))
348
349 (defun epg-signature-key-id (signature)
350   "Return the key-id of SIGNATURE."
351   (unless (eq (car signature) 'epg-signature)
352     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
353   (aref (cdr signature) 1))
354
355 (defun epg-signature-validity (signature)
356   "Return the validity of SIGNATURE."
357   (unless (eq (car signature) 'epg-signature)
358     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
359   (aref (cdr signature) 2))
360
361 (defun epg-signature-fingerprint (signature)
362   "Return the fingerprint of SIGNATURE."
363   (unless (eq (car signature) 'epg-signature)
364     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
365   (aref (cdr signature) 3))
366
367 (defun epg-signature-creation-time (signature)
368   "Return the creation time of SIGNATURE."
369   (unless (eq (car signature) 'epg-signature)
370     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
371   (aref (cdr signature) 4))
372
373 (defun epg-signature-expiration-time (signature)
374   "Return the expiration time of SIGNATURE."
375   (unless (eq (car signature) 'epg-signature)
376     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
377   (aref (cdr signature) 5))
378
379 (defun epg-signature-pubkey-algorithm (signature)
380   "Return the public key algorithm of SIGNATURE."
381   (unless (eq (car signature) 'epg-signature)
382     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
383   (aref (cdr signature) 6))
384
385 (defun epg-signature-digest-algorithm (signature)
386   "Return the digest algorithm of SIGNATURE."
387   (unless (eq (car signature) 'epg-signature)
388     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
389   (aref (cdr signature) 7))
390
391 (defun epg-signature-set-status (signature status)
392  "Set the status code of SIGNATURE."
393   (unless (eq (car signature) 'epg-signature)
394     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
395   (aset (cdr signature) 0 status))
396
397 (defun epg-signature-set-key-id (signature key-id)
398  "Set the key-id of SIGNATURE."
399   (unless (eq (car signature) 'epg-signature)
400     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
401   (aset (cdr signature) 1 key-id))
402
403 (defun epg-signature-set-validity (signature validity)
404  "Set the validity of SIGNATURE."
405   (unless (eq (car signature) 'epg-signature)
406     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
407   (aset (cdr signature) 2 validity))
408
409 (defun epg-signature-set-fingerprint (signature fingerprint)
410  "Set the fingerprint of SIGNATURE."
411   (unless (eq (car signature) 'epg-signature)
412     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
413   (aset (cdr signature) 3 fingerprint))
414
415 (defun epg-signature-set-creation-time (signature creation-time)
416   "Set the creation time of SIGNATURE."
417   (unless (eq (car signature) 'epg-signature)
418     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
419   (aset (cdr signature) 4 creation-time))
420
421 (defun epg-signature-set-expiration-time (signature expiration-time)
422   "Set the expiration time of SIGNATURE."
423   (unless (eq (car signature) 'epg-signature)
424     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
425   (aset (cdr signature) 5 expiration-time))
426
427 (defun epg-signature-set-pubkey-algorithm (signature pubkey-algorithm)
428   "Set the public key algorithm of SIGNATURE."
429   (unless (eq (car signature) 'epg-signature)
430     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
431   (aset (cdr signature) 6 pubkey-algorithm))
432
433 (defun epg-signature-set-digest-algorithm (signature digest-algorithm)
434   "Set the digest algorithm of SIGNATURE."
435   (unless (eq (car signature) 'epg-signature)
436     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
437   (aset (cdr signature) 7 digest-algorithm))
438
439 (defun epg-make-key (owner-trust)
440   "Return a key object."
441   (cons 'epg-key (vector owner-trust nil nil)))
442
443 (defun epg-key-owner-trust (key)
444   "Return the owner trust of KEY."
445   (unless (eq (car key) 'epg-key)
446     (signal 'wrong-type-argument (list 'epg-key-p key)))
447   (aref (cdr key) 0))
448
449 (defun epg-key-sub-key-list (key)
450   "Return the sub key list of KEY."
451   (unless (eq (car key) 'epg-key)
452     (signal 'wrong-type-argument (list 'epg-key-p key)))
453   (aref (cdr key) 1))
454
455 (defun epg-key-user-id-list (key)
456   "Return the user ID list of KEY."
457   (unless (eq (car key) 'epg-key)
458     (signal 'wrong-type-argument (list 'epg-key-p key)))
459   (aref (cdr key) 2))
460
461 (defun epg-key-set-sub-key-list (key sub-key-list)
462   "Set the sub key list of KEY."
463   (unless (eq (car key) 'epg-key)
464     (signal 'wrong-type-argument (list 'epg-key-p key)))
465   (aset (cdr key) 1 sub-key-list))
466
467 (defun epg-key-set-user-id-list (key user-id-list)
468   "Set the user ID list of KEY."
469   (unless (eq (car key) 'epg-key)
470     (signal 'wrong-type-argument (list 'epg-key-p key)))
471   (aset (cdr key) 2 user-id-list))
472
473 (defun epg-make-sub-key (validity capability secret algorithm length id
474                                   creation-time expiration-time)
475   "Return a sub key object."
476   (cons 'epg-sub-key
477         (vector validity capability secret algorithm length id creation-time
478                 expiration-time nil)))
479
480 (defun epg-sub-key-validity (sub-key)
481   "Return the validity of SUB-KEY."
482   (unless (eq (car sub-key) 'epg-sub-key)
483     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
484   (aref (cdr sub-key) 0))
485
486 (defun epg-sub-key-capability (sub-key)
487   "Return the capability of SUB-KEY."
488   (unless (eq (car sub-key) 'epg-sub-key)
489     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
490   (aref (cdr sub-key) 1))
491
492 (defun epg-sub-key-secret (sub-key)
493   "Return non-nil if SUB-KEY is a secret key."
494   (unless (eq (car sub-key) 'epg-sub-key)
495     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
496   (aref (cdr sub-key) 2))
497
498 (defun epg-sub-key-algorithm (sub-key)
499   "Return the algorithm of SUB-KEY."
500   (unless (eq (car sub-key) 'epg-sub-key)
501     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
502   (aref (cdr sub-key) 3))
503
504 (defun epg-sub-key-length (sub-key)
505   "Return the length of SUB-KEY."
506   (unless (eq (car sub-key) 'epg-sub-key)
507     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
508   (aref (cdr sub-key) 4))
509
510 (defun epg-sub-key-id (sub-key)
511   "Return the ID of SUB-KEY."
512   (unless (eq (car sub-key) 'epg-sub-key)
513     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
514   (aref (cdr sub-key) 5))
515
516 (defun epg-sub-key-creation-time (sub-key)
517   "Return the creation time of SUB-KEY."
518   (unless (eq (car sub-key) 'epg-sub-key)
519     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
520   (aref (cdr sub-key) 6))
521
522 (defun epg-sub-key-expiration-time (sub-key)
523   "Return the expiration time of SUB-KEY."
524   (unless (eq (car sub-key) 'epg-sub-key)
525     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
526   (aref (cdr sub-key) 7))
527
528 (defun epg-sub-key-fingerprint (sub-key)
529   "Return the fingerprint of SUB-KEY."
530   (unless (eq (car sub-key) 'epg-sub-key)
531     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
532   (aref (cdr sub-key) 8))
533
534 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
535   "Set the fingerprint of SUB-KEY.
536 This function is for internal use only."
537   (unless (eq (car sub-key) 'epg-sub-key)
538     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
539   (aset (cdr sub-key) 8 fingerprint))
540
541 (defun epg-make-user-id (validity string)
542   "Return a user ID object."
543   (cons 'epg-user-id (vector validity string nil)))
544
545 (defun epg-user-id-validity (user-id)
546   "Return the validity of USER-ID."
547   (unless (eq (car user-id) 'epg-user-id)
548     (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
549   (aref (cdr user-id) 0))
550
551 (defun epg-user-id-string (user-id)
552   "Return the name of USER-ID."
553   (unless (eq (car user-id) 'epg-user-id)
554     (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
555   (aref (cdr user-id) 1))
556
557 (defun epg-user-id-signature-list (user-id)
558   "Return the signature list of USER-ID."
559   (unless (eq (car user-id) 'epg-user-id)
560     (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
561   (aref (cdr user-id) 2))
562
563 (defun epg-user-id-set-signature-list (user-id signature-list)
564   "Set the signature list of USER-ID."
565   (unless (eq (car user-id) 'epg-user-id)
566     (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
567   (aset (cdr user-id) 2 signature-list))
568
569 (defun epg-context-result-for (context name)
570   "Return the result of CONTEXT associated with NAME."
571   (cdr (assq name (epg-context-result context))))
572
573 (defun epg-context-set-result-for (context name value)
574   "Set the result of CONTEXT associated with NAME to VALUE."
575   (let* ((result (epg-context-result context))
576          (entry (assq name result)))
577     (if entry
578         (setcdr entry value)
579       (epg-context-set-result context (cons (cons name value) result)))))
580
581 (defun epg-signature-to-string (signature)
582   "Convert SIGNATURE to a human readable string."
583   (let ((user-id (cdr (assoc (epg-signature-key-id signature)
584                              epg-user-id-alist))))
585     (concat
586      (cond ((eq (epg-signature-status signature) 'good)
587             "Good signature from ")
588            ((eq (epg-signature-status signature) 'bad)
589             "Bad signature from ")
590            ((eq (epg-signature-status signature) 'expired)
591             "Expired signature from ")
592            ((eq (epg-signature-status signature) 'expired-key)
593             "Signature made by expired key ")
594            ((eq (epg-signature-status signature) 'revoked-key)
595             "Signature made by revoked key ")
596            ((eq (epg-signature-status signature) 'no-pubkey)
597             "No public key for "))
598      (epg-signature-key-id signature)
599      " "
600      (if user-id
601          (concat (if (stringp user-id)
602                      user-id
603                    (epg-decode-dn user-id))
604                  " ")
605        "")
606      (if (epg-signature-validity signature)
607          (format "(trust %s)"  (epg-signature-validity signature))
608        ""))))
609
610 (defun epg-verify-result-to-string (verify-result)
611   "Convert VERIFY-RESULT to a human readable string."
612   (mapconcat #'epg-signature-to-string verify-result "\n"))
613
614 (defun epg-start (context args)
615   "Start `epg-gpg-program' in a subprocess with given ARGS."
616   (let* ((args (append (list "--no-tty"
617                              "--status-fd" "1"
618                              "--yes")
619                        (unless (eq (epg-context-protocol context) 'CMS)
620                          (list "--command-fd" "0"))
621                        (if (epg-context-armor context) '("--armor"))
622                        (if (epg-context-textmode context) '("--textmode"))
623                        (if (epg-context-output-file context)
624                            (list "--output" (epg-context-output-file context)))
625                        args))
626          (coding-system-for-write 'binary)
627          process-connection-type
628          (orig-mode (default-file-modes))
629          (buffer (generate-new-buffer " *epg*"))
630          process)
631     (if epg-debug
632         (save-excursion
633           (unless epg-debug-buffer
634             (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
635           (set-buffer epg-debug-buffer)
636           (goto-char (point-max))
637           (insert (format "%s %s\n"
638                           (if (eq (epg-context-protocol context) 'CMS)
639                               epg-gpgsm-program
640                            epg-gpg-program)
641                           (mapconcat #'identity args " ")))))
642     (with-current-buffer buffer
643       (make-local-variable 'epg-read-point)
644       (setq epg-read-point (point-min))
645       (make-local-variable 'epg-pending-status-list)
646       (setq epg-pending-status-list nil)
647       (make-local-variable 'epg-key-id)
648       (setq epg-key-id nil)
649       (make-local-variable 'epg-context)
650       (setq epg-context context))
651     (unwind-protect
652         (progn
653           (set-default-file-modes 448)
654           (setq process
655                 (apply #'start-process "epg" buffer
656                        (if (eq (epg-context-protocol context) 'CMS)
657                            epg-gpgsm-program
658                          epg-gpg-program)
659                        args)))
660       (set-default-file-modes orig-mode))
661     (set-process-filter process #'epg-process-filter)
662     (epg-context-set-process context process)))
663
664 (defun epg-process-filter (process input)
665   (if epg-debug
666       (save-excursion
667         (unless epg-debug-buffer
668           (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
669         (set-buffer epg-debug-buffer)
670         (goto-char (point-max))
671         (insert input)))
672   (if (buffer-live-p (process-buffer process))
673       (save-excursion
674         (set-buffer (process-buffer process))
675         (goto-char (point-max))
676         (insert input)
677         (goto-char epg-read-point)
678         (beginning-of-line)
679         (while (looking-at ".*\n")      ;the input line finished
680           (save-excursion
681             (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
682                 (let* ((status (match-string 1))
683                        (string (match-string 2))
684                        (symbol (intern-soft (concat "epg-status-" status))))
685                   (if (member status epg-pending-status-list)
686                       (setq epg-pending-status-list nil))
687                   (if (and symbol
688                            (fboundp symbol))
689                       (funcall symbol process string)))))
690           (forward-line))
691         (setq epg-read-point (point)))))
692
693 (defun epg-read-output (context)
694   "Read the output file CONTEXT and return the content as a string."
695   (with-temp-buffer
696     (if (fboundp 'set-buffer-multibyte)
697         (set-buffer-multibyte nil))
698     (if (file-exists-p (epg-context-output-file context))
699         (let ((coding-system-for-read (if (epg-context-textmode context)
700                                           'raw-text
701                                         'binary)))
702           (insert-file-contents (epg-context-output-file context))
703           (buffer-string)))))
704
705 (defun epg-wait-for-status (context status-list)
706   "Wait until one of elements in STATUS-LIST arrives."
707   (with-current-buffer (process-buffer (epg-context-process context))
708     (setq epg-pending-status-list status-list)
709     (while (and (eq (process-status (epg-context-process context)) 'run)
710                 epg-pending-status-list)
711       (accept-process-output (epg-context-process context) 0 1))))
712
713 (defun epg-wait-for-completion (context)
714   "Wait until the `epg-gpg-program' process completes."
715   (while (eq (process-status (epg-context-process context)) 'run)
716     (accept-process-output (epg-context-process context) 0 1)))
717
718 (defun epg-flush (context)
719   "Flush the input to the `epg-gpg-program' process."
720   (if (eq (process-status (epg-context-process context)) 'run)
721       (process-send-eof (epg-context-process context))))
722
723 (defun epg-reset (context)
724   "Reset the CONTEXT."
725   (if (and (epg-context-process context)
726            (buffer-live-p (process-buffer (epg-context-process context))))
727       (kill-buffer (process-buffer (epg-context-process context))))
728   (epg-context-set-process context nil))
729
730 (defun epg-delete-output-file (context)
731   "Delete the output file of CONTEXT."
732   (if (and (epg-context-output-file context)
733            (file-exists-p (epg-context-output-file context)))
734       (delete-file (epg-context-output-file context))))
735
736 (defun epg-status-USERID_HINT (process string)
737   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
738       (let* ((key-id (match-string 1 string))
739              (user-id (match-string 2 string))
740              (entry (assoc key-id epg-user-id-alist)))
741         (if entry
742             (setcdr entry user-id)
743           (setq epg-user-id-alist (cons (cons key-id user-id)
744                                         epg-user-id-alist))))))
745
746 (defun epg-status-NEED_PASSPHRASE (process string)
747   (if (string-match "\\`\\([^ ]+\\)" string)
748       (setq epg-key-id (match-string 1 string))))
749
750 (defun epg-status-NEED_PASSPHRASE_SYM (process string)
751   (setq epg-key-id 'SYM))
752
753 (defun epg-status-NEED_PASSPHRASE_PIN (process string)
754   (setq epg-key-id 'PIN))
755
756 (defun epg-status-GET_HIDDEN (process string)
757   (if (and epg-key-id
758            (string-match "\\`passphrase\\." string))
759       (let (inhibit-quit
760             passphrase
761             passphrase-with-new-line)
762         (unwind-protect
763             (condition-case nil
764                 (progn
765                   (setq passphrase
766                         (funcall
767                          (if (consp (epg-context-passphrase-callback
768                                      epg-context))
769                              (car (epg-context-passphrase-callback
770                                    epg-context))
771                            (epg-context-passphrase-callback epg-context))
772                          epg-context
773                          epg-key-id
774                          (if (consp (epg-context-passphrase-callback
775                                      epg-context))
776                              (cdr (epg-context-passphrase-callback
777                                    epg-context)))))
778                   (when passphrase
779                     (setq passphrase-with-new-line (concat passphrase "\n"))
780                     (fillarray passphrase 0)
781                     (setq passphrase nil)
782                     (process-send-string process passphrase-with-new-line)))
783               (quit
784                (epg-context-set-result-for
785                 epg-context 'error
786                 (cons 'quit
787                       (epg-context-result-for epg-context 'error)))
788                (delete-process process)))
789           (if passphrase
790               (fillarray passphrase 0))
791           (if passphrase-with-new-line
792               (fillarray passphrase-with-new-line 0))))))
793
794 (defun epg-status-GET_BOOL (process string)
795   (let ((entry (assoc string epg-prompt-alist))
796         inhibit-quit)
797     (condition-case nil
798       (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
799           (process-send-string process "y\n")
800         (process-send-string process "n\n"))
801       (quit
802        (epg-context-set-result-for
803         epg-context 'error
804         (cons 'quit
805               (epg-context-result-for epg-context 'error)))
806        (delete-process process)))))
807
808 (defun epg-status-GET_LINE (process string)
809   (let ((entry (assoc string epg-prompt-alist))
810         inhibit-quit)
811     (condition-case nil
812         (process-send-string
813          process
814          (concat (read-string (if entry (cdr entry) (concat string ": ")))
815                  "\n"))
816       (quit
817        (epg-context-set-result-for
818         epg-context 'error
819         (cons 'quit
820               (epg-context-result-for epg-context 'error)))
821        (delete-process process)))))
822
823 (defun epg-signature-status-internal (status string)
824   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
825       (let* ((key-id (match-string 1 string))
826              (user-id (match-string 2 string))
827              (entry (assoc key-id epg-user-id-alist)))
828         (epg-context-set-result-for
829          epg-context
830          'verify
831          (cons (epg-make-signature status key-id)
832                (epg-context-result-for epg-context 'verify)))
833         (if (eq (epg-context-protocol epg-context) 'CMS)
834             (condition-case nil
835                 (setq user-id (epg-dn-from-string user-id))
836               (error)))
837         (if entry
838             (setcdr entry user-id)
839           (setq epg-user-id-alist
840                 (cons (cons key-id user-id) epg-user-id-alist))))
841     (epg-context-set-result-for
842      epg-context
843      'verify
844      (cons (epg-make-signature status)
845            (epg-context-result-for epg-context 'verify)))))
846
847 (defun epg-status-GOODSIG (process string)
848   (epg-signature-status-internal 'good string))
849
850 (defun epg-status-EXPSIG (process string)
851   (epg-signature-status-internal 'expired string))
852
853 (defun epg-status-EXPKEYSIG (process string)
854   (epg-signature-status-internal 'expired-key string))
855
856 (defun epg-status-REVKEYSIG (process string)
857   (epg-signature-status-internal 'revoked-key string))
858
859 (defun epg-status-BADSIG (process string)
860   (epg-signature-status-internal 'bad string))
861
862 (defun epg-status-NO_PUBKEY (process string)
863   (epg-context-set-result-for
864      epg-context
865      'verify
866      (cons (epg-make-signature 'no-pubkey string)
867            (epg-context-result-for epg-context 'verify))))
868
869 (defun epg-status-ERRSIG (process string)
870   (let ((signatures (car (epg-context-result-for epg-context 'verify))))
871     (unless signatures
872       (setq signatures (list (epg-make-signature 'error)))
873       (epg-context-set-result-for epg-context 'verify signatures))
874     (when (and (not (eq (epg-signature-status (car signatures)) 'good))
875                (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
876 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
877                              string))
878       (epg-signature-set-key-id
879        (car signatures)
880        (match-string 1 string))
881       (epg-signature-set-pubkey-algorithm
882        (car signatures)
883        (string-to-number (match-string 2 string)))
884       (epg-signature-set-digest-algorithm
885        (car signatures)
886        (string-to-number (match-string 3 string)))
887 ;      (epg-signature-set-class
888 ;       (car signatures)
889 ;       (string-to-number (match-string 4 string) 16))
890       (epg-signature-set-creation-time
891        (car signatures)
892        (match-string 5 string)))))
893
894 (defun epg-status-VALIDSIG (process string)
895   (let ((signature (car (epg-context-result-for epg-context 'verify))))
896     (when (and signature
897                (eq (epg-signature-status signature) 'good)
898                (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
899 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
900 \\(.*\\)"
901                            string))
902       (epg-signature-set-fingerprint
903        signature
904        (match-string 1 string))
905       (epg-signature-set-creation-time
906        signature
907        (match-string 2 string))
908       (epg-signature-set-expiration-time
909        signature
910        (match-string 3 string))
911 ;      (epg-signature-set-version
912 ;       signature
913 ;       (string-to-number (match-string 4 string)))
914       (epg-signature-set-pubkey-algorithm
915        signature 
916        (string-to-number (match-string 5 string)))
917       (epg-signature-set-digest-algorithm
918        signature
919        (string-to-number (match-string 6 string)))
920 ;      (epg-signature-set-class
921 ;       signature
922 ;       (string-to-number (match-string 7 string) 16))
923       )))
924
925 (defun epg-status-TRUST_UNDEFINED (process string)
926   (let ((signature (car (epg-context-result-for epg-context 'verify))))
927     (if (and signature
928              (eq (epg-signature-status signature) 'good))
929         (epg-signature-set-validity signature 'undefined))))
930
931 (defun epg-status-TRUST_NEVER (process string)
932   (let ((signature (car (epg-context-result-for epg-context 'verify))))
933     (if (and signature
934              (eq (epg-signature-status signature) 'good))
935         (epg-signature-set-validity signature 'never))))
936
937 (defun epg-status-TRUST_MARGINAL (process string)
938   (let ((signature (car (epg-context-result-for epg-context 'verify))))
939     (if (and signature
940              (eq (epg-signature-status signature) 'marginal))
941         (epg-signature-set-validity signature 'marginal))))
942
943 (defun epg-status-TRUST_FULLY (process string)
944   (let ((signature (car (epg-context-result-for epg-context 'verify))))
945     (if (and signature
946              (eq (epg-signature-status signature) 'good))
947         (epg-signature-set-validity signature 'full))))
948
949 (defun epg-status-TRUST_ULTIMATE (process string)
950   (let ((signature (car (epg-context-result-for epg-context 'verify))))
951     (if (and signature
952              (eq (epg-signature-status signature) 'good))
953         (epg-signature-set-validity signature 'ultimate))))
954
955 (defun epg-status-PROGRESS (process string)
956   (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
957                     string)
958       (funcall (if (consp (epg-context-progress-callback epg-context))
959                    (car (epg-context-progress-callback epg-context))
960                  (epg-context-progress-callback epg-context))
961                epg-context
962                (match-string 1 string)
963                (match-string 2 string)
964                (string-to-number (match-string 3 string))
965                (string-to-number (match-string 4 string))
966                (if (consp (epg-context-progress-callback epg-context))
967                    (cdr (epg-context-progress-callback epg-context))))))
968
969 (defun epg-status-DECRYPTION_FAILED (process string)
970   (epg-context-set-result-for
971    epg-context 'error
972    (cons 'decryption-failed
973          (epg-context-result-for epg-context 'error))))
974
975 (defun epg-status-NODATA (process string)
976   (epg-context-set-result-for
977    epg-context 'error
978    (cons (cons 'no-data (string-to-number string))
979          (epg-context-result-for epg-context 'error))))
980
981 (defun epg-status-UNEXPECTED (process string)
982   (epg-context-set-result-for
983    epg-context 'error
984    (cons (cons 'unexpected (string-to-number string))
985          (epg-context-result-for epg-context 'error))))
986
987 (defun epg-status-KEYEXPIRED (process string)
988   (epg-context-set-result-for
989    epg-context 'error
990    (cons (cons 'key-expired string)
991          (epg-context-result-for epg-context 'error))))
992
993 (defun epg-status-KEYREVOKED (process string)
994   (epg-context-set-result-for
995    epg-context 'error
996    (cons 'key-revoked
997          (epg-context-result-for epg-context 'error))))
998
999 (defun epg-status-BADARMOR (process string)
1000   (epg-context-set-result-for
1001    epg-context 'error
1002    (cons 'bad-armor
1003          (epg-context-result-for epg-context 'error))))
1004
1005 (defun epg-status-INV_RECP (process string)
1006   (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1007       (epg-context-set-result-for
1008        epg-context 'error
1009        (cons (list 'invalid-recipient
1010                    (string-to-number (match-string 1 string))
1011                    (match-string 2 string))
1012              (epg-context-result-for epg-context 'error)))))
1013
1014 (defun epg-status-NO_RECP (process string)
1015   (epg-context-set-result-for
1016    epg-context 'error
1017    (cons 'no-recipients
1018          (epg-context-result-for epg-context 'error))))
1019
1020 (defun epg-status-DELETE_PROBLEM (process string)
1021   (if (string-match "\\`\\([0-9]+\\)" string)
1022       (epg-context-set-result-for
1023        epg-context 'error
1024        (cons (cons 'delete-problem (string-to-number (match-string 1 string)))
1025              (epg-context-result-for epg-context 'error)))))
1026
1027 (defun epg-status-SIG_CREATED (process string)
1028   (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1029 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1030       (epg-context-set-result-for
1031        epg-context 'sign
1032        (cons (list (cons 'type (string-to-char (match-string 1 string)))
1033                    (cons 'pubkey-algorithm
1034                          (string-to-number (match-string 2 string)))
1035                    (cons 'digest-algorithm
1036                          (string-to-number (match-string 3 string)))
1037                    (cons 'class (string-to-number (match-string 4 string) 16))
1038                    (cons 'creation-time (match-string 5 string))
1039                    (cons 'fingerprint (substring string (match-end 0))))
1040              (epg-context-result-for epg-context 'sign)))))
1041
1042 (defun epg-passphrase-callback-function (context key-id handback)
1043   (read-passwd
1044    (if (eq key-id 'SYM)
1045        "Passphrase for symmetric encryption: "
1046      (if (eq key-id 'PIN)
1047          "Passphrase for PIN: "
1048        (let ((entry (assoc key-id epg-user-id-alist)))
1049          (if entry
1050              (format "Passphrase for %s %s: " key-id (cdr entry))
1051            (format "Passphrase for %s: " key-id)))))))
1052
1053 (defun epg-progress-callback-function (context what char current total
1054                                                handback)
1055   (message "%s: %d%%/%d%%" what current total))
1056
1057 (defun epg-configuration ()
1058   "Return a list of internal configuration parameters of `epg-gpg-program'."
1059   (let (config type)
1060     (with-temp-buffer
1061       (apply #'call-process epg-gpg-program nil (list t nil) nil
1062              '("--with-colons" "--list-config"))
1063       (goto-char (point-min))
1064       (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
1065         (setq type (intern (match-string 1))
1066               config (cons (cons type
1067                                  (if (memq type
1068                                            '(pubkey cipher digest compress))
1069                                      (mapcar #'string-to-number
1070                                              (delete "" (split-string
1071                                                          (match-string 2)
1072                                                          ";")))
1073                                    (match-string 2)))
1074                            config))))
1075     config))
1076
1077 (defun epg-list-keys-1 (context name mode)
1078   (let ((args (append (list "--with-colons" "--no-greeting" "--batch"
1079                             "--with-fingerprint"
1080                             "--with-fingerprint"
1081                             (if (or (eq mode t) (eq mode 'secret))
1082                                 "--list-secret-keys"
1083                               (if mode
1084                                   "--list-sigs"
1085                                 "--list-keys")))
1086                       (unless (eq (epg-context-protocol context) 'CMS)
1087                         '("--fixed-list-mode"))
1088                       (if name (list name))))
1089         keys string field index)
1090     (with-temp-buffer
1091       (apply #'call-process
1092              (if (eq (epg-context-protocol context) 'CMS)
1093                  epg-gpgsm-program
1094                epg-gpg-program)
1095              nil (list t nil) nil args)
1096       (goto-char (point-min))
1097       (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1098         (setq keys (cons (make-vector 15 nil) keys)
1099               string (match-string 0)
1100               index 0
1101               field 0)
1102         (while (eq index
1103                    (string-match "\\([^:]+\\)?:" string index))
1104           (setq index (match-end 0))
1105           (aset (car keys) field (match-string 1 string))
1106           (setq field (1+ field))))
1107       (nreverse keys))))
1108
1109 (defun epg-make-sub-key-1 (line)
1110   (epg-make-sub-key
1111    (if (aref line 1)
1112        (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1113    (delq nil
1114          (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1115                  (aref line 11)))
1116    (member (aref line 0) '("sec" "ssb"))
1117    (string-to-number (aref line 3))
1118    (string-to-number (aref line 2))
1119    (aref line 4)
1120    (aref line 5)
1121    (aref line 6)))
1122
1123 (defun epg-list-keys (context &optional name mode)
1124   "Return a list of epg-key objects matched with NAME.
1125 If MODE is nil, only public keyring should be searched.
1126 If MODE is t or 'secret, only secret keyring should be searched. 
1127 Otherwise, only public keyring should be searched and the key
1128 signatures should be included."
1129   (let ((lines (epg-list-keys-1 context name mode))
1130         keys cert key-id user-id-string entry)
1131     (while lines
1132       (cond
1133        ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1134         (when (car keys)
1135           (epg-key-set-sub-key-list
1136            (car keys)
1137            (nreverse (epg-key-sub-key-list (car keys))))
1138           (epg-key-set-user-id-list
1139            (car keys)
1140            (nreverse (epg-key-user-id-list (car keys))))
1141           (setq key-id
1142                 (epg-sub-key-id (car (epg-key-sub-key-list (car keys))))
1143                 user-id-string
1144                 (epg-user-id-string (car (epg-key-user-id-list (car keys))))
1145                 entry (assoc key-id epg-user-id-alist))
1146           (if entry
1147               (setcdr entry user-id-string)
1148             (setq epg-user-id-alist (cons (cons key-id user-id-string)
1149                                           epg-user-id-alist))))
1150         (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1151               keys (cons (epg-make-key
1152                           (if (aref (car lines) 8)
1153                               (cdr (assq (string-to-char (aref (car lines) 8))
1154                                          epg-key-validity-alist))))
1155                          keys))
1156         (epg-key-set-sub-key-list
1157          (car keys)
1158          (cons (epg-make-sub-key-1 (car lines))
1159                (epg-key-sub-key-list (car keys)))))
1160        ((member (aref (car lines) 0) '("sub" "ssb"))
1161         (epg-key-set-sub-key-list
1162          (car keys)
1163          (cons (epg-make-sub-key-1 (car lines))
1164                (epg-key-sub-key-list (car keys)))))
1165        ((equal (aref (car lines) 0) "uid")
1166         (epg-key-set-user-id-list
1167          (car keys)
1168          (cons (epg-make-user-id
1169                 (if (aref (car lines) 1)
1170                     (cdr (assq (string-to-char (aref (car lines) 1))
1171                                epg-key-validity-alist)))
1172                 (if cert
1173                     (condition-case nil
1174                         (epg-dn-from-string (aref (car lines) 9))
1175                       (error (aref (car lines) 9)))
1176                   (aref (car lines) 9)))
1177                (epg-key-user-id-list (car keys)))))
1178        ((equal (aref (car lines) 0) "fpr")
1179         (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1180                                      (aref (car lines) 9))))
1181       (setq lines (cdr lines)))
1182     (when (car keys)
1183       (epg-key-set-sub-key-list
1184        (car keys)
1185        (nreverse (epg-key-sub-key-list (car keys))))
1186       (epg-key-set-user-id-list
1187        (car keys)
1188        (nreverse (epg-key-user-id-list (car keys))))
1189       (setq key-id
1190                 (epg-sub-key-id (car (epg-key-sub-key-list (car keys))))
1191                 user-id-string
1192                 (epg-user-id-string (car (epg-key-user-id-list (car keys))))
1193                 entry (assoc key-id epg-user-id-alist))
1194           (if entry
1195               (setcdr entry user-id-string)
1196             (setq epg-user-id-alist (cons (cons key-id user-id-string)
1197                                           epg-user-id-alist))))
1198     (nreverse keys)))
1199
1200 (if (fboundp 'make-temp-file)
1201     (defalias 'epg-make-temp-file 'make-temp-file)
1202   ;; stolen from poe.el.
1203   (defun epg-make-temp-file (prefix)
1204     "Create a temporary file.
1205 The returned file name (created by appending some random characters at the end
1206 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1207 is guaranteed to point to a newly created empty file.
1208 You can then use `write-region' to write new data into the file."
1209     (let (tempdir tempfile)
1210       (setq prefix (expand-file-name prefix temporary-file-directory))
1211       (unwind-protect
1212           (let (file)
1213             ;; First, create a temporary directory.
1214             (while (condition-case ()
1215                        (progn
1216                          (setq tempdir (make-temp-name
1217                                         (concat
1218                                          (file-name-directory prefix)
1219                                          "DIR")))
1220                          ;; return nil or signal an error.
1221                          (make-directory tempdir))
1222                      ;; let's try again.
1223                      (file-already-exists t)))
1224             (set-file-modes tempdir 448)
1225             ;; Second, create a temporary file in the tempdir.
1226             ;; There *is* a race condition between `make-temp-name'
1227             ;; and `write-region', but we don't care it since we are
1228             ;; in a private directory now.
1229             (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1230             (write-region "" nil tempfile nil 'silent)
1231             (set-file-modes tempfile 384)
1232             ;; Finally, make a hard-link from the tempfile.
1233             (while (condition-case ()
1234                        (progn
1235                          (setq file (make-temp-name prefix))
1236                          ;; return nil or signal an error.
1237                          (add-name-to-file tempfile file))
1238                      ;; let's try again.
1239                      (file-already-exists t)))
1240             file)
1241         ;; Cleanup the tempfile.
1242         (and tempfile
1243              (file-exists-p tempfile)
1244              (delete-file tempfile))
1245         ;; Cleanup the tempdir.
1246         (and tempdir
1247              (file-directory-p tempdir)
1248              (delete-directory tempdir))))))
1249
1250 ;;;###autoload
1251 (defun epg-cancel (context)
1252   (if (eq (process-status (epg-context-process context)) 'run)
1253       (delete-process (epg-context-process context))))
1254   
1255 ;;;###autoload
1256 (defun epg-start-decrypt (context cipher)
1257   "Initiate a decrypt operation on CIPHER.
1258 CIPHER is a data object.
1259
1260 If you use this function, you will need to wait for the completion of
1261 `epg-gpg-program' by using `epg-wait-for-completion' and call
1262 `epg-reset' to clear a temporaly output file.
1263 If you are unsure, use synchronous version of this function
1264 `epg-decrypt-file' or `epg-decrypt-string' instead."
1265   (unless (epg-data-file cipher)
1266     (error "Not a file"))
1267   (epg-context-set-result context nil)
1268   (epg-start context (list "--decrypt" (epg-data-file cipher)))
1269   ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1270   (unless (eq (epg-context-protocol context) 'CMS)
1271     (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1272
1273 ;;;###autoload
1274 (defun epg-decrypt-file (context cipher plain)
1275   "Decrypt a file CIPHER and store the result to a file PLAIN.
1276 If PLAIN is nil, it returns the result as a string."
1277   (unwind-protect
1278       (progn
1279         (if plain
1280             (epg-context-set-output-file context plain)
1281           (epg-context-set-output-file context
1282                                        (epg-make-temp-file "epg-output")))
1283         (epg-start-decrypt context (epg-make-data-from-file cipher))
1284         (epg-wait-for-completion context)
1285         (if (epg-context-result-for context 'error)
1286             (error "Decrypt failed: %S"
1287                    (epg-context-result-for context 'error)))
1288         (unless plain
1289           (epg-read-output context)))
1290     (unless plain
1291       (epg-delete-output-file context))
1292     (epg-reset context)))
1293
1294 ;;;###autoload
1295 (defun epg-decrypt-string (context cipher)
1296   "Decrypt a string CIPHER and return the plain text."
1297   (let ((input-file (epg-make-temp-file "epg-input"))
1298         (coding-system-for-write 'binary))
1299     (unwind-protect
1300         (progn
1301           (write-region cipher nil input-file nil 'quiet)
1302           (epg-context-set-output-file context
1303                                        (epg-make-temp-file "epg-output"))
1304           (epg-start-decrypt context (epg-make-data-from-file input-file))
1305           (epg-flush context)
1306           (epg-wait-for-completion context)
1307           (if (epg-context-result-for context 'error)
1308               (error "Decrypt failed: %S"
1309                      (epg-context-result-for context 'error)))
1310           (epg-read-output context))
1311       (epg-delete-output-file context)
1312       (if (file-exists-p input-file)
1313           (delete-file input-file))
1314       (epg-reset context))))
1315
1316 ;;;###autoload
1317 (defun epg-start-verify (context signature &optional signed-text)
1318   "Initiate a verify operation on SIGNATURE.
1319 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1320
1321 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1322 For a normal or a clear text signature, SIGNED-TEXT should be nil.
1323
1324 If you use this function, you will need to wait for the completion of
1325 `epg-gpg-program' by using `epg-wait-for-completion' and call
1326 `epg-reset' to clear a temporaly output file.
1327 If you are unsure, use synchronous version of this function
1328 `epg-verify-file' or `epg-verify-string' instead."
1329   (epg-context-set-result context nil)
1330   (if signed-text
1331       ;; Detached signature.
1332       (if (epg-data-file signed-text)
1333           (epg-start context (list "--verify" (epg-data-file signature)
1334                                    (epg-data-file signed-text)))
1335         (epg-start context (list "--verify" (epg-data-file signature) "-"))
1336         (if (eq (process-status (epg-context-process context)) 'run)
1337             (process-send-string (epg-context-process context)
1338                                  (epg-data-string signed-text))))
1339     ;; Normal (or cleartext) signature.
1340     (if (epg-data-file signature)
1341         (epg-start context (list "--verify" (epg-data-file signature)))
1342       (epg-start context (list "--verify"))
1343       (if (eq (process-status (epg-context-process context)) 'run)
1344           (process-send-string (epg-context-process context)
1345                                (epg-data-string signature))))))
1346
1347 ;;;###autoload
1348 (defun epg-verify-file (context signature &optional signed-text plain)
1349   "Verify a file SIGNATURE.
1350 SIGNED-TEXT and PLAIN are also a file if they are specified.
1351
1352 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1353 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1354   (unwind-protect
1355       (progn
1356         (if plain
1357             (epg-context-set-output-file context plain)
1358           (epg-context-set-output-file context
1359                                        (epg-make-temp-file "epg-output")))
1360         (if signed-text
1361             (epg-start-verify context
1362                               (epg-make-data-from-file signature)
1363                               (epg-make-data-from-file signed-text))
1364           (epg-start-verify context
1365                             (epg-make-data-from-file signature)))
1366         (epg-wait-for-completion context)
1367 ;       (if (epg-context-result-for context 'error)
1368 ;           (error "Verify failed: %S"
1369 ;                  (epg-context-result-for context 'error)))
1370         (unless plain
1371           (epg-read-output context)))
1372     (unless plain
1373       (epg-delete-output-file context))
1374     (epg-reset context)))
1375
1376 ;;;###autoload
1377 (defun epg-verify-string (context signature &optional signed-text)
1378   "Verify a string SIGNATURE.
1379 SIGNED-TEXT is a string if it is specified.
1380
1381 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1382 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1383   (let ((coding-system-for-write 'binary)
1384         input-file)
1385     (unwind-protect
1386         (progn
1387           (epg-context-set-output-file context
1388                                        (epg-make-temp-file "epg-output"))
1389           (if signed-text
1390               (progn
1391                 (setq input-file (epg-make-temp-file "epg-signature"))
1392                 (write-region signature nil input-file nil 'quiet)
1393                 (epg-start-verify context
1394                                   (epg-make-data-from-file input-file)
1395                                   (epg-make-data-from-string signed-text)))
1396             (epg-start-verify context (epg-make-data-from-string signature)))
1397           (epg-flush context)
1398           (epg-wait-for-completion context)
1399 ;         (if (epg-context-result-for context 'error)
1400 ;             (error "Verify failed: %S"
1401 ;                    (epg-context-result-for context 'error)))
1402           (epg-read-output context))
1403       (epg-delete-output-file context)
1404       (if (and input-file
1405                (file-exists-p input-file))
1406           (delete-file input-file))
1407       (epg-reset context))))
1408
1409 ;;;###autoload
1410 (defun epg-start-sign (context plain &optional mode)
1411   "Initiate a sign operation on PLAIN.
1412 PLAIN is a data object.
1413
1414 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1415 If MODE is t or 'detached, it makes a detached signature.
1416 Otherwise, it makes a normal signature.
1417
1418 If you use this function, you will need to wait for the completion of
1419 `epg-gpg-program' by using `epg-wait-for-completion' and call
1420 `epg-reset' to clear a temporaly output file.
1421 If you are unsure, use synchronous version of this function
1422 `epg-sign-file' or `epg-sign-string' instead."
1423   (epg-context-set-result context nil)
1424   (epg-start context
1425              (append (list (if (eq mode 'clearsign)
1426                                "--clearsign"
1427                              (if (or (eq mode t) (eq mode 'detached))
1428                                  "--detach-sign"
1429                                "--sign")))
1430                      (apply #'nconc
1431                             (mapcar
1432                              (lambda (signer)
1433                                (list "-u"
1434                                      (epg-sub-key-id
1435                                       (car (epg-key-sub-key-list signer)))))
1436                              (epg-context-signers context)))
1437                      (if (epg-data-file plain)
1438                          (list (epg-data-file plain)))))
1439   ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1440   (unless (eq (epg-context-protocol context) 'CMS)
1441     (epg-wait-for-status context '("BEGIN_SIGNING")))
1442   (if (and (epg-data-string plain)
1443            (eq (process-status (epg-context-process context)) 'run))
1444       (process-send-string (epg-context-process context)
1445                            (epg-data-string plain))))
1446
1447 ;;;###autoload
1448 (defun epg-sign-file (context plain signature &optional mode)
1449   "Sign a file PLAIN and store the result to a file SIGNATURE.
1450 If SIGNATURE is nil, it returns the result as a string.
1451 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1452 If MODE is t or 'detached, it makes a detached signature.
1453 Otherwise, it makes a normal signature."
1454   (unwind-protect
1455       (progn
1456         (if signature
1457             (epg-context-set-output-file context signature)
1458           (epg-context-set-output-file context
1459                                        (epg-make-temp-file "epg-output")))
1460         (epg-start-sign context (epg-make-data-from-file plain) mode)
1461         (epg-wait-for-completion context)
1462         (if (epg-context-result-for context 'sign)
1463             (if (epg-context-result-for context 'error)
1464                 (message "Sign warning: %S"
1465                          (epg-context-result-for context 'error)))
1466           (if (epg-context-result-for context 'error)
1467               (error "Sign failed: %S"
1468                      (epg-context-result-for context 'error))
1469             (error "Sign failed")))
1470         (unless signature
1471           (epg-read-output context)))
1472     (unless signature
1473       (epg-delete-output-file context))
1474     (epg-reset context)))
1475
1476 ;;;###autoload
1477 (defun epg-sign-string (context plain &optional mode)
1478   "Sign a string PLAIN and return the output as string.
1479 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1480 If MODE is t or 'detached, it makes a detached signature.
1481 Otherwise, it makes a normal signature."
1482   (unwind-protect
1483       (progn
1484         (epg-context-set-output-file context
1485                                      (epg-make-temp-file "epg-output"))
1486         (epg-start-sign context (epg-make-data-from-string plain) mode)
1487         (epg-flush context)
1488         (epg-wait-for-completion context)
1489         (unless (epg-context-result-for context 'sign)
1490           (if (epg-context-result-for context 'error)
1491               (error "Sign failed: %S"
1492                      (epg-context-result-for context 'error))
1493             (error "Sign failed")))
1494         (epg-read-output context))
1495     (epg-delete-output-file context)
1496     (epg-reset context)))
1497
1498 ;;;###autoload
1499 (defun epg-start-encrypt (context plain recipients
1500                                   &optional sign always-trust)
1501   "Initiate an encrypt operation on PLAIN.
1502 PLAIN is a data object.
1503 If RECIPIENTS is nil, it performs symmetric encryption.
1504
1505 If you use this function, you will need to wait for the completion of
1506 `epg-gpg-program' by using `epg-wait-for-completion' and call
1507 `epg-reset' to clear a temporaly output file.
1508 If you are unsure, use synchronous version of this function
1509 `epg-encrypt-file' or `epg-encrypt-string' instead."
1510   (epg-context-set-result context nil)
1511   (epg-start context
1512              (append (if always-trust '("--always-trust"))
1513                      (if recipients '("--encrypt") '("--symmetric"))
1514                      (if sign
1515                          (cons "--sign"
1516                                (apply #'nconc
1517                                       (mapcar (lambda (signer)
1518                                                 (list "-u" signer))
1519                                               (epg-context-signers context)))))
1520                      (apply #'nconc
1521                             (mapcar
1522                              (lambda (recipient)
1523                                (list "-r"
1524                                      (epg-sub-key-id
1525                                       (car (epg-key-sub-key-list recipient)))))
1526                              recipients))
1527                      (if (epg-data-file plain)
1528                          (list (epg-data-file plain)))))
1529   ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1530   (unless (eq (epg-context-protocol context) 'CMS)
1531     (if sign
1532         (epg-wait-for-status context '("BEGIN_SIGNING"))
1533       (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
1534   (if (and (epg-data-string plain)
1535            (eq (process-status (epg-context-process context)) 'run))
1536       (process-send-string (epg-context-process context)
1537                            (epg-data-string plain))))
1538
1539 ;;;###autoload
1540 (defun epg-encrypt-file (context plain recipients
1541                                  cipher &optional sign always-trust)
1542   "Encrypt a file PLAIN and store the result to a file CIPHER.
1543 If CIPHER is nil, it returns the result as a string.
1544 If RECIPIENTS is nil, it performs symmetric encryption."
1545   (unwind-protect
1546       (progn
1547         (if cipher
1548             (epg-context-set-output-file context cipher)
1549           (epg-context-set-output-file context
1550                                        (epg-make-temp-file "epg-output")))
1551         (epg-start-encrypt context (epg-make-data-from-file plain)
1552                            recipients sign always-trust)
1553         (epg-wait-for-completion context)
1554         (if (and sign
1555                  (not (epg-context-result-for context 'sign)))
1556             (if (epg-context-result-for context 'error)
1557                 (error "Sign failed: %S"
1558                        (epg-context-result-for context 'error))
1559                 (error "Sign failed")))
1560         (if (epg-context-result-for context 'error)
1561             (error "Encrypt failed: %S"
1562                    (epg-context-result-for context 'error)))
1563         (unless cipher
1564           (epg-read-output context)))
1565     (unless cipher
1566       (epg-delete-output-file context))
1567     (epg-reset context)))
1568
1569 ;;;###autoload
1570 (defun epg-encrypt-string (context plain recipients
1571                                    &optional sign always-trust)
1572   "Encrypt a string PLAIN.
1573 If RECIPIENTS is nil, it performs symmetric encryption."
1574   (unwind-protect
1575       (progn
1576         (epg-context-set-output-file context
1577                                      (epg-make-temp-file "epg-output"))
1578         (epg-start-encrypt context (epg-make-data-from-string plain)
1579                            recipients sign always-trust)
1580         (epg-flush context)
1581         (epg-wait-for-completion context)
1582         (if (and sign
1583                  (not (epg-context-result-for context 'sign)))
1584             (if (epg-context-result-for context 'error)
1585                 (error "Sign failed: %S"
1586                        (epg-context-result-for context 'error))
1587               (error "Sign failed")))
1588         (if (epg-context-result-for context 'error)
1589             (error "Encrypt failed: %S"
1590                    (epg-context-result-for context 'error)))
1591         (epg-read-output context))
1592     (epg-delete-output-file context)
1593     (epg-reset context)))
1594
1595 ;;;###autoload
1596 (defun epg-start-export-keys (context keys)
1597   "Initiate an export keys operation.
1598
1599 If you use this function, you will need to wait for the completion of
1600 `epg-gpg-program' by using `epg-wait-for-completion' and call
1601 `epg-reset' to clear a temporaly output file.
1602 If you are unsure, use synchronous version of this function
1603 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1604   (epg-context-set-result context nil)
1605   (epg-start context (cons "--export"
1606                            (mapcar
1607                             (lambda (key)
1608                               (epg-sub-key-id
1609                                (car (epg-key-sub-key-list key))))
1610                             keys))))
1611
1612 ;;;###autoload
1613 (defun epg-export-keys-to-file (context keys file)
1614   "Extract public KEYS."
1615   (unwind-protect
1616       (progn
1617         (if keys
1618             (epg-context-set-output-file context file)
1619           (epg-context-set-output-file context
1620                                        (epg-make-temp-file "epg-output")))
1621         (epg-start-export-keys context keys)
1622         (epg-wait-for-completion context)
1623         (if (epg-context-result-for context 'error)
1624             (error "Export keys failed: %S"
1625                    (epg-context-result-for context 'error)))
1626         (unless file
1627           (epg-read-output context)))
1628     (unless file
1629       (epg-delete-output-file context))
1630     (epg-reset context)))
1631
1632 ;;;###autoload
1633 (defun epg-export-keys-to-string (context keys)
1634   "Extract public KEYS and return them as a string."
1635   (epg-export-keys-to-file context keys nil))
1636
1637 ;;;###autoload
1638 (defun epg-start-import-keys (context keys)
1639   "Initiate an import keys operation.
1640 KEYS is a data object.
1641
1642 If you use this function, you will need to wait for the completion of
1643 `epg-gpg-program' by using `epg-wait-for-completion' and call
1644 `epg-reset' to clear a temporaly output file.
1645 If you are unsure, use synchronous version of this function
1646 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1647   (epg-context-set-result context nil)
1648   (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
1649   (epg-start context (list "--import" (epg-data-file keys)))
1650   (if (and (epg-data-string keys)
1651            (eq (process-status (epg-context-process context)) 'run))
1652       (process-send-string (epg-context-process context)
1653                            (epg-data-string keys))))
1654   
1655 (defun epg-import-keys-1 (context keys)
1656   (unwind-protect
1657       (progn
1658         (epg-start-import-keys context keys)
1659         (if (epg-data-file keys)
1660             (epg-flush context))
1661         (epg-wait-for-completion context)
1662         (if (epg-context-result-for context 'error)
1663             (error "Import keys failed: %S"
1664                    (epg-context-result-for context 'error)))
1665         (epg-read-output context))
1666     (epg-reset context)))
1667
1668 ;;;###autoload
1669 (defun epg-import-keys-from-file (context keys)
1670   "Add keys from a file KEYS."
1671   (epg-import-keys-1 context (epg-make-data-from-file keys)))
1672
1673 ;;;###autoload
1674 (defun epg-import-keys-from-string (context keys)
1675   "Add keys from a string KEYS."
1676   (epg-import-keys-1 context (epg-make-data-from-string keys)))
1677
1678 ;;;###autoload
1679 (defun epg-start-delete-keys (context keys &optional allow-secret)
1680   "Initiate an delete keys operation.
1681
1682 If you use this function, you will need to wait for the completion of
1683 `epg-gpg-program' by using `epg-wait-for-completion' and call
1684 `epg-reset' to clear a temporaly output file.
1685 If you are unsure, use synchronous version of this function
1686 `epg-delete-keys' instead."
1687   (epg-context-set-result context nil)
1688   (epg-start context (cons (if allow-secret
1689                                "--delete-secret-key"
1690                              "--delete-key")
1691                            (mapcar
1692                             (lambda (key)
1693                               (epg-sub-key-id
1694                                (car (epg-key-sub-key-list key))))
1695                             keys))))
1696
1697 ;;;###autoload
1698 (defun epg-delete-keys (context keys &optional allow-secret)
1699   "Delete KEYS from the key ring."
1700   (unwind-protect
1701       (progn
1702         (epg-start-delete-keys context keys allow-secret)
1703         (epg-wait-for-completion context)
1704         (if (epg-context-result-for context 'error)
1705             (error "Delete keys failed: %S"
1706                    (epg-context-result-for context 'error))))
1707     (epg-reset context)))
1708
1709 ;;;###autoload
1710 (defun epg-start-sign-keys (context keys &optional local)
1711   "Initiate an sign keys operation.
1712
1713 If you use this function, you will need to wait for the completion of
1714 `epg-gpg-program' by using `epg-wait-for-completion' and call
1715 `epg-reset' to clear a temporaly output file.
1716 If you are unsure, use synchronous version of this function
1717 `epg-sign-keys' instead."
1718   (epg-context-set-result context nil)
1719   (epg-start context (cons (if local
1720                                "--lsign-key"
1721                              "--sign-key")
1722                            (mapcar
1723                             (lambda (key)
1724                               (epg-sub-key-id
1725                                (car (epg-key-sub-key-list key))))
1726                             keys))))
1727
1728 ;;;###autoload
1729 (defun epg-sign-keys (context keys &optional local)
1730   "Sign KEYS from the key ring."
1731   (unwind-protect
1732       (progn
1733         (epg-start-sign-keys context keys local)
1734         (epg-wait-for-completion context)
1735         (if (epg-context-result-for context 'error)
1736             (error "Sign keys failed: %S"
1737                    (epg-context-result-for context 'error))))
1738     (epg-reset context)))
1739
1740 (defun epg-decode-hexstring (string)
1741   (let ((index 0))
1742     (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
1743       (setq string (replace-match "\\x\\&" t nil string)
1744             index (+ index 4)))
1745     (car (read-from-string (concat "\"" string "\"")))))
1746
1747 (defun epg-decode-quotedstring (string)
1748   (let ((index 0))
1749     (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
1750 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
1751                          string index)
1752       (if (match-beginning 2)
1753           (setq string (replace-match "\\2" t nil string)
1754                 index (1+ index))
1755         (if (match-beginning 3)
1756             (setq string (replace-match "\\x\\3" t nil string)
1757                   index (+ index 4))
1758           (setq string (replace-match "\\\\\\\\\\4" t nil string)
1759                 index (+ index 3)))))
1760     (car (read-from-string (concat "\"" string "\"")))))
1761
1762 (defun epg-dn-from-string (string)
1763   "Parse STRING as LADPv3 Distinguished Names (RFC2253).
1764 The return value is an alist mapping from types to values."
1765   (let ((index 0)
1766         (length (length string))
1767         alist type value group)
1768     (while (< index length)
1769       (if (eq index (string-match "[ \t\n\r]*" string index))
1770           (setq index (match-end 0)))
1771       (if (eq index (string-match
1772                      "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
1773                      string index))
1774           (setq type (match-string 1 string)
1775                 index (match-end 0))
1776         (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
1777                                     string index))
1778             (setq type (match-string 1 string)
1779                   index (match-end 0))))
1780       (unless type
1781         (error "Invalid type"))
1782       (if (eq index (string-match
1783                      "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
1784                      string index))
1785           (setq index (match-end 0)
1786                 value (epg-decode-quotedstring (match-string 0 string)))
1787         (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
1788             (setq index (match-end 0)
1789                   value (epg-decode-hexstring (match-string 1 string)))
1790           (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
1791                                       string index))
1792               (setq index (match-end 0)
1793                     value (epg-decode-quotedstring (match-string 0 string))))))
1794       (if group
1795           (if (stringp (car (car alist)))
1796               (setcar alist (list (cons type value) (car alist)))
1797             (setcar alist (cons (cons type value) (car alist))))
1798         (if (consp (car (car alist)))
1799             (setcar alist (nreverse (car alist))))
1800         (setq alist (cons (cons type value) alist)
1801               type nil
1802               value nil))
1803       (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
1804           (setq index (match-end 0)
1805                 group (eq (aref string (match-beginning 1)) ?+))))
1806     (nreverse alist)))
1807
1808 (defun epg-decode-dn (alist)
1809   "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
1810 Type names are resolved using `epg-dn-type-alist'."
1811   (mapconcat
1812    (lambda (rdn)
1813      (if (stringp (car rdn))
1814          (let ((entry (assoc (car rdn) epg-dn-type-alist)))
1815            (if entry
1816                (format "%s=%s" (cdr entry) (cdr rdn))
1817              (format "%s=%s" (car rdn) (cdr rdn))))
1818        (concat "(" (epg-decode-dn rdn) ")")))
1819    alist
1820    ", "))
1821
1822 (provide 'epg)
1823
1824 ;;; epg.el ends here