Initial Commit
[packages] / xemacs-packages / easypg / 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 PASSPHRASE-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 PROGRESS-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       (if (fboundp 'set-buffer-multibyte)
1099           (set-buffer-multibyte nil))
1100       (make-local-variable 'epg-last-status)
1101       (setq epg-last-status nil)
1102       (make-local-variable 'epg-read-point)
1103       (setq epg-read-point (point-min))
1104       (make-local-variable 'epg-process-filter-running)
1105       (setq epg-process-filter-running nil)
1106       (make-local-variable 'epg-pending-status-list)
1107       (setq epg-pending-status-list nil)
1108       (make-local-variable 'epg-key-id)
1109       (setq epg-key-id nil)
1110       (make-local-variable 'epg-context)
1111       (setq epg-context context))
1112     (unwind-protect
1113         (progn
1114           (set-default-file-modes 448)
1115           (setq process
1116                 (apply #'start-process "epg" buffer
1117                        (if (eq (epg-context-protocol context) 'CMS)
1118                            epg-gpgsm-program
1119                          epg-gpg-program)
1120                        args)))
1121       (set-default-file-modes orig-mode))
1122     (set-process-filter process #'epg--process-filter)
1123     (epg-context-set-process context process)))
1124
1125 (defun epg--process-filter (process input)
1126   (if epg-debug
1127       (save-excursion
1128         (unless epg-debug-buffer
1129           (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
1130         (set-buffer epg-debug-buffer)
1131         (goto-char (point-max))
1132         (insert input)))
1133   (if (buffer-live-p (process-buffer process))
1134       (save-excursion
1135         (set-buffer (process-buffer process))
1136         (goto-char (point-max))
1137         (insert input)
1138         (unless epg-process-filter-running
1139           (unwind-protect
1140               (progn
1141                 (setq epg-process-filter-running t)
1142                 (goto-char epg-read-point)
1143                 (beginning-of-line)
1144                 (while (looking-at ".*\n") ;the input line finished
1145                   (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
1146                       (let* ((status (match-string 1))
1147                              (string (match-string 2))
1148                              (symbol (intern-soft (concat "epg--status-"
1149                                                           status))))
1150                         (if (member status epg-pending-status-list)
1151                             (setq epg-pending-status-list nil))
1152                         (if (and symbol
1153                                  (fboundp symbol))
1154                             (funcall symbol epg-context string))
1155                         (setq epg-last-status (cons status string))))
1156                   (forward-line)
1157                   (setq epg-read-point (point))))
1158             (setq epg-process-filter-running nil))))))
1159
1160 (defun epg-read-output (context)
1161   "Read the output file CONTEXT and return the content as a string."
1162   (with-temp-buffer
1163     (if (fboundp 'set-buffer-multibyte)
1164         (set-buffer-multibyte nil))
1165     (if (file-exists-p (epg-context-output-file context))
1166         (let ((coding-system-for-read 'binary))
1167           (insert-file-contents (epg-context-output-file context))
1168           (buffer-string)))))
1169
1170 (defun epg-wait-for-status (context status-list)
1171   "Wait until one of elements in STATUS-LIST arrives."
1172   (with-current-buffer (process-buffer (epg-context-process context))
1173     (setq epg-pending-status-list status-list)
1174     (while (and (eq (process-status (epg-context-process context)) 'run)
1175                 epg-pending-status-list)
1176       (accept-process-output (epg-context-process context) 1))))
1177
1178 (defun epg-wait-for-completion (context)
1179   "Wait until the `epg-gpg-program' process completes."
1180   (while (eq (process-status (epg-context-process context)) 'run)
1181     (accept-process-output (epg-context-process context) 1)))
1182
1183 (defun epg-reset (context)
1184   "Reset the CONTEXT."
1185   (if (and (epg-context-process context)
1186            (buffer-live-p (process-buffer (epg-context-process context))))
1187       (kill-buffer (process-buffer (epg-context-process context))))
1188   (epg-context-set-process context nil))
1189
1190 (defun epg-delete-output-file (context)
1191   "Delete the output file of CONTEXT."
1192   (if (and (epg-context-output-file context)
1193            (file-exists-p (epg-context-output-file context)))
1194       (delete-file (epg-context-output-file context))))
1195
1196 (eval-and-compile
1197   (if (fboundp 'decode-coding-string)
1198       (defalias 'epg--decode-coding-string 'decode-coding-string)
1199     (defalias 'epg--decode-coding-string 'identity)))
1200
1201 (defun epg--status-USERID_HINT (context string)
1202   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1203       (let* ((key-id (match-string 1 string))
1204              (user-id (match-string 2 string))
1205              (entry (assoc key-id epg-user-id-alist)))
1206         (condition-case nil
1207             (setq user-id (epg--decode-coding-string
1208                            (epg--decode-percent-escape user-id)
1209                            'utf-8))
1210           (error))
1211         (if entry
1212             (setcdr entry user-id)
1213           (setq epg-user-id-alist (cons (cons key-id user-id)
1214                                         epg-user-id-alist))))))
1215
1216 (defun epg--status-NEED_PASSPHRASE (context string)
1217   (if (string-match "\\`\\([^ ]+\\)" string)
1218       (setq epg-key-id (match-string 1 string))))
1219
1220 (defun epg--status-NEED_PASSPHRASE_SYM (context string)
1221   (setq epg-key-id 'SYM))
1222
1223 (defun epg--status-NEED_PASSPHRASE_PIN (context string)
1224   (setq epg-key-id 'PIN))
1225
1226 (eval-and-compile
1227   (if (fboundp 'clear-string)
1228       (defalias 'epg--clear-string 'clear-string)
1229     (defun epg--clear-string (string)
1230       (fillarray string 0))))
1231
1232 (eval-and-compile
1233   (if (fboundp 'encode-coding-string)
1234       (defalias 'epg--encode-coding-string 'encode-coding-string)
1235     (defalias 'epg--encode-coding-string 'identity)))
1236
1237 (defun epg--status-GET_HIDDEN (context string)
1238   (when (and epg-key-id
1239              (string-match "\\`passphrase\\." string))
1240     (unless (epg-context-passphrase-callback context)
1241       (error "passphrase-callback not set"))
1242     (let (inhibit-quit
1243           passphrase
1244           passphrase-with-new-line
1245           encoded-passphrase-with-new-line)
1246       (unwind-protect
1247           (condition-case nil
1248               (progn
1249                 (setq passphrase
1250                       (funcall
1251                        (if (consp (epg-context-passphrase-callback context))
1252                            (car (epg-context-passphrase-callback context))
1253                          (epg-context-passphrase-callback context))
1254                        context
1255                        epg-key-id
1256                        (if (consp (epg-context-passphrase-callback context))
1257                            (cdr (epg-context-passphrase-callback context)))))
1258                 (when passphrase
1259                   (setq passphrase-with-new-line (concat passphrase "\n"))
1260                   (epg--clear-string passphrase)
1261                   (setq passphrase nil)
1262                   (if epg-passphrase-coding-system
1263                       (progn
1264                         (setq encoded-passphrase-with-new-line
1265                               (epg--encode-coding-string
1266                                passphrase-with-new-line
1267                                (coding-system-change-eol-conversion
1268                                 epg-passphrase-coding-system 'unix)))
1269                         (epg--clear-string passphrase-with-new-line)
1270                         (setq passphrase-with-new-line nil))
1271                     (setq encoded-passphrase-with-new-line
1272                           passphrase-with-new-line
1273                           passphrase-with-new-line nil))
1274                   (process-send-string (epg-context-process context)
1275                                        encoded-passphrase-with-new-line)))
1276             (quit
1277              (epg-context-set-result-for
1278               context 'error
1279               (cons '(quit)
1280                     (epg-context-result-for context 'error)))
1281              (delete-process (epg-context-process context))))
1282         (if passphrase
1283             (epg--clear-string passphrase))
1284         (if passphrase-with-new-line
1285             (epg--clear-string passphrase-with-new-line))
1286         (if encoded-passphrase-with-new-line
1287             (epg--clear-string encoded-passphrase-with-new-line))))))
1288
1289 (defun epg--prompt-GET_BOOL (context string)
1290   (let ((entry (assoc string epg-prompt-alist)))
1291     (y-or-n-p (if entry (cdr entry) (concat string "? ")))))
1292
1293 (defun epg--prompt-GET_BOOL-untrusted_key.override (context string)
1294   (y-or-n-p (if (and (equal (car epg-last-status) "USERID_HINT")
1295                      (string-match "\\`\\([^ ]+\\) \\(.*\\)"
1296                                    (cdr epg-last-status)))
1297                 (let* ((key-id (match-string 1 (cdr epg-last-status)))
1298                        (user-id (match-string 2 (cdr epg-last-status)))
1299                        (entry (assoc key-id epg-user-id-alist)))
1300                   (if entry
1301                       (setq user-id (cdr entry)))
1302                   (format "Untrusted key %s %s.  Use anyway? " key-id user-id))
1303               "Use untrusted key anyway? ")))
1304
1305 (defun epg--status-GET_BOOL (context string)
1306   (let (inhibit-quit)
1307     (condition-case nil
1308         (if (funcall (or (intern-soft (concat "epg--prompt-GET_BOOL-" string))
1309                          #'epg--prompt-GET_BOOL)
1310                      context string)
1311             (process-send-string (epg-context-process context) "y\n")
1312           (process-send-string (epg-context-process context) "n\n"))
1313       (quit
1314        (epg-context-set-result-for
1315         context 'error
1316         (cons '(quit)
1317               (epg-context-result-for context 'error)))
1318        (delete-process (epg-context-process context))))))
1319
1320 (defun epg--status-GET_LINE (context string)
1321   (let ((entry (assoc string epg-prompt-alist))
1322         inhibit-quit)
1323     (condition-case nil
1324         (process-send-string (epg-context-process context)
1325                              (concat (read-string
1326                                       (if entry
1327                                           (cdr entry)
1328                                         (concat string ": ")))
1329                                      "\n"))
1330       (quit
1331        (epg-context-set-result-for
1332         context 'error
1333         (cons '(quit)
1334               (epg-context-result-for context 'error)))
1335        (delete-process (epg-context-process context))))))
1336
1337 (defun epg--status-*SIG (context status string)
1338   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1339       (let* ((key-id (match-string 1 string))
1340              (user-id (match-string 2 string))
1341              (entry (assoc key-id epg-user-id-alist)))
1342         (epg-context-set-result-for
1343          context
1344          'verify
1345          (cons (epg-make-signature status key-id)
1346                (epg-context-result-for context 'verify)))
1347         (condition-case nil
1348             (if (eq (epg-context-protocol context) 'CMS)
1349                 (setq user-id (epg-dn-from-string user-id))
1350               (setq user-id (epg--decode-coding-string
1351                              (epg--decode-percent-escape user-id)
1352                              'utf-8)))
1353           (error))
1354         (if entry
1355             (setcdr entry user-id)
1356           (setq epg-user-id-alist
1357                 (cons (cons key-id user-id) epg-user-id-alist))))
1358     (epg-context-set-result-for
1359      context
1360      'verify
1361      (cons (epg-make-signature status)
1362            (epg-context-result-for context 'verify)))))
1363
1364 (defun epg--status-GOODSIG (context string)
1365   (epg--status-*SIG context 'good string))
1366
1367 (defun epg--status-EXPSIG (context string)
1368   (epg--status-*SIG context 'expired string))
1369
1370 (defun epg--status-EXPKEYSIG (context string)
1371   (epg--status-*SIG context 'expired-key string))
1372
1373 (defun epg--status-REVKEYSIG (context string)
1374   (epg--status-*SIG context 'revoked-key string))
1375
1376 (defun epg--status-BADSIG (context string)
1377   (epg--status-*SIG context 'bad string))
1378
1379 (defun epg--status-NO_PUBKEY (context string)
1380   (let ((signature (car (epg-context-result-for context 'verify))))
1381     (if (and signature
1382              (eq (epg-signature-status signature) 'error)
1383              (equal (epg-signature-key-id signature) string))
1384         (epg-signature-set-status signature 'no-pubkey))))
1385
1386 (defun epg--time-from-seconds (seconds)
1387   (let ((number-seconds (string-to-number (concat seconds ".0"))))
1388     (cons (floor (/ number-seconds 65536))
1389           (floor (mod number-seconds 65536)))))
1390
1391 (defun epg--status-ERRSIG (context string)
1392   (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1393 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
1394                     string)
1395       (let ((signature (epg-make-signature 'error)))
1396         (epg-context-set-result-for
1397          context
1398          'verify
1399          (cons signature
1400                (epg-context-result-for context 'verify)))
1401         (epg-signature-set-key-id
1402          signature
1403          (match-string 1 string))
1404         (epg-signature-set-pubkey-algorithm
1405          signature
1406          (string-to-number (match-string 2 string)))
1407         (epg-signature-set-digest-algorithm
1408          signature
1409          (string-to-number (match-string 3 string)))
1410         (epg-signature-set-class
1411          signature
1412          (string-to-number (match-string 4 string) 16))
1413         (epg-signature-set-creation-time
1414          signature
1415          (epg--time-from-seconds (match-string 5 string))))))
1416
1417 (defun epg--status-VALIDSIG (context string)
1418   (let ((signature (car (epg-context-result-for context 'verify))))
1419     (when (and signature
1420                (eq (epg-signature-status signature) 'good)
1421                (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
1422 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
1423 \\(.*\\)"
1424                            string))
1425       (epg-signature-set-fingerprint
1426        signature
1427        (match-string 1 string))
1428       (epg-signature-set-creation-time
1429        signature
1430        (epg--time-from-seconds (match-string 2 string)))
1431       (unless (equal (match-string 3 string) "0")
1432         (epg-signature-set-expiration-time
1433          signature
1434          (epg--time-from-seconds (match-string 3 string))))
1435       (epg-signature-set-version
1436        signature
1437        (string-to-number (match-string 4 string)))
1438       (epg-signature-set-pubkey-algorithm
1439        signature 
1440        (string-to-number (match-string 5 string)))
1441       (epg-signature-set-digest-algorithm
1442        signature
1443        (string-to-number (match-string 6 string)))
1444       (epg-signature-set-class
1445        signature
1446        (string-to-number (match-string 7 string) 16)))))
1447
1448 (defun epg--status-TRUST_UNDEFINED (context string)
1449   (let ((signature (car (epg-context-result-for context 'verify))))
1450     (if (and signature
1451              (eq (epg-signature-status signature) 'good))
1452         (epg-signature-set-validity signature 'undefined))))
1453
1454 (defun epg--status-TRUST_NEVER (context string)
1455   (let ((signature (car (epg-context-result-for context 'verify))))
1456     (if (and signature
1457              (eq (epg-signature-status signature) 'good))
1458         (epg-signature-set-validity signature 'never))))
1459
1460 (defun epg--status-TRUST_MARGINAL (context string)
1461   (let ((signature (car (epg-context-result-for context 'verify))))
1462     (if (and signature
1463              (eq (epg-signature-status signature) 'marginal))
1464         (epg-signature-set-validity signature 'marginal))))
1465
1466 (defun epg--status-TRUST_FULLY (context string)
1467   (let ((signature (car (epg-context-result-for context 'verify))))
1468     (if (and signature
1469              (eq (epg-signature-status signature) 'good))
1470         (epg-signature-set-validity signature 'full))))
1471
1472 (defun epg--status-TRUST_ULTIMATE (context string)
1473   (let ((signature (car (epg-context-result-for context 'verify))))
1474     (if (and signature
1475              (eq (epg-signature-status signature) 'good))
1476         (epg-signature-set-validity signature 'ultimate))))
1477
1478 (defun epg--status-NOTATION_NAME (context string)
1479   (let ((signature (car (epg-context-result-for context 'verify))))
1480     (if signature
1481         (epg-signature-set-notations
1482          signature
1483          (cons (epg-make-sig-notation string nil t nil)
1484                (epg-sig-notations signature))))))
1485
1486 (defun epg--status-NOTATION_DATA (context string)
1487   (let ((signature (car (epg-context-result-for context 'verify)))
1488         notation)
1489     (if (and signature
1490              (setq notation (car (epg-sig-notations signature))))
1491         (epg-sig-notation-set-value notation string))))
1492
1493 (defun epg--status-POLICY_URL (context string)
1494   (let ((signature (car (epg-context-result-for context 'verify))))
1495     (if signature
1496         (epg-signature-set-notations
1497          signature
1498          (cons (epg-make-sig-notation nil string t nil)
1499                (epg-sig-notations signature))))))
1500
1501 (defun epg--status-PROGRESS (context string)
1502   (if (and (epg-context-progress-callback context)
1503            (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1504                          string))
1505       (funcall (if (consp (epg-context-progress-callback context))
1506                    (car (epg-context-progress-callback context))
1507                  (epg-context-progress-callback context))
1508                context
1509                (match-string 1 string)
1510                (match-string 2 string)
1511                (string-to-number (match-string 3 string))
1512                (string-to-number (match-string 4 string))
1513                (if (consp (epg-context-progress-callback context))
1514                    (cdr (epg-context-progress-callback context))))))
1515
1516 (defun epg--status-ENC_TO (context string)
1517   (if (string-match "\\`\\([0-9A-Za-z]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
1518       (epg-context-set-result-for
1519        context 'encrypted-to
1520        (cons (list (match-string 1 string)
1521                    (string-to-number (match-string 2 string))
1522                    (string-to-number (match-string 3 string)))
1523              (epg-context-result-for context 'encrypted-to)))))
1524
1525 (defun epg--status-DECRYPTION_FAILED (context string)
1526   (epg-context-set-result-for context 'decryption-failed t))
1527
1528 (defun epg--status-DECRYPTION_OKAY (context string)
1529   (epg-context-set-result-for context 'decryption-okay t))
1530
1531 (defun epg--status-NODATA (context string)
1532   (epg-context-set-result-for
1533    context 'error
1534    (cons (cons 'no-data (string-to-number string))
1535          (epg-context-result-for context 'error))))
1536
1537 (defun epg--status-UNEXPECTED (context string)
1538   (epg-context-set-result-for
1539    context 'error
1540    (cons (cons 'unexpected (string-to-number string))
1541          (epg-context-result-for context 'error))))
1542
1543 (defun epg--status-KEYEXPIRED (context string)
1544   (epg-context-set-result-for
1545    context 'error
1546    (cons (list 'key-expired (cons 'expiration-time
1547                                   (epg--time-from-seconds string)))
1548          (epg-context-result-for context 'error))))
1549
1550 (defun epg--status-KEYREVOKED (context string)
1551   (epg-context-set-result-for
1552    context 'error
1553    (cons '(key-revoked)
1554          (epg-context-result-for context 'error))))
1555
1556 (defun epg--status-BADARMOR (context string)
1557   (epg-context-set-result-for
1558    context 'error
1559    (cons '(bad-armor)
1560          (epg-context-result-for context 'error))))
1561
1562 (defun epg--status-INV_RECP (context string)
1563   (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1564       (epg-context-set-result-for
1565        context 'error
1566        (cons (list 'invalid-recipient
1567                    (cons 'reason
1568                          (string-to-number (match-string 1 string)))
1569                    (cons 'requested-recipient
1570                          (match-string 2 string)))
1571              (epg-context-result-for context 'error)))))
1572
1573 (defun epg--status-NO_RECP (context string)
1574   (epg-context-set-result-for
1575    context 'error
1576    (cons '(no-recipients)
1577          (epg-context-result-for context 'error))))
1578
1579 (defun epg--status-DELETE_PROBLEM (context string)
1580   (if (string-match "\\`\\([0-9]+\\)" string)
1581       (epg-context-set-result-for
1582        context 'error
1583        (cons (cons 'delete-problem
1584                    (string-to-number (match-string 1 string)))
1585              (epg-context-result-for context 'error)))))
1586
1587 (defun epg--status-SIG_CREATED (context string)
1588   (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1589 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1590       (epg-context-set-result-for
1591        context 'sign
1592        (cons (epg-make-new-signature
1593               (cdr (assq (aref (match-string 1 string) 0)
1594                          epg-new-signature-type-alist))
1595               (string-to-number (match-string 2 string))
1596               (string-to-number (match-string 3 string))
1597               (string-to-number (match-string 4 string) 16)
1598               (epg--time-from-seconds (match-string 5 string))
1599               (substring string (match-end 0)))
1600              (epg-context-result-for context 'sign)))))
1601
1602 (defun epg--status-KEY_CREATED (context string)
1603   (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1604       (epg-context-set-result-for
1605        context 'generate-key
1606        (cons (list (cons 'type (string-to-char (match-string 1 string)))
1607                    (cons 'fingerprint (match-string 2 string)))
1608              (epg-context-result-for context 'generate-key)))))
1609
1610 (defun epg--status-KEY_NOT_CREATED (context string)
1611   (epg-context-set-result-for
1612    context 'error
1613    (cons '(key-not-created)
1614          (epg-context-result-for context 'error))))
1615
1616 (defun epg--status-IMPORTED (context string)
1617   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1618       (let* ((key-id (match-string 1 string))
1619              (user-id (match-string 2 string))
1620              (entry (assoc key-id epg-user-id-alist)))
1621         (condition-case nil
1622             (setq user-id (epg--decode-coding-string
1623                            (epg--decode-percent-escape user-id)
1624                            'utf-8))
1625           (error))
1626         (if entry
1627             (setcdr entry user-id)
1628           (setq epg-user-id-alist (cons (cons key-id user-id)
1629                                         epg-user-id-alist))))))
1630
1631 (defun epg--status-IMPORT_OK (context string)
1632   (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1633       (let ((reason (string-to-number (match-string 1 string))))
1634         (epg-context-set-result-for
1635          context 'import-status
1636          (cons (epg-make-import-status (if (match-beginning 2)
1637                                            (match-string 3 string))
1638                                        nil
1639                                        (/= (logand reason 1) 0)
1640                                        (/= (logand reason 2) 0)
1641                                        (/= (logand reason 4) 0)
1642                                        (/= (logand reason 8) 0)
1643                                        (/= (logand reason 16) 0))
1644                (epg-context-result-for context 'import-status))))))
1645
1646 (defun epg--status-IMPORT_PROBLEM (context string)
1647   (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1648       (epg-context-set-result-for
1649        context 'import-status
1650        (cons (epg-make-import-status
1651               (if (match-beginning 2)
1652                   (match-string 3 string))
1653               (string-to-number (match-string 1 string)))
1654              (epg-context-result-for context 'import-status)))))
1655
1656 (defun epg--status-IMPORT_RES (context string)
1657   (when (string-match "\\`\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1658 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1659 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
1660     (epg-context-set-result-for
1661      context 'import
1662      (epg-make-import-result (string-to-number (match-string 1 string))
1663                              (string-to-number (match-string 2 string))
1664                              (string-to-number (match-string 3 string))
1665                              (string-to-number (match-string 4 string))
1666                              (string-to-number (match-string 5 string))
1667                              (string-to-number (match-string 6 string))
1668                              (string-to-number (match-string 7 string))
1669                              (string-to-number (match-string 8 string))
1670                              (string-to-number (match-string 9 string))
1671                              (string-to-number (match-string 10 string))
1672                              (string-to-number (match-string 11 string))
1673                              (string-to-number (match-string 12 string))
1674                              (string-to-number (match-string 13 string))
1675                              (epg-context-result-for context 'import-status)))
1676     (epg-context-set-result-for context 'import-status nil)))
1677
1678 (defun epg-passphrase-callback-function (context key-id handback)
1679   (if (eq key-id 'SYM)
1680       (read-passwd "Passphrase for symmetric encryption: "
1681                    (eq (epg-context-operation context) 'encrypt))
1682     (read-passwd
1683      (if (eq key-id 'PIN)
1684         "Passphrase for PIN: "
1685        (let ((entry (assoc key-id epg-user-id-alist)))
1686          (if entry
1687              (format "Passphrase for %s %s: " key-id (cdr entry))
1688            (format "Passphrase for %s: " key-id)))))))
1689
1690 (make-obsolete 'epg-passphrase-callback-function
1691                'epa-passphrase-callback-function)
1692
1693 (defun epg--list-keys-1 (context name mode)
1694   (let ((args (append (if epg-gpg-home-directory
1695                           (list "--homedir" epg-gpg-home-directory))
1696                       '("--with-colons" "--no-greeting" "--batch"
1697                         "--with-fingerprint" "--with-fingerprint")
1698                       (unless (eq (epg-context-protocol context) 'CMS)
1699                         '("--fixed-list-mode"))))
1700         (list-keys-option (if (memq mode '(t secret))
1701                               "--list-secret-keys"
1702                             (if (memq mode '(nil public))
1703                                 "--list-keys"
1704                               "--list-sigs")))
1705         (coding-system-for-read 'binary)
1706         keys string field index)
1707     (if name
1708         (progn
1709           (unless (listp name)
1710             (setq name (list name)))
1711           (while name
1712             (setq args (append args (list list-keys-option (car name)))
1713                   name (cdr name))))
1714       (setq args (append args (list list-keys-option))))
1715     (with-temp-buffer
1716       (apply #'call-process
1717              (if (eq (epg-context-protocol context) 'CMS)
1718                  epg-gpgsm-program
1719                epg-gpg-program)
1720              nil (list t nil) nil args)
1721       (goto-char (point-min))
1722       (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1723         (setq keys (cons (make-vector 15 nil) keys)
1724               string (match-string 0)
1725               index 0
1726               field 0)
1727         (while (eq index
1728                    (string-match "\\([^:]+\\)?:" string index))
1729           (setq index (match-end 0))
1730           (aset (car keys) field (match-string 1 string))
1731           (setq field (1+ field))))
1732       (nreverse keys))))
1733
1734 (defun epg--make-sub-key-1 (line)
1735   (epg-make-sub-key
1736    (if (aref line 1)
1737        (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1738    (delq nil
1739          (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1740                  (aref line 11)))
1741    (member (aref line 0) '("sec" "ssb"))
1742    (string-to-number (aref line 3))
1743    (string-to-number (aref line 2))
1744    (aref line 4)
1745    (epg--time-from-seconds (aref line 5))
1746    (if (aref line 6)
1747        (epg--time-from-seconds (aref line 6)))))
1748
1749 ;;;###autoload
1750 (defun epg-list-keys (context &optional name mode)
1751   "Return a list of epg-key objects matched with NAME.
1752 If MODE is nil or 'public, only public keyring should be searched.
1753 If MODE is t or 'secret, only secret keyring should be searched. 
1754 Otherwise, only public keyring should be searched and the key
1755 signatures should be included.
1756 NAME is either a string or a list of strings."
1757   (let ((lines (epg--list-keys-1 context name mode))
1758         keys cert pointer pointer-1 index string)
1759     (while lines
1760       (cond
1761        ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1762         (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1763               keys (cons (epg-make-key
1764                           (if (aref (car lines) 8)
1765                               (cdr (assq (string-to-char (aref (car lines) 8))
1766                                          epg-key-validity-alist))))
1767                          keys))
1768         (epg-key-set-sub-key-list
1769          (car keys)
1770          (cons (epg--make-sub-key-1 (car lines))
1771                (epg-key-sub-key-list (car keys)))))
1772        ((member (aref (car lines) 0) '("sub" "ssb"))
1773         (epg-key-set-sub-key-list
1774          (car keys)
1775          (cons (epg--make-sub-key-1 (car lines))
1776                (epg-key-sub-key-list (car keys)))))
1777        ((equal (aref (car lines) 0) "uid")
1778         ;; Decode the UID name as a backslash escaped UTF-8 string,
1779         ;; generated by GnuPG/GpgSM.
1780         (setq string (copy-sequence (aref (car lines) 9))
1781               index 0)
1782         (while (string-match "\"" string index)
1783           (setq string (replace-match "\\\"" t t string)
1784                 index (1+ (match-end 0))))
1785         (condition-case nil
1786             (setq string (epg--decode-coding-string
1787                           (car (read-from-string (concat "\"" string "\"")))
1788                           'utf-8))
1789           (error
1790            (setq string (aref (car lines) 9))))
1791         (epg-key-set-user-id-list
1792          (car keys)
1793          (cons (epg-make-user-id
1794                 (if (aref (car lines) 1)
1795                     (cdr (assq (string-to-char (aref (car lines) 1))
1796                                epg-key-validity-alist)))
1797                 (if cert
1798                     (condition-case nil
1799                         (epg-dn-from-string string)
1800                       (error string))
1801                   string))
1802                (epg-key-user-id-list (car keys)))))
1803        ((equal (aref (car lines) 0) "fpr")
1804         (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1805                                      (aref (car lines) 9)))
1806        ((equal (aref (car lines) 0) "sig")
1807         (epg-user-id-set-signature-list
1808          (car (epg-key-user-id-list (car keys)))
1809          (cons
1810           (epg-make-key-signature
1811            (if (aref (car lines) 1)
1812                (cdr (assq (string-to-char (aref (car lines) 1))
1813                           epg-key-validity-alist)))
1814            (string-to-number (aref (car lines) 3))
1815            (aref (car lines) 4)
1816            (epg--time-from-seconds (aref (car lines) 5))
1817            (epg--time-from-seconds (aref (car lines) 6))
1818            (aref (car lines) 9)
1819            (string-to-number (aref (car lines) 10) 16)
1820            (eq (aref (aref (car lines) 10) 2) ?x))
1821           (epg-user-id-signature-list
1822            (car (epg-key-user-id-list (car keys))))))))
1823       (setq lines (cdr lines)))
1824     (setq keys (nreverse keys)
1825           pointer keys)
1826     (while pointer
1827       (epg-key-set-sub-key-list
1828        (car pointer)
1829        (nreverse (epg-key-sub-key-list (car pointer))))
1830       (setq pointer-1 (epg-key-set-user-id-list
1831                           (car pointer)
1832                           (nreverse (epg-key-user-id-list (car pointer)))))
1833       (while pointer-1
1834         (epg-user-id-set-signature-list
1835          (car pointer-1)
1836          (nreverse (epg-user-id-signature-list (car pointer-1))))
1837         (setq pointer-1 (cdr pointer-1)))
1838       (setq pointer (cdr pointer)))
1839     keys))
1840
1841 (eval-and-compile
1842   (if (fboundp 'make-temp-file)
1843       (defalias 'epg--make-temp-file 'make-temp-file)
1844     (defvar temporary-file-directory)
1845     ;; stolen from poe.el.
1846     (defun epg--make-temp-file (prefix)
1847       "Create a temporary file.
1848 The returned file name (created by appending some random characters at the end
1849 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1850 is guaranteed to point to a newly created empty file.
1851 You can then use `write-region' to write new data into the file."
1852       (let (tempdir tempfile)
1853         (setq prefix (expand-file-name prefix
1854                                        (if (featurep 'xemacs)
1855                                            (temp-directory)
1856                                          temporary-file-directory)))
1857         (unwind-protect
1858             (let (file)
1859               ;; First, create a temporary directory.
1860               (while (condition-case ()
1861                          (progn
1862                            (setq tempdir (make-temp-name
1863                                           (concat
1864                                            (file-name-directory prefix)
1865                                            "DIR")))
1866                            ;; return nil or signal an error.
1867                            (make-directory tempdir))
1868                        ;; let's try again.
1869                        (file-already-exists t)))
1870               (set-file-modes tempdir 448)
1871               ;; Second, create a temporary file in the tempdir.
1872               ;; There *is* a race condition between `make-temp-name'
1873               ;; and `write-region', but we don't care it since we are
1874               ;; in a private directory now.
1875               (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1876               (write-region "" nil tempfile nil 'silent)
1877               (set-file-modes tempfile 384)
1878               ;; Finally, make a hard-link from the tempfile.
1879               (while (condition-case ()
1880                          (progn
1881                            (setq file (make-temp-name prefix))
1882                            ;; return nil or signal an error.
1883                            (add-name-to-file tempfile file))
1884                        ;; let's try again.
1885                        (file-already-exists t)))
1886               file)
1887           ;; Cleanup the tempfile.
1888           (and tempfile
1889                (file-exists-p tempfile)
1890                (delete-file tempfile))
1891           ;; Cleanup the tempdir.
1892           (and tempdir
1893                (file-directory-p tempdir)
1894                (delete-directory tempdir)))))))
1895
1896 (defun epg--args-from-sig-notations (notations)
1897   (apply #'nconc
1898          (mapcar
1899           (lambda (notation)
1900             (if (and (epg-sig-notation-name notation)
1901                      (not (epg-sig-notation-human-readable notation)))
1902                 (error "Unreadable"))
1903             (if (epg-sig-notation-name notation)
1904                 (list "--sig-notation"
1905                       (if (epg-sig-notation-critical notation)
1906                           (concat "!" (epg-sig-notation-name notation)
1907                                   "=" (epg-sig-notation-value notation))
1908                         (concat (epg-sig-notation-name notation)
1909                                 "=" (epg-sig-notation-value notation))))
1910               (list "--sig-policy-url"
1911                     (if (epg-sig-notation-critical notation)
1912                         (concat "!" (epg-sig-notation-value notation))
1913                       (epg-sig-notation-value notation)))))
1914           notations)))
1915
1916 ;;;###autoload
1917 (defun epg-cancel (context)
1918   (if (buffer-live-p (process-buffer (epg-context-process context)))
1919       (save-excursion
1920         (set-buffer (process-buffer (epg-context-process context)))
1921         (epg-context-set-result-for
1922          epg-context 'error
1923          (cons '(quit)
1924                (epg-context-result-for epg-context 'error)))))
1925   (if (eq (process-status (epg-context-process context)) 'run)
1926       (delete-process (epg-context-process context))))
1927
1928 ;;;###autoload
1929 (defun epg-start-decrypt (context cipher)
1930   "Initiate a decrypt operation on CIPHER.
1931 CIPHER must be a file data object.
1932
1933 If you use this function, you will need to wait for the completion of
1934 `epg-gpg-program' by using `epg-wait-for-completion' and call
1935 `epg-reset' to clear a temporaly output file.
1936 If you are unsure, use synchronous version of this function
1937 `epg-decrypt-file' or `epg-decrypt-string' instead."
1938   (unless (epg-data-file cipher)
1939     (error "Not a file"))
1940   (epg-context-set-operation context 'decrypt)
1941   (epg-context-set-result context nil)
1942   (epg--start context (list "--decrypt" "--" (epg-data-file cipher)))
1943   ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1944   (unless (eq (epg-context-protocol context) 'CMS)
1945     (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1946
1947 (defun epg--check-error-for-decrypt (context)
1948   (if (epg-context-result-for context 'decryption-failed)
1949       (signal 'epg-error (list "Decryption failed")))
1950   (if (epg-context-result-for context 'no-secret-key)
1951       (signal 'epg-error
1952               (list "No secret key"
1953                     (epg-context-result-for context 'no-secret-key))))
1954     (unless (epg-context-result-for context 'decryption-okay)
1955       (let* ((error (epg-context-result-for context 'error)))
1956         (if (assq 'no-data error)
1957             (signal 'epg-error (list "No data")))
1958         (signal 'epg-error (list "Can't decrypt" error)))))
1959
1960 ;;;###autoload
1961 (defun epg-decrypt-file (context cipher plain)
1962   "Decrypt a file CIPHER and store the result to a file PLAIN.
1963 If PLAIN is nil, it returns the result as a string."
1964   (unwind-protect
1965       (progn
1966         (if plain
1967             (epg-context-set-output-file context plain)
1968           (epg-context-set-output-file context
1969                                        (epg--make-temp-file "epg-output")))
1970         (epg-start-decrypt context (epg-make-data-from-file cipher))
1971         (epg-wait-for-completion context)
1972         (epg--check-error-for-decrypt context)
1973         (unless plain
1974           (epg-read-output context)))
1975     (unless plain
1976       (epg-delete-output-file context))
1977     (epg-reset context)))
1978
1979 ;;;###autoload
1980 (defun epg-decrypt-string (context cipher)
1981   "Decrypt a string CIPHER and return the plain text."
1982   (let ((input-file (epg--make-temp-file "epg-input"))
1983         (coding-system-for-write 'binary))
1984     (unwind-protect
1985         (progn
1986           (write-region cipher nil input-file nil 'quiet)
1987           (epg-context-set-output-file context
1988                                        (epg--make-temp-file "epg-output"))
1989           (epg-start-decrypt context (epg-make-data-from-file input-file))
1990           (epg-wait-for-completion context)
1991           (epg--check-error-for-decrypt context)
1992           (epg-read-output context))
1993       (epg-delete-output-file context)
1994       (if (file-exists-p input-file)
1995           (delete-file input-file))
1996       (epg-reset context))))
1997
1998 ;;;###autoload
1999 (defun epg-start-verify (context signature &optional signed-text)
2000   "Initiate a verify operation on SIGNATURE.
2001 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
2002
2003 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
2004 For a normal or a cleartext signature, SIGNED-TEXT should be nil.
2005
2006 If you use this function, you will need to wait for the completion of
2007 `epg-gpg-program' by using `epg-wait-for-completion' and call
2008 `epg-reset' to clear a temporaly output file.
2009 If you are unsure, use synchronous version of this function
2010 `epg-verify-file' or `epg-verify-string' instead."
2011   (epg-context-set-operation context 'verify)
2012   (epg-context-set-result context nil)
2013   (if signed-text
2014       ;; Detached signature.
2015       (if (epg-data-file signed-text)
2016           (epg--start context (list "--verify" "--" (epg-data-file signature)
2017                                    (epg-data-file signed-text)))
2018         (epg--start context (list "--verify" "--" (epg-data-file signature)
2019                                   "-"))
2020         (if (eq (process-status (epg-context-process context)) 'run)
2021             (process-send-string (epg-context-process context)
2022                                  (epg-data-string signed-text)))
2023         (if (eq (process-status (epg-context-process context)) 'run)
2024             (process-send-eof (epg-context-process context))))
2025     ;; Normal (or cleartext) signature.
2026     (if (epg-data-file signature)
2027         (epg--start context (list "--" (epg-data-file signature)))
2028       (epg--start context '("-"))
2029       (if (eq (process-status (epg-context-process context)) 'run)
2030           (process-send-string (epg-context-process context)
2031                                (epg-data-string signature)))
2032       (if (eq (process-status (epg-context-process context)) 'run)
2033           (process-send-eof (epg-context-process context))))))
2034
2035 ;;;###autoload
2036 (defun epg-verify-file (context signature &optional signed-text plain)
2037   "Verify a file SIGNATURE.
2038 SIGNED-TEXT and PLAIN are also a file if they are specified.
2039
2040 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
2041 string.  For a normal or a cleartext signature, SIGNED-TEXT should be
2042 nil.  In the latter case, if PLAIN is specified, the plaintext is
2043 stored into the file after successful verification."
2044   (unwind-protect
2045       (progn
2046         (if plain
2047             (epg-context-set-output-file context plain)
2048           (epg-context-set-output-file context
2049                                        (epg--make-temp-file "epg-output")))
2050         (if signed-text
2051             (epg-start-verify context
2052                               (epg-make-data-from-file signature)
2053                               (epg-make-data-from-file signed-text))
2054           (epg-start-verify context
2055                             (epg-make-data-from-file signature)))
2056         (epg-wait-for-completion context)
2057         (unless plain
2058           (epg-read-output context)))
2059     (unless plain
2060       (epg-delete-output-file context))
2061     (epg-reset context)))
2062
2063 ;;;###autoload
2064 (defun epg-verify-string (context signature &optional signed-text)
2065   "Verify a string SIGNATURE.
2066 SIGNED-TEXT is a string if it is specified.
2067
2068 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
2069 string.  For a normal or a cleartext signature, SIGNED-TEXT should be
2070 nil.  In the latter case, this function returns the plaintext after
2071 successful verification."
2072   (let ((coding-system-for-write 'binary)
2073         input-file)
2074     (unwind-protect
2075         (progn
2076           (epg-context-set-output-file context
2077                                        (epg--make-temp-file "epg-output"))
2078           (if signed-text
2079               (progn
2080                 (setq input-file (epg--make-temp-file "epg-signature"))
2081                 (write-region signature nil input-file nil 'quiet)
2082                 (epg-start-verify context
2083                                   (epg-make-data-from-file input-file)
2084                                   (epg-make-data-from-string signed-text)))
2085             (epg-start-verify context (epg-make-data-from-string signature)))
2086           (epg-wait-for-completion context)
2087           (epg-read-output context))
2088       (epg-delete-output-file context)
2089       (if (and input-file
2090                (file-exists-p input-file))
2091           (delete-file input-file))
2092       (epg-reset context))))
2093
2094 ;;;###autoload
2095 (defun epg-start-sign (context plain &optional mode)
2096   "Initiate a sign operation on PLAIN.
2097 PLAIN is a data object.
2098
2099 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2100 If it is nil or 'normal, it makes a normal signature.
2101 Otherwise, it makes a cleartext signature.
2102
2103 If you use this function, you will need to wait for the completion of
2104 `epg-gpg-program' by using `epg-wait-for-completion' and call
2105 `epg-reset' to clear a temporaly output file.
2106 If you are unsure, use synchronous version of this function
2107 `epg-sign-file' or `epg-sign-string' instead."
2108   (epg-context-set-operation context 'sign)
2109   (epg-context-set-result context nil)
2110   (unless (memq mode '(t detached nil normal)) ;i.e. cleartext
2111     (epg-context-set-armor context nil)
2112     (epg-context-set-textmode context nil))
2113   (epg--start context
2114              (append (list (if (memq mode '(t detached))
2115                                "--detach-sign"
2116                              (if (memq mode '(nil normal))
2117                                  "--sign"
2118                                "--clearsign")))
2119                      (apply #'nconc
2120                             (mapcar
2121                              (lambda (signer)
2122                                (list "-u"
2123                                      (epg-sub-key-id
2124                                       (car (epg-key-sub-key-list signer)))))
2125                              (epg-context-signers context)))
2126                      (epg--args-from-sig-notations
2127                       (epg-context-sig-notations context))
2128                      (if (epg-data-file plain)
2129                          (list "--" (epg-data-file plain)))))
2130   ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2131   (unless (eq (epg-context-protocol context) 'CMS)
2132     (epg-wait-for-status context '("BEGIN_SIGNING")))
2133   (when (epg-data-string plain)
2134     (if (eq (process-status (epg-context-process context)) 'run)
2135         (process-send-string (epg-context-process context)
2136                              (epg-data-string plain)))
2137     (if (eq (process-status (epg-context-process context)) 'run)
2138         (process-send-eof (epg-context-process context)))))
2139
2140 ;;;###autoload
2141 (defun epg-sign-file (context plain signature &optional mode)
2142   "Sign a file PLAIN and store the result to a file SIGNATURE.
2143 If SIGNATURE is nil, it returns the result as a string.
2144 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2145 If it is nil or 'normal, it makes a normal signature.
2146 Otherwise, it makes a cleartext signature."
2147   (unwind-protect
2148       (progn
2149         (if signature
2150             (epg-context-set-output-file context signature)
2151           (epg-context-set-output-file context
2152                                        (epg--make-temp-file "epg-output")))
2153         (epg-start-sign context (epg-make-data-from-file plain) mode)
2154         (epg-wait-for-completion context)
2155         (unless (epg-context-result-for context 'sign)
2156           (if (epg-context-result-for context 'error)
2157               (error "Sign failed: %S"
2158                      (epg-context-result-for context 'error))
2159             (error "Sign failed")))
2160         (unless signature
2161           (epg-read-output context)))
2162     (unless signature
2163       (epg-delete-output-file context))
2164     (epg-reset context)))
2165
2166 ;;;###autoload
2167 (defun epg-sign-string (context plain &optional mode)
2168   "Sign a string PLAIN and return the output as string.
2169 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2170 If it is nil or 'normal, it makes a normal signature.
2171 Otherwise, it makes a cleartext signature."
2172   (let ((input-file
2173          (unless (or (eq (epg-context-protocol context) 'CMS)
2174                      (condition-case nil
2175                          (progn
2176                            (epg-check-configuration (epg-configuration))
2177                            t)
2178                        (error)))
2179            (epg--make-temp-file "epg-input")))
2180         (coding-system-for-write 'binary))
2181     (unwind-protect
2182         (progn
2183           (epg-context-set-output-file context
2184                                        (epg--make-temp-file "epg-output"))
2185           (if input-file
2186               (write-region plain nil input-file nil 'quiet))
2187           (epg-start-sign context
2188                           (if input-file
2189                               (epg-make-data-from-file input-file)
2190                             (epg-make-data-from-string plain))
2191                           mode)
2192           (epg-wait-for-completion context)
2193           (unless (epg-context-result-for context 'sign)
2194             (if (epg-context-result-for context 'error)
2195                 (error "Sign failed: %S"
2196                        (epg-context-result-for context 'error))
2197               (error "Sign failed")))
2198           (epg-read-output context))
2199       (epg-delete-output-file context)
2200       (if input-file
2201           (delete-file input-file))
2202       (epg-reset context))))
2203
2204 ;;;###autoload
2205 (defun epg-start-encrypt (context plain recipients
2206                                   &optional sign always-trust)
2207   "Initiate an encrypt operation on PLAIN.
2208 PLAIN is a data object.
2209 If RECIPIENTS is nil, it performs symmetric encryption.
2210
2211 If you use this function, you will need to wait for the completion of
2212 `epg-gpg-program' by using `epg-wait-for-completion' and call
2213 `epg-reset' to clear a temporaly output file.
2214 If you are unsure, use synchronous version of this function
2215 `epg-encrypt-file' or `epg-encrypt-string' instead."
2216   (epg-context-set-operation context 'encrypt)
2217   (epg-context-set-result context nil)
2218   (epg--start context
2219              (append (if always-trust '("--always-trust"))
2220                      (if recipients '("--encrypt") '("--symmetric"))
2221                      (if sign '("--sign"))
2222                      (if sign
2223                          (apply #'nconc
2224                                 (mapcar
2225                                  (lambda (signer)
2226                                    (list "-u"
2227                                          (epg-sub-key-id
2228                                           (car (epg-key-sub-key-list
2229                                                 signer)))))
2230                                  (epg-context-signers context))))
2231                      (if sign
2232                          (epg--args-from-sig-notations
2233                           (epg-context-sig-notations context)))
2234                      (apply #'nconc
2235                             (mapcar
2236                              (lambda (recipient)
2237                                (list "-r"
2238                                      (epg-sub-key-id
2239                                       (car (epg-key-sub-key-list recipient)))))
2240                              recipients))
2241                      (if (epg-data-file plain)
2242                          (list "--" (epg-data-file plain)))))
2243   ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2244   (unless (eq (epg-context-protocol context) 'CMS)
2245     (if sign
2246         (epg-wait-for-status context '("BEGIN_SIGNING"))
2247       (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
2248   (when (epg-data-string plain)
2249     (if (eq (process-status (epg-context-process context)) 'run)
2250         (process-send-string (epg-context-process context)
2251                              (epg-data-string plain)))
2252     (if (eq (process-status (epg-context-process context)) 'run)
2253         (process-send-eof (epg-context-process context)))))
2254
2255 ;;;###autoload
2256 (defun epg-encrypt-file (context plain recipients
2257                                  cipher &optional sign always-trust)
2258   "Encrypt a file PLAIN and store the result to a file CIPHER.
2259 If CIPHER is nil, it returns the result as a string.
2260 If RECIPIENTS is nil, it performs symmetric encryption."
2261   (unwind-protect
2262       (progn
2263         (if cipher
2264             (epg-context-set-output-file context cipher)
2265           (epg-context-set-output-file context
2266                                        (epg--make-temp-file "epg-output")))
2267         (epg-start-encrypt context (epg-make-data-from-file plain)
2268                            recipients sign always-trust)
2269         (epg-wait-for-completion context)
2270         (if (and sign
2271                  (not (epg-context-result-for context 'sign)))
2272             (if (epg-context-result-for context 'error)
2273                 (error "Sign failed: %S"
2274                        (epg-context-result-for context 'error))
2275                 (error "Sign failed")))
2276         (if (epg-context-result-for context 'error)
2277             (error "Encrypt failed: %S"
2278                    (epg-context-result-for context 'error)))
2279         (unless cipher
2280           (epg-read-output context)))
2281     (unless cipher
2282       (epg-delete-output-file context))
2283     (epg-reset context)))
2284
2285 ;;;###autoload
2286 (defun epg-encrypt-string (context plain recipients
2287                                    &optional sign always-trust)
2288   "Encrypt a string PLAIN.
2289 If RECIPIENTS is nil, it performs symmetric encryption."
2290   (let ((input-file
2291          (unless (or (not sign)
2292                      (eq (epg-context-protocol context) 'CMS)
2293                      (condition-case nil
2294                          (progn
2295                            (epg-check-configuration (epg-configuration))
2296                            t)
2297                        (error)))
2298            (epg--make-temp-file "epg-input")))
2299         (coding-system-for-write 'binary))
2300     (unwind-protect
2301         (progn
2302           (epg-context-set-output-file context
2303                                        (epg--make-temp-file "epg-output"))
2304           (if input-file
2305               (write-region plain nil input-file nil 'quiet))
2306           (epg-start-encrypt context
2307                              (if input-file
2308                                  (epg-make-data-from-file input-file)
2309                                (epg-make-data-from-string plain))
2310                              recipients sign always-trust)
2311           (epg-wait-for-completion context)
2312           (if (and sign
2313                    (not (epg-context-result-for context 'sign)))
2314               (if (epg-context-result-for context 'error)
2315                   (error "Sign failed: %S"
2316                          (epg-context-result-for context 'error))
2317                 (error "Sign failed")))
2318           (if (epg-context-result-for context 'error)
2319               (error "Encrypt failed: %S"
2320                      (epg-context-result-for context 'error)))
2321           (epg-read-output context))
2322       (epg-delete-output-file context)
2323       (if input-file
2324           (delete-file input-file))
2325       (epg-reset context))))
2326
2327 ;;;###autoload
2328 (defun epg-start-export-keys (context keys)
2329   "Initiate an export keys operation.
2330
2331 If you use this function, you will need to wait for the completion of
2332 `epg-gpg-program' by using `epg-wait-for-completion' and call
2333 `epg-reset' to clear a temporaly output file.
2334 If you are unsure, use synchronous version of this function
2335 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
2336   (epg-context-set-operation context 'export-keys)
2337   (epg-context-set-result context nil)
2338   (epg--start context (cons "--export"
2339                            (mapcar
2340                             (lambda (key)
2341                               (epg-sub-key-id
2342                                (car (epg-key-sub-key-list key))))
2343                             keys))))
2344
2345 ;;;###autoload
2346 (defun epg-export-keys-to-file (context keys file)
2347   "Extract public KEYS."
2348   (unwind-protect
2349       (progn
2350         (if file
2351             (epg-context-set-output-file context file)
2352           (epg-context-set-output-file context
2353                                        (epg--make-temp-file "epg-output")))
2354         (epg-start-export-keys context keys)
2355         (epg-wait-for-completion context)
2356         (if (epg-context-result-for context 'error)
2357             (error "Export keys failed: %S"
2358                    (epg-context-result-for context 'error)))
2359         (unless file
2360           (epg-read-output context)))
2361     (unless file
2362       (epg-delete-output-file context))
2363     (epg-reset context)))
2364
2365 ;;;###autoload
2366 (defun epg-export-keys-to-string (context keys)
2367   "Extract public KEYS and return them as a string."
2368   (epg-export-keys-to-file context keys nil))
2369
2370 ;;;###autoload
2371 (defun epg-start-import-keys (context keys)
2372   "Initiate an import keys operation.
2373 KEYS is a data object.
2374
2375 If you use this function, you will need to wait for the completion of
2376 `epg-gpg-program' by using `epg-wait-for-completion' and call
2377 `epg-reset' to clear a temporaly output file.
2378 If you are unsure, use synchronous version of this function
2379 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
2380   (epg-context-set-operation context 'import-keys)
2381   (epg-context-set-result context nil)
2382   (epg--start context (if (epg-data-file keys)
2383                           (list "--import" "--" (epg-data-file keys))
2384                         (list "--import")))
2385   (when (epg-data-string keys)
2386     (if (eq (process-status (epg-context-process context)) 'run)
2387         (process-send-string (epg-context-process context)
2388                              (epg-data-string keys)))
2389     (if (eq (process-status (epg-context-process context)) 'run)
2390         (process-send-eof (epg-context-process context)))))
2391
2392 (defun epg--import-keys-1 (context keys)
2393   (unwind-protect
2394       (progn
2395         (epg-start-import-keys context keys)
2396         (epg-wait-for-completion context)
2397         (if (epg-context-result-for context 'error)
2398             (error "Import keys failed: %S"
2399                    (epg-context-result-for context 'error))))
2400     (epg-reset context)))
2401
2402 ;;;###autoload
2403 (defun epg-import-keys-from-file (context keys)
2404   "Add keys from a file KEYS."
2405   (epg--import-keys-1 context (epg-make-data-from-file keys)))
2406
2407 ;;;###autoload
2408 (defun epg-import-keys-from-string (context keys)
2409   "Add keys from a string KEYS."
2410   (epg--import-keys-1 context (epg-make-data-from-string keys)))
2411
2412 ;;;###autoload
2413 (defun epg-start-receive-keys (context key-id-list)
2414   "Initiate a receive key operation.
2415 KEY-ID-LIST is a list of key IDs.
2416
2417 If you use this function, you will need to wait for the completion of
2418 `epg-gpg-program' by using `epg-wait-for-completion' and call
2419 `epg-reset' to clear a temporaly output file.
2420 If you are unsure, use synchronous version of this function
2421 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2422   (epg-context-set-operation context 'receive-keys)
2423   (epg-context-set-result context nil)
2424   (epg--start context (cons "--recv-keys" key-id-list)))
2425
2426 ;;;###autoload
2427 (defun epg-receive-keys (context keys)
2428   "Add keys from server.
2429 KEYS is a list of key IDs"
2430   (unwind-protect
2431       (progn
2432         (epg-start-receive-keys context keys)
2433         (epg-wait-for-completion context)
2434         (if (epg-context-result-for context 'error)
2435             (error "Receive keys failed: %S"
2436                    (epg-context-result-for context 'error))))
2437     (epg-reset context)))
2438
2439 ;;;###autoload
2440 (defalias 'epg-import-keys-from-server 'epg-receive-keys)
2441
2442 ;;;###autoload
2443 (defun epg-start-delete-keys (context keys &optional allow-secret)
2444   "Initiate an delete keys operation.
2445
2446 If you use this function, you will need to wait for the completion of
2447 `epg-gpg-program' by using `epg-wait-for-completion' and call
2448 `epg-reset' to clear a temporaly output file.
2449 If you are unsure, use synchronous version of this function
2450 `epg-delete-keys' instead."
2451   (epg-context-set-operation context 'delete-keys)
2452   (epg-context-set-result context nil)
2453   (epg--start context (cons (if allow-secret
2454                                "--delete-secret-key"
2455                              "--delete-key")
2456                             (mapcar
2457                              (lambda (key)
2458                                (epg-sub-key-id
2459                                 (car (epg-key-sub-key-list key))))
2460                              keys))))
2461
2462 ;;;###autoload
2463 (defun epg-delete-keys (context keys &optional allow-secret)
2464   "Delete KEYS from the key ring."
2465   (unwind-protect
2466       (progn
2467         (epg-start-delete-keys context keys allow-secret)
2468         (epg-wait-for-completion context)
2469         (let ((entry (assq 'delete-problem
2470                            (epg-context-result-for context 'error))))
2471           (if entry
2472               (if (setq entry (assq (cdr entry)
2473                                     epg-delete-problem-reason-alist))
2474                   (error "Delete keys failed: %s" (cdr entry))
2475                 (error "Delete keys failed")))))
2476     (epg-reset context)))
2477
2478 ;;;###autoload
2479 (defun epg-start-sign-keys (context keys &optional local)
2480   "Initiate a sign keys operation.
2481
2482 If you use this function, you will need to wait for the completion of
2483 `epg-gpg-program' by using `epg-wait-for-completion' and call
2484 `epg-reset' to clear a temporaly output file.
2485 If you are unsure, use synchronous version of this function
2486 `epg-sign-keys' instead."
2487   (epg-context-set-operation context 'sign-keys)
2488   (epg-context-set-result context nil)
2489   (epg--start context (cons (if local
2490                                "--lsign-key"
2491                              "--sign-key")
2492                            (mapcar
2493                             (lambda (key)
2494                               (epg-sub-key-id
2495                                (car (epg-key-sub-key-list key))))
2496                             keys))))
2497 (make-obsolete 'epg-start-sign-keys "Do not use.")
2498
2499 ;;;###autoload
2500 (defun epg-sign-keys (context keys &optional local)
2501   "Sign KEYS from the key ring."
2502   (unwind-protect
2503       (progn
2504         (epg-start-sign-keys context keys local)
2505         (epg-wait-for-completion context)
2506         (if (epg-context-result-for context 'error)
2507             (error "Sign keys failed: %S"
2508                    (epg-context-result-for context 'error))))
2509     (epg-reset context)))
2510 (make-obsolete 'epg-sign-keys "Do not use.")
2511
2512 ;;;###autoload
2513 (defun epg-start-generate-key (context parameters)
2514   "Initiate a key generation.
2515 PARAMETERS specifies parameters for the key.
2516
2517 If you use this function, you will need to wait for the completion of
2518 `epg-gpg-program' by using `epg-wait-for-completion' and call
2519 `epg-reset' to clear a temporaly output file.
2520 If you are unsure, use synchronous version of this function
2521 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2522   (epg-context-set-operation context 'generate-key)
2523   (epg-context-set-result context nil)
2524   (if (epg-data-file parameters)
2525       (epg--start context (list "--batch" "--genkey" "--"
2526                                (epg-data-file parameters)))
2527     (epg--start context '("--batch" "--genkey"))
2528     (if (eq (process-status (epg-context-process context)) 'run)
2529         (process-send-string (epg-context-process context)
2530                              (epg-data-string parameters)))
2531     (if (eq (process-status (epg-context-process context)) 'run)
2532         (process-send-eof (epg-context-process context)))))
2533
2534 ;;;###autoload
2535 (defun epg-generate-key-from-file (context parameters)
2536   "Generate a new key pair.
2537 PARAMETERS is a file which tells how to create the key."
2538   (unwind-protect
2539       (progn
2540         (epg-start-generate-key context (epg-make-data-from-file parameters))
2541         (epg-wait-for-completion context)
2542         (if (epg-context-result-for context 'error)
2543             (error "Generate key failed: %S"
2544                    (epg-context-result-for context 'error))))
2545     (epg-reset context)))
2546
2547 ;;;###autoload
2548 (defun epg-generate-key-from-string (context parameters)
2549   "Generate a new key pair.
2550 PARAMETERS is a string which tells how to create the key."
2551   (unwind-protect
2552       (progn
2553         (epg-start-generate-key context (epg-make-data-from-string parameters))
2554         (epg-wait-for-completion context)
2555         (if (epg-context-result-for context 'error)
2556             (error "Generate key failed: %S"
2557                    (epg-context-result-for context 'error))))
2558     (epg-reset context)))
2559
2560 (defun epg--decode-percent-escape (string)
2561   (let ((index 0))
2562     (while (string-match "%\\(\\(%\\)\\|\\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2563                          string index)
2564       (if (match-beginning 2)
2565           (setq string (replace-match "%" t t string)
2566                 index (1- (match-end 0)))
2567         (setq string (replace-match
2568                       (string (string-to-number (match-string 3 string) 16))
2569                       t t string)
2570               index (- (match-end 0) 2))))
2571     string))
2572
2573 (defun epg--decode-hexstring (string)
2574   (let ((index 0))
2575     (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
2576       (setq string (replace-match (string (string-to-number
2577                                            (match-string 0 string) 16))
2578                                   t t string)
2579             index (1- (match-end 0))))
2580     string))
2581
2582 (defun epg--decode-quotedstring (string)
2583   (let ((index 0))
2584     (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2585 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2586                          string index)
2587       (if (match-beginning 2)
2588           (setq string (replace-match "\\2" t nil string)
2589                 index (1- (match-end 0)))
2590         (if (match-beginning 3)
2591             (setq string (replace-match (string (string-to-number
2592                                                  (match-string 0 string) 16))
2593                                         t t string)
2594                   index (- (match-end 0) 2)))))
2595     string))
2596
2597 (defun epg-dn-from-string (string)
2598   "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2599 The return value is an alist mapping from types to values."
2600   (let ((index 0)
2601         (length (length string))
2602         alist type value group)
2603     (while (< index length)
2604       (if (eq index (string-match "[ \t\n\r]*" string index))
2605           (setq index (match-end 0)))
2606       (if (eq index (string-match
2607                      "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2608                      string index))
2609           (setq type (match-string 1 string)
2610                 index (match-end 0))
2611         (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2612                                     string index))
2613             (setq type (match-string 1 string)
2614                   index (match-end 0))))
2615       (unless type
2616         (error "Invalid type"))
2617       (if (eq index (string-match
2618                      "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2619                      string index))
2620           (setq index (match-end 0)
2621                 value (epg--decode-quotedstring (match-string 0 string)))
2622         (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
2623             (setq index (match-end 0)
2624                   value (epg--decode-hexstring (match-string 1 string)))
2625           (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2626                                       string index))
2627               (setq index (match-end 0)
2628                     value (epg--decode-quotedstring
2629                            (match-string 0 string))))))
2630       (if group
2631           (if (stringp (car (car alist)))
2632               (setcar alist (list (cons type value) (car alist)))
2633             (setcar alist (cons (cons type value) (car alist))))
2634         (if (consp (car (car alist)))
2635             (setcar alist (nreverse (car alist))))
2636         (setq alist (cons (cons type value) alist)
2637               type nil
2638               value nil))
2639       (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
2640           (setq index (match-end 0)
2641                 group (eq (aref string (match-beginning 1)) ?+))))
2642     (nreverse alist)))
2643
2644 (defun epg-decode-dn (alist)
2645   "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2646 Type names are resolved using `epg-dn-type-alist'."
2647   (mapconcat
2648    (lambda (rdn)
2649      (if (stringp (car rdn))
2650          (let ((entry (assoc (car rdn) epg-dn-type-alist)))
2651            (if entry
2652                (format "%s=%s" (cdr entry) (cdr rdn))
2653              (format "%s=%s" (car rdn) (cdr rdn))))
2654        (concat "(" (epg-decode-dn rdn) ")")))
2655    alist
2656    ", "))
2657
2658 (provide 'epg)
2659
2660 ;;; epg.el ends here