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