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.
30 (defvar epg-user-id nil
31 "GnuPG ID of your default identity.")
33 (defvar epg-user-id-alist nil
34 "An alist mapping from key ID to user ID.")
36 (defvar epg-read-point nil)
37 (defvar epg-process-filter-running nil)
38 (defvar epg-pending-status-list nil)
39 (defvar epg-key-id nil)
40 (defvar epg-context nil)
41 (defvar epg-debug-buffer nil)
43 ;; from gnupg/include/cipher.h
44 (defconst epg-cipher-algorithm-alist
56 ;; from gnupg/include/cipher.h
57 (defconst epg-pubkey-algorithm-alist
65 ;; from gnupg/include/cipher.h
66 (defconst epg-digest-algorithm-alist
74 ;; from gnupg/include/cipher.h
75 (defconst epg-compress-algorithm-alist
81 (defconst epg-invalid-recipients-reason-alist
82 '((0 . "No specific reason given")
84 (2 . "Ambigious specification")
85 (3 . "Wrong key usage")
90 (8 . "Policy mismatch")
91 (9 . "Not a secret key")
92 (10 . "Key not trusted")))
94 (defconst epg-delete-problem-reason-alist
96 (2 . "Must delete secret key first")
97 (3 . "Ambigious specification")))
99 (defconst epg-import-ok-reason-alist
100 '((0 . "Not actually changed")
101 (1 . "Entirely new key")
103 (4 . "New signatures")
105 (16 . "Contains private key")))
107 (defconst epg-import-problem-reason-alist
108 '((0 . "No specific reason given")
109 (1 . "Invalid Certificate")
110 (2 . "Issuer Certificate missing")
111 (3 . "Certificate Chain too long")
112 (4 . "Error storing certificate")))
114 (defconst epg-no-data-reason-alist
115 '((1 . "No armored data")
116 (2 . "Expected a packet but did not found one")
117 (3 . "Invalid packet found, this may indicate a non OpenPGP message")
118 (4 . "Signature expected but not found")))
120 (defconst epg-unexpected-reason-alist nil)
122 (defvar epg-key-validity-alist
135 (defvar epg-key-capablity-alist
139 (?a . authentication)))
141 (defvar epg-new-signature-type-alist
146 (defvar epg-dn-type-alist
147 '(("1.2.840.113549.1.9.1" . "EMail")
151 ("0.2.262.1.10.7.20" . "NameDistinguisher")
152 ("2.5.4.16" . "ADDR")
155 ("2.5.4.17" . "PostalCode")
156 ("2.5.4.65" . "Pseudo")
157 ("2.5.4.5" . "SerialNumber")))
159 (defvar epg-prompt-alist nil)
161 (defun epg-make-data-from-file (file)
162 "Make a data object from FILE."
163 (cons 'epg-data (vector file nil)))
165 (defun epg-make-data-from-string (string)
166 "Make a data object from STRING."
167 (cons 'epg-data (vector nil string)))
169 (defun epg-data-file (data)
170 "Return the file of DATA."
171 (unless (eq (car data) 'epg-data)
172 (signal 'wrong-type-argument (list 'epg-data-p data)))
175 (defun epg-data-string (data)
176 "Return the string of DATA."
177 (unless (eq (car data) 'epg-data)
178 (signal 'wrong-type-argument (list 'epg-data-p data)))
181 (defun epg-make-context (&optional protocol armor textmode include-certs
182 cipher-algorithm digest-algorithm
184 "Return a context object."
186 (vector (or protocol 'OpenPGP) armor textmode include-certs
187 cipher-algorithm digest-algorithm compress-algorithm
188 #'epg-passphrase-callback-function
190 nil nil nil nil nil)))
192 (defun epg-context-protocol (context)
193 "Return the protocol used within CONTEXT."
194 (unless (eq (car context) 'epg-context)
195 (signal 'wrong-type-argument (list 'epg-context-p context)))
196 (aref (cdr context) 0))
198 (defun epg-context-armor (context)
199 "Return t if the output shouled be ASCII armored in CONTEXT."
200 (unless (eq (car context) 'epg-context)
201 (signal 'wrong-type-argument (list 'epg-context-p context)))
202 (aref (cdr context) 1))
204 (defun epg-context-textmode (context)
205 "Return t if canonical text mode should be used in CONTEXT."
206 (unless (eq (car context) 'epg-context)
207 (signal 'wrong-type-argument (list 'epg-context-p context)))
208 (aref (cdr context) 2))
210 (defun epg-context-include-certs (context)
211 "Return how many certificates should be included in an S/MIME signed
213 (unless (eq (car context) 'epg-context)
214 (signal 'wrong-type-argument (list 'epg-context-p context)))
215 (aref (cdr context) 3))
217 (defun epg-context-cipher-algorithm (context)
218 "Return the cipher algorithm in CONTEXT."
219 (unless (eq (car context) 'epg-context)
220 (signal 'wrong-type-argument (list 'epg-context-p context)))
221 (aref (cdr context) 4))
223 (defun epg-context-digest-algorithm (context)
224 "Return the digest algorithm in CONTEXT."
225 (unless (eq (car context) 'epg-context)
226 (signal 'wrong-type-argument (list 'epg-context-p context)))
227 (aref (cdr context) 5))
229 (defun epg-context-compress-algorithm (context)
230 "Return the compress algorithm in CONTEXT."
231 (unless (eq (car context) 'epg-context)
232 (signal 'wrong-type-argument (list 'epg-context-p context)))
233 (aref (cdr context) 6))
235 (defun epg-context-passphrase-callback (context)
236 "Return the function used to query passphrase."
237 (unless (eq (car context) 'epg-context)
238 (signal 'wrong-type-argument (list 'epg-context-p context)))
239 (aref (cdr context) 7))
241 (defun epg-context-progress-callback (context)
242 "Return the function which handles progress update."
243 (unless (eq (car context) 'epg-context)
244 (signal 'wrong-type-argument (list 'epg-context-p context)))
245 (aref (cdr context) 8))
247 (defun epg-context-signers (context)
248 "Return the list of key-id for singning."
249 (unless (eq (car context) 'epg-context)
250 (signal 'wrong-type-argument (list 'epg-context-p context)))
251 (aref (cdr context) 9))
253 (defun epg-context-process (context)
254 "Return the process object of `epg-gpg-program'.
255 This function is for internal use only."
256 (unless (eq (car context) 'epg-context)
257 (signal 'wrong-type-argument (list 'epg-context-p context)))
258 (aref (cdr context) 10))
260 (defun epg-context-output-file (context)
261 "Return the output file of `epg-gpg-program'.
262 This function is for internal use only."
263 (unless (eq (car context) 'epg-context)
264 (signal 'wrong-type-argument (list 'epg-context-p context)))
265 (aref (cdr context) 11))
267 (defun epg-context-result (context)
268 "Return the result of the previous cryptographic operation."
269 (unless (eq (car context) 'epg-context)
270 (signal 'wrong-type-argument (list 'epg-context-p context)))
271 (aref (cdr context) 12))
273 (defun epg-context-operation (context)
274 "Return the name of the current cryptographic operation."
275 (unless (eq (car context) 'epg-context)
276 (signal 'wrong-type-argument (list 'epg-context-p context)))
277 (aref (cdr context) 13))
279 (defun epg-context-set-protocol (context protocol)
280 "Set the protocol used within CONTEXT."
281 (unless (eq (car context) 'epg-context)
282 (signal 'wrong-type-argument (list 'epg-context-p context)))
283 (aset (cdr context) 0 protocol))
285 (defun epg-context-set-armor (context armor)
286 "Specify if the output shouled be ASCII armored in CONTEXT."
287 (unless (eq (car context) 'epg-context)
288 (signal 'wrong-type-argument (list 'epg-context-p context)))
289 (aset (cdr context) 1 armor))
291 (defun epg-context-set-textmode (context textmode)
292 "Specify if canonical text mode should be used in CONTEXT."
293 (unless (eq (car context) 'epg-context)
294 (signal 'wrong-type-argument (list 'epg-context-p context)))
295 (aset (cdr context) 2 textmode))
297 (defun epg-context-set-include-certs (context include-certs)
298 "Set how many certificates should be included in an S/MIME signed message."
299 (unless (eq (car context) 'epg-context)
300 (signal 'wrong-type-argument (list 'epg-context-p context)))
301 (aset (cdr context) 3 include-certs))
303 (defun epg-context-set-cipher-algorithm (context cipher-algorithm)
304 "Set the cipher algorithm in CONTEXT."
305 (unless (eq (car context) 'epg-context)
306 (signal 'wrong-type-argument (list 'epg-context-p context)))
307 (aset (cdr context) 4 cipher-algorithm))
309 (defun epg-context-set-digest-algorithm (context digest-algorithm)
310 "Set the digest algorithm in CONTEXT."
311 (unless (eq (car context) 'epg-context)
312 (signal 'wrong-type-argument (list 'epg-context-p context)))
313 (aset (cdr context) 5 digest-algorithm))
315 (defun epg-context-set-compress-algorithm (context compress-algorithm)
316 "Set the compress algorithm in CONTEXT."
317 (unless (eq (car context) 'epg-context)
318 (signal 'wrong-type-argument (list 'epg-context-p context)))
319 (aset (cdr context) 6 compress-algorithm))
321 (defun epg-context-set-passphrase-callback (context
323 "Set the function used to query passphrase."
324 (unless (eq (car context) 'epg-context)
325 (signal 'wrong-type-argument (list 'epg-context-p context)))
326 (aset (cdr context) 7 passphrase-callback))
328 (defun epg-context-set-progress-callback (context progress-callback)
329 "Set the function which handles progress update."
330 (unless (eq (car context) 'epg-context)
331 (signal 'wrong-type-argument (list 'epg-context-p context)))
332 (aset (cdr context) 8 progress-callback))
334 (defun epg-context-set-signers (context signers)
335 "Set the list of key-id for singning."
336 (unless (eq (car context) 'epg-context)
337 (signal 'wrong-type-argument (list 'epg-context-p context)))
338 (aset (cdr context) 9 signers))
340 (defun epg-context-set-process (context process)
341 "Set the process object of `epg-gpg-program'.
342 This function is for internal use only."
343 (unless (eq (car context) 'epg-context)
344 (signal 'wrong-type-argument (list 'epg-context-p context)))
345 (aset (cdr context) 10 process))
347 (defun epg-context-set-output-file (context output-file)
348 "Set the output file of `epg-gpg-program'.
349 This function is for internal use only."
350 (unless (eq (car context) 'epg-context)
351 (signal 'wrong-type-argument (list 'epg-context-p context)))
352 (aset (cdr context) 11 output-file))
354 (defun epg-context-set-result (context result)
355 "Set the result of the previous cryptographic operation."
356 (unless (eq (car context) 'epg-context)
357 (signal 'wrong-type-argument (list 'epg-context-p context)))
358 (aset (cdr context) 12 result))
360 (defun epg-context-set-operation (context operation)
361 "Set the name of the current cryptographic operation."
362 (unless (eq (car context) 'epg-context)
363 (signal 'wrong-type-argument (list 'epg-context-p context)))
364 (aset (cdr context) 13 operation))
366 (defun epg-make-signature (status &optional key-id)
367 "Return a signature object."
368 (cons 'epg-signature (vector status key-id nil nil nil nil nil nil nil nil)))
370 (defun epg-signature-status (signature)
371 "Return the status code of SIGNATURE."
372 (unless (eq (car signature) 'epg-signature)
373 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
374 (aref (cdr signature) 0))
376 (defun epg-signature-key-id (signature)
377 "Return the key-id of SIGNATURE."
378 (unless (eq (car signature) 'epg-signature)
379 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
380 (aref (cdr signature) 1))
382 (defun epg-signature-validity (signature)
383 "Return the validity of SIGNATURE."
384 (unless (eq (car signature) 'epg-signature)
385 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
386 (aref (cdr signature) 2))
388 (defun epg-signature-fingerprint (signature)
389 "Return the fingerprint of SIGNATURE."
390 (unless (eq (car signature) 'epg-signature)
391 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
392 (aref (cdr signature) 3))
394 (defun epg-signature-creation-time (signature)
395 "Return the creation time of SIGNATURE."
396 (unless (eq (car signature) 'epg-signature)
397 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
398 (aref (cdr signature) 4))
400 (defun epg-signature-expiration-time (signature)
401 "Return the expiration time of SIGNATURE."
402 (unless (eq (car signature) 'epg-signature)
403 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
404 (aref (cdr signature) 5))
406 (defun epg-signature-pubkey-algorithm (signature)
407 "Return the public key algorithm of SIGNATURE."
408 (unless (eq (car signature) 'epg-signature)
409 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
410 (aref (cdr signature) 6))
412 (defun epg-signature-digest-algorithm (signature)
413 "Return the digest algorithm of SIGNATURE."
414 (unless (eq (car signature) 'epg-signature)
415 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
416 (aref (cdr signature) 7))
418 (defun epg-signature-class (signature)
419 "Return the class of SIGNATURE."
420 (unless (eq (car signature) 'epg-signature)
421 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
422 (aref (cdr signature) 8))
424 (defun epg-signature-version (signature)
425 "Return the version of SIGNATURE."
426 (unless (eq (car signature) 'epg-signature)
427 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
428 (aref (cdr signature) 9))
430 (defun epg-signature-set-status (signature status)
431 "Set the status code of SIGNATURE."
432 (unless (eq (car signature) 'epg-signature)
433 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
434 (aset (cdr signature) 0 status))
436 (defun epg-signature-set-key-id (signature key-id)
437 "Set the key-id of SIGNATURE."
438 (unless (eq (car signature) 'epg-signature)
439 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
440 (aset (cdr signature) 1 key-id))
442 (defun epg-signature-set-validity (signature validity)
443 "Set the validity of SIGNATURE."
444 (unless (eq (car signature) 'epg-signature)
445 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
446 (aset (cdr signature) 2 validity))
448 (defun epg-signature-set-fingerprint (signature fingerprint)
449 "Set the fingerprint of SIGNATURE."
450 (unless (eq (car signature) 'epg-signature)
451 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
452 (aset (cdr signature) 3 fingerprint))
454 (defun epg-signature-set-creation-time (signature creation-time)
455 "Set the creation time of SIGNATURE."
456 (unless (eq (car signature) 'epg-signature)
457 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
458 (aset (cdr signature) 4 creation-time))
460 (defun epg-signature-set-expiration-time (signature expiration-time)
461 "Set the expiration time of SIGNATURE."
462 (unless (eq (car signature) 'epg-signature)
463 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
464 (aset (cdr signature) 5 expiration-time))
466 (defun epg-signature-set-pubkey-algorithm (signature pubkey-algorithm)
467 "Set the public key algorithm of SIGNATURE."
468 (unless (eq (car signature) 'epg-signature)
469 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
470 (aset (cdr signature) 6 pubkey-algorithm))
472 (defun epg-signature-set-digest-algorithm (signature digest-algorithm)
473 "Set the digest algorithm of SIGNATURE."
474 (unless (eq (car signature) 'epg-signature)
475 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
476 (aset (cdr signature) 7 digest-algorithm))
478 (defun epg-signature-set-class (signature class)
479 "Set the class of SIGNATURE."
480 (unless (eq (car signature) 'epg-signature)
481 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
482 (aset (cdr signature) 8 class))
484 (defun epg-signature-set-version (signature version)
485 "Set the version of SIGNATURE."
486 (unless (eq (car signature) 'epg-signature)
487 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
488 (aset (cdr signature) 9 version))
490 (defun epg-make-new-signature (type pubkey-algorithm digest-algorithm
491 class creation-time fingerprint)
492 "Return a new signature object."
493 (cons 'epg-new-signature (vector type pubkey-algorithm digest-algorithm
494 class creation-time fingerprint)))
496 (defun epg-new-signature-type (new-signature)
497 "Return the type of NEW-SIGNATURE."
498 (unless (eq (car new-signature) 'epg-new-signature)
499 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
500 (aref (cdr new-signature) 0))
502 (defun epg-new-signature-pubkey-algorithm (new-signature)
503 "Return the public key algorithm of NEW-SIGNATURE."
504 (unless (eq (car new-signature) 'epg-new-signature)
505 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
506 (aref (cdr new-signature) 1))
508 (defun epg-new-signature-digest-algorithm (new-signature)
509 "Return the digest algorithm of NEW-SIGNATURE."
510 (unless (eq (car new-signature) 'epg-new-signature)
511 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
512 (aref (cdr new-signature) 2))
514 (defun epg-new-signature-class (new-signature)
515 "Return the class of NEW-SIGNATURE."
516 (unless (eq (car new-signature) 'epg-new-signature)
517 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
518 (aref (cdr new-signature) 3))
520 (defun epg-new-signature-creation-time (new-signature)
521 "Return the creation time of NEW-SIGNATURE."
522 (unless (eq (car new-signature) 'epg-new-signature)
523 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
524 (aref (cdr new-signature) 4))
526 (defun epg-new-signature-fingerprint (new-signature)
527 "Return the fingerprint of NEW-SIGNATURE."
528 (unless (eq (car new-signature) 'epg-new-signature)
529 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
530 (aref (cdr new-signature) 5))
532 (defun epg-make-key (owner-trust)
533 "Return a key object."
534 (cons 'epg-key (vector owner-trust nil nil)))
536 (defun epg-key-owner-trust (key)
537 "Return the owner trust of KEY."
538 (unless (eq (car key) 'epg-key)
539 (signal 'wrong-type-argument (list 'epg-key-p key)))
542 (defun epg-key-sub-key-list (key)
543 "Return the sub key list of KEY."
544 (unless (eq (car key) 'epg-key)
545 (signal 'wrong-type-argument (list 'epg-key-p key)))
548 (defun epg-key-user-id-list (key)
549 "Return the user ID list of KEY."
550 (unless (eq (car key) 'epg-key)
551 (signal 'wrong-type-argument (list 'epg-key-p key)))
554 (defun epg-key-set-sub-key-list (key sub-key-list)
555 "Set the sub key list of KEY."
556 (unless (eq (car key) 'epg-key)
557 (signal 'wrong-type-argument (list 'epg-key-p key)))
558 (aset (cdr key) 1 sub-key-list))
560 (defun epg-key-set-user-id-list (key user-id-list)
561 "Set the user ID list of KEY."
562 (unless (eq (car key) 'epg-key)
563 (signal 'wrong-type-argument (list 'epg-key-p key)))
564 (aset (cdr key) 2 user-id-list))
566 (defun epg-make-sub-key (validity capability secret-p algorithm length id
567 creation-time expiration-time)
568 "Return a sub key object."
570 (vector validity capability secret-p algorithm length id creation-time
571 expiration-time nil)))
573 (defun epg-sub-key-validity (sub-key)
574 "Return the validity of SUB-KEY."
575 (unless (eq (car sub-key) 'epg-sub-key)
576 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
577 (aref (cdr sub-key) 0))
579 (defun epg-sub-key-capability (sub-key)
580 "Return the capability of SUB-KEY."
581 (unless (eq (car sub-key) 'epg-sub-key)
582 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
583 (aref (cdr sub-key) 1))
585 (defun epg-sub-key-secret-p (sub-key)
586 "Return non-nil if SUB-KEY is a secret key."
587 (unless (eq (car sub-key) 'epg-sub-key)
588 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
589 (aref (cdr sub-key) 2))
591 (defun epg-sub-key-algorithm (sub-key)
592 "Return the algorithm of SUB-KEY."
593 (unless (eq (car sub-key) 'epg-sub-key)
594 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
595 (aref (cdr sub-key) 3))
597 (defun epg-sub-key-length (sub-key)
598 "Return the length of SUB-KEY."
599 (unless (eq (car sub-key) 'epg-sub-key)
600 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
601 (aref (cdr sub-key) 4))
603 (defun epg-sub-key-id (sub-key)
604 "Return the ID of SUB-KEY."
605 (unless (eq (car sub-key) 'epg-sub-key)
606 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
607 (aref (cdr sub-key) 5))
609 (defun epg-sub-key-creation-time (sub-key)
610 "Return the creation time of SUB-KEY."
611 (unless (eq (car sub-key) 'epg-sub-key)
612 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
613 (aref (cdr sub-key) 6))
615 (defun epg-sub-key-expiration-time (sub-key)
616 "Return the expiration time of SUB-KEY."
617 (unless (eq (car sub-key) 'epg-sub-key)
618 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
619 (aref (cdr sub-key) 7))
621 (defun epg-sub-key-fingerprint (sub-key)
622 "Return the fingerprint of SUB-KEY."
623 (unless (eq (car sub-key) 'epg-sub-key)
624 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
625 (aref (cdr sub-key) 8))
627 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
628 "Set the fingerprint of SUB-KEY.
629 This function is for internal use only."
630 (unless (eq (car sub-key) 'epg-sub-key)
631 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
632 (aset (cdr sub-key) 8 fingerprint))
634 (defun epg-make-user-id (validity string)
635 "Return a user ID object."
636 (cons 'epg-user-id (vector validity string nil)))
638 (defun epg-user-id-validity (user-id)
639 "Return the validity of USER-ID."
640 (unless (eq (car user-id) 'epg-user-id)
641 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
642 (aref (cdr user-id) 0))
644 (defun epg-user-id-string (user-id)
645 "Return the name of USER-ID."
646 (unless (eq (car user-id) 'epg-user-id)
647 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
648 (aref (cdr user-id) 1))
650 (defun epg-user-id-signature-list (user-id)
651 "Return the signature list of USER-ID."
652 (unless (eq (car user-id) 'epg-user-id)
653 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
654 (aref (cdr user-id) 2))
656 (defun epg-user-id-set-signature-list (user-id signature-list)
657 "Set the signature list of USER-ID."
658 (unless (eq (car user-id) 'epg-user-id)
659 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
660 (aset (cdr user-id) 2 signature-list))
662 (defun epg-make-key-signature (validity pubkey-algorithm key-id creation-time
663 expiration-time user-id class
665 "Return a key signature object."
666 (cons 'epg-key-signature
667 (vector validity pubkey-algorithm key-id creation-time expiration-time
668 user-id class exportable-p)))
670 (defun epg-key-signature-validity (key-signature)
671 "Return the validity of KEY-SIGNATURE."
672 (unless (eq (car key-signature) 'epg-key-signature)
673 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
674 (aref (cdr key-signature) 0))
676 (defun epg-key-signature-pubkey-algorithm (key-signature)
677 "Return the public key algorithm of KEY-SIGNATURE."
678 (unless (eq (car key-signature) 'epg-key-signature)
679 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
680 (aref (cdr key-signature) 1))
682 (defun epg-key-signature-key-id (key-signature)
683 "Return the key-id of KEY-SIGNATURE."
684 (unless (eq (car key-signature) 'epg-key-signature)
685 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
686 (aref (cdr key-signature) 2))
688 (defun epg-key-signature-creation-time (key-signature)
689 "Return the creation time of KEY-SIGNATURE."
690 (unless (eq (car key-signature) 'epg-key-signature)
691 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
692 (aref (cdr key-signature) 3))
694 (defun epg-key-signature-expiration-time (key-signature)
695 "Return the expiration time of KEY-SIGNATURE."
696 (unless (eq (car key-signature) 'epg-key-signature)
697 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
698 (aref (cdr key-signature) 4))
700 (defun epg-key-signature-user-id (key-signature)
701 "Return the user-id of KEY-SIGNATURE."
702 (unless (eq (car key-signature) 'epg-key-signature)
703 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
704 (aref (cdr key-signature) 5))
706 (defun epg-key-signature-class (key-signature)
707 "Return the class of KEY-SIGNATURE."
708 (unless (eq (car key-signature) 'epg-key-signature)
709 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
710 (aref (cdr key-signature) 6))
712 (defun epg-key-signature-exportable-p (key-signature)
713 "Return t if KEY-SIGNATURE is exportable."
714 (unless (eq (car key-signature) 'epg-key-signature)
715 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
716 (aref (cdr key-signature) 7))
718 (defun epg-context-result-for (context name)
719 "Return the result of CONTEXT associated with NAME."
720 (cdr (assq name (epg-context-result context))))
722 (defun epg-context-set-result-for (context name value)
723 "Set the result of CONTEXT associated with NAME to VALUE."
724 (let* ((result (epg-context-result context))
725 (entry (assq name result)))
728 (epg-context-set-result context (cons (cons name value) result)))))
730 (defun epg-signature-to-string (signature)
731 "Convert SIGNATURE to a human readable string."
732 (let ((user-id (cdr (assoc (epg-signature-key-id signature)
733 epg-user-id-alist))))
735 (cond ((eq (epg-signature-status signature) 'good)
736 "Good signature from ")
737 ((eq (epg-signature-status signature) 'bad)
738 "Bad signature from ")
739 ((eq (epg-signature-status signature) 'expired)
740 "Expired signature from ")
741 ((eq (epg-signature-status signature) 'expired-key)
742 "Signature made by expired key ")
743 ((eq (epg-signature-status signature) 'revoked-key)
744 "Signature made by revoked key ")
745 ((eq (epg-signature-status signature) 'no-pubkey)
746 "No public key for "))
747 (epg-signature-key-id signature)
750 (if (stringp user-id)
752 (epg-decode-dn user-id)))
754 (if (epg-signature-validity signature)
755 (format " (trust %s)" (epg-signature-validity signature))
758 (defun epg-verify-result-to-string (verify-result)
759 "Convert VERIFY-RESULT to a human readable string."
760 (mapconcat #'epg-signature-to-string verify-result "\n"))
762 (defun epg-new-signature-to-string (new-signature)
763 "Convert NEW-SIGNATURE to a human readable string."
765 (cond ((eq (epg-new-signature-type new-signature) 'detached)
766 "Detached signature ")
767 ((eq (epg-new-signature-type new-signature) 'clear)
768 "Clear text signature ")
771 (cdr (assq (epg-new-signature-pubkey-algorithm new-signature)
772 epg-pubkey-algorithm-alist))
774 (cdr (assq (epg-new-signature-digest-algorithm new-signature)
775 epg-digest-algorithm-alist))
777 (format "%02X " (epg-new-signature-class new-signature))
778 (epg-new-signature-fingerprint new-signature)))
780 (defun epg--start (context args)
781 "Start `epg-gpg-program' in a subprocess with given ARGS."
782 (if (and (epg-context-process context)
783 (eq (process-status (epg-context-process context)) 'run))
784 (error "%s is already running in this context"
785 (if (eq (epg-context-protocol context) 'CMS)
788 (let* ((args (append (list "--no-tty"
791 (if (epg-context-progress-callback context)
792 (list "--enable-progress-filter"))
793 (if epg-gpg-home-directory
794 (list "--homedir" epg-gpg-home-directory))
795 (unless (eq (epg-context-protocol context) 'CMS)
796 (list "--command-fd" "0"))
797 (if (epg-context-armor context) '("--armor"))
798 (if (epg-context-textmode context) '("--textmode"))
799 (if (epg-context-output-file context)
800 (list "--output" (epg-context-output-file context)))
802 (coding-system-for-write 'binary)
803 process-connection-type
804 (orig-mode (default-file-modes))
805 (buffer (generate-new-buffer " *epg*"))
809 (unless epg-debug-buffer
810 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
811 (set-buffer epg-debug-buffer)
812 (goto-char (point-max))
813 (insert (format "%s %s\n"
814 (if (eq (epg-context-protocol context) 'CMS)
817 (mapconcat #'identity args " ")))))
818 (with-current-buffer buffer
819 (make-local-variable 'epg-read-point)
820 (setq epg-read-point (point-min))
821 (make-local-variable 'epg-process-filter-running)
822 (setq epg-process-filter-running nil)
823 (make-local-variable 'epg-pending-status-list)
824 (setq epg-pending-status-list nil)
825 (make-local-variable 'epg-key-id)
826 (setq epg-key-id nil)
827 (make-local-variable 'epg-context)
828 (setq epg-context context))
831 (set-default-file-modes 448)
833 (apply #'start-process "epg" buffer
834 (if (eq (epg-context-protocol context) 'CMS)
838 (set-default-file-modes orig-mode))
839 (set-process-filter process #'epg--process-filter)
840 (epg-context-set-process context process)))
842 (defun epg--process-filter (process input)
845 (unless epg-debug-buffer
846 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
847 (set-buffer epg-debug-buffer)
848 (goto-char (point-max))
850 (if (buffer-live-p (process-buffer process))
852 (set-buffer (process-buffer process))
853 (goto-char (point-max))
855 (unless epg-process-filter-running
858 (setq epg-process-filter-running t)
859 (goto-char epg-read-point)
861 (while (looking-at ".*\n") ;the input line finished
862 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
863 (let* ((status (match-string 1))
864 (string (match-string 2))
865 (symbol (intern-soft (concat "epg--status-"
867 (if (member status epg-pending-status-list)
868 (setq epg-pending-status-list nil))
871 (funcall symbol epg-context string))))
873 (setq epg-read-point (point))))
874 (setq epg-process-filter-running nil))))))
876 (defun epg-read-output (context)
877 "Read the output file CONTEXT and return the content as a string."
879 (if (fboundp 'set-buffer-multibyte)
880 (set-buffer-multibyte nil))
881 (if (file-exists-p (epg-context-output-file context))
882 (let ((coding-system-for-read 'binary))
883 (insert-file-contents (epg-context-output-file context))
886 (defun epg-wait-for-status (context status-list)
887 "Wait until one of elements in STATUS-LIST arrives."
888 (with-current-buffer (process-buffer (epg-context-process context))
889 (setq epg-pending-status-list status-list)
890 (while (and (eq (process-status (epg-context-process context)) 'run)
891 epg-pending-status-list)
892 (accept-process-output (epg-context-process context) 1))))
894 (defun epg-wait-for-completion (context)
895 "Wait until the `epg-gpg-program' process completes."
896 (while (eq (process-status (epg-context-process context)) 'run)
897 (accept-process-output (epg-context-process context) 1)))
899 (defun epg-reset (context)
901 (if (and (epg-context-process context)
902 (buffer-live-p (process-buffer (epg-context-process context))))
903 (kill-buffer (process-buffer (epg-context-process context))))
904 (epg-context-set-process context nil))
906 (defun epg-delete-output-file (context)
907 "Delete the output file of CONTEXT."
908 (if (and (epg-context-output-file context)
909 (file-exists-p (epg-context-output-file context)))
910 (delete-file (epg-context-output-file context))))
912 (defun epg--status-USERID_HINT (context string)
913 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
914 (let* ((key-id (match-string 1 string))
915 (user-id (match-string 2 string))
916 (entry (assoc key-id epg-user-id-alist)))
918 (setcdr entry user-id)
919 (setq epg-user-id-alist (cons (cons key-id user-id)
920 epg-user-id-alist))))))
922 (defun epg--status-NEED_PASSPHRASE (context string)
923 (if (string-match "\\`\\([^ ]+\\)" string)
924 (setq epg-key-id (match-string 1 string))))
926 (defun epg--status-NEED_PASSPHRASE_SYM (context string)
927 (setq epg-key-id 'SYM))
929 (defun epg--status-NEED_PASSPHRASE_PIN (context string)
930 (setq epg-key-id 'PIN))
932 (defun epg--status-GET_HIDDEN (context string)
933 (when (and epg-key-id
934 (string-match "\\`passphrase\\." string))
935 (unless (epg-context-passphrase-callback context)
936 (error "passphrase-callback not set"))
939 passphrase-with-new-line
940 encoded-passphrase-with-new-line)
946 (if (consp (epg-context-passphrase-callback context))
947 (car (epg-context-passphrase-callback context))
948 (epg-context-passphrase-callback context))
951 (if (consp (epg-context-passphrase-callback context))
952 (cdr (epg-context-passphrase-callback context)))))
954 (setq passphrase-with-new-line (concat passphrase "\n"))
955 (epg--clear-string passphrase)
956 (setq passphrase nil)
957 (if epg-passphrase-coding-system
959 (setq encoded-passphrase-with-new-line
960 (encode-coding-string
961 passphrase-with-new-line
962 epg-passphrase-coding-system))
963 (epg--clear-string passphrase-with-new-line)
964 (setq passphrase-with-new-line nil))
965 (setq encoded-passphrase-with-new-line
966 passphrase-with-new-line
967 passphrase-with-new-line nil))
968 (process-send-string (epg-context-process context)
969 encoded-passphrase-with-new-line)))
971 (epg-context-set-result-for
974 (epg-context-result-for context 'error)))
975 (delete-process (epg-context-process context))))
977 (epg--clear-string passphrase))
978 (if passphrase-with-new-line
979 (epg--clear-string passphrase-with-new-line))
980 (if encoded-passphrase-with-new-line
981 (epg--clear-string encoded-passphrase-with-new-line))))))
983 (defun epg--status-GET_BOOL (context string)
984 (let ((entry (assoc string epg-prompt-alist))
987 (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
988 (process-send-string (epg-context-process context) "y\n")
989 (process-send-string (epg-context-process context) "n\n"))
991 (epg-context-set-result-for
994 (epg-context-result-for context 'error)))
995 (delete-process (epg-context-process context))))))
997 (defun epg--status-GET_LINE (context string)
998 (let ((entry (assoc string epg-prompt-alist))
1001 (process-send-string (epg-context-process context)
1002 (concat (read-string
1005 (concat string ": ")))
1008 (epg-context-set-result-for
1011 (epg-context-result-for context 'error)))
1012 (delete-process (epg-context-process context))))))
1014 (defun epg--status-*SIG (context status string)
1015 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1016 (let* ((key-id (match-string 1 string))
1017 (user-id (match-string 2 string))
1018 (entry (assoc key-id epg-user-id-alist)))
1019 (epg-context-set-result-for
1022 (cons (epg-make-signature status key-id)
1023 (epg-context-result-for context 'verify)))
1024 (if (eq (epg-context-protocol context) 'CMS)
1026 (setq user-id (epg-dn-from-string user-id))
1029 (setcdr entry user-id)
1030 (setq epg-user-id-alist
1031 (cons (cons key-id user-id) epg-user-id-alist))))
1032 (epg-context-set-result-for
1035 (cons (epg-make-signature status)
1036 (epg-context-result-for context 'verify)))))
1038 (defun epg--status-GOODSIG (context string)
1039 (epg--status-*SIG context 'good string))
1041 (defun epg--status-EXPSIG (context string)
1042 (epg--status-*SIG context 'expired string))
1044 (defun epg--status-EXPKEYSIG (context string)
1045 (epg--status-*SIG context 'expired-key string))
1047 (defun epg--status-REVKEYSIG (context string)
1048 (epg--status-*SIG context 'revoked-key string))
1050 (defun epg--status-BADSIG (context string)
1051 (epg--status-*SIG context 'bad string))
1053 (defun epg--status-NO_PUBKEY (context string)
1054 (let ((signature (car (epg-context-result-for context 'verify))))
1056 (eq (epg-signature-status signature) 'error)
1057 (equal (epg-signature-key-id signature) string))
1058 (epg-signature-set-status signature 'no-pubkey))))
1060 (defun epg--time-from-seconds (seconds)
1061 (let ((number-seconds (string-to-number (concat seconds ".0"))))
1062 (cons (floor (/ number-seconds 65536))
1063 (floor (mod number-seconds 65536)))))
1065 (defun epg--status-ERRSIG (context string)
1066 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1067 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
1069 (let ((signature (epg-make-signature 'error)))
1070 (epg-context-set-result-for
1074 (epg-context-result-for context 'verify)))
1075 (epg-signature-set-key-id
1077 (match-string 1 string))
1078 (epg-signature-set-pubkey-algorithm
1080 (string-to-number (match-string 2 string)))
1081 (epg-signature-set-digest-algorithm
1083 (string-to-number (match-string 3 string)))
1084 (epg-signature-set-class
1086 (string-to-number (match-string 4 string) 16))
1087 (epg-signature-set-creation-time
1089 (epg--time-from-seconds (match-string 5 string))))))
1091 (defun epg--status-VALIDSIG (context string)
1092 (let ((signature (car (epg-context-result-for context 'verify))))
1093 (when (and signature
1094 (eq (epg-signature-status signature) 'good)
1095 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
1096 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
1099 (epg-signature-set-fingerprint
1101 (match-string 1 string))
1102 (epg-signature-set-creation-time
1104 (epg--time-from-seconds (match-string 2 string)))
1105 (epg-signature-set-expiration-time
1107 (epg--time-from-seconds (match-string 3 string)))
1108 (epg-signature-set-version
1110 (string-to-number (match-string 4 string)))
1111 (epg-signature-set-pubkey-algorithm
1113 (string-to-number (match-string 5 string)))
1114 (epg-signature-set-digest-algorithm
1116 (string-to-number (match-string 6 string)))
1117 (epg-signature-set-class
1119 (string-to-number (match-string 7 string) 16)))))
1121 (defun epg--status-TRUST_UNDEFINED (context string)
1122 (let ((signature (car (epg-context-result-for context 'verify))))
1124 (eq (epg-signature-status signature) 'good))
1125 (epg-signature-set-validity signature 'undefined))))
1127 (defun epg--status-TRUST_NEVER (context string)
1128 (let ((signature (car (epg-context-result-for context 'verify))))
1130 (eq (epg-signature-status signature) 'good))
1131 (epg-signature-set-validity signature 'never))))
1133 (defun epg--status-TRUST_MARGINAL (context string)
1134 (let ((signature (car (epg-context-result-for context 'verify))))
1136 (eq (epg-signature-status signature) 'marginal))
1137 (epg-signature-set-validity signature 'marginal))))
1139 (defun epg--status-TRUST_FULLY (context string)
1140 (let ((signature (car (epg-context-result-for context 'verify))))
1142 (eq (epg-signature-status signature) 'good))
1143 (epg-signature-set-validity signature 'full))))
1145 (defun epg--status-TRUST_ULTIMATE (context string)
1146 (let ((signature (car (epg-context-result-for context 'verify))))
1148 (eq (epg-signature-status signature) 'good))
1149 (epg-signature-set-validity signature 'ultimate))))
1151 (defun epg--status-PROGRESS (context string)
1152 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1154 (funcall (if (consp (epg-context-progress-callback context))
1155 (car (epg-context-progress-callback context))
1156 (epg-context-progress-callback context))
1158 (match-string 1 string)
1159 (match-string 2 string)
1160 (string-to-number (match-string 3 string))
1161 (string-to-number (match-string 4 string))
1162 (if (consp (epg-context-progress-callback context))
1163 (cdr (epg-context-progress-callback context))))))
1165 (defun epg--status-DECRYPTION_FAILED (context string)
1166 (epg-context-set-result-for
1168 (cons '(decryption-failed)
1169 (epg-context-result-for context 'error))))
1171 (defun epg--status-NODATA (context string)
1172 (epg-context-set-result-for
1174 (cons (list 'no-data (cons 'reason (string-to-number string)))
1175 (epg-context-result-for context 'error))))
1177 (defun epg--status-UNEXPECTED (context string)
1178 (epg-context-set-result-for
1180 (cons (list 'unexpected (cons 'reason (string-to-number string)))
1181 (epg-context-result-for context 'error))))
1183 (defun epg--status-KEYEXPIRED (context string)
1184 (epg-context-set-result-for
1186 (cons (list 'key-expired (cons 'expiration-time
1187 (epg--time-from-seconds string)))
1188 (epg-context-result-for context 'error))))
1190 (defun epg--status-KEYREVOKED (context string)
1191 (epg-context-set-result-for
1193 (cons '(key-revoked)
1194 (epg-context-result-for context 'error))))
1196 (defun epg--status-BADARMOR (context string)
1197 (epg-context-set-result-for
1200 (epg-context-result-for context 'error))))
1202 (defun epg--status-INV_RECP (context string)
1203 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1204 (epg-context-set-result-for
1206 (cons (list 'invalid-recipient
1208 (string-to-number (match-string 1 string)))
1209 (cons 'requested-recipient
1210 (match-string 2 string)))
1211 (epg-context-result-for context 'error)))))
1213 (defun epg--status-NO_RECP (context string)
1214 (epg-context-set-result-for
1216 (cons '(no-recipients)
1217 (epg-context-result-for context 'error))))
1219 (defun epg--status-DELETE_PROBLEM (context string)
1220 (if (string-match "\\`\\([0-9]+\\)" string)
1221 (epg-context-set-result-for
1223 (cons (list 'delete-problem
1224 (cons 'reason (string-to-number (match-string 1 string))))
1225 (epg-context-result-for context 'error)))))
1227 (defun epg--status-SIG_CREATED (context string)
1228 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1229 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1230 (epg-context-set-result-for
1232 (cons (epg-make-new-signature
1233 (cdr (assq (aref (match-string 1 string) 0)
1234 epg-new-signature-type-alist))
1235 (string-to-number (match-string 2 string))
1236 (string-to-number (match-string 3 string))
1237 (string-to-number (match-string 4 string) 16)
1238 (epg--time-from-seconds (match-string 5 string))
1239 (substring string (match-end 0)))
1240 (epg-context-result-for context 'sign)))))
1242 (defun epg--status-KEY_CREATED (context string)
1243 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1244 (epg-context-set-result-for
1245 context 'generate-key
1246 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1247 (cons 'fingerprint (match-string 2 string)))
1248 (epg-context-result-for context 'generate-key)))))
1250 (defun epg--status-KEY_NOT_CREATED (context string)
1251 (epg-context-set-result-for
1253 (cons '(key-not-created)
1254 (epg-context-result-for context 'error))))
1256 (defun epg--status-IMPORTED (context string)
1257 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1258 (let* ((key-id (match-string 1 string))
1259 (user-id (match-string 2 string))
1260 (entry (assoc key-id epg-user-id-alist)))
1262 (setcdr entry user-id)
1263 (setq epg-user-id-alist (cons (cons key-id user-id)
1264 epg-user-id-alist)))
1265 (epg-context-set-result-for
1267 (cons (list (cons 'key-id key-id)
1268 (cons 'user-id user-id))
1269 (epg-context-result-for context 'import))))))
1271 (defun epg--status-IMPORT_OK (context string)
1272 (let ((result (epg-context-result-for context 'import)))
1274 (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string))
1276 (append (list (cons 'reason
1278 (match-string 1 string))))
1279 (if (match-beginning 2)
1280 (list (cons 'fingerprint
1281 (match-string 3 string))))
1284 (defun epg--status-IMPORT_PROBLEM (context string)
1285 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1286 (epg-context-set-result-for
1288 (cons (cons 'import-problem
1289 (append (list (cons 'reason
1291 (match-string 1 string))))
1292 (if (match-beginning 2)
1293 (list (cons 'fingerprint
1294 (match-string 3 string))))))
1295 (epg-context-result-for context 'error)))))
1297 (defun epg-passphrase-callback-function (context key-id handback)
1298 (if (eq key-id 'SYM)
1299 (read-passwd "Passphrase for symmetric encryption: "
1300 (eq (epg-context-operation context) 'encrypt))
1302 (if (eq key-id 'PIN)
1303 "Passphrase for PIN: "
1304 (let ((entry (assoc key-id epg-user-id-alist)))
1306 (format "Passphrase for %s %s: " key-id (cdr entry))
1307 (format "Passphrase for %s: " key-id)))))))
1309 (make-obsolete 'epg-passphrase-callback-function "Don't use")
1311 (defun epg--list-keys-1 (context name mode)
1312 (let ((args (append (if epg-gpg-home-directory
1313 (list "--homedir" epg-gpg-home-directory))
1314 (list "--with-colons" "--no-greeting" "--batch"
1315 "--with-fingerprint"
1316 "--with-fingerprint"
1317 (if (memq mode '(t secret))
1318 "--list-secret-keys"
1319 (if (memq mode '(nil public))
1322 (unless (eq (epg-context-protocol context) 'CMS)
1323 '("--fixed-list-mode"))
1324 (if name (list name))))
1325 keys string field index)
1327 (apply #'call-process
1328 (if (eq (epg-context-protocol context) 'CMS)
1331 nil (list t nil) nil args)
1332 (goto-char (point-min))
1333 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1334 (setq keys (cons (make-vector 15 nil) keys)
1335 string (match-string 0)
1339 (string-match "\\([^:]+\\)?:" string index))
1340 (setq index (match-end 0))
1341 (aset (car keys) field (match-string 1 string))
1342 (setq field (1+ field))))
1345 (defun epg--make-sub-key-1 (line)
1348 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1350 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1352 (member (aref line 0) '("sec" "ssb"))
1353 (string-to-number (aref line 3))
1354 (string-to-number (aref line 2))
1356 (epg--time-from-seconds (aref line 5))
1357 (epg--time-from-seconds (aref line 6))))
1360 (defun epg-list-keys (context &optional name mode)
1361 "Return a list of epg-key objects matched with NAME.
1362 If MODE is nil or 'public, only public keyring should be searched.
1363 If MODE is t or 'secret, only secret keyring should be searched.
1364 Otherwise, only public keyring should be searched and the key
1365 signatures should be included."
1366 (let ((lines (epg--list-keys-1 context name mode))
1367 keys cert pointer pointer-1)
1370 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1371 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1372 keys (cons (epg-make-key
1373 (if (aref (car lines) 8)
1374 (cdr (assq (string-to-char (aref (car lines) 8))
1375 epg-key-validity-alist))))
1377 (epg-key-set-sub-key-list
1379 (cons (epg--make-sub-key-1 (car lines))
1380 (epg-key-sub-key-list (car keys)))))
1381 ((member (aref (car lines) 0) '("sub" "ssb"))
1382 (epg-key-set-sub-key-list
1384 (cons (epg--make-sub-key-1 (car lines))
1385 (epg-key-sub-key-list (car keys)))))
1386 ((equal (aref (car lines) 0) "uid")
1387 (epg-key-set-user-id-list
1389 (cons (epg-make-user-id
1390 (if (aref (car lines) 1)
1391 (cdr (assq (string-to-char (aref (car lines) 1))
1392 epg-key-validity-alist)))
1395 (epg-dn-from-string (aref (car lines) 9))
1396 (error (aref (car lines) 9)))
1397 (aref (car lines) 9)))
1398 (epg-key-user-id-list (car keys)))))
1399 ((equal (aref (car lines) 0) "fpr")
1400 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1401 (aref (car lines) 9)))
1402 ((equal (aref (car lines) 0) "sig")
1403 (epg-user-id-set-signature-list
1404 (car (epg-key-user-id-list (car keys)))
1406 (epg-make-key-signature
1407 (if (aref (car lines) 1)
1408 (cdr (assq (string-to-char (aref (car lines) 1))
1409 epg-key-validity-alist)))
1410 (string-to-number (aref (car lines) 3))
1411 (aref (car lines) 4)
1412 (epg--time-from-seconds (aref (car lines) 5))
1413 (epg--time-from-seconds (aref (car lines) 6))
1414 (aref (car lines) 9)
1415 (string-to-number (aref (car lines) 10) 16)
1416 (eq (aref (aref (car lines) 10) 2) ?x))
1417 (epg-user-id-signature-list
1418 (car (epg-key-user-id-list (car keys))))))))
1419 (setq lines (cdr lines)))
1420 (setq keys (nreverse keys)
1423 (epg-key-set-sub-key-list
1425 (nreverse (epg-key-sub-key-list (car pointer))))
1426 (setq pointer-1 (epg-key-set-user-id-list
1428 (nreverse (epg-key-user-id-list (car pointer)))))
1430 (epg-user-id-set-signature-list
1432 (nreverse (epg-user-id-signature-list (car pointer-1))))
1433 (setq pointer-1 (cdr pointer-1)))
1434 (setq pointer (cdr pointer)))
1437 (if (fboundp 'make-temp-file)
1438 (defalias 'epg--make-temp-file 'make-temp-file)
1439 (defvar temporary-file-directory)
1440 ;; stolen from poe.el.
1441 (defun epg--make-temp-file (prefix)
1442 "Create a temporary file.
1443 The returned file name (created by appending some random characters at the end
1444 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1445 is guaranteed to point to a newly created empty file.
1446 You can then use `write-region' to write new data into the file."
1447 (let (tempdir tempfile)
1448 (setq prefix (expand-file-name prefix
1449 (if (featurep 'xemacs)
1451 temporary-file-directory)))
1454 ;; First, create a temporary directory.
1455 (while (condition-case ()
1457 (setq tempdir (make-temp-name
1459 (file-name-directory prefix)
1461 ;; return nil or signal an error.
1462 (make-directory tempdir))
1464 (file-already-exists t)))
1465 (set-file-modes tempdir 448)
1466 ;; Second, create a temporary file in the tempdir.
1467 ;; There *is* a race condition between `make-temp-name'
1468 ;; and `write-region', but we don't care it since we are
1469 ;; in a private directory now.
1470 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1471 (write-region "" nil tempfile nil 'silent)
1472 (set-file-modes tempfile 384)
1473 ;; Finally, make a hard-link from the tempfile.
1474 (while (condition-case ()
1476 (setq file (make-temp-name prefix))
1477 ;; return nil or signal an error.
1478 (add-name-to-file tempfile file))
1480 (file-already-exists t)))
1482 ;; Cleanup the tempfile.
1484 (file-exists-p tempfile)
1485 (delete-file tempfile))
1486 ;; Cleanup the tempdir.
1488 (file-directory-p tempdir)
1489 (delete-directory tempdir))))))
1491 (if (fboundp 'clear-string)
1492 (defalias 'epg--clear-string 'clear-string)
1493 (defun epg--clear-string (string)
1494 (fillarray string 0)))
1497 (defun epg-cancel (context)
1498 (if (buffer-live-p (process-buffer (epg-context-process context)))
1500 (set-buffer (process-buffer (epg-context-process context)))
1501 (epg-context-set-result-for
1504 (epg-context-result-for epg-context 'error)))))
1505 (if (eq (process-status (epg-context-process context)) 'run)
1506 (delete-process (epg-context-process context))))
1509 (defun epg-start-decrypt (context cipher)
1510 "Initiate a decrypt operation on CIPHER.
1511 CIPHER must be a file data object.
1513 If you use this function, you will need to wait for the completion of
1514 `epg-gpg-program' by using `epg-wait-for-completion' and call
1515 `epg-reset' to clear a temporaly output file.
1516 If you are unsure, use synchronous version of this function
1517 `epg-decrypt-file' or `epg-decrypt-string' instead."
1518 (unless (epg-data-file cipher)
1519 (error "Not a file"))
1520 (epg-context-set-operation context 'decrypt)
1521 (epg-context-set-result context nil)
1522 (epg--start context (list "--decrypt" "--" (epg-data-file cipher)))
1523 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1524 (unless (eq (epg-context-protocol context) 'CMS)
1525 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1528 (defun epg-decrypt-file (context cipher plain)
1529 "Decrypt a file CIPHER and store the result to a file PLAIN.
1530 If PLAIN is nil, it returns the result as a string."
1534 (epg-context-set-output-file context plain)
1535 (epg-context-set-output-file context
1536 (epg--make-temp-file "epg-output")))
1537 (epg-start-decrypt context (epg-make-data-from-file cipher))
1538 (epg-wait-for-completion context)
1539 (if (epg-context-result-for context 'error)
1540 (error "Decrypt failed: %S"
1541 (epg-context-result-for context 'error)))
1543 (epg-read-output context)))
1545 (epg-delete-output-file context))
1546 (epg-reset context)))
1549 (defun epg-decrypt-string (context cipher)
1550 "Decrypt a string CIPHER and return the plain text."
1551 (let ((input-file (epg--make-temp-file "epg-input"))
1552 (coding-system-for-write 'binary))
1555 (write-region cipher nil input-file nil 'quiet)
1556 (epg-context-set-output-file context
1557 (epg--make-temp-file "epg-output"))
1558 (epg-start-decrypt context (epg-make-data-from-file input-file))
1559 (epg-wait-for-completion context)
1560 (if (epg-context-result-for context 'error)
1561 (error "Decrypt failed: %S"
1562 (epg-context-result-for context 'error)))
1563 (epg-read-output context))
1564 (epg-delete-output-file context)
1565 (if (file-exists-p input-file)
1566 (delete-file input-file))
1567 (epg-reset context))))
1570 (defun epg-start-verify (context signature &optional signed-text)
1571 "Initiate a verify operation on SIGNATURE.
1572 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1574 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1575 For a normal or a clear text signature, SIGNED-TEXT should be nil.
1577 If you use this function, you will need to wait for the completion of
1578 `epg-gpg-program' by using `epg-wait-for-completion' and call
1579 `epg-reset' to clear a temporaly output file.
1580 If you are unsure, use synchronous version of this function
1581 `epg-verify-file' or `epg-verify-string' instead."
1582 (epg-context-set-operation context 'verify)
1583 (epg-context-set-result context nil)
1585 ;; Detached signature.
1586 (if (epg-data-file signed-text)
1587 (epg--start context (list "--verify" "--" (epg-data-file signature)
1588 (epg-data-file signed-text)))
1589 (epg--start context (list "--verify" "--" (epg-data-file signature)
1591 (if (eq (process-status (epg-context-process context)) 'run)
1592 (process-send-string (epg-context-process context)
1593 (epg-data-string signed-text)))
1594 (if (eq (process-status (epg-context-process context)) 'run)
1595 (process-send-eof (epg-context-process context))))
1596 ;; Normal (or cleartext) signature.
1597 (if (epg-data-file signature)
1598 (epg--start context (list "--verify" "--" (epg-data-file signature)))
1599 (epg--start context (list "--verify"))
1600 (if (eq (process-status (epg-context-process context)) 'run)
1601 (process-send-string (epg-context-process context)
1602 (epg-data-string signature)))
1603 (if (eq (process-status (epg-context-process context)) 'run)
1604 (process-send-eof (epg-context-process context))))))
1607 (defun epg-verify-file (context signature &optional signed-text plain)
1608 "Verify a file SIGNATURE.
1609 SIGNED-TEXT and PLAIN are also a file if they are specified.
1611 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1612 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1616 (epg-context-set-output-file context plain)
1617 (epg-context-set-output-file context
1618 (epg--make-temp-file "epg-output")))
1620 (epg-start-verify context
1621 (epg-make-data-from-file signature)
1622 (epg-make-data-from-file signed-text))
1623 (epg-start-verify context
1624 (epg-make-data-from-file signature)))
1625 (epg-wait-for-completion context)
1626 ; (if (epg-context-result-for context 'error)
1627 ; (error "Verify failed: %S"
1628 ; (epg-context-result-for context 'error)))
1630 (epg-read-output context)))
1632 (epg-delete-output-file context))
1633 (epg-reset context)))
1636 (defun epg-verify-string (context signature &optional signed-text)
1637 "Verify a string SIGNATURE.
1638 SIGNED-TEXT is a string if it is specified.
1640 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1641 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1642 (let ((coding-system-for-write 'binary)
1646 (epg-context-set-output-file context
1647 (epg--make-temp-file "epg-output"))
1650 (setq input-file (epg--make-temp-file "epg-signature"))
1651 (write-region signature nil input-file nil 'quiet)
1652 (epg-start-verify context
1653 (epg-make-data-from-file input-file)
1654 (epg-make-data-from-string signed-text)))
1655 (epg-start-verify context (epg-make-data-from-string signature)))
1656 (epg-wait-for-completion context)
1657 ; (if (epg-context-result-for context 'error)
1658 ; (error "Verify failed: %S"
1659 ; (epg-context-result-for context 'error)))
1660 (epg-read-output context))
1661 (epg-delete-output-file context)
1663 (file-exists-p input-file))
1664 (delete-file input-file))
1665 (epg-reset context))))
1668 (defun epg-start-sign (context plain &optional mode)
1669 "Initiate a sign operation on PLAIN.
1670 PLAIN is a data object.
1672 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1673 If it is nil or 'normal, it makes a normal signature.
1674 Otherwise, it makes a clear text signature.
1676 If you use this function, you will need to wait for the completion of
1677 `epg-gpg-program' by using `epg-wait-for-completion' and call
1678 `epg-reset' to clear a temporaly output file.
1679 If you are unsure, use synchronous version of this function
1680 `epg-sign-file' or `epg-sign-string' instead."
1681 (epg-context-set-operation context 'sign)
1682 (epg-context-set-result context nil)
1684 (append (list (if (memq mode '(t detached))
1686 (if (memq mode '(nil normal))
1694 (car (epg-key-sub-key-list signer)))))
1695 (epg-context-signers context)))
1696 (if (epg-data-file plain)
1697 (list "--" (epg-data-file plain)))))
1698 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1699 (unless (eq (epg-context-protocol context) 'CMS)
1700 (epg-wait-for-status context '("BEGIN_SIGNING")))
1701 (when (epg-data-string plain)
1702 (if (eq (process-status (epg-context-process context)) 'run)
1703 (process-send-string (epg-context-process context)
1704 (epg-data-string plain)))
1705 (if (eq (process-status (epg-context-process context)) 'run)
1706 (process-send-eof (epg-context-process context)))))
1709 (defun epg-sign-file (context plain signature &optional mode)
1710 "Sign a file PLAIN and store the result to a file SIGNATURE.
1711 If SIGNATURE is nil, it returns the result as a string.
1712 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1713 If it is nil or 'normal, it makes a normal signature.
1714 Otherwise, it makes a clear text signature."
1718 (epg-context-set-output-file context signature)
1719 (epg-context-set-output-file context
1720 (epg--make-temp-file "epg-output")))
1721 (epg-start-sign context (epg-make-data-from-file plain) mode)
1722 (epg-wait-for-completion context)
1723 (unless (epg-context-result-for context 'sign)
1724 (if (epg-context-result-for context 'error)
1725 (error "Sign failed: %S"
1726 (epg-context-result-for context 'error))
1727 (error "Sign failed")))
1729 (epg-read-output context)))
1731 (epg-delete-output-file context))
1732 (epg-reset context)))
1735 (defun epg-sign-string (context plain &optional mode)
1736 "Sign a string PLAIN and return the output as string.
1737 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1738 If it is nil or 'normal, it makes a normal signature.
1739 Otherwise, it makes a clear text signature."
1742 (epg-context-set-output-file context
1743 (epg--make-temp-file "epg-output"))
1744 (epg-start-sign context (epg-make-data-from-string plain) mode)
1745 (epg-wait-for-completion context)
1746 (unless (epg-context-result-for context 'sign)
1747 (if (epg-context-result-for context 'error)
1748 (error "Sign failed: %S"
1749 (epg-context-result-for context 'error))
1750 (error "Sign failed")))
1751 (epg-read-output context))
1752 (epg-delete-output-file context)
1753 (epg-reset context)))
1756 (defun epg-start-encrypt (context plain recipients
1757 &optional sign always-trust)
1758 "Initiate an encrypt operation on PLAIN.
1759 PLAIN is a data object.
1760 If RECIPIENTS is nil, it performs symmetric encryption.
1762 If you use this function, you will need to wait for the completion of
1763 `epg-gpg-program' by using `epg-wait-for-completion' and call
1764 `epg-reset' to clear a temporaly output file.
1765 If you are unsure, use synchronous version of this function
1766 `epg-encrypt-file' or `epg-encrypt-string' instead."
1767 (epg-context-set-operation context 'encrypt)
1768 (epg-context-set-result context nil)
1770 (append (if always-trust '("--always-trust"))
1771 (if recipients '("--encrypt") '("--symmetric"))
1779 (car (epg-key-sub-key-list
1781 (epg-context-signers context)))))
1787 (car (epg-key-sub-key-list recipient)))))
1789 (if (epg-data-file plain)
1790 (list "--" (epg-data-file plain)))))
1791 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1792 (unless (eq (epg-context-protocol context) 'CMS)
1794 (epg-wait-for-status context '("BEGIN_SIGNING"))
1795 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
1796 (when (epg-data-string plain)
1797 (if (eq (process-status (epg-context-process context)) 'run)
1798 (process-send-string (epg-context-process context)
1799 (epg-data-string plain)))
1800 (if (eq (process-status (epg-context-process context)) 'run)
1801 (process-send-eof (epg-context-process context)))))
1804 (defun epg-encrypt-file (context plain recipients
1805 cipher &optional sign always-trust)
1806 "Encrypt a file PLAIN and store the result to a file CIPHER.
1807 If CIPHER is nil, it returns the result as a string.
1808 If RECIPIENTS is nil, it performs symmetric encryption."
1812 (epg-context-set-output-file context cipher)
1813 (epg-context-set-output-file context
1814 (epg--make-temp-file "epg-output")))
1815 (epg-start-encrypt context (epg-make-data-from-file plain)
1816 recipients sign always-trust)
1817 (epg-wait-for-completion context)
1819 (not (epg-context-result-for context 'sign)))
1820 (if (epg-context-result-for context 'error)
1821 (error "Sign failed: %S"
1822 (epg-context-result-for context 'error))
1823 (error "Sign failed")))
1824 (if (epg-context-result-for context 'error)
1825 (error "Encrypt failed: %S"
1826 (epg-context-result-for context 'error)))
1828 (epg-read-output context)))
1830 (epg-delete-output-file context))
1831 (epg-reset context)))
1834 (defun epg-encrypt-string (context plain recipients
1835 &optional sign always-trust)
1836 "Encrypt a string PLAIN.
1837 If RECIPIENTS is nil, it performs symmetric encryption."
1840 (epg-context-set-output-file context
1841 (epg--make-temp-file "epg-output"))
1842 (epg-start-encrypt context (epg-make-data-from-string plain)
1843 recipients sign always-trust)
1844 (epg-wait-for-completion context)
1846 (not (epg-context-result-for context 'sign)))
1847 (if (epg-context-result-for context 'error)
1848 (error "Sign failed: %S"
1849 (epg-context-result-for context 'error))
1850 (error "Sign failed")))
1851 (if (epg-context-result-for context 'error)
1852 (error "Encrypt failed: %S"
1853 (epg-context-result-for context 'error)))
1854 (epg-read-output context))
1855 (epg-delete-output-file context)
1856 (epg-reset context)))
1859 (defun epg-start-export-keys (context keys)
1860 "Initiate an export keys operation.
1862 If you use this function, you will need to wait for the completion of
1863 `epg-gpg-program' by using `epg-wait-for-completion' and call
1864 `epg-reset' to clear a temporaly output file.
1865 If you are unsure, use synchronous version of this function
1866 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1867 (epg-context-set-operation context 'export-keys)
1868 (epg-context-set-result context nil)
1869 (epg--start context (cons "--export"
1873 (car (epg-key-sub-key-list key))))
1877 (defun epg-export-keys-to-file (context keys file)
1878 "Extract public KEYS."
1882 (epg-context-set-output-file context file)
1883 (epg-context-set-output-file context
1884 (epg--make-temp-file "epg-output")))
1885 (epg-start-export-keys context keys)
1886 (epg-wait-for-completion context)
1887 (if (epg-context-result-for context 'error)
1888 (error "Export keys failed: %S"
1889 (epg-context-result-for context 'error)))
1891 (epg-read-output context)))
1893 (epg-delete-output-file context))
1894 (epg-reset context)))
1897 (defun epg-export-keys-to-string (context keys)
1898 "Extract public KEYS and return them as a string."
1899 (epg-export-keys-to-file context keys nil))
1902 (defun epg-start-import-keys (context keys)
1903 "Initiate an import keys operation.
1904 KEYS is a data object.
1906 If you use this function, you will need to wait for the completion of
1907 `epg-gpg-program' by using `epg-wait-for-completion' and call
1908 `epg-reset' to clear a temporaly output file.
1909 If you are unsure, use synchronous version of this function
1910 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1911 (epg-context-set-operation context 'import-keys)
1912 (epg-context-set-result context nil)
1913 (epg--start context (if (epg-data-file keys)
1914 (list "--import" "--" (epg-data-file keys))
1916 (when (epg-data-string keys)
1917 (if (eq (process-status (epg-context-process context)) 'run)
1918 (process-send-string (epg-context-process context)
1919 (epg-data-string keys)))
1920 (if (eq (process-status (epg-context-process context)) 'run)
1921 (process-send-eof (epg-context-process context)))))
1923 (defun epg--import-keys-1 (context keys)
1926 (epg-start-import-keys context keys)
1927 (epg-wait-for-completion context)
1928 (if (epg-context-result-for context 'error)
1929 (error "Import keys failed: %S"
1930 (epg-context-result-for context 'error))))
1931 (epg-reset context)))
1934 (defun epg-import-keys-from-file (context keys)
1935 "Add keys from a file KEYS."
1936 (epg--import-keys-1 context (epg-make-data-from-file keys)))
1939 (defun epg-import-keys-from-string (context keys)
1940 "Add keys from a string KEYS."
1941 (epg--import-keys-1 context (epg-make-data-from-string keys)))
1944 (defun epg-start-receive-keys (context key-id-list)
1945 "Initiate a receive key operation.
1946 KEY-ID-LIST is a list of key IDs.
1948 If you use this function, you will need to wait for the completion of
1949 `epg-gpg-program' by using `epg-wait-for-completion' and call
1950 `epg-reset' to clear a temporaly output file.
1951 If you are unsure, use synchronous version of this function
1952 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
1953 (epg-context-set-operation context 'receive-keys)
1954 (epg-context-set-result context nil)
1955 (epg--start context (cons "--recv-keys" key-id-list)))
1958 (defun epg-receive-keys (context keys)
1959 "Add keys from server.
1960 KEYS is a list of key IDs"
1963 (epg-start-receive-keys context keys)
1964 (epg-wait-for-completion context)
1965 (if (epg-context-result-for context 'error)
1966 (error "Receive keys failed: %S"
1967 (epg-context-result-for context 'error))))
1968 (epg-reset context)))
1971 (defalias 'epg-import-keys-from-server 'epg-receive-keys)
1974 (defun epg-start-delete-keys (context keys &optional allow-secret)
1975 "Initiate an delete keys operation.
1977 If you use this function, you will need to wait for the completion of
1978 `epg-gpg-program' by using `epg-wait-for-completion' and call
1979 `epg-reset' to clear a temporaly output file.
1980 If you are unsure, use synchronous version of this function
1981 `epg-delete-keys' instead."
1982 (epg-context-set-operation context 'delete-keys)
1983 (epg-context-set-result context nil)
1984 (epg--start context (cons (if allow-secret
1985 "--delete-secret-key"
1990 (car (epg-key-sub-key-list key))))
1994 (defun epg-delete-keys (context keys &optional allow-secret)
1995 "Delete KEYS from the key ring."
1998 (epg-start-delete-keys context keys allow-secret)
1999 (epg-wait-for-completion context)
2000 (if (epg-context-result-for context 'error)
2001 (error "Delete keys failed: %S"
2002 (epg-context-result-for context 'error))))
2003 (epg-reset context)))
2006 (defun epg-start-sign-keys (context keys &optional local)
2007 "Initiate an sign keys operation.
2009 If you use this function, you will need to wait for the completion of
2010 `epg-gpg-program' by using `epg-wait-for-completion' and call
2011 `epg-reset' to clear a temporaly output file.
2012 If you are unsure, use synchronous version of this function
2013 `epg-sign-keys' instead."
2014 (epg-context-set-operation context 'sign-keys)
2015 (epg-context-set-result context nil)
2016 (epg--start context (cons (if local
2022 (car (epg-key-sub-key-list key))))
2026 (defun epg-sign-keys (context keys &optional local)
2027 "Sign KEYS from the key ring."
2030 (epg-start-sign-keys context keys local)
2031 (epg-wait-for-completion context)
2032 (if (epg-context-result-for context 'error)
2033 (error "Sign keys failed: %S"
2034 (epg-context-result-for context 'error))))
2035 (epg-reset context)))
2038 (defun epg-start-generate-key (context parameters)
2039 "Initiate a key generation.
2040 PARAMETERS specifies parameters for the key.
2042 If you use this function, you will need to wait for the completion of
2043 `epg-gpg-program' by using `epg-wait-for-completion' and call
2044 `epg-reset' to clear a temporaly output file.
2045 If you are unsure, use synchronous version of this function
2046 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2047 (epg-context-set-operation context 'generate-key)
2048 (epg-context-set-result context nil)
2049 (if (epg-data-file parameters)
2050 (epg--start context (list "--batch" "--genkey" "--"
2051 (epg-data-file parameters)))
2052 (epg--start context '("--batch" "--genkey"))
2053 (if (eq (process-status (epg-context-process context)) 'run)
2054 (process-send-string (epg-context-process context)
2055 (epg-data-string parameters)))
2056 (if (eq (process-status (epg-context-process context)) 'run)
2057 (process-send-eof (epg-context-process context)))))
2060 (defun epg-generate-key-from-file (context parameters)
2061 "Generate a new key pair.
2062 PARAMETERS is a file which tells how to create the key."
2065 (epg-start-generate-key context (epg-make-data-from-file parameters))
2066 (epg-wait-for-completion context)
2067 (if (epg-context-result-for context 'error)
2068 (error "Generate key failed: %S"
2069 (epg-context-result-for context 'error))))
2070 (epg-reset context)))
2073 (defun epg-generate-key-from-string (context parameters)
2074 "Generate a new key pair.
2075 PARAMETERS is a string which tells how to create the key."
2078 (epg-start-generate-key context (epg-make-data-from-string parameters))
2079 (epg-wait-for-completion context)
2080 (if (epg-context-result-for context 'error)
2081 (error "Generate key failed: %S"
2082 (epg-context-result-for context 'error))))
2083 (epg-reset context)))
2085 (defun epg--decode-hexstring (string)
2087 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
2088 (setq string (replace-match "\\x\\&" t nil string)
2090 (car (read-from-string (concat "\"" string "\"")))))
2092 (defun epg--decode-quotedstring (string)
2094 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2095 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
2097 (if (match-beginning 2)
2098 (setq string (replace-match "\\2" t nil string)
2100 (if (match-beginning 3)
2101 (setq string (replace-match "\\x\\3" t nil string)
2103 (setq string (replace-match "\\\\\\\\\\4" t nil string)
2104 index (+ index 3)))))
2105 (car (read-from-string (concat "\"" string "\"")))))
2107 (defun epg-dn-from-string (string)
2108 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2109 The return value is an alist mapping from types to values."
2111 (length (length string))
2112 alist type value group)
2113 (while (< index length)
2114 (if (eq index (string-match "[ \t\n\r]*" string index))
2115 (setq index (match-end 0)))
2116 (if (eq index (string-match
2117 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2119 (setq type (match-string 1 string)
2120 index (match-end 0))
2121 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2123 (setq type (match-string 1 string)
2124 index (match-end 0))))
2126 (error "Invalid type"))
2127 (if (eq index (string-match
2128 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2130 (setq index (match-end 0)
2131 value (epg--decode-quotedstring (match-string 0 string)))
2132 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
2133 (setq index (match-end 0)
2134 value (epg--decode-hexstring (match-string 1 string)))
2135 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2137 (setq index (match-end 0)
2138 value (epg--decode-quotedstring
2139 (match-string 0 string))))))
2141 (if (stringp (car (car alist)))
2142 (setcar alist (list (cons type value) (car alist)))
2143 (setcar alist (cons (cons type value) (car alist))))
2144 (if (consp (car (car alist)))
2145 (setcar alist (nreverse (car alist))))
2146 (setq alist (cons (cons type value) alist)
2149 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
2150 (setq index (match-end 0)
2151 group (eq (aref string (match-beginning 1)) ?+))))
2154 (defun epg-decode-dn (alist)
2155 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2156 Type names are resolved using `epg-dn-type-alist'."
2159 (if (stringp (car rdn))
2160 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
2162 (format "%s=%s" (cdr entry) (cdr rdn))
2163 (format "%s=%s" (car rdn) (cdr rdn))))
2164 (concat "(" (epg-decode-dn rdn) ")")))
2170 ;;; epg.el ends here