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      (epg-make-import-result (string-to-number (match-string 1 string))
1574                              (string-to-number (match-string 2 string))
1575                              (string-to-number (match-string 3 string))
1576                              (string-to-number (match-string 4 string))
1577                              (string-to-number (match-string 5 string))
1578                              (string-to-number (match-string 6 string))
1579                              (string-to-number (match-string 7 string))
1580                              (string-to-number (match-string 8 string))
1581                              (string-to-number (match-string 9 string))
1582                              (string-to-number (match-string 10 string))
1583                              (string-to-number (match-string 11 string))
1584                              (string-to-number (match-string 12 string))
1585                              (string-to-number (match-string 13 string))
1586                              (epg-context-result-for context 'import-status)))
1587     (epg-context-set-result-for context 'import-status nil)))
1588
1589 (defun epg-passphrase-callback-function (context key-id handback)
1590   (if (eq key-id 'SYM)
1591       (read-passwd "Passphrase for symmetric encryption: "
1592                    (eq (epg-context-operation context) 'encrypt))
1593     (read-passwd
1594      (if (eq key-id 'PIN)
1595         "Passphrase for PIN: "
1596        (let ((entry (assoc key-id epg-user-id-alist)))
1597          (if entry
1598              (format "Passphrase for %s %s: " key-id (cdr entry))
1599            (format "Passphrase for %s: " key-id)))))))
1600
1601 (make-obsolete 'epg-passphrase-callback-function
1602                'epa-passphrase-callback-function)
1603
1604 (defun epg--list-keys-1 (context name mode)
1605   (let ((args (append (if epg-gpg-home-directory
1606                           (list "--homedir" epg-gpg-home-directory))
1607                       (list "--with-colons" "--no-greeting" "--batch"
1608                             "--with-fingerprint"
1609                             "--with-fingerprint"
1610                             (if (memq mode '(t secret))
1611                                 "--list-secret-keys"
1612                               (if (memq mode '(nil public))
1613                                   "--list-keys"
1614                                 "--list-sigs")))
1615                       (unless (eq (epg-context-protocol context) 'CMS)
1616                         '("--fixed-list-mode"))
1617                       (if name (list name))))
1618         keys string field index)
1619     (with-temp-buffer
1620       (apply #'call-process
1621              (if (eq (epg-context-protocol context) 'CMS)
1622                  epg-gpgsm-program
1623                epg-gpg-program)
1624              nil (list t nil) nil args)
1625       (goto-char (point-min))
1626       (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1627         (setq keys (cons (make-vector 15 nil) keys)
1628               string (match-string 0)
1629               index 0
1630               field 0)
1631         (while (eq index
1632                    (string-match "\\([^:]+\\)?:" string index))
1633           (setq index (match-end 0))
1634           (aset (car keys) field (match-string 1 string))
1635           (setq field (1+ field))))
1636       (nreverse keys))))
1637
1638 (defun epg--make-sub-key-1 (line)
1639   (epg-make-sub-key
1640    (if (aref line 1)
1641        (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1642    (delq nil
1643          (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1644                  (aref line 11)))
1645    (member (aref line 0) '("sec" "ssb"))
1646    (string-to-number (aref line 3))
1647    (string-to-number (aref line 2))
1648    (aref line 4)
1649    (epg--time-from-seconds (aref line 5))
1650    (epg--time-from-seconds (aref line 6))))
1651
1652 ;;;###autoload
1653 (defun epg-list-keys (context &optional name mode)
1654   "Return a list of epg-key objects matched with NAME.
1655 If MODE is nil or 'public, only public keyring should be searched.
1656 If MODE is t or 'secret, only secret keyring should be searched. 
1657 Otherwise, only public keyring should be searched and the key
1658 signatures should be included."
1659   (let ((lines (epg--list-keys-1 context name mode))
1660         keys cert pointer pointer-1)
1661     (while lines
1662       (cond
1663        ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1664         (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1665               keys (cons (epg-make-key
1666                           (if (aref (car lines) 8)
1667                               (cdr (assq (string-to-char (aref (car lines) 8))
1668                                          epg-key-validity-alist))))
1669                          keys))
1670         (epg-key-set-sub-key-list
1671          (car keys)
1672          (cons (epg--make-sub-key-1 (car lines))
1673                (epg-key-sub-key-list (car keys)))))
1674        ((member (aref (car lines) 0) '("sub" "ssb"))
1675         (epg-key-set-sub-key-list
1676          (car keys)
1677          (cons (epg--make-sub-key-1 (car lines))
1678                (epg-key-sub-key-list (car keys)))))
1679        ((equal (aref (car lines) 0) "uid")
1680         (epg-key-set-user-id-list
1681          (car keys)
1682          (cons (epg-make-user-id
1683                 (if (aref (car lines) 1)
1684                     (cdr (assq (string-to-char (aref (car lines) 1))
1685                                epg-key-validity-alist)))
1686                 (if cert
1687                     (condition-case nil
1688                         (epg-dn-from-string (aref (car lines) 9))
1689                       (error (aref (car lines) 9)))
1690                   (aref (car lines) 9)))
1691                (epg-key-user-id-list (car keys)))))
1692        ((equal (aref (car lines) 0) "fpr")
1693         (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1694                                      (aref (car lines) 9)))
1695        ((equal (aref (car lines) 0) "sig")
1696         (epg-user-id-set-signature-list
1697          (car (epg-key-user-id-list (car keys)))
1698          (cons
1699           (epg-make-key-signature
1700            (if (aref (car lines) 1)
1701                (cdr (assq (string-to-char (aref (car lines) 1))
1702                           epg-key-validity-alist)))
1703            (string-to-number (aref (car lines) 3))
1704            (aref (car lines) 4)
1705            (epg--time-from-seconds (aref (car lines) 5))
1706            (epg--time-from-seconds (aref (car lines) 6))
1707            (aref (car lines) 9)
1708            (string-to-number (aref (car lines) 10) 16)
1709            (eq (aref (aref (car lines) 10) 2) ?x))
1710           (epg-user-id-signature-list
1711            (car (epg-key-user-id-list (car keys))))))))
1712       (setq lines (cdr lines)))
1713     (setq keys (nreverse keys)
1714           pointer keys)
1715     (while pointer
1716       (epg-key-set-sub-key-list
1717        (car pointer)
1718        (nreverse (epg-key-sub-key-list (car pointer))))
1719       (setq pointer-1 (epg-key-set-user-id-list
1720                           (car pointer)
1721                           (nreverse (epg-key-user-id-list (car pointer)))))
1722       (while pointer-1
1723         (epg-user-id-set-signature-list
1724          (car pointer-1)
1725          (nreverse (epg-user-id-signature-list (car pointer-1))))
1726         (setq pointer-1 (cdr pointer-1)))
1727       (setq pointer (cdr pointer)))
1728     keys))
1729
1730 (if (fboundp 'make-temp-file)
1731     (defalias 'epg--make-temp-file 'make-temp-file)
1732   (defvar temporary-file-directory)
1733   ;; stolen from poe.el.
1734   (defun epg--make-temp-file (prefix)
1735     "Create a temporary file.
1736 The returned file name (created by appending some random characters at the end
1737 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1738 is guaranteed to point to a newly created empty file.
1739 You can then use `write-region' to write new data into the file."
1740     (let (tempdir tempfile)
1741       (setq prefix (expand-file-name prefix
1742                                      (if (featurep 'xemacs)
1743                                          (temp-directory)
1744                                        temporary-file-directory)))
1745       (unwind-protect
1746           (let (file)
1747             ;; First, create a temporary directory.
1748             (while (condition-case ()
1749                        (progn
1750                          (setq tempdir (make-temp-name
1751                                         (concat
1752                                          (file-name-directory prefix)
1753                                          "DIR")))
1754                          ;; return nil or signal an error.
1755                          (make-directory tempdir))
1756                      ;; let's try again.
1757                      (file-already-exists t)))
1758             (set-file-modes tempdir 448)
1759             ;; Second, create a temporary file in the tempdir.
1760             ;; There *is* a race condition between `make-temp-name'
1761             ;; and `write-region', but we don't care it since we are
1762             ;; in a private directory now.
1763             (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1764             (write-region "" nil tempfile nil 'silent)
1765             (set-file-modes tempfile 384)
1766             ;; Finally, make a hard-link from the tempfile.
1767             (while (condition-case ()
1768                        (progn
1769                          (setq file (make-temp-name prefix))
1770                          ;; return nil or signal an error.
1771                          (add-name-to-file tempfile file))
1772                      ;; let's try again.
1773                      (file-already-exists t)))
1774             file)
1775         ;; Cleanup the tempfile.
1776         (and tempfile
1777              (file-exists-p tempfile)
1778              (delete-file tempfile))
1779         ;; Cleanup the tempdir.
1780         (and tempdir
1781              (file-directory-p tempdir)
1782              (delete-directory tempdir))))))
1783
1784 (if (fboundp 'clear-string)
1785     (defalias 'epg--clear-string 'clear-string)
1786   (defun epg--clear-string (string)
1787     (fillarray string 0)))
1788
1789 (defun epg--args-from-sig-notations (notations)
1790   (apply #'nconc
1791          (mapcar
1792           (lambda (notation)
1793             (if (and (epg-sig-notation-name notation)
1794                      (not (epg-sig-notation-human-readable notation)))
1795                 (error "Unreadable"))
1796             (if (epg-sig-notation-name notation)
1797                 (list "--sig-notation"
1798                       (if (epg-sig-notation-critical notation)
1799                           (concat "!" (epg-sig-notation-name notation)
1800                                   "=" (epg-sig-notation-value notation))
1801                         (concat (epg-sig-notation-name notation)
1802                                 "=" (epg-sig-notation-value notation))))
1803               (list "--sig-policy-url"
1804                     (if (epg-sig-notation-critical notation)
1805                         (concat "!" (epg-sig-notation-value notation))
1806                       (epg-sig-notation-value notation)))))
1807           notations)))
1808
1809 ;;;###autoload
1810 (defun epg-cancel (context)
1811   (if (buffer-live-p (process-buffer (epg-context-process context)))
1812       (save-excursion
1813         (set-buffer (process-buffer (epg-context-process context)))
1814         (epg-context-set-result-for
1815          epg-context 'error
1816          (cons '(quit)
1817                (epg-context-result-for epg-context 'error)))))
1818   (if (eq (process-status (epg-context-process context)) 'run)
1819       (delete-process (epg-context-process context))))
1820   
1821 ;;;###autoload
1822 (defun epg-start-decrypt (context cipher)
1823   "Initiate a decrypt operation on CIPHER.
1824 CIPHER must be a file data object.
1825
1826 If you use this function, you will need to wait for the completion of
1827 `epg-gpg-program' by using `epg-wait-for-completion' and call
1828 `epg-reset' to clear a temporaly output file.
1829 If you are unsure, use synchronous version of this function
1830 `epg-decrypt-file' or `epg-decrypt-string' instead."
1831   (unless (epg-data-file cipher)
1832     (error "Not a file"))
1833   (epg-context-set-operation context 'decrypt)
1834   (epg-context-set-result context nil)
1835   (epg--start context (list "--decrypt" "--" (epg-data-file cipher)))
1836   ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1837   (unless (eq (epg-context-protocol context) 'CMS)
1838     (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1839
1840 ;;;###autoload
1841 (defun epg-decrypt-file (context cipher plain)
1842   "Decrypt a file CIPHER and store the result to a file PLAIN.
1843 If PLAIN is nil, it returns the result as a string."
1844   (unwind-protect
1845       (progn
1846         (if plain
1847             (epg-context-set-output-file context plain)
1848           (epg-context-set-output-file context
1849                                        (epg--make-temp-file "epg-output")))
1850         (epg-start-decrypt context (epg-make-data-from-file cipher))
1851         (epg-wait-for-completion context)
1852         (if (epg-context-result-for context 'error)
1853             (error "Decrypt failed: %S"
1854                    (epg-context-result-for context 'error)))
1855         (unless plain
1856           (epg-read-output context)))
1857     (unless plain
1858       (epg-delete-output-file context))
1859     (epg-reset context)))
1860
1861 ;;;###autoload
1862 (defun epg-decrypt-string (context cipher)
1863   "Decrypt a string CIPHER and return the plain text."
1864   (let ((input-file (epg--make-temp-file "epg-input"))
1865         (coding-system-for-write 'binary))
1866     (unwind-protect
1867         (progn
1868           (write-region cipher nil input-file nil 'quiet)
1869           (epg-context-set-output-file context
1870                                        (epg--make-temp-file "epg-output"))
1871           (epg-start-decrypt context (epg-make-data-from-file input-file))
1872           (epg-wait-for-completion context)
1873           (if (epg-context-result-for context 'error)
1874               (error "Decrypt failed: %S"
1875                      (epg-context-result-for context 'error)))
1876           (epg-read-output context))
1877       (epg-delete-output-file context)
1878       (if (file-exists-p input-file)
1879           (delete-file input-file))
1880       (epg-reset context))))
1881
1882 ;;;###autoload
1883 (defun epg-start-verify (context signature &optional signed-text)
1884   "Initiate a verify operation on SIGNATURE.
1885 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1886
1887 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1888 For a normal or a cleartext signature, SIGNED-TEXT should be nil.
1889
1890 If you use this function, you will need to wait for the completion of
1891 `epg-gpg-program' by using `epg-wait-for-completion' and call
1892 `epg-reset' to clear a temporaly output file.
1893 If you are unsure, use synchronous version of this function
1894 `epg-verify-file' or `epg-verify-string' instead."
1895   (epg-context-set-operation context 'verify)
1896   (epg-context-set-result context nil)
1897   (if signed-text
1898       ;; Detached signature.
1899       (if (epg-data-file signed-text)
1900           (epg--start context (list "--verify" "--" (epg-data-file signature)
1901                                    (epg-data-file signed-text)))
1902         (epg--start context (list "--verify" "--" (epg-data-file signature)
1903                                   "-"))
1904         (if (eq (process-status (epg-context-process context)) 'run)
1905             (process-send-string (epg-context-process context)
1906                                  (epg-data-string signed-text)))
1907         (if (eq (process-status (epg-context-process context)) 'run)
1908             (process-send-eof (epg-context-process context))))
1909     ;; Normal (or cleartext) signature.
1910     (if (epg-data-file signature)
1911         (epg--start context (list "--" (epg-data-file signature)))
1912       (epg--start context '("-"))
1913       (if (eq (process-status (epg-context-process context)) 'run)
1914           (process-send-string (epg-context-process context)
1915                                (epg-data-string signature)))
1916       (if (eq (process-status (epg-context-process context)) 'run)
1917           (process-send-eof (epg-context-process context))))))
1918
1919 ;;;###autoload
1920 (defun epg-verify-file (context signature &optional signed-text plain)
1921   "Verify a file SIGNATURE.
1922 SIGNED-TEXT and PLAIN are also a file if they are specified.
1923
1924 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1925 string.  For a normal or a cleartext signature, SIGNED-TEXT should be
1926 nil.  In the latter case, if PLAIN is specified, the plaintext is
1927 stored into the file after successful verification."
1928   (unwind-protect
1929       (progn
1930         (if plain
1931             (epg-context-set-output-file context plain)
1932           (epg-context-set-output-file context
1933                                        (epg--make-temp-file "epg-output")))
1934         (if signed-text
1935             (epg-start-verify context
1936                               (epg-make-data-from-file signature)
1937                               (epg-make-data-from-file signed-text))
1938           (epg-start-verify context
1939                             (epg-make-data-from-file signature)))
1940         (epg-wait-for-completion context)
1941         (unless plain
1942           (epg-read-output context)))
1943     (unless plain
1944       (epg-delete-output-file context))
1945     (epg-reset context)))
1946
1947 ;;;###autoload
1948 (defun epg-verify-string (context signature &optional signed-text)
1949   "Verify a string SIGNATURE.
1950 SIGNED-TEXT is a string if it is specified.
1951
1952 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1953 string.  For a normal or a cleartext signature, SIGNED-TEXT should be
1954 nil.  In the latter case, this function returns the plaintext after
1955 successful verification."
1956   (let ((coding-system-for-write 'binary)
1957         input-file)
1958     (unwind-protect
1959         (progn
1960           (epg-context-set-output-file context
1961                                        (epg--make-temp-file "epg-output"))
1962           (if signed-text
1963               (progn
1964                 (setq input-file (epg--make-temp-file "epg-signature"))
1965                 (write-region signature nil input-file nil 'quiet)
1966                 (epg-start-verify context
1967                                   (epg-make-data-from-file input-file)
1968                                   (epg-make-data-from-string signed-text)))
1969             (epg-start-verify context (epg-make-data-from-string signature)))
1970           (epg-wait-for-completion context)
1971           (epg-read-output context))
1972       (epg-delete-output-file context)
1973       (if (and input-file
1974                (file-exists-p input-file))
1975           (delete-file input-file))
1976       (epg-reset context))))
1977
1978 ;;;###autoload
1979 (defun epg-start-sign (context plain &optional mode)
1980   "Initiate a sign operation on PLAIN.
1981 PLAIN is a data object.
1982
1983 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1984 If it is nil or 'normal, it makes a normal signature.
1985 Otherwise, it makes a cleartext signature.
1986
1987 If you use this function, you will need to wait for the completion of
1988 `epg-gpg-program' by using `epg-wait-for-completion' and call
1989 `epg-reset' to clear a temporaly output file.
1990 If you are unsure, use synchronous version of this function
1991 `epg-sign-file' or `epg-sign-string' instead."
1992   (epg-context-set-operation context 'sign)
1993   (epg-context-set-result context nil)
1994   (unless (memq mode '(t detached nil normal)) ;i.e. cleartext
1995     (epg-context-set-armor context nil)
1996     (epg-context-set-textmode context nil))
1997   (epg--start context
1998              (append (list (if (memq mode '(t detached))
1999                                "--detach-sign"
2000                              (if (memq mode '(nil normal))
2001                                  "--sign"
2002                                "--clearsign")))
2003                      (apply #'nconc
2004                             (mapcar
2005                              (lambda (signer)
2006                                (list "-u"
2007                                      (epg-sub-key-id
2008                                       (car (epg-key-sub-key-list signer)))))
2009                              (epg-context-signers context)))
2010                      (epg--args-from-sig-notations
2011                       (epg-context-sig-notations context))
2012                      (if (epg-data-file plain)
2013                          (list "--" (epg-data-file plain)))))
2014   ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2015   (unless (eq (epg-context-protocol context) 'CMS)
2016     (epg-wait-for-status context '("BEGIN_SIGNING")))
2017   (when (epg-data-string plain)
2018     (if (eq (process-status (epg-context-process context)) 'run)
2019         (process-send-string (epg-context-process context)
2020                              (epg-data-string plain)))
2021     (if (eq (process-status (epg-context-process context)) 'run)
2022         (process-send-eof (epg-context-process context)))))
2023
2024 ;;;###autoload
2025 (defun epg-sign-file (context plain signature &optional mode)
2026   "Sign a file PLAIN and store the result to a file SIGNATURE.
2027 If SIGNATURE is nil, it returns the result as a string.
2028 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2029 If it is nil or 'normal, it makes a normal signature.
2030 Otherwise, it makes a cleartext signature."
2031   (unwind-protect
2032       (progn
2033         (if signature
2034             (epg-context-set-output-file context signature)
2035           (epg-context-set-output-file context
2036                                        (epg--make-temp-file "epg-output")))
2037         (epg-start-sign context (epg-make-data-from-file plain) mode)
2038         (epg-wait-for-completion context)
2039         (unless (epg-context-result-for context 'sign)
2040           (if (epg-context-result-for context 'error)
2041               (error "Sign failed: %S"
2042                      (epg-context-result-for context 'error))
2043             (error "Sign failed")))
2044         (unless signature
2045           (epg-read-output context)))
2046     (unless signature
2047       (epg-delete-output-file context))
2048     (epg-reset context)))
2049
2050 ;;;###autoload
2051 (defun epg-sign-string (context plain &optional mode)
2052   "Sign a string PLAIN and return the output as string.
2053 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2054 If it is nil or 'normal, it makes a normal signature.
2055 Otherwise, it makes a cleartext signature."
2056   (unwind-protect
2057       (progn
2058         (epg-context-set-output-file context
2059                                      (epg--make-temp-file "epg-output"))
2060         (epg-start-sign context (epg-make-data-from-string plain) mode)
2061         (epg-wait-for-completion context)
2062         (unless (epg-context-result-for context 'sign)
2063           (if (epg-context-result-for context 'error)
2064               (error "Sign failed: %S"
2065                      (epg-context-result-for context 'error))
2066             (error "Sign failed")))
2067         (epg-read-output context))
2068     (epg-delete-output-file context)
2069     (epg-reset context)))
2070
2071 ;;;###autoload
2072 (defun epg-start-encrypt (context plain recipients
2073                                   &optional sign always-trust)
2074   "Initiate an encrypt operation on PLAIN.
2075 PLAIN is a data object.
2076 If RECIPIENTS is nil, it performs symmetric encryption.
2077
2078 If you use this function, you will need to wait for the completion of
2079 `epg-gpg-program' by using `epg-wait-for-completion' and call
2080 `epg-reset' to clear a temporaly output file.
2081 If you are unsure, use synchronous version of this function
2082 `epg-encrypt-file' or `epg-encrypt-string' instead."
2083   (epg-context-set-operation context 'encrypt)
2084   (epg-context-set-result context nil)
2085   (epg--start context
2086              (append (if always-trust '("--always-trust"))
2087                      (if recipients '("--encrypt") '("--symmetric"))
2088                      (if sign '("--sign"))
2089                      (if sign
2090                          (apply #'nconc
2091                                 (mapcar
2092                                  (lambda (signer)
2093                                    (list "-u"
2094                                          (epg-sub-key-id
2095                                           (car (epg-key-sub-key-list
2096                                                 signer)))))
2097                                  (epg-context-signers context))))
2098                      (if sign
2099                          (epg--args-from-sig-notations
2100                           (epg-context-sig-notations context)))
2101                      (apply #'nconc
2102                             (mapcar
2103                              (lambda (recipient)
2104                                (list "-r"
2105                                      (epg-sub-key-id
2106                                       (car (epg-key-sub-key-list recipient)))))
2107                              recipients))
2108                      (if (epg-data-file plain)
2109                          (list "--" (epg-data-file plain)))))
2110   ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2111   (unless (eq (epg-context-protocol context) 'CMS)
2112     (if sign
2113         (epg-wait-for-status context '("BEGIN_SIGNING"))
2114       (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
2115   (when (epg-data-string plain)
2116     (if (eq (process-status (epg-context-process context)) 'run)
2117         (process-send-string (epg-context-process context)
2118                              (epg-data-string plain)))
2119     (if (eq (process-status (epg-context-process context)) 'run)
2120         (process-send-eof (epg-context-process context)))))
2121
2122 ;;;###autoload
2123 (defun epg-encrypt-file (context plain recipients
2124                                  cipher &optional sign always-trust)
2125   "Encrypt a file PLAIN and store the result to a file CIPHER.
2126 If CIPHER is nil, it returns the result as a string.
2127 If RECIPIENTS is nil, it performs symmetric encryption."
2128   (unwind-protect
2129       (progn
2130         (if cipher
2131             (epg-context-set-output-file context cipher)
2132           (epg-context-set-output-file context
2133                                        (epg--make-temp-file "epg-output")))
2134         (epg-start-encrypt context (epg-make-data-from-file plain)
2135                            recipients sign always-trust)
2136         (epg-wait-for-completion context)
2137         (if (and sign
2138                  (not (epg-context-result-for context 'sign)))
2139             (if (epg-context-result-for context 'error)
2140                 (error "Sign failed: %S"
2141                        (epg-context-result-for context 'error))
2142                 (error "Sign failed")))
2143         (if (epg-context-result-for context 'error)
2144             (error "Encrypt failed: %S"
2145                    (epg-context-result-for context 'error)))
2146         (unless cipher
2147           (epg-read-output context)))
2148     (unless cipher
2149       (epg-delete-output-file context))
2150     (epg-reset context)))
2151
2152 ;;;###autoload
2153 (defun epg-encrypt-string (context plain recipients
2154                                    &optional sign always-trust)
2155   "Encrypt a string PLAIN.
2156 If RECIPIENTS is nil, it performs symmetric encryption."
2157   (unwind-protect
2158       (progn
2159         (epg-context-set-output-file context
2160                                      (epg--make-temp-file "epg-output"))
2161         (epg-start-encrypt context (epg-make-data-from-string plain)
2162                            recipients sign always-trust)
2163         (epg-wait-for-completion context)
2164         (if (and sign
2165                  (not (epg-context-result-for context 'sign)))
2166             (if (epg-context-result-for context 'error)
2167                 (error "Sign failed: %S"
2168                        (epg-context-result-for context 'error))
2169               (error "Sign failed")))
2170         (if (epg-context-result-for context 'error)
2171             (error "Encrypt failed: %S"
2172                    (epg-context-result-for context 'error)))
2173         (epg-read-output context))
2174     (epg-delete-output-file context)
2175     (epg-reset context)))
2176
2177 ;;;###autoload
2178 (defun epg-start-export-keys (context keys)
2179   "Initiate an export keys operation.
2180
2181 If you use this function, you will need to wait for the completion of
2182 `epg-gpg-program' by using `epg-wait-for-completion' and call
2183 `epg-reset' to clear a temporaly output file.
2184 If you are unsure, use synchronous version of this function
2185 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
2186   (epg-context-set-operation context 'export-keys)
2187   (epg-context-set-result context nil)
2188   (epg--start context (cons "--export"
2189                            (mapcar
2190                             (lambda (key)
2191                               (epg-sub-key-id
2192                                (car (epg-key-sub-key-list key))))
2193                             keys))))
2194
2195 ;;;###autoload
2196 (defun epg-export-keys-to-file (context keys file)
2197   "Extract public KEYS."
2198   (unwind-protect
2199       (progn
2200         (if keys
2201             (epg-context-set-output-file context file)
2202           (epg-context-set-output-file context
2203                                        (epg--make-temp-file "epg-output")))
2204         (epg-start-export-keys context keys)
2205         (epg-wait-for-completion context)
2206         (if (epg-context-result-for context 'error)
2207             (error "Export keys failed: %S"
2208                    (epg-context-result-for context 'error)))
2209         (unless file
2210           (epg-read-output context)))
2211     (unless file
2212       (epg-delete-output-file context))
2213     (epg-reset context)))
2214
2215 ;;;###autoload
2216 (defun epg-export-keys-to-string (context keys)
2217   "Extract public KEYS and return them as a string."
2218   (epg-export-keys-to-file context keys nil))
2219
2220 ;;;###autoload
2221 (defun epg-start-import-keys (context keys)
2222   "Initiate an import keys operation.
2223 KEYS is a data object.
2224
2225 If you use this function, you will need to wait for the completion of
2226 `epg-gpg-program' by using `epg-wait-for-completion' and call
2227 `epg-reset' to clear a temporaly output file.
2228 If you are unsure, use synchronous version of this function
2229 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
2230   (epg-context-set-operation context 'import-keys)
2231   (epg-context-set-result context nil)
2232   (epg--start context (if (epg-data-file keys)
2233                           (list "--import" "--" (epg-data-file keys))
2234                         (list "--import")))
2235   (when (epg-data-string keys)
2236     (if (eq (process-status (epg-context-process context)) 'run)
2237         (process-send-string (epg-context-process context)
2238                              (epg-data-string keys)))
2239     (if (eq (process-status (epg-context-process context)) 'run)
2240         (process-send-eof (epg-context-process context)))))
2241
2242 (defun epg--import-keys-1 (context keys)
2243   (unwind-protect
2244       (progn
2245         (epg-start-import-keys context keys)
2246         (epg-wait-for-completion context)
2247         (if (epg-context-result-for context 'error)
2248             (error "Import keys failed: %S"
2249                    (epg-context-result-for context 'error))))
2250     (epg-reset context)))
2251
2252 ;;;###autoload
2253 (defun epg-import-keys-from-file (context keys)
2254   "Add keys from a file KEYS."
2255   (epg--import-keys-1 context (epg-make-data-from-file keys)))
2256
2257 ;;;###autoload
2258 (defun epg-import-keys-from-string (context keys)
2259   "Add keys from a string KEYS."
2260   (epg--import-keys-1 context (epg-make-data-from-string keys)))
2261
2262 ;;;###autoload
2263 (defun epg-start-receive-keys (context key-id-list)
2264   "Initiate a receive key operation.
2265 KEY-ID-LIST is a list of key IDs.
2266
2267 If you use this function, you will need to wait for the completion of
2268 `epg-gpg-program' by using `epg-wait-for-completion' and call
2269 `epg-reset' to clear a temporaly output file.
2270 If you are unsure, use synchronous version of this function
2271 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2272   (epg-context-set-operation context 'receive-keys)
2273   (epg-context-set-result context nil)
2274   (epg--start context (cons "--recv-keys" key-id-list)))
2275
2276 ;;;###autoload
2277 (defun epg-receive-keys (context keys)
2278   "Add keys from server.
2279 KEYS is a list of key IDs"
2280   (unwind-protect
2281       (progn
2282         (epg-start-receive-keys context keys)
2283         (epg-wait-for-completion context)
2284         (if (epg-context-result-for context 'error)
2285             (error "Receive keys failed: %S"
2286                    (epg-context-result-for context 'error))))
2287     (epg-reset context)))
2288
2289 ;;;###autoload
2290 (defalias 'epg-import-keys-from-server 'epg-receive-keys)
2291
2292 ;;;###autoload
2293 (defun epg-start-delete-keys (context keys &optional allow-secret)
2294   "Initiate an delete keys operation.
2295
2296 If you use this function, you will need to wait for the completion of
2297 `epg-gpg-program' by using `epg-wait-for-completion' and call
2298 `epg-reset' to clear a temporaly output file.
2299 If you are unsure, use synchronous version of this function
2300 `epg-delete-keys' instead."
2301   (epg-context-set-operation context 'delete-keys)
2302   (epg-context-set-result context nil)
2303   (epg--start context (cons (if allow-secret
2304                                "--delete-secret-key"
2305                              "--delete-key")
2306                            (mapcar
2307                             (lambda (key)
2308                               (epg-sub-key-id
2309                                (car (epg-key-sub-key-list key))))
2310                             keys))))
2311
2312 ;;;###autoload
2313 (defun epg-delete-keys (context keys &optional allow-secret)
2314   "Delete KEYS from the key ring."
2315   (unwind-protect
2316       (progn
2317         (epg-start-delete-keys context keys allow-secret)
2318         (epg-wait-for-completion context)
2319         (if (epg-context-result-for context 'error)
2320             (error "Delete keys failed: %S"
2321                    (epg-context-result-for context 'error))))
2322     (epg-reset context)))
2323
2324 ;;;###autoload
2325 (defun epg-start-sign-keys (context keys &optional local)
2326   "Initiate an sign keys operation.
2327
2328 If you use this function, you will need to wait for the completion of
2329 `epg-gpg-program' by using `epg-wait-for-completion' and call
2330 `epg-reset' to clear a temporaly output file.
2331 If you are unsure, use synchronous version of this function
2332 `epg-sign-keys' instead."
2333   (epg-context-set-operation context 'sign-keys)
2334   (epg-context-set-result context nil)
2335   (epg--start context (cons (if local
2336                                "--lsign-key"
2337                              "--sign-key")
2338                            (mapcar
2339                             (lambda (key)
2340                               (epg-sub-key-id
2341                                (car (epg-key-sub-key-list key))))
2342                             keys))))
2343 (make-obsolete 'epg-start-sign-keys "Do not use.")
2344
2345 ;;;###autoload
2346 (defun epg-sign-keys (context keys &optional local)
2347   "Sign KEYS from the key ring."
2348   (unwind-protect
2349       (progn
2350         (epg-start-sign-keys context keys local)
2351         (epg-wait-for-completion context)
2352         (if (epg-context-result-for context 'error)
2353             (error "Sign keys failed: %S"
2354                    (epg-context-result-for context 'error))))
2355     (epg-reset context)))
2356 (make-obsolete 'epg-sign-keys "Do not use.")
2357
2358 ;;;###autoload
2359 (defun epg-start-generate-key (context parameters)
2360   "Initiate a key generation.
2361 PARAMETERS specifies parameters for the key.
2362
2363 If you use this function, you will need to wait for the completion of
2364 `epg-gpg-program' by using `epg-wait-for-completion' and call
2365 `epg-reset' to clear a temporaly output file.
2366 If you are unsure, use synchronous version of this function
2367 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2368   (epg-context-set-operation context 'generate-key)
2369   (epg-context-set-result context nil)
2370   (if (epg-data-file parameters)
2371       (epg--start context (list "--batch" "--genkey" "--"
2372                                (epg-data-file parameters)))
2373     (epg--start context '("--batch" "--genkey"))
2374     (if (eq (process-status (epg-context-process context)) 'run)
2375         (process-send-string (epg-context-process context)
2376                              (epg-data-string parameters)))
2377     (if (eq (process-status (epg-context-process context)) 'run)
2378         (process-send-eof (epg-context-process context)))))
2379
2380 ;;;###autoload
2381 (defun epg-generate-key-from-file (context parameters)
2382   "Generate a new key pair.
2383 PARAMETERS is a file which tells how to create the key."
2384   (unwind-protect
2385       (progn
2386         (epg-start-generate-key context (epg-make-data-from-file parameters))
2387         (epg-wait-for-completion context)
2388         (if (epg-context-result-for context 'error)
2389             (error "Generate key failed: %S"
2390                    (epg-context-result-for context 'error))))
2391     (epg-reset context)))
2392
2393 ;;;###autoload
2394 (defun epg-generate-key-from-string (context parameters)
2395   "Generate a new key pair.
2396 PARAMETERS is a string which tells how to create the key."
2397   (unwind-protect
2398       (progn
2399         (epg-start-generate-key context (epg-make-data-from-string parameters))
2400         (epg-wait-for-completion context)
2401         (if (epg-context-result-for context 'error)
2402             (error "Generate key failed: %S"
2403                    (epg-context-result-for context 'error))))
2404     (epg-reset context)))
2405
2406 (defun epg--decode-hexstring (string)
2407   (let ((index 0))
2408     (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
2409       (setq string (replace-match "\\x\\&" t nil string)
2410             index (+ index 4)))
2411     (car (read-from-string (concat "\"" string "\"")))))
2412
2413 (defun epg--decode-quotedstring (string)
2414   (let ((index 0))
2415     (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2416 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
2417                          string index)
2418       (if (match-beginning 2)
2419           (setq string (replace-match "\\2" t nil string)
2420                 index (1+ index))
2421         (if (match-beginning 3)
2422             (setq string (replace-match "\\x\\3" t nil string)
2423                   index (+ index 4))
2424           (setq string (replace-match "\\\\\\\\\\4" t nil string)
2425                 index (+ index 3)))))
2426     (car (read-from-string (concat "\"" string "\"")))))
2427
2428 (defun epg-dn-from-string (string)
2429   "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2430 The return value is an alist mapping from types to values."
2431   (let ((index 0)
2432         (length (length string))
2433         alist type value group)
2434     (while (< index length)
2435       (if (eq index (string-match "[ \t\n\r]*" string index))
2436           (setq index (match-end 0)))
2437       (if (eq index (string-match
2438                      "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2439                      string index))
2440           (setq type (match-string 1 string)
2441                 index (match-end 0))
2442         (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2443                                     string index))
2444             (setq type (match-string 1 string)
2445                   index (match-end 0))))
2446       (unless type
2447         (error "Invalid type"))
2448       (if (eq index (string-match
2449                      "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2450                      string index))
2451           (setq index (match-end 0)
2452                 value (epg--decode-quotedstring (match-string 0 string)))
2453         (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
2454             (setq index (match-end 0)
2455                   value (epg--decode-hexstring (match-string 1 string)))
2456           (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2457                                       string index))
2458               (setq index (match-end 0)
2459                     value (epg--decode-quotedstring
2460                            (match-string 0 string))))))
2461       (if group
2462           (if (stringp (car (car alist)))
2463               (setcar alist (list (cons type value) (car alist)))
2464             (setcar alist (cons (cons type value) (car alist))))
2465         (if (consp (car (car alist)))
2466             (setcar alist (nreverse (car alist))))
2467         (setq alist (cons (cons type value) alist)
2468               type nil
2469               value nil))
2470       (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
2471           (setq index (match-end 0)
2472                 group (eq (aref string (match-beginning 1)) ?+))))
2473     (nreverse alist)))
2474
2475 (defun epg-decode-dn (alist)
2476   "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2477 Type names are resolved using `epg-dn-type-alist'."
2478   (mapconcat
2479    (lambda (rdn)
2480      (if (stringp (car rdn))
2481          (let ((entry (assoc (car rdn) epg-dn-type-alist)))
2482            (if entry
2483                (format "%s=%s" (cdr entry) (cdr rdn))
2484              (format "%s=%s" (car rdn) (cdr rdn))))
2485        (concat "(" (epg-decode-dn rdn) ")")))
2486    alist
2487    ", "))
2488
2489 (provide 'epg)
2490
2491 ;;; epg.el ends here