* epg.el (epg-error): Set `error-message' property.
[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-signature-to-string (signature)
971 "Convert SIGNATURE to a human readable string."
972 (let* ((user-id (cdr (assoc (epg-signature-key-id signature)
973 epg-user-id-alist)))
974 (pubkey-algorithm (epg-signature-pubkey-algorithm signature)))
975 (concat
976 (cond ((eq (epg-signature-status signature) 'good)
977 "Good signature from ")
978 ((eq (epg-signature-status signature) 'bad)
979 "Bad signature from ")
980 ((eq (epg-signature-status signature) 'expired)
981 "Expired signature from ")
982 ((eq (epg-signature-status signature) 'expired-key)
983 "Signature made by expired key ")
984 ((eq (epg-signature-status signature) 'revoked-key)
985 "Signature made by revoked key ")
986 ((eq (epg-signature-status signature) 'no-pubkey)
987 "No public key for "))
988 (epg-signature-key-id signature)
989 (if user-id
990 (concat " "
991 (if (stringp user-id)
992 user-id
993 (epg-decode-dn user-id)))
994 "")
995 (if (epg-signature-validity signature)
996 (format " (trust %s)" (epg-signature-validity signature))
997 "")
998 (if (epg-signature-creation-time signature)
999 (format-time-string " created at %Y-%m-%dT%T%z"
1000 (epg-signature-creation-time signature))
1001 "")
1002 (if pubkey-algorithm
1003 (concat " using "
1004 (or (cdr (assq pubkey-algorithm epg-pubkey-algorithm-alist))
1005 (format "(unknown algorithm %d)" pubkey-algorithm)))
1006 ""))))
1007
1008 (defun epg-verify-result-to-string (verify-result)
1009 "Convert VERIFY-RESULT to a human readable string."
1010 (mapconcat #'epg-signature-to-string verify-result "\n"))
1011
1012 (defun epg-new-signature-to-string (new-signature)
1013 "Convert NEW-SIGNATURE to a human readable string."
1014 (concat
1015 (cond ((eq (epg-new-signature-type new-signature) 'detached)
1016 "Detached signature ")
1017 ((eq (epg-new-signature-type new-signature) 'clear)
1018 "Cleartext signature ")
1019 (t
1020 "Signature "))
1021 (cdr (assq (epg-new-signature-pubkey-algorithm new-signature)
1022 epg-pubkey-algorithm-alist))
1023 "/"
1024 (cdr (assq (epg-new-signature-digest-algorithm new-signature)
1025 epg-digest-algorithm-alist))
1026 " "
1027 (format "%02X " (epg-new-signature-class new-signature))
1028 (epg-new-signature-fingerprint new-signature)))
1029
1030 (defun epg-import-result-to-string (import-result)
1031 "Convert IMPORT-RESULT to a human readable string."
1032 (concat (format "Total number processed: %d\n"
1033 (epg-import-result-considered import-result))
1034 (if (> (epg-import-result-not-imported import-result) 0)
1035 (format " skipped new keys: %d\n"
1036 (epg-import-result-not-imported import-result)))
1037 (if (> (epg-import-result-no-user-id import-result) 0)
1038 (format " w/o user IDs: %d\n"
1039 (epg-import-result-no-user-id import-result)))
1040 (if (> (epg-import-result-imported import-result) 0)
1041 (concat (format " imported: %d"
1042 (epg-import-result-imported import-result))
1043 (if (> (epg-import-result-imported-rsa import-result) 0)
1044 (format " (RSA: %d)"
1045 (epg-import-result-imported-rsa
1046 import-result)))
1047 "\n"))
1048 (if (> (epg-import-result-unchanged import-result) 0)
1049 (format " unchanged: %d\n"
1050 (epg-import-result-unchanged import-result)))
1051 (if (> (epg-import-result-new-user-ids import-result) 0)
1052 (format " new user IDs: %d\n"
1053 (epg-import-result-new-user-ids import-result)))
1054 (if (> (epg-import-result-new-sub-keys import-result) 0)
1055 (format " new subkeys: %d\n"
1056 (epg-import-result-new-sub-keys import-result)))
1057 (if (> (epg-import-result-new-signatures import-result) 0)
1058 (format " new signatures: %d\n"
1059 (epg-import-result-new-signatures import-result)))
1060 (if (> (epg-import-result-new-revocations import-result) 0)
1061 (format " new key revocations: %d\n"
1062 (epg-import-result-new-revocations import-result)))
1063 (if (> (epg-import-result-secret-read import-result) 0)
1064 (format " secret keys read: %d\n"
1065 (epg-import-result-secret-read import-result)))
1066 (if (> (epg-import-result-secret-imported import-result) 0)
1067 (format " secret keys imported: %d\n"
1068 (epg-import-result-secret-imported import-result)))
1069 (if (> (epg-import-result-secret-unchanged import-result) 0)
1070 (format " secret keys unchanged: %d\n"
1071 (epg-import-result-secret-unchanged import-result)))))
1072
1073 (defun epg-error-to-string (error)
1074 (cond
1075 ((eq (car error) 'exit)
1076 "Exit")
1077 ((eq (car error) 'quit)
1078 "Cancelled")
1079 ((eq (car error) 'no-data)
1080 (let ((entry (assq (cdr error) epg-no-data-reason-alist)))
1081 (if entry
1082 (format "No data (%s)" (downcase (cdr entry)))
1083 "No data")))
1084 ((eq (car error) 'unexpected)
1085 (let ((entry (assq (cdr error) epg-unexpected-reason-alist)))
1086 (if entry
1087 (format "Unexpected (%s)" (downcase (cdr entry)))
1088 "Unexpected")))
1089 ((eq (car error) 'bad-armor)
1090 "Bad armor")
1091 ((memq (car error) '(invalid-recipient invalid-signer))
1092 (concat
1093 (if (eq (car error) 'invalid-recipient)
1094 "Unusable public key"
1095 "Unusable secret key")
1096 (let ((entry (assq 'requested (cdr error))))
1097 (if entry
1098 (format ": %s" (cdr entry))
1099 ": <unknown>"))
1100 (let ((entry (assq 'reason (cdr error))))
1101 (if (and entry
1102 (> (cdr entry) 0) ;no specific reason given
1103 (setq entry (assq (cdr entry)
1104 epg-invalid-recipients-reason-alist)))
1105 (format " (%s)" (downcase (cdr entry)))
1106 ""))))
1107 ((eq (car error) 'no-pubkey)
1108 (format "No public key: %s" (cdr error)))
1109 ((eq (car error) 'no-seckey)
1110 (format "No secret key: %s" (cdr error)))
1111 ((eq (car error) 'no-recipients)
1112 "No recipients")
1113 ((eq (car error) 'no-signers)
1114 "No signers")
1115 ((eq (car error) 'delete-problem)
1116 (let ((entry (assq (cdr error) epg-delete-problem-reason-alist)))
1117 (if entry
1118 (format "Delete problem (%s)" (downcase (cdr entry)))
1119 "Delete problem")))
1120 ((eq (car error) 'key-not-created)
1121 "Key not created")))
1122
1123 (defun epg-errors-to-string (errors)
1124 (mapconcat #'epg-error-to-string errors "; "))
1125
1126 (defun epg--start (context args)
1127 "Start `epg-gpg-program' in a subprocess with given ARGS."
1128 (if (and (epg-context-process context)
1129 (eq (process-status (epg-context-process context)) 'run))
1130 (error "%s is already running in this context"
1131 (if (eq (epg-context-protocol context) 'CMS)
1132 epg-gpgsm-program
1133 epg-gpg-program)))
1134 (let* ((agent-info (getenv "GPG_AGENT_INFO"))
1135 (args (append (list "--no-tty"
1136 "--status-fd" "1"
1137 "--yes")
1138 (if (and (not (eq (epg-context-protocol context) 'CMS))
1139 (string-match ":" (or agent-info "")))
1140 '("--use-agent"))
1141 (if (and (not (eq (epg-context-protocol context) 'CMS))
1142 (epg-context-progress-callback context))
1143 '("--enable-progress-filter"))
1144 (if epg-gpg-home-directory
1145 (list "--homedir" epg-gpg-home-directory))
1146 (unless (eq (epg-context-protocol context) 'CMS)
1147 '("--command-fd" "0"))
1148 (if (epg-context-armor context) '("--armor"))
1149 (if (epg-context-textmode context) '("--textmode"))
1150 (if (epg-context-output-file context)
1151 (list "--output" (epg-context-output-file context)))
1152 args))
1153 (coding-system-for-write 'binary)
1154 (coding-system-for-read 'binary)
1155 process-connection-type
1156 (orig-mode (default-file-modes))
1157 (buffer (generate-new-buffer " *epg*"))
1158 process)
1159 (if epg-debug
1160 (save-excursion
1161 (unless epg-debug-buffer
1162 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
1163 (set-buffer epg-debug-buffer)
1164 (goto-char (point-max))
1165 (insert (if agent-info
1166 (format "GPG_AGENT_INFO=%s\n" agent-info)
1167 "GPG_AGENT_INFO is not set\n")
1168 (format "%s %s\n"
1169 (if (eq (epg-context-protocol context) 'CMS)
1170 epg-gpgsm-program
1171 epg-gpg-program)
1172 (mapconcat #'identity args " ")))))
1173 (with-current-buffer buffer
1174 (if (fboundp 'set-buffer-multibyte)
1175 (set-buffer-multibyte nil))
1176 (make-local-variable 'epg-last-status)
1177 (setq epg-last-status nil)
1178 (make-local-variable 'epg-read-point)
1179 (setq epg-read-point (point-min))
1180 (make-local-variable 'epg-process-filter-running)
1181 (setq epg-process-filter-running nil)
1182 (make-local-variable 'epg-pending-status-list)
1183 (setq epg-pending-status-list nil)
1184 (make-local-variable 'epg-key-id)
1185 (setq epg-key-id nil)
1186 (make-local-variable 'epg-context)
1187 (setq epg-context context))
1188 (unwind-protect
1189 (progn
1190 (set-default-file-modes 448)
1191 (setq process
1192 (apply #'start-process "epg" buffer
1193 (if (eq (epg-context-protocol context) 'CMS)
1194 epg-gpgsm-program
1195 epg-gpg-program)
1196 args)))
1197 (set-default-file-modes orig-mode))
1198 (set-process-filter process #'epg--process-filter)
1199 (epg-context-set-process context process)))
1200
1201 (defun epg--process-filter (process input)
1202 (if epg-debug
1203 (save-excursion
1204 (unless epg-debug-buffer
1205 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
1206 (set-buffer epg-debug-buffer)
1207 (goto-char (point-max))
1208 (insert input)))
1209 (if (buffer-live-p (process-buffer process))
1210 (with-current-buffer (process-buffer process)
1211 (goto-char (point-max))
1212 (insert input)
1213 (unless epg-process-filter-running
1214 (unwind-protect
1215 (progn
1216 (setq epg-process-filter-running t)
1217 (goto-char epg-read-point)
1218 (beginning-of-line)
1219 (while (looking-at ".*\n") ;the input line finished
1220 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
1221 (let* ((status (match-string 1))
1222 (string (match-string 2))
1223 (symbol (intern-soft (concat "epg--status-"
1224 status))))
1225 (if (member status epg-pending-status-list)
1226 (setq epg-pending-status-list nil))
1227 (if (and symbol
1228 (fboundp symbol))
1229 (funcall symbol epg-context string))
1230 (setq epg-last-status (cons status string))))
1231 (forward-line)
1232 (setq epg-read-point (point))))
1233 (setq epg-process-filter-running nil))))))
1234
1235 (defun epg-read-output (context)
1236 "Read the output file CONTEXT and return the content as a string."
1237 (with-temp-buffer
1238 (if (fboundp 'set-buffer-multibyte)
1239 (set-buffer-multibyte nil))
1240 (if (file-exists-p (epg-context-output-file context))
1241 (let ((coding-system-for-read 'binary))
1242 (insert-file-contents (epg-context-output-file context))
1243 (buffer-string)))))
1244
1245 (defun epg-wait-for-status (context status-list)
1246 "Wait until one of elements in STATUS-LIST arrives."
1247 (with-current-buffer (process-buffer (epg-context-process context))
1248 (setq epg-pending-status-list status-list)
1249 (while (and (eq (process-status (epg-context-process context)) 'run)
1250 epg-pending-status-list)
1251 (accept-process-output (epg-context-process context) 1))
1252 (if epg-pending-status-list
1253 (epg-context-set-result-for
1254 context 'error
1255 (cons '(exit)
1256 (epg-context-result-for context 'error))))))
1257
1258 (defun epg-wait-for-completion (context)
1259 "Wait until the `epg-gpg-program' process completes."
1260 (while (eq (process-status (epg-context-process context)) 'run)
1261 (accept-process-output (epg-context-process context) 1))
1262 ;; This line is needed to run the process-filter right now.
1263 (sleep-for 0.1)
1264 (epg-context-set-result-for
1265 context 'error
1266 (nreverse (epg-context-result-for context 'error))))
1267
1268 (defun epg-reset (context)
1269 "Reset the CONTEXT."
1270 (if (and (epg-context-process context)
1271 (buffer-live-p (process-buffer (epg-context-process context))))
1272 (kill-buffer (process-buffer (epg-context-process context))))
1273 (epg-context-set-process context nil))
1274
1275 (defun epg-delete-output-file (context)
1276 "Delete the output file of CONTEXT."
1277 (if (and (epg-context-output-file context)
1278 (file-exists-p (epg-context-output-file context)))
1279 (delete-file (epg-context-output-file context))))
1280
1281 (eval-and-compile
1282 (if (fboundp 'decode-coding-string)
1283 (defalias 'epg--decode-coding-string 'decode-coding-string)
1284 (defalias 'epg--decode-coding-string 'identity)))
1285
1286 (defun epg--status-USERID_HINT (_context string)
1287 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1288 (let* ((key-id (match-string 1 string))
1289 (user-id (match-string 2 string))
1290 (entry (assoc key-id epg-user-id-alist)))
1291 (condition-case nil
1292 (setq user-id (epg--decode-coding-string
1293 (epg--decode-percent-escape user-id)
1294 'utf-8))
1295 (error))
1296 (if entry
1297 (setcdr entry user-id)
1298 (setq epg-user-id-alist (cons (cons key-id user-id)
1299 epg-user-id-alist))))))
1300
1301 (defun epg--status-NEED_PASSPHRASE (_context string)
1302 (if (string-match "\\`\\([^ ]+\\)" string)
1303 (setq epg-key-id (match-string 1 string))))
1304
1305 (defun epg--status-NEED_PASSPHRASE_SYM (_context _string)
1306 (setq epg-key-id 'SYM))
1307
1308 (defun epg--status-NEED_PASSPHRASE_PIN (_context _string)
1309 (setq epg-key-id 'PIN))
1310
1311 (eval-and-compile
1312 (if (fboundp 'clear-string)
1313 (defalias 'epg--clear-string 'clear-string)
1314 (defun epg--clear-string (string)
1315 (fillarray string 0))))
1316
1317 (eval-and-compile
1318 (if (fboundp 'encode-coding-string)
1319 (defalias 'epg--encode-coding-string 'encode-coding-string)
1320 (defalias 'epg--encode-coding-string 'identity)))
1321
1322 (defun epg--status-GET_HIDDEN (context string)
1323 (when (and epg-key-id
1324 (string-match "\\`passphrase\\." string))
1325 (unless (epg-context-passphrase-callback context)
1326 (error "passphrase-callback not set"))
1327 (let (inhibit-quit
1328 passphrase
1329 passphrase-with-new-line
1330 encoded-passphrase-with-new-line)
1331 (unwind-protect
1332 (condition-case nil
1333 (progn
1334 (setq passphrase
1335 (funcall
1336 (car (epg-context-passphrase-callback context))
1337 context
1338 epg-key-id
1339 (cdr (epg-context-passphrase-callback context))))
1340 (when passphrase
1341 (setq passphrase-with-new-line (concat passphrase "\n"))
1342 (epg--clear-string passphrase)
1343 (setq passphrase nil)
1344 (if epg-passphrase-coding-system
1345 (progn
1346 (setq encoded-passphrase-with-new-line
1347 (epg--encode-coding-string
1348 passphrase-with-new-line
1349 (coding-system-change-eol-conversion
1350 epg-passphrase-coding-system 'unix)))
1351 (epg--clear-string passphrase-with-new-line)
1352 (setq passphrase-with-new-line nil))
1353 (setq encoded-passphrase-with-new-line
1354 passphrase-with-new-line
1355 passphrase-with-new-line nil))
1356 (process-send-string (epg-context-process context)
1357 encoded-passphrase-with-new-line)))
1358 (quit
1359 (epg-context-set-result-for
1360 context 'error
1361 (cons '(quit)
1362 (epg-context-result-for context 'error)))
1363 (delete-process (epg-context-process context))))
1364 (if passphrase
1365 (epg--clear-string passphrase))
1366 (if passphrase-with-new-line
1367 (epg--clear-string passphrase-with-new-line))
1368 (if encoded-passphrase-with-new-line
1369 (epg--clear-string encoded-passphrase-with-new-line))))))
1370
1371 (defun epg--prompt-GET_BOOL (_context string)
1372 (let ((entry (assoc string epg-prompt-alist)))
1373 (y-or-n-p (if entry (cdr entry) (concat string "? ")))))
1374
1375 (defun epg--prompt-GET_BOOL-untrusted_key.override (_context _string)
1376 (y-or-n-p (if (and (equal (car epg-last-status) "USERID_HINT")
1377 (string-match "\\`\\([^ ]+\\) \\(.*\\)"
1378 (cdr epg-last-status)))
1379 (let* ((key-id (match-string 1 (cdr epg-last-status)))
1380 (user-id (match-string 2 (cdr epg-last-status)))
1381 (entry (assoc key-id epg-user-id-alist)))
1382 (if entry
1383 (setq user-id (cdr entry)))
1384 (format "Untrusted key %s %s. Use anyway? " key-id user-id))
1385 "Use untrusted key anyway? ")))
1386
1387 (defun epg--status-GET_BOOL (context string)
1388 (let (inhibit-quit)
1389 (condition-case nil
1390 (if (funcall (or (intern-soft (concat "epg--prompt-GET_BOOL-" string))
1391 #'epg--prompt-GET_BOOL)
1392 context string)
1393 (process-send-string (epg-context-process context) "y\n")
1394 (process-send-string (epg-context-process context) "n\n"))
1395 (quit
1396 (epg-context-set-result-for
1397 context 'error
1398 (cons '(quit)
1399 (epg-context-result-for context 'error)))
1400 (delete-process (epg-context-process context))))))
1401
1402 (defun epg--status-GET_LINE (context string)
1403 (let ((entry (assoc string epg-prompt-alist))
1404 inhibit-quit)
1405 (condition-case nil
1406 (process-send-string (epg-context-process context)
1407 (concat (read-string
1408 (if entry
1409 (cdr entry)
1410 (concat string ": ")))
1411 "\n"))
1412 (quit
1413 (epg-context-set-result-for
1414 context 'error
1415 (cons '(quit)
1416 (epg-context-result-for context 'error)))
1417 (delete-process (epg-context-process context))))))
1418
1419 (defun epg--status-*SIG (context status string)
1420 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1421 (let* ((key-id (match-string 1 string))
1422 (user-id (match-string 2 string))
1423 (entry (assoc key-id epg-user-id-alist)))
1424 (epg-context-set-result-for
1425 context
1426 'verify
1427 (cons (epg-make-signature status key-id)
1428 (epg-context-result-for context 'verify)))
1429 (condition-case nil
1430 (if (eq (epg-context-protocol context) 'CMS)
1431 (setq user-id (epg-dn-from-string user-id))
1432 (setq user-id (epg--decode-coding-string
1433 (epg--decode-percent-escape user-id)
1434 'utf-8)))
1435 (error))
1436 (if entry
1437 (setcdr entry user-id)
1438 (setq epg-user-id-alist
1439 (cons (cons key-id user-id) epg-user-id-alist))))
1440 (epg-context-set-result-for
1441 context
1442 'verify
1443 (cons (epg-make-signature status)
1444 (epg-context-result-for context 'verify)))))
1445
1446 (defun epg--status-GOODSIG (context string)
1447 (epg--status-*SIG context 'good string))
1448
1449 (defun epg--status-EXPSIG (context string)
1450 (epg--status-*SIG context 'expired string))
1451
1452 (defun epg--status-EXPKEYSIG (context string)
1453 (epg--status-*SIG context 'expired-key string))
1454
1455 (defun epg--status-REVKEYSIG (context string)
1456 (epg--status-*SIG context 'revoked-key string))
1457
1458 (defun epg--status-BADSIG (context string)
1459 (epg--status-*SIG context 'bad string))
1460
1461 (defun epg--status-NO_PUBKEY (context string)
1462 (if (eq (epg-context-operation context) 'verify)
1463 (let ((signature (car (epg-context-result-for context 'verify))))
1464 (if (and signature
1465 (eq (epg-signature-status signature) 'error)
1466 (equal (epg-signature-key-id signature) string))
1467 (epg-signature-set-status signature 'no-pubkey)))
1468 (epg-context-set-result-for
1469 context 'error
1470 (cons (cons 'no-pubkey string)
1471 (epg-context-result-for context 'error)))))
1472
1473 (defun epg--status-NO_SECKEY (context string)
1474 (epg-context-set-result-for
1475 context 'error
1476 (cons (cons 'no-seckey string)
1477 (epg-context-result-for context 'error))))
1478
1479 (defun epg--time-from-seconds (seconds)
1480 (let ((number-seconds (string-to-number (concat seconds ".0"))))
1481 (cons (floor (/ number-seconds 65536))
1482 (floor (mod number-seconds 65536)))))
1483
1484 (defun epg--status-ERRSIG (context string)
1485 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1486 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
1487 string)
1488 (let ((signature (epg-make-signature 'error)))
1489 (epg-context-set-result-for
1490 context
1491 'verify
1492 (cons signature
1493 (epg-context-result-for context 'verify)))
1494 (epg-signature-set-key-id
1495 signature
1496 (match-string 1 string))
1497 (epg-signature-set-pubkey-algorithm
1498 signature
1499 (string-to-number (match-string 2 string)))
1500 (epg-signature-set-digest-algorithm
1501 signature
1502 (string-to-number (match-string 3 string)))
1503 (epg-signature-set-class
1504 signature
1505 (string-to-number (match-string 4 string) 16))
1506 (epg-signature-set-creation-time
1507 signature
1508 (epg--time-from-seconds (match-string 5 string))))))
1509
1510 (defun epg--status-VALIDSIG (context string)
1511 (let ((signature (car (epg-context-result-for context 'verify))))
1512 (when (and signature
1513 (eq (epg-signature-status signature) 'good)
1514 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
1515 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
1516 \\(.*\\)"
1517 string))
1518 (epg-signature-set-fingerprint
1519 signature
1520 (match-string 1 string))
1521 (epg-signature-set-creation-time
1522 signature
1523 (epg--time-from-seconds (match-string 2 string)))
1524 (unless (equal (match-string 3 string) "0")
1525 (epg-signature-set-expiration-time
1526 signature
1527 (epg--time-from-seconds (match-string 3 string))))
1528 (epg-signature-set-version
1529 signature
1530 (string-to-number (match-string 4 string)))
1531 (epg-signature-set-pubkey-algorithm
1532 signature
1533 (string-to-number (match-string 5 string)))
1534 (epg-signature-set-digest-algorithm
1535 signature
1536 (string-to-number (match-string 6 string)))
1537 (epg-signature-set-class
1538 signature
1539 (string-to-number (match-string 7 string) 16)))))
1540
1541 (defun epg--status-TRUST_UNDEFINED (context _string)
1542 (let ((signature (car (epg-context-result-for context 'verify))))
1543 (if (and signature
1544 (eq (epg-signature-status signature) 'good))
1545 (epg-signature-set-validity signature 'undefined))))
1546
1547 (defun epg--status-TRUST_NEVER (context _string)
1548 (let ((signature (car (epg-context-result-for context 'verify))))
1549 (if (and signature
1550 (eq (epg-signature-status signature) 'good))
1551 (epg-signature-set-validity signature 'never))))
1552
1553 (defun epg--status-TRUST_MARGINAL (context _string)
1554 (let ((signature (car (epg-context-result-for context 'verify))))
1555 (if (and signature
1556 (eq (epg-signature-status signature) 'marginal))
1557 (epg-signature-set-validity signature 'marginal))))
1558
1559 (defun epg--status-TRUST_FULLY (context _string)
1560 (let ((signature (car (epg-context-result-for context 'verify))))
1561 (if (and signature
1562 (eq (epg-signature-status signature) 'good))
1563 (epg-signature-set-validity signature 'full))))
1564
1565 (defun epg--status-TRUST_ULTIMATE (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 'ultimate))))
1570
1571 (defun epg--status-NOTATION_NAME (context string)
1572 (let ((signature (car (epg-context-result-for context 'verify))))
1573 (if signature
1574 (epg-signature-set-notations
1575 signature
1576 (cons (epg-make-sig-notation string nil t nil)
1577 (epg-sig-notations signature))))))
1578
1579 (defun epg--status-NOTATION_DATA (context string)
1580 (let ((signature (car (epg-context-result-for context 'verify)))
1581 notation)
1582 (if (and signature
1583 (setq notation (car (epg-sig-notations signature))))
1584 (epg-sig-notation-set-value notation string))))
1585
1586 (defun epg--status-POLICY_URL (context string)
1587 (let ((signature (car (epg-context-result-for context 'verify))))
1588 (if signature
1589 (epg-signature-set-notations
1590 signature
1591 (cons (epg-make-sig-notation nil string t nil)
1592 (epg-sig-notations signature))))))
1593
1594 (defun epg--status-PROGRESS (context string)
1595 (if (and (epg-context-progress-callback context)
1596 (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1597 string))
1598 (funcall (car (epg-context-progress-callback context))
1599 context
1600 (match-string 1 string)
1601 (match-string 2 string)
1602 (string-to-number (match-string 3 string))
1603 (string-to-number (match-string 4 string))
1604 (cdr (epg-context-progress-callback context)))))
1605
1606 (defun epg--status-ENC_TO (context string)
1607 (if (string-match "\\`\\([0-9A-Za-z]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
1608 (epg-context-set-result-for
1609 context 'encrypted-to
1610 (cons (list (match-string 1 string)
1611 (string-to-number (match-string 2 string))
1612 (string-to-number (match-string 3 string)))
1613 (epg-context-result-for context 'encrypted-to)))))
1614
1615 (defun epg--status-DECRYPTION_FAILED (context _string)
1616 (epg-context-set-result-for context 'decryption-failed t))
1617
1618 (defun epg--status-DECRYPTION_OKAY (context _string)
1619 (epg-context-set-result-for context 'decryption-okay t))
1620
1621 (defun epg--status-NODATA (context string)
1622 (epg-context-set-result-for
1623 context 'error
1624 (cons (cons 'no-data (string-to-number string))
1625 (epg-context-result-for context 'error))))
1626
1627 (defun epg--status-UNEXPECTED (context string)
1628 (epg-context-set-result-for
1629 context 'error
1630 (cons (cons 'unexpected (string-to-number string))
1631 (epg-context-result-for context 'error))))
1632
1633 (defun epg--status-KEYEXPIRED (context string)
1634 (epg-context-set-result-for
1635 context 'key
1636 (cons (list 'key-expired (cons 'expiration-time
1637 (epg--time-from-seconds string)))
1638 (epg-context-result-for context 'key))))
1639
1640 (defun epg--status-KEYREVOKED (context _string)
1641 (epg-context-set-result-for
1642 context 'key
1643 (cons '(key-revoked)
1644 (epg-context-result-for context 'key))))
1645
1646 (defun epg--status-BADARMOR (context _string)
1647 (epg-context-set-result-for
1648 context 'error
1649 (cons '(bad-armor)
1650 (epg-context-result-for context 'error))))
1651
1652 (defun epg--status-INV_RECP (context string)
1653 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1654 (epg-context-set-result-for
1655 context 'error
1656 (cons (list 'invalid-recipient
1657 (cons 'reason
1658 (string-to-number (match-string 1 string)))
1659 (cons 'requested
1660 (match-string 2 string)))
1661 (epg-context-result-for context 'error)))))
1662
1663 (defun epg--status-INV_SGNR (context string)
1664 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1665 (epg-context-set-result-for
1666 context 'error
1667 (cons (list 'invalid-signer
1668 (cons 'reason
1669 (string-to-number (match-string 1 string)))
1670 (cons 'requested
1671 (match-string 2 string)))
1672 (epg-context-result-for context 'error)))))
1673
1674 (defun epg--status-NO_RECP (context _string)
1675 (epg-context-set-result-for
1676 context 'error
1677 (cons '(no-recipients)
1678 (epg-context-result-for context 'error))))
1679
1680 (defun epg--status-NO_SGNR (context _string)
1681 (epg-context-set-result-for
1682 context 'error
1683 (cons '(no-signers)
1684 (epg-context-result-for context 'error))))
1685
1686 (defun epg--status-DELETE_PROBLEM (context string)
1687 (if (string-match "\\`\\([0-9]+\\)" string)
1688 (epg-context-set-result-for
1689 context 'error
1690 (cons (cons 'delete-problem
1691 (string-to-number (match-string 1 string)))
1692 (epg-context-result-for context 'error)))))
1693
1694 (defun epg--status-SIG_CREATED (context string)
1695 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1696 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1697 (epg-context-set-result-for
1698 context 'sign
1699 (cons (epg-make-new-signature
1700 (cdr (assq (aref (match-string 1 string) 0)
1701 epg-new-signature-type-alist))
1702 (string-to-number (match-string 2 string))
1703 (string-to-number (match-string 3 string))
1704 (string-to-number (match-string 4 string) 16)
1705 (epg--time-from-seconds (match-string 5 string))
1706 (substring string (match-end 0)))
1707 (epg-context-result-for context 'sign)))))
1708
1709 (defun epg--status-KEY_CREATED (context string)
1710 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1711 (epg-context-set-result-for
1712 context 'generate-key
1713 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1714 (cons 'fingerprint (match-string 2 string)))
1715 (epg-context-result-for context 'generate-key)))))
1716
1717 (defun epg--status-KEY_NOT_CREATED (context _string)
1718 (epg-context-set-result-for
1719 context 'error
1720 (cons '(key-not-created)
1721 (epg-context-result-for context 'error))))
1722
1723 (defun epg--status-IMPORTED (_context string)
1724 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1725 (let* ((key-id (match-string 1 string))
1726 (user-id (match-string 2 string))
1727 (entry (assoc key-id epg-user-id-alist)))
1728 (condition-case nil
1729 (setq user-id (epg--decode-coding-string
1730 (epg--decode-percent-escape user-id)
1731 'utf-8))
1732 (error))
1733 (if entry
1734 (setcdr entry user-id)
1735 (setq epg-user-id-alist (cons (cons key-id user-id)
1736 epg-user-id-alist))))))
1737
1738 (defun epg--status-IMPORT_OK (context string)
1739 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1740 (let ((reason (string-to-number (match-string 1 string))))
1741 (epg-context-set-result-for
1742 context 'import-status
1743 (cons (epg-make-import-status (if (match-beginning 2)
1744 (match-string 3 string))
1745 nil
1746 (/= (logand reason 1) 0)
1747 (/= (logand reason 2) 0)
1748 (/= (logand reason 4) 0)
1749 (/= (logand reason 8) 0)
1750 (/= (logand reason 16) 0))
1751 (epg-context-result-for context 'import-status))))))
1752
1753 (defun epg--status-IMPORT_PROBLEM (context string)
1754 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1755 (epg-context-set-result-for
1756 context 'import-status
1757 (cons (epg-make-import-status
1758 (if (match-beginning 2)
1759 (match-string 3 string))
1760 (string-to-number (match-string 1 string)))
1761 (epg-context-result-for context 'import-status)))))
1762
1763 (defun epg--status-IMPORT_RES (context string)
1764 (when (string-match "\\`\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1765 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1766 \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
1767 (epg-context-set-result-for
1768 context 'import
1769 (epg-make-import-result (string-to-number (match-string 1 string))
1770 (string-to-number (match-string 2 string))
1771 (string-to-number (match-string 3 string))
1772 (string-to-number (match-string 4 string))
1773 (string-to-number (match-string 5 string))
1774 (string-to-number (match-string 6 string))
1775 (string-to-number (match-string 7 string))
1776 (string-to-number (match-string 8 string))
1777 (string-to-number (match-string 9 string))
1778 (string-to-number (match-string 10 string))
1779 (string-to-number (match-string 11 string))
1780 (string-to-number (match-string 12 string))
1781 (string-to-number (match-string 13 string))
1782 (epg-context-result-for context 'import-status)))
1783 (epg-context-set-result-for context 'import-status nil)))
1784
1785 (defun epg-passphrase-callback-function (context key-id _handback)
1786 (declare (obsolete epa-passphrase-callback-function "23.1"))
1787 (if (eq key-id 'SYM)
1788 (read-passwd "Passphrase for symmetric encryption: "
1789 (eq (epg-context-operation context) 'encrypt))
1790 (read-passwd
1791 (if (eq key-id 'PIN)
1792 "Passphrase for PIN: "
1793 (let ((entry (assoc key-id epg-user-id-alist)))
1794 (if entry
1795 (format "Passphrase for %s %s: " key-id (cdr entry))
1796 (format "Passphrase for %s: " key-id)))))))
1797
1798 (defun epg--list-keys-1 (context name mode)
1799 (let ((args (append (if epg-gpg-home-directory
1800 (list "--homedir" epg-gpg-home-directory))
1801 '("--with-colons" "--no-greeting" "--batch"
1802 "--with-fingerprint" "--with-fingerprint")
1803 (unless (eq (epg-context-protocol context) 'CMS)
1804 '("--fixed-list-mode"))))
1805 (list-keys-option (if (memq mode '(t secret))
1806 "--list-secret-keys"
1807 (if (memq mode '(nil public))
1808 "--list-keys"
1809 "--list-sigs")))
1810 (coding-system-for-read 'binary)
1811 keys string field index)
1812 (if name
1813 (progn
1814 (unless (listp name)
1815 (setq name (list name)))
1816 (while name
1817 (setq args (append args (list list-keys-option (car name)))
1818 name (cdr name))))
1819 (setq args (append args (list list-keys-option))))
1820 (with-temp-buffer
1821 (apply #'call-process
1822 (if (eq (epg-context-protocol context) 'CMS)
1823 epg-gpgsm-program
1824 epg-gpg-program)
1825 nil (list t nil) nil args)
1826 (goto-char (point-min))
1827 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1828 (setq keys (cons (make-vector 15 nil) keys)
1829 string (match-string 0)
1830 index 0
1831 field 0)
1832 (while (eq index
1833 (string-match "\\([^:]+\\)?:" string index))
1834 (setq index (match-end 0))
1835 (aset (car keys) field (match-string 1 string))
1836 (setq field (1+ field))))
1837 (nreverse keys))))
1838
1839 (defun epg--make-sub-key-1 (line)
1840 (epg-make-sub-key
1841 (if (aref line 1)
1842 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1843 (delq nil
1844 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1845 (aref line 11)))
1846 (member (aref line 0) '("sec" "ssb"))
1847 (string-to-number (aref line 3))
1848 (string-to-number (aref line 2))
1849 (aref line 4)
1850 (epg--time-from-seconds (aref line 5))
1851 (if (aref line 6)
1852 (epg--time-from-seconds (aref line 6)))))
1853
1854 (defun epg-list-keys (context &optional name mode)
1855 "Return a list of epg-key objects matched with NAME.
1856 If MODE is nil or 'public, only public keyring should be searched.
1857 If MODE is t or 'secret, only secret keyring should be searched.
1858 Otherwise, only public keyring should be searched and the key
1859 signatures should be included.
1860 NAME is either a string or a list of strings."
1861 (let ((lines (epg--list-keys-1 context name mode))
1862 keys cert pointer pointer-1 index string)
1863 (while lines
1864 (cond
1865 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1866 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1867 keys (cons (epg-make-key
1868 (if (aref (car lines) 8)
1869 (cdr (assq (string-to-char (aref (car lines) 8))
1870 epg-key-validity-alist))))
1871 keys))
1872 (epg-key-set-sub-key-list
1873 (car keys)
1874 (cons (epg--make-sub-key-1 (car lines))
1875 (epg-key-sub-key-list (car keys)))))
1876 ((member (aref (car lines) 0) '("sub" "ssb"))
1877 (epg-key-set-sub-key-list
1878 (car keys)
1879 (cons (epg--make-sub-key-1 (car lines))
1880 (epg-key-sub-key-list (car keys)))))
1881 ((equal (aref (car lines) 0) "uid")
1882 ;; Decode the UID name as a backslash escaped UTF-8 string,
1883 ;; generated by GnuPG/GpgSM.
1884 (setq string (copy-sequence (aref (car lines) 9))
1885 index 0)
1886 (while (string-match "\"" string index)
1887 (setq string (replace-match "\\\"" t t string)
1888 index (1+ (match-end 0))))
1889 (condition-case nil
1890 (setq string (epg--decode-coding-string
1891 (car (read-from-string (concat "\"" string "\"")))
1892 'utf-8))
1893 (error
1894 (setq string (aref (car lines) 9))))
1895 (epg-key-set-user-id-list
1896 (car keys)
1897 (cons (epg-make-user-id
1898 (if (aref (car lines) 1)
1899 (cdr (assq (string-to-char (aref (car lines) 1))
1900 epg-key-validity-alist)))
1901 (if cert
1902 (condition-case nil
1903 (epg-dn-from-string string)
1904 (error string))
1905 string))
1906 (epg-key-user-id-list (car keys)))))
1907 ((equal (aref (car lines) 0) "fpr")
1908 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1909 (aref (car lines) 9)))
1910 ((equal (aref (car lines) 0) "sig")
1911 (epg-user-id-set-signature-list
1912 (car (epg-key-user-id-list (car keys)))
1913 (cons
1914 (epg-make-key-signature
1915 (if (aref (car lines) 1)
1916 (cdr (assq (string-to-char (aref (car lines) 1))
1917 epg-key-validity-alist)))
1918 (string-to-number (aref (car lines) 3))
1919 (aref (car lines) 4)
1920 (epg--time-from-seconds (aref (car lines) 5))
1921 (epg--time-from-seconds (aref (car lines) 6))
1922 (aref (car lines) 9)
1923 (string-to-number (aref (car lines) 10) 16)
1924 (eq (aref (aref (car lines) 10) 2) ?x))
1925 (epg-user-id-signature-list
1926 (car (epg-key-user-id-list (car keys))))))))
1927 (setq lines (cdr lines)))
1928 (setq keys (nreverse keys)
1929 pointer keys)
1930 (while pointer
1931 (epg-key-set-sub-key-list
1932 (car pointer)
1933 (nreverse (epg-key-sub-key-list (car pointer))))
1934 (setq pointer-1 (epg-key-set-user-id-list
1935 (car pointer)
1936 (nreverse (epg-key-user-id-list (car pointer)))))
1937 (while pointer-1
1938 (epg-user-id-set-signature-list
1939 (car pointer-1)
1940 (nreverse (epg-user-id-signature-list (car pointer-1))))
1941 (setq pointer-1 (cdr pointer-1)))
1942 (setq pointer (cdr pointer)))
1943 keys))
1944
1945 (eval-and-compile
1946 (if (fboundp 'make-temp-file)
1947 (defalias 'epg--make-temp-file 'make-temp-file)
1948 (defvar temporary-file-directory)
1949 ;; stolen from poe.el.
1950 (defun epg--make-temp-file (prefix)
1951 "Create a temporary file.
1952 The returned file name (created by appending some random characters at the end
1953 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1954 is guaranteed to point to a newly created empty file.
1955 You can then use `write-region' to write new data into the file."
1956 (let ((orig-modes (default-file-modes))
1957 tempdir tempfile)
1958 (setq prefix (expand-file-name prefix
1959 (if (featurep 'xemacs)
1960 (temp-directory)
1961 temporary-file-directory)))
1962 (unwind-protect
1963 (let (file)
1964 ;; First, create a temporary directory.
1965 (set-default-file-modes #o700)
1966 (while (condition-case ()
1967 (progn
1968 (setq tempdir (make-temp-name
1969 (concat
1970 (file-name-directory prefix)
1971 "DIR")))
1972 ;; return nil or signal an error.
1973 (make-directory tempdir))
1974 ;; let's try again.
1975 (file-already-exists t)))
1976 ;; Second, create a temporary file in the tempdir.
1977 ;; There *is* a race condition between `make-temp-name'
1978 ;; and `write-region', but we don't care it since we are
1979 ;; in a private directory now.
1980 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1981 (write-region "" nil tempfile nil 'silent)
1982 ;; Finally, make a hard-link from the tempfile.
1983 (while (condition-case ()
1984 (progn
1985 (setq file (make-temp-name prefix))
1986 ;; return nil or signal an error.
1987 (add-name-to-file tempfile file))
1988 ;; let's try again.
1989 (file-already-exists t)))
1990 file)
1991 (set-default-file-modes orig-modes)
1992 ;; Cleanup the tempfile.
1993 (and tempfile
1994 (file-exists-p tempfile)
1995 (delete-file tempfile))
1996 ;; Cleanup the tempdir.
1997 (and tempdir
1998 (file-directory-p tempdir)
1999 (delete-directory tempdir)))))))
2000
2001 (defun epg--args-from-sig-notations (notations)
2002 (apply #'nconc
2003 (mapcar
2004 (lambda (notation)
2005 (if (and (epg-sig-notation-name notation)
2006 (not (epg-sig-notation-human-readable notation)))
2007 (error "Unreadable"))
2008 (if (epg-sig-notation-name notation)
2009 (list "--sig-notation"
2010 (if (epg-sig-notation-critical notation)
2011 (concat "!" (epg-sig-notation-name notation)
2012 "=" (epg-sig-notation-value notation))
2013 (concat (epg-sig-notation-name notation)
2014 "=" (epg-sig-notation-value notation))))
2015 (list "--sig-policy-url"
2016 (if (epg-sig-notation-critical notation)
2017 (concat "!" (epg-sig-notation-value notation))
2018 (epg-sig-notation-value notation)))))
2019 notations)))
2020
2021 (defun epg-cancel (context)
2022 (if (buffer-live-p (process-buffer (epg-context-process context)))
2023 (with-current-buffer (process-buffer (epg-context-process context))
2024 (epg-context-set-result-for
2025 epg-context 'error
2026 (cons '(quit)
2027 (epg-context-result-for epg-context 'error)))))
2028 (if (eq (process-status (epg-context-process context)) 'run)
2029 (delete-process (epg-context-process context))))
2030
2031 (defun epg-start-decrypt (context cipher)
2032 "Initiate a decrypt operation on CIPHER.
2033 CIPHER must be a file data object.
2034
2035 If you use this function, you will need to wait for the completion of
2036 `epg-gpg-program' by using `epg-wait-for-completion' and call
2037 `epg-reset' to clear a temporary output file.
2038 If you are unsure, use synchronous version of this function
2039 `epg-decrypt-file' or `epg-decrypt-string' instead."
2040 (unless (epg-data-file cipher)
2041 (error "Not a file"))
2042 (epg-context-set-operation context 'decrypt)
2043 (epg-context-set-result context nil)
2044 (epg--start context (list "--decrypt" "--" (epg-data-file cipher)))
2045 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2046 (unless (eq (epg-context-protocol context) 'CMS)
2047 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
2048
2049 (defun epg--check-error-for-decrypt (context)
2050 (let ((errors (epg-context-result-for context 'error)))
2051 (if (epg-context-result-for context 'decryption-failed)
2052 (signal 'epg-error
2053 (list "Decryption failed" (epg-errors-to-string errors))))
2054 (unless (epg-context-result-for context 'decryption-okay)
2055 (signal 'epg-error
2056 (list "Can't decrypt" (epg-errors-to-string errors))))))
2057
2058 (defun epg-decrypt-file (context cipher plain)
2059 "Decrypt a file CIPHER and store the result to a file PLAIN.
2060 If PLAIN is nil, it returns the result as a string."
2061 (unwind-protect
2062 (progn
2063 (if plain
2064 (epg-context-set-output-file context plain)
2065 (epg-context-set-output-file context
2066 (epg--make-temp-file "epg-output")))
2067 (epg-start-decrypt context (epg-make-data-from-file cipher))
2068 (epg-wait-for-completion context)
2069 (epg--check-error-for-decrypt context)
2070 (unless plain
2071 (epg-read-output context)))
2072 (unless plain
2073 (epg-delete-output-file context))
2074 (epg-reset context)))
2075
2076 (defun epg-decrypt-string (context cipher)
2077 "Decrypt a string CIPHER and return the plain text."
2078 (let ((input-file (epg--make-temp-file "epg-input"))
2079 (coding-system-for-write 'binary))
2080 (unwind-protect
2081 (progn
2082 (write-region cipher nil input-file nil 'quiet)
2083 (epg-context-set-output-file context
2084 (epg--make-temp-file "epg-output"))
2085 (epg-start-decrypt context (epg-make-data-from-file input-file))
2086 (epg-wait-for-completion context)
2087 (epg--check-error-for-decrypt context)
2088 (epg-read-output context))
2089 (epg-delete-output-file context)
2090 (if (file-exists-p input-file)
2091 (delete-file input-file))
2092 (epg-reset context))))
2093
2094 (defun epg-start-verify (context signature &optional signed-text)
2095 "Initiate a verify operation on SIGNATURE.
2096 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
2097
2098 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
2099 For a normal or a cleartext signature, SIGNED-TEXT should be nil.
2100
2101 If you use this function, you will need to wait for the completion of
2102 `epg-gpg-program' by using `epg-wait-for-completion' and call
2103 `epg-reset' to clear a temporary output file.
2104 If you are unsure, use synchronous version of this function
2105 `epg-verify-file' or `epg-verify-string' instead."
2106 (epg-context-set-operation context 'verify)
2107 (epg-context-set-result context nil)
2108 (if signed-text
2109 ;; Detached signature.
2110 (if (epg-data-file signed-text)
2111 (epg--start context (list "--verify" "--" (epg-data-file signature)
2112 (epg-data-file signed-text)))
2113 (epg--start context (list "--verify" "--" (epg-data-file signature)
2114 "-"))
2115 (if (eq (process-status (epg-context-process context)) 'run)
2116 (process-send-string (epg-context-process context)
2117 (epg-data-string signed-text)))
2118 (if (eq (process-status (epg-context-process context)) 'run)
2119 (process-send-eof (epg-context-process context))))
2120 ;; Normal (or cleartext) signature.
2121 (if (epg-data-file signature)
2122 (epg--start context (if (eq (epg-context-protocol context) 'CMS)
2123 (list "--verify" "--" (epg-data-file signature))
2124 (list "--" (epg-data-file signature))))
2125 (epg--start context (if (eq (epg-context-protocol context) 'CMS)
2126 '("--verify" "-")
2127 '("-")))
2128 (if (eq (process-status (epg-context-process context)) 'run)
2129 (process-send-string (epg-context-process context)
2130 (epg-data-string signature)))
2131 (if (eq (process-status (epg-context-process context)) 'run)
2132 (process-send-eof (epg-context-process context))))))
2133
2134 (defun epg-verify-file (context signature &optional signed-text plain)
2135 "Verify a file SIGNATURE.
2136 SIGNED-TEXT and PLAIN are also a file if they are specified.
2137
2138 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
2139 string. For a normal or a cleartext signature, SIGNED-TEXT should be
2140 nil. In the latter case, if PLAIN is specified, the plaintext is
2141 stored into the file after successful verification."
2142 (unwind-protect
2143 (progn
2144 (if plain
2145 (epg-context-set-output-file context plain)
2146 (epg-context-set-output-file context
2147 (epg--make-temp-file "epg-output")))
2148 (if signed-text
2149 (epg-start-verify context
2150 (epg-make-data-from-file signature)
2151 (epg-make-data-from-file signed-text))
2152 (epg-start-verify context
2153 (epg-make-data-from-file signature)))
2154 (epg-wait-for-completion context)
2155 (unless plain
2156 (epg-read-output context)))
2157 (unless plain
2158 (epg-delete-output-file context))
2159 (epg-reset context)))
2160
2161 (defun epg-verify-string (context signature &optional signed-text)
2162 "Verify a string SIGNATURE.
2163 SIGNED-TEXT is a string if it is specified.
2164
2165 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
2166 string. For a normal or a cleartext signature, SIGNED-TEXT should be
2167 nil. In the latter case, this function returns the plaintext after
2168 successful verification."
2169 (let ((coding-system-for-write 'binary)
2170 input-file)
2171 (unwind-protect
2172 (progn
2173 (epg-context-set-output-file context
2174 (epg--make-temp-file "epg-output"))
2175 (if signed-text
2176 (progn
2177 (setq input-file (epg--make-temp-file "epg-signature"))
2178 (write-region signature nil input-file nil 'quiet)
2179 (epg-start-verify context
2180 (epg-make-data-from-file input-file)
2181 (epg-make-data-from-string signed-text)))
2182 (epg-start-verify context (epg-make-data-from-string signature)))
2183 (epg-wait-for-completion context)
2184 (epg-read-output context))
2185 (epg-delete-output-file context)
2186 (if (and input-file
2187 (file-exists-p input-file))
2188 (delete-file input-file))
2189 (epg-reset context))))
2190
2191 (defun epg-start-sign (context plain &optional mode)
2192 "Initiate a sign operation on PLAIN.
2193 PLAIN is a data object.
2194
2195 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2196 If it is nil or 'normal, it makes a normal signature.
2197 Otherwise, it makes a cleartext signature.
2198
2199 If you use this function, you will need to wait for the completion of
2200 `epg-gpg-program' by using `epg-wait-for-completion' and call
2201 `epg-reset' to clear a temporary output file.
2202 If you are unsure, use synchronous version of this function
2203 `epg-sign-file' or `epg-sign-string' instead."
2204 (epg-context-set-operation context 'sign)
2205 (epg-context-set-result context nil)
2206 (unless (memq mode '(t detached nil normal)) ;i.e. cleartext
2207 (epg-context-set-armor context nil)
2208 (epg-context-set-textmode context nil))
2209 (epg--start context
2210 (append (list (if (memq mode '(t detached))
2211 "--detach-sign"
2212 (if (memq mode '(nil normal))
2213 "--sign"
2214 "--clearsign")))
2215 (apply #'nconc
2216 (mapcar
2217 (lambda (signer)
2218 (list "-u"
2219 (epg-sub-key-id
2220 (car (epg-key-sub-key-list signer)))))
2221 (epg-context-signers context)))
2222 (epg--args-from-sig-notations
2223 (epg-context-sig-notations context))
2224 (if (epg-data-file plain)
2225 (list "--" (epg-data-file plain)))))
2226 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2227 (unless (eq (epg-context-protocol context) 'CMS)
2228 (epg-wait-for-status context '("BEGIN_SIGNING")))
2229 (when (epg-data-string plain)
2230 (if (eq (process-status (epg-context-process context)) 'run)
2231 (process-send-string (epg-context-process context)
2232 (epg-data-string plain)))
2233 (if (eq (process-status (epg-context-process context)) 'run)
2234 (process-send-eof (epg-context-process context)))))
2235
2236 (defun epg-sign-file (context plain signature &optional mode)
2237 "Sign a file PLAIN and store the result to a file SIGNATURE.
2238 If SIGNATURE is nil, it returns the result as a string.
2239 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2240 If it is nil or 'normal, it makes a normal signature.
2241 Otherwise, it makes a cleartext signature."
2242 (unwind-protect
2243 (progn
2244 (if signature
2245 (epg-context-set-output-file context signature)
2246 (epg-context-set-output-file context
2247 (epg--make-temp-file "epg-output")))
2248 (epg-start-sign context (epg-make-data-from-file plain) mode)
2249 (epg-wait-for-completion context)
2250 (unless (epg-context-result-for context 'sign)
2251 (let ((errors (epg-context-result-for context 'error)))
2252 (signal 'epg-error
2253 (list "Sign failed" (epg-errors-to-string errors)))))
2254 (unless signature
2255 (epg-read-output context)))
2256 (unless signature
2257 (epg-delete-output-file context))
2258 (epg-reset context)))
2259
2260 (defun epg-sign-string (context plain &optional mode)
2261 "Sign a string PLAIN and return the output as string.
2262 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
2263 If it is nil or 'normal, it makes a normal signature.
2264 Otherwise, it makes a cleartext signature."
2265 (let ((input-file
2266 (unless (or (eq (epg-context-protocol context) 'CMS)
2267 (condition-case nil
2268 (progn
2269 (epg-check-configuration (epg-configuration))
2270 t)
2271 (error)))
2272 (epg--make-temp-file "epg-input")))
2273 (coding-system-for-write 'binary))
2274 (unwind-protect
2275 (progn
2276 (epg-context-set-output-file context
2277 (epg--make-temp-file "epg-output"))
2278 (if input-file
2279 (write-region plain nil input-file nil 'quiet))
2280 (epg-start-sign context
2281 (if input-file
2282 (epg-make-data-from-file input-file)
2283 (epg-make-data-from-string plain))
2284 mode)
2285 (epg-wait-for-completion context)
2286 (unless (epg-context-result-for context 'sign)
2287 (if (epg-context-result-for context 'error)
2288 (let ((errors (epg-context-result-for context 'error)))
2289 (signal 'epg-error
2290 (list "Sign failed" (epg-errors-to-string errors))))))
2291 (epg-read-output context))
2292 (epg-delete-output-file context)
2293 (if input-file
2294 (delete-file input-file))
2295 (epg-reset context))))
2296
2297 (defun epg-start-encrypt (context plain recipients
2298 &optional sign always-trust)
2299 "Initiate an encrypt operation on PLAIN.
2300 PLAIN is a data object.
2301 If RECIPIENTS is nil, it performs symmetric encryption.
2302
2303 If you use this function, you will need to wait for the completion of
2304 `epg-gpg-program' by using `epg-wait-for-completion' and call
2305 `epg-reset' to clear a temporary output file.
2306 If you are unsure, use synchronous version of this function
2307 `epg-encrypt-file' or `epg-encrypt-string' instead."
2308 (epg-context-set-operation context 'encrypt)
2309 (epg-context-set-result context nil)
2310 (epg--start context
2311 (append (if always-trust '("--always-trust"))
2312 (if recipients '("--encrypt") '("--symmetric"))
2313 (if sign '("--sign"))
2314 (if sign
2315 (apply #'nconc
2316 (mapcar
2317 (lambda (signer)
2318 (list "-u"
2319 (epg-sub-key-id
2320 (car (epg-key-sub-key-list
2321 signer)))))
2322 (epg-context-signers context))))
2323 (if sign
2324 (epg--args-from-sig-notations
2325 (epg-context-sig-notations context)))
2326 (apply #'nconc
2327 (mapcar
2328 (lambda (recipient)
2329 (list "-r"
2330 (epg-sub-key-id
2331 (car (epg-key-sub-key-list recipient)))))
2332 recipients))
2333 (if (epg-data-file plain)
2334 (list "--" (epg-data-file plain)))))
2335 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
2336 (unless (eq (epg-context-protocol context) 'CMS)
2337 (if sign
2338 (epg-wait-for-status context '("BEGIN_SIGNING"))
2339 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
2340 (when (epg-data-string plain)
2341 (if (eq (process-status (epg-context-process context)) 'run)
2342 (process-send-string (epg-context-process context)
2343 (epg-data-string plain)))
2344 (if (eq (process-status (epg-context-process context)) 'run)
2345 (process-send-eof (epg-context-process context)))))
2346
2347 (defun epg-encrypt-file (context plain recipients
2348 cipher &optional sign always-trust)
2349 "Encrypt a file PLAIN and store the result to a file CIPHER.
2350 If CIPHER is nil, it returns the result as a string.
2351 If RECIPIENTS is nil, it performs symmetric encryption."
2352 (unwind-protect
2353 (progn
2354 (if cipher
2355 (epg-context-set-output-file context cipher)
2356 (epg-context-set-output-file context
2357 (epg--make-temp-file "epg-output")))
2358 (epg-start-encrypt context (epg-make-data-from-file plain)
2359 recipients sign always-trust)
2360 (epg-wait-for-completion context)
2361 (let ((errors (epg-context-result-for context 'error)))
2362 (if (and sign
2363 (not (epg-context-result-for context 'sign)))
2364 (signal 'epg-error
2365 (list "Sign failed" (epg-errors-to-string errors))))
2366 (if errors
2367 (signal 'epg-error
2368 (list "Encrypt failed" (epg-errors-to-string errors)))))
2369 (unless cipher
2370 (epg-read-output context)))
2371 (unless cipher
2372 (epg-delete-output-file context))
2373 (epg-reset context)))
2374
2375 (defun epg-encrypt-string (context plain recipients
2376 &optional sign always-trust)
2377 "Encrypt a string PLAIN.
2378 If RECIPIENTS is nil, it performs symmetric encryption."
2379 (let ((input-file
2380 (unless (or (not sign)
2381 (eq (epg-context-protocol context) 'CMS)
2382 (condition-case nil
2383 (progn
2384 (epg-check-configuration (epg-configuration))
2385 t)
2386 (error)))
2387 (epg--make-temp-file "epg-input")))
2388 (coding-system-for-write 'binary))
2389 (unwind-protect
2390 (progn
2391 (epg-context-set-output-file context
2392 (epg--make-temp-file "epg-output"))
2393 (if input-file
2394 (write-region plain nil input-file nil 'quiet))
2395 (epg-start-encrypt context
2396 (if input-file
2397 (epg-make-data-from-file input-file)
2398 (epg-make-data-from-string plain))
2399 recipients sign always-trust)
2400 (epg-wait-for-completion context)
2401 (let ((errors (epg-context-result-for context 'error)))
2402 (if (and sign
2403 (not (epg-context-result-for context 'sign)))
2404 (signal 'epg-error
2405 (list "Sign failed" (epg-errors-to-string errors))))
2406 (if errors
2407 (signal 'epg-error
2408 (list "Encrypt failed" (epg-errors-to-string errors)))))
2409 (epg-read-output context))
2410 (epg-delete-output-file context)
2411 (if input-file
2412 (delete-file input-file))
2413 (epg-reset context))))
2414
2415 (defun epg-start-export-keys (context keys)
2416 "Initiate an export keys operation.
2417
2418 If you use this function, you will need to wait for the completion of
2419 `epg-gpg-program' by using `epg-wait-for-completion' and call
2420 `epg-reset' to clear a temporary output file.
2421 If you are unsure, use synchronous version of this function
2422 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
2423 (epg-context-set-operation context 'export-keys)
2424 (epg-context-set-result context nil)
2425 (epg--start context (cons "--export"
2426 (mapcar
2427 (lambda (key)
2428 (epg-sub-key-id
2429 (car (epg-key-sub-key-list key))))
2430 keys))))
2431
2432 (defun epg-export-keys-to-file (context keys file)
2433 "Extract public KEYS."
2434 (unwind-protect
2435 (progn
2436 (if file
2437 (epg-context-set-output-file context file)
2438 (epg-context-set-output-file context
2439 (epg--make-temp-file "epg-output")))
2440 (epg-start-export-keys context keys)
2441 (epg-wait-for-completion context)
2442 (let ((errors (epg-context-result-for context 'error)))
2443 (if errors
2444 (signal 'epg-error
2445 (list "Export keys failed"
2446 (epg-errors-to-string errors)))))
2447 (unless file
2448 (epg-read-output context)))
2449 (unless file
2450 (epg-delete-output-file context))
2451 (epg-reset context)))
2452
2453 (defun epg-export-keys-to-string (context keys)
2454 "Extract public KEYS and return them as a string."
2455 (epg-export-keys-to-file context keys nil))
2456
2457 (defun epg-start-import-keys (context keys)
2458 "Initiate an import keys operation.
2459 KEYS is a data object.
2460
2461 If you use this function, you will need to wait for the completion of
2462 `epg-gpg-program' by using `epg-wait-for-completion' and call
2463 `epg-reset' to clear a temporary output file.
2464 If you are unsure, use synchronous version of this function
2465 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
2466 (epg-context-set-operation context 'import-keys)
2467 (epg-context-set-result context nil)
2468 (epg--start context (if (epg-data-file keys)
2469 (list "--import" "--" (epg-data-file keys))
2470 (list "--import")))
2471 (when (epg-data-string keys)
2472 (if (eq (process-status (epg-context-process context)) 'run)
2473 (process-send-string (epg-context-process context)
2474 (epg-data-string keys)))
2475 (if (eq (process-status (epg-context-process context)) 'run)
2476 (process-send-eof (epg-context-process context)))))
2477
2478 (defun epg--import-keys-1 (context keys)
2479 (unwind-protect
2480 (progn
2481 (epg-start-import-keys context keys)
2482 (epg-wait-for-completion context)
2483 (let ((errors (epg-context-result-for context 'error)))
2484 (if errors
2485 (signal 'epg-error
2486 (list "Import keys failed"
2487 (epg-errors-to-string errors))))))
2488 (epg-reset context)))
2489
2490 (defun epg-import-keys-from-file (context keys)
2491 "Add keys from a file KEYS."
2492 (epg--import-keys-1 context (epg-make-data-from-file keys)))
2493
2494 (defun epg-import-keys-from-string (context keys)
2495 "Add keys from a string KEYS."
2496 (epg--import-keys-1 context (epg-make-data-from-string keys)))
2497
2498 (defun epg-start-receive-keys (context key-id-list)
2499 "Initiate a receive key operation.
2500 KEY-ID-LIST is a list of key IDs.
2501
2502 If you use this function, you will need to wait for the completion of
2503 `epg-gpg-program' by using `epg-wait-for-completion' and call
2504 `epg-reset' to clear a temporary output file.
2505 If you are unsure, use synchronous version of this function
2506 `epg-receive-keys' instead."
2507 (epg-context-set-operation context 'receive-keys)
2508 (epg-context-set-result context nil)
2509 (epg--start context (cons "--recv-keys" key-id-list)))
2510
2511 (defun epg-receive-keys (context keys)
2512 "Add keys from server.
2513 KEYS is a list of key IDs"
2514 (unwind-protect
2515 (progn
2516 (epg-start-receive-keys context keys)
2517 (epg-wait-for-completion context)
2518 (let ((errors (epg-context-result-for context 'error)))
2519 (if errors
2520 (signal 'epg-error
2521 (list "Receive keys failed"
2522 (epg-errors-to-string errors))))))
2523 (epg-reset context)))
2524
2525 (defalias 'epg-import-keys-from-server 'epg-receive-keys)
2526
2527 (defun epg-start-delete-keys (context keys &optional allow-secret)
2528 "Initiate a delete keys operation.
2529
2530 If you use this function, you will need to wait for the completion of
2531 `epg-gpg-program' by using `epg-wait-for-completion' and call
2532 `epg-reset' to clear a temporary output file.
2533 If you are unsure, use synchronous version of this function
2534 `epg-delete-keys' instead."
2535 (epg-context-set-operation context 'delete-keys)
2536 (epg-context-set-result context nil)
2537 (epg--start context (cons (if allow-secret
2538 "--delete-secret-key"
2539 "--delete-key")
2540 (mapcar
2541 (lambda (key)
2542 (epg-sub-key-id
2543 (car (epg-key-sub-key-list key))))
2544 keys))))
2545
2546 (defun epg-delete-keys (context keys &optional allow-secret)
2547 "Delete KEYS from the key ring."
2548 (unwind-protect
2549 (progn
2550 (epg-start-delete-keys context keys allow-secret)
2551 (epg-wait-for-completion context)
2552 (let ((errors (epg-context-result-for context 'error)))
2553 (if errors
2554 (signal 'epg-error
2555 (list "Delete keys failed"
2556 (epg-errors-to-string errors))))))
2557 (epg-reset context)))
2558
2559 (defun epg-start-sign-keys (context keys &optional local)
2560 "Initiate a sign keys operation.
2561
2562 If you use this function, you will need to wait for the completion of
2563 `epg-gpg-program' by using `epg-wait-for-completion' and call
2564 `epg-reset' to clear a temporary output file.
2565 If you are unsure, use synchronous version of this function
2566 `epg-sign-keys' instead."
2567 (declare (obsolete nil "23.1"))
2568 (epg-context-set-operation context 'sign-keys)
2569 (epg-context-set-result context nil)
2570 (epg--start context (cons (if local
2571 "--lsign-key"
2572 "--sign-key")
2573 (mapcar
2574 (lambda (key)
2575 (epg-sub-key-id
2576 (car (epg-key-sub-key-list key))))
2577 keys))))
2578
2579 (defun epg-sign-keys (context keys &optional local)
2580 "Sign KEYS from the key ring."
2581 (declare (obsolete nil "23.1"))
2582 (unwind-protect
2583 (progn
2584 (epg-start-sign-keys context keys local)
2585 (epg-wait-for-completion context)
2586 (let ((errors (epg-context-result-for context 'error)))
2587 (if errors
2588 (signal 'epg-error
2589 (list "Sign keys failed"
2590 (epg-errors-to-string errors))))))
2591 (epg-reset context)))
2592
2593 (defun epg-start-generate-key (context parameters)
2594 "Initiate a key generation.
2595 PARAMETERS specifies parameters for the key.
2596
2597 If you use this function, you will need to wait for the completion of
2598 `epg-gpg-program' by using `epg-wait-for-completion' and call
2599 `epg-reset' to clear a temporary output file.
2600 If you are unsure, use synchronous version of this function
2601 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2602 (epg-context-set-operation context 'generate-key)
2603 (epg-context-set-result context nil)
2604 (if (epg-data-file parameters)
2605 (epg--start context (list "--batch" "--genkey" "--"
2606 (epg-data-file parameters)))
2607 (epg--start context '("--batch" "--genkey"))
2608 (if (eq (process-status (epg-context-process context)) 'run)
2609 (process-send-string (epg-context-process context)
2610 (epg-data-string parameters)))
2611 (if (eq (process-status (epg-context-process context)) 'run)
2612 (process-send-eof (epg-context-process context)))))
2613
2614 (defun epg-generate-key-from-file (context parameters)
2615 "Generate a new key pair.
2616 PARAMETERS is a file which tells how to create the key."
2617 (unwind-protect
2618 (progn
2619 (epg-start-generate-key context (epg-make-data-from-file parameters))
2620 (epg-wait-for-completion context)
2621 (let ((errors (epg-context-result-for context 'error)))
2622 (if errors
2623 (signal 'epg-error
2624 (list "Generate key failed"
2625 (epg-errors-to-string errors))))))
2626 (epg-reset context)))
2627
2628 (defun epg-generate-key-from-string (context parameters)
2629 "Generate a new key pair.
2630 PARAMETERS is a string which tells how to create the key."
2631 (unwind-protect
2632 (progn
2633 (epg-start-generate-key context (epg-make-data-from-string parameters))
2634 (epg-wait-for-completion context)
2635 (let ((errors (epg-context-result-for context 'error)))
2636 (if errors
2637 (signal 'epg-error
2638 (list "Generate key failed"
2639 (epg-errors-to-string errors))))))
2640 (epg-reset context)))
2641
2642 (defun epg--decode-percent-escape (string)
2643 (let ((index 0))
2644 (while (string-match "%\\(\\(%\\)\\|\\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2645 string index)
2646 (if (match-beginning 2)
2647 (setq string (replace-match "%" t t string)
2648 index (1- (match-end 0)))
2649 (setq string (replace-match
2650 (string (string-to-number (match-string 3 string) 16))
2651 t t string)
2652 index (- (match-end 0) 2))))
2653 string))
2654
2655 (defun epg--decode-hexstring (string)
2656 (let ((index 0))
2657 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
2658 (setq string (replace-match (string (string-to-number
2659 (match-string 0 string) 16))
2660 t t string)
2661 index (1- (match-end 0))))
2662 string))
2663
2664 (defun epg--decode-quotedstring (string)
2665 (let ((index 0))
2666 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2667 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\)"
2668 string index)
2669 (if (match-beginning 2)
2670 (setq string (replace-match "\\2" t nil string)
2671 index (1- (match-end 0)))
2672 (if (match-beginning 3)
2673 (setq string (replace-match (string (string-to-number
2674 (match-string 0 string) 16))
2675 t t string)
2676 index (- (match-end 0) 2)))))
2677 string))
2678
2679 (defun epg-dn-from-string (string)
2680 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2681 The return value is an alist mapping from types to values."
2682 (let ((index 0)
2683 (length (length string))
2684 alist type value group)
2685 (while (< index length)
2686 (if (eq index (string-match "[ \t\n\r]*" string index))
2687 (setq index (match-end 0)))
2688 (if (eq index (string-match
2689 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2690 string index))
2691 (setq type (match-string 1 string)
2692 index (match-end 0))
2693 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2694 string index))
2695 (setq type (match-string 1 string)
2696 index (match-end 0))))
2697 (unless type
2698 (error "Invalid type"))
2699 (if (eq index (string-match
2700 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2701 string index))
2702 (setq index (match-end 0)
2703 value (epg--decode-quotedstring (match-string 0 string)))
2704 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
2705 (setq index (match-end 0)
2706 value (epg--decode-hexstring (match-string 1 string)))
2707 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2708 string index))
2709 (setq index (match-end 0)
2710 value (epg--decode-quotedstring
2711 (match-string 0 string))))))
2712 (if group
2713 (if (stringp (car (car alist)))
2714 (setcar alist (list (cons type value) (car alist)))
2715 (setcar alist (cons (cons type value) (car alist))))
2716 (if (consp (car (car alist)))
2717 (setcar alist (nreverse (car alist))))
2718 (setq alist (cons (cons type value) alist)
2719 type nil
2720 value nil))
2721 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
2722 (setq index (match-end 0)
2723 group (eq (aref string (match-beginning 1)) ?+))))
2724 (nreverse alist)))
2725
2726 (defun epg-decode-dn (alist)
2727 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2728 Type names are resolved using `epg-dn-type-alist'."
2729 (mapconcat
2730 (lambda (rdn)
2731 (if (stringp (car rdn))
2732 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
2733 (if entry
2734 (format "%s=%s" (cdr entry) (cdr rdn))
2735 (format "%s=%s" (car rdn) (cdr rdn))))
2736 (concat "(" (epg-decode-dn rdn) ")")))
2737 alist
2738 ", "))
2739
2740 (provide 'epg)
2741
2742 ;;; epg.el ends here