1 ;;; epg.el --- the EasyPG Library
2 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
3 ;; 2005, 2006 Free Software Foundation, Inc.
4 ;; Copyright (C) 2006 Daiki Ueno
6 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Keywords: PGP, GnuPG
9 ;; This file is part of EasyPG.
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
31 (defcustom epg-gpg-program "gpg"
32 "The `gpg' executable."
36 (defcustom epg-gpgsm-program "gpgsm"
37 "The `gpgsm' executable."
41 (defconst epg-version-number "0.0.1")
43 (defvar epg-user-id nil
44 "GnuPG ID of your default identity.")
46 (defvar epg-user-id-alist nil
47 "An alist mapping from key ID to user ID.")
49 (defvar epg-read-point nil)
50 (defvar epg-pending-status-list nil)
51 (defvar epg-key-id nil)
52 (defvar epg-context nil)
53 (defvar epg-debug nil)
54 (defvar epg-debug-buffer nil)
56 ;; from gnupg/include/cipher.h
57 (defconst epg-cipher-algorithm-alist
69 ;; from gnupg/include/cipher.h
70 (defconst epg-pubkey-algorithm-alist
78 ;; from gnupg/include/cipher.h
79 (defconst epg-digest-algorithm-alist
87 ;; from gnupg/include/cipher.h
88 (defconst epg-compress-algorithm-alist
94 (defconst epg-invalid-recipients-reason-alist
95 '((0 . "No specific reason given")
97 (2 . "Ambigious specification")
98 (3 . "Wrong key usage")
103 (8 . "Policy mismatch")
104 (9 . "Not a secret key")
105 (10 . "Key not trusted")))
107 (defconst epg-delete-problem-reason-alist
108 '((1 . "No such key")
109 (2 . "Must delete secret key first")
110 (3 . "Ambigious specification")))
112 (defconst epg-import-ok-reason-alist
113 '((0 . "Not actually changed")
114 (1 . "Entirely new key")
116 (4 . "New signatures")
118 (16 . "Contains private key")))
120 (defconst epg-import-problem-reason-alist
121 '((0 . "No specific reason given")
122 (1 . "Invalid Certificate")
123 (2 . "Issuer Certificate missing")
124 (3 . "Certificate Chain too long")
125 (4 . "Error storing certificate")))
127 (defvar epg-key-validity-alist
140 (defvar epg-key-capablity-alist
144 (?a . authentication)))
146 (defvar epg-new-signature-type-alist
151 (defvar epg-dn-type-alist
152 '(("1.2.840.113549.1.9.1" . "EMail")
156 ("0.2.262.1.10.7.20" . "NameDistinguisher")
157 ("2.5.4.16" . "ADDR")
160 ("2.5.4.17" . "PostalCode")
161 ("2.5.4.65" . "Pseudo")
162 ("2.5.4.5" . "SerialNumber")))
164 (defvar epg-prompt-alist nil)
166 (defun epg-make-data-from-file (file)
167 "Make a data object from FILE."
168 (cons 'epg-data (vector file nil)))
170 (defun epg-make-data-from-string (string)
171 "Make a data object from STRING."
172 (cons 'epg-data (vector nil string)))
174 (defun epg-data-file (data)
175 "Return the file of DATA."
176 (unless (eq (car data) 'epg-data)
177 (signal 'wrong-type-argument (list 'epg-data-p data)))
180 (defun epg-data-string (data)
181 "Return the string of DATA."
182 (unless (eq (car data) 'epg-data)
183 (signal 'wrong-type-argument (list 'epg-data-p data)))
186 (defun epg-make-context (&optional protocol armor textmode include-certs
187 cipher-algorithm digest-algorithm
189 "Return a context object."
191 (vector (or protocol 'OpenPGP) armor textmode include-certs
192 cipher-algorithm digest-algorithm compress-algorithm
193 #'epg-passphrase-callback-function
194 #'epg-progress-callback-function
197 (defun epg-context-protocol (context)
198 "Return the protocol used within CONTEXT."
199 (unless (eq (car context) 'epg-context)
200 (signal 'wrong-type-argument (list 'epg-context-p context)))
201 (aref (cdr context) 0))
203 (defun epg-context-armor (context)
204 "Return t if the output shouled be ASCII armored in CONTEXT."
205 (unless (eq (car context) 'epg-context)
206 (signal 'wrong-type-argument (list 'epg-context-p context)))
207 (aref (cdr context) 1))
209 (defun epg-context-textmode (context)
210 "Return t if canonical text mode should be used in CONTEXT."
211 (unless (eq (car context) 'epg-context)
212 (signal 'wrong-type-argument (list 'epg-context-p context)))
213 (aref (cdr context) 2))
215 (defun epg-context-include-certs (context)
216 "Return how many certificates should be included in an S/MIME signed
218 (unless (eq (car context) 'epg-context)
219 (signal 'wrong-type-argument (list 'epg-context-p context)))
220 (aref (cdr context) 3))
222 (defun epg-context-cipher-algorithm (context)
223 "Return the cipher algorithm in CONTEXT."
224 (unless (eq (car context) 'epg-context)
225 (signal 'wrong-type-argument (list 'epg-context-p context)))
226 (aref (cdr context) 4))
228 (defun epg-context-digest-algorithm (context)
229 "Return the digest algorithm in CONTEXT."
230 (unless (eq (car context) 'epg-context)
231 (signal 'wrong-type-argument (list 'epg-context-p context)))
232 (aref (cdr context) 5))
234 (defun epg-context-compress-algorithm (context)
235 "Return the compress algorithm in CONTEXT."
236 (unless (eq (car context) 'epg-context)
237 (signal 'wrong-type-argument (list 'epg-context-p context)))
238 (aref (cdr context) 6))
240 (defun epg-context-passphrase-callback (context)
241 "Return the function used to query passphrase."
242 (unless (eq (car context) 'epg-context)
243 (signal 'wrong-type-argument (list 'epg-context-p context)))
244 (aref (cdr context) 7))
246 (defun epg-context-progress-callback (context)
247 "Return the function which handles progress update."
248 (unless (eq (car context) 'epg-context)
249 (signal 'wrong-type-argument (list 'epg-context-p context)))
250 (aref (cdr context) 8))
252 (defun epg-context-signers (context)
253 "Return the list of key-id for singning."
254 (unless (eq (car context) 'epg-context)
255 (signal 'wrong-type-argument (list 'epg-context-p context)))
256 (aref (cdr context) 9))
258 (defun epg-context-process (context)
259 "Return the process object of `epg-gpg-program'.
260 This function is for internal use only."
261 (unless (eq (car context) 'epg-context)
262 (signal 'wrong-type-argument (list 'epg-context-p context)))
263 (aref (cdr context) 10))
265 (defun epg-context-output-file (context)
266 "Return the output file of `epg-gpg-program'.
267 This function is for internal use only."
268 (unless (eq (car context) 'epg-context)
269 (signal 'wrong-type-argument (list 'epg-context-p context)))
270 (aref (cdr context) 11))
272 (defun epg-context-result (context)
273 "Return the result of the previous cryptographic operation."
274 (unless (eq (car context) 'epg-context)
275 (signal 'wrong-type-argument (list 'epg-context-p context)))
276 (aref (cdr context) 12))
278 (defun epg-context-set-protocol (context protocol)
279 "Set the protocol used within CONTEXT."
280 (unless (eq (car context) 'epg-context)
281 (signal 'wrong-type-argument (list 'epg-context-p context)))
282 (aset (cdr context) 0 protocol))
284 (defun epg-context-set-armor (context armor)
285 "Specify if the output shouled be ASCII armored in CONTEXT."
286 (unless (eq (car context) 'epg-context)
287 (signal 'wrong-type-argument (list 'epg-context-p context)))
288 (aset (cdr context) 1 armor))
290 (defun epg-context-set-textmode (context textmode)
291 "Specify if canonical text mode should be used in CONTEXT."
292 (unless (eq (car context) 'epg-context)
293 (signal 'wrong-type-argument (list 'epg-context-p context)))
294 (aset (cdr context) 2 textmode))
296 (defun epg-context-set-include-certs (context include-certs)
297 "Set how many certificates should be included in an S/MIME signed message."
298 (unless (eq (car context) 'epg-context)
299 (signal 'wrong-type-argument (list 'epg-context-p context)))
300 (aset (cdr context) 3 include-certs))
302 (defun epg-context-set-cipher-algorithm (context cipher-algorithm)
303 "Set the cipher algorithm in CONTEXT."
304 (unless (eq (car context) 'epg-context)
305 (signal 'wrong-type-argument (list 'epg-context-p context)))
306 (aset (cdr context) 4 cipher-algorithm))
308 (defun epg-context-set-digest-algorithm (context digest-algorithm)
309 "Set the digest algorithm in CONTEXT."
310 (unless (eq (car context) 'epg-context)
311 (signal 'wrong-type-argument (list 'epg-context-p context)))
312 (aset (cdr context) 5 digest-algorithm))
314 (defun epg-context-set-compress-algorithm (context compress-algorithm)
315 "Set the compress algorithm in CONTEXT."
316 (unless (eq (car context) 'epg-context)
317 (signal 'wrong-type-argument (list 'epg-context-p context)))
318 (aset (cdr context) 6 compress-algorithm))
320 (defun epg-context-set-passphrase-callback (context
322 "Set the function used to query passphrase."
323 (unless (eq (car context) 'epg-context)
324 (signal 'wrong-type-argument (list 'epg-context-p context)))
325 (aset (cdr context) 7 passphrase-callback))
327 (defun epg-context-set-progress-callback (context progress-callback)
328 "Set the function which handles progress update."
329 (unless (eq (car context) 'epg-context)
330 (signal 'wrong-type-argument (list 'epg-context-p context)))
331 (aset (cdr context) 8 progress-callback))
333 (defun epg-context-set-signers (context signers)
334 "Set the list of key-id for singning."
335 (unless (eq (car context) 'epg-context)
336 (signal 'wrong-type-argument (list 'epg-context-p context)))
337 (aset (cdr context) 9 signers))
339 (defun epg-context-set-process (context process)
340 "Set the process object of `epg-gpg-program'.
341 This function is for internal use only."
342 (unless (eq (car context) 'epg-context)
343 (signal 'wrong-type-argument (list 'epg-context-p context)))
344 (aset (cdr context) 10 process))
346 (defun epg-context-set-output-file (context output-file)
347 "Set the output file of `epg-gpg-program'.
348 This function is for internal use only."
349 (unless (eq (car context) 'epg-context)
350 (signal 'wrong-type-argument (list 'epg-context-p context)))
351 (aset (cdr context) 11 output-file))
353 (defun epg-context-set-result (context result)
354 "Set the result of the previous cryptographic operation."
355 (unless (eq (car context) 'epg-context)
356 (signal 'wrong-type-argument (list 'epg-context-p context)))
357 (aset (cdr context) 12 result))
359 (defun epg-make-signature (status &optional key-id)
360 "Return a signature object."
361 (cons 'epg-signature (vector status key-id nil nil nil nil nil nil nil nil)))
363 (defun epg-signature-status (signature)
364 "Return the status code of SIGNATURE."
365 (unless (eq (car signature) 'epg-signature)
366 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
367 (aref (cdr signature) 0))
369 (defun epg-signature-key-id (signature)
370 "Return the key-id of SIGNATURE."
371 (unless (eq (car signature) 'epg-signature)
372 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
373 (aref (cdr signature) 1))
375 (defun epg-signature-validity (signature)
376 "Return the validity of SIGNATURE."
377 (unless (eq (car signature) 'epg-signature)
378 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
379 (aref (cdr signature) 2))
381 (defun epg-signature-fingerprint (signature)
382 "Return the fingerprint of SIGNATURE."
383 (unless (eq (car signature) 'epg-signature)
384 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
385 (aref (cdr signature) 3))
387 (defun epg-signature-creation-time (signature)
388 "Return the creation time of SIGNATURE."
389 (unless (eq (car signature) 'epg-signature)
390 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
391 (aref (cdr signature) 4))
393 (defun epg-signature-expiration-time (signature)
394 "Return the expiration time of SIGNATURE."
395 (unless (eq (car signature) 'epg-signature)
396 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
397 (aref (cdr signature) 5))
399 (defun epg-signature-pubkey-algorithm (signature)
400 "Return the public key algorithm of SIGNATURE."
401 (unless (eq (car signature) 'epg-signature)
402 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
403 (aref (cdr signature) 6))
405 (defun epg-signature-digest-algorithm (signature)
406 "Return the digest algorithm of SIGNATURE."
407 (unless (eq (car signature) 'epg-signature)
408 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
409 (aref (cdr signature) 7))
411 (defun epg-signature-class (signature)
412 "Return the class of SIGNATURE."
413 (unless (eq (car signature) 'epg-signature)
414 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
415 (aref (cdr signature) 8))
417 (defun epg-signature-version (signature)
418 "Return the version of SIGNATURE."
419 (unless (eq (car signature) 'epg-signature)
420 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
421 (aref (cdr signature) 9))
423 (defun epg-signature-set-status (signature status)
424 "Set the status code of SIGNATURE."
425 (unless (eq (car signature) 'epg-signature)
426 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
427 (aset (cdr signature) 0 status))
429 (defun epg-signature-set-key-id (signature key-id)
430 "Set the key-id of SIGNATURE."
431 (unless (eq (car signature) 'epg-signature)
432 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
433 (aset (cdr signature) 1 key-id))
435 (defun epg-signature-set-validity (signature validity)
436 "Set the validity of SIGNATURE."
437 (unless (eq (car signature) 'epg-signature)
438 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
439 (aset (cdr signature) 2 validity))
441 (defun epg-signature-set-fingerprint (signature fingerprint)
442 "Set the fingerprint of SIGNATURE."
443 (unless (eq (car signature) 'epg-signature)
444 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
445 (aset (cdr signature) 3 fingerprint))
447 (defun epg-signature-set-creation-time (signature creation-time)
448 "Set the creation time of SIGNATURE."
449 (unless (eq (car signature) 'epg-signature)
450 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
451 (aset (cdr signature) 4 creation-time))
453 (defun epg-signature-set-expiration-time (signature expiration-time)
454 "Set the expiration time of SIGNATURE."
455 (unless (eq (car signature) 'epg-signature)
456 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
457 (aset (cdr signature) 5 expiration-time))
459 (defun epg-signature-set-pubkey-algorithm (signature pubkey-algorithm)
460 "Set the public key algorithm of SIGNATURE."
461 (unless (eq (car signature) 'epg-signature)
462 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
463 (aset (cdr signature) 6 pubkey-algorithm))
465 (defun epg-signature-set-digest-algorithm (signature digest-algorithm)
466 "Set the digest algorithm of SIGNATURE."
467 (unless (eq (car signature) 'epg-signature)
468 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
469 (aset (cdr signature) 7 digest-algorithm))
471 (defun epg-signature-set-class (signature class)
472 "Set the class of SIGNATURE."
473 (unless (eq (car signature) 'epg-signature)
474 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
475 (aset (cdr signature) 8 class))
477 (defun epg-signature-set-version (signature version)
478 "Set the version of SIGNATURE."
479 (unless (eq (car signature) 'epg-signature)
480 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
481 (aset (cdr signature) 9 version))
483 (defun epg-make-new-signature (type pubkey-algorithm digest-algorithm
484 class creation-time fingerprint)
485 "Return a new signature object."
486 (cons 'epg-new-signature (vector type pubkey-algorithm digest-algorithm
487 class creation-time fingerprint)))
489 (defun epg-new-signature-type (new-signature)
490 "Return the type of NEW-SIGNATURE."
491 (unless (eq (car new-signature) 'epg-new-signature)
492 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
493 (aref (cdr new-signature) 0))
495 (defun epg-new-signature-pubkey-algorithm (new-signature)
496 "Return the public key algorithm of NEW-SIGNATURE."
497 (unless (eq (car new-signature) 'epg-new-signature)
498 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
499 (aref (cdr new-signature) 1))
501 (defun epg-new-signature-digest-algorithm (new-signature)
502 "Return the digest algorithm of NEW-SIGNATURE."
503 (unless (eq (car new-signature) 'epg-new-signature)
504 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
505 (aref (cdr new-signature) 2))
507 (defun epg-new-signature-class (new-signature)
508 "Return the class of NEW-SIGNATURE."
509 (unless (eq (car new-signature) 'epg-new-signature)
510 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
511 (aref (cdr new-signature) 3))
513 (defun epg-new-signature-creation-time (new-signature)
514 "Return the creation time of NEW-SIGNATURE."
515 (unless (eq (car new-signature) 'epg-new-signature)
516 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
517 (aref (cdr new-signature) 4))
519 (defun epg-new-signature-fingerprint (new-signature)
520 "Return the fingerprint of NEW-SIGNATURE."
521 (unless (eq (car new-signature) 'epg-new-signature)
522 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
523 (aref (cdr new-signature) 5))
525 (defun epg-make-key (owner-trust)
526 "Return a key object."
527 (cons 'epg-key (vector owner-trust nil nil)))
529 (defun epg-key-owner-trust (key)
530 "Return the owner trust of KEY."
531 (unless (eq (car key) 'epg-key)
532 (signal 'wrong-type-argument (list 'epg-key-p key)))
535 (defun epg-key-sub-key-list (key)
536 "Return the sub key list of KEY."
537 (unless (eq (car key) 'epg-key)
538 (signal 'wrong-type-argument (list 'epg-key-p key)))
541 (defun epg-key-user-id-list (key)
542 "Return the user ID list of KEY."
543 (unless (eq (car key) 'epg-key)
544 (signal 'wrong-type-argument (list 'epg-key-p key)))
547 (defun epg-key-set-sub-key-list (key sub-key-list)
548 "Set the sub key list of KEY."
549 (unless (eq (car key) 'epg-key)
550 (signal 'wrong-type-argument (list 'epg-key-p key)))
551 (aset (cdr key) 1 sub-key-list))
553 (defun epg-key-set-user-id-list (key user-id-list)
554 "Set the user ID list of KEY."
555 (unless (eq (car key) 'epg-key)
556 (signal 'wrong-type-argument (list 'epg-key-p key)))
557 (aset (cdr key) 2 user-id-list))
559 (defun epg-make-sub-key (validity capability secret-p algorithm length id
560 creation-time expiration-time)
561 "Return a sub key object."
563 (vector validity capability secret-p algorithm length id creation-time
564 expiration-time nil)))
566 (defun epg-sub-key-validity (sub-key)
567 "Return the validity of SUB-KEY."
568 (unless (eq (car sub-key) 'epg-sub-key)
569 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
570 (aref (cdr sub-key) 0))
572 (defun epg-sub-key-capability (sub-key)
573 "Return the capability of SUB-KEY."
574 (unless (eq (car sub-key) 'epg-sub-key)
575 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
576 (aref (cdr sub-key) 1))
578 (defun epg-sub-key-secret-p (sub-key)
579 "Return non-nil if SUB-KEY is a secret key."
580 (unless (eq (car sub-key) 'epg-sub-key)
581 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
582 (aref (cdr sub-key) 2))
584 (defun epg-sub-key-algorithm (sub-key)
585 "Return the algorithm of SUB-KEY."
586 (unless (eq (car sub-key) 'epg-sub-key)
587 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
588 (aref (cdr sub-key) 3))
590 (defun epg-sub-key-length (sub-key)
591 "Return the length of SUB-KEY."
592 (unless (eq (car sub-key) 'epg-sub-key)
593 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
594 (aref (cdr sub-key) 4))
596 (defun epg-sub-key-id (sub-key)
597 "Return the ID of SUB-KEY."
598 (unless (eq (car sub-key) 'epg-sub-key)
599 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
600 (aref (cdr sub-key) 5))
602 (defun epg-sub-key-creation-time (sub-key)
603 "Return the creation time of SUB-KEY."
604 (unless (eq (car sub-key) 'epg-sub-key)
605 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
606 (aref (cdr sub-key) 6))
608 (defun epg-sub-key-expiration-time (sub-key)
609 "Return the expiration time of SUB-KEY."
610 (unless (eq (car sub-key) 'epg-sub-key)
611 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
612 (aref (cdr sub-key) 7))
614 (defun epg-sub-key-fingerprint (sub-key)
615 "Return the fingerprint of SUB-KEY."
616 (unless (eq (car sub-key) 'epg-sub-key)
617 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
618 (aref (cdr sub-key) 8))
620 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
621 "Set the fingerprint of SUB-KEY.
622 This function is for internal use only."
623 (unless (eq (car sub-key) 'epg-sub-key)
624 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
625 (aset (cdr sub-key) 8 fingerprint))
627 (defun epg-make-user-id (validity string)
628 "Return a user ID object."
629 (cons 'epg-user-id (vector validity string nil)))
631 (defun epg-user-id-validity (user-id)
632 "Return the validity of USER-ID."
633 (unless (eq (car user-id) 'epg-user-id)
634 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
635 (aref (cdr user-id) 0))
637 (defun epg-user-id-string (user-id)
638 "Return the name of USER-ID."
639 (unless (eq (car user-id) 'epg-user-id)
640 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
641 (aref (cdr user-id) 1))
643 (defun epg-user-id-signature-list (user-id)
644 "Return the signature list of USER-ID."
645 (unless (eq (car user-id) 'epg-user-id)
646 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
647 (aref (cdr user-id) 2))
649 (defun epg-user-id-set-signature-list (user-id signature-list)
650 "Set the signature list of USER-ID."
651 (unless (eq (car user-id) 'epg-user-id)
652 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
653 (aset (cdr user-id) 2 signature-list))
655 (defun epg-make-key-signature (validity pubkey-algorithm key-id creation-time
656 expiration-time user-id class
658 "Return a key signature object."
659 (cons 'epg-key-signature
660 (vector validity pubkey-algorithm key-id creation-time expiration-time
661 user-id class exportable-p)))
663 (defun epg-key-signature-validity (key-signature)
664 "Return the validity of KEY-SIGNATURE."
665 (unless (eq (car key-signature) 'epg-key-signature)
666 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
667 (aref (cdr key-signature) 0))
669 (defun epg-key-signature-pubkey-algorithm (key-signature)
670 "Return the public key algorithm of KEY-SIGNATURE."
671 (unless (eq (car key-signature) 'epg-key-signature)
672 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
673 (aref (cdr key-signature) 1))
675 (defun epg-key-signature-key-id (key-signature)
676 "Return the key-id of KEY-SIGNATURE."
677 (unless (eq (car key-signature) 'epg-key-signature)
678 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
679 (aref (cdr key-signature) 2))
681 (defun epg-key-signature-creation-time (key-signature)
682 "Return the creation time of KEY-SIGNATURE."
683 (unless (eq (car key-signature) 'epg-key-signature)
684 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
685 (aref (cdr key-signature) 3))
687 (defun epg-key-signature-expiration-time (key-signature)
688 "Return the expiration time of KEY-SIGNATURE."
689 (unless (eq (car key-signature) 'epg-key-signature)
690 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
691 (aref (cdr key-signature) 4))
693 (defun epg-key-signature-user-id (key-signature)
694 "Return the user-id of KEY-SIGNATURE."
695 (unless (eq (car key-signature) 'epg-key-signature)
696 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
697 (aref (cdr key-signature) 5))
699 (defun epg-key-signature-class (key-signature)
700 "Return the class of KEY-SIGNATURE."
701 (unless (eq (car key-signature) 'epg-key-signature)
702 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
703 (aref (cdr key-signature) 6))
705 (defun epg-key-signature-exportable-p (key-signature)
706 "Return t if KEY-SIGNATURE is exportable."
707 (unless (eq (car key-signature) 'epg-key-signature)
708 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
709 (aref (cdr key-signature) 7))
711 (defun epg-context-result-for (context name)
712 "Return the result of CONTEXT associated with NAME."
713 (cdr (assq name (epg-context-result context))))
715 (defun epg-context-set-result-for (context name value)
716 "Set the result of CONTEXT associated with NAME to VALUE."
717 (let* ((result (epg-context-result context))
718 (entry (assq name result)))
721 (epg-context-set-result context (cons (cons name value) result)))))
723 (defun epg-signature-to-string (signature)
724 "Convert SIGNATURE to a human readable string."
725 (let ((user-id (cdr (assoc (epg-signature-key-id signature)
726 epg-user-id-alist))))
728 (cond ((eq (epg-signature-status signature) 'good)
729 "Good signature from ")
730 ((eq (epg-signature-status signature) 'bad)
731 "Bad signature from ")
732 ((eq (epg-signature-status signature) 'expired)
733 "Expired signature from ")
734 ((eq (epg-signature-status signature) 'expired-key)
735 "Signature made by expired key ")
736 ((eq (epg-signature-status signature) 'revoked-key)
737 "Signature made by revoked key ")
738 ((eq (epg-signature-status signature) 'no-pubkey)
739 "No public key for "))
740 (epg-signature-key-id signature)
743 (concat (if (stringp user-id)
745 (epg-decode-dn user-id))
748 (if (epg-signature-validity signature)
749 (format "(trust %s)" (epg-signature-validity signature))
752 (defun epg-verify-result-to-string (verify-result)
753 "Convert VERIFY-RESULT to a human readable string."
754 (mapconcat #'epg-signature-to-string verify-result "\n"))
756 (defun epg-new-signature-to-string (new-signature)
757 "Convert NEW-SIGNATURE to a human readable string."
759 (cond ((eq (epg-new-signature-type new-signature) 'detached)
760 "Detached signature ")
761 ((eq (epg-new-signature-type new-signature) 'clear)
762 "Clear text signature ")
765 (cdr (assq (epg-new-signature-pubkey-algorithm new-signature)
766 epg-pubkey-algorithm-alist))
768 (cdr (assq (epg-new-signature-digest-algorithm new-signature)
769 epg-digest-algorithm-alist))
771 (format "%02X " (epg-new-signature-class new-signature))
772 (epg-new-signature-fingerprint new-signature)))
774 (defun epg-start (context args)
775 "Start `epg-gpg-program' in a subprocess with given ARGS."
776 (if (and (epg-context-process context)
777 (eq (process-status (epg-context-process context)) 'run))
778 (error "%s is already running in this context"
779 (if (eq (epg-context-protocol context) 'CMS)
782 (let* ((args (append (list "--no-tty"
785 (unless (eq (epg-context-protocol context) 'CMS)
786 (list "--command-fd" "0"))
787 (if (epg-context-armor context) '("--armor"))
788 (if (epg-context-textmode context) '("--textmode"))
789 (if (epg-context-output-file context)
790 (list "--output" (epg-context-output-file context)))
792 (coding-system-for-write 'binary)
793 process-connection-type
794 (orig-mode (default-file-modes))
795 (buffer (generate-new-buffer " *epg*"))
799 (unless epg-debug-buffer
800 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
801 (set-buffer epg-debug-buffer)
802 (goto-char (point-max))
803 (insert (format "%s %s\n"
804 (if (eq (epg-context-protocol context) 'CMS)
807 (mapconcat #'identity args " ")))))
808 (with-current-buffer buffer
809 (make-local-variable 'epg-read-point)
810 (setq epg-read-point (point-min))
811 (make-local-variable 'epg-pending-status-list)
812 (setq epg-pending-status-list nil)
813 (make-local-variable 'epg-key-id)
814 (setq epg-key-id nil)
815 (make-local-variable 'epg-context)
816 (setq epg-context context))
819 (set-default-file-modes 448)
821 (apply #'start-process "epg" buffer
822 (if (eq (epg-context-protocol context) 'CMS)
826 (set-default-file-modes orig-mode))
827 (set-process-filter process #'epg-process-filter)
828 (epg-context-set-process context process)))
830 (defun epg-process-filter (process input)
833 (unless epg-debug-buffer
834 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
835 (set-buffer epg-debug-buffer)
836 (goto-char (point-max))
838 (if (buffer-live-p (process-buffer process))
840 (set-buffer (process-buffer process))
841 (goto-char (point-max))
843 (goto-char epg-read-point)
845 (while (looking-at ".*\n") ;the input line finished
847 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
848 (let* ((status (match-string 1))
849 (string (match-string 2))
850 (symbol (intern-soft (concat "epg-status-" status))))
851 (if (member status epg-pending-status-list)
852 (setq epg-pending-status-list nil))
855 (funcall symbol process string)))))
857 (setq epg-read-point (point)))))
859 (defun epg-read-output (context)
860 "Read the output file CONTEXT and return the content as a string."
862 (if (fboundp 'set-buffer-multibyte)
863 (set-buffer-multibyte nil))
864 (if (file-exists-p (epg-context-output-file context))
865 (let ((coding-system-for-read 'binary))
866 (insert-file-contents (epg-context-output-file context))
869 (defun epg-wait-for-status (context status-list)
870 "Wait until one of elements in STATUS-LIST arrives."
871 (with-current-buffer (process-buffer (epg-context-process context))
872 (setq epg-pending-status-list status-list)
873 (while (and (eq (process-status (epg-context-process context)) 'run)
874 epg-pending-status-list)
875 (accept-process-output (epg-context-process context) 0 1))))
877 (defun epg-wait-for-completion (context)
878 "Wait until the `epg-gpg-program' process completes."
879 (while (eq (process-status (epg-context-process context)) 'run)
880 (accept-process-output (epg-context-process context) 0 1)))
882 (defun epg-reset (context)
884 (if (and (epg-context-process context)
885 (buffer-live-p (process-buffer (epg-context-process context))))
886 (kill-buffer (process-buffer (epg-context-process context))))
887 (epg-context-set-process context nil))
889 (defun epg-delete-output-file (context)
890 "Delete the output file of CONTEXT."
891 (if (and (epg-context-output-file context)
892 (file-exists-p (epg-context-output-file context)))
893 (delete-file (epg-context-output-file context))))
895 (defun epg-status-USERID_HINT (process string)
896 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
897 (let* ((key-id (match-string 1 string))
898 (user-id (match-string 2 string))
899 (entry (assoc key-id epg-user-id-alist)))
901 (setcdr entry user-id)
902 (setq epg-user-id-alist (cons (cons key-id user-id)
903 epg-user-id-alist))))))
905 (defun epg-status-NEED_PASSPHRASE (process string)
906 (if (string-match "\\`\\([^ ]+\\)" string)
907 (setq epg-key-id (match-string 1 string))))
909 (defun epg-status-NEED_PASSPHRASE_SYM (process string)
910 (setq epg-key-id 'SYM))
912 (defun epg-status-NEED_PASSPHRASE_PIN (process string)
913 (setq epg-key-id 'PIN))
915 (defun epg-status-GET_HIDDEN (process string)
917 (string-match "\\`passphrase\\." string))
920 passphrase-with-new-line)
926 (if (consp (epg-context-passphrase-callback
928 (car (epg-context-passphrase-callback
930 (epg-context-passphrase-callback epg-context))
933 (if (consp (epg-context-passphrase-callback
935 (cdr (epg-context-passphrase-callback
938 (setq passphrase-with-new-line (concat passphrase "\n"))
939 (fillarray passphrase 0)
940 (setq passphrase nil)
941 (process-send-string process passphrase-with-new-line)))
943 (epg-context-set-result-for
946 (epg-context-result-for epg-context 'error)))
947 (delete-process process)))
949 (fillarray passphrase 0))
950 (if passphrase-with-new-line
951 (fillarray passphrase-with-new-line 0))))))
953 (defun epg-status-GET_BOOL (process string)
954 (let ((entry (assoc string epg-prompt-alist))
957 (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
958 (process-send-string process "y\n")
959 (process-send-string process "n\n"))
961 (epg-context-set-result-for
964 (epg-context-result-for epg-context 'error)))
965 (delete-process process)))))
967 (defun epg-status-GET_LINE (process string)
968 (let ((entry (assoc string epg-prompt-alist))
973 (concat (read-string (if entry (cdr entry) (concat string ": ")))
976 (epg-context-set-result-for
979 (epg-context-result-for epg-context 'error)))
980 (delete-process process)))))
982 (defun epg-status-*SIG (status string)
983 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
984 (let* ((key-id (match-string 1 string))
985 (user-id (match-string 2 string))
986 (entry (assoc key-id epg-user-id-alist)))
987 (epg-context-set-result-for
990 (cons (epg-make-signature status key-id)
991 (epg-context-result-for epg-context 'verify)))
992 (if (eq (epg-context-protocol epg-context) 'CMS)
994 (setq user-id (epg-dn-from-string user-id))
997 (setcdr entry user-id)
998 (setq epg-user-id-alist
999 (cons (cons key-id user-id) epg-user-id-alist))))
1000 (epg-context-set-result-for
1003 (cons (epg-make-signature status)
1004 (epg-context-result-for epg-context 'verify)))))
1006 (defun epg-status-GOODSIG (process string)
1007 (epg-status-*SIG 'good string))
1009 (defun epg-status-EXPSIG (process string)
1010 (epg-status-*SIG 'expired string))
1012 (defun epg-status-EXPKEYSIG (process string)
1013 (epg-status-*SIG 'expired-key string))
1015 (defun epg-status-REVKEYSIG (process string)
1016 (epg-status-*SIG 'revoked-key string))
1018 (defun epg-status-BADSIG (process string)
1019 (epg-status-*SIG 'bad string))
1021 (defun epg-status-NO_PUBKEY (process string)
1022 (epg-context-set-result-for
1025 (cons (epg-make-signature 'no-pubkey string)
1026 (epg-context-result-for epg-context 'verify))))
1028 (defun epg-status-ERRSIG (process string)
1029 (let ((signatures (car (epg-context-result-for epg-context 'verify))))
1031 (setq signatures (list (epg-make-signature 'error)))
1032 (epg-context-set-result-for epg-context 'verify signatures))
1033 (when (and (not (eq (epg-signature-status (car signatures)) 'good))
1034 (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1035 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
1037 (epg-signature-set-key-id
1039 (match-string 1 string))
1040 (epg-signature-set-pubkey-algorithm
1042 (string-to-number (match-string 2 string)))
1043 (epg-signature-set-digest-algorithm
1045 (string-to-number (match-string 3 string)))
1046 (epg-signature-set-class
1048 (string-to-number (match-string 4 string) 16))
1049 (epg-signature-set-creation-time
1051 (match-string 5 string)))))
1053 (defun epg-status-VALIDSIG (process string)
1054 (let ((signature (car (epg-context-result-for epg-context 'verify))))
1055 (when (and signature
1056 (eq (epg-signature-status signature) 'good)
1057 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
1058 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
1061 (epg-signature-set-fingerprint
1063 (match-string 1 string))
1064 (epg-signature-set-creation-time
1066 (match-string 2 string))
1067 (epg-signature-set-expiration-time
1069 (match-string 3 string))
1070 (epg-signature-set-version
1072 (string-to-number (match-string 4 string)))
1073 (epg-signature-set-pubkey-algorithm
1075 (string-to-number (match-string 5 string)))
1076 (epg-signature-set-digest-algorithm
1078 (string-to-number (match-string 6 string)))
1079 (epg-signature-set-class
1081 (string-to-number (match-string 7 string) 16)))))
1083 (defun epg-status-TRUST_UNDEFINED (process string)
1084 (let ((signature (car (epg-context-result-for epg-context 'verify))))
1086 (eq (epg-signature-status signature) 'good))
1087 (epg-signature-set-validity signature 'undefined))))
1089 (defun epg-status-TRUST_NEVER (process string)
1090 (let ((signature (car (epg-context-result-for epg-context 'verify))))
1092 (eq (epg-signature-status signature) 'good))
1093 (epg-signature-set-validity signature 'never))))
1095 (defun epg-status-TRUST_MARGINAL (process string)
1096 (let ((signature (car (epg-context-result-for epg-context 'verify))))
1098 (eq (epg-signature-status signature) 'marginal))
1099 (epg-signature-set-validity signature 'marginal))))
1101 (defun epg-status-TRUST_FULLY (process string)
1102 (let ((signature (car (epg-context-result-for epg-context 'verify))))
1104 (eq (epg-signature-status signature) 'good))
1105 (epg-signature-set-validity signature 'full))))
1107 (defun epg-status-TRUST_ULTIMATE (process string)
1108 (let ((signature (car (epg-context-result-for epg-context 'verify))))
1110 (eq (epg-signature-status signature) 'good))
1111 (epg-signature-set-validity signature 'ultimate))))
1113 (defun epg-status-PROGRESS (process string)
1114 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1116 (funcall (if (consp (epg-context-progress-callback epg-context))
1117 (car (epg-context-progress-callback epg-context))
1118 (epg-context-progress-callback epg-context))
1120 (match-string 1 string)
1121 (match-string 2 string)
1122 (string-to-number (match-string 3 string))
1123 (string-to-number (match-string 4 string))
1124 (if (consp (epg-context-progress-callback epg-context))
1125 (cdr (epg-context-progress-callback epg-context))))))
1127 (defun epg-status-DECRYPTION_FAILED (process string)
1128 (epg-context-set-result-for
1130 (cons '(decryption-failed)
1131 (epg-context-result-for epg-context 'error))))
1133 (defun epg-status-NODATA (process string)
1134 (epg-context-set-result-for
1136 (cons (list 'no-data (cons 'reason (string-to-number string)))
1137 (epg-context-result-for epg-context 'error))))
1139 (defun epg-status-UNEXPECTED (process string)
1140 (epg-context-set-result-for
1142 (cons (list 'unexpected (cons 'reason (string-to-number string)))
1143 (epg-context-result-for epg-context 'error))))
1145 (defun epg-status-KEYEXPIRED (process string)
1146 (epg-context-set-result-for
1148 (cons (list 'key-expired (cons 'expiration-time string))
1149 (epg-context-result-for epg-context 'error))))
1151 (defun epg-status-KEYREVOKED (process string)
1152 (epg-context-set-result-for
1154 (cons '(key-revoked)
1155 (epg-context-result-for epg-context 'error))))
1157 (defun epg-status-BADARMOR (process string)
1158 (epg-context-set-result-for
1161 (epg-context-result-for epg-context 'error))))
1163 (defun epg-status-INV_RECP (process string)
1164 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1165 (epg-context-set-result-for
1167 (cons (list 'invalid-recipient
1169 (string-to-number (match-string 1 string)))
1170 (cons 'requested-recipient
1171 (match-string 2 string)))
1172 (epg-context-result-for epg-context 'error)))))
1174 (defun epg-status-NO_RECP (process string)
1175 (epg-context-set-result-for
1177 (cons '(no-recipients)
1178 (epg-context-result-for epg-context 'error))))
1180 (defun epg-status-DELETE_PROBLEM (process string)
1181 (if (string-match "\\`\\([0-9]+\\)" string)
1182 (epg-context-set-result-for
1184 (cons (list 'delete-problem
1185 (cons 'reason (string-to-number (match-string 1 string))))
1186 (epg-context-result-for epg-context 'error)))))
1188 (defun epg-status-SIG_CREATED (process string)
1189 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1190 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1191 (epg-context-set-result-for
1193 (cons (epg-make-new-signature
1194 (cdr (assq (aref (match-string 1 string) 0)
1195 epg-new-signature-type-alist))
1196 (string-to-number (match-string 2 string))
1197 (string-to-number (match-string 3 string))
1198 (string-to-number (match-string 4 string) 16)
1199 (match-string 5 string)
1200 (substring string (match-end 0)))
1201 (epg-context-result-for epg-context 'sign)))))
1203 (defun epg-status-KEY_CREATED (process string)
1204 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1205 (epg-context-set-result-for
1206 epg-context 'generate-key
1207 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1208 (cons 'fingerprint (match-string 2 string)))
1209 (epg-context-result-for epg-context 'generate-key)))))
1211 (defun epg-status-KEY_NOT_CREATED (process string)
1212 (epg-context-set-result-for
1214 (cons '(key-not-created)
1215 (epg-context-result-for epg-context 'error))))
1217 (defun epg-status-IMPORTED (process string)
1218 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1219 (let* ((key-id (match-string 1 string))
1220 (user-id (match-string 2 string))
1221 (entry (assoc key-id epg-user-id-alist)))
1223 (setcdr entry user-id)
1224 (setq epg-user-id-alist (cons (cons key-id user-id)
1225 epg-user-id-alist)))
1226 (epg-context-set-result-for
1228 (cons (list (cons 'key-id key-id)
1229 (cons 'user-id user-id))
1230 (epg-context-result-for epg-context 'import))))))
1232 (defun epg-status-IMPORT_OK (process string)
1233 (let ((result (epg-context-result-for epg-context 'import)))
1235 (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string))
1237 (append (list (cons 'reason
1239 (match-string 1 string))))
1240 (if (match-beginning 2)
1241 (list (cons 'fingerprint
1242 (match-string 3 string))))
1245 (defun epg-status-IMPORT_PROBLEM (process string)
1246 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1247 (epg-context-set-result-for
1249 (cons (cons 'import-problem
1250 (append (list (cons 'reason
1252 (match-string 1 string))))
1253 (if (match-beginning 2)
1254 (list (cons 'fingerprint
1255 (match-string 3 string))))))
1256 (epg-context-result-for epg-context 'error)))))
1258 (defun epg-passphrase-callback-function (context key-id handback)
1260 (if (eq key-id 'SYM)
1261 "Passphrase for symmetric encryption: "
1262 (if (eq key-id 'PIN)
1263 "Passphrase for PIN: "
1264 (let ((entry (assoc key-id epg-user-id-alist)))
1266 (format "Passphrase for %s %s: " key-id (cdr entry))
1267 (format "Passphrase for %s: " key-id)))))))
1269 (defun epg-progress-callback-function (context what char current total
1271 (message "%s: %d%%/%d%%" what current total))
1273 (defun epg-configuration ()
1274 "Return a list of internal configuration parameters of `epg-gpg-program'."
1277 (apply #'call-process epg-gpg-program nil (list t nil) nil
1278 '("--with-colons" "--list-config"))
1279 (goto-char (point-min))
1280 (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
1281 (setq type (intern (match-string 1))
1282 config (cons (cons type
1284 '(pubkey cipher digest compress))
1285 (mapcar #'string-to-number
1286 (delete "" (split-string
1293 (defun epg-list-keys-1 (context name mode)
1294 (let ((args (append (list "--with-colons" "--no-greeting" "--batch"
1295 "--with-fingerprint"
1296 "--with-fingerprint"
1297 (if (memq mode '(t secret))
1298 "--list-secret-keys"
1299 (if (memq mode '(nil public))
1302 (unless (eq (epg-context-protocol context) 'CMS)
1303 '("--fixed-list-mode"))
1304 (if name (list name))))
1305 keys string field index)
1307 (apply #'call-process
1308 (if (eq (epg-context-protocol context) 'CMS)
1311 nil (list t nil) nil args)
1312 (goto-char (point-min))
1313 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1314 (setq keys (cons (make-vector 15 nil) keys)
1315 string (match-string 0)
1319 (string-match "\\([^:]+\\)?:" string index))
1320 (setq index (match-end 0))
1321 (aset (car keys) field (match-string 1 string))
1322 (setq field (1+ field))))
1325 (defun epg-make-sub-key-1 (line)
1328 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1330 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1332 (member (aref line 0) '("sec" "ssb"))
1333 (string-to-number (aref line 3))
1334 (string-to-number (aref line 2))
1340 (defun epg-list-keys (context &optional name mode)
1341 "Return a list of epg-key objects matched with NAME.
1342 If MODE is nil or 'public, only public keyring should be searched.
1343 If MODE is t or 'secret, only secret keyring should be searched.
1344 Otherwise, only public keyring should be searched and the key
1345 signatures should be included."
1346 (let ((lines (epg-list-keys-1 context name mode))
1347 keys cert pointer pointer-1)
1350 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1351 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1352 keys (cons (epg-make-key
1353 (if (aref (car lines) 8)
1354 (cdr (assq (string-to-char (aref (car lines) 8))
1355 epg-key-validity-alist))))
1357 (epg-key-set-sub-key-list
1359 (cons (epg-make-sub-key-1 (car lines))
1360 (epg-key-sub-key-list (car keys)))))
1361 ((member (aref (car lines) 0) '("sub" "ssb"))
1362 (epg-key-set-sub-key-list
1364 (cons (epg-make-sub-key-1 (car lines))
1365 (epg-key-sub-key-list (car keys)))))
1366 ((equal (aref (car lines) 0) "uid")
1367 (epg-key-set-user-id-list
1369 (cons (epg-make-user-id
1370 (if (aref (car lines) 1)
1371 (cdr (assq (string-to-char (aref (car lines) 1))
1372 epg-key-validity-alist)))
1375 (epg-dn-from-string (aref (car lines) 9))
1376 (error (aref (car lines) 9)))
1377 (aref (car lines) 9)))
1378 (epg-key-user-id-list (car keys)))))
1379 ((equal (aref (car lines) 0) "fpr")
1380 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1381 (aref (car lines) 9)))
1382 ((equal (aref (car lines) 0) "sig")
1383 (epg-user-id-set-signature-list
1384 (car (epg-key-user-id-list (car keys)))
1386 (epg-make-key-signature
1387 (if (aref (car lines) 1)
1388 (cdr (assq (string-to-char (aref (car lines) 1))
1389 epg-key-validity-alist)))
1390 (string-to-number (aref (car lines) 3))
1391 (aref (car lines) 4)
1392 (aref (car lines) 5)
1393 (aref (car lines) 6)
1394 (aref (car lines) 9)
1395 (string-to-number (aref (car lines) 10) 16)
1396 (eq (aref (aref (car lines) 10) 2) ?x))
1397 (epg-user-id-signature-list
1398 (car (epg-key-user-id-list (car keys))))))))
1399 (setq lines (cdr lines)))
1400 (setq keys (nreverse keys)
1403 (epg-key-set-sub-key-list
1405 (nreverse (epg-key-sub-key-list (car pointer))))
1406 (setq pointer-1 (epg-key-set-user-id-list
1408 (nreverse (epg-key-user-id-list (car pointer)))))
1410 (epg-user-id-set-signature-list
1412 (nreverse (epg-user-id-signature-list (car pointer-1))))
1413 (setq pointer-1 (cdr pointer-1)))
1414 (setq pointer (cdr pointer)))
1417 (if (fboundp 'make-temp-file)
1418 (defalias 'epg-make-temp-file 'make-temp-file)
1419 (defvar temporary-file-directory)
1420 ;; stolen from poe.el.
1421 (defun epg-make-temp-file (prefix)
1422 "Create a temporary file.
1423 The returned file name (created by appending some random characters at the end
1424 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1425 is guaranteed to point to a newly created empty file.
1426 You can then use `write-region' to write new data into the file."
1427 (let (tempdir tempfile)
1428 (setq prefix (expand-file-name prefix
1429 (if (featurep 'xemacs)
1431 temporary-file-directory)))
1434 ;; First, create a temporary directory.
1435 (while (condition-case ()
1437 (setq tempdir (make-temp-name
1439 (file-name-directory prefix)
1441 ;; return nil or signal an error.
1442 (make-directory tempdir))
1444 (file-already-exists t)))
1445 (set-file-modes tempdir 448)
1446 ;; Second, create a temporary file in the tempdir.
1447 ;; There *is* a race condition between `make-temp-name'
1448 ;; and `write-region', but we don't care it since we are
1449 ;; in a private directory now.
1450 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1451 (write-region "" nil tempfile nil 'silent)
1452 (set-file-modes tempfile 384)
1453 ;; Finally, make a hard-link from the tempfile.
1454 (while (condition-case ()
1456 (setq file (make-temp-name prefix))
1457 ;; return nil or signal an error.
1458 (add-name-to-file tempfile file))
1460 (file-already-exists t)))
1462 ;; Cleanup the tempfile.
1464 (file-exists-p tempfile)
1465 (delete-file tempfile))
1466 ;; Cleanup the tempdir.
1468 (file-directory-p tempdir)
1469 (delete-directory tempdir))))))
1472 (defun epg-cancel (context)
1473 (if (buffer-live-p (process-buffer (epg-context-process context)))
1475 (set-buffer (process-buffer (epg-context-process context)))
1476 (epg-context-set-result-for
1479 (epg-context-result-for epg-context 'error)))))
1480 (if (eq (process-status (epg-context-process context)) 'run)
1481 (delete-process (epg-context-process context))))
1484 (defun epg-start-decrypt (context cipher)
1485 "Initiate a decrypt operation on CIPHER.
1486 CIPHER is a data object.
1488 If you use this function, you will need to wait for the completion of
1489 `epg-gpg-program' by using `epg-wait-for-completion' and call
1490 `epg-reset' to clear a temporaly output file.
1491 If you are unsure, use synchronous version of this function
1492 `epg-decrypt-file' or `epg-decrypt-string' instead."
1493 (unless (epg-data-file cipher)
1494 (error "Not a file"))
1495 (epg-context-set-result context nil)
1496 (epg-start context (list "--decrypt" (epg-data-file cipher)))
1497 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1498 (unless (eq (epg-context-protocol context) 'CMS)
1499 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1502 (defun epg-decrypt-file (context cipher plain)
1503 "Decrypt a file CIPHER and store the result to a file PLAIN.
1504 If PLAIN is nil, it returns the result as a string."
1508 (epg-context-set-output-file context plain)
1509 (epg-context-set-output-file context
1510 (epg-make-temp-file "epg-output")))
1511 (epg-start-decrypt context (epg-make-data-from-file cipher))
1512 (epg-wait-for-completion context)
1513 (if (epg-context-result-for context 'error)
1514 (error "Decrypt failed: %S"
1515 (epg-context-result-for context 'error)))
1517 (epg-read-output context)))
1519 (epg-delete-output-file context))
1520 (epg-reset context)))
1523 (defun epg-decrypt-string (context cipher)
1524 "Decrypt a string CIPHER and return the plain text."
1525 (let ((input-file (epg-make-temp-file "epg-input"))
1526 (coding-system-for-write 'binary))
1529 (write-region cipher nil input-file nil 'quiet)
1530 (epg-context-set-output-file context
1531 (epg-make-temp-file "epg-output"))
1532 (epg-start-decrypt context (epg-make-data-from-file input-file))
1533 (epg-wait-for-completion context)
1534 (if (epg-context-result-for context 'error)
1535 (error "Decrypt failed: %S"
1536 (epg-context-result-for context 'error)))
1537 (epg-read-output context))
1538 (epg-delete-output-file context)
1539 (if (file-exists-p input-file)
1540 (delete-file input-file))
1541 (epg-reset context))))
1544 (defun epg-start-verify (context signature &optional signed-text)
1545 "Initiate a verify operation on SIGNATURE.
1546 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1548 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1549 For a normal or a clear text signature, SIGNED-TEXT should be nil.
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-verify-file' or `epg-verify-string' instead."
1556 (epg-context-set-result context nil)
1558 ;; Detached signature.
1559 (if (epg-data-file signed-text)
1560 (epg-start context (list "--verify" (epg-data-file signature)
1561 (epg-data-file signed-text)))
1562 (epg-start context (list "--verify" (epg-data-file signature) "-"))
1563 (if (eq (process-status (epg-context-process context)) 'run)
1564 (process-send-string (epg-context-process context)
1565 (epg-data-string signed-text)))
1566 (if (eq (process-status (epg-context-process context)) 'run)
1567 (process-send-eof (epg-context-process context))))
1568 ;; Normal (or cleartext) signature.
1569 (if (epg-data-file signature)
1570 (epg-start context (list "--verify" (epg-data-file signature)))
1571 (epg-start context (list "--verify"))
1572 (if (eq (process-status (epg-context-process context)) 'run)
1573 (process-send-string (epg-context-process context)
1574 (epg-data-string signature)))
1575 (if (eq (process-status (epg-context-process context)) 'run)
1576 (process-send-eof (epg-context-process context))))))
1579 (defun epg-verify-file (context signature &optional signed-text plain)
1580 "Verify a file SIGNATURE.
1581 SIGNED-TEXT and PLAIN are also a file if they are specified.
1583 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1584 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1588 (epg-context-set-output-file context plain)
1589 (epg-context-set-output-file context
1590 (epg-make-temp-file "epg-output")))
1592 (epg-start-verify context
1593 (epg-make-data-from-file signature)
1594 (epg-make-data-from-file signed-text))
1595 (epg-start-verify context
1596 (epg-make-data-from-file signature)))
1597 (epg-wait-for-completion context)
1598 ; (if (epg-context-result-for context 'error)
1599 ; (error "Verify failed: %S"
1600 ; (epg-context-result-for context 'error)))
1602 (epg-read-output context)))
1604 (epg-delete-output-file context))
1605 (epg-reset context)))
1608 (defun epg-verify-string (context signature &optional signed-text)
1609 "Verify a string SIGNATURE.
1610 SIGNED-TEXT is a string if it is specified.
1612 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1613 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1614 (let ((coding-system-for-write 'binary)
1618 (epg-context-set-output-file context
1619 (epg-make-temp-file "epg-output"))
1622 (setq input-file (epg-make-temp-file "epg-signature"))
1623 (write-region signature nil input-file nil 'quiet)
1624 (epg-start-verify context
1625 (epg-make-data-from-file input-file)
1626 (epg-make-data-from-string signed-text)))
1627 (epg-start-verify context (epg-make-data-from-string signature)))
1628 (epg-wait-for-completion context)
1629 ; (if (epg-context-result-for context 'error)
1630 ; (error "Verify failed: %S"
1631 ; (epg-context-result-for context 'error)))
1632 (epg-read-output context))
1633 (epg-delete-output-file context)
1635 (file-exists-p input-file))
1636 (delete-file input-file))
1637 (epg-reset context))))
1640 (defun epg-start-sign (context plain &optional mode)
1641 "Initiate a sign operation on PLAIN.
1642 PLAIN is a data object.
1644 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1645 If it is nil or 'normal, it makes a normal signature.
1646 Otherwise, it makes a clear text signature.
1648 If you use this function, you will need to wait for the completion of
1649 `epg-gpg-program' by using `epg-wait-for-completion' and call
1650 `epg-reset' to clear a temporaly output file.
1651 If you are unsure, use synchronous version of this function
1652 `epg-sign-file' or `epg-sign-string' instead."
1653 (epg-context-set-result context nil)
1655 (append (list (if (memq mode '(t detached))
1657 (if (memq mode '(nil normal))
1665 (car (epg-key-sub-key-list signer)))))
1666 (epg-context-signers context)))
1667 (if (epg-data-file plain)
1668 (list (epg-data-file plain)))))
1669 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1670 (unless (eq (epg-context-protocol context) 'CMS)
1671 (epg-wait-for-status context '("BEGIN_SIGNING")))
1672 (when (epg-data-string plain)
1673 (if (eq (process-status (epg-context-process context)) 'run)
1674 (process-send-string (epg-context-process context)
1675 (epg-data-string plain)))
1676 (if (eq (process-status (epg-context-process context)) 'run)
1677 (process-send-eof (epg-context-process context)))))
1680 (defun epg-sign-file (context plain signature &optional mode)
1681 "Sign a file PLAIN and store the result to a file SIGNATURE.
1682 If SIGNATURE is nil, it returns the result as a string.
1683 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1684 If it is nil or 'normal, it makes a normal signature.
1685 Otherwise, it makes a clear text signature."
1689 (epg-context-set-output-file context signature)
1690 (epg-context-set-output-file context
1691 (epg-make-temp-file "epg-output")))
1692 (epg-start-sign context (epg-make-data-from-file plain) mode)
1693 (epg-wait-for-completion context)
1694 (unless (epg-context-result-for context 'sign)
1695 (if (epg-context-result-for context 'error)
1696 (error "Sign failed: %S"
1697 (epg-context-result-for context 'error))
1698 (error "Sign failed")))
1700 (epg-read-output context)))
1702 (epg-delete-output-file context))
1703 (epg-reset context)))
1706 (defun epg-sign-string (context plain &optional mode)
1707 "Sign a string PLAIN and return the output as string.
1708 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1709 If it is nil or 'normal, it makes a normal signature.
1710 Otherwise, it makes a clear text signature."
1713 (epg-context-set-output-file context
1714 (epg-make-temp-file "epg-output"))
1715 (epg-start-sign context (epg-make-data-from-string plain) mode)
1716 (epg-wait-for-completion context)
1717 (unless (epg-context-result-for context 'sign)
1718 (if (epg-context-result-for context 'error)
1719 (error "Sign failed: %S"
1720 (epg-context-result-for context 'error))
1721 (error "Sign failed")))
1722 (epg-read-output context))
1723 (epg-delete-output-file context)
1724 (epg-reset context)))
1727 (defun epg-start-encrypt (context plain recipients
1728 &optional sign always-trust)
1729 "Initiate an encrypt operation on PLAIN.
1730 PLAIN is a data object.
1731 If RECIPIENTS is nil, it performs symmetric encryption.
1733 If you use this function, you will need to wait for the completion of
1734 `epg-gpg-program' by using `epg-wait-for-completion' and call
1735 `epg-reset' to clear a temporaly output file.
1736 If you are unsure, use synchronous version of this function
1737 `epg-encrypt-file' or `epg-encrypt-string' instead."
1738 (epg-context-set-result context nil)
1740 (append (if always-trust '("--always-trust"))
1741 (if recipients '("--encrypt") '("--symmetric"))
1745 (mapcar (lambda (signer)
1747 (epg-context-signers context)))))
1753 (car (epg-key-sub-key-list recipient)))))
1755 (if (epg-data-file plain)
1756 (list (epg-data-file plain)))))
1757 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1758 (unless (eq (epg-context-protocol context) 'CMS)
1760 (epg-wait-for-status context '("BEGIN_SIGNING"))
1761 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
1762 (when (epg-data-string plain)
1763 (if (eq (process-status (epg-context-process context)) 'run)
1764 (process-send-string (epg-context-process context)
1765 (epg-data-string plain)))
1766 (if (eq (process-status (epg-context-process context)) 'run)
1767 (process-send-eof (epg-context-process context)))))
1770 (defun epg-encrypt-file (context plain recipients
1771 cipher &optional sign always-trust)
1772 "Encrypt a file PLAIN and store the result to a file CIPHER.
1773 If CIPHER is nil, it returns the result as a string.
1774 If RECIPIENTS is nil, it performs symmetric encryption."
1778 (epg-context-set-output-file context cipher)
1779 (epg-context-set-output-file context
1780 (epg-make-temp-file "epg-output")))
1781 (epg-start-encrypt context (epg-make-data-from-file plain)
1782 recipients sign always-trust)
1783 (epg-wait-for-completion context)
1785 (not (epg-context-result-for context 'sign)))
1786 (if (epg-context-result-for context 'error)
1787 (error "Sign failed: %S"
1788 (epg-context-result-for context 'error))
1789 (error "Sign failed")))
1790 (if (epg-context-result-for context 'error)
1791 (error "Encrypt failed: %S"
1792 (epg-context-result-for context 'error)))
1794 (epg-read-output context)))
1796 (epg-delete-output-file context))
1797 (epg-reset context)))
1800 (defun epg-encrypt-string (context plain recipients
1801 &optional sign always-trust)
1802 "Encrypt a string PLAIN.
1803 If RECIPIENTS is nil, it performs symmetric encryption."
1806 (epg-context-set-output-file context
1807 (epg-make-temp-file "epg-output"))
1808 (epg-start-encrypt context (epg-make-data-from-string plain)
1809 recipients sign always-trust)
1810 (epg-wait-for-completion context)
1812 (not (epg-context-result-for context 'sign)))
1813 (if (epg-context-result-for context 'error)
1814 (error "Sign failed: %S"
1815 (epg-context-result-for context 'error))
1816 (error "Sign failed")))
1817 (if (epg-context-result-for context 'error)
1818 (error "Encrypt failed: %S"
1819 (epg-context-result-for context 'error)))
1820 (epg-read-output context))
1821 (epg-delete-output-file context)
1822 (epg-reset context)))
1825 (defun epg-start-export-keys (context keys)
1826 "Initiate an export keys operation.
1828 If you use this function, you will need to wait for the completion of
1829 `epg-gpg-program' by using `epg-wait-for-completion' and call
1830 `epg-reset' to clear a temporaly output file.
1831 If you are unsure, use synchronous version of this function
1832 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1833 (epg-context-set-result context nil)
1834 (epg-start context (cons "--export"
1838 (car (epg-key-sub-key-list key))))
1842 (defun epg-export-keys-to-file (context keys file)
1843 "Extract public KEYS."
1847 (epg-context-set-output-file context file)
1848 (epg-context-set-output-file context
1849 (epg-make-temp-file "epg-output")))
1850 (epg-start-export-keys context keys)
1851 (epg-wait-for-completion context)
1852 (if (epg-context-result-for context 'error)
1853 (error "Export keys failed: %S"
1854 (epg-context-result-for context 'error)))
1856 (epg-read-output context)))
1858 (epg-delete-output-file context))
1859 (epg-reset context)))
1862 (defun epg-export-keys-to-string (context keys)
1863 "Extract public KEYS and return them as a string."
1864 (epg-export-keys-to-file context keys nil))
1867 (defun epg-start-import-keys (context keys)
1868 "Initiate an import keys operation.
1869 KEYS is a data object.
1871 If you use this function, you will need to wait for the completion of
1872 `epg-gpg-program' by using `epg-wait-for-completion' and call
1873 `epg-reset' to clear a temporaly output file.
1874 If you are unsure, use synchronous version of this function
1875 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1876 (epg-context-set-result context nil)
1877 (epg-start context (list "--import" (epg-data-file keys)))
1878 (when (epg-data-string keys)
1879 (if (eq (process-status (epg-context-process context)) 'run)
1880 (process-send-string (epg-context-process context)
1881 (epg-data-string keys)))
1882 (if (eq (process-status (epg-context-process context)) 'run)
1883 (process-send-eof (epg-context-process context)))))
1885 (defun epg-import-keys-1 (context keys)
1888 (epg-start-import-keys context keys)
1889 (epg-wait-for-completion context)
1890 (if (epg-context-result-for context 'error)
1891 (error "Import keys failed: %S"
1892 (epg-context-result-for context 'error))))
1893 (epg-reset context)))
1896 (defun epg-import-keys-from-file (context keys)
1897 "Add keys from a file KEYS."
1898 (epg-import-keys-1 context (epg-make-data-from-file keys)))
1901 (defun epg-import-keys-from-string (context keys)
1902 "Add keys from a string KEYS."
1903 (epg-import-keys-1 context (epg-make-data-from-string keys)))
1906 (defun epg-start-receive-keys (context key-id-list)
1907 "Initiate a receive key operation.
1908 KEY-ID-LIST is a list of key IDs.
1910 If you use this function, you will need to wait for the completion of
1911 `epg-gpg-program' by using `epg-wait-for-completion' and call
1912 `epg-reset' to clear a temporaly output file.
1913 If you are unsure, use synchronous version of this function
1914 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
1915 (epg-context-set-result context nil)
1916 (epg-start context (cons "--recv-keys" key-id-list)))
1919 (defun epg-receive-keys (context keys)
1920 "Add keys from server.
1921 KEYS is a list of key IDs"
1924 (epg-start-receive-keys context keys)
1925 (epg-wait-for-completion context)
1926 (if (epg-context-result-for context 'error)
1927 (error "Receive keys failed: %S"
1928 (epg-context-result-for context 'error))))
1929 (epg-reset context)))
1932 (defalias 'epg-import-keys-from-server 'epg-receive-keys)
1935 (defun epg-start-delete-keys (context keys &optional allow-secret)
1936 "Initiate an delete keys operation.
1938 If you use this function, you will need to wait for the completion of
1939 `epg-gpg-program' by using `epg-wait-for-completion' and call
1940 `epg-reset' to clear a temporaly output file.
1941 If you are unsure, use synchronous version of this function
1942 `epg-delete-keys' instead."
1943 (epg-context-set-result context nil)
1944 (epg-start context (cons (if allow-secret
1945 "--delete-secret-key"
1950 (car (epg-key-sub-key-list key))))
1954 (defun epg-delete-keys (context keys &optional allow-secret)
1955 "Delete KEYS from the key ring."
1958 (epg-start-delete-keys context keys allow-secret)
1959 (epg-wait-for-completion context)
1960 (if (epg-context-result-for context 'error)
1961 (error "Delete keys failed: %S"
1962 (epg-context-result-for context 'error))))
1963 (epg-reset context)))
1966 (defun epg-start-sign-keys (context keys &optional local)
1967 "Initiate an sign keys operation.
1969 If you use this function, you will need to wait for the completion of
1970 `epg-gpg-program' by using `epg-wait-for-completion' and call
1971 `epg-reset' to clear a temporaly output file.
1972 If you are unsure, use synchronous version of this function
1973 `epg-sign-keys' instead."
1974 (epg-context-set-result context nil)
1975 (epg-start context (cons (if local
1981 (car (epg-key-sub-key-list key))))
1985 (defun epg-sign-keys (context keys &optional local)
1986 "Sign KEYS from the key ring."
1989 (epg-start-sign-keys context keys local)
1990 (epg-wait-for-completion context)
1991 (if (epg-context-result-for context 'error)
1992 (error "Sign keys failed: %S"
1993 (epg-context-result-for context 'error))))
1994 (epg-reset context)))
1997 (defun epg-start-generate-key (context parameters)
1998 "Initiate a key generation.
1999 PARAMETERS specifies parameters for the key.
2001 If you use this function, you will need to wait for the completion of
2002 `epg-gpg-program' by using `epg-wait-for-completion' and call
2003 `epg-reset' to clear a temporaly output file.
2004 If you are unsure, use synchronous version of this function
2005 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2006 (epg-context-set-result context nil)
2007 (if (epg-data-file parameters)
2008 (epg-start context (list "--batch" "--genkey"
2009 (epg-data-file parameters)))
2010 (epg-start context '("--batch" "--genkey"))
2011 (if (eq (process-status (epg-context-process context)) 'run)
2012 (process-send-string (epg-context-process context)
2013 (epg-data-string parameters)))
2014 (if (eq (process-status (epg-context-process context)) 'run)
2015 (process-send-eof (epg-context-process context)))))
2018 (defun epg-generate-key-from-file (context parameters)
2019 "Generate a new key pair.
2020 PARAMETERS is a file which tells how to create the key."
2023 (epg-start-generate-key context (epg-make-data-from-file parameters))
2024 (epg-wait-for-completion context)
2025 (if (epg-context-result-for context 'error)
2026 (error "Generate key failed: %S"
2027 (epg-context-result-for context 'error))))
2028 (epg-reset context)))
2031 (defun epg-generate-key-from-string (context parameters)
2032 "Generate a new key pair.
2033 PARAMETERS is a string which tells how to create the key."
2036 (epg-start-generate-key context (epg-make-data-from-string parameters))
2037 (epg-wait-for-completion context)
2038 (if (epg-context-result-for context 'error)
2039 (error "Generate key failed: %S"
2040 (epg-context-result-for context 'error))))
2041 (epg-reset context)))
2043 (defun epg-decode-hexstring (string)
2045 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
2046 (setq string (replace-match "\\x\\&" t nil string)
2048 (car (read-from-string (concat "\"" string "\"")))))
2050 (defun epg-decode-quotedstring (string)
2052 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2053 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
2055 (if (match-beginning 2)
2056 (setq string (replace-match "\\2" t nil string)
2058 (if (match-beginning 3)
2059 (setq string (replace-match "\\x\\3" t nil string)
2061 (setq string (replace-match "\\\\\\\\\\4" t nil string)
2062 index (+ index 3)))))
2063 (car (read-from-string (concat "\"" string "\"")))))
2065 (defun epg-dn-from-string (string)
2066 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2067 The return value is an alist mapping from types to values."
2069 (length (length string))
2070 alist type value group)
2071 (while (< index length)
2072 (if (eq index (string-match "[ \t\n\r]*" string index))
2073 (setq index (match-end 0)))
2074 (if (eq index (string-match
2075 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2077 (setq type (match-string 1 string)
2078 index (match-end 0))
2079 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2081 (setq type (match-string 1 string)
2082 index (match-end 0))))
2084 (error "Invalid type"))
2085 (if (eq index (string-match
2086 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2088 (setq index (match-end 0)
2089 value (epg-decode-quotedstring (match-string 0 string)))
2090 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
2091 (setq index (match-end 0)
2092 value (epg-decode-hexstring (match-string 1 string)))
2093 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2095 (setq index (match-end 0)
2096 value (epg-decode-quotedstring (match-string 0 string))))))
2098 (if (stringp (car (car alist)))
2099 (setcar alist (list (cons type value) (car alist)))
2100 (setcar alist (cons (cons type value) (car alist))))
2101 (if (consp (car (car alist)))
2102 (setcar alist (nreverse (car alist))))
2103 (setq alist (cons (cons type value) alist)
2106 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
2107 (setq index (match-end 0)
2108 group (eq (aref string (match-beginning 1)) ?+))))
2111 (defun epg-decode-dn (alist)
2112 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2113 Type names are resolved using `epg-dn-type-alist'."
2116 (if (stringp (car rdn))
2117 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
2119 (format "%s=%s" (cdr entry) (cdr rdn))
2120 (format "%s=%s" (car rdn) (cdr rdn))))
2121 (concat "(" (epg-decode-dn rdn) ")")))
2127 ;;; epg.el ends here