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