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