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