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