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