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 (or protocol 'OpenPGP) 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 &optional key-id)
340 "Return a signature object."
341 (cons 'epg-signature (vector status key-id nil nil nil nil 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-validity (signature)
356 "Return the validity 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-fingerprint (signature)
362 "Return the fingerprint 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-creation-time (signature)
368 "Return the creation time 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-expiration-time (signature)
374 "Return the expiration time of SIGNATURE."
375 (unless (eq (car signature) 'epg-signature)
376 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
377 (aref (cdr signature) 5))
379 (defun epg-signature-pubkey-algorithm (signature)
380 "Return the public key algorithm of SIGNATURE."
381 (unless (eq (car signature) 'epg-signature)
382 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
383 (aref (cdr signature) 6))
385 (defun epg-signature-digest-algorithm (signature)
386 "Return the digest algorithm of SIGNATURE."
387 (unless (eq (car signature) 'epg-signature)
388 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
389 (aref (cdr signature) 7))
391 (defun epg-signature-set-status (signature status)
392 "Set the status code of SIGNATURE."
393 (unless (eq (car signature) 'epg-signature)
394 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
395 (aset (cdr signature) 0 status))
397 (defun epg-signature-set-key-id (signature key-id)
398 "Set the key-id of SIGNATURE."
399 (unless (eq (car signature) 'epg-signature)
400 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
401 (aset (cdr signature) 1 key-id))
403 (defun epg-signature-set-validity (signature validity)
404 "Set the validity of SIGNATURE."
405 (unless (eq (car signature) 'epg-signature)
406 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
407 (aset (cdr signature) 2 validity))
409 (defun epg-signature-set-fingerprint (signature fingerprint)
410 "Set the fingerprint of SIGNATURE."
411 (unless (eq (car signature) 'epg-signature)
412 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
413 (aset (cdr signature) 3 fingerprint))
415 (defun epg-signature-set-creation-time (signature creation-time)
416 "Set the creation time of SIGNATURE."
417 (unless (eq (car signature) 'epg-signature)
418 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
419 (aset (cdr signature) 4 creation-time))
421 (defun epg-signature-set-expiration-time (signature expiration-time)
422 "Set the expiration time of SIGNATURE."
423 (unless (eq (car signature) 'epg-signature)
424 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
425 (aset (cdr signature) 5 expiration-time))
427 (defun epg-signature-set-pubkey-algorithm (signature pubkey-algorithm)
428 "Set the public key algorithm of SIGNATURE."
429 (unless (eq (car signature) 'epg-signature)
430 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
431 (aset (cdr signature) 6 pubkey-algorithm))
433 (defun epg-signature-set-digest-algorithm (signature digest-algorithm)
434 "Set the digest algorithm of SIGNATURE."
435 (unless (eq (car signature) 'epg-signature)
436 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
437 (aset (cdr signature) 7 digest-algorithm))
439 (defun epg-make-key (owner-trust)
440 "Return a key object."
441 (cons 'epg-key (vector owner-trust nil nil)))
443 (defun epg-key-owner-trust (key)
444 "Return the owner trust of KEY."
445 (unless (eq (car key) 'epg-key)
446 (signal 'wrong-type-argument (list 'epg-key-p key)))
449 (defun epg-key-sub-key-list (key)
450 "Return the sub key list of KEY."
451 (unless (eq (car key) 'epg-key)
452 (signal 'wrong-type-argument (list 'epg-key-p key)))
455 (defun epg-key-user-id-list (key)
456 "Return the user ID list of KEY."
457 (unless (eq (car key) 'epg-key)
458 (signal 'wrong-type-argument (list 'epg-key-p key)))
461 (defun epg-key-set-sub-key-list (key sub-key-list)
462 "Set the sub key list of KEY."
463 (unless (eq (car key) 'epg-key)
464 (signal 'wrong-type-argument (list 'epg-key-p key)))
465 (aset (cdr key) 1 sub-key-list))
467 (defun epg-key-set-user-id-list (key user-id-list)
468 "Set the user ID list of KEY."
469 (unless (eq (car key) 'epg-key)
470 (signal 'wrong-type-argument (list 'epg-key-p key)))
471 (aset (cdr key) 2 user-id-list))
473 (defun epg-make-sub-key (validity capability secret algorithm length id
474 creation-time expiration-time)
475 "Return a sub key object."
477 (vector validity capability secret algorithm length id creation-time
478 expiration-time nil)))
480 (defun epg-sub-key-validity (sub-key)
481 "Return the validity 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) 0))
486 (defun epg-sub-key-capability (sub-key)
487 "Return the capability 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) 1))
492 (defun epg-sub-key-secret (sub-key)
493 "Return non-nil if SUB-KEY is a secret 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) 2))
498 (defun epg-sub-key-algorithm (sub-key)
499 "Return the algorithm of SUB-KEY."
500 (unless (eq (car sub-key) 'epg-sub-key)
501 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
502 (aref (cdr sub-key) 3))
504 (defun epg-sub-key-length (sub-key)
505 "Return the length of SUB-KEY."
506 (unless (eq (car sub-key) 'epg-sub-key)
507 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
508 (aref (cdr sub-key) 4))
510 (defun epg-sub-key-id (sub-key)
511 "Return the ID of SUB-KEY."
512 (unless (eq (car sub-key) 'epg-sub-key)
513 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
514 (aref (cdr sub-key) 5))
516 (defun epg-sub-key-creation-time (sub-key)
517 "Return the creation time of SUB-KEY."
518 (unless (eq (car sub-key) 'epg-sub-key)
519 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
520 (aref (cdr sub-key) 6))
522 (defun epg-sub-key-expiration-time (sub-key)
523 "Return the expiration time of SUB-KEY."
524 (unless (eq (car sub-key) 'epg-sub-key)
525 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
526 (aref (cdr sub-key) 7))
528 (defun epg-sub-key-fingerprint (sub-key)
529 "Return the fingerprint of SUB-KEY."
530 (unless (eq (car sub-key) 'epg-sub-key)
531 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
532 (aref (cdr sub-key) 8))
534 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
535 "Set the fingerprint of SUB-KEY.
536 This function is for internal use only."
537 (unless (eq (car sub-key) 'epg-sub-key)
538 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
539 (aset (cdr sub-key) 8 fingerprint))
541 (defun epg-make-user-id (validity name)
542 "Return a user ID object."
543 (cons 'epg-user-id (vector validity name nil)))
545 (defun epg-user-id-validity (user-id)
546 "Return the validity of USER-ID."
547 (unless (eq (car user-id) 'epg-user-id)
548 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
549 (aref (cdr user-id) 0))
551 (defun epg-user-id-name (user-id)
552 "Return the name of USER-ID."
553 (unless (eq (car user-id) 'epg-user-id)
554 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
555 (aref (cdr user-id) 1))
557 (defun epg-user-id-signature-list (user-id)
558 "Return the signature list of USER-ID."
559 (unless (eq (car user-id) 'epg-user-id)
560 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
561 (aref (cdr user-id) 2))
563 (defun epg-user-id-set-signature-list (user-id signature-list)
564 "Set the signature list of USER-ID."
565 (unless (eq (car user-id) 'epg-user-id)
566 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
567 (aset (cdr user-id) 2 signature-list))
569 (defun epg-context-result-for (context name)
570 (cdr (assq name (epg-context-result context))))
572 (defun epg-context-set-result-for (context name value)
573 (let* ((result (epg-context-result context))
574 (entry (assq name result)))
577 (epg-context-set-result context (cons (cons name value) result)))))
579 (defun epg-signature-to-string (signature)
580 (let ((user-id (cdr (assoc (epg-signature-key-id signature)
581 epg-user-id-alist))))
583 (cond ((eq (epg-signature-status signature) 'good)
585 ((eq (epg-signature-status signature) 'bad)
587 ((eq (epg-signature-status signature) 'expired)
588 "Expired signature ")
589 ((eq (epg-signature-status signature) 'expired-key)
590 "Signature made by expired key ")
591 ((eq (epg-signature-status signature) 'revoked-key)
592 "Signature made by revoked key ")
593 ((eq (epg-signature-status signature) 'no-pubkey)
594 "No public key for "))
595 (epg-signature-key-id signature)
598 (if (stringp user-id)
600 (epg-decode-dn user-id))
603 (if (epg-signature-validity signature)
604 (format "(trust %s)" (epg-signature-validity signature))
607 (defun epg-verify-result-to-string (verify-result)
608 (mapconcat #'epg-signature-to-string verify-result "\n"))
610 (defun epg-start (context args)
611 "Start `epg-gpg-program' in a subprocess with given ARGS."
612 (let* ((args (append (list "--no-tty"
615 (unless (eq (epg-context-protocol context) 'CMS)
616 (list "--command-fd" "0"))
617 (if (epg-context-armor context) '("--armor"))
618 (if (epg-context-textmode context) '("--textmode"))
619 (if (epg-context-output-file context)
620 (list "--output" (epg-context-output-file context)))
622 (coding-system-for-write 'binary)
623 process-connection-type
624 (orig-mode (default-file-modes))
625 (buffer (generate-new-buffer " *epg*"))
629 (unless epg-debug-buffer
630 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
631 (set-buffer epg-debug-buffer)
632 (goto-char (point-max))
633 (insert (format "%s %s\n"
634 (if (eq (epg-context-protocol context) 'CMS)
637 (mapconcat #'identity args " ")))))
638 (with-current-buffer buffer
639 (make-local-variable 'epg-read-point)
640 (setq epg-read-point (point-min))
641 (make-local-variable 'epg-pending-status-list)
642 (setq epg-pending-status-list nil)
643 (make-local-variable 'epg-key-id)
644 (setq epg-key-id nil)
645 (make-local-variable 'epg-context)
646 (setq epg-context context))
649 (set-default-file-modes 448)
651 (apply #'start-process "epg" buffer
652 (if (eq (epg-context-protocol context) 'CMS)
656 (set-default-file-modes orig-mode))
657 (set-process-filter process #'epg-process-filter)
658 (epg-context-set-process context process)))
660 (defun epg-process-filter (process input)
663 (unless epg-debug-buffer
664 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
665 (set-buffer epg-debug-buffer)
666 (goto-char (point-max))
668 (if (buffer-live-p (process-buffer process))
670 (set-buffer (process-buffer process))
671 (goto-char (point-max))
673 (goto-char epg-read-point)
675 (while (looking-at ".*\n") ;the input line finished
677 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
678 (let* ((status (match-string 1))
679 (string (match-string 2))
680 (symbol (intern-soft (concat "epg-status-" status))))
681 (if (member status epg-pending-status-list)
682 (setq epg-pending-status-list nil))
685 (funcall symbol process string)))))
687 (setq epg-read-point (point)))))
689 (defun epg-read-output (context)
691 (if (fboundp 'set-buffer-multibyte)
692 (set-buffer-multibyte nil))
693 (if (file-exists-p (epg-context-output-file context))
694 (let ((coding-system-for-read (if (epg-context-textmode context)
697 (insert-file-contents (epg-context-output-file context))
700 (defun epg-wait-for-status (context status-list)
701 (with-current-buffer (process-buffer (epg-context-process context))
702 (setq epg-pending-status-list status-list)
703 (while (and (eq (process-status (epg-context-process context)) 'run)
704 epg-pending-status-list)
705 (accept-process-output (epg-context-process context) 1))))
707 (defun epg-wait-for-completion (context)
708 (while (eq (process-status (epg-context-process context)) 'run)
709 ;; We can't use accept-process-output instead of sit-for here
710 ;; because it may cause an interrupt during the sentinel execution.
713 (defun epg-flush (context)
714 (if (eq (process-status (epg-context-process context)) 'run)
715 (process-send-eof (epg-context-process context))))
717 (defun epg-reset (context)
718 (if (and (epg-context-process context)
719 (buffer-live-p (process-buffer (epg-context-process context))))
720 (kill-buffer (process-buffer (epg-context-process context))))
721 (epg-context-set-process context nil))
723 (defun epg-delete-output-file (context)
724 (if (and (epg-context-output-file context)
725 (file-exists-p (epg-context-output-file context)))
726 (delete-file (epg-context-output-file context))))
728 (defun epg-status-USERID_HINT (process string)
729 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
730 (let* ((key-id (match-string 1 string))
731 (user-id (match-string 2 string))
732 (entry (assoc key-id epg-user-id-alist)))
734 (setcdr entry user-id)
735 (setq epg-user-id-alist (cons (cons key-id user-id)
736 epg-user-id-alist))))))
738 (defun epg-status-NEED_PASSPHRASE (process string)
739 (if (string-match "\\`\\([^ ]+\\)" string)
740 (setq epg-key-id (match-string 1 string))))
742 (defun epg-status-NEED_PASSPHRASE_SYM (process string)
743 (setq epg-key-id 'SYM))
745 (defun epg-status-NEED_PASSPHRASE_PIN (process string)
746 (setq epg-key-id 'PIN))
748 (defun epg-status-GET_HIDDEN (process string)
750 (string-match "\\`passphrase\\." string))
753 passphrase-with-new-line)
759 (if (consp (epg-context-passphrase-callback
761 (car (epg-context-passphrase-callback
763 (epg-context-passphrase-callback epg-context))
766 (if (consp (epg-context-passphrase-callback
768 (cdr (epg-context-passphrase-callback
771 (setq passphrase-with-new-line (concat passphrase "\n"))
772 (fillarray passphrase 0)
773 (setq passphrase nil)
774 (process-send-string process passphrase-with-new-line)))
776 (epg-context-set-result-for
779 (epg-context-result-for epg-context 'error)))
780 (delete-process process)))
782 (fillarray passphrase 0))
783 (if passphrase-with-new-line
784 (fillarray passphrase-with-new-line 0))))))
786 (defun epg-status-GET_BOOL (process string)
787 (let ((entry (assoc string epg-prompt-alist))
790 (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
791 (process-send-string process "y\n")
792 (process-send-string process "n\n"))
794 (epg-context-set-result-for
797 (epg-context-result-for epg-context 'error)))
798 (delete-process process)))))
800 (defun epg-status-GET_LINE (process string)
801 (let ((entry (assoc string epg-prompt-alist))
806 (concat (read-string (if entry (cdr entry) (concat string ": ")))
809 (epg-context-set-result-for
812 (epg-context-result-for epg-context 'error)))
813 (delete-process process)))))
815 (defun epg-signature-status-internal (status string)
816 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
817 (let* ((key-id (match-string 1 string))
818 (user-id (match-string 2 string))
819 (entry (assoc key-id epg-user-id-alist)))
820 (epg-context-set-result-for
823 (cons (epg-make-signature status key-id)
824 (epg-context-result-for epg-context 'verify)))
825 (if (eq (epg-context-protocol epg-context) 'CMS)
827 (setq user-id (epg-dn-from-string user-id))
830 (setcdr entry user-id)
831 (setq epg-user-id-alist
832 (cons (cons key-id user-id) epg-user-id-alist))))
833 (epg-context-set-result-for
836 (cons (epg-make-signature status)
837 (epg-context-result-for epg-context 'verify)))))
839 (defun epg-status-GOODSIG (process string)
840 (epg-signature-status-internal 'good string))
842 (defun epg-status-EXPSIG (process string)
843 (epg-signature-status-internal 'expired string))
845 (defun epg-status-EXPKEYSIG (process string)
846 (epg-signature-status-internal 'expired-key string))
848 (defun epg-status-REVKEYSIG (process string)
849 (epg-signature-status-internal 'revoked-key string))
851 (defun epg-status-BADSIG (process string)
852 (epg-signature-status-internal 'bad string))
854 (defun epg-status-NO_PUBKEY (process string)
855 (epg-context-set-result-for
858 (cons (epg-make-signature 'no-pubkey string)
859 (epg-context-result-for epg-context 'verify))))
861 (defun epg-status-ERRSIG (process string)
862 (let ((signatures (car (epg-context-result-for epg-context 'verify))))
864 (setq signatures (list (epg-make-signature 'error)))
865 (epg-context-set-result-for epg-context 'verify signatures))
866 (when (and (not (eq (epg-signature-status (car signatures)) 'good))
867 (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
868 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
870 (epg-signature-set-key-id
872 (match-string 1 string))
873 (epg-signature-set-pubkey-algorithm
875 (string-to-number (match-string 2 string)))
876 (epg-signature-set-digest-algorithm
878 (string-to-number (match-string 3 string)))
879 ; (epg-signature-set-class
881 ; (string-to-number (match-string 4 string) 16))
882 (epg-signature-set-creation-time
884 (match-string 5 string)))))
886 (defun epg-status-VALIDSIG (process string)
887 (let ((signature (car (epg-context-result-for epg-context 'verify))))
889 (eq (epg-signature-status signature) 'good)
890 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
891 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
894 (epg-signature-set-fingerprint
896 (match-string 1 string))
897 (epg-signature-set-creation-time
899 (match-string 2 string))
900 (epg-signature-set-expiration-time
902 (match-string 3 string))
903 ; (epg-signature-set-version
905 ; (string-to-number (match-string 4 string)))
906 (epg-signature-set-pubkey-algorithm
908 (string-to-number (match-string 5 string)))
909 (epg-signature-set-digest-algorithm
911 (string-to-number (match-string 6 string)))
912 ; (epg-signature-set-class
914 ; (string-to-number (match-string 7 string) 16))
917 (defun epg-status-TRUST_UNDEFINED (process string)
918 (let ((signature (car (epg-context-result-for epg-context 'verify))))
920 (eq (epg-signature-status signature) 'good))
921 (epg-signature-set-validity signature 'undefined))))
923 (defun epg-status-TRUST_NEVER (process string)
924 (let ((signature (car (epg-context-result-for epg-context 'verify))))
926 (eq (epg-signature-status signature) 'good))
927 (epg-signature-set-validity signature 'never))))
929 (defun epg-status-TRUST_MARGINAL (process string)
930 (let ((signature (car (epg-context-result-for epg-context 'verify))))
932 (eq (epg-signature-status signature) 'marginal))
933 (epg-signature-set-validity signature 'marginal))))
935 (defun epg-status-TRUST_FULLY (process string)
936 (let ((signature (car (epg-context-result-for epg-context 'verify))))
938 (eq (epg-signature-status signature) 'good))
939 (epg-signature-set-validity signature 'full))))
941 (defun epg-status-TRUST_ULTIMATE (process string)
942 (let ((signature (car (epg-context-result-for epg-context 'verify))))
944 (eq (epg-signature-status signature) 'good))
945 (epg-signature-set-validity signature 'ultimate))))
947 (defun epg-status-PROGRESS (process string)
948 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
950 (funcall (if (consp (epg-context-progress-callback epg-context))
951 (car (epg-context-progress-callback epg-context))
952 (epg-context-progress-callback epg-context))
954 (match-string 1 string)
955 (match-string 2 string)
956 (string-to-number (match-string 3 string))
957 (string-to-number (match-string 4 string))
958 (if (consp (epg-context-progress-callback epg-context))
959 (cdr (epg-context-progress-callback epg-context))))))
961 (defun epg-status-DECRYPTION_FAILED (process string)
962 (epg-context-set-result-for
964 (cons 'decryption-failed
965 (epg-context-result-for epg-context 'error))))
967 (defun epg-status-NODATA (process string)
968 (epg-context-set-result-for
970 (cons (cons 'no-data (string-to-number string))
971 (epg-context-result-for epg-context 'error))))
973 (defun epg-status-UNEXPECTED (process string)
974 (epg-context-set-result-for
976 (cons (cons 'unexpected (string-to-number string))
977 (epg-context-result-for epg-context 'error))))
979 (defun epg-status-KEYEXPIRED (process string)
980 (epg-context-set-result-for
982 (cons (cons 'key-expired string)
983 (epg-context-result-for epg-context 'error))))
985 (defun epg-status-KEYREVOKED (process string)
986 (epg-context-set-result-for
989 (epg-context-result-for epg-context 'error))))
991 (defun epg-status-BADARMOR (process string)
992 (epg-context-set-result-for
995 (epg-context-result-for epg-context 'error))))
997 (defun epg-status-INV_RECP (process string)
998 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
999 (epg-context-set-result-for
1001 (cons (list 'invalid-recipient
1002 (string-to-number (match-string 1 string))
1003 (match-string 2 string))
1004 (epg-context-result-for epg-context 'error)))))
1006 (defun epg-status-NO_RECP (process string)
1007 (epg-context-set-result-for
1009 (cons 'no-recipients
1010 (epg-context-result-for epg-context 'error))))
1012 (defun epg-status-DELETE_PROBLEM (process string)
1013 (if (string-match "\\`\\([0-9]+\\)" string)
1014 (epg-context-set-result-for
1016 (cons (cons 'delete-problem (string-to-number (match-string 1 string)))
1017 (epg-context-result-for epg-context 'error)))))
1019 (defun epg-status-SIG_CREATED (process string)
1020 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1021 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1022 (epg-context-set-result-for
1024 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1025 (cons 'pubkey-algorithm
1026 (string-to-number (match-string 2 string)))
1027 (cons 'digest-algorithm
1028 (string-to-number (match-string 3 string)))
1029 (cons 'class (string-to-number (match-string 4 string) 16))
1030 (cons 'creation-time (match-string 5 string))
1031 (cons 'fingerprint (substring string (match-end 0))))
1032 (epg-context-result-for epg-context 'sign)))))
1034 (defun epg-passphrase-callback-function (context key-id handback)
1036 (if (eq key-id 'SYM)
1037 "Passphrase for symmetric encryption: "
1038 (if (eq key-id 'PIN)
1039 "Passphrase for PIN: "
1040 (let ((entry (assoc key-id epg-user-id-alist)))
1042 (format "Passphrase for %s %s: " key-id (cdr entry))
1043 (format "Passphrase for %s: " key-id)))))))
1045 (defun epg-progress-callback-function (context what char current total
1047 (message "%s: %d%%/%d%%" what current total))
1049 (defun epg-configuration ()
1050 "Return a list of internal configuration parameters of `epg-gpg-program'."
1053 (apply #'call-process epg-gpg-program nil (list t nil) nil
1054 '("--with-colons" "--list-config"))
1055 (goto-char (point-min))
1056 (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
1057 (setq type (intern (match-string 1))
1058 config (cons (cons type
1060 '(pubkey cipher digest compress))
1061 (mapcar #'string-to-number
1062 (delete "" (split-string
1069 (defun epg-list-keys-1 (context name mode)
1070 (let ((args (append (list "--with-colons" "--no-greeting" "--batch"
1071 "--with-fingerprint"
1072 "--with-fingerprint"
1073 (if (or (eq mode t) (eq mode 'secret))
1074 "--list-secret-keys"
1078 (unless (eq (epg-context-protocol context) 'CMS)
1079 '("--fixed-list-mode"))
1080 (if name (list name))))
1081 keys string field index)
1083 (apply #'call-process
1084 (if (eq (epg-context-protocol context) 'CMS)
1087 nil (list t nil) nil args)
1088 (goto-char (point-min))
1089 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1090 (setq keys (cons (make-vector 15 nil) keys)
1091 string (match-string 0)
1095 (string-match "\\([^:]+\\)?:" string index))
1096 (setq index (match-end 0))
1097 (aset (car keys) field (match-string 1 string))
1098 (setq field (1+ field))))
1101 (defun epg-make-sub-key-1 (line)
1104 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1106 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1108 (member (aref line 0) '("sec" "ssb"))
1109 (string-to-number (aref line 3))
1110 (string-to-number (aref line 2))
1115 (defun epg-list-keys (context &optional name mode)
1116 "Return a list of epg-key objects matched with NAME.
1117 If MODE is nil, only public keyring should be searched.
1118 If MODE is t or 'secret, only secret keyring should be searched.
1119 Otherwise, only public keyring should be searched and the key
1120 signatures should be included."
1121 (let ((lines (epg-list-keys-1 context name mode))
1125 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1127 (epg-key-set-sub-key-list
1129 (nreverse (epg-key-sub-key-list (car keys))))
1130 (epg-key-set-user-id-list
1132 (nreverse (epg-key-user-id-list (car keys)))))
1133 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1134 keys (cons (epg-make-key
1135 (if (aref (car lines) 8)
1136 (cdr (assq (string-to-char (aref (car lines) 8))
1137 epg-key-validity-alist))))
1139 (epg-key-set-sub-key-list
1141 (cons (epg-make-sub-key-1 (car lines))
1142 (epg-key-sub-key-list (car keys)))))
1143 ((member (aref (car lines) 0) '("sub" "ssb"))
1144 (epg-key-set-sub-key-list
1146 (cons (epg-make-sub-key-1 (car lines))
1147 (epg-key-sub-key-list (car keys)))))
1148 ((equal (aref (car lines) 0) "uid")
1149 (epg-key-set-user-id-list
1151 (cons (epg-make-user-id
1152 (if (aref (car lines) 1)
1153 (cdr (assq (string-to-char (aref (car lines) 1))
1154 epg-key-validity-alist)))
1157 (epg-dn-from-string (aref (car lines) 9))
1158 (error (aref (car lines) 9)))
1159 (aref (car lines) 9)))
1160 (epg-key-user-id-list (car keys)))))
1161 ((equal (aref (car lines) 0) "fpr")
1162 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1163 (aref (car lines) 9))))
1164 (setq lines (cdr lines)))
1166 (epg-key-set-sub-key-list
1168 (nreverse (epg-key-sub-key-list (car keys))))
1169 (epg-key-set-user-id-list
1171 (nreverse (epg-key-user-id-list (car keys)))))
1174 (if (fboundp 'make-temp-file)
1175 (defalias 'epg-make-temp-file 'make-temp-file)
1176 ;; stolen from poe.el.
1177 (defun epg-make-temp-file (prefix)
1178 "Create a temporary file.
1179 The returned file name (created by appending some random characters at the end
1180 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1181 is guaranteed to point to a newly created empty file.
1182 You can then use `write-region' to write new data into the file."
1183 (let (tempdir tempfile)
1186 ;; First, create a temporary directory.
1187 (while (condition-case ()
1189 (setq tempdir (make-temp-name
1191 (file-name-directory prefix)
1193 ;; return nil or signal an error.
1194 (make-directory tempdir))
1196 (file-already-exists t)))
1197 (set-file-modes tempdir 448)
1198 ;; Second, create a temporary file in the tempdir.
1199 ;; There *is* a race condition between `make-temp-name'
1200 ;; and `write-region', but we don't care it since we are
1201 ;; in a private directory now.
1202 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1203 (write-region "" nil tempfile nil 'silent)
1204 (set-file-modes tempfile 384)
1205 ;; Finally, make a hard-link from the tempfile.
1206 (while (condition-case ()
1208 (setq file (make-temp-name prefix))
1209 ;; return nil or signal an error.
1210 (add-name-to-file tempfile file))
1212 (file-already-exists t)))
1214 ;; Cleanup the tempfile.
1216 (file-exists-p tempfile)
1217 (delete-file tempfile))
1218 ;; Cleanup the tempdir.
1220 (file-directory-p tempdir)
1221 (delete-directory tempdir))))))
1224 (defun epg-cancel (context)
1225 (if (eq (process-status (epg-context-process context)) 'run)
1226 (delete-process (epg-context-process context))))
1229 (defun epg-start-decrypt (context cipher)
1230 "Initiate a decrypt operation on CIPHER.
1231 CIPHER is a data object.
1233 If you use this function, you will need to wait for the completion of
1234 `epg-gpg-program' by using `epg-wait-for-completion' and call
1235 `epg-reset' to clear a temporaly output file.
1236 If you are unsure, use synchronous version of this function
1237 `epg-decrypt-file' or `epg-decrypt-string' instead."
1238 (unless (epg-data-file cipher)
1239 (error "Not a file"))
1240 (epg-context-set-result context nil)
1241 (epg-start context (list "--decrypt" (epg-data-file cipher)))
1242 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1243 (unless (eq (epg-context-protocol context) 'CMS)
1244 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1247 (defun epg-decrypt-file (context cipher plain)
1248 "Decrypt a file CIPHER and store the result to a file PLAIN.
1249 If PLAIN is nil, it returns the result as a string."
1253 (epg-context-set-output-file context plain)
1254 (epg-context-set-output-file context
1255 (epg-make-temp-file "epg-output")))
1256 (epg-start-decrypt context (epg-make-data-from-file cipher))
1257 (epg-wait-for-completion context)
1258 (if (epg-context-result-for context 'error)
1259 (error "Decrypt failed: %S"
1260 (epg-context-result-for context 'error)))
1262 (epg-read-output context)))
1264 (epg-delete-output-file context))
1265 (epg-reset context)))
1268 (defun epg-decrypt-string (context cipher)
1269 "Decrypt a string CIPHER and return the plain text."
1270 (let ((input-file (epg-make-temp-file "epg-input"))
1271 (coding-system-for-write 'binary))
1274 (write-region cipher nil input-file nil 'quiet)
1275 (epg-context-set-output-file context
1276 (epg-make-temp-file "epg-output"))
1277 (epg-start-decrypt context (epg-make-data-from-file input-file))
1279 (epg-wait-for-completion context)
1280 (if (epg-context-result-for context 'error)
1281 (error "Decrypt failed: %S"
1282 (epg-context-result-for context 'error)))
1283 (epg-read-output context))
1284 (epg-delete-output-file context)
1285 (if (file-exists-p input-file)
1286 (delete-file input-file))
1287 (epg-reset context))))
1290 (defun epg-start-verify (context signature &optional signed-text)
1291 "Initiate a verify operation on SIGNATURE.
1292 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1294 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1295 For a normal or a clear text signature, SIGNED-TEXT should be nil.
1297 If you use this function, you will need to wait for the completion of
1298 `epg-gpg-program' by using `epg-wait-for-completion' and call
1299 `epg-reset' to clear a temporaly output file.
1300 If you are unsure, use synchronous version of this function
1301 `epg-verify-file' or `epg-verify-string' instead."
1302 (epg-context-set-result context nil)
1304 ;; Detached signature.
1305 (if (epg-data-file signed-text)
1306 (epg-start context (list "--verify" (epg-data-file signature)
1307 (epg-data-file signed-text)))
1308 (epg-start context (list "--verify" (epg-data-file signature) "-"))
1309 (if (eq (process-status (epg-context-process context)) 'run)
1310 (process-send-string (epg-context-process context)
1311 (epg-data-string signed-text))))
1312 ;; Normal (or cleartext) signature.
1313 (if (epg-data-file signature)
1314 (epg-start context (list "--verify" (epg-data-file signature)))
1315 (epg-start context (list "--verify"))
1316 (if (eq (process-status (epg-context-process context)) 'run)
1317 (process-send-string (epg-context-process context)
1318 (epg-data-string signature))))))
1321 (defun epg-verify-file (context signature &optional signed-text plain)
1322 "Verify a file SIGNATURE.
1323 SIGNED-TEXT and PLAIN are also a file if they are specified.
1325 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1326 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1330 (epg-context-set-output-file context plain)
1331 (epg-context-set-output-file context
1332 (epg-make-temp-file "epg-output")))
1334 (epg-start-verify context
1335 (epg-make-data-from-file signature)
1336 (epg-make-data-from-file signed-text))
1337 (epg-start-verify context
1338 (epg-make-data-from-file signature)))
1339 (epg-wait-for-completion context)
1340 ; (if (epg-context-result-for context 'error)
1341 ; (error "Verify failed: %S"
1342 ; (epg-context-result-for context 'error)))
1344 (epg-read-output context)))
1346 (epg-delete-output-file context))
1347 (epg-reset context)))
1350 (defun epg-verify-string (context signature &optional signed-text)
1351 "Verify a string SIGNATURE.
1352 SIGNED-TEXT is a string if it is specified.
1354 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1355 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1356 (let ((coding-system-for-write 'binary)
1360 (epg-context-set-output-file context
1361 (epg-make-temp-file "epg-output"))
1364 (setq input-file (epg-make-temp-file "epg-signature"))
1365 (write-region signature nil input-file nil 'quiet)
1366 (epg-start-verify context
1367 (epg-make-data-from-file input-file)
1368 (epg-make-data-from-string signed-text)))
1369 (epg-start-verify context (epg-make-data-from-string signature)))
1371 (epg-wait-for-completion context)
1372 ; (if (epg-context-result-for context 'error)
1373 ; (error "Verify failed: %S"
1374 ; (epg-context-result-for context 'error)))
1375 (epg-read-output context))
1376 (epg-delete-output-file context)
1378 (file-exists-p input-file))
1379 (delete-file input-file))
1380 (epg-reset context))))
1383 (defun epg-start-sign (context plain &optional mode)
1384 "Initiate a sign operation on PLAIN.
1385 PLAIN is a data object.
1387 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1388 If MODE is t or 'detached, it makes a detached signature.
1389 Otherwise, it makes a normal signature.
1391 If you use this function, you will need to wait for the completion of
1392 `epg-gpg-program' by using `epg-wait-for-completion' and call
1393 `epg-reset' to clear a temporaly output file.
1394 If you are unsure, use synchronous version of this function
1395 `epg-sign-file' or `epg-sign-string' instead."
1396 (epg-context-set-result context nil)
1398 (append (list (if (eq mode 'clearsign)
1400 (if (or (eq mode t) (eq mode 'detached))
1408 (car (epg-key-sub-key-list signer)))))
1409 (epg-context-signers context)))
1410 (if (epg-data-file plain)
1411 (list (epg-data-file plain)))))
1412 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1413 (unless (eq (epg-context-protocol context) 'CMS)
1414 (epg-wait-for-status context '("BEGIN_SIGNING")))
1415 (if (and (epg-data-string plain)
1416 (eq (process-status (epg-context-process context)) 'run))
1417 (process-send-string (epg-context-process context)
1418 (epg-data-string plain))))
1421 (defun epg-sign-file (context plain signature &optional mode)
1422 "Sign a file PLAIN and store the result to a file SIGNATURE.
1423 If SIGNATURE is nil, it returns the result as a string.
1424 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1425 If MODE is t or 'detached, it makes a detached signature.
1426 Otherwise, it makes a normal signature."
1430 (epg-context-set-output-file context signature)
1431 (epg-context-set-output-file context
1432 (epg-make-temp-file "epg-output")))
1433 (epg-start-sign context (epg-make-data-from-file plain) mode)
1434 (epg-wait-for-completion context)
1435 (if (epg-context-result-for context 'sign)
1436 (if (epg-context-result-for context 'error)
1437 (message "Sign warning: %S"
1438 (epg-context-result-for context 'error)))
1439 (if (epg-context-result-for context 'error)
1440 (error "Sign failed: %S"
1441 (epg-context-result-for context 'error))
1442 (error "Sign failed")))
1444 (epg-read-output context)))
1446 (epg-delete-output-file context))
1447 (epg-reset context)))
1450 (defun epg-sign-string (context plain &optional mode)
1451 "Sign a string PLAIN and return the output as string.
1452 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1453 If MODE is t or 'detached, it makes a detached signature.
1454 Otherwise, it makes a normal signature."
1457 (epg-context-set-output-file context
1458 (epg-make-temp-file "epg-output"))
1459 (epg-start-sign context (epg-make-data-from-string plain) mode)
1461 (epg-wait-for-completion context)
1462 (unless (epg-context-result-for context 'sign)
1463 (if (epg-context-result-for context 'error)
1464 (error "Sign failed: %S"
1465 (epg-context-result-for context 'error))
1466 (error "Sign failed")))
1467 (epg-read-output context))
1468 (epg-delete-output-file context)
1469 (epg-reset context)))
1472 (defun epg-start-encrypt (context plain recipients
1473 &optional sign always-trust)
1474 "Initiate an encrypt operation on PLAIN.
1475 PLAIN is a data object.
1476 If RECIPIENTS is nil, it performs symmetric encryption.
1478 If you use this function, you will need to wait for the completion of
1479 `epg-gpg-program' by using `epg-wait-for-completion' and call
1480 `epg-reset' to clear a temporaly output file.
1481 If you are unsure, use synchronous version of this function
1482 `epg-encrypt-file' or `epg-encrypt-string' instead."
1483 (epg-context-set-result context nil)
1485 (append (if always-trust '("--always-trust"))
1486 (if recipients '("--encrypt") '("--symmetric"))
1490 (mapcar (lambda (signer)
1492 (epg-context-signers context)))))
1498 (car (epg-key-sub-key-list recipient)))))
1500 (if (epg-data-file plain)
1501 (list (epg-data-file plain)))))
1502 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1503 (unless (eq (epg-context-protocol context) 'CMS)
1505 (epg-wait-for-status context '("BEGIN_SIGNING"))
1506 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
1507 (if (and (epg-data-string plain)
1508 (eq (process-status (epg-context-process context)) 'run))
1509 (process-send-string (epg-context-process context)
1510 (epg-data-string plain))))
1513 (defun epg-encrypt-file (context plain recipients
1514 cipher &optional sign always-trust)
1515 "Encrypt a file PLAIN and store the result to a file CIPHER.
1516 If CIPHER is nil, it returns the result as a string.
1517 If RECIPIENTS is nil, it performs symmetric encryption."
1521 (epg-context-set-output-file context cipher)
1522 (epg-context-set-output-file context
1523 (epg-make-temp-file "epg-output")))
1524 (epg-start-encrypt context (epg-make-data-from-file plain)
1525 recipients sign always-trust)
1526 (epg-wait-for-completion context)
1528 (not (epg-context-result-for context 'sign)))
1529 (if (epg-context-result-for context 'error)
1530 (error "Sign failed: %S"
1531 (epg-context-result-for context 'error))
1532 (error "Sign failed")))
1533 (if (epg-context-result-for context 'error)
1534 (error "Encrypt failed: %S"
1535 (epg-context-result-for context 'error)))
1537 (epg-read-output context)))
1539 (epg-delete-output-file context))
1540 (epg-reset context)))
1543 (defun epg-encrypt-string (context plain recipients
1544 &optional sign always-trust)
1545 "Encrypt a string PLAIN.
1546 If RECIPIENTS is nil, it performs symmetric encryption."
1549 (epg-context-set-output-file context
1550 (epg-make-temp-file "epg-output"))
1551 (epg-start-encrypt context (epg-make-data-from-string plain)
1552 recipients sign always-trust)
1554 (epg-wait-for-completion context)
1556 (not (epg-context-result-for context 'sign)))
1557 (if (epg-context-result-for context 'error)
1558 (error "Sign failed: %S"
1559 (epg-context-result-for context 'error))
1560 (error "Sign failed")))
1561 (if (epg-context-result-for context 'error)
1562 (error "Encrypt failed: %S"
1563 (epg-context-result-for context 'error)))
1564 (epg-read-output context))
1565 (epg-delete-output-file context)
1566 (epg-reset context)))
1569 (defun epg-start-export-keys (context keys)
1570 "Initiate an export keys operation.
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-export-keys-to-file' or `epg-export-keys-to-string' instead."
1577 (epg-context-set-result context nil)
1578 (epg-start context (cons "--export"
1582 (car (epg-key-sub-key-list key))))
1586 (defun epg-export-keys-to-file (context keys file)
1587 "Extract public KEYS."
1591 (epg-context-set-output-file context file)
1592 (epg-context-set-output-file context
1593 (epg-make-temp-file "epg-output")))
1594 (epg-start-export-keys context keys)
1595 (epg-wait-for-completion context)
1596 (if (epg-context-result-for context 'error)
1597 (error "Export keys failed: %S"
1598 (epg-context-result-for context 'error)))
1600 (epg-read-output context)))
1602 (epg-delete-output-file context))
1603 (epg-reset context)))
1606 (defun epg-export-keys-to-string (context keys)
1607 "Extract public KEYS and return them as a string."
1608 (epg-export-keys-to-file context keys nil))
1611 (defun epg-start-import-keys (context keys)
1612 "Initiate an import keys operation.
1613 KEYS is a data object.
1615 If you use this function, you will need to wait for the completion of
1616 `epg-gpg-program' by using `epg-wait-for-completion' and call
1617 `epg-reset' to clear a temporaly output file.
1618 If you are unsure, use synchronous version of this function
1619 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1620 (epg-context-set-result context nil)
1621 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
1622 (epg-start context (list "--import" (epg-data-file keys)))
1623 (if (and (epg-data-string keys)
1624 (eq (process-status (epg-context-process context)) 'run))
1625 (process-send-string (epg-context-process context)
1626 (epg-data-string keys))))
1628 (defun epg-import-keys-1 (context keys)
1631 (epg-start-import-keys context keys)
1632 (if (epg-data-file keys)
1633 (epg-flush context))
1634 (epg-wait-for-completion context)
1635 (if (epg-context-result-for context 'error)
1636 (error "Import keys failed: %S"
1637 (epg-context-result-for context 'error)))
1638 (epg-read-output context))
1639 (epg-reset context)))
1642 (defun epg-import-keys-from-file (context keys)
1643 "Add keys from a file KEYS."
1644 (epg-import-keys-1 context (epg-make-data-from-file keys)))
1647 (defun epg-import-keys-from-string (context keys)
1648 "Add keys from a string KEYS."
1649 (epg-import-keys-1 context (epg-make-data-from-string keys)))
1652 (defun epg-start-delete-keys (context keys &optional allow-secret)
1653 "Initiate an delete 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-delete-keys' instead."
1660 (epg-context-set-result context nil)
1661 (epg-start context (cons (if allow-secret
1662 "--delete-secret-key"
1667 (car (epg-key-sub-key-list key))))
1671 (defun epg-delete-keys (context keys &optional allow-secret)
1672 "Delete KEYS from the key ring."
1675 (epg-start-delete-keys context keys allow-secret)
1676 (epg-wait-for-completion context)
1677 (if (epg-context-result-for context 'error)
1678 (error "Delete keys failed: %S"
1679 (epg-context-result-for context 'error))))
1680 (epg-reset context)))
1683 (defun epg-start-sign-keys (context keys &optional local)
1684 "Initiate an sign keys operation.
1686 If you use this function, you will need to wait for the completion of
1687 `epg-gpg-program' by using `epg-wait-for-completion' and call
1688 `epg-reset' to clear a temporaly output file.
1689 If you are unsure, use synchronous version of this function
1690 `epg-sign-keys' instead."
1691 (epg-context-set-result context nil)
1692 (epg-start context (cons (if local
1698 (car (epg-key-sub-key-list key))))
1702 (defun epg-sign-keys (context keys &optional local)
1703 "Sign KEYS from the key ring."
1706 (epg-start-sign-keys context keys local)
1707 (epg-wait-for-completion context)
1708 (if (epg-context-result-for context 'error)
1709 (error "Sign keys failed: %S"
1710 (epg-context-result-for context 'error))))
1711 (epg-reset context)))
1713 (defun epg-decode-hexstring (string)
1715 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
1716 (setq string (replace-match "\\x\\&" t nil string)
1718 (car (read-from-string (concat "\"" string "\"")))))
1720 (defun epg-decode-quotedstring (string)
1722 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
1723 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
1725 (if (match-beginning 2)
1726 (setq string (replace-match "\\2" t nil string)
1728 (if (match-beginning 3)
1729 (setq string (replace-match "\\x\\3" t nil string)
1731 (setq string (replace-match "\\\\\\\\\\4" t nil string)
1732 index (+ index 3)))))
1733 (car (read-from-string (concat "\"" string "\"")))))
1735 (defun epg-dn-from-string (string)
1736 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
1737 The return value is an alist mapping from types to values."
1739 (length (length string))
1740 alist type value group)
1741 (while (< index length)
1742 (if (eq index (string-match "[ \t\n\r]*" string index))
1743 (setq index (match-end 0)))
1744 (if (eq index (string-match
1745 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
1747 (setq type (match-string 1 string)
1748 index (match-end 0))
1749 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
1751 (setq type (match-string 1 string)
1752 index (match-end 0))))
1754 (error "Invalid type"))
1755 (if (eq index (string-match
1756 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
1758 (setq index (match-end 0)
1759 value (epg-decode-quotedstring (match-string 0 string)))
1760 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
1761 (setq index (match-end 0)
1762 value (epg-decode-hexstring (match-string 1 string)))
1763 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
1765 (setq index (match-end 0)
1766 value (epg-decode-quotedstring (match-string 0 string))))))
1768 (if (stringp (car (car alist)))
1769 (setcar alist (list (cons type value) (car alist)))
1770 (setcar alist (cons (cons type value) (car alist))))
1771 (if (consp (car (car alist)))
1772 (setcar alist (nreverse (car alist))))
1773 (setq alist (cons (cons type value) alist)
1776 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
1777 (setq index (match-end 0)
1778 group (eq (aref string (match-beginning 1)) ?+))))
1781 (defun epg-decode-dn (alist)
1782 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
1783 Type names are resolved using `epg-dn-type-alist'."
1786 (if (stringp (car rdn))
1787 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
1789 (format "%s=%s" (cdr entry) (cdr rdn))
1790 (format "%s=%s" (car rdn) (cdr rdn))))
1791 (concat "(" (epg-decode-dn rdn) ")")))
1797 ;;; epg.el ends here