;;; epg.el --- the EasyPG Library
;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
-;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
+;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
;; Author: Daiki Ueno <ueno@unixuser.org>
;; Keywords: PGP, GnuPG
;; This file is part of GNU Emacs.
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 3, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING. If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Code:
(8 . "AES192")
(9 . "AES256")
(10 . "TWOFISH")
+ (11 . "CAMELLIA128")
+ (12 . "CAMELLIA256")
(110 . "DUMMY")))
;; from gnupg/include/cipher.h
(defconst epg-digest-algorithm-alist
'((1 . "MD5")
(2 . "SHA1")
- (3 . "RMD160")
+ (3 . "RIPEMD160")
(8 . "SHA256")
(9 . "SHA384")
- (10 . "SHA512")))
+ (10 . "SHA512")
+ (11 . "SHA224")))
;; from gnupg/include/cipher.h
(defconst epg-compress-algorithm-alist
(signal 'wrong-type-argument (list 'epg-data-p data)))
(aref (cdr data) 1))
+;;;###autoload
(defun epg-make-context (&optional protocol armor textmode include-certs
cipher-algorithm digest-algorithm
compress-algorithm)
(cons 'epg-context
(vector (or protocol 'OpenPGP) armor textmode include-certs
cipher-algorithm digest-algorithm compress-algorithm
- #'epg-passphrase-callback-function
+ (list #'epg-passphrase-callback-function)
nil
nil nil nil nil nil nil)))
(defun epg-context-set-passphrase-callback (context
passphrase-callback)
- "Set the function used to query passphrase."
+ "Set the function used to query passphrase.
+
+PASSPHRASE-CALLBACK is either a function, or a cons-cell whose
+car is a function and cdr is a callback data.
+
+The function gets three arguments: the context, the key-id in
+question, and the callback data (if any).
+
+The callback may not be called if you use GnuPG 2.x, which relies
+on the external program called `gpg-agent' for passphrase query.
+If you really want to intercept passphrase query, consider
+installing GnuPG 1.x _along with_ GnuPG 2.x, which does passphrase
+query by itself and Emacs can intercept them."
(unless (eq (car-safe context) 'epg-context)
(signal 'wrong-type-argument (list 'epg-context-p context)))
- (aset (cdr context) 7 passphrase-callback))
+ (aset (cdr context) 7 (if (consp passphrase-callback)
+ passphrase-callback
+ (list passphrase-callback))))
(defun epg-context-set-progress-callback (context
progress-callback)
"Set the function which handles progress update.
-If optional argument HANDBACK is specified, it is passed to PROGRESS-CALLBACK."
+
+PROGRESS-CALLBACK is either a function, or a cons-cell whose
+car is a function and cdr is a callback data.
+
+The function gets five arguments: the context, the operation
+description, the character to display a progress unit, the
+current amount done, the total amount to be done, and the
+callback data (if any)."
(unless (eq (car-safe context) 'epg-context)
(signal 'wrong-type-argument (list 'epg-context-p context)))
- (aset (cdr context) 8 progress-callback))
+ (aset (cdr context) 8 (if (consp progress-callback)
+ progress-callback
+ (list progress-callback))))
(defun epg-context-set-signers (context signers)
"Set the list of key-id for signing."
(goto-char (point-max))
(insert input)))
(if (buffer-live-p (process-buffer process))
- (save-excursion
- (set-buffer (process-buffer process))
+ (with-current-buffer (process-buffer process)
(goto-char (point-max))
(insert input)
(unless epg-process-filter-running
(setq epg-pending-status-list status-list)
(while (and (eq (process-status (epg-context-process context)) 'run)
epg-pending-status-list)
- (accept-process-output (epg-context-process context) 1))))
+ (accept-process-output (epg-context-process context) 1))
+ (if epg-pending-status-list
+ (epg-context-set-result-for
+ context 'error
+ (cons (list 'exit)
+ (epg-context-result-for context 'error))))))
(defun epg-wait-for-completion (context)
"Wait until the `epg-gpg-program' process completes."
(while (eq (process-status (epg-context-process context)) 'run)
- (accept-process-output (epg-context-process context) 1)))
+ (accept-process-output (epg-context-process context) 1))
+ ;; This line is needed to run the process-filter right now.
+ (sleep-for 0.1))
(defun epg-reset (context)
"Reset the CONTEXT."
(progn
(setq passphrase
(funcall
- (if (consp (epg-context-passphrase-callback context))
- (car (epg-context-passphrase-callback context))
- (epg-context-passphrase-callback context))
+ (car (epg-context-passphrase-callback context))
context
epg-key-id
- (if (consp (epg-context-passphrase-callback context))
- (cdr (epg-context-passphrase-callback context)))))
+ (cdr (epg-context-passphrase-callback context))))
(when passphrase
(setq passphrase-with-new-line (concat passphrase "\n"))
(epg--clear-string passphrase)
(if (and (epg-context-progress-callback context)
(string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
string))
- (funcall (if (consp (epg-context-progress-callback context))
- (car (epg-context-progress-callback context))
- (epg-context-progress-callback context))
+ (funcall (car (epg-context-progress-callback context))
context
(match-string 1 string)
(match-string 2 string)
(string-to-number (match-string 3 string))
(string-to-number (match-string 4 string))
- (if (consp (epg-context-progress-callback context))
- (cdr (epg-context-progress-callback context))))))
+ (cdr (epg-context-progress-callback context)))))
(defun epg--status-ENC_TO (context string)
(if (string-match "\\`\\([0-9A-Za-z]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
(defun epg--status-KEYEXPIRED (context string)
(epg-context-set-result-for
- context 'error
+ context 'key
(cons (list 'key-expired (cons 'expiration-time
(epg--time-from-seconds string)))
(epg-context-result-for context 'error))))
(defun epg--status-KEYREVOKED (context string)
(epg-context-set-result-for
- context 'error
+ context 'key
(cons '(key-revoked)
(epg-context-result-for context 'error))))
(format "Passphrase for %s: " key-id)))))))
(make-obsolete 'epg-passphrase-callback-function
- 'epa-passphrase-callback-function)
+ 'epa-passphrase-callback-function "23.1")
(defun epg--list-keys-1 (context name mode)
(let ((args (append (if epg-gpg-home-directory
(if (aref line 6)
(epg--time-from-seconds (aref line 6)))))
-;;;###autoload
(defun epg-list-keys (context &optional name mode)
"Return a list of epg-key objects matched with NAME.
If MODE is nil or 'public, only public keyring should be searched.
(epg-sig-notation-value notation)))))
notations)))
-;;;###autoload
(defun epg-cancel (context)
(if (buffer-live-p (process-buffer (epg-context-process context)))
- (save-excursion
- (set-buffer (process-buffer (epg-context-process context)))
+ (with-current-buffer (process-buffer (epg-context-process context))
(epg-context-set-result-for
epg-context 'error
(cons '(quit)
(if (eq (process-status (epg-context-process context)) 'run)
(delete-process (epg-context-process context))))
-;;;###autoload
(defun epg-start-decrypt (context cipher)
"Initiate a decrypt operation on CIPHER.
CIPHER must be a file data object.
(signal 'epg-error (list "No data")))
(signal 'epg-error (list "Can't decrypt" error)))))
-;;;###autoload
(defun epg-decrypt-file (context cipher plain)
"Decrypt a file CIPHER and store the result to a file PLAIN.
If PLAIN is nil, it returns the result as a string."
(epg-delete-output-file context))
(epg-reset context)))
-;;;###autoload
(defun epg-decrypt-string (context cipher)
"Decrypt a string CIPHER and return the plain text."
(let ((input-file (epg--make-temp-file "epg-input"))
(delete-file input-file))
(epg-reset context))))
-;;;###autoload
(defun epg-start-verify (context signature &optional signed-text)
"Initiate a verify operation on SIGNATURE.
SIGNATURE and SIGNED-TEXT are a data object if they are specified.
(process-send-eof (epg-context-process context))))
;; Normal (or cleartext) signature.
(if (epg-data-file signature)
- (epg--start context (list "--" (epg-data-file signature)))
- (epg--start context '("-"))
+ (epg--start context (if (eq (epg-context-protocol context) 'CMS)
+ (list "--verify" "--" (epg-data-file signature))
+ (list "--" (epg-data-file signature))))
+ (epg--start context (if (eq (epg-context-protocol context) 'CMS)
+ '("--verify" "-")
+ '("-")))
(if (eq (process-status (epg-context-process context)) 'run)
(process-send-string (epg-context-process context)
(epg-data-string signature)))
(if (eq (process-status (epg-context-process context)) 'run)
(process-send-eof (epg-context-process context))))))
-;;;###autoload
(defun epg-verify-file (context signature &optional signed-text plain)
"Verify a file SIGNATURE.
SIGNED-TEXT and PLAIN are also a file if they are specified.
(epg-delete-output-file context))
(epg-reset context)))
-;;;###autoload
(defun epg-verify-string (context signature &optional signed-text)
"Verify a string SIGNATURE.
SIGNED-TEXT is a string if it is specified.
(delete-file input-file))
(epg-reset context))))
-;;;###autoload
(defun epg-start-sign (context plain &optional mode)
"Initiate a sign operation on PLAIN.
PLAIN is a data object.
(if (eq (process-status (epg-context-process context)) 'run)
(process-send-eof (epg-context-process context)))))
-;;;###autoload
(defun epg-sign-file (context plain signature &optional mode)
"Sign a file PLAIN and store the result to a file SIGNATURE.
If SIGNATURE is nil, it returns the result as a string.
(epg-delete-output-file context))
(epg-reset context)))
-;;;###autoload
(defun epg-sign-string (context plain &optional mode)
"Sign a string PLAIN and return the output as string.
If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
(delete-file input-file))
(epg-reset context))))
-;;;###autoload
(defun epg-start-encrypt (context plain recipients
&optional sign always-trust)
"Initiate an encrypt operation on PLAIN.
(if (eq (process-status (epg-context-process context)) 'run)
(process-send-eof (epg-context-process context)))))
-;;;###autoload
(defun epg-encrypt-file (context plain recipients
cipher &optional sign always-trust)
"Encrypt a file PLAIN and store the result to a file CIPHER.
(epg-delete-output-file context))
(epg-reset context)))
-;;;###autoload
(defun epg-encrypt-string (context plain recipients
&optional sign always-trust)
"Encrypt a string PLAIN.
(delete-file input-file))
(epg-reset context))))
-;;;###autoload
(defun epg-start-export-keys (context keys)
"Initiate an export keys operation.
(car (epg-key-sub-key-list key))))
keys))))
-;;;###autoload
(defun epg-export-keys-to-file (context keys file)
"Extract public KEYS."
(unwind-protect
(epg-delete-output-file context))
(epg-reset context)))
-;;;###autoload
(defun epg-export-keys-to-string (context keys)
"Extract public KEYS and return them as a string."
(epg-export-keys-to-file context keys nil))
-;;;###autoload
(defun epg-start-import-keys (context keys)
"Initiate an import keys operation.
KEYS is a data object.
(epg-context-result-for context 'error))))
(epg-reset context)))
-;;;###autoload
(defun epg-import-keys-from-file (context keys)
"Add keys from a file KEYS."
(epg--import-keys-1 context (epg-make-data-from-file keys)))
-;;;###autoload
(defun epg-import-keys-from-string (context keys)
"Add keys from a string KEYS."
(epg--import-keys-1 context (epg-make-data-from-string keys)))
-;;;###autoload
(defun epg-start-receive-keys (context key-id-list)
"Initiate a receive key operation.
KEY-ID-LIST is a list of key IDs.
`epg-gpg-program' by using `epg-wait-for-completion' and call
`epg-reset' to clear a temporaly output file.
If you are unsure, use synchronous version of this function
-`epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
+`epg-receive-keys' instead."
(epg-context-set-operation context 'receive-keys)
(epg-context-set-result context nil)
(epg--start context (cons "--recv-keys" key-id-list)))
-;;;###autoload
(defun epg-receive-keys (context keys)
"Add keys from server.
KEYS is a list of key IDs"
(epg-context-result-for context 'error))))
(epg-reset context)))
-;;;###autoload
(defalias 'epg-import-keys-from-server 'epg-receive-keys)
-;;;###autoload
(defun epg-start-delete-keys (context keys &optional allow-secret)
"Initiate a delete keys operation.
(car (epg-key-sub-key-list key))))
keys))))
-;;;###autoload
(defun epg-delete-keys (context keys &optional allow-secret)
"Delete KEYS from the key ring."
(unwind-protect
(error "Delete keys failed")))))
(epg-reset context)))
-;;;###autoload
(defun epg-start-sign-keys (context keys &optional local)
"Initiate a sign keys operation.
(epg-sub-key-id
(car (epg-key-sub-key-list key))))
keys))))
-(make-obsolete 'epg-start-sign-keys "do not use.")
+(make-obsolete 'epg-start-sign-keys "do not use." "23.1")
-;;;###autoload
(defun epg-sign-keys (context keys &optional local)
"Sign KEYS from the key ring."
(unwind-protect
(error "Sign keys failed: %S"
(epg-context-result-for context 'error))))
(epg-reset context)))
-(make-obsolete 'epg-sign-keys "do not use.")
+(make-obsolete 'epg-sign-keys "do not use." "23.1")
-;;;###autoload
(defun epg-start-generate-key (context parameters)
"Initiate a key generation.
PARAMETERS specifies parameters for the key.
(if (eq (process-status (epg-context-process context)) 'run)
(process-send-eof (epg-context-process context)))))
-;;;###autoload
(defun epg-generate-key-from-file (context parameters)
"Generate a new key pair.
PARAMETERS is a file which tells how to create the key."
(epg-context-result-for context 'error))))
(epg-reset context)))
-;;;###autoload
(defun epg-generate-key-from-string (context parameters)
"Generate a new key pair.
PARAMETERS is a string which tells how to create the key."