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.
32 (defcustom epg-gpg-program "gpg"
33 "The `gpg' executable."
37 (defcustom epg-gpgsm-program "gpgsm"
38 "The `gpgsm' executable."
42 (defconst epg-version-number "0.0.3")
44 (defvar epg-user-id nil
45 "GnuPG ID of your default identity.")
47 (defvar epg-user-id-alist nil
48 "An alist mapping from key ID to user ID.")
50 (defvar epg-read-point nil)
51 (defvar epg-process-filter-running nil)
52 (defvar epg-pending-status-list nil)
53 (defvar epg-key-id nil)
54 (defvar epg-context nil)
55 (defvar epg-debug nil)
56 (defvar epg-debug-buffer nil)
58 ;; from gnupg/include/cipher.h
59 (defconst epg-cipher-algorithm-alist
71 ;; from gnupg/include/cipher.h
72 (defconst epg-pubkey-algorithm-alist
80 ;; from gnupg/include/cipher.h
81 (defconst epg-digest-algorithm-alist
89 ;; from gnupg/include/cipher.h
90 (defconst epg-compress-algorithm-alist
96 (defconst epg-invalid-recipients-reason-alist
97 '((0 . "No specific reason given")
99 (2 . "Ambigious specification")
100 (3 . "Wrong key usage")
105 (8 . "Policy mismatch")
106 (9 . "Not a secret key")
107 (10 . "Key not trusted")))
109 (defconst epg-delete-problem-reason-alist
110 '((1 . "No such key")
111 (2 . "Must delete secret key first")
112 (3 . "Ambigious specification")))
114 (defconst epg-import-ok-reason-alist
115 '((0 . "Not actually changed")
116 (1 . "Entirely new key")
118 (4 . "New signatures")
120 (16 . "Contains private key")))
122 (defconst epg-import-problem-reason-alist
123 '((0 . "No specific reason given")
124 (1 . "Invalid Certificate")
125 (2 . "Issuer Certificate missing")
126 (3 . "Certificate Chain too long")
127 (4 . "Error storing certificate")))
129 (defconst epg-no-data-reason-alist
130 '((1 . "No armored data")
131 (2 . "Expected a packet but did not found one")
132 (3 . "Invalid packet found, this may indicate a non OpenPGP message")
133 (4 . "Signature expected but not found")))
135 (defconst epg-unexpected-reason-alist nil)
137 (defvar epg-key-validity-alist
150 (defvar epg-key-capablity-alist
154 (?a . authentication)))
156 (defvar epg-new-signature-type-alist
161 (defvar epg-dn-type-alist
162 '(("1.2.840.113549.1.9.1" . "EMail")
166 ("0.2.262.1.10.7.20" . "NameDistinguisher")
167 ("2.5.4.16" . "ADDR")
170 ("2.5.4.17" . "PostalCode")
171 ("2.5.4.65" . "Pseudo")
172 ("2.5.4.5" . "SerialNumber")))
174 (defvar epg-prompt-alist nil)
176 (defun epg-make-data-from-file (file)
177 "Make a data object from FILE."
178 (cons 'epg-data (vector file nil)))
180 (defun epg-make-data-from-string (string)
181 "Make a data object from STRING."
182 (cons 'epg-data (vector nil string)))
184 (defun epg-data-file (data)
185 "Return the file of DATA."
186 (unless (eq (car data) 'epg-data)
187 (signal 'wrong-type-argument (list 'epg-data-p data)))
190 (defun epg-data-string (data)
191 "Return the string of DATA."
192 (unless (eq (car data) 'epg-data)
193 (signal 'wrong-type-argument (list 'epg-data-p data)))
196 (defun epg-make-context (&optional protocol armor textmode include-certs
197 cipher-algorithm digest-algorithm
199 "Return a context object."
201 (vector (or protocol 'OpenPGP) armor textmode include-certs
202 cipher-algorithm digest-algorithm compress-algorithm
203 #'epg-passphrase-callback-function
204 #'epg-progress-callback-function
205 nil nil nil nil nil)))
207 (defun epg-context-protocol (context)
208 "Return the protocol used within CONTEXT."
209 (unless (eq (car context) 'epg-context)
210 (signal 'wrong-type-argument (list 'epg-context-p context)))
211 (aref (cdr context) 0))
213 (defun epg-context-armor (context)
214 "Return t if the output shouled be ASCII armored in CONTEXT."
215 (unless (eq (car context) 'epg-context)
216 (signal 'wrong-type-argument (list 'epg-context-p context)))
217 (aref (cdr context) 1))
219 (defun epg-context-textmode (context)
220 "Return t if canonical text mode should be used in CONTEXT."
221 (unless (eq (car context) 'epg-context)
222 (signal 'wrong-type-argument (list 'epg-context-p context)))
223 (aref (cdr context) 2))
225 (defun epg-context-include-certs (context)
226 "Return how many certificates should be included in an S/MIME signed
228 (unless (eq (car context) 'epg-context)
229 (signal 'wrong-type-argument (list 'epg-context-p context)))
230 (aref (cdr context) 3))
232 (defun epg-context-cipher-algorithm (context)
233 "Return the cipher algorithm in CONTEXT."
234 (unless (eq (car context) 'epg-context)
235 (signal 'wrong-type-argument (list 'epg-context-p context)))
236 (aref (cdr context) 4))
238 (defun epg-context-digest-algorithm (context)
239 "Return the digest algorithm in CONTEXT."
240 (unless (eq (car context) 'epg-context)
241 (signal 'wrong-type-argument (list 'epg-context-p context)))
242 (aref (cdr context) 5))
244 (defun epg-context-compress-algorithm (context)
245 "Return the compress algorithm in CONTEXT."
246 (unless (eq (car context) 'epg-context)
247 (signal 'wrong-type-argument (list 'epg-context-p context)))
248 (aref (cdr context) 6))
250 (defun epg-context-passphrase-callback (context)
251 "Return the function used to query passphrase."
252 (unless (eq (car context) 'epg-context)
253 (signal 'wrong-type-argument (list 'epg-context-p context)))
254 (aref (cdr context) 7))
256 (defun epg-context-progress-callback (context)
257 "Return the function which handles progress update."
258 (unless (eq (car context) 'epg-context)
259 (signal 'wrong-type-argument (list 'epg-context-p context)))
260 (aref (cdr context) 8))
262 (defun epg-context-signers (context)
263 "Return the list of key-id for singning."
264 (unless (eq (car context) 'epg-context)
265 (signal 'wrong-type-argument (list 'epg-context-p context)))
266 (aref (cdr context) 9))
268 (defun epg-context-process (context)
269 "Return the process object of `epg-gpg-program'.
270 This function is for internal use only."
271 (unless (eq (car context) 'epg-context)
272 (signal 'wrong-type-argument (list 'epg-context-p context)))
273 (aref (cdr context) 10))
275 (defun epg-context-output-file (context)
276 "Return the output file of `epg-gpg-program'.
277 This function is for internal use only."
278 (unless (eq (car context) 'epg-context)
279 (signal 'wrong-type-argument (list 'epg-context-p context)))
280 (aref (cdr context) 11))
282 (defun epg-context-result (context)
283 "Return the result of the previous cryptographic operation."
284 (unless (eq (car context) 'epg-context)
285 (signal 'wrong-type-argument (list 'epg-context-p context)))
286 (aref (cdr context) 12))
288 (defun epg-context-operation (context)
289 "Return the name of the current cryptographic operation."
290 (unless (eq (car context) 'epg-context)
291 (signal 'wrong-type-argument (list 'epg-context-p context)))
292 (aref (cdr context) 13))
294 (defun epg-context-set-protocol (context protocol)
295 "Set the protocol used within CONTEXT."
296 (unless (eq (car context) 'epg-context)
297 (signal 'wrong-type-argument (list 'epg-context-p context)))
298 (aset (cdr context) 0 protocol))
300 (defun epg-context-set-armor (context armor)
301 "Specify if the output shouled be ASCII armored in CONTEXT."
302 (unless (eq (car context) 'epg-context)
303 (signal 'wrong-type-argument (list 'epg-context-p context)))
304 (aset (cdr context) 1 armor))
306 (defun epg-context-set-textmode (context textmode)
307 "Specify if canonical text mode should be used in CONTEXT."
308 (unless (eq (car context) 'epg-context)
309 (signal 'wrong-type-argument (list 'epg-context-p context)))
310 (aset (cdr context) 2 textmode))
312 (defun epg-context-set-include-certs (context include-certs)
313 "Set how many certificates should be included in an S/MIME signed message."
314 (unless (eq (car context) 'epg-context)
315 (signal 'wrong-type-argument (list 'epg-context-p context)))
316 (aset (cdr context) 3 include-certs))
318 (defun epg-context-set-cipher-algorithm (context cipher-algorithm)
319 "Set the cipher algorithm in CONTEXT."
320 (unless (eq (car context) 'epg-context)
321 (signal 'wrong-type-argument (list 'epg-context-p context)))
322 (aset (cdr context) 4 cipher-algorithm))
324 (defun epg-context-set-digest-algorithm (context digest-algorithm)
325 "Set the digest algorithm in CONTEXT."
326 (unless (eq (car context) 'epg-context)
327 (signal 'wrong-type-argument (list 'epg-context-p context)))
328 (aset (cdr context) 5 digest-algorithm))
330 (defun epg-context-set-compress-algorithm (context compress-algorithm)
331 "Set the compress algorithm in CONTEXT."
332 (unless (eq (car context) 'epg-context)
333 (signal 'wrong-type-argument (list 'epg-context-p context)))
334 (aset (cdr context) 6 compress-algorithm))
336 (defun epg-context-set-passphrase-callback (context
338 "Set the function used to query passphrase."
339 (unless (eq (car context) 'epg-context)
340 (signal 'wrong-type-argument (list 'epg-context-p context)))
341 (aset (cdr context) 7 passphrase-callback))
343 (defun epg-context-set-progress-callback (context progress-callback)
344 "Set the function which handles progress update."
345 (unless (eq (car context) 'epg-context)
346 (signal 'wrong-type-argument (list 'epg-context-p context)))
347 (aset (cdr context) 8 progress-callback))
349 (defun epg-context-set-signers (context signers)
350 "Set the list of key-id for singning."
351 (unless (eq (car context) 'epg-context)
352 (signal 'wrong-type-argument (list 'epg-context-p context)))
353 (aset (cdr context) 9 signers))
355 (defun epg-context-set-process (context process)
356 "Set the process object of `epg-gpg-program'.
357 This function is for internal use only."
358 (unless (eq (car context) 'epg-context)
359 (signal 'wrong-type-argument (list 'epg-context-p context)))
360 (aset (cdr context) 10 process))
362 (defun epg-context-set-output-file (context output-file)
363 "Set the output file of `epg-gpg-program'.
364 This function is for internal use only."
365 (unless (eq (car context) 'epg-context)
366 (signal 'wrong-type-argument (list 'epg-context-p context)))
367 (aset (cdr context) 11 output-file))
369 (defun epg-context-set-result (context result)
370 "Set the result of the previous cryptographic operation."
371 (unless (eq (car context) 'epg-context)
372 (signal 'wrong-type-argument (list 'epg-context-p context)))
373 (aset (cdr context) 12 result))
375 (defun epg-context-set-operation (context operation)
376 "Set the name of the current cryptographic operation."
377 (unless (eq (car context) 'epg-context)
378 (signal 'wrong-type-argument (list 'epg-context-p context)))
379 (aset (cdr context) 13 operation))
381 (defun epg-make-signature (status &optional key-id)
382 "Return a signature object."
383 (cons 'epg-signature (vector status key-id nil nil nil nil nil nil nil nil)))
385 (defun epg-signature-status (signature)
386 "Return the status code of SIGNATURE."
387 (unless (eq (car signature) 'epg-signature)
388 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
389 (aref (cdr signature) 0))
391 (defun epg-signature-key-id (signature)
392 "Return the key-id of SIGNATURE."
393 (unless (eq (car signature) 'epg-signature)
394 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
395 (aref (cdr signature) 1))
397 (defun epg-signature-validity (signature)
398 "Return the validity of SIGNATURE."
399 (unless (eq (car signature) 'epg-signature)
400 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
401 (aref (cdr signature) 2))
403 (defun epg-signature-fingerprint (signature)
404 "Return the fingerprint of SIGNATURE."
405 (unless (eq (car signature) 'epg-signature)
406 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
407 (aref (cdr signature) 3))
409 (defun epg-signature-creation-time (signature)
410 "Return the creation time of SIGNATURE."
411 (unless (eq (car signature) 'epg-signature)
412 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
413 (aref (cdr signature) 4))
415 (defun epg-signature-expiration-time (signature)
416 "Return the expiration time of SIGNATURE."
417 (unless (eq (car signature) 'epg-signature)
418 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
419 (aref (cdr signature) 5))
421 (defun epg-signature-pubkey-algorithm (signature)
422 "Return the public key algorithm of SIGNATURE."
423 (unless (eq (car signature) 'epg-signature)
424 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
425 (aref (cdr signature) 6))
427 (defun epg-signature-digest-algorithm (signature)
428 "Return the digest algorithm of SIGNATURE."
429 (unless (eq (car signature) 'epg-signature)
430 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
431 (aref (cdr signature) 7))
433 (defun epg-signature-class (signature)
434 "Return the class of SIGNATURE."
435 (unless (eq (car signature) 'epg-signature)
436 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
437 (aref (cdr signature) 8))
439 (defun epg-signature-version (signature)
440 "Return the version of SIGNATURE."
441 (unless (eq (car signature) 'epg-signature)
442 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
443 (aref (cdr signature) 9))
445 (defun epg-signature-set-status (signature status)
446 "Set the status code of SIGNATURE."
447 (unless (eq (car signature) 'epg-signature)
448 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
449 (aset (cdr signature) 0 status))
451 (defun epg-signature-set-key-id (signature key-id)
452 "Set the key-id of SIGNATURE."
453 (unless (eq (car signature) 'epg-signature)
454 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
455 (aset (cdr signature) 1 key-id))
457 (defun epg-signature-set-validity (signature validity)
458 "Set the validity of SIGNATURE."
459 (unless (eq (car signature) 'epg-signature)
460 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
461 (aset (cdr signature) 2 validity))
463 (defun epg-signature-set-fingerprint (signature fingerprint)
464 "Set the fingerprint of SIGNATURE."
465 (unless (eq (car signature) 'epg-signature)
466 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
467 (aset (cdr signature) 3 fingerprint))
469 (defun epg-signature-set-creation-time (signature creation-time)
470 "Set the creation time of SIGNATURE."
471 (unless (eq (car signature) 'epg-signature)
472 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
473 (aset (cdr signature) 4 creation-time))
475 (defun epg-signature-set-expiration-time (signature expiration-time)
476 "Set the expiration time of SIGNATURE."
477 (unless (eq (car signature) 'epg-signature)
478 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
479 (aset (cdr signature) 5 expiration-time))
481 (defun epg-signature-set-pubkey-algorithm (signature pubkey-algorithm)
482 "Set the public key algorithm of SIGNATURE."
483 (unless (eq (car signature) 'epg-signature)
484 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
485 (aset (cdr signature) 6 pubkey-algorithm))
487 (defun epg-signature-set-digest-algorithm (signature digest-algorithm)
488 "Set the digest algorithm of SIGNATURE."
489 (unless (eq (car signature) 'epg-signature)
490 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
491 (aset (cdr signature) 7 digest-algorithm))
493 (defun epg-signature-set-class (signature class)
494 "Set the class of SIGNATURE."
495 (unless (eq (car signature) 'epg-signature)
496 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
497 (aset (cdr signature) 8 class))
499 (defun epg-signature-set-version (signature version)
500 "Set the version of SIGNATURE."
501 (unless (eq (car signature) 'epg-signature)
502 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
503 (aset (cdr signature) 9 version))
505 (defun epg-make-new-signature (type pubkey-algorithm digest-algorithm
506 class creation-time fingerprint)
507 "Return a new signature object."
508 (cons 'epg-new-signature (vector type pubkey-algorithm digest-algorithm
509 class creation-time fingerprint)))
511 (defun epg-new-signature-type (new-signature)
512 "Return the type of NEW-SIGNATURE."
513 (unless (eq (car new-signature) 'epg-new-signature)
514 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
515 (aref (cdr new-signature) 0))
517 (defun epg-new-signature-pubkey-algorithm (new-signature)
518 "Return the public key algorithm of NEW-SIGNATURE."
519 (unless (eq (car new-signature) 'epg-new-signature)
520 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
521 (aref (cdr new-signature) 1))
523 (defun epg-new-signature-digest-algorithm (new-signature)
524 "Return the digest algorithm of NEW-SIGNATURE."
525 (unless (eq (car new-signature) 'epg-new-signature)
526 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
527 (aref (cdr new-signature) 2))
529 (defun epg-new-signature-class (new-signature)
530 "Return the class of NEW-SIGNATURE."
531 (unless (eq (car new-signature) 'epg-new-signature)
532 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
533 (aref (cdr new-signature) 3))
535 (defun epg-new-signature-creation-time (new-signature)
536 "Return the creation time of NEW-SIGNATURE."
537 (unless (eq (car new-signature) 'epg-new-signature)
538 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
539 (aref (cdr new-signature) 4))
541 (defun epg-new-signature-fingerprint (new-signature)
542 "Return the fingerprint of NEW-SIGNATURE."
543 (unless (eq (car new-signature) 'epg-new-signature)
544 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
545 (aref (cdr new-signature) 5))
547 (defun epg-make-key (owner-trust)
548 "Return a key object."
549 (cons 'epg-key (vector owner-trust nil nil)))
551 (defun epg-key-owner-trust (key)
552 "Return the owner trust of KEY."
553 (unless (eq (car key) 'epg-key)
554 (signal 'wrong-type-argument (list 'epg-key-p key)))
557 (defun epg-key-sub-key-list (key)
558 "Return the sub key list of KEY."
559 (unless (eq (car key) 'epg-key)
560 (signal 'wrong-type-argument (list 'epg-key-p key)))
563 (defun epg-key-user-id-list (key)
564 "Return the user ID list of KEY."
565 (unless (eq (car key) 'epg-key)
566 (signal 'wrong-type-argument (list 'epg-key-p key)))
569 (defun epg-key-set-sub-key-list (key sub-key-list)
570 "Set the sub key list of KEY."
571 (unless (eq (car key) 'epg-key)
572 (signal 'wrong-type-argument (list 'epg-key-p key)))
573 (aset (cdr key) 1 sub-key-list))
575 (defun epg-key-set-user-id-list (key user-id-list)
576 "Set the user ID list of KEY."
577 (unless (eq (car key) 'epg-key)
578 (signal 'wrong-type-argument (list 'epg-key-p key)))
579 (aset (cdr key) 2 user-id-list))
581 (defun epg-make-sub-key (validity capability secret-p algorithm length id
582 creation-time expiration-time)
583 "Return a sub key object."
585 (vector validity capability secret-p algorithm length id creation-time
586 expiration-time nil)))
588 (defun epg-sub-key-validity (sub-key)
589 "Return the validity of SUB-KEY."
590 (unless (eq (car sub-key) 'epg-sub-key)
591 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
592 (aref (cdr sub-key) 0))
594 (defun epg-sub-key-capability (sub-key)
595 "Return the capability of SUB-KEY."
596 (unless (eq (car sub-key) 'epg-sub-key)
597 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
598 (aref (cdr sub-key) 1))
600 (defun epg-sub-key-secret-p (sub-key)
601 "Return non-nil if SUB-KEY is a secret key."
602 (unless (eq (car sub-key) 'epg-sub-key)
603 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
604 (aref (cdr sub-key) 2))
606 (defun epg-sub-key-algorithm (sub-key)
607 "Return the algorithm of SUB-KEY."
608 (unless (eq (car sub-key) 'epg-sub-key)
609 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
610 (aref (cdr sub-key) 3))
612 (defun epg-sub-key-length (sub-key)
613 "Return the length of SUB-KEY."
614 (unless (eq (car sub-key) 'epg-sub-key)
615 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
616 (aref (cdr sub-key) 4))
618 (defun epg-sub-key-id (sub-key)
619 "Return the ID of SUB-KEY."
620 (unless (eq (car sub-key) 'epg-sub-key)
621 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
622 (aref (cdr sub-key) 5))
624 (defun epg-sub-key-creation-time (sub-key)
625 "Return the creation time of SUB-KEY."
626 (unless (eq (car sub-key) 'epg-sub-key)
627 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
628 (aref (cdr sub-key) 6))
630 (defun epg-sub-key-expiration-time (sub-key)
631 "Return the expiration time of SUB-KEY."
632 (unless (eq (car sub-key) 'epg-sub-key)
633 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
634 (aref (cdr sub-key) 7))
636 (defun epg-sub-key-fingerprint (sub-key)
637 "Return the fingerprint of SUB-KEY."
638 (unless (eq (car sub-key) 'epg-sub-key)
639 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
640 (aref (cdr sub-key) 8))
642 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
643 "Set the fingerprint of SUB-KEY.
644 This function is for internal use only."
645 (unless (eq (car sub-key) 'epg-sub-key)
646 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
647 (aset (cdr sub-key) 8 fingerprint))
649 (defun epg-make-user-id (validity string)
650 "Return a user ID object."
651 (cons 'epg-user-id (vector validity string nil)))
653 (defun epg-user-id-validity (user-id)
654 "Return the validity of USER-ID."
655 (unless (eq (car user-id) 'epg-user-id)
656 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
657 (aref (cdr user-id) 0))
659 (defun epg-user-id-string (user-id)
660 "Return the name of USER-ID."
661 (unless (eq (car user-id) 'epg-user-id)
662 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
663 (aref (cdr user-id) 1))
665 (defun epg-user-id-signature-list (user-id)
666 "Return the signature list of USER-ID."
667 (unless (eq (car user-id) 'epg-user-id)
668 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
669 (aref (cdr user-id) 2))
671 (defun epg-user-id-set-signature-list (user-id signature-list)
672 "Set the signature list of USER-ID."
673 (unless (eq (car user-id) 'epg-user-id)
674 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
675 (aset (cdr user-id) 2 signature-list))
677 (defun epg-make-key-signature (validity pubkey-algorithm key-id creation-time
678 expiration-time user-id class
680 "Return a key signature object."
681 (cons 'epg-key-signature
682 (vector validity pubkey-algorithm key-id creation-time expiration-time
683 user-id class exportable-p)))
685 (defun epg-key-signature-validity (key-signature)
686 "Return the validity of KEY-SIGNATURE."
687 (unless (eq (car key-signature) 'epg-key-signature)
688 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
689 (aref (cdr key-signature) 0))
691 (defun epg-key-signature-pubkey-algorithm (key-signature)
692 "Return the public key algorithm of KEY-SIGNATURE."
693 (unless (eq (car key-signature) 'epg-key-signature)
694 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
695 (aref (cdr key-signature) 1))
697 (defun epg-key-signature-key-id (key-signature)
698 "Return the key-id of KEY-SIGNATURE."
699 (unless (eq (car key-signature) 'epg-key-signature)
700 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
701 (aref (cdr key-signature) 2))
703 (defun epg-key-signature-creation-time (key-signature)
704 "Return the creation time of KEY-SIGNATURE."
705 (unless (eq (car key-signature) 'epg-key-signature)
706 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
707 (aref (cdr key-signature) 3))
709 (defun epg-key-signature-expiration-time (key-signature)
710 "Return the expiration time of KEY-SIGNATURE."
711 (unless (eq (car key-signature) 'epg-key-signature)
712 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
713 (aref (cdr key-signature) 4))
715 (defun epg-key-signature-user-id (key-signature)
716 "Return the user-id of KEY-SIGNATURE."
717 (unless (eq (car key-signature) 'epg-key-signature)
718 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
719 (aref (cdr key-signature) 5))
721 (defun epg-key-signature-class (key-signature)
722 "Return the class of KEY-SIGNATURE."
723 (unless (eq (car key-signature) 'epg-key-signature)
724 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
725 (aref (cdr key-signature) 6))
727 (defun epg-key-signature-exportable-p (key-signature)
728 "Return t if KEY-SIGNATURE is exportable."
729 (unless (eq (car key-signature) 'epg-key-signature)
730 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
731 (aref (cdr key-signature) 7))
733 (defun epg-context-result-for (context name)
734 "Return the result of CONTEXT associated with NAME."
735 (cdr (assq name (epg-context-result context))))
737 (defun epg-context-set-result-for (context name value)
738 "Set the result of CONTEXT associated with NAME to VALUE."
739 (let* ((result (epg-context-result context))
740 (entry (assq name result)))
743 (epg-context-set-result context (cons (cons name value) result)))))
745 (defun epg-signature-to-string (signature)
746 "Convert SIGNATURE to a human readable string."
747 (let ((user-id (cdr (assoc (epg-signature-key-id signature)
748 epg-user-id-alist))))
750 (cond ((eq (epg-signature-status signature) 'good)
751 "Good signature from ")
752 ((eq (epg-signature-status signature) 'bad)
753 "Bad signature from ")
754 ((eq (epg-signature-status signature) 'expired)
755 "Expired signature from ")
756 ((eq (epg-signature-status signature) 'expired-key)
757 "Signature made by expired key ")
758 ((eq (epg-signature-status signature) 'revoked-key)
759 "Signature made by revoked key ")
760 ((eq (epg-signature-status signature) 'no-pubkey)
761 "No public key for "))
762 (epg-signature-key-id signature)
765 (if (stringp user-id)
767 (epg-decode-dn user-id)))
769 (if (epg-signature-validity signature)
770 (format " (trust %s)" (epg-signature-validity signature))
773 (defun epg-verify-result-to-string (verify-result)
774 "Convert VERIFY-RESULT to a human readable string."
775 (mapconcat #'epg-signature-to-string verify-result "\n"))
777 (defun epg-new-signature-to-string (new-signature)
778 "Convert NEW-SIGNATURE to a human readable string."
780 (cond ((eq (epg-new-signature-type new-signature) 'detached)
781 "Detached signature ")
782 ((eq (epg-new-signature-type new-signature) 'clear)
783 "Clear text signature ")
786 (cdr (assq (epg-new-signature-pubkey-algorithm new-signature)
787 epg-pubkey-algorithm-alist))
789 (cdr (assq (epg-new-signature-digest-algorithm new-signature)
790 epg-digest-algorithm-alist))
792 (format "%02X " (epg-new-signature-class new-signature))
793 (epg-new-signature-fingerprint new-signature)))
795 (defun epg--start (context args)
796 "Start `epg-gpg-program' in a subprocess with given ARGS."
797 (if (and (epg-context-process context)
798 (eq (process-status (epg-context-process context)) 'run))
799 (error "%s is already running in this context"
800 (if (eq (epg-context-protocol context) 'CMS)
803 (let* ((args (append (list "--no-tty"
806 (unless (eq (epg-context-protocol context) 'CMS)
807 (list "--command-fd" "0"))
808 (if (epg-context-armor context) '("--armor"))
809 (if (epg-context-textmode context) '("--textmode"))
810 (if (epg-context-output-file context)
811 (list "--output" (epg-context-output-file context)))
813 (coding-system-for-write 'binary)
814 process-connection-type
815 (orig-mode (default-file-modes))
816 (buffer (generate-new-buffer " *epg*"))
820 (unless epg-debug-buffer
821 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
822 (set-buffer epg-debug-buffer)
823 (goto-char (point-max))
824 (insert (format "%s %s\n"
825 (if (eq (epg-context-protocol context) 'CMS)
828 (mapconcat #'identity args " ")))))
829 (with-current-buffer buffer
830 (make-local-variable 'epg-read-point)
831 (setq epg-read-point (point-min))
832 (make-local-variable 'epg-process-filter-running)
833 (setq epg-process-filter-running nil)
834 (make-local-variable 'epg-pending-status-list)
835 (setq epg-pending-status-list nil)
836 (make-local-variable 'epg-key-id)
837 (setq epg-key-id nil)
838 (make-local-variable 'epg-context)
839 (setq epg-context context))
842 (set-default-file-modes 448)
844 (apply #'start-process "epg" buffer
845 (if (eq (epg-context-protocol context) 'CMS)
849 (set-default-file-modes orig-mode))
850 (set-process-filter process #'epg--process-filter)
851 (epg-context-set-process context process)))
853 (defun epg--process-filter (process input)
856 (unless epg-debug-buffer
857 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
858 (set-buffer epg-debug-buffer)
859 (goto-char (point-max))
861 (if (buffer-live-p (process-buffer process))
863 (set-buffer (process-buffer process))
864 (goto-char (point-max))
866 (unless epg-process-filter-running
869 (setq epg-process-filter-running t)
870 (goto-char epg-read-point)
872 (while (looking-at ".*\n") ;the input line finished
873 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
874 (let* ((status (match-string 1))
875 (string (match-string 2))
876 (symbol (intern-soft (concat "epg--status-"
878 (if (member status epg-pending-status-list)
879 (setq epg-pending-status-list nil))
882 (funcall symbol epg-context string))))
884 (setq epg-read-point (point))))
885 (setq epg-process-filter-running nil))))))
887 (defun epg-read-output (context)
888 "Read the output file CONTEXT and return the content as a string."
890 (if (fboundp 'set-buffer-multibyte)
891 (set-buffer-multibyte nil))
892 (if (file-exists-p (epg-context-output-file context))
893 (let ((coding-system-for-read 'binary))
894 (insert-file-contents (epg-context-output-file context))
897 (defun epg-wait-for-status (context status-list)
898 "Wait until one of elements in STATUS-LIST arrives."
899 (with-current-buffer (process-buffer (epg-context-process context))
900 (setq epg-pending-status-list status-list)
901 (while (and (eq (process-status (epg-context-process context)) 'run)
902 epg-pending-status-list)
903 (accept-process-output (epg-context-process context) 1))))
905 (defun epg-wait-for-completion (context)
906 "Wait until the `epg-gpg-program' process completes."
907 (while (eq (process-status (epg-context-process context)) 'run)
908 (accept-process-output (epg-context-process context) 1)))
910 (defun epg-reset (context)
912 (if (and (epg-context-process context)
913 (buffer-live-p (process-buffer (epg-context-process context))))
914 (kill-buffer (process-buffer (epg-context-process context))))
915 (epg-context-set-process context nil))
917 (defun epg-delete-output-file (context)
918 "Delete the output file of CONTEXT."
919 (if (and (epg-context-output-file context)
920 (file-exists-p (epg-context-output-file context)))
921 (delete-file (epg-context-output-file context))))
923 (defun epg--status-USERID_HINT (context string)
924 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
925 (let* ((key-id (match-string 1 string))
926 (user-id (match-string 2 string))
927 (entry (assoc key-id epg-user-id-alist)))
929 (setcdr entry user-id)
930 (setq epg-user-id-alist (cons (cons key-id user-id)
931 epg-user-id-alist))))))
933 (defun epg--status-NEED_PASSPHRASE (context string)
934 (if (string-match "\\`\\([^ ]+\\)" string)
935 (setq epg-key-id (match-string 1 string))))
937 (defun epg--status-NEED_PASSPHRASE_SYM (context string)
938 (setq epg-key-id 'SYM))
940 (defun epg--status-NEED_PASSPHRASE_PIN (context string)
941 (setq epg-key-id 'PIN))
943 (defun epg--status-GET_HIDDEN (context string)
945 (string-match "\\`passphrase\\." string))
948 passphrase-with-new-line)
954 (if (consp (epg-context-passphrase-callback context))
955 (car (epg-context-passphrase-callback context))
956 (epg-context-passphrase-callback context))
959 (if (consp (epg-context-passphrase-callback context))
960 (cdr (epg-context-passphrase-callback context)))))
962 (setq passphrase-with-new-line (concat passphrase "\n"))
963 (fillarray passphrase 0)
964 (setq passphrase nil)
965 (process-send-string (epg-context-process context)
966 passphrase-with-new-line)))
968 (epg-context-set-result-for
971 (epg-context-result-for context 'error)))
972 (delete-process (epg-context-process context))))
974 (fillarray passphrase 0))
975 (if passphrase-with-new-line
976 (fillarray passphrase-with-new-line 0))))))
978 (defun epg--status-GET_BOOL (context string)
979 (let ((entry (assoc string epg-prompt-alist))
982 (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
983 (process-send-string (epg-context-process context) "y\n")
984 (process-send-string (epg-context-process context) "n\n"))
986 (epg-context-set-result-for
989 (epg-context-result-for context 'error)))
990 (delete-process (epg-context-process context))))))
992 (defun epg--status-GET_LINE (context string)
993 (let ((entry (assoc string epg-prompt-alist))
996 (process-send-string (epg-context-process context)
1000 (concat string ": ")))
1003 (epg-context-set-result-for
1006 (epg-context-result-for context 'error)))
1007 (delete-process (epg-context-process context))))))
1009 (defun epg--status-*SIG (context status string)
1010 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1011 (let* ((key-id (match-string 1 string))
1012 (user-id (match-string 2 string))
1013 (entry (assoc key-id epg-user-id-alist)))
1014 (epg-context-set-result-for
1017 (cons (epg-make-signature status key-id)
1018 (epg-context-result-for context 'verify)))
1019 (if (eq (epg-context-protocol context) 'CMS)
1021 (setq user-id (epg-dn-from-string user-id))
1024 (setcdr entry user-id)
1025 (setq epg-user-id-alist
1026 (cons (cons key-id user-id) epg-user-id-alist))))
1027 (epg-context-set-result-for
1030 (cons (epg-make-signature status)
1031 (epg-context-result-for context 'verify)))))
1033 (defun epg--status-GOODSIG (context string)
1034 (epg--status-*SIG context 'good string))
1036 (defun epg--status-EXPSIG (context string)
1037 (epg--status-*SIG context 'expired string))
1039 (defun epg--status-EXPKEYSIG (context string)
1040 (epg--status-*SIG context 'expired-key string))
1042 (defun epg--status-REVKEYSIG (context string)
1043 (epg--status-*SIG context 'revoked-key string))
1045 (defun epg--status-BADSIG (context string)
1046 (epg--status-*SIG context 'bad string))
1048 (defun epg--status-NO_PUBKEY (context string)
1049 (let ((signature (car (epg-context-result-for context 'verify))))
1051 (eq (epg-signature-status signature) 'error)
1052 (equal (epg-signature-key-id signature) string))
1053 (epg-signature-set-status signature 'no-pubkey))))
1055 (defun epg--time-from-seconds (seconds)
1056 (let ((number-seconds (string-to-number (concat seconds ".0"))))
1057 (cons (floor (/ number-seconds 65536))
1058 (floor (mod number-seconds 65536)))))
1060 (defun epg--status-ERRSIG (context string)
1061 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1062 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
1064 (let ((signature (epg-make-signature 'error)))
1065 (epg-context-set-result-for
1069 (epg-context-result-for context 'verify)))
1070 (epg-signature-set-key-id
1072 (match-string 1 string))
1073 (epg-signature-set-pubkey-algorithm
1075 (string-to-number (match-string 2 string)))
1076 (epg-signature-set-digest-algorithm
1078 (string-to-number (match-string 3 string)))
1079 (epg-signature-set-class
1081 (string-to-number (match-string 4 string) 16))
1082 (epg-signature-set-creation-time
1084 (epg--time-from-seconds (match-string 5 string))))))
1086 (defun epg--status-VALIDSIG (context string)
1087 (let ((signature (car (epg-context-result-for context 'verify))))
1088 (when (and signature
1089 (eq (epg-signature-status signature) 'good)
1090 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
1091 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
1094 (epg-signature-set-fingerprint
1096 (match-string 1 string))
1097 (epg-signature-set-creation-time
1099 (epg--time-from-seconds (match-string 2 string)))
1100 (epg-signature-set-expiration-time
1102 (epg--time-from-seconds (match-string 3 string)))
1103 (epg-signature-set-version
1105 (string-to-number (match-string 4 string)))
1106 (epg-signature-set-pubkey-algorithm
1108 (string-to-number (match-string 5 string)))
1109 (epg-signature-set-digest-algorithm
1111 (string-to-number (match-string 6 string)))
1112 (epg-signature-set-class
1114 (string-to-number (match-string 7 string) 16)))))
1116 (defun epg--status-TRUST_UNDEFINED (context string)
1117 (let ((signature (car (epg-context-result-for context 'verify))))
1119 (eq (epg-signature-status signature) 'good))
1120 (epg-signature-set-validity signature 'undefined))))
1122 (defun epg--status-TRUST_NEVER (context string)
1123 (let ((signature (car (epg-context-result-for context 'verify))))
1125 (eq (epg-signature-status signature) 'good))
1126 (epg-signature-set-validity signature 'never))))
1128 (defun epg--status-TRUST_MARGINAL (context string)
1129 (let ((signature (car (epg-context-result-for context 'verify))))
1131 (eq (epg-signature-status signature) 'marginal))
1132 (epg-signature-set-validity signature 'marginal))))
1134 (defun epg--status-TRUST_FULLY (context string)
1135 (let ((signature (car (epg-context-result-for context 'verify))))
1137 (eq (epg-signature-status signature) 'good))
1138 (epg-signature-set-validity signature 'full))))
1140 (defun epg--status-TRUST_ULTIMATE (context string)
1141 (let ((signature (car (epg-context-result-for context 'verify))))
1143 (eq (epg-signature-status signature) 'good))
1144 (epg-signature-set-validity signature 'ultimate))))
1146 (defun epg--status-PROGRESS (context string)
1147 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1149 (funcall (if (consp (epg-context-progress-callback context))
1150 (car (epg-context-progress-callback context))
1151 (epg-context-progress-callback context))
1153 (match-string 1 string)
1154 (match-string 2 string)
1155 (string-to-number (match-string 3 string))
1156 (string-to-number (match-string 4 string))
1157 (if (consp (epg-context-progress-callback context))
1158 (cdr (epg-context-progress-callback context))))))
1160 (defun epg--status-DECRYPTION_FAILED (context string)
1161 (epg-context-set-result-for
1163 (cons '(decryption-failed)
1164 (epg-context-result-for context 'error))))
1166 (defun epg--status-NODATA (context string)
1167 (epg-context-set-result-for
1169 (cons (list 'no-data (cons 'reason (string-to-number string)))
1170 (epg-context-result-for context 'error))))
1172 (defun epg--status-UNEXPECTED (context string)
1173 (epg-context-set-result-for
1175 (cons (list 'unexpected (cons 'reason (string-to-number string)))
1176 (epg-context-result-for context 'error))))
1178 (defun epg--status-KEYEXPIRED (context string)
1179 (epg-context-set-result-for
1181 (cons (list 'key-expired (cons 'expiration-time
1182 (epg--time-from-seconds string)))
1183 (epg-context-result-for context 'error))))
1185 (defun epg--status-KEYREVOKED (context string)
1186 (epg-context-set-result-for
1188 (cons '(key-revoked)
1189 (epg-context-result-for context 'error))))
1191 (defun epg--status-BADARMOR (context string)
1192 (epg-context-set-result-for
1195 (epg-context-result-for context 'error))))
1197 (defun epg--status-INV_RECP (context string)
1198 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1199 (epg-context-set-result-for
1201 (cons (list 'invalid-recipient
1203 (string-to-number (match-string 1 string)))
1204 (cons 'requested-recipient
1205 (match-string 2 string)))
1206 (epg-context-result-for context 'error)))))
1208 (defun epg--status-NO_RECP (context string)
1209 (epg-context-set-result-for
1211 (cons '(no-recipients)
1212 (epg-context-result-for context 'error))))
1214 (defun epg--status-DELETE_PROBLEM (context string)
1215 (if (string-match "\\`\\([0-9]+\\)" string)
1216 (epg-context-set-result-for
1218 (cons (list 'delete-problem
1219 (cons 'reason (string-to-number (match-string 1 string))))
1220 (epg-context-result-for context 'error)))))
1222 (defun epg--status-SIG_CREATED (context string)
1223 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1224 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1225 (epg-context-set-result-for
1227 (cons (epg-make-new-signature
1228 (cdr (assq (aref (match-string 1 string) 0)
1229 epg-new-signature-type-alist))
1230 (string-to-number (match-string 2 string))
1231 (string-to-number (match-string 3 string))
1232 (string-to-number (match-string 4 string) 16)
1233 (epg--time-from-seconds (match-string 5 string))
1234 (substring string (match-end 0)))
1235 (epg-context-result-for context 'sign)))))
1237 (defun epg--status-KEY_CREATED (context string)
1238 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1239 (epg-context-set-result-for
1240 context 'generate-key
1241 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1242 (cons 'fingerprint (match-string 2 string)))
1243 (epg-context-result-for context 'generate-key)))))
1245 (defun epg--status-KEY_NOT_CREATED (context string)
1246 (epg-context-set-result-for
1248 (cons '(key-not-created)
1249 (epg-context-result-for context 'error))))
1251 (defun epg--status-IMPORTED (context string)
1252 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1253 (let* ((key-id (match-string 1 string))
1254 (user-id (match-string 2 string))
1255 (entry (assoc key-id epg-user-id-alist)))
1257 (setcdr entry user-id)
1258 (setq epg-user-id-alist (cons (cons key-id user-id)
1259 epg-user-id-alist)))
1260 (epg-context-set-result-for
1262 (cons (list (cons 'key-id key-id)
1263 (cons 'user-id user-id))
1264 (epg-context-result-for context 'import))))))
1266 (defun epg--status-IMPORT_OK (context string)
1267 (let ((result (epg-context-result-for context 'import)))
1269 (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string))
1271 (append (list (cons 'reason
1273 (match-string 1 string))))
1274 (if (match-beginning 2)
1275 (list (cons 'fingerprint
1276 (match-string 3 string))))
1279 (defun epg--status-IMPORT_PROBLEM (context string)
1280 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1281 (epg-context-set-result-for
1283 (cons (cons 'import-problem
1284 (append (list (cons 'reason
1286 (match-string 1 string))))
1287 (if (match-beginning 2)
1288 (list (cons 'fingerprint
1289 (match-string 3 string))))))
1290 (epg-context-result-for context 'error)))))
1292 (defun epg-passphrase-callback-function (context key-id handback)
1293 (if (eq key-id 'SYM)
1294 (read-passwd "Passphrase for symmetric encryption: "
1295 (eq (epg-context-operation context) 'encrypt))
1297 (if (eq key-id 'PIN)
1298 "Passphrase for PIN: "
1299 (let ((entry (assoc key-id epg-user-id-alist)))
1301 (format "Passphrase for %s %s: " key-id (cdr entry))
1302 (format "Passphrase for %s: " key-id)))))))
1304 (defun epg-progress-callback-function (context what char current total
1306 (message "%s: %d%%/%d%%" what current total))
1308 (defun epg-configuration ()
1309 "Return a list of internal configuration parameters of `epg-gpg-program'."
1312 (apply #'call-process epg-gpg-program nil (list t nil) nil
1313 '("--with-colons" "--list-config"))
1314 (goto-char (point-min))
1315 (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
1316 (setq type (intern (match-string 1))
1317 config (cons (cons type
1319 '(pubkey cipher digest compress))
1320 (mapcar #'string-to-number
1321 (delete "" (split-string
1328 (defun epg--list-keys-1 (context name mode)
1329 (let ((args (append (list "--with-colons" "--no-greeting" "--batch"
1330 "--with-fingerprint"
1331 "--with-fingerprint"
1332 (if (memq mode '(t secret))
1333 "--list-secret-keys"
1334 (if (memq mode '(nil public))
1337 (unless (eq (epg-context-protocol context) 'CMS)
1338 '("--fixed-list-mode"))
1339 (if name (list name))))
1340 keys string field index)
1342 (apply #'call-process
1343 (if (eq (epg-context-protocol context) 'CMS)
1346 nil (list t nil) nil args)
1347 (goto-char (point-min))
1348 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1349 (setq keys (cons (make-vector 15 nil) keys)
1350 string (match-string 0)
1354 (string-match "\\([^:]+\\)?:" string index))
1355 (setq index (match-end 0))
1356 (aset (car keys) field (match-string 1 string))
1357 (setq field (1+ field))))
1360 (defun epg--make-sub-key-1 (line)
1363 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1365 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1367 (member (aref line 0) '("sec" "ssb"))
1368 (string-to-number (aref line 3))
1369 (string-to-number (aref line 2))
1371 (epg--time-from-seconds (aref line 5))
1372 (epg--time-from-seconds (aref line 6))))
1375 (defun epg-list-keys (context &optional name mode)
1376 "Return a list of epg-key objects matched with NAME.
1377 If MODE is nil or 'public, only public keyring should be searched.
1378 If MODE is t or 'secret, only secret keyring should be searched.
1379 Otherwise, only public keyring should be searched and the key
1380 signatures should be included."
1381 (let ((lines (epg--list-keys-1 context name mode))
1382 keys cert pointer pointer-1)
1385 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1386 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1387 keys (cons (epg-make-key
1388 (if (aref (car lines) 8)
1389 (cdr (assq (string-to-char (aref (car lines) 8))
1390 epg-key-validity-alist))))
1392 (epg-key-set-sub-key-list
1394 (cons (epg--make-sub-key-1 (car lines))
1395 (epg-key-sub-key-list (car keys)))))
1396 ((member (aref (car lines) 0) '("sub" "ssb"))
1397 (epg-key-set-sub-key-list
1399 (cons (epg--make-sub-key-1 (car lines))
1400 (epg-key-sub-key-list (car keys)))))
1401 ((equal (aref (car lines) 0) "uid")
1402 (epg-key-set-user-id-list
1404 (cons (epg-make-user-id
1405 (if (aref (car lines) 1)
1406 (cdr (assq (string-to-char (aref (car lines) 1))
1407 epg-key-validity-alist)))
1410 (epg-dn-from-string (aref (car lines) 9))
1411 (error (aref (car lines) 9)))
1412 (aref (car lines) 9)))
1413 (epg-key-user-id-list (car keys)))))
1414 ((equal (aref (car lines) 0) "fpr")
1415 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1416 (aref (car lines) 9)))
1417 ((equal (aref (car lines) 0) "sig")
1418 (epg-user-id-set-signature-list
1419 (car (epg-key-user-id-list (car keys)))
1421 (epg-make-key-signature
1422 (if (aref (car lines) 1)
1423 (cdr (assq (string-to-char (aref (car lines) 1))
1424 epg-key-validity-alist)))
1425 (string-to-number (aref (car lines) 3))
1426 (aref (car lines) 4)
1427 (epg--time-from-seconds (aref (car lines) 5))
1428 (epg--time-from-seconds (aref (car lines) 6))
1429 (aref (car lines) 9)
1430 (string-to-number (aref (car lines) 10) 16)
1431 (eq (aref (aref (car lines) 10) 2) ?x))
1432 (epg-user-id-signature-list
1433 (car (epg-key-user-id-list (car keys))))))))
1434 (setq lines (cdr lines)))
1435 (setq keys (nreverse keys)
1438 (epg-key-set-sub-key-list
1440 (nreverse (epg-key-sub-key-list (car pointer))))
1441 (setq pointer-1 (epg-key-set-user-id-list
1443 (nreverse (epg-key-user-id-list (car pointer)))))
1445 (epg-user-id-set-signature-list
1447 (nreverse (epg-user-id-signature-list (car pointer-1))))
1448 (setq pointer-1 (cdr pointer-1)))
1449 (setq pointer (cdr pointer)))
1452 (if (fboundp 'make-temp-file)
1453 (defalias 'epg--make-temp-file 'make-temp-file)
1454 (defvar temporary-file-directory)
1455 ;; stolen from poe.el.
1456 (defun epg--make-temp-file (prefix)
1457 "Create a temporary file.
1458 The returned file name (created by appending some random characters at the end
1459 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1460 is guaranteed to point to a newly created empty file.
1461 You can then use `write-region' to write new data into the file."
1462 (let (tempdir tempfile)
1463 (setq prefix (expand-file-name prefix
1464 (if (featurep 'xemacs)
1466 temporary-file-directory)))
1469 ;; First, create a temporary directory.
1470 (while (condition-case ()
1472 (setq tempdir (make-temp-name
1474 (file-name-directory prefix)
1476 ;; return nil or signal an error.
1477 (make-directory tempdir))
1479 (file-already-exists t)))
1480 (set-file-modes tempdir 448)
1481 ;; Second, create a temporary file in the tempdir.
1482 ;; There *is* a race condition between `make-temp-name'
1483 ;; and `write-region', but we don't care it since we are
1484 ;; in a private directory now.
1485 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1486 (write-region "" nil tempfile nil 'silent)
1487 (set-file-modes tempfile 384)
1488 ;; Finally, make a hard-link from the tempfile.
1489 (while (condition-case ()
1491 (setq file (make-temp-name prefix))
1492 ;; return nil or signal an error.
1493 (add-name-to-file tempfile file))
1495 (file-already-exists t)))
1497 ;; Cleanup the tempfile.
1499 (file-exists-p tempfile)
1500 (delete-file tempfile))
1501 ;; Cleanup the tempdir.
1503 (file-directory-p tempdir)
1504 (delete-directory tempdir))))))
1507 (defun epg-cancel (context)
1508 (if (buffer-live-p (process-buffer (epg-context-process context)))
1510 (set-buffer (process-buffer (epg-context-process context)))
1511 (epg-context-set-result-for
1514 (epg-context-result-for epg-context 'error)))))
1515 (if (eq (process-status (epg-context-process context)) 'run)
1516 (delete-process (epg-context-process context))))
1519 (defun epg-start-decrypt (context cipher)
1520 "Initiate a decrypt operation on CIPHER.
1521 CIPHER is a data object.
1523 If you use this function, you will need to wait for the completion of
1524 `epg-gpg-program' by using `epg-wait-for-completion' and call
1525 `epg-reset' to clear a temporaly output file.
1526 If you are unsure, use synchronous version of this function
1527 `epg-decrypt-file' or `epg-decrypt-string' instead."
1528 (unless (epg-data-file cipher)
1529 (error "Not a file"))
1530 (epg-context-set-operation context 'decrypt)
1531 (epg-context-set-result context nil)
1532 (epg--start context (list "--decrypt" (epg-data-file cipher)))
1533 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1534 (unless (eq (epg-context-protocol context) 'CMS)
1535 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1538 (defun epg-decrypt-file (context cipher plain)
1539 "Decrypt a file CIPHER and store the result to a file PLAIN.
1540 If PLAIN is nil, it returns the result as a string."
1544 (epg-context-set-output-file context plain)
1545 (epg-context-set-output-file context
1546 (epg--make-temp-file "epg-output")))
1547 (epg-start-decrypt context (epg-make-data-from-file cipher))
1548 (epg-wait-for-completion context)
1549 (if (epg-context-result-for context 'error)
1550 (error "Decrypt failed: %S"
1551 (epg-context-result-for context 'error)))
1553 (epg-read-output context)))
1555 (epg-delete-output-file context))
1556 (epg-reset context)))
1559 (defun epg-decrypt-string (context cipher)
1560 "Decrypt a string CIPHER and return the plain text."
1561 (let ((input-file (epg--make-temp-file "epg-input"))
1562 (coding-system-for-write 'binary))
1565 (write-region cipher nil input-file nil 'quiet)
1566 (epg-context-set-output-file context
1567 (epg--make-temp-file "epg-output"))
1568 (epg-start-decrypt context (epg-make-data-from-file input-file))
1569 (epg-wait-for-completion context)
1570 (if (epg-context-result-for context 'error)
1571 (error "Decrypt failed: %S"
1572 (epg-context-result-for context 'error)))
1573 (epg-read-output context))
1574 (epg-delete-output-file context)
1575 (if (file-exists-p input-file)
1576 (delete-file input-file))
1577 (epg-reset context))))
1580 (defun epg-start-verify (context signature &optional signed-text)
1581 "Initiate a verify operation on SIGNATURE.
1582 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1584 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1585 For a normal or a clear text signature, SIGNED-TEXT should be nil.
1587 If you use this function, you will need to wait for the completion of
1588 `epg-gpg-program' by using `epg-wait-for-completion' and call
1589 `epg-reset' to clear a temporaly output file.
1590 If you are unsure, use synchronous version of this function
1591 `epg-verify-file' or `epg-verify-string' instead."
1592 (epg-context-set-operation context 'verify)
1593 (epg-context-set-result context nil)
1595 ;; Detached signature.
1596 (if (epg-data-file signed-text)
1597 (epg--start context (list "--verify" (epg-data-file signature)
1598 (epg-data-file signed-text)))
1599 (epg--start context (list "--verify" (epg-data-file signature) "-"))
1600 (if (eq (process-status (epg-context-process context)) 'run)
1601 (process-send-string (epg-context-process context)
1602 (epg-data-string signed-text)))
1603 (if (eq (process-status (epg-context-process context)) 'run)
1604 (process-send-eof (epg-context-process context))))
1605 ;; Normal (or cleartext) signature.
1606 (if (epg-data-file signature)
1607 (epg--start context (list "--verify" (epg-data-file signature)))
1608 (epg--start context (list "--verify"))
1609 (if (eq (process-status (epg-context-process context)) 'run)
1610 (process-send-string (epg-context-process context)
1611 (epg-data-string signature)))
1612 (if (eq (process-status (epg-context-process context)) 'run)
1613 (process-send-eof (epg-context-process context))))))
1616 (defun epg-verify-file (context signature &optional signed-text plain)
1617 "Verify a file SIGNATURE.
1618 SIGNED-TEXT and PLAIN are also a file if they are specified.
1620 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1621 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1625 (epg-context-set-output-file context plain)
1626 (epg-context-set-output-file context
1627 (epg--make-temp-file "epg-output")))
1629 (epg-start-verify context
1630 (epg-make-data-from-file signature)
1631 (epg-make-data-from-file signed-text))
1632 (epg-start-verify context
1633 (epg-make-data-from-file signature)))
1634 (epg-wait-for-completion context)
1635 ; (if (epg-context-result-for context 'error)
1636 ; (error "Verify failed: %S"
1637 ; (epg-context-result-for context 'error)))
1639 (epg-read-output context)))
1641 (epg-delete-output-file context))
1642 (epg-reset context)))
1645 (defun epg-verify-string (context signature &optional signed-text)
1646 "Verify a string SIGNATURE.
1647 SIGNED-TEXT is a string if it is specified.
1649 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1650 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1651 (let ((coding-system-for-write 'binary)
1655 (epg-context-set-output-file context
1656 (epg--make-temp-file "epg-output"))
1659 (setq input-file (epg--make-temp-file "epg-signature"))
1660 (write-region signature nil input-file nil 'quiet)
1661 (epg-start-verify context
1662 (epg-make-data-from-file input-file)
1663 (epg-make-data-from-string signed-text)))
1664 (epg-start-verify context (epg-make-data-from-string signature)))
1665 (epg-wait-for-completion context)
1666 ; (if (epg-context-result-for context 'error)
1667 ; (error "Verify failed: %S"
1668 ; (epg-context-result-for context 'error)))
1669 (epg-read-output context))
1670 (epg-delete-output-file context)
1672 (file-exists-p input-file))
1673 (delete-file input-file))
1674 (epg-reset context))))
1677 (defun epg-start-sign (context plain &optional mode)
1678 "Initiate a sign operation on PLAIN.
1679 PLAIN is a data object.
1681 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1682 If it is nil or 'normal, it makes a normal signature.
1683 Otherwise, it makes a clear text signature.
1685 If you use this function, you will need to wait for the completion of
1686 `epg-gpg-program' by using `epg-wait-for-completion' and call
1687 `epg-reset' to clear a temporaly output file.
1688 If you are unsure, use synchronous version of this function
1689 `epg-sign-file' or `epg-sign-string' instead."
1690 (epg-context-set-operation context 'sign)
1691 (epg-context-set-result context nil)
1693 (append (list (if (memq mode '(t detached))
1695 (if (memq mode '(nil normal))
1703 (car (epg-key-sub-key-list signer)))))
1704 (epg-context-signers context)))
1705 (if (epg-data-file plain)
1706 (list (epg-data-file plain)))))
1707 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1708 (unless (eq (epg-context-protocol context) 'CMS)
1709 (epg-wait-for-status context '("BEGIN_SIGNING")))
1710 (when (epg-data-string plain)
1711 (if (eq (process-status (epg-context-process context)) 'run)
1712 (process-send-string (epg-context-process context)
1713 (epg-data-string plain)))
1714 (if (eq (process-status (epg-context-process context)) 'run)
1715 (process-send-eof (epg-context-process context)))))
1718 (defun epg-sign-file (context plain signature &optional mode)
1719 "Sign a file PLAIN and store the result to a file SIGNATURE.
1720 If SIGNATURE is nil, it returns the result as a string.
1721 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1722 If it is nil or 'normal, it makes a normal signature.
1723 Otherwise, it makes a clear text signature."
1727 (epg-context-set-output-file context signature)
1728 (epg-context-set-output-file context
1729 (epg--make-temp-file "epg-output")))
1730 (epg-start-sign context (epg-make-data-from-file plain) mode)
1731 (epg-wait-for-completion context)
1732 (unless (epg-context-result-for context 'sign)
1733 (if (epg-context-result-for context 'error)
1734 (error "Sign failed: %S"
1735 (epg-context-result-for context 'error))
1736 (error "Sign failed")))
1738 (epg-read-output context)))
1740 (epg-delete-output-file context))
1741 (epg-reset context)))
1744 (defun epg-sign-string (context plain &optional mode)
1745 "Sign a string PLAIN and return the output as string.
1746 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1747 If it is nil or 'normal, it makes a normal signature.
1748 Otherwise, it makes a clear text signature."
1751 (epg-context-set-output-file context
1752 (epg--make-temp-file "epg-output"))
1753 (epg-start-sign context (epg-make-data-from-string plain) mode)
1754 (epg-wait-for-completion context)
1755 (unless (epg-context-result-for context 'sign)
1756 (if (epg-context-result-for context 'error)
1757 (error "Sign failed: %S"
1758 (epg-context-result-for context 'error))
1759 (error "Sign failed")))
1760 (epg-read-output context))
1761 (epg-delete-output-file context)
1762 (epg-reset context)))
1765 (defun epg-start-encrypt (context plain recipients
1766 &optional sign always-trust)
1767 "Initiate an encrypt operation on PLAIN.
1768 PLAIN is a data object.
1769 If RECIPIENTS is nil, it performs symmetric encryption.
1771 If you use this function, you will need to wait for the completion of
1772 `epg-gpg-program' by using `epg-wait-for-completion' and call
1773 `epg-reset' to clear a temporaly output file.
1774 If you are unsure, use synchronous version of this function
1775 `epg-encrypt-file' or `epg-encrypt-string' instead."
1776 (epg-context-set-operation context 'encrypt)
1777 (epg-context-set-result context nil)
1779 (append (if always-trust '("--always-trust"))
1780 (if recipients '("--encrypt") '("--symmetric"))
1788 (car (epg-key-sub-key-list
1790 (epg-context-signers context)))))
1796 (car (epg-key-sub-key-list recipient)))))
1798 (if (epg-data-file plain)
1799 (list (epg-data-file plain)))))
1800 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1801 (unless (eq (epg-context-protocol context) 'CMS)
1803 (epg-wait-for-status context '("BEGIN_SIGNING"))
1804 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
1805 (when (epg-data-string plain)
1806 (if (eq (process-status (epg-context-process context)) 'run)
1807 (process-send-string (epg-context-process context)
1808 (epg-data-string plain)))
1809 (if (eq (process-status (epg-context-process context)) 'run)
1810 (process-send-eof (epg-context-process context)))))
1813 (defun epg-encrypt-file (context plain recipients
1814 cipher &optional sign always-trust)
1815 "Encrypt a file PLAIN and store the result to a file CIPHER.
1816 If CIPHER is nil, it returns the result as a string.
1817 If RECIPIENTS is nil, it performs symmetric encryption."
1821 (epg-context-set-output-file context cipher)
1822 (epg-context-set-output-file context
1823 (epg--make-temp-file "epg-output")))
1824 (epg-start-encrypt context (epg-make-data-from-file plain)
1825 recipients sign always-trust)
1826 (epg-wait-for-completion context)
1828 (not (epg-context-result-for context 'sign)))
1829 (if (epg-context-result-for context 'error)
1830 (error "Sign failed: %S"
1831 (epg-context-result-for context 'error))
1832 (error "Sign failed")))
1833 (if (epg-context-result-for context 'error)
1834 (error "Encrypt failed: %S"
1835 (epg-context-result-for context 'error)))
1837 (epg-read-output context)))
1839 (epg-delete-output-file context))
1840 (epg-reset context)))
1843 (defun epg-encrypt-string (context plain recipients
1844 &optional sign always-trust)
1845 "Encrypt a string PLAIN.
1846 If RECIPIENTS is nil, it performs symmetric encryption."
1849 (epg-context-set-output-file context
1850 (epg--make-temp-file "epg-output"))
1851 (epg-start-encrypt context (epg-make-data-from-string plain)
1852 recipients sign always-trust)
1853 (epg-wait-for-completion context)
1855 (not (epg-context-result-for context 'sign)))
1856 (if (epg-context-result-for context 'error)
1857 (error "Sign failed: %S"
1858 (epg-context-result-for context 'error))
1859 (error "Sign failed")))
1860 (if (epg-context-result-for context 'error)
1861 (error "Encrypt failed: %S"
1862 (epg-context-result-for context 'error)))
1863 (epg-read-output context))
1864 (epg-delete-output-file context)
1865 (epg-reset context)))
1868 (defun epg-start-export-keys (context keys)
1869 "Initiate an export keys operation.
1871 If you use this function, you will need to wait for the completion of
1872 `epg-gpg-program' by using `epg-wait-for-completion' and call
1873 `epg-reset' to clear a temporaly output file.
1874 If you are unsure, use synchronous version of this function
1875 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1876 (epg-context-set-operation context 'export-keys)
1877 (epg-context-set-result context nil)
1878 (epg--start context (cons "--export"
1882 (car (epg-key-sub-key-list key))))
1886 (defun epg-export-keys-to-file (context keys file)
1887 "Extract public KEYS."
1891 (epg-context-set-output-file context file)
1892 (epg-context-set-output-file context
1893 (epg--make-temp-file "epg-output")))
1894 (epg-start-export-keys context keys)
1895 (epg-wait-for-completion context)
1896 (if (epg-context-result-for context 'error)
1897 (error "Export keys failed: %S"
1898 (epg-context-result-for context 'error)))
1900 (epg-read-output context)))
1902 (epg-delete-output-file context))
1903 (epg-reset context)))
1906 (defun epg-export-keys-to-string (context keys)
1907 "Extract public KEYS and return them as a string."
1908 (epg-export-keys-to-file context keys nil))
1911 (defun epg-start-import-keys (context keys)
1912 "Initiate an import keys operation.
1913 KEYS is a data object.
1915 If you use this function, you will need to wait for the completion of
1916 `epg-gpg-program' by using `epg-wait-for-completion' and call
1917 `epg-reset' to clear a temporaly output file.
1918 If you are unsure, use synchronous version of this function
1919 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1920 (epg-context-set-operation context 'import-keys)
1921 (epg-context-set-result context nil)
1922 (epg--start context (if (epg-data-file keys)
1923 (list "--import" (epg-data-file keys))
1925 (when (epg-data-string keys)
1926 (if (eq (process-status (epg-context-process context)) 'run)
1927 (process-send-string (epg-context-process context)
1928 (epg-data-string keys)))
1929 (if (eq (process-status (epg-context-process context)) 'run)
1930 (process-send-eof (epg-context-process context)))))
1932 (defun epg--import-keys-1 (context keys)
1935 (epg-start-import-keys context keys)
1936 (epg-wait-for-completion context)
1937 (if (epg-context-result-for context 'error)
1938 (error "Import keys failed: %S"
1939 (epg-context-result-for context 'error))))
1940 (epg-reset context)))
1943 (defun epg-import-keys-from-file (context keys)
1944 "Add keys from a file KEYS."
1945 (epg--import-keys-1 context (epg-make-data-from-file keys)))
1948 (defun epg-import-keys-from-string (context keys)
1949 "Add keys from a string KEYS."
1950 (epg--import-keys-1 context (epg-make-data-from-string keys)))
1953 (defun epg-start-receive-keys (context key-id-list)
1954 "Initiate a receive key operation.
1955 KEY-ID-LIST is a list of key IDs.
1957 If you use this function, you will need to wait for the completion of
1958 `epg-gpg-program' by using `epg-wait-for-completion' and call
1959 `epg-reset' to clear a temporaly output file.
1960 If you are unsure, use synchronous version of this function
1961 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
1962 (epg-context-set-operation context 'receive-keys)
1963 (epg-context-set-result context nil)
1964 (epg--start context (cons "--recv-keys" key-id-list)))
1967 (defun epg-receive-keys (context keys)
1968 "Add keys from server.
1969 KEYS is a list of key IDs"
1972 (epg-start-receive-keys context keys)
1973 (epg-wait-for-completion context)
1974 (if (epg-context-result-for context 'error)
1975 (error "Receive keys failed: %S"
1976 (epg-context-result-for context 'error))))
1977 (epg-reset context)))
1980 (defalias 'epg-import-keys-from-server 'epg-receive-keys)
1983 (defun epg-start-delete-keys (context keys &optional allow-secret)
1984 "Initiate an delete keys operation.
1986 If you use this function, you will need to wait for the completion of
1987 `epg-gpg-program' by using `epg-wait-for-completion' and call
1988 `epg-reset' to clear a temporaly output file.
1989 If you are unsure, use synchronous version of this function
1990 `epg-delete-keys' instead."
1991 (epg-context-set-operation context 'delete-keys)
1992 (epg-context-set-result context nil)
1993 (epg--start context (cons (if allow-secret
1994 "--delete-secret-key"
1999 (car (epg-key-sub-key-list key))))
2003 (defun epg-delete-keys (context keys &optional allow-secret)
2004 "Delete KEYS from the key ring."
2007 (epg-start-delete-keys context keys allow-secret)
2008 (epg-wait-for-completion context)
2009 (if (epg-context-result-for context 'error)
2010 (error "Delete keys failed: %S"
2011 (epg-context-result-for context 'error))))
2012 (epg-reset context)))
2015 (defun epg-start-sign-keys (context keys &optional local)
2016 "Initiate an sign keys operation.
2018 If you use this function, you will need to wait for the completion of
2019 `epg-gpg-program' by using `epg-wait-for-completion' and call
2020 `epg-reset' to clear a temporaly output file.
2021 If you are unsure, use synchronous version of this function
2022 `epg-sign-keys' instead."
2023 (epg-context-set-operation context 'sign-keys)
2024 (epg-context-set-result context nil)
2025 (epg--start context (cons (if local
2031 (car (epg-key-sub-key-list key))))
2035 (defun epg-sign-keys (context keys &optional local)
2036 "Sign KEYS from the key ring."
2039 (epg-start-sign-keys context keys local)
2040 (epg-wait-for-completion context)
2041 (if (epg-context-result-for context 'error)
2042 (error "Sign keys failed: %S"
2043 (epg-context-result-for context 'error))))
2044 (epg-reset context)))
2047 (defun epg-start-generate-key (context parameters)
2048 "Initiate a key generation.
2049 PARAMETERS specifies parameters for the key.
2051 If you use this function, you will need to wait for the completion of
2052 `epg-gpg-program' by using `epg-wait-for-completion' and call
2053 `epg-reset' to clear a temporaly output file.
2054 If you are unsure, use synchronous version of this function
2055 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2056 (epg-context-set-operation context 'generate-key)
2057 (epg-context-set-result context nil)
2058 (if (epg-data-file parameters)
2059 (epg--start context (list "--batch" "--genkey"
2060 (epg-data-file parameters)))
2061 (epg--start context '("--batch" "--genkey"))
2062 (if (eq (process-status (epg-context-process context)) 'run)
2063 (process-send-string (epg-context-process context)
2064 (epg-data-string parameters)))
2065 (if (eq (process-status (epg-context-process context)) 'run)
2066 (process-send-eof (epg-context-process context)))))
2069 (defun epg-generate-key-from-file (context parameters)
2070 "Generate a new key pair.
2071 PARAMETERS is a file which tells how to create the key."
2074 (epg-start-generate-key context (epg-make-data-from-file parameters))
2075 (epg-wait-for-completion context)
2076 (if (epg-context-result-for context 'error)
2077 (error "Generate key failed: %S"
2078 (epg-context-result-for context 'error))))
2079 (epg-reset context)))
2082 (defun epg-generate-key-from-string (context parameters)
2083 "Generate a new key pair.
2084 PARAMETERS is a string which tells how to create the key."
2087 (epg-start-generate-key context (epg-make-data-from-string parameters))
2088 (epg-wait-for-completion context)
2089 (if (epg-context-result-for context 'error)
2090 (error "Generate key failed: %S"
2091 (epg-context-result-for context 'error))))
2092 (epg-reset context)))
2094 (defun epg--decode-hexstring (string)
2096 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
2097 (setq string (replace-match "\\x\\&" t nil string)
2099 (car (read-from-string (concat "\"" string "\"")))))
2101 (defun epg--decode-quotedstring (string)
2103 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2104 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
2106 (if (match-beginning 2)
2107 (setq string (replace-match "\\2" t nil string)
2109 (if (match-beginning 3)
2110 (setq string (replace-match "\\x\\3" t nil string)
2112 (setq string (replace-match "\\\\\\\\\\4" t nil string)
2113 index (+ index 3)))))
2114 (car (read-from-string (concat "\"" string "\"")))))
2116 (defun epg-dn-from-string (string)
2117 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2118 The return value is an alist mapping from types to values."
2120 (length (length string))
2121 alist type value group)
2122 (while (< index length)
2123 (if (eq index (string-match "[ \t\n\r]*" string index))
2124 (setq index (match-end 0)))
2125 (if (eq index (string-match
2126 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2128 (setq type (match-string 1 string)
2129 index (match-end 0))
2130 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2132 (setq type (match-string 1 string)
2133 index (match-end 0))))
2135 (error "Invalid type"))
2136 (if (eq index (string-match
2137 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2139 (setq index (match-end 0)
2140 value (epg--decode-quotedstring (match-string 0 string)))
2141 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
2142 (setq index (match-end 0)
2143 value (epg--decode-hexstring (match-string 1 string)))
2144 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2146 (setq index (match-end 0)
2147 value (epg--decode-quotedstring
2148 (match-string 0 string))))))
2150 (if (stringp (car (car alist)))
2151 (setcar alist (list (cons type value) (car alist)))
2152 (setcar alist (cons (cons type value) (car alist))))
2153 (if (consp (car (car alist)))
2154 (setcar alist (nreverse (car alist))))
2155 (setq alist (cons (cons type value) alist)
2158 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
2159 (setq index (match-end 0)
2160 group (eq (aref string (match-beginning 1)) ?+))))
2163 (defun epg-decode-dn (alist)
2164 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2165 Type names are resolved using `epg-dn-type-alist'."
2168 (if (stringp (car rdn))
2169 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
2171 (format "%s=%s" (cdr entry) (cdr rdn))
2172 (format "%s=%s" (car rdn) (cdr rdn))))
2173 (concat "(" (epg-decode-dn rdn) ")")))
2179 ;;; epg.el ends here