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 (put 'epg-error 'error-conditions '(epg-error error))
163 (defun epg-make-data-from-file (file)
164 "Make a data object from FILE."
165 (cons 'epg-data (vector file nil)))
167 (defun epg-make-data-from-string (string)
168 "Make a data object from STRING."
169 (cons 'epg-data (vector nil string)))
171 (defun epg-data-file (data)
172 "Return the file of DATA."
173 (unless (eq (car-safe data) 'epg-data)
174 (signal 'wrong-type-argument (list 'epg-data-p data)))
177 (defun epg-data-string (data)
178 "Return the string of DATA."
179 (unless (eq (car-safe data) 'epg-data)
180 (signal 'wrong-type-argument (list 'epg-data-p data)))
183 (defun epg-make-context (&optional protocol armor textmode include-certs
184 cipher-algorithm digest-algorithm
186 "Return a context object."
188 (vector (or protocol 'OpenPGP) armor textmode include-certs
189 cipher-algorithm digest-algorithm compress-algorithm
190 #'epg-passphrase-callback-function
192 nil nil nil nil nil nil)))
194 (defun epg-context-protocol (context)
195 "Return the protocol used within CONTEXT."
196 (unless (eq (car-safe context) 'epg-context)
197 (signal 'wrong-type-argument (list 'epg-context-p context)))
198 (aref (cdr context) 0))
200 (defun epg-context-armor (context)
201 "Return t if the output shouled be ASCII armored in CONTEXT."
202 (unless (eq (car-safe context) 'epg-context)
203 (signal 'wrong-type-argument (list 'epg-context-p context)))
204 (aref (cdr context) 1))
206 (defun epg-context-textmode (context)
207 "Return t if canonical text mode should be used in CONTEXT."
208 (unless (eq (car-safe context) 'epg-context)
209 (signal 'wrong-type-argument (list 'epg-context-p context)))
210 (aref (cdr context) 2))
212 (defun epg-context-include-certs (context)
213 "Return how many certificates should be included in an S/MIME signed
215 (unless (eq (car-safe context) 'epg-context)
216 (signal 'wrong-type-argument (list 'epg-context-p context)))
217 (aref (cdr context) 3))
219 (defun epg-context-cipher-algorithm (context)
220 "Return the cipher algorithm in CONTEXT."
221 (unless (eq (car-safe context) 'epg-context)
222 (signal 'wrong-type-argument (list 'epg-context-p context)))
223 (aref (cdr context) 4))
225 (defun epg-context-digest-algorithm (context)
226 "Return the digest algorithm in CONTEXT."
227 (unless (eq (car-safe context) 'epg-context)
228 (signal 'wrong-type-argument (list 'epg-context-p context)))
229 (aref (cdr context) 5))
231 (defun epg-context-compress-algorithm (context)
232 "Return the compress algorithm in CONTEXT."
233 (unless (eq (car-safe context) 'epg-context)
234 (signal 'wrong-type-argument (list 'epg-context-p context)))
235 (aref (cdr context) 6))
237 (defun epg-context-passphrase-callback (context)
238 "Return the function used to query passphrase."
239 (unless (eq (car-safe context) 'epg-context)
240 (signal 'wrong-type-argument (list 'epg-context-p context)))
241 (aref (cdr context) 7))
243 (defun epg-context-progress-callback (context)
244 "Return the function which handles progress update."
245 (unless (eq (car-safe context) 'epg-context)
246 (signal 'wrong-type-argument (list 'epg-context-p context)))
247 (aref (cdr context) 8))
249 (defun epg-context-signers (context)
250 "Return the list of key-id for singning."
251 (unless (eq (car-safe context) 'epg-context)
252 (signal 'wrong-type-argument (list 'epg-context-p context)))
253 (aref (cdr context) 9))
255 (defun epg-context-sig-notations (context)
256 "Return the list of notations for singning."
257 (unless (eq (car-safe context) 'epg-context)
258 (signal 'wrong-type-argument (list 'epg-context-p context)))
259 (aref (cdr context) 10))
261 (defun epg-context-process (context)
262 "Return the process object of `epg-gpg-program'.
263 This function is for internal use only."
264 (unless (eq (car-safe context) 'epg-context)
265 (signal 'wrong-type-argument (list 'epg-context-p context)))
266 (aref (cdr context) 11))
268 (defun epg-context-output-file (context)
269 "Return the output file of `epg-gpg-program'.
270 This function is for internal use only."
271 (unless (eq (car-safe context) 'epg-context)
272 (signal 'wrong-type-argument (list 'epg-context-p context)))
273 (aref (cdr context) 12))
275 (defun epg-context-result (context)
276 "Return the result of the previous cryptographic operation."
277 (unless (eq (car-safe context) 'epg-context)
278 (signal 'wrong-type-argument (list 'epg-context-p context)))
279 (aref (cdr context) 13))
281 (defun epg-context-operation (context)
282 "Return the name of the current cryptographic operation."
283 (unless (eq (car-safe context) 'epg-context)
284 (signal 'wrong-type-argument (list 'epg-context-p context)))
285 (aref (cdr context) 14))
287 (defun epg-context-set-protocol (context protocol)
288 "Set the protocol used within CONTEXT."
289 (unless (eq (car-safe context) 'epg-context)
290 (signal 'wrong-type-argument (list 'epg-context-p context)))
291 (aset (cdr context) 0 protocol))
293 (defun epg-context-set-armor (context armor)
294 "Specify if the output shouled be ASCII armored in CONTEXT."
295 (unless (eq (car-safe context) 'epg-context)
296 (signal 'wrong-type-argument (list 'epg-context-p context)))
297 (aset (cdr context) 1 armor))
299 (defun epg-context-set-textmode (context textmode)
300 "Specify if canonical text mode should be used in CONTEXT."
301 (unless (eq (car-safe context) 'epg-context)
302 (signal 'wrong-type-argument (list 'epg-context-p context)))
303 (aset (cdr context) 2 textmode))
305 (defun epg-context-set-include-certs (context include-certs)
306 "Set how many certificates should be included in an S/MIME signed message."
307 (unless (eq (car-safe context) 'epg-context)
308 (signal 'wrong-type-argument (list 'epg-context-p context)))
309 (aset (cdr context) 3 include-certs))
311 (defun epg-context-set-cipher-algorithm (context cipher-algorithm)
312 "Set the cipher algorithm in CONTEXT."
313 (unless (eq (car-safe context) 'epg-context)
314 (signal 'wrong-type-argument (list 'epg-context-p context)))
315 (aset (cdr context) 4 cipher-algorithm))
317 (defun epg-context-set-digest-algorithm (context digest-algorithm)
318 "Set the digest algorithm in CONTEXT."
319 (unless (eq (car-safe context) 'epg-context)
320 (signal 'wrong-type-argument (list 'epg-context-p context)))
321 (aset (cdr context) 5 digest-algorithm))
323 (defun epg-context-set-compress-algorithm (context compress-algorithm)
324 "Set the compress algorithm in CONTEXT."
325 (unless (eq (car-safe context) 'epg-context)
326 (signal 'wrong-type-argument (list 'epg-context-p context)))
327 (aset (cdr context) 6 compress-algorithm))
329 (defun epg-context-set-passphrase-callback (context
331 "Set the function used to query passphrase."
332 (unless (eq (car-safe context) 'epg-context)
333 (signal 'wrong-type-argument (list 'epg-context-p context)))
334 (aset (cdr context) 7 passphrase-callback))
336 (defun epg-context-set-progress-callback (context progress-callback)
337 "Set the function which handles progress update."
338 (unless (eq (car-safe context) 'epg-context)
339 (signal 'wrong-type-argument (list 'epg-context-p context)))
340 (aset (cdr context) 8 progress-callback))
342 (defun epg-context-set-signers (context signers)
343 "Set the list of key-id for singning."
344 (unless (eq (car-safe context) 'epg-context)
345 (signal 'wrong-type-argument (list 'epg-context-p context)))
346 (aset (cdr context) 9 signers))
348 (defun epg-context-set-sig-notations (context notations)
349 "Set the list of notations for singning."
350 (unless (eq (car-safe context) 'epg-context)
351 (signal 'wrong-type-argument (list 'epg-context-p context)))
352 (aset (cdr context) 10 notations))
354 (defun epg-context-set-process (context process)
355 "Set the process object of `epg-gpg-program'.
356 This function is for internal use only."
357 (unless (eq (car-safe context) 'epg-context)
358 (signal 'wrong-type-argument (list 'epg-context-p context)))
359 (aset (cdr context) 11 process))
361 (defun epg-context-set-output-file (context output-file)
362 "Set the output file of `epg-gpg-program'.
363 This function is for internal use only."
364 (unless (eq (car-safe context) 'epg-context)
365 (signal 'wrong-type-argument (list 'epg-context-p context)))
366 (aset (cdr context) 12 output-file))
368 (defun epg-context-set-result (context result)
369 "Set the result of the previous cryptographic operation."
370 (unless (eq (car-safe context) 'epg-context)
371 (signal 'wrong-type-argument (list 'epg-context-p context)))
372 (aset (cdr context) 13 result))
374 (defun epg-context-set-operation (context operation)
375 "Set the name of the current cryptographic operation."
376 (unless (eq (car-safe context) 'epg-context)
377 (signal 'wrong-type-argument (list 'epg-context-p context)))
378 (aset (cdr context) 14 operation))
380 (defun epg-make-signature (status &optional key-id)
381 "Return a signature object."
382 (cons 'epg-signature (vector status key-id nil nil nil nil nil nil nil nil
385 (defun epg-signature-status (signature)
386 "Return the status code of SIGNATURE."
387 (unless (eq (car-safe signature) 'epg-signature)
388 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
389 (aref (cdr signature) 0))
391 (defun epg-signature-key-id (signature)
392 "Return the key-id of SIGNATURE."
393 (unless (eq (car-safe signature) 'epg-signature)
394 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
395 (aref (cdr signature) 1))
397 (defun epg-signature-validity (signature)
398 "Return the validity of SIGNATURE."
399 (unless (eq (car-safe signature) 'epg-signature)
400 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
401 (aref (cdr signature) 2))
403 (defun epg-signature-fingerprint (signature)
404 "Return the fingerprint of SIGNATURE."
405 (unless (eq (car-safe signature) 'epg-signature)
406 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
407 (aref (cdr signature) 3))
409 (defun epg-signature-creation-time (signature)
410 "Return the creation time of SIGNATURE."
411 (unless (eq (car-safe signature) 'epg-signature)
412 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
413 (aref (cdr signature) 4))
415 (defun epg-signature-expiration-time (signature)
416 "Return the expiration time of SIGNATURE."
417 (unless (eq (car-safe signature) 'epg-signature)
418 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
419 (aref (cdr signature) 5))
421 (defun epg-signature-pubkey-algorithm (signature)
422 "Return the public key algorithm of SIGNATURE."
423 (unless (eq (car-safe signature) 'epg-signature)
424 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
425 (aref (cdr signature) 6))
427 (defun epg-signature-digest-algorithm (signature)
428 "Return the digest algorithm of SIGNATURE."
429 (unless (eq (car-safe signature) 'epg-signature)
430 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
431 (aref (cdr signature) 7))
433 (defun epg-signature-class (signature)
434 "Return the class of SIGNATURE."
435 (unless (eq (car-safe signature) 'epg-signature)
436 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
437 (aref (cdr signature) 8))
439 (defun epg-signature-version (signature)
440 "Return the version of SIGNATURE."
441 (unless (eq (car-safe signature) 'epg-signature)
442 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
443 (aref (cdr signature) 9))
445 (defun epg-sig-notations (signature)
446 "Return the list of notations of SIGNATURE."
447 (unless (eq (car-safe signature) 'epg-signature)
448 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
449 (aref (cdr signature) 10))
451 (defun epg-signature-set-status (signature status)
452 "Set the status code of SIGNATURE."
453 (unless (eq (car-safe signature) 'epg-signature)
454 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
455 (aset (cdr signature) 0 status))
457 (defun epg-signature-set-key-id (signature key-id)
458 "Set the key-id of SIGNATURE."
459 (unless (eq (car-safe signature) 'epg-signature)
460 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
461 (aset (cdr signature) 1 key-id))
463 (defun epg-signature-set-validity (signature validity)
464 "Set the validity of SIGNATURE."
465 (unless (eq (car-safe signature) 'epg-signature)
466 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
467 (aset (cdr signature) 2 validity))
469 (defun epg-signature-set-fingerprint (signature fingerprint)
470 "Set the fingerprint of SIGNATURE."
471 (unless (eq (car-safe signature) 'epg-signature)
472 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
473 (aset (cdr signature) 3 fingerprint))
475 (defun epg-signature-set-creation-time (signature creation-time)
476 "Set the creation time of SIGNATURE."
477 (unless (eq (car-safe signature) 'epg-signature)
478 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
479 (aset (cdr signature) 4 creation-time))
481 (defun epg-signature-set-expiration-time (signature expiration-time)
482 "Set the expiration time of SIGNATURE."
483 (unless (eq (car-safe signature) 'epg-signature)
484 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
485 (aset (cdr signature) 5 expiration-time))
487 (defun epg-signature-set-pubkey-algorithm (signature pubkey-algorithm)
488 "Set the public key algorithm of SIGNATURE."
489 (unless (eq (car-safe signature) 'epg-signature)
490 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
491 (aset (cdr signature) 6 pubkey-algorithm))
493 (defun epg-signature-set-digest-algorithm (signature digest-algorithm)
494 "Set the digest algorithm of SIGNATURE."
495 (unless (eq (car-safe signature) 'epg-signature)
496 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
497 (aset (cdr signature) 7 digest-algorithm))
499 (defun epg-signature-set-class (signature class)
500 "Set the class of SIGNATURE."
501 (unless (eq (car-safe signature) 'epg-signature)
502 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
503 (aset (cdr signature) 8 class))
505 (defun epg-signature-set-version (signature version)
506 "Set the version of SIGNATURE."
507 (unless (eq (car-safe signature) 'epg-signature)
508 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
509 (aset (cdr signature) 9 version))
511 (defun epg-signature-set-notations (signature notations)
512 "Set the list of notations of SIGNATURE."
513 (unless (eq (car-safe signature) 'epg-signature)
514 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
515 (aset (cdr signature) 10 notations))
517 (defun epg-make-new-signature (type pubkey-algorithm digest-algorithm
518 class creation-time fingerprint)
519 "Return a new signature object."
520 (cons 'epg-new-signature (vector type pubkey-algorithm digest-algorithm
521 class creation-time fingerprint)))
523 (defun epg-new-signature-type (new-signature)
524 "Return the type of NEW-SIGNATURE."
525 (unless (eq (car-safe new-signature) 'epg-new-signature)
526 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
527 (aref (cdr new-signature) 0))
529 (defun epg-new-signature-pubkey-algorithm (new-signature)
530 "Return the public key algorithm of NEW-SIGNATURE."
531 (unless (eq (car-safe new-signature) 'epg-new-signature)
532 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
533 (aref (cdr new-signature) 1))
535 (defun epg-new-signature-digest-algorithm (new-signature)
536 "Return the digest algorithm of NEW-SIGNATURE."
537 (unless (eq (car-safe new-signature) 'epg-new-signature)
538 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
539 (aref (cdr new-signature) 2))
541 (defun epg-new-signature-class (new-signature)
542 "Return the class of NEW-SIGNATURE."
543 (unless (eq (car-safe new-signature) 'epg-new-signature)
544 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
545 (aref (cdr new-signature) 3))
547 (defun epg-new-signature-creation-time (new-signature)
548 "Return the creation time of NEW-SIGNATURE."
549 (unless (eq (car-safe new-signature) 'epg-new-signature)
550 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
551 (aref (cdr new-signature) 4))
553 (defun epg-new-signature-fingerprint (new-signature)
554 "Return the fingerprint of NEW-SIGNATURE."
555 (unless (eq (car-safe new-signature) 'epg-new-signature)
556 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
557 (aref (cdr new-signature) 5))
559 (defun epg-make-key (owner-trust)
560 "Return a key object."
561 (cons 'epg-key (vector owner-trust nil nil)))
563 (defun epg-key-owner-trust (key)
564 "Return the owner trust of KEY."
565 (unless (eq (car-safe key) 'epg-key)
566 (signal 'wrong-type-argument (list 'epg-key-p key)))
569 (defun epg-key-sub-key-list (key)
570 "Return the sub key list of KEY."
571 (unless (eq (car-safe key) 'epg-key)
572 (signal 'wrong-type-argument (list 'epg-key-p key)))
575 (defun epg-key-user-id-list (key)
576 "Return the user ID list of KEY."
577 (unless (eq (car-safe key) 'epg-key)
578 (signal 'wrong-type-argument (list 'epg-key-p key)))
581 (defun epg-key-set-sub-key-list (key sub-key-list)
582 "Set the sub key list of KEY."
583 (unless (eq (car-safe key) 'epg-key)
584 (signal 'wrong-type-argument (list 'epg-key-p key)))
585 (aset (cdr key) 1 sub-key-list))
587 (defun epg-key-set-user-id-list (key user-id-list)
588 "Set the user ID list of KEY."
589 (unless (eq (car-safe key) 'epg-key)
590 (signal 'wrong-type-argument (list 'epg-key-p key)))
591 (aset (cdr key) 2 user-id-list))
593 (defun epg-make-sub-key (validity capability secret-p algorithm length id
594 creation-time expiration-time)
595 "Return a sub key object."
597 (vector validity capability secret-p algorithm length id creation-time
598 expiration-time nil)))
600 (defun epg-sub-key-validity (sub-key)
601 "Return the validity of SUB-KEY."
602 (unless (eq (car-safe sub-key) 'epg-sub-key)
603 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
604 (aref (cdr sub-key) 0))
606 (defun epg-sub-key-capability (sub-key)
607 "Return the capability of SUB-KEY."
608 (unless (eq (car-safe sub-key) 'epg-sub-key)
609 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
610 (aref (cdr sub-key) 1))
612 (defun epg-sub-key-secret-p (sub-key)
613 "Return non-nil if SUB-KEY is a secret key."
614 (unless (eq (car-safe sub-key) 'epg-sub-key)
615 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
616 (aref (cdr sub-key) 2))
618 (defun epg-sub-key-algorithm (sub-key)
619 "Return the algorithm of SUB-KEY."
620 (unless (eq (car-safe sub-key) 'epg-sub-key)
621 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
622 (aref (cdr sub-key) 3))
624 (defun epg-sub-key-length (sub-key)
625 "Return the length of SUB-KEY."
626 (unless (eq (car-safe sub-key) 'epg-sub-key)
627 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
628 (aref (cdr sub-key) 4))
630 (defun epg-sub-key-id (sub-key)
631 "Return the ID of SUB-KEY."
632 (unless (eq (car-safe sub-key) 'epg-sub-key)
633 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
634 (aref (cdr sub-key) 5))
636 (defun epg-sub-key-creation-time (sub-key)
637 "Return the creation time of SUB-KEY."
638 (unless (eq (car-safe sub-key) 'epg-sub-key)
639 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
640 (aref (cdr sub-key) 6))
642 (defun epg-sub-key-expiration-time (sub-key)
643 "Return the expiration time of SUB-KEY."
644 (unless (eq (car-safe sub-key) 'epg-sub-key)
645 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
646 (aref (cdr sub-key) 7))
648 (defun epg-sub-key-fingerprint (sub-key)
649 "Return the fingerprint of SUB-KEY."
650 (unless (eq (car-safe sub-key) 'epg-sub-key)
651 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
652 (aref (cdr sub-key) 8))
654 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
655 "Set the fingerprint of SUB-KEY.
656 This function is for internal use only."
657 (unless (eq (car-safe sub-key) 'epg-sub-key)
658 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
659 (aset (cdr sub-key) 8 fingerprint))
661 (defun epg-make-user-id (validity string)
662 "Return a user ID object."
663 (cons 'epg-user-id (vector validity string nil)))
665 (defun epg-user-id-validity (user-id)
666 "Return the validity of USER-ID."
667 (unless (eq (car-safe user-id) 'epg-user-id)
668 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
669 (aref (cdr user-id) 0))
671 (defun epg-user-id-string (user-id)
672 "Return the name of USER-ID."
673 (unless (eq (car-safe user-id) 'epg-user-id)
674 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
675 (aref (cdr user-id) 1))
677 (defun epg-user-id-signature-list (user-id)
678 "Return the signature list of USER-ID."
679 (unless (eq (car-safe user-id) 'epg-user-id)
680 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
681 (aref (cdr user-id) 2))
683 (defun epg-user-id-set-signature-list (user-id signature-list)
684 "Set the signature list of USER-ID."
685 (unless (eq (car-safe user-id) 'epg-user-id)
686 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
687 (aset (cdr user-id) 2 signature-list))
689 (defun epg-make-key-signature (validity pubkey-algorithm key-id creation-time
690 expiration-time user-id class
692 "Return a key signature object."
693 (cons 'epg-key-signature
694 (vector validity pubkey-algorithm key-id creation-time expiration-time
695 user-id class exportable-p)))
697 (defun epg-key-signature-validity (key-signature)
698 "Return the validity of KEY-SIGNATURE."
699 (unless (eq (car-safe key-signature) 'epg-key-signature)
700 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
701 (aref (cdr key-signature) 0))
703 (defun epg-key-signature-pubkey-algorithm (key-signature)
704 "Return the public key algorithm of KEY-SIGNATURE."
705 (unless (eq (car-safe key-signature) 'epg-key-signature)
706 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
707 (aref (cdr key-signature) 1))
709 (defun epg-key-signature-key-id (key-signature)
710 "Return the key-id of KEY-SIGNATURE."
711 (unless (eq (car-safe key-signature) 'epg-key-signature)
712 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
713 (aref (cdr key-signature) 2))
715 (defun epg-key-signature-creation-time (key-signature)
716 "Return the creation time of KEY-SIGNATURE."
717 (unless (eq (car-safe key-signature) 'epg-key-signature)
718 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
719 (aref (cdr key-signature) 3))
721 (defun epg-key-signature-expiration-time (key-signature)
722 "Return the expiration time of KEY-SIGNATURE."
723 (unless (eq (car-safe key-signature) 'epg-key-signature)
724 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
725 (aref (cdr key-signature) 4))
727 (defun epg-key-signature-user-id (key-signature)
728 "Return the user-id of KEY-SIGNATURE."
729 (unless (eq (car-safe key-signature) 'epg-key-signature)
730 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
731 (aref (cdr key-signature) 5))
733 (defun epg-key-signature-class (key-signature)
734 "Return the class of KEY-SIGNATURE."
735 (unless (eq (car-safe key-signature) 'epg-key-signature)
736 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
737 (aref (cdr key-signature) 6))
739 (defun epg-key-signature-exportable-p (key-signature)
740 "Return t if KEY-SIGNATURE is exportable."
741 (unless (eq (car-safe key-signature) 'epg-key-signature)
742 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
743 (aref (cdr key-signature) 7))
745 (defun epg-make-sig-notation (name value &optional human-readable
747 "Return a notation object."
748 (cons 'epg-sig-notation (vector name value human-readable critical)))
750 (defun epg-sig-notation-name (sig-notation)
751 "Return the name of SIG-NOTATION."
752 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
753 (signal 'wrong-type-argument (list 'epg-sig-notation-p
755 (aref (cdr sig-notation) 0))
757 (defun epg-sig-notation-value (sig-notation)
758 "Return the value of SIG-NOTATION."
759 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
760 (signal 'wrong-type-argument (list 'epg-sig-notation-p
762 (aref (cdr sig-notation) 1))
764 (defun epg-sig-notation-human-readable (sig-notation)
765 "Return the human-readable of SIG-NOTATION."
766 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
767 (signal 'wrong-type-argument (list 'epg-sig-notation-p
769 (aref (cdr sig-notation) 2))
771 (defun epg-sig-notation-critical (sig-notation)
772 "Return the critical of SIG-NOTATION."
773 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
774 (signal 'wrong-type-argument (list 'epg-sig-notation-p
776 (aref (cdr sig-notation) 3))
778 (defun epg-sig-notation-set-value (sig-notation value)
779 "Set the value of SIG-NOTATION."
780 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
781 (signal 'wrong-type-argument (list 'epg-sig-notation-p
783 (aset (cdr sig-notation) 1 value))
785 (defun epg-make-import-status (fingerprint &optional reason new user-id
786 signature sub-key secret)
787 "Return a import status object."
788 (cons 'epg-import-status (vector fingerprint reason new user-id signature
791 (defun epg-import-status-fingerprint (import-status)
792 "Return the fingerprint of the key that was considered."
793 (unless (eq (car-safe import-status) 'epg-import-status)
794 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
795 (aref (cdr import-status) 0))
797 (defun epg-import-status-reason (import-status)
798 "Return the reason code for import failure."
799 (unless (eq (car-safe import-status) 'epg-import-status)
800 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
801 (aref (cdr import-status) 1))
803 (defun epg-import-status-new (import-status)
804 "Return t if the imported key was new."
805 (unless (eq (car-safe import-status) 'epg-import-status)
806 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
807 (aref (cdr import-status) 2))
809 (defun epg-import-status-user-id (import-status)
810 "Return t if the imported key contained new user IDs."
811 (unless (eq (car-safe import-status) 'epg-import-status)
812 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
813 (aref (cdr import-status) 3))
815 (defun epg-import-status-signature (import-status)
816 "Return t if the imported key contained new signatures."
817 (unless (eq (car-safe import-status) 'epg-import-status)
818 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
819 (aref (cdr import-status) 4))
821 (defun epg-import-status-sub-key (import-status)
822 "Return t if the imported key contained new sub keys."
823 (unless (eq (car-safe import-status) 'epg-import-status)
824 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
825 (aref (cdr import-status) 5))
827 (defun epg-import-status-secret (import-status)
828 "Return t if the imported key contained a secret key."
829 (unless (eq (car-safe import-status) 'epg-import-status)
830 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
831 (aref (cdr import-status) 6))
833 (defun epg-make-import-result (considered no-user-id imported imported-rsa
834 unchanged new-user-ids new-sub-keys
835 new-signatures new-revocations
836 secret-read secret-imported
837 secret-unchanged not-imported
839 "Return a import result object."
840 (cons 'epg-import-result (vector considered no-user-id imported imported-rsa
841 unchanged new-user-ids new-sub-keys
842 new-signatures new-revocations secret-read
843 secret-imported secret-unchanged
844 not-imported imports)))
846 (defun epg-import-result-considered (import-result)
847 "Return the total number of considered keys."
848 (unless (eq (car-safe import-result) 'epg-import-result)
849 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
850 (aref (cdr import-result) 0))
852 (defun epg-import-result-no-user-id (import-result)
853 "Return the number of keys without user ID."
854 (unless (eq (car-safe import-result) 'epg-import-result)
855 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
856 (aref (cdr import-result) 1))
858 (defun epg-import-result-imported (import-result)
859 "Return the number of imported keys."
860 (unless (eq (car-safe import-result) 'epg-import-result)
861 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
862 (aref (cdr import-result) 2))
864 (defun epg-import-result-imported-rsa (import-result)
865 "Return the number of imported RSA keys."
866 (unless (eq (car-safe import-result) 'epg-import-result)
867 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
868 (aref (cdr import-result) 3))
870 (defun epg-import-result-unchanged (import-result)
871 "Return the number of unchanged keys."
872 (unless (eq (car-safe import-result) 'epg-import-result)
873 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
874 (aref (cdr import-result) 4))
876 (defun epg-import-result-new-user-ids (import-result)
877 "Return the number of new user IDs."
878 (unless (eq (car-safe import-result) 'epg-import-result)
879 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
880 (aref (cdr import-result) 5))
882 (defun epg-import-result-new-sub-keys (import-result)
883 "Return the number of new sub keys."
884 (unless (eq (car-safe import-result) 'epg-import-result)
885 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
886 (aref (cdr import-result) 6))
888 (defun epg-import-result-new-signatures (import-result)
889 "Return the number of new signatures."
890 (unless (eq (car-safe import-result) 'epg-import-result)
891 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
892 (aref (cdr import-result) 7))
894 (defun epg-import-result-new-revocations (import-result)
895 "Return the number of new revocations."
896 (unless (eq (car-safe import-result) 'epg-import-result)
897 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
898 (aref (cdr import-result) 8))
900 (defun epg-import-result-secret-read (import-result)
901 "Return the total number of secret keys read."
902 (unless (eq (car-safe import-result) 'epg-import-result)
903 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
904 (aref (cdr import-result) 9))
906 (defun epg-import-result-secret-imported (import-result)
907 "Return the number of imported secret keys."
908 (unless (eq (car-safe import-result) 'epg-import-result)
909 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
910 (aref (cdr import-result) 10))
912 (defun epg-import-result-secret-unchanged (import-result)
913 "Return the number of unchanged secret keys."
914 (unless (eq (car-safe import-result) 'epg-import-result)
915 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
916 (aref (cdr import-result) 11))
918 (defun epg-import-result-not-imported (import-result)
919 "Return the number of keys not imported."
920 (unless (eq (car-safe import-result) 'epg-import-result)
921 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
922 (aref (cdr import-result) 12))
924 (defun epg-import-result-imports (import-result)
925 "Return the list of `epg-import-status' objects."
926 (unless (eq (car-safe import-result) 'epg-import-result)
927 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
928 (aref (cdr import-result) 13))
930 (defun epg-context-result-for (context name)
931 "Return the result of CONTEXT associated with NAME."
932 (cdr (assq name (epg-context-result context))))
934 (defun epg-context-set-result-for (context name value)
935 "Set the result of CONTEXT associated with NAME to VALUE."
936 (let* ((result (epg-context-result context))
937 (entry (assq name result)))
940 (epg-context-set-result context (cons (cons name value) result)))))
942 (defun epg-signature-to-string (signature)
943 "Convert SIGNATURE to a human readable string."
944 (let ((user-id (cdr (assoc (epg-signature-key-id signature)
945 epg-user-id-alist))))
947 (cond ((eq (epg-signature-status signature) 'good)
948 "Good signature from ")
949 ((eq (epg-signature-status signature) 'bad)
950 "Bad signature from ")
951 ((eq (epg-signature-status signature) 'expired)
952 "Expired signature from ")
953 ((eq (epg-signature-status signature) 'expired-key)
954 "Signature made by expired key ")
955 ((eq (epg-signature-status signature) 'revoked-key)
956 "Signature made by revoked key ")
957 ((eq (epg-signature-status signature) 'no-pubkey)
958 "No public key for "))
959 (epg-signature-key-id signature)
962 (if (stringp user-id)
964 (epg-decode-dn user-id)))
966 (if (epg-signature-validity signature)
967 (format " (trust %s)" (epg-signature-validity signature))
970 (defun epg-verify-result-to-string (verify-result)
971 "Convert VERIFY-RESULT to a human readable string."
972 (mapconcat #'epg-signature-to-string verify-result "\n"))
974 (defun epg-new-signature-to-string (new-signature)
975 "Convert NEW-SIGNATURE to a human readable string."
977 (cond ((eq (epg-new-signature-type new-signature) 'detached)
978 "Detached signature ")
979 ((eq (epg-new-signature-type new-signature) 'clear)
980 "Cleartext signature ")
983 (cdr (assq (epg-new-signature-pubkey-algorithm new-signature)
984 epg-pubkey-algorithm-alist))
986 (cdr (assq (epg-new-signature-digest-algorithm new-signature)
987 epg-digest-algorithm-alist))
989 (format "%02X " (epg-new-signature-class new-signature))
990 (epg-new-signature-fingerprint new-signature)))
992 (defun epg-import-result-to-string (import-result)
993 "Convert IMPORT-RESULT to a human readable string."
994 (concat (format "Total number processed: %d\n"
995 (epg-import-result-considered import-result))
996 (if (> (epg-import-result-not-imported import-result) 0)
997 (format " skipped new keys: %d\n"
998 (epg-import-result-not-imported import-result)))
999 (if (> (epg-import-result-no-user-id import-result) 0)
1000 (format " w/o user IDs: %d\n"
1001 (epg-import-result-no-user-id import-result)))
1002 (if (> (epg-import-result-imported import-result) 0)
1003 (concat (format " imported: %d"
1004 (epg-import-result-imported import-result))
1005 (if (> (epg-import-result-imported-rsa import-result) 0)
1006 (format " (RSA: %d)"
1007 (epg-import-result-imported-rsa
1010 (if (> (epg-import-result-unchanged import-result) 0)
1011 (format " unchanged: %d\n"
1012 (epg-import-result-unchanged import-result)))
1013 (if (> (epg-import-result-new-user-ids import-result) 0)
1014 (format " new user IDs: %d\n"
1015 (epg-import-result-new-user-ids import-result)))
1016 (if (> (epg-import-result-new-sub-keys import-result) 0)
1017 (format " new subkeys: %d\n"
1018 (epg-import-result-new-sub-keys import-result)))
1019 (if (> (epg-import-result-new-signatures import-result) 0)
1020 (format " new signatures: %d\n"
1021 (epg-import-result-new-signatures import-result)))
1022 (if (> (epg-import-result-new-revocations import-result) 0)
1023 (format " new key revocations: %d\n"
1024 (epg-import-result-new-revocations import-result)))
1025 (if (> (epg-import-result-secret-read import-result) 0)
1026 (format " secret keys read: %d\n"
1027 (epg-import-result-secret-read import-result)))
1028 (if (> (epg-import-result-secret-imported import-result) 0)
1029 (format " secret keys imported: %d\n"
1030 (epg-import-result-secret-imported import-result)))
1031 (if (> (epg-import-result-secret-unchanged import-result) 0)
1032 (format " secret keys unchanged: %d\n"
1033 (epg-import-result-secret-unchanged import-result)))))
1035 (defun epg--start (context args)
1036 "Start `epg-gpg-program' in a subprocess with given ARGS."
1037 (if (and (epg-context-process context)
1038 (eq (process-status (epg-context-process context)) 'run))
1039 (error "%s is already running in this context"
1040 (if (eq (epg-context-protocol context) 'CMS)
1043 (let* ((args (append (list "--no-tty"
1046 (if (epg-context-progress-callback context)
1047 (list "--enable-progress-filter"))
1048 (if epg-gpg-home-directory
1049 (list "--homedir" epg-gpg-home-directory))
1050 (unless (eq (epg-context-protocol context) 'CMS)
1051 (list "--command-fd" "0"))
1052 (if (epg-context-armor context) '("--armor"))
1053 (if (epg-context-textmode context) '("--textmode"))
1054 (if (epg-context-output-file context)
1055 (list "--output" (epg-context-output-file context)))
1057 (coding-system-for-write 'binary)
1058 process-connection-type
1059 (orig-mode (default-file-modes))
1060 (buffer (generate-new-buffer " *epg*"))
1064 (unless epg-debug-buffer
1065 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
1066 (set-buffer epg-debug-buffer)
1067 (goto-char (point-max))
1068 (insert (format "%s %s\n"
1069 (if (eq (epg-context-protocol context) 'CMS)
1072 (mapconcat #'identity args " ")))))
1073 (with-current-buffer buffer
1074 (make-local-variable 'epg-read-point)
1075 (setq epg-read-point (point-min))
1076 (make-local-variable 'epg-process-filter-running)
1077 (setq epg-process-filter-running nil)
1078 (make-local-variable 'epg-pending-status-list)
1079 (setq epg-pending-status-list nil)
1080 (make-local-variable 'epg-key-id)
1081 (setq epg-key-id nil)
1082 (make-local-variable 'epg-context)
1083 (setq epg-context context))
1086 (set-default-file-modes 448)
1088 (apply #'start-process "epg" buffer
1089 (if (eq (epg-context-protocol context) 'CMS)
1093 (set-default-file-modes orig-mode))
1094 (set-process-filter process #'epg--process-filter)
1095 (epg-context-set-process context process)))
1097 (defun epg--process-filter (process input)
1100 (unless epg-debug-buffer
1101 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
1102 (set-buffer epg-debug-buffer)
1103 (goto-char (point-max))
1105 (if (buffer-live-p (process-buffer process))
1107 (set-buffer (process-buffer process))
1108 (goto-char (point-max))
1110 (unless epg-process-filter-running
1113 (setq epg-process-filter-running t)
1114 (goto-char epg-read-point)
1116 (while (looking-at ".*\n") ;the input line finished
1117 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
1118 (let* ((status (match-string 1))
1119 (string (match-string 2))
1120 (symbol (intern-soft (concat "epg--status-"
1122 (if (member status epg-pending-status-list)
1123 (setq epg-pending-status-list nil))
1126 (funcall symbol epg-context string))))
1128 (setq epg-read-point (point))))
1129 (setq epg-process-filter-running nil))))))
1131 (defun epg-read-output (context)
1132 "Read the output file CONTEXT and return the content as a string."
1134 (if (fboundp 'set-buffer-multibyte)
1135 (set-buffer-multibyte nil))
1136 (if (file-exists-p (epg-context-output-file context))
1137 (let ((coding-system-for-read 'binary))
1138 (insert-file-contents (epg-context-output-file context))
1141 (defun epg-wait-for-status (context status-list)
1142 "Wait until one of elements in STATUS-LIST arrives."
1143 (with-current-buffer (process-buffer (epg-context-process context))
1144 (setq epg-pending-status-list status-list)
1145 (while (and (eq (process-status (epg-context-process context)) 'run)
1146 epg-pending-status-list)
1147 (accept-process-output (epg-context-process context) 1))))
1149 (defun epg-wait-for-completion (context)
1150 "Wait until the `epg-gpg-program' process completes."
1151 (while (eq (process-status (epg-context-process context)) 'run)
1152 (accept-process-output (epg-context-process context) 1)))
1154 (defun epg-reset (context)
1155 "Reset the CONTEXT."
1156 (if (and (epg-context-process context)
1157 (buffer-live-p (process-buffer (epg-context-process context))))
1158 (kill-buffer (process-buffer (epg-context-process context))))
1159 (epg-context-set-process context nil))
1161 (defun epg-delete-output-file (context)
1162 "Delete the output file of CONTEXT."
1163 (if (and (epg-context-output-file context)
1164 (file-exists-p (epg-context-output-file context)))
1165 (delete-file (epg-context-output-file context))))
1167 (defun epg--status-USERID_HINT (context string)
1168 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1169 (let* ((key-id (match-string 1 string))
1170 (user-id (match-string 2 string))
1171 (entry (assoc key-id epg-user-id-alist)))
1173 (setcdr entry user-id)
1174 (setq epg-user-id-alist (cons (cons key-id user-id)
1175 epg-user-id-alist))))))
1177 (defun epg--status-NEED_PASSPHRASE (context string)
1178 (if (string-match "\\`\\([^ ]+\\)" string)
1179 (setq epg-key-id (match-string 1 string))))
1181 (defun epg--status-NEED_PASSPHRASE_SYM (context string)
1182 (setq epg-key-id 'SYM))
1184 (defun epg--status-NEED_PASSPHRASE_PIN (context string)
1185 (setq epg-key-id 'PIN))
1187 (defun epg--status-GET_HIDDEN (context string)
1188 (when (and epg-key-id
1189 (string-match "\\`passphrase\\." string))
1190 (unless (epg-context-passphrase-callback context)
1191 (error "passphrase-callback not set"))
1194 passphrase-with-new-line
1195 encoded-passphrase-with-new-line)
1201 (if (consp (epg-context-passphrase-callback context))
1202 (car (epg-context-passphrase-callback context))
1203 (epg-context-passphrase-callback context))
1206 (if (consp (epg-context-passphrase-callback context))
1207 (cdr (epg-context-passphrase-callback context)))))
1209 (setq passphrase-with-new-line (concat passphrase "\n"))
1210 (epg--clear-string passphrase)
1211 (setq passphrase nil)
1212 (if epg-passphrase-coding-system
1214 (setq encoded-passphrase-with-new-line
1215 (encode-coding-string
1216 passphrase-with-new-line
1217 epg-passphrase-coding-system))
1218 (epg--clear-string passphrase-with-new-line)
1219 (setq passphrase-with-new-line nil))
1220 (setq encoded-passphrase-with-new-line
1221 passphrase-with-new-line
1222 passphrase-with-new-line nil))
1223 (process-send-string (epg-context-process context)
1224 encoded-passphrase-with-new-line)))
1226 (epg-context-set-result-for
1229 (epg-context-result-for context 'error)))
1230 (delete-process (epg-context-process context))))
1232 (epg--clear-string passphrase))
1233 (if passphrase-with-new-line
1234 (epg--clear-string passphrase-with-new-line))
1235 (if encoded-passphrase-with-new-line
1236 (epg--clear-string encoded-passphrase-with-new-line))))))
1238 (defun epg--status-GET_BOOL (context string)
1239 (let ((entry (assoc string epg-prompt-alist))
1242 (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
1243 (process-send-string (epg-context-process context) "y\n")
1244 (process-send-string (epg-context-process context) "n\n"))
1246 (epg-context-set-result-for
1249 (epg-context-result-for context 'error)))
1250 (delete-process (epg-context-process context))))))
1252 (defun epg--status-GET_LINE (context string)
1253 (let ((entry (assoc string epg-prompt-alist))
1256 (process-send-string (epg-context-process context)
1257 (concat (read-string
1260 (concat string ": ")))
1263 (epg-context-set-result-for
1266 (epg-context-result-for context 'error)))
1267 (delete-process (epg-context-process context))))))
1269 (defun epg--status-*SIG (context status string)
1270 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1271 (let* ((key-id (match-string 1 string))
1272 (user-id (match-string 2 string))
1273 (entry (assoc key-id epg-user-id-alist)))
1274 (epg-context-set-result-for
1277 (cons (epg-make-signature status key-id)
1278 (epg-context-result-for context 'verify)))
1279 (if (eq (epg-context-protocol context) 'CMS)
1281 (setq user-id (epg-dn-from-string user-id))
1284 (setcdr entry user-id)
1285 (setq epg-user-id-alist
1286 (cons (cons key-id user-id) epg-user-id-alist))))
1287 (epg-context-set-result-for
1290 (cons (epg-make-signature status)
1291 (epg-context-result-for context 'verify)))))
1293 (defun epg--status-GOODSIG (context string)
1294 (epg--status-*SIG context 'good string))
1296 (defun epg--status-EXPSIG (context string)
1297 (epg--status-*SIG context 'expired string))
1299 (defun epg--status-EXPKEYSIG (context string)
1300 (epg--status-*SIG context 'expired-key string))
1302 (defun epg--status-REVKEYSIG (context string)
1303 (epg--status-*SIG context 'revoked-key string))
1305 (defun epg--status-BADSIG (context string)
1306 (epg--status-*SIG context 'bad string))
1308 (defun epg--status-NO_PUBKEY (context string)
1309 (let ((signature (car (epg-context-result-for context 'verify))))
1311 (eq (epg-signature-status signature) 'error)
1312 (equal (epg-signature-key-id signature) string))
1313 (epg-signature-set-status signature 'no-pubkey))))
1315 (defun epg--time-from-seconds (seconds)
1316 (let ((number-seconds (string-to-number (concat seconds ".0"))))
1317 (cons (floor (/ number-seconds 65536))
1318 (floor (mod number-seconds 65536)))))
1320 (defun epg--status-ERRSIG (context string)
1321 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1322 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
1324 (let ((signature (epg-make-signature 'error)))
1325 (epg-context-set-result-for
1329 (epg-context-result-for context 'verify)))
1330 (epg-signature-set-key-id
1332 (match-string 1 string))
1333 (epg-signature-set-pubkey-algorithm
1335 (string-to-number (match-string 2 string)))
1336 (epg-signature-set-digest-algorithm
1338 (string-to-number (match-string 3 string)))
1339 (epg-signature-set-class
1341 (string-to-number (match-string 4 string) 16))
1342 (epg-signature-set-creation-time
1344 (epg--time-from-seconds (match-string 5 string))))))
1346 (defun epg--status-VALIDSIG (context string)
1347 (let ((signature (car (epg-context-result-for context 'verify))))
1348 (when (and signature
1349 (eq (epg-signature-status signature) 'good)
1350 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
1351 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
1354 (epg-signature-set-fingerprint
1356 (match-string 1 string))
1357 (epg-signature-set-creation-time
1359 (epg--time-from-seconds (match-string 2 string)))
1360 (epg-signature-set-expiration-time
1362 (epg--time-from-seconds (match-string 3 string)))
1363 (epg-signature-set-version
1365 (string-to-number (match-string 4 string)))
1366 (epg-signature-set-pubkey-algorithm
1368 (string-to-number (match-string 5 string)))
1369 (epg-signature-set-digest-algorithm
1371 (string-to-number (match-string 6 string)))
1372 (epg-signature-set-class
1374 (string-to-number (match-string 7 string) 16)))))
1376 (defun epg--status-TRUST_UNDEFINED (context string)
1377 (let ((signature (car (epg-context-result-for context 'verify))))
1379 (eq (epg-signature-status signature) 'good))
1380 (epg-signature-set-validity signature 'undefined))))
1382 (defun epg--status-TRUST_NEVER (context string)
1383 (let ((signature (car (epg-context-result-for context 'verify))))
1385 (eq (epg-signature-status signature) 'good))
1386 (epg-signature-set-validity signature 'never))))
1388 (defun epg--status-TRUST_MARGINAL (context string)
1389 (let ((signature (car (epg-context-result-for context 'verify))))
1391 (eq (epg-signature-status signature) 'marginal))
1392 (epg-signature-set-validity signature 'marginal))))
1394 (defun epg--status-TRUST_FULLY (context string)
1395 (let ((signature (car (epg-context-result-for context 'verify))))
1397 (eq (epg-signature-status signature) 'good))
1398 (epg-signature-set-validity signature 'full))))
1400 (defun epg--status-TRUST_ULTIMATE (context string)
1401 (let ((signature (car (epg-context-result-for context 'verify))))
1403 (eq (epg-signature-status signature) 'good))
1404 (epg-signature-set-validity signature 'ultimate))))
1406 (defun epg--status-NOTATION_NAME (context string)
1407 (let ((signature (car (epg-context-result-for context 'verify))))
1409 (epg-signature-set-notations
1411 (cons (epg-make-sig-notation string nil t nil)
1412 (epg-sig-notations signature))))))
1414 (defun epg--status-NOTATION_DATA (context string)
1415 (let ((signature (car (epg-context-result-for context 'verify)))
1418 (setq notation (car (epg-sig-notations signature))))
1419 (epg-sig-notation-set-value notation string))))
1421 (defun epg--status-POLICY_URL (context string)
1422 (let ((signature (car (epg-context-result-for context 'verify))))
1424 (epg-signature-set-notations
1426 (cons (epg-make-sig-notation nil string t nil)
1427 (epg-sig-notations signature))))))
1429 (defun epg--status-PROGRESS (context string)
1430 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1432 (funcall (if (consp (epg-context-progress-callback context))
1433 (car (epg-context-progress-callback context))
1434 (epg-context-progress-callback context))
1436 (match-string 1 string)
1437 (match-string 2 string)
1438 (string-to-number (match-string 3 string))
1439 (string-to-number (match-string 4 string))
1440 (if (consp (epg-context-progress-callback context))
1441 (cdr (epg-context-progress-callback context))))))
1443 (defun epg--status-DECRYPTION_FAILED (context string)
1444 (epg-context-set-result-for context 'decryption-failed t))
1446 (defun epg--status-DECRYPTION_OKAY (context string)
1447 (epg-context-set-result-for context 'decryption-okay t))
1449 (defun epg--status-NODATA (context string)
1450 (epg-context-set-result-for
1452 (cons (cons 'no-data (string-to-number string))
1453 (epg-context-result-for context 'error))))
1455 (defun epg--status-UNEXPECTED (context string)
1456 (epg-context-set-result-for
1458 (cons (cons 'unexpected (string-to-number string))
1459 (epg-context-result-for context 'error))))
1461 (defun epg--status-KEYEXPIRED (context string)
1462 (epg-context-set-result-for
1464 (cons (list 'key-expired (cons 'expiration-time
1465 (epg--time-from-seconds string)))
1466 (epg-context-result-for context 'error))))
1468 (defun epg--status-KEYREVOKED (context string)
1469 (epg-context-set-result-for
1471 (cons '(key-revoked)
1472 (epg-context-result-for context 'error))))
1474 (defun epg--status-BADARMOR (context string)
1475 (epg-context-set-result-for
1478 (epg-context-result-for context 'error))))
1480 (defun epg--status-INV_RECP (context string)
1481 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1482 (epg-context-set-result-for
1484 (cons (list 'invalid-recipient
1486 (string-to-number (match-string 1 string)))
1487 (cons 'requested-recipient
1488 (match-string 2 string)))
1489 (epg-context-result-for context 'error)))))
1491 (defun epg--status-NO_RECP (context string)
1492 (epg-context-set-result-for
1494 (cons '(no-recipients)
1495 (epg-context-result-for context 'error))))
1497 (defun epg--status-DELETE_PROBLEM (context string)
1498 (if (string-match "\\`\\([0-9]+\\)" string)
1499 (epg-context-set-result-for
1501 (cons (cons 'delete-problem
1502 (string-to-number (match-string 1 string)))
1503 (epg-context-result-for context 'error)))))
1505 (defun epg--status-SIG_CREATED (context string)
1506 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1507 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1508 (epg-context-set-result-for
1510 (cons (epg-make-new-signature
1511 (cdr (assq (aref (match-string 1 string) 0)
1512 epg-new-signature-type-alist))
1513 (string-to-number (match-string 2 string))
1514 (string-to-number (match-string 3 string))
1515 (string-to-number (match-string 4 string) 16)
1516 (epg--time-from-seconds (match-string 5 string))
1517 (substring string (match-end 0)))
1518 (epg-context-result-for context 'sign)))))
1520 (defun epg--status-KEY_CREATED (context string)
1521 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1522 (epg-context-set-result-for
1523 context 'generate-key
1524 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1525 (cons 'fingerprint (match-string 2 string)))
1526 (epg-context-result-for context 'generate-key)))))
1528 (defun epg--status-KEY_NOT_CREATED (context string)
1529 (epg-context-set-result-for
1531 (cons '(key-not-created)
1532 (epg-context-result-for context 'error))))
1534 (defun epg--status-IMPORTED (context string)
1535 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1536 (let* ((key-id (match-string 1 string))
1537 (user-id (match-string 2 string))
1538 (entry (assoc key-id epg-user-id-alist)))
1540 (setcdr entry user-id)
1541 (setq epg-user-id-alist (cons (cons key-id user-id)
1542 epg-user-id-alist))))))
1544 (defun epg--status-IMPORT_OK (context string)
1545 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1546 (let ((reason (string-to-number (match-string 1 string))))
1547 (epg-context-set-result-for
1548 context 'import-status
1549 (cons (epg-make-import-status (if (match-beginning 2)
1550 (match-string 3 string))
1552 (/= (logand reason 1) 0)
1553 (/= (logand reason 2) 0)
1554 (/= (logand reason 4) 0)
1555 (/= (logand reason 8) 0)
1556 (/= (logand reason 16) 0))
1557 (epg-context-result-for context 'import-status))))))
1559 (defun epg--status-IMPORT_PROBLEM (context string)
1560 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1561 (epg-context-set-result-for
1562 context 'import-status
1563 (cons (epg-make-import-status
1564 (if (match-beginning 2)
1565 (match-string 3 string))
1566 (string-to-number (match-string 1 string)))
1567 (epg-context-result-for context 'import-status)))))
1569 (defun epg--status-IMPORT_RES (context string)
1570 (when (string-match "\\`\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1571 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1572 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
1573 (epg-context-set-result-for
1575 (epg-make-import-result (string-to-number (match-string 1 string))
1576 (string-to-number (match-string 2 string))
1577 (string-to-number (match-string 3 string))
1578 (string-to-number (match-string 4 string))
1579 (string-to-number (match-string 5 string))
1580 (string-to-number (match-string 6 string))
1581 (string-to-number (match-string 7 string))
1582 (string-to-number (match-string 8 string))
1583 (string-to-number (match-string 9 string))
1584 (string-to-number (match-string 10 string))
1585 (string-to-number (match-string 11 string))
1586 (string-to-number (match-string 12 string))
1587 (string-to-number (match-string 13 string))
1588 (epg-context-result-for context 'import-status)))
1589 (epg-context-set-result-for context 'import-status nil)))
1591 (defun epg-passphrase-callback-function (context key-id handback)
1592 (if (eq key-id 'SYM)
1593 (read-passwd "Passphrase for symmetric encryption: "
1594 (eq (epg-context-operation context) 'encrypt))
1596 (if (eq key-id 'PIN)
1597 "Passphrase for PIN: "
1598 (let ((entry (assoc key-id epg-user-id-alist)))
1600 (format "Passphrase for %s %s: " key-id (cdr entry))
1601 (format "Passphrase for %s: " key-id)))))))
1603 (make-obsolete 'epg-passphrase-callback-function
1604 'epa-passphrase-callback-function)
1606 (defun epg--list-keys-1 (context name mode)
1607 (let ((args (append (if epg-gpg-home-directory
1608 (list "--homedir" epg-gpg-home-directory))
1609 (list "--with-colons" "--no-greeting" "--batch"
1610 "--with-fingerprint"
1611 "--with-fingerprint"
1612 (if (memq mode '(t secret))
1613 "--list-secret-keys"
1614 (if (memq mode '(nil public))
1617 (unless (eq (epg-context-protocol context) 'CMS)
1618 '("--fixed-list-mode"))
1619 (if name (list name))))
1620 keys string field index)
1622 (apply #'call-process
1623 (if (eq (epg-context-protocol context) 'CMS)
1626 nil (list t nil) nil args)
1627 (goto-char (point-min))
1628 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1629 (setq keys (cons (make-vector 15 nil) keys)
1630 string (match-string 0)
1634 (string-match "\\([^:]+\\)?:" string index))
1635 (setq index (match-end 0))
1636 (aset (car keys) field (match-string 1 string))
1637 (setq field (1+ field))))
1640 (defun epg--make-sub-key-1 (line)
1643 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1645 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1647 (member (aref line 0) '("sec" "ssb"))
1648 (string-to-number (aref line 3))
1649 (string-to-number (aref line 2))
1651 (epg--time-from-seconds (aref line 5))
1652 (epg--time-from-seconds (aref line 6))))
1655 (defun epg-list-keys (context &optional name mode)
1656 "Return a list of epg-key objects matched with NAME.
1657 If MODE is nil or 'public, only public keyring should be searched.
1658 If MODE is t or 'secret, only secret keyring should be searched.
1659 Otherwise, only public keyring should be searched and the key
1660 signatures should be included."
1661 (let ((lines (epg--list-keys-1 context name mode))
1662 keys cert pointer pointer-1)
1665 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1666 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1667 keys (cons (epg-make-key
1668 (if (aref (car lines) 8)
1669 (cdr (assq (string-to-char (aref (car lines) 8))
1670 epg-key-validity-alist))))
1672 (epg-key-set-sub-key-list
1674 (cons (epg--make-sub-key-1 (car lines))
1675 (epg-key-sub-key-list (car keys)))))
1676 ((member (aref (car lines) 0) '("sub" "ssb"))
1677 (epg-key-set-sub-key-list
1679 (cons (epg--make-sub-key-1 (car lines))
1680 (epg-key-sub-key-list (car keys)))))
1681 ((equal (aref (car lines) 0) "uid")
1682 (epg-key-set-user-id-list
1684 (cons (epg-make-user-id
1685 (if (aref (car lines) 1)
1686 (cdr (assq (string-to-char (aref (car lines) 1))
1687 epg-key-validity-alist)))
1690 (epg-dn-from-string (aref (car lines) 9))
1691 (error (aref (car lines) 9)))
1692 (aref (car lines) 9)))
1693 (epg-key-user-id-list (car keys)))))
1694 ((equal (aref (car lines) 0) "fpr")
1695 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1696 (aref (car lines) 9)))
1697 ((equal (aref (car lines) 0) "sig")
1698 (epg-user-id-set-signature-list
1699 (car (epg-key-user-id-list (car keys)))
1701 (epg-make-key-signature
1702 (if (aref (car lines) 1)
1703 (cdr (assq (string-to-char (aref (car lines) 1))
1704 epg-key-validity-alist)))
1705 (string-to-number (aref (car lines) 3))
1706 (aref (car lines) 4)
1707 (epg--time-from-seconds (aref (car lines) 5))
1708 (epg--time-from-seconds (aref (car lines) 6))
1709 (aref (car lines) 9)
1710 (string-to-number (aref (car lines) 10) 16)
1711 (eq (aref (aref (car lines) 10) 2) ?x))
1712 (epg-user-id-signature-list
1713 (car (epg-key-user-id-list (car keys))))))))
1714 (setq lines (cdr lines)))
1715 (setq keys (nreverse keys)
1718 (epg-key-set-sub-key-list
1720 (nreverse (epg-key-sub-key-list (car pointer))))
1721 (setq pointer-1 (epg-key-set-user-id-list
1723 (nreverse (epg-key-user-id-list (car pointer)))))
1725 (epg-user-id-set-signature-list
1727 (nreverse (epg-user-id-signature-list (car pointer-1))))
1728 (setq pointer-1 (cdr pointer-1)))
1729 (setq pointer (cdr pointer)))
1732 (if (fboundp 'make-temp-file)
1733 (defalias 'epg--make-temp-file 'make-temp-file)
1734 (defvar temporary-file-directory)
1735 ;; stolen from poe.el.
1736 (defun epg--make-temp-file (prefix)
1737 "Create a temporary file.
1738 The returned file name (created by appending some random characters at the end
1739 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1740 is guaranteed to point to a newly created empty file.
1741 You can then use `write-region' to write new data into the file."
1742 (let (tempdir tempfile)
1743 (setq prefix (expand-file-name prefix
1744 (if (featurep 'xemacs)
1746 temporary-file-directory)))
1749 ;; First, create a temporary directory.
1750 (while (condition-case ()
1752 (setq tempdir (make-temp-name
1754 (file-name-directory prefix)
1756 ;; return nil or signal an error.
1757 (make-directory tempdir))
1759 (file-already-exists t)))
1760 (set-file-modes tempdir 448)
1761 ;; Second, create a temporary file in the tempdir.
1762 ;; There *is* a race condition between `make-temp-name'
1763 ;; and `write-region', but we don't care it since we are
1764 ;; in a private directory now.
1765 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1766 (write-region "" nil tempfile nil 'silent)
1767 (set-file-modes tempfile 384)
1768 ;; Finally, make a hard-link from the tempfile.
1769 (while (condition-case ()
1771 (setq file (make-temp-name prefix))
1772 ;; return nil or signal an error.
1773 (add-name-to-file tempfile file))
1775 (file-already-exists t)))
1777 ;; Cleanup the tempfile.
1779 (file-exists-p tempfile)
1780 (delete-file tempfile))
1781 ;; Cleanup the tempdir.
1783 (file-directory-p tempdir)
1784 (delete-directory tempdir))))))
1786 (if (fboundp 'clear-string)
1787 (defalias 'epg--clear-string 'clear-string)
1788 (defun epg--clear-string (string)
1789 (fillarray string 0)))
1791 (defun epg--args-from-sig-notations (notations)
1795 (if (and (epg-sig-notation-name notation)
1796 (not (epg-sig-notation-human-readable notation)))
1797 (error "Unreadable"))
1798 (if (epg-sig-notation-name notation)
1799 (list "--sig-notation"
1800 (if (epg-sig-notation-critical notation)
1801 (concat "!" (epg-sig-notation-name notation)
1802 "=" (epg-sig-notation-value notation))
1803 (concat (epg-sig-notation-name notation)
1804 "=" (epg-sig-notation-value notation))))
1805 (list "--sig-policy-url"
1806 (if (epg-sig-notation-critical notation)
1807 (concat "!" (epg-sig-notation-value notation))
1808 (epg-sig-notation-value notation)))))
1812 (defun epg-cancel (context)
1813 (if (buffer-live-p (process-buffer (epg-context-process context)))
1815 (set-buffer (process-buffer (epg-context-process context)))
1816 (epg-context-set-result-for
1819 (epg-context-result-for epg-context 'error)))))
1820 (if (eq (process-status (epg-context-process context)) 'run)
1821 (delete-process (epg-context-process context))))
1824 (defun epg-start-decrypt (context cipher)
1825 "Initiate a decrypt operation on CIPHER.
1826 CIPHER must be a file data object.
1828 If you use this function, you will need to wait for the completion of
1829 `epg-gpg-program' by using `epg-wait-for-completion' and call
1830 `epg-reset' to clear a temporaly output file.
1831 If you are unsure, use synchronous version of this function
1832 `epg-decrypt-file' or `epg-decrypt-string' instead."
1833 (unless (epg-data-file cipher)
1834 (error "Not a file"))
1835 (epg-context-set-operation context 'decrypt)
1836 (epg-context-set-result context nil)
1837 (epg--start context (list "--decrypt" "--" (epg-data-file cipher)))
1838 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1839 (unless (eq (epg-context-protocol context) 'CMS)
1840 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1842 (defun epg--check-decrypt (context)
1843 (if (epg-context-result-for context 'decryption-failed)
1844 (signal 'epg-error (list "Decryption failed")))
1845 (if (epg-context-result-for context 'no-secret-key)
1847 (list "No secret key"
1848 (epg-context-result-for context 'no-secret-key))))
1849 (unless (epg-context-result-for context 'decryption-okay)
1850 (let* ((error (epg-context-result-for context 'error)))
1851 (if (assq 'no-data error)
1852 (signal 'epg-error (list "No data")))
1853 (signal 'epg-error (list "Can't decrypt" error)))))
1856 (defun epg-decrypt-file (context cipher plain)
1857 "Decrypt a file CIPHER and store the result to a file PLAIN.
1858 If PLAIN is nil, it returns the result as a string."
1862 (epg-context-set-output-file context plain)
1863 (epg-context-set-output-file context
1864 (epg--make-temp-file "epg-output")))
1865 (epg-start-decrypt context (epg-make-data-from-file cipher))
1866 (epg-wait-for-completion context)
1867 (epg--check-decrypt context)
1869 (epg-read-output context)))
1871 (epg-delete-output-file context))
1872 (epg-reset context)))
1875 (defun epg-decrypt-string (context cipher)
1876 "Decrypt a string CIPHER and return the plain text."
1877 (let ((input-file (epg--make-temp-file "epg-input"))
1878 (coding-system-for-write 'binary))
1881 (write-region cipher nil input-file nil 'quiet)
1882 (epg-context-set-output-file context
1883 (epg--make-temp-file "epg-output"))
1884 (epg-start-decrypt context (epg-make-data-from-file input-file))
1885 (epg-wait-for-completion context)
1886 (epg--check-decrypt context)
1887 (epg-read-output context))
1888 (epg-delete-output-file context)
1889 (if (file-exists-p input-file)
1890 (delete-file input-file))
1891 (epg-reset context))))
1894 (defun epg-start-verify (context signature &optional signed-text)
1895 "Initiate a verify operation on SIGNATURE.
1896 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1898 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1899 For a normal or a cleartext signature, SIGNED-TEXT should be nil.
1901 If you use this function, you will need to wait for the completion of
1902 `epg-gpg-program' by using `epg-wait-for-completion' and call
1903 `epg-reset' to clear a temporaly output file.
1904 If you are unsure, use synchronous version of this function
1905 `epg-verify-file' or `epg-verify-string' instead."
1906 (epg-context-set-operation context 'verify)
1907 (epg-context-set-result context nil)
1909 ;; Detached signature.
1910 (if (epg-data-file signed-text)
1911 (epg--start context (list "--verify" "--" (epg-data-file signature)
1912 (epg-data-file signed-text)))
1913 (epg--start context (list "--verify" "--" (epg-data-file signature)
1915 (if (eq (process-status (epg-context-process context)) 'run)
1916 (process-send-string (epg-context-process context)
1917 (epg-data-string signed-text)))
1918 (if (eq (process-status (epg-context-process context)) 'run)
1919 (process-send-eof (epg-context-process context))))
1920 ;; Normal (or cleartext) signature.
1921 (if (epg-data-file signature)
1922 (epg--start context (list "--" (epg-data-file signature)))
1923 (epg--start context '("-"))
1924 (if (eq (process-status (epg-context-process context)) 'run)
1925 (process-send-string (epg-context-process context)
1926 (epg-data-string signature)))
1927 (if (eq (process-status (epg-context-process context)) 'run)
1928 (process-send-eof (epg-context-process context))))))
1931 (defun epg-verify-file (context signature &optional signed-text plain)
1932 "Verify a file SIGNATURE.
1933 SIGNED-TEXT and PLAIN are also a file if they are specified.
1935 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1936 string. For a normal or a cleartext signature, SIGNED-TEXT should be
1937 nil. In the latter case, if PLAIN is specified, the plaintext is
1938 stored into the file after successful verification."
1942 (epg-context-set-output-file context plain)
1943 (epg-context-set-output-file context
1944 (epg--make-temp-file "epg-output")))
1946 (epg-start-verify context
1947 (epg-make-data-from-file signature)
1948 (epg-make-data-from-file signed-text))
1949 (epg-start-verify context
1950 (epg-make-data-from-file signature)))
1951 (epg-wait-for-completion context)
1953 (epg-read-output context)))
1955 (epg-delete-output-file context))
1956 (epg-reset context)))
1959 (defun epg-verify-string (context signature &optional signed-text)
1960 "Verify a string SIGNATURE.
1961 SIGNED-TEXT is a string if it is specified.
1963 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1964 string. For a normal or a cleartext signature, SIGNED-TEXT should be
1965 nil. In the latter case, this function returns the plaintext after
1966 successful verification."
1967 (let ((coding-system-for-write 'binary)
1971 (epg-context-set-output-file context
1972 (epg--make-temp-file "epg-output"))
1975 (setq input-file (epg--make-temp-file "epg-signature"))
1976 (write-region signature nil input-file nil 'quiet)
1977 (epg-start-verify context
1978 (epg-make-data-from-file input-file)
1979 (epg-make-data-from-string signed-text)))
1980 (epg-start-verify context (epg-make-data-from-string signature)))
1981 (epg-wait-for-completion context)
1982 (epg-read-output context))
1983 (epg-delete-output-file context)
1985 (file-exists-p input-file))
1986 (delete-file input-file))
1987 (epg-reset context))))
1990 (defun epg-start-sign (context plain &optional mode)
1991 "Initiate a sign operation on PLAIN.
1992 PLAIN is a data object.
1994 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1995 If it is nil or 'normal, it makes a normal signature.
1996 Otherwise, it makes a cleartext signature.
1998 If you use this function, you will need to wait for the completion of
1999 `epg-gpg-program' by using `epg-wait-for-completion' and call
2000 `epg-reset' to clear a temporaly output file.
2001 If you are unsure, use synchronous version of this function
2002 `epg-sign-file' or `epg-sign-string' instead."
2003 (epg-context-set-operation context 'sign)
2004 (epg-context-set-result context nil)
2005 (unless (memq mode '(t detached nil normal)) ;i.e. cleartext
2006 (epg-context-set-armor context nil)
2007 (epg-context-set-textmode context nil))
2009 (append (list (if (memq mode '(t detached))
2011 (if (memq mode '(nil normal))
2019 (car (epg-key-sub-key-list signer)))))
2020 (epg-context-signers context)))
2021 (epg--args-from-sig-notations
2022 (epg-context-sig-notations context))
2023 (if (epg-data-file plain)
2024 (list "--" (epg-data-file plain)))))
2025 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2026 (unless (eq (epg-context-protocol context) 'CMS)
2027 (epg-wait-for-status context '("BEGIN_SIGNING")))
2028 (when (epg-data-string plain)
2029 (if (eq (process-status (epg-context-process context)) 'run)
2030 (process-send-string (epg-context-process context)
2031 (epg-data-string plain)))
2032 (if (eq (process-status (epg-context-process context)) 'run)
2033 (process-send-eof (epg-context-process context)))))
2036 (defun epg-sign-file (context plain signature &optional mode)
2037 "Sign a file PLAIN and store the result to a file SIGNATURE.
2038 If SIGNATURE is nil, it returns the result as a string.
2039 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2040 If it is nil or 'normal, it makes a normal signature.
2041 Otherwise, it makes a cleartext signature."
2045 (epg-context-set-output-file context signature)
2046 (epg-context-set-output-file context
2047 (epg--make-temp-file "epg-output")))
2048 (epg-start-sign context (epg-make-data-from-file plain) mode)
2049 (epg-wait-for-completion context)
2050 (unless (epg-context-result-for context 'sign)
2051 (if (epg-context-result-for context 'error)
2052 (error "Sign failed: %S"
2053 (epg-context-result-for context 'error))
2054 (error "Sign failed")))
2056 (epg-read-output context)))
2058 (epg-delete-output-file context))
2059 (epg-reset context)))
2062 (defun epg-sign-string (context plain &optional mode)
2063 "Sign a string PLAIN and return the output as string.
2064 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2065 If it is nil or 'normal, it makes a normal signature.
2066 Otherwise, it makes a cleartext signature."
2069 (epg-context-set-output-file context
2070 (epg--make-temp-file "epg-output"))
2071 (epg-start-sign context (epg-make-data-from-string plain) mode)
2072 (epg-wait-for-completion context)
2073 (unless (epg-context-result-for context 'sign)
2074 (if (epg-context-result-for context 'error)
2075 (error "Sign failed: %S"
2076 (epg-context-result-for context 'error))
2077 (error "Sign failed")))
2078 (epg-read-output context))
2079 (epg-delete-output-file context)
2080 (epg-reset context)))
2083 (defun epg-start-encrypt (context plain recipients
2084 &optional sign always-trust)
2085 "Initiate an encrypt operation on PLAIN.
2086 PLAIN is a data object.
2087 If RECIPIENTS is nil, it performs symmetric encryption.
2089 If you use this function, you will need to wait for the completion of
2090 `epg-gpg-program' by using `epg-wait-for-completion' and call
2091 `epg-reset' to clear a temporaly output file.
2092 If you are unsure, use synchronous version of this function
2093 `epg-encrypt-file' or `epg-encrypt-string' instead."
2094 (epg-context-set-operation context 'encrypt)
2095 (epg-context-set-result context nil)
2097 (append (if always-trust '("--always-trust"))
2098 (if recipients '("--encrypt") '("--symmetric"))
2099 (if sign '("--sign"))
2106 (car (epg-key-sub-key-list
2108 (epg-context-signers context))))
2110 (epg--args-from-sig-notations
2111 (epg-context-sig-notations context)))
2117 (car (epg-key-sub-key-list recipient)))))
2119 (if (epg-data-file plain)
2120 (list "--" (epg-data-file plain)))))
2121 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2122 (unless (eq (epg-context-protocol context) 'CMS)
2124 (epg-wait-for-status context '("BEGIN_SIGNING"))
2125 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
2126 (when (epg-data-string plain)
2127 (if (eq (process-status (epg-context-process context)) 'run)
2128 (process-send-string (epg-context-process context)
2129 (epg-data-string plain)))
2130 (if (eq (process-status (epg-context-process context)) 'run)
2131 (process-send-eof (epg-context-process context)))))
2134 (defun epg-encrypt-file (context plain recipients
2135 cipher &optional sign always-trust)
2136 "Encrypt a file PLAIN and store the result to a file CIPHER.
2137 If CIPHER is nil, it returns the result as a string.
2138 If RECIPIENTS is nil, it performs symmetric encryption."
2142 (epg-context-set-output-file context cipher)
2143 (epg-context-set-output-file context
2144 (epg--make-temp-file "epg-output")))
2145 (epg-start-encrypt context (epg-make-data-from-file plain)
2146 recipients sign always-trust)
2147 (epg-wait-for-completion context)
2149 (not (epg-context-result-for context 'sign)))
2150 (if (epg-context-result-for context 'error)
2151 (error "Sign failed: %S"
2152 (epg-context-result-for context 'error))
2153 (error "Sign failed")))
2154 (if (epg-context-result-for context 'error)
2155 (error "Encrypt failed: %S"
2156 (epg-context-result-for context 'error)))
2158 (epg-read-output context)))
2160 (epg-delete-output-file context))
2161 (epg-reset context)))
2164 (defun epg-encrypt-string (context plain recipients
2165 &optional sign always-trust)
2166 "Encrypt a string PLAIN.
2167 If RECIPIENTS is nil, it performs symmetric encryption."
2170 (epg-context-set-output-file context
2171 (epg--make-temp-file "epg-output"))
2172 (epg-start-encrypt context (epg-make-data-from-string plain)
2173 recipients sign always-trust)
2174 (epg-wait-for-completion context)
2176 (not (epg-context-result-for context 'sign)))
2177 (if (epg-context-result-for context 'error)
2178 (error "Sign failed: %S"
2179 (epg-context-result-for context 'error))
2180 (error "Sign failed")))
2181 (if (epg-context-result-for context 'error)
2182 (error "Encrypt failed: %S"
2183 (epg-context-result-for context 'error)))
2184 (epg-read-output context))
2185 (epg-delete-output-file context)
2186 (epg-reset context)))
2189 (defun epg-start-export-keys (context keys)
2190 "Initiate an export keys operation.
2192 If you use this function, you will need to wait for the completion of
2193 `epg-gpg-program' by using `epg-wait-for-completion' and call
2194 `epg-reset' to clear a temporaly output file.
2195 If you are unsure, use synchronous version of this function
2196 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
2197 (epg-context-set-operation context 'export-keys)
2198 (epg-context-set-result context nil)
2199 (epg--start context (cons "--export"
2203 (car (epg-key-sub-key-list key))))
2207 (defun epg-export-keys-to-file (context keys file)
2208 "Extract public KEYS."
2212 (epg-context-set-output-file context file)
2213 (epg-context-set-output-file context
2214 (epg--make-temp-file "epg-output")))
2215 (epg-start-export-keys context keys)
2216 (epg-wait-for-completion context)
2217 (if (epg-context-result-for context 'error)
2218 (error "Export keys failed: %S"
2219 (epg-context-result-for context 'error)))
2221 (epg-read-output context)))
2223 (epg-delete-output-file context))
2224 (epg-reset context)))
2227 (defun epg-export-keys-to-string (context keys)
2228 "Extract public KEYS and return them as a string."
2229 (epg-export-keys-to-file context keys nil))
2232 (defun epg-start-import-keys (context keys)
2233 "Initiate an import keys operation.
2234 KEYS is a data object.
2236 If you use this function, you will need to wait for the completion of
2237 `epg-gpg-program' by using `epg-wait-for-completion' and call
2238 `epg-reset' to clear a temporaly output file.
2239 If you are unsure, use synchronous version of this function
2240 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
2241 (epg-context-set-operation context 'import-keys)
2242 (epg-context-set-result context nil)
2243 (epg--start context (if (epg-data-file keys)
2244 (list "--import" "--" (epg-data-file keys))
2246 (when (epg-data-string keys)
2247 (if (eq (process-status (epg-context-process context)) 'run)
2248 (process-send-string (epg-context-process context)
2249 (epg-data-string keys)))
2250 (if (eq (process-status (epg-context-process context)) 'run)
2251 (process-send-eof (epg-context-process context)))))
2253 (defun epg--import-keys-1 (context keys)
2256 (epg-start-import-keys context keys)
2257 (epg-wait-for-completion context)
2258 (if (epg-context-result-for context 'error)
2259 (error "Import keys failed: %S"
2260 (epg-context-result-for context 'error))))
2261 (epg-reset context)))
2264 (defun epg-import-keys-from-file (context keys)
2265 "Add keys from a file KEYS."
2266 (epg--import-keys-1 context (epg-make-data-from-file keys)))
2269 (defun epg-import-keys-from-string (context keys)
2270 "Add keys from a string KEYS."
2271 (epg--import-keys-1 context (epg-make-data-from-string keys)))
2274 (defun epg-start-receive-keys (context key-id-list)
2275 "Initiate a receive key operation.
2276 KEY-ID-LIST is a list of key IDs.
2278 If you use this function, you will need to wait for the completion of
2279 `epg-gpg-program' by using `epg-wait-for-completion' and call
2280 `epg-reset' to clear a temporaly output file.
2281 If you are unsure, use synchronous version of this function
2282 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2283 (epg-context-set-operation context 'receive-keys)
2284 (epg-context-set-result context nil)
2285 (epg--start context (cons "--recv-keys" key-id-list)))
2288 (defun epg-receive-keys (context keys)
2289 "Add keys from server.
2290 KEYS is a list of key IDs"
2293 (epg-start-receive-keys context keys)
2294 (epg-wait-for-completion context)
2295 (if (epg-context-result-for context 'error)
2296 (error "Receive keys failed: %S"
2297 (epg-context-result-for context 'error))))
2298 (epg-reset context)))
2301 (defalias 'epg-import-keys-from-server 'epg-receive-keys)
2304 (defun epg-start-delete-keys (context keys &optional allow-secret)
2305 "Initiate an delete keys operation.
2307 If you use this function, you will need to wait for the completion of
2308 `epg-gpg-program' by using `epg-wait-for-completion' and call
2309 `epg-reset' to clear a temporaly output file.
2310 If you are unsure, use synchronous version of this function
2311 `epg-delete-keys' instead."
2312 (epg-context-set-operation context 'delete-keys)
2313 (epg-context-set-result context nil)
2314 (epg--start context (cons (if allow-secret
2315 "--delete-secret-key"
2320 (car (epg-key-sub-key-list key))))
2324 (defun epg-delete-keys (context keys &optional allow-secret)
2325 "Delete KEYS from the key ring."
2328 (epg-start-delete-keys context keys allow-secret)
2329 (epg-wait-for-completion context)
2330 (let ((entry (assq 'delete-problem
2331 (epg-context-result-for context 'error))))
2333 (if (setq entry (assq (cdr entry)
2334 epg-delete-problem-reason-alist))
2335 (error "Delete keys failed: %s" (cdr entry)))
2336 (error "Delete keys failed" (cdr entry)))))
2337 (epg-reset context)))
2340 (defun epg-start-sign-keys (context keys &optional local)
2341 "Initiate an sign keys operation.
2343 If you use this function, you will need to wait for the completion of
2344 `epg-gpg-program' by using `epg-wait-for-completion' and call
2345 `epg-reset' to clear a temporaly output file.
2346 If you are unsure, use synchronous version of this function
2347 `epg-sign-keys' instead."
2348 (epg-context-set-operation context 'sign-keys)
2349 (epg-context-set-result context nil)
2350 (epg--start context (cons (if local
2356 (car (epg-key-sub-key-list key))))
2358 (make-obsolete 'epg-start-sign-keys "Do not use.")
2361 (defun epg-sign-keys (context keys &optional local)
2362 "Sign KEYS from the key ring."
2365 (epg-start-sign-keys context keys local)
2366 (epg-wait-for-completion context)
2367 (if (epg-context-result-for context 'error)
2368 (error "Sign keys failed: %S"
2369 (epg-context-result-for context 'error))))
2370 (epg-reset context)))
2371 (make-obsolete 'epg-sign-keys "Do not use.")
2374 (defun epg-start-generate-key (context parameters)
2375 "Initiate a key generation.
2376 PARAMETERS specifies parameters for the key.
2378 If you use this function, you will need to wait for the completion of
2379 `epg-gpg-program' by using `epg-wait-for-completion' and call
2380 `epg-reset' to clear a temporaly output file.
2381 If you are unsure, use synchronous version of this function
2382 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2383 (epg-context-set-operation context 'generate-key)
2384 (epg-context-set-result context nil)
2385 (if (epg-data-file parameters)
2386 (epg--start context (list "--batch" "--genkey" "--"
2387 (epg-data-file parameters)))
2388 (epg--start context '("--batch" "--genkey"))
2389 (if (eq (process-status (epg-context-process context)) 'run)
2390 (process-send-string (epg-context-process context)
2391 (epg-data-string parameters)))
2392 (if (eq (process-status (epg-context-process context)) 'run)
2393 (process-send-eof (epg-context-process context)))))
2396 (defun epg-generate-key-from-file (context parameters)
2397 "Generate a new key pair.
2398 PARAMETERS is a file which tells how to create the key."
2401 (epg-start-generate-key context (epg-make-data-from-file parameters))
2402 (epg-wait-for-completion context)
2403 (if (epg-context-result-for context 'error)
2404 (error "Generate key failed: %S"
2405 (epg-context-result-for context 'error))))
2406 (epg-reset context)))
2409 (defun epg-generate-key-from-string (context parameters)
2410 "Generate a new key pair.
2411 PARAMETERS is a string which tells how to create the key."
2414 (epg-start-generate-key context (epg-make-data-from-string parameters))
2415 (epg-wait-for-completion context)
2416 (if (epg-context-result-for context 'error)
2417 (error "Generate key failed: %S"
2418 (epg-context-result-for context 'error))))
2419 (epg-reset context)))
2421 (defun epg--decode-hexstring (string)
2423 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
2424 (setq string (replace-match "\\x\\&" t nil string)
2426 (car (read-from-string (concat "\"" string "\"")))))
2428 (defun epg--decode-quotedstring (string)
2430 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2431 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
2433 (if (match-beginning 2)
2434 (setq string (replace-match "\\2" t nil string)
2436 (if (match-beginning 3)
2437 (setq string (replace-match "\\x\\3" t nil string)
2439 (setq string (replace-match "\\\\\\\\\\4" t nil string)
2440 index (+ index 3)))))
2441 (car (read-from-string (concat "\"" string "\"")))))
2443 (defun epg-dn-from-string (string)
2444 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2445 The return value is an alist mapping from types to values."
2447 (length (length string))
2448 alist type value group)
2449 (while (< index length)
2450 (if (eq index (string-match "[ \t\n\r]*" string index))
2451 (setq index (match-end 0)))
2452 (if (eq index (string-match
2453 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2455 (setq type (match-string 1 string)
2456 index (match-end 0))
2457 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2459 (setq type (match-string 1 string)
2460 index (match-end 0))))
2462 (error "Invalid type"))
2463 (if (eq index (string-match
2464 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2466 (setq index (match-end 0)
2467 value (epg--decode-quotedstring (match-string 0 string)))
2468 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
2469 (setq index (match-end 0)
2470 value (epg--decode-hexstring (match-string 1 string)))
2471 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2473 (setq index (match-end 0)
2474 value (epg--decode-quotedstring
2475 (match-string 0 string))))))
2477 (if (stringp (car (car alist)))
2478 (setcar alist (list (cons type value) (car alist)))
2479 (setcar alist (cons (cons type value) (car alist))))
2480 (if (consp (car (car alist)))
2481 (setcar alist (nreverse (car alist))))
2482 (setq alist (cons (cons type value) alist)
2485 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
2486 (setq index (match-end 0)
2487 group (eq (aref string (match-beginning 1)) ?+))))
2490 (defun epg-decode-dn (alist)
2491 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2492 Type names are resolved using `epg-dn-type-alist'."
2495 (if (stringp (car rdn))
2496 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
2498 (format "%s=%s" (cdr entry) (cdr rdn))
2499 (format "%s=%s" (car rdn) (cdr rdn))))
2500 (concat "(" (epg-decode-dn rdn) ")")))
2506 ;;; epg.el ends here