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