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