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