Debian Codegraph @

mew-smime.el

raw
1 ;;; mew-smime.el --- S/MIME for Mew
2
3 ;; Author: Kazu Yamamoto <[email protected]>
4 ;; Created: Nov 12, 2004
5
6 ;;; Code:
7
8 (require 'mew)
9
10 ;; for gpgsm only
11
12 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13 ;;;
14 ;;; S/MIME program
15 ;;;
16
17 (defvar mew-prog-smime "gpgsm")
18 (defvar mew-smime-ver nil)
19
20 (defvar mew-prog-smime-options '("--include-certs" "3" "--status-fd" "1"))
21
22 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
23 ;;;
24 ;;; S/MIME check
25 ;;;
26
27 (defun mew-smime-setup ()
28 (if (mew-which-exec mew-prog-smime)
29 (setq mew-smime-ver t)))
30
31 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
32 ;;;
33 ;;; S/MIME signing
34 ;;;
35
36 (defvar mew-smime-type "smime-type")
37 (defvar mew-smime-file-name "smime")
38 (defvar mew-smime-signature-suffix ".p7s") ;; application/pkcs7-signature
39 (defvar mew-smime-mime-suffix ".p7m") ;; application/pkcs7-mime
40
41 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
42 ;;;
43 ;;; Password
44 ;;;
45
46 (defun mew-smime-passtag () "GPGSM")
47
48 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
49 ;;;
50 ;;; Micalg
51 ;;;
52
53 (defvar mew-smime-hash-alist
54 '(("1" . "md5")
55 ("2" . "sha1")
56 ("3" . "ripemd160")
57 ("5" . "md2")
58 ("6" . "tiger192")
59 ("7" . "haval-5-160")
60 ("8" . "sha256")
61 ("9" . "sha384")
62 ("10" . "sha512")))
63
64 (defun mew-smime-get-micalg ()
65 (with-temp-buffer
66 (insert mew-smime-string)
67 (goto-char (point-min))
68 (if (re-search-forward "SIG_CREATED [A-Z] [0-9]+ \\([0-9]+\\)" nil t)
69 (cdr (assoc (mew-match-string 1) mew-smime-hash-alist))
70 "sha1")))
71
72 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
73 ;;;
74 ;;; Internal variables
75 ;;;
76
77 (defvar mew-smime-running nil)
78 (defvar mew-smime-string nil)
79 (defvar mew-smime-failure nil)
80
81 (defvar mew-smime-decrypt-msg nil)
82 (defvar mew-smime-sign-msg nil)
83
84 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
85 ;;;
86 ;;; Filter and sentinel
87 ;;;
88
89 (defvar mew-smime-prompt-enter-pass "Enter S/MIME pass phrase: ")
90
91 (defun mew-smime-passphrase ()
92 (let ((prompt mew-smime-prompt-enter-pass)
93 (tag (mew-smime-passtag)))
94 (mew-input-passwd prompt tag)))
95
96 (defvar mew-smime-msg-enter-pass "Enter passphrase:")
97 (defvar mew-smime-msg-bad-pass ": Bad")
98
99 (defvar mew-smime-result-sec-succ "S/MIME decrypted")
100 (defvar mew-smime-result-pass "Pass phrase is wrong")
101
102 (defun mew-smime-debug (label string)
103 (when (mew-debug 'smime)
104 (with-current-buffer (get-buffer-create mew-buffer-debug)
105 (goto-char (point-max))
106 (insert (format "\n<%s>\n%s\n" label string)))))
107
108 (defun mew-smime-process-filter (process string)
109 (save-excursion
110 ;; sign or decrypt
111 (mew-smime-debug "S/MIME filter" string)
112 (setq mew-smime-string (concat mew-smime-string string))
113 (cond
114 ;; pass phrase for sign or decrypt
115 ((string-match mew-smime-msg-enter-pass string)
116 (process-send-string process (format "%s\n" (mew-smime-passphrase))))
117
118 ;; pass phrases were wrong three times
119 ((string-match mew-smime-msg-bad-pass string)
120 (setq mew-smime-failure t) ;; password failure
121 (mew-passwd-set-passwd (mew-smime-passtag) nil)) ;; cancel anyway
122
123 ((string-match "INV_RECP \\([0-9]*\\) \\([^ \n]*\\)" string)
124 (setq mew-smime-failure (cdr (assoc (mew-match-string 1 string) mew-smime-inv-recp-alist)))
125 (setq mew-smime-failure (concat mew-smime-failure " for " (mew-match-string 2 string)))))))
126
127 (defun mew-smime-process-sentinel (_process _event)
128 (save-excursion
129 (let ((decrypted mew-smime-result-sec-succ)
130 (msg ""))
131 (if (not mew-smime-failure)
132 (cond
133 ((eq mew-smime-running 'decrypting)
134 (setq mew-smime-decrypt-msg decrypted))
135 ((eq mew-smime-running 'signing)
136 (setq mew-smime-sign-msg nil)))
137 (cond
138 ;; sign or decrypt
139 ((eq mew-smime-failure t)
140 (setq msg mew-smime-result-pass))
141 ((stringp mew-smime-failure)
142 (setq msg mew-smime-failure)))
143 (cond
144 ((eq mew-smime-running 'decrypting)
145 (setq mew-smime-decrypt-msg msg))
146 ((eq mew-smime-running 'signing)
147 (setq mew-smime-sign-msg msg))))
148 (setq mew-smime-running nil))))
149
150 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
151 ;;;
152 ;;; Checking
153 ;;;
154
155 (defun mew-smime-verify-check ()
156 (let (addr warning result trust ret)
157 (goto-char (point-min))
158 (if (re-search-forward "EMail=\\(.*\\)" nil t)
159 (setq addr (mew-match-string 1)))
160 (goto-char (point-min))
161 (cond
162 ((search-forward "GOODSIG" nil t)
163 (setq result "Good S/MIME sign"))
164 ((search-forward "EXPSIG" nil t)
165 (setq result "Good S/MIME sign")
166 (setq warning "signature has expired"))
167 ((search-forward "EXPKEYSIG" nil t)
168 (setq result "Good S/MIME sign")
169 (setq warning "certificate has expired"))
170 ((search-forward "REVKEYSIG" nil t)
171 (setq result "Good S/MIME sign")
172 (setq warning "certificate has been revoked"))
173 ((search-forward "BADSIG" nil t)
174 (setq result "BAD S/MIME sign"))
175 ((search-forward "ERRORSIG" nil t)
176 (setq result "BAD S/MIME sign")) ;; xxx
177 (t
178 (setq result "BAD S/MIME sign")))
179 (goto-char (point-min))
180 ;; xxx error code check for TRUST_UNDEFINED/NEVER?
181 (if (re-search-forward "TRUST_\\([A-Z]*\\)" nil t)
182 (setq trust (mew-match-string 1)))
183 (setq ret result)
184 (if addr (setq ret (concat ret " <" addr ">")))
185 (if trust (setq ret (concat ret " " trust)))
186 (if warning (setq ret (concat ret " -" warning)))
187 ret))
188
189 (defvar mew-smime-inv-recp-alist
190 '(( "0" . "No specific reason given")
191 ( "1" . "Not Found")
192 ( "2" . "Ambigious specification")
193 ( "3" . "Wrong key usage")
194 ( "4" . "Key revoked")
195 ( "5" . "Key expired")
196 ( "6" . "No CRL known")
197 ( "7" . "CRL too old")
198 ( "8" . "Policy mismatch")
199 ( "9" . "Not a secret key")
200 ("10" . "Key not trusted")
201 ("11" . "Missing certifciate")))
202
203 (defun mew-smime-encrypt-check ()
204 (let (ret) ;; this should be nil
205 (goto-char (point-min))
206 (when (re-search-forward "INV_RECP \\([0-9]*\\) \\([^ \n]*\\)" nil t)
207 (setq ret (cdr (assoc (mew-match-string 1) mew-smime-inv-recp-alist)))
208 (setq ret (concat ret " for " (mew-match-string 2))))
209 ret))
210
211 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
212 ;;;
213 ;;; S/MIME short cut
214 ;;;
215
216 (defun mew-smime-sign-message (&optional arg)
217 "Sign the entire draft with S/MIME. Input your passphrase."
218 (interactive "P")
219 (mew-smime-encode-message 'smime-signature arg))
220
221 (defun mew-smime-encrypt-message ()
222 "Encrypt the entire draft with S/MIME."
223 (interactive)
224 (mew-smime-encode-message 'smime-encryption))
225
226 (defun mew-smime-sign-encrypt-message (&optional _arg)
227 "Sign then encrypt the entire draft with S/MIME. Input your passphrase."
228 (interactive "P")
229 (mew-smime-encode-message 'smime-signature-encryption))
230
231 (defun mew-smime-encrypt-sign-message (&optional _arg)
232 "Encrypt then sign the entire draft with S/MIME. Input your passphrase."
233 (interactive "P")
234 (mew-smime-encode-message 'smime-encryption-signature))
235
236 (defun mew-smime-encode-message (type &optional ask-signer)
237 (if (null mew-smime-ver)
238 (message "%s does not exist" mew-prog-smime)
239 (if (and ask-signer (string-match "signature" (symbol-name type)))
240 (mew-draft-make-message type (car (mew-input-address "Who's key?: ")))
241 (mew-draft-make-message type))))
242
243 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
244 ;;;
245 ;;; Encoding
246 ;;;
247
248 (defun mew-encode-smime (type cte decrypters)
249 (let ((file1 (mew-save-transfer-form (point-min) (point-max) nil cte))
250 file2 errmsg file2-errmsg)
251 (cond
252 ((string= type mew-ct-smm-sig)
253 (setq file2-errmsg (mew-smime-sign file1)))
254 ((string= type mew-ct-smm-enc)
255 (setq file2-errmsg (mew-smime-encrypt file1 decrypters))))
256 (mew-set '(file2 errmsg) file2-errmsg)
257 (if errmsg
258 (progn
259 (mew-delete-file file1)
260 (mew-delete-file file2)
261 (mew-tinfo-set-privacy-err t)
262 (mew-tinfo-set-privacy-type nil)
263 (mew-draft-mode-name)
264 (mew-encode-error errmsg))
265 (mew-encode-singlepart
266 (mew-encode-syntax-single file2
267 (list mew-ct-smm
268 (list mew-smime-type type)
269 (list "name" (concat mew-smime-file-name mew-smime-mime-suffix)))
270 mew-b64)))
271 (mew-delete-file file1)
272 (mew-delete-file file2)))
273
274 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
275 ;;;
276 ;;; Signature
277 ;;;
278
279 (defun mew-smime-sign (file1)
280 (setq mew-smime-running 'signing)
281 (setq mew-smime-string nil)
282 (setq mew-smime-sign-msg nil)
283 (setq mew-smime-failure nil)
284 (let* ((process-connection-type mew-connection-type2)
285 (file2 (mew-make-temp-name))
286 (prog mew-prog-smime)
287 (opts `("--sign" ,@mew-prog-smime-options
288 "--local-user" ,mew-inherit-encode-smime-signer
289 "--output" ,file2 ,file1))
290 process)
291 (message "S/MIME signing...")
292 (setq process (apply 'mew-start-process-lang "GPGSM sign" nil prog opts))
293 (mew-set-process-cs process mew-cs-autoconv mew-cs-dummy)
294 (set-process-filter process 'mew-smime-process-filter)
295 (set-process-sentinel process 'mew-smime-process-sentinel)
296 (mew-rendezvous mew-smime-running)
297 (unless (file-exists-p file2) ;; for unpredictable error
298 (mew-passwd-set-passwd (mew-smime-passtag) nil))
299 (message "S/MIME signing...done")
300 (list file2 mew-smime-sign-msg)))
301
302 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
303 ;;;
304 ;;; Encryption
305 ;;;
306
307 (defvar mew-smime-cipher "AES")
308
309 (defun mew-smime-encrypt (file1 decrypters)
310 (let* ((process-connection-type mew-connection-type2)
311 (file2 (mew-make-temp-name))
312 (decs (mew-gpg-roption decrypters "--recipient"))
313 (prog mew-prog-smime)
314 (opts `("--encrypt" ,@mew-prog-smime-options
315 ,@decs
316 "--cipher-algo" ,mew-smime-cipher
317 "--output" ,file2 ,file1))
318 check)
319 (message "S/MIME encrypting...")
320 (with-temp-buffer
321 (apply 'mew-call-process-lang prog nil t nil opts)
322 (setq check (mew-smime-encrypt-check)))
323 (message "S/MIME encrypting...done")
324 (list file2 check)))
325
326 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
327 ;;;
328 ;;; Decoding
329 ;;;
330
331 (defun mew-decode-smime (syntax cnt)
332 (let* ((ctl (mew-syntax-get-ct syntax))
333 (type (mew-syntax-get-param ctl mew-smime-type))
334 file1 file2 syntax2 result privacy)
335 ;; xxx smime-type is optional, sigh
336 (if (null type) (setq type mew-ct-smm-enc))
337 (mew-syntax-set-ct syntax mew-type-apo)
338 (setq file1 (mew-save-decode-form syntax))
339 (setq file2 (mew-make-temp-name))
340 (delete-region (point-min) (point-max))
341 (cond
342 ((mew-case-equal type mew-ct-smm-sig)
343 (setq result (mew-smime-verify file1 file2)))
344 ((mew-case-equal type mew-ct-smm-enc)
345 (setq result (mew-smime-decrypt file1 file2)))
346 (t
347 (setq syntax2 syntax)
348 (mew-syntax-set-ct syntax mew-type-apo)))
349 (if (and (file-exists-p file2)
350 (> (mew-file-get-size file2) 0)) ;; shell creates file2 anyway
351 (mew-flet
352 (mew-insert-file-contents file2)
353 (put-text-property (point-min) (point-max) 'mew-noncontents nil)
354 (mew-decode-crlf-magic))
355 (insert "\n") ;; CT: text/plain; charset=us-ascii
356 (insert (format "%s could not be decrypted.\n" mew-ct-smm))
357 (mew-xinfo-set-not-decrypted t))
358 ;; Throw away garbage
359 (mew-delete-file file1)
360 (mew-delete-file file2)
361 (goto-char (point-min))
362 (setq syntax2 (mew-decode-singlepart cnt nil nil))
363 (setq privacy (mew-syntax-get-privacy syntax2))
364 (if privacy (setq result (concat result "\n\t")))
365 (mew-syntax-set-privacy
366 syntax2 (cons (mew-make-privacy-dinfo :ct mew-ct-smm :proto type :result result) privacy))
367 syntax2))
368
369 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
370 ;;;
371 ;;; Verification
372 ;;;
373
374 (defun mew-smime-verify (file1 file2)
375 (let ((prog mew-prog-smime)
376 (opts `("--verify" ,@mew-prog-smime-options
377 "--output" ,file2 ,file1))
378 result)
379 (message "S/MIME verifying...")
380 (with-temp-buffer
381 (apply 'mew-call-process-lang prog nil t nil opts)
382 (setq result (mew-smime-verify-check)))
383 (message "S/MIME verifying...done")
384 result))
385
386 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
387 ;;;
388 ;;; Decription
389 ;;;
390
391 (defun mew-smime-decrypt (file1 file2)
392 (setq mew-smime-running 'decrypting)
393 (setq mew-smime-string nil)
394 (setq mew-smime-decrypt-msg nil)
395 (setq mew-smime-failure nil)
396 (let ((process-connection-type mew-connection-type2)
397 (prog mew-prog-smime)
398 (opts `("--decrypt"
399 "--output" ,file2 ,file1))
400 process)
401 (message "S/MIME decrypting...")
402 (setq process (apply 'mew-start-process-lang "GPGSM decrypt" nil prog opts))
403 (mew-set-process-cs process mew-cs-autoconv mew-cs-dummy)
404 (set-process-filter process 'mew-smime-process-filter)
405 (set-process-sentinel process 'mew-smime-process-sentinel)
406 (mew-rendezvous mew-smime-running)
407 (unless (file-exists-p file2) ;; for unpredictable error
408 (mew-passwd-set-passwd (mew-smime-passtag) nil))
409 (message "S/MIME decrypting...done")
410 mew-smime-decrypt-msg))
411
412 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
413 ;;;
414 ;;; Multipart/Signed
415 ;;;
416
417 (defun mew-smime-detach-verify (file1 file2)
418 (let ((prog mew-prog-smime)
419 (opts `("--verify" ,@mew-prog-smime-options
420 ,file2 ,file1))
421 ret)
422 (message "S/MIME verifying...")
423 (with-temp-buffer
424 (apply 'mew-call-process-lang prog nil t nil opts)
425 (setq ret (mew-smime-verify-check)))
426 (message "S/MIME verifying...done")
427 ret))
428
429 (defun mew-smime-detach-sign (file1)
430 (setq mew-smime-running 'signing)
431 (setq mew-smime-string nil)
432 (setq mew-smime-sign-msg nil)
433 (setq mew-smime-failure nil)
434 (let* ((process-connection-type mew-connection-type2)
435 (file2 (concat (mew-make-temp-name mew-smime-file-name)
436 mew-smime-signature-suffix))
437 (prog mew-prog-smime)
438 (opts `("--detach-sign" ,@mew-prog-smime-options
439 "--local-user" ,mew-inherit-encode-smime-signer
440 "--output" ,file2 ,file1))
441 process)
442 (message "S/MIME signing...")
443 (setq process (apply 'mew-start-process-lang "GPGSM sign" nil prog opts))
444 (mew-set-process-cs process mew-cs-autoconv mew-cs-dummy)
445 (set-process-filter process 'mew-smime-process-filter)
446 (set-process-sentinel process 'mew-smime-process-sentinel)
447 (mew-rendezvous mew-smime-running)
448 (unless (file-exists-p file2) ;; for unpredictable error
449 (mew-passwd-set-passwd (mew-smime-passtag) nil))
450 (message "S/MIME signing...done")
451 (list file2 mew-b64 (mew-smime-get-micalg) mew-smime-sign-msg)))
452
453 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
454 ;;;
455 ;;; GPG
456 ;;;
457
458 (defun mew-gpg-roption (decrypters roption)
459 (if (and mew-encrypt-to-myself
460 (not (member mew-inherit-encode-smime-signer decrypters)))
461 (setq decrypters (cons mew-inherit-encode-smime-signer decrypters)))
462 (setq decrypters
463 (mapcar (lambda (x)
464 (if (string-match "^[^<].*@" x) (concat "<" x ">") x))
465 decrypters))
466 (let (decs)
467 (dolist (decrypter decrypters)
468 (setq decs (cons decrypter (cons roption decs))))
469 (nreverse decs)))
470
471 (provide 'mew-smime)
472
473 ;;; Copyright Notice:
474
475 ;; Copyright (C) 2004-2015 Mew developing team.
476 ;; All rights reserved.
477
478 ;; Redistribution and use in source and binary forms, with or without
479 ;; modification, are permitted provided that the following conditions
480 ;; are met:
481 ;;
482 ;; 1. Redistributions of source code must retain the above copyright
483 ;; notice, this list of conditions and the following disclaimer.
484 ;; 2. Redistributions in binary form must reproduce the above copyright
485 ;; notice, this list of conditions and the following disclaimer in the
486 ;; documentation and/or other materials provided with the distribution.
487 ;; 3. Neither the name of the team nor the names of its contributors
488 ;; may be used to endorse or promote products derived from this software
489 ;; without specific prior written permission.
490 ;;
491 ;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
492 ;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
493 ;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
494 ;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
495 ;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
496 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
497 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
498 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
499 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
500 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
501 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
502
503 ;;; mew-smime.el ends here