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