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