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