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