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 (and (epg-context-process context)
617 (eq (process-status (epg-context-process context)) 'run))
618 (error "%s is already running in this context"
619 (if (eq (epg-context-protocol context) 'CMS)
622 (let* ((args (append (list "--no-tty"
625 (unless (eq (epg-context-protocol context) 'CMS)
626 (list "--command-fd" "0"))
627 (if (epg-context-armor context) '("--armor"))
628 (if (epg-context-textmode context) '("--textmode"))
629 (if (epg-context-output-file context)
630 (list "--output" (epg-context-output-file context)))
632 (coding-system-for-write 'binary)
633 process-connection-type
634 (orig-mode (default-file-modes))
635 (buffer (generate-new-buffer " *epg*"))
639 (unless epg-debug-buffer
640 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
641 (set-buffer epg-debug-buffer)
642 (goto-char (point-max))
643 (insert (format "%s %s\n"
644 (if (eq (epg-context-protocol context) 'CMS)
647 (mapconcat #'identity args " ")))))
648 (with-current-buffer buffer
649 (make-local-variable 'epg-read-point)
650 (setq epg-read-point (point-min))
651 (make-local-variable 'epg-pending-status-list)
652 (setq epg-pending-status-list nil)
653 (make-local-variable 'epg-key-id)
654 (setq epg-key-id nil)
655 (make-local-variable 'epg-context)
656 (setq epg-context context))
659 (set-default-file-modes 448)
661 (apply #'start-process "epg" buffer
662 (if (eq (epg-context-protocol context) 'CMS)
666 (set-default-file-modes orig-mode))
667 (set-process-filter process #'epg-process-filter)
668 (epg-context-set-process context process)))
670 (defun epg-process-filter (process input)
673 (unless epg-debug-buffer
674 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
675 (set-buffer epg-debug-buffer)
676 (goto-char (point-max))
678 (if (buffer-live-p (process-buffer process))
680 (set-buffer (process-buffer process))
681 (goto-char (point-max))
683 (goto-char epg-read-point)
685 (while (looking-at ".*\n") ;the input line finished
687 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
688 (let* ((status (match-string 1))
689 (string (match-string 2))
690 (symbol (intern-soft (concat "epg-status-" status))))
691 (if (member status epg-pending-status-list)
692 (setq epg-pending-status-list nil))
695 (funcall symbol process string)))))
697 (setq epg-read-point (point)))))
699 (defun epg-read-output (context)
700 "Read the output file CONTEXT and return the content as a string."
702 (if (fboundp 'set-buffer-multibyte)
703 (set-buffer-multibyte nil))
704 (if (file-exists-p (epg-context-output-file context))
705 (let ((coding-system-for-read 'binary))
706 (insert-file-contents (epg-context-output-file context))
709 (defun epg-wait-for-status (context status-list)
710 "Wait until one of elements in STATUS-LIST arrives."
711 (with-current-buffer (process-buffer (epg-context-process context))
712 (setq epg-pending-status-list status-list)
713 (while (and (eq (process-status (epg-context-process context)) 'run)
714 epg-pending-status-list)
715 (accept-process-output (epg-context-process context) 0 1))))
717 (defun epg-wait-for-completion (context)
718 "Wait until the `epg-gpg-program' process completes."
719 (while (eq (process-status (epg-context-process context)) 'run)
720 (accept-process-output (epg-context-process context) 0 1)))
722 (defun epg-reset (context)
724 (if (and (epg-context-process context)
725 (buffer-live-p (process-buffer (epg-context-process context))))
726 (kill-buffer (process-buffer (epg-context-process context))))
727 (epg-context-set-process context nil))
729 (defun epg-delete-output-file (context)
730 "Delete the output file of CONTEXT."
731 (if (and (epg-context-output-file context)
732 (file-exists-p (epg-context-output-file context)))
733 (delete-file (epg-context-output-file context))))
735 (defun epg-status-USERID_HINT (process string)
736 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
737 (let* ((key-id (match-string 1 string))
738 (user-id (match-string 2 string))
739 (entry (assoc key-id epg-user-id-alist)))
741 (setcdr entry user-id)
742 (setq epg-user-id-alist (cons (cons key-id user-id)
743 epg-user-id-alist))))))
745 (defun epg-status-NEED_PASSPHRASE (process string)
746 (if (string-match "\\`\\([^ ]+\\)" string)
747 (setq epg-key-id (match-string 1 string))))
749 (defun epg-status-NEED_PASSPHRASE_SYM (process string)
750 (setq epg-key-id 'SYM))
752 (defun epg-status-NEED_PASSPHRASE_PIN (process string)
753 (setq epg-key-id 'PIN))
755 (defun epg-status-GET_HIDDEN (process string)
757 (string-match "\\`passphrase\\." string))
760 passphrase-with-new-line)
766 (if (consp (epg-context-passphrase-callback
768 (car (epg-context-passphrase-callback
770 (epg-context-passphrase-callback epg-context))
773 (if (consp (epg-context-passphrase-callback
775 (cdr (epg-context-passphrase-callback
778 (setq passphrase-with-new-line (concat passphrase "\n"))
779 (fillarray passphrase 0)
780 (setq passphrase nil)
781 (process-send-string process passphrase-with-new-line)))
783 (epg-context-set-result-for
786 (epg-context-result-for epg-context 'error)))
787 (delete-process process)))
789 (fillarray passphrase 0))
790 (if passphrase-with-new-line
791 (fillarray passphrase-with-new-line 0))))))
793 (defun epg-status-GET_BOOL (process string)
794 (let ((entry (assoc string epg-prompt-alist))
797 (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
798 (process-send-string process "y\n")
799 (process-send-string process "n\n"))
801 (epg-context-set-result-for
804 (epg-context-result-for epg-context 'error)))
805 (delete-process process)))))
807 (defun epg-status-GET_LINE (process string)
808 (let ((entry (assoc string epg-prompt-alist))
813 (concat (read-string (if entry (cdr entry) (concat string ": ")))
816 (epg-context-set-result-for
819 (epg-context-result-for epg-context 'error)))
820 (delete-process process)))))
822 (defun epg-status-*SIG (status string)
823 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
824 (let* ((key-id (match-string 1 string))
825 (user-id (match-string 2 string))
826 (entry (assoc key-id epg-user-id-alist)))
827 (epg-context-set-result-for
830 (cons (epg-make-signature status key-id)
831 (epg-context-result-for epg-context 'verify)))
832 (if (eq (epg-context-protocol epg-context) 'CMS)
834 (setq user-id (epg-dn-from-string user-id))
837 (setcdr entry user-id)
838 (setq epg-user-id-alist
839 (cons (cons key-id user-id) epg-user-id-alist))))
840 (epg-context-set-result-for
843 (cons (epg-make-signature status)
844 (epg-context-result-for epg-context 'verify)))))
846 (defun epg-status-GOODSIG (process string)
847 (epg-status-*SIG 'good string))
849 (defun epg-status-EXPSIG (process string)
850 (epg-status-*SIG 'expired string))
852 (defun epg-status-EXPKEYSIG (process string)
853 (epg-status-*SIG 'expired-key string))
855 (defun epg-status-REVKEYSIG (process string)
856 (epg-status-*SIG 'revoked-key string))
858 (defun epg-status-BADSIG (process string)
859 (epg-status-*SIG 'bad string))
861 (defun epg-status-NO_PUBKEY (process string)
862 (epg-context-set-result-for
865 (cons (epg-make-signature 'no-pubkey string)
866 (epg-context-result-for epg-context 'verify))))
868 (defun epg-status-ERRSIG (process string)
869 (let ((signatures (car (epg-context-result-for epg-context 'verify))))
871 (setq signatures (list (epg-make-signature 'error)))
872 (epg-context-set-result-for epg-context 'verify signatures))
873 (when (and (not (eq (epg-signature-status (car signatures)) 'good))
874 (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
875 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
877 (epg-signature-set-key-id
879 (match-string 1 string))
880 (epg-signature-set-pubkey-algorithm
882 (string-to-number (match-string 2 string)))
883 (epg-signature-set-digest-algorithm
885 (string-to-number (match-string 3 string)))
886 ; (epg-signature-set-class
888 ; (string-to-number (match-string 4 string) 16))
889 (epg-signature-set-creation-time
891 (match-string 5 string)))))
893 (defun epg-status-VALIDSIG (process string)
894 (let ((signature (car (epg-context-result-for epg-context 'verify))))
896 (eq (epg-signature-status signature) 'good)
897 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
898 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
901 (epg-signature-set-fingerprint
903 (match-string 1 string))
904 (epg-signature-set-creation-time
906 (match-string 2 string))
907 (epg-signature-set-expiration-time
909 (match-string 3 string))
910 ; (epg-signature-set-version
912 ; (string-to-number (match-string 4 string)))
913 (epg-signature-set-pubkey-algorithm
915 (string-to-number (match-string 5 string)))
916 (epg-signature-set-digest-algorithm
918 (string-to-number (match-string 6 string)))
919 ; (epg-signature-set-class
921 ; (string-to-number (match-string 7 string) 16))
924 (defun epg-status-TRUST_UNDEFINED (process string)
925 (let ((signature (car (epg-context-result-for epg-context 'verify))))
927 (eq (epg-signature-status signature) 'good))
928 (epg-signature-set-validity signature 'undefined))))
930 (defun epg-status-TRUST_NEVER (process string)
931 (let ((signature (car (epg-context-result-for epg-context 'verify))))
933 (eq (epg-signature-status signature) 'good))
934 (epg-signature-set-validity signature 'never))))
936 (defun epg-status-TRUST_MARGINAL (process string)
937 (let ((signature (car (epg-context-result-for epg-context 'verify))))
939 (eq (epg-signature-status signature) 'marginal))
940 (epg-signature-set-validity signature 'marginal))))
942 (defun epg-status-TRUST_FULLY (process string)
943 (let ((signature (car (epg-context-result-for epg-context 'verify))))
945 (eq (epg-signature-status signature) 'good))
946 (epg-signature-set-validity signature 'full))))
948 (defun epg-status-TRUST_ULTIMATE (process string)
949 (let ((signature (car (epg-context-result-for epg-context 'verify))))
951 (eq (epg-signature-status signature) 'good))
952 (epg-signature-set-validity signature 'ultimate))))
954 (defun epg-status-PROGRESS (process string)
955 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
957 (funcall (if (consp (epg-context-progress-callback epg-context))
958 (car (epg-context-progress-callback epg-context))
959 (epg-context-progress-callback epg-context))
961 (match-string 1 string)
962 (match-string 2 string)
963 (string-to-number (match-string 3 string))
964 (string-to-number (match-string 4 string))
965 (if (consp (epg-context-progress-callback epg-context))
966 (cdr (epg-context-progress-callback epg-context))))))
968 (defun epg-status-DECRYPTION_FAILED (process string)
969 (epg-context-set-result-for
971 (cons '(decryption-failed)
972 (epg-context-result-for epg-context 'error))))
974 (defun epg-status-NODATA (process string)
975 (epg-context-set-result-for
977 (cons (cons 'no-data (string-to-number string))
978 (epg-context-result-for epg-context 'error))))
980 (defun epg-status-UNEXPECTED (process string)
981 (epg-context-set-result-for
983 (cons (cons 'unexpected (string-to-number string))
984 (epg-context-result-for epg-context 'error))))
986 (defun epg-status-KEYEXPIRED (process string)
987 (epg-context-set-result-for
989 (cons (cons 'key-expired string)
990 (epg-context-result-for epg-context 'error))))
992 (defun epg-status-KEYREVOKED (process string)
993 (epg-context-set-result-for
996 (epg-context-result-for epg-context 'error))))
998 (defun epg-status-BADARMOR (process string)
999 (epg-context-set-result-for
1002 (epg-context-result-for epg-context 'error))))
1004 (defun epg-status-INV_RECP (process string)
1005 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1006 (epg-context-set-result-for
1008 (cons (list 'invalid-recipient
1009 (string-to-number (match-string 1 string))
1010 (match-string 2 string))
1011 (epg-context-result-for epg-context 'error)))))
1013 (defun epg-status-NO_RECP (process string)
1014 (epg-context-set-result-for
1016 (cons '(no-recipients)
1017 (epg-context-result-for epg-context 'error))))
1019 (defun epg-status-DELETE_PROBLEM (process string)
1020 (if (string-match "\\`\\([0-9]+\\)" string)
1021 (epg-context-set-result-for
1023 (cons (cons 'delete-problem (string-to-number (match-string 1 string)))
1024 (epg-context-result-for epg-context 'error)))))
1026 (defun epg-status-SIG_CREATED (process string)
1027 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1028 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1029 (epg-context-set-result-for
1031 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1032 (cons 'pubkey-algorithm
1033 (string-to-number (match-string 2 string)))
1034 (cons 'digest-algorithm
1035 (string-to-number (match-string 3 string)))
1036 (cons 'class (string-to-number (match-string 4 string) 16))
1037 (cons 'creation-time (match-string 5 string))
1038 (cons 'fingerprint (substring string (match-end 0))))
1039 (epg-context-result-for epg-context 'sign)))))
1041 (defun epg-status-KEY_CREATED (process string)
1042 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1043 (epg-context-set-result-for
1044 epg-context 'generate-key
1045 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1046 (cons 'fingerprint (match-string 2 string)))
1047 (epg-context-result-for epg-context 'generate-key)))))
1049 (defun epg-status-KEY_NOT_CREATED (process string)
1050 (epg-context-set-result-for
1052 (cons '(key-not-created)
1053 (epg-context-result-for epg-context 'error))))
1055 (defun epg-passphrase-callback-function (context key-id handback)
1057 (if (eq key-id 'SYM)
1058 "Passphrase for symmetric encryption: "
1059 (if (eq key-id 'PIN)
1060 "Passphrase for PIN: "
1061 (let ((entry (assoc key-id epg-user-id-alist)))
1063 (format "Passphrase for %s %s: " key-id (cdr entry))
1064 (format "Passphrase for %s: " key-id)))))))
1066 (defun epg-progress-callback-function (context what char current total
1068 (message "%s: %d%%/%d%%" what current total))
1070 (defun epg-configuration ()
1071 "Return a list of internal configuration parameters of `epg-gpg-program'."
1074 (apply #'call-process epg-gpg-program nil (list t nil) nil
1075 '("--with-colons" "--list-config"))
1076 (goto-char (point-min))
1077 (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
1078 (setq type (intern (match-string 1))
1079 config (cons (cons type
1081 '(pubkey cipher digest compress))
1082 (mapcar #'string-to-number
1083 (delete "" (split-string
1090 (defun epg-list-keys-1 (context name mode)
1091 (let ((args (append (list "--with-colons" "--no-greeting" "--batch"
1092 "--with-fingerprint"
1093 "--with-fingerprint"
1094 (if (or (eq mode t) (eq mode 'secret))
1095 "--list-secret-keys"
1099 (unless (eq (epg-context-protocol context) 'CMS)
1100 '("--fixed-list-mode"))
1101 (if name (list name))))
1102 keys string field index)
1104 (apply #'call-process
1105 (if (eq (epg-context-protocol context) 'CMS)
1108 nil (list t nil) nil args)
1109 (goto-char (point-min))
1110 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1111 (setq keys (cons (make-vector 15 nil) keys)
1112 string (match-string 0)
1116 (string-match "\\([^:]+\\)?:" string index))
1117 (setq index (match-end 0))
1118 (aset (car keys) field (match-string 1 string))
1119 (setq field (1+ field))))
1122 (defun epg-make-sub-key-1 (line)
1125 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1127 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1129 (member (aref line 0) '("sec" "ssb"))
1130 (string-to-number (aref line 3))
1131 (string-to-number (aref line 2))
1136 (defun epg-list-keys-postprocess-one-key (key)
1137 (let (key-id user-id-string entry)
1138 (epg-key-set-sub-key-list
1140 (nreverse (epg-key-sub-key-list key)))
1141 (epg-key-set-user-id-list
1143 (nreverse (epg-key-user-id-list key)))
1145 (epg-sub-key-id (car (epg-key-sub-key-list key)))
1147 (epg-user-id-string (car (epg-key-user-id-list key)))
1148 entry (assoc key-id epg-user-id-alist))
1150 (setcdr entry user-id-string)
1151 (setq epg-user-id-alist (cons (cons key-id user-id-string)
1152 epg-user-id-alist)))))
1154 (defun epg-list-keys (context &optional name mode)
1155 "Return a list of epg-key objects matched with NAME.
1156 If MODE is nil, only public keyring should be searched.
1157 If MODE is t or 'secret, only secret keyring should be searched.
1158 Otherwise, only public keyring should be searched and the key
1159 signatures should be included."
1160 (let ((lines (epg-list-keys-1 context name mode))
1164 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1166 (epg-list-keys-postprocess-one-key (car keys)))
1167 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1168 keys (cons (epg-make-key
1169 (if (aref (car lines) 8)
1170 (cdr (assq (string-to-char (aref (car lines) 8))
1171 epg-key-validity-alist))))
1173 (epg-key-set-sub-key-list
1175 (cons (epg-make-sub-key-1 (car lines))
1176 (epg-key-sub-key-list (car keys)))))
1177 ((member (aref (car lines) 0) '("sub" "ssb"))
1178 (epg-key-set-sub-key-list
1180 (cons (epg-make-sub-key-1 (car lines))
1181 (epg-key-sub-key-list (car keys)))))
1182 ((equal (aref (car lines) 0) "uid")
1183 (epg-key-set-user-id-list
1185 (cons (epg-make-user-id
1186 (if (aref (car lines) 1)
1187 (cdr (assq (string-to-char (aref (car lines) 1))
1188 epg-key-validity-alist)))
1191 (epg-dn-from-string (aref (car lines) 9))
1192 (error (aref (car lines) 9)))
1193 (aref (car lines) 9)))
1194 (epg-key-user-id-list (car keys)))))
1195 ((equal (aref (car lines) 0) "fpr")
1196 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1197 (aref (car lines) 9))))
1198 (setq lines (cdr lines)))
1200 (epg-list-keys-postprocess-one-key (car keys)))
1203 (if (fboundp 'make-temp-file)
1204 (defalias 'epg-make-temp-file 'make-temp-file)
1205 (defvar temporary-file-directory)
1206 ;; stolen from poe.el.
1207 (defun epg-make-temp-file (prefix)
1208 "Create a temporary file.
1209 The returned file name (created by appending some random characters at the end
1210 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1211 is guaranteed to point to a newly created empty file.
1212 You can then use `write-region' to write new data into the file."
1213 (let (tempdir tempfile)
1214 (setq prefix (expand-file-name prefix
1215 (if (featurep 'xemacs)
1217 temporary-file-directory)))
1220 ;; First, create a temporary directory.
1221 (while (condition-case ()
1223 (setq tempdir (make-temp-name
1225 (file-name-directory prefix)
1227 ;; return nil or signal an error.
1228 (make-directory tempdir))
1230 (file-already-exists t)))
1231 (set-file-modes tempdir 448)
1232 ;; Second, create a temporary file in the tempdir.
1233 ;; There *is* a race condition between `make-temp-name'
1234 ;; and `write-region', but we don't care it since we are
1235 ;; in a private directory now.
1236 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1237 (write-region "" nil tempfile nil 'silent)
1238 (set-file-modes tempfile 384)
1239 ;; Finally, make a hard-link from the tempfile.
1240 (while (condition-case ()
1242 (setq file (make-temp-name prefix))
1243 ;; return nil or signal an error.
1244 (add-name-to-file tempfile file))
1246 (file-already-exists t)))
1248 ;; Cleanup the tempfile.
1250 (file-exists-p tempfile)
1251 (delete-file tempfile))
1252 ;; Cleanup the tempdir.
1254 (file-directory-p tempdir)
1255 (delete-directory tempdir))))))
1258 (defun epg-cancel (context)
1259 (if (buffer-live-p (process-buffer (epg-context-process context)))
1261 (set-buffer (process-buffer (epg-context-process context)))
1262 (epg-context-set-result-for
1265 (epg-context-result-for epg-context 'error)))))
1266 (if (eq (process-status (epg-context-process context)) 'run)
1267 (delete-process (epg-context-process context))))
1270 (defun epg-start-decrypt (context cipher)
1271 "Initiate a decrypt operation on CIPHER.
1272 CIPHER is a data object.
1274 If you use this function, you will need to wait for the completion of
1275 `epg-gpg-program' by using `epg-wait-for-completion' and call
1276 `epg-reset' to clear a temporaly output file.
1277 If you are unsure, use synchronous version of this function
1278 `epg-decrypt-file' or `epg-decrypt-string' instead."
1279 (unless (epg-data-file cipher)
1280 (error "Not a file"))
1281 (epg-context-set-result context nil)
1282 (epg-start context (list "--decrypt" (epg-data-file cipher)))
1283 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1284 (unless (eq (epg-context-protocol context) 'CMS)
1285 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1288 (defun epg-decrypt-file (context cipher plain)
1289 "Decrypt a file CIPHER and store the result to a file PLAIN.
1290 If PLAIN is nil, it returns the result as a string."
1294 (epg-context-set-output-file context plain)
1295 (epg-context-set-output-file context
1296 (epg-make-temp-file "epg-output")))
1297 (epg-start-decrypt context (epg-make-data-from-file cipher))
1298 (epg-wait-for-completion context)
1299 (if (epg-context-result-for context 'error)
1300 (error "Decrypt failed: %S"
1301 (epg-context-result-for context 'error)))
1303 (epg-read-output context)))
1305 (epg-delete-output-file context))
1306 (epg-reset context)))
1309 (defun epg-decrypt-string (context cipher)
1310 "Decrypt a string CIPHER and return the plain text."
1311 (let ((input-file (epg-make-temp-file "epg-input"))
1312 (coding-system-for-write 'binary))
1315 (write-region cipher nil input-file nil 'quiet)
1316 (epg-context-set-output-file context
1317 (epg-make-temp-file "epg-output"))
1318 (epg-start-decrypt context (epg-make-data-from-file input-file))
1319 (epg-wait-for-completion context)
1320 (if (epg-context-result-for context 'error)
1321 (error "Decrypt failed: %S"
1322 (epg-context-result-for context 'error)))
1323 (epg-read-output context))
1324 (epg-delete-output-file context)
1325 (if (file-exists-p input-file)
1326 (delete-file input-file))
1327 (epg-reset context))))
1330 (defun epg-start-verify (context signature &optional signed-text)
1331 "Initiate a verify operation on SIGNATURE.
1332 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1334 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1335 For a normal or a clear text signature, SIGNED-TEXT should be nil.
1337 If you use this function, you will need to wait for the completion of
1338 `epg-gpg-program' by using `epg-wait-for-completion' and call
1339 `epg-reset' to clear a temporaly output file.
1340 If you are unsure, use synchronous version of this function
1341 `epg-verify-file' or `epg-verify-string' instead."
1342 (epg-context-set-result context nil)
1344 ;; Detached signature.
1345 (if (epg-data-file signed-text)
1346 (epg-start context (list "--verify" (epg-data-file signature)
1347 (epg-data-file signed-text)))
1348 (epg-start context (list "--verify" (epg-data-file signature) "-"))
1349 (if (eq (process-status (epg-context-process context)) 'run)
1350 (process-send-string (epg-context-process context)
1351 (epg-data-string signed-text)))
1352 (if (eq (process-status (epg-context-process context)) 'run)
1353 (process-send-eof (epg-context-process context))))
1354 ;; Normal (or cleartext) signature.
1355 (if (epg-data-file signature)
1356 (epg-start context (list "--verify" (epg-data-file signature)))
1357 (epg-start context (list "--verify"))
1358 (if (eq (process-status (epg-context-process context)) 'run)
1359 (process-send-string (epg-context-process context)
1360 (epg-data-string signature)))
1361 (if (eq (process-status (epg-context-process context)) 'run)
1362 (process-send-eof (epg-context-process context))))))
1365 (defun epg-verify-file (context signature &optional signed-text plain)
1366 "Verify a file SIGNATURE.
1367 SIGNED-TEXT and PLAIN are also a file if they are specified.
1369 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1370 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1374 (epg-context-set-output-file context plain)
1375 (epg-context-set-output-file context
1376 (epg-make-temp-file "epg-output")))
1378 (epg-start-verify context
1379 (epg-make-data-from-file signature)
1380 (epg-make-data-from-file signed-text))
1381 (epg-start-verify context
1382 (epg-make-data-from-file signature)))
1383 (epg-wait-for-completion context)
1384 ; (if (epg-context-result-for context 'error)
1385 ; (error "Verify failed: %S"
1386 ; (epg-context-result-for context 'error)))
1388 (epg-read-output context)))
1390 (epg-delete-output-file context))
1391 (epg-reset context)))
1394 (defun epg-verify-string (context signature &optional signed-text)
1395 "Verify a string SIGNATURE.
1396 SIGNED-TEXT is a string if it is specified.
1398 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1399 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1400 (let ((coding-system-for-write 'binary)
1404 (epg-context-set-output-file context
1405 (epg-make-temp-file "epg-output"))
1408 (setq input-file (epg-make-temp-file "epg-signature"))
1409 (write-region signature nil input-file nil 'quiet)
1410 (epg-start-verify context
1411 (epg-make-data-from-file input-file)
1412 (epg-make-data-from-string signed-text)))
1413 (epg-start-verify context (epg-make-data-from-string signature)))
1414 (epg-wait-for-completion context)
1415 ; (if (epg-context-result-for context 'error)
1416 ; (error "Verify failed: %S"
1417 ; (epg-context-result-for context 'error)))
1418 (epg-read-output context))
1419 (epg-delete-output-file context)
1421 (file-exists-p input-file))
1422 (delete-file input-file))
1423 (epg-reset context))))
1426 (defun epg-start-sign (context plain &optional mode)
1427 "Initiate a sign operation on PLAIN.
1428 PLAIN is a data object.
1430 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1431 If MODE is t or 'detached, it makes a detached signature.
1432 Otherwise, it makes a normal signature.
1434 If you use this function, you will need to wait for the completion of
1435 `epg-gpg-program' by using `epg-wait-for-completion' and call
1436 `epg-reset' to clear a temporaly output file.
1437 If you are unsure, use synchronous version of this function
1438 `epg-sign-file' or `epg-sign-string' instead."
1439 (epg-context-set-result context nil)
1441 (append (list (if (eq mode 'clearsign)
1443 (if (or (eq mode t) (eq mode 'detached))
1451 (car (epg-key-sub-key-list signer)))))
1452 (epg-context-signers context)))
1453 (if (epg-data-file plain)
1454 (list (epg-data-file plain)))))
1455 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1456 (unless (eq (epg-context-protocol context) 'CMS)
1457 (epg-wait-for-status context '("BEGIN_SIGNING")))
1458 (when (epg-data-string plain)
1459 (if (eq (process-status (epg-context-process context)) 'run)
1460 (process-send-string (epg-context-process context)
1461 (epg-data-string plain)))
1462 (if (eq (process-status (epg-context-process context)) 'run)
1463 (process-send-eof (epg-context-process context)))))
1466 (defun epg-sign-file (context plain signature &optional mode)
1467 "Sign a file PLAIN and store the result to a file SIGNATURE.
1468 If SIGNATURE is nil, it returns the result as a string.
1469 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1470 If MODE is t or 'detached, it makes a detached signature.
1471 Otherwise, it makes a normal signature."
1475 (epg-context-set-output-file context signature)
1476 (epg-context-set-output-file context
1477 (epg-make-temp-file "epg-output")))
1478 (epg-start-sign context (epg-make-data-from-file plain) mode)
1479 (epg-wait-for-completion context)
1480 (if (epg-context-result-for context 'sign)
1481 (if (epg-context-result-for context 'error)
1482 (message "Sign warning: %S"
1483 (epg-context-result-for context 'error)))
1484 (if (epg-context-result-for context 'error)
1485 (error "Sign failed: %S"
1486 (epg-context-result-for context 'error))
1487 (error "Sign failed")))
1489 (epg-read-output context)))
1491 (epg-delete-output-file context))
1492 (epg-reset context)))
1495 (defun epg-sign-string (context plain &optional mode)
1496 "Sign a string PLAIN and return the output as string.
1497 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1498 If MODE is t or 'detached, it makes a detached signature.
1499 Otherwise, it makes a normal signature."
1502 (epg-context-set-output-file context
1503 (epg-make-temp-file "epg-output"))
1504 (epg-start-sign context (epg-make-data-from-string plain) mode)
1505 (epg-wait-for-completion context)
1506 (unless (epg-context-result-for context 'sign)
1507 (if (epg-context-result-for context 'error)
1508 (error "Sign failed: %S"
1509 (epg-context-result-for context 'error))
1510 (error "Sign failed")))
1511 (epg-read-output context))
1512 (epg-delete-output-file context)
1513 (epg-reset context)))
1516 (defun epg-start-encrypt (context plain recipients
1517 &optional sign always-trust)
1518 "Initiate an encrypt operation on PLAIN.
1519 PLAIN is a data object.
1520 If RECIPIENTS is nil, it performs symmetric encryption.
1522 If you use this function, you will need to wait for the completion of
1523 `epg-gpg-program' by using `epg-wait-for-completion' and call
1524 `epg-reset' to clear a temporaly output file.
1525 If you are unsure, use synchronous version of this function
1526 `epg-encrypt-file' or `epg-encrypt-string' instead."
1527 (epg-context-set-result context nil)
1529 (append (if always-trust '("--always-trust"))
1530 (if recipients '("--encrypt") '("--symmetric"))
1534 (mapcar (lambda (signer)
1536 (epg-context-signers context)))))
1542 (car (epg-key-sub-key-list recipient)))))
1544 (if (epg-data-file plain)
1545 (list (epg-data-file plain)))))
1546 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1547 (unless (eq (epg-context-protocol context) 'CMS)
1549 (epg-wait-for-status context '("BEGIN_SIGNING"))
1550 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
1551 (when (epg-data-string plain)
1552 (if (eq (process-status (epg-context-process context)) 'run)
1553 (process-send-string (epg-context-process context)
1554 (epg-data-string plain)))
1555 (if (eq (process-status (epg-context-process context)) 'run)
1556 (process-send-eof (epg-context-process context)))))
1559 (defun epg-encrypt-file (context plain recipients
1560 cipher &optional sign always-trust)
1561 "Encrypt a file PLAIN and store the result to a file CIPHER.
1562 If CIPHER is nil, it returns the result as a string.
1563 If RECIPIENTS is nil, it performs symmetric encryption."
1567 (epg-context-set-output-file context cipher)
1568 (epg-context-set-output-file context
1569 (epg-make-temp-file "epg-output")))
1570 (epg-start-encrypt context (epg-make-data-from-file plain)
1571 recipients sign always-trust)
1572 (epg-wait-for-completion context)
1574 (not (epg-context-result-for context 'sign)))
1575 (if (epg-context-result-for context 'error)
1576 (error "Sign failed: %S"
1577 (epg-context-result-for context 'error))
1578 (error "Sign failed")))
1579 (if (epg-context-result-for context 'error)
1580 (error "Encrypt failed: %S"
1581 (epg-context-result-for context 'error)))
1583 (epg-read-output context)))
1585 (epg-delete-output-file context))
1586 (epg-reset context)))
1589 (defun epg-encrypt-string (context plain recipients
1590 &optional sign always-trust)
1591 "Encrypt a string PLAIN.
1592 If RECIPIENTS is nil, it performs symmetric encryption."
1595 (epg-context-set-output-file context
1596 (epg-make-temp-file "epg-output"))
1597 (epg-start-encrypt context (epg-make-data-from-string plain)
1598 recipients sign always-trust)
1599 (epg-wait-for-completion context)
1601 (not (epg-context-result-for context 'sign)))
1602 (if (epg-context-result-for context 'error)
1603 (error "Sign failed: %S"
1604 (epg-context-result-for context 'error))
1605 (error "Sign failed")))
1606 (if (epg-context-result-for context 'error)
1607 (error "Encrypt failed: %S"
1608 (epg-context-result-for context 'error)))
1609 (epg-read-output context))
1610 (epg-delete-output-file context)
1611 (epg-reset context)))
1614 (defun epg-start-export-keys (context keys)
1615 "Initiate an export keys operation.
1617 If you use this function, you will need to wait for the completion of
1618 `epg-gpg-program' by using `epg-wait-for-completion' and call
1619 `epg-reset' to clear a temporaly output file.
1620 If you are unsure, use synchronous version of this function
1621 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1622 (epg-context-set-result context nil)
1623 (epg-start context (cons "--export"
1627 (car (epg-key-sub-key-list key))))
1631 (defun epg-export-keys-to-file (context keys file)
1632 "Extract public KEYS."
1636 (epg-context-set-output-file context file)
1637 (epg-context-set-output-file context
1638 (epg-make-temp-file "epg-output")))
1639 (epg-start-export-keys context keys)
1640 (epg-wait-for-completion context)
1641 (if (epg-context-result-for context 'error)
1642 (error "Export keys failed: %S"
1643 (epg-context-result-for context 'error)))
1645 (epg-read-output context)))
1647 (epg-delete-output-file context))
1648 (epg-reset context)))
1651 (defun epg-export-keys-to-string (context keys)
1652 "Extract public KEYS and return them as a string."
1653 (epg-export-keys-to-file context keys nil))
1656 (defun epg-start-import-keys (context keys)
1657 "Initiate an import keys operation.
1658 KEYS is a data object.
1660 If you use this function, you will need to wait for the completion of
1661 `epg-gpg-program' by using `epg-wait-for-completion' and call
1662 `epg-reset' to clear a temporaly output file.
1663 If you are unsure, use synchronous version of this function
1664 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1665 (epg-context-set-result context nil)
1666 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
1667 (epg-start context (list "--import" (epg-data-file keys)))
1668 (when (epg-data-string keys)
1669 (if (eq (process-status (epg-context-process context)) 'run)
1670 (process-send-string (epg-context-process context)
1671 (epg-data-string keys)))
1672 (if (eq (process-status (epg-context-process context)) 'run)
1673 (process-send-eof (epg-context-process context)))))
1675 (defun epg-import-keys-1 (context keys)
1678 (epg-start-import-keys context keys)
1679 (epg-wait-for-completion context)
1680 (if (epg-context-result-for context 'error)
1681 (error "Import keys failed: %S"
1682 (epg-context-result-for context 'error)))
1683 (epg-read-output context))
1684 (epg-reset context)))
1687 (defun epg-import-keys-from-file (context keys)
1688 "Add keys from a file KEYS."
1689 (epg-import-keys-1 context (epg-make-data-from-file keys)))
1692 (defun epg-import-keys-from-string (context keys)
1693 "Add keys from a string KEYS."
1694 (epg-import-keys-1 context (epg-make-data-from-string keys)))
1697 (defun epg-start-delete-keys (context keys &optional allow-secret)
1698 "Initiate an delete keys operation.
1700 If you use this function, you will need to wait for the completion of
1701 `epg-gpg-program' by using `epg-wait-for-completion' and call
1702 `epg-reset' to clear a temporaly output file.
1703 If you are unsure, use synchronous version of this function
1704 `epg-delete-keys' instead."
1705 (epg-context-set-result context nil)
1706 (epg-start context (cons (if allow-secret
1707 "--delete-secret-key"
1712 (car (epg-key-sub-key-list key))))
1716 (defun epg-delete-keys (context keys &optional allow-secret)
1717 "Delete KEYS from the key ring."
1720 (epg-start-delete-keys context keys allow-secret)
1721 (epg-wait-for-completion context)
1722 (if (epg-context-result-for context 'error)
1723 (error "Delete keys failed: %S"
1724 (epg-context-result-for context 'error))))
1725 (epg-reset context)))
1728 (defun epg-start-sign-keys (context keys &optional local)
1729 "Initiate an sign keys operation.
1731 If you use this function, you will need to wait for the completion of
1732 `epg-gpg-program' by using `epg-wait-for-completion' and call
1733 `epg-reset' to clear a temporaly output file.
1734 If you are unsure, use synchronous version of this function
1735 `epg-sign-keys' instead."
1736 (epg-context-set-result context nil)
1737 (epg-start context (cons (if local
1743 (car (epg-key-sub-key-list key))))
1747 (defun epg-sign-keys (context keys &optional local)
1748 "Sign KEYS from the key ring."
1751 (epg-start-sign-keys context keys local)
1752 (epg-wait-for-completion context)
1753 (if (epg-context-result-for context 'error)
1754 (error "Sign keys failed: %S"
1755 (epg-context-result-for context 'error))))
1756 (epg-reset context)))
1759 (defun epg-start-generate-key (context parameters)
1760 "Initiate a key generation.
1761 PARAMETERS specifies parameters for the key.
1763 If you use this function, you will need to wait for the completion of
1764 `epg-gpg-program' by using `epg-wait-for-completion' and call
1765 `epg-reset' to clear a temporaly output file.
1766 If you are unsure, use synchronous version of this function
1767 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
1768 (epg-context-set-result context nil)
1769 (if (epg-data-file parameters)
1770 (epg-start context (list "--batch" "--genkey"
1771 (epg-data-file parameters)))
1772 (epg-start context '("--batch" "--genkey"))
1773 (if (eq (process-status (epg-context-process context)) 'run)
1774 (process-send-string (epg-context-process context)
1775 (epg-data-string parameters)))
1776 (if (eq (process-status (epg-context-process context)) 'run)
1777 (process-send-eof (epg-context-process context)))))
1780 (defun epg-generate-key-from-file (context parameters)
1781 "Generate a new key pair.
1782 PARAMETERS is a file which tells how to create the key."
1785 (epg-start-generate-key context (epg-make-data-from-file parameters))
1786 (epg-wait-for-completion context)
1787 (if (epg-context-result-for context 'error)
1788 (error "Generate key failed: %S"
1789 (epg-context-result-for context 'error))))
1790 (epg-reset context)))
1793 (defun epg-generate-key-from-string (context parameters)
1794 "Generate a new key pair.
1795 PARAMETERS is a string which tells how to create the key."
1798 (epg-start-generate-key context (epg-make-data-from-string parameters))
1799 (epg-wait-for-completion context)
1800 (if (epg-context-result-for context 'error)
1801 (error "Generate key failed: %S"
1802 (epg-context-result-for context 'error))))
1803 (epg-reset context)))
1805 (defun epg-decode-hexstring (string)
1807 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
1808 (setq string (replace-match "\\x\\&" t nil string)
1810 (car (read-from-string (concat "\"" string "\"")))))
1812 (defun epg-decode-quotedstring (string)
1814 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
1815 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
1817 (if (match-beginning 2)
1818 (setq string (replace-match "\\2" t nil string)
1820 (if (match-beginning 3)
1821 (setq string (replace-match "\\x\\3" t nil string)
1823 (setq string (replace-match "\\\\\\\\\\4" t nil string)
1824 index (+ index 3)))))
1825 (car (read-from-string (concat "\"" string "\"")))))
1827 (defun epg-dn-from-string (string)
1828 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
1829 The return value is an alist mapping from types to values."
1831 (length (length string))
1832 alist type value group)
1833 (while (< index length)
1834 (if (eq index (string-match "[ \t\n\r]*" string index))
1835 (setq index (match-end 0)))
1836 (if (eq index (string-match
1837 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
1839 (setq type (match-string 1 string)
1840 index (match-end 0))
1841 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
1843 (setq type (match-string 1 string)
1844 index (match-end 0))))
1846 (error "Invalid type"))
1847 (if (eq index (string-match
1848 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
1850 (setq index (match-end 0)
1851 value (epg-decode-quotedstring (match-string 0 string)))
1852 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
1853 (setq index (match-end 0)
1854 value (epg-decode-hexstring (match-string 1 string)))
1855 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
1857 (setq index (match-end 0)
1858 value (epg-decode-quotedstring (match-string 0 string))))))
1860 (if (stringp (car (car alist)))
1861 (setcar alist (list (cons type value) (car alist)))
1862 (setcar alist (cons (cons type value) (car alist))))
1863 (if (consp (car (car alist)))
1864 (setcar alist (nreverse (car alist))))
1865 (setq alist (cons (cons type value) alist)
1868 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
1869 (setq index (match-end 0)
1870 group (eq (aref string (match-beginning 1)) ?+))))
1873 (defun epg-decode-dn (alist)
1874 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
1875 Type names are resolved using `epg-dn-type-alist'."
1878 (if (stringp (car rdn))
1879 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
1881 (format "%s=%s" (cdr entry) (cdr rdn))
1882 (format "%s=%s" (car rdn) (cdr rdn))))
1883 (concat "(" (epg-decode-dn rdn) ")")))
1889 ;;; epg.el ends here