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
)
41 ;; from gnupg/include/cipher.h
42 (defconst epg-cipher-algorithm-alist
56 ;; from gnupg/include/cipher.h
57 (defconst epg-pubkey-algorithm-alist
65 ;; from gnupg/include/cipher.h
66 (defconst epg-digest-algorithm-alist
75 ;; from gnupg/include/cipher.h
76 (defconst epg-compress-algorithm-alist
82 (defconst epg-invalid-recipients-reason-alist
83 '((0 .
"No specific reason given")
85 (2 .
"Ambiguous specification")
86 (3 .
"Wrong key usage")
91 (8 .
"Policy mismatch")
92 (9 .
"Not a secret key")
93 (10 .
"Key not trusted")))
95 (defconst epg-delete-problem-reason-alist
97 (2 .
"Must delete secret key first")
98 (3 .
"Ambiguous specification")))
100 (defconst epg-import-ok-reason-alist
101 '((0 .
"Not actually changed")
102 (1 .
"Entirely new key")
104 (4 .
"New signatures")
106 (16 .
"Contains private key")))
108 (defconst epg-import-problem-reason-alist
109 '((0 .
"No specific reason given")
110 (1 .
"Invalid Certificate")
111 (2 .
"Issuer Certificate missing")
112 (3 .
"Certificate Chain too long")
113 (4 .
"Error storing certificate")))
115 (defconst epg-no-data-reason-alist
116 '((1 .
"No armored data")
117 (2 .
"Expected a packet but did not found one")
118 (3 .
"Invalid packet found, this may indicate a non OpenPGP message")
119 (4 .
"Signature expected but not found")))
121 (defconst epg-unexpected-reason-alist nil
)
123 (defvar epg-key-validity-alist
136 (defvar epg-key-capablity-alist
140 (?a . authentication
)
143 (defvar epg-new-signature-type-alist
148 (defvar epg-dn-type-alist
149 '(("1.2.840.113549.1.9.1" .
"EMail")
153 ("0.2.262.1.10.7.20" .
"NameDistinguisher")
154 ("2.5.4.16" .
"ADDR")
157 ("2.5.4.17" .
"PostalCode")
158 ("2.5.4.65" .
"Pseudo")
159 ("2.5.4.5" .
"SerialNumber")))
161 (defvar epg-prompt-alist nil
)
163 (put 'epg-error
'error-conditions
'(epg-error error
))
164 (put 'epg-error
'error-message
"GPG error")
166 (defun epg-make-data-from-file (file)
167 "Make a data object from FILE."
168 (cons 'epg-data
(vector file nil
)))
170 (defun epg-make-data-from-string (string)
171 "Make a data object from STRING."
172 (cons 'epg-data
(vector nil string
)))
174 (defun epg-data-file (data)
175 "Return the file of DATA."
176 (unless (eq (car-safe data
) 'epg-data
)
177 (signal 'wrong-type-argument
(list 'epg-data-p data
)))
180 (defun epg-data-string (data)
181 "Return the string of DATA."
182 (unless (eq (car-safe data
) 'epg-data
)
183 (signal 'wrong-type-argument
(list 'epg-data-p data
)))
187 (defun epg-make-context (&optional protocol armor textmode include-certs
188 cipher-algorithm digest-algorithm
190 "Return a context object."
192 (vector (or protocol
'OpenPGP
) armor textmode include-certs
193 cipher-algorithm digest-algorithm compress-algorithm
194 (list #'epg-passphrase-callback-function
)
196 nil nil nil nil nil nil
)))
198 (defun epg-context-protocol (context)
199 "Return the protocol used within CONTEXT."
200 (unless (eq (car-safe context
) 'epg-context
)
201 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
202 (aref (cdr context
) 0))
204 (defun epg-context-armor (context)
205 "Return t if the output should be ASCII armored in CONTEXT."
206 (unless (eq (car-safe context
) 'epg-context
)
207 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
208 (aref (cdr context
) 1))
210 (defun epg-context-textmode (context)
211 "Return t if canonical text mode should be used in CONTEXT."
212 (unless (eq (car-safe context
) 'epg-context
)
213 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
214 (aref (cdr context
) 2))
216 (defun epg-context-include-certs (context)
217 "Return how many certificates should be included in an S/MIME signed message."
218 (unless (eq (car-safe context
) 'epg-context
)
219 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
220 (aref (cdr context
) 3))
222 (defun epg-context-cipher-algorithm (context)
223 "Return the cipher algorithm in CONTEXT."
224 (unless (eq (car-safe context
) 'epg-context
)
225 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
226 (aref (cdr context
) 4))
228 (defun epg-context-digest-algorithm (context)
229 "Return the digest algorithm in CONTEXT."
230 (unless (eq (car-safe context
) 'epg-context
)
231 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
232 (aref (cdr context
) 5))
234 (defun epg-context-compress-algorithm (context)
235 "Return the compress algorithm in CONTEXT."
236 (unless (eq (car-safe context
) 'epg-context
)
237 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
238 (aref (cdr context
) 6))
240 (defun epg-context-passphrase-callback (context)
241 "Return the function used to query passphrase."
242 (unless (eq (car-safe context
) 'epg-context
)
243 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
244 (aref (cdr context
) 7))
246 (defun epg-context-progress-callback (context)
247 "Return the function which handles progress update."
248 (unless (eq (car-safe context
) 'epg-context
)
249 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
250 (aref (cdr context
) 8))
252 (defun epg-context-signers (context)
253 "Return the list of key-id for signing."
254 (unless (eq (car-safe context
) 'epg-context
)
255 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
256 (aref (cdr context
) 9))
258 (defun epg-context-sig-notations (context)
259 "Return the list of notations for signing."
260 (unless (eq (car-safe context
) 'epg-context
)
261 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
262 (aref (cdr context
) 10))
264 (defun epg-context-process (context)
265 "Return the process object of `epg-gpg-program'.
266 This function is for internal use only."
267 (unless (eq (car-safe context
) 'epg-context
)
268 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
269 (aref (cdr context
) 11))
271 (defun epg-context-output-file (context)
272 "Return the output file of `epg-gpg-program'.
273 This function is for internal use only."
274 (unless (eq (car-safe context
) 'epg-context
)
275 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
276 (aref (cdr context
) 12))
278 (defun epg-context-result (context)
279 "Return the result of the previous cryptographic operation."
280 (unless (eq (car-safe context
) 'epg-context
)
281 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
282 (aref (cdr context
) 13))
284 (defun epg-context-operation (context)
285 "Return the name of the current cryptographic operation."
286 (unless (eq (car-safe context
) 'epg-context
)
287 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
288 (aref (cdr context
) 14))
290 (defun epg-context-set-protocol (context protocol
)
291 "Set the protocol used within CONTEXT."
292 (unless (eq (car-safe context
) 'epg-context
)
293 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
294 (aset (cdr context
) 0 protocol
))
296 (defun epg-context-set-armor (context armor
)
297 "Specify if the output should be ASCII armored in CONTEXT."
298 (unless (eq (car-safe context
) 'epg-context
)
299 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
300 (aset (cdr context
) 1 armor
))
302 (defun epg-context-set-textmode (context textmode
)
303 "Specify if canonical text mode should be used in CONTEXT."
304 (unless (eq (car-safe context
) 'epg-context
)
305 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
306 (aset (cdr context
) 2 textmode
))
308 (defun epg-context-set-include-certs (context include-certs
)
309 "Set how many certificates should be included in an S/MIME signed message."
310 (unless (eq (car-safe context
) 'epg-context
)
311 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
312 (aset (cdr context
) 3 include-certs
))
314 (defun epg-context-set-cipher-algorithm (context cipher-algorithm
)
315 "Set the cipher algorithm in CONTEXT."
316 (unless (eq (car-safe context
) 'epg-context
)
317 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
318 (aset (cdr context
) 4 cipher-algorithm
))
320 (defun epg-context-set-digest-algorithm (context digest-algorithm
)
321 "Set the digest algorithm in CONTEXT."
322 (unless (eq (car-safe context
) 'epg-context
)
323 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
324 (aset (cdr context
) 5 digest-algorithm
))
326 (defun epg-context-set-compress-algorithm (context compress-algorithm
)
327 "Set the compress algorithm in CONTEXT."
328 (unless (eq (car-safe context
) 'epg-context
)
329 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
330 (aset (cdr context
) 6 compress-algorithm
))
332 (defun epg-context-set-passphrase-callback (context
334 "Set the function used to query passphrase.
336 PASSPHRASE-CALLBACK is either a function, or a cons-cell whose
337 car is a function and cdr is a callback data.
339 The function gets three arguments: the context, the key-id in
340 question, and the callback data (if any).
342 The callback may not be called if you use GnuPG 2.x, which relies
343 on the external program called `gpg-agent' for passphrase query.
344 If you really want to intercept passphrase query, consider
345 installing GnuPG 1.x _along with_ GnuPG 2.x, which does passphrase
346 query by itself and Emacs can intercept them."
347 (unless (eq (car-safe context
) 'epg-context
)
348 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
349 (aset (cdr context
) 7 (if (consp passphrase-callback
)
351 (list passphrase-callback
))))
353 (defun epg-context-set-progress-callback (context
355 "Set the function which handles progress update.
357 PROGRESS-CALLBACK is either a function, or a cons-cell whose
358 car is a function and cdr is a callback data.
360 The function gets six arguments: the context, the operation
361 description, the character to display a progress unit, the
362 current amount done, the total amount to be done, and the
363 callback data (if any)."
364 (unless (eq (car-safe context
) 'epg-context
)
365 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
366 (aset (cdr context
) 8 (if (consp progress-callback
)
368 (list progress-callback
))))
370 (defun epg-context-set-signers (context signers
)
371 "Set the list of key-id for signing."
372 (unless (eq (car-safe context
) 'epg-context
)
373 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
374 (aset (cdr context
) 9 signers
))
376 (defun epg-context-set-sig-notations (context notations
)
377 "Set the list of notations for signing."
378 (unless (eq (car-safe context
) 'epg-context
)
379 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
380 (aset (cdr context
) 10 notations
))
382 (defun epg-context-set-process (context process
)
383 "Set the process object of `epg-gpg-program'.
384 This function is for internal use only."
385 (unless (eq (car-safe context
) 'epg-context
)
386 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
387 (aset (cdr context
) 11 process
))
389 (defun epg-context-set-output-file (context output-file
)
390 "Set the output file of `epg-gpg-program'.
391 This function is for internal use only."
392 (unless (eq (car-safe context
) 'epg-context
)
393 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
394 (aset (cdr context
) 12 output-file
))
396 (defun epg-context-set-result (context result
)
397 "Set the result of the previous cryptographic operation."
398 (unless (eq (car-safe context
) 'epg-context
)
399 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
400 (aset (cdr context
) 13 result
))
402 (defun epg-context-set-operation (context operation
)
403 "Set the name of the current cryptographic operation."
404 (unless (eq (car-safe context
) 'epg-context
)
405 (signal 'wrong-type-argument
(list 'epg-context-p context
)))
406 (aset (cdr context
) 14 operation
))
408 (defun epg-make-signature (status &optional key-id
)
409 "Return a signature object."
410 (cons 'epg-signature
(vector status key-id nil nil nil nil nil nil nil nil
413 (defun epg-signature-status (signature)
414 "Return the status code of SIGNATURE."
415 (unless (eq (car-safe signature
) 'epg-signature
)
416 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
417 (aref (cdr signature
) 0))
419 (defun epg-signature-key-id (signature)
420 "Return the key-id of SIGNATURE."
421 (unless (eq (car-safe signature
) 'epg-signature
)
422 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
423 (aref (cdr signature
) 1))
425 (defun epg-signature-validity (signature)
426 "Return the validity of SIGNATURE."
427 (unless (eq (car-safe signature
) 'epg-signature
)
428 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
429 (aref (cdr signature
) 2))
431 (defun epg-signature-fingerprint (signature)
432 "Return the fingerprint of SIGNATURE."
433 (unless (eq (car-safe signature
) 'epg-signature
)
434 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
435 (aref (cdr signature
) 3))
437 (defun epg-signature-creation-time (signature)
438 "Return the creation time of SIGNATURE."
439 (unless (eq (car-safe signature
) 'epg-signature
)
440 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
441 (aref (cdr signature
) 4))
443 (defun epg-signature-expiration-time (signature)
444 "Return the expiration time of SIGNATURE."
445 (unless (eq (car-safe signature
) 'epg-signature
)
446 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
447 (aref (cdr signature
) 5))
449 (defun epg-signature-pubkey-algorithm (signature)
450 "Return the public key algorithm of SIGNATURE."
451 (unless (eq (car-safe signature
) 'epg-signature
)
452 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
453 (aref (cdr signature
) 6))
455 (defun epg-signature-digest-algorithm (signature)
456 "Return the digest algorithm of SIGNATURE."
457 (unless (eq (car-safe signature
) 'epg-signature
)
458 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
459 (aref (cdr signature
) 7))
461 (defun epg-signature-class (signature)
462 "Return the class of SIGNATURE."
463 (unless (eq (car-safe signature
) 'epg-signature
)
464 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
465 (aref (cdr signature
) 8))
467 (defun epg-signature-version (signature)
468 "Return the version of SIGNATURE."
469 (unless (eq (car-safe signature
) 'epg-signature
)
470 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
471 (aref (cdr signature
) 9))
473 (defun epg-sig-notations (signature)
474 "Return the list of notations of SIGNATURE."
475 (unless (eq (car-safe signature
) 'epg-signature
)
476 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
477 (aref (cdr signature
) 10))
479 (defun epg-signature-set-status (signature status
)
480 "Set the status code of SIGNATURE."
481 (unless (eq (car-safe signature
) 'epg-signature
)
482 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
483 (aset (cdr signature
) 0 status
))
485 (defun epg-signature-set-key-id (signature key-id
)
486 "Set the key-id of SIGNATURE."
487 (unless (eq (car-safe signature
) 'epg-signature
)
488 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
489 (aset (cdr signature
) 1 key-id
))
491 (defun epg-signature-set-validity (signature validity
)
492 "Set the validity of SIGNATURE."
493 (unless (eq (car-safe signature
) 'epg-signature
)
494 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
495 (aset (cdr signature
) 2 validity
))
497 (defun epg-signature-set-fingerprint (signature fingerprint
)
498 "Set the fingerprint of SIGNATURE."
499 (unless (eq (car-safe signature
) 'epg-signature
)
500 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
501 (aset (cdr signature
) 3 fingerprint
))
503 (defun epg-signature-set-creation-time (signature creation-time
)
504 "Set the creation time of SIGNATURE."
505 (unless (eq (car-safe signature
) 'epg-signature
)
506 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
507 (aset (cdr signature
) 4 creation-time
))
509 (defun epg-signature-set-expiration-time (signature expiration-time
)
510 "Set the expiration time of SIGNATURE."
511 (unless (eq (car-safe signature
) 'epg-signature
)
512 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
513 (aset (cdr signature
) 5 expiration-time
))
515 (defun epg-signature-set-pubkey-algorithm (signature pubkey-algorithm
)
516 "Set the public key algorithm of SIGNATURE."
517 (unless (eq (car-safe signature
) 'epg-signature
)
518 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
519 (aset (cdr signature
) 6 pubkey-algorithm
))
521 (defun epg-signature-set-digest-algorithm (signature digest-algorithm
)
522 "Set the digest algorithm of SIGNATURE."
523 (unless (eq (car-safe signature
) 'epg-signature
)
524 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
525 (aset (cdr signature
) 7 digest-algorithm
))
527 (defun epg-signature-set-class (signature class
)
528 "Set the class of SIGNATURE."
529 (unless (eq (car-safe signature
) 'epg-signature
)
530 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
531 (aset (cdr signature
) 8 class
))
533 (defun epg-signature-set-version (signature version
)
534 "Set the version of SIGNATURE."
535 (unless (eq (car-safe signature
) 'epg-signature
)
536 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
537 (aset (cdr signature
) 9 version
))
539 (defun epg-signature-set-notations (signature notations
)
540 "Set the list of notations of SIGNATURE."
541 (unless (eq (car-safe signature
) 'epg-signature
)
542 (signal 'wrong-type-argument
(list 'epg-signature-p signature
)))
543 (aset (cdr signature
) 10 notations
))
545 (defun epg-make-new-signature (type pubkey-algorithm digest-algorithm
546 class creation-time fingerprint
)
547 "Return a new signature object."
548 (cons 'epg-new-signature
(vector type pubkey-algorithm digest-algorithm
549 class creation-time fingerprint
)))
551 (defun epg-new-signature-type (new-signature)
552 "Return the type of NEW-SIGNATURE."
553 (unless (eq (car-safe new-signature
) 'epg-new-signature
)
554 (signal 'wrong-type-argument
(list 'epg-new-signature-p new-signature
)))
555 (aref (cdr new-signature
) 0))
557 (defun epg-new-signature-pubkey-algorithm (new-signature)
558 "Return the public key algorithm of NEW-SIGNATURE."
559 (unless (eq (car-safe new-signature
) 'epg-new-signature
)
560 (signal 'wrong-type-argument
(list 'epg-new-signature-p new-signature
)))
561 (aref (cdr new-signature
) 1))
563 (defun epg-new-signature-digest-algorithm (new-signature)
564 "Return the digest algorithm of NEW-SIGNATURE."
565 (unless (eq (car-safe new-signature
) 'epg-new-signature
)
566 (signal 'wrong-type-argument
(list 'epg-new-signature-p new-signature
)))
567 (aref (cdr new-signature
) 2))
569 (defun epg-new-signature-class (new-signature)
570 "Return the class of NEW-SIGNATURE."
571 (unless (eq (car-safe new-signature
) 'epg-new-signature
)
572 (signal 'wrong-type-argument
(list 'epg-new-signature-p new-signature
)))
573 (aref (cdr new-signature
) 3))
575 (defun epg-new-signature-creation-time (new-signature)
576 "Return the creation time of NEW-SIGNATURE."
577 (unless (eq (car-safe new-signature
) 'epg-new-signature
)
578 (signal 'wrong-type-argument
(list 'epg-new-signature-p new-signature
)))
579 (aref (cdr new-signature
) 4))
581 (defun epg-new-signature-fingerprint (new-signature)
582 "Return the fingerprint of NEW-SIGNATURE."
583 (unless (eq (car-safe new-signature
) 'epg-new-signature
)
584 (signal 'wrong-type-argument
(list 'epg-new-signature-p new-signature
)))
585 (aref (cdr new-signature
) 5))
587 (defun epg-make-key (owner-trust)
588 "Return a key object."
589 (cons 'epg-key
(vector owner-trust nil nil
)))
591 (defun epg-key-owner-trust (key)
592 "Return the owner trust of KEY."
593 (unless (eq (car-safe key
) 'epg-key
)
594 (signal 'wrong-type-argument
(list 'epg-key-p key
)))
597 (defun epg-key-sub-key-list (key)
598 "Return the sub key list of KEY."
599 (unless (eq (car-safe key
) 'epg-key
)
600 (signal 'wrong-type-argument
(list 'epg-key-p key
)))
603 (defun epg-key-user-id-list (key)
604 "Return the user ID list of KEY."
605 (unless (eq (car-safe key
) 'epg-key
)
606 (signal 'wrong-type-argument
(list 'epg-key-p key
)))
609 (defun epg-key-set-sub-key-list (key sub-key-list
)
610 "Set the sub key list of KEY."
611 (unless (eq (car-safe key
) 'epg-key
)
612 (signal 'wrong-type-argument
(list 'epg-key-p key
)))
613 (aset (cdr key
) 1 sub-key-list
))
615 (defun epg-key-set-user-id-list (key user-id-list
)
616 "Set the user ID list of KEY."
617 (unless (eq (car-safe key
) 'epg-key
)
618 (signal 'wrong-type-argument
(list 'epg-key-p key
)))
619 (aset (cdr key
) 2 user-id-list
))
621 (defun epg-make-sub-key (validity capability secret-p algorithm length id
622 creation-time expiration-time
)
623 "Return a sub key object."
625 (vector validity capability secret-p algorithm length id creation-time
626 expiration-time nil
)))
628 (defun epg-sub-key-validity (sub-key)
629 "Return the validity of SUB-KEY."
630 (unless (eq (car-safe sub-key
) 'epg-sub-key
)
631 (signal 'wrong-type-argument
(list 'epg-sub-key-p sub-key
)))
632 (aref (cdr sub-key
) 0))
634 (defun epg-sub-key-capability (sub-key)
635 "Return the capability of SUB-KEY."
636 (unless (eq (car-safe sub-key
) 'epg-sub-key
)
637 (signal 'wrong-type-argument
(list 'epg-sub-key-p sub-key
)))
638 (aref (cdr sub-key
) 1))
640 (defun epg-sub-key-secret-p (sub-key)
641 "Return non-nil if SUB-KEY is a secret key."
642 (unless (eq (car-safe sub-key
) 'epg-sub-key
)
643 (signal 'wrong-type-argument
(list 'epg-sub-key-p sub-key
)))
644 (aref (cdr sub-key
) 2))
646 (defun epg-sub-key-algorithm (sub-key)
647 "Return the algorithm of SUB-KEY."
648 (unless (eq (car-safe sub-key
) 'epg-sub-key
)
649 (signal 'wrong-type-argument
(list 'epg-sub-key-p sub-key
)))
650 (aref (cdr sub-key
) 3))
652 (defun epg-sub-key-length (sub-key)
653 "Return the length of SUB-KEY."
654 (unless (eq (car-safe sub-key
) 'epg-sub-key
)
655 (signal 'wrong-type-argument
(list 'epg-sub-key-p sub-key
)))
656 (aref (cdr sub-key
) 4))
658 (defun epg-sub-key-id (sub-key)
659 "Return the ID of SUB-KEY."
660 (unless (eq (car-safe sub-key
) 'epg-sub-key
)
661 (signal 'wrong-type-argument
(list 'epg-sub-key-p sub-key
)))
662 (aref (cdr sub-key
) 5))
664 (defun epg-sub-key-creation-time (sub-key)
665 "Return the creation time of SUB-KEY."
666 (unless (eq (car-safe sub-key
) 'epg-sub-key
)
667 (signal 'wrong-type-argument
(list 'epg-sub-key-p sub-key
)))
668 (aref (cdr sub-key
) 6))
670 (defun epg-sub-key-expiration-time (sub-key)
671 "Return the expiration time of SUB-KEY."
672 (unless (eq (car-safe sub-key
) 'epg-sub-key
)
673 (signal 'wrong-type-argument
(list 'epg-sub-key-p sub-key
)))
674 (aref (cdr sub-key
) 7))
676 (defun epg-sub-key-fingerprint (sub-key)
677 "Return the fingerprint of SUB-KEY."
678 (unless (eq (car-safe sub-key
) 'epg-sub-key
)
679 (signal 'wrong-type-argument
(list 'epg-sub-key-p sub-key
)))
680 (aref (cdr sub-key
) 8))
682 (defun epg-sub-key-set-fingerprint (sub-key fingerprint
)
683 "Set the fingerprint of SUB-KEY.
684 This function is for internal use only."
685 (unless (eq (car-safe sub-key
) 'epg-sub-key
)
686 (signal 'wrong-type-argument
(list 'epg-sub-key-p sub-key
)))
687 (aset (cdr sub-key
) 8 fingerprint
))
689 (defun epg-make-user-id (validity string
)
690 "Return a user ID object."
691 (cons 'epg-user-id
(vector validity string nil
)))
693 (defun epg-user-id-validity (user-id)
694 "Return the validity of USER-ID."
695 (unless (eq (car-safe user-id
) 'epg-user-id
)
696 (signal 'wrong-type-argument
(list 'epg-user-id-p user-id
)))
697 (aref (cdr user-id
) 0))
699 (defun epg-user-id-string (user-id)
700 "Return the name of USER-ID."
701 (unless (eq (car-safe user-id
) 'epg-user-id
)
702 (signal 'wrong-type-argument
(list 'epg-user-id-p user-id
)))
703 (aref (cdr user-id
) 1))
705 (defun epg-user-id-signature-list (user-id)
706 "Return the signature list of USER-ID."
707 (unless (eq (car-safe user-id
) 'epg-user-id
)
708 (signal 'wrong-type-argument
(list 'epg-user-id-p user-id
)))
709 (aref (cdr user-id
) 2))
711 (defun epg-user-id-set-signature-list (user-id signature-list
)
712 "Set the signature list of USER-ID."
713 (unless (eq (car-safe user-id
) 'epg-user-id
)
714 (signal 'wrong-type-argument
(list 'epg-user-id-p user-id
)))
715 (aset (cdr user-id
) 2 signature-list
))
717 (defun epg-make-key-signature (validity pubkey-algorithm key-id creation-time
718 expiration-time user-id class
720 "Return a key signature object."
721 (cons 'epg-key-signature
722 (vector validity pubkey-algorithm key-id creation-time expiration-time
723 user-id class exportable-p
)))
725 (defun epg-key-signature-validity (key-signature)
726 "Return the validity of KEY-SIGNATURE."
727 (unless (eq (car-safe key-signature
) 'epg-key-signature
)
728 (signal 'wrong-type-argument
(list 'epg-key-signature-p key-signature
)))
729 (aref (cdr key-signature
) 0))
731 (defun epg-key-signature-pubkey-algorithm (key-signature)
732 "Return the public key algorithm of KEY-SIGNATURE."
733 (unless (eq (car-safe key-signature
) 'epg-key-signature
)
734 (signal 'wrong-type-argument
(list 'epg-key-signature-p key-signature
)))
735 (aref (cdr key-signature
) 1))
737 (defun epg-key-signature-key-id (key-signature)
738 "Return the key-id of KEY-SIGNATURE."
739 (unless (eq (car-safe key-signature
) 'epg-key-signature
)
740 (signal 'wrong-type-argument
(list 'epg-key-signature-p key-signature
)))
741 (aref (cdr key-signature
) 2))
743 (defun epg-key-signature-creation-time (key-signature)
744 "Return the creation time of KEY-SIGNATURE."
745 (unless (eq (car-safe key-signature
) 'epg-key-signature
)
746 (signal 'wrong-type-argument
(list 'epg-key-signature-p key-signature
)))
747 (aref (cdr key-signature
) 3))
749 (defun epg-key-signature-expiration-time (key-signature)
750 "Return the expiration time of KEY-SIGNATURE."
751 (unless (eq (car-safe key-signature
) 'epg-key-signature
)
752 (signal 'wrong-type-argument
(list 'epg-key-signature-p key-signature
)))
753 (aref (cdr key-signature
) 4))
755 (defun epg-key-signature-user-id (key-signature)
756 "Return the user-id of KEY-SIGNATURE."
757 (unless (eq (car-safe key-signature
) 'epg-key-signature
)
758 (signal 'wrong-type-argument
(list 'epg-key-signature-p key-signature
)))
759 (aref (cdr key-signature
) 5))
761 (defun epg-key-signature-class (key-signature)
762 "Return the class of KEY-SIGNATURE."
763 (unless (eq (car-safe key-signature
) 'epg-key-signature
)
764 (signal 'wrong-type-argument
(list 'epg-key-signature-p key-signature
)))
765 (aref (cdr key-signature
) 6))
767 (defun epg-key-signature-exportable-p (key-signature)
768 "Return t if KEY-SIGNATURE is exportable."
769 (unless (eq (car-safe key-signature
) 'epg-key-signature
)
770 (signal 'wrong-type-argument
(list 'epg-key-signature-p key-signature
)))
771 (aref (cdr key-signature
) 7))
773 (defun epg-make-sig-notation (name value
&optional human-readable
775 "Return a notation object."
776 (cons 'epg-sig-notation
(vector name value human-readable critical
)))
778 (defun epg-sig-notation-name (sig-notation)
779 "Return the name of SIG-NOTATION."
780 (unless (eq (car-safe sig-notation
) 'epg-sig-notation
)
781 (signal 'wrong-type-argument
(list 'epg-sig-notation-p
783 (aref (cdr sig-notation
) 0))
785 (defun epg-sig-notation-value (sig-notation)
786 "Return the value of SIG-NOTATION."
787 (unless (eq (car-safe sig-notation
) 'epg-sig-notation
)
788 (signal 'wrong-type-argument
(list 'epg-sig-notation-p
790 (aref (cdr sig-notation
) 1))
792 (defun epg-sig-notation-human-readable (sig-notation)
793 "Return the human-readable of SIG-NOTATION."
794 (unless (eq (car-safe sig-notation
) 'epg-sig-notation
)
795 (signal 'wrong-type-argument
(list 'epg-sig-notation-p
797 (aref (cdr sig-notation
) 2))
799 (defun epg-sig-notation-critical (sig-notation)
800 "Return the critical of SIG-NOTATION."
801 (unless (eq (car-safe sig-notation
) 'epg-sig-notation
)
802 (signal 'wrong-type-argument
(list 'epg-sig-notation-p
804 (aref (cdr sig-notation
) 3))
806 (defun epg-sig-notation-set-value (sig-notation value
)
807 "Set the value of SIG-NOTATION."
808 (unless (eq (car-safe sig-notation
) 'epg-sig-notation
)
809 (signal 'wrong-type-argument
(list 'epg-sig-notation-p
811 (aset (cdr sig-notation
) 1 value
))
813 (defun epg-make-import-status (fingerprint &optional reason new user-id
814 signature sub-key secret
)
815 "Return an import status object."
816 (cons 'epg-import-status
(vector fingerprint reason new user-id signature
819 (defun epg-import-status-fingerprint (import-status)
820 "Return the fingerprint of the key that was considered."
821 (unless (eq (car-safe import-status
) 'epg-import-status
)
822 (signal 'wrong-type-argument
(list 'epg-import-status-p import-status
)))
823 (aref (cdr import-status
) 0))
825 (defun epg-import-status-reason (import-status)
826 "Return the reason code for import failure."
827 (unless (eq (car-safe import-status
) 'epg-import-status
)
828 (signal 'wrong-type-argument
(list 'epg-import-status-p import-status
)))
829 (aref (cdr import-status
) 1))
831 (defun epg-import-status-new (import-status)
832 "Return t if the imported key was new."
833 (unless (eq (car-safe import-status
) 'epg-import-status
)
834 (signal 'wrong-type-argument
(list 'epg-import-status-p import-status
)))
835 (aref (cdr import-status
) 2))
837 (defun epg-import-status-user-id (import-status)
838 "Return t if the imported key contained new user IDs."
839 (unless (eq (car-safe import-status
) 'epg-import-status
)
840 (signal 'wrong-type-argument
(list 'epg-import-status-p import-status
)))
841 (aref (cdr import-status
) 3))
843 (defun epg-import-status-signature (import-status)
844 "Return t if the imported key contained new signatures."
845 (unless (eq (car-safe import-status
) 'epg-import-status
)
846 (signal 'wrong-type-argument
(list 'epg-import-status-p import-status
)))
847 (aref (cdr import-status
) 4))
849 (defun epg-import-status-sub-key (import-status)
850 "Return t if the imported key contained new sub keys."
851 (unless (eq (car-safe import-status
) 'epg-import-status
)
852 (signal 'wrong-type-argument
(list 'epg-import-status-p import-status
)))
853 (aref (cdr import-status
) 5))
855 (defun epg-import-status-secret (import-status)
856 "Return t if the imported key contained a secret key."
857 (unless (eq (car-safe import-status
) 'epg-import-status
)
858 (signal 'wrong-type-argument
(list 'epg-import-status-p import-status
)))
859 (aref (cdr import-status
) 6))
861 (defun epg-make-import-result (considered no-user-id imported imported-rsa
862 unchanged new-user-ids new-sub-keys
863 new-signatures new-revocations
864 secret-read secret-imported
865 secret-unchanged not-imported
867 "Return an import result object."
868 (cons 'epg-import-result
(vector considered no-user-id imported imported-rsa
869 unchanged new-user-ids new-sub-keys
870 new-signatures new-revocations secret-read
871 secret-imported secret-unchanged
872 not-imported imports
)))
874 (defun epg-import-result-considered (import-result)
875 "Return the total number of considered keys."
876 (unless (eq (car-safe import-result
) 'epg-import-result
)
877 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
878 (aref (cdr import-result
) 0))
880 (defun epg-import-result-no-user-id (import-result)
881 "Return the number of keys without user ID."
882 (unless (eq (car-safe import-result
) 'epg-import-result
)
883 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
884 (aref (cdr import-result
) 1))
886 (defun epg-import-result-imported (import-result)
887 "Return the number of imported keys."
888 (unless (eq (car-safe import-result
) 'epg-import-result
)
889 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
890 (aref (cdr import-result
) 2))
892 (defun epg-import-result-imported-rsa (import-result)
893 "Return the number of imported RSA keys."
894 (unless (eq (car-safe import-result
) 'epg-import-result
)
895 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
896 (aref (cdr import-result
) 3))
898 (defun epg-import-result-unchanged (import-result)
899 "Return the number of unchanged keys."
900 (unless (eq (car-safe import-result
) 'epg-import-result
)
901 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
902 (aref (cdr import-result
) 4))
904 (defun epg-import-result-new-user-ids (import-result)
905 "Return the number of new user IDs."
906 (unless (eq (car-safe import-result
) 'epg-import-result
)
907 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
908 (aref (cdr import-result
) 5))
910 (defun epg-import-result-new-sub-keys (import-result)
911 "Return the number of new sub keys."
912 (unless (eq (car-safe import-result
) 'epg-import-result
)
913 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
914 (aref (cdr import-result
) 6))
916 (defun epg-import-result-new-signatures (import-result)
917 "Return the number of new signatures."
918 (unless (eq (car-safe import-result
) 'epg-import-result
)
919 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
920 (aref (cdr import-result
) 7))
922 (defun epg-import-result-new-revocations (import-result)
923 "Return the number of new revocations."
924 (unless (eq (car-safe import-result
) 'epg-import-result
)
925 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
926 (aref (cdr import-result
) 8))
928 (defun epg-import-result-secret-read (import-result)
929 "Return the total number of secret keys read."
930 (unless (eq (car-safe import-result
) 'epg-import-result
)
931 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
932 (aref (cdr import-result
) 9))
934 (defun epg-import-result-secret-imported (import-result)
935 "Return the number of imported secret keys."
936 (unless (eq (car-safe import-result
) 'epg-import-result
)
937 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
938 (aref (cdr import-result
) 10))
940 (defun epg-import-result-secret-unchanged (import-result)
941 "Return the number of unchanged secret keys."
942 (unless (eq (car-safe import-result
) 'epg-import-result
)
943 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
944 (aref (cdr import-result
) 11))
946 (defun epg-import-result-not-imported (import-result)
947 "Return the number of keys not imported."
948 (unless (eq (car-safe import-result
) 'epg-import-result
)
949 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
950 (aref (cdr import-result
) 12))
952 (defun epg-import-result-imports (import-result)
953 "Return the list of `epg-import-status' objects."
954 (unless (eq (car-safe import-result
) 'epg-import-result
)
955 (signal 'wrong-type-argument
(list 'epg-import-result-p import-result
)))
956 (aref (cdr import-result
) 13))
958 (defun epg-context-result-for (context name
)
959 "Return the result of CONTEXT associated with NAME."
960 (cdr (assq name
(epg-context-result context
))))
962 (defun epg-context-set-result-for (context name value
)
963 "Set the result of CONTEXT associated with NAME to VALUE."
964 (let* ((result (epg-context-result context
))
965 (entry (assq name result
)))
968 (epg-context-set-result context
(cons (cons name value
) result
)))))
970 (defun epg-signature-to-string (signature)
971 "Convert SIGNATURE to a human readable string."
972 (let* ((user-id (cdr (assoc (epg-signature-key-id signature
)
974 (pubkey-algorithm (epg-signature-pubkey-algorithm signature
))
975 (key-id (epg-signature-key-id signature
)))
977 (cond ((eq (epg-signature-status signature
) 'good
)
978 "Good signature from ")
979 ((eq (epg-signature-status signature
) 'bad
)
980 "Bad signature from ")
981 ((eq (epg-signature-status signature
) 'expired
)
982 "Expired signature from ")
983 ((eq (epg-signature-status signature
) 'expired-key
)
984 "Signature made by expired key ")
985 ((eq (epg-signature-status signature
) 'revoked-key
)
986 "Signature made by revoked key ")
987 ((eq (epg-signature-status signature
) 'no-pubkey
)
988 "No public key for "))
992 (if (stringp user-id
)
994 (epg-decode-dn user-id
)))
996 (if (epg-signature-validity signature
)
997 (format " (trust %s)" (epg-signature-validity signature
))
999 (if (epg-signature-creation-time signature
)
1000 (format-time-string " created at %Y-%m-%dT%T%z"
1001 (epg-signature-creation-time signature
))
1003 (if pubkey-algorithm
1005 (or (cdr (assq pubkey-algorithm epg-pubkey-algorithm-alist
))
1006 (format "(unknown algorithm %d)" pubkey-algorithm
)))
1009 (defun epg-verify-result-to-string (verify-result)
1010 "Convert VERIFY-RESULT to a human readable string."
1011 (mapconcat #'epg-signature-to-string verify-result
"\n"))
1013 (defun epg-new-signature-to-string (new-signature)
1014 "Convert NEW-SIGNATURE to a human readable string."
1016 (cond ((eq (epg-new-signature-type new-signature
) 'detached
)
1017 "Detached signature ")
1018 ((eq (epg-new-signature-type new-signature
) 'clear
)
1019 "Cleartext signature ")
1022 (cdr (assq (epg-new-signature-pubkey-algorithm new-signature
)
1023 epg-pubkey-algorithm-alist
))
1025 (cdr (assq (epg-new-signature-digest-algorithm new-signature
)
1026 epg-digest-algorithm-alist
))
1028 (format "%02X " (epg-new-signature-class new-signature
))
1029 (epg-new-signature-fingerprint new-signature
)))
1031 (defun epg-import-result-to-string (import-result)
1032 "Convert IMPORT-RESULT to a human readable string."
1033 (concat (format "Total number processed: %d\n"
1034 (epg-import-result-considered import-result
))
1035 (if (> (epg-import-result-not-imported import-result
) 0)
1036 (format " skipped new keys: %d\n"
1037 (epg-import-result-not-imported import-result
)))
1038 (if (> (epg-import-result-no-user-id import-result
) 0)
1039 (format " w/o user IDs: %d\n"
1040 (epg-import-result-no-user-id import-result
)))
1041 (if (> (epg-import-result-imported import-result
) 0)
1042 (concat (format " imported: %d"
1043 (epg-import-result-imported import-result
))
1044 (if (> (epg-import-result-imported-rsa import-result
) 0)
1045 (format " (RSA: %d)"
1046 (epg-import-result-imported-rsa
1049 (if (> (epg-import-result-unchanged import-result
) 0)
1050 (format " unchanged: %d\n"
1051 (epg-import-result-unchanged import-result
)))
1052 (if (> (epg-import-result-new-user-ids import-result
) 0)
1053 (format " new user IDs: %d\n"
1054 (epg-import-result-new-user-ids import-result
)))
1055 (if (> (epg-import-result-new-sub-keys import-result
) 0)
1056 (format " new subkeys: %d\n"
1057 (epg-import-result-new-sub-keys import-result
)))
1058 (if (> (epg-import-result-new-signatures import-result
) 0)
1059 (format " new signatures: %d\n"
1060 (epg-import-result-new-signatures import-result
)))
1061 (if (> (epg-import-result-new-revocations import-result
) 0)
1062 (format " new key revocations: %d\n"
1063 (epg-import-result-new-revocations import-result
)))
1064 (if (> (epg-import-result-secret-read import-result
) 0)
1065 (format " secret keys read: %d\n"
1066 (epg-import-result-secret-read import-result
)))
1067 (if (> (epg-import-result-secret-imported import-result
) 0)
1068 (format " secret keys imported: %d\n"
1069 (epg-import-result-secret-imported import-result
)))
1070 (if (> (epg-import-result-secret-unchanged import-result
) 0)
1071 (format " secret keys unchanged: %d\n"
1072 (epg-import-result-secret-unchanged import-result
)))))
1074 (defun epg-error-to-string (error)
1076 ((eq (car error
) 'exit
)
1078 ((eq (car error
) 'quit
)
1080 ((eq (car error
) 'no-data
)
1081 (let ((entry (assq (cdr error
) epg-no-data-reason-alist
)))
1083 (format "No data (%s)" (downcase (cdr entry
)))
1085 ((eq (car error
) 'unexpected
)
1086 (let ((entry (assq (cdr error
) epg-unexpected-reason-alist
)))
1088 (format "Unexpected (%s)" (downcase (cdr entry
)))
1090 ((eq (car error
) 'bad-armor
)
1092 ((memq (car error
) '(invalid-recipient invalid-signer
))
1094 (if (eq (car error
) 'invalid-recipient
)
1095 "Unusable public key"
1096 "Unusable secret key")
1097 (let ((entry (assq 'requested
(cdr error
))))
1099 (format ": %s" (cdr entry
))
1101 (let ((entry (assq 'reason
(cdr error
))))
1103 (> (cdr entry
) 0) ;no specific reason given
1104 (setq entry
(assq (cdr entry
)
1105 epg-invalid-recipients-reason-alist
)))
1106 (format " (%s)" (downcase (cdr entry
)))
1108 ((eq (car error
) 'no-pubkey
)
1109 (format "No public key: %s" (cdr error
)))
1110 ((eq (car error
) 'no-seckey
)
1111 (format "No secret key: %s" (cdr error
)))
1112 ((eq (car error
) 'no-recipients
)
1114 ((eq (car error
) 'no-signers
)
1116 ((eq (car error
) 'delete-problem
)
1117 (let ((entry (assq (cdr error
) epg-delete-problem-reason-alist
)))
1119 (format "Delete problem (%s)" (downcase (cdr entry
)))
1121 ((eq (car error
) 'key-not-created
)
1122 "Key not created")))
1124 (defun epg-errors-to-string (errors)
1125 (mapconcat #'epg-error-to-string errors
"; "))
1127 (defun epg--start (context args
)
1128 "Start `epg-gpg-program' in a subprocess with given ARGS."
1129 (if (and (epg-context-process context
)
1130 (eq (process-status (epg-context-process context
)) 'run
))
1131 (error "%s is already running in this context"
1132 (if (eq (epg-context-protocol context
) 'CMS
)
1135 (let* ((agent-info (getenv "GPG_AGENT_INFO"))
1136 (args (append (list "--no-tty"
1139 (if (and (not (eq (epg-context-protocol context
) 'CMS
))
1140 (string-match ":" (or agent-info
"")))
1142 (if (and (not (eq (epg-context-protocol context
) 'CMS
))
1143 (epg-context-progress-callback context
))
1144 '("--enable-progress-filter"))
1145 (if epg-gpg-home-directory
1146 (list "--homedir" epg-gpg-home-directory
))
1147 (unless (eq (epg-context-protocol context
) 'CMS
)
1148 '("--command-fd" "0"))
1149 (if (epg-context-armor context
) '("--armor"))
1150 (if (epg-context-textmode context
) '("--textmode"))
1151 (if (epg-context-output-file context
)
1152 (list "--output" (epg-context-output-file context
)))
1154 (coding-system-for-write 'binary
)
1155 (coding-system-for-read 'binary
)
1156 process-connection-type
1157 (orig-mode (default-file-modes))
1158 (buffer (generate-new-buffer " *epg*"))
1162 (unless epg-debug-buffer
1163 (setq epg-debug-buffer
(generate-new-buffer " *epg-debug*")))
1164 (set-buffer epg-debug-buffer
)
1165 (goto-char (point-max))
1166 (insert (if agent-info
1167 (format "GPG_AGENT_INFO=%s\n" agent-info
)
1168 "GPG_AGENT_INFO is not set\n")
1170 (if (eq (epg-context-protocol context
) 'CMS
)
1173 (mapconcat #'identity args
" ")))))
1174 (with-current-buffer buffer
1175 (if (fboundp 'set-buffer-multibyte
)
1176 (set-buffer-multibyte nil
))
1177 (make-local-variable 'epg-last-status
)
1178 (setq epg-last-status nil
)
1179 (make-local-variable 'epg-read-point
)
1180 (setq epg-read-point
(point-min))
1181 (make-local-variable 'epg-process-filter-running
)
1182 (setq epg-process-filter-running nil
)
1183 (make-local-variable 'epg-pending-status-list
)
1184 (setq epg-pending-status-list nil
)
1185 (make-local-variable 'epg-key-id
)
1186 (setq epg-key-id nil
)
1187 (make-local-variable 'epg-context
)
1188 (setq epg-context context
))
1191 (set-default-file-modes 448)
1193 (apply #'start-process
"epg" buffer
1194 (if (eq (epg-context-protocol context
) 'CMS
)
1198 (set-default-file-modes orig-mode
))
1199 (set-process-filter process
#'epg--process-filter
)
1200 (epg-context-set-process context process
)))
1202 (defun epg--process-filter (process input
)
1205 (unless epg-debug-buffer
1206 (setq epg-debug-buffer
(generate-new-buffer " *epg-debug*")))
1207 (set-buffer epg-debug-buffer
)
1208 (goto-char (point-max))
1210 (if (buffer-live-p (process-buffer process
))
1211 (with-current-buffer (process-buffer process
)
1212 (goto-char (point-max))
1214 (unless epg-process-filter-running
1217 (setq epg-process-filter-running t
)
1218 (goto-char epg-read-point
)
1220 (while (looking-at ".*\n") ;the input line finished
1221 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
1222 (let* ((status (match-string 1))
1223 (string (match-string 2))
1224 (symbol (intern-soft (concat "epg--status-"
1226 (if (member status epg-pending-status-list
)
1227 (setq epg-pending-status-list nil
))
1230 (funcall symbol epg-context string
))
1231 (setq epg-last-status
(cons status string
))))
1233 (setq epg-read-point
(point))))
1234 (setq epg-process-filter-running nil
))))))
1236 (defun epg-read-output (context)
1237 "Read the output file CONTEXT and return the content as a string."
1239 (if (fboundp 'set-buffer-multibyte
)
1240 (set-buffer-multibyte nil
))
1241 (if (file-exists-p (epg-context-output-file context
))
1242 (let ((coding-system-for-read 'binary
))
1243 (insert-file-contents (epg-context-output-file context
))
1246 (defun epg-wait-for-status (context status-list
)
1247 "Wait until one of elements in STATUS-LIST arrives."
1248 (with-current-buffer (process-buffer (epg-context-process context
))
1249 (setq epg-pending-status-list status-list
)
1250 (while (and (eq (process-status (epg-context-process context
)) 'run
)
1251 epg-pending-status-list
)
1252 (accept-process-output (epg-context-process context
) 1))
1253 (if epg-pending-status-list
1254 (epg-context-set-result-for
1257 (epg-context-result-for context
'error
))))))
1259 (defun epg-wait-for-completion (context)
1260 "Wait until the `epg-gpg-program' process completes."
1261 (while (eq (process-status (epg-context-process context
)) 'run
)
1262 (accept-process-output (epg-context-process context
) 1))
1263 ;; This line is needed to run the process-filter right now.
1265 (epg-context-set-result-for
1267 (nreverse (epg-context-result-for context
'error
))))
1269 (defun epg-reset (context)
1270 "Reset the CONTEXT."
1271 (if (and (epg-context-process context
)
1272 (buffer-live-p (process-buffer (epg-context-process context
))))
1273 (kill-buffer (process-buffer (epg-context-process context
))))
1274 (epg-context-set-process context nil
))
1276 (defun epg-delete-output-file (context)
1277 "Delete the output file of CONTEXT."
1278 (if (and (epg-context-output-file context
)
1279 (file-exists-p (epg-context-output-file context
)))
1280 (delete-file (epg-context-output-file context
))))
1283 (if (fboundp 'decode-coding-string
)
1284 (defalias 'epg--decode-coding-string
'decode-coding-string
)
1285 (defalias 'epg--decode-coding-string
'identity
)))
1287 (defun epg--status-USERID_HINT (_context string
)
1288 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string
)
1289 (let* ((key-id (match-string 1 string
))
1290 (user-id (match-string 2 string
))
1291 (entry (assoc key-id epg-user-id-alist
)))
1293 (setq user-id
(epg--decode-coding-string
1294 (epg--decode-percent-escape user-id
)
1298 (setcdr entry user-id
)
1299 (setq epg-user-id-alist
(cons (cons key-id user-id
)
1300 epg-user-id-alist
))))))
1302 (defun epg--status-NEED_PASSPHRASE (_context string
)
1303 (if (string-match "\\`\\([^ ]+\\)" string
)
1304 (setq epg-key-id
(match-string 1 string
))))
1306 (defun epg--status-NEED_PASSPHRASE_SYM (_context _string
)
1307 (setq epg-key-id
'SYM
))
1309 (defun epg--status-NEED_PASSPHRASE_PIN (_context _string
)
1310 (setq epg-key-id
'PIN
))
1313 (if (fboundp 'clear-string
)
1314 (defalias 'epg--clear-string
'clear-string
)
1315 (defun epg--clear-string (string)
1316 (fillarray string
0))))
1319 (if (fboundp 'encode-coding-string
)
1320 (defalias 'epg--encode-coding-string
'encode-coding-string
)
1321 (defalias 'epg--encode-coding-string
'identity
)))
1323 (defun epg--status-GET_HIDDEN (context string
)
1324 (when (and epg-key-id
1325 (string-match "\\`passphrase\\." string
))
1326 (unless (epg-context-passphrase-callback context
)
1327 (error "passphrase-callback not set"))
1330 passphrase-with-new-line
1331 encoded-passphrase-with-new-line
)
1337 (car (epg-context-passphrase-callback context
))
1340 (cdr (epg-context-passphrase-callback context
))))
1342 (setq passphrase-with-new-line
(concat passphrase
"\n"))
1343 (epg--clear-string passphrase
)
1344 (setq passphrase nil
)
1345 (if epg-passphrase-coding-system
1347 (setq encoded-passphrase-with-new-line
1348 (epg--encode-coding-string
1349 passphrase-with-new-line
1350 (coding-system-change-eol-conversion
1351 epg-passphrase-coding-system
'unix
)))
1352 (epg--clear-string passphrase-with-new-line
)
1353 (setq passphrase-with-new-line nil
))
1354 (setq encoded-passphrase-with-new-line
1355 passphrase-with-new-line
1356 passphrase-with-new-line nil
))
1357 (process-send-string (epg-context-process context
)
1358 encoded-passphrase-with-new-line
)))
1360 (epg-context-set-result-for
1363 (epg-context-result-for context
'error
)))
1364 (delete-process (epg-context-process context
))))
1366 (epg--clear-string passphrase
))
1367 (if passphrase-with-new-line
1368 (epg--clear-string passphrase-with-new-line
))
1369 (if encoded-passphrase-with-new-line
1370 (epg--clear-string encoded-passphrase-with-new-line
))))))
1372 (defun epg--prompt-GET_BOOL (_context string
)
1373 (let ((entry (assoc string epg-prompt-alist
)))
1374 (y-or-n-p (if entry
(cdr entry
) (concat string
"? ")))))
1376 (defun epg--prompt-GET_BOOL-untrusted_key.override
(_context _string
)
1377 (y-or-n-p (if (and (equal (car epg-last-status
) "USERID_HINT")
1378 (string-match "\\`\\([^ ]+\\) \\(.*\\)"
1379 (cdr epg-last-status
)))
1380 (let* ((key-id (match-string 1 (cdr epg-last-status
)))
1381 (user-id (match-string 2 (cdr epg-last-status
)))
1382 (entry (assoc key-id epg-user-id-alist
)))
1384 (setq user-id
(cdr entry
)))
1385 (format "Untrusted key %s %s. Use anyway? " key-id user-id
))
1386 "Use untrusted key anyway? ")))
1388 (defun epg--status-GET_BOOL (context string
)
1391 (if (funcall (or (intern-soft (concat "epg--prompt-GET_BOOL-" string
))
1392 #'epg--prompt-GET_BOOL
)
1394 (process-send-string (epg-context-process context
) "y\n")
1395 (process-send-string (epg-context-process context
) "n\n"))
1397 (epg-context-set-result-for
1400 (epg-context-result-for context
'error
)))
1401 (delete-process (epg-context-process context
))))))
1403 (defun epg--status-GET_LINE (context string
)
1404 (let ((entry (assoc string epg-prompt-alist
))
1407 (process-send-string (epg-context-process context
)
1408 (concat (read-string
1411 (concat string
": ")))
1414 (epg-context-set-result-for
1417 (epg-context-result-for context
'error
)))
1418 (delete-process (epg-context-process context
))))))
1420 (defun epg--status-*SIG
(context status string
)
1421 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string
)
1422 (let* ((key-id (match-string 1 string
))
1423 (user-id (match-string 2 string
))
1424 (entry (assoc key-id epg-user-id-alist
)))
1425 (epg-context-set-result-for
1428 (cons (epg-make-signature status key-id
)
1429 (epg-context-result-for context
'verify
)))
1431 (if (eq (epg-context-protocol context
) 'CMS
)
1432 (setq user-id
(epg-dn-from-string user-id
))
1433 (setq user-id
(epg--decode-coding-string
1434 (epg--decode-percent-escape user-id
)
1438 (setcdr entry user-id
)
1439 (setq epg-user-id-alist
1440 (cons (cons key-id user-id
) epg-user-id-alist
))))
1441 (epg-context-set-result-for
1444 (cons (epg-make-signature status
)
1445 (epg-context-result-for context
'verify
)))))
1447 (defun epg--status-GOODSIG (context string
)
1448 (epg--status-*SIG context
'good string
))
1450 (defun epg--status-EXPSIG (context string
)
1451 (epg--status-*SIG context
'expired string
))
1453 (defun epg--status-EXPKEYSIG (context string
)
1454 (epg--status-*SIG context
'expired-key string
))
1456 (defun epg--status-REVKEYSIG (context string
)
1457 (epg--status-*SIG context
'revoked-key string
))
1459 (defun epg--status-BADSIG (context string
)
1460 (epg--status-*SIG context
'bad string
))
1462 (defun epg--status-NO_PUBKEY (context string
)
1463 (if (eq (epg-context-operation context
) 'verify
)
1464 (let ((signature (car (epg-context-result-for context
'verify
))))
1466 (eq (epg-signature-status signature
) 'error
)
1467 (equal (epg-signature-key-id signature
) string
))
1468 (epg-signature-set-status signature
'no-pubkey
)))
1469 (epg-context-set-result-for
1471 (cons (cons 'no-pubkey string
)
1472 (epg-context-result-for context
'error
)))))
1474 (defun epg--status-NO_SECKEY (context string
)
1475 (epg-context-set-result-for
1477 (cons (cons 'no-seckey string
)
1478 (epg-context-result-for context
'error
))))
1480 (defun epg--time-from-seconds (seconds)
1481 (let ((number-seconds (string-to-number (concat seconds
".0"))))
1482 (cons (floor (/ number-seconds
65536))
1483 (floor (mod number-seconds
65536)))))
1485 (defun epg--status-ERRSIG (context string
)
1486 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1487 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
1489 (let ((signature (epg-make-signature 'error
)))
1490 (epg-context-set-result-for
1494 (epg-context-result-for context
'verify
)))
1495 (epg-signature-set-key-id
1497 (match-string 1 string
))
1498 (epg-signature-set-pubkey-algorithm
1500 (string-to-number (match-string 2 string
)))
1501 (epg-signature-set-digest-algorithm
1503 (string-to-number (match-string 3 string
)))
1504 (epg-signature-set-class
1506 (string-to-number (match-string 4 string
) 16))
1507 (epg-signature-set-creation-time
1509 (epg--time-from-seconds (match-string 5 string
))))))
1511 (defun epg--status-VALIDSIG (context string
)
1512 (let ((signature (car (epg-context-result-for context
'verify
))))
1513 (when (and signature
1514 (eq (epg-signature-status signature
) 'good
)
1515 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
1516 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
1519 (epg-signature-set-fingerprint
1521 (match-string 1 string
))
1522 (epg-signature-set-creation-time
1524 (epg--time-from-seconds (match-string 2 string
)))
1525 (unless (equal (match-string 3 string
) "0")
1526 (epg-signature-set-expiration-time
1528 (epg--time-from-seconds (match-string 3 string
))))
1529 (epg-signature-set-version
1531 (string-to-number (match-string 4 string
)))
1532 (epg-signature-set-pubkey-algorithm
1534 (string-to-number (match-string 5 string
)))
1535 (epg-signature-set-digest-algorithm
1537 (string-to-number (match-string 6 string
)))
1538 (epg-signature-set-class
1540 (string-to-number (match-string 7 string
) 16)))))
1542 (defun epg--status-TRUST_UNDEFINED (context _string
)
1543 (let ((signature (car (epg-context-result-for context
'verify
))))
1545 (eq (epg-signature-status signature
) 'good
))
1546 (epg-signature-set-validity signature
'undefined
))))
1548 (defun epg--status-TRUST_NEVER (context _string
)
1549 (let ((signature (car (epg-context-result-for context
'verify
))))
1551 (eq (epg-signature-status signature
) 'good
))
1552 (epg-signature-set-validity signature
'never
))))
1554 (defun epg--status-TRUST_MARGINAL (context _string
)
1555 (let ((signature (car (epg-context-result-for context
'verify
))))
1557 (eq (epg-signature-status signature
) 'marginal
))
1558 (epg-signature-set-validity signature
'marginal
))))
1560 (defun epg--status-TRUST_FULLY (context _string
)
1561 (let ((signature (car (epg-context-result-for context
'verify
))))
1563 (eq (epg-signature-status signature
) 'good
))
1564 (epg-signature-set-validity signature
'full
))))
1566 (defun epg--status-TRUST_ULTIMATE (context _string
)
1567 (let ((signature (car (epg-context-result-for context
'verify
))))
1569 (eq (epg-signature-status signature
) 'good
))
1570 (epg-signature-set-validity signature
'ultimate
))))
1572 (defun epg--status-NOTATION_NAME (context string
)
1573 (let ((signature (car (epg-context-result-for context
'verify
))))
1575 (epg-signature-set-notations
1577 (cons (epg-make-sig-notation string nil t nil
)
1578 (epg-sig-notations signature
))))))
1580 (defun epg--status-NOTATION_DATA (context string
)
1581 (let ((signature (car (epg-context-result-for context
'verify
)))
1584 (setq notation
(car (epg-sig-notations signature
))))
1585 (epg-sig-notation-set-value notation string
))))
1587 (defun epg--status-POLICY_URL (context string
)
1588 (let ((signature (car (epg-context-result-for context
'verify
))))
1590 (epg-signature-set-notations
1592 (cons (epg-make-sig-notation nil string t nil
)
1593 (epg-sig-notations signature
))))))
1595 (defun epg--status-PROGRESS (context string
)
1596 (if (and (epg-context-progress-callback context
)
1597 (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1599 (funcall (car (epg-context-progress-callback context
))
1601 (match-string 1 string
)
1602 (match-string 2 string
)
1603 (string-to-number (match-string 3 string
))
1604 (string-to-number (match-string 4 string
))
1605 (cdr (epg-context-progress-callback context
)))))
1607 (defun epg--status-ENC_TO (context string
)
1608 (if (string-match "\\`\\([0-9A-Za-z]+\\) \\([0-9]+\\) \\([0-9]+\\)" string
)
1609 (epg-context-set-result-for
1610 context
'encrypted-to
1611 (cons (list (match-string 1 string
)
1612 (string-to-number (match-string 2 string
))
1613 (string-to-number (match-string 3 string
)))
1614 (epg-context-result-for context
'encrypted-to
)))))
1616 (defun epg--status-DECRYPTION_FAILED (context _string
)
1617 (epg-context-set-result-for context
'decryption-failed t
))
1619 (defun epg--status-DECRYPTION_OKAY (context _string
)
1620 (epg-context-set-result-for context
'decryption-okay t
))
1622 (defun epg--status-NODATA (context string
)
1623 (epg-context-set-result-for
1625 (cons (cons 'no-data
(string-to-number string
))
1626 (epg-context-result-for context
'error
))))
1628 (defun epg--status-UNEXPECTED (context string
)
1629 (epg-context-set-result-for
1631 (cons (cons 'unexpected
(string-to-number string
))
1632 (epg-context-result-for context
'error
))))
1634 (defun epg--status-KEYEXPIRED (context string
)
1635 (epg-context-set-result-for
1637 (cons (list 'key-expired
(cons 'expiration-time
1638 (epg--time-from-seconds string
)))
1639 (epg-context-result-for context
'key
))))
1641 (defun epg--status-KEYREVOKED (context _string
)
1642 (epg-context-set-result-for
1644 (cons '(key-revoked)
1645 (epg-context-result-for context
'key
))))
1647 (defun epg--status-BADARMOR (context _string
)
1648 (epg-context-set-result-for
1651 (epg-context-result-for context
'error
))))
1653 (defun epg--status-INV_RECP (context string
)
1654 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string
)
1655 (epg-context-set-result-for
1657 (cons (list 'invalid-recipient
1659 (string-to-number (match-string 1 string
)))
1661 (match-string 2 string
)))
1662 (epg-context-result-for context
'error
)))))
1664 (defun epg--status-INV_SGNR (context string
)
1665 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string
)
1666 (epg-context-set-result-for
1668 (cons (list 'invalid-signer
1670 (string-to-number (match-string 1 string
)))
1672 (match-string 2 string
)))
1673 (epg-context-result-for context
'error
)))))
1675 (defun epg--status-NO_RECP (context _string
)
1676 (epg-context-set-result-for
1678 (cons '(no-recipients)
1679 (epg-context-result-for context
'error
))))
1681 (defun epg--status-NO_SGNR (context _string
)
1682 (epg-context-set-result-for
1685 (epg-context-result-for context
'error
))))
1687 (defun epg--status-DELETE_PROBLEM (context string
)
1688 (if (string-match "\\`\\([0-9]+\\)" string
)
1689 (epg-context-set-result-for
1691 (cons (cons 'delete-problem
1692 (string-to-number (match-string 1 string
)))
1693 (epg-context-result-for context
'error
)))))
1695 (defun epg--status-SIG_CREATED (context string
)
1696 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1697 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string
)
1698 (epg-context-set-result-for
1700 (cons (epg-make-new-signature
1701 (cdr (assq (aref (match-string 1 string
) 0)
1702 epg-new-signature-type-alist
))
1703 (string-to-number (match-string 2 string
))
1704 (string-to-number (match-string 3 string
))
1705 (string-to-number (match-string 4 string
) 16)
1706 (epg--time-from-seconds (match-string 5 string
))
1707 (substring string
(match-end 0)))
1708 (epg-context-result-for context
'sign
)))))
1710 (defun epg--status-KEY_CREATED (context string
)
1711 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string
)
1712 (epg-context-set-result-for
1713 context
'generate-key
1714 (cons (list (cons 'type
(string-to-char (match-string 1 string
)))
1715 (cons 'fingerprint
(match-string 2 string
)))
1716 (epg-context-result-for context
'generate-key
)))))
1718 (defun epg--status-KEY_NOT_CREATED (context _string
)
1719 (epg-context-set-result-for
1721 (cons '(key-not-created)
1722 (epg-context-result-for context
'error
))))
1724 (defun epg--status-IMPORTED (_context string
)
1725 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string
)
1726 (let* ((key-id (match-string 1 string
))
1727 (user-id (match-string 2 string
))
1728 (entry (assoc key-id epg-user-id-alist
)))
1730 (setq user-id
(epg--decode-coding-string
1731 (epg--decode-percent-escape user-id
)
1735 (setcdr entry user-id
)
1736 (setq epg-user-id-alist
(cons (cons key-id user-id
)
1737 epg-user-id-alist
))))))
1739 (defun epg--status-IMPORT_OK (context string
)
1740 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string
)
1741 (let ((reason (string-to-number (match-string 1 string
))))
1742 (epg-context-set-result-for
1743 context
'import-status
1744 (cons (epg-make-import-status (if (match-beginning 2)
1745 (match-string 3 string
))
1747 (/= (logand reason
1) 0)
1748 (/= (logand reason
2) 0)
1749 (/= (logand reason
4) 0)
1750 (/= (logand reason
8) 0)
1751 (/= (logand reason
16) 0))
1752 (epg-context-result-for context
'import-status
))))))
1754 (defun epg--status-IMPORT_PROBLEM (context string
)
1755 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string
)
1756 (epg-context-set-result-for
1757 context
'import-status
1758 (cons (epg-make-import-status
1759 (if (match-beginning 2)
1760 (match-string 3 string
))
1761 (string-to-number (match-string 1 string
)))
1762 (epg-context-result-for context
'import-status
)))))
1764 (defun epg--status-IMPORT_RES (context string
)
1765 (when (string-match "\\`\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1766 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1767 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" string
)
1768 (epg-context-set-result-for
1770 (epg-make-import-result (string-to-number (match-string 1 string
))
1771 (string-to-number (match-string 2 string
))
1772 (string-to-number (match-string 3 string
))
1773 (string-to-number (match-string 4 string
))
1774 (string-to-number (match-string 5 string
))
1775 (string-to-number (match-string 6 string
))
1776 (string-to-number (match-string 7 string
))
1777 (string-to-number (match-string 8 string
))
1778 (string-to-number (match-string 9 string
))
1779 (string-to-number (match-string 10 string
))
1780 (string-to-number (match-string 11 string
))
1781 (string-to-number (match-string 12 string
))
1782 (string-to-number (match-string 13 string
))
1783 (epg-context-result-for context
'import-status
)))
1784 (epg-context-set-result-for context
'import-status nil
)))
1786 (defun epg-passphrase-callback-function (context key-id _handback
)
1787 (declare (obsolete epa-passphrase-callback-function
"23.1"))
1788 (if (eq key-id
'SYM
)
1789 (read-passwd "Passphrase for symmetric encryption: "
1790 (eq (epg-context-operation context
) 'encrypt
))
1792 (if (eq key-id
'PIN
)
1793 "Passphrase for PIN: "
1794 (let ((entry (assoc key-id epg-user-id-alist
)))
1796 (format "Passphrase for %s %s: " key-id
(cdr entry
))
1797 (format "Passphrase for %s: " key-id
)))))))
1799 (defun epg--list-keys-1 (context name mode
)
1800 (let ((args (append (if epg-gpg-home-directory
1801 (list "--homedir" epg-gpg-home-directory
))
1802 '("--with-colons" "--no-greeting" "--batch"
1803 "--with-fingerprint" "--with-fingerprint")
1804 (unless (eq (epg-context-protocol context
) 'CMS
)
1805 '("--fixed-list-mode"))))
1806 (list-keys-option (if (memq mode
'(t secret
))
1807 "--list-secret-keys"
1808 (if (memq mode
'(nil public
))
1811 (coding-system-for-read 'binary
)
1812 keys string field index
)
1815 (unless (listp name
)
1816 (setq name
(list name
)))
1818 (setq args
(append args
(list list-keys-option
(car name
)))
1820 (setq args
(append args
(list list-keys-option
))))
1822 (apply #'call-process
1823 (if (eq (epg-context-protocol context
) 'CMS
)
1826 nil
(list t nil
) nil args
)
1827 (goto-char (point-min))
1828 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t
)
1829 (setq keys
(cons (make-vector 15 nil
) keys
)
1830 string
(match-string 0)
1834 (string-match "\\([^:]+\\)?:" string index
))
1835 (setq index
(match-end 0))
1836 (aset (car keys
) field
(match-string 1 string
))
1837 (setq field
(1+ field
))))
1840 (defun epg--make-sub-key-1 (line)
1843 (cdr (assq (string-to-char (aref line
1)) epg-key-validity-alist
)))
1845 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist
)))
1847 (member (aref line
0) '("sec" "ssb"))
1848 (string-to-number (aref line
3))
1849 (string-to-number (aref line
2))
1851 (epg--time-from-seconds (aref line
5))
1853 (epg--time-from-seconds (aref line
6)))))
1855 (defun epg-list-keys (context &optional name mode
)
1856 "Return a list of epg-key objects matched with NAME.
1857 If MODE is nil or 'public, only public keyring should be searched.
1858 If MODE is t or 'secret, only secret keyring should be searched.
1859 Otherwise, only public keyring should be searched and the key
1860 signatures should be included.
1861 NAME is either a string or a list of strings."
1862 (let ((lines (epg--list-keys-1 context name mode
))
1863 keys cert pointer pointer-1 index string
)
1866 ((member (aref (car lines
) 0) '("pub" "sec" "crt" "crs"))
1867 (setq cert
(member (aref (car lines
) 0) '("crt" "crs"))
1868 keys
(cons (epg-make-key
1869 (if (aref (car lines
) 8)
1870 (cdr (assq (string-to-char (aref (car lines
) 8))
1871 epg-key-validity-alist
))))
1873 (epg-key-set-sub-key-list
1875 (cons (epg--make-sub-key-1 (car lines
))
1876 (epg-key-sub-key-list (car keys
)))))
1877 ((member (aref (car lines
) 0) '("sub" "ssb"))
1878 (epg-key-set-sub-key-list
1880 (cons (epg--make-sub-key-1 (car lines
))
1881 (epg-key-sub-key-list (car keys
)))))
1882 ((equal (aref (car lines
) 0) "uid")
1883 ;; Decode the UID name as a backslash escaped UTF-8 string,
1884 ;; generated by GnuPG/GpgSM.
1885 (setq string
(copy-sequence (aref (car lines
) 9))
1887 (while (string-match "\"" string index
)
1888 (setq string
(replace-match "\\\"" t t string
)
1889 index
(1+ (match-end 0))))
1891 (setq string
(epg--decode-coding-string
1892 (car (read-from-string (concat "\"" string
"\"")))
1895 (setq string
(aref (car lines
) 9))))
1896 (epg-key-set-user-id-list
1898 (cons (epg-make-user-id
1899 (if (aref (car lines
) 1)
1900 (cdr (assq (string-to-char (aref (car lines
) 1))
1901 epg-key-validity-alist
)))
1904 (epg-dn-from-string string
)
1907 (epg-key-user-id-list (car keys
)))))
1908 ((equal (aref (car lines
) 0) "fpr")
1909 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys
)))
1910 (aref (car lines
) 9)))
1911 ((equal (aref (car lines
) 0) "sig")
1912 (epg-user-id-set-signature-list
1913 (car (epg-key-user-id-list (car keys
)))
1915 (epg-make-key-signature
1916 (if (aref (car lines
) 1)
1917 (cdr (assq (string-to-char (aref (car lines
) 1))
1918 epg-key-validity-alist
)))
1919 (string-to-number (aref (car lines
) 3))
1920 (aref (car lines
) 4)
1921 (epg--time-from-seconds (aref (car lines
) 5))
1922 (epg--time-from-seconds (aref (car lines
) 6))
1923 (aref (car lines
) 9)
1924 (string-to-number (aref (car lines
) 10) 16)
1925 (eq (aref (aref (car lines
) 10) 2) ?x
))
1926 (epg-user-id-signature-list
1927 (car (epg-key-user-id-list (car keys
))))))))
1928 (setq lines
(cdr lines
)))
1929 (setq keys
(nreverse keys
)
1932 (epg-key-set-sub-key-list
1934 (nreverse (epg-key-sub-key-list (car pointer
))))
1935 (setq pointer-1
(epg-key-set-user-id-list
1937 (nreverse (epg-key-user-id-list (car pointer
)))))
1939 (epg-user-id-set-signature-list
1941 (nreverse (epg-user-id-signature-list (car pointer-1
))))
1942 (setq pointer-1
(cdr pointer-1
)))
1943 (setq pointer
(cdr pointer
)))
1947 (if (fboundp 'make-temp-file
)
1948 (defalias 'epg--make-temp-file
'make-temp-file
)
1949 (defvar temporary-file-directory
)
1950 ;; stolen from poe.el.
1951 (defun epg--make-temp-file (prefix)
1952 "Create a temporary file.
1953 The returned file name (created by appending some random characters at the end
1954 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1955 is guaranteed to point to a newly created empty file.
1956 You can then use `write-region' to write new data into the file."
1957 (let ((orig-modes (default-file-modes))
1959 (setq prefix
(expand-file-name prefix
1960 (if (featurep 'xemacs
)
1962 temporary-file-directory
)))
1965 ;; First, create a temporary directory.
1966 (set-default-file-modes #o700
)
1967 (while (condition-case ()
1969 (setq tempdir
(make-temp-name
1971 (file-name-directory prefix
)
1973 ;; return nil or signal an error.
1974 (make-directory tempdir
))
1976 (file-already-exists t
)))
1977 ;; Second, create a temporary file in the tempdir.
1978 ;; There *is* a race condition between `make-temp-name'
1979 ;; and `write-region', but we don't care it since we are
1980 ;; in a private directory now.
1981 (setq tempfile
(make-temp-name (concat tempdir
"/EMU")))
1982 (write-region "" nil tempfile nil
'silent
)
1983 ;; Finally, make a hard-link from the tempfile.
1984 (while (condition-case ()
1986 (setq file
(make-temp-name prefix
))
1987 ;; return nil or signal an error.
1988 (add-name-to-file tempfile file
))
1990 (file-already-exists t
)))
1992 (set-default-file-modes orig-modes
)
1993 ;; Cleanup the tempfile.
1995 (file-exists-p tempfile
)
1996 (delete-file tempfile
))
1997 ;; Cleanup the tempdir.
1999 (file-directory-p tempdir
)
2000 (delete-directory tempdir
)))))))
2002 (defun epg--args-from-sig-notations (notations)
2006 (if (and (epg-sig-notation-name notation
)
2007 (not (epg-sig-notation-human-readable notation
)))
2008 (error "Unreadable"))
2009 (if (epg-sig-notation-name notation
)
2010 (list "--sig-notation"
2011 (if (epg-sig-notation-critical notation
)
2012 (concat "!" (epg-sig-notation-name notation
)
2013 "=" (epg-sig-notation-value notation
))
2014 (concat (epg-sig-notation-name notation
)
2015 "=" (epg-sig-notation-value notation
))))
2016 (list "--sig-policy-url"
2017 (if (epg-sig-notation-critical notation
)
2018 (concat "!" (epg-sig-notation-value notation
))
2019 (epg-sig-notation-value notation
)))))
2022 (defun epg-cancel (context)
2023 (if (buffer-live-p (process-buffer (epg-context-process context
)))
2024 (with-current-buffer (process-buffer (epg-context-process context
))
2025 (epg-context-set-result-for
2028 (epg-context-result-for epg-context
'error
)))))
2029 (if (eq (process-status (epg-context-process context
)) 'run
)
2030 (delete-process (epg-context-process context
))))
2032 (defun epg-start-decrypt (context cipher
)
2033 "Initiate a decrypt operation on CIPHER.
2034 CIPHER must be a file data object.
2036 If you use this function, you will need to wait for the completion of
2037 `epg-gpg-program' by using `epg-wait-for-completion' and call
2038 `epg-reset' to clear a temporary output file.
2039 If you are unsure, use synchronous version of this function
2040 `epg-decrypt-file' or `epg-decrypt-string' instead."
2041 (unless (epg-data-file cipher
)
2042 (error "Not a file"))
2043 (epg-context-set-operation context
'decrypt
)
2044 (epg-context-set-result context nil
)
2045 (epg--start context
(list "--decrypt" "--" (epg-data-file cipher
)))
2046 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2047 (unless (eq (epg-context-protocol context
) 'CMS
)
2048 (epg-wait-for-status context
'("BEGIN_DECRYPTION"))))
2050 (defun epg--check-error-for-decrypt (context)
2051 (let ((errors (epg-context-result-for context
'error
)))
2052 (if (epg-context-result-for context
'decryption-failed
)
2054 (list "Decryption failed" (epg-errors-to-string errors
))))
2055 (unless (epg-context-result-for context
'decryption-okay
)
2057 (list "Can't decrypt" (epg-errors-to-string errors
))))))
2059 (defun epg-decrypt-file (context cipher plain
)
2060 "Decrypt a file CIPHER and store the result to a file PLAIN.
2061 If PLAIN is nil, it returns the result as a string."
2065 (epg-context-set-output-file context plain
)
2066 (epg-context-set-output-file context
2067 (epg--make-temp-file "epg-output")))
2068 (epg-start-decrypt context
(epg-make-data-from-file cipher
))
2069 (epg-wait-for-completion context
)
2070 (epg--check-error-for-decrypt context
)
2072 (epg-read-output context
)))
2074 (epg-delete-output-file context
))
2075 (epg-reset context
)))
2077 (defun epg-decrypt-string (context cipher
)
2078 "Decrypt a string CIPHER and return the plain text."
2079 (let ((input-file (epg--make-temp-file "epg-input"))
2080 (coding-system-for-write 'binary
))
2083 (write-region cipher nil input-file nil
'quiet
)
2084 (epg-context-set-output-file context
2085 (epg--make-temp-file "epg-output"))
2086 (epg-start-decrypt context
(epg-make-data-from-file input-file
))
2087 (epg-wait-for-completion context
)
2088 (epg--check-error-for-decrypt context
)
2089 (epg-read-output context
))
2090 (epg-delete-output-file context
)
2091 (if (file-exists-p input-file
)
2092 (delete-file input-file
))
2093 (epg-reset context
))))
2095 (defun epg-start-verify (context signature
&optional signed-text
)
2096 "Initiate a verify operation on SIGNATURE.
2097 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
2099 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
2100 For a normal or a cleartext signature, SIGNED-TEXT should be nil.
2102 If you use this function, you will need to wait for the completion of
2103 `epg-gpg-program' by using `epg-wait-for-completion' and call
2104 `epg-reset' to clear a temporary output file.
2105 If you are unsure, use synchronous version of this function
2106 `epg-verify-file' or `epg-verify-string' instead."
2107 (epg-context-set-operation context
'verify
)
2108 (epg-context-set-result context nil
)
2110 ;; Detached signature.
2111 (if (epg-data-file signed-text
)
2112 (epg--start context
(list "--verify" "--" (epg-data-file signature
)
2113 (epg-data-file signed-text
)))
2114 (epg--start context
(list "--verify" "--" (epg-data-file signature
)
2116 (if (eq (process-status (epg-context-process context
)) 'run
)
2117 (process-send-string (epg-context-process context
)
2118 (epg-data-string signed-text
)))
2119 (if (eq (process-status (epg-context-process context
)) 'run
)
2120 (process-send-eof (epg-context-process context
))))
2121 ;; Normal (or cleartext) signature.
2122 (if (epg-data-file signature
)
2123 (epg--start context
(if (eq (epg-context-protocol context
) 'CMS
)
2124 (list "--verify" "--" (epg-data-file signature
))
2125 (list "--" (epg-data-file signature
))))
2126 (epg--start context
(if (eq (epg-context-protocol context
) 'CMS
)
2129 (if (eq (process-status (epg-context-process context
)) 'run
)
2130 (process-send-string (epg-context-process context
)
2131 (epg-data-string signature
)))
2132 (if (eq (process-status (epg-context-process context
)) 'run
)
2133 (process-send-eof (epg-context-process context
))))))
2135 (defun epg-verify-file (context signature
&optional signed-text plain
)
2136 "Verify a file SIGNATURE.
2137 SIGNED-TEXT and PLAIN are also a file if they are specified.
2139 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
2140 string. For a normal or a cleartext signature, SIGNED-TEXT should be
2141 nil. In the latter case, if PLAIN is specified, the plaintext is
2142 stored into the file after successful verification."
2146 (epg-context-set-output-file context plain
)
2147 (epg-context-set-output-file context
2148 (epg--make-temp-file "epg-output")))
2150 (epg-start-verify context
2151 (epg-make-data-from-file signature
)
2152 (epg-make-data-from-file signed-text
))
2153 (epg-start-verify context
2154 (epg-make-data-from-file signature
)))
2155 (epg-wait-for-completion context
)
2157 (epg-read-output context
)))
2159 (epg-delete-output-file context
))
2160 (epg-reset context
)))
2162 (defun epg-verify-string (context signature
&optional signed-text
)
2163 "Verify a string SIGNATURE.
2164 SIGNED-TEXT is a string if it is specified.
2166 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
2167 string. For a normal or a cleartext signature, SIGNED-TEXT should be
2168 nil. In the latter case, this function returns the plaintext after
2169 successful verification."
2170 (let ((coding-system-for-write 'binary
)
2174 (epg-context-set-output-file context
2175 (epg--make-temp-file "epg-output"))
2178 (setq input-file
(epg--make-temp-file "epg-signature"))
2179 (write-region signature nil input-file nil
'quiet
)
2180 (epg-start-verify context
2181 (epg-make-data-from-file input-file
)
2182 (epg-make-data-from-string signed-text
)))
2183 (epg-start-verify context
(epg-make-data-from-string signature
)))
2184 (epg-wait-for-completion context
)
2185 (epg-read-output context
))
2186 (epg-delete-output-file context
)
2188 (file-exists-p input-file
))
2189 (delete-file input-file
))
2190 (epg-reset context
))))
2192 (defun epg-start-sign (context plain
&optional mode
)
2193 "Initiate a sign operation on PLAIN.
2194 PLAIN is a data object.
2196 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2197 If it is nil or 'normal, it makes a normal signature.
2198 Otherwise, it makes a cleartext signature.
2200 If you use this function, you will need to wait for the completion of
2201 `epg-gpg-program' by using `epg-wait-for-completion' and call
2202 `epg-reset' to clear a temporary output file.
2203 If you are unsure, use synchronous version of this function
2204 `epg-sign-file' or `epg-sign-string' instead."
2205 (epg-context-set-operation context
'sign
)
2206 (epg-context-set-result context nil
)
2207 (unless (memq mode
'(t detached nil normal
)) ;i.e. cleartext
2208 (epg-context-set-armor context nil
)
2209 (epg-context-set-textmode context nil
))
2211 (append (list (if (memq mode
'(t detached
))
2213 (if (memq mode
'(nil normal
))
2221 (car (epg-key-sub-key-list signer
)))))
2222 (epg-context-signers context
)))
2223 (epg--args-from-sig-notations
2224 (epg-context-sig-notations context
))
2225 (if (epg-data-file plain
)
2226 (list "--" (epg-data-file plain
)))))
2227 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2228 (unless (eq (epg-context-protocol context
) 'CMS
)
2229 (epg-wait-for-status context
'("BEGIN_SIGNING")))
2230 (when (epg-data-string plain
)
2231 (if (eq (process-status (epg-context-process context
)) 'run
)
2232 (process-send-string (epg-context-process context
)
2233 (epg-data-string plain
)))
2234 (if (eq (process-status (epg-context-process context
)) 'run
)
2235 (process-send-eof (epg-context-process context
)))))
2237 (defun epg-sign-file (context plain signature
&optional mode
)
2238 "Sign a file PLAIN and store the result to a file SIGNATURE.
2239 If SIGNATURE is nil, it returns the result as a string.
2240 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2241 If it is nil or 'normal, it makes a normal signature.
2242 Otherwise, it makes a cleartext signature."
2246 (epg-context-set-output-file context signature
)
2247 (epg-context-set-output-file context
2248 (epg--make-temp-file "epg-output")))
2249 (epg-start-sign context
(epg-make-data-from-file plain
) mode
)
2250 (epg-wait-for-completion context
)
2251 (unless (epg-context-result-for context
'sign
)
2252 (let ((errors (epg-context-result-for context
'error
)))
2254 (list "Sign failed" (epg-errors-to-string errors
)))))
2256 (epg-read-output context
)))
2258 (epg-delete-output-file context
))
2259 (epg-reset context
)))
2261 (defun epg-sign-string (context plain
&optional mode
)
2262 "Sign a string PLAIN and return the output as string.
2263 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2264 If it is nil or 'normal, it makes a normal signature.
2265 Otherwise, it makes a cleartext signature."
2267 (unless (or (eq (epg-context-protocol context
) 'CMS
)
2270 (epg-check-configuration (epg-configuration))
2273 (epg--make-temp-file "epg-input")))
2274 (coding-system-for-write 'binary
))
2277 (epg-context-set-output-file context
2278 (epg--make-temp-file "epg-output"))
2280 (write-region plain nil input-file nil
'quiet
))
2281 (epg-start-sign context
2283 (epg-make-data-from-file input-file
)
2284 (epg-make-data-from-string plain
))
2286 (epg-wait-for-completion context
)
2287 (unless (epg-context-result-for context
'sign
)
2288 (if (epg-context-result-for context
'error
)
2289 (let ((errors (epg-context-result-for context
'error
)))
2291 (list "Sign failed" (epg-errors-to-string errors
))))))
2292 (epg-read-output context
))
2293 (epg-delete-output-file context
)
2295 (delete-file input-file
))
2296 (epg-reset context
))))
2298 (defun epg-start-encrypt (context plain recipients
2299 &optional sign always-trust
)
2300 "Initiate an encrypt operation on PLAIN.
2301 PLAIN is a data object.
2302 If RECIPIENTS is nil, it performs symmetric encryption.
2304 If you use this function, you will need to wait for the completion of
2305 `epg-gpg-program' by using `epg-wait-for-completion' and call
2306 `epg-reset' to clear a temporary output file.
2307 If you are unsure, use synchronous version of this function
2308 `epg-encrypt-file' or `epg-encrypt-string' instead."
2309 (epg-context-set-operation context
'encrypt
)
2310 (epg-context-set-result context nil
)
2312 (append (if always-trust
'("--always-trust"))
2313 (if recipients
'("--encrypt") '("--symmetric"))
2314 (if sign
'("--sign"))
2321 (car (epg-key-sub-key-list
2323 (epg-context-signers context
))))
2325 (epg--args-from-sig-notations
2326 (epg-context-sig-notations context
)))
2332 (car (epg-key-sub-key-list recipient
)))))
2334 (if (epg-data-file plain
)
2335 (list "--" (epg-data-file plain
)))))
2336 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2337 (unless (eq (epg-context-protocol context
) 'CMS
)
2339 (epg-wait-for-status context
'("BEGIN_SIGNING"))
2340 (epg-wait-for-status context
'("BEGIN_ENCRYPTION"))))
2341 (when (epg-data-string plain
)
2342 (if (eq (process-status (epg-context-process context
)) 'run
)
2343 (process-send-string (epg-context-process context
)
2344 (epg-data-string plain
)))
2345 (if (eq (process-status (epg-context-process context
)) 'run
)
2346 (process-send-eof (epg-context-process context
)))))
2348 (defun epg-encrypt-file (context plain recipients
2349 cipher
&optional sign always-trust
)
2350 "Encrypt a file PLAIN and store the result to a file CIPHER.
2351 If CIPHER is nil, it returns the result as a string.
2352 If RECIPIENTS is nil, it performs symmetric encryption."
2356 (epg-context-set-output-file context cipher
)
2357 (epg-context-set-output-file context
2358 (epg--make-temp-file "epg-output")))
2359 (epg-start-encrypt context
(epg-make-data-from-file plain
)
2360 recipients sign always-trust
)
2361 (epg-wait-for-completion context
)
2362 (let ((errors (epg-context-result-for context
'error
)))
2364 (not (epg-context-result-for context
'sign
)))
2366 (list "Sign failed" (epg-errors-to-string errors
))))
2369 (list "Encrypt failed" (epg-errors-to-string errors
)))))
2371 (epg-read-output context
)))
2373 (epg-delete-output-file context
))
2374 (epg-reset context
)))
2376 (defun epg-encrypt-string (context plain recipients
2377 &optional sign always-trust
)
2378 "Encrypt a string PLAIN.
2379 If RECIPIENTS is nil, it performs symmetric encryption."
2381 (unless (or (not sign
)
2382 (eq (epg-context-protocol context
) 'CMS
)
2385 (epg-check-configuration (epg-configuration))
2388 (epg--make-temp-file "epg-input")))
2389 (coding-system-for-write 'binary
))
2392 (epg-context-set-output-file context
2393 (epg--make-temp-file "epg-output"))
2395 (write-region plain nil input-file nil
'quiet
))
2396 (epg-start-encrypt context
2398 (epg-make-data-from-file input-file
)
2399 (epg-make-data-from-string plain
))
2400 recipients sign always-trust
)
2401 (epg-wait-for-completion context
)
2402 (let ((errors (epg-context-result-for context
'error
)))
2404 (not (epg-context-result-for context
'sign
)))
2406 (list "Sign failed" (epg-errors-to-string errors
))))
2409 (list "Encrypt failed" (epg-errors-to-string errors
)))))
2410 (epg-read-output context
))
2411 (epg-delete-output-file context
)
2413 (delete-file input-file
))
2414 (epg-reset context
))))
2416 (defun epg-start-export-keys (context keys
)
2417 "Initiate an export keys operation.
2419 If you use this function, you will need to wait for the completion of
2420 `epg-gpg-program' by using `epg-wait-for-completion' and call
2421 `epg-reset' to clear a temporary output file.
2422 If you are unsure, use synchronous version of this function
2423 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
2424 (epg-context-set-operation context
'export-keys
)
2425 (epg-context-set-result context nil
)
2426 (epg--start context
(cons "--export"
2430 (car (epg-key-sub-key-list key
))))
2433 (defun epg-export-keys-to-file (context keys file
)
2434 "Extract public KEYS."
2438 (epg-context-set-output-file context file
)
2439 (epg-context-set-output-file context
2440 (epg--make-temp-file "epg-output")))
2441 (epg-start-export-keys context keys
)
2442 (epg-wait-for-completion context
)
2443 (let ((errors (epg-context-result-for context
'error
)))
2446 (list "Export keys failed"
2447 (epg-errors-to-string errors
)))))
2449 (epg-read-output context
)))
2451 (epg-delete-output-file context
))
2452 (epg-reset context
)))
2454 (defun epg-export-keys-to-string (context keys
)
2455 "Extract public KEYS and return them as a string."
2456 (epg-export-keys-to-file context keys nil
))
2458 (defun epg-start-import-keys (context keys
)
2459 "Initiate an import keys operation.
2460 KEYS is a data object.
2462 If you use this function, you will need to wait for the completion of
2463 `epg-gpg-program' by using `epg-wait-for-completion' and call
2464 `epg-reset' to clear a temporary output file.
2465 If you are unsure, use synchronous version of this function
2466 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
2467 (epg-context-set-operation context
'import-keys
)
2468 (epg-context-set-result context nil
)
2469 (epg--start context
(if (epg-data-file keys
)
2470 (list "--import" "--" (epg-data-file keys
))
2472 (when (epg-data-string keys
)
2473 (if (eq (process-status (epg-context-process context
)) 'run
)
2474 (process-send-string (epg-context-process context
)
2475 (epg-data-string keys
)))
2476 (if (eq (process-status (epg-context-process context
)) 'run
)
2477 (process-send-eof (epg-context-process context
)))))
2479 (defun epg--import-keys-1 (context keys
)
2482 (epg-start-import-keys context keys
)
2483 (epg-wait-for-completion context
)
2484 (let ((errors (epg-context-result-for context
'error
)))
2487 (list "Import keys failed"
2488 (epg-errors-to-string errors
))))))
2489 (epg-reset context
)))
2491 (defun epg-import-keys-from-file (context keys
)
2492 "Add keys from a file KEYS."
2493 (epg--import-keys-1 context
(epg-make-data-from-file keys
)))
2495 (defun epg-import-keys-from-string (context keys
)
2496 "Add keys from a string KEYS."
2497 (epg--import-keys-1 context
(epg-make-data-from-string keys
)))
2499 (defun epg-start-receive-keys (context key-id-list
)
2500 "Initiate a receive key operation.
2501 KEY-ID-LIST is a list of key IDs.
2503 If you use this function, you will need to wait for the completion of
2504 `epg-gpg-program' by using `epg-wait-for-completion' and call
2505 `epg-reset' to clear a temporary output file.
2506 If you are unsure, use synchronous version of this function
2507 `epg-receive-keys' instead."
2508 (epg-context-set-operation context
'receive-keys
)
2509 (epg-context-set-result context nil
)
2510 (epg--start context
(cons "--recv-keys" key-id-list
)))
2512 (defun epg-receive-keys (context keys
)
2513 "Add keys from server.
2514 KEYS is a list of key IDs"
2517 (epg-start-receive-keys context keys
)
2518 (epg-wait-for-completion context
)
2519 (let ((errors (epg-context-result-for context
'error
)))
2522 (list "Receive keys failed"
2523 (epg-errors-to-string errors
))))))
2524 (epg-reset context
)))
2526 (defalias 'epg-import-keys-from-server
'epg-receive-keys
)
2528 (defun epg-start-delete-keys (context keys
&optional allow-secret
)
2529 "Initiate a delete keys operation.
2531 If you use this function, you will need to wait for the completion of
2532 `epg-gpg-program' by using `epg-wait-for-completion' and call
2533 `epg-reset' to clear a temporary output file.
2534 If you are unsure, use synchronous version of this function
2535 `epg-delete-keys' instead."
2536 (epg-context-set-operation context
'delete-keys
)
2537 (epg-context-set-result context nil
)
2538 (epg--start context
(cons (if allow-secret
2539 "--delete-secret-key"
2544 (car (epg-key-sub-key-list key
))))
2547 (defun epg-delete-keys (context keys
&optional allow-secret
)
2548 "Delete KEYS from the key ring."
2551 (epg-start-delete-keys context keys allow-secret
)
2552 (epg-wait-for-completion context
)
2553 (let ((errors (epg-context-result-for context
'error
)))
2556 (list "Delete keys failed"
2557 (epg-errors-to-string errors
))))))
2558 (epg-reset context
)))
2560 (defun epg-start-sign-keys (context keys
&optional local
)
2561 "Initiate a sign keys operation.
2563 If you use this function, you will need to wait for the completion of
2564 `epg-gpg-program' by using `epg-wait-for-completion' and call
2565 `epg-reset' to clear a temporary output file.
2566 If you are unsure, use synchronous version of this function
2567 `epg-sign-keys' instead."
2568 (declare (obsolete nil
"23.1"))
2569 (epg-context-set-operation context
'sign-keys
)
2570 (epg-context-set-result context nil
)
2571 (epg--start context
(cons (if local
2577 (car (epg-key-sub-key-list key
))))
2580 (defun epg-sign-keys (context keys
&optional local
)
2581 "Sign KEYS from the key ring."
2582 (declare (obsolete nil
"23.1"))
2585 (epg-start-sign-keys context keys local
)
2586 (epg-wait-for-completion context
)
2587 (let ((errors (epg-context-result-for context
'error
)))
2590 (list "Sign keys failed"
2591 (epg-errors-to-string errors
))))))
2592 (epg-reset context
)))
2594 (defun epg-start-generate-key (context parameters
)
2595 "Initiate a key generation.
2596 PARAMETERS specifies parameters for the key.
2598 If you use this function, you will need to wait for the completion of
2599 `epg-gpg-program' by using `epg-wait-for-completion' and call
2600 `epg-reset' to clear a temporary output file.
2601 If you are unsure, use synchronous version of this function
2602 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2603 (epg-context-set-operation context
'generate-key
)
2604 (epg-context-set-result context nil
)
2605 (if (epg-data-file parameters
)
2606 (epg--start context
(list "--batch" "--genkey" "--"
2607 (epg-data-file parameters
)))
2608 (epg--start context
'("--batch" "--genkey"))
2609 (if (eq (process-status (epg-context-process context
)) 'run
)
2610 (process-send-string (epg-context-process context
)
2611 (epg-data-string parameters
)))
2612 (if (eq (process-status (epg-context-process context
)) 'run
)
2613 (process-send-eof (epg-context-process context
)))))
2615 (defun epg-generate-key-from-file (context parameters
)
2616 "Generate a new key pair.
2617 PARAMETERS is a file which tells how to create the key."
2620 (epg-start-generate-key context
(epg-make-data-from-file parameters
))
2621 (epg-wait-for-completion context
)
2622 (let ((errors (epg-context-result-for context
'error
)))
2625 (list "Generate key failed"
2626 (epg-errors-to-string errors
))))))
2627 (epg-reset context
)))
2629 (defun epg-generate-key-from-string (context parameters
)
2630 "Generate a new key pair.
2631 PARAMETERS is a string which tells how to create the key."
2634 (epg-start-generate-key context
(epg-make-data-from-string parameters
))
2635 (epg-wait-for-completion context
)
2636 (let ((errors (epg-context-result-for context
'error
)))
2639 (list "Generate key failed"
2640 (epg-errors-to-string errors
))))))
2641 (epg-reset context
)))
2643 (defun epg--decode-percent-escape (string)
2645 (while (string-match "%\\(\\(%\\)\\|\\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2647 (if (match-beginning 2)
2648 (setq string
(replace-match "%" t t string
)
2649 index
(1- (match-end 0)))
2650 (setq string
(replace-match
2651 (string (string-to-number (match-string 3 string
) 16))
2653 index
(- (match-end 0) 2))))
2656 (defun epg--decode-hexstring (string)
2658 (while (eq index
(string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index
))
2659 (setq string
(replace-match (string (string-to-number
2660 (match-string 0 string
) 16))
2662 index
(1- (match-end 0))))
2665 (defun epg--decode-quotedstring (string)
2667 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2668 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2670 (if (match-beginning 2)
2671 (setq string
(replace-match "\\2" t nil string
)
2672 index
(1- (match-end 0)))
2673 (if (match-beginning 3)
2674 (setq string
(replace-match (string (string-to-number
2675 (match-string 0 string
) 16))
2677 index
(- (match-end 0) 2)))))
2680 (defun epg-dn-from-string (string)
2681 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2682 The return value is an alist mapping from types to values."
2684 (length (length string
))
2685 alist type value group
)
2686 (while (< index length
)
2687 (if (eq index
(string-match "[ \t\n\r]*" string index
))
2688 (setq index
(match-end 0)))
2689 (if (eq index
(string-match
2690 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2692 (setq type
(match-string 1 string
)
2693 index
(match-end 0))
2694 (if (eq index
(string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2696 (setq type
(match-string 1 string
)
2697 index
(match-end 0))))
2699 (error "Invalid type"))
2700 (if (eq index
(string-match
2701 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2703 (setq index
(match-end 0)
2704 value
(epg--decode-quotedstring (match-string 0 string
)))
2705 (if (eq index
(string-match "#\\([0-9A-Fa-f]+\\)" string index
))
2706 (setq index
(match-end 0)
2707 value
(epg--decode-hexstring (match-string 1 string
)))
2708 (if (eq index
(string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2710 (setq index
(match-end 0)
2711 value
(epg--decode-quotedstring
2712 (match-string 0 string
))))))
2714 (if (stringp (car (car alist
)))
2715 (setcar alist
(list (cons type value
) (car alist
)))
2716 (setcar alist
(cons (cons type value
) (car alist
))))
2717 (if (consp (car (car alist
)))
2718 (setcar alist
(nreverse (car alist
))))
2719 (setq alist
(cons (cons type value
) alist
)
2722 (if (eq index
(string-match "[ \t\n\r]*\\([,;+]\\)" string index
))
2723 (setq index
(match-end 0)
2724 group
(eq (aref string
(match-beginning 1)) ?
+))))
2727 (defun epg-decode-dn (alist)
2728 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2729 Type names are resolved using `epg-dn-type-alist'."
2732 (if (stringp (car rdn
))
2733 (let ((entry (assoc (car rdn
) epg-dn-type-alist
)))
2735 (format "%s=%s" (cdr entry
) (cdr rdn
))
2736 (format "%s=%s" (car rdn
) (cdr rdn
))))
2737 (concat "(" (epg-decode-dn rdn
) ")")))
2743 ;;; epg.el ends here