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