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-signature-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-signature-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 signers))
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
382 (defun epg-signature-status (signature)
383   "Return the status code of SIGNATURE."
384   (unless (eq (car-safe signature) 'epg-signature)
385     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
386   (aref (cdr signature) 0))
387
388 (defun epg-signature-key-id (signature)
389   "Return the key-id of SIGNATURE."
390   (unless (eq (car-safe signature) 'epg-signature)
391     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
392   (aref (cdr signature) 1))
393
394 (defun epg-signature-validity (signature)
395   "Return the validity of SIGNATURE."
396   (unless (eq (car-safe signature) 'epg-signature)
397     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
398   (aref (cdr signature) 2))
399
400 (defun epg-signature-fingerprint (signature)
401   "Return the fingerprint of SIGNATURE."
402   (unless (eq (car-safe signature) 'epg-signature)
403     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
404   (aref (cdr signature) 3))
405
406 (defun epg-signature-creation-time (signature)
407   "Return the creation time of SIGNATURE."
408   (unless (eq (car-safe signature) 'epg-signature)
409     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
410   (aref (cdr signature) 4))
411
412 (defun epg-signature-expiration-time (signature)
413   "Return the expiration time of SIGNATURE."
414   (unless (eq (car-safe signature) 'epg-signature)
415     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
416   (aref (cdr signature) 5))
417
418 (defun epg-signature-pubkey-algorithm (signature)
419   "Return the public key algorithm of SIGNATURE."
420   (unless (eq (car-safe signature) 'epg-signature)
421     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
422   (aref (cdr signature) 6))
423
424 (defun epg-signature-digest-algorithm (signature)
425   "Return the digest algorithm of SIGNATURE."
426   (unless (eq (car-safe signature) 'epg-signature)
427     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
428   (aref (cdr signature) 7))
429
430 (defun epg-signature-class (signature)
431   "Return the class of SIGNATURE."
432   (unless (eq (car-safe signature) 'epg-signature)
433     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
434   (aref (cdr signature) 8))
435
436 (defun epg-signature-version (signature)
437   "Return the version of SIGNATURE."
438   (unless (eq (car-safe signature) 'epg-signature)
439     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
440   (aref (cdr signature) 9))
441
442 (defun epg-signature-set-status (signature status)
443  "Set the status code of SIGNATURE."
444   (unless (eq (car-safe signature) 'epg-signature)
445     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
446   (aset (cdr signature) 0 status))
447
448 (defun epg-signature-set-key-id (signature key-id)
449  "Set the key-id of SIGNATURE."
450   (unless (eq (car-safe signature) 'epg-signature)
451     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
452   (aset (cdr signature) 1 key-id))
453
454 (defun epg-signature-set-validity (signature validity)
455  "Set the validity of SIGNATURE."
456   (unless (eq (car-safe signature) 'epg-signature)
457     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
458   (aset (cdr signature) 2 validity))
459
460 (defun epg-signature-set-fingerprint (signature fingerprint)
461  "Set the fingerprint of SIGNATURE."
462   (unless (eq (car-safe signature) 'epg-signature)
463     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
464   (aset (cdr signature) 3 fingerprint))
465
466 (defun epg-signature-set-creation-time (signature creation-time)
467   "Set the creation time of SIGNATURE."
468   (unless (eq (car-safe signature) 'epg-signature)
469     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
470   (aset (cdr signature) 4 creation-time))
471
472 (defun epg-signature-set-expiration-time (signature expiration-time)
473   "Set the expiration time of SIGNATURE."
474   (unless (eq (car-safe signature) 'epg-signature)
475     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
476   (aset (cdr signature) 5 expiration-time))
477
478 (defun epg-signature-set-pubkey-algorithm (signature pubkey-algorithm)
479   "Set the public key algorithm of SIGNATURE."
480   (unless (eq (car-safe signature) 'epg-signature)
481     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
482   (aset (cdr signature) 6 pubkey-algorithm))
483
484 (defun epg-signature-set-digest-algorithm (signature digest-algorithm)
485   "Set the digest algorithm of SIGNATURE."
486   (unless (eq (car-safe signature) 'epg-signature)
487     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
488   (aset (cdr signature) 7 digest-algorithm))
489
490 (defun epg-signature-set-class (signature class)
491   "Set the class of SIGNATURE."
492   (unless (eq (car-safe signature) 'epg-signature)
493     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
494   (aset (cdr signature) 8 class))
495
496 (defun epg-signature-set-version (signature version)
497   "Set the version of SIGNATURE."
498   (unless (eq (car-safe signature) 'epg-signature)
499     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
500   (aset (cdr signature) 9 version))
501
502 (defun epg-make-new-signature (type pubkey-algorithm digest-algorithm
503                                     class creation-time fingerprint)
504   "Return a new signature object."
505   (cons 'epg-new-signature (vector type pubkey-algorithm digest-algorithm
506                                    class creation-time fingerprint)))
507
508 (defun epg-new-signature-type (new-signature)
509   "Return the type of NEW-SIGNATURE."
510   (unless (eq (car-safe new-signature) 'epg-new-signature)
511     (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
512   (aref (cdr new-signature) 0))
513
514 (defun epg-new-signature-pubkey-algorithm (new-signature)
515   "Return the public key algorithm of NEW-SIGNATURE."
516   (unless (eq (car-safe new-signature) 'epg-new-signature)
517     (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
518   (aref (cdr new-signature) 1))
519
520 (defun epg-new-signature-digest-algorithm (new-signature)
521   "Return the digest algorithm of NEW-SIGNATURE."
522   (unless (eq (car-safe new-signature) 'epg-new-signature)
523     (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
524   (aref (cdr new-signature) 2))
525
526 (defun epg-new-signature-class (new-signature)
527   "Return the class of NEW-SIGNATURE."
528   (unless (eq (car-safe new-signature) 'epg-new-signature)
529     (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
530   (aref (cdr new-signature) 3))
531
532 (defun epg-new-signature-creation-time (new-signature)
533   "Return the creation time of NEW-SIGNATURE."
534   (unless (eq (car-safe new-signature) 'epg-new-signature)
535     (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
536   (aref (cdr new-signature) 4))
537
538 (defun epg-new-signature-fingerprint (new-signature)
539   "Return the fingerprint of NEW-SIGNATURE."
540   (unless (eq (car-safe new-signature) 'epg-new-signature)
541     (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
542   (aref (cdr new-signature) 5))
543
544 (defun epg-make-key (owner-trust)
545   "Return a key object."
546   (cons 'epg-key (vector owner-trust nil nil)))
547
548 (defun epg-key-owner-trust (key)
549   "Return the owner trust of KEY."
550   (unless (eq (car-safe key) 'epg-key)
551     (signal 'wrong-type-argument (list 'epg-key-p key)))
552   (aref (cdr key) 0))
553
554 (defun epg-key-sub-key-list (key)
555   "Return the sub key list of KEY."
556   (unless (eq (car-safe key) 'epg-key)
557     (signal 'wrong-type-argument (list 'epg-key-p key)))
558   (aref (cdr key) 1))
559
560 (defun epg-key-user-id-list (key)
561   "Return the user ID list of KEY."
562   (unless (eq (car-safe key) 'epg-key)
563     (signal 'wrong-type-argument (list 'epg-key-p key)))
564   (aref (cdr key) 2))
565
566 (defun epg-key-set-sub-key-list (key sub-key-list)
567   "Set the sub key list of KEY."
568   (unless (eq (car-safe key) 'epg-key)
569     (signal 'wrong-type-argument (list 'epg-key-p key)))
570   (aset (cdr key) 1 sub-key-list))
571
572 (defun epg-key-set-user-id-list (key user-id-list)
573   "Set the user ID list of KEY."
574   (unless (eq (car-safe key) 'epg-key)
575     (signal 'wrong-type-argument (list 'epg-key-p key)))
576   (aset (cdr key) 2 user-id-list))
577
578 (defun epg-make-sub-key (validity capability secret-p algorithm length id
579                                   creation-time expiration-time)
580   "Return a sub key object."
581   (cons 'epg-sub-key
582         (vector validity capability secret-p algorithm length id creation-time
583                 expiration-time nil)))
584
585 (defun epg-sub-key-validity (sub-key)
586   "Return the validity of SUB-KEY."
587   (unless (eq (car-safe sub-key) 'epg-sub-key)
588     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
589   (aref (cdr sub-key) 0))
590
591 (defun epg-sub-key-capability (sub-key)
592   "Return the capability of SUB-KEY."
593   (unless (eq (car-safe sub-key) 'epg-sub-key)
594     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
595   (aref (cdr sub-key) 1))
596
597 (defun epg-sub-key-secret-p (sub-key)
598   "Return non-nil if SUB-KEY is a secret key."
599   (unless (eq (car-safe sub-key) 'epg-sub-key)
600     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
601   (aref (cdr sub-key) 2))
602
603 (defun epg-sub-key-algorithm (sub-key)
604   "Return the algorithm of SUB-KEY."
605   (unless (eq (car-safe sub-key) 'epg-sub-key)
606     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
607   (aref (cdr sub-key) 3))
608
609 (defun epg-sub-key-length (sub-key)
610   "Return the length of SUB-KEY."
611   (unless (eq (car-safe sub-key) 'epg-sub-key)
612     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
613   (aref (cdr sub-key) 4))
614
615 (defun epg-sub-key-id (sub-key)
616   "Return the ID of SUB-KEY."
617   (unless (eq (car-safe sub-key) 'epg-sub-key)
618     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
619   (aref (cdr sub-key) 5))
620
621 (defun epg-sub-key-creation-time (sub-key)
622   "Return the creation time of SUB-KEY."
623   (unless (eq (car-safe sub-key) 'epg-sub-key)
624     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
625   (aref (cdr sub-key) 6))
626
627 (defun epg-sub-key-expiration-time (sub-key)
628   "Return the expiration time of SUB-KEY."
629   (unless (eq (car-safe sub-key) 'epg-sub-key)
630     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
631   (aref (cdr sub-key) 7))
632
633 (defun epg-sub-key-fingerprint (sub-key)
634   "Return the fingerprint of SUB-KEY."
635   (unless (eq (car-safe sub-key) 'epg-sub-key)
636     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
637   (aref (cdr sub-key) 8))
638
639 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
640   "Set the fingerprint of SUB-KEY.
641 This function is for internal use only."
642   (unless (eq (car-safe sub-key) 'epg-sub-key)
643     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
644   (aset (cdr sub-key) 8 fingerprint))
645
646 (defun epg-make-user-id (validity string)
647   "Return a user ID object."
648   (cons 'epg-user-id (vector validity string nil)))
649
650 (defun epg-user-id-validity (user-id)
651   "Return the validity of USER-ID."
652   (unless (eq (car-safe user-id) 'epg-user-id)
653     (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
654   (aref (cdr user-id) 0))
655
656 (defun epg-user-id-string (user-id)
657   "Return the name of USER-ID."
658   (unless (eq (car-safe user-id) 'epg-user-id)
659     (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
660   (aref (cdr user-id) 1))
661
662 (defun epg-user-id-signature-list (user-id)
663   "Return the signature list of USER-ID."
664   (unless (eq (car-safe user-id) 'epg-user-id)
665     (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
666   (aref (cdr user-id) 2))
667
668 (defun epg-user-id-set-signature-list (user-id signature-list)
669   "Set the signature list of USER-ID."
670   (unless (eq (car-safe user-id) 'epg-user-id)
671     (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
672   (aset (cdr user-id) 2 signature-list))
673
674 (defun epg-make-key-signature (validity pubkey-algorithm key-id creation-time
675                                         expiration-time user-id class
676                                         exportable-p)
677   "Return a key signature object."
678   (cons 'epg-key-signature
679         (vector validity pubkey-algorithm key-id creation-time expiration-time
680                 user-id class exportable-p)))
681
682 (defun epg-key-signature-validity (key-signature)
683   "Return the validity of KEY-SIGNATURE."
684   (unless (eq (car-safe key-signature) 'epg-key-signature)
685     (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
686   (aref (cdr key-signature) 0))
687
688 (defun epg-key-signature-pubkey-algorithm (key-signature)
689   "Return the public key algorithm of KEY-SIGNATURE."
690   (unless (eq (car-safe key-signature) 'epg-key-signature)
691     (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
692   (aref (cdr key-signature) 1))
693
694 (defun epg-key-signature-key-id (key-signature)
695   "Return the key-id of KEY-SIGNATURE."
696   (unless (eq (car-safe key-signature) 'epg-key-signature)
697     (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
698   (aref (cdr key-signature) 2))
699
700 (defun epg-key-signature-creation-time (key-signature)
701   "Return the creation time of KEY-SIGNATURE."
702   (unless (eq (car-safe key-signature) 'epg-key-signature)
703     (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
704   (aref (cdr key-signature) 3))
705
706 (defun epg-key-signature-expiration-time (key-signature)
707   "Return the expiration time of KEY-SIGNATURE."
708   (unless (eq (car-safe key-signature) 'epg-key-signature)
709     (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
710   (aref (cdr key-signature) 4))
711
712 (defun epg-key-signature-user-id (key-signature)
713   "Return the user-id of KEY-SIGNATURE."
714   (unless (eq (car-safe key-signature) 'epg-key-signature)
715     (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
716   (aref (cdr key-signature) 5))
717
718 (defun epg-key-signature-class (key-signature)
719   "Return the class of KEY-SIGNATURE."
720   (unless (eq (car-safe key-signature) 'epg-key-signature)
721     (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
722   (aref (cdr key-signature) 6))
723
724 (defun epg-key-signature-exportable-p (key-signature)
725   "Return t if KEY-SIGNATURE is exportable."
726   (unless (eq (car-safe key-signature) 'epg-key-signature)
727     (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
728   (aref (cdr key-signature) 7))
729
730 (defun epg-make-signature-notation (name value &optional human-readable
731                                          critical)
732   "Return a notation object."
733   (cons 'epg-signature-notation (vector name value human-readable critical)))
734
735 (defun epg-signature-notation-name (signature-notation)
736   "Return the name of SIGNATURE-NOTATION."
737   (unless (eq (car-safe signature-notation) 'epg-signature-notation)
738     (signal 'wrong-type-argument (list 'epg-signature-notation-p
739                                        signature-notation)))
740   (aref (cdr signature-notation) 0))
741
742 (defun epg-signature-notation-value (signature-notation)
743   "Return the value of SIGNATURE-NOTATION."
744   (unless (eq (car-safe signature-notation) 'epg-signature-notation)
745     (signal 'wrong-type-argument (list 'epg-signature-notation-p
746                                        signature-notation)))
747   (aref (cdr signature-notation) 1))
748
749 (defun epg-signature-notation-human-readable (signature-notation)
750   "Return the human-readable of SIGNATURE-NOTATION."
751   (unless (eq (car-safe signature-notation) 'epg-signature-notation)
752     (signal 'wrong-type-argument (list 'epg-signature-notation-p
753                                        signature-notation)))
754   (aref (cdr signature-notation) 2))
755
756 (defun epg-signature-notation-critical (signature-notation)
757   "Return the critical of SIGNATURE-NOTATION."
758   (unless (eq (car-safe signature-notation) 'epg-signature-notation)
759     (signal 'wrong-type-argument (list 'epg-signature-notation-p
760                                        signature-notation)))
761   (aref (cdr signature-notation) 3))
762
763 (defun epg-context-result-for (context name)
764   "Return the result of CONTEXT associated with NAME."
765   (cdr (assq name (epg-context-result context))))
766
767 (defun epg-context-set-result-for (context name value)
768   "Set the result of CONTEXT associated with NAME to VALUE."
769   (let* ((result (epg-context-result context))
770          (entry (assq name result)))
771     (if entry
772         (setcdr entry value)
773       (epg-context-set-result context (cons (cons name value) result)))))
774
775 (defun epg-signature-to-string (signature)
776   "Convert SIGNATURE to a human readable string."
777   (let ((user-id (cdr (assoc (epg-signature-key-id signature)
778                              epg-user-id-alist))))
779     (concat
780      (cond ((eq (epg-signature-status signature) 'good)
781             "Good signature from ")
782            ((eq (epg-signature-status signature) 'bad)
783             "Bad signature from ")
784            ((eq (epg-signature-status signature) 'expired)
785             "Expired signature from ")
786            ((eq (epg-signature-status signature) 'expired-key)
787             "Signature made by expired key ")
788            ((eq (epg-signature-status signature) 'revoked-key)
789             "Signature made by revoked key ")
790            ((eq (epg-signature-status signature) 'no-pubkey)
791             "No public key for "))
792      (epg-signature-key-id signature)
793      (if user-id
794          (concat " "
795                  (if (stringp user-id)
796                      user-id
797                    (epg-decode-dn user-id)))
798        "")
799      (if (epg-signature-validity signature)
800          (format " (trust %s)"  (epg-signature-validity signature))
801        ""))))
802
803 (defun epg-verify-result-to-string (verify-result)
804   "Convert VERIFY-RESULT to a human readable string."
805   (mapconcat #'epg-signature-to-string verify-result "\n"))
806
807 (defun epg-new-signature-to-string (new-signature)
808   "Convert NEW-SIGNATURE to a human readable string."
809   (concat
810    (cond ((eq (epg-new-signature-type new-signature) 'detached)
811           "Detached signature ")
812          ((eq (epg-new-signature-type new-signature) 'clear)
813           "Cleartext signature ")
814          (t
815           "Signature "))
816    (cdr (assq (epg-new-signature-pubkey-algorithm new-signature)
817               epg-pubkey-algorithm-alist))
818    "/"
819    (cdr (assq (epg-new-signature-digest-algorithm new-signature)
820               epg-digest-algorithm-alist))
821    " "
822    (format "%02X " (epg-new-signature-class new-signature))
823    (epg-new-signature-fingerprint new-signature)))
824
825 (defun epg--start (context args)
826   "Start `epg-gpg-program' in a subprocess with given ARGS."
827   (if (and (epg-context-process context)
828            (eq (process-status (epg-context-process context)) 'run))
829       (error "%s is already running in this context"
830              (if (eq (epg-context-protocol context) 'CMS)
831                  epg-gpgsm-program
832                epg-gpg-program)))
833   (let* ((args (append (list "--no-tty"
834                              "--status-fd" "1"
835                              "--yes")
836                        (if (epg-context-progress-callback context)
837                            (list "--enable-progress-filter"))
838                        (if epg-gpg-home-directory
839                            (list "--homedir" epg-gpg-home-directory))
840                        (unless (eq (epg-context-protocol context) 'CMS)
841                          (list "--command-fd" "0"))
842                        (if (epg-context-armor context) '("--armor"))
843                        (if (epg-context-textmode context) '("--textmode"))
844                        (if (epg-context-output-file context)
845                            (list "--output" (epg-context-output-file context)))
846                        args))
847          (coding-system-for-write 'binary)
848          process-connection-type
849          (orig-mode (default-file-modes))
850          (buffer (generate-new-buffer " *epg*"))
851          process)
852     (if epg-debug
853         (save-excursion
854           (unless epg-debug-buffer
855             (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
856           (set-buffer epg-debug-buffer)
857           (goto-char (point-max))
858           (insert (format "%s %s\n"
859                           (if (eq (epg-context-protocol context) 'CMS)
860                               epg-gpgsm-program
861                            epg-gpg-program)
862                           (mapconcat #'identity args " ")))))
863     (with-current-buffer buffer
864       (make-local-variable 'epg-read-point)
865       (setq epg-read-point (point-min))
866       (make-local-variable 'epg-process-filter-running)
867       (setq epg-process-filter-running nil)
868       (make-local-variable 'epg-pending-status-list)
869       (setq epg-pending-status-list nil)
870       (make-local-variable 'epg-key-id)
871       (setq epg-key-id nil)
872       (make-local-variable 'epg-context)
873       (setq epg-context context))
874     (unwind-protect
875         (progn
876           (set-default-file-modes 448)
877           (setq process
878                 (apply #'start-process "epg" buffer
879                        (if (eq (epg-context-protocol context) 'CMS)
880                            epg-gpgsm-program
881                          epg-gpg-program)
882                        args)))
883       (set-default-file-modes orig-mode))
884     (set-process-filter process #'epg--process-filter)
885     (epg-context-set-process context process)))
886
887 (defun epg--process-filter (process input)
888   (if epg-debug
889       (save-excursion
890         (unless epg-debug-buffer
891           (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
892         (set-buffer epg-debug-buffer)
893         (goto-char (point-max))
894         (insert input)))
895   (if (buffer-live-p (process-buffer process))
896       (save-excursion
897         (set-buffer (process-buffer process))
898         (goto-char (point-max))
899         (insert input)
900         (unless epg-process-filter-running
901           (unwind-protect
902               (progn
903                 (setq epg-process-filter-running t)
904                 (goto-char epg-read-point)
905                 (beginning-of-line)
906                 (while (looking-at ".*\n") ;the input line finished
907                   (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
908                       (let* ((status (match-string 1))
909                              (string (match-string 2))
910                              (symbol (intern-soft (concat "epg--status-"
911                                                           status))))
912                         (if (member status epg-pending-status-list)
913                             (setq epg-pending-status-list nil))
914                         (if (and symbol
915                                  (fboundp symbol))
916                             (funcall symbol epg-context string))))
917                   (forward-line)
918                   (setq epg-read-point (point))))
919             (setq epg-process-filter-running nil))))))
920
921 (defun epg-read-output (context)
922   "Read the output file CONTEXT and return the content as a string."
923   (with-temp-buffer
924     (if (fboundp 'set-buffer-multibyte)
925         (set-buffer-multibyte nil))
926     (if (file-exists-p (epg-context-output-file context))
927         (let ((coding-system-for-read 'binary))
928           (insert-file-contents (epg-context-output-file context))
929           (buffer-string)))))
930
931 (defun epg-wait-for-status (context status-list)
932   "Wait until one of elements in STATUS-LIST arrives."
933   (with-current-buffer (process-buffer (epg-context-process context))
934     (setq epg-pending-status-list status-list)
935     (while (and (eq (process-status (epg-context-process context)) 'run)
936                 epg-pending-status-list)
937       (accept-process-output (epg-context-process context) 1))))
938
939 (defun epg-wait-for-completion (context)
940   "Wait until the `epg-gpg-program' process completes."
941   (while (eq (process-status (epg-context-process context)) 'run)
942     (accept-process-output (epg-context-process context) 1)))
943
944 (defun epg-reset (context)
945   "Reset the CONTEXT."
946   (if (and (epg-context-process context)
947            (buffer-live-p (process-buffer (epg-context-process context))))
948       (kill-buffer (process-buffer (epg-context-process context))))
949   (epg-context-set-process context nil))
950
951 (defun epg-delete-output-file (context)
952   "Delete the output file of CONTEXT."
953   (if (and (epg-context-output-file context)
954            (file-exists-p (epg-context-output-file context)))
955       (delete-file (epg-context-output-file context))))
956
957 (defun epg--status-USERID_HINT (context string)
958   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
959       (let* ((key-id (match-string 1 string))
960              (user-id (match-string 2 string))
961              (entry (assoc key-id epg-user-id-alist)))
962         (if entry
963             (setcdr entry user-id)
964           (setq epg-user-id-alist (cons (cons key-id user-id)
965                                         epg-user-id-alist))))))
966
967 (defun epg--status-NEED_PASSPHRASE (context string)
968   (if (string-match "\\`\\([^ ]+\\)" string)
969       (setq epg-key-id (match-string 1 string))))
970
971 (defun epg--status-NEED_PASSPHRASE_SYM (context string)
972   (setq epg-key-id 'SYM))
973
974 (defun epg--status-NEED_PASSPHRASE_PIN (context string)
975   (setq epg-key-id 'PIN))
976
977 (defun epg--status-GET_HIDDEN (context string)
978   (when (and epg-key-id
979              (string-match "\\`passphrase\\." string))
980     (unless (epg-context-passphrase-callback context)
981       (error "passphrase-callback not set"))
982     (let (inhibit-quit
983           passphrase
984           passphrase-with-new-line
985           encoded-passphrase-with-new-line)
986       (unwind-protect
987           (condition-case nil
988               (progn
989                 (setq passphrase
990                       (funcall
991                        (if (consp (epg-context-passphrase-callback context))
992                            (car (epg-context-passphrase-callback context))
993                          (epg-context-passphrase-callback context))
994                        context
995                        epg-key-id
996                        (if (consp (epg-context-passphrase-callback context))
997                            (cdr (epg-context-passphrase-callback context)))))
998                 (when passphrase
999                   (setq passphrase-with-new-line (concat passphrase "\n"))
1000                   (epg--clear-string passphrase)
1001                   (setq passphrase nil)
1002                   (if epg-passphrase-coding-system
1003                       (progn
1004                         (setq encoded-passphrase-with-new-line
1005                               (encode-coding-string
1006                                passphrase-with-new-line
1007                                epg-passphrase-coding-system))
1008                         (epg--clear-string passphrase-with-new-line)
1009                         (setq passphrase-with-new-line nil))
1010                     (setq encoded-passphrase-with-new-line
1011                           passphrase-with-new-line
1012                           passphrase-with-new-line nil))
1013                   (process-send-string (epg-context-process context)
1014                                        encoded-passphrase-with-new-line)))
1015             (quit
1016              (epg-context-set-result-for
1017               context 'error
1018               (cons '(quit)
1019                     (epg-context-result-for context 'error)))
1020              (delete-process (epg-context-process context))))
1021         (if passphrase
1022             (epg--clear-string passphrase))
1023         (if passphrase-with-new-line
1024             (epg--clear-string passphrase-with-new-line))
1025         (if encoded-passphrase-with-new-line
1026             (epg--clear-string encoded-passphrase-with-new-line))))))
1027
1028 (defun epg--status-GET_BOOL (context string)
1029   (let ((entry (assoc string epg-prompt-alist))
1030         inhibit-quit)
1031     (condition-case nil
1032       (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
1033           (process-send-string (epg-context-process context) "y\n")
1034         (process-send-string (epg-context-process context) "n\n"))
1035       (quit
1036        (epg-context-set-result-for
1037         context 'error
1038         (cons '(quit)
1039               (epg-context-result-for context 'error)))
1040        (delete-process (epg-context-process context))))))
1041
1042 (defun epg--status-GET_LINE (context string)
1043   (let ((entry (assoc string epg-prompt-alist))
1044         inhibit-quit)
1045     (condition-case nil
1046         (process-send-string (epg-context-process context)
1047                              (concat (read-string
1048                                       (if entry
1049                                           (cdr entry)
1050                                         (concat string ": ")))
1051                                      "\n"))
1052       (quit
1053        (epg-context-set-result-for
1054         context 'error
1055         (cons '(quit)
1056               (epg-context-result-for context 'error)))
1057        (delete-process (epg-context-process context))))))
1058
1059 (defun epg--status-*SIG (context status string)
1060   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1061       (let* ((key-id (match-string 1 string))
1062              (user-id (match-string 2 string))
1063              (entry (assoc key-id epg-user-id-alist)))
1064         (epg-context-set-result-for
1065          context
1066          'verify
1067          (cons (epg-make-signature status key-id)
1068                (epg-context-result-for context 'verify)))
1069         (if (eq (epg-context-protocol context) 'CMS)
1070             (condition-case nil
1071                 (setq user-id (epg-dn-from-string user-id))
1072               (error)))
1073         (if entry
1074             (setcdr entry user-id)
1075           (setq epg-user-id-alist
1076                 (cons (cons key-id user-id) epg-user-id-alist))))
1077     (epg-context-set-result-for
1078      context
1079      'verify
1080      (cons (epg-make-signature status)
1081            (epg-context-result-for context 'verify)))))
1082
1083 (defun epg--status-GOODSIG (context string)
1084   (epg--status-*SIG context 'good string))
1085
1086 (defun epg--status-EXPSIG (context string)
1087   (epg--status-*SIG context 'expired string))
1088
1089 (defun epg--status-EXPKEYSIG (context string)
1090   (epg--status-*SIG context 'expired-key string))
1091
1092 (defun epg--status-REVKEYSIG (context string)
1093   (epg--status-*SIG context 'revoked-key string))
1094
1095 (defun epg--status-BADSIG (context string)
1096   (epg--status-*SIG context 'bad string))
1097
1098 (defun epg--status-NO_PUBKEY (context string)
1099   (let ((signature (car (epg-context-result-for context 'verify))))
1100     (if (and signature
1101              (eq (epg-signature-status signature) 'error)
1102              (equal (epg-signature-key-id signature) string))
1103         (epg-signature-set-status signature 'no-pubkey))))
1104
1105 (defun epg--time-from-seconds (seconds)
1106   (let ((number-seconds (string-to-number (concat seconds ".0"))))
1107     (cons (floor (/ number-seconds 65536))
1108           (floor (mod number-seconds 65536)))))
1109
1110 (defun epg--status-ERRSIG (context string)
1111   (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1112 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
1113                     string)
1114       (let ((signature (epg-make-signature 'error)))
1115         (epg-context-set-result-for
1116          context
1117          'verify
1118          (cons signature
1119                (epg-context-result-for context 'verify)))
1120         (epg-signature-set-key-id
1121          signature
1122          (match-string 1 string))
1123         (epg-signature-set-pubkey-algorithm
1124          signature
1125          (string-to-number (match-string 2 string)))
1126         (epg-signature-set-digest-algorithm
1127          signature
1128          (string-to-number (match-string 3 string)))
1129         (epg-signature-set-class
1130          signature
1131          (string-to-number (match-string 4 string) 16))
1132         (epg-signature-set-creation-time
1133          signature
1134          (epg--time-from-seconds (match-string 5 string))))))
1135
1136 (defun epg--status-VALIDSIG (context string)
1137   (let ((signature (car (epg-context-result-for context 'verify))))
1138     (when (and signature
1139                (eq (epg-signature-status signature) 'good)
1140                (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
1141 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
1142 \\(.*\\)"
1143                            string))
1144       (epg-signature-set-fingerprint
1145        signature
1146        (match-string 1 string))
1147       (epg-signature-set-creation-time
1148        signature
1149        (epg--time-from-seconds (match-string 2 string)))
1150       (epg-signature-set-expiration-time
1151        signature
1152        (epg--time-from-seconds (match-string 3 string)))
1153       (epg-signature-set-version
1154        signature
1155        (string-to-number (match-string 4 string)))
1156       (epg-signature-set-pubkey-algorithm
1157        signature 
1158        (string-to-number (match-string 5 string)))
1159       (epg-signature-set-digest-algorithm
1160        signature
1161        (string-to-number (match-string 6 string)))
1162       (epg-signature-set-class
1163        signature
1164        (string-to-number (match-string 7 string) 16)))))
1165
1166 (defun epg--status-TRUST_UNDEFINED (context string)
1167   (let ((signature (car (epg-context-result-for context 'verify))))
1168     (if (and signature
1169              (eq (epg-signature-status signature) 'good))
1170         (epg-signature-set-validity signature 'undefined))))
1171
1172 (defun epg--status-TRUST_NEVER (context string)
1173   (let ((signature (car (epg-context-result-for context 'verify))))
1174     (if (and signature
1175              (eq (epg-signature-status signature) 'good))
1176         (epg-signature-set-validity signature 'never))))
1177
1178 (defun epg--status-TRUST_MARGINAL (context string)
1179   (let ((signature (car (epg-context-result-for context 'verify))))
1180     (if (and signature
1181              (eq (epg-signature-status signature) 'marginal))
1182         (epg-signature-set-validity signature 'marginal))))
1183
1184 (defun epg--status-TRUST_FULLY (context string)
1185   (let ((signature (car (epg-context-result-for context 'verify))))
1186     (if (and signature
1187              (eq (epg-signature-status signature) 'good))
1188         (epg-signature-set-validity signature 'full))))
1189
1190 (defun epg--status-TRUST_ULTIMATE (context string)
1191   (let ((signature (car (epg-context-result-for context 'verify))))
1192     (if (and signature
1193              (eq (epg-signature-status signature) 'good))
1194         (epg-signature-set-validity signature 'ultimate))))
1195
1196 (defun epg--status-PROGRESS (context string)
1197   (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1198                     string)
1199       (funcall (if (consp (epg-context-progress-callback context))
1200                    (car (epg-context-progress-callback context))
1201                  (epg-context-progress-callback context))
1202                context
1203                (match-string 1 string)
1204                (match-string 2 string)
1205                (string-to-number (match-string 3 string))
1206                (string-to-number (match-string 4 string))
1207                (if (consp (epg-context-progress-callback context))
1208                    (cdr (epg-context-progress-callback context))))))
1209
1210 (defun epg--status-DECRYPTION_FAILED (context string)
1211   (epg-context-set-result-for
1212    context 'error
1213    (cons '(decryption-failed)
1214          (epg-context-result-for context 'error))))
1215
1216 (defun epg--status-NODATA (context string)
1217   (epg-context-set-result-for
1218    context 'error
1219    (cons (list 'no-data (cons 'reason (string-to-number string)))
1220          (epg-context-result-for context 'error))))
1221
1222 (defun epg--status-UNEXPECTED (context string)
1223   (epg-context-set-result-for
1224    context 'error
1225    (cons (list 'unexpected (cons 'reason (string-to-number string)))
1226          (epg-context-result-for context 'error))))
1227
1228 (defun epg--status-KEYEXPIRED (context string)
1229   (epg-context-set-result-for
1230    context 'error
1231    (cons (list 'key-expired (cons 'expiration-time
1232                                   (epg--time-from-seconds string)))
1233          (epg-context-result-for context 'error))))
1234
1235 (defun epg--status-KEYREVOKED (context string)
1236   (epg-context-set-result-for
1237    context 'error
1238    (cons '(key-revoked)
1239          (epg-context-result-for context 'error))))
1240
1241 (defun epg--status-BADARMOR (context string)
1242   (epg-context-set-result-for
1243    context 'error
1244    (cons '(bad-armor)
1245          (epg-context-result-for context 'error))))
1246
1247 (defun epg--status-INV_RECP (context string)
1248   (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1249       (epg-context-set-result-for
1250        context 'error
1251        (cons (list 'invalid-recipient
1252                    (cons 'reason
1253                          (string-to-number (match-string 1 string)))
1254                    (cons 'requested-recipient
1255                          (match-string 2 string)))
1256              (epg-context-result-for context 'error)))))
1257
1258 (defun epg--status-NO_RECP (context string)
1259   (epg-context-set-result-for
1260    context 'error
1261    (cons '(no-recipients)
1262          (epg-context-result-for context 'error))))
1263
1264 (defun epg--status-DELETE_PROBLEM (context string)
1265   (if (string-match "\\`\\([0-9]+\\)" string)
1266       (epg-context-set-result-for
1267        context 'error
1268        (cons (list 'delete-problem
1269                    (cons 'reason (string-to-number (match-string 1 string))))
1270              (epg-context-result-for context 'error)))))
1271
1272 (defun epg--status-SIG_CREATED (context string)
1273   (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1274 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1275       (epg-context-set-result-for
1276        context 'sign
1277        (cons (epg-make-new-signature
1278               (cdr (assq (aref (match-string 1 string) 0)
1279                          epg-new-signature-type-alist))
1280               (string-to-number (match-string 2 string))
1281               (string-to-number (match-string 3 string))
1282               (string-to-number (match-string 4 string) 16)
1283               (epg--time-from-seconds (match-string 5 string))
1284               (substring string (match-end 0)))
1285              (epg-context-result-for context 'sign)))))
1286
1287 (defun epg--status-KEY_CREATED (context string)
1288   (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1289       (epg-context-set-result-for
1290        context 'generate-key
1291        (cons (list (cons 'type (string-to-char (match-string 1 string)))
1292                    (cons 'fingerprint (match-string 2 string)))
1293              (epg-context-result-for context 'generate-key)))))
1294
1295 (defun epg--status-KEY_NOT_CREATED (context string)
1296   (epg-context-set-result-for
1297    context 'error
1298    (cons '(key-not-created)
1299          (epg-context-result-for context 'error))))
1300
1301 (defun epg--status-IMPORTED (context string)
1302   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1303       (let* ((key-id (match-string 1 string))
1304              (user-id (match-string 2 string))
1305              (entry (assoc key-id epg-user-id-alist)))
1306         (if entry
1307             (setcdr entry user-id)
1308           (setq epg-user-id-alist (cons (cons key-id user-id)
1309                                         epg-user-id-alist)))
1310         (epg-context-set-result-for
1311          context 'import
1312          (cons (list (cons 'key-id key-id)
1313                      (cons 'user-id user-id))
1314                (epg-context-result-for context 'import))))))
1315
1316 (defun epg--status-IMPORT_OK (context string)
1317   (let ((result (epg-context-result-for context 'import)))
1318     (if (and result
1319              (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string))
1320         (setcar result
1321                 (append (list (cons 'reason
1322                                     (string-to-number
1323                                      (match-string 1 string))))
1324                         (if (match-beginning 2)
1325                             (list (cons 'fingerprint
1326                                         (match-string 3 string))))
1327                         (car result))))))
1328
1329 (defun epg--status-IMPORT_PROBLEM (context string)
1330   (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1331       (epg-context-set-result-for
1332        context 'error
1333        (cons (cons 'import-problem
1334                    (append (list (cons 'reason
1335                                        (string-to-number
1336                                         (match-string 1 string))))
1337                            (if (match-beginning 2)
1338                                (list (cons 'fingerprint
1339                                            (match-string 3 string))))))
1340              (epg-context-result-for context 'error)))))
1341
1342 (defun epg-passphrase-callback-function (context key-id handback)
1343   (if (eq key-id 'SYM)
1344       (read-passwd "Passphrase for symmetric encryption: "
1345                    (eq (epg-context-operation context) 'encrypt))
1346     (read-passwd
1347      (if (eq key-id 'PIN)
1348         "Passphrase for PIN: "
1349        (let ((entry (assoc key-id epg-user-id-alist)))
1350          (if entry
1351              (format "Passphrase for %s %s: " key-id (cdr entry))
1352            (format "Passphrase for %s: " key-id)))))))
1353
1354 (make-obsolete 'epg-passphrase-callback-function
1355                'epa-passphrase-callback-function)
1356
1357 (defun epg--list-keys-1 (context name mode)
1358   (let ((args (append (if epg-gpg-home-directory
1359                           (list "--homedir" epg-gpg-home-directory))
1360                       (list "--with-colons" "--no-greeting" "--batch"
1361                             "--with-fingerprint"
1362                             "--with-fingerprint"
1363                             (if (memq mode '(t secret))
1364                                 "--list-secret-keys"
1365                               (if (memq mode '(nil public))
1366                                   "--list-keys"
1367                                 "--list-sigs")))
1368                       (unless (eq (epg-context-protocol context) 'CMS)
1369                         '("--fixed-list-mode"))
1370                       (if name (list name))))
1371         keys string field index)
1372     (with-temp-buffer
1373       (apply #'call-process
1374              (if (eq (epg-context-protocol context) 'CMS)
1375                  epg-gpgsm-program
1376                epg-gpg-program)
1377              nil (list t nil) nil args)
1378       (goto-char (point-min))
1379       (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1380         (setq keys (cons (make-vector 15 nil) keys)
1381               string (match-string 0)
1382               index 0
1383               field 0)
1384         (while (eq index
1385                    (string-match "\\([^:]+\\)?:" string index))
1386           (setq index (match-end 0))
1387           (aset (car keys) field (match-string 1 string))
1388           (setq field (1+ field))))
1389       (nreverse keys))))
1390
1391 (defun epg--make-sub-key-1 (line)
1392   (epg-make-sub-key
1393    (if (aref line 1)
1394        (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1395    (delq nil
1396          (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1397                  (aref line 11)))
1398    (member (aref line 0) '("sec" "ssb"))
1399    (string-to-number (aref line 3))
1400    (string-to-number (aref line 2))
1401    (aref line 4)
1402    (epg--time-from-seconds (aref line 5))
1403    (epg--time-from-seconds (aref line 6))))
1404
1405 ;;;###autoload
1406 (defun epg-list-keys (context &optional name mode)
1407   "Return a list of epg-key objects matched with NAME.
1408 If MODE is nil or 'public, only public keyring should be searched.
1409 If MODE is t or 'secret, only secret keyring should be searched. 
1410 Otherwise, only public keyring should be searched and the key
1411 signatures should be included."
1412   (let ((lines (epg--list-keys-1 context name mode))
1413         keys cert pointer pointer-1)
1414     (while lines
1415       (cond
1416        ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1417         (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1418               keys (cons (epg-make-key
1419                           (if (aref (car lines) 8)
1420                               (cdr (assq (string-to-char (aref (car lines) 8))
1421                                          epg-key-validity-alist))))
1422                          keys))
1423         (epg-key-set-sub-key-list
1424          (car keys)
1425          (cons (epg--make-sub-key-1 (car lines))
1426                (epg-key-sub-key-list (car keys)))))
1427        ((member (aref (car lines) 0) '("sub" "ssb"))
1428         (epg-key-set-sub-key-list
1429          (car keys)
1430          (cons (epg--make-sub-key-1 (car lines))
1431                (epg-key-sub-key-list (car keys)))))
1432        ((equal (aref (car lines) 0) "uid")
1433         (epg-key-set-user-id-list
1434          (car keys)
1435          (cons (epg-make-user-id
1436                 (if (aref (car lines) 1)
1437                     (cdr (assq (string-to-char (aref (car lines) 1))
1438                                epg-key-validity-alist)))
1439                 (if cert
1440                     (condition-case nil
1441                         (epg-dn-from-string (aref (car lines) 9))
1442                       (error (aref (car lines) 9)))
1443                   (aref (car lines) 9)))
1444                (epg-key-user-id-list (car keys)))))
1445        ((equal (aref (car lines) 0) "fpr")
1446         (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1447                                      (aref (car lines) 9)))
1448        ((equal (aref (car lines) 0) "sig")
1449         (epg-user-id-set-signature-list
1450          (car (epg-key-user-id-list (car keys)))
1451          (cons
1452           (epg-make-key-signature
1453            (if (aref (car lines) 1)
1454                (cdr (assq (string-to-char (aref (car lines) 1))
1455                           epg-key-validity-alist)))
1456            (string-to-number (aref (car lines) 3))
1457            (aref (car lines) 4)
1458            (epg--time-from-seconds (aref (car lines) 5))
1459            (epg--time-from-seconds (aref (car lines) 6))
1460            (aref (car lines) 9)
1461            (string-to-number (aref (car lines) 10) 16)
1462            (eq (aref (aref (car lines) 10) 2) ?x))
1463           (epg-user-id-signature-list
1464            (car (epg-key-user-id-list (car keys))))))))
1465       (setq lines (cdr lines)))
1466     (setq keys (nreverse keys)
1467           pointer keys)
1468     (while pointer
1469       (epg-key-set-sub-key-list
1470        (car pointer)
1471        (nreverse (epg-key-sub-key-list (car pointer))))
1472       (setq pointer-1 (epg-key-set-user-id-list
1473                           (car pointer)
1474                           (nreverse (epg-key-user-id-list (car pointer)))))
1475       (while pointer-1
1476         (epg-user-id-set-signature-list
1477          (car pointer-1)
1478          (nreverse (epg-user-id-signature-list (car pointer-1))))
1479         (setq pointer-1 (cdr pointer-1)))
1480       (setq pointer (cdr pointer)))
1481     keys))
1482
1483 (if (fboundp 'make-temp-file)
1484     (defalias 'epg--make-temp-file 'make-temp-file)
1485   (defvar temporary-file-directory)
1486   ;; stolen from poe.el.
1487   (defun epg--make-temp-file (prefix)
1488     "Create a temporary file.
1489 The returned file name (created by appending some random characters at the end
1490 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1491 is guaranteed to point to a newly created empty file.
1492 You can then use `write-region' to write new data into the file."
1493     (let (tempdir tempfile)
1494       (setq prefix (expand-file-name prefix
1495                                      (if (featurep 'xemacs)
1496                                          (temp-directory)
1497                                        temporary-file-directory)))
1498       (unwind-protect
1499           (let (file)
1500             ;; First, create a temporary directory.
1501             (while (condition-case ()
1502                        (progn
1503                          (setq tempdir (make-temp-name
1504                                         (concat
1505                                          (file-name-directory prefix)
1506                                          "DIR")))
1507                          ;; return nil or signal an error.
1508                          (make-directory tempdir))
1509                      ;; let's try again.
1510                      (file-already-exists t)))
1511             (set-file-modes tempdir 448)
1512             ;; Second, create a temporary file in the tempdir.
1513             ;; There *is* a race condition between `make-temp-name'
1514             ;; and `write-region', but we don't care it since we are
1515             ;; in a private directory now.
1516             (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1517             (write-region "" nil tempfile nil 'silent)
1518             (set-file-modes tempfile 384)
1519             ;; Finally, make a hard-link from the tempfile.
1520             (while (condition-case ()
1521                        (progn
1522                          (setq file (make-temp-name prefix))
1523                          ;; return nil or signal an error.
1524                          (add-name-to-file tempfile file))
1525                      ;; let's try again.
1526                      (file-already-exists t)))
1527             file)
1528         ;; Cleanup the tempfile.
1529         (and tempfile
1530              (file-exists-p tempfile)
1531              (delete-file tempfile))
1532         ;; Cleanup the tempdir.
1533         (and tempdir
1534              (file-directory-p tempdir)
1535              (delete-directory tempdir))))))
1536
1537 (if (fboundp 'clear-string)
1538     (defalias 'epg--clear-string 'clear-string)
1539   (defun epg--clear-string (string)
1540     (fillarray string 0)))
1541
1542 (defun epg--args-from-signature-notations (notations)
1543   (apply #'nconc
1544          (mapcar
1545           (lambda (notation)
1546             (if (and (epg-signature-notation-name notation)
1547                      (not (epg-signature-notation-human-readable notation)))
1548                 (error "Unreadable"))
1549             (if (epg-signature-notation-name notation)
1550                 (list "--sig-notation"
1551                       (if (epg-signature-notation-critical notation)
1552                           (concat "!" (epg-signature-notation-name notation)
1553                                   "=" (epg-signature-notation-value notation))
1554                         (concat (epg-signature-notation-name notation)
1555                                 "=" (epg-signature-notation-value notation))))
1556               (list "--sig-policy-url"
1557                     (epg-signature-notation-value notation))))
1558           notations))
1559
1560 ;;;###autoload
1561 (defun epg-cancel (context)
1562   (if (buffer-live-p (process-buffer (epg-context-process context)))
1563       (save-excursion
1564         (set-buffer (process-buffer (epg-context-process context)))
1565         (epg-context-set-result-for
1566          epg-context 'error
1567          (cons '(quit)
1568                (epg-context-result-for epg-context 'error)))))
1569   (if (eq (process-status (epg-context-process context)) 'run)
1570       (delete-process (epg-context-process context))))
1571   
1572 ;;;###autoload
1573 (defun epg-start-decrypt (context cipher)
1574   "Initiate a decrypt operation on CIPHER.
1575 CIPHER must be a file data object.
1576
1577 If you use this function, you will need to wait for the completion of
1578 `epg-gpg-program' by using `epg-wait-for-completion' and call
1579 `epg-reset' to clear a temporaly output file.
1580 If you are unsure, use synchronous version of this function
1581 `epg-decrypt-file' or `epg-decrypt-string' instead."
1582   (unless (epg-data-file cipher)
1583     (error "Not a file"))
1584   (epg-context-set-operation context 'decrypt)
1585   (epg-context-set-result context nil)
1586   (epg--start context (list "--decrypt" "--" (epg-data-file cipher)))
1587   ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1588   (unless (eq (epg-context-protocol context) 'CMS)
1589     (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1590
1591 ;;;###autoload
1592 (defun epg-decrypt-file (context cipher plain)
1593   "Decrypt a file CIPHER and store the result to a file PLAIN.
1594 If PLAIN is nil, it returns the result as a string."
1595   (unwind-protect
1596       (progn
1597         (if plain
1598             (epg-context-set-output-file context plain)
1599           (epg-context-set-output-file context
1600                                        (epg--make-temp-file "epg-output")))
1601         (epg-start-decrypt context (epg-make-data-from-file cipher))
1602         (epg-wait-for-completion context)
1603         (if (epg-context-result-for context 'error)
1604             (error "Decrypt failed: %S"
1605                    (epg-context-result-for context 'error)))
1606         (unless plain
1607           (epg-read-output context)))
1608     (unless plain
1609       (epg-delete-output-file context))
1610     (epg-reset context)))
1611
1612 ;;;###autoload
1613 (defun epg-decrypt-string (context cipher)
1614   "Decrypt a string CIPHER and return the plain text."
1615   (let ((input-file (epg--make-temp-file "epg-input"))
1616         (coding-system-for-write 'binary))
1617     (unwind-protect
1618         (progn
1619           (write-region cipher nil input-file nil 'quiet)
1620           (epg-context-set-output-file context
1621                                        (epg--make-temp-file "epg-output"))
1622           (epg-start-decrypt context (epg-make-data-from-file input-file))
1623           (epg-wait-for-completion context)
1624           (if (epg-context-result-for context 'error)
1625               (error "Decrypt failed: %S"
1626                      (epg-context-result-for context 'error)))
1627           (epg-read-output context))
1628       (epg-delete-output-file context)
1629       (if (file-exists-p input-file)
1630           (delete-file input-file))
1631       (epg-reset context))))
1632
1633 ;;;###autoload
1634 (defun epg-start-verify (context signature &optional signed-text)
1635   "Initiate a verify operation on SIGNATURE.
1636 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1637
1638 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1639 For a normal or a cleartext signature, SIGNED-TEXT should be nil.
1640
1641 If you use this function, you will need to wait for the completion of
1642 `epg-gpg-program' by using `epg-wait-for-completion' and call
1643 `epg-reset' to clear a temporaly output file.
1644 If you are unsure, use synchronous version of this function
1645 `epg-verify-file' or `epg-verify-string' instead."
1646   (epg-context-set-operation context 'verify)
1647   (epg-context-set-result context nil)
1648   (if signed-text
1649       ;; Detached signature.
1650       (if (epg-data-file signed-text)
1651           (epg--start context (list "--verify" "--" (epg-data-file signature)
1652                                    (epg-data-file signed-text)))
1653         (epg--start context (list "--verify" "--" (epg-data-file signature)
1654                                   "-"))
1655         (if (eq (process-status (epg-context-process context)) 'run)
1656             (process-send-string (epg-context-process context)
1657                                  (epg-data-string signed-text)))
1658         (if (eq (process-status (epg-context-process context)) 'run)
1659             (process-send-eof (epg-context-process context))))
1660     ;; Normal (or cleartext) signature.
1661     (if (epg-data-file signature)
1662         (epg--start context (list "--" (epg-data-file signature)))
1663       (epg--start context '("-"))
1664       (if (eq (process-status (epg-context-process context)) 'run)
1665           (process-send-string (epg-context-process context)
1666                                (epg-data-string signature)))
1667       (if (eq (process-status (epg-context-process context)) 'run)
1668           (process-send-eof (epg-context-process context))))))
1669
1670 ;;;###autoload
1671 (defun epg-verify-file (context signature &optional signed-text plain)
1672   "Verify a file SIGNATURE.
1673 SIGNED-TEXT and PLAIN are also a file if they are specified.
1674
1675 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1676 string.  For a normal or a cleartext signature, SIGNED-TEXT should be
1677 nil.  In the latter case, if PLAIN is specified, the plaintext is
1678 stored into the file after successful verification."
1679   (unwind-protect
1680       (progn
1681         (if plain
1682             (epg-context-set-output-file context plain)
1683           (epg-context-set-output-file context
1684                                        (epg--make-temp-file "epg-output")))
1685         (if signed-text
1686             (epg-start-verify context
1687                               (epg-make-data-from-file signature)
1688                               (epg-make-data-from-file signed-text))
1689           (epg-start-verify context
1690                             (epg-make-data-from-file signature)))
1691         (epg-wait-for-completion context)
1692         (unless plain
1693           (epg-read-output context)))
1694     (unless plain
1695       (epg-delete-output-file context))
1696     (epg-reset context)))
1697
1698 ;;;###autoload
1699 (defun epg-verify-string (context signature &optional signed-text)
1700   "Verify a string SIGNATURE.
1701 SIGNED-TEXT is a string if it is specified.
1702
1703 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1704 string.  For a normal or a cleartext signature, SIGNED-TEXT should be
1705 nil.  In the latter case, this function returns the plaintext after
1706 successful verification."
1707   (let ((coding-system-for-write 'binary)
1708         input-file)
1709     (unwind-protect
1710         (progn
1711           (epg-context-set-output-file context
1712                                        (epg--make-temp-file "epg-output"))
1713           (if signed-text
1714               (progn
1715                 (setq input-file (epg--make-temp-file "epg-signature"))
1716                 (write-region signature nil input-file nil 'quiet)
1717                 (epg-start-verify context
1718                                   (epg-make-data-from-file input-file)
1719                                   (epg-make-data-from-string signed-text)))
1720             (epg-start-verify context (epg-make-data-from-string signature)))
1721           (epg-wait-for-completion context)
1722           (epg-read-output context))
1723       (epg-delete-output-file context)
1724       (if (and input-file
1725                (file-exists-p input-file))
1726           (delete-file input-file))
1727       (epg-reset context))))
1728
1729 ;;;###autoload
1730 (defun epg-start-sign (context plain &optional mode)
1731   "Initiate a sign operation on PLAIN.
1732 PLAIN is a data object.
1733
1734 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1735 If it is nil or 'normal, it makes a normal signature.
1736 Otherwise, it makes a cleartext signature.
1737
1738 If you use this function, you will need to wait for the completion of
1739 `epg-gpg-program' by using `epg-wait-for-completion' and call
1740 `epg-reset' to clear a temporaly output file.
1741 If you are unsure, use synchronous version of this function
1742 `epg-sign-file' or `epg-sign-string' instead."
1743   (epg-context-set-operation context 'sign)
1744   (epg-context-set-result context nil)
1745   (unless (memq mode '(t detached nil normal)) ;i.e. cleartext
1746     (epg-context-set-armor context nil)
1747     (epg-context-set-textmode context nil))
1748   (epg--start context
1749              (append (list (if (memq mode '(t detached))
1750                                "--detach-sign"
1751                              (if (memq mode '(nil normal))
1752                                  "--sign"
1753                                "--clearsign")))
1754                      (apply #'nconc
1755                             (mapcar
1756                              (lambda (signer)
1757                                (list "-u"
1758                                      (epg-sub-key-id
1759                                       (car (epg-key-sub-key-list signer)))))
1760                              (epg-context-signers context)))
1761                      (epg--args-from-signature-notations
1762                       (epg-context-signature-notations context))
1763                      (if (epg-data-file plain)
1764                          (list "--" (epg-data-file plain)))))
1765   ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1766   (unless (eq (epg-context-protocol context) 'CMS)
1767     (epg-wait-for-status context '("BEGIN_SIGNING")))
1768   (when (epg-data-string plain)
1769     (if (eq (process-status (epg-context-process context)) 'run)
1770         (process-send-string (epg-context-process context)
1771                              (epg-data-string plain)))
1772     (if (eq (process-status (epg-context-process context)) 'run)
1773         (process-send-eof (epg-context-process context)))))
1774
1775 ;;;###autoload
1776 (defun epg-sign-file (context plain signature &optional mode)
1777   "Sign a file PLAIN and store the result to a file SIGNATURE.
1778 If SIGNATURE is nil, it returns the result as a string.
1779 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1780 If it is nil or 'normal, it makes a normal signature.
1781 Otherwise, it makes a cleartext signature."
1782   (unwind-protect
1783       (progn
1784         (if signature
1785             (epg-context-set-output-file context signature)
1786           (epg-context-set-output-file context
1787                                        (epg--make-temp-file "epg-output")))
1788         (epg-start-sign context (epg-make-data-from-file plain) mode)
1789         (epg-wait-for-completion context)
1790         (unless (epg-context-result-for context 'sign)
1791           (if (epg-context-result-for context 'error)
1792               (error "Sign failed: %S"
1793                      (epg-context-result-for context 'error))
1794             (error "Sign failed")))
1795         (unless signature
1796           (epg-read-output context)))
1797     (unless signature
1798       (epg-delete-output-file context))
1799     (epg-reset context)))
1800
1801 ;;;###autoload
1802 (defun epg-sign-string (context plain &optional mode)
1803   "Sign a string PLAIN and return the output as string.
1804 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1805 If it is nil or 'normal, it makes a normal signature.
1806 Otherwise, it makes a cleartext signature."
1807   (unwind-protect
1808       (progn
1809         (epg-context-set-output-file context
1810                                      (epg--make-temp-file "epg-output"))
1811         (epg-start-sign context (epg-make-data-from-string plain) mode)
1812         (epg-wait-for-completion context)
1813         (unless (epg-context-result-for context 'sign)
1814           (if (epg-context-result-for context 'error)
1815               (error "Sign failed: %S"
1816                      (epg-context-result-for context 'error))
1817             (error "Sign failed")))
1818         (epg-read-output context))
1819     (epg-delete-output-file context)
1820     (epg-reset context)))
1821
1822 ;;;###autoload
1823 (defun epg-start-encrypt (context plain recipients
1824                                   &optional sign always-trust)
1825   "Initiate an encrypt operation on PLAIN.
1826 PLAIN is a data object.
1827 If RECIPIENTS is nil, it performs symmetric encryption.
1828
1829 If you use this function, you will need to wait for the completion of
1830 `epg-gpg-program' by using `epg-wait-for-completion' and call
1831 `epg-reset' to clear a temporaly output file.
1832 If you are unsure, use synchronous version of this function
1833 `epg-encrypt-file' or `epg-encrypt-string' instead."
1834   (epg-context-set-operation context 'encrypt)
1835   (epg-context-set-result context nil)
1836   (epg--start context
1837              (append (if always-trust '("--always-trust"))
1838                      (if recipients '("--encrypt") '("--symmetric"))
1839                      (if sign '("--sign"))
1840                      (if sign
1841                          (apply #'nconc
1842                                 (mapcar
1843                                  (lambda (signer)
1844                                    (list "-u"
1845                                          (epg-sub-key-id
1846                                           (car (epg-key-sub-key-list
1847                                                 signer)))))
1848                                  (epg-context-signers context))))
1849                      (if sign
1850                          (epg--args-from-signature-notations
1851                           (epg-context-signature-notations context)))
1852                      (apply #'nconc
1853                             (mapcar
1854                              (lambda (recipient)
1855                                (list "-r"
1856                                      (epg-sub-key-id
1857                                       (car (epg-key-sub-key-list recipient)))))
1858                              recipients))
1859                      (if (epg-data-file plain)
1860                          (list "--" (epg-data-file plain)))))
1861   ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1862   (unless (eq (epg-context-protocol context) 'CMS)
1863     (if sign
1864         (epg-wait-for-status context '("BEGIN_SIGNING"))
1865       (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
1866   (when (epg-data-string plain)
1867     (if (eq (process-status (epg-context-process context)) 'run)
1868         (process-send-string (epg-context-process context)
1869                              (epg-data-string plain)))
1870     (if (eq (process-status (epg-context-process context)) 'run)
1871         (process-send-eof (epg-context-process context)))))
1872
1873 ;;;###autoload
1874 (defun epg-encrypt-file (context plain recipients
1875                                  cipher &optional sign always-trust)
1876   "Encrypt a file PLAIN and store the result to a file CIPHER.
1877 If CIPHER is nil, it returns the result as a string.
1878 If RECIPIENTS is nil, it performs symmetric encryption."
1879   (unwind-protect
1880       (progn
1881         (if cipher
1882             (epg-context-set-output-file context cipher)
1883           (epg-context-set-output-file context
1884                                        (epg--make-temp-file "epg-output")))
1885         (epg-start-encrypt context (epg-make-data-from-file plain)
1886                            recipients sign always-trust)
1887         (epg-wait-for-completion context)
1888         (if (and sign
1889                  (not (epg-context-result-for context 'sign)))
1890             (if (epg-context-result-for context 'error)
1891                 (error "Sign failed: %S"
1892                        (epg-context-result-for context 'error))
1893                 (error "Sign failed")))
1894         (if (epg-context-result-for context 'error)
1895             (error "Encrypt failed: %S"
1896                    (epg-context-result-for context 'error)))
1897         (unless cipher
1898           (epg-read-output context)))
1899     (unless cipher
1900       (epg-delete-output-file context))
1901     (epg-reset context)))
1902
1903 ;;;###autoload
1904 (defun epg-encrypt-string (context plain recipients
1905                                    &optional sign always-trust)
1906   "Encrypt a string PLAIN.
1907 If RECIPIENTS is nil, it performs symmetric encryption."
1908   (unwind-protect
1909       (progn
1910         (epg-context-set-output-file context
1911                                      (epg--make-temp-file "epg-output"))
1912         (epg-start-encrypt context (epg-make-data-from-string plain)
1913                            recipients sign always-trust)
1914         (epg-wait-for-completion context)
1915         (if (and sign
1916                  (not (epg-context-result-for context 'sign)))
1917             (if (epg-context-result-for context 'error)
1918                 (error "Sign failed: %S"
1919                        (epg-context-result-for context 'error))
1920               (error "Sign failed")))
1921         (if (epg-context-result-for context 'error)
1922             (error "Encrypt failed: %S"
1923                    (epg-context-result-for context 'error)))
1924         (epg-read-output context))
1925     (epg-delete-output-file context)
1926     (epg-reset context)))
1927
1928 ;;;###autoload
1929 (defun epg-start-export-keys (context keys)
1930   "Initiate an export keys operation.
1931
1932 If you use this function, you will need to wait for the completion of
1933 `epg-gpg-program' by using `epg-wait-for-completion' and call
1934 `epg-reset' to clear a temporaly output file.
1935 If you are unsure, use synchronous version of this function
1936 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1937   (epg-context-set-operation context 'export-keys)
1938   (epg-context-set-result context nil)
1939   (epg--start context (cons "--export"
1940                            (mapcar
1941                             (lambda (key)
1942                               (epg-sub-key-id
1943                                (car (epg-key-sub-key-list key))))
1944                             keys))))
1945
1946 ;;;###autoload
1947 (defun epg-export-keys-to-file (context keys file)
1948   "Extract public KEYS."
1949   (unwind-protect
1950       (progn
1951         (if keys
1952             (epg-context-set-output-file context file)
1953           (epg-context-set-output-file context
1954                                        (epg--make-temp-file "epg-output")))
1955         (epg-start-export-keys context keys)
1956         (epg-wait-for-completion context)
1957         (if (epg-context-result-for context 'error)
1958             (error "Export keys failed: %S"
1959                    (epg-context-result-for context 'error)))
1960         (unless file
1961           (epg-read-output context)))
1962     (unless file
1963       (epg-delete-output-file context))
1964     (epg-reset context)))
1965
1966 ;;;###autoload
1967 (defun epg-export-keys-to-string (context keys)
1968   "Extract public KEYS and return them as a string."
1969   (epg-export-keys-to-file context keys nil))
1970
1971 ;;;###autoload
1972 (defun epg-start-import-keys (context keys)
1973   "Initiate an import keys operation.
1974 KEYS is a data object.
1975
1976 If you use this function, you will need to wait for the completion of
1977 `epg-gpg-program' by using `epg-wait-for-completion' and call
1978 `epg-reset' to clear a temporaly output file.
1979 If you are unsure, use synchronous version of this function
1980 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1981   (epg-context-set-operation context 'import-keys)
1982   (epg-context-set-result context nil)
1983   (epg--start context (if (epg-data-file keys)
1984                           (list "--import" "--" (epg-data-file keys))
1985                         (list "--import")))
1986   (when (epg-data-string keys)
1987     (if (eq (process-status (epg-context-process context)) 'run)
1988         (process-send-string (epg-context-process context)
1989                              (epg-data-string keys)))
1990     (if (eq (process-status (epg-context-process context)) 'run)
1991         (process-send-eof (epg-context-process context)))))
1992
1993 (defun epg--import-keys-1 (context keys)
1994   (unwind-protect
1995       (progn
1996         (epg-start-import-keys context keys)
1997         (epg-wait-for-completion context)
1998         (if (epg-context-result-for context 'error)
1999             (error "Import keys failed: %S"
2000                    (epg-context-result-for context 'error))))
2001     (epg-reset context)))
2002
2003 ;;;###autoload
2004 (defun epg-import-keys-from-file (context keys)
2005   "Add keys from a file KEYS."
2006   (epg--import-keys-1 context (epg-make-data-from-file keys)))
2007
2008 ;;;###autoload
2009 (defun epg-import-keys-from-string (context keys)
2010   "Add keys from a string KEYS."
2011   (epg--import-keys-1 context (epg-make-data-from-string keys)))
2012
2013 ;;;###autoload
2014 (defun epg-start-receive-keys (context key-id-list)
2015   "Initiate a receive key operation.
2016 KEY-ID-LIST is a list of key IDs.
2017
2018 If you use this function, you will need to wait for the completion of
2019 `epg-gpg-program' by using `epg-wait-for-completion' and call
2020 `epg-reset' to clear a temporaly output file.
2021 If you are unsure, use synchronous version of this function
2022 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2023   (epg-context-set-operation context 'receive-keys)
2024   (epg-context-set-result context nil)
2025   (epg--start context (cons "--recv-keys" key-id-list)))
2026
2027 ;;;###autoload
2028 (defun epg-receive-keys (context keys)
2029   "Add keys from server.
2030 KEYS is a list of key IDs"
2031   (unwind-protect
2032       (progn
2033         (epg-start-receive-keys context keys)
2034         (epg-wait-for-completion context)
2035         (if (epg-context-result-for context 'error)
2036             (error "Receive keys failed: %S"
2037                    (epg-context-result-for context 'error))))
2038     (epg-reset context)))
2039
2040 ;;;###autoload
2041 (defalias 'epg-import-keys-from-server 'epg-receive-keys)
2042
2043 ;;;###autoload
2044 (defun epg-start-delete-keys (context keys &optional allow-secret)
2045   "Initiate an delete keys operation.
2046
2047 If you use this function, you will need to wait for the completion of
2048 `epg-gpg-program' by using `epg-wait-for-completion' and call
2049 `epg-reset' to clear a temporaly output file.
2050 If you are unsure, use synchronous version of this function
2051 `epg-delete-keys' instead."
2052   (epg-context-set-operation context 'delete-keys)
2053   (epg-context-set-result context nil)
2054   (epg--start context (cons (if allow-secret
2055                                "--delete-secret-key"
2056                              "--delete-key")
2057                            (mapcar
2058                             (lambda (key)
2059                               (epg-sub-key-id
2060                                (car (epg-key-sub-key-list key))))
2061                             keys))))
2062
2063 ;;;###autoload
2064 (defun epg-delete-keys (context keys &optional allow-secret)
2065   "Delete KEYS from the key ring."
2066   (unwind-protect
2067       (progn
2068         (epg-start-delete-keys context keys allow-secret)
2069         (epg-wait-for-completion context)
2070         (if (epg-context-result-for context 'error)
2071             (error "Delete keys failed: %S"
2072                    (epg-context-result-for context 'error))))
2073     (epg-reset context)))
2074
2075 ;;;###autoload
2076 (defun epg-start-sign-keys (context keys &optional local)
2077   "Initiate an sign keys operation.
2078
2079 If you use this function, you will need to wait for the completion of
2080 `epg-gpg-program' by using `epg-wait-for-completion' and call
2081 `epg-reset' to clear a temporaly output file.
2082 If you are unsure, use synchronous version of this function
2083 `epg-sign-keys' instead."
2084   (epg-context-set-operation context 'sign-keys)
2085   (epg-context-set-result context nil)
2086   (epg--start context (cons (if local
2087                                "--lsign-key"
2088                              "--sign-key")
2089                            (mapcar
2090                             (lambda (key)
2091                               (epg-sub-key-id
2092                                (car (epg-key-sub-key-list key))))
2093                             keys))))
2094
2095 ;;;###autoload
2096 (defun epg-sign-keys (context keys &optional local)
2097   "Sign KEYS from the key ring."
2098   (unwind-protect
2099       (progn
2100         (epg-start-sign-keys context keys local)
2101         (epg-wait-for-completion context)
2102         (if (epg-context-result-for context 'error)
2103             (error "Sign keys failed: %S"
2104                    (epg-context-result-for context 'error))))
2105     (epg-reset context)))
2106
2107 ;;;###autoload
2108 (defun epg-start-generate-key (context parameters)
2109   "Initiate a key generation.
2110 PARAMETERS specifies parameters for the key.
2111
2112 If you use this function, you will need to wait for the completion of
2113 `epg-gpg-program' by using `epg-wait-for-completion' and call
2114 `epg-reset' to clear a temporaly output file.
2115 If you are unsure, use synchronous version of this function
2116 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2117   (epg-context-set-operation context 'generate-key)
2118   (epg-context-set-result context nil)
2119   (if (epg-data-file parameters)
2120       (epg--start context (list "--batch" "--genkey" "--"
2121                                (epg-data-file parameters)))
2122     (epg--start context '("--batch" "--genkey"))
2123     (if (eq (process-status (epg-context-process context)) 'run)
2124         (process-send-string (epg-context-process context)
2125                              (epg-data-string parameters)))
2126     (if (eq (process-status (epg-context-process context)) 'run)
2127         (process-send-eof (epg-context-process context)))))
2128
2129 ;;;###autoload
2130 (defun epg-generate-key-from-file (context parameters)
2131   "Generate a new key pair.
2132 PARAMETERS is a file which tells how to create the key."
2133   (unwind-protect
2134       (progn
2135         (epg-start-generate-key context (epg-make-data-from-file parameters))
2136         (epg-wait-for-completion context)
2137         (if (epg-context-result-for context 'error)
2138             (error "Generate key failed: %S"
2139                    (epg-context-result-for context 'error))))
2140     (epg-reset context)))
2141
2142 ;;;###autoload
2143 (defun epg-generate-key-from-string (context parameters)
2144   "Generate a new key pair.
2145 PARAMETERS is a string which tells how to create the key."
2146   (unwind-protect
2147       (progn
2148         (epg-start-generate-key context (epg-make-data-from-string parameters))
2149         (epg-wait-for-completion context)
2150         (if (epg-context-result-for context 'error)
2151             (error "Generate key failed: %S"
2152                    (epg-context-result-for context 'error))))
2153     (epg-reset context)))
2154
2155 (defun epg--decode-hexstring (string)
2156   (let ((index 0))
2157     (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
2158       (setq string (replace-match "\\x\\&" t nil string)
2159             index (+ index 4)))
2160     (car (read-from-string (concat "\"" string "\"")))))
2161
2162 (defun epg--decode-quotedstring (string)
2163   (let ((index 0))
2164     (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2165 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
2166                          string index)
2167       (if (match-beginning 2)
2168           (setq string (replace-match "\\2" t nil string)
2169                 index (1+ index))
2170         (if (match-beginning 3)
2171             (setq string (replace-match "\\x\\3" t nil string)
2172                   index (+ index 4))
2173           (setq string (replace-match "\\\\\\\\\\4" t nil string)
2174                 index (+ index 3)))))
2175     (car (read-from-string (concat "\"" string "\"")))))
2176
2177 (defun epg-dn-from-string (string)
2178   "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2179 The return value is an alist mapping from types to values."
2180   (let ((index 0)
2181         (length (length string))
2182         alist type value group)
2183     (while (< index length)
2184       (if (eq index (string-match "[ \t\n\r]*" string index))
2185           (setq index (match-end 0)))
2186       (if (eq index (string-match
2187                      "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2188                      string index))
2189           (setq type (match-string 1 string)
2190                 index (match-end 0))
2191         (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2192                                     string index))
2193             (setq type (match-string 1 string)
2194                   index (match-end 0))))
2195       (unless type
2196         (error "Invalid type"))
2197       (if (eq index (string-match
2198                      "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2199                      string index))
2200           (setq index (match-end 0)
2201                 value (epg--decode-quotedstring (match-string 0 string)))
2202         (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
2203             (setq index (match-end 0)
2204                   value (epg--decode-hexstring (match-string 1 string)))
2205           (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2206                                       string index))
2207               (setq index (match-end 0)
2208                     value (epg--decode-quotedstring
2209                            (match-string 0 string))))))
2210       (if group
2211           (if (stringp (car (car alist)))
2212               (setcar alist (list (cons type value) (car alist)))
2213             (setcar alist (cons (cons type value) (car alist))))
2214         (if (consp (car (car alist)))
2215             (setcar alist (nreverse (car alist))))
2216         (setq alist (cons (cons type value) alist)
2217               type nil
2218               value nil))
2219       (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
2220           (setq index (match-end 0)
2221                 group (eq (aref string (match-beginning 1)) ?+))))
2222     (nreverse alist)))
2223
2224 (defun epg-decode-dn (alist)
2225   "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2226 Type names are resolved using `epg-dn-type-alist'."
2227   (mapconcat
2228    (lambda (rdn)
2229      (if (stringp (car rdn))
2230          (let ((entry (assoc (car rdn) epg-dn-type-alist)))
2231            (if entry
2232                (format "%s=%s" (cdr entry) (cdr rdn))
2233              (format "%s=%s" (car rdn) (cdr rdn))))
2234        (concat "(" (epg-decode-dn rdn) ")")))
2235    alist
2236    ", "))
2237
2238 (provide 'epg)
2239
2240 ;;; epg.el ends here