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