Merge from emacs-24; up to 2012-12-10T20:27:33Z!eggert@cs.ucla.edu
[emacs.git] / lisp / epg.el
blob3f04aa2e07a919b1c337554b0f9af1eaa4eae08a
1 ;;; epg.el --- the EasyPG Library -*- lexical-binding: t -*-
2 ;; Copyright (C) 1999-2000, 2002-2013 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-capablity-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 (put 'epg-error 'error-conditions '(epg-error error))
166 (put 'epg-error 'error-message "GPG error")
168 (defun epg-make-data-from-file (file)
169 "Make a data object from FILE."
170 (cons 'epg-data (vector file nil)))
172 (defun epg-make-data-from-string (string)
173 "Make a data object from STRING."
174 (cons 'epg-data (vector nil string)))
176 (defun epg-data-file (data)
177 "Return the file of DATA."
178 (unless (eq (car-safe data) 'epg-data)
179 (signal 'wrong-type-argument (list 'epg-data-p data)))
180 (aref (cdr data) 0))
182 (defun epg-data-string (data)
183 "Return the string of DATA."
184 (unless (eq (car-safe data) 'epg-data)
185 (signal 'wrong-type-argument (list 'epg-data-p data)))
186 (aref (cdr data) 1))
188 ;;;###autoload
189 (defun epg-make-context (&optional protocol armor textmode include-certs
190 cipher-algorithm digest-algorithm
191 compress-algorithm)
192 "Return a context object."
193 (cons 'epg-context
194 (vector (or protocol 'OpenPGP) armor textmode include-certs
195 cipher-algorithm digest-algorithm compress-algorithm
196 (list #'epg-passphrase-callback-function)
198 nil nil nil nil nil nil)))
200 (defun epg-context-protocol (context)
201 "Return the protocol used within CONTEXT."
202 (unless (eq (car-safe context) 'epg-context)
203 (signal 'wrong-type-argument (list 'epg-context-p context)))
204 (aref (cdr context) 0))
206 (defun epg-context-armor (context)
207 "Return t if the output should be ASCII armored in CONTEXT."
208 (unless (eq (car-safe context) 'epg-context)
209 (signal 'wrong-type-argument (list 'epg-context-p context)))
210 (aref (cdr context) 1))
212 (defun epg-context-textmode (context)
213 "Return t if canonical text mode should be used in CONTEXT."
214 (unless (eq (car-safe context) 'epg-context)
215 (signal 'wrong-type-argument (list 'epg-context-p context)))
216 (aref (cdr context) 2))
218 (defun epg-context-include-certs (context)
219 "Return how many certificates should be included in an S/MIME signed message."
220 (unless (eq (car-safe context) 'epg-context)
221 (signal 'wrong-type-argument (list 'epg-context-p context)))
222 (aref (cdr context) 3))
224 (defun epg-context-cipher-algorithm (context)
225 "Return the cipher algorithm in CONTEXT."
226 (unless (eq (car-safe context) 'epg-context)
227 (signal 'wrong-type-argument (list 'epg-context-p context)))
228 (aref (cdr context) 4))
230 (defun epg-context-digest-algorithm (context)
231 "Return the digest algorithm in CONTEXT."
232 (unless (eq (car-safe context) 'epg-context)
233 (signal 'wrong-type-argument (list 'epg-context-p context)))
234 (aref (cdr context) 5))
236 (defun epg-context-compress-algorithm (context)
237 "Return the compress algorithm in CONTEXT."
238 (unless (eq (car-safe context) 'epg-context)
239 (signal 'wrong-type-argument (list 'epg-context-p context)))
240 (aref (cdr context) 6))
242 (defun epg-context-passphrase-callback (context)
243 "Return the function used to query passphrase."
244 (unless (eq (car-safe context) 'epg-context)
245 (signal 'wrong-type-argument (list 'epg-context-p context)))
246 (aref (cdr context) 7))
248 (defun epg-context-progress-callback (context)
249 "Return the function which handles progress update."
250 (unless (eq (car-safe context) 'epg-context)
251 (signal 'wrong-type-argument (list 'epg-context-p context)))
252 (aref (cdr context) 8))
254 (defun epg-context-signers (context)
255 "Return the list of key-id for signing."
256 (unless (eq (car-safe context) 'epg-context)
257 (signal 'wrong-type-argument (list 'epg-context-p context)))
258 (aref (cdr context) 9))
260 (defun epg-context-sig-notations (context)
261 "Return the list of notations for signing."
262 (unless (eq (car-safe context) 'epg-context)
263 (signal 'wrong-type-argument (list 'epg-context-p context)))
264 (aref (cdr context) 10))
266 (defun epg-context-process (context)
267 "Return the process object of `epg-gpg-program'.
268 This function is for internal use only."
269 (unless (eq (car-safe context) 'epg-context)
270 (signal 'wrong-type-argument (list 'epg-context-p context)))
271 (aref (cdr context) 11))
273 (defun epg-context-output-file (context)
274 "Return the output file of `epg-gpg-program'.
275 This function is for internal use only."
276 (unless (eq (car-safe context) 'epg-context)
277 (signal 'wrong-type-argument (list 'epg-context-p context)))
278 (aref (cdr context) 12))
280 (defun epg-context-result (context)
281 "Return the result of the previous cryptographic operation."
282 (unless (eq (car-safe context) 'epg-context)
283 (signal 'wrong-type-argument (list 'epg-context-p context)))
284 (aref (cdr context) 13))
286 (defun epg-context-operation (context)
287 "Return the name of the current cryptographic operation."
288 (unless (eq (car-safe context) 'epg-context)
289 (signal 'wrong-type-argument (list 'epg-context-p context)))
290 (aref (cdr context) 14))
292 (defun epg-context-set-protocol (context protocol)
293 "Set the protocol used within CONTEXT."
294 (unless (eq (car-safe context) 'epg-context)
295 (signal 'wrong-type-argument (list 'epg-context-p context)))
296 (aset (cdr context) 0 protocol))
298 (defun epg-context-set-armor (context armor)
299 "Specify if the output should be ASCII armored in CONTEXT."
300 (unless (eq (car-safe context) 'epg-context)
301 (signal 'wrong-type-argument (list 'epg-context-p context)))
302 (aset (cdr context) 1 armor))
304 (defun epg-context-set-textmode (context textmode)
305 "Specify if canonical text mode should be used in CONTEXT."
306 (unless (eq (car-safe context) 'epg-context)
307 (signal 'wrong-type-argument (list 'epg-context-p context)))
308 (aset (cdr context) 2 textmode))
310 (defun epg-context-set-include-certs (context include-certs)
311 "Set how many certificates should be included in an S/MIME signed message."
312 (unless (eq (car-safe context) 'epg-context)
313 (signal 'wrong-type-argument (list 'epg-context-p context)))
314 (aset (cdr context) 3 include-certs))
316 (defun epg-context-set-cipher-algorithm (context cipher-algorithm)
317 "Set the cipher algorithm in CONTEXT."
318 (unless (eq (car-safe context) 'epg-context)
319 (signal 'wrong-type-argument (list 'epg-context-p context)))
320 (aset (cdr context) 4 cipher-algorithm))
322 (defun epg-context-set-digest-algorithm (context digest-algorithm)
323 "Set the digest algorithm in CONTEXT."
324 (unless (eq (car-safe context) 'epg-context)
325 (signal 'wrong-type-argument (list 'epg-context-p context)))
326 (aset (cdr context) 5 digest-algorithm))
328 (defun epg-context-set-compress-algorithm (context compress-algorithm)
329 "Set the compress algorithm in CONTEXT."
330 (unless (eq (car-safe context) 'epg-context)
331 (signal 'wrong-type-argument (list 'epg-context-p context)))
332 (aset (cdr context) 6 compress-algorithm))
334 (defun epg-context-set-passphrase-callback (context
335 passphrase-callback)
336 "Set the function used to query passphrase.
338 PASSPHRASE-CALLBACK is either a function, or a cons-cell whose
339 car is a function and cdr is a callback data.
341 The function gets three arguments: the context, the key-id in
342 question, and the callback data (if any).
344 The callback may not be called if you use GnuPG 2.x, which relies
345 on the external program called `gpg-agent' for passphrase query.
346 If you really want to intercept passphrase query, consider
347 installing GnuPG 1.x _along with_ GnuPG 2.x, which does passphrase
348 query by itself and Emacs can intercept them."
349 (unless (eq (car-safe context) 'epg-context)
350 (signal 'wrong-type-argument (list 'epg-context-p context)))
351 (aset (cdr context) 7 (if (consp passphrase-callback)
352 passphrase-callback
353 (list passphrase-callback))))
355 (defun epg-context-set-progress-callback (context
356 progress-callback)
357 "Set the function which handles progress update.
359 PROGRESS-CALLBACK is either a function, or a cons-cell whose
360 car is a function and cdr is a callback data.
362 The function gets six arguments: the context, the operation
363 description, the character to display a progress unit, the
364 current amount done, the total amount to be done, and the
365 callback data (if any)."
366 (unless (eq (car-safe context) 'epg-context)
367 (signal 'wrong-type-argument (list 'epg-context-p context)))
368 (aset (cdr context) 8 (if (consp progress-callback)
369 progress-callback
370 (list progress-callback))))
372 (defun epg-context-set-signers (context signers)
373 "Set the list of key-id for signing."
374 (unless (eq (car-safe context) 'epg-context)
375 (signal 'wrong-type-argument (list 'epg-context-p context)))
376 (aset (cdr context) 9 signers))
378 (defun epg-context-set-sig-notations (context notations)
379 "Set the list of notations for signing."
380 (unless (eq (car-safe context) 'epg-context)
381 (signal 'wrong-type-argument (list 'epg-context-p context)))
382 (aset (cdr context) 10 notations))
384 (defun epg-context-set-process (context process)
385 "Set the process object of `epg-gpg-program'.
386 This function is for internal use only."
387 (unless (eq (car-safe context) 'epg-context)
388 (signal 'wrong-type-argument (list 'epg-context-p context)))
389 (aset (cdr context) 11 process))
391 (defun epg-context-set-output-file (context output-file)
392 "Set the output file of `epg-gpg-program'.
393 This function is for internal use only."
394 (unless (eq (car-safe context) 'epg-context)
395 (signal 'wrong-type-argument (list 'epg-context-p context)))
396 (aset (cdr context) 12 output-file))
398 (defun epg-context-set-result (context result)
399 "Set the result of the previous cryptographic operation."
400 (unless (eq (car-safe context) 'epg-context)
401 (signal 'wrong-type-argument (list 'epg-context-p context)))
402 (aset (cdr context) 13 result))
404 (defun epg-context-set-operation (context operation)
405 "Set the name of the current cryptographic operation."
406 (unless (eq (car-safe context) 'epg-context)
407 (signal 'wrong-type-argument (list 'epg-context-p context)))
408 (aset (cdr context) 14 operation))
410 (defun epg-make-signature (status &optional key-id)
411 "Return a signature object."
412 (cons 'epg-signature (vector status key-id nil nil nil nil nil nil nil nil
413 nil)))
415 (defun epg-signature-status (signature)
416 "Return the status code of SIGNATURE."
417 (unless (eq (car-safe signature) 'epg-signature)
418 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
419 (aref (cdr signature) 0))
421 (defun epg-signature-key-id (signature)
422 "Return the key-id of SIGNATURE."
423 (unless (eq (car-safe signature) 'epg-signature)
424 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
425 (aref (cdr signature) 1))
427 (defun epg-signature-validity (signature)
428 "Return the validity of SIGNATURE."
429 (unless (eq (car-safe signature) 'epg-signature)
430 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
431 (aref (cdr signature) 2))
433 (defun epg-signature-fingerprint (signature)
434 "Return the fingerprint of SIGNATURE."
435 (unless (eq (car-safe signature) 'epg-signature)
436 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
437 (aref (cdr signature) 3))
439 (defun epg-signature-creation-time (signature)
440 "Return the creation time of SIGNATURE."
441 (unless (eq (car-safe signature) 'epg-signature)
442 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
443 (aref (cdr signature) 4))
445 (defun epg-signature-expiration-time (signature)
446 "Return the expiration time of SIGNATURE."
447 (unless (eq (car-safe signature) 'epg-signature)
448 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
449 (aref (cdr signature) 5))
451 (defun epg-signature-pubkey-algorithm (signature)
452 "Return the public key algorithm of SIGNATURE."
453 (unless (eq (car-safe signature) 'epg-signature)
454 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
455 (aref (cdr signature) 6))
457 (defun epg-signature-digest-algorithm (signature)
458 "Return the digest algorithm of SIGNATURE."
459 (unless (eq (car-safe signature) 'epg-signature)
460 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
461 (aref (cdr signature) 7))
463 (defun epg-signature-class (signature)
464 "Return the class of SIGNATURE."
465 (unless (eq (car-safe signature) 'epg-signature)
466 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
467 (aref (cdr signature) 8))
469 (defun epg-signature-version (signature)
470 "Return the version of SIGNATURE."
471 (unless (eq (car-safe signature) 'epg-signature)
472 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
473 (aref (cdr signature) 9))
475 (defun epg-sig-notations (signature)
476 "Return the list of notations of SIGNATURE."
477 (unless (eq (car-safe signature) 'epg-signature)
478 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
479 (aref (cdr signature) 10))
481 (defun epg-signature-set-status (signature status)
482 "Set the status code of SIGNATURE."
483 (unless (eq (car-safe signature) 'epg-signature)
484 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
485 (aset (cdr signature) 0 status))
487 (defun epg-signature-set-key-id (signature key-id)
488 "Set the key-id of SIGNATURE."
489 (unless (eq (car-safe signature) 'epg-signature)
490 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
491 (aset (cdr signature) 1 key-id))
493 (defun epg-signature-set-validity (signature validity)
494 "Set the validity of SIGNATURE."
495 (unless (eq (car-safe signature) 'epg-signature)
496 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
497 (aset (cdr signature) 2 validity))
499 (defun epg-signature-set-fingerprint (signature fingerprint)
500 "Set the fingerprint of SIGNATURE."
501 (unless (eq (car-safe signature) 'epg-signature)
502 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
503 (aset (cdr signature) 3 fingerprint))
505 (defun epg-signature-set-creation-time (signature creation-time)
506 "Set the creation time of SIGNATURE."
507 (unless (eq (car-safe signature) 'epg-signature)
508 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
509 (aset (cdr signature) 4 creation-time))
511 (defun epg-signature-set-expiration-time (signature expiration-time)
512 "Set the expiration time of SIGNATURE."
513 (unless (eq (car-safe signature) 'epg-signature)
514 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
515 (aset (cdr signature) 5 expiration-time))
517 (defun epg-signature-set-pubkey-algorithm (signature pubkey-algorithm)
518 "Set the public key algorithm of SIGNATURE."
519 (unless (eq (car-safe signature) 'epg-signature)
520 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
521 (aset (cdr signature) 6 pubkey-algorithm))
523 (defun epg-signature-set-digest-algorithm (signature digest-algorithm)
524 "Set the digest algorithm of SIGNATURE."
525 (unless (eq (car-safe signature) 'epg-signature)
526 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
527 (aset (cdr signature) 7 digest-algorithm))
529 (defun epg-signature-set-class (signature class)
530 "Set the class of SIGNATURE."
531 (unless (eq (car-safe signature) 'epg-signature)
532 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
533 (aset (cdr signature) 8 class))
535 (defun epg-signature-set-version (signature version)
536 "Set the version of SIGNATURE."
537 (unless (eq (car-safe signature) 'epg-signature)
538 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
539 (aset (cdr signature) 9 version))
541 (defun epg-signature-set-notations (signature notations)
542 "Set the list of notations of SIGNATURE."
543 (unless (eq (car-safe signature) 'epg-signature)
544 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
545 (aset (cdr signature) 10 notations))
547 (defun epg-make-new-signature (type pubkey-algorithm digest-algorithm
548 class creation-time fingerprint)
549 "Return a new signature object."
550 (cons 'epg-new-signature (vector type pubkey-algorithm digest-algorithm
551 class creation-time fingerprint)))
553 (defun epg-new-signature-type (new-signature)
554 "Return the type of NEW-SIGNATURE."
555 (unless (eq (car-safe new-signature) 'epg-new-signature)
556 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
557 (aref (cdr new-signature) 0))
559 (defun epg-new-signature-pubkey-algorithm (new-signature)
560 "Return the public key algorithm of NEW-SIGNATURE."
561 (unless (eq (car-safe new-signature) 'epg-new-signature)
562 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
563 (aref (cdr new-signature) 1))
565 (defun epg-new-signature-digest-algorithm (new-signature)
566 "Return the digest algorithm of NEW-SIGNATURE."
567 (unless (eq (car-safe new-signature) 'epg-new-signature)
568 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
569 (aref (cdr new-signature) 2))
571 (defun epg-new-signature-class (new-signature)
572 "Return the class of NEW-SIGNATURE."
573 (unless (eq (car-safe new-signature) 'epg-new-signature)
574 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
575 (aref (cdr new-signature) 3))
577 (defun epg-new-signature-creation-time (new-signature)
578 "Return the creation time of NEW-SIGNATURE."
579 (unless (eq (car-safe new-signature) 'epg-new-signature)
580 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
581 (aref (cdr new-signature) 4))
583 (defun epg-new-signature-fingerprint (new-signature)
584 "Return the fingerprint of NEW-SIGNATURE."
585 (unless (eq (car-safe new-signature) 'epg-new-signature)
586 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
587 (aref (cdr new-signature) 5))
589 (defun epg-make-key (owner-trust)
590 "Return a key object."
591 (cons 'epg-key (vector owner-trust nil nil)))
593 (defun epg-key-owner-trust (key)
594 "Return the owner trust of KEY."
595 (unless (eq (car-safe key) 'epg-key)
596 (signal 'wrong-type-argument (list 'epg-key-p key)))
597 (aref (cdr key) 0))
599 (defun epg-key-sub-key-list (key)
600 "Return the sub key list of KEY."
601 (unless (eq (car-safe key) 'epg-key)
602 (signal 'wrong-type-argument (list 'epg-key-p key)))
603 (aref (cdr key) 1))
605 (defun epg-key-user-id-list (key)
606 "Return the user ID list of KEY."
607 (unless (eq (car-safe key) 'epg-key)
608 (signal 'wrong-type-argument (list 'epg-key-p key)))
609 (aref (cdr key) 2))
611 (defun epg-key-set-sub-key-list (key sub-key-list)
612 "Set the sub key list of KEY."
613 (unless (eq (car-safe key) 'epg-key)
614 (signal 'wrong-type-argument (list 'epg-key-p key)))
615 (aset (cdr key) 1 sub-key-list))
617 (defun epg-key-set-user-id-list (key user-id-list)
618 "Set the user ID list of KEY."
619 (unless (eq (car-safe key) 'epg-key)
620 (signal 'wrong-type-argument (list 'epg-key-p key)))
621 (aset (cdr key) 2 user-id-list))
623 (defun epg-make-sub-key (validity capability secret-p algorithm length id
624 creation-time expiration-time)
625 "Return a sub key object."
626 (cons 'epg-sub-key
627 (vector validity capability secret-p algorithm length id creation-time
628 expiration-time nil)))
630 (defun epg-sub-key-validity (sub-key)
631 "Return the validity of SUB-KEY."
632 (unless (eq (car-safe sub-key) 'epg-sub-key)
633 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
634 (aref (cdr sub-key) 0))
636 (defun epg-sub-key-capability (sub-key)
637 "Return the capability of SUB-KEY."
638 (unless (eq (car-safe sub-key) 'epg-sub-key)
639 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
640 (aref (cdr sub-key) 1))
642 (defun epg-sub-key-secret-p (sub-key)
643 "Return non-nil if SUB-KEY is a secret key."
644 (unless (eq (car-safe sub-key) 'epg-sub-key)
645 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
646 (aref (cdr sub-key) 2))
648 (defun epg-sub-key-algorithm (sub-key)
649 "Return the algorithm of SUB-KEY."
650 (unless (eq (car-safe sub-key) 'epg-sub-key)
651 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
652 (aref (cdr sub-key) 3))
654 (defun epg-sub-key-length (sub-key)
655 "Return the length of SUB-KEY."
656 (unless (eq (car-safe sub-key) 'epg-sub-key)
657 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
658 (aref (cdr sub-key) 4))
660 (defun epg-sub-key-id (sub-key)
661 "Return the ID of SUB-KEY."
662 (unless (eq (car-safe sub-key) 'epg-sub-key)
663 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
664 (aref (cdr sub-key) 5))
666 (defun epg-sub-key-creation-time (sub-key)
667 "Return the creation time of SUB-KEY."
668 (unless (eq (car-safe sub-key) 'epg-sub-key)
669 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
670 (aref (cdr sub-key) 6))
672 (defun epg-sub-key-expiration-time (sub-key)
673 "Return the expiration time of SUB-KEY."
674 (unless (eq (car-safe sub-key) 'epg-sub-key)
675 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
676 (aref (cdr sub-key) 7))
678 (defun epg-sub-key-fingerprint (sub-key)
679 "Return the fingerprint of SUB-KEY."
680 (unless (eq (car-safe sub-key) 'epg-sub-key)
681 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
682 (aref (cdr sub-key) 8))
684 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
685 "Set the fingerprint of SUB-KEY.
686 This function is for internal use only."
687 (unless (eq (car-safe sub-key) 'epg-sub-key)
688 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
689 (aset (cdr sub-key) 8 fingerprint))
691 (defun epg-make-user-id (validity string)
692 "Return a user ID object."
693 (cons 'epg-user-id (vector validity string nil)))
695 (defun epg-user-id-validity (user-id)
696 "Return the validity of USER-ID."
697 (unless (eq (car-safe user-id) 'epg-user-id)
698 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
699 (aref (cdr user-id) 0))
701 (defun epg-user-id-string (user-id)
702 "Return the name of USER-ID."
703 (unless (eq (car-safe user-id) 'epg-user-id)
704 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
705 (aref (cdr user-id) 1))
707 (defun epg-user-id-signature-list (user-id)
708 "Return the signature list of USER-ID."
709 (unless (eq (car-safe user-id) 'epg-user-id)
710 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
711 (aref (cdr user-id) 2))
713 (defun epg-user-id-set-signature-list (user-id signature-list)
714 "Set the signature list of USER-ID."
715 (unless (eq (car-safe user-id) 'epg-user-id)
716 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
717 (aset (cdr user-id) 2 signature-list))
719 (defun epg-make-key-signature (validity pubkey-algorithm key-id creation-time
720 expiration-time user-id class
721 exportable-p)
722 "Return a key signature object."
723 (cons 'epg-key-signature
724 (vector validity pubkey-algorithm key-id creation-time expiration-time
725 user-id class exportable-p)))
727 (defun epg-key-signature-validity (key-signature)
728 "Return the validity of KEY-SIGNATURE."
729 (unless (eq (car-safe key-signature) 'epg-key-signature)
730 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
731 (aref (cdr key-signature) 0))
733 (defun epg-key-signature-pubkey-algorithm (key-signature)
734 "Return the public key algorithm of KEY-SIGNATURE."
735 (unless (eq (car-safe key-signature) 'epg-key-signature)
736 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
737 (aref (cdr key-signature) 1))
739 (defun epg-key-signature-key-id (key-signature)
740 "Return the key-id of KEY-SIGNATURE."
741 (unless (eq (car-safe key-signature) 'epg-key-signature)
742 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
743 (aref (cdr key-signature) 2))
745 (defun epg-key-signature-creation-time (key-signature)
746 "Return the creation time of KEY-SIGNATURE."
747 (unless (eq (car-safe key-signature) 'epg-key-signature)
748 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
749 (aref (cdr key-signature) 3))
751 (defun epg-key-signature-expiration-time (key-signature)
752 "Return the expiration time of KEY-SIGNATURE."
753 (unless (eq (car-safe key-signature) 'epg-key-signature)
754 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
755 (aref (cdr key-signature) 4))
757 (defun epg-key-signature-user-id (key-signature)
758 "Return the user-id of KEY-SIGNATURE."
759 (unless (eq (car-safe key-signature) 'epg-key-signature)
760 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
761 (aref (cdr key-signature) 5))
763 (defun epg-key-signature-class (key-signature)
764 "Return the class of KEY-SIGNATURE."
765 (unless (eq (car-safe key-signature) 'epg-key-signature)
766 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
767 (aref (cdr key-signature) 6))
769 (defun epg-key-signature-exportable-p (key-signature)
770 "Return t if KEY-SIGNATURE is exportable."
771 (unless (eq (car-safe key-signature) 'epg-key-signature)
772 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
773 (aref (cdr key-signature) 7))
775 (defun epg-make-sig-notation (name value &optional human-readable
776 critical)
777 "Return a notation object."
778 (cons 'epg-sig-notation (vector name value human-readable critical)))
780 (defun epg-sig-notation-name (sig-notation)
781 "Return the name of SIG-NOTATION."
782 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
783 (signal 'wrong-type-argument (list 'epg-sig-notation-p
784 sig-notation)))
785 (aref (cdr sig-notation) 0))
787 (defun epg-sig-notation-value (sig-notation)
788 "Return the value of SIG-NOTATION."
789 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
790 (signal 'wrong-type-argument (list 'epg-sig-notation-p
791 sig-notation)))
792 (aref (cdr sig-notation) 1))
794 (defun epg-sig-notation-human-readable (sig-notation)
795 "Return the human-readable of SIG-NOTATION."
796 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
797 (signal 'wrong-type-argument (list 'epg-sig-notation-p
798 sig-notation)))
799 (aref (cdr sig-notation) 2))
801 (defun epg-sig-notation-critical (sig-notation)
802 "Return the critical of SIG-NOTATION."
803 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
804 (signal 'wrong-type-argument (list 'epg-sig-notation-p
805 sig-notation)))
806 (aref (cdr sig-notation) 3))
808 (defun epg-sig-notation-set-value (sig-notation value)
809 "Set the value of SIG-NOTATION."
810 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
811 (signal 'wrong-type-argument (list 'epg-sig-notation-p
812 sig-notation)))
813 (aset (cdr sig-notation) 1 value))
815 (defun epg-make-import-status (fingerprint &optional reason new user-id
816 signature sub-key secret)
817 "Return an import status object."
818 (cons 'epg-import-status (vector fingerprint reason new user-id signature
819 sub-key secret)))
821 (defun epg-import-status-fingerprint (import-status)
822 "Return the fingerprint of the key that was considered."
823 (unless (eq (car-safe import-status) 'epg-import-status)
824 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
825 (aref (cdr import-status) 0))
827 (defun epg-import-status-reason (import-status)
828 "Return the reason code for import failure."
829 (unless (eq (car-safe import-status) 'epg-import-status)
830 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
831 (aref (cdr import-status) 1))
833 (defun epg-import-status-new (import-status)
834 "Return t if the imported key was new."
835 (unless (eq (car-safe import-status) 'epg-import-status)
836 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
837 (aref (cdr import-status) 2))
839 (defun epg-import-status-user-id (import-status)
840 "Return t if the imported key contained new user IDs."
841 (unless (eq (car-safe import-status) 'epg-import-status)
842 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
843 (aref (cdr import-status) 3))
845 (defun epg-import-status-signature (import-status)
846 "Return t if the imported key contained new signatures."
847 (unless (eq (car-safe import-status) 'epg-import-status)
848 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
849 (aref (cdr import-status) 4))
851 (defun epg-import-status-sub-key (import-status)
852 "Return t if the imported key contained new sub keys."
853 (unless (eq (car-safe import-status) 'epg-import-status)
854 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
855 (aref (cdr import-status) 5))
857 (defun epg-import-status-secret (import-status)
858 "Return t if the imported key contained a secret key."
859 (unless (eq (car-safe import-status) 'epg-import-status)
860 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
861 (aref (cdr import-status) 6))
863 (defun epg-make-import-result (considered no-user-id imported imported-rsa
864 unchanged new-user-ids new-sub-keys
865 new-signatures new-revocations
866 secret-read secret-imported
867 secret-unchanged not-imported
868 imports)
869 "Return an import result object."
870 (cons 'epg-import-result (vector considered no-user-id imported imported-rsa
871 unchanged new-user-ids new-sub-keys
872 new-signatures new-revocations secret-read
873 secret-imported secret-unchanged
874 not-imported imports)))
876 (defun epg-import-result-considered (import-result)
877 "Return the total number of considered keys."
878 (unless (eq (car-safe import-result) 'epg-import-result)
879 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
880 (aref (cdr import-result) 0))
882 (defun epg-import-result-no-user-id (import-result)
883 "Return the number of keys without user ID."
884 (unless (eq (car-safe import-result) 'epg-import-result)
885 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
886 (aref (cdr import-result) 1))
888 (defun epg-import-result-imported (import-result)
889 "Return the number of imported keys."
890 (unless (eq (car-safe import-result) 'epg-import-result)
891 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
892 (aref (cdr import-result) 2))
894 (defun epg-import-result-imported-rsa (import-result)
895 "Return the number of imported RSA keys."
896 (unless (eq (car-safe import-result) 'epg-import-result)
897 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
898 (aref (cdr import-result) 3))
900 (defun epg-import-result-unchanged (import-result)
901 "Return the number of unchanged keys."
902 (unless (eq (car-safe import-result) 'epg-import-result)
903 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
904 (aref (cdr import-result) 4))
906 (defun epg-import-result-new-user-ids (import-result)
907 "Return the number of new user IDs."
908 (unless (eq (car-safe import-result) 'epg-import-result)
909 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
910 (aref (cdr import-result) 5))
912 (defun epg-import-result-new-sub-keys (import-result)
913 "Return the number of new sub keys."
914 (unless (eq (car-safe import-result) 'epg-import-result)
915 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
916 (aref (cdr import-result) 6))
918 (defun epg-import-result-new-signatures (import-result)
919 "Return the number of new signatures."
920 (unless (eq (car-safe import-result) 'epg-import-result)
921 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
922 (aref (cdr import-result) 7))
924 (defun epg-import-result-new-revocations (import-result)
925 "Return the number of new revocations."
926 (unless (eq (car-safe import-result) 'epg-import-result)
927 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
928 (aref (cdr import-result) 8))
930 (defun epg-import-result-secret-read (import-result)
931 "Return the total number of secret keys read."
932 (unless (eq (car-safe import-result) 'epg-import-result)
933 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
934 (aref (cdr import-result) 9))
936 (defun epg-import-result-secret-imported (import-result)
937 "Return the number of imported secret keys."
938 (unless (eq (car-safe import-result) 'epg-import-result)
939 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
940 (aref (cdr import-result) 10))
942 (defun epg-import-result-secret-unchanged (import-result)
943 "Return the number of unchanged secret keys."
944 (unless (eq (car-safe import-result) 'epg-import-result)
945 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
946 (aref (cdr import-result) 11))
948 (defun epg-import-result-not-imported (import-result)
949 "Return the number of keys not imported."
950 (unless (eq (car-safe import-result) 'epg-import-result)
951 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
952 (aref (cdr import-result) 12))
954 (defun epg-import-result-imports (import-result)
955 "Return the list of `epg-import-status' objects."
956 (unless (eq (car-safe import-result) 'epg-import-result)
957 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
958 (aref (cdr import-result) 13))
960 (defun epg-context-result-for (context name)
961 "Return the result of CONTEXT associated with NAME."
962 (cdr (assq name (epg-context-result context))))
964 (defun epg-context-set-result-for (context name value)
965 "Set the result of CONTEXT associated with NAME to VALUE."
966 (let* ((result (epg-context-result context))
967 (entry (assq name result)))
968 (if entry
969 (setcdr entry value)
970 (epg-context-set-result context (cons (cons name value) result)))))
972 (defun epg-signature-to-string (signature)
973 "Convert SIGNATURE to a human readable string."
974 (let* ((user-id (cdr (assoc (epg-signature-key-id signature)
975 epg-user-id-alist)))
976 (pubkey-algorithm (epg-signature-pubkey-algorithm signature))
977 (key-id (epg-signature-key-id signature)))
978 (concat
979 (cond ((eq (epg-signature-status signature) 'good)
980 "Good signature from ")
981 ((eq (epg-signature-status signature) 'bad)
982 "Bad signature from ")
983 ((eq (epg-signature-status signature) 'expired)
984 "Expired signature from ")
985 ((eq (epg-signature-status signature) 'expired-key)
986 "Signature made by expired key ")
987 ((eq (epg-signature-status signature) 'revoked-key)
988 "Signature made by revoked key ")
989 ((eq (epg-signature-status signature) 'no-pubkey)
990 "No public key for "))
991 key-id
992 (if user-id
993 (concat " "
994 (if (stringp user-id)
995 user-id
996 (epg-decode-dn user-id)))
998 (if (epg-signature-validity signature)
999 (format " (trust %s)" (epg-signature-validity signature))
1001 (if (epg-signature-creation-time signature)
1002 (format-time-string " created at %Y-%m-%dT%T%z"
1003 (epg-signature-creation-time signature))
1005 (if pubkey-algorithm
1006 (concat " using "
1007 (or (cdr (assq pubkey-algorithm epg-pubkey-algorithm-alist))
1008 (format "(unknown algorithm %d)" pubkey-algorithm)))
1009 ""))))
1011 (defun epg-verify-result-to-string (verify-result)
1012 "Convert VERIFY-RESULT to a human readable string."
1013 (mapconcat #'epg-signature-to-string verify-result "\n"))
1015 (defun epg-new-signature-to-string (new-signature)
1016 "Convert NEW-SIGNATURE to a human readable string."
1017 (concat
1018 (cond ((eq (epg-new-signature-type new-signature) 'detached)
1019 "Detached signature ")
1020 ((eq (epg-new-signature-type new-signature) 'clear)
1021 "Cleartext signature ")
1023 "Signature "))
1024 (cdr (assq (epg-new-signature-pubkey-algorithm new-signature)
1025 epg-pubkey-algorithm-alist))
1027 (cdr (assq (epg-new-signature-digest-algorithm new-signature)
1028 epg-digest-algorithm-alist))
1030 (format "%02X " (epg-new-signature-class new-signature))
1031 (epg-new-signature-fingerprint new-signature)))
1033 (defun epg-import-result-to-string (import-result)
1034 "Convert IMPORT-RESULT to a human readable string."
1035 (concat (format "Total number processed: %d\n"
1036 (epg-import-result-considered import-result))
1037 (if (> (epg-import-result-not-imported import-result) 0)
1038 (format " skipped new keys: %d\n"
1039 (epg-import-result-not-imported import-result)))
1040 (if (> (epg-import-result-no-user-id import-result) 0)
1041 (format " w/o user IDs: %d\n"
1042 (epg-import-result-no-user-id import-result)))
1043 (if (> (epg-import-result-imported import-result) 0)
1044 (concat (format " imported: %d"
1045 (epg-import-result-imported import-result))
1046 (if (> (epg-import-result-imported-rsa import-result) 0)
1047 (format " (RSA: %d)"
1048 (epg-import-result-imported-rsa
1049 import-result)))
1050 "\n"))
1051 (if (> (epg-import-result-unchanged import-result) 0)
1052 (format " unchanged: %d\n"
1053 (epg-import-result-unchanged import-result)))
1054 (if (> (epg-import-result-new-user-ids import-result) 0)
1055 (format " new user IDs: %d\n"
1056 (epg-import-result-new-user-ids import-result)))
1057 (if (> (epg-import-result-new-sub-keys import-result) 0)
1058 (format " new subkeys: %d\n"
1059 (epg-import-result-new-sub-keys import-result)))
1060 (if (> (epg-import-result-new-signatures import-result) 0)
1061 (format " new signatures: %d\n"
1062 (epg-import-result-new-signatures import-result)))
1063 (if (> (epg-import-result-new-revocations import-result) 0)
1064 (format " new key revocations: %d\n"
1065 (epg-import-result-new-revocations import-result)))
1066 (if (> (epg-import-result-secret-read import-result) 0)
1067 (format " secret keys read: %d\n"
1068 (epg-import-result-secret-read import-result)))
1069 (if (> (epg-import-result-secret-imported import-result) 0)
1070 (format " secret keys imported: %d\n"
1071 (epg-import-result-secret-imported import-result)))
1072 (if (> (epg-import-result-secret-unchanged import-result) 0)
1073 (format " secret keys unchanged: %d\n"
1074 (epg-import-result-secret-unchanged import-result)))))
1076 (defun epg-error-to-string (error)
1077 (cond
1078 ((eq (car error) 'exit)
1079 "Exit")
1080 ((eq (car error) 'quit)
1081 "Cancelled")
1082 ((eq (car error) 'no-data)
1083 (let ((entry (assq (cdr error) epg-no-data-reason-alist)))
1084 (if entry
1085 (format "No data (%s)" (downcase (cdr entry)))
1086 "No data")))
1087 ((eq (car error) 'unexpected)
1088 (let ((entry (assq (cdr error) epg-unexpected-reason-alist)))
1089 (if entry
1090 (format "Unexpected (%s)" (downcase (cdr entry)))
1091 "Unexpected")))
1092 ((eq (car error) 'bad-armor)
1093 "Bad armor")
1094 ((memq (car error) '(invalid-recipient invalid-signer))
1095 (concat
1096 (if (eq (car error) 'invalid-recipient)
1097 "Unusable public key"
1098 "Unusable secret key")
1099 (let ((entry (assq 'requested (cdr error))))
1100 (if entry
1101 (format ": %s" (cdr entry))
1102 ": <unknown>"))
1103 (let ((entry (assq 'reason (cdr error))))
1104 (if (and entry
1105 (> (cdr entry) 0) ;no specific reason given
1106 (setq entry (assq (cdr entry)
1107 epg-invalid-recipients-reason-alist)))
1108 (format " (%s)" (downcase (cdr entry)))
1109 ""))))
1110 ((eq (car error) 'no-pubkey)
1111 (format "No public key: %s" (cdr error)))
1112 ((eq (car error) 'no-seckey)
1113 (format "No secret key: %s" (cdr error)))
1114 ((eq (car error) 'no-recipients)
1115 "No recipients")
1116 ((eq (car error) 'no-signers)
1117 "No signers")
1118 ((eq (car error) 'delete-problem)
1119 (let ((entry (assq (cdr error) epg-delete-problem-reason-alist)))
1120 (if entry
1121 (format "Delete problem (%s)" (downcase (cdr entry)))
1122 "Delete problem")))
1123 ((eq (car error) 'key-not-created)
1124 "Key not created")))
1126 (defun epg-errors-to-string (errors)
1127 (mapconcat #'epg-error-to-string errors "; "))
1129 (defun epg--start (context args)
1130 "Start `epg-gpg-program' in a subprocess with given ARGS."
1131 (if (and (epg-context-process context)
1132 (eq (process-status (epg-context-process context)) 'run))
1133 (error "%s is already running in this context"
1134 (if (eq (epg-context-protocol context) 'CMS)
1135 epg-gpgsm-program
1136 epg-gpg-program)))
1137 (let* ((agent-info (getenv "GPG_AGENT_INFO"))
1138 (args (append (list "--no-tty"
1139 "--status-fd" "1"
1140 "--yes")
1141 (if (and (not (eq (epg-context-protocol context) 'CMS))
1142 (string-match ":" (or agent-info "")))
1143 '("--use-agent"))
1144 (if (and (not (eq (epg-context-protocol context) 'CMS))
1145 (epg-context-progress-callback context))
1146 '("--enable-progress-filter"))
1147 (if epg-gpg-home-directory
1148 (list "--homedir" epg-gpg-home-directory))
1149 (unless (eq (epg-context-protocol context) 'CMS)
1150 '("--command-fd" "0"))
1151 (if (epg-context-armor context) '("--armor"))
1152 (if (epg-context-textmode context) '("--textmode"))
1153 (if (epg-context-output-file context)
1154 (list "--output" (epg-context-output-file context)))
1155 args))
1156 (coding-system-for-write 'binary)
1157 (coding-system-for-read 'binary)
1158 process-connection-type
1159 (process-environment process-environment)
1160 (orig-mode (default-file-modes))
1161 (buffer (generate-new-buffer " *epg*"))
1162 process
1163 terminal-name
1164 agent-file
1165 (agent-mtime '(0 0 0 0)))
1166 ;; Set GPG_TTY and TERM for pinentry-curses. Note that we can't
1167 ;; use `terminal-name' here to get the real pty name for the child
1168 ;; process, though /dev/fd/0" is not portable.
1169 (unless (memq system-type '(ms-dos windows-nt))
1170 (with-temp-buffer
1171 (condition-case nil
1172 (when (= (call-process "tty" "/dev/fd/0" t) 0)
1173 (delete-char -1)
1174 (setq terminal-name (buffer-string)))
1175 (file-error))))
1176 (when terminal-name
1177 (setq process-environment
1178 (cons (concat "GPG_TTY=" terminal-name)
1179 (cons "TERM=xterm" process-environment))))
1180 ;; Record modified time of gpg-agent socket to restore the Emacs
1181 ;; frame on text terminal in `epg-wait-for-completion'.
1182 ;; See
1183 ;; <http://lists.gnu.org/archive/html/emacs-devel/2007-02/msg00755.html>
1184 ;; for more details.
1185 (when (and agent-info (string-match "\\(.*\\):[0-9]+:[0-9]+" agent-info))
1186 (setq agent-file (match-string 1 agent-info)
1187 agent-mtime (or (nth 5 (file-attributes agent-file)) '(0 0 0 0))))
1188 (if epg-debug
1189 (save-excursion
1190 (unless epg-debug-buffer
1191 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
1192 (set-buffer epg-debug-buffer)
1193 (goto-char (point-max))
1194 (insert (if agent-info
1195 (format "GPG_AGENT_INFO=%s\n" agent-info)
1196 "GPG_AGENT_INFO is not set\n")
1197 (format "%s %s\n"
1198 (if (eq (epg-context-protocol context) 'CMS)
1199 epg-gpgsm-program
1200 epg-gpg-program)
1201 (mapconcat #'identity args " ")))))
1202 (with-current-buffer buffer
1203 (if (fboundp 'set-buffer-multibyte)
1204 (set-buffer-multibyte nil))
1205 (make-local-variable 'epg-last-status)
1206 (setq epg-last-status nil)
1207 (make-local-variable 'epg-read-point)
1208 (setq epg-read-point (point-min))
1209 (make-local-variable 'epg-process-filter-running)
1210 (setq epg-process-filter-running nil)
1211 (make-local-variable 'epg-pending-status-list)
1212 (setq epg-pending-status-list nil)
1213 (make-local-variable 'epg-key-id)
1214 (setq epg-key-id nil)
1215 (make-local-variable 'epg-context)
1216 (setq epg-context context)
1217 (make-local-variable 'epg-agent-file)
1218 (setq epg-agent-file agent-file)
1219 (make-local-variable 'epg-agent-mtime)
1220 (setq epg-agent-mtime agent-mtime))
1221 (unwind-protect
1222 (progn
1223 (set-default-file-modes 448)
1224 (setq process
1225 (apply #'start-process "epg" buffer
1226 (if (eq (epg-context-protocol context) 'CMS)
1227 epg-gpgsm-program
1228 epg-gpg-program)
1229 args)))
1230 (set-default-file-modes orig-mode))
1231 (set-process-filter process #'epg--process-filter)
1232 (epg-context-set-process context process)))
1234 (defun epg--process-filter (process input)
1235 (if epg-debug
1236 (save-excursion
1237 (unless epg-debug-buffer
1238 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
1239 (set-buffer epg-debug-buffer)
1240 (goto-char (point-max))
1241 (insert input)))
1242 (if (buffer-live-p (process-buffer process))
1243 (with-current-buffer (process-buffer process)
1244 (goto-char (point-max))
1245 (insert input)
1246 (unless epg-process-filter-running
1247 (unwind-protect
1248 (progn
1249 (setq epg-process-filter-running t)
1250 (goto-char epg-read-point)
1251 (beginning-of-line)
1252 (while (looking-at ".*\n") ;the input line finished
1253 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
1254 (let* ((status (match-string 1))
1255 (string (match-string 2))
1256 (symbol (intern-soft (concat "epg--status-"
1257 status))))
1258 (if (member status epg-pending-status-list)
1259 (setq epg-pending-status-list nil))
1260 (if (and symbol
1261 (fboundp symbol))
1262 (funcall symbol epg-context string))
1263 (setq epg-last-status (cons status string))))
1264 (forward-line)
1265 (setq epg-read-point (point))))
1266 (setq epg-process-filter-running nil))))))
1268 (defun epg-read-output (context)
1269 "Read the output file CONTEXT and return the content as a string."
1270 (with-temp-buffer
1271 (if (fboundp 'set-buffer-multibyte)
1272 (set-buffer-multibyte nil))
1273 (if (file-exists-p (epg-context-output-file context))
1274 (let ((coding-system-for-read 'binary))
1275 (insert-file-contents (epg-context-output-file context))
1276 (buffer-string)))))
1278 (defun epg-wait-for-status (context status-list)
1279 "Wait until one of elements in STATUS-LIST arrives."
1280 (with-current-buffer (process-buffer (epg-context-process context))
1281 (setq epg-pending-status-list status-list)
1282 (while (and (eq (process-status (epg-context-process context)) 'run)
1283 epg-pending-status-list)
1284 (accept-process-output (epg-context-process context) 1))
1285 (if epg-pending-status-list
1286 (epg-context-set-result-for
1287 context 'error
1288 (cons '(exit)
1289 (epg-context-result-for context 'error))))))
1291 (defun epg-wait-for-completion (context)
1292 "Wait until the `epg-gpg-program' process completes."
1293 (while (eq (process-status (epg-context-process context)) 'run)
1294 (accept-process-output (epg-context-process context) 1))
1295 ;; This line is needed to run the process-filter right now.
1296 (sleep-for 0.1)
1297 ;; Restore Emacs frame on text terminal, when pinentry-curses has terminated.
1298 (if (with-current-buffer (process-buffer (epg-context-process context))
1299 (and epg-agent-file
1300 (> (float-time (or (nth 5 (file-attributes epg-agent-file))
1301 '(0 0 0 0)))
1302 (float-time epg-agent-mtime))))
1303 (redraw-frame (selected-frame)))
1304 (epg-context-set-result-for
1305 context 'error
1306 (nreverse (epg-context-result-for context 'error))))
1308 (defun epg-reset (context)
1309 "Reset the CONTEXT."
1310 (if (and (epg-context-process context)
1311 (buffer-live-p (process-buffer (epg-context-process context))))
1312 (kill-buffer (process-buffer (epg-context-process context))))
1313 (epg-context-set-process context nil))
1315 (defun epg-delete-output-file (context)
1316 "Delete the output file of CONTEXT."
1317 (if (and (epg-context-output-file context)
1318 (file-exists-p (epg-context-output-file context)))
1319 (delete-file (epg-context-output-file context))))
1321 (eval-and-compile
1322 (if (fboundp 'decode-coding-string)
1323 (defalias 'epg--decode-coding-string 'decode-coding-string)
1324 (defalias 'epg--decode-coding-string 'identity)))
1326 (defun epg--status-USERID_HINT (_context string)
1327 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1328 (let* ((key-id (match-string 1 string))
1329 (user-id (match-string 2 string))
1330 (entry (assoc key-id epg-user-id-alist)))
1331 (condition-case nil
1332 (setq user-id (epg--decode-coding-string
1333 (epg--decode-percent-escape user-id)
1334 'utf-8))
1335 (error))
1336 (if entry
1337 (setcdr entry user-id)
1338 (setq epg-user-id-alist (cons (cons key-id user-id)
1339 epg-user-id-alist))))))
1341 (defun epg--status-NEED_PASSPHRASE (_context string)
1342 (if (string-match "\\`\\([^ ]+\\)" string)
1343 (setq epg-key-id (match-string 1 string))))
1345 (defun epg--status-NEED_PASSPHRASE_SYM (_context _string)
1346 (setq epg-key-id 'SYM))
1348 (defun epg--status-NEED_PASSPHRASE_PIN (_context _string)
1349 (setq epg-key-id 'PIN))
1351 (eval-and-compile
1352 (if (fboundp 'clear-string)
1353 (defalias 'epg--clear-string 'clear-string)
1354 (defun epg--clear-string (string)
1355 (fillarray string 0))))
1357 (eval-and-compile
1358 (if (fboundp 'encode-coding-string)
1359 (defalias 'epg--encode-coding-string 'encode-coding-string)
1360 (defalias 'epg--encode-coding-string 'identity)))
1362 (defun epg--status-GET_HIDDEN (context string)
1363 (when (and epg-key-id
1364 (string-match "\\`passphrase\\." string))
1365 (unless (epg-context-passphrase-callback context)
1366 (error "passphrase-callback not set"))
1367 (let (inhibit-quit
1368 passphrase
1369 passphrase-with-new-line
1370 encoded-passphrase-with-new-line)
1371 (unwind-protect
1372 (condition-case nil
1373 (progn
1374 (setq passphrase
1375 (funcall
1376 (car (epg-context-passphrase-callback context))
1377 context
1378 epg-key-id
1379 (cdr (epg-context-passphrase-callback context))))
1380 (when passphrase
1381 (setq passphrase-with-new-line (concat passphrase "\n"))
1382 (epg--clear-string passphrase)
1383 (setq passphrase nil)
1384 (if epg-passphrase-coding-system
1385 (progn
1386 (setq encoded-passphrase-with-new-line
1387 (epg--encode-coding-string
1388 passphrase-with-new-line
1389 (coding-system-change-eol-conversion
1390 epg-passphrase-coding-system 'unix)))
1391 (epg--clear-string passphrase-with-new-line)
1392 (setq passphrase-with-new-line nil))
1393 (setq encoded-passphrase-with-new-line
1394 passphrase-with-new-line
1395 passphrase-with-new-line nil))
1396 (process-send-string (epg-context-process context)
1397 encoded-passphrase-with-new-line)))
1398 (quit
1399 (epg-context-set-result-for
1400 context 'error
1401 (cons '(quit)
1402 (epg-context-result-for context 'error)))
1403 (delete-process (epg-context-process context))))
1404 (if passphrase
1405 (epg--clear-string passphrase))
1406 (if passphrase-with-new-line
1407 (epg--clear-string passphrase-with-new-line))
1408 (if encoded-passphrase-with-new-line
1409 (epg--clear-string encoded-passphrase-with-new-line))))))
1411 (defun epg--prompt-GET_BOOL (_context string)
1412 (let ((entry (assoc string epg-prompt-alist)))
1413 (y-or-n-p (if entry (cdr entry) (concat string "? ")))))
1415 (defun epg--prompt-GET_BOOL-untrusted_key.override (_context _string)
1416 (y-or-n-p (if (and (equal (car epg-last-status) "USERID_HINT")
1417 (string-match "\\`\\([^ ]+\\) \\(.*\\)"
1418 (cdr epg-last-status)))
1419 (let* ((key-id (match-string 1 (cdr epg-last-status)))
1420 (user-id (match-string 2 (cdr epg-last-status)))
1421 (entry (assoc key-id epg-user-id-alist)))
1422 (if entry
1423 (setq user-id (cdr entry)))
1424 (format "Untrusted key %s %s. Use anyway? " key-id user-id))
1425 "Use untrusted key anyway? ")))
1427 (defun epg--status-GET_BOOL (context string)
1428 (let (inhibit-quit)
1429 (condition-case nil
1430 (if (funcall (or (intern-soft (concat "epg--prompt-GET_BOOL-" string))
1431 #'epg--prompt-GET_BOOL)
1432 context string)
1433 (process-send-string (epg-context-process context) "y\n")
1434 (process-send-string (epg-context-process context) "n\n"))
1435 (quit
1436 (epg-context-set-result-for
1437 context 'error
1438 (cons '(quit)
1439 (epg-context-result-for context 'error)))
1440 (delete-process (epg-context-process context))))))
1442 (defun epg--status-GET_LINE (context string)
1443 (let ((entry (assoc string epg-prompt-alist))
1444 inhibit-quit)
1445 (condition-case nil
1446 (process-send-string (epg-context-process context)
1447 (concat (read-string
1448 (if entry
1449 (cdr entry)
1450 (concat string ": ")))
1451 "\n"))
1452 (quit
1453 (epg-context-set-result-for
1454 context 'error
1455 (cons '(quit)
1456 (epg-context-result-for context 'error)))
1457 (delete-process (epg-context-process context))))))
1459 (defun epg--status-*SIG (context status string)
1460 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1461 (let* ((key-id (match-string 1 string))
1462 (user-id (match-string 2 string))
1463 (entry (assoc key-id epg-user-id-alist)))
1464 (epg-context-set-result-for
1465 context
1466 'verify
1467 (cons (epg-make-signature status key-id)
1468 (epg-context-result-for context 'verify)))
1469 (condition-case nil
1470 (if (eq (epg-context-protocol context) 'CMS)
1471 (setq user-id (epg-dn-from-string user-id))
1472 (setq user-id (epg--decode-coding-string
1473 (epg--decode-percent-escape user-id)
1474 'utf-8)))
1475 (error))
1476 (if entry
1477 (setcdr entry user-id)
1478 (setq epg-user-id-alist
1479 (cons (cons key-id user-id) epg-user-id-alist))))
1480 (epg-context-set-result-for
1481 context
1482 'verify
1483 (cons (epg-make-signature status)
1484 (epg-context-result-for context 'verify)))))
1486 (defun epg--status-GOODSIG (context string)
1487 (epg--status-*SIG context 'good string))
1489 (defun epg--status-EXPSIG (context string)
1490 (epg--status-*SIG context 'expired string))
1492 (defun epg--status-EXPKEYSIG (context string)
1493 (epg--status-*SIG context 'expired-key string))
1495 (defun epg--status-REVKEYSIG (context string)
1496 (epg--status-*SIG context 'revoked-key string))
1498 (defun epg--status-BADSIG (context string)
1499 (epg--status-*SIG context 'bad string))
1501 (defun epg--status-NO_PUBKEY (context string)
1502 (if (eq (epg-context-operation context) 'verify)
1503 (let ((signature (car (epg-context-result-for context 'verify))))
1504 (if (and signature
1505 (eq (epg-signature-status signature) 'error)
1506 (equal (epg-signature-key-id signature) string))
1507 (epg-signature-set-status signature 'no-pubkey)))
1508 (epg-context-set-result-for
1509 context 'error
1510 (cons (cons 'no-pubkey string)
1511 (epg-context-result-for context 'error)))))
1513 (defun epg--status-NO_SECKEY (context string)
1514 (epg-context-set-result-for
1515 context 'error
1516 (cons (cons 'no-seckey string)
1517 (epg-context-result-for context 'error))))
1519 (defun epg--time-from-seconds (seconds)
1520 (let ((number-seconds (string-to-number (concat seconds ".0"))))
1521 (cons (floor (/ number-seconds 65536))
1522 (floor (mod number-seconds 65536)))))
1524 (defun epg--status-ERRSIG (context string)
1525 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1526 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
1527 string)
1528 (let ((signature (epg-make-signature 'error)))
1529 (epg-context-set-result-for
1530 context
1531 'verify
1532 (cons signature
1533 (epg-context-result-for context 'verify)))
1534 (epg-signature-set-key-id
1535 signature
1536 (match-string 1 string))
1537 (epg-signature-set-pubkey-algorithm
1538 signature
1539 (string-to-number (match-string 2 string)))
1540 (epg-signature-set-digest-algorithm
1541 signature
1542 (string-to-number (match-string 3 string)))
1543 (epg-signature-set-class
1544 signature
1545 (string-to-number (match-string 4 string) 16))
1546 (epg-signature-set-creation-time
1547 signature
1548 (epg--time-from-seconds (match-string 5 string))))))
1550 (defun epg--status-VALIDSIG (context string)
1551 (let ((signature (car (epg-context-result-for context 'verify))))
1552 (when (and signature
1553 (eq (epg-signature-status signature) 'good)
1554 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
1555 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
1556 \\(.*\\)"
1557 string))
1558 (epg-signature-set-fingerprint
1559 signature
1560 (match-string 1 string))
1561 (epg-signature-set-creation-time
1562 signature
1563 (epg--time-from-seconds (match-string 2 string)))
1564 (unless (equal (match-string 3 string) "0")
1565 (epg-signature-set-expiration-time
1566 signature
1567 (epg--time-from-seconds (match-string 3 string))))
1568 (epg-signature-set-version
1569 signature
1570 (string-to-number (match-string 4 string)))
1571 (epg-signature-set-pubkey-algorithm
1572 signature
1573 (string-to-number (match-string 5 string)))
1574 (epg-signature-set-digest-algorithm
1575 signature
1576 (string-to-number (match-string 6 string)))
1577 (epg-signature-set-class
1578 signature
1579 (string-to-number (match-string 7 string) 16)))))
1581 (defun epg--status-TRUST_UNDEFINED (context _string)
1582 (let ((signature (car (epg-context-result-for context 'verify))))
1583 (if (and signature
1584 (eq (epg-signature-status signature) 'good))
1585 (epg-signature-set-validity signature 'undefined))))
1587 (defun epg--status-TRUST_NEVER (context _string)
1588 (let ((signature (car (epg-context-result-for context 'verify))))
1589 (if (and signature
1590 (eq (epg-signature-status signature) 'good))
1591 (epg-signature-set-validity signature 'never))))
1593 (defun epg--status-TRUST_MARGINAL (context _string)
1594 (let ((signature (car (epg-context-result-for context 'verify))))
1595 (if (and signature
1596 (eq (epg-signature-status signature) 'marginal))
1597 (epg-signature-set-validity signature 'marginal))))
1599 (defun epg--status-TRUST_FULLY (context _string)
1600 (let ((signature (car (epg-context-result-for context 'verify))))
1601 (if (and signature
1602 (eq (epg-signature-status signature) 'good))
1603 (epg-signature-set-validity signature 'full))))
1605 (defun epg--status-TRUST_ULTIMATE (context _string)
1606 (let ((signature (car (epg-context-result-for context 'verify))))
1607 (if (and signature
1608 (eq (epg-signature-status signature) 'good))
1609 (epg-signature-set-validity signature 'ultimate))))
1611 (defun epg--status-NOTATION_NAME (context string)
1612 (let ((signature (car (epg-context-result-for context 'verify))))
1613 (if signature
1614 (epg-signature-set-notations
1615 signature
1616 (cons (epg-make-sig-notation string nil t nil)
1617 (epg-sig-notations signature))))))
1619 (defun epg--status-NOTATION_DATA (context string)
1620 (let ((signature (car (epg-context-result-for context 'verify)))
1621 notation)
1622 (if (and signature
1623 (setq notation (car (epg-sig-notations signature))))
1624 (epg-sig-notation-set-value notation string))))
1626 (defun epg--status-POLICY_URL (context string)
1627 (let ((signature (car (epg-context-result-for context 'verify))))
1628 (if signature
1629 (epg-signature-set-notations
1630 signature
1631 (cons (epg-make-sig-notation nil string t nil)
1632 (epg-sig-notations signature))))))
1634 (defun epg--status-PROGRESS (context string)
1635 (if (and (epg-context-progress-callback context)
1636 (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1637 string))
1638 (funcall (car (epg-context-progress-callback context))
1639 context
1640 (match-string 1 string)
1641 (match-string 2 string)
1642 (string-to-number (match-string 3 string))
1643 (string-to-number (match-string 4 string))
1644 (cdr (epg-context-progress-callback context)))))
1646 (defun epg--status-ENC_TO (context string)
1647 (if (string-match "\\`\\([0-9A-Za-z]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
1648 (epg-context-set-result-for
1649 context 'encrypted-to
1650 (cons (list (match-string 1 string)
1651 (string-to-number (match-string 2 string))
1652 (string-to-number (match-string 3 string)))
1653 (epg-context-result-for context 'encrypted-to)))))
1655 (defun epg--status-DECRYPTION_FAILED (context _string)
1656 (epg-context-set-result-for context 'decryption-failed t))
1658 (defun epg--status-DECRYPTION_OKAY (context _string)
1659 (epg-context-set-result-for context 'decryption-okay t))
1661 (defun epg--status-NODATA (context string)
1662 (epg-context-set-result-for
1663 context 'error
1664 (cons (cons 'no-data (string-to-number string))
1665 (epg-context-result-for context 'error))))
1667 (defun epg--status-UNEXPECTED (context string)
1668 (epg-context-set-result-for
1669 context 'error
1670 (cons (cons 'unexpected (string-to-number string))
1671 (epg-context-result-for context 'error))))
1673 (defun epg--status-KEYEXPIRED (context string)
1674 (epg-context-set-result-for
1675 context 'key
1676 (cons (list 'key-expired (cons 'expiration-time
1677 (epg--time-from-seconds string)))
1678 (epg-context-result-for context 'key))))
1680 (defun epg--status-KEYREVOKED (context _string)
1681 (epg-context-set-result-for
1682 context 'key
1683 (cons '(key-revoked)
1684 (epg-context-result-for context 'key))))
1686 (defun epg--status-BADARMOR (context _string)
1687 (epg-context-set-result-for
1688 context 'error
1689 (cons '(bad-armor)
1690 (epg-context-result-for context 'error))))
1692 (defun epg--status-INV_RECP (context string)
1693 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1694 (epg-context-set-result-for
1695 context 'error
1696 (cons (list 'invalid-recipient
1697 (cons 'reason
1698 (string-to-number (match-string 1 string)))
1699 (cons 'requested
1700 (match-string 2 string)))
1701 (epg-context-result-for context 'error)))))
1703 (defun epg--status-INV_SGNR (context string)
1704 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1705 (epg-context-set-result-for
1706 context 'error
1707 (cons (list 'invalid-signer
1708 (cons 'reason
1709 (string-to-number (match-string 1 string)))
1710 (cons 'requested
1711 (match-string 2 string)))
1712 (epg-context-result-for context 'error)))))
1714 (defun epg--status-NO_RECP (context _string)
1715 (epg-context-set-result-for
1716 context 'error
1717 (cons '(no-recipients)
1718 (epg-context-result-for context 'error))))
1720 (defun epg--status-NO_SGNR (context _string)
1721 (epg-context-set-result-for
1722 context 'error
1723 (cons '(no-signers)
1724 (epg-context-result-for context 'error))))
1726 (defun epg--status-DELETE_PROBLEM (context string)
1727 (if (string-match "\\`\\([0-9]+\\)" string)
1728 (epg-context-set-result-for
1729 context 'error
1730 (cons (cons 'delete-problem
1731 (string-to-number (match-string 1 string)))
1732 (epg-context-result-for context 'error)))))
1734 (defun epg--status-SIG_CREATED (context string)
1735 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1736 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1737 (epg-context-set-result-for
1738 context 'sign
1739 (cons (epg-make-new-signature
1740 (cdr (assq (aref (match-string 1 string) 0)
1741 epg-new-signature-type-alist))
1742 (string-to-number (match-string 2 string))
1743 (string-to-number (match-string 3 string))
1744 (string-to-number (match-string 4 string) 16)
1745 (epg--time-from-seconds (match-string 5 string))
1746 (substring string (match-end 0)))
1747 (epg-context-result-for context 'sign)))))
1749 (defun epg--status-KEY_CREATED (context string)
1750 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1751 (epg-context-set-result-for
1752 context 'generate-key
1753 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1754 (cons 'fingerprint (match-string 2 string)))
1755 (epg-context-result-for context 'generate-key)))))
1757 (defun epg--status-KEY_NOT_CREATED (context _string)
1758 (epg-context-set-result-for
1759 context 'error
1760 (cons '(key-not-created)
1761 (epg-context-result-for context 'error))))
1763 (defun epg--status-IMPORTED (_context string)
1764 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1765 (let* ((key-id (match-string 1 string))
1766 (user-id (match-string 2 string))
1767 (entry (assoc key-id epg-user-id-alist)))
1768 (condition-case nil
1769 (setq user-id (epg--decode-coding-string
1770 (epg--decode-percent-escape user-id)
1771 'utf-8))
1772 (error))
1773 (if entry
1774 (setcdr entry user-id)
1775 (setq epg-user-id-alist (cons (cons key-id user-id)
1776 epg-user-id-alist))))))
1778 (defun epg--status-IMPORT_OK (context string)
1779 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1780 (let ((reason (string-to-number (match-string 1 string))))
1781 (epg-context-set-result-for
1782 context 'import-status
1783 (cons (epg-make-import-status (if (match-beginning 2)
1784 (match-string 3 string))
1786 (/= (logand reason 1) 0)
1787 (/= (logand reason 2) 0)
1788 (/= (logand reason 4) 0)
1789 (/= (logand reason 8) 0)
1790 (/= (logand reason 16) 0))
1791 (epg-context-result-for context 'import-status))))))
1793 (defun epg--status-IMPORT_PROBLEM (context string)
1794 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1795 (epg-context-set-result-for
1796 context 'import-status
1797 (cons (epg-make-import-status
1798 (if (match-beginning 2)
1799 (match-string 3 string))
1800 (string-to-number (match-string 1 string)))
1801 (epg-context-result-for context 'import-status)))))
1803 (defun epg--status-IMPORT_RES (context string)
1804 (when (string-match "\\`\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1805 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1806 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
1807 (epg-context-set-result-for
1808 context 'import
1809 (epg-make-import-result (string-to-number (match-string 1 string))
1810 (string-to-number (match-string 2 string))
1811 (string-to-number (match-string 3 string))
1812 (string-to-number (match-string 4 string))
1813 (string-to-number (match-string 5 string))
1814 (string-to-number (match-string 6 string))
1815 (string-to-number (match-string 7 string))
1816 (string-to-number (match-string 8 string))
1817 (string-to-number (match-string 9 string))
1818 (string-to-number (match-string 10 string))
1819 (string-to-number (match-string 11 string))
1820 (string-to-number (match-string 12 string))
1821 (string-to-number (match-string 13 string))
1822 (epg-context-result-for context 'import-status)))
1823 (epg-context-set-result-for context 'import-status nil)))
1825 (defun epg-passphrase-callback-function (context key-id _handback)
1826 (declare (obsolete epa-passphrase-callback-function "23.1"))
1827 (if (eq key-id 'SYM)
1828 (read-passwd "Passphrase for symmetric encryption: "
1829 (eq (epg-context-operation context) 'encrypt))
1830 (read-passwd
1831 (if (eq key-id 'PIN)
1832 "Passphrase for PIN: "
1833 (let ((entry (assoc key-id epg-user-id-alist)))
1834 (if entry
1835 (format "Passphrase for %s %s: " key-id (cdr entry))
1836 (format "Passphrase for %s: " key-id)))))))
1838 (defun epg--list-keys-1 (context name mode)
1839 (let ((args (append (if epg-gpg-home-directory
1840 (list "--homedir" epg-gpg-home-directory))
1841 '("--with-colons" "--no-greeting" "--batch"
1842 "--with-fingerprint" "--with-fingerprint")
1843 (unless (eq (epg-context-protocol context) 'CMS)
1844 '("--fixed-list-mode"))))
1845 (list-keys-option (if (memq mode '(t secret))
1846 "--list-secret-keys"
1847 (if (memq mode '(nil public))
1848 "--list-keys"
1849 "--list-sigs")))
1850 (coding-system-for-read 'binary)
1851 keys string field index)
1852 (if name
1853 (progn
1854 (unless (listp name)
1855 (setq name (list name)))
1856 (while name
1857 (setq args (append args (list list-keys-option (car name)))
1858 name (cdr name))))
1859 (setq args (append args (list list-keys-option))))
1860 (with-temp-buffer
1861 (apply #'call-process
1862 (if (eq (epg-context-protocol context) 'CMS)
1863 epg-gpgsm-program
1864 epg-gpg-program)
1865 nil (list t nil) nil args)
1866 (goto-char (point-min))
1867 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1868 (setq keys (cons (make-vector 15 nil) keys)
1869 string (match-string 0)
1870 index 0
1871 field 0)
1872 (while (eq index
1873 (string-match "\\([^:]+\\)?:" string index))
1874 (setq index (match-end 0))
1875 (aset (car keys) field (match-string 1 string))
1876 (setq field (1+ field))))
1877 (nreverse keys))))
1879 (defun epg--make-sub-key-1 (line)
1880 (epg-make-sub-key
1881 (if (aref line 1)
1882 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1883 (delq nil
1884 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1885 (aref line 11)))
1886 (member (aref line 0) '("sec" "ssb"))
1887 (string-to-number (aref line 3))
1888 (string-to-number (aref line 2))
1889 (aref line 4)
1890 (epg--time-from-seconds (aref line 5))
1891 (if (aref line 6)
1892 (epg--time-from-seconds (aref line 6)))))
1894 (defun epg-list-keys (context &optional name mode)
1895 "Return a list of epg-key objects matched with NAME.
1896 If MODE is nil or 'public, only public keyring should be searched.
1897 If MODE is t or 'secret, only secret keyring should be searched.
1898 Otherwise, only public keyring should be searched and the key
1899 signatures should be included.
1900 NAME is either a string or a list of strings."
1901 (let ((lines (epg--list-keys-1 context name mode))
1902 keys cert pointer pointer-1 index string)
1903 (while lines
1904 (cond
1905 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1906 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1907 keys (cons (epg-make-key
1908 (if (aref (car lines) 8)
1909 (cdr (assq (string-to-char (aref (car lines) 8))
1910 epg-key-validity-alist))))
1911 keys))
1912 (epg-key-set-sub-key-list
1913 (car keys)
1914 (cons (epg--make-sub-key-1 (car lines))
1915 (epg-key-sub-key-list (car keys)))))
1916 ((member (aref (car lines) 0) '("sub" "ssb"))
1917 (epg-key-set-sub-key-list
1918 (car keys)
1919 (cons (epg--make-sub-key-1 (car lines))
1920 (epg-key-sub-key-list (car keys)))))
1921 ((equal (aref (car lines) 0) "uid")
1922 ;; Decode the UID name as a backslash escaped UTF-8 string,
1923 ;; generated by GnuPG/GpgSM.
1924 (setq string (copy-sequence (aref (car lines) 9))
1925 index 0)
1926 (while (string-match "\"" string index)
1927 (setq string (replace-match "\\\"" t t string)
1928 index (1+ (match-end 0))))
1929 (condition-case nil
1930 (setq string (epg--decode-coding-string
1931 (car (read-from-string (concat "\"" string "\"")))
1932 'utf-8))
1933 (error
1934 (setq string (aref (car lines) 9))))
1935 (epg-key-set-user-id-list
1936 (car keys)
1937 (cons (epg-make-user-id
1938 (if (aref (car lines) 1)
1939 (cdr (assq (string-to-char (aref (car lines) 1))
1940 epg-key-validity-alist)))
1941 (if cert
1942 (condition-case nil
1943 (epg-dn-from-string string)
1944 (error string))
1945 string))
1946 (epg-key-user-id-list (car keys)))))
1947 ((equal (aref (car lines) 0) "fpr")
1948 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1949 (aref (car lines) 9)))
1950 ((equal (aref (car lines) 0) "sig")
1951 (epg-user-id-set-signature-list
1952 (car (epg-key-user-id-list (car keys)))
1953 (cons
1954 (epg-make-key-signature
1955 (if (aref (car lines) 1)
1956 (cdr (assq (string-to-char (aref (car lines) 1))
1957 epg-key-validity-alist)))
1958 (string-to-number (aref (car lines) 3))
1959 (aref (car lines) 4)
1960 (epg--time-from-seconds (aref (car lines) 5))
1961 (epg--time-from-seconds (aref (car lines) 6))
1962 (aref (car lines) 9)
1963 (string-to-number (aref (car lines) 10) 16)
1964 (eq (aref (aref (car lines) 10) 2) ?x))
1965 (epg-user-id-signature-list
1966 (car (epg-key-user-id-list (car keys))))))))
1967 (setq lines (cdr lines)))
1968 (setq keys (nreverse keys)
1969 pointer keys)
1970 (while pointer
1971 (epg-key-set-sub-key-list
1972 (car pointer)
1973 (nreverse (epg-key-sub-key-list (car pointer))))
1974 (setq pointer-1 (epg-key-set-user-id-list
1975 (car pointer)
1976 (nreverse (epg-key-user-id-list (car pointer)))))
1977 (while pointer-1
1978 (epg-user-id-set-signature-list
1979 (car pointer-1)
1980 (nreverse (epg-user-id-signature-list (car pointer-1))))
1981 (setq pointer-1 (cdr pointer-1)))
1982 (setq pointer (cdr pointer)))
1983 keys))
1985 (eval-and-compile
1986 (if (fboundp 'make-temp-file)
1987 (defalias 'epg--make-temp-file 'make-temp-file)
1988 (defvar temporary-file-directory)
1989 ;; stolen from poe.el.
1990 (defun epg--make-temp-file (prefix)
1991 "Create a temporary file.
1992 The returned file name (created by appending some random characters at the end
1993 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1994 is guaranteed to point to a newly created empty file.
1995 You can then use `write-region' to write new data into the file."
1996 (let ((orig-modes (default-file-modes))
1997 tempdir tempfile)
1998 (setq prefix (expand-file-name prefix
1999 (if (featurep 'xemacs)
2000 (temp-directory)
2001 temporary-file-directory)))
2002 (unwind-protect
2003 (let (file)
2004 ;; First, create a temporary directory.
2005 (set-default-file-modes #o700)
2006 (while (condition-case ()
2007 (progn
2008 (setq tempdir (make-temp-name
2009 (concat
2010 (file-name-directory prefix)
2011 "DIR")))
2012 ;; return nil or signal an error.
2013 (make-directory tempdir))
2014 ;; let's try again.
2015 (file-already-exists t)))
2016 ;; Second, create a temporary file in the tempdir.
2017 ;; There *is* a race condition between `make-temp-name'
2018 ;; and `write-region', but we don't care it since we are
2019 ;; in a private directory now.
2020 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
2021 (write-region "" nil tempfile nil 'silent)
2022 ;; Finally, make a hard-link from the tempfile.
2023 (while (condition-case ()
2024 (progn
2025 (setq file (make-temp-name prefix))
2026 ;; return nil or signal an error.
2027 (add-name-to-file tempfile file))
2028 ;; let's try again.
2029 (file-already-exists t)))
2030 file)
2031 (set-default-file-modes orig-modes)
2032 ;; Cleanup the tempfile.
2033 (and tempfile
2034 (file-exists-p tempfile)
2035 (delete-file tempfile))
2036 ;; Cleanup the tempdir.
2037 (and tempdir
2038 (file-directory-p tempdir)
2039 (delete-directory tempdir)))))))
2041 (defun epg--args-from-sig-notations (notations)
2042 (apply #'nconc
2043 (mapcar
2044 (lambda (notation)
2045 (if (and (epg-sig-notation-name notation)
2046 (not (epg-sig-notation-human-readable notation)))
2047 (error "Unreadable"))
2048 (if (epg-sig-notation-name notation)
2049 (list "--sig-notation"
2050 (if (epg-sig-notation-critical notation)
2051 (concat "!" (epg-sig-notation-name notation)
2052 "=" (epg-sig-notation-value notation))
2053 (concat (epg-sig-notation-name notation)
2054 "=" (epg-sig-notation-value notation))))
2055 (list "--sig-policy-url"
2056 (if (epg-sig-notation-critical notation)
2057 (concat "!" (epg-sig-notation-value notation))
2058 (epg-sig-notation-value notation)))))
2059 notations)))
2061 (defun epg-cancel (context)
2062 (if (buffer-live-p (process-buffer (epg-context-process context)))
2063 (with-current-buffer (process-buffer (epg-context-process context))
2064 (epg-context-set-result-for
2065 epg-context 'error
2066 (cons '(quit)
2067 (epg-context-result-for epg-context 'error)))))
2068 (if (eq (process-status (epg-context-process context)) 'run)
2069 (delete-process (epg-context-process context))))
2071 (defun epg-start-decrypt (context cipher)
2072 "Initiate a decrypt operation on CIPHER.
2073 CIPHER must be a file data object.
2075 If you use this function, you will need to wait for the completion of
2076 `epg-gpg-program' by using `epg-wait-for-completion' and call
2077 `epg-reset' to clear a temporary output file.
2078 If you are unsure, use synchronous version of this function
2079 `epg-decrypt-file' or `epg-decrypt-string' instead."
2080 (unless (epg-data-file cipher)
2081 (error "Not a file"))
2082 (epg-context-set-operation context 'decrypt)
2083 (epg-context-set-result context nil)
2084 (epg--start context (list "--decrypt" "--" (epg-data-file cipher)))
2085 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2086 (unless (eq (epg-context-protocol context) 'CMS)
2087 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
2089 (defun epg--check-error-for-decrypt (context)
2090 (let ((errors (epg-context-result-for context 'error)))
2091 (if (epg-context-result-for context 'decryption-failed)
2092 (signal 'epg-error
2093 (list "Decryption failed" (epg-errors-to-string errors))))
2094 (unless (epg-context-result-for context 'decryption-okay)
2095 (signal 'epg-error
2096 (list "Can't decrypt" (epg-errors-to-string errors))))))
2098 (defun epg-decrypt-file (context cipher plain)
2099 "Decrypt a file CIPHER and store the result to a file PLAIN.
2100 If PLAIN is nil, it returns the result as a string."
2101 (unwind-protect
2102 (progn
2103 (if plain
2104 (epg-context-set-output-file context plain)
2105 (epg-context-set-output-file context
2106 (epg--make-temp-file "epg-output")))
2107 (epg-start-decrypt context (epg-make-data-from-file cipher))
2108 (epg-wait-for-completion context)
2109 (epg--check-error-for-decrypt context)
2110 (unless plain
2111 (epg-read-output context)))
2112 (unless plain
2113 (epg-delete-output-file context))
2114 (epg-reset context)))
2116 (defun epg-decrypt-string (context cipher)
2117 "Decrypt a string CIPHER and return the plain text."
2118 (let ((input-file (epg--make-temp-file "epg-input"))
2119 (coding-system-for-write 'binary))
2120 (unwind-protect
2121 (progn
2122 (write-region cipher nil input-file nil 'quiet)
2123 (epg-context-set-output-file context
2124 (epg--make-temp-file "epg-output"))
2125 (epg-start-decrypt context (epg-make-data-from-file input-file))
2126 (epg-wait-for-completion context)
2127 (epg--check-error-for-decrypt context)
2128 (epg-read-output context))
2129 (epg-delete-output-file context)
2130 (if (file-exists-p input-file)
2131 (delete-file input-file))
2132 (epg-reset context))))
2134 (defun epg-start-verify (context signature &optional signed-text)
2135 "Initiate a verify operation on SIGNATURE.
2136 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
2138 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
2139 For a normal or a cleartext signature, SIGNED-TEXT should be nil.
2141 If you use this function, you will need to wait for the completion of
2142 `epg-gpg-program' by using `epg-wait-for-completion' and call
2143 `epg-reset' to clear a temporary output file.
2144 If you are unsure, use synchronous version of this function
2145 `epg-verify-file' or `epg-verify-string' instead."
2146 (epg-context-set-operation context 'verify)
2147 (epg-context-set-result context nil)
2148 (if signed-text
2149 ;; Detached signature.
2150 (if (epg-data-file signed-text)
2151 (epg--start context (list "--verify" "--" (epg-data-file signature)
2152 (epg-data-file signed-text)))
2153 (epg--start context (list "--verify" "--" (epg-data-file signature)
2154 "-"))
2155 (if (eq (process-status (epg-context-process context)) 'run)
2156 (process-send-string (epg-context-process context)
2157 (epg-data-string signed-text)))
2158 (if (eq (process-status (epg-context-process context)) 'run)
2159 (process-send-eof (epg-context-process context))))
2160 ;; Normal (or cleartext) signature.
2161 (if (epg-data-file signature)
2162 (epg--start context (if (eq (epg-context-protocol context) 'CMS)
2163 (list "--verify" "--" (epg-data-file signature))
2164 (list "--" (epg-data-file signature))))
2165 (epg--start context (if (eq (epg-context-protocol context) 'CMS)
2166 '("--verify" "-")
2167 '("-")))
2168 (if (eq (process-status (epg-context-process context)) 'run)
2169 (process-send-string (epg-context-process context)
2170 (epg-data-string signature)))
2171 (if (eq (process-status (epg-context-process context)) 'run)
2172 (process-send-eof (epg-context-process context))))))
2174 (defun epg-verify-file (context signature &optional signed-text plain)
2175 "Verify a file SIGNATURE.
2176 SIGNED-TEXT and PLAIN are also a file if they are specified.
2178 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
2179 string. For a normal or a cleartext signature, SIGNED-TEXT should be
2180 nil. In the latter case, if PLAIN is specified, the plaintext is
2181 stored into the file after successful verification."
2182 (unwind-protect
2183 (progn
2184 (if plain
2185 (epg-context-set-output-file context plain)
2186 (epg-context-set-output-file context
2187 (epg--make-temp-file "epg-output")))
2188 (if signed-text
2189 (epg-start-verify context
2190 (epg-make-data-from-file signature)
2191 (epg-make-data-from-file signed-text))
2192 (epg-start-verify context
2193 (epg-make-data-from-file signature)))
2194 (epg-wait-for-completion context)
2195 (unless plain
2196 (epg-read-output context)))
2197 (unless plain
2198 (epg-delete-output-file context))
2199 (epg-reset context)))
2201 (defun epg-verify-string (context signature &optional signed-text)
2202 "Verify a string SIGNATURE.
2203 SIGNED-TEXT is a string if it is specified.
2205 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
2206 string. For a normal or a cleartext signature, SIGNED-TEXT should be
2207 nil. In the latter case, this function returns the plaintext after
2208 successful verification."
2209 (let ((coding-system-for-write 'binary)
2210 input-file)
2211 (unwind-protect
2212 (progn
2213 (epg-context-set-output-file context
2214 (epg--make-temp-file "epg-output"))
2215 (if signed-text
2216 (progn
2217 (setq input-file (epg--make-temp-file "epg-signature"))
2218 (write-region signature nil input-file nil 'quiet)
2219 (epg-start-verify context
2220 (epg-make-data-from-file input-file)
2221 (epg-make-data-from-string signed-text)))
2222 (epg-start-verify context (epg-make-data-from-string signature)))
2223 (epg-wait-for-completion context)
2224 (epg-read-output context))
2225 (epg-delete-output-file context)
2226 (if (and input-file
2227 (file-exists-p input-file))
2228 (delete-file input-file))
2229 (epg-reset context))))
2231 (defun epg-start-sign (context plain &optional mode)
2232 "Initiate a sign operation on PLAIN.
2233 PLAIN is a data object.
2235 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2236 If it is nil or 'normal, it makes a normal signature.
2237 Otherwise, it makes a cleartext signature.
2239 If you use this function, you will need to wait for the completion of
2240 `epg-gpg-program' by using `epg-wait-for-completion' and call
2241 `epg-reset' to clear a temporary output file.
2242 If you are unsure, use synchronous version of this function
2243 `epg-sign-file' or `epg-sign-string' instead."
2244 (epg-context-set-operation context 'sign)
2245 (epg-context-set-result context nil)
2246 (unless (memq mode '(t detached nil normal)) ;i.e. cleartext
2247 (epg-context-set-armor context nil)
2248 (epg-context-set-textmode context nil))
2249 (epg--start context
2250 (append (list (if (memq mode '(t detached))
2251 "--detach-sign"
2252 (if (memq mode '(nil normal))
2253 "--sign"
2254 "--clearsign")))
2255 (apply #'nconc
2256 (mapcar
2257 (lambda (signer)
2258 (list "-u"
2259 (epg-sub-key-id
2260 (car (epg-key-sub-key-list signer)))))
2261 (epg-context-signers context)))
2262 (epg--args-from-sig-notations
2263 (epg-context-sig-notations context))
2264 (if (epg-data-file plain)
2265 (list "--" (epg-data-file plain)))))
2266 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2267 (unless (eq (epg-context-protocol context) 'CMS)
2268 (epg-wait-for-status context '("BEGIN_SIGNING")))
2269 (when (epg-data-string plain)
2270 (if (eq (process-status (epg-context-process context)) 'run)
2271 (process-send-string (epg-context-process context)
2272 (epg-data-string plain)))
2273 (if (eq (process-status (epg-context-process context)) 'run)
2274 (process-send-eof (epg-context-process context)))))
2276 (defun epg-sign-file (context plain signature &optional mode)
2277 "Sign a file PLAIN and store the result to a file SIGNATURE.
2278 If SIGNATURE is nil, it returns the result as a string.
2279 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2280 If it is nil or 'normal, it makes a normal signature.
2281 Otherwise, it makes a cleartext signature."
2282 (unwind-protect
2283 (progn
2284 (if signature
2285 (epg-context-set-output-file context signature)
2286 (epg-context-set-output-file context
2287 (epg--make-temp-file "epg-output")))
2288 (epg-start-sign context (epg-make-data-from-file plain) mode)
2289 (epg-wait-for-completion context)
2290 (unless (epg-context-result-for context 'sign)
2291 (let ((errors (epg-context-result-for context 'error)))
2292 (signal 'epg-error
2293 (list "Sign failed" (epg-errors-to-string errors)))))
2294 (unless signature
2295 (epg-read-output context)))
2296 (unless signature
2297 (epg-delete-output-file context))
2298 (epg-reset context)))
2300 (defun epg-sign-string (context plain &optional mode)
2301 "Sign a string PLAIN and return the output as string.
2302 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2303 If it is nil or 'normal, it makes a normal signature.
2304 Otherwise, it makes a cleartext signature."
2305 (let ((input-file
2306 (unless (or (eq (epg-context-protocol context) 'CMS)
2307 (condition-case nil
2308 (progn
2309 (epg-check-configuration (epg-configuration))
2311 (error)))
2312 (epg--make-temp-file "epg-input")))
2313 (coding-system-for-write 'binary))
2314 (unwind-protect
2315 (progn
2316 (epg-context-set-output-file context
2317 (epg--make-temp-file "epg-output"))
2318 (if input-file
2319 (write-region plain nil input-file nil 'quiet))
2320 (epg-start-sign context
2321 (if input-file
2322 (epg-make-data-from-file input-file)
2323 (epg-make-data-from-string plain))
2324 mode)
2325 (epg-wait-for-completion context)
2326 (unless (epg-context-result-for context 'sign)
2327 (if (epg-context-result-for context 'error)
2328 (let ((errors (epg-context-result-for context 'error)))
2329 (signal 'epg-error
2330 (list "Sign failed" (epg-errors-to-string errors))))))
2331 (epg-read-output context))
2332 (epg-delete-output-file context)
2333 (if input-file
2334 (delete-file input-file))
2335 (epg-reset context))))
2337 (defun epg-start-encrypt (context plain recipients
2338 &optional sign always-trust)
2339 "Initiate an encrypt operation on PLAIN.
2340 PLAIN is a data object.
2341 If RECIPIENTS is nil, it performs symmetric encryption.
2343 If you use this function, you will need to wait for the completion of
2344 `epg-gpg-program' by using `epg-wait-for-completion' and call
2345 `epg-reset' to clear a temporary output file.
2346 If you are unsure, use synchronous version of this function
2347 `epg-encrypt-file' or `epg-encrypt-string' instead."
2348 (epg-context-set-operation context 'encrypt)
2349 (epg-context-set-result context nil)
2350 (epg--start context
2351 (append (if always-trust '("--always-trust"))
2352 (if recipients '("--encrypt") '("--symmetric"))
2353 (if sign '("--sign"))
2354 (if sign
2355 (apply #'nconc
2356 (mapcar
2357 (lambda (signer)
2358 (list "-u"
2359 (epg-sub-key-id
2360 (car (epg-key-sub-key-list
2361 signer)))))
2362 (epg-context-signers context))))
2363 (if sign
2364 (epg--args-from-sig-notations
2365 (epg-context-sig-notations context)))
2366 (apply #'nconc
2367 (mapcar
2368 (lambda (recipient)
2369 (list "-r"
2370 (epg-sub-key-id
2371 (car (epg-key-sub-key-list recipient)))))
2372 recipients))
2373 (if (epg-data-file plain)
2374 (list "--" (epg-data-file plain)))))
2375 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2376 (unless (eq (epg-context-protocol context) 'CMS)
2377 (if sign
2378 (epg-wait-for-status context '("BEGIN_SIGNING"))
2379 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
2380 (when (epg-data-string plain)
2381 (if (eq (process-status (epg-context-process context)) 'run)
2382 (process-send-string (epg-context-process context)
2383 (epg-data-string plain)))
2384 (if (eq (process-status (epg-context-process context)) 'run)
2385 (process-send-eof (epg-context-process context)))))
2387 (defun epg-encrypt-file (context plain recipients
2388 cipher &optional sign always-trust)
2389 "Encrypt a file PLAIN and store the result to a file CIPHER.
2390 If CIPHER is nil, it returns the result as a string.
2391 If RECIPIENTS is nil, it performs symmetric encryption."
2392 (unwind-protect
2393 (progn
2394 (if cipher
2395 (epg-context-set-output-file context cipher)
2396 (epg-context-set-output-file context
2397 (epg--make-temp-file "epg-output")))
2398 (epg-start-encrypt context (epg-make-data-from-file plain)
2399 recipients sign always-trust)
2400 (epg-wait-for-completion context)
2401 (let ((errors (epg-context-result-for context 'error)))
2402 (if (and sign
2403 (not (epg-context-result-for context 'sign)))
2404 (signal 'epg-error
2405 (list "Sign failed" (epg-errors-to-string errors))))
2406 (if errors
2407 (signal 'epg-error
2408 (list "Encrypt failed" (epg-errors-to-string errors)))))
2409 (unless cipher
2410 (epg-read-output context)))
2411 (unless cipher
2412 (epg-delete-output-file context))
2413 (epg-reset context)))
2415 (defun epg-encrypt-string (context plain recipients
2416 &optional sign always-trust)
2417 "Encrypt a string PLAIN.
2418 If RECIPIENTS is nil, it performs symmetric encryption."
2419 (let ((input-file
2420 (unless (or (not sign)
2421 (eq (epg-context-protocol context) 'CMS)
2422 (condition-case nil
2423 (progn
2424 (epg-check-configuration (epg-configuration))
2426 (error)))
2427 (epg--make-temp-file "epg-input")))
2428 (coding-system-for-write 'binary))
2429 (unwind-protect
2430 (progn
2431 (epg-context-set-output-file context
2432 (epg--make-temp-file "epg-output"))
2433 (if input-file
2434 (write-region plain nil input-file nil 'quiet))
2435 (epg-start-encrypt context
2436 (if input-file
2437 (epg-make-data-from-file input-file)
2438 (epg-make-data-from-string plain))
2439 recipients sign always-trust)
2440 (epg-wait-for-completion context)
2441 (let ((errors (epg-context-result-for context 'error)))
2442 (if (and sign
2443 (not (epg-context-result-for context 'sign)))
2444 (signal 'epg-error
2445 (list "Sign failed" (epg-errors-to-string errors))))
2446 (if errors
2447 (signal 'epg-error
2448 (list "Encrypt failed" (epg-errors-to-string errors)))))
2449 (epg-read-output context))
2450 (epg-delete-output-file context)
2451 (if input-file
2452 (delete-file input-file))
2453 (epg-reset context))))
2455 (defun epg-start-export-keys (context keys)
2456 "Initiate an export keys operation.
2458 If you use this function, you will need to wait for the completion of
2459 `epg-gpg-program' by using `epg-wait-for-completion' and call
2460 `epg-reset' to clear a temporary output file.
2461 If you are unsure, use synchronous version of this function
2462 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
2463 (epg-context-set-operation context 'export-keys)
2464 (epg-context-set-result context nil)
2465 (epg--start context (cons "--export"
2466 (mapcar
2467 (lambda (key)
2468 (epg-sub-key-id
2469 (car (epg-key-sub-key-list key))))
2470 keys))))
2472 (defun epg-export-keys-to-file (context keys file)
2473 "Extract public KEYS."
2474 (unwind-protect
2475 (progn
2476 (if file
2477 (epg-context-set-output-file context file)
2478 (epg-context-set-output-file context
2479 (epg--make-temp-file "epg-output")))
2480 (epg-start-export-keys context keys)
2481 (epg-wait-for-completion context)
2482 (let ((errors (epg-context-result-for context 'error)))
2483 (if errors
2484 (signal 'epg-error
2485 (list "Export keys failed"
2486 (epg-errors-to-string errors)))))
2487 (unless file
2488 (epg-read-output context)))
2489 (unless file
2490 (epg-delete-output-file context))
2491 (epg-reset context)))
2493 (defun epg-export-keys-to-string (context keys)
2494 "Extract public KEYS and return them as a string."
2495 (epg-export-keys-to-file context keys nil))
2497 (defun epg-start-import-keys (context keys)
2498 "Initiate an import keys operation.
2499 KEYS is a data object.
2501 If you use this function, you will need to wait for the completion of
2502 `epg-gpg-program' by using `epg-wait-for-completion' and call
2503 `epg-reset' to clear a temporary output file.
2504 If you are unsure, use synchronous version of this function
2505 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
2506 (epg-context-set-operation context 'import-keys)
2507 (epg-context-set-result context nil)
2508 (epg--start context (if (epg-data-file keys)
2509 (list "--import" "--" (epg-data-file keys))
2510 (list "--import")))
2511 (when (epg-data-string keys)
2512 (if (eq (process-status (epg-context-process context)) 'run)
2513 (process-send-string (epg-context-process context)
2514 (epg-data-string keys)))
2515 (if (eq (process-status (epg-context-process context)) 'run)
2516 (process-send-eof (epg-context-process context)))))
2518 (defun epg--import-keys-1 (context keys)
2519 (unwind-protect
2520 (progn
2521 (epg-start-import-keys context keys)
2522 (epg-wait-for-completion context)
2523 (let ((errors (epg-context-result-for context 'error)))
2524 (if errors
2525 (signal 'epg-error
2526 (list "Import keys failed"
2527 (epg-errors-to-string errors))))))
2528 (epg-reset context)))
2530 (defun epg-import-keys-from-file (context keys)
2531 "Add keys from a file KEYS."
2532 (epg--import-keys-1 context (epg-make-data-from-file keys)))
2534 (defun epg-import-keys-from-string (context keys)
2535 "Add keys from a string KEYS."
2536 (epg--import-keys-1 context (epg-make-data-from-string keys)))
2538 (defun epg-start-receive-keys (context key-id-list)
2539 "Initiate a receive key operation.
2540 KEY-ID-LIST is a list of key IDs.
2542 If you use this function, you will need to wait for the completion of
2543 `epg-gpg-program' by using `epg-wait-for-completion' and call
2544 `epg-reset' to clear a temporary output file.
2545 If you are unsure, use synchronous version of this function
2546 `epg-receive-keys' instead."
2547 (epg-context-set-operation context 'receive-keys)
2548 (epg-context-set-result context nil)
2549 (epg--start context (cons "--recv-keys" key-id-list)))
2551 (defun epg-receive-keys (context keys)
2552 "Add keys from server.
2553 KEYS is a list of key IDs"
2554 (unwind-protect
2555 (progn
2556 (epg-start-receive-keys context keys)
2557 (epg-wait-for-completion context)
2558 (let ((errors (epg-context-result-for context 'error)))
2559 (if errors
2560 (signal 'epg-error
2561 (list "Receive keys failed"
2562 (epg-errors-to-string errors))))))
2563 (epg-reset context)))
2565 (defalias 'epg-import-keys-from-server 'epg-receive-keys)
2567 (defun epg-start-delete-keys (context keys &optional allow-secret)
2568 "Initiate a delete keys operation.
2570 If you use this function, you will need to wait for the completion of
2571 `epg-gpg-program' by using `epg-wait-for-completion' and call
2572 `epg-reset' to clear a temporary output file.
2573 If you are unsure, use synchronous version of this function
2574 `epg-delete-keys' instead."
2575 (epg-context-set-operation context 'delete-keys)
2576 (epg-context-set-result context nil)
2577 (epg--start context (cons (if allow-secret
2578 "--delete-secret-key"
2579 "--delete-key")
2580 (mapcar
2581 (lambda (key)
2582 (epg-sub-key-id
2583 (car (epg-key-sub-key-list key))))
2584 keys))))
2586 (defun epg-delete-keys (context keys &optional allow-secret)
2587 "Delete KEYS from the key ring."
2588 (unwind-protect
2589 (progn
2590 (epg-start-delete-keys context keys allow-secret)
2591 (epg-wait-for-completion context)
2592 (let ((errors (epg-context-result-for context 'error)))
2593 (if errors
2594 (signal 'epg-error
2595 (list "Delete keys failed"
2596 (epg-errors-to-string errors))))))
2597 (epg-reset context)))
2599 (defun epg-start-sign-keys (context keys &optional local)
2600 "Initiate a sign keys operation.
2602 If you use this function, you will need to wait for the completion of
2603 `epg-gpg-program' by using `epg-wait-for-completion' and call
2604 `epg-reset' to clear a temporary output file.
2605 If you are unsure, use synchronous version of this function
2606 `epg-sign-keys' instead."
2607 (declare (obsolete nil "23.1"))
2608 (epg-context-set-operation context 'sign-keys)
2609 (epg-context-set-result context nil)
2610 (epg--start context (cons (if local
2611 "--lsign-key"
2612 "--sign-key")
2613 (mapcar
2614 (lambda (key)
2615 (epg-sub-key-id
2616 (car (epg-key-sub-key-list key))))
2617 keys))))
2619 (defun epg-sign-keys (context keys &optional local)
2620 "Sign KEYS from the key ring."
2621 (declare (obsolete nil "23.1"))
2622 (unwind-protect
2623 (progn
2624 (epg-start-sign-keys context keys local)
2625 (epg-wait-for-completion context)
2626 (let ((errors (epg-context-result-for context 'error)))
2627 (if errors
2628 (signal 'epg-error
2629 (list "Sign keys failed"
2630 (epg-errors-to-string errors))))))
2631 (epg-reset context)))
2633 (defun epg-start-generate-key (context parameters)
2634 "Initiate a key generation.
2635 PARAMETERS specifies parameters for the key.
2637 If you use this function, you will need to wait for the completion of
2638 `epg-gpg-program' by using `epg-wait-for-completion' and call
2639 `epg-reset' to clear a temporary output file.
2640 If you are unsure, use synchronous version of this function
2641 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2642 (epg-context-set-operation context 'generate-key)
2643 (epg-context-set-result context nil)
2644 (if (epg-data-file parameters)
2645 (epg--start context (list "--batch" "--genkey" "--"
2646 (epg-data-file parameters)))
2647 (epg--start context '("--batch" "--genkey"))
2648 (if (eq (process-status (epg-context-process context)) 'run)
2649 (process-send-string (epg-context-process context)
2650 (epg-data-string parameters)))
2651 (if (eq (process-status (epg-context-process context)) 'run)
2652 (process-send-eof (epg-context-process context)))))
2654 (defun epg-generate-key-from-file (context parameters)
2655 "Generate a new key pair.
2656 PARAMETERS is a file which tells how to create the key."
2657 (unwind-protect
2658 (progn
2659 (epg-start-generate-key context (epg-make-data-from-file parameters))
2660 (epg-wait-for-completion context)
2661 (let ((errors (epg-context-result-for context 'error)))
2662 (if errors
2663 (signal 'epg-error
2664 (list "Generate key failed"
2665 (epg-errors-to-string errors))))))
2666 (epg-reset context)))
2668 (defun epg-generate-key-from-string (context parameters)
2669 "Generate a new key pair.
2670 PARAMETERS is a string which tells how to create the key."
2671 (unwind-protect
2672 (progn
2673 (epg-start-generate-key context (epg-make-data-from-string parameters))
2674 (epg-wait-for-completion context)
2675 (let ((errors (epg-context-result-for context 'error)))
2676 (if errors
2677 (signal 'epg-error
2678 (list "Generate key failed"
2679 (epg-errors-to-string errors))))))
2680 (epg-reset context)))
2682 (defun epg--decode-percent-escape (string)
2683 (let ((index 0))
2684 (while (string-match "%\\(\\(%\\)\\|\\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2685 string index)
2686 (if (match-beginning 2)
2687 (setq string (replace-match "%" t t string)
2688 index (1- (match-end 0)))
2689 (setq string (replace-match
2690 (string (string-to-number (match-string 3 string) 16))
2691 t t string)
2692 index (- (match-end 0) 2))))
2693 string))
2695 (defun epg--decode-hexstring (string)
2696 (let ((index 0))
2697 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
2698 (setq string (replace-match (string (string-to-number
2699 (match-string 0 string) 16))
2700 t t string)
2701 index (1- (match-end 0))))
2702 string))
2704 (defun epg--decode-quotedstring (string)
2705 (let ((index 0))
2706 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2707 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2708 string index)
2709 (if (match-beginning 2)
2710 (setq string (replace-match "\\2" t nil string)
2711 index (1- (match-end 0)))
2712 (if (match-beginning 3)
2713 (setq string (replace-match (string (string-to-number
2714 (match-string 0 string) 16))
2715 t t string)
2716 index (- (match-end 0) 2)))))
2717 string))
2719 (defun epg-dn-from-string (string)
2720 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2721 The return value is an alist mapping from types to values."
2722 (let ((index 0)
2723 (length (length string))
2724 alist type value group)
2725 (while (< index length)
2726 (if (eq index (string-match "[ \t\n\r]*" string index))
2727 (setq index (match-end 0)))
2728 (if (eq index (string-match
2729 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2730 string index))
2731 (setq type (match-string 1 string)
2732 index (match-end 0))
2733 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2734 string index))
2735 (setq type (match-string 1 string)
2736 index (match-end 0))))
2737 (unless type
2738 (error "Invalid type"))
2739 (if (eq index (string-match
2740 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2741 string index))
2742 (setq index (match-end 0)
2743 value (epg--decode-quotedstring (match-string 0 string)))
2744 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
2745 (setq index (match-end 0)
2746 value (epg--decode-hexstring (match-string 1 string)))
2747 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2748 string index))
2749 (setq index (match-end 0)
2750 value (epg--decode-quotedstring
2751 (match-string 0 string))))))
2752 (if group
2753 (if (stringp (car (car alist)))
2754 (setcar alist (list (cons type value) (car alist)))
2755 (setcar alist (cons (cons type value) (car alist))))
2756 (if (consp (car (car alist)))
2757 (setcar alist (nreverse (car alist))))
2758 (setq alist (cons (cons type value) alist)
2759 type nil
2760 value nil))
2761 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
2762 (setq index (match-end 0)
2763 group (eq (aref string (match-beginning 1)) ?+))))
2764 (nreverse alist)))
2766 (defun epg-decode-dn (alist)
2767 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2768 Type names are resolved using `epg-dn-type-alist'."
2769 (mapconcat
2770 (lambda (rdn)
2771 (if (stringp (car rdn))
2772 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
2773 (if entry
2774 (format "%s=%s" (cdr entry) (cdr rdn))
2775 (format "%s=%s" (car rdn) (cdr rdn))))
2776 (concat "(" (epg-decode-dn rdn) ")")))
2777 alist
2778 ", "))
2780 (provide 'epg)
2782 ;;; epg.el ends here