mml-sec.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. ;;; mml-sec.el --- A package with security functions for MML documents
  2. ;; Copyright (C) 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Simon Josefsson <simon@josefsson.org>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;; Code:
  17. (eval-when-compile (require 'cl))
  18. (autoload 'mml2015-sign "mml2015")
  19. (autoload 'mml2015-encrypt "mml2015")
  20. (autoload 'mml1991-sign "mml1991")
  21. (autoload 'mml1991-encrypt "mml1991")
  22. (autoload 'message-goto-body "message")
  23. (autoload 'mml-insert-tag "mml")
  24. (autoload 'mml-smime-sign "mml-smime")
  25. (autoload 'mml-smime-encrypt "mml-smime")
  26. (autoload 'mml-smime-sign-query "mml-smime")
  27. (autoload 'mml-smime-encrypt-query "mml-smime")
  28. (autoload 'mml-smime-verify "mml-smime")
  29. (autoload 'mml-smime-verify-test "mml-smime")
  30. (defvar mml-sign-alist
  31. '(("smime" mml-smime-sign-buffer mml-smime-sign-query)
  32. ("pgp" mml-pgp-sign-buffer list)
  33. ("pgpauto" mml-pgpauto-sign-buffer list)
  34. ("pgpmime" mml-pgpmime-sign-buffer list))
  35. "Alist of MIME signer functions.")
  36. (defcustom mml-default-sign-method "pgpmime"
  37. "Default sign method.
  38. The string must have an entry in `mml-sign-alist'."
  39. :version "22.1"
  40. :type '(choice (const "smime")
  41. (const "pgp")
  42. (const "pgpauto")
  43. (const "pgpmime")
  44. string)
  45. :group 'message)
  46. (defvar mml-encrypt-alist
  47. '(("smime" mml-smime-encrypt-buffer mml-smime-encrypt-query)
  48. ("pgp" mml-pgp-encrypt-buffer list)
  49. ("pgpauto" mml-pgpauto-sign-buffer list)
  50. ("pgpmime" mml-pgpmime-encrypt-buffer list))
  51. "Alist of MIME encryption functions.")
  52. (defcustom mml-default-encrypt-method "pgpmime"
  53. "Default encryption method.
  54. The string must have an entry in `mml-encrypt-alist'."
  55. :version "22.1"
  56. :type '(choice (const "smime")
  57. (const "pgp")
  58. (const "pgpauto")
  59. (const "pgpmime")
  60. string)
  61. :group 'message)
  62. (defcustom mml-signencrypt-style-alist
  63. '(("smime" separate)
  64. ("pgp" combined)
  65. ("pgpauto" combined)
  66. ("pgpmime" combined))
  67. "Alist specifying if `signencrypt' results in two separate operations or not.
  68. The first entry indicates the MML security type, valid entries include
  69. the strings \"smime\", \"pgp\", and \"pgpmime\". The second entry is
  70. a symbol `separate' or `combined' where `separate' means that MML signs
  71. and encrypt messages in a two step process, and `combined' means that MML
  72. signs and encrypt the message in one step.
  73. Note that the output generated by using a `combined' mode is NOT
  74. understood by all PGP implementations, in particular PGP version
  75. 2 does not support it! See Info node `(message)Security' for
  76. details."
  77. :version "22.1"
  78. :group 'message
  79. :type '(repeat (list (choice (const :tag "S/MIME" "smime")
  80. (const :tag "PGP" "pgp")
  81. (const :tag "PGP/MIME" "pgpmime")
  82. (string :tag "User defined"))
  83. (choice (const :tag "Separate" separate)
  84. (const :tag "Combined" combined)))))
  85. (defcustom mml-secure-verbose nil
  86. "If non-nil, ask the user about the current operation more verbosely."
  87. :group 'message
  88. :type 'boolean)
  89. (defcustom mml-secure-cache-passphrase
  90. (if (boundp 'password-cache)
  91. password-cache
  92. t)
  93. "If t, cache passphrase."
  94. :group 'message
  95. :type 'boolean)
  96. (defcustom mml-secure-passphrase-cache-expiry
  97. (if (boundp 'password-cache-expiry)
  98. password-cache-expiry
  99. 16)
  100. "How many seconds the passphrase is cached.
  101. Whether the passphrase is cached at all is controlled by
  102. `mml-secure-cache-passphrase'."
  103. :group 'message
  104. :type 'integer)
  105. ;;; Configuration/helper functions
  106. (defun mml-signencrypt-style (method &optional style)
  107. "Function for setting/getting the signencrypt-style used. Takes two
  108. arguments, the method (e.g. \"pgp\") and optionally the mode
  109. \(e.g. combined). If the mode is omitted, the current value is returned.
  110. For example, if you prefer to use combined sign & encrypt with
  111. smime, putting the following in your Gnus startup file will
  112. enable that behavior:
  113. \(mml-set-signencrypt-style \"smime\" combined)
  114. You can also customize or set `mml-signencrypt-style-alist' instead."
  115. (let ((style-item (assoc method mml-signencrypt-style-alist)))
  116. (if style-item
  117. (if (or (eq style 'separate)
  118. (eq style 'combined))
  119. ;; valid style setting?
  120. (setf (second style-item) style)
  121. ;; otherwise, just return the current value
  122. (second style-item))
  123. (message "Warning, attempt to set invalid signencrypt style"))))
  124. ;;; Security functions
  125. (defun mml-smime-sign-buffer (cont)
  126. (or (mml-smime-sign cont)
  127. (error "Signing failed... inspect message logs for errors")))
  128. (defun mml-smime-encrypt-buffer (cont &optional sign)
  129. (when sign
  130. (message "Combined sign and encrypt S/MIME not support yet")
  131. (sit-for 1))
  132. (or (mml-smime-encrypt cont)
  133. (error "Encryption failed... inspect message logs for errors")))
  134. (defun mml-pgp-sign-buffer (cont)
  135. (or (mml1991-sign cont)
  136. (error "Signing failed... inspect message logs for errors")))
  137. (defun mml-pgp-encrypt-buffer (cont &optional sign)
  138. (or (mml1991-encrypt cont sign)
  139. (error "Encryption failed... inspect message logs for errors")))
  140. (defun mml-pgpmime-sign-buffer (cont)
  141. (or (mml2015-sign cont)
  142. (error "Signing failed... inspect message logs for errors")))
  143. (defun mml-pgpmime-encrypt-buffer (cont &optional sign)
  144. (or (mml2015-encrypt cont sign)
  145. (error "Encryption failed... inspect message logs for errors")))
  146. (defun mml-pgpauto-sign-buffer (cont)
  147. (message-goto-body)
  148. (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
  149. (mml2015-sign cont)
  150. (mml1991-sign cont))
  151. (error "Encryption failed... inspect message logs for errors")))
  152. (defun mml-pgpauto-encrypt-buffer (cont &optional sign)
  153. (message-goto-body)
  154. (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
  155. (mml2015-encrypt cont sign)
  156. (mml1991-encrypt cont sign))
  157. (error "Encryption failed... inspect message logs for errors")))
  158. (defun mml-secure-part (method &optional sign)
  159. (save-excursion
  160. (let ((tags (funcall (nth 2 (assoc method (if sign mml-sign-alist
  161. mml-encrypt-alist))))))
  162. (cond ((re-search-backward
  163. "<#\\(multipart\\|part\\|external\\|mml\\)" nil t)
  164. (goto-char (match-end 0))
  165. (insert (if sign " sign=" " encrypt=") method)
  166. (while tags
  167. (let ((key (pop tags))
  168. (value (pop tags)))
  169. (when value
  170. ;; Quote VALUE if it contains suspicious characters.
  171. (when (string-match "[\"'\\~/*;() \t\n]" value)
  172. (setq value (prin1-to-string value)))
  173. (insert (format " %s=%s" key value))))))
  174. ((or (re-search-backward
  175. (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
  176. (re-search-forward
  177. (concat "^" (regexp-quote mail-header-separator) "\n") nil t))
  178. (goto-char (match-end 0))
  179. (apply 'mml-insert-tag 'part (cons (if sign 'sign 'encrypt)
  180. (cons method tags))))
  181. (t (error "The message is corrupted. No mail header separator"))))))
  182. (defvar mml-secure-method
  183. (if (equal mml-default-encrypt-method mml-default-sign-method)
  184. mml-default-sign-method
  185. "pgpmime")
  186. "Current security method. Internal variable.")
  187. (defun mml-secure-sign (&optional method)
  188. "Add MML tags to sign this MML part.
  189. Use METHOD if given. Else use `mml-secure-method' or
  190. `mml-default-sign-method'."
  191. (interactive)
  192. (mml-secure-part
  193. (or method mml-secure-method mml-default-sign-method)
  194. 'sign))
  195. (defun mml-secure-encrypt (&optional method)
  196. "Add MML tags to encrypt this MML part.
  197. Use METHOD if given. Else use `mml-secure-method' or
  198. `mml-default-sign-method'."
  199. (interactive)
  200. (mml-secure-part
  201. (or method mml-secure-method mml-default-sign-method)))
  202. (defun mml-secure-sign-pgp ()
  203. "Add MML tags to PGP sign this MML part."
  204. (interactive)
  205. (mml-secure-part "pgp" 'sign))
  206. (defun mml-secure-sign-pgpauto ()
  207. "Add MML tags to PGP-auto sign this MML part."
  208. (interactive)
  209. (mml-secure-part "pgpauto" 'sign))
  210. (defun mml-secure-sign-pgpmime ()
  211. "Add MML tags to PGP/MIME sign this MML part."
  212. (interactive)
  213. (mml-secure-part "pgpmime" 'sign))
  214. (defun mml-secure-sign-smime ()
  215. "Add MML tags to S/MIME sign this MML part."
  216. (interactive)
  217. (mml-secure-part "smime" 'sign))
  218. (defun mml-secure-encrypt-pgp ()
  219. "Add MML tags to PGP encrypt this MML part."
  220. (interactive)
  221. (mml-secure-part "pgp"))
  222. (defun mml-secure-encrypt-pgpmime ()
  223. "Add MML tags to PGP/MIME encrypt this MML part."
  224. (interactive)
  225. (mml-secure-part "pgpmime"))
  226. (defun mml-secure-encrypt-smime ()
  227. "Add MML tags to S/MIME encrypt this MML part."
  228. (interactive)
  229. (mml-secure-part "smime"))
  230. ;; defuns that add the proper <#secure ...> tag to the top of the message body
  231. (defun mml-secure-message (method &optional modesym)
  232. (let ((mode (prin1-to-string modesym))
  233. (tags (append
  234. (if (or (eq modesym 'sign)
  235. (eq modesym 'signencrypt))
  236. (funcall (nth 2 (assoc method mml-sign-alist))))
  237. (if (or (eq modesym 'encrypt)
  238. (eq modesym 'signencrypt))
  239. (funcall (nth 2 (assoc method mml-encrypt-alist))))))
  240. insert-loc)
  241. (mml-unsecure-message)
  242. (save-excursion
  243. (goto-char (point-min))
  244. (cond ((re-search-forward
  245. (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
  246. (goto-char (setq insert-loc (match-end 0)))
  247. (unless (looking-at "<#secure")
  248. (apply 'mml-insert-tag
  249. 'secure 'method method 'mode mode tags)))
  250. (t (error
  251. "The message is corrupted. No mail header separator"))))
  252. (when (eql insert-loc (point))
  253. (forward-line 1))))
  254. (defun mml-unsecure-message ()
  255. "Remove security related MML tags from message."
  256. (interactive)
  257. (save-excursion
  258. (goto-char (point-max))
  259. (when (re-search-backward "^<#secure.*>\n" nil t)
  260. (delete-region (match-beginning 0) (match-end 0)))))
  261. (defun mml-secure-message-sign (&optional method)
  262. "Add MML tags to sign the entire message.
  263. Use METHOD if given. Else use `mml-secure-method' or
  264. `mml-default-sign-method'."
  265. (interactive)
  266. (mml-secure-message
  267. (or method mml-secure-method mml-default-sign-method)
  268. 'sign))
  269. (defun mml-secure-message-sign-encrypt (&optional method)
  270. "Add MML tag to sign and encrypt the entire message.
  271. Use METHOD if given. Else use `mml-secure-method' or
  272. `mml-default-sign-method'."
  273. (interactive)
  274. (mml-secure-message
  275. (or method mml-secure-method mml-default-sign-method)
  276. 'signencrypt))
  277. (defun mml-secure-message-encrypt (&optional method)
  278. "Add MML tag to encrypt the entire message.
  279. Use METHOD if given. Else use `mml-secure-method' or
  280. `mml-default-sign-method'."
  281. (interactive)
  282. (mml-secure-message
  283. (or method mml-secure-method mml-default-sign-method)
  284. 'encrypt))
  285. (defun mml-secure-message-sign-smime ()
  286. "Add MML tag to encrypt/sign the entire message."
  287. (interactive)
  288. (mml-secure-message "smime" 'sign))
  289. (defun mml-secure-message-sign-pgp ()
  290. "Add MML tag to encrypt/sign the entire message."
  291. (interactive)
  292. (mml-secure-message "pgp" 'sign))
  293. (defun mml-secure-message-sign-pgpmime ()
  294. "Add MML tag to encrypt/sign the entire message."
  295. (interactive)
  296. (mml-secure-message "pgpmime" 'sign))
  297. (defun mml-secure-message-sign-pgpauto ()
  298. "Add MML tag to encrypt/sign the entire message."
  299. (interactive)
  300. (mml-secure-message "pgpauto" 'sign))
  301. (defun mml-secure-message-encrypt-smime (&optional dontsign)
  302. "Add MML tag to encrypt and sign the entire message.
  303. If called with a prefix argument, only encrypt (do NOT sign)."
  304. (interactive "P")
  305. (mml-secure-message "smime" (if dontsign 'encrypt 'signencrypt)))
  306. (defun mml-secure-message-encrypt-pgp (&optional dontsign)
  307. "Add MML tag to encrypt and sign the entire message.
  308. If called with a prefix argument, only encrypt (do NOT sign)."
  309. (interactive "P")
  310. (mml-secure-message "pgp" (if dontsign 'encrypt 'signencrypt)))
  311. (defun mml-secure-message-encrypt-pgpmime (&optional dontsign)
  312. "Add MML tag to encrypt and sign the entire message.
  313. If called with a prefix argument, only encrypt (do NOT sign)."
  314. (interactive "P")
  315. (mml-secure-message "pgpmime" (if dontsign 'encrypt 'signencrypt)))
  316. (defun mml-secure-message-encrypt-pgpauto (&optional dontsign)
  317. "Add MML tag to encrypt and sign the entire message.
  318. If called with a prefix argument, only encrypt (do NOT sign)."
  319. (interactive "P")
  320. (mml-secure-message "pgpauto" (if dontsign 'encrypt 'signencrypt)))
  321. (provide 'mml-sec)
  322. ;;; mml-sec.el ends here