1 ;;; epg.el --- the EasyPG Library
2 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
3 ;; 2005, 2006 Free Software Foundation, Inc.
4 ;; Copyright (C) 2006 Daiki Ueno
6 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Keywords: PGP, GnuPG
9 ;; This file is part of EasyPG.
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
31 (defcustom epg-gpg-program "gpg"
32 "The `gpg' executable."
36 (defcustom epg-gpgsm-program "gpgsm"
37 "The `gpgsm' executable."
41 (defconst epg-version-number "0.0.1")
43 (defvar epg-user-id nil
44 "GnuPG ID of your default identity.")
46 (defvar epg-user-id-alist nil
47 "An alist mapping from key ID to user ID.")
49 (defvar epg-read-point nil)
50 (defvar epg-pending-status-list nil)
51 (defvar epg-key-id nil)
52 (defvar epg-context nil)
53 (defvar epg-debug nil)
54 (defvar epg-debug-buffer nil)
56 ;; from gnupg/include/cipher.h
57 (defconst epg-cipher-algorithm-alist
69 ;; from gnupg/include/cipher.h
70 (defconst epg-pubkey-algorithm-alist
78 ;; from gnupg/include/cipher.h
79 (defconst epg-digest-algorithm-alist
87 ;; from gnupg/include/cipher.h
88 (defconst epg-compress-algorithm-alist
94 (defconst epg-invalid-recipients-alist
95 '((0 . "No specific reason given")
97 (2 . "Ambigious specification")
98 (3 . "Wrong key usage")
103 (8 . "Policy mismatch")
104 (9 . "Not a secret key")
105 (10 . "Key not trusted")))
107 (defconst epg-delete-problem-alist
108 '((1 . "No such key")
109 (2 . "Must delete secret key first")
110 (3 . "Ambigious specification")))
112 (defvar epg-key-validity-alist
125 (defvar epg-key-capablity-alist
129 (?a . authentication)))
131 (defvar epg-dn-type-alist
132 '(("1.2.840.113549.1.9.1" . "EMail")
136 ("0.2.262.1.10.7.20" . "NameDistinguisher")
137 ("2.5.4.16" . "ADDR")
140 ("2.5.4.17" . "PostalCode")
141 ("2.5.4.65" . "Pseudo")
142 ("2.5.4.5" . "SerialNumber")))
144 (defvar epg-prompt-alist nil)
146 (defun epg-make-data-from-file (file)
147 "Make a data object from FILE."
148 (cons 'epg-data (vector file nil)))
150 (defun epg-make-data-from-string (string)
151 "Make a data object from STRING."
152 (cons 'epg-data (vector nil string)))
154 (defun epg-data-file (data)
155 "Return the file of DATA."
156 (unless (eq (car data) 'epg-data)
157 (signal 'wrong-type-argument (list 'epg-data-p data)))
160 (defun epg-data-string (data)
161 "Return the string of DATA."
162 (unless (eq (car data) 'epg-data)
163 (signal 'wrong-type-argument (list 'epg-data-p data)))
166 (defun epg-make-context (&optional protocol armor textmode include-certs
167 cipher-algorithm digest-algorithm
169 "Return a context object."
171 (vector protocol armor textmode include-certs
172 cipher-algorithm digest-algorithm compress-algorithm
173 #'epg-passphrase-callback-function
174 #'epg-progress-callback-function
177 (defun epg-context-protocol (context)
178 "Return the protocol used within CONTEXT."
179 (unless (eq (car context) 'epg-context)
180 (signal 'wrong-type-argument (list 'epg-context-p context)))
181 (aref (cdr context) 0))
183 (defun epg-context-armor (context)
184 "Return t if the output shouled be ASCII armored in CONTEXT."
185 (unless (eq (car context) 'epg-context)
186 (signal 'wrong-type-argument (list 'epg-context-p context)))
187 (aref (cdr context) 1))
189 (defun epg-context-textmode (context)
190 "Return t if canonical text mode should be used in CONTEXT."
191 (unless (eq (car context) 'epg-context)
192 (signal 'wrong-type-argument (list 'epg-context-p context)))
193 (aref (cdr context) 2))
195 (defun epg-context-include-certs (context)
196 "Return how many certificates should be included in an S/MIME signed
198 (unless (eq (car context) 'epg-context)
199 (signal 'wrong-type-argument (list 'epg-context-p context)))
200 (aref (cdr context) 3))
202 (defun epg-context-cipher-algorithm (context)
203 "Return the cipher algorithm in CONTEXT."
204 (unless (eq (car context) 'epg-context)
205 (signal 'wrong-type-argument (list 'epg-context-p context)))
206 (aref (cdr context) 4))
208 (defun epg-context-digest-algorithm (context)
209 "Return the digest algorithm in CONTEXT."
210 (unless (eq (car context) 'epg-context)
211 (signal 'wrong-type-argument (list 'epg-context-p context)))
212 (aref (cdr context) 5))
214 (defun epg-context-compress-algorithm (context)
215 "Return the compress algorithm in CONTEXT."
216 (unless (eq (car context) 'epg-context)
217 (signal 'wrong-type-argument (list 'epg-context-p context)))
218 (aref (cdr context) 6))
220 (defun epg-context-passphrase-callback (context)
221 "Return the function used to query passphrase."
222 (unless (eq (car context) 'epg-context)
223 (signal 'wrong-type-argument (list 'epg-context-p context)))
224 (aref (cdr context) 7))
226 (defun epg-context-progress-callback (context)
227 "Return the function which handles progress update."
228 (unless (eq (car context) 'epg-context)
229 (signal 'wrong-type-argument (list 'epg-context-p context)))
230 (aref (cdr context) 8))
232 (defun epg-context-signers (context)
233 "Return the list of key-id for singning."
234 (unless (eq (car context) 'epg-context)
235 (signal 'wrong-type-argument (list 'epg-context-p context)))
236 (aref (cdr context) 9))
238 (defun epg-context-process (context)
239 "Return the process object of `epg-gpg-program'.
240 This function is for internal use only."
241 (unless (eq (car context) 'epg-context)
242 (signal 'wrong-type-argument (list 'epg-context-p context)))
243 (aref (cdr context) 10))
245 (defun epg-context-output-file (context)
246 "Return the output file of `epg-gpg-program'.
247 This function is for internal use only."
248 (unless (eq (car context) 'epg-context)
249 (signal 'wrong-type-argument (list 'epg-context-p context)))
250 (aref (cdr context) 11))
252 (defun epg-context-result (context)
253 "Return the result of the previous cryptographic operation."
254 (unless (eq (car context) 'epg-context)
255 (signal 'wrong-type-argument (list 'epg-context-p context)))
256 (aref (cdr context) 12))
258 (defun epg-context-set-protocol (context protocol)
259 "Set the protocol used within CONTEXT."
260 (unless (eq (car context) 'epg-context)
261 (signal 'wrong-type-argument (list 'epg-context-p context)))
262 (aset (cdr context) 0 protocol))
264 (defun epg-context-set-armor (context armor)
265 "Specify if the output shouled be ASCII armored in CONTEXT."
266 (unless (eq (car context) 'epg-context)
267 (signal 'wrong-type-argument (list 'epg-context-p context)))
268 (aset (cdr context) 1 armor))
270 (defun epg-context-set-textmode (context textmode)
271 "Specify if canonical text mode should be used in CONTEXT."
272 (unless (eq (car context) 'epg-context)
273 (signal 'wrong-type-argument (list 'epg-context-p context)))
274 (aset (cdr context) 2 textmode))
276 (defun epg-context-set-include-certs (context include-certs)
277 "Set how many certificates should be included in an S/MIME signed message."
278 (unless (eq (car context) 'epg-context)
279 (signal 'wrong-type-argument (list 'epg-context-p context)))
280 (aset (cdr context) 3 include-certs))
282 (defun epg-context-set-cipher-algorithm (context cipher-algorithm)
283 "Set the cipher algorithm in CONTEXT."
284 (unless (eq (car context) 'epg-context)
285 (signal 'wrong-type-argument (list 'epg-context-p context)))
286 (aset (cdr context) 4 cipher-algorithm))
288 (defun epg-context-set-digest-algorithm (context digest-algorithm)
289 "Set the digest algorithm in CONTEXT."
290 (unless (eq (car context) 'epg-context)
291 (signal 'wrong-type-argument (list 'epg-context-p context)))
292 (aset (cdr context) 5 digest-algorithm))
294 (defun epg-context-set-compress-algorithm (context compress-algorithm)
295 "Set the compress algorithm in CONTEXT."
296 (unless (eq (car context) 'epg-context)
297 (signal 'wrong-type-argument (list 'epg-context-p context)))
298 (aset (cdr context) 6 compress-algorithm))
300 (defun epg-context-set-passphrase-callback (context
302 "Set the function used to query passphrase."
303 (unless (eq (car context) 'epg-context)
304 (signal 'wrong-type-argument (list 'epg-context-p context)))
305 (aset (cdr context) 7 passphrase-callback))
307 (defun epg-context-set-progress-callback (context progress-callback)
308 "Set the function which handles progress update."
309 (unless (eq (car context) 'epg-context)
310 (signal 'wrong-type-argument (list 'epg-context-p context)))
311 (aset (cdr context) 8 progress-callback))
313 (defun epg-context-set-signers (context signers)
314 "Set the list of key-id for singning."
315 (unless (eq (car context) 'epg-context)
316 (signal 'wrong-type-argument (list 'epg-context-p context)))
317 (aset (cdr context) 9 signers))
319 (defun epg-context-set-process (context process)
320 "Set the process object of `epg-gpg-program'.
321 This function is for internal use only."
322 (unless (eq (car context) 'epg-context)
323 (signal 'wrong-type-argument (list 'epg-context-p context)))
324 (aset (cdr context) 10 process))
326 (defun epg-context-set-output-file (context output-file)
327 "Set the output file of `epg-gpg-program'.
328 This function is for internal use only."
329 (unless (eq (car context) 'epg-context)
330 (signal 'wrong-type-argument (list 'epg-context-p context)))
331 (aset (cdr context) 11 output-file))
333 (defun epg-context-set-result (context result)
334 "Set the result of the previous cryptographic operation."
335 (unless (eq (car context) 'epg-context)
336 (signal 'wrong-type-argument (list 'epg-context-p context)))
337 (aset (cdr context) 12 result))
339 (defun epg-make-signature (status key-id user-id)
340 "Return a signature object."
341 (cons 'epg-signature (vector status key-id user-id nil nil)))
343 (defun epg-signature-status (signature)
344 "Return the status code of SIGNATURE."
345 (unless (eq (car signature) 'epg-signature)
346 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
347 (aref (cdr signature) 0))
349 (defun epg-signature-key-id (signature)
350 "Return the key-id of SIGNATURE."
351 (unless (eq (car signature) 'epg-signature)
352 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
353 (aref (cdr signature) 1))
355 (defun epg-signature-user-id (signature)
356 "Return the user-id of SIGNATURE."
357 (unless (eq (car signature) 'epg-signature)
358 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
359 (aref (cdr signature) 2))
361 (defun epg-signature-validity (signature)
362 "Return the validity of SIGNATURE."
363 (unless (eq (car signature) 'epg-signature)
364 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
365 (aref (cdr signature) 3))
367 (defun epg-signature-fingerprint (signature)
368 "Return the fingerprint of SIGNATURE."
369 (unless (eq (car signature) 'epg-signature)
370 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
371 (aref (cdr signature) 4))
373 (defun epg-signature-set-status (signature status)
374 "Set the status code of SIGNATURE."
375 (unless (eq (car signature) 'epg-signature)
376 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
377 (aset (cdr signature) 0 status))
379 (defun epg-signature-set-key-id (signature key-id)
380 "Set the key-id of SIGNATURE."
381 (unless (eq (car signature) 'epg-signature)
382 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
383 (aset (cdr signature) 1 key-id))
385 (defun epg-signature-set-user-id (signature user-id)
386 "Set the user-id of SIGNATURE."
387 (unless (eq (car signature) 'epg-signature)
388 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
389 (aset (cdr signature) 2 user-id))
391 (defun epg-signature-set-validity (signature validity)
392 "Set the validity of SIGNATURE."
393 (unless (eq (car signature) 'epg-signature)
394 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
395 (aset (cdr signature) 3 validity))
397 (defun epg-signature-set-fingerprint (signature fingerprint)
398 "Set the fingerprint of SIGNATURE."
399 (unless (eq (car signature) 'epg-signature)
400 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
401 (aset (cdr signature) 4 fingerprint))
403 (defun epg-make-key (owner-trust)
404 "Return a key object."
405 (cons 'epg-key (vector owner-trust nil nil)))
407 (defun epg-key-owner-trust (key)
408 "Return the owner trust of KEY."
409 (unless (eq (car key) 'epg-key)
410 (signal 'wrong-type-argument (list 'epg-key-p key)))
413 (defun epg-key-sub-key-list (key)
414 "Return the sub key list of KEY."
415 (unless (eq (car key) 'epg-key)
416 (signal 'wrong-type-argument (list 'epg-key-p key)))
419 (defun epg-key-user-id-list (key)
420 "Return the user ID list of KEY."
421 (unless (eq (car key) 'epg-key)
422 (signal 'wrong-type-argument (list 'epg-key-p key)))
425 (defun epg-key-set-sub-key-list (key sub-key-list)
426 "Set the sub key list of KEY."
427 (unless (eq (car key) 'epg-key)
428 (signal 'wrong-type-argument (list 'epg-key-p key)))
429 (aset (cdr key) 1 sub-key-list))
431 (defun epg-key-set-user-id-list (key user-id-list)
432 "Set the user ID list of KEY."
433 (unless (eq (car key) 'epg-key)
434 (signal 'wrong-type-argument (list 'epg-key-p key)))
435 (aset (cdr key) 2 user-id-list))
437 (defun epg-make-sub-key (validity capability secret algorithm length id
438 creation-time expiration-time)
439 "Return a sub key object."
441 (vector validity capability secret algorithm length id creation-time
442 expiration-time nil)))
444 (defun epg-sub-key-validity (sub-key)
445 "Return the validity of SUB-KEY."
446 (unless (eq (car sub-key) 'epg-sub-key)
447 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
448 (aref (cdr sub-key) 0))
450 (defun epg-sub-key-capability (sub-key)
451 "Return the capability of SUB-KEY."
452 (unless (eq (car sub-key) 'epg-sub-key)
453 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
454 (aref (cdr sub-key) 1))
456 (defun epg-sub-key-secret (sub-key)
457 "Return non-nil if SUB-KEY is a secret key."
458 (unless (eq (car sub-key) 'epg-sub-key)
459 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
460 (aref (cdr sub-key) 2))
462 (defun epg-sub-key-algorithm (sub-key)
463 "Return the algorithm of SUB-KEY."
464 (unless (eq (car sub-key) 'epg-sub-key)
465 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
466 (aref (cdr sub-key) 3))
468 (defun epg-sub-key-length (sub-key)
469 "Return the length of SUB-KEY."
470 (unless (eq (car sub-key) 'epg-sub-key)
471 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
472 (aref (cdr sub-key) 4))
474 (defun epg-sub-key-id (sub-key)
475 "Return the ID of SUB-KEY."
476 (unless (eq (car sub-key) 'epg-sub-key)
477 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
478 (aref (cdr sub-key) 5))
480 (defun epg-sub-key-creation-time (sub-key)
481 "Return the creation time of SUB-KEY."
482 (unless (eq (car sub-key) 'epg-sub-key)
483 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
484 (aref (cdr sub-key) 6))
486 (defun epg-sub-key-expiration-time (sub-key)
487 "Return the expiration time of SUB-KEY."
488 (unless (eq (car sub-key) 'epg-sub-key)
489 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
490 (aref (cdr sub-key) 7))
492 (defun epg-sub-key-fingerprint (sub-key)
493 "Return the fingerprint of SUB-KEY."
494 (unless (eq (car sub-key) 'epg-sub-key)
495 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
496 (aref (cdr sub-key) 8))
498 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
499 "Set the fingerprint of SUB-KEY.
500 This function is for internal use only."
501 (unless (eq (car sub-key) 'epg-sub-key)
502 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
503 (aset (cdr sub-key) 8 fingerprint))
505 (defun epg-make-user-id (validity name)
506 "Return a user ID object."
507 (cons 'epg-user-id (vector validity name nil)))
509 (defun epg-user-id-validity (user-id)
510 "Return the validity of USER-ID."
511 (unless (eq (car user-id) 'epg-user-id)
512 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
513 (aref (cdr user-id) 0))
515 (defun epg-user-id-name (user-id)
516 "Return the name of USER-ID."
517 (unless (eq (car user-id) 'epg-user-id)
518 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
519 (aref (cdr user-id) 1))
521 (defun epg-user-id-signature-list (user-id)
522 "Return the signature list of USER-ID."
523 (unless (eq (car user-id) 'epg-user-id)
524 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
525 (aref (cdr user-id) 2))
527 (defun epg-user-id-set-signature-list (user-id signature-list)
528 "Set the signature list of USER-ID."
529 (unless (eq (car user-id) 'epg-user-id)
530 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
531 (aset (cdr user-id) 2 signature-list))
533 (defun epg-context-result-for (context name)
534 (cdr (assq name (epg-context-result context))))
536 (defun epg-context-set-result-for (context name value)
537 (let* ((result (epg-context-result context))
538 (entry (assq name result)))
541 (epg-context-set-result context (cons (cons name value) result)))))
543 (defun epg-signature-to-string (signature)
544 (format "%s signature from %s %s%s"
545 (capitalize (symbol-name (epg-signature-status signature)))
546 (epg-signature-key-id signature)
547 (epg-signature-user-id signature)
548 (if (epg-signature-validity signature)
549 (format " (trust %s)"
550 (epg-signature-validity signature))
553 (defun epg-verify-result-to-string (verify-result)
554 (mapconcat #'epg-signature-to-string verify-result "\n"))
556 (defun epg-start (context args)
557 "Start `epg-gpg-program' in a subprocess with given ARGS."
558 (let* ((args (append (list "--no-tty"
561 (unless (eq (epg-context-protocol context) 'CMS)
562 (list "--command-fd" "0"))
563 (if (epg-context-armor context) '("--armor"))
564 (if (epg-context-textmode context) '("--textmode"))
565 (if (epg-context-output-file context)
566 (list "--output" (epg-context-output-file context)))
568 (coding-system-for-write 'binary)
569 process-connection-type
570 (orig-mode (default-file-modes))
571 (buffer (generate-new-buffer " *epg*"))
575 (unless epg-debug-buffer
576 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
577 (set-buffer epg-debug-buffer)
578 (goto-char (point-max))
579 (insert (format "%s %s\n"
580 (if (eq (epg-context-protocol context) 'CMS)
583 (mapconcat #'identity args " ")))))
584 (with-current-buffer buffer
585 (make-local-variable 'epg-read-point)
586 (setq epg-read-point (point-min))
587 (make-local-variable 'epg-pending-status-list)
588 (setq epg-pending-status-list nil)
589 (make-local-variable 'epg-key-id)
590 (setq epg-key-id nil)
591 (make-local-variable 'epg-context)
592 (setq epg-context context))
595 (set-default-file-modes 448)
597 (apply #'start-process "epg" buffer
598 (if (eq (epg-context-protocol context) 'CMS)
602 (set-default-file-modes orig-mode))
603 (set-process-filter process #'epg-process-filter)
604 (set-process-sentinel process #'epg-process-sentinel)
605 (epg-context-set-process context process)))
607 (defun epg-process-filter (process input)
610 (unless epg-debug-buffer
611 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
612 (set-buffer epg-debug-buffer)
613 (goto-char (point-max))
615 (if (buffer-live-p (process-buffer process))
617 (set-buffer (process-buffer process))
618 (goto-char (point-max))
620 (goto-char epg-read-point)
622 (while (looking-at ".*\n") ;the input line finished
624 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
625 (let* ((status (match-string 1))
626 (string (match-string 2))
627 (symbol (intern-soft (concat "epg-status-" status))))
628 (if (member status epg-pending-status-list)
629 (setq epg-pending-status-list nil))
632 (funcall symbol process string)))))
634 (setq epg-read-point (point)))))
636 (defun epg-process-sentinel (process status)
637 (if (and (buffer-live-p (process-buffer process))
638 (not (equal status "finished\n")))
640 (set-buffer (process-buffer process))
641 ;; gpg process exited abnormally, but we have not received an
642 ;; error response from it. Set it here.
643 (unless (epg-context-result-for epg-context 'error)
644 (if (string-match "\\`exited abnormally with code \\(.*\\)\n" status)
645 (epg-context-set-result-for
647 (list (cons 'exit (string-to-number (match-string 1 status)))))
648 (epg-context-set-result-for epg-context 'error
649 (list (cons 'signal status))))))))
651 (defun epg-read-output (context)
653 (if (fboundp 'set-buffer-multibyte)
654 (set-buffer-multibyte nil))
655 (if (file-exists-p (epg-context-output-file context))
656 (let ((coding-system-for-read (if (epg-context-textmode context)
659 (insert-file-contents (epg-context-output-file context))
662 (defun epg-wait-for-status (context status-list)
663 (with-current-buffer (process-buffer (epg-context-process context))
664 (setq epg-pending-status-list status-list)
665 (while (and (eq (process-status (epg-context-process context)) 'run)
666 epg-pending-status-list)
667 (accept-process-output (epg-context-process context) 1))))
669 (defun epg-wait-for-completion (context)
670 (while (eq (process-status (epg-context-process context)) 'run)
671 ;; We can't use accept-process-output instead of sit-for here
672 ;; because it may cause an interrupt during the sentinel execution.
675 (defun epg-flush (context)
676 (if (eq (process-status (epg-context-process context)) 'run)
677 (process-send-eof (epg-context-process context))))
679 (defun epg-reset (context)
680 (if (and (epg-context-process context)
681 (buffer-live-p (process-buffer (epg-context-process context))))
682 (kill-buffer (process-buffer (epg-context-process context))))
683 (epg-context-set-process context nil))
685 (defun epg-delete-output-file (context)
686 (if (and (epg-context-output-file context)
687 (file-exists-p (epg-context-output-file context)))
688 (delete-file (epg-context-output-file context))))
690 (defun epg-status-USERID_HINT (process string)
691 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
692 (let* ((key-id (match-string 1 string))
693 (user-id (match-string 2 string))
694 (entry (assoc key-id epg-user-id-alist)))
696 (setcdr entry user-id)
697 (setq epg-user-id-alist (cons (cons key-id user-id)
698 epg-user-id-alist))))))
700 (defun epg-status-NEED_PASSPHRASE (process string)
701 (if (string-match "\\`\\([^ ]+\\)" string)
702 (setq epg-key-id (match-string 1 string))))
704 (defun epg-status-NEED_PASSPHRASE_SYM (process string)
705 (setq epg-key-id 'SYM))
707 (defun epg-status-NEED_PASSPHRASE_PIN (process string)
708 (setq epg-key-id 'PIN))
710 (defun epg-status-GET_HIDDEN (process string)
712 (string-match "\\`passphrase\\." string))
715 passphrase-with-new-line)
721 (if (consp (epg-context-passphrase-callback
723 (car (epg-context-passphrase-callback
725 (epg-context-passphrase-callback epg-context))
728 (if (consp (epg-context-passphrase-callback
730 (cdr (epg-context-passphrase-callback
733 (setq passphrase-with-new-line (concat passphrase "\n"))
734 (fillarray passphrase 0)
735 (setq passphrase nil)
736 (process-send-string process passphrase-with-new-line)))
738 (epg-context-set-result-for
741 (epg-context-result-for epg-context 'error)))
742 (delete-process process)))
744 (fillarray passphrase 0))
745 (if passphrase-with-new-line
746 (fillarray passphrase-with-new-line 0))))))
748 (defun epg-status-GET_BOOL (process string)
749 (let ((entry (assoc string epg-prompt-alist))
752 (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
753 (process-send-string process "y\n")
754 (process-send-string process "n\n"))
756 (epg-context-set-result-for
759 (epg-context-result-for epg-context 'error)))
760 (delete-process process)))))
762 (defun epg-status-GET_LINE (process string)
763 (let ((entry (assoc string epg-prompt-alist))
768 (concat (read-string (if entry (cdr entry) (concat string ": ")))
771 (epg-context-set-result-for
774 (epg-context-result-for epg-context 'error)))
775 (delete-process process)))))
777 (defun epg-status-GOODSIG (process string)
778 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
779 (epg-context-set-result-for
782 (cons (epg-make-signature
784 (match-string 1 string)
785 (if (eq (epg-context-protocol epg-context) 'CMS)
787 (epg-dn-from-string (match-string 2 string))
788 (error (match-string 2 string)))
789 (match-string 2 string)))
790 (epg-context-result-for epg-context 'verify)))))
792 (defun epg-status-EXPSIG (process string)
793 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
794 (epg-context-set-result-for
797 (cons (epg-make-signature
799 (match-string 1 string)
800 (if (eq (epg-context-protocol epg-context) 'CMS)
802 (epg-dn-from-string (match-string 2 string))
803 (error (match-string 2 string)))
804 (match-string 2 string)))
805 (epg-context-result-for epg-context 'verify)))))
807 (defun epg-status-EXPKEYSIG (process string)
808 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
809 (epg-context-set-result-for
812 (cons (epg-make-signature
814 (match-string 1 string)
815 (if (eq (epg-context-protocol epg-context) 'CMS)
817 (epg-dn-from-string (match-string 2 string))
818 (error (match-string 2 string)))
819 (match-string 2 string)))
820 (epg-context-result-for epg-context 'verify)))))
822 (defun epg-status-REVKEYSIG (process string)
823 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
824 (epg-context-set-result-for
827 (cons (epg-make-signature
829 (match-string 1 string)
830 (if (eq (epg-context-protocol epg-context) 'CMS)
832 (epg-dn-from-string (match-string 2 string))
833 (error (match-string 2 string)))
834 (match-string 2 string)))
835 (epg-context-result-for epg-context 'verify)))))
837 (defun epg-status-BADSIG (process string)
838 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
839 (epg-context-set-result-for
842 (cons (epg-make-signature
844 (match-string 1 string)
845 (if (eq (epg-context-protocol epg-context) 'CMS)
847 (epg-dn-from-string (match-string 2 string))
848 (error (match-string 2 string)))
849 (match-string 2 string)))
850 (epg-context-result-for epg-context 'verify)))))
852 (defun epg-status-VALIDSIG (process string)
853 (let ((signature (car (epg-context-result-for epg-context 'verify))))
855 (eq (epg-signature-status signature) 'good)
856 (string-match "\\`\\([^ ]+\\) " string))
857 (epg-signature-set-fingerprint signature (match-string 1 string)))))
859 (defun epg-status-TRUST_UNDEFINED (process string)
860 (let ((signature (car (epg-context-result-for epg-context 'verify))))
862 (eq (epg-signature-status signature) 'good))
863 (epg-signature-set-validity signature 'undefined))))
865 (defun epg-status-TRUST_NEVER (process string)
866 (let ((signature (car (epg-context-result-for epg-context 'verify))))
868 (eq (epg-signature-status signature) 'good))
869 (epg-signature-set-validity signature 'never))))
871 (defun epg-status-TRUST_MARGINAL (process string)
872 (let ((signature (car (epg-context-result-for epg-context 'verify))))
874 (eq (epg-signature-status signature) 'marginal))
875 (epg-signature-set-validity signature 'marginal))))
877 (defun epg-status-TRUST_FULLY (process string)
878 (let ((signature (car (epg-context-result-for epg-context 'verify))))
880 (eq (epg-signature-status signature) 'good))
881 (epg-signature-set-validity signature 'full))))
883 (defun epg-status-TRUST_ULTIMATE (process string)
884 (let ((signature (car (epg-context-result-for epg-context 'verify))))
886 (eq (epg-signature-status signature) 'good))
887 (epg-signature-set-validity signature 'ultimate))))
889 (defun epg-status-NO_PUBKEY (process string)
890 (epg-context-set-result-for
892 (cons (cons 'no-pubkey string)
893 (epg-context-result-for epg-context 'error))))
895 (defun epg-status-NO_SECKEY (process string)
896 (epg-context-set-result-for
898 (cons (cons 'no-seckey string)
899 (epg-context-result-for epg-context 'error))))
901 (defun epg-status-PROGRESS (process string)
902 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
904 (funcall (if (consp (epg-context-progress-callback epg-context))
905 (car (epg-context-progress-callback epg-context))
906 (epg-context-progress-callback epg-context))
908 (match-string 1 string)
909 (match-string 2 string)
910 (string-to-number (match-string 3 string))
911 (string-to-number (match-string 4 string))
912 (if (consp (epg-context-progress-callback epg-context))
913 (cdr (epg-context-progress-callback epg-context))))))
915 (defun epg-status-DECRYPTION_FAILED (process string)
916 (epg-context-set-result-for
918 (cons 'decryption-failed
919 (epg-context-result-for epg-context 'error))))
921 (defun epg-status-NODATA (process string)
922 (epg-context-set-result-for
924 (cons (cons 'no-data (string-to-number string))
925 (epg-context-result-for epg-context 'error))))
927 (defun epg-status-UNEXPECTED (process string)
928 (epg-context-set-result-for
930 (cons (cons 'unexpected (string-to-number string))
931 (epg-context-result-for epg-context 'error))))
933 (defun epg-status-KEYEXPIRED (process string)
934 (epg-context-set-result-for
936 (cons (cons 'key-expired string)
937 (epg-context-result-for epg-context 'error))))
939 (defun epg-status-KEYREVOKED (process string)
940 (epg-context-set-result-for
943 (epg-context-result-for epg-context 'error))))
945 (defun epg-status-BADARMOR (process string)
946 (epg-context-set-result-for
949 (epg-context-result-for epg-context 'error))))
951 (defun epg-status-INV_RECP (process string)
952 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
953 (epg-context-set-result-for
955 (cons (list 'invalid-recipient
956 (string-to-number (match-string 1 string))
957 (match-string 2 string))
958 (epg-context-result-for epg-context 'error)))))
960 (defun epg-status-NO_RECP (process string)
961 (epg-context-set-result-for
964 (epg-context-result-for epg-context 'error))))
966 (defun epg-status-DELETE_PROBLEM (process string)
967 (if (string-match "\\`\\([0-9]+\\)" string)
968 (epg-context-set-result-for
970 (cons (cons 'delete-problem (string-to-number (match-string 1 string)))
971 (epg-context-result-for epg-context 'error)))))
973 (defun epg-status-SIG_CREATED (process string)
974 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
975 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
976 (epg-context-set-result-for
978 (cons (list (cons 'type (string-to-char (match-string 1 string)))
979 (cons 'pubkey-algorithm
980 (string-to-number (match-string 2 string)))
981 (cons 'digest-algorithm
982 (string-to-number (match-string 3 string)))
983 (cons 'class (string-to-number (match-string 4 string) 16))
984 (cons 'creation-time (match-string 5 string))
985 (cons 'fingerprint (substring string (match-end 0))))
986 (epg-context-result-for epg-context 'sign)))))
988 (defun epg-passphrase-callback-function (context key-id handback)
991 "Passphrase for symmetric encryption: "
993 "Passphrase for PIN: "
994 (let ((entry (assoc key-id epg-user-id-alist)))
996 (format "Passphrase for %s %s: " key-id (cdr entry))
997 (format "Passphrase for %s: " key-id)))))))
999 (defun epg-progress-callback-function (context what char current total
1001 (message "%s: %d%%/%d%%" what current total))
1003 (defun epg-configuration ()
1004 "Return a list of internal configuration parameters of `epg-gpg-program'."
1007 (apply #'call-process epg-gpg-program nil (list t nil) nil
1008 '("--with-colons" "--list-config"))
1009 (goto-char (point-min))
1010 (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
1011 (setq type (intern (match-string 1))
1012 config (cons (cons type
1014 '(pubkey cipher digest compress))
1015 (mapcar #'string-to-number
1016 (delete "" (split-string
1023 (defun epg-list-keys-1 (context name mode)
1024 (let ((args (append (list "--with-colons" "--no-greeting" "--batch"
1025 "--with-fingerprint"
1026 "--with-fingerprint"
1027 (if (or (eq mode t) (eq mode 'secret))
1028 "--list-secret-keys"
1032 (unless (eq (epg-context-protocol context) 'CMS)
1033 '("--fixed-list-mode"))
1034 (if name (list name))))
1035 keys string field index)
1037 (apply #'call-process
1038 (if (eq (epg-context-protocol context) 'CMS)
1041 nil (list t nil) nil args)
1042 (goto-char (point-min))
1043 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1044 (setq keys (cons (make-vector 15 nil) keys)
1045 string (match-string 0)
1049 (string-match "\\([^:]+\\)?:" string index))
1050 (setq index (match-end 0))
1051 (aset (car keys) field (match-string 1 string))
1052 (setq field (1+ field))))
1055 (defun epg-make-sub-key-1 (line)
1058 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1060 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1062 (member (aref line 0) '("sec" "ssb"))
1063 (string-to-number (aref line 3))
1064 (string-to-number (aref line 2))
1069 (defun epg-list-keys (context &optional name mode)
1070 "Return a list of epg-key objects matched with NAME.
1071 If MODE is nil, only public keyring should be searched.
1072 If MODE is t or 'secret, only secret keyring should be searched.
1073 Otherwise, only public keyring should be searched and the key
1074 signatures should be included."
1075 (let ((lines (epg-list-keys-1 context name mode))
1079 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1081 (epg-key-set-sub-key-list
1083 (nreverse (epg-key-sub-key-list (car keys))))
1084 (epg-key-set-user-id-list
1086 (nreverse (epg-key-user-id-list (car keys)))))
1087 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1088 keys (cons (epg-make-key
1089 (if (aref (car lines) 8)
1090 (cdr (assq (string-to-char (aref (car lines) 8))
1091 epg-key-validity-alist))))
1093 (epg-key-set-sub-key-list
1095 (cons (epg-make-sub-key-1 (car lines))
1096 (epg-key-sub-key-list (car keys)))))
1097 ((member (aref (car lines) 0) '("sub" "ssb"))
1098 (epg-key-set-sub-key-list
1100 (cons (epg-make-sub-key-1 (car lines))
1101 (epg-key-sub-key-list (car keys)))))
1102 ((equal (aref (car lines) 0) "uid")
1103 (epg-key-set-user-id-list
1105 (cons (epg-make-user-id
1106 (if (aref (car lines) 1)
1107 (cdr (assq (string-to-char (aref (car lines) 1))
1108 epg-key-validity-alist)))
1111 (epg-dn-from-string (aref (car lines) 9))
1112 (error (aref (car lines) 9)))
1113 (aref (car lines) 9)))
1114 (epg-key-user-id-list (car keys)))))
1115 ((equal (aref (car lines) 0) "fpr")
1116 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1117 (aref (car lines) 9))))
1118 (setq lines (cdr lines)))
1120 (epg-key-set-sub-key-list
1122 (nreverse (epg-key-sub-key-list (car keys))))
1123 (epg-key-set-user-id-list
1125 (nreverse (epg-key-user-id-list (car keys)))))
1128 (if (fboundp 'make-temp-file)
1129 (defalias 'epg-make-temp-file 'make-temp-file)
1130 ;; stolen from poe.el.
1131 (defun epg-make-temp-file (prefix)
1132 "Create a temporary file.
1133 The returned file name (created by appending some random characters at the end
1134 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1135 is guaranteed to point to a newly created empty file.
1136 You can then use `write-region' to write new data into the file."
1137 (let (tempdir tempfile)
1140 ;; First, create a temporary directory.
1141 (while (condition-case ()
1143 (setq tempdir (make-temp-name
1145 (file-name-directory prefix)
1147 ;; return nil or signal an error.
1148 (make-directory tempdir))
1150 (file-already-exists t)))
1151 (set-file-modes tempdir 448)
1152 ;; Second, create a temporary file in the tempdir.
1153 ;; There *is* a race condition between `make-temp-name'
1154 ;; and `write-region', but we don't care it since we are
1155 ;; in a private directory now.
1156 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1157 (write-region "" nil tempfile nil 'silent)
1158 (set-file-modes tempfile 384)
1159 ;; Finally, make a hard-link from the tempfile.
1160 (while (condition-case ()
1162 (setq file (make-temp-name prefix))
1163 ;; return nil or signal an error.
1164 (add-name-to-file tempfile file))
1166 (file-already-exists t)))
1168 ;; Cleanup the tempfile.
1170 (file-exists-p tempfile)
1171 (delete-file tempfile))
1172 ;; Cleanup the tempdir.
1174 (file-directory-p tempdir)
1175 (delete-directory tempdir))))))
1178 (defun epg-cancel (context)
1179 (if (eq (process-status (epg-context-process context)) 'run)
1180 (delete-process (epg-context-process context))))
1183 (defun epg-start-decrypt (context cipher)
1184 "Initiate a decrypt operation on CIPHER.
1185 CIPHER is a data object.
1187 If you use this function, you will need to wait for the completion of
1188 `epg-gpg-program' by using `epg-wait-for-completion' and call
1189 `epg-reset' to clear a temporaly output file.
1190 If you are unsure, use synchronous version of this function
1191 `epg-decrypt-file' or `epg-decrypt-string' instead."
1192 (unless (epg-data-file cipher)
1193 (error "Not a file"))
1194 (epg-context-set-result context nil)
1195 (epg-start context (list "--decrypt" (epg-data-file cipher)))
1196 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1197 (unless (eq (epg-context-protocol context) 'CMS)
1198 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1201 (defun epg-decrypt-file (context cipher plain)
1202 "Decrypt a file CIPHER and store the result to a file PLAIN.
1203 If PLAIN is nil, it returns the result as a string."
1207 (epg-context-set-output-file context plain)
1208 (epg-context-set-output-file context
1209 (epg-make-temp-file "epg-output")))
1210 (epg-start-decrypt context (epg-make-data-from-file cipher))
1211 (epg-wait-for-completion context)
1212 (if (epg-context-result-for context 'error)
1213 (error "Decrypt failed: %S"
1214 (epg-context-result-for context 'error)))
1216 (epg-read-output context)))
1218 (epg-delete-output-file context))
1219 (epg-reset context)))
1222 (defun epg-decrypt-string (context cipher)
1223 "Decrypt a string CIPHER and return the plain text."
1224 (let ((input-file (epg-make-temp-file "epg-input"))
1225 (coding-system-for-write 'binary))
1228 (write-region cipher nil input-file nil 'quiet)
1229 (epg-context-set-output-file context
1230 (epg-make-temp-file "epg-output"))
1231 (epg-start-decrypt context (epg-make-data-from-file input-file))
1233 (epg-wait-for-completion context)
1234 (if (epg-context-result-for context 'error)
1235 (error "Decrypt failed: %S"
1236 (epg-context-result-for context 'error)))
1237 (epg-read-output context))
1238 (epg-delete-output-file context)
1239 (if (file-exists-p input-file)
1240 (delete-file input-file))
1241 (epg-reset context))))
1244 (defun epg-start-verify (context signature &optional signed-text)
1245 "Initiate a verify operation on SIGNATURE.
1246 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1248 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1249 For a normal or a clear text signature, SIGNED-TEXT should be nil.
1251 If you use this function, you will need to wait for the completion of
1252 `epg-gpg-program' by using `epg-wait-for-completion' and call
1253 `epg-reset' to clear a temporaly output file.
1254 If you are unsure, use synchronous version of this function
1255 `epg-verify-file' or `epg-verify-string' instead."
1256 (epg-context-set-result context nil)
1258 ;; Detached signature.
1259 (if (epg-data-file signed-text)
1260 (epg-start context (list "--verify" (epg-data-file signature)
1261 (epg-data-file signed-text)))
1262 (epg-start context (list "--verify" (epg-data-file signature) "-"))
1263 (if (eq (process-status (epg-context-process context)) 'run)
1264 (process-send-string (epg-context-process context)
1265 (epg-data-string signed-text))))
1266 ;; Normal (or cleartext) signature.
1267 (if (epg-data-file signature)
1268 (epg-start context (list "--verify" (epg-data-file signature)))
1269 (epg-start context (list "--verify"))
1270 (if (eq (process-status (epg-context-process context)) 'run)
1271 (process-send-string (epg-context-process context)
1272 (epg-data-string signature))))))
1275 (defun epg-verify-file (context signature &optional signed-text plain)
1276 "Verify a file SIGNATURE.
1277 SIGNED-TEXT and PLAIN are also a file if they are specified.
1279 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1280 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1284 (epg-context-set-output-file context plain)
1285 (epg-context-set-output-file context
1286 (epg-make-temp-file "epg-output")))
1288 (epg-start-verify context
1289 (epg-make-data-from-file signature)
1290 (epg-make-data-from-file signed-text))
1291 (epg-start-verify context
1292 (epg-make-data-from-file signature)))
1293 (epg-wait-for-completion context)
1295 (epg-read-output context)))
1297 (epg-delete-output-file context))
1298 (epg-reset context)))
1301 (defun epg-verify-string (context signature &optional signed-text)
1302 "Verify a string SIGNATURE.
1303 SIGNED-TEXT is a string if it is specified.
1305 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1306 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1307 (let ((coding-system-for-write 'binary)
1311 (epg-context-set-output-file context
1312 (epg-make-temp-file "epg-output"))
1315 (setq input-file (epg-make-temp-file "epg-signature"))
1316 (write-region signature nil input-file nil 'quiet)
1317 (epg-start-verify context
1318 (epg-make-data-from-file input-file)
1319 (epg-make-data-from-string signed-text)))
1320 (epg-start-verify context (epg-make-data-from-string signature)))
1322 (epg-wait-for-completion context)
1323 (epg-read-output context))
1324 (epg-delete-output-file context)
1326 (file-exists-p input-file))
1327 (delete-file input-file))
1328 (epg-reset context))))
1331 (defun epg-start-sign (context plain &optional mode)
1332 "Initiate a sign operation on PLAIN.
1333 PLAIN is a data object.
1335 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1336 If MODE is t or 'detached, it makes a detached signature.
1337 Otherwise, it makes a normal signature.
1339 If you use this function, you will need to wait for the completion of
1340 `epg-gpg-program' by using `epg-wait-for-completion' and call
1341 `epg-reset' to clear a temporaly output file.
1342 If you are unsure, use synchronous version of this function
1343 `epg-sign-file' or `epg-sign-string' instead."
1344 (epg-context-set-result context nil)
1346 (append (list (if (eq mode 'clearsign)
1348 (if (or (eq mode t) (eq mode 'detached))
1356 (car (epg-key-sub-key-list signer)))))
1357 (epg-context-signers context)))
1358 (if (epg-data-file plain)
1359 (list (epg-data-file plain)))))
1360 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1361 (unless (eq (epg-context-protocol context) 'CMS)
1362 (epg-wait-for-status context '("BEGIN_SIGNING")))
1363 (if (and (epg-data-string plain)
1364 (eq (process-status (epg-context-process context)) 'run))
1365 (process-send-string (epg-context-process context)
1366 (epg-data-string plain))))
1369 (defun epg-sign-file (context plain signature &optional mode)
1370 "Sign a file PLAIN and store the result to a file SIGNATURE.
1371 If SIGNATURE is nil, it returns the result as a string.
1372 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1373 If MODE is t or 'detached, it makes a detached signature.
1374 Otherwise, it makes a normal signature."
1378 (epg-context-set-output-file context signature)
1379 (epg-context-set-output-file context
1380 (epg-make-temp-file "epg-output")))
1381 (epg-start-sign context (epg-make-data-from-file plain) mode)
1382 (epg-wait-for-completion context)
1383 (if (epg-context-result-for context 'sign)
1384 (if (epg-context-result-for context 'error)
1385 (message "Sign warning: %S"
1386 (epg-context-result-for context 'error)))
1387 (if (epg-context-result-for context 'error)
1388 (error "Sign failed: %S"
1389 (epg-context-result-for context 'error))
1390 (error "Sign failed")))
1392 (epg-read-output context)))
1394 (epg-delete-output-file context))
1395 (epg-reset context)))
1398 (defun epg-sign-string (context plain &optional mode)
1399 "Sign a string PLAIN and return the output as string.
1400 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1401 If MODE is t or 'detached, it makes a detached signature.
1402 Otherwise, it makes a normal signature."
1405 (epg-context-set-output-file context
1406 (epg-make-temp-file "epg-output"))
1407 (epg-start-sign context (epg-make-data-from-string plain) mode)
1409 (epg-wait-for-completion context)
1410 (if (epg-context-result-for context 'sign)
1411 (if (epg-context-result-for context 'error)
1412 (message "Sign warning: %S"
1413 (epg-context-result-for context 'error)))
1414 (if (epg-context-result-for context 'error)
1415 (error "Sign failed: %S"
1416 (epg-context-result-for context 'error))
1417 (error "Sign failed")))
1418 (epg-read-output context))
1419 (epg-delete-output-file context)
1420 (epg-reset context)))
1423 (defun epg-start-encrypt (context plain recipients
1424 &optional sign always-trust)
1425 "Initiate an encrypt operation on PLAIN.
1426 PLAIN is a data object.
1427 If RECIPIENTS is nil, it performs symmetric encryption.
1429 If you use this function, you will need to wait for the completion of
1430 `epg-gpg-program' by using `epg-wait-for-completion' and call
1431 `epg-reset' to clear a temporaly output file.
1432 If you are unsure, use synchronous version of this function
1433 `epg-encrypt-file' or `epg-encrypt-string' instead."
1434 (epg-context-set-result context nil)
1436 (append (if always-trust '("--always-trust"))
1437 (if recipients '("--encrypt") '("--symmetric"))
1441 (mapcar (lambda (signer)
1443 (epg-context-signers context)))))
1449 (car (epg-key-sub-key-list recipient)))))
1451 (if (epg-data-file plain)
1452 (list (epg-data-file plain)))))
1453 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1454 (unless (eq (epg-context-protocol context) 'CMS)
1456 (epg-wait-for-status context '("BEGIN_SIGNING"))
1457 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
1458 (if (and (epg-data-string plain)
1459 (eq (process-status (epg-context-process context)) 'run))
1460 (process-send-string (epg-context-process context)
1461 (epg-data-string plain))))
1464 (defun epg-encrypt-file (context plain recipients
1465 cipher &optional sign always-trust)
1466 "Encrypt a file PLAIN and store the result to a file CIPHER.
1467 If CIPHER is nil, it returns the result as a string.
1468 If RECIPIENTS is nil, it performs symmetric encryption."
1472 (epg-context-set-output-file context cipher)
1473 (epg-context-set-output-file context
1474 (epg-make-temp-file "epg-output")))
1475 (epg-start-encrypt context (epg-make-data-from-file plain)
1476 recipients sign always-trust)
1477 (epg-wait-for-completion context)
1479 (if (epg-context-result-for context 'sign)
1480 (if (epg-context-result-for context 'error)
1481 (message "Sign warning: %S"
1482 (epg-context-result-for context 'error)))
1483 (if (epg-context-result-for context 'error)
1484 (error "Sign failed: %S"
1485 (epg-context-result-for context 'error))
1486 (error "Sign failed"))))
1487 (if (epg-context-result-for context 'error)
1488 (error "Encrypt failed: %S"
1489 (epg-context-result-for context 'error)))
1491 (epg-read-output context)))
1493 (epg-delete-output-file context))
1494 (epg-reset context)))
1497 (defun epg-encrypt-string (context plain recipients
1498 &optional sign always-trust)
1499 "Encrypt a string PLAIN.
1500 If RECIPIENTS is nil, it performs symmetric encryption."
1503 (epg-context-set-output-file context
1504 (epg-make-temp-file "epg-output"))
1505 (epg-start-encrypt context (epg-make-data-from-string plain)
1506 recipients sign always-trust)
1508 (epg-wait-for-completion context)
1510 (if (epg-context-result-for context 'sign)
1511 (if (epg-context-result-for context 'error)
1512 (message "Sign warning: %S"
1513 (epg-context-result-for context 'error)))
1514 (if (epg-context-result-for context 'error)
1515 (error "Sign failed: %S"
1516 (epg-context-result-for context 'error))
1517 (error "Sign failed"))))
1518 (if (epg-context-result-for context 'error)
1519 (error "Encrypt failed: %S"
1520 (epg-context-result-for context 'error)))
1521 (epg-read-output context))
1522 (epg-delete-output-file context)
1523 (epg-reset context)))
1526 (defun epg-start-export-keys (context keys)
1527 "Initiate an export keys operation.
1529 If you use this function, you will need to wait for the completion of
1530 `epg-gpg-program' by using `epg-wait-for-completion' and call
1531 `epg-reset' to clear a temporaly output file.
1532 If you are unsure, use synchronous version of this function
1533 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1534 (epg-context-set-result context nil)
1535 (epg-start context (cons "--export"
1539 (car (epg-key-sub-key-list key))))
1543 (defun epg-export-keys-to-file (context keys file)
1544 "Extract public KEYS."
1548 (epg-context-set-output-file context file)
1549 (epg-context-set-output-file context
1550 (epg-make-temp-file "epg-output")))
1551 (epg-start-export-keys context keys)
1552 (epg-wait-for-completion context)
1553 (if (epg-context-result-for context 'error)
1554 (error "Export keys failed: %S"
1555 (epg-context-result-for context 'error)))
1557 (epg-read-output context)))
1559 (epg-delete-output-file context))
1560 (epg-reset context)))
1563 (defun epg-export-keys-to-string (context keys)
1564 "Extract public KEYS and return them as a string."
1565 (epg-export-keys-to-file context keys nil))
1568 (defun epg-start-import-keys (context keys)
1569 "Initiate an import keys operation.
1570 KEYS is a data object.
1572 If you use this function, you will need to wait for the completion of
1573 `epg-gpg-program' by using `epg-wait-for-completion' and call
1574 `epg-reset' to clear a temporaly output file.
1575 If you are unsure, use synchronous version of this function
1576 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1577 (epg-context-set-result context nil)
1578 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
1579 (epg-start context (list "--import" (epg-data-file keys)))
1580 (if (and (epg-data-string keys)
1581 (eq (process-status (epg-context-process context)) 'run))
1582 (process-send-string (epg-context-process context)
1583 (epg-data-string keys))))
1585 (defun epg-import-keys-1 (context keys)
1588 (epg-start-import-keys context keys)
1589 (if (epg-data-file keys)
1590 (epg-flush context))
1591 (epg-wait-for-completion context)
1592 (if (epg-context-result-for context 'error)
1593 (error "Import keys failed: %S"
1594 (epg-context-result-for context 'error)))
1595 (epg-read-output context))
1596 (epg-reset context)))
1599 (defun epg-import-keys-from-file (context keys)
1600 "Add keys from a file KEYS."
1601 (epg-import-keys-1 context (epg-make-data-from-file keys)))
1604 (defun epg-import-keys-from-string (context keys)
1605 "Add keys from a string KEYS."
1606 (epg-import-keys-1 context (epg-make-data-from-string keys)))
1609 (defun epg-start-delete-keys (context keys &optional allow-secret)
1610 "Initiate an delete keys operation.
1612 If you use this function, you will need to wait for the completion of
1613 `epg-gpg-program' by using `epg-wait-for-completion' and call
1614 `epg-reset' to clear a temporaly output file.
1615 If you are unsure, use synchronous version of this function
1616 `epg-delete-keys' instead."
1617 (epg-context-set-result context nil)
1618 (epg-start context (cons (if allow-secret
1619 "--delete-secret-key"
1624 (car (epg-key-sub-key-list key))))
1628 (defun epg-delete-keys (context keys &optional allow-secret)
1629 "Delete KEYS from the key ring."
1632 (epg-start-delete-keys context keys allow-secret)
1633 (epg-wait-for-completion context)
1634 (if (epg-context-result-for context 'error)
1635 (error "Delete keys failed: %S"
1636 (epg-context-result-for context 'error))))
1637 (epg-reset context)))
1640 (defun epg-start-sign-keys (context keys &optional local)
1641 "Initiate an sign keys operation.
1643 If you use this function, you will need to wait for the completion of
1644 `epg-gpg-program' by using `epg-wait-for-completion' and call
1645 `epg-reset' to clear a temporaly output file.
1646 If you are unsure, use synchronous version of this function
1647 `epg-sign-keys' instead."
1648 (epg-context-set-result context nil)
1649 (epg-start context (cons (if local
1655 (car (epg-key-sub-key-list key))))
1659 (defun epg-sign-keys (context keys &optional local)
1660 "Sign KEYS from the key ring."
1663 (epg-start-sign-keys context keys local)
1664 (epg-wait-for-completion context)
1665 (if (epg-context-result-for context 'error)
1666 (error "Sign keys failed: %S"
1667 (epg-context-result-for context 'error))))
1668 (epg-reset context)))
1670 (defun epg-decode-hexstring (string)
1672 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
1673 (setq string (replace-match "\\x\\&" t nil string)
1675 (car (read-from-string (concat "\"" string "\"")))))
1677 (defun epg-decode-quotedstring (string)
1679 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
1680 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
1682 (if (match-beginning 2)
1683 (setq string (replace-match "\\2" t nil string)
1685 (if (match-beginning 3)
1686 (setq string (replace-match "\\x\\3" t nil string)
1688 (setq string (replace-match "\\\\\\\\\\4" t nil string)
1689 index (+ index 3)))))
1690 (car (read-from-string (concat "\"" string "\"")))))
1692 (defun epg-dn-from-string (string)
1693 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
1694 The return value is an alist mapping from types to values."
1696 (length (length string))
1697 alist type value group)
1698 (while (< index length)
1699 (if (eq index (string-match "[ \t\n\r]*" string index))
1700 (setq index (match-end 0)))
1701 (if (eq index (string-match
1702 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
1704 (setq type (match-string 1 string)
1705 index (match-end 0))
1706 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
1708 (setq type (match-string 1 string)
1709 index (match-end 0))))
1711 (error "Invalid type"))
1712 (if (eq index (string-match
1713 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
1715 (setq index (match-end 0)
1716 value (epg-decode-quotedstring (match-string 0 string)))
1717 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
1718 (setq index (match-end 0)
1719 value (epg-decode-hexstring (match-string 1 string)))
1720 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
1722 (setq index (match-end 0)
1723 value (epg-decode-quotedstring (match-string 0 string))))))
1725 (if (stringp (car (car alist)))
1726 (setcar alist (list (cons type value) (car alist)))
1727 (setcar alist (cons (cons type value) (car alist))))
1728 (if (consp (car (car alist)))
1729 (setcar alist (nreverse (car alist))))
1730 (setq alist (cons (cons type value) alist)
1733 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
1734 (setq index (match-end 0)
1735 group (eq (aref string (match-beginning 1)) ?+))))
1738 (defun epg-decode-dn (alist)
1739 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
1740 Type names are resolved using `epg-dn-type-alist'."
1743 (if (stringp (car rdn))
1744 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
1746 (format "%s=%s" (cdr entry) (cdr rdn))
1747 (format "%s=%s" (car rdn) (cdr rdn))))
1748 (concat "(" (epg-decode-dn rdn) ")")))
1754 ;;; epg.el ends here