Item said "vc-update/vc-merge should deal with VC systems that don't
[emacs.git] / lisp / epg.el
blobb7d973266f6e0d9cfd47c0b21281c4701b249a93
1 ;;; epg.el --- the EasyPG Library
2 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
3 ;; 2005, 2006, 2007, 2008 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 (110 . "DUMMY")))
54 ;; from gnupg/include/cipher.h
55 (defconst epg-pubkey-algorithm-alist
56 '((1 . "RSA")
57 (2 . "RSA_E")
58 (3 . "RSA_S")
59 (16 . "ELGAMAL_E")
60 (17 . "DSA")
61 (20 . "ELGAMAL")))
63 ;; from gnupg/include/cipher.h
64 (defconst epg-digest-algorithm-alist
65 '((1 . "MD5")
66 (2 . "SHA1")
67 (3 . "RMD160")
68 (8 . "SHA256")
69 (9 . "SHA384")
70 (10 . "SHA512")))
72 ;; from gnupg/include/cipher.h
73 (defconst epg-compress-algorithm-alist
74 '((0 . "NONE")
75 (1 . "ZIP")
76 (2 . "ZLIB")
77 (3 . "BZIP2")))
79 (defconst epg-invalid-recipients-reason-alist
80 '((0 . "No specific reason given")
81 (1 . "Not Found")
82 (2 . "Ambigious specification")
83 (3 . "Wrong key usage")
84 (4 . "Key revoked")
85 (5 . "Key expired")
86 (6 . "No CRL known")
87 (7 . "CRL too old")
88 (8 . "Policy mismatch")
89 (9 . "Not a secret key")
90 (10 . "Key not trusted")))
92 (defconst epg-delete-problem-reason-alist
93 '((1 . "No such key")
94 (2 . "Must delete secret key first")
95 (3 . "Ambigious specification")))
97 (defconst epg-import-ok-reason-alist
98 '((0 . "Not actually changed")
99 (1 . "Entirely new key")
100 (2 . "New user IDs")
101 (4 . "New signatures")
102 (8 . "New subkeys")
103 (16 . "Contains private key")))
105 (defconst epg-import-problem-reason-alist
106 '((0 . "No specific reason given")
107 (1 . "Invalid Certificate")
108 (2 . "Issuer Certificate missing")
109 (3 . "Certificate Chain too long")
110 (4 . "Error storing certificate")))
112 (defconst epg-no-data-reason-alist
113 '((1 . "No armored data")
114 (2 . "Expected a packet but did not found one")
115 (3 . "Invalid packet found, this may indicate a non OpenPGP message")
116 (4 . "Signature expected but not found")))
118 (defconst epg-unexpected-reason-alist nil)
120 (defvar epg-key-validity-alist
121 '((?o . unknown)
122 (?i . invalid)
123 (?d . disabled)
124 (?r . revoked)
125 (?e . expired)
126 (?- . none)
127 (?q . undefined)
128 (?n . never)
129 (?m . marginal)
130 (?f . full)
131 (?u . ultimate)))
133 (defvar epg-key-capablity-alist
134 '((?e . encrypt)
135 (?s . sign)
136 (?c . certify)
137 (?a . authentication)))
139 (defvar epg-new-signature-type-alist
140 '((?D . detached)
141 (?C . clear)
142 (?S . normal)))
144 (defvar epg-dn-type-alist
145 '(("1.2.840.113549.1.9.1" . "EMail")
146 ("2.5.4.12" . "T")
147 ("2.5.4.42" . "GN")
148 ("2.5.4.4" . "SN")
149 ("0.2.262.1.10.7.20" . "NameDistinguisher")
150 ("2.5.4.16" . "ADDR")
151 ("2.5.4.15" . "BC")
152 ("2.5.4.13" . "D")
153 ("2.5.4.17" . "PostalCode")
154 ("2.5.4.65" . "Pseudo")
155 ("2.5.4.5" . "SerialNumber")))
157 (defvar epg-prompt-alist nil)
159 (put 'epg-error 'error-conditions '(epg-error error))
161 (defun epg-make-data-from-file (file)
162 "Make a data object from FILE."
163 (cons 'epg-data (vector file nil)))
165 (defun epg-make-data-from-string (string)
166 "Make a data object from STRING."
167 (cons 'epg-data (vector nil string)))
169 (defun epg-data-file (data)
170 "Return the file of DATA."
171 (unless (eq (car-safe data) 'epg-data)
172 (signal 'wrong-type-argument (list 'epg-data-p data)))
173 (aref (cdr data) 0))
175 (defun epg-data-string (data)
176 "Return the string of DATA."
177 (unless (eq (car-safe data) 'epg-data)
178 (signal 'wrong-type-argument (list 'epg-data-p data)))
179 (aref (cdr data) 1))
181 (defun epg-make-context (&optional protocol armor textmode include-certs
182 cipher-algorithm digest-algorithm
183 compress-algorithm)
184 "Return a context object."
185 (cons 'epg-context
186 (vector (or protocol 'OpenPGP) armor textmode include-certs
187 cipher-algorithm digest-algorithm compress-algorithm
188 #'epg-passphrase-callback-function
190 nil nil nil nil nil nil)))
192 (defun epg-context-protocol (context)
193 "Return the protocol used within CONTEXT."
194 (unless (eq (car-safe context) 'epg-context)
195 (signal 'wrong-type-argument (list 'epg-context-p context)))
196 (aref (cdr context) 0))
198 (defun epg-context-armor (context)
199 "Return t if the output should be ASCII armored in CONTEXT."
200 (unless (eq (car-safe context) 'epg-context)
201 (signal 'wrong-type-argument (list 'epg-context-p context)))
202 (aref (cdr context) 1))
204 (defun epg-context-textmode (context)
205 "Return t if canonical text mode should be used in CONTEXT."
206 (unless (eq (car-safe context) 'epg-context)
207 (signal 'wrong-type-argument (list 'epg-context-p context)))
208 (aref (cdr context) 2))
210 (defun epg-context-include-certs (context)
211 "Return how many certificates should be included in an S/MIME signed message."
212 (unless (eq (car-safe context) 'epg-context)
213 (signal 'wrong-type-argument (list 'epg-context-p context)))
214 (aref (cdr context) 3))
216 (defun epg-context-cipher-algorithm (context)
217 "Return the cipher algorithm in CONTEXT."
218 (unless (eq (car-safe context) 'epg-context)
219 (signal 'wrong-type-argument (list 'epg-context-p context)))
220 (aref (cdr context) 4))
222 (defun epg-context-digest-algorithm (context)
223 "Return the digest algorithm in CONTEXT."
224 (unless (eq (car-safe context) 'epg-context)
225 (signal 'wrong-type-argument (list 'epg-context-p context)))
226 (aref (cdr context) 5))
228 (defun epg-context-compress-algorithm (context)
229 "Return the compress algorithm in CONTEXT."
230 (unless (eq (car-safe context) 'epg-context)
231 (signal 'wrong-type-argument (list 'epg-context-p context)))
232 (aref (cdr context) 6))
234 (defun epg-context-passphrase-callback (context)
235 "Return the function used to query passphrase."
236 (unless (eq (car-safe context) 'epg-context)
237 (signal 'wrong-type-argument (list 'epg-context-p context)))
238 (aref (cdr context) 7))
240 (defun epg-context-progress-callback (context)
241 "Return the function which handles progress update."
242 (unless (eq (car-safe context) 'epg-context)
243 (signal 'wrong-type-argument (list 'epg-context-p context)))
244 (aref (cdr context) 8))
246 (defun epg-context-signers (context)
247 "Return the list of key-id for signing."
248 (unless (eq (car-safe context) 'epg-context)
249 (signal 'wrong-type-argument (list 'epg-context-p context)))
250 (aref (cdr context) 9))
252 (defun epg-context-sig-notations (context)
253 "Return the list of notations for signing."
254 (unless (eq (car-safe context) 'epg-context)
255 (signal 'wrong-type-argument (list 'epg-context-p context)))
256 (aref (cdr context) 10))
258 (defun epg-context-process (context)
259 "Return the process object of `epg-gpg-program'.
260 This function is for internal use only."
261 (unless (eq (car-safe context) 'epg-context)
262 (signal 'wrong-type-argument (list 'epg-context-p context)))
263 (aref (cdr context) 11))
265 (defun epg-context-output-file (context)
266 "Return the output file of `epg-gpg-program'.
267 This function is for internal use only."
268 (unless (eq (car-safe context) 'epg-context)
269 (signal 'wrong-type-argument (list 'epg-context-p context)))
270 (aref (cdr context) 12))
272 (defun epg-context-result (context)
273 "Return the result of the previous cryptographic operation."
274 (unless (eq (car-safe context) 'epg-context)
275 (signal 'wrong-type-argument (list 'epg-context-p context)))
276 (aref (cdr context) 13))
278 (defun epg-context-operation (context)
279 "Return the name of the current cryptographic operation."
280 (unless (eq (car-safe context) 'epg-context)
281 (signal 'wrong-type-argument (list 'epg-context-p context)))
282 (aref (cdr context) 14))
284 (defun epg-context-set-protocol (context protocol)
285 "Set the protocol used within CONTEXT."
286 (unless (eq (car-safe context) 'epg-context)
287 (signal 'wrong-type-argument (list 'epg-context-p context)))
288 (aset (cdr context) 0 protocol))
290 (defun epg-context-set-armor (context armor)
291 "Specify if the output should be ASCII armored in CONTEXT."
292 (unless (eq (car-safe context) 'epg-context)
293 (signal 'wrong-type-argument (list 'epg-context-p context)))
294 (aset (cdr context) 1 armor))
296 (defun epg-context-set-textmode (context textmode)
297 "Specify if canonical text mode should be used in CONTEXT."
298 (unless (eq (car-safe context) 'epg-context)
299 (signal 'wrong-type-argument (list 'epg-context-p context)))
300 (aset (cdr context) 2 textmode))
302 (defun epg-context-set-include-certs (context include-certs)
303 "Set how many certificates should be included in an S/MIME signed message."
304 (unless (eq (car-safe context) 'epg-context)
305 (signal 'wrong-type-argument (list 'epg-context-p context)))
306 (aset (cdr context) 3 include-certs))
308 (defun epg-context-set-cipher-algorithm (context cipher-algorithm)
309 "Set the cipher algorithm in CONTEXT."
310 (unless (eq (car-safe context) 'epg-context)
311 (signal 'wrong-type-argument (list 'epg-context-p context)))
312 (aset (cdr context) 4 cipher-algorithm))
314 (defun epg-context-set-digest-algorithm (context digest-algorithm)
315 "Set the digest algorithm in CONTEXT."
316 (unless (eq (car-safe context) 'epg-context)
317 (signal 'wrong-type-argument (list 'epg-context-p context)))
318 (aset (cdr context) 5 digest-algorithm))
320 (defun epg-context-set-compress-algorithm (context compress-algorithm)
321 "Set the compress algorithm in CONTEXT."
322 (unless (eq (car-safe context) 'epg-context)
323 (signal 'wrong-type-argument (list 'epg-context-p context)))
324 (aset (cdr context) 6 compress-algorithm))
326 (defun epg-context-set-passphrase-callback (context
327 passphrase-callback)
328 "Set the function used to query passphrase."
329 (unless (eq (car-safe context) 'epg-context)
330 (signal 'wrong-type-argument (list 'epg-context-p context)))
331 (aset (cdr context) 7 passphrase-callback))
333 (defun epg-context-set-progress-callback (context
334 progress-callback)
335 "Set the function which handles progress update.
336 If optional argument HANDBACK is specified, it is passed to PROGRESS-CALLBACK."
337 (unless (eq (car-safe context) 'epg-context)
338 (signal 'wrong-type-argument (list 'epg-context-p context)))
339 (aset (cdr context) 8 progress-callback))
341 (defun epg-context-set-signers (context signers)
342 "Set the list of key-id for signing."
343 (unless (eq (car-safe context) 'epg-context)
344 (signal 'wrong-type-argument (list 'epg-context-p context)))
345 (aset (cdr context) 9 signers))
347 (defun epg-context-set-sig-notations (context notations)
348 "Set the list of notations for signing."
349 (unless (eq (car-safe context) 'epg-context)
350 (signal 'wrong-type-argument (list 'epg-context-p context)))
351 (aset (cdr context) 10 notations))
353 (defun epg-context-set-process (context process)
354 "Set the process object of `epg-gpg-program'.
355 This function is for internal use only."
356 (unless (eq (car-safe context) 'epg-context)
357 (signal 'wrong-type-argument (list 'epg-context-p context)))
358 (aset (cdr context) 11 process))
360 (defun epg-context-set-output-file (context output-file)
361 "Set the output file 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) 12 output-file))
367 (defun epg-context-set-result (context result)
368 "Set the result of the previous cryptographic operation."
369 (unless (eq (car-safe context) 'epg-context)
370 (signal 'wrong-type-argument (list 'epg-context-p context)))
371 (aset (cdr context) 13 result))
373 (defun epg-context-set-operation (context operation)
374 "Set the name of the current cryptographic operation."
375 (unless (eq (car-safe context) 'epg-context)
376 (signal 'wrong-type-argument (list 'epg-context-p context)))
377 (aset (cdr context) 14 operation))
379 (defun epg-make-signature (status &optional key-id)
380 "Return a signature object."
381 (cons 'epg-signature (vector status key-id nil nil nil nil nil nil nil nil
382 nil)))
384 (defun epg-signature-status (signature)
385 "Return the status code of SIGNATURE."
386 (unless (eq (car-safe signature) 'epg-signature)
387 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
388 (aref (cdr signature) 0))
390 (defun epg-signature-key-id (signature)
391 "Return the key-id of SIGNATURE."
392 (unless (eq (car-safe signature) 'epg-signature)
393 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
394 (aref (cdr signature) 1))
396 (defun epg-signature-validity (signature)
397 "Return the validity of SIGNATURE."
398 (unless (eq (car-safe signature) 'epg-signature)
399 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
400 (aref (cdr signature) 2))
402 (defun epg-signature-fingerprint (signature)
403 "Return the fingerprint of SIGNATURE."
404 (unless (eq (car-safe signature) 'epg-signature)
405 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
406 (aref (cdr signature) 3))
408 (defun epg-signature-creation-time (signature)
409 "Return the creation time of SIGNATURE."
410 (unless (eq (car-safe signature) 'epg-signature)
411 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
412 (aref (cdr signature) 4))
414 (defun epg-signature-expiration-time (signature)
415 "Return the expiration time of SIGNATURE."
416 (unless (eq (car-safe signature) 'epg-signature)
417 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
418 (aref (cdr signature) 5))
420 (defun epg-signature-pubkey-algorithm (signature)
421 "Return the public key algorithm of SIGNATURE."
422 (unless (eq (car-safe signature) 'epg-signature)
423 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
424 (aref (cdr signature) 6))
426 (defun epg-signature-digest-algorithm (signature)
427 "Return the digest algorithm of SIGNATURE."
428 (unless (eq (car-safe signature) 'epg-signature)
429 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
430 (aref (cdr signature) 7))
432 (defun epg-signature-class (signature)
433 "Return the class of SIGNATURE."
434 (unless (eq (car-safe signature) 'epg-signature)
435 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
436 (aref (cdr signature) 8))
438 (defun epg-signature-version (signature)
439 "Return the version of SIGNATURE."
440 (unless (eq (car-safe signature) 'epg-signature)
441 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
442 (aref (cdr signature) 9))
444 (defun epg-sig-notations (signature)
445 "Return the list of notations of SIGNATURE."
446 (unless (eq (car-safe signature) 'epg-signature)
447 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
448 (aref (cdr signature) 10))
450 (defun epg-signature-set-status (signature status)
451 "Set the status code of SIGNATURE."
452 (unless (eq (car-safe signature) 'epg-signature)
453 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
454 (aset (cdr signature) 0 status))
456 (defun epg-signature-set-key-id (signature key-id)
457 "Set the key-id of SIGNATURE."
458 (unless (eq (car-safe signature) 'epg-signature)
459 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
460 (aset (cdr signature) 1 key-id))
462 (defun epg-signature-set-validity (signature validity)
463 "Set the validity of SIGNATURE."
464 (unless (eq (car-safe signature) 'epg-signature)
465 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
466 (aset (cdr signature) 2 validity))
468 (defun epg-signature-set-fingerprint (signature fingerprint)
469 "Set the fingerprint of SIGNATURE."
470 (unless (eq (car-safe signature) 'epg-signature)
471 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
472 (aset (cdr signature) 3 fingerprint))
474 (defun epg-signature-set-creation-time (signature creation-time)
475 "Set the creation time of SIGNATURE."
476 (unless (eq (car-safe signature) 'epg-signature)
477 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
478 (aset (cdr signature) 4 creation-time))
480 (defun epg-signature-set-expiration-time (signature expiration-time)
481 "Set the expiration time of SIGNATURE."
482 (unless (eq (car-safe signature) 'epg-signature)
483 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
484 (aset (cdr signature) 5 expiration-time))
486 (defun epg-signature-set-pubkey-algorithm (signature pubkey-algorithm)
487 "Set the public key algorithm of SIGNATURE."
488 (unless (eq (car-safe signature) 'epg-signature)
489 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
490 (aset (cdr signature) 6 pubkey-algorithm))
492 (defun epg-signature-set-digest-algorithm (signature digest-algorithm)
493 "Set the digest algorithm of SIGNATURE."
494 (unless (eq (car-safe signature) 'epg-signature)
495 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
496 (aset (cdr signature) 7 digest-algorithm))
498 (defun epg-signature-set-class (signature class)
499 "Set the class of SIGNATURE."
500 (unless (eq (car-safe signature) 'epg-signature)
501 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
502 (aset (cdr signature) 8 class))
504 (defun epg-signature-set-version (signature version)
505 "Set the version of SIGNATURE."
506 (unless (eq (car-safe signature) 'epg-signature)
507 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
508 (aset (cdr signature) 9 version))
510 (defun epg-signature-set-notations (signature notations)
511 "Set the list of notations of SIGNATURE."
512 (unless (eq (car-safe signature) 'epg-signature)
513 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
514 (aset (cdr signature) 10 notations))
516 (defun epg-make-new-signature (type pubkey-algorithm digest-algorithm
517 class creation-time fingerprint)
518 "Return a new signature object."
519 (cons 'epg-new-signature (vector type pubkey-algorithm digest-algorithm
520 class creation-time fingerprint)))
522 (defun epg-new-signature-type (new-signature)
523 "Return the type of NEW-SIGNATURE."
524 (unless (eq (car-safe new-signature) 'epg-new-signature)
525 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
526 (aref (cdr new-signature) 0))
528 (defun epg-new-signature-pubkey-algorithm (new-signature)
529 "Return the public key algorithm of NEW-SIGNATURE."
530 (unless (eq (car-safe new-signature) 'epg-new-signature)
531 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
532 (aref (cdr new-signature) 1))
534 (defun epg-new-signature-digest-algorithm (new-signature)
535 "Return the digest algorithm of NEW-SIGNATURE."
536 (unless (eq (car-safe new-signature) 'epg-new-signature)
537 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
538 (aref (cdr new-signature) 2))
540 (defun epg-new-signature-class (new-signature)
541 "Return the class of NEW-SIGNATURE."
542 (unless (eq (car-safe new-signature) 'epg-new-signature)
543 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
544 (aref (cdr new-signature) 3))
546 (defun epg-new-signature-creation-time (new-signature)
547 "Return the creation time of NEW-SIGNATURE."
548 (unless (eq (car-safe new-signature) 'epg-new-signature)
549 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
550 (aref (cdr new-signature) 4))
552 (defun epg-new-signature-fingerprint (new-signature)
553 "Return the fingerprint of NEW-SIGNATURE."
554 (unless (eq (car-safe new-signature) 'epg-new-signature)
555 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
556 (aref (cdr new-signature) 5))
558 (defun epg-make-key (owner-trust)
559 "Return a key object."
560 (cons 'epg-key (vector owner-trust nil nil)))
562 (defun epg-key-owner-trust (key)
563 "Return the owner trust of KEY."
564 (unless (eq (car-safe key) 'epg-key)
565 (signal 'wrong-type-argument (list 'epg-key-p key)))
566 (aref (cdr key) 0))
568 (defun epg-key-sub-key-list (key)
569 "Return the sub key list of KEY."
570 (unless (eq (car-safe key) 'epg-key)
571 (signal 'wrong-type-argument (list 'epg-key-p key)))
572 (aref (cdr key) 1))
574 (defun epg-key-user-id-list (key)
575 "Return the user ID list of KEY."
576 (unless (eq (car-safe key) 'epg-key)
577 (signal 'wrong-type-argument (list 'epg-key-p key)))
578 (aref (cdr key) 2))
580 (defun epg-key-set-sub-key-list (key sub-key-list)
581 "Set the sub key list of KEY."
582 (unless (eq (car-safe key) 'epg-key)
583 (signal 'wrong-type-argument (list 'epg-key-p key)))
584 (aset (cdr key) 1 sub-key-list))
586 (defun epg-key-set-user-id-list (key user-id-list)
587 "Set the user ID list of KEY."
588 (unless (eq (car-safe key) 'epg-key)
589 (signal 'wrong-type-argument (list 'epg-key-p key)))
590 (aset (cdr key) 2 user-id-list))
592 (defun epg-make-sub-key (validity capability secret-p algorithm length id
593 creation-time expiration-time)
594 "Return a sub key object."
595 (cons 'epg-sub-key
596 (vector validity capability secret-p algorithm length id creation-time
597 expiration-time nil)))
599 (defun epg-sub-key-validity (sub-key)
600 "Return the validity of SUB-KEY."
601 (unless (eq (car-safe sub-key) 'epg-sub-key)
602 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
603 (aref (cdr sub-key) 0))
605 (defun epg-sub-key-capability (sub-key)
606 "Return the capability of SUB-KEY."
607 (unless (eq (car-safe sub-key) 'epg-sub-key)
608 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
609 (aref (cdr sub-key) 1))
611 (defun epg-sub-key-secret-p (sub-key)
612 "Return non-nil if SUB-KEY is a secret key."
613 (unless (eq (car-safe sub-key) 'epg-sub-key)
614 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
615 (aref (cdr sub-key) 2))
617 (defun epg-sub-key-algorithm (sub-key)
618 "Return the algorithm of SUB-KEY."
619 (unless (eq (car-safe sub-key) 'epg-sub-key)
620 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
621 (aref (cdr sub-key) 3))
623 (defun epg-sub-key-length (sub-key)
624 "Return the length of SUB-KEY."
625 (unless (eq (car-safe sub-key) 'epg-sub-key)
626 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
627 (aref (cdr sub-key) 4))
629 (defun epg-sub-key-id (sub-key)
630 "Return the ID of SUB-KEY."
631 (unless (eq (car-safe sub-key) 'epg-sub-key)
632 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
633 (aref (cdr sub-key) 5))
635 (defun epg-sub-key-creation-time (sub-key)
636 "Return the creation time of SUB-KEY."
637 (unless (eq (car-safe sub-key) 'epg-sub-key)
638 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
639 (aref (cdr sub-key) 6))
641 (defun epg-sub-key-expiration-time (sub-key)
642 "Return the expiration time of SUB-KEY."
643 (unless (eq (car-safe sub-key) 'epg-sub-key)
644 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
645 (aref (cdr sub-key) 7))
647 (defun epg-sub-key-fingerprint (sub-key)
648 "Return the fingerprint of SUB-KEY."
649 (unless (eq (car-safe sub-key) 'epg-sub-key)
650 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
651 (aref (cdr sub-key) 8))
653 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
654 "Set the fingerprint of SUB-KEY.
655 This function is for internal use only."
656 (unless (eq (car-safe sub-key) 'epg-sub-key)
657 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
658 (aset (cdr sub-key) 8 fingerprint))
660 (defun epg-make-user-id (validity string)
661 "Return a user ID object."
662 (cons 'epg-user-id (vector validity string nil)))
664 (defun epg-user-id-validity (user-id)
665 "Return the validity of USER-ID."
666 (unless (eq (car-safe user-id) 'epg-user-id)
667 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
668 (aref (cdr user-id) 0))
670 (defun epg-user-id-string (user-id)
671 "Return the name of USER-ID."
672 (unless (eq (car-safe user-id) 'epg-user-id)
673 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
674 (aref (cdr user-id) 1))
676 (defun epg-user-id-signature-list (user-id)
677 "Return the signature list of USER-ID."
678 (unless (eq (car-safe user-id) 'epg-user-id)
679 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
680 (aref (cdr user-id) 2))
682 (defun epg-user-id-set-signature-list (user-id signature-list)
683 "Set the signature list of USER-ID."
684 (unless (eq (car-safe user-id) 'epg-user-id)
685 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
686 (aset (cdr user-id) 2 signature-list))
688 (defun epg-make-key-signature (validity pubkey-algorithm key-id creation-time
689 expiration-time user-id class
690 exportable-p)
691 "Return a key signature object."
692 (cons 'epg-key-signature
693 (vector validity pubkey-algorithm key-id creation-time expiration-time
694 user-id class exportable-p)))
696 (defun epg-key-signature-validity (key-signature)
697 "Return the validity of KEY-SIGNATURE."
698 (unless (eq (car-safe key-signature) 'epg-key-signature)
699 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
700 (aref (cdr key-signature) 0))
702 (defun epg-key-signature-pubkey-algorithm (key-signature)
703 "Return the public key algorithm of KEY-SIGNATURE."
704 (unless (eq (car-safe key-signature) 'epg-key-signature)
705 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
706 (aref (cdr key-signature) 1))
708 (defun epg-key-signature-key-id (key-signature)
709 "Return the key-id of KEY-SIGNATURE."
710 (unless (eq (car-safe key-signature) 'epg-key-signature)
711 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
712 (aref (cdr key-signature) 2))
714 (defun epg-key-signature-creation-time (key-signature)
715 "Return the creation time of KEY-SIGNATURE."
716 (unless (eq (car-safe key-signature) 'epg-key-signature)
717 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
718 (aref (cdr key-signature) 3))
720 (defun epg-key-signature-expiration-time (key-signature)
721 "Return the expiration time of KEY-SIGNATURE."
722 (unless (eq (car-safe key-signature) 'epg-key-signature)
723 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
724 (aref (cdr key-signature) 4))
726 (defun epg-key-signature-user-id (key-signature)
727 "Return the user-id of KEY-SIGNATURE."
728 (unless (eq (car-safe key-signature) 'epg-key-signature)
729 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
730 (aref (cdr key-signature) 5))
732 (defun epg-key-signature-class (key-signature)
733 "Return the class of KEY-SIGNATURE."
734 (unless (eq (car-safe key-signature) 'epg-key-signature)
735 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
736 (aref (cdr key-signature) 6))
738 (defun epg-key-signature-exportable-p (key-signature)
739 "Return t if KEY-SIGNATURE is exportable."
740 (unless (eq (car-safe key-signature) 'epg-key-signature)
741 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
742 (aref (cdr key-signature) 7))
744 (defun epg-make-sig-notation (name value &optional human-readable
745 critical)
746 "Return a notation object."
747 (cons 'epg-sig-notation (vector name value human-readable critical)))
749 (defun epg-sig-notation-name (sig-notation)
750 "Return the name of SIG-NOTATION."
751 (unless (eq (car-safe sig-notation) 'epg-sig-notation)
752 (signal 'wrong-type-argument (list 'epg-sig-notation-p
753 sig-notation)))
754 (aref (cdr sig-notation) 0))
756 (defun epg-sig-notation-value (sig-notation)
757 "Return the value 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) 1))
763 (defun epg-sig-notation-human-readable (sig-notation)
764 "Return the human-readable 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) 2))
770 (defun epg-sig-notation-critical (sig-notation)
771 "Return the critical 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) 3))
777 (defun epg-sig-notation-set-value (sig-notation value)
778 "Set the value 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 (aset (cdr sig-notation) 1 value))
784 (defun epg-make-import-status (fingerprint &optional reason new user-id
785 signature sub-key secret)
786 "Return an import status object."
787 (cons 'epg-import-status (vector fingerprint reason new user-id signature
788 sub-key secret)))
790 (defun epg-import-status-fingerprint (import-status)
791 "Return the fingerprint of the key that was considered."
792 (unless (eq (car-safe import-status) 'epg-import-status)
793 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
794 (aref (cdr import-status) 0))
796 (defun epg-import-status-reason (import-status)
797 "Return the reason code for import failure."
798 (unless (eq (car-safe import-status) 'epg-import-status)
799 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
800 (aref (cdr import-status) 1))
802 (defun epg-import-status-new (import-status)
803 "Return t if the imported key was new."
804 (unless (eq (car-safe import-status) 'epg-import-status)
805 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
806 (aref (cdr import-status) 2))
808 (defun epg-import-status-user-id (import-status)
809 "Return t if the imported key contained new user IDs."
810 (unless (eq (car-safe import-status) 'epg-import-status)
811 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
812 (aref (cdr import-status) 3))
814 (defun epg-import-status-signature (import-status)
815 "Return t if the imported key contained new signatures."
816 (unless (eq (car-safe import-status) 'epg-import-status)
817 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
818 (aref (cdr import-status) 4))
820 (defun epg-import-status-sub-key (import-status)
821 "Return t if the imported key contained new sub keys."
822 (unless (eq (car-safe import-status) 'epg-import-status)
823 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
824 (aref (cdr import-status) 5))
826 (defun epg-import-status-secret (import-status)
827 "Return t if the imported key contained a secret key."
828 (unless (eq (car-safe import-status) 'epg-import-status)
829 (signal 'wrong-type-argument (list 'epg-import-status-p import-status)))
830 (aref (cdr import-status) 6))
832 (defun epg-make-import-result (considered no-user-id imported imported-rsa
833 unchanged new-user-ids new-sub-keys
834 new-signatures new-revocations
835 secret-read secret-imported
836 secret-unchanged not-imported
837 imports)
838 "Return an import result object."
839 (cons 'epg-import-result (vector considered no-user-id imported imported-rsa
840 unchanged new-user-ids new-sub-keys
841 new-signatures new-revocations secret-read
842 secret-imported secret-unchanged
843 not-imported imports)))
845 (defun epg-import-result-considered (import-result)
846 "Return the total number of considered keys."
847 (unless (eq (car-safe import-result) 'epg-import-result)
848 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
849 (aref (cdr import-result) 0))
851 (defun epg-import-result-no-user-id (import-result)
852 "Return the number of keys without user ID."
853 (unless (eq (car-safe import-result) 'epg-import-result)
854 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
855 (aref (cdr import-result) 1))
857 (defun epg-import-result-imported (import-result)
858 "Return the number of imported keys."
859 (unless (eq (car-safe import-result) 'epg-import-result)
860 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
861 (aref (cdr import-result) 2))
863 (defun epg-import-result-imported-rsa (import-result)
864 "Return the number of imported RSA keys."
865 (unless (eq (car-safe import-result) 'epg-import-result)
866 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
867 (aref (cdr import-result) 3))
869 (defun epg-import-result-unchanged (import-result)
870 "Return the number of unchanged keys."
871 (unless (eq (car-safe import-result) 'epg-import-result)
872 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
873 (aref (cdr import-result) 4))
875 (defun epg-import-result-new-user-ids (import-result)
876 "Return the number of new user IDs."
877 (unless (eq (car-safe import-result) 'epg-import-result)
878 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
879 (aref (cdr import-result) 5))
881 (defun epg-import-result-new-sub-keys (import-result)
882 "Return the number of new sub keys."
883 (unless (eq (car-safe import-result) 'epg-import-result)
884 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
885 (aref (cdr import-result) 6))
887 (defun epg-import-result-new-signatures (import-result)
888 "Return the number of new signatures."
889 (unless (eq (car-safe import-result) 'epg-import-result)
890 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
891 (aref (cdr import-result) 7))
893 (defun epg-import-result-new-revocations (import-result)
894 "Return the number of new revocations."
895 (unless (eq (car-safe import-result) 'epg-import-result)
896 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
897 (aref (cdr import-result) 8))
899 (defun epg-import-result-secret-read (import-result)
900 "Return the total number of secret keys read."
901 (unless (eq (car-safe import-result) 'epg-import-result)
902 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
903 (aref (cdr import-result) 9))
905 (defun epg-import-result-secret-imported (import-result)
906 "Return the number of imported secret keys."
907 (unless (eq (car-safe import-result) 'epg-import-result)
908 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
909 (aref (cdr import-result) 10))
911 (defun epg-import-result-secret-unchanged (import-result)
912 "Return the number of unchanged secret keys."
913 (unless (eq (car-safe import-result) 'epg-import-result)
914 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
915 (aref (cdr import-result) 11))
917 (defun epg-import-result-not-imported (import-result)
918 "Return the number of keys not imported."
919 (unless (eq (car-safe import-result) 'epg-import-result)
920 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
921 (aref (cdr import-result) 12))
923 (defun epg-import-result-imports (import-result)
924 "Return the list of `epg-import-status' objects."
925 (unless (eq (car-safe import-result) 'epg-import-result)
926 (signal 'wrong-type-argument (list 'epg-import-result-p import-result)))
927 (aref (cdr import-result) 13))
929 (defun epg-context-result-for (context name)
930 "Return the result of CONTEXT associated with NAME."
931 (cdr (assq name (epg-context-result context))))
933 (defun epg-context-set-result-for (context name value)
934 "Set the result of CONTEXT associated with NAME to VALUE."
935 (let* ((result (epg-context-result context))
936 (entry (assq name result)))
937 (if entry
938 (setcdr entry value)
939 (epg-context-set-result context (cons (cons name value) result)))))
941 (defun epg-signature-to-string (signature)
942 "Convert SIGNATURE to a human readable string."
943 (let* ((user-id (cdr (assoc (epg-signature-key-id signature)
944 epg-user-id-alist)))
945 (pubkey-algorithm (epg-signature-pubkey-algorithm signature)))
946 (concat
947 (cond ((eq (epg-signature-status signature) 'good)
948 "Good signature from ")
949 ((eq (epg-signature-status signature) 'bad)
950 "Bad signature from ")
951 ((eq (epg-signature-status signature) 'expired)
952 "Expired signature from ")
953 ((eq (epg-signature-status signature) 'expired-key)
954 "Signature made by expired key ")
955 ((eq (epg-signature-status signature) 'revoked-key)
956 "Signature made by revoked key ")
957 ((eq (epg-signature-status signature) 'no-pubkey)
958 "No public key for "))
959 (epg-signature-key-id signature)
960 (if user-id
961 (concat " "
962 (if (stringp user-id)
963 user-id
964 (epg-decode-dn user-id)))
966 (if (epg-signature-validity signature)
967 (format " (trust %s)" (epg-signature-validity signature))
969 (if (epg-signature-creation-time signature)
970 (format-time-string " created at %Y-%m-%dT%T%z"
971 (epg-signature-creation-time signature))
973 (if pubkey-algorithm
974 (concat " using "
975 (or (cdr (assq pubkey-algorithm epg-pubkey-algorithm-alist))
976 (format "(unknown algorithm %d)" pubkey-algorithm)))
977 ""))))
979 (defun epg-verify-result-to-string (verify-result)
980 "Convert VERIFY-RESULT to a human readable string."
981 (mapconcat #'epg-signature-to-string verify-result "\n"))
983 (defun epg-new-signature-to-string (new-signature)
984 "Convert NEW-SIGNATURE to a human readable string."
985 (concat
986 (cond ((eq (epg-new-signature-type new-signature) 'detached)
987 "Detached signature ")
988 ((eq (epg-new-signature-type new-signature) 'clear)
989 "Cleartext signature ")
991 "Signature "))
992 (cdr (assq (epg-new-signature-pubkey-algorithm new-signature)
993 epg-pubkey-algorithm-alist))
995 (cdr (assq (epg-new-signature-digest-algorithm new-signature)
996 epg-digest-algorithm-alist))
998 (format "%02X " (epg-new-signature-class new-signature))
999 (epg-new-signature-fingerprint new-signature)))
1001 (defun epg-import-result-to-string (import-result)
1002 "Convert IMPORT-RESULT to a human readable string."
1003 (concat (format "Total number processed: %d\n"
1004 (epg-import-result-considered import-result))
1005 (if (> (epg-import-result-not-imported import-result) 0)
1006 (format " skipped new keys: %d\n"
1007 (epg-import-result-not-imported import-result)))
1008 (if (> (epg-import-result-no-user-id import-result) 0)
1009 (format " w/o user IDs: %d\n"
1010 (epg-import-result-no-user-id import-result)))
1011 (if (> (epg-import-result-imported import-result) 0)
1012 (concat (format " imported: %d"
1013 (epg-import-result-imported import-result))
1014 (if (> (epg-import-result-imported-rsa import-result) 0)
1015 (format " (RSA: %d)"
1016 (epg-import-result-imported-rsa
1017 import-result)))
1018 "\n"))
1019 (if (> (epg-import-result-unchanged import-result) 0)
1020 (format " unchanged: %d\n"
1021 (epg-import-result-unchanged import-result)))
1022 (if (> (epg-import-result-new-user-ids import-result) 0)
1023 (format " new user IDs: %d\n"
1024 (epg-import-result-new-user-ids import-result)))
1025 (if (> (epg-import-result-new-sub-keys import-result) 0)
1026 (format " new subkeys: %d\n"
1027 (epg-import-result-new-sub-keys import-result)))
1028 (if (> (epg-import-result-new-signatures import-result) 0)
1029 (format " new signatures: %d\n"
1030 (epg-import-result-new-signatures import-result)))
1031 (if (> (epg-import-result-new-revocations import-result) 0)
1032 (format " new key revocations: %d\n"
1033 (epg-import-result-new-revocations import-result)))
1034 (if (> (epg-import-result-secret-read import-result) 0)
1035 (format " secret keys read: %d\n"
1036 (epg-import-result-secret-read import-result)))
1037 (if (> (epg-import-result-secret-imported import-result) 0)
1038 (format " secret keys imported: %d\n"
1039 (epg-import-result-secret-imported import-result)))
1040 (if (> (epg-import-result-secret-unchanged import-result) 0)
1041 (format " secret keys unchanged: %d\n"
1042 (epg-import-result-secret-unchanged import-result)))))
1044 (defun epg--start (context args)
1045 "Start `epg-gpg-program' in a subprocess with given ARGS."
1046 (if (and (epg-context-process context)
1047 (eq (process-status (epg-context-process context)) 'run))
1048 (error "%s is already running in this context"
1049 (if (eq (epg-context-protocol context) 'CMS)
1050 epg-gpgsm-program
1051 epg-gpg-program)))
1052 (let* ((args (append (list "--no-tty"
1053 "--status-fd" "1"
1054 "--yes")
1055 (if (and (not (eq (epg-context-protocol context) 'CMS))
1056 (string-match ":" (or (getenv "GPG_AGENT_INFO")
1057 "")))
1058 '("--use-agent"))
1059 (if (and (not (eq (epg-context-protocol context) 'CMS))
1060 (epg-context-progress-callback context))
1061 '("--enable-progress-filter"))
1062 (if epg-gpg-home-directory
1063 (list "--homedir" epg-gpg-home-directory))
1064 (unless (eq (epg-context-protocol context) 'CMS)
1065 '("--command-fd" "0"))
1066 (if (epg-context-armor context) '("--armor"))
1067 (if (epg-context-textmode context) '("--textmode"))
1068 (if (epg-context-output-file context)
1069 (list "--output" (epg-context-output-file context)))
1070 args))
1071 (coding-system-for-write 'binary)
1072 (coding-system-for-read 'binary)
1073 process-connection-type
1074 (orig-mode (default-file-modes))
1075 (buffer (generate-new-buffer " *epg*"))
1076 process)
1077 (if epg-debug
1078 (save-excursion
1079 (unless epg-debug-buffer
1080 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
1081 (set-buffer epg-debug-buffer)
1082 (goto-char (point-max))
1083 (insert (format "%s %s\n"
1084 (if (eq (epg-context-protocol context) 'CMS)
1085 epg-gpgsm-program
1086 epg-gpg-program)
1087 (mapconcat #'identity args " ")))))
1088 (with-current-buffer buffer
1089 (if (fboundp 'set-buffer-multibyte)
1090 (set-buffer-multibyte nil))
1091 (make-local-variable 'epg-last-status)
1092 (setq epg-last-status nil)
1093 (make-local-variable 'epg-read-point)
1094 (setq epg-read-point (point-min))
1095 (make-local-variable 'epg-process-filter-running)
1096 (setq epg-process-filter-running nil)
1097 (make-local-variable 'epg-pending-status-list)
1098 (setq epg-pending-status-list nil)
1099 (make-local-variable 'epg-key-id)
1100 (setq epg-key-id nil)
1101 (make-local-variable 'epg-context)
1102 (setq epg-context context))
1103 (unwind-protect
1104 (progn
1105 (set-default-file-modes 448)
1106 (setq process
1107 (apply #'start-process "epg" buffer
1108 (if (eq (epg-context-protocol context) 'CMS)
1109 epg-gpgsm-program
1110 epg-gpg-program)
1111 args)))
1112 (set-default-file-modes orig-mode))
1113 (set-process-filter process #'epg--process-filter)
1114 (epg-context-set-process context process)))
1116 (defun epg--process-filter (process input)
1117 (if epg-debug
1118 (save-excursion
1119 (unless epg-debug-buffer
1120 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
1121 (set-buffer epg-debug-buffer)
1122 (goto-char (point-max))
1123 (insert input)))
1124 (if (buffer-live-p (process-buffer process))
1125 (save-excursion
1126 (set-buffer (process-buffer process))
1127 (goto-char (point-max))
1128 (insert input)
1129 (unless epg-process-filter-running
1130 (unwind-protect
1131 (progn
1132 (setq epg-process-filter-running t)
1133 (goto-char epg-read-point)
1134 (beginning-of-line)
1135 (while (looking-at ".*\n") ;the input line finished
1136 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
1137 (let* ((status (match-string 1))
1138 (string (match-string 2))
1139 (symbol (intern-soft (concat "epg--status-"
1140 status))))
1141 (if (member status epg-pending-status-list)
1142 (setq epg-pending-status-list nil))
1143 (if (and symbol
1144 (fboundp symbol))
1145 (funcall symbol epg-context string))
1146 (setq epg-last-status (cons status string))))
1147 (forward-line)
1148 (setq epg-read-point (point))))
1149 (setq epg-process-filter-running nil))))))
1151 (defun epg-read-output (context)
1152 "Read the output file CONTEXT and return the content as a string."
1153 (with-temp-buffer
1154 (if (fboundp 'set-buffer-multibyte)
1155 (set-buffer-multibyte nil))
1156 (if (file-exists-p (epg-context-output-file context))
1157 (let ((coding-system-for-read 'binary))
1158 (insert-file-contents (epg-context-output-file context))
1159 (buffer-string)))))
1161 (defun epg-wait-for-status (context status-list)
1162 "Wait until one of elements in STATUS-LIST arrives."
1163 (with-current-buffer (process-buffer (epg-context-process context))
1164 (setq epg-pending-status-list status-list)
1165 (while (and (eq (process-status (epg-context-process context)) 'run)
1166 epg-pending-status-list)
1167 (accept-process-output (epg-context-process context) 1))))
1169 (defun epg-wait-for-completion (context)
1170 "Wait until the `epg-gpg-program' process completes."
1171 (while (eq (process-status (epg-context-process context)) 'run)
1172 (accept-process-output (epg-context-process context) 1)))
1174 (defun epg-reset (context)
1175 "Reset the CONTEXT."
1176 (if (and (epg-context-process context)
1177 (buffer-live-p (process-buffer (epg-context-process context))))
1178 (kill-buffer (process-buffer (epg-context-process context))))
1179 (epg-context-set-process context nil))
1181 (defun epg-delete-output-file (context)
1182 "Delete the output file of CONTEXT."
1183 (if (and (epg-context-output-file context)
1184 (file-exists-p (epg-context-output-file context)))
1185 (delete-file (epg-context-output-file context))))
1187 (eval-and-compile
1188 (if (fboundp 'decode-coding-string)
1189 (defalias 'epg--decode-coding-string 'decode-coding-string)
1190 (defalias 'epg--decode-coding-string 'identity)))
1192 (defun epg--status-USERID_HINT (context string)
1193 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1194 (let* ((key-id (match-string 1 string))
1195 (user-id (match-string 2 string))
1196 (entry (assoc key-id epg-user-id-alist)))
1197 (condition-case nil
1198 (setq user-id (epg--decode-coding-string
1199 (epg--decode-percent-escape user-id)
1200 'utf-8))
1201 (error))
1202 (if entry
1203 (setcdr entry user-id)
1204 (setq epg-user-id-alist (cons (cons key-id user-id)
1205 epg-user-id-alist))))))
1207 (defun epg--status-NEED_PASSPHRASE (context string)
1208 (if (string-match "\\`\\([^ ]+\\)" string)
1209 (setq epg-key-id (match-string 1 string))))
1211 (defun epg--status-NEED_PASSPHRASE_SYM (context string)
1212 (setq epg-key-id 'SYM))
1214 (defun epg--status-NEED_PASSPHRASE_PIN (context string)
1215 (setq epg-key-id 'PIN))
1217 (eval-and-compile
1218 (if (fboundp 'clear-string)
1219 (defalias 'epg--clear-string 'clear-string)
1220 (defun epg--clear-string (string)
1221 (fillarray string 0))))
1223 (eval-and-compile
1224 (if (fboundp 'encode-coding-string)
1225 (defalias 'epg--encode-coding-string 'encode-coding-string)
1226 (defalias 'epg--encode-coding-string 'identity)))
1228 (defun epg--status-GET_HIDDEN (context string)
1229 (when (and epg-key-id
1230 (string-match "\\`passphrase\\." string))
1231 (unless (epg-context-passphrase-callback context)
1232 (error "passphrase-callback not set"))
1233 (let (inhibit-quit
1234 passphrase
1235 passphrase-with-new-line
1236 encoded-passphrase-with-new-line)
1237 (unwind-protect
1238 (condition-case nil
1239 (progn
1240 (setq passphrase
1241 (funcall
1242 (if (consp (epg-context-passphrase-callback context))
1243 (car (epg-context-passphrase-callback context))
1244 (epg-context-passphrase-callback context))
1245 context
1246 epg-key-id
1247 (if (consp (epg-context-passphrase-callback context))
1248 (cdr (epg-context-passphrase-callback context)))))
1249 (when passphrase
1250 (setq passphrase-with-new-line (concat passphrase "\n"))
1251 (epg--clear-string passphrase)
1252 (setq passphrase nil)
1253 (if epg-passphrase-coding-system
1254 (progn
1255 (setq encoded-passphrase-with-new-line
1256 (epg--encode-coding-string
1257 passphrase-with-new-line
1258 (coding-system-change-eol-conversion
1259 epg-passphrase-coding-system 'unix)))
1260 (epg--clear-string passphrase-with-new-line)
1261 (setq passphrase-with-new-line nil))
1262 (setq encoded-passphrase-with-new-line
1263 passphrase-with-new-line
1264 passphrase-with-new-line nil))
1265 (process-send-string (epg-context-process context)
1266 encoded-passphrase-with-new-line)))
1267 (quit
1268 (epg-context-set-result-for
1269 context 'error
1270 (cons '(quit)
1271 (epg-context-result-for context 'error)))
1272 (delete-process (epg-context-process context))))
1273 (if passphrase
1274 (epg--clear-string passphrase))
1275 (if passphrase-with-new-line
1276 (epg--clear-string passphrase-with-new-line))
1277 (if encoded-passphrase-with-new-line
1278 (epg--clear-string encoded-passphrase-with-new-line))))))
1280 (defun epg--prompt-GET_BOOL (context string)
1281 (let ((entry (assoc string epg-prompt-alist)))
1282 (y-or-n-p (if entry (cdr entry) (concat string "? ")))))
1284 (defun epg--prompt-GET_BOOL-untrusted_key.override (context string)
1285 (y-or-n-p (if (and (equal (car epg-last-status) "USERID_HINT")
1286 (string-match "\\`\\([^ ]+\\) \\(.*\\)"
1287 (cdr epg-last-status)))
1288 (let* ((key-id (match-string 1 (cdr epg-last-status)))
1289 (user-id (match-string 2 (cdr epg-last-status)))
1290 (entry (assoc key-id epg-user-id-alist)))
1291 (if entry
1292 (setq user-id (cdr entry)))
1293 (format "Untrusted key %s %s. Use anyway? " key-id user-id))
1294 "Use untrusted key anyway? ")))
1296 (defun epg--status-GET_BOOL (context string)
1297 (let (inhibit-quit)
1298 (condition-case nil
1299 (if (funcall (or (intern-soft (concat "epg--prompt-GET_BOOL-" string))
1300 #'epg--prompt-GET_BOOL)
1301 context string)
1302 (process-send-string (epg-context-process context) "y\n")
1303 (process-send-string (epg-context-process context) "n\n"))
1304 (quit
1305 (epg-context-set-result-for
1306 context 'error
1307 (cons '(quit)
1308 (epg-context-result-for context 'error)))
1309 (delete-process (epg-context-process context))))))
1311 (defun epg--status-GET_LINE (context string)
1312 (let ((entry (assoc string epg-prompt-alist))
1313 inhibit-quit)
1314 (condition-case nil
1315 (process-send-string (epg-context-process context)
1316 (concat (read-string
1317 (if entry
1318 (cdr entry)
1319 (concat string ": ")))
1320 "\n"))
1321 (quit
1322 (epg-context-set-result-for
1323 context 'error
1324 (cons '(quit)
1325 (epg-context-result-for context 'error)))
1326 (delete-process (epg-context-process context))))))
1328 (defun epg--status-*SIG (context status string)
1329 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1330 (let* ((key-id (match-string 1 string))
1331 (user-id (match-string 2 string))
1332 (entry (assoc key-id epg-user-id-alist)))
1333 (epg-context-set-result-for
1334 context
1335 'verify
1336 (cons (epg-make-signature status key-id)
1337 (epg-context-result-for context 'verify)))
1338 (condition-case nil
1339 (if (eq (epg-context-protocol context) 'CMS)
1340 (setq user-id (epg-dn-from-string user-id))
1341 (setq user-id (epg--decode-coding-string
1342 (epg--decode-percent-escape user-id)
1343 'utf-8)))
1344 (error))
1345 (if entry
1346 (setcdr entry user-id)
1347 (setq epg-user-id-alist
1348 (cons (cons key-id user-id) epg-user-id-alist))))
1349 (epg-context-set-result-for
1350 context
1351 'verify
1352 (cons (epg-make-signature status)
1353 (epg-context-result-for context 'verify)))))
1355 (defun epg--status-GOODSIG (context string)
1356 (epg--status-*SIG context 'good string))
1358 (defun epg--status-EXPSIG (context string)
1359 (epg--status-*SIG context 'expired string))
1361 (defun epg--status-EXPKEYSIG (context string)
1362 (epg--status-*SIG context 'expired-key string))
1364 (defun epg--status-REVKEYSIG (context string)
1365 (epg--status-*SIG context 'revoked-key string))
1367 (defun epg--status-BADSIG (context string)
1368 (epg--status-*SIG context 'bad string))
1370 (defun epg--status-NO_PUBKEY (context string)
1371 (let ((signature (car (epg-context-result-for context 'verify))))
1372 (if (and signature
1373 (eq (epg-signature-status signature) 'error)
1374 (equal (epg-signature-key-id signature) string))
1375 (epg-signature-set-status signature 'no-pubkey))))
1377 (defun epg--time-from-seconds (seconds)
1378 (let ((number-seconds (string-to-number (concat seconds ".0"))))
1379 (cons (floor (/ number-seconds 65536))
1380 (floor (mod number-seconds 65536)))))
1382 (defun epg--status-ERRSIG (context string)
1383 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1384 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
1385 string)
1386 (let ((signature (epg-make-signature 'error)))
1387 (epg-context-set-result-for
1388 context
1389 'verify
1390 (cons signature
1391 (epg-context-result-for context 'verify)))
1392 (epg-signature-set-key-id
1393 signature
1394 (match-string 1 string))
1395 (epg-signature-set-pubkey-algorithm
1396 signature
1397 (string-to-number (match-string 2 string)))
1398 (epg-signature-set-digest-algorithm
1399 signature
1400 (string-to-number (match-string 3 string)))
1401 (epg-signature-set-class
1402 signature
1403 (string-to-number (match-string 4 string) 16))
1404 (epg-signature-set-creation-time
1405 signature
1406 (epg--time-from-seconds (match-string 5 string))))))
1408 (defun epg--status-VALIDSIG (context string)
1409 (let ((signature (car (epg-context-result-for context 'verify))))
1410 (when (and signature
1411 (eq (epg-signature-status signature) 'good)
1412 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
1413 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
1414 \\(.*\\)"
1415 string))
1416 (epg-signature-set-fingerprint
1417 signature
1418 (match-string 1 string))
1419 (epg-signature-set-creation-time
1420 signature
1421 (epg--time-from-seconds (match-string 2 string)))
1422 (unless (equal (match-string 3 string) "0")
1423 (epg-signature-set-expiration-time
1424 signature
1425 (epg--time-from-seconds (match-string 3 string))))
1426 (epg-signature-set-version
1427 signature
1428 (string-to-number (match-string 4 string)))
1429 (epg-signature-set-pubkey-algorithm
1430 signature
1431 (string-to-number (match-string 5 string)))
1432 (epg-signature-set-digest-algorithm
1433 signature
1434 (string-to-number (match-string 6 string)))
1435 (epg-signature-set-class
1436 signature
1437 (string-to-number (match-string 7 string) 16)))))
1439 (defun epg--status-TRUST_UNDEFINED (context string)
1440 (let ((signature (car (epg-context-result-for context 'verify))))
1441 (if (and signature
1442 (eq (epg-signature-status signature) 'good))
1443 (epg-signature-set-validity signature 'undefined))))
1445 (defun epg--status-TRUST_NEVER (context string)
1446 (let ((signature (car (epg-context-result-for context 'verify))))
1447 (if (and signature
1448 (eq (epg-signature-status signature) 'good))
1449 (epg-signature-set-validity signature 'never))))
1451 (defun epg--status-TRUST_MARGINAL (context string)
1452 (let ((signature (car (epg-context-result-for context 'verify))))
1453 (if (and signature
1454 (eq (epg-signature-status signature) 'marginal))
1455 (epg-signature-set-validity signature 'marginal))))
1457 (defun epg--status-TRUST_FULLY (context string)
1458 (let ((signature (car (epg-context-result-for context 'verify))))
1459 (if (and signature
1460 (eq (epg-signature-status signature) 'good))
1461 (epg-signature-set-validity signature 'full))))
1463 (defun epg--status-TRUST_ULTIMATE (context string)
1464 (let ((signature (car (epg-context-result-for context 'verify))))
1465 (if (and signature
1466 (eq (epg-signature-status signature) 'good))
1467 (epg-signature-set-validity signature 'ultimate))))
1469 (defun epg--status-NOTATION_NAME (context string)
1470 (let ((signature (car (epg-context-result-for context 'verify))))
1471 (if signature
1472 (epg-signature-set-notations
1473 signature
1474 (cons (epg-make-sig-notation string nil t nil)
1475 (epg-sig-notations signature))))))
1477 (defun epg--status-NOTATION_DATA (context string)
1478 (let ((signature (car (epg-context-result-for context 'verify)))
1479 notation)
1480 (if (and signature
1481 (setq notation (car (epg-sig-notations signature))))
1482 (epg-sig-notation-set-value notation string))))
1484 (defun epg--status-POLICY_URL (context string)
1485 (let ((signature (car (epg-context-result-for context 'verify))))
1486 (if signature
1487 (epg-signature-set-notations
1488 signature
1489 (cons (epg-make-sig-notation nil string t nil)
1490 (epg-sig-notations signature))))))
1492 (defun epg--status-PROGRESS (context string)
1493 (if (and (epg-context-progress-callback context)
1494 (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1495 string))
1496 (funcall (if (consp (epg-context-progress-callback context))
1497 (car (epg-context-progress-callback context))
1498 (epg-context-progress-callback context))
1499 context
1500 (match-string 1 string)
1501 (match-string 2 string)
1502 (string-to-number (match-string 3 string))
1503 (string-to-number (match-string 4 string))
1504 (if (consp (epg-context-progress-callback context))
1505 (cdr (epg-context-progress-callback context))))))
1507 (defun epg--status-ENC_TO (context string)
1508 (if (string-match "\\`\\([0-9A-Za-z]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
1509 (epg-context-set-result-for
1510 context 'encrypted-to
1511 (cons (list (match-string 1 string)
1512 (string-to-number (match-string 2 string))
1513 (string-to-number (match-string 3 string)))
1514 (epg-context-result-for context 'encrypted-to)))))
1516 (defun epg--status-DECRYPTION_FAILED (context string)
1517 (epg-context-set-result-for context 'decryption-failed t))
1519 (defun epg--status-DECRYPTION_OKAY (context string)
1520 (epg-context-set-result-for context 'decryption-okay t))
1522 (defun epg--status-NODATA (context string)
1523 (epg-context-set-result-for
1524 context 'error
1525 (cons (cons 'no-data (string-to-number string))
1526 (epg-context-result-for context 'error))))
1528 (defun epg--status-UNEXPECTED (context string)
1529 (epg-context-set-result-for
1530 context 'error
1531 (cons (cons 'unexpected (string-to-number string))
1532 (epg-context-result-for context 'error))))
1534 (defun epg--status-KEYEXPIRED (context string)
1535 (epg-context-set-result-for
1536 context 'error
1537 (cons (list 'key-expired (cons 'expiration-time
1538 (epg--time-from-seconds string)))
1539 (epg-context-result-for context 'error))))
1541 (defun epg--status-KEYREVOKED (context string)
1542 (epg-context-set-result-for
1543 context 'error
1544 (cons '(key-revoked)
1545 (epg-context-result-for context 'error))))
1547 (defun epg--status-BADARMOR (context string)
1548 (epg-context-set-result-for
1549 context 'error
1550 (cons '(bad-armor)
1551 (epg-context-result-for context 'error))))
1553 (defun epg--status-INV_RECP (context string)
1554 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1555 (epg-context-set-result-for
1556 context 'error
1557 (cons (list 'invalid-recipient
1558 (cons 'reason
1559 (string-to-number (match-string 1 string)))
1560 (cons 'requested-recipient
1561 (match-string 2 string)))
1562 (epg-context-result-for context 'error)))))
1564 (defun epg--status-NO_RECP (context string)
1565 (epg-context-set-result-for
1566 context 'error
1567 (cons '(no-recipients)
1568 (epg-context-result-for context 'error))))
1570 (defun epg--status-DELETE_PROBLEM (context string)
1571 (if (string-match "\\`\\([0-9]+\\)" string)
1572 (epg-context-set-result-for
1573 context 'error
1574 (cons (cons 'delete-problem
1575 (string-to-number (match-string 1 string)))
1576 (epg-context-result-for context 'error)))))
1578 (defun epg--status-SIG_CREATED (context string)
1579 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1580 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1581 (epg-context-set-result-for
1582 context 'sign
1583 (cons (epg-make-new-signature
1584 (cdr (assq (aref (match-string 1 string) 0)
1585 epg-new-signature-type-alist))
1586 (string-to-number (match-string 2 string))
1587 (string-to-number (match-string 3 string))
1588 (string-to-number (match-string 4 string) 16)
1589 (epg--time-from-seconds (match-string 5 string))
1590 (substring string (match-end 0)))
1591 (epg-context-result-for context 'sign)))))
1593 (defun epg--status-KEY_CREATED (context string)
1594 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1595 (epg-context-set-result-for
1596 context 'generate-key
1597 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1598 (cons 'fingerprint (match-string 2 string)))
1599 (epg-context-result-for context 'generate-key)))))
1601 (defun epg--status-KEY_NOT_CREATED (context string)
1602 (epg-context-set-result-for
1603 context 'error
1604 (cons '(key-not-created)
1605 (epg-context-result-for context 'error))))
1607 (defun epg--status-IMPORTED (context string)
1608 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1609 (let* ((key-id (match-string 1 string))
1610 (user-id (match-string 2 string))
1611 (entry (assoc key-id epg-user-id-alist)))
1612 (condition-case nil
1613 (setq user-id (epg--decode-coding-string
1614 (epg--decode-percent-escape user-id)
1615 'utf-8))
1616 (error))
1617 (if entry
1618 (setcdr entry user-id)
1619 (setq epg-user-id-alist (cons (cons key-id user-id)
1620 epg-user-id-alist))))))
1622 (defun epg--status-IMPORT_OK (context string)
1623 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1624 (let ((reason (string-to-number (match-string 1 string))))
1625 (epg-context-set-result-for
1626 context 'import-status
1627 (cons (epg-make-import-status (if (match-beginning 2)
1628 (match-string 3 string))
1630 (/= (logand reason 1) 0)
1631 (/= (logand reason 2) 0)
1632 (/= (logand reason 4) 0)
1633 (/= (logand reason 8) 0)
1634 (/= (logand reason 16) 0))
1635 (epg-context-result-for context 'import-status))))))
1637 (defun epg--status-IMPORT_PROBLEM (context string)
1638 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1639 (epg-context-set-result-for
1640 context 'import-status
1641 (cons (epg-make-import-status
1642 (if (match-beginning 2)
1643 (match-string 3 string))
1644 (string-to-number (match-string 1 string)))
1645 (epg-context-result-for context 'import-status)))))
1647 (defun epg--status-IMPORT_RES (context string)
1648 (when (string-match "\\`\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1649 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1650 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
1651 (epg-context-set-result-for
1652 context 'import
1653 (epg-make-import-result (string-to-number (match-string 1 string))
1654 (string-to-number (match-string 2 string))
1655 (string-to-number (match-string 3 string))
1656 (string-to-number (match-string 4 string))
1657 (string-to-number (match-string 5 string))
1658 (string-to-number (match-string 6 string))
1659 (string-to-number (match-string 7 string))
1660 (string-to-number (match-string 8 string))
1661 (string-to-number (match-string 9 string))
1662 (string-to-number (match-string 10 string))
1663 (string-to-number (match-string 11 string))
1664 (string-to-number (match-string 12 string))
1665 (string-to-number (match-string 13 string))
1666 (epg-context-result-for context 'import-status)))
1667 (epg-context-set-result-for context 'import-status nil)))
1669 (defun epg-passphrase-callback-function (context key-id handback)
1670 (if (eq key-id 'SYM)
1671 (read-passwd "Passphrase for symmetric encryption: "
1672 (eq (epg-context-operation context) 'encrypt))
1673 (read-passwd
1674 (if (eq key-id 'PIN)
1675 "Passphrase for PIN: "
1676 (let ((entry (assoc key-id epg-user-id-alist)))
1677 (if entry
1678 (format "Passphrase for %s %s: " key-id (cdr entry))
1679 (format "Passphrase for %s: " key-id)))))))
1681 (make-obsolete 'epg-passphrase-callback-function
1682 'epa-passphrase-callback-function)
1684 (defun epg--list-keys-1 (context name mode)
1685 (let ((args (append (if epg-gpg-home-directory
1686 (list "--homedir" epg-gpg-home-directory))
1687 '("--with-colons" "--no-greeting" "--batch"
1688 "--with-fingerprint" "--with-fingerprint")
1689 (unless (eq (epg-context-protocol context) 'CMS)
1690 '("--fixed-list-mode"))))
1691 (list-keys-option (if (memq mode '(t secret))
1692 "--list-secret-keys"
1693 (if (memq mode '(nil public))
1694 "--list-keys"
1695 "--list-sigs")))
1696 (coding-system-for-read 'binary)
1697 keys string field index)
1698 (if name
1699 (progn
1700 (unless (listp name)
1701 (setq name (list name)))
1702 (while name
1703 (setq args (append args (list list-keys-option (car name)))
1704 name (cdr name))))
1705 (setq args (append args (list list-keys-option))))
1706 (with-temp-buffer
1707 (apply #'call-process
1708 (if (eq (epg-context-protocol context) 'CMS)
1709 epg-gpgsm-program
1710 epg-gpg-program)
1711 nil (list t nil) nil args)
1712 (goto-char (point-min))
1713 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1714 (setq keys (cons (make-vector 15 nil) keys)
1715 string (match-string 0)
1716 index 0
1717 field 0)
1718 (while (eq index
1719 (string-match "\\([^:]+\\)?:" string index))
1720 (setq index (match-end 0))
1721 (aset (car keys) field (match-string 1 string))
1722 (setq field (1+ field))))
1723 (nreverse keys))))
1725 (defun epg--make-sub-key-1 (line)
1726 (epg-make-sub-key
1727 (if (aref line 1)
1728 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1729 (delq nil
1730 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1731 (aref line 11)))
1732 (member (aref line 0) '("sec" "ssb"))
1733 (string-to-number (aref line 3))
1734 (string-to-number (aref line 2))
1735 (aref line 4)
1736 (epg--time-from-seconds (aref line 5))
1737 (if (aref line 6)
1738 (epg--time-from-seconds (aref line 6)))))
1740 ;;;###autoload
1741 (defun epg-list-keys (context &optional name mode)
1742 "Return a list of epg-key objects matched with NAME.
1743 If MODE is nil or 'public, only public keyring should be searched.
1744 If MODE is t or 'secret, only secret keyring should be searched.
1745 Otherwise, only public keyring should be searched and the key
1746 signatures should be included.
1747 NAME is either a string or a list of strings."
1748 (let ((lines (epg--list-keys-1 context name mode))
1749 keys cert pointer pointer-1 index string)
1750 (while lines
1751 (cond
1752 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1753 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1754 keys (cons (epg-make-key
1755 (if (aref (car lines) 8)
1756 (cdr (assq (string-to-char (aref (car lines) 8))
1757 epg-key-validity-alist))))
1758 keys))
1759 (epg-key-set-sub-key-list
1760 (car keys)
1761 (cons (epg--make-sub-key-1 (car lines))
1762 (epg-key-sub-key-list (car keys)))))
1763 ((member (aref (car lines) 0) '("sub" "ssb"))
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 ((equal (aref (car lines) 0) "uid")
1769 ;; Decode the UID name as a backslash escaped UTF-8 string,
1770 ;; generated by GnuPG/GpgSM.
1771 (setq string (copy-sequence (aref (car lines) 9))
1772 index 0)
1773 (while (string-match "\"" string index)
1774 (setq string (replace-match "\\\"" t t string)
1775 index (1+ (match-end 0))))
1776 (condition-case nil
1777 (setq string (epg--decode-coding-string
1778 (car (read-from-string (concat "\"" string "\"")))
1779 'utf-8))
1780 (error
1781 (setq string (aref (car lines) 9))))
1782 (epg-key-set-user-id-list
1783 (car keys)
1784 (cons (epg-make-user-id
1785 (if (aref (car lines) 1)
1786 (cdr (assq (string-to-char (aref (car lines) 1))
1787 epg-key-validity-alist)))
1788 (if cert
1789 (condition-case nil
1790 (epg-dn-from-string string)
1791 (error string))
1792 string))
1793 (epg-key-user-id-list (car keys)))))
1794 ((equal (aref (car lines) 0) "fpr")
1795 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1796 (aref (car lines) 9)))
1797 ((equal (aref (car lines) 0) "sig")
1798 (epg-user-id-set-signature-list
1799 (car (epg-key-user-id-list (car keys)))
1800 (cons
1801 (epg-make-key-signature
1802 (if (aref (car lines) 1)
1803 (cdr (assq (string-to-char (aref (car lines) 1))
1804 epg-key-validity-alist)))
1805 (string-to-number (aref (car lines) 3))
1806 (aref (car lines) 4)
1807 (epg--time-from-seconds (aref (car lines) 5))
1808 (epg--time-from-seconds (aref (car lines) 6))
1809 (aref (car lines) 9)
1810 (string-to-number (aref (car lines) 10) 16)
1811 (eq (aref (aref (car lines) 10) 2) ?x))
1812 (epg-user-id-signature-list
1813 (car (epg-key-user-id-list (car keys))))))))
1814 (setq lines (cdr lines)))
1815 (setq keys (nreverse keys)
1816 pointer keys)
1817 (while pointer
1818 (epg-key-set-sub-key-list
1819 (car pointer)
1820 (nreverse (epg-key-sub-key-list (car pointer))))
1821 (setq pointer-1 (epg-key-set-user-id-list
1822 (car pointer)
1823 (nreverse (epg-key-user-id-list (car pointer)))))
1824 (while pointer-1
1825 (epg-user-id-set-signature-list
1826 (car pointer-1)
1827 (nreverse (epg-user-id-signature-list (car pointer-1))))
1828 (setq pointer-1 (cdr pointer-1)))
1829 (setq pointer (cdr pointer)))
1830 keys))
1832 (eval-and-compile
1833 (if (fboundp 'make-temp-file)
1834 (defalias 'epg--make-temp-file 'make-temp-file)
1835 (defvar temporary-file-directory)
1836 ;; stolen from poe.el.
1837 (defun epg--make-temp-file (prefix)
1838 "Create a temporary file.
1839 The returned file name (created by appending some random characters at the end
1840 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1841 is guaranteed to point to a newly created empty file.
1842 You can then use `write-region' to write new data into the file."
1843 (let (tempdir tempfile)
1844 (setq prefix (expand-file-name prefix
1845 (if (featurep 'xemacs)
1846 (temp-directory)
1847 temporary-file-directory)))
1848 (unwind-protect
1849 (let (file)
1850 ;; First, create a temporary directory.
1851 (while (condition-case ()
1852 (progn
1853 (setq tempdir (make-temp-name
1854 (concat
1855 (file-name-directory prefix)
1856 "DIR")))
1857 ;; return nil or signal an error.
1858 (make-directory tempdir))
1859 ;; let's try again.
1860 (file-already-exists t)))
1861 (set-file-modes tempdir 448)
1862 ;; Second, create a temporary file in the tempdir.
1863 ;; There *is* a race condition between `make-temp-name'
1864 ;; and `write-region', but we don't care it since we are
1865 ;; in a private directory now.
1866 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1867 (write-region "" nil tempfile nil 'silent)
1868 (set-file-modes tempfile 384)
1869 ;; Finally, make a hard-link from the tempfile.
1870 (while (condition-case ()
1871 (progn
1872 (setq file (make-temp-name prefix))
1873 ;; return nil or signal an error.
1874 (add-name-to-file tempfile file))
1875 ;; let's try again.
1876 (file-already-exists t)))
1877 file)
1878 ;; Cleanup the tempfile.
1879 (and tempfile
1880 (file-exists-p tempfile)
1881 (delete-file tempfile))
1882 ;; Cleanup the tempdir.
1883 (and tempdir
1884 (file-directory-p tempdir)
1885 (delete-directory tempdir)))))))
1887 (defun epg--args-from-sig-notations (notations)
1888 (apply #'nconc
1889 (mapcar
1890 (lambda (notation)
1891 (if (and (epg-sig-notation-name notation)
1892 (not (epg-sig-notation-human-readable notation)))
1893 (error "Unreadable"))
1894 (if (epg-sig-notation-name notation)
1895 (list "--sig-notation"
1896 (if (epg-sig-notation-critical notation)
1897 (concat "!" (epg-sig-notation-name notation)
1898 "=" (epg-sig-notation-value notation))
1899 (concat (epg-sig-notation-name notation)
1900 "=" (epg-sig-notation-value notation))))
1901 (list "--sig-policy-url"
1902 (if (epg-sig-notation-critical notation)
1903 (concat "!" (epg-sig-notation-value notation))
1904 (epg-sig-notation-value notation)))))
1905 notations)))
1907 ;;;###autoload
1908 (defun epg-cancel (context)
1909 (if (buffer-live-p (process-buffer (epg-context-process context)))
1910 (save-excursion
1911 (set-buffer (process-buffer (epg-context-process context)))
1912 (epg-context-set-result-for
1913 epg-context 'error
1914 (cons '(quit)
1915 (epg-context-result-for epg-context 'error)))))
1916 (if (eq (process-status (epg-context-process context)) 'run)
1917 (delete-process (epg-context-process context))))
1919 ;;;###autoload
1920 (defun epg-start-decrypt (context cipher)
1921 "Initiate a decrypt operation on CIPHER.
1922 CIPHER must be a file data object.
1924 If you use this function, you will need to wait for the completion of
1925 `epg-gpg-program' by using `epg-wait-for-completion' and call
1926 `epg-reset' to clear a temporaly output file.
1927 If you are unsure, use synchronous version of this function
1928 `epg-decrypt-file' or `epg-decrypt-string' instead."
1929 (unless (epg-data-file cipher)
1930 (error "Not a file"))
1931 (epg-context-set-operation context 'decrypt)
1932 (epg-context-set-result context nil)
1933 (epg--start context (list "--decrypt" "--" (epg-data-file cipher)))
1934 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1935 (unless (eq (epg-context-protocol context) 'CMS)
1936 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1938 (defun epg--check-error-for-decrypt (context)
1939 (if (epg-context-result-for context 'decryption-failed)
1940 (signal 'epg-error (list "Decryption failed")))
1941 (if (epg-context-result-for context 'no-secret-key)
1942 (signal 'epg-error
1943 (list "No secret key"
1944 (epg-context-result-for context 'no-secret-key))))
1945 (unless (epg-context-result-for context 'decryption-okay)
1946 (let* ((error (epg-context-result-for context 'error)))
1947 (if (assq 'no-data error)
1948 (signal 'epg-error (list "No data")))
1949 (signal 'epg-error (list "Can't decrypt" error)))))
1951 ;;;###autoload
1952 (defun epg-decrypt-file (context cipher plain)
1953 "Decrypt a file CIPHER and store the result to a file PLAIN.
1954 If PLAIN is nil, it returns the result as a string."
1955 (unwind-protect
1956 (progn
1957 (if plain
1958 (epg-context-set-output-file context plain)
1959 (epg-context-set-output-file context
1960 (epg--make-temp-file "epg-output")))
1961 (epg-start-decrypt context (epg-make-data-from-file cipher))
1962 (epg-wait-for-completion context)
1963 (epg--check-error-for-decrypt context)
1964 (unless plain
1965 (epg-read-output context)))
1966 (unless plain
1967 (epg-delete-output-file context))
1968 (epg-reset context)))
1970 ;;;###autoload
1971 (defun epg-decrypt-string (context cipher)
1972 "Decrypt a string CIPHER and return the plain text."
1973 (let ((input-file (epg--make-temp-file "epg-input"))
1974 (coding-system-for-write 'binary))
1975 (unwind-protect
1976 (progn
1977 (write-region cipher nil input-file nil 'quiet)
1978 (epg-context-set-output-file context
1979 (epg--make-temp-file "epg-output"))
1980 (epg-start-decrypt context (epg-make-data-from-file input-file))
1981 (epg-wait-for-completion context)
1982 (epg--check-error-for-decrypt context)
1983 (epg-read-output context))
1984 (epg-delete-output-file context)
1985 (if (file-exists-p input-file)
1986 (delete-file input-file))
1987 (epg-reset context))))
1989 ;;;###autoload
1990 (defun epg-start-verify (context signature &optional signed-text)
1991 "Initiate a verify operation on SIGNATURE.
1992 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1994 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1995 For a normal or a cleartext signature, SIGNED-TEXT should be nil.
1997 If you use this function, you will need to wait for the completion of
1998 `epg-gpg-program' by using `epg-wait-for-completion' and call
1999 `epg-reset' to clear a temporaly output file.
2000 If you are unsure, use synchronous version of this function
2001 `epg-verify-file' or `epg-verify-string' instead."
2002 (epg-context-set-operation context 'verify)
2003 (epg-context-set-result context nil)
2004 (if signed-text
2005 ;; Detached signature.
2006 (if (epg-data-file signed-text)
2007 (epg--start context (list "--verify" "--" (epg-data-file signature)
2008 (epg-data-file signed-text)))
2009 (epg--start context (list "--verify" "--" (epg-data-file signature)
2010 "-"))
2011 (if (eq (process-status (epg-context-process context)) 'run)
2012 (process-send-string (epg-context-process context)
2013 (epg-data-string signed-text)))
2014 (if (eq (process-status (epg-context-process context)) 'run)
2015 (process-send-eof (epg-context-process context))))
2016 ;; Normal (or cleartext) signature.
2017 (if (epg-data-file signature)
2018 (epg--start context (list "--" (epg-data-file signature)))
2019 (epg--start context '("-"))
2020 (if (eq (process-status (epg-context-process context)) 'run)
2021 (process-send-string (epg-context-process context)
2022 (epg-data-string signature)))
2023 (if (eq (process-status (epg-context-process context)) 'run)
2024 (process-send-eof (epg-context-process context))))))
2026 ;;;###autoload
2027 (defun epg-verify-file (context signature &optional signed-text plain)
2028 "Verify a file SIGNATURE.
2029 SIGNED-TEXT and PLAIN are also a file if they are specified.
2031 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
2032 string. For a normal or a cleartext signature, SIGNED-TEXT should be
2033 nil. In the latter case, if PLAIN is specified, the plaintext is
2034 stored into the file after successful verification."
2035 (unwind-protect
2036 (progn
2037 (if plain
2038 (epg-context-set-output-file context plain)
2039 (epg-context-set-output-file context
2040 (epg--make-temp-file "epg-output")))
2041 (if signed-text
2042 (epg-start-verify context
2043 (epg-make-data-from-file signature)
2044 (epg-make-data-from-file signed-text))
2045 (epg-start-verify context
2046 (epg-make-data-from-file signature)))
2047 (epg-wait-for-completion context)
2048 (unless plain
2049 (epg-read-output context)))
2050 (unless plain
2051 (epg-delete-output-file context))
2052 (epg-reset context)))
2054 ;;;###autoload
2055 (defun epg-verify-string (context signature &optional signed-text)
2056 "Verify a string SIGNATURE.
2057 SIGNED-TEXT is a string if it is specified.
2059 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
2060 string. For a normal or a cleartext signature, SIGNED-TEXT should be
2061 nil. In the latter case, this function returns the plaintext after
2062 successful verification."
2063 (let ((coding-system-for-write 'binary)
2064 input-file)
2065 (unwind-protect
2066 (progn
2067 (epg-context-set-output-file context
2068 (epg--make-temp-file "epg-output"))
2069 (if signed-text
2070 (progn
2071 (setq input-file (epg--make-temp-file "epg-signature"))
2072 (write-region signature nil input-file nil 'quiet)
2073 (epg-start-verify context
2074 (epg-make-data-from-file input-file)
2075 (epg-make-data-from-string signed-text)))
2076 (epg-start-verify context (epg-make-data-from-string signature)))
2077 (epg-wait-for-completion context)
2078 (epg-read-output context))
2079 (epg-delete-output-file context)
2080 (if (and input-file
2081 (file-exists-p input-file))
2082 (delete-file input-file))
2083 (epg-reset context))))
2085 ;;;###autoload
2086 (defun epg-start-sign (context plain &optional mode)
2087 "Initiate a sign operation on PLAIN.
2088 PLAIN is a data object.
2090 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2091 If it is nil or 'normal, it makes a normal signature.
2092 Otherwise, it makes a cleartext signature.
2094 If you use this function, you will need to wait for the completion of
2095 `epg-gpg-program' by using `epg-wait-for-completion' and call
2096 `epg-reset' to clear a temporaly output file.
2097 If you are unsure, use synchronous version of this function
2098 `epg-sign-file' or `epg-sign-string' instead."
2099 (epg-context-set-operation context 'sign)
2100 (epg-context-set-result context nil)
2101 (unless (memq mode '(t detached nil normal)) ;i.e. cleartext
2102 (epg-context-set-armor context nil)
2103 (epg-context-set-textmode context nil))
2104 (epg--start context
2105 (append (list (if (memq mode '(t detached))
2106 "--detach-sign"
2107 (if (memq mode '(nil normal))
2108 "--sign"
2109 "--clearsign")))
2110 (apply #'nconc
2111 (mapcar
2112 (lambda (signer)
2113 (list "-u"
2114 (epg-sub-key-id
2115 (car (epg-key-sub-key-list signer)))))
2116 (epg-context-signers context)))
2117 (epg--args-from-sig-notations
2118 (epg-context-sig-notations context))
2119 (if (epg-data-file plain)
2120 (list "--" (epg-data-file plain)))))
2121 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2122 (unless (eq (epg-context-protocol context) 'CMS)
2123 (epg-wait-for-status context '("BEGIN_SIGNING")))
2124 (when (epg-data-string plain)
2125 (if (eq (process-status (epg-context-process context)) 'run)
2126 (process-send-string (epg-context-process context)
2127 (epg-data-string plain)))
2128 (if (eq (process-status (epg-context-process context)) 'run)
2129 (process-send-eof (epg-context-process context)))))
2131 ;;;###autoload
2132 (defun epg-sign-file (context plain signature &optional mode)
2133 "Sign a file PLAIN and store the result to a file SIGNATURE.
2134 If SIGNATURE is nil, it returns the result as a string.
2135 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2136 If it is nil or 'normal, it makes a normal signature.
2137 Otherwise, it makes a cleartext signature."
2138 (unwind-protect
2139 (progn
2140 (if signature
2141 (epg-context-set-output-file context signature)
2142 (epg-context-set-output-file context
2143 (epg--make-temp-file "epg-output")))
2144 (epg-start-sign context (epg-make-data-from-file plain) mode)
2145 (epg-wait-for-completion context)
2146 (unless (epg-context-result-for context 'sign)
2147 (if (epg-context-result-for context 'error)
2148 (error "Sign failed: %S"
2149 (epg-context-result-for context 'error))
2150 (error "Sign failed")))
2151 (unless signature
2152 (epg-read-output context)))
2153 (unless signature
2154 (epg-delete-output-file context))
2155 (epg-reset context)))
2157 ;;;###autoload
2158 (defun epg-sign-string (context plain &optional mode)
2159 "Sign a string PLAIN and return the output as string.
2160 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2161 If it is nil or 'normal, it makes a normal signature.
2162 Otherwise, it makes a cleartext signature."
2163 (let ((input-file
2164 (unless (or (eq (epg-context-protocol context) 'CMS)
2165 (condition-case nil
2166 (progn
2167 (epg-check-configuration (epg-configuration))
2169 (error)))
2170 (epg--make-temp-file "epg-input")))
2171 (coding-system-for-write 'binary))
2172 (unwind-protect
2173 (progn
2174 (epg-context-set-output-file context
2175 (epg--make-temp-file "epg-output"))
2176 (if input-file
2177 (write-region plain nil input-file nil 'quiet))
2178 (epg-start-sign context
2179 (if input-file
2180 (epg-make-data-from-file input-file)
2181 (epg-make-data-from-string plain))
2182 mode)
2183 (epg-wait-for-completion context)
2184 (unless (epg-context-result-for context 'sign)
2185 (if (epg-context-result-for context 'error)
2186 (error "Sign failed: %S"
2187 (epg-context-result-for context 'error))
2188 (error "Sign failed")))
2189 (epg-read-output context))
2190 (epg-delete-output-file context)
2191 (if input-file
2192 (delete-file input-file))
2193 (epg-reset context))))
2195 ;;;###autoload
2196 (defun epg-start-encrypt (context plain recipients
2197 &optional sign always-trust)
2198 "Initiate an encrypt operation on PLAIN.
2199 PLAIN is a data object.
2200 If RECIPIENTS is nil, it performs symmetric encryption.
2202 If you use this function, you will need to wait for the completion of
2203 `epg-gpg-program' by using `epg-wait-for-completion' and call
2204 `epg-reset' to clear a temporaly output file.
2205 If you are unsure, use synchronous version of this function
2206 `epg-encrypt-file' or `epg-encrypt-string' instead."
2207 (epg-context-set-operation context 'encrypt)
2208 (epg-context-set-result context nil)
2209 (epg--start context
2210 (append (if always-trust '("--always-trust"))
2211 (if recipients '("--encrypt") '("--symmetric"))
2212 (if sign '("--sign"))
2213 (if sign
2214 (apply #'nconc
2215 (mapcar
2216 (lambda (signer)
2217 (list "-u"
2218 (epg-sub-key-id
2219 (car (epg-key-sub-key-list
2220 signer)))))
2221 (epg-context-signers context))))
2222 (if sign
2223 (epg--args-from-sig-notations
2224 (epg-context-sig-notations context)))
2225 (apply #'nconc
2226 (mapcar
2227 (lambda (recipient)
2228 (list "-r"
2229 (epg-sub-key-id
2230 (car (epg-key-sub-key-list recipient)))))
2231 recipients))
2232 (if (epg-data-file plain)
2233 (list "--" (epg-data-file plain)))))
2234 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2235 (unless (eq (epg-context-protocol context) 'CMS)
2236 (if sign
2237 (epg-wait-for-status context '("BEGIN_SIGNING"))
2238 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
2239 (when (epg-data-string plain)
2240 (if (eq (process-status (epg-context-process context)) 'run)
2241 (process-send-string (epg-context-process context)
2242 (epg-data-string plain)))
2243 (if (eq (process-status (epg-context-process context)) 'run)
2244 (process-send-eof (epg-context-process context)))))
2246 ;;;###autoload
2247 (defun epg-encrypt-file (context plain recipients
2248 cipher &optional sign always-trust)
2249 "Encrypt a file PLAIN and store the result to a file CIPHER.
2250 If CIPHER is nil, it returns the result as a string.
2251 If RECIPIENTS is nil, it performs symmetric encryption."
2252 (unwind-protect
2253 (progn
2254 (if cipher
2255 (epg-context-set-output-file context cipher)
2256 (epg-context-set-output-file context
2257 (epg--make-temp-file "epg-output")))
2258 (epg-start-encrypt context (epg-make-data-from-file plain)
2259 recipients sign always-trust)
2260 (epg-wait-for-completion context)
2261 (if (and sign
2262 (not (epg-context-result-for context 'sign)))
2263 (if (epg-context-result-for context 'error)
2264 (error "Sign failed: %S"
2265 (epg-context-result-for context 'error))
2266 (error "Sign failed")))
2267 (if (epg-context-result-for context 'error)
2268 (error "Encrypt failed: %S"
2269 (epg-context-result-for context 'error)))
2270 (unless cipher
2271 (epg-read-output context)))
2272 (unless cipher
2273 (epg-delete-output-file context))
2274 (epg-reset context)))
2276 ;;;###autoload
2277 (defun epg-encrypt-string (context plain recipients
2278 &optional sign always-trust)
2279 "Encrypt a string PLAIN.
2280 If RECIPIENTS is nil, it performs symmetric encryption."
2281 (let ((input-file
2282 (unless (or (not sign)
2283 (eq (epg-context-protocol context) 'CMS)
2284 (condition-case nil
2285 (progn
2286 (epg-check-configuration (epg-configuration))
2288 (error)))
2289 (epg--make-temp-file "epg-input")))
2290 (coding-system-for-write 'binary))
2291 (unwind-protect
2292 (progn
2293 (epg-context-set-output-file context
2294 (epg--make-temp-file "epg-output"))
2295 (if input-file
2296 (write-region plain nil input-file nil 'quiet))
2297 (epg-start-encrypt context
2298 (if input-file
2299 (epg-make-data-from-file input-file)
2300 (epg-make-data-from-string plain))
2301 recipients sign always-trust)
2302 (epg-wait-for-completion context)
2303 (if (and sign
2304 (not (epg-context-result-for context 'sign)))
2305 (if (epg-context-result-for context 'error)
2306 (error "Sign failed: %S"
2307 (epg-context-result-for context 'error))
2308 (error "Sign failed")))
2309 (if (epg-context-result-for context 'error)
2310 (error "Encrypt failed: %S"
2311 (epg-context-result-for context 'error)))
2312 (epg-read-output context))
2313 (epg-delete-output-file context)
2314 (if input-file
2315 (delete-file input-file))
2316 (epg-reset context))))
2318 ;;;###autoload
2319 (defun epg-start-export-keys (context keys)
2320 "Initiate an export keys operation.
2322 If you use this function, you will need to wait for the completion of
2323 `epg-gpg-program' by using `epg-wait-for-completion' and call
2324 `epg-reset' to clear a temporaly output file.
2325 If you are unsure, use synchronous version of this function
2326 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
2327 (epg-context-set-operation context 'export-keys)
2328 (epg-context-set-result context nil)
2329 (epg--start context (cons "--export"
2330 (mapcar
2331 (lambda (key)
2332 (epg-sub-key-id
2333 (car (epg-key-sub-key-list key))))
2334 keys))))
2336 ;;;###autoload
2337 (defun epg-export-keys-to-file (context keys file)
2338 "Extract public KEYS."
2339 (unwind-protect
2340 (progn
2341 (if file
2342 (epg-context-set-output-file context file)
2343 (epg-context-set-output-file context
2344 (epg--make-temp-file "epg-output")))
2345 (epg-start-export-keys context keys)
2346 (epg-wait-for-completion context)
2347 (if (epg-context-result-for context 'error)
2348 (error "Export keys failed: %S"
2349 (epg-context-result-for context 'error)))
2350 (unless file
2351 (epg-read-output context)))
2352 (unless file
2353 (epg-delete-output-file context))
2354 (epg-reset context)))
2356 ;;;###autoload
2357 (defun epg-export-keys-to-string (context keys)
2358 "Extract public KEYS and return them as a string."
2359 (epg-export-keys-to-file context keys nil))
2361 ;;;###autoload
2362 (defun epg-start-import-keys (context keys)
2363 "Initiate an import keys operation.
2364 KEYS is a data object.
2366 If you use this function, you will need to wait for the completion of
2367 `epg-gpg-program' by using `epg-wait-for-completion' and call
2368 `epg-reset' to clear a temporaly output file.
2369 If you are unsure, use synchronous version of this function
2370 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
2371 (epg-context-set-operation context 'import-keys)
2372 (epg-context-set-result context nil)
2373 (epg--start context (if (epg-data-file keys)
2374 (list "--import" "--" (epg-data-file keys))
2375 (list "--import")))
2376 (when (epg-data-string keys)
2377 (if (eq (process-status (epg-context-process context)) 'run)
2378 (process-send-string (epg-context-process context)
2379 (epg-data-string keys)))
2380 (if (eq (process-status (epg-context-process context)) 'run)
2381 (process-send-eof (epg-context-process context)))))
2383 (defun epg--import-keys-1 (context keys)
2384 (unwind-protect
2385 (progn
2386 (epg-start-import-keys context keys)
2387 (epg-wait-for-completion context)
2388 (if (epg-context-result-for context 'error)
2389 (error "Import keys failed: %S"
2390 (epg-context-result-for context 'error))))
2391 (epg-reset context)))
2393 ;;;###autoload
2394 (defun epg-import-keys-from-file (context keys)
2395 "Add keys from a file KEYS."
2396 (epg--import-keys-1 context (epg-make-data-from-file keys)))
2398 ;;;###autoload
2399 (defun epg-import-keys-from-string (context keys)
2400 "Add keys from a string KEYS."
2401 (epg--import-keys-1 context (epg-make-data-from-string keys)))
2403 ;;;###autoload
2404 (defun epg-start-receive-keys (context key-id-list)
2405 "Initiate a receive key operation.
2406 KEY-ID-LIST is a list of key IDs.
2408 If you use this function, you will need to wait for the completion of
2409 `epg-gpg-program' by using `epg-wait-for-completion' and call
2410 `epg-reset' to clear a temporaly output file.
2411 If you are unsure, use synchronous version of this function
2412 `epg-receive-keys' instead."
2413 (epg-context-set-operation context 'receive-keys)
2414 (epg-context-set-result context nil)
2415 (epg--start context (cons "--recv-keys" key-id-list)))
2417 ;;;###autoload
2418 (defun epg-receive-keys (context keys)
2419 "Add keys from server.
2420 KEYS is a list of key IDs"
2421 (unwind-protect
2422 (progn
2423 (epg-start-receive-keys context keys)
2424 (epg-wait-for-completion context)
2425 (if (epg-context-result-for context 'error)
2426 (error "Receive keys failed: %S"
2427 (epg-context-result-for context 'error))))
2428 (epg-reset context)))
2430 ;;;###autoload
2431 (defalias 'epg-import-keys-from-server 'epg-receive-keys)
2433 ;;;###autoload
2434 (defun epg-start-delete-keys (context keys &optional allow-secret)
2435 "Initiate a delete keys operation.
2437 If you use this function, you will need to wait for the completion of
2438 `epg-gpg-program' by using `epg-wait-for-completion' and call
2439 `epg-reset' to clear a temporaly output file.
2440 If you are unsure, use synchronous version of this function
2441 `epg-delete-keys' instead."
2442 (epg-context-set-operation context 'delete-keys)
2443 (epg-context-set-result context nil)
2444 (epg--start context (cons (if allow-secret
2445 "--delete-secret-key"
2446 "--delete-key")
2447 (mapcar
2448 (lambda (key)
2449 (epg-sub-key-id
2450 (car (epg-key-sub-key-list key))))
2451 keys))))
2453 ;;;###autoload
2454 (defun epg-delete-keys (context keys &optional allow-secret)
2455 "Delete KEYS from the key ring."
2456 (unwind-protect
2457 (progn
2458 (epg-start-delete-keys context keys allow-secret)
2459 (epg-wait-for-completion context)
2460 (let ((entry (assq 'delete-problem
2461 (epg-context-result-for context 'error))))
2462 (if entry
2463 (if (setq entry (assq (cdr entry)
2464 epg-delete-problem-reason-alist))
2465 (error "Delete keys failed: %s" (cdr entry))
2466 (error "Delete keys failed")))))
2467 (epg-reset context)))
2469 ;;;###autoload
2470 (defun epg-start-sign-keys (context keys &optional local)
2471 "Initiate a sign keys operation.
2473 If you use this function, you will need to wait for the completion of
2474 `epg-gpg-program' by using `epg-wait-for-completion' and call
2475 `epg-reset' to clear a temporaly output file.
2476 If you are unsure, use synchronous version of this function
2477 `epg-sign-keys' instead."
2478 (epg-context-set-operation context 'sign-keys)
2479 (epg-context-set-result context nil)
2480 (epg--start context (cons (if local
2481 "--lsign-key"
2482 "--sign-key")
2483 (mapcar
2484 (lambda (key)
2485 (epg-sub-key-id
2486 (car (epg-key-sub-key-list key))))
2487 keys))))
2488 (make-obsolete 'epg-start-sign-keys "do not use.")
2490 ;;;###autoload
2491 (defun epg-sign-keys (context keys &optional local)
2492 "Sign KEYS from the key ring."
2493 (unwind-protect
2494 (progn
2495 (epg-start-sign-keys context keys local)
2496 (epg-wait-for-completion context)
2497 (if (epg-context-result-for context 'error)
2498 (error "Sign keys failed: %S"
2499 (epg-context-result-for context 'error))))
2500 (epg-reset context)))
2501 (make-obsolete 'epg-sign-keys "do not use.")
2503 ;;;###autoload
2504 (defun epg-start-generate-key (context parameters)
2505 "Initiate a key generation.
2506 PARAMETERS specifies parameters for the key.
2508 If you use this function, you will need to wait for the completion of
2509 `epg-gpg-program' by using `epg-wait-for-completion' and call
2510 `epg-reset' to clear a temporaly output file.
2511 If you are unsure, use synchronous version of this function
2512 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2513 (epg-context-set-operation context 'generate-key)
2514 (epg-context-set-result context nil)
2515 (if (epg-data-file parameters)
2516 (epg--start context (list "--batch" "--genkey" "--"
2517 (epg-data-file parameters)))
2518 (epg--start context '("--batch" "--genkey"))
2519 (if (eq (process-status (epg-context-process context)) 'run)
2520 (process-send-string (epg-context-process context)
2521 (epg-data-string parameters)))
2522 (if (eq (process-status (epg-context-process context)) 'run)
2523 (process-send-eof (epg-context-process context)))))
2525 ;;;###autoload
2526 (defun epg-generate-key-from-file (context parameters)
2527 "Generate a new key pair.
2528 PARAMETERS is a file which tells how to create the key."
2529 (unwind-protect
2530 (progn
2531 (epg-start-generate-key context (epg-make-data-from-file parameters))
2532 (epg-wait-for-completion context)
2533 (if (epg-context-result-for context 'error)
2534 (error "Generate key failed: %S"
2535 (epg-context-result-for context 'error))))
2536 (epg-reset context)))
2538 ;;;###autoload
2539 (defun epg-generate-key-from-string (context parameters)
2540 "Generate a new key pair.
2541 PARAMETERS is a string which tells how to create the key."
2542 (unwind-protect
2543 (progn
2544 (epg-start-generate-key context (epg-make-data-from-string parameters))
2545 (epg-wait-for-completion context)
2546 (if (epg-context-result-for context 'error)
2547 (error "Generate key failed: %S"
2548 (epg-context-result-for context 'error))))
2549 (epg-reset context)))
2551 (defun epg--decode-percent-escape (string)
2552 (let ((index 0))
2553 (while (string-match "%\\(\\(%\\)\\|\\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2554 string index)
2555 (if (match-beginning 2)
2556 (setq string (replace-match "%" t t string)
2557 index (1- (match-end 0)))
2558 (setq string (replace-match
2559 (string (string-to-number (match-string 3 string) 16))
2560 t t string)
2561 index (- (match-end 0) 2))))
2562 string))
2564 (defun epg--decode-hexstring (string)
2565 (let ((index 0))
2566 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
2567 (setq string (replace-match (string (string-to-number
2568 (match-string 0 string) 16))
2569 t t string)
2570 index (1- (match-end 0))))
2571 string))
2573 (defun epg--decode-quotedstring (string)
2574 (let ((index 0))
2575 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2576 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2577 string index)
2578 (if (match-beginning 2)
2579 (setq string (replace-match "\\2" t nil string)
2580 index (1- (match-end 0)))
2581 (if (match-beginning 3)
2582 (setq string (replace-match (string (string-to-number
2583 (match-string 0 string) 16))
2584 t t string)
2585 index (- (match-end 0) 2)))))
2586 string))
2588 (defun epg-dn-from-string (string)
2589 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2590 The return value is an alist mapping from types to values."
2591 (let ((index 0)
2592 (length (length string))
2593 alist type value group)
2594 (while (< index length)
2595 (if (eq index (string-match "[ \t\n\r]*" string index))
2596 (setq index (match-end 0)))
2597 (if (eq index (string-match
2598 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2599 string index))
2600 (setq type (match-string 1 string)
2601 index (match-end 0))
2602 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2603 string index))
2604 (setq type (match-string 1 string)
2605 index (match-end 0))))
2606 (unless type
2607 (error "Invalid type"))
2608 (if (eq index (string-match
2609 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2610 string index))
2611 (setq index (match-end 0)
2612 value (epg--decode-quotedstring (match-string 0 string)))
2613 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
2614 (setq index (match-end 0)
2615 value (epg--decode-hexstring (match-string 1 string)))
2616 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2617 string index))
2618 (setq index (match-end 0)
2619 value (epg--decode-quotedstring
2620 (match-string 0 string))))))
2621 (if group
2622 (if (stringp (car (car alist)))
2623 (setcar alist (list (cons type value) (car alist)))
2624 (setcar alist (cons (cons type value) (car alist))))
2625 (if (consp (car (car alist)))
2626 (setcar alist (nreverse (car alist))))
2627 (setq alist (cons (cons type value) alist)
2628 type nil
2629 value nil))
2630 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
2631 (setq index (match-end 0)
2632 group (eq (aref string (match-beginning 1)) ?+))))
2633 (nreverse alist)))
2635 (defun epg-decode-dn (alist)
2636 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2637 Type names are resolved using `epg-dn-type-alist'."
2638 (mapconcat
2639 (lambda (rdn)
2640 (if (stringp (car rdn))
2641 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
2642 (if entry
2643 (format "%s=%s" (cdr entry) (cdr rdn))
2644 (format "%s=%s" (car rdn) (cdr rdn))))
2645 (concat "(" (epg-decode-dn rdn) ")")))
2646 alist
2647 ", "))
2649 (provide 'epg)
2651 ;; arch-tag: de8f0acc-1bcf-4c14-a09e-bfffe1b579b7
2652 ;;; epg.el ends here