* epg.el (epg-new-signature-type-alist): New variable.
[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-start (context args)
662   "Start `epg-gpg-program' in a subprocess with given ARGS."
663   (if (and (epg-context-process context)
664            (eq (process-status (epg-context-process context)) 'run))
665       (error "%s is already running in this context"
666              (if (eq (epg-context-protocol context) 'CMS)
667                  epg-gpgsm-program
668                epg-gpg-program)))
669   (let* ((args (append (list "--no-tty"
670                              "--status-fd" "1"
671                              "--yes")
672                        (unless (eq (epg-context-protocol context) 'CMS)
673                          (list "--command-fd" "0"))
674                        (if (epg-context-armor context) '("--armor"))
675                        (if (epg-context-textmode context) '("--textmode"))
676                        (if (epg-context-output-file context)
677                            (list "--output" (epg-context-output-file context)))
678                        args))
679          (coding-system-for-write 'binary)
680          process-connection-type
681          (orig-mode (default-file-modes))
682          (buffer (generate-new-buffer " *epg*"))
683          process)
684     (if epg-debug
685         (save-excursion
686           (unless epg-debug-buffer
687             (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
688           (set-buffer epg-debug-buffer)
689           (goto-char (point-max))
690           (insert (format "%s %s\n"
691                           (if (eq (epg-context-protocol context) 'CMS)
692                               epg-gpgsm-program
693                            epg-gpg-program)
694                           (mapconcat #'identity args " ")))))
695     (with-current-buffer buffer
696       (make-local-variable 'epg-read-point)
697       (setq epg-read-point (point-min))
698       (make-local-variable 'epg-pending-status-list)
699       (setq epg-pending-status-list nil)
700       (make-local-variable 'epg-key-id)
701       (setq epg-key-id nil)
702       (make-local-variable 'epg-context)
703       (setq epg-context context))
704     (unwind-protect
705         (progn
706           (set-default-file-modes 448)
707           (setq process
708                 (apply #'start-process "epg" buffer
709                        (if (eq (epg-context-protocol context) 'CMS)
710                            epg-gpgsm-program
711                          epg-gpg-program)
712                        args)))
713       (set-default-file-modes orig-mode))
714     (set-process-filter process #'epg-process-filter)
715     (epg-context-set-process context process)))
716
717 (defun epg-process-filter (process input)
718   (if epg-debug
719       (save-excursion
720         (unless epg-debug-buffer
721           (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
722         (set-buffer epg-debug-buffer)
723         (goto-char (point-max))
724         (insert input)))
725   (if (buffer-live-p (process-buffer process))
726       (save-excursion
727         (set-buffer (process-buffer process))
728         (goto-char (point-max))
729         (insert input)
730         (goto-char epg-read-point)
731         (beginning-of-line)
732         (while (looking-at ".*\n")      ;the input line finished
733           (save-excursion
734             (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
735                 (let* ((status (match-string 1))
736                        (string (match-string 2))
737                        (symbol (intern-soft (concat "epg-status-" status))))
738                   (if (member status epg-pending-status-list)
739                       (setq epg-pending-status-list nil))
740                   (if (and symbol
741                            (fboundp symbol))
742                       (funcall symbol process string)))))
743           (forward-line))
744         (setq epg-read-point (point)))))
745
746 (defun epg-read-output (context)
747   "Read the output file CONTEXT and return the content as a string."
748   (with-temp-buffer
749     (if (fboundp 'set-buffer-multibyte)
750         (set-buffer-multibyte nil))
751     (if (file-exists-p (epg-context-output-file context))
752         (let ((coding-system-for-read 'binary))
753           (insert-file-contents (epg-context-output-file context))
754           (buffer-string)))))
755
756 (defun epg-wait-for-status (context status-list)
757   "Wait until one of elements in STATUS-LIST arrives."
758   (with-current-buffer (process-buffer (epg-context-process context))
759     (setq epg-pending-status-list status-list)
760     (while (and (eq (process-status (epg-context-process context)) 'run)
761                 epg-pending-status-list)
762       (accept-process-output (epg-context-process context) 0 1))))
763
764 (defun epg-wait-for-completion (context)
765   "Wait until the `epg-gpg-program' process completes."
766   (while (eq (process-status (epg-context-process context)) 'run)
767     (accept-process-output (epg-context-process context) 0 1)))
768
769 (defun epg-reset (context)
770   "Reset the CONTEXT."
771   (if (and (epg-context-process context)
772            (buffer-live-p (process-buffer (epg-context-process context))))
773       (kill-buffer (process-buffer (epg-context-process context))))
774   (epg-context-set-process context nil))
775
776 (defun epg-delete-output-file (context)
777   "Delete the output file of CONTEXT."
778   (if (and (epg-context-output-file context)
779            (file-exists-p (epg-context-output-file context)))
780       (delete-file (epg-context-output-file context))))
781
782 (defun epg-status-USERID_HINT (process string)
783   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
784       (let* ((key-id (match-string 1 string))
785              (user-id (match-string 2 string))
786              (entry (assoc key-id epg-user-id-alist)))
787         (if entry
788             (setcdr entry user-id)
789           (setq epg-user-id-alist (cons (cons key-id user-id)
790                                         epg-user-id-alist))))))
791
792 (defun epg-status-NEED_PASSPHRASE (process string)
793   (if (string-match "\\`\\([^ ]+\\)" string)
794       (setq epg-key-id (match-string 1 string))))
795
796 (defun epg-status-NEED_PASSPHRASE_SYM (process string)
797   (setq epg-key-id 'SYM))
798
799 (defun epg-status-NEED_PASSPHRASE_PIN (process string)
800   (setq epg-key-id 'PIN))
801
802 (defun epg-status-GET_HIDDEN (process string)
803   (if (and epg-key-id
804            (string-match "\\`passphrase\\." string))
805       (let (inhibit-quit
806             passphrase
807             passphrase-with-new-line)
808         (unwind-protect
809             (condition-case nil
810                 (progn
811                   (setq passphrase
812                         (funcall
813                          (if (consp (epg-context-passphrase-callback
814                                      epg-context))
815                              (car (epg-context-passphrase-callback
816                                    epg-context))
817                            (epg-context-passphrase-callback epg-context))
818                          epg-context
819                          epg-key-id
820                          (if (consp (epg-context-passphrase-callback
821                                      epg-context))
822                              (cdr (epg-context-passphrase-callback
823                                    epg-context)))))
824                   (when passphrase
825                     (setq passphrase-with-new-line (concat passphrase "\n"))
826                     (fillarray passphrase 0)
827                     (setq passphrase nil)
828                     (process-send-string process passphrase-with-new-line)))
829               (quit
830                (epg-context-set-result-for
831                 epg-context 'error
832                 (cons '(quit)
833                       (epg-context-result-for epg-context 'error)))
834                (delete-process process)))
835           (if passphrase
836               (fillarray passphrase 0))
837           (if passphrase-with-new-line
838               (fillarray passphrase-with-new-line 0))))))
839
840 (defun epg-status-GET_BOOL (process string)
841   (let ((entry (assoc string epg-prompt-alist))
842         inhibit-quit)
843     (condition-case nil
844       (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
845           (process-send-string process "y\n")
846         (process-send-string process "n\n"))
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
854 (defun epg-status-GET_LINE (process string)
855   (let ((entry (assoc string epg-prompt-alist))
856         inhibit-quit)
857     (condition-case nil
858         (process-send-string
859          process
860          (concat (read-string (if entry (cdr entry) (concat string ": ")))
861                  "\n"))
862       (quit
863        (epg-context-set-result-for
864         epg-context 'error
865         (cons '(quit)
866               (epg-context-result-for epg-context 'error)))
867        (delete-process process)))))
868
869 (defun epg-status-*SIG (status string)
870   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
871       (let* ((key-id (match-string 1 string))
872              (user-id (match-string 2 string))
873              (entry (assoc key-id epg-user-id-alist)))
874         (epg-context-set-result-for
875          epg-context
876          'verify
877          (cons (epg-make-signature status key-id)
878                (epg-context-result-for epg-context 'verify)))
879         (if (eq (epg-context-protocol epg-context) 'CMS)
880             (condition-case nil
881                 (setq user-id (epg-dn-from-string user-id))
882               (error)))
883         (if entry
884             (setcdr entry user-id)
885           (setq epg-user-id-alist
886                 (cons (cons key-id user-id) epg-user-id-alist))))
887     (epg-context-set-result-for
888      epg-context
889      'verify
890      (cons (epg-make-signature status)
891            (epg-context-result-for epg-context 'verify)))))
892
893 (defun epg-status-GOODSIG (process string)
894   (epg-status-*SIG 'good string))
895
896 (defun epg-status-EXPSIG (process string)
897   (epg-status-*SIG 'expired string))
898
899 (defun epg-status-EXPKEYSIG (process string)
900   (epg-status-*SIG 'expired-key string))
901
902 (defun epg-status-REVKEYSIG (process string)
903   (epg-status-*SIG 'revoked-key string))
904
905 (defun epg-status-BADSIG (process string)
906   (epg-status-*SIG 'bad string))
907
908 (defun epg-status-NO_PUBKEY (process string)
909   (epg-context-set-result-for
910      epg-context
911      'verify
912      (cons (epg-make-signature 'no-pubkey string)
913            (epg-context-result-for epg-context 'verify))))
914
915 (defun epg-status-ERRSIG (process string)
916   (let ((signatures (car (epg-context-result-for epg-context 'verify))))
917     (unless signatures
918       (setq signatures (list (epg-make-signature 'error)))
919       (epg-context-set-result-for epg-context 'verify signatures))
920     (when (and (not (eq (epg-signature-status (car signatures)) 'good))
921                (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
922 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
923                              string))
924       (epg-signature-set-key-id
925        (car signatures)
926        (match-string 1 string))
927       (epg-signature-set-pubkey-algorithm
928        (car signatures)
929        (string-to-number (match-string 2 string)))
930       (epg-signature-set-digest-algorithm
931        (car signatures)
932        (string-to-number (match-string 3 string)))
933 ;      (epg-signature-set-class
934 ;       (car signatures)
935 ;       (string-to-number (match-string 4 string) 16))
936       (epg-signature-set-creation-time
937        (car signatures)
938        (match-string 5 string)))))
939
940 (defun epg-status-VALIDSIG (process string)
941   (let ((signature (car (epg-context-result-for epg-context 'verify))))
942     (when (and signature
943                (eq (epg-signature-status signature) 'good)
944                (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
945 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
946 \\(.*\\)"
947                            string))
948       (epg-signature-set-fingerprint
949        signature
950        (match-string 1 string))
951       (epg-signature-set-creation-time
952        signature
953        (match-string 2 string))
954       (epg-signature-set-expiration-time
955        signature
956        (match-string 3 string))
957 ;      (epg-signature-set-version
958 ;       signature
959 ;       (string-to-number (match-string 4 string)))
960       (epg-signature-set-pubkey-algorithm
961        signature 
962        (string-to-number (match-string 5 string)))
963       (epg-signature-set-digest-algorithm
964        signature
965        (string-to-number (match-string 6 string)))
966 ;      (epg-signature-set-class
967 ;       signature
968 ;       (string-to-number (match-string 7 string) 16))
969       )))
970
971 (defun epg-status-TRUST_UNDEFINED (process string)
972   (let ((signature (car (epg-context-result-for epg-context 'verify))))
973     (if (and signature
974              (eq (epg-signature-status signature) 'good))
975         (epg-signature-set-validity signature 'undefined))))
976
977 (defun epg-status-TRUST_NEVER (process string)
978   (let ((signature (car (epg-context-result-for epg-context 'verify))))
979     (if (and signature
980              (eq (epg-signature-status signature) 'good))
981         (epg-signature-set-validity signature 'never))))
982
983 (defun epg-status-TRUST_MARGINAL (process string)
984   (let ((signature (car (epg-context-result-for epg-context 'verify))))
985     (if (and signature
986              (eq (epg-signature-status signature) 'marginal))
987         (epg-signature-set-validity signature 'marginal))))
988
989 (defun epg-status-TRUST_FULLY (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 'full))))
994
995 (defun epg-status-TRUST_ULTIMATE (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 'ultimate))))
1000
1001 (defun epg-status-PROGRESS (process string)
1002   (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1003                     string)
1004       (funcall (if (consp (epg-context-progress-callback epg-context))
1005                    (car (epg-context-progress-callback epg-context))
1006                  (epg-context-progress-callback epg-context))
1007                epg-context
1008                (match-string 1 string)
1009                (match-string 2 string)
1010                (string-to-number (match-string 3 string))
1011                (string-to-number (match-string 4 string))
1012                (if (consp (epg-context-progress-callback epg-context))
1013                    (cdr (epg-context-progress-callback epg-context))))))
1014
1015 (defun epg-status-DECRYPTION_FAILED (process string)
1016   (epg-context-set-result-for
1017    epg-context 'error
1018    (cons '(decryption-failed)
1019          (epg-context-result-for epg-context 'error))))
1020
1021 (defun epg-status-NODATA (process string)
1022   (epg-context-set-result-for
1023    epg-context 'error
1024    (cons (cons 'no-data (string-to-number string))
1025          (epg-context-result-for epg-context 'error))))
1026
1027 (defun epg-status-UNEXPECTED (process string)
1028   (epg-context-set-result-for
1029    epg-context 'error
1030    (cons (cons 'unexpected (string-to-number string))
1031          (epg-context-result-for epg-context 'error))))
1032
1033 (defun epg-status-KEYEXPIRED (process string)
1034   (epg-context-set-result-for
1035    epg-context 'error
1036    (cons (cons 'key-expired string)
1037          (epg-context-result-for epg-context 'error))))
1038
1039 (defun epg-status-KEYREVOKED (process string)
1040   (epg-context-set-result-for
1041    epg-context 'error
1042    (cons '(key-revoked)
1043          (epg-context-result-for epg-context 'error))))
1044
1045 (defun epg-status-BADARMOR (process string)
1046   (epg-context-set-result-for
1047    epg-context 'error
1048    (cons '(bad-armor)
1049          (epg-context-result-for epg-context 'error))))
1050
1051 (defun epg-status-INV_RECP (process string)
1052   (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1053       (epg-context-set-result-for
1054        epg-context 'error
1055        (cons (list 'invalid-recipient
1056                    (string-to-number (match-string 1 string))
1057                    (match-string 2 string))
1058              (epg-context-result-for epg-context 'error)))))
1059
1060 (defun epg-status-NO_RECP (process string)
1061   (epg-context-set-result-for
1062    epg-context 'error
1063    (cons '(no-recipients)
1064          (epg-context-result-for epg-context 'error))))
1065
1066 (defun epg-status-DELETE_PROBLEM (process string)
1067   (if (string-match "\\`\\([0-9]+\\)" string)
1068       (epg-context-set-result-for
1069        epg-context 'error
1070        (cons (cons 'delete-problem (string-to-number (match-string 1 string)))
1071              (epg-context-result-for epg-context 'error)))))
1072
1073 (defun epg-status-SIG_CREATED (process string)
1074   (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1075 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1076       (epg-context-set-result-for
1077        epg-context 'sign
1078        (cons (epg-make-new-signature
1079               (cdr (assq (aref (match-string 1 string) 0)
1080                          epg-new-signature-type-alist))
1081               (string-to-number (match-string 2 string))
1082               (string-to-number (match-string 3 string))
1083               (string-to-number (match-string 4 string) 16)
1084               (match-string 5 string)
1085               (substring string (match-end 0)))
1086              (epg-context-result-for epg-context 'sign)))))
1087
1088 (defun epg-status-KEY_CREATED (process string)
1089   (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1090       (epg-context-set-result-for
1091        epg-context 'generate-key
1092        (cons (list (cons 'type (string-to-char (match-string 1 string)))
1093                    (cons 'fingerprint (match-string 2 string)))
1094              (epg-context-result-for epg-context 'generate-key)))))
1095
1096 (defun epg-status-KEY_NOT_CREATED (process string)
1097   (epg-context-set-result-for
1098    epg-context 'error
1099    (cons '(key-not-created)
1100          (epg-context-result-for epg-context 'error))))
1101
1102 (defun epg-passphrase-callback-function (context key-id handback)
1103   (read-passwd
1104    (if (eq key-id 'SYM)
1105        "Passphrase for symmetric encryption: "
1106      (if (eq key-id 'PIN)
1107          "Passphrase for PIN: "
1108        (let ((entry (assoc key-id epg-user-id-alist)))
1109          (if entry
1110              (format "Passphrase for %s %s: " key-id (cdr entry))
1111            (format "Passphrase for %s: " key-id)))))))
1112
1113 (defun epg-progress-callback-function (context what char current total
1114                                                handback)
1115   (message "%s: %d%%/%d%%" what current total))
1116
1117 (defun epg-configuration ()
1118   "Return a list of internal configuration parameters of `epg-gpg-program'."
1119   (let (config type)
1120     (with-temp-buffer
1121       (apply #'call-process epg-gpg-program nil (list t nil) nil
1122              '("--with-colons" "--list-config"))
1123       (goto-char (point-min))
1124       (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
1125         (setq type (intern (match-string 1))
1126               config (cons (cons type
1127                                  (if (memq type
1128                                            '(pubkey cipher digest compress))
1129                                      (mapcar #'string-to-number
1130                                              (delete "" (split-string
1131                                                          (match-string 2)
1132                                                          ";")))
1133                                    (match-string 2)))
1134                            config))))
1135     config))
1136
1137 (defun epg-list-keys-1 (context name mode)
1138   (let ((args (append (list "--with-colons" "--no-greeting" "--batch"
1139                             "--with-fingerprint"
1140                             "--with-fingerprint"
1141                             (if (or (eq mode t) (eq mode 'secret))
1142                                 "--list-secret-keys"
1143                               (if mode
1144                                   "--list-sigs"
1145                                 "--list-keys")))
1146                       (unless (eq (epg-context-protocol context) 'CMS)
1147                         '("--fixed-list-mode"))
1148                       (if name (list name))))
1149         keys string field index)
1150     (with-temp-buffer
1151       (apply #'call-process
1152              (if (eq (epg-context-protocol context) 'CMS)
1153                  epg-gpgsm-program
1154                epg-gpg-program)
1155              nil (list t nil) nil args)
1156       (goto-char (point-min))
1157       (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1158         (setq keys (cons (make-vector 15 nil) keys)
1159               string (match-string 0)
1160               index 0
1161               field 0)
1162         (while (eq index
1163                    (string-match "\\([^:]+\\)?:" string index))
1164           (setq index (match-end 0))
1165           (aset (car keys) field (match-string 1 string))
1166           (setq field (1+ field))))
1167       (nreverse keys))))
1168
1169 (defun epg-make-sub-key-1 (line)
1170   (epg-make-sub-key
1171    (if (aref line 1)
1172        (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1173    (delq nil
1174          (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1175                  (aref line 11)))
1176    (member (aref line 0) '("sec" "ssb"))
1177    (string-to-number (aref line 3))
1178    (string-to-number (aref line 2))
1179    (aref line 4)
1180    (aref line 5)
1181    (aref line 6)))
1182
1183 (defun epg-list-keys-postprocess-one-key (key)
1184   (let (key-id user-id-string entry)
1185     (epg-key-set-sub-key-list
1186      key
1187      (nreverse (epg-key-sub-key-list key)))
1188     (epg-key-set-user-id-list
1189      key
1190      (nreverse (epg-key-user-id-list key)))
1191     (setq key-id
1192           (epg-sub-key-id (car (epg-key-sub-key-list key)))
1193           user-id-string
1194           (epg-user-id-string (car (epg-key-user-id-list key)))
1195           entry (assoc key-id epg-user-id-alist))
1196     (if entry
1197         (setcdr entry user-id-string)
1198       (setq epg-user-id-alist (cons (cons key-id user-id-string)
1199                                     epg-user-id-alist)))))
1200
1201 (defun epg-list-keys (context &optional name mode)
1202   "Return a list of epg-key objects matched with NAME.
1203 If MODE is nil, only public keyring should be searched.
1204 If MODE is t or 'secret, only secret keyring should be searched. 
1205 Otherwise, only public keyring should be searched and the key
1206 signatures should be included."
1207   (let ((lines (epg-list-keys-1 context name mode))
1208         keys cert)
1209     (while lines
1210       (cond
1211        ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1212         (if (car keys)
1213             (epg-list-keys-postprocess-one-key (car keys)))
1214         (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1215               keys (cons (epg-make-key
1216                           (if (aref (car lines) 8)
1217                               (cdr (assq (string-to-char (aref (car lines) 8))
1218                                          epg-key-validity-alist))))
1219                          keys))
1220         (epg-key-set-sub-key-list
1221          (car keys)
1222          (cons (epg-make-sub-key-1 (car lines))
1223                (epg-key-sub-key-list (car keys)))))
1224        ((member (aref (car lines) 0) '("sub" "ssb"))
1225         (epg-key-set-sub-key-list
1226          (car keys)
1227          (cons (epg-make-sub-key-1 (car lines))
1228                (epg-key-sub-key-list (car keys)))))
1229        ((equal (aref (car lines) 0) "uid")
1230         (epg-key-set-user-id-list
1231          (car keys)
1232          (cons (epg-make-user-id
1233                 (if (aref (car lines) 1)
1234                     (cdr (assq (string-to-char (aref (car lines) 1))
1235                                epg-key-validity-alist)))
1236                 (if cert
1237                     (condition-case nil
1238                         (epg-dn-from-string (aref (car lines) 9))
1239                       (error (aref (car lines) 9)))
1240                   (aref (car lines) 9)))
1241                (epg-key-user-id-list (car keys)))))
1242        ((equal (aref (car lines) 0) "fpr")
1243         (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1244                                      (aref (car lines) 9))))
1245       (setq lines (cdr lines)))
1246     (if (car keys)
1247         (epg-list-keys-postprocess-one-key (car keys)))
1248     (nreverse keys)))
1249
1250 (if (fboundp 'make-temp-file)
1251     (defalias 'epg-make-temp-file 'make-temp-file)
1252   (defvar temporary-file-directory)
1253   ;; stolen from poe.el.
1254   (defun epg-make-temp-file (prefix)
1255     "Create a temporary file.
1256 The returned file name (created by appending some random characters at the end
1257 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1258 is guaranteed to point to a newly created empty file.
1259 You can then use `write-region' to write new data into the file."
1260     (let (tempdir tempfile)
1261       (setq prefix (expand-file-name prefix
1262                                      (if (featurep 'xemacs)
1263                                          (temp-directory)
1264                                        temporary-file-directory)))
1265       (unwind-protect
1266           (let (file)
1267             ;; First, create a temporary directory.
1268             (while (condition-case ()
1269                        (progn
1270                          (setq tempdir (make-temp-name
1271                                         (concat
1272                                          (file-name-directory prefix)
1273                                          "DIR")))
1274                          ;; return nil or signal an error.
1275                          (make-directory tempdir))
1276                      ;; let's try again.
1277                      (file-already-exists t)))
1278             (set-file-modes tempdir 448)
1279             ;; Second, create a temporary file in the tempdir.
1280             ;; There *is* a race condition between `make-temp-name'
1281             ;; and `write-region', but we don't care it since we are
1282             ;; in a private directory now.
1283             (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1284             (write-region "" nil tempfile nil 'silent)
1285             (set-file-modes tempfile 384)
1286             ;; Finally, make a hard-link from the tempfile.
1287             (while (condition-case ()
1288                        (progn
1289                          (setq file (make-temp-name prefix))
1290                          ;; return nil or signal an error.
1291                          (add-name-to-file tempfile file))
1292                      ;; let's try again.
1293                      (file-already-exists t)))
1294             file)
1295         ;; Cleanup the tempfile.
1296         (and tempfile
1297              (file-exists-p tempfile)
1298              (delete-file tempfile))
1299         ;; Cleanup the tempdir.
1300         (and tempdir
1301              (file-directory-p tempdir)
1302              (delete-directory tempdir))))))
1303
1304 ;;;###autoload
1305 (defun epg-cancel (context)
1306   (if (buffer-live-p (process-buffer (epg-context-process context)))
1307       (save-excursion
1308         (set-buffer (process-buffer (epg-context-process context)))
1309         (epg-context-set-result-for
1310          epg-context 'error
1311          (cons '(quit)
1312                (epg-context-result-for epg-context 'error)))))
1313   (if (eq (process-status (epg-context-process context)) 'run)
1314       (delete-process (epg-context-process context))))
1315   
1316 ;;;###autoload
1317 (defun epg-start-decrypt (context cipher)
1318   "Initiate a decrypt operation on CIPHER.
1319 CIPHER is a data object.
1320
1321 If you use this function, you will need to wait for the completion of
1322 `epg-gpg-program' by using `epg-wait-for-completion' and call
1323 `epg-reset' to clear a temporaly output file.
1324 If you are unsure, use synchronous version of this function
1325 `epg-decrypt-file' or `epg-decrypt-string' instead."
1326   (unless (epg-data-file cipher)
1327     (error "Not a file"))
1328   (epg-context-set-result context nil)
1329   (epg-start context (list "--decrypt" (epg-data-file cipher)))
1330   ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1331   (unless (eq (epg-context-protocol context) 'CMS)
1332     (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1333
1334 ;;;###autoload
1335 (defun epg-decrypt-file (context cipher plain)
1336   "Decrypt a file CIPHER and store the result to a file PLAIN.
1337 If PLAIN is nil, it returns the result as a string."
1338   (unwind-protect
1339       (progn
1340         (if plain
1341             (epg-context-set-output-file context plain)
1342           (epg-context-set-output-file context
1343                                        (epg-make-temp-file "epg-output")))
1344         (epg-start-decrypt context (epg-make-data-from-file cipher))
1345         (epg-wait-for-completion context)
1346         (if (epg-context-result-for context 'error)
1347             (error "Decrypt failed: %S"
1348                    (epg-context-result-for context 'error)))
1349         (unless plain
1350           (epg-read-output context)))
1351     (unless plain
1352       (epg-delete-output-file context))
1353     (epg-reset context)))
1354
1355 ;;;###autoload
1356 (defun epg-decrypt-string (context cipher)
1357   "Decrypt a string CIPHER and return the plain text."
1358   (let ((input-file (epg-make-temp-file "epg-input"))
1359         (coding-system-for-write 'binary))
1360     (unwind-protect
1361         (progn
1362           (write-region cipher nil input-file nil 'quiet)
1363           (epg-context-set-output-file context
1364                                        (epg-make-temp-file "epg-output"))
1365           (epg-start-decrypt context (epg-make-data-from-file input-file))
1366           (epg-wait-for-completion context)
1367           (if (epg-context-result-for context 'error)
1368               (error "Decrypt failed: %S"
1369                      (epg-context-result-for context 'error)))
1370           (epg-read-output context))
1371       (epg-delete-output-file context)
1372       (if (file-exists-p input-file)
1373           (delete-file input-file))
1374       (epg-reset context))))
1375
1376 ;;;###autoload
1377 (defun epg-start-verify (context signature &optional signed-text)
1378   "Initiate a verify operation on SIGNATURE.
1379 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1380
1381 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1382 For a normal or a clear text signature, SIGNED-TEXT should be nil.
1383
1384 If you use this function, you will need to wait for the completion of
1385 `epg-gpg-program' by using `epg-wait-for-completion' and call
1386 `epg-reset' to clear a temporaly output file.
1387 If you are unsure, use synchronous version of this function
1388 `epg-verify-file' or `epg-verify-string' instead."
1389   (epg-context-set-result context nil)
1390   (if signed-text
1391       ;; Detached signature.
1392       (if (epg-data-file signed-text)
1393           (epg-start context (list "--verify" (epg-data-file signature)
1394                                    (epg-data-file signed-text)))
1395         (epg-start context (list "--verify" (epg-data-file signature) "-"))
1396         (if (eq (process-status (epg-context-process context)) 'run)
1397             (process-send-string (epg-context-process context)
1398                                  (epg-data-string signed-text)))
1399         (if (eq (process-status (epg-context-process context)) 'run)
1400             (process-send-eof (epg-context-process context))))
1401     ;; Normal (or cleartext) signature.
1402     (if (epg-data-file signature)
1403         (epg-start context (list "--verify" (epg-data-file signature)))
1404       (epg-start context (list "--verify"))
1405       (if (eq (process-status (epg-context-process context)) 'run)
1406           (process-send-string (epg-context-process context)
1407                                (epg-data-string signature)))
1408       (if (eq (process-status (epg-context-process context)) 'run)
1409           (process-send-eof (epg-context-process context))))))
1410
1411 ;;;###autoload
1412 (defun epg-verify-file (context signature &optional signed-text plain)
1413   "Verify a file SIGNATURE.
1414 SIGNED-TEXT and PLAIN are also a file if they are specified.
1415
1416 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1417 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1418   (unwind-protect
1419       (progn
1420         (if plain
1421             (epg-context-set-output-file context plain)
1422           (epg-context-set-output-file context
1423                                        (epg-make-temp-file "epg-output")))
1424         (if signed-text
1425             (epg-start-verify context
1426                               (epg-make-data-from-file signature)
1427                               (epg-make-data-from-file signed-text))
1428           (epg-start-verify context
1429                             (epg-make-data-from-file signature)))
1430         (epg-wait-for-completion context)
1431 ;       (if (epg-context-result-for context 'error)
1432 ;           (error "Verify failed: %S"
1433 ;                  (epg-context-result-for context 'error)))
1434         (unless plain
1435           (epg-read-output context)))
1436     (unless plain
1437       (epg-delete-output-file context))
1438     (epg-reset context)))
1439
1440 ;;;###autoload
1441 (defun epg-verify-string (context signature &optional signed-text)
1442   "Verify a string SIGNATURE.
1443 SIGNED-TEXT is a string if it is specified.
1444
1445 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1446 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1447   (let ((coding-system-for-write 'binary)
1448         input-file)
1449     (unwind-protect
1450         (progn
1451           (epg-context-set-output-file context
1452                                        (epg-make-temp-file "epg-output"))
1453           (if signed-text
1454               (progn
1455                 (setq input-file (epg-make-temp-file "epg-signature"))
1456                 (write-region signature nil input-file nil 'quiet)
1457                 (epg-start-verify context
1458                                   (epg-make-data-from-file input-file)
1459                                   (epg-make-data-from-string signed-text)))
1460             (epg-start-verify context (epg-make-data-from-string signature)))
1461           (epg-wait-for-completion context)
1462 ;         (if (epg-context-result-for context 'error)
1463 ;             (error "Verify failed: %S"
1464 ;                    (epg-context-result-for context 'error)))
1465           (epg-read-output context))
1466       (epg-delete-output-file context)
1467       (if (and input-file
1468                (file-exists-p input-file))
1469           (delete-file input-file))
1470       (epg-reset context))))
1471
1472 ;;;###autoload
1473 (defun epg-start-sign (context plain &optional mode)
1474   "Initiate a sign operation on PLAIN.
1475 PLAIN is a data object.
1476
1477 If optional 3rd argument MODE is 'clear, it makes a clear text signature.
1478 If MODE is t or 'detached, it makes a detached signature.
1479 Otherwise, it makes a normal signature.
1480
1481 If you use this function, you will need to wait for the completion of
1482 `epg-gpg-program' by using `epg-wait-for-completion' and call
1483 `epg-reset' to clear a temporaly output file.
1484 If you are unsure, use synchronous version of this function
1485 `epg-sign-file' or `epg-sign-string' instead."
1486   (epg-context-set-result context nil)
1487   (epg-start context
1488              (append (list (if (eq mode 'clear)
1489                                "--clearsign"
1490                              (if (or (eq mode t) (eq mode 'detached))
1491                                  "--detach-sign"
1492                                "--sign")))
1493                      (apply #'nconc
1494                             (mapcar
1495                              (lambda (signer)
1496                                (list "-u"
1497                                      (epg-sub-key-id
1498                                       (car (epg-key-sub-key-list signer)))))
1499                              (epg-context-signers context)))
1500                      (if (epg-data-file plain)
1501                          (list (epg-data-file plain)))))
1502   ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1503   (unless (eq (epg-context-protocol context) 'CMS)
1504     (epg-wait-for-status context '("BEGIN_SIGNING")))
1505   (when (epg-data-string plain)
1506     (if (eq (process-status (epg-context-process context)) 'run)
1507         (process-send-string (epg-context-process context)
1508                              (epg-data-string plain)))
1509     (if (eq (process-status (epg-context-process context)) 'run)
1510         (process-send-eof (epg-context-process context)))))
1511
1512 ;;;###autoload
1513 (defun epg-sign-file (context plain signature &optional mode)
1514   "Sign a file PLAIN and store the result to a file SIGNATURE.
1515 If SIGNATURE is nil, it returns the result as a string.
1516 If optional 3rd argument MODE is 'clear, it makes a clear text signature.
1517 If MODE is t or 'detached, it makes a detached signature.
1518 Otherwise, it makes a normal signature."
1519   (unwind-protect
1520       (progn
1521         (if signature
1522             (epg-context-set-output-file context signature)
1523           (epg-context-set-output-file context
1524                                        (epg-make-temp-file "epg-output")))
1525         (epg-start-sign context (epg-make-data-from-file plain) mode)
1526         (epg-wait-for-completion context)
1527         (unless (epg-context-result-for context 'sign)
1528           (if (epg-context-result-for context 'error)
1529               (error "Sign failed: %S"
1530                      (epg-context-result-for context 'error))
1531             (error "Sign failed")))
1532         (unless signature
1533           (epg-read-output context)))
1534     (unless signature
1535       (epg-delete-output-file context))
1536     (epg-reset context)))
1537
1538 ;;;###autoload
1539 (defun epg-sign-string (context plain &optional mode)
1540   "Sign a string PLAIN and return the output as string.
1541 If optional 3rd argument MODE is 'clear, it makes a clear text signature.
1542 If MODE is t or 'detached, it makes a detached signature.
1543 Otherwise, it makes a normal signature."
1544   (unwind-protect
1545       (progn
1546         (epg-context-set-output-file context
1547                                      (epg-make-temp-file "epg-output"))
1548         (epg-start-sign context (epg-make-data-from-string plain) mode)
1549         (epg-wait-for-completion context)
1550         (unless (epg-context-result-for context 'sign)
1551           (if (epg-context-result-for context 'error)
1552               (error "Sign failed: %S"
1553                      (epg-context-result-for context 'error))
1554             (error "Sign failed")))
1555         (epg-read-output context))
1556     (epg-delete-output-file context)
1557     (epg-reset context)))
1558
1559 ;;;###autoload
1560 (defun epg-start-encrypt (context plain recipients
1561                                   &optional sign always-trust)
1562   "Initiate an encrypt operation on PLAIN.
1563 PLAIN is a data object.
1564 If RECIPIENTS is nil, it performs symmetric encryption.
1565
1566 If you use this function, you will need to wait for the completion of
1567 `epg-gpg-program' by using `epg-wait-for-completion' and call
1568 `epg-reset' to clear a temporaly output file.
1569 If you are unsure, use synchronous version of this function
1570 `epg-encrypt-file' or `epg-encrypt-string' instead."
1571   (epg-context-set-result context nil)
1572   (epg-start context
1573              (append (if always-trust '("--always-trust"))
1574                      (if recipients '("--encrypt") '("--symmetric"))
1575                      (if sign
1576                          (cons "--sign"
1577                                (apply #'nconc
1578                                       (mapcar (lambda (signer)
1579                                                 (list "-u" signer))
1580                                               (epg-context-signers context)))))
1581                      (apply #'nconc
1582                             (mapcar
1583                              (lambda (recipient)
1584                                (list "-r"
1585                                      (epg-sub-key-id
1586                                       (car (epg-key-sub-key-list recipient)))))
1587                              recipients))
1588                      (if (epg-data-file plain)
1589                          (list (epg-data-file plain)))))
1590   ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1591   (unless (eq (epg-context-protocol context) 'CMS)
1592     (if sign
1593         (epg-wait-for-status context '("BEGIN_SIGNING"))
1594       (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
1595   (when (epg-data-string plain)
1596     (if (eq (process-status (epg-context-process context)) 'run)
1597         (process-send-string (epg-context-process context)
1598                              (epg-data-string plain)))
1599     (if (eq (process-status (epg-context-process context)) 'run)
1600         (process-send-eof (epg-context-process context)))))
1601
1602 ;;;###autoload
1603 (defun epg-encrypt-file (context plain recipients
1604                                  cipher &optional sign always-trust)
1605   "Encrypt a file PLAIN and store the result to a file CIPHER.
1606 If CIPHER is nil, it returns the result as a string.
1607 If RECIPIENTS is nil, it performs symmetric encryption."
1608   (unwind-protect
1609       (progn
1610         (if cipher
1611             (epg-context-set-output-file context cipher)
1612           (epg-context-set-output-file context
1613                                        (epg-make-temp-file "epg-output")))
1614         (epg-start-encrypt context (epg-make-data-from-file plain)
1615                            recipients sign always-trust)
1616         (epg-wait-for-completion context)
1617         (if (and sign
1618                  (not (epg-context-result-for context 'sign)))
1619             (if (epg-context-result-for context 'error)
1620                 (error "Sign failed: %S"
1621                        (epg-context-result-for context 'error))
1622                 (error "Sign failed")))
1623         (if (epg-context-result-for context 'error)
1624             (error "Encrypt failed: %S"
1625                    (epg-context-result-for context 'error)))
1626         (unless cipher
1627           (epg-read-output context)))
1628     (unless cipher
1629       (epg-delete-output-file context))
1630     (epg-reset context)))
1631
1632 ;;;###autoload
1633 (defun epg-encrypt-string (context plain recipients
1634                                    &optional sign always-trust)
1635   "Encrypt a string PLAIN.
1636 If RECIPIENTS is nil, it performs symmetric encryption."
1637   (unwind-protect
1638       (progn
1639         (epg-context-set-output-file context
1640                                      (epg-make-temp-file "epg-output"))
1641         (epg-start-encrypt context (epg-make-data-from-string plain)
1642                            recipients sign always-trust)
1643         (epg-wait-for-completion context)
1644         (if (and sign
1645                  (not (epg-context-result-for context 'sign)))
1646             (if (epg-context-result-for context 'error)
1647                 (error "Sign failed: %S"
1648                        (epg-context-result-for context 'error))
1649               (error "Sign failed")))
1650         (if (epg-context-result-for context 'error)
1651             (error "Encrypt failed: %S"
1652                    (epg-context-result-for context 'error)))
1653         (epg-read-output context))
1654     (epg-delete-output-file context)
1655     (epg-reset context)))
1656
1657 ;;;###autoload
1658 (defun epg-start-export-keys (context keys)
1659   "Initiate an export keys operation.
1660
1661 If you use this function, you will need to wait for the completion of
1662 `epg-gpg-program' by using `epg-wait-for-completion' and call
1663 `epg-reset' to clear a temporaly output file.
1664 If you are unsure, use synchronous version of this function
1665 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1666   (epg-context-set-result context nil)
1667   (epg-start context (cons "--export"
1668                            (mapcar
1669                             (lambda (key)
1670                               (epg-sub-key-id
1671                                (car (epg-key-sub-key-list key))))
1672                             keys))))
1673
1674 ;;;###autoload
1675 (defun epg-export-keys-to-file (context keys file)
1676   "Extract public KEYS."
1677   (unwind-protect
1678       (progn
1679         (if keys
1680             (epg-context-set-output-file context file)
1681           (epg-context-set-output-file context
1682                                        (epg-make-temp-file "epg-output")))
1683         (epg-start-export-keys context keys)
1684         (epg-wait-for-completion context)
1685         (if (epg-context-result-for context 'error)
1686             (error "Export keys failed: %S"
1687                    (epg-context-result-for context 'error)))
1688         (unless file
1689           (epg-read-output context)))
1690     (unless file
1691       (epg-delete-output-file context))
1692     (epg-reset context)))
1693
1694 ;;;###autoload
1695 (defun epg-export-keys-to-string (context keys)
1696   "Extract public KEYS and return them as a string."
1697   (epg-export-keys-to-file context keys nil))
1698
1699 ;;;###autoload
1700 (defun epg-start-import-keys (context keys)
1701   "Initiate an import keys operation.
1702 KEYS is a data object.
1703
1704 If you use this function, you will need to wait for the completion of
1705 `epg-gpg-program' by using `epg-wait-for-completion' and call
1706 `epg-reset' to clear a temporaly output file.
1707 If you are unsure, use synchronous version of this function
1708 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1709   (epg-context-set-result context nil)
1710   (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
1711   (epg-start context (list "--import" (epg-data-file keys)))
1712   (when (epg-data-string keys)
1713     (if (eq (process-status (epg-context-process context)) 'run)
1714         (process-send-string (epg-context-process context)
1715                              (epg-data-string keys)))
1716     (if (eq (process-status (epg-context-process context)) 'run)
1717         (process-send-eof (epg-context-process context)))))
1718
1719 (defun epg-import-keys-1 (context keys)
1720   (unwind-protect
1721       (progn
1722         (epg-start-import-keys context keys)
1723         (epg-wait-for-completion context)
1724         (if (epg-context-result-for context 'error)
1725             (error "Import keys failed: %S"
1726                    (epg-context-result-for context 'error)))
1727         (epg-read-output context))
1728     (epg-reset context)))
1729
1730 ;;;###autoload
1731 (defun epg-import-keys-from-file (context keys)
1732   "Add keys from a file KEYS."
1733   (epg-import-keys-1 context (epg-make-data-from-file keys)))
1734
1735 ;;;###autoload
1736 (defun epg-import-keys-from-string (context keys)
1737   "Add keys from a string KEYS."
1738   (epg-import-keys-1 context (epg-make-data-from-string keys)))
1739
1740 ;;;###autoload
1741 (defun epg-start-delete-keys (context keys &optional allow-secret)
1742   "Initiate an delete keys operation.
1743
1744 If you use this function, you will need to wait for the completion of
1745 `epg-gpg-program' by using `epg-wait-for-completion' and call
1746 `epg-reset' to clear a temporaly output file.
1747 If you are unsure, use synchronous version of this function
1748 `epg-delete-keys' instead."
1749   (epg-context-set-result context nil)
1750   (epg-start context (cons (if allow-secret
1751                                "--delete-secret-key"
1752                              "--delete-key")
1753                            (mapcar
1754                             (lambda (key)
1755                               (epg-sub-key-id
1756                                (car (epg-key-sub-key-list key))))
1757                             keys))))
1758
1759 ;;;###autoload
1760 (defun epg-delete-keys (context keys &optional allow-secret)
1761   "Delete KEYS from the key ring."
1762   (unwind-protect
1763       (progn
1764         (epg-start-delete-keys context keys allow-secret)
1765         (epg-wait-for-completion context)
1766         (if (epg-context-result-for context 'error)
1767             (error "Delete keys failed: %S"
1768                    (epg-context-result-for context 'error))))
1769     (epg-reset context)))
1770
1771 ;;;###autoload
1772 (defun epg-start-sign-keys (context keys &optional local)
1773   "Initiate an sign keys operation.
1774
1775 If you use this function, you will need to wait for the completion of
1776 `epg-gpg-program' by using `epg-wait-for-completion' and call
1777 `epg-reset' to clear a temporaly output file.
1778 If you are unsure, use synchronous version of this function
1779 `epg-sign-keys' instead."
1780   (epg-context-set-result context nil)
1781   (epg-start context (cons (if local
1782                                "--lsign-key"
1783                              "--sign-key")
1784                            (mapcar
1785                             (lambda (key)
1786                               (epg-sub-key-id
1787                                (car (epg-key-sub-key-list key))))
1788                             keys))))
1789
1790 ;;;###autoload
1791 (defun epg-sign-keys (context keys &optional local)
1792   "Sign KEYS from the key ring."
1793   (unwind-protect
1794       (progn
1795         (epg-start-sign-keys context keys local)
1796         (epg-wait-for-completion context)
1797         (if (epg-context-result-for context 'error)
1798             (error "Sign keys failed: %S"
1799                    (epg-context-result-for context 'error))))
1800     (epg-reset context)))
1801
1802 ;;;###autoload
1803 (defun epg-start-generate-key (context parameters)
1804   "Initiate a key generation.
1805 PARAMETERS specifies parameters for the key.
1806
1807 If you use this function, you will need to wait for the completion of
1808 `epg-gpg-program' by using `epg-wait-for-completion' and call
1809 `epg-reset' to clear a temporaly output file.
1810 If you are unsure, use synchronous version of this function
1811 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
1812   (epg-context-set-result context nil)
1813   (if (epg-data-file parameters)
1814       (epg-start context (list "--batch" "--genkey"
1815                                (epg-data-file parameters)))
1816     (epg-start context '("--batch" "--genkey"))
1817     (if (eq (process-status (epg-context-process context)) 'run)
1818         (process-send-string (epg-context-process context)
1819                              (epg-data-string parameters)))
1820     (if (eq (process-status (epg-context-process context)) 'run)
1821         (process-send-eof (epg-context-process context)))))
1822
1823 ;;;###autoload
1824 (defun epg-generate-key-from-file (context parameters)
1825   "Generate a new key pair.
1826 PARAMETERS is a file which tells how to create the key."
1827   (unwind-protect
1828       (progn
1829         (epg-start-generate-key context (epg-make-data-from-file parameters))
1830         (epg-wait-for-completion context)
1831         (if (epg-context-result-for context 'error)
1832             (error "Generate key failed: %S"
1833                    (epg-context-result-for context 'error))))
1834     (epg-reset context)))
1835
1836 ;;;###autoload
1837 (defun epg-generate-key-from-string (context parameters)
1838   "Generate a new key pair.
1839 PARAMETERS is a string which tells how to create the key."
1840   (unwind-protect
1841       (progn
1842         (epg-start-generate-key context (epg-make-data-from-string parameters))
1843         (epg-wait-for-completion context)
1844         (if (epg-context-result-for context 'error)
1845             (error "Generate key failed: %S"
1846                    (epg-context-result-for context 'error))))
1847     (epg-reset context)))
1848
1849 (defun epg-decode-hexstring (string)
1850   (let ((index 0))
1851     (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
1852       (setq string (replace-match "\\x\\&" t nil string)
1853             index (+ index 4)))
1854     (car (read-from-string (concat "\"" string "\"")))))
1855
1856 (defun epg-decode-quotedstring (string)
1857   (let ((index 0))
1858     (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
1859 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
1860                          string index)
1861       (if (match-beginning 2)
1862           (setq string (replace-match "\\2" t nil string)
1863                 index (1+ index))
1864         (if (match-beginning 3)
1865             (setq string (replace-match "\\x\\3" t nil string)
1866                   index (+ index 4))
1867           (setq string (replace-match "\\\\\\\\\\4" t nil string)
1868                 index (+ index 3)))))
1869     (car (read-from-string (concat "\"" string "\"")))))
1870
1871 (defun epg-dn-from-string (string)
1872   "Parse STRING as LADPv3 Distinguished Names (RFC2253).
1873 The return value is an alist mapping from types to values."
1874   (let ((index 0)
1875         (length (length string))
1876         alist type value group)
1877     (while (< index length)
1878       (if (eq index (string-match "[ \t\n\r]*" string index))
1879           (setq index (match-end 0)))
1880       (if (eq index (string-match
1881                      "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
1882                      string index))
1883           (setq type (match-string 1 string)
1884                 index (match-end 0))
1885         (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
1886                                     string index))
1887             (setq type (match-string 1 string)
1888                   index (match-end 0))))
1889       (unless type
1890         (error "Invalid type"))
1891       (if (eq index (string-match
1892                      "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
1893                      string index))
1894           (setq index (match-end 0)
1895                 value (epg-decode-quotedstring (match-string 0 string)))
1896         (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
1897             (setq index (match-end 0)
1898                   value (epg-decode-hexstring (match-string 1 string)))
1899           (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
1900                                       string index))
1901               (setq index (match-end 0)
1902                     value (epg-decode-quotedstring (match-string 0 string))))))
1903       (if group
1904           (if (stringp (car (car alist)))
1905               (setcar alist (list (cons type value) (car alist)))
1906             (setcar alist (cons (cons type value) (car alist))))
1907         (if (consp (car (car alist)))
1908             (setcar alist (nreverse (car alist))))
1909         (setq alist (cons (cons type value) alist)
1910               type nil
1911               value nil))
1912       (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
1913           (setq index (match-end 0)
1914                 group (eq (aref string (match-beginning 1)) ?+))))
1915     (nreverse alist)))
1916
1917 (defun epg-decode-dn (alist)
1918   "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
1919 Type names are resolved using `epg-dn-type-alist'."
1920   (mapconcat
1921    (lambda (rdn)
1922      (if (stringp (car rdn))
1923          (let ((entry (assoc (car rdn) epg-dn-type-alist)))
1924            (if entry
1925                (format "%s=%s" (cdr entry) (cdr rdn))
1926              (format "%s=%s" (car rdn) (cdr rdn))))
1927        (concat "(" (epg-decode-dn rdn) ")")))
1928    alist
1929    ", "))
1930
1931 (provide 'epg)
1932
1933 ;;; epg.el ends here