1 ;;; epg.el --- the EasyPG Library
2 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
3 ;; 2005, 2006 Free Software Foundation, Inc.
4 ;; Copyright (C) 2006 Daiki Ueno
6 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Keywords: PGP, GnuPG
9 ;; This file is part of EasyPG.
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
31 (defcustom epg-gpg-program "gpg"
32 "The `gpg' executable."
36 (defcustom epg-gpgsm-program "gpgsm"
37 "The `gpgsm' executable."
41 (defconst epg-version-number "0.0.1")
43 (defvar epg-user-id nil
44 "GnuPG ID of your default identity.")
46 (defvar epg-user-id-alist nil
47 "An alist mapping from key ID to user ID.")
49 (defvar epg-read-point nil)
50 (defvar epg-pending-status-list nil)
51 (defvar epg-key-id nil)
52 (defvar epg-context nil)
53 (defvar epg-debug nil)
54 (defvar epg-debug-buffer nil)
56 ;; from gnupg/include/cipher.h
57 (defconst epg-cipher-algorithm-alist
69 ;; from gnupg/include/cipher.h
70 (defconst epg-pubkey-algorithm-alist
78 ;; from gnupg/include/cipher.h
79 (defconst epg-digest-algorithm-alist
87 ;; from gnupg/include/cipher.h
88 (defconst epg-compress-algorithm-alist
94 (defconst epg-invalid-recipients-alist
95 '((0 . "No specific reason given")
97 (2 . "Ambigious specification")
98 (3 . "Wrong key usage")
103 (8 . "Policy mismatch")
104 (9 . "Not a secret key")
105 (10 . "Key not trusted")))
107 (defconst epg-delete-problem-alist
108 '((1 . "No such key")
109 (2 . "Must delete secret key first")
110 (3 . "Ambigious specification")))
112 (defvar epg-key-validity-alist
125 (defvar epg-key-capablity-alist
129 (?a . authentication)))
131 (defvar epg-dn-type-alist
132 '(("1.2.840.113549.1.9.1" . "EMail")
136 ("0.2.262.1.10.7.20" . "NameDistinguisher")
137 ("2.5.4.16" . "ADDR")
140 ("2.5.4.17" . "PostalCode")
141 ("2.5.4.65" . "Pseudo")
142 ("2.5.4.5" . "SerialNumber")))
144 (defvar epg-prompt-alist nil)
146 (defun epg-make-data-from-file (file)
147 "Make a data object from FILE."
148 (cons 'epg-data (vector file nil)))
150 (defun epg-make-data-from-string (string)
151 "Make a data object from STRING."
152 (cons 'epg-data (vector nil string)))
154 (defun epg-data-file (data)
155 "Return the file of DATA."
156 (unless (eq (car data) 'epg-data)
157 (signal 'wrong-type-argument (list 'epg-data-p data)))
160 (defun epg-data-string (data)
161 "Return the string of DATA."
162 (unless (eq (car data) 'epg-data)
163 (signal 'wrong-type-argument (list 'epg-data-p data)))
166 (defun epg-make-context (&optional protocol armor textmode include-certs
167 cipher-algorithm digest-algorithm
169 "Return a context object."
171 (vector protocol armor textmode include-certs
172 cipher-algorithm digest-algorithm compress-algorithm
173 #'epg-passphrase-callback-function
174 #'epg-progress-callback-function
177 (defun epg-context-protocol (context)
178 "Return the protocol used within CONTEXT."
179 (unless (eq (car context) 'epg-context)
180 (signal 'wrong-type-argument (list 'epg-context-p context)))
181 (aref (cdr context) 0))
183 (defun epg-context-armor (context)
184 "Return t if the output shouled be ASCII armored in CONTEXT."
185 (unless (eq (car context) 'epg-context)
186 (signal 'wrong-type-argument (list 'epg-context-p context)))
187 (aref (cdr context) 1))
189 (defun epg-context-textmode (context)
190 "Return t if canonical text mode should be used in CONTEXT."
191 (unless (eq (car context) 'epg-context)
192 (signal 'wrong-type-argument (list 'epg-context-p context)))
193 (aref (cdr context) 2))
195 (defun epg-context-include-certs (context)
196 "Return how many certificates should be included in an S/MIME signed
198 (unless (eq (car context) 'epg-context)
199 (signal 'wrong-type-argument (list 'epg-context-p context)))
200 (aref (cdr context) 3))
202 (defun epg-context-cipher-algorithm (context)
203 "Return the cipher algorithm in CONTEXT."
204 (unless (eq (car context) 'epg-context)
205 (signal 'wrong-type-argument (list 'epg-context-p context)))
206 (aref (cdr context) 4))
208 (defun epg-context-digest-algorithm (context)
209 "Return the digest algorithm in CONTEXT."
210 (unless (eq (car context) 'epg-context)
211 (signal 'wrong-type-argument (list 'epg-context-p context)))
212 (aref (cdr context) 5))
214 (defun epg-context-compress-algorithm (context)
215 "Return the compress algorithm in CONTEXT."
216 (unless (eq (car context) 'epg-context)
217 (signal 'wrong-type-argument (list 'epg-context-p context)))
218 (aref (cdr context) 6))
220 (defun epg-context-passphrase-callback (context)
221 "Return the function used to query passphrase."
222 (unless (eq (car context) 'epg-context)
223 (signal 'wrong-type-argument (list 'epg-context-p context)))
224 (aref (cdr context) 7))
226 (defun epg-context-progress-callback (context)
227 "Return the function which handles progress update."
228 (unless (eq (car context) 'epg-context)
229 (signal 'wrong-type-argument (list 'epg-context-p context)))
230 (aref (cdr context) 8))
232 (defun epg-context-signers (context)
233 "Return the list of key-id for singning."
234 (unless (eq (car context) 'epg-context)
235 (signal 'wrong-type-argument (list 'epg-context-p context)))
236 (aref (cdr context) 9))
238 (defun epg-context-process (context)
239 "Return the process object of `epg-gpg-program'.
240 This function is for internal use only."
241 (unless (eq (car context) 'epg-context)
242 (signal 'wrong-type-argument (list 'epg-context-p context)))
243 (aref (cdr context) 10))
245 (defun epg-context-output-file (context)
246 "Return the output file of `epg-gpg-program'.
247 This function is for internal use only."
248 (unless (eq (car context) 'epg-context)
249 (signal 'wrong-type-argument (list 'epg-context-p context)))
250 (aref (cdr context) 11))
252 (defun epg-context-result (context)
253 "Return the result of the previous cryptographic operation."
254 (unless (eq (car context) 'epg-context)
255 (signal 'wrong-type-argument (list 'epg-context-p context)))
256 (aref (cdr context) 12))
258 (defun epg-context-set-protocol (context protocol)
259 "Set the protocol used within CONTEXT."
260 (unless (eq (car context) 'epg-context)
261 (signal 'wrong-type-argument (list 'epg-context-p context)))
262 (aset (cdr context) 0 protocol))
264 (defun epg-context-set-armor (context armor)
265 "Specify if the output shouled be ASCII armored in CONTEXT."
266 (unless (eq (car context) 'epg-context)
267 (signal 'wrong-type-argument (list 'epg-context-p context)))
268 (aset (cdr context) 1 armor))
270 (defun epg-context-set-textmode (context textmode)
271 "Specify if canonical text mode should be used in CONTEXT."
272 (unless (eq (car context) 'epg-context)
273 (signal 'wrong-type-argument (list 'epg-context-p context)))
274 (aset (cdr context) 2 textmode))
276 (defun epg-context-set-include-certs (context include-certs)
277 "Set how many certificates should be included in an S/MIME signed message."
278 (unless (eq (car context) 'epg-context)
279 (signal 'wrong-type-argument (list 'epg-context-p context)))
280 (aset (cdr context) 3 include-certs))
282 (defun epg-context-set-cipher-algorithm (context cipher-algorithm)
283 "Set the cipher algorithm in CONTEXT."
284 (unless (eq (car context) 'epg-context)
285 (signal 'wrong-type-argument (list 'epg-context-p context)))
286 (aset (cdr context) 4 cipher-algorithm))
288 (defun epg-context-set-digest-algorithm (context digest-algorithm)
289 "Set the digest algorithm in CONTEXT."
290 (unless (eq (car context) 'epg-context)
291 (signal 'wrong-type-argument (list 'epg-context-p context)))
292 (aset (cdr context) 5 digest-algorithm))
294 (defun epg-context-set-compress-algorithm (context compress-algorithm)
295 "Set the compress algorithm in CONTEXT."
296 (unless (eq (car context) 'epg-context)
297 (signal 'wrong-type-argument (list 'epg-context-p context)))
298 (aset (cdr context) 6 compress-algorithm))
300 (defun epg-context-set-passphrase-callback (context
302 "Set the function used to query passphrase."
303 (unless (eq (car context) 'epg-context)
304 (signal 'wrong-type-argument (list 'epg-context-p context)))
305 (aset (cdr context) 7 passphrase-callback))
307 (defun epg-context-set-progress-callback (context progress-callback)
308 "Set the function which handles progress update."
309 (unless (eq (car context) 'epg-context)
310 (signal 'wrong-type-argument (list 'epg-context-p context)))
311 (aset (cdr context) 8 progress-callback))
313 (defun epg-context-set-signers (context signers)
314 "Set the list of key-id for singning."
315 (unless (eq (car context) 'epg-context)
316 (signal 'wrong-type-argument (list 'epg-context-p context)))
317 (aset (cdr context) 9 signers))
319 (defun epg-context-set-process (context process)
320 "Set the process object of `epg-gpg-program'.
321 This function is for internal use only."
322 (unless (eq (car context) 'epg-context)
323 (signal 'wrong-type-argument (list 'epg-context-p context)))
324 (aset (cdr context) 10 process))
326 (defun epg-context-set-output-file (context output-file)
327 "Set the output file of `epg-gpg-program'.
328 This function is for internal use only."
329 (unless (eq (car context) 'epg-context)
330 (signal 'wrong-type-argument (list 'epg-context-p context)))
331 (aset (cdr context) 11 output-file))
333 (defun epg-context-set-result (context result)
334 "Set the result of the previous cryptographic operation."
335 (unless (eq (car context) 'epg-context)
336 (signal 'wrong-type-argument (list 'epg-context-p context)))
337 (aset (cdr context) 12 result))
339 (defun epg-make-signature (status &optional key-id)
340 "Return a signature object."
341 (cons 'epg-signature (vector status key-id nil nil nil nil nil nil)))
343 (defun epg-signature-status (signature)
344 "Return the status code of SIGNATURE."
345 (unless (eq (car signature) 'epg-signature)
346 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
347 (aref (cdr signature) 0))
349 (defun epg-signature-key-id (signature)
350 "Return the key-id of SIGNATURE."
351 (unless (eq (car signature) 'epg-signature)
352 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
353 (aref (cdr signature) 1))
355 (defun epg-signature-validity (signature)
356 "Return the validity of SIGNATURE."
357 (unless (eq (car signature) 'epg-signature)
358 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
359 (aref (cdr signature) 2))
361 (defun epg-signature-fingerprint (signature)
362 "Return the fingerprint of SIGNATURE."
363 (unless (eq (car signature) 'epg-signature)
364 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
365 (aref (cdr signature) 3))
367 (defun epg-signature-creation-time (signature)
368 "Return the creation time of SIGNATURE."
369 (unless (eq (car signature) 'epg-signature)
370 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
371 (aref (cdr signature) 4))
373 (defun epg-signature-expiration-time (signature)
374 "Return the expiration time of SIGNATURE."
375 (unless (eq (car signature) 'epg-signature)
376 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
377 (aref (cdr signature) 5))
379 (defun epg-signature-pubkey-algorithm (signature)
380 "Return the public key algorithm of SIGNATURE."
381 (unless (eq (car signature) 'epg-signature)
382 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
383 (aref (cdr signature) 6))
385 (defun epg-signature-digest-algorithm (signature)
386 "Return the digest algorithm of SIGNATURE."
387 (unless (eq (car signature) 'epg-signature)
388 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
389 (aref (cdr signature) 7))
391 (defun epg-signature-set-status (signature status)
392 "Set the status code of SIGNATURE."
393 (unless (eq (car signature) 'epg-signature)
394 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
395 (aset (cdr signature) 0 status))
397 (defun epg-signature-set-key-id (signature key-id)
398 "Set the key-id of SIGNATURE."
399 (unless (eq (car signature) 'epg-signature)
400 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
401 (aset (cdr signature) 1 key-id))
403 (defun epg-signature-set-validity (signature validity)
404 "Set the validity of SIGNATURE."
405 (unless (eq (car signature) 'epg-signature)
406 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
407 (aset (cdr signature) 2 validity))
409 (defun epg-signature-set-fingerprint (signature fingerprint)
410 "Set the fingerprint of SIGNATURE."
411 (unless (eq (car signature) 'epg-signature)
412 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
413 (aset (cdr signature) 3 fingerprint))
415 (defun epg-signature-set-creation-time (signature creation-time)
416 "Set the creation time of SIGNATURE."
417 (unless (eq (car signature) 'epg-signature)
418 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
419 (aset (cdr signature) 4 creation-time))
421 (defun epg-signature-set-expiration-time (signature expiration-time)
422 "Set the expiration time of SIGNATURE."
423 (unless (eq (car signature) 'epg-signature)
424 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
425 (aset (cdr signature) 5 expiration-time))
427 (defun epg-signature-set-pubkey-algorithm (signature pubkey-algorithm)
428 "Set the public key algorithm of SIGNATURE."
429 (unless (eq (car signature) 'epg-signature)
430 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
431 (aset (cdr signature) 6 pubkey-algorithm))
433 (defun epg-signature-set-digest-algorithm (signature digest-algorithm)
434 "Set the digest algorithm of SIGNATURE."
435 (unless (eq (car signature) 'epg-signature)
436 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
437 (aset (cdr signature) 7 digest-algorithm))
439 (defun epg-make-key (owner-trust)
440 "Return a key object."
441 (cons 'epg-key (vector owner-trust nil nil)))
443 (defun epg-key-owner-trust (key)
444 "Return the owner trust of KEY."
445 (unless (eq (car key) 'epg-key)
446 (signal 'wrong-type-argument (list 'epg-key-p key)))
449 (defun epg-key-sub-key-list (key)
450 "Return the sub key list of KEY."
451 (unless (eq (car key) 'epg-key)
452 (signal 'wrong-type-argument (list 'epg-key-p key)))
455 (defun epg-key-user-id-list (key)
456 "Return the user ID list of KEY."
457 (unless (eq (car key) 'epg-key)
458 (signal 'wrong-type-argument (list 'epg-key-p key)))
461 (defun epg-key-set-sub-key-list (key sub-key-list)
462 "Set the sub key list of KEY."
463 (unless (eq (car key) 'epg-key)
464 (signal 'wrong-type-argument (list 'epg-key-p key)))
465 (aset (cdr key) 1 sub-key-list))
467 (defun epg-key-set-user-id-list (key user-id-list)
468 "Set the user ID list of KEY."
469 (unless (eq (car key) 'epg-key)
470 (signal 'wrong-type-argument (list 'epg-key-p key)))
471 (aset (cdr key) 2 user-id-list))
473 (defun epg-make-sub-key (validity capability secret algorithm length id
474 creation-time expiration-time)
475 "Return a sub key object."
477 (vector validity capability secret algorithm length id creation-time
478 expiration-time nil)))
480 (defun epg-sub-key-validity (sub-key)
481 "Return the validity of SUB-KEY."
482 (unless (eq (car sub-key) 'epg-sub-key)
483 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
484 (aref (cdr sub-key) 0))
486 (defun epg-sub-key-capability (sub-key)
487 "Return the capability of SUB-KEY."
488 (unless (eq (car sub-key) 'epg-sub-key)
489 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
490 (aref (cdr sub-key) 1))
492 (defun epg-sub-key-secret (sub-key)
493 "Return non-nil if SUB-KEY is a secret key."
494 (unless (eq (car sub-key) 'epg-sub-key)
495 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
496 (aref (cdr sub-key) 2))
498 (defun epg-sub-key-algorithm (sub-key)
499 "Return the algorithm of SUB-KEY."
500 (unless (eq (car sub-key) 'epg-sub-key)
501 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
502 (aref (cdr sub-key) 3))
504 (defun epg-sub-key-length (sub-key)
505 "Return the length of SUB-KEY."
506 (unless (eq (car sub-key) 'epg-sub-key)
507 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
508 (aref (cdr sub-key) 4))
510 (defun epg-sub-key-id (sub-key)
511 "Return the ID of SUB-KEY."
512 (unless (eq (car sub-key) 'epg-sub-key)
513 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
514 (aref (cdr sub-key) 5))
516 (defun epg-sub-key-creation-time (sub-key)
517 "Return the creation time of SUB-KEY."
518 (unless (eq (car sub-key) 'epg-sub-key)
519 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
520 (aref (cdr sub-key) 6))
522 (defun epg-sub-key-expiration-time (sub-key)
523 "Return the expiration time of SUB-KEY."
524 (unless (eq (car sub-key) 'epg-sub-key)
525 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
526 (aref (cdr sub-key) 7))
528 (defun epg-sub-key-fingerprint (sub-key)
529 "Return the fingerprint of SUB-KEY."
530 (unless (eq (car sub-key) 'epg-sub-key)
531 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
532 (aref (cdr sub-key) 8))
534 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
535 "Set the fingerprint of SUB-KEY.
536 This function is for internal use only."
537 (unless (eq (car sub-key) 'epg-sub-key)
538 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
539 (aset (cdr sub-key) 8 fingerprint))
541 (defun epg-make-user-id (validity name)
542 "Return a user ID object."
543 (cons 'epg-user-id (vector validity name nil)))
545 (defun epg-user-id-validity (user-id)
546 "Return the validity of USER-ID."
547 (unless (eq (car user-id) 'epg-user-id)
548 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
549 (aref (cdr user-id) 0))
551 (defun epg-user-id-name (user-id)
552 "Return the name of USER-ID."
553 (unless (eq (car user-id) 'epg-user-id)
554 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
555 (aref (cdr user-id) 1))
557 (defun epg-user-id-signature-list (user-id)
558 "Return the signature list of USER-ID."
559 (unless (eq (car user-id) 'epg-user-id)
560 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
561 (aref (cdr user-id) 2))
563 (defun epg-user-id-set-signature-list (user-id signature-list)
564 "Set the signature list of USER-ID."
565 (unless (eq (car user-id) 'epg-user-id)
566 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
567 (aset (cdr user-id) 2 signature-list))
569 (defun epg-context-result-for (context name)
570 (cdr (assq name (epg-context-result context))))
572 (defun epg-context-set-result-for (context name value)
573 (let* ((result (epg-context-result context))
574 (entry (assq name result)))
577 (epg-context-set-result context (cons (cons name value) result)))))
579 (defun epg-signature-to-string (signature)
580 (let ((user-id (cdr (assoc (epg-signature-key-id signature)
581 epg-user-id-alist))))
583 (cond ((eq (epg-signature-status signature) 'good)
585 ((eq (epg-signature-status signature) 'bad)
587 ((eq (epg-signature-status signature) 'expired)
588 "Expired signature ")
589 ((eq (epg-signature-status signature) 'expired-key)
590 "Signature made by expired key ")
591 ((eq (epg-signature-status signature) 'revoked-key)
592 "Signature made by revoked key ")
593 ((eq (epg-signature-status signature) 'no-pubkey)
594 "No public key for "))
595 (epg-signature-key-id signature)
597 (concat " from " user-id " ")
599 (if (epg-signature-validity signature)
600 (format "(trust %s)" (epg-signature-validity signature))
603 (defun epg-verify-result-to-string (verify-result)
604 (mapconcat #'epg-signature-to-string verify-result "\n"))
606 (defun epg-start (context args)
607 "Start `epg-gpg-program' in a subprocess with given ARGS."
608 (let* ((args (append (list "--no-tty"
611 (unless (eq (epg-context-protocol context) 'CMS)
612 (list "--command-fd" "0"))
613 (if (epg-context-armor context) '("--armor"))
614 (if (epg-context-textmode context) '("--textmode"))
615 (if (epg-context-output-file context)
616 (list "--output" (epg-context-output-file context)))
618 (coding-system-for-write 'binary)
619 process-connection-type
620 (orig-mode (default-file-modes))
621 (buffer (generate-new-buffer " *epg*"))
625 (unless epg-debug-buffer
626 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
627 (set-buffer epg-debug-buffer)
628 (goto-char (point-max))
629 (insert (format "%s %s\n"
630 (if (eq (epg-context-protocol context) 'CMS)
633 (mapconcat #'identity args " ")))))
634 (with-current-buffer buffer
635 (make-local-variable 'epg-read-point)
636 (setq epg-read-point (point-min))
637 (make-local-variable 'epg-pending-status-list)
638 (setq epg-pending-status-list nil)
639 (make-local-variable 'epg-key-id)
640 (setq epg-key-id nil)
641 (make-local-variable 'epg-context)
642 (setq epg-context context))
645 (set-default-file-modes 448)
647 (apply #'start-process "epg" buffer
648 (if (eq (epg-context-protocol context) 'CMS)
652 (set-default-file-modes orig-mode))
653 (set-process-filter process #'epg-process-filter)
654 (epg-context-set-process context process)))
656 (defun epg-process-filter (process input)
659 (unless epg-debug-buffer
660 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
661 (set-buffer epg-debug-buffer)
662 (goto-char (point-max))
664 (if (buffer-live-p (process-buffer process))
666 (set-buffer (process-buffer process))
667 (goto-char (point-max))
669 (goto-char epg-read-point)
671 (while (looking-at ".*\n") ;the input line finished
673 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
674 (let* ((status (match-string 1))
675 (string (match-string 2))
676 (symbol (intern-soft (concat "epg-status-" status))))
677 (if (member status epg-pending-status-list)
678 (setq epg-pending-status-list nil))
681 (funcall symbol process string)))))
683 (setq epg-read-point (point)))))
685 (defun epg-read-output (context)
687 (if (fboundp 'set-buffer-multibyte)
688 (set-buffer-multibyte nil))
689 (if (file-exists-p (epg-context-output-file context))
690 (let ((coding-system-for-read (if (epg-context-textmode context)
693 (insert-file-contents (epg-context-output-file context))
696 (defun epg-wait-for-status (context status-list)
697 (with-current-buffer (process-buffer (epg-context-process context))
698 (setq epg-pending-status-list status-list)
699 (while (and (eq (process-status (epg-context-process context)) 'run)
700 epg-pending-status-list)
701 (accept-process-output (epg-context-process context) 1))))
703 (defun epg-wait-for-completion (context)
704 (while (eq (process-status (epg-context-process context)) 'run)
705 ;; We can't use accept-process-output instead of sit-for here
706 ;; because it may cause an interrupt during the sentinel execution.
709 (defun epg-flush (context)
710 (if (eq (process-status (epg-context-process context)) 'run)
711 (process-send-eof (epg-context-process context))))
713 (defun epg-reset (context)
714 (if (and (epg-context-process context)
715 (buffer-live-p (process-buffer (epg-context-process context))))
716 (kill-buffer (process-buffer (epg-context-process context))))
717 (epg-context-set-process context nil))
719 (defun epg-delete-output-file (context)
720 (if (and (epg-context-output-file context)
721 (file-exists-p (epg-context-output-file context)))
722 (delete-file (epg-context-output-file context))))
724 (defun epg-status-USERID_HINT (process string)
725 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
726 (let* ((key-id (match-string 1 string))
727 (user-id (match-string 2 string))
728 (entry (assoc key-id epg-user-id-alist)))
730 (setcdr entry user-id)
731 (setq epg-user-id-alist (cons (cons key-id user-id)
732 epg-user-id-alist))))))
734 (defun epg-status-NEED_PASSPHRASE (process string)
735 (if (string-match "\\`\\([^ ]+\\)" string)
736 (setq epg-key-id (match-string 1 string))))
738 (defun epg-status-NEED_PASSPHRASE_SYM (process string)
739 (setq epg-key-id 'SYM))
741 (defun epg-status-NEED_PASSPHRASE_PIN (process string)
742 (setq epg-key-id 'PIN))
744 (defun epg-status-GET_HIDDEN (process string)
746 (string-match "\\`passphrase\\." string))
749 passphrase-with-new-line)
755 (if (consp (epg-context-passphrase-callback
757 (car (epg-context-passphrase-callback
759 (epg-context-passphrase-callback epg-context))
762 (if (consp (epg-context-passphrase-callback
764 (cdr (epg-context-passphrase-callback
767 (setq passphrase-with-new-line (concat passphrase "\n"))
768 (fillarray passphrase 0)
769 (setq passphrase nil)
770 (process-send-string process passphrase-with-new-line)))
772 (epg-context-set-result-for
775 (epg-context-result-for epg-context 'error)))
776 (delete-process process)))
778 (fillarray passphrase 0))
779 (if passphrase-with-new-line
780 (fillarray passphrase-with-new-line 0))))))
782 (defun epg-status-GET_BOOL (process string)
783 (let ((entry (assoc string epg-prompt-alist))
786 (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
787 (process-send-string process "y\n")
788 (process-send-string process "n\n"))
790 (epg-context-set-result-for
793 (epg-context-result-for epg-context 'error)))
794 (delete-process process)))))
796 (defun epg-status-GET_LINE (process string)
797 (let ((entry (assoc string epg-prompt-alist))
802 (concat (read-string (if entry (cdr entry) (concat string ": ")))
805 (epg-context-set-result-for
808 (epg-context-result-for epg-context 'error)))
809 (delete-process process)))))
811 (defun epg-signature-status-internal (status string)
812 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
813 (let* ((key-id (match-string 1 string))
814 (user-id (match-string 2 string))
815 (entry (assoc key-id epg-user-id-alist)))
816 (epg-context-set-result-for
819 (cons (epg-make-signature status key-id)
820 (epg-context-result-for epg-context 'verify)))
821 (if (eq (epg-context-protocol epg-context) 'CMS)
823 (setq user-id (epg-dn-from-string user-id))
826 (setcdr entry user-id)
827 (setq epg-user-id-alist
828 (cons (cons key-id user-id) epg-user-id-alist))))
829 (epg-context-set-result-for
832 (cons (epg-make-signature status)
833 (epg-context-result-for epg-context 'verify)))))
835 (defun epg-status-GOODSIG (process string)
836 (epg-signature-status-internal 'good string))
838 (defun epg-status-EXPSIG (process string)
839 (epg-signature-status-internal 'expired string))
841 (defun epg-status-EXPKEYSIG (process string)
842 (epg-signature-status-internal 'expired-key string))
844 (defun epg-status-REVKEYSIG (process string)
845 (epg-signature-status-internal 'revoked-key string))
847 (defun epg-status-BADSIG (process string)
848 (epg-signature-status-internal 'bad string))
850 (defun epg-status-NO_PUBKEY (process string)
851 (epg-context-set-result-for
854 (cons (epg-make-signature 'no-pubkey string)
855 (epg-context-result-for epg-context 'verify))))
857 (defun epg-status-ERRSIG (process string)
858 (let ((signatures (car (epg-context-result-for epg-context 'verify))))
860 (setq signatures (list (epg-make-signature 'error)))
861 (epg-context-set-result-for epg-context 'verify signatures))
862 (when (and (not (eq (epg-signature-status (car signatures)) 'good))
863 (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
864 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
866 (epg-signature-set-key-id
868 (match-string 1 string))
869 (epg-signature-set-pubkey-algorithm
871 (string-to-number (match-string 2 string)))
872 (epg-signature-set-digest-algorithm
874 (string-to-number (match-string 3 string)))
875 ; (epg-signature-set-class
877 ; (string-to-number (match-string 4 string) 16))
878 (epg-signature-set-creation-time
880 (match-string 5 string)))))
882 (defun epg-status-VALIDSIG (process string)
883 (let ((signature (car (epg-context-result-for epg-context 'verify))))
885 (eq (epg-signature-status signature) 'good)
886 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
887 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
890 (epg-signature-set-fingerprint
892 (match-string 1 string))
893 (epg-signature-set-creation-time
895 (match-string 2 string))
896 (epg-signature-set-expiration-time
898 (match-string 3 string))
899 ; (epg-signature-set-version
901 ; (string-to-number (match-string 4 string)))
902 (epg-signature-set-pubkey-algorithm
904 (string-to-number (match-string 5 string)))
905 (epg-signature-set-digest-algorithm
907 (string-to-number (match-string 6 string)))
908 ; (epg-signature-set-class
910 ; (string-to-number (match-string 7 string) 16))
913 (defun epg-status-TRUST_UNDEFINED (process string)
914 (let ((signature (car (epg-context-result-for epg-context 'verify))))
916 (eq (epg-signature-status signature) 'good))
917 (epg-signature-set-validity signature 'undefined))))
919 (defun epg-status-TRUST_NEVER (process string)
920 (let ((signature (car (epg-context-result-for epg-context 'verify))))
922 (eq (epg-signature-status signature) 'good))
923 (epg-signature-set-validity signature 'never))))
925 (defun epg-status-TRUST_MARGINAL (process string)
926 (let ((signature (car (epg-context-result-for epg-context 'verify))))
928 (eq (epg-signature-status signature) 'marginal))
929 (epg-signature-set-validity signature 'marginal))))
931 (defun epg-status-TRUST_FULLY (process string)
932 (let ((signature (car (epg-context-result-for epg-context 'verify))))
934 (eq (epg-signature-status signature) 'good))
935 (epg-signature-set-validity signature 'full))))
937 (defun epg-status-TRUST_ULTIMATE (process string)
938 (let ((signature (car (epg-context-result-for epg-context 'verify))))
940 (eq (epg-signature-status signature) 'good))
941 (epg-signature-set-validity signature 'ultimate))))
943 (defun epg-status-PROGRESS (process string)
944 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
946 (funcall (if (consp (epg-context-progress-callback epg-context))
947 (car (epg-context-progress-callback epg-context))
948 (epg-context-progress-callback epg-context))
950 (match-string 1 string)
951 (match-string 2 string)
952 (string-to-number (match-string 3 string))
953 (string-to-number (match-string 4 string))
954 (if (consp (epg-context-progress-callback epg-context))
955 (cdr (epg-context-progress-callback epg-context))))))
957 (defun epg-status-DECRYPTION_FAILED (process string)
958 (epg-context-set-result-for
960 (cons 'decryption-failed
961 (epg-context-result-for epg-context 'error))))
963 (defun epg-status-NODATA (process string)
964 (epg-context-set-result-for
966 (cons (cons 'no-data (string-to-number string))
967 (epg-context-result-for epg-context 'error))))
969 (defun epg-status-UNEXPECTED (process string)
970 (epg-context-set-result-for
972 (cons (cons 'unexpected (string-to-number string))
973 (epg-context-result-for epg-context 'error))))
975 (defun epg-status-KEYEXPIRED (process string)
976 (epg-context-set-result-for
978 (cons (cons 'key-expired string)
979 (epg-context-result-for epg-context 'error))))
981 (defun epg-status-KEYREVOKED (process string)
982 (epg-context-set-result-for
985 (epg-context-result-for epg-context 'error))))
987 (defun epg-status-BADARMOR (process string)
988 (epg-context-set-result-for
991 (epg-context-result-for epg-context 'error))))
993 (defun epg-status-INV_RECP (process string)
994 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
995 (epg-context-set-result-for
997 (cons (list 'invalid-recipient
998 (string-to-number (match-string 1 string))
999 (match-string 2 string))
1000 (epg-context-result-for epg-context 'error)))))
1002 (defun epg-status-NO_RECP (process string)
1003 (epg-context-set-result-for
1005 (cons 'no-recipients
1006 (epg-context-result-for epg-context 'error))))
1008 (defun epg-status-DELETE_PROBLEM (process string)
1009 (if (string-match "\\`\\([0-9]+\\)" string)
1010 (epg-context-set-result-for
1012 (cons (cons 'delete-problem (string-to-number (match-string 1 string)))
1013 (epg-context-result-for epg-context 'error)))))
1015 (defun epg-status-SIG_CREATED (process string)
1016 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1017 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1018 (epg-context-set-result-for
1020 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1021 (cons 'pubkey-algorithm
1022 (string-to-number (match-string 2 string)))
1023 (cons 'digest-algorithm
1024 (string-to-number (match-string 3 string)))
1025 (cons 'class (string-to-number (match-string 4 string) 16))
1026 (cons 'creation-time (match-string 5 string))
1027 (cons 'fingerprint (substring string (match-end 0))))
1028 (epg-context-result-for epg-context 'sign)))))
1030 (defun epg-passphrase-callback-function (context key-id handback)
1032 (if (eq key-id 'SYM)
1033 "Passphrase for symmetric encryption: "
1034 (if (eq key-id 'PIN)
1035 "Passphrase for PIN: "
1036 (let ((entry (assoc key-id epg-user-id-alist)))
1038 (format "Passphrase for %s %s: " key-id (cdr entry))
1039 (format "Passphrase for %s: " key-id)))))))
1041 (defun epg-progress-callback-function (context what char current total
1043 (message "%s: %d%%/%d%%" what current total))
1045 (defun epg-configuration ()
1046 "Return a list of internal configuration parameters of `epg-gpg-program'."
1049 (apply #'call-process epg-gpg-program nil (list t nil) nil
1050 '("--with-colons" "--list-config"))
1051 (goto-char (point-min))
1052 (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
1053 (setq type (intern (match-string 1))
1054 config (cons (cons type
1056 '(pubkey cipher digest compress))
1057 (mapcar #'string-to-number
1058 (delete "" (split-string
1065 (defun epg-list-keys-1 (context name mode)
1066 (let ((args (append (list "--with-colons" "--no-greeting" "--batch"
1067 "--with-fingerprint"
1068 "--with-fingerprint"
1069 (if (or (eq mode t) (eq mode 'secret))
1070 "--list-secret-keys"
1074 (unless (eq (epg-context-protocol context) 'CMS)
1075 '("--fixed-list-mode"))
1076 (if name (list name))))
1077 keys string field index)
1079 (apply #'call-process
1080 (if (eq (epg-context-protocol context) 'CMS)
1083 nil (list t nil) nil args)
1084 (goto-char (point-min))
1085 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1086 (setq keys (cons (make-vector 15 nil) keys)
1087 string (match-string 0)
1091 (string-match "\\([^:]+\\)?:" string index))
1092 (setq index (match-end 0))
1093 (aset (car keys) field (match-string 1 string))
1094 (setq field (1+ field))))
1097 (defun epg-make-sub-key-1 (line)
1100 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1102 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1104 (member (aref line 0) '("sec" "ssb"))
1105 (string-to-number (aref line 3))
1106 (string-to-number (aref line 2))
1111 (defun epg-list-keys (context &optional name mode)
1112 "Return a list of epg-key objects matched with NAME.
1113 If MODE is nil, only public keyring should be searched.
1114 If MODE is t or 'secret, only secret keyring should be searched.
1115 Otherwise, only public keyring should be searched and the key
1116 signatures should be included."
1117 (let ((lines (epg-list-keys-1 context name mode))
1121 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1123 (epg-key-set-sub-key-list
1125 (nreverse (epg-key-sub-key-list (car keys))))
1126 (epg-key-set-user-id-list
1128 (nreverse (epg-key-user-id-list (car keys)))))
1129 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1130 keys (cons (epg-make-key
1131 (if (aref (car lines) 8)
1132 (cdr (assq (string-to-char (aref (car lines) 8))
1133 epg-key-validity-alist))))
1135 (epg-key-set-sub-key-list
1137 (cons (epg-make-sub-key-1 (car lines))
1138 (epg-key-sub-key-list (car keys)))))
1139 ((member (aref (car lines) 0) '("sub" "ssb"))
1140 (epg-key-set-sub-key-list
1142 (cons (epg-make-sub-key-1 (car lines))
1143 (epg-key-sub-key-list (car keys)))))
1144 ((equal (aref (car lines) 0) "uid")
1145 (epg-key-set-user-id-list
1147 (cons (epg-make-user-id
1148 (if (aref (car lines) 1)
1149 (cdr (assq (string-to-char (aref (car lines) 1))
1150 epg-key-validity-alist)))
1153 (epg-dn-from-string (aref (car lines) 9))
1154 (error (aref (car lines) 9)))
1155 (aref (car lines) 9)))
1156 (epg-key-user-id-list (car keys)))))
1157 ((equal (aref (car lines) 0) "fpr")
1158 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1159 (aref (car lines) 9))))
1160 (setq lines (cdr lines)))
1162 (epg-key-set-sub-key-list
1164 (nreverse (epg-key-sub-key-list (car keys))))
1165 (epg-key-set-user-id-list
1167 (nreverse (epg-key-user-id-list (car keys)))))
1170 (if (fboundp 'make-temp-file)
1171 (defalias 'epg-make-temp-file 'make-temp-file)
1172 ;; stolen from poe.el.
1173 (defun epg-make-temp-file (prefix)
1174 "Create a temporary file.
1175 The returned file name (created by appending some random characters at the end
1176 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1177 is guaranteed to point to a newly created empty file.
1178 You can then use `write-region' to write new data into the file."
1179 (let (tempdir tempfile)
1182 ;; First, create a temporary directory.
1183 (while (condition-case ()
1185 (setq tempdir (make-temp-name
1187 (file-name-directory prefix)
1189 ;; return nil or signal an error.
1190 (make-directory tempdir))
1192 (file-already-exists t)))
1193 (set-file-modes tempdir 448)
1194 ;; Second, create a temporary file in the tempdir.
1195 ;; There *is* a race condition between `make-temp-name'
1196 ;; and `write-region', but we don't care it since we are
1197 ;; in a private directory now.
1198 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1199 (write-region "" nil tempfile nil 'silent)
1200 (set-file-modes tempfile 384)
1201 ;; Finally, make a hard-link from the tempfile.
1202 (while (condition-case ()
1204 (setq file (make-temp-name prefix))
1205 ;; return nil or signal an error.
1206 (add-name-to-file tempfile file))
1208 (file-already-exists t)))
1210 ;; Cleanup the tempfile.
1212 (file-exists-p tempfile)
1213 (delete-file tempfile))
1214 ;; Cleanup the tempdir.
1216 (file-directory-p tempdir)
1217 (delete-directory tempdir))))))
1220 (defun epg-cancel (context)
1221 (if (eq (process-status (epg-context-process context)) 'run)
1222 (delete-process (epg-context-process context))))
1225 (defun epg-start-decrypt (context cipher)
1226 "Initiate a decrypt operation on CIPHER.
1227 CIPHER is a data object.
1229 If you use this function, you will need to wait for the completion of
1230 `epg-gpg-program' by using `epg-wait-for-completion' and call
1231 `epg-reset' to clear a temporaly output file.
1232 If you are unsure, use synchronous version of this function
1233 `epg-decrypt-file' or `epg-decrypt-string' instead."
1234 (unless (epg-data-file cipher)
1235 (error "Not a file"))
1236 (epg-context-set-result context nil)
1237 (epg-start context (list "--decrypt" (epg-data-file cipher)))
1238 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1239 (unless (eq (epg-context-protocol context) 'CMS)
1240 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1243 (defun epg-decrypt-file (context cipher plain)
1244 "Decrypt a file CIPHER and store the result to a file PLAIN.
1245 If PLAIN is nil, it returns the result as a string."
1249 (epg-context-set-output-file context plain)
1250 (epg-context-set-output-file context
1251 (epg-make-temp-file "epg-output")))
1252 (epg-start-decrypt context (epg-make-data-from-file cipher))
1253 (epg-wait-for-completion context)
1254 (if (epg-context-result-for context 'error)
1255 (error "Decrypt failed: %S"
1256 (epg-context-result-for context 'error)))
1258 (epg-read-output context)))
1260 (epg-delete-output-file context))
1261 (epg-reset context)))
1264 (defun epg-decrypt-string (context cipher)
1265 "Decrypt a string CIPHER and return the plain text."
1266 (let ((input-file (epg-make-temp-file "epg-input"))
1267 (coding-system-for-write 'binary))
1270 (write-region cipher nil input-file nil 'quiet)
1271 (epg-context-set-output-file context
1272 (epg-make-temp-file "epg-output"))
1273 (epg-start-decrypt context (epg-make-data-from-file input-file))
1275 (epg-wait-for-completion context)
1276 (if (epg-context-result-for context 'error)
1277 (error "Decrypt failed: %S"
1278 (epg-context-result-for context 'error)))
1279 (epg-read-output context))
1280 (epg-delete-output-file context)
1281 (if (file-exists-p input-file)
1282 (delete-file input-file))
1283 (epg-reset context))))
1286 (defun epg-start-verify (context signature &optional signed-text)
1287 "Initiate a verify operation on SIGNATURE.
1288 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1290 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1291 For a normal or a clear text signature, SIGNED-TEXT should be nil.
1293 If you use this function, you will need to wait for the completion of
1294 `epg-gpg-program' by using `epg-wait-for-completion' and call
1295 `epg-reset' to clear a temporaly output file.
1296 If you are unsure, use synchronous version of this function
1297 `epg-verify-file' or `epg-verify-string' instead."
1298 (epg-context-set-result context nil)
1300 ;; Detached signature.
1301 (if (epg-data-file signed-text)
1302 (epg-start context (list "--verify" (epg-data-file signature)
1303 (epg-data-file signed-text)))
1304 (epg-start context (list "--verify" (epg-data-file signature) "-"))
1305 (if (eq (process-status (epg-context-process context)) 'run)
1306 (process-send-string (epg-context-process context)
1307 (epg-data-string signed-text))))
1308 ;; Normal (or cleartext) signature.
1309 (if (epg-data-file signature)
1310 (epg-start context (list "--verify" (epg-data-file signature)))
1311 (epg-start context (list "--verify"))
1312 (if (eq (process-status (epg-context-process context)) 'run)
1313 (process-send-string (epg-context-process context)
1314 (epg-data-string signature))))))
1317 (defun epg-verify-file (context signature &optional signed-text plain)
1318 "Verify a file SIGNATURE.
1319 SIGNED-TEXT and PLAIN are also a file if they are specified.
1321 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1322 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1326 (epg-context-set-output-file context plain)
1327 (epg-context-set-output-file context
1328 (epg-make-temp-file "epg-output")))
1330 (epg-start-verify context
1331 (epg-make-data-from-file signature)
1332 (epg-make-data-from-file signed-text))
1333 (epg-start-verify context
1334 (epg-make-data-from-file signature)))
1335 (epg-wait-for-completion context)
1336 ; (if (epg-context-result-for context 'error)
1337 ; (error "Verify failed: %S"
1338 ; (epg-context-result-for context 'error)))
1340 (epg-read-output context)))
1342 (epg-delete-output-file context))
1343 (epg-reset context)))
1346 (defun epg-verify-string (context signature &optional signed-text)
1347 "Verify a string SIGNATURE.
1348 SIGNED-TEXT is a string if it is specified.
1350 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1351 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1352 (let ((coding-system-for-write 'binary)
1356 (epg-context-set-output-file context
1357 (epg-make-temp-file "epg-output"))
1360 (setq input-file (epg-make-temp-file "epg-signature"))
1361 (write-region signature nil input-file nil 'quiet)
1362 (epg-start-verify context
1363 (epg-make-data-from-file input-file)
1364 (epg-make-data-from-string signed-text)))
1365 (epg-start-verify context (epg-make-data-from-string signature)))
1367 (epg-wait-for-completion context)
1368 ; (if (epg-context-result-for context 'error)
1369 ; (error "Verify failed: %S"
1370 ; (epg-context-result-for context 'error)))
1371 (epg-read-output context))
1372 (epg-delete-output-file context)
1374 (file-exists-p input-file))
1375 (delete-file input-file))
1376 (epg-reset context))))
1379 (defun epg-start-sign (context plain &optional mode)
1380 "Initiate a sign operation on PLAIN.
1381 PLAIN is a data object.
1383 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1384 If MODE is t or 'detached, it makes a detached signature.
1385 Otherwise, it makes a normal signature.
1387 If you use this function, you will need to wait for the completion of
1388 `epg-gpg-program' by using `epg-wait-for-completion' and call
1389 `epg-reset' to clear a temporaly output file.
1390 If you are unsure, use synchronous version of this function
1391 `epg-sign-file' or `epg-sign-string' instead."
1392 (epg-context-set-result context nil)
1394 (append (list (if (eq mode 'clearsign)
1396 (if (or (eq mode t) (eq mode 'detached))
1404 (car (epg-key-sub-key-list signer)))))
1405 (epg-context-signers context)))
1406 (if (epg-data-file plain)
1407 (list (epg-data-file plain)))))
1408 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1409 (unless (eq (epg-context-protocol context) 'CMS)
1410 (epg-wait-for-status context '("BEGIN_SIGNING")))
1411 (if (and (epg-data-string plain)
1412 (eq (process-status (epg-context-process context)) 'run))
1413 (process-send-string (epg-context-process context)
1414 (epg-data-string plain))))
1417 (defun epg-sign-file (context plain signature &optional mode)
1418 "Sign a file PLAIN and store the result to a file SIGNATURE.
1419 If SIGNATURE is nil, it returns the result as a string.
1420 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1421 If MODE is t or 'detached, it makes a detached signature.
1422 Otherwise, it makes a normal signature."
1426 (epg-context-set-output-file context signature)
1427 (epg-context-set-output-file context
1428 (epg-make-temp-file "epg-output")))
1429 (epg-start-sign context (epg-make-data-from-file plain) mode)
1430 (epg-wait-for-completion context)
1431 (if (epg-context-result-for context 'sign)
1432 (if (epg-context-result-for context 'error)
1433 (message "Sign warning: %S"
1434 (epg-context-result-for context 'error)))
1435 (if (epg-context-result-for context 'error)
1436 (error "Sign failed: %S"
1437 (epg-context-result-for context 'error))
1438 (error "Sign failed")))
1440 (epg-read-output context)))
1442 (epg-delete-output-file context))
1443 (epg-reset context)))
1446 (defun epg-sign-string (context plain &optional mode)
1447 "Sign a string PLAIN and return the output as string.
1448 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1449 If MODE is t or 'detached, it makes a detached signature.
1450 Otherwise, it makes a normal signature."
1453 (epg-context-set-output-file context
1454 (epg-make-temp-file "epg-output"))
1455 (epg-start-sign context (epg-make-data-from-string plain) mode)
1457 (epg-wait-for-completion context)
1458 (unless (epg-context-result-for context 'sign)
1459 (if (epg-context-result-for context 'error)
1460 (error "Sign failed: %S"
1461 (epg-context-result-for context 'error))
1462 (error "Sign failed")))
1463 (epg-read-output context))
1464 (epg-delete-output-file context)
1465 (epg-reset context)))
1468 (defun epg-start-encrypt (context plain recipients
1469 &optional sign always-trust)
1470 "Initiate an encrypt operation on PLAIN.
1471 PLAIN is a data object.
1472 If RECIPIENTS is nil, it performs symmetric encryption.
1474 If you use this function, you will need to wait for the completion of
1475 `epg-gpg-program' by using `epg-wait-for-completion' and call
1476 `epg-reset' to clear a temporaly output file.
1477 If you are unsure, use synchronous version of this function
1478 `epg-encrypt-file' or `epg-encrypt-string' instead."
1479 (epg-context-set-result context nil)
1481 (append (if always-trust '("--always-trust"))
1482 (if recipients '("--encrypt") '("--symmetric"))
1486 (mapcar (lambda (signer)
1488 (epg-context-signers context)))))
1494 (car (epg-key-sub-key-list recipient)))))
1496 (if (epg-data-file plain)
1497 (list (epg-data-file plain)))))
1498 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1499 (unless (eq (epg-context-protocol context) 'CMS)
1501 (epg-wait-for-status context '("BEGIN_SIGNING"))
1502 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
1503 (if (and (epg-data-string plain)
1504 (eq (process-status (epg-context-process context)) 'run))
1505 (process-send-string (epg-context-process context)
1506 (epg-data-string plain))))
1509 (defun epg-encrypt-file (context plain recipients
1510 cipher &optional sign always-trust)
1511 "Encrypt a file PLAIN and store the result to a file CIPHER.
1512 If CIPHER is nil, it returns the result as a string.
1513 If RECIPIENTS is nil, it performs symmetric encryption."
1517 (epg-context-set-output-file context cipher)
1518 (epg-context-set-output-file context
1519 (epg-make-temp-file "epg-output")))
1520 (epg-start-encrypt context (epg-make-data-from-file plain)
1521 recipients sign always-trust)
1522 (epg-wait-for-completion context)
1524 (not (epg-context-result-for context 'sign)))
1525 (if (epg-context-result-for context 'error)
1526 (error "Sign failed: %S"
1527 (epg-context-result-for context 'error))
1528 (error "Sign failed")))
1529 (if (epg-context-result-for context 'error)
1530 (error "Encrypt failed: %S"
1531 (epg-context-result-for context 'error)))
1533 (epg-read-output context)))
1535 (epg-delete-output-file context))
1536 (epg-reset context)))
1539 (defun epg-encrypt-string (context plain recipients
1540 &optional sign always-trust)
1541 "Encrypt a string PLAIN.
1542 If RECIPIENTS is nil, it performs symmetric encryption."
1545 (epg-context-set-output-file context
1546 (epg-make-temp-file "epg-output"))
1547 (epg-start-encrypt context (epg-make-data-from-string plain)
1548 recipients sign always-trust)
1550 (epg-wait-for-completion context)
1552 (not (epg-context-result-for context 'sign)))
1553 (if (epg-context-result-for context 'error)
1554 (error "Sign failed: %S"
1555 (epg-context-result-for context 'error))
1556 (error "Sign failed")))
1557 (if (epg-context-result-for context 'error)
1558 (error "Encrypt failed: %S"
1559 (epg-context-result-for context 'error)))
1560 (epg-read-output context))
1561 (epg-delete-output-file context)
1562 (epg-reset context)))
1565 (defun epg-start-export-keys (context keys)
1566 "Initiate an export keys operation.
1568 If you use this function, you will need to wait for the completion of
1569 `epg-gpg-program' by using `epg-wait-for-completion' and call
1570 `epg-reset' to clear a temporaly output file.
1571 If you are unsure, use synchronous version of this function
1572 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1573 (epg-context-set-result context nil)
1574 (epg-start context (cons "--export"
1578 (car (epg-key-sub-key-list key))))
1582 (defun epg-export-keys-to-file (context keys file)
1583 "Extract public KEYS."
1587 (epg-context-set-output-file context file)
1588 (epg-context-set-output-file context
1589 (epg-make-temp-file "epg-output")))
1590 (epg-start-export-keys context keys)
1591 (epg-wait-for-completion context)
1592 (if (epg-context-result-for context 'error)
1593 (error "Export keys failed: %S"
1594 (epg-context-result-for context 'error)))
1596 (epg-read-output context)))
1598 (epg-delete-output-file context))
1599 (epg-reset context)))
1602 (defun epg-export-keys-to-string (context keys)
1603 "Extract public KEYS and return them as a string."
1604 (epg-export-keys-to-file context keys nil))
1607 (defun epg-start-import-keys (context keys)
1608 "Initiate an import keys operation.
1609 KEYS is a data object.
1611 If you use this function, you will need to wait for the completion of
1612 `epg-gpg-program' by using `epg-wait-for-completion' and call
1613 `epg-reset' to clear a temporaly output file.
1614 If you are unsure, use synchronous version of this function
1615 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1616 (epg-context-set-result context nil)
1617 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
1618 (epg-start context (list "--import" (epg-data-file keys)))
1619 (if (and (epg-data-string keys)
1620 (eq (process-status (epg-context-process context)) 'run))
1621 (process-send-string (epg-context-process context)
1622 (epg-data-string keys))))
1624 (defun epg-import-keys-1 (context keys)
1627 (epg-start-import-keys context keys)
1628 (if (epg-data-file keys)
1629 (epg-flush context))
1630 (epg-wait-for-completion context)
1631 (if (epg-context-result-for context 'error)
1632 (error "Import keys failed: %S"
1633 (epg-context-result-for context 'error)))
1634 (epg-read-output context))
1635 (epg-reset context)))
1638 (defun epg-import-keys-from-file (context keys)
1639 "Add keys from a file KEYS."
1640 (epg-import-keys-1 context (epg-make-data-from-file keys)))
1643 (defun epg-import-keys-from-string (context keys)
1644 "Add keys from a string KEYS."
1645 (epg-import-keys-1 context (epg-make-data-from-string keys)))
1648 (defun epg-start-delete-keys (context keys &optional allow-secret)
1649 "Initiate an delete keys operation.
1651 If you use this function, you will need to wait for the completion of
1652 `epg-gpg-program' by using `epg-wait-for-completion' and call
1653 `epg-reset' to clear a temporaly output file.
1654 If you are unsure, use synchronous version of this function
1655 `epg-delete-keys' instead."
1656 (epg-context-set-result context nil)
1657 (epg-start context (cons (if allow-secret
1658 "--delete-secret-key"
1663 (car (epg-key-sub-key-list key))))
1667 (defun epg-delete-keys (context keys &optional allow-secret)
1668 "Delete KEYS from the key ring."
1671 (epg-start-delete-keys context keys allow-secret)
1672 (epg-wait-for-completion context)
1673 (if (epg-context-result-for context 'error)
1674 (error "Delete keys failed: %S"
1675 (epg-context-result-for context 'error))))
1676 (epg-reset context)))
1679 (defun epg-start-sign-keys (context keys &optional local)
1680 "Initiate an sign keys operation.
1682 If you use this function, you will need to wait for the completion of
1683 `epg-gpg-program' by using `epg-wait-for-completion' and call
1684 `epg-reset' to clear a temporaly output file.
1685 If you are unsure, use synchronous version of this function
1686 `epg-sign-keys' instead."
1687 (epg-context-set-result context nil)
1688 (epg-start context (cons (if local
1694 (car (epg-key-sub-key-list key))))
1698 (defun epg-sign-keys (context keys &optional local)
1699 "Sign KEYS from the key ring."
1702 (epg-start-sign-keys context keys local)
1703 (epg-wait-for-completion context)
1704 (if (epg-context-result-for context 'error)
1705 (error "Sign keys failed: %S"
1706 (epg-context-result-for context 'error))))
1707 (epg-reset context)))
1709 (defun epg-decode-hexstring (string)
1711 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
1712 (setq string (replace-match "\\x\\&" t nil string)
1714 (car (read-from-string (concat "\"" string "\"")))))
1716 (defun epg-decode-quotedstring (string)
1718 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
1719 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
1721 (if (match-beginning 2)
1722 (setq string (replace-match "\\2" t nil string)
1724 (if (match-beginning 3)
1725 (setq string (replace-match "\\x\\3" t nil string)
1727 (setq string (replace-match "\\\\\\\\\\4" t nil string)
1728 index (+ index 3)))))
1729 (car (read-from-string (concat "\"" string "\"")))))
1731 (defun epg-dn-from-string (string)
1732 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
1733 The return value is an alist mapping from types to values."
1735 (length (length string))
1736 alist type value group)
1737 (while (< index length)
1738 (if (eq index (string-match "[ \t\n\r]*" string index))
1739 (setq index (match-end 0)))
1740 (if (eq index (string-match
1741 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
1743 (setq type (match-string 1 string)
1744 index (match-end 0))
1745 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
1747 (setq type (match-string 1 string)
1748 index (match-end 0))))
1750 (error "Invalid type"))
1751 (if (eq index (string-match
1752 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
1754 (setq index (match-end 0)
1755 value (epg-decode-quotedstring (match-string 0 string)))
1756 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
1757 (setq index (match-end 0)
1758 value (epg-decode-hexstring (match-string 1 string)))
1759 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
1761 (setq index (match-end 0)
1762 value (epg-decode-quotedstring (match-string 0 string))))))
1764 (if (stringp (car (car alist)))
1765 (setcar alist (list (cons type value) (car alist)))
1766 (setcar alist (cons (cons type value) (car alist))))
1767 (if (consp (car (car alist)))
1768 (setcar alist (nreverse (car alist))))
1769 (setq alist (cons (cons type value) alist)
1772 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
1773 (setq index (match-end 0)
1774 group (eq (aref string (match-beginning 1)) ?+))))
1777 (defun epg-decode-dn (alist)
1778 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
1779 Type names are resolved using `epg-dn-type-alist'."
1782 (if (stringp (car rdn))
1783 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
1785 (format "%s=%s" (cdr entry) (cdr rdn))
1786 (format "%s=%s" (car rdn) (cdr rdn))))
1787 (concat "(" (epg-decode-dn rdn) ")")))
1793 ;;; epg.el ends here