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