Fixed signature formatting.
[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 name)
542   "Return a user ID object."
543   (cons 'epg-user-id (vector validity name 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-name (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      (if user-id
600          (concat (if (stringp user-id)
601                      user-id
602                    (epg-decode-dn user-id))
603                  " ")
604        "")
605      (if (epg-signature-validity signature)
606          (format "(trust %s)"  (epg-signature-validity signature))
607        ""))))
608
609 (defun epg-verify-result-to-string (verify-result)
610   "Convert VERIFY-RESULT to a human readable string."
611   (mapconcat #'epg-signature-to-string verify-result "\n"))
612
613 (defun epg-start (context args)
614   "Start `epg-gpg-program' in a subprocess with given ARGS."
615   (let* ((args (append (list "--no-tty"
616                              "--status-fd" "1"
617                              "--yes")
618                        (unless (eq (epg-context-protocol context) 'CMS)
619                          (list "--command-fd" "0"))
620                        (if (epg-context-armor context) '("--armor"))
621                        (if (epg-context-textmode context) '("--textmode"))
622                        (if (epg-context-output-file context)
623                            (list "--output" (epg-context-output-file context)))
624                        args))
625          (coding-system-for-write 'binary)
626          process-connection-type
627          (orig-mode (default-file-modes))
628          (buffer (generate-new-buffer " *epg*"))
629          process)
630     (if epg-debug
631         (save-excursion
632           (unless epg-debug-buffer
633             (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
634           (set-buffer epg-debug-buffer)
635           (goto-char (point-max))
636           (insert (format "%s %s\n"
637                           (if (eq (epg-context-protocol context) 'CMS)
638                               epg-gpgsm-program
639                            epg-gpg-program)
640                           (mapconcat #'identity args " ")))))
641     (with-current-buffer buffer
642       (make-local-variable 'epg-read-point)
643       (setq epg-read-point (point-min))
644       (make-local-variable 'epg-pending-status-list)
645       (setq epg-pending-status-list nil)
646       (make-local-variable 'epg-key-id)
647       (setq epg-key-id nil)
648       (make-local-variable 'epg-context)
649       (setq epg-context context))
650     (unwind-protect
651         (progn
652           (set-default-file-modes 448)
653           (setq process
654                 (apply #'start-process "epg" buffer
655                        (if (eq (epg-context-protocol context) 'CMS)
656                            epg-gpgsm-program
657                          epg-gpg-program)
658                        args)))
659       (set-default-file-modes orig-mode))
660     (set-process-filter process #'epg-process-filter)
661     (epg-context-set-process context process)))
662
663 (defun epg-process-filter (process input)
664   (if epg-debug
665       (save-excursion
666         (unless epg-debug-buffer
667           (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
668         (set-buffer epg-debug-buffer)
669         (goto-char (point-max))
670         (insert input)))
671   (if (buffer-live-p (process-buffer process))
672       (save-excursion
673         (set-buffer (process-buffer process))
674         (goto-char (point-max))
675         (insert input)
676         (goto-char epg-read-point)
677         (beginning-of-line)
678         (while (looking-at ".*\n")      ;the input line finished
679           (save-excursion
680             (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
681                 (let* ((status (match-string 1))
682                        (string (match-string 2))
683                        (symbol (intern-soft (concat "epg-status-" status))))
684                   (if (member status epg-pending-status-list)
685                       (setq epg-pending-status-list nil))
686                   (if (and symbol
687                            (fboundp symbol))
688                       (funcall symbol process string)))))
689           (forward-line))
690         (setq epg-read-point (point)))))
691
692 (defun epg-read-output (context)
693   "Read the output file CONTEXT and return the content as a string."
694   (with-temp-buffer
695     (if (fboundp 'set-buffer-multibyte)
696         (set-buffer-multibyte nil))
697     (if (file-exists-p (epg-context-output-file context))
698         (let ((coding-system-for-read (if (epg-context-textmode context)
699                                           'raw-text
700                                         'binary)))
701           (insert-file-contents (epg-context-output-file context))
702           (buffer-string)))))
703
704 (defun epg-wait-for-status (context status-list)
705   "Wait until one of elements in STATUS-LIST arrives."
706   (with-current-buffer (process-buffer (epg-context-process context))
707     (setq epg-pending-status-list status-list)
708     (while (and (eq (process-status (epg-context-process context)) 'run)
709                 epg-pending-status-list)
710       (accept-process-output (epg-context-process context) 0 1))))
711
712 (defun epg-wait-for-completion (context)
713   "Wait until the `epg-gpg-program' process completes."
714   (while (eq (process-status (epg-context-process context)) 'run)
715     (accept-process-output (epg-context-process context) 0 1)))
716
717 (defun epg-flush (context)
718   "Flush the input to the `epg-gpg-program' process."
719   (if (eq (process-status (epg-context-process context)) 'run)
720       (process-send-eof (epg-context-process context))))
721
722 (defun epg-reset (context)
723   "Reset the CONTEXT."
724   (if (and (epg-context-process context)
725            (buffer-live-p (process-buffer (epg-context-process context))))
726       (kill-buffer (process-buffer (epg-context-process context))))
727   (epg-context-set-process context nil))
728
729 (defun epg-delete-output-file (context)
730   "Delete the output file of CONTEXT."
731   (if (and (epg-context-output-file context)
732            (file-exists-p (epg-context-output-file context)))
733       (delete-file (epg-context-output-file context))))
734
735 (defun epg-status-USERID_HINT (process string)
736   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
737       (let* ((key-id (match-string 1 string))
738              (user-id (match-string 2 string))
739              (entry (assoc key-id epg-user-id-alist)))
740         (if entry
741             (setcdr entry user-id)
742           (setq epg-user-id-alist (cons (cons key-id user-id)
743                                         epg-user-id-alist))))))
744
745 (defun epg-status-NEED_PASSPHRASE (process string)
746   (if (string-match "\\`\\([^ ]+\\)" string)
747       (setq epg-key-id (match-string 1 string))))
748
749 (defun epg-status-NEED_PASSPHRASE_SYM (process string)
750   (setq epg-key-id 'SYM))
751
752 (defun epg-status-NEED_PASSPHRASE_PIN (process string)
753   (setq epg-key-id 'PIN))
754
755 (defun epg-status-GET_HIDDEN (process string)
756   (if (and epg-key-id
757            (string-match "\\`passphrase\\." string))
758       (let (inhibit-quit
759             passphrase
760             passphrase-with-new-line)
761         (unwind-protect
762             (condition-case nil
763                 (progn
764                   (setq passphrase
765                         (funcall
766                          (if (consp (epg-context-passphrase-callback
767                                      epg-context))
768                              (car (epg-context-passphrase-callback
769                                    epg-context))
770                            (epg-context-passphrase-callback epg-context))
771                          epg-context
772                          epg-key-id
773                          (if (consp (epg-context-passphrase-callback
774                                      epg-context))
775                              (cdr (epg-context-passphrase-callback
776                                    epg-context)))))
777                   (when passphrase
778                     (setq passphrase-with-new-line (concat passphrase "\n"))
779                     (fillarray passphrase 0)
780                     (setq passphrase nil)
781                     (process-send-string process passphrase-with-new-line)))
782               (quit
783                (epg-context-set-result-for
784                 epg-context 'error
785                 (cons 'quit
786                       (epg-context-result-for epg-context 'error)))
787                (delete-process process)))
788           (if passphrase
789               (fillarray passphrase 0))
790           (if passphrase-with-new-line
791               (fillarray passphrase-with-new-line 0))))))
792
793 (defun epg-status-GET_BOOL (process string)
794   (let ((entry (assoc string epg-prompt-alist))
795         inhibit-quit)
796     (condition-case nil
797       (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
798           (process-send-string process "y\n")
799         (process-send-string process "n\n"))
800       (quit
801        (epg-context-set-result-for
802         epg-context 'error
803         (cons 'quit
804               (epg-context-result-for epg-context 'error)))
805        (delete-process process)))))
806
807 (defun epg-status-GET_LINE (process string)
808   (let ((entry (assoc string epg-prompt-alist))
809         inhibit-quit)
810     (condition-case nil
811         (process-send-string
812          process
813          (concat (read-string (if entry (cdr entry) (concat string ": ")))
814                  "\n"))
815       (quit
816        (epg-context-set-result-for
817         epg-context 'error
818         (cons 'quit
819               (epg-context-result-for epg-context 'error)))
820        (delete-process process)))))
821
822 (defun epg-signature-status-internal (status string)
823   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
824       (let* ((key-id (match-string 1 string))
825              (user-id (match-string 2 string))
826              (entry (assoc key-id epg-user-id-alist)))
827         (epg-context-set-result-for
828          epg-context
829          'verify
830          (cons (epg-make-signature status key-id)
831                (epg-context-result-for epg-context 'verify)))
832         (if (eq (epg-context-protocol epg-context) 'CMS)
833             (condition-case nil
834                 (setq user-id (epg-dn-from-string user-id))
835               (error)))
836         (if entry
837             (setcdr entry user-id)
838           (setq epg-user-id-alist
839                 (cons (cons key-id user-id) epg-user-id-alist))))
840     (epg-context-set-result-for
841      epg-context
842      'verify
843      (cons (epg-make-signature status)
844            (epg-context-result-for epg-context 'verify)))))
845
846 (defun epg-status-GOODSIG (process string)
847   (epg-signature-status-internal 'good string))
848
849 (defun epg-status-EXPSIG (process string)
850   (epg-signature-status-internal 'expired string))
851
852 (defun epg-status-EXPKEYSIG (process string)
853   (epg-signature-status-internal 'expired-key string))
854
855 (defun epg-status-REVKEYSIG (process string)
856   (epg-signature-status-internal 'revoked-key string))
857
858 (defun epg-status-BADSIG (process string)
859   (epg-signature-status-internal 'bad string))
860
861 (defun epg-status-NO_PUBKEY (process string)
862   (epg-context-set-result-for
863      epg-context
864      'verify
865      (cons (epg-make-signature 'no-pubkey string)
866            (epg-context-result-for epg-context 'verify))))
867
868 (defun epg-status-ERRSIG (process string)
869   (let ((signatures (car (epg-context-result-for epg-context 'verify))))
870     (unless signatures
871       (setq signatures (list (epg-make-signature 'error)))
872       (epg-context-set-result-for epg-context 'verify signatures))
873     (when (and (not (eq (epg-signature-status (car signatures)) 'good))
874                (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
875 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
876                              string))
877       (epg-signature-set-key-id
878        (car signatures)
879        (match-string 1 string))
880       (epg-signature-set-pubkey-algorithm
881        (car signatures)
882        (string-to-number (match-string 2 string)))
883       (epg-signature-set-digest-algorithm
884        (car signatures)
885        (string-to-number (match-string 3 string)))
886 ;      (epg-signature-set-class
887 ;       (car signatures)
888 ;       (string-to-number (match-string 4 string) 16))
889       (epg-signature-set-creation-time
890        (car signatures)
891        (match-string 5 string)))))
892
893 (defun epg-status-VALIDSIG (process string)
894   (let ((signature (car (epg-context-result-for epg-context 'verify))))
895     (when (and signature
896                (eq (epg-signature-status signature) 'good)
897                (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
898 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
899 \\(.*\\)"
900                            string))
901       (epg-signature-set-fingerprint
902        signature
903        (match-string 1 string))
904       (epg-signature-set-creation-time
905        signature
906        (match-string 2 string))
907       (epg-signature-set-expiration-time
908        signature
909        (match-string 3 string))
910 ;      (epg-signature-set-version
911 ;       signature
912 ;       (string-to-number (match-string 4 string)))
913       (epg-signature-set-pubkey-algorithm
914        signature 
915        (string-to-number (match-string 5 string)))
916       (epg-signature-set-digest-algorithm
917        signature
918        (string-to-number (match-string 6 string)))
919 ;      (epg-signature-set-class
920 ;       signature
921 ;       (string-to-number (match-string 7 string) 16))
922       )))
923
924 (defun epg-status-TRUST_UNDEFINED (process string)
925   (let ((signature (car (epg-context-result-for epg-context 'verify))))
926     (if (and signature
927              (eq (epg-signature-status signature) 'good))
928         (epg-signature-set-validity signature 'undefined))))
929
930 (defun epg-status-TRUST_NEVER (process string)
931   (let ((signature (car (epg-context-result-for epg-context 'verify))))
932     (if (and signature
933              (eq (epg-signature-status signature) 'good))
934         (epg-signature-set-validity signature 'never))))
935
936 (defun epg-status-TRUST_MARGINAL (process string)
937   (let ((signature (car (epg-context-result-for epg-context 'verify))))
938     (if (and signature
939              (eq (epg-signature-status signature) 'marginal))
940         (epg-signature-set-validity signature 'marginal))))
941
942 (defun epg-status-TRUST_FULLY (process string)
943   (let ((signature (car (epg-context-result-for epg-context 'verify))))
944     (if (and signature
945              (eq (epg-signature-status signature) 'good))
946         (epg-signature-set-validity signature 'full))))
947
948 (defun epg-status-TRUST_ULTIMATE (process string)
949   (let ((signature (car (epg-context-result-for epg-context 'verify))))
950     (if (and signature
951              (eq (epg-signature-status signature) 'good))
952         (epg-signature-set-validity signature 'ultimate))))
953
954 (defun epg-status-PROGRESS (process string)
955   (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
956                     string)
957       (funcall (if (consp (epg-context-progress-callback epg-context))
958                    (car (epg-context-progress-callback epg-context))
959                  (epg-context-progress-callback epg-context))
960                epg-context
961                (match-string 1 string)
962                (match-string 2 string)
963                (string-to-number (match-string 3 string))
964                (string-to-number (match-string 4 string))
965                (if (consp (epg-context-progress-callback epg-context))
966                    (cdr (epg-context-progress-callback epg-context))))))
967
968 (defun epg-status-DECRYPTION_FAILED (process string)
969   (epg-context-set-result-for
970    epg-context 'error
971    (cons 'decryption-failed
972          (epg-context-result-for epg-context 'error))))
973
974 (defun epg-status-NODATA (process string)
975   (epg-context-set-result-for
976    epg-context 'error
977    (cons (cons 'no-data (string-to-number string))
978          (epg-context-result-for epg-context 'error))))
979
980 (defun epg-status-UNEXPECTED (process string)
981   (epg-context-set-result-for
982    epg-context 'error
983    (cons (cons 'unexpected (string-to-number string))
984          (epg-context-result-for epg-context 'error))))
985
986 (defun epg-status-KEYEXPIRED (process string)
987   (epg-context-set-result-for
988    epg-context 'error
989    (cons (cons 'key-expired string)
990          (epg-context-result-for epg-context 'error))))
991
992 (defun epg-status-KEYREVOKED (process string)
993   (epg-context-set-result-for
994    epg-context 'error
995    (cons 'key-revoked
996          (epg-context-result-for epg-context 'error))))
997
998 (defun epg-status-BADARMOR (process string)
999   (epg-context-set-result-for
1000    epg-context 'error
1001    (cons 'bad-armor
1002          (epg-context-result-for epg-context 'error))))
1003
1004 (defun epg-status-INV_RECP (process string)
1005   (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1006       (epg-context-set-result-for
1007        epg-context 'error
1008        (cons (list 'invalid-recipient
1009                    (string-to-number (match-string 1 string))
1010                    (match-string 2 string))
1011              (epg-context-result-for epg-context 'error)))))
1012
1013 (defun epg-status-NO_RECP (process string)
1014   (epg-context-set-result-for
1015    epg-context 'error
1016    (cons 'no-recipients
1017          (epg-context-result-for epg-context 'error))))
1018
1019 (defun epg-status-DELETE_PROBLEM (process string)
1020   (if (string-match "\\`\\([0-9]+\\)" string)
1021       (epg-context-set-result-for
1022        epg-context 'error
1023        (cons (cons 'delete-problem (string-to-number (match-string 1 string)))
1024              (epg-context-result-for epg-context 'error)))))
1025
1026 (defun epg-status-SIG_CREATED (process string)
1027   (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1028 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1029       (epg-context-set-result-for
1030        epg-context 'sign
1031        (cons (list (cons 'type (string-to-char (match-string 1 string)))
1032                    (cons 'pubkey-algorithm
1033                          (string-to-number (match-string 2 string)))
1034                    (cons 'digest-algorithm
1035                          (string-to-number (match-string 3 string)))
1036                    (cons 'class (string-to-number (match-string 4 string) 16))
1037                    (cons 'creation-time (match-string 5 string))
1038                    (cons 'fingerprint (substring string (match-end 0))))
1039              (epg-context-result-for epg-context 'sign)))))
1040
1041 (defun epg-passphrase-callback-function (context key-id handback)
1042   (read-passwd
1043    (if (eq key-id 'SYM)
1044        "Passphrase for symmetric encryption: "
1045      (if (eq key-id 'PIN)
1046          "Passphrase for PIN: "
1047        (let ((entry (assoc key-id epg-user-id-alist)))
1048          (if entry
1049              (format "Passphrase for %s %s: " key-id (cdr entry))
1050            (format "Passphrase for %s: " key-id)))))))
1051
1052 (defun epg-progress-callback-function (context what char current total
1053                                                handback)
1054   (message "%s: %d%%/%d%%" what current total))
1055
1056 (defun epg-configuration ()
1057   "Return a list of internal configuration parameters of `epg-gpg-program'."
1058   (let (config type)
1059     (with-temp-buffer
1060       (apply #'call-process epg-gpg-program nil (list t nil) nil
1061              '("--with-colons" "--list-config"))
1062       (goto-char (point-min))
1063       (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
1064         (setq type (intern (match-string 1))
1065               config (cons (cons type
1066                                  (if (memq type
1067                                            '(pubkey cipher digest compress))
1068                                      (mapcar #'string-to-number
1069                                              (delete "" (split-string
1070                                                          (match-string 2)
1071                                                          ";")))
1072                                    (match-string 2)))
1073                            config))))
1074     config))
1075
1076 (defun epg-list-keys-1 (context name mode)
1077   (let ((args (append (list "--with-colons" "--no-greeting" "--batch"
1078                             "--with-fingerprint"
1079                             "--with-fingerprint"
1080                             (if (or (eq mode t) (eq mode 'secret))
1081                                 "--list-secret-keys"
1082                               (if mode
1083                                   "--list-sigs"
1084                                 "--list-keys")))
1085                       (unless (eq (epg-context-protocol context) 'CMS)
1086                         '("--fixed-list-mode"))
1087                       (if name (list name))))
1088         keys string field index)
1089     (with-temp-buffer
1090       (apply #'call-process
1091              (if (eq (epg-context-protocol context) 'CMS)
1092                  epg-gpgsm-program
1093                epg-gpg-program)
1094              nil (list t nil) nil args)
1095       (goto-char (point-min))
1096       (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1097         (setq keys (cons (make-vector 15 nil) keys)
1098               string (match-string 0)
1099               index 0
1100               field 0)
1101         (while (eq index
1102                    (string-match "\\([^:]+\\)?:" string index))
1103           (setq index (match-end 0))
1104           (aset (car keys) field (match-string 1 string))
1105           (setq field (1+ field))))
1106       (nreverse keys))))
1107
1108 (defun epg-make-sub-key-1 (line)
1109   (epg-make-sub-key
1110    (if (aref line 1)
1111        (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1112    (delq nil
1113          (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1114                  (aref line 11)))
1115    (member (aref line 0) '("sec" "ssb"))
1116    (string-to-number (aref line 3))
1117    (string-to-number (aref line 2))
1118    (aref line 4)
1119    (aref line 5)
1120    (aref line 6)))
1121
1122 (defun epg-list-keys (context &optional name mode)
1123   "Return a list of epg-key objects matched with NAME.
1124 If MODE is nil, only public keyring should be searched.
1125 If MODE is t or 'secret, only secret keyring should be searched. 
1126 Otherwise, only public keyring should be searched and the key
1127 signatures should be included."
1128   (let ((lines (epg-list-keys-1 context name mode))
1129         keys cert)
1130     (while lines
1131       (cond
1132        ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1133         (when (car keys)
1134           (epg-key-set-sub-key-list
1135            (car keys)
1136            (nreverse (epg-key-sub-key-list (car keys))))
1137           (epg-key-set-user-id-list
1138            (car keys)
1139            (nreverse (epg-key-user-id-list (car keys)))))
1140         (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1141               keys (cons (epg-make-key
1142                           (if (aref (car lines) 8)
1143                               (cdr (assq (string-to-char (aref (car lines) 8))
1144                                          epg-key-validity-alist))))
1145                          keys))
1146         (epg-key-set-sub-key-list
1147          (car keys)
1148          (cons (epg-make-sub-key-1 (car lines))
1149                (epg-key-sub-key-list (car keys)))))
1150        ((member (aref (car lines) 0) '("sub" "ssb"))
1151         (epg-key-set-sub-key-list
1152          (car keys)
1153          (cons (epg-make-sub-key-1 (car lines))
1154                (epg-key-sub-key-list (car keys)))))
1155        ((equal (aref (car lines) 0) "uid")
1156         (epg-key-set-user-id-list
1157          (car keys)
1158          (cons (epg-make-user-id
1159                 (if (aref (car lines) 1)
1160                     (cdr (assq (string-to-char (aref (car lines) 1))
1161                                epg-key-validity-alist)))
1162                 (if cert
1163                     (condition-case nil
1164                         (epg-dn-from-string (aref (car lines) 9))
1165                       (error (aref (car lines) 9)))
1166                   (aref (car lines) 9)))
1167                (epg-key-user-id-list (car keys)))))
1168        ((equal (aref (car lines) 0) "fpr")
1169         (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1170                                      (aref (car lines) 9))))
1171       (setq lines (cdr lines)))
1172     (when (car keys)
1173       (epg-key-set-sub-key-list
1174        (car keys)
1175        (nreverse (epg-key-sub-key-list (car keys))))
1176       (epg-key-set-user-id-list
1177        (car keys)
1178        (nreverse (epg-key-user-id-list (car keys)))))
1179     (nreverse keys)))
1180
1181 (if (fboundp 'make-temp-file)
1182     (defalias 'epg-make-temp-file 'make-temp-file)
1183   ;; stolen from poe.el.
1184   (defun epg-make-temp-file (prefix)
1185     "Create a temporary file.
1186 The returned file name (created by appending some random characters at the end
1187 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1188 is guaranteed to point to a newly created empty file.
1189 You can then use `write-region' to write new data into the file."
1190     (let (tempdir tempfile)
1191       (unwind-protect
1192           (let (file)
1193             ;; First, create a temporary directory.
1194             (while (condition-case ()
1195                        (progn
1196                          (setq tempdir (make-temp-name
1197                                         (concat
1198                                          (file-name-directory prefix)
1199                                          "DIR")))
1200                          ;; return nil or signal an error.
1201                          (make-directory tempdir))
1202                      ;; let's try again.
1203                      (file-already-exists t)))
1204             (set-file-modes tempdir 448)
1205             ;; Second, create a temporary file in the tempdir.
1206             ;; There *is* a race condition between `make-temp-name'
1207             ;; and `write-region', but we don't care it since we are
1208             ;; in a private directory now.
1209             (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1210             (write-region "" nil tempfile nil 'silent)
1211             (set-file-modes tempfile 384)
1212             ;; Finally, make a hard-link from the tempfile.
1213             (while (condition-case ()
1214                        (progn
1215                          (setq file (make-temp-name prefix))
1216                          ;; return nil or signal an error.
1217                          (add-name-to-file tempfile file))
1218                      ;; let's try again.
1219                      (file-already-exists t)))
1220             file)
1221         ;; Cleanup the tempfile.
1222         (and tempfile
1223              (file-exists-p tempfile)
1224              (delete-file tempfile))
1225         ;; Cleanup the tempdir.
1226         (and tempdir
1227              (file-directory-p tempdir)
1228              (delete-directory tempdir))))))
1229
1230 ;;;###autoload
1231 (defun epg-cancel (context)
1232   (if (eq (process-status (epg-context-process context)) 'run)
1233       (delete-process (epg-context-process context))))
1234   
1235 ;;;###autoload
1236 (defun epg-start-decrypt (context cipher)
1237   "Initiate a decrypt operation on CIPHER.
1238 CIPHER is a data object.
1239
1240 If you use this function, you will need to wait for the completion of
1241 `epg-gpg-program' by using `epg-wait-for-completion' and call
1242 `epg-reset' to clear a temporaly output file.
1243 If you are unsure, use synchronous version of this function
1244 `epg-decrypt-file' or `epg-decrypt-string' instead."
1245   (unless (epg-data-file cipher)
1246     (error "Not a file"))
1247   (epg-context-set-result context nil)
1248   (epg-start context (list "--decrypt" (epg-data-file cipher)))
1249   ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1250   (unless (eq (epg-context-protocol context) 'CMS)
1251     (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1252
1253 ;;;###autoload
1254 (defun epg-decrypt-file (context cipher plain)
1255   "Decrypt a file CIPHER and store the result to a file PLAIN.
1256 If PLAIN is nil, it returns the result as a string."
1257   (unwind-protect
1258       (progn
1259         (if plain
1260             (epg-context-set-output-file context plain)
1261           (epg-context-set-output-file context
1262                                        (epg-make-temp-file "epg-output")))
1263         (epg-start-decrypt context (epg-make-data-from-file cipher))
1264         (epg-wait-for-completion context)
1265         (if (epg-context-result-for context 'error)
1266             (error "Decrypt failed: %S"
1267                    (epg-context-result-for context 'error)))
1268         (unless plain
1269           (epg-read-output context)))
1270     (unless plain
1271       (epg-delete-output-file context))
1272     (epg-reset context)))
1273
1274 ;;;###autoload
1275 (defun epg-decrypt-string (context cipher)
1276   "Decrypt a string CIPHER and return the plain text."
1277   (let ((input-file (epg-make-temp-file "epg-input"))
1278         (coding-system-for-write 'binary))
1279     (unwind-protect
1280         (progn
1281           (write-region cipher nil input-file nil 'quiet)
1282           (epg-context-set-output-file context
1283                                        (epg-make-temp-file "epg-output"))
1284           (epg-start-decrypt context (epg-make-data-from-file input-file))
1285           (epg-flush context)
1286           (epg-wait-for-completion context)
1287           (if (epg-context-result-for context 'error)
1288               (error "Decrypt failed: %S"
1289                      (epg-context-result-for context 'error)))
1290           (epg-read-output context))
1291       (epg-delete-output-file context)
1292       (if (file-exists-p input-file)
1293           (delete-file input-file))
1294       (epg-reset context))))
1295
1296 ;;;###autoload
1297 (defun epg-start-verify (context signature &optional signed-text)
1298   "Initiate a verify operation on SIGNATURE.
1299 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1300
1301 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1302 For a normal or a clear text signature, SIGNED-TEXT should be nil.
1303
1304 If you use this function, you will need to wait for the completion of
1305 `epg-gpg-program' by using `epg-wait-for-completion' and call
1306 `epg-reset' to clear a temporaly output file.
1307 If you are unsure, use synchronous version of this function
1308 `epg-verify-file' or `epg-verify-string' instead."
1309   (epg-context-set-result context nil)
1310   (if signed-text
1311       ;; Detached signature.
1312       (if (epg-data-file signed-text)
1313           (epg-start context (list "--verify" (epg-data-file signature)
1314                                    (epg-data-file signed-text)))
1315         (epg-start context (list "--verify" (epg-data-file signature) "-"))
1316         (if (eq (process-status (epg-context-process context)) 'run)
1317             (process-send-string (epg-context-process context)
1318                                  (epg-data-string signed-text))))
1319     ;; Normal (or cleartext) signature.
1320     (if (epg-data-file signature)
1321         (epg-start context (list "--verify" (epg-data-file signature)))
1322       (epg-start context (list "--verify"))
1323       (if (eq (process-status (epg-context-process context)) 'run)
1324           (process-send-string (epg-context-process context)
1325                                (epg-data-string signature))))))
1326
1327 ;;;###autoload
1328 (defun epg-verify-file (context signature &optional signed-text plain)
1329   "Verify a file SIGNATURE.
1330 SIGNED-TEXT and PLAIN are also a file if they are specified.
1331
1332 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1333 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1334   (unwind-protect
1335       (progn
1336         (if plain
1337             (epg-context-set-output-file context plain)
1338           (epg-context-set-output-file context
1339                                        (epg-make-temp-file "epg-output")))
1340         (if signed-text
1341             (epg-start-verify context
1342                               (epg-make-data-from-file signature)
1343                               (epg-make-data-from-file signed-text))
1344           (epg-start-verify context
1345                             (epg-make-data-from-file signature)))
1346         (epg-wait-for-completion context)
1347 ;       (if (epg-context-result-for context 'error)
1348 ;           (error "Verify failed: %S"
1349 ;                  (epg-context-result-for context 'error)))
1350         (unless plain
1351           (epg-read-output context)))
1352     (unless plain
1353       (epg-delete-output-file context))
1354     (epg-reset context)))
1355
1356 ;;;###autoload
1357 (defun epg-verify-string (context signature &optional signed-text)
1358   "Verify a string SIGNATURE.
1359 SIGNED-TEXT is a string if it is specified.
1360
1361 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1362 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1363   (let ((coding-system-for-write 'binary)
1364         input-file)
1365     (unwind-protect
1366         (progn
1367           (epg-context-set-output-file context
1368                                        (epg-make-temp-file "epg-output"))
1369           (if signed-text
1370               (progn
1371                 (setq input-file (epg-make-temp-file "epg-signature"))
1372                 (write-region signature nil input-file nil 'quiet)
1373                 (epg-start-verify context
1374                                   (epg-make-data-from-file input-file)
1375                                   (epg-make-data-from-string signed-text)))
1376             (epg-start-verify context (epg-make-data-from-string signature)))
1377           (epg-flush context)
1378           (epg-wait-for-completion context)
1379 ;         (if (epg-context-result-for context 'error)
1380 ;             (error "Verify failed: %S"
1381 ;                    (epg-context-result-for context 'error)))
1382           (epg-read-output context))
1383       (epg-delete-output-file context)
1384       (if (and input-file
1385                (file-exists-p input-file))
1386           (delete-file input-file))
1387       (epg-reset context))))
1388
1389 ;;;###autoload
1390 (defun epg-start-sign (context plain &optional mode)
1391   "Initiate a sign operation on PLAIN.
1392 PLAIN is a data object.
1393
1394 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1395 If MODE is t or 'detached, it makes a detached signature.
1396 Otherwise, it makes a normal signature.
1397
1398 If you use this function, you will need to wait for the completion of
1399 `epg-gpg-program' by using `epg-wait-for-completion' and call
1400 `epg-reset' to clear a temporaly output file.
1401 If you are unsure, use synchronous version of this function
1402 `epg-sign-file' or `epg-sign-string' instead."
1403   (epg-context-set-result context nil)
1404   (epg-start context
1405              (append (list (if (eq mode 'clearsign)
1406                                "--clearsign"
1407                              (if (or (eq mode t) (eq mode 'detached))
1408                                  "--detach-sign"
1409                                "--sign")))
1410                      (apply #'nconc
1411                             (mapcar
1412                              (lambda (signer)
1413                                (list "-u"
1414                                      (epg-sub-key-id
1415                                       (car (epg-key-sub-key-list signer)))))
1416                              (epg-context-signers context)))
1417                      (if (epg-data-file plain)
1418                          (list (epg-data-file plain)))))
1419   ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1420   (unless (eq (epg-context-protocol context) 'CMS)
1421     (epg-wait-for-status context '("BEGIN_SIGNING")))
1422   (if (and (epg-data-string plain)
1423            (eq (process-status (epg-context-process context)) 'run))
1424       (process-send-string (epg-context-process context)
1425                            (epg-data-string plain))))
1426
1427 ;;;###autoload
1428 (defun epg-sign-file (context plain signature &optional mode)
1429   "Sign a file PLAIN and store the result to a file SIGNATURE.
1430 If SIGNATURE is nil, it returns the result as a string.
1431 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1432 If MODE is t or 'detached, it makes a detached signature.
1433 Otherwise, it makes a normal signature."
1434   (unwind-protect
1435       (progn
1436         (if signature
1437             (epg-context-set-output-file context signature)
1438           (epg-context-set-output-file context
1439                                        (epg-make-temp-file "epg-output")))
1440         (epg-start-sign context (epg-make-data-from-file plain) mode)
1441         (epg-wait-for-completion context)
1442         (if (epg-context-result-for context 'sign)
1443             (if (epg-context-result-for context 'error)
1444                 (message "Sign warning: %S"
1445                          (epg-context-result-for context 'error)))
1446           (if (epg-context-result-for context 'error)
1447               (error "Sign failed: %S"
1448                      (epg-context-result-for context 'error))
1449             (error "Sign failed")))
1450         (unless signature
1451           (epg-read-output context)))
1452     (unless signature
1453       (epg-delete-output-file context))
1454     (epg-reset context)))
1455
1456 ;;;###autoload
1457 (defun epg-sign-string (context plain &optional mode)
1458   "Sign a string PLAIN and return the output as string.
1459 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1460 If MODE is t or 'detached, it makes a detached signature.
1461 Otherwise, it makes a normal signature."
1462   (unwind-protect
1463       (progn
1464         (epg-context-set-output-file context
1465                                      (epg-make-temp-file "epg-output"))
1466         (epg-start-sign context (epg-make-data-from-string plain) mode)
1467         (epg-flush context)
1468         (epg-wait-for-completion context)
1469         (unless (epg-context-result-for context 'sign)
1470           (if (epg-context-result-for context 'error)
1471               (error "Sign failed: %S"
1472                      (epg-context-result-for context 'error))
1473             (error "Sign failed")))
1474         (epg-read-output context))
1475     (epg-delete-output-file context)
1476     (epg-reset context)))
1477
1478 ;;;###autoload
1479 (defun epg-start-encrypt (context plain recipients
1480                                   &optional sign always-trust)
1481   "Initiate an encrypt operation on PLAIN.
1482 PLAIN is a data object.
1483 If RECIPIENTS is nil, it performs symmetric encryption.
1484
1485 If you use this function, you will need to wait for the completion of
1486 `epg-gpg-program' by using `epg-wait-for-completion' and call
1487 `epg-reset' to clear a temporaly output file.
1488 If you are unsure, use synchronous version of this function
1489 `epg-encrypt-file' or `epg-encrypt-string' instead."
1490   (epg-context-set-result context nil)
1491   (epg-start context
1492              (append (if always-trust '("--always-trust"))
1493                      (if recipients '("--encrypt") '("--symmetric"))
1494                      (if sign
1495                          (cons "--sign"
1496                                (apply #'nconc
1497                                       (mapcar (lambda (signer)
1498                                                 (list "-u" signer))
1499                                               (epg-context-signers context)))))
1500                      (apply #'nconc
1501                             (mapcar
1502                              (lambda (recipient)
1503                                (list "-r"
1504                                      (epg-sub-key-id
1505                                       (car (epg-key-sub-key-list recipient)))))
1506                              recipients))
1507                      (if (epg-data-file plain)
1508                          (list (epg-data-file plain)))))
1509   ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1510   (unless (eq (epg-context-protocol context) 'CMS)
1511     (if sign
1512         (epg-wait-for-status context '("BEGIN_SIGNING"))
1513       (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
1514   (if (and (epg-data-string plain)
1515            (eq (process-status (epg-context-process context)) 'run))
1516       (process-send-string (epg-context-process context)
1517                            (epg-data-string plain))))
1518
1519 ;;;###autoload
1520 (defun epg-encrypt-file (context plain recipients
1521                                  cipher &optional sign always-trust)
1522   "Encrypt a file PLAIN and store the result to a file CIPHER.
1523 If CIPHER is nil, it returns the result as a string.
1524 If RECIPIENTS is nil, it performs symmetric encryption."
1525   (unwind-protect
1526       (progn
1527         (if cipher
1528             (epg-context-set-output-file context cipher)
1529           (epg-context-set-output-file context
1530                                        (epg-make-temp-file "epg-output")))
1531         (epg-start-encrypt context (epg-make-data-from-file plain)
1532                            recipients sign always-trust)
1533         (epg-wait-for-completion context)
1534         (if (and sign
1535                  (not (epg-context-result-for context 'sign)))
1536             (if (epg-context-result-for context 'error)
1537                 (error "Sign failed: %S"
1538                        (epg-context-result-for context 'error))
1539                 (error "Sign failed")))
1540         (if (epg-context-result-for context 'error)
1541             (error "Encrypt failed: %S"
1542                    (epg-context-result-for context 'error)))
1543         (unless cipher
1544           (epg-read-output context)))
1545     (unless cipher
1546       (epg-delete-output-file context))
1547     (epg-reset context)))
1548
1549 ;;;###autoload
1550 (defun epg-encrypt-string (context plain recipients
1551                                    &optional sign always-trust)
1552   "Encrypt a string PLAIN.
1553 If RECIPIENTS is nil, it performs symmetric encryption."
1554   (unwind-protect
1555       (progn
1556         (epg-context-set-output-file context
1557                                      (epg-make-temp-file "epg-output"))
1558         (epg-start-encrypt context (epg-make-data-from-string plain)
1559                            recipients sign always-trust)
1560         (epg-flush context)
1561         (epg-wait-for-completion context)
1562         (if (and sign
1563                  (not (epg-context-result-for context 'sign)))
1564             (if (epg-context-result-for context 'error)
1565                 (error "Sign failed: %S"
1566                        (epg-context-result-for context 'error))
1567               (error "Sign failed")))
1568         (if (epg-context-result-for context 'error)
1569             (error "Encrypt failed: %S"
1570                    (epg-context-result-for context 'error)))
1571         (epg-read-output context))
1572     (epg-delete-output-file context)
1573     (epg-reset context)))
1574
1575 ;;;###autoload
1576 (defun epg-start-export-keys (context keys)
1577   "Initiate an export keys operation.
1578
1579 If you use this function, you will need to wait for the completion of
1580 `epg-gpg-program' by using `epg-wait-for-completion' and call
1581 `epg-reset' to clear a temporaly output file.
1582 If you are unsure, use synchronous version of this function
1583 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1584   (epg-context-set-result context nil)
1585   (epg-start context (cons "--export"
1586                            (mapcar
1587                             (lambda (key)
1588                               (epg-sub-key-id
1589                                (car (epg-key-sub-key-list key))))
1590                             keys))))
1591
1592 ;;;###autoload
1593 (defun epg-export-keys-to-file (context keys file)
1594   "Extract public KEYS."
1595   (unwind-protect
1596       (progn
1597         (if keys
1598             (epg-context-set-output-file context file)
1599           (epg-context-set-output-file context
1600                                        (epg-make-temp-file "epg-output")))
1601         (epg-start-export-keys context keys)
1602         (epg-wait-for-completion context)
1603         (if (epg-context-result-for context 'error)
1604             (error "Export keys failed: %S"
1605                    (epg-context-result-for context 'error)))
1606         (unless file
1607           (epg-read-output context)))
1608     (unless file
1609       (epg-delete-output-file context))
1610     (epg-reset context)))
1611
1612 ;;;###autoload
1613 (defun epg-export-keys-to-string (context keys)
1614   "Extract public KEYS and return them as a string."
1615   (epg-export-keys-to-file context keys nil))
1616
1617 ;;;###autoload
1618 (defun epg-start-import-keys (context keys)
1619   "Initiate an import keys operation.
1620 KEYS is a data object.
1621
1622 If you use this function, you will need to wait for the completion of
1623 `epg-gpg-program' by using `epg-wait-for-completion' and call
1624 `epg-reset' to clear a temporaly output file.
1625 If you are unsure, use synchronous version of this function
1626 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1627   (epg-context-set-result context nil)
1628   (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
1629   (epg-start context (list "--import" (epg-data-file keys)))
1630   (if (and (epg-data-string keys)
1631            (eq (process-status (epg-context-process context)) 'run))
1632       (process-send-string (epg-context-process context)
1633                            (epg-data-string keys))))
1634   
1635 (defun epg-import-keys-1 (context keys)
1636   (unwind-protect
1637       (progn
1638         (epg-start-import-keys context keys)
1639         (if (epg-data-file keys)
1640             (epg-flush context))
1641         (epg-wait-for-completion context)
1642         (if (epg-context-result-for context 'error)
1643             (error "Import keys failed: %S"
1644                    (epg-context-result-for context 'error)))
1645         (epg-read-output context))
1646     (epg-reset context)))
1647
1648 ;;;###autoload
1649 (defun epg-import-keys-from-file (context keys)
1650   "Add keys from a file KEYS."
1651   (epg-import-keys-1 context (epg-make-data-from-file keys)))
1652
1653 ;;;###autoload
1654 (defun epg-import-keys-from-string (context keys)
1655   "Add keys from a string KEYS."
1656   (epg-import-keys-1 context (epg-make-data-from-string keys)))
1657
1658 ;;;###autoload
1659 (defun epg-start-delete-keys (context keys &optional allow-secret)
1660   "Initiate an delete keys operation.
1661
1662 If you use this function, you will need to wait for the completion of
1663 `epg-gpg-program' by using `epg-wait-for-completion' and call
1664 `epg-reset' to clear a temporaly output file.
1665 If you are unsure, use synchronous version of this function
1666 `epg-delete-keys' instead."
1667   (epg-context-set-result context nil)
1668   (epg-start context (cons (if allow-secret
1669                                "--delete-secret-key"
1670                              "--delete-key")
1671                            (mapcar
1672                             (lambda (key)
1673                               (epg-sub-key-id
1674                                (car (epg-key-sub-key-list key))))
1675                             keys))))
1676
1677 ;;;###autoload
1678 (defun epg-delete-keys (context keys &optional allow-secret)
1679   "Delete KEYS from the key ring."
1680   (unwind-protect
1681       (progn
1682         (epg-start-delete-keys context keys allow-secret)
1683         (epg-wait-for-completion context)
1684         (if (epg-context-result-for context 'error)
1685             (error "Delete keys failed: %S"
1686                    (epg-context-result-for context 'error))))
1687     (epg-reset context)))
1688
1689 ;;;###autoload
1690 (defun epg-start-sign-keys (context keys &optional local)
1691   "Initiate an sign keys operation.
1692
1693 If you use this function, you will need to wait for the completion of
1694 `epg-gpg-program' by using `epg-wait-for-completion' and call
1695 `epg-reset' to clear a temporaly output file.
1696 If you are unsure, use synchronous version of this function
1697 `epg-sign-keys' instead."
1698   (epg-context-set-result context nil)
1699   (epg-start context (cons (if local
1700                                "--lsign-key"
1701                              "--sign-key")
1702                            (mapcar
1703                             (lambda (key)
1704                               (epg-sub-key-id
1705                                (car (epg-key-sub-key-list key))))
1706                             keys))))
1707
1708 ;;;###autoload
1709 (defun epg-sign-keys (context keys &optional local)
1710   "Sign KEYS from the key ring."
1711   (unwind-protect
1712       (progn
1713         (epg-start-sign-keys context keys local)
1714         (epg-wait-for-completion context)
1715         (if (epg-context-result-for context 'error)
1716             (error "Sign keys failed: %S"
1717                    (epg-context-result-for context 'error))))
1718     (epg-reset context)))
1719
1720 (defun epg-decode-hexstring (string)
1721   (let ((index 0))
1722     (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
1723       (setq string (replace-match "\\x\\&" t nil string)
1724             index (+ index 4)))
1725     (car (read-from-string (concat "\"" string "\"")))))
1726
1727 (defun epg-decode-quotedstring (string)
1728   (let ((index 0))
1729     (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
1730 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
1731                          string index)
1732       (if (match-beginning 2)
1733           (setq string (replace-match "\\2" t nil string)
1734                 index (1+ index))
1735         (if (match-beginning 3)
1736             (setq string (replace-match "\\x\\3" t nil string)
1737                   index (+ index 4))
1738           (setq string (replace-match "\\\\\\\\\\4" t nil string)
1739                 index (+ index 3)))))
1740     (car (read-from-string (concat "\"" string "\"")))))
1741
1742 (defun epg-dn-from-string (string)
1743   "Parse STRING as LADPv3 Distinguished Names (RFC2253).
1744 The return value is an alist mapping from types to values."
1745   (let ((index 0)
1746         (length (length string))
1747         alist type value group)
1748     (while (< index length)
1749       (if (eq index (string-match "[ \t\n\r]*" string index))
1750           (setq index (match-end 0)))
1751       (if (eq index (string-match
1752                      "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
1753                      string index))
1754           (setq type (match-string 1 string)
1755                 index (match-end 0))
1756         (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
1757                                     string index))
1758             (setq type (match-string 1 string)
1759                   index (match-end 0))))
1760       (unless type
1761         (error "Invalid type"))
1762       (if (eq index (string-match
1763                      "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
1764                      string index))
1765           (setq index (match-end 0)
1766                 value (epg-decode-quotedstring (match-string 0 string)))
1767         (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
1768             (setq index (match-end 0)
1769                   value (epg-decode-hexstring (match-string 1 string)))
1770           (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
1771                                       string index))
1772               (setq index (match-end 0)
1773                     value (epg-decode-quotedstring (match-string 0 string))))))
1774       (if group
1775           (if (stringp (car (car alist)))
1776               (setcar alist (list (cons type value) (car alist)))
1777             (setcar alist (cons (cons type value) (car alist))))
1778         (if (consp (car (car alist)))
1779             (setcar alist (nreverse (car alist))))
1780         (setq alist (cons (cons type value) alist)
1781               type nil
1782               value nil))
1783       (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
1784           (setq index (match-end 0)
1785                 group (eq (aref string (match-beginning 1)) ?+))))
1786     (nreverse alist)))
1787
1788 (defun epg-decode-dn (alist)
1789   "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
1790 Type names are resolved using `epg-dn-type-alist'."
1791   (mapconcat
1792    (lambda (rdn)
1793      (if (stringp (car rdn))
1794          (let ((entry (assoc (car rdn) epg-dn-type-alist)))
1795            (if entry
1796                (format "%s=%s" (cdr entry) (cdr rdn))
1797              (format "%s=%s" (car rdn) (cdr rdn))))
1798        (concat "(" (epg-decode-dn rdn) ")")))
1799    alist
1800    ", "))
1801
1802 (provide 'epg)
1803
1804 ;;; epg.el ends here