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 string)
542 "Return a user ID object."
543 (cons 'epg-user-id (vector validity string 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-string (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 "Return the result of CONTEXT associated with NAME."
571 (cdr (assq name (epg-context-result context))))
573 (defun epg-context-set-result-for (context name value)
574 "Set the result of CONTEXT associated with NAME to VALUE."
575 (let* ((result (epg-context-result context))
576 (entry (assq name result)))
579 (epg-context-set-result context (cons (cons name value) result)))))
581 (defun epg-signature-to-string (signature)
582 "Convert SIGNATURE to a human readable string."
583 (let ((user-id (cdr (assoc (epg-signature-key-id signature)
584 epg-user-id-alist))))
586 (cond ((eq (epg-signature-status signature) 'good)
587 "Good signature from ")
588 ((eq (epg-signature-status signature) 'bad)
589 "Bad signature from ")
590 ((eq (epg-signature-status signature) 'expired)
591 "Expired signature from ")
592 ((eq (epg-signature-status signature) 'expired-key)
593 "Signature made by expired key ")
594 ((eq (epg-signature-status signature) 'revoked-key)
595 "Signature made by revoked key ")
596 ((eq (epg-signature-status signature) 'no-pubkey)
597 "No public key for "))
598 (epg-signature-key-id signature)
601 (concat (if (stringp user-id)
603 (epg-decode-dn user-id))
606 (if (epg-signature-validity signature)
607 (format "(trust %s)" (epg-signature-validity signature))
610 (defun epg-verify-result-to-string (verify-result)
611 "Convert VERIFY-RESULT to a human readable string."
612 (mapconcat #'epg-signature-to-string verify-result "\n"))
614 (defun epg-start (context args)
615 "Start `epg-gpg-program' in a subprocess with given ARGS."
616 (if (eq (process-status (epg-context-process context)) 'run)
617 (error "%s is already running in this context"
618 (if (eq (epg-context-protocol context) 'CMS)
621 (let* ((args (append (list "--no-tty"
624 (unless (eq (epg-context-protocol context) 'CMS)
625 (list "--command-fd" "0"))
626 (if (epg-context-armor context) '("--armor"))
627 (if (epg-context-textmode context) '("--textmode"))
628 (if (epg-context-output-file context)
629 (list "--output" (epg-context-output-file context)))
631 (coding-system-for-write 'binary)
632 process-connection-type
633 (orig-mode (default-file-modes))
634 (buffer (generate-new-buffer " *epg*"))
638 (unless epg-debug-buffer
639 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
640 (set-buffer epg-debug-buffer)
641 (goto-char (point-max))
642 (insert (format "%s %s\n"
643 (if (eq (epg-context-protocol context) 'CMS)
646 (mapconcat #'identity args " ")))))
647 (with-current-buffer buffer
648 (make-local-variable 'epg-read-point)
649 (setq epg-read-point (point-min))
650 (make-local-variable 'epg-pending-status-list)
651 (setq epg-pending-status-list nil)
652 (make-local-variable 'epg-key-id)
653 (setq epg-key-id nil)
654 (make-local-variable 'epg-context)
655 (setq epg-context context))
658 (set-default-file-modes 448)
660 (apply #'start-process "epg" buffer
661 (if (eq (epg-context-protocol context) 'CMS)
665 (set-default-file-modes orig-mode))
666 (set-process-filter process #'epg-process-filter)
667 (epg-context-set-process context process)))
669 (defun epg-process-filter (process input)
672 (unless epg-debug-buffer
673 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
674 (set-buffer epg-debug-buffer)
675 (goto-char (point-max))
677 (if (buffer-live-p (process-buffer process))
679 (set-buffer (process-buffer process))
680 (goto-char (point-max))
682 (goto-char epg-read-point)
684 (while (looking-at ".*\n") ;the input line finished
686 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
687 (let* ((status (match-string 1))
688 (string (match-string 2))
689 (symbol (intern-soft (concat "epg-status-" status))))
690 (if (member status epg-pending-status-list)
691 (setq epg-pending-status-list nil))
694 (funcall symbol process string)))))
696 (setq epg-read-point (point)))))
698 (defun epg-read-output (context)
699 "Read the output file CONTEXT and return the content as a string."
701 (if (fboundp 'set-buffer-multibyte)
702 (set-buffer-multibyte nil))
703 (if (file-exists-p (epg-context-output-file context))
704 (let ((coding-system-for-read 'binary))
705 (insert-file-contents (epg-context-output-file context))
708 (defun epg-wait-for-status (context status-list)
709 "Wait until one of elements in STATUS-LIST arrives."
710 (with-current-buffer (process-buffer (epg-context-process context))
711 (setq epg-pending-status-list status-list)
712 (while (and (eq (process-status (epg-context-process context)) 'run)
713 epg-pending-status-list)
714 (accept-process-output (epg-context-process context) 0 1))))
716 (defun epg-wait-for-completion (context)
717 "Wait until the `epg-gpg-program' process completes."
718 (while (eq (process-status (epg-context-process context)) 'run)
719 (accept-process-output (epg-context-process context) 0 1)))
721 (defun epg-flush (context)
722 "Flush the input to the `epg-gpg-program' process."
723 (if (eq (process-status (epg-context-process context)) 'run)
724 (process-send-eof (epg-context-process context))))
726 (defun epg-reset (context)
728 (if (and (epg-context-process context)
729 (buffer-live-p (process-buffer (epg-context-process context))))
730 (kill-buffer (process-buffer (epg-context-process context))))
731 (epg-context-set-process context nil))
733 (defun epg-delete-output-file (context)
734 "Delete the output file of CONTEXT."
735 (if (and (epg-context-output-file context)
736 (file-exists-p (epg-context-output-file context)))
737 (delete-file (epg-context-output-file context))))
739 (defun epg-status-USERID_HINT (process string)
740 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
741 (let* ((key-id (match-string 1 string))
742 (user-id (match-string 2 string))
743 (entry (assoc key-id epg-user-id-alist)))
745 (setcdr entry user-id)
746 (setq epg-user-id-alist (cons (cons key-id user-id)
747 epg-user-id-alist))))))
749 (defun epg-status-NEED_PASSPHRASE (process string)
750 (if (string-match "\\`\\([^ ]+\\)" string)
751 (setq epg-key-id (match-string 1 string))))
753 (defun epg-status-NEED_PASSPHRASE_SYM (process string)
754 (setq epg-key-id 'SYM))
756 (defun epg-status-NEED_PASSPHRASE_PIN (process string)
757 (setq epg-key-id 'PIN))
759 (defun epg-status-GET_HIDDEN (process string)
761 (string-match "\\`passphrase\\." string))
764 passphrase-with-new-line)
770 (if (consp (epg-context-passphrase-callback
772 (car (epg-context-passphrase-callback
774 (epg-context-passphrase-callback epg-context))
777 (if (consp (epg-context-passphrase-callback
779 (cdr (epg-context-passphrase-callback
782 (setq passphrase-with-new-line (concat passphrase "\n"))
783 (fillarray passphrase 0)
784 (setq passphrase nil)
785 (process-send-string process passphrase-with-new-line)))
787 (epg-context-set-result-for
790 (epg-context-result-for epg-context 'error)))
791 (delete-process process)))
793 (fillarray passphrase 0))
794 (if passphrase-with-new-line
795 (fillarray passphrase-with-new-line 0))))))
797 (defun epg-status-GET_BOOL (process string)
798 (let ((entry (assoc string epg-prompt-alist))
801 (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
802 (process-send-string process "y\n")
803 (process-send-string process "n\n"))
805 (epg-context-set-result-for
808 (epg-context-result-for epg-context 'error)))
809 (delete-process process)))))
811 (defun epg-status-GET_LINE (process string)
812 (let ((entry (assoc string epg-prompt-alist))
817 (concat (read-string (if entry (cdr entry) (concat string ": ")))
820 (epg-context-set-result-for
823 (epg-context-result-for epg-context 'error)))
824 (delete-process process)))))
826 (defun epg-status-*SIG (status string)
827 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
828 (let* ((key-id (match-string 1 string))
829 (user-id (match-string 2 string))
830 (entry (assoc key-id epg-user-id-alist)))
831 (epg-context-set-result-for
834 (cons (epg-make-signature status key-id)
835 (epg-context-result-for epg-context 'verify)))
836 (if (eq (epg-context-protocol epg-context) 'CMS)
838 (setq user-id (epg-dn-from-string user-id))
841 (setcdr entry user-id)
842 (setq epg-user-id-alist
843 (cons (cons key-id user-id) epg-user-id-alist))))
844 (epg-context-set-result-for
847 (cons (epg-make-signature status)
848 (epg-context-result-for epg-context 'verify)))))
850 (defun epg-status-GOODSIG (process string)
851 (epg-status-*SIG 'good string))
853 (defun epg-status-EXPSIG (process string)
854 (epg-status-*SIG 'expired string))
856 (defun epg-status-EXPKEYSIG (process string)
857 (epg-status-*SIG 'expired-key string))
859 (defun epg-status-REVKEYSIG (process string)
860 (epg-status-*SIG 'revoked-key string))
862 (defun epg-status-BADSIG (process string)
863 (epg-status-*SIG 'bad string))
865 (defun epg-status-NO_PUBKEY (process string)
866 (epg-context-set-result-for
869 (cons (epg-make-signature 'no-pubkey string)
870 (epg-context-result-for epg-context 'verify))))
872 (defun epg-status-ERRSIG (process string)
873 (let ((signatures (car (epg-context-result-for epg-context 'verify))))
875 (setq signatures (list (epg-make-signature 'error)))
876 (epg-context-set-result-for epg-context 'verify signatures))
877 (when (and (not (eq (epg-signature-status (car signatures)) 'good))
878 (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
879 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
881 (epg-signature-set-key-id
883 (match-string 1 string))
884 (epg-signature-set-pubkey-algorithm
886 (string-to-number (match-string 2 string)))
887 (epg-signature-set-digest-algorithm
889 (string-to-number (match-string 3 string)))
890 ; (epg-signature-set-class
892 ; (string-to-number (match-string 4 string) 16))
893 (epg-signature-set-creation-time
895 (match-string 5 string)))))
897 (defun epg-status-VALIDSIG (process string)
898 (let ((signature (car (epg-context-result-for epg-context 'verify))))
900 (eq (epg-signature-status signature) 'good)
901 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
902 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
905 (epg-signature-set-fingerprint
907 (match-string 1 string))
908 (epg-signature-set-creation-time
910 (match-string 2 string))
911 (epg-signature-set-expiration-time
913 (match-string 3 string))
914 ; (epg-signature-set-version
916 ; (string-to-number (match-string 4 string)))
917 (epg-signature-set-pubkey-algorithm
919 (string-to-number (match-string 5 string)))
920 (epg-signature-set-digest-algorithm
922 (string-to-number (match-string 6 string)))
923 ; (epg-signature-set-class
925 ; (string-to-number (match-string 7 string) 16))
928 (defun epg-status-TRUST_UNDEFINED (process string)
929 (let ((signature (car (epg-context-result-for epg-context 'verify))))
931 (eq (epg-signature-status signature) 'good))
932 (epg-signature-set-validity signature 'undefined))))
934 (defun epg-status-TRUST_NEVER (process string)
935 (let ((signature (car (epg-context-result-for epg-context 'verify))))
937 (eq (epg-signature-status signature) 'good))
938 (epg-signature-set-validity signature 'never))))
940 (defun epg-status-TRUST_MARGINAL (process string)
941 (let ((signature (car (epg-context-result-for epg-context 'verify))))
943 (eq (epg-signature-status signature) 'marginal))
944 (epg-signature-set-validity signature 'marginal))))
946 (defun epg-status-TRUST_FULLY (process string)
947 (let ((signature (car (epg-context-result-for epg-context 'verify))))
949 (eq (epg-signature-status signature) 'good))
950 (epg-signature-set-validity signature 'full))))
952 (defun epg-status-TRUST_ULTIMATE (process string)
953 (let ((signature (car (epg-context-result-for epg-context 'verify))))
955 (eq (epg-signature-status signature) 'good))
956 (epg-signature-set-validity signature 'ultimate))))
958 (defun epg-status-PROGRESS (process string)
959 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
961 (funcall (if (consp (epg-context-progress-callback epg-context))
962 (car (epg-context-progress-callback epg-context))
963 (epg-context-progress-callback epg-context))
965 (match-string 1 string)
966 (match-string 2 string)
967 (string-to-number (match-string 3 string))
968 (string-to-number (match-string 4 string))
969 (if (consp (epg-context-progress-callback epg-context))
970 (cdr (epg-context-progress-callback epg-context))))))
972 (defun epg-status-DECRYPTION_FAILED (process string)
973 (epg-context-set-result-for
975 (cons '(decryption-failed)
976 (epg-context-result-for epg-context 'error))))
978 (defun epg-status-NODATA (process string)
979 (epg-context-set-result-for
981 (cons (cons 'no-data (string-to-number string))
982 (epg-context-result-for epg-context 'error))))
984 (defun epg-status-UNEXPECTED (process string)
985 (epg-context-set-result-for
987 (cons (cons 'unexpected (string-to-number string))
988 (epg-context-result-for epg-context 'error))))
990 (defun epg-status-KEYEXPIRED (process string)
991 (epg-context-set-result-for
993 (cons (cons 'key-expired string)
994 (epg-context-result-for epg-context 'error))))
996 (defun epg-status-KEYREVOKED (process string)
997 (epg-context-set-result-for
1000 (epg-context-result-for epg-context 'error))))
1002 (defun epg-status-BADARMOR (process string)
1003 (epg-context-set-result-for
1006 (epg-context-result-for epg-context 'error))))
1008 (defun epg-status-INV_RECP (process string)
1009 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1010 (epg-context-set-result-for
1012 (cons (list 'invalid-recipient
1013 (string-to-number (match-string 1 string))
1014 (match-string 2 string))
1015 (epg-context-result-for epg-context 'error)))))
1017 (defun epg-status-NO_RECP (process string)
1018 (epg-context-set-result-for
1020 (cons '(no-recipients)
1021 (epg-context-result-for epg-context 'error))))
1023 (defun epg-status-DELETE_PROBLEM (process string)
1024 (if (string-match "\\`\\([0-9]+\\)" string)
1025 (epg-context-set-result-for
1027 (cons (cons 'delete-problem (string-to-number (match-string 1 string)))
1028 (epg-context-result-for epg-context 'error)))))
1030 (defun epg-status-SIG_CREATED (process string)
1031 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1032 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1033 (epg-context-set-result-for
1035 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1036 (cons 'pubkey-algorithm
1037 (string-to-number (match-string 2 string)))
1038 (cons 'digest-algorithm
1039 (string-to-number (match-string 3 string)))
1040 (cons 'class (string-to-number (match-string 4 string) 16))
1041 (cons 'creation-time (match-string 5 string))
1042 (cons 'fingerprint (substring string (match-end 0))))
1043 (epg-context-result-for epg-context 'sign)))))
1045 (defun epg-passphrase-callback-function (context key-id handback)
1047 (if (eq key-id 'SYM)
1048 "Passphrase for symmetric encryption: "
1049 (if (eq key-id 'PIN)
1050 "Passphrase for PIN: "
1051 (let ((entry (assoc key-id epg-user-id-alist)))
1053 (format "Passphrase for %s %s: " key-id (cdr entry))
1054 (format "Passphrase for %s: " key-id)))))))
1056 (defun epg-progress-callback-function (context what char current total
1058 (message "%s: %d%%/%d%%" what current total))
1060 (defun epg-configuration ()
1061 "Return a list of internal configuration parameters of `epg-gpg-program'."
1064 (apply #'call-process epg-gpg-program nil (list t nil) nil
1065 '("--with-colons" "--list-config"))
1066 (goto-char (point-min))
1067 (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
1068 (setq type (intern (match-string 1))
1069 config (cons (cons type
1071 '(pubkey cipher digest compress))
1072 (mapcar #'string-to-number
1073 (delete "" (split-string
1080 (defun epg-list-keys-1 (context name mode)
1081 (let ((args (append (list "--with-colons" "--no-greeting" "--batch"
1082 "--with-fingerprint"
1083 "--with-fingerprint"
1084 (if (or (eq mode t) (eq mode 'secret))
1085 "--list-secret-keys"
1089 (unless (eq (epg-context-protocol context) 'CMS)
1090 '("--fixed-list-mode"))
1091 (if name (list name))))
1092 keys string field index)
1094 (apply #'call-process
1095 (if (eq (epg-context-protocol context) 'CMS)
1098 nil (list t nil) nil args)
1099 (goto-char (point-min))
1100 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1101 (setq keys (cons (make-vector 15 nil) keys)
1102 string (match-string 0)
1106 (string-match "\\([^:]+\\)?:" string index))
1107 (setq index (match-end 0))
1108 (aset (car keys) field (match-string 1 string))
1109 (setq field (1+ field))))
1112 (defun epg-make-sub-key-1 (line)
1115 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1117 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1119 (member (aref line 0) '("sec" "ssb"))
1120 (string-to-number (aref line 3))
1121 (string-to-number (aref line 2))
1126 (defun epg-list-keys-postprocess-one-key (key)
1127 (let (key-id user-id-string entry)
1128 (epg-key-set-sub-key-list
1130 (nreverse (epg-key-sub-key-list key)))
1131 (epg-key-set-user-id-list
1133 (nreverse (epg-key-user-id-list key)))
1135 (epg-sub-key-id (car (epg-key-sub-key-list key)))
1137 (epg-user-id-string (car (epg-key-user-id-list key)))
1138 entry (assoc key-id epg-user-id-alist))
1140 (setcdr entry user-id-string)
1141 (setq epg-user-id-alist (cons (cons key-id user-id-string)
1142 epg-user-id-alist)))))
1144 (defun epg-list-keys (context &optional name mode)
1145 "Return a list of epg-key objects matched with NAME.
1146 If MODE is nil, only public keyring should be searched.
1147 If MODE is t or 'secret, only secret keyring should be searched.
1148 Otherwise, only public keyring should be searched and the key
1149 signatures should be included."
1150 (let ((lines (epg-list-keys-1 context name mode))
1154 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1156 (epg-list-keys-postprocess-one-key (car keys)))
1157 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1158 keys (cons (epg-make-key
1159 (if (aref (car lines) 8)
1160 (cdr (assq (string-to-char (aref (car lines) 8))
1161 epg-key-validity-alist))))
1163 (epg-key-set-sub-key-list
1165 (cons (epg-make-sub-key-1 (car lines))
1166 (epg-key-sub-key-list (car keys)))))
1167 ((member (aref (car lines) 0) '("sub" "ssb"))
1168 (epg-key-set-sub-key-list
1170 (cons (epg-make-sub-key-1 (car lines))
1171 (epg-key-sub-key-list (car keys)))))
1172 ((equal (aref (car lines) 0) "uid")
1173 (epg-key-set-user-id-list
1175 (cons (epg-make-user-id
1176 (if (aref (car lines) 1)
1177 (cdr (assq (string-to-char (aref (car lines) 1))
1178 epg-key-validity-alist)))
1181 (epg-dn-from-string (aref (car lines) 9))
1182 (error (aref (car lines) 9)))
1183 (aref (car lines) 9)))
1184 (epg-key-user-id-list (car keys)))))
1185 ((equal (aref (car lines) 0) "fpr")
1186 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1187 (aref (car lines) 9))))
1188 (setq lines (cdr lines)))
1190 (epg-list-keys-postprocess-one-key (car keys)))
1193 (if (fboundp 'make-temp-file)
1194 (defalias 'epg-make-temp-file 'make-temp-file)
1195 (defvar temporary-file-directory)
1196 ;; stolen from poe.el.
1197 (defun epg-make-temp-file (prefix)
1198 "Create a temporary file.
1199 The returned file name (created by appending some random characters at the end
1200 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1201 is guaranteed to point to a newly created empty file.
1202 You can then use `write-region' to write new data into the file."
1203 (let (tempdir tempfile)
1204 (setq prefix (expand-file-name prefix
1205 (if (featurep 'xemacs)
1207 temporary-file-directory)))
1210 ;; First, create a temporary directory.
1211 (while (condition-case ()
1213 (setq tempdir (make-temp-name
1215 (file-name-directory prefix)
1217 ;; return nil or signal an error.
1218 (make-directory tempdir))
1220 (file-already-exists t)))
1221 (set-file-modes tempdir 448)
1222 ;; Second, create a temporary file in the tempdir.
1223 ;; There *is* a race condition between `make-temp-name'
1224 ;; and `write-region', but we don't care it since we are
1225 ;; in a private directory now.
1226 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1227 (write-region "" nil tempfile nil 'silent)
1228 (set-file-modes tempfile 384)
1229 ;; Finally, make a hard-link from the tempfile.
1230 (while (condition-case ()
1232 (setq file (make-temp-name prefix))
1233 ;; return nil or signal an error.
1234 (add-name-to-file tempfile file))
1236 (file-already-exists t)))
1238 ;; Cleanup the tempfile.
1240 (file-exists-p tempfile)
1241 (delete-file tempfile))
1242 ;; Cleanup the tempdir.
1244 (file-directory-p tempdir)
1245 (delete-directory tempdir))))))
1248 (defun epg-cancel (context)
1249 (if (buffer-live-p (process-buffer (epg-context-process context)))
1251 (set-buffer (process-buffer (epg-context-process context)))
1252 (epg-context-set-result-for
1255 (epg-context-result-for epg-context 'error)))))
1256 (if (eq (process-status (epg-context-process context)) 'run)
1257 (delete-process (epg-context-process context))))
1260 (defun epg-start-decrypt (context cipher)
1261 "Initiate a decrypt operation on CIPHER.
1262 CIPHER is a data object.
1264 If you use this function, you will need to wait for the completion of
1265 `epg-gpg-program' by using `epg-wait-for-completion' and call
1266 `epg-reset' to clear a temporaly output file.
1267 If you are unsure, use synchronous version of this function
1268 `epg-decrypt-file' or `epg-decrypt-string' instead."
1269 (unless (epg-data-file cipher)
1270 (error "Not a file"))
1271 (epg-context-set-result context nil)
1272 (epg-start context (list "--decrypt" (epg-data-file cipher)))
1273 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1274 (unless (eq (epg-context-protocol context) 'CMS)
1275 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1278 (defun epg-decrypt-file (context cipher plain)
1279 "Decrypt a file CIPHER and store the result to a file PLAIN.
1280 If PLAIN is nil, it returns the result as a string."
1284 (epg-context-set-output-file context plain)
1285 (epg-context-set-output-file context
1286 (epg-make-temp-file "epg-output")))
1287 (epg-start-decrypt context (epg-make-data-from-file cipher))
1288 (epg-wait-for-completion context)
1289 (if (epg-context-result-for context 'error)
1290 (error "Decrypt failed: %S"
1291 (epg-context-result-for context 'error)))
1293 (epg-read-output context)))
1295 (epg-delete-output-file context))
1296 (epg-reset context)))
1299 (defun epg-decrypt-string (context cipher)
1300 "Decrypt a string CIPHER and return the plain text."
1301 (let ((input-file (epg-make-temp-file "epg-input"))
1302 (coding-system-for-write 'binary))
1305 (write-region cipher nil input-file nil 'quiet)
1306 (epg-context-set-output-file context
1307 (epg-make-temp-file "epg-output"))
1308 (epg-start-decrypt context (epg-make-data-from-file input-file))
1310 (epg-wait-for-completion context)
1311 (if (epg-context-result-for context 'error)
1312 (error "Decrypt failed: %S"
1313 (epg-context-result-for context 'error)))
1314 (epg-read-output context))
1315 (epg-delete-output-file context)
1316 (if (file-exists-p input-file)
1317 (delete-file input-file))
1318 (epg-reset context))))
1321 (defun epg-start-verify (context signature &optional signed-text)
1322 "Initiate a verify operation on SIGNATURE.
1323 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1325 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1326 For a normal or a clear text signature, SIGNED-TEXT should be nil.
1328 If you use this function, you will need to wait for the completion of
1329 `epg-gpg-program' by using `epg-wait-for-completion' and call
1330 `epg-reset' to clear a temporaly output file.
1331 If you are unsure, use synchronous version of this function
1332 `epg-verify-file' or `epg-verify-string' instead."
1333 (epg-context-set-result context nil)
1335 ;; Detached signature.
1336 (if (epg-data-file signed-text)
1337 (epg-start context (list "--verify" (epg-data-file signature)
1338 (epg-data-file signed-text)))
1339 (epg-start context (list "--verify" (epg-data-file signature) "-"))
1340 (if (eq (process-status (epg-context-process context)) 'run)
1341 (process-send-string (epg-context-process context)
1342 (epg-data-string signed-text))))
1343 ;; Normal (or cleartext) signature.
1344 (if (epg-data-file signature)
1345 (epg-start context (list "--verify" (epg-data-file signature)))
1346 (epg-start context (list "--verify"))
1347 (if (eq (process-status (epg-context-process context)) 'run)
1348 (process-send-string (epg-context-process context)
1349 (epg-data-string signature))))))
1352 (defun epg-verify-file (context signature &optional signed-text plain)
1353 "Verify a file SIGNATURE.
1354 SIGNED-TEXT and PLAIN are also a file if they are specified.
1356 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1357 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1361 (epg-context-set-output-file context plain)
1362 (epg-context-set-output-file context
1363 (epg-make-temp-file "epg-output")))
1365 (epg-start-verify context
1366 (epg-make-data-from-file signature)
1367 (epg-make-data-from-file signed-text))
1368 (epg-start-verify context
1369 (epg-make-data-from-file signature)))
1370 (epg-wait-for-completion context)
1371 ; (if (epg-context-result-for context 'error)
1372 ; (error "Verify failed: %S"
1373 ; (epg-context-result-for context 'error)))
1375 (epg-read-output context)))
1377 (epg-delete-output-file context))
1378 (epg-reset context)))
1381 (defun epg-verify-string (context signature &optional signed-text)
1382 "Verify a string SIGNATURE.
1383 SIGNED-TEXT is a string if it is specified.
1385 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1386 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1387 (let ((coding-system-for-write 'binary)
1391 (epg-context-set-output-file context
1392 (epg-make-temp-file "epg-output"))
1395 (setq input-file (epg-make-temp-file "epg-signature"))
1396 (write-region signature nil input-file nil 'quiet)
1397 (epg-start-verify context
1398 (epg-make-data-from-file input-file)
1399 (epg-make-data-from-string signed-text)))
1400 (epg-start-verify context (epg-make-data-from-string signature)))
1402 (epg-wait-for-completion context)
1403 ; (if (epg-context-result-for context 'error)
1404 ; (error "Verify failed: %S"
1405 ; (epg-context-result-for context 'error)))
1406 (epg-read-output context))
1407 (epg-delete-output-file context)
1409 (file-exists-p input-file))
1410 (delete-file input-file))
1411 (epg-reset context))))
1414 (defun epg-start-sign (context plain &optional mode)
1415 "Initiate a sign operation on PLAIN.
1416 PLAIN is a data object.
1418 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1419 If MODE is t or 'detached, it makes a detached signature.
1420 Otherwise, it makes a normal signature.
1422 If you use this function, you will need to wait for the completion of
1423 `epg-gpg-program' by using `epg-wait-for-completion' and call
1424 `epg-reset' to clear a temporaly output file.
1425 If you are unsure, use synchronous version of this function
1426 `epg-sign-file' or `epg-sign-string' instead."
1427 (epg-context-set-result context nil)
1429 (append (list (if (eq mode 'clearsign)
1431 (if (or (eq mode t) (eq mode 'detached))
1439 (car (epg-key-sub-key-list signer)))))
1440 (epg-context-signers context)))
1441 (if (epg-data-file plain)
1442 (list (epg-data-file plain)))))
1443 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1444 (unless (eq (epg-context-protocol context) 'CMS)
1445 (epg-wait-for-status context '("BEGIN_SIGNING")))
1446 (if (and (epg-data-string plain)
1447 (eq (process-status (epg-context-process context)) 'run))
1448 (process-send-string (epg-context-process context)
1449 (epg-data-string plain))))
1452 (defun epg-sign-file (context plain signature &optional mode)
1453 "Sign a file PLAIN and store the result to a file SIGNATURE.
1454 If SIGNATURE is nil, it returns the result as a string.
1455 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1456 If MODE is t or 'detached, it makes a detached signature.
1457 Otherwise, it makes a normal signature."
1461 (epg-context-set-output-file context signature)
1462 (epg-context-set-output-file context
1463 (epg-make-temp-file "epg-output")))
1464 (epg-start-sign context (epg-make-data-from-file plain) mode)
1465 (epg-wait-for-completion context)
1466 (if (epg-context-result-for context 'sign)
1467 (if (epg-context-result-for context 'error)
1468 (message "Sign warning: %S"
1469 (epg-context-result-for context 'error)))
1470 (if (epg-context-result-for context 'error)
1471 (error "Sign failed: %S"
1472 (epg-context-result-for context 'error))
1473 (error "Sign failed")))
1475 (epg-read-output context)))
1477 (epg-delete-output-file context))
1478 (epg-reset context)))
1481 (defun epg-sign-string (context plain &optional mode)
1482 "Sign a string PLAIN and return the output as string.
1483 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1484 If MODE is t or 'detached, it makes a detached signature.
1485 Otherwise, it makes a normal signature."
1488 (epg-context-set-output-file context
1489 (epg-make-temp-file "epg-output"))
1490 (epg-start-sign context (epg-make-data-from-string plain) mode)
1492 (epg-wait-for-completion context)
1493 (unless (epg-context-result-for context 'sign)
1494 (if (epg-context-result-for context 'error)
1495 (error "Sign failed: %S"
1496 (epg-context-result-for context 'error))
1497 (error "Sign failed")))
1498 (epg-read-output context))
1499 (epg-delete-output-file context)
1500 (epg-reset context)))
1503 (defun epg-start-encrypt (context plain recipients
1504 &optional sign always-trust)
1505 "Initiate an encrypt operation on PLAIN.
1506 PLAIN is a data object.
1507 If RECIPIENTS is nil, it performs symmetric encryption.
1509 If you use this function, you will need to wait for the completion of
1510 `epg-gpg-program' by using `epg-wait-for-completion' and call
1511 `epg-reset' to clear a temporaly output file.
1512 If you are unsure, use synchronous version of this function
1513 `epg-encrypt-file' or `epg-encrypt-string' instead."
1514 (epg-context-set-result context nil)
1516 (append (if always-trust '("--always-trust"))
1517 (if recipients '("--encrypt") '("--symmetric"))
1521 (mapcar (lambda (signer)
1523 (epg-context-signers context)))))
1529 (car (epg-key-sub-key-list recipient)))))
1531 (if (epg-data-file plain)
1532 (list (epg-data-file plain)))))
1533 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1534 (unless (eq (epg-context-protocol context) 'CMS)
1536 (epg-wait-for-status context '("BEGIN_SIGNING"))
1537 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
1538 (if (and (epg-data-string plain)
1539 (eq (process-status (epg-context-process context)) 'run))
1540 (process-send-string (epg-context-process context)
1541 (epg-data-string plain))))
1544 (defun epg-encrypt-file (context plain recipients
1545 cipher &optional sign always-trust)
1546 "Encrypt a file PLAIN and store the result to a file CIPHER.
1547 If CIPHER is nil, it returns the result as a string.
1548 If RECIPIENTS is nil, it performs symmetric encryption."
1552 (epg-context-set-output-file context cipher)
1553 (epg-context-set-output-file context
1554 (epg-make-temp-file "epg-output")))
1555 (epg-start-encrypt context (epg-make-data-from-file plain)
1556 recipients sign always-trust)
1557 (epg-wait-for-completion context)
1559 (not (epg-context-result-for context 'sign)))
1560 (if (epg-context-result-for context 'error)
1561 (error "Sign failed: %S"
1562 (epg-context-result-for context 'error))
1563 (error "Sign failed")))
1564 (if (epg-context-result-for context 'error)
1565 (error "Encrypt failed: %S"
1566 (epg-context-result-for context 'error)))
1568 (epg-read-output context)))
1570 (epg-delete-output-file context))
1571 (epg-reset context)))
1574 (defun epg-encrypt-string (context plain recipients
1575 &optional sign always-trust)
1576 "Encrypt a string PLAIN.
1577 If RECIPIENTS is nil, it performs symmetric encryption."
1580 (epg-context-set-output-file context
1581 (epg-make-temp-file "epg-output"))
1582 (epg-start-encrypt context (epg-make-data-from-string plain)
1583 recipients sign always-trust)
1585 (epg-wait-for-completion context)
1587 (not (epg-context-result-for context 'sign)))
1588 (if (epg-context-result-for context 'error)
1589 (error "Sign failed: %S"
1590 (epg-context-result-for context 'error))
1591 (error "Sign failed")))
1592 (if (epg-context-result-for context 'error)
1593 (error "Encrypt failed: %S"
1594 (epg-context-result-for context 'error)))
1595 (epg-read-output context))
1596 (epg-delete-output-file context)
1597 (epg-reset context)))
1600 (defun epg-start-export-keys (context keys)
1601 "Initiate an export keys operation.
1603 If you use this function, you will need to wait for the completion of
1604 `epg-gpg-program' by using `epg-wait-for-completion' and call
1605 `epg-reset' to clear a temporaly output file.
1606 If you are unsure, use synchronous version of this function
1607 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1608 (epg-context-set-result context nil)
1609 (epg-start context (cons "--export"
1613 (car (epg-key-sub-key-list key))))
1617 (defun epg-export-keys-to-file (context keys file)
1618 "Extract public KEYS."
1622 (epg-context-set-output-file context file)
1623 (epg-context-set-output-file context
1624 (epg-make-temp-file "epg-output")))
1625 (epg-start-export-keys context keys)
1626 (epg-wait-for-completion context)
1627 (if (epg-context-result-for context 'error)
1628 (error "Export keys failed: %S"
1629 (epg-context-result-for context 'error)))
1631 (epg-read-output context)))
1633 (epg-delete-output-file context))
1634 (epg-reset context)))
1637 (defun epg-export-keys-to-string (context keys)
1638 "Extract public KEYS and return them as a string."
1639 (epg-export-keys-to-file context keys nil))
1642 (defun epg-start-import-keys (context keys)
1643 "Initiate an import keys operation.
1644 KEYS is a data object.
1646 If you use this function, you will need to wait for the completion of
1647 `epg-gpg-program' by using `epg-wait-for-completion' and call
1648 `epg-reset' to clear a temporaly output file.
1649 If you are unsure, use synchronous version of this function
1650 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1651 (epg-context-set-result context nil)
1652 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
1653 (epg-start context (list "--import" (epg-data-file keys)))
1654 (if (and (epg-data-string keys)
1655 (eq (process-status (epg-context-process context)) 'run))
1656 (process-send-string (epg-context-process context)
1657 (epg-data-string keys))))
1659 (defun epg-import-keys-1 (context keys)
1662 (epg-start-import-keys context keys)
1663 (if (epg-data-file keys)
1664 (epg-flush context))
1665 (epg-wait-for-completion context)
1666 (if (epg-context-result-for context 'error)
1667 (error "Import keys failed: %S"
1668 (epg-context-result-for context 'error)))
1669 (epg-read-output context))
1670 (epg-reset context)))
1673 (defun epg-import-keys-from-file (context keys)
1674 "Add keys from a file KEYS."
1675 (epg-import-keys-1 context (epg-make-data-from-file keys)))
1678 (defun epg-import-keys-from-string (context keys)
1679 "Add keys from a string KEYS."
1680 (epg-import-keys-1 context (epg-make-data-from-string keys)))
1683 (defun epg-start-delete-keys (context keys &optional allow-secret)
1684 "Initiate an delete 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-delete-keys' instead."
1691 (epg-context-set-result context nil)
1692 (epg-start context (cons (if allow-secret
1693 "--delete-secret-key"
1698 (car (epg-key-sub-key-list key))))
1702 (defun epg-delete-keys (context keys &optional allow-secret)
1703 "Delete KEYS from the key ring."
1706 (epg-start-delete-keys context keys allow-secret)
1707 (epg-wait-for-completion context)
1708 (if (epg-context-result-for context 'error)
1709 (error "Delete keys failed: %S"
1710 (epg-context-result-for context 'error))))
1711 (epg-reset context)))
1714 (defun epg-start-sign-keys (context keys &optional local)
1715 "Initiate an sign keys operation.
1717 If you use this function, you will need to wait for the completion of
1718 `epg-gpg-program' by using `epg-wait-for-completion' and call
1719 `epg-reset' to clear a temporaly output file.
1720 If you are unsure, use synchronous version of this function
1721 `epg-sign-keys' instead."
1722 (epg-context-set-result context nil)
1723 (epg-start context (cons (if local
1729 (car (epg-key-sub-key-list key))))
1733 (defun epg-sign-keys (context keys &optional local)
1734 "Sign KEYS from the key ring."
1737 (epg-start-sign-keys context keys local)
1738 (epg-wait-for-completion context)
1739 (if (epg-context-result-for context 'error)
1740 (error "Sign keys failed: %S"
1741 (epg-context-result-for context 'error))))
1742 (epg-reset context)))
1744 (defun epg-decode-hexstring (string)
1746 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
1747 (setq string (replace-match "\\x\\&" t nil string)
1749 (car (read-from-string (concat "\"" string "\"")))))
1751 (defun epg-decode-quotedstring (string)
1753 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
1754 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
1756 (if (match-beginning 2)
1757 (setq string (replace-match "\\2" t nil string)
1759 (if (match-beginning 3)
1760 (setq string (replace-match "\\x\\3" t nil string)
1762 (setq string (replace-match "\\\\\\\\\\4" t nil string)
1763 index (+ index 3)))))
1764 (car (read-from-string (concat "\"" string "\"")))))
1766 (defun epg-dn-from-string (string)
1767 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
1768 The return value is an alist mapping from types to values."
1770 (length (length string))
1771 alist type value group)
1772 (while (< index length)
1773 (if (eq index (string-match "[ \t\n\r]*" string index))
1774 (setq index (match-end 0)))
1775 (if (eq index (string-match
1776 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
1778 (setq type (match-string 1 string)
1779 index (match-end 0))
1780 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
1782 (setq type (match-string 1 string)
1783 index (match-end 0))))
1785 (error "Invalid type"))
1786 (if (eq index (string-match
1787 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
1789 (setq index (match-end 0)
1790 value (epg-decode-quotedstring (match-string 0 string)))
1791 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
1792 (setq index (match-end 0)
1793 value (epg-decode-hexstring (match-string 1 string)))
1794 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
1796 (setq index (match-end 0)
1797 value (epg-decode-quotedstring (match-string 0 string))))))
1799 (if (stringp (car (car alist)))
1800 (setcar alist (list (cons type value) (car alist)))
1801 (setcar alist (cons (cons type value) (car alist))))
1802 (if (consp (car (car alist)))
1803 (setcar alist (nreverse (car alist))))
1804 (setq alist (cons (cons type value) alist)
1807 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
1808 (setq index (match-end 0)
1809 group (eq (aref string (match-beginning 1)) ?+))))
1812 (defun epg-decode-dn (alist)
1813 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
1814 Type names are resolved using `epg-dn-type-alist'."
1817 (if (stringp (car rdn))
1818 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
1820 (format "%s=%s" (cdr entry) (cdr rdn))
1821 (format "%s=%s" (car rdn) (cdr rdn))))
1822 (concat "(" (epg-decode-dn rdn) ")")))
1828 ;;; epg.el ends here