1 ;;; epg.el --- the EasyPG Library
2 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
3 ;; 2005, 2006 Free Software Foundation, Inc.
4 ;; Copyright (C) 2006 Daiki Ueno
6 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Keywords: PGP, GnuPG
9 ;; This file is part of EasyPG.
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 (defvar epg-user-id nil
31 "GnuPG ID of your default identity.")
33 (defvar epg-user-id-alist nil
34 "An alist mapping from key ID to user ID.")
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
56 ;; from gnupg/include/cipher.h
57 (defconst epg-pubkey-algorithm-alist
65 ;; from gnupg/include/cipher.h
66 (defconst epg-digest-algorithm-alist
74 ;; from gnupg/include/cipher.h
75 (defconst epg-compress-algorithm-alist
81 (defconst epg-invalid-recipients-reason-alist
82 '((0 . "No specific reason given")
84 (2 . "Ambigious specification")
85 (3 . "Wrong key usage")
90 (8 . "Policy mismatch")
91 (9 . "Not a secret key")
92 (10 . "Key not trusted")))
94 (defconst epg-delete-problem-reason-alist
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")
103 (4 . "New signatures")
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
135 (defvar epg-key-capablity-alist
139 (?a . authentication)))
141 (defvar epg-new-signature-type-alist
146 (defvar epg-dn-type-alist
147 '(("1.2.840.113549.1.9.1" . "EMail")
151 ("0.2.262.1.10.7.20" . "NameDistinguisher")
152 ("2.5.4.16" . "ADDR")
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 (defun epg-make-data-from-file (file)
162 "Make a data object from FILE."
163 (cons 'epg-data (vector file nil)))
165 (defun epg-make-data-from-string (string)
166 "Make a data object from STRING."
167 (cons 'epg-data (vector nil string)))
169 (defun epg-data-file (data)
170 "Return the file of DATA."
171 (unless (eq (car-safe data) 'epg-data)
172 (signal 'wrong-type-argument (list 'epg-data-p data)))
175 (defun epg-data-string (data)
176 "Return the string of DATA."
177 (unless (eq (car-safe data) 'epg-data)
178 (signal 'wrong-type-argument (list 'epg-data-p data)))
181 (defun epg-make-context (&optional protocol armor textmode include-certs
182 cipher-algorithm digest-algorithm
184 "Return a context object."
186 (vector (or protocol 'OpenPGP) armor textmode include-certs
187 cipher-algorithm digest-algorithm compress-algorithm
188 #'epg-passphrase-callback-function
190 nil nil nil nil nil nil)))
192 (defun epg-context-protocol (context)
193 "Return the protocol used within CONTEXT."
194 (unless (eq (car-safe context) 'epg-context)
195 (signal 'wrong-type-argument (list 'epg-context-p context)))
196 (aref (cdr context) 0))
198 (defun epg-context-armor (context)
199 "Return t if the output shouled be ASCII armored in CONTEXT."
200 (unless (eq (car-safe context) 'epg-context)
201 (signal 'wrong-type-argument (list 'epg-context-p context)))
202 (aref (cdr context) 1))
204 (defun epg-context-textmode (context)
205 "Return t if canonical text mode should be used in CONTEXT."
206 (unless (eq (car-safe context) 'epg-context)
207 (signal 'wrong-type-argument (list 'epg-context-p context)))
208 (aref (cdr context) 2))
210 (defun epg-context-include-certs (context)
211 "Return how many certificates should be included in an S/MIME signed
213 (unless (eq (car-safe context) 'epg-context)
214 (signal 'wrong-type-argument (list 'epg-context-p context)))
215 (aref (cdr context) 3))
217 (defun epg-context-cipher-algorithm (context)
218 "Return the cipher algorithm in CONTEXT."
219 (unless (eq (car-safe context) 'epg-context)
220 (signal 'wrong-type-argument (list 'epg-context-p context)))
221 (aref (cdr context) 4))
223 (defun epg-context-digest-algorithm (context)
224 "Return the digest algorithm in CONTEXT."
225 (unless (eq (car-safe context) 'epg-context)
226 (signal 'wrong-type-argument (list 'epg-context-p context)))
227 (aref (cdr context) 5))
229 (defun epg-context-compress-algorithm (context)
230 "Return the compress algorithm in CONTEXT."
231 (unless (eq (car-safe context) 'epg-context)
232 (signal 'wrong-type-argument (list 'epg-context-p context)))
233 (aref (cdr context) 6))
235 (defun epg-context-passphrase-callback (context)
236 "Return the function used to query passphrase."
237 (unless (eq (car-safe context) 'epg-context)
238 (signal 'wrong-type-argument (list 'epg-context-p context)))
239 (aref (cdr context) 7))
241 (defun epg-context-progress-callback (context)
242 "Return the function which handles progress update."
243 (unless (eq (car-safe context) 'epg-context)
244 (signal 'wrong-type-argument (list 'epg-context-p context)))
245 (aref (cdr context) 8))
247 (defun epg-context-signers (context)
248 "Return the list of key-id for singning."
249 (unless (eq (car-safe context) 'epg-context)
250 (signal 'wrong-type-argument (list 'epg-context-p context)))
251 (aref (cdr context) 9))
253 (defun epg-context-signature-notations (context)
254 "Return the list of notations for singning."
255 (unless (eq (car-safe context) 'epg-context)
256 (signal 'wrong-type-argument (list 'epg-context-p context)))
257 (aref (cdr context) 10))
259 (defun epg-context-process (context)
260 "Return the process object of `epg-gpg-program'.
261 This function is for internal use only."
262 (unless (eq (car-safe context) 'epg-context)
263 (signal 'wrong-type-argument (list 'epg-context-p context)))
264 (aref (cdr context) 11))
266 (defun epg-context-output-file (context)
267 "Return the output file of `epg-gpg-program'.
268 This function is for internal use only."
269 (unless (eq (car-safe context) 'epg-context)
270 (signal 'wrong-type-argument (list 'epg-context-p context)))
271 (aref (cdr context) 12))
273 (defun epg-context-result (context)
274 "Return the result of the previous cryptographic operation."
275 (unless (eq (car-safe context) 'epg-context)
276 (signal 'wrong-type-argument (list 'epg-context-p context)))
277 (aref (cdr context) 13))
279 (defun epg-context-operation (context)
280 "Return the name of the current cryptographic operation."
281 (unless (eq (car-safe context) 'epg-context)
282 (signal 'wrong-type-argument (list 'epg-context-p context)))
283 (aref (cdr context) 14))
285 (defun epg-context-set-protocol (context protocol)
286 "Set the protocol used within CONTEXT."
287 (unless (eq (car-safe context) 'epg-context)
288 (signal 'wrong-type-argument (list 'epg-context-p context)))
289 (aset (cdr context) 0 protocol))
291 (defun epg-context-set-armor (context armor)
292 "Specify if the output shouled be ASCII armored in CONTEXT."
293 (unless (eq (car-safe context) 'epg-context)
294 (signal 'wrong-type-argument (list 'epg-context-p context)))
295 (aset (cdr context) 1 armor))
297 (defun epg-context-set-textmode (context textmode)
298 "Specify if canonical text mode should be used in CONTEXT."
299 (unless (eq (car-safe context) 'epg-context)
300 (signal 'wrong-type-argument (list 'epg-context-p context)))
301 (aset (cdr context) 2 textmode))
303 (defun epg-context-set-include-certs (context include-certs)
304 "Set how many certificates should be included in an S/MIME signed message."
305 (unless (eq (car-safe context) 'epg-context)
306 (signal 'wrong-type-argument (list 'epg-context-p context)))
307 (aset (cdr context) 3 include-certs))
309 (defun epg-context-set-cipher-algorithm (context cipher-algorithm)
310 "Set the cipher algorithm in CONTEXT."
311 (unless (eq (car-safe context) 'epg-context)
312 (signal 'wrong-type-argument (list 'epg-context-p context)))
313 (aset (cdr context) 4 cipher-algorithm))
315 (defun epg-context-set-digest-algorithm (context digest-algorithm)
316 "Set the digest algorithm in CONTEXT."
317 (unless (eq (car-safe context) 'epg-context)
318 (signal 'wrong-type-argument (list 'epg-context-p context)))
319 (aset (cdr context) 5 digest-algorithm))
321 (defun epg-context-set-compress-algorithm (context compress-algorithm)
322 "Set the compress algorithm in CONTEXT."
323 (unless (eq (car-safe context) 'epg-context)
324 (signal 'wrong-type-argument (list 'epg-context-p context)))
325 (aset (cdr context) 6 compress-algorithm))
327 (defun epg-context-set-passphrase-callback (context
329 "Set the function used to query passphrase."
330 (unless (eq (car-safe context) 'epg-context)
331 (signal 'wrong-type-argument (list 'epg-context-p context)))
332 (aset (cdr context) 7 passphrase-callback))
334 (defun epg-context-set-progress-callback (context progress-callback)
335 "Set the function which handles progress update."
336 (unless (eq (car-safe context) 'epg-context)
337 (signal 'wrong-type-argument (list 'epg-context-p context)))
338 (aset (cdr context) 8 progress-callback))
340 (defun epg-context-set-signers (context signers)
341 "Set the list of key-id for singning."
342 (unless (eq (car-safe context) 'epg-context)
343 (signal 'wrong-type-argument (list 'epg-context-p context)))
344 (aset (cdr context) 9 signers))
346 (defun epg-context-set-signature-notations (context notations)
347 "Set the list of notations for singning."
348 (unless (eq (car-safe context) 'epg-context)
349 (signal 'wrong-type-argument (list 'epg-context-p context)))
350 (aset (cdr context) 10 signers))
352 (defun epg-context-set-process (context process)
353 "Set the process object of `epg-gpg-program'.
354 This function is for internal use only."
355 (unless (eq (car-safe context) 'epg-context)
356 (signal 'wrong-type-argument (list 'epg-context-p context)))
357 (aset (cdr context) 11 process))
359 (defun epg-context-set-output-file (context output-file)
360 "Set the output file of `epg-gpg-program'.
361 This function is for internal use only."
362 (unless (eq (car-safe context) 'epg-context)
363 (signal 'wrong-type-argument (list 'epg-context-p context)))
364 (aset (cdr context) 12 output-file))
366 (defun epg-context-set-result (context result)
367 "Set the result of the previous cryptographic operation."
368 (unless (eq (car-safe context) 'epg-context)
369 (signal 'wrong-type-argument (list 'epg-context-p context)))
370 (aset (cdr context) 13 result))
372 (defun epg-context-set-operation (context operation)
373 "Set the name of the current cryptographic operation."
374 (unless (eq (car-safe context) 'epg-context)
375 (signal 'wrong-type-argument (list 'epg-context-p context)))
376 (aset (cdr context) 14 operation))
378 (defun epg-make-signature (status &optional key-id)
379 "Return a signature object."
380 (cons 'epg-signature (vector status key-id nil nil nil nil nil nil nil nil)))
382 (defun epg-signature-status (signature)
383 "Return the status code of SIGNATURE."
384 (unless (eq (car-safe signature) 'epg-signature)
385 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
386 (aref (cdr signature) 0))
388 (defun epg-signature-key-id (signature)
389 "Return the key-id of SIGNATURE."
390 (unless (eq (car-safe signature) 'epg-signature)
391 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
392 (aref (cdr signature) 1))
394 (defun epg-signature-validity (signature)
395 "Return the validity of SIGNATURE."
396 (unless (eq (car-safe signature) 'epg-signature)
397 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
398 (aref (cdr signature) 2))
400 (defun epg-signature-fingerprint (signature)
401 "Return the fingerprint of SIGNATURE."
402 (unless (eq (car-safe signature) 'epg-signature)
403 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
404 (aref (cdr signature) 3))
406 (defun epg-signature-creation-time (signature)
407 "Return the creation time of SIGNATURE."
408 (unless (eq (car-safe signature) 'epg-signature)
409 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
410 (aref (cdr signature) 4))
412 (defun epg-signature-expiration-time (signature)
413 "Return the expiration time of SIGNATURE."
414 (unless (eq (car-safe signature) 'epg-signature)
415 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
416 (aref (cdr signature) 5))
418 (defun epg-signature-pubkey-algorithm (signature)
419 "Return the public key algorithm of SIGNATURE."
420 (unless (eq (car-safe signature) 'epg-signature)
421 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
422 (aref (cdr signature) 6))
424 (defun epg-signature-digest-algorithm (signature)
425 "Return the digest algorithm of SIGNATURE."
426 (unless (eq (car-safe signature) 'epg-signature)
427 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
428 (aref (cdr signature) 7))
430 (defun epg-signature-class (signature)
431 "Return the class of SIGNATURE."
432 (unless (eq (car-safe signature) 'epg-signature)
433 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
434 (aref (cdr signature) 8))
436 (defun epg-signature-version (signature)
437 "Return the version of SIGNATURE."
438 (unless (eq (car-safe signature) 'epg-signature)
439 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
440 (aref (cdr signature) 9))
442 (defun epg-signature-set-status (signature status)
443 "Set the status code of SIGNATURE."
444 (unless (eq (car-safe signature) 'epg-signature)
445 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
446 (aset (cdr signature) 0 status))
448 (defun epg-signature-set-key-id (signature key-id)
449 "Set the key-id of SIGNATURE."
450 (unless (eq (car-safe signature) 'epg-signature)
451 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
452 (aset (cdr signature) 1 key-id))
454 (defun epg-signature-set-validity (signature validity)
455 "Set the validity of SIGNATURE."
456 (unless (eq (car-safe signature) 'epg-signature)
457 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
458 (aset (cdr signature) 2 validity))
460 (defun epg-signature-set-fingerprint (signature fingerprint)
461 "Set the fingerprint of SIGNATURE."
462 (unless (eq (car-safe signature) 'epg-signature)
463 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
464 (aset (cdr signature) 3 fingerprint))
466 (defun epg-signature-set-creation-time (signature creation-time)
467 "Set the creation time of SIGNATURE."
468 (unless (eq (car-safe signature) 'epg-signature)
469 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
470 (aset (cdr signature) 4 creation-time))
472 (defun epg-signature-set-expiration-time (signature expiration-time)
473 "Set the expiration time of SIGNATURE."
474 (unless (eq (car-safe signature) 'epg-signature)
475 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
476 (aset (cdr signature) 5 expiration-time))
478 (defun epg-signature-set-pubkey-algorithm (signature pubkey-algorithm)
479 "Set the public key algorithm of SIGNATURE."
480 (unless (eq (car-safe signature) 'epg-signature)
481 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
482 (aset (cdr signature) 6 pubkey-algorithm))
484 (defun epg-signature-set-digest-algorithm (signature digest-algorithm)
485 "Set the digest algorithm of SIGNATURE."
486 (unless (eq (car-safe signature) 'epg-signature)
487 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
488 (aset (cdr signature) 7 digest-algorithm))
490 (defun epg-signature-set-class (signature class)
491 "Set the class of SIGNATURE."
492 (unless (eq (car-safe signature) 'epg-signature)
493 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
494 (aset (cdr signature) 8 class))
496 (defun epg-signature-set-version (signature version)
497 "Set the version of SIGNATURE."
498 (unless (eq (car-safe signature) 'epg-signature)
499 (signal 'wrong-type-argument (list 'epg-signature-p signature)))
500 (aset (cdr signature) 9 version))
502 (defun epg-make-new-signature (type pubkey-algorithm digest-algorithm
503 class creation-time fingerprint)
504 "Return a new signature object."
505 (cons 'epg-new-signature (vector type pubkey-algorithm digest-algorithm
506 class creation-time fingerprint)))
508 (defun epg-new-signature-type (new-signature)
509 "Return the type of NEW-SIGNATURE."
510 (unless (eq (car-safe new-signature) 'epg-new-signature)
511 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
512 (aref (cdr new-signature) 0))
514 (defun epg-new-signature-pubkey-algorithm (new-signature)
515 "Return the public key algorithm of NEW-SIGNATURE."
516 (unless (eq (car-safe new-signature) 'epg-new-signature)
517 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
518 (aref (cdr new-signature) 1))
520 (defun epg-new-signature-digest-algorithm (new-signature)
521 "Return the digest algorithm of NEW-SIGNATURE."
522 (unless (eq (car-safe new-signature) 'epg-new-signature)
523 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
524 (aref (cdr new-signature) 2))
526 (defun epg-new-signature-class (new-signature)
527 "Return the class of NEW-SIGNATURE."
528 (unless (eq (car-safe new-signature) 'epg-new-signature)
529 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
530 (aref (cdr new-signature) 3))
532 (defun epg-new-signature-creation-time (new-signature)
533 "Return the creation time of NEW-SIGNATURE."
534 (unless (eq (car-safe new-signature) 'epg-new-signature)
535 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
536 (aref (cdr new-signature) 4))
538 (defun epg-new-signature-fingerprint (new-signature)
539 "Return the fingerprint of NEW-SIGNATURE."
540 (unless (eq (car-safe new-signature) 'epg-new-signature)
541 (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
542 (aref (cdr new-signature) 5))
544 (defun epg-make-key (owner-trust)
545 "Return a key object."
546 (cons 'epg-key (vector owner-trust nil nil)))
548 (defun epg-key-owner-trust (key)
549 "Return the owner trust of KEY."
550 (unless (eq (car-safe key) 'epg-key)
551 (signal 'wrong-type-argument (list 'epg-key-p key)))
554 (defun epg-key-sub-key-list (key)
555 "Return the sub key list of KEY."
556 (unless (eq (car-safe key) 'epg-key)
557 (signal 'wrong-type-argument (list 'epg-key-p key)))
560 (defun epg-key-user-id-list (key)
561 "Return the user ID list of KEY."
562 (unless (eq (car-safe key) 'epg-key)
563 (signal 'wrong-type-argument (list 'epg-key-p key)))
566 (defun epg-key-set-sub-key-list (key sub-key-list)
567 "Set the sub key list of KEY."
568 (unless (eq (car-safe key) 'epg-key)
569 (signal 'wrong-type-argument (list 'epg-key-p key)))
570 (aset (cdr key) 1 sub-key-list))
572 (defun epg-key-set-user-id-list (key user-id-list)
573 "Set the user ID list of KEY."
574 (unless (eq (car-safe key) 'epg-key)
575 (signal 'wrong-type-argument (list 'epg-key-p key)))
576 (aset (cdr key) 2 user-id-list))
578 (defun epg-make-sub-key (validity capability secret-p algorithm length id
579 creation-time expiration-time)
580 "Return a sub key object."
582 (vector validity capability secret-p algorithm length id creation-time
583 expiration-time nil)))
585 (defun epg-sub-key-validity (sub-key)
586 "Return the validity of SUB-KEY."
587 (unless (eq (car-safe sub-key) 'epg-sub-key)
588 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
589 (aref (cdr sub-key) 0))
591 (defun epg-sub-key-capability (sub-key)
592 "Return the capability of SUB-KEY."
593 (unless (eq (car-safe sub-key) 'epg-sub-key)
594 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
595 (aref (cdr sub-key) 1))
597 (defun epg-sub-key-secret-p (sub-key)
598 "Return non-nil if SUB-KEY is a secret key."
599 (unless (eq (car-safe sub-key) 'epg-sub-key)
600 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
601 (aref (cdr sub-key) 2))
603 (defun epg-sub-key-algorithm (sub-key)
604 "Return the algorithm of SUB-KEY."
605 (unless (eq (car-safe sub-key) 'epg-sub-key)
606 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
607 (aref (cdr sub-key) 3))
609 (defun epg-sub-key-length (sub-key)
610 "Return the length of SUB-KEY."
611 (unless (eq (car-safe sub-key) 'epg-sub-key)
612 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
613 (aref (cdr sub-key) 4))
615 (defun epg-sub-key-id (sub-key)
616 "Return the ID of SUB-KEY."
617 (unless (eq (car-safe sub-key) 'epg-sub-key)
618 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
619 (aref (cdr sub-key) 5))
621 (defun epg-sub-key-creation-time (sub-key)
622 "Return the creation time of SUB-KEY."
623 (unless (eq (car-safe sub-key) 'epg-sub-key)
624 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
625 (aref (cdr sub-key) 6))
627 (defun epg-sub-key-expiration-time (sub-key)
628 "Return the expiration time of SUB-KEY."
629 (unless (eq (car-safe sub-key) 'epg-sub-key)
630 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
631 (aref (cdr sub-key) 7))
633 (defun epg-sub-key-fingerprint (sub-key)
634 "Return the fingerprint of SUB-KEY."
635 (unless (eq (car-safe sub-key) 'epg-sub-key)
636 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
637 (aref (cdr sub-key) 8))
639 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
640 "Set the fingerprint of SUB-KEY.
641 This function is for internal use only."
642 (unless (eq (car-safe sub-key) 'epg-sub-key)
643 (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
644 (aset (cdr sub-key) 8 fingerprint))
646 (defun epg-make-user-id (validity string)
647 "Return a user ID object."
648 (cons 'epg-user-id (vector validity string nil)))
650 (defun epg-user-id-validity (user-id)
651 "Return the validity of USER-ID."
652 (unless (eq (car-safe user-id) 'epg-user-id)
653 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
654 (aref (cdr user-id) 0))
656 (defun epg-user-id-string (user-id)
657 "Return the name of USER-ID."
658 (unless (eq (car-safe user-id) 'epg-user-id)
659 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
660 (aref (cdr user-id) 1))
662 (defun epg-user-id-signature-list (user-id)
663 "Return the signature list of USER-ID."
664 (unless (eq (car-safe user-id) 'epg-user-id)
665 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
666 (aref (cdr user-id) 2))
668 (defun epg-user-id-set-signature-list (user-id signature-list)
669 "Set the signature list of USER-ID."
670 (unless (eq (car-safe user-id) 'epg-user-id)
671 (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
672 (aset (cdr user-id) 2 signature-list))
674 (defun epg-make-key-signature (validity pubkey-algorithm key-id creation-time
675 expiration-time user-id class
677 "Return a key signature object."
678 (cons 'epg-key-signature
679 (vector validity pubkey-algorithm key-id creation-time expiration-time
680 user-id class exportable-p)))
682 (defun epg-key-signature-validity (key-signature)
683 "Return the validity of KEY-SIGNATURE."
684 (unless (eq (car-safe key-signature) 'epg-key-signature)
685 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
686 (aref (cdr key-signature) 0))
688 (defun epg-key-signature-pubkey-algorithm (key-signature)
689 "Return the public key algorithm of KEY-SIGNATURE."
690 (unless (eq (car-safe key-signature) 'epg-key-signature)
691 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
692 (aref (cdr key-signature) 1))
694 (defun epg-key-signature-key-id (key-signature)
695 "Return the key-id of KEY-SIGNATURE."
696 (unless (eq (car-safe key-signature) 'epg-key-signature)
697 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
698 (aref (cdr key-signature) 2))
700 (defun epg-key-signature-creation-time (key-signature)
701 "Return the creation time of KEY-SIGNATURE."
702 (unless (eq (car-safe key-signature) 'epg-key-signature)
703 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
704 (aref (cdr key-signature) 3))
706 (defun epg-key-signature-expiration-time (key-signature)
707 "Return the expiration time of KEY-SIGNATURE."
708 (unless (eq (car-safe key-signature) 'epg-key-signature)
709 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
710 (aref (cdr key-signature) 4))
712 (defun epg-key-signature-user-id (key-signature)
713 "Return the user-id of KEY-SIGNATURE."
714 (unless (eq (car-safe key-signature) 'epg-key-signature)
715 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
716 (aref (cdr key-signature) 5))
718 (defun epg-key-signature-class (key-signature)
719 "Return the class of KEY-SIGNATURE."
720 (unless (eq (car-safe key-signature) 'epg-key-signature)
721 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
722 (aref (cdr key-signature) 6))
724 (defun epg-key-signature-exportable-p (key-signature)
725 "Return t if KEY-SIGNATURE is exportable."
726 (unless (eq (car-safe key-signature) 'epg-key-signature)
727 (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
728 (aref (cdr key-signature) 7))
730 (defun epg-make-signature-notation (name value &optional human-readable
732 "Return a notation object."
733 (cons 'epg-signature-notation (vector name value human-readable critical)))
735 (defun epg-signature-notation-name (signature-notation)
736 "Return the name of SIGNATURE-NOTATION."
737 (unless (eq (car-safe signature-notation) 'epg-signature-notation)
738 (signal 'wrong-type-argument (list 'epg-signature-notation-p
739 signature-notation)))
740 (aref (cdr signature-notation) 0))
742 (defun epg-signature-notation-value (signature-notation)
743 "Return the value of SIGNATURE-NOTATION."
744 (unless (eq (car-safe signature-notation) 'epg-signature-notation)
745 (signal 'wrong-type-argument (list 'epg-signature-notation-p
746 signature-notation)))
747 (aref (cdr signature-notation) 1))
749 (defun epg-signature-notation-human-readable (signature-notation)
750 "Return the human-readable of SIGNATURE-NOTATION."
751 (unless (eq (car-safe signature-notation) 'epg-signature-notation)
752 (signal 'wrong-type-argument (list 'epg-signature-notation-p
753 signature-notation)))
754 (aref (cdr signature-notation) 2))
756 (defun epg-signature-notation-critical (signature-notation)
757 "Return the critical of SIGNATURE-NOTATION."
758 (unless (eq (car-safe signature-notation) 'epg-signature-notation)
759 (signal 'wrong-type-argument (list 'epg-signature-notation-p
760 signature-notation)))
761 (aref (cdr signature-notation) 3))
763 (defun epg-context-result-for (context name)
764 "Return the result of CONTEXT associated with NAME."
765 (cdr (assq name (epg-context-result context))))
767 (defun epg-context-set-result-for (context name value)
768 "Set the result of CONTEXT associated with NAME to VALUE."
769 (let* ((result (epg-context-result context))
770 (entry (assq name result)))
773 (epg-context-set-result context (cons (cons name value) result)))))
775 (defun epg-signature-to-string (signature)
776 "Convert SIGNATURE to a human readable string."
777 (let ((user-id (cdr (assoc (epg-signature-key-id signature)
778 epg-user-id-alist))))
780 (cond ((eq (epg-signature-status signature) 'good)
781 "Good signature from ")
782 ((eq (epg-signature-status signature) 'bad)
783 "Bad signature from ")
784 ((eq (epg-signature-status signature) 'expired)
785 "Expired signature from ")
786 ((eq (epg-signature-status signature) 'expired-key)
787 "Signature made by expired key ")
788 ((eq (epg-signature-status signature) 'revoked-key)
789 "Signature made by revoked key ")
790 ((eq (epg-signature-status signature) 'no-pubkey)
791 "No public key for "))
792 (epg-signature-key-id signature)
795 (if (stringp user-id)
797 (epg-decode-dn user-id)))
799 (if (epg-signature-validity signature)
800 (format " (trust %s)" (epg-signature-validity signature))
803 (defun epg-verify-result-to-string (verify-result)
804 "Convert VERIFY-RESULT to a human readable string."
805 (mapconcat #'epg-signature-to-string verify-result "\n"))
807 (defun epg-new-signature-to-string (new-signature)
808 "Convert NEW-SIGNATURE to a human readable string."
810 (cond ((eq (epg-new-signature-type new-signature) 'detached)
811 "Detached signature ")
812 ((eq (epg-new-signature-type new-signature) 'clear)
813 "Cleartext signature ")
816 (cdr (assq (epg-new-signature-pubkey-algorithm new-signature)
817 epg-pubkey-algorithm-alist))
819 (cdr (assq (epg-new-signature-digest-algorithm new-signature)
820 epg-digest-algorithm-alist))
822 (format "%02X " (epg-new-signature-class new-signature))
823 (epg-new-signature-fingerprint new-signature)))
825 (defun epg--start (context args)
826 "Start `epg-gpg-program' in a subprocess with given ARGS."
827 (if (and (epg-context-process context)
828 (eq (process-status (epg-context-process context)) 'run))
829 (error "%s is already running in this context"
830 (if (eq (epg-context-protocol context) 'CMS)
833 (let* ((args (append (list "--no-tty"
836 (if (epg-context-progress-callback context)
837 (list "--enable-progress-filter"))
838 (if epg-gpg-home-directory
839 (list "--homedir" epg-gpg-home-directory))
840 (unless (eq (epg-context-protocol context) 'CMS)
841 (list "--command-fd" "0"))
842 (if (epg-context-armor context) '("--armor"))
843 (if (epg-context-textmode context) '("--textmode"))
844 (if (epg-context-output-file context)
845 (list "--output" (epg-context-output-file context)))
847 (coding-system-for-write 'binary)
848 process-connection-type
849 (orig-mode (default-file-modes))
850 (buffer (generate-new-buffer " *epg*"))
854 (unless epg-debug-buffer
855 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
856 (set-buffer epg-debug-buffer)
857 (goto-char (point-max))
858 (insert (format "%s %s\n"
859 (if (eq (epg-context-protocol context) 'CMS)
862 (mapconcat #'identity args " ")))))
863 (with-current-buffer buffer
864 (make-local-variable 'epg-read-point)
865 (setq epg-read-point (point-min))
866 (make-local-variable 'epg-process-filter-running)
867 (setq epg-process-filter-running nil)
868 (make-local-variable 'epg-pending-status-list)
869 (setq epg-pending-status-list nil)
870 (make-local-variable 'epg-key-id)
871 (setq epg-key-id nil)
872 (make-local-variable 'epg-context)
873 (setq epg-context context))
876 (set-default-file-modes 448)
878 (apply #'start-process "epg" buffer
879 (if (eq (epg-context-protocol context) 'CMS)
883 (set-default-file-modes orig-mode))
884 (set-process-filter process #'epg--process-filter)
885 (epg-context-set-process context process)))
887 (defun epg--process-filter (process input)
890 (unless epg-debug-buffer
891 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
892 (set-buffer epg-debug-buffer)
893 (goto-char (point-max))
895 (if (buffer-live-p (process-buffer process))
897 (set-buffer (process-buffer process))
898 (goto-char (point-max))
900 (unless epg-process-filter-running
903 (setq epg-process-filter-running t)
904 (goto-char epg-read-point)
906 (while (looking-at ".*\n") ;the input line finished
907 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
908 (let* ((status (match-string 1))
909 (string (match-string 2))
910 (symbol (intern-soft (concat "epg--status-"
912 (if (member status epg-pending-status-list)
913 (setq epg-pending-status-list nil))
916 (funcall symbol epg-context string))))
918 (setq epg-read-point (point))))
919 (setq epg-process-filter-running nil))))))
921 (defun epg-read-output (context)
922 "Read the output file CONTEXT and return the content as a string."
924 (if (fboundp 'set-buffer-multibyte)
925 (set-buffer-multibyte nil))
926 (if (file-exists-p (epg-context-output-file context))
927 (let ((coding-system-for-read 'binary))
928 (insert-file-contents (epg-context-output-file context))
931 (defun epg-wait-for-status (context status-list)
932 "Wait until one of elements in STATUS-LIST arrives."
933 (with-current-buffer (process-buffer (epg-context-process context))
934 (setq epg-pending-status-list status-list)
935 (while (and (eq (process-status (epg-context-process context)) 'run)
936 epg-pending-status-list)
937 (accept-process-output (epg-context-process context) 1))))
939 (defun epg-wait-for-completion (context)
940 "Wait until the `epg-gpg-program' process completes."
941 (while (eq (process-status (epg-context-process context)) 'run)
942 (accept-process-output (epg-context-process context) 1)))
944 (defun epg-reset (context)
946 (if (and (epg-context-process context)
947 (buffer-live-p (process-buffer (epg-context-process context))))
948 (kill-buffer (process-buffer (epg-context-process context))))
949 (epg-context-set-process context nil))
951 (defun epg-delete-output-file (context)
952 "Delete the output file of CONTEXT."
953 (if (and (epg-context-output-file context)
954 (file-exists-p (epg-context-output-file context)))
955 (delete-file (epg-context-output-file context))))
957 (defun epg--status-USERID_HINT (context string)
958 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
959 (let* ((key-id (match-string 1 string))
960 (user-id (match-string 2 string))
961 (entry (assoc key-id epg-user-id-alist)))
963 (setcdr entry user-id)
964 (setq epg-user-id-alist (cons (cons key-id user-id)
965 epg-user-id-alist))))))
967 (defun epg--status-NEED_PASSPHRASE (context string)
968 (if (string-match "\\`\\([^ ]+\\)" string)
969 (setq epg-key-id (match-string 1 string))))
971 (defun epg--status-NEED_PASSPHRASE_SYM (context string)
972 (setq epg-key-id 'SYM))
974 (defun epg--status-NEED_PASSPHRASE_PIN (context string)
975 (setq epg-key-id 'PIN))
977 (defun epg--status-GET_HIDDEN (context string)
978 (when (and epg-key-id
979 (string-match "\\`passphrase\\." string))
980 (unless (epg-context-passphrase-callback context)
981 (error "passphrase-callback not set"))
984 passphrase-with-new-line
985 encoded-passphrase-with-new-line)
991 (if (consp (epg-context-passphrase-callback context))
992 (car (epg-context-passphrase-callback context))
993 (epg-context-passphrase-callback context))
996 (if (consp (epg-context-passphrase-callback context))
997 (cdr (epg-context-passphrase-callback context)))))
999 (setq passphrase-with-new-line (concat passphrase "\n"))
1000 (epg--clear-string passphrase)
1001 (setq passphrase nil)
1002 (if epg-passphrase-coding-system
1004 (setq encoded-passphrase-with-new-line
1005 (encode-coding-string
1006 passphrase-with-new-line
1007 epg-passphrase-coding-system))
1008 (epg--clear-string passphrase-with-new-line)
1009 (setq passphrase-with-new-line nil))
1010 (setq encoded-passphrase-with-new-line
1011 passphrase-with-new-line
1012 passphrase-with-new-line nil))
1013 (process-send-string (epg-context-process context)
1014 encoded-passphrase-with-new-line)))
1016 (epg-context-set-result-for
1019 (epg-context-result-for context 'error)))
1020 (delete-process (epg-context-process context))))
1022 (epg--clear-string passphrase))
1023 (if passphrase-with-new-line
1024 (epg--clear-string passphrase-with-new-line))
1025 (if encoded-passphrase-with-new-line
1026 (epg--clear-string encoded-passphrase-with-new-line))))))
1028 (defun epg--status-GET_BOOL (context string)
1029 (let ((entry (assoc string epg-prompt-alist))
1032 (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
1033 (process-send-string (epg-context-process context) "y\n")
1034 (process-send-string (epg-context-process context) "n\n"))
1036 (epg-context-set-result-for
1039 (epg-context-result-for context 'error)))
1040 (delete-process (epg-context-process context))))))
1042 (defun epg--status-GET_LINE (context string)
1043 (let ((entry (assoc string epg-prompt-alist))
1046 (process-send-string (epg-context-process context)
1047 (concat (read-string
1050 (concat string ": ")))
1053 (epg-context-set-result-for
1056 (epg-context-result-for context 'error)))
1057 (delete-process (epg-context-process context))))))
1059 (defun epg--status-*SIG (context status string)
1060 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1061 (let* ((key-id (match-string 1 string))
1062 (user-id (match-string 2 string))
1063 (entry (assoc key-id epg-user-id-alist)))
1064 (epg-context-set-result-for
1067 (cons (epg-make-signature status key-id)
1068 (epg-context-result-for context 'verify)))
1069 (if (eq (epg-context-protocol context) 'CMS)
1071 (setq user-id (epg-dn-from-string user-id))
1074 (setcdr entry user-id)
1075 (setq epg-user-id-alist
1076 (cons (cons key-id user-id) epg-user-id-alist))))
1077 (epg-context-set-result-for
1080 (cons (epg-make-signature status)
1081 (epg-context-result-for context 'verify)))))
1083 (defun epg--status-GOODSIG (context string)
1084 (epg--status-*SIG context 'good string))
1086 (defun epg--status-EXPSIG (context string)
1087 (epg--status-*SIG context 'expired string))
1089 (defun epg--status-EXPKEYSIG (context string)
1090 (epg--status-*SIG context 'expired-key string))
1092 (defun epg--status-REVKEYSIG (context string)
1093 (epg--status-*SIG context 'revoked-key string))
1095 (defun epg--status-BADSIG (context string)
1096 (epg--status-*SIG context 'bad string))
1098 (defun epg--status-NO_PUBKEY (context string)
1099 (let ((signature (car (epg-context-result-for context 'verify))))
1101 (eq (epg-signature-status signature) 'error)
1102 (equal (epg-signature-key-id signature) string))
1103 (epg-signature-set-status signature 'no-pubkey))))
1105 (defun epg--time-from-seconds (seconds)
1106 (let ((number-seconds (string-to-number (concat seconds ".0"))))
1107 (cons (floor (/ number-seconds 65536))
1108 (floor (mod number-seconds 65536)))))
1110 (defun epg--status-ERRSIG (context string)
1111 (if (string-match "\\`\\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) \
1112 \\([0-9A-Fa-f][0-9A-Fa-f]\\) \\([^ ]+\\) \\([0-9]+\\)"
1114 (let ((signature (epg-make-signature 'error)))
1115 (epg-context-set-result-for
1119 (epg-context-result-for context 'verify)))
1120 (epg-signature-set-key-id
1122 (match-string 1 string))
1123 (epg-signature-set-pubkey-algorithm
1125 (string-to-number (match-string 2 string)))
1126 (epg-signature-set-digest-algorithm
1128 (string-to-number (match-string 3 string)))
1129 (epg-signature-set-class
1131 (string-to-number (match-string 4 string) 16))
1132 (epg-signature-set-creation-time
1134 (epg--time-from-seconds (match-string 5 string))))))
1136 (defun epg--status-VALIDSIG (context string)
1137 (let ((signature (car (epg-context-result-for context 'verify))))
1138 (when (and signature
1139 (eq (epg-signature-status signature) 'good)
1140 (string-match "\\`\\([^ ]+\\) [^ ]+ \\([^ ]+\\) \\([^ ]+\\) \
1141 \\([0-9]+\\) [^ ]+ \\([0-9]+\\) \\([0-9]+\\) \\([0-9A-Fa-f][0-9A-Fa-f]\\) \
1144 (epg-signature-set-fingerprint
1146 (match-string 1 string))
1147 (epg-signature-set-creation-time
1149 (epg--time-from-seconds (match-string 2 string)))
1150 (epg-signature-set-expiration-time
1152 (epg--time-from-seconds (match-string 3 string)))
1153 (epg-signature-set-version
1155 (string-to-number (match-string 4 string)))
1156 (epg-signature-set-pubkey-algorithm
1158 (string-to-number (match-string 5 string)))
1159 (epg-signature-set-digest-algorithm
1161 (string-to-number (match-string 6 string)))
1162 (epg-signature-set-class
1164 (string-to-number (match-string 7 string) 16)))))
1166 (defun epg--status-TRUST_UNDEFINED (context string)
1167 (let ((signature (car (epg-context-result-for context 'verify))))
1169 (eq (epg-signature-status signature) 'good))
1170 (epg-signature-set-validity signature 'undefined))))
1172 (defun epg--status-TRUST_NEVER (context string)
1173 (let ((signature (car (epg-context-result-for context 'verify))))
1175 (eq (epg-signature-status signature) 'good))
1176 (epg-signature-set-validity signature 'never))))
1178 (defun epg--status-TRUST_MARGINAL (context string)
1179 (let ((signature (car (epg-context-result-for context 'verify))))
1181 (eq (epg-signature-status signature) 'marginal))
1182 (epg-signature-set-validity signature 'marginal))))
1184 (defun epg--status-TRUST_FULLY (context string)
1185 (let ((signature (car (epg-context-result-for context 'verify))))
1187 (eq (epg-signature-status signature) 'good))
1188 (epg-signature-set-validity signature 'full))))
1190 (defun epg--status-TRUST_ULTIMATE (context string)
1191 (let ((signature (car (epg-context-result-for context 'verify))))
1193 (eq (epg-signature-status signature) 'good))
1194 (epg-signature-set-validity signature 'ultimate))))
1196 (defun epg--status-PROGRESS (context string)
1197 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
1199 (funcall (if (consp (epg-context-progress-callback context))
1200 (car (epg-context-progress-callback context))
1201 (epg-context-progress-callback context))
1203 (match-string 1 string)
1204 (match-string 2 string)
1205 (string-to-number (match-string 3 string))
1206 (string-to-number (match-string 4 string))
1207 (if (consp (epg-context-progress-callback context))
1208 (cdr (epg-context-progress-callback context))))))
1210 (defun epg--status-DECRYPTION_FAILED (context string)
1211 (epg-context-set-result-for
1213 (cons '(decryption-failed)
1214 (epg-context-result-for context 'error))))
1216 (defun epg--status-NODATA (context string)
1217 (epg-context-set-result-for
1219 (cons (list 'no-data (cons 'reason (string-to-number string)))
1220 (epg-context-result-for context 'error))))
1222 (defun epg--status-UNEXPECTED (context string)
1223 (epg-context-set-result-for
1225 (cons (list 'unexpected (cons 'reason (string-to-number string)))
1226 (epg-context-result-for context 'error))))
1228 (defun epg--status-KEYEXPIRED (context string)
1229 (epg-context-set-result-for
1231 (cons (list 'key-expired (cons 'expiration-time
1232 (epg--time-from-seconds string)))
1233 (epg-context-result-for context 'error))))
1235 (defun epg--status-KEYREVOKED (context string)
1236 (epg-context-set-result-for
1238 (cons '(key-revoked)
1239 (epg-context-result-for context 'error))))
1241 (defun epg--status-BADARMOR (context string)
1242 (epg-context-set-result-for
1245 (epg-context-result-for context 'error))))
1247 (defun epg--status-INV_RECP (context string)
1248 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
1249 (epg-context-set-result-for
1251 (cons (list 'invalid-recipient
1253 (string-to-number (match-string 1 string)))
1254 (cons 'requested-recipient
1255 (match-string 2 string)))
1256 (epg-context-result-for context 'error)))))
1258 (defun epg--status-NO_RECP (context string)
1259 (epg-context-set-result-for
1261 (cons '(no-recipients)
1262 (epg-context-result-for context 'error))))
1264 (defun epg--status-DELETE_PROBLEM (context string)
1265 (if (string-match "\\`\\([0-9]+\\)" string)
1266 (epg-context-set-result-for
1268 (cons (list 'delete-problem
1269 (cons 'reason (string-to-number (match-string 1 string))))
1270 (epg-context-result-for context 'error)))))
1272 (defun epg--status-SIG_CREATED (context string)
1273 (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
1274 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
1275 (epg-context-set-result-for
1277 (cons (epg-make-new-signature
1278 (cdr (assq (aref (match-string 1 string) 0)
1279 epg-new-signature-type-alist))
1280 (string-to-number (match-string 2 string))
1281 (string-to-number (match-string 3 string))
1282 (string-to-number (match-string 4 string) 16)
1283 (epg--time-from-seconds (match-string 5 string))
1284 (substring string (match-end 0)))
1285 (epg-context-result-for context 'sign)))))
1287 (defun epg--status-KEY_CREATED (context string)
1288 (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
1289 (epg-context-set-result-for
1290 context 'generate-key
1291 (cons (list (cons 'type (string-to-char (match-string 1 string)))
1292 (cons 'fingerprint (match-string 2 string)))
1293 (epg-context-result-for context 'generate-key)))))
1295 (defun epg--status-KEY_NOT_CREATED (context string)
1296 (epg-context-set-result-for
1298 (cons '(key-not-created)
1299 (epg-context-result-for context 'error))))
1301 (defun epg--status-IMPORTED (context string)
1302 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
1303 (let* ((key-id (match-string 1 string))
1304 (user-id (match-string 2 string))
1305 (entry (assoc key-id epg-user-id-alist)))
1307 (setcdr entry user-id)
1308 (setq epg-user-id-alist (cons (cons key-id user-id)
1309 epg-user-id-alist)))
1310 (epg-context-set-result-for
1312 (cons (list (cons 'key-id key-id)
1313 (cons 'user-id user-id))
1314 (epg-context-result-for context 'import))))))
1316 (defun epg--status-IMPORT_OK (context string)
1317 (let ((result (epg-context-result-for context 'import)))
1319 (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string))
1321 (append (list (cons 'reason
1323 (match-string 1 string))))
1324 (if (match-beginning 2)
1325 (list (cons 'fingerprint
1326 (match-string 3 string))))
1329 (defun epg--status-IMPORT_PROBLEM (context string)
1330 (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
1331 (epg-context-set-result-for
1333 (cons (cons 'import-problem
1334 (append (list (cons 'reason
1336 (match-string 1 string))))
1337 (if (match-beginning 2)
1338 (list (cons 'fingerprint
1339 (match-string 3 string))))))
1340 (epg-context-result-for context 'error)))))
1342 (defun epg-passphrase-callback-function (context key-id handback)
1343 (if (eq key-id 'SYM)
1344 (read-passwd "Passphrase for symmetric encryption: "
1345 (eq (epg-context-operation context) 'encrypt))
1347 (if (eq key-id 'PIN)
1348 "Passphrase for PIN: "
1349 (let ((entry (assoc key-id epg-user-id-alist)))
1351 (format "Passphrase for %s %s: " key-id (cdr entry))
1352 (format "Passphrase for %s: " key-id)))))))
1354 (make-obsolete 'epg-passphrase-callback-function
1355 'epa-passphrase-callback-function)
1357 (defun epg--list-keys-1 (context name mode)
1358 (let ((args (append (if epg-gpg-home-directory
1359 (list "--homedir" epg-gpg-home-directory))
1360 (list "--with-colons" "--no-greeting" "--batch"
1361 "--with-fingerprint"
1362 "--with-fingerprint"
1363 (if (memq mode '(t secret))
1364 "--list-secret-keys"
1365 (if (memq mode '(nil public))
1368 (unless (eq (epg-context-protocol context) 'CMS)
1369 '("--fixed-list-mode"))
1370 (if name (list name))))
1371 keys string field index)
1373 (apply #'call-process
1374 (if (eq (epg-context-protocol context) 'CMS)
1377 nil (list t nil) nil args)
1378 (goto-char (point-min))
1379 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
1380 (setq keys (cons (make-vector 15 nil) keys)
1381 string (match-string 0)
1385 (string-match "\\([^:]+\\)?:" string index))
1386 (setq index (match-end 0))
1387 (aset (car keys) field (match-string 1 string))
1388 (setq field (1+ field))))
1391 (defun epg--make-sub-key-1 (line)
1394 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
1396 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
1398 (member (aref line 0) '("sec" "ssb"))
1399 (string-to-number (aref line 3))
1400 (string-to-number (aref line 2))
1402 (epg--time-from-seconds (aref line 5))
1403 (epg--time-from-seconds (aref line 6))))
1406 (defun epg-list-keys (context &optional name mode)
1407 "Return a list of epg-key objects matched with NAME.
1408 If MODE is nil or 'public, only public keyring should be searched.
1409 If MODE is t or 'secret, only secret keyring should be searched.
1410 Otherwise, only public keyring should be searched and the key
1411 signatures should be included."
1412 (let ((lines (epg--list-keys-1 context name mode))
1413 keys cert pointer pointer-1)
1416 ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
1417 (setq cert (member (aref (car lines) 0) '("crt" "crs"))
1418 keys (cons (epg-make-key
1419 (if (aref (car lines) 8)
1420 (cdr (assq (string-to-char (aref (car lines) 8))
1421 epg-key-validity-alist))))
1423 (epg-key-set-sub-key-list
1425 (cons (epg--make-sub-key-1 (car lines))
1426 (epg-key-sub-key-list (car keys)))))
1427 ((member (aref (car lines) 0) '("sub" "ssb"))
1428 (epg-key-set-sub-key-list
1430 (cons (epg--make-sub-key-1 (car lines))
1431 (epg-key-sub-key-list (car keys)))))
1432 ((equal (aref (car lines) 0) "uid")
1433 (epg-key-set-user-id-list
1435 (cons (epg-make-user-id
1436 (if (aref (car lines) 1)
1437 (cdr (assq (string-to-char (aref (car lines) 1))
1438 epg-key-validity-alist)))
1441 (epg-dn-from-string (aref (car lines) 9))
1442 (error (aref (car lines) 9)))
1443 (aref (car lines) 9)))
1444 (epg-key-user-id-list (car keys)))))
1445 ((equal (aref (car lines) 0) "fpr")
1446 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
1447 (aref (car lines) 9)))
1448 ((equal (aref (car lines) 0) "sig")
1449 (epg-user-id-set-signature-list
1450 (car (epg-key-user-id-list (car keys)))
1452 (epg-make-key-signature
1453 (if (aref (car lines) 1)
1454 (cdr (assq (string-to-char (aref (car lines) 1))
1455 epg-key-validity-alist)))
1456 (string-to-number (aref (car lines) 3))
1457 (aref (car lines) 4)
1458 (epg--time-from-seconds (aref (car lines) 5))
1459 (epg--time-from-seconds (aref (car lines) 6))
1460 (aref (car lines) 9)
1461 (string-to-number (aref (car lines) 10) 16)
1462 (eq (aref (aref (car lines) 10) 2) ?x))
1463 (epg-user-id-signature-list
1464 (car (epg-key-user-id-list (car keys))))))))
1465 (setq lines (cdr lines)))
1466 (setq keys (nreverse keys)
1469 (epg-key-set-sub-key-list
1471 (nreverse (epg-key-sub-key-list (car pointer))))
1472 (setq pointer-1 (epg-key-set-user-id-list
1474 (nreverse (epg-key-user-id-list (car pointer)))))
1476 (epg-user-id-set-signature-list
1478 (nreverse (epg-user-id-signature-list (car pointer-1))))
1479 (setq pointer-1 (cdr pointer-1)))
1480 (setq pointer (cdr pointer)))
1483 (if (fboundp 'make-temp-file)
1484 (defalias 'epg--make-temp-file 'make-temp-file)
1485 (defvar temporary-file-directory)
1486 ;; stolen from poe.el.
1487 (defun epg--make-temp-file (prefix)
1488 "Create a temporary file.
1489 The returned file name (created by appending some random characters at the end
1490 of PREFIX, and expanding against `temporary-file-directory' if necessary),
1491 is guaranteed to point to a newly created empty file.
1492 You can then use `write-region' to write new data into the file."
1493 (let (tempdir tempfile)
1494 (setq prefix (expand-file-name prefix
1495 (if (featurep 'xemacs)
1497 temporary-file-directory)))
1500 ;; First, create a temporary directory.
1501 (while (condition-case ()
1503 (setq tempdir (make-temp-name
1505 (file-name-directory prefix)
1507 ;; return nil or signal an error.
1508 (make-directory tempdir))
1510 (file-already-exists t)))
1511 (set-file-modes tempdir 448)
1512 ;; Second, create a temporary file in the tempdir.
1513 ;; There *is* a race condition between `make-temp-name'
1514 ;; and `write-region', but we don't care it since we are
1515 ;; in a private directory now.
1516 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
1517 (write-region "" nil tempfile nil 'silent)
1518 (set-file-modes tempfile 384)
1519 ;; Finally, make a hard-link from the tempfile.
1520 (while (condition-case ()
1522 (setq file (make-temp-name prefix))
1523 ;; return nil or signal an error.
1524 (add-name-to-file tempfile file))
1526 (file-already-exists t)))
1528 ;; Cleanup the tempfile.
1530 (file-exists-p tempfile)
1531 (delete-file tempfile))
1532 ;; Cleanup the tempdir.
1534 (file-directory-p tempdir)
1535 (delete-directory tempdir))))))
1537 (if (fboundp 'clear-string)
1538 (defalias 'epg--clear-string 'clear-string)
1539 (defun epg--clear-string (string)
1540 (fillarray string 0)))
1542 (defun epg--args-from-signature-notations (notations)
1546 (if (and (epg-signature-notation-name notation)
1547 (not (epg-signature-notation-human-readable notation)))
1548 (error "Unreadable"))
1549 (if (epg-signature-notation-name notation)
1550 (list "--sig-notation"
1551 (if (epg-signature-notation-critical notation)
1552 (concat "!" (epg-signature-notation-name notation)
1553 "=" (epg-signature-notation-value notation))
1554 (concat (epg-signature-notation-name notation)
1555 "=" (epg-signature-notation-value notation))))
1556 (list "--sig-policy-url"
1557 (epg-signature-notation-value notation))))
1561 (defun epg-cancel (context)
1562 (if (buffer-live-p (process-buffer (epg-context-process context)))
1564 (set-buffer (process-buffer (epg-context-process context)))
1565 (epg-context-set-result-for
1568 (epg-context-result-for epg-context 'error)))))
1569 (if (eq (process-status (epg-context-process context)) 'run)
1570 (delete-process (epg-context-process context))))
1573 (defun epg-start-decrypt (context cipher)
1574 "Initiate a decrypt operation on CIPHER.
1575 CIPHER must be a file data object.
1577 If you use this function, you will need to wait for the completion of
1578 `epg-gpg-program' by using `epg-wait-for-completion' and call
1579 `epg-reset' to clear a temporaly output file.
1580 If you are unsure, use synchronous version of this function
1581 `epg-decrypt-file' or `epg-decrypt-string' instead."
1582 (unless (epg-data-file cipher)
1583 (error "Not a file"))
1584 (epg-context-set-operation context 'decrypt)
1585 (epg-context-set-result context nil)
1586 (epg--start context (list "--decrypt" "--" (epg-data-file cipher)))
1587 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1588 (unless (eq (epg-context-protocol context) 'CMS)
1589 (epg-wait-for-status context '("BEGIN_DECRYPTION"))))
1592 (defun epg-decrypt-file (context cipher plain)
1593 "Decrypt a file CIPHER and store the result to a file PLAIN.
1594 If PLAIN is nil, it returns the result as a string."
1598 (epg-context-set-output-file context plain)
1599 (epg-context-set-output-file context
1600 (epg--make-temp-file "epg-output")))
1601 (epg-start-decrypt context (epg-make-data-from-file cipher))
1602 (epg-wait-for-completion context)
1603 (if (epg-context-result-for context 'error)
1604 (error "Decrypt failed: %S"
1605 (epg-context-result-for context 'error)))
1607 (epg-read-output context)))
1609 (epg-delete-output-file context))
1610 (epg-reset context)))
1613 (defun epg-decrypt-string (context cipher)
1614 "Decrypt a string CIPHER and return the plain text."
1615 (let ((input-file (epg--make-temp-file "epg-input"))
1616 (coding-system-for-write 'binary))
1619 (write-region cipher nil input-file nil 'quiet)
1620 (epg-context-set-output-file context
1621 (epg--make-temp-file "epg-output"))
1622 (epg-start-decrypt context (epg-make-data-from-file input-file))
1623 (epg-wait-for-completion context)
1624 (if (epg-context-result-for context 'error)
1625 (error "Decrypt failed: %S"
1626 (epg-context-result-for context 'error)))
1627 (epg-read-output context))
1628 (epg-delete-output-file context)
1629 (if (file-exists-p input-file)
1630 (delete-file input-file))
1631 (epg-reset context))))
1634 (defun epg-start-verify (context signature &optional signed-text)
1635 "Initiate a verify operation on SIGNATURE.
1636 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
1638 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
1639 For a normal or a cleartext signature, SIGNED-TEXT should be nil.
1641 If you use this function, you will need to wait for the completion of
1642 `epg-gpg-program' by using `epg-wait-for-completion' and call
1643 `epg-reset' to clear a temporaly output file.
1644 If you are unsure, use synchronous version of this function
1645 `epg-verify-file' or `epg-verify-string' instead."
1646 (epg-context-set-operation context 'verify)
1647 (epg-context-set-result context nil)
1649 ;; Detached signature.
1650 (if (epg-data-file signed-text)
1651 (epg--start context (list "--verify" "--" (epg-data-file signature)
1652 (epg-data-file signed-text)))
1653 (epg--start context (list "--verify" "--" (epg-data-file signature)
1655 (if (eq (process-status (epg-context-process context)) 'run)
1656 (process-send-string (epg-context-process context)
1657 (epg-data-string signed-text)))
1658 (if (eq (process-status (epg-context-process context)) 'run)
1659 (process-send-eof (epg-context-process context))))
1660 ;; Normal (or cleartext) signature.
1661 (if (epg-data-file signature)
1662 (epg--start context (list "--" (epg-data-file signature)))
1663 (epg--start context '("-"))
1664 (if (eq (process-status (epg-context-process context)) 'run)
1665 (process-send-string (epg-context-process context)
1666 (epg-data-string signature)))
1667 (if (eq (process-status (epg-context-process context)) 'run)
1668 (process-send-eof (epg-context-process context))))))
1671 (defun epg-verify-file (context signature &optional signed-text plain)
1672 "Verify a file SIGNATURE.
1673 SIGNED-TEXT and PLAIN are also a file if they are specified.
1675 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1676 string. For a normal or a cleartext signature, SIGNED-TEXT should be
1677 nil. In the latter case, if PLAIN is specified, the plaintext is
1678 stored into the file after successful verification."
1682 (epg-context-set-output-file context plain)
1683 (epg-context-set-output-file context
1684 (epg--make-temp-file "epg-output")))
1686 (epg-start-verify context
1687 (epg-make-data-from-file signature)
1688 (epg-make-data-from-file signed-text))
1689 (epg-start-verify context
1690 (epg-make-data-from-file signature)))
1691 (epg-wait-for-completion context)
1693 (epg-read-output context)))
1695 (epg-delete-output-file context))
1696 (epg-reset context)))
1699 (defun epg-verify-string (context signature &optional signed-text)
1700 "Verify a string SIGNATURE.
1701 SIGNED-TEXT is a string if it is specified.
1703 For a detached signature, both SIGNATURE and SIGNED-TEXT should be
1704 string. For a normal or a cleartext signature, SIGNED-TEXT should be
1705 nil. In the latter case, this function returns the plaintext after
1706 successful verification."
1707 (let ((coding-system-for-write 'binary)
1711 (epg-context-set-output-file context
1712 (epg--make-temp-file "epg-output"))
1715 (setq input-file (epg--make-temp-file "epg-signature"))
1716 (write-region signature nil input-file nil 'quiet)
1717 (epg-start-verify context
1718 (epg-make-data-from-file input-file)
1719 (epg-make-data-from-string signed-text)))
1720 (epg-start-verify context (epg-make-data-from-string signature)))
1721 (epg-wait-for-completion context)
1722 (epg-read-output context))
1723 (epg-delete-output-file context)
1725 (file-exists-p input-file))
1726 (delete-file input-file))
1727 (epg-reset context))))
1730 (defun epg-start-sign (context plain &optional mode)
1731 "Initiate a sign operation on PLAIN.
1732 PLAIN is a data object.
1734 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1735 If it is nil or 'normal, it makes a normal signature.
1736 Otherwise, it makes a cleartext signature.
1738 If you use this function, you will need to wait for the completion of
1739 `epg-gpg-program' by using `epg-wait-for-completion' and call
1740 `epg-reset' to clear a temporaly output file.
1741 If you are unsure, use synchronous version of this function
1742 `epg-sign-file' or `epg-sign-string' instead."
1743 (epg-context-set-operation context 'sign)
1744 (epg-context-set-result context nil)
1745 (unless (memq mode '(t detached nil normal)) ;i.e. cleartext
1746 (epg-context-set-armor context nil)
1747 (epg-context-set-textmode context nil))
1749 (append (list (if (memq mode '(t detached))
1751 (if (memq mode '(nil normal))
1759 (car (epg-key-sub-key-list signer)))))
1760 (epg-context-signers context)))
1761 (epg--args-from-signature-notations
1762 (epg-context-signature-notations context))
1763 (if (epg-data-file plain)
1764 (list "--" (epg-data-file plain)))))
1765 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1766 (unless (eq (epg-context-protocol context) 'CMS)
1767 (epg-wait-for-status context '("BEGIN_SIGNING")))
1768 (when (epg-data-string plain)
1769 (if (eq (process-status (epg-context-process context)) 'run)
1770 (process-send-string (epg-context-process context)
1771 (epg-data-string plain)))
1772 (if (eq (process-status (epg-context-process context)) 'run)
1773 (process-send-eof (epg-context-process context)))))
1776 (defun epg-sign-file (context plain signature &optional mode)
1777 "Sign a file PLAIN and store the result to a file SIGNATURE.
1778 If SIGNATURE is nil, it returns the result as a string.
1779 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1780 If it is nil or 'normal, it makes a normal signature.
1781 Otherwise, it makes a cleartext signature."
1785 (epg-context-set-output-file context signature)
1786 (epg-context-set-output-file context
1787 (epg--make-temp-file "epg-output")))
1788 (epg-start-sign context (epg-make-data-from-file plain) mode)
1789 (epg-wait-for-completion context)
1790 (unless (epg-context-result-for context 'sign)
1791 (if (epg-context-result-for context 'error)
1792 (error "Sign failed: %S"
1793 (epg-context-result-for context 'error))
1794 (error "Sign failed")))
1796 (epg-read-output context)))
1798 (epg-delete-output-file context))
1799 (epg-reset context)))
1802 (defun epg-sign-string (context plain &optional mode)
1803 "Sign a string PLAIN and return the output as string.
1804 If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
1805 If it is nil or 'normal, it makes a normal signature.
1806 Otherwise, it makes a cleartext signature."
1809 (epg-context-set-output-file context
1810 (epg--make-temp-file "epg-output"))
1811 (epg-start-sign context (epg-make-data-from-string plain) mode)
1812 (epg-wait-for-completion context)
1813 (unless (epg-context-result-for context 'sign)
1814 (if (epg-context-result-for context 'error)
1815 (error "Sign failed: %S"
1816 (epg-context-result-for context 'error))
1817 (error "Sign failed")))
1818 (epg-read-output context))
1819 (epg-delete-output-file context)
1820 (epg-reset context)))
1823 (defun epg-start-encrypt (context plain recipients
1824 &optional sign always-trust)
1825 "Initiate an encrypt operation on PLAIN.
1826 PLAIN is a data object.
1827 If RECIPIENTS is nil, it performs symmetric encryption.
1829 If you use this function, you will need to wait for the completion of
1830 `epg-gpg-program' by using `epg-wait-for-completion' and call
1831 `epg-reset' to clear a temporaly output file.
1832 If you are unsure, use synchronous version of this function
1833 `epg-encrypt-file' or `epg-encrypt-string' instead."
1834 (epg-context-set-operation context 'encrypt)
1835 (epg-context-set-result context nil)
1837 (append (if always-trust '("--always-trust"))
1838 (if recipients '("--encrypt") '("--symmetric"))
1839 (if sign '("--sign"))
1846 (car (epg-key-sub-key-list
1848 (epg-context-signers context))))
1850 (epg--args-from-signature-notations
1851 (epg-context-signature-notations context)))
1857 (car (epg-key-sub-key-list recipient)))))
1859 (if (epg-data-file plain)
1860 (list "--" (epg-data-file plain)))))
1861 ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
1862 (unless (eq (epg-context-protocol context) 'CMS)
1864 (epg-wait-for-status context '("BEGIN_SIGNING"))
1865 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
1866 (when (epg-data-string plain)
1867 (if (eq (process-status (epg-context-process context)) 'run)
1868 (process-send-string (epg-context-process context)
1869 (epg-data-string plain)))
1870 (if (eq (process-status (epg-context-process context)) 'run)
1871 (process-send-eof (epg-context-process context)))))
1874 (defun epg-encrypt-file (context plain recipients
1875 cipher &optional sign always-trust)
1876 "Encrypt a file PLAIN and store the result to a file CIPHER.
1877 If CIPHER is nil, it returns the result as a string.
1878 If RECIPIENTS is nil, it performs symmetric encryption."
1882 (epg-context-set-output-file context cipher)
1883 (epg-context-set-output-file context
1884 (epg--make-temp-file "epg-output")))
1885 (epg-start-encrypt context (epg-make-data-from-file plain)
1886 recipients sign always-trust)
1887 (epg-wait-for-completion context)
1889 (not (epg-context-result-for context 'sign)))
1890 (if (epg-context-result-for context 'error)
1891 (error "Sign failed: %S"
1892 (epg-context-result-for context 'error))
1893 (error "Sign failed")))
1894 (if (epg-context-result-for context 'error)
1895 (error "Encrypt failed: %S"
1896 (epg-context-result-for context 'error)))
1898 (epg-read-output context)))
1900 (epg-delete-output-file context))
1901 (epg-reset context)))
1904 (defun epg-encrypt-string (context plain recipients
1905 &optional sign always-trust)
1906 "Encrypt a string PLAIN.
1907 If RECIPIENTS is nil, it performs symmetric encryption."
1910 (epg-context-set-output-file context
1911 (epg--make-temp-file "epg-output"))
1912 (epg-start-encrypt context (epg-make-data-from-string plain)
1913 recipients sign always-trust)
1914 (epg-wait-for-completion context)
1916 (not (epg-context-result-for context 'sign)))
1917 (if (epg-context-result-for context 'error)
1918 (error "Sign failed: %S"
1919 (epg-context-result-for context 'error))
1920 (error "Sign failed")))
1921 (if (epg-context-result-for context 'error)
1922 (error "Encrypt failed: %S"
1923 (epg-context-result-for context 'error)))
1924 (epg-read-output context))
1925 (epg-delete-output-file context)
1926 (epg-reset context)))
1929 (defun epg-start-export-keys (context keys)
1930 "Initiate an export keys operation.
1932 If you use this function, you will need to wait for the completion of
1933 `epg-gpg-program' by using `epg-wait-for-completion' and call
1934 `epg-reset' to clear a temporaly output file.
1935 If you are unsure, use synchronous version of this function
1936 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1937 (epg-context-set-operation context 'export-keys)
1938 (epg-context-set-result context nil)
1939 (epg--start context (cons "--export"
1943 (car (epg-key-sub-key-list key))))
1947 (defun epg-export-keys-to-file (context keys file)
1948 "Extract public KEYS."
1952 (epg-context-set-output-file context file)
1953 (epg-context-set-output-file context
1954 (epg--make-temp-file "epg-output")))
1955 (epg-start-export-keys context keys)
1956 (epg-wait-for-completion context)
1957 (if (epg-context-result-for context 'error)
1958 (error "Export keys failed: %S"
1959 (epg-context-result-for context 'error)))
1961 (epg-read-output context)))
1963 (epg-delete-output-file context))
1964 (epg-reset context)))
1967 (defun epg-export-keys-to-string (context keys)
1968 "Extract public KEYS and return them as a string."
1969 (epg-export-keys-to-file context keys nil))
1972 (defun epg-start-import-keys (context keys)
1973 "Initiate an import keys operation.
1974 KEYS is a data object.
1976 If you use this function, you will need to wait for the completion of
1977 `epg-gpg-program' by using `epg-wait-for-completion' and call
1978 `epg-reset' to clear a temporaly output file.
1979 If you are unsure, use synchronous version of this function
1980 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1981 (epg-context-set-operation context 'import-keys)
1982 (epg-context-set-result context nil)
1983 (epg--start context (if (epg-data-file keys)
1984 (list "--import" "--" (epg-data-file keys))
1986 (when (epg-data-string keys)
1987 (if (eq (process-status (epg-context-process context)) 'run)
1988 (process-send-string (epg-context-process context)
1989 (epg-data-string keys)))
1990 (if (eq (process-status (epg-context-process context)) 'run)
1991 (process-send-eof (epg-context-process context)))))
1993 (defun epg--import-keys-1 (context keys)
1996 (epg-start-import-keys context keys)
1997 (epg-wait-for-completion context)
1998 (if (epg-context-result-for context 'error)
1999 (error "Import keys failed: %S"
2000 (epg-context-result-for context 'error))))
2001 (epg-reset context)))
2004 (defun epg-import-keys-from-file (context keys)
2005 "Add keys from a file KEYS."
2006 (epg--import-keys-1 context (epg-make-data-from-file keys)))
2009 (defun epg-import-keys-from-string (context keys)
2010 "Add keys from a string KEYS."
2011 (epg--import-keys-1 context (epg-make-data-from-string keys)))
2014 (defun epg-start-receive-keys (context key-id-list)
2015 "Initiate a receive key operation.
2016 KEY-ID-LIST is a list of key IDs.
2018 If you use this function, you will need to wait for the completion of
2019 `epg-gpg-program' by using `epg-wait-for-completion' and call
2020 `epg-reset' to clear a temporaly output file.
2021 If you are unsure, use synchronous version of this function
2022 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2023 (epg-context-set-operation context 'receive-keys)
2024 (epg-context-set-result context nil)
2025 (epg--start context (cons "--recv-keys" key-id-list)))
2028 (defun epg-receive-keys (context keys)
2029 "Add keys from server.
2030 KEYS is a list of key IDs"
2033 (epg-start-receive-keys context keys)
2034 (epg-wait-for-completion context)
2035 (if (epg-context-result-for context 'error)
2036 (error "Receive keys failed: %S"
2037 (epg-context-result-for context 'error))))
2038 (epg-reset context)))
2041 (defalias 'epg-import-keys-from-server 'epg-receive-keys)
2044 (defun epg-start-delete-keys (context keys &optional allow-secret)
2045 "Initiate an delete keys operation.
2047 If you use this function, you will need to wait for the completion of
2048 `epg-gpg-program' by using `epg-wait-for-completion' and call
2049 `epg-reset' to clear a temporaly output file.
2050 If you are unsure, use synchronous version of this function
2051 `epg-delete-keys' instead."
2052 (epg-context-set-operation context 'delete-keys)
2053 (epg-context-set-result context nil)
2054 (epg--start context (cons (if allow-secret
2055 "--delete-secret-key"
2060 (car (epg-key-sub-key-list key))))
2064 (defun epg-delete-keys (context keys &optional allow-secret)
2065 "Delete KEYS from the key ring."
2068 (epg-start-delete-keys context keys allow-secret)
2069 (epg-wait-for-completion context)
2070 (if (epg-context-result-for context 'error)
2071 (error "Delete keys failed: %S"
2072 (epg-context-result-for context 'error))))
2073 (epg-reset context)))
2076 (defun epg-start-sign-keys (context keys &optional local)
2077 "Initiate an sign keys operation.
2079 If you use this function, you will need to wait for the completion of
2080 `epg-gpg-program' by using `epg-wait-for-completion' and call
2081 `epg-reset' to clear a temporaly output file.
2082 If you are unsure, use synchronous version of this function
2083 `epg-sign-keys' instead."
2084 (epg-context-set-operation context 'sign-keys)
2085 (epg-context-set-result context nil)
2086 (epg--start context (cons (if local
2092 (car (epg-key-sub-key-list key))))
2096 (defun epg-sign-keys (context keys &optional local)
2097 "Sign KEYS from the key ring."
2100 (epg-start-sign-keys context keys local)
2101 (epg-wait-for-completion context)
2102 (if (epg-context-result-for context 'error)
2103 (error "Sign keys failed: %S"
2104 (epg-context-result-for context 'error))))
2105 (epg-reset context)))
2108 (defun epg-start-generate-key (context parameters)
2109 "Initiate a key generation.
2110 PARAMETERS specifies parameters for the key.
2112 If you use this function, you will need to wait for the completion of
2113 `epg-gpg-program' by using `epg-wait-for-completion' and call
2114 `epg-reset' to clear a temporaly output file.
2115 If you are unsure, use synchronous version of this function
2116 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
2117 (epg-context-set-operation context 'generate-key)
2118 (epg-context-set-result context nil)
2119 (if (epg-data-file parameters)
2120 (epg--start context (list "--batch" "--genkey" "--"
2121 (epg-data-file parameters)))
2122 (epg--start context '("--batch" "--genkey"))
2123 (if (eq (process-status (epg-context-process context)) 'run)
2124 (process-send-string (epg-context-process context)
2125 (epg-data-string parameters)))
2126 (if (eq (process-status (epg-context-process context)) 'run)
2127 (process-send-eof (epg-context-process context)))))
2130 (defun epg-generate-key-from-file (context parameters)
2131 "Generate a new key pair.
2132 PARAMETERS is a file which tells how to create the key."
2135 (epg-start-generate-key context (epg-make-data-from-file parameters))
2136 (epg-wait-for-completion context)
2137 (if (epg-context-result-for context 'error)
2138 (error "Generate key failed: %S"
2139 (epg-context-result-for context 'error))))
2140 (epg-reset context)))
2143 (defun epg-generate-key-from-string (context parameters)
2144 "Generate a new key pair.
2145 PARAMETERS is a string which tells how to create the key."
2148 (epg-start-generate-key context (epg-make-data-from-string parameters))
2149 (epg-wait-for-completion context)
2150 (if (epg-context-result-for context 'error)
2151 (error "Generate key failed: %S"
2152 (epg-context-result-for context 'error))))
2153 (epg-reset context)))
2155 (defun epg--decode-hexstring (string)
2157 (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))
2158 (setq string (replace-match "\\x\\&" t nil string)
2160 (car (read-from-string (concat "\"" string "\"")))))
2162 (defun epg--decode-quotedstring (string)
2164 (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\
2165 \\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)"
2167 (if (match-beginning 2)
2168 (setq string (replace-match "\\2" t nil string)
2170 (if (match-beginning 3)
2171 (setq string (replace-match "\\x\\3" t nil string)
2173 (setq string (replace-match "\\\\\\\\\\4" t nil string)
2174 index (+ index 3)))))
2175 (car (read-from-string (concat "\"" string "\"")))))
2177 (defun epg-dn-from-string (string)
2178 "Parse STRING as LADPv3 Distinguished Names (RFC2253).
2179 The return value is an alist mapping from types to values."
2181 (length (length string))
2182 alist type value group)
2183 (while (< index length)
2184 (if (eq index (string-match "[ \t\n\r]*" string index))
2185 (setq index (match-end 0)))
2186 (if (eq index (string-match
2187 "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*"
2189 (setq type (match-string 1 string)
2190 index (match-end 0))
2191 (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*"
2193 (setq type (match-string 1 string)
2194 index (match-end 0))))
2196 (error "Invalid type"))
2197 (if (eq index (string-match
2198 "\\([^,=+<>#;\\\"]\\|\\\\.\\)+"
2200 (setq index (match-end 0)
2201 value (epg--decode-quotedstring (match-string 0 string)))
2202 (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index))
2203 (setq index (match-end 0)
2204 value (epg--decode-hexstring (match-string 1 string)))
2205 (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\""
2207 (setq index (match-end 0)
2208 value (epg--decode-quotedstring
2209 (match-string 0 string))))))
2211 (if (stringp (car (car alist)))
2212 (setcar alist (list (cons type value) (car alist)))
2213 (setcar alist (cons (cons type value) (car alist))))
2214 (if (consp (car (car alist)))
2215 (setcar alist (nreverse (car alist))))
2216 (setq alist (cons (cons type value) alist)
2219 (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index))
2220 (setq index (match-end 0)
2221 group (eq (aref string (match-beginning 1)) ?+))))
2224 (defun epg-decode-dn (alist)
2225 "Convert ALIST returned by `epg-dn-from-string' to a human readable form.
2226 Type names are resolved using `epg-dn-type-alist'."
2229 (if (stringp (car rdn))
2230 (let ((entry (assoc (car rdn) epg-dn-type-alist)))
2232 (format "%s=%s" (cdr entry) (cdr rdn))
2233 (format "%s=%s" (car rdn) (cdr rdn))))
2234 (concat "(" (epg-decode-dn rdn) ")")))
2240 ;;; epg.el ends here