-;;; epg.el --- the EasyPG Library
-;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
-;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
+;;; epg.el --- the EasyPG Library -*- lexical-binding: t -*-
+;; Copyright (C) 1999-2000, 2002-2012 Free Software Foundation, Inc.
;; Author: Daiki Ueno <ueno@unixuser.org>
;; Keywords: PGP, GnuPG
+;; Version: 1.0.0
;; This file is part of GNU Emacs.
(defconst epg-invalid-recipients-reason-alist
'((0 . "No specific reason given")
(1 . "Not Found")
- (2 . "Ambigious specification")
+ (2 . "Ambiguous specification")
(3 . "Wrong key usage")
(4 . "Key revoked")
(5 . "Key expired")
(defconst epg-delete-problem-reason-alist
'((1 . "No such key")
(2 . "Must delete secret key first")
- (3 . "Ambigious specification")))
+ (3 . "Ambiguous specification")))
(defconst epg-import-ok-reason-alist
'((0 . "Not actually changed")
'((?e . encrypt)
(?s . sign)
(?c . certify)
- (?a . authentication)))
+ (?a . authentication)
+ (?D . disabled)))
(defvar epg-new-signature-type-alist
'((?D . detached)
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
+The function gets six 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)."
(format " secret keys unchanged: %d\n"
(epg-import-result-secret-unchanged import-result)))))
+(defun epg-error-to-string (error)
+ (cond
+ ((eq (car error) 'exit)
+ "Exit")
+ ((eq (car error) 'quit)
+ "Cancelled")
+ ((eq (car error) 'no-data)
+ (let ((entry (assq (cdr error) epg-no-data-reason-alist)))
+ (if entry
+ (format "No data (%s)" (downcase (cdr entry)))
+ "No data")))
+ ((eq (car error) 'unexpected)
+ (let ((entry (assq (cdr error) epg-unexpected-reason-alist)))
+ (if entry
+ (format "Unexpected (%s)" (downcase (cdr entry)))
+ "Unexpected")))
+ ((eq (car error) 'bad-armor)
+ "Bad armor")
+ ((memq (car error) '(invalid-recipient invalid-signer))
+ (concat
+ (if (eq (car error) 'invalid-recipient)
+ "Unusable public key"
+ "Unusable secret key")
+ (let ((entry (assq 'requested (cdr error))))
+ (if entry
+ (format ": %s" (cdr entry))
+ ": <unknown>"))
+ (let ((entry (assq 'reason (cdr error))))
+ (if (and entry
+ (> (cdr entry) 0) ;no specific reason given
+ (setq entry (assq (cdr entry)
+ epg-invalid-recipients-reason-alist)))
+ (format " (%s)" (downcase (cdr entry)))
+ ""))))
+ ((eq (car error) 'no-pubkey)
+ (format "No public key: %s" (cdr error)))
+ ((eq (car error) 'no-seckey)
+ (format "No secret key: %s" (cdr error)))
+ ((eq (car error) 'no-recipients)
+ "No recipients")
+ ((eq (car error) 'no-signers)
+ "No signers")
+ ((eq (car error) 'delete-problem)
+ (let ((entry (assq (cdr error) epg-delete-problem-reason-alist)))
+ (if entry
+ (format "Delete problem (%s)" (downcase (cdr entry)))
+ "Delete problem")))
+ ((eq (car error) 'key-not-created)
+ "Key not created")))
+
+(defun epg-errors-to-string (errors)
+ (mapconcat #'epg-error-to-string errors "; "))
+
(defun epg--start (context args)
"Start `epg-gpg-program' in a subprocess with given ARGS."
(if (and (epg-context-process context)
(if epg-pending-status-list
(epg-context-set-result-for
context 'error
- (cons (list 'exit)
+ (cons '(exit)
(epg-context-result-for context 'error))))))
(defun epg-wait-for-completion (context)
(while (eq (process-status (epg-context-process context)) 'run)
(accept-process-output (epg-context-process context) 1))
;; This line is needed to run the process-filter right now.
- (sleep-for 0.1))
+ (sleep-for 0.1)
+ (epg-context-set-result-for
+ context 'error
+ (nreverse (epg-context-result-for context 'error))))
(defun epg-reset (context)
"Reset the CONTEXT."
(defalias 'epg--decode-coding-string 'decode-coding-string)
(defalias 'epg--decode-coding-string 'identity)))
-(defun epg--status-USERID_HINT (context string)
+(defun epg--status-USERID_HINT (_context string)
(if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
(let* ((key-id (match-string 1 string))
(user-id (match-string 2 string))
(setq epg-user-id-alist (cons (cons key-id user-id)
epg-user-id-alist))))))
-(defun epg--status-NEED_PASSPHRASE (context string)
+(defun epg--status-NEED_PASSPHRASE (_context string)
(if (string-match "\\`\\([^ ]+\\)" string)
(setq epg-key-id (match-string 1 string))))
-(defun epg--status-NEED_PASSPHRASE_SYM (context string)
+(defun epg--status-NEED_PASSPHRASE_SYM (_context _string)
(setq epg-key-id 'SYM))
-(defun epg--status-NEED_PASSPHRASE_PIN (context string)
+(defun epg--status-NEED_PASSPHRASE_PIN (_context _string)
(setq epg-key-id 'PIN))
(eval-and-compile
(if encoded-passphrase-with-new-line
(epg--clear-string encoded-passphrase-with-new-line))))))
-(defun epg--prompt-GET_BOOL (context string)
+(defun epg--prompt-GET_BOOL (_context string)
(let ((entry (assoc string epg-prompt-alist)))
(y-or-n-p (if entry (cdr entry) (concat string "? ")))))
-(defun epg--prompt-GET_BOOL-untrusted_key.override (context string)
+(defun epg--prompt-GET_BOOL-untrusted_key.override (_context _string)
(y-or-n-p (if (and (equal (car epg-last-status) "USERID_HINT")
(string-match "\\`\\([^ ]+\\) \\(.*\\)"
(cdr epg-last-status)))
(epg--status-*SIG context 'bad string))
(defun epg--status-NO_PUBKEY (context string)
- (let ((signature (car (epg-context-result-for context 'verify))))
- (if (and signature
- (eq (epg-signature-status signature) 'error)
- (equal (epg-signature-key-id signature) string))
- (epg-signature-set-status signature 'no-pubkey))))
+ (if (eq (epg-context-operation context) 'verify)
+ (let ((signature (car (epg-context-result-for context 'verify))))
+ (if (and signature
+ (eq (epg-signature-status signature) 'error)
+ (equal (epg-signature-key-id signature) string))
+ (epg-signature-set-status signature 'no-pubkey)))
+ (epg-context-set-result-for
+ context 'error
+ (cons (cons 'no-pubkey string)
+ (epg-context-result-for context 'error)))))
+
+(defun epg--status-NO_SECKEY (context string)
+ (epg-context-set-result-for
+ context 'error
+ (cons (cons 'no-seckey string)
+ (epg-context-result-for context 'error))))
(defun epg--time-from-seconds (seconds)
(let ((number-seconds (string-to-number (concat seconds ".0"))))
signature
(string-to-number (match-string 7 string) 16)))))
-(defun epg--status-TRUST_UNDEFINED (context string)
+(defun epg--status-TRUST_UNDEFINED (context _string)
(let ((signature (car (epg-context-result-for context 'verify))))
(if (and signature
(eq (epg-signature-status signature) 'good))
(epg-signature-set-validity signature 'undefined))))
-(defun epg--status-TRUST_NEVER (context string)
+(defun epg--status-TRUST_NEVER (context _string)
(let ((signature (car (epg-context-result-for context 'verify))))
(if (and signature
(eq (epg-signature-status signature) 'good))
(epg-signature-set-validity signature 'never))))
-(defun epg--status-TRUST_MARGINAL (context string)
+(defun epg--status-TRUST_MARGINAL (context _string)
(let ((signature (car (epg-context-result-for context 'verify))))
(if (and signature
(eq (epg-signature-status signature) 'marginal))
(epg-signature-set-validity signature 'marginal))))
-(defun epg--status-TRUST_FULLY (context string)
+(defun epg--status-TRUST_FULLY (context _string)
(let ((signature (car (epg-context-result-for context 'verify))))
(if (and signature
(eq (epg-signature-status signature) 'good))
(epg-signature-set-validity signature 'full))))
-(defun epg--status-TRUST_ULTIMATE (context string)
+(defun epg--status-TRUST_ULTIMATE (context _string)
(let ((signature (car (epg-context-result-for context 'verify))))
(if (and signature
(eq (epg-signature-status signature) 'good))
(string-to-number (match-string 3 string)))
(epg-context-result-for context 'encrypted-to)))))
-(defun epg--status-DECRYPTION_FAILED (context string)
+(defun epg--status-DECRYPTION_FAILED (context _string)
(epg-context-set-result-for context 'decryption-failed t))
-(defun epg--status-DECRYPTION_OKAY (context string)
+(defun epg--status-DECRYPTION_OKAY (context _string)
(epg-context-set-result-for context 'decryption-okay t))
(defun epg--status-NODATA (context string)
context 'key
(cons (list 'key-expired (cons 'expiration-time
(epg--time-from-seconds string)))
- (epg-context-result-for context 'error))))
+ (epg-context-result-for context 'key))))
-(defun epg--status-KEYREVOKED (context string)
+(defun epg--status-KEYREVOKED (context _string)
(epg-context-set-result-for
context 'key
(cons '(key-revoked)
- (epg-context-result-for context 'error))))
+ (epg-context-result-for context 'key))))
-(defun epg--status-BADARMOR (context string)
+(defun epg--status-BADARMOR (context _string)
(epg-context-set-result-for
context 'error
(cons '(bad-armor)
(cons (list 'invalid-recipient
(cons 'reason
(string-to-number (match-string 1 string)))
- (cons 'requested-recipient
+ (cons 'requested
+ (match-string 2 string)))
+ (epg-context-result-for context 'error)))))
+
+(defun epg--status-INV_SGNR (context string)
+ (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
+ (epg-context-set-result-for
+ context 'error
+ (cons (list 'invalid-signer
+ (cons 'reason
+ (string-to-number (match-string 1 string)))
+ (cons 'requested
(match-string 2 string)))
(epg-context-result-for context 'error)))))
-(defun epg--status-NO_RECP (context string)
+(defun epg--status-NO_RECP (context _string)
(epg-context-set-result-for
context 'error
(cons '(no-recipients)
(epg-context-result-for context 'error))))
+(defun epg--status-NO_SGNR (context _string)
+ (epg-context-set-result-for
+ context 'error
+ (cons '(no-signers)
+ (epg-context-result-for context 'error))))
+
(defun epg--status-DELETE_PROBLEM (context string)
(if (string-match "\\`\\([0-9]+\\)" string)
(epg-context-set-result-for
(cons 'fingerprint (match-string 2 string)))
(epg-context-result-for context 'generate-key)))))
-(defun epg--status-KEY_NOT_CREATED (context string)
+(defun epg--status-KEY_NOT_CREATED (context _string)
(epg-context-set-result-for
context 'error
(cons '(key-not-created)
(epg-context-result-for context 'error))))
-(defun epg--status-IMPORTED (context string)
+(defun epg--status-IMPORTED (_context string)
(if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
(let* ((key-id (match-string 1 string))
(user-id (match-string 2 string))
(epg-context-result-for context 'import-status)))
(epg-context-set-result-for context 'import-status nil)))
-(defun epg-passphrase-callback-function (context key-id handback)
+(defun epg-passphrase-callback-function (context key-id _handback)
(if (eq key-id 'SYM)
(read-passwd "Passphrase for symmetric encryption: "
(eq (epg-context-operation context) 'encrypt))
If you use this function, you will need to wait for the completion of
`epg-gpg-program' by using `epg-wait-for-completion' and call
-`epg-reset' to clear a temporaly output file.
+`epg-reset' to clear a temporary output file.
If you are unsure, use synchronous version of this function
`epg-decrypt-file' or `epg-decrypt-string' instead."
(unless (epg-data-file cipher)
(epg-wait-for-status context '("BEGIN_DECRYPTION"))))
(defun epg--check-error-for-decrypt (context)
- (if (epg-context-result-for context 'decryption-failed)
- (signal 'epg-error (list "Decryption failed")))
- (if (epg-context-result-for context 'no-secret-key)
- (signal 'epg-error
- (list "No secret key"
- (epg-context-result-for context 'no-secret-key))))
+ (let ((errors (epg-context-result-for context 'error)))
+ (if (epg-context-result-for context 'decryption-failed)
+ (signal 'epg-error
+ (list "Decryption failed" (epg-errors-to-string errors))))
(unless (epg-context-result-for context 'decryption-okay)
- (let* ((error (epg-context-result-for context 'error)))
- (if (assq 'no-data error)
- (signal 'epg-error (list "No data")))
- (signal 'epg-error (list "Can't decrypt" error)))))
+ (signal 'epg-error
+ (list "Can't decrypt" (epg-errors-to-string errors))))))
(defun epg-decrypt-file (context cipher plain)
"Decrypt a file CIPHER and store the result to a file PLAIN.
If you use this function, you will need to wait for the completion of
`epg-gpg-program' by using `epg-wait-for-completion' and call
-`epg-reset' to clear a temporaly output file.
+`epg-reset' to clear a temporary output file.
If you are unsure, use synchronous version of this function
`epg-verify-file' or `epg-verify-string' instead."
(epg-context-set-operation context 'verify)
If you use this function, you will need to wait for the completion of
`epg-gpg-program' by using `epg-wait-for-completion' and call
-`epg-reset' to clear a temporaly output file.
+`epg-reset' to clear a temporary output file.
If you are unsure, use synchronous version of this function
`epg-sign-file' or `epg-sign-string' instead."
(epg-context-set-operation context 'sign)
(epg-start-sign context (epg-make-data-from-file plain) mode)
(epg-wait-for-completion context)
(unless (epg-context-result-for context 'sign)
- (if (epg-context-result-for context 'error)
- (error "Sign failed: %S"
- (epg-context-result-for context 'error))
- (error "Sign failed")))
+ (let ((errors (epg-context-result-for context 'error)))
+ (signal 'epg-error
+ (list "Sign failed" (epg-errors-to-string errors)))))
(unless signature
(epg-read-output context)))
(unless signature
(epg-wait-for-completion context)
(unless (epg-context-result-for context 'sign)
(if (epg-context-result-for context 'error)
- (error "Sign failed: %S"
- (epg-context-result-for context 'error))
- (error "Sign failed")))
+ (let ((errors (epg-context-result-for context 'error)))
+ (signal 'epg-error
+ (list "Sign failed" (epg-errors-to-string errors))))))
(epg-read-output context))
(epg-delete-output-file context)
(if input-file
If you use this function, you will need to wait for the completion of
`epg-gpg-program' by using `epg-wait-for-completion' and call
-`epg-reset' to clear a temporaly output file.
+`epg-reset' to clear a temporary output file.
If you are unsure, use synchronous version of this function
`epg-encrypt-file' or `epg-encrypt-string' instead."
(epg-context-set-operation context 'encrypt)
(epg-start-encrypt context (epg-make-data-from-file plain)
recipients sign always-trust)
(epg-wait-for-completion context)
- (if (and sign
- (not (epg-context-result-for context 'sign)))
- (if (epg-context-result-for context 'error)
- (error "Sign failed: %S"
- (epg-context-result-for context 'error))
- (error "Sign failed")))
- (if (epg-context-result-for context 'error)
- (error "Encrypt failed: %S"
- (epg-context-result-for context 'error)))
+ (let ((errors (epg-context-result-for context 'error)))
+ (if (and sign
+ (not (epg-context-result-for context 'sign)))
+ (signal 'epg-error
+ (list "Sign failed" (epg-errors-to-string errors))))
+ (if errors
+ (signal 'epg-error
+ (list "Encrypt failed" (epg-errors-to-string errors)))))
(unless cipher
(epg-read-output context)))
(unless cipher
(epg-make-data-from-string plain))
recipients sign always-trust)
(epg-wait-for-completion context)
- (if (and sign
- (not (epg-context-result-for context 'sign)))
- (if (epg-context-result-for context 'error)
- (error "Sign failed: %S"
- (epg-context-result-for context 'error))
- (error "Sign failed")))
- (if (epg-context-result-for context 'error)
- (error "Encrypt failed: %S"
- (epg-context-result-for context 'error)))
+ (let ((errors (epg-context-result-for context 'error)))
+ (if (and sign
+ (not (epg-context-result-for context 'sign)))
+ (signal 'epg-error
+ (list "Sign failed" (epg-errors-to-string errors))))
+ (if errors
+ (signal 'epg-error
+ (list "Encrypt failed" (epg-errors-to-string errors)))))
(epg-read-output context))
(epg-delete-output-file context)
(if input-file
If you use this function, you will need to wait for the completion of
`epg-gpg-program' by using `epg-wait-for-completion' and call
-`epg-reset' to clear a temporaly output file.
+`epg-reset' to clear a temporary output file.
If you are unsure, use synchronous version of this function
`epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
(epg-context-set-operation context 'export-keys)
(epg--make-temp-file "epg-output")))
(epg-start-export-keys context keys)
(epg-wait-for-completion context)
- (if (epg-context-result-for context 'error)
- (error "Export keys failed: %S"
- (epg-context-result-for context 'error)))
+ (let ((errors (epg-context-result-for context 'error)))
+ (if errors
+ (signal 'epg-error
+ (list "Export keys failed"
+ (epg-errors-to-string errors)))))
(unless file
(epg-read-output context)))
(unless file
If you use this function, you will need to wait for the completion of
`epg-gpg-program' by using `epg-wait-for-completion' and call
-`epg-reset' to clear a temporaly output file.
+`epg-reset' to clear a temporary output file.
If you are unsure, use synchronous version of this function
`epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
(epg-context-set-operation context 'import-keys)
(progn
(epg-start-import-keys context keys)
(epg-wait-for-completion context)
- (if (epg-context-result-for context 'error)
- (error "Import keys failed: %S"
- (epg-context-result-for context 'error))))
+ (let ((errors (epg-context-result-for context 'error)))
+ (if errors
+ (signal 'epg-error
+ (list "Import keys failed"
+ (epg-errors-to-string errors))))))
(epg-reset context)))
(defun epg-import-keys-from-file (context keys)
If you use this function, you will need to wait for the completion of
`epg-gpg-program' by using `epg-wait-for-completion' and call
-`epg-reset' to clear a temporaly output file.
+`epg-reset' to clear a temporary output file.
If you are unsure, use synchronous version of this function
`epg-receive-keys' instead."
(epg-context-set-operation context 'receive-keys)
(progn
(epg-start-receive-keys context keys)
(epg-wait-for-completion context)
- (if (epg-context-result-for context 'error)
- (error "Receive keys failed: %S"
- (epg-context-result-for context 'error))))
+ (let ((errors (epg-context-result-for context 'error)))
+ (if errors
+ (signal 'epg-error
+ (list "Receive keys failed"
+ (epg-errors-to-string errors))))))
(epg-reset context)))
(defalias 'epg-import-keys-from-server 'epg-receive-keys)
If you use this function, you will need to wait for the completion of
`epg-gpg-program' by using `epg-wait-for-completion' and call
-`epg-reset' to clear a temporaly output file.
+`epg-reset' to clear a temporary output file.
If you are unsure, use synchronous version of this function
`epg-delete-keys' instead."
(epg-context-set-operation context 'delete-keys)
(progn
(epg-start-delete-keys context keys allow-secret)
(epg-wait-for-completion context)
- (let ((entry (assq 'delete-problem
- (epg-context-result-for context 'error))))
- (if entry
- (if (setq entry (assq (cdr entry)
- epg-delete-problem-reason-alist))
- (error "Delete keys failed: %s" (cdr entry))
- (error "Delete keys failed")))))
+ (let ((errors (epg-context-result-for context 'error)))
+ (if errors
+ (signal 'epg-error
+ (list "Delete keys failed"
+ (epg-errors-to-string errors))))))
(epg-reset context)))
(defun epg-start-sign-keys (context keys &optional local)
If you use this function, you will need to wait for the completion of
`epg-gpg-program' by using `epg-wait-for-completion' and call
-`epg-reset' to clear a temporaly output file.
+`epg-reset' to clear a temporary output file.
If you are unsure, use synchronous version of this function
`epg-sign-keys' instead."
(epg-context-set-operation context 'sign-keys)
(progn
(epg-start-sign-keys context keys local)
(epg-wait-for-completion context)
- (if (epg-context-result-for context 'error)
- (error "Sign keys failed: %S"
- (epg-context-result-for context 'error))))
+ (let ((errors (epg-context-result-for context 'error)))
+ (if errors
+ (signal 'epg-error
+ (list "Sign keys failed"
+ (epg-errors-to-string errors))))))
(epg-reset context)))
(make-obsolete 'epg-sign-keys "do not use." "23.1")
If you use this function, you will need to wait for the completion of
`epg-gpg-program' by using `epg-wait-for-completion' and call
-`epg-reset' to clear a temporaly output file.
+`epg-reset' to clear a temporary 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-context-set-operation context 'generate-key)
(progn
(epg-start-generate-key context (epg-make-data-from-file parameters))
(epg-wait-for-completion context)
- (if (epg-context-result-for context 'error)
- (error "Generate key failed: %S"
- (epg-context-result-for context 'error))))
+ (let ((errors (epg-context-result-for context 'error)))
+ (if errors
+ (signal 'epg-error
+ (list "Generate key failed"
+ (epg-errors-to-string errors))))))
(epg-reset context)))
(defun epg-generate-key-from-string (context parameters)
(progn
(epg-start-generate-key context (epg-make-data-from-string parameters))
(epg-wait-for-completion context)
- (if (epg-context-result-for context 'error)
- (error "Generate key failed: %S"
- (epg-context-result-for context 'error))))
+ (let ((errors (epg-context-result-for context 'error)))
+ (if errors
+ (signal 'epg-error
+ (list "Generate key failed"
+ (epg-errors-to-string errors))))))
(epg-reset context)))
(defun epg--decode-percent-escape (string)
(provide 'epg)
-;; arch-tag: de8f0acc-1bcf-4c14-a09e-bfffe1b579b7
;;; epg.el ends here