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