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
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/>.
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
58 ;; from gnupg/include/cipher.h
59 (defconst epg-pubkey-algorithm-alist
67 ;; from gnupg/include/cipher.h
68 (defconst epg-digest-algorithm-alist
77 ;; from gnupg/include/cipher.h
78 (defconst epg-compress-algorithm-alist
84 (defconst epg-invalid-recipients-reason-alist
85 '((0 .
"No specific reason given")
87 (2 .
"Ambiguous specification")
88 (3 .
"Wrong key usage")
93 (8 .
"Policy mismatch")
94 (9 .
"Not a secret key")
95 (10 .
"Key not trusted")))
97 (defconst epg-delete-problem-reason-alist
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")
106 (4 .
"New signatures")
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
138 (defvar epg-key-capablity-alist
142 (?a . authentication
)
145 (defvar epg-new-signature-type-alist
150 (defvar epg-dn-type-alist
151 '(("1.2.840.113549.1.9.1" .
"EMail")
155 ("0.2.262.1.10.7.20" .
"NameDistinguisher")
156 ("2.5.4.16" .
"ADDR")
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
)))
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
)))
189 (defun epg-make-context (&optional protocol armor textmode include-certs
190 cipher-algorithm digest-algorithm
192 "Return a context object."
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 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-pinentry-mode (context)
293 "Return the mode of pinentry invocation."
294 (unless (eq (car-safe context
) 'epg-context
)
295 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
296 (aref (cdr context
) 15))
298 (defun epg-context-set-protocol (context protocol
)
299 "Set the protocol used within CONTEXT."
300 (unless (eq (car-safe context
) 'epg-context
)
301 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
302 (aset (cdr context
) 0 protocol
))
304 (defun epg-context-set-armor (context armor
)
305 "Specify if the output should be ASCII armored in CONTEXT."
306 (unless (eq (car-safe context
) 'epg-context
)
307 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
308 (aset (cdr context
) 1 armor
))
310 (defun epg-context-set-textmode (context textmode
)
311 "Specify if canonical text mode should be used in CONTEXT."
312 (unless (eq (car-safe context
) 'epg-context
)
313 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
314 (aset (cdr context
) 2 textmode
))
316 (defun epg-context-set-include-certs (context include-certs
)
317 "Set how many certificates should be included in an S/MIME signed message."
318 (unless (eq (car-safe context
) 'epg-context
)
319 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
320 (aset (cdr context
) 3 include-certs
))
322 (defun epg-context-set-cipher-algorithm (context cipher-algorithm
)
323 "Set the cipher 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
) 4 cipher-algorithm
))
328 (defun epg-context-set-digest-algorithm (context digest-algorithm
)
329 "Set the digest 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
) 5 digest-algorithm
))
334 (defun epg-context-set-compress-algorithm (context compress-algorithm
)
335 "Set the compress algorithm in CONTEXT."
336 (unless (eq (car-safe context
) 'epg-context
)
337 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
338 (aset (cdr context
) 6 compress-algorithm
))
340 (defun epg-context-set-passphrase-callback (context
342 "Set the function used to query passphrase.
344 PASSPHRASE-CALLBACK is either a function, or a cons-cell whose
345 car is a function and cdr is a callback data.
347 The function gets three arguments: the context, the key-id in
348 question, and the callback data (if any).
350 The callback may not be called if you use GnuPG 2.x, which relies
351 on the external program called `gpg-agent' for passphrase query.
352 If you really want to intercept passphrase query, consider
353 installing GnuPG 1.x _along with_ GnuPG 2.x, which does passphrase
354 query by itself and Emacs can intercept them."
355 (unless (eq (car-safe context
) 'epg-context
)
356 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
357 (aset (cdr context
) 7 (if (consp passphrase-callback
)
359 (list passphrase-callback
))))
361 (defun epg-context-set-progress-callback (context
363 "Set the function which handles progress update.
365 PROGRESS-CALLBACK is either a function, or a cons-cell whose
366 car is a function and cdr is a callback data.
368 The function gets six arguments: the context, the operation
369 description, the character to display a progress unit, the
370 current amount done, the total amount to be done, and the
371 callback data (if any)."
372 (unless (eq (car-safe context
) 'epg-context
)
373 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
374 (aset (cdr context
) 8 (if (consp progress-callback
)
376 (list progress-callback
))))
378 (defun epg-context-set-signers (context signers
)
379 "Set the list of key-id for signing."
380 (unless (eq (car-safe context
) 'epg-context
)
381 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
382 (aset (cdr context
) 9 signers
))
384 (defun epg-context-set-sig-notations (context notations
)
385 "Set the list of notations for signing."
386 (unless (eq (car-safe context
) 'epg-context
)
387 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
388 (aset (cdr context
) 10 notations
))
390 (defun epg-context-set-process (context process
)
391 "Set the process object of `epg-gpg-program'.
392 This function is for internal use only."
393 (unless (eq (car-safe context
) 'epg-context
)
394 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
395 (aset (cdr context
) 11 process
))
397 (defun epg-context-set-output-file (context output-file
)
398 "Set the output file of `epg-gpg-program'.
399 This function is for internal use only."
400 (unless (eq (car-safe context
) 'epg-context
)
401 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
402 (aset (cdr context
) 12 output-file
))
404 (defun epg-context-set-result (context result
)
405 "Set the result of the previous cryptographic operation."
406 (unless (eq (car-safe context
) 'epg-context
)
407 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
408 (aset (cdr context
) 13 result
))
410 (defun epg-context-set-operation (context operation
)
411 "Set the name of the current cryptographic operation."
412 (unless (eq (car-safe context
) 'epg-context
)
413 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
414 (aset (cdr context
) 14 operation
))
416 (defun epg-context-set-pinentry-mode (context mode
)
417 "Set the mode of pinentry invocation."
418 (unless (eq (car-safe context
) 'epg-context
)
419 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
420 (unless (memq mode
'(nil ask cancel error loopback
))
421 (signal 'epg-error
(list "Unknown pinentry mode" mode
)))
422 (aset (cdr context
) 15 mode
))
424 (defun epg-make-signature (status &optional key-id
)
425 "Return a signature object."
426 (cons 'epg-signature
(vector status key-id nil nil nil nil nil nil nil nil
429 (defun epg-signature-status (signature)
430 "Return the status code of SIGNATURE."
431 (unless (eq (car-safe signature
) 'epg-signature
)
432 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
433 (aref (cdr signature
) 0))
435 (defun epg-signature-key-id (signature)
436 "Return the key-id of SIGNATURE."
437 (unless (eq (car-safe signature
) 'epg-signature
)
438 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
439 (aref (cdr signature
) 1))
441 (defun epg-signature-validity (signature)
442 "Return the validity of SIGNATURE."
443 (unless (eq (car-safe signature
) 'epg-signature
)
444 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
445 (aref (cdr signature
) 2))
447 (defun epg-signature-fingerprint (signature)
448 "Return the fingerprint of SIGNATURE."
449 (unless (eq (car-safe signature
) 'epg-signature
)
450 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
451 (aref (cdr signature
) 3))
453 (defun epg-signature-creation-time (signature)
454 "Return the creation time of SIGNATURE."
455 (unless (eq (car-safe signature
) 'epg-signature
)
456 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
457 (aref (cdr signature
) 4))
459 (defun epg-signature-expiration-time (signature)
460 "Return the expiration time of SIGNATURE."
461 (unless (eq (car-safe signature
) 'epg-signature
)
462 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
463 (aref (cdr signature
) 5))
465 (defun epg-signature-pubkey-algorithm (signature)
466 "Return the public key algorithm of SIGNATURE."
467 (unless (eq (car-safe signature
) 'epg-signature
)
468 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
469 (aref (cdr signature
) 6))
471 (defun epg-signature-digest-algorithm (signature)
472 "Return the digest algorithm of SIGNATURE."
473 (unless (eq (car-safe signature
) 'epg-signature
)
474 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
475 (aref (cdr signature
) 7))
477 (defun epg-signature-class (signature)
478 "Return the class of SIGNATURE."
479 (unless (eq (car-safe signature
) 'epg-signature
)
480 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
481 (aref (cdr signature
) 8))
483 (defun epg-signature-version (signature)
484 "Return the version of SIGNATURE."
485 (unless (eq (car-safe signature
) 'epg-signature
)
486 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
487 (aref (cdr signature
) 9))
489 (defun epg-sig-notations (signature)
490 "Return the list of notations of SIGNATURE."
491 (unless (eq (car-safe signature
) 'epg-signature
)
492 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
493 (aref (cdr signature
) 10))
495 (defun epg-signature-set-status (signature status
)
496 "Set the status code of SIGNATURE."
497 (unless (eq (car-safe signature
) 'epg-signature
)
498 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
499 (aset (cdr signature
) 0 status
))
501 (defun epg-signature-set-key-id (signature key-id
)
502 "Set the key-id of SIGNATURE."
503 (unless (eq (car-safe signature
) 'epg-signature
)
504 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
505 (aset (cdr signature
) 1 key-id
))
507 (defun epg-signature-set-validity (signature validity
)
508 "Set the validity of SIGNATURE."
509 (unless (eq (car-safe signature
) 'epg-signature
)
510 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
511 (aset (cdr signature
) 2 validity
))
513 (defun epg-signature-set-fingerprint (signature fingerprint
)
514 "Set the fingerprint of SIGNATURE."
515 (unless (eq (car-safe signature
) 'epg-signature
)
516 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
517 (aset (cdr signature
) 3 fingerprint
))
519 (defun epg-signature-set-creation-time (signature creation-time
)
520 "Set the creation time of SIGNATURE."
521 (unless (eq (car-safe signature
) 'epg-signature
)
522 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
523 (aset (cdr signature
) 4 creation-time
))
525 (defun epg-signature-set-expiration-time (signature expiration-time
)
526 "Set the expiration time of SIGNATURE."
527 (unless (eq (car-safe signature
) 'epg-signature
)
528 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
529 (aset (cdr signature
) 5 expiration-time
))
531 (defun epg-signature-set-pubkey-algorithm (signature pubkey-algorithm
)
532 "Set the public key algorithm of SIGNATURE."
533 (unless (eq (car-safe signature
) 'epg-signature
)
534 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
535 (aset (cdr signature
) 6 pubkey-algorithm
))
537 (defun epg-signature-set-digest-algorithm (signature digest-algorithm
)
538 "Set the digest algorithm of SIGNATURE."
539 (unless (eq (car-safe signature
) 'epg-signature
)
540 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
541 (aset (cdr signature
) 7 digest-algorithm
))
543 (defun epg-signature-set-class (signature class
)
544 "Set the class of SIGNATURE."
545 (unless (eq (car-safe signature
) 'epg-signature
)
546 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
547 (aset (cdr signature
) 8 class
))
549 (defun epg-signature-set-version (signature version
)
550 "Set the version of SIGNATURE."
551 (unless (eq (car-safe signature
) 'epg-signature
)
552 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
553 (aset (cdr signature
) 9 version
))
555 (defun epg-signature-set-notations (signature notations
)
556 "Set the list of notations of SIGNATURE."
557 (unless (eq (car-safe signature
) 'epg-signature
)
558 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
559 (aset (cdr signature
) 10 notations
))
561 (defun epg-make-new-signature (type pubkey-algorithm digest-algorithm
562 class creation-time fingerprint
)
563 "Return a new signature object."
564 (cons 'epg-new-signature
(vector type pubkey-algorithm digest-algorithm
565 class creation-time fingerprint
)))
567 (defun epg-new-signature-type (new-signature)
568 "Return the type of NEW-SIGNATURE."
569 (unless (eq (car-safe new-signature
) 'epg-new-signature
)
570 (signal 'wrong-type-argument
(list 'epg-new-signature-p new-signature
)))
571 (aref (cdr new-signature
) 0))
573 (defun epg-new-signature-pubkey-algorithm (new-signature)
574 "Return the public key algorithm of NEW-SIGNATURE."
575 (unless (eq (car-safe new-signature
) 'epg-new-signature
)
576 (signal 'wrong-type-argument
(list 'epg-new-signature-p new-signature
)))
577 (aref (cdr new-signature
) 1))
579 (defun epg-new-signature-digest-algorithm (new-signature)
580 "Return the digest algorithm of NEW-SIGNATURE."
581 (unless (eq (car-safe new-signature
) 'epg-new-signature
)
582 (signal 'wrong-type-argument
(list 'epg-new-signature-p new-signature
)))
583 (aref (cdr new-signature
) 2))
585 (defun epg-new-signature-class (new-signature)
586 "Return the class of NEW-SIGNATURE."
587 (unless (eq (car-safe new-signature
) 'epg-new-signature
)
588 (signal 'wrong-type-argument
(list 'epg-new-signature-p new-signature
)))
589 (aref (cdr new-signature
) 3))
591 (defun epg-new-signature-creation-time (new-signature)
592 "Return the creation time of NEW-SIGNATURE."
593 (unless (eq (car-safe new-signature
) 'epg-new-signature
)
594 (signal 'wrong-type-argument
(list 'epg-new-signature-p new-signature
)))
595 (aref (cdr new-signature
) 4))
597 (defun epg-new-signature-fingerprint (new-signature)
598 "Return the fingerprint of NEW-SIGNATURE."
599 (unless (eq (car-safe new-signature
) 'epg-new-signature
)
600 (signal 'wrong-type-argument
(list 'epg-new-signature-p new-signature
)))
601 (aref (cdr new-signature
) 5))
603 (defun epg-make-key (owner-trust)
604 "Return a key object."
605 (cons 'epg-key
(vector owner-trust nil nil
)))
607 (defun epg-key-owner-trust (key)
608 "Return the owner trust of KEY."
609 (unless (eq (car-safe key
) 'epg-key
)
610 (signal 'wrong-type-argument
(list 'epg-key-p key
)))
613 (defun epg-key-sub-key-list (key)
614 "Return the sub key list of KEY."
615 (unless (eq (car-safe key
) 'epg-key
)
616 (signal 'wrong-type-argument
(list 'epg-key-p key
)))
619 (defun epg-key-user-id-list (key)
620 "Return the user ID list of KEY."
621 (unless (eq (car-safe key
) 'epg-key
)
622 (signal 'wrong-type-argument
(list 'epg-key-p key
)))
625 (defun epg-key-set-sub-key-list (key sub-key-list
)
626 "Set the sub key list of KEY."
627 (unless (eq (car-safe key
) 'epg-key
)
628 (signal 'wrong-type-argument
(list 'epg-key-p key
)))
629 (aset (cdr key
) 1 sub-key-list
))
631 (defun epg-key-set-user-id-list (key user-id-list
)
632 "Set the user ID list of KEY."
633 (unless (eq (car-safe key
) 'epg-key
)
634 (signal 'wrong-type-argument
(list 'epg-key-p key
)))
635 (aset (cdr key
) 2 user-id-list
))
637 (defun epg-make-sub-key (validity capability secret-p algorithm length id
638 creation-time expiration-time
)
639 "Return a sub key object."
641 (vector validity capability secret-p algorithm length id creation-time
642 expiration-time nil
)))
644 (defun epg-sub-key-validity (sub-key)
645 "Return the validity of SUB-KEY."
646 (unless (eq (car-safe sub-key
) 'epg-sub-key
)
647 (signal 'wrong-type-argument
(list 'epg-sub-key-p sub-key
)))
648 (aref (cdr sub-key
) 0))
650 (defun epg-sub-key-capability (sub-key)
651 "Return the capability of SUB-KEY."
652 (unless (eq (car-safe sub-key
) 'epg-sub-key
)
653 (signal 'wrong-type-argument
(list 'epg-sub-key-p sub-key
)))
654 (aref (cdr sub-key
) 1))
656 (defun epg-sub-key-secret-p (sub-key)
657 "Return non-nil if SUB-KEY is a secret key."
658 (unless (eq (car-safe sub-key
) 'epg-sub-key
)
659 (signal 'wrong-type-argument
(list 'epg-sub-key-p sub-key
)))
660 (aref (cdr sub-key
) 2))
662 (defun epg-sub-key-algorithm (sub-key)
663 "Return the algorithm of SUB-KEY."
664 (unless (eq (car-safe sub-key
) 'epg-sub-key
)
665 (signal 'wrong-type-argument
(list 'epg-sub-key-p sub-key
)))
666 (aref (cdr sub-key
) 3))
668 (defun epg-sub-key-length (sub-key)
669 "Return the length of SUB-KEY."
670 (unless (eq (car-safe sub-key
) 'epg-sub-key
)
671 (signal 'wrong-type-argument
(list 'epg-sub-key-p sub-key
)))
672 (aref (cdr sub-key
) 4))
674 (defun epg-sub-key-id (sub-key)
675 "Return the ID of SUB-KEY."
676 (unless (eq (car-safe sub-key
) 'epg-sub-key
)
677 (signal 'wrong-type-argument
(list 'epg-sub-key-p sub-key
)))
678 (aref (cdr sub-key
) 5))
680 (defun epg-sub-key-creation-time (sub-key)
681 "Return the creation time of SUB-KEY."
682 (unless (eq (car-safe sub-key
) 'epg-sub-key
)
683 (signal 'wrong-type-argument
(list 'epg-sub-key-p sub-key
)))
684 (aref (cdr sub-key
) 6))
686 (defun epg-sub-key-expiration-time (sub-key)
687 "Return the expiration time of SUB-KEY."
688 (unless (eq (car-safe sub-key
) 'epg-sub-key
)
689 (signal 'wrong-type-argument
(list 'epg-sub-key-p sub-key
)))
690 (aref (cdr sub-key
) 7))
692 (defun epg-sub-key-fingerprint (sub-key)
693 "Return the fingerprint of SUB-KEY."
694 (unless (eq (car-safe sub-key
) 'epg-sub-key
)
695 (signal 'wrong-type-argument
(list 'epg-sub-key-p sub-key
)))
696 (aref (cdr sub-key
) 8))
698 (defun epg-sub-key-set-fingerprint (sub-key fingerprint
)
699 "Set the fingerprint of SUB-KEY.
700 This function is for internal use only."
701 (unless (eq (car-safe sub-key
) 'epg-sub-key
)
702 (signal 'wrong-type-argument
(list 'epg-sub-key-p sub-key
)))
703 (aset (cdr sub-key
) 8 fingerprint
))
705 (defun epg-make-user-id (validity string
)
706 "Return a user ID object."
707 (cons 'epg-user-id
(vector validity string nil
)))
709 (defun epg-user-id-validity (user-id)
710 "Return the validity of USER-ID."
711 (unless (eq (car-safe user-id
) 'epg-user-id
)
712 (signal 'wrong-type-argument
(list 'epg-user-id-p user-id
)))
713 (aref (cdr user-id
) 0))
715 (defun epg-user-id-string (user-id)
716 "Return the name of USER-ID."
717 (unless (eq (car-safe user-id
) 'epg-user-id
)
718 (signal 'wrong-type-argument
(list 'epg-user-id-p user-id
)))
719 (aref (cdr user-id
) 1))
721 (defun epg-user-id-signature-list (user-id)
722 "Return the signature list of USER-ID."
723 (unless (eq (car-safe user-id
) 'epg-user-id
)
724 (signal 'wrong-type-argument
(list 'epg-user-id-p user-id
)))
725 (aref (cdr user-id
) 2))
727 (defun epg-user-id-set-signature-list (user-id signature-list
)
728 "Set the signature list of USER-ID."
729 (unless (eq (car-safe user-id
) 'epg-user-id
)
730 (signal 'wrong-type-argument
(list 'epg-user-id-p user-id
)))
731 (aset (cdr user-id
) 2 signature-list
))
733 (defun epg-make-key-signature (validity pubkey-algorithm key-id creation-time
734 expiration-time user-id class
736 "Return a key signature object."
737 (cons 'epg-key-signature
738 (vector validity pubkey-algorithm key-id creation-time expiration-time
739 user-id class exportable-p
)))
741 (defun epg-key-signature-validity (key-signature)
742 "Return the validity of KEY-SIGNATURE."
743 (unless (eq (car-safe key-signature
) 'epg-key-signature
)
744 (signal 'wrong-type-argument
(list 'epg-key-signature-p key-signature
)))
745 (aref (cdr key-signature
) 0))
747 (defun epg-key-signature-pubkey-algorithm (key-signature)
748 "Return the public key algorithm of KEY-SIGNATURE."
749 (unless (eq (car-safe key-signature
) 'epg-key-signature
)
750 (signal 'wrong-type-argument
(list 'epg-key-signature-p key-signature
)))
751 (aref (cdr key-signature
) 1))
753 (defun epg-key-signature-key-id (key-signature)
754 "Return the key-id of KEY-SIGNATURE."
755 (unless (eq (car-safe key-signature
) 'epg-key-signature
)
756 (signal 'wrong-type-argument
(list 'epg-key-signature-p key-signature
)))
757 (aref (cdr key-signature
) 2))
759 (defun epg-key-signature-creation-time (key-signature)
760 "Return the creation time of KEY-SIGNATURE."
761 (unless (eq (car-safe key-signature
) 'epg-key-signature
)
762 (signal 'wrong-type-argument
(list 'epg-key-signature-p key-signature
)))
763 (aref (cdr key-signature
) 3))
765 (defun epg-key-signature-expiration-time (key-signature)
766 "Return the expiration time of KEY-SIGNATURE."
767 (unless (eq (car-safe key-signature
) 'epg-key-signature
)
768 (signal 'wrong-type-argument
(list 'epg-key-signature-p key-signature
)))
769 (aref (cdr key-signature
) 4))
771 (defun epg-key-signature-user-id (key-signature)
772 "Return the user-id of KEY-SIGNATURE."
773 (unless (eq (car-safe key-signature
) 'epg-key-signature
)
774 (signal 'wrong-type-argument
(list 'epg-key-signature-p key-signature
)))
775 (aref (cdr key-signature
) 5))
777 (defun epg-key-signature-class (key-signature)
778 "Return the class of KEY-SIGNATURE."
779 (unless (eq (car-safe key-signature
) 'epg-key-signature
)
780 (signal 'wrong-type-argument
(list 'epg-key-signature-p key-signature
)))
781 (aref (cdr key-signature
) 6))
783 (defun epg-key-signature-exportable-p (key-signature)
784 "Return t if KEY-SIGNATURE is exportable."
785 (unless (eq (car-safe key-signature
) 'epg-key-signature
)
786 (signal 'wrong-type-argument
(list 'epg-key-signature-p key-signature
)))
787 (aref (cdr key-signature
) 7))
789 (defun epg-make-sig-notation (name value
&optional human-readable
791 "Return a notation object."
792 (cons 'epg-sig-notation
(vector name value human-readable critical
)))
794 (defun epg-sig-notation-name (sig-notation)
795 "Return the name of SIG-NOTATION."
796 (unless (eq (car-safe sig-notation
) 'epg-sig-notation
)
797 (signal 'wrong-type-argument
(list 'epg-sig-notation-p
799 (aref (cdr sig-notation
) 0))
801 (defun epg-sig-notation-value (sig-notation)
802 "Return the value of SIG-NOTATION."
803 (unless (eq (car-safe sig-notation
) 'epg-sig-notation
)
804 (signal 'wrong-type-argument
(list 'epg-sig-notation-p
806 (aref (cdr sig-notation
) 1))
808 (defun epg-sig-notation-human-readable (sig-notation)
809 "Return the human-readable of SIG-NOTATION."
810 (unless (eq (car-safe sig-notation
) 'epg-sig-notation
)
811 (signal 'wrong-type-argument
(list 'epg-sig-notation-p
813 (aref (cdr sig-notation
) 2))
815 (defun epg-sig-notation-critical (sig-notation)
816 "Return the critical of SIG-NOTATION."
817 (unless (eq (car-safe sig-notation
) 'epg-sig-notation
)
818 (signal 'wrong-type-argument
(list 'epg-sig-notation-p
820 (aref (cdr sig-notation
) 3))
822 (defun epg-sig-notation-set-value (sig-notation value
)
823 "Set the value of SIG-NOTATION."
824 (unless (eq (car-safe sig-notation
) 'epg-sig-notation
)
825 (signal 'wrong-type-argument
(list 'epg-sig-notation-p
827 (aset (cdr sig-notation
) 1 value
))
829 (defun epg-make-import-status (fingerprint &optional reason new user-id
830 signature sub-key secret
)
831 "Return an import status object."
832 (cons 'epg-import-status
(vector fingerprint reason new user-id signature
835 (defun epg-import-status-fingerprint (import-status)
836 "Return the fingerprint of the key that was considered."
837 (unless (eq (car-safe import-status
) 'epg-import-status
)
838 (signal 'wrong-type-argument
(list 'epg-import-status-p import-status
)))
839 (aref (cdr import-status
) 0))
841 (defun epg-import-status-reason (import-status)
842 "Return the reason code for import failure."
843 (unless (eq (car-safe import-status
) 'epg-import-status
)
844 (signal 'wrong-type-argument
(list 'epg-import-status-p import-status
)))
845 (aref (cdr import-status
) 1))
847 (defun epg-import-status-new (import-status)
848 "Return t if the imported key was new."
849 (unless (eq (car-safe import-status
) 'epg-import-status
)
850 (signal 'wrong-type-argument
(list 'epg-import-status-p import-status
)))
851 (aref (cdr import-status
) 2))
853 (defun epg-import-status-user-id (import-status)
854 "Return t if the imported key contained new user IDs."
855 (unless (eq (car-safe import-status
) 'epg-import-status
)
856 (signal 'wrong-type-argument
(list 'epg-import-status-p import-status
)))
857 (aref (cdr import-status
) 3))
859 (defun epg-import-status-signature (import-status)
860 "Return t if the imported key contained new signatures."
861 (unless (eq (car-safe import-status
) 'epg-import-status
)
862 (signal 'wrong-type-argument
(list 'epg-import-status-p import-status
)))
863 (aref (cdr import-status
) 4))
865 (defun epg-import-status-sub-key (import-status)
866 "Return t if the imported key contained new sub keys."
867 (unless (eq (car-safe import-status
) 'epg-import-status
)
868 (signal 'wrong-type-argument
(list 'epg-import-status-p import-status
)))
869 (aref (cdr import-status
) 5))
871 (defun epg-import-status-secret (import-status)
872 "Return t if the imported key contained a secret key."
873 (unless (eq (car-safe import-status
) 'epg-import-status
)
874 (signal 'wrong-type-argument
(list 'epg-import-status-p import-status
)))
875 (aref (cdr import-status
) 6))
877 (defun epg-make-import-result (considered no-user-id imported imported-rsa
878 unchanged new-user-ids new-sub-keys
879 new-signatures new-revocations
880 secret-read secret-imported
881 secret-unchanged not-imported
883 "Return an import result object."
884 (cons 'epg-import-result
(vector considered no-user-id imported imported-rsa
885 unchanged new-user-ids new-sub-keys
886 new-signatures new-revocations secret-read
887 secret-imported secret-unchanged
888 not-imported imports
)))
890 (defun epg-import-result-considered (import-result)
891 "Return the total number of considered keys."
892 (unless (eq (car-safe import-result
) 'epg-import-result
)
893 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
894 (aref (cdr import-result
) 0))
896 (defun epg-import-result-no-user-id (import-result)
897 "Return the number of keys without user ID."
898 (unless (eq (car-safe import-result
) 'epg-import-result
)
899 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
900 (aref (cdr import-result
) 1))
902 (defun epg-import-result-imported (import-result)
903 "Return the number of imported keys."
904 (unless (eq (car-safe import-result
) 'epg-import-result
)
905 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
906 (aref (cdr import-result
) 2))
908 (defun epg-import-result-imported-rsa (import-result)
909 "Return the number of imported RSA keys."
910 (unless (eq (car-safe import-result
) 'epg-import-result
)
911 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
912 (aref (cdr import-result
) 3))
914 (defun epg-import-result-unchanged (import-result)
915 "Return the number of unchanged keys."
916 (unless (eq (car-safe import-result
) 'epg-import-result
)
917 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
918 (aref (cdr import-result
) 4))
920 (defun epg-import-result-new-user-ids (import-result)
921 "Return the number of new user IDs."
922 (unless (eq (car-safe import-result
) 'epg-import-result
)
923 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
924 (aref (cdr import-result
) 5))
926 (defun epg-import-result-new-sub-keys (import-result)
927 "Return the number of new sub keys."
928 (unless (eq (car-safe import-result
) 'epg-import-result
)
929 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
930 (aref (cdr import-result
) 6))
932 (defun epg-import-result-new-signatures (import-result)
933 "Return the number of new signatures."
934 (unless (eq (car-safe import-result
) 'epg-import-result
)
935 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
936 (aref (cdr import-result
) 7))
938 (defun epg-import-result-new-revocations (import-result)
939 "Return the number of new revocations."
940 (unless (eq (car-safe import-result
) 'epg-import-result
)
941 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
942 (aref (cdr import-result
) 8))
944 (defun epg-import-result-secret-read (import-result)
945 "Return the total number of secret keys read."
946 (unless (eq (car-safe import-result
) 'epg-import-result
)
947 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
948 (aref (cdr import-result
) 9))
950 (defun epg-import-result-secret-imported (import-result)
951 "Return the number of imported secret keys."
952 (unless (eq (car-safe import-result
) 'epg-import-result
)
953 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
954 (aref (cdr import-result
) 10))
956 (defun epg-import-result-secret-unchanged (import-result)
957 "Return the number of unchanged secret keys."
958 (unless (eq (car-safe import-result
) 'epg-import-result
)
959 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
960 (aref (cdr import-result
) 11))
962 (defun epg-import-result-not-imported (import-result)
963 "Return the number of keys not imported."
964 (unless (eq (car-safe import-result
) 'epg-import-result
)
965 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
966 (aref (cdr import-result
) 12))
968 (defun epg-import-result-imports (import-result)
969 "Return the list of `epg-import-status' objects."
970 (unless (eq (car-safe import-result
) 'epg-import-result
)
971 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
972 (aref (cdr import-result
) 13))
974 (defun epg-context-result-for (context name
)
975 "Return the result of CONTEXT associated with NAME."
976 (cdr (assq name
(epg-context-result context
))))
978 (defun epg-context-set-result-for (context name value
)
979 "Set the result of CONTEXT associated with NAME to VALUE."
980 (let* ((result (epg-context-result context
))
981 (entry (assq name result
)))
984 (epg-context-set-result context
(cons (cons name value
) result
)))))
986 (defun epg-signature-to-string (signature)
987 "Convert SIGNATURE to a human readable string."
988 (let* ((user-id (cdr (assoc (epg-signature-key-id signature
)
990 (pubkey-algorithm (epg-signature-pubkey-algorithm signature
))
991 (key-id (epg-signature-key-id signature
)))
993 (cond ((eq (epg-signature-status signature
) 'good
)
994 "Good signature from ")
995 ((eq (epg-signature-status signature
) 'bad
)
996 "Bad signature from ")
997 ((eq (epg-signature-status signature
) 'expired
)
998 "Expired signature from ")
999 ((eq (epg-signature-status signature
) 'expired-key
)
1000 "Signature made by expired key ")
1001 ((eq (epg-signature-status signature
) 'revoked-key
)
1002 "Signature made by revoked key ")
1003 ((eq (epg-signature-status signature
) 'no-pubkey
)
1004 "No public key for "))
1008 (if (stringp user-id
)
1010 (epg-decode-dn user-id
)))
1012 (if (epg-signature-validity signature
)
1013 (format " (trust %s)" (epg-signature-validity signature
))
1015 (if (epg-signature-creation-time signature
)
1016 (format-time-string " created at %Y-%m-%dT%T%z"
1017 (epg-signature-creation-time signature
))
1019 (if pubkey-algorithm
1021 (or (cdr (assq pubkey-algorithm epg-pubkey-algorithm-alist
))
1022 (format "(unknown algorithm %d)" pubkey-algorithm
)))
1025 (defun epg-verify-result-to-string (verify-result)
1026 "Convert VERIFY-RESULT to a human readable string."
1027 (mapconcat #'epg-signature-to-string verify-result
"\n"))
1029 (defun epg-new-signature-to-string (new-signature)
1030 "Convert NEW-SIGNATURE to a human readable string."
1032 (cond ((eq (epg-new-signature-type new-signature
) 'detached
)
1033 "Detached signature ")
1034 ((eq (epg-new-signature-type new-signature
) 'clear
)
1035 "Cleartext signature ")
1038 (cdr (assq (epg-new-signature-pubkey-algorithm new-signature
)
1039 epg-pubkey-algorithm-alist
))
1041 (cdr (assq (epg-new-signature-digest-algorithm new-signature
)
1042 epg-digest-algorithm-alist
))
1044 (format "%02X " (epg-new-signature-class new-signature
))
1045 (epg-new-signature-fingerprint new-signature
)))
1047 (defun epg-import-result-to-string (import-result)
1048 "Convert IMPORT-RESULT to a human readable string."
1049 (concat (format "Total number processed: %d\n"
1050 (epg-import-result-considered import-result
))
1051 (if (> (epg-import-result-not-imported import-result
) 0)
1052 (format " skipped new keys: %d\n"
1053 (epg-import-result-not-imported import-result
)))
1054 (if (> (epg-import-result-no-user-id import-result
) 0)
1055 (format " w/o user IDs: %d\n"
1056 (epg-import-result-no-user-id import-result
)))
1057 (if (> (epg-import-result-imported import-result
) 0)
1058 (concat (format " imported: %d"
1059 (epg-import-result-imported import-result
))
1060 (if (> (epg-import-result-imported-rsa import-result
) 0)
1061 (format " (RSA: %d)"
1062 (epg-import-result-imported-rsa
1065 (if (> (epg-import-result-unchanged import-result
) 0)
1066 (format " unchanged: %d\n"
1067 (epg-import-result-unchanged import-result
)))
1068 (if (> (epg-import-result-new-user-ids import-result
) 0)
1069 (format " new user IDs: %d\n"
1070 (epg-import-result-new-user-ids import-result
)))
1071 (if (> (epg-import-result-new-sub-keys import-result
) 0)
1072 (format " new subkeys: %d\n"
1073 (epg-import-result-new-sub-keys import-result
)))
1074 (if (> (epg-import-result-new-signatures import-result
) 0)
1075 (format " new signatures: %d\n"
1076 (epg-import-result-new-signatures import-result
)))
1077 (if (> (epg-import-result-new-revocations import-result
) 0)
1078 (format " new key revocations: %d\n"
1079 (epg-import-result-new-revocations import-result
)))
1080 (if (> (epg-import-result-secret-read import-result
) 0)
1081 (format " secret keys read: %d\n"
1082 (epg-import-result-secret-read import-result
)))
1083 (if (> (epg-import-result-secret-imported import-result
) 0)
1084 (format " secret keys imported: %d\n"
1085 (epg-import-result-secret-imported import-result
)))
1086 (if (> (epg-import-result-secret-unchanged import-result
) 0)
1087 (format " secret keys unchanged: %d\n"
1088 (epg-import-result-secret-unchanged import-result
)))))
1090 (defun epg-error-to-string (error)
1092 ((eq (car error
) 'exit
)
1094 ((eq (car error
) 'quit
)
1096 ((eq (car error
) 'no-data
)
1097 (let ((entry (assq (cdr error
) epg-no-data-reason-alist
)))
1099 (format "No data (%s)" (downcase (cdr entry
)))
1101 ((eq (car error
) 'unexpected
)
1102 (let ((entry (assq (cdr error
) epg-unexpected-reason-alist
)))
1104 (format "Unexpected (%s)" (downcase (cdr entry
)))
1106 ((eq (car error
) 'bad-armor
)
1108 ((memq (car error
) '(invalid-recipient invalid-signer
))
1110 (if (eq (car error
) 'invalid-recipient
)
1111 "Unusable public key"
1112 "Unusable secret key")
1113 (let ((entry (assq 'requested
(cdr error
))))
1115 (format ": %s" (cdr entry
))
1117 (let ((entry (assq 'reason
(cdr error
))))
1119 (> (cdr entry
) 0) ;no specific reason given
1120 (setq entry
(assq (cdr entry
)
1121 epg-invalid-recipients-reason-alist
)))
1122 (format " (%s)" (downcase (cdr entry
)))
1124 ((eq (car error
) 'no-pubkey
)
1125 (format "No public key: %s" (cdr error
)))
1126 ((eq (car error
) 'no-seckey
)
1127 (format "No secret key: %s" (cdr error
)))
1128 ((eq (car error
) 'no-recipients
)
1130 ((eq (car error
) 'no-signers
)
1132 ((eq (car error
) 'delete-problem
)
1133 (let ((entry (assq (cdr error
) epg-delete-problem-reason-alist
)))
1135 (format "Delete problem (%s)" (downcase (cdr entry
)))
1137 ((eq (car error
) 'key-not-created
)
1138 "Key not created")))
1140 (defun epg-errors-to-string (errors)
1141 (mapconcat #'epg-error-to-string errors
"; "))
1143 (defun epg--start (context args
)
1144 "Start `epg-gpg-program' in a subprocess with given ARGS."
1145 (if (and (epg-context-process context
)
1146 (eq (process-status (epg-context-process context
)) 'run
))
1147 (error "%s is already running in this context"
1148 (if (eq (epg-context-protocol context
) 'CMS
)
1151 (let* ((agent-info (getenv "GPG_AGENT_INFO"))
1152 (args (append (list "--no-tty"
1155 (if (and (not (eq (epg-context-protocol context
) 'CMS
))
1156 (string-match ":" (or agent-info
"")))
1158 (if (and (not (eq (epg-context-protocol context
) 'CMS
))
1159 (epg-context-progress-callback context
))
1160 '("--enable-progress-filter"))
1161 (if epg-gpg-home-directory
1162 (list "--homedir" epg-gpg-home-directory
))
1163 (unless (eq (epg-context-protocol context
) 'CMS
)
1164 '("--command-fd" "0"))
1165 (if (epg-context-armor context
) '("--armor"))
1166 (if (epg-context-textmode context
) '("--textmode"))
1167 (if (epg-context-output-file context
)
1168 (list "--output" (epg-context-output-file context
)))
1169 (if (epg-context-pinentry-mode context
)
1170 (list "--pinentry-mode"
1171 (symbol-name (epg-context-pinentry-mode
1174 (coding-system-for-write 'binary
)
1175 (coding-system-for-read 'binary
)
1176 process-connection-type
1177 (process-environment process-environment
)
1178 (orig-mode (default-file-modes))
1179 (buffer (generate-new-buffer " *epg*"))
1183 (agent-mtime '(0 0 0 0)))
1184 ;; Set GPG_TTY and TERM for pinentry-curses. Note that we can't
1185 ;; use `terminal-name' here to get the real pty name for the child
1186 ;; process, though /dev/fd/0" is not portable.
1187 (unless (memq system-type
'(ms-dos windows-nt
))
1190 (when (= (call-process "tty" "/dev/fd/0" t
) 0)
1192 (setq terminal-name
(buffer-string)))
1195 (setq process-environment
1196 (cons (concat "GPG_TTY=" terminal-name
)
1197 (cons "TERM=xterm" process-environment
))))
1198 ;; Record modified time of gpg-agent socket to restore the Emacs
1199 ;; frame on text terminal in `epg-wait-for-completion'.
1201 ;; <http://lists.gnu.org/archive/html/emacs-devel/2007-02/msg00755.html>
1202 ;; for more details.
1203 (when (and agent-info
(string-match "\\(.*\\):[0-9]+:[0-9]+" agent-info
))
1204 (setq agent-file
(match-string 1 agent-info
)
1205 agent-mtime
(or (nth 5 (file-attributes agent-file
)) '(0 0 0 0))))
1208 (unless epg-debug-buffer
1209 (setq epg-debug-buffer
(generate-new-buffer " *epg-debug*")))
1210 (set-buffer epg-debug-buffer
)
1211 (goto-char (point-max))
1212 (insert (if agent-info
1213 (format "GPG_AGENT_INFO=%s\n" agent-info
)
1214 "GPG_AGENT_INFO is not set\n")
1216 (if (eq (epg-context-protocol context
) 'CMS
)
1219 (mapconcat #'identity args
" ")))))
1220 (with-current-buffer buffer
1221 (if (fboundp 'set-buffer-multibyte
)
1222 (set-buffer-multibyte nil
))
1223 (make-local-variable 'epg-last-status
)
1224 (setq epg-last-status nil
)
1225 (make-local-variable 'epg-read-point
)
1226 (setq epg-read-point
(point-min))
1227 (make-local-variable 'epg-process-filter-running
)
1228 (setq epg-process-filter-running nil
)
1229 (make-local-variable 'epg-pending-status-list
)
1230 (setq epg-pending-status-list nil
)
1231 (make-local-variable 'epg-key-id
)
1232 (setq epg-key-id nil
)
1233 (make-local-variable 'epg-context
)
1234 (setq epg-context context
)
1235 (make-local-variable 'epg-agent-file
)
1236 (setq epg-agent-file agent-file
)
1237 (make-local-variable 'epg-agent-mtime
)
1238 (setq epg-agent-mtime agent-mtime
))
1241 (set-default-file-modes 448)
1243 (apply #'start-process
"epg" buffer
1244 (if (eq (epg-context-protocol context
) 'CMS
)
1248 (set-default-file-modes orig-mode
))
1249 (set-process-filter process
#'epg--process-filter
)
1250 (epg-context-set-process context process
)))
1252 (defun epg--process-filter (process input
)
1255 (unless epg-debug-buffer
1256 (setq epg-debug-buffer
(generate-new-buffer " *epg-debug*")))
1257 (set-buffer epg-debug-buffer
)
1258 (goto-char (point-max))
1260 (if (buffer-live-p (process-buffer process
))
1261 (with-current-buffer (process-buffer process
)
1262 (goto-char (point-max))
1264 (unless epg-process-filter-running
1267 (setq epg-process-filter-running t
)
1268 (goto-char epg-read-point
)
1270 (while (looking-at ".*\n") ;the input line finished
1271 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
1272 (let* ((status (match-string 1))
1273 (string (match-string 2))
1274 (symbol (intern-soft (concat "epg--status-"
1276 (if (member status epg-pending-status-list
)
1277 (setq epg-pending-status-list nil
))
1280 (funcall symbol epg-context string
))
1281 (setq epg-last-status
(cons status string
))))
1283 (setq epg-read-point
(point))))
1284 (setq epg-process-filter-running nil
))))))
1286 (defun epg-read-output (context)
1287 "Read the output file CONTEXT and return the content as a string."
1289 (if (fboundp 'set-buffer-multibyte
)
1290 (set-buffer-multibyte nil
))
1291 (if (file-exists-p (epg-context-output-file context
))
1292 (let ((coding-system-for-read 'binary
))
1293 (insert-file-contents (epg-context-output-file context
))
1296 (defun epg-wait-for-status (context status-list
)
1297 "Wait until one of elements in STATUS-LIST arrives."
1298 (with-current-buffer (process-buffer (epg-context-process context
))
1299 (setq epg-pending-status-list status-list
)
1300 (while (and (eq (process-status (epg-context-process context
)) 'run
)
1301 epg-pending-status-list
)
1302 (accept-process-output (epg-context-process context
) 1))
1303 (if epg-pending-status-list
1304 (epg-context-set-result-for
1307 (epg-context-result-for context
'error
))))))
1309 (defun epg-wait-for-completion (context)
1310 "Wait until the `epg-gpg-program' process completes."
1311 (while (eq (process-status (epg-context-process context
)) 'run
)
1312 (accept-process-output (epg-context-process context
) 1))
1313 ;; This line is needed to run the process-filter right now.
1315 ;; Restore Emacs frame on text terminal, when pinentry-curses has terminated.
1316 (if (with-current-buffer (process-buffer (epg-context-process context
))
1318 (> (float-time (or (nth 5 (file-attributes epg-agent-file
))
1320 (float-time epg-agent-mtime
))))
1321 (redraw-frame (selected-frame)))
1322 (epg-context-set-result-for
1324 (nreverse (epg-context-result-for context
'error
))))
1326 (defun epg-reset (context)
1327 "Reset the CONTEXT."
1328 (if (and (epg-context-process context
)
1329 (buffer-live-p (process-buffer (epg-context-process context
))))
1330 (kill-buffer (process-buffer (epg-context-process context
))))
1331 (epg-context-set-process context nil
))
1333 (defun epg-delete-output-file (context)
1334 "Delete the output file of CONTEXT."
1335 (if (and (epg-context-output-file context
)
1336 (file-exists-p (epg-context-output-file context
)))
1337 (delete-file (epg-context-output-file context
))))
1340 (if (fboundp 'decode-coding-string
)
1341 (defalias 'epg--decode-coding-string
'decode-coding-string
)
1342 (defalias 'epg--decode-coding-string
'identity
)))
1344 (defun epg--status-USERID_HINT (_context string
)
1345 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string
)
1346 (let* ((key-id (match-string 1 string
))
1347 (user-id (match-string 2 string
))
1348 (entry (assoc key-id epg-user-id-alist
)))
1350 (setq user-id
(epg--decode-coding-string
1351 (epg--decode-percent-escape user-id
)
1355 (setcdr entry user-id
)
1356 (setq epg-user-id-alist
(cons (cons key-id user-id
)
1357 epg-user-id-alist
))))))
1359 (defun epg--status-NEED_PASSPHRASE (_context string
)
1360 (if (string-match "\\`\\([^ ]+\\)" string
)
1361 (setq epg-key-id
(match-string 1 string
))))
1363 (defun epg--status-NEED_PASSPHRASE_SYM (_context _string
)
1364 (setq epg-key-id
'SYM
))
1366 (defun epg--status-NEED_PASSPHRASE_PIN (_context _string
)
1367 (setq epg-key-id
'PIN
))
1370 (if (fboundp 'clear-string
)
1371 (defalias 'epg--clear-string
'clear-string
)
1372 (defun epg--clear-string (string)
1373 (fillarray string
0))))
1376 (if (fboundp 'encode-coding-string
)
1377 (defalias 'epg--encode-coding-string
'encode-coding-string
)
1378 (defalias 'epg--encode-coding-string
'identity
)))
1380 (defun epg--status-GET_HIDDEN (context string
)
1381 (when (and epg-key-id
1382 (string-match "\\`passphrase\\." string
))
1383 (unless (epg-context-passphrase-callback context
)
1384 (error "passphrase-callback not set"))
1387 passphrase-with-new-line
1388 encoded-passphrase-with-new-line
)
1394 (car (epg-context-passphrase-callback context
))
1397 (cdr (epg-context-passphrase-callback context
))))
1399 (setq passphrase-with-new-line
(concat passphrase
"\n"))
1400 (epg--clear-string passphrase
)
1401 (setq passphrase nil
)
1402 (if epg-passphrase-coding-system
1404 (setq encoded-passphrase-with-new-line
1405 (epg--encode-coding-string
1406 passphrase-with-new-line
1407 (coding-system-change-eol-conversion
1408 epg-passphrase-coding-system
'unix
)))
1409 (epg--clear-string passphrase-with-new-line
)
1410 (setq passphrase-with-new-line nil
))
1411 (setq encoded-passphrase-with-new-line
1412 passphrase-with-new-line
1413 passphrase-with-new-line nil
))
1414 (process-send-string (epg-context-process context
)
1415 encoded-passphrase-with-new-line
)))
1417 (epg-context-set-result-for
1420 (epg-context-result-for context
'error
)))
1421 (delete-process (epg-context-process context
))))
1423 (epg--clear-string passphrase
))
1424 (if passphrase-with-new-line
1425 (epg--clear-string passphrase-with-new-line
))
1426 (if encoded-passphrase-with-new-line
1427 (epg--clear-string encoded-passphrase-with-new-line
))))))
1429 (defun epg--prompt-GET_BOOL (_context string
)
1430 (let ((entry (assoc string epg-prompt-alist
)))
1431 (y-or-n-p (if entry
(cdr entry
) (concat string
"? ")))))
1433 (defun epg--prompt-GET_BOOL-untrusted_key.override
(_context _string
)
1434 (y-or-n-p (if (and (equal (car epg-last-status
) "USERID_HINT")
1435 (string-match "\\`\\([^ ]+\\) \\(.*\\)"
1436 (cdr epg-last-status
)))
1437 (let* ((key-id (match-string 1 (cdr epg-last-status
)))
1438 (user-id (match-string 2 (cdr epg-last-status
)))
1439 (entry (assoc key-id epg-user-id-alist
)))
1441 (setq user-id
(cdr entry
)))
1442 (format "Untrusted key %s %s. Use anyway? " key-id user-id
))
1443 "Use untrusted key anyway? ")))
1445 (defun epg--status-GET_BOOL (context string
)
1448 (if (funcall (or (intern-soft (concat "epg--prompt-GET_BOOL-" string
))
1449 #'epg--prompt-GET_BOOL
)
1451 (process-send-string (epg-context-process context
) "y\n")
1452 (process-send-string (epg-context-process context
) "n\n"))
1454 (epg-context-set-result-for
1457 (epg-context-result-for context
'error
)))
1458 (delete-process (epg-context-process context
))))))
1460 (defun epg--status-GET_LINE (context string
)
1461 (let ((entry (assoc string epg-prompt-alist
))
1464 (process-send-string (epg-context-process context
)
1465 (concat (read-string
1468 (concat string
": ")))
1471 (epg-context-set-result-for
1474 (epg-context-result-for context
'error
)))
1475 (delete-process (epg-context-process context
))))))
1477 (defun epg--status-*SIG
(context status string
)
1478 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string
)
1479 (let* ((key-id (match-string 1 string
))
1480 (user-id (match-string 2 string
))
1481 (entry (assoc key-id epg-user-id-alist
)))
1482 (epg-context-set-result-for
1485 (cons (epg-make-signature status key-id
)
1486 (epg-context-result-for context
'verify
)))
1488 (if (eq (epg-context-protocol context
) 'CMS
)
1489 (setq user-id
(epg-dn-from-string user-id
))
1490 (setq user-id
(epg--decode-coding-string
1491 (epg--decode-percent-escape user-id
)
1495 (setcdr entry user-id
)
1496 (setq epg-user-id-alist
1497 (cons (cons key-id user-id
) epg-user-id-alist
))))
1498 (epg-context-set-result-for
1501 (cons (epg-make-signature status
)
1502 (epg-context-result-for context
'verify
)))))
1504 (defun epg--status-GOODSIG (context string
)
1505 (epg--status-*SIG context
'good string
))
1507 (defun epg--status-EXPSIG (context string
)
1508 (epg--status-*SIG context
'expired string
))
1510 (defun epg--status-EXPKEYSIG (context string
)
1511 (epg--status-*SIG context
'expired-key string
))
1513 (defun epg--status-REVKEYSIG (context string
)
1514 (epg--status-*SIG context
'revoked-key string
))
1516 (defun epg--status-BADSIG (context string
)
1517 (epg--status-*SIG context
'bad string
))
1519 (defun epg--status-NO_PUBKEY (context string
)
1520 (if (eq (epg-context-operation context
) 'verify
)
1521 (let ((signature (car (epg-context-result-for context
'verify
))))
1523 (eq (epg-signature-status signature
) 'error
)
1524 (equal (epg-signature-key-id signature
) string
))
1525 (epg-signature-set-status signature
'no-pubkey
)))
1526 (epg-context-set-result-for
1528 (cons (cons 'no-pubkey string
)
1529 (epg-context-result-for context
'error
)))))
1531 (defun epg--status-NO_SECKEY (context string
)
1532 (epg-context-set-result-for
1534 (cons (cons 'no-seckey string
)
1535 (epg-context-result-for context
'error
))))
1537 (defun epg--time-from-seconds (seconds)
1538 (let ((number-seconds (string-to-number (concat seconds
".0"))))
1539 (cons (floor (/ number-seconds
65536))
1540 (floor (mod number-seconds
65536)))))
1542 (defun epg--status-ERRSIG (context string
)
1543 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1544 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
1546 (let ((signature (epg-make-signature 'error
)))
1547 (epg-context-set-result-for
1551 (epg-context-result-for context
'verify
)))
1552 (epg-signature-set-key-id
1554 (match-string 1 string
))
1555 (epg-signature-set-pubkey-algorithm
1557 (string-to-number (match-string 2 string
)))
1558 (epg-signature-set-digest-algorithm
1560 (string-to-number (match-string 3 string
)))
1561 (epg-signature-set-class
1563 (string-to-number (match-string 4 string
) 16))
1564 (epg-signature-set-creation-time
1566 (epg--time-from-seconds (match-string 5 string
))))))
1568 (defun epg--status-VALIDSIG (context string
)
1569 (let ((signature (car (epg-context-result-for context
'verify
))))
1570 (when (and signature
1571 (eq (epg-signature-status signature
) 'good
)
1572 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
1573 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
1576 (epg-signature-set-fingerprint
1578 (match-string 1 string
))
1579 (epg-signature-set-creation-time
1581 (epg--time-from-seconds (match-string 2 string
)))
1582 (unless (equal (match-string 3 string
) "0")
1583 (epg-signature-set-expiration-time
1585 (epg--time-from-seconds (match-string 3 string
))))
1586 (epg-signature-set-version
1588 (string-to-number (match-string 4 string
)))
1589 (epg-signature-set-pubkey-algorithm
1591 (string-to-number (match-string 5 string
)))
1592 (epg-signature-set-digest-algorithm
1594 (string-to-number (match-string 6 string
)))
1595 (epg-signature-set-class
1597 (string-to-number (match-string 7 string
) 16)))))
1599 (defun epg--status-TRUST_UNDEFINED (context _string
)
1600 (let ((signature (car (epg-context-result-for context
'verify
))))
1602 (eq (epg-signature-status signature
) 'good
))
1603 (epg-signature-set-validity signature
'undefined
))))
1605 (defun epg--status-TRUST_NEVER (context _string
)
1606 (let ((signature (car (epg-context-result-for context
'verify
))))
1608 (eq (epg-signature-status signature
) 'good
))
1609 (epg-signature-set-validity signature
'never
))))
1611 (defun epg--status-TRUST_MARGINAL (context _string
)
1612 (let ((signature (car (epg-context-result-for context
'verify
))))
1614 (eq (epg-signature-status signature
) 'marginal
))
1615 (epg-signature-set-validity signature
'marginal
))))
1617 (defun epg--status-TRUST_FULLY (context _string
)
1618 (let ((signature (car (epg-context-result-for context
'verify
))))
1620 (eq (epg-signature-status signature
) 'good
))
1621 (epg-signature-set-validity signature
'full
))))
1623 (defun epg--status-TRUST_ULTIMATE (context _string
)
1624 (let ((signature (car (epg-context-result-for context
'verify
))))
1626 (eq (epg-signature-status signature
) 'good
))
1627 (epg-signature-set-validity signature
'ultimate
))))
1629 (defun epg--status-NOTATION_NAME (context string
)
1630 (let ((signature (car (epg-context-result-for context
'verify
))))
1632 (epg-signature-set-notations
1634 (cons (epg-make-sig-notation string nil t nil
)
1635 (epg-sig-notations signature
))))))
1637 (defun epg--status-NOTATION_DATA (context string
)
1638 (let ((signature (car (epg-context-result-for context
'verify
)))
1641 (setq notation
(car (epg-sig-notations signature
))))
1642 (epg-sig-notation-set-value notation string
))))
1644 (defun epg--status-POLICY_URL (context string
)
1645 (let ((signature (car (epg-context-result-for context
'verify
))))
1647 (epg-signature-set-notations
1649 (cons (epg-make-sig-notation nil string t nil
)
1650 (epg-sig-notations signature
))))))
1652 (defun epg--status-PROGRESS (context string
)
1653 (if (and (epg-context-progress-callback context
)
1654 (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1656 (funcall (car (epg-context-progress-callback context
))
1658 (match-string 1 string
)
1659 (match-string 2 string
)
1660 (string-to-number (match-string 3 string
))
1661 (string-to-number (match-string 4 string
))
1662 (cdr (epg-context-progress-callback context
)))))
1664 (defun epg--status-ENC_TO (context string
)
1665 (if (string-match "\\`\\([0-9A-Za-z]+\\) \\([0-9]+\\) \\([0-9]+\\)" string
)
1666 (epg-context-set-result-for
1667 context
'encrypted-to
1668 (cons (list (match-string 1 string
)
1669 (string-to-number (match-string 2 string
))
1670 (string-to-number (match-string 3 string
)))
1671 (epg-context-result-for context
'encrypted-to
)))))
1673 (defun epg--status-DECRYPTION_FAILED (context _string
)
1674 (epg-context-set-result-for context
'decryption-failed t
))
1676 (defun epg--status-DECRYPTION_OKAY (context _string
)
1677 (epg-context-set-result-for context
'decryption-okay t
))
1679 (defun epg--status-NODATA (context string
)
1680 (epg-context-set-result-for
1682 (cons (cons 'no-data
(string-to-number string
))
1683 (epg-context-result-for context
'error
))))
1685 (defun epg--status-UNEXPECTED (context string
)
1686 (epg-context-set-result-for
1688 (cons (cons 'unexpected
(string-to-number string
))
1689 (epg-context-result-for context
'error
))))
1691 (defun epg--status-KEYEXPIRED (context string
)
1692 (epg-context-set-result-for
1694 (cons (list 'key-expired
(cons 'expiration-time
1695 (epg--time-from-seconds string
)))
1696 (epg-context-result-for context
'key
))))
1698 (defun epg--status-KEYREVOKED (context _string
)
1699 (epg-context-set-result-for
1701 (cons '(key-revoked)
1702 (epg-context-result-for context
'key
))))
1704 (defun epg--status-BADARMOR (context _string
)
1705 (epg-context-set-result-for
1708 (epg-context-result-for context
'error
))))
1710 (defun epg--status-INV_RECP (context string
)
1711 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string
)
1712 (epg-context-set-result-for
1714 (cons (list 'invalid-recipient
1716 (string-to-number (match-string 1 string
)))
1718 (match-string 2 string
)))
1719 (epg-context-result-for context
'error
)))))
1721 (defun epg--status-INV_SGNR (context string
)
1722 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string
)
1723 (epg-context-set-result-for
1725 (cons (list 'invalid-signer
1727 (string-to-number (match-string 1 string
)))
1729 (match-string 2 string
)))
1730 (epg-context-result-for context
'error
)))))
1732 (defun epg--status-NO_RECP (context _string
)
1733 (epg-context-set-result-for
1735 (cons '(no-recipients)
1736 (epg-context-result-for context
'error
))))
1738 (defun epg--status-NO_SGNR (context _string
)
1739 (epg-context-set-result-for
1742 (epg-context-result-for context
'error
))))
1744 (defun epg--status-DELETE_PROBLEM (context string
)
1745 (if (string-match "\\`\\([0-9]+\\)" string
)
1746 (epg-context-set-result-for
1748 (cons (cons 'delete-problem
1749 (string-to-number (match-string 1 string
)))
1750 (epg-context-result-for context
'error
)))))
1752 (defun epg--status-SIG_CREATED (context string
)
1753 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1754 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string
)
1755 (epg-context-set-result-for
1757 (cons (epg-make-new-signature
1758 (cdr (assq (aref (match-string 1 string
) 0)
1759 epg-new-signature-type-alist
))
1760 (string-to-number (match-string 2 string
))
1761 (string-to-number (match-string 3 string
))
1762 (string-to-number (match-string 4 string
) 16)
1763 (epg--time-from-seconds (match-string 5 string
))
1764 (substring string
(match-end 0)))
1765 (epg-context-result-for context
'sign
)))))
1767 (defun epg--status-KEY_CREATED (context string
)
1768 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string
)
1769 (epg-context-set-result-for
1770 context
'generate-key
1771 (cons (list (cons 'type
(string-to-char (match-string 1 string
)))
1772 (cons 'fingerprint
(match-string 2 string
)))
1773 (epg-context-result-for context
'generate-key
)))))
1775 (defun epg--status-KEY_NOT_CREATED (context _string
)
1776 (epg-context-set-result-for
1778 (cons '(key-not-created)
1779 (epg-context-result-for context
'error
))))
1781 (defun epg--status-IMPORTED (_context string
)
1782 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string
)
1783 (let* ((key-id (match-string 1 string
))
1784 (user-id (match-string 2 string
))
1785 (entry (assoc key-id epg-user-id-alist
)))
1787 (setq user-id
(epg--decode-coding-string
1788 (epg--decode-percent-escape user-id
)
1792 (setcdr entry user-id
)
1793 (setq epg-user-id-alist
(cons (cons key-id user-id
)
1794 epg-user-id-alist
))))))
1796 (defun epg--status-IMPORT_OK (context string
)
1797 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string
)
1798 (let ((reason (string-to-number (match-string 1 string
))))
1799 (epg-context-set-result-for
1800 context
'import-status
1801 (cons (epg-make-import-status (if (match-beginning 2)
1802 (match-string 3 string
))
1804 (/= (logand reason
1) 0)
1805 (/= (logand reason
2) 0)
1806 (/= (logand reason
4) 0)
1807 (/= (logand reason
8) 0)
1808 (/= (logand reason
16) 0))
1809 (epg-context-result-for context
'import-status
))))))
1811 (defun epg--status-IMPORT_PROBLEM (context string
)
1812 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string
)
1813 (epg-context-set-result-for
1814 context
'import-status
1815 (cons (epg-make-import-status
1816 (if (match-beginning 2)
1817 (match-string 3 string
))
1818 (string-to-number (match-string 1 string
)))
1819 (epg-context-result-for context
'import-status
)))))
1821 (defun epg--status-IMPORT_RES (context string
)
1822 (when (string-match "\\`\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1823 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1824 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" string
)
1825 (epg-context-set-result-for
1827 (epg-make-import-result (string-to-number (match-string 1 string
))
1828 (string-to-number (match-string 2 string
))
1829 (string-to-number (match-string 3 string
))
1830 (string-to-number (match-string 4 string
))
1831 (string-to-number (match-string 5 string
))
1832 (string-to-number (match-string 6 string
))
1833 (string-to-number (match-string 7 string
))
1834 (string-to-number (match-string 8 string
))
1835 (string-to-number (match-string 9 string
))
1836 (string-to-number (match-string 10 string
))
1837 (string-to-number (match-string 11 string
))
1838 (string-to-number (match-string 12 string
))
1839 (string-to-number (match-string 13 string
))
1840 (epg-context-result-for context
'import-status
)))
1841 (epg-context-set-result-for context
'import-status nil
)))
1843 (defun epg-passphrase-callback-function (context key-id _handback
)
1844 (declare (obsolete epa-passphrase-callback-function
"23.1"))
1845 (if (eq key-id
'SYM
)
1846 (read-passwd "Passphrase for symmetric encryption: "
1847 (eq (epg-context-operation context
) 'encrypt
))
1849 (if (eq key-id
'PIN
)
1850 "Passphrase for PIN: "
1851 (let ((entry (assoc key-id epg-user-id-alist
)))
1853 (format "Passphrase for %s %s: " key-id
(cdr entry
))
1854 (format "Passphrase for %s: " key-id
)))))))
1856 (defun epg--list-keys-1 (context name mode
)
1857 (let ((args (append (if epg-gpg-home-directory
1858 (list "--homedir" epg-gpg-home-directory
))
1859 '("--with-colons" "--no-greeting" "--batch"
1860 "--with-fingerprint" "--with-fingerprint")
1861 (unless (eq (epg-context-protocol context
) 'CMS
)
1862 '("--fixed-list-mode"))))
1863 (list-keys-option (if (memq mode
'(t secret
))
1864 "--list-secret-keys"
1865 (if (memq mode
'(nil public
))
1868 (coding-system-for-read 'binary
)
1869 keys string field index
)
1872 (unless (listp name
)
1873 (setq name
(list name
)))
1875 (setq args
(append args
(list list-keys-option
(car name
)))
1877 (setq args
(append args
(list list-keys-option
))))
1879 (apply #'call-process
1880 (if (eq (epg-context-protocol context
) 'CMS
)
1883 nil
(list t nil
) nil args
)
1884 (goto-char (point-min))
1885 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t
)
1886 (setq keys
(cons (make-vector 15 nil
) keys
)
1887 string
(match-string 0)
1891 (string-match "\\([^:]+\\)?:" string index
))
1892 (setq index
(match-end 0))
1893 (aset (car keys
) field
(match-string 1 string
))
1894 (setq field
(1+ field
))))
1897 (defun epg--make-sub-key-1 (line)
1900 (cdr (assq (string-to-char (aref line
1)) epg-key-validity-alist
)))
1902 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist
)))
1904 (member (aref line
0) '("sec" "ssb"))
1905 (string-to-number (aref line
3))
1906 (string-to-number (aref line
2))
1908 (epg--time-from-seconds (aref line
5))
1910 (epg--time-from-seconds (aref line
6)))))
1912 (defun epg-list-keys (context &optional name mode
)
1913 "Return a list of epg-key objects matched with NAME.
1914 If MODE is nil or 'public, only public keyring should be searched.
1915 If MODE is t or 'secret, only secret keyring should be searched.
1916 Otherwise, only public keyring should be searched and the key
1917 signatures should be included.
1918 NAME is either a string or a list of strings."
1919 (let ((lines (epg--list-keys-1 context name mode
))
1920 keys cert pointer pointer-1 index string
)
1923 ((member (aref (car lines
) 0) '("pub" "sec" "crt" "crs"))
1924 (setq cert
(member (aref (car lines
) 0) '("crt" "crs"))
1925 keys
(cons (epg-make-key
1926 (if (aref (car lines
) 8)
1927 (cdr (assq (string-to-char (aref (car lines
) 8))
1928 epg-key-validity-alist
))))
1930 (epg-key-set-sub-key-list
1932 (cons (epg--make-sub-key-1 (car lines
))
1933 (epg-key-sub-key-list (car keys
)))))
1934 ((member (aref (car lines
) 0) '("sub" "ssb"))
1935 (epg-key-set-sub-key-list
1937 (cons (epg--make-sub-key-1 (car lines
))
1938 (epg-key-sub-key-list (car keys
)))))
1939 ((equal (aref (car lines
) 0) "uid")
1940 ;; Decode the UID name as a backslash escaped UTF-8 string,
1941 ;; generated by GnuPG/GpgSM.
1942 (setq string
(copy-sequence (aref (car lines
) 9))
1944 (while (string-match "\"" string index
)
1945 (setq string
(replace-match "\\\"" t t string
)
1946 index
(1+ (match-end 0))))
1948 (setq string
(epg--decode-coding-string
1949 (car (read-from-string (concat "\"" string
"\"")))
1952 (setq string
(aref (car lines
) 9))))
1953 (epg-key-set-user-id-list
1955 (cons (epg-make-user-id
1956 (if (aref (car lines
) 1)
1957 (cdr (assq (string-to-char (aref (car lines
) 1))
1958 epg-key-validity-alist
)))
1961 (epg-dn-from-string string
)
1964 (epg-key-user-id-list (car keys
)))))
1965 ((equal (aref (car lines
) 0) "fpr")
1966 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys
)))
1967 (aref (car lines
) 9)))
1968 ((equal (aref (car lines
) 0) "sig")
1969 (epg-user-id-set-signature-list
1970 (car (epg-key-user-id-list (car keys
)))
1972 (epg-make-key-signature
1973 (if (aref (car lines
) 1)
1974 (cdr (assq (string-to-char (aref (car lines
) 1))
1975 epg-key-validity-alist
)))
1976 (string-to-number (aref (car lines
) 3))
1977 (aref (car lines
) 4)
1978 (epg--time-from-seconds (aref (car lines
) 5))
1979 (epg--time-from-seconds (aref (car lines
) 6))
1980 (aref (car lines
) 9)
1981 (string-to-number (aref (car lines
) 10) 16)
1982 (eq (aref (aref (car lines
) 10) 2) ?x
))
1983 (epg-user-id-signature-list
1984 (car (epg-key-user-id-list (car keys
))))))))
1985 (setq lines
(cdr lines
)))
1986 (setq keys
(nreverse keys
)
1989 (epg-key-set-sub-key-list
1991 (nreverse (epg-key-sub-key-list (car pointer
))))
1992 (setq pointer-1
(epg-key-set-user-id-list
1994 (nreverse (epg-key-user-id-list (car pointer
)))))
1996 (epg-user-id-set-signature-list
1998 (nreverse (epg-user-id-signature-list (car pointer-1
))))
1999 (setq pointer-1
(cdr pointer-1
)))
2000 (setq pointer
(cdr pointer
)))
2004 (if (fboundp 'make-temp-file
)
2005 (defalias 'epg--make-temp-file
'make-temp-file
)
2006 (defvar temporary-file-directory
)
2007 ;; stolen from poe.el.
2008 (defun epg--make-temp-file (prefix)
2009 "Create a temporary file.
2010 The returned file name (created by appending some random characters at the end
2011 of PREFIX, and expanding against `temporary-file-directory' if necessary),
2012 is guaranteed to point to a newly created empty file.
2013 You can then use `write-region' to write new data into the file."
2014 (let ((orig-modes (default-file-modes))
2016 (setq prefix
(expand-file-name prefix
2017 (if (featurep 'xemacs
)
2019 temporary-file-directory
)))
2022 ;; First, create a temporary directory.
2023 (set-default-file-modes #o700
)
2024 (while (condition-case ()
2026 (setq tempdir
(make-temp-name
2028 (file-name-directory prefix
)
2030 ;; return nil or signal an error.
2031 (make-directory tempdir
))
2033 (file-already-exists t
)))
2034 ;; Second, create a temporary file in the tempdir.
2035 ;; There *is* a race condition between `make-temp-name'
2036 ;; and `write-region', but we don't care it since we are
2037 ;; in a private directory now.
2038 (setq tempfile
(make-temp-name (concat tempdir
"/EMU")))
2039 (write-region "" nil tempfile nil
'silent
)
2040 ;; Finally, make a hard-link from the tempfile.
2041 (while (condition-case ()
2043 (setq file
(make-temp-name prefix
))
2044 ;; return nil or signal an error.
2045 (add-name-to-file tempfile file
))
2047 (file-already-exists t
)))
2049 (set-default-file-modes orig-modes
)
2050 ;; Cleanup the tempfile.
2052 (file-exists-p tempfile
)
2053 (delete-file tempfile
))
2054 ;; Cleanup the tempdir.
2056 (file-directory-p tempdir
)
2057 (delete-directory tempdir
)))))))
2059 (defun epg--args-from-sig-notations (notations)
2063 (if (and (epg-sig-notation-name notation
)
2064 (not (epg-sig-notation-human-readable notation
)))
2065 (error "Unreadable"))
2066 (if (epg-sig-notation-name notation
)
2067 (list "--sig-notation"
2068 (if (epg-sig-notation-critical notation
)
2069 (concat "!" (epg-sig-notation-name notation
)
2070 "=" (epg-sig-notation-value notation
))
2071 (concat (epg-sig-notation-name notation
)
2072 "=" (epg-sig-notation-value notation
))))
2073 (list "--sig-policy-url"
2074 (if (epg-sig-notation-critical notation
)
2075 (concat "!" (epg-sig-notation-value notation
))
2076 (epg-sig-notation-value notation
)))))
2079 (defun epg-cancel (context)
2080 (if (buffer-live-p (process-buffer (epg-context-process context
)))
2081 (with-current-buffer (process-buffer (epg-context-process context
))
2082 (epg-context-set-result-for
2085 (epg-context-result-for epg-context
'error
)))))
2086 (if (eq (process-status (epg-context-process context
)) 'run
)
2087 (delete-process (epg-context-process context
))))
2089 (defun epg-start-decrypt (context cipher
)
2090 "Initiate a decrypt operation on CIPHER.
2091 CIPHER must be a file data object.
2093 If you use this function, you will need to wait for the completion of
2094 `epg-gpg-program' by using `epg-wait-for-completion' and call
2095 `epg-reset' to clear a temporary output file.
2096 If you are unsure, use synchronous version of this function
2097 `epg-decrypt-file' or `epg-decrypt-string' instead."
2098 (unless (epg-data-file cipher
)
2099 (error "Not a file"))
2100 (epg-context-set-operation context
'decrypt
)
2101 (epg-context-set-result context nil
)
2102 (epg--start context
(list "--decrypt" "--" (epg-data-file cipher
)))
2103 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2104 (unless (eq (epg-context-protocol context
) 'CMS
)
2105 (epg-wait-for-status context
'("BEGIN_DECRYPTION"))))
2107 (defun epg--check-error-for-decrypt (context)
2108 (let ((errors (epg-context-result-for context
'error
)))
2109 (if (epg-context-result-for context
'decryption-failed
)
2111 (list "Decryption failed" (epg-errors-to-string errors
))))
2112 (unless (epg-context-result-for context
'decryption-okay
)
2114 (list "Can't decrypt" (epg-errors-to-string errors
))))))
2116 (defun epg-decrypt-file (context cipher plain
)
2117 "Decrypt a file CIPHER and store the result to a file PLAIN.
2118 If PLAIN is nil, it returns the result as a string."
2122 (epg-context-set-output-file context plain
)
2123 (epg-context-set-output-file context
2124 (epg--make-temp-file "epg-output")))
2125 (epg-start-decrypt context
(epg-make-data-from-file cipher
))
2126 (epg-wait-for-completion context
)
2127 (epg--check-error-for-decrypt context
)
2129 (epg-read-output context
)))
2131 (epg-delete-output-file context
))
2132 (epg-reset context
)))
2134 (defun epg-decrypt-string (context cipher
)
2135 "Decrypt a string CIPHER and return the plain text."
2136 (let ((input-file (epg--make-temp-file "epg-input"))
2137 (coding-system-for-write 'binary
))
2140 (write-region cipher nil input-file nil
'quiet
)
2141 (epg-context-set-output-file context
2142 (epg--make-temp-file "epg-output"))
2143 (epg-start-decrypt context
(epg-make-data-from-file input-file
))
2144 (epg-wait-for-completion context
)
2145 (epg--check-error-for-decrypt context
)
2146 (epg-read-output context
))
2147 (epg-delete-output-file context
)
2148 (if (file-exists-p input-file
)
2149 (delete-file input-file
))
2150 (epg-reset context
))))
2152 (defun epg-start-verify (context signature
&optional signed-text
)
2153 "Initiate a verify operation on SIGNATURE.
2154 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
2156 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
2157 For a normal or a cleartext signature, SIGNED-TEXT should be nil.
2159 If you use this function, you will need to wait for the completion of
2160 `epg-gpg-program' by using `epg-wait-for-completion' and call
2161 `epg-reset' to clear a temporary output file.
2162 If you are unsure, use synchronous version of this function
2163 `epg-verify-file' or `epg-verify-string' instead."
2164 (epg-context-set-operation context
'verify
)
2165 (epg-context-set-result context nil
)
2167 ;; Detached signature.
2168 (if (epg-data-file signed-text
)
2169 (epg--start context
(list "--verify" "--" (epg-data-file signature
)
2170 (epg-data-file signed-text
)))
2171 (epg--start context
(list "--verify" "--" (epg-data-file signature
)
2173 (if (eq (process-status (epg-context-process context
)) 'run
)
2174 (process-send-string (epg-context-process context
)
2175 (epg-data-string signed-text
)))
2176 (if (eq (process-status (epg-context-process context
)) 'run
)
2177 (process-send-eof (epg-context-process context
))))
2178 ;; Normal (or cleartext) signature.
2179 (if (epg-data-file signature
)
2180 (epg--start context
(if (eq (epg-context-protocol context
) 'CMS
)
2181 (list "--verify" "--" (epg-data-file signature
))
2182 (list "--" (epg-data-file signature
))))
2183 (epg--start context
(if (eq (epg-context-protocol context
) 'CMS
)
2186 (if (eq (process-status (epg-context-process context
)) 'run
)
2187 (process-send-string (epg-context-process context
)
2188 (epg-data-string signature
)))
2189 (if (eq (process-status (epg-context-process context
)) 'run
)
2190 (process-send-eof (epg-context-process context
))))))
2192 (defun epg-verify-file (context signature
&optional signed-text plain
)
2193 "Verify a file SIGNATURE.
2194 SIGNED-TEXT and PLAIN are also a file if they are specified.
2196 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
2197 string. For a normal or a cleartext signature, SIGNED-TEXT should be
2198 nil. In the latter case, if PLAIN is specified, the plaintext is
2199 stored into the file after successful verification."
2203 (epg-context-set-output-file context plain
)
2204 (epg-context-set-output-file context
2205 (epg--make-temp-file "epg-output")))
2207 (epg-start-verify context
2208 (epg-make-data-from-file signature
)
2209 (epg-make-data-from-file signed-text
))
2210 (epg-start-verify context
2211 (epg-make-data-from-file signature
)))
2212 (epg-wait-for-completion context
)
2214 (epg-read-output context
)))
2216 (epg-delete-output-file context
))
2217 (epg-reset context
)))
2219 (defun epg-verify-string (context signature
&optional signed-text
)
2220 "Verify a string SIGNATURE.
2221 SIGNED-TEXT is a string if it is specified.
2223 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
2224 string. For a normal or a cleartext signature, SIGNED-TEXT should be
2225 nil. In the latter case, this function returns the plaintext after
2226 successful verification."
2227 (let ((coding-system-for-write 'binary
)
2231 (epg-context-set-output-file context
2232 (epg--make-temp-file "epg-output"))
2235 (setq input-file
(epg--make-temp-file "epg-signature"))
2236 (write-region signature nil input-file nil
'quiet
)
2237 (epg-start-verify context
2238 (epg-make-data-from-file input-file
)
2239 (epg-make-data-from-string signed-text
)))
2240 (epg-start-verify context
(epg-make-data-from-string signature
)))
2241 (epg-wait-for-completion context
)
2242 (epg-read-output context
))
2243 (epg-delete-output-file context
)
2245 (file-exists-p input-file
))
2246 (delete-file input-file
))
2247 (epg-reset context
))))
2249 (defun epg-start-sign (context plain
&optional mode
)
2250 "Initiate a sign operation on PLAIN.
2251 PLAIN is a data object.
2253 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2254 If it is nil or 'normal, it makes a normal signature.
2255 Otherwise, it makes a cleartext signature.
2257 If you use this function, you will need to wait for the completion of
2258 `epg-gpg-program' by using `epg-wait-for-completion' and call
2259 `epg-reset' to clear a temporary output file.
2260 If you are unsure, use synchronous version of this function
2261 `epg-sign-file' or `epg-sign-string' instead."
2262 (epg-context-set-operation context
'sign
)
2263 (epg-context-set-result context nil
)
2264 (unless (memq mode
'(t detached nil normal
)) ;i.e. cleartext
2265 (epg-context-set-armor context nil
)
2266 (epg-context-set-textmode context nil
))
2268 (append (list (if (memq mode
'(t detached
))
2270 (if (memq mode
'(nil normal
))
2278 (car (epg-key-sub-key-list signer
)))))
2279 (epg-context-signers context
)))
2280 (epg--args-from-sig-notations
2281 (epg-context-sig-notations context
))
2282 (if (epg-data-file plain
)
2283 (list "--" (epg-data-file plain
)))))
2284 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2285 (unless (eq (epg-context-protocol context
) 'CMS
)
2286 (epg-wait-for-status context
'("BEGIN_SIGNING")))
2287 (when (epg-data-string plain
)
2288 (if (eq (process-status (epg-context-process context
)) 'run
)
2289 (process-send-string (epg-context-process context
)
2290 (epg-data-string plain
)))
2291 (if (eq (process-status (epg-context-process context
)) 'run
)
2292 (process-send-eof (epg-context-process context
)))))
2294 (defun epg-sign-file (context plain signature
&optional mode
)
2295 "Sign a file PLAIN and store the result to a file SIGNATURE.
2296 If SIGNATURE is nil, it returns the result as a string.
2297 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2298 If it is nil or 'normal, it makes a normal signature.
2299 Otherwise, it makes a cleartext signature."
2303 (epg-context-set-output-file context signature
)
2304 (epg-context-set-output-file context
2305 (epg--make-temp-file "epg-output")))
2306 (epg-start-sign context
(epg-make-data-from-file plain
) mode
)
2307 (epg-wait-for-completion context
)
2308 (unless (epg-context-result-for context
'sign
)
2309 (let ((errors (epg-context-result-for context
'error
)))
2311 (list "Sign failed" (epg-errors-to-string errors
)))))
2313 (epg-read-output context
)))
2315 (epg-delete-output-file context
))
2316 (epg-reset context
)))
2318 (defun epg-sign-string (context plain
&optional mode
)
2319 "Sign a string PLAIN and return the output as string.
2320 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2321 If it is nil or 'normal, it makes a normal signature.
2322 Otherwise, it makes a cleartext signature."
2324 (unless (or (eq (epg-context-protocol context
) 'CMS
)
2327 (epg-check-configuration (epg-configuration))
2330 (epg--make-temp-file "epg-input")))
2331 (coding-system-for-write 'binary
))
2334 (epg-context-set-output-file context
2335 (epg--make-temp-file "epg-output"))
2337 (write-region plain nil input-file nil
'quiet
))
2338 (epg-start-sign context
2340 (epg-make-data-from-file input-file
)
2341 (epg-make-data-from-string plain
))
2343 (epg-wait-for-completion context
)
2344 (unless (epg-context-result-for context
'sign
)
2345 (if (epg-context-result-for context
'error
)
2346 (let ((errors (epg-context-result-for context
'error
)))
2348 (list "Sign failed" (epg-errors-to-string errors
))))))
2349 (epg-read-output context
))
2350 (epg-delete-output-file context
)
2352 (delete-file input-file
))
2353 (epg-reset context
))))
2355 (defun epg-start-encrypt (context plain recipients
2356 &optional sign always-trust
)
2357 "Initiate an encrypt operation on PLAIN.
2358 PLAIN is a data object.
2359 If RECIPIENTS is nil, it performs symmetric encryption.
2361 If you use this function, you will need to wait for the completion of
2362 `epg-gpg-program' by using `epg-wait-for-completion' and call
2363 `epg-reset' to clear a temporary output file.
2364 If you are unsure, use synchronous version of this function
2365 `epg-encrypt-file' or `epg-encrypt-string' instead."
2366 (epg-context-set-operation context
'encrypt
)
2367 (epg-context-set-result context nil
)
2369 (append (if always-trust
'("--always-trust"))
2370 (if recipients
'("--encrypt") '("--symmetric"))
2371 (if sign
'("--sign"))
2378 (car (epg-key-sub-key-list
2380 (epg-context-signers context
))))
2382 (epg--args-from-sig-notations
2383 (epg-context-sig-notations context
)))
2389 (car (epg-key-sub-key-list recipient
)))))
2391 (if (epg-data-file plain
)
2392 (list "--" (epg-data-file plain
)))))
2393 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2394 (unless (eq (epg-context-protocol context
) 'CMS
)
2396 (epg-wait-for-status context
'("BEGIN_SIGNING"))
2397 (epg-wait-for-status context
'("BEGIN_ENCRYPTION"))))
2398 (when (epg-data-string plain
)
2399 (if (eq (process-status (epg-context-process context
)) 'run
)
2400 (process-send-string (epg-context-process context
)
2401 (epg-data-string plain
)))
2402 (if (eq (process-status (epg-context-process context
)) 'run
)
2403 (process-send-eof (epg-context-process context
)))))
2405 (defun epg-encrypt-file (context plain recipients
2406 cipher
&optional sign always-trust
)
2407 "Encrypt a file PLAIN and store the result to a file CIPHER.
2408 If CIPHER is nil, it returns the result as a string.
2409 If RECIPIENTS is nil, it performs symmetric encryption."
2413 (epg-context-set-output-file context cipher
)
2414 (epg-context-set-output-file context
2415 (epg--make-temp-file "epg-output")))
2416 (epg-start-encrypt context
(epg-make-data-from-file plain
)
2417 recipients sign always-trust
)
2418 (epg-wait-for-completion context
)
2419 (let ((errors (epg-context-result-for context
'error
)))
2421 (not (epg-context-result-for context
'sign
)))
2423 (list "Sign failed" (epg-errors-to-string errors
))))
2426 (list "Encrypt failed" (epg-errors-to-string errors
)))))
2428 (epg-read-output context
)))
2430 (epg-delete-output-file context
))
2431 (epg-reset context
)))
2433 (defun epg-encrypt-string (context plain recipients
2434 &optional sign always-trust
)
2435 "Encrypt a string PLAIN.
2436 If RECIPIENTS is nil, it performs symmetric encryption."
2438 (unless (or (not sign
)
2439 (eq (epg-context-protocol context
) 'CMS
)
2442 (epg-check-configuration (epg-configuration))
2445 (epg--make-temp-file "epg-input")))
2446 (coding-system-for-write 'binary
))
2449 (epg-context-set-output-file context
2450 (epg--make-temp-file "epg-output"))
2452 (write-region plain nil input-file nil
'quiet
))
2453 (epg-start-encrypt context
2455 (epg-make-data-from-file input-file
)
2456 (epg-make-data-from-string plain
))
2457 recipients sign always-trust
)
2458 (epg-wait-for-completion context
)
2459 (let ((errors (epg-context-result-for context
'error
)))
2461 (not (epg-context-result-for context
'sign
)))
2463 (list "Sign failed" (epg-errors-to-string errors
))))
2466 (list "Encrypt failed" (epg-errors-to-string errors
)))))
2467 (epg-read-output context
))
2468 (epg-delete-output-file context
)
2470 (delete-file input-file
))
2471 (epg-reset context
))))
2473 (defun epg-start-export-keys (context keys
)
2474 "Initiate an export keys operation.
2476 If you use this function, you will need to wait for the completion of
2477 `epg-gpg-program' by using `epg-wait-for-completion' and call
2478 `epg-reset' to clear a temporary output file.
2479 If you are unsure, use synchronous version of this function
2480 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
2481 (epg-context-set-operation context
'export-keys
)
2482 (epg-context-set-result context nil
)
2483 (epg--start context
(cons "--export"
2487 (car (epg-key-sub-key-list key
))))
2490 (defun epg-export-keys-to-file (context keys file
)
2491 "Extract public KEYS."
2495 (epg-context-set-output-file context file
)
2496 (epg-context-set-output-file context
2497 (epg--make-temp-file "epg-output")))
2498 (epg-start-export-keys context keys
)
2499 (epg-wait-for-completion context
)
2500 (let ((errors (epg-context-result-for context
'error
)))
2503 (list "Export keys failed"
2504 (epg-errors-to-string errors
)))))
2506 (epg-read-output context
)))
2508 (epg-delete-output-file context
))
2509 (epg-reset context
)))
2511 (defun epg-export-keys-to-string (context keys
)
2512 "Extract public KEYS and return them as a string."
2513 (epg-export-keys-to-file context keys nil
))
2515 (defun epg-start-import-keys (context keys
)
2516 "Initiate an import keys operation.
2517 KEYS is a data object.
2519 If you use this function, you will need to wait for the completion of
2520 `epg-gpg-program' by using `epg-wait-for-completion' and call
2521 `epg-reset' to clear a temporary output file.
2522 If you are unsure, use synchronous version of this function
2523 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
2524 (epg-context-set-operation context
'import-keys
)
2525 (epg-context-set-result context nil
)
2526 (epg--start context
(if (epg-data-file keys
)
2527 (list "--import" "--" (epg-data-file keys
))
2529 (when (epg-data-string keys
)
2530 (if (eq (process-status (epg-context-process context
)) 'run
)
2531 (process-send-string (epg-context-process context
)
2532 (epg-data-string keys
)))
2533 (if (eq (process-status (epg-context-process context
)) 'run
)
2534 (process-send-eof (epg-context-process context
)))))
2536 (defun epg--import-keys-1 (context keys
)
2539 (epg-start-import-keys context keys
)
2540 (epg-wait-for-completion context
)
2541 (let ((errors (epg-context-result-for context
'error
)))
2544 (list "Import keys failed"
2545 (epg-errors-to-string errors
))))))
2546 (epg-reset context
)))
2548 (defun epg-import-keys-from-file (context keys
)
2549 "Add keys from a file KEYS."
2550 (epg--import-keys-1 context
(epg-make-data-from-file keys
)))
2552 (defun epg-import-keys-from-string (context keys
)
2553 "Add keys from a string KEYS."
2554 (epg--import-keys-1 context
(epg-make-data-from-string keys
)))
2556 (defun epg-start-receive-keys (context key-id-list
)
2557 "Initiate a receive key operation.
2558 KEY-ID-LIST is a list of key IDs.
2560 If you use this function, you will need to wait for the completion of
2561 `epg-gpg-program' by using `epg-wait-for-completion' and call
2562 `epg-reset' to clear a temporary output file.
2563 If you are unsure, use synchronous version of this function
2564 `epg-receive-keys' instead."
2565 (epg-context-set-operation context
'receive-keys
)
2566 (epg-context-set-result context nil
)
2567 (epg--start context
(cons "--recv-keys" key-id-list
)))
2569 (defun epg-receive-keys (context keys
)
2570 "Add keys from server.
2571 KEYS is a list of key IDs"
2574 (epg-start-receive-keys context keys
)
2575 (epg-wait-for-completion context
)
2576 (let ((errors (epg-context-result-for context
'error
)))
2579 (list "Receive keys failed"
2580 (epg-errors-to-string errors
))))))
2581 (epg-reset context
)))
2583 (defalias 'epg-import-keys-from-server
'epg-receive-keys
)
2585 (defun epg-start-delete-keys (context keys
&optional allow-secret
)
2586 "Initiate a delete keys operation.
2588 If you use this function, you will need to wait for the completion of
2589 `epg-gpg-program' by using `epg-wait-for-completion' and call
2590 `epg-reset' to clear a temporary output file.
2591 If you are unsure, use synchronous version of this function
2592 `epg-delete-keys' instead."
2593 (epg-context-set-operation context
'delete-keys
)
2594 (epg-context-set-result context nil
)
2595 (epg--start context
(cons (if allow-secret
2596 "--delete-secret-key"
2601 (car (epg-key-sub-key-list key
))))
2604 (defun epg-delete-keys (context keys
&optional allow-secret
)
2605 "Delete KEYS from the key ring."
2608 (epg-start-delete-keys context keys allow-secret
)
2609 (epg-wait-for-completion context
)
2610 (let ((errors (epg-context-result-for context
'error
)))
2613 (list "Delete keys failed"
2614 (epg-errors-to-string errors
))))))
2615 (epg-reset context
)))
2617 (defun epg-start-sign-keys (context keys
&optional local
)
2618 "Initiate a sign keys operation.
2620 If you use this function, you will need to wait for the completion of
2621 `epg-gpg-program' by using `epg-wait-for-completion' and call
2622 `epg-reset' to clear a temporary output file.
2623 If you are unsure, use synchronous version of this function
2624 `epg-sign-keys' instead."
2625 (declare (obsolete nil
"23.1"))
2626 (epg-context-set-operation context
'sign-keys
)
2627 (epg-context-set-result context nil
)
2628 (epg--start context
(cons (if local
2634 (car (epg-key-sub-key-list key
))))
2637 (defun epg-sign-keys (context keys
&optional local
)
2638 "Sign KEYS from the key ring."
2639 (declare (obsolete nil
"23.1"))
2642 (epg-start-sign-keys context keys local
)
2643 (epg-wait-for-completion context
)
2644 (let ((errors (epg-context-result-for context
'error
)))
2647 (list "Sign keys failed"
2648 (epg-errors-to-string errors
))))))
2649 (epg-reset context
)))
2651 (defun epg-start-generate-key (context parameters
)
2652 "Initiate a key generation.
2653 PARAMETERS specifies parameters for the key.
2655 If you use this function, you will need to wait for the completion of
2656 `epg-gpg-program' by using `epg-wait-for-completion' and call
2657 `epg-reset' to clear a temporary output file.
2658 If you are unsure, use synchronous version of this function
2659 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2660 (epg-context-set-operation context
'generate-key
)
2661 (epg-context-set-result context nil
)
2662 (if (epg-data-file parameters
)
2663 (epg--start context
(list "--batch" "--genkey" "--"
2664 (epg-data-file parameters
)))
2665 (epg--start context
'("--batch" "--genkey"))
2666 (if (eq (process-status (epg-context-process context
)) 'run
)
2667 (process-send-string (epg-context-process context
)
2668 (epg-data-string parameters
)))
2669 (if (eq (process-status (epg-context-process context
)) 'run
)
2670 (process-send-eof (epg-context-process context
)))))
2672 (defun epg-generate-key-from-file (context parameters
)
2673 "Generate a new key pair.
2674 PARAMETERS is a file which tells how to create the key."
2677 (epg-start-generate-key context
(epg-make-data-from-file parameters
))
2678 (epg-wait-for-completion context
)
2679 (let ((errors (epg-context-result-for context
'error
)))
2682 (list "Generate key failed"
2683 (epg-errors-to-string errors
))))))
2684 (epg-reset context
)))
2686 (defun epg-generate-key-from-string (context parameters
)
2687 "Generate a new key pair.
2688 PARAMETERS is a string which tells how to create the key."
2691 (epg-start-generate-key context
(epg-make-data-from-string parameters
))
2692 (epg-wait-for-completion context
)
2693 (let ((errors (epg-context-result-for context
'error
)))
2696 (list "Generate key failed"
2697 (epg-errors-to-string errors
))))))
2698 (epg-reset context
)))
2700 (defun epg--decode-percent-escape (string)
2702 (while (string-match "%\\(\\(%\\)\\|\\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2704 (if (match-beginning 2)
2705 (setq string
(replace-match "%" t t string
)
2706 index
(1- (match-end 0)))
2707 (setq string
(replace-match
2708 (string (string-to-number (match-string 3 string
) 16))
2710 index
(- (match-end 0) 2))))
2713 (defun epg--decode-hexstring (string)
2715 (while (eq index
(string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index
))
2716 (setq string
(replace-match (string (string-to-number
2717 (match-string 0 string
) 16))
2719 index
(1- (match-end 0))))
2722 (defun epg--decode-quotedstring (string)
2724 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2725 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2727 (if (match-beginning 2)
2728 (setq string
(replace-match "\\2" t nil string
)
2729 index
(1- (match-end 0)))
2730 (if (match-beginning 3)
2731 (setq string
(replace-match (string (string-to-number
2732 (match-string 0 string
) 16))
2734 index
(- (match-end 0) 2)))))
2737 (defun epg-dn-from-string (string)
2738 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2739 The return value is an alist mapping from types to values."
2741 (length (length string
))
2742 alist type value group
)
2743 (while (< index length
)
2744 (if (eq index
(string-match "[ \t\n\r]*" string index
))
2745 (setq index
(match-end 0)))
2746 (if (eq index
(string-match
2747 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2749 (setq type
(match-string 1 string
)
2750 index
(match-end 0))
2751 (if (eq index
(string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2753 (setq type
(match-string 1 string
)
2754 index
(match-end 0))))
2756 (error "Invalid type"))
2757 (if (eq index
(string-match
2758 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2760 (setq index
(match-end 0)
2761 value
(epg--decode-quotedstring (match-string 0 string
)))
2762 (if (eq index
(string-match "#\\([0-9A-Fa-f]+\\)" string index
))
2763 (setq index
(match-end 0)
2764 value
(epg--decode-hexstring (match-string 1 string
)))
2765 (if (eq index
(string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2767 (setq index
(match-end 0)
2768 value
(epg--decode-quotedstring
2769 (match-string 0 string
))))))
2771 (if (stringp (car (car alist
)))
2772 (setcar alist
(list (cons type value
) (car alist
)))
2773 (setcar alist
(cons (cons type value
) (car alist
))))
2774 (if (consp (car (car alist
)))
2775 (setcar alist
(nreverse (car alist
))))
2776 (setq alist
(cons (cons type value
) alist
)
2779 (if (eq index
(string-match "[ \t\n\r]*\\([,;+]\\)" string index
))
2780 (setq index
(match-end 0)
2781 group
(eq (aref string
(match-beginning 1)) ?
+))))
2784 (defun epg-decode-dn (alist)
2785 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2786 Type names are resolved using `epg-dn-type-alist'."
2789 (if (stringp (car rdn
))
2790 (let ((entry (assoc (car rdn
) epg-dn-type-alist
)))
2792 (format "%s=%s" (cdr entry
) (cdr rdn
))
2793 (format "%s=%s" (car rdn
) (cdr rdn
))))
2794 (concat "(" (epg-decode-dn rdn
) ")")))
2800 ;;; epg.el ends here