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