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