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 (defcustom epg-gpg-home-directory nil
43 "The directory which contains the `gpg' configuration files."
45 :type '(choice (const :tag "Default" nil) directory))
47 (defconst epg-version-number "0.0.4")
49 (defvar epg-user-id nil
50 "GnuPG ID of your default identity.")
52 (defvar epg-user-id-alist nil
53 "An alist mapping from key ID to user ID.")
55 (defvar epg-read-point nil)
56 (defvar epg-process-filter-running nil)
57 (defvar epg-pending-status-list nil)
58 (defvar epg-key-id nil)
59 (defvar epg-context nil)
60 (defvar epg-debug nil)
61 (defvar epg-debug-buffer nil)
63 ;; from gnupg/include/cipher.h
64 (defconst epg-cipher-algorithm-alist
76 ;; from gnupg/include/cipher.h
77 (defconst epg-pubkey-algorithm-alist
85 ;; from gnupg/include/cipher.h
86 (defconst epg-digest-algorithm-alist
94 ;; from gnupg/include/cipher.h
95 (defconst epg-compress-algorithm-alist
101 (defconst epg-invalid-recipients-reason-alist
102 '((0 . "No specific reason given")
104 (2 . "Ambigious specification")
105 (3 . "Wrong key usage")
110 (8 . "Policy mismatch")
111 (9 . "Not a secret key")
112 (10 . "Key not trusted")))
114 (defconst epg-delete-problem-reason-alist
115 '((1 . "No such key")
116 (2 . "Must delete secret key first")
117 (3 . "Ambigious specification")))
119 (defconst epg-import-ok-reason-alist
120 '((0 . "Not actually changed")
121 (1 . "Entirely new key")
123 (4 . "New signatures")
125 (16 . "Contains private key")))
127 (defconst epg-import-problem-reason-alist
128 '((0 . "No specific reason given")
129 (1 . "Invalid Certificate")
130 (2 . "Issuer Certificate missing")
131 (3 . "Certificate Chain too long")
132 (4 . "Error storing certificate")))
134 (defconst epg-no-data-reason-alist
135 '((1 . "No armored data")
136 (2 . "Expected a packet but did not found one")
137 (3 . "Invalid packet found, this may indicate a non OpenPGP message")
138 (4 . "Signature expected but not found")))
140 (defconst epg-unexpected-reason-alist nil)
142 (defvar epg-key-validity-alist
155 (defvar epg-key-capablity-alist
159 (?a . authentication)))
161 (defvar epg-new-signature-type-alist
166 (defvar epg-dn-type-alist
167 '(("1.2.840.113549.1.9.1" . "EMail")
171 ("0.2.262.1.10.7.20" . "NameDistinguisher")
172 ("2.5.4.16" . "ADDR")
175 ("2.5.4.17" . "PostalCode")
176 ("2.5.4.65" . "Pseudo")
177 ("2.5.4.5" . "SerialNumber")))
179 (defvar epg-prompt-alist nil)
181 (defun epg-make-data-from-file (file)
182 "Make a data object from FILE."
183 (cons 'epg-data (vector file nil)))
185 (defun epg-make-data-from-string (string)
186 "Make a data object from STRING."
187 (cons 'epg-data (vector nil string)))
189 (defun epg-data-file (data)
190 "Return the file of DATA."
191 (unless (eq (car data) 'epg-data)
192 (signal 'wrong-type-argument (list 'epg-data-p data)))
195 (defun epg-data-string (data)
196 "Return the string of DATA."
197 (unless (eq (car data) 'epg-data)
198 (signal 'wrong-type-argument (list 'epg-data-p data)))
201 (defun epg-make-context (&optional protocol armor textmode include-certs
202 cipher-algorithm digest-algorithm
204 "Return a context object."
206 (vector (or protocol 'OpenPGP) armor textmode include-certs
207 cipher-algorithm digest-algorithm compress-algorithm
208 #'epg-passphrase-callback-function
209 #'epg-progress-callback-function
210 nil nil nil nil nil)))
212 (defun epg-context-protocol (context)
213 "Return the protocol used within CONTEXT."
214 (unless (eq (car context) 'epg-context)
215 (signal 'wrong-type-argument (list 'epg-context-p context)))
216 (aref (cdr context) 0))
218 (defun epg-context-armor (context)
219 "Return t if the output shouled be ASCII armored in CONTEXT."
220 (unless (eq (car context) 'epg-context)
221 (signal 'wrong-type-argument (list 'epg-context-p context)))
222 (aref (cdr context) 1))
224 (defun epg-context-textmode (context)
225 "Return t if canonical text mode should be used in CONTEXT."
226 (unless (eq (car context) 'epg-context)
227 (signal 'wrong-type-argument (list 'epg-context-p context)))
228 (aref (cdr context) 2))
230 (defun epg-context-include-certs (context)
231 "Return how many certificates should be included in an S/MIME signed
233 (unless (eq (car context) 'epg-context)
234 (signal 'wrong-type-argument (list 'epg-context-p context)))
235 (aref (cdr context) 3))
237 (defun epg-context-cipher-algorithm (context)
238 "Return the cipher algorithm in CONTEXT."
239 (unless (eq (car context) 'epg-context)
240 (signal 'wrong-type-argument (list 'epg-context-p context)))
241 (aref (cdr context) 4))
243 (defun epg-context-digest-algorithm (context)
244 "Return the digest algorithm in CONTEXT."
245 (unless (eq (car context) 'epg-context)
246 (signal 'wrong-type-argument (list 'epg-context-p context)))
247 (aref (cdr context) 5))
249 (defun epg-context-compress-algorithm (context)
250 "Return the compress algorithm in CONTEXT."
251 (unless (eq (car context) 'epg-context)
252 (signal 'wrong-type-argument (list 'epg-context-p context)))
253 (aref (cdr context) 6))
255 (defun epg-context-passphrase-callback (context)
256 "Return the function used to query passphrase."
257 (unless (eq (car context) 'epg-context)
258 (signal 'wrong-type-argument (list 'epg-context-p context)))
259 (aref (cdr context) 7))
261 (defun epg-context-progress-callback (context)
262 "Return the function which handles progress update."
263 (unless (eq (car context) 'epg-context)
264 (signal 'wrong-type-argument (list 'epg-context-p context)))
265 (aref (cdr context) 8))
267 (defun epg-context-signers (context)
268 "Return the list of key-id for singning."
269 (unless (eq (car context) 'epg-context)
270 (signal 'wrong-type-argument (list 'epg-context-p context)))
271 (aref (cdr context) 9))
273 (defun epg-context-process (context)
274 "Return the process object of `epg-gpg-program'.
275 This function is for internal use only."
276 (unless (eq (car context) 'epg-context)
277 (signal 'wrong-type-argument (list 'epg-context-p context)))
278 (aref (cdr context) 10))
280 (defun epg-context-output-file (context)
281 "Return the output file of `epg-gpg-program'.
282 This function is for internal use only."
283 (unless (eq (car context) 'epg-context)
284 (signal 'wrong-type-argument (list 'epg-context-p context)))
285 (aref (cdr context) 11))
287 (defun epg-context-result (context)
288 "Return the result of the previous cryptographic operation."
289 (unless (eq (car context) 'epg-context)
290 (signal 'wrong-type-argument (list 'epg-context-p context)))
291 (aref (cdr context) 12))
293 (defun epg-context-operation (context)
294 "Return the name of the current cryptographic operation."
295 (unless (eq (car context) 'epg-context)
296 (signal 'wrong-type-argument (list 'epg-context-p context)))
297 (aref (cdr context) 13))
299 (defun epg-context-set-protocol (context protocol)
300 "Set the protocol used within CONTEXT."
301 (unless (eq (car context) 'epg-context)
302 (signal 'wrong-type-argument (list 'epg-context-p context)))
303 (aset (cdr context) 0 protocol))
305 (defun epg-context-set-armor (context armor)
306 "Specify if the output shouled be ASCII armored in CONTEXT."
307 (unless (eq (car context) 'epg-context)
308 (signal 'wrong-type-argument (list 'epg-context-p context)))
309 (aset (cdr context) 1 armor))
311 (defun epg-context-set-textmode (context textmode)
312 "Specify if canonical text mode should be used in CONTEXT."
313 (unless (eq (car context) 'epg-context)
314 (signal 'wrong-type-argument (list 'epg-context-p context)))
315 (aset (cdr context) 2 textmode))
317 (defun epg-context-set-include-certs (context include-certs)
318 "Set how many certificates should be included in an S/MIME signed message."
319 (unless (eq (car context) 'epg-context)
320 (signal 'wrong-type-argument (list 'epg-context-p context)))
321 (aset (cdr context) 3 include-certs))
323 (defun epg-context-set-cipher-algorithm (context cipher-algorithm)
324 "Set the cipher algorithm in CONTEXT."
325 (unless (eq (car context) 'epg-context)
326 (signal 'wrong-type-argument (list 'epg-context-p context)))
327 (aset (cdr context) 4 cipher-algorithm))
329 (defun epg-context-set-digest-algorithm (context digest-algorithm)
330 "Set the digest algorithm in CONTEXT."
331 (unless (eq (car context) 'epg-context)
332 (signal 'wrong-type-argument (list 'epg-context-p context)))
333 (aset (cdr context) 5 digest-algorithm))
335 (defun epg-context-set-compress-algorithm (context compress-algorithm)
336 "Set the compress algorithm in CONTEXT."
337 (unless (eq (car context) 'epg-context)
338 (signal 'wrong-type-argument (list 'epg-context-p context)))
339 (aset (cdr context) 6 compress-algorithm))
341 (defun epg-context-set-passphrase-callback (context
343 "Set the function used to query passphrase."
344 (unless (eq (car context) 'epg-context)
345 (signal 'wrong-type-argument (list 'epg-context-p context)))
346 (aset (cdr context) 7 passphrase-callback))
348 (defun epg-context-set-progress-callback (context progress-callback)
349 "Set the function which handles progress update."
350 (unless (eq (car context) 'epg-context)
351 (signal 'wrong-type-argument (list 'epg-context-p context)))
352 (aset (cdr context) 8 progress-callback))
354 (defun epg-context-set-signers (context signers)
355 "Set the list of key-id for singning."
356 (unless (eq (car context) 'epg-context)
357 (signal 'wrong-type-argument (list 'epg-context-p context)))
358 (aset (cdr context) 9 signers))
360 (defun epg-context-set-process (context process)
361 "Set the process object of `epg-gpg-program'.
362 This function is for internal use only."
363 (unless (eq (car context) 'epg-context)
364 (signal 'wrong-type-argument (list 'epg-context-p context)))
365 (aset (cdr context) 10 process))
367 (defun epg-context-set-output-file (context output-file)
368 "Set the output file of `epg-gpg-program'.
369 This function is for internal use only."
370 (unless (eq (car context) 'epg-context)
371 (signal 'wrong-type-argument (list 'epg-context-p context)))
372 (aset (cdr context) 11 output-file))
374 (defun epg-context-set-result (context result)
375 "Set the result of the previous cryptographic operation."
376 (unless (eq (car context) 'epg-context)
377 (signal 'wrong-type-argument (list 'epg-context-p context)))
378 (aset (cdr context) 12 result))
380 (defun epg-context-set-operation (context operation)
381 "Set the name of the current cryptographic operation."
382 (unless (eq (car context) 'epg-context)
383 (signal 'wrong-type-argument (list 'epg-context-p context)))
384 (aset (cdr context) 13 operation))
386 (defun epg-make-signature (status &optional key-id)
387 "Return a signature object."
388 (cons 'epg-signature (vector status key-id nil nil nil nil nil nil nil nil)))
390 (defun epg-signature-status (signature)
391 "Return the status code of SIGNATURE."
392 (unless (eq (car signature) 'epg-signature)
393 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
394 (aref (cdr signature) 0))
396 (defun epg-signature-key-id (signature)
397 "Return the key-id of SIGNATURE."
398 (unless (eq (car signature) 'epg-signature)
399 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
400 (aref (cdr signature) 1))
402 (defun epg-signature-validity (signature)
403 "Return the validity of SIGNATURE."
404 (unless (eq (car signature) 'epg-signature)
405 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
406 (aref (cdr signature) 2))
408 (defun epg-signature-fingerprint (signature)
409 "Return the fingerprint of SIGNATURE."
410 (unless (eq (car signature) 'epg-signature)
411 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
412 (aref (cdr signature) 3))
414 (defun epg-signature-creation-time (signature)
415 "Return the creation time of SIGNATURE."
416 (unless (eq (car signature) 'epg-signature)
417 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
418 (aref (cdr signature) 4))
420 (defun epg-signature-expiration-time (signature)
421 "Return the expiration time of SIGNATURE."
422 (unless (eq (car signature) 'epg-signature)
423 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
424 (aref (cdr signature) 5))
426 (defun epg-signature-pubkey-algorithm (signature)
427 "Return the public key algorithm of SIGNATURE."
428 (unless (eq (car signature) 'epg-signature)
429 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
430 (aref (cdr signature) 6))
432 (defun epg-signature-digest-algorithm (signature)
433 "Return the digest algorithm of SIGNATURE."
434 (unless (eq (car signature) 'epg-signature)
435 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
436 (aref (cdr signature) 7))
438 (defun epg-signature-class (signature)
439 "Return the class of SIGNATURE."
440 (unless (eq (car signature) 'epg-signature)
441 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
442 (aref (cdr signature) 8))
444 (defun epg-signature-version (signature)
445 "Return the version of SIGNATURE."
446 (unless (eq (car signature) 'epg-signature)
447 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
448 (aref (cdr signature) 9))
450 (defun epg-signature-set-status (signature status)
451 "Set the status code of SIGNATURE."
452 (unless (eq (car signature) 'epg-signature)
453 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
454 (aset (cdr signature) 0 status))
456 (defun epg-signature-set-key-id (signature key-id)
457 "Set the key-id of SIGNATURE."
458 (unless (eq (car signature) 'epg-signature)
459 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
460 (aset (cdr signature) 1 key-id))
462 (defun epg-signature-set-validity (signature validity)
463 "Set the validity of SIGNATURE."
464 (unless (eq (car signature) 'epg-signature)
465 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
466 (aset (cdr signature) 2 validity))
468 (defun epg-signature-set-fingerprint (signature fingerprint)
469 "Set the fingerprint of SIGNATURE."
470 (unless (eq (car signature) 'epg-signature)
471 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
472 (aset (cdr signature) 3 fingerprint))
474 (defun epg-signature-set-creation-time (signature creation-time)
475 "Set the creation time of SIGNATURE."
476 (unless (eq (car signature) 'epg-signature)
477 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
478 (aset (cdr signature) 4 creation-time))
480 (defun epg-signature-set-expiration-time (signature expiration-time)
481 "Set the expiration time of SIGNATURE."
482 (unless (eq (car signature) 'epg-signature)
483 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
484 (aset (cdr signature) 5 expiration-time))
486 (defun epg-signature-set-pubkey-algorithm (signature pubkey-algorithm)
487 "Set the public key algorithm of SIGNATURE."
488 (unless (eq (car signature) 'epg-signature)
489 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
490 (aset (cdr signature) 6 pubkey-algorithm))
492 (defun epg-signature-set-digest-algorithm (signature digest-algorithm)
493 "Set the digest algorithm of SIGNATURE."
494 (unless (eq (car signature) 'epg-signature)
495 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
496 (aset (cdr signature) 7 digest-algorithm))
498 (defun epg-signature-set-class (signature class)
499 "Set the class of SIGNATURE."
500 (unless (eq (car signature) 'epg-signature)
501 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
502 (aset (cdr signature) 8 class))
504 (defun epg-signature-set-version (signature version)
505 "Set the version of SIGNATURE."
506 (unless (eq (car signature) 'epg-signature)
507 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
508 (aset (cdr signature) 9 version))
510 (defun epg-make-new-signature (type pubkey-algorithm digest-algorithm
511 class creation-time fingerprint)
512 "Return a new signature object."
513 (cons 'epg-new-signature (vector type pubkey-algorithm digest-algorithm
514 class creation-time fingerprint)))
516 (defun epg-new-signature-type (new-signature)
517 "Return the type of NEW-SIGNATURE."
518 (unless (eq (car new-signature) 'epg-new-signature)
519 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
520 (aref (cdr new-signature) 0))
522 (defun epg-new-signature-pubkey-algorithm (new-signature)
523 "Return the public key algorithm of NEW-SIGNATURE."
524 (unless (eq (car new-signature) 'epg-new-signature)
525 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
526 (aref (cdr new-signature) 1))
528 (defun epg-new-signature-digest-algorithm (new-signature)
529 "Return the digest algorithm of NEW-SIGNATURE."
530 (unless (eq (car new-signature) 'epg-new-signature)
531 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
532 (aref (cdr new-signature) 2))
534 (defun epg-new-signature-class (new-signature)
535 "Return the class of NEW-SIGNATURE."
536 (unless (eq (car new-signature) 'epg-new-signature)
537 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
538 (aref (cdr new-signature) 3))
540 (defun epg-new-signature-creation-time (new-signature)
541 "Return the creation time of NEW-SIGNATURE."
542 (unless (eq (car new-signature) 'epg-new-signature)
543 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
544 (aref (cdr new-signature) 4))
546 (defun epg-new-signature-fingerprint (new-signature)
547 "Return the fingerprint of NEW-SIGNATURE."
548 (unless (eq (car new-signature) 'epg-new-signature)
549 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
550 (aref (cdr new-signature) 5))
552 (defun epg-make-key (owner-trust)
553 "Return a key object."
554 (cons 'epg-key (vector owner-trust nil nil)))
556 (defun epg-key-owner-trust (key)
557 "Return the owner trust of KEY."
558 (unless (eq (car key) 'epg-key)
559 (signal 'wrong-type-argument (list 'epg-key-p key)))
562 (defun epg-key-sub-key-list (key)
563 "Return the sub key list of KEY."
564 (unless (eq (car key) 'epg-key)
565 (signal 'wrong-type-argument (list 'epg-key-p key)))
568 (defun epg-key-user-id-list (key)
569 "Return the user ID list of KEY."
570 (unless (eq (car key) 'epg-key)
571 (signal 'wrong-type-argument (list 'epg-key-p key)))
574 (defun epg-key-set-sub-key-list (key sub-key-list)
575 "Set the sub key list of KEY."
576 (unless (eq (car key) 'epg-key)
577 (signal 'wrong-type-argument (list 'epg-key-p key)))
578 (aset (cdr key) 1 sub-key-list))
580 (defun epg-key-set-user-id-list (key user-id-list)
581 "Set the user ID list of KEY."
582 (unless (eq (car key) 'epg-key)
583 (signal 'wrong-type-argument (list 'epg-key-p key)))
584 (aset (cdr key) 2 user-id-list))
586 (defun epg-make-sub-key (validity capability secret-p algorithm length id
587 creation-time expiration-time)
588 "Return a sub key object."
590 (vector validity capability secret-p algorithm length id creation-time
591 expiration-time nil)))
593 (defun epg-sub-key-validity (sub-key)
594 "Return the validity of SUB-KEY."
595 (unless (eq (car sub-key) 'epg-sub-key)
596 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
597 (aref (cdr sub-key) 0))
599 (defun epg-sub-key-capability (sub-key)
600 "Return the capability of SUB-KEY."
601 (unless (eq (car sub-key) 'epg-sub-key)
602 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
603 (aref (cdr sub-key) 1))
605 (defun epg-sub-key-secret-p (sub-key)
606 "Return non-nil if SUB-KEY is a secret key."
607 (unless (eq (car sub-key) 'epg-sub-key)
608 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
609 (aref (cdr sub-key) 2))
611 (defun epg-sub-key-algorithm (sub-key)
612 "Return the algorithm of SUB-KEY."
613 (unless (eq (car sub-key) 'epg-sub-key)
614 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
615 (aref (cdr sub-key) 3))
617 (defun epg-sub-key-length (sub-key)
618 "Return the length of SUB-KEY."
619 (unless (eq (car sub-key) 'epg-sub-key)
620 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
621 (aref (cdr sub-key) 4))
623 (defun epg-sub-key-id (sub-key)
624 "Return the ID of SUB-KEY."
625 (unless (eq (car sub-key) 'epg-sub-key)
626 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
627 (aref (cdr sub-key) 5))
629 (defun epg-sub-key-creation-time (sub-key)
630 "Return the creation time of SUB-KEY."
631 (unless (eq (car sub-key) 'epg-sub-key)
632 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
633 (aref (cdr sub-key) 6))
635 (defun epg-sub-key-expiration-time (sub-key)
636 "Return the expiration time of SUB-KEY."
637 (unless (eq (car sub-key) 'epg-sub-key)
638 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
639 (aref (cdr sub-key) 7))
641 (defun epg-sub-key-fingerprint (sub-key)
642 "Return the fingerprint of SUB-KEY."
643 (unless (eq (car sub-key) 'epg-sub-key)
644 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
645 (aref (cdr sub-key) 8))
647 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
648 "Set the fingerprint of SUB-KEY.
649 This function is for internal use only."
650 (unless (eq (car sub-key) 'epg-sub-key)
651 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
652 (aset (cdr sub-key) 8 fingerprint))
654 (defun epg-make-user-id (validity string)
655 "Return a user ID object."
656 (cons 'epg-user-id (vector validity string nil)))
658 (defun epg-user-id-validity (user-id)
659 "Return the validity of USER-ID."
660 (unless (eq (car user-id) 'epg-user-id)
661 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
662 (aref (cdr user-id) 0))
664 (defun epg-user-id-string (user-id)
665 "Return the name of USER-ID."
666 (unless (eq (car user-id) 'epg-user-id)
667 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
668 (aref (cdr user-id) 1))
670 (defun epg-user-id-signature-list (user-id)
671 "Return the signature list of USER-ID."
672 (unless (eq (car user-id) 'epg-user-id)
673 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
674 (aref (cdr user-id) 2))
676 (defun epg-user-id-set-signature-list (user-id signature-list)
677 "Set the signature list of USER-ID."
678 (unless (eq (car user-id) 'epg-user-id)
679 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
680 (aset (cdr user-id) 2 signature-list))
682 (defun epg-make-key-signature (validity pubkey-algorithm key-id creation-time
683 expiration-time user-id class
685 "Return a key signature object."
686 (cons 'epg-key-signature
687 (vector validity pubkey-algorithm key-id creation-time expiration-time
688 user-id class exportable-p)))
690 (defun epg-key-signature-validity (key-signature)
691 "Return the validity of KEY-SIGNATURE."
692 (unless (eq (car key-signature) 'epg-key-signature)
693 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
694 (aref (cdr key-signature) 0))
696 (defun epg-key-signature-pubkey-algorithm (key-signature)
697 "Return the public key algorithm of KEY-SIGNATURE."
698 (unless (eq (car key-signature) 'epg-key-signature)
699 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
700 (aref (cdr key-signature) 1))
702 (defun epg-key-signature-key-id (key-signature)
703 "Return the key-id of KEY-SIGNATURE."
704 (unless (eq (car key-signature) 'epg-key-signature)
705 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
706 (aref (cdr key-signature) 2))
708 (defun epg-key-signature-creation-time (key-signature)
709 "Return the creation time of KEY-SIGNATURE."
710 (unless (eq (car key-signature) 'epg-key-signature)
711 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
712 (aref (cdr key-signature) 3))
714 (defun epg-key-signature-expiration-time (key-signature)
715 "Return the expiration time of KEY-SIGNATURE."
716 (unless (eq (car key-signature) 'epg-key-signature)
717 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
718 (aref (cdr key-signature) 4))
720 (defun epg-key-signature-user-id (key-signature)
721 "Return the user-id of KEY-SIGNATURE."
722 (unless (eq (car key-signature) 'epg-key-signature)
723 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
724 (aref (cdr key-signature) 5))
726 (defun epg-key-signature-class (key-signature)
727 "Return the class of KEY-SIGNATURE."
728 (unless (eq (car key-signature) 'epg-key-signature)
729 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
730 (aref (cdr key-signature) 6))
732 (defun epg-key-signature-exportable-p (key-signature)
733 "Return t if KEY-SIGNATURE is exportable."
734 (unless (eq (car key-signature) 'epg-key-signature)
735 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
736 (aref (cdr key-signature) 7))
738 (defun epg-context-result-for (context name)
739 "Return the result of CONTEXT associated with NAME."
740 (cdr (assq name (epg-context-result context))))
742 (defun epg-context-set-result-for (context name value)
743 "Set the result of CONTEXT associated with NAME to VALUE."
744 (let* ((result (epg-context-result context))
745 (entry (assq name result)))
748 (epg-context-set-result context (cons (cons name value) result)))))
750 (defun epg-signature-to-string (signature)
751 "Convert SIGNATURE to a human readable string."
752 (let ((user-id (cdr (assoc (epg-signature-key-id signature)
753 epg-user-id-alist))))
755 (cond ((eq (epg-signature-status signature) 'good)
756 "Good signature from ")
757 ((eq (epg-signature-status signature) 'bad)
758 "Bad signature from ")
759 ((eq (epg-signature-status signature) 'expired)
760 "Expired signature from ")
761 ((eq (epg-signature-status signature) 'expired-key)
762 "Signature made by expired key ")
763 ((eq (epg-signature-status signature) 'revoked-key)
764 "Signature made by revoked key ")
765 ((eq (epg-signature-status signature) 'no-pubkey)
766 "No public key for "))
767 (epg-signature-key-id signature)
770 (if (stringp user-id)
772 (epg-decode-dn user-id)))
774 (if (epg-signature-validity signature)
775 (format " (trust %s)" (epg-signature-validity signature))
778 (defun epg-verify-result-to-string (verify-result)
779 "Convert VERIFY-RESULT to a human readable string."
780 (mapconcat #'epg-signature-to-string verify-result "\n"))
782 (defun epg-new-signature-to-string (new-signature)
783 "Convert NEW-SIGNATURE to a human readable string."
785 (cond ((eq (epg-new-signature-type new-signature) 'detached)
786 "Detached signature ")
787 ((eq (epg-new-signature-type new-signature) 'clear)
788 "Clear text signature ")
791 (cdr (assq (epg-new-signature-pubkey-algorithm new-signature)
792 epg-pubkey-algorithm-alist))
794 (cdr (assq (epg-new-signature-digest-algorithm new-signature)
795 epg-digest-algorithm-alist))
797 (format "%02X " (epg-new-signature-class new-signature))
798 (epg-new-signature-fingerprint new-signature)))
800 (defun epg--start (context args)
801 "Start `epg-gpg-program' in a subprocess with given ARGS."
802 (if (and (epg-context-process context)
803 (eq (process-status (epg-context-process context)) 'run))
804 (error "%s is already running in this context"
805 (if (eq (epg-context-protocol context) 'CMS)
808 (let* ((args (append (list "--no-tty"
811 (if epg-gpg-home-directory
812 (list "--homedir" epg-gpg-home-directory))
813 (unless (eq (epg-context-protocol context) 'CMS)
814 (list "--command-fd" "0"))
815 (if (epg-context-armor context) '("--armor"))
816 (if (epg-context-textmode context) '("--textmode"))
817 (if (epg-context-output-file context)
818 (list "--output" (epg-context-output-file context)))
820 (coding-system-for-write 'binary)
821 process-connection-type
822 (orig-mode (default-file-modes))
823 (buffer (generate-new-buffer " *epg*"))
827 (unless epg-debug-buffer
828 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
829 (set-buffer epg-debug-buffer)
830 (goto-char (point-max))
831 (insert (format "%s %s\n"
832 (if (eq (epg-context-protocol context) 'CMS)
835 (mapconcat #'identity args " ")))))
836 (with-current-buffer buffer
837 (make-local-variable 'epg-read-point)
838 (setq epg-read-point (point-min))
839 (make-local-variable 'epg-process-filter-running)
840 (setq epg-process-filter-running nil)
841 (make-local-variable 'epg-pending-status-list)
842 (setq epg-pending-status-list nil)
843 (make-local-variable 'epg-key-id)
844 (setq epg-key-id nil)
845 (make-local-variable 'epg-context)
846 (setq epg-context context))
849 (set-default-file-modes 448)
851 (apply #'start-process "epg" buffer
852 (if (eq (epg-context-protocol context) 'CMS)
856 (set-default-file-modes orig-mode))
857 (set-process-filter process #'epg--process-filter)
858 (epg-context-set-process context process)))
860 (defun epg--process-filter (process input)
863 (unless epg-debug-buffer
864 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
865 (set-buffer epg-debug-buffer)
866 (goto-char (point-max))
868 (if (buffer-live-p (process-buffer process))
870 (set-buffer (process-buffer process))
871 (goto-char (point-max))
873 (unless epg-process-filter-running
876 (setq epg-process-filter-running t)
877 (goto-char epg-read-point)
879 (while (looking-at ".*\n") ;the input line finished
880 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
881 (let* ((status (match-string 1))
882 (string (match-string 2))
883 (symbol (intern-soft (concat "epg--status-"
885 (if (member status epg-pending-status-list)
886 (setq epg-pending-status-list nil))
889 (funcall symbol epg-context string))))
891 (setq epg-read-point (point))))
892 (setq epg-process-filter-running nil))))))
894 (defun epg-read-output (context)
895 "Read the output file CONTEXT and return the content as a string."
897 (if (fboundp 'set-buffer-multibyte)
898 (set-buffer-multibyte nil))
899 (if (file-exists-p (epg-context-output-file context))
900 (let ((coding-system-for-read 'binary))
901 (insert-file-contents (epg-context-output-file context))
904 (defun epg-wait-for-status (context status-list)
905 "Wait until one of elements in STATUS-LIST arrives."
906 (with-current-buffer (process-buffer (epg-context-process context))
907 (setq epg-pending-status-list status-list)
908 (while (and (eq (process-status (epg-context-process context)) 'run)
909 epg-pending-status-list)
910 (accept-process-output (epg-context-process context) 1))))
912 (defun epg-wait-for-completion (context)
913 "Wait until the `epg-gpg-program' process completes."
914 (while (eq (process-status (epg-context-process context)) 'run)
915 (accept-process-output (epg-context-process context) 1)))
917 (defun epg-reset (context)
919 (if (and (epg-context-process context)
920 (buffer-live-p (process-buffer (epg-context-process context))))
921 (kill-buffer (process-buffer (epg-context-process context))))
922 (epg-context-set-process context nil))
924 (defun epg-delete-output-file (context)
925 "Delete the output file of CONTEXT."
926 (if (and (epg-context-output-file context)
927 (file-exists-p (epg-context-output-file context)))
928 (delete-file (epg-context-output-file context))))
930 (defun epg--status-USERID_HINT (context string)
931 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
932 (let* ((key-id (match-string 1 string))
933 (user-id (match-string 2 string))
934 (entry (assoc key-id epg-user-id-alist)))
936 (setcdr entry user-id)
937 (setq epg-user-id-alist (cons (cons key-id user-id)
938 epg-user-id-alist))))))
940 (defun epg--status-NEED_PASSPHRASE (context string)
941 (if (string-match "\\`\\([^ ]+\\)" string)
942 (setq epg-key-id (match-string 1 string))))
944 (defun epg--status-NEED_PASSPHRASE_SYM (context string)
945 (setq epg-key-id 'SYM))
947 (defun epg--status-NEED_PASSPHRASE_PIN (context string)
948 (setq epg-key-id 'PIN))
950 (defun epg--status-GET_HIDDEN (context string)
952 (string-match "\\`passphrase\\." string))
955 passphrase-with-new-line)
961 (if (consp (epg-context-passphrase-callback context))
962 (car (epg-context-passphrase-callback context))
963 (epg-context-passphrase-callback context))
966 (if (consp (epg-context-passphrase-callback context))
967 (cdr (epg-context-passphrase-callback context)))))
969 (setq passphrase-with-new-line (concat passphrase "\n"))
970 (fillarray passphrase 0)
971 (setq passphrase nil)
972 (process-send-string (epg-context-process context)
973 passphrase-with-new-line)))
975 (epg-context-set-result-for
978 (epg-context-result-for context 'error)))
979 (delete-process (epg-context-process context))))
981 (fillarray passphrase 0))
982 (if passphrase-with-new-line
983 (fillarray passphrase-with-new-line 0))))))
985 (defun epg--status-GET_BOOL (context string)
986 (let ((entry (assoc string epg-prompt-alist))
989 (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
990 (process-send-string (epg-context-process context) "y\n")
991 (process-send-string (epg-context-process context) "n\n"))
993 (epg-context-set-result-for
996 (epg-context-result-for context 'error)))
997 (delete-process (epg-context-process context))))))
999 (defun epg--status-GET_LINE (context string)
1000 (let ((entry (assoc string epg-prompt-alist))
1003 (process-send-string (epg-context-process context)
1004 (concat (read-string
1007 (concat string ": ")))
1010 (epg-context-set-result-for
1013 (epg-context-result-for context 'error)))
1014 (delete-process (epg-context-process context))))))
1016 (defun epg--status-*SIG (context status string)
1017 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1018 (let* ((key-id (match-string 1 string))
1019 (user-id (match-string 2 string))
1020 (entry (assoc key-id epg-user-id-alist)))
1021 (epg-context-set-result-for
1024 (cons (epg-make-signature status key-id)
1025 (epg-context-result-for context 'verify)))
1026 (if (eq (epg-context-protocol context) 'CMS)
1028 (setq user-id (epg-dn-from-string user-id))
1031 (setcdr entry user-id)
1032 (setq epg-user-id-alist
1033 (cons (cons key-id user-id) epg-user-id-alist))))
1034 (epg-context-set-result-for
1037 (cons (epg-make-signature status)
1038 (epg-context-result-for context 'verify)))))
1040 (defun epg--status-GOODSIG (context string)
1041 (epg--status-*SIG context 'good string))
1043 (defun epg--status-EXPSIG (context string)
1044 (epg--status-*SIG context 'expired string))
1046 (defun epg--status-EXPKEYSIG (context string)
1047 (epg--status-*SIG context 'expired-key string))
1049 (defun epg--status-REVKEYSIG (context string)
1050 (epg--status-*SIG context 'revoked-key string))
1052 (defun epg--status-BADSIG (context string)
1053 (epg--status-*SIG context 'bad string))
1055 (defun epg--status-NO_PUBKEY (context string)
1056 (let ((signature (car (epg-context-result-for context 'verify))))
1058 (eq (epg-signature-status signature) 'error)
1059 (equal (epg-signature-key-id signature) string))
1060 (epg-signature-set-status signature 'no-pubkey))))
1062 (defun epg--time-from-seconds (seconds)
1063 (let ((number-seconds (string-to-number (concat seconds ".0"))))
1064 (cons (floor (/ number-seconds 65536))
1065 (floor (mod number-seconds 65536)))))
1067 (defun epg--status-ERRSIG (context string)
1068 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1069 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
1071 (let ((signature (epg-make-signature 'error)))
1072 (epg-context-set-result-for
1076 (epg-context-result-for context 'verify)))
1077 (epg-signature-set-key-id
1079 (match-string 1 string))
1080 (epg-signature-set-pubkey-algorithm
1082 (string-to-number (match-string 2 string)))
1083 (epg-signature-set-digest-algorithm
1085 (string-to-number (match-string 3 string)))
1086 (epg-signature-set-class
1088 (string-to-number (match-string 4 string) 16))
1089 (epg-signature-set-creation-time
1091 (epg--time-from-seconds (match-string 5 string))))))
1093 (defun epg--status-VALIDSIG (context string)
1094 (let ((signature (car (epg-context-result-for context 'verify))))
1095 (when (and signature
1096 (eq (epg-signature-status signature) 'good)
1097 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
1098 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
1101 (epg-signature-set-fingerprint
1103 (match-string 1 string))
1104 (epg-signature-set-creation-time
1106 (epg--time-from-seconds (match-string 2 string)))
1107 (epg-signature-set-expiration-time
1109 (epg--time-from-seconds (match-string 3 string)))
1110 (epg-signature-set-version
1112 (string-to-number (match-string 4 string)))
1113 (epg-signature-set-pubkey-algorithm
1115 (string-to-number (match-string 5 string)))
1116 (epg-signature-set-digest-algorithm
1118 (string-to-number (match-string 6 string)))
1119 (epg-signature-set-class
1121 (string-to-number (match-string 7 string) 16)))))
1123 (defun epg--status-TRUST_UNDEFINED (context string)
1124 (let ((signature (car (epg-context-result-for context 'verify))))
1126 (eq (epg-signature-status signature) 'good))
1127 (epg-signature-set-validity signature 'undefined))))
1129 (defun epg--status-TRUST_NEVER (context string)
1130 (let ((signature (car (epg-context-result-for context 'verify))))
1132 (eq (epg-signature-status signature) 'good))
1133 (epg-signature-set-validity signature 'never))))
1135 (defun epg--status-TRUST_MARGINAL (context string)
1136 (let ((signature (car (epg-context-result-for context 'verify))))
1138 (eq (epg-signature-status signature) 'marginal))
1139 (epg-signature-set-validity signature 'marginal))))
1141 (defun epg--status-TRUST_FULLY (context string)
1142 (let ((signature (car (epg-context-result-for context 'verify))))
1144 (eq (epg-signature-status signature) 'good))
1145 (epg-signature-set-validity signature 'full))))
1147 (defun epg--status-TRUST_ULTIMATE (context string)
1148 (let ((signature (car (epg-context-result-for context 'verify))))
1150 (eq (epg-signature-status signature) 'good))
1151 (epg-signature-set-validity signature 'ultimate))))
1153 (defun epg--status-PROGRESS (context string)
1154 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1156 (funcall (if (consp (epg-context-progress-callback context))
1157 (car (epg-context-progress-callback context))
1158 (epg-context-progress-callback context))
1160 (match-string 1 string)
1161 (match-string 2 string)
1162 (string-to-number (match-string 3 string))
1163 (string-to-number (match-string 4 string))
1164 (if (consp (epg-context-progress-callback context))
1165 (cdr (epg-context-progress-callback context))))))
1167 (defun epg--status-DECRYPTION_FAILED (context string)
1168 (epg-context-set-result-for
1170 (cons '(decryption-failed)
1171 (epg-context-result-for context 'error))))
1173 (defun epg--status-NODATA (context string)
1174 (epg-context-set-result-for
1176 (cons (list 'no-data (cons 'reason (string-to-number string)))
1177 (epg-context-result-for context 'error))))
1179 (defun epg--status-UNEXPECTED (context string)
1180 (epg-context-set-result-for
1182 (cons (list 'unexpected (cons 'reason (string-to-number string)))
1183 (epg-context-result-for context 'error))))
1185 (defun epg--status-KEYEXPIRED (context string)
1186 (epg-context-set-result-for
1188 (cons (list 'key-expired (cons 'expiration-time
1189 (epg--time-from-seconds string)))
1190 (epg-context-result-for context 'error))))
1192 (defun epg--status-KEYREVOKED (context string)
1193 (epg-context-set-result-for
1195 (cons '(key-revoked)
1196 (epg-context-result-for context 'error))))
1198 (defun epg--status-BADARMOR (context string)
1199 (epg-context-set-result-for
1202 (epg-context-result-for context 'error))))
1204 (defun epg--status-INV_RECP (context string)
1205 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1206 (epg-context-set-result-for
1208 (cons (list 'invalid-recipient
1210 (string-to-number (match-string 1 string)))
1211 (cons 'requested-recipient
1212 (match-string 2 string)))
1213 (epg-context-result-for context 'error)))))
1215 (defun epg--status-NO_RECP (context string)
1216 (epg-context-set-result-for
1218 (cons '(no-recipients)
1219 (epg-context-result-for context 'error))))
1221 (defun epg--status-DELETE_PROBLEM (context string)
1222 (if (string-match "\\`\\([0-9]+\\)" string)
1223 (epg-context-set-result-for
1225 (cons (list 'delete-problem
1226 (cons 'reason (string-to-number (match-string 1 string))))
1227 (epg-context-result-for context 'error)))))
1229 (defun epg--status-SIG_CREATED (context string)
1230 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1231 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1232 (epg-context-set-result-for
1234 (cons (epg-make-new-signature
1235 (cdr (assq (aref (match-string 1 string) 0)
1236 epg-new-signature-type-alist))
1237 (string-to-number (match-string 2 string))
1238 (string-to-number (match-string 3 string))
1239 (string-to-number (match-string 4 string) 16)
1240 (epg--time-from-seconds (match-string 5 string))
1241 (substring string (match-end 0)))
1242 (epg-context-result-for context 'sign)))))
1244 (defun epg--status-KEY_CREATED (context string)
1245 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1246 (epg-context-set-result-for
1247 context 'generate-key
1248 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1249 (cons 'fingerprint (match-string 2 string)))
1250 (epg-context-result-for context 'generate-key)))))
1252 (defun epg--status-KEY_NOT_CREATED (context string)
1253 (epg-context-set-result-for
1255 (cons '(key-not-created)
1256 (epg-context-result-for context 'error))))
1258 (defun epg--status-IMPORTED (context string)
1259 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1260 (let* ((key-id (match-string 1 string))
1261 (user-id (match-string 2 string))
1262 (entry (assoc key-id epg-user-id-alist)))
1264 (setcdr entry user-id)
1265 (setq epg-user-id-alist (cons (cons key-id user-id)
1266 epg-user-id-alist)))
1267 (epg-context-set-result-for
1269 (cons (list (cons 'key-id key-id)
1270 (cons 'user-id user-id))
1271 (epg-context-result-for context 'import))))))
1273 (defun epg--status-IMPORT_OK (context string)
1274 (let ((result (epg-context-result-for context 'import)))
1276 (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string))
1278 (append (list (cons 'reason
1280 (match-string 1 string))))
1281 (if (match-beginning 2)
1282 (list (cons 'fingerprint
1283 (match-string 3 string))))
1286 (defun epg--status-IMPORT_PROBLEM (context string)
1287 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1288 (epg-context-set-result-for
1290 (cons (cons 'import-problem
1291 (append (list (cons 'reason
1293 (match-string 1 string))))
1294 (if (match-beginning 2)
1295 (list (cons 'fingerprint
1296 (match-string 3 string))))))
1297 (epg-context-result-for context 'error)))))
1299 (defun epg-passphrase-callback-function (context key-id handback)
1300 (if (eq key-id 'SYM)
1301 (read-passwd "Passphrase for symmetric encryption: "
1302 (eq (epg-context-operation context) 'encrypt))
1304 (if (eq key-id 'PIN)
1305 "Passphrase for PIN: "
1306 (let ((entry (assoc key-id epg-user-id-alist)))
1308 (format "Passphrase for %s %s: " key-id (cdr entry))
1309 (format "Passphrase for %s: " key-id)))))))
1311 (defun epg-progress-callback-function (context what char current total
1313 (message "%s: %d%%/%d%%" what current total))
1316 (defun epg-configuration ()
1317 "Return a list of internal configuration parameters of `epg-gpg-program'."
1320 (apply #'call-process epg-gpg-program nil (list t nil) nil
1321 '("--with-colons" "--list-config"))
1322 (goto-char (point-min))
1323 (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
1324 (setq type (intern (match-string 1))
1325 config (cons (cons type
1327 '(pubkey cipher digest compress))
1328 (mapcar #'string-to-number
1329 (delete "" (split-string
1337 (defun epg-check-configuration (config)
1338 "Verify that CONFIGURATION is sufficient."
1339 (let ((entry (assq 'version config))
1342 (stringp (cdr entry))
1343 (string-match "\\`\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)"
1345 (error "Undetermined version: %S" entry))
1346 (setq major (string-to-number (match-string 1 (cdr entry)))
1347 minor (string-to-number (match-string 2 (cdr entry)))
1348 teeny (string-to-number (match-string 3 (cdr entry))))
1349 (unless (or (> major 1)
1354 (error "Unsupported version: %s" (cdr entry)))))
1356 (defun epg--list-keys-1 (context name mode)
1357 (let ((args (append (list "--with-colons" "--no-greeting" "--batch"
1358 "--with-fingerprint"
1359 "--with-fingerprint"
1360 (if (memq mode '(t secret))
1361 "--list-secret-keys"
1362 (if (memq mode '(nil public))
1365 (unless (eq (epg-context-protocol context) 'CMS)
1366 '("--fixed-list-mode"))
1367 (if name (list name))))
1368 keys string field index)
1370 (apply #'call-process
1371 (if (eq (epg-context-protocol context) 'CMS)
1374 nil (list t nil) nil args)
1375 (goto-char (point-min))
1376 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1377 (setq keys (cons (make-vector 15 nil) keys)
1378 string (match-string 0)
1382 (string-match "\\([^:]+\\)?:" string index))
1383 (setq index (match-end 0))
1384 (aset (car keys) field (match-string 1 string))
1385 (setq field (1+ field))))
1388 (defun epg--make-sub-key-1 (line)
1391 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1393 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1395 (member (aref line 0) '("sec" "ssb"))
1396 (string-to-number (aref line 3))
1397 (string-to-number (aref line 2))
1399 (epg--time-from-seconds (aref line 5))
1400 (epg--time-from-seconds (aref line 6))))
1403 (defun epg-list-keys (context &optional name mode)
1404 "Return a list of epg-key objects matched with NAME.
1405 If MODE is nil or 'public, only public keyring should be searched.
1406 If MODE is t or 'secret, only secret keyring should be searched.
1407 Otherwise, only public keyring should be searched and the key
1408 signatures should be included."
1409 (let ((lines (epg--list-keys-1 context name mode))
1410 keys cert pointer pointer-1)
1413 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1414 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1415 keys (cons (epg-make-key
1416 (if (aref (car lines) 8)
1417 (cdr (assq (string-to-char (aref (car lines) 8))
1418 epg-key-validity-alist))))
1420 (epg-key-set-sub-key-list
1422 (cons (epg--make-sub-key-1 (car lines))
1423 (epg-key-sub-key-list (car keys)))))
1424 ((member (aref (car lines) 0) '("sub" "ssb"))
1425 (epg-key-set-sub-key-list
1427 (cons (epg--make-sub-key-1 (car lines))
1428 (epg-key-sub-key-list (car keys)))))
1429 ((equal (aref (car lines) 0) "uid")
1430 (epg-key-set-user-id-list
1432 (cons (epg-make-user-id
1433 (if (aref (car lines) 1)
1434 (cdr (assq (string-to-char (aref (car lines) 1))
1435 epg-key-validity-alist)))
1438 (epg-dn-from-string (aref (car lines) 9))
1439 (error (aref (car lines) 9)))
1440 (aref (car lines) 9)))
1441 (epg-key-user-id-list (car keys)))))
1442 ((equal (aref (car lines) 0) "fpr")
1443 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1444 (aref (car lines) 9)))
1445 ((equal (aref (car lines) 0) "sig")
1446 (epg-user-id-set-signature-list
1447 (car (epg-key-user-id-list (car keys)))
1449 (epg-make-key-signature
1450 (if (aref (car lines) 1)
1451 (cdr (assq (string-to-char (aref (car lines) 1))
1452 epg-key-validity-alist)))
1453 (string-to-number (aref (car lines) 3))
1454 (aref (car lines) 4)
1455 (epg--time-from-seconds (aref (car lines) 5))
1456 (epg--time-from-seconds (aref (car lines) 6))
1457 (aref (car lines) 9)
1458 (string-to-number (aref (car lines) 10) 16)
1459 (eq (aref (aref (car lines) 10) 2) ?x))
1460 (epg-user-id-signature-list
1461 (car (epg-key-user-id-list (car keys))))))))
1462 (setq lines (cdr lines)))
1463 (setq keys (nreverse keys)
1466 (epg-key-set-sub-key-list
1468 (nreverse (epg-key-sub-key-list (car pointer))))
1469 (setq pointer-1 (epg-key-set-user-id-list
1471 (nreverse (epg-key-user-id-list (car pointer)))))
1473 (epg-user-id-set-signature-list
1475 (nreverse (epg-user-id-signature-list (car pointer-1))))
1476 (setq pointer-1 (cdr pointer-1)))
1477 (setq pointer (cdr pointer)))
1480 (if (fboundp 'make-temp-file)
1481 (defalias 'epg--make-temp-file 'make-temp-file)
1482 (defvar temporary-file-directory)
1483 ;; stolen from poe.el.
1484 (defun epg--make-temp-file (prefix)
1485 "Create a temporary file.
1486 The returned file name (created by appending some random characters at the end
1487 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1488 is guaranteed to point to a newly created empty file.
1489 You can then use `write-region' to write new data into the file."
1490 (let (tempdir tempfile)
1491 (setq prefix (expand-file-name prefix
1492 (if (featurep 'xemacs)
1494 temporary-file-directory)))
1497 ;; First, create a temporary directory.
1498 (while (condition-case ()
1500 (setq tempdir (make-temp-name
1502 (file-name-directory prefix)
1504 ;; return nil or signal an error.
1505 (make-directory tempdir))
1507 (file-already-exists t)))
1508 (set-file-modes tempdir 448)
1509 ;; Second, create a temporary file in the tempdir.
1510 ;; There *is* a race condition between `make-temp-name'
1511 ;; and `write-region', but we don't care it since we are
1512 ;; in a private directory now.
1513 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1514 (write-region "" nil tempfile nil 'silent)
1515 (set-file-modes tempfile 384)
1516 ;; Finally, make a hard-link from the tempfile.
1517 (while (condition-case ()
1519 (setq file (make-temp-name prefix))
1520 ;; return nil or signal an error.
1521 (add-name-to-file tempfile file))
1523 (file-already-exists t)))
1525 ;; Cleanup the tempfile.
1527 (file-exists-p tempfile)
1528 (delete-file tempfile))
1529 ;; Cleanup the tempdir.
1531 (file-directory-p tempdir)
1532 (delete-directory tempdir))))))
1535 (defun epg-cancel (context)
1536 (if (buffer-live-p (process-buffer (epg-context-process context)))
1538 (set-buffer (process-buffer (epg-context-process context)))
1539 (epg-context-set-result-for
1542 (epg-context-result-for epg-context 'error)))))
1543 (if (eq (process-status (epg-context-process context)) 'run)
1544 (delete-process (epg-context-process context))))
1547 (defun epg-start-decrypt (context cipher)
1548 "Initiate a decrypt operation on CIPHER.
1549 CIPHER is a data object.
1551 If you use this function, you will need to wait for the completion of
1552 `epg-gpg-program' by using `epg-wait-for-completion' and call
1553 `epg-reset' to clear a temporaly output file.
1554 If you are unsure, use synchronous version of this function
1555 `epg-decrypt-file' or `epg-decrypt-string' instead."
1556 (unless (epg-data-file cipher)
1557 (error "Not a file"))
1558 (epg-context-set-operation context 'decrypt)
1559 (epg-context-set-result context nil)
1560 (epg--start context (list "--decrypt" (epg-data-file cipher)))
1561 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1562 (unless (eq (epg-context-protocol context) 'CMS)
1563 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1566 (defun epg-decrypt-file (context cipher plain)
1567 "Decrypt a file CIPHER and store the result to a file PLAIN.
1568 If PLAIN is nil, it returns the result as a string."
1572 (epg-context-set-output-file context plain)
1573 (epg-context-set-output-file context
1574 (epg--make-temp-file "epg-output")))
1575 (epg-start-decrypt context (epg-make-data-from-file cipher))
1576 (epg-wait-for-completion context)
1577 (if (epg-context-result-for context 'error)
1578 (error "Decrypt failed: %S"
1579 (epg-context-result-for context 'error)))
1581 (epg-read-output context)))
1583 (epg-delete-output-file context))
1584 (epg-reset context)))
1587 (defun epg-decrypt-string (context cipher)
1588 "Decrypt a string CIPHER and return the plain text."
1589 (let ((input-file (epg--make-temp-file "epg-input"))
1590 (coding-system-for-write 'binary))
1593 (write-region cipher nil input-file nil 'quiet)
1594 (epg-context-set-output-file context
1595 (epg--make-temp-file "epg-output"))
1596 (epg-start-decrypt context (epg-make-data-from-file input-file))
1597 (epg-wait-for-completion context)
1598 (if (epg-context-result-for context 'error)
1599 (error "Decrypt failed: %S"
1600 (epg-context-result-for context 'error)))
1601 (epg-read-output context))
1602 (epg-delete-output-file context)
1603 (if (file-exists-p input-file)
1604 (delete-file input-file))
1605 (epg-reset context))))
1608 (defun epg-start-verify (context signature &optional signed-text)
1609 "Initiate a verify operation on SIGNATURE.
1610 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1612 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1613 For a normal or a clear text signature, SIGNED-TEXT should be nil.
1615 If you use this function, you will need to wait for the completion of
1616 `epg-gpg-program' by using `epg-wait-for-completion' and call
1617 `epg-reset' to clear a temporaly output file.
1618 If you are unsure, use synchronous version of this function
1619 `epg-verify-file' or `epg-verify-string' instead."
1620 (epg-context-set-operation context 'verify)
1621 (epg-context-set-result context nil)
1623 ;; Detached signature.
1624 (if (epg-data-file signed-text)
1625 (epg--start context (list "--verify" (epg-data-file signature)
1626 (epg-data-file signed-text)))
1627 (epg--start context (list "--verify" (epg-data-file signature) "-"))
1628 (if (eq (process-status (epg-context-process context)) 'run)
1629 (process-send-string (epg-context-process context)
1630 (epg-data-string signed-text)))
1631 (if (eq (process-status (epg-context-process context)) 'run)
1632 (process-send-eof (epg-context-process context))))
1633 ;; Normal (or cleartext) signature.
1634 (if (epg-data-file signature)
1635 (epg--start context (list "--verify" (epg-data-file signature)))
1636 (epg--start context (list "--verify"))
1637 (if (eq (process-status (epg-context-process context)) 'run)
1638 (process-send-string (epg-context-process context)
1639 (epg-data-string signature)))
1640 (if (eq (process-status (epg-context-process context)) 'run)
1641 (process-send-eof (epg-context-process context))))))
1644 (defun epg-verify-file (context signature &optional signed-text plain)
1645 "Verify a file SIGNATURE.
1646 SIGNED-TEXT and PLAIN are also a file if they are specified.
1648 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1649 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1653 (epg-context-set-output-file context plain)
1654 (epg-context-set-output-file context
1655 (epg--make-temp-file "epg-output")))
1657 (epg-start-verify context
1658 (epg-make-data-from-file signature)
1659 (epg-make-data-from-file signed-text))
1660 (epg-start-verify context
1661 (epg-make-data-from-file signature)))
1662 (epg-wait-for-completion context)
1663 ; (if (epg-context-result-for context 'error)
1664 ; (error "Verify failed: %S"
1665 ; (epg-context-result-for context 'error)))
1667 (epg-read-output context)))
1669 (epg-delete-output-file context))
1670 (epg-reset context)))
1673 (defun epg-verify-string (context signature &optional signed-text)
1674 "Verify a string SIGNATURE.
1675 SIGNED-TEXT is a string if it is specified.
1677 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1678 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1679 (let ((coding-system-for-write 'binary)
1683 (epg-context-set-output-file context
1684 (epg--make-temp-file "epg-output"))
1687 (setq input-file (epg--make-temp-file "epg-signature"))
1688 (write-region signature nil input-file nil 'quiet)
1689 (epg-start-verify context
1690 (epg-make-data-from-file input-file)
1691 (epg-make-data-from-string signed-text)))
1692 (epg-start-verify context (epg-make-data-from-string signature)))
1693 (epg-wait-for-completion context)
1694 ; (if (epg-context-result-for context 'error)
1695 ; (error "Verify failed: %S"
1696 ; (epg-context-result-for context 'error)))
1697 (epg-read-output context))
1698 (epg-delete-output-file context)
1700 (file-exists-p input-file))
1701 (delete-file input-file))
1702 (epg-reset context))))
1705 (defun epg-start-sign (context plain &optional mode)
1706 "Initiate a sign operation on PLAIN.
1707 PLAIN is a data object.
1709 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1710 If it is nil or 'normal, it makes a normal signature.
1711 Otherwise, it makes a clear text signature.
1713 If you use this function, you will need to wait for the completion of
1714 `epg-gpg-program' by using `epg-wait-for-completion' and call
1715 `epg-reset' to clear a temporaly output file.
1716 If you are unsure, use synchronous version of this function
1717 `epg-sign-file' or `epg-sign-string' instead."
1718 (epg-context-set-operation context 'sign)
1719 (epg-context-set-result context nil)
1721 (append (list (if (memq mode '(t detached))
1723 (if (memq mode '(nil normal))
1731 (car (epg-key-sub-key-list signer)))))
1732 (epg-context-signers context)))
1733 (if (epg-data-file plain)
1734 (list (epg-data-file plain)))))
1735 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1736 (unless (eq (epg-context-protocol context) 'CMS)
1737 (epg-wait-for-status context '("BEGIN_SIGNING")))
1738 (when (epg-data-string plain)
1739 (if (eq (process-status (epg-context-process context)) 'run)
1740 (process-send-string (epg-context-process context)
1741 (epg-data-string plain)))
1742 (if (eq (process-status (epg-context-process context)) 'run)
1743 (process-send-eof (epg-context-process context)))))
1746 (defun epg-sign-file (context plain signature &optional mode)
1747 "Sign a file PLAIN and store the result to a file SIGNATURE.
1748 If SIGNATURE is nil, it returns the result as a string.
1749 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1750 If it is nil or 'normal, it makes a normal signature.
1751 Otherwise, it makes a clear text signature."
1755 (epg-context-set-output-file context signature)
1756 (epg-context-set-output-file context
1757 (epg--make-temp-file "epg-output")))
1758 (epg-start-sign context (epg-make-data-from-file plain) mode)
1759 (epg-wait-for-completion context)
1760 (unless (epg-context-result-for context 'sign)
1761 (if (epg-context-result-for context 'error)
1762 (error "Sign failed: %S"
1763 (epg-context-result-for context 'error))
1764 (error "Sign failed")))
1766 (epg-read-output context)))
1768 (epg-delete-output-file context))
1769 (epg-reset context)))
1772 (defun epg-sign-string (context plain &optional mode)
1773 "Sign a string PLAIN and return the output as string.
1774 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1775 If it is nil or 'normal, it makes a normal signature.
1776 Otherwise, it makes a clear text signature."
1779 (epg-context-set-output-file context
1780 (epg--make-temp-file "epg-output"))
1781 (epg-start-sign context (epg-make-data-from-string plain) mode)
1782 (epg-wait-for-completion context)
1783 (unless (epg-context-result-for context 'sign)
1784 (if (epg-context-result-for context 'error)
1785 (error "Sign failed: %S"
1786 (epg-context-result-for context 'error))
1787 (error "Sign failed")))
1788 (epg-read-output context))
1789 (epg-delete-output-file context)
1790 (epg-reset context)))
1793 (defun epg-start-encrypt (context plain recipients
1794 &optional sign always-trust)
1795 "Initiate an encrypt operation on PLAIN.
1796 PLAIN is a data object.
1797 If RECIPIENTS is nil, it performs symmetric encryption.
1799 If you use this function, you will need to wait for the completion of
1800 `epg-gpg-program' by using `epg-wait-for-completion' and call
1801 `epg-reset' to clear a temporaly output file.
1802 If you are unsure, use synchronous version of this function
1803 `epg-encrypt-file' or `epg-encrypt-string' instead."
1804 (epg-context-set-operation context 'encrypt)
1805 (epg-context-set-result context nil)
1807 (append (if always-trust '("--always-trust"))
1808 (if recipients '("--encrypt") '("--symmetric"))
1816 (car (epg-key-sub-key-list
1818 (epg-context-signers context)))))
1824 (car (epg-key-sub-key-list recipient)))))
1826 (if (epg-data-file plain)
1827 (list (epg-data-file plain)))))
1828 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1829 (unless (eq (epg-context-protocol context) 'CMS)
1831 (epg-wait-for-status context '("BEGIN_SIGNING"))
1832 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
1833 (when (epg-data-string plain)
1834 (if (eq (process-status (epg-context-process context)) 'run)
1835 (process-send-string (epg-context-process context)
1836 (epg-data-string plain)))
1837 (if (eq (process-status (epg-context-process context)) 'run)
1838 (process-send-eof (epg-context-process context)))))
1841 (defun epg-encrypt-file (context plain recipients
1842 cipher &optional sign always-trust)
1843 "Encrypt a file PLAIN and store the result to a file CIPHER.
1844 If CIPHER is nil, it returns the result as a string.
1845 If RECIPIENTS is nil, it performs symmetric encryption."
1849 (epg-context-set-output-file context cipher)
1850 (epg-context-set-output-file context
1851 (epg--make-temp-file "epg-output")))
1852 (epg-start-encrypt context (epg-make-data-from-file plain)
1853 recipients sign always-trust)
1854 (epg-wait-for-completion context)
1856 (not (epg-context-result-for context 'sign)))
1857 (if (epg-context-result-for context 'error)
1858 (error "Sign failed: %S"
1859 (epg-context-result-for context 'error))
1860 (error "Sign failed")))
1861 (if (epg-context-result-for context 'error)
1862 (error "Encrypt failed: %S"
1863 (epg-context-result-for context 'error)))
1865 (epg-read-output context)))
1867 (epg-delete-output-file context))
1868 (epg-reset context)))
1871 (defun epg-encrypt-string (context plain recipients
1872 &optional sign always-trust)
1873 "Encrypt a string PLAIN.
1874 If RECIPIENTS is nil, it performs symmetric encryption."
1877 (epg-context-set-output-file context
1878 (epg--make-temp-file "epg-output"))
1879 (epg-start-encrypt context (epg-make-data-from-string plain)
1880 recipients sign always-trust)
1881 (epg-wait-for-completion context)
1883 (not (epg-context-result-for context 'sign)))
1884 (if (epg-context-result-for context 'error)
1885 (error "Sign failed: %S"
1886 (epg-context-result-for context 'error))
1887 (error "Sign failed")))
1888 (if (epg-context-result-for context 'error)
1889 (error "Encrypt failed: %S"
1890 (epg-context-result-for context 'error)))
1891 (epg-read-output context))
1892 (epg-delete-output-file context)
1893 (epg-reset context)))
1896 (defun epg-start-export-keys (context keys)
1897 "Initiate an export keys operation.
1899 If you use this function, you will need to wait for the completion of
1900 `epg-gpg-program' by using `epg-wait-for-completion' and call
1901 `epg-reset' to clear a temporaly output file.
1902 If you are unsure, use synchronous version of this function
1903 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1904 (epg-context-set-operation context 'export-keys)
1905 (epg-context-set-result context nil)
1906 (epg--start context (cons "--export"
1910 (car (epg-key-sub-key-list key))))
1914 (defun epg-export-keys-to-file (context keys file)
1915 "Extract public KEYS."
1919 (epg-context-set-output-file context file)
1920 (epg-context-set-output-file context
1921 (epg--make-temp-file "epg-output")))
1922 (epg-start-export-keys context keys)
1923 (epg-wait-for-completion context)
1924 (if (epg-context-result-for context 'error)
1925 (error "Export keys failed: %S"
1926 (epg-context-result-for context 'error)))
1928 (epg-read-output context)))
1930 (epg-delete-output-file context))
1931 (epg-reset context)))
1934 (defun epg-export-keys-to-string (context keys)
1935 "Extract public KEYS and return them as a string."
1936 (epg-export-keys-to-file context keys nil))
1939 (defun epg-start-import-keys (context keys)
1940 "Initiate an import keys operation.
1941 KEYS is a data object.
1943 If you use this function, you will need to wait for the completion of
1944 `epg-gpg-program' by using `epg-wait-for-completion' and call
1945 `epg-reset' to clear a temporaly output file.
1946 If you are unsure, use synchronous version of this function
1947 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1948 (epg-context-set-operation context 'import-keys)
1949 (epg-context-set-result context nil)
1950 (epg--start context (if (epg-data-file keys)
1951 (list "--import" (epg-data-file keys))
1953 (when (epg-data-string keys)
1954 (if (eq (process-status (epg-context-process context)) 'run)
1955 (process-send-string (epg-context-process context)
1956 (epg-data-string keys)))
1957 (if (eq (process-status (epg-context-process context)) 'run)
1958 (process-send-eof (epg-context-process context)))))
1960 (defun epg--import-keys-1 (context keys)
1963 (epg-start-import-keys context keys)
1964 (epg-wait-for-completion context)
1965 (if (epg-context-result-for context 'error)
1966 (error "Import keys failed: %S"
1967 (epg-context-result-for context 'error))))
1968 (epg-reset context)))
1971 (defun epg-import-keys-from-file (context keys)
1972 "Add keys from a file KEYS."
1973 (epg--import-keys-1 context (epg-make-data-from-file keys)))
1976 (defun epg-import-keys-from-string (context keys)
1977 "Add keys from a string KEYS."
1978 (epg--import-keys-1 context (epg-make-data-from-string keys)))
1981 (defun epg-start-receive-keys (context key-id-list)
1982 "Initiate a receive key operation.
1983 KEY-ID-LIST is a list of key IDs.
1985 If you use this function, you will need to wait for the completion of
1986 `epg-gpg-program' by using `epg-wait-for-completion' and call
1987 `epg-reset' to clear a temporaly output file.
1988 If you are unsure, use synchronous version of this function
1989 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
1990 (epg-context-set-operation context 'receive-keys)
1991 (epg-context-set-result context nil)
1992 (epg--start context (cons "--recv-keys" key-id-list)))
1995 (defun epg-receive-keys (context keys)
1996 "Add keys from server.
1997 KEYS is a list of key IDs"
2000 (epg-start-receive-keys context keys)
2001 (epg-wait-for-completion context)
2002 (if (epg-context-result-for context 'error)
2003 (error "Receive keys failed: %S"
2004 (epg-context-result-for context 'error))))
2005 (epg-reset context)))
2008 (defalias 'epg-import-keys-from-server 'epg-receive-keys)
2011 (defun epg-start-delete-keys (context keys &optional allow-secret)
2012 "Initiate an delete keys operation.
2014 If you use this function, you will need to wait for the completion of
2015 `epg-gpg-program' by using `epg-wait-for-completion' and call
2016 `epg-reset' to clear a temporaly output file.
2017 If you are unsure, use synchronous version of this function
2018 `epg-delete-keys' instead."
2019 (epg-context-set-operation context 'delete-keys)
2020 (epg-context-set-result context nil)
2021 (epg--start context (cons (if allow-secret
2022 "--delete-secret-key"
2027 (car (epg-key-sub-key-list key))))
2031 (defun epg-delete-keys (context keys &optional allow-secret)
2032 "Delete KEYS from the key ring."
2035 (epg-start-delete-keys context keys allow-secret)
2036 (epg-wait-for-completion context)
2037 (if (epg-context-result-for context 'error)
2038 (error "Delete keys failed: %S"
2039 (epg-context-result-for context 'error))))
2040 (epg-reset context)))
2043 (defun epg-start-sign-keys (context keys &optional local)
2044 "Initiate an sign keys operation.
2046 If you use this function, you will need to wait for the completion of
2047 `epg-gpg-program' by using `epg-wait-for-completion' and call
2048 `epg-reset' to clear a temporaly output file.
2049 If you are unsure, use synchronous version of this function
2050 `epg-sign-keys' instead."
2051 (epg-context-set-operation context 'sign-keys)
2052 (epg-context-set-result context nil)
2053 (epg--start context (cons (if local
2059 (car (epg-key-sub-key-list key))))
2063 (defun epg-sign-keys (context keys &optional local)
2064 "Sign KEYS from the key ring."
2067 (epg-start-sign-keys context keys local)
2068 (epg-wait-for-completion context)
2069 (if (epg-context-result-for context 'error)
2070 (error "Sign keys failed: %S"
2071 (epg-context-result-for context 'error))))
2072 (epg-reset context)))
2075 (defun epg-start-generate-key (context parameters)
2076 "Initiate a key generation.
2077 PARAMETERS specifies parameters for the key.
2079 If you use this function, you will need to wait for the completion of
2080 `epg-gpg-program' by using `epg-wait-for-completion' and call
2081 `epg-reset' to clear a temporaly output file.
2082 If you are unsure, use synchronous version of this function
2083 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2084 (epg-context-set-operation context 'generate-key)
2085 (epg-context-set-result context nil)
2086 (if (epg-data-file parameters)
2087 (epg--start context (list "--batch" "--genkey"
2088 (epg-data-file parameters)))
2089 (epg--start context '("--batch" "--genkey"))
2090 (if (eq (process-status (epg-context-process context)) 'run)
2091 (process-send-string (epg-context-process context)
2092 (epg-data-string parameters)))
2093 (if (eq (process-status (epg-context-process context)) 'run)
2094 (process-send-eof (epg-context-process context)))))
2097 (defun epg-generate-key-from-file (context parameters)
2098 "Generate a new key pair.
2099 PARAMETERS is a file which tells how to create the key."
2102 (epg-start-generate-key context (epg-make-data-from-file parameters))
2103 (epg-wait-for-completion context)
2104 (if (epg-context-result-for context 'error)
2105 (error "Generate key failed: %S"
2106 (epg-context-result-for context 'error))))
2107 (epg-reset context)))
2110 (defun epg-generate-key-from-string (context parameters)
2111 "Generate a new key pair.
2112 PARAMETERS is a string which tells how to create the key."
2115 (epg-start-generate-key context (epg-make-data-from-string parameters))
2116 (epg-wait-for-completion context)
2117 (if (epg-context-result-for context 'error)
2118 (error "Generate key failed: %S"
2119 (epg-context-result-for context 'error))))
2120 (epg-reset context)))
2122 (defun epg--decode-hexstring (string)
2124 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
2125 (setq string (replace-match "\\x\\&" t nil string)
2127 (car (read-from-string (concat "\"" string "\"")))))
2129 (defun epg--decode-quotedstring (string)
2131 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2132 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
2134 (if (match-beginning 2)
2135 (setq string (replace-match "\\2" t nil string)
2137 (if (match-beginning 3)
2138 (setq string (replace-match "\\x\\3" t nil string)
2140 (setq string (replace-match "\\\\\\\\\\4" t nil string)
2141 index (+ index 3)))))
2142 (car (read-from-string (concat "\"" string "\"")))))
2144 (defun epg-dn-from-string (string)
2145 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2146 The return value is an alist mapping from types to values."
2148 (length (length string))
2149 alist type value group)
2150 (while (< index length)
2151 (if (eq index (string-match "[ \t\n\r]*" string index))
2152 (setq index (match-end 0)))
2153 (if (eq index (string-match
2154 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2156 (setq type (match-string 1 string)
2157 index (match-end 0))
2158 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2160 (setq type (match-string 1 string)
2161 index (match-end 0))))
2163 (error "Invalid type"))
2164 (if (eq index (string-match
2165 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2167 (setq index (match-end 0)
2168 value (epg--decode-quotedstring (match-string 0 string)))
2169 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
2170 (setq index (match-end 0)
2171 value (epg--decode-hexstring (match-string 1 string)))
2172 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2174 (setq index (match-end 0)
2175 value (epg--decode-quotedstring
2176 (match-string 0 string))))))
2178 (if (stringp (car (car alist)))
2179 (setcar alist (list (cons type value) (car alist)))
2180 (setcar alist (cons (cons type value) (car alist))))
2181 (if (consp (car (car alist)))
2182 (setcar alist (nreverse (car alist))))
2183 (setq alist (cons (cons type value) alist)
2186 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
2187 (setq index (match-end 0)
2188 group (eq (aref string (match-beginning 1)) ?+))))
2191 (defun epg-decode-dn (alist)
2192 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2193 Type names are resolved using `epg-dn-type-alist'."
2196 (if (stringp (car rdn))
2197 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
2199 (format "%s=%s" (cdr entry) (cdr rdn))
2200 (format "%s=%s" (car rdn) (cdr rdn))))
2201 (concat "(" (epg-decode-dn rdn) ")")))
2207 ;;; epg.el ends here