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)
942 (if (consp (epg-context-passphrase-callback context))
943 (car (epg-context-passphrase-callback context))
944 (epg-context-passphrase-callback context))
947 (if (consp (epg-context-passphrase-callback context))
948 (cdr (epg-context-passphrase-callback context)))))
950 (setq passphrase-with-new-line (concat passphrase "\n"))
951 (fillarray passphrase 0)
952 (setq passphrase nil)
953 (process-send-string (epg-context-process context)
954 passphrase-with-new-line)))
956 (epg-context-set-result-for
959 (epg-context-result-for context 'error)))
960 (delete-process (epg-context-process context))))
962 (fillarray passphrase 0))
963 (if passphrase-with-new-line
964 (fillarray passphrase-with-new-line 0))))))
966 (defun epg--status-GET_BOOL (context string)
967 (let ((entry (assoc string epg-prompt-alist))
970 (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
971 (process-send-string (epg-context-process context) "y\n")
972 (process-send-string (epg-context-process context) "n\n"))
974 (epg-context-set-result-for
977 (epg-context-result-for context 'error)))
978 (delete-process (epg-context-process context))))))
980 (defun epg--status-GET_LINE (context string)
981 (let ((entry (assoc string epg-prompt-alist))
984 (process-send-string (epg-context-process context)
988 (concat string ": ")))
991 (epg-context-set-result-for
994 (epg-context-result-for context 'error)))
995 (delete-process (epg-context-process context))))))
997 (defun epg--status-*SIG (context status string)
998 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
999 (let* ((key-id (match-string 1 string))
1000 (user-id (match-string 2 string))
1001 (entry (assoc key-id epg-user-id-alist)))
1002 (epg-context-set-result-for
1005 (cons (epg-make-signature status key-id)
1006 (epg-context-result-for context 'verify)))
1007 (if (eq (epg-context-protocol context) 'CMS)
1009 (setq user-id (epg-dn-from-string user-id))
1012 (setcdr entry user-id)
1013 (setq epg-user-id-alist
1014 (cons (cons key-id user-id) epg-user-id-alist))))
1015 (epg-context-set-result-for
1018 (cons (epg-make-signature status)
1019 (epg-context-result-for context 'verify)))))
1021 (defun epg--status-GOODSIG (context string)
1022 (epg--status-*SIG context 'good string))
1024 (defun epg--status-EXPSIG (context string)
1025 (epg--status-*SIG context 'expired string))
1027 (defun epg--status-EXPKEYSIG (context string)
1028 (epg--status-*SIG context 'expired-key string))
1030 (defun epg--status-REVKEYSIG (context string)
1031 (epg--status-*SIG context 'revoked-key string))
1033 (defun epg--status-BADSIG (context string)
1034 (epg--status-*SIG context 'bad string))
1036 (defun epg--status-NO_PUBKEY (context string)
1037 (let ((signature (car (epg-context-result-for context 'verify))))
1039 (eq (epg-signature-status signature) 'error)
1040 (equal (epg-signature-key-id signature) string))
1041 (epg-signature-set-status signature 'no-pubkey))))
1043 (defun epg--time-from-seconds (seconds)
1044 (let ((number-seconds (string-to-number (concat seconds ".0"))))
1045 (cons (floor (/ number-seconds 65536))
1046 (floor (mod number-seconds 65536)))))
1048 (defun epg--status-ERRSIG (context string)
1049 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1050 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
1052 (let ((signature (epg-make-signature 'error)))
1053 (epg-context-set-result-for
1057 (epg-context-result-for context 'verify)))
1058 (epg-signature-set-key-id
1060 (match-string 1 string))
1061 (epg-signature-set-pubkey-algorithm
1063 (string-to-number (match-string 2 string)))
1064 (epg-signature-set-digest-algorithm
1066 (string-to-number (match-string 3 string)))
1067 (epg-signature-set-class
1069 (string-to-number (match-string 4 string) 16))
1070 (epg-signature-set-creation-time
1072 (epg--time-from-seconds (match-string 5 string))))))
1074 (defun epg--status-VALIDSIG (context string)
1075 (let ((signature (car (epg-context-result-for context 'verify))))
1076 (when (and signature
1077 (eq (epg-signature-status signature) 'good)
1078 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
1079 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
1082 (epg-signature-set-fingerprint
1084 (match-string 1 string))
1085 (epg-signature-set-creation-time
1087 (epg--time-from-seconds (match-string 2 string)))
1088 (epg-signature-set-expiration-time
1090 (epg--time-from-seconds (match-string 3 string)))
1091 (epg-signature-set-version
1093 (string-to-number (match-string 4 string)))
1094 (epg-signature-set-pubkey-algorithm
1096 (string-to-number (match-string 5 string)))
1097 (epg-signature-set-digest-algorithm
1099 (string-to-number (match-string 6 string)))
1100 (epg-signature-set-class
1102 (string-to-number (match-string 7 string) 16)))))
1104 (defun epg--status-TRUST_UNDEFINED (context string)
1105 (let ((signature (car (epg-context-result-for context 'verify))))
1107 (eq (epg-signature-status signature) 'good))
1108 (epg-signature-set-validity signature 'undefined))))
1110 (defun epg--status-TRUST_NEVER (context string)
1111 (let ((signature (car (epg-context-result-for context 'verify))))
1113 (eq (epg-signature-status signature) 'good))
1114 (epg-signature-set-validity signature 'never))))
1116 (defun epg--status-TRUST_MARGINAL (context string)
1117 (let ((signature (car (epg-context-result-for context 'verify))))
1119 (eq (epg-signature-status signature) 'marginal))
1120 (epg-signature-set-validity signature 'marginal))))
1122 (defun epg--status-TRUST_FULLY (context string)
1123 (let ((signature (car (epg-context-result-for context 'verify))))
1125 (eq (epg-signature-status signature) 'good))
1126 (epg-signature-set-validity signature 'full))))
1128 (defun epg--status-TRUST_ULTIMATE (context string)
1129 (let ((signature (car (epg-context-result-for context 'verify))))
1131 (eq (epg-signature-status signature) 'good))
1132 (epg-signature-set-validity signature 'ultimate))))
1134 (defun epg--status-PROGRESS (context string)
1135 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1137 (funcall (if (consp (epg-context-progress-callback context))
1138 (car (epg-context-progress-callback context))
1139 (epg-context-progress-callback context))
1141 (match-string 1 string)
1142 (match-string 2 string)
1143 (string-to-number (match-string 3 string))
1144 (string-to-number (match-string 4 string))
1145 (if (consp (epg-context-progress-callback context))
1146 (cdr (epg-context-progress-callback context))))))
1148 (defun epg--status-DECRYPTION_FAILED (context string)
1149 (epg-context-set-result-for
1151 (cons '(decryption-failed)
1152 (epg-context-result-for context 'error))))
1154 (defun epg--status-NODATA (context string)
1155 (epg-context-set-result-for
1157 (cons (list 'no-data (cons 'reason (string-to-number string)))
1158 (epg-context-result-for context 'error))))
1160 (defun epg--status-UNEXPECTED (context string)
1161 (epg-context-set-result-for
1163 (cons (list 'unexpected (cons 'reason (string-to-number string)))
1164 (epg-context-result-for context 'error))))
1166 (defun epg--status-KEYEXPIRED (context string)
1167 (epg-context-set-result-for
1169 (cons (list 'key-expired (cons 'expiration-time
1170 (epg--time-from-seconds string)))
1171 (epg-context-result-for context 'error))))
1173 (defun epg--status-KEYREVOKED (context string)
1174 (epg-context-set-result-for
1176 (cons '(key-revoked)
1177 (epg-context-result-for context 'error))))
1179 (defun epg--status-BADARMOR (context string)
1180 (epg-context-set-result-for
1183 (epg-context-result-for context 'error))))
1185 (defun epg--status-INV_RECP (context string)
1186 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1187 (epg-context-set-result-for
1189 (cons (list 'invalid-recipient
1191 (string-to-number (match-string 1 string)))
1192 (cons 'requested-recipient
1193 (match-string 2 string)))
1194 (epg-context-result-for context 'error)))))
1196 (defun epg--status-NO_RECP (context string)
1197 (epg-context-set-result-for
1199 (cons '(no-recipients)
1200 (epg-context-result-for context 'error))))
1202 (defun epg--status-DELETE_PROBLEM (context string)
1203 (if (string-match "\\`\\([0-9]+\\)" string)
1204 (epg-context-set-result-for
1206 (cons (list 'delete-problem
1207 (cons 'reason (string-to-number (match-string 1 string))))
1208 (epg-context-result-for context 'error)))))
1210 (defun epg--status-SIG_CREATED (context string)
1211 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1212 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1213 (epg-context-set-result-for
1215 (cons (epg-make-new-signature
1216 (cdr (assq (aref (match-string 1 string) 0)
1217 epg-new-signature-type-alist))
1218 (string-to-number (match-string 2 string))
1219 (string-to-number (match-string 3 string))
1220 (string-to-number (match-string 4 string) 16)
1221 (epg--time-from-seconds (match-string 5 string))
1222 (substring string (match-end 0)))
1223 (epg-context-result-for context 'sign)))))
1225 (defun epg--status-KEY_CREATED (context string)
1226 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1227 (epg-context-set-result-for
1228 context 'generate-key
1229 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1230 (cons 'fingerprint (match-string 2 string)))
1231 (epg-context-result-for context 'generate-key)))))
1233 (defun epg--status-KEY_NOT_CREATED (context string)
1234 (epg-context-set-result-for
1236 (cons '(key-not-created)
1237 (epg-context-result-for context 'error))))
1239 (defun epg--status-IMPORTED (context string)
1240 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1241 (let* ((key-id (match-string 1 string))
1242 (user-id (match-string 2 string))
1243 (entry (assoc key-id epg-user-id-alist)))
1245 (setcdr entry user-id)
1246 (setq epg-user-id-alist (cons (cons key-id user-id)
1247 epg-user-id-alist)))
1248 (epg-context-set-result-for
1250 (cons (list (cons 'key-id key-id)
1251 (cons 'user-id user-id))
1252 (epg-context-result-for context 'import))))))
1254 (defun epg--status-IMPORT_OK (context string)
1255 (let ((result (epg-context-result-for context 'import)))
1257 (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string))
1259 (append (list (cons 'reason
1261 (match-string 1 string))))
1262 (if (match-beginning 2)
1263 (list (cons 'fingerprint
1264 (match-string 3 string))))
1267 (defun epg--status-IMPORT_PROBLEM (context string)
1268 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1269 (epg-context-set-result-for
1271 (cons (cons 'import-problem
1272 (append (list (cons 'reason
1274 (match-string 1 string))))
1275 (if (match-beginning 2)
1276 (list (cons 'fingerprint
1277 (match-string 3 string))))))
1278 (epg-context-result-for context 'error)))))
1280 (defun epg-passphrase-callback-function (context key-id handback)
1281 (if (eq key-id 'SYM)
1282 (read-passwd "Passphrase for symmetric encryption: "
1283 (eq (epg-context-operation context) 'encrypt))
1285 (if (eq key-id 'PIN)
1286 "Passphrase for PIN: "
1287 (let ((entry (assoc key-id epg-user-id-alist)))
1289 (format "Passphrase for %s %s: " key-id (cdr entry))
1290 (format "Passphrase for %s: " key-id)))))))
1292 (defun epg-progress-callback-function (context what char current total
1294 (message "%s: %d%%/%d%%" what current total))
1296 (defun epg--list-keys-1 (context name mode)
1297 (let ((args (append (list "--with-colons" "--no-greeting" "--batch"
1298 "--with-fingerprint"
1299 "--with-fingerprint"
1300 (if (memq mode '(t secret))
1301 "--list-secret-keys"
1302 (if (memq mode '(nil public))
1305 (unless (eq (epg-context-protocol context) 'CMS)
1306 '("--fixed-list-mode"))
1307 (if name (list name))))
1308 keys string field index)
1310 (apply #'call-process
1311 (if (eq (epg-context-protocol context) 'CMS)
1314 nil (list t nil) nil args)
1315 (goto-char (point-min))
1316 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1317 (setq keys (cons (make-vector 15 nil) keys)
1318 string (match-string 0)
1322 (string-match "\\([^:]+\\)?:" string index))
1323 (setq index (match-end 0))
1324 (aset (car keys) field (match-string 1 string))
1325 (setq field (1+ field))))
1328 (defun epg--make-sub-key-1 (line)
1331 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1333 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1335 (member (aref line 0) '("sec" "ssb"))
1336 (string-to-number (aref line 3))
1337 (string-to-number (aref line 2))
1339 (epg--time-from-seconds (aref line 5))
1340 (epg--time-from-seconds (aref line 6))))
1343 (defun epg-list-keys (context &optional name mode)
1344 "Return a list of epg-key objects matched with NAME.
1345 If MODE is nil or 'public, only public keyring should be searched.
1346 If MODE is t or 'secret, only secret keyring should be searched.
1347 Otherwise, only public keyring should be searched and the key
1348 signatures should be included."
1349 (let ((lines (epg--list-keys-1 context name mode))
1350 keys cert pointer pointer-1)
1353 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1354 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1355 keys (cons (epg-make-key
1356 (if (aref (car lines) 8)
1357 (cdr (assq (string-to-char (aref (car lines) 8))
1358 epg-key-validity-alist))))
1360 (epg-key-set-sub-key-list
1362 (cons (epg--make-sub-key-1 (car lines))
1363 (epg-key-sub-key-list (car keys)))))
1364 ((member (aref (car lines) 0) '("sub" "ssb"))
1365 (epg-key-set-sub-key-list
1367 (cons (epg--make-sub-key-1 (car lines))
1368 (epg-key-sub-key-list (car keys)))))
1369 ((equal (aref (car lines) 0) "uid")
1370 (epg-key-set-user-id-list
1372 (cons (epg-make-user-id
1373 (if (aref (car lines) 1)
1374 (cdr (assq (string-to-char (aref (car lines) 1))
1375 epg-key-validity-alist)))
1378 (epg-dn-from-string (aref (car lines) 9))
1379 (error (aref (car lines) 9)))
1380 (aref (car lines) 9)))
1381 (epg-key-user-id-list (car keys)))))
1382 ((equal (aref (car lines) 0) "fpr")
1383 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1384 (aref (car lines) 9)))
1385 ((equal (aref (car lines) 0) "sig")
1386 (epg-user-id-set-signature-list
1387 (car (epg-key-user-id-list (car keys)))
1389 (epg-make-key-signature
1390 (if (aref (car lines) 1)
1391 (cdr (assq (string-to-char (aref (car lines) 1))
1392 epg-key-validity-alist)))
1393 (string-to-number (aref (car lines) 3))
1394 (aref (car lines) 4)
1395 (epg--time-from-seconds (aref (car lines) 5))
1396 (epg--time-from-seconds (aref (car lines) 6))
1397 (aref (car lines) 9)
1398 (string-to-number (aref (car lines) 10) 16)
1399 (eq (aref (aref (car lines) 10) 2) ?x))
1400 (epg-user-id-signature-list
1401 (car (epg-key-user-id-list (car keys))))))))
1402 (setq lines (cdr lines)))
1403 (setq keys (nreverse keys)
1406 (epg-key-set-sub-key-list
1408 (nreverse (epg-key-sub-key-list (car pointer))))
1409 (setq pointer-1 (epg-key-set-user-id-list
1411 (nreverse (epg-key-user-id-list (car pointer)))))
1413 (epg-user-id-set-signature-list
1415 (nreverse (epg-user-id-signature-list (car pointer-1))))
1416 (setq pointer-1 (cdr pointer-1)))
1417 (setq pointer (cdr pointer)))
1420 (if (fboundp 'make-temp-file)
1421 (defalias 'epg--make-temp-file 'make-temp-file)
1422 (defvar temporary-file-directory)
1423 ;; stolen from poe.el.
1424 (defun epg--make-temp-file (prefix)
1425 "Create a temporary file.
1426 The returned file name (created by appending some random characters at the end
1427 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1428 is guaranteed to point to a newly created empty file.
1429 You can then use `write-region' to write new data into the file."
1430 (let (tempdir tempfile)
1431 (setq prefix (expand-file-name prefix
1432 (if (featurep 'xemacs)
1434 temporary-file-directory)))
1437 ;; First, create a temporary directory.
1438 (while (condition-case ()
1440 (setq tempdir (make-temp-name
1442 (file-name-directory prefix)
1444 ;; return nil or signal an error.
1445 (make-directory tempdir))
1447 (file-already-exists t)))
1448 (set-file-modes tempdir 448)
1449 ;; Second, create a temporary file in the tempdir.
1450 ;; There *is* a race condition between `make-temp-name'
1451 ;; and `write-region', but we don't care it since we are
1452 ;; in a private directory now.
1453 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1454 (write-region "" nil tempfile nil 'silent)
1455 (set-file-modes tempfile 384)
1456 ;; Finally, make a hard-link from the tempfile.
1457 (while (condition-case ()
1459 (setq file (make-temp-name prefix))
1460 ;; return nil or signal an error.
1461 (add-name-to-file tempfile file))
1463 (file-already-exists t)))
1465 ;; Cleanup the tempfile.
1467 (file-exists-p tempfile)
1468 (delete-file tempfile))
1469 ;; Cleanup the tempdir.
1471 (file-directory-p tempdir)
1472 (delete-directory tempdir))))))
1475 (defun epg-cancel (context)
1476 (if (buffer-live-p (process-buffer (epg-context-process context)))
1478 (set-buffer (process-buffer (epg-context-process context)))
1479 (epg-context-set-result-for
1482 (epg-context-result-for epg-context 'error)))))
1483 (if (eq (process-status (epg-context-process context)) 'run)
1484 (delete-process (epg-context-process context))))
1487 (defun epg-start-decrypt (context cipher)
1488 "Initiate a decrypt operation on CIPHER.
1489 CIPHER is a data object.
1491 If you use this function, you will need to wait for the completion of
1492 `epg-gpg-program' by using `epg-wait-for-completion' and call
1493 `epg-reset' to clear a temporaly output file.
1494 If you are unsure, use synchronous version of this function
1495 `epg-decrypt-file' or `epg-decrypt-string' instead."
1496 (unless (epg-data-file cipher)
1497 (error "Not a file"))
1498 (epg-context-set-operation context 'decrypt)
1499 (epg-context-set-result context nil)
1500 (epg--start context (list "--decrypt" (epg-data-file cipher)))
1501 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1502 (unless (eq (epg-context-protocol context) 'CMS)
1503 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1506 (defun epg-decrypt-file (context cipher plain)
1507 "Decrypt a file CIPHER and store the result to a file PLAIN.
1508 If PLAIN is nil, it returns the result as a string."
1512 (epg-context-set-output-file context plain)
1513 (epg-context-set-output-file context
1514 (epg--make-temp-file "epg-output")))
1515 (epg-start-decrypt context (epg-make-data-from-file cipher))
1516 (epg-wait-for-completion context)
1517 (if (epg-context-result-for context 'error)
1518 (error "Decrypt failed: %S"
1519 (epg-context-result-for context 'error)))
1521 (epg-read-output context)))
1523 (epg-delete-output-file context))
1524 (epg-reset context)))
1527 (defun epg-decrypt-string (context cipher)
1528 "Decrypt a string CIPHER and return the plain text."
1529 (let ((input-file (epg--make-temp-file "epg-input"))
1530 (coding-system-for-write 'binary))
1533 (write-region cipher nil input-file nil 'quiet)
1534 (epg-context-set-output-file context
1535 (epg--make-temp-file "epg-output"))
1536 (epg-start-decrypt context (epg-make-data-from-file input-file))
1537 (epg-wait-for-completion context)
1538 (if (epg-context-result-for context 'error)
1539 (error "Decrypt failed: %S"
1540 (epg-context-result-for context 'error)))
1541 (epg-read-output context))
1542 (epg-delete-output-file context)
1543 (if (file-exists-p input-file)
1544 (delete-file input-file))
1545 (epg-reset context))))
1548 (defun epg-start-verify (context signature &optional signed-text)
1549 "Initiate a verify operation on SIGNATURE.
1550 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1552 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1553 For a normal or a clear text signature, SIGNED-TEXT should be nil.
1555 If you use this function, you will need to wait for the completion of
1556 `epg-gpg-program' by using `epg-wait-for-completion' and call
1557 `epg-reset' to clear a temporaly output file.
1558 If you are unsure, use synchronous version of this function
1559 `epg-verify-file' or `epg-verify-string' instead."
1560 (epg-context-set-operation context 'verify)
1561 (epg-context-set-result context nil)
1563 ;; Detached signature.
1564 (if (epg-data-file signed-text)
1565 (epg--start context (list "--verify" (epg-data-file signature)
1566 (epg-data-file signed-text)))
1567 (epg--start context (list "--verify" (epg-data-file signature) "-"))
1568 (if (eq (process-status (epg-context-process context)) 'run)
1569 (process-send-string (epg-context-process context)
1570 (epg-data-string signed-text)))
1571 (if (eq (process-status (epg-context-process context)) 'run)
1572 (process-send-eof (epg-context-process context))))
1573 ;; Normal (or cleartext) signature.
1574 (if (epg-data-file signature)
1575 (epg--start context (list "--verify" (epg-data-file signature)))
1576 (epg--start context (list "--verify"))
1577 (if (eq (process-status (epg-context-process context)) 'run)
1578 (process-send-string (epg-context-process context)
1579 (epg-data-string signature)))
1580 (if (eq (process-status (epg-context-process context)) 'run)
1581 (process-send-eof (epg-context-process context))))))
1584 (defun epg-verify-file (context signature &optional signed-text plain)
1585 "Verify a file SIGNATURE.
1586 SIGNED-TEXT and PLAIN are also a file if they are specified.
1588 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1589 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1593 (epg-context-set-output-file context plain)
1594 (epg-context-set-output-file context
1595 (epg--make-temp-file "epg-output")))
1597 (epg-start-verify context
1598 (epg-make-data-from-file signature)
1599 (epg-make-data-from-file signed-text))
1600 (epg-start-verify context
1601 (epg-make-data-from-file signature)))
1602 (epg-wait-for-completion context)
1603 ; (if (epg-context-result-for context 'error)
1604 ; (error "Verify failed: %S"
1605 ; (epg-context-result-for context 'error)))
1607 (epg-read-output context)))
1609 (epg-delete-output-file context))
1610 (epg-reset context)))
1613 (defun epg-verify-string (context signature &optional signed-text)
1614 "Verify a string SIGNATURE.
1615 SIGNED-TEXT is a string if it is specified.
1617 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1618 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1619 (let ((coding-system-for-write 'binary)
1623 (epg-context-set-output-file context
1624 (epg--make-temp-file "epg-output"))
1627 (setq input-file (epg--make-temp-file "epg-signature"))
1628 (write-region signature nil input-file nil 'quiet)
1629 (epg-start-verify context
1630 (epg-make-data-from-file input-file)
1631 (epg-make-data-from-string signed-text)))
1632 (epg-start-verify context (epg-make-data-from-string signature)))
1633 (epg-wait-for-completion context)
1634 ; (if (epg-context-result-for context 'error)
1635 ; (error "Verify failed: %S"
1636 ; (epg-context-result-for context 'error)))
1637 (epg-read-output context))
1638 (epg-delete-output-file context)
1640 (file-exists-p input-file))
1641 (delete-file input-file))
1642 (epg-reset context))))
1645 (defun epg-start-sign (context plain &optional mode)
1646 "Initiate a sign operation on PLAIN.
1647 PLAIN is a data object.
1649 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1650 If it is nil or 'normal, it makes a normal signature.
1651 Otherwise, it makes a clear text signature.
1653 If you use this function, you will need to wait for the completion of
1654 `epg-gpg-program' by using `epg-wait-for-completion' and call
1655 `epg-reset' to clear a temporaly output file.
1656 If you are unsure, use synchronous version of this function
1657 `epg-sign-file' or `epg-sign-string' instead."
1658 (epg-context-set-operation context 'sign)
1659 (epg-context-set-result context nil)
1661 (append (list (if (memq mode '(t detached))
1663 (if (memq mode '(nil normal))
1671 (car (epg-key-sub-key-list signer)))))
1672 (epg-context-signers context)))
1673 (if (epg-data-file plain)
1674 (list (epg-data-file plain)))))
1675 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1676 (unless (eq (epg-context-protocol context) 'CMS)
1677 (epg-wait-for-status context '("BEGIN_SIGNING")))
1678 (when (epg-data-string plain)
1679 (if (eq (process-status (epg-context-process context)) 'run)
1680 (process-send-string (epg-context-process context)
1681 (epg-data-string plain)))
1682 (if (eq (process-status (epg-context-process context)) 'run)
1683 (process-send-eof (epg-context-process context)))))
1686 (defun epg-sign-file (context plain signature &optional mode)
1687 "Sign a file PLAIN and store the result to a file SIGNATURE.
1688 If SIGNATURE is nil, it returns the result as a string.
1689 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1690 If it is nil or 'normal, it makes a normal signature.
1691 Otherwise, it makes a clear text signature."
1695 (epg-context-set-output-file context signature)
1696 (epg-context-set-output-file context
1697 (epg--make-temp-file "epg-output")))
1698 (epg-start-sign context (epg-make-data-from-file plain) mode)
1699 (epg-wait-for-completion context)
1700 (unless (epg-context-result-for context 'sign)
1701 (if (epg-context-result-for context 'error)
1702 (error "Sign failed: %S"
1703 (epg-context-result-for context 'error))
1704 (error "Sign failed")))
1706 (epg-read-output context)))
1708 (epg-delete-output-file context))
1709 (epg-reset context)))
1712 (defun epg-sign-string (context plain &optional mode)
1713 "Sign a string PLAIN and return the output as string.
1714 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1715 If it is nil or 'normal, it makes a normal signature.
1716 Otherwise, it makes a clear text signature."
1719 (epg-context-set-output-file context
1720 (epg--make-temp-file "epg-output"))
1721 (epg-start-sign context (epg-make-data-from-string plain) mode)
1722 (epg-wait-for-completion context)
1723 (unless (epg-context-result-for context 'sign)
1724 (if (epg-context-result-for context 'error)
1725 (error "Sign failed: %S"
1726 (epg-context-result-for context 'error))
1727 (error "Sign failed")))
1728 (epg-read-output context))
1729 (epg-delete-output-file context)
1730 (epg-reset context)))
1733 (defun epg-start-encrypt (context plain recipients
1734 &optional sign always-trust)
1735 "Initiate an encrypt operation on PLAIN.
1736 PLAIN is a data object.
1737 If RECIPIENTS is nil, it performs symmetric encryption.
1739 If you use this function, you will need to wait for the completion of
1740 `epg-gpg-program' by using `epg-wait-for-completion' and call
1741 `epg-reset' to clear a temporaly output file.
1742 If you are unsure, use synchronous version of this function
1743 `epg-encrypt-file' or `epg-encrypt-string' instead."
1744 (epg-context-set-operation context 'encrypt)
1745 (epg-context-set-result context nil)
1747 (append (if always-trust '("--always-trust"))
1748 (if recipients '("--encrypt") '("--symmetric"))
1756 (car (epg-key-sub-key-list
1758 (epg-context-signers context)))))
1764 (car (epg-key-sub-key-list recipient)))))
1766 (if (epg-data-file plain)
1767 (list (epg-data-file plain)))))
1768 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1769 (unless (eq (epg-context-protocol context) 'CMS)
1771 (epg-wait-for-status context '("BEGIN_SIGNING"))
1772 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
1773 (when (epg-data-string plain)
1774 (if (eq (process-status (epg-context-process context)) 'run)
1775 (process-send-string (epg-context-process context)
1776 (epg-data-string plain)))
1777 (if (eq (process-status (epg-context-process context)) 'run)
1778 (process-send-eof (epg-context-process context)))))
1781 (defun epg-encrypt-file (context plain recipients
1782 cipher &optional sign always-trust)
1783 "Encrypt a file PLAIN and store the result to a file CIPHER.
1784 If CIPHER is nil, it returns the result as a string.
1785 If RECIPIENTS is nil, it performs symmetric encryption."
1789 (epg-context-set-output-file context cipher)
1790 (epg-context-set-output-file context
1791 (epg--make-temp-file "epg-output")))
1792 (epg-start-encrypt context (epg-make-data-from-file plain)
1793 recipients sign always-trust)
1794 (epg-wait-for-completion context)
1796 (not (epg-context-result-for context 'sign)))
1797 (if (epg-context-result-for context 'error)
1798 (error "Sign failed: %S"
1799 (epg-context-result-for context 'error))
1800 (error "Sign failed")))
1801 (if (epg-context-result-for context 'error)
1802 (error "Encrypt failed: %S"
1803 (epg-context-result-for context 'error)))
1805 (epg-read-output context)))
1807 (epg-delete-output-file context))
1808 (epg-reset context)))
1811 (defun epg-encrypt-string (context plain recipients
1812 &optional sign always-trust)
1813 "Encrypt a string PLAIN.
1814 If RECIPIENTS is nil, it performs symmetric encryption."
1817 (epg-context-set-output-file context
1818 (epg--make-temp-file "epg-output"))
1819 (epg-start-encrypt context (epg-make-data-from-string plain)
1820 recipients sign always-trust)
1821 (epg-wait-for-completion context)
1823 (not (epg-context-result-for context 'sign)))
1824 (if (epg-context-result-for context 'error)
1825 (error "Sign failed: %S"
1826 (epg-context-result-for context 'error))
1827 (error "Sign failed")))
1828 (if (epg-context-result-for context 'error)
1829 (error "Encrypt failed: %S"
1830 (epg-context-result-for context 'error)))
1831 (epg-read-output context))
1832 (epg-delete-output-file context)
1833 (epg-reset context)))
1836 (defun epg-start-export-keys (context keys)
1837 "Initiate an export keys operation.
1839 If you use this function, you will need to wait for the completion of
1840 `epg-gpg-program' by using `epg-wait-for-completion' and call
1841 `epg-reset' to clear a temporaly output file.
1842 If you are unsure, use synchronous version of this function
1843 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1844 (epg-context-set-operation context 'export-keys)
1845 (epg-context-set-result context nil)
1846 (epg--start context (cons "--export"
1850 (car (epg-key-sub-key-list key))))
1854 (defun epg-export-keys-to-file (context keys file)
1855 "Extract public KEYS."
1859 (epg-context-set-output-file context file)
1860 (epg-context-set-output-file context
1861 (epg--make-temp-file "epg-output")))
1862 (epg-start-export-keys context keys)
1863 (epg-wait-for-completion context)
1864 (if (epg-context-result-for context 'error)
1865 (error "Export keys failed: %S"
1866 (epg-context-result-for context 'error)))
1868 (epg-read-output context)))
1870 (epg-delete-output-file context))
1871 (epg-reset context)))
1874 (defun epg-export-keys-to-string (context keys)
1875 "Extract public KEYS and return them as a string."
1876 (epg-export-keys-to-file context keys nil))
1879 (defun epg-start-import-keys (context keys)
1880 "Initiate an import keys operation.
1881 KEYS is a data object.
1883 If you use this function, you will need to wait for the completion of
1884 `epg-gpg-program' by using `epg-wait-for-completion' and call
1885 `epg-reset' to clear a temporaly output file.
1886 If you are unsure, use synchronous version of this function
1887 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1888 (epg-context-set-operation context 'import-keys)
1889 (epg-context-set-result context nil)
1890 (epg--start context (if (epg-data-file keys)
1891 (list "--import" (epg-data-file keys))
1893 (when (epg-data-string keys)
1894 (if (eq (process-status (epg-context-process context)) 'run)
1895 (process-send-string (epg-context-process context)
1896 (epg-data-string keys)))
1897 (if (eq (process-status (epg-context-process context)) 'run)
1898 (process-send-eof (epg-context-process context)))))
1900 (defun epg--import-keys-1 (context keys)
1903 (epg-start-import-keys context keys)
1904 (epg-wait-for-completion context)
1905 (if (epg-context-result-for context 'error)
1906 (error "Import keys failed: %S"
1907 (epg-context-result-for context 'error))))
1908 (epg-reset context)))
1911 (defun epg-import-keys-from-file (context keys)
1912 "Add keys from a file KEYS."
1913 (epg--import-keys-1 context (epg-make-data-from-file keys)))
1916 (defun epg-import-keys-from-string (context keys)
1917 "Add keys from a string KEYS."
1918 (epg--import-keys-1 context (epg-make-data-from-string keys)))
1921 (defun epg-start-receive-keys (context key-id-list)
1922 "Initiate a receive key operation.
1923 KEY-ID-LIST is a list of key IDs.
1925 If you use this function, you will need to wait for the completion of
1926 `epg-gpg-program' by using `epg-wait-for-completion' and call
1927 `epg-reset' to clear a temporaly output file.
1928 If you are unsure, use synchronous version of this function
1929 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
1930 (epg-context-set-operation context 'receive-keys)
1931 (epg-context-set-result context nil)
1932 (epg--start context (cons "--recv-keys" key-id-list)))
1935 (defun epg-receive-keys (context keys)
1936 "Add keys from server.
1937 KEYS is a list of key IDs"
1940 (epg-start-receive-keys context keys)
1941 (epg-wait-for-completion context)
1942 (if (epg-context-result-for context 'error)
1943 (error "Receive keys failed: %S"
1944 (epg-context-result-for context 'error))))
1945 (epg-reset context)))
1948 (defalias 'epg-import-keys-from-server 'epg-receive-keys)
1951 (defun epg-start-delete-keys (context keys &optional allow-secret)
1952 "Initiate an delete keys operation.
1954 If you use this function, you will need to wait for the completion of
1955 `epg-gpg-program' by using `epg-wait-for-completion' and call
1956 `epg-reset' to clear a temporaly output file.
1957 If you are unsure, use synchronous version of this function
1958 `epg-delete-keys' instead."
1959 (epg-context-set-operation context 'delete-keys)
1960 (epg-context-set-result context nil)
1961 (epg--start context (cons (if allow-secret
1962 "--delete-secret-key"
1967 (car (epg-key-sub-key-list key))))
1971 (defun epg-delete-keys (context keys &optional allow-secret)
1972 "Delete KEYS from the key ring."
1975 (epg-start-delete-keys context keys allow-secret)
1976 (epg-wait-for-completion context)
1977 (if (epg-context-result-for context 'error)
1978 (error "Delete keys failed: %S"
1979 (epg-context-result-for context 'error))))
1980 (epg-reset context)))
1983 (defun epg-start-sign-keys (context keys &optional local)
1984 "Initiate an sign keys operation.
1986 If you use this function, you will need to wait for the completion of
1987 `epg-gpg-program' by using `epg-wait-for-completion' and call
1988 `epg-reset' to clear a temporaly output file.
1989 If you are unsure, use synchronous version of this function
1990 `epg-sign-keys' instead."
1991 (epg-context-set-operation context 'sign-keys)
1992 (epg-context-set-result context nil)
1993 (epg--start context (cons (if local
1999 (car (epg-key-sub-key-list key))))
2003 (defun epg-sign-keys (context keys &optional local)
2004 "Sign KEYS from the key ring."
2007 (epg-start-sign-keys context keys local)
2008 (epg-wait-for-completion context)
2009 (if (epg-context-result-for context 'error)
2010 (error "Sign keys failed: %S"
2011 (epg-context-result-for context 'error))))
2012 (epg-reset context)))
2015 (defun epg-start-generate-key (context parameters)
2016 "Initiate a key generation.
2017 PARAMETERS specifies parameters for the key.
2019 If you use this function, you will need to wait for the completion of
2020 `epg-gpg-program' by using `epg-wait-for-completion' and call
2021 `epg-reset' to clear a temporaly output file.
2022 If you are unsure, use synchronous version of this function
2023 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2024 (epg-context-set-operation context 'generate-key)
2025 (epg-context-set-result context nil)
2026 (if (epg-data-file parameters)
2027 (epg--start context (list "--batch" "--genkey"
2028 (epg-data-file parameters)))
2029 (epg--start context '("--batch" "--genkey"))
2030 (if (eq (process-status (epg-context-process context)) 'run)
2031 (process-send-string (epg-context-process context)
2032 (epg-data-string parameters)))
2033 (if (eq (process-status (epg-context-process context)) 'run)
2034 (process-send-eof (epg-context-process context)))))
2037 (defun epg-generate-key-from-file (context parameters)
2038 "Generate a new key pair.
2039 PARAMETERS is a file which tells how to create the key."
2042 (epg-start-generate-key context (epg-make-data-from-file parameters))
2043 (epg-wait-for-completion context)
2044 (if (epg-context-result-for context 'error)
2045 (error "Generate key failed: %S"
2046 (epg-context-result-for context 'error))))
2047 (epg-reset context)))
2050 (defun epg-generate-key-from-string (context parameters)
2051 "Generate a new key pair.
2052 PARAMETERS is a string which tells how to create the key."
2055 (epg-start-generate-key context (epg-make-data-from-string parameters))
2056 (epg-wait-for-completion context)
2057 (if (epg-context-result-for context 'error)
2058 (error "Generate key failed: %S"
2059 (epg-context-result-for context 'error))))
2060 (epg-reset context)))
2062 (defun epg--decode-hexstring (string)
2064 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
2065 (setq string (replace-match "\\x\\&" t nil string)
2067 (car (read-from-string (concat "\"" string "\"")))))
2069 (defun epg--decode-quotedstring (string)
2071 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2072 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
2074 (if (match-beginning 2)
2075 (setq string (replace-match "\\2" t nil string)
2077 (if (match-beginning 3)
2078 (setq string (replace-match "\\x\\3" t nil string)
2080 (setq string (replace-match "\\\\\\\\\\4" t nil string)
2081 index (+ index 3)))))
2082 (car (read-from-string (concat "\"" string "\"")))))
2084 (defun epg-dn-from-string (string)
2085 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2086 The return value is an alist mapping from types to values."
2088 (length (length string))
2089 alist type value group)
2090 (while (< index length)
2091 (if (eq index (string-match "[ \t\n\r]*" string index))
2092 (setq index (match-end 0)))
2093 (if (eq index (string-match
2094 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2096 (setq type (match-string 1 string)
2097 index (match-end 0))
2098 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2100 (setq type (match-string 1 string)
2101 index (match-end 0))))
2103 (error "Invalid type"))
2104 (if (eq index (string-match
2105 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2107 (setq index (match-end 0)
2108 value (epg--decode-quotedstring (match-string 0 string)))
2109 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
2110 (setq index (match-end 0)
2111 value (epg--decode-hexstring (match-string 1 string)))
2112 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2114 (setq index (match-end 0)
2115 value (epg--decode-quotedstring
2116 (match-string 0 string))))))
2118 (if (stringp (car (car alist)))
2119 (setcar alist (list (cons type value) (car alist)))
2120 (setcar alist (cons (cons type value) (car alist))))
2121 (if (consp (car (car alist)))
2122 (setcar alist (nreverse (car alist))))
2123 (setq alist (cons (cons type value) alist)
2126 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
2127 (setq index (match-end 0)
2128 group (eq (aref string (match-beginning 1)) ?+))))
2131 (defun epg-decode-dn (alist)
2132 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2133 Type names are resolved using `epg-dn-type-alist'."
2136 (if (stringp (car rdn))
2137 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
2139 (format "%s=%s" (cdr entry) (cdr rdn))
2140 (format "%s=%s" (car rdn) (cdr rdn))))
2141 (concat "(" (epg-decode-dn rdn) ")")))
2147 ;;; epg.el ends here