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