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