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-ERRSIG (process string)
853 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
854 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
856 (epg-context-set-result-for
859 (cons (list (cons 'key-id (match-string 1 string))
860 (cons 'pubkey-algorithm
861 (string-to-number (match-string 2 string)))
862 (cons 'digest-algorithm
863 (string-to-number (match-string 3 string)))
865 (string-to-number (match-string 4 string)))
866 (cons 'creation-time (match-string 5 string))
867 (cons 'rc (string-to-number (match-string 6 string))))
868 (epg-context-result-for epg-context 'error)))))
870 (defun epg-status-VALIDSIG (process string)
871 (let ((signature (car (epg-context-result-for epg-context 'verify))))
873 (eq (epg-signature-status signature) 'good)
874 (string-match "\\`\\([^ ]+\\) " string))
875 (epg-signature-set-fingerprint signature (match-string 1 string)))))
877 (defun epg-status-TRUST_UNDEFINED (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 'undefined))))
883 (defun epg-status-TRUST_NEVER (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 'never))))
889 (defun epg-status-TRUST_MARGINAL (process string)
890 (let ((signature (car (epg-context-result-for epg-context 'verify))))
892 (eq (epg-signature-status signature) 'marginal))
893 (epg-signature-set-validity signature 'marginal))))
895 (defun epg-status-TRUST_FULLY (process string)
896 (let ((signature (car (epg-context-result-for epg-context 'verify))))
898 (eq (epg-signature-status signature) 'good))
899 (epg-signature-set-validity signature 'full))))
901 (defun epg-status-TRUST_ULTIMATE (process string)
902 (let ((signature (car (epg-context-result-for epg-context 'verify))))
904 (eq (epg-signature-status signature) 'good))
905 (epg-signature-set-validity signature 'ultimate))))
907 (defun epg-status-PROGRESS (process string)
908 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
910 (funcall (if (consp (epg-context-progress-callback epg-context))
911 (car (epg-context-progress-callback epg-context))
912 (epg-context-progress-callback epg-context))
914 (match-string 1 string)
915 (match-string 2 string)
916 (string-to-number (match-string 3 string))
917 (string-to-number (match-string 4 string))
918 (if (consp (epg-context-progress-callback epg-context))
919 (cdr (epg-context-progress-callback epg-context))))))
921 (defun epg-status-DECRYPTION_FAILED (process string)
922 (epg-context-set-result-for
924 (cons 'decryption-failed
925 (epg-context-result-for epg-context 'error))))
927 (defun epg-status-NODATA (process string)
928 (epg-context-set-result-for
930 (cons (cons 'no-data (string-to-number string))
931 (epg-context-result-for epg-context 'error))))
933 (defun epg-status-UNEXPECTED (process string)
934 (epg-context-set-result-for
936 (cons (cons 'unexpected (string-to-number string))
937 (epg-context-result-for epg-context 'error))))
939 (defun epg-status-KEYEXPIRED (process string)
940 (epg-context-set-result-for
942 (cons (cons 'key-expired string)
943 (epg-context-result-for epg-context 'error))))
945 (defun epg-status-KEYREVOKED (process string)
946 (epg-context-set-result-for
949 (epg-context-result-for epg-context 'error))))
951 (defun epg-status-BADARMOR (process string)
952 (epg-context-set-result-for
955 (epg-context-result-for epg-context 'error))))
957 (defun epg-status-INV_RECP (process string)
958 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
959 (epg-context-set-result-for
961 (cons (list 'invalid-recipient
962 (string-to-number (match-string 1 string))
963 (match-string 2 string))
964 (epg-context-result-for epg-context 'error)))))
966 (defun epg-status-NO_RECP (process string)
967 (epg-context-set-result-for
970 (epg-context-result-for epg-context 'error))))
972 (defun epg-status-DELETE_PROBLEM (process string)
973 (if (string-match "\\`\\([0-9]+\\)" string)
974 (epg-context-set-result-for
976 (cons (cons 'delete-problem (string-to-number (match-string 1 string)))
977 (epg-context-result-for epg-context 'error)))))
979 (defun epg-status-SIG_CREATED (process string)
980 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
981 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
982 (epg-context-set-result-for
984 (cons (list (cons 'type (string-to-char (match-string 1 string)))
985 (cons 'pubkey-algorithm
986 (string-to-number (match-string 2 string)))
987 (cons 'digest-algorithm
988 (string-to-number (match-string 3 string)))
989 (cons 'class (string-to-number (match-string 4 string) 16))
990 (cons 'creation-time (match-string 5 string))
991 (cons 'fingerprint (substring string (match-end 0))))
992 (epg-context-result-for epg-context 'sign)))))
994 (defun epg-passphrase-callback-function (context key-id handback)
997 "Passphrase for symmetric encryption: "
999 "Passphrase for PIN: "
1000 (let ((entry (assoc key-id epg-user-id-alist)))
1002 (format "Passphrase for %s %s: " key-id (cdr entry))
1003 (format "Passphrase for %s: " key-id)))))))
1005 (defun epg-progress-callback-function (context what char current total
1007 (message "%s: %d%%/%d%%" what current total))
1009 (defun epg-configuration ()
1010 "Return a list of internal configuration parameters of `epg-gpg-program'."
1013 (apply #'call-process epg-gpg-program nil (list t nil) nil
1014 '("--with-colons" "--list-config"))
1015 (goto-char (point-min))
1016 (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
1017 (setq type (intern (match-string 1))
1018 config (cons (cons type
1020 '(pubkey cipher digest compress))
1021 (mapcar #'string-to-number
1022 (delete "" (split-string
1029 (defun epg-list-keys-1 (context name mode)
1030 (let ((args (append (list "--with-colons" "--no-greeting" "--batch"
1031 "--with-fingerprint"
1032 "--with-fingerprint"
1033 (if (or (eq mode t) (eq mode 'secret))
1034 "--list-secret-keys"
1038 (unless (eq (epg-context-protocol context) 'CMS)
1039 '("--fixed-list-mode"))
1040 (if name (list name))))
1041 keys string field index)
1043 (apply #'call-process
1044 (if (eq (epg-context-protocol context) 'CMS)
1047 nil (list t nil) nil args)
1048 (goto-char (point-min))
1049 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1050 (setq keys (cons (make-vector 15 nil) keys)
1051 string (match-string 0)
1055 (string-match "\\([^:]+\\)?:" string index))
1056 (setq index (match-end 0))
1057 (aset (car keys) field (match-string 1 string))
1058 (setq field (1+ field))))
1061 (defun epg-make-sub-key-1 (line)
1064 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1066 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1068 (member (aref line 0) '("sec" "ssb"))
1069 (string-to-number (aref line 3))
1070 (string-to-number (aref line 2))
1075 (defun epg-list-keys (context &optional name mode)
1076 "Return a list of epg-key objects matched with NAME.
1077 If MODE is nil, only public keyring should be searched.
1078 If MODE is t or 'secret, only secret keyring should be searched.
1079 Otherwise, only public keyring should be searched and the key
1080 signatures should be included."
1081 (let ((lines (epg-list-keys-1 context name mode))
1085 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1087 (epg-key-set-sub-key-list
1089 (nreverse (epg-key-sub-key-list (car keys))))
1090 (epg-key-set-user-id-list
1092 (nreverse (epg-key-user-id-list (car keys)))))
1093 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1094 keys (cons (epg-make-key
1095 (if (aref (car lines) 8)
1096 (cdr (assq (string-to-char (aref (car lines) 8))
1097 epg-key-validity-alist))))
1099 (epg-key-set-sub-key-list
1101 (cons (epg-make-sub-key-1 (car lines))
1102 (epg-key-sub-key-list (car keys)))))
1103 ((member (aref (car lines) 0) '("sub" "ssb"))
1104 (epg-key-set-sub-key-list
1106 (cons (epg-make-sub-key-1 (car lines))
1107 (epg-key-sub-key-list (car keys)))))
1108 ((equal (aref (car lines) 0) "uid")
1109 (epg-key-set-user-id-list
1111 (cons (epg-make-user-id
1112 (if (aref (car lines) 1)
1113 (cdr (assq (string-to-char (aref (car lines) 1))
1114 epg-key-validity-alist)))
1117 (epg-dn-from-string (aref (car lines) 9))
1118 (error (aref (car lines) 9)))
1119 (aref (car lines) 9)))
1120 (epg-key-user-id-list (car keys)))))
1121 ((equal (aref (car lines) 0) "fpr")
1122 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1123 (aref (car lines) 9))))
1124 (setq lines (cdr lines)))
1126 (epg-key-set-sub-key-list
1128 (nreverse (epg-key-sub-key-list (car keys))))
1129 (epg-key-set-user-id-list
1131 (nreverse (epg-key-user-id-list (car keys)))))
1134 (if (fboundp 'make-temp-file)
1135 (defalias 'epg-make-temp-file 'make-temp-file)
1136 ;; stolen from poe.el.
1137 (defun epg-make-temp-file (prefix)
1138 "Create a temporary file.
1139 The returned file name (created by appending some random characters at the end
1140 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1141 is guaranteed to point to a newly created empty file.
1142 You can then use `write-region' to write new data into the file."
1143 (let (tempdir tempfile)
1146 ;; First, create a temporary directory.
1147 (while (condition-case ()
1149 (setq tempdir (make-temp-name
1151 (file-name-directory prefix)
1153 ;; return nil or signal an error.
1154 (make-directory tempdir))
1156 (file-already-exists t)))
1157 (set-file-modes tempdir 448)
1158 ;; Second, create a temporary file in the tempdir.
1159 ;; There *is* a race condition between `make-temp-name'
1160 ;; and `write-region', but we don't care it since we are
1161 ;; in a private directory now.
1162 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1163 (write-region "" nil tempfile nil 'silent)
1164 (set-file-modes tempfile 384)
1165 ;; Finally, make a hard-link from the tempfile.
1166 (while (condition-case ()
1168 (setq file (make-temp-name prefix))
1169 ;; return nil or signal an error.
1170 (add-name-to-file tempfile file))
1172 (file-already-exists t)))
1174 ;; Cleanup the tempfile.
1176 (file-exists-p tempfile)
1177 (delete-file tempfile))
1178 ;; Cleanup the tempdir.
1180 (file-directory-p tempdir)
1181 (delete-directory tempdir))))))
1184 (defun epg-cancel (context)
1185 (if (eq (process-status (epg-context-process context)) 'run)
1186 (delete-process (epg-context-process context))))
1189 (defun epg-start-decrypt (context cipher)
1190 "Initiate a decrypt operation on CIPHER.
1191 CIPHER is a data object.
1193 If you use this function, you will need to wait for the completion of
1194 `epg-gpg-program' by using `epg-wait-for-completion' and call
1195 `epg-reset' to clear a temporaly output file.
1196 If you are unsure, use synchronous version of this function
1197 `epg-decrypt-file' or `epg-decrypt-string' instead."
1198 (unless (epg-data-file cipher)
1199 (error "Not a file"))
1200 (epg-context-set-result context nil)
1201 (epg-start context (list "--decrypt" (epg-data-file cipher)))
1202 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1203 (unless (eq (epg-context-protocol context) 'CMS)
1204 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1207 (defun epg-decrypt-file (context cipher plain)
1208 "Decrypt a file CIPHER and store the result to a file PLAIN.
1209 If PLAIN is nil, it returns the result as a string."
1213 (epg-context-set-output-file context plain)
1214 (epg-context-set-output-file context
1215 (epg-make-temp-file "epg-output")))
1216 (epg-start-decrypt context (epg-make-data-from-file cipher))
1217 (epg-wait-for-completion context)
1218 (if (epg-context-result-for context 'error)
1219 (error "Decrypt failed: %S"
1220 (epg-context-result-for context 'error)))
1222 (epg-read-output context)))
1224 (epg-delete-output-file context))
1225 (epg-reset context)))
1228 (defun epg-decrypt-string (context cipher)
1229 "Decrypt a string CIPHER and return the plain text."
1230 (let ((input-file (epg-make-temp-file "epg-input"))
1231 (coding-system-for-write 'binary))
1234 (write-region cipher nil input-file nil 'quiet)
1235 (epg-context-set-output-file context
1236 (epg-make-temp-file "epg-output"))
1237 (epg-start-decrypt context (epg-make-data-from-file input-file))
1239 (epg-wait-for-completion context)
1240 (if (epg-context-result-for context 'error)
1241 (error "Decrypt failed: %S"
1242 (epg-context-result-for context 'error)))
1243 (epg-read-output context))
1244 (epg-delete-output-file context)
1245 (if (file-exists-p input-file)
1246 (delete-file input-file))
1247 (epg-reset context))))
1250 (defun epg-start-verify (context signature &optional signed-text)
1251 "Initiate a verify operation on SIGNATURE.
1252 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1254 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1255 For a normal or a clear text signature, SIGNED-TEXT should be nil.
1257 If you use this function, you will need to wait for the completion of
1258 `epg-gpg-program' by using `epg-wait-for-completion' and call
1259 `epg-reset' to clear a temporaly output file.
1260 If you are unsure, use synchronous version of this function
1261 `epg-verify-file' or `epg-verify-string' instead."
1262 (epg-context-set-result context nil)
1264 ;; Detached signature.
1265 (if (epg-data-file signed-text)
1266 (epg-start context (list "--verify" (epg-data-file signature)
1267 (epg-data-file signed-text)))
1268 (epg-start context (list "--verify" (epg-data-file signature) "-"))
1269 (if (eq (process-status (epg-context-process context)) 'run)
1270 (process-send-string (epg-context-process context)
1271 (epg-data-string signed-text))))
1272 ;; Normal (or cleartext) signature.
1273 (if (epg-data-file signature)
1274 (epg-start context (list "--verify" (epg-data-file signature)))
1275 (epg-start context (list "--verify"))
1276 (if (eq (process-status (epg-context-process context)) 'run)
1277 (process-send-string (epg-context-process context)
1278 (epg-data-string signature))))))
1281 (defun epg-verify-file (context signature &optional signed-text plain)
1282 "Verify a file SIGNATURE.
1283 SIGNED-TEXT and PLAIN are also a file if they are specified.
1285 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1286 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1290 (epg-context-set-output-file context plain)
1291 (epg-context-set-output-file context
1292 (epg-make-temp-file "epg-output")))
1294 (epg-start-verify context
1295 (epg-make-data-from-file signature)
1296 (epg-make-data-from-file signed-text))
1297 (epg-start-verify context
1298 (epg-make-data-from-file signature)))
1299 (epg-wait-for-completion context)
1300 (if (epg-context-result-for context 'error)
1301 (error "Verify failed: %S"
1302 (epg-context-result-for context 'error)))
1304 (epg-read-output context)))
1306 (epg-delete-output-file context))
1307 (epg-reset context)))
1310 (defun epg-verify-string (context signature &optional signed-text)
1311 "Verify a string SIGNATURE.
1312 SIGNED-TEXT is a string if it is specified.
1314 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1315 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1316 (let ((coding-system-for-write 'binary)
1320 (epg-context-set-output-file context
1321 (epg-make-temp-file "epg-output"))
1324 (setq input-file (epg-make-temp-file "epg-signature"))
1325 (write-region signature nil input-file nil 'quiet)
1326 (epg-start-verify context
1327 (epg-make-data-from-file input-file)
1328 (epg-make-data-from-string signed-text)))
1329 (epg-start-verify context (epg-make-data-from-string signature)))
1331 (epg-wait-for-completion context)
1332 (if (epg-context-result-for context 'error)
1333 (error "Verify failed: %S"
1334 (epg-context-result-for context 'error)))
1335 (epg-read-output context))
1336 (epg-delete-output-file context)
1338 (file-exists-p input-file))
1339 (delete-file input-file))
1340 (epg-reset context))))
1343 (defun epg-start-sign (context plain &optional mode)
1344 "Initiate a sign operation on PLAIN.
1345 PLAIN is a data object.
1347 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1348 If MODE is t or 'detached, it makes a detached signature.
1349 Otherwise, it makes a normal signature.
1351 If you use this function, you will need to wait for the completion of
1352 `epg-gpg-program' by using `epg-wait-for-completion' and call
1353 `epg-reset' to clear a temporaly output file.
1354 If you are unsure, use synchronous version of this function
1355 `epg-sign-file' or `epg-sign-string' instead."
1356 (epg-context-set-result context nil)
1358 (append (list (if (eq mode 'clearsign)
1360 (if (or (eq mode t) (eq mode 'detached))
1368 (car (epg-key-sub-key-list signer)))))
1369 (epg-context-signers context)))
1370 (if (epg-data-file plain)
1371 (list (epg-data-file plain)))))
1372 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1373 (unless (eq (epg-context-protocol context) 'CMS)
1374 (epg-wait-for-status context '("BEGIN_SIGNING")))
1375 (if (and (epg-data-string plain)
1376 (eq (process-status (epg-context-process context)) 'run))
1377 (process-send-string (epg-context-process context)
1378 (epg-data-string plain))))
1381 (defun epg-sign-file (context plain signature &optional mode)
1382 "Sign a file PLAIN and store the result to a file SIGNATURE.
1383 If SIGNATURE is nil, it returns the result as a string.
1384 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1385 If MODE is t or 'detached, it makes a detached signature.
1386 Otherwise, it makes a normal signature."
1390 (epg-context-set-output-file context signature)
1391 (epg-context-set-output-file context
1392 (epg-make-temp-file "epg-output")))
1393 (epg-start-sign context (epg-make-data-from-file plain) mode)
1394 (epg-wait-for-completion context)
1395 (if (epg-context-result-for context 'sign)
1396 (if (epg-context-result-for context 'error)
1397 (message "Sign warning: %S"
1398 (epg-context-result-for context 'error)))
1399 (if (epg-context-result-for context 'error)
1400 (error "Sign failed: %S"
1401 (epg-context-result-for context 'error))
1402 (error "Sign failed")))
1404 (epg-read-output context)))
1406 (epg-delete-output-file context))
1407 (epg-reset context)))
1410 (defun epg-sign-string (context plain &optional mode)
1411 "Sign a string PLAIN and return the output as string.
1412 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1413 If MODE is t or 'detached, it makes a detached signature.
1414 Otherwise, it makes a normal signature."
1417 (epg-context-set-output-file context
1418 (epg-make-temp-file "epg-output"))
1419 (epg-start-sign context (epg-make-data-from-string plain) mode)
1421 (epg-wait-for-completion context)
1422 (if (epg-context-result-for context 'sign)
1423 (if (epg-context-result-for context 'error)
1424 (message "Sign warning: %S"
1425 (epg-context-result-for context 'error)))
1426 (if (epg-context-result-for context 'error)
1427 (error "Sign failed: %S"
1428 (epg-context-result-for context 'error))
1429 (error "Sign failed")))
1430 (epg-read-output context))
1431 (epg-delete-output-file context)
1432 (epg-reset context)))
1435 (defun epg-start-encrypt (context plain recipients
1436 &optional sign always-trust)
1437 "Initiate an encrypt operation on PLAIN.
1438 PLAIN is a data object.
1439 If RECIPIENTS is nil, it performs symmetric encryption.
1441 If you use this function, you will need to wait for the completion of
1442 `epg-gpg-program' by using `epg-wait-for-completion' and call
1443 `epg-reset' to clear a temporaly output file.
1444 If you are unsure, use synchronous version of this function
1445 `epg-encrypt-file' or `epg-encrypt-string' instead."
1446 (epg-context-set-result context nil)
1448 (append (if always-trust '("--always-trust"))
1449 (if recipients '("--encrypt") '("--symmetric"))
1453 (mapcar (lambda (signer)
1455 (epg-context-signers context)))))
1461 (car (epg-key-sub-key-list recipient)))))
1463 (if (epg-data-file plain)
1464 (list (epg-data-file plain)))))
1465 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1466 (unless (eq (epg-context-protocol context) 'CMS)
1468 (epg-wait-for-status context '("BEGIN_SIGNING"))
1469 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
1470 (if (and (epg-data-string plain)
1471 (eq (process-status (epg-context-process context)) 'run))
1472 (process-send-string (epg-context-process context)
1473 (epg-data-string plain))))
1476 (defun epg-encrypt-file (context plain recipients
1477 cipher &optional sign always-trust)
1478 "Encrypt a file PLAIN and store the result to a file CIPHER.
1479 If CIPHER is nil, it returns the result as a string.
1480 If RECIPIENTS is nil, it performs symmetric encryption."
1484 (epg-context-set-output-file context cipher)
1485 (epg-context-set-output-file context
1486 (epg-make-temp-file "epg-output")))
1487 (epg-start-encrypt context (epg-make-data-from-file plain)
1488 recipients sign always-trust)
1489 (epg-wait-for-completion context)
1491 (if (epg-context-result-for context 'sign)
1492 (if (epg-context-result-for context 'error)
1493 (message "Sign warning: %S"
1494 (epg-context-result-for context 'error)))
1495 (if (epg-context-result-for context 'error)
1496 (error "Sign failed: %S"
1497 (epg-context-result-for context 'error))
1498 (error "Sign failed"))))
1499 (if (epg-context-result-for context 'error)
1500 (error "Encrypt failed: %S"
1501 (epg-context-result-for context 'error)))
1503 (epg-read-output context)))
1505 (epg-delete-output-file context))
1506 (epg-reset context)))
1509 (defun epg-encrypt-string (context plain recipients
1510 &optional sign always-trust)
1511 "Encrypt a string PLAIN.
1512 If RECIPIENTS is nil, it performs symmetric encryption."
1515 (epg-context-set-output-file context
1516 (epg-make-temp-file "epg-output"))
1517 (epg-start-encrypt context (epg-make-data-from-string plain)
1518 recipients sign always-trust)
1520 (epg-wait-for-completion context)
1522 (if (epg-context-result-for context 'sign)
1523 (if (epg-context-result-for context 'error)
1524 (message "Sign warning: %S"
1525 (epg-context-result-for context 'error)))
1526 (if (epg-context-result-for context 'error)
1527 (error "Sign failed: %S"
1528 (epg-context-result-for context 'error))
1529 (error "Sign failed"))))
1530 (if (epg-context-result-for context 'error)
1531 (error "Encrypt failed: %S"
1532 (epg-context-result-for context 'error)))
1533 (epg-read-output context))
1534 (epg-delete-output-file context)
1535 (epg-reset context)))
1538 (defun epg-start-export-keys (context keys)
1539 "Initiate an export keys operation.
1541 If you use this function, you will need to wait for the completion of
1542 `epg-gpg-program' by using `epg-wait-for-completion' and call
1543 `epg-reset' to clear a temporaly output file.
1544 If you are unsure, use synchronous version of this function
1545 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1546 (epg-context-set-result context nil)
1547 (epg-start context (cons "--export"
1551 (car (epg-key-sub-key-list key))))
1555 (defun epg-export-keys-to-file (context keys file)
1556 "Extract public KEYS."
1560 (epg-context-set-output-file context file)
1561 (epg-context-set-output-file context
1562 (epg-make-temp-file "epg-output")))
1563 (epg-start-export-keys context keys)
1564 (epg-wait-for-completion context)
1565 (if (epg-context-result-for context 'error)
1566 (error "Export keys failed: %S"
1567 (epg-context-result-for context 'error)))
1569 (epg-read-output context)))
1571 (epg-delete-output-file context))
1572 (epg-reset context)))
1575 (defun epg-export-keys-to-string (context keys)
1576 "Extract public KEYS and return them as a string."
1577 (epg-export-keys-to-file context keys nil))
1580 (defun epg-start-import-keys (context keys)
1581 "Initiate an import keys operation.
1582 KEYS is a data object.
1584 If you use this function, you will need to wait for the completion of
1585 `epg-gpg-program' by using `epg-wait-for-completion' and call
1586 `epg-reset' to clear a temporaly output file.
1587 If you are unsure, use synchronous version of this function
1588 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1589 (epg-context-set-result context nil)
1590 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
1591 (epg-start context (list "--import" (epg-data-file keys)))
1592 (if (and (epg-data-string keys)
1593 (eq (process-status (epg-context-process context)) 'run))
1594 (process-send-string (epg-context-process context)
1595 (epg-data-string keys))))
1597 (defun epg-import-keys-1 (context keys)
1600 (epg-start-import-keys context keys)
1601 (if (epg-data-file keys)
1602 (epg-flush context))
1603 (epg-wait-for-completion context)
1604 (if (epg-context-result-for context 'error)
1605 (error "Import keys failed: %S"
1606 (epg-context-result-for context 'error)))
1607 (epg-read-output context))
1608 (epg-reset context)))
1611 (defun epg-import-keys-from-file (context keys)
1612 "Add keys from a file KEYS."
1613 (epg-import-keys-1 context (epg-make-data-from-file keys)))
1616 (defun epg-import-keys-from-string (context keys)
1617 "Add keys from a string KEYS."
1618 (epg-import-keys-1 context (epg-make-data-from-string keys)))
1621 (defun epg-start-delete-keys (context keys &optional allow-secret)
1622 "Initiate an delete keys operation.
1624 If you use this function, you will need to wait for the completion of
1625 `epg-gpg-program' by using `epg-wait-for-completion' and call
1626 `epg-reset' to clear a temporaly output file.
1627 If you are unsure, use synchronous version of this function
1628 `epg-delete-keys' instead."
1629 (epg-context-set-result context nil)
1630 (epg-start context (cons (if allow-secret
1631 "--delete-secret-key"
1636 (car (epg-key-sub-key-list key))))
1640 (defun epg-delete-keys (context keys &optional allow-secret)
1641 "Delete KEYS from the key ring."
1644 (epg-start-delete-keys context keys allow-secret)
1645 (epg-wait-for-completion context)
1646 (if (epg-context-result-for context 'error)
1647 (error "Delete keys failed: %S"
1648 (epg-context-result-for context 'error))))
1649 (epg-reset context)))
1652 (defun epg-start-sign-keys (context keys &optional local)
1653 "Initiate an sign keys operation.
1655 If you use this function, you will need to wait for the completion of
1656 `epg-gpg-program' by using `epg-wait-for-completion' and call
1657 `epg-reset' to clear a temporaly output file.
1658 If you are unsure, use synchronous version of this function
1659 `epg-sign-keys' instead."
1660 (epg-context-set-result context nil)
1661 (epg-start context (cons (if local
1667 (car (epg-key-sub-key-list key))))
1671 (defun epg-sign-keys (context keys &optional local)
1672 "Sign KEYS from the key ring."
1675 (epg-start-sign-keys context keys local)
1676 (epg-wait-for-completion context)
1677 (if (epg-context-result-for context 'error)
1678 (error "Sign keys failed: %S"
1679 (epg-context-result-for context 'error))))
1680 (epg-reset context)))
1682 (defun epg-decode-hexstring (string)
1684 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
1685 (setq string (replace-match "\\x\\&" t nil string)
1687 (car (read-from-string (concat "\"" string "\"")))))
1689 (defun epg-decode-quotedstring (string)
1691 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
1692 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
1694 (if (match-beginning 2)
1695 (setq string (replace-match "\\2" t nil string)
1697 (if (match-beginning 3)
1698 (setq string (replace-match "\\x\\3" t nil string)
1700 (setq string (replace-match "\\\\\\\\\\4" t nil string)
1701 index (+ index 3)))))
1702 (car (read-from-string (concat "\"" string "\"")))))
1704 (defun epg-dn-from-string (string)
1705 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
1706 The return value is an alist mapping from types to values."
1708 (length (length string))
1709 alist type value group)
1710 (while (< index length)
1711 (if (eq index (string-match "[ \t\n\r]*" string index))
1712 (setq index (match-end 0)))
1713 (if (eq index (string-match
1714 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
1716 (setq type (match-string 1 string)
1717 index (match-end 0))
1718 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
1720 (setq type (match-string 1 string)
1721 index (match-end 0))))
1723 (error "Invalid type"))
1724 (if (eq index (string-match
1725 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
1727 (setq index (match-end 0)
1728 value (epg-decode-quotedstring (match-string 0 string)))
1729 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
1730 (setq index (match-end 0)
1731 value (epg-decode-hexstring (match-string 1 string)))
1732 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
1734 (setq index (match-end 0)
1735 value (epg-decode-quotedstring (match-string 0 string))))))
1737 (if (stringp (car (car alist)))
1738 (setcar alist (list (cons type value) (car alist)))
1739 (setcar alist (cons (cons type value) (car alist))))
1740 (if (consp (car (car alist)))
1741 (setcar alist (nreverse (car alist))))
1742 (setq alist (cons (cons type value) alist)
1745 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
1746 (setq index (match-end 0)
1747 group (eq (aref string (match-beginning 1)) ?+))))
1750 (defun epg-decode-dn (alist)
1751 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
1752 Type names are resolved using `epg-dn-type-alist'."
1755 (if (stringp (car rdn))
1756 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
1758 (format "%s=%s" (cdr entry) (cdr rdn))
1759 (format "%s=%s" (car rdn) (cdr rdn))))
1760 (concat "(" (epg-decode-dn rdn) ")")))
1766 ;;; epg.el ends here