(epg-import-result-to-string): New function.
[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 (require 'epg-config)
29
30 (defvar epg-user-id nil
31   "GnuPG ID of your default identity.")
32
33 (defvar epg-user-id-alist nil
34   "An alist mapping from key ID to user ID.")
35
36 (defvar epg-read-point nil)
37 (defvar epg-process-filter-running nil)
38 (defvar epg-pending-status-list nil)
39 (defvar epg-key-id nil)
40 (defvar epg-context nil)
41 (defvar epg-debug-buffer nil)
42
43 ;; from gnupg/include/cipher.h
44 (defconst epg-cipher-algorithm-alist
45   '((0 . "NONE")
46     (1 . "IDEA")
47     (2 . "3DES")
48     (3 . "CAST5")
49     (4 . "BLOWFISH")
50     (7 . "AES")
51     (8 . "AES192")
52     (9 . "AES256")
53     (10 . "TWOFISH")
54     (110 . "DUMMY")))
55
56 ;; from gnupg/include/cipher.h
57 (defconst epg-pubkey-algorithm-alist
58   '((1 . "RSA")
59     (2 . "RSA_E")
60     (3 . "RSA_S")
61     (16 . "ELGAMAL_E")
62     (17 . "DSA")
63     (20 . "ELGAMAL")))
64
65 ;; from gnupg/include/cipher.h
66 (defconst epg-digest-algorithm-alist
67   '((1 . "MD5")
68     (2 . "SHA1")
69     (3 . "RMD160")
70     (8 . "SHA256")
71     (9 . "SHA384")
72     (10 . "SHA512")))
73
74 ;; from gnupg/include/cipher.h
75 (defconst epg-compress-algorithm-alist
76   '((0 . "NONE")
77     (1 . "ZIP")
78     (2 . "ZLIB")
79     (3 . "BZIP2")))
80
81 (defconst epg-invalid-recipients-reason-alist
82   '((0 . "No specific reason given")
83     (1 . "Not Found")
84     (2 . "Ambigious specification")
85     (3 . "Wrong key usage")
86     (4 . "Key revoked")
87     (5 . "Key expired")
88     (6 . "No CRL known")
89     (7 . "CRL too old")
90     (8 . "Policy mismatch")
91     (9 . "Not a secret key")
92     (10 . "Key not trusted")))
93
94 (defconst epg-delete-problem-reason-alist
95   '((1 . "No such key")
96     (2 . "Must delete secret key first")
97     (3 . "Ambigious specification")))
98
99 (defconst epg-import-ok-reason-alist
100   '((0 . "Not actually changed")
101     (1 . "Entirely new key")
102     (2 . "New user IDs")
103     (4 . "New signatures")
104     (8 . "New subkeys")
105     (16 . "Contains private key")))
106
107 (defconst epg-import-problem-reason-alist
108   '((0 . "No specific reason given")
109     (1 . "Invalid Certificate")
110     (2 . "Issuer Certificate missing")
111     (3 . "Certificate Chain too long")
112     (4 . "Error storing certificate")))
113
114 (defconst epg-no-data-reason-alist
115   '((1 . "No armored data")
116     (2 . "Expected a packet but did not found one")
117     (3 . "Invalid packet found, this may indicate a non OpenPGP message")
118     (4 . "Signature expected but not found")))
119
120 (defconst epg-unexpected-reason-alist nil)
121
122 (defvar epg-key-validity-alist
123   '((?o . unknown)
124     (?i . invalid)
125     (?d . disabled)
126     (?r . revoked)
127     (?e . expired)
128     (?- . none)
129     (?q . undefined)
130     (?n . never)
131     (?m . marginal)
132     (?f . full)
133     (?u . ultimate)))
134
135 (defvar epg-key-capablity-alist
136   '((?e . encrypt)
137     (?s . sign)
138     (?c . certify)
139     (?a . authentication)))
140
141 (defvar epg-new-signature-type-alist
142   '((?D . detached)
143     (?C . clear)
144     (?S . normal)))
145
146 (defvar epg-dn-type-alist
147   '(("1.2.840.113549.1.9.1" . "EMail")
148     ("2.5.4.12" . "T")
149     ("2.5.4.42" . "GN")
150     ("2.5.4.4" . "SN")
151     ("0.2.262.1.10.7.20" . "NameDistinguisher")
152     ("2.5.4.16" . "ADDR")
153     ("2.5.4.15" . "BC")
154     ("2.5.4.13" . "D")
155     ("2.5.4.17" . "PostalCode")
156     ("2.5.4.65" . "Pseudo")
157     ("2.5.4.5" . "SerialNumber")))
158
159 (defvar epg-prompt-alist nil)
160
161 (defun epg-make-data-from-file (file)
162   "Make a data object from FILE."
163   (cons 'epg-data (vector file nil)))
164
165 (defun epg-make-data-from-string (string)
166   "Make a data object from STRING."
167   (cons 'epg-data (vector nil string)))
168
169 (defun epg-data-file (data)
170   "Return the file of DATA."
171   (unless (eq (car-safe data) 'epg-data)
172     (signal 'wrong-type-argument (list 'epg-data-p data)))
173   (aref (cdr data) 0))
174
175 (defun epg-data-string (data)
176   "Return the string of DATA."
177   (unless (eq (car-safe data) 'epg-data)
178     (signal 'wrong-type-argument (list 'epg-data-p data)))
179   (aref (cdr data) 1))
180
181 (defun epg-make-context (&optional protocol armor textmode include-certs
182                                    cipher-algorithm digest-algorithm
183                                    compress-algorithm)
184   "Return a context object."
185   (cons 'epg-context
186         (vector (or protocol 'OpenPGP) armor textmode include-certs
187                 cipher-algorithm digest-algorithm compress-algorithm
188                 #'epg-passphrase-callback-function
189                 nil
190                 nil nil nil nil nil nil)))
191
192 (defun epg-context-protocol (context)
193   "Return the protocol used within CONTEXT."
194   (unless (eq (car-safe context) 'epg-context)
195     (signal 'wrong-type-argument (list 'epg-context-p context)))
196   (aref (cdr context) 0))
197
198 (defun epg-context-armor (context)
199   "Return t if the output shouled be ASCII armored in CONTEXT."
200   (unless (eq (car-safe context) 'epg-context)
201     (signal 'wrong-type-argument (list 'epg-context-p context)))
202   (aref (cdr context) 1))
203
204 (defun epg-context-textmode (context)
205   "Return t if canonical text mode should be used in CONTEXT."
206   (unless (eq (car-safe context) 'epg-context)
207     (signal 'wrong-type-argument (list 'epg-context-p context)))
208   (aref (cdr context) 2))
209
210 (defun epg-context-include-certs (context)
211   "Return how many certificates should be included in an S/MIME signed
212 message."
213   (unless (eq (car-safe context) 'epg-context)
214     (signal 'wrong-type-argument (list 'epg-context-p context)))
215   (aref (cdr context) 3))
216
217 (defun epg-context-cipher-algorithm (context)
218   "Return the cipher algorithm in CONTEXT."
219   (unless (eq (car-safe context) 'epg-context)
220     (signal 'wrong-type-argument (list 'epg-context-p context)))
221   (aref (cdr context) 4))
222
223 (defun epg-context-digest-algorithm (context)
224   "Return the digest algorithm in CONTEXT."
225   (unless (eq (car-safe context) 'epg-context)
226     (signal 'wrong-type-argument (list 'epg-context-p context)))
227   (aref (cdr context) 5))
228
229 (defun epg-context-compress-algorithm (context)
230   "Return the compress algorithm in CONTEXT."
231   (unless (eq (car-safe context) 'epg-context)
232     (signal 'wrong-type-argument (list 'epg-context-p context)))
233   (aref (cdr context) 6))
234
235 (defun epg-context-passphrase-callback (context)
236   "Return the function used to query passphrase."
237   (unless (eq (car-safe context) 'epg-context)
238     (signal 'wrong-type-argument (list 'epg-context-p context)))
239   (aref (cdr context) 7))
240
241 (defun epg-context-progress-callback (context)
242   "Return the function which handles progress update."
243   (unless (eq (car-safe context) 'epg-context)
244     (signal 'wrong-type-argument (list 'epg-context-p context)))
245   (aref (cdr context) 8))
246
247 (defun epg-context-signers (context)
248   "Return the list of key-id for singning."
249   (unless (eq (car-safe context) 'epg-context)
250     (signal 'wrong-type-argument (list 'epg-context-p context)))
251   (aref (cdr context) 9))
252
253 (defun epg-context-sig-notations (context)
254   "Return the list of notations for singning."
255   (unless (eq (car-safe context) 'epg-context)
256     (signal 'wrong-type-argument (list 'epg-context-p context)))
257   (aref (cdr context) 10))
258
259 (defun epg-context-process (context)
260   "Return the process object of `epg-gpg-program'.
261 This function is for internal use only."
262   (unless (eq (car-safe context) 'epg-context)
263     (signal 'wrong-type-argument (list 'epg-context-p context)))
264   (aref (cdr context) 11))
265
266 (defun epg-context-output-file (context)
267   "Return the output file of `epg-gpg-program'.
268 This function is for internal use only."
269   (unless (eq (car-safe context) 'epg-context)
270     (signal 'wrong-type-argument (list 'epg-context-p context)))
271   (aref (cdr context) 12))
272
273 (defun epg-context-result (context)
274   "Return the result of the previous cryptographic operation."
275   (unless (eq (car-safe context) 'epg-context)
276     (signal 'wrong-type-argument (list 'epg-context-p context)))
277   (aref (cdr context) 13))
278
279 (defun epg-context-operation (context)
280   "Return the name of the current cryptographic operation."
281   (unless (eq (car-safe context) 'epg-context)
282     (signal 'wrong-type-argument (list 'epg-context-p context)))
283   (aref (cdr context) 14))
284
285 (defun epg-context-set-protocol (context protocol)
286   "Set the protocol used within CONTEXT."
287   (unless (eq (car-safe context) 'epg-context)
288     (signal 'wrong-type-argument (list 'epg-context-p context)))
289   (aset (cdr context) 0 protocol))
290
291 (defun epg-context-set-armor (context armor)
292   "Specify if the output shouled be ASCII armored in CONTEXT."
293   (unless (eq (car-safe context) 'epg-context)
294     (signal 'wrong-type-argument (list 'epg-context-p context)))
295   (aset (cdr context) 1 armor))
296
297 (defun epg-context-set-textmode (context textmode)
298   "Specify if canonical text mode should be used in CONTEXT."
299   (unless (eq (car-safe context) 'epg-context)
300     (signal 'wrong-type-argument (list 'epg-context-p context)))
301   (aset (cdr context) 2 textmode))
302
303 (defun epg-context-set-include-certs (context include-certs)
304  "Set how many certificates should be included in an S/MIME signed message."
305   (unless (eq (car-safe context) 'epg-context)
306     (signal 'wrong-type-argument (list 'epg-context-p context)))
307   (aset (cdr context) 3 include-certs))
308
309 (defun epg-context-set-cipher-algorithm (context cipher-algorithm)
310  "Set the cipher algorithm in CONTEXT."
311   (unless (eq (car-safe context) 'epg-context)
312     (signal 'wrong-type-argument (list 'epg-context-p context)))
313   (aset (cdr context) 4 cipher-algorithm))
314
315 (defun epg-context-set-digest-algorithm (context digest-algorithm)
316  "Set the digest algorithm in CONTEXT."
317   (unless (eq (car-safe context) 'epg-context)
318     (signal 'wrong-type-argument (list 'epg-context-p context)))
319   (aset (cdr context) 5 digest-algorithm))
320
321 (defun epg-context-set-compress-algorithm (context compress-algorithm)
322  "Set the compress algorithm in CONTEXT."
323   (unless (eq (car-safe context) 'epg-context)
324     (signal 'wrong-type-argument (list 'epg-context-p context)))
325   (aset (cdr context) 6 compress-algorithm))
326
327 (defun epg-context-set-passphrase-callback (context
328                                                  passphrase-callback)
329   "Set the function used to query passphrase."
330   (unless (eq (car-safe context) 'epg-context)
331     (signal 'wrong-type-argument (list 'epg-context-p context)))
332   (aset (cdr context) 7 passphrase-callback))
333
334 (defun epg-context-set-progress-callback (context progress-callback)
335   "Set the function which handles progress update."
336   (unless (eq (car-safe context) 'epg-context)
337     (signal 'wrong-type-argument (list 'epg-context-p context)))
338   (aset (cdr context) 8 progress-callback))
339
340 (defun epg-context-set-signers (context signers)
341   "Set the list of key-id for singning."
342   (unless (eq (car-safe context) 'epg-context)
343     (signal 'wrong-type-argument (list 'epg-context-p context)))
344   (aset (cdr context) 9 signers))
345
346 (defun epg-context-set-sig-notations (context notations)
347   "Set the list of notations for singning."
348   (unless (eq (car-safe context) 'epg-context)
349     (signal 'wrong-type-argument (list 'epg-context-p context)))
350   (aset (cdr context) 10 notations))
351
352 (defun epg-context-set-process (context process)
353   "Set the process object of `epg-gpg-program'.
354 This function is for internal use only."
355   (unless (eq (car-safe context) 'epg-context)
356     (signal 'wrong-type-argument (list 'epg-context-p context)))
357   (aset (cdr context) 11 process))
358
359 (defun epg-context-set-output-file (context output-file)
360   "Set the output file of `epg-gpg-program'.
361 This function is for internal use only."
362   (unless (eq (car-safe context) 'epg-context)
363     (signal 'wrong-type-argument (list 'epg-context-p context)))
364   (aset (cdr context) 12 output-file))
365
366 (defun epg-context-set-result (context result)
367   "Set the result of the previous cryptographic operation."
368   (unless (eq (car-safe context) 'epg-context)
369     (signal 'wrong-type-argument (list 'epg-context-p context)))
370   (aset (cdr context) 13 result))
371
372 (defun epg-context-set-operation (context operation)
373   "Set the name of the current cryptographic operation."
374   (unless (eq (car-safe context) 'epg-context)
375     (signal 'wrong-type-argument (list 'epg-context-p context)))
376   (aset (cdr context) 14 operation))
377
378 (defun epg-make-signature (status &optional key-id)
379   "Return a signature object."
380   (cons 'epg-signature (vector status key-id nil nil nil nil nil nil nil nil
381                                nil)))
382
383 (defun epg-signature-status (signature)
384   "Return the status code of SIGNATURE."
385   (unless (eq (car-safe signature) 'epg-signature)
386     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
387   (aref (cdr signature) 0))
388
389 (defun epg-signature-key-id (signature)
390   "Return the key-id of SIGNATURE."
391   (unless (eq (car-safe signature) 'epg-signature)
392     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
393   (aref (cdr signature) 1))
394
395 (defun epg-signature-validity (signature)
396   "Return the validity of SIGNATURE."
397   (unless (eq (car-safe signature) 'epg-signature)
398     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
399   (aref (cdr signature) 2))
400
401 (defun epg-signature-fingerprint (signature)
402   "Return the fingerprint of SIGNATURE."
403   (unless (eq (car-safe signature) 'epg-signature)
404     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
405   (aref (cdr signature) 3))
406
407 (defun epg-signature-creation-time (signature)
408   "Return the creation time of SIGNATURE."
409   (unless (eq (car-safe signature) 'epg-signature)
410     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
411   (aref (cdr signature) 4))
412
413 (defun epg-signature-expiration-time (signature)
414   "Return the expiration time of SIGNATURE."
415   (unless (eq (car-safe signature) 'epg-signature)
416     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
417   (aref (cdr signature) 5))
418
419 (defun epg-signature-pubkey-algorithm (signature)
420   "Return the public key algorithm of SIGNATURE."
421   (unless (eq (car-safe signature) 'epg-signature)
422     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
423   (aref (cdr signature) 6))
424
425 (defun epg-signature-digest-algorithm (signature)
426   "Return the digest algorithm of SIGNATURE."
427   (unless (eq (car-safe signature) 'epg-signature)
428     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
429   (aref (cdr signature) 7))
430
431 (defun epg-signature-class (signature)
432   "Return the class of SIGNATURE."
433   (unless (eq (car-safe signature) 'epg-signature)
434     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
435   (aref (cdr signature) 8))
436
437 (defun epg-signature-version (signature)
438   "Return the version of SIGNATURE."
439   (unless (eq (car-safe signature) 'epg-signature)
440     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
441   (aref (cdr signature) 9))
442
443 (defun epg-sig-notations (signature)
444   "Return the list of notations of SIGNATURE."
445   (unless (eq (car-safe signature) 'epg-signature)
446     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
447   (aref (cdr signature) 10))
448
449 (defun epg-signature-set-status (signature status)
450  "Set the status code of SIGNATURE."
451   (unless (eq (car-safe signature) 'epg-signature)
452     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
453   (aset (cdr signature) 0 status))
454
455 (defun epg-signature-set-key-id (signature key-id)
456  "Set the key-id of SIGNATURE."
457   (unless (eq (car-safe signature) 'epg-signature)
458     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
459   (aset (cdr signature) 1 key-id))
460
461 (defun epg-signature-set-validity (signature validity)
462  "Set the validity of SIGNATURE."
463   (unless (eq (car-safe signature) 'epg-signature)
464     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
465   (aset (cdr signature) 2 validity))
466
467 (defun epg-signature-set-fingerprint (signature fingerprint)
468  "Set the fingerprint of SIGNATURE."
469   (unless (eq (car-safe signature) 'epg-signature)
470     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
471   (aset (cdr signature) 3 fingerprint))
472
473 (defun epg-signature-set-creation-time (signature creation-time)
474   "Set the creation time of SIGNATURE."
475   (unless (eq (car-safe signature) 'epg-signature)
476     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
477   (aset (cdr signature) 4 creation-time))
478
479 (defun epg-signature-set-expiration-time (signature expiration-time)
480   "Set the expiration time of SIGNATURE."
481   (unless (eq (car-safe signature) 'epg-signature)
482     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
483   (aset (cdr signature) 5 expiration-time))
484
485 (defun epg-signature-set-pubkey-algorithm (signature pubkey-algorithm)
486   "Set the public key algorithm of SIGNATURE."
487   (unless (eq (car-safe signature) 'epg-signature)
488     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
489   (aset (cdr signature) 6 pubkey-algorithm))
490
491 (defun epg-signature-set-digest-algorithm (signature digest-algorithm)
492   "Set the digest algorithm of SIGNATURE."
493   (unless (eq (car-safe signature) 'epg-signature)
494     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
495   (aset (cdr signature) 7 digest-algorithm))
496
497 (defun epg-signature-set-class (signature class)
498   "Set the class of SIGNATURE."
499   (unless (eq (car-safe signature) 'epg-signature)
500     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
501   (aset (cdr signature) 8 class))
502
503 (defun epg-signature-set-version (signature version)
504   "Set the version of SIGNATURE."
505   (unless (eq (car-safe signature) 'epg-signature)
506     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
507   (aset (cdr signature) 9 version))
508
509 (defun epg-signature-set-notations (signature notations)
510   "Set the list of notations of SIGNATURE."
511   (unless (eq (car-safe signature) 'epg-signature)
512     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
513   (aset (cdr signature) 10 notations))
514
515 (defun epg-make-new-signature (type pubkey-algorithm digest-algorithm
516                                     class creation-time fingerprint)
517   "Return a new signature object."
518   (cons 'epg-new-signature (vector type pubkey-algorithm digest-algorithm
519                                    class creation-time fingerprint)))
520
521 (defun epg-new-signature-type (new-signature)
522   "Return the type of NEW-SIGNATURE."
523   (unless (eq (car-safe new-signature) 'epg-new-signature)
524     (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
525   (aref (cdr new-signature) 0))
526
527 (defun epg-new-signature-pubkey-algorithm (new-signature)
528   "Return the public key algorithm of NEW-SIGNATURE."
529   (unless (eq (car-safe new-signature) 'epg-new-signature)
530     (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
531   (aref (cdr new-signature) 1))
532
533 (defun epg-new-signature-digest-algorithm (new-signature)
534   "Return the digest algorithm of NEW-SIGNATURE."
535   (unless (eq (car-safe new-signature) 'epg-new-signature)
536     (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
537   (aref (cdr new-signature) 2))
538
539 (defun epg-new-signature-class (new-signature)
540   "Return the class of NEW-SIGNATURE."
541   (unless (eq (car-safe new-signature) 'epg-new-signature)
542     (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
543   (aref (cdr new-signature) 3))
544
545 (defun epg-new-signature-creation-time (new-signature)
546   "Return the creation time of NEW-SIGNATURE."
547   (unless (eq (car-safe new-signature) 'epg-new-signature)
548     (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
549   (aref (cdr new-signature) 4))
550
551 (defun epg-new-signature-fingerprint (new-signature)
552   "Return the fingerprint of NEW-SIGNATURE."
553   (unless (eq (car-safe new-signature) 'epg-new-signature)
554     (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
555   (aref (cdr new-signature) 5))
556
557 (defun epg-make-key (owner-trust)
558   "Return a key object."
559   (cons 'epg-key (vector owner-trust nil nil)))
560
561 (defun epg-key-owner-trust (key)
562   "Return the owner trust of KEY."
563   (unless (eq (car-safe key) 'epg-key)
564     (signal 'wrong-type-argument (list 'epg-key-p key)))
565   (aref (cdr key) 0))
566
567 (defun epg-key-sub-key-list (key)
568   "Return the sub key list of KEY."
569   (unless (eq (car-safe key) 'epg-key)
570     (signal 'wrong-type-argument (list 'epg-key-p key)))
571   (aref (cdr key) 1))
572
573 (defun epg-key-user-id-list (key)
574   "Return the user ID list of KEY."
575   (unless (eq (car-safe key) 'epg-key)
576     (signal 'wrong-type-argument (list 'epg-key-p key)))
577   (aref (cdr key) 2))
578
579 (defun epg-key-set-sub-key-list (key sub-key-list)
580   "Set the sub key list of KEY."
581   (unless (eq (car-safe key) 'epg-key)
582     (signal 'wrong-type-argument (list 'epg-key-p key)))
583   (aset (cdr key) 1 sub-key-list))
584
585 (defun epg-key-set-user-id-list (key user-id-list)
586   "Set the user ID list of KEY."
587   (unless (eq (car-safe key) 'epg-key)
588     (signal 'wrong-type-argument (list 'epg-key-p key)))
589   (aset (cdr key) 2 user-id-list))
590
591 (defun epg-make-sub-key (validity capability secret-p algorithm length id
592                                   creation-time expiration-time)
593   "Return a sub key object."
594   (cons 'epg-sub-key
595         (vector validity capability secret-p algorithm length id creation-time
596                 expiration-time nil)))
597
598 (defun epg-sub-key-validity (sub-key)
599   "Return the validity of SUB-KEY."
600   (unless (eq (car-safe sub-key) 'epg-sub-key)
601     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
602   (aref (cdr sub-key) 0))
603
604 (defun epg-sub-key-capability (sub-key)
605   "Return the capability of SUB-KEY."
606   (unless (eq (car-safe sub-key) 'epg-sub-key)
607     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
608   (aref (cdr sub-key) 1))
609
610 (defun epg-sub-key-secret-p (sub-key)
611   "Return non-nil if SUB-KEY is a secret key."
612   (unless (eq (car-safe sub-key) 'epg-sub-key)
613     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
614   (aref (cdr sub-key) 2))
615
616 (defun epg-sub-key-algorithm (sub-key)
617   "Return the algorithm of SUB-KEY."
618   (unless (eq (car-safe sub-key) 'epg-sub-key)
619     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
620   (aref (cdr sub-key) 3))
621
622 (defun epg-sub-key-length (sub-key)
623   "Return the length of SUB-KEY."
624   (unless (eq (car-safe sub-key) 'epg-sub-key)
625     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
626   (aref (cdr sub-key) 4))
627
628 (defun epg-sub-key-id (sub-key)
629   "Return the ID of SUB-KEY."
630   (unless (eq (car-safe sub-key) 'epg-sub-key)
631     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
632   (aref (cdr sub-key) 5))
633
634 (defun epg-sub-key-creation-time (sub-key)
635   "Return the creation time of SUB-KEY."
636   (unless (eq (car-safe sub-key) 'epg-sub-key)
637     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
638   (aref (cdr sub-key) 6))
639
640 (defun epg-sub-key-expiration-time (sub-key)
641   "Return the expiration time of SUB-KEY."
642   (unless (eq (car-safe sub-key) 'epg-sub-key)
643     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
644   (aref (cdr sub-key) 7))
645
646 (defun epg-sub-key-fingerprint (sub-key)
647   "Return the fingerprint of SUB-KEY."
648   (unless (eq (car-safe sub-key) 'epg-sub-key)
649     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
650   (aref (cdr sub-key) 8))
651
652 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
653   "Set the fingerprint of SUB-KEY.
654 This function is for internal use only."
655   (unless (eq (car-safe sub-key) 'epg-sub-key)
656     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
657   (aset (cdr sub-key) 8 fingerprint))
658
659 (defun epg-make-user-id (validity string)
660   "Return a user ID object."
661   (cons 'epg-user-id (vector validity string nil)))
662
663 (defun epg-user-id-validity (user-id)
664   "Return the validity of USER-ID."
665   (unless (eq (car-safe user-id) 'epg-user-id)
666     (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
667   (aref (cdr user-id) 0))
668
669 (defun epg-user-id-string (user-id)
670   "Return the name of USER-ID."
671   (unless (eq (car-safe user-id) 'epg-user-id)
672     (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
673   (aref (cdr user-id) 1))
674
675 (defun epg-user-id-signature-list (user-id)
676   "Return the signature list of USER-ID."
677   (unless (eq (car-safe user-id) 'epg-user-id)
678     (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
679   (aref (cdr user-id) 2))
680
681 (defun epg-user-id-set-signature-list (user-id signature-list)
682   "Set the signature list of USER-ID."
683   (unless (eq (car-safe user-id) 'epg-user-id)
684     (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
685   (aset (cdr user-id) 2 signature-list))
686
687 (defun epg-make-key-signature (validity pubkey-algorithm key-id creation-time
688                                         expiration-time user-id class
689                                         exportable-p)
690   "Return a key signature object."
691   (cons 'epg-key-signature
692         (vector validity pubkey-algorithm key-id creation-time expiration-time
693                 user-id class exportable-p)))
694
695 (defun epg-key-signature-validity (key-signature)
696   "Return the validity of KEY-SIGNATURE."
697   (unless (eq (car-safe key-signature) 'epg-key-signature)
698     (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
699   (aref (cdr key-signature) 0))
700
701 (defun epg-key-signature-pubkey-algorithm (key-signature)
702   "Return the public key algorithm of KEY-SIGNATURE."
703   (unless (eq (car-safe key-signature) 'epg-key-signature)
704     (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
705   (aref (cdr key-signature) 1))
706
707 (defun epg-key-signature-key-id (key-signature)
708   "Return the key-id of KEY-SIGNATURE."
709   (unless (eq (car-safe key-signature) 'epg-key-signature)
710     (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
711   (aref (cdr key-signature) 2))
712
713 (defun epg-key-signature-creation-time (key-signature)
714   "Return the creation time of KEY-SIGNATURE."
715   (unless (eq (car-safe key-signature) 'epg-key-signature)
716     (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
717   (aref (cdr key-signature) 3))
718
719 (defun epg-key-signature-expiration-time (key-signature)
720   "Return the expiration time of KEY-SIGNATURE."
721   (unless (eq (car-safe key-signature) 'epg-key-signature)
722     (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
723   (aref (cdr key-signature) 4))
724
725 (defun epg-key-signature-user-id (key-signature)
726   "Return the user-id of KEY-SIGNATURE."
727   (unless (eq (car-safe key-signature) 'epg-key-signature)
728     (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
729   (aref (cdr key-signature) 5))
730
731 (defun epg-key-signature-class (key-signature)
732   "Return the class of KEY-SIGNATURE."
733   (unless (eq (car-safe key-signature) 'epg-key-signature)
734     (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
735   (aref (cdr key-signature) 6))
736
737 (defun epg-key-signature-exportable-p (key-signature)
738   "Return t if KEY-SIGNATURE is exportable."
739   (unless (eq (car-safe key-signature) 'epg-key-signature)
740     (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
741   (aref (cdr key-signature) 7))
742
743 (defun epg-make-sig-notation (name value &optional human-readable
744                                          critical)
745   "Return a notation object."
746   (cons 'epg-sig-notation (vector name value human-readable critical)))
747
748 (defun epg-sig-notation-name (sig-notation)
749   "Return the name of SIG-NOTATION."
750   (unless (eq (car-safe sig-notation) 'epg-sig-notation)
751     (signal 'wrong-type-argument (list 'epg-sig-notation-p
752                                        sig-notation)))
753   (aref (cdr sig-notation) 0))
754
755 (defun epg-sig-notation-value (sig-notation)
756   "Return the value of SIG-NOTATION."
757   (unless (eq (car-safe sig-notation) 'epg-sig-notation)
758     (signal 'wrong-type-argument (list 'epg-sig-notation-p
759                                        sig-notation)))
760   (aref (cdr sig-notation) 1))
761
762 (defun epg-sig-notation-human-readable (sig-notation)
763   "Return the human-readable of SIG-NOTATION."
764   (unless (eq (car-safe sig-notation) 'epg-sig-notation)
765     (signal 'wrong-type-argument (list 'epg-sig-notation-p
766                                        sig-notation)))
767   (aref (cdr sig-notation) 2))
768
769 (defun epg-sig-notation-critical (sig-notation)
770   "Return the critical of SIG-NOTATION."
771   (unless (eq (car-safe sig-notation) 'epg-sig-notation)
772     (signal 'wrong-type-argument (list 'epg-sig-notation-p
773                                        sig-notation)))
774   (aref (cdr sig-notation) 3))
775
776 (defun epg-sig-notation-set-value (sig-notation value)
777   "Set the value of SIG-NOTATION."
778   (unless (eq (car-safe sig-notation) 'epg-sig-notation)
779     (signal 'wrong-type-argument (list 'epg-sig-notation-p
780                                        sig-notation)))
781   (aset (cdr sig-notation) 1 value))
782
783 (defun epg-make-import-status (fingerprint reason new user-id signature sub-key
784                                            secret)
785   "Return a import status object."
786   (cons 'epg-import-status (vector fingerprint reason new user-id signature
787                                    sub-key secret)))
788
789 (defun epg-import-status-fingerprint (import-status)
790   "Return the fingerprint of the key that was considered."
791   (unless (eq (car-safe import-status) 'epg-import-status)
792     (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
793   (aref (cdr import-status) 0))
794
795 (defun epg-import-status-reason (import-status)
796   "Return the reason code for import failure."
797   (unless (eq (car-safe import-status) 'epg-import-status)
798     (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
799   (aref (cdr import-status) 1))
800
801 (defun epg-import-status-new (import-status)
802   "Return t if the imported key was new."
803   (unless (eq (car-safe import-status) 'epg-import-status)
804     (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
805   (aref (cdr import-status) 2))
806
807 (defun epg-import-status-user-id (import-status)
808   "Return t if the imported key contained new user IDs."
809   (unless (eq (car-safe import-status) 'epg-import-status)
810     (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
811   (aref (cdr import-status) 3))
812
813 (defun epg-import-status-signature (import-status)
814   "Return t if the imported key contained new signatures."
815   (unless (eq (car-safe import-status) 'epg-import-status)
816     (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
817   (aref (cdr import-status) 4))
818
819 (defun epg-import-status-sub-key (import-status)
820   "Return t if the imported key contained new sub keys."
821   (unless (eq (car-safe import-status) 'epg-import-status)
822     (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
823   (aref (cdr import-status) 5))
824
825 (defun epg-import-status-secret (import-status)
826   "Return t if the imported key contained a secret key."
827   (unless (eq (car-safe import-status) 'epg-import-status)
828     (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
829   (aref (cdr import-status) 6))
830
831 (defun epg-make-import-result (considered no-user-id imported imported-rsa
832                                           unchanged new-user-id new-sub-key
833                                           new-signature new-revocation
834                                           secret-read secret-imported
835                                           secret-unchanged not-imported)
836   "Return a import result object."
837   (cons 'epg-import-result (vector considered no-user-id imported imported-rsa
838                                    unchanged new-user-ids new-sub-keys
839                                    new-signatures new-revocations secret-read
840                                    secret-imported secret-unchanged
841                                    not-imported nil)))
842
843 (defun epg-import-result-considered (import-result)
844   "Return the total number of considered keys."
845   (unless (eq (car-safe import-result) 'epg-import-result)
846     (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
847   (aref (cdr import-result) 0))
848
849 (defun epg-import-result-no-user-id (import-result)
850   "Return the number of keys without user ID."
851   (unless (eq (car-safe import-result) 'epg-import-result)
852     (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
853   (aref (cdr import-result) 1))
854
855 (defun epg-import-result-imported (import-result)
856   "Return the number of imported keys."
857   (unless (eq (car-safe import-result) 'epg-import-result)
858     (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
859   (aref (cdr import-result) 2))
860
861 (defun epg-import-result-imported-rsa (import-result)
862   "Return the number of imported RSA keys."
863   (unless (eq (car-safe import-result) 'epg-import-result)
864     (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
865   (aref (cdr import-result) 3))
866
867 (defun epg-import-result-unchanged (import-result)
868   "Return the number of unchanged keys."
869   (unless (eq (car-safe import-result) 'epg-import-result)
870     (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
871   (aref (cdr import-result) 4))
872
873 (defun epg-import-result-new-user-ids (import-result)
874   "Return the number of new user IDs."
875   (unless (eq (car-safe import-result) 'epg-import-result)
876     (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
877   (aref (cdr import-result) 5))
878
879 (defun epg-import-result-new-sub-keys (import-result)
880   "Return the number of new sub keys."
881   (unless (eq (car-safe import-result) 'epg-import-result)
882     (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
883   (aref (cdr import-result) 6))
884
885 (defun epg-import-result-new-signatures (import-result)
886   "Return the number of new signatures."
887   (unless (eq (car-safe import-result) 'epg-import-result)
888     (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
889   (aref (cdr import-result) 7))
890
891 (defun epg-import-result-new-revocations (import-result)
892   "Return the number of new revocations."
893   (unless (eq (car-safe import-result) 'epg-import-result)
894     (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
895   (aref (cdr import-result) 8))
896
897 (defun epg-import-result-secret-read (import-result)
898   "Return the total number of secret keys read."
899   (unless (eq (car-safe import-result) 'epg-import-result)
900     (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
901   (aref (cdr import-result) 9))
902
903 (defun epg-import-result-secret-imported (import-result)
904   "Return the number of imported secret keys."
905   (unless (eq (car-safe import-result) 'epg-import-result)
906     (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
907   (aref (cdr import-result) 10))
908
909 (defun epg-import-result-secret-unchanged (import-result)
910   "Return the number of unchanged secret keys."
911   (unless (eq (car-safe import-result) 'epg-import-result)
912     (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
913   (aref (cdr import-result) 11))
914
915 (defun epg-import-result-not-imported (import-result)
916   "Return the number of keys not imported."
917   (unless (eq (car-safe import-result) 'epg-import-result)
918     (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
919   (aref (cdr import-result) 12))
920
921 (defun epg-import-result-imports (import-result)
922   "Return the list of `epg-import-status' objects."
923   (unless (eq (car-safe import-result) 'epg-import-result)
924     (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
925   (aref (cdr import-result) 13))
926
927 (defun epg-context-result-for (context name)
928   "Return the result of CONTEXT associated with NAME."
929   (cdr (assq name (epg-context-result context))))
930
931 (defun epg-context-set-result-for (context name value)
932   "Set the result of CONTEXT associated with NAME to VALUE."
933   (let* ((result (epg-context-result context))
934          (entry (assq name result)))
935     (if entry
936         (setcdr entry value)
937       (epg-context-set-result context (cons (cons name value) result)))))
938
939 (defun epg-signature-to-string (signature)
940   "Convert SIGNATURE to a human readable string."
941   (let ((user-id (cdr (assoc (epg-signature-key-id signature)
942                              epg-user-id-alist))))
943     (concat
944      (cond ((eq (epg-signature-status signature) 'good)
945             "Good signature from ")
946            ((eq (epg-signature-status signature) 'bad)
947             "Bad signature from ")
948            ((eq (epg-signature-status signature) 'expired)
949             "Expired signature from ")
950            ((eq (epg-signature-status signature) 'expired-key)
951             "Signature made by expired key ")
952            ((eq (epg-signature-status signature) 'revoked-key)
953             "Signature made by revoked key ")
954            ((eq (epg-signature-status signature) 'no-pubkey)
955             "No public key for "))
956      (epg-signature-key-id signature)
957      (if user-id
958          (concat " "
959                  (if (stringp user-id)
960                      user-id
961                    (epg-decode-dn user-id)))
962        "")
963      (if (epg-signature-validity signature)
964          (format " (trust %s)"  (epg-signature-validity signature))
965        ""))))
966
967 (defun epg-verify-result-to-string (verify-result)
968   "Convert VERIFY-RESULT to a human readable string."
969   (mapconcat #'epg-signature-to-string verify-result "\n"))
970
971 (defun epg-new-signature-to-string (new-signature)
972   "Convert NEW-SIGNATURE to a human readable string."
973   (concat
974    (cond ((eq (epg-new-signature-type new-signature) 'detached)
975           "Detached signature ")
976          ((eq (epg-new-signature-type new-signature) 'clear)
977           "Cleartext signature ")
978          (t
979           "Signature "))
980    (cdr (assq (epg-new-signature-pubkey-algorithm new-signature)
981               epg-pubkey-algorithm-alist))
982    "/"
983    (cdr (assq (epg-new-signature-digest-algorithm new-signature)
984               epg-digest-algorithm-alist))
985    " "
986    (format "%02X " (epg-new-signature-class new-signature))
987    (epg-new-signature-fingerprint new-signature)))
988
989 (defun epg-import-result-to-string (import-result)
990   "Convert IMPORT-RESULT to a human readable string."
991   (concat (format "Total number processed: %d\n"
992                   (epg-import-result-considered import-result))
993           (if (> (epg-import-result-not-imported import-result) 0)
994               (format "      skipped new keys: %d\n"
995                       (epg-import-result-not-imported import-result)))
996           (if (> (epg-import-result-no-user-id import-result) 0)
997               (format "          w/o user IDs: %d\n"
998                       (epg-import-result-no-user-id import-result)))
999           (if (> (epg-import-result-imported import-result) 0)
1000               (concat (format "              imported: %d"
1001                               (epg-import-result-imported import-result))
1002                       (if (> (epg-import-result-imported-rsa import-result) 0)
1003                           (format "  (RSA: %d)"
1004                                   (epg-import-result-imported-rsa
1005                                    import-result)))
1006                       "\n"))
1007           (if (> (epg-import-result-unchanged import-result) 0)
1008               (format "             unchanged: %d\n"
1009                       (epg-import-result-unchanged import-result)))
1010           (if (> (epg-import-result-new-user-ids import-result) 0)
1011               (format "          new user IDs: %d\n"
1012                       (epg-import-result-new-user-ids import-result)))
1013           (if (> (epg-import-result-new-sub-keys import-result) 0)
1014               (format "           new subkeys: %d\n"
1015                       (epg-import-result-new-sub-keys import-result)))
1016           (if (> (epg-import-result-new-signatures import-result) 0)
1017               (format "        new signatures: %d\n"
1018                       (epg-import-result-new-signatures import-result)))
1019           (if (> (epg-import-result-new-revocations import-result) 0)
1020               (format "   new key revocations: %d\n"
1021                       (epg-import-result-new-revocations import-result)))
1022           (if (> (epg-import-result-secret-read import-result) 0)
1023               (format "      secret keys read: %d\n"
1024                       (epg-import-result-secret-read import-result)))
1025           (if (> (epg-import-result-secret-imported import-result) 0)
1026               (format "  secret keys imported: %d\n"
1027                       (epg-import-result-secret-imported import-result)))
1028           (if (> (epg-import-result-secret-unchanged import-result) 0)
1029               (format " secret keys unchanged: %d\n"
1030                       (epg-import-result-secret-unchanged import-result)))))
1031
1032 (defun epg--start (context args)
1033   "Start `epg-gpg-program' in a subprocess with given ARGS."
1034   (if (and (epg-context-process context)
1035            (eq (process-status (epg-context-process context)) 'run))
1036       (error "%s is already running in this context"
1037              (if (eq (epg-context-protocol context) 'CMS)
1038                  epg-gpgsm-program
1039                epg-gpg-program)))
1040   (let* ((args (append (list "--no-tty"
1041                              "--status-fd" "1"
1042                              "--yes")
1043                        (if (epg-context-progress-callback context)
1044                            (list "--enable-progress-filter"))
1045                        (if epg-gpg-home-directory
1046                            (list "--homedir" epg-gpg-home-directory))
1047                        (unless (eq (epg-context-protocol context) 'CMS)
1048                          (list "--command-fd" "0"))
1049                        (if (epg-context-armor context) '("--armor"))
1050                        (if (epg-context-textmode context) '("--textmode"))
1051                        (if (epg-context-output-file context)
1052                            (list "--output" (epg-context-output-file context)))
1053                        args))
1054          (coding-system-for-write 'binary)
1055          process-connection-type
1056          (orig-mode (default-file-modes))
1057          (buffer (generate-new-buffer " *epg*"))
1058          process)
1059     (if epg-debug
1060         (save-excursion
1061           (unless epg-debug-buffer
1062             (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
1063           (set-buffer epg-debug-buffer)
1064           (goto-char (point-max))
1065           (insert (format "%s %s\n"
1066                           (if (eq (epg-context-protocol context) 'CMS)
1067                               epg-gpgsm-program
1068                            epg-gpg-program)
1069                           (mapconcat #'identity args " ")))))
1070     (with-current-buffer buffer
1071       (make-local-variable 'epg-read-point)
1072       (setq epg-read-point (point-min))
1073       (make-local-variable 'epg-process-filter-running)
1074       (setq epg-process-filter-running nil)
1075       (make-local-variable 'epg-pending-status-list)
1076       (setq epg-pending-status-list nil)
1077       (make-local-variable 'epg-key-id)
1078       (setq epg-key-id nil)
1079       (make-local-variable 'epg-context)
1080       (setq epg-context context))
1081     (unwind-protect
1082         (progn
1083           (set-default-file-modes 448)
1084           (setq process
1085                 (apply #'start-process "epg" buffer
1086                        (if (eq (epg-context-protocol context) 'CMS)
1087                            epg-gpgsm-program
1088                          epg-gpg-program)
1089                        args)))
1090       (set-default-file-modes orig-mode))
1091     (set-process-filter process #'epg--process-filter)
1092     (epg-context-set-process context process)))
1093
1094 (defun epg--process-filter (process input)
1095   (if epg-debug
1096       (save-excursion
1097         (unless epg-debug-buffer
1098           (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
1099         (set-buffer epg-debug-buffer)
1100         (goto-char (point-max))
1101         (insert input)))
1102   (if (buffer-live-p (process-buffer process))
1103       (save-excursion
1104         (set-buffer (process-buffer process))
1105         (goto-char (point-max))
1106         (insert input)
1107         (unless epg-process-filter-running
1108           (unwind-protect
1109               (progn
1110                 (setq epg-process-filter-running t)
1111                 (goto-char epg-read-point)
1112                 (beginning-of-line)
1113                 (while (looking-at ".*\n") ;the input line finished
1114                   (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
1115                       (let* ((status (match-string 1))
1116                              (string (match-string 2))
1117                              (symbol (intern-soft (concat "epg--status-"
1118                                                           status))))
1119                         (if (member status epg-pending-status-list)
1120                             (setq epg-pending-status-list nil))
1121                         (if (and symbol
1122                                  (fboundp symbol))
1123                             (funcall symbol epg-context string))))
1124                   (forward-line)
1125                   (setq epg-read-point (point))))
1126             (setq epg-process-filter-running nil))))))
1127
1128 (defun epg-read-output (context)
1129   "Read the output file CONTEXT and return the content as a string."
1130   (with-temp-buffer
1131     (if (fboundp 'set-buffer-multibyte)
1132         (set-buffer-multibyte nil))
1133     (if (file-exists-p (epg-context-output-file context))
1134         (let ((coding-system-for-read 'binary))
1135           (insert-file-contents (epg-context-output-file context))
1136           (buffer-string)))))
1137
1138 (defun epg-wait-for-status (context status-list)
1139   "Wait until one of elements in STATUS-LIST arrives."
1140   (with-current-buffer (process-buffer (epg-context-process context))
1141     (setq epg-pending-status-list status-list)
1142     (while (and (eq (process-status (epg-context-process context)) 'run)
1143                 epg-pending-status-list)
1144       (accept-process-output (epg-context-process context) 1))))
1145
1146 (defun epg-wait-for-completion (context)
1147   "Wait until the `epg-gpg-program' process completes."
1148   (while (eq (process-status (epg-context-process context)) 'run)
1149     (accept-process-output (epg-context-process context) 1)))
1150
1151 (defun epg-reset (context)
1152   "Reset the CONTEXT."
1153   (if (and (epg-context-process context)
1154            (buffer-live-p (process-buffer (epg-context-process context))))
1155       (kill-buffer (process-buffer (epg-context-process context))))
1156   (epg-context-set-process context nil))
1157
1158 (defun epg-delete-output-file (context)
1159   "Delete the output file of CONTEXT."
1160   (if (and (epg-context-output-file context)
1161            (file-exists-p (epg-context-output-file context)))
1162       (delete-file (epg-context-output-file context))))
1163
1164 (defun epg--status-USERID_HINT (context string)
1165   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1166       (let* ((key-id (match-string 1 string))
1167              (user-id (match-string 2 string))
1168              (entry (assoc key-id epg-user-id-alist)))
1169         (if entry
1170             (setcdr entry user-id)
1171           (setq epg-user-id-alist (cons (cons key-id user-id)
1172                                         epg-user-id-alist))))))
1173
1174 (defun epg--status-NEED_PASSPHRASE (context string)
1175   (if (string-match "\\`\\([^ ]+\\)" string)
1176       (setq epg-key-id (match-string 1 string))))
1177
1178 (defun epg--status-NEED_PASSPHRASE_SYM (context string)
1179   (setq epg-key-id 'SYM))
1180
1181 (defun epg--status-NEED_PASSPHRASE_PIN (context string)
1182   (setq epg-key-id 'PIN))
1183
1184 (defun epg--status-GET_HIDDEN (context string)
1185   (when (and epg-key-id
1186              (string-match "\\`passphrase\\." string))
1187     (unless (epg-context-passphrase-callback context)
1188       (error "passphrase-callback not set"))
1189     (let (inhibit-quit
1190           passphrase
1191           passphrase-with-new-line
1192           encoded-passphrase-with-new-line)
1193       (unwind-protect
1194           (condition-case nil
1195               (progn
1196                 (setq passphrase
1197                       (funcall
1198                        (if (consp (epg-context-passphrase-callback context))
1199                            (car (epg-context-passphrase-callback context))
1200                          (epg-context-passphrase-callback context))
1201                        context
1202                        epg-key-id
1203                        (if (consp (epg-context-passphrase-callback context))
1204                            (cdr (epg-context-passphrase-callback context)))))
1205                 (when passphrase
1206                   (setq passphrase-with-new-line (concat passphrase "\n"))
1207                   (epg--clear-string passphrase)
1208                   (setq passphrase nil)
1209                   (if epg-passphrase-coding-system
1210                       (progn
1211                         (setq encoded-passphrase-with-new-line
1212                               (encode-coding-string
1213                                passphrase-with-new-line
1214                                epg-passphrase-coding-system))
1215                         (epg--clear-string passphrase-with-new-line)
1216                         (setq passphrase-with-new-line nil))
1217                     (setq encoded-passphrase-with-new-line
1218                           passphrase-with-new-line
1219                           passphrase-with-new-line nil))
1220                   (process-send-string (epg-context-process context)
1221                                        encoded-passphrase-with-new-line)))
1222             (quit
1223              (epg-context-set-result-for
1224               context 'error
1225               (cons '(quit)
1226                     (epg-context-result-for context 'error)))
1227              (delete-process (epg-context-process context))))
1228         (if passphrase
1229             (epg--clear-string passphrase))
1230         (if passphrase-with-new-line
1231             (epg--clear-string passphrase-with-new-line))
1232         (if encoded-passphrase-with-new-line
1233             (epg--clear-string encoded-passphrase-with-new-line))))))
1234
1235 (defun epg--status-GET_BOOL (context string)
1236   (let ((entry (assoc string epg-prompt-alist))
1237         inhibit-quit)
1238     (condition-case nil
1239       (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
1240           (process-send-string (epg-context-process context) "y\n")
1241         (process-send-string (epg-context-process context) "n\n"))
1242       (quit
1243        (epg-context-set-result-for
1244         context 'error
1245         (cons '(quit)
1246               (epg-context-result-for context 'error)))
1247        (delete-process (epg-context-process context))))))
1248
1249 (defun epg--status-GET_LINE (context string)
1250   (let ((entry (assoc string epg-prompt-alist))
1251         inhibit-quit)
1252     (condition-case nil
1253         (process-send-string (epg-context-process context)
1254                              (concat (read-string
1255                                       (if entry
1256                                           (cdr entry)
1257                                         (concat string ": ")))
1258                                      "\n"))
1259       (quit
1260        (epg-context-set-result-for
1261         context 'error
1262         (cons '(quit)
1263               (epg-context-result-for context 'error)))
1264        (delete-process (epg-context-process context))))))
1265
1266 (defun epg--status-*SIG (context status string)
1267   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1268       (let* ((key-id (match-string 1 string))
1269              (user-id (match-string 2 string))
1270              (entry (assoc key-id epg-user-id-alist)))
1271         (epg-context-set-result-for
1272          context
1273          'verify
1274          (cons (epg-make-signature status key-id)
1275                (epg-context-result-for context 'verify)))
1276         (if (eq (epg-context-protocol context) 'CMS)
1277             (condition-case nil
1278                 (setq user-id (epg-dn-from-string user-id))
1279               (error)))
1280         (if entry
1281             (setcdr entry user-id)
1282           (setq epg-user-id-alist
1283                 (cons (cons key-id user-id) epg-user-id-alist))))
1284     (epg-context-set-result-for
1285      context
1286      'verify
1287      (cons (epg-make-signature status)
1288            (epg-context-result-for context 'verify)))))
1289
1290 (defun epg--status-GOODSIG (context string)
1291   (epg--status-*SIG context 'good string))
1292
1293 (defun epg--status-EXPSIG (context string)
1294   (epg--status-*SIG context 'expired string))
1295
1296 (defun epg--status-EXPKEYSIG (context string)
1297   (epg--status-*SIG context 'expired-key string))
1298
1299 (defun epg--status-REVKEYSIG (context string)
1300   (epg--status-*SIG context 'revoked-key string))
1301
1302 (defun epg--status-BADSIG (context string)
1303   (epg--status-*SIG context 'bad string))
1304
1305 (defun epg--status-NO_PUBKEY (context string)
1306   (let ((signature (car (epg-context-result-for context 'verify))))
1307     (if (and signature
1308              (eq (epg-signature-status signature) 'error)
1309              (equal (epg-signature-key-id signature) string))
1310         (epg-signature-set-status signature 'no-pubkey))))
1311
1312 (defun epg--time-from-seconds (seconds)
1313   (let ((number-seconds (string-to-number (concat seconds ".0"))))
1314     (cons (floor (/ number-seconds 65536))
1315           (floor (mod number-seconds 65536)))))
1316
1317 (defun epg--status-ERRSIG (context string)
1318   (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1319 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
1320                     string)
1321       (let ((signature (epg-make-signature 'error)))
1322         (epg-context-set-result-for
1323          context
1324          'verify
1325          (cons signature
1326                (epg-context-result-for context 'verify)))
1327         (epg-signature-set-key-id
1328          signature
1329          (match-string 1 string))
1330         (epg-signature-set-pubkey-algorithm
1331          signature
1332          (string-to-number (match-string 2 string)))
1333         (epg-signature-set-digest-algorithm
1334          signature
1335          (string-to-number (match-string 3 string)))
1336         (epg-signature-set-class
1337          signature
1338          (string-to-number (match-string 4 string) 16))
1339         (epg-signature-set-creation-time
1340          signature
1341          (epg--time-from-seconds (match-string 5 string))))))
1342
1343 (defun epg--status-VALIDSIG (context string)
1344   (let ((signature (car (epg-context-result-for context 'verify))))
1345     (when (and signature
1346                (eq (epg-signature-status signature) 'good)
1347                (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
1348 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
1349 \\(.*\\)"
1350                            string))
1351       (epg-signature-set-fingerprint
1352        signature
1353        (match-string 1 string))
1354       (epg-signature-set-creation-time
1355        signature
1356        (epg--time-from-seconds (match-string 2 string)))
1357       (epg-signature-set-expiration-time
1358        signature
1359        (epg--time-from-seconds (match-string 3 string)))
1360       (epg-signature-set-version
1361        signature
1362        (string-to-number (match-string 4 string)))
1363       (epg-signature-set-pubkey-algorithm
1364        signature 
1365        (string-to-number (match-string 5 string)))
1366       (epg-signature-set-digest-algorithm
1367        signature
1368        (string-to-number (match-string 6 string)))
1369       (epg-signature-set-class
1370        signature
1371        (string-to-number (match-string 7 string) 16)))))
1372
1373 (defun epg--status-TRUST_UNDEFINED (context string)
1374   (let ((signature (car (epg-context-result-for context 'verify))))
1375     (if (and signature
1376              (eq (epg-signature-status signature) 'good))
1377         (epg-signature-set-validity signature 'undefined))))
1378
1379 (defun epg--status-TRUST_NEVER (context string)
1380   (let ((signature (car (epg-context-result-for context 'verify))))
1381     (if (and signature
1382              (eq (epg-signature-status signature) 'good))
1383         (epg-signature-set-validity signature 'never))))
1384
1385 (defun epg--status-TRUST_MARGINAL (context string)
1386   (let ((signature (car (epg-context-result-for context 'verify))))
1387     (if (and signature
1388              (eq (epg-signature-status signature) 'marginal))
1389         (epg-signature-set-validity signature 'marginal))))
1390
1391 (defun epg--status-TRUST_FULLY (context string)
1392   (let ((signature (car (epg-context-result-for context 'verify))))
1393     (if (and signature
1394              (eq (epg-signature-status signature) 'good))
1395         (epg-signature-set-validity signature 'full))))
1396
1397 (defun epg--status-TRUST_ULTIMATE (context string)
1398   (let ((signature (car (epg-context-result-for context 'verify))))
1399     (if (and signature
1400              (eq (epg-signature-status signature) 'good))
1401         (epg-signature-set-validity signature 'ultimate))))
1402
1403 (defun epg--status-NOTATION_NAME (context string)
1404   (let ((signature (car (epg-context-result-for context 'verify))))
1405     (if signature
1406         (epg-signature-set-notations
1407          signature
1408          (cons (epg-make-sig-notation string nil t nil)
1409                (epg-sig-notations signature))))))
1410
1411 (defun epg--status-NOTATION_DATA (context string)
1412   (let ((signature (car (epg-context-result-for context 'verify)))
1413         notation)
1414     (if (and signature
1415              (setq notation (car (epg-sig-notations signature))))
1416         (epg-sig-notation-set-value notation string))))
1417
1418 (defun epg--status-POLICY_URL (context string)
1419   (let ((signature (car (epg-context-result-for context 'verify))))
1420     (if signature
1421         (epg-signature-set-notations
1422          signature
1423          (cons (epg-make-sig-notation nil string t nil)
1424                (epg-sig-notations signature))))))
1425
1426 (defun epg--status-PROGRESS (context string)
1427   (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1428                     string)
1429       (funcall (if (consp (epg-context-progress-callback context))
1430                    (car (epg-context-progress-callback context))
1431                  (epg-context-progress-callback context))
1432                context
1433                (match-string 1 string)
1434                (match-string 2 string)
1435                (string-to-number (match-string 3 string))
1436                (string-to-number (match-string 4 string))
1437                (if (consp (epg-context-progress-callback context))
1438                    (cdr (epg-context-progress-callback context))))))
1439
1440 (defun epg--status-DECRYPTION_FAILED (context string)
1441   (epg-context-set-result-for
1442    context 'error
1443    (cons '(decryption-failed)
1444          (epg-context-result-for context 'error))))
1445
1446 (defun epg--status-NODATA (context string)
1447   (epg-context-set-result-for
1448    context 'error
1449    (cons (list 'no-data (cons 'reason (string-to-number string)))
1450          (epg-context-result-for context 'error))))
1451
1452 (defun epg--status-UNEXPECTED (context string)
1453   (epg-context-set-result-for
1454    context 'error
1455    (cons (list 'unexpected (cons 'reason (string-to-number string)))
1456          (epg-context-result-for context 'error))))
1457
1458 (defun epg--status-KEYEXPIRED (context string)
1459   (epg-context-set-result-for
1460    context 'error
1461    (cons (list 'key-expired (cons 'expiration-time
1462                                   (epg--time-from-seconds string)))
1463          (epg-context-result-for context 'error))))
1464
1465 (defun epg--status-KEYREVOKED (context string)
1466   (epg-context-set-result-for
1467    context 'error
1468    (cons '(key-revoked)
1469          (epg-context-result-for context 'error))))
1470
1471 (defun epg--status-BADARMOR (context string)
1472   (epg-context-set-result-for
1473    context 'error
1474    (cons '(bad-armor)
1475          (epg-context-result-for context 'error))))
1476
1477 (defun epg--status-INV_RECP (context string)
1478   (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1479       (epg-context-set-result-for
1480        context 'error
1481        (cons (list 'invalid-recipient
1482                    (cons 'reason
1483                          (string-to-number (match-string 1 string)))
1484                    (cons 'requested-recipient
1485                          (match-string 2 string)))
1486              (epg-context-result-for context 'error)))))
1487
1488 (defun epg--status-NO_RECP (context string)
1489   (epg-context-set-result-for
1490    context 'error
1491    (cons '(no-recipients)
1492          (epg-context-result-for context 'error))))
1493
1494 (defun epg--status-DELETE_PROBLEM (context string)
1495   (if (string-match "\\`\\([0-9]+\\)" string)
1496       (epg-context-set-result-for
1497        context 'error
1498        (cons (list 'delete-problem
1499                    (cons 'reason (string-to-number (match-string 1 string))))
1500              (epg-context-result-for context 'error)))))
1501
1502 (defun epg--status-SIG_CREATED (context string)
1503   (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1504 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1505       (epg-context-set-result-for
1506        context 'sign
1507        (cons (epg-make-new-signature
1508               (cdr (assq (aref (match-string 1 string) 0)
1509                          epg-new-signature-type-alist))
1510               (string-to-number (match-string 2 string))
1511               (string-to-number (match-string 3 string))
1512               (string-to-number (match-string 4 string) 16)
1513               (epg--time-from-seconds (match-string 5 string))
1514               (substring string (match-end 0)))
1515              (epg-context-result-for context 'sign)))))
1516
1517 (defun epg--status-KEY_CREATED (context string)
1518   (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1519       (epg-context-set-result-for
1520        context 'generate-key
1521        (cons (list (cons 'type (string-to-char (match-string 1 string)))
1522                    (cons 'fingerprint (match-string 2 string)))
1523              (epg-context-result-for context 'generate-key)))))
1524
1525 (defun epg--status-KEY_NOT_CREATED (context string)
1526   (epg-context-set-result-for
1527    context 'error
1528    (cons '(key-not-created)
1529          (epg-context-result-for context 'error))))
1530
1531 (defun epg--status-IMPORTED (context string)
1532   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1533       (let* ((key-id (match-string 1 string))
1534              (user-id (match-string 2 string))
1535              (entry (assoc key-id epg-user-id-alist)))
1536         (if entry
1537             (setcdr entry user-id)
1538           (setq epg-user-id-alist (cons (cons key-id user-id)
1539                                         epg-user-id-alist))))))
1540
1541 (defun epg--status-IMPORT_OK (context string)
1542   (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1543       (let ((reason (string-to-number (match-string 1 string))))
1544         (epg-context-set-result-for
1545          context 'import-status
1546          (cons (epg-make-import-status (if (match-beginning 2)
1547                                            (match-string 3 string))
1548                                        nil
1549                                        (/= (logand reason 1) 0)
1550                                        (/= (logand reason 2) 0)
1551                                        (/= (logand reason 4) 0)
1552                                        (/= (logand reason 8) 0)
1553                                        (/= (logand reason 16) 0))
1554                (epg-context-result-for context 'import-status))))))
1555
1556 (defun epg--status-IMPORT_PROBLEM (context string)
1557   (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1558       (epg-context-set-result-for
1559        context 'import-status
1560        (cons (epg-make-import-status
1561               (if (match-beginning 2)
1562                   (match-string 3 string))
1563               (string-to-number (match-string 1 string)))
1564              (epg-context-result-for context 'import-status)))))
1565
1566 (defun epg--status-IMPORT_RES (context string)
1567   (when (string-match "\\`\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1568 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1569 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
1570     (epg-context-set-result-for
1571      context 'import
1572      (cons (epg-make-import-result
1573             (string-to-number (match-string 1 string))
1574             (string-to-number (match-string 2 string))
1575             (string-to-number (match-string 3 string))
1576             (string-to-number (match-string 4 string))
1577             (string-to-number (match-string 5 string))
1578             (string-to-number (match-string 6 string))
1579             (string-to-number (match-string 7 string))
1580             (string-to-number (match-string 8 string))
1581             (string-to-number (match-string 9 string))
1582             (string-to-number (match-string 10 string))
1583             (string-to-number (match-string 11 string))
1584             (string-to-number (match-string 12 string))
1585             (string-to-number (match-string 13 string))
1586             (epg-context-result-for context 'import-status))
1587            (epg-context-result-for context 'import)))
1588     (epg-context-set-result-for context 'import-status nil)))
1589
1590 (defun epg-passphrase-callback-function (context key-id handback)
1591   (if (eq key-id 'SYM)
1592       (read-passwd "Passphrase for symmetric encryption: "
1593                    (eq (epg-context-operation context) 'encrypt))
1594     (read-passwd
1595      (if (eq key-id 'PIN)
1596         "Passphrase for PIN: "
1597        (let ((entry (assoc key-id epg-user-id-alist)))
1598          (if entry
1599              (format "Passphrase for %s %s: " key-id (cdr entry))
1600            (format "Passphrase for %s: " key-id)))))))
1601
1602 (make-obsolete 'epg-passphrase-callback-function
1603                'epa-passphrase-callback-function)
1604
1605 (defun epg--list-keys-1 (context name mode)
1606   (let ((args (append (if epg-gpg-home-directory
1607                           (list "--homedir" epg-gpg-home-directory))
1608                       (list "--with-colons" "--no-greeting" "--batch"
1609                             "--with-fingerprint"
1610                             "--with-fingerprint"
1611                             (if (memq mode '(t secret))
1612                                 "--list-secret-keys"
1613                               (if (memq mode '(nil public))
1614                                   "--list-keys"
1615                                 "--list-sigs")))
1616                       (unless (eq (epg-context-protocol context) 'CMS)
1617                         '("--fixed-list-mode"))
1618                       (if name (list name))))
1619         keys string field index)
1620     (with-temp-buffer
1621       (apply #'call-process
1622              (if (eq (epg-context-protocol context) 'CMS)
1623                  epg-gpgsm-program
1624                epg-gpg-program)
1625              nil (list t nil) nil args)
1626       (goto-char (point-min))
1627       (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1628         (setq keys (cons (make-vector 15 nil) keys)
1629               string (match-string 0)
1630               index 0
1631               field 0)
1632         (while (eq index
1633                    (string-match "\\([^:]+\\)?:" string index))
1634           (setq index (match-end 0))
1635           (aset (car keys) field (match-string 1 string))
1636           (setq field (1+ field))))
1637       (nreverse keys))))
1638
1639 (defun epg--make-sub-key-1 (line)
1640   (epg-make-sub-key
1641    (if (aref line 1)
1642        (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1643    (delq nil
1644          (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1645                  (aref line 11)))
1646    (member (aref line 0) '("sec" "ssb"))
1647    (string-to-number (aref line 3))
1648    (string-to-number (aref line 2))
1649    (aref line 4)
1650    (epg--time-from-seconds (aref line 5))
1651    (epg--time-from-seconds (aref line 6))))
1652
1653 ;;;###autoload
1654 (defun epg-list-keys (context &optional name mode)
1655   "Return a list of epg-key objects matched with NAME.
1656 If MODE is nil or 'public, only public keyring should be searched.
1657 If MODE is t or 'secret, only secret keyring should be searched. 
1658 Otherwise, only public keyring should be searched and the key
1659 signatures should be included."
1660   (let ((lines (epg--list-keys-1 context name mode))
1661         keys cert pointer pointer-1)
1662     (while lines
1663       (cond
1664        ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1665         (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1666               keys (cons (epg-make-key
1667                           (if (aref (car lines) 8)
1668                               (cdr (assq (string-to-char (aref (car lines) 8))
1669                                          epg-key-validity-alist))))
1670                          keys))
1671         (epg-key-set-sub-key-list
1672          (car keys)
1673          (cons (epg--make-sub-key-1 (car lines))
1674                (epg-key-sub-key-list (car keys)))))
1675        ((member (aref (car lines) 0) '("sub" "ssb"))
1676         (epg-key-set-sub-key-list
1677          (car keys)
1678          (cons (epg--make-sub-key-1 (car lines))
1679                (epg-key-sub-key-list (car keys)))))
1680        ((equal (aref (car lines) 0) "uid")
1681         (epg-key-set-user-id-list
1682          (car keys)
1683          (cons (epg-make-user-id
1684                 (if (aref (car lines) 1)
1685                     (cdr (assq (string-to-char (aref (car lines) 1))
1686                                epg-key-validity-alist)))
1687                 (if cert
1688                     (condition-case nil
1689                         (epg-dn-from-string (aref (car lines) 9))
1690                       (error (aref (car lines) 9)))
1691                   (aref (car lines) 9)))
1692                (epg-key-user-id-list (car keys)))))
1693        ((equal (aref (car lines) 0) "fpr")
1694         (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1695                                      (aref (car lines) 9)))
1696        ((equal (aref (car lines) 0) "sig")
1697         (epg-user-id-set-signature-list
1698          (car (epg-key-user-id-list (car keys)))
1699          (cons
1700           (epg-make-key-signature
1701            (if (aref (car lines) 1)
1702                (cdr (assq (string-to-char (aref (car lines) 1))
1703                           epg-key-validity-alist)))
1704            (string-to-number (aref (car lines) 3))
1705            (aref (car lines) 4)
1706            (epg--time-from-seconds (aref (car lines) 5))
1707            (epg--time-from-seconds (aref (car lines) 6))
1708            (aref (car lines) 9)
1709            (string-to-number (aref (car lines) 10) 16)
1710            (eq (aref (aref (car lines) 10) 2) ?x))
1711           (epg-user-id-signature-list
1712            (car (epg-key-user-id-list (car keys))))))))
1713       (setq lines (cdr lines)))
1714     (setq keys (nreverse keys)
1715           pointer keys)
1716     (while pointer
1717       (epg-key-set-sub-key-list
1718        (car pointer)
1719        (nreverse (epg-key-sub-key-list (car pointer))))
1720       (setq pointer-1 (epg-key-set-user-id-list
1721                           (car pointer)
1722                           (nreverse (epg-key-user-id-list (car pointer)))))
1723       (while pointer-1
1724         (epg-user-id-set-signature-list
1725          (car pointer-1)
1726          (nreverse (epg-user-id-signature-list (car pointer-1))))
1727         (setq pointer-1 (cdr pointer-1)))
1728       (setq pointer (cdr pointer)))
1729     keys))
1730
1731 (if (fboundp 'make-temp-file)
1732     (defalias 'epg--make-temp-file 'make-temp-file)
1733   (defvar temporary-file-directory)
1734   ;; stolen from poe.el.
1735   (defun epg--make-temp-file (prefix)
1736     "Create a temporary file.
1737 The returned file name (created by appending some random characters at the end
1738 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1739 is guaranteed to point to a newly created empty file.
1740 You can then use `write-region' to write new data into the file."
1741     (let (tempdir tempfile)
1742       (setq prefix (expand-file-name prefix
1743                                      (if (featurep 'xemacs)
1744                                          (temp-directory)
1745                                        temporary-file-directory)))
1746       (unwind-protect
1747           (let (file)
1748             ;; First, create a temporary directory.
1749             (while (condition-case ()
1750                        (progn
1751                          (setq tempdir (make-temp-name
1752                                         (concat
1753                                          (file-name-directory prefix)
1754                                          "DIR")))
1755                          ;; return nil or signal an error.
1756                          (make-directory tempdir))
1757                      ;; let's try again.
1758                      (file-already-exists t)))
1759             (set-file-modes tempdir 448)
1760             ;; Second, create a temporary file in the tempdir.
1761             ;; There *is* a race condition between `make-temp-name'
1762             ;; and `write-region', but we don't care it since we are
1763             ;; in a private directory now.
1764             (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1765             (write-region "" nil tempfile nil 'silent)
1766             (set-file-modes tempfile 384)
1767             ;; Finally, make a hard-link from the tempfile.
1768             (while (condition-case ()
1769                        (progn
1770                          (setq file (make-temp-name prefix))
1771                          ;; return nil or signal an error.
1772                          (add-name-to-file tempfile file))
1773                      ;; let's try again.
1774                      (file-already-exists t)))
1775             file)
1776         ;; Cleanup the tempfile.
1777         (and tempfile
1778              (file-exists-p tempfile)
1779              (delete-file tempfile))
1780         ;; Cleanup the tempdir.
1781         (and tempdir
1782              (file-directory-p tempdir)
1783              (delete-directory tempdir))))))
1784
1785 (if (fboundp 'clear-string)
1786     (defalias 'epg--clear-string 'clear-string)
1787   (defun epg--clear-string (string)
1788     (fillarray string 0)))
1789
1790 (defun epg--args-from-sig-notations (notations)
1791   (apply #'nconc
1792          (mapcar
1793           (lambda (notation)
1794             (if (and (epg-sig-notation-name notation)
1795                      (not (epg-sig-notation-human-readable notation)))
1796                 (error "Unreadable"))
1797             (if (epg-sig-notation-name notation)
1798                 (list "--sig-notation"
1799                       (if (epg-sig-notation-critical notation)
1800                           (concat "!" (epg-sig-notation-name notation)
1801                                   "=" (epg-sig-notation-value notation))
1802                         (concat (epg-sig-notation-name notation)
1803                                 "=" (epg-sig-notation-value notation))))
1804               (list "--sig-policy-url"
1805                     (if (epg-sig-notation-critical notation)
1806                         (concat "!" (epg-sig-notation-value notation))
1807                       (epg-sig-notation-value notation)))))
1808           notations)))
1809
1810 ;;;###autoload
1811 (defun epg-cancel (context)
1812   (if (buffer-live-p (process-buffer (epg-context-process context)))
1813       (save-excursion
1814         (set-buffer (process-buffer (epg-context-process context)))
1815         (epg-context-set-result-for
1816          epg-context 'error
1817          (cons '(quit)
1818                (epg-context-result-for epg-context 'error)))))
1819   (if (eq (process-status (epg-context-process context)) 'run)
1820       (delete-process (epg-context-process context))))
1821   
1822 ;;;###autoload
1823 (defun epg-start-decrypt (context cipher)
1824   "Initiate a decrypt operation on CIPHER.
1825 CIPHER must be a file data object.
1826
1827 If you use this function, you will need to wait for the completion of
1828 `epg-gpg-program' by using `epg-wait-for-completion' and call
1829 `epg-reset' to clear a temporaly output file.
1830 If you are unsure, use synchronous version of this function
1831 `epg-decrypt-file' or `epg-decrypt-string' instead."
1832   (unless (epg-data-file cipher)
1833     (error "Not a file"))
1834   (epg-context-set-operation context 'decrypt)
1835   (epg-context-set-result context nil)
1836   (epg--start context (list "--decrypt" "--" (epg-data-file cipher)))
1837   ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1838   (unless (eq (epg-context-protocol context) 'CMS)
1839     (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1840
1841 ;;;###autoload
1842 (defun epg-decrypt-file (context cipher plain)
1843   "Decrypt a file CIPHER and store the result to a file PLAIN.
1844 If PLAIN is nil, it returns the result as a string."
1845   (unwind-protect
1846       (progn
1847         (if plain
1848             (epg-context-set-output-file context plain)
1849           (epg-context-set-output-file context
1850                                        (epg--make-temp-file "epg-output")))
1851         (epg-start-decrypt context (epg-make-data-from-file cipher))
1852         (epg-wait-for-completion context)
1853         (if (epg-context-result-for context 'error)
1854             (error "Decrypt failed: %S"
1855                    (epg-context-result-for context 'error)))
1856         (unless plain
1857           (epg-read-output context)))
1858     (unless plain
1859       (epg-delete-output-file context))
1860     (epg-reset context)))
1861
1862 ;;;###autoload
1863 (defun epg-decrypt-string (context cipher)
1864   "Decrypt a string CIPHER and return the plain text."
1865   (let ((input-file (epg--make-temp-file "epg-input"))
1866         (coding-system-for-write 'binary))
1867     (unwind-protect
1868         (progn
1869           (write-region cipher nil input-file nil 'quiet)
1870           (epg-context-set-output-file context
1871                                        (epg--make-temp-file "epg-output"))
1872           (epg-start-decrypt context (epg-make-data-from-file input-file))
1873           (epg-wait-for-completion context)
1874           (if (epg-context-result-for context 'error)
1875               (error "Decrypt failed: %S"
1876                      (epg-context-result-for context 'error)))
1877           (epg-read-output context))
1878       (epg-delete-output-file context)
1879       (if (file-exists-p input-file)
1880           (delete-file input-file))
1881       (epg-reset context))))
1882
1883 ;;;###autoload
1884 (defun epg-start-verify (context signature &optional signed-text)
1885   "Initiate a verify operation on SIGNATURE.
1886 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1887
1888 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1889 For a normal or a cleartext signature, SIGNED-TEXT should be nil.
1890
1891 If you use this function, you will need to wait for the completion of
1892 `epg-gpg-program' by using `epg-wait-for-completion' and call
1893 `epg-reset' to clear a temporaly output file.
1894 If you are unsure, use synchronous version of this function
1895 `epg-verify-file' or `epg-verify-string' instead."
1896   (epg-context-set-operation context 'verify)
1897   (epg-context-set-result context nil)
1898   (if signed-text
1899       ;; Detached signature.
1900       (if (epg-data-file signed-text)
1901           (epg--start context (list "--verify" "--" (epg-data-file signature)
1902                                    (epg-data-file signed-text)))
1903         (epg--start context (list "--verify" "--" (epg-data-file signature)
1904                                   "-"))
1905         (if (eq (process-status (epg-context-process context)) 'run)
1906             (process-send-string (epg-context-process context)
1907                                  (epg-data-string signed-text)))
1908         (if (eq (process-status (epg-context-process context)) 'run)
1909             (process-send-eof (epg-context-process context))))
1910     ;; Normal (or cleartext) signature.
1911     (if (epg-data-file signature)
1912         (epg--start context (list "--" (epg-data-file signature)))
1913       (epg--start context '("-"))
1914       (if (eq (process-status (epg-context-process context)) 'run)
1915           (process-send-string (epg-context-process context)
1916                                (epg-data-string signature)))
1917       (if (eq (process-status (epg-context-process context)) 'run)
1918           (process-send-eof (epg-context-process context))))))
1919
1920 ;;;###autoload
1921 (defun epg-verify-file (context signature &optional signed-text plain)
1922   "Verify a file SIGNATURE.
1923 SIGNED-TEXT and PLAIN are also a file if they are specified.
1924
1925 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1926 string.  For a normal or a cleartext signature, SIGNED-TEXT should be
1927 nil.  In the latter case, if PLAIN is specified, the plaintext is
1928 stored into the file after successful verification."
1929   (unwind-protect
1930       (progn
1931         (if plain
1932             (epg-context-set-output-file context plain)
1933           (epg-context-set-output-file context
1934                                        (epg--make-temp-file "epg-output")))
1935         (if signed-text
1936             (epg-start-verify context
1937                               (epg-make-data-from-file signature)
1938                               (epg-make-data-from-file signed-text))
1939           (epg-start-verify context
1940                             (epg-make-data-from-file signature)))
1941         (epg-wait-for-completion context)
1942         (unless plain
1943           (epg-read-output context)))
1944     (unless plain
1945       (epg-delete-output-file context))
1946     (epg-reset context)))
1947
1948 ;;;###autoload
1949 (defun epg-verify-string (context signature &optional signed-text)
1950   "Verify a string SIGNATURE.
1951 SIGNED-TEXT is a string if it is specified.
1952
1953 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1954 string.  For a normal or a cleartext signature, SIGNED-TEXT should be
1955 nil.  In the latter case, this function returns the plaintext after
1956 successful verification."
1957   (let ((coding-system-for-write 'binary)
1958         input-file)
1959     (unwind-protect
1960         (progn
1961           (epg-context-set-output-file context
1962                                        (epg--make-temp-file "epg-output"))
1963           (if signed-text
1964               (progn
1965                 (setq input-file (epg--make-temp-file "epg-signature"))
1966                 (write-region signature nil input-file nil 'quiet)
1967                 (epg-start-verify context
1968                                   (epg-make-data-from-file input-file)
1969                                   (epg-make-data-from-string signed-text)))
1970             (epg-start-verify context (epg-make-data-from-string signature)))
1971           (epg-wait-for-completion context)
1972           (epg-read-output context))
1973       (epg-delete-output-file context)
1974       (if (and input-file
1975                (file-exists-p input-file))
1976           (delete-file input-file))
1977       (epg-reset context))))
1978
1979 ;;;###autoload
1980 (defun epg-start-sign (context plain &optional mode)
1981   "Initiate a sign operation on PLAIN.
1982 PLAIN is a data object.
1983
1984 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1985 If it is nil or 'normal, it makes a normal signature.
1986 Otherwise, it makes a cleartext signature.
1987
1988 If you use this function, you will need to wait for the completion of
1989 `epg-gpg-program' by using `epg-wait-for-completion' and call
1990 `epg-reset' to clear a temporaly output file.
1991 If you are unsure, use synchronous version of this function
1992 `epg-sign-file' or `epg-sign-string' instead."
1993   (epg-context-set-operation context 'sign)
1994   (epg-context-set-result context nil)
1995   (unless (memq mode '(t detached nil normal)) ;i.e. cleartext
1996     (epg-context-set-armor context nil)
1997     (epg-context-set-textmode context nil))
1998   (epg--start context
1999              (append (list (if (memq mode '(t detached))
2000                                "--detach-sign"
2001                              (if (memq mode '(nil normal))
2002                                  "--sign"
2003                                "--clearsign")))
2004                      (apply #'nconc
2005                             (mapcar
2006                              (lambda (signer)
2007                                (list "-u"
2008                                      (epg-sub-key-id
2009                                       (car (epg-key-sub-key-list signer)))))
2010                              (epg-context-signers context)))
2011                      (epg--args-from-sig-notations
2012                       (epg-context-sig-notations context))
2013                      (if (epg-data-file plain)
2014                          (list "--" (epg-data-file plain)))))
2015   ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2016   (unless (eq (epg-context-protocol context) 'CMS)
2017     (epg-wait-for-status context '("BEGIN_SIGNING")))
2018   (when (epg-data-string plain)
2019     (if (eq (process-status (epg-context-process context)) 'run)
2020         (process-send-string (epg-context-process context)
2021                              (epg-data-string plain)))
2022     (if (eq (process-status (epg-context-process context)) 'run)
2023         (process-send-eof (epg-context-process context)))))
2024
2025 ;;;###autoload
2026 (defun epg-sign-file (context plain signature &optional mode)
2027   "Sign a file PLAIN and store the result to a file SIGNATURE.
2028 If SIGNATURE is nil, it returns the result as a string.
2029 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2030 If it is nil or 'normal, it makes a normal signature.
2031 Otherwise, it makes a cleartext signature."
2032   (unwind-protect
2033       (progn
2034         (if signature
2035             (epg-context-set-output-file context signature)
2036           (epg-context-set-output-file context
2037                                        (epg--make-temp-file "epg-output")))
2038         (epg-start-sign context (epg-make-data-from-file plain) mode)
2039         (epg-wait-for-completion context)
2040         (unless (epg-context-result-for context 'sign)
2041           (if (epg-context-result-for context 'error)
2042               (error "Sign failed: %S"
2043                      (epg-context-result-for context 'error))
2044             (error "Sign failed")))
2045         (unless signature
2046           (epg-read-output context)))
2047     (unless signature
2048       (epg-delete-output-file context))
2049     (epg-reset context)))
2050
2051 ;;;###autoload
2052 (defun epg-sign-string (context plain &optional mode)
2053   "Sign a string PLAIN and return the output as string.
2054 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2055 If it is nil or 'normal, it makes a normal signature.
2056 Otherwise, it makes a cleartext signature."
2057   (unwind-protect
2058       (progn
2059         (epg-context-set-output-file context
2060                                      (epg--make-temp-file "epg-output"))
2061         (epg-start-sign context (epg-make-data-from-string plain) mode)
2062         (epg-wait-for-completion context)
2063         (unless (epg-context-result-for context 'sign)
2064           (if (epg-context-result-for context 'error)
2065               (error "Sign failed: %S"
2066                      (epg-context-result-for context 'error))
2067             (error "Sign failed")))
2068         (epg-read-output context))
2069     (epg-delete-output-file context)
2070     (epg-reset context)))
2071
2072 ;;;###autoload
2073 (defun epg-start-encrypt (context plain recipients
2074                                   &optional sign always-trust)
2075   "Initiate an encrypt operation on PLAIN.
2076 PLAIN is a data object.
2077 If RECIPIENTS is nil, it performs symmetric encryption.
2078
2079 If you use this function, you will need to wait for the completion of
2080 `epg-gpg-program' by using `epg-wait-for-completion' and call
2081 `epg-reset' to clear a temporaly output file.
2082 If you are unsure, use synchronous version of this function
2083 `epg-encrypt-file' or `epg-encrypt-string' instead."
2084   (epg-context-set-operation context 'encrypt)
2085   (epg-context-set-result context nil)
2086   (epg--start context
2087              (append (if always-trust '("--always-trust"))
2088                      (if recipients '("--encrypt") '("--symmetric"))
2089                      (if sign '("--sign"))
2090                      (if sign
2091                          (apply #'nconc
2092                                 (mapcar
2093                                  (lambda (signer)
2094                                    (list "-u"
2095                                          (epg-sub-key-id
2096                                           (car (epg-key-sub-key-list
2097                                                 signer)))))
2098                                  (epg-context-signers context))))
2099                      (if sign
2100                          (epg--args-from-sig-notations
2101                           (epg-context-sig-notations context)))
2102                      (apply #'nconc
2103                             (mapcar
2104                              (lambda (recipient)
2105                                (list "-r"
2106                                      (epg-sub-key-id
2107                                       (car (epg-key-sub-key-list recipient)))))
2108                              recipients))
2109                      (if (epg-data-file plain)
2110                          (list "--" (epg-data-file plain)))))
2111   ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2112   (unless (eq (epg-context-protocol context) 'CMS)
2113     (if sign
2114         (epg-wait-for-status context '("BEGIN_SIGNING"))
2115       (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
2116   (when (epg-data-string plain)
2117     (if (eq (process-status (epg-context-process context)) 'run)
2118         (process-send-string (epg-context-process context)
2119                              (epg-data-string plain)))
2120     (if (eq (process-status (epg-context-process context)) 'run)
2121         (process-send-eof (epg-context-process context)))))
2122
2123 ;;;###autoload
2124 (defun epg-encrypt-file (context plain recipients
2125                                  cipher &optional sign always-trust)
2126   "Encrypt a file PLAIN and store the result to a file CIPHER.
2127 If CIPHER is nil, it returns the result as a string.
2128 If RECIPIENTS is nil, it performs symmetric encryption."
2129   (unwind-protect
2130       (progn
2131         (if cipher
2132             (epg-context-set-output-file context cipher)
2133           (epg-context-set-output-file context
2134                                        (epg--make-temp-file "epg-output")))
2135         (epg-start-encrypt context (epg-make-data-from-file plain)
2136                            recipients sign always-trust)
2137         (epg-wait-for-completion context)
2138         (if (and sign
2139                  (not (epg-context-result-for context 'sign)))
2140             (if (epg-context-result-for context 'error)
2141                 (error "Sign failed: %S"
2142                        (epg-context-result-for context 'error))
2143                 (error "Sign failed")))
2144         (if (epg-context-result-for context 'error)
2145             (error "Encrypt failed: %S"
2146                    (epg-context-result-for context 'error)))
2147         (unless cipher
2148           (epg-read-output context)))
2149     (unless cipher
2150       (epg-delete-output-file context))
2151     (epg-reset context)))
2152
2153 ;;;###autoload
2154 (defun epg-encrypt-string (context plain recipients
2155                                    &optional sign always-trust)
2156   "Encrypt a string PLAIN.
2157 If RECIPIENTS is nil, it performs symmetric encryption."
2158   (unwind-protect
2159       (progn
2160         (epg-context-set-output-file context
2161                                      (epg--make-temp-file "epg-output"))
2162         (epg-start-encrypt context (epg-make-data-from-string plain)
2163                            recipients sign always-trust)
2164         (epg-wait-for-completion context)
2165         (if (and sign
2166                  (not (epg-context-result-for context 'sign)))
2167             (if (epg-context-result-for context 'error)
2168                 (error "Sign failed: %S"
2169                        (epg-context-result-for context 'error))
2170               (error "Sign failed")))
2171         (if (epg-context-result-for context 'error)
2172             (error "Encrypt failed: %S"
2173                    (epg-context-result-for context 'error)))
2174         (epg-read-output context))
2175     (epg-delete-output-file context)
2176     (epg-reset context)))
2177
2178 ;;;###autoload
2179 (defun epg-start-export-keys (context keys)
2180   "Initiate an export keys operation.
2181
2182 If you use this function, you will need to wait for the completion of
2183 `epg-gpg-program' by using `epg-wait-for-completion' and call
2184 `epg-reset' to clear a temporaly output file.
2185 If you are unsure, use synchronous version of this function
2186 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
2187   (epg-context-set-operation context 'export-keys)
2188   (epg-context-set-result context nil)
2189   (epg--start context (cons "--export"
2190                            (mapcar
2191                             (lambda (key)
2192                               (epg-sub-key-id
2193                                (car (epg-key-sub-key-list key))))
2194                             keys))))
2195
2196 ;;;###autoload
2197 (defun epg-export-keys-to-file (context keys file)
2198   "Extract public KEYS."
2199   (unwind-protect
2200       (progn
2201         (if keys
2202             (epg-context-set-output-file context file)
2203           (epg-context-set-output-file context
2204                                        (epg--make-temp-file "epg-output")))
2205         (epg-start-export-keys context keys)
2206         (epg-wait-for-completion context)
2207         (if (epg-context-result-for context 'error)
2208             (error "Export keys failed: %S"
2209                    (epg-context-result-for context 'error)))
2210         (unless file
2211           (epg-read-output context)))
2212     (unless file
2213       (epg-delete-output-file context))
2214     (epg-reset context)))
2215
2216 ;;;###autoload
2217 (defun epg-export-keys-to-string (context keys)
2218   "Extract public KEYS and return them as a string."
2219   (epg-export-keys-to-file context keys nil))
2220
2221 ;;;###autoload
2222 (defun epg-start-import-keys (context keys)
2223   "Initiate an import keys operation.
2224 KEYS is a data object.
2225
2226 If you use this function, you will need to wait for the completion of
2227 `epg-gpg-program' by using `epg-wait-for-completion' and call
2228 `epg-reset' to clear a temporaly output file.
2229 If you are unsure, use synchronous version of this function
2230 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
2231   (epg-context-set-operation context 'import-keys)
2232   (epg-context-set-result context nil)
2233   (epg--start context (if (epg-data-file keys)
2234                           (list "--import" "--" (epg-data-file keys))
2235                         (list "--import")))
2236   (when (epg-data-string keys)
2237     (if (eq (process-status (epg-context-process context)) 'run)
2238         (process-send-string (epg-context-process context)
2239                              (epg-data-string keys)))
2240     (if (eq (process-status (epg-context-process context)) 'run)
2241         (process-send-eof (epg-context-process context)))))
2242
2243 (defun epg--import-keys-1 (context keys)
2244   (unwind-protect
2245       (progn
2246         (epg-start-import-keys context keys)
2247         (epg-wait-for-completion context)
2248         (if (epg-context-result-for context 'error)
2249             (error "Import keys failed: %S"
2250                    (epg-context-result-for context 'error))))
2251     (epg-reset context)))
2252
2253 ;;;###autoload
2254 (defun epg-import-keys-from-file (context keys)
2255   "Add keys from a file KEYS."
2256   (epg--import-keys-1 context (epg-make-data-from-file keys)))
2257
2258 ;;;###autoload
2259 (defun epg-import-keys-from-string (context keys)
2260   "Add keys from a string KEYS."
2261   (epg--import-keys-1 context (epg-make-data-from-string keys)))
2262
2263 ;;;###autoload
2264 (defun epg-start-receive-keys (context key-id-list)
2265   "Initiate a receive key operation.
2266 KEY-ID-LIST is a list of key IDs.
2267
2268 If you use this function, you will need to wait for the completion of
2269 `epg-gpg-program' by using `epg-wait-for-completion' and call
2270 `epg-reset' to clear a temporaly output file.
2271 If you are unsure, use synchronous version of this function
2272 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2273   (epg-context-set-operation context 'receive-keys)
2274   (epg-context-set-result context nil)
2275   (epg--start context (cons "--recv-keys" key-id-list)))
2276
2277 ;;;###autoload
2278 (defun epg-receive-keys (context keys)
2279   "Add keys from server.
2280 KEYS is a list of key IDs"
2281   (unwind-protect
2282       (progn
2283         (epg-start-receive-keys context keys)
2284         (epg-wait-for-completion context)
2285         (if (epg-context-result-for context 'error)
2286             (error "Receive keys failed: %S"
2287                    (epg-context-result-for context 'error))))
2288     (epg-reset context)))
2289
2290 ;;;###autoload
2291 (defalias 'epg-import-keys-from-server 'epg-receive-keys)
2292
2293 ;;;###autoload
2294 (defun epg-start-delete-keys (context keys &optional allow-secret)
2295   "Initiate an delete keys operation.
2296
2297 If you use this function, you will need to wait for the completion of
2298 `epg-gpg-program' by using `epg-wait-for-completion' and call
2299 `epg-reset' to clear a temporaly output file.
2300 If you are unsure, use synchronous version of this function
2301 `epg-delete-keys' instead."
2302   (epg-context-set-operation context 'delete-keys)
2303   (epg-context-set-result context nil)
2304   (epg--start context (cons (if allow-secret
2305                                "--delete-secret-key"
2306                              "--delete-key")
2307                            (mapcar
2308                             (lambda (key)
2309                               (epg-sub-key-id
2310                                (car (epg-key-sub-key-list key))))
2311                             keys))))
2312
2313 ;;;###autoload
2314 (defun epg-delete-keys (context keys &optional allow-secret)
2315   "Delete KEYS from the key ring."
2316   (unwind-protect
2317       (progn
2318         (epg-start-delete-keys context keys allow-secret)
2319         (epg-wait-for-completion context)
2320         (if (epg-context-result-for context 'error)
2321             (error "Delete keys failed: %S"
2322                    (epg-context-result-for context 'error))))
2323     (epg-reset context)))
2324
2325 ;;;###autoload
2326 (defun epg-start-sign-keys (context keys &optional local)
2327   "Initiate an sign keys operation.
2328
2329 If you use this function, you will need to wait for the completion of
2330 `epg-gpg-program' by using `epg-wait-for-completion' and call
2331 `epg-reset' to clear a temporaly output file.
2332 If you are unsure, use synchronous version of this function
2333 `epg-sign-keys' instead."
2334   (epg-context-set-operation context 'sign-keys)
2335   (epg-context-set-result context nil)
2336   (epg--start context (cons (if local
2337                                "--lsign-key"
2338                              "--sign-key")
2339                            (mapcar
2340                             (lambda (key)
2341                               (epg-sub-key-id
2342                                (car (epg-key-sub-key-list key))))
2343                             keys))))
2344 (make-obsolete 'epg-start-sign-keys "Do not use.")
2345
2346 ;;;###autoload
2347 (defun epg-sign-keys (context keys &optional local)
2348   "Sign KEYS from the key ring."
2349   (unwind-protect
2350       (progn
2351         (epg-start-sign-keys context keys local)
2352         (epg-wait-for-completion context)
2353         (if (epg-context-result-for context 'error)
2354             (error "Sign keys failed: %S"
2355                    (epg-context-result-for context 'error))))
2356     (epg-reset context)))
2357 (make-obsolete 'epg-sign-keys "Do not use.")
2358
2359 ;;;###autoload
2360 (defun epg-start-generate-key (context parameters)
2361   "Initiate a key generation.
2362 PARAMETERS specifies parameters for the key.
2363
2364 If you use this function, you will need to wait for the completion of
2365 `epg-gpg-program' by using `epg-wait-for-completion' and call
2366 `epg-reset' to clear a temporaly output file.
2367 If you are unsure, use synchronous version of this function
2368 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2369   (epg-context-set-operation context 'generate-key)
2370   (epg-context-set-result context nil)
2371   (if (epg-data-file parameters)
2372       (epg--start context (list "--batch" "--genkey" "--"
2373                                (epg-data-file parameters)))
2374     (epg--start context '("--batch" "--genkey"))
2375     (if (eq (process-status (epg-context-process context)) 'run)
2376         (process-send-string (epg-context-process context)
2377                              (epg-data-string parameters)))
2378     (if (eq (process-status (epg-context-process context)) 'run)
2379         (process-send-eof (epg-context-process context)))))
2380
2381 ;;;###autoload
2382 (defun epg-generate-key-from-file (context parameters)
2383   "Generate a new key pair.
2384 PARAMETERS is a file which tells how to create the key."
2385   (unwind-protect
2386       (progn
2387         (epg-start-generate-key context (epg-make-data-from-file parameters))
2388         (epg-wait-for-completion context)
2389         (if (epg-context-result-for context 'error)
2390             (error "Generate key failed: %S"
2391                    (epg-context-result-for context 'error))))
2392     (epg-reset context)))
2393
2394 ;;;###autoload
2395 (defun epg-generate-key-from-string (context parameters)
2396   "Generate a new key pair.
2397 PARAMETERS is a string which tells how to create the key."
2398   (unwind-protect
2399       (progn
2400         (epg-start-generate-key context (epg-make-data-from-string parameters))
2401         (epg-wait-for-completion context)
2402         (if (epg-context-result-for context 'error)
2403             (error "Generate key failed: %S"
2404                    (epg-context-result-for context 'error))))
2405     (epg-reset context)))
2406
2407 (defun epg--decode-hexstring (string)
2408   (let ((index 0))
2409     (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
2410       (setq string (replace-match "\\x\\&" t nil string)
2411             index (+ index 4)))
2412     (car (read-from-string (concat "\"" string "\"")))))
2413
2414 (defun epg--decode-quotedstring (string)
2415   (let ((index 0))
2416     (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2417 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
2418                          string index)
2419       (if (match-beginning 2)
2420           (setq string (replace-match "\\2" t nil string)
2421                 index (1+ index))
2422         (if (match-beginning 3)
2423             (setq string (replace-match "\\x\\3" t nil string)
2424                   index (+ index 4))
2425           (setq string (replace-match "\\\\\\\\\\4" t nil string)
2426                 index (+ index 3)))))
2427     (car (read-from-string (concat "\"" string "\"")))))
2428
2429 (defun epg-dn-from-string (string)
2430   "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2431 The return value is an alist mapping from types to values."
2432   (let ((index 0)
2433         (length (length string))
2434         alist type value group)
2435     (while (< index length)
2436       (if (eq index (string-match "[ \t\n\r]*" string index))
2437           (setq index (match-end 0)))
2438       (if (eq index (string-match
2439                      "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2440                      string index))
2441           (setq type (match-string 1 string)
2442                 index (match-end 0))
2443         (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2444                                     string index))
2445             (setq type (match-string 1 string)
2446                   index (match-end 0))))
2447       (unless type
2448         (error "Invalid type"))
2449       (if (eq index (string-match
2450                      "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2451                      string index))
2452           (setq index (match-end 0)
2453                 value (epg--decode-quotedstring (match-string 0 string)))
2454         (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
2455             (setq index (match-end 0)
2456                   value (epg--decode-hexstring (match-string 1 string)))
2457           (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2458                                       string index))
2459               (setq index (match-end 0)
2460                     value (epg--decode-quotedstring
2461                            (match-string 0 string))))))
2462       (if group
2463           (if (stringp (car (car alist)))
2464               (setcar alist (list (cons type value) (car alist)))
2465             (setcar alist (cons (cons type value) (car alist))))
2466         (if (consp (car (car alist)))
2467             (setcar alist (nreverse (car alist))))
2468         (setq alist (cons (cons type value) alist)
2469               type nil
2470               value nil))
2471       (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
2472           (setq index (match-end 0)
2473                 group (eq (aref string (match-beginning 1)) ?+))))
2474     (nreverse alist)))
2475
2476 (defun epg-decode-dn (alist)
2477   "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2478 Type names are resolved using `epg-dn-type-alist'."
2479   (mapconcat
2480    (lambda (rdn)
2481      (if (stringp (car rdn))
2482          (let ((entry (assoc (car rdn) epg-dn-type-alist)))
2483            (if entry
2484                (format "%s=%s" (cdr entry) (cdr rdn))
2485              (format "%s=%s" (car rdn) (cdr rdn))))
2486        (concat "(" (epg-decode-dn rdn) ")")))
2487    alist
2488    ", "))
2489
2490 (provide 'epg)
2491
2492 ;;; epg.el ends here