X-Git-Url: http://git.chise.org/gitweb/?a=blobdiff_plain;f=epg.el;h=b32d57a64205ed5f928fe3bae83847df7db73db2;hb=7dd310a5477089fc298a838e5676fbed2892e282;hp=5245a57adcd0fc043959b4fe09c543383680c8b9;hpb=98fff073633e867d113023a64d217890c0f63922;p=elisp%2Fepg.git diff --git a/epg.el b/epg.el index 5245a57..b32d57a 100644 --- a/epg.el +++ b/epg.el @@ -33,6 +33,11 @@ :group 'epg :type 'string) +(defcustom epg-gpgsm-program "gpgsm" + "The `gpgsm' executable." + :group 'epg + :type 'string) + (defconst epg-version-number "0.0.0") (defvar epg-user-id nil @@ -123,6 +128,19 @@ (?c . certify) (?a . authentication))) +(defvar epg-dn-type-alist + '(("1.2.840.113549.1.9.1" . "EMail") + ("2.5.4.12" . "T") + ("2.5.4.42" . "GN") + ("2.5.4.4" . "SN") + ("0.2.262.1.10.7.20" . "NameDistinguisher") + ("2.5.4.16" . "ADDR") + ("2.5.4.15" . "BC") + ("2.5.4.13" . "D") + ("2.5.4.17" . "PostalCode") + ("2.5.4.65" . "Pseudo") + ("2.5.4.5" . "SerialNumber"))) + (defvar epg-prompt-alist nil) (defun epg-make-data-from-file (file) @@ -423,8 +441,9 @@ This function is for internal use only." "Start `epg-gpg-program' in a subprocess with given ARGS." (let* ((args (append (list "--no-tty" "--status-fd" "1" - "--command-fd" "0" "--yes") + (unless (eq (epg-context-protocol context) 'CMS) + (list "--command-fd" "0")) (if (epg-context-armor context) '("--armor")) (if (epg-context-textmode context) '("--textmode")) (if (epg-context-output-file context) @@ -441,7 +460,10 @@ This function is for internal use only." (setq epg-debug-buffer (generate-new-buffer " *epg-debug*"))) (set-buffer epg-debug-buffer) (goto-char (point-max)) - (insert (format "%s %s\n" epg-gpg-program + (insert (format "%s %s\n" + (if (eq (epg-context-protocol context) 'CMS) + epg-gpgsm-program + epg-gpg-program) (mapconcat #'identity args " "))))) (with-current-buffer buffer (make-local-variable 'epg-read-point) @@ -456,9 +478,14 @@ This function is for internal use only." (progn (set-default-file-modes 448) (setq process - (apply #'start-process "epg" buffer epg-gpg-program args))) + (apply #'start-process "epg" buffer + (if (eq (epg-context-protocol context) 'CMS) + epg-gpgsm-program + epg-gpg-program) + args))) (set-default-file-modes orig-mode)) (set-process-filter process #'epg-process-filter) + (set-process-sentinel process #'epg-process-sentinel) (epg-context-set-process context process))) (defun epg-process-filter (process input) @@ -490,6 +517,21 @@ This function is for internal use only." (forward-line)) (setq epg-read-point (point))))) +(defun epg-process-sentinel (process status) + (if (and (buffer-live-p (process-buffer process)) + (not (equal status "finished\n"))) + (save-excursion + (set-buffer (process-buffer process)) + ;; gpg process exited abnormally, but we have not received an + ;; error response from it. Set it here. + (unless (epg-context-result-for epg-context 'error) + (if (string-match "\\`exited abnormally with code \\(.*\\)\n" status) + (epg-context-set-result-for + epg-context 'error + (list (cons 'exit (string-to-number (match-string 1 status))))) + (epg-context-set-result-for epg-context 'error + (list (cons 'signal status)))))))) + (defun epg-read-output (context) (with-temp-buffer (if (fboundp 'set-buffer-multibyte) @@ -620,9 +662,14 @@ This function is for internal use only." (epg-context-set-result-for epg-context 'verify - (cons (epg-make-signature 'good - (match-string 1 string) - (match-string 2 string)) + (cons (epg-make-signature + 'good + (match-string 1 string) + (if (eq (epg-context-protocol epg-context) 'CMS) + (condition-case nil + (epg-dn-from-string (match-string 2 string)) + (error (match-string 2 string))) + (match-string 2 string))) (epg-context-result-for epg-context 'verify))))) (defun epg-status-EXPSIG (process string) @@ -630,9 +677,14 @@ This function is for internal use only." (epg-context-set-result-for epg-context 'verify - (cons (epg-make-signature 'expired - (match-string 1 string) - (match-string 2 string)) + (cons (epg-make-signature + 'expired + (match-string 1 string) + (if (eq (epg-context-protocol epg-context) 'CMS) + (condition-case nil + (epg-dn-from-string (match-string 2 string)) + (error (match-string 2 string))) + (match-string 2 string))) (epg-context-result-for epg-context 'verify))))) (defun epg-status-EXPKEYSIG (process string) @@ -640,9 +692,14 @@ This function is for internal use only." (epg-context-set-result-for epg-context 'verify - (cons (epg-make-signature 'expired-key - (match-string 1 string) - (match-string 2 string)) + (cons (epg-make-signature + 'expired-key + (match-string 1 string) + (if (eq (epg-context-protocol epg-context) 'CMS) + (condition-case nil + (epg-dn-from-string (match-string 2 string)) + (error (match-string 2 string))) + (match-string 2 string))) (epg-context-result-for epg-context 'verify))))) (defun epg-status-REVKEYSIG (process string) @@ -650,9 +707,14 @@ This function is for internal use only." (epg-context-set-result-for epg-context 'verify - (cons (epg-make-signature 'revoked-key - (match-string 1 string) - (match-string 2 string)) + (cons (epg-make-signature + 'revoked-key + (match-string 1 string) + (if (eq (epg-context-protocol epg-context) 'CMS) + (condition-case nil + (epg-dn-from-string (match-string 2 string)) + (error (match-string 2 string))) + (match-string 2 string))) (epg-context-result-for epg-context 'verify))))) (defun epg-status-BADSIG (process string) @@ -660,9 +722,14 @@ This function is for internal use only." (epg-context-set-result-for epg-context 'verify - (cons (epg-make-signature 'bad - (match-string 1 string) - (match-string 2 string)) + (cons (epg-make-signature + 'bad + (match-string 1 string) + (if (eq (epg-context-protocol epg-context) 'CMS) + (condition-case nil + (epg-dn-from-string (match-string 2 string)) + (error (match-string 2 string))) + (match-string 2 string))) (epg-context-result-for epg-context 'verify))))) (defun epg-status-VALIDSIG (process string) @@ -822,15 +889,21 @@ This function is for internal use only." config)))) config)) -(defun epg-list-keys-1 (name mode) +(defun epg-list-keys-1 (context name mode) (let ((args (append (list "--with-colons" "--no-greeting" "--batch" - "--fixed-list-mode" "--with-fingerprint" + "--with-fingerprint" "--with-fingerprint" (if mode "--list-secret-keys" "--list-keys")) + (unless (eq (epg-context-protocol context) 'CMS) + '("--fixed-list-mode")) (if name (list name)))) keys string field index) (with-temp-buffer - (apply #'call-process epg-gpg-program nil (list t nil) nil args) + (apply #'call-process + (if (eq (epg-context-protocol context) 'CMS) + epg-gpgsm-program + epg-gpg-program) + nil (list t nil) nil args) (goto-char (point-min)) (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t) (setq keys (cons (make-vector 15 nil) keys) @@ -858,12 +931,12 @@ This function is for internal use only." (aref line 5) (aref line 6))) -(defun epg-list-keys (&optional name mode) - (let ((lines (epg-list-keys-1 name mode)) - keys) +(defun epg-list-keys (context &optional name mode) + (let ((lines (epg-list-keys-1 context name mode)) + keys cert) (while lines (cond - ((member (aref (car lines) 0) '("pub" "sec")) + ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs")) (when (car keys) (epg-key-set-sub-key-list (car keys) @@ -871,7 +944,8 @@ This function is for internal use only." (epg-key-set-user-id-list (car keys) (nreverse (epg-key-user-id-list (car keys))))) - (setq keys (cons (epg-make-key + (setq cert (member (aref (car lines) 0) '("crt" "crs")) + keys (cons (epg-make-key (if (aref (car lines) 8) (cdr (assq (string-to-char (aref (car lines) 8)) epg-key-validity-alist)))) @@ -892,12 +966,23 @@ This function is for internal use only." (if (aref (car lines) 1) (cdr (assq (string-to-char (aref (car lines) 1)) epg-key-validity-alist))) - (aref (car lines) 9)) + (if cert + (condition-case nil + (epg-dn-from-string (aref (car lines) 9)) + (error (aref (car lines) 9))) + (aref (car lines) 9))) (epg-key-user-id-list (car keys))))) ((equal (aref (car lines) 0) "fpr") (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys))) (aref (car lines) 9)))) (setq lines (cdr lines))) + (when (car keys) + (epg-key-set-sub-key-list + (car keys) + (nreverse (epg-key-sub-key-list (car keys)))) + (epg-key-set-user-id-list + (car keys) + (nreverse (epg-key-user-id-list (car keys))))) (nreverse keys))) (if (fboundp 'make-temp-file) @@ -963,7 +1048,9 @@ If you are unsure, use synchronous version of this function (error "Not a file")) (epg-context-set-result context nil) (epg-start context (list "--decrypt" (epg-data-file cipher))) - (epg-wait-for-status context '("BEGIN_DECRYPTION"))) + ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed. + (unless (eq (epg-context-protocol context) 'CMS) + (epg-wait-for-status context '("BEGIN_DECRYPTION")))) ;;;###autoload (defun epg-decrypt-file (context cipher plain) @@ -993,7 +1080,7 @@ If PLAIN is nil, it returns the result as a string." (coding-system-for-write 'binary)) (unwind-protect (progn - (write-region cipher nil input-file) + (write-region cipher nil input-file nil 'quiet) (epg-context-set-output-file context (epg-make-temp-file "epg-output")) (epg-start-decrypt context (epg-make-data-from-file input-file)) @@ -1081,7 +1168,7 @@ For a normal or a clear text signature, SIGNED-TEXT should be nil." (if signed-text (progn (setq input-file (epg-make-temp-file "epg-signature")) - (write-region signature nil input-file) + (write-region signature nil input-file nil 'quiet) (epg-start-verify context (epg-make-data-from-file input-file) (epg-make-data-from-string signed-text))) @@ -1125,7 +1212,9 @@ If you are unsure, use synchronous version of this function (epg-context-signers context))) (if (epg-data-file plain) (list (epg-data-file plain))))) - (epg-wait-for-status context '("BEGIN_SIGNING")) + ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed. + (unless (eq (epg-context-protocol context) 'CMS) + (epg-wait-for-status context '("BEGIN_SIGNING"))) (if (and (epg-data-string plain) (eq (process-status (epg-context-process context)) 'run)) (process-send-string (epg-context-process context) @@ -1146,9 +1235,9 @@ Otherwise, it makes a normal signature." (epg-make-temp-file "epg-output"))) (epg-start-sign context (epg-make-data-from-file plain) mode) (epg-wait-for-completion context) - (if (epg-context-result-for context 'error) - (error "Sign failed: %S" - (epg-context-result-for context 'error))) + (unless (epg-context-result-for context 'sign) + (error "Sign failed: %S" + (epg-context-result-for context 'error))) (unless signature (epg-read-output context))) (unless signature @@ -1168,9 +1257,9 @@ Otherwise, it makes a normal signature." (epg-start-sign context (epg-make-data-from-string plain) mode) (epg-flush context) (epg-wait-for-completion context) - (if (epg-context-result-for context 'error) - (error "Sign failed: %S" - (epg-context-result-for context 'error))) + (unless (epg-context-result-for context 'sign) + (error "Sign failed: %S" + (epg-context-result-for context 'error))) (epg-read-output context)) (epg-delete-output-file context) (epg-reset context))) @@ -1206,9 +1295,11 @@ If you are unsure, use synchronous version of this function recipients)) (if (epg-data-file plain) (list (epg-data-file plain))))) - (if sign - (epg-wait-for-status context '("BEGIN_SIGNING")) - (epg-wait-for-status context '("BEGIN_ENCRYPTION"))) + ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed. + (unless (eq (epg-context-protocol context) 'CMS) + (if sign + (epg-wait-for-status context '("BEGIN_SIGNING")) + (epg-wait-for-status context '("BEGIN_ENCRYPTION")))) (if (and (epg-data-string plain) (eq (process-status (epg-context-process context)) 'run)) (process-send-string (epg-context-process context) @@ -1229,6 +1320,10 @@ If RECIPIENTS is nil, it performs symmetric encryption." (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))) + (error "Sign encrypt failed: %S" + (epg-context-result-for context 'error))) (if (epg-context-result-for context 'error) (error "Encrypt failed: %S" (epg-context-result-for context 'error))) @@ -1251,6 +1346,10 @@ If RECIPIENTS is nil, it performs symmetric encryption." recipients sign always-trust) (epg-flush context) (epg-wait-for-completion context) + (if (and sign + (not (epg-context-result-for context 'sign))) + (error "Sign encrypt failed: %S" + (epg-context-result-for context 'error))) (if (epg-context-result-for context 'error) (error "Encrypt failed: %S" (epg-context-result-for context 'error))) @@ -1403,6 +1502,88 @@ If you are unsure, use synchronous version of this function (epg-context-result-for context 'error)))) (epg-reset context))) +(defun epg-decode-hexstring (string) + (let ((index 0)) + (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index)) + (setq string (replace-match "\\x\\&" t nil string) + index (+ index 4))) + (car (read-from-string (concat "\"" string "\""))))) + +(defun epg-decode-quotedstring (string) + (let ((index 0)) + (while (string-match "\\\\\\(\\([,=+<>#;\\\"]\\)\\|\ +\\([0-9A-Fa-f][0-9A-Fa-f]\\)\\|\\(.\\)\\)" + string index) + (if (match-beginning 2) + (setq string (replace-match "\\2" t nil string) + index (1+ index)) + (if (match-beginning 3) + (setq string (replace-match "\\x\\3" t nil string) + index (+ index 4)) + (setq string (replace-match "\\\\\\\\\\4" t nil string) + index (+ index 3))))) + (car (read-from-string (concat "\"" string "\""))))) + +(defun epg-dn-from-string (string) + "Parse STRING as LADPv3 Distinguished Names (RFC2253). +The return value is an alist mapping from types to values." + (let ((index 0) + (length (length string)) + alist type value group) + (while (< index length) + (if (eq index (string-match "[ \t\n\r]*" string index)) + (setq index (match-end 0))) + (if (eq index (string-match + "\\([0-9]+\\(\\.[0-9]+\\)*\\)\[ \t\n\r]*=[ \t\n\r]*" + string index)) + (setq type (match-string 1 string) + index (match-end 0)) + (if (eq index (string-match "\\([0-9A-Za-z]+\\)[ \t\n\r]*=[ \t\n\r]*" + string index)) + (setq type (match-string 1 string) + index (match-end 0)))) + (unless type + (error "Invalid type")) + (if (eq index (string-match + "\\([^,=+<>#;\\\"]\\|\\\\.\\)+" + string index)) + (setq index (match-end 0) + value (epg-decode-quotedstring (match-string 0 string))) + (if (eq index (string-match "#\\([0-9A-Fa-f]+\\)" string index)) + (setq index (match-end 0) + value (epg-decode-hexstring (match-string 1 string))) + (if (eq index (string-match "\"\\([^\\\"]\\|\\\\.\\)*\"" + string index)) + (setq index (match-end 0) + value (epg-decode-quotedstring (match-string 0 string)))))) + (if group + (if (stringp (car (car alist))) + (setcar alist (list (cons type value) (car alist))) + (setcar alist (cons (cons type value) (car alist)))) + (if (consp (car (car alist))) + (setcar alist (nreverse (car alist)))) + (setq alist (cons (cons type value) alist) + type nil + value nil)) + (if (eq index (string-match "[ \t\n\r]*\\([,;+]\\)" string index)) + (setq index (match-end 0) + group (eq (aref string (match-beginning 1)) ?+)))) + (nreverse alist))) + +(defun epg-decode-dn (alist) + "Convert ALIST returned by `epg-dn-from-string' to a human readable form. +Type names are resolved using `epg-dn-type-alist'." + (mapconcat + (lambda (rdn) + (if (stringp (car rdn)) + (let ((entry (assoc (car rdn) epg-dn-type-alist))) + (if entry + (format "%s=%s" (cdr entry) (cdr rdn)) + (format "%s=%s" (car rdn) (cdr rdn)))) + (concat "(" (epg-decode-dn rdn) ")"))) + alist + ", ")) + (provide 'epg) ;;; epg.el ends here