1 ;;; epg.el --- the EasyPG Library
2 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
3 ;; 2005, 2006 Free Software Foundation, Inc.
4 ;; Copyright (C) 2006 Daiki Ueno
6 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Keywords: PGP, GnuPG
9 ;; This file is part of EasyPG.
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 (defvar epg-user-id nil
31 "GnuPG ID of your default identity.")
33 (defvar epg-user-id-alist nil
34 "An alist mapping from key ID to user ID.")
36 (defvar epg-read-point nil)
37 (defvar epg-process-filter-running nil)
38 (defvar epg-pending-status-list nil)
39 (defvar epg-key-id nil)
40 (defvar epg-context nil)
41 (defvar epg-debug-buffer nil)
43 ;; from gnupg/include/cipher.h
44 (defconst epg-cipher-algorithm-alist
56 ;; from gnupg/include/cipher.h
57 (defconst epg-pubkey-algorithm-alist
65 ;; from gnupg/include/cipher.h
66 (defconst epg-digest-algorithm-alist
74 ;; from gnupg/include/cipher.h
75 (defconst epg-compress-algorithm-alist
81 (defconst epg-invalid-recipients-reason-alist
82 '((0 . "No specific reason given")
84 (2 . "Ambigious specification")
85 (3 . "Wrong key usage")
90 (8 . "Policy mismatch")
91 (9 . "Not a secret key")
92 (10 . "Key not trusted")))
94 (defconst epg-delete-problem-reason-alist
96 (2 . "Must delete secret key first")
97 (3 . "Ambigious specification")))
99 (defconst epg-import-ok-reason-alist
100 '((0 . "Not actually changed")
101 (1 . "Entirely new key")
103 (4 . "New signatures")
105 (16 . "Contains private key")))
107 (defconst epg-import-problem-reason-alist
108 '((0 . "No specific reason given")
109 (1 . "Invalid Certificate")
110 (2 . "Issuer Certificate missing")
111 (3 . "Certificate Chain too long")
112 (4 . "Error storing certificate")))
114 (defconst epg-no-data-reason-alist
115 '((1 . "No armored data")
116 (2 . "Expected a packet but did not found one")
117 (3 . "Invalid packet found, this may indicate a non OpenPGP message")
118 (4 . "Signature expected but not found")))
120 (defconst epg-unexpected-reason-alist nil)
122 (defvar epg-key-validity-alist
135 (defvar epg-key-capablity-alist
139 (?a . authentication)))
141 (defvar epg-new-signature-type-alist
146 (defvar epg-dn-type-alist
147 '(("1.2.840.113549.1.9.1" . "EMail")
151 ("0.2.262.1.10.7.20" . "NameDistinguisher")
152 ("2.5.4.16" . "ADDR")
155 ("2.5.4.17" . "PostalCode")
156 ("2.5.4.65" . "Pseudo")
157 ("2.5.4.5" . "SerialNumber")))
159 (defvar epg-prompt-alist nil)
161 (defun epg-make-data-from-file (file)
162 "Make a data object from FILE."
163 (cons 'epg-data (vector file nil)))
165 (defun epg-make-data-from-string (string)
166 "Make a data object from STRING."
167 (cons 'epg-data (vector nil string)))
169 (defun epg-data-file (data)
170 "Return the file of DATA."
171 (unless (eq (car-safe data) 'epg-data)
172 (signal 'wrong-type-argument (list 'epg-data-p data)))
175 (defun epg-data-string (data)
176 "Return the string of DATA."
177 (unless (eq (car-safe data) 'epg-data)
178 (signal 'wrong-type-argument (list 'epg-data-p data)))
181 (defun epg-make-context (&optional protocol armor textmode include-certs
182 cipher-algorithm digest-algorithm
184 "Return a context object."
186 (vector (or protocol 'OpenPGP) armor textmode include-certs
187 cipher-algorithm digest-algorithm compress-algorithm
188 #'epg-passphrase-callback-function
190 nil nil nil nil nil nil)))
192 (defun epg-context-protocol (context)
193 "Return the protocol used within CONTEXT."
194 (unless (eq (car-safe context) 'epg-context)
195 (signal 'wrong-type-argument (list 'epg-context-p context)))
196 (aref (cdr context) 0))
198 (defun epg-context-armor (context)
199 "Return t if the output shouled be ASCII armored in CONTEXT."
200 (unless (eq (car-safe context) 'epg-context)
201 (signal 'wrong-type-argument (list 'epg-context-p context)))
202 (aref (cdr context) 1))
204 (defun epg-context-textmode (context)
205 "Return t if canonical text mode should be used in CONTEXT."
206 (unless (eq (car-safe context) 'epg-context)
207 (signal 'wrong-type-argument (list 'epg-context-p context)))
208 (aref (cdr context) 2))
210 (defun epg-context-include-certs (context)
211 "Return how many certificates should be included in an S/MIME signed
213 (unless (eq (car-safe context) 'epg-context)
214 (signal 'wrong-type-argument (list 'epg-context-p context)))
215 (aref (cdr context) 3))
217 (defun epg-context-cipher-algorithm (context)
218 "Return the cipher algorithm in CONTEXT."
219 (unless (eq (car-safe context) 'epg-context)
220 (signal 'wrong-type-argument (list 'epg-context-p context)))
221 (aref (cdr context) 4))
223 (defun epg-context-digest-algorithm (context)
224 "Return the digest algorithm in CONTEXT."
225 (unless (eq (car-safe context) 'epg-context)
226 (signal 'wrong-type-argument (list 'epg-context-p context)))
227 (aref (cdr context) 5))
229 (defun epg-context-compress-algorithm (context)
230 "Return the compress algorithm in CONTEXT."
231 (unless (eq (car-safe context) 'epg-context)
232 (signal 'wrong-type-argument (list 'epg-context-p context)))
233 (aref (cdr context) 6))
235 (defun epg-context-passphrase-callback (context)
236 "Return the function used to query passphrase."
237 (unless (eq (car-safe context) 'epg-context)
238 (signal 'wrong-type-argument (list 'epg-context-p context)))
239 (aref (cdr context) 7))
241 (defun epg-context-progress-callback (context)
242 "Return the function which handles progress update."
243 (unless (eq (car-safe context) 'epg-context)
244 (signal 'wrong-type-argument (list 'epg-context-p context)))
245 (aref (cdr context) 8))
247 (defun epg-context-signers (context)
248 "Return the list of key-id for singning."
249 (unless (eq (car-safe context) 'epg-context)
250 (signal 'wrong-type-argument (list 'epg-context-p context)))
251 (aref (cdr context) 9))
253 (defun epg-context-sig-notations (context)
254 "Return the list of notations for singning."
255 (unless (eq (car-safe context) 'epg-context)
256 (signal 'wrong-type-argument (list 'epg-context-p context)))
257 (aref (cdr context) 10))
259 (defun epg-context-process (context)
260 "Return the process object of `epg-gpg-program'.
261 This function is for internal use only."
262 (unless (eq (car-safe context) 'epg-context)
263 (signal 'wrong-type-argument (list 'epg-context-p context)))
264 (aref (cdr context) 11))
266 (defun epg-context-output-file (context)
267 "Return the output file of `epg-gpg-program'.
268 This function is for internal use only."
269 (unless (eq (car-safe context) 'epg-context)
270 (signal 'wrong-type-argument (list 'epg-context-p context)))
271 (aref (cdr context) 12))
273 (defun epg-context-result (context)
274 "Return the result of the previous cryptographic operation."
275 (unless (eq (car-safe context) 'epg-context)
276 (signal 'wrong-type-argument (list 'epg-context-p context)))
277 (aref (cdr context) 13))
279 (defun epg-context-operation (context)
280 "Return the name of the current cryptographic operation."
281 (unless (eq (car-safe context) 'epg-context)
282 (signal 'wrong-type-argument (list 'epg-context-p context)))
283 (aref (cdr context) 14))
285 (defun epg-context-set-protocol (context protocol)
286 "Set the protocol used within CONTEXT."
287 (unless (eq (car-safe context) 'epg-context)
288 (signal 'wrong-type-argument (list 'epg-context-p context)))
289 (aset (cdr context) 0 protocol))
291 (defun epg-context-set-armor (context armor)
292 "Specify if the output shouled be ASCII armored in CONTEXT."
293 (unless (eq (car-safe context) 'epg-context)
294 (signal 'wrong-type-argument (list 'epg-context-p context)))
295 (aset (cdr context) 1 armor))
297 (defun epg-context-set-textmode (context textmode)
298 "Specify if canonical text mode should be used in CONTEXT."
299 (unless (eq (car-safe context) 'epg-context)
300 (signal 'wrong-type-argument (list 'epg-context-p context)))
301 (aset (cdr context) 2 textmode))
303 (defun epg-context-set-include-certs (context include-certs)
304 "Set how many certificates should be included in an S/MIME signed message."
305 (unless (eq (car-safe context) 'epg-context)
306 (signal 'wrong-type-argument (list 'epg-context-p context)))
307 (aset (cdr context) 3 include-certs))
309 (defun epg-context-set-cipher-algorithm (context cipher-algorithm)
310 "Set the cipher algorithm in CONTEXT."
311 (unless (eq (car-safe context) 'epg-context)
312 (signal 'wrong-type-argument (list 'epg-context-p context)))
313 (aset (cdr context) 4 cipher-algorithm))
315 (defun epg-context-set-digest-algorithm (context digest-algorithm)
316 "Set the digest algorithm in CONTEXT."
317 (unless (eq (car-safe context) 'epg-context)
318 (signal 'wrong-type-argument (list 'epg-context-p context)))
319 (aset (cdr context) 5 digest-algorithm))
321 (defun epg-context-set-compress-algorithm (context compress-algorithm)
322 "Set the compress algorithm in CONTEXT."
323 (unless (eq (car-safe context) 'epg-context)
324 (signal 'wrong-type-argument (list 'epg-context-p context)))
325 (aset (cdr context) 6 compress-algorithm))
327 (defun epg-context-set-passphrase-callback (context
329 "Set the function used to query passphrase."
330 (unless (eq (car-safe context) 'epg-context)
331 (signal 'wrong-type-argument (list 'epg-context-p context)))
332 (aset (cdr context) 7 passphrase-callback))
334 (defun epg-context-set-progress-callback (context progress-callback)
335 "Set the function which handles progress update."
336 (unless (eq (car-safe context) 'epg-context)
337 (signal 'wrong-type-argument (list 'epg-context-p context)))
338 (aset (cdr context) 8 progress-callback))
340 (defun epg-context-set-signers (context signers)
341 "Set the list of key-id for singning."
342 (unless (eq (car-safe context) 'epg-context)
343 (signal 'wrong-type-argument (list 'epg-context-p context)))
344 (aset (cdr context) 9 signers))
346 (defun epg-context-set-sig-notations (context notations)
347 "Set the list of notations for singning."
348 (unless (eq (car-safe context) 'epg-context)
349 (signal 'wrong-type-argument (list 'epg-context-p context)))
350 (aset (cdr context) 10 signers))
352 (defun epg-context-set-process (context process)
353 "Set the process object of `epg-gpg-program'.
354 This function is for internal use only."
355 (unless (eq (car-safe context) 'epg-context)
356 (signal 'wrong-type-argument (list 'epg-context-p context)))
357 (aset (cdr context) 11 process))
359 (defun epg-context-set-output-file (context output-file)
360 "Set the output file of `epg-gpg-program'.
361 This function is for internal use only."
362 (unless (eq (car-safe context) 'epg-context)
363 (signal 'wrong-type-argument (list 'epg-context-p context)))
364 (aset (cdr context) 12 output-file))
366 (defun epg-context-set-result (context result)
367 "Set the result of the previous cryptographic operation."
368 (unless (eq (car-safe context) 'epg-context)
369 (signal 'wrong-type-argument (list 'epg-context-p context)))
370 (aset (cdr context) 13 result))
372 (defun epg-context-set-operation (context operation)
373 "Set the name of the current cryptographic operation."
374 (unless (eq (car-safe context) 'epg-context)
375 (signal 'wrong-type-argument (list 'epg-context-p context)))
376 (aset (cdr context) 14 operation))
378 (defun epg-make-signature (status &optional key-id)
379 "Return a signature object."
380 (cons 'epg-signature (vector status key-id nil nil nil nil nil nil nil nil
383 (defun epg-signature-status (signature)
384 "Return the status code of SIGNATURE."
385 (unless (eq (car-safe signature) 'epg-signature)
386 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
387 (aref (cdr signature) 0))
389 (defun epg-signature-key-id (signature)
390 "Return the key-id of SIGNATURE."
391 (unless (eq (car-safe signature) 'epg-signature)
392 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
393 (aref (cdr signature) 1))
395 (defun epg-signature-validity (signature)
396 "Return the validity of SIGNATURE."
397 (unless (eq (car-safe signature) 'epg-signature)
398 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
399 (aref (cdr signature) 2))
401 (defun epg-signature-fingerprint (signature)
402 "Return the fingerprint of SIGNATURE."
403 (unless (eq (car-safe signature) 'epg-signature)
404 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
405 (aref (cdr signature) 3))
407 (defun epg-signature-creation-time (signature)
408 "Return the creation time of SIGNATURE."
409 (unless (eq (car-safe signature) 'epg-signature)
410 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
411 (aref (cdr signature) 4))
413 (defun epg-signature-expiration-time (signature)
414 "Return the expiration time of SIGNATURE."
415 (unless (eq (car-safe signature) 'epg-signature)
416 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
417 (aref (cdr signature) 5))
419 (defun epg-signature-pubkey-algorithm (signature)
420 "Return the public key algorithm of SIGNATURE."
421 (unless (eq (car-safe signature) 'epg-signature)
422 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
423 (aref (cdr signature) 6))
425 (defun epg-signature-digest-algorithm (signature)
426 "Return the digest algorithm of SIGNATURE."
427 (unless (eq (car-safe signature) 'epg-signature)
428 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
429 (aref (cdr signature) 7))
431 (defun epg-signature-class (signature)
432 "Return the class of SIGNATURE."
433 (unless (eq (car-safe signature) 'epg-signature)
434 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
435 (aref (cdr signature) 8))
437 (defun epg-signature-version (signature)
438 "Return the version of SIGNATURE."
439 (unless (eq (car-safe signature) 'epg-signature)
440 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
441 (aref (cdr signature) 9))
443 (defun epg-signature-notations (signature)
444 "Return the list of notations of SIGNATURE."
445 (unless (eq (car-safe signature) 'epg-signature)
446 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
447 (aref (cdr signature) 10))
449 (defun epg-signature-set-status (signature status)
450 "Set the status code of SIGNATURE."
451 (unless (eq (car-safe signature) 'epg-signature)
452 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
453 (aset (cdr signature) 0 status))
455 (defun epg-signature-set-key-id (signature key-id)
456 "Set the key-id of SIGNATURE."
457 (unless (eq (car-safe signature) 'epg-signature)
458 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
459 (aset (cdr signature) 1 key-id))
461 (defun epg-signature-set-validity (signature validity)
462 "Set the validity of SIGNATURE."
463 (unless (eq (car-safe signature) 'epg-signature)
464 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
465 (aset (cdr signature) 2 validity))
467 (defun epg-signature-set-fingerprint (signature fingerprint)
468 "Set the fingerprint of SIGNATURE."
469 (unless (eq (car-safe signature) 'epg-signature)
470 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
471 (aset (cdr signature) 3 fingerprint))
473 (defun epg-signature-set-creation-time (signature creation-time)
474 "Set the creation time of SIGNATURE."
475 (unless (eq (car-safe signature) 'epg-signature)
476 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
477 (aset (cdr signature) 4 creation-time))
479 (defun epg-signature-set-expiration-time (signature expiration-time)
480 "Set the expiration time of SIGNATURE."
481 (unless (eq (car-safe signature) 'epg-signature)
482 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
483 (aset (cdr signature) 5 expiration-time))
485 (defun epg-signature-set-pubkey-algorithm (signature pubkey-algorithm)
486 "Set the public key algorithm of SIGNATURE."
487 (unless (eq (car-safe signature) 'epg-signature)
488 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
489 (aset (cdr signature) 6 pubkey-algorithm))
491 (defun epg-signature-set-digest-algorithm (signature digest-algorithm)
492 "Set the digest algorithm of SIGNATURE."
493 (unless (eq (car-safe signature) 'epg-signature)
494 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
495 (aset (cdr signature) 7 digest-algorithm))
497 (defun epg-signature-set-class (signature class)
498 "Set the class of SIGNATURE."
499 (unless (eq (car-safe signature) 'epg-signature)
500 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
501 (aset (cdr signature) 8 class))
503 (defun epg-signature-set-version (signature version)
504 "Set the version of SIGNATURE."
505 (unless (eq (car-safe signature) 'epg-signature)
506 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
507 (aset (cdr signature) 9 version))
509 (defun epg-signature-set-notations (signature notations)
510 "Set the list of notations of SIGNATURE."
511 (unless (eq (car-safe signature) 'epg-signature)
512 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
513 (aset (cdr signature) 10 notations))
515 (defun epg-make-new-signature (type pubkey-algorithm digest-algorithm
516 class creation-time fingerprint)
517 "Return a new signature object."
518 (cons 'epg-new-signature (vector type pubkey-algorithm digest-algorithm
519 class creation-time fingerprint)))
521 (defun epg-new-signature-type (new-signature)
522 "Return the type of NEW-SIGNATURE."
523 (unless (eq (car-safe new-signature) 'epg-new-signature)
524 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
525 (aref (cdr new-signature) 0))
527 (defun epg-new-signature-pubkey-algorithm (new-signature)
528 "Return the public key algorithm of NEW-SIGNATURE."
529 (unless (eq (car-safe new-signature) 'epg-new-signature)
530 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
531 (aref (cdr new-signature) 1))
533 (defun epg-new-signature-digest-algorithm (new-signature)
534 "Return the digest algorithm of NEW-SIGNATURE."
535 (unless (eq (car-safe new-signature) 'epg-new-signature)
536 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
537 (aref (cdr new-signature) 2))
539 (defun epg-new-signature-class (new-signature)
540 "Return the class of NEW-SIGNATURE."
541 (unless (eq (car-safe new-signature) 'epg-new-signature)
542 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
543 (aref (cdr new-signature) 3))
545 (defun epg-new-signature-creation-time (new-signature)
546 "Return the creation time of NEW-SIGNATURE."
547 (unless (eq (car-safe new-signature) 'epg-new-signature)
548 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
549 (aref (cdr new-signature) 4))
551 (defun epg-new-signature-fingerprint (new-signature)
552 "Return the fingerprint of NEW-SIGNATURE."
553 (unless (eq (car-safe new-signature) 'epg-new-signature)
554 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
555 (aref (cdr new-signature) 5))
557 (defun epg-make-key (owner-trust)
558 "Return a key object."
559 (cons 'epg-key (vector owner-trust nil nil)))
561 (defun epg-key-owner-trust (key)
562 "Return the owner trust of KEY."
563 (unless (eq (car-safe key) 'epg-key)
564 (signal 'wrong-type-argument (list 'epg-key-p key)))
567 (defun epg-key-sub-key-list (key)
568 "Return the sub key list of KEY."
569 (unless (eq (car-safe key) 'epg-key)
570 (signal 'wrong-type-argument (list 'epg-key-p key)))
573 (defun epg-key-user-id-list (key)
574 "Return the user ID list of KEY."
575 (unless (eq (car-safe key) 'epg-key)
576 (signal 'wrong-type-argument (list 'epg-key-p key)))
579 (defun epg-key-set-sub-key-list (key sub-key-list)
580 "Set the sub key list of KEY."
581 (unless (eq (car-safe key) 'epg-key)
582 (signal 'wrong-type-argument (list 'epg-key-p key)))
583 (aset (cdr key) 1 sub-key-list))
585 (defun epg-key-set-user-id-list (key user-id-list)
586 "Set the user ID list of KEY."
587 (unless (eq (car-safe key) 'epg-key)
588 (signal 'wrong-type-argument (list 'epg-key-p key)))
589 (aset (cdr key) 2 user-id-list))
591 (defun epg-make-sub-key (validity capability secret-p algorithm length id
592 creation-time expiration-time)
593 "Return a sub key object."
595 (vector validity capability secret-p algorithm length id creation-time
596 expiration-time nil)))
598 (defun epg-sub-key-validity (sub-key)
599 "Return the validity of SUB-KEY."
600 (unless (eq (car-safe sub-key) 'epg-sub-key)
601 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
602 (aref (cdr sub-key) 0))
604 (defun epg-sub-key-capability (sub-key)
605 "Return the capability of SUB-KEY."
606 (unless (eq (car-safe sub-key) 'epg-sub-key)
607 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
608 (aref (cdr sub-key) 1))
610 (defun epg-sub-key-secret-p (sub-key)
611 "Return non-nil if SUB-KEY is a secret key."
612 (unless (eq (car-safe sub-key) 'epg-sub-key)
613 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
614 (aref (cdr sub-key) 2))
616 (defun epg-sub-key-algorithm (sub-key)
617 "Return the algorithm of SUB-KEY."
618 (unless (eq (car-safe sub-key) 'epg-sub-key)
619 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
620 (aref (cdr sub-key) 3))
622 (defun epg-sub-key-length (sub-key)
623 "Return the length of SUB-KEY."
624 (unless (eq (car-safe sub-key) 'epg-sub-key)
625 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
626 (aref (cdr sub-key) 4))
628 (defun epg-sub-key-id (sub-key)
629 "Return the ID of SUB-KEY."
630 (unless (eq (car-safe sub-key) 'epg-sub-key)
631 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
632 (aref (cdr sub-key) 5))
634 (defun epg-sub-key-creation-time (sub-key)
635 "Return the creation time of SUB-KEY."
636 (unless (eq (car-safe sub-key) 'epg-sub-key)
637 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
638 (aref (cdr sub-key) 6))
640 (defun epg-sub-key-expiration-time (sub-key)
641 "Return the expiration time of SUB-KEY."
642 (unless (eq (car-safe sub-key) 'epg-sub-key)
643 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
644 (aref (cdr sub-key) 7))
646 (defun epg-sub-key-fingerprint (sub-key)
647 "Return the fingerprint of SUB-KEY."
648 (unless (eq (car-safe sub-key) 'epg-sub-key)
649 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
650 (aref (cdr sub-key) 8))
652 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
653 "Set the fingerprint of SUB-KEY.
654 This function is for internal use only."
655 (unless (eq (car-safe sub-key) 'epg-sub-key)
656 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
657 (aset (cdr sub-key) 8 fingerprint))
659 (defun epg-make-user-id (validity string)
660 "Return a user ID object."
661 (cons 'epg-user-id (vector validity string nil)))
663 (defun epg-user-id-validity (user-id)
664 "Return the validity of USER-ID."
665 (unless (eq (car-safe user-id) 'epg-user-id)
666 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
667 (aref (cdr user-id) 0))
669 (defun epg-user-id-string (user-id)
670 "Return the name of USER-ID."
671 (unless (eq (car-safe user-id) 'epg-user-id)
672 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
673 (aref (cdr user-id) 1))
675 (defun epg-user-id-signature-list (user-id)
676 "Return the signature list of USER-ID."
677 (unless (eq (car-safe user-id) 'epg-user-id)
678 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
679 (aref (cdr user-id) 2))
681 (defun epg-user-id-set-signature-list (user-id signature-list)
682 "Set the signature list of USER-ID."
683 (unless (eq (car-safe user-id) 'epg-user-id)
684 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
685 (aset (cdr user-id) 2 signature-list))
687 (defun epg-make-key-signature (validity pubkey-algorithm key-id creation-time
688 expiration-time user-id class
690 "Return a key signature object."
691 (cons 'epg-key-signature
692 (vector validity pubkey-algorithm key-id creation-time expiration-time
693 user-id class exportable-p)))
695 (defun epg-key-signature-validity (key-signature)
696 "Return the validity of KEY-SIGNATURE."
697 (unless (eq (car-safe key-signature) 'epg-key-signature)
698 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
699 (aref (cdr key-signature) 0))
701 (defun epg-key-signature-pubkey-algorithm (key-signature)
702 "Return the public key algorithm of KEY-SIGNATURE."
703 (unless (eq (car-safe key-signature) 'epg-key-signature)
704 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
705 (aref (cdr key-signature) 1))
707 (defun epg-key-signature-key-id (key-signature)
708 "Return the key-id of KEY-SIGNATURE."
709 (unless (eq (car-safe key-signature) 'epg-key-signature)
710 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
711 (aref (cdr key-signature) 2))
713 (defun epg-key-signature-creation-time (key-signature)
714 "Return the creation time of KEY-SIGNATURE."
715 (unless (eq (car-safe key-signature) 'epg-key-signature)
716 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
717 (aref (cdr key-signature) 3))
719 (defun epg-key-signature-expiration-time (key-signature)
720 "Return the expiration time of KEY-SIGNATURE."
721 (unless (eq (car-safe key-signature) 'epg-key-signature)
722 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
723 (aref (cdr key-signature) 4))
725 (defun epg-key-signature-user-id (key-signature)
726 "Return the user-id of KEY-SIGNATURE."
727 (unless (eq (car-safe key-signature) 'epg-key-signature)
728 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
729 (aref (cdr key-signature) 5))
731 (defun epg-key-signature-class (key-signature)
732 "Return the class of KEY-SIGNATURE."
733 (unless (eq (car-safe key-signature) 'epg-key-signature)
734 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
735 (aref (cdr key-signature) 6))
737 (defun epg-key-signature-exportable-p (key-signature)
738 "Return t if KEY-SIGNATURE is exportable."
739 (unless (eq (car-safe key-signature) 'epg-key-signature)
740 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
741 (aref (cdr key-signature) 7))
743 (defun epg-make-sig-notation (name value &optional human-readable
745 "Return a notation object."
746 (cons 'epg-sig-notation (vector name value human-readable critical)))
748 (defun epg-sig-notation-name (sig-notation)
749 "Return the name of SIG-NOTATION."
750 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
751 (signal 'wrong-type-argument (list 'epg-sig-notation-p
753 (aref (cdr sig-notation) 0))
755 (defun epg-sig-notation-value (sig-notation)
756 "Return the value of SIG-NOTATION."
757 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
758 (signal 'wrong-type-argument (list 'epg-sig-notation-p
760 (aref (cdr sig-notation) 1))
762 (defun epg-sig-notation-human-readable (sig-notation)
763 "Return the human-readable of SIG-NOTATION."
764 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
765 (signal 'wrong-type-argument (list 'epg-sig-notation-p
767 (aref (cdr sig-notation) 2))
769 (defun epg-sig-notation-critical (sig-notation)
770 "Return the critical of SIG-NOTATION."
771 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
772 (signal 'wrong-type-argument (list 'epg-sig-notation-p
774 (aref (cdr sig-notation) 3))
776 (defun epg-sig-notation-set-value (sig-notation value)
777 "Set the value of SIG-NOTATION."
778 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
779 (signal 'wrong-type-argument (list 'epg-sig-notation-p
781 (aset (cdr sig-notation) 1 value))
783 (defun epg-context-result-for (context name)
784 "Return the result of CONTEXT associated with NAME."
785 (cdr (assq name (epg-context-result context))))
787 (defun epg-context-set-result-for (context name value)
788 "Set the result of CONTEXT associated with NAME to VALUE."
789 (let* ((result (epg-context-result context))
790 (entry (assq name result)))
793 (epg-context-set-result context (cons (cons name value) result)))))
795 (defun epg-signature-to-string (signature)
796 "Convert SIGNATURE to a human readable string."
797 (let ((user-id (cdr (assoc (epg-signature-key-id signature)
798 epg-user-id-alist))))
800 (cond ((eq (epg-signature-status signature) 'good)
801 "Good signature from ")
802 ((eq (epg-signature-status signature) 'bad)
803 "Bad signature from ")
804 ((eq (epg-signature-status signature) 'expired)
805 "Expired signature from ")
806 ((eq (epg-signature-status signature) 'expired-key)
807 "Signature made by expired key ")
808 ((eq (epg-signature-status signature) 'revoked-key)
809 "Signature made by revoked key ")
810 ((eq (epg-signature-status signature) 'no-pubkey)
811 "No public key for "))
812 (epg-signature-key-id signature)
815 (if (stringp user-id)
817 (epg-decode-dn user-id)))
819 (if (epg-signature-validity signature)
820 (format " (trust %s)" (epg-signature-validity signature))
823 (defun epg-verify-result-to-string (verify-result)
824 "Convert VERIFY-RESULT to a human readable string."
825 (mapconcat #'epg-signature-to-string verify-result "\n"))
827 (defun epg-new-signature-to-string (new-signature)
828 "Convert NEW-SIGNATURE to a human readable string."
830 (cond ((eq (epg-new-signature-type new-signature) 'detached)
831 "Detached signature ")
832 ((eq (epg-new-signature-type new-signature) 'clear)
833 "Cleartext signature ")
836 (cdr (assq (epg-new-signature-pubkey-algorithm new-signature)
837 epg-pubkey-algorithm-alist))
839 (cdr (assq (epg-new-signature-digest-algorithm new-signature)
840 epg-digest-algorithm-alist))
842 (format "%02X " (epg-new-signature-class new-signature))
843 (epg-new-signature-fingerprint new-signature)))
845 (defun epg--start (context args)
846 "Start `epg-gpg-program' in a subprocess with given ARGS."
847 (if (and (epg-context-process context)
848 (eq (process-status (epg-context-process context)) 'run))
849 (error "%s is already running in this context"
850 (if (eq (epg-context-protocol context) 'CMS)
853 (let* ((args (append (list "--no-tty"
856 (if (epg-context-progress-callback context)
857 (list "--enable-progress-filter"))
858 (if epg-gpg-home-directory
859 (list "--homedir" epg-gpg-home-directory))
860 (unless (eq (epg-context-protocol context) 'CMS)
861 (list "--command-fd" "0"))
862 (if (epg-context-armor context) '("--armor"))
863 (if (epg-context-textmode context) '("--textmode"))
864 (if (epg-context-output-file context)
865 (list "--output" (epg-context-output-file context)))
867 (coding-system-for-write 'binary)
868 process-connection-type
869 (orig-mode (default-file-modes))
870 (buffer (generate-new-buffer " *epg*"))
874 (unless epg-debug-buffer
875 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
876 (set-buffer epg-debug-buffer)
877 (goto-char (point-max))
878 (insert (format "%s %s\n"
879 (if (eq (epg-context-protocol context) 'CMS)
882 (mapconcat #'identity args " ")))))
883 (with-current-buffer buffer
884 (make-local-variable 'epg-read-point)
885 (setq epg-read-point (point-min))
886 (make-local-variable 'epg-process-filter-running)
887 (setq epg-process-filter-running nil)
888 (make-local-variable 'epg-pending-status-list)
889 (setq epg-pending-status-list nil)
890 (make-local-variable 'epg-key-id)
891 (setq epg-key-id nil)
892 (make-local-variable 'epg-context)
893 (setq epg-context context))
896 (set-default-file-modes 448)
898 (apply #'start-process "epg" buffer
899 (if (eq (epg-context-protocol context) 'CMS)
903 (set-default-file-modes orig-mode))
904 (set-process-filter process #'epg--process-filter)
905 (epg-context-set-process context process)))
907 (defun epg--process-filter (process input)
910 (unless epg-debug-buffer
911 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
912 (set-buffer epg-debug-buffer)
913 (goto-char (point-max))
915 (if (buffer-live-p (process-buffer process))
917 (set-buffer (process-buffer process))
918 (goto-char (point-max))
920 (unless epg-process-filter-running
923 (setq epg-process-filter-running t)
924 (goto-char epg-read-point)
926 (while (looking-at ".*\n") ;the input line finished
927 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
928 (let* ((status (match-string 1))
929 (string (match-string 2))
930 (symbol (intern-soft (concat "epg--status-"
932 (if (member status epg-pending-status-list)
933 (setq epg-pending-status-list nil))
936 (funcall symbol epg-context string))))
938 (setq epg-read-point (point))))
939 (setq epg-process-filter-running nil))))))
941 (defun epg-read-output (context)
942 "Read the output file CONTEXT and return the content as a string."
944 (if (fboundp 'set-buffer-multibyte)
945 (set-buffer-multibyte nil))
946 (if (file-exists-p (epg-context-output-file context))
947 (let ((coding-system-for-read 'binary))
948 (insert-file-contents (epg-context-output-file context))
951 (defun epg-wait-for-status (context status-list)
952 "Wait until one of elements in STATUS-LIST arrives."
953 (with-current-buffer (process-buffer (epg-context-process context))
954 (setq epg-pending-status-list status-list)
955 (while (and (eq (process-status (epg-context-process context)) 'run)
956 epg-pending-status-list)
957 (accept-process-output (epg-context-process context) 1))))
959 (defun epg-wait-for-completion (context)
960 "Wait until the `epg-gpg-program' process completes."
961 (while (eq (process-status (epg-context-process context)) 'run)
962 (accept-process-output (epg-context-process context) 1)))
964 (defun epg-reset (context)
966 (if (and (epg-context-process context)
967 (buffer-live-p (process-buffer (epg-context-process context))))
968 (kill-buffer (process-buffer (epg-context-process context))))
969 (epg-context-set-process context nil))
971 (defun epg-delete-output-file (context)
972 "Delete the output file of CONTEXT."
973 (if (and (epg-context-output-file context)
974 (file-exists-p (epg-context-output-file context)))
975 (delete-file (epg-context-output-file context))))
977 (defun epg--status-USERID_HINT (context string)
978 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
979 (let* ((key-id (match-string 1 string))
980 (user-id (match-string 2 string))
981 (entry (assoc key-id epg-user-id-alist)))
983 (setcdr entry user-id)
984 (setq epg-user-id-alist (cons (cons key-id user-id)
985 epg-user-id-alist))))))
987 (defun epg--status-NEED_PASSPHRASE (context string)
988 (if (string-match "\\`\\([^ ]+\\)" string)
989 (setq epg-key-id (match-string 1 string))))
991 (defun epg--status-NEED_PASSPHRASE_SYM (context string)
992 (setq epg-key-id 'SYM))
994 (defun epg--status-NEED_PASSPHRASE_PIN (context string)
995 (setq epg-key-id 'PIN))
997 (defun epg--status-GET_HIDDEN (context string)
998 (when (and epg-key-id
999 (string-match "\\`passphrase\\." string))
1000 (unless (epg-context-passphrase-callback context)
1001 (error "passphrase-callback not set"))
1004 passphrase-with-new-line
1005 encoded-passphrase-with-new-line)
1011 (if (consp (epg-context-passphrase-callback context))
1012 (car (epg-context-passphrase-callback context))
1013 (epg-context-passphrase-callback context))
1016 (if (consp (epg-context-passphrase-callback context))
1017 (cdr (epg-context-passphrase-callback context)))))
1019 (setq passphrase-with-new-line (concat passphrase "\n"))
1020 (epg--clear-string passphrase)
1021 (setq passphrase nil)
1022 (if epg-passphrase-coding-system
1024 (setq encoded-passphrase-with-new-line
1025 (encode-coding-string
1026 passphrase-with-new-line
1027 epg-passphrase-coding-system))
1028 (epg--clear-string passphrase-with-new-line)
1029 (setq passphrase-with-new-line nil))
1030 (setq encoded-passphrase-with-new-line
1031 passphrase-with-new-line
1032 passphrase-with-new-line nil))
1033 (process-send-string (epg-context-process context)
1034 encoded-passphrase-with-new-line)))
1036 (epg-context-set-result-for
1039 (epg-context-result-for context 'error)))
1040 (delete-process (epg-context-process context))))
1042 (epg--clear-string passphrase))
1043 (if passphrase-with-new-line
1044 (epg--clear-string passphrase-with-new-line))
1045 (if encoded-passphrase-with-new-line
1046 (epg--clear-string encoded-passphrase-with-new-line))))))
1048 (defun epg--status-GET_BOOL (context string)
1049 (let ((entry (assoc string epg-prompt-alist))
1052 (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
1053 (process-send-string (epg-context-process context) "y\n")
1054 (process-send-string (epg-context-process context) "n\n"))
1056 (epg-context-set-result-for
1059 (epg-context-result-for context 'error)))
1060 (delete-process (epg-context-process context))))))
1062 (defun epg--status-GET_LINE (context string)
1063 (let ((entry (assoc string epg-prompt-alist))
1066 (process-send-string (epg-context-process context)
1067 (concat (read-string
1070 (concat string ": ")))
1073 (epg-context-set-result-for
1076 (epg-context-result-for context 'error)))
1077 (delete-process (epg-context-process context))))))
1079 (defun epg--status-*SIG (context status string)
1080 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1081 (let* ((key-id (match-string 1 string))
1082 (user-id (match-string 2 string))
1083 (entry (assoc key-id epg-user-id-alist)))
1084 (epg-context-set-result-for
1087 (cons (epg-make-signature status key-id)
1088 (epg-context-result-for context 'verify)))
1089 (if (eq (epg-context-protocol context) 'CMS)
1091 (setq user-id (epg-dn-from-string user-id))
1094 (setcdr entry user-id)
1095 (setq epg-user-id-alist
1096 (cons (cons key-id user-id) epg-user-id-alist))))
1097 (epg-context-set-result-for
1100 (cons (epg-make-signature status)
1101 (epg-context-result-for context 'verify)))))
1103 (defun epg--status-GOODSIG (context string)
1104 (epg--status-*SIG context 'good string))
1106 (defun epg--status-EXPSIG (context string)
1107 (epg--status-*SIG context 'expired string))
1109 (defun epg--status-EXPKEYSIG (context string)
1110 (epg--status-*SIG context 'expired-key string))
1112 (defun epg--status-REVKEYSIG (context string)
1113 (epg--status-*SIG context 'revoked-key string))
1115 (defun epg--status-BADSIG (context string)
1116 (epg--status-*SIG context 'bad string))
1118 (defun epg--status-NO_PUBKEY (context string)
1119 (let ((signature (car (epg-context-result-for context 'verify))))
1121 (eq (epg-signature-status signature) 'error)
1122 (equal (epg-signature-key-id signature) string))
1123 (epg-signature-set-status signature 'no-pubkey))))
1125 (defun epg--time-from-seconds (seconds)
1126 (let ((number-seconds (string-to-number (concat seconds ".0"))))
1127 (cons (floor (/ number-seconds 65536))
1128 (floor (mod number-seconds 65536)))))
1130 (defun epg--status-ERRSIG (context string)
1131 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1132 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
1134 (let ((signature (epg-make-signature 'error)))
1135 (epg-context-set-result-for
1139 (epg-context-result-for context 'verify)))
1140 (epg-signature-set-key-id
1142 (match-string 1 string))
1143 (epg-signature-set-pubkey-algorithm
1145 (string-to-number (match-string 2 string)))
1146 (epg-signature-set-digest-algorithm
1148 (string-to-number (match-string 3 string)))
1149 (epg-signature-set-class
1151 (string-to-number (match-string 4 string) 16))
1152 (epg-signature-set-creation-time
1154 (epg--time-from-seconds (match-string 5 string))))))
1156 (defun epg--status-VALIDSIG (context string)
1157 (let ((signature (car (epg-context-result-for context 'verify))))
1158 (when (and signature
1159 (eq (epg-signature-status signature) 'good)
1160 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
1161 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
1164 (epg-signature-set-fingerprint
1166 (match-string 1 string))
1167 (epg-signature-set-creation-time
1169 (epg--time-from-seconds (match-string 2 string)))
1170 (epg-signature-set-expiration-time
1172 (epg--time-from-seconds (match-string 3 string)))
1173 (epg-signature-set-version
1175 (string-to-number (match-string 4 string)))
1176 (epg-signature-set-pubkey-algorithm
1178 (string-to-number (match-string 5 string)))
1179 (epg-signature-set-digest-algorithm
1181 (string-to-number (match-string 6 string)))
1182 (epg-signature-set-class
1184 (string-to-number (match-string 7 string) 16)))))
1186 (defun epg--status-TRUST_UNDEFINED (context string)
1187 (let ((signature (car (epg-context-result-for context 'verify))))
1189 (eq (epg-signature-status signature) 'good))
1190 (epg-signature-set-validity signature 'undefined))))
1192 (defun epg--status-TRUST_NEVER (context string)
1193 (let ((signature (car (epg-context-result-for context 'verify))))
1195 (eq (epg-signature-status signature) 'good))
1196 (epg-signature-set-validity signature 'never))))
1198 (defun epg--status-TRUST_MARGINAL (context string)
1199 (let ((signature (car (epg-context-result-for context 'verify))))
1201 (eq (epg-signature-status signature) 'marginal))
1202 (epg-signature-set-validity signature 'marginal))))
1204 (defun epg--status-TRUST_FULLY (context string)
1205 (let ((signature (car (epg-context-result-for context 'verify))))
1207 (eq (epg-signature-status signature) 'good))
1208 (epg-signature-set-validity signature 'full))))
1210 (defun epg--status-TRUST_ULTIMATE (context string)
1211 (let ((signature (car (epg-context-result-for context 'verify))))
1213 (eq (epg-signature-status signature) 'good))
1214 (epg-signature-set-validity signature 'ultimate))))
1216 (defun epg--status-NOTATION_NAME (context string)
1217 (let ((signature (car (epg-context-result-for context 'verify))))
1219 (epg-signature-set-notations
1221 (cons (epg-make-sig-notation string nil t nil)
1222 (epg-sig-notations signature))))))
1224 (defun epg--status-NOTATION_DATA (context string)
1225 (let ((signature (car (epg-context-result-for context 'verify)))
1228 (setq notation (car (epg-sig-notations signature))))
1229 (epg-sig-notation-set-value notation string))))
1231 (defun epg--status-POLICY_URL (context string)
1232 (let ((signature (car (epg-context-result-for context 'verify))))
1234 (epg-signature-set-notations
1236 (cons (epg-make-sig-notation nil string t nil)
1237 (epg-sig-notations signature))))))
1239 (defun epg--status-PROGRESS (context string)
1240 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1242 (funcall (if (consp (epg-context-progress-callback context))
1243 (car (epg-context-progress-callback context))
1244 (epg-context-progress-callback context))
1246 (match-string 1 string)
1247 (match-string 2 string)
1248 (string-to-number (match-string 3 string))
1249 (string-to-number (match-string 4 string))
1250 (if (consp (epg-context-progress-callback context))
1251 (cdr (epg-context-progress-callback context))))))
1253 (defun epg--status-DECRYPTION_FAILED (context string)
1254 (epg-context-set-result-for
1256 (cons '(decryption-failed)
1257 (epg-context-result-for context 'error))))
1259 (defun epg--status-NODATA (context string)
1260 (epg-context-set-result-for
1262 (cons (list 'no-data (cons 'reason (string-to-number string)))
1263 (epg-context-result-for context 'error))))
1265 (defun epg--status-UNEXPECTED (context string)
1266 (epg-context-set-result-for
1268 (cons (list 'unexpected (cons 'reason (string-to-number string)))
1269 (epg-context-result-for context 'error))))
1271 (defun epg--status-KEYEXPIRED (context string)
1272 (epg-context-set-result-for
1274 (cons (list 'key-expired (cons 'expiration-time
1275 (epg--time-from-seconds string)))
1276 (epg-context-result-for context 'error))))
1278 (defun epg--status-KEYREVOKED (context string)
1279 (epg-context-set-result-for
1281 (cons '(key-revoked)
1282 (epg-context-result-for context 'error))))
1284 (defun epg--status-BADARMOR (context string)
1285 (epg-context-set-result-for
1288 (epg-context-result-for context 'error))))
1290 (defun epg--status-INV_RECP (context string)
1291 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1292 (epg-context-set-result-for
1294 (cons (list 'invalid-recipient
1296 (string-to-number (match-string 1 string)))
1297 (cons 'requested-recipient
1298 (match-string 2 string)))
1299 (epg-context-result-for context 'error)))))
1301 (defun epg--status-NO_RECP (context string)
1302 (epg-context-set-result-for
1304 (cons '(no-recipients)
1305 (epg-context-result-for context 'error))))
1307 (defun epg--status-DELETE_PROBLEM (context string)
1308 (if (string-match "\\`\\([0-9]+\\)" string)
1309 (epg-context-set-result-for
1311 (cons (list 'delete-problem
1312 (cons 'reason (string-to-number (match-string 1 string))))
1313 (epg-context-result-for context 'error)))))
1315 (defun epg--status-SIG_CREATED (context string)
1316 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1317 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1318 (epg-context-set-result-for
1320 (cons (epg-make-new-signature
1321 (cdr (assq (aref (match-string 1 string) 0)
1322 epg-new-signature-type-alist))
1323 (string-to-number (match-string 2 string))
1324 (string-to-number (match-string 3 string))
1325 (string-to-number (match-string 4 string) 16)
1326 (epg--time-from-seconds (match-string 5 string))
1327 (substring string (match-end 0)))
1328 (epg-context-result-for context 'sign)))))
1330 (defun epg--status-KEY_CREATED (context string)
1331 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1332 (epg-context-set-result-for
1333 context 'generate-key
1334 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1335 (cons 'fingerprint (match-string 2 string)))
1336 (epg-context-result-for context 'generate-key)))))
1338 (defun epg--status-KEY_NOT_CREATED (context string)
1339 (epg-context-set-result-for
1341 (cons '(key-not-created)
1342 (epg-context-result-for context 'error))))
1344 (defun epg--status-IMPORTED (context string)
1345 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1346 (let* ((key-id (match-string 1 string))
1347 (user-id (match-string 2 string))
1348 (entry (assoc key-id epg-user-id-alist)))
1350 (setcdr entry user-id)
1351 (setq epg-user-id-alist (cons (cons key-id user-id)
1352 epg-user-id-alist)))
1353 (epg-context-set-result-for
1355 (cons (list (cons 'key-id key-id)
1356 (cons 'user-id user-id))
1357 (epg-context-result-for context 'import))))))
1359 (defun epg--status-IMPORT_OK (context string)
1360 (let ((result (epg-context-result-for context 'import)))
1362 (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string))
1364 (append (list (cons 'reason
1366 (match-string 1 string))))
1367 (if (match-beginning 2)
1368 (list (cons 'fingerprint
1369 (match-string 3 string))))
1372 (defun epg--status-IMPORT_PROBLEM (context string)
1373 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1374 (epg-context-set-result-for
1376 (cons (cons 'import-problem
1377 (append (list (cons 'reason
1379 (match-string 1 string))))
1380 (if (match-beginning 2)
1381 (list (cons 'fingerprint
1382 (match-string 3 string))))))
1383 (epg-context-result-for context 'error)))))
1385 (defun epg-passphrase-callback-function (context key-id handback)
1386 (if (eq key-id 'SYM)
1387 (read-passwd "Passphrase for symmetric encryption: "
1388 (eq (epg-context-operation context) 'encrypt))
1390 (if (eq key-id 'PIN)
1391 "Passphrase for PIN: "
1392 (let ((entry (assoc key-id epg-user-id-alist)))
1394 (format "Passphrase for %s %s: " key-id (cdr entry))
1395 (format "Passphrase for %s: " key-id)))))))
1397 (make-obsolete 'epg-passphrase-callback-function
1398 'epa-passphrase-callback-function)
1400 (defun epg--list-keys-1 (context name mode)
1401 (let ((args (append (if epg-gpg-home-directory
1402 (list "--homedir" epg-gpg-home-directory))
1403 (list "--with-colons" "--no-greeting" "--batch"
1404 "--with-fingerprint"
1405 "--with-fingerprint"
1406 (if (memq mode '(t secret))
1407 "--list-secret-keys"
1408 (if (memq mode '(nil public))
1411 (unless (eq (epg-context-protocol context) 'CMS)
1412 '("--fixed-list-mode"))
1413 (if name (list name))))
1414 keys string field index)
1416 (apply #'call-process
1417 (if (eq (epg-context-protocol context) 'CMS)
1420 nil (list t nil) nil args)
1421 (goto-char (point-min))
1422 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1423 (setq keys (cons (make-vector 15 nil) keys)
1424 string (match-string 0)
1428 (string-match "\\([^:]+\\)?:" string index))
1429 (setq index (match-end 0))
1430 (aset (car keys) field (match-string 1 string))
1431 (setq field (1+ field))))
1434 (defun epg--make-sub-key-1 (line)
1437 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1439 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1441 (member (aref line 0) '("sec" "ssb"))
1442 (string-to-number (aref line 3))
1443 (string-to-number (aref line 2))
1445 (epg--time-from-seconds (aref line 5))
1446 (epg--time-from-seconds (aref line 6))))
1449 (defun epg-list-keys (context &optional name mode)
1450 "Return a list of epg-key objects matched with NAME.
1451 If MODE is nil or 'public, only public keyring should be searched.
1452 If MODE is t or 'secret, only secret keyring should be searched.
1453 Otherwise, only public keyring should be searched and the key
1454 signatures should be included."
1455 (let ((lines (epg--list-keys-1 context name mode))
1456 keys cert pointer pointer-1)
1459 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1460 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1461 keys (cons (epg-make-key
1462 (if (aref (car lines) 8)
1463 (cdr (assq (string-to-char (aref (car lines) 8))
1464 epg-key-validity-alist))))
1466 (epg-key-set-sub-key-list
1468 (cons (epg--make-sub-key-1 (car lines))
1469 (epg-key-sub-key-list (car keys)))))
1470 ((member (aref (car lines) 0) '("sub" "ssb"))
1471 (epg-key-set-sub-key-list
1473 (cons (epg--make-sub-key-1 (car lines))
1474 (epg-key-sub-key-list (car keys)))))
1475 ((equal (aref (car lines) 0) "uid")
1476 (epg-key-set-user-id-list
1478 (cons (epg-make-user-id
1479 (if (aref (car lines) 1)
1480 (cdr (assq (string-to-char (aref (car lines) 1))
1481 epg-key-validity-alist)))
1484 (epg-dn-from-string (aref (car lines) 9))
1485 (error (aref (car lines) 9)))
1486 (aref (car lines) 9)))
1487 (epg-key-user-id-list (car keys)))))
1488 ((equal (aref (car lines) 0) "fpr")
1489 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1490 (aref (car lines) 9)))
1491 ((equal (aref (car lines) 0) "sig")
1492 (epg-user-id-set-signature-list
1493 (car (epg-key-user-id-list (car keys)))
1495 (epg-make-key-signature
1496 (if (aref (car lines) 1)
1497 (cdr (assq (string-to-char (aref (car lines) 1))
1498 epg-key-validity-alist)))
1499 (string-to-number (aref (car lines) 3))
1500 (aref (car lines) 4)
1501 (epg--time-from-seconds (aref (car lines) 5))
1502 (epg--time-from-seconds (aref (car lines) 6))
1503 (aref (car lines) 9)
1504 (string-to-number (aref (car lines) 10) 16)
1505 (eq (aref (aref (car lines) 10) 2) ?x))
1506 (epg-user-id-signature-list
1507 (car (epg-key-user-id-list (car keys))))))))
1508 (setq lines (cdr lines)))
1509 (setq keys (nreverse keys)
1512 (epg-key-set-sub-key-list
1514 (nreverse (epg-key-sub-key-list (car pointer))))
1515 (setq pointer-1 (epg-key-set-user-id-list
1517 (nreverse (epg-key-user-id-list (car pointer)))))
1519 (epg-user-id-set-signature-list
1521 (nreverse (epg-user-id-signature-list (car pointer-1))))
1522 (setq pointer-1 (cdr pointer-1)))
1523 (setq pointer (cdr pointer)))
1526 (if (fboundp 'make-temp-file)
1527 (defalias 'epg--make-temp-file 'make-temp-file)
1528 (defvar temporary-file-directory)
1529 ;; stolen from poe.el.
1530 (defun epg--make-temp-file (prefix)
1531 "Create a temporary file.
1532 The returned file name (created by appending some random characters at the end
1533 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1534 is guaranteed to point to a newly created empty file.
1535 You can then use `write-region' to write new data into the file."
1536 (let (tempdir tempfile)
1537 (setq prefix (expand-file-name prefix
1538 (if (featurep 'xemacs)
1540 temporary-file-directory)))
1543 ;; First, create a temporary directory.
1544 (while (condition-case ()
1546 (setq tempdir (make-temp-name
1548 (file-name-directory prefix)
1550 ;; return nil or signal an error.
1551 (make-directory tempdir))
1553 (file-already-exists t)))
1554 (set-file-modes tempdir 448)
1555 ;; Second, create a temporary file in the tempdir.
1556 ;; There *is* a race condition between `make-temp-name'
1557 ;; and `write-region', but we don't care it since we are
1558 ;; in a private directory now.
1559 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1560 (write-region "" nil tempfile nil 'silent)
1561 (set-file-modes tempfile 384)
1562 ;; Finally, make a hard-link from the tempfile.
1563 (while (condition-case ()
1565 (setq file (make-temp-name prefix))
1566 ;; return nil or signal an error.
1567 (add-name-to-file tempfile file))
1569 (file-already-exists t)))
1571 ;; Cleanup the tempfile.
1573 (file-exists-p tempfile)
1574 (delete-file tempfile))
1575 ;; Cleanup the tempdir.
1577 (file-directory-p tempdir)
1578 (delete-directory tempdir))))))
1580 (if (fboundp 'clear-string)
1581 (defalias 'epg--clear-string 'clear-string)
1582 (defun epg--clear-string (string)
1583 (fillarray string 0)))
1585 (defun epg--args-from-sig-notations (notations)
1589 (if (and (epg-sig-notation-name notation)
1590 (not (epg-sig-notation-human-readable notation)))
1591 (error "Unreadable"))
1592 (if (epg-sig-notation-name notation)
1593 (list "--sig-notation"
1594 (if (epg-sig-notation-critical notation)
1595 (concat "!" (epg-sig-notation-name notation)
1596 "=" (epg-sig-notation-value notation))
1597 (concat (epg-sig-notation-name notation)
1598 "=" (epg-sig-notation-value notation))))
1599 (list "--sig-policy-url"
1600 (if (epg-sig-notation-critical notation)
1601 (concat "!" (epg-sig-notation-value notation))
1602 (epg-sig-notation-value notation))))
1606 (defun epg-cancel (context)
1607 (if (buffer-live-p (process-buffer (epg-context-process context)))
1609 (set-buffer (process-buffer (epg-context-process context)))
1610 (epg-context-set-result-for
1613 (epg-context-result-for epg-context 'error)))))
1614 (if (eq (process-status (epg-context-process context)) 'run)
1615 (delete-process (epg-context-process context))))
1618 (defun epg-start-decrypt (context cipher)
1619 "Initiate a decrypt operation on CIPHER.
1620 CIPHER must be a file data object.
1622 If you use this function, you will need to wait for the completion of
1623 `epg-gpg-program' by using `epg-wait-for-completion' and call
1624 `epg-reset' to clear a temporaly output file.
1625 If you are unsure, use synchronous version of this function
1626 `epg-decrypt-file' or `epg-decrypt-string' instead."
1627 (unless (epg-data-file cipher)
1628 (error "Not a file"))
1629 (epg-context-set-operation context 'decrypt)
1630 (epg-context-set-result context nil)
1631 (epg--start context (list "--decrypt" "--" (epg-data-file cipher)))
1632 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1633 (unless (eq (epg-context-protocol context) 'CMS)
1634 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1637 (defun epg-decrypt-file (context cipher plain)
1638 "Decrypt a file CIPHER and store the result to a file PLAIN.
1639 If PLAIN is nil, it returns the result as a string."
1643 (epg-context-set-output-file context plain)
1644 (epg-context-set-output-file context
1645 (epg--make-temp-file "epg-output")))
1646 (epg-start-decrypt context (epg-make-data-from-file cipher))
1647 (epg-wait-for-completion context)
1648 (if (epg-context-result-for context 'error)
1649 (error "Decrypt failed: %S"
1650 (epg-context-result-for context 'error)))
1652 (epg-read-output context)))
1654 (epg-delete-output-file context))
1655 (epg-reset context)))
1658 (defun epg-decrypt-string (context cipher)
1659 "Decrypt a string CIPHER and return the plain text."
1660 (let ((input-file (epg--make-temp-file "epg-input"))
1661 (coding-system-for-write 'binary))
1664 (write-region cipher nil input-file nil 'quiet)
1665 (epg-context-set-output-file context
1666 (epg--make-temp-file "epg-output"))
1667 (epg-start-decrypt context (epg-make-data-from-file input-file))
1668 (epg-wait-for-completion context)
1669 (if (epg-context-result-for context 'error)
1670 (error "Decrypt failed: %S"
1671 (epg-context-result-for context 'error)))
1672 (epg-read-output context))
1673 (epg-delete-output-file context)
1674 (if (file-exists-p input-file)
1675 (delete-file input-file))
1676 (epg-reset context))))
1679 (defun epg-start-verify (context signature &optional signed-text)
1680 "Initiate a verify operation on SIGNATURE.
1681 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1683 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1684 For a normal or a cleartext signature, SIGNED-TEXT should be nil.
1686 If you use this function, you will need to wait for the completion of
1687 `epg-gpg-program' by using `epg-wait-for-completion' and call
1688 `epg-reset' to clear a temporaly output file.
1689 If you are unsure, use synchronous version of this function
1690 `epg-verify-file' or `epg-verify-string' instead."
1691 (epg-context-set-operation context 'verify)
1692 (epg-context-set-result context nil)
1694 ;; Detached signature.
1695 (if (epg-data-file signed-text)
1696 (epg--start context (list "--verify" "--" (epg-data-file signature)
1697 (epg-data-file signed-text)))
1698 (epg--start context (list "--verify" "--" (epg-data-file signature)
1700 (if (eq (process-status (epg-context-process context)) 'run)
1701 (process-send-string (epg-context-process context)
1702 (epg-data-string signed-text)))
1703 (if (eq (process-status (epg-context-process context)) 'run)
1704 (process-send-eof (epg-context-process context))))
1705 ;; Normal (or cleartext) signature.
1706 (if (epg-data-file signature)
1707 (epg--start context (list "--" (epg-data-file signature)))
1708 (epg--start context '("-"))
1709 (if (eq (process-status (epg-context-process context)) 'run)
1710 (process-send-string (epg-context-process context)
1711 (epg-data-string signature)))
1712 (if (eq (process-status (epg-context-process context)) 'run)
1713 (process-send-eof (epg-context-process context))))))
1716 (defun epg-verify-file (context signature &optional signed-text plain)
1717 "Verify a file SIGNATURE.
1718 SIGNED-TEXT and PLAIN are also a file if they are specified.
1720 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1721 string. For a normal or a cleartext signature, SIGNED-TEXT should be
1722 nil. In the latter case, if PLAIN is specified, the plaintext is
1723 stored into the file after successful verification."
1727 (epg-context-set-output-file context plain)
1728 (epg-context-set-output-file context
1729 (epg--make-temp-file "epg-output")))
1731 (epg-start-verify context
1732 (epg-make-data-from-file signature)
1733 (epg-make-data-from-file signed-text))
1734 (epg-start-verify context
1735 (epg-make-data-from-file signature)))
1736 (epg-wait-for-completion context)
1738 (epg-read-output context)))
1740 (epg-delete-output-file context))
1741 (epg-reset context)))
1744 (defun epg-verify-string (context signature &optional signed-text)
1745 "Verify a string SIGNATURE.
1746 SIGNED-TEXT is a string if it is specified.
1748 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1749 string. For a normal or a cleartext signature, SIGNED-TEXT should be
1750 nil. In the latter case, this function returns the plaintext after
1751 successful verification."
1752 (let ((coding-system-for-write 'binary)
1756 (epg-context-set-output-file context
1757 (epg--make-temp-file "epg-output"))
1760 (setq input-file (epg--make-temp-file "epg-signature"))
1761 (write-region signature nil input-file nil 'quiet)
1762 (epg-start-verify context
1763 (epg-make-data-from-file input-file)
1764 (epg-make-data-from-string signed-text)))
1765 (epg-start-verify context (epg-make-data-from-string signature)))
1766 (epg-wait-for-completion context)
1767 (epg-read-output context))
1768 (epg-delete-output-file context)
1770 (file-exists-p input-file))
1771 (delete-file input-file))
1772 (epg-reset context))))
1775 (defun epg-start-sign (context plain &optional mode)
1776 "Initiate a sign operation on PLAIN.
1777 PLAIN is a data object.
1779 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1780 If it is nil or 'normal, it makes a normal signature.
1781 Otherwise, it makes a cleartext signature.
1783 If you use this function, you will need to wait for the completion of
1784 `epg-gpg-program' by using `epg-wait-for-completion' and call
1785 `epg-reset' to clear a temporaly output file.
1786 If you are unsure, use synchronous version of this function
1787 `epg-sign-file' or `epg-sign-string' instead."
1788 (epg-context-set-operation context 'sign)
1789 (epg-context-set-result context nil)
1790 (unless (memq mode '(t detached nil normal)) ;i.e. cleartext
1791 (epg-context-set-armor context nil)
1792 (epg-context-set-textmode context nil))
1794 (append (list (if (memq mode '(t detached))
1796 (if (memq mode '(nil normal))
1804 (car (epg-key-sub-key-list signer)))))
1805 (epg-context-signers context)))
1806 (epg--args-from-sig-notations
1807 (epg-context-sig-notations context))
1808 (if (epg-data-file plain)
1809 (list "--" (epg-data-file plain)))))
1810 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1811 (unless (eq (epg-context-protocol context) 'CMS)
1812 (epg-wait-for-status context '("BEGIN_SIGNING")))
1813 (when (epg-data-string plain)
1814 (if (eq (process-status (epg-context-process context)) 'run)
1815 (process-send-string (epg-context-process context)
1816 (epg-data-string plain)))
1817 (if (eq (process-status (epg-context-process context)) 'run)
1818 (process-send-eof (epg-context-process context)))))
1821 (defun epg-sign-file (context plain signature &optional mode)
1822 "Sign a file PLAIN and store the result to a file SIGNATURE.
1823 If SIGNATURE is nil, it returns the result as a string.
1824 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1825 If it is nil or 'normal, it makes a normal signature.
1826 Otherwise, it makes a cleartext signature."
1830 (epg-context-set-output-file context signature)
1831 (epg-context-set-output-file context
1832 (epg--make-temp-file "epg-output")))
1833 (epg-start-sign context (epg-make-data-from-file plain) mode)
1834 (epg-wait-for-completion context)
1835 (unless (epg-context-result-for context 'sign)
1836 (if (epg-context-result-for context 'error)
1837 (error "Sign failed: %S"
1838 (epg-context-result-for context 'error))
1839 (error "Sign failed")))
1841 (epg-read-output context)))
1843 (epg-delete-output-file context))
1844 (epg-reset context)))
1847 (defun epg-sign-string (context plain &optional mode)
1848 "Sign a string PLAIN and return the output as string.
1849 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1850 If it is nil or 'normal, it makes a normal signature.
1851 Otherwise, it makes a cleartext signature."
1854 (epg-context-set-output-file context
1855 (epg--make-temp-file "epg-output"))
1856 (epg-start-sign context (epg-make-data-from-string plain) mode)
1857 (epg-wait-for-completion context)
1858 (unless (epg-context-result-for context 'sign)
1859 (if (epg-context-result-for context 'error)
1860 (error "Sign failed: %S"
1861 (epg-context-result-for context 'error))
1862 (error "Sign failed")))
1863 (epg-read-output context))
1864 (epg-delete-output-file context)
1865 (epg-reset context)))
1868 (defun epg-start-encrypt (context plain recipients
1869 &optional sign always-trust)
1870 "Initiate an encrypt operation on PLAIN.
1871 PLAIN is a data object.
1872 If RECIPIENTS is nil, it performs symmetric encryption.
1874 If you use this function, you will need to wait for the completion of
1875 `epg-gpg-program' by using `epg-wait-for-completion' and call
1876 `epg-reset' to clear a temporaly output file.
1877 If you are unsure, use synchronous version of this function
1878 `epg-encrypt-file' or `epg-encrypt-string' instead."
1879 (epg-context-set-operation context 'encrypt)
1880 (epg-context-set-result context nil)
1882 (append (if always-trust '("--always-trust"))
1883 (if recipients '("--encrypt") '("--symmetric"))
1884 (if sign '("--sign"))
1891 (car (epg-key-sub-key-list
1893 (epg-context-signers context))))
1895 (epg--args-from-sig-notations
1896 (epg-context-sig-notations context)))
1902 (car (epg-key-sub-key-list recipient)))))
1904 (if (epg-data-file plain)
1905 (list "--" (epg-data-file plain)))))
1906 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1907 (unless (eq (epg-context-protocol context) 'CMS)
1909 (epg-wait-for-status context '("BEGIN_SIGNING"))
1910 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
1911 (when (epg-data-string plain)
1912 (if (eq (process-status (epg-context-process context)) 'run)
1913 (process-send-string (epg-context-process context)
1914 (epg-data-string plain)))
1915 (if (eq (process-status (epg-context-process context)) 'run)
1916 (process-send-eof (epg-context-process context)))))
1919 (defun epg-encrypt-file (context plain recipients
1920 cipher &optional sign always-trust)
1921 "Encrypt a file PLAIN and store the result to a file CIPHER.
1922 If CIPHER is nil, it returns the result as a string.
1923 If RECIPIENTS is nil, it performs symmetric encryption."
1927 (epg-context-set-output-file context cipher)
1928 (epg-context-set-output-file context
1929 (epg--make-temp-file "epg-output")))
1930 (epg-start-encrypt context (epg-make-data-from-file plain)
1931 recipients sign always-trust)
1932 (epg-wait-for-completion context)
1934 (not (epg-context-result-for context 'sign)))
1935 (if (epg-context-result-for context 'error)
1936 (error "Sign failed: %S"
1937 (epg-context-result-for context 'error))
1938 (error "Sign failed")))
1939 (if (epg-context-result-for context 'error)
1940 (error "Encrypt failed: %S"
1941 (epg-context-result-for context 'error)))
1943 (epg-read-output context)))
1945 (epg-delete-output-file context))
1946 (epg-reset context)))
1949 (defun epg-encrypt-string (context plain recipients
1950 &optional sign always-trust)
1951 "Encrypt a string PLAIN.
1952 If RECIPIENTS is nil, it performs symmetric encryption."
1955 (epg-context-set-output-file context
1956 (epg--make-temp-file "epg-output"))
1957 (epg-start-encrypt context (epg-make-data-from-string plain)
1958 recipients sign always-trust)
1959 (epg-wait-for-completion context)
1961 (not (epg-context-result-for context 'sign)))
1962 (if (epg-context-result-for context 'error)
1963 (error "Sign failed: %S"
1964 (epg-context-result-for context 'error))
1965 (error "Sign failed")))
1966 (if (epg-context-result-for context 'error)
1967 (error "Encrypt failed: %S"
1968 (epg-context-result-for context 'error)))
1969 (epg-read-output context))
1970 (epg-delete-output-file context)
1971 (epg-reset context)))
1974 (defun epg-start-export-keys (context keys)
1975 "Initiate an export keys operation.
1977 If you use this function, you will need to wait for the completion of
1978 `epg-gpg-program' by using `epg-wait-for-completion' and call
1979 `epg-reset' to clear a temporaly output file.
1980 If you are unsure, use synchronous version of this function
1981 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1982 (epg-context-set-operation context 'export-keys)
1983 (epg-context-set-result context nil)
1984 (epg--start context (cons "--export"
1988 (car (epg-key-sub-key-list key))))
1992 (defun epg-export-keys-to-file (context keys file)
1993 "Extract public KEYS."
1997 (epg-context-set-output-file context file)
1998 (epg-context-set-output-file context
1999 (epg--make-temp-file "epg-output")))
2000 (epg-start-export-keys context keys)
2001 (epg-wait-for-completion context)
2002 (if (epg-context-result-for context 'error)
2003 (error "Export keys failed: %S"
2004 (epg-context-result-for context 'error)))
2006 (epg-read-output context)))
2008 (epg-delete-output-file context))
2009 (epg-reset context)))
2012 (defun epg-export-keys-to-string (context keys)
2013 "Extract public KEYS and return them as a string."
2014 (epg-export-keys-to-file context keys nil))
2017 (defun epg-start-import-keys (context keys)
2018 "Initiate an import keys operation.
2019 KEYS is a data object.
2021 If you use this function, you will need to wait for the completion of
2022 `epg-gpg-program' by using `epg-wait-for-completion' and call
2023 `epg-reset' to clear a temporaly output file.
2024 If you are unsure, use synchronous version of this function
2025 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
2026 (epg-context-set-operation context 'import-keys)
2027 (epg-context-set-result context nil)
2028 (epg--start context (if (epg-data-file keys)
2029 (list "--import" "--" (epg-data-file keys))
2031 (when (epg-data-string keys)
2032 (if (eq (process-status (epg-context-process context)) 'run)
2033 (process-send-string (epg-context-process context)
2034 (epg-data-string keys)))
2035 (if (eq (process-status (epg-context-process context)) 'run)
2036 (process-send-eof (epg-context-process context)))))
2038 (defun epg--import-keys-1 (context keys)
2041 (epg-start-import-keys context keys)
2042 (epg-wait-for-completion context)
2043 (if (epg-context-result-for context 'error)
2044 (error "Import keys failed: %S"
2045 (epg-context-result-for context 'error))))
2046 (epg-reset context)))
2049 (defun epg-import-keys-from-file (context keys)
2050 "Add keys from a file KEYS."
2051 (epg--import-keys-1 context (epg-make-data-from-file keys)))
2054 (defun epg-import-keys-from-string (context keys)
2055 "Add keys from a string KEYS."
2056 (epg--import-keys-1 context (epg-make-data-from-string keys)))
2059 (defun epg-start-receive-keys (context key-id-list)
2060 "Initiate a receive key operation.
2061 KEY-ID-LIST is a list of key IDs.
2063 If you use this function, you will need to wait for the completion of
2064 `epg-gpg-program' by using `epg-wait-for-completion' and call
2065 `epg-reset' to clear a temporaly output file.
2066 If you are unsure, use synchronous version of this function
2067 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2068 (epg-context-set-operation context 'receive-keys)
2069 (epg-context-set-result context nil)
2070 (epg--start context (cons "--recv-keys" key-id-list)))
2073 (defun epg-receive-keys (context keys)
2074 "Add keys from server.
2075 KEYS is a list of key IDs"
2078 (epg-start-receive-keys context keys)
2079 (epg-wait-for-completion context)
2080 (if (epg-context-result-for context 'error)
2081 (error "Receive keys failed: %S"
2082 (epg-context-result-for context 'error))))
2083 (epg-reset context)))
2086 (defalias 'epg-import-keys-from-server 'epg-receive-keys)
2089 (defun epg-start-delete-keys (context keys &optional allow-secret)
2090 "Initiate an delete keys operation.
2092 If you use this function, you will need to wait for the completion of
2093 `epg-gpg-program' by using `epg-wait-for-completion' and call
2094 `epg-reset' to clear a temporaly output file.
2095 If you are unsure, use synchronous version of this function
2096 `epg-delete-keys' instead."
2097 (epg-context-set-operation context 'delete-keys)
2098 (epg-context-set-result context nil)
2099 (epg--start context (cons (if allow-secret
2100 "--delete-secret-key"
2105 (car (epg-key-sub-key-list key))))
2109 (defun epg-delete-keys (context keys &optional allow-secret)
2110 "Delete KEYS from the key ring."
2113 (epg-start-delete-keys context keys allow-secret)
2114 (epg-wait-for-completion context)
2115 (if (epg-context-result-for context 'error)
2116 (error "Delete keys failed: %S"
2117 (epg-context-result-for context 'error))))
2118 (epg-reset context)))
2121 (defun epg-start-sign-keys (context keys &optional local)
2122 "Initiate an sign keys operation.
2124 If you use this function, you will need to wait for the completion of
2125 `epg-gpg-program' by using `epg-wait-for-completion' and call
2126 `epg-reset' to clear a temporaly output file.
2127 If you are unsure, use synchronous version of this function
2128 `epg-sign-keys' instead."
2129 (epg-context-set-operation context 'sign-keys)
2130 (epg-context-set-result context nil)
2131 (epg--start context (cons (if local
2137 (car (epg-key-sub-key-list key))))
2141 (defun epg-sign-keys (context keys &optional local)
2142 "Sign KEYS from the key ring."
2145 (epg-start-sign-keys context keys local)
2146 (epg-wait-for-completion context)
2147 (if (epg-context-result-for context 'error)
2148 (error "Sign keys failed: %S"
2149 (epg-context-result-for context 'error))))
2150 (epg-reset context)))
2153 (defun epg-start-generate-key (context parameters)
2154 "Initiate a key generation.
2155 PARAMETERS specifies parameters for the key.
2157 If you use this function, you will need to wait for the completion of
2158 `epg-gpg-program' by using `epg-wait-for-completion' and call
2159 `epg-reset' to clear a temporaly output file.
2160 If you are unsure, use synchronous version of this function
2161 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2162 (epg-context-set-operation context 'generate-key)
2163 (epg-context-set-result context nil)
2164 (if (epg-data-file parameters)
2165 (epg--start context (list "--batch" "--genkey" "--"
2166 (epg-data-file parameters)))
2167 (epg--start context '("--batch" "--genkey"))
2168 (if (eq (process-status (epg-context-process context)) 'run)
2169 (process-send-string (epg-context-process context)
2170 (epg-data-string parameters)))
2171 (if (eq (process-status (epg-context-process context)) 'run)
2172 (process-send-eof (epg-context-process context)))))
2175 (defun epg-generate-key-from-file (context parameters)
2176 "Generate a new key pair.
2177 PARAMETERS is a file which tells how to create the key."
2180 (epg-start-generate-key context (epg-make-data-from-file parameters))
2181 (epg-wait-for-completion context)
2182 (if (epg-context-result-for context 'error)
2183 (error "Generate key failed: %S"
2184 (epg-context-result-for context 'error))))
2185 (epg-reset context)))
2188 (defun epg-generate-key-from-string (context parameters)
2189 "Generate a new key pair.
2190 PARAMETERS is a string which tells how to create the key."
2193 (epg-start-generate-key context (epg-make-data-from-string parameters))
2194 (epg-wait-for-completion context)
2195 (if (epg-context-result-for context 'error)
2196 (error "Generate key failed: %S"
2197 (epg-context-result-for context 'error))))
2198 (epg-reset context)))
2200 (defun epg--decode-hexstring (string)
2202 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
2203 (setq string (replace-match "\\x\\&" t nil string)
2205 (car (read-from-string (concat "\"" string "\"")))))
2207 (defun epg--decode-quotedstring (string)
2209 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2210 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
2212 (if (match-beginning 2)
2213 (setq string (replace-match "\\2" t nil string)
2215 (if (match-beginning 3)
2216 (setq string (replace-match "\\x\\3" t nil string)
2218 (setq string (replace-match "\\\\\\\\\\4" t nil string)
2219 index (+ index 3)))))
2220 (car (read-from-string (concat "\"" string "\"")))))
2222 (defun epg-dn-from-string (string)
2223 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2224 The return value is an alist mapping from types to values."
2226 (length (length string))
2227 alist type value group)
2228 (while (< index length)
2229 (if (eq index (string-match "[ \t\n\r]*" string index))
2230 (setq index (match-end 0)))
2231 (if (eq index (string-match
2232 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2234 (setq type (match-string 1 string)
2235 index (match-end 0))
2236 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2238 (setq type (match-string 1 string)
2239 index (match-end 0))))
2241 (error "Invalid type"))
2242 (if (eq index (string-match
2243 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2245 (setq index (match-end 0)
2246 value (epg--decode-quotedstring (match-string 0 string)))
2247 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
2248 (setq index (match-end 0)
2249 value (epg--decode-hexstring (match-string 1 string)))
2250 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2252 (setq index (match-end 0)
2253 value (epg--decode-quotedstring
2254 (match-string 0 string))))))
2256 (if (stringp (car (car alist)))
2257 (setcar alist (list (cons type value) (car alist)))
2258 (setcar alist (cons (cons type value) (car alist))))
2259 (if (consp (car (car alist)))
2260 (setcar alist (nreverse (car alist))))
2261 (setq alist (cons (cons type value) alist)
2264 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
2265 (setq index (match-end 0)
2266 group (eq (aref string (match-beginning 1)) ?+))))
2269 (defun epg-decode-dn (alist)
2270 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2271 Type names are resolved using `epg-dn-type-alist'."
2274 (if (stringp (car rdn))
2275 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
2277 (format "%s=%s" (cdr entry) (cdr rdn))
2278 (format "%s=%s" (car rdn) (cdr rdn))))
2279 (concat "(" (epg-decode-dn rdn) ")")))
2285 ;;; epg.el ends here