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