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