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