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 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-class (signature)
397 "Return the class of SIGNATURE."
398 (unless (eq (car signature) 'epg-signature)
399 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
400 (aref (cdr signature) 8))
402 (defun epg-signature-version (signature)
403 "Return the version of SIGNATURE."
404 (unless (eq (car signature) 'epg-signature)
405 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
406 (aref (cdr signature) 9))
408 (defun epg-signature-set-status (signature status)
409 "Set the status code of SIGNATURE."
410 (unless (eq (car signature) 'epg-signature)
411 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
412 (aset (cdr signature) 0 status))
414 (defun epg-signature-set-key-id (signature key-id)
415 "Set the key-id of SIGNATURE."
416 (unless (eq (car signature) 'epg-signature)
417 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
418 (aset (cdr signature) 1 key-id))
420 (defun epg-signature-set-validity (signature validity)
421 "Set the validity of SIGNATURE."
422 (unless (eq (car signature) 'epg-signature)
423 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
424 (aset (cdr signature) 2 validity))
426 (defun epg-signature-set-fingerprint (signature fingerprint)
427 "Set the fingerprint of SIGNATURE."
428 (unless (eq (car signature) 'epg-signature)
429 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
430 (aset (cdr signature) 3 fingerprint))
432 (defun epg-signature-set-creation-time (signature creation-time)
433 "Set the creation time of SIGNATURE."
434 (unless (eq (car signature) 'epg-signature)
435 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
436 (aset (cdr signature) 4 creation-time))
438 (defun epg-signature-set-expiration-time (signature expiration-time)
439 "Set the expiration time of SIGNATURE."
440 (unless (eq (car signature) 'epg-signature)
441 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
442 (aset (cdr signature) 5 expiration-time))
444 (defun epg-signature-set-pubkey-algorithm (signature pubkey-algorithm)
445 "Set the public key algorithm of SIGNATURE."
446 (unless (eq (car signature) 'epg-signature)
447 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
448 (aset (cdr signature) 6 pubkey-algorithm))
450 (defun epg-signature-set-digest-algorithm (signature digest-algorithm)
451 "Set the digest algorithm of SIGNATURE."
452 (unless (eq (car signature) 'epg-signature)
453 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
454 (aset (cdr signature) 7 digest-algorithm))
456 (defun epg-signature-set-class (signature class)
457 "Set the class of SIGNATURE."
458 (unless (eq (car signature) 'epg-signature)
459 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
460 (aset (cdr signature) 8 class))
462 (defun epg-signature-set-version (signature version)
463 "Set the version of SIGNATURE."
464 (unless (eq (car signature) 'epg-signature)
465 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
466 (aset (cdr signature) 9 version))
468 (defun epg-make-new-signature (type pubkey-algorithm digest-algorithm
469 class creation-time fingerprint)
470 "Return a new signature object."
471 (cons 'epg-new-signature (vector type pubkey-algorithm digest-algorithm
472 class creation-time fingerprint)))
474 (defun epg-new-signature-type (new-signature)
475 "Return the type 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) 0))
480 (defun epg-new-signature-pubkey-algorithm (new-signature)
481 "Return the public key algorithm 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) 1))
486 (defun epg-new-signature-digest-algorithm (new-signature)
487 "Return the digest algorithm of NEW-SIGNATURE."
488 (unless (eq (car new-signature) 'epg-new-signature)
489 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
490 (aref (cdr new-signature) 2))
492 (defun epg-new-signature-class (new-signature)
493 "Return the class of NEW-SIGNATURE."
494 (unless (eq (car new-signature) 'epg-new-signature)
495 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
496 (aref (cdr new-signature) 3))
498 (defun epg-new-signature-creation-time (new-signature)
499 "Return the creation time of NEW-SIGNATURE."
500 (unless (eq (car new-signature) 'epg-new-signature)
501 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
502 (aref (cdr new-signature) 4))
504 (defun epg-new-signature-fingerprint (new-signature)
505 "Return the fingerprint of NEW-SIGNATURE."
506 (unless (eq (car new-signature) 'epg-new-signature)
507 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
508 (aref (cdr new-signature) 5))
510 (defun epg-make-key (owner-trust)
511 "Return a key object."
512 (cons 'epg-key (vector owner-trust nil nil)))
514 (defun epg-key-owner-trust (key)
515 "Return the owner trust of KEY."
516 (unless (eq (car key) 'epg-key)
517 (signal 'wrong-type-argument (list 'epg-key-p key)))
520 (defun epg-key-sub-key-list (key)
521 "Return the sub key list of KEY."
522 (unless (eq (car key) 'epg-key)
523 (signal 'wrong-type-argument (list 'epg-key-p key)))
526 (defun epg-key-user-id-list (key)
527 "Return the user ID list of KEY."
528 (unless (eq (car key) 'epg-key)
529 (signal 'wrong-type-argument (list 'epg-key-p key)))
532 (defun epg-key-set-sub-key-list (key sub-key-list)
533 "Set the sub key list of KEY."
534 (unless (eq (car key) 'epg-key)
535 (signal 'wrong-type-argument (list 'epg-key-p key)))
536 (aset (cdr key) 1 sub-key-list))
538 (defun epg-key-set-user-id-list (key user-id-list)
539 "Set the user ID list of KEY."
540 (unless (eq (car key) 'epg-key)
541 (signal 'wrong-type-argument (list 'epg-key-p key)))
542 (aset (cdr key) 2 user-id-list))
544 (defun epg-make-sub-key (validity capability secret algorithm length id
545 creation-time expiration-time)
546 "Return a sub key object."
548 (vector validity capability secret algorithm length id creation-time
549 expiration-time nil)))
551 (defun epg-sub-key-validity (sub-key)
552 "Return the validity 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) 0))
557 (defun epg-sub-key-capability (sub-key)
558 "Return the capability 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) 1))
563 (defun epg-sub-key-secret (sub-key)
564 "Return non-nil if SUB-KEY is a secret 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) 2))
569 (defun epg-sub-key-algorithm (sub-key)
570 "Return the algorithm 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) 3))
575 (defun epg-sub-key-length (sub-key)
576 "Return the length 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) 4))
581 (defun epg-sub-key-id (sub-key)
582 "Return the ID of SUB-KEY."
583 (unless (eq (car sub-key) 'epg-sub-key)
584 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
585 (aref (cdr sub-key) 5))
587 (defun epg-sub-key-creation-time (sub-key)
588 "Return the creation time of SUB-KEY."
589 (unless (eq (car sub-key) 'epg-sub-key)
590 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
591 (aref (cdr sub-key) 6))
593 (defun epg-sub-key-expiration-time (sub-key)
594 "Return the expiration time of SUB-KEY."
595 (unless (eq (car sub-key) 'epg-sub-key)
596 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
597 (aref (cdr sub-key) 7))
599 (defun epg-sub-key-fingerprint (sub-key)
600 "Return the fingerprint of SUB-KEY."
601 (unless (eq (car sub-key) 'epg-sub-key)
602 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
603 (aref (cdr sub-key) 8))
605 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
606 "Set the fingerprint of SUB-KEY.
607 This function is for internal use only."
608 (unless (eq (car sub-key) 'epg-sub-key)
609 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
610 (aset (cdr sub-key) 8 fingerprint))
612 (defun epg-make-user-id (validity string)
613 "Return a user ID object."
614 (cons 'epg-user-id (vector validity string nil)))
616 (defun epg-user-id-validity (user-id)
617 "Return the validity of USER-ID."
618 (unless (eq (car user-id) 'epg-user-id)
619 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
620 (aref (cdr user-id) 0))
622 (defun epg-user-id-string (user-id)
623 "Return the name of USER-ID."
624 (unless (eq (car user-id) 'epg-user-id)
625 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
626 (aref (cdr user-id) 1))
628 (defun epg-user-id-signature-list (user-id)
629 "Return the signature list of USER-ID."
630 (unless (eq (car user-id) 'epg-user-id)
631 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
632 (aref (cdr user-id) 2))
634 (defun epg-user-id-set-signature-list (user-id signature-list)
635 "Set the signature list of USER-ID."
636 (unless (eq (car user-id) 'epg-user-id)
637 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
638 (aset (cdr user-id) 2 signature-list))
640 (defun epg-context-result-for (context name)
641 "Return the result of CONTEXT associated with NAME."
642 (cdr (assq name (epg-context-result context))))
644 (defun epg-context-set-result-for (context name value)
645 "Set the result of CONTEXT associated with NAME to VALUE."
646 (let* ((result (epg-context-result context))
647 (entry (assq name result)))
650 (epg-context-set-result context (cons (cons name value) result)))))
652 (defun epg-signature-to-string (signature)
653 "Convert SIGNATURE to a human readable string."
654 (let ((user-id (cdr (assoc (epg-signature-key-id signature)
655 epg-user-id-alist))))
657 (cond ((eq (epg-signature-status signature) 'good)
658 "Good signature from ")
659 ((eq (epg-signature-status signature) 'bad)
660 "Bad signature from ")
661 ((eq (epg-signature-status signature) 'expired)
662 "Expired signature from ")
663 ((eq (epg-signature-status signature) 'expired-key)
664 "Signature made by expired key ")
665 ((eq (epg-signature-status signature) 'revoked-key)
666 "Signature made by revoked key ")
667 ((eq (epg-signature-status signature) 'no-pubkey)
668 "No public key for "))
669 (epg-signature-key-id signature)
672 (concat (if (stringp user-id)
674 (epg-decode-dn user-id))
677 (if (epg-signature-validity signature)
678 (format "(trust %s)" (epg-signature-validity signature))
681 (defun epg-verify-result-to-string (verify-result)
682 "Convert VERIFY-RESULT to a human readable string."
683 (mapconcat #'epg-signature-to-string verify-result "\n"))
685 (defun epg-new-signature-to-string (new-signature)
686 "Convert NEW-SIGNATURE to a human readable string."
688 (cond ((eq (epg-new-signature-type new-signature) 'detached)
689 "Detached signature ")
690 ((eq (epg-new-signature-type new-signature) 'clear)
691 "Clear text signature ")
694 (cdr (assq (epg-new-signature-pubkey-algorithm new-signature)
695 epg-pubkey-algorithm-alist))
697 (cdr (assq (epg-new-signature-digest-algorithm new-signature)
698 epg-digest-algorithm-alist))
700 (format "%02X " (epg-new-signature-class new-signature))
701 (epg-new-signature-fingerprint new-signature)))
703 (defun epg-start (context args)
704 "Start `epg-gpg-program' in a subprocess with given ARGS."
705 (if (and (epg-context-process context)
706 (eq (process-status (epg-context-process context)) 'run))
707 (error "%s is already running in this context"
708 (if (eq (epg-context-protocol context) 'CMS)
711 (let* ((args (append (list "--no-tty"
714 (unless (eq (epg-context-protocol context) 'CMS)
715 (list "--command-fd" "0"))
716 (if (epg-context-armor context) '("--armor"))
717 (if (epg-context-textmode context) '("--textmode"))
718 (if (epg-context-output-file context)
719 (list "--output" (epg-context-output-file context)))
721 (coding-system-for-write 'binary)
722 process-connection-type
723 (orig-mode (default-file-modes))
724 (buffer (generate-new-buffer " *epg*"))
728 (unless epg-debug-buffer
729 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
730 (set-buffer epg-debug-buffer)
731 (goto-char (point-max))
732 (insert (format "%s %s\n"
733 (if (eq (epg-context-protocol context) 'CMS)
736 (mapconcat #'identity args " ")))))
737 (with-current-buffer buffer
738 (make-local-variable 'epg-read-point)
739 (setq epg-read-point (point-min))
740 (make-local-variable 'epg-pending-status-list)
741 (setq epg-pending-status-list nil)
742 (make-local-variable 'epg-key-id)
743 (setq epg-key-id nil)
744 (make-local-variable 'epg-context)
745 (setq epg-context context))
748 (set-default-file-modes 448)
750 (apply #'start-process "epg" buffer
751 (if (eq (epg-context-protocol context) 'CMS)
755 (set-default-file-modes orig-mode))
756 (set-process-filter process #'epg-process-filter)
757 (epg-context-set-process context process)))
759 (defun epg-process-filter (process input)
762 (unless epg-debug-buffer
763 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
764 (set-buffer epg-debug-buffer)
765 (goto-char (point-max))
767 (if (buffer-live-p (process-buffer process))
769 (set-buffer (process-buffer process))
770 (goto-char (point-max))
772 (goto-char epg-read-point)
774 (while (looking-at ".*\n") ;the input line finished
776 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
777 (let* ((status (match-string 1))
778 (string (match-string 2))
779 (symbol (intern-soft (concat "epg-status-" status))))
780 (if (member status epg-pending-status-list)
781 (setq epg-pending-status-list nil))
784 (funcall symbol process string)))))
786 (setq epg-read-point (point)))))
788 (defun epg-read-output (context)
789 "Read the output file CONTEXT and return the content as a string."
791 (if (fboundp 'set-buffer-multibyte)
792 (set-buffer-multibyte nil))
793 (if (file-exists-p (epg-context-output-file context))
794 (let ((coding-system-for-read 'binary))
795 (insert-file-contents (epg-context-output-file context))
798 (defun epg-wait-for-status (context status-list)
799 "Wait until one of elements in STATUS-LIST arrives."
800 (with-current-buffer (process-buffer (epg-context-process context))
801 (setq epg-pending-status-list status-list)
802 (while (and (eq (process-status (epg-context-process context)) 'run)
803 epg-pending-status-list)
804 (accept-process-output (epg-context-process context) 0 1))))
806 (defun epg-wait-for-completion (context)
807 "Wait until the `epg-gpg-program' process completes."
808 (while (eq (process-status (epg-context-process context)) 'run)
809 (accept-process-output (epg-context-process context) 0 1)))
811 (defun epg-reset (context)
813 (if (and (epg-context-process context)
814 (buffer-live-p (process-buffer (epg-context-process context))))
815 (kill-buffer (process-buffer (epg-context-process context))))
816 (epg-context-set-process context nil))
818 (defun epg-delete-output-file (context)
819 "Delete the output file of CONTEXT."
820 (if (and (epg-context-output-file context)
821 (file-exists-p (epg-context-output-file context)))
822 (delete-file (epg-context-output-file context))))
824 (defun epg-status-USERID_HINT (process string)
825 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
826 (let* ((key-id (match-string 1 string))
827 (user-id (match-string 2 string))
828 (entry (assoc key-id epg-user-id-alist)))
830 (setcdr entry user-id)
831 (setq epg-user-id-alist (cons (cons key-id user-id)
832 epg-user-id-alist))))))
834 (defun epg-status-NEED_PASSPHRASE (process string)
835 (if (string-match "\\`\\([^ ]+\\)" string)
836 (setq epg-key-id (match-string 1 string))))
838 (defun epg-status-NEED_PASSPHRASE_SYM (process string)
839 (setq epg-key-id 'SYM))
841 (defun epg-status-NEED_PASSPHRASE_PIN (process string)
842 (setq epg-key-id 'PIN))
844 (defun epg-status-GET_HIDDEN (process string)
846 (string-match "\\`passphrase\\." string))
849 passphrase-with-new-line)
855 (if (consp (epg-context-passphrase-callback
857 (car (epg-context-passphrase-callback
859 (epg-context-passphrase-callback epg-context))
862 (if (consp (epg-context-passphrase-callback
864 (cdr (epg-context-passphrase-callback
867 (setq passphrase-with-new-line (concat passphrase "\n"))
868 (fillarray passphrase 0)
869 (setq passphrase nil)
870 (process-send-string process passphrase-with-new-line)))
872 (epg-context-set-result-for
875 (epg-context-result-for epg-context 'error)))
876 (delete-process process)))
878 (fillarray passphrase 0))
879 (if passphrase-with-new-line
880 (fillarray passphrase-with-new-line 0))))))
882 (defun epg-status-GET_BOOL (process string)
883 (let ((entry (assoc string epg-prompt-alist))
886 (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
887 (process-send-string process "y\n")
888 (process-send-string process "n\n"))
890 (epg-context-set-result-for
893 (epg-context-result-for epg-context 'error)))
894 (delete-process process)))))
896 (defun epg-status-GET_LINE (process string)
897 (let ((entry (assoc string epg-prompt-alist))
902 (concat (read-string (if entry (cdr entry) (concat string ": ")))
905 (epg-context-set-result-for
908 (epg-context-result-for epg-context 'error)))
909 (delete-process process)))))
911 (defun epg-status-*SIG (status string)
912 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
913 (let* ((key-id (match-string 1 string))
914 (user-id (match-string 2 string))
915 (entry (assoc key-id epg-user-id-alist)))
916 (epg-context-set-result-for
919 (cons (epg-make-signature status key-id)
920 (epg-context-result-for epg-context 'verify)))
921 (if (eq (epg-context-protocol epg-context) 'CMS)
923 (setq user-id (epg-dn-from-string user-id))
926 (setcdr entry user-id)
927 (setq epg-user-id-alist
928 (cons (cons key-id user-id) epg-user-id-alist))))
929 (epg-context-set-result-for
932 (cons (epg-make-signature status)
933 (epg-context-result-for epg-context 'verify)))))
935 (defun epg-status-GOODSIG (process string)
936 (epg-status-*SIG 'good string))
938 (defun epg-status-EXPSIG (process string)
939 (epg-status-*SIG 'expired string))
941 (defun epg-status-EXPKEYSIG (process string)
942 (epg-status-*SIG 'expired-key string))
944 (defun epg-status-REVKEYSIG (process string)
945 (epg-status-*SIG 'revoked-key string))
947 (defun epg-status-BADSIG (process string)
948 (epg-status-*SIG 'bad string))
950 (defun epg-status-NO_PUBKEY (process string)
951 (epg-context-set-result-for
954 (cons (epg-make-signature 'no-pubkey string)
955 (epg-context-result-for epg-context 'verify))))
957 (defun epg-status-ERRSIG (process string)
958 (let ((signatures (car (epg-context-result-for epg-context 'verify))))
960 (setq signatures (list (epg-make-signature 'error)))
961 (epg-context-set-result-for epg-context 'verify signatures))
962 (when (and (not (eq (epg-signature-status (car signatures)) 'good))
963 (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
964 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
966 (epg-signature-set-key-id
968 (match-string 1 string))
969 (epg-signature-set-pubkey-algorithm
971 (string-to-number (match-string 2 string)))
972 (epg-signature-set-digest-algorithm
974 (string-to-number (match-string 3 string)))
975 (epg-signature-set-class
977 (string-to-number (match-string 4 string) 16))
978 (epg-signature-set-creation-time
980 (match-string 5 string)))))
982 (defun epg-status-VALIDSIG (process string)
983 (let ((signature (car (epg-context-result-for epg-context 'verify))))
985 (eq (epg-signature-status signature) 'good)
986 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
987 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
990 (epg-signature-set-fingerprint
992 (match-string 1 string))
993 (epg-signature-set-creation-time
995 (match-string 2 string))
996 (epg-signature-set-expiration-time
998 (match-string 3 string))
999 (epg-signature-set-version
1001 (string-to-number (match-string 4 string)))
1002 (epg-signature-set-pubkey-algorithm
1004 (string-to-number (match-string 5 string)))
1005 (epg-signature-set-digest-algorithm
1007 (string-to-number (match-string 6 string)))
1008 (epg-signature-set-class
1010 (string-to-number (match-string 7 string) 16)))))
1012 (defun epg-status-TRUST_UNDEFINED (process string)
1013 (let ((signature (car (epg-context-result-for epg-context 'verify))))
1015 (eq (epg-signature-status signature) 'good))
1016 (epg-signature-set-validity signature 'undefined))))
1018 (defun epg-status-TRUST_NEVER (process string)
1019 (let ((signature (car (epg-context-result-for epg-context 'verify))))
1021 (eq (epg-signature-status signature) 'good))
1022 (epg-signature-set-validity signature 'never))))
1024 (defun epg-status-TRUST_MARGINAL (process string)
1025 (let ((signature (car (epg-context-result-for epg-context 'verify))))
1027 (eq (epg-signature-status signature) 'marginal))
1028 (epg-signature-set-validity signature 'marginal))))
1030 (defun epg-status-TRUST_FULLY (process string)
1031 (let ((signature (car (epg-context-result-for epg-context 'verify))))
1033 (eq (epg-signature-status signature) 'good))
1034 (epg-signature-set-validity signature 'full))))
1036 (defun epg-status-TRUST_ULTIMATE (process string)
1037 (let ((signature (car (epg-context-result-for epg-context 'verify))))
1039 (eq (epg-signature-status signature) 'good))
1040 (epg-signature-set-validity signature 'ultimate))))
1042 (defun epg-status-PROGRESS (process string)
1043 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1045 (funcall (if (consp (epg-context-progress-callback epg-context))
1046 (car (epg-context-progress-callback epg-context))
1047 (epg-context-progress-callback epg-context))
1049 (match-string 1 string)
1050 (match-string 2 string)
1051 (string-to-number (match-string 3 string))
1052 (string-to-number (match-string 4 string))
1053 (if (consp (epg-context-progress-callback epg-context))
1054 (cdr (epg-context-progress-callback epg-context))))))
1056 (defun epg-status-DECRYPTION_FAILED (process string)
1057 (epg-context-set-result-for
1059 (cons '(decryption-failed)
1060 (epg-context-result-for epg-context 'error))))
1062 (defun epg-status-NODATA (process string)
1063 (epg-context-set-result-for
1065 (cons (cons 'no-data (string-to-number string))
1066 (epg-context-result-for epg-context 'error))))
1068 (defun epg-status-UNEXPECTED (process string)
1069 (epg-context-set-result-for
1071 (cons (cons 'unexpected (string-to-number string))
1072 (epg-context-result-for epg-context 'error))))
1074 (defun epg-status-KEYEXPIRED (process string)
1075 (epg-context-set-result-for
1077 (cons (cons 'key-expired string)
1078 (epg-context-result-for epg-context 'error))))
1080 (defun epg-status-KEYREVOKED (process string)
1081 (epg-context-set-result-for
1083 (cons '(key-revoked)
1084 (epg-context-result-for epg-context 'error))))
1086 (defun epg-status-BADARMOR (process string)
1087 (epg-context-set-result-for
1090 (epg-context-result-for epg-context 'error))))
1092 (defun epg-status-INV_RECP (process string)
1093 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1094 (epg-context-set-result-for
1096 (cons (list 'invalid-recipient
1097 (string-to-number (match-string 1 string))
1098 (match-string 2 string))
1099 (epg-context-result-for epg-context 'error)))))
1101 (defun epg-status-NO_RECP (process string)
1102 (epg-context-set-result-for
1104 (cons '(no-recipients)
1105 (epg-context-result-for epg-context 'error))))
1107 (defun epg-status-DELETE_PROBLEM (process string)
1108 (if (string-match "\\`\\([0-9]+\\)" string)
1109 (epg-context-set-result-for
1111 (cons (cons 'delete-problem (string-to-number (match-string 1 string)))
1112 (epg-context-result-for epg-context 'error)))))
1114 (defun epg-status-SIG_CREATED (process string)
1115 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1116 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1117 (epg-context-set-result-for
1119 (cons (epg-make-new-signature
1120 (cdr (assq (aref (match-string 1 string) 0)
1121 epg-new-signature-type-alist))
1122 (string-to-number (match-string 2 string))
1123 (string-to-number (match-string 3 string))
1124 (string-to-number (match-string 4 string) 16)
1125 (match-string 5 string)
1126 (substring string (match-end 0)))
1127 (epg-context-result-for epg-context 'sign)))))
1129 (defun epg-status-KEY_CREATED (process string)
1130 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1131 (epg-context-set-result-for
1132 epg-context 'generate-key
1133 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1134 (cons 'fingerprint (match-string 2 string)))
1135 (epg-context-result-for epg-context 'generate-key)))))
1137 (defun epg-status-KEY_NOT_CREATED (process string)
1138 (epg-context-set-result-for
1140 (cons '(key-not-created)
1141 (epg-context-result-for epg-context 'error))))
1143 (defun epg-passphrase-callback-function (context key-id handback)
1145 (if (eq key-id 'SYM)
1146 "Passphrase for symmetric encryption: "
1147 (if (eq key-id 'PIN)
1148 "Passphrase for PIN: "
1149 (let ((entry (assoc key-id epg-user-id-alist)))
1151 (format "Passphrase for %s %s: " key-id (cdr entry))
1152 (format "Passphrase for %s: " key-id)))))))
1154 (defun epg-progress-callback-function (context what char current total
1156 (message "%s: %d%%/%d%%" what current total))
1158 (defun epg-configuration ()
1159 "Return a list of internal configuration parameters of `epg-gpg-program'."
1162 (apply #'call-process epg-gpg-program nil (list t nil) nil
1163 '("--with-colons" "--list-config"))
1164 (goto-char (point-min))
1165 (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
1166 (setq type (intern (match-string 1))
1167 config (cons (cons type
1169 '(pubkey cipher digest compress))
1170 (mapcar #'string-to-number
1171 (delete "" (split-string
1178 (defun epg-list-keys-1 (context name mode)
1179 (let ((args (append (list "--with-colons" "--no-greeting" "--batch"
1180 "--with-fingerprint"
1181 "--with-fingerprint"
1182 (if (or (eq mode t) (eq mode 'secret))
1183 "--list-secret-keys"
1187 (unless (eq (epg-context-protocol context) 'CMS)
1188 '("--fixed-list-mode"))
1189 (if name (list name))))
1190 keys string field index)
1192 (apply #'call-process
1193 (if (eq (epg-context-protocol context) 'CMS)
1196 nil (list t nil) nil args)
1197 (goto-char (point-min))
1198 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1199 (setq keys (cons (make-vector 15 nil) keys)
1200 string (match-string 0)
1204 (string-match "\\([^:]+\\)?:" string index))
1205 (setq index (match-end 0))
1206 (aset (car keys) field (match-string 1 string))
1207 (setq field (1+ field))))
1210 (defun epg-make-sub-key-1 (line)
1213 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1215 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1217 (member (aref line 0) '("sec" "ssb"))
1218 (string-to-number (aref line 3))
1219 (string-to-number (aref line 2))
1224 (defun epg-list-keys-postprocess-one-key (key)
1225 (let (key-id user-id-string entry)
1226 (epg-key-set-sub-key-list
1228 (nreverse (epg-key-sub-key-list key)))
1229 (epg-key-set-user-id-list
1231 (nreverse (epg-key-user-id-list key)))
1233 (epg-sub-key-id (car (epg-key-sub-key-list key)))
1235 (epg-user-id-string (car (epg-key-user-id-list key)))
1236 entry (assoc key-id epg-user-id-alist))
1238 (setcdr entry user-id-string)
1239 (setq epg-user-id-alist (cons (cons key-id user-id-string)
1240 epg-user-id-alist)))))
1242 (defun epg-list-keys (context &optional name mode)
1243 "Return a list of epg-key objects matched with NAME.
1244 If MODE is nil, only public keyring should be searched.
1245 If MODE is t or 'secret, only secret keyring should be searched.
1246 Otherwise, only public keyring should be searched and the key
1247 signatures should be included."
1248 (let ((lines (epg-list-keys-1 context name mode))
1252 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1254 (epg-list-keys-postprocess-one-key (car keys)))
1255 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1256 keys (cons (epg-make-key
1257 (if (aref (car lines) 8)
1258 (cdr (assq (string-to-char (aref (car lines) 8))
1259 epg-key-validity-alist))))
1261 (epg-key-set-sub-key-list
1263 (cons (epg-make-sub-key-1 (car lines))
1264 (epg-key-sub-key-list (car keys)))))
1265 ((member (aref (car lines) 0) '("sub" "ssb"))
1266 (epg-key-set-sub-key-list
1268 (cons (epg-make-sub-key-1 (car lines))
1269 (epg-key-sub-key-list (car keys)))))
1270 ((equal (aref (car lines) 0) "uid")
1271 (epg-key-set-user-id-list
1273 (cons (epg-make-user-id
1274 (if (aref (car lines) 1)
1275 (cdr (assq (string-to-char (aref (car lines) 1))
1276 epg-key-validity-alist)))
1279 (epg-dn-from-string (aref (car lines) 9))
1280 (error (aref (car lines) 9)))
1281 (aref (car lines) 9)))
1282 (epg-key-user-id-list (car keys)))))
1283 ((equal (aref (car lines) 0) "fpr")
1284 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1285 (aref (car lines) 9))))
1286 (setq lines (cdr lines)))
1288 (epg-list-keys-postprocess-one-key (car keys)))
1291 (if (fboundp 'make-temp-file)
1292 (defalias 'epg-make-temp-file 'make-temp-file)
1293 (defvar temporary-file-directory)
1294 ;; stolen from poe.el.
1295 (defun epg-make-temp-file (prefix)
1296 "Create a temporary file.
1297 The returned file name (created by appending some random characters at the end
1298 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1299 is guaranteed to point to a newly created empty file.
1300 You can then use `write-region' to write new data into the file."
1301 (let (tempdir tempfile)
1302 (setq prefix (expand-file-name prefix
1303 (if (featurep 'xemacs)
1305 temporary-file-directory)))
1308 ;; First, create a temporary directory.
1309 (while (condition-case ()
1311 (setq tempdir (make-temp-name
1313 (file-name-directory prefix)
1315 ;; return nil or signal an error.
1316 (make-directory tempdir))
1318 (file-already-exists t)))
1319 (set-file-modes tempdir 448)
1320 ;; Second, create a temporary file in the tempdir.
1321 ;; There *is* a race condition between `make-temp-name'
1322 ;; and `write-region', but we don't care it since we are
1323 ;; in a private directory now.
1324 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1325 (write-region "" nil tempfile nil 'silent)
1326 (set-file-modes tempfile 384)
1327 ;; Finally, make a hard-link from the tempfile.
1328 (while (condition-case ()
1330 (setq file (make-temp-name prefix))
1331 ;; return nil or signal an error.
1332 (add-name-to-file tempfile file))
1334 (file-already-exists t)))
1336 ;; Cleanup the tempfile.
1338 (file-exists-p tempfile)
1339 (delete-file tempfile))
1340 ;; Cleanup the tempdir.
1342 (file-directory-p tempdir)
1343 (delete-directory tempdir))))))
1346 (defun epg-cancel (context)
1347 (if (buffer-live-p (process-buffer (epg-context-process context)))
1349 (set-buffer (process-buffer (epg-context-process context)))
1350 (epg-context-set-result-for
1353 (epg-context-result-for epg-context 'error)))))
1354 (if (eq (process-status (epg-context-process context)) 'run)
1355 (delete-process (epg-context-process context))))
1358 (defun epg-start-decrypt (context cipher)
1359 "Initiate a decrypt operation on CIPHER.
1360 CIPHER is a data object.
1362 If you use this function, you will need to wait for the completion of
1363 `epg-gpg-program' by using `epg-wait-for-completion' and call
1364 `epg-reset' to clear a temporaly output file.
1365 If you are unsure, use synchronous version of this function
1366 `epg-decrypt-file' or `epg-decrypt-string' instead."
1367 (unless (epg-data-file cipher)
1368 (error "Not a file"))
1369 (epg-context-set-result context nil)
1370 (epg-start context (list "--decrypt" (epg-data-file cipher)))
1371 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1372 (unless (eq (epg-context-protocol context) 'CMS)
1373 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1376 (defun epg-decrypt-file (context cipher plain)
1377 "Decrypt a file CIPHER and store the result to a file PLAIN.
1378 If PLAIN is nil, it returns the result as a string."
1382 (epg-context-set-output-file context plain)
1383 (epg-context-set-output-file context
1384 (epg-make-temp-file "epg-output")))
1385 (epg-start-decrypt context (epg-make-data-from-file cipher))
1386 (epg-wait-for-completion context)
1387 (if (epg-context-result-for context 'error)
1388 (error "Decrypt failed: %S"
1389 (epg-context-result-for context 'error)))
1391 (epg-read-output context)))
1393 (epg-delete-output-file context))
1394 (epg-reset context)))
1397 (defun epg-decrypt-string (context cipher)
1398 "Decrypt a string CIPHER and return the plain text."
1399 (let ((input-file (epg-make-temp-file "epg-input"))
1400 (coding-system-for-write 'binary))
1403 (write-region cipher nil input-file nil 'quiet)
1404 (epg-context-set-output-file context
1405 (epg-make-temp-file "epg-output"))
1406 (epg-start-decrypt context (epg-make-data-from-file input-file))
1407 (epg-wait-for-completion context)
1408 (if (epg-context-result-for context 'error)
1409 (error "Decrypt failed: %S"
1410 (epg-context-result-for context 'error)))
1411 (epg-read-output context))
1412 (epg-delete-output-file context)
1413 (if (file-exists-p input-file)
1414 (delete-file input-file))
1415 (epg-reset context))))
1418 (defun epg-start-verify (context signature &optional signed-text)
1419 "Initiate a verify operation on SIGNATURE.
1420 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1422 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1423 For a normal or a clear text signature, SIGNED-TEXT should be nil.
1425 If you use this function, you will need to wait for the completion of
1426 `epg-gpg-program' by using `epg-wait-for-completion' and call
1427 `epg-reset' to clear a temporaly output file.
1428 If you are unsure, use synchronous version of this function
1429 `epg-verify-file' or `epg-verify-string' instead."
1430 (epg-context-set-result context nil)
1432 ;; Detached signature.
1433 (if (epg-data-file signed-text)
1434 (epg-start context (list "--verify" (epg-data-file signature)
1435 (epg-data-file signed-text)))
1436 (epg-start context (list "--verify" (epg-data-file signature) "-"))
1437 (if (eq (process-status (epg-context-process context)) 'run)
1438 (process-send-string (epg-context-process context)
1439 (epg-data-string signed-text)))
1440 (if (eq (process-status (epg-context-process context)) 'run)
1441 (process-send-eof (epg-context-process context))))
1442 ;; Normal (or cleartext) signature.
1443 (if (epg-data-file signature)
1444 (epg-start context (list "--verify" (epg-data-file signature)))
1445 (epg-start context (list "--verify"))
1446 (if (eq (process-status (epg-context-process context)) 'run)
1447 (process-send-string (epg-context-process context)
1448 (epg-data-string signature)))
1449 (if (eq (process-status (epg-context-process context)) 'run)
1450 (process-send-eof (epg-context-process context))))))
1453 (defun epg-verify-file (context signature &optional signed-text plain)
1454 "Verify a file SIGNATURE.
1455 SIGNED-TEXT and PLAIN are also a file if they are specified.
1457 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1458 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1462 (epg-context-set-output-file context plain)
1463 (epg-context-set-output-file context
1464 (epg-make-temp-file "epg-output")))
1466 (epg-start-verify context
1467 (epg-make-data-from-file signature)
1468 (epg-make-data-from-file signed-text))
1469 (epg-start-verify context
1470 (epg-make-data-from-file signature)))
1471 (epg-wait-for-completion context)
1472 ; (if (epg-context-result-for context 'error)
1473 ; (error "Verify failed: %S"
1474 ; (epg-context-result-for context 'error)))
1476 (epg-read-output context)))
1478 (epg-delete-output-file context))
1479 (epg-reset context)))
1482 (defun epg-verify-string (context signature &optional signed-text)
1483 "Verify a string SIGNATURE.
1484 SIGNED-TEXT is a string if it is specified.
1486 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1487 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1488 (let ((coding-system-for-write 'binary)
1492 (epg-context-set-output-file context
1493 (epg-make-temp-file "epg-output"))
1496 (setq input-file (epg-make-temp-file "epg-signature"))
1497 (write-region signature nil input-file nil 'quiet)
1498 (epg-start-verify context
1499 (epg-make-data-from-file input-file)
1500 (epg-make-data-from-string signed-text)))
1501 (epg-start-verify context (epg-make-data-from-string signature)))
1502 (epg-wait-for-completion context)
1503 ; (if (epg-context-result-for context 'error)
1504 ; (error "Verify failed: %S"
1505 ; (epg-context-result-for context 'error)))
1506 (epg-read-output context))
1507 (epg-delete-output-file context)
1509 (file-exists-p input-file))
1510 (delete-file input-file))
1511 (epg-reset context))))
1514 (defun epg-start-sign (context plain &optional mode)
1515 "Initiate a sign operation on PLAIN.
1516 PLAIN is a data object.
1518 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1519 If it is nil or 'normal, it makes a normal signature.
1520 Otherwise, it makes a clear text signature.
1522 If you use this function, you will need to wait for the completion of
1523 `epg-gpg-program' by using `epg-wait-for-completion' and call
1524 `epg-reset' to clear a temporaly output file.
1525 If you are unsure, use synchronous version of this function
1526 `epg-sign-file' or `epg-sign-string' instead."
1527 (epg-context-set-result context nil)
1529 (append (list (if (or (eq mode t) (eq mode 'detached))
1531 (if (or (null mode) (eq mode 'normal))
1539 (car (epg-key-sub-key-list signer)))))
1540 (epg-context-signers context)))
1541 (if (epg-data-file plain)
1542 (list (epg-data-file plain)))))
1543 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1544 (unless (eq (epg-context-protocol context) 'CMS)
1545 (epg-wait-for-status context '("BEGIN_SIGNING")))
1546 (when (epg-data-string plain)
1547 (if (eq (process-status (epg-context-process context)) 'run)
1548 (process-send-string (epg-context-process context)
1549 (epg-data-string plain)))
1550 (if (eq (process-status (epg-context-process context)) 'run)
1551 (process-send-eof (epg-context-process context)))))
1554 (defun epg-sign-file (context plain signature &optional mode)
1555 "Sign a file PLAIN and store the result to a file SIGNATURE.
1556 If SIGNATURE is nil, it returns the result as a string.
1557 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1558 If it is nil or 'normal, it makes a normal signature.
1559 Otherwise, it makes a clear text signature."
1563 (epg-context-set-output-file context signature)
1564 (epg-context-set-output-file context
1565 (epg-make-temp-file "epg-output")))
1566 (epg-start-sign context (epg-make-data-from-file plain) mode)
1567 (epg-wait-for-completion context)
1568 (unless (epg-context-result-for context 'sign)
1569 (if (epg-context-result-for context 'error)
1570 (error "Sign failed: %S"
1571 (epg-context-result-for context 'error))
1572 (error "Sign failed")))
1574 (epg-read-output context)))
1576 (epg-delete-output-file context))
1577 (epg-reset context)))
1580 (defun epg-sign-string (context plain &optional mode)
1581 "Sign a string PLAIN and return the output as string.
1582 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1583 If it is nil or 'normal, it makes a normal signature.
1584 Otherwise, it makes a clear text signature."
1587 (epg-context-set-output-file context
1588 (epg-make-temp-file "epg-output"))
1589 (epg-start-sign context (epg-make-data-from-string plain) mode)
1590 (epg-wait-for-completion context)
1591 (unless (epg-context-result-for context 'sign)
1592 (if (epg-context-result-for context 'error)
1593 (error "Sign failed: %S"
1594 (epg-context-result-for context 'error))
1595 (error "Sign failed")))
1596 (epg-read-output context))
1597 (epg-delete-output-file context)
1598 (epg-reset context)))
1601 (defun epg-start-encrypt (context plain recipients
1602 &optional sign always-trust)
1603 "Initiate an encrypt operation on PLAIN.
1604 PLAIN is a data object.
1605 If RECIPIENTS is nil, it performs symmetric encryption.
1607 If you use this function, you will need to wait for the completion of
1608 `epg-gpg-program' by using `epg-wait-for-completion' and call
1609 `epg-reset' to clear a temporaly output file.
1610 If you are unsure, use synchronous version of this function
1611 `epg-encrypt-file' or `epg-encrypt-string' instead."
1612 (epg-context-set-result context nil)
1614 (append (if always-trust '("--always-trust"))
1615 (if recipients '("--encrypt") '("--symmetric"))
1619 (mapcar (lambda (signer)
1621 (epg-context-signers context)))))
1627 (car (epg-key-sub-key-list recipient)))))
1629 (if (epg-data-file plain)
1630 (list (epg-data-file plain)))))
1631 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1632 (unless (eq (epg-context-protocol context) 'CMS)
1634 (epg-wait-for-status context '("BEGIN_SIGNING"))
1635 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
1636 (when (epg-data-string plain)
1637 (if (eq (process-status (epg-context-process context)) 'run)
1638 (process-send-string (epg-context-process context)
1639 (epg-data-string plain)))
1640 (if (eq (process-status (epg-context-process context)) 'run)
1641 (process-send-eof (epg-context-process context)))))
1644 (defun epg-encrypt-file (context plain recipients
1645 cipher &optional sign always-trust)
1646 "Encrypt a file PLAIN and store the result to a file CIPHER.
1647 If CIPHER is nil, it returns the result as a string.
1648 If RECIPIENTS is nil, it performs symmetric encryption."
1652 (epg-context-set-output-file context cipher)
1653 (epg-context-set-output-file context
1654 (epg-make-temp-file "epg-output")))
1655 (epg-start-encrypt context (epg-make-data-from-file plain)
1656 recipients sign always-trust)
1657 (epg-wait-for-completion context)
1659 (not (epg-context-result-for context 'sign)))
1660 (if (epg-context-result-for context 'error)
1661 (error "Sign failed: %S"
1662 (epg-context-result-for context 'error))
1663 (error "Sign failed")))
1664 (if (epg-context-result-for context 'error)
1665 (error "Encrypt failed: %S"
1666 (epg-context-result-for context 'error)))
1668 (epg-read-output context)))
1670 (epg-delete-output-file context))
1671 (epg-reset context)))
1674 (defun epg-encrypt-string (context plain recipients
1675 &optional sign always-trust)
1676 "Encrypt a string PLAIN.
1677 If RECIPIENTS is nil, it performs symmetric encryption."
1680 (epg-context-set-output-file context
1681 (epg-make-temp-file "epg-output"))
1682 (epg-start-encrypt context (epg-make-data-from-string plain)
1683 recipients sign always-trust)
1684 (epg-wait-for-completion context)
1686 (not (epg-context-result-for context 'sign)))
1687 (if (epg-context-result-for context 'error)
1688 (error "Sign failed: %S"
1689 (epg-context-result-for context 'error))
1690 (error "Sign failed")))
1691 (if (epg-context-result-for context 'error)
1692 (error "Encrypt failed: %S"
1693 (epg-context-result-for context 'error)))
1694 (epg-read-output context))
1695 (epg-delete-output-file context)
1696 (epg-reset context)))
1699 (defun epg-start-export-keys (context keys)
1700 "Initiate an export keys operation.
1702 If you use this function, you will need to wait for the completion of
1703 `epg-gpg-program' by using `epg-wait-for-completion' and call
1704 `epg-reset' to clear a temporaly output file.
1705 If you are unsure, use synchronous version of this function
1706 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1707 (epg-context-set-result context nil)
1708 (epg-start context (cons "--export"
1712 (car (epg-key-sub-key-list key))))
1716 (defun epg-export-keys-to-file (context keys file)
1717 "Extract public KEYS."
1721 (epg-context-set-output-file context file)
1722 (epg-context-set-output-file context
1723 (epg-make-temp-file "epg-output")))
1724 (epg-start-export-keys context keys)
1725 (epg-wait-for-completion context)
1726 (if (epg-context-result-for context 'error)
1727 (error "Export keys failed: %S"
1728 (epg-context-result-for context 'error)))
1730 (epg-read-output context)))
1732 (epg-delete-output-file context))
1733 (epg-reset context)))
1736 (defun epg-export-keys-to-string (context keys)
1737 "Extract public KEYS and return them as a string."
1738 (epg-export-keys-to-file context keys nil))
1741 (defun epg-start-import-keys (context keys)
1742 "Initiate an import keys operation.
1743 KEYS is a data object.
1745 If you use this function, you will need to wait for the completion of
1746 `epg-gpg-program' by using `epg-wait-for-completion' and call
1747 `epg-reset' to clear a temporaly output file.
1748 If you are unsure, use synchronous version of this function
1749 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1750 (epg-context-set-result context nil)
1751 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
1752 (epg-start context (list "--import" (epg-data-file keys)))
1753 (when (epg-data-string keys)
1754 (if (eq (process-status (epg-context-process context)) 'run)
1755 (process-send-string (epg-context-process context)
1756 (epg-data-string keys)))
1757 (if (eq (process-status (epg-context-process context)) 'run)
1758 (process-send-eof (epg-context-process context)))))
1760 (defun epg-import-keys-1 (context keys)
1763 (epg-start-import-keys context keys)
1764 (epg-wait-for-completion context)
1765 (if (epg-context-result-for context 'error)
1766 (error "Import keys failed: %S"
1767 (epg-context-result-for context 'error)))
1768 (epg-read-output context))
1769 (epg-reset context)))
1772 (defun epg-import-keys-from-file (context keys)
1773 "Add keys from a file KEYS."
1774 (epg-import-keys-1 context (epg-make-data-from-file keys)))
1777 (defun epg-import-keys-from-string (context keys)
1778 "Add keys from a string KEYS."
1779 (epg-import-keys-1 context (epg-make-data-from-string keys)))
1782 (defun epg-start-delete-keys (context keys &optional allow-secret)
1783 "Initiate an delete keys operation.
1785 If you use this function, you will need to wait for the completion of
1786 `epg-gpg-program' by using `epg-wait-for-completion' and call
1787 `epg-reset' to clear a temporaly output file.
1788 If you are unsure, use synchronous version of this function
1789 `epg-delete-keys' instead."
1790 (epg-context-set-result context nil)
1791 (epg-start context (cons (if allow-secret
1792 "--delete-secret-key"
1797 (car (epg-key-sub-key-list key))))
1801 (defun epg-delete-keys (context keys &optional allow-secret)
1802 "Delete KEYS from the key ring."
1805 (epg-start-delete-keys context keys allow-secret)
1806 (epg-wait-for-completion context)
1807 (if (epg-context-result-for context 'error)
1808 (error "Delete keys failed: %S"
1809 (epg-context-result-for context 'error))))
1810 (epg-reset context)))
1813 (defun epg-start-sign-keys (context keys &optional local)
1814 "Initiate an sign keys operation.
1816 If you use this function, you will need to wait for the completion of
1817 `epg-gpg-program' by using `epg-wait-for-completion' and call
1818 `epg-reset' to clear a temporaly output file.
1819 If you are unsure, use synchronous version of this function
1820 `epg-sign-keys' instead."
1821 (epg-context-set-result context nil)
1822 (epg-start context (cons (if local
1828 (car (epg-key-sub-key-list key))))
1832 (defun epg-sign-keys (context keys &optional local)
1833 "Sign KEYS from the key ring."
1836 (epg-start-sign-keys context keys local)
1837 (epg-wait-for-completion context)
1838 (if (epg-context-result-for context 'error)
1839 (error "Sign keys failed: %S"
1840 (epg-context-result-for context 'error))))
1841 (epg-reset context)))
1844 (defun epg-start-generate-key (context parameters)
1845 "Initiate a key generation.
1846 PARAMETERS specifies parameters for the key.
1848 If you use this function, you will need to wait for the completion of
1849 `epg-gpg-program' by using `epg-wait-for-completion' and call
1850 `epg-reset' to clear a temporaly output file.
1851 If you are unsure, use synchronous version of this function
1852 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
1853 (epg-context-set-result context nil)
1854 (if (epg-data-file parameters)
1855 (epg-start context (list "--batch" "--genkey"
1856 (epg-data-file parameters)))
1857 (epg-start context '("--batch" "--genkey"))
1858 (if (eq (process-status (epg-context-process context)) 'run)
1859 (process-send-string (epg-context-process context)
1860 (epg-data-string parameters)))
1861 (if (eq (process-status (epg-context-process context)) 'run)
1862 (process-send-eof (epg-context-process context)))))
1865 (defun epg-generate-key-from-file (context parameters)
1866 "Generate a new key pair.
1867 PARAMETERS is a file which tells how to create the key."
1870 (epg-start-generate-key context (epg-make-data-from-file parameters))
1871 (epg-wait-for-completion context)
1872 (if (epg-context-result-for context 'error)
1873 (error "Generate key failed: %S"
1874 (epg-context-result-for context 'error))))
1875 (epg-reset context)))
1878 (defun epg-generate-key-from-string (context parameters)
1879 "Generate a new key pair.
1880 PARAMETERS is a string which tells how to create the key."
1883 (epg-start-generate-key context (epg-make-data-from-string parameters))
1884 (epg-wait-for-completion context)
1885 (if (epg-context-result-for context 'error)
1886 (error "Generate key failed: %S"
1887 (epg-context-result-for context 'error))))
1888 (epg-reset context)))
1890 (defun epg-decode-hexstring (string)
1892 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
1893 (setq string (replace-match "\\x\\&" t nil string)
1895 (car (read-from-string (concat "\"" string "\"")))))
1897 (defun epg-decode-quotedstring (string)
1899 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
1900 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
1902 (if (match-beginning 2)
1903 (setq string (replace-match "\\2" t nil string)
1905 (if (match-beginning 3)
1906 (setq string (replace-match "\\x\\3" t nil string)
1908 (setq string (replace-match "\\\\\\\\\\4" t nil string)
1909 index (+ index 3)))))
1910 (car (read-from-string (concat "\"" string "\"")))))
1912 (defun epg-dn-from-string (string)
1913 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
1914 The return value is an alist mapping from types to values."
1916 (length (length string))
1917 alist type value group)
1918 (while (< index length)
1919 (if (eq index (string-match "[ \t\n\r]*" string index))
1920 (setq index (match-end 0)))
1921 (if (eq index (string-match
1922 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
1924 (setq type (match-string 1 string)
1925 index (match-end 0))
1926 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
1928 (setq type (match-string 1 string)
1929 index (match-end 0))))
1931 (error "Invalid type"))
1932 (if (eq index (string-match
1933 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
1935 (setq index (match-end 0)
1936 value (epg-decode-quotedstring (match-string 0 string)))
1937 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
1938 (setq index (match-end 0)
1939 value (epg-decode-hexstring (match-string 1 string)))
1940 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
1942 (setq index (match-end 0)
1943 value (epg-decode-quotedstring (match-string 0 string))))))
1945 (if (stringp (car (car alist)))
1946 (setcar alist (list (cons type value) (car alist)))
1947 (setcar alist (cons (cons type value) (car alist))))
1948 (if (consp (car (car alist)))
1949 (setcar alist (nreverse (car alist))))
1950 (setq alist (cons (cons type value) alist)
1953 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
1954 (setq index (match-end 0)
1955 group (eq (aref string (match-beginning 1)) ?+))))
1958 (defun epg-decode-dn (alist)
1959 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
1960 Type names are resolved using `epg-dn-type-alist'."
1963 (if (stringp (car rdn))
1964 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
1966 (format "%s=%s" (cdr entry) (cdr rdn))
1967 (format "%s=%s" (car rdn) (cdr rdn))))
1968 (concat "(" (epg-decode-dn rdn) ")")))
1974 ;;; epg.el ends here