1 ;;; epg.el --- the EasyPG Library -*- lexical-binding: t -*-
2 ;; Copyright (C) 1999-2000, 2002-2012 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)))
200 (defun epg-context-protocol (context)
201 "Return the protocol used within CONTEXT."
202 (unless (eq (car-safe context) 'epg-context)
203 (signal 'wrong-type-argument (list 'epg-context-p context)))
204 (aref (cdr context) 0))
206 (defun epg-context-armor (context)
207 "Return t if the output should be ASCII armored in CONTEXT."
208 (unless (eq (car-safe context) 'epg-context)
209 (signal 'wrong-type-argument (list 'epg-context-p context)))
210 (aref (cdr context) 1))
212 (defun epg-context-textmode (context)
213 "Return t if canonical text mode should be used in CONTEXT."
214 (unless (eq (car-safe context) 'epg-context)
215 (signal 'wrong-type-argument (list 'epg-context-p context)))
216 (aref (cdr context) 2))
218 (defun epg-context-include-certs (context)
219 "Return how many certificates should be included in an S/MIME signed message."
220 (unless (eq (car-safe context) 'epg-context)
221 (signal 'wrong-type-argument (list 'epg-context-p context)))
222 (aref (cdr context) 3))
224 (defun epg-context-cipher-algorithm (context)
225 "Return the cipher algorithm in CONTEXT."
226 (unless (eq (car-safe context) 'epg-context)
227 (signal 'wrong-type-argument (list 'epg-context-p context)))
228 (aref (cdr context) 4))
230 (defun epg-context-digest-algorithm (context)
231 "Return the digest algorithm in CONTEXT."
232 (unless (eq (car-safe context) 'epg-context)
233 (signal 'wrong-type-argument (list 'epg-context-p context)))
234 (aref (cdr context) 5))
236 (defun epg-context-compress-algorithm (context)
237 "Return the compress algorithm in CONTEXT."
238 (unless (eq (car-safe context) 'epg-context)
239 (signal 'wrong-type-argument (list 'epg-context-p context)))
240 (aref (cdr context) 6))
242 (defun epg-context-passphrase-callback (context)
243 "Return the function used to query passphrase."
244 (unless (eq (car-safe context) 'epg-context)
245 (signal 'wrong-type-argument (list 'epg-context-p context)))
246 (aref (cdr context) 7))
248 (defun epg-context-progress-callback (context)
249 "Return the function which handles progress update."
250 (unless (eq (car-safe context) 'epg-context)
251 (signal 'wrong-type-argument (list 'epg-context-p context)))
252 (aref (cdr context) 8))
254 (defun epg-context-signers (context)
255 "Return the list of key-id for signing."
256 (unless (eq (car-safe context) 'epg-context)
257 (signal 'wrong-type-argument (list 'epg-context-p context)))
258 (aref (cdr context) 9))
260 (defun epg-context-sig-notations (context)
261 "Return the list of notations for signing."
262 (unless (eq (car-safe context) 'epg-context)
263 (signal 'wrong-type-argument (list 'epg-context-p context)))
264 (aref (cdr context) 10))
266 (defun epg-context-process (context)
267 "Return the process object of `epg-gpg-program'.
268 This function is for internal use only."
269 (unless (eq (car-safe context) 'epg-context)
270 (signal 'wrong-type-argument (list 'epg-context-p context)))
271 (aref (cdr context) 11))
273 (defun epg-context-output-file (context)
274 "Return the output file of `epg-gpg-program'.
275 This function is for internal use only."
276 (unless (eq (car-safe context) 'epg-context)
277 (signal 'wrong-type-argument (list 'epg-context-p context)))
278 (aref (cdr context) 12))
280 (defun epg-context-result (context)
281 "Return the result of the previous cryptographic operation."
282 (unless (eq (car-safe context) 'epg-context)
283 (signal 'wrong-type-argument (list 'epg-context-p context)))
284 (aref (cdr context) 13))
286 (defun epg-context-operation (context)
287 "Return the name of the current cryptographic operation."
288 (unless (eq (car-safe context) 'epg-context)
289 (signal 'wrong-type-argument (list 'epg-context-p context)))
290 (aref (cdr context) 14))
292 (defun epg-context-set-protocol (context protocol)
293 "Set the protocol used within CONTEXT."
294 (unless (eq (car-safe context) 'epg-context)
295 (signal 'wrong-type-argument (list 'epg-context-p context)))
296 (aset (cdr context) 0 protocol))
298 (defun epg-context-set-armor (context armor)
299 "Specify if the output should be ASCII armored in CONTEXT."
300 (unless (eq (car-safe context) 'epg-context)
301 (signal 'wrong-type-argument (list 'epg-context-p context)))
302 (aset (cdr context) 1 armor))
304 (defun epg-context-set-textmode (context textmode)
305 "Specify if canonical text mode should be used in CONTEXT."
306 (unless (eq (car-safe context) 'epg-context)
307 (signal 'wrong-type-argument (list 'epg-context-p context)))
308 (aset (cdr context) 2 textmode))
310 (defun epg-context-set-include-certs (context include-certs)
311 "Set how many certificates should be included in an S/MIME signed message."
312 (unless (eq (car-safe context) 'epg-context)
313 (signal 'wrong-type-argument (list 'epg-context-p context)))
314 (aset (cdr context) 3 include-certs))
316 (defun epg-context-set-cipher-algorithm (context cipher-algorithm)
317 "Set the cipher algorithm in CONTEXT."
318 (unless (eq (car-safe context) 'epg-context)
319 (signal 'wrong-type-argument (list 'epg-context-p context)))
320 (aset (cdr context) 4 cipher-algorithm))
322 (defun epg-context-set-digest-algorithm (context digest-algorithm)
323 "Set the digest algorithm in CONTEXT."
324 (unless (eq (car-safe context) 'epg-context)
325 (signal 'wrong-type-argument (list 'epg-context-p context)))
326 (aset (cdr context) 5 digest-algorithm))
328 (defun epg-context-set-compress-algorithm (context compress-algorithm)
329 "Set the compress algorithm in CONTEXT."
330 (unless (eq (car-safe context) 'epg-context)
331 (signal 'wrong-type-argument (list 'epg-context-p context)))
332 (aset (cdr context) 6 compress-algorithm))
334 (defun epg-context-set-passphrase-callback (context
336 "Set the function used to query passphrase.
338 PASSPHRASE-CALLBACK is either a function, or a cons-cell whose
339 car is a function and cdr is a callback data.
341 The function gets three arguments: the context, the key-id in
342 question, and the callback data (if any).
344 The callback may not be called if you use GnuPG 2.x, which relies
345 on the external program called `gpg-agent' for passphrase query.
346 If you really want to intercept passphrase query, consider
347 installing GnuPG 1.x _along with_ GnuPG 2.x, which does passphrase
348 query by itself and Emacs can intercept them."
349 (unless (eq (car-safe context) 'epg-context)
350 (signal 'wrong-type-argument (list 'epg-context-p context)))
351 (aset (cdr context) 7 (if (consp passphrase-callback)
353 (list passphrase-callback))))
355 (defun epg-context-set-progress-callback (context
357 "Set the function which handles progress update.
359 PROGRESS-CALLBACK is either a function, or a cons-cell whose
360 car is a function and cdr is a callback data.
362 The function gets six arguments: the context, the operation
363 description, the character to display a progress unit, the
364 current amount done, the total amount to be done, and the
365 callback data (if any)."
366 (unless (eq (car-safe context) 'epg-context)
367 (signal 'wrong-type-argument (list 'epg-context-p context)))
368 (aset (cdr context) 8 (if (consp progress-callback)
370 (list progress-callback))))
372 (defun epg-context-set-signers (context signers)
373 "Set the list of key-id for signing."
374 (unless (eq (car-safe context) 'epg-context)
375 (signal 'wrong-type-argument (list 'epg-context-p context)))
376 (aset (cdr context) 9 signers))
378 (defun epg-context-set-sig-notations (context notations)
379 "Set the list of notations for signing."
380 (unless (eq (car-safe context) 'epg-context)
381 (signal 'wrong-type-argument (list 'epg-context-p context)))
382 (aset (cdr context) 10 notations))
384 (defun epg-context-set-process (context process)
385 "Set the process object of `epg-gpg-program'.
386 This function is for internal use only."
387 (unless (eq (car-safe context) 'epg-context)
388 (signal 'wrong-type-argument (list 'epg-context-p context)))
389 (aset (cdr context) 11 process))
391 (defun epg-context-set-output-file (context output-file)
392 "Set the output file of `epg-gpg-program'.
393 This function is for internal use only."
394 (unless (eq (car-safe context) 'epg-context)
395 (signal 'wrong-type-argument (list 'epg-context-p context)))
396 (aset (cdr context) 12 output-file))
398 (defun epg-context-set-result (context result)
399 "Set the result of the previous cryptographic operation."
400 (unless (eq (car-safe context) 'epg-context)
401 (signal 'wrong-type-argument (list 'epg-context-p context)))
402 (aset (cdr context) 13 result))
404 (defun epg-context-set-operation (context operation)
405 "Set the name of the current cryptographic operation."
406 (unless (eq (car-safe context) 'epg-context)
407 (signal 'wrong-type-argument (list 'epg-context-p context)))
408 (aset (cdr context) 14 operation))
410 (defun epg-make-signature (status &optional key-id)
411 "Return a signature object."
412 (cons 'epg-signature (vector status key-id nil nil nil nil nil nil nil nil
415 (defun epg-signature-status (signature)
416 "Return the status code of SIGNATURE."
417 (unless (eq (car-safe signature) 'epg-signature)
418 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
419 (aref (cdr signature) 0))
421 (defun epg-signature-key-id (signature)
422 "Return the key-id of SIGNATURE."
423 (unless (eq (car-safe signature) 'epg-signature)
424 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
425 (aref (cdr signature) 1))
427 (defun epg-signature-validity (signature)
428 "Return the validity of SIGNATURE."
429 (unless (eq (car-safe signature) 'epg-signature)
430 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
431 (aref (cdr signature) 2))
433 (defun epg-signature-fingerprint (signature)
434 "Return the fingerprint of SIGNATURE."
435 (unless (eq (car-safe signature) 'epg-signature)
436 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
437 (aref (cdr signature) 3))
439 (defun epg-signature-creation-time (signature)
440 "Return the creation time of SIGNATURE."
441 (unless (eq (car-safe signature) 'epg-signature)
442 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
443 (aref (cdr signature) 4))
445 (defun epg-signature-expiration-time (signature)
446 "Return the expiration time of SIGNATURE."
447 (unless (eq (car-safe signature) 'epg-signature)
448 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
449 (aref (cdr signature) 5))
451 (defun epg-signature-pubkey-algorithm (signature)
452 "Return the public key algorithm of SIGNATURE."
453 (unless (eq (car-safe signature) 'epg-signature)
454 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
455 (aref (cdr signature) 6))
457 (defun epg-signature-digest-algorithm (signature)
458 "Return the digest algorithm of SIGNATURE."
459 (unless (eq (car-safe signature) 'epg-signature)
460 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
461 (aref (cdr signature) 7))
463 (defun epg-signature-class (signature)
464 "Return the class of SIGNATURE."
465 (unless (eq (car-safe signature) 'epg-signature)
466 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
467 (aref (cdr signature) 8))
469 (defun epg-signature-version (signature)
470 "Return the version of SIGNATURE."
471 (unless (eq (car-safe signature) 'epg-signature)
472 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
473 (aref (cdr signature) 9))
475 (defun epg-sig-notations (signature)
476 "Return the list of notations of SIGNATURE."
477 (unless (eq (car-safe signature) 'epg-signature)
478 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
479 (aref (cdr signature) 10))
481 (defun epg-signature-set-status (signature status)
482 "Set the status code of SIGNATURE."
483 (unless (eq (car-safe signature) 'epg-signature)
484 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
485 (aset (cdr signature) 0 status))
487 (defun epg-signature-set-key-id (signature key-id)
488 "Set the key-id of SIGNATURE."
489 (unless (eq (car-safe signature) 'epg-signature)
490 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
491 (aset (cdr signature) 1 key-id))
493 (defun epg-signature-set-validity (signature validity)
494 "Set the validity of SIGNATURE."
495 (unless (eq (car-safe signature) 'epg-signature)
496 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
497 (aset (cdr signature) 2 validity))
499 (defun epg-signature-set-fingerprint (signature fingerprint)
500 "Set the fingerprint of SIGNATURE."
501 (unless (eq (car-safe signature) 'epg-signature)
502 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
503 (aset (cdr signature) 3 fingerprint))
505 (defun epg-signature-set-creation-time (signature creation-time)
506 "Set the creation time of SIGNATURE."
507 (unless (eq (car-safe signature) 'epg-signature)
508 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
509 (aset (cdr signature) 4 creation-time))
511 (defun epg-signature-set-expiration-time (signature expiration-time)
512 "Set the expiration time of SIGNATURE."
513 (unless (eq (car-safe signature) 'epg-signature)
514 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
515 (aset (cdr signature) 5 expiration-time))
517 (defun epg-signature-set-pubkey-algorithm (signature pubkey-algorithm)
518 "Set the public key algorithm of SIGNATURE."
519 (unless (eq (car-safe signature) 'epg-signature)
520 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
521 (aset (cdr signature) 6 pubkey-algorithm))
523 (defun epg-signature-set-digest-algorithm (signature digest-algorithm)
524 "Set the digest algorithm of SIGNATURE."
525 (unless (eq (car-safe signature) 'epg-signature)
526 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
527 (aset (cdr signature) 7 digest-algorithm))
529 (defun epg-signature-set-class (signature class)
530 "Set the class of SIGNATURE."
531 (unless (eq (car-safe signature) 'epg-signature)
532 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
533 (aset (cdr signature) 8 class))
535 (defun epg-signature-set-version (signature version)
536 "Set the version of SIGNATURE."
537 (unless (eq (car-safe signature) 'epg-signature)
538 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
539 (aset (cdr signature) 9 version))
541 (defun epg-signature-set-notations (signature notations)
542 "Set the list of notations of SIGNATURE."
543 (unless (eq (car-safe signature) 'epg-signature)
544 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
545 (aset (cdr signature) 10 notations))
547 (defun epg-make-new-signature (type pubkey-algorithm digest-algorithm
548 class creation-time fingerprint)
549 "Return a new signature object."
550 (cons 'epg-new-signature (vector type pubkey-algorithm digest-algorithm
551 class creation-time fingerprint)))
553 (defun epg-new-signature-type (new-signature)
554 "Return the type of NEW-SIGNATURE."
555 (unless (eq (car-safe new-signature) 'epg-new-signature)
556 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
557 (aref (cdr new-signature) 0))
559 (defun epg-new-signature-pubkey-algorithm (new-signature)
560 "Return the public key algorithm of NEW-SIGNATURE."
561 (unless (eq (car-safe new-signature) 'epg-new-signature)
562 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
563 (aref (cdr new-signature) 1))
565 (defun epg-new-signature-digest-algorithm (new-signature)
566 "Return the digest algorithm of NEW-SIGNATURE."
567 (unless (eq (car-safe new-signature) 'epg-new-signature)
568 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
569 (aref (cdr new-signature) 2))
571 (defun epg-new-signature-class (new-signature)
572 "Return the class of NEW-SIGNATURE."
573 (unless (eq (car-safe new-signature) 'epg-new-signature)
574 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
575 (aref (cdr new-signature) 3))
577 (defun epg-new-signature-creation-time (new-signature)
578 "Return the creation time of NEW-SIGNATURE."
579 (unless (eq (car-safe new-signature) 'epg-new-signature)
580 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
581 (aref (cdr new-signature) 4))
583 (defun epg-new-signature-fingerprint (new-signature)
584 "Return the fingerprint of NEW-SIGNATURE."
585 (unless (eq (car-safe new-signature) 'epg-new-signature)
586 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
587 (aref (cdr new-signature) 5))
589 (defun epg-make-key (owner-trust)
590 "Return a key object."
591 (cons 'epg-key (vector owner-trust nil nil)))
593 (defun epg-key-owner-trust (key)
594 "Return the owner trust of KEY."
595 (unless (eq (car-safe key) 'epg-key)
596 (signal 'wrong-type-argument (list 'epg-key-p key)))
599 (defun epg-key-sub-key-list (key)
600 "Return the sub key list of KEY."
601 (unless (eq (car-safe key) 'epg-key)
602 (signal 'wrong-type-argument (list 'epg-key-p key)))
605 (defun epg-key-user-id-list (key)
606 "Return the user ID list of KEY."
607 (unless (eq (car-safe key) 'epg-key)
608 (signal 'wrong-type-argument (list 'epg-key-p key)))
611 (defun epg-key-set-sub-key-list (key sub-key-list)
612 "Set the sub key list of KEY."
613 (unless (eq (car-safe key) 'epg-key)
614 (signal 'wrong-type-argument (list 'epg-key-p key)))
615 (aset (cdr key) 1 sub-key-list))
617 (defun epg-key-set-user-id-list (key user-id-list)
618 "Set the user ID list of KEY."
619 (unless (eq (car-safe key) 'epg-key)
620 (signal 'wrong-type-argument (list 'epg-key-p key)))
621 (aset (cdr key) 2 user-id-list))
623 (defun epg-make-sub-key (validity capability secret-p algorithm length id
624 creation-time expiration-time)
625 "Return a sub key object."
627 (vector validity capability secret-p algorithm length id creation-time
628 expiration-time nil)))
630 (defun epg-sub-key-validity (sub-key)
631 "Return the validity of SUB-KEY."
632 (unless (eq (car-safe sub-key) 'epg-sub-key)
633 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
634 (aref (cdr sub-key) 0))
636 (defun epg-sub-key-capability (sub-key)
637 "Return the capability of SUB-KEY."
638 (unless (eq (car-safe sub-key) 'epg-sub-key)
639 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
640 (aref (cdr sub-key) 1))
642 (defun epg-sub-key-secret-p (sub-key)
643 "Return non-nil if SUB-KEY is a secret key."
644 (unless (eq (car-safe sub-key) 'epg-sub-key)
645 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
646 (aref (cdr sub-key) 2))
648 (defun epg-sub-key-algorithm (sub-key)
649 "Return the algorithm of SUB-KEY."
650 (unless (eq (car-safe sub-key) 'epg-sub-key)
651 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
652 (aref (cdr sub-key) 3))
654 (defun epg-sub-key-length (sub-key)
655 "Return the length of SUB-KEY."
656 (unless (eq (car-safe sub-key) 'epg-sub-key)
657 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
658 (aref (cdr sub-key) 4))
660 (defun epg-sub-key-id (sub-key)
661 "Return the ID of SUB-KEY."
662 (unless (eq (car-safe sub-key) 'epg-sub-key)
663 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
664 (aref (cdr sub-key) 5))
666 (defun epg-sub-key-creation-time (sub-key)
667 "Return the creation time of SUB-KEY."
668 (unless (eq (car-safe sub-key) 'epg-sub-key)
669 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
670 (aref (cdr sub-key) 6))
672 (defun epg-sub-key-expiration-time (sub-key)
673 "Return the expiration time of SUB-KEY."
674 (unless (eq (car-safe sub-key) 'epg-sub-key)
675 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
676 (aref (cdr sub-key) 7))
678 (defun epg-sub-key-fingerprint (sub-key)
679 "Return the fingerprint of SUB-KEY."
680 (unless (eq (car-safe sub-key) 'epg-sub-key)
681 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
682 (aref (cdr sub-key) 8))
684 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
685 "Set the fingerprint of SUB-KEY.
686 This function is for internal use only."
687 (unless (eq (car-safe sub-key) 'epg-sub-key)
688 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
689 (aset (cdr sub-key) 8 fingerprint))
691 (defun epg-make-user-id (validity string)
692 "Return a user ID object."
693 (cons 'epg-user-id (vector validity string nil)))
695 (defun epg-user-id-validity (user-id)
696 "Return the validity of USER-ID."
697 (unless (eq (car-safe user-id) 'epg-user-id)
698 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
699 (aref (cdr user-id) 0))
701 (defun epg-user-id-string (user-id)
702 "Return the name of USER-ID."
703 (unless (eq (car-safe user-id) 'epg-user-id)
704 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
705 (aref (cdr user-id) 1))
707 (defun epg-user-id-signature-list (user-id)
708 "Return the signature list of USER-ID."
709 (unless (eq (car-safe user-id) 'epg-user-id)
710 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
711 (aref (cdr user-id) 2))
713 (defun epg-user-id-set-signature-list (user-id signature-list)
714 "Set the signature list of USER-ID."
715 (unless (eq (car-safe user-id) 'epg-user-id)
716 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
717 (aset (cdr user-id) 2 signature-list))
719 (defun epg-make-key-signature (validity pubkey-algorithm key-id creation-time
720 expiration-time user-id class
722 "Return a key signature object."
723 (cons 'epg-key-signature
724 (vector validity pubkey-algorithm key-id creation-time expiration-time
725 user-id class exportable-p)))
727 (defun epg-key-signature-validity (key-signature)
728 "Return the validity of KEY-SIGNATURE."
729 (unless (eq (car-safe key-signature) 'epg-key-signature)
730 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
731 (aref (cdr key-signature) 0))
733 (defun epg-key-signature-pubkey-algorithm (key-signature)
734 "Return the public key algorithm of KEY-SIGNATURE."
735 (unless (eq (car-safe key-signature) 'epg-key-signature)
736 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
737 (aref (cdr key-signature) 1))
739 (defun epg-key-signature-key-id (key-signature)
740 "Return the key-id of KEY-SIGNATURE."
741 (unless (eq (car-safe key-signature) 'epg-key-signature)
742 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
743 (aref (cdr key-signature) 2))
745 (defun epg-key-signature-creation-time (key-signature)
746 "Return the creation time of KEY-SIGNATURE."
747 (unless (eq (car-safe key-signature) 'epg-key-signature)
748 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
749 (aref (cdr key-signature) 3))
751 (defun epg-key-signature-expiration-time (key-signature)
752 "Return the expiration time of KEY-SIGNATURE."
753 (unless (eq (car-safe key-signature) 'epg-key-signature)
754 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
755 (aref (cdr key-signature) 4))
757 (defun epg-key-signature-user-id (key-signature)
758 "Return the user-id of KEY-SIGNATURE."
759 (unless (eq (car-safe key-signature) 'epg-key-signature)
760 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
761 (aref (cdr key-signature) 5))
763 (defun epg-key-signature-class (key-signature)
764 "Return the class of KEY-SIGNATURE."
765 (unless (eq (car-safe key-signature) 'epg-key-signature)
766 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
767 (aref (cdr key-signature) 6))
769 (defun epg-key-signature-exportable-p (key-signature)
770 "Return t if KEY-SIGNATURE is exportable."
771 (unless (eq (car-safe key-signature) 'epg-key-signature)
772 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
773 (aref (cdr key-signature) 7))
775 (defun epg-make-sig-notation (name value &optional human-readable
777 "Return a notation object."
778 (cons 'epg-sig-notation (vector name value human-readable critical)))
780 (defun epg-sig-notation-name (sig-notation)
781 "Return the name of SIG-NOTATION."
782 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
783 (signal 'wrong-type-argument (list 'epg-sig-notation-p
785 (aref (cdr sig-notation) 0))
787 (defun epg-sig-notation-value (sig-notation)
788 "Return the value of SIG-NOTATION."
789 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
790 (signal 'wrong-type-argument (list 'epg-sig-notation-p
792 (aref (cdr sig-notation) 1))
794 (defun epg-sig-notation-human-readable (sig-notation)
795 "Return the human-readable of SIG-NOTATION."
796 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
797 (signal 'wrong-type-argument (list 'epg-sig-notation-p
799 (aref (cdr sig-notation) 2))
801 (defun epg-sig-notation-critical (sig-notation)
802 "Return the critical of SIG-NOTATION."
803 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
804 (signal 'wrong-type-argument (list 'epg-sig-notation-p
806 (aref (cdr sig-notation) 3))
808 (defun epg-sig-notation-set-value (sig-notation value)
809 "Set the value of SIG-NOTATION."
810 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
811 (signal 'wrong-type-argument (list 'epg-sig-notation-p
813 (aset (cdr sig-notation) 1 value))
815 (defun epg-make-import-status (fingerprint &optional reason new user-id
816 signature sub-key secret)
817 "Return an import status object."
818 (cons 'epg-import-status (vector fingerprint reason new user-id signature
821 (defun epg-import-status-fingerprint (import-status)
822 "Return the fingerprint of the key that was considered."
823 (unless (eq (car-safe import-status) 'epg-import-status)
824 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
825 (aref (cdr import-status) 0))
827 (defun epg-import-status-reason (import-status)
828 "Return the reason code for import failure."
829 (unless (eq (car-safe import-status) 'epg-import-status)
830 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
831 (aref (cdr import-status) 1))
833 (defun epg-import-status-new (import-status)
834 "Return t if the imported key was new."
835 (unless (eq (car-safe import-status) 'epg-import-status)
836 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
837 (aref (cdr import-status) 2))
839 (defun epg-import-status-user-id (import-status)
840 "Return t if the imported key contained new user IDs."
841 (unless (eq (car-safe import-status) 'epg-import-status)
842 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
843 (aref (cdr import-status) 3))
845 (defun epg-import-status-signature (import-status)
846 "Return t if the imported key contained new signatures."
847 (unless (eq (car-safe import-status) 'epg-import-status)
848 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
849 (aref (cdr import-status) 4))
851 (defun epg-import-status-sub-key (import-status)
852 "Return t if the imported key contained new sub keys."
853 (unless (eq (car-safe import-status) 'epg-import-status)
854 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
855 (aref (cdr import-status) 5))
857 (defun epg-import-status-secret (import-status)
858 "Return t if the imported key contained a secret key."
859 (unless (eq (car-safe import-status) 'epg-import-status)
860 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
861 (aref (cdr import-status) 6))
863 (defun epg-make-import-result (considered no-user-id imported imported-rsa
864 unchanged new-user-ids new-sub-keys
865 new-signatures new-revocations
866 secret-read secret-imported
867 secret-unchanged not-imported
869 "Return an import result object."
870 (cons 'epg-import-result (vector considered no-user-id imported imported-rsa
871 unchanged new-user-ids new-sub-keys
872 new-signatures new-revocations secret-read
873 secret-imported secret-unchanged
874 not-imported imports)))
876 (defun epg-import-result-considered (import-result)
877 "Return the total number of considered keys."
878 (unless (eq (car-safe import-result) 'epg-import-result)
879 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
880 (aref (cdr import-result) 0))
882 (defun epg-import-result-no-user-id (import-result)
883 "Return the number of keys without user ID."
884 (unless (eq (car-safe import-result) 'epg-import-result)
885 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
886 (aref (cdr import-result) 1))
888 (defun epg-import-result-imported (import-result)
889 "Return the number of imported keys."
890 (unless (eq (car-safe import-result) 'epg-import-result)
891 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
892 (aref (cdr import-result) 2))
894 (defun epg-import-result-imported-rsa (import-result)
895 "Return the number of imported RSA keys."
896 (unless (eq (car-safe import-result) 'epg-import-result)
897 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
898 (aref (cdr import-result) 3))
900 (defun epg-import-result-unchanged (import-result)
901 "Return the number of unchanged keys."
902 (unless (eq (car-safe import-result) 'epg-import-result)
903 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
904 (aref (cdr import-result) 4))
906 (defun epg-import-result-new-user-ids (import-result)
907 "Return the number of new user IDs."
908 (unless (eq (car-safe import-result) 'epg-import-result)
909 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
910 (aref (cdr import-result) 5))
912 (defun epg-import-result-new-sub-keys (import-result)
913 "Return the number of new sub keys."
914 (unless (eq (car-safe import-result) 'epg-import-result)
915 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
916 (aref (cdr import-result) 6))
918 (defun epg-import-result-new-signatures (import-result)
919 "Return the number of new signatures."
920 (unless (eq (car-safe import-result) 'epg-import-result)
921 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
922 (aref (cdr import-result) 7))
924 (defun epg-import-result-new-revocations (import-result)
925 "Return the number of new revocations."
926 (unless (eq (car-safe import-result) 'epg-import-result)
927 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
928 (aref (cdr import-result) 8))
930 (defun epg-import-result-secret-read (import-result)
931 "Return the total number of secret keys read."
932 (unless (eq (car-safe import-result) 'epg-import-result)
933 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
934 (aref (cdr import-result) 9))
936 (defun epg-import-result-secret-imported (import-result)
937 "Return the number of imported secret keys."
938 (unless (eq (car-safe import-result) 'epg-import-result)
939 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
940 (aref (cdr import-result) 10))
942 (defun epg-import-result-secret-unchanged (import-result)
943 "Return the number of unchanged secret keys."
944 (unless (eq (car-safe import-result) 'epg-import-result)
945 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
946 (aref (cdr import-result) 11))
948 (defun epg-import-result-not-imported (import-result)
949 "Return the number of keys not imported."
950 (unless (eq (car-safe import-result) 'epg-import-result)
951 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
952 (aref (cdr import-result) 12))
954 (defun epg-import-result-imports (import-result)
955 "Return the list of `epg-import-status' objects."
956 (unless (eq (car-safe import-result) 'epg-import-result)
957 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
958 (aref (cdr import-result) 13))
960 (defun epg-context-result-for (context name)
961 "Return the result of CONTEXT associated with NAME."
962 (cdr (assq name (epg-context-result context))))
964 (defun epg-context-set-result-for (context name value)
965 "Set the result of CONTEXT associated with NAME to VALUE."
966 (let* ((result (epg-context-result context))
967 (entry (assq name result)))
970 (epg-context-set-result context (cons (cons name value) result)))))
972 (defun epg-signature-to-string (signature)
973 "Convert SIGNATURE to a human readable string."
974 (let* ((user-id (cdr (assoc (epg-signature-key-id signature)
976 (pubkey-algorithm (epg-signature-pubkey-algorithm signature))
977 (key-id (epg-signature-key-id signature)))
979 (cond ((eq (epg-signature-status signature) 'good)
980 "Good signature from ")
981 ((eq (epg-signature-status signature) 'bad)
982 "Bad signature from ")
983 ((eq (epg-signature-status signature) 'expired)
984 "Expired signature from ")
985 ((eq (epg-signature-status signature) 'expired-key)
986 "Signature made by expired key ")
987 ((eq (epg-signature-status signature) 'revoked-key)
988 "Signature made by revoked key ")
989 ((eq (epg-signature-status signature) 'no-pubkey)
990 "No public key for "))
994 (if (stringp user-id)
996 (epg-decode-dn user-id)))
998 (if (epg-signature-validity signature)
999 (format " (trust %s)" (epg-signature-validity signature))
1001 (if (epg-signature-creation-time signature)
1002 (format-time-string " created at %Y-%m-%dT%T%z"
1003 (epg-signature-creation-time signature))
1005 (if pubkey-algorithm
1007 (or (cdr (assq pubkey-algorithm epg-pubkey-algorithm-alist))
1008 (format "(unknown algorithm %d)" pubkey-algorithm)))
1011 (defun epg-verify-result-to-string (verify-result)
1012 "Convert VERIFY-RESULT to a human readable string."
1013 (mapconcat #'epg-signature-to-string verify-result "\n"))
1015 (defun epg-new-signature-to-string (new-signature)
1016 "Convert NEW-SIGNATURE to a human readable string."
1018 (cond ((eq (epg-new-signature-type new-signature) 'detached)
1019 "Detached signature ")
1020 ((eq (epg-new-signature-type new-signature) 'clear)
1021 "Cleartext signature ")
1024 (cdr (assq (epg-new-signature-pubkey-algorithm new-signature)
1025 epg-pubkey-algorithm-alist))
1027 (cdr (assq (epg-new-signature-digest-algorithm new-signature)
1028 epg-digest-algorithm-alist))
1030 (format "%02X " (epg-new-signature-class new-signature))
1031 (epg-new-signature-fingerprint new-signature)))
1033 (defun epg-import-result-to-string (import-result)
1034 "Convert IMPORT-RESULT to a human readable string."
1035 (concat (format "Total number processed: %d\n"
1036 (epg-import-result-considered import-result))
1037 (if (> (epg-import-result-not-imported import-result) 0)
1038 (format " skipped new keys: %d\n"
1039 (epg-import-result-not-imported import-result)))
1040 (if (> (epg-import-result-no-user-id import-result) 0)
1041 (format " w/o user IDs: %d\n"
1042 (epg-import-result-no-user-id import-result)))
1043 (if (> (epg-import-result-imported import-result) 0)
1044 (concat (format " imported: %d"
1045 (epg-import-result-imported import-result))
1046 (if (> (epg-import-result-imported-rsa import-result) 0)
1047 (format " (RSA: %d)"
1048 (epg-import-result-imported-rsa
1051 (if (> (epg-import-result-unchanged import-result) 0)
1052 (format " unchanged: %d\n"
1053 (epg-import-result-unchanged import-result)))
1054 (if (> (epg-import-result-new-user-ids import-result) 0)
1055 (format " new user IDs: %d\n"
1056 (epg-import-result-new-user-ids import-result)))
1057 (if (> (epg-import-result-new-sub-keys import-result) 0)
1058 (format " new subkeys: %d\n"
1059 (epg-import-result-new-sub-keys import-result)))
1060 (if (> (epg-import-result-new-signatures import-result) 0)
1061 (format " new signatures: %d\n"
1062 (epg-import-result-new-signatures import-result)))
1063 (if (> (epg-import-result-new-revocations import-result) 0)
1064 (format " new key revocations: %d\n"
1065 (epg-import-result-new-revocations import-result)))
1066 (if (> (epg-import-result-secret-read import-result) 0)
1067 (format " secret keys read: %d\n"
1068 (epg-import-result-secret-read import-result)))
1069 (if (> (epg-import-result-secret-imported import-result) 0)
1070 (format " secret keys imported: %d\n"
1071 (epg-import-result-secret-imported import-result)))
1072 (if (> (epg-import-result-secret-unchanged import-result) 0)
1073 (format " secret keys unchanged: %d\n"
1074 (epg-import-result-secret-unchanged import-result)))))
1076 (defun epg-error-to-string (error)
1078 ((eq (car error) 'exit)
1080 ((eq (car error) 'quit)
1082 ((eq (car error) 'no-data)
1083 (let ((entry (assq (cdr error) epg-no-data-reason-alist)))
1085 (format "No data (%s)" (downcase (cdr entry)))
1087 ((eq (car error) 'unexpected)
1088 (let ((entry (assq (cdr error) epg-unexpected-reason-alist)))
1090 (format "Unexpected (%s)" (downcase (cdr entry)))
1092 ((eq (car error) 'bad-armor)
1094 ((memq (car error) '(invalid-recipient invalid-signer))
1096 (if (eq (car error) 'invalid-recipient)
1097 "Unusable public key"
1098 "Unusable secret key")
1099 (let ((entry (assq 'requested (cdr error))))
1101 (format ": %s" (cdr entry))
1103 (let ((entry (assq 'reason (cdr error))))
1105 (> (cdr entry) 0) ;no specific reason given
1106 (setq entry (assq (cdr entry)
1107 epg-invalid-recipients-reason-alist)))
1108 (format " (%s)" (downcase (cdr entry)))
1110 ((eq (car error) 'no-pubkey)
1111 (format "No public key: %s" (cdr error)))
1112 ((eq (car error) 'no-seckey)
1113 (format "No secret key: %s" (cdr error)))
1114 ((eq (car error) 'no-recipients)
1116 ((eq (car error) 'no-signers)
1118 ((eq (car error) 'delete-problem)
1119 (let ((entry (assq (cdr error) epg-delete-problem-reason-alist)))
1121 (format "Delete problem (%s)" (downcase (cdr entry)))
1123 ((eq (car error) 'key-not-created)
1124 "Key not created")))
1126 (defun epg-errors-to-string (errors)
1127 (mapconcat #'epg-error-to-string errors "; "))
1129 (defun epg--start (context args)
1130 "Start `epg-gpg-program' in a subprocess with given ARGS."
1131 (if (and (epg-context-process context)
1132 (eq (process-status (epg-context-process context)) 'run))
1133 (error "%s is already running in this context"
1134 (if (eq (epg-context-protocol context) 'CMS)
1137 (let* ((agent-info (getenv "GPG_AGENT_INFO"))
1138 (args (append (list "--no-tty"
1141 (if (and (not (eq (epg-context-protocol context) 'CMS))
1142 (string-match ":" (or agent-info "")))
1144 (if (and (not (eq (epg-context-protocol context) 'CMS))
1145 (epg-context-progress-callback context))
1146 '("--enable-progress-filter"))
1147 (if epg-gpg-home-directory
1148 (list "--homedir" epg-gpg-home-directory))
1149 (unless (eq (epg-context-protocol context) 'CMS)
1150 '("--command-fd" "0"))
1151 (if (epg-context-armor context) '("--armor"))
1152 (if (epg-context-textmode context) '("--textmode"))
1153 (if (epg-context-output-file context)
1154 (list "--output" (epg-context-output-file context)))
1156 (coding-system-for-write 'binary)
1157 (coding-system-for-read 'binary)
1158 process-connection-type
1159 (orig-mode (default-file-modes))
1160 (buffer (generate-new-buffer " *epg*"))
1164 (agent-mtime '(0 0 0 0)))
1165 ;; Set GPG_TTY and TERM for pinentry-curses. Note that we can't
1166 ;; use `terminal-name' here to get the real pty name for the child
1167 ;; process, though /dev/fd/0" is not portable.
1169 (when (= (call-process "tty" "/dev/fd/0" t) 0)
1170 (delete-backward-char 1)
1171 (setq terminal-name (buffer-string))))
1173 (setenv "GPG_TTY" terminal-name)
1174 (setenv "TERM" "xterm"))
1175 ;; Record modified time of gpg-agent socket to restore the Emacs
1176 ;; frame on text terminal in `epg-wait-for-completion'.
1178 ;; <http://lists.gnu.org/archive/html/emacs-devel/2007-02/msg00755.html>
1179 ;; for more details.
1180 (when (and agent-info (string-match "\\(.*\\):[0-9]+:[0-9]+" agent-info))
1181 (setq agent-file (match-string 1 agent-info)
1182 agent-mtime (or (nth 5 (file-attributes agent-file)) '(0 0 0 0))))
1185 (unless epg-debug-buffer
1186 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
1187 (set-buffer epg-debug-buffer)
1188 (goto-char (point-max))
1189 (insert (if agent-info
1190 (format "GPG_AGENT_INFO=%s\n" agent-info)
1191 "GPG_AGENT_INFO is not set\n")
1193 (if (eq (epg-context-protocol context) 'CMS)
1196 (mapconcat #'identity args " ")))))
1197 (with-current-buffer buffer
1198 (if (fboundp 'set-buffer-multibyte)
1199 (set-buffer-multibyte nil))
1200 (make-local-variable 'epg-last-status)
1201 (setq epg-last-status nil)
1202 (make-local-variable 'epg-read-point)
1203 (setq epg-read-point (point-min))
1204 (make-local-variable 'epg-process-filter-running)
1205 (setq epg-process-filter-running nil)
1206 (make-local-variable 'epg-pending-status-list)
1207 (setq epg-pending-status-list nil)
1208 (make-local-variable 'epg-key-id)
1209 (setq epg-key-id nil)
1210 (make-local-variable 'epg-context)
1211 (setq epg-context context)
1212 (make-local-variable 'epg-agent-file)
1213 (setq epg-agent-file agent-file)
1214 (make-local-variable 'epg-agent-mtime)
1215 (setq epg-agent-mtime agent-mtime))
1218 (set-default-file-modes 448)
1220 (apply #'start-process "epg" buffer
1221 (if (eq (epg-context-protocol context) 'CMS)
1225 (set-default-file-modes orig-mode))
1226 (set-process-filter process #'epg--process-filter)
1227 (epg-context-set-process context process)))
1229 (defun epg--process-filter (process input)
1232 (unless epg-debug-buffer
1233 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
1234 (set-buffer epg-debug-buffer)
1235 (goto-char (point-max))
1237 (if (buffer-live-p (process-buffer process))
1238 (with-current-buffer (process-buffer process)
1239 (goto-char (point-max))
1241 (unless epg-process-filter-running
1244 (setq epg-process-filter-running t)
1245 (goto-char epg-read-point)
1247 (while (looking-at ".*\n") ;the input line finished
1248 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
1249 (let* ((status (match-string 1))
1250 (string (match-string 2))
1251 (symbol (intern-soft (concat "epg--status-"
1253 (if (member status epg-pending-status-list)
1254 (setq epg-pending-status-list nil))
1257 (funcall symbol epg-context string))
1258 (setq epg-last-status (cons status string))))
1260 (setq epg-read-point (point))))
1261 (setq epg-process-filter-running nil))))))
1263 (defun epg-read-output (context)
1264 "Read the output file CONTEXT and return the content as a string."
1266 (if (fboundp 'set-buffer-multibyte)
1267 (set-buffer-multibyte nil))
1268 (if (file-exists-p (epg-context-output-file context))
1269 (let ((coding-system-for-read 'binary))
1270 (insert-file-contents (epg-context-output-file context))
1273 (defun epg-wait-for-status (context status-list)
1274 "Wait until one of elements in STATUS-LIST arrives."
1275 (with-current-buffer (process-buffer (epg-context-process context))
1276 (setq epg-pending-status-list status-list)
1277 (while (and (eq (process-status (epg-context-process context)) 'run)
1278 epg-pending-status-list)
1279 (accept-process-output (epg-context-process context) 1))
1280 (if epg-pending-status-list
1281 (epg-context-set-result-for
1284 (epg-context-result-for context 'error))))))
1286 (defun epg-wait-for-completion (context)
1287 "Wait until the `epg-gpg-program' process completes."
1288 (while (eq (process-status (epg-context-process context)) 'run)
1289 (accept-process-output (epg-context-process context) 1))
1290 ;; This line is needed to run the process-filter right now.
1292 ;; Restore Emacs frame on text terminal, when pinentry-curses has terminated.
1293 (if (with-current-buffer (process-buffer (epg-context-process context))
1295 (> (float-time (or (nth 5 (file-attributes epg-agent-file))
1297 (float-time epg-agent-mtime))))
1299 (epg-context-set-result-for
1301 (nreverse (epg-context-result-for context 'error))))
1303 (defun epg-reset (context)
1304 "Reset the CONTEXT."
1305 (if (and (epg-context-process context)
1306 (buffer-live-p (process-buffer (epg-context-process context))))
1307 (kill-buffer (process-buffer (epg-context-process context))))
1308 (epg-context-set-process context nil))
1310 (defun epg-delete-output-file (context)
1311 "Delete the output file of CONTEXT."
1312 (if (and (epg-context-output-file context)
1313 (file-exists-p (epg-context-output-file context)))
1314 (delete-file (epg-context-output-file context))))
1317 (if (fboundp 'decode-coding-string)
1318 (defalias 'epg--decode-coding-string 'decode-coding-string)
1319 (defalias 'epg--decode-coding-string 'identity)))
1321 (defun epg--status-USERID_HINT (_context string)
1322 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1323 (let* ((key-id (match-string 1 string))
1324 (user-id (match-string 2 string))
1325 (entry (assoc key-id epg-user-id-alist)))
1327 (setq user-id (epg--decode-coding-string
1328 (epg--decode-percent-escape user-id)
1332 (setcdr entry user-id)
1333 (setq epg-user-id-alist (cons (cons key-id user-id)
1334 epg-user-id-alist))))))
1336 (defun epg--status-NEED_PASSPHRASE (_context string)
1337 (if (string-match "\\`\\([^ ]+\\)" string)
1338 (setq epg-key-id (match-string 1 string))))
1340 (defun epg--status-NEED_PASSPHRASE_SYM (_context _string)
1341 (setq epg-key-id 'SYM))
1343 (defun epg--status-NEED_PASSPHRASE_PIN (_context _string)
1344 (setq epg-key-id 'PIN))
1347 (if (fboundp 'clear-string)
1348 (defalias 'epg--clear-string 'clear-string)
1349 (defun epg--clear-string (string)
1350 (fillarray string 0))))
1353 (if (fboundp 'encode-coding-string)
1354 (defalias 'epg--encode-coding-string 'encode-coding-string)
1355 (defalias 'epg--encode-coding-string 'identity)))
1357 (defun epg--status-GET_HIDDEN (context string)
1358 (when (and epg-key-id
1359 (string-match "\\`passphrase\\." string))
1360 (unless (epg-context-passphrase-callback context)
1361 (error "passphrase-callback not set"))
1364 passphrase-with-new-line
1365 encoded-passphrase-with-new-line)
1371 (car (epg-context-passphrase-callback context))
1374 (cdr (epg-context-passphrase-callback context))))
1376 (setq passphrase-with-new-line (concat passphrase "\n"))
1377 (epg--clear-string passphrase)
1378 (setq passphrase nil)
1379 (if epg-passphrase-coding-system
1381 (setq encoded-passphrase-with-new-line
1382 (epg--encode-coding-string
1383 passphrase-with-new-line
1384 (coding-system-change-eol-conversion
1385 epg-passphrase-coding-system 'unix)))
1386 (epg--clear-string passphrase-with-new-line)
1387 (setq passphrase-with-new-line nil))
1388 (setq encoded-passphrase-with-new-line
1389 passphrase-with-new-line
1390 passphrase-with-new-line nil))
1391 (process-send-string (epg-context-process context)
1392 encoded-passphrase-with-new-line)))
1394 (epg-context-set-result-for
1397 (epg-context-result-for context 'error)))
1398 (delete-process (epg-context-process context))))
1400 (epg--clear-string passphrase))
1401 (if passphrase-with-new-line
1402 (epg--clear-string passphrase-with-new-line))
1403 (if encoded-passphrase-with-new-line
1404 (epg--clear-string encoded-passphrase-with-new-line))))))
1406 (defun epg--prompt-GET_BOOL (_context string)
1407 (let ((entry (assoc string epg-prompt-alist)))
1408 (y-or-n-p (if entry (cdr entry) (concat string "? ")))))
1410 (defun epg--prompt-GET_BOOL-untrusted_key.override (_context _string)
1411 (y-or-n-p (if (and (equal (car epg-last-status) "USERID_HINT")
1412 (string-match "\\`\\([^ ]+\\) \\(.*\\)"
1413 (cdr epg-last-status)))
1414 (let* ((key-id (match-string 1 (cdr epg-last-status)))
1415 (user-id (match-string 2 (cdr epg-last-status)))
1416 (entry (assoc key-id epg-user-id-alist)))
1418 (setq user-id (cdr entry)))
1419 (format "Untrusted key %s %s. Use anyway? " key-id user-id))
1420 "Use untrusted key anyway? ")))
1422 (defun epg--status-GET_BOOL (context string)
1425 (if (funcall (or (intern-soft (concat "epg--prompt-GET_BOOL-" string))
1426 #'epg--prompt-GET_BOOL)
1428 (process-send-string (epg-context-process context) "y\n")
1429 (process-send-string (epg-context-process context) "n\n"))
1431 (epg-context-set-result-for
1434 (epg-context-result-for context 'error)))
1435 (delete-process (epg-context-process context))))))
1437 (defun epg--status-GET_LINE (context string)
1438 (let ((entry (assoc string epg-prompt-alist))
1441 (process-send-string (epg-context-process context)
1442 (concat (read-string
1445 (concat string ": ")))
1448 (epg-context-set-result-for
1451 (epg-context-result-for context 'error)))
1452 (delete-process (epg-context-process context))))))
1454 (defun epg--status-*SIG (context status string)
1455 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1456 (let* ((key-id (match-string 1 string))
1457 (user-id (match-string 2 string))
1458 (entry (assoc key-id epg-user-id-alist)))
1459 (epg-context-set-result-for
1462 (cons (epg-make-signature status key-id)
1463 (epg-context-result-for context 'verify)))
1465 (if (eq (epg-context-protocol context) 'CMS)
1466 (setq user-id (epg-dn-from-string user-id))
1467 (setq user-id (epg--decode-coding-string
1468 (epg--decode-percent-escape user-id)
1472 (setcdr entry user-id)
1473 (setq epg-user-id-alist
1474 (cons (cons key-id user-id) epg-user-id-alist))))
1475 (epg-context-set-result-for
1478 (cons (epg-make-signature status)
1479 (epg-context-result-for context 'verify)))))
1481 (defun epg--status-GOODSIG (context string)
1482 (epg--status-*SIG context 'good string))
1484 (defun epg--status-EXPSIG (context string)
1485 (epg--status-*SIG context 'expired string))
1487 (defun epg--status-EXPKEYSIG (context string)
1488 (epg--status-*SIG context 'expired-key string))
1490 (defun epg--status-REVKEYSIG (context string)
1491 (epg--status-*SIG context 'revoked-key string))
1493 (defun epg--status-BADSIG (context string)
1494 (epg--status-*SIG context 'bad string))
1496 (defun epg--status-NO_PUBKEY (context string)
1497 (if (eq (epg-context-operation context) 'verify)
1498 (let ((signature (car (epg-context-result-for context 'verify))))
1500 (eq (epg-signature-status signature) 'error)
1501 (equal (epg-signature-key-id signature) string))
1502 (epg-signature-set-status signature 'no-pubkey)))
1503 (epg-context-set-result-for
1505 (cons (cons 'no-pubkey string)
1506 (epg-context-result-for context 'error)))))
1508 (defun epg--status-NO_SECKEY (context string)
1509 (epg-context-set-result-for
1511 (cons (cons 'no-seckey string)
1512 (epg-context-result-for context 'error))))
1514 (defun epg--time-from-seconds (seconds)
1515 (let ((number-seconds (string-to-number (concat seconds ".0"))))
1516 (cons (floor (/ number-seconds 65536))
1517 (floor (mod number-seconds 65536)))))
1519 (defun epg--status-ERRSIG (context string)
1520 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1521 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
1523 (let ((signature (epg-make-signature 'error)))
1524 (epg-context-set-result-for
1528 (epg-context-result-for context 'verify)))
1529 (epg-signature-set-key-id
1531 (match-string 1 string))
1532 (epg-signature-set-pubkey-algorithm
1534 (string-to-number (match-string 2 string)))
1535 (epg-signature-set-digest-algorithm
1537 (string-to-number (match-string 3 string)))
1538 (epg-signature-set-class
1540 (string-to-number (match-string 4 string) 16))
1541 (epg-signature-set-creation-time
1543 (epg--time-from-seconds (match-string 5 string))))))
1545 (defun epg--status-VALIDSIG (context string)
1546 (let ((signature (car (epg-context-result-for context 'verify))))
1547 (when (and signature
1548 (eq (epg-signature-status signature) 'good)
1549 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
1550 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
1553 (epg-signature-set-fingerprint
1555 (match-string 1 string))
1556 (epg-signature-set-creation-time
1558 (epg--time-from-seconds (match-string 2 string)))
1559 (unless (equal (match-string 3 string) "0")
1560 (epg-signature-set-expiration-time
1562 (epg--time-from-seconds (match-string 3 string))))
1563 (epg-signature-set-version
1565 (string-to-number (match-string 4 string)))
1566 (epg-signature-set-pubkey-algorithm
1568 (string-to-number (match-string 5 string)))
1569 (epg-signature-set-digest-algorithm
1571 (string-to-number (match-string 6 string)))
1572 (epg-signature-set-class
1574 (string-to-number (match-string 7 string) 16)))))
1576 (defun epg--status-TRUST_UNDEFINED (context _string)
1577 (let ((signature (car (epg-context-result-for context 'verify))))
1579 (eq (epg-signature-status signature) 'good))
1580 (epg-signature-set-validity signature 'undefined))))
1582 (defun epg--status-TRUST_NEVER (context _string)
1583 (let ((signature (car (epg-context-result-for context 'verify))))
1585 (eq (epg-signature-status signature) 'good))
1586 (epg-signature-set-validity signature 'never))))
1588 (defun epg--status-TRUST_MARGINAL (context _string)
1589 (let ((signature (car (epg-context-result-for context 'verify))))
1591 (eq (epg-signature-status signature) 'marginal))
1592 (epg-signature-set-validity signature 'marginal))))
1594 (defun epg--status-TRUST_FULLY (context _string)
1595 (let ((signature (car (epg-context-result-for context 'verify))))
1597 (eq (epg-signature-status signature) 'good))
1598 (epg-signature-set-validity signature 'full))))
1600 (defun epg--status-TRUST_ULTIMATE (context _string)
1601 (let ((signature (car (epg-context-result-for context 'verify))))
1603 (eq (epg-signature-status signature) 'good))
1604 (epg-signature-set-validity signature 'ultimate))))
1606 (defun epg--status-NOTATION_NAME (context string)
1607 (let ((signature (car (epg-context-result-for context 'verify))))
1609 (epg-signature-set-notations
1611 (cons (epg-make-sig-notation string nil t nil)
1612 (epg-sig-notations signature))))))
1614 (defun epg--status-NOTATION_DATA (context string)
1615 (let ((signature (car (epg-context-result-for context 'verify)))
1618 (setq notation (car (epg-sig-notations signature))))
1619 (epg-sig-notation-set-value notation string))))
1621 (defun epg--status-POLICY_URL (context string)
1622 (let ((signature (car (epg-context-result-for context 'verify))))
1624 (epg-signature-set-notations
1626 (cons (epg-make-sig-notation nil string t nil)
1627 (epg-sig-notations signature))))))
1629 (defun epg--status-PROGRESS (context string)
1630 (if (and (epg-context-progress-callback context)
1631 (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1633 (funcall (car (epg-context-progress-callback context))
1635 (match-string 1 string)
1636 (match-string 2 string)
1637 (string-to-number (match-string 3 string))
1638 (string-to-number (match-string 4 string))
1639 (cdr (epg-context-progress-callback context)))))
1641 (defun epg--status-ENC_TO (context string)
1642 (if (string-match "\\`\\([0-9A-Za-z]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
1643 (epg-context-set-result-for
1644 context 'encrypted-to
1645 (cons (list (match-string 1 string)
1646 (string-to-number (match-string 2 string))
1647 (string-to-number (match-string 3 string)))
1648 (epg-context-result-for context 'encrypted-to)))))
1650 (defun epg--status-DECRYPTION_FAILED (context _string)
1651 (epg-context-set-result-for context 'decryption-failed t))
1653 (defun epg--status-DECRYPTION_OKAY (context _string)
1654 (epg-context-set-result-for context 'decryption-okay t))
1656 (defun epg--status-NODATA (context string)
1657 (epg-context-set-result-for
1659 (cons (cons 'no-data (string-to-number string))
1660 (epg-context-result-for context 'error))))
1662 (defun epg--status-UNEXPECTED (context string)
1663 (epg-context-set-result-for
1665 (cons (cons 'unexpected (string-to-number string))
1666 (epg-context-result-for context 'error))))
1668 (defun epg--status-KEYEXPIRED (context string)
1669 (epg-context-set-result-for
1671 (cons (list 'key-expired (cons 'expiration-time
1672 (epg--time-from-seconds string)))
1673 (epg-context-result-for context 'key))))
1675 (defun epg--status-KEYREVOKED (context _string)
1676 (epg-context-set-result-for
1678 (cons '(key-revoked)
1679 (epg-context-result-for context 'key))))
1681 (defun epg--status-BADARMOR (context _string)
1682 (epg-context-set-result-for
1685 (epg-context-result-for context 'error))))
1687 (defun epg--status-INV_RECP (context string)
1688 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1689 (epg-context-set-result-for
1691 (cons (list 'invalid-recipient
1693 (string-to-number (match-string 1 string)))
1695 (match-string 2 string)))
1696 (epg-context-result-for context 'error)))))
1698 (defun epg--status-INV_SGNR (context string)
1699 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1700 (epg-context-set-result-for
1702 (cons (list 'invalid-signer
1704 (string-to-number (match-string 1 string)))
1706 (match-string 2 string)))
1707 (epg-context-result-for context 'error)))))
1709 (defun epg--status-NO_RECP (context _string)
1710 (epg-context-set-result-for
1712 (cons '(no-recipients)
1713 (epg-context-result-for context 'error))))
1715 (defun epg--status-NO_SGNR (context _string)
1716 (epg-context-set-result-for
1719 (epg-context-result-for context 'error))))
1721 (defun epg--status-DELETE_PROBLEM (context string)
1722 (if (string-match "\\`\\([0-9]+\\)" string)
1723 (epg-context-set-result-for
1725 (cons (cons 'delete-problem
1726 (string-to-number (match-string 1 string)))
1727 (epg-context-result-for context 'error)))))
1729 (defun epg--status-SIG_CREATED (context string)
1730 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1731 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1732 (epg-context-set-result-for
1734 (cons (epg-make-new-signature
1735 (cdr (assq (aref (match-string 1 string) 0)
1736 epg-new-signature-type-alist))
1737 (string-to-number (match-string 2 string))
1738 (string-to-number (match-string 3 string))
1739 (string-to-number (match-string 4 string) 16)
1740 (epg--time-from-seconds (match-string 5 string))
1741 (substring string (match-end 0)))
1742 (epg-context-result-for context 'sign)))))
1744 (defun epg--status-KEY_CREATED (context string)
1745 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1746 (epg-context-set-result-for
1747 context 'generate-key
1748 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1749 (cons 'fingerprint (match-string 2 string)))
1750 (epg-context-result-for context 'generate-key)))))
1752 (defun epg--status-KEY_NOT_CREATED (context _string)
1753 (epg-context-set-result-for
1755 (cons '(key-not-created)
1756 (epg-context-result-for context 'error))))
1758 (defun epg--status-IMPORTED (_context string)
1759 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1760 (let* ((key-id (match-string 1 string))
1761 (user-id (match-string 2 string))
1762 (entry (assoc key-id epg-user-id-alist)))
1764 (setq user-id (epg--decode-coding-string
1765 (epg--decode-percent-escape user-id)
1769 (setcdr entry user-id)
1770 (setq epg-user-id-alist (cons (cons key-id user-id)
1771 epg-user-id-alist))))))
1773 (defun epg--status-IMPORT_OK (context string)
1774 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1775 (let ((reason (string-to-number (match-string 1 string))))
1776 (epg-context-set-result-for
1777 context 'import-status
1778 (cons (epg-make-import-status (if (match-beginning 2)
1779 (match-string 3 string))
1781 (/= (logand reason 1) 0)
1782 (/= (logand reason 2) 0)
1783 (/= (logand reason 4) 0)
1784 (/= (logand reason 8) 0)
1785 (/= (logand reason 16) 0))
1786 (epg-context-result-for context 'import-status))))))
1788 (defun epg--status-IMPORT_PROBLEM (context string)
1789 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1790 (epg-context-set-result-for
1791 context 'import-status
1792 (cons (epg-make-import-status
1793 (if (match-beginning 2)
1794 (match-string 3 string))
1795 (string-to-number (match-string 1 string)))
1796 (epg-context-result-for context 'import-status)))))
1798 (defun epg--status-IMPORT_RES (context string)
1799 (when (string-match "\\`\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1800 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1801 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
1802 (epg-context-set-result-for
1804 (epg-make-import-result (string-to-number (match-string 1 string))
1805 (string-to-number (match-string 2 string))
1806 (string-to-number (match-string 3 string))
1807 (string-to-number (match-string 4 string))
1808 (string-to-number (match-string 5 string))
1809 (string-to-number (match-string 6 string))
1810 (string-to-number (match-string 7 string))
1811 (string-to-number (match-string 8 string))
1812 (string-to-number (match-string 9 string))
1813 (string-to-number (match-string 10 string))
1814 (string-to-number (match-string 11 string))
1815 (string-to-number (match-string 12 string))
1816 (string-to-number (match-string 13 string))
1817 (epg-context-result-for context 'import-status)))
1818 (epg-context-set-result-for context 'import-status nil)))
1820 (defun epg-passphrase-callback-function (context key-id _handback)
1821 (declare (obsolete epa-passphrase-callback-function "23.1"))
1822 (if (eq key-id 'SYM)
1823 (read-passwd "Passphrase for symmetric encryption: "
1824 (eq (epg-context-operation context) 'encrypt))
1826 (if (eq key-id 'PIN)
1827 "Passphrase for PIN: "
1828 (let ((entry (assoc key-id epg-user-id-alist)))
1830 (format "Passphrase for %s %s: " key-id (cdr entry))
1831 (format "Passphrase for %s: " key-id)))))))
1833 (defun epg--list-keys-1 (context name mode)
1834 (let ((args (append (if epg-gpg-home-directory
1835 (list "--homedir" epg-gpg-home-directory))
1836 '("--with-colons" "--no-greeting" "--batch"
1837 "--with-fingerprint" "--with-fingerprint")
1838 (unless (eq (epg-context-protocol context) 'CMS)
1839 '("--fixed-list-mode"))))
1840 (list-keys-option (if (memq mode '(t secret))
1841 "--list-secret-keys"
1842 (if (memq mode '(nil public))
1845 (coding-system-for-read 'binary)
1846 keys string field index)
1849 (unless (listp name)
1850 (setq name (list name)))
1852 (setq args (append args (list list-keys-option (car name)))
1854 (setq args (append args (list list-keys-option))))
1856 (apply #'call-process
1857 (if (eq (epg-context-protocol context) 'CMS)
1860 nil (list t nil) nil args)
1861 (goto-char (point-min))
1862 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1863 (setq keys (cons (make-vector 15 nil) keys)
1864 string (match-string 0)
1868 (string-match "\\([^:]+\\)?:" string index))
1869 (setq index (match-end 0))
1870 (aset (car keys) field (match-string 1 string))
1871 (setq field (1+ field))))
1874 (defun epg--make-sub-key-1 (line)
1877 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1879 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1881 (member (aref line 0) '("sec" "ssb"))
1882 (string-to-number (aref line 3))
1883 (string-to-number (aref line 2))
1885 (epg--time-from-seconds (aref line 5))
1887 (epg--time-from-seconds (aref line 6)))))
1889 (defun epg-list-keys (context &optional name mode)
1890 "Return a list of epg-key objects matched with NAME.
1891 If MODE is nil or 'public, only public keyring should be searched.
1892 If MODE is t or 'secret, only secret keyring should be searched.
1893 Otherwise, only public keyring should be searched and the key
1894 signatures should be included.
1895 NAME is either a string or a list of strings."
1896 (let ((lines (epg--list-keys-1 context name mode))
1897 keys cert pointer pointer-1 index string)
1900 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1901 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1902 keys (cons (epg-make-key
1903 (if (aref (car lines) 8)
1904 (cdr (assq (string-to-char (aref (car lines) 8))
1905 epg-key-validity-alist))))
1907 (epg-key-set-sub-key-list
1909 (cons (epg--make-sub-key-1 (car lines))
1910 (epg-key-sub-key-list (car keys)))))
1911 ((member (aref (car lines) 0) '("sub" "ssb"))
1912 (epg-key-set-sub-key-list
1914 (cons (epg--make-sub-key-1 (car lines))
1915 (epg-key-sub-key-list (car keys)))))
1916 ((equal (aref (car lines) 0) "uid")
1917 ;; Decode the UID name as a backslash escaped UTF-8 string,
1918 ;; generated by GnuPG/GpgSM.
1919 (setq string (copy-sequence (aref (car lines) 9))
1921 (while (string-match "\"" string index)
1922 (setq string (replace-match "\\\"" t t string)
1923 index (1+ (match-end 0))))
1925 (setq string (epg--decode-coding-string
1926 (car (read-from-string (concat "\"" string "\"")))
1929 (setq string (aref (car lines) 9))))
1930 (epg-key-set-user-id-list
1932 (cons (epg-make-user-id
1933 (if (aref (car lines) 1)
1934 (cdr (assq (string-to-char (aref (car lines) 1))
1935 epg-key-validity-alist)))
1938 (epg-dn-from-string string)
1941 (epg-key-user-id-list (car keys)))))
1942 ((equal (aref (car lines) 0) "fpr")
1943 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1944 (aref (car lines) 9)))
1945 ((equal (aref (car lines) 0) "sig")
1946 (epg-user-id-set-signature-list
1947 (car (epg-key-user-id-list (car keys)))
1949 (epg-make-key-signature
1950 (if (aref (car lines) 1)
1951 (cdr (assq (string-to-char (aref (car lines) 1))
1952 epg-key-validity-alist)))
1953 (string-to-number (aref (car lines) 3))
1954 (aref (car lines) 4)
1955 (epg--time-from-seconds (aref (car lines) 5))
1956 (epg--time-from-seconds (aref (car lines) 6))
1957 (aref (car lines) 9)
1958 (string-to-number (aref (car lines) 10) 16)
1959 (eq (aref (aref (car lines) 10) 2) ?x))
1960 (epg-user-id-signature-list
1961 (car (epg-key-user-id-list (car keys))))))))
1962 (setq lines (cdr lines)))
1963 (setq keys (nreverse keys)
1966 (epg-key-set-sub-key-list
1968 (nreverse (epg-key-sub-key-list (car pointer))))
1969 (setq pointer-1 (epg-key-set-user-id-list
1971 (nreverse (epg-key-user-id-list (car pointer)))))
1973 (epg-user-id-set-signature-list
1975 (nreverse (epg-user-id-signature-list (car pointer-1))))
1976 (setq pointer-1 (cdr pointer-1)))
1977 (setq pointer (cdr pointer)))
1981 (if (fboundp 'make-temp-file)
1982 (defalias 'epg--make-temp-file 'make-temp-file)
1983 (defvar temporary-file-directory)
1984 ;; stolen from poe.el.
1985 (defun epg--make-temp-file (prefix)
1986 "Create a temporary file.
1987 The returned file name (created by appending some random characters at the end
1988 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1989 is guaranteed to point to a newly created empty file.
1990 You can then use `write-region' to write new data into the file."
1991 (let ((orig-modes (default-file-modes))
1993 (setq prefix (expand-file-name prefix
1994 (if (featurep 'xemacs)
1996 temporary-file-directory)))
1999 ;; First, create a temporary directory.
2000 (set-default-file-modes #o700)
2001 (while (condition-case ()
2003 (setq tempdir (make-temp-name
2005 (file-name-directory prefix)
2007 ;; return nil or signal an error.
2008 (make-directory tempdir))
2010 (file-already-exists t)))
2011 ;; Second, create a temporary file in the tempdir.
2012 ;; There *is* a race condition between `make-temp-name'
2013 ;; and `write-region', but we don't care it since we are
2014 ;; in a private directory now.
2015 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
2016 (write-region "" nil tempfile nil 'silent)
2017 ;; Finally, make a hard-link from the tempfile.
2018 (while (condition-case ()
2020 (setq file (make-temp-name prefix))
2021 ;; return nil or signal an error.
2022 (add-name-to-file tempfile file))
2024 (file-already-exists t)))
2026 (set-default-file-modes orig-modes)
2027 ;; Cleanup the tempfile.
2029 (file-exists-p tempfile)
2030 (delete-file tempfile))
2031 ;; Cleanup the tempdir.
2033 (file-directory-p tempdir)
2034 (delete-directory tempdir)))))))
2036 (defun epg--args-from-sig-notations (notations)
2040 (if (and (epg-sig-notation-name notation)
2041 (not (epg-sig-notation-human-readable notation)))
2042 (error "Unreadable"))
2043 (if (epg-sig-notation-name notation)
2044 (list "--sig-notation"
2045 (if (epg-sig-notation-critical notation)
2046 (concat "!" (epg-sig-notation-name notation)
2047 "=" (epg-sig-notation-value notation))
2048 (concat (epg-sig-notation-name notation)
2049 "=" (epg-sig-notation-value notation))))
2050 (list "--sig-policy-url"
2051 (if (epg-sig-notation-critical notation)
2052 (concat "!" (epg-sig-notation-value notation))
2053 (epg-sig-notation-value notation)))))
2056 (defun epg-cancel (context)
2057 (if (buffer-live-p (process-buffer (epg-context-process context)))
2058 (with-current-buffer (process-buffer (epg-context-process context))
2059 (epg-context-set-result-for
2062 (epg-context-result-for epg-context 'error)))))
2063 (if (eq (process-status (epg-context-process context)) 'run)
2064 (delete-process (epg-context-process context))))
2066 (defun epg-start-decrypt (context cipher)
2067 "Initiate a decrypt operation on CIPHER.
2068 CIPHER must be a file data object.
2070 If you use this function, you will need to wait for the completion of
2071 `epg-gpg-program' by using `epg-wait-for-completion' and call
2072 `epg-reset' to clear a temporary output file.
2073 If you are unsure, use synchronous version of this function
2074 `epg-decrypt-file' or `epg-decrypt-string' instead."
2075 (unless (epg-data-file cipher)
2076 (error "Not a file"))
2077 (epg-context-set-operation context 'decrypt)
2078 (epg-context-set-result context nil)
2079 (epg--start context (list "--decrypt" "--" (epg-data-file cipher)))
2080 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2081 (unless (eq (epg-context-protocol context) 'CMS)
2082 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
2084 (defun epg--check-error-for-decrypt (context)
2085 (let ((errors (epg-context-result-for context 'error)))
2086 (if (epg-context-result-for context 'decryption-failed)
2088 (list "Decryption failed" (epg-errors-to-string errors))))
2089 (unless (epg-context-result-for context 'decryption-okay)
2091 (list "Can't decrypt" (epg-errors-to-string errors))))))
2093 (defun epg-decrypt-file (context cipher plain)
2094 "Decrypt a file CIPHER and store the result to a file PLAIN.
2095 If PLAIN is nil, it returns the result as a string."
2099 (epg-context-set-output-file context plain)
2100 (epg-context-set-output-file context
2101 (epg--make-temp-file "epg-output")))
2102 (epg-start-decrypt context (epg-make-data-from-file cipher))
2103 (epg-wait-for-completion context)
2104 (epg--check-error-for-decrypt context)
2106 (epg-read-output context)))
2108 (epg-delete-output-file context))
2109 (epg-reset context)))
2111 (defun epg-decrypt-string (context cipher)
2112 "Decrypt a string CIPHER and return the plain text."
2113 (let ((input-file (epg--make-temp-file "epg-input"))
2114 (coding-system-for-write 'binary))
2117 (write-region cipher nil input-file nil 'quiet)
2118 (epg-context-set-output-file context
2119 (epg--make-temp-file "epg-output"))
2120 (epg-start-decrypt context (epg-make-data-from-file input-file))
2121 (epg-wait-for-completion context)
2122 (epg--check-error-for-decrypt context)
2123 (epg-read-output context))
2124 (epg-delete-output-file context)
2125 (if (file-exists-p input-file)
2126 (delete-file input-file))
2127 (epg-reset context))))
2129 (defun epg-start-verify (context signature &optional signed-text)
2130 "Initiate a verify operation on SIGNATURE.
2131 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
2133 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
2134 For a normal or a cleartext signature, SIGNED-TEXT should be nil.
2136 If you use this function, you will need to wait for the completion of
2137 `epg-gpg-program' by using `epg-wait-for-completion' and call
2138 `epg-reset' to clear a temporary output file.
2139 If you are unsure, use synchronous version of this function
2140 `epg-verify-file' or `epg-verify-string' instead."
2141 (epg-context-set-operation context 'verify)
2142 (epg-context-set-result context nil)
2144 ;; Detached signature.
2145 (if (epg-data-file signed-text)
2146 (epg--start context (list "--verify" "--" (epg-data-file signature)
2147 (epg-data-file signed-text)))
2148 (epg--start context (list "--verify" "--" (epg-data-file signature)
2150 (if (eq (process-status (epg-context-process context)) 'run)
2151 (process-send-string (epg-context-process context)
2152 (epg-data-string signed-text)))
2153 (if (eq (process-status (epg-context-process context)) 'run)
2154 (process-send-eof (epg-context-process context))))
2155 ;; Normal (or cleartext) signature.
2156 (if (epg-data-file signature)
2157 (epg--start context (if (eq (epg-context-protocol context) 'CMS)
2158 (list "--verify" "--" (epg-data-file signature))
2159 (list "--" (epg-data-file signature))))
2160 (epg--start context (if (eq (epg-context-protocol context) 'CMS)
2163 (if (eq (process-status (epg-context-process context)) 'run)
2164 (process-send-string (epg-context-process context)
2165 (epg-data-string signature)))
2166 (if (eq (process-status (epg-context-process context)) 'run)
2167 (process-send-eof (epg-context-process context))))))
2169 (defun epg-verify-file (context signature &optional signed-text plain)
2170 "Verify a file SIGNATURE.
2171 SIGNED-TEXT and PLAIN are also a file if they are specified.
2173 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
2174 string. For a normal or a cleartext signature, SIGNED-TEXT should be
2175 nil. In the latter case, if PLAIN is specified, the plaintext is
2176 stored into the file after successful verification."
2180 (epg-context-set-output-file context plain)
2181 (epg-context-set-output-file context
2182 (epg--make-temp-file "epg-output")))
2184 (epg-start-verify context
2185 (epg-make-data-from-file signature)
2186 (epg-make-data-from-file signed-text))
2187 (epg-start-verify context
2188 (epg-make-data-from-file signature)))
2189 (epg-wait-for-completion context)
2191 (epg-read-output context)))
2193 (epg-delete-output-file context))
2194 (epg-reset context)))
2196 (defun epg-verify-string (context signature &optional signed-text)
2197 "Verify a string SIGNATURE.
2198 SIGNED-TEXT is a string if it is specified.
2200 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
2201 string. For a normal or a cleartext signature, SIGNED-TEXT should be
2202 nil. In the latter case, this function returns the plaintext after
2203 successful verification."
2204 (let ((coding-system-for-write 'binary)
2208 (epg-context-set-output-file context
2209 (epg--make-temp-file "epg-output"))
2212 (setq input-file (epg--make-temp-file "epg-signature"))
2213 (write-region signature nil input-file nil 'quiet)
2214 (epg-start-verify context
2215 (epg-make-data-from-file input-file)
2216 (epg-make-data-from-string signed-text)))
2217 (epg-start-verify context (epg-make-data-from-string signature)))
2218 (epg-wait-for-completion context)
2219 (epg-read-output context))
2220 (epg-delete-output-file context)
2222 (file-exists-p input-file))
2223 (delete-file input-file))
2224 (epg-reset context))))
2226 (defun epg-start-sign (context plain &optional mode)
2227 "Initiate a sign operation on PLAIN.
2228 PLAIN is a data object.
2230 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2231 If it is nil or 'normal, it makes a normal signature.
2232 Otherwise, it makes a cleartext signature.
2234 If you use this function, you will need to wait for the completion of
2235 `epg-gpg-program' by using `epg-wait-for-completion' and call
2236 `epg-reset' to clear a temporary output file.
2237 If you are unsure, use synchronous version of this function
2238 `epg-sign-file' or `epg-sign-string' instead."
2239 (epg-context-set-operation context 'sign)
2240 (epg-context-set-result context nil)
2241 (unless (memq mode '(t detached nil normal)) ;i.e. cleartext
2242 (epg-context-set-armor context nil)
2243 (epg-context-set-textmode context nil))
2245 (append (list (if (memq mode '(t detached))
2247 (if (memq mode '(nil normal))
2255 (car (epg-key-sub-key-list signer)))))
2256 (epg-context-signers context)))
2257 (epg--args-from-sig-notations
2258 (epg-context-sig-notations context))
2259 (if (epg-data-file plain)
2260 (list "--" (epg-data-file plain)))))
2261 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2262 (unless (eq (epg-context-protocol context) 'CMS)
2263 (epg-wait-for-status context '("BEGIN_SIGNING")))
2264 (when (epg-data-string plain)
2265 (if (eq (process-status (epg-context-process context)) 'run)
2266 (process-send-string (epg-context-process context)
2267 (epg-data-string plain)))
2268 (if (eq (process-status (epg-context-process context)) 'run)
2269 (process-send-eof (epg-context-process context)))))
2271 (defun epg-sign-file (context plain signature &optional mode)
2272 "Sign a file PLAIN and store the result to a file SIGNATURE.
2273 If SIGNATURE is nil, it returns the result as a string.
2274 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2275 If it is nil or 'normal, it makes a normal signature.
2276 Otherwise, it makes a cleartext signature."
2280 (epg-context-set-output-file context signature)
2281 (epg-context-set-output-file context
2282 (epg--make-temp-file "epg-output")))
2283 (epg-start-sign context (epg-make-data-from-file plain) mode)
2284 (epg-wait-for-completion context)
2285 (unless (epg-context-result-for context 'sign)
2286 (let ((errors (epg-context-result-for context 'error)))
2288 (list "Sign failed" (epg-errors-to-string errors)))))
2290 (epg-read-output context)))
2292 (epg-delete-output-file context))
2293 (epg-reset context)))
2295 (defun epg-sign-string (context plain &optional mode)
2296 "Sign a string PLAIN and return the output as 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."
2301 (unless (or (eq (epg-context-protocol context) 'CMS)
2304 (epg-check-configuration (epg-configuration))
2307 (epg--make-temp-file "epg-input")))
2308 (coding-system-for-write 'binary))
2311 (epg-context-set-output-file context
2312 (epg--make-temp-file "epg-output"))
2314 (write-region plain nil input-file nil 'quiet))
2315 (epg-start-sign context
2317 (epg-make-data-from-file input-file)
2318 (epg-make-data-from-string plain))
2320 (epg-wait-for-completion context)
2321 (unless (epg-context-result-for context 'sign)
2322 (if (epg-context-result-for context 'error)
2323 (let ((errors (epg-context-result-for context 'error)))
2325 (list "Sign failed" (epg-errors-to-string errors))))))
2326 (epg-read-output context))
2327 (epg-delete-output-file context)
2329 (delete-file input-file))
2330 (epg-reset context))))
2332 (defun epg-start-encrypt (context plain recipients
2333 &optional sign always-trust)
2334 "Initiate an encrypt operation on PLAIN.
2335 PLAIN is a data object.
2336 If RECIPIENTS is nil, it performs symmetric encryption.
2338 If you use this function, you will need to wait for the completion of
2339 `epg-gpg-program' by using `epg-wait-for-completion' and call
2340 `epg-reset' to clear a temporary output file.
2341 If you are unsure, use synchronous version of this function
2342 `epg-encrypt-file' or `epg-encrypt-string' instead."
2343 (epg-context-set-operation context 'encrypt)
2344 (epg-context-set-result context nil)
2346 (append (if always-trust '("--always-trust"))
2347 (if recipients '("--encrypt") '("--symmetric"))
2348 (if sign '("--sign"))
2355 (car (epg-key-sub-key-list
2357 (epg-context-signers context))))
2359 (epg--args-from-sig-notations
2360 (epg-context-sig-notations context)))
2366 (car (epg-key-sub-key-list recipient)))))
2368 (if (epg-data-file plain)
2369 (list "--" (epg-data-file plain)))))
2370 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2371 (unless (eq (epg-context-protocol context) 'CMS)
2373 (epg-wait-for-status context '("BEGIN_SIGNING"))
2374 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
2375 (when (epg-data-string plain)
2376 (if (eq (process-status (epg-context-process context)) 'run)
2377 (process-send-string (epg-context-process context)
2378 (epg-data-string plain)))
2379 (if (eq (process-status (epg-context-process context)) 'run)
2380 (process-send-eof (epg-context-process context)))))
2382 (defun epg-encrypt-file (context plain recipients
2383 cipher &optional sign always-trust)
2384 "Encrypt a file PLAIN and store the result to a file CIPHER.
2385 If CIPHER is nil, it returns the result as a string.
2386 If RECIPIENTS is nil, it performs symmetric encryption."
2390 (epg-context-set-output-file context cipher)
2391 (epg-context-set-output-file context
2392 (epg--make-temp-file "epg-output")))
2393 (epg-start-encrypt context (epg-make-data-from-file plain)
2394 recipients sign always-trust)
2395 (epg-wait-for-completion context)
2396 (let ((errors (epg-context-result-for context 'error)))
2398 (not (epg-context-result-for context 'sign)))
2400 (list "Sign failed" (epg-errors-to-string errors))))
2403 (list "Encrypt failed" (epg-errors-to-string errors)))))
2405 (epg-read-output context)))
2407 (epg-delete-output-file context))
2408 (epg-reset context)))
2410 (defun epg-encrypt-string (context plain recipients
2411 &optional sign always-trust)
2412 "Encrypt a string PLAIN.
2413 If RECIPIENTS is nil, it performs symmetric encryption."
2415 (unless (or (not sign)
2416 (eq (epg-context-protocol context) 'CMS)
2419 (epg-check-configuration (epg-configuration))
2422 (epg--make-temp-file "epg-input")))
2423 (coding-system-for-write 'binary))
2426 (epg-context-set-output-file context
2427 (epg--make-temp-file "epg-output"))
2429 (write-region plain nil input-file nil 'quiet))
2430 (epg-start-encrypt context
2432 (epg-make-data-from-file input-file)
2433 (epg-make-data-from-string plain))
2434 recipients sign always-trust)
2435 (epg-wait-for-completion context)
2436 (let ((errors (epg-context-result-for context 'error)))
2438 (not (epg-context-result-for context 'sign)))
2440 (list "Sign failed" (epg-errors-to-string errors))))
2443 (list "Encrypt failed" (epg-errors-to-string errors)))))
2444 (epg-read-output context))
2445 (epg-delete-output-file context)
2447 (delete-file input-file))
2448 (epg-reset context))))
2450 (defun epg-start-export-keys (context keys)
2451 "Initiate an export keys operation.
2453 If you use this function, you will need to wait for the completion of
2454 `epg-gpg-program' by using `epg-wait-for-completion' and call
2455 `epg-reset' to clear a temporary output file.
2456 If you are unsure, use synchronous version of this function
2457 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
2458 (epg-context-set-operation context 'export-keys)
2459 (epg-context-set-result context nil)
2460 (epg--start context (cons "--export"
2464 (car (epg-key-sub-key-list key))))
2467 (defun epg-export-keys-to-file (context keys file)
2468 "Extract public KEYS."
2472 (epg-context-set-output-file context file)
2473 (epg-context-set-output-file context
2474 (epg--make-temp-file "epg-output")))
2475 (epg-start-export-keys context keys)
2476 (epg-wait-for-completion context)
2477 (let ((errors (epg-context-result-for context 'error)))
2480 (list "Export keys failed"
2481 (epg-errors-to-string errors)))))
2483 (epg-read-output context)))
2485 (epg-delete-output-file context))
2486 (epg-reset context)))
2488 (defun epg-export-keys-to-string (context keys)
2489 "Extract public KEYS and return them as a string."
2490 (epg-export-keys-to-file context keys nil))
2492 (defun epg-start-import-keys (context keys)
2493 "Initiate an import keys operation.
2494 KEYS is a data object.
2496 If you use this function, you will need to wait for the completion of
2497 `epg-gpg-program' by using `epg-wait-for-completion' and call
2498 `epg-reset' to clear a temporary output file.
2499 If you are unsure, use synchronous version of this function
2500 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
2501 (epg-context-set-operation context 'import-keys)
2502 (epg-context-set-result context nil)
2503 (epg--start context (if (epg-data-file keys)
2504 (list "--import" "--" (epg-data-file keys))
2506 (when (epg-data-string keys)
2507 (if (eq (process-status (epg-context-process context)) 'run)
2508 (process-send-string (epg-context-process context)
2509 (epg-data-string keys)))
2510 (if (eq (process-status (epg-context-process context)) 'run)
2511 (process-send-eof (epg-context-process context)))))
2513 (defun epg--import-keys-1 (context keys)
2516 (epg-start-import-keys context keys)
2517 (epg-wait-for-completion context)
2518 (let ((errors (epg-context-result-for context 'error)))
2521 (list "Import keys failed"
2522 (epg-errors-to-string errors))))))
2523 (epg-reset context)))
2525 (defun epg-import-keys-from-file (context keys)
2526 "Add keys from a file KEYS."
2527 (epg--import-keys-1 context (epg-make-data-from-file keys)))
2529 (defun epg-import-keys-from-string (context keys)
2530 "Add keys from a string KEYS."
2531 (epg--import-keys-1 context (epg-make-data-from-string keys)))
2533 (defun epg-start-receive-keys (context key-id-list)
2534 "Initiate a receive key operation.
2535 KEY-ID-LIST is a list of key IDs.
2537 If you use this function, you will need to wait for the completion of
2538 `epg-gpg-program' by using `epg-wait-for-completion' and call
2539 `epg-reset' to clear a temporary output file.
2540 If you are unsure, use synchronous version of this function
2541 `epg-receive-keys' instead."
2542 (epg-context-set-operation context 'receive-keys)
2543 (epg-context-set-result context nil)
2544 (epg--start context (cons "--recv-keys" key-id-list)))
2546 (defun epg-receive-keys (context keys)
2547 "Add keys from server.
2548 KEYS is a list of key IDs"
2551 (epg-start-receive-keys context keys)
2552 (epg-wait-for-completion context)
2553 (let ((errors (epg-context-result-for context 'error)))
2556 (list "Receive keys failed"
2557 (epg-errors-to-string errors))))))
2558 (epg-reset context)))
2560 (defalias 'epg-import-keys-from-server 'epg-receive-keys)
2562 (defun epg-start-delete-keys (context keys &optional allow-secret)
2563 "Initiate a delete keys operation.
2565 If you use this function, you will need to wait for the completion of
2566 `epg-gpg-program' by using `epg-wait-for-completion' and call
2567 `epg-reset' to clear a temporary output file.
2568 If you are unsure, use synchronous version of this function
2569 `epg-delete-keys' instead."
2570 (epg-context-set-operation context 'delete-keys)
2571 (epg-context-set-result context nil)
2572 (epg--start context (cons (if allow-secret
2573 "--delete-secret-key"
2578 (car (epg-key-sub-key-list key))))
2581 (defun epg-delete-keys (context keys &optional allow-secret)
2582 "Delete KEYS from the key ring."
2585 (epg-start-delete-keys context keys allow-secret)
2586 (epg-wait-for-completion context)
2587 (let ((errors (epg-context-result-for context 'error)))
2590 (list "Delete keys failed"
2591 (epg-errors-to-string errors))))))
2592 (epg-reset context)))
2594 (defun epg-start-sign-keys (context keys &optional local)
2595 "Initiate a sign keys operation.
2597 If you use this function, you will need to wait for the completion of
2598 `epg-gpg-program' by using `epg-wait-for-completion' and call
2599 `epg-reset' to clear a temporary output file.
2600 If you are unsure, use synchronous version of this function
2601 `epg-sign-keys' instead."
2602 (declare (obsolete nil "23.1"))
2603 (epg-context-set-operation context 'sign-keys)
2604 (epg-context-set-result context nil)
2605 (epg--start context (cons (if local
2611 (car (epg-key-sub-key-list key))))
2614 (defun epg-sign-keys (context keys &optional local)
2615 "Sign KEYS from the key ring."
2616 (declare (obsolete nil "23.1"))
2619 (epg-start-sign-keys context keys local)
2620 (epg-wait-for-completion context)
2621 (let ((errors (epg-context-result-for context 'error)))
2624 (list "Sign keys failed"
2625 (epg-errors-to-string errors))))))
2626 (epg-reset context)))
2628 (defun epg-start-generate-key (context parameters)
2629 "Initiate a key generation.
2630 PARAMETERS specifies parameters for the key.
2632 If you use this function, you will need to wait for the completion of
2633 `epg-gpg-program' by using `epg-wait-for-completion' and call
2634 `epg-reset' to clear a temporary output file.
2635 If you are unsure, use synchronous version of this function
2636 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2637 (epg-context-set-operation context 'generate-key)
2638 (epg-context-set-result context nil)
2639 (if (epg-data-file parameters)
2640 (epg--start context (list "--batch" "--genkey" "--"
2641 (epg-data-file parameters)))
2642 (epg--start context '("--batch" "--genkey"))
2643 (if (eq (process-status (epg-context-process context)) 'run)
2644 (process-send-string (epg-context-process context)
2645 (epg-data-string parameters)))
2646 (if (eq (process-status (epg-context-process context)) 'run)
2647 (process-send-eof (epg-context-process context)))))
2649 (defun epg-generate-key-from-file (context parameters)
2650 "Generate a new key pair.
2651 PARAMETERS is a file which tells how to create the key."
2654 (epg-start-generate-key context (epg-make-data-from-file parameters))
2655 (epg-wait-for-completion context)
2656 (let ((errors (epg-context-result-for context 'error)))
2659 (list "Generate key failed"
2660 (epg-errors-to-string errors))))))
2661 (epg-reset context)))
2663 (defun epg-generate-key-from-string (context parameters)
2664 "Generate a new key pair.
2665 PARAMETERS is a string which tells how to create the key."
2668 (epg-start-generate-key context (epg-make-data-from-string parameters))
2669 (epg-wait-for-completion context)
2670 (let ((errors (epg-context-result-for context 'error)))
2673 (list "Generate key failed"
2674 (epg-errors-to-string errors))))))
2675 (epg-reset context)))
2677 (defun epg--decode-percent-escape (string)
2679 (while (string-match "%\\(\\(%\\)\\|\\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2681 (if (match-beginning 2)
2682 (setq string (replace-match "%" t t string)
2683 index (1- (match-end 0)))
2684 (setq string (replace-match
2685 (string (string-to-number (match-string 3 string) 16))
2687 index (- (match-end 0) 2))))
2690 (defun epg--decode-hexstring (string)
2692 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
2693 (setq string (replace-match (string (string-to-number
2694 (match-string 0 string) 16))
2696 index (1- (match-end 0))))
2699 (defun epg--decode-quotedstring (string)
2701 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2702 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2704 (if (match-beginning 2)
2705 (setq string (replace-match "\\2" t nil string)
2706 index (1- (match-end 0)))
2707 (if (match-beginning 3)
2708 (setq string (replace-match (string (string-to-number
2709 (match-string 0 string) 16))
2711 index (- (match-end 0) 2)))))
2714 (defun epg-dn-from-string (string)
2715 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2716 The return value is an alist mapping from types to values."
2718 (length (length string))
2719 alist type value group)
2720 (while (< index length)
2721 (if (eq index (string-match "[ \t\n\r]*" string index))
2722 (setq index (match-end 0)))
2723 (if (eq index (string-match
2724 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2726 (setq type (match-string 1 string)
2727 index (match-end 0))
2728 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2730 (setq type (match-string 1 string)
2731 index (match-end 0))))
2733 (error "Invalid type"))
2734 (if (eq index (string-match
2735 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2737 (setq index (match-end 0)
2738 value (epg--decode-quotedstring (match-string 0 string)))
2739 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
2740 (setq index (match-end 0)
2741 value (epg--decode-hexstring (match-string 1 string)))
2742 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2744 (setq index (match-end 0)
2745 value (epg--decode-quotedstring
2746 (match-string 0 string))))))
2748 (if (stringp (car (car alist)))
2749 (setcar alist (list (cons type value) (car alist)))
2750 (setcar alist (cons (cons type value) (car alist))))
2751 (if (consp (car (car alist)))
2752 (setcar alist (nreverse (car alist))))
2753 (setq alist (cons (cons type value) alist)
2756 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
2757 (setq index (match-end 0)
2758 group (eq (aref string (match-beginning 1)) ?+))))
2761 (defun epg-decode-dn (alist)
2762 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2763 Type names are resolved using `epg-dn-type-alist'."
2766 (if (stringp (car rdn))
2767 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
2769 (format "%s=%s" (cdr entry) (cdr rdn))
2770 (format "%s=%s" (car rdn) (cdr rdn))))
2771 (concat "(" (epg-decode-dn rdn) ")")))
2777 ;;; epg.el ends here