smime.el 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. ;;; smime.el --- S/MIME support library
  2. ;; Copyright (C) 2000-2015 Free Software Foundation, Inc.
  3. ;; Author: Simon Josefsson <simon@josefsson.org>
  4. ;; Keywords: SMIME X.509 PEM OpenSSL
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This library perform S/MIME operations from within Emacs.
  18. ;;
  19. ;; Functions for fetching certificates from public repositories are
  20. ;; provided, currently from DNS and LDAP.
  21. ;;
  22. ;; It uses OpenSSL (tested with version 0.9.5a and 0.9.6) for signing,
  23. ;; encryption and decryption.
  24. ;;
  25. ;; Some general knowledge of S/MIME, X.509, PKCS#12, PEM etc is
  26. ;; probably required to use this library in any useful way.
  27. ;; Especially, don't expect this library to buy security for you. If
  28. ;; you don't understand what you are doing, you're as likely to lose
  29. ;; security than gain any by using this library.
  30. ;;
  31. ;; This library is not intended to provide a "raw" API for S/MIME,
  32. ;; PKCSx or similar, it's intended to perform common operations
  33. ;; done on messages encoded in these formats. The terminology chosen
  34. ;; reflect this.
  35. ;;
  36. ;; The home of this file is in Gnus, but also available from
  37. ;; http://josefsson.org/smime.html.
  38. ;;; Quick introduction:
  39. ;; Get your S/MIME certificate from VeriSign or someplace. I used
  40. ;; Netscape to generate the key and certificate request and stuff, and
  41. ;; Netscape can export the key into PKCS#12 format.
  42. ;;
  43. ;; Enter OpenSSL. To be able to use this library, it need to have the
  44. ;; SMIME key readable in PEM format. OpenSSL is used to convert the
  45. ;; key:
  46. ;;
  47. ;; $ openssl pkcs12 -in mykey.p12 -clcerts -nodes > mykey.pem
  48. ;; ...
  49. ;;
  50. ;; Now, use M-x customize-variable smime-keys and add mykey.pem as
  51. ;; a key.
  52. ;;
  53. ;; Now you should be able to sign messages! Create a buffer and write
  54. ;; something and run M-x smime-sign-buffer RET RET and you should see
  55. ;; your message MIME armored and a signature. Encryption, M-x
  56. ;; smime-encrypt-buffer, should also work.
  57. ;;
  58. ;; To be able to verify messages you need to build up trust with
  59. ;; someone. Perhaps you trust the CA that issued your certificate, at
  60. ;; least I did, so I export it's certificates from my PKCS#12
  61. ;; certificate with:
  62. ;;
  63. ;; $ openssl pkcs12 -in mykey.p12 -cacerts -nodes > cacert.pem
  64. ;; ...
  65. ;;
  66. ;; Now, use M-x customize-variable smime-CAs and add cacert.pem as a
  67. ;; CA certificate.
  68. ;;
  69. ;; You should now be able to sign messages, and even verify messages
  70. ;; sent by others that use the same CA as you.
  71. ;; Bugs:
  72. ;;
  73. ;; Don't complain that this package doesn't do encrypted PEM files,
  74. ;; submit a patch instead. I store my keys in a safe place, so I
  75. ;; didn't need the encryption. Also, programming was made easier by
  76. ;; that decision. One might think that this even influenced were I
  77. ;; store my keys, and one would probably be right. :-)
  78. ;;
  79. ;; Update: Mathias Herberts sent the patch. However, it uses
  80. ;; environment variables to pass the password to OpenSSL, which is
  81. ;; slightly insecure. Hence a new todo: use a better -passin method.
  82. ;;
  83. ;; Cache password for e.g. 1h
  84. ;;
  85. ;; Suggestions and comments are appreciated, mail me at simon@josefsson.org.
  86. ;; begin rant
  87. ;;
  88. ;; I would include pointers to introductory text on concepts used in
  89. ;; this library here, but the material I've read are so horrible I
  90. ;; don't want to recommend them.
  91. ;;
  92. ;; Why can't someone write a simple introduction to all this stuff?
  93. ;; Until then, much of this resemble security by obscurity.
  94. ;;
  95. ;; Also, I'm not going to mention anything about the wonders of
  96. ;; cryptopolitics. Oops, I just did.
  97. ;;
  98. ;; end rant
  99. ;;; Revision history:
  100. ;; 2000-06-05 initial version, committed to Gnus CVS contrib/
  101. ;; 2000-10-28 retrieve certificates via DNS CERT RRs
  102. ;; 2001-10-14 posted to gnu.emacs.sources
  103. ;; 2005-02-13 retrieve certificates via LDAP
  104. ;;; Code:
  105. (require 'dig)
  106. (if (locate-library "password-cache")
  107. (require 'password-cache)
  108. (require 'password))
  109. (eval-when-compile (require 'cl))
  110. (eval-and-compile
  111. (cond
  112. ((fboundp 'replace-in-string)
  113. (defalias 'smime-replace-in-string 'replace-in-string))
  114. ((fboundp 'replace-regexp-in-string)
  115. (defun smime-replace-in-string (string regexp newtext &optional literal)
  116. "Replace all matches for REGEXP with NEWTEXT in STRING.
  117. If LITERAL is non-nil, insert NEWTEXT literally. Return a new
  118. string containing the replacements.
  119. This is a compatibility function for different Emacsen."
  120. (replace-regexp-in-string regexp newtext string nil literal)))))
  121. (defgroup smime nil
  122. "S/MIME configuration."
  123. :group 'mime)
  124. (defcustom smime-keys nil
  125. "*Map mail addresses to a file containing Certificate (and private key).
  126. The file is assumed to be in PEM format. You can also associate additional
  127. certificates to be sent with every message to each address."
  128. :type '(repeat (list (string :tag "Mail address")
  129. (file :tag "File name")
  130. (repeat :tag "Additional certificate files"
  131. (file :tag "File name"))))
  132. :group 'smime)
  133. (defcustom smime-CA-directory nil
  134. "*Directory containing certificates for CAs you trust.
  135. Directory should contain files (in PEM format) named to the X.509
  136. hash of the certificate. This can be done using OpenSSL such as:
  137. $ ln -s ca.pem \\=`openssl x509 -noout -hash -in ca.pem\\=`.0
  138. where `ca.pem' is the file containing a PEM encoded X.509 CA
  139. certificate."
  140. :type '(choice (const :tag "none" nil)
  141. directory)
  142. :group 'smime)
  143. (defcustom smime-CA-file nil
  144. "*Files containing certificates for CAs you trust.
  145. File should contain certificates in PEM format."
  146. :version "22.1"
  147. :type '(choice (const :tag "none" nil)
  148. file)
  149. :group 'smime)
  150. (defcustom smime-certificate-directory "~/Mail/certs/"
  151. "*Directory containing other people's certificates.
  152. It should contain files named to the X.509 hash of the certificate,
  153. and the files themselves should be in PEM format."
  154. ;The S/MIME library provide simple functionality for fetching
  155. ;certificates into this directory, so there is no need to populate it
  156. ;manually.
  157. :type 'directory
  158. :group 'smime)
  159. (defcustom smime-openssl-program
  160. (and (condition-case ()
  161. (eq 0 (call-process "openssl" nil nil nil "version"))
  162. (error nil))
  163. "openssl")
  164. "*Name of OpenSSL binary."
  165. :type 'string
  166. :group 'smime)
  167. ;; OpenSSL option to select the encryption cipher
  168. (defcustom smime-encrypt-cipher "-des3"
  169. "*Cipher algorithm used for encryption."
  170. :version "22.1"
  171. :type '(choice (const :tag "Triple DES" "-des3")
  172. (const :tag "DES" "-des")
  173. (const :tag "RC2 40 bits" "-rc2-40")
  174. (const :tag "RC2 64 bits" "-rc2-64")
  175. (const :tag "RC2 128 bits" "-rc2-128"))
  176. :group 'smime)
  177. (defcustom smime-crl-check nil
  178. "*Check revocation status of signers certificate using CRLs.
  179. Enabling this will have OpenSSL check the signers certificate
  180. against a certificate revocation list (CRL).
  181. For this to work the CRL must be up-to-date and since they are
  182. normally updated quite often (i.e., several times a day) you
  183. probably need some tool to keep them up-to-date. Unfortunately
  184. Gnus cannot do this for you.
  185. The CRL should either be appended (in PEM format) to your
  186. `smime-CA-file' or be located in a file (also in PEM format) in
  187. your `smime-certificate-directory' named to the X.509 hash of the
  188. certificate with .r0 as file name extension.
  189. At least OpenSSL version 0.9.7 is required for this to work."
  190. :type '(choice (const :tag "No check" nil)
  191. (const :tag "Check certificate" "-crl_check")
  192. (const :tag "Check certificate chain" "-crl_check_all"))
  193. :group 'smime)
  194. (defcustom smime-dns-server nil
  195. "*DNS server to query certificates from.
  196. If nil, use system defaults."
  197. :version "22.1"
  198. :type '(choice (const :tag "System defaults")
  199. string)
  200. :group 'smime)
  201. (defcustom smime-ldap-host-list nil
  202. "A list of LDAP hosts with S/MIME user certificates.
  203. If needed search base, binddn, passwd, etc. for the LDAP host
  204. must be set in `ldap-host-parameters-alist'."
  205. :type '(repeat (string :tag "Host name"))
  206. :version "23.1" ;; No Gnus
  207. :group 'smime)
  208. (defvar smime-details-buffer "*OpenSSL output*")
  209. ;; Use mm-util?
  210. (eval-and-compile
  211. (defalias 'smime-make-temp-file
  212. (if (fboundp 'make-temp-file)
  213. 'make-temp-file
  214. (lambda (prefix &optional dir-flag) ;; Simple implementation
  215. (expand-file-name
  216. (make-temp-name prefix)
  217. (if (fboundp 'temp-directory)
  218. (temp-directory)
  219. temporary-file-directory))))))
  220. ;; Password dialog function
  221. (declare-function password-read-and-add "password-cache" (prompt &optional key))
  222. (defun smime-ask-passphrase (&optional cache-key)
  223. "Asks the passphrase to unlock the secret key.
  224. If `cache-key' and `password-cache' is non-nil then cache the
  225. password under `cache-key'."
  226. (let ((passphrase
  227. (password-read-and-add
  228. "Passphrase for secret key (RET for no passphrase): " cache-key)))
  229. (if (string= passphrase "")
  230. nil
  231. passphrase)))
  232. ;; OpenSSL wrappers.
  233. (defun smime-call-openssl-region (b e buf &rest args)
  234. (case (apply 'call-process-region b e smime-openssl-program nil buf nil args)
  235. (0 t)
  236. (1 (message "OpenSSL: An error occurred parsing the command options.") nil)
  237. (2 (message "OpenSSL: One of the input files could not be read.") nil)
  238. (3 (message "OpenSSL: An error occurred creating the PKCS#7 file or when reading the MIME message.") nil)
  239. (4 (message "OpenSSL: An error occurred decrypting or verifying the message.") nil)
  240. (t (error "Unknown OpenSSL exitcode") nil)))
  241. (defun smime-make-certfiles (certfiles)
  242. (if certfiles
  243. (append (list "-certfile" (expand-file-name (car certfiles)))
  244. (smime-make-certfiles (cdr certfiles)))))
  245. ;; Sign+encrypt region
  246. (defun smime-sign-region (b e keyfile)
  247. "Sign region with certified key in KEYFILE.
  248. If signing fails, the buffer is not modified. Region is assumed to
  249. have proper MIME tags. KEYFILE is expected to contain a PEM encoded
  250. private key and certificate as its car, and a list of additional
  251. certificates to include in its caar. If no additional certificates is
  252. included, KEYFILE may be the file containing the PEM encoded private
  253. key and certificate itself."
  254. (smime-new-details-buffer)
  255. (let* ((certfiles (and (cdr-safe keyfile) (cadr keyfile)))
  256. (keyfile (or (car-safe keyfile) keyfile))
  257. (buffer (generate-new-buffer " *smime*"))
  258. (passphrase (smime-ask-passphrase (expand-file-name keyfile)))
  259. (tmpfile (smime-make-temp-file "smime")))
  260. (if passphrase
  261. (setenv "GNUS_SMIME_PASSPHRASE" passphrase))
  262. (prog1
  263. (when (prog1
  264. (apply 'smime-call-openssl-region b e (list buffer tmpfile)
  265. "smime" "-sign" "-signer" (expand-file-name keyfile)
  266. (append
  267. (smime-make-certfiles certfiles)
  268. (if passphrase
  269. (list "-passin" "env:GNUS_SMIME_PASSPHRASE"))))
  270. (if passphrase
  271. (setenv "GNUS_SMIME_PASSPHRASE" "" t))
  272. (with-current-buffer smime-details-buffer
  273. (insert-file-contents tmpfile)
  274. (delete-file tmpfile)))
  275. (delete-region b e)
  276. (insert-buffer-substring buffer)
  277. (goto-char b)
  278. (when (looking-at "^MIME-Version: 1.0$")
  279. (delete-region (point) (progn (forward-line 1) (point))))
  280. t)
  281. (with-current-buffer smime-details-buffer
  282. (goto-char (point-max))
  283. (insert-buffer-substring buffer))
  284. (kill-buffer buffer))))
  285. (defun smime-encrypt-region (b e certfiles)
  286. "Encrypt region for recipients specified in CERTFILES.
  287. If encryption fails, the buffer is not modified. Region is assumed to
  288. have proper MIME tags. CERTFILES is a list of filenames, each file
  289. is expected to contain of a PEM encoded certificate."
  290. (smime-new-details-buffer)
  291. (let ((buffer (generate-new-buffer " *smime*"))
  292. (tmpfile (smime-make-temp-file "smime")))
  293. (prog1
  294. (when (prog1
  295. (apply 'smime-call-openssl-region b e (list buffer tmpfile)
  296. "smime" "-encrypt" smime-encrypt-cipher
  297. (mapcar 'expand-file-name certfiles))
  298. (with-current-buffer smime-details-buffer
  299. (insert-file-contents tmpfile)
  300. (delete-file tmpfile)))
  301. (delete-region b e)
  302. (insert-buffer-substring buffer)
  303. (goto-char b)
  304. (when (looking-at "^MIME-Version: 1.0$")
  305. (delete-region (point) (progn (forward-line 1) (point))))
  306. t)
  307. (with-current-buffer smime-details-buffer
  308. (goto-char (point-max))
  309. (insert-buffer-substring buffer))
  310. (kill-buffer buffer))))
  311. ;; Sign+encrypt buffer
  312. (defun smime-sign-buffer (&optional keyfile buffer)
  313. "S/MIME sign BUFFER with key in KEYFILE.
  314. KEYFILE should contain a PEM encoded key and certificate."
  315. (interactive)
  316. (with-current-buffer (or buffer (current-buffer))
  317. (unless (smime-sign-region
  318. (point-min) (point-max)
  319. (if keyfile
  320. keyfile
  321. (smime-get-key-with-certs-by-email
  322. (gnus-completing-read
  323. "Sign using key"
  324. smime-keys nil (car-safe (car-safe smime-keys))))))
  325. (error "Signing failed"))))
  326. (defun smime-encrypt-buffer (&optional certfiles buffer)
  327. "S/MIME encrypt BUFFER for recipients specified in CERTFILES.
  328. CERTFILES is a list of filenames, each file is expected to consist of
  329. a PEM encoded key and certificate. Uses current buffer if BUFFER is
  330. nil."
  331. (interactive)
  332. (with-current-buffer (or buffer (current-buffer))
  333. (unless (smime-encrypt-region
  334. (point-min) (point-max)
  335. (or certfiles
  336. (list (read-file-name "Recipient's S/MIME certificate: "
  337. smime-certificate-directory nil))))
  338. (error "Encryption failed"))))
  339. ;; Verify+decrypt region
  340. (defun smime-verify-region (b e)
  341. "Verify S/MIME message in region between B and E.
  342. Returns non-nil on success.
  343. Any details (stdout and stderr) are left in the buffer specified by
  344. `smime-details-buffer'."
  345. (smime-new-details-buffer)
  346. (let ((CAs (append (if smime-CA-file
  347. (list "-CAfile"
  348. (expand-file-name smime-CA-file)))
  349. (if smime-CA-directory
  350. (list "-CApath"
  351. (expand-file-name smime-CA-directory))))))
  352. (unless CAs
  353. (error "No CA configured"))
  354. (if smime-crl-check
  355. (add-to-list 'CAs smime-crl-check))
  356. (if (apply 'smime-call-openssl-region b e (list smime-details-buffer t)
  357. "smime" "-verify" "-out" "/dev/null" CAs)
  358. t
  359. (insert-buffer-substring smime-details-buffer)
  360. nil)))
  361. (defun smime-noverify-region (b e)
  362. "Verify integrity of S/MIME message in region between B and E.
  363. Returns non-nil on success.
  364. Any details (stdout and stderr) are left in the buffer specified by
  365. `smime-details-buffer'."
  366. (smime-new-details-buffer)
  367. (if (apply 'smime-call-openssl-region b e (list smime-details-buffer t)
  368. "smime" "-verify" "-noverify" "-out" '("/dev/null"))
  369. t
  370. (insert-buffer-substring smime-details-buffer)
  371. nil))
  372. (defun smime-decrypt-region (b e keyfile &optional from)
  373. "Decrypt S/MIME message in region between B and E with key in KEYFILE.
  374. Optional FROM specifies sender's mail address.
  375. On success, replaces region with decrypted data and return non-nil.
  376. Any details (stderr on success, stdout and stderr on error) are left
  377. in the buffer specified by `smime-details-buffer'."
  378. (smime-new-details-buffer)
  379. (let ((buffer (generate-new-buffer " *smime*"))
  380. CAs (passphrase (smime-ask-passphrase (expand-file-name keyfile)))
  381. (tmpfile (smime-make-temp-file "smime")))
  382. (if passphrase
  383. (setenv "GNUS_SMIME_PASSPHRASE" passphrase))
  384. (if (prog1
  385. (apply 'smime-call-openssl-region b e
  386. (list buffer tmpfile)
  387. "smime" "-decrypt" "-recip" (expand-file-name keyfile)
  388. (if passphrase
  389. (list "-passin" "env:GNUS_SMIME_PASSPHRASE")))
  390. (if passphrase
  391. (setenv "GNUS_SMIME_PASSPHRASE" "" t))
  392. (with-current-buffer smime-details-buffer
  393. (insert-file-contents tmpfile)
  394. (delete-file tmpfile)))
  395. (progn
  396. (delete-region b e)
  397. (when from
  398. (insert "From: " from "\n"))
  399. (insert-buffer-substring buffer)
  400. (kill-buffer buffer)
  401. t)
  402. (with-current-buffer smime-details-buffer
  403. (insert-buffer-substring buffer))
  404. (kill-buffer buffer)
  405. (delete-region b e)
  406. (insert-buffer-substring smime-details-buffer)
  407. nil)))
  408. ;; Verify+Decrypt buffer
  409. (defun smime-verify-buffer (&optional buffer)
  410. "Verify integrity of S/MIME message in BUFFER.
  411. Uses current buffer if BUFFER is nil. Returns non-nil on success.
  412. Any details (stdout and stderr) are left in the buffer specified by
  413. `smime-details-buffer'."
  414. (interactive)
  415. (with-current-buffer (or buffer (current-buffer))
  416. (smime-verify-region (point-min) (point-max))))
  417. (defun smime-noverify-buffer (&optional buffer)
  418. "Verify integrity of S/MIME message in BUFFER.
  419. Does NOT verify validity of certificate (only message integrity).
  420. Uses current buffer if BUFFER is nil. Returns non-nil on success.
  421. Any details (stdout and stderr) are left in the buffer specified by
  422. `smime-details-buffer'."
  423. (interactive)
  424. (with-current-buffer (or buffer (current-buffer))
  425. (smime-noverify-region (point-min) (point-max))))
  426. (defun smime-decrypt-buffer (&optional buffer keyfile)
  427. "Decrypt S/MIME message in BUFFER using KEYFILE.
  428. Uses current buffer if BUFFER is nil, and query user of KEYFILE if it's nil.
  429. On success, replaces data in buffer and return non-nil.
  430. Any details (stderr on success, stdout and stderr on error) are left
  431. in the buffer specified by `smime-details-buffer'."
  432. (interactive)
  433. (with-current-buffer (or buffer (current-buffer))
  434. (smime-decrypt-region
  435. (point-min) (point-max)
  436. (expand-file-name
  437. (or keyfile
  438. (smime-get-key-by-email
  439. (gnus-completing-read
  440. "Decipher using key"
  441. smime-keys nil (car-safe (car-safe smime-keys)))))))))
  442. ;; Various operations
  443. (defun smime-new-details-buffer ()
  444. (with-current-buffer (get-buffer-create smime-details-buffer)
  445. (erase-buffer)))
  446. (defun smime-pkcs7-region (b e)
  447. "Convert S/MIME message between points B and E into a PKCS7 message."
  448. (smime-new-details-buffer)
  449. (when (smime-call-openssl-region b e smime-details-buffer "smime" "-pk7out")
  450. (delete-region b e)
  451. (insert-buffer-substring smime-details-buffer)
  452. t))
  453. (defun smime-pkcs7-certificates-region (b e)
  454. "Extract any certificates enclosed in PKCS7 message between points B and E."
  455. (smime-new-details-buffer)
  456. (when (smime-call-openssl-region
  457. b e smime-details-buffer "pkcs7" "-print_certs" "-text")
  458. (delete-region b e)
  459. (insert-buffer-substring smime-details-buffer)
  460. t))
  461. (defun smime-pkcs7-email-region (b e)
  462. "Get email addresses contained in certificate between points B and E.
  463. A string or a list of strings is returned."
  464. (smime-new-details-buffer)
  465. (when (smime-call-openssl-region
  466. b e smime-details-buffer "x509" "-email" "-noout")
  467. (delete-region b e)
  468. (insert-buffer-substring smime-details-buffer)
  469. t))
  470. ;; Utility functions
  471. (defun smime-get-certfiles (keyfile keys)
  472. (if keys
  473. (let ((curkey (car keys))
  474. (otherkeys (cdr keys)))
  475. (if (string= keyfile (cadr curkey))
  476. (caddr curkey)
  477. (smime-get-certfiles keyfile otherkeys)))))
  478. (defun smime-buffer-as-string-region (b e)
  479. "Return each line in region between B and E as a list of strings."
  480. (save-excursion
  481. (goto-char b)
  482. (let (res)
  483. (while (< (point) e)
  484. (let ((str (buffer-substring (point) (point-at-eol))))
  485. (unless (string= "" str)
  486. (push str res)))
  487. (forward-line))
  488. res)))
  489. ;; Find certificates
  490. (defun smime-mail-to-domain (mailaddr)
  491. (if (string-match "@" mailaddr)
  492. (replace-match "." 'fixedcase 'literal mailaddr)
  493. mailaddr))
  494. (defun smime-cert-by-dns (mail)
  495. "Find certificate via DNS for address MAIL."
  496. (let* ((dig-dns-server smime-dns-server)
  497. (digbuf (dig-invoke (smime-mail-to-domain mail) "cert" nil nil "+vc"))
  498. (retbuf (generate-new-buffer (format "*certificate for %s*" mail)))
  499. (certrr (with-current-buffer digbuf
  500. (dig-extract-rr (smime-mail-to-domain mail) "cert")))
  501. (cert (and certrr (dig-rr-get-pkix-cert certrr))))
  502. (if cert
  503. (with-current-buffer retbuf
  504. (insert "-----BEGIN CERTIFICATE-----\n")
  505. (let ((i 0) (len (length cert)))
  506. (while (> (- len 64) i)
  507. (insert (substring cert i (+ i 64)) "\n")
  508. (setq i (+ i 64)))
  509. (insert (substring cert i len) "\n"))
  510. (insert "-----END CERTIFICATE-----\n"))
  511. (kill-buffer retbuf)
  512. (setq retbuf nil))
  513. (kill-buffer digbuf)
  514. retbuf))
  515. (declare-function ldap-search "ldap"
  516. (filter &optional host attributes attrsonly withdn))
  517. (defun smime-cert-by-ldap-1 (mail host)
  518. "Get certificate for MAIL from the ldap server at HOST."
  519. (let ((ldapresult
  520. (funcall
  521. (if (featurep 'xemacs)
  522. (progn
  523. (require 'smime-ldap)
  524. 'smime-ldap-search)
  525. (progn
  526. (require 'ldap)
  527. 'ldap-search))
  528. (concat "mail=" mail)
  529. host '("userCertificate") nil))
  530. (retbuf (generate-new-buffer (format "*certificate for %s*" mail)))
  531. cert)
  532. (if (and (>= (length ldapresult) 1)
  533. (> (length (cadaar ldapresult)) 0))
  534. (with-current-buffer retbuf
  535. ;; Certificates on LDAP servers _should_ be in DER format,
  536. ;; but there are some servers out there that distributes the
  537. ;; certificates in PEM format (with or without
  538. ;; header/footer) so we try to handle them anyway.
  539. (if (or (string= (substring (cadaar ldapresult) 0 27)
  540. "-----BEGIN CERTIFICATE-----")
  541. (string= (substring (cadaar ldapresult) 0 3)
  542. "MII"))
  543. (setq cert
  544. (smime-replace-in-string
  545. (cadaar ldapresult)
  546. (concat "\\(\n\\|\r\\|-----BEGIN CERTIFICATE-----\\|"
  547. "-----END CERTIFICATE-----\\)")
  548. "" t))
  549. (setq cert (base64-encode-string (cadaar ldapresult) t)))
  550. (insert "-----BEGIN CERTIFICATE-----\n")
  551. (let ((i 0) (len (length cert)))
  552. (while (> (- len 64) i)
  553. (insert (substring cert i (+ i 64)) "\n")
  554. (setq i (+ i 64)))
  555. (insert (substring cert i len) "\n"))
  556. (insert "-----END CERTIFICATE-----\n"))
  557. (kill-buffer retbuf)
  558. (setq retbuf nil))
  559. retbuf))
  560. (defun smime-cert-by-ldap (mail)
  561. "Find certificate via LDAP for address MAIL."
  562. (if smime-ldap-host-list
  563. (catch 'certbuf
  564. (dolist (host smime-ldap-host-list)
  565. (let ((retbuf (smime-cert-by-ldap-1 mail host)))
  566. (when retbuf
  567. (throw 'certbuf retbuf)))))))
  568. ;; User interface.
  569. (defvar smime-buffer "*SMIME*")
  570. (defvar smime-mode-map
  571. (let ((map (make-sparse-keymap)))
  572. (suppress-keymap map)
  573. (define-key map "q" 'smime-exit)
  574. (define-key map "f" 'smime-certificate-info)
  575. map))
  576. (autoload 'gnus-completing-read "gnus-util")
  577. (put 'smime-mode 'mode-class 'special)
  578. (define-derived-mode smime-mode fundamental-mode ;special-mode
  579. "SMIME"
  580. "Major mode for browsing, viewing and fetching certificates.
  581. All normal editing commands are switched off.
  582. \\<smime-mode-map>
  583. The following commands are available:
  584. \\{smime-mode-map}"
  585. (setq mode-line-process nil)
  586. (buffer-disable-undo)
  587. (setq truncate-lines t)
  588. (setq buffer-read-only t))
  589. (defun smime-certificate-info (certfile)
  590. (interactive "fCertificate file: ")
  591. (let ((buffer (get-buffer-create (format "*certificate %s*" certfile))))
  592. (switch-to-buffer buffer)
  593. (erase-buffer)
  594. (call-process smime-openssl-program nil buffer 'display
  595. "x509" "-in" (expand-file-name certfile) "-text")
  596. (fundamental-mode)
  597. (set-buffer-modified-p nil)
  598. (setq buffer-read-only t)
  599. (goto-char (point-min))))
  600. (defun smime-draw-buffer ()
  601. (with-current-buffer smime-buffer
  602. (let (buffer-read-only)
  603. (erase-buffer)
  604. (insert "\nYour keys:\n")
  605. (dolist (key smime-keys)
  606. (insert
  607. (format "\t\t%s: %s\n" (car key) (cadr key))))
  608. (insert "\nTrusted Certificate Authorities:\n")
  609. (insert "\nKnown Certificates:\n"))))
  610. (defun smime ()
  611. "Go to the SMIME buffer."
  612. (interactive)
  613. (unless (get-buffer smime-buffer)
  614. (with-current-buffer (get-buffer-create smime-buffer)
  615. (smime-mode)))
  616. (smime-draw-buffer)
  617. (switch-to-buffer smime-buffer))
  618. (defun smime-exit ()
  619. "Quit the S/MIME buffer."
  620. (interactive)
  621. (kill-buffer (current-buffer)))
  622. ;; Other functions
  623. (defun smime-get-key-by-email (email)
  624. (cadr (assoc email smime-keys)))
  625. (defun smime-get-key-with-certs-by-email (email)
  626. (cdr (assoc email smime-keys)))
  627. (provide 'smime)
  628. ;;; smime.el ends here