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 notations))
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-sig-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-make-import-status (fingerprint reason new user-id signature sub-key
785 "Return a import status object."
786 (cons 'epg-import-status (vector fingerprint reason new user-id signature
789 (defun epg-import-status-fingerprint (import-status)
790 "Return the fingerprint of the key that was considered."
791 (unless (eq (car-safe import-status) 'epg-import-status)
792 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
793 (aref (cdr import-status) 0))
795 (defun epg-import-status-reason (import-status)
796 "Return the reason code for import failure."
797 (unless (eq (car-safe import-status) 'epg-import-status)
798 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
799 (aref (cdr import-status) 1))
801 (defun epg-import-status-new (import-status)
802 "Return t if the imported key was new."
803 (unless (eq (car-safe import-status) 'epg-import-status)
804 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
805 (aref (cdr import-status) 2))
807 (defun epg-import-status-user-id (import-status)
808 "Return t if the imported key contained new user IDs."
809 (unless (eq (car-safe import-status) 'epg-import-status)
810 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
811 (aref (cdr import-status) 3))
813 (defun epg-import-status-signature (import-status)
814 "Return t if the imported key contained new signatures."
815 (unless (eq (car-safe import-status) 'epg-import-status)
816 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
817 (aref (cdr import-status) 4))
819 (defun epg-import-status-sub-key (import-status)
820 "Return t if the imported key contained new sub keys."
821 (unless (eq (car-safe import-status) 'epg-import-status)
822 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
823 (aref (cdr import-status) 5))
825 (defun epg-import-status-secret (import-status)
826 "Return t if the imported key contained a secret key."
827 (unless (eq (car-safe import-status) 'epg-import-status)
828 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
829 (aref (cdr import-status) 6))
831 (defun epg-make-import-result (considered no-user-id imported imported-rsa
832 unchanged new-user-id new-sub-key
833 new-signature new-revocation
834 secret-read secret-imported
835 secret-unchanged not-imported)
836 "Return a import result object."
837 (cons 'epg-import-result (vector considered no-user-id imported imported-rsa
838 unchanged new-user-ids new-sub-keys
839 new-signatures new-revocations secret-read
840 secret-imported secret-unchanged
843 (defun epg-import-result-considered (import-result)
844 "Return the total number of considered keys."
845 (unless (eq (car-safe import-result) 'epg-import-result)
846 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
847 (aref (cdr import-result) 0))
849 (defun epg-import-result-no-user-id (import-result)
850 "Return the number of keys without user ID."
851 (unless (eq (car-safe import-result) 'epg-import-result)
852 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
853 (aref (cdr import-result) 1))
855 (defun epg-import-result-imported (import-result)
856 "Return the number of imported keys."
857 (unless (eq (car-safe import-result) 'epg-import-result)
858 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
859 (aref (cdr import-result) 2))
861 (defun epg-import-result-imported-rsa (import-result)
862 "Return the number of imported RSA keys."
863 (unless (eq (car-safe import-result) 'epg-import-result)
864 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
865 (aref (cdr import-result) 3))
867 (defun epg-import-result-unchanged (import-result)
868 "Return the number of unchanged keys."
869 (unless (eq (car-safe import-result) 'epg-import-result)
870 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
871 (aref (cdr import-result) 4))
873 (defun epg-import-result-new-user-ids (import-result)
874 "Return the number of new user IDs."
875 (unless (eq (car-safe import-result) 'epg-import-result)
876 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
877 (aref (cdr import-result) 5))
879 (defun epg-import-result-new-sub-keys (import-result)
880 "Return the number of new sub keys."
881 (unless (eq (car-safe import-result) 'epg-import-result)
882 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
883 (aref (cdr import-result) 6))
885 (defun epg-import-result-new-signatures (import-result)
886 "Return the number of new signatures."
887 (unless (eq (car-safe import-result) 'epg-import-result)
888 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
889 (aref (cdr import-result) 7))
891 (defun epg-import-result-new-revocations (import-result)
892 "Return the number of new revocations."
893 (unless (eq (car-safe import-result) 'epg-import-result)
894 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
895 (aref (cdr import-result) 8))
897 (defun epg-import-result-secret-read (import-result)
898 "Return the total number of secret keys read."
899 (unless (eq (car-safe import-result) 'epg-import-result)
900 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
901 (aref (cdr import-result) 9))
903 (defun epg-import-result-secret-imported (import-result)
904 "Return the number of imported secret keys."
905 (unless (eq (car-safe import-result) 'epg-import-result)
906 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
907 (aref (cdr import-result) 10))
909 (defun epg-import-result-secret-unchanged (import-result)
910 "Return the number of unchanged secret keys."
911 (unless (eq (car-safe import-result) 'epg-import-result)
912 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
913 (aref (cdr import-result) 11))
915 (defun epg-import-result-not-imported (import-result)
916 "Return the number of keys not imported."
917 (unless (eq (car-safe import-result) 'epg-import-result)
918 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
919 (aref (cdr import-result) 12))
921 (defun epg-import-result-imports (import-result)
922 "Return the list of `epg-import-status' objects."
923 (unless (eq (car-safe import-result) 'epg-import-result)
924 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
925 (aref (cdr import-result) 13))
927 (defun epg-context-result-for (context name)
928 "Return the result of CONTEXT associated with NAME."
929 (cdr (assq name (epg-context-result context))))
931 (defun epg-context-set-result-for (context name value)
932 "Set the result of CONTEXT associated with NAME to VALUE."
933 (let* ((result (epg-context-result context))
934 (entry (assq name result)))
937 (epg-context-set-result context (cons (cons name value) result)))))
939 (defun epg-signature-to-string (signature)
940 "Convert SIGNATURE to a human readable string."
941 (let ((user-id (cdr (assoc (epg-signature-key-id signature)
942 epg-user-id-alist))))
944 (cond ((eq (epg-signature-status signature) 'good)
945 "Good signature from ")
946 ((eq (epg-signature-status signature) 'bad)
947 "Bad signature from ")
948 ((eq (epg-signature-status signature) 'expired)
949 "Expired signature from ")
950 ((eq (epg-signature-status signature) 'expired-key)
951 "Signature made by expired key ")
952 ((eq (epg-signature-status signature) 'revoked-key)
953 "Signature made by revoked key ")
954 ((eq (epg-signature-status signature) 'no-pubkey)
955 "No public key for "))
956 (epg-signature-key-id signature)
959 (if (stringp user-id)
961 (epg-decode-dn user-id)))
963 (if (epg-signature-validity signature)
964 (format " (trust %s)" (epg-signature-validity signature))
967 (defun epg-verify-result-to-string (verify-result)
968 "Convert VERIFY-RESULT to a human readable string."
969 (mapconcat #'epg-signature-to-string verify-result "\n"))
971 (defun epg-new-signature-to-string (new-signature)
972 "Convert NEW-SIGNATURE to a human readable string."
974 (cond ((eq (epg-new-signature-type new-signature) 'detached)
975 "Detached signature ")
976 ((eq (epg-new-signature-type new-signature) 'clear)
977 "Cleartext signature ")
980 (cdr (assq (epg-new-signature-pubkey-algorithm new-signature)
981 epg-pubkey-algorithm-alist))
983 (cdr (assq (epg-new-signature-digest-algorithm new-signature)
984 epg-digest-algorithm-alist))
986 (format "%02X " (epg-new-signature-class new-signature))
987 (epg-new-signature-fingerprint new-signature)))
989 (defun epg--start (context args)
990 "Start `epg-gpg-program' in a subprocess with given ARGS."
991 (if (and (epg-context-process context)
992 (eq (process-status (epg-context-process context)) 'run))
993 (error "%s is already running in this context"
994 (if (eq (epg-context-protocol context) 'CMS)
997 (let* ((args (append (list "--no-tty"
1000 (if (epg-context-progress-callback context)
1001 (list "--enable-progress-filter"))
1002 (if epg-gpg-home-directory
1003 (list "--homedir" epg-gpg-home-directory))
1004 (unless (eq (epg-context-protocol context) 'CMS)
1005 (list "--command-fd" "0"))
1006 (if (epg-context-armor context) '("--armor"))
1007 (if (epg-context-textmode context) '("--textmode"))
1008 (if (epg-context-output-file context)
1009 (list "--output" (epg-context-output-file context)))
1011 (coding-system-for-write 'binary)
1012 process-connection-type
1013 (orig-mode (default-file-modes))
1014 (buffer (generate-new-buffer " *epg*"))
1018 (unless epg-debug-buffer
1019 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
1020 (set-buffer epg-debug-buffer)
1021 (goto-char (point-max))
1022 (insert (format "%s %s\n"
1023 (if (eq (epg-context-protocol context) 'CMS)
1026 (mapconcat #'identity args " ")))))
1027 (with-current-buffer buffer
1028 (make-local-variable 'epg-read-point)
1029 (setq epg-read-point (point-min))
1030 (make-local-variable 'epg-process-filter-running)
1031 (setq epg-process-filter-running nil)
1032 (make-local-variable 'epg-pending-status-list)
1033 (setq epg-pending-status-list nil)
1034 (make-local-variable 'epg-key-id)
1035 (setq epg-key-id nil)
1036 (make-local-variable 'epg-context)
1037 (setq epg-context context))
1040 (set-default-file-modes 448)
1042 (apply #'start-process "epg" buffer
1043 (if (eq (epg-context-protocol context) 'CMS)
1047 (set-default-file-modes orig-mode))
1048 (set-process-filter process #'epg--process-filter)
1049 (epg-context-set-process context process)))
1051 (defun epg--process-filter (process input)
1054 (unless epg-debug-buffer
1055 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
1056 (set-buffer epg-debug-buffer)
1057 (goto-char (point-max))
1059 (if (buffer-live-p (process-buffer process))
1061 (set-buffer (process-buffer process))
1062 (goto-char (point-max))
1064 (unless epg-process-filter-running
1067 (setq epg-process-filter-running t)
1068 (goto-char epg-read-point)
1070 (while (looking-at ".*\n") ;the input line finished
1071 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
1072 (let* ((status (match-string 1))
1073 (string (match-string 2))
1074 (symbol (intern-soft (concat "epg--status-"
1076 (if (member status epg-pending-status-list)
1077 (setq epg-pending-status-list nil))
1080 (funcall symbol epg-context string))))
1082 (setq epg-read-point (point))))
1083 (setq epg-process-filter-running nil))))))
1085 (defun epg-read-output (context)
1086 "Read the output file CONTEXT and return the content as a string."
1088 (if (fboundp 'set-buffer-multibyte)
1089 (set-buffer-multibyte nil))
1090 (if (file-exists-p (epg-context-output-file context))
1091 (let ((coding-system-for-read 'binary))
1092 (insert-file-contents (epg-context-output-file context))
1095 (defun epg-wait-for-status (context status-list)
1096 "Wait until one of elements in STATUS-LIST arrives."
1097 (with-current-buffer (process-buffer (epg-context-process context))
1098 (setq epg-pending-status-list status-list)
1099 (while (and (eq (process-status (epg-context-process context)) 'run)
1100 epg-pending-status-list)
1101 (accept-process-output (epg-context-process context) 1))))
1103 (defun epg-wait-for-completion (context)
1104 "Wait until the `epg-gpg-program' process completes."
1105 (while (eq (process-status (epg-context-process context)) 'run)
1106 (accept-process-output (epg-context-process context) 1)))
1108 (defun epg-reset (context)
1109 "Reset the CONTEXT."
1110 (if (and (epg-context-process context)
1111 (buffer-live-p (process-buffer (epg-context-process context))))
1112 (kill-buffer (process-buffer (epg-context-process context))))
1113 (epg-context-set-process context nil))
1115 (defun epg-delete-output-file (context)
1116 "Delete the output file of CONTEXT."
1117 (if (and (epg-context-output-file context)
1118 (file-exists-p (epg-context-output-file context)))
1119 (delete-file (epg-context-output-file context))))
1121 (defun epg--status-USERID_HINT (context string)
1122 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1123 (let* ((key-id (match-string 1 string))
1124 (user-id (match-string 2 string))
1125 (entry (assoc key-id epg-user-id-alist)))
1127 (setcdr entry user-id)
1128 (setq epg-user-id-alist (cons (cons key-id user-id)
1129 epg-user-id-alist))))))
1131 (defun epg--status-NEED_PASSPHRASE (context string)
1132 (if (string-match "\\`\\([^ ]+\\)" string)
1133 (setq epg-key-id (match-string 1 string))))
1135 (defun epg--status-NEED_PASSPHRASE_SYM (context string)
1136 (setq epg-key-id 'SYM))
1138 (defun epg--status-NEED_PASSPHRASE_PIN (context string)
1139 (setq epg-key-id 'PIN))
1141 (defun epg--status-GET_HIDDEN (context string)
1142 (when (and epg-key-id
1143 (string-match "\\`passphrase\\." string))
1144 (unless (epg-context-passphrase-callback context)
1145 (error "passphrase-callback not set"))
1148 passphrase-with-new-line
1149 encoded-passphrase-with-new-line)
1155 (if (consp (epg-context-passphrase-callback context))
1156 (car (epg-context-passphrase-callback context))
1157 (epg-context-passphrase-callback context))
1160 (if (consp (epg-context-passphrase-callback context))
1161 (cdr (epg-context-passphrase-callback context)))))
1163 (setq passphrase-with-new-line (concat passphrase "\n"))
1164 (epg--clear-string passphrase)
1165 (setq passphrase nil)
1166 (if epg-passphrase-coding-system
1168 (setq encoded-passphrase-with-new-line
1169 (encode-coding-string
1170 passphrase-with-new-line
1171 epg-passphrase-coding-system))
1172 (epg--clear-string passphrase-with-new-line)
1173 (setq passphrase-with-new-line nil))
1174 (setq encoded-passphrase-with-new-line
1175 passphrase-with-new-line
1176 passphrase-with-new-line nil))
1177 (process-send-string (epg-context-process context)
1178 encoded-passphrase-with-new-line)))
1180 (epg-context-set-result-for
1183 (epg-context-result-for context 'error)))
1184 (delete-process (epg-context-process context))))
1186 (epg--clear-string passphrase))
1187 (if passphrase-with-new-line
1188 (epg--clear-string passphrase-with-new-line))
1189 (if encoded-passphrase-with-new-line
1190 (epg--clear-string encoded-passphrase-with-new-line))))))
1192 (defun epg--status-GET_BOOL (context string)
1193 (let ((entry (assoc string epg-prompt-alist))
1196 (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
1197 (process-send-string (epg-context-process context) "y\n")
1198 (process-send-string (epg-context-process context) "n\n"))
1200 (epg-context-set-result-for
1203 (epg-context-result-for context 'error)))
1204 (delete-process (epg-context-process context))))))
1206 (defun epg--status-GET_LINE (context string)
1207 (let ((entry (assoc string epg-prompt-alist))
1210 (process-send-string (epg-context-process context)
1211 (concat (read-string
1214 (concat string ": ")))
1217 (epg-context-set-result-for
1220 (epg-context-result-for context 'error)))
1221 (delete-process (epg-context-process context))))))
1223 (defun epg--status-*SIG (context status string)
1224 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1225 (let* ((key-id (match-string 1 string))
1226 (user-id (match-string 2 string))
1227 (entry (assoc key-id epg-user-id-alist)))
1228 (epg-context-set-result-for
1231 (cons (epg-make-signature status key-id)
1232 (epg-context-result-for context 'verify)))
1233 (if (eq (epg-context-protocol context) 'CMS)
1235 (setq user-id (epg-dn-from-string user-id))
1238 (setcdr entry user-id)
1239 (setq epg-user-id-alist
1240 (cons (cons key-id user-id) epg-user-id-alist))))
1241 (epg-context-set-result-for
1244 (cons (epg-make-signature status)
1245 (epg-context-result-for context 'verify)))))
1247 (defun epg--status-GOODSIG (context string)
1248 (epg--status-*SIG context 'good string))
1250 (defun epg--status-EXPSIG (context string)
1251 (epg--status-*SIG context 'expired string))
1253 (defun epg--status-EXPKEYSIG (context string)
1254 (epg--status-*SIG context 'expired-key string))
1256 (defun epg--status-REVKEYSIG (context string)
1257 (epg--status-*SIG context 'revoked-key string))
1259 (defun epg--status-BADSIG (context string)
1260 (epg--status-*SIG context 'bad string))
1262 (defun epg--status-NO_PUBKEY (context string)
1263 (let ((signature (car (epg-context-result-for context 'verify))))
1265 (eq (epg-signature-status signature) 'error)
1266 (equal (epg-signature-key-id signature) string))
1267 (epg-signature-set-status signature 'no-pubkey))))
1269 (defun epg--time-from-seconds (seconds)
1270 (let ((number-seconds (string-to-number (concat seconds ".0"))))
1271 (cons (floor (/ number-seconds 65536))
1272 (floor (mod number-seconds 65536)))))
1274 (defun epg--status-ERRSIG (context string)
1275 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1276 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
1278 (let ((signature (epg-make-signature 'error)))
1279 (epg-context-set-result-for
1283 (epg-context-result-for context 'verify)))
1284 (epg-signature-set-key-id
1286 (match-string 1 string))
1287 (epg-signature-set-pubkey-algorithm
1289 (string-to-number (match-string 2 string)))
1290 (epg-signature-set-digest-algorithm
1292 (string-to-number (match-string 3 string)))
1293 (epg-signature-set-class
1295 (string-to-number (match-string 4 string) 16))
1296 (epg-signature-set-creation-time
1298 (epg--time-from-seconds (match-string 5 string))))))
1300 (defun epg--status-VALIDSIG (context string)
1301 (let ((signature (car (epg-context-result-for context 'verify))))
1302 (when (and signature
1303 (eq (epg-signature-status signature) 'good)
1304 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
1305 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
1308 (epg-signature-set-fingerprint
1310 (match-string 1 string))
1311 (epg-signature-set-creation-time
1313 (epg--time-from-seconds (match-string 2 string)))
1314 (epg-signature-set-expiration-time
1316 (epg--time-from-seconds (match-string 3 string)))
1317 (epg-signature-set-version
1319 (string-to-number (match-string 4 string)))
1320 (epg-signature-set-pubkey-algorithm
1322 (string-to-number (match-string 5 string)))
1323 (epg-signature-set-digest-algorithm
1325 (string-to-number (match-string 6 string)))
1326 (epg-signature-set-class
1328 (string-to-number (match-string 7 string) 16)))))
1330 (defun epg--status-TRUST_UNDEFINED (context string)
1331 (let ((signature (car (epg-context-result-for context 'verify))))
1333 (eq (epg-signature-status signature) 'good))
1334 (epg-signature-set-validity signature 'undefined))))
1336 (defun epg--status-TRUST_NEVER (context string)
1337 (let ((signature (car (epg-context-result-for context 'verify))))
1339 (eq (epg-signature-status signature) 'good))
1340 (epg-signature-set-validity signature 'never))))
1342 (defun epg--status-TRUST_MARGINAL (context string)
1343 (let ((signature (car (epg-context-result-for context 'verify))))
1345 (eq (epg-signature-status signature) 'marginal))
1346 (epg-signature-set-validity signature 'marginal))))
1348 (defun epg--status-TRUST_FULLY (context string)
1349 (let ((signature (car (epg-context-result-for context 'verify))))
1351 (eq (epg-signature-status signature) 'good))
1352 (epg-signature-set-validity signature 'full))))
1354 (defun epg--status-TRUST_ULTIMATE (context string)
1355 (let ((signature (car (epg-context-result-for context 'verify))))
1357 (eq (epg-signature-status signature) 'good))
1358 (epg-signature-set-validity signature 'ultimate))))
1360 (defun epg--status-NOTATION_NAME (context string)
1361 (let ((signature (car (epg-context-result-for context 'verify))))
1363 (epg-signature-set-notations
1365 (cons (epg-make-sig-notation string nil t nil)
1366 (epg-sig-notations signature))))))
1368 (defun epg--status-NOTATION_DATA (context string)
1369 (let ((signature (car (epg-context-result-for context 'verify)))
1372 (setq notation (car (epg-sig-notations signature))))
1373 (epg-sig-notation-set-value notation string))))
1375 (defun epg--status-POLICY_URL (context string)
1376 (let ((signature (car (epg-context-result-for context 'verify))))
1378 (epg-signature-set-notations
1380 (cons (epg-make-sig-notation nil string t nil)
1381 (epg-sig-notations signature))))))
1383 (defun epg--status-PROGRESS (context string)
1384 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1386 (funcall (if (consp (epg-context-progress-callback context))
1387 (car (epg-context-progress-callback context))
1388 (epg-context-progress-callback context))
1390 (match-string 1 string)
1391 (match-string 2 string)
1392 (string-to-number (match-string 3 string))
1393 (string-to-number (match-string 4 string))
1394 (if (consp (epg-context-progress-callback context))
1395 (cdr (epg-context-progress-callback context))))))
1397 (defun epg--status-DECRYPTION_FAILED (context string)
1398 (epg-context-set-result-for
1400 (cons '(decryption-failed)
1401 (epg-context-result-for context 'error))))
1403 (defun epg--status-NODATA (context string)
1404 (epg-context-set-result-for
1406 (cons (list 'no-data (cons 'reason (string-to-number string)))
1407 (epg-context-result-for context 'error))))
1409 (defun epg--status-UNEXPECTED (context string)
1410 (epg-context-set-result-for
1412 (cons (list 'unexpected (cons 'reason (string-to-number string)))
1413 (epg-context-result-for context 'error))))
1415 (defun epg--status-KEYEXPIRED (context string)
1416 (epg-context-set-result-for
1418 (cons (list 'key-expired (cons 'expiration-time
1419 (epg--time-from-seconds string)))
1420 (epg-context-result-for context 'error))))
1422 (defun epg--status-KEYREVOKED (context string)
1423 (epg-context-set-result-for
1425 (cons '(key-revoked)
1426 (epg-context-result-for context 'error))))
1428 (defun epg--status-BADARMOR (context string)
1429 (epg-context-set-result-for
1432 (epg-context-result-for context 'error))))
1434 (defun epg--status-INV_RECP (context string)
1435 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1436 (epg-context-set-result-for
1438 (cons (list 'invalid-recipient
1440 (string-to-number (match-string 1 string)))
1441 (cons 'requested-recipient
1442 (match-string 2 string)))
1443 (epg-context-result-for context 'error)))))
1445 (defun epg--status-NO_RECP (context string)
1446 (epg-context-set-result-for
1448 (cons '(no-recipients)
1449 (epg-context-result-for context 'error))))
1451 (defun epg--status-DELETE_PROBLEM (context string)
1452 (if (string-match "\\`\\([0-9]+\\)" string)
1453 (epg-context-set-result-for
1455 (cons (list 'delete-problem
1456 (cons 'reason (string-to-number (match-string 1 string))))
1457 (epg-context-result-for context 'error)))))
1459 (defun epg--status-SIG_CREATED (context string)
1460 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1461 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1462 (epg-context-set-result-for
1464 (cons (epg-make-new-signature
1465 (cdr (assq (aref (match-string 1 string) 0)
1466 epg-new-signature-type-alist))
1467 (string-to-number (match-string 2 string))
1468 (string-to-number (match-string 3 string))
1469 (string-to-number (match-string 4 string) 16)
1470 (epg--time-from-seconds (match-string 5 string))
1471 (substring string (match-end 0)))
1472 (epg-context-result-for context 'sign)))))
1474 (defun epg--status-KEY_CREATED (context string)
1475 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1476 (epg-context-set-result-for
1477 context 'generate-key
1478 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1479 (cons 'fingerprint (match-string 2 string)))
1480 (epg-context-result-for context 'generate-key)))))
1482 (defun epg--status-KEY_NOT_CREATED (context string)
1483 (epg-context-set-result-for
1485 (cons '(key-not-created)
1486 (epg-context-result-for context 'error))))
1488 (defun epg--status-IMPORTED (context string)
1489 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1490 (let* ((key-id (match-string 1 string))
1491 (user-id (match-string 2 string))
1492 (entry (assoc key-id epg-user-id-alist)))
1494 (setcdr entry user-id)
1495 (setq epg-user-id-alist (cons (cons key-id user-id)
1496 epg-user-id-alist))))))
1498 (defun epg--status-IMPORT_OK (context string)
1499 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1500 (let ((reason (string-to-number (match-string 1 string))))
1501 (epg-context-set-result-for
1502 context 'import-status
1503 (cons (epg-make-import-status (if (match-beginning 2)
1504 (match-string 3 string))
1506 (/= (logand reason 1) 0)
1507 (/= (logand reason 2) 0)
1508 (/= (logand reason 4) 0)
1509 (/= (logand reason 8) 0)
1510 (/= (logand reason 16) 0))
1511 (epg-context-result-for context 'import-status))))))
1513 (defun epg--status-IMPORT_PROBLEM (context string)
1514 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1515 (epg-context-set-result-for
1516 context 'import-status
1517 (cons (epg-make-import-status
1518 (if (match-beginning 2)
1519 (match-string 3 string))
1520 (string-to-number (match-string 1 string)))
1521 (epg-context-result-for context 'import-status)))))
1523 (defun epg--status-IMPORT_RES (context string)
1524 (when (string-match "\\`\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1525 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1526 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
1527 (epg-context-set-result-for
1529 (cons (epg-make-import-result
1530 (string-to-number (match-string 1 string))
1531 (string-to-number (match-string 2 string))
1532 (string-to-number (match-string 3 string))
1533 (string-to-number (match-string 4 string))
1534 (string-to-number (match-string 5 string))
1535 (string-to-number (match-string 6 string))
1536 (string-to-number (match-string 7 string))
1537 (string-to-number (match-string 8 string))
1538 (string-to-number (match-string 9 string))
1539 (string-to-number (match-string 10 string))
1540 (string-to-number (match-string 11 string))
1541 (string-to-number (match-string 12 string))
1542 (string-to-number (match-string 13 string))
1543 (epg-context-result-for context 'import-status))
1544 (epg-context-result-for context 'import)))
1545 (epg-context-set-result-for context 'import-status nil)))
1547 (defun epg-passphrase-callback-function (context key-id handback)
1548 (if (eq key-id 'SYM)
1549 (read-passwd "Passphrase for symmetric encryption: "
1550 (eq (epg-context-operation context) 'encrypt))
1552 (if (eq key-id 'PIN)
1553 "Passphrase for PIN: "
1554 (let ((entry (assoc key-id epg-user-id-alist)))
1556 (format "Passphrase for %s %s: " key-id (cdr entry))
1557 (format "Passphrase for %s: " key-id)))))))
1559 (make-obsolete 'epg-passphrase-callback-function
1560 'epa-passphrase-callback-function)
1562 (defun epg--list-keys-1 (context name mode)
1563 (let ((args (append (if epg-gpg-home-directory
1564 (list "--homedir" epg-gpg-home-directory))
1565 (list "--with-colons" "--no-greeting" "--batch"
1566 "--with-fingerprint"
1567 "--with-fingerprint"
1568 (if (memq mode '(t secret))
1569 "--list-secret-keys"
1570 (if (memq mode '(nil public))
1573 (unless (eq (epg-context-protocol context) 'CMS)
1574 '("--fixed-list-mode"))
1575 (if name (list name))))
1576 keys string field index)
1578 (apply #'call-process
1579 (if (eq (epg-context-protocol context) 'CMS)
1582 nil (list t nil) nil args)
1583 (goto-char (point-min))
1584 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1585 (setq keys (cons (make-vector 15 nil) keys)
1586 string (match-string 0)
1590 (string-match "\\([^:]+\\)?:" string index))
1591 (setq index (match-end 0))
1592 (aset (car keys) field (match-string 1 string))
1593 (setq field (1+ field))))
1596 (defun epg--make-sub-key-1 (line)
1599 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1601 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1603 (member (aref line 0) '("sec" "ssb"))
1604 (string-to-number (aref line 3))
1605 (string-to-number (aref line 2))
1607 (epg--time-from-seconds (aref line 5))
1608 (epg--time-from-seconds (aref line 6))))
1611 (defun epg-list-keys (context &optional name mode)
1612 "Return a list of epg-key objects matched with NAME.
1613 If MODE is nil or 'public, only public keyring should be searched.
1614 If MODE is t or 'secret, only secret keyring should be searched.
1615 Otherwise, only public keyring should be searched and the key
1616 signatures should be included."
1617 (let ((lines (epg--list-keys-1 context name mode))
1618 keys cert pointer pointer-1)
1621 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1622 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1623 keys (cons (epg-make-key
1624 (if (aref (car lines) 8)
1625 (cdr (assq (string-to-char (aref (car lines) 8))
1626 epg-key-validity-alist))))
1628 (epg-key-set-sub-key-list
1630 (cons (epg--make-sub-key-1 (car lines))
1631 (epg-key-sub-key-list (car keys)))))
1632 ((member (aref (car lines) 0) '("sub" "ssb"))
1633 (epg-key-set-sub-key-list
1635 (cons (epg--make-sub-key-1 (car lines))
1636 (epg-key-sub-key-list (car keys)))))
1637 ((equal (aref (car lines) 0) "uid")
1638 (epg-key-set-user-id-list
1640 (cons (epg-make-user-id
1641 (if (aref (car lines) 1)
1642 (cdr (assq (string-to-char (aref (car lines) 1))
1643 epg-key-validity-alist)))
1646 (epg-dn-from-string (aref (car lines) 9))
1647 (error (aref (car lines) 9)))
1648 (aref (car lines) 9)))
1649 (epg-key-user-id-list (car keys)))))
1650 ((equal (aref (car lines) 0) "fpr")
1651 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1652 (aref (car lines) 9)))
1653 ((equal (aref (car lines) 0) "sig")
1654 (epg-user-id-set-signature-list
1655 (car (epg-key-user-id-list (car keys)))
1657 (epg-make-key-signature
1658 (if (aref (car lines) 1)
1659 (cdr (assq (string-to-char (aref (car lines) 1))
1660 epg-key-validity-alist)))
1661 (string-to-number (aref (car lines) 3))
1662 (aref (car lines) 4)
1663 (epg--time-from-seconds (aref (car lines) 5))
1664 (epg--time-from-seconds (aref (car lines) 6))
1665 (aref (car lines) 9)
1666 (string-to-number (aref (car lines) 10) 16)
1667 (eq (aref (aref (car lines) 10) 2) ?x))
1668 (epg-user-id-signature-list
1669 (car (epg-key-user-id-list (car keys))))))))
1670 (setq lines (cdr lines)))
1671 (setq keys (nreverse keys)
1674 (epg-key-set-sub-key-list
1676 (nreverse (epg-key-sub-key-list (car pointer))))
1677 (setq pointer-1 (epg-key-set-user-id-list
1679 (nreverse (epg-key-user-id-list (car pointer)))))
1681 (epg-user-id-set-signature-list
1683 (nreverse (epg-user-id-signature-list (car pointer-1))))
1684 (setq pointer-1 (cdr pointer-1)))
1685 (setq pointer (cdr pointer)))
1688 (if (fboundp 'make-temp-file)
1689 (defalias 'epg--make-temp-file 'make-temp-file)
1690 (defvar temporary-file-directory)
1691 ;; stolen from poe.el.
1692 (defun epg--make-temp-file (prefix)
1693 "Create a temporary file.
1694 The returned file name (created by appending some random characters at the end
1695 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1696 is guaranteed to point to a newly created empty file.
1697 You can then use `write-region' to write new data into the file."
1698 (let (tempdir tempfile)
1699 (setq prefix (expand-file-name prefix
1700 (if (featurep 'xemacs)
1702 temporary-file-directory)))
1705 ;; First, create a temporary directory.
1706 (while (condition-case ()
1708 (setq tempdir (make-temp-name
1710 (file-name-directory prefix)
1712 ;; return nil or signal an error.
1713 (make-directory tempdir))
1715 (file-already-exists t)))
1716 (set-file-modes tempdir 448)
1717 ;; Second, create a temporary file in the tempdir.
1718 ;; There *is* a race condition between `make-temp-name'
1719 ;; and `write-region', but we don't care it since we are
1720 ;; in a private directory now.
1721 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1722 (write-region "" nil tempfile nil 'silent)
1723 (set-file-modes tempfile 384)
1724 ;; Finally, make a hard-link from the tempfile.
1725 (while (condition-case ()
1727 (setq file (make-temp-name prefix))
1728 ;; return nil or signal an error.
1729 (add-name-to-file tempfile file))
1731 (file-already-exists t)))
1733 ;; Cleanup the tempfile.
1735 (file-exists-p tempfile)
1736 (delete-file tempfile))
1737 ;; Cleanup the tempdir.
1739 (file-directory-p tempdir)
1740 (delete-directory tempdir))))))
1742 (if (fboundp 'clear-string)
1743 (defalias 'epg--clear-string 'clear-string)
1744 (defun epg--clear-string (string)
1745 (fillarray string 0)))
1747 (defun epg--args-from-sig-notations (notations)
1751 (if (and (epg-sig-notation-name notation)
1752 (not (epg-sig-notation-human-readable notation)))
1753 (error "Unreadable"))
1754 (if (epg-sig-notation-name notation)
1755 (list "--sig-notation"
1756 (if (epg-sig-notation-critical notation)
1757 (concat "!" (epg-sig-notation-name notation)
1758 "=" (epg-sig-notation-value notation))
1759 (concat (epg-sig-notation-name notation)
1760 "=" (epg-sig-notation-value notation))))
1761 (list "--sig-policy-url"
1762 (if (epg-sig-notation-critical notation)
1763 (concat "!" (epg-sig-notation-value notation))
1764 (epg-sig-notation-value notation)))))
1768 (defun epg-cancel (context)
1769 (if (buffer-live-p (process-buffer (epg-context-process context)))
1771 (set-buffer (process-buffer (epg-context-process context)))
1772 (epg-context-set-result-for
1775 (epg-context-result-for epg-context 'error)))))
1776 (if (eq (process-status (epg-context-process context)) 'run)
1777 (delete-process (epg-context-process context))))
1780 (defun epg-start-decrypt (context cipher)
1781 "Initiate a decrypt operation on CIPHER.
1782 CIPHER must be a file data object.
1784 If you use this function, you will need to wait for the completion of
1785 `epg-gpg-program' by using `epg-wait-for-completion' and call
1786 `epg-reset' to clear a temporaly output file.
1787 If you are unsure, use synchronous version of this function
1788 `epg-decrypt-file' or `epg-decrypt-string' instead."
1789 (unless (epg-data-file cipher)
1790 (error "Not a file"))
1791 (epg-context-set-operation context 'decrypt)
1792 (epg-context-set-result context nil)
1793 (epg--start context (list "--decrypt" "--" (epg-data-file cipher)))
1794 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1795 (unless (eq (epg-context-protocol context) 'CMS)
1796 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1799 (defun epg-decrypt-file (context cipher plain)
1800 "Decrypt a file CIPHER and store the result to a file PLAIN.
1801 If PLAIN is nil, it returns the result as a string."
1805 (epg-context-set-output-file context plain)
1806 (epg-context-set-output-file context
1807 (epg--make-temp-file "epg-output")))
1808 (epg-start-decrypt context (epg-make-data-from-file cipher))
1809 (epg-wait-for-completion context)
1810 (if (epg-context-result-for context 'error)
1811 (error "Decrypt failed: %S"
1812 (epg-context-result-for context 'error)))
1814 (epg-read-output context)))
1816 (epg-delete-output-file context))
1817 (epg-reset context)))
1820 (defun epg-decrypt-string (context cipher)
1821 "Decrypt a string CIPHER and return the plain text."
1822 (let ((input-file (epg--make-temp-file "epg-input"))
1823 (coding-system-for-write 'binary))
1826 (write-region cipher nil input-file nil 'quiet)
1827 (epg-context-set-output-file context
1828 (epg--make-temp-file "epg-output"))
1829 (epg-start-decrypt context (epg-make-data-from-file input-file))
1830 (epg-wait-for-completion context)
1831 (if (epg-context-result-for context 'error)
1832 (error "Decrypt failed: %S"
1833 (epg-context-result-for context 'error)))
1834 (epg-read-output context))
1835 (epg-delete-output-file context)
1836 (if (file-exists-p input-file)
1837 (delete-file input-file))
1838 (epg-reset context))))
1841 (defun epg-start-verify (context signature &optional signed-text)
1842 "Initiate a verify operation on SIGNATURE.
1843 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1845 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1846 For a normal or a cleartext signature, SIGNED-TEXT should be nil.
1848 If you use this function, you will need to wait for the completion of
1849 `epg-gpg-program' by using `epg-wait-for-completion' and call
1850 `epg-reset' to clear a temporaly output file.
1851 If you are unsure, use synchronous version of this function
1852 `epg-verify-file' or `epg-verify-string' instead."
1853 (epg-context-set-operation context 'verify)
1854 (epg-context-set-result context nil)
1856 ;; Detached signature.
1857 (if (epg-data-file signed-text)
1858 (epg--start context (list "--verify" "--" (epg-data-file signature)
1859 (epg-data-file signed-text)))
1860 (epg--start context (list "--verify" "--" (epg-data-file signature)
1862 (if (eq (process-status (epg-context-process context)) 'run)
1863 (process-send-string (epg-context-process context)
1864 (epg-data-string signed-text)))
1865 (if (eq (process-status (epg-context-process context)) 'run)
1866 (process-send-eof (epg-context-process context))))
1867 ;; Normal (or cleartext) signature.
1868 (if (epg-data-file signature)
1869 (epg--start context (list "--" (epg-data-file signature)))
1870 (epg--start context '("-"))
1871 (if (eq (process-status (epg-context-process context)) 'run)
1872 (process-send-string (epg-context-process context)
1873 (epg-data-string signature)))
1874 (if (eq (process-status (epg-context-process context)) 'run)
1875 (process-send-eof (epg-context-process context))))))
1878 (defun epg-verify-file (context signature &optional signed-text plain)
1879 "Verify a file SIGNATURE.
1880 SIGNED-TEXT and PLAIN are also a file if they are specified.
1882 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1883 string. For a normal or a cleartext signature, SIGNED-TEXT should be
1884 nil. In the latter case, if PLAIN is specified, the plaintext is
1885 stored into the file after successful verification."
1889 (epg-context-set-output-file context plain)
1890 (epg-context-set-output-file context
1891 (epg--make-temp-file "epg-output")))
1893 (epg-start-verify context
1894 (epg-make-data-from-file signature)
1895 (epg-make-data-from-file signed-text))
1896 (epg-start-verify context
1897 (epg-make-data-from-file signature)))
1898 (epg-wait-for-completion context)
1900 (epg-read-output context)))
1902 (epg-delete-output-file context))
1903 (epg-reset context)))
1906 (defun epg-verify-string (context signature &optional signed-text)
1907 "Verify a string SIGNATURE.
1908 SIGNED-TEXT is a string if it is specified.
1910 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1911 string. For a normal or a cleartext signature, SIGNED-TEXT should be
1912 nil. In the latter case, this function returns the plaintext after
1913 successful verification."
1914 (let ((coding-system-for-write 'binary)
1918 (epg-context-set-output-file context
1919 (epg--make-temp-file "epg-output"))
1922 (setq input-file (epg--make-temp-file "epg-signature"))
1923 (write-region signature nil input-file nil 'quiet)
1924 (epg-start-verify context
1925 (epg-make-data-from-file input-file)
1926 (epg-make-data-from-string signed-text)))
1927 (epg-start-verify context (epg-make-data-from-string signature)))
1928 (epg-wait-for-completion context)
1929 (epg-read-output context))
1930 (epg-delete-output-file context)
1932 (file-exists-p input-file))
1933 (delete-file input-file))
1934 (epg-reset context))))
1937 (defun epg-start-sign (context plain &optional mode)
1938 "Initiate a sign operation on PLAIN.
1939 PLAIN is a data object.
1941 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1942 If it is nil or 'normal, it makes a normal signature.
1943 Otherwise, it makes a cleartext signature.
1945 If you use this function, you will need to wait for the completion of
1946 `epg-gpg-program' by using `epg-wait-for-completion' and call
1947 `epg-reset' to clear a temporaly output file.
1948 If you are unsure, use synchronous version of this function
1949 `epg-sign-file' or `epg-sign-string' instead."
1950 (epg-context-set-operation context 'sign)
1951 (epg-context-set-result context nil)
1952 (unless (memq mode '(t detached nil normal)) ;i.e. cleartext
1953 (epg-context-set-armor context nil)
1954 (epg-context-set-textmode context nil))
1956 (append (list (if (memq mode '(t detached))
1958 (if (memq mode '(nil normal))
1966 (car (epg-key-sub-key-list signer)))))
1967 (epg-context-signers context)))
1968 (epg--args-from-sig-notations
1969 (epg-context-sig-notations context))
1970 (if (epg-data-file plain)
1971 (list "--" (epg-data-file plain)))))
1972 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1973 (unless (eq (epg-context-protocol context) 'CMS)
1974 (epg-wait-for-status context '("BEGIN_SIGNING")))
1975 (when (epg-data-string plain)
1976 (if (eq (process-status (epg-context-process context)) 'run)
1977 (process-send-string (epg-context-process context)
1978 (epg-data-string plain)))
1979 (if (eq (process-status (epg-context-process context)) 'run)
1980 (process-send-eof (epg-context-process context)))))
1983 (defun epg-sign-file (context plain signature &optional mode)
1984 "Sign a file PLAIN and store the result to a file SIGNATURE.
1985 If SIGNATURE is nil, it returns the result as a string.
1986 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1987 If it is nil or 'normal, it makes a normal signature.
1988 Otherwise, it makes a cleartext signature."
1992 (epg-context-set-output-file context signature)
1993 (epg-context-set-output-file context
1994 (epg--make-temp-file "epg-output")))
1995 (epg-start-sign context (epg-make-data-from-file plain) mode)
1996 (epg-wait-for-completion context)
1997 (unless (epg-context-result-for context 'sign)
1998 (if (epg-context-result-for context 'error)
1999 (error "Sign failed: %S"
2000 (epg-context-result-for context 'error))
2001 (error "Sign failed")))
2003 (epg-read-output context)))
2005 (epg-delete-output-file context))
2006 (epg-reset context)))
2009 (defun epg-sign-string (context plain &optional mode)
2010 "Sign a string PLAIN and return the output as string.
2011 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2012 If it is nil or 'normal, it makes a normal signature.
2013 Otherwise, it makes a cleartext signature."
2016 (epg-context-set-output-file context
2017 (epg--make-temp-file "epg-output"))
2018 (epg-start-sign context (epg-make-data-from-string plain) mode)
2019 (epg-wait-for-completion context)
2020 (unless (epg-context-result-for context 'sign)
2021 (if (epg-context-result-for context 'error)
2022 (error "Sign failed: %S"
2023 (epg-context-result-for context 'error))
2024 (error "Sign failed")))
2025 (epg-read-output context))
2026 (epg-delete-output-file context)
2027 (epg-reset context)))
2030 (defun epg-start-encrypt (context plain recipients
2031 &optional sign always-trust)
2032 "Initiate an encrypt operation on PLAIN.
2033 PLAIN is a data object.
2034 If RECIPIENTS is nil, it performs symmetric encryption.
2036 If you use this function, you will need to wait for the completion of
2037 `epg-gpg-program' by using `epg-wait-for-completion' and call
2038 `epg-reset' to clear a temporaly output file.
2039 If you are unsure, use synchronous version of this function
2040 `epg-encrypt-file' or `epg-encrypt-string' instead."
2041 (epg-context-set-operation context 'encrypt)
2042 (epg-context-set-result context nil)
2044 (append (if always-trust '("--always-trust"))
2045 (if recipients '("--encrypt") '("--symmetric"))
2046 (if sign '("--sign"))
2053 (car (epg-key-sub-key-list
2055 (epg-context-signers context))))
2057 (epg--args-from-sig-notations
2058 (epg-context-sig-notations context)))
2064 (car (epg-key-sub-key-list recipient)))))
2066 (if (epg-data-file plain)
2067 (list "--" (epg-data-file plain)))))
2068 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2069 (unless (eq (epg-context-protocol context) 'CMS)
2071 (epg-wait-for-status context '("BEGIN_SIGNING"))
2072 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
2073 (when (epg-data-string plain)
2074 (if (eq (process-status (epg-context-process context)) 'run)
2075 (process-send-string (epg-context-process context)
2076 (epg-data-string plain)))
2077 (if (eq (process-status (epg-context-process context)) 'run)
2078 (process-send-eof (epg-context-process context)))))
2081 (defun epg-encrypt-file (context plain recipients
2082 cipher &optional sign always-trust)
2083 "Encrypt a file PLAIN and store the result to a file CIPHER.
2084 If CIPHER is nil, it returns the result as a string.
2085 If RECIPIENTS is nil, it performs symmetric encryption."
2089 (epg-context-set-output-file context cipher)
2090 (epg-context-set-output-file context
2091 (epg--make-temp-file "epg-output")))
2092 (epg-start-encrypt context (epg-make-data-from-file plain)
2093 recipients sign always-trust)
2094 (epg-wait-for-completion context)
2096 (not (epg-context-result-for context 'sign)))
2097 (if (epg-context-result-for context 'error)
2098 (error "Sign failed: %S"
2099 (epg-context-result-for context 'error))
2100 (error "Sign failed")))
2101 (if (epg-context-result-for context 'error)
2102 (error "Encrypt failed: %S"
2103 (epg-context-result-for context 'error)))
2105 (epg-read-output context)))
2107 (epg-delete-output-file context))
2108 (epg-reset context)))
2111 (defun epg-encrypt-string (context plain recipients
2112 &optional sign always-trust)
2113 "Encrypt a string PLAIN.
2114 If RECIPIENTS is nil, it performs symmetric encryption."
2117 (epg-context-set-output-file context
2118 (epg--make-temp-file "epg-output"))
2119 (epg-start-encrypt context (epg-make-data-from-string plain)
2120 recipients sign always-trust)
2121 (epg-wait-for-completion context)
2123 (not (epg-context-result-for context 'sign)))
2124 (if (epg-context-result-for context 'error)
2125 (error "Sign failed: %S"
2126 (epg-context-result-for context 'error))
2127 (error "Sign failed")))
2128 (if (epg-context-result-for context 'error)
2129 (error "Encrypt failed: %S"
2130 (epg-context-result-for context 'error)))
2131 (epg-read-output context))
2132 (epg-delete-output-file context)
2133 (epg-reset context)))
2136 (defun epg-start-export-keys (context keys)
2137 "Initiate an export keys operation.
2139 If you use this function, you will need to wait for the completion of
2140 `epg-gpg-program' by using `epg-wait-for-completion' and call
2141 `epg-reset' to clear a temporaly output file.
2142 If you are unsure, use synchronous version of this function
2143 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
2144 (epg-context-set-operation context 'export-keys)
2145 (epg-context-set-result context nil)
2146 (epg--start context (cons "--export"
2150 (car (epg-key-sub-key-list key))))
2154 (defun epg-export-keys-to-file (context keys file)
2155 "Extract public KEYS."
2159 (epg-context-set-output-file context file)
2160 (epg-context-set-output-file context
2161 (epg--make-temp-file "epg-output")))
2162 (epg-start-export-keys context keys)
2163 (epg-wait-for-completion context)
2164 (if (epg-context-result-for context 'error)
2165 (error "Export keys failed: %S"
2166 (epg-context-result-for context 'error)))
2168 (epg-read-output context)))
2170 (epg-delete-output-file context))
2171 (epg-reset context)))
2174 (defun epg-export-keys-to-string (context keys)
2175 "Extract public KEYS and return them as a string."
2176 (epg-export-keys-to-file context keys nil))
2179 (defun epg-start-import-keys (context keys)
2180 "Initiate an import keys operation.
2181 KEYS is a data object.
2183 If you use this function, you will need to wait for the completion of
2184 `epg-gpg-program' by using `epg-wait-for-completion' and call
2185 `epg-reset' to clear a temporaly output file.
2186 If you are unsure, use synchronous version of this function
2187 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
2188 (epg-context-set-operation context 'import-keys)
2189 (epg-context-set-result context nil)
2190 (epg--start context (if (epg-data-file keys)
2191 (list "--import" "--" (epg-data-file keys))
2193 (when (epg-data-string keys)
2194 (if (eq (process-status (epg-context-process context)) 'run)
2195 (process-send-string (epg-context-process context)
2196 (epg-data-string keys)))
2197 (if (eq (process-status (epg-context-process context)) 'run)
2198 (process-send-eof (epg-context-process context)))))
2200 (defun epg--import-keys-1 (context keys)
2203 (epg-start-import-keys context keys)
2204 (epg-wait-for-completion context)
2205 (if (epg-context-result-for context 'error)
2206 (error "Import keys failed: %S"
2207 (epg-context-result-for context 'error))))
2208 (epg-reset context)))
2211 (defun epg-import-keys-from-file (context keys)
2212 "Add keys from a file KEYS."
2213 (epg--import-keys-1 context (epg-make-data-from-file keys)))
2216 (defun epg-import-keys-from-string (context keys)
2217 "Add keys from a string KEYS."
2218 (epg--import-keys-1 context (epg-make-data-from-string keys)))
2221 (defun epg-start-receive-keys (context key-id-list)
2222 "Initiate a receive key operation.
2223 KEY-ID-LIST is a list of key IDs.
2225 If you use this function, you will need to wait for the completion of
2226 `epg-gpg-program' by using `epg-wait-for-completion' and call
2227 `epg-reset' to clear a temporaly output file.
2228 If you are unsure, use synchronous version of this function
2229 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2230 (epg-context-set-operation context 'receive-keys)
2231 (epg-context-set-result context nil)
2232 (epg--start context (cons "--recv-keys" key-id-list)))
2235 (defun epg-receive-keys (context keys)
2236 "Add keys from server.
2237 KEYS is a list of key IDs"
2240 (epg-start-receive-keys context keys)
2241 (epg-wait-for-completion context)
2242 (if (epg-context-result-for context 'error)
2243 (error "Receive keys failed: %S"
2244 (epg-context-result-for context 'error))))
2245 (epg-reset context)))
2248 (defalias 'epg-import-keys-from-server 'epg-receive-keys)
2251 (defun epg-start-delete-keys (context keys &optional allow-secret)
2252 "Initiate an delete keys operation.
2254 If you use this function, you will need to wait for the completion of
2255 `epg-gpg-program' by using `epg-wait-for-completion' and call
2256 `epg-reset' to clear a temporaly output file.
2257 If you are unsure, use synchronous version of this function
2258 `epg-delete-keys' instead."
2259 (epg-context-set-operation context 'delete-keys)
2260 (epg-context-set-result context nil)
2261 (epg--start context (cons (if allow-secret
2262 "--delete-secret-key"
2267 (car (epg-key-sub-key-list key))))
2271 (defun epg-delete-keys (context keys &optional allow-secret)
2272 "Delete KEYS from the key ring."
2275 (epg-start-delete-keys context keys allow-secret)
2276 (epg-wait-for-completion context)
2277 (if (epg-context-result-for context 'error)
2278 (error "Delete keys failed: %S"
2279 (epg-context-result-for context 'error))))
2280 (epg-reset context)))
2283 (defun epg-start-sign-keys (context keys &optional local)
2284 "Initiate an sign keys operation.
2286 If you use this function, you will need to wait for the completion of
2287 `epg-gpg-program' by using `epg-wait-for-completion' and call
2288 `epg-reset' to clear a temporaly output file.
2289 If you are unsure, use synchronous version of this function
2290 `epg-sign-keys' instead."
2291 (epg-context-set-operation context 'sign-keys)
2292 (epg-context-set-result context nil)
2293 (epg--start context (cons (if local
2299 (car (epg-key-sub-key-list key))))
2301 (make-obsolete 'epg-start-sign-keys "Do not use.")
2304 (defun epg-sign-keys (context keys &optional local)
2305 "Sign KEYS from the key ring."
2308 (epg-start-sign-keys context keys local)
2309 (epg-wait-for-completion context)
2310 (if (epg-context-result-for context 'error)
2311 (error "Sign keys failed: %S"
2312 (epg-context-result-for context 'error))))
2313 (epg-reset context)))
2314 (make-obsolete 'epg-sign-keys "Do not use.")
2317 (defun epg-start-generate-key (context parameters)
2318 "Initiate a key generation.
2319 PARAMETERS specifies parameters for the key.
2321 If you use this function, you will need to wait for the completion of
2322 `epg-gpg-program' by using `epg-wait-for-completion' and call
2323 `epg-reset' to clear a temporaly output file.
2324 If you are unsure, use synchronous version of this function
2325 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2326 (epg-context-set-operation context 'generate-key)
2327 (epg-context-set-result context nil)
2328 (if (epg-data-file parameters)
2329 (epg--start context (list "--batch" "--genkey" "--"
2330 (epg-data-file parameters)))
2331 (epg--start context '("--batch" "--genkey"))
2332 (if (eq (process-status (epg-context-process context)) 'run)
2333 (process-send-string (epg-context-process context)
2334 (epg-data-string parameters)))
2335 (if (eq (process-status (epg-context-process context)) 'run)
2336 (process-send-eof (epg-context-process context)))))
2339 (defun epg-generate-key-from-file (context parameters)
2340 "Generate a new key pair.
2341 PARAMETERS is a file which tells how to create the key."
2344 (epg-start-generate-key context (epg-make-data-from-file parameters))
2345 (epg-wait-for-completion context)
2346 (if (epg-context-result-for context 'error)
2347 (error "Generate key failed: %S"
2348 (epg-context-result-for context 'error))))
2349 (epg-reset context)))
2352 (defun epg-generate-key-from-string (context parameters)
2353 "Generate a new key pair.
2354 PARAMETERS is a string which tells how to create the key."
2357 (epg-start-generate-key context (epg-make-data-from-string parameters))
2358 (epg-wait-for-completion context)
2359 (if (epg-context-result-for context 'error)
2360 (error "Generate key failed: %S"
2361 (epg-context-result-for context 'error))))
2362 (epg-reset context)))
2364 (defun epg--decode-hexstring (string)
2366 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
2367 (setq string (replace-match "\\x\\&" t nil string)
2369 (car (read-from-string (concat "\"" string "\"")))))
2371 (defun epg--decode-quotedstring (string)
2373 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2374 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
2376 (if (match-beginning 2)
2377 (setq string (replace-match "\\2" t nil string)
2379 (if (match-beginning 3)
2380 (setq string (replace-match "\\x\\3" t nil string)
2382 (setq string (replace-match "\\\\\\\\\\4" t nil string)
2383 index (+ index 3)))))
2384 (car (read-from-string (concat "\"" string "\"")))))
2386 (defun epg-dn-from-string (string)
2387 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2388 The return value is an alist mapping from types to values."
2390 (length (length string))
2391 alist type value group)
2392 (while (< index length)
2393 (if (eq index (string-match "[ \t\n\r]*" string index))
2394 (setq index (match-end 0)))
2395 (if (eq index (string-match
2396 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2398 (setq type (match-string 1 string)
2399 index (match-end 0))
2400 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2402 (setq type (match-string 1 string)
2403 index (match-end 0))))
2405 (error "Invalid type"))
2406 (if (eq index (string-match
2407 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2409 (setq index (match-end 0)
2410 value (epg--decode-quotedstring (match-string 0 string)))
2411 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
2412 (setq index (match-end 0)
2413 value (epg--decode-hexstring (match-string 1 string)))
2414 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2416 (setq index (match-end 0)
2417 value (epg--decode-quotedstring
2418 (match-string 0 string))))))
2420 (if (stringp (car (car alist)))
2421 (setcar alist (list (cons type value) (car alist)))
2422 (setcar alist (cons (cons type value) (car alist))))
2423 (if (consp (car (car alist)))
2424 (setcar alist (nreverse (car alist))))
2425 (setq alist (cons (cons type value) alist)
2428 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
2429 (setq index (match-end 0)
2430 group (eq (aref string (match-beginning 1)) ?+))))
2433 (defun epg-decode-dn (alist)
2434 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2435 Type names are resolved using `epg-dn-type-alist'."
2438 (if (stringp (car rdn))
2439 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
2441 (format "%s=%s" (cdr entry) (cdr rdn))
2442 (format "%s=%s" (car rdn) (cdr rdn))))
2443 (concat "(" (epg-decode-dn rdn) ")")))
2449 ;;; epg.el ends here