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-new-signature-type-alist
136 (defvar epg-dn-type-alist
137 '(("1.2.840.113549.1.9.1" . "EMail")
141 ("0.2.262.1.10.7.20" . "NameDistinguisher")
142 ("2.5.4.16" . "ADDR")
145 ("2.5.4.17" . "PostalCode")
146 ("2.5.4.65" . "Pseudo")
147 ("2.5.4.5" . "SerialNumber")))
149 (defvar epg-prompt-alist nil)
151 (defun epg-make-data-from-file (file)
152 "Make a data object from FILE."
153 (cons 'epg-data (vector file nil)))
155 (defun epg-make-data-from-string (string)
156 "Make a data object from STRING."
157 (cons 'epg-data (vector nil string)))
159 (defun epg-data-file (data)
160 "Return the file of DATA."
161 (unless (eq (car data) 'epg-data)
162 (signal 'wrong-type-argument (list 'epg-data-p data)))
165 (defun epg-data-string (data)
166 "Return the string of DATA."
167 (unless (eq (car data) 'epg-data)
168 (signal 'wrong-type-argument (list 'epg-data-p data)))
171 (defun epg-make-context (&optional protocol armor textmode include-certs
172 cipher-algorithm digest-algorithm
174 "Return a context object."
176 (vector (or protocol 'OpenPGP) armor textmode include-certs
177 cipher-algorithm digest-algorithm compress-algorithm
178 #'epg-passphrase-callback-function
179 #'epg-progress-callback-function
182 (defun epg-context-protocol (context)
183 "Return the protocol used within CONTEXT."
184 (unless (eq (car context) 'epg-context)
185 (signal 'wrong-type-argument (list 'epg-context-p context)))
186 (aref (cdr context) 0))
188 (defun epg-context-armor (context)
189 "Return t if the output shouled be ASCII armored in CONTEXT."
190 (unless (eq (car context) 'epg-context)
191 (signal 'wrong-type-argument (list 'epg-context-p context)))
192 (aref (cdr context) 1))
194 (defun epg-context-textmode (context)
195 "Return t if canonical text mode should be used in CONTEXT."
196 (unless (eq (car context) 'epg-context)
197 (signal 'wrong-type-argument (list 'epg-context-p context)))
198 (aref (cdr context) 2))
200 (defun epg-context-include-certs (context)
201 "Return how many certificates should be included in an S/MIME signed
203 (unless (eq (car context) 'epg-context)
204 (signal 'wrong-type-argument (list 'epg-context-p context)))
205 (aref (cdr context) 3))
207 (defun epg-context-cipher-algorithm (context)
208 "Return the cipher algorithm in CONTEXT."
209 (unless (eq (car context) 'epg-context)
210 (signal 'wrong-type-argument (list 'epg-context-p context)))
211 (aref (cdr context) 4))
213 (defun epg-context-digest-algorithm (context)
214 "Return the digest algorithm in CONTEXT."
215 (unless (eq (car context) 'epg-context)
216 (signal 'wrong-type-argument (list 'epg-context-p context)))
217 (aref (cdr context) 5))
219 (defun epg-context-compress-algorithm (context)
220 "Return the compress algorithm in CONTEXT."
221 (unless (eq (car context) 'epg-context)
222 (signal 'wrong-type-argument (list 'epg-context-p context)))
223 (aref (cdr context) 6))
225 (defun epg-context-passphrase-callback (context)
226 "Return the function used to query passphrase."
227 (unless (eq (car context) 'epg-context)
228 (signal 'wrong-type-argument (list 'epg-context-p context)))
229 (aref (cdr context) 7))
231 (defun epg-context-progress-callback (context)
232 "Return the function which handles progress update."
233 (unless (eq (car context) 'epg-context)
234 (signal 'wrong-type-argument (list 'epg-context-p context)))
235 (aref (cdr context) 8))
237 (defun epg-context-signers (context)
238 "Return the list of key-id for singning."
239 (unless (eq (car context) 'epg-context)
240 (signal 'wrong-type-argument (list 'epg-context-p context)))
241 (aref (cdr context) 9))
243 (defun epg-context-process (context)
244 "Return the process object of `epg-gpg-program'.
245 This function is for internal use only."
246 (unless (eq (car context) 'epg-context)
247 (signal 'wrong-type-argument (list 'epg-context-p context)))
248 (aref (cdr context) 10))
250 (defun epg-context-output-file (context)
251 "Return the output file of `epg-gpg-program'.
252 This function is for internal use only."
253 (unless (eq (car context) 'epg-context)
254 (signal 'wrong-type-argument (list 'epg-context-p context)))
255 (aref (cdr context) 11))
257 (defun epg-context-result (context)
258 "Return the result of the previous cryptographic operation."
259 (unless (eq (car context) 'epg-context)
260 (signal 'wrong-type-argument (list 'epg-context-p context)))
261 (aref (cdr context) 12))
263 (defun epg-context-set-protocol (context protocol)
264 "Set the protocol used within CONTEXT."
265 (unless (eq (car context) 'epg-context)
266 (signal 'wrong-type-argument (list 'epg-context-p context)))
267 (aset (cdr context) 0 protocol))
269 (defun epg-context-set-armor (context armor)
270 "Specify if the output shouled be ASCII armored in CONTEXT."
271 (unless (eq (car context) 'epg-context)
272 (signal 'wrong-type-argument (list 'epg-context-p context)))
273 (aset (cdr context) 1 armor))
275 (defun epg-context-set-textmode (context textmode)
276 "Specify if canonical text mode should be used in CONTEXT."
277 (unless (eq (car context) 'epg-context)
278 (signal 'wrong-type-argument (list 'epg-context-p context)))
279 (aset (cdr context) 2 textmode))
281 (defun epg-context-set-include-certs (context include-certs)
282 "Set how many certificates should be included in an S/MIME signed message."
283 (unless (eq (car context) 'epg-context)
284 (signal 'wrong-type-argument (list 'epg-context-p context)))
285 (aset (cdr context) 3 include-certs))
287 (defun epg-context-set-cipher-algorithm (context cipher-algorithm)
288 "Set the cipher algorithm in CONTEXT."
289 (unless (eq (car context) 'epg-context)
290 (signal 'wrong-type-argument (list 'epg-context-p context)))
291 (aset (cdr context) 4 cipher-algorithm))
293 (defun epg-context-set-digest-algorithm (context digest-algorithm)
294 "Set the digest algorithm in CONTEXT."
295 (unless (eq (car context) 'epg-context)
296 (signal 'wrong-type-argument (list 'epg-context-p context)))
297 (aset (cdr context) 5 digest-algorithm))
299 (defun epg-context-set-compress-algorithm (context compress-algorithm)
300 "Set the compress algorithm in CONTEXT."
301 (unless (eq (car context) 'epg-context)
302 (signal 'wrong-type-argument (list 'epg-context-p context)))
303 (aset (cdr context) 6 compress-algorithm))
305 (defun epg-context-set-passphrase-callback (context
307 "Set the function used to query passphrase."
308 (unless (eq (car context) 'epg-context)
309 (signal 'wrong-type-argument (list 'epg-context-p context)))
310 (aset (cdr context) 7 passphrase-callback))
312 (defun epg-context-set-progress-callback (context progress-callback)
313 "Set the function which handles progress update."
314 (unless (eq (car context) 'epg-context)
315 (signal 'wrong-type-argument (list 'epg-context-p context)))
316 (aset (cdr context) 8 progress-callback))
318 (defun epg-context-set-signers (context signers)
319 "Set the list of key-id for singning."
320 (unless (eq (car context) 'epg-context)
321 (signal 'wrong-type-argument (list 'epg-context-p context)))
322 (aset (cdr context) 9 signers))
324 (defun epg-context-set-process (context process)
325 "Set the process object of `epg-gpg-program'.
326 This function is for internal use only."
327 (unless (eq (car context) 'epg-context)
328 (signal 'wrong-type-argument (list 'epg-context-p context)))
329 (aset (cdr context) 10 process))
331 (defun epg-context-set-output-file (context output-file)
332 "Set the output file of `epg-gpg-program'.
333 This function is for internal use only."
334 (unless (eq (car context) 'epg-context)
335 (signal 'wrong-type-argument (list 'epg-context-p context)))
336 (aset (cdr context) 11 output-file))
338 (defun epg-context-set-result (context result)
339 "Set the result of the previous cryptographic operation."
340 (unless (eq (car context) 'epg-context)
341 (signal 'wrong-type-argument (list 'epg-context-p context)))
342 (aset (cdr context) 12 result))
344 (defun epg-make-signature (status &optional key-id)
345 "Return a signature object."
346 (cons 'epg-signature (vector status key-id nil nil nil nil nil nil)))
348 (defun epg-signature-status (signature)
349 "Return the status code of SIGNATURE."
350 (unless (eq (car signature) 'epg-signature)
351 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
352 (aref (cdr signature) 0))
354 (defun epg-signature-key-id (signature)
355 "Return the key-id of SIGNATURE."
356 (unless (eq (car signature) 'epg-signature)
357 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
358 (aref (cdr signature) 1))
360 (defun epg-signature-validity (signature)
361 "Return the validity of SIGNATURE."
362 (unless (eq (car signature) 'epg-signature)
363 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
364 (aref (cdr signature) 2))
366 (defun epg-signature-fingerprint (signature)
367 "Return the fingerprint of SIGNATURE."
368 (unless (eq (car signature) 'epg-signature)
369 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
370 (aref (cdr signature) 3))
372 (defun epg-signature-creation-time (signature)
373 "Return the creation time of SIGNATURE."
374 (unless (eq (car signature) 'epg-signature)
375 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
376 (aref (cdr signature) 4))
378 (defun epg-signature-expiration-time (signature)
379 "Return the expiration time of SIGNATURE."
380 (unless (eq (car signature) 'epg-signature)
381 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
382 (aref (cdr signature) 5))
384 (defun epg-signature-pubkey-algorithm (signature)
385 "Return the public key algorithm of SIGNATURE."
386 (unless (eq (car signature) 'epg-signature)
387 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
388 (aref (cdr signature) 6))
390 (defun epg-signature-digest-algorithm (signature)
391 "Return the digest algorithm of SIGNATURE."
392 (unless (eq (car signature) 'epg-signature)
393 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
394 (aref (cdr signature) 7))
396 (defun epg-signature-set-status (signature status)
397 "Set the status code of SIGNATURE."
398 (unless (eq (car signature) 'epg-signature)
399 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
400 (aset (cdr signature) 0 status))
402 (defun epg-signature-set-key-id (signature key-id)
403 "Set the key-id of SIGNATURE."
404 (unless (eq (car signature) 'epg-signature)
405 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
406 (aset (cdr signature) 1 key-id))
408 (defun epg-signature-set-validity (signature validity)
409 "Set the validity of SIGNATURE."
410 (unless (eq (car signature) 'epg-signature)
411 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
412 (aset (cdr signature) 2 validity))
414 (defun epg-signature-set-fingerprint (signature fingerprint)
415 "Set the fingerprint of SIGNATURE."
416 (unless (eq (car signature) 'epg-signature)
417 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
418 (aset (cdr signature) 3 fingerprint))
420 (defun epg-signature-set-creation-time (signature creation-time)
421 "Set the creation time of SIGNATURE."
422 (unless (eq (car signature) 'epg-signature)
423 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
424 (aset (cdr signature) 4 creation-time))
426 (defun epg-signature-set-expiration-time (signature expiration-time)
427 "Set the expiration time of SIGNATURE."
428 (unless (eq (car signature) 'epg-signature)
429 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
430 (aset (cdr signature) 5 expiration-time))
432 (defun epg-signature-set-pubkey-algorithm (signature pubkey-algorithm)
433 "Set the public key algorithm of SIGNATURE."
434 (unless (eq (car signature) 'epg-signature)
435 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
436 (aset (cdr signature) 6 pubkey-algorithm))
438 (defun epg-signature-set-digest-algorithm (signature digest-algorithm)
439 "Set the digest algorithm of SIGNATURE."
440 (unless (eq (car signature) 'epg-signature)
441 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
442 (aset (cdr signature) 7 digest-algorithm))
444 (defun epg-make-new-signature (type pubkey-algorithm digest-algorithm
445 class creation-time fingerprint)
446 "Return a new signature object."
447 (cons 'epg-new-signature (vector type pubkey-algorithm digest-algorithm
448 class creation-time fingerprint)))
450 (defun epg-new-signature-type (new-signature)
451 "Return the type of NEW-SIGNATURE."
452 (unless (eq (car new-signature) 'epg-new-signature)
453 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
454 (aref (cdr new-signature) 0))
456 (defun epg-new-signature-pubkey-algorithm (new-signature)
457 "Return the public key algorithm of NEW-SIGNATURE."
458 (unless (eq (car new-signature) 'epg-new-signature)
459 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
460 (aref (cdr new-signature) 1))
462 (defun epg-new-signature-digest-algorithm (new-signature)
463 "Return the digest algorithm of NEW-SIGNATURE."
464 (unless (eq (car new-signature) 'epg-new-signature)
465 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
466 (aref (cdr new-signature) 2))
468 (defun epg-new-signature-class (new-signature)
469 "Return the class of NEW-SIGNATURE."
470 (unless (eq (car new-signature) 'epg-new-signature)
471 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
472 (aref (cdr new-signature) 3))
474 (defun epg-new-signature-creation-time (new-signature)
475 "Return the creation time of NEW-SIGNATURE."
476 (unless (eq (car new-signature) 'epg-new-signature)
477 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
478 (aref (cdr new-signature) 4))
480 (defun epg-new-signature-fingerprint (new-signature)
481 "Return the fingerprint of NEW-SIGNATURE."
482 (unless (eq (car new-signature) 'epg-new-signature)
483 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
484 (aref (cdr new-signature) 5))
486 (defun epg-make-key (owner-trust)
487 "Return a key object."
488 (cons 'epg-key (vector owner-trust nil nil)))
490 (defun epg-key-owner-trust (key)
491 "Return the owner trust of KEY."
492 (unless (eq (car key) 'epg-key)
493 (signal 'wrong-type-argument (list 'epg-key-p key)))
496 (defun epg-key-sub-key-list (key)
497 "Return the sub key list of KEY."
498 (unless (eq (car key) 'epg-key)
499 (signal 'wrong-type-argument (list 'epg-key-p key)))
502 (defun epg-key-user-id-list (key)
503 "Return the user ID list of KEY."
504 (unless (eq (car key) 'epg-key)
505 (signal 'wrong-type-argument (list 'epg-key-p key)))
508 (defun epg-key-set-sub-key-list (key sub-key-list)
509 "Set the sub key list of KEY."
510 (unless (eq (car key) 'epg-key)
511 (signal 'wrong-type-argument (list 'epg-key-p key)))
512 (aset (cdr key) 1 sub-key-list))
514 (defun epg-key-set-user-id-list (key user-id-list)
515 "Set the user ID list of KEY."
516 (unless (eq (car key) 'epg-key)
517 (signal 'wrong-type-argument (list 'epg-key-p key)))
518 (aset (cdr key) 2 user-id-list))
520 (defun epg-make-sub-key (validity capability secret algorithm length id
521 creation-time expiration-time)
522 "Return a sub key object."
524 (vector validity capability secret algorithm length id creation-time
525 expiration-time nil)))
527 (defun epg-sub-key-validity (sub-key)
528 "Return the validity of SUB-KEY."
529 (unless (eq (car sub-key) 'epg-sub-key)
530 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
531 (aref (cdr sub-key) 0))
533 (defun epg-sub-key-capability (sub-key)
534 "Return the capability of SUB-KEY."
535 (unless (eq (car sub-key) 'epg-sub-key)
536 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
537 (aref (cdr sub-key) 1))
539 (defun epg-sub-key-secret (sub-key)
540 "Return non-nil if SUB-KEY is a secret key."
541 (unless (eq (car sub-key) 'epg-sub-key)
542 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
543 (aref (cdr sub-key) 2))
545 (defun epg-sub-key-algorithm (sub-key)
546 "Return the algorithm of SUB-KEY."
547 (unless (eq (car sub-key) 'epg-sub-key)
548 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
549 (aref (cdr sub-key) 3))
551 (defun epg-sub-key-length (sub-key)
552 "Return the length of SUB-KEY."
553 (unless (eq (car sub-key) 'epg-sub-key)
554 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
555 (aref (cdr sub-key) 4))
557 (defun epg-sub-key-id (sub-key)
558 "Return the ID of SUB-KEY."
559 (unless (eq (car sub-key) 'epg-sub-key)
560 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
561 (aref (cdr sub-key) 5))
563 (defun epg-sub-key-creation-time (sub-key)
564 "Return the creation time of SUB-KEY."
565 (unless (eq (car sub-key) 'epg-sub-key)
566 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
567 (aref (cdr sub-key) 6))
569 (defun epg-sub-key-expiration-time (sub-key)
570 "Return the expiration time of SUB-KEY."
571 (unless (eq (car sub-key) 'epg-sub-key)
572 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
573 (aref (cdr sub-key) 7))
575 (defun epg-sub-key-fingerprint (sub-key)
576 "Return the fingerprint of SUB-KEY."
577 (unless (eq (car sub-key) 'epg-sub-key)
578 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
579 (aref (cdr sub-key) 8))
581 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
582 "Set the fingerprint of SUB-KEY.
583 This function is for internal use only."
584 (unless (eq (car sub-key) 'epg-sub-key)
585 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
586 (aset (cdr sub-key) 8 fingerprint))
588 (defun epg-make-user-id (validity string)
589 "Return a user ID object."
590 (cons 'epg-user-id (vector validity string nil)))
592 (defun epg-user-id-validity (user-id)
593 "Return the validity of USER-ID."
594 (unless (eq (car user-id) 'epg-user-id)
595 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
596 (aref (cdr user-id) 0))
598 (defun epg-user-id-string (user-id)
599 "Return the name of USER-ID."
600 (unless (eq (car user-id) 'epg-user-id)
601 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
602 (aref (cdr user-id) 1))
604 (defun epg-user-id-signature-list (user-id)
605 "Return the signature list of USER-ID."
606 (unless (eq (car user-id) 'epg-user-id)
607 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
608 (aref (cdr user-id) 2))
610 (defun epg-user-id-set-signature-list (user-id signature-list)
611 "Set the signature list of USER-ID."
612 (unless (eq (car user-id) 'epg-user-id)
613 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
614 (aset (cdr user-id) 2 signature-list))
616 (defun epg-context-result-for (context name)
617 "Return the result of CONTEXT associated with NAME."
618 (cdr (assq name (epg-context-result context))))
620 (defun epg-context-set-result-for (context name value)
621 "Set the result of CONTEXT associated with NAME to VALUE."
622 (let* ((result (epg-context-result context))
623 (entry (assq name result)))
626 (epg-context-set-result context (cons (cons name value) result)))))
628 (defun epg-signature-to-string (signature)
629 "Convert SIGNATURE to a human readable string."
630 (let ((user-id (cdr (assoc (epg-signature-key-id signature)
631 epg-user-id-alist))))
633 (cond ((eq (epg-signature-status signature) 'good)
634 "Good signature from ")
635 ((eq (epg-signature-status signature) 'bad)
636 "Bad signature from ")
637 ((eq (epg-signature-status signature) 'expired)
638 "Expired signature from ")
639 ((eq (epg-signature-status signature) 'expired-key)
640 "Signature made by expired key ")
641 ((eq (epg-signature-status signature) 'revoked-key)
642 "Signature made by revoked key ")
643 ((eq (epg-signature-status signature) 'no-pubkey)
644 "No public key for "))
645 (epg-signature-key-id signature)
648 (concat (if (stringp user-id)
650 (epg-decode-dn user-id))
653 (if (epg-signature-validity signature)
654 (format "(trust %s)" (epg-signature-validity signature))
657 (defun epg-verify-result-to-string (verify-result)
658 "Convert VERIFY-RESULT to a human readable string."
659 (mapconcat #'epg-signature-to-string verify-result "\n"))
661 (defun epg-start (context args)
662 "Start `epg-gpg-program' in a subprocess with given ARGS."
663 (if (and (epg-context-process context)
664 (eq (process-status (epg-context-process context)) 'run))
665 (error "%s is already running in this context"
666 (if (eq (epg-context-protocol context) 'CMS)
669 (let* ((args (append (list "--no-tty"
672 (unless (eq (epg-context-protocol context) 'CMS)
673 (list "--command-fd" "0"))
674 (if (epg-context-armor context) '("--armor"))
675 (if (epg-context-textmode context) '("--textmode"))
676 (if (epg-context-output-file context)
677 (list "--output" (epg-context-output-file context)))
679 (coding-system-for-write 'binary)
680 process-connection-type
681 (orig-mode (default-file-modes))
682 (buffer (generate-new-buffer " *epg*"))
686 (unless epg-debug-buffer
687 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
688 (set-buffer epg-debug-buffer)
689 (goto-char (point-max))
690 (insert (format "%s %s\n"
691 (if (eq (epg-context-protocol context) 'CMS)
694 (mapconcat #'identity args " ")))))
695 (with-current-buffer buffer
696 (make-local-variable 'epg-read-point)
697 (setq epg-read-point (point-min))
698 (make-local-variable 'epg-pending-status-list)
699 (setq epg-pending-status-list nil)
700 (make-local-variable 'epg-key-id)
701 (setq epg-key-id nil)
702 (make-local-variable 'epg-context)
703 (setq epg-context context))
706 (set-default-file-modes 448)
708 (apply #'start-process "epg" buffer
709 (if (eq (epg-context-protocol context) 'CMS)
713 (set-default-file-modes orig-mode))
714 (set-process-filter process #'epg-process-filter)
715 (epg-context-set-process context process)))
717 (defun epg-process-filter (process input)
720 (unless epg-debug-buffer
721 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
722 (set-buffer epg-debug-buffer)
723 (goto-char (point-max))
725 (if (buffer-live-p (process-buffer process))
727 (set-buffer (process-buffer process))
728 (goto-char (point-max))
730 (goto-char epg-read-point)
732 (while (looking-at ".*\n") ;the input line finished
734 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
735 (let* ((status (match-string 1))
736 (string (match-string 2))
737 (symbol (intern-soft (concat "epg-status-" status))))
738 (if (member status epg-pending-status-list)
739 (setq epg-pending-status-list nil))
742 (funcall symbol process string)))))
744 (setq epg-read-point (point)))))
746 (defun epg-read-output (context)
747 "Read the output file CONTEXT and return the content as a string."
749 (if (fboundp 'set-buffer-multibyte)
750 (set-buffer-multibyte nil))
751 (if (file-exists-p (epg-context-output-file context))
752 (let ((coding-system-for-read 'binary))
753 (insert-file-contents (epg-context-output-file context))
756 (defun epg-wait-for-status (context status-list)
757 "Wait until one of elements in STATUS-LIST arrives."
758 (with-current-buffer (process-buffer (epg-context-process context))
759 (setq epg-pending-status-list status-list)
760 (while (and (eq (process-status (epg-context-process context)) 'run)
761 epg-pending-status-list)
762 (accept-process-output (epg-context-process context) 0 1))))
764 (defun epg-wait-for-completion (context)
765 "Wait until the `epg-gpg-program' process completes."
766 (while (eq (process-status (epg-context-process context)) 'run)
767 (accept-process-output (epg-context-process context) 0 1)))
769 (defun epg-reset (context)
771 (if (and (epg-context-process context)
772 (buffer-live-p (process-buffer (epg-context-process context))))
773 (kill-buffer (process-buffer (epg-context-process context))))
774 (epg-context-set-process context nil))
776 (defun epg-delete-output-file (context)
777 "Delete the output file of CONTEXT."
778 (if (and (epg-context-output-file context)
779 (file-exists-p (epg-context-output-file context)))
780 (delete-file (epg-context-output-file context))))
782 (defun epg-status-USERID_HINT (process string)
783 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
784 (let* ((key-id (match-string 1 string))
785 (user-id (match-string 2 string))
786 (entry (assoc key-id epg-user-id-alist)))
788 (setcdr entry user-id)
789 (setq epg-user-id-alist (cons (cons key-id user-id)
790 epg-user-id-alist))))))
792 (defun epg-status-NEED_PASSPHRASE (process string)
793 (if (string-match "\\`\\([^ ]+\\)" string)
794 (setq epg-key-id (match-string 1 string))))
796 (defun epg-status-NEED_PASSPHRASE_SYM (process string)
797 (setq epg-key-id 'SYM))
799 (defun epg-status-NEED_PASSPHRASE_PIN (process string)
800 (setq epg-key-id 'PIN))
802 (defun epg-status-GET_HIDDEN (process string)
804 (string-match "\\`passphrase\\." string))
807 passphrase-with-new-line)
813 (if (consp (epg-context-passphrase-callback
815 (car (epg-context-passphrase-callback
817 (epg-context-passphrase-callback epg-context))
820 (if (consp (epg-context-passphrase-callback
822 (cdr (epg-context-passphrase-callback
825 (setq passphrase-with-new-line (concat passphrase "\n"))
826 (fillarray passphrase 0)
827 (setq passphrase nil)
828 (process-send-string process passphrase-with-new-line)))
830 (epg-context-set-result-for
833 (epg-context-result-for epg-context 'error)))
834 (delete-process process)))
836 (fillarray passphrase 0))
837 (if passphrase-with-new-line
838 (fillarray passphrase-with-new-line 0))))))
840 (defun epg-status-GET_BOOL (process string)
841 (let ((entry (assoc string epg-prompt-alist))
844 (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
845 (process-send-string process "y\n")
846 (process-send-string process "n\n"))
848 (epg-context-set-result-for
851 (epg-context-result-for epg-context 'error)))
852 (delete-process process)))))
854 (defun epg-status-GET_LINE (process string)
855 (let ((entry (assoc string epg-prompt-alist))
860 (concat (read-string (if entry (cdr entry) (concat string ": ")))
863 (epg-context-set-result-for
866 (epg-context-result-for epg-context 'error)))
867 (delete-process process)))))
869 (defun epg-status-*SIG (status string)
870 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
871 (let* ((key-id (match-string 1 string))
872 (user-id (match-string 2 string))
873 (entry (assoc key-id epg-user-id-alist)))
874 (epg-context-set-result-for
877 (cons (epg-make-signature status key-id)
878 (epg-context-result-for epg-context 'verify)))
879 (if (eq (epg-context-protocol epg-context) 'CMS)
881 (setq user-id (epg-dn-from-string user-id))
884 (setcdr entry user-id)
885 (setq epg-user-id-alist
886 (cons (cons key-id user-id) epg-user-id-alist))))
887 (epg-context-set-result-for
890 (cons (epg-make-signature status)
891 (epg-context-result-for epg-context 'verify)))))
893 (defun epg-status-GOODSIG (process string)
894 (epg-status-*SIG 'good string))
896 (defun epg-status-EXPSIG (process string)
897 (epg-status-*SIG 'expired string))
899 (defun epg-status-EXPKEYSIG (process string)
900 (epg-status-*SIG 'expired-key string))
902 (defun epg-status-REVKEYSIG (process string)
903 (epg-status-*SIG 'revoked-key string))
905 (defun epg-status-BADSIG (process string)
906 (epg-status-*SIG 'bad string))
908 (defun epg-status-NO_PUBKEY (process string)
909 (epg-context-set-result-for
912 (cons (epg-make-signature 'no-pubkey string)
913 (epg-context-result-for epg-context 'verify))))
915 (defun epg-status-ERRSIG (process string)
916 (let ((signatures (car (epg-context-result-for epg-context 'verify))))
918 (setq signatures (list (epg-make-signature 'error)))
919 (epg-context-set-result-for epg-context 'verify signatures))
920 (when (and (not (eq (epg-signature-status (car signatures)) 'good))
921 (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
922 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
924 (epg-signature-set-key-id
926 (match-string 1 string))
927 (epg-signature-set-pubkey-algorithm
929 (string-to-number (match-string 2 string)))
930 (epg-signature-set-digest-algorithm
932 (string-to-number (match-string 3 string)))
933 ; (epg-signature-set-class
935 ; (string-to-number (match-string 4 string) 16))
936 (epg-signature-set-creation-time
938 (match-string 5 string)))))
940 (defun epg-status-VALIDSIG (process string)
941 (let ((signature (car (epg-context-result-for epg-context 'verify))))
943 (eq (epg-signature-status signature) 'good)
944 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
945 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
948 (epg-signature-set-fingerprint
950 (match-string 1 string))
951 (epg-signature-set-creation-time
953 (match-string 2 string))
954 (epg-signature-set-expiration-time
956 (match-string 3 string))
957 ; (epg-signature-set-version
959 ; (string-to-number (match-string 4 string)))
960 (epg-signature-set-pubkey-algorithm
962 (string-to-number (match-string 5 string)))
963 (epg-signature-set-digest-algorithm
965 (string-to-number (match-string 6 string)))
966 ; (epg-signature-set-class
968 ; (string-to-number (match-string 7 string) 16))
971 (defun epg-status-TRUST_UNDEFINED (process string)
972 (let ((signature (car (epg-context-result-for epg-context 'verify))))
974 (eq (epg-signature-status signature) 'good))
975 (epg-signature-set-validity signature 'undefined))))
977 (defun epg-status-TRUST_NEVER (process string)
978 (let ((signature (car (epg-context-result-for epg-context 'verify))))
980 (eq (epg-signature-status signature) 'good))
981 (epg-signature-set-validity signature 'never))))
983 (defun epg-status-TRUST_MARGINAL (process string)
984 (let ((signature (car (epg-context-result-for epg-context 'verify))))
986 (eq (epg-signature-status signature) 'marginal))
987 (epg-signature-set-validity signature 'marginal))))
989 (defun epg-status-TRUST_FULLY (process string)
990 (let ((signature (car (epg-context-result-for epg-context 'verify))))
992 (eq (epg-signature-status signature) 'good))
993 (epg-signature-set-validity signature 'full))))
995 (defun epg-status-TRUST_ULTIMATE (process string)
996 (let ((signature (car (epg-context-result-for epg-context 'verify))))
998 (eq (epg-signature-status signature) 'good))
999 (epg-signature-set-validity signature 'ultimate))))
1001 (defun epg-status-PROGRESS (process string)
1002 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1004 (funcall (if (consp (epg-context-progress-callback epg-context))
1005 (car (epg-context-progress-callback epg-context))
1006 (epg-context-progress-callback epg-context))
1008 (match-string 1 string)
1009 (match-string 2 string)
1010 (string-to-number (match-string 3 string))
1011 (string-to-number (match-string 4 string))
1012 (if (consp (epg-context-progress-callback epg-context))
1013 (cdr (epg-context-progress-callback epg-context))))))
1015 (defun epg-status-DECRYPTION_FAILED (process string)
1016 (epg-context-set-result-for
1018 (cons '(decryption-failed)
1019 (epg-context-result-for epg-context 'error))))
1021 (defun epg-status-NODATA (process string)
1022 (epg-context-set-result-for
1024 (cons (cons 'no-data (string-to-number string))
1025 (epg-context-result-for epg-context 'error))))
1027 (defun epg-status-UNEXPECTED (process string)
1028 (epg-context-set-result-for
1030 (cons (cons 'unexpected (string-to-number string))
1031 (epg-context-result-for epg-context 'error))))
1033 (defun epg-status-KEYEXPIRED (process string)
1034 (epg-context-set-result-for
1036 (cons (cons 'key-expired string)
1037 (epg-context-result-for epg-context 'error))))
1039 (defun epg-status-KEYREVOKED (process string)
1040 (epg-context-set-result-for
1042 (cons '(key-revoked)
1043 (epg-context-result-for epg-context 'error))))
1045 (defun epg-status-BADARMOR (process string)
1046 (epg-context-set-result-for
1049 (epg-context-result-for epg-context 'error))))
1051 (defun epg-status-INV_RECP (process string)
1052 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1053 (epg-context-set-result-for
1055 (cons (list 'invalid-recipient
1056 (string-to-number (match-string 1 string))
1057 (match-string 2 string))
1058 (epg-context-result-for epg-context 'error)))))
1060 (defun epg-status-NO_RECP (process string)
1061 (epg-context-set-result-for
1063 (cons '(no-recipients)
1064 (epg-context-result-for epg-context 'error))))
1066 (defun epg-status-DELETE_PROBLEM (process string)
1067 (if (string-match "\\`\\([0-9]+\\)" string)
1068 (epg-context-set-result-for
1070 (cons (cons 'delete-problem (string-to-number (match-string 1 string)))
1071 (epg-context-result-for epg-context 'error)))))
1073 (defun epg-status-SIG_CREATED (process string)
1074 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1075 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1076 (epg-context-set-result-for
1078 (cons (epg-make-new-signature
1079 (cdr (assq (aref (match-string 1 string) 0)
1080 epg-new-signature-type-alist))
1081 (string-to-number (match-string 2 string))
1082 (string-to-number (match-string 3 string))
1083 (string-to-number (match-string 4 string) 16)
1084 (match-string 5 string)
1085 (substring string (match-end 0)))
1086 (epg-context-result-for epg-context 'sign)))))
1088 (defun epg-status-KEY_CREATED (process string)
1089 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1090 (epg-context-set-result-for
1091 epg-context 'generate-key
1092 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1093 (cons 'fingerprint (match-string 2 string)))
1094 (epg-context-result-for epg-context 'generate-key)))))
1096 (defun epg-status-KEY_NOT_CREATED (process string)
1097 (epg-context-set-result-for
1099 (cons '(key-not-created)
1100 (epg-context-result-for epg-context 'error))))
1102 (defun epg-passphrase-callback-function (context key-id handback)
1104 (if (eq key-id 'SYM)
1105 "Passphrase for symmetric encryption: "
1106 (if (eq key-id 'PIN)
1107 "Passphrase for PIN: "
1108 (let ((entry (assoc key-id epg-user-id-alist)))
1110 (format "Passphrase for %s %s: " key-id (cdr entry))
1111 (format "Passphrase for %s: " key-id)))))))
1113 (defun epg-progress-callback-function (context what char current total
1115 (message "%s: %d%%/%d%%" what current total))
1117 (defun epg-configuration ()
1118 "Return a list of internal configuration parameters of `epg-gpg-program'."
1121 (apply #'call-process epg-gpg-program nil (list t nil) nil
1122 '("--with-colons" "--list-config"))
1123 (goto-char (point-min))
1124 (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
1125 (setq type (intern (match-string 1))
1126 config (cons (cons type
1128 '(pubkey cipher digest compress))
1129 (mapcar #'string-to-number
1130 (delete "" (split-string
1137 (defun epg-list-keys-1 (context name mode)
1138 (let ((args (append (list "--with-colons" "--no-greeting" "--batch"
1139 "--with-fingerprint"
1140 "--with-fingerprint"
1141 (if (or (eq mode t) (eq mode 'secret))
1142 "--list-secret-keys"
1146 (unless (eq (epg-context-protocol context) 'CMS)
1147 '("--fixed-list-mode"))
1148 (if name (list name))))
1149 keys string field index)
1151 (apply #'call-process
1152 (if (eq (epg-context-protocol context) 'CMS)
1155 nil (list t nil) nil args)
1156 (goto-char (point-min))
1157 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1158 (setq keys (cons (make-vector 15 nil) keys)
1159 string (match-string 0)
1163 (string-match "\\([^:]+\\)?:" string index))
1164 (setq index (match-end 0))
1165 (aset (car keys) field (match-string 1 string))
1166 (setq field (1+ field))))
1169 (defun epg-make-sub-key-1 (line)
1172 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1174 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1176 (member (aref line 0) '("sec" "ssb"))
1177 (string-to-number (aref line 3))
1178 (string-to-number (aref line 2))
1183 (defun epg-list-keys-postprocess-one-key (key)
1184 (let (key-id user-id-string entry)
1185 (epg-key-set-sub-key-list
1187 (nreverse (epg-key-sub-key-list key)))
1188 (epg-key-set-user-id-list
1190 (nreverse (epg-key-user-id-list key)))
1192 (epg-sub-key-id (car (epg-key-sub-key-list key)))
1194 (epg-user-id-string (car (epg-key-user-id-list key)))
1195 entry (assoc key-id epg-user-id-alist))
1197 (setcdr entry user-id-string)
1198 (setq epg-user-id-alist (cons (cons key-id user-id-string)
1199 epg-user-id-alist)))))
1201 (defun epg-list-keys (context &optional name mode)
1202 "Return a list of epg-key objects matched with NAME.
1203 If MODE is nil, only public keyring should be searched.
1204 If MODE is t or 'secret, only secret keyring should be searched.
1205 Otherwise, only public keyring should be searched and the key
1206 signatures should be included."
1207 (let ((lines (epg-list-keys-1 context name mode))
1211 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1213 (epg-list-keys-postprocess-one-key (car keys)))
1214 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1215 keys (cons (epg-make-key
1216 (if (aref (car lines) 8)
1217 (cdr (assq (string-to-char (aref (car lines) 8))
1218 epg-key-validity-alist))))
1220 (epg-key-set-sub-key-list
1222 (cons (epg-make-sub-key-1 (car lines))
1223 (epg-key-sub-key-list (car keys)))))
1224 ((member (aref (car lines) 0) '("sub" "ssb"))
1225 (epg-key-set-sub-key-list
1227 (cons (epg-make-sub-key-1 (car lines))
1228 (epg-key-sub-key-list (car keys)))))
1229 ((equal (aref (car lines) 0) "uid")
1230 (epg-key-set-user-id-list
1232 (cons (epg-make-user-id
1233 (if (aref (car lines) 1)
1234 (cdr (assq (string-to-char (aref (car lines) 1))
1235 epg-key-validity-alist)))
1238 (epg-dn-from-string (aref (car lines) 9))
1239 (error (aref (car lines) 9)))
1240 (aref (car lines) 9)))
1241 (epg-key-user-id-list (car keys)))))
1242 ((equal (aref (car lines) 0) "fpr")
1243 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1244 (aref (car lines) 9))))
1245 (setq lines (cdr lines)))
1247 (epg-list-keys-postprocess-one-key (car keys)))
1250 (if (fboundp 'make-temp-file)
1251 (defalias 'epg-make-temp-file 'make-temp-file)
1252 (defvar temporary-file-directory)
1253 ;; stolen from poe.el.
1254 (defun epg-make-temp-file (prefix)
1255 "Create a temporary file.
1256 The returned file name (created by appending some random characters at the end
1257 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1258 is guaranteed to point to a newly created empty file.
1259 You can then use `write-region' to write new data into the file."
1260 (let (tempdir tempfile)
1261 (setq prefix (expand-file-name prefix
1262 (if (featurep 'xemacs)
1264 temporary-file-directory)))
1267 ;; First, create a temporary directory.
1268 (while (condition-case ()
1270 (setq tempdir (make-temp-name
1272 (file-name-directory prefix)
1274 ;; return nil or signal an error.
1275 (make-directory tempdir))
1277 (file-already-exists t)))
1278 (set-file-modes tempdir 448)
1279 ;; Second, create a temporary file in the tempdir.
1280 ;; There *is* a race condition between `make-temp-name'
1281 ;; and `write-region', but we don't care it since we are
1282 ;; in a private directory now.
1283 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1284 (write-region "" nil tempfile nil 'silent)
1285 (set-file-modes tempfile 384)
1286 ;; Finally, make a hard-link from the tempfile.
1287 (while (condition-case ()
1289 (setq file (make-temp-name prefix))
1290 ;; return nil or signal an error.
1291 (add-name-to-file tempfile file))
1293 (file-already-exists t)))
1295 ;; Cleanup the tempfile.
1297 (file-exists-p tempfile)
1298 (delete-file tempfile))
1299 ;; Cleanup the tempdir.
1301 (file-directory-p tempdir)
1302 (delete-directory tempdir))))))
1305 (defun epg-cancel (context)
1306 (if (buffer-live-p (process-buffer (epg-context-process context)))
1308 (set-buffer (process-buffer (epg-context-process context)))
1309 (epg-context-set-result-for
1312 (epg-context-result-for epg-context 'error)))))
1313 (if (eq (process-status (epg-context-process context)) 'run)
1314 (delete-process (epg-context-process context))))
1317 (defun epg-start-decrypt (context cipher)
1318 "Initiate a decrypt operation on CIPHER.
1319 CIPHER is a data object.
1321 If you use this function, you will need to wait for the completion of
1322 `epg-gpg-program' by using `epg-wait-for-completion' and call
1323 `epg-reset' to clear a temporaly output file.
1324 If you are unsure, use synchronous version of this function
1325 `epg-decrypt-file' or `epg-decrypt-string' instead."
1326 (unless (epg-data-file cipher)
1327 (error "Not a file"))
1328 (epg-context-set-result context nil)
1329 (epg-start context (list "--decrypt" (epg-data-file cipher)))
1330 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1331 (unless (eq (epg-context-protocol context) 'CMS)
1332 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1335 (defun epg-decrypt-file (context cipher plain)
1336 "Decrypt a file CIPHER and store the result to a file PLAIN.
1337 If PLAIN is nil, it returns the result as a string."
1341 (epg-context-set-output-file context plain)
1342 (epg-context-set-output-file context
1343 (epg-make-temp-file "epg-output")))
1344 (epg-start-decrypt context (epg-make-data-from-file cipher))
1345 (epg-wait-for-completion context)
1346 (if (epg-context-result-for context 'error)
1347 (error "Decrypt failed: %S"
1348 (epg-context-result-for context 'error)))
1350 (epg-read-output context)))
1352 (epg-delete-output-file context))
1353 (epg-reset context)))
1356 (defun epg-decrypt-string (context cipher)
1357 "Decrypt a string CIPHER and return the plain text."
1358 (let ((input-file (epg-make-temp-file "epg-input"))
1359 (coding-system-for-write 'binary))
1362 (write-region cipher nil input-file nil 'quiet)
1363 (epg-context-set-output-file context
1364 (epg-make-temp-file "epg-output"))
1365 (epg-start-decrypt context (epg-make-data-from-file input-file))
1366 (epg-wait-for-completion context)
1367 (if (epg-context-result-for context 'error)
1368 (error "Decrypt failed: %S"
1369 (epg-context-result-for context 'error)))
1370 (epg-read-output context))
1371 (epg-delete-output-file context)
1372 (if (file-exists-p input-file)
1373 (delete-file input-file))
1374 (epg-reset context))))
1377 (defun epg-start-verify (context signature &optional signed-text)
1378 "Initiate a verify operation on SIGNATURE.
1379 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1381 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1382 For a normal or a clear text signature, SIGNED-TEXT should be nil.
1384 If you use this function, you will need to wait for the completion of
1385 `epg-gpg-program' by using `epg-wait-for-completion' and call
1386 `epg-reset' to clear a temporaly output file.
1387 If you are unsure, use synchronous version of this function
1388 `epg-verify-file' or `epg-verify-string' instead."
1389 (epg-context-set-result context nil)
1391 ;; Detached signature.
1392 (if (epg-data-file signed-text)
1393 (epg-start context (list "--verify" (epg-data-file signature)
1394 (epg-data-file signed-text)))
1395 (epg-start context (list "--verify" (epg-data-file signature) "-"))
1396 (if (eq (process-status (epg-context-process context)) 'run)
1397 (process-send-string (epg-context-process context)
1398 (epg-data-string signed-text)))
1399 (if (eq (process-status (epg-context-process context)) 'run)
1400 (process-send-eof (epg-context-process context))))
1401 ;; Normal (or cleartext) signature.
1402 (if (epg-data-file signature)
1403 (epg-start context (list "--verify" (epg-data-file signature)))
1404 (epg-start context (list "--verify"))
1405 (if (eq (process-status (epg-context-process context)) 'run)
1406 (process-send-string (epg-context-process context)
1407 (epg-data-string signature)))
1408 (if (eq (process-status (epg-context-process context)) 'run)
1409 (process-send-eof (epg-context-process context))))))
1412 (defun epg-verify-file (context signature &optional signed-text plain)
1413 "Verify a file SIGNATURE.
1414 SIGNED-TEXT and PLAIN are also a file if they are specified.
1416 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1417 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1421 (epg-context-set-output-file context plain)
1422 (epg-context-set-output-file context
1423 (epg-make-temp-file "epg-output")))
1425 (epg-start-verify context
1426 (epg-make-data-from-file signature)
1427 (epg-make-data-from-file signed-text))
1428 (epg-start-verify context
1429 (epg-make-data-from-file signature)))
1430 (epg-wait-for-completion context)
1431 ; (if (epg-context-result-for context 'error)
1432 ; (error "Verify failed: %S"
1433 ; (epg-context-result-for context 'error)))
1435 (epg-read-output context)))
1437 (epg-delete-output-file context))
1438 (epg-reset context)))
1441 (defun epg-verify-string (context signature &optional signed-text)
1442 "Verify a string SIGNATURE.
1443 SIGNED-TEXT is a string if it is specified.
1445 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1446 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1447 (let ((coding-system-for-write 'binary)
1451 (epg-context-set-output-file context
1452 (epg-make-temp-file "epg-output"))
1455 (setq input-file (epg-make-temp-file "epg-signature"))
1456 (write-region signature nil input-file nil 'quiet)
1457 (epg-start-verify context
1458 (epg-make-data-from-file input-file)
1459 (epg-make-data-from-string signed-text)))
1460 (epg-start-verify context (epg-make-data-from-string signature)))
1461 (epg-wait-for-completion context)
1462 ; (if (epg-context-result-for context 'error)
1463 ; (error "Verify failed: %S"
1464 ; (epg-context-result-for context 'error)))
1465 (epg-read-output context))
1466 (epg-delete-output-file context)
1468 (file-exists-p input-file))
1469 (delete-file input-file))
1470 (epg-reset context))))
1473 (defun epg-start-sign (context plain &optional mode)
1474 "Initiate a sign operation on PLAIN.
1475 PLAIN is a data object.
1477 If optional 3rd argument MODE is 'clear, it makes a clear text signature.
1478 If MODE is t or 'detached, it makes a detached signature.
1479 Otherwise, it makes a normal signature.
1481 If you use this function, you will need to wait for the completion of
1482 `epg-gpg-program' by using `epg-wait-for-completion' and call
1483 `epg-reset' to clear a temporaly output file.
1484 If you are unsure, use synchronous version of this function
1485 `epg-sign-file' or `epg-sign-string' instead."
1486 (epg-context-set-result context nil)
1488 (append (list (if (eq mode 'clear)
1490 (if (or (eq mode t) (eq mode 'detached))
1498 (car (epg-key-sub-key-list signer)))))
1499 (epg-context-signers context)))
1500 (if (epg-data-file plain)
1501 (list (epg-data-file plain)))))
1502 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1503 (unless (eq (epg-context-protocol context) 'CMS)
1504 (epg-wait-for-status context '("BEGIN_SIGNING")))
1505 (when (epg-data-string plain)
1506 (if (eq (process-status (epg-context-process context)) 'run)
1507 (process-send-string (epg-context-process context)
1508 (epg-data-string plain)))
1509 (if (eq (process-status (epg-context-process context)) 'run)
1510 (process-send-eof (epg-context-process context)))))
1513 (defun epg-sign-file (context plain signature &optional mode)
1514 "Sign a file PLAIN and store the result to a file SIGNATURE.
1515 If SIGNATURE is nil, it returns the result as a string.
1516 If optional 3rd argument MODE is 'clear, it makes a clear text signature.
1517 If MODE is t or 'detached, it makes a detached signature.
1518 Otherwise, it makes a normal signature."
1522 (epg-context-set-output-file context signature)
1523 (epg-context-set-output-file context
1524 (epg-make-temp-file "epg-output")))
1525 (epg-start-sign context (epg-make-data-from-file plain) mode)
1526 (epg-wait-for-completion context)
1527 (unless (epg-context-result-for context 'sign)
1528 (if (epg-context-result-for context 'error)
1529 (error "Sign failed: %S"
1530 (epg-context-result-for context 'error))
1531 (error "Sign failed")))
1533 (epg-read-output context)))
1535 (epg-delete-output-file context))
1536 (epg-reset context)))
1539 (defun epg-sign-string (context plain &optional mode)
1540 "Sign a string PLAIN and return the output as string.
1541 If optional 3rd argument MODE is 'clear, it makes a clear text signature.
1542 If MODE is t or 'detached, it makes a detached signature.
1543 Otherwise, it makes a normal signature."
1546 (epg-context-set-output-file context
1547 (epg-make-temp-file "epg-output"))
1548 (epg-start-sign context (epg-make-data-from-string plain) mode)
1549 (epg-wait-for-completion context)
1550 (unless (epg-context-result-for context 'sign)
1551 (if (epg-context-result-for context 'error)
1552 (error "Sign failed: %S"
1553 (epg-context-result-for context 'error))
1554 (error "Sign failed")))
1555 (epg-read-output context))
1556 (epg-delete-output-file context)
1557 (epg-reset context)))
1560 (defun epg-start-encrypt (context plain recipients
1561 &optional sign always-trust)
1562 "Initiate an encrypt operation on PLAIN.
1563 PLAIN is a data object.
1564 If RECIPIENTS is nil, it performs symmetric encryption.
1566 If you use this function, you will need to wait for the completion of
1567 `epg-gpg-program' by using `epg-wait-for-completion' and call
1568 `epg-reset' to clear a temporaly output file.
1569 If you are unsure, use synchronous version of this function
1570 `epg-encrypt-file' or `epg-encrypt-string' instead."
1571 (epg-context-set-result context nil)
1573 (append (if always-trust '("--always-trust"))
1574 (if recipients '("--encrypt") '("--symmetric"))
1578 (mapcar (lambda (signer)
1580 (epg-context-signers context)))))
1586 (car (epg-key-sub-key-list recipient)))))
1588 (if (epg-data-file plain)
1589 (list (epg-data-file plain)))))
1590 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1591 (unless (eq (epg-context-protocol context) 'CMS)
1593 (epg-wait-for-status context '("BEGIN_SIGNING"))
1594 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
1595 (when (epg-data-string plain)
1596 (if (eq (process-status (epg-context-process context)) 'run)
1597 (process-send-string (epg-context-process context)
1598 (epg-data-string plain)))
1599 (if (eq (process-status (epg-context-process context)) 'run)
1600 (process-send-eof (epg-context-process context)))))
1603 (defun epg-encrypt-file (context plain recipients
1604 cipher &optional sign always-trust)
1605 "Encrypt a file PLAIN and store the result to a file CIPHER.
1606 If CIPHER is nil, it returns the result as a string.
1607 If RECIPIENTS is nil, it performs symmetric encryption."
1611 (epg-context-set-output-file context cipher)
1612 (epg-context-set-output-file context
1613 (epg-make-temp-file "epg-output")))
1614 (epg-start-encrypt context (epg-make-data-from-file plain)
1615 recipients sign always-trust)
1616 (epg-wait-for-completion context)
1618 (not (epg-context-result-for context 'sign)))
1619 (if (epg-context-result-for context 'error)
1620 (error "Sign failed: %S"
1621 (epg-context-result-for context 'error))
1622 (error "Sign failed")))
1623 (if (epg-context-result-for context 'error)
1624 (error "Encrypt failed: %S"
1625 (epg-context-result-for context 'error)))
1627 (epg-read-output context)))
1629 (epg-delete-output-file context))
1630 (epg-reset context)))
1633 (defun epg-encrypt-string (context plain recipients
1634 &optional sign always-trust)
1635 "Encrypt a string PLAIN.
1636 If RECIPIENTS is nil, it performs symmetric encryption."
1639 (epg-context-set-output-file context
1640 (epg-make-temp-file "epg-output"))
1641 (epg-start-encrypt context (epg-make-data-from-string plain)
1642 recipients sign always-trust)
1643 (epg-wait-for-completion context)
1645 (not (epg-context-result-for context 'sign)))
1646 (if (epg-context-result-for context 'error)
1647 (error "Sign failed: %S"
1648 (epg-context-result-for context 'error))
1649 (error "Sign failed")))
1650 (if (epg-context-result-for context 'error)
1651 (error "Encrypt failed: %S"
1652 (epg-context-result-for context 'error)))
1653 (epg-read-output context))
1654 (epg-delete-output-file context)
1655 (epg-reset context)))
1658 (defun epg-start-export-keys (context keys)
1659 "Initiate an export keys operation.
1661 If you use this function, you will need to wait for the completion of
1662 `epg-gpg-program' by using `epg-wait-for-completion' and call
1663 `epg-reset' to clear a temporaly output file.
1664 If you are unsure, use synchronous version of this function
1665 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1666 (epg-context-set-result context nil)
1667 (epg-start context (cons "--export"
1671 (car (epg-key-sub-key-list key))))
1675 (defun epg-export-keys-to-file (context keys file)
1676 "Extract public KEYS."
1680 (epg-context-set-output-file context file)
1681 (epg-context-set-output-file context
1682 (epg-make-temp-file "epg-output")))
1683 (epg-start-export-keys context keys)
1684 (epg-wait-for-completion context)
1685 (if (epg-context-result-for context 'error)
1686 (error "Export keys failed: %S"
1687 (epg-context-result-for context 'error)))
1689 (epg-read-output context)))
1691 (epg-delete-output-file context))
1692 (epg-reset context)))
1695 (defun epg-export-keys-to-string (context keys)
1696 "Extract public KEYS and return them as a string."
1697 (epg-export-keys-to-file context keys nil))
1700 (defun epg-start-import-keys (context keys)
1701 "Initiate an import keys operation.
1702 KEYS is a data object.
1704 If you use this function, you will need to wait for the completion of
1705 `epg-gpg-program' by using `epg-wait-for-completion' and call
1706 `epg-reset' to clear a temporaly output file.
1707 If you are unsure, use synchronous version of this function
1708 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1709 (epg-context-set-result context nil)
1710 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
1711 (epg-start context (list "--import" (epg-data-file keys)))
1712 (when (epg-data-string keys)
1713 (if (eq (process-status (epg-context-process context)) 'run)
1714 (process-send-string (epg-context-process context)
1715 (epg-data-string keys)))
1716 (if (eq (process-status (epg-context-process context)) 'run)
1717 (process-send-eof (epg-context-process context)))))
1719 (defun epg-import-keys-1 (context keys)
1722 (epg-start-import-keys context keys)
1723 (epg-wait-for-completion context)
1724 (if (epg-context-result-for context 'error)
1725 (error "Import keys failed: %S"
1726 (epg-context-result-for context 'error)))
1727 (epg-read-output context))
1728 (epg-reset context)))
1731 (defun epg-import-keys-from-file (context keys)
1732 "Add keys from a file KEYS."
1733 (epg-import-keys-1 context (epg-make-data-from-file keys)))
1736 (defun epg-import-keys-from-string (context keys)
1737 "Add keys from a string KEYS."
1738 (epg-import-keys-1 context (epg-make-data-from-string keys)))
1741 (defun epg-start-delete-keys (context keys &optional allow-secret)
1742 "Initiate an delete keys operation.
1744 If you use this function, you will need to wait for the completion of
1745 `epg-gpg-program' by using `epg-wait-for-completion' and call
1746 `epg-reset' to clear a temporaly output file.
1747 If you are unsure, use synchronous version of this function
1748 `epg-delete-keys' instead."
1749 (epg-context-set-result context nil)
1750 (epg-start context (cons (if allow-secret
1751 "--delete-secret-key"
1756 (car (epg-key-sub-key-list key))))
1760 (defun epg-delete-keys (context keys &optional allow-secret)
1761 "Delete KEYS from the key ring."
1764 (epg-start-delete-keys context keys allow-secret)
1765 (epg-wait-for-completion context)
1766 (if (epg-context-result-for context 'error)
1767 (error "Delete keys failed: %S"
1768 (epg-context-result-for context 'error))))
1769 (epg-reset context)))
1772 (defun epg-start-sign-keys (context keys &optional local)
1773 "Initiate an sign keys operation.
1775 If you use this function, you will need to wait for the completion of
1776 `epg-gpg-program' by using `epg-wait-for-completion' and call
1777 `epg-reset' to clear a temporaly output file.
1778 If you are unsure, use synchronous version of this function
1779 `epg-sign-keys' instead."
1780 (epg-context-set-result context nil)
1781 (epg-start context (cons (if local
1787 (car (epg-key-sub-key-list key))))
1791 (defun epg-sign-keys (context keys &optional local)
1792 "Sign KEYS from the key ring."
1795 (epg-start-sign-keys context keys local)
1796 (epg-wait-for-completion context)
1797 (if (epg-context-result-for context 'error)
1798 (error "Sign keys failed: %S"
1799 (epg-context-result-for context 'error))))
1800 (epg-reset context)))
1803 (defun epg-start-generate-key (context parameters)
1804 "Initiate a key generation.
1805 PARAMETERS specifies parameters for the key.
1807 If you use this function, you will need to wait for the completion of
1808 `epg-gpg-program' by using `epg-wait-for-completion' and call
1809 `epg-reset' to clear a temporaly output file.
1810 If you are unsure, use synchronous version of this function
1811 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
1812 (epg-context-set-result context nil)
1813 (if (epg-data-file parameters)
1814 (epg-start context (list "--batch" "--genkey"
1815 (epg-data-file parameters)))
1816 (epg-start context '("--batch" "--genkey"))
1817 (if (eq (process-status (epg-context-process context)) 'run)
1818 (process-send-string (epg-context-process context)
1819 (epg-data-string parameters)))
1820 (if (eq (process-status (epg-context-process context)) 'run)
1821 (process-send-eof (epg-context-process context)))))
1824 (defun epg-generate-key-from-file (context parameters)
1825 "Generate a new key pair.
1826 PARAMETERS is a file which tells how to create the key."
1829 (epg-start-generate-key context (epg-make-data-from-file parameters))
1830 (epg-wait-for-completion context)
1831 (if (epg-context-result-for context 'error)
1832 (error "Generate key failed: %S"
1833 (epg-context-result-for context 'error))))
1834 (epg-reset context)))
1837 (defun epg-generate-key-from-string (context parameters)
1838 "Generate a new key pair.
1839 PARAMETERS is a string which tells how to create the key."
1842 (epg-start-generate-key context (epg-make-data-from-string parameters))
1843 (epg-wait-for-completion context)
1844 (if (epg-context-result-for context 'error)
1845 (error "Generate key failed: %S"
1846 (epg-context-result-for context 'error))))
1847 (epg-reset context)))
1849 (defun epg-decode-hexstring (string)
1851 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
1852 (setq string (replace-match "\\x\\&" t nil string)
1854 (car (read-from-string (concat "\"" string "\"")))))
1856 (defun epg-decode-quotedstring (string)
1858 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
1859 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
1861 (if (match-beginning 2)
1862 (setq string (replace-match "\\2" t nil string)
1864 (if (match-beginning 3)
1865 (setq string (replace-match "\\x\\3" t nil string)
1867 (setq string (replace-match "\\\\\\\\\\4" t nil string)
1868 index (+ index 3)))))
1869 (car (read-from-string (concat "\"" string "\"")))))
1871 (defun epg-dn-from-string (string)
1872 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
1873 The return value is an alist mapping from types to values."
1875 (length (length string))
1876 alist type value group)
1877 (while (< index length)
1878 (if (eq index (string-match "[ \t\n\r]*" string index))
1879 (setq index (match-end 0)))
1880 (if (eq index (string-match
1881 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
1883 (setq type (match-string 1 string)
1884 index (match-end 0))
1885 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
1887 (setq type (match-string 1 string)
1888 index (match-end 0))))
1890 (error "Invalid type"))
1891 (if (eq index (string-match
1892 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
1894 (setq index (match-end 0)
1895 value (epg-decode-quotedstring (match-string 0 string)))
1896 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
1897 (setq index (match-end 0)
1898 value (epg-decode-hexstring (match-string 1 string)))
1899 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
1901 (setq index (match-end 0)
1902 value (epg-decode-quotedstring (match-string 0 string))))))
1904 (if (stringp (car (car alist)))
1905 (setcar alist (list (cons type value) (car alist)))
1906 (setcar alist (cons (cons type value) (car alist))))
1907 (if (consp (car (car alist)))
1908 (setcar alist (nreverse (car alist))))
1909 (setq alist (cons (cons type value) alist)
1912 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
1913 (setq index (match-end 0)
1914 group (eq (aref string (match-beginning 1)) ?+))))
1917 (defun epg-decode-dn (alist)
1918 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
1919 Type names are resolved using `epg-dn-type-alist'."
1922 (if (stringp (car rdn))
1923 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
1925 (format "%s=%s" (cdr entry) (cdr rdn))
1926 (format "%s=%s" (car rdn) (cdr rdn))))
1927 (concat "(" (epg-decode-dn rdn) ")")))
1933 ;;; epg.el ends here