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