OSDN Git Service

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