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