2 "EasyPG, yet another GnuPG interface.")
4 (defcustom epg-gpg-program "gpg"
5 "The `gpg' executable."
9 (defvar epg-user-id nil
10 "GnuPG ID of your default identity.")
12 (defvar epg-user-id-alist nil
13 "An alist mapping from key ID to user ID.")
15 (defvar epg-read-point nil)
16 (defvar epg-pending-status-list nil)
17 (defvar epg-key-id nil)
18 (defvar epg-context nil)
19 (defvar epg-debug nil)
21 (defvar epg-colons-pub-spec
23 (length "[0-9]+" 0 string-to-number)
24 (algorithm "[0-9]+" 0 string-to-number)
26 (creation-date "[0-9]+")
27 (expiration-date "[0-9]+")
32 (capability "[escaESCA]*"))
33 "The schema of keylisting output whose type is \"pub\".
34 This is used by `epg-list-keys'.")
36 (defvar epg-colons-sec-spec
38 (length "[0-9]+" 0 string-to-number)
39 (algorithm "[0-9]+" 0 string-to-number)
41 (creation-date "[0-9]+")
42 (expiration-date "[0-9]+")
45 "The schema of keylisting output whose type is \"sec\".
46 This is used by `epg-list-keys'.")
48 (defvar epg-colons-uid-spec
53 (creation-date "[0-9]+")
54 (expiration-date "[0-9]+")
58 "The schema of keylisting output whose type is \"uid\".
59 This is used by `epg-list-keys'.")
61 (defun epg-make-context (&optional protocol armor textmode include-certs)
62 "Return a context object."
63 (vector protocol armor textmode include-certs
64 (cons #'epg-passphrase-callback-function nil)
65 (cons #'epg-progress-callback-function nil)
68 (defun epg-context-protocol (context)
69 "Return the protocol used within the context."
72 (defun epg-context-armor (context)
73 "Return t if the output shouled be ASCII armored in the CONTEXT context."
76 (defun epg-context-textmode (context)
77 "Return t if canonical text mode should be used in the CONTEXT context."
80 (defun epg-context-include-certs (context)
81 "Return how many certificates should be included in an S/MIME signed
85 (defun epg-context-passphrase-callback-info (context)
86 "Return the function used to query passphrase."
89 (defun epg-context-progress-callback-info (context)
90 "Return the function which handles progress update."
93 (defun epg-context-signers (context)
94 "Return the list of key-id for singning."
97 (defun epg-context-process (context)
98 "Return the process object of `epg-gpg-program'.
99 This function is for internal use only."
102 (defun epg-context-output-file (context)
103 "Return the output file of `epg-gpg-program'.
104 This function is for internal use only."
107 (defun epg-context-result (context)
108 "Return the result of the previous cryptographic operation."
111 (defun epg-context-set-protocol (context protocol)
112 "Set the protocol used within the context."
113 (aset context 0 protocol))
115 (defun epg-context-set-armor (context armor)
116 "Specify if the output shouled be ASCII armored in the CONTEXT context."
117 (aset context 1 armor))
119 (defun epg-context-set-textmode (context textmode)
120 "Specify if canonical text mode should be used in the CONTEXT context."
121 (aset context 2 textmode))
123 (defun epg-context-set-include-certs (context include-certs)
124 "Set how many certificates should be included in an S/MIME signed message."
125 (aset context 3 include-certs))
127 (defun epg-context-set-passphrase-callback-info (context
128 passphrase-callback-info)
129 "Set the function used to query passphrase."
130 (aset context 4 passphrase-callback-info))
132 (defun epg-context-set-progress-callback-info (context progress-callback-info)
133 "Set the function which handles progress update."
134 (aset context 5 progress-callback-info))
136 (defun epg-context-set-signers (context signers)
137 "Set the list of key-id for singning."
138 (aset context 6 signers))
140 (defun epg-context-set-process (context process)
141 "Set the process object of `epg-gpg-program'.
142 This function is for internal use only."
143 (aset context 7 process))
145 (defun epg-context-set-output-file (context output-file)
146 "Set the output file of `epg-gpg-program'.
147 This function is for internal use only."
148 (aset context 8 output-file))
150 (defun epg-context-set-result (context result)
151 "Set the result of the previous cryptographic operation."
152 (aset context 9 result))
154 (defun epg-make-signature (status key-id user-id)
155 "Return a signature object."
156 (vector status key-id user-id nil))
158 (defun epg-signature-status (signature)
159 "Return the status code of SIGNATURE."
162 (defun epg-signature-key-id (signature)
163 "Return the key-id of SIGNATURE."
166 (defun epg-signature-user-id (signature)
167 "Return the user-id of SIGNATURE."
170 (defun epg-signature-validity (signature)
171 "Return the validity of SIGNATURE."
174 (defun epg-signature-set-status (signature status)
175 "Set the status code of SIGNATURE."
176 (aset signature 0 status))
178 (defun epg-signature-set-key-id (signature key-id)
179 "Set the key-id of SIGNATURE."
180 (aset signature 1 key-id))
182 (defun epg-signature-set-user-id (signature user-id)
183 "Set the user-id of SIGNATURE."
184 (aset signature 2 user-id))
186 (defun epg-signature-set-validity (signature validity)
187 "Set the validity of SIGNATURE."
188 (aset signature 3 validity))
190 (defun epg-context-result-for (context name)
191 (cdr (assq name (epg-context-result context))))
193 (defun epg-context-set-result-for (context name value)
194 (let* ((result (epg-context-result context))
195 (entry (assq name result)))
198 (epg-context-set-result context (cons (cons name value) result)))))
200 (defun epg-start (context args)
201 "Start `epg-gpg-program' in a subprocess with given ARGS."
202 (let* ((args (append (list "--no-tty"
206 (if (epg-context-armor context) '("--armor"))
207 (if (epg-context-textmode context) '("--textmode"))
208 (if (epg-context-output-file context)
209 (list "--output" (epg-context-output-file context)))
211 (coding-system-for-write 'binary)
212 process-connection-type
213 (orig-mode (default-file-modes))
214 (buffer (generate-new-buffer " *epg*"))
216 (with-current-buffer buffer
217 (make-local-variable 'epg-read-point)
218 (setq epg-read-point (point-min))
219 (make-local-variable 'epg-pending-status-list)
220 (setq epg-pending-status-list nil)
221 (make-local-variable 'epg-key-id)
222 (setq epg-key-id nil)
223 (make-local-variable 'epg-context)
224 (setq epg-context context))
227 (set-default-file-modes 448)
229 (apply #'start-process "epg" buffer epg-gpg-program args)))
230 (set-default-file-modes orig-mode))
231 (set-process-filter process #'epg-process-filter)
232 (epg-context-set-process context process)))
234 (defun epg-process-filter (process input)
237 (set-buffer (get-buffer-create " *epg-debug*"))
238 (goto-char (point-max))
240 (if (buffer-live-p (process-buffer process))
242 (set-buffer (process-buffer process))
243 (goto-char (point-max))
245 (goto-char epg-read-point)
247 (while (looking-at ".*\n") ;the input line is finished
249 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
250 (let* ((status (match-string 1))
251 (string (match-string 2))
252 (symbol (intern-soft (concat "epg-status-" status))))
253 (if (member status epg-pending-status-list)
254 (setq epg-pending-status-list nil))
257 (funcall symbol process string)))))
259 (setq epg-read-point (point)))))
261 (defun epg-read-output (context)
263 (set-buffer-multibyte nil)
264 (if (file-exists-p (epg-context-output-file context))
265 (let ((coding-system-for-read (if (epg-context-textmode context)
268 (insert-file-contents (epg-context-output-file context))
271 (defun epg-wait-for-status (context status-list)
272 (with-current-buffer (process-buffer (epg-context-process context))
273 (setq epg-pending-status-list status-list)
274 (while (and (eq (process-status (epg-context-process context)) 'run)
275 epg-pending-status-list)
276 (accept-process-output (epg-context-process context) 1))))
278 (defun epg-wait-for-completion (context)
279 (process-send-eof (epg-context-process context))
280 (while (eq (process-status (epg-context-process context)) 'run)
281 ;; We can't use accept-process-output instead of sit-for here
282 ;; because it may cause an interrupt during the sentinel execution.
285 (defun epg-reset (context)
286 (if (and (epg-context-process context)
287 (buffer-live-p (process-buffer (epg-context-process context))))
288 (kill-buffer (process-buffer (epg-context-process context))))
289 (epg-context-set-process context nil)
290 (if (file-exists-p (epg-context-output-file context))
291 (delete-file (epg-context-output-file context)))
292 (aset context 9 nil))
294 (defun epg-status-USERID_HINT (process string)
295 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
296 (let* ((key-id (match-string 1 string))
297 (user-id (match-string 2 string))
298 (entry (assoc key-id epg-user-id-alist)))
300 (setcdr entry user-id)
301 (setq epg-user-id-alist (cons (cons key-id user-id)
302 epg-user-id-alist))))))
304 (defun epg-status-NEED_PASSPHRASE (process string)
305 (if (string-match "\\`\\([^ ]+\\)" string)
306 (setq epg-key-id (match-string 1 string))))
308 (defun epg-status-NEED_PASSPHRASE_SYM (process string)
309 (setq epg-key-id 'SYM))
311 (defun epg-status-NEED_PASSPHRASE_PIN (process string)
312 (setq epg-key-id 'PIN))
314 (defun epg-status-GET_HIDDEN (process string)
316 (funcall (car (epg-context-passphrase-callback-info epg-context))
318 (cdr (epg-context-passphrase-callback-info epg-context))))
323 (setq string (concat passphrase "\n"))
324 (fillarray passphrase 0)
325 (setq passphrase nil)
326 (process-send-string process string))
328 (fillarray string 0))))))
330 (defun epg-status-GOODSIG (process string)
331 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
332 (epg-context-set-result-for
335 (cons (epg-make-signature 'good
336 (match-string 1 string)
337 (match-string 2 string))
338 (epg-context-result-for epg-context 'verify)))))
340 (defun epg-status-EXPSIG (process string)
341 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
342 (epg-context-set-result-for
345 (cons (epg-make-signature 'expired
346 (match-string 1 string)
347 (match-string 2 string))
348 (epg-context-result-for epg-context 'verify)))))
350 (defun epg-status-EXPKEYSIG (process string)
351 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
352 (epg-context-set-result-for
355 (cons (epg-make-signature 'expired-key
356 (match-string 1 string)
357 (match-string 2 string))
358 (epg-context-result-for epg-context 'verify)))))
360 (defun epg-status-REVKEYSIG (process string)
361 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
362 (epg-context-set-result-for
365 (cons (epg-make-signature 'revoked-key
366 (match-string 1 string)
367 (match-string 2 string))
368 (epg-context-result-for epg-context 'verify)))))
370 (defun epg-status-BADSIG (process string)
371 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
372 (epg-context-set-result-for
375 (cons (epg-make-signature 'bad
376 (match-string 1 string)
377 (match-string 2 string))
378 (epg-context-result-for epg-context 'verify)))))
380 (defun epg-status-TRUST_UNDEFINED (process string)
381 (let ((signature (car (epg-context-result-for-for epg-context 'verify))))
383 (eq (epg-signature-status signature) 'good))
384 (epg-signature-set-validity signature 'unknown))))
386 (defun epg-status-TRUST_NEVER (process string)
387 (let ((signature (car (epg-context-result-for epg-context 'verify))))
389 (eq (epg-signature-status signature) 'good))
390 (epg-signature-set-validity signature 'never))))
392 (defun epg-status-TRUST_MARGINAL (process string)
393 (let ((signature (car (epg-context-result-for epg-context 'verify))))
395 (eq (epg-signature-status signature) 'marginal))
396 (epg-signature-set-validity signature 'marginal))))
398 (defun epg-status-TRUST_FULLY (process string)
399 (let ((signature (car (epg-context-result-for epg-context 'verify))))
401 (eq (epg-signature-status signature) 'good))
402 (epg-signature-set-validity signature 'full))))
404 (defun epg-status-TRUST_ULTIMATE (process string)
405 (let ((signature (car (epg-context-result-for epg-context 'verify))))
407 (eq (epg-signature-status signature) 'good))
408 (epg-signature-set-validity signature 'full))))
410 (defun epg-status-DECRYPTION_FAILED (process string)
411 (epg-context-set-result-for epg-context 'decrypt 'failed))
413 (defun epg-status-PROGRESS (process string)
414 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
416 (funcall (car (epg-context-progress-callback-info epg-context))
417 (match-string 1 string)
418 (match-string 2 string)
419 (string-to-number (match-string 3 string))
420 (string-to-number (match-string 4 string))
421 (cdr (epg-context-progress-callback-info epg-context)))))
423 (defun epg-passphrase-callback-function (key-id handback)
426 "Passphrase for symmetric encryption: "
428 "Passphrase for PIN: "
429 (format "Passphrase for %s: "
430 (let ((entry (assoc key-id epg-user-id-alist)))
435 (defun epg-progress-callback-function (what char current total handback)
436 (message "%s: %d%%/%d%%" what current total))
438 (defun epg-list-keys (name &optional secret)
439 "List keys associated with STRING."
440 (let ((args (list "--with-colons" "--no-greeting" "--batch"
442 (if secret "--list-secret-keys" "--list-keys")
444 keys type symbol pointer)
446 (apply #'call-process epg-gpg-program nil (list t nil) nil args)
447 (goto-char (point-min))
448 (while (looking-at "\\([a-z][a-z][a-z]\\):\\(.*\\)")
449 (setq type (match-string 1)
450 symbol (intern-soft (format "epg-colons-%s-spec" type)))
451 (if (member type '("pub" "sec"))
452 (setq keys (cons nil keys)))
455 (setcar keys (cons (cons (intern type)
457 (symbol-value symbol)
463 (setcar pointer (nreverse (car pointer)))
464 (setq pointer (cdr pointer)))
467 (defun epg-parse-colons (alist string)
471 (or (null (car alist))
474 (concat "\\(" (nth 1 (car alist)) "\\)?:")
478 (setq index (match-end 0))
479 (if (match-beginning 1)
481 (cons (cons (car (car alist))
482 (funcall (or (nth 3 (car alist)) #'identity)
484 (1+ (or (nth 2 (car alist)) 0))
487 (setq index (1+ index)))
488 (setq alist (cdr alist)))
491 (defalias 'epg-make-temp-file 'make-temp-file)
494 (defun epg-decrypt-start (context input-file)
495 "Initiate a decrypt operation on INPUT-FILE.
497 If you use this function, you will need to wait for the completion of
498 `epg-gpg-program' by using `epg-wait-for-completion' and call
499 `epg-reset' to clear a temporaly output file.
500 If you are unsure, use synchronous version of this function
501 `epg-decrypt-string' instead."
502 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
504 (list "--decrypt" input-file))
505 (epg-wait-for-status context '("BEGIN_DECRYPTION")))
508 (defun epg-decrypt-file (context input-file)
509 "Decrypt INPUT-FILE and return the plain text."
512 (epg-decrypt-start context input-file)
513 (epg-wait-for-completion context)
514 (unless (epg-context-result-for context 'decrypt)
515 (epg-read-output context)))
517 (if (file-exists-p input-file)
518 (delete-file input-file))))
521 (defun epg-decrypt-string (context string)
522 "Decrypt STRING and return the plain text."
523 (let ((input-file (epg-make-temp-file "epg-input"))
524 (coding-system-for-write 'binary))
527 (write-region string nil input-file)
528 (epg-decrypt-file context input-file))
529 (if (file-exists-p input-file)
530 (delete-file input-file)))))
533 (defun epg-verify-start (context signature &optional string)
534 "Initiate a verify operation on SIGNATURE.
536 For a detached signature, both SIGNATURE and STRING should be string.
537 For a normal or a clear text signature, STRING should be nil.
539 If you use this function, you will need to wait for the completion of
540 `epg-gpg-program' by using `epg-wait-for-completion' and call
541 `epg-reset' to clear a temporaly output file.
542 If you are unsure, use synchronous version of this function
543 `epg-verify-string' instead."
544 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
546 ;; Detached signature.
549 (append (list "--verify")
550 (list signature "-")))
551 (if (eq (process-status (epg-context-process context)) 'run)
552 (process-send-string (epg-context-process context) string)))
553 ;; Normal (or cleartext) signature.
554 (epg-start context (list "--verify"))
555 (if (eq (process-status (epg-context-process context)) 'run)
556 (process-send-string (epg-context-process context) signature))))
559 (defun epg-verify-string (context signature &optional string)
562 For a detached signature, both SIGNATURE and STRING should be string.
563 For a normal or a clear text signature, STRING should be nil."
564 (let ((input-file (epg-make-temp-file "epg-input"))
565 (coding-system-for-write 'binary))
569 (write-region signature nil input-file))
570 (epg-verify-start context input-file string)
571 (epg-wait-for-completion context)
572 (epg-context-result-for context 'verify))
574 (if (file-exists-p input-file)
575 (delete-file input-file)))))
578 (defun epg-sign-start (context string &optional mode)
579 "Initiate a sign operation on STRING.
581 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
582 If MODE is t or 'detached, it makes a detached signature.
583 Otherwise, it makes a normal signature.
585 If you use this function, you will need to wait for the completion of
586 `epg-gpg-program' by using `epg-wait-for-completion' and call
587 `epg-reset' to clear a temporaly output file.
588 If you are unsure, use synchronous version of this function
589 `epg-sign-string' instead."
590 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
592 (append (list (if (eq 'clearsign)
594 (if (or (eq mode t) (eq mode 'detached))
598 (mapcar (lambda (signer)
600 (epg-context-signers context)))))
601 (epg-wait-for-status context '("BEGIN_SIGNING"))
602 (if (eq (process-status (epg-context-process context)) 'run)
603 (process-send-string (epg-context-process context) string)))
606 (defun epg-sign-string (context string &optional mode)
607 "Sign STRING and return the output as string.
608 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
609 If MODE is t or 'detached, it makes a detached signature.
610 Otherwise, it makes a normal signature."
613 (epg-sign-start context string mode)
614 (epg-wait-for-completion context)
615 (epg-read-output context))
616 (epg-reset context)))
619 (defun epg-encrypt-start (context string recipients
620 &optional sign always-trust)
621 "Initiate a encrypt operation on STRING.
622 If RECIPIENTS is nil, it performs symmetric encryption.
624 If you use this function, you will need to wait for the completion of
625 `epg-gpg-program' by using `epg-wait-for-completion' and call
626 `epg-reset' to clear a temporaly output file.
627 If you are unsure, use synchronous version of this function
628 `epg-encrypt-string' instead."
629 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
631 (append (if always-trust '("--always-trust"))
632 (if recipients '("--encrypt") '("--symmetric"))
636 (mapcar (lambda (signer)
638 (epg-context-signers context)))))
640 (mapcar (lambda (recipient)
641 (list "-r" recipient))
644 (epg-wait-for-status context '("BEGIN_SIGNING"))
645 (if (null recipients)
646 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
647 (if (eq (process-status (epg-context-process context)) 'run)
648 (process-send-string (epg-context-process context) string)))
651 (defun epg-encrypt-string (context string recipients
652 &optional sign always-trust)
654 If RECIPIENTS is nil, it performs symmetric encryption."
657 (epg-encrypt-start context string recipients sign always-trust)
658 (epg-wait-for-completion context)
659 (epg-read-output context))
660 (epg-reset context)))