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-new-signature-to-string (new-signature)
662 "Convert NEW-SIGNATURE to a human readable string."
664 (cond ((eq (epg-new-signature-type new-signature) 'detached)
665 "Detached signature ")
666 ((eq (epg-new-signature-type new-signature) 'clear)
667 "Clear text signature ")
670 (cdr (assq (epg-new-signature-pubkey-algorithm new-signature)
671 epg-pubkey-algorithm-alist))
673 (cdr (assq (epg-new-signature-digest-algorithm new-signature)
674 epg-digest-algorithm-alist))
676 (format "%02X " (epg-new-signature-class new-signature))
677 (epg-new-signature-fingerprint new-signature)))
679 (defun epg-start (context args)
680 "Start `epg-gpg-program' in a subprocess with given ARGS."
681 (if (and (epg-context-process context)
682 (eq (process-status (epg-context-process context)) 'run))
683 (error "%s is already running in this context"
684 (if (eq (epg-context-protocol context) 'CMS)
687 (let* ((args (append (list "--no-tty"
690 (unless (eq (epg-context-protocol context) 'CMS)
691 (list "--command-fd" "0"))
692 (if (epg-context-armor context) '("--armor"))
693 (if (epg-context-textmode context) '("--textmode"))
694 (if (epg-context-output-file context)
695 (list "--output" (epg-context-output-file context)))
697 (coding-system-for-write 'binary)
698 process-connection-type
699 (orig-mode (default-file-modes))
700 (buffer (generate-new-buffer " *epg*"))
704 (unless epg-debug-buffer
705 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
706 (set-buffer epg-debug-buffer)
707 (goto-char (point-max))
708 (insert (format "%s %s\n"
709 (if (eq (epg-context-protocol context) 'CMS)
712 (mapconcat #'identity args " ")))))
713 (with-current-buffer buffer
714 (make-local-variable 'epg-read-point)
715 (setq epg-read-point (point-min))
716 (make-local-variable 'epg-pending-status-list)
717 (setq epg-pending-status-list nil)
718 (make-local-variable 'epg-key-id)
719 (setq epg-key-id nil)
720 (make-local-variable 'epg-context)
721 (setq epg-context context))
724 (set-default-file-modes 448)
726 (apply #'start-process "epg" buffer
727 (if (eq (epg-context-protocol context) 'CMS)
731 (set-default-file-modes orig-mode))
732 (set-process-filter process #'epg-process-filter)
733 (epg-context-set-process context process)))
735 (defun epg-process-filter (process input)
738 (unless epg-debug-buffer
739 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
740 (set-buffer epg-debug-buffer)
741 (goto-char (point-max))
743 (if (buffer-live-p (process-buffer process))
745 (set-buffer (process-buffer process))
746 (goto-char (point-max))
748 (goto-char epg-read-point)
750 (while (looking-at ".*\n") ;the input line finished
752 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
753 (let* ((status (match-string 1))
754 (string (match-string 2))
755 (symbol (intern-soft (concat "epg-status-" status))))
756 (if (member status epg-pending-status-list)
757 (setq epg-pending-status-list nil))
760 (funcall symbol process string)))))
762 (setq epg-read-point (point)))))
764 (defun epg-read-output (context)
765 "Read the output file CONTEXT and return the content as a string."
767 (if (fboundp 'set-buffer-multibyte)
768 (set-buffer-multibyte nil))
769 (if (file-exists-p (epg-context-output-file context))
770 (let ((coding-system-for-read 'binary))
771 (insert-file-contents (epg-context-output-file context))
774 (defun epg-wait-for-status (context status-list)
775 "Wait until one of elements in STATUS-LIST arrives."
776 (with-current-buffer (process-buffer (epg-context-process context))
777 (setq epg-pending-status-list status-list)
778 (while (and (eq (process-status (epg-context-process context)) 'run)
779 epg-pending-status-list)
780 (accept-process-output (epg-context-process context) 0 1))))
782 (defun epg-wait-for-completion (context)
783 "Wait until the `epg-gpg-program' process completes."
784 (while (eq (process-status (epg-context-process context)) 'run)
785 (accept-process-output (epg-context-process context) 0 1)))
787 (defun epg-reset (context)
789 (if (and (epg-context-process context)
790 (buffer-live-p (process-buffer (epg-context-process context))))
791 (kill-buffer (process-buffer (epg-context-process context))))
792 (epg-context-set-process context nil))
794 (defun epg-delete-output-file (context)
795 "Delete the output file of CONTEXT."
796 (if (and (epg-context-output-file context)
797 (file-exists-p (epg-context-output-file context)))
798 (delete-file (epg-context-output-file context))))
800 (defun epg-status-USERID_HINT (process string)
801 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
802 (let* ((key-id (match-string 1 string))
803 (user-id (match-string 2 string))
804 (entry (assoc key-id epg-user-id-alist)))
806 (setcdr entry user-id)
807 (setq epg-user-id-alist (cons (cons key-id user-id)
808 epg-user-id-alist))))))
810 (defun epg-status-NEED_PASSPHRASE (process string)
811 (if (string-match "\\`\\([^ ]+\\)" string)
812 (setq epg-key-id (match-string 1 string))))
814 (defun epg-status-NEED_PASSPHRASE_SYM (process string)
815 (setq epg-key-id 'SYM))
817 (defun epg-status-NEED_PASSPHRASE_PIN (process string)
818 (setq epg-key-id 'PIN))
820 (defun epg-status-GET_HIDDEN (process string)
822 (string-match "\\`passphrase\\." string))
825 passphrase-with-new-line)
831 (if (consp (epg-context-passphrase-callback
833 (car (epg-context-passphrase-callback
835 (epg-context-passphrase-callback epg-context))
838 (if (consp (epg-context-passphrase-callback
840 (cdr (epg-context-passphrase-callback
843 (setq passphrase-with-new-line (concat passphrase "\n"))
844 (fillarray passphrase 0)
845 (setq passphrase nil)
846 (process-send-string process passphrase-with-new-line)))
848 (epg-context-set-result-for
851 (epg-context-result-for epg-context 'error)))
852 (delete-process process)))
854 (fillarray passphrase 0))
855 (if passphrase-with-new-line
856 (fillarray passphrase-with-new-line 0))))))
858 (defun epg-status-GET_BOOL (process string)
859 (let ((entry (assoc string epg-prompt-alist))
862 (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
863 (process-send-string process "y\n")
864 (process-send-string process "n\n"))
866 (epg-context-set-result-for
869 (epg-context-result-for epg-context 'error)))
870 (delete-process process)))))
872 (defun epg-status-GET_LINE (process string)
873 (let ((entry (assoc string epg-prompt-alist))
878 (concat (read-string (if entry (cdr entry) (concat string ": ")))
881 (epg-context-set-result-for
884 (epg-context-result-for epg-context 'error)))
885 (delete-process process)))))
887 (defun epg-status-*SIG (status string)
888 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
889 (let* ((key-id (match-string 1 string))
890 (user-id (match-string 2 string))
891 (entry (assoc key-id epg-user-id-alist)))
892 (epg-context-set-result-for
895 (cons (epg-make-signature status key-id)
896 (epg-context-result-for epg-context 'verify)))
897 (if (eq (epg-context-protocol epg-context) 'CMS)
899 (setq user-id (epg-dn-from-string user-id))
902 (setcdr entry user-id)
903 (setq epg-user-id-alist
904 (cons (cons key-id user-id) epg-user-id-alist))))
905 (epg-context-set-result-for
908 (cons (epg-make-signature status)
909 (epg-context-result-for epg-context 'verify)))))
911 (defun epg-status-GOODSIG (process string)
912 (epg-status-*SIG 'good string))
914 (defun epg-status-EXPSIG (process string)
915 (epg-status-*SIG 'expired string))
917 (defun epg-status-EXPKEYSIG (process string)
918 (epg-status-*SIG 'expired-key string))
920 (defun epg-status-REVKEYSIG (process string)
921 (epg-status-*SIG 'revoked-key string))
923 (defun epg-status-BADSIG (process string)
924 (epg-status-*SIG 'bad string))
926 (defun epg-status-NO_PUBKEY (process string)
927 (epg-context-set-result-for
930 (cons (epg-make-signature 'no-pubkey string)
931 (epg-context-result-for epg-context 'verify))))
933 (defun epg-status-ERRSIG (process string)
934 (let ((signatures (car (epg-context-result-for epg-context 'verify))))
936 (setq signatures (list (epg-make-signature 'error)))
937 (epg-context-set-result-for epg-context 'verify signatures))
938 (when (and (not (eq (epg-signature-status (car signatures)) 'good))
939 (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
940 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
942 (epg-signature-set-key-id
944 (match-string 1 string))
945 (epg-signature-set-pubkey-algorithm
947 (string-to-number (match-string 2 string)))
948 (epg-signature-set-digest-algorithm
950 (string-to-number (match-string 3 string)))
951 ; (epg-signature-set-class
953 ; (string-to-number (match-string 4 string) 16))
954 (epg-signature-set-creation-time
956 (match-string 5 string)))))
958 (defun epg-status-VALIDSIG (process string)
959 (let ((signature (car (epg-context-result-for epg-context 'verify))))
961 (eq (epg-signature-status signature) 'good)
962 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
963 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
966 (epg-signature-set-fingerprint
968 (match-string 1 string))
969 (epg-signature-set-creation-time
971 (match-string 2 string))
972 (epg-signature-set-expiration-time
974 (match-string 3 string))
975 ; (epg-signature-set-version
977 ; (string-to-number (match-string 4 string)))
978 (epg-signature-set-pubkey-algorithm
980 (string-to-number (match-string 5 string)))
981 (epg-signature-set-digest-algorithm
983 (string-to-number (match-string 6 string)))
984 ; (epg-signature-set-class
986 ; (string-to-number (match-string 7 string) 16))
989 (defun epg-status-TRUST_UNDEFINED (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 'undefined))))
995 (defun epg-status-TRUST_NEVER (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 'never))))
1001 (defun epg-status-TRUST_MARGINAL (process string)
1002 (let ((signature (car (epg-context-result-for epg-context 'verify))))
1004 (eq (epg-signature-status signature) 'marginal))
1005 (epg-signature-set-validity signature 'marginal))))
1007 (defun epg-status-TRUST_FULLY (process string)
1008 (let ((signature (car (epg-context-result-for epg-context 'verify))))
1010 (eq (epg-signature-status signature) 'good))
1011 (epg-signature-set-validity signature 'full))))
1013 (defun epg-status-TRUST_ULTIMATE (process string)
1014 (let ((signature (car (epg-context-result-for epg-context 'verify))))
1016 (eq (epg-signature-status signature) 'good))
1017 (epg-signature-set-validity signature 'ultimate))))
1019 (defun epg-status-PROGRESS (process string)
1020 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1022 (funcall (if (consp (epg-context-progress-callback epg-context))
1023 (car (epg-context-progress-callback epg-context))
1024 (epg-context-progress-callback epg-context))
1026 (match-string 1 string)
1027 (match-string 2 string)
1028 (string-to-number (match-string 3 string))
1029 (string-to-number (match-string 4 string))
1030 (if (consp (epg-context-progress-callback epg-context))
1031 (cdr (epg-context-progress-callback epg-context))))))
1033 (defun epg-status-DECRYPTION_FAILED (process string)
1034 (epg-context-set-result-for
1036 (cons '(decryption-failed)
1037 (epg-context-result-for epg-context 'error))))
1039 (defun epg-status-NODATA (process string)
1040 (epg-context-set-result-for
1042 (cons (cons 'no-data (string-to-number string))
1043 (epg-context-result-for epg-context 'error))))
1045 (defun epg-status-UNEXPECTED (process string)
1046 (epg-context-set-result-for
1048 (cons (cons 'unexpected (string-to-number string))
1049 (epg-context-result-for epg-context 'error))))
1051 (defun epg-status-KEYEXPIRED (process string)
1052 (epg-context-set-result-for
1054 (cons (cons 'key-expired string)
1055 (epg-context-result-for epg-context 'error))))
1057 (defun epg-status-KEYREVOKED (process string)
1058 (epg-context-set-result-for
1060 (cons '(key-revoked)
1061 (epg-context-result-for epg-context 'error))))
1063 (defun epg-status-BADARMOR (process string)
1064 (epg-context-set-result-for
1067 (epg-context-result-for epg-context 'error))))
1069 (defun epg-status-INV_RECP (process string)
1070 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1071 (epg-context-set-result-for
1073 (cons (list 'invalid-recipient
1074 (string-to-number (match-string 1 string))
1075 (match-string 2 string))
1076 (epg-context-result-for epg-context 'error)))))
1078 (defun epg-status-NO_RECP (process string)
1079 (epg-context-set-result-for
1081 (cons '(no-recipients)
1082 (epg-context-result-for epg-context 'error))))
1084 (defun epg-status-DELETE_PROBLEM (process string)
1085 (if (string-match "\\`\\([0-9]+\\)" string)
1086 (epg-context-set-result-for
1088 (cons (cons 'delete-problem (string-to-number (match-string 1 string)))
1089 (epg-context-result-for epg-context 'error)))))
1091 (defun epg-status-SIG_CREATED (process string)
1092 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1093 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1094 (epg-context-set-result-for
1096 (cons (epg-make-new-signature
1097 (cdr (assq (aref (match-string 1 string) 0)
1098 epg-new-signature-type-alist))
1099 (string-to-number (match-string 2 string))
1100 (string-to-number (match-string 3 string))
1101 (string-to-number (match-string 4 string) 16)
1102 (match-string 5 string)
1103 (substring string (match-end 0)))
1104 (epg-context-result-for epg-context 'sign)))))
1106 (defun epg-status-KEY_CREATED (process string)
1107 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1108 (epg-context-set-result-for
1109 epg-context 'generate-key
1110 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1111 (cons 'fingerprint (match-string 2 string)))
1112 (epg-context-result-for epg-context 'generate-key)))))
1114 (defun epg-status-KEY_NOT_CREATED (process string)
1115 (epg-context-set-result-for
1117 (cons '(key-not-created)
1118 (epg-context-result-for epg-context 'error))))
1120 (defun epg-passphrase-callback-function (context key-id handback)
1122 (if (eq key-id 'SYM)
1123 "Passphrase for symmetric encryption: "
1124 (if (eq key-id 'PIN)
1125 "Passphrase for PIN: "
1126 (let ((entry (assoc key-id epg-user-id-alist)))
1128 (format "Passphrase for %s %s: " key-id (cdr entry))
1129 (format "Passphrase for %s: " key-id)))))))
1131 (defun epg-progress-callback-function (context what char current total
1133 (message "%s: %d%%/%d%%" what current total))
1135 (defun epg-configuration ()
1136 "Return a list of internal configuration parameters of `epg-gpg-program'."
1139 (apply #'call-process epg-gpg-program nil (list t nil) nil
1140 '("--with-colons" "--list-config"))
1141 (goto-char (point-min))
1142 (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
1143 (setq type (intern (match-string 1))
1144 config (cons (cons type
1146 '(pubkey cipher digest compress))
1147 (mapcar #'string-to-number
1148 (delete "" (split-string
1155 (defun epg-list-keys-1 (context name mode)
1156 (let ((args (append (list "--with-colons" "--no-greeting" "--batch"
1157 "--with-fingerprint"
1158 "--with-fingerprint"
1159 (if (or (eq mode t) (eq mode 'secret))
1160 "--list-secret-keys"
1164 (unless (eq (epg-context-protocol context) 'CMS)
1165 '("--fixed-list-mode"))
1166 (if name (list name))))
1167 keys string field index)
1169 (apply #'call-process
1170 (if (eq (epg-context-protocol context) 'CMS)
1173 nil (list t nil) nil args)
1174 (goto-char (point-min))
1175 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1176 (setq keys (cons (make-vector 15 nil) keys)
1177 string (match-string 0)
1181 (string-match "\\([^:]+\\)?:" string index))
1182 (setq index (match-end 0))
1183 (aset (car keys) field (match-string 1 string))
1184 (setq field (1+ field))))
1187 (defun epg-make-sub-key-1 (line)
1190 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1192 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1194 (member (aref line 0) '("sec" "ssb"))
1195 (string-to-number (aref line 3))
1196 (string-to-number (aref line 2))
1201 (defun epg-list-keys-postprocess-one-key (key)
1202 (let (key-id user-id-string entry)
1203 (epg-key-set-sub-key-list
1205 (nreverse (epg-key-sub-key-list key)))
1206 (epg-key-set-user-id-list
1208 (nreverse (epg-key-user-id-list key)))
1210 (epg-sub-key-id (car (epg-key-sub-key-list key)))
1212 (epg-user-id-string (car (epg-key-user-id-list key)))
1213 entry (assoc key-id epg-user-id-alist))
1215 (setcdr entry user-id-string)
1216 (setq epg-user-id-alist (cons (cons key-id user-id-string)
1217 epg-user-id-alist)))))
1219 (defun epg-list-keys (context &optional name mode)
1220 "Return a list of epg-key objects matched with NAME.
1221 If MODE is nil, only public keyring should be searched.
1222 If MODE is t or 'secret, only secret keyring should be searched.
1223 Otherwise, only public keyring should be searched and the key
1224 signatures should be included."
1225 (let ((lines (epg-list-keys-1 context name mode))
1229 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1231 (epg-list-keys-postprocess-one-key (car keys)))
1232 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1233 keys (cons (epg-make-key
1234 (if (aref (car lines) 8)
1235 (cdr (assq (string-to-char (aref (car lines) 8))
1236 epg-key-validity-alist))))
1238 (epg-key-set-sub-key-list
1240 (cons (epg-make-sub-key-1 (car lines))
1241 (epg-key-sub-key-list (car keys)))))
1242 ((member (aref (car lines) 0) '("sub" "ssb"))
1243 (epg-key-set-sub-key-list
1245 (cons (epg-make-sub-key-1 (car lines))
1246 (epg-key-sub-key-list (car keys)))))
1247 ((equal (aref (car lines) 0) "uid")
1248 (epg-key-set-user-id-list
1250 (cons (epg-make-user-id
1251 (if (aref (car lines) 1)
1252 (cdr (assq (string-to-char (aref (car lines) 1))
1253 epg-key-validity-alist)))
1256 (epg-dn-from-string (aref (car lines) 9))
1257 (error (aref (car lines) 9)))
1258 (aref (car lines) 9)))
1259 (epg-key-user-id-list (car keys)))))
1260 ((equal (aref (car lines) 0) "fpr")
1261 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1262 (aref (car lines) 9))))
1263 (setq lines (cdr lines)))
1265 (epg-list-keys-postprocess-one-key (car keys)))
1268 (if (fboundp 'make-temp-file)
1269 (defalias 'epg-make-temp-file 'make-temp-file)
1270 (defvar temporary-file-directory)
1271 ;; stolen from poe.el.
1272 (defun epg-make-temp-file (prefix)
1273 "Create a temporary file.
1274 The returned file name (created by appending some random characters at the end
1275 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1276 is guaranteed to point to a newly created empty file.
1277 You can then use `write-region' to write new data into the file."
1278 (let (tempdir tempfile)
1279 (setq prefix (expand-file-name prefix
1280 (if (featurep 'xemacs)
1282 temporary-file-directory)))
1285 ;; First, create a temporary directory.
1286 (while (condition-case ()
1288 (setq tempdir (make-temp-name
1290 (file-name-directory prefix)
1292 ;; return nil or signal an error.
1293 (make-directory tempdir))
1295 (file-already-exists t)))
1296 (set-file-modes tempdir 448)
1297 ;; Second, create a temporary file in the tempdir.
1298 ;; There *is* a race condition between `make-temp-name'
1299 ;; and `write-region', but we don't care it since we are
1300 ;; in a private directory now.
1301 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1302 (write-region "" nil tempfile nil 'silent)
1303 (set-file-modes tempfile 384)
1304 ;; Finally, make a hard-link from the tempfile.
1305 (while (condition-case ()
1307 (setq file (make-temp-name prefix))
1308 ;; return nil or signal an error.
1309 (add-name-to-file tempfile file))
1311 (file-already-exists t)))
1313 ;; Cleanup the tempfile.
1315 (file-exists-p tempfile)
1316 (delete-file tempfile))
1317 ;; Cleanup the tempdir.
1319 (file-directory-p tempdir)
1320 (delete-directory tempdir))))))
1323 (defun epg-cancel (context)
1324 (if (buffer-live-p (process-buffer (epg-context-process context)))
1326 (set-buffer (process-buffer (epg-context-process context)))
1327 (epg-context-set-result-for
1330 (epg-context-result-for epg-context 'error)))))
1331 (if (eq (process-status (epg-context-process context)) 'run)
1332 (delete-process (epg-context-process context))))
1335 (defun epg-start-decrypt (context cipher)
1336 "Initiate a decrypt operation on CIPHER.
1337 CIPHER is a data object.
1339 If you use this function, you will need to wait for the completion of
1340 `epg-gpg-program' by using `epg-wait-for-completion' and call
1341 `epg-reset' to clear a temporaly output file.
1342 If you are unsure, use synchronous version of this function
1343 `epg-decrypt-file' or `epg-decrypt-string' instead."
1344 (unless (epg-data-file cipher)
1345 (error "Not a file"))
1346 (epg-context-set-result context nil)
1347 (epg-start context (list "--decrypt" (epg-data-file cipher)))
1348 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1349 (unless (eq (epg-context-protocol context) 'CMS)
1350 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1353 (defun epg-decrypt-file (context cipher plain)
1354 "Decrypt a file CIPHER and store the result to a file PLAIN.
1355 If PLAIN is nil, it returns the result as a string."
1359 (epg-context-set-output-file context plain)
1360 (epg-context-set-output-file context
1361 (epg-make-temp-file "epg-output")))
1362 (epg-start-decrypt context (epg-make-data-from-file cipher))
1363 (epg-wait-for-completion context)
1364 (if (epg-context-result-for context 'error)
1365 (error "Decrypt failed: %S"
1366 (epg-context-result-for context 'error)))
1368 (epg-read-output context)))
1370 (epg-delete-output-file context))
1371 (epg-reset context)))
1374 (defun epg-decrypt-string (context cipher)
1375 "Decrypt a string CIPHER and return the plain text."
1376 (let ((input-file (epg-make-temp-file "epg-input"))
1377 (coding-system-for-write 'binary))
1380 (write-region cipher nil input-file nil 'quiet)
1381 (epg-context-set-output-file context
1382 (epg-make-temp-file "epg-output"))
1383 (epg-start-decrypt context (epg-make-data-from-file input-file))
1384 (epg-wait-for-completion context)
1385 (if (epg-context-result-for context 'error)
1386 (error "Decrypt failed: %S"
1387 (epg-context-result-for context 'error)))
1388 (epg-read-output context))
1389 (epg-delete-output-file context)
1390 (if (file-exists-p input-file)
1391 (delete-file input-file))
1392 (epg-reset context))))
1395 (defun epg-start-verify (context signature &optional signed-text)
1396 "Initiate a verify operation on SIGNATURE.
1397 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1399 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1400 For a normal or a clear text signature, SIGNED-TEXT should be nil.
1402 If you use this function, you will need to wait for the completion of
1403 `epg-gpg-program' by using `epg-wait-for-completion' and call
1404 `epg-reset' to clear a temporaly output file.
1405 If you are unsure, use synchronous version of this function
1406 `epg-verify-file' or `epg-verify-string' instead."
1407 (epg-context-set-result context nil)
1409 ;; Detached signature.
1410 (if (epg-data-file signed-text)
1411 (epg-start context (list "--verify" (epg-data-file signature)
1412 (epg-data-file signed-text)))
1413 (epg-start context (list "--verify" (epg-data-file signature) "-"))
1414 (if (eq (process-status (epg-context-process context)) 'run)
1415 (process-send-string (epg-context-process context)
1416 (epg-data-string signed-text)))
1417 (if (eq (process-status (epg-context-process context)) 'run)
1418 (process-send-eof (epg-context-process context))))
1419 ;; Normal (or cleartext) signature.
1420 (if (epg-data-file signature)
1421 (epg-start context (list "--verify" (epg-data-file signature)))
1422 (epg-start context (list "--verify"))
1423 (if (eq (process-status (epg-context-process context)) 'run)
1424 (process-send-string (epg-context-process context)
1425 (epg-data-string signature)))
1426 (if (eq (process-status (epg-context-process context)) 'run)
1427 (process-send-eof (epg-context-process context))))))
1430 (defun epg-verify-file (context signature &optional signed-text plain)
1431 "Verify a file SIGNATURE.
1432 SIGNED-TEXT and PLAIN are also a file if they are specified.
1434 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1435 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1439 (epg-context-set-output-file context plain)
1440 (epg-context-set-output-file context
1441 (epg-make-temp-file "epg-output")))
1443 (epg-start-verify context
1444 (epg-make-data-from-file signature)
1445 (epg-make-data-from-file signed-text))
1446 (epg-start-verify context
1447 (epg-make-data-from-file signature)))
1448 (epg-wait-for-completion context)
1449 ; (if (epg-context-result-for context 'error)
1450 ; (error "Verify failed: %S"
1451 ; (epg-context-result-for context 'error)))
1453 (epg-read-output context)))
1455 (epg-delete-output-file context))
1456 (epg-reset context)))
1459 (defun epg-verify-string (context signature &optional signed-text)
1460 "Verify a string SIGNATURE.
1461 SIGNED-TEXT is a string if it is specified.
1463 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1464 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1465 (let ((coding-system-for-write 'binary)
1469 (epg-context-set-output-file context
1470 (epg-make-temp-file "epg-output"))
1473 (setq input-file (epg-make-temp-file "epg-signature"))
1474 (write-region signature nil input-file nil 'quiet)
1475 (epg-start-verify context
1476 (epg-make-data-from-file input-file)
1477 (epg-make-data-from-string signed-text)))
1478 (epg-start-verify context (epg-make-data-from-string signature)))
1479 (epg-wait-for-completion context)
1480 ; (if (epg-context-result-for context 'error)
1481 ; (error "Verify failed: %S"
1482 ; (epg-context-result-for context 'error)))
1483 (epg-read-output context))
1484 (epg-delete-output-file context)
1486 (file-exists-p input-file))
1487 (delete-file input-file))
1488 (epg-reset context))))
1491 (defun epg-start-sign (context plain &optional mode)
1492 "Initiate a sign operation on PLAIN.
1493 PLAIN is a data object.
1495 If optional 3rd argument MODE is 'clear, it makes a clear text signature.
1496 If MODE is t or 'detached, it makes a detached signature.
1497 Otherwise, it makes a normal signature.
1499 If you use this function, you will need to wait for the completion of
1500 `epg-gpg-program' by using `epg-wait-for-completion' and call
1501 `epg-reset' to clear a temporaly output file.
1502 If you are unsure, use synchronous version of this function
1503 `epg-sign-file' or `epg-sign-string' instead."
1504 (epg-context-set-result context nil)
1506 (append (list (if (eq mode 'clear)
1508 (if (or (eq mode t) (eq mode 'detached))
1516 (car (epg-key-sub-key-list signer)))))
1517 (epg-context-signers context)))
1518 (if (epg-data-file plain)
1519 (list (epg-data-file plain)))))
1520 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1521 (unless (eq (epg-context-protocol context) 'CMS)
1522 (epg-wait-for-status context '("BEGIN_SIGNING")))
1523 (when (epg-data-string plain)
1524 (if (eq (process-status (epg-context-process context)) 'run)
1525 (process-send-string (epg-context-process context)
1526 (epg-data-string plain)))
1527 (if (eq (process-status (epg-context-process context)) 'run)
1528 (process-send-eof (epg-context-process context)))))
1531 (defun epg-sign-file (context plain signature &optional mode)
1532 "Sign a file PLAIN and store the result to a file SIGNATURE.
1533 If SIGNATURE is nil, it returns the result as a string.
1534 If optional 3rd argument MODE is 'clear, it makes a clear text signature.
1535 If MODE is t or 'detached, it makes a detached signature.
1536 Otherwise, it makes a normal signature."
1540 (epg-context-set-output-file context signature)
1541 (epg-context-set-output-file context
1542 (epg-make-temp-file "epg-output")))
1543 (epg-start-sign context (epg-make-data-from-file plain) mode)
1544 (epg-wait-for-completion context)
1545 (unless (epg-context-result-for context 'sign)
1546 (if (epg-context-result-for context 'error)
1547 (error "Sign failed: %S"
1548 (epg-context-result-for context 'error))
1549 (error "Sign failed")))
1551 (epg-read-output context)))
1553 (epg-delete-output-file context))
1554 (epg-reset context)))
1557 (defun epg-sign-string (context plain &optional mode)
1558 "Sign a string PLAIN and return the output as string.
1559 If optional 3rd argument MODE is 'clear, it makes a clear text signature.
1560 If MODE is t or 'detached, it makes a detached signature.
1561 Otherwise, it makes a normal signature."
1564 (epg-context-set-output-file context
1565 (epg-make-temp-file "epg-output"))
1566 (epg-start-sign context (epg-make-data-from-string 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")))
1573 (epg-read-output context))
1574 (epg-delete-output-file context)
1575 (epg-reset context)))
1578 (defun epg-start-encrypt (context plain recipients
1579 &optional sign always-trust)
1580 "Initiate an encrypt operation on PLAIN.
1581 PLAIN is a data object.
1582 If RECIPIENTS is nil, it performs symmetric encryption.
1584 If you use this function, you will need to wait for the completion of
1585 `epg-gpg-program' by using `epg-wait-for-completion' and call
1586 `epg-reset' to clear a temporaly output file.
1587 If you are unsure, use synchronous version of this function
1588 `epg-encrypt-file' or `epg-encrypt-string' instead."
1589 (epg-context-set-result context nil)
1591 (append (if always-trust '("--always-trust"))
1592 (if recipients '("--encrypt") '("--symmetric"))
1596 (mapcar (lambda (signer)
1598 (epg-context-signers context)))))
1604 (car (epg-key-sub-key-list recipient)))))
1606 (if (epg-data-file plain)
1607 (list (epg-data-file plain)))))
1608 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1609 (unless (eq (epg-context-protocol context) 'CMS)
1611 (epg-wait-for-status context '("BEGIN_SIGNING"))
1612 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
1613 (when (epg-data-string plain)
1614 (if (eq (process-status (epg-context-process context)) 'run)
1615 (process-send-string (epg-context-process context)
1616 (epg-data-string plain)))
1617 (if (eq (process-status (epg-context-process context)) 'run)
1618 (process-send-eof (epg-context-process context)))))
1621 (defun epg-encrypt-file (context plain recipients
1622 cipher &optional sign always-trust)
1623 "Encrypt a file PLAIN and store the result to a file CIPHER.
1624 If CIPHER is nil, it returns the result as a string.
1625 If RECIPIENTS is nil, it performs symmetric encryption."
1629 (epg-context-set-output-file context cipher)
1630 (epg-context-set-output-file context
1631 (epg-make-temp-file "epg-output")))
1632 (epg-start-encrypt context (epg-make-data-from-file plain)
1633 recipients sign always-trust)
1634 (epg-wait-for-completion context)
1636 (not (epg-context-result-for context 'sign)))
1637 (if (epg-context-result-for context 'error)
1638 (error "Sign failed: %S"
1639 (epg-context-result-for context 'error))
1640 (error "Sign failed")))
1641 (if (epg-context-result-for context 'error)
1642 (error "Encrypt failed: %S"
1643 (epg-context-result-for context 'error)))
1645 (epg-read-output context)))
1647 (epg-delete-output-file context))
1648 (epg-reset context)))
1651 (defun epg-encrypt-string (context plain recipients
1652 &optional sign always-trust)
1653 "Encrypt a string PLAIN.
1654 If RECIPIENTS is nil, it performs symmetric encryption."
1657 (epg-context-set-output-file context
1658 (epg-make-temp-file "epg-output"))
1659 (epg-start-encrypt context (epg-make-data-from-string plain)
1660 recipients sign always-trust)
1661 (epg-wait-for-completion context)
1663 (not (epg-context-result-for context 'sign)))
1664 (if (epg-context-result-for context 'error)
1665 (error "Sign failed: %S"
1666 (epg-context-result-for context 'error))
1667 (error "Sign failed")))
1668 (if (epg-context-result-for context 'error)
1669 (error "Encrypt failed: %S"
1670 (epg-context-result-for context 'error)))
1671 (epg-read-output context))
1672 (epg-delete-output-file context)
1673 (epg-reset context)))
1676 (defun epg-start-export-keys (context keys)
1677 "Initiate an export keys operation.
1679 If you use this function, you will need to wait for the completion of
1680 `epg-gpg-program' by using `epg-wait-for-completion' and call
1681 `epg-reset' to clear a temporaly output file.
1682 If you are unsure, use synchronous version of this function
1683 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1684 (epg-context-set-result context nil)
1685 (epg-start context (cons "--export"
1689 (car (epg-key-sub-key-list key))))
1693 (defun epg-export-keys-to-file (context keys file)
1694 "Extract public KEYS."
1698 (epg-context-set-output-file context file)
1699 (epg-context-set-output-file context
1700 (epg-make-temp-file "epg-output")))
1701 (epg-start-export-keys context keys)
1702 (epg-wait-for-completion context)
1703 (if (epg-context-result-for context 'error)
1704 (error "Export keys failed: %S"
1705 (epg-context-result-for context 'error)))
1707 (epg-read-output context)))
1709 (epg-delete-output-file context))
1710 (epg-reset context)))
1713 (defun epg-export-keys-to-string (context keys)
1714 "Extract public KEYS and return them as a string."
1715 (epg-export-keys-to-file context keys nil))
1718 (defun epg-start-import-keys (context keys)
1719 "Initiate an import keys operation.
1720 KEYS is a data object.
1722 If you use this function, you will need to wait for the completion of
1723 `epg-gpg-program' by using `epg-wait-for-completion' and call
1724 `epg-reset' to clear a temporaly output file.
1725 If you are unsure, use synchronous version of this function
1726 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1727 (epg-context-set-result context nil)
1728 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
1729 (epg-start context (list "--import" (epg-data-file keys)))
1730 (when (epg-data-string keys)
1731 (if (eq (process-status (epg-context-process context)) 'run)
1732 (process-send-string (epg-context-process context)
1733 (epg-data-string keys)))
1734 (if (eq (process-status (epg-context-process context)) 'run)
1735 (process-send-eof (epg-context-process context)))))
1737 (defun epg-import-keys-1 (context keys)
1740 (epg-start-import-keys context keys)
1741 (epg-wait-for-completion context)
1742 (if (epg-context-result-for context 'error)
1743 (error "Import keys failed: %S"
1744 (epg-context-result-for context 'error)))
1745 (epg-read-output context))
1746 (epg-reset context)))
1749 (defun epg-import-keys-from-file (context keys)
1750 "Add keys from a file KEYS."
1751 (epg-import-keys-1 context (epg-make-data-from-file keys)))
1754 (defun epg-import-keys-from-string (context keys)
1755 "Add keys from a string KEYS."
1756 (epg-import-keys-1 context (epg-make-data-from-string keys)))
1759 (defun epg-start-delete-keys (context keys &optional allow-secret)
1760 "Initiate an delete keys operation.
1762 If you use this function, you will need to wait for the completion of
1763 `epg-gpg-program' by using `epg-wait-for-completion' and call
1764 `epg-reset' to clear a temporaly output file.
1765 If you are unsure, use synchronous version of this function
1766 `epg-delete-keys' instead."
1767 (epg-context-set-result context nil)
1768 (epg-start context (cons (if allow-secret
1769 "--delete-secret-key"
1774 (car (epg-key-sub-key-list key))))
1778 (defun epg-delete-keys (context keys &optional allow-secret)
1779 "Delete KEYS from the key ring."
1782 (epg-start-delete-keys context keys allow-secret)
1783 (epg-wait-for-completion context)
1784 (if (epg-context-result-for context 'error)
1785 (error "Delete keys failed: %S"
1786 (epg-context-result-for context 'error))))
1787 (epg-reset context)))
1790 (defun epg-start-sign-keys (context keys &optional local)
1791 "Initiate an sign keys operation.
1793 If you use this function, you will need to wait for the completion of
1794 `epg-gpg-program' by using `epg-wait-for-completion' and call
1795 `epg-reset' to clear a temporaly output file.
1796 If you are unsure, use synchronous version of this function
1797 `epg-sign-keys' instead."
1798 (epg-context-set-result context nil)
1799 (epg-start context (cons (if local
1805 (car (epg-key-sub-key-list key))))
1809 (defun epg-sign-keys (context keys &optional local)
1810 "Sign KEYS from the key ring."
1813 (epg-start-sign-keys context keys local)
1814 (epg-wait-for-completion context)
1815 (if (epg-context-result-for context 'error)
1816 (error "Sign keys failed: %S"
1817 (epg-context-result-for context 'error))))
1818 (epg-reset context)))
1821 (defun epg-start-generate-key (context parameters)
1822 "Initiate a key generation.
1823 PARAMETERS specifies parameters for the key.
1825 If you use this function, you will need to wait for the completion of
1826 `epg-gpg-program' by using `epg-wait-for-completion' and call
1827 `epg-reset' to clear a temporaly output file.
1828 If you are unsure, use synchronous version of this function
1829 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
1830 (epg-context-set-result context nil)
1831 (if (epg-data-file parameters)
1832 (epg-start context (list "--batch" "--genkey"
1833 (epg-data-file parameters)))
1834 (epg-start context '("--batch" "--genkey"))
1835 (if (eq (process-status (epg-context-process context)) 'run)
1836 (process-send-string (epg-context-process context)
1837 (epg-data-string parameters)))
1838 (if (eq (process-status (epg-context-process context)) 'run)
1839 (process-send-eof (epg-context-process context)))))
1842 (defun epg-generate-key-from-file (context parameters)
1843 "Generate a new key pair.
1844 PARAMETERS is a file which tells how to create the key."
1847 (epg-start-generate-key context (epg-make-data-from-file parameters))
1848 (epg-wait-for-completion context)
1849 (if (epg-context-result-for context 'error)
1850 (error "Generate key failed: %S"
1851 (epg-context-result-for context 'error))))
1852 (epg-reset context)))
1855 (defun epg-generate-key-from-string (context parameters)
1856 "Generate a new key pair.
1857 PARAMETERS is a string which tells how to create the key."
1860 (epg-start-generate-key context (epg-make-data-from-string parameters))
1861 (epg-wait-for-completion context)
1862 (if (epg-context-result-for context 'error)
1863 (error "Generate key failed: %S"
1864 (epg-context-result-for context 'error))))
1865 (epg-reset context)))
1867 (defun epg-decode-hexstring (string)
1869 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
1870 (setq string (replace-match "\\x\\&" t nil string)
1872 (car (read-from-string (concat "\"" string "\"")))))
1874 (defun epg-decode-quotedstring (string)
1876 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
1877 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
1879 (if (match-beginning 2)
1880 (setq string (replace-match "\\2" t nil string)
1882 (if (match-beginning 3)
1883 (setq string (replace-match "\\x\\3" t nil string)
1885 (setq string (replace-match "\\\\\\\\\\4" t nil string)
1886 index (+ index 3)))))
1887 (car (read-from-string (concat "\"" string "\"")))))
1889 (defun epg-dn-from-string (string)
1890 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
1891 The return value is an alist mapping from types to values."
1893 (length (length string))
1894 alist type value group)
1895 (while (< index length)
1896 (if (eq index (string-match "[ \t\n\r]*" string index))
1897 (setq index (match-end 0)))
1898 (if (eq index (string-match
1899 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
1901 (setq type (match-string 1 string)
1902 index (match-end 0))
1903 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
1905 (setq type (match-string 1 string)
1906 index (match-end 0))))
1908 (error "Invalid type"))
1909 (if (eq index (string-match
1910 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
1912 (setq index (match-end 0)
1913 value (epg-decode-quotedstring (match-string 0 string)))
1914 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
1915 (setq index (match-end 0)
1916 value (epg-decode-hexstring (match-string 1 string)))
1917 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
1919 (setq index (match-end 0)
1920 value (epg-decode-quotedstring (match-string 0 string))))))
1922 (if (stringp (car (car alist)))
1923 (setcar alist (list (cons type value) (car alist)))
1924 (setcar alist (cons (cons type value) (car alist))))
1925 (if (consp (car (car alist)))
1926 (setcar alist (nreverse (car alist))))
1927 (setq alist (cons (cons type value) alist)
1930 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
1931 (setq index (match-end 0)
1932 group (eq (aref string (match-beginning 1)) ?+))))
1935 (defun epg-decode-dn (alist)
1936 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
1937 Type names are resolved using `epg-dn-type-alist'."
1940 (if (stringp (car rdn))
1941 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
1943 (format "%s=%s" (cdr entry) (cdr rdn))
1944 (format "%s=%s" (car rdn) (cdr rdn))))
1945 (concat "(" (epg-decode-dn rdn) ")")))
1951 ;;; epg.el ends here