Add algorithm names.
[emacs.git] / lisp / epg.el
blobf8d72f46cc07fff2f258c372432677ea5b48e3f6
1 ;;; epg.el --- the EasyPG Library
2 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
3 ;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Keywords: PGP, GnuPG
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Code:
25 (require 'epg-config)
27 (defvar epg-user-id nil
28 "GnuPG ID of your default identity.")
30 (defvar epg-user-id-alist nil
31 "An alist mapping from key ID to user ID.")
33 (defvar epg-last-status nil)
34 (defvar epg-read-point nil)
35 (defvar epg-process-filter-running nil)
36 (defvar epg-pending-status-list nil)
37 (defvar epg-key-id nil)
38 (defvar epg-context nil)
39 (defvar epg-debug-buffer nil)
41 ;; from gnupg/include/cipher.h
42 (defconst epg-cipher-algorithm-alist
43 '((0 . "NONE")
44 (1 . "IDEA")
45 (2 . "3DES")
46 (3 . "CAST5")
47 (4 . "BLOWFISH")
48 (7 . "AES")
49 (8 . "AES192")
50 (9 . "AES256")
51 (10 . "TWOFISH")
52 (11 . "CAMELLIA128")
53 (12 . "CAMELLIA256")
54 (110 . "DUMMY")))
56 ;; from gnupg/include/cipher.h
57 (defconst epg-pubkey-algorithm-alist
58 '((1 . "RSA")
59 (2 . "RSA_E")
60 (3 . "RSA_S")
61 (16 . "ELGAMAL_E")
62 (17 . "DSA")
63 (20 . "ELGAMAL")))
65 ;; from gnupg/include/cipher.h
66 (defconst epg-digest-algorithm-alist
67 '((1 . "MD5")
68 (2 . "SHA1")
69 (3 . "RMD160")
70 (8 . "SHA256")
71 (9 . "SHA384")
72 (10 . "SHA512")
73 (11 . "SHA224")))
75 ;; from gnupg/include/cipher.h
76 (defconst epg-compress-algorithm-alist
77 '((0 . "NONE")
78 (1 . "ZIP")
79 (2 . "ZLIB")
80 (3 . "BZIP2")))
82 (defconst epg-invalid-recipients-reason-alist
83 '((0 . "No specific reason given")
84 (1 . "Not Found")
85 (2 . "Ambigious specification")
86 (3 . "Wrong key usage")
87 (4 . "Key revoked")
88 (5 . "Key expired")
89 (6 . "No CRL known")
90 (7 . "CRL too old")
91 (8 . "Policy mismatch")
92 (9 . "Not a secret key")
93 (10 . "Key not trusted")))
95 (defconst epg-delete-problem-reason-alist
96 '((1 . "No such key")
97 (2 . "Must delete secret key first")
98 (3 . "Ambigious specification")))
100 (defconst epg-import-ok-reason-alist
101 '((0 . "Not actually changed")
102 (1 . "Entirely new key")
103 (2 . "New user IDs")
104 (4 . "New signatures")
105 (8 . "New subkeys")
106 (16 . "Contains private key")))
108 (defconst epg-import-problem-reason-alist
109 '((0 . "No specific reason given")
110 (1 . "Invalid Certificate")
111 (2 . "Issuer Certificate missing")
112 (3 . "Certificate Chain too long")
113 (4 . "Error storing certificate")))
115 (defconst epg-no-data-reason-alist
116 '((1 . "No armored data")
117 (2 . "Expected a packet but did not found one")
118 (3 . "Invalid packet found, this may indicate a non OpenPGP message")
119 (4 . "Signature expected but not found")))
121 (defconst epg-unexpected-reason-alist nil)
123 (defvar epg-key-validity-alist
124 '((?o . unknown)
125 (?i . invalid)
126 (?d . disabled)
127 (?r . revoked)
128 (?e . expired)
129 (?- . none)
130 (?q . undefined)
131 (?n . never)
132 (?m . marginal)
133 (?f . full)
134 (?u . ultimate)))
136 (defvar epg-key-capablity-alist
137 '((?e . encrypt)
138 (?s . sign)
139 (?c . certify)
140 (?a . authentication)))
142 (defvar epg-new-signature-type-alist
143 '((?D . detached)
144 (?C . clear)
145 (?S . normal)))
147 (defvar epg-dn-type-alist
148 '(("1.2.840.113549.1.9.1" . "EMail")
149 ("2.5.4.12" . "T")
150 ("2.5.4.42" . "GN")
151 ("2.5.4.4" . "SN")
152 ("0.2.262.1.10.7.20" . "NameDistinguisher")
153 ("2.5.4.16" . "ADDR")
154 ("2.5.4.15" . "BC")
155 ("2.5.4.13" . "D")
156 ("2.5.4.17" . "PostalCode")
157 ("2.5.4.65" . "Pseudo")
158 ("2.5.4.5" . "SerialNumber")))
160 (defvar epg-prompt-alist nil)
162 (put 'epg-error 'error-conditions '(epg-error error))
164 (defun epg-make-data-from-file (file)
165 "Make a data object from FILE."
166 (cons 'epg-data (vector file nil)))
168 (defun epg-make-data-from-string (string)
169 "Make a data object from STRING."
170 (cons 'epg-data (vector nil string)))
172 (defun epg-data-file (data)
173 "Return the file of DATA."
174 (unless (eq (car-safe data) 'epg-data)
175 (signal 'wrong-type-argument (list 'epg-data-p data)))
176 (aref (cdr data) 0))
178 (defun epg-data-string (data)
179 "Return the string of DATA."
180 (unless (eq (car-safe data) 'epg-data)
181 (signal 'wrong-type-argument (list 'epg-data-p data)))
182 (aref (cdr data) 1))
184 (defun epg-make-context (&optional protocol armor textmode include-certs
185 cipher-algorithm digest-algorithm
186 compress-algorithm)
187 "Return a context object."
188 (cons 'epg-context
189 (vector (or protocol 'OpenPGP) armor textmode include-certs
190 cipher-algorithm digest-algorithm compress-algorithm
191 (list #'epg-passphrase-callback-function)
193 nil nil nil nil nil nil)))
195 (defun epg-context-protocol (context)
196 "Return the protocol used within CONTEXT."
197 (unless (eq (car-safe context) 'epg-context)
198 (signal 'wrong-type-argument (list 'epg-context-p context)))
199 (aref (cdr context) 0))
201 (defun epg-context-armor (context)
202 "Return t if the output should be ASCII armored in CONTEXT."
203 (unless (eq (car-safe context) 'epg-context)
204 (signal 'wrong-type-argument (list 'epg-context-p context)))
205 (aref (cdr context) 1))
207 (defun epg-context-textmode (context)
208 "Return t if canonical text mode should be used in CONTEXT."
209 (unless (eq (car-safe context) 'epg-context)
210 (signal 'wrong-type-argument (list 'epg-context-p context)))
211 (aref (cdr context) 2))
213 (defun epg-context-include-certs (context)
214 "Return how many certificates should be included in an S/MIME signed message."
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 signing."
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 signing."
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 should 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
330 passphrase-callback)
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 (if (consp passphrase-callback)
335 passphrase-callback
336 (list passphrase-callback))))
338 (defun epg-context-set-progress-callback (context
339 progress-callback)
340 "Set the function which handles progress update.
341 If optional argument HANDBACK is specified, it is passed to PROGRESS-CALLBACK."
342 (unless (eq (car-safe context) 'epg-context)
343 (signal 'wrong-type-argument (list 'epg-context-p context)))
344 (aset (cdr context) 8 (if (consp progress-callback)
345 progress-callback
346 (list progress-callback))))
348 (defun epg-context-set-signers (context signers)
349 "Set the list of key-id for signing."
350 (unless (eq (car-safe context) 'epg-context)
351 (signal 'wrong-type-argument (list 'epg-context-p context)))
352 (aset (cdr context) 9 signers))
354 (defun epg-context-set-sig-notations (context notations)
355 "Set the list of notations for signing."
356 (unless (eq (car-safe context) 'epg-context)
357 (signal 'wrong-type-argument (list 'epg-context-p context)))
358 (aset (cdr context) 10 notations))
360 (defun epg-context-set-process (context process)
361 "Set the process object of `epg-gpg-program'.
362 This function is for internal use only."
363 (unless (eq (car-safe context) 'epg-context)
364 (signal 'wrong-type-argument (list 'epg-context-p context)))
365 (aset (cdr context) 11 process))
367 (defun epg-context-set-output-file (context output-file)
368 "Set the output file of `epg-gpg-program'.
369 This function is for internal use only."
370 (unless (eq (car-safe context) 'epg-context)
371 (signal 'wrong-type-argument (list 'epg-context-p context)))
372 (aset (cdr context) 12 output-file))
374 (defun epg-context-set-result (context result)
375 "Set the result of the previous cryptographic operation."
376 (unless (eq (car-safe context) 'epg-context)
377 (signal 'wrong-type-argument (list 'epg-context-p context)))
378 (aset (cdr context) 13 result))
380 (defun epg-context-set-operation (context operation)
381 "Set the name of the current cryptographic operation."
382 (unless (eq (car-safe context) 'epg-context)
383 (signal 'wrong-type-argument (list 'epg-context-p context)))
384 (aset (cdr context) 14 operation))
386 (defun epg-make-signature (status &optional key-id)
387 "Return a signature object."
388 (cons 'epg-signature (vector status key-id nil nil nil nil nil nil nil nil
389 nil)))
391 (defun epg-signature-status (signature)
392 "Return the status code of SIGNATURE."
393 (unless (eq (car-safe signature) 'epg-signature)
394 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
395 (aref (cdr signature) 0))
397 (defun epg-signature-key-id (signature)
398 "Return the key-id of SIGNATURE."
399 (unless (eq (car-safe signature) 'epg-signature)
400 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
401 (aref (cdr signature) 1))
403 (defun epg-signature-validity (signature)
404 "Return the validity of SIGNATURE."
405 (unless (eq (car-safe signature) 'epg-signature)
406 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
407 (aref (cdr signature) 2))
409 (defun epg-signature-fingerprint (signature)
410 "Return the fingerprint of SIGNATURE."
411 (unless (eq (car-safe signature) 'epg-signature)
412 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
413 (aref (cdr signature) 3))
415 (defun epg-signature-creation-time (signature)
416 "Return the creation 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) 4))
421 (defun epg-signature-expiration-time (signature)
422 "Return the expiration time of SIGNATURE."
423 (unless (eq (car-safe signature) 'epg-signature)
424 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
425 (aref (cdr signature) 5))
427 (defun epg-signature-pubkey-algorithm (signature)
428 "Return the public key 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) 6))
433 (defun epg-signature-digest-algorithm (signature)
434 "Return the digest algorithm of SIGNATURE."
435 (unless (eq (car-safe signature) 'epg-signature)
436 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
437 (aref (cdr signature) 7))
439 (defun epg-signature-class (signature)
440 "Return the class of SIGNATURE."
441 (unless (eq (car-safe signature) 'epg-signature)
442 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
443 (aref (cdr signature) 8))
445 (defun epg-signature-version (signature)
446 "Return the version of SIGNATURE."
447 (unless (eq (car-safe signature) 'epg-signature)
448 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
449 (aref (cdr signature) 9))
451 (defun epg-sig-notations (signature)
452 "Return the list of notations of SIGNATURE."
453 (unless (eq (car-safe signature) 'epg-signature)
454 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
455 (aref (cdr signature) 10))
457 (defun epg-signature-set-status (signature status)
458 "Set the status code of SIGNATURE."
459 (unless (eq (car-safe signature) 'epg-signature)
460 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
461 (aset (cdr signature) 0 status))
463 (defun epg-signature-set-key-id (signature key-id)
464 "Set the key-id of SIGNATURE."
465 (unless (eq (car-safe signature) 'epg-signature)
466 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
467 (aset (cdr signature) 1 key-id))
469 (defun epg-signature-set-validity (signature validity)
470 "Set the validity of SIGNATURE."
471 (unless (eq (car-safe signature) 'epg-signature)
472 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
473 (aset (cdr signature) 2 validity))
475 (defun epg-signature-set-fingerprint (signature fingerprint)
476 "Set the fingerprint of SIGNATURE."
477 (unless (eq (car-safe signature) 'epg-signature)
478 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
479 (aset (cdr signature) 3 fingerprint))
481 (defun epg-signature-set-creation-time (signature creation-time)
482 "Set the creation 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) 4 creation-time))
487 (defun epg-signature-set-expiration-time (signature expiration-time)
488 "Set the expiration time of SIGNATURE."
489 (unless (eq (car-safe signature) 'epg-signature)
490 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
491 (aset (cdr signature) 5 expiration-time))
493 (defun epg-signature-set-pubkey-algorithm (signature pubkey-algorithm)
494 "Set the public key 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) 6 pubkey-algorithm))
499 (defun epg-signature-set-digest-algorithm (signature digest-algorithm)
500 "Set the digest algorithm of SIGNATURE."
501 (unless (eq (car-safe signature) 'epg-signature)
502 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
503 (aset (cdr signature) 7 digest-algorithm))
505 (defun epg-signature-set-class (signature class)
506 "Set the class of SIGNATURE."
507 (unless (eq (car-safe signature) 'epg-signature)
508 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
509 (aset (cdr signature) 8 class))
511 (defun epg-signature-set-version (signature version)
512 "Set the version of SIGNATURE."
513 (unless (eq (car-safe signature) 'epg-signature)
514 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
515 (aset (cdr signature) 9 version))
517 (defun epg-signature-set-notations (signature notations)
518 "Set the list of notations of SIGNATURE."
519 (unless (eq (car-safe signature) 'epg-signature)
520 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
521 (aset (cdr signature) 10 notations))
523 (defun epg-make-new-signature (type pubkey-algorithm digest-algorithm
524 class creation-time fingerprint)
525 "Return a new signature object."
526 (cons 'epg-new-signature (vector type pubkey-algorithm digest-algorithm
527 class creation-time fingerprint)))
529 (defun epg-new-signature-type (new-signature)
530 "Return the type 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) 0))
535 (defun epg-new-signature-pubkey-algorithm (new-signature)
536 "Return the public key 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) 1))
541 (defun epg-new-signature-digest-algorithm (new-signature)
542 "Return the digest algorithm 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) 2))
547 (defun epg-new-signature-class (new-signature)
548 "Return the class 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) 3))
553 (defun epg-new-signature-creation-time (new-signature)
554 "Return the creation time 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) 4))
559 (defun epg-new-signature-fingerprint (new-signature)
560 "Return the fingerprint of NEW-SIGNATURE."
561 (unless (eq (car-safe new-signature) 'epg-new-signature)
562 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
563 (aref (cdr new-signature) 5))
565 (defun epg-make-key (owner-trust)
566 "Return a key object."
567 (cons 'epg-key (vector owner-trust nil nil)))
569 (defun epg-key-owner-trust (key)
570 "Return the owner trust of KEY."
571 (unless (eq (car-safe key) 'epg-key)
572 (signal 'wrong-type-argument (list 'epg-key-p key)))
573 (aref (cdr key) 0))
575 (defun epg-key-sub-key-list (key)
576 "Return the sub key list of KEY."
577 (unless (eq (car-safe key) 'epg-key)
578 (signal 'wrong-type-argument (list 'epg-key-p key)))
579 (aref (cdr key) 1))
581 (defun epg-key-user-id-list (key)
582 "Return the user ID list of KEY."
583 (unless (eq (car-safe key) 'epg-key)
584 (signal 'wrong-type-argument (list 'epg-key-p key)))
585 (aref (cdr key) 2))
587 (defun epg-key-set-sub-key-list (key sub-key-list)
588 "Set the sub key 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) 1 sub-key-list))
593 (defun epg-key-set-user-id-list (key user-id-list)
594 "Set the user ID list of KEY."
595 (unless (eq (car-safe key) 'epg-key)
596 (signal 'wrong-type-argument (list 'epg-key-p key)))
597 (aset (cdr key) 2 user-id-list))
599 (defun epg-make-sub-key (validity capability secret-p algorithm length id
600 creation-time expiration-time)
601 "Return a sub key object."
602 (cons 'epg-sub-key
603 (vector validity capability secret-p algorithm length id creation-time
604 expiration-time nil)))
606 (defun epg-sub-key-validity (sub-key)
607 "Return the validity 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) 0))
612 (defun epg-sub-key-capability (sub-key)
613 "Return the capability of SUB-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) 1))
618 (defun epg-sub-key-secret-p (sub-key)
619 "Return non-nil if SUB-KEY is a secret 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) 2))
624 (defun epg-sub-key-algorithm (sub-key)
625 "Return the algorithm 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) 3))
630 (defun epg-sub-key-length (sub-key)
631 "Return the length 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) 4))
636 (defun epg-sub-key-id (sub-key)
637 "Return the ID 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) 5))
642 (defun epg-sub-key-creation-time (sub-key)
643 "Return the creation 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) 6))
648 (defun epg-sub-key-expiration-time (sub-key)
649 "Return the expiration time 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) 7))
654 (defun epg-sub-key-fingerprint (sub-key)
655 "Return the fingerprint of SUB-KEY."
656 (unless (eq (car-safe sub-key) 'epg-sub-key)
657 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
658 (aref (cdr sub-key) 8))
660 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
661 "Set the fingerprint of SUB-KEY.
662 This function is for internal use only."
663 (unless (eq (car-safe sub-key) 'epg-sub-key)
664 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
665 (aset (cdr sub-key) 8 fingerprint))
667 (defun epg-make-user-id (validity string)
668 "Return a user ID object."
669 (cons 'epg-user-id (vector validity string nil)))
671 (defun epg-user-id-validity (user-id)
672 "Return the validity 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) 0))
677 (defun epg-user-id-string (user-id)
678 "Return the name 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) 1))
683 (defun epg-user-id-signature-list (user-id)
684 "Return 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 (aref (cdr user-id) 2))
689 (defun epg-user-id-set-signature-list (user-id signature-list)
690 "Set the signature list of USER-ID."
691 (unless (eq (car-safe user-id) 'epg-user-id)
692 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
693 (aset (cdr user-id) 2 signature-list))
695 (defun epg-make-key-signature (validity pubkey-algorithm key-id creation-time
696 expiration-time user-id class
697 exportable-p)
698 "Return a key signature object."
699 (cons 'epg-key-signature
700 (vector validity pubkey-algorithm key-id creation-time expiration-time
701 user-id class exportable-p)))
703 (defun epg-key-signature-validity (key-signature)
704 "Return the validity 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) 0))
709 (defun epg-key-signature-pubkey-algorithm (key-signature)
710 "Return the public key algorithm 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) 1))
715 (defun epg-key-signature-key-id (key-signature)
716 "Return the key-id 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) 2))
721 (defun epg-key-signature-creation-time (key-signature)
722 "Return the creation 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) 3))
727 (defun epg-key-signature-expiration-time (key-signature)
728 "Return the expiration time 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) 4))
733 (defun epg-key-signature-user-id (key-signature)
734 "Return the user-id 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) 5))
739 (defun epg-key-signature-class (key-signature)
740 "Return the class of KEY-SIGNATURE."
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) 6))
745 (defun epg-key-signature-exportable-p (key-signature)
746 "Return t if KEY-SIGNATURE is exportable."
747 (unless (eq (car-safe key-signature) 'epg-key-signature)
748 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
749 (aref (cdr key-signature) 7))
751 (defun epg-make-sig-notation (name value &optional human-readable
752 critical)
753 "Return a notation object."
754 (cons 'epg-sig-notation (vector name value human-readable critical)))
756 (defun epg-sig-notation-name (sig-notation)
757 "Return the name of SIG-NOTATION."
758 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
759 (signal 'wrong-type-argument (list 'epg-sig-notation-p
760 sig-notation)))
761 (aref (cdr sig-notation) 0))
763 (defun epg-sig-notation-value (sig-notation)
764 "Return the value of SIG-NOTATION."
765 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
766 (signal 'wrong-type-argument (list 'epg-sig-notation-p
767 sig-notation)))
768 (aref (cdr sig-notation) 1))
770 (defun epg-sig-notation-human-readable (sig-notation)
771 "Return the human-readable of SIG-NOTATION."
772 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
773 (signal 'wrong-type-argument (list 'epg-sig-notation-p
774 sig-notation)))
775 (aref (cdr sig-notation) 2))
777 (defun epg-sig-notation-critical (sig-notation)
778 "Return the critical of SIG-NOTATION."
779 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
780 (signal 'wrong-type-argument (list 'epg-sig-notation-p
781 sig-notation)))
782 (aref (cdr sig-notation) 3))
784 (defun epg-sig-notation-set-value (sig-notation value)
785 "Set the value of SIG-NOTATION."
786 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
787 (signal 'wrong-type-argument (list 'epg-sig-notation-p
788 sig-notation)))
789 (aset (cdr sig-notation) 1 value))
791 (defun epg-make-import-status (fingerprint &optional reason new user-id
792 signature sub-key secret)
793 "Return an import status object."
794 (cons 'epg-import-status (vector fingerprint reason new user-id signature
795 sub-key secret)))
797 (defun epg-import-status-fingerprint (import-status)
798 "Return the fingerprint of the key that was considered."
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) 0))
803 (defun epg-import-status-reason (import-status)
804 "Return the reason code for import failure."
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) 1))
809 (defun epg-import-status-new (import-status)
810 "Return t if the imported key was new."
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) 2))
815 (defun epg-import-status-user-id (import-status)
816 "Return t if the imported key contained new user IDs."
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) 3))
821 (defun epg-import-status-signature (import-status)
822 "Return t if the imported key contained new signatures."
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) 4))
827 (defun epg-import-status-sub-key (import-status)
828 "Return t if the imported key contained new sub keys."
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) 5))
833 (defun epg-import-status-secret (import-status)
834 "Return t if the imported key contained a secret key."
835 (unless (eq (car-safe import-status) 'epg-import-status)
836 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
837 (aref (cdr import-status) 6))
839 (defun epg-make-import-result (considered no-user-id imported imported-rsa
840 unchanged new-user-ids new-sub-keys
841 new-signatures new-revocations
842 secret-read secret-imported
843 secret-unchanged not-imported
844 imports)
845 "Return an import result object."
846 (cons 'epg-import-result (vector considered no-user-id imported imported-rsa
847 unchanged new-user-ids new-sub-keys
848 new-signatures new-revocations secret-read
849 secret-imported secret-unchanged
850 not-imported imports)))
852 (defun epg-import-result-considered (import-result)
853 "Return the total number of considered keys."
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) 0))
858 (defun epg-import-result-no-user-id (import-result)
859 "Return the number of keys without user ID."
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) 1))
864 (defun epg-import-result-imported (import-result)
865 "Return the number of imported 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) 2))
870 (defun epg-import-result-imported-rsa (import-result)
871 "Return the number of imported RSA 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) 3))
876 (defun epg-import-result-unchanged (import-result)
877 "Return the number of unchanged keys."
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) 4))
882 (defun epg-import-result-new-user-ids (import-result)
883 "Return the number of new user IDs."
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) 5))
888 (defun epg-import-result-new-sub-keys (import-result)
889 "Return the number of new sub keys."
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) 6))
894 (defun epg-import-result-new-signatures (import-result)
895 "Return the number of new signatures."
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) 7))
900 (defun epg-import-result-new-revocations (import-result)
901 "Return the number of new revocations."
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) 8))
906 (defun epg-import-result-secret-read (import-result)
907 "Return the total number of secret keys read."
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) 9))
912 (defun epg-import-result-secret-imported (import-result)
913 "Return the number of imported 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) 10))
918 (defun epg-import-result-secret-unchanged (import-result)
919 "Return the number of unchanged secret keys."
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) 11))
924 (defun epg-import-result-not-imported (import-result)
925 "Return the number of keys not imported."
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) 12))
930 (defun epg-import-result-imports (import-result)
931 "Return the list of `epg-import-status' objects."
932 (unless (eq (car-safe import-result) 'epg-import-result)
933 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
934 (aref (cdr import-result) 13))
936 (defun epg-context-result-for (context name)
937 "Return the result of CONTEXT associated with NAME."
938 (cdr (assq name (epg-context-result context))))
940 (defun epg-context-set-result-for (context name value)
941 "Set the result of CONTEXT associated with NAME to VALUE."
942 (let* ((result (epg-context-result context))
943 (entry (assq name result)))
944 (if entry
945 (setcdr entry value)
946 (epg-context-set-result context (cons (cons name value) result)))))
948 (defun epg-signature-to-string (signature)
949 "Convert SIGNATURE to a human readable string."
950 (let* ((user-id (cdr (assoc (epg-signature-key-id signature)
951 epg-user-id-alist)))
952 (pubkey-algorithm (epg-signature-pubkey-algorithm signature)))
953 (concat
954 (cond ((eq (epg-signature-status signature) 'good)
955 "Good signature from ")
956 ((eq (epg-signature-status signature) 'bad)
957 "Bad signature from ")
958 ((eq (epg-signature-status signature) 'expired)
959 "Expired signature from ")
960 ((eq (epg-signature-status signature) 'expired-key)
961 "Signature made by expired key ")
962 ((eq (epg-signature-status signature) 'revoked-key)
963 "Signature made by revoked key ")
964 ((eq (epg-signature-status signature) 'no-pubkey)
965 "No public key for "))
966 (epg-signature-key-id signature)
967 (if user-id
968 (concat " "
969 (if (stringp user-id)
970 user-id
971 (epg-decode-dn user-id)))
973 (if (epg-signature-validity signature)
974 (format " (trust %s)" (epg-signature-validity signature))
976 (if (epg-signature-creation-time signature)
977 (format-time-string " created at %Y-%m-%dT%T%z"
978 (epg-signature-creation-time signature))
980 (if pubkey-algorithm
981 (concat " using "
982 (or (cdr (assq pubkey-algorithm epg-pubkey-algorithm-alist))
983 (format "(unknown algorithm %d)" pubkey-algorithm)))
984 ""))))
986 (defun epg-verify-result-to-string (verify-result)
987 "Convert VERIFY-RESULT to a human readable string."
988 (mapconcat #'epg-signature-to-string verify-result "\n"))
990 (defun epg-new-signature-to-string (new-signature)
991 "Convert NEW-SIGNATURE to a human readable string."
992 (concat
993 (cond ((eq (epg-new-signature-type new-signature) 'detached)
994 "Detached signature ")
995 ((eq (epg-new-signature-type new-signature) 'clear)
996 "Cleartext signature ")
998 "Signature "))
999 (cdr (assq (epg-new-signature-pubkey-algorithm new-signature)
1000 epg-pubkey-algorithm-alist))
1002 (cdr (assq (epg-new-signature-digest-algorithm new-signature)
1003 epg-digest-algorithm-alist))
1005 (format "%02X " (epg-new-signature-class new-signature))
1006 (epg-new-signature-fingerprint new-signature)))
1008 (defun epg-import-result-to-string (import-result)
1009 "Convert IMPORT-RESULT to a human readable string."
1010 (concat (format "Total number processed: %d\n"
1011 (epg-import-result-considered import-result))
1012 (if (> (epg-import-result-not-imported import-result) 0)
1013 (format " skipped new keys: %d\n"
1014 (epg-import-result-not-imported import-result)))
1015 (if (> (epg-import-result-no-user-id import-result) 0)
1016 (format " w/o user IDs: %d\n"
1017 (epg-import-result-no-user-id import-result)))
1018 (if (> (epg-import-result-imported import-result) 0)
1019 (concat (format " imported: %d"
1020 (epg-import-result-imported import-result))
1021 (if (> (epg-import-result-imported-rsa import-result) 0)
1022 (format " (RSA: %d)"
1023 (epg-import-result-imported-rsa
1024 import-result)))
1025 "\n"))
1026 (if (> (epg-import-result-unchanged import-result) 0)
1027 (format " unchanged: %d\n"
1028 (epg-import-result-unchanged import-result)))
1029 (if (> (epg-import-result-new-user-ids import-result) 0)
1030 (format " new user IDs: %d\n"
1031 (epg-import-result-new-user-ids import-result)))
1032 (if (> (epg-import-result-new-sub-keys import-result) 0)
1033 (format " new subkeys: %d\n"
1034 (epg-import-result-new-sub-keys import-result)))
1035 (if (> (epg-import-result-new-signatures import-result) 0)
1036 (format " new signatures: %d\n"
1037 (epg-import-result-new-signatures import-result)))
1038 (if (> (epg-import-result-new-revocations import-result) 0)
1039 (format " new key revocations: %d\n"
1040 (epg-import-result-new-revocations import-result)))
1041 (if (> (epg-import-result-secret-read import-result) 0)
1042 (format " secret keys read: %d\n"
1043 (epg-import-result-secret-read import-result)))
1044 (if (> (epg-import-result-secret-imported import-result) 0)
1045 (format " secret keys imported: %d\n"
1046 (epg-import-result-secret-imported import-result)))
1047 (if (> (epg-import-result-secret-unchanged import-result) 0)
1048 (format " secret keys unchanged: %d\n"
1049 (epg-import-result-secret-unchanged import-result)))))
1051 (defun epg--start (context args)
1052 "Start `epg-gpg-program' in a subprocess with given ARGS."
1053 (if (and (epg-context-process context)
1054 (eq (process-status (epg-context-process context)) 'run))
1055 (error "%s is already running in this context"
1056 (if (eq (epg-context-protocol context) 'CMS)
1057 epg-gpgsm-program
1058 epg-gpg-program)))
1059 (let* ((args (append (list "--no-tty"
1060 "--status-fd" "1"
1061 "--yes")
1062 (if (and (not (eq (epg-context-protocol context) 'CMS))
1063 (string-match ":" (or (getenv "GPG_AGENT_INFO")
1064 "")))
1065 '("--use-agent"))
1066 (if (and (not (eq (epg-context-protocol context) 'CMS))
1067 (epg-context-progress-callback context))
1068 '("--enable-progress-filter"))
1069 (if epg-gpg-home-directory
1070 (list "--homedir" epg-gpg-home-directory))
1071 (unless (eq (epg-context-protocol context) 'CMS)
1072 '("--command-fd" "0"))
1073 (if (epg-context-armor context) '("--armor"))
1074 (if (epg-context-textmode context) '("--textmode"))
1075 (if (epg-context-output-file context)
1076 (list "--output" (epg-context-output-file context)))
1077 args))
1078 (coding-system-for-write 'binary)
1079 (coding-system-for-read 'binary)
1080 process-connection-type
1081 (orig-mode (default-file-modes))
1082 (buffer (generate-new-buffer " *epg*"))
1083 process)
1084 (if epg-debug
1085 (save-excursion
1086 (unless epg-debug-buffer
1087 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
1088 (set-buffer epg-debug-buffer)
1089 (goto-char (point-max))
1090 (insert (format "%s %s\n"
1091 (if (eq (epg-context-protocol context) 'CMS)
1092 epg-gpgsm-program
1093 epg-gpg-program)
1094 (mapconcat #'identity args " ")))))
1095 (with-current-buffer buffer
1096 (if (fboundp 'set-buffer-multibyte)
1097 (set-buffer-multibyte nil))
1098 (make-local-variable 'epg-last-status)
1099 (setq epg-last-status nil)
1100 (make-local-variable 'epg-read-point)
1101 (setq epg-read-point (point-min))
1102 (make-local-variable 'epg-process-filter-running)
1103 (setq epg-process-filter-running nil)
1104 (make-local-variable 'epg-pending-status-list)
1105 (setq epg-pending-status-list nil)
1106 (make-local-variable 'epg-key-id)
1107 (setq epg-key-id nil)
1108 (make-local-variable 'epg-context)
1109 (setq epg-context context))
1110 (unwind-protect
1111 (progn
1112 (set-default-file-modes 448)
1113 (setq process
1114 (apply #'start-process "epg" buffer
1115 (if (eq (epg-context-protocol context) 'CMS)
1116 epg-gpgsm-program
1117 epg-gpg-program)
1118 args)))
1119 (set-default-file-modes orig-mode))
1120 (set-process-filter process #'epg--process-filter)
1121 (epg-context-set-process context process)))
1123 (defun epg--process-filter (process input)
1124 (if epg-debug
1125 (save-excursion
1126 (unless epg-debug-buffer
1127 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
1128 (set-buffer epg-debug-buffer)
1129 (goto-char (point-max))
1130 (insert input)))
1131 (if (buffer-live-p (process-buffer process))
1132 (save-excursion
1133 (set-buffer (process-buffer process))
1134 (goto-char (point-max))
1135 (insert input)
1136 (unless epg-process-filter-running
1137 (unwind-protect
1138 (progn
1139 (setq epg-process-filter-running t)
1140 (goto-char epg-read-point)
1141 (beginning-of-line)
1142 (while (looking-at ".*\n") ;the input line finished
1143 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
1144 (let* ((status (match-string 1))
1145 (string (match-string 2))
1146 (symbol (intern-soft (concat "epg--status-"
1147 status))))
1148 (if (member status epg-pending-status-list)
1149 (setq epg-pending-status-list nil))
1150 (if (and symbol
1151 (fboundp symbol))
1152 (funcall symbol epg-context string))
1153 (setq epg-last-status (cons status string))))
1154 (forward-line)
1155 (setq epg-read-point (point))))
1156 (setq epg-process-filter-running nil))))))
1158 (defun epg-read-output (context)
1159 "Read the output file CONTEXT and return the content as a string."
1160 (with-temp-buffer
1161 (if (fboundp 'set-buffer-multibyte)
1162 (set-buffer-multibyte nil))
1163 (if (file-exists-p (epg-context-output-file context))
1164 (let ((coding-system-for-read 'binary))
1165 (insert-file-contents (epg-context-output-file context))
1166 (buffer-string)))))
1168 (defun epg-wait-for-status (context status-list)
1169 "Wait until one of elements in STATUS-LIST arrives."
1170 (with-current-buffer (process-buffer (epg-context-process context))
1171 (setq epg-pending-status-list status-list)
1172 (while (and (eq (process-status (epg-context-process context)) 'run)
1173 epg-pending-status-list)
1174 (accept-process-output (epg-context-process context) 1))
1175 (if epg-pending-status-list
1176 (epg-context-set-result-for context 'error 'exit))))
1178 (defun epg-wait-for-completion (context)
1179 "Wait until the `epg-gpg-program' process completes."
1180 (while (eq (process-status (epg-context-process context)) 'run)
1181 (accept-process-output (epg-context-process context) 1))
1182 ;; This line is needed to run the process-filter right now.
1183 (sleep-for 0.1))
1185 (defun epg-reset (context)
1186 "Reset the CONTEXT."
1187 (if (and (epg-context-process context)
1188 (buffer-live-p (process-buffer (epg-context-process context))))
1189 (kill-buffer (process-buffer (epg-context-process context))))
1190 (epg-context-set-process context nil))
1192 (defun epg-delete-output-file (context)
1193 "Delete the output file of CONTEXT."
1194 (if (and (epg-context-output-file context)
1195 (file-exists-p (epg-context-output-file context)))
1196 (delete-file (epg-context-output-file context))))
1198 (eval-and-compile
1199 (if (fboundp 'decode-coding-string)
1200 (defalias 'epg--decode-coding-string 'decode-coding-string)
1201 (defalias 'epg--decode-coding-string 'identity)))
1203 (defun epg--status-USERID_HINT (context string)
1204 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1205 (let* ((key-id (match-string 1 string))
1206 (user-id (match-string 2 string))
1207 (entry (assoc key-id epg-user-id-alist)))
1208 (condition-case nil
1209 (setq user-id (epg--decode-coding-string
1210 (epg--decode-percent-escape user-id)
1211 'utf-8))
1212 (error))
1213 (if entry
1214 (setcdr entry user-id)
1215 (setq epg-user-id-alist (cons (cons key-id user-id)
1216 epg-user-id-alist))))))
1218 (defun epg--status-NEED_PASSPHRASE (context string)
1219 (if (string-match "\\`\\([^ ]+\\)" string)
1220 (setq epg-key-id (match-string 1 string))))
1222 (defun epg--status-NEED_PASSPHRASE_SYM (context string)
1223 (setq epg-key-id 'SYM))
1225 (defun epg--status-NEED_PASSPHRASE_PIN (context string)
1226 (setq epg-key-id 'PIN))
1228 (eval-and-compile
1229 (if (fboundp 'clear-string)
1230 (defalias 'epg--clear-string 'clear-string)
1231 (defun epg--clear-string (string)
1232 (fillarray string 0))))
1234 (eval-and-compile
1235 (if (fboundp 'encode-coding-string)
1236 (defalias 'epg--encode-coding-string 'encode-coding-string)
1237 (defalias 'epg--encode-coding-string 'identity)))
1239 (defun epg--status-GET_HIDDEN (context string)
1240 (when (and epg-key-id
1241 (string-match "\\`passphrase\\." string))
1242 (unless (epg-context-passphrase-callback context)
1243 (error "passphrase-callback not set"))
1244 (let (inhibit-quit
1245 passphrase
1246 passphrase-with-new-line
1247 encoded-passphrase-with-new-line)
1248 (unwind-protect
1249 (condition-case nil
1250 (progn
1251 (setq passphrase
1252 (funcall
1253 (car (epg-context-passphrase-callback context))
1254 context
1255 epg-key-id
1256 (cdr (epg-context-passphrase-callback context))))
1257 (when passphrase
1258 (setq passphrase-with-new-line (concat passphrase "\n"))
1259 (epg--clear-string passphrase)
1260 (setq passphrase nil)
1261 (if epg-passphrase-coding-system
1262 (progn
1263 (setq encoded-passphrase-with-new-line
1264 (epg--encode-coding-string
1265 passphrase-with-new-line
1266 (coding-system-change-eol-conversion
1267 epg-passphrase-coding-system 'unix)))
1268 (epg--clear-string passphrase-with-new-line)
1269 (setq passphrase-with-new-line nil))
1270 (setq encoded-passphrase-with-new-line
1271 passphrase-with-new-line
1272 passphrase-with-new-line nil))
1273 (process-send-string (epg-context-process context)
1274 encoded-passphrase-with-new-line)))
1275 (quit
1276 (epg-context-set-result-for
1277 context 'error
1278 (cons '(quit)
1279 (epg-context-result-for context 'error)))
1280 (delete-process (epg-context-process context))))
1281 (if passphrase
1282 (epg--clear-string passphrase))
1283 (if passphrase-with-new-line
1284 (epg--clear-string passphrase-with-new-line))
1285 (if encoded-passphrase-with-new-line
1286 (epg--clear-string encoded-passphrase-with-new-line))))))
1288 (defun epg--prompt-GET_BOOL (context string)
1289 (let ((entry (assoc string epg-prompt-alist)))
1290 (y-or-n-p (if entry (cdr entry) (concat string "? ")))))
1292 (defun epg--prompt-GET_BOOL-untrusted_key.override (context string)
1293 (y-or-n-p (if (and (equal (car epg-last-status) "USERID_HINT")
1294 (string-match "\\`\\([^ ]+\\) \\(.*\\)"
1295 (cdr epg-last-status)))
1296 (let* ((key-id (match-string 1 (cdr epg-last-status)))
1297 (user-id (match-string 2 (cdr epg-last-status)))
1298 (entry (assoc key-id epg-user-id-alist)))
1299 (if entry
1300 (setq user-id (cdr entry)))
1301 (format "Untrusted key %s %s. Use anyway? " key-id user-id))
1302 "Use untrusted key anyway? ")))
1304 (defun epg--status-GET_BOOL (context string)
1305 (let (inhibit-quit)
1306 (condition-case nil
1307 (if (funcall (or (intern-soft (concat "epg--prompt-GET_BOOL-" string))
1308 #'epg--prompt-GET_BOOL)
1309 context string)
1310 (process-send-string (epg-context-process context) "y\n")
1311 (process-send-string (epg-context-process context) "n\n"))
1312 (quit
1313 (epg-context-set-result-for
1314 context 'error
1315 (cons '(quit)
1316 (epg-context-result-for context 'error)))
1317 (delete-process (epg-context-process context))))))
1319 (defun epg--status-GET_LINE (context string)
1320 (let ((entry (assoc string epg-prompt-alist))
1321 inhibit-quit)
1322 (condition-case nil
1323 (process-send-string (epg-context-process context)
1324 (concat (read-string
1325 (if entry
1326 (cdr entry)
1327 (concat string ": ")))
1328 "\n"))
1329 (quit
1330 (epg-context-set-result-for
1331 context 'error
1332 (cons '(quit)
1333 (epg-context-result-for context 'error)))
1334 (delete-process (epg-context-process context))))))
1336 (defun epg--status-*SIG (context status string)
1337 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1338 (let* ((key-id (match-string 1 string))
1339 (user-id (match-string 2 string))
1340 (entry (assoc key-id epg-user-id-alist)))
1341 (epg-context-set-result-for
1342 context
1343 'verify
1344 (cons (epg-make-signature status key-id)
1345 (epg-context-result-for context 'verify)))
1346 (condition-case nil
1347 (if (eq (epg-context-protocol context) 'CMS)
1348 (setq user-id (epg-dn-from-string user-id))
1349 (setq user-id (epg--decode-coding-string
1350 (epg--decode-percent-escape user-id)
1351 'utf-8)))
1352 (error))
1353 (if entry
1354 (setcdr entry user-id)
1355 (setq epg-user-id-alist
1356 (cons (cons key-id user-id) epg-user-id-alist))))
1357 (epg-context-set-result-for
1358 context
1359 'verify
1360 (cons (epg-make-signature status)
1361 (epg-context-result-for context 'verify)))))
1363 (defun epg--status-GOODSIG (context string)
1364 (epg--status-*SIG context 'good string))
1366 (defun epg--status-EXPSIG (context string)
1367 (epg--status-*SIG context 'expired string))
1369 (defun epg--status-EXPKEYSIG (context string)
1370 (epg--status-*SIG context 'expired-key string))
1372 (defun epg--status-REVKEYSIG (context string)
1373 (epg--status-*SIG context 'revoked-key string))
1375 (defun epg--status-BADSIG (context string)
1376 (epg--status-*SIG context 'bad string))
1378 (defun epg--status-NO_PUBKEY (context string)
1379 (let ((signature (car (epg-context-result-for context 'verify))))
1380 (if (and signature
1381 (eq (epg-signature-status signature) 'error)
1382 (equal (epg-signature-key-id signature) string))
1383 (epg-signature-set-status signature 'no-pubkey))))
1385 (defun epg--time-from-seconds (seconds)
1386 (let ((number-seconds (string-to-number (concat seconds ".0"))))
1387 (cons (floor (/ number-seconds 65536))
1388 (floor (mod number-seconds 65536)))))
1390 (defun epg--status-ERRSIG (context string)
1391 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1392 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
1393 string)
1394 (let ((signature (epg-make-signature 'error)))
1395 (epg-context-set-result-for
1396 context
1397 'verify
1398 (cons signature
1399 (epg-context-result-for context 'verify)))
1400 (epg-signature-set-key-id
1401 signature
1402 (match-string 1 string))
1403 (epg-signature-set-pubkey-algorithm
1404 signature
1405 (string-to-number (match-string 2 string)))
1406 (epg-signature-set-digest-algorithm
1407 signature
1408 (string-to-number (match-string 3 string)))
1409 (epg-signature-set-class
1410 signature
1411 (string-to-number (match-string 4 string) 16))
1412 (epg-signature-set-creation-time
1413 signature
1414 (epg--time-from-seconds (match-string 5 string))))))
1416 (defun epg--status-VALIDSIG (context string)
1417 (let ((signature (car (epg-context-result-for context 'verify))))
1418 (when (and signature
1419 (eq (epg-signature-status signature) 'good)
1420 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
1421 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
1422 \\(.*\\)"
1423 string))
1424 (epg-signature-set-fingerprint
1425 signature
1426 (match-string 1 string))
1427 (epg-signature-set-creation-time
1428 signature
1429 (epg--time-from-seconds (match-string 2 string)))
1430 (unless (equal (match-string 3 string) "0")
1431 (epg-signature-set-expiration-time
1432 signature
1433 (epg--time-from-seconds (match-string 3 string))))
1434 (epg-signature-set-version
1435 signature
1436 (string-to-number (match-string 4 string)))
1437 (epg-signature-set-pubkey-algorithm
1438 signature
1439 (string-to-number (match-string 5 string)))
1440 (epg-signature-set-digest-algorithm
1441 signature
1442 (string-to-number (match-string 6 string)))
1443 (epg-signature-set-class
1444 signature
1445 (string-to-number (match-string 7 string) 16)))))
1447 (defun epg--status-TRUST_UNDEFINED (context string)
1448 (let ((signature (car (epg-context-result-for context 'verify))))
1449 (if (and signature
1450 (eq (epg-signature-status signature) 'good))
1451 (epg-signature-set-validity signature 'undefined))))
1453 (defun epg--status-TRUST_NEVER (context string)
1454 (let ((signature (car (epg-context-result-for context 'verify))))
1455 (if (and signature
1456 (eq (epg-signature-status signature) 'good))
1457 (epg-signature-set-validity signature 'never))))
1459 (defun epg--status-TRUST_MARGINAL (context string)
1460 (let ((signature (car (epg-context-result-for context 'verify))))
1461 (if (and signature
1462 (eq (epg-signature-status signature) 'marginal))
1463 (epg-signature-set-validity signature 'marginal))))
1465 (defun epg--status-TRUST_FULLY (context string)
1466 (let ((signature (car (epg-context-result-for context 'verify))))
1467 (if (and signature
1468 (eq (epg-signature-status signature) 'good))
1469 (epg-signature-set-validity signature 'full))))
1471 (defun epg--status-TRUST_ULTIMATE (context string)
1472 (let ((signature (car (epg-context-result-for context 'verify))))
1473 (if (and signature
1474 (eq (epg-signature-status signature) 'good))
1475 (epg-signature-set-validity signature 'ultimate))))
1477 (defun epg--status-NOTATION_NAME (context string)
1478 (let ((signature (car (epg-context-result-for context 'verify))))
1479 (if signature
1480 (epg-signature-set-notations
1481 signature
1482 (cons (epg-make-sig-notation string nil t nil)
1483 (epg-sig-notations signature))))))
1485 (defun epg--status-NOTATION_DATA (context string)
1486 (let ((signature (car (epg-context-result-for context 'verify)))
1487 notation)
1488 (if (and signature
1489 (setq notation (car (epg-sig-notations signature))))
1490 (epg-sig-notation-set-value notation string))))
1492 (defun epg--status-POLICY_URL (context string)
1493 (let ((signature (car (epg-context-result-for context 'verify))))
1494 (if signature
1495 (epg-signature-set-notations
1496 signature
1497 (cons (epg-make-sig-notation nil string t nil)
1498 (epg-sig-notations signature))))))
1500 (defun epg--status-PROGRESS (context string)
1501 (if (and (epg-context-progress-callback context)
1502 (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1503 string))
1504 (funcall (car (epg-context-progress-callback context))
1505 context
1506 (match-string 1 string)
1507 (match-string 2 string)
1508 (string-to-number (match-string 3 string))
1509 (string-to-number (match-string 4 string))
1510 (cdr (epg-context-progress-callback context)))))
1512 (defun epg--status-ENC_TO (context string)
1513 (if (string-match "\\`\\([0-9A-Za-z]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
1514 (epg-context-set-result-for
1515 context 'encrypted-to
1516 (cons (list (match-string 1 string)
1517 (string-to-number (match-string 2 string))
1518 (string-to-number (match-string 3 string)))
1519 (epg-context-result-for context 'encrypted-to)))))
1521 (defun epg--status-DECRYPTION_FAILED (context string)
1522 (epg-context-set-result-for context 'decryption-failed t))
1524 (defun epg--status-DECRYPTION_OKAY (context string)
1525 (epg-context-set-result-for context 'decryption-okay t))
1527 (defun epg--status-NODATA (context string)
1528 (epg-context-set-result-for
1529 context 'error
1530 (cons (cons 'no-data (string-to-number string))
1531 (epg-context-result-for context 'error))))
1533 (defun epg--status-UNEXPECTED (context string)
1534 (epg-context-set-result-for
1535 context 'error
1536 (cons (cons 'unexpected (string-to-number string))
1537 (epg-context-result-for context 'error))))
1539 (defun epg--status-KEYEXPIRED (context string)
1540 (epg-context-set-result-for
1541 context 'error
1542 (cons (list 'key-expired (cons 'expiration-time
1543 (epg--time-from-seconds string)))
1544 (epg-context-result-for context 'error))))
1546 (defun epg--status-KEYREVOKED (context string)
1547 (epg-context-set-result-for
1548 context 'error
1549 (cons '(key-revoked)
1550 (epg-context-result-for context 'error))))
1552 (defun epg--status-BADARMOR (context string)
1553 (epg-context-set-result-for
1554 context 'error
1555 (cons '(bad-armor)
1556 (epg-context-result-for context 'error))))
1558 (defun epg--status-INV_RECP (context string)
1559 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1560 (epg-context-set-result-for
1561 context 'error
1562 (cons (list 'invalid-recipient
1563 (cons 'reason
1564 (string-to-number (match-string 1 string)))
1565 (cons 'requested-recipient
1566 (match-string 2 string)))
1567 (epg-context-result-for context 'error)))))
1569 (defun epg--status-NO_RECP (context string)
1570 (epg-context-set-result-for
1571 context 'error
1572 (cons '(no-recipients)
1573 (epg-context-result-for context 'error))))
1575 (defun epg--status-DELETE_PROBLEM (context string)
1576 (if (string-match "\\`\\([0-9]+\\)" string)
1577 (epg-context-set-result-for
1578 context 'error
1579 (cons (cons 'delete-problem
1580 (string-to-number (match-string 1 string)))
1581 (epg-context-result-for context 'error)))))
1583 (defun epg--status-SIG_CREATED (context string)
1584 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1585 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1586 (epg-context-set-result-for
1587 context 'sign
1588 (cons (epg-make-new-signature
1589 (cdr (assq (aref (match-string 1 string) 0)
1590 epg-new-signature-type-alist))
1591 (string-to-number (match-string 2 string))
1592 (string-to-number (match-string 3 string))
1593 (string-to-number (match-string 4 string) 16)
1594 (epg--time-from-seconds (match-string 5 string))
1595 (substring string (match-end 0)))
1596 (epg-context-result-for context 'sign)))))
1598 (defun epg--status-KEY_CREATED (context string)
1599 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1600 (epg-context-set-result-for
1601 context 'generate-key
1602 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1603 (cons 'fingerprint (match-string 2 string)))
1604 (epg-context-result-for context 'generate-key)))))
1606 (defun epg--status-KEY_NOT_CREATED (context string)
1607 (epg-context-set-result-for
1608 context 'error
1609 (cons '(key-not-created)
1610 (epg-context-result-for context 'error))))
1612 (defun epg--status-IMPORTED (context string)
1613 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1614 (let* ((key-id (match-string 1 string))
1615 (user-id (match-string 2 string))
1616 (entry (assoc key-id epg-user-id-alist)))
1617 (condition-case nil
1618 (setq user-id (epg--decode-coding-string
1619 (epg--decode-percent-escape user-id)
1620 'utf-8))
1621 (error))
1622 (if entry
1623 (setcdr entry user-id)
1624 (setq epg-user-id-alist (cons (cons key-id user-id)
1625 epg-user-id-alist))))))
1627 (defun epg--status-IMPORT_OK (context string)
1628 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1629 (let ((reason (string-to-number (match-string 1 string))))
1630 (epg-context-set-result-for
1631 context 'import-status
1632 (cons (epg-make-import-status (if (match-beginning 2)
1633 (match-string 3 string))
1635 (/= (logand reason 1) 0)
1636 (/= (logand reason 2) 0)
1637 (/= (logand reason 4) 0)
1638 (/= (logand reason 8) 0)
1639 (/= (logand reason 16) 0))
1640 (epg-context-result-for context 'import-status))))))
1642 (defun epg--status-IMPORT_PROBLEM (context string)
1643 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1644 (epg-context-set-result-for
1645 context 'import-status
1646 (cons (epg-make-import-status
1647 (if (match-beginning 2)
1648 (match-string 3 string))
1649 (string-to-number (match-string 1 string)))
1650 (epg-context-result-for context 'import-status)))))
1652 (defun epg--status-IMPORT_RES (context string)
1653 (when (string-match "\\`\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1654 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1655 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
1656 (epg-context-set-result-for
1657 context 'import
1658 (epg-make-import-result (string-to-number (match-string 1 string))
1659 (string-to-number (match-string 2 string))
1660 (string-to-number (match-string 3 string))
1661 (string-to-number (match-string 4 string))
1662 (string-to-number (match-string 5 string))
1663 (string-to-number (match-string 6 string))
1664 (string-to-number (match-string 7 string))
1665 (string-to-number (match-string 8 string))
1666 (string-to-number (match-string 9 string))
1667 (string-to-number (match-string 10 string))
1668 (string-to-number (match-string 11 string))
1669 (string-to-number (match-string 12 string))
1670 (string-to-number (match-string 13 string))
1671 (epg-context-result-for context 'import-status)))
1672 (epg-context-set-result-for context 'import-status nil)))
1674 (defun epg-passphrase-callback-function (context key-id handback)
1675 (if (eq key-id 'SYM)
1676 (read-passwd "Passphrase for symmetric encryption: "
1677 (eq (epg-context-operation context) 'encrypt))
1678 (read-passwd
1679 (if (eq key-id 'PIN)
1680 "Passphrase for PIN: "
1681 (let ((entry (assoc key-id epg-user-id-alist)))
1682 (if entry
1683 (format "Passphrase for %s %s: " key-id (cdr entry))
1684 (format "Passphrase for %s: " key-id)))))))
1686 (make-obsolete 'epg-passphrase-callback-function
1687 'epa-passphrase-callback-function "23.1")
1689 (defun epg--list-keys-1 (context name mode)
1690 (let ((args (append (if epg-gpg-home-directory
1691 (list "--homedir" epg-gpg-home-directory))
1692 '("--with-colons" "--no-greeting" "--batch"
1693 "--with-fingerprint" "--with-fingerprint")
1694 (unless (eq (epg-context-protocol context) 'CMS)
1695 '("--fixed-list-mode"))))
1696 (list-keys-option (if (memq mode '(t secret))
1697 "--list-secret-keys"
1698 (if (memq mode '(nil public))
1699 "--list-keys"
1700 "--list-sigs")))
1701 (coding-system-for-read 'binary)
1702 keys string field index)
1703 (if name
1704 (progn
1705 (unless (listp name)
1706 (setq name (list name)))
1707 (while name
1708 (setq args (append args (list list-keys-option (car name)))
1709 name (cdr name))))
1710 (setq args (append args (list list-keys-option))))
1711 (with-temp-buffer
1712 (apply #'call-process
1713 (if (eq (epg-context-protocol context) 'CMS)
1714 epg-gpgsm-program
1715 epg-gpg-program)
1716 nil (list t nil) nil args)
1717 (goto-char (point-min))
1718 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1719 (setq keys (cons (make-vector 15 nil) keys)
1720 string (match-string 0)
1721 index 0
1722 field 0)
1723 (while (eq index
1724 (string-match "\\([^:]+\\)?:" string index))
1725 (setq index (match-end 0))
1726 (aset (car keys) field (match-string 1 string))
1727 (setq field (1+ field))))
1728 (nreverse keys))))
1730 (defun epg--make-sub-key-1 (line)
1731 (epg-make-sub-key
1732 (if (aref line 1)
1733 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1734 (delq nil
1735 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1736 (aref line 11)))
1737 (member (aref line 0) '("sec" "ssb"))
1738 (string-to-number (aref line 3))
1739 (string-to-number (aref line 2))
1740 (aref line 4)
1741 (epg--time-from-seconds (aref line 5))
1742 (if (aref line 6)
1743 (epg--time-from-seconds (aref line 6)))))
1745 ;;;###autoload
1746 (defun epg-list-keys (context &optional name mode)
1747 "Return a list of epg-key objects matched with NAME.
1748 If MODE is nil or 'public, only public keyring should be searched.
1749 If MODE is t or 'secret, only secret keyring should be searched.
1750 Otherwise, only public keyring should be searched and the key
1751 signatures should be included.
1752 NAME is either a string or a list of strings."
1753 (let ((lines (epg--list-keys-1 context name mode))
1754 keys cert pointer pointer-1 index string)
1755 (while lines
1756 (cond
1757 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1758 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1759 keys (cons (epg-make-key
1760 (if (aref (car lines) 8)
1761 (cdr (assq (string-to-char (aref (car lines) 8))
1762 epg-key-validity-alist))))
1763 keys))
1764 (epg-key-set-sub-key-list
1765 (car keys)
1766 (cons (epg--make-sub-key-1 (car lines))
1767 (epg-key-sub-key-list (car keys)))))
1768 ((member (aref (car lines) 0) '("sub" "ssb"))
1769 (epg-key-set-sub-key-list
1770 (car keys)
1771 (cons (epg--make-sub-key-1 (car lines))
1772 (epg-key-sub-key-list (car keys)))))
1773 ((equal (aref (car lines) 0) "uid")
1774 ;; Decode the UID name as a backslash escaped UTF-8 string,
1775 ;; generated by GnuPG/GpgSM.
1776 (setq string (copy-sequence (aref (car lines) 9))
1777 index 0)
1778 (while (string-match "\"" string index)
1779 (setq string (replace-match "\\\"" t t string)
1780 index (1+ (match-end 0))))
1781 (condition-case nil
1782 (setq string (epg--decode-coding-string
1783 (car (read-from-string (concat "\"" string "\"")))
1784 'utf-8))
1785 (error
1786 (setq string (aref (car lines) 9))))
1787 (epg-key-set-user-id-list
1788 (car keys)
1789 (cons (epg-make-user-id
1790 (if (aref (car lines) 1)
1791 (cdr (assq (string-to-char (aref (car lines) 1))
1792 epg-key-validity-alist)))
1793 (if cert
1794 (condition-case nil
1795 (epg-dn-from-string string)
1796 (error string))
1797 string))
1798 (epg-key-user-id-list (car keys)))))
1799 ((equal (aref (car lines) 0) "fpr")
1800 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1801 (aref (car lines) 9)))
1802 ((equal (aref (car lines) 0) "sig")
1803 (epg-user-id-set-signature-list
1804 (car (epg-key-user-id-list (car keys)))
1805 (cons
1806 (epg-make-key-signature
1807 (if (aref (car lines) 1)
1808 (cdr (assq (string-to-char (aref (car lines) 1))
1809 epg-key-validity-alist)))
1810 (string-to-number (aref (car lines) 3))
1811 (aref (car lines) 4)
1812 (epg--time-from-seconds (aref (car lines) 5))
1813 (epg--time-from-seconds (aref (car lines) 6))
1814 (aref (car lines) 9)
1815 (string-to-number (aref (car lines) 10) 16)
1816 (eq (aref (aref (car lines) 10) 2) ?x))
1817 (epg-user-id-signature-list
1818 (car (epg-key-user-id-list (car keys))))))))
1819 (setq lines (cdr lines)))
1820 (setq keys (nreverse keys)
1821 pointer keys)
1822 (while pointer
1823 (epg-key-set-sub-key-list
1824 (car pointer)
1825 (nreverse (epg-key-sub-key-list (car pointer))))
1826 (setq pointer-1 (epg-key-set-user-id-list
1827 (car pointer)
1828 (nreverse (epg-key-user-id-list (car pointer)))))
1829 (while pointer-1
1830 (epg-user-id-set-signature-list
1831 (car pointer-1)
1832 (nreverse (epg-user-id-signature-list (car pointer-1))))
1833 (setq pointer-1 (cdr pointer-1)))
1834 (setq pointer (cdr pointer)))
1835 keys))
1837 (eval-and-compile
1838 (if (fboundp 'make-temp-file)
1839 (defalias 'epg--make-temp-file 'make-temp-file)
1840 (defvar temporary-file-directory)
1841 ;; stolen from poe.el.
1842 (defun epg--make-temp-file (prefix)
1843 "Create a temporary file.
1844 The returned file name (created by appending some random characters at the end
1845 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1846 is guaranteed to point to a newly created empty file.
1847 You can then use `write-region' to write new data into the file."
1848 (let (tempdir tempfile)
1849 (setq prefix (expand-file-name prefix
1850 (if (featurep 'xemacs)
1851 (temp-directory)
1852 temporary-file-directory)))
1853 (unwind-protect
1854 (let (file)
1855 ;; First, create a temporary directory.
1856 (while (condition-case ()
1857 (progn
1858 (setq tempdir (make-temp-name
1859 (concat
1860 (file-name-directory prefix)
1861 "DIR")))
1862 ;; return nil or signal an error.
1863 (make-directory tempdir))
1864 ;; let's try again.
1865 (file-already-exists t)))
1866 (set-file-modes tempdir 448)
1867 ;; Second, create a temporary file in the tempdir.
1868 ;; There *is* a race condition between `make-temp-name'
1869 ;; and `write-region', but we don't care it since we are
1870 ;; in a private directory now.
1871 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1872 (write-region "" nil tempfile nil 'silent)
1873 (set-file-modes tempfile 384)
1874 ;; Finally, make a hard-link from the tempfile.
1875 (while (condition-case ()
1876 (progn
1877 (setq file (make-temp-name prefix))
1878 ;; return nil or signal an error.
1879 (add-name-to-file tempfile file))
1880 ;; let's try again.
1881 (file-already-exists t)))
1882 file)
1883 ;; Cleanup the tempfile.
1884 (and tempfile
1885 (file-exists-p tempfile)
1886 (delete-file tempfile))
1887 ;; Cleanup the tempdir.
1888 (and tempdir
1889 (file-directory-p tempdir)
1890 (delete-directory tempdir)))))))
1892 (defun epg--args-from-sig-notations (notations)
1893 (apply #'nconc
1894 (mapcar
1895 (lambda (notation)
1896 (if (and (epg-sig-notation-name notation)
1897 (not (epg-sig-notation-human-readable notation)))
1898 (error "Unreadable"))
1899 (if (epg-sig-notation-name notation)
1900 (list "--sig-notation"
1901 (if (epg-sig-notation-critical notation)
1902 (concat "!" (epg-sig-notation-name notation)
1903 "=" (epg-sig-notation-value notation))
1904 (concat (epg-sig-notation-name notation)
1905 "=" (epg-sig-notation-value notation))))
1906 (list "--sig-policy-url"
1907 (if (epg-sig-notation-critical notation)
1908 (concat "!" (epg-sig-notation-value notation))
1909 (epg-sig-notation-value notation)))))
1910 notations)))
1912 ;;;###autoload
1913 (defun epg-cancel (context)
1914 (if (buffer-live-p (process-buffer (epg-context-process context)))
1915 (save-excursion
1916 (set-buffer (process-buffer (epg-context-process context)))
1917 (epg-context-set-result-for
1918 epg-context 'error
1919 (cons '(quit)
1920 (epg-context-result-for epg-context 'error)))))
1921 (if (eq (process-status (epg-context-process context)) 'run)
1922 (delete-process (epg-context-process context))))
1924 ;;;###autoload
1925 (defun epg-start-decrypt (context cipher)
1926 "Initiate a decrypt operation on CIPHER.
1927 CIPHER must be a file data object.
1929 If you use this function, you will need to wait for the completion of
1930 `epg-gpg-program' by using `epg-wait-for-completion' and call
1931 `epg-reset' to clear a temporaly output file.
1932 If you are unsure, use synchronous version of this function
1933 `epg-decrypt-file' or `epg-decrypt-string' instead."
1934 (unless (epg-data-file cipher)
1935 (error "Not a file"))
1936 (epg-context-set-operation context 'decrypt)
1937 (epg-context-set-result context nil)
1938 (epg--start context (list "--decrypt" "--" (epg-data-file cipher)))
1939 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1940 (unless (eq (epg-context-protocol context) 'CMS)
1941 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1943 (defun epg--check-error-for-decrypt (context)
1944 (if (epg-context-result-for context 'decryption-failed)
1945 (signal 'epg-error (list "Decryption failed")))
1946 (if (epg-context-result-for context 'no-secret-key)
1947 (signal 'epg-error
1948 (list "No secret key"
1949 (epg-context-result-for context 'no-secret-key))))
1950 (unless (epg-context-result-for context 'decryption-okay)
1951 (let* ((error (epg-context-result-for context 'error)))
1952 (if (assq 'no-data error)
1953 (signal 'epg-error (list "No data")))
1954 (signal 'epg-error (list "Can't decrypt" error)))))
1956 ;;;###autoload
1957 (defun epg-decrypt-file (context cipher plain)
1958 "Decrypt a file CIPHER and store the result to a file PLAIN.
1959 If PLAIN is nil, it returns the result as a string."
1960 (unwind-protect
1961 (progn
1962 (if plain
1963 (epg-context-set-output-file context plain)
1964 (epg-context-set-output-file context
1965 (epg--make-temp-file "epg-output")))
1966 (epg-start-decrypt context (epg-make-data-from-file cipher))
1967 (epg-wait-for-completion context)
1968 (epg--check-error-for-decrypt context)
1969 (unless plain
1970 (epg-read-output context)))
1971 (unless plain
1972 (epg-delete-output-file context))
1973 (epg-reset context)))
1975 ;;;###autoload
1976 (defun epg-decrypt-string (context cipher)
1977 "Decrypt a string CIPHER and return the plain text."
1978 (let ((input-file (epg--make-temp-file "epg-input"))
1979 (coding-system-for-write 'binary))
1980 (unwind-protect
1981 (progn
1982 (write-region cipher nil input-file nil 'quiet)
1983 (epg-context-set-output-file context
1984 (epg--make-temp-file "epg-output"))
1985 (epg-start-decrypt context (epg-make-data-from-file input-file))
1986 (epg-wait-for-completion context)
1987 (epg--check-error-for-decrypt context)
1988 (epg-read-output context))
1989 (epg-delete-output-file context)
1990 (if (file-exists-p input-file)
1991 (delete-file input-file))
1992 (epg-reset context))))
1994 ;;;###autoload
1995 (defun epg-start-verify (context signature &optional signed-text)
1996 "Initiate a verify operation on SIGNATURE.
1997 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1999 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
2000 For a normal or a cleartext signature, SIGNED-TEXT should be nil.
2002 If you use this function, you will need to wait for the completion of
2003 `epg-gpg-program' by using `epg-wait-for-completion' and call
2004 `epg-reset' to clear a temporaly output file.
2005 If you are unsure, use synchronous version of this function
2006 `epg-verify-file' or `epg-verify-string' instead."
2007 (epg-context-set-operation context 'verify)
2008 (epg-context-set-result context nil)
2009 (if signed-text
2010 ;; Detached signature.
2011 (if (epg-data-file signed-text)
2012 (epg--start context (list "--verify" "--" (epg-data-file signature)
2013 (epg-data-file signed-text)))
2014 (epg--start context (list "--verify" "--" (epg-data-file signature)
2015 "-"))
2016 (if (eq (process-status (epg-context-process context)) 'run)
2017 (process-send-string (epg-context-process context)
2018 (epg-data-string signed-text)))
2019 (if (eq (process-status (epg-context-process context)) 'run)
2020 (process-send-eof (epg-context-process context))))
2021 ;; Normal (or cleartext) signature.
2022 (if (epg-data-file signature)
2023 (epg--start context (if (eq (epg-context-protocol context) 'CMS)
2024 (list "--verify" "--" (epg-data-file signature))
2025 (list "--" (epg-data-file signature))))
2026 (epg--start context (if (eq (epg-context-protocol context) 'CMS)
2027 '("--verify" "-")
2028 '("-")))
2029 (if (eq (process-status (epg-context-process context)) 'run)
2030 (process-send-string (epg-context-process context)
2031 (epg-data-string signature)))
2032 (if (eq (process-status (epg-context-process context)) 'run)
2033 (process-send-eof (epg-context-process context))))))
2035 ;;;###autoload
2036 (defun epg-verify-file (context signature &optional signed-text plain)
2037 "Verify a file SIGNATURE.
2038 SIGNED-TEXT and PLAIN are also a file if they are specified.
2040 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
2041 string. For a normal or a cleartext signature, SIGNED-TEXT should be
2042 nil. In the latter case, if PLAIN is specified, the plaintext is
2043 stored into the file after successful verification."
2044 (unwind-protect
2045 (progn
2046 (if plain
2047 (epg-context-set-output-file context plain)
2048 (epg-context-set-output-file context
2049 (epg--make-temp-file "epg-output")))
2050 (if signed-text
2051 (epg-start-verify context
2052 (epg-make-data-from-file signature)
2053 (epg-make-data-from-file signed-text))
2054 (epg-start-verify context
2055 (epg-make-data-from-file signature)))
2056 (epg-wait-for-completion context)
2057 (unless plain
2058 (epg-read-output context)))
2059 (unless plain
2060 (epg-delete-output-file context))
2061 (epg-reset context)))
2063 ;;;###autoload
2064 (defun epg-verify-string (context signature &optional signed-text)
2065 "Verify a string SIGNATURE.
2066 SIGNED-TEXT is a string if it is specified.
2068 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
2069 string. For a normal or a cleartext signature, SIGNED-TEXT should be
2070 nil. In the latter case, this function returns the plaintext after
2071 successful verification."
2072 (let ((coding-system-for-write 'binary)
2073 input-file)
2074 (unwind-protect
2075 (progn
2076 (epg-context-set-output-file context
2077 (epg--make-temp-file "epg-output"))
2078 (if signed-text
2079 (progn
2080 (setq input-file (epg--make-temp-file "epg-signature"))
2081 (write-region signature nil input-file nil 'quiet)
2082 (epg-start-verify context
2083 (epg-make-data-from-file input-file)
2084 (epg-make-data-from-string signed-text)))
2085 (epg-start-verify context (epg-make-data-from-string signature)))
2086 (epg-wait-for-completion context)
2087 (epg-read-output context))
2088 (epg-delete-output-file context)
2089 (if (and input-file
2090 (file-exists-p input-file))
2091 (delete-file input-file))
2092 (epg-reset context))))
2094 ;;;###autoload
2095 (defun epg-start-sign (context plain &optional mode)
2096 "Initiate a sign operation on PLAIN.
2097 PLAIN is a data object.
2099 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2100 If it is nil or 'normal, it makes a normal signature.
2101 Otherwise, it makes a cleartext signature.
2103 If you use this function, you will need to wait for the completion of
2104 `epg-gpg-program' by using `epg-wait-for-completion' and call
2105 `epg-reset' to clear a temporaly output file.
2106 If you are unsure, use synchronous version of this function
2107 `epg-sign-file' or `epg-sign-string' instead."
2108 (epg-context-set-operation context 'sign)
2109 (epg-context-set-result context nil)
2110 (unless (memq mode '(t detached nil normal)) ;i.e. cleartext
2111 (epg-context-set-armor context nil)
2112 (epg-context-set-textmode context nil))
2113 (epg--start context
2114 (append (list (if (memq mode '(t detached))
2115 "--detach-sign"
2116 (if (memq mode '(nil normal))
2117 "--sign"
2118 "--clearsign")))
2119 (apply #'nconc
2120 (mapcar
2121 (lambda (signer)
2122 (list "-u"
2123 (epg-sub-key-id
2124 (car (epg-key-sub-key-list signer)))))
2125 (epg-context-signers context)))
2126 (epg--args-from-sig-notations
2127 (epg-context-sig-notations context))
2128 (if (epg-data-file plain)
2129 (list "--" (epg-data-file plain)))))
2130 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2131 (unless (eq (epg-context-protocol context) 'CMS)
2132 (epg-wait-for-status context '("BEGIN_SIGNING")))
2133 (when (epg-data-string plain)
2134 (if (eq (process-status (epg-context-process context)) 'run)
2135 (process-send-string (epg-context-process context)
2136 (epg-data-string plain)))
2137 (if (eq (process-status (epg-context-process context)) 'run)
2138 (process-send-eof (epg-context-process context)))))
2140 ;;;###autoload
2141 (defun epg-sign-file (context plain signature &optional mode)
2142 "Sign a file PLAIN and store the result to a file SIGNATURE.
2143 If SIGNATURE is nil, it returns the result as a string.
2144 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2145 If it is nil or 'normal, it makes a normal signature.
2146 Otherwise, it makes a cleartext signature."
2147 (unwind-protect
2148 (progn
2149 (if signature
2150 (epg-context-set-output-file context signature)
2151 (epg-context-set-output-file context
2152 (epg--make-temp-file "epg-output")))
2153 (epg-start-sign context (epg-make-data-from-file plain) mode)
2154 (epg-wait-for-completion context)
2155 (unless (epg-context-result-for context 'sign)
2156 (if (epg-context-result-for context 'error)
2157 (error "Sign failed: %S"
2158 (epg-context-result-for context 'error))
2159 (error "Sign failed")))
2160 (unless signature
2161 (epg-read-output context)))
2162 (unless signature
2163 (epg-delete-output-file context))
2164 (epg-reset context)))
2166 ;;;###autoload
2167 (defun epg-sign-string (context plain &optional mode)
2168 "Sign a string PLAIN and return the output as string.
2169 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2170 If it is nil or 'normal, it makes a normal signature.
2171 Otherwise, it makes a cleartext signature."
2172 (let ((input-file
2173 (unless (or (eq (epg-context-protocol context) 'CMS)
2174 (condition-case nil
2175 (progn
2176 (epg-check-configuration (epg-configuration))
2178 (error)))
2179 (epg--make-temp-file "epg-input")))
2180 (coding-system-for-write 'binary))
2181 (unwind-protect
2182 (progn
2183 (epg-context-set-output-file context
2184 (epg--make-temp-file "epg-output"))
2185 (if input-file
2186 (write-region plain nil input-file nil 'quiet))
2187 (epg-start-sign context
2188 (if input-file
2189 (epg-make-data-from-file input-file)
2190 (epg-make-data-from-string plain))
2191 mode)
2192 (epg-wait-for-completion context)
2193 (unless (epg-context-result-for context 'sign)
2194 (if (epg-context-result-for context 'error)
2195 (error "Sign failed: %S"
2196 (epg-context-result-for context 'error))
2197 (error "Sign failed")))
2198 (epg-read-output context))
2199 (epg-delete-output-file context)
2200 (if input-file
2201 (delete-file input-file))
2202 (epg-reset context))))
2204 ;;;###autoload
2205 (defun epg-start-encrypt (context plain recipients
2206 &optional sign always-trust)
2207 "Initiate an encrypt operation on PLAIN.
2208 PLAIN is a data object.
2209 If RECIPIENTS is nil, it performs symmetric encryption.
2211 If you use this function, you will need to wait for the completion of
2212 `epg-gpg-program' by using `epg-wait-for-completion' and call
2213 `epg-reset' to clear a temporaly output file.
2214 If you are unsure, use synchronous version of this function
2215 `epg-encrypt-file' or `epg-encrypt-string' instead."
2216 (epg-context-set-operation context 'encrypt)
2217 (epg-context-set-result context nil)
2218 (epg--start context
2219 (append (if always-trust '("--always-trust"))
2220 (if recipients '("--encrypt") '("--symmetric"))
2221 (if sign '("--sign"))
2222 (if sign
2223 (apply #'nconc
2224 (mapcar
2225 (lambda (signer)
2226 (list "-u"
2227 (epg-sub-key-id
2228 (car (epg-key-sub-key-list
2229 signer)))))
2230 (epg-context-signers context))))
2231 (if sign
2232 (epg--args-from-sig-notations
2233 (epg-context-sig-notations context)))
2234 (apply #'nconc
2235 (mapcar
2236 (lambda (recipient)
2237 (list "-r"
2238 (epg-sub-key-id
2239 (car (epg-key-sub-key-list recipient)))))
2240 recipients))
2241 (if (epg-data-file plain)
2242 (list "--" (epg-data-file plain)))))
2243 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2244 (unless (eq (epg-context-protocol context) 'CMS)
2245 (if sign
2246 (epg-wait-for-status context '("BEGIN_SIGNING"))
2247 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
2248 (when (epg-data-string plain)
2249 (if (eq (process-status (epg-context-process context)) 'run)
2250 (process-send-string (epg-context-process context)
2251 (epg-data-string plain)))
2252 (if (eq (process-status (epg-context-process context)) 'run)
2253 (process-send-eof (epg-context-process context)))))
2255 ;;;###autoload
2256 (defun epg-encrypt-file (context plain recipients
2257 cipher &optional sign always-trust)
2258 "Encrypt a file PLAIN and store the result to a file CIPHER.
2259 If CIPHER is nil, it returns the result as a string.
2260 If RECIPIENTS is nil, it performs symmetric encryption."
2261 (unwind-protect
2262 (progn
2263 (if cipher
2264 (epg-context-set-output-file context cipher)
2265 (epg-context-set-output-file context
2266 (epg--make-temp-file "epg-output")))
2267 (epg-start-encrypt context (epg-make-data-from-file plain)
2268 recipients sign always-trust)
2269 (epg-wait-for-completion context)
2270 (if (and sign
2271 (not (epg-context-result-for context 'sign)))
2272 (if (epg-context-result-for context 'error)
2273 (error "Sign failed: %S"
2274 (epg-context-result-for context 'error))
2275 (error "Sign failed")))
2276 (if (epg-context-result-for context 'error)
2277 (error "Encrypt failed: %S"
2278 (epg-context-result-for context 'error)))
2279 (unless cipher
2280 (epg-read-output context)))
2281 (unless cipher
2282 (epg-delete-output-file context))
2283 (epg-reset context)))
2285 ;;;###autoload
2286 (defun epg-encrypt-string (context plain recipients
2287 &optional sign always-trust)
2288 "Encrypt a string PLAIN.
2289 If RECIPIENTS is nil, it performs symmetric encryption."
2290 (let ((input-file
2291 (unless (or (not sign)
2292 (eq (epg-context-protocol context) 'CMS)
2293 (condition-case nil
2294 (progn
2295 (epg-check-configuration (epg-configuration))
2297 (error)))
2298 (epg--make-temp-file "epg-input")))
2299 (coding-system-for-write 'binary))
2300 (unwind-protect
2301 (progn
2302 (epg-context-set-output-file context
2303 (epg--make-temp-file "epg-output"))
2304 (if input-file
2305 (write-region plain nil input-file nil 'quiet))
2306 (epg-start-encrypt context
2307 (if input-file
2308 (epg-make-data-from-file input-file)
2309 (epg-make-data-from-string plain))
2310 recipients sign always-trust)
2311 (epg-wait-for-completion context)
2312 (if (and sign
2313 (not (epg-context-result-for context 'sign)))
2314 (if (epg-context-result-for context 'error)
2315 (error "Sign failed: %S"
2316 (epg-context-result-for context 'error))
2317 (error "Sign failed")))
2318 (if (epg-context-result-for context 'error)
2319 (error "Encrypt failed: %S"
2320 (epg-context-result-for context 'error)))
2321 (epg-read-output context))
2322 (epg-delete-output-file context)
2323 (if input-file
2324 (delete-file input-file))
2325 (epg-reset context))))
2327 ;;;###autoload
2328 (defun epg-start-export-keys (context keys)
2329 "Initiate an export keys operation.
2331 If you use this function, you will need to wait for the completion of
2332 `epg-gpg-program' by using `epg-wait-for-completion' and call
2333 `epg-reset' to clear a temporaly output file.
2334 If you are unsure, use synchronous version of this function
2335 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
2336 (epg-context-set-operation context 'export-keys)
2337 (epg-context-set-result context nil)
2338 (epg--start context (cons "--export"
2339 (mapcar
2340 (lambda (key)
2341 (epg-sub-key-id
2342 (car (epg-key-sub-key-list key))))
2343 keys))))
2345 ;;;###autoload
2346 (defun epg-export-keys-to-file (context keys file)
2347 "Extract public KEYS."
2348 (unwind-protect
2349 (progn
2350 (if file
2351 (epg-context-set-output-file context file)
2352 (epg-context-set-output-file context
2353 (epg--make-temp-file "epg-output")))
2354 (epg-start-export-keys context keys)
2355 (epg-wait-for-completion context)
2356 (if (epg-context-result-for context 'error)
2357 (error "Export keys failed: %S"
2358 (epg-context-result-for context 'error)))
2359 (unless file
2360 (epg-read-output context)))
2361 (unless file
2362 (epg-delete-output-file context))
2363 (epg-reset context)))
2365 ;;;###autoload
2366 (defun epg-export-keys-to-string (context keys)
2367 "Extract public KEYS and return them as a string."
2368 (epg-export-keys-to-file context keys nil))
2370 ;;;###autoload
2371 (defun epg-start-import-keys (context keys)
2372 "Initiate an import keys operation.
2373 KEYS is a data object.
2375 If you use this function, you will need to wait for the completion of
2376 `epg-gpg-program' by using `epg-wait-for-completion' and call
2377 `epg-reset' to clear a temporaly output file.
2378 If you are unsure, use synchronous version of this function
2379 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
2380 (epg-context-set-operation context 'import-keys)
2381 (epg-context-set-result context nil)
2382 (epg--start context (if (epg-data-file keys)
2383 (list "--import" "--" (epg-data-file keys))
2384 (list "--import")))
2385 (when (epg-data-string keys)
2386 (if (eq (process-status (epg-context-process context)) 'run)
2387 (process-send-string (epg-context-process context)
2388 (epg-data-string keys)))
2389 (if (eq (process-status (epg-context-process context)) 'run)
2390 (process-send-eof (epg-context-process context)))))
2392 (defun epg--import-keys-1 (context keys)
2393 (unwind-protect
2394 (progn
2395 (epg-start-import-keys context keys)
2396 (epg-wait-for-completion context)
2397 (if (epg-context-result-for context 'error)
2398 (error "Import keys failed: %S"
2399 (epg-context-result-for context 'error))))
2400 (epg-reset context)))
2402 ;;;###autoload
2403 (defun epg-import-keys-from-file (context keys)
2404 "Add keys from a file KEYS."
2405 (epg--import-keys-1 context (epg-make-data-from-file keys)))
2407 ;;;###autoload
2408 (defun epg-import-keys-from-string (context keys)
2409 "Add keys from a string KEYS."
2410 (epg--import-keys-1 context (epg-make-data-from-string keys)))
2412 ;;;###autoload
2413 (defun epg-start-receive-keys (context key-id-list)
2414 "Initiate a receive key operation.
2415 KEY-ID-LIST is a list of key IDs.
2417 If you use this function, you will need to wait for the completion of
2418 `epg-gpg-program' by using `epg-wait-for-completion' and call
2419 `epg-reset' to clear a temporaly output file.
2420 If you are unsure, use synchronous version of this function
2421 `epg-receive-keys' instead."
2422 (epg-context-set-operation context 'receive-keys)
2423 (epg-context-set-result context nil)
2424 (epg--start context (cons "--recv-keys" key-id-list)))
2426 ;;;###autoload
2427 (defun epg-receive-keys (context keys)
2428 "Add keys from server.
2429 KEYS is a list of key IDs"
2430 (unwind-protect
2431 (progn
2432 (epg-start-receive-keys context keys)
2433 (epg-wait-for-completion context)
2434 (if (epg-context-result-for context 'error)
2435 (error "Receive keys failed: %S"
2436 (epg-context-result-for context 'error))))
2437 (epg-reset context)))
2439 ;;;###autoload
2440 (defalias 'epg-import-keys-from-server 'epg-receive-keys)
2442 ;;;###autoload
2443 (defun epg-start-delete-keys (context keys &optional allow-secret)
2444 "Initiate a delete keys operation.
2446 If you use this function, you will need to wait for the completion of
2447 `epg-gpg-program' by using `epg-wait-for-completion' and call
2448 `epg-reset' to clear a temporaly output file.
2449 If you are unsure, use synchronous version of this function
2450 `epg-delete-keys' instead."
2451 (epg-context-set-operation context 'delete-keys)
2452 (epg-context-set-result context nil)
2453 (epg--start context (cons (if allow-secret
2454 "--delete-secret-key"
2455 "--delete-key")
2456 (mapcar
2457 (lambda (key)
2458 (epg-sub-key-id
2459 (car (epg-key-sub-key-list key))))
2460 keys))))
2462 ;;;###autoload
2463 (defun epg-delete-keys (context keys &optional allow-secret)
2464 "Delete KEYS from the key ring."
2465 (unwind-protect
2466 (progn
2467 (epg-start-delete-keys context keys allow-secret)
2468 (epg-wait-for-completion context)
2469 (let ((entry (assq 'delete-problem
2470 (epg-context-result-for context 'error))))
2471 (if entry
2472 (if (setq entry (assq (cdr entry)
2473 epg-delete-problem-reason-alist))
2474 (error "Delete keys failed: %s" (cdr entry))
2475 (error "Delete keys failed")))))
2476 (epg-reset context)))
2478 ;;;###autoload
2479 (defun epg-start-sign-keys (context keys &optional local)
2480 "Initiate a sign keys operation.
2482 If you use this function, you will need to wait for the completion of
2483 `epg-gpg-program' by using `epg-wait-for-completion' and call
2484 `epg-reset' to clear a temporaly output file.
2485 If you are unsure, use synchronous version of this function
2486 `epg-sign-keys' instead."
2487 (epg-context-set-operation context 'sign-keys)
2488 (epg-context-set-result context nil)
2489 (epg--start context (cons (if local
2490 "--lsign-key"
2491 "--sign-key")
2492 (mapcar
2493 (lambda (key)
2494 (epg-sub-key-id
2495 (car (epg-key-sub-key-list key))))
2496 keys))))
2497 (make-obsolete 'epg-start-sign-keys "do not use." "23.1")
2499 ;;;###autoload
2500 (defun epg-sign-keys (context keys &optional local)
2501 "Sign KEYS from the key ring."
2502 (unwind-protect
2503 (progn
2504 (epg-start-sign-keys context keys local)
2505 (epg-wait-for-completion context)
2506 (if (epg-context-result-for context 'error)
2507 (error "Sign keys failed: %S"
2508 (epg-context-result-for context 'error))))
2509 (epg-reset context)))
2510 (make-obsolete 'epg-sign-keys "do not use." "23.1")
2512 ;;;###autoload
2513 (defun epg-start-generate-key (context parameters)
2514 "Initiate a key generation.
2515 PARAMETERS specifies parameters for the key.
2517 If you use this function, you will need to wait for the completion of
2518 `epg-gpg-program' by using `epg-wait-for-completion' and call
2519 `epg-reset' to clear a temporaly output file.
2520 If you are unsure, use synchronous version of this function
2521 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2522 (epg-context-set-operation context 'generate-key)
2523 (epg-context-set-result context nil)
2524 (if (epg-data-file parameters)
2525 (epg--start context (list "--batch" "--genkey" "--"
2526 (epg-data-file parameters)))
2527 (epg--start context '("--batch" "--genkey"))
2528 (if (eq (process-status (epg-context-process context)) 'run)
2529 (process-send-string (epg-context-process context)
2530 (epg-data-string parameters)))
2531 (if (eq (process-status (epg-context-process context)) 'run)
2532 (process-send-eof (epg-context-process context)))))
2534 ;;;###autoload
2535 (defun epg-generate-key-from-file (context parameters)
2536 "Generate a new key pair.
2537 PARAMETERS is a file which tells how to create the key."
2538 (unwind-protect
2539 (progn
2540 (epg-start-generate-key context (epg-make-data-from-file parameters))
2541 (epg-wait-for-completion context)
2542 (if (epg-context-result-for context 'error)
2543 (error "Generate key failed: %S"
2544 (epg-context-result-for context 'error))))
2545 (epg-reset context)))
2547 ;;;###autoload
2548 (defun epg-generate-key-from-string (context parameters)
2549 "Generate a new key pair.
2550 PARAMETERS is a string which tells how to create the key."
2551 (unwind-protect
2552 (progn
2553 (epg-start-generate-key context (epg-make-data-from-string parameters))
2554 (epg-wait-for-completion context)
2555 (if (epg-context-result-for context 'error)
2556 (error "Generate key failed: %S"
2557 (epg-context-result-for context 'error))))
2558 (epg-reset context)))
2560 (defun epg--decode-percent-escape (string)
2561 (let ((index 0))
2562 (while (string-match "%\\(\\(%\\)\\|\\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2563 string index)
2564 (if (match-beginning 2)
2565 (setq string (replace-match "%" t t string)
2566 index (1- (match-end 0)))
2567 (setq string (replace-match
2568 (string (string-to-number (match-string 3 string) 16))
2569 t t string)
2570 index (- (match-end 0) 2))))
2571 string))
2573 (defun epg--decode-hexstring (string)
2574 (let ((index 0))
2575 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
2576 (setq string (replace-match (string (string-to-number
2577 (match-string 0 string) 16))
2578 t t string)
2579 index (1- (match-end 0))))
2580 string))
2582 (defun epg--decode-quotedstring (string)
2583 (let ((index 0))
2584 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2585 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2586 string index)
2587 (if (match-beginning 2)
2588 (setq string (replace-match "\\2" t nil string)
2589 index (1- (match-end 0)))
2590 (if (match-beginning 3)
2591 (setq string (replace-match (string (string-to-number
2592 (match-string 0 string) 16))
2593 t t string)
2594 index (- (match-end 0) 2)))))
2595 string))
2597 (defun epg-dn-from-string (string)
2598 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2599 The return value is an alist mapping from types to values."
2600 (let ((index 0)
2601 (length (length string))
2602 alist type value group)
2603 (while (< index length)
2604 (if (eq index (string-match "[ \t\n\r]*" string index))
2605 (setq index (match-end 0)))
2606 (if (eq index (string-match
2607 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2608 string index))
2609 (setq type (match-string 1 string)
2610 index (match-end 0))
2611 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2612 string index))
2613 (setq type (match-string 1 string)
2614 index (match-end 0))))
2615 (unless type
2616 (error "Invalid type"))
2617 (if (eq index (string-match
2618 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2619 string index))
2620 (setq index (match-end 0)
2621 value (epg--decode-quotedstring (match-string 0 string)))
2622 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
2623 (setq index (match-end 0)
2624 value (epg--decode-hexstring (match-string 1 string)))
2625 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2626 string index))
2627 (setq index (match-end 0)
2628 value (epg--decode-quotedstring
2629 (match-string 0 string))))))
2630 (if group
2631 (if (stringp (car (car alist)))
2632 (setcar alist (list (cons type value) (car alist)))
2633 (setcar alist (cons (cons type value) (car alist))))
2634 (if (consp (car (car alist)))
2635 (setcar alist (nreverse (car alist))))
2636 (setq alist (cons (cons type value) alist)
2637 type nil
2638 value nil))
2639 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
2640 (setq index (match-end 0)
2641 group (eq (aref string (match-beginning 1)) ?+))))
2642 (nreverse alist)))
2644 (defun epg-decode-dn (alist)
2645 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2646 Type names are resolved using `epg-dn-type-alist'."
2647 (mapconcat
2648 (lambda (rdn)
2649 (if (stringp (car rdn))
2650 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
2651 (if entry
2652 (format "%s=%s" (cdr entry) (cdr rdn))
2653 (format "%s=%s" (car rdn) (cdr rdn))))
2654 (concat "(" (epg-decode-dn rdn) ")")))
2655 alist
2656 ", "))
2658 (provide 'epg)
2660 ;; arch-tag: de8f0acc-1bcf-4c14-a09e-bfffe1b579b7
2661 ;;; epg.el ends here