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 nil)
42 (defvar epg-debug-buffer nil)
44 ;; from gnupg/include/cipher.h
45 (defconst epg-cipher-algorithm-alist
57 ;; from gnupg/include/cipher.h
58 (defconst epg-pubkey-algorithm-alist
66 ;; from gnupg/include/cipher.h
67 (defconst epg-digest-algorithm-alist
75 ;; from gnupg/include/cipher.h
76 (defconst epg-compress-algorithm-alist
82 (defconst epg-invalid-recipients-reason-alist
83 '((0 . "No specific reason given")
85 (2 . "Ambigious specification")
86 (3 . "Wrong key usage")
91 (8 . "Policy mismatch")
92 (9 . "Not a secret key")
93 (10 . "Key not trusted")))
95 (defconst epg-delete-problem-reason-alist
97 (2 . "Must delete secret key first")
98 (3 . "Ambigious specification")))
100 (defconst epg-import-ok-reason-alist
101 '((0 . "Not actually changed")
102 (1 . "Entirely new key")
104 (4 . "New signatures")
106 (16 . "Contains private key")))
108 (defconst epg-import-problem-reason-alist
109 '((0 . "No specific reason given")
110 (1 . "Invalid Certificate")
111 (2 . "Issuer Certificate missing")
112 (3 . "Certificate Chain too long")
113 (4 . "Error storing certificate")))
115 (defconst epg-no-data-reason-alist
116 '((1 . "No armored data")
117 (2 . "Expected a packet but did not found one")
118 (3 . "Invalid packet found, this may indicate a non OpenPGP message")
119 (4 . "Signature expected but not found")))
121 (defconst epg-unexpected-reason-alist nil)
123 (defvar epg-key-validity-alist
136 (defvar epg-key-capablity-alist
140 (?a . authentication)))
142 (defvar epg-new-signature-type-alist
147 (defvar epg-dn-type-alist
148 '(("1.2.840.113549.1.9.1" . "EMail")
152 ("0.2.262.1.10.7.20" . "NameDistinguisher")
153 ("2.5.4.16" . "ADDR")
156 ("2.5.4.17" . "PostalCode")
157 ("2.5.4.65" . "Pseudo")
158 ("2.5.4.5" . "SerialNumber")))
160 (defvar epg-prompt-alist nil)
162 (defun epg-make-data-from-file (file)
163 "Make a data object from FILE."
164 (cons 'epg-data (vector file nil)))
166 (defun epg-make-data-from-string (string)
167 "Make a data object from STRING."
168 (cons 'epg-data (vector nil string)))
170 (defun epg-data-file (data)
171 "Return the file of DATA."
172 (unless (eq (car data) 'epg-data)
173 (signal 'wrong-type-argument (list 'epg-data-p data)))
176 (defun epg-data-string (data)
177 "Return the string of DATA."
178 (unless (eq (car data) 'epg-data)
179 (signal 'wrong-type-argument (list 'epg-data-p data)))
182 (defun epg-make-context (&optional protocol armor textmode include-certs
183 cipher-algorithm digest-algorithm
185 "Return a context object."
187 (vector (or protocol 'OpenPGP) armor textmode include-certs
188 cipher-algorithm digest-algorithm compress-algorithm
189 #'epg-passphrase-callback-function
190 #'epg-progress-callback-function
191 nil nil nil nil nil)))
193 (defun epg-context-protocol (context)
194 "Return the protocol used within CONTEXT."
195 (unless (eq (car context) 'epg-context)
196 (signal 'wrong-type-argument (list 'epg-context-p context)))
197 (aref (cdr context) 0))
199 (defun epg-context-armor (context)
200 "Return t if the output shouled be ASCII armored in CONTEXT."
201 (unless (eq (car context) 'epg-context)
202 (signal 'wrong-type-argument (list 'epg-context-p context)))
203 (aref (cdr context) 1))
205 (defun epg-context-textmode (context)
206 "Return t if canonical text mode should be used in CONTEXT."
207 (unless (eq (car context) 'epg-context)
208 (signal 'wrong-type-argument (list 'epg-context-p context)))
209 (aref (cdr context) 2))
211 (defun epg-context-include-certs (context)
212 "Return how many certificates should be included in an S/MIME signed
214 (unless (eq (car context) 'epg-context)
215 (signal 'wrong-type-argument (list 'epg-context-p context)))
216 (aref (cdr context) 3))
218 (defun epg-context-cipher-algorithm (context)
219 "Return the cipher algorithm in CONTEXT."
220 (unless (eq (car context) 'epg-context)
221 (signal 'wrong-type-argument (list 'epg-context-p context)))
222 (aref (cdr context) 4))
224 (defun epg-context-digest-algorithm (context)
225 "Return the digest algorithm in CONTEXT."
226 (unless (eq (car context) 'epg-context)
227 (signal 'wrong-type-argument (list 'epg-context-p context)))
228 (aref (cdr context) 5))
230 (defun epg-context-compress-algorithm (context)
231 "Return the compress algorithm in CONTEXT."
232 (unless (eq (car context) 'epg-context)
233 (signal 'wrong-type-argument (list 'epg-context-p context)))
234 (aref (cdr context) 6))
236 (defun epg-context-passphrase-callback (context)
237 "Return the function used to query passphrase."
238 (unless (eq (car context) 'epg-context)
239 (signal 'wrong-type-argument (list 'epg-context-p context)))
240 (aref (cdr context) 7))
242 (defun epg-context-progress-callback (context)
243 "Return the function which handles progress update."
244 (unless (eq (car context) 'epg-context)
245 (signal 'wrong-type-argument (list 'epg-context-p context)))
246 (aref (cdr context) 8))
248 (defun epg-context-signers (context)
249 "Return the list of key-id for singning."
250 (unless (eq (car context) 'epg-context)
251 (signal 'wrong-type-argument (list 'epg-context-p context)))
252 (aref (cdr context) 9))
254 (defun epg-context-process (context)
255 "Return the process object of `epg-gpg-program'.
256 This function is for internal use only."
257 (unless (eq (car context) 'epg-context)
258 (signal 'wrong-type-argument (list 'epg-context-p context)))
259 (aref (cdr context) 10))
261 (defun epg-context-output-file (context)
262 "Return the output file of `epg-gpg-program'.
263 This function is for internal use only."
264 (unless (eq (car context) 'epg-context)
265 (signal 'wrong-type-argument (list 'epg-context-p context)))
266 (aref (cdr context) 11))
268 (defun epg-context-result (context)
269 "Return the result of the previous cryptographic operation."
270 (unless (eq (car context) 'epg-context)
271 (signal 'wrong-type-argument (list 'epg-context-p context)))
272 (aref (cdr context) 12))
274 (defun epg-context-operation (context)
275 "Return the name of the current cryptographic operation."
276 (unless (eq (car context) 'epg-context)
277 (signal 'wrong-type-argument (list 'epg-context-p context)))
278 (aref (cdr context) 13))
280 (defun epg-context-set-protocol (context protocol)
281 "Set the protocol used within CONTEXT."
282 (unless (eq (car context) 'epg-context)
283 (signal 'wrong-type-argument (list 'epg-context-p context)))
284 (aset (cdr context) 0 protocol))
286 (defun epg-context-set-armor (context armor)
287 "Specify if the output shouled be ASCII armored in CONTEXT."
288 (unless (eq (car context) 'epg-context)
289 (signal 'wrong-type-argument (list 'epg-context-p context)))
290 (aset (cdr context) 1 armor))
292 (defun epg-context-set-textmode (context textmode)
293 "Specify if canonical text mode should be used in CONTEXT."
294 (unless (eq (car context) 'epg-context)
295 (signal 'wrong-type-argument (list 'epg-context-p context)))
296 (aset (cdr context) 2 textmode))
298 (defun epg-context-set-include-certs (context include-certs)
299 "Set how many certificates should be included in an S/MIME signed message."
300 (unless (eq (car context) 'epg-context)
301 (signal 'wrong-type-argument (list 'epg-context-p context)))
302 (aset (cdr context) 3 include-certs))
304 (defun epg-context-set-cipher-algorithm (context cipher-algorithm)
305 "Set the cipher algorithm in CONTEXT."
306 (unless (eq (car context) 'epg-context)
307 (signal 'wrong-type-argument (list 'epg-context-p context)))
308 (aset (cdr context) 4 cipher-algorithm))
310 (defun epg-context-set-digest-algorithm (context digest-algorithm)
311 "Set the digest algorithm in CONTEXT."
312 (unless (eq (car context) 'epg-context)
313 (signal 'wrong-type-argument (list 'epg-context-p context)))
314 (aset (cdr context) 5 digest-algorithm))
316 (defun epg-context-set-compress-algorithm (context compress-algorithm)
317 "Set the compress algorithm in CONTEXT."
318 (unless (eq (car context) 'epg-context)
319 (signal 'wrong-type-argument (list 'epg-context-p context)))
320 (aset (cdr context) 6 compress-algorithm))
322 (defun epg-context-set-passphrase-callback (context
324 "Set the function used to query passphrase."
325 (unless (eq (car context) 'epg-context)
326 (signal 'wrong-type-argument (list 'epg-context-p context)))
327 (aset (cdr context) 7 passphrase-callback))
329 (defun epg-context-set-progress-callback (context progress-callback)
330 "Set the function which handles progress update."
331 (unless (eq (car context) 'epg-context)
332 (signal 'wrong-type-argument (list 'epg-context-p context)))
333 (aset (cdr context) 8 progress-callback))
335 (defun epg-context-set-signers (context signers)
336 "Set the list of key-id for singning."
337 (unless (eq (car context) 'epg-context)
338 (signal 'wrong-type-argument (list 'epg-context-p context)))
339 (aset (cdr context) 9 signers))
341 (defun epg-context-set-process (context process)
342 "Set the process object of `epg-gpg-program'.
343 This function is for internal use only."
344 (unless (eq (car context) 'epg-context)
345 (signal 'wrong-type-argument (list 'epg-context-p context)))
346 (aset (cdr context) 10 process))
348 (defun epg-context-set-output-file (context output-file)
349 "Set the output file of `epg-gpg-program'.
350 This function is for internal use only."
351 (unless (eq (car context) 'epg-context)
352 (signal 'wrong-type-argument (list 'epg-context-p context)))
353 (aset (cdr context) 11 output-file))
355 (defun epg-context-set-result (context result)
356 "Set the result of the previous cryptographic operation."
357 (unless (eq (car context) 'epg-context)
358 (signal 'wrong-type-argument (list 'epg-context-p context)))
359 (aset (cdr context) 12 result))
361 (defun epg-context-set-operation (context operation)
362 "Set the name of the current cryptographic operation."
363 (unless (eq (car context) 'epg-context)
364 (signal 'wrong-type-argument (list 'epg-context-p context)))
365 (aset (cdr context) 13 operation))
367 (defun epg-make-signature (status &optional key-id)
368 "Return a signature object."
369 (cons 'epg-signature (vector status key-id nil nil nil nil nil nil nil nil)))
371 (defun epg-signature-status (signature)
372 "Return the status code of SIGNATURE."
373 (unless (eq (car signature) 'epg-signature)
374 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
375 (aref (cdr signature) 0))
377 (defun epg-signature-key-id (signature)
378 "Return the key-id of SIGNATURE."
379 (unless (eq (car signature) 'epg-signature)
380 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
381 (aref (cdr signature) 1))
383 (defun epg-signature-validity (signature)
384 "Return the validity of SIGNATURE."
385 (unless (eq (car signature) 'epg-signature)
386 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
387 (aref (cdr signature) 2))
389 (defun epg-signature-fingerprint (signature)
390 "Return the fingerprint of SIGNATURE."
391 (unless (eq (car signature) 'epg-signature)
392 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
393 (aref (cdr signature) 3))
395 (defun epg-signature-creation-time (signature)
396 "Return the creation time of SIGNATURE."
397 (unless (eq (car signature) 'epg-signature)
398 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
399 (aref (cdr signature) 4))
401 (defun epg-signature-expiration-time (signature)
402 "Return the expiration time of SIGNATURE."
403 (unless (eq (car signature) 'epg-signature)
404 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
405 (aref (cdr signature) 5))
407 (defun epg-signature-pubkey-algorithm (signature)
408 "Return the public key algorithm of SIGNATURE."
409 (unless (eq (car signature) 'epg-signature)
410 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
411 (aref (cdr signature) 6))
413 (defun epg-signature-digest-algorithm (signature)
414 "Return the digest algorithm of SIGNATURE."
415 (unless (eq (car signature) 'epg-signature)
416 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
417 (aref (cdr signature) 7))
419 (defun epg-signature-class (signature)
420 "Return the class of SIGNATURE."
421 (unless (eq (car signature) 'epg-signature)
422 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
423 (aref (cdr signature) 8))
425 (defun epg-signature-version (signature)
426 "Return the version of SIGNATURE."
427 (unless (eq (car signature) 'epg-signature)
428 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
429 (aref (cdr signature) 9))
431 (defun epg-signature-set-status (signature status)
432 "Set the status code of SIGNATURE."
433 (unless (eq (car signature) 'epg-signature)
434 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
435 (aset (cdr signature) 0 status))
437 (defun epg-signature-set-key-id (signature key-id)
438 "Set the key-id of SIGNATURE."
439 (unless (eq (car signature) 'epg-signature)
440 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
441 (aset (cdr signature) 1 key-id))
443 (defun epg-signature-set-validity (signature validity)
444 "Set the validity of SIGNATURE."
445 (unless (eq (car signature) 'epg-signature)
446 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
447 (aset (cdr signature) 2 validity))
449 (defun epg-signature-set-fingerprint (signature fingerprint)
450 "Set the fingerprint of SIGNATURE."
451 (unless (eq (car signature) 'epg-signature)
452 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
453 (aset (cdr signature) 3 fingerprint))
455 (defun epg-signature-set-creation-time (signature creation-time)
456 "Set the creation time of SIGNATURE."
457 (unless (eq (car signature) 'epg-signature)
458 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
459 (aset (cdr signature) 4 creation-time))
461 (defun epg-signature-set-expiration-time (signature expiration-time)
462 "Set the expiration time of SIGNATURE."
463 (unless (eq (car signature) 'epg-signature)
464 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
465 (aset (cdr signature) 5 expiration-time))
467 (defun epg-signature-set-pubkey-algorithm (signature pubkey-algorithm)
468 "Set the public key algorithm of SIGNATURE."
469 (unless (eq (car signature) 'epg-signature)
470 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
471 (aset (cdr signature) 6 pubkey-algorithm))
473 (defun epg-signature-set-digest-algorithm (signature digest-algorithm)
474 "Set the digest algorithm of SIGNATURE."
475 (unless (eq (car signature) 'epg-signature)
476 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
477 (aset (cdr signature) 7 digest-algorithm))
479 (defun epg-signature-set-class (signature class)
480 "Set the class of SIGNATURE."
481 (unless (eq (car signature) 'epg-signature)
482 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
483 (aset (cdr signature) 8 class))
485 (defun epg-signature-set-version (signature version)
486 "Set the version of SIGNATURE."
487 (unless (eq (car signature) 'epg-signature)
488 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
489 (aset (cdr signature) 9 version))
491 (defun epg-make-new-signature (type pubkey-algorithm digest-algorithm
492 class creation-time fingerprint)
493 "Return a new signature object."
494 (cons 'epg-new-signature (vector type pubkey-algorithm digest-algorithm
495 class creation-time fingerprint)))
497 (defun epg-new-signature-type (new-signature)
498 "Return the type of NEW-SIGNATURE."
499 (unless (eq (car new-signature) 'epg-new-signature)
500 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
501 (aref (cdr new-signature) 0))
503 (defun epg-new-signature-pubkey-algorithm (new-signature)
504 "Return the public key algorithm of NEW-SIGNATURE."
505 (unless (eq (car new-signature) 'epg-new-signature)
506 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
507 (aref (cdr new-signature) 1))
509 (defun epg-new-signature-digest-algorithm (new-signature)
510 "Return the digest algorithm of NEW-SIGNATURE."
511 (unless (eq (car new-signature) 'epg-new-signature)
512 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
513 (aref (cdr new-signature) 2))
515 (defun epg-new-signature-class (new-signature)
516 "Return the class of NEW-SIGNATURE."
517 (unless (eq (car new-signature) 'epg-new-signature)
518 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
519 (aref (cdr new-signature) 3))
521 (defun epg-new-signature-creation-time (new-signature)
522 "Return the creation time of NEW-SIGNATURE."
523 (unless (eq (car new-signature) 'epg-new-signature)
524 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
525 (aref (cdr new-signature) 4))
527 (defun epg-new-signature-fingerprint (new-signature)
528 "Return the fingerprint of NEW-SIGNATURE."
529 (unless (eq (car new-signature) 'epg-new-signature)
530 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
531 (aref (cdr new-signature) 5))
533 (defun epg-make-key (owner-trust)
534 "Return a key object."
535 (cons 'epg-key (vector owner-trust nil nil)))
537 (defun epg-key-owner-trust (key)
538 "Return the owner trust of KEY."
539 (unless (eq (car key) 'epg-key)
540 (signal 'wrong-type-argument (list 'epg-key-p key)))
543 (defun epg-key-sub-key-list (key)
544 "Return the sub key list of KEY."
545 (unless (eq (car key) 'epg-key)
546 (signal 'wrong-type-argument (list 'epg-key-p key)))
549 (defun epg-key-user-id-list (key)
550 "Return the user ID list of KEY."
551 (unless (eq (car key) 'epg-key)
552 (signal 'wrong-type-argument (list 'epg-key-p key)))
555 (defun epg-key-set-sub-key-list (key sub-key-list)
556 "Set the sub key list of KEY."
557 (unless (eq (car key) 'epg-key)
558 (signal 'wrong-type-argument (list 'epg-key-p key)))
559 (aset (cdr key) 1 sub-key-list))
561 (defun epg-key-set-user-id-list (key user-id-list)
562 "Set the user ID list of KEY."
563 (unless (eq (car key) 'epg-key)
564 (signal 'wrong-type-argument (list 'epg-key-p key)))
565 (aset (cdr key) 2 user-id-list))
567 (defun epg-make-sub-key (validity capability secret-p algorithm length id
568 creation-time expiration-time)
569 "Return a sub key object."
571 (vector validity capability secret-p algorithm length id creation-time
572 expiration-time nil)))
574 (defun epg-sub-key-validity (sub-key)
575 "Return the validity of SUB-KEY."
576 (unless (eq (car sub-key) 'epg-sub-key)
577 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
578 (aref (cdr sub-key) 0))
580 (defun epg-sub-key-capability (sub-key)
581 "Return the capability of SUB-KEY."
582 (unless (eq (car sub-key) 'epg-sub-key)
583 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
584 (aref (cdr sub-key) 1))
586 (defun epg-sub-key-secret-p (sub-key)
587 "Return non-nil if SUB-KEY is a secret key."
588 (unless (eq (car sub-key) 'epg-sub-key)
589 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
590 (aref (cdr sub-key) 2))
592 (defun epg-sub-key-algorithm (sub-key)
593 "Return the algorithm of SUB-KEY."
594 (unless (eq (car sub-key) 'epg-sub-key)
595 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
596 (aref (cdr sub-key) 3))
598 (defun epg-sub-key-length (sub-key)
599 "Return the length of SUB-KEY."
600 (unless (eq (car sub-key) 'epg-sub-key)
601 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
602 (aref (cdr sub-key) 4))
604 (defun epg-sub-key-id (sub-key)
605 "Return the ID of SUB-KEY."
606 (unless (eq (car sub-key) 'epg-sub-key)
607 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
608 (aref (cdr sub-key) 5))
610 (defun epg-sub-key-creation-time (sub-key)
611 "Return the creation time of SUB-KEY."
612 (unless (eq (car sub-key) 'epg-sub-key)
613 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
614 (aref (cdr sub-key) 6))
616 (defun epg-sub-key-expiration-time (sub-key)
617 "Return the expiration time of SUB-KEY."
618 (unless (eq (car sub-key) 'epg-sub-key)
619 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
620 (aref (cdr sub-key) 7))
622 (defun epg-sub-key-fingerprint (sub-key)
623 "Return the fingerprint of SUB-KEY."
624 (unless (eq (car sub-key) 'epg-sub-key)
625 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
626 (aref (cdr sub-key) 8))
628 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
629 "Set the fingerprint of SUB-KEY.
630 This function is for internal use only."
631 (unless (eq (car sub-key) 'epg-sub-key)
632 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
633 (aset (cdr sub-key) 8 fingerprint))
635 (defun epg-make-user-id (validity string)
636 "Return a user ID object."
637 (cons 'epg-user-id (vector validity string nil)))
639 (defun epg-user-id-validity (user-id)
640 "Return the validity of USER-ID."
641 (unless (eq (car user-id) 'epg-user-id)
642 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
643 (aref (cdr user-id) 0))
645 (defun epg-user-id-string (user-id)
646 "Return the name of USER-ID."
647 (unless (eq (car user-id) 'epg-user-id)
648 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
649 (aref (cdr user-id) 1))
651 (defun epg-user-id-signature-list (user-id)
652 "Return the signature list of USER-ID."
653 (unless (eq (car user-id) 'epg-user-id)
654 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
655 (aref (cdr user-id) 2))
657 (defun epg-user-id-set-signature-list (user-id signature-list)
658 "Set the signature list of USER-ID."
659 (unless (eq (car user-id) 'epg-user-id)
660 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
661 (aset (cdr user-id) 2 signature-list))
663 (defun epg-make-key-signature (validity pubkey-algorithm key-id creation-time
664 expiration-time user-id class
666 "Return a key signature object."
667 (cons 'epg-key-signature
668 (vector validity pubkey-algorithm key-id creation-time expiration-time
669 user-id class exportable-p)))
671 (defun epg-key-signature-validity (key-signature)
672 "Return the validity of KEY-SIGNATURE."
673 (unless (eq (car key-signature) 'epg-key-signature)
674 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
675 (aref (cdr key-signature) 0))
677 (defun epg-key-signature-pubkey-algorithm (key-signature)
678 "Return the public key algorithm of KEY-SIGNATURE."
679 (unless (eq (car key-signature) 'epg-key-signature)
680 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
681 (aref (cdr key-signature) 1))
683 (defun epg-key-signature-key-id (key-signature)
684 "Return the key-id of KEY-SIGNATURE."
685 (unless (eq (car key-signature) 'epg-key-signature)
686 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
687 (aref (cdr key-signature) 2))
689 (defun epg-key-signature-creation-time (key-signature)
690 "Return the creation time of KEY-SIGNATURE."
691 (unless (eq (car key-signature) 'epg-key-signature)
692 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
693 (aref (cdr key-signature) 3))
695 (defun epg-key-signature-expiration-time (key-signature)
696 "Return the expiration time of KEY-SIGNATURE."
697 (unless (eq (car key-signature) 'epg-key-signature)
698 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
699 (aref (cdr key-signature) 4))
701 (defun epg-key-signature-user-id (key-signature)
702 "Return the user-id of KEY-SIGNATURE."
703 (unless (eq (car key-signature) 'epg-key-signature)
704 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
705 (aref (cdr key-signature) 5))
707 (defun epg-key-signature-class (key-signature)
708 "Return the class of KEY-SIGNATURE."
709 (unless (eq (car key-signature) 'epg-key-signature)
710 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
711 (aref (cdr key-signature) 6))
713 (defun epg-key-signature-exportable-p (key-signature)
714 "Return t if KEY-SIGNATURE is exportable."
715 (unless (eq (car key-signature) 'epg-key-signature)
716 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
717 (aref (cdr key-signature) 7))
719 (defun epg-context-result-for (context name)
720 "Return the result of CONTEXT associated with NAME."
721 (cdr (assq name (epg-context-result context))))
723 (defun epg-context-set-result-for (context name value)
724 "Set the result of CONTEXT associated with NAME to VALUE."
725 (let* ((result (epg-context-result context))
726 (entry (assq name result)))
729 (epg-context-set-result context (cons (cons name value) result)))))
731 (defun epg-signature-to-string (signature)
732 "Convert SIGNATURE to a human readable string."
733 (let ((user-id (cdr (assoc (epg-signature-key-id signature)
734 epg-user-id-alist))))
736 (cond ((eq (epg-signature-status signature) 'good)
737 "Good signature from ")
738 ((eq (epg-signature-status signature) 'bad)
739 "Bad signature from ")
740 ((eq (epg-signature-status signature) 'expired)
741 "Expired signature from ")
742 ((eq (epg-signature-status signature) 'expired-key)
743 "Signature made by expired key ")
744 ((eq (epg-signature-status signature) 'revoked-key)
745 "Signature made by revoked key ")
746 ((eq (epg-signature-status signature) 'no-pubkey)
747 "No public key for "))
748 (epg-signature-key-id signature)
751 (if (stringp user-id)
753 (epg-decode-dn user-id)))
755 (if (epg-signature-validity signature)
756 (format " (trust %s)" (epg-signature-validity signature))
759 (defun epg-verify-result-to-string (verify-result)
760 "Convert VERIFY-RESULT to a human readable string."
761 (mapconcat #'epg-signature-to-string verify-result "\n"))
763 (defun epg-new-signature-to-string (new-signature)
764 "Convert NEW-SIGNATURE to a human readable string."
766 (cond ((eq (epg-new-signature-type new-signature) 'detached)
767 "Detached signature ")
768 ((eq (epg-new-signature-type new-signature) 'clear)
769 "Clear text signature ")
772 (cdr (assq (epg-new-signature-pubkey-algorithm new-signature)
773 epg-pubkey-algorithm-alist))
775 (cdr (assq (epg-new-signature-digest-algorithm new-signature)
776 epg-digest-algorithm-alist))
778 (format "%02X " (epg-new-signature-class new-signature))
779 (epg-new-signature-fingerprint new-signature)))
781 (defun epg--start (context args)
782 "Start `epg-gpg-program' in a subprocess with given ARGS."
783 (if (and (epg-context-process context)
784 (eq (process-status (epg-context-process context)) 'run))
785 (error "%s is already running in this context"
786 (if (eq (epg-context-protocol context) 'CMS)
789 (let* ((args (append (list "--no-tty"
792 (if epg-gpg-home-directory
793 (list "--homedir" epg-gpg-home-directory))
794 (unless (eq (epg-context-protocol context) 'CMS)
795 (list "--command-fd" "0"))
796 (if (epg-context-armor context) '("--armor"))
797 (if (epg-context-textmode context) '("--textmode"))
798 (if (epg-context-output-file context)
799 (list "--output" (epg-context-output-file context)))
801 (coding-system-for-write 'binary)
802 process-connection-type
803 (orig-mode (default-file-modes))
804 (buffer (generate-new-buffer " *epg*"))
808 (unless epg-debug-buffer
809 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
810 (set-buffer epg-debug-buffer)
811 (goto-char (point-max))
812 (insert (format "%s %s\n"
813 (if (eq (epg-context-protocol context) 'CMS)
816 (mapconcat #'identity args " ")))))
817 (with-current-buffer buffer
818 (make-local-variable 'epg-read-point)
819 (setq epg-read-point (point-min))
820 (make-local-variable 'epg-process-filter-running)
821 (setq epg-process-filter-running nil)
822 (make-local-variable 'epg-pending-status-list)
823 (setq epg-pending-status-list nil)
824 (make-local-variable 'epg-key-id)
825 (setq epg-key-id nil)
826 (make-local-variable 'epg-context)
827 (setq epg-context context))
830 (set-default-file-modes 448)
832 (apply #'start-process "epg" buffer
833 (if (eq (epg-context-protocol context) 'CMS)
837 (set-default-file-modes orig-mode))
838 (set-process-filter process #'epg--process-filter)
839 (epg-context-set-process context process)))
841 (defun epg--process-filter (process input)
844 (unless epg-debug-buffer
845 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
846 (set-buffer epg-debug-buffer)
847 (goto-char (point-max))
849 (if (buffer-live-p (process-buffer process))
851 (set-buffer (process-buffer process))
852 (goto-char (point-max))
854 (unless epg-process-filter-running
857 (setq epg-process-filter-running t)
858 (goto-char epg-read-point)
860 (while (looking-at ".*\n") ;the input line finished
861 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
862 (let* ((status (match-string 1))
863 (string (match-string 2))
864 (symbol (intern-soft (concat "epg--status-"
866 (if (member status epg-pending-status-list)
867 (setq epg-pending-status-list nil))
870 (funcall symbol epg-context string))))
872 (setq epg-read-point (point))))
873 (setq epg-process-filter-running nil))))))
875 (defun epg-read-output (context)
876 "Read the output file CONTEXT and return the content as a string."
878 (if (fboundp 'set-buffer-multibyte)
879 (set-buffer-multibyte nil))
880 (if (file-exists-p (epg-context-output-file context))
881 (let ((coding-system-for-read 'binary))
882 (insert-file-contents (epg-context-output-file context))
885 (defun epg-wait-for-status (context status-list)
886 "Wait until one of elements in STATUS-LIST arrives."
887 (with-current-buffer (process-buffer (epg-context-process context))
888 (setq epg-pending-status-list status-list)
889 (while (and (eq (process-status (epg-context-process context)) 'run)
890 epg-pending-status-list)
891 (accept-process-output (epg-context-process context) 1))))
893 (defun epg-wait-for-completion (context)
894 "Wait until the `epg-gpg-program' process completes."
895 (while (eq (process-status (epg-context-process context)) 'run)
896 (accept-process-output (epg-context-process context) 1)))
898 (defun epg-reset (context)
900 (if (and (epg-context-process context)
901 (buffer-live-p (process-buffer (epg-context-process context))))
902 (kill-buffer (process-buffer (epg-context-process context))))
903 (epg-context-set-process context nil))
905 (defun epg-delete-output-file (context)
906 "Delete the output file of CONTEXT."
907 (if (and (epg-context-output-file context)
908 (file-exists-p (epg-context-output-file context)))
909 (delete-file (epg-context-output-file context))))
911 (defun epg--status-USERID_HINT (context string)
912 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
913 (let* ((key-id (match-string 1 string))
914 (user-id (match-string 2 string))
915 (entry (assoc key-id epg-user-id-alist)))
917 (setcdr entry user-id)
918 (setq epg-user-id-alist (cons (cons key-id user-id)
919 epg-user-id-alist))))))
921 (defun epg--status-NEED_PASSPHRASE (context string)
922 (if (string-match "\\`\\([^ ]+\\)" string)
923 (setq epg-key-id (match-string 1 string))))
925 (defun epg--status-NEED_PASSPHRASE_SYM (context string)
926 (setq epg-key-id 'SYM))
928 (defun epg--status-NEED_PASSPHRASE_PIN (context string)
929 (setq epg-key-id 'PIN))
931 (defun epg--status-GET_HIDDEN (context string)
933 (string-match "\\`passphrase\\." string))
936 passphrase-with-new-line
937 encoded-passphrase-with-new-line)
943 (if (consp (epg-context-passphrase-callback context))
944 (car (epg-context-passphrase-callback context))
945 (epg-context-passphrase-callback context))
948 (if (consp (epg-context-passphrase-callback context))
949 (cdr (epg-context-passphrase-callback context)))))
951 (setq passphrase-with-new-line (concat passphrase "\n"))
952 (epg--clear-string passphrase)
954 encoded-passphrase-with-new-line
955 (encode-coding-string passphrase-with-new-line
956 (terminal-coding-system)))
958 (process-send-string (epg-context-process context)
959 encoded-passphrase-with-new-line)))
961 (epg-context-set-result-for
964 (epg-context-result-for context 'error)))
965 (delete-process (epg-context-process context))))
967 (epg--clear-string passphrase))
968 (if passphrase-with-new-line
969 (epg--clear-string passphrase-with-new-line))
970 (if encoded-passphrase-with-new-line
971 (epg--clear-string encoded-passphrase-with-new-line))))))
973 (defun epg--status-GET_BOOL (context string)
974 (let ((entry (assoc string epg-prompt-alist))
977 (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
978 (process-send-string (epg-context-process context) "y\n")
979 (process-send-string (epg-context-process context) "n\n"))
981 (epg-context-set-result-for
984 (epg-context-result-for context 'error)))
985 (delete-process (epg-context-process context))))))
987 (defun epg--status-GET_LINE (context string)
988 (let ((entry (assoc string epg-prompt-alist))
991 (process-send-string (epg-context-process context)
995 (concat string ": ")))
998 (epg-context-set-result-for
1001 (epg-context-result-for context 'error)))
1002 (delete-process (epg-context-process context))))))
1004 (defun epg--status-*SIG (context status string)
1005 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1006 (let* ((key-id (match-string 1 string))
1007 (user-id (match-string 2 string))
1008 (entry (assoc key-id epg-user-id-alist)))
1009 (epg-context-set-result-for
1012 (cons (epg-make-signature status key-id)
1013 (epg-context-result-for context 'verify)))
1014 (if (eq (epg-context-protocol context) 'CMS)
1016 (setq user-id (epg-dn-from-string user-id))
1019 (setcdr entry user-id)
1020 (setq epg-user-id-alist
1021 (cons (cons key-id user-id) epg-user-id-alist))))
1022 (epg-context-set-result-for
1025 (cons (epg-make-signature status)
1026 (epg-context-result-for context 'verify)))))
1028 (defun epg--status-GOODSIG (context string)
1029 (epg--status-*SIG context 'good string))
1031 (defun epg--status-EXPSIG (context string)
1032 (epg--status-*SIG context 'expired string))
1034 (defun epg--status-EXPKEYSIG (context string)
1035 (epg--status-*SIG context 'expired-key string))
1037 (defun epg--status-REVKEYSIG (context string)
1038 (epg--status-*SIG context 'revoked-key string))
1040 (defun epg--status-BADSIG (context string)
1041 (epg--status-*SIG context 'bad string))
1043 (defun epg--status-NO_PUBKEY (context string)
1044 (let ((signature (car (epg-context-result-for context 'verify))))
1046 (eq (epg-signature-status signature) 'error)
1047 (equal (epg-signature-key-id signature) string))
1048 (epg-signature-set-status signature 'no-pubkey))))
1050 (defun epg--time-from-seconds (seconds)
1051 (let ((number-seconds (string-to-number (concat seconds ".0"))))
1052 (cons (floor (/ number-seconds 65536))
1053 (floor (mod number-seconds 65536)))))
1055 (defun epg--status-ERRSIG (context string)
1056 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1057 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
1059 (let ((signature (epg-make-signature 'error)))
1060 (epg-context-set-result-for
1064 (epg-context-result-for context 'verify)))
1065 (epg-signature-set-key-id
1067 (match-string 1 string))
1068 (epg-signature-set-pubkey-algorithm
1070 (string-to-number (match-string 2 string)))
1071 (epg-signature-set-digest-algorithm
1073 (string-to-number (match-string 3 string)))
1074 (epg-signature-set-class
1076 (string-to-number (match-string 4 string) 16))
1077 (epg-signature-set-creation-time
1079 (epg--time-from-seconds (match-string 5 string))))))
1081 (defun epg--status-VALIDSIG (context string)
1082 (let ((signature (car (epg-context-result-for context 'verify))))
1083 (when (and signature
1084 (eq (epg-signature-status signature) 'good)
1085 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
1086 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
1089 (epg-signature-set-fingerprint
1091 (match-string 1 string))
1092 (epg-signature-set-creation-time
1094 (epg--time-from-seconds (match-string 2 string)))
1095 (epg-signature-set-expiration-time
1097 (epg--time-from-seconds (match-string 3 string)))
1098 (epg-signature-set-version
1100 (string-to-number (match-string 4 string)))
1101 (epg-signature-set-pubkey-algorithm
1103 (string-to-number (match-string 5 string)))
1104 (epg-signature-set-digest-algorithm
1106 (string-to-number (match-string 6 string)))
1107 (epg-signature-set-class
1109 (string-to-number (match-string 7 string) 16)))))
1111 (defun epg--status-TRUST_UNDEFINED (context string)
1112 (let ((signature (car (epg-context-result-for context 'verify))))
1114 (eq (epg-signature-status signature) 'good))
1115 (epg-signature-set-validity signature 'undefined))))
1117 (defun epg--status-TRUST_NEVER (context string)
1118 (let ((signature (car (epg-context-result-for context 'verify))))
1120 (eq (epg-signature-status signature) 'good))
1121 (epg-signature-set-validity signature 'never))))
1123 (defun epg--status-TRUST_MARGINAL (context string)
1124 (let ((signature (car (epg-context-result-for context 'verify))))
1126 (eq (epg-signature-status signature) 'marginal))
1127 (epg-signature-set-validity signature 'marginal))))
1129 (defun epg--status-TRUST_FULLY (context string)
1130 (let ((signature (car (epg-context-result-for context 'verify))))
1132 (eq (epg-signature-status signature) 'good))
1133 (epg-signature-set-validity signature 'full))))
1135 (defun epg--status-TRUST_ULTIMATE (context string)
1136 (let ((signature (car (epg-context-result-for context 'verify))))
1138 (eq (epg-signature-status signature) 'good))
1139 (epg-signature-set-validity signature 'ultimate))))
1141 (defun epg--status-PROGRESS (context string)
1142 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1144 (funcall (if (consp (epg-context-progress-callback context))
1145 (car (epg-context-progress-callback context))
1146 (epg-context-progress-callback context))
1148 (match-string 1 string)
1149 (match-string 2 string)
1150 (string-to-number (match-string 3 string))
1151 (string-to-number (match-string 4 string))
1152 (if (consp (epg-context-progress-callback context))
1153 (cdr (epg-context-progress-callback context))))))
1155 (defun epg--status-DECRYPTION_FAILED (context string)
1156 (epg-context-set-result-for
1158 (cons '(decryption-failed)
1159 (epg-context-result-for context 'error))))
1161 (defun epg--status-NODATA (context string)
1162 (epg-context-set-result-for
1164 (cons (list 'no-data (cons 'reason (string-to-number string)))
1165 (epg-context-result-for context 'error))))
1167 (defun epg--status-UNEXPECTED (context string)
1168 (epg-context-set-result-for
1170 (cons (list 'unexpected (cons 'reason (string-to-number string)))
1171 (epg-context-result-for context 'error))))
1173 (defun epg--status-KEYEXPIRED (context string)
1174 (epg-context-set-result-for
1176 (cons (list 'key-expired (cons 'expiration-time
1177 (epg--time-from-seconds string)))
1178 (epg-context-result-for context 'error))))
1180 (defun epg--status-KEYREVOKED (context string)
1181 (epg-context-set-result-for
1183 (cons '(key-revoked)
1184 (epg-context-result-for context 'error))))
1186 (defun epg--status-BADARMOR (context string)
1187 (epg-context-set-result-for
1190 (epg-context-result-for context 'error))))
1192 (defun epg--status-INV_RECP (context string)
1193 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1194 (epg-context-set-result-for
1196 (cons (list 'invalid-recipient
1198 (string-to-number (match-string 1 string)))
1199 (cons 'requested-recipient
1200 (match-string 2 string)))
1201 (epg-context-result-for context 'error)))))
1203 (defun epg--status-NO_RECP (context string)
1204 (epg-context-set-result-for
1206 (cons '(no-recipients)
1207 (epg-context-result-for context 'error))))
1209 (defun epg--status-DELETE_PROBLEM (context string)
1210 (if (string-match "\\`\\([0-9]+\\)" string)
1211 (epg-context-set-result-for
1213 (cons (list 'delete-problem
1214 (cons 'reason (string-to-number (match-string 1 string))))
1215 (epg-context-result-for context 'error)))))
1217 (defun epg--status-SIG_CREATED (context string)
1218 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1219 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1220 (epg-context-set-result-for
1222 (cons (epg-make-new-signature
1223 (cdr (assq (aref (match-string 1 string) 0)
1224 epg-new-signature-type-alist))
1225 (string-to-number (match-string 2 string))
1226 (string-to-number (match-string 3 string))
1227 (string-to-number (match-string 4 string) 16)
1228 (epg--time-from-seconds (match-string 5 string))
1229 (substring string (match-end 0)))
1230 (epg-context-result-for context 'sign)))))
1232 (defun epg--status-KEY_CREATED (context string)
1233 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1234 (epg-context-set-result-for
1235 context 'generate-key
1236 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1237 (cons 'fingerprint (match-string 2 string)))
1238 (epg-context-result-for context 'generate-key)))))
1240 (defun epg--status-KEY_NOT_CREATED (context string)
1241 (epg-context-set-result-for
1243 (cons '(key-not-created)
1244 (epg-context-result-for context 'error))))
1246 (defun epg--status-IMPORTED (context string)
1247 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1248 (let* ((key-id (match-string 1 string))
1249 (user-id (match-string 2 string))
1250 (entry (assoc key-id epg-user-id-alist)))
1252 (setcdr entry user-id)
1253 (setq epg-user-id-alist (cons (cons key-id user-id)
1254 epg-user-id-alist)))
1255 (epg-context-set-result-for
1257 (cons (list (cons 'key-id key-id)
1258 (cons 'user-id user-id))
1259 (epg-context-result-for context 'import))))))
1261 (defun epg--status-IMPORT_OK (context string)
1262 (let ((result (epg-context-result-for context 'import)))
1264 (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string))
1266 (append (list (cons 'reason
1268 (match-string 1 string))))
1269 (if (match-beginning 2)
1270 (list (cons 'fingerprint
1271 (match-string 3 string))))
1274 (defun epg--status-IMPORT_PROBLEM (context string)
1275 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1276 (epg-context-set-result-for
1278 (cons (cons 'import-problem
1279 (append (list (cons 'reason
1281 (match-string 1 string))))
1282 (if (match-beginning 2)
1283 (list (cons 'fingerprint
1284 (match-string 3 string))))))
1285 (epg-context-result-for context 'error)))))
1287 (defun epg-passphrase-callback-function (context key-id handback)
1288 (if (eq key-id 'SYM)
1289 (read-passwd "Passphrase for symmetric encryption: "
1290 (eq (epg-context-operation context) 'encrypt))
1292 (if (eq key-id 'PIN)
1293 "Passphrase for PIN: "
1294 (let ((entry (assoc key-id epg-user-id-alist)))
1296 (format "Passphrase for %s %s: " key-id (cdr entry))
1297 (format "Passphrase for %s: " key-id)))))))
1299 (defun epg-progress-callback-function (context what char current total
1301 (message "%s: %d%%/%d%%" what current total))
1303 (defun epg--list-keys-1 (context name mode)
1304 (let ((args (append (if epg-gpg-home-directory
1305 (list "--homedir" epg-gpg-home-directory))
1306 (list "--with-colons" "--no-greeting" "--batch"
1307 "--with-fingerprint"
1308 "--with-fingerprint"
1309 (if (memq mode '(t secret))
1310 "--list-secret-keys"
1311 (if (memq mode '(nil public))
1314 (unless (eq (epg-context-protocol context) 'CMS)
1315 '("--fixed-list-mode"))
1316 (if name (list name))))
1317 keys string field index)
1319 (apply #'call-process
1320 (if (eq (epg-context-protocol context) 'CMS)
1323 nil (list t nil) nil args)
1324 (goto-char (point-min))
1325 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1326 (setq keys (cons (make-vector 15 nil) keys)
1327 string (match-string 0)
1331 (string-match "\\([^:]+\\)?:" string index))
1332 (setq index (match-end 0))
1333 (aset (car keys) field (match-string 1 string))
1334 (setq field (1+ field))))
1337 (defun epg--make-sub-key-1 (line)
1340 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1342 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1344 (member (aref line 0) '("sec" "ssb"))
1345 (string-to-number (aref line 3))
1346 (string-to-number (aref line 2))
1348 (epg--time-from-seconds (aref line 5))
1349 (epg--time-from-seconds (aref line 6))))
1352 (defun epg-list-keys (context &optional name mode)
1353 "Return a list of epg-key objects matched with NAME.
1354 If MODE is nil or 'public, only public keyring should be searched.
1355 If MODE is t or 'secret, only secret keyring should be searched.
1356 Otherwise, only public keyring should be searched and the key
1357 signatures should be included."
1358 (let ((lines (epg--list-keys-1 context name mode))
1359 keys cert pointer pointer-1)
1362 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1363 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1364 keys (cons (epg-make-key
1365 (if (aref (car lines) 8)
1366 (cdr (assq (string-to-char (aref (car lines) 8))
1367 epg-key-validity-alist))))
1369 (epg-key-set-sub-key-list
1371 (cons (epg--make-sub-key-1 (car lines))
1372 (epg-key-sub-key-list (car keys)))))
1373 ((member (aref (car lines) 0) '("sub" "ssb"))
1374 (epg-key-set-sub-key-list
1376 (cons (epg--make-sub-key-1 (car lines))
1377 (epg-key-sub-key-list (car keys)))))
1378 ((equal (aref (car lines) 0) "uid")
1379 (epg-key-set-user-id-list
1381 (cons (epg-make-user-id
1382 (if (aref (car lines) 1)
1383 (cdr (assq (string-to-char (aref (car lines) 1))
1384 epg-key-validity-alist)))
1387 (epg-dn-from-string (aref (car lines) 9))
1388 (error (aref (car lines) 9)))
1389 (aref (car lines) 9)))
1390 (epg-key-user-id-list (car keys)))))
1391 ((equal (aref (car lines) 0) "fpr")
1392 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1393 (aref (car lines) 9)))
1394 ((equal (aref (car lines) 0) "sig")
1395 (epg-user-id-set-signature-list
1396 (car (epg-key-user-id-list (car keys)))
1398 (epg-make-key-signature
1399 (if (aref (car lines) 1)
1400 (cdr (assq (string-to-char (aref (car lines) 1))
1401 epg-key-validity-alist)))
1402 (string-to-number (aref (car lines) 3))
1403 (aref (car lines) 4)
1404 (epg--time-from-seconds (aref (car lines) 5))
1405 (epg--time-from-seconds (aref (car lines) 6))
1406 (aref (car lines) 9)
1407 (string-to-number (aref (car lines) 10) 16)
1408 (eq (aref (aref (car lines) 10) 2) ?x))
1409 (epg-user-id-signature-list
1410 (car (epg-key-user-id-list (car keys))))))))
1411 (setq lines (cdr lines)))
1412 (setq keys (nreverse keys)
1415 (epg-key-set-sub-key-list
1417 (nreverse (epg-key-sub-key-list (car pointer))))
1418 (setq pointer-1 (epg-key-set-user-id-list
1420 (nreverse (epg-key-user-id-list (car pointer)))))
1422 (epg-user-id-set-signature-list
1424 (nreverse (epg-user-id-signature-list (car pointer-1))))
1425 (setq pointer-1 (cdr pointer-1)))
1426 (setq pointer (cdr pointer)))
1429 (if (fboundp 'make-temp-file)
1430 (defalias 'epg--make-temp-file 'make-temp-file)
1431 (defvar temporary-file-directory)
1432 ;; stolen from poe.el.
1433 (defun epg--make-temp-file (prefix)
1434 "Create a temporary file.
1435 The returned file name (created by appending some random characters at the end
1436 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1437 is guaranteed to point to a newly created empty file.
1438 You can then use `write-region' to write new data into the file."
1439 (let (tempdir tempfile)
1440 (setq prefix (expand-file-name prefix
1441 (if (featurep 'xemacs)
1443 temporary-file-directory)))
1446 ;; First, create a temporary directory.
1447 (while (condition-case ()
1449 (setq tempdir (make-temp-name
1451 (file-name-directory prefix)
1453 ;; return nil or signal an error.
1454 (make-directory tempdir))
1456 (file-already-exists t)))
1457 (set-file-modes tempdir 448)
1458 ;; Second, create a temporary file in the tempdir.
1459 ;; There *is* a race condition between `make-temp-name'
1460 ;; and `write-region', but we don't care it since we are
1461 ;; in a private directory now.
1462 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1463 (write-region "" nil tempfile nil 'silent)
1464 (set-file-modes tempfile 384)
1465 ;; Finally, make a hard-link from the tempfile.
1466 (while (condition-case ()
1468 (setq file (make-temp-name prefix))
1469 ;; return nil or signal an error.
1470 (add-name-to-file tempfile file))
1472 (file-already-exists t)))
1474 ;; Cleanup the tempfile.
1476 (file-exists-p tempfile)
1477 (delete-file tempfile))
1478 ;; Cleanup the tempdir.
1480 (file-directory-p tempdir)
1481 (delete-directory tempdir))))))
1483 (if (fboundp 'clear-string)
1484 (defalias 'epg--clear-string 'clear-string)
1485 (defun epg--clear-string (string)
1486 (fillarray string 0)))
1489 (defun epg-cancel (context)
1490 (if (buffer-live-p (process-buffer (epg-context-process context)))
1492 (set-buffer (process-buffer (epg-context-process context)))
1493 (epg-context-set-result-for
1496 (epg-context-result-for epg-context 'error)))))
1497 (if (eq (process-status (epg-context-process context)) 'run)
1498 (delete-process (epg-context-process context))))
1501 (defun epg-start-decrypt (context cipher)
1502 "Initiate a decrypt operation on CIPHER.
1503 CIPHER is a data object.
1505 If you use this function, you will need to wait for the completion of
1506 `epg-gpg-program' by using `epg-wait-for-completion' and call
1507 `epg-reset' to clear a temporaly output file.
1508 If you are unsure, use synchronous version of this function
1509 `epg-decrypt-file' or `epg-decrypt-string' instead."
1510 (unless (epg-data-file cipher)
1511 (error "Not a file"))
1512 (epg-context-set-operation context 'decrypt)
1513 (epg-context-set-result context nil)
1514 (epg--start context (list "--decrypt" (epg-data-file cipher)))
1515 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1516 (unless (eq (epg-context-protocol context) 'CMS)
1517 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1520 (defun epg-decrypt-file (context cipher plain)
1521 "Decrypt a file CIPHER and store the result to a file PLAIN.
1522 If PLAIN is nil, it returns the result as a string."
1526 (epg-context-set-output-file context plain)
1527 (epg-context-set-output-file context
1528 (epg--make-temp-file "epg-output")))
1529 (epg-start-decrypt context (epg-make-data-from-file cipher))
1530 (epg-wait-for-completion context)
1531 (if (epg-context-result-for context 'error)
1532 (error "Decrypt failed: %S"
1533 (epg-context-result-for context 'error)))
1535 (epg-read-output context)))
1537 (epg-delete-output-file context))
1538 (epg-reset context)))
1541 (defun epg-decrypt-string (context cipher)
1542 "Decrypt a string CIPHER and return the plain text."
1543 (let ((input-file (epg--make-temp-file "epg-input"))
1544 (coding-system-for-write 'binary))
1547 (write-region cipher nil input-file nil 'quiet)
1548 (epg-context-set-output-file context
1549 (epg--make-temp-file "epg-output"))
1550 (epg-start-decrypt context (epg-make-data-from-file input-file))
1551 (epg-wait-for-completion context)
1552 (if (epg-context-result-for context 'error)
1553 (error "Decrypt failed: %S"
1554 (epg-context-result-for context 'error)))
1555 (epg-read-output context))
1556 (epg-delete-output-file context)
1557 (if (file-exists-p input-file)
1558 (delete-file input-file))
1559 (epg-reset context))))
1562 (defun epg-start-verify (context signature &optional signed-text)
1563 "Initiate a verify operation on SIGNATURE.
1564 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1566 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1567 For a normal or a clear text signature, SIGNED-TEXT should be nil.
1569 If you use this function, you will need to wait for the completion of
1570 `epg-gpg-program' by using `epg-wait-for-completion' and call
1571 `epg-reset' to clear a temporaly output file.
1572 If you are unsure, use synchronous version of this function
1573 `epg-verify-file' or `epg-verify-string' instead."
1574 (epg-context-set-operation context 'verify)
1575 (epg-context-set-result context nil)
1577 ;; Detached signature.
1578 (if (epg-data-file signed-text)
1579 (epg--start context (list "--verify" (epg-data-file signature)
1580 (epg-data-file signed-text)))
1581 (epg--start context (list "--verify" (epg-data-file signature) "-"))
1582 (if (eq (process-status (epg-context-process context)) 'run)
1583 (process-send-string (epg-context-process context)
1584 (epg-data-string signed-text)))
1585 (if (eq (process-status (epg-context-process context)) 'run)
1586 (process-send-eof (epg-context-process context))))
1587 ;; Normal (or cleartext) signature.
1588 (if (epg-data-file signature)
1589 (epg--start context (list "--verify" (epg-data-file signature)))
1590 (epg--start context (list "--verify"))
1591 (if (eq (process-status (epg-context-process context)) 'run)
1592 (process-send-string (epg-context-process context)
1593 (epg-data-string signature)))
1594 (if (eq (process-status (epg-context-process context)) 'run)
1595 (process-send-eof (epg-context-process context))))))
1598 (defun epg-verify-file (context signature &optional signed-text plain)
1599 "Verify a file SIGNATURE.
1600 SIGNED-TEXT and PLAIN are also a file if they are specified.
1602 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1603 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1607 (epg-context-set-output-file context plain)
1608 (epg-context-set-output-file context
1609 (epg--make-temp-file "epg-output")))
1611 (epg-start-verify context
1612 (epg-make-data-from-file signature)
1613 (epg-make-data-from-file signed-text))
1614 (epg-start-verify context
1615 (epg-make-data-from-file signature)))
1616 (epg-wait-for-completion context)
1617 ; (if (epg-context-result-for context 'error)
1618 ; (error "Verify failed: %S"
1619 ; (epg-context-result-for context 'error)))
1621 (epg-read-output context)))
1623 (epg-delete-output-file context))
1624 (epg-reset context)))
1627 (defun epg-verify-string (context signature &optional signed-text)
1628 "Verify a string SIGNATURE.
1629 SIGNED-TEXT is a string if it is specified.
1631 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1632 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1633 (let ((coding-system-for-write 'binary)
1637 (epg-context-set-output-file context
1638 (epg--make-temp-file "epg-output"))
1641 (setq input-file (epg--make-temp-file "epg-signature"))
1642 (write-region signature nil input-file nil 'quiet)
1643 (epg-start-verify context
1644 (epg-make-data-from-file input-file)
1645 (epg-make-data-from-string signed-text)))
1646 (epg-start-verify context (epg-make-data-from-string signature)))
1647 (epg-wait-for-completion context)
1648 ; (if (epg-context-result-for context 'error)
1649 ; (error "Verify failed: %S"
1650 ; (epg-context-result-for context 'error)))
1651 (epg-read-output context))
1652 (epg-delete-output-file context)
1654 (file-exists-p input-file))
1655 (delete-file input-file))
1656 (epg-reset context))))
1659 (defun epg-start-sign (context plain &optional mode)
1660 "Initiate a sign operation on PLAIN.
1661 PLAIN is a data object.
1663 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1664 If it is nil or 'normal, it makes a normal signature.
1665 Otherwise, it makes a clear text signature.
1667 If you use this function, you will need to wait for the completion of
1668 `epg-gpg-program' by using `epg-wait-for-completion' and call
1669 `epg-reset' to clear a temporaly output file.
1670 If you are unsure, use synchronous version of this function
1671 `epg-sign-file' or `epg-sign-string' instead."
1672 (epg-context-set-operation context 'sign)
1673 (epg-context-set-result context nil)
1675 (append (list (if (memq mode '(t detached))
1677 (if (memq mode '(nil normal))
1685 (car (epg-key-sub-key-list signer)))))
1686 (epg-context-signers context)))
1687 (if (epg-data-file plain)
1688 (list (epg-data-file plain)))))
1689 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1690 (unless (eq (epg-context-protocol context) 'CMS)
1691 (epg-wait-for-status context '("BEGIN_SIGNING")))
1692 (when (epg-data-string plain)
1693 (if (eq (process-status (epg-context-process context)) 'run)
1694 (process-send-string (epg-context-process context)
1695 (epg-data-string plain)))
1696 (if (eq (process-status (epg-context-process context)) 'run)
1697 (process-send-eof (epg-context-process context)))))
1700 (defun epg-sign-file (context plain signature &optional mode)
1701 "Sign a file PLAIN and store the result to a file SIGNATURE.
1702 If SIGNATURE is nil, it returns the result as a string.
1703 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1704 If it is nil or 'normal, it makes a normal signature.
1705 Otherwise, it makes a clear text signature."
1709 (epg-context-set-output-file context signature)
1710 (epg-context-set-output-file context
1711 (epg--make-temp-file "epg-output")))
1712 (epg-start-sign context (epg-make-data-from-file plain) mode)
1713 (epg-wait-for-completion context)
1714 (unless (epg-context-result-for context 'sign)
1715 (if (epg-context-result-for context 'error)
1716 (error "Sign failed: %S"
1717 (epg-context-result-for context 'error))
1718 (error "Sign failed")))
1720 (epg-read-output context)))
1722 (epg-delete-output-file context))
1723 (epg-reset context)))
1726 (defun epg-sign-string (context plain &optional mode)
1727 "Sign a string PLAIN and return the output as string.
1728 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1729 If it is nil or 'normal, it makes a normal signature.
1730 Otherwise, it makes a clear text signature."
1733 (epg-context-set-output-file context
1734 (epg--make-temp-file "epg-output"))
1735 (epg-start-sign context (epg-make-data-from-string plain) mode)
1736 (epg-wait-for-completion context)
1737 (unless (epg-context-result-for context 'sign)
1738 (if (epg-context-result-for context 'error)
1739 (error "Sign failed: %S"
1740 (epg-context-result-for context 'error))
1741 (error "Sign failed")))
1742 (epg-read-output context))
1743 (epg-delete-output-file context)
1744 (epg-reset context)))
1747 (defun epg-start-encrypt (context plain recipients
1748 &optional sign always-trust)
1749 "Initiate an encrypt operation on PLAIN.
1750 PLAIN is a data object.
1751 If RECIPIENTS is nil, it performs symmetric encryption.
1753 If you use this function, you will need to wait for the completion of
1754 `epg-gpg-program' by using `epg-wait-for-completion' and call
1755 `epg-reset' to clear a temporaly output file.
1756 If you are unsure, use synchronous version of this function
1757 `epg-encrypt-file' or `epg-encrypt-string' instead."
1758 (epg-context-set-operation context 'encrypt)
1759 (epg-context-set-result context nil)
1761 (append (if always-trust '("--always-trust"))
1762 (if recipients '("--encrypt") '("--symmetric"))
1770 (car (epg-key-sub-key-list
1772 (epg-context-signers context)))))
1778 (car (epg-key-sub-key-list recipient)))))
1780 (if (epg-data-file plain)
1781 (list (epg-data-file plain)))))
1782 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1783 (unless (eq (epg-context-protocol context) 'CMS)
1785 (epg-wait-for-status context '("BEGIN_SIGNING"))
1786 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
1787 (when (epg-data-string plain)
1788 (if (eq (process-status (epg-context-process context)) 'run)
1789 (process-send-string (epg-context-process context)
1790 (epg-data-string plain)))
1791 (if (eq (process-status (epg-context-process context)) 'run)
1792 (process-send-eof (epg-context-process context)))))
1795 (defun epg-encrypt-file (context plain recipients
1796 cipher &optional sign always-trust)
1797 "Encrypt a file PLAIN and store the result to a file CIPHER.
1798 If CIPHER is nil, it returns the result as a string.
1799 If RECIPIENTS is nil, it performs symmetric encryption."
1803 (epg-context-set-output-file context cipher)
1804 (epg-context-set-output-file context
1805 (epg--make-temp-file "epg-output")))
1806 (epg-start-encrypt context (epg-make-data-from-file plain)
1807 recipients sign always-trust)
1808 (epg-wait-for-completion context)
1810 (not (epg-context-result-for context 'sign)))
1811 (if (epg-context-result-for context 'error)
1812 (error "Sign failed: %S"
1813 (epg-context-result-for context 'error))
1814 (error "Sign failed")))
1815 (if (epg-context-result-for context 'error)
1816 (error "Encrypt failed: %S"
1817 (epg-context-result-for context 'error)))
1819 (epg-read-output context)))
1821 (epg-delete-output-file context))
1822 (epg-reset context)))
1825 (defun epg-encrypt-string (context plain recipients
1826 &optional sign always-trust)
1827 "Encrypt a string PLAIN.
1828 If RECIPIENTS is nil, it performs symmetric encryption."
1831 (epg-context-set-output-file context
1832 (epg--make-temp-file "epg-output"))
1833 (epg-start-encrypt context (epg-make-data-from-string plain)
1834 recipients sign always-trust)
1835 (epg-wait-for-completion context)
1837 (not (epg-context-result-for context 'sign)))
1838 (if (epg-context-result-for context 'error)
1839 (error "Sign failed: %S"
1840 (epg-context-result-for context 'error))
1841 (error "Sign failed")))
1842 (if (epg-context-result-for context 'error)
1843 (error "Encrypt failed: %S"
1844 (epg-context-result-for context 'error)))
1845 (epg-read-output context))
1846 (epg-delete-output-file context)
1847 (epg-reset context)))
1850 (defun epg-start-export-keys (context keys)
1851 "Initiate an export keys operation.
1853 If you use this function, you will need to wait for the completion of
1854 `epg-gpg-program' by using `epg-wait-for-completion' and call
1855 `epg-reset' to clear a temporaly output file.
1856 If you are unsure, use synchronous version of this function
1857 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1858 (epg-context-set-operation context 'export-keys)
1859 (epg-context-set-result context nil)
1860 (epg--start context (cons "--export"
1864 (car (epg-key-sub-key-list key))))
1868 (defun epg-export-keys-to-file (context keys file)
1869 "Extract public KEYS."
1873 (epg-context-set-output-file context file)
1874 (epg-context-set-output-file context
1875 (epg--make-temp-file "epg-output")))
1876 (epg-start-export-keys context keys)
1877 (epg-wait-for-completion context)
1878 (if (epg-context-result-for context 'error)
1879 (error "Export keys failed: %S"
1880 (epg-context-result-for context 'error)))
1882 (epg-read-output context)))
1884 (epg-delete-output-file context))
1885 (epg-reset context)))
1888 (defun epg-export-keys-to-string (context keys)
1889 "Extract public KEYS and return them as a string."
1890 (epg-export-keys-to-file context keys nil))
1893 (defun epg-start-import-keys (context keys)
1894 "Initiate an import keys operation.
1895 KEYS is a data object.
1897 If you use this function, you will need to wait for the completion of
1898 `epg-gpg-program' by using `epg-wait-for-completion' and call
1899 `epg-reset' to clear a temporaly output file.
1900 If you are unsure, use synchronous version of this function
1901 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1902 (epg-context-set-operation context 'import-keys)
1903 (epg-context-set-result context nil)
1904 (epg--start context (if (epg-data-file keys)
1905 (list "--import" (epg-data-file keys))
1907 (when (epg-data-string keys)
1908 (if (eq (process-status (epg-context-process context)) 'run)
1909 (process-send-string (epg-context-process context)
1910 (epg-data-string keys)))
1911 (if (eq (process-status (epg-context-process context)) 'run)
1912 (process-send-eof (epg-context-process context)))))
1914 (defun epg--import-keys-1 (context keys)
1917 (epg-start-import-keys context keys)
1918 (epg-wait-for-completion context)
1919 (if (epg-context-result-for context 'error)
1920 (error "Import keys failed: %S"
1921 (epg-context-result-for context 'error))))
1922 (epg-reset context)))
1925 (defun epg-import-keys-from-file (context keys)
1926 "Add keys from a file KEYS."
1927 (epg--import-keys-1 context (epg-make-data-from-file keys)))
1930 (defun epg-import-keys-from-string (context keys)
1931 "Add keys from a string KEYS."
1932 (epg--import-keys-1 context (epg-make-data-from-string keys)))
1935 (defun epg-start-receive-keys (context key-id-list)
1936 "Initiate a receive key operation.
1937 KEY-ID-LIST is a list of key IDs.
1939 If you use this function, you will need to wait for the completion of
1940 `epg-gpg-program' by using `epg-wait-for-completion' and call
1941 `epg-reset' to clear a temporaly output file.
1942 If you are unsure, use synchronous version of this function
1943 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
1944 (epg-context-set-operation context 'receive-keys)
1945 (epg-context-set-result context nil)
1946 (epg--start context (cons "--recv-keys" key-id-list)))
1949 (defun epg-receive-keys (context keys)
1950 "Add keys from server.
1951 KEYS is a list of key IDs"
1954 (epg-start-receive-keys context keys)
1955 (epg-wait-for-completion context)
1956 (if (epg-context-result-for context 'error)
1957 (error "Receive keys failed: %S"
1958 (epg-context-result-for context 'error))))
1959 (epg-reset context)))
1962 (defalias 'epg-import-keys-from-server 'epg-receive-keys)
1965 (defun epg-start-delete-keys (context keys &optional allow-secret)
1966 "Initiate an delete keys operation.
1968 If you use this function, you will need to wait for the completion of
1969 `epg-gpg-program' by using `epg-wait-for-completion' and call
1970 `epg-reset' to clear a temporaly output file.
1971 If you are unsure, use synchronous version of this function
1972 `epg-delete-keys' instead."
1973 (epg-context-set-operation context 'delete-keys)
1974 (epg-context-set-result context nil)
1975 (epg--start context (cons (if allow-secret
1976 "--delete-secret-key"
1981 (car (epg-key-sub-key-list key))))
1985 (defun epg-delete-keys (context keys &optional allow-secret)
1986 "Delete KEYS from the key ring."
1989 (epg-start-delete-keys context keys allow-secret)
1990 (epg-wait-for-completion context)
1991 (if (epg-context-result-for context 'error)
1992 (error "Delete keys failed: %S"
1993 (epg-context-result-for context 'error))))
1994 (epg-reset context)))
1997 (defun epg-start-sign-keys (context keys &optional local)
1998 "Initiate an sign keys operation.
2000 If you use this function, you will need to wait for the completion of
2001 `epg-gpg-program' by using `epg-wait-for-completion' and call
2002 `epg-reset' to clear a temporaly output file.
2003 If you are unsure, use synchronous version of this function
2004 `epg-sign-keys' instead."
2005 (epg-context-set-operation context 'sign-keys)
2006 (epg-context-set-result context nil)
2007 (epg--start context (cons (if local
2013 (car (epg-key-sub-key-list key))))
2017 (defun epg-sign-keys (context keys &optional local)
2018 "Sign KEYS from the key ring."
2021 (epg-start-sign-keys context keys local)
2022 (epg-wait-for-completion context)
2023 (if (epg-context-result-for context 'error)
2024 (error "Sign keys failed: %S"
2025 (epg-context-result-for context 'error))))
2026 (epg-reset context)))
2029 (defun epg-start-generate-key (context parameters)
2030 "Initiate a key generation.
2031 PARAMETERS specifies parameters for the key.
2033 If you use this function, you will need to wait for the completion of
2034 `epg-gpg-program' by using `epg-wait-for-completion' and call
2035 `epg-reset' to clear a temporaly output file.
2036 If you are unsure, use synchronous version of this function
2037 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2038 (epg-context-set-operation context 'generate-key)
2039 (epg-context-set-result context nil)
2040 (if (epg-data-file parameters)
2041 (epg--start context (list "--batch" "--genkey"
2042 (epg-data-file parameters)))
2043 (epg--start context '("--batch" "--genkey"))
2044 (if (eq (process-status (epg-context-process context)) 'run)
2045 (process-send-string (epg-context-process context)
2046 (epg-data-string parameters)))
2047 (if (eq (process-status (epg-context-process context)) 'run)
2048 (process-send-eof (epg-context-process context)))))
2051 (defun epg-generate-key-from-file (context parameters)
2052 "Generate a new key pair.
2053 PARAMETERS is a file which tells how to create the key."
2056 (epg-start-generate-key context (epg-make-data-from-file parameters))
2057 (epg-wait-for-completion context)
2058 (if (epg-context-result-for context 'error)
2059 (error "Generate key failed: %S"
2060 (epg-context-result-for context 'error))))
2061 (epg-reset context)))
2064 (defun epg-generate-key-from-string (context parameters)
2065 "Generate a new key pair.
2066 PARAMETERS is a string which tells how to create the key."
2069 (epg-start-generate-key context (epg-make-data-from-string parameters))
2070 (epg-wait-for-completion context)
2071 (if (epg-context-result-for context 'error)
2072 (error "Generate key failed: %S"
2073 (epg-context-result-for context 'error))))
2074 (epg-reset context)))
2076 (defun epg--decode-hexstring (string)
2078 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
2079 (setq string (replace-match "\\x\\&" t nil string)
2081 (car (read-from-string (concat "\"" string "\"")))))
2083 (defun epg--decode-quotedstring (string)
2085 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2086 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
2088 (if (match-beginning 2)
2089 (setq string (replace-match "\\2" t nil string)
2091 (if (match-beginning 3)
2092 (setq string (replace-match "\\x\\3" t nil string)
2094 (setq string (replace-match "\\\\\\\\\\4" t nil string)
2095 index (+ index 3)))))
2096 (car (read-from-string (concat "\"" string "\"")))))
2098 (defun epg-dn-from-string (string)
2099 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2100 The return value is an alist mapping from types to values."
2102 (length (length string))
2103 alist type value group)
2104 (while (< index length)
2105 (if (eq index (string-match "[ \t\n\r]*" string index))
2106 (setq index (match-end 0)))
2107 (if (eq index (string-match
2108 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2110 (setq type (match-string 1 string)
2111 index (match-end 0))
2112 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2114 (setq type (match-string 1 string)
2115 index (match-end 0))))
2117 (error "Invalid type"))
2118 (if (eq index (string-match
2119 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2121 (setq index (match-end 0)
2122 value (epg--decode-quotedstring (match-string 0 string)))
2123 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
2124 (setq index (match-end 0)
2125 value (epg--decode-hexstring (match-string 1 string)))
2126 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2128 (setq index (match-end 0)
2129 value (epg--decode-quotedstring
2130 (match-string 0 string))))))
2132 (if (stringp (car (car alist)))
2133 (setcar alist (list (cons type value) (car alist)))
2134 (setcar alist (cons (cons type value) (car alist))))
2135 (if (consp (car (car alist)))
2136 (setcar alist (nreverse (car alist))))
2137 (setq alist (cons (cons type value) alist)
2140 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
2141 (setq index (match-end 0)
2142 group (eq (aref string (match-beginning 1)) ?+))))
2145 (defun epg-decode-dn (alist)
2146 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2147 Type names are resolved using `epg-dn-type-alist'."
2150 (if (stringp (car rdn))
2151 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
2153 (format "%s=%s" (cdr entry) (cdr rdn))
2154 (format "%s=%s" (car rdn) (cdr rdn))))
2155 (concat "(" (epg-decode-dn rdn) ")")))
2161 ;;; epg.el ends here