* term.el: Fix typos in docstrings.
[emacs.git] / lisp / epg.el
blob9fde76d5f8570311e28524e9cf2604ddbb6df6c8
1 ;;; epg.el --- the EasyPG Library
2 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
3 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Keywords: PGP, GnuPG
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Code:
25 (require 'epg-config)
27 (defvar epg-user-id nil
28 "GnuPG ID of your default identity.")
30 (defvar epg-user-id-alist nil
31 "An alist mapping from key ID to user ID.")
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)
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")
52 (11 . "CAMELLIA128")
53 (12 . "CAMELLIA256")
54 (110 . "DUMMY")))
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")))
65 ;; from gnupg/include/cipher.h
66 (defconst epg-digest-algorithm-alist
67 '((1 . "MD5")
68 (2 . "SHA1")
69 (3 . "RMD160")
70 (8 . "SHA256")
71 (9 . "SHA384")
72 (10 . "SHA512")
73 (11 . "SHA224")))
75 ;; from gnupg/include/cipher.h
76 (defconst epg-compress-algorithm-alist
77 '((0 . "NONE")
78 (1 . "ZIP")
79 (2 . "ZLIB")
80 (3 . "BZIP2")))
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")))
95 (defconst epg-delete-problem-reason-alist
96 '((1 . "No such key")
97 (2 . "Must delete secret key first")
98 (3 . "Ambigious specification")))
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")))
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")))
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")))
121 (defconst epg-unexpected-reason-alist nil)
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)))
136 (defvar epg-key-capablity-alist
137 '((?e . encrypt)
138 (?s . sign)
139 (?c . certify)
140 (?a . authentication)))
142 (defvar epg-new-signature-type-alist
143 '((?D . detached)
144 (?C . clear)
145 (?S . normal)))
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")))
160 (defvar epg-prompt-alist nil)
162 (put 'epg-error 'error-conditions '(epg-error error))
164 (defun epg-make-data-from-file (file)
165 "Make a data object from FILE."
166 (cons 'epg-data (vector file nil)))
168 (defun epg-make-data-from-string (string)
169 "Make a data object from STRING."
170 (cons 'epg-data (vector nil string)))
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))
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))
184 ;;;###autoload
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
192 (list #'epg-passphrase-callback-function)
194 nil nil nil nil nil nil)))
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))
202 (defun epg-context-armor (context)
203 "Return t if the output should be ASCII armored in CONTEXT."
204 (unless (eq (car-safe context) 'epg-context)
205 (signal 'wrong-type-argument (list 'epg-context-p context)))
206 (aref (cdr context) 1))
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))
214 (defun epg-context-include-certs (context)
215 "Return how many certificates should be included in an S/MIME signed message."
216 (unless (eq (car-safe context) 'epg-context)
217 (signal 'wrong-type-argument (list 'epg-context-p context)))
218 (aref (cdr context) 3))
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))
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))
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))
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))
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))
250 (defun epg-context-signers (context)
251 "Return the list of key-id for signing."
252 (unless (eq (car-safe context) 'epg-context)
253 (signal 'wrong-type-argument (list 'epg-context-p context)))
254 (aref (cdr context) 9))
256 (defun epg-context-sig-notations (context)
257 "Return the list of notations for signing."
258 (unless (eq (car-safe context) 'epg-context)
259 (signal 'wrong-type-argument (list 'epg-context-p context)))
260 (aref (cdr context) 10))
262 (defun epg-context-process (context)
263 "Return the process object of `epg-gpg-program'.
264 This 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))
269 (defun epg-context-output-file (context)
270 "Return the output file of `epg-gpg-program'.
271 This 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))
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))
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))
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))
294 (defun epg-context-set-armor (context armor)
295 "Specify if the output should be ASCII armored in CONTEXT."
296 (unless (eq (car-safe context) 'epg-context)
297 (signal 'wrong-type-argument (list 'epg-context-p context)))
298 (aset (cdr context) 1 armor))
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))
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))
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))
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))
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))
330 (defun epg-context-set-passphrase-callback (context
331 passphrase-callback)
332 "Set the function used to query passphrase.
334 PASSPHRASE-CALLBACK is either a function, or a cons-cell whose
335 car is a function and cdr is a callback data.
337 The function gets three arguments: the context, the key-id in
338 question, and the callback data (if any)."
339 (unless (eq (car-safe context) 'epg-context)
340 (signal 'wrong-type-argument (list 'epg-context-p context)))
341 (aset (cdr context) 7 (if (consp passphrase-callback)
342 passphrase-callback
343 (list passphrase-callback))))
345 (defun epg-context-set-progress-callback (context
346 progress-callback)
347 "Set the function which handles progress update.
349 PROGRESS-CALLBACK is either a function, or a cons-cell whose
350 car is a function and cdr is a callback data.
352 The function gets five arguments: the context, the operation
353 description, the character to display a progress unit, the
354 current amount done, the total amount to be done, and the
355 callback data (if any)."
356 (unless (eq (car-safe context) 'epg-context)
357 (signal 'wrong-type-argument (list 'epg-context-p context)))
358 (aset (cdr context) 8 (if (consp progress-callback)
359 progress-callback
360 (list progress-callback))))
362 (defun epg-context-set-signers (context signers)
363 "Set the list of key-id for signing."
364 (unless (eq (car-safe context) 'epg-context)
365 (signal 'wrong-type-argument (list 'epg-context-p context)))
366 (aset (cdr context) 9 signers))
368 (defun epg-context-set-sig-notations (context notations)
369 "Set the list of notations for signing."
370 (unless (eq (car-safe context) 'epg-context)
371 (signal 'wrong-type-argument (list 'epg-context-p context)))
372 (aset (cdr context) 10 notations))
374 (defun epg-context-set-process (context process)
375 "Set the process object of `epg-gpg-program'.
376 This 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))
381 (defun epg-context-set-output-file (context output-file)
382 "Set the output file of `epg-gpg-program'.
383 This 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))
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))
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))
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)))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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)))
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))
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))
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))
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))
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))
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))
579 (defun epg-make-key (owner-trust)
580 "Return a key object."
581 (cons 'epg-key (vector owner-trust nil nil)))
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))
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))
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))
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))
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))
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)))
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))
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))
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))
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))
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))
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))
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))
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))
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))
674 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
675 "Set the fingerprint of SUB-KEY.
676 This 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))
681 (defun epg-make-user-id (validity string)
682 "Return a user ID object."
683 (cons 'epg-user-id (vector validity string nil)))
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))
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))
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))
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))
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)))
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))
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))
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))
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))
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))
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))
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))
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))
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)))
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))
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))
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))
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))
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))
805 (defun epg-make-import-status (fingerprint &optional reason new user-id
806 signature sub-key secret)
807 "Return an import status object."
808 (cons 'epg-import-status (vector fingerprint reason new user-id signature
809 sub-key secret)))
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))
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))
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))
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))
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))
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))
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))
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)
859 "Return an import result object."
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)))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))))
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)))))
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)))
987 (if (epg-signature-validity signature)
988 (format " (trust %s)" (epg-signature-validity signature))
990 (if (epg-signature-creation-time signature)
991 (format-time-string " created at %Y-%m-%dT%T%z"
992 (epg-signature-creation-time signature))
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 ""))))
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"))
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 ")
1012 "Signature "))
1013 (cdr (assq (epg-new-signature-pubkey-algorithm new-signature)
1014 epg-pubkey-algorithm-alist))
1016 (cdr (assq (epg-new-signature-digest-algorithm new-signature)
1017 epg-digest-algorithm-alist))
1019 (format "%02X " (epg-new-signature-class new-signature))
1020 (epg-new-signature-fingerprint new-signature)))
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)))))
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)))
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 (with-current-buffer (process-buffer process)
1147 (goto-char (point-max))
1148 (insert input)
1149 (unless epg-process-filter-running
1150 (unwind-protect
1151 (progn
1152 (setq epg-process-filter-running t)
1153 (goto-char epg-read-point)
1154 (beginning-of-line)
1155 (while (looking-at ".*\n") ;the input line finished
1156 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
1157 (let* ((status (match-string 1))
1158 (string (match-string 2))
1159 (symbol (intern-soft (concat "epg--status-"
1160 status))))
1161 (if (member status epg-pending-status-list)
1162 (setq epg-pending-status-list nil))
1163 (if (and symbol
1164 (fboundp symbol))
1165 (funcall symbol epg-context string))
1166 (setq epg-last-status (cons status string))))
1167 (forward-line)
1168 (setq epg-read-point (point))))
1169 (setq epg-process-filter-running nil))))))
1171 (defun epg-read-output (context)
1172 "Read the output file CONTEXT and return the content as a string."
1173 (with-temp-buffer
1174 (if (fboundp 'set-buffer-multibyte)
1175 (set-buffer-multibyte nil))
1176 (if (file-exists-p (epg-context-output-file context))
1177 (let ((coding-system-for-read 'binary))
1178 (insert-file-contents (epg-context-output-file context))
1179 (buffer-string)))))
1181 (defun epg-wait-for-status (context status-list)
1182 "Wait until one of elements in STATUS-LIST arrives."
1183 (with-current-buffer (process-buffer (epg-context-process context))
1184 (setq epg-pending-status-list status-list)
1185 (while (and (eq (process-status (epg-context-process context)) 'run)
1186 epg-pending-status-list)
1187 (accept-process-output (epg-context-process context) 1))
1188 (if epg-pending-status-list
1189 (epg-context-set-result-for
1190 context 'error
1191 (cons (list 'exit)
1192 (epg-context-result-for context 'error))))))
1194 (defun epg-wait-for-completion (context)
1195 "Wait until the `epg-gpg-program' process completes."
1196 (while (eq (process-status (epg-context-process context)) 'run)
1197 (accept-process-output (epg-context-process context) 1))
1198 ;; This line is needed to run the process-filter right now.
1199 (sleep-for 0.1))
1201 (defun epg-reset (context)
1202 "Reset the CONTEXT."
1203 (if (and (epg-context-process context)
1204 (buffer-live-p (process-buffer (epg-context-process context))))
1205 (kill-buffer (process-buffer (epg-context-process context))))
1206 (epg-context-set-process context nil))
1208 (defun epg-delete-output-file (context)
1209 "Delete the output file of CONTEXT."
1210 (if (and (epg-context-output-file context)
1211 (file-exists-p (epg-context-output-file context)))
1212 (delete-file (epg-context-output-file context))))
1214 (eval-and-compile
1215 (if (fboundp 'decode-coding-string)
1216 (defalias 'epg--decode-coding-string 'decode-coding-string)
1217 (defalias 'epg--decode-coding-string 'identity)))
1219 (defun epg--status-USERID_HINT (context string)
1220 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1221 (let* ((key-id (match-string 1 string))
1222 (user-id (match-string 2 string))
1223 (entry (assoc key-id epg-user-id-alist)))
1224 (condition-case nil
1225 (setq user-id (epg--decode-coding-string
1226 (epg--decode-percent-escape user-id)
1227 'utf-8))
1228 (error))
1229 (if entry
1230 (setcdr entry user-id)
1231 (setq epg-user-id-alist (cons (cons key-id user-id)
1232 epg-user-id-alist))))))
1234 (defun epg--status-NEED_PASSPHRASE (context string)
1235 (if (string-match "\\`\\([^ ]+\\)" string)
1236 (setq epg-key-id (match-string 1 string))))
1238 (defun epg--status-NEED_PASSPHRASE_SYM (context string)
1239 (setq epg-key-id 'SYM))
1241 (defun epg--status-NEED_PASSPHRASE_PIN (context string)
1242 (setq epg-key-id 'PIN))
1244 (eval-and-compile
1245 (if (fboundp 'clear-string)
1246 (defalias 'epg--clear-string 'clear-string)
1247 (defun epg--clear-string (string)
1248 (fillarray string 0))))
1250 (eval-and-compile
1251 (if (fboundp 'encode-coding-string)
1252 (defalias 'epg--encode-coding-string 'encode-coding-string)
1253 (defalias 'epg--encode-coding-string 'identity)))
1255 (defun epg--status-GET_HIDDEN (context string)
1256 (when (and epg-key-id
1257 (string-match "\\`passphrase\\." string))
1258 (unless (epg-context-passphrase-callback context)
1259 (error "passphrase-callback not set"))
1260 (let (inhibit-quit
1261 passphrase
1262 passphrase-with-new-line
1263 encoded-passphrase-with-new-line)
1264 (unwind-protect
1265 (condition-case nil
1266 (progn
1267 (setq passphrase
1268 (funcall
1269 (car (epg-context-passphrase-callback context))
1270 context
1271 epg-key-id
1272 (cdr (epg-context-passphrase-callback context))))
1273 (when passphrase
1274 (setq passphrase-with-new-line (concat passphrase "\n"))
1275 (epg--clear-string passphrase)
1276 (setq passphrase nil)
1277 (if epg-passphrase-coding-system
1278 (progn
1279 (setq encoded-passphrase-with-new-line
1280 (epg--encode-coding-string
1281 passphrase-with-new-line
1282 (coding-system-change-eol-conversion
1283 epg-passphrase-coding-system 'unix)))
1284 (epg--clear-string passphrase-with-new-line)
1285 (setq passphrase-with-new-line nil))
1286 (setq encoded-passphrase-with-new-line
1287 passphrase-with-new-line
1288 passphrase-with-new-line nil))
1289 (process-send-string (epg-context-process context)
1290 encoded-passphrase-with-new-line)))
1291 (quit
1292 (epg-context-set-result-for
1293 context 'error
1294 (cons '(quit)
1295 (epg-context-result-for context 'error)))
1296 (delete-process (epg-context-process context))))
1297 (if passphrase
1298 (epg--clear-string passphrase))
1299 (if passphrase-with-new-line
1300 (epg--clear-string passphrase-with-new-line))
1301 (if encoded-passphrase-with-new-line
1302 (epg--clear-string encoded-passphrase-with-new-line))))))
1304 (defun epg--prompt-GET_BOOL (context string)
1305 (let ((entry (assoc string epg-prompt-alist)))
1306 (y-or-n-p (if entry (cdr entry) (concat string "? ")))))
1308 (defun epg--prompt-GET_BOOL-untrusted_key.override (context string)
1309 (y-or-n-p (if (and (equal (car epg-last-status) "USERID_HINT")
1310 (string-match "\\`\\([^ ]+\\) \\(.*\\)"
1311 (cdr epg-last-status)))
1312 (let* ((key-id (match-string 1 (cdr epg-last-status)))
1313 (user-id (match-string 2 (cdr epg-last-status)))
1314 (entry (assoc key-id epg-user-id-alist)))
1315 (if entry
1316 (setq user-id (cdr entry)))
1317 (format "Untrusted key %s %s. Use anyway? " key-id user-id))
1318 "Use untrusted key anyway? ")))
1320 (defun epg--status-GET_BOOL (context string)
1321 (let (inhibit-quit)
1322 (condition-case nil
1323 (if (funcall (or (intern-soft (concat "epg--prompt-GET_BOOL-" string))
1324 #'epg--prompt-GET_BOOL)
1325 context string)
1326 (process-send-string (epg-context-process context) "y\n")
1327 (process-send-string (epg-context-process context) "n\n"))
1328 (quit
1329 (epg-context-set-result-for
1330 context 'error
1331 (cons '(quit)
1332 (epg-context-result-for context 'error)))
1333 (delete-process (epg-context-process context))))))
1335 (defun epg--status-GET_LINE (context string)
1336 (let ((entry (assoc string epg-prompt-alist))
1337 inhibit-quit)
1338 (condition-case nil
1339 (process-send-string (epg-context-process context)
1340 (concat (read-string
1341 (if entry
1342 (cdr entry)
1343 (concat string ": ")))
1344 "\n"))
1345 (quit
1346 (epg-context-set-result-for
1347 context 'error
1348 (cons '(quit)
1349 (epg-context-result-for context 'error)))
1350 (delete-process (epg-context-process context))))))
1352 (defun epg--status-*SIG (context status string)
1353 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1354 (let* ((key-id (match-string 1 string))
1355 (user-id (match-string 2 string))
1356 (entry (assoc key-id epg-user-id-alist)))
1357 (epg-context-set-result-for
1358 context
1359 'verify
1360 (cons (epg-make-signature status key-id)
1361 (epg-context-result-for context 'verify)))
1362 (condition-case nil
1363 (if (eq (epg-context-protocol context) 'CMS)
1364 (setq user-id (epg-dn-from-string user-id))
1365 (setq user-id (epg--decode-coding-string
1366 (epg--decode-percent-escape user-id)
1367 'utf-8)))
1368 (error))
1369 (if entry
1370 (setcdr entry user-id)
1371 (setq epg-user-id-alist
1372 (cons (cons key-id user-id) epg-user-id-alist))))
1373 (epg-context-set-result-for
1374 context
1375 'verify
1376 (cons (epg-make-signature status)
1377 (epg-context-result-for context 'verify)))))
1379 (defun epg--status-GOODSIG (context string)
1380 (epg--status-*SIG context 'good string))
1382 (defun epg--status-EXPSIG (context string)
1383 (epg--status-*SIG context 'expired string))
1385 (defun epg--status-EXPKEYSIG (context string)
1386 (epg--status-*SIG context 'expired-key string))
1388 (defun epg--status-REVKEYSIG (context string)
1389 (epg--status-*SIG context 'revoked-key string))
1391 (defun epg--status-BADSIG (context string)
1392 (epg--status-*SIG context 'bad string))
1394 (defun epg--status-NO_PUBKEY (context string)
1395 (let ((signature (car (epg-context-result-for context 'verify))))
1396 (if (and signature
1397 (eq (epg-signature-status signature) 'error)
1398 (equal (epg-signature-key-id signature) string))
1399 (epg-signature-set-status signature 'no-pubkey))))
1401 (defun epg--time-from-seconds (seconds)
1402 (let ((number-seconds (string-to-number (concat seconds ".0"))))
1403 (cons (floor (/ number-seconds 65536))
1404 (floor (mod number-seconds 65536)))))
1406 (defun epg--status-ERRSIG (context string)
1407 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1408 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
1409 string)
1410 (let ((signature (epg-make-signature 'error)))
1411 (epg-context-set-result-for
1412 context
1413 'verify
1414 (cons signature
1415 (epg-context-result-for context 'verify)))
1416 (epg-signature-set-key-id
1417 signature
1418 (match-string 1 string))
1419 (epg-signature-set-pubkey-algorithm
1420 signature
1421 (string-to-number (match-string 2 string)))
1422 (epg-signature-set-digest-algorithm
1423 signature
1424 (string-to-number (match-string 3 string)))
1425 (epg-signature-set-class
1426 signature
1427 (string-to-number (match-string 4 string) 16))
1428 (epg-signature-set-creation-time
1429 signature
1430 (epg--time-from-seconds (match-string 5 string))))))
1432 (defun epg--status-VALIDSIG (context string)
1433 (let ((signature (car (epg-context-result-for context 'verify))))
1434 (when (and signature
1435 (eq (epg-signature-status signature) 'good)
1436 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
1437 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
1438 \\(.*\\)"
1439 string))
1440 (epg-signature-set-fingerprint
1441 signature
1442 (match-string 1 string))
1443 (epg-signature-set-creation-time
1444 signature
1445 (epg--time-from-seconds (match-string 2 string)))
1446 (unless (equal (match-string 3 string) "0")
1447 (epg-signature-set-expiration-time
1448 signature
1449 (epg--time-from-seconds (match-string 3 string))))
1450 (epg-signature-set-version
1451 signature
1452 (string-to-number (match-string 4 string)))
1453 (epg-signature-set-pubkey-algorithm
1454 signature
1455 (string-to-number (match-string 5 string)))
1456 (epg-signature-set-digest-algorithm
1457 signature
1458 (string-to-number (match-string 6 string)))
1459 (epg-signature-set-class
1460 signature
1461 (string-to-number (match-string 7 string) 16)))))
1463 (defun epg--status-TRUST_UNDEFINED (context string)
1464 (let ((signature (car (epg-context-result-for context 'verify))))
1465 (if (and signature
1466 (eq (epg-signature-status signature) 'good))
1467 (epg-signature-set-validity signature 'undefined))))
1469 (defun epg--status-TRUST_NEVER (context string)
1470 (let ((signature (car (epg-context-result-for context 'verify))))
1471 (if (and signature
1472 (eq (epg-signature-status signature) 'good))
1473 (epg-signature-set-validity signature 'never))))
1475 (defun epg--status-TRUST_MARGINAL (context string)
1476 (let ((signature (car (epg-context-result-for context 'verify))))
1477 (if (and signature
1478 (eq (epg-signature-status signature) 'marginal))
1479 (epg-signature-set-validity signature 'marginal))))
1481 (defun epg--status-TRUST_FULLY (context string)
1482 (let ((signature (car (epg-context-result-for context 'verify))))
1483 (if (and signature
1484 (eq (epg-signature-status signature) 'good))
1485 (epg-signature-set-validity signature 'full))))
1487 (defun epg--status-TRUST_ULTIMATE (context string)
1488 (let ((signature (car (epg-context-result-for context 'verify))))
1489 (if (and signature
1490 (eq (epg-signature-status signature) 'good))
1491 (epg-signature-set-validity signature 'ultimate))))
1493 (defun epg--status-NOTATION_NAME (context string)
1494 (let ((signature (car (epg-context-result-for context 'verify))))
1495 (if signature
1496 (epg-signature-set-notations
1497 signature
1498 (cons (epg-make-sig-notation string nil t nil)
1499 (epg-sig-notations signature))))))
1501 (defun epg--status-NOTATION_DATA (context string)
1502 (let ((signature (car (epg-context-result-for context 'verify)))
1503 notation)
1504 (if (and signature
1505 (setq notation (car (epg-sig-notations signature))))
1506 (epg-sig-notation-set-value notation string))))
1508 (defun epg--status-POLICY_URL (context string)
1509 (let ((signature (car (epg-context-result-for context 'verify))))
1510 (if signature
1511 (epg-signature-set-notations
1512 signature
1513 (cons (epg-make-sig-notation nil string t nil)
1514 (epg-sig-notations signature))))))
1516 (defun epg--status-PROGRESS (context string)
1517 (if (and (epg-context-progress-callback context)
1518 (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1519 string))
1520 (funcall (car (epg-context-progress-callback context))
1521 context
1522 (match-string 1 string)
1523 (match-string 2 string)
1524 (string-to-number (match-string 3 string))
1525 (string-to-number (match-string 4 string))
1526 (cdr (epg-context-progress-callback context)))))
1528 (defun epg--status-ENC_TO (context string)
1529 (if (string-match "\\`\\([0-9A-Za-z]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
1530 (epg-context-set-result-for
1531 context 'encrypted-to
1532 (cons (list (match-string 1 string)
1533 (string-to-number (match-string 2 string))
1534 (string-to-number (match-string 3 string)))
1535 (epg-context-result-for context 'encrypted-to)))))
1537 (defun epg--status-DECRYPTION_FAILED (context string)
1538 (epg-context-set-result-for context 'decryption-failed t))
1540 (defun epg--status-DECRYPTION_OKAY (context string)
1541 (epg-context-set-result-for context 'decryption-okay t))
1543 (defun epg--status-NODATA (context string)
1544 (epg-context-set-result-for
1545 context 'error
1546 (cons (cons 'no-data (string-to-number string))
1547 (epg-context-result-for context 'error))))
1549 (defun epg--status-UNEXPECTED (context string)
1550 (epg-context-set-result-for
1551 context 'error
1552 (cons (cons 'unexpected (string-to-number string))
1553 (epg-context-result-for context 'error))))
1555 (defun epg--status-KEYEXPIRED (context string)
1556 (epg-context-set-result-for
1557 context 'error
1558 (cons (list 'key-expired (cons 'expiration-time
1559 (epg--time-from-seconds string)))
1560 (epg-context-result-for context 'error))))
1562 (defun epg--status-KEYREVOKED (context string)
1563 (epg-context-set-result-for
1564 context 'error
1565 (cons '(key-revoked)
1566 (epg-context-result-for context 'error))))
1568 (defun epg--status-BADARMOR (context string)
1569 (epg-context-set-result-for
1570 context 'error
1571 (cons '(bad-armor)
1572 (epg-context-result-for context 'error))))
1574 (defun epg--status-INV_RECP (context string)
1575 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1576 (epg-context-set-result-for
1577 context 'error
1578 (cons (list 'invalid-recipient
1579 (cons 'reason
1580 (string-to-number (match-string 1 string)))
1581 (cons 'requested-recipient
1582 (match-string 2 string)))
1583 (epg-context-result-for context 'error)))))
1585 (defun epg--status-NO_RECP (context string)
1586 (epg-context-set-result-for
1587 context 'error
1588 (cons '(no-recipients)
1589 (epg-context-result-for context 'error))))
1591 (defun epg--status-DELETE_PROBLEM (context string)
1592 (if (string-match "\\`\\([0-9]+\\)" string)
1593 (epg-context-set-result-for
1594 context 'error
1595 (cons (cons 'delete-problem
1596 (string-to-number (match-string 1 string)))
1597 (epg-context-result-for context 'error)))))
1599 (defun epg--status-SIG_CREATED (context string)
1600 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1601 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1602 (epg-context-set-result-for
1603 context 'sign
1604 (cons (epg-make-new-signature
1605 (cdr (assq (aref (match-string 1 string) 0)
1606 epg-new-signature-type-alist))
1607 (string-to-number (match-string 2 string))
1608 (string-to-number (match-string 3 string))
1609 (string-to-number (match-string 4 string) 16)
1610 (epg--time-from-seconds (match-string 5 string))
1611 (substring string (match-end 0)))
1612 (epg-context-result-for context 'sign)))))
1614 (defun epg--status-KEY_CREATED (context string)
1615 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1616 (epg-context-set-result-for
1617 context 'generate-key
1618 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1619 (cons 'fingerprint (match-string 2 string)))
1620 (epg-context-result-for context 'generate-key)))))
1622 (defun epg--status-KEY_NOT_CREATED (context string)
1623 (epg-context-set-result-for
1624 context 'error
1625 (cons '(key-not-created)
1626 (epg-context-result-for context 'error))))
1628 (defun epg--status-IMPORTED (context string)
1629 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1630 (let* ((key-id (match-string 1 string))
1631 (user-id (match-string 2 string))
1632 (entry (assoc key-id epg-user-id-alist)))
1633 (condition-case nil
1634 (setq user-id (epg--decode-coding-string
1635 (epg--decode-percent-escape user-id)
1636 'utf-8))
1637 (error))
1638 (if entry
1639 (setcdr entry user-id)
1640 (setq epg-user-id-alist (cons (cons key-id user-id)
1641 epg-user-id-alist))))))
1643 (defun epg--status-IMPORT_OK (context string)
1644 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1645 (let ((reason (string-to-number (match-string 1 string))))
1646 (epg-context-set-result-for
1647 context 'import-status
1648 (cons (epg-make-import-status (if (match-beginning 2)
1649 (match-string 3 string))
1651 (/= (logand reason 1) 0)
1652 (/= (logand reason 2) 0)
1653 (/= (logand reason 4) 0)
1654 (/= (logand reason 8) 0)
1655 (/= (logand reason 16) 0))
1656 (epg-context-result-for context 'import-status))))))
1658 (defun epg--status-IMPORT_PROBLEM (context string)
1659 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1660 (epg-context-set-result-for
1661 context 'import-status
1662 (cons (epg-make-import-status
1663 (if (match-beginning 2)
1664 (match-string 3 string))
1665 (string-to-number (match-string 1 string)))
1666 (epg-context-result-for context 'import-status)))))
1668 (defun epg--status-IMPORT_RES (context string)
1669 (when (string-match "\\`\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1670 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1671 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
1672 (epg-context-set-result-for
1673 context 'import
1674 (epg-make-import-result (string-to-number (match-string 1 string))
1675 (string-to-number (match-string 2 string))
1676 (string-to-number (match-string 3 string))
1677 (string-to-number (match-string 4 string))
1678 (string-to-number (match-string 5 string))
1679 (string-to-number (match-string 6 string))
1680 (string-to-number (match-string 7 string))
1681 (string-to-number (match-string 8 string))
1682 (string-to-number (match-string 9 string))
1683 (string-to-number (match-string 10 string))
1684 (string-to-number (match-string 11 string))
1685 (string-to-number (match-string 12 string))
1686 (string-to-number (match-string 13 string))
1687 (epg-context-result-for context 'import-status)))
1688 (epg-context-set-result-for context 'import-status nil)))
1690 (defun epg-passphrase-callback-function (context key-id handback)
1691 (if (eq key-id 'SYM)
1692 (read-passwd "Passphrase for symmetric encryption: "
1693 (eq (epg-context-operation context) 'encrypt))
1694 (read-passwd
1695 (if (eq key-id 'PIN)
1696 "Passphrase for PIN: "
1697 (let ((entry (assoc key-id epg-user-id-alist)))
1698 (if entry
1699 (format "Passphrase for %s %s: " key-id (cdr entry))
1700 (format "Passphrase for %s: " key-id)))))))
1702 (make-obsolete 'epg-passphrase-callback-function
1703 'epa-passphrase-callback-function "23.1")
1705 (defun epg--list-keys-1 (context name mode)
1706 (let ((args (append (if epg-gpg-home-directory
1707 (list "--homedir" epg-gpg-home-directory))
1708 '("--with-colons" "--no-greeting" "--batch"
1709 "--with-fingerprint" "--with-fingerprint")
1710 (unless (eq (epg-context-protocol context) 'CMS)
1711 '("--fixed-list-mode"))))
1712 (list-keys-option (if (memq mode '(t secret))
1713 "--list-secret-keys"
1714 (if (memq mode '(nil public))
1715 "--list-keys"
1716 "--list-sigs")))
1717 (coding-system-for-read 'binary)
1718 keys string field index)
1719 (if name
1720 (progn
1721 (unless (listp name)
1722 (setq name (list name)))
1723 (while name
1724 (setq args (append args (list list-keys-option (car name)))
1725 name (cdr name))))
1726 (setq args (append args (list list-keys-option))))
1727 (with-temp-buffer
1728 (apply #'call-process
1729 (if (eq (epg-context-protocol context) 'CMS)
1730 epg-gpgsm-program
1731 epg-gpg-program)
1732 nil (list t nil) nil args)
1733 (goto-char (point-min))
1734 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1735 (setq keys (cons (make-vector 15 nil) keys)
1736 string (match-string 0)
1737 index 0
1738 field 0)
1739 (while (eq index
1740 (string-match "\\([^:]+\\)?:" string index))
1741 (setq index (match-end 0))
1742 (aset (car keys) field (match-string 1 string))
1743 (setq field (1+ field))))
1744 (nreverse keys))))
1746 (defun epg--make-sub-key-1 (line)
1747 (epg-make-sub-key
1748 (if (aref line 1)
1749 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1750 (delq nil
1751 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1752 (aref line 11)))
1753 (member (aref line 0) '("sec" "ssb"))
1754 (string-to-number (aref line 3))
1755 (string-to-number (aref line 2))
1756 (aref line 4)
1757 (epg--time-from-seconds (aref line 5))
1758 (if (aref line 6)
1759 (epg--time-from-seconds (aref line 6)))))
1761 (defun epg-list-keys (context &optional name mode)
1762 "Return a list of epg-key objects matched with NAME.
1763 If MODE is nil or 'public, only public keyring should be searched.
1764 If MODE is t or 'secret, only secret keyring should be searched.
1765 Otherwise, only public keyring should be searched and the key
1766 signatures should be included.
1767 NAME is either a string or a list of strings."
1768 (let ((lines (epg--list-keys-1 context name mode))
1769 keys cert pointer pointer-1 index string)
1770 (while lines
1771 (cond
1772 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1773 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1774 keys (cons (epg-make-key
1775 (if (aref (car lines) 8)
1776 (cdr (assq (string-to-char (aref (car lines) 8))
1777 epg-key-validity-alist))))
1778 keys))
1779 (epg-key-set-sub-key-list
1780 (car keys)
1781 (cons (epg--make-sub-key-1 (car lines))
1782 (epg-key-sub-key-list (car keys)))))
1783 ((member (aref (car lines) 0) '("sub" "ssb"))
1784 (epg-key-set-sub-key-list
1785 (car keys)
1786 (cons (epg--make-sub-key-1 (car lines))
1787 (epg-key-sub-key-list (car keys)))))
1788 ((equal (aref (car lines) 0) "uid")
1789 ;; Decode the UID name as a backslash escaped UTF-8 string,
1790 ;; generated by GnuPG/GpgSM.
1791 (setq string (copy-sequence (aref (car lines) 9))
1792 index 0)
1793 (while (string-match "\"" string index)
1794 (setq string (replace-match "\\\"" t t string)
1795 index (1+ (match-end 0))))
1796 (condition-case nil
1797 (setq string (epg--decode-coding-string
1798 (car (read-from-string (concat "\"" string "\"")))
1799 'utf-8))
1800 (error
1801 (setq string (aref (car lines) 9))))
1802 (epg-key-set-user-id-list
1803 (car keys)
1804 (cons (epg-make-user-id
1805 (if (aref (car lines) 1)
1806 (cdr (assq (string-to-char (aref (car lines) 1))
1807 epg-key-validity-alist)))
1808 (if cert
1809 (condition-case nil
1810 (epg-dn-from-string string)
1811 (error string))
1812 string))
1813 (epg-key-user-id-list (car keys)))))
1814 ((equal (aref (car lines) 0) "fpr")
1815 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1816 (aref (car lines) 9)))
1817 ((equal (aref (car lines) 0) "sig")
1818 (epg-user-id-set-signature-list
1819 (car (epg-key-user-id-list (car keys)))
1820 (cons
1821 (epg-make-key-signature
1822 (if (aref (car lines) 1)
1823 (cdr (assq (string-to-char (aref (car lines) 1))
1824 epg-key-validity-alist)))
1825 (string-to-number (aref (car lines) 3))
1826 (aref (car lines) 4)
1827 (epg--time-from-seconds (aref (car lines) 5))
1828 (epg--time-from-seconds (aref (car lines) 6))
1829 (aref (car lines) 9)
1830 (string-to-number (aref (car lines) 10) 16)
1831 (eq (aref (aref (car lines) 10) 2) ?x))
1832 (epg-user-id-signature-list
1833 (car (epg-key-user-id-list (car keys))))))))
1834 (setq lines (cdr lines)))
1835 (setq keys (nreverse keys)
1836 pointer keys)
1837 (while pointer
1838 (epg-key-set-sub-key-list
1839 (car pointer)
1840 (nreverse (epg-key-sub-key-list (car pointer))))
1841 (setq pointer-1 (epg-key-set-user-id-list
1842 (car pointer)
1843 (nreverse (epg-key-user-id-list (car pointer)))))
1844 (while pointer-1
1845 (epg-user-id-set-signature-list
1846 (car pointer-1)
1847 (nreverse (epg-user-id-signature-list (car pointer-1))))
1848 (setq pointer-1 (cdr pointer-1)))
1849 (setq pointer (cdr pointer)))
1850 keys))
1852 (eval-and-compile
1853 (if (fboundp 'make-temp-file)
1854 (defalias 'epg--make-temp-file 'make-temp-file)
1855 (defvar temporary-file-directory)
1856 ;; stolen from poe.el.
1857 (defun epg--make-temp-file (prefix)
1858 "Create a temporary file.
1859 The returned file name (created by appending some random characters at the end
1860 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1861 is guaranteed to point to a newly created empty file.
1862 You can then use `write-region' to write new data into the file."
1863 (let (tempdir tempfile)
1864 (setq prefix (expand-file-name prefix
1865 (if (featurep 'xemacs)
1866 (temp-directory)
1867 temporary-file-directory)))
1868 (unwind-protect
1869 (let (file)
1870 ;; First, create a temporary directory.
1871 (while (condition-case ()
1872 (progn
1873 (setq tempdir (make-temp-name
1874 (concat
1875 (file-name-directory prefix)
1876 "DIR")))
1877 ;; return nil or signal an error.
1878 (make-directory tempdir))
1879 ;; let's try again.
1880 (file-already-exists t)))
1881 (set-file-modes tempdir 448)
1882 ;; Second, create a temporary file in the tempdir.
1883 ;; There *is* a race condition between `make-temp-name'
1884 ;; and `write-region', but we don't care it since we are
1885 ;; in a private directory now.
1886 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1887 (write-region "" nil tempfile nil 'silent)
1888 (set-file-modes tempfile 384)
1889 ;; Finally, make a hard-link from the tempfile.
1890 (while (condition-case ()
1891 (progn
1892 (setq file (make-temp-name prefix))
1893 ;; return nil or signal an error.
1894 (add-name-to-file tempfile file))
1895 ;; let's try again.
1896 (file-already-exists t)))
1897 file)
1898 ;; Cleanup the tempfile.
1899 (and tempfile
1900 (file-exists-p tempfile)
1901 (delete-file tempfile))
1902 ;; Cleanup the tempdir.
1903 (and tempdir
1904 (file-directory-p tempdir)
1905 (delete-directory tempdir)))))))
1907 (defun epg--args-from-sig-notations (notations)
1908 (apply #'nconc
1909 (mapcar
1910 (lambda (notation)
1911 (if (and (epg-sig-notation-name notation)
1912 (not (epg-sig-notation-human-readable notation)))
1913 (error "Unreadable"))
1914 (if (epg-sig-notation-name notation)
1915 (list "--sig-notation"
1916 (if (epg-sig-notation-critical notation)
1917 (concat "!" (epg-sig-notation-name notation)
1918 "=" (epg-sig-notation-value notation))
1919 (concat (epg-sig-notation-name notation)
1920 "=" (epg-sig-notation-value notation))))
1921 (list "--sig-policy-url"
1922 (if (epg-sig-notation-critical notation)
1923 (concat "!" (epg-sig-notation-value notation))
1924 (epg-sig-notation-value notation)))))
1925 notations)))
1927 (defun epg-cancel (context)
1928 (if (buffer-live-p (process-buffer (epg-context-process context)))
1929 (with-current-buffer (process-buffer (epg-context-process context))
1930 (epg-context-set-result-for
1931 epg-context 'error
1932 (cons '(quit)
1933 (epg-context-result-for epg-context 'error)))))
1934 (if (eq (process-status (epg-context-process context)) 'run)
1935 (delete-process (epg-context-process context))))
1937 (defun epg-start-decrypt (context cipher)
1938 "Initiate a decrypt operation on CIPHER.
1939 CIPHER must be a file data object.
1941 If you use this function, you will need to wait for the completion of
1942 `epg-gpg-program' by using `epg-wait-for-completion' and call
1943 `epg-reset' to clear a temporaly output file.
1944 If you are unsure, use synchronous version of this function
1945 `epg-decrypt-file' or `epg-decrypt-string' instead."
1946 (unless (epg-data-file cipher)
1947 (error "Not a file"))
1948 (epg-context-set-operation context 'decrypt)
1949 (epg-context-set-result context nil)
1950 (epg--start context (list "--decrypt" "--" (epg-data-file cipher)))
1951 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1952 (unless (eq (epg-context-protocol context) 'CMS)
1953 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1955 (defun epg--check-error-for-decrypt (context)
1956 (if (epg-context-result-for context 'decryption-failed)
1957 (signal 'epg-error (list "Decryption failed")))
1958 (if (epg-context-result-for context 'no-secret-key)
1959 (signal 'epg-error
1960 (list "No secret key"
1961 (epg-context-result-for context 'no-secret-key))))
1962 (unless (epg-context-result-for context 'decryption-okay)
1963 (let* ((error (epg-context-result-for context 'error)))
1964 (if (assq 'no-data error)
1965 (signal 'epg-error (list "No data")))
1966 (signal 'epg-error (list "Can't decrypt" error)))))
1968 (defun epg-decrypt-file (context cipher plain)
1969 "Decrypt a file CIPHER and store the result to a file PLAIN.
1970 If PLAIN is nil, it returns the result as a string."
1971 (unwind-protect
1972 (progn
1973 (if plain
1974 (epg-context-set-output-file context plain)
1975 (epg-context-set-output-file context
1976 (epg--make-temp-file "epg-output")))
1977 (epg-start-decrypt context (epg-make-data-from-file cipher))
1978 (epg-wait-for-completion context)
1979 (epg--check-error-for-decrypt context)
1980 (unless plain
1981 (epg-read-output context)))
1982 (unless plain
1983 (epg-delete-output-file context))
1984 (epg-reset context)))
1986 (defun epg-decrypt-string (context cipher)
1987 "Decrypt a string CIPHER and return the plain text."
1988 (let ((input-file (epg--make-temp-file "epg-input"))
1989 (coding-system-for-write 'binary))
1990 (unwind-protect
1991 (progn
1992 (write-region cipher nil input-file nil 'quiet)
1993 (epg-context-set-output-file context
1994 (epg--make-temp-file "epg-output"))
1995 (epg-start-decrypt context (epg-make-data-from-file input-file))
1996 (epg-wait-for-completion context)
1997 (epg--check-error-for-decrypt context)
1998 (epg-read-output context))
1999 (epg-delete-output-file context)
2000 (if (file-exists-p input-file)
2001 (delete-file input-file))
2002 (epg-reset context))))
2004 (defun epg-start-verify (context signature &optional signed-text)
2005 "Initiate a verify operation on SIGNATURE.
2006 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
2008 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
2009 For a normal or a cleartext signature, SIGNED-TEXT should be nil.
2011 If you use this function, you will need to wait for the completion of
2012 `epg-gpg-program' by using `epg-wait-for-completion' and call
2013 `epg-reset' to clear a temporaly output file.
2014 If you are unsure, use synchronous version of this function
2015 `epg-verify-file' or `epg-verify-string' instead."
2016 (epg-context-set-operation context 'verify)
2017 (epg-context-set-result context nil)
2018 (if signed-text
2019 ;; Detached signature.
2020 (if (epg-data-file signed-text)
2021 (epg--start context (list "--verify" "--" (epg-data-file signature)
2022 (epg-data-file signed-text)))
2023 (epg--start context (list "--verify" "--" (epg-data-file signature)
2024 "-"))
2025 (if (eq (process-status (epg-context-process context)) 'run)
2026 (process-send-string (epg-context-process context)
2027 (epg-data-string signed-text)))
2028 (if (eq (process-status (epg-context-process context)) 'run)
2029 (process-send-eof (epg-context-process context))))
2030 ;; Normal (or cleartext) signature.
2031 (if (epg-data-file signature)
2032 (epg--start context (if (eq (epg-context-protocol context) 'CMS)
2033 (list "--verify" "--" (epg-data-file signature))
2034 (list "--" (epg-data-file signature))))
2035 (epg--start context (if (eq (epg-context-protocol context) 'CMS)
2036 '("--verify" "-")
2037 '("-")))
2038 (if (eq (process-status (epg-context-process context)) 'run)
2039 (process-send-string (epg-context-process context)
2040 (epg-data-string signature)))
2041 (if (eq (process-status (epg-context-process context)) 'run)
2042 (process-send-eof (epg-context-process context))))))
2044 (defun epg-verify-file (context signature &optional signed-text plain)
2045 "Verify a file SIGNATURE.
2046 SIGNED-TEXT and PLAIN are also a file if they are specified.
2048 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
2049 string. For a normal or a cleartext signature, SIGNED-TEXT should be
2050 nil. In the latter case, if PLAIN is specified, the plaintext is
2051 stored into the file after successful verification."
2052 (unwind-protect
2053 (progn
2054 (if plain
2055 (epg-context-set-output-file context plain)
2056 (epg-context-set-output-file context
2057 (epg--make-temp-file "epg-output")))
2058 (if signed-text
2059 (epg-start-verify context
2060 (epg-make-data-from-file signature)
2061 (epg-make-data-from-file signed-text))
2062 (epg-start-verify context
2063 (epg-make-data-from-file signature)))
2064 (epg-wait-for-completion context)
2065 (unless plain
2066 (epg-read-output context)))
2067 (unless plain
2068 (epg-delete-output-file context))
2069 (epg-reset context)))
2071 (defun epg-verify-string (context signature &optional signed-text)
2072 "Verify a string SIGNATURE.
2073 SIGNED-TEXT is a string if it is specified.
2075 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
2076 string. For a normal or a cleartext signature, SIGNED-TEXT should be
2077 nil. In the latter case, this function returns the plaintext after
2078 successful verification."
2079 (let ((coding-system-for-write 'binary)
2080 input-file)
2081 (unwind-protect
2082 (progn
2083 (epg-context-set-output-file context
2084 (epg--make-temp-file "epg-output"))
2085 (if signed-text
2086 (progn
2087 (setq input-file (epg--make-temp-file "epg-signature"))
2088 (write-region signature nil input-file nil 'quiet)
2089 (epg-start-verify context
2090 (epg-make-data-from-file input-file)
2091 (epg-make-data-from-string signed-text)))
2092 (epg-start-verify context (epg-make-data-from-string signature)))
2093 (epg-wait-for-completion context)
2094 (epg-read-output context))
2095 (epg-delete-output-file context)
2096 (if (and input-file
2097 (file-exists-p input-file))
2098 (delete-file input-file))
2099 (epg-reset context))))
2101 (defun epg-start-sign (context plain &optional mode)
2102 "Initiate a sign operation on PLAIN.
2103 PLAIN is a data object.
2105 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2106 If it is nil or 'normal, it makes a normal signature.
2107 Otherwise, it makes a cleartext signature.
2109 If you use this function, you will need to wait for the completion of
2110 `epg-gpg-program' by using `epg-wait-for-completion' and call
2111 `epg-reset' to clear a temporaly output file.
2112 If you are unsure, use synchronous version of this function
2113 `epg-sign-file' or `epg-sign-string' instead."
2114 (epg-context-set-operation context 'sign)
2115 (epg-context-set-result context nil)
2116 (unless (memq mode '(t detached nil normal)) ;i.e. cleartext
2117 (epg-context-set-armor context nil)
2118 (epg-context-set-textmode context nil))
2119 (epg--start context
2120 (append (list (if (memq mode '(t detached))
2121 "--detach-sign"
2122 (if (memq mode '(nil normal))
2123 "--sign"
2124 "--clearsign")))
2125 (apply #'nconc
2126 (mapcar
2127 (lambda (signer)
2128 (list "-u"
2129 (epg-sub-key-id
2130 (car (epg-key-sub-key-list signer)))))
2131 (epg-context-signers context)))
2132 (epg--args-from-sig-notations
2133 (epg-context-sig-notations context))
2134 (if (epg-data-file plain)
2135 (list "--" (epg-data-file plain)))))
2136 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2137 (unless (eq (epg-context-protocol context) 'CMS)
2138 (epg-wait-for-status context '("BEGIN_SIGNING")))
2139 (when (epg-data-string plain)
2140 (if (eq (process-status (epg-context-process context)) 'run)
2141 (process-send-string (epg-context-process context)
2142 (epg-data-string plain)))
2143 (if (eq (process-status (epg-context-process context)) 'run)
2144 (process-send-eof (epg-context-process context)))))
2146 (defun epg-sign-file (context plain signature &optional mode)
2147 "Sign a file PLAIN and store the result to a file SIGNATURE.
2148 If SIGNATURE is nil, it returns the result as a string.
2149 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2150 If it is nil or 'normal, it makes a normal signature.
2151 Otherwise, it makes a cleartext signature."
2152 (unwind-protect
2153 (progn
2154 (if signature
2155 (epg-context-set-output-file context signature)
2156 (epg-context-set-output-file context
2157 (epg--make-temp-file "epg-output")))
2158 (epg-start-sign context (epg-make-data-from-file plain) mode)
2159 (epg-wait-for-completion context)
2160 (unless (epg-context-result-for context 'sign)
2161 (if (epg-context-result-for context 'error)
2162 (error "Sign failed: %S"
2163 (epg-context-result-for context 'error))
2164 (error "Sign failed")))
2165 (unless signature
2166 (epg-read-output context)))
2167 (unless signature
2168 (epg-delete-output-file context))
2169 (epg-reset context)))
2171 (defun epg-sign-string (context plain &optional mode)
2172 "Sign a string PLAIN and return the output as string.
2173 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2174 If it is nil or 'normal, it makes a normal signature.
2175 Otherwise, it makes a cleartext signature."
2176 (let ((input-file
2177 (unless (or (eq (epg-context-protocol context) 'CMS)
2178 (condition-case nil
2179 (progn
2180 (epg-check-configuration (epg-configuration))
2182 (error)))
2183 (epg--make-temp-file "epg-input")))
2184 (coding-system-for-write 'binary))
2185 (unwind-protect
2186 (progn
2187 (epg-context-set-output-file context
2188 (epg--make-temp-file "epg-output"))
2189 (if input-file
2190 (write-region plain nil input-file nil 'quiet))
2191 (epg-start-sign context
2192 (if input-file
2193 (epg-make-data-from-file input-file)
2194 (epg-make-data-from-string plain))
2195 mode)
2196 (epg-wait-for-completion context)
2197 (unless (epg-context-result-for context 'sign)
2198 (if (epg-context-result-for context 'error)
2199 (error "Sign failed: %S"
2200 (epg-context-result-for context 'error))
2201 (error "Sign failed")))
2202 (epg-read-output context))
2203 (epg-delete-output-file context)
2204 (if input-file
2205 (delete-file input-file))
2206 (epg-reset context))))
2208 (defun epg-start-encrypt (context plain recipients
2209 &optional sign always-trust)
2210 "Initiate an encrypt operation on PLAIN.
2211 PLAIN is a data object.
2212 If RECIPIENTS is nil, it performs symmetric encryption.
2214 If you use this function, you will need to wait for the completion of
2215 `epg-gpg-program' by using `epg-wait-for-completion' and call
2216 `epg-reset' to clear a temporaly output file.
2217 If you are unsure, use synchronous version of this function
2218 `epg-encrypt-file' or `epg-encrypt-string' instead."
2219 (epg-context-set-operation context 'encrypt)
2220 (epg-context-set-result context nil)
2221 (epg--start context
2222 (append (if always-trust '("--always-trust"))
2223 (if recipients '("--encrypt") '("--symmetric"))
2224 (if sign '("--sign"))
2225 (if sign
2226 (apply #'nconc
2227 (mapcar
2228 (lambda (signer)
2229 (list "-u"
2230 (epg-sub-key-id
2231 (car (epg-key-sub-key-list
2232 signer)))))
2233 (epg-context-signers context))))
2234 (if sign
2235 (epg--args-from-sig-notations
2236 (epg-context-sig-notations context)))
2237 (apply #'nconc
2238 (mapcar
2239 (lambda (recipient)
2240 (list "-r"
2241 (epg-sub-key-id
2242 (car (epg-key-sub-key-list recipient)))))
2243 recipients))
2244 (if (epg-data-file plain)
2245 (list "--" (epg-data-file plain)))))
2246 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2247 (unless (eq (epg-context-protocol context) 'CMS)
2248 (if sign
2249 (epg-wait-for-status context '("BEGIN_SIGNING"))
2250 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
2251 (when (epg-data-string plain)
2252 (if (eq (process-status (epg-context-process context)) 'run)
2253 (process-send-string (epg-context-process context)
2254 (epg-data-string plain)))
2255 (if (eq (process-status (epg-context-process context)) 'run)
2256 (process-send-eof (epg-context-process context)))))
2258 (defun epg-encrypt-file (context plain recipients
2259 cipher &optional sign always-trust)
2260 "Encrypt a file PLAIN and store the result to a file CIPHER.
2261 If CIPHER is nil, it returns the result as a string.
2262 If RECIPIENTS is nil, it performs symmetric encryption."
2263 (unwind-protect
2264 (progn
2265 (if cipher
2266 (epg-context-set-output-file context cipher)
2267 (epg-context-set-output-file context
2268 (epg--make-temp-file "epg-output")))
2269 (epg-start-encrypt context (epg-make-data-from-file plain)
2270 recipients sign always-trust)
2271 (epg-wait-for-completion context)
2272 (if (and sign
2273 (not (epg-context-result-for context 'sign)))
2274 (if (epg-context-result-for context 'error)
2275 (error "Sign failed: %S"
2276 (epg-context-result-for context 'error))
2277 (error "Sign failed")))
2278 (if (epg-context-result-for context 'error)
2279 (error "Encrypt failed: %S"
2280 (epg-context-result-for context 'error)))
2281 (unless cipher
2282 (epg-read-output context)))
2283 (unless cipher
2284 (epg-delete-output-file context))
2285 (epg-reset context)))
2287 (defun epg-encrypt-string (context plain recipients
2288 &optional sign always-trust)
2289 "Encrypt a string PLAIN.
2290 If RECIPIENTS is nil, it performs symmetric encryption."
2291 (let ((input-file
2292 (unless (or (not sign)
2293 (eq (epg-context-protocol context) 'CMS)
2294 (condition-case nil
2295 (progn
2296 (epg-check-configuration (epg-configuration))
2298 (error)))
2299 (epg--make-temp-file "epg-input")))
2300 (coding-system-for-write 'binary))
2301 (unwind-protect
2302 (progn
2303 (epg-context-set-output-file context
2304 (epg--make-temp-file "epg-output"))
2305 (if input-file
2306 (write-region plain nil input-file nil 'quiet))
2307 (epg-start-encrypt context
2308 (if input-file
2309 (epg-make-data-from-file input-file)
2310 (epg-make-data-from-string plain))
2311 recipients sign always-trust)
2312 (epg-wait-for-completion context)
2313 (if (and sign
2314 (not (epg-context-result-for context 'sign)))
2315 (if (epg-context-result-for context 'error)
2316 (error "Sign failed: %S"
2317 (epg-context-result-for context 'error))
2318 (error "Sign failed")))
2319 (if (epg-context-result-for context 'error)
2320 (error "Encrypt failed: %S"
2321 (epg-context-result-for context 'error)))
2322 (epg-read-output context))
2323 (epg-delete-output-file context)
2324 (if input-file
2325 (delete-file input-file))
2326 (epg-reset context))))
2328 (defun epg-start-export-keys (context keys)
2329 "Initiate an export keys operation.
2331 If you use this function, you will need to wait for the completion of
2332 `epg-gpg-program' by using `epg-wait-for-completion' and call
2333 `epg-reset' to clear a temporaly output file.
2334 If you are unsure, use synchronous version of this function
2335 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
2336 (epg-context-set-operation context 'export-keys)
2337 (epg-context-set-result context nil)
2338 (epg--start context (cons "--export"
2339 (mapcar
2340 (lambda (key)
2341 (epg-sub-key-id
2342 (car (epg-key-sub-key-list key))))
2343 keys))))
2345 (defun epg-export-keys-to-file (context keys file)
2346 "Extract public KEYS."
2347 (unwind-protect
2348 (progn
2349 (if file
2350 (epg-context-set-output-file context file)
2351 (epg-context-set-output-file context
2352 (epg--make-temp-file "epg-output")))
2353 (epg-start-export-keys context keys)
2354 (epg-wait-for-completion context)
2355 (if (epg-context-result-for context 'error)
2356 (error "Export keys failed: %S"
2357 (epg-context-result-for context 'error)))
2358 (unless file
2359 (epg-read-output context)))
2360 (unless file
2361 (epg-delete-output-file context))
2362 (epg-reset context)))
2364 (defun epg-export-keys-to-string (context keys)
2365 "Extract public KEYS and return them as a string."
2366 (epg-export-keys-to-file context keys nil))
2368 (defun epg-start-import-keys (context keys)
2369 "Initiate an import keys operation.
2370 KEYS is a data object.
2372 If you use this function, you will need to wait for the completion of
2373 `epg-gpg-program' by using `epg-wait-for-completion' and call
2374 `epg-reset' to clear a temporaly output file.
2375 If you are unsure, use synchronous version of this function
2376 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
2377 (epg-context-set-operation context 'import-keys)
2378 (epg-context-set-result context nil)
2379 (epg--start context (if (epg-data-file keys)
2380 (list "--import" "--" (epg-data-file keys))
2381 (list "--import")))
2382 (when (epg-data-string keys)
2383 (if (eq (process-status (epg-context-process context)) 'run)
2384 (process-send-string (epg-context-process context)
2385 (epg-data-string keys)))
2386 (if (eq (process-status (epg-context-process context)) 'run)
2387 (process-send-eof (epg-context-process context)))))
2389 (defun epg--import-keys-1 (context keys)
2390 (unwind-protect
2391 (progn
2392 (epg-start-import-keys context keys)
2393 (epg-wait-for-completion context)
2394 (if (epg-context-result-for context 'error)
2395 (error "Import keys failed: %S"
2396 (epg-context-result-for context 'error))))
2397 (epg-reset context)))
2399 (defun epg-import-keys-from-file (context keys)
2400 "Add keys from a file KEYS."
2401 (epg--import-keys-1 context (epg-make-data-from-file keys)))
2403 (defun epg-import-keys-from-string (context keys)
2404 "Add keys from a string KEYS."
2405 (epg--import-keys-1 context (epg-make-data-from-string keys)))
2407 (defun epg-start-receive-keys (context key-id-list)
2408 "Initiate a receive key operation.
2409 KEY-ID-LIST is a list of key IDs.
2411 If you use this function, you will need to wait for the completion of
2412 `epg-gpg-program' by using `epg-wait-for-completion' and call
2413 `epg-reset' to clear a temporaly output file.
2414 If you are unsure, use synchronous version of this function
2415 `epg-receive-keys' instead."
2416 (epg-context-set-operation context 'receive-keys)
2417 (epg-context-set-result context nil)
2418 (epg--start context (cons "--recv-keys" key-id-list)))
2420 (defun epg-receive-keys (context keys)
2421 "Add keys from server.
2422 KEYS is a list of key IDs"
2423 (unwind-protect
2424 (progn
2425 (epg-start-receive-keys context keys)
2426 (epg-wait-for-completion context)
2427 (if (epg-context-result-for context 'error)
2428 (error "Receive keys failed: %S"
2429 (epg-context-result-for context 'error))))
2430 (epg-reset context)))
2432 (defalias 'epg-import-keys-from-server 'epg-receive-keys)
2434 (defun epg-start-delete-keys (context keys &optional allow-secret)
2435 "Initiate a delete keys operation.
2437 If you use this function, you will need to wait for the completion of
2438 `epg-gpg-program' by using `epg-wait-for-completion' and call
2439 `epg-reset' to clear a temporaly output file.
2440 If you are unsure, use synchronous version of this function
2441 `epg-delete-keys' instead."
2442 (epg-context-set-operation context 'delete-keys)
2443 (epg-context-set-result context nil)
2444 (epg--start context (cons (if allow-secret
2445 "--delete-secret-key"
2446 "--delete-key")
2447 (mapcar
2448 (lambda (key)
2449 (epg-sub-key-id
2450 (car (epg-key-sub-key-list key))))
2451 keys))))
2453 (defun epg-delete-keys (context keys &optional allow-secret)
2454 "Delete KEYS from the key ring."
2455 (unwind-protect
2456 (progn
2457 (epg-start-delete-keys context keys allow-secret)
2458 (epg-wait-for-completion context)
2459 (let ((entry (assq 'delete-problem
2460 (epg-context-result-for context 'error))))
2461 (if entry
2462 (if (setq entry (assq (cdr entry)
2463 epg-delete-problem-reason-alist))
2464 (error "Delete keys failed: %s" (cdr entry))
2465 (error "Delete keys failed")))))
2466 (epg-reset context)))
2468 (defun epg-start-sign-keys (context keys &optional local)
2469 "Initiate a sign keys operation.
2471 If you use this function, you will need to wait for the completion of
2472 `epg-gpg-program' by using `epg-wait-for-completion' and call
2473 `epg-reset' to clear a temporaly output file.
2474 If you are unsure, use synchronous version of this function
2475 `epg-sign-keys' instead."
2476 (epg-context-set-operation context 'sign-keys)
2477 (epg-context-set-result context nil)
2478 (epg--start context (cons (if local
2479 "--lsign-key"
2480 "--sign-key")
2481 (mapcar
2482 (lambda (key)
2483 (epg-sub-key-id
2484 (car (epg-key-sub-key-list key))))
2485 keys))))
2486 (make-obsolete 'epg-start-sign-keys "do not use." "23.1")
2488 (defun epg-sign-keys (context keys &optional local)
2489 "Sign KEYS from the key ring."
2490 (unwind-protect
2491 (progn
2492 (epg-start-sign-keys context keys local)
2493 (epg-wait-for-completion context)
2494 (if (epg-context-result-for context 'error)
2495 (error "Sign keys failed: %S"
2496 (epg-context-result-for context 'error))))
2497 (epg-reset context)))
2498 (make-obsolete 'epg-sign-keys "do not use." "23.1")
2500 (defun epg-start-generate-key (context parameters)
2501 "Initiate a key generation.
2502 PARAMETERS specifies parameters for the key.
2504 If you use this function, you will need to wait for the completion of
2505 `epg-gpg-program' by using `epg-wait-for-completion' and call
2506 `epg-reset' to clear a temporaly output file.
2507 If you are unsure, use synchronous version of this function
2508 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2509 (epg-context-set-operation context 'generate-key)
2510 (epg-context-set-result context nil)
2511 (if (epg-data-file parameters)
2512 (epg--start context (list "--batch" "--genkey" "--"
2513 (epg-data-file parameters)))
2514 (epg--start context '("--batch" "--genkey"))
2515 (if (eq (process-status (epg-context-process context)) 'run)
2516 (process-send-string (epg-context-process context)
2517 (epg-data-string parameters)))
2518 (if (eq (process-status (epg-context-process context)) 'run)
2519 (process-send-eof (epg-context-process context)))))
2521 (defun epg-generate-key-from-file (context parameters)
2522 "Generate a new key pair.
2523 PARAMETERS is a file which tells how to create the key."
2524 (unwind-protect
2525 (progn
2526 (epg-start-generate-key context (epg-make-data-from-file parameters))
2527 (epg-wait-for-completion context)
2528 (if (epg-context-result-for context 'error)
2529 (error "Generate key failed: %S"
2530 (epg-context-result-for context 'error))))
2531 (epg-reset context)))
2533 (defun epg-generate-key-from-string (context parameters)
2534 "Generate a new key pair.
2535 PARAMETERS is a string which tells how to create the key."
2536 (unwind-protect
2537 (progn
2538 (epg-start-generate-key context (epg-make-data-from-string parameters))
2539 (epg-wait-for-completion context)
2540 (if (epg-context-result-for context 'error)
2541 (error "Generate key failed: %S"
2542 (epg-context-result-for context 'error))))
2543 (epg-reset context)))
2545 (defun epg--decode-percent-escape (string)
2546 (let ((index 0))
2547 (while (string-match "%\\(\\(%\\)\\|\\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2548 string index)
2549 (if (match-beginning 2)
2550 (setq string (replace-match "%" t t string)
2551 index (1- (match-end 0)))
2552 (setq string (replace-match
2553 (string (string-to-number (match-string 3 string) 16))
2554 t t string)
2555 index (- (match-end 0) 2))))
2556 string))
2558 (defun epg--decode-hexstring (string)
2559 (let ((index 0))
2560 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
2561 (setq string (replace-match (string (string-to-number
2562 (match-string 0 string) 16))
2563 t t string)
2564 index (1- (match-end 0))))
2565 string))
2567 (defun epg--decode-quotedstring (string)
2568 (let ((index 0))
2569 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2570 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2571 string index)
2572 (if (match-beginning 2)
2573 (setq string (replace-match "\\2" t nil string)
2574 index (1- (match-end 0)))
2575 (if (match-beginning 3)
2576 (setq string (replace-match (string (string-to-number
2577 (match-string 0 string) 16))
2578 t t string)
2579 index (- (match-end 0) 2)))))
2580 string))
2582 (defun epg-dn-from-string (string)
2583 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2584 The return value is an alist mapping from types to values."
2585 (let ((index 0)
2586 (length (length string))
2587 alist type value group)
2588 (while (< index length)
2589 (if (eq index (string-match "[ \t\n\r]*" string index))
2590 (setq index (match-end 0)))
2591 (if (eq index (string-match
2592 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2593 string index))
2594 (setq type (match-string 1 string)
2595 index (match-end 0))
2596 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2597 string index))
2598 (setq type (match-string 1 string)
2599 index (match-end 0))))
2600 (unless type
2601 (error "Invalid type"))
2602 (if (eq index (string-match
2603 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2604 string index))
2605 (setq index (match-end 0)
2606 value (epg--decode-quotedstring (match-string 0 string)))
2607 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
2608 (setq index (match-end 0)
2609 value (epg--decode-hexstring (match-string 1 string)))
2610 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2611 string index))
2612 (setq index (match-end 0)
2613 value (epg--decode-quotedstring
2614 (match-string 0 string))))))
2615 (if group
2616 (if (stringp (car (car alist)))
2617 (setcar alist (list (cons type value) (car alist)))
2618 (setcar alist (cons (cons type value) (car alist))))
2619 (if (consp (car (car alist)))
2620 (setcar alist (nreverse (car alist))))
2621 (setq alist (cons (cons type value) alist)
2622 type nil
2623 value nil))
2624 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
2625 (setq index (match-end 0)
2626 group (eq (aref string (match-beginning 1)) ?+))))
2627 (nreverse alist)))
2629 (defun epg-decode-dn (alist)
2630 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2631 Type names are resolved using `epg-dn-type-alist'."
2632 (mapconcat
2633 (lambda (rdn)
2634 (if (stringp (car rdn))
2635 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
2636 (if entry
2637 (format "%s=%s" (cdr entry) (cdr rdn))
2638 (format "%s=%s" (car rdn) (cdr rdn))))
2639 (concat "(" (epg-decode-dn rdn) ")")))
2640 alist
2641 ", "))
2643 (provide 'epg)
2645 ;; arch-tag: de8f0acc-1bcf-4c14-a09e-bfffe1b579b7
2646 ;;; epg.el ends here